sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include "core_priv.h"
  35. #include <linux/slab.h>
  36. #include <linux/stat.h>
  37. #include <linux/string.h>
  38. #include <rdma/ib_mad.h>
  39. struct ib_port {
  40. struct kobject kobj;
  41. struct ib_device *ibdev;
  42. struct attribute_group gid_group;
  43. struct attribute_group pkey_group;
  44. u8 port_num;
  45. };
  46. struct port_attribute {
  47. struct attribute attr;
  48. ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
  49. ssize_t (*store)(struct ib_port *, struct port_attribute *,
  50. const char *buf, size_t count);
  51. };
  52. #define PORT_ATTR(_name, _mode, _show, _store) \
  53. struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
  54. #define PORT_ATTR_RO(_name) \
  55. struct port_attribute port_attr_##_name = __ATTR_RO(_name)
  56. struct port_table_attribute {
  57. struct port_attribute attr;
  58. char name[8];
  59. int index;
  60. };
  61. static ssize_t port_attr_show(struct kobject *kobj,
  62. struct attribute *attr, char *buf)
  63. {
  64. struct port_attribute *port_attr =
  65. container_of(attr, struct port_attribute, attr);
  66. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  67. if (!port_attr->show)
  68. return -EIO;
  69. return port_attr->show(p, port_attr, buf);
  70. }
  71. static const struct sysfs_ops port_sysfs_ops = {
  72. .show = port_attr_show
  73. };
  74. static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
  75. char *buf)
  76. {
  77. struct ib_port_attr attr;
  78. ssize_t ret;
  79. static const char *state_name[] = {
  80. [IB_PORT_NOP] = "NOP",
  81. [IB_PORT_DOWN] = "DOWN",
  82. [IB_PORT_INIT] = "INIT",
  83. [IB_PORT_ARMED] = "ARMED",
  84. [IB_PORT_ACTIVE] = "ACTIVE",
  85. [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
  86. };
  87. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  88. if (ret)
  89. return ret;
  90. return sprintf(buf, "%d: %s\n", attr.state,
  91. attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
  92. state_name[attr.state] : "UNKNOWN");
  93. }
  94. static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
  95. char *buf)
  96. {
  97. struct ib_port_attr attr;
  98. ssize_t ret;
  99. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  100. if (ret)
  101. return ret;
  102. return sprintf(buf, "0x%x\n", attr.lid);
  103. }
  104. static ssize_t lid_mask_count_show(struct ib_port *p,
  105. struct port_attribute *unused,
  106. char *buf)
  107. {
  108. struct ib_port_attr attr;
  109. ssize_t ret;
  110. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  111. if (ret)
  112. return ret;
  113. return sprintf(buf, "%d\n", attr.lmc);
  114. }
  115. static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
  116. char *buf)
  117. {
  118. struct ib_port_attr attr;
  119. ssize_t ret;
  120. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  121. if (ret)
  122. return ret;
  123. return sprintf(buf, "0x%x\n", attr.sm_lid);
  124. }
  125. static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
  126. char *buf)
  127. {
  128. struct ib_port_attr attr;
  129. ssize_t ret;
  130. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  131. if (ret)
  132. return ret;
  133. return sprintf(buf, "%d\n", attr.sm_sl);
  134. }
  135. static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
  136. char *buf)
  137. {
  138. struct ib_port_attr attr;
  139. ssize_t ret;
  140. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  141. if (ret)
  142. return ret;
  143. return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
  144. }
  145. static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
  146. char *buf)
  147. {
  148. struct ib_port_attr attr;
  149. char *speed = "";
  150. int rate; /* in deci-Gb/sec */
  151. ssize_t ret;
  152. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  153. if (ret)
  154. return ret;
  155. switch (attr.active_speed) {
  156. case IB_SPEED_DDR:
  157. speed = " DDR";
  158. rate = 50;
  159. break;
  160. case IB_SPEED_QDR:
  161. speed = " QDR";
  162. rate = 100;
  163. break;
  164. case IB_SPEED_FDR10:
  165. speed = " FDR10";
  166. rate = 100;
  167. break;
  168. case IB_SPEED_FDR:
  169. speed = " FDR";
  170. rate = 140;
  171. break;
  172. case IB_SPEED_EDR:
  173. speed = " EDR";
  174. rate = 250;
  175. break;
  176. case IB_SPEED_SDR:
  177. default: /* default to SDR for invalid rates */
  178. rate = 25;
  179. break;
  180. }
  181. rate *= ib_width_enum_to_int(attr.active_width);
  182. if (rate < 0)
  183. return -EINVAL;
  184. return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
  185. rate / 10, rate % 10 ? ".5" : "",
  186. ib_width_enum_to_int(attr.active_width), speed);
  187. }
  188. static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
  189. char *buf)
  190. {
  191. struct ib_port_attr attr;
  192. ssize_t ret;
  193. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  194. if (ret)
  195. return ret;
  196. switch (attr.phys_state) {
  197. case 1: return sprintf(buf, "1: Sleep\n");
  198. case 2: return sprintf(buf, "2: Polling\n");
  199. case 3: return sprintf(buf, "3: Disabled\n");
  200. case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
  201. case 5: return sprintf(buf, "5: LinkUp\n");
  202. case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
  203. case 7: return sprintf(buf, "7: Phy Test\n");
  204. default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
  205. }
  206. }
  207. static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
  208. char *buf)
  209. {
  210. switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
  211. case IB_LINK_LAYER_INFINIBAND:
  212. return sprintf(buf, "%s\n", "InfiniBand");
  213. case IB_LINK_LAYER_ETHERNET:
  214. return sprintf(buf, "%s\n", "Ethernet");
  215. default:
  216. return sprintf(buf, "%s\n", "Unknown");
  217. }
  218. }
  219. static PORT_ATTR_RO(state);
  220. static PORT_ATTR_RO(lid);
  221. static PORT_ATTR_RO(lid_mask_count);
  222. static PORT_ATTR_RO(sm_lid);
  223. static PORT_ATTR_RO(sm_sl);
  224. static PORT_ATTR_RO(cap_mask);
  225. static PORT_ATTR_RO(rate);
  226. static PORT_ATTR_RO(phys_state);
  227. static PORT_ATTR_RO(link_layer);
  228. static struct attribute *port_default_attrs[] = {
  229. &port_attr_state.attr,
  230. &port_attr_lid.attr,
  231. &port_attr_lid_mask_count.attr,
  232. &port_attr_sm_lid.attr,
  233. &port_attr_sm_sl.attr,
  234. &port_attr_cap_mask.attr,
  235. &port_attr_rate.attr,
  236. &port_attr_phys_state.attr,
  237. &port_attr_link_layer.attr,
  238. NULL
  239. };
  240. static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
  241. char *buf)
  242. {
  243. struct port_table_attribute *tab_attr =
  244. container_of(attr, struct port_table_attribute, attr);
  245. union ib_gid gid;
  246. ssize_t ret;
  247. ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
  248. if (ret)
  249. return ret;
  250. return sprintf(buf, "%pI6\n", gid.raw);
  251. }
  252. static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
  253. char *buf)
  254. {
  255. struct port_table_attribute *tab_attr =
  256. container_of(attr, struct port_table_attribute, attr);
  257. u16 pkey;
  258. ssize_t ret;
  259. ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
  260. if (ret)
  261. return ret;
  262. return sprintf(buf, "0x%04x\n", pkey);
  263. }
  264. #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
  265. struct port_table_attribute port_pma_attr_##_name = { \
  266. .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
  267. .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
  268. }
  269. static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
  270. char *buf)
  271. {
  272. struct port_table_attribute *tab_attr =
  273. container_of(attr, struct port_table_attribute, attr);
  274. int offset = tab_attr->index & 0xffff;
  275. int width = (tab_attr->index >> 16) & 0xff;
  276. struct ib_mad *in_mad = NULL;
  277. struct ib_mad *out_mad = NULL;
  278. ssize_t ret;
  279. if (!p->ibdev->process_mad)
  280. return sprintf(buf, "N/A (no PMA)\n");
  281. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  282. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  283. if (!in_mad || !out_mad) {
  284. ret = -ENOMEM;
  285. goto out;
  286. }
  287. in_mad->mad_hdr.base_version = 1;
  288. in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
  289. in_mad->mad_hdr.class_version = 1;
  290. in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  291. in_mad->mad_hdr.attr_id = cpu_to_be16(0x12); /* PortCounters */
  292. in_mad->data[41] = p->port_num; /* PortSelect field */
  293. if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
  294. p->port_num, NULL, NULL, in_mad, out_mad) &
  295. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
  296. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
  297. ret = -EINVAL;
  298. goto out;
  299. }
  300. switch (width) {
  301. case 4:
  302. ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
  303. (4 - (offset % 8))) & 0xf);
  304. break;
  305. case 8:
  306. ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
  307. break;
  308. case 16:
  309. ret = sprintf(buf, "%u\n",
  310. be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
  311. break;
  312. case 32:
  313. ret = sprintf(buf, "%u\n",
  314. be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
  315. break;
  316. default:
  317. ret = 0;
  318. }
  319. out:
  320. kfree(in_mad);
  321. kfree(out_mad);
  322. return ret;
  323. }
  324. static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
  325. static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
  326. static PORT_PMA_ATTR(link_downed , 2, 8, 56);
  327. static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
  328. static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
  329. static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
  330. static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
  331. static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
  332. static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
  333. static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
  334. static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
  335. static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
  336. static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
  337. static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
  338. static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
  339. static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
  340. static struct attribute *pma_attrs[] = {
  341. &port_pma_attr_symbol_error.attr.attr,
  342. &port_pma_attr_link_error_recovery.attr.attr,
  343. &port_pma_attr_link_downed.attr.attr,
  344. &port_pma_attr_port_rcv_errors.attr.attr,
  345. &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
  346. &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
  347. &port_pma_attr_port_xmit_discards.attr.attr,
  348. &port_pma_attr_port_xmit_constraint_errors.attr.attr,
  349. &port_pma_attr_port_rcv_constraint_errors.attr.attr,
  350. &port_pma_attr_local_link_integrity_errors.attr.attr,
  351. &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
  352. &port_pma_attr_VL15_dropped.attr.attr,
  353. &port_pma_attr_port_xmit_data.attr.attr,
  354. &port_pma_attr_port_rcv_data.attr.attr,
  355. &port_pma_attr_port_xmit_packets.attr.attr,
  356. &port_pma_attr_port_rcv_packets.attr.attr,
  357. NULL
  358. };
  359. static struct attribute_group pma_group = {
  360. .name = "counters",
  361. .attrs = pma_attrs
  362. };
  363. static void ib_port_release(struct kobject *kobj)
  364. {
  365. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  366. struct attribute *a;
  367. int i;
  368. for (i = 0; (a = p->gid_group.attrs[i]); ++i)
  369. kfree(a);
  370. kfree(p->gid_group.attrs);
  371. for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
  372. kfree(a);
  373. kfree(p->pkey_group.attrs);
  374. kfree(p);
  375. }
  376. static struct kobj_type port_type = {
  377. .release = ib_port_release,
  378. .sysfs_ops = &port_sysfs_ops,
  379. .default_attrs = port_default_attrs
  380. };
  381. static void ib_device_release(struct device *device)
  382. {
  383. struct ib_device *dev = container_of(device, struct ib_device, dev);
  384. kfree(dev);
  385. }
  386. static int ib_device_uevent(struct device *device,
  387. struct kobj_uevent_env *env)
  388. {
  389. struct ib_device *dev = container_of(device, struct ib_device, dev);
  390. if (add_uevent_var(env, "NAME=%s", dev->name))
  391. return -ENOMEM;
  392. /*
  393. * It would be nice to pass the node GUID with the event...
  394. */
  395. return 0;
  396. }
  397. static struct attribute **
  398. alloc_group_attrs(ssize_t (*show)(struct ib_port *,
  399. struct port_attribute *, char *buf),
  400. int len)
  401. {
  402. struct attribute **tab_attr;
  403. struct port_table_attribute *element;
  404. int i;
  405. tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
  406. if (!tab_attr)
  407. return NULL;
  408. for (i = 0; i < len; i++) {
  409. element = kzalloc(sizeof(struct port_table_attribute),
  410. GFP_KERNEL);
  411. if (!element)
  412. goto err;
  413. if (snprintf(element->name, sizeof(element->name),
  414. "%d", i) >= sizeof(element->name)) {
  415. kfree(element);
  416. goto err;
  417. }
  418. element->attr.attr.name = element->name;
  419. element->attr.attr.mode = S_IRUGO;
  420. element->attr.show = show;
  421. element->index = i;
  422. sysfs_attr_init(&element->attr.attr);
  423. tab_attr[i] = &element->attr.attr;
  424. }
  425. return tab_attr;
  426. err:
  427. while (--i >= 0)
  428. kfree(tab_attr[i]);
  429. kfree(tab_attr);
  430. return NULL;
  431. }
  432. static int add_port(struct ib_device *device, int port_num,
  433. int (*port_callback)(struct ib_device *,
  434. u8, struct kobject *))
  435. {
  436. struct ib_port *p;
  437. struct ib_port_attr attr;
  438. int i;
  439. int ret;
  440. ret = ib_query_port(device, port_num, &attr);
  441. if (ret)
  442. return ret;
  443. p = kzalloc(sizeof *p, GFP_KERNEL);
  444. if (!p)
  445. return -ENOMEM;
  446. p->ibdev = device;
  447. p->port_num = port_num;
  448. ret = kobject_init_and_add(&p->kobj, &port_type,
  449. kobject_get(device->ports_parent),
  450. "%d", port_num);
  451. if (ret)
  452. goto err_put;
  453. ret = sysfs_create_group(&p->kobj, &pma_group);
  454. if (ret)
  455. goto err_put;
  456. p->gid_group.name = "gids";
  457. p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
  458. if (!p->gid_group.attrs)
  459. goto err_remove_pma;
  460. ret = sysfs_create_group(&p->kobj, &p->gid_group);
  461. if (ret)
  462. goto err_free_gid;
  463. p->pkey_group.name = "pkeys";
  464. p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
  465. attr.pkey_tbl_len);
  466. if (!p->pkey_group.attrs)
  467. goto err_remove_gid;
  468. ret = sysfs_create_group(&p->kobj, &p->pkey_group);
  469. if (ret)
  470. goto err_free_pkey;
  471. if (port_callback) {
  472. ret = port_callback(device, port_num, &p->kobj);
  473. if (ret)
  474. goto err_remove_pkey;
  475. }
  476. list_add_tail(&p->kobj.entry, &device->port_list);
  477. kobject_uevent(&p->kobj, KOBJ_ADD);
  478. return 0;
  479. err_remove_pkey:
  480. sysfs_remove_group(&p->kobj, &p->pkey_group);
  481. err_free_pkey:
  482. for (i = 0; i < attr.pkey_tbl_len; ++i)
  483. kfree(p->pkey_group.attrs[i]);
  484. kfree(p->pkey_group.attrs);
  485. err_remove_gid:
  486. sysfs_remove_group(&p->kobj, &p->gid_group);
  487. err_free_gid:
  488. for (i = 0; i < attr.gid_tbl_len; ++i)
  489. kfree(p->gid_group.attrs[i]);
  490. kfree(p->gid_group.attrs);
  491. err_remove_pma:
  492. sysfs_remove_group(&p->kobj, &pma_group);
  493. err_put:
  494. kobject_put(device->ports_parent);
  495. kfree(p);
  496. return ret;
  497. }
  498. static ssize_t show_node_type(struct device *device,
  499. struct device_attribute *attr, char *buf)
  500. {
  501. struct ib_device *dev = container_of(device, struct ib_device, dev);
  502. switch (dev->node_type) {
  503. case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
  504. case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
  505. case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
  506. case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
  507. default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
  508. }
  509. }
  510. static ssize_t show_sys_image_guid(struct device *device,
  511. struct device_attribute *dev_attr, char *buf)
  512. {
  513. struct ib_device *dev = container_of(device, struct ib_device, dev);
  514. struct ib_device_attr attr;
  515. ssize_t ret;
  516. ret = ib_query_device(dev, &attr);
  517. if (ret)
  518. return ret;
  519. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  520. be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
  521. be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
  522. be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
  523. be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
  524. }
  525. static ssize_t show_node_guid(struct device *device,
  526. struct device_attribute *attr, char *buf)
  527. {
  528. struct ib_device *dev = container_of(device, struct ib_device, dev);
  529. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  530. be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
  531. be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
  532. be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
  533. be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
  534. }
  535. static ssize_t show_node_desc(struct device *device,
  536. struct device_attribute *attr, char *buf)
  537. {
  538. struct ib_device *dev = container_of(device, struct ib_device, dev);
  539. return sprintf(buf, "%.64s\n", dev->node_desc);
  540. }
  541. static ssize_t set_node_desc(struct device *device,
  542. struct device_attribute *attr,
  543. const char *buf, size_t count)
  544. {
  545. struct ib_device *dev = container_of(device, struct ib_device, dev);
  546. struct ib_device_modify desc = {};
  547. int ret;
  548. if (!dev->modify_device)
  549. return -EIO;
  550. memcpy(desc.node_desc, buf, min_t(int, count, 64));
  551. ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
  552. if (ret)
  553. return ret;
  554. return count;
  555. }
  556. static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
  557. static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
  558. static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
  559. static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
  560. static struct device_attribute *ib_class_attributes[] = {
  561. &dev_attr_node_type,
  562. &dev_attr_sys_image_guid,
  563. &dev_attr_node_guid,
  564. &dev_attr_node_desc
  565. };
  566. static struct class ib_class = {
  567. .name = "infiniband",
  568. .dev_release = ib_device_release,
  569. .dev_uevent = ib_device_uevent,
  570. };
  571. /* Show a given an attribute in the statistics group */
  572. static ssize_t show_protocol_stat(const struct device *device,
  573. struct device_attribute *attr, char *buf,
  574. unsigned offset)
  575. {
  576. struct ib_device *dev = container_of(device, struct ib_device, dev);
  577. union rdma_protocol_stats stats;
  578. ssize_t ret;
  579. ret = dev->get_protocol_stats(dev, &stats);
  580. if (ret)
  581. return ret;
  582. return sprintf(buf, "%llu\n",
  583. (unsigned long long) ((u64 *) &stats)[offset]);
  584. }
  585. /* generate a read-only iwarp statistics attribute */
  586. #define IW_STATS_ENTRY(name) \
  587. static ssize_t show_##name(struct device *device, \
  588. struct device_attribute *attr, char *buf) \
  589. { \
  590. return show_protocol_stat(device, attr, buf, \
  591. offsetof(struct iw_protocol_stats, name) / \
  592. sizeof (u64)); \
  593. } \
  594. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  595. IW_STATS_ENTRY(ipInReceives);
  596. IW_STATS_ENTRY(ipInHdrErrors);
  597. IW_STATS_ENTRY(ipInTooBigErrors);
  598. IW_STATS_ENTRY(ipInNoRoutes);
  599. IW_STATS_ENTRY(ipInAddrErrors);
  600. IW_STATS_ENTRY(ipInUnknownProtos);
  601. IW_STATS_ENTRY(ipInTruncatedPkts);
  602. IW_STATS_ENTRY(ipInDiscards);
  603. IW_STATS_ENTRY(ipInDelivers);
  604. IW_STATS_ENTRY(ipOutForwDatagrams);
  605. IW_STATS_ENTRY(ipOutRequests);
  606. IW_STATS_ENTRY(ipOutDiscards);
  607. IW_STATS_ENTRY(ipOutNoRoutes);
  608. IW_STATS_ENTRY(ipReasmTimeout);
  609. IW_STATS_ENTRY(ipReasmReqds);
  610. IW_STATS_ENTRY(ipReasmOKs);
  611. IW_STATS_ENTRY(ipReasmFails);
  612. IW_STATS_ENTRY(ipFragOKs);
  613. IW_STATS_ENTRY(ipFragFails);
  614. IW_STATS_ENTRY(ipFragCreates);
  615. IW_STATS_ENTRY(ipInMcastPkts);
  616. IW_STATS_ENTRY(ipOutMcastPkts);
  617. IW_STATS_ENTRY(ipInBcastPkts);
  618. IW_STATS_ENTRY(ipOutBcastPkts);
  619. IW_STATS_ENTRY(tcpRtoAlgorithm);
  620. IW_STATS_ENTRY(tcpRtoMin);
  621. IW_STATS_ENTRY(tcpRtoMax);
  622. IW_STATS_ENTRY(tcpMaxConn);
  623. IW_STATS_ENTRY(tcpActiveOpens);
  624. IW_STATS_ENTRY(tcpPassiveOpens);
  625. IW_STATS_ENTRY(tcpAttemptFails);
  626. IW_STATS_ENTRY(tcpEstabResets);
  627. IW_STATS_ENTRY(tcpCurrEstab);
  628. IW_STATS_ENTRY(tcpInSegs);
  629. IW_STATS_ENTRY(tcpOutSegs);
  630. IW_STATS_ENTRY(tcpRetransSegs);
  631. IW_STATS_ENTRY(tcpInErrs);
  632. IW_STATS_ENTRY(tcpOutRsts);
  633. static struct attribute *iw_proto_stats_attrs[] = {
  634. &dev_attr_ipInReceives.attr,
  635. &dev_attr_ipInHdrErrors.attr,
  636. &dev_attr_ipInTooBigErrors.attr,
  637. &dev_attr_ipInNoRoutes.attr,
  638. &dev_attr_ipInAddrErrors.attr,
  639. &dev_attr_ipInUnknownProtos.attr,
  640. &dev_attr_ipInTruncatedPkts.attr,
  641. &dev_attr_ipInDiscards.attr,
  642. &dev_attr_ipInDelivers.attr,
  643. &dev_attr_ipOutForwDatagrams.attr,
  644. &dev_attr_ipOutRequests.attr,
  645. &dev_attr_ipOutDiscards.attr,
  646. &dev_attr_ipOutNoRoutes.attr,
  647. &dev_attr_ipReasmTimeout.attr,
  648. &dev_attr_ipReasmReqds.attr,
  649. &dev_attr_ipReasmOKs.attr,
  650. &dev_attr_ipReasmFails.attr,
  651. &dev_attr_ipFragOKs.attr,
  652. &dev_attr_ipFragFails.attr,
  653. &dev_attr_ipFragCreates.attr,
  654. &dev_attr_ipInMcastPkts.attr,
  655. &dev_attr_ipOutMcastPkts.attr,
  656. &dev_attr_ipInBcastPkts.attr,
  657. &dev_attr_ipOutBcastPkts.attr,
  658. &dev_attr_tcpRtoAlgorithm.attr,
  659. &dev_attr_tcpRtoMin.attr,
  660. &dev_attr_tcpRtoMax.attr,
  661. &dev_attr_tcpMaxConn.attr,
  662. &dev_attr_tcpActiveOpens.attr,
  663. &dev_attr_tcpPassiveOpens.attr,
  664. &dev_attr_tcpAttemptFails.attr,
  665. &dev_attr_tcpEstabResets.attr,
  666. &dev_attr_tcpCurrEstab.attr,
  667. &dev_attr_tcpInSegs.attr,
  668. &dev_attr_tcpOutSegs.attr,
  669. &dev_attr_tcpRetransSegs.attr,
  670. &dev_attr_tcpInErrs.attr,
  671. &dev_attr_tcpOutRsts.attr,
  672. NULL
  673. };
  674. static struct attribute_group iw_stats_group = {
  675. .name = "proto_stats",
  676. .attrs = iw_proto_stats_attrs,
  677. };
  678. int ib_device_register_sysfs(struct ib_device *device,
  679. int (*port_callback)(struct ib_device *,
  680. u8, struct kobject *))
  681. {
  682. struct device *class_dev = &device->dev;
  683. int ret;
  684. int i;
  685. class_dev->class = &ib_class;
  686. class_dev->parent = device->dma_device;
  687. dev_set_name(class_dev, device->name);
  688. dev_set_drvdata(class_dev, device);
  689. INIT_LIST_HEAD(&device->port_list);
  690. ret = device_register(class_dev);
  691. if (ret)
  692. goto err;
  693. for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
  694. ret = device_create_file(class_dev, ib_class_attributes[i]);
  695. if (ret)
  696. goto err_unregister;
  697. }
  698. device->ports_parent = kobject_create_and_add("ports",
  699. kobject_get(&class_dev->kobj));
  700. if (!device->ports_parent) {
  701. ret = -ENOMEM;
  702. goto err_put;
  703. }
  704. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  705. ret = add_port(device, 0, port_callback);
  706. if (ret)
  707. goto err_put;
  708. } else {
  709. for (i = 1; i <= device->phys_port_cnt; ++i) {
  710. ret = add_port(device, i, port_callback);
  711. if (ret)
  712. goto err_put;
  713. }
  714. }
  715. if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
  716. ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
  717. if (ret)
  718. goto err_put;
  719. }
  720. return 0;
  721. err_put:
  722. {
  723. struct kobject *p, *t;
  724. struct ib_port *port;
  725. list_for_each_entry_safe(p, t, &device->port_list, entry) {
  726. list_del(&p->entry);
  727. port = container_of(p, struct ib_port, kobj);
  728. sysfs_remove_group(p, &pma_group);
  729. sysfs_remove_group(p, &port->pkey_group);
  730. sysfs_remove_group(p, &port->gid_group);
  731. kobject_put(p);
  732. }
  733. }
  734. kobject_put(&class_dev->kobj);
  735. err_unregister:
  736. device_unregister(class_dev);
  737. err:
  738. return ret;
  739. }
  740. void ib_device_unregister_sysfs(struct ib_device *device)
  741. {
  742. struct kobject *p, *t;
  743. struct ib_port *port;
  744. /* Hold kobject until ib_dealloc_device() */
  745. kobject_get(&device->dev.kobj);
  746. list_for_each_entry_safe(p, t, &device->port_list, entry) {
  747. list_del(&p->entry);
  748. port = container_of(p, struct ib_port, kobj);
  749. sysfs_remove_group(p, &pma_group);
  750. sysfs_remove_group(p, &port->pkey_group);
  751. sysfs_remove_group(p, &port->gid_group);
  752. kobject_put(p);
  753. }
  754. kobject_put(device->ports_parent);
  755. device_unregister(&device->dev);
  756. }
  757. int ib_sysfs_setup(void)
  758. {
  759. return class_register(&ib_class);
  760. }
  761. void ib_sysfs_cleanup(void)
  762. {
  763. class_unregister(&ib_class);
  764. }