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 <linux/vmalloc.h>
  14. #include <asm/uv/bios.h>
  15. #include <asm/uv/uv.h>
  16. #include <asm/uv/uv_hub.h>
  17. #include <asm/uv/uv_geo.h>
  18. #define INVALID_CNODE -1
  19. struct kobject *sgi_uv_kobj;
  20. static struct kset *uv_pcibus_kset;
  21. static struct kset *uv_hubs_kset;
  22. static struct uv_bios_hub_info *hub_buf;
  23. static struct uv_bios_port_info **port_buf;
  24. static struct uv_hub **uv_hubs;
  25. static struct uv_pci_top_obj **uv_pci_objs;
  26. static int num_pci_lines;
  27. static int num_cnodes;
  28. static int *prev_obj_to_cnode;
  29. static int uv_bios_obj_cnt;
  30. static signed short uv_master_nasid = -1;
  31. static void *uv_biosheap;
  32. static const char *uv_type_string(void)
  33. {
  34. if (is_uv5_hub())
  35. return "9.0";
  36. else if (is_uv4a_hub())
  37. return "7.1";
  38. else if (is_uv4_hub())
  39. return "7.0";
  40. else if (is_uv3_hub())
  41. return "5.0";
  42. else if (is_uv2_hub())
  43. return "3.0";
  44. else if (uv_get_hubless_system())
  45. return "0.1";
  46. else
  47. return "unknown";
  48. }
  49. static int ordinal_to_nasid(int ordinal)
  50. {
  51. if (ordinal < num_cnodes && ordinal >= 0)
  52. return UV_PNODE_TO_NASID(uv_blade_to_pnode(ordinal));
  53. else
  54. return -1;
  55. }
  56. static union geoid_u cnode_to_geoid(int cnode)
  57. {
  58. union geoid_u geoid;
  59. uv_bios_get_geoinfo(ordinal_to_nasid(cnode), (u64)sizeof(union geoid_u), (u64 *)&geoid);
  60. return geoid;
  61. }
  62. static int location_to_bpos(char *location, int *rack, int *slot, int *blade)
  63. {
  64. char type, r, b, h;
  65. int idb, idh;
  66. if (sscanf(location, "%c%03d%c%02d%c%2d%c%d",
  67. &r, rack, &type, slot, &b, &idb, &h, &idh) != 8)
  68. return -1;
  69. *blade = idb * 2 + idh;
  70. return 0;
  71. }
  72. static int cache_obj_to_cnode(struct uv_bios_hub_info *obj)
  73. {
  74. int cnode;
  75. union geoid_u geoid;
  76. int obj_rack, obj_slot, obj_blade;
  77. int rack, slot, blade;
  78. if (!obj->f.fields.this_part && !obj->f.fields.is_shared)
  79. return 0;
  80. if (location_to_bpos(obj->location, &obj_rack, &obj_slot, &obj_blade))
  81. return -1;
  82. for (cnode = 0; cnode < num_cnodes; cnode++) {
  83. geoid = cnode_to_geoid(cnode);
  84. rack = geo_rack(geoid);
  85. slot = geo_slot(geoid);
  86. blade = geo_blade(geoid);
  87. if (obj_rack == rack && obj_slot == slot && obj_blade == blade)
  88. prev_obj_to_cnode[obj->id] = cnode;
  89. }
  90. return 0;
  91. }
  92. static int get_obj_to_cnode(int obj_id)
  93. {
  94. return prev_obj_to_cnode[obj_id];
  95. }
  96. struct uv_hub {
  97. struct kobject kobj;
  98. struct uv_bios_hub_info *hub_info;
  99. struct uv_port **ports;
  100. };
  101. #define to_uv_hub(kobj_ptr) container_of(kobj_ptr, struct uv_hub, kobj)
  102. static ssize_t hub_name_show(struct uv_bios_hub_info *hub_info, char *buf)
  103. {
  104. return sysfs_emit(buf, "%s\n", hub_info->name);
  105. }
  106. static ssize_t hub_location_show(struct uv_bios_hub_info *hub_info, char *buf)
  107. {
  108. return sysfs_emit(buf, "%s\n", hub_info->location);
  109. }
  110. static ssize_t hub_partition_show(struct uv_bios_hub_info *hub_info, char *buf)
  111. {
  112. return sysfs_emit(buf, "%d\n", hub_info->f.fields.this_part);
  113. }
  114. static ssize_t hub_shared_show(struct uv_bios_hub_info *hub_info, char *buf)
  115. {
  116. return sysfs_emit(buf, "%d\n", hub_info->f.fields.is_shared);
  117. }
  118. static ssize_t hub_nasid_show(struct uv_bios_hub_info *hub_info, char *buf)
  119. {
  120. int cnode = get_obj_to_cnode(hub_info->id);
  121. return sysfs_emit(buf, "%d\n", ordinal_to_nasid(cnode));
  122. }
  123. static ssize_t hub_cnode_show(struct uv_bios_hub_info *hub_info, char *buf)
  124. {
  125. return sysfs_emit(buf, "%d\n", get_obj_to_cnode(hub_info->id));
  126. }
  127. struct hub_sysfs_entry {
  128. struct attribute attr;
  129. ssize_t (*show)(struct uv_bios_hub_info *hub_info, char *buf);
  130. ssize_t (*store)(struct uv_bios_hub_info *hub_info, const char *buf, size_t sz);
  131. };
  132. static struct hub_sysfs_entry name_attribute =
  133. __ATTR(name, 0444, hub_name_show, NULL);
  134. static struct hub_sysfs_entry location_attribute =
  135. __ATTR(location, 0444, hub_location_show, NULL);
  136. static struct hub_sysfs_entry partition_attribute =
  137. __ATTR(this_partition, 0444, hub_partition_show, NULL);
  138. static struct hub_sysfs_entry shared_attribute =
  139. __ATTR(shared, 0444, hub_shared_show, NULL);
  140. static struct hub_sysfs_entry nasid_attribute =
  141. __ATTR(nasid, 0444, hub_nasid_show, NULL);
  142. static struct hub_sysfs_entry cnode_attribute =
  143. __ATTR(cnode, 0444, hub_cnode_show, NULL);
  144. static struct attribute *uv_hub_attrs[] = {
  145. &name_attribute.attr,
  146. &location_attribute.attr,
  147. &partition_attribute.attr,
  148. &shared_attribute.attr,
  149. &nasid_attribute.attr,
  150. &cnode_attribute.attr,
  151. NULL,
  152. };
  153. ATTRIBUTE_GROUPS(uv_hub);
  154. static void hub_release(struct kobject *kobj)
  155. {
  156. struct uv_hub *hub = to_uv_hub(kobj);
  157. kfree(hub);
  158. }
  159. static ssize_t hub_type_show(struct kobject *kobj, struct attribute *attr,
  160. char *buf)
  161. {
  162. struct uv_hub *hub = to_uv_hub(kobj);
  163. struct uv_bios_hub_info *bios_hub_info = hub->hub_info;
  164. struct hub_sysfs_entry *entry;
  165. entry = container_of(attr, struct hub_sysfs_entry, attr);
  166. if (!entry->show)
  167. return -EIO;
  168. return entry->show(bios_hub_info, buf);
  169. }
  170. static const struct sysfs_ops hub_sysfs_ops = {
  171. .show = hub_type_show,
  172. };
  173. static const struct kobj_type hub_attr_type = {
  174. .release = hub_release,
  175. .sysfs_ops = &hub_sysfs_ops,
  176. .default_groups = uv_hub_groups,
  177. };
  178. static int uv_hubs_init(void)
  179. {
  180. s64 biosr;
  181. u64 sz;
  182. int i, ret;
  183. prev_obj_to_cnode = kmalloc_objs(*prev_obj_to_cnode, uv_bios_obj_cnt);
  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 = kzalloc_objs(*uv_hubs, uv_bios_obj_cnt);
  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_obj(*uv_hubs[i]);
  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 sysfs_emit(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 sysfs_emit(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 const 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 = kzalloc_objs(*port_buf, uv_bios_obj_cnt);
  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 = kzalloc_objs(*uv_hubs[j]->ports,
  324. hub_buf[j].ports);
  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_obj(*uv_hubs[j]->ports[k]);
  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 sysfs_emit(buf, "%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 sysfs_emit(buf, "%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 sysfs_emit(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 sysfs_emit(buf, "%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 sysfs_emit(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 const 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 = kzalloc_objs(*uv_pci_objs, num_pci_lines);
  573. if (!uv_pci_objs) {
  574. kfree(pci_top_str);
  575. ret = -ENOMEM;
  576. goto err_pci_top_str;
  577. }
  578. start = pci_top_str;
  579. while ((found = strsep(&start, "\n")) != NULL) {
  580. uv_pci_objs[k] = kzalloc_obj(*uv_pci_objs[k]);
  581. if (!uv_pci_objs[k]) {
  582. ret = -ENOMEM;
  583. goto err_pci_obj;
  584. }
  585. ret = init_pci_top_obj(uv_pci_objs[k], found);
  586. if (ret)
  587. goto err_pci_obj;
  588. k++;
  589. if (k == num_pci_lines)
  590. break;
  591. }
  592. }
  593. kfree(pci_top_str);
  594. if (biosr == BIOS_STATUS_SUCCESS || biosr == BIOS_STATUS_UNIMPLEMENTED)
  595. break;
  596. }
  597. return 0;
  598. err_pci_obj:
  599. k--;
  600. for (; k >= 0; k--)
  601. kobject_put(&uv_pci_objs[k]->kobj);
  602. kfree(uv_pci_objs);
  603. kfree(pci_top_str);
  604. err_pci_top_str:
  605. kset_unregister(uv_pcibus_kset);
  606. return ret;
  607. }
  608. static void pci_topology_exit(void)
  609. {
  610. int k;
  611. for (k = 0; k < num_pci_lines; k++)
  612. kobject_put(&uv_pci_objs[k]->kobj);
  613. kset_unregister(uv_pcibus_kset);
  614. kfree(uv_pci_objs);
  615. }
  616. static ssize_t partition_id_show(struct kobject *kobj,
  617. struct kobj_attribute *attr, char *buf)
  618. {
  619. return sysfs_emit(buf, "%ld\n", sn_partition_id);
  620. }
  621. static ssize_t coherence_id_show(struct kobject *kobj,
  622. struct kobj_attribute *attr, char *buf)
  623. {
  624. return sysfs_emit(buf, "%ld\n", sn_coherency_id);
  625. }
  626. static ssize_t uv_type_show(struct kobject *kobj,
  627. struct kobj_attribute *attr, char *buf)
  628. {
  629. return sysfs_emit(buf, "%s\n", uv_type_string());
  630. }
  631. static ssize_t uv_archtype_show(struct kobject *kobj,
  632. struct kobj_attribute *attr, char *buf)
  633. {
  634. return uv_get_archtype(buf, PAGE_SIZE);
  635. }
  636. static ssize_t uv_hub_type_show(struct kobject *kobj,
  637. struct kobj_attribute *attr, char *buf)
  638. {
  639. return sysfs_emit(buf, "0x%x\n", uv_hub_type());
  640. }
  641. static ssize_t uv_hubless_show(struct kobject *kobj,
  642. struct kobj_attribute *attr, char *buf)
  643. {
  644. return sysfs_emit(buf, "0x%x\n", uv_get_hubless_system());
  645. }
  646. static struct kobj_attribute partition_id_attr =
  647. __ATTR(partition_id, 0444, partition_id_show, NULL);
  648. static struct kobj_attribute coherence_id_attr =
  649. __ATTR(coherence_id, 0444, coherence_id_show, NULL);
  650. static struct kobj_attribute uv_type_attr =
  651. __ATTR(uv_type, 0444, uv_type_show, NULL);
  652. static struct kobj_attribute uv_archtype_attr =
  653. __ATTR(archtype, 0444, uv_archtype_show, NULL);
  654. static struct kobj_attribute uv_hub_type_attr =
  655. __ATTR(hub_type, 0444, uv_hub_type_show, NULL);
  656. static struct kobj_attribute uv_hubless_attr =
  657. __ATTR(hubless, 0444, uv_hubless_show, NULL);
  658. static struct attribute *base_attrs[] = {
  659. &partition_id_attr.attr,
  660. &coherence_id_attr.attr,
  661. &uv_type_attr.attr,
  662. &uv_archtype_attr.attr,
  663. &uv_hub_type_attr.attr,
  664. NULL,
  665. };
  666. static const struct attribute_group base_attr_group = {
  667. .attrs = base_attrs
  668. };
  669. static int initial_bios_setup(void)
  670. {
  671. u64 v;
  672. s64 biosr;
  673. biosr = uv_bios_get_master_nasid((u64)sizeof(uv_master_nasid), (u64 *)&uv_master_nasid);
  674. if (biosr)
  675. return -EINVAL;
  676. biosr = uv_bios_get_heapsize((u64)uv_master_nasid, (u64)sizeof(u64), &v);
  677. if (biosr)
  678. return -EINVAL;
  679. uv_biosheap = vmalloc(v);
  680. if (!uv_biosheap)
  681. return -ENOMEM;
  682. biosr = uv_bios_install_heap((u64)uv_master_nasid, v, (u64 *)uv_biosheap);
  683. if (biosr) {
  684. vfree(uv_biosheap);
  685. return -EINVAL;
  686. }
  687. biosr = uv_bios_obj_count((u64)uv_master_nasid, sizeof(u64), &v);
  688. if (biosr) {
  689. vfree(uv_biosheap);
  690. return -EINVAL;
  691. }
  692. uv_bios_obj_cnt = (int)v;
  693. return 0;
  694. }
  695. static struct attribute *hubless_base_attrs[] = {
  696. &partition_id_attr.attr,
  697. &uv_type_attr.attr,
  698. &uv_archtype_attr.attr,
  699. &uv_hubless_attr.attr,
  700. NULL,
  701. };
  702. static const struct attribute_group hubless_base_attr_group = {
  703. .attrs = hubless_base_attrs
  704. };
  705. static int __init uv_sysfs_hubless_init(void)
  706. {
  707. int ret;
  708. ret = sysfs_create_group(sgi_uv_kobj, &hubless_base_attr_group);
  709. if (ret) {
  710. pr_warn("sysfs_create_group hubless_base_attr_group failed\n");
  711. kobject_put(sgi_uv_kobj);
  712. }
  713. return ret;
  714. }
  715. static int __init uv_sysfs_init(void)
  716. {
  717. int ret = 0;
  718. if (!is_uv_system() && !uv_get_hubless_system())
  719. return -ENODEV;
  720. num_cnodes = uv_num_possible_blades();
  721. if (!sgi_uv_kobj)
  722. sgi_uv_kobj = kobject_create_and_add("sgi_uv", firmware_kobj);
  723. if (!sgi_uv_kobj) {
  724. pr_warn("kobject_create_and_add sgi_uv failed\n");
  725. return -EINVAL;
  726. }
  727. if (uv_get_hubless_system())
  728. return uv_sysfs_hubless_init();
  729. ret = sysfs_create_group(sgi_uv_kobj, &base_attr_group);
  730. if (ret) {
  731. pr_warn("sysfs_create_group base_attr_group failed\n");
  732. goto err_create_group;
  733. }
  734. ret = initial_bios_setup();
  735. if (ret)
  736. goto err_bios_setup;
  737. ret = uv_hubs_init();
  738. if (ret)
  739. goto err_hubs_init;
  740. ret = uv_ports_init();
  741. if (ret)
  742. goto err_ports_init;
  743. ret = pci_topology_init();
  744. if (ret)
  745. goto err_pci_init;
  746. return 0;
  747. err_pci_init:
  748. uv_ports_exit();
  749. err_ports_init:
  750. uv_hubs_exit();
  751. err_hubs_init:
  752. vfree(uv_biosheap);
  753. err_bios_setup:
  754. sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
  755. err_create_group:
  756. kobject_put(sgi_uv_kobj);
  757. return ret;
  758. }
  759. static void __exit uv_sysfs_hubless_exit(void)
  760. {
  761. sysfs_remove_group(sgi_uv_kobj, &hubless_base_attr_group);
  762. kobject_put(sgi_uv_kobj);
  763. }
  764. static void __exit uv_sysfs_exit(void)
  765. {
  766. if (!is_uv_system()) {
  767. if (uv_get_hubless_system())
  768. uv_sysfs_hubless_exit();
  769. return;
  770. }
  771. pci_topology_exit();
  772. uv_ports_exit();
  773. uv_hubs_exit();
  774. vfree(uv_biosheap);
  775. sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
  776. kobject_put(sgi_uv_kobj);
  777. }
  778. #ifndef MODULE
  779. device_initcall(uv_sysfs_init);
  780. #else
  781. module_init(uv_sysfs_init);
  782. #endif
  783. module_exit(uv_sysfs_exit);
  784. MODULE_AUTHOR("Hewlett Packard Enterprise");
  785. MODULE_DESCRIPTION("Sysfs structure for HPE UV systems");
  786. MODULE_LICENSE("GPL");