sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. * $Id: sysfs.c 1349 2004-12-16 21:09:43Z roland $
  33. */
  34. #include "core_priv.h"
  35. #include <ib_mad.h>
  36. struct ib_port {
  37. struct kobject kobj;
  38. struct ib_device *ibdev;
  39. struct attribute_group gid_group;
  40. struct attribute **gid_attr;
  41. struct attribute_group pkey_group;
  42. struct attribute **pkey_attr;
  43. u8 port_num;
  44. };
  45. struct port_attribute {
  46. struct attribute attr;
  47. ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
  48. ssize_t (*store)(struct ib_port *, struct port_attribute *,
  49. const char *buf, size_t count);
  50. };
  51. #define PORT_ATTR(_name, _mode, _show, _store) \
  52. struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
  53. #define PORT_ATTR_RO(_name) \
  54. struct port_attribute port_attr_##_name = __ATTR_RO(_name)
  55. struct port_table_attribute {
  56. struct port_attribute attr;
  57. int index;
  58. };
  59. static ssize_t port_attr_show(struct kobject *kobj,
  60. struct attribute *attr, char *buf)
  61. {
  62. struct port_attribute *port_attr =
  63. container_of(attr, struct port_attribute, attr);
  64. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  65. if (!port_attr->show)
  66. return 0;
  67. return port_attr->show(p, port_attr, buf);
  68. }
  69. static struct sysfs_ops port_sysfs_ops = {
  70. .show = port_attr_show
  71. };
  72. static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
  73. char *buf)
  74. {
  75. struct ib_port_attr attr;
  76. ssize_t ret;
  77. static const char *state_name[] = {
  78. [IB_PORT_NOP] = "NOP",
  79. [IB_PORT_DOWN] = "DOWN",
  80. [IB_PORT_INIT] = "INIT",
  81. [IB_PORT_ARMED] = "ARMED",
  82. [IB_PORT_ACTIVE] = "ACTIVE",
  83. [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
  84. };
  85. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  86. if (ret)
  87. return ret;
  88. return sprintf(buf, "%d: %s\n", attr.state,
  89. attr.state >= 0 && attr.state <= ARRAY_SIZE(state_name) ?
  90. state_name[attr.state] : "UNKNOWN");
  91. }
  92. static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
  93. char *buf)
  94. {
  95. struct ib_port_attr attr;
  96. ssize_t ret;
  97. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  98. if (ret)
  99. return ret;
  100. return sprintf(buf, "0x%x\n", attr.lid);
  101. }
  102. static ssize_t lid_mask_count_show(struct ib_port *p,
  103. struct port_attribute *unused,
  104. char *buf)
  105. {
  106. struct ib_port_attr attr;
  107. ssize_t ret;
  108. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  109. if (ret)
  110. return ret;
  111. return sprintf(buf, "%d\n", attr.lmc);
  112. }
  113. static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
  114. char *buf)
  115. {
  116. struct ib_port_attr attr;
  117. ssize_t ret;
  118. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  119. if (ret)
  120. return ret;
  121. return sprintf(buf, "0x%x\n", attr.sm_lid);
  122. }
  123. static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
  124. char *buf)
  125. {
  126. struct ib_port_attr attr;
  127. ssize_t ret;
  128. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  129. if (ret)
  130. return ret;
  131. return sprintf(buf, "%d\n", attr.sm_sl);
  132. }
  133. static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
  134. char *buf)
  135. {
  136. struct ib_port_attr attr;
  137. ssize_t ret;
  138. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  139. if (ret)
  140. return ret;
  141. return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
  142. }
  143. static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
  144. char *buf)
  145. {
  146. struct ib_port_attr attr;
  147. char *speed = "";
  148. int rate;
  149. ssize_t ret;
  150. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  151. if (ret)
  152. return ret;
  153. switch (attr.active_speed) {
  154. case 2: speed = " DDR"; break;
  155. case 4: speed = " QDR"; break;
  156. }
  157. rate = 25 * ib_width_enum_to_int(attr.active_width) * attr.active_speed;
  158. if (rate < 0)
  159. return -EINVAL;
  160. return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
  161. rate / 10, rate % 10 ? ".5" : "",
  162. ib_width_enum_to_int(attr.active_width), speed);
  163. }
  164. static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
  165. char *buf)
  166. {
  167. struct ib_port_attr attr;
  168. ssize_t ret;
  169. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  170. if (ret)
  171. return ret;
  172. switch (attr.phys_state) {
  173. case 1: return sprintf(buf, "1: Sleep\n");
  174. case 2: return sprintf(buf, "2: Polling\n");
  175. case 3: return sprintf(buf, "3: Disabled\n");
  176. case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
  177. case 5: return sprintf(buf, "5: LinkUp\n");
  178. case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
  179. case 7: return sprintf(buf, "7: Phy Test\n");
  180. default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
  181. }
  182. }
  183. static PORT_ATTR_RO(state);
  184. static PORT_ATTR_RO(lid);
  185. static PORT_ATTR_RO(lid_mask_count);
  186. static PORT_ATTR_RO(sm_lid);
  187. static PORT_ATTR_RO(sm_sl);
  188. static PORT_ATTR_RO(cap_mask);
  189. static PORT_ATTR_RO(rate);
  190. static PORT_ATTR_RO(phys_state);
  191. static struct attribute *port_default_attrs[] = {
  192. &port_attr_state.attr,
  193. &port_attr_lid.attr,
  194. &port_attr_lid_mask_count.attr,
  195. &port_attr_sm_lid.attr,
  196. &port_attr_sm_sl.attr,
  197. &port_attr_cap_mask.attr,
  198. &port_attr_rate.attr,
  199. &port_attr_phys_state.attr,
  200. NULL
  201. };
  202. static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
  203. char *buf)
  204. {
  205. struct port_table_attribute *tab_attr =
  206. container_of(attr, struct port_table_attribute, attr);
  207. union ib_gid gid;
  208. ssize_t ret;
  209. ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
  210. if (ret)
  211. return ret;
  212. return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  213. be16_to_cpu(((u16 *) gid.raw)[0]),
  214. be16_to_cpu(((u16 *) gid.raw)[1]),
  215. be16_to_cpu(((u16 *) gid.raw)[2]),
  216. be16_to_cpu(((u16 *) gid.raw)[3]),
  217. be16_to_cpu(((u16 *) gid.raw)[4]),
  218. be16_to_cpu(((u16 *) gid.raw)[5]),
  219. be16_to_cpu(((u16 *) gid.raw)[6]),
  220. be16_to_cpu(((u16 *) gid.raw)[7]));
  221. }
  222. static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
  223. char *buf)
  224. {
  225. struct port_table_attribute *tab_attr =
  226. container_of(attr, struct port_table_attribute, attr);
  227. u16 pkey;
  228. ssize_t ret;
  229. ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
  230. if (ret)
  231. return ret;
  232. return sprintf(buf, "0x%04x\n", pkey);
  233. }
  234. #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
  235. struct port_table_attribute port_pma_attr_##_name = { \
  236. .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
  237. .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
  238. }
  239. static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
  240. char *buf)
  241. {
  242. struct port_table_attribute *tab_attr =
  243. container_of(attr, struct port_table_attribute, attr);
  244. int offset = tab_attr->index & 0xffff;
  245. int width = (tab_attr->index >> 16) & 0xff;
  246. struct ib_mad *in_mad = NULL;
  247. struct ib_mad *out_mad = NULL;
  248. ssize_t ret;
  249. if (!p->ibdev->process_mad)
  250. return sprintf(buf, "N/A (no PMA)\n");
  251. in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  252. out_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
  253. if (!in_mad || !out_mad) {
  254. ret = -ENOMEM;
  255. goto out;
  256. }
  257. memset(in_mad, 0, sizeof *in_mad);
  258. in_mad->mad_hdr.base_version = 1;
  259. in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
  260. in_mad->mad_hdr.class_version = 1;
  261. in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  262. in_mad->mad_hdr.attr_id = cpu_to_be16(0x12); /* PortCounters */
  263. in_mad->data[41] = p->port_num; /* PortSelect field */
  264. if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
  265. p->port_num, NULL, NULL, in_mad, out_mad) &
  266. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
  267. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
  268. ret = -EINVAL;
  269. goto out;
  270. }
  271. switch (width) {
  272. case 4:
  273. ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
  274. (offset % 4)) & 0xf);
  275. break;
  276. case 8:
  277. ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
  278. break;
  279. case 16:
  280. ret = sprintf(buf, "%u\n",
  281. be16_to_cpup((u16 *)(out_mad->data + 40 + offset / 8)));
  282. break;
  283. case 32:
  284. ret = sprintf(buf, "%u\n",
  285. be32_to_cpup((u32 *)(out_mad->data + 40 + offset / 8)));
  286. break;
  287. default:
  288. ret = 0;
  289. }
  290. out:
  291. kfree(in_mad);
  292. kfree(out_mad);
  293. return ret;
  294. }
  295. static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
  296. static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
  297. static PORT_PMA_ATTR(link_downed , 2, 8, 56);
  298. static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
  299. static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
  300. static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
  301. static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
  302. static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
  303. static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
  304. static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
  305. static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
  306. static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
  307. static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
  308. static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
  309. static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
  310. static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
  311. static struct attribute *pma_attrs[] = {
  312. &port_pma_attr_symbol_error.attr.attr,
  313. &port_pma_attr_link_error_recovery.attr.attr,
  314. &port_pma_attr_link_downed.attr.attr,
  315. &port_pma_attr_port_rcv_errors.attr.attr,
  316. &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
  317. &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
  318. &port_pma_attr_port_xmit_discards.attr.attr,
  319. &port_pma_attr_port_xmit_constraint_errors.attr.attr,
  320. &port_pma_attr_port_rcv_constraint_errors.attr.attr,
  321. &port_pma_attr_local_link_integrity_errors.attr.attr,
  322. &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
  323. &port_pma_attr_VL15_dropped.attr.attr,
  324. &port_pma_attr_port_xmit_data.attr.attr,
  325. &port_pma_attr_port_rcv_data.attr.attr,
  326. &port_pma_attr_port_xmit_packets.attr.attr,
  327. &port_pma_attr_port_rcv_packets.attr.attr,
  328. NULL
  329. };
  330. static struct attribute_group pma_group = {
  331. .name = "counters",
  332. .attrs = pma_attrs
  333. };
  334. static void ib_port_release(struct kobject *kobj)
  335. {
  336. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  337. struct attribute *a;
  338. int i;
  339. for (i = 0; (a = p->gid_attr[i]); ++i) {
  340. kfree(a->name);
  341. kfree(a);
  342. }
  343. for (i = 0; (a = p->pkey_attr[i]); ++i) {
  344. kfree(a->name);
  345. kfree(a);
  346. }
  347. kfree(p->gid_attr);
  348. kfree(p);
  349. }
  350. static struct kobj_type port_type = {
  351. .release = ib_port_release,
  352. .sysfs_ops = &port_sysfs_ops,
  353. .default_attrs = port_default_attrs
  354. };
  355. static void ib_device_release(struct class_device *cdev)
  356. {
  357. struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
  358. kfree(dev);
  359. }
  360. static int ib_device_hotplug(struct class_device *cdev, char **envp,
  361. int num_envp, char *buf, int size)
  362. {
  363. struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
  364. int i = 0, len = 0;
  365. if (add_hotplug_env_var(envp, num_envp, &i, buf, size, &len,
  366. "NAME=%s", dev->name))
  367. return -ENOMEM;
  368. /*
  369. * It might be nice to pass the node GUID to hotplug, but
  370. * right now the only way to get it is to query the device
  371. * provider, and this can crash during device removal because
  372. * we are will be running after driver removal has started.
  373. * We could add a node_guid field to struct ib_device, or we
  374. * could just let the hotplug script read the node GUID from
  375. * sysfs when devices are added.
  376. */
  377. envp[i] = NULL;
  378. return 0;
  379. }
  380. static int alloc_group(struct attribute ***attr,
  381. ssize_t (*show)(struct ib_port *,
  382. struct port_attribute *, char *buf),
  383. int len)
  384. {
  385. struct port_table_attribute ***tab_attr =
  386. (struct port_table_attribute ***) attr;
  387. int i;
  388. int ret;
  389. *tab_attr = kmalloc((1 + len) * sizeof *tab_attr, GFP_KERNEL);
  390. if (!*tab_attr)
  391. return -ENOMEM;
  392. memset(*tab_attr, 0, (1 + len) * sizeof *tab_attr);
  393. for (i = 0; i < len; ++i) {
  394. (*tab_attr)[i] = kmalloc(sizeof *(*tab_attr)[i], GFP_KERNEL);
  395. if (!(*tab_attr)[i]) {
  396. ret = -ENOMEM;
  397. goto err;
  398. }
  399. memset((*tab_attr)[i], 0, sizeof *(*tab_attr)[i]);
  400. (*tab_attr)[i]->attr.attr.name = kmalloc(8, GFP_KERNEL);
  401. if (!(*tab_attr)[i]->attr.attr.name) {
  402. ret = -ENOMEM;
  403. goto err;
  404. }
  405. if (snprintf((*tab_attr)[i]->attr.attr.name, 8, "%d", i) >= 8) {
  406. ret = -ENOMEM;
  407. goto err;
  408. }
  409. (*tab_attr)[i]->attr.attr.mode = S_IRUGO;
  410. (*tab_attr)[i]->attr.attr.owner = THIS_MODULE;
  411. (*tab_attr)[i]->attr.show = show;
  412. (*tab_attr)[i]->index = i;
  413. }
  414. return 0;
  415. err:
  416. for (i = 0; i < len; ++i) {
  417. if ((*tab_attr)[i])
  418. kfree((*tab_attr)[i]->attr.attr.name);
  419. kfree((*tab_attr)[i]);
  420. }
  421. kfree(*tab_attr);
  422. return ret;
  423. }
  424. static int add_port(struct ib_device *device, int port_num)
  425. {
  426. struct ib_port *p;
  427. struct ib_port_attr attr;
  428. int i;
  429. int ret;
  430. ret = ib_query_port(device, port_num, &attr);
  431. if (ret)
  432. return ret;
  433. p = kmalloc(sizeof *p, GFP_KERNEL);
  434. if (!p)
  435. return -ENOMEM;
  436. memset(p, 0, sizeof *p);
  437. p->ibdev = device;
  438. p->port_num = port_num;
  439. p->kobj.ktype = &port_type;
  440. p->kobj.parent = kobject_get(&device->ports_parent);
  441. if (!p->kobj.parent) {
  442. ret = -EBUSY;
  443. goto err;
  444. }
  445. ret = kobject_set_name(&p->kobj, "%d", port_num);
  446. if (ret)
  447. goto err_put;
  448. ret = kobject_register(&p->kobj);
  449. if (ret)
  450. goto err_put;
  451. ret = sysfs_create_group(&p->kobj, &pma_group);
  452. if (ret)
  453. goto err_put;
  454. ret = alloc_group(&p->gid_attr, show_port_gid, attr.gid_tbl_len);
  455. if (ret)
  456. goto err_remove_pma;
  457. p->gid_group.name = "gids";
  458. p->gid_group.attrs = p->gid_attr;
  459. ret = sysfs_create_group(&p->kobj, &p->gid_group);
  460. if (ret)
  461. goto err_free_gid;
  462. ret = alloc_group(&p->pkey_attr, show_port_pkey, attr.pkey_tbl_len);
  463. if (ret)
  464. goto err_remove_gid;
  465. p->pkey_group.name = "pkeys";
  466. p->pkey_group.attrs = p->pkey_attr;
  467. ret = sysfs_create_group(&p->kobj, &p->pkey_group);
  468. if (ret)
  469. goto err_free_pkey;
  470. list_add_tail(&p->kobj.entry, &device->port_list);
  471. return 0;
  472. err_free_pkey:
  473. for (i = 0; i < attr.pkey_tbl_len; ++i) {
  474. kfree(p->pkey_attr[i]->name);
  475. kfree(p->pkey_attr[i]);
  476. }
  477. kfree(p->pkey_attr);
  478. err_remove_gid:
  479. sysfs_remove_group(&p->kobj, &p->gid_group);
  480. err_free_gid:
  481. for (i = 0; i < attr.gid_tbl_len; ++i) {
  482. kfree(p->gid_attr[i]->name);
  483. kfree(p->gid_attr[i]);
  484. }
  485. kfree(p->gid_attr);
  486. err_remove_pma:
  487. sysfs_remove_group(&p->kobj, &pma_group);
  488. err_put:
  489. kobject_put(&device->ports_parent);
  490. err:
  491. kfree(p);
  492. return ret;
  493. }
  494. static ssize_t show_node_type(struct class_device *cdev, char *buf)
  495. {
  496. struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
  497. switch (dev->node_type) {
  498. case IB_NODE_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
  499. case IB_NODE_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
  500. case IB_NODE_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
  501. default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
  502. }
  503. }
  504. static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf)
  505. {
  506. struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
  507. struct ib_device_attr attr;
  508. ssize_t ret;
  509. ret = ib_query_device(dev, &attr);
  510. if (ret)
  511. return ret;
  512. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  513. be16_to_cpu(((u16 *) &attr.sys_image_guid)[0]),
  514. be16_to_cpu(((u16 *) &attr.sys_image_guid)[1]),
  515. be16_to_cpu(((u16 *) &attr.sys_image_guid)[2]),
  516. be16_to_cpu(((u16 *) &attr.sys_image_guid)[3]));
  517. }
  518. static ssize_t show_node_guid(struct class_device *cdev, char *buf)
  519. {
  520. struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
  521. struct ib_device_attr attr;
  522. ssize_t ret;
  523. ret = ib_query_device(dev, &attr);
  524. if (ret)
  525. return ret;
  526. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  527. be16_to_cpu(((u16 *) &attr.node_guid)[0]),
  528. be16_to_cpu(((u16 *) &attr.node_guid)[1]),
  529. be16_to_cpu(((u16 *) &attr.node_guid)[2]),
  530. be16_to_cpu(((u16 *) &attr.node_guid)[3]));
  531. }
  532. static CLASS_DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
  533. static CLASS_DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
  534. static CLASS_DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
  535. static struct class_device_attribute *ib_class_attributes[] = {
  536. &class_device_attr_node_type,
  537. &class_device_attr_sys_image_guid,
  538. &class_device_attr_node_guid
  539. };
  540. static struct class ib_class = {
  541. .name = "infiniband",
  542. .release = ib_device_release,
  543. .hotplug = ib_device_hotplug,
  544. };
  545. int ib_device_register_sysfs(struct ib_device *device)
  546. {
  547. struct class_device *class_dev = &device->class_dev;
  548. int ret;
  549. int i;
  550. class_dev->class = &ib_class;
  551. class_dev->class_data = device;
  552. strlcpy(class_dev->class_id, device->name, BUS_ID_SIZE);
  553. INIT_LIST_HEAD(&device->port_list);
  554. ret = class_device_register(class_dev);
  555. if (ret)
  556. goto err;
  557. for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
  558. ret = class_device_create_file(class_dev, ib_class_attributes[i]);
  559. if (ret)
  560. goto err_unregister;
  561. }
  562. device->ports_parent.parent = kobject_get(&class_dev->kobj);
  563. if (!device->ports_parent.parent) {
  564. ret = -EBUSY;
  565. goto err_unregister;
  566. }
  567. ret = kobject_set_name(&device->ports_parent, "ports");
  568. if (ret)
  569. goto err_put;
  570. ret = kobject_register(&device->ports_parent);
  571. if (ret)
  572. goto err_put;
  573. if (device->node_type == IB_NODE_SWITCH) {
  574. ret = add_port(device, 0);
  575. if (ret)
  576. goto err_put;
  577. } else {
  578. int i;
  579. for (i = 1; i <= device->phys_port_cnt; ++i) {
  580. ret = add_port(device, i);
  581. if (ret)
  582. goto err_put;
  583. }
  584. }
  585. return 0;
  586. err_put:
  587. {
  588. struct kobject *p, *t;
  589. struct ib_port *port;
  590. list_for_each_entry_safe(p, t, &device->port_list, entry) {
  591. list_del(&p->entry);
  592. port = container_of(p, struct ib_port, kobj);
  593. sysfs_remove_group(p, &pma_group);
  594. sysfs_remove_group(p, &port->pkey_group);
  595. sysfs_remove_group(p, &port->gid_group);
  596. kobject_unregister(p);
  597. }
  598. }
  599. kobject_put(&class_dev->kobj);
  600. err_unregister:
  601. class_device_unregister(class_dev);
  602. err:
  603. return ret;
  604. }
  605. void ib_device_unregister_sysfs(struct ib_device *device)
  606. {
  607. struct kobject *p, *t;
  608. struct ib_port *port;
  609. list_for_each_entry_safe(p, t, &device->port_list, entry) {
  610. list_del(&p->entry);
  611. port = container_of(p, struct ib_port, kobj);
  612. sysfs_remove_group(p, &pma_group);
  613. sysfs_remove_group(p, &port->pkey_group);
  614. sysfs_remove_group(p, &port->gid_group);
  615. kobject_unregister(p);
  616. }
  617. kobject_unregister(&device->ports_parent);
  618. class_device_unregister(&device->class_dev);
  619. }
  620. int ib_sysfs_setup(void)
  621. {
  622. return class_register(&ib_class);
  623. }
  624. void ib_sysfs_cleanup(void)
  625. {
  626. class_unregister(&ib_class);
  627. }