uv_sysfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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_array(uv_bios_obj_cnt, sizeof(*prev_obj_to_cnode),
  184. GFP_KERNEL);
  185. if (!prev_obj_to_cnode)
  186. return -ENOMEM;
  187. for (i = 0; i < uv_bios_obj_cnt; i++)
  188. prev_obj_to_cnode[i] = INVALID_CNODE;
  189. uv_hubs_kset = kset_create_and_add("hubs", NULL, sgi_uv_kobj);
  190. if (!uv_hubs_kset) {
  191. ret = -ENOMEM;
  192. goto err_hubs_kset;
  193. }
  194. sz = uv_bios_obj_cnt * sizeof(*hub_buf);
  195. hub_buf = kzalloc(sz, GFP_KERNEL);
  196. if (!hub_buf) {
  197. ret = -ENOMEM;
  198. goto err_hub_buf;
  199. }
  200. biosr = uv_bios_enum_objs((u64)uv_master_nasid, sz, (u64 *)hub_buf);
  201. if (biosr) {
  202. ret = -EINVAL;
  203. goto err_enum_objs;
  204. }
  205. uv_hubs = kcalloc(uv_bios_obj_cnt, sizeof(*uv_hubs), GFP_KERNEL);
  206. if (!uv_hubs) {
  207. ret = -ENOMEM;
  208. goto err_enum_objs;
  209. }
  210. for (i = 0; i < uv_bios_obj_cnt; i++) {
  211. uv_hubs[i] = kzalloc(sizeof(*uv_hubs[i]), GFP_KERNEL);
  212. if (!uv_hubs[i]) {
  213. i--;
  214. ret = -ENOMEM;
  215. goto err_hubs;
  216. }
  217. uv_hubs[i]->hub_info = &hub_buf[i];
  218. cache_obj_to_cnode(uv_hubs[i]->hub_info);
  219. uv_hubs[i]->kobj.kset = uv_hubs_kset;
  220. ret = kobject_init_and_add(&uv_hubs[i]->kobj, &hub_attr_type,
  221. NULL, "hub_%u", hub_buf[i].id);
  222. if (ret)
  223. goto err_hubs;
  224. kobject_uevent(&uv_hubs[i]->kobj, KOBJ_ADD);
  225. }
  226. return 0;
  227. err_hubs:
  228. for (; i >= 0; i--)
  229. kobject_put(&uv_hubs[i]->kobj);
  230. kfree(uv_hubs);
  231. err_enum_objs:
  232. kfree(hub_buf);
  233. err_hub_buf:
  234. kset_unregister(uv_hubs_kset);
  235. err_hubs_kset:
  236. kfree(prev_obj_to_cnode);
  237. return ret;
  238. }
  239. static void uv_hubs_exit(void)
  240. {
  241. int i;
  242. for (i = 0; i < uv_bios_obj_cnt; i++)
  243. kobject_put(&uv_hubs[i]->kobj);
  244. kfree(uv_hubs);
  245. kfree(hub_buf);
  246. kset_unregister(uv_hubs_kset);
  247. kfree(prev_obj_to_cnode);
  248. }
  249. struct uv_port {
  250. struct kobject kobj;
  251. struct uv_bios_port_info *port_info;
  252. };
  253. #define to_uv_port(kobj_ptr) container_of(kobj_ptr, struct uv_port, kobj)
  254. static ssize_t uv_port_conn_hub_show(struct uv_bios_port_info *port, char *buf)
  255. {
  256. return sysfs_emit(buf, "%d\n", port->conn_id);
  257. }
  258. static ssize_t uv_port_conn_port_show(struct uv_bios_port_info *port, char *buf)
  259. {
  260. return sysfs_emit(buf, "%d\n", port->conn_port);
  261. }
  262. struct uv_port_sysfs_entry {
  263. struct attribute attr;
  264. ssize_t (*show)(struct uv_bios_port_info *port_info, char *buf);
  265. ssize_t (*store)(struct uv_bios_port_info *port_info, const char *buf, size_t size);
  266. };
  267. static struct uv_port_sysfs_entry uv_port_conn_hub_attribute =
  268. __ATTR(conn_hub, 0444, uv_port_conn_hub_show, NULL);
  269. static struct uv_port_sysfs_entry uv_port_conn_port_attribute =
  270. __ATTR(conn_port, 0444, uv_port_conn_port_show, NULL);
  271. static struct attribute *uv_port_attrs[] = {
  272. &uv_port_conn_hub_attribute.attr,
  273. &uv_port_conn_port_attribute.attr,
  274. NULL,
  275. };
  276. ATTRIBUTE_GROUPS(uv_port);
  277. static void uv_port_release(struct kobject *kobj)
  278. {
  279. struct uv_port *port = to_uv_port(kobj);
  280. kfree(port);
  281. }
  282. static ssize_t uv_port_type_show(struct kobject *kobj, struct attribute *attr,
  283. char *buf)
  284. {
  285. struct uv_port *port = to_uv_port(kobj);
  286. struct uv_bios_port_info *port_info = port->port_info;
  287. struct uv_port_sysfs_entry *entry;
  288. entry = container_of(attr, struct uv_port_sysfs_entry, attr);
  289. if (!entry->show)
  290. return -EIO;
  291. return entry->show(port_info, buf);
  292. }
  293. static const struct sysfs_ops uv_port_sysfs_ops = {
  294. .show = uv_port_type_show,
  295. };
  296. static const struct kobj_type uv_port_attr_type = {
  297. .release = uv_port_release,
  298. .sysfs_ops = &uv_port_sysfs_ops,
  299. .default_groups = uv_port_groups,
  300. };
  301. static int uv_ports_init(void)
  302. {
  303. s64 biosr;
  304. int j = 0, k = 0, ret, sz;
  305. port_buf = kcalloc(uv_bios_obj_cnt, sizeof(*port_buf), GFP_KERNEL);
  306. if (!port_buf)
  307. return -ENOMEM;
  308. for (j = 0; j < uv_bios_obj_cnt; j++) {
  309. sz = hub_buf[j].ports * sizeof(*port_buf[j]);
  310. port_buf[j] = kzalloc(sz, GFP_KERNEL);
  311. if (!port_buf[j]) {
  312. ret = -ENOMEM;
  313. j--;
  314. goto err_port_info;
  315. }
  316. biosr = uv_bios_enum_ports((u64)uv_master_nasid, (u64)hub_buf[j].id, sz,
  317. (u64 *)port_buf[j]);
  318. if (biosr) {
  319. ret = -EINVAL;
  320. goto err_port_info;
  321. }
  322. }
  323. for (j = 0; j < uv_bios_obj_cnt; j++) {
  324. uv_hubs[j]->ports = kcalloc(hub_buf[j].ports,
  325. sizeof(*uv_hubs[j]->ports), GFP_KERNEL);
  326. if (!uv_hubs[j]->ports) {
  327. ret = -ENOMEM;
  328. j--;
  329. goto err_ports;
  330. }
  331. }
  332. for (j = 0; j < uv_bios_obj_cnt; j++) {
  333. for (k = 0; k < hub_buf[j].ports; k++) {
  334. uv_hubs[j]->ports[k] = kzalloc(sizeof(*uv_hubs[j]->ports[k]), GFP_KERNEL);
  335. if (!uv_hubs[j]->ports[k]) {
  336. ret = -ENOMEM;
  337. k--;
  338. goto err_kobj_ports;
  339. }
  340. uv_hubs[j]->ports[k]->port_info = &port_buf[j][k];
  341. ret = kobject_init_and_add(&uv_hubs[j]->ports[k]->kobj, &uv_port_attr_type,
  342. &uv_hubs[j]->kobj, "port_%d", port_buf[j][k].port);
  343. if (ret)
  344. goto err_kobj_ports;
  345. kobject_uevent(&uv_hubs[j]->ports[k]->kobj, KOBJ_ADD);
  346. }
  347. }
  348. return 0;
  349. err_kobj_ports:
  350. for (; j >= 0; j--) {
  351. for (; k >= 0; k--)
  352. kobject_put(&uv_hubs[j]->ports[k]->kobj);
  353. if (j > 0)
  354. k = hub_buf[j-1].ports - 1;
  355. }
  356. j = uv_bios_obj_cnt - 1;
  357. err_ports:
  358. for (; j >= 0; j--)
  359. kfree(uv_hubs[j]->ports);
  360. j = uv_bios_obj_cnt - 1;
  361. err_port_info:
  362. for (; j >= 0; j--)
  363. kfree(port_buf[j]);
  364. kfree(port_buf);
  365. return ret;
  366. }
  367. static void uv_ports_exit(void)
  368. {
  369. int j, k;
  370. for (j = 0; j < uv_bios_obj_cnt; j++) {
  371. for (k = hub_buf[j].ports - 1; k >= 0; k--)
  372. kobject_put(&uv_hubs[j]->ports[k]->kobj);
  373. }
  374. for (j = 0; j < uv_bios_obj_cnt; j++) {
  375. kfree(uv_hubs[j]->ports);
  376. kfree(port_buf[j]);
  377. }
  378. kfree(port_buf);
  379. }
  380. struct uv_pci_top_obj {
  381. struct kobject kobj;
  382. char *type;
  383. char *location;
  384. int iio_stack;
  385. char *ppb_addr;
  386. int slot;
  387. };
  388. #define to_uv_pci_top_obj(kobj_ptr) container_of(kobj_ptr, struct uv_pci_top_obj, kobj)
  389. static ssize_t uv_pci_type_show(struct uv_pci_top_obj *top_obj, char *buf)
  390. {
  391. return sysfs_emit(buf, "%s\n", top_obj->type);
  392. }
  393. static ssize_t uv_pci_location_show(struct uv_pci_top_obj *top_obj, char *buf)
  394. {
  395. return sysfs_emit(buf, "%s\n", top_obj->location);
  396. }
  397. static ssize_t uv_pci_iio_stack_show(struct uv_pci_top_obj *top_obj, char *buf)
  398. {
  399. return sysfs_emit(buf, "%d\n", top_obj->iio_stack);
  400. }
  401. static ssize_t uv_pci_ppb_addr_show(struct uv_pci_top_obj *top_obj, char *buf)
  402. {
  403. return sysfs_emit(buf, "%s\n", top_obj->ppb_addr);
  404. }
  405. static ssize_t uv_pci_slot_show(struct uv_pci_top_obj *top_obj, char *buf)
  406. {
  407. return sysfs_emit(buf, "%d\n", top_obj->slot);
  408. }
  409. struct uv_pci_top_sysfs_entry {
  410. struct attribute attr;
  411. ssize_t (*show)(struct uv_pci_top_obj *top_obj, char *buf);
  412. ssize_t (*store)(struct uv_pci_top_obj *top_obj, const char *buf, size_t size);
  413. };
  414. static struct uv_pci_top_sysfs_entry uv_pci_type_attribute =
  415. __ATTR(type, 0444, uv_pci_type_show, NULL);
  416. static struct uv_pci_top_sysfs_entry uv_pci_location_attribute =
  417. __ATTR(location, 0444, uv_pci_location_show, NULL);
  418. static struct uv_pci_top_sysfs_entry uv_pci_iio_stack_attribute =
  419. __ATTR(iio_stack, 0444, uv_pci_iio_stack_show, NULL);
  420. static struct uv_pci_top_sysfs_entry uv_pci_ppb_addr_attribute =
  421. __ATTR(ppb_addr, 0444, uv_pci_ppb_addr_show, NULL);
  422. static struct uv_pci_top_sysfs_entry uv_pci_slot_attribute =
  423. __ATTR(slot, 0444, uv_pci_slot_show, NULL);
  424. static void uv_pci_top_release(struct kobject *kobj)
  425. {
  426. struct uv_pci_top_obj *top_obj = to_uv_pci_top_obj(kobj);
  427. kfree(top_obj->type);
  428. kfree(top_obj->location);
  429. kfree(top_obj->ppb_addr);
  430. kfree(top_obj);
  431. }
  432. static ssize_t pci_top_type_show(struct kobject *kobj,
  433. struct attribute *attr, char *buf)
  434. {
  435. struct uv_pci_top_obj *top_obj = to_uv_pci_top_obj(kobj);
  436. struct uv_pci_top_sysfs_entry *entry;
  437. entry = container_of(attr, struct uv_pci_top_sysfs_entry, attr);
  438. if (!entry->show)
  439. return -EIO;
  440. return entry->show(top_obj, buf);
  441. }
  442. static const struct sysfs_ops uv_pci_top_sysfs_ops = {
  443. .show = pci_top_type_show,
  444. };
  445. static const struct kobj_type uv_pci_top_attr_type = {
  446. .release = uv_pci_top_release,
  447. .sysfs_ops = &uv_pci_top_sysfs_ops,
  448. };
  449. static int init_pci_top_obj(struct uv_pci_top_obj *top_obj, char *line)
  450. {
  451. char *start;
  452. char type[11], location[14], ppb_addr[15];
  453. int str_cnt, ret;
  454. unsigned int tmp_match[2];
  455. // Minimum line length
  456. if (strlen(line) < 36)
  457. return -EINVAL;
  458. //Line must match format "pcibus %4x:%2x" to be valid
  459. str_cnt = sscanf(line, "pcibus %4x:%2x", &tmp_match[0], &tmp_match[1]);
  460. if (str_cnt < 2)
  461. return -EINVAL;
  462. /* Connect pcibus to segment:bus number with '_'
  463. * to concatenate name tokens.
  464. * pcibus 0000:00 ... -> pcibus_0000:00 ...
  465. */
  466. line[6] = '_';
  467. /* Null terminate after the concatencated name tokens
  468. * to produce kobj name string.
  469. */
  470. line[14] = '\0';
  471. // Use start to index after name tokens string for remainder of line info.
  472. start = &line[15];
  473. top_obj->iio_stack = -1;
  474. top_obj->slot = -1;
  475. /* r001i01b00h0 BASE IO (IIO Stack 0)
  476. * r001i01b00h1 PCIe IO (IIO Stack 1)
  477. * r001i01b03h1 PCIe SLOT
  478. * r001i01b00h0 NODE IO
  479. * r001i01b00h0 Riser
  480. * (IIO Stack #) may not be present.
  481. */
  482. if (start[0] == 'r') {
  483. str_cnt = sscanf(start, "%13s %10[^(] %*s %*s %d)",
  484. location, type, &top_obj->iio_stack);
  485. if (str_cnt < 2)
  486. return -EINVAL;
  487. top_obj->type = kstrdup(type, GFP_KERNEL);
  488. if (!top_obj->type)
  489. return -ENOMEM;
  490. top_obj->location = kstrdup(location, GFP_KERNEL);
  491. if (!top_obj->location) {
  492. kfree(top_obj->type);
  493. return -ENOMEM;
  494. }
  495. }
  496. /* PPB at 0000:80:00.00 (slot 3)
  497. * (slot #) may not be present.
  498. */
  499. else if (start[0] == 'P') {
  500. str_cnt = sscanf(start, "%10s %*s %14s %*s %d)",
  501. type, ppb_addr, &top_obj->slot);
  502. if (str_cnt < 2)
  503. return -EINVAL;
  504. top_obj->type = kstrdup(type, GFP_KERNEL);
  505. if (!top_obj->type)
  506. return -ENOMEM;
  507. top_obj->ppb_addr = kstrdup(ppb_addr, GFP_KERNEL);
  508. if (!top_obj->ppb_addr) {
  509. kfree(top_obj->type);
  510. return -ENOMEM;
  511. }
  512. } else
  513. return -EINVAL;
  514. top_obj->kobj.kset = uv_pcibus_kset;
  515. ret = kobject_init_and_add(&top_obj->kobj, &uv_pci_top_attr_type, NULL, "%s", line);
  516. if (ret)
  517. goto err_add_sysfs;
  518. if (top_obj->type) {
  519. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_type_attribute.attr);
  520. if (ret)
  521. goto err_add_sysfs;
  522. }
  523. if (top_obj->location) {
  524. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_location_attribute.attr);
  525. if (ret)
  526. goto err_add_sysfs;
  527. }
  528. if (top_obj->iio_stack >= 0) {
  529. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_iio_stack_attribute.attr);
  530. if (ret)
  531. goto err_add_sysfs;
  532. }
  533. if (top_obj->ppb_addr) {
  534. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_ppb_addr_attribute.attr);
  535. if (ret)
  536. goto err_add_sysfs;
  537. }
  538. if (top_obj->slot >= 0) {
  539. ret = sysfs_create_file(&top_obj->kobj, &uv_pci_slot_attribute.attr);
  540. if (ret)
  541. goto err_add_sysfs;
  542. }
  543. kobject_uevent(&top_obj->kobj, KOBJ_ADD);
  544. return 0;
  545. err_add_sysfs:
  546. kobject_put(&top_obj->kobj);
  547. return ret;
  548. }
  549. static int pci_topology_init(void)
  550. {
  551. char *pci_top_str, *start, *found, *count;
  552. size_t sz;
  553. s64 biosr;
  554. int l = 0, k = 0;
  555. int len, ret;
  556. uv_pcibus_kset = kset_create_and_add("pcibuses", NULL, sgi_uv_kobj);
  557. if (!uv_pcibus_kset)
  558. return -ENOMEM;
  559. for (sz = PAGE_SIZE; sz < 16 * PAGE_SIZE; sz += PAGE_SIZE) {
  560. pci_top_str = kmalloc(sz, GFP_KERNEL);
  561. if (!pci_top_str) {
  562. ret = -ENOMEM;
  563. goto err_pci_top_str;
  564. }
  565. biosr = uv_bios_get_pci_topology((u64)sz, (u64 *)pci_top_str);
  566. if (biosr == BIOS_STATUS_SUCCESS) {
  567. len = strnlen(pci_top_str, sz);
  568. for (count = pci_top_str; count < pci_top_str + len; count++) {
  569. if (*count == '\n')
  570. l++;
  571. }
  572. num_pci_lines = l;
  573. uv_pci_objs = kcalloc(num_pci_lines,
  574. sizeof(*uv_pci_objs), GFP_KERNEL);
  575. if (!uv_pci_objs) {
  576. kfree(pci_top_str);
  577. ret = -ENOMEM;
  578. goto err_pci_top_str;
  579. }
  580. start = pci_top_str;
  581. while ((found = strsep(&start, "\n")) != NULL) {
  582. uv_pci_objs[k] = kzalloc(sizeof(*uv_pci_objs[k]), GFP_KERNEL);
  583. if (!uv_pci_objs[k]) {
  584. ret = -ENOMEM;
  585. goto err_pci_obj;
  586. }
  587. ret = init_pci_top_obj(uv_pci_objs[k], found);
  588. if (ret)
  589. goto err_pci_obj;
  590. k++;
  591. if (k == num_pci_lines)
  592. break;
  593. }
  594. }
  595. kfree(pci_top_str);
  596. if (biosr == BIOS_STATUS_SUCCESS || biosr == BIOS_STATUS_UNIMPLEMENTED)
  597. break;
  598. }
  599. return 0;
  600. err_pci_obj:
  601. k--;
  602. for (; k >= 0; k--)
  603. kobject_put(&uv_pci_objs[k]->kobj);
  604. kfree(uv_pci_objs);
  605. kfree(pci_top_str);
  606. err_pci_top_str:
  607. kset_unregister(uv_pcibus_kset);
  608. return ret;
  609. }
  610. static void pci_topology_exit(void)
  611. {
  612. int k;
  613. for (k = 0; k < num_pci_lines; k++)
  614. kobject_put(&uv_pci_objs[k]->kobj);
  615. kset_unregister(uv_pcibus_kset);
  616. kfree(uv_pci_objs);
  617. }
  618. static ssize_t partition_id_show(struct kobject *kobj,
  619. struct kobj_attribute *attr, char *buf)
  620. {
  621. return sysfs_emit(buf, "%ld\n", sn_partition_id);
  622. }
  623. static ssize_t coherence_id_show(struct kobject *kobj,
  624. struct kobj_attribute *attr, char *buf)
  625. {
  626. return sysfs_emit(buf, "%ld\n", sn_coherency_id);
  627. }
  628. static ssize_t uv_type_show(struct kobject *kobj,
  629. struct kobj_attribute *attr, char *buf)
  630. {
  631. return sysfs_emit(buf, "%s\n", uv_type_string());
  632. }
  633. static ssize_t uv_archtype_show(struct kobject *kobj,
  634. struct kobj_attribute *attr, char *buf)
  635. {
  636. return uv_get_archtype(buf, PAGE_SIZE);
  637. }
  638. static ssize_t uv_hub_type_show(struct kobject *kobj,
  639. struct kobj_attribute *attr, char *buf)
  640. {
  641. return sysfs_emit(buf, "0x%x\n", uv_hub_type());
  642. }
  643. static ssize_t uv_hubless_show(struct kobject *kobj,
  644. struct kobj_attribute *attr, char *buf)
  645. {
  646. return sysfs_emit(buf, "0x%x\n", uv_get_hubless_system());
  647. }
  648. static struct kobj_attribute partition_id_attr =
  649. __ATTR(partition_id, 0444, partition_id_show, NULL);
  650. static struct kobj_attribute coherence_id_attr =
  651. __ATTR(coherence_id, 0444, coherence_id_show, NULL);
  652. static struct kobj_attribute uv_type_attr =
  653. __ATTR(uv_type, 0444, uv_type_show, NULL);
  654. static struct kobj_attribute uv_archtype_attr =
  655. __ATTR(archtype, 0444, uv_archtype_show, NULL);
  656. static struct kobj_attribute uv_hub_type_attr =
  657. __ATTR(hub_type, 0444, uv_hub_type_show, NULL);
  658. static struct kobj_attribute uv_hubless_attr =
  659. __ATTR(hubless, 0444, uv_hubless_show, NULL);
  660. static struct attribute *base_attrs[] = {
  661. &partition_id_attr.attr,
  662. &coherence_id_attr.attr,
  663. &uv_type_attr.attr,
  664. &uv_archtype_attr.attr,
  665. &uv_hub_type_attr.attr,
  666. NULL,
  667. };
  668. static const struct attribute_group base_attr_group = {
  669. .attrs = base_attrs
  670. };
  671. static int initial_bios_setup(void)
  672. {
  673. u64 v;
  674. s64 biosr;
  675. biosr = uv_bios_get_master_nasid((u64)sizeof(uv_master_nasid), (u64 *)&uv_master_nasid);
  676. if (biosr)
  677. return -EINVAL;
  678. biosr = uv_bios_get_heapsize((u64)uv_master_nasid, (u64)sizeof(u64), &v);
  679. if (biosr)
  680. return -EINVAL;
  681. uv_biosheap = vmalloc(v);
  682. if (!uv_biosheap)
  683. return -ENOMEM;
  684. biosr = uv_bios_install_heap((u64)uv_master_nasid, v, (u64 *)uv_biosheap);
  685. if (biosr) {
  686. vfree(uv_biosheap);
  687. return -EINVAL;
  688. }
  689. biosr = uv_bios_obj_count((u64)uv_master_nasid, sizeof(u64), &v);
  690. if (biosr) {
  691. vfree(uv_biosheap);
  692. return -EINVAL;
  693. }
  694. uv_bios_obj_cnt = (int)v;
  695. return 0;
  696. }
  697. static struct attribute *hubless_base_attrs[] = {
  698. &partition_id_attr.attr,
  699. &uv_type_attr.attr,
  700. &uv_archtype_attr.attr,
  701. &uv_hubless_attr.attr,
  702. NULL,
  703. };
  704. static const struct attribute_group hubless_base_attr_group = {
  705. .attrs = hubless_base_attrs
  706. };
  707. static int __init uv_sysfs_hubless_init(void)
  708. {
  709. int ret;
  710. ret = sysfs_create_group(sgi_uv_kobj, &hubless_base_attr_group);
  711. if (ret) {
  712. pr_warn("sysfs_create_group hubless_base_attr_group failed\n");
  713. kobject_put(sgi_uv_kobj);
  714. }
  715. return ret;
  716. }
  717. static int __init uv_sysfs_init(void)
  718. {
  719. int ret = 0;
  720. if (!is_uv_system() && !uv_get_hubless_system())
  721. return -ENODEV;
  722. num_cnodes = uv_num_possible_blades();
  723. if (!sgi_uv_kobj)
  724. sgi_uv_kobj = kobject_create_and_add("sgi_uv", firmware_kobj);
  725. if (!sgi_uv_kobj) {
  726. pr_warn("kobject_create_and_add sgi_uv failed\n");
  727. return -EINVAL;
  728. }
  729. if (uv_get_hubless_system())
  730. return uv_sysfs_hubless_init();
  731. ret = sysfs_create_group(sgi_uv_kobj, &base_attr_group);
  732. if (ret) {
  733. pr_warn("sysfs_create_group base_attr_group failed\n");
  734. goto err_create_group;
  735. }
  736. ret = initial_bios_setup();
  737. if (ret)
  738. goto err_bios_setup;
  739. ret = uv_hubs_init();
  740. if (ret)
  741. goto err_hubs_init;
  742. ret = uv_ports_init();
  743. if (ret)
  744. goto err_ports_init;
  745. ret = pci_topology_init();
  746. if (ret)
  747. goto err_pci_init;
  748. return 0;
  749. err_pci_init:
  750. uv_ports_exit();
  751. err_ports_init:
  752. uv_hubs_exit();
  753. err_hubs_init:
  754. vfree(uv_biosheap);
  755. err_bios_setup:
  756. sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
  757. err_create_group:
  758. kobject_put(sgi_uv_kobj);
  759. return ret;
  760. }
  761. static void __exit uv_sysfs_hubless_exit(void)
  762. {
  763. sysfs_remove_group(sgi_uv_kobj, &hubless_base_attr_group);
  764. kobject_put(sgi_uv_kobj);
  765. }
  766. static void __exit uv_sysfs_exit(void)
  767. {
  768. if (!is_uv_system()) {
  769. if (uv_get_hubless_system())
  770. uv_sysfs_hubless_exit();
  771. return;
  772. }
  773. pci_topology_exit();
  774. uv_ports_exit();
  775. uv_hubs_exit();
  776. vfree(uv_biosheap);
  777. sysfs_remove_group(sgi_uv_kobj, &base_attr_group);
  778. kobject_put(sgi_uv_kobj);
  779. }
  780. #ifndef MODULE
  781. device_initcall(uv_sysfs_init);
  782. #else
  783. module_init(uv_sysfs_init);
  784. #endif
  785. module_exit(uv_sysfs_exit);
  786. MODULE_AUTHOR("Hewlett Packard Enterprise");
  787. MODULE_DESCRIPTION("Sysfs structure for HPE UV systems");
  788. MODULE_LICENSE("GPL");