uv_sysfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * This file supports the /sys/firmware/sgi_uv topology tree on HPE UV.
  4. *
  5. * Copyright (c) 2020 Hewlett Packard Enterprise. All Rights Reserved.
  6. * Copyright (c) Justin Ernst
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/device.h>
  11. #include <linux/slab.h>
  12. #include <linux/kobject.h>
  13. #include <asm/uv/bios.h>
  14. #include <asm/uv/uv.h>
  15. #include <asm/uv/uv_hub.h>
  16. #include <asm/uv/uv_geo.h>
  17. #define INVALID_CNODE -1
  18. struct kobject *sgi_uv_kobj;
  19. static struct kset *uv_pcibus_kset;
  20. static struct kset *uv_hubs_kset;
  21. static struct uv_bios_hub_info *hub_buf;
  22. static struct uv_bios_port_info **port_buf;
  23. static struct uv_hub **uv_hubs;
  24. static struct uv_pci_top_obj **uv_pci_objs;
  25. static int num_pci_lines;
  26. static int num_cnodes;
  27. static int *prev_obj_to_cnode;
  28. static int uv_bios_obj_cnt;
  29. static signed short uv_master_nasid = -1;
  30. static void *uv_biosheap;
  31. static const char *uv_type_string(void)
  32. {
  33. if (is_uv5_hub())
  34. return "9.0";
  35. else if (is_uv4a_hub())
  36. return "7.1";
  37. else if (is_uv4_hub())
  38. return "7.0";
  39. else if (is_uv3_hub())
  40. return "5.0";
  41. else if (is_uv2_hub())
  42. return "3.0";
  43. else if (uv_get_hubless_system())
  44. return "0.1";
  45. else
  46. return "unknown";
  47. }
  48. static int ordinal_to_nasid(int ordinal)
  49. {
  50. if (ordinal < num_cnodes && ordinal >= 0)
  51. return UV_PNODE_TO_NASID(uv_blade_to_pnode(ordinal));
  52. else
  53. return -1;
  54. }
  55. static union geoid_u cnode_to_geoid(int cnode)
  56. {
  57. union geoid_u geoid;
  58. uv_bios_get_geoinfo(ordinal_to_nasid(cnode), (u64)sizeof(union geoid_u), (u64 *)&geoid);
  59. return geoid;
  60. }
  61. static int location_to_bpos(char *location, int *rack, int *slot, int *blade)
  62. {
  63. char type, r, b, h;
  64. int idb, idh;
  65. if (sscanf(location, "%c%03d%c%02d%c%2d%c%d",
  66. &r, rack, &type, slot, &b, &idb, &h, &idh) != 8)
  67. return -1;
  68. *blade = idb * 2 + idh;
  69. return 0;
  70. }
  71. static int cache_obj_to_cnode(struct uv_bios_hub_info *obj)
  72. {
  73. int cnode;
  74. union geoid_u geoid;
  75. int obj_rack, obj_slot, obj_blade;
  76. int rack, slot, blade;
  77. if (!obj->f.fields.this_part && !obj->f.fields.is_shared)
  78. return 0;
  79. if (location_to_bpos(obj->location, &obj_rack, &obj_slot, &obj_blade))
  80. return -1;
  81. for (cnode = 0; cnode < num_cnodes; cnode++) {
  82. geoid = cnode_to_geoid(cnode);
  83. rack = geo_rack(geoid);
  84. slot = geo_slot(geoid);
  85. blade = geo_blade(geoid);
  86. if (obj_rack == rack && obj_slot == slot && obj_blade == blade)
  87. prev_obj_to_cnode[obj->id] = cnode;
  88. }
  89. return 0;
  90. }
  91. static int get_obj_to_cnode(int obj_id)
  92. {
  93. return prev_obj_to_cnode[obj_id];
  94. }
  95. struct uv_hub {
  96. struct kobject kobj;
  97. struct uv_bios_hub_info *hub_info;
  98. struct uv_port **ports;
  99. };
  100. #define to_uv_hub(kobj_ptr) container_of(kobj_ptr, struct uv_hub, kobj)
  101. static ssize_t hub_name_show(struct uv_bios_hub_info *hub_info, char *buf)
  102. {
  103. return scnprintf(buf, PAGE_SIZE, "%s\n", hub_info->name);
  104. }
  105. static ssize_t hub_location_show(struct uv_bios_hub_info *hub_info, char *buf)
  106. {
  107. return scnprintf(buf, PAGE_SIZE, "%s\n", hub_info->location);
  108. }
  109. static ssize_t hub_partition_show(struct uv_bios_hub_info *hub_info, char *buf)
  110. {
  111. return sprintf(buf, "%d\n", hub_info->f.fields.this_part);
  112. }
  113. static ssize_t hub_shared_show(struct uv_bios_hub_info *hub_info, char *buf)
  114. {
  115. return sprintf(buf, "%d\n", hub_info->f.fields.is_shared);
  116. }
  117. static ssize_t hub_nasid_show(struct uv_bios_hub_info *hub_info, char *buf)
  118. {
  119. int cnode = get_obj_to_cnode(hub_info->id);
  120. return sprintf(buf, "%d\n", ordinal_to_nasid(cnode));
  121. }
  122. static ssize_t hub_cnode_show(struct uv_bios_hub_info *hub_info, char *buf)
  123. {
  124. return sprintf(buf, "%d\n", get_obj_to_cnode(hub_info->id));
  125. }
  126. struct hub_sysfs_entry {
  127. struct attribute attr;
  128. ssize_t (*show)(struct uv_bios_hub_info *hub_info, char *buf);
  129. ssize_t (*store)(struct uv_bios_hub_info *hub_info, const char *buf, size_t sz);
  130. };
  131. static struct hub_sysfs_entry name_attribute =
  132. __ATTR(name, 0444, hub_name_show, NULL);
  133. static struct hub_sysfs_entry location_attribute =
  134. __ATTR(location, 0444, hub_location_show, NULL);
  135. static struct hub_sysfs_entry partition_attribute =
  136. __ATTR(this_partition, 0444, hub_partition_show, NULL);
  137. static struct hub_sysfs_entry shared_attribute =
  138. __ATTR(shared, 0444, hub_shared_show, NULL);
  139. static struct hub_sysfs_entry nasid_attribute =
  140. __ATTR(nasid, 0444, hub_nasid_show, NULL);
  141. static struct hub_sysfs_entry cnode_attribute =
  142. __ATTR(cnode, 0444, hub_cnode_show, NULL);
  143. static struct attribute *uv_hub_attrs[] = {
  144. &name_attribute.attr,
  145. &location_attribute.attr,
  146. &partition_attribute.attr,
  147. &shared_attribute.attr,
  148. &nasid_attribute.attr,
  149. &cnode_attribute.attr,
  150. NULL,
  151. };
  152. ATTRIBUTE_GROUPS(uv_hub);
  153. static void hub_release(struct kobject *kobj)
  154. {
  155. struct uv_hub *hub = to_uv_hub(kobj);
  156. kfree(hub);
  157. }
  158. static ssize_t hub_type_show(struct kobject *kobj, struct attribute *attr,
  159. char *buf)
  160. {
  161. struct uv_hub *hub = to_uv_hub(kobj);
  162. struct uv_bios_hub_info *bios_hub_info = hub->hub_info;
  163. struct hub_sysfs_entry *entry;
  164. entry = container_of(attr, struct hub_sysfs_entry, attr);
  165. if (!entry->show)
  166. return -EIO;
  167. return entry->show(bios_hub_info, buf);
  168. }
  169. static const struct sysfs_ops hub_sysfs_ops = {
  170. .show = hub_type_show,
  171. };
  172. static struct kobj_type hub_attr_type = {
  173. .release = hub_release,
  174. .sysfs_ops = &hub_sysfs_ops,
  175. .default_groups = uv_hub_groups,
  176. };
  177. static int uv_hubs_init(void)
  178. {
  179. s64 biosr;
  180. u64 sz;
  181. int i, ret;
  182. prev_obj_to_cnode = kmalloc_array(uv_bios_obj_cnt, sizeof(*prev_obj_to_cnode),
  183. GFP_KERNEL);
  184. if (!prev_obj_to_cnode)
  185. return -ENOMEM;
  186. for (i = 0; i < uv_bios_obj_cnt; i++)
  187. prev_obj_to_cnode[i] = INVALID_CNODE;
  188. uv_hubs_kset = kset_create_and_add("hubs", NULL, sgi_uv_kobj);
  189. if (!uv_hubs_kset) {
  190. ret = -ENOMEM;
  191. goto err_hubs_kset;
  192. }
  193. sz = uv_bios_obj_cnt * sizeof(*hub_buf);
  194. hub_buf = kzalloc(sz, GFP_KERNEL);
  195. if (!hub_buf) {
  196. ret = -ENOMEM;
  197. goto err_hub_buf;
  198. }
  199. biosr = uv_bios_enum_objs((u64)uv_master_nasid, sz, (u64 *)hub_buf);
  200. if (biosr) {
  201. ret = -EINVAL;
  202. goto err_enum_objs;
  203. }
  204. uv_hubs = kcalloc(uv_bios_obj_cnt, sizeof(*uv_hubs), GFP_KERNEL);
  205. if (!uv_hubs) {
  206. ret = -ENOMEM;
  207. goto err_enum_objs;
  208. }
  209. for (i = 0; i < uv_bios_obj_cnt; i++) {
  210. uv_hubs[i] = kzalloc(sizeof(*uv_hubs[i]), GFP_KERNEL);
  211. if (!uv_hubs[i]) {
  212. i--;
  213. ret = -ENOMEM;
  214. goto err_hubs;
  215. }
  216. uv_hubs[i]->hub_info = &hub_buf[i];
  217. cache_obj_to_cnode(uv_hubs[i]->hub_info);
  218. uv_hubs[i]->kobj.kset = uv_hubs_kset;
  219. ret = kobject_init_and_add(&uv_hubs[i]->kobj, &hub_attr_type,
  220. NULL, "hub_%u", hub_buf[i].id);
  221. if (ret)
  222. goto err_hubs;
  223. kobject_uevent(&uv_hubs[i]->kobj, KOBJ_ADD);
  224. }
  225. return 0;
  226. err_hubs:
  227. for (; i >= 0; i--)
  228. kobject_put(&uv_hubs[i]->kobj);
  229. kfree(uv_hubs);
  230. err_enum_objs:
  231. kfree(hub_buf);
  232. err_hub_buf:
  233. kset_unregister(uv_hubs_kset);
  234. err_hubs_kset:
  235. kfree(prev_obj_to_cnode);
  236. return ret;
  237. }
  238. static void uv_hubs_exit(void)
  239. {
  240. int i;
  241. for (i = 0; i < uv_bios_obj_cnt; i++)
  242. kobject_put(&uv_hubs[i]->kobj);
  243. kfree(uv_hubs);
  244. kfree(hub_buf);
  245. kset_unregister(uv_hubs_kset);
  246. kfree(prev_obj_to_cnode);
  247. }
  248. struct uv_port {
  249. struct kobject kobj;
  250. struct uv_bios_port_info *port_info;
  251. };
  252. #define to_uv_port(kobj_ptr) container_of(kobj_ptr, struct uv_port, kobj)
  253. static ssize_t uv_port_conn_hub_show(struct uv_bios_port_info *port, char *buf)
  254. {
  255. return sprintf(buf, "%d\n", port->conn_id);
  256. }
  257. static ssize_t uv_port_conn_port_show(struct uv_bios_port_info *port, char *buf)
  258. {
  259. return sprintf(buf, "%d\n", port->conn_port);
  260. }
  261. struct uv_port_sysfs_entry {
  262. struct attribute attr;
  263. ssize_t (*show)(struct uv_bios_port_info *port_info, char *buf);
  264. ssize_t (*store)(struct uv_bios_port_info *port_info, const char *buf, size_t size);
  265. };
  266. static struct uv_port_sysfs_entry uv_port_conn_hub_attribute =
  267. __ATTR(conn_hub, 0444, uv_port_conn_hub_show, NULL);
  268. static struct uv_port_sysfs_entry uv_port_conn_port_attribute =
  269. __ATTR(conn_port, 0444, uv_port_conn_port_show, NULL);
  270. static struct attribute *uv_port_attrs[] = {
  271. &uv_port_conn_hub_attribute.attr,
  272. &uv_port_conn_port_attribute.attr,
  273. NULL,
  274. };
  275. ATTRIBUTE_GROUPS(uv_port);
  276. static void uv_port_release(struct kobject *kobj)
  277. {
  278. struct uv_port *port = to_uv_port(kobj);
  279. kfree(port);
  280. }
  281. static ssize_t uv_port_type_show(struct kobject *kobj, struct attribute *attr,
  282. char *buf)
  283. {
  284. struct uv_port *port = to_uv_port(kobj);
  285. struct uv_bios_port_info *port_info = port->port_info;
  286. struct uv_port_sysfs_entry *entry;
  287. entry = container_of(attr, struct uv_port_sysfs_entry, attr);
  288. if (!entry->show)
  289. return -EIO;
  290. return entry->show(port_info, buf);
  291. }
  292. static const struct sysfs_ops uv_port_sysfs_ops = {
  293. .show = uv_port_type_show,
  294. };
  295. static struct kobj_type uv_port_attr_type = {
  296. .release = uv_port_release,
  297. .sysfs_ops = &uv_port_sysfs_ops,
  298. .default_groups = uv_port_groups,
  299. };
  300. static int uv_ports_init(void)
  301. {
  302. s64 biosr;
  303. int j = 0, k = 0, ret, sz;
  304. port_buf = kcalloc(uv_bios_obj_cnt, sizeof(*port_buf), GFP_KERNEL);
  305. if (!port_buf)
  306. return -ENOMEM;
  307. for (j = 0; j < uv_bios_obj_cnt; j++) {
  308. sz = hub_buf[j].ports * sizeof(*port_buf[j]);
  309. port_buf[j] = kzalloc(sz, GFP_KERNEL);
  310. if (!port_buf[j]) {
  311. ret = -ENOMEM;
  312. j--;
  313. goto err_port_info;
  314. }
  315. biosr = uv_bios_enum_ports((u64)uv_master_nasid, (u64)hub_buf[j].id, sz,
  316. (u64 *)port_buf[j]);
  317. if (biosr) {
  318. ret = -EINVAL;
  319. goto err_port_info;
  320. }
  321. }
  322. for (j = 0; j < uv_bios_obj_cnt; j++) {
  323. uv_hubs[j]->ports = kcalloc(hub_buf[j].ports,
  324. sizeof(*uv_hubs[j]->ports), GFP_KERNEL);
  325. if (!uv_hubs[j]->ports) {
  326. ret = -ENOMEM;
  327. j--;
  328. goto err_ports;
  329. }
  330. }
  331. for (j = 0; j < uv_bios_obj_cnt; j++) {
  332. for (k = 0; k < hub_buf[j].ports; k++) {
  333. uv_hubs[j]->ports[k] = kzalloc(sizeof(*uv_hubs[j]->ports[k]), GFP_KERNEL);
  334. if (!uv_hubs[j]->ports[k]) {
  335. ret = -ENOMEM;
  336. k--;
  337. goto err_kobj_ports;
  338. }
  339. uv_hubs[j]->ports[k]->port_info = &port_buf[j][k];
  340. ret = kobject_init_and_add(&uv_hubs[j]->ports[k]->kobj, &uv_port_attr_type,
  341. &uv_hubs[j]->kobj, "port_%d", port_buf[j][k].port);
  342. if (ret)
  343. goto err_kobj_ports;
  344. kobject_uevent(&uv_hubs[j]->ports[k]->kobj, KOBJ_ADD);
  345. }
  346. }
  347. return 0;
  348. err_kobj_ports:
  349. for (; j >= 0; j--) {
  350. for (; k >= 0; k--)
  351. kobject_put(&uv_hubs[j]->ports[k]->kobj);
  352. if (j > 0)
  353. k = hub_buf[j-1].ports - 1;
  354. }
  355. j = uv_bios_obj_cnt - 1;
  356. err_ports:
  357. for (; j >= 0; j--)
  358. kfree(uv_hubs[j]->ports);
  359. j = uv_bios_obj_cnt - 1;
  360. err_port_info:
  361. for (; j >= 0; j--)
  362. kfree(port_buf[j]);
  363. kfree(port_buf);
  364. return ret;
  365. }
  366. static void uv_ports_exit(void)
  367. {
  368. int j, k;
  369. for (j = 0; j < uv_bios_obj_cnt; j++) {
  370. for (k = hub_buf[j].ports - 1; k >= 0; k--)
  371. kobject_put(&uv_hubs[j]->ports[k]->kobj);
  372. }
  373. for (j = 0; j < uv_bios_obj_cnt; j++) {
  374. kfree(uv_hubs[j]->ports);
  375. kfree(port_buf[j]);
  376. }
  377. kfree(port_buf);
  378. }
  379. struct uv_pci_top_obj {
  380. struct kobject kobj;
  381. char *type;
  382. char *location;
  383. int iio_stack;
  384. char *ppb_addr;
  385. int slot;
  386. };
  387. #define to_uv_pci_top_obj(kobj_ptr) container_of(kobj_ptr, struct uv_pci_top_obj, kobj)
  388. static ssize_t uv_pci_type_show(struct uv_pci_top_obj *top_obj, char *buf)
  389. {
  390. return scnprintf(buf, PAGE_SIZE, "%s\n", top_obj->type);
  391. }
  392. static ssize_t uv_pci_location_show(struct uv_pci_top_obj *top_obj, char *buf)
  393. {
  394. return scnprintf(buf, PAGE_SIZE, "%s\n", top_obj->location);
  395. }
  396. static ssize_t uv_pci_iio_stack_show(struct uv_pci_top_obj *top_obj, char *buf)
  397. {
  398. return sprintf(buf, "%d\n", top_obj->iio_stack);
  399. }
  400. static ssize_t uv_pci_ppb_addr_show(struct uv_pci_top_obj *top_obj, char *buf)
  401. {
  402. return scnprintf(buf, PAGE_SIZE, "%s\n", top_obj->ppb_addr);
  403. }
  404. static ssize_t uv_pci_slot_show(struct uv_pci_top_obj *top_obj, char *buf)
  405. {
  406. return sprintf(buf, "%d\n", top_obj->slot);
  407. }
  408. struct uv_pci_top_sysfs_entry {
  409. struct attribute attr;
  410. ssize_t (*show)(struct uv_pci_top_obj *top_obj, char *buf);
  411. ssize_t (*store)(struct uv_pci_top_obj *top_obj, const char *buf, size_t size);
  412. };
  413. static struct uv_pci_top_sysfs_entry uv_pci_type_attribute =
  414. __ATTR(type, 0444, uv_pci_type_show, NULL);
  415. static struct uv_pci_top_sysfs_entry uv_pci_location_attribute =
  416. __ATTR(location, 0444, uv_pci_location_show, NULL);
  417. static struct uv_pci_top_sysfs_entry uv_pci_iio_stack_attribute =
  418. __ATTR(iio_stack, 0444, uv_pci_iio_stack_show, NULL);
  419. static struct uv_pci_top_sysfs_entry uv_pci_ppb_addr_attribute =
  420. __ATTR(ppb_addr, 0444, uv_pci_ppb_addr_show, NULL);
  421. static struct uv_pci_top_sysfs_entry uv_pci_slot_attribute =
  422. __ATTR(slot, 0444, uv_pci_slot_show, NULL);
  423. static void uv_pci_top_release(struct kobject *kobj)
  424. {
  425. struct uv_pci_top_obj *top_obj = to_uv_pci_top_obj(kobj);
  426. kfree(top_obj->type);
  427. kfree(top_obj->location);
  428. kfree(top_obj->ppb_addr);
  429. kfree(top_obj);
  430. }
  431. static ssize_t pci_top_type_show(struct kobject *kobj,
  432. struct attribute *attr, char *buf)
  433. {
  434. struct uv_pci_top_obj *top_obj = to_uv_pci_top_obj(kobj);
  435. struct uv_pci_top_sysfs_entry *entry;
  436. entry = container_of(attr, struct uv_pci_top_sysfs_entry, attr);
  437. if (!entry->show)
  438. return -EIO;
  439. return entry->show(top_obj, buf);
  440. }
  441. static const struct sysfs_ops uv_pci_top_sysfs_ops = {
  442. .show = pci_top_type_show,
  443. };
  444. static struct kobj_type uv_pci_top_attr_type = {
  445. .release = uv_pci_top_release,
  446. .sysfs_ops = &uv_pci_top_sysfs_ops,
  447. };
  448. static int init_pci_top_obj(struct uv_pci_top_obj *top_obj, char *line)
  449. {
  450. char *start;
  451. char type[11], location[14], ppb_addr[15];
  452. int str_cnt, ret;
  453. unsigned int tmp_match[2];
  454. // Minimum line length
  455. if (strlen(line) < 36)
  456. return -EINVAL;
  457. //Line must match format "pcibus %4x:%2x" to be valid
  458. str_cnt = sscanf(line, "pcibus %4x:%2x", &tmp_match[0], &tmp_match[1]);
  459. if (str_cnt < 2)
  460. return -EINVAL;
  461. /* Connect pcibus to segment:bus number with '_'
  462. * to concatenate name tokens.
  463. * pcibus 0000:00 ... -> pcibus_0000:00 ...
  464. */
  465. line[6] = '_';
  466. /* Null terminate after the concatencated name tokens
  467. * to produce kobj name string.
  468. */
  469. line[14] = '\0';
  470. // Use start to index after name tokens string for remainder of line info.
  471. start = &line[15];
  472. top_obj->iio_stack = -1;
  473. top_obj->slot = -1;
  474. /* r001i01b00h0 BASE IO (IIO Stack 0)
  475. * r001i01b00h1 PCIe IO (IIO Stack 1)
  476. * r001i01b03h1 PCIe SLOT
  477. * r001i01b00h0 NODE IO
  478. * r001i01b00h0 Riser
  479. * (IIO Stack #) may not be present.
  480. */
  481. if (start[0] == 'r') {
  482. str_cnt = sscanf(start, "%13s %10[^(] %*s %*s %d)",
  483. location, type, &top_obj->iio_stack);
  484. if (str_cnt < 2)
  485. return -EINVAL;
  486. top_obj->type = kstrdup(type, GFP_KERNEL);
  487. if (!top_obj->type)
  488. return -ENOMEM;
  489. top_obj->location = kstrdup(location, GFP_KERNEL);
  490. if (!top_obj->location) {
  491. kfree(top_obj->type);
  492. return -ENOMEM;
  493. }
  494. }
  495. /* PPB at 0000:80:00.00 (slot 3)
  496. * (slot #) may not be present.
  497. */
  498. else if (start[0] == 'P') {
  499. str_cnt = sscanf(start, "%10s %*s %14s %*s %d)",
  500. type, ppb_addr, &top_obj->slot);
  501. if (str_cnt < 2)
  502. return -EINVAL;
  503. top_obj->type = kstrdup(type, GFP_KERNEL);
  504. if (!top_obj->type)
  505. return -ENOMEM;
  506. top_obj->ppb_addr = kstrdup(ppb_addr, GFP_KERNEL);
  507. if (!top_obj->ppb_addr) {
  508. kfree(top_obj->type);
  509. return -ENOMEM;
  510. }
  511. } else
  512. return -EINVAL;
  513. top_obj->kobj.kset = uv_pcibus_kset;
  514. ret = kobject_init_and_add(&top_obj->kobj, &uv_pci_top_attr_type, NULL, "%s", line);
  515. if (ret)
  516. goto err_add_sysfs;
  517. if (top_obj->type) {
  518. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_type_attribute.attr);
  519. if (ret)
  520. goto err_add_sysfs;
  521. }
  522. if (top_obj->location) {
  523. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_location_attribute.attr);
  524. if (ret)
  525. goto err_add_sysfs;
  526. }
  527. if (top_obj->iio_stack >= 0) {
  528. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_iio_stack_attribute.attr);
  529. if (ret)
  530. goto err_add_sysfs;
  531. }
  532. if (top_obj->ppb_addr) {
  533. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_ppb_addr_attribute.attr);
  534. if (ret)
  535. goto err_add_sysfs;
  536. }
  537. if (top_obj->slot >= 0) {
  538. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_slot_attribute.attr);
  539. if (ret)
  540. goto err_add_sysfs;
  541. }
  542. kobject_uevent(&top_obj->kobj, KOBJ_ADD);
  543. return 0;
  544. err_add_sysfs:
  545. kobject_put(&top_obj->kobj);
  546. return ret;
  547. }
  548. static int pci_topology_init(void)
  549. {
  550. char *pci_top_str, *start, *found, *count;
  551. size_t sz;
  552. s64 biosr;
  553. int l = 0, k = 0;
  554. int len, ret;
  555. uv_pcibus_kset = kset_create_and_add("pcibuses", NULL, sgi_uv_kobj);
  556. if (!uv_pcibus_kset)
  557. return -ENOMEM;
  558. for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
  559. pci_top_str = kmalloc(sz, GFP_KERNEL);
  560. if (!pci_top_str) {
  561. ret = -ENOMEM;
  562. goto err_pci_top_str;
  563. }
  564. biosr = uv_bios_get_pci_topology((u64)sz, (u64 *)pci_top_str);
  565. if (biosr == BIOS_STATUS_SUCCESS) {
  566. len = strnlen(pci_top_str, sz);
  567. for (count = pci_top_str; count < pci_top_str + len; count++) {
  568. if (*count == '\n')
  569. l++;
  570. }
  571. num_pci_lines = l;
  572. uv_pci_objs = kcalloc(num_pci_lines,
  573. sizeof(*uv_pci_objs), GFP_KERNEL);
  574. if (!uv_pci_objs) {
  575. kfree(pci_top_str);
  576. ret = -ENOMEM;
  577. goto err_pci_top_str;
  578. }
  579. start = pci_top_str;
  580. while ((found = strsep(&start, "\n")) != NULL) {
  581. uv_pci_objs[k] = kzalloc(sizeof(*uv_pci_objs[k]), GFP_KERNEL);
  582. if (!uv_pci_objs[k]) {
  583. ret = -ENOMEM;
  584. goto err_pci_obj;
  585. }
  586. ret = init_pci_top_obj(uv_pci_objs[k], found);
  587. if (ret)
  588. goto err_pci_obj;
  589. k++;
  590. if (k == num_pci_lines)
  591. break;
  592. }
  593. }
  594. kfree(pci_top_str);
  595. if (biosr == BIOS_STATUS_SUCCESS || biosr == BIOS_STATUS_UNIMPLEMENTED)
  596. break;
  597. }
  598. return 0;
  599. err_pci_obj:
  600. k--;
  601. for (; k >= 0; k--)
  602. kobject_put(&uv_pci_objs[k]->kobj);
  603. kfree(uv_pci_objs);
  604. kfree(pci_top_str);
  605. err_pci_top_str:
  606. kset_unregister(uv_pcibus_kset);
  607. return ret;
  608. }
  609. static void pci_topology_exit(void)
  610. {
  611. int k;
  612. for (k = 0; k < num_pci_lines; k++)
  613. kobject_put(&uv_pci_objs[k]->kobj);
  614. kset_unregister(uv_pcibus_kset);
  615. kfree(uv_pci_objs);
  616. }
  617. static ssize_t partition_id_show(struct kobject *kobj,
  618. struct kobj_attribute *attr, char *buf)
  619. {
  620. return sprintf(buf, "%ld\n", sn_partition_id);
  621. }
  622. static ssize_t coherence_id_show(struct kobject *kobj,
  623. struct kobj_attribute *attr, char *buf)
  624. {
  625. return sprintf(buf, "%ld\n", sn_coherency_id);
  626. }
  627. static ssize_t uv_type_show(struct kobject *kobj,
  628. struct kobj_attribute *attr, char *buf)
  629. {
  630. return scnprintf(buf, PAGE_SIZE, "%s\n", uv_type_string());
  631. }
  632. static ssize_t uv_archtype_show(struct kobject *kobj,
  633. struct kobj_attribute *attr, char *buf)
  634. {
  635. return uv_get_archtype(buf, PAGE_SIZE);
  636. }
  637. static ssize_t uv_hub_type_show(struct kobject *kobj,
  638. struct kobj_attribute *attr, char *buf)
  639. {
  640. return scnprintf(buf, PAGE_SIZE, "0x%x\n", uv_hub_type());
  641. }
  642. static ssize_t uv_hubless_show(struct kobject *kobj,
  643. struct kobj_attribute *attr, char *buf)
  644. {
  645. return scnprintf(buf, PAGE_SIZE, "0x%x\n", uv_get_hubless_system());
  646. }
  647. static struct kobj_attribute partition_id_attr =
  648. __ATTR(partition_id, 0444, partition_id_show, NULL);
  649. static struct kobj_attribute coherence_id_attr =
  650. __ATTR(coherence_id, 0444, coherence_id_show, NULL);
  651. static struct kobj_attribute uv_type_attr =
  652. __ATTR(uv_type, 0444, uv_type_show, NULL);
  653. static struct kobj_attribute uv_archtype_attr =
  654. __ATTR(archtype, 0444, uv_archtype_show, NULL);
  655. static struct kobj_attribute uv_hub_type_attr =
  656. __ATTR(hub_type, 0444, uv_hub_type_show, NULL);
  657. static struct kobj_attribute uv_hubless_attr =
  658. __ATTR(hubless, 0444, uv_hubless_show, NULL);
  659. static struct attribute *base_attrs[] = {
  660. &partition_id_attr.attr,
  661. &coherence_id_attr.attr,
  662. &uv_type_attr.attr,
  663. &uv_archtype_attr.attr,
  664. &uv_hub_type_attr.attr,
  665. NULL,
  666. };
  667. static const struct attribute_group base_attr_group = {
  668. .attrs = base_attrs
  669. };
  670. static int initial_bios_setup(void)
  671. {
  672. u64 v;
  673. s64 biosr;
  674. biosr = uv_bios_get_master_nasid((u64)sizeof(uv_master_nasid), (u64 *)&uv_master_nasid);
  675. if (biosr)
  676. return -EINVAL;
  677. biosr = uv_bios_get_heapsize((u64)uv_master_nasid, (u64)sizeof(u64), &v);
  678. if (biosr)
  679. return -EINVAL;
  680. uv_biosheap = vmalloc(v);
  681. if (!uv_biosheap)
  682. return -ENOMEM;
  683. biosr = uv_bios_install_heap((u64)uv_master_nasid, v, (u64 *)uv_biosheap);
  684. if (biosr) {
  685. vfree(uv_biosheap);
  686. return -EINVAL;
  687. }
  688. biosr = uv_bios_obj_count((u64)uv_master_nasid, sizeof(u64), &v);
  689. if (biosr) {
  690. vfree(uv_biosheap);
  691. return -EINVAL;
  692. }
  693. uv_bios_obj_cnt = (int)v;
  694. return 0;
  695. }
  696. static struct attribute *hubless_base_attrs[] = {
  697. &partition_id_attr.attr,
  698. &uv_type_attr.attr,
  699. &uv_archtype_attr.attr,
  700. &uv_hubless_attr.attr,
  701. NULL,
  702. };
  703. static const struct attribute_group hubless_base_attr_group = {
  704. .attrs = hubless_base_attrs
  705. };
  706. static int __init uv_sysfs_hubless_init(void)
  707. {
  708. int ret;
  709. ret = sysfs_create_group(sgi_uv_kobj, &hubless_base_attr_group);
  710. if (ret) {
  711. pr_warn("sysfs_create_group hubless_base_attr_group failed\n");
  712. kobject_put(sgi_uv_kobj);
  713. }
  714. return ret;
  715. }
  716. static int __init uv_sysfs_init(void)
  717. {
  718. int ret = 0;
  719. if (!is_uv_system() && !uv_get_hubless_system())
  720. return -ENODEV;
  721. num_cnodes = uv_num_possible_blades();
  722. if (!sgi_uv_kobj)
  723. sgi_uv_kobj = kobject_create_and_add("sgi_uv", firmware_kobj);
  724. if (!sgi_uv_kobj) {
  725. pr_warn("kobject_create_and_add sgi_uv failed\n");
  726. return -EINVAL;
  727. }
  728. if (uv_get_hubless_system())
  729. return uv_sysfs_hubless_init();
  730. ret = sysfs_create_group(sgi_uv_kobj, &base_attr_group);
  731. if (ret) {
  732. pr_warn("sysfs_create_group base_attr_group failed\n");
  733. goto err_create_group;
  734. }
  735. ret = initial_bios_setup();
  736. if (ret)
  737. goto err_bios_setup;
  738. ret = uv_hubs_init();
  739. if (ret)
  740. goto err_hubs_init;
  741. ret = uv_ports_init();
  742. if (ret)
  743. goto err_ports_init;
  744. ret = pci_topology_init();
  745. if (ret)
  746. goto err_pci_init;
  747. return 0;
  748. err_pci_init:
  749. uv_ports_exit();
  750. err_ports_init:
  751. uv_hubs_exit();
  752. err_hubs_init:
  753. vfree(uv_biosheap);
  754. err_bios_setup:
  755. sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
  756. err_create_group:
  757. kobject_put(sgi_uv_kobj);
  758. return ret;
  759. }
  760. static void __exit uv_sysfs_hubless_exit(void)
  761. {
  762. sysfs_remove_group(sgi_uv_kobj, &hubless_base_attr_group);
  763. kobject_put(sgi_uv_kobj);
  764. }
  765. static void __exit uv_sysfs_exit(void)
  766. {
  767. if (!is_uv_system()) {
  768. if (uv_get_hubless_system())
  769. uv_sysfs_hubless_exit();
  770. return;
  771. }
  772. pci_topology_exit();
  773. uv_ports_exit();
  774. uv_hubs_exit();
  775. vfree(uv_biosheap);
  776. sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
  777. kobject_put(sgi_uv_kobj);
  778. }
  779. #ifndef MODULE
  780. device_initcall(uv_sysfs_init);
  781. #else
  782. module_init(uv_sysfs_init);
  783. #endif
  784. module_exit(uv_sysfs_exit);
  785. MODULE_AUTHOR("Hewlett Packard Enterprise");
  786. MODULE_LICENSE("GPL");