sysfs.c 23 KB

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