sysfs.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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 <linux/netdevice.h>
  39. #include <linux/ethtool.h>
  40. #include <rdma/ib_mad.h>
  41. #include <rdma/ib_pma.h>
  42. struct ib_port;
  43. struct gid_attr_group {
  44. struct ib_port *port;
  45. struct kobject kobj;
  46. struct attribute_group ndev;
  47. struct attribute_group type;
  48. };
  49. struct ib_port {
  50. struct kobject kobj;
  51. struct ib_device *ibdev;
  52. struct gid_attr_group *gid_attr_group;
  53. struct attribute_group gid_group;
  54. struct attribute_group pkey_group;
  55. struct attribute_group *pma_table;
  56. struct attribute_group *hw_stats_ag;
  57. struct rdma_hw_stats *hw_stats;
  58. u8 port_num;
  59. };
  60. struct port_attribute {
  61. struct attribute attr;
  62. ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
  63. ssize_t (*store)(struct ib_port *, struct port_attribute *,
  64. const char *buf, size_t count);
  65. };
  66. #define PORT_ATTR(_name, _mode, _show, _store) \
  67. struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
  68. #define PORT_ATTR_RO(_name) \
  69. struct port_attribute port_attr_##_name = __ATTR_RO(_name)
  70. struct port_table_attribute {
  71. struct port_attribute attr;
  72. char name[8];
  73. int index;
  74. __be16 attr_id;
  75. };
  76. struct hw_stats_attribute {
  77. struct attribute attr;
  78. ssize_t (*show)(struct kobject *kobj,
  79. struct attribute *attr, char *buf);
  80. ssize_t (*store)(struct kobject *kobj,
  81. struct attribute *attr,
  82. const char *buf,
  83. size_t count);
  84. int index;
  85. u8 port_num;
  86. };
  87. static ssize_t port_attr_show(struct kobject *kobj,
  88. struct attribute *attr, char *buf)
  89. {
  90. struct port_attribute *port_attr =
  91. container_of(attr, struct port_attribute, attr);
  92. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  93. if (!port_attr->show)
  94. return -EIO;
  95. return port_attr->show(p, port_attr, buf);
  96. }
  97. static ssize_t port_attr_store(struct kobject *kobj,
  98. struct attribute *attr,
  99. const char *buf, size_t count)
  100. {
  101. struct port_attribute *port_attr =
  102. container_of(attr, struct port_attribute, attr);
  103. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  104. if (!port_attr->store)
  105. return -EIO;
  106. return port_attr->store(p, port_attr, buf, count);
  107. }
  108. static const struct sysfs_ops port_sysfs_ops = {
  109. .show = port_attr_show,
  110. .store = port_attr_store
  111. };
  112. static ssize_t gid_attr_show(struct kobject *kobj,
  113. struct attribute *attr, char *buf)
  114. {
  115. struct port_attribute *port_attr =
  116. container_of(attr, struct port_attribute, attr);
  117. struct ib_port *p = container_of(kobj, struct gid_attr_group,
  118. kobj)->port;
  119. if (!port_attr->show)
  120. return -EIO;
  121. return port_attr->show(p, port_attr, buf);
  122. }
  123. static const struct sysfs_ops gid_attr_sysfs_ops = {
  124. .show = gid_attr_show
  125. };
  126. static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
  127. char *buf)
  128. {
  129. struct ib_port_attr attr;
  130. ssize_t ret;
  131. static const char *state_name[] = {
  132. [IB_PORT_NOP] = "NOP",
  133. [IB_PORT_DOWN] = "DOWN",
  134. [IB_PORT_INIT] = "INIT",
  135. [IB_PORT_ARMED] = "ARMED",
  136. [IB_PORT_ACTIVE] = "ACTIVE",
  137. [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
  138. };
  139. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  140. if (ret)
  141. return ret;
  142. return sprintf(buf, "%d: %s\n", attr.state,
  143. attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
  144. state_name[attr.state] : "UNKNOWN");
  145. }
  146. static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
  147. char *buf)
  148. {
  149. struct ib_port_attr attr;
  150. ssize_t ret;
  151. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  152. if (ret)
  153. return ret;
  154. return sprintf(buf, "0x%x\n", attr.lid);
  155. }
  156. static ssize_t lid_mask_count_show(struct ib_port *p,
  157. struct port_attribute *unused,
  158. char *buf)
  159. {
  160. struct ib_port_attr attr;
  161. ssize_t ret;
  162. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  163. if (ret)
  164. return ret;
  165. return sprintf(buf, "%d\n", attr.lmc);
  166. }
  167. static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
  168. char *buf)
  169. {
  170. struct ib_port_attr attr;
  171. ssize_t ret;
  172. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  173. if (ret)
  174. return ret;
  175. return sprintf(buf, "0x%x\n", attr.sm_lid);
  176. }
  177. static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
  178. char *buf)
  179. {
  180. struct ib_port_attr attr;
  181. ssize_t ret;
  182. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  183. if (ret)
  184. return ret;
  185. return sprintf(buf, "%d\n", attr.sm_sl);
  186. }
  187. static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
  188. char *buf)
  189. {
  190. struct ib_port_attr attr;
  191. ssize_t ret;
  192. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  193. if (ret)
  194. return ret;
  195. return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
  196. }
  197. static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
  198. char *buf)
  199. {
  200. struct ib_port_attr attr;
  201. char *speed = "";
  202. int rate; /* in deci-Gb/sec */
  203. ssize_t ret;
  204. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  205. if (ret)
  206. return ret;
  207. switch (attr.active_speed) {
  208. case IB_SPEED_DDR:
  209. speed = " DDR";
  210. rate = 50;
  211. break;
  212. case IB_SPEED_QDR:
  213. speed = " QDR";
  214. rate = 100;
  215. break;
  216. case IB_SPEED_FDR10:
  217. speed = " FDR10";
  218. rate = 100;
  219. break;
  220. case IB_SPEED_FDR:
  221. speed = " FDR";
  222. rate = 140;
  223. break;
  224. case IB_SPEED_EDR:
  225. speed = " EDR";
  226. rate = 250;
  227. break;
  228. case IB_SPEED_HDR:
  229. speed = " HDR";
  230. rate = 500;
  231. break;
  232. case IB_SPEED_SDR:
  233. default: /* default to SDR for invalid rates */
  234. rate = 25;
  235. break;
  236. }
  237. rate *= ib_width_enum_to_int(attr.active_width);
  238. if (rate < 0)
  239. return -EINVAL;
  240. return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
  241. rate / 10, rate % 10 ? ".5" : "",
  242. ib_width_enum_to_int(attr.active_width), speed);
  243. }
  244. static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
  245. char *buf)
  246. {
  247. struct ib_port_attr attr;
  248. ssize_t ret;
  249. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  250. if (ret)
  251. return ret;
  252. switch (attr.phys_state) {
  253. case 1: return sprintf(buf, "1: Sleep\n");
  254. case 2: return sprintf(buf, "2: Polling\n");
  255. case 3: return sprintf(buf, "3: Disabled\n");
  256. case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
  257. case 5: return sprintf(buf, "5: LinkUp\n");
  258. case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
  259. case 7: return sprintf(buf, "7: Phy Test\n");
  260. default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
  261. }
  262. }
  263. static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
  264. char *buf)
  265. {
  266. switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
  267. case IB_LINK_LAYER_INFINIBAND:
  268. return sprintf(buf, "%s\n", "InfiniBand");
  269. case IB_LINK_LAYER_ETHERNET:
  270. return sprintf(buf, "%s\n", "Ethernet");
  271. default:
  272. return sprintf(buf, "%s\n", "Unknown");
  273. }
  274. }
  275. static PORT_ATTR_RO(state);
  276. static PORT_ATTR_RO(lid);
  277. static PORT_ATTR_RO(lid_mask_count);
  278. static PORT_ATTR_RO(sm_lid);
  279. static PORT_ATTR_RO(sm_sl);
  280. static PORT_ATTR_RO(cap_mask);
  281. static PORT_ATTR_RO(rate);
  282. static PORT_ATTR_RO(phys_state);
  283. static PORT_ATTR_RO(link_layer);
  284. static struct attribute *port_default_attrs[] = {
  285. &port_attr_state.attr,
  286. &port_attr_lid.attr,
  287. &port_attr_lid_mask_count.attr,
  288. &port_attr_sm_lid.attr,
  289. &port_attr_sm_sl.attr,
  290. &port_attr_cap_mask.attr,
  291. &port_attr_rate.attr,
  292. &port_attr_phys_state.attr,
  293. &port_attr_link_layer.attr,
  294. NULL
  295. };
  296. static size_t print_ndev(struct ib_gid_attr *gid_attr, char *buf)
  297. {
  298. if (!gid_attr->ndev)
  299. return -EINVAL;
  300. return sprintf(buf, "%s\n", gid_attr->ndev->name);
  301. }
  302. static size_t print_gid_type(struct ib_gid_attr *gid_attr, char *buf)
  303. {
  304. return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
  305. }
  306. static ssize_t _show_port_gid_attr(struct ib_port *p,
  307. struct port_attribute *attr,
  308. char *buf,
  309. size_t (*print)(struct ib_gid_attr *gid_attr,
  310. char *buf))
  311. {
  312. struct port_table_attribute *tab_attr =
  313. container_of(attr, struct port_table_attribute, attr);
  314. union ib_gid gid;
  315. struct ib_gid_attr gid_attr = {};
  316. ssize_t ret;
  317. ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid,
  318. &gid_attr);
  319. if (ret)
  320. goto err;
  321. ret = print(&gid_attr, buf);
  322. err:
  323. if (gid_attr.ndev)
  324. dev_put(gid_attr.ndev);
  325. return ret;
  326. }
  327. static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
  328. char *buf)
  329. {
  330. struct port_table_attribute *tab_attr =
  331. container_of(attr, struct port_table_attribute, attr);
  332. union ib_gid gid;
  333. ssize_t ret;
  334. ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid, NULL);
  335. if (ret)
  336. return ret;
  337. return sprintf(buf, "%pI6\n", gid.raw);
  338. }
  339. static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
  340. struct port_attribute *attr, char *buf)
  341. {
  342. return _show_port_gid_attr(p, attr, buf, print_ndev);
  343. }
  344. static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
  345. struct port_attribute *attr,
  346. char *buf)
  347. {
  348. return _show_port_gid_attr(p, attr, buf, print_gid_type);
  349. }
  350. static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
  351. char *buf)
  352. {
  353. struct port_table_attribute *tab_attr =
  354. container_of(attr, struct port_table_attribute, attr);
  355. u16 pkey;
  356. ssize_t ret;
  357. ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
  358. if (ret)
  359. return ret;
  360. return sprintf(buf, "0x%04x\n", pkey);
  361. }
  362. #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
  363. struct port_table_attribute port_pma_attr_##_name = { \
  364. .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
  365. .index = (_offset) | ((_width) << 16) | ((_counter) << 24), \
  366. .attr_id = IB_PMA_PORT_COUNTERS , \
  367. }
  368. #define PORT_PMA_ATTR_EXT(_name, _width, _offset) \
  369. struct port_table_attribute port_pma_attr_ext_##_name = { \
  370. .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
  371. .index = (_offset) | ((_width) << 16), \
  372. .attr_id = IB_PMA_PORT_COUNTERS_EXT , \
  373. }
  374. /*
  375. * Get a Perfmgmt MAD block of data.
  376. * Returns error code or the number of bytes retrieved.
  377. */
  378. static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
  379. void *data, int offset, size_t size)
  380. {
  381. struct ib_mad *in_mad;
  382. struct ib_mad *out_mad;
  383. size_t mad_size = sizeof(*out_mad);
  384. u16 out_mad_pkey_index = 0;
  385. ssize_t ret;
  386. if (!dev->process_mad)
  387. return -ENOSYS;
  388. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  389. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  390. if (!in_mad || !out_mad) {
  391. ret = -ENOMEM;
  392. goto out;
  393. }
  394. in_mad->mad_hdr.base_version = 1;
  395. in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
  396. in_mad->mad_hdr.class_version = 1;
  397. in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  398. in_mad->mad_hdr.attr_id = attr;
  399. if (attr != IB_PMA_CLASS_PORT_INFO)
  400. in_mad->data[41] = port_num; /* PortSelect field */
  401. if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY,
  402. port_num, NULL, NULL,
  403. (const struct ib_mad_hdr *)in_mad, mad_size,
  404. (struct ib_mad_hdr *)out_mad, &mad_size,
  405. &out_mad_pkey_index) &
  406. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
  407. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
  408. ret = -EINVAL;
  409. goto out;
  410. }
  411. memcpy(data, out_mad->data + offset, size);
  412. ret = size;
  413. out:
  414. kfree(in_mad);
  415. kfree(out_mad);
  416. return ret;
  417. }
  418. static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
  419. char *buf)
  420. {
  421. struct port_table_attribute *tab_attr =
  422. container_of(attr, struct port_table_attribute, attr);
  423. int offset = tab_attr->index & 0xffff;
  424. int width = (tab_attr->index >> 16) & 0xff;
  425. ssize_t ret;
  426. u8 data[8];
  427. ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
  428. 40 + offset / 8, sizeof(data));
  429. if (ret < 0)
  430. return sprintf(buf, "N/A (no PMA)\n");
  431. switch (width) {
  432. case 4:
  433. ret = sprintf(buf, "%u\n", (*data >>
  434. (4 - (offset % 8))) & 0xf);
  435. break;
  436. case 8:
  437. ret = sprintf(buf, "%u\n", *data);
  438. break;
  439. case 16:
  440. ret = sprintf(buf, "%u\n",
  441. be16_to_cpup((__be16 *)data));
  442. break;
  443. case 32:
  444. ret = sprintf(buf, "%u\n",
  445. be32_to_cpup((__be32 *)data));
  446. break;
  447. case 64:
  448. ret = sprintf(buf, "%llu\n",
  449. be64_to_cpup((__be64 *)data));
  450. break;
  451. default:
  452. ret = 0;
  453. }
  454. return ret;
  455. }
  456. static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
  457. static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
  458. static PORT_PMA_ATTR(link_downed , 2, 8, 56);
  459. static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
  460. static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
  461. static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
  462. static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
  463. static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
  464. static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
  465. static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
  466. static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
  467. static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
  468. static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
  469. static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
  470. static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
  471. static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
  472. static PORT_PMA_ATTR(port_xmit_wait , 0, 32, 320);
  473. /*
  474. * Counters added by extended set
  475. */
  476. static PORT_PMA_ATTR_EXT(port_xmit_data , 64, 64);
  477. static PORT_PMA_ATTR_EXT(port_rcv_data , 64, 128);
  478. static PORT_PMA_ATTR_EXT(port_xmit_packets , 64, 192);
  479. static PORT_PMA_ATTR_EXT(port_rcv_packets , 64, 256);
  480. static PORT_PMA_ATTR_EXT(unicast_xmit_packets , 64, 320);
  481. static PORT_PMA_ATTR_EXT(unicast_rcv_packets , 64, 384);
  482. static PORT_PMA_ATTR_EXT(multicast_xmit_packets , 64, 448);
  483. static PORT_PMA_ATTR_EXT(multicast_rcv_packets , 64, 512);
  484. static struct attribute *pma_attrs[] = {
  485. &port_pma_attr_symbol_error.attr.attr,
  486. &port_pma_attr_link_error_recovery.attr.attr,
  487. &port_pma_attr_link_downed.attr.attr,
  488. &port_pma_attr_port_rcv_errors.attr.attr,
  489. &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
  490. &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
  491. &port_pma_attr_port_xmit_discards.attr.attr,
  492. &port_pma_attr_port_xmit_constraint_errors.attr.attr,
  493. &port_pma_attr_port_rcv_constraint_errors.attr.attr,
  494. &port_pma_attr_local_link_integrity_errors.attr.attr,
  495. &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
  496. &port_pma_attr_VL15_dropped.attr.attr,
  497. &port_pma_attr_port_xmit_data.attr.attr,
  498. &port_pma_attr_port_rcv_data.attr.attr,
  499. &port_pma_attr_port_xmit_packets.attr.attr,
  500. &port_pma_attr_port_rcv_packets.attr.attr,
  501. &port_pma_attr_port_xmit_wait.attr.attr,
  502. NULL
  503. };
  504. static struct attribute *pma_attrs_ext[] = {
  505. &port_pma_attr_symbol_error.attr.attr,
  506. &port_pma_attr_link_error_recovery.attr.attr,
  507. &port_pma_attr_link_downed.attr.attr,
  508. &port_pma_attr_port_rcv_errors.attr.attr,
  509. &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
  510. &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
  511. &port_pma_attr_port_xmit_discards.attr.attr,
  512. &port_pma_attr_port_xmit_constraint_errors.attr.attr,
  513. &port_pma_attr_port_rcv_constraint_errors.attr.attr,
  514. &port_pma_attr_local_link_integrity_errors.attr.attr,
  515. &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
  516. &port_pma_attr_VL15_dropped.attr.attr,
  517. &port_pma_attr_ext_port_xmit_data.attr.attr,
  518. &port_pma_attr_ext_port_rcv_data.attr.attr,
  519. &port_pma_attr_ext_port_xmit_packets.attr.attr,
  520. &port_pma_attr_port_xmit_wait.attr.attr,
  521. &port_pma_attr_ext_port_rcv_packets.attr.attr,
  522. &port_pma_attr_ext_unicast_rcv_packets.attr.attr,
  523. &port_pma_attr_ext_unicast_xmit_packets.attr.attr,
  524. &port_pma_attr_ext_multicast_rcv_packets.attr.attr,
  525. &port_pma_attr_ext_multicast_xmit_packets.attr.attr,
  526. NULL
  527. };
  528. static struct attribute *pma_attrs_noietf[] = {
  529. &port_pma_attr_symbol_error.attr.attr,
  530. &port_pma_attr_link_error_recovery.attr.attr,
  531. &port_pma_attr_link_downed.attr.attr,
  532. &port_pma_attr_port_rcv_errors.attr.attr,
  533. &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
  534. &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
  535. &port_pma_attr_port_xmit_discards.attr.attr,
  536. &port_pma_attr_port_xmit_constraint_errors.attr.attr,
  537. &port_pma_attr_port_rcv_constraint_errors.attr.attr,
  538. &port_pma_attr_local_link_integrity_errors.attr.attr,
  539. &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
  540. &port_pma_attr_VL15_dropped.attr.attr,
  541. &port_pma_attr_ext_port_xmit_data.attr.attr,
  542. &port_pma_attr_ext_port_rcv_data.attr.attr,
  543. &port_pma_attr_ext_port_xmit_packets.attr.attr,
  544. &port_pma_attr_ext_port_rcv_packets.attr.attr,
  545. &port_pma_attr_port_xmit_wait.attr.attr,
  546. NULL
  547. };
  548. static struct attribute_group pma_group = {
  549. .name = "counters",
  550. .attrs = pma_attrs
  551. };
  552. static struct attribute_group pma_group_ext = {
  553. .name = "counters",
  554. .attrs = pma_attrs_ext
  555. };
  556. static struct attribute_group pma_group_noietf = {
  557. .name = "counters",
  558. .attrs = pma_attrs_noietf
  559. };
  560. static void ib_port_release(struct kobject *kobj)
  561. {
  562. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  563. struct attribute *a;
  564. int i;
  565. if (p->gid_group.attrs) {
  566. for (i = 0; (a = p->gid_group.attrs[i]); ++i)
  567. kfree(a);
  568. kfree(p->gid_group.attrs);
  569. }
  570. if (p->pkey_group.attrs) {
  571. for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
  572. kfree(a);
  573. kfree(p->pkey_group.attrs);
  574. }
  575. kfree(p);
  576. }
  577. static void ib_port_gid_attr_release(struct kobject *kobj)
  578. {
  579. struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
  580. kobj);
  581. struct attribute *a;
  582. int i;
  583. if (g->ndev.attrs) {
  584. for (i = 0; (a = g->ndev.attrs[i]); ++i)
  585. kfree(a);
  586. kfree(g->ndev.attrs);
  587. }
  588. if (g->type.attrs) {
  589. for (i = 0; (a = g->type.attrs[i]); ++i)
  590. kfree(a);
  591. kfree(g->type.attrs);
  592. }
  593. kfree(g);
  594. }
  595. static struct kobj_type port_type = {
  596. .release = ib_port_release,
  597. .sysfs_ops = &port_sysfs_ops,
  598. .default_attrs = port_default_attrs
  599. };
  600. static struct kobj_type gid_attr_type = {
  601. .sysfs_ops = &gid_attr_sysfs_ops,
  602. .release = ib_port_gid_attr_release
  603. };
  604. static struct attribute **
  605. alloc_group_attrs(ssize_t (*show)(struct ib_port *,
  606. struct port_attribute *, char *buf),
  607. int len)
  608. {
  609. struct attribute **tab_attr;
  610. struct port_table_attribute *element;
  611. int i;
  612. tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
  613. if (!tab_attr)
  614. return NULL;
  615. for (i = 0; i < len; i++) {
  616. element = kzalloc(sizeof(struct port_table_attribute),
  617. GFP_KERNEL);
  618. if (!element)
  619. goto err;
  620. if (snprintf(element->name, sizeof(element->name),
  621. "%d", i) >= sizeof(element->name)) {
  622. kfree(element);
  623. goto err;
  624. }
  625. element->attr.attr.name = element->name;
  626. element->attr.attr.mode = S_IRUGO;
  627. element->attr.show = show;
  628. element->index = i;
  629. sysfs_attr_init(&element->attr.attr);
  630. tab_attr[i] = &element->attr.attr;
  631. }
  632. return tab_attr;
  633. err:
  634. while (--i >= 0)
  635. kfree(tab_attr[i]);
  636. kfree(tab_attr);
  637. return NULL;
  638. }
  639. /*
  640. * Figure out which counter table to use depending on
  641. * the device capabilities.
  642. */
  643. static struct attribute_group *get_counter_table(struct ib_device *dev,
  644. int port_num)
  645. {
  646. struct ib_class_port_info cpi;
  647. if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
  648. &cpi, 40, sizeof(cpi)) >= 0) {
  649. if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH)
  650. /* We have extended counters */
  651. return &pma_group_ext;
  652. if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
  653. /* But not the IETF ones */
  654. return &pma_group_noietf;
  655. }
  656. /* Fall back to normal counters */
  657. return &pma_group;
  658. }
  659. static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
  660. u8 port_num, int index)
  661. {
  662. int ret;
  663. if (time_is_after_eq_jiffies(stats->timestamp + stats->lifespan))
  664. return 0;
  665. ret = dev->get_hw_stats(dev, stats, port_num, index);
  666. if (ret < 0)
  667. return ret;
  668. if (ret == stats->num_counters)
  669. stats->timestamp = jiffies;
  670. return 0;
  671. }
  672. static ssize_t print_hw_stat(struct rdma_hw_stats *stats, int index, char *buf)
  673. {
  674. return sprintf(buf, "%llu\n", stats->value[index]);
  675. }
  676. static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
  677. char *buf)
  678. {
  679. struct ib_device *dev;
  680. struct ib_port *port;
  681. struct hw_stats_attribute *hsa;
  682. struct rdma_hw_stats *stats;
  683. int ret;
  684. hsa = container_of(attr, struct hw_stats_attribute, attr);
  685. if (!hsa->port_num) {
  686. dev = container_of((struct device *)kobj,
  687. struct ib_device, dev);
  688. stats = dev->hw_stats;
  689. } else {
  690. port = container_of(kobj, struct ib_port, kobj);
  691. dev = port->ibdev;
  692. stats = port->hw_stats;
  693. }
  694. ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index);
  695. if (ret)
  696. return ret;
  697. return print_hw_stat(stats, hsa->index, buf);
  698. }
  699. static ssize_t show_stats_lifespan(struct kobject *kobj,
  700. struct attribute *attr,
  701. char *buf)
  702. {
  703. struct hw_stats_attribute *hsa;
  704. int msecs;
  705. hsa = container_of(attr, struct hw_stats_attribute, attr);
  706. if (!hsa->port_num) {
  707. struct ib_device *dev = container_of((struct device *)kobj,
  708. struct ib_device, dev);
  709. msecs = jiffies_to_msecs(dev->hw_stats->lifespan);
  710. } else {
  711. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  712. msecs = jiffies_to_msecs(p->hw_stats->lifespan);
  713. }
  714. return sprintf(buf, "%d\n", msecs);
  715. }
  716. static ssize_t set_stats_lifespan(struct kobject *kobj,
  717. struct attribute *attr,
  718. const char *buf, size_t count)
  719. {
  720. struct hw_stats_attribute *hsa;
  721. int msecs;
  722. int jiffies;
  723. int ret;
  724. ret = kstrtoint(buf, 10, &msecs);
  725. if (ret)
  726. return ret;
  727. if (msecs < 0 || msecs > 10000)
  728. return -EINVAL;
  729. jiffies = msecs_to_jiffies(msecs);
  730. hsa = container_of(attr, struct hw_stats_attribute, attr);
  731. if (!hsa->port_num) {
  732. struct ib_device *dev = container_of((struct device *)kobj,
  733. struct ib_device, dev);
  734. dev->hw_stats->lifespan = jiffies;
  735. } else {
  736. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  737. p->hw_stats->lifespan = jiffies;
  738. }
  739. return count;
  740. }
  741. static void free_hsag(struct kobject *kobj, struct attribute_group *attr_group)
  742. {
  743. struct attribute **attr;
  744. sysfs_remove_group(kobj, attr_group);
  745. for (attr = attr_group->attrs; *attr; attr++)
  746. kfree(*attr);
  747. kfree(attr_group);
  748. }
  749. static struct attribute *alloc_hsa(int index, u8 port_num, const char *name)
  750. {
  751. struct hw_stats_attribute *hsa;
  752. hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
  753. if (!hsa)
  754. return NULL;
  755. hsa->attr.name = (char *)name;
  756. hsa->attr.mode = S_IRUGO;
  757. hsa->show = show_hw_stats;
  758. hsa->store = NULL;
  759. hsa->index = index;
  760. hsa->port_num = port_num;
  761. return &hsa->attr;
  762. }
  763. static struct attribute *alloc_hsa_lifespan(char *name, u8 port_num)
  764. {
  765. struct hw_stats_attribute *hsa;
  766. hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
  767. if (!hsa)
  768. return NULL;
  769. hsa->attr.name = name;
  770. hsa->attr.mode = S_IWUSR | S_IRUGO;
  771. hsa->show = show_stats_lifespan;
  772. hsa->store = set_stats_lifespan;
  773. hsa->index = 0;
  774. hsa->port_num = port_num;
  775. return &hsa->attr;
  776. }
  777. static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
  778. u8 port_num)
  779. {
  780. struct attribute_group *hsag;
  781. struct rdma_hw_stats *stats;
  782. int i, ret;
  783. stats = device->alloc_hw_stats(device, port_num);
  784. if (!stats)
  785. return;
  786. if (!stats->names || stats->num_counters <= 0)
  787. goto err_free_stats;
  788. /*
  789. * Two extra attribue elements here, one for the lifespan entry and
  790. * one to NULL terminate the list for the sysfs core code
  791. */
  792. hsag = kzalloc(sizeof(*hsag) +
  793. sizeof(void *) * (stats->num_counters + 2),
  794. GFP_KERNEL);
  795. if (!hsag)
  796. goto err_free_stats;
  797. ret = device->get_hw_stats(device, stats, port_num,
  798. stats->num_counters);
  799. if (ret != stats->num_counters)
  800. goto err_free_hsag;
  801. stats->timestamp = jiffies;
  802. hsag->name = "hw_counters";
  803. hsag->attrs = (void *)hsag + sizeof(*hsag);
  804. for (i = 0; i < stats->num_counters; i++) {
  805. hsag->attrs[i] = alloc_hsa(i, port_num, stats->names[i]);
  806. if (!hsag->attrs[i])
  807. goto err;
  808. sysfs_attr_init(hsag->attrs[i]);
  809. }
  810. /* treat an error here as non-fatal */
  811. hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num);
  812. if (hsag->attrs[i])
  813. sysfs_attr_init(hsag->attrs[i]);
  814. if (port) {
  815. struct kobject *kobj = &port->kobj;
  816. ret = sysfs_create_group(kobj, hsag);
  817. if (ret)
  818. goto err;
  819. port->hw_stats_ag = hsag;
  820. port->hw_stats = stats;
  821. } else {
  822. struct kobject *kobj = &device->dev.kobj;
  823. ret = sysfs_create_group(kobj, hsag);
  824. if (ret)
  825. goto err;
  826. device->hw_stats_ag = hsag;
  827. device->hw_stats = stats;
  828. }
  829. return;
  830. err:
  831. for (; i >= 0; i--)
  832. kfree(hsag->attrs[i]);
  833. err_free_hsag:
  834. kfree(hsag);
  835. err_free_stats:
  836. kfree(stats);
  837. return;
  838. }
  839. static int add_port(struct ib_device *device, int port_num,
  840. int (*port_callback)(struct ib_device *,
  841. u8, struct kobject *))
  842. {
  843. struct ib_port *p;
  844. struct ib_port_attr attr;
  845. int i;
  846. int ret;
  847. ret = ib_query_port(device, port_num, &attr);
  848. if (ret)
  849. return ret;
  850. p = kzalloc(sizeof *p, GFP_KERNEL);
  851. if (!p)
  852. return -ENOMEM;
  853. p->ibdev = device;
  854. p->port_num = port_num;
  855. ret = kobject_init_and_add(&p->kobj, &port_type,
  856. device->ports_parent,
  857. "%d", port_num);
  858. if (ret) {
  859. kfree(p);
  860. return ret;
  861. }
  862. p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
  863. if (!p->gid_attr_group) {
  864. ret = -ENOMEM;
  865. goto err_put;
  866. }
  867. p->gid_attr_group->port = p;
  868. ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
  869. &p->kobj, "gid_attrs");
  870. if (ret) {
  871. kfree(p->gid_attr_group);
  872. goto err_put;
  873. }
  874. p->pma_table = get_counter_table(device, port_num);
  875. ret = sysfs_create_group(&p->kobj, p->pma_table);
  876. if (ret)
  877. goto err_put_gid_attrs;
  878. p->gid_group.name = "gids";
  879. p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
  880. if (!p->gid_group.attrs) {
  881. ret = -ENOMEM;
  882. goto err_remove_pma;
  883. }
  884. ret = sysfs_create_group(&p->kobj, &p->gid_group);
  885. if (ret)
  886. goto err_free_gid;
  887. p->gid_attr_group->ndev.name = "ndevs";
  888. p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
  889. attr.gid_tbl_len);
  890. if (!p->gid_attr_group->ndev.attrs) {
  891. ret = -ENOMEM;
  892. goto err_remove_gid;
  893. }
  894. ret = sysfs_create_group(&p->gid_attr_group->kobj,
  895. &p->gid_attr_group->ndev);
  896. if (ret)
  897. goto err_free_gid_ndev;
  898. p->gid_attr_group->type.name = "types";
  899. p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
  900. attr.gid_tbl_len);
  901. if (!p->gid_attr_group->type.attrs) {
  902. ret = -ENOMEM;
  903. goto err_remove_gid_ndev;
  904. }
  905. ret = sysfs_create_group(&p->gid_attr_group->kobj,
  906. &p->gid_attr_group->type);
  907. if (ret)
  908. goto err_free_gid_type;
  909. p->pkey_group.name = "pkeys";
  910. p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
  911. attr.pkey_tbl_len);
  912. if (!p->pkey_group.attrs) {
  913. ret = -ENOMEM;
  914. goto err_remove_gid_type;
  915. }
  916. ret = sysfs_create_group(&p->kobj, &p->pkey_group);
  917. if (ret)
  918. goto err_free_pkey;
  919. if (port_callback) {
  920. ret = port_callback(device, port_num, &p->kobj);
  921. if (ret)
  922. goto err_remove_pkey;
  923. }
  924. /*
  925. * If port == 0, it means we have only one port and the parent
  926. * device, not this port device, should be the holder of the
  927. * hw_counters
  928. */
  929. if (device->alloc_hw_stats && port_num)
  930. setup_hw_stats(device, p, port_num);
  931. list_add_tail(&p->kobj.entry, &device->port_list);
  932. kobject_uevent(&p->kobj, KOBJ_ADD);
  933. return 0;
  934. err_remove_pkey:
  935. sysfs_remove_group(&p->kobj, &p->pkey_group);
  936. err_free_pkey:
  937. for (i = 0; i < attr.pkey_tbl_len; ++i)
  938. kfree(p->pkey_group.attrs[i]);
  939. kfree(p->pkey_group.attrs);
  940. p->pkey_group.attrs = NULL;
  941. err_remove_gid_type:
  942. sysfs_remove_group(&p->gid_attr_group->kobj,
  943. &p->gid_attr_group->type);
  944. err_free_gid_type:
  945. for (i = 0; i < attr.gid_tbl_len; ++i)
  946. kfree(p->gid_attr_group->type.attrs[i]);
  947. kfree(p->gid_attr_group->type.attrs);
  948. p->gid_attr_group->type.attrs = NULL;
  949. err_remove_gid_ndev:
  950. sysfs_remove_group(&p->gid_attr_group->kobj,
  951. &p->gid_attr_group->ndev);
  952. err_free_gid_ndev:
  953. for (i = 0; i < attr.gid_tbl_len; ++i)
  954. kfree(p->gid_attr_group->ndev.attrs[i]);
  955. kfree(p->gid_attr_group->ndev.attrs);
  956. p->gid_attr_group->ndev.attrs = NULL;
  957. err_remove_gid:
  958. sysfs_remove_group(&p->kobj, &p->gid_group);
  959. err_free_gid:
  960. for (i = 0; i < attr.gid_tbl_len; ++i)
  961. kfree(p->gid_group.attrs[i]);
  962. kfree(p->gid_group.attrs);
  963. p->gid_group.attrs = NULL;
  964. err_remove_pma:
  965. sysfs_remove_group(&p->kobj, p->pma_table);
  966. err_put_gid_attrs:
  967. kobject_put(&p->gid_attr_group->kobj);
  968. err_put:
  969. kobject_put(&p->kobj);
  970. return ret;
  971. }
  972. static ssize_t show_node_type(struct device *device,
  973. struct device_attribute *attr, char *buf)
  974. {
  975. struct ib_device *dev = container_of(device, struct ib_device, dev);
  976. switch (dev->node_type) {
  977. case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
  978. case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
  979. case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
  980. case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
  981. case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
  982. case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
  983. default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
  984. }
  985. }
  986. static ssize_t show_sys_image_guid(struct device *device,
  987. struct device_attribute *dev_attr, char *buf)
  988. {
  989. struct ib_device *dev = container_of(device, struct ib_device, dev);
  990. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  991. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
  992. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
  993. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
  994. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
  995. }
  996. static ssize_t show_node_guid(struct device *device,
  997. struct device_attribute *attr, char *buf)
  998. {
  999. struct ib_device *dev = container_of(device, struct ib_device, dev);
  1000. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  1001. be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
  1002. be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
  1003. be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
  1004. be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
  1005. }
  1006. static ssize_t show_node_desc(struct device *device,
  1007. struct device_attribute *attr, char *buf)
  1008. {
  1009. struct ib_device *dev = container_of(device, struct ib_device, dev);
  1010. return sprintf(buf, "%.64s\n", dev->node_desc);
  1011. }
  1012. static ssize_t set_node_desc(struct device *device,
  1013. struct device_attribute *attr,
  1014. const char *buf, size_t count)
  1015. {
  1016. struct ib_device *dev = container_of(device, struct ib_device, dev);
  1017. struct ib_device_modify desc = {};
  1018. int ret;
  1019. if (!dev->modify_device)
  1020. return -EIO;
  1021. memcpy(desc.node_desc, buf, min_t(int, count, IB_DEVICE_NODE_DESC_MAX));
  1022. ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
  1023. if (ret)
  1024. return ret;
  1025. return count;
  1026. }
  1027. static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
  1028. char *buf)
  1029. {
  1030. struct ib_device *dev = container_of(device, struct ib_device, dev);
  1031. ib_get_device_fw_str(dev, buf);
  1032. strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
  1033. return strlen(buf);
  1034. }
  1035. static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
  1036. static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
  1037. static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
  1038. static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
  1039. static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
  1040. static struct device_attribute *ib_class_attributes[] = {
  1041. &dev_attr_node_type,
  1042. &dev_attr_sys_image_guid,
  1043. &dev_attr_node_guid,
  1044. &dev_attr_node_desc,
  1045. &dev_attr_fw_ver,
  1046. };
  1047. static void free_port_list_attributes(struct ib_device *device)
  1048. {
  1049. struct kobject *p, *t;
  1050. list_for_each_entry_safe(p, t, &device->port_list, entry) {
  1051. struct ib_port *port = container_of(p, struct ib_port, kobj);
  1052. list_del(&p->entry);
  1053. if (port->hw_stats) {
  1054. kfree(port->hw_stats);
  1055. free_hsag(&port->kobj, port->hw_stats_ag);
  1056. }
  1057. sysfs_remove_group(p, port->pma_table);
  1058. sysfs_remove_group(p, &port->pkey_group);
  1059. sysfs_remove_group(p, &port->gid_group);
  1060. sysfs_remove_group(&port->gid_attr_group->kobj,
  1061. &port->gid_attr_group->ndev);
  1062. sysfs_remove_group(&port->gid_attr_group->kobj,
  1063. &port->gid_attr_group->type);
  1064. kobject_put(&port->gid_attr_group->kobj);
  1065. kobject_put(p);
  1066. }
  1067. kobject_put(device->ports_parent);
  1068. }
  1069. int ib_device_register_sysfs(struct ib_device *device,
  1070. int (*port_callback)(struct ib_device *,
  1071. u8, struct kobject *))
  1072. {
  1073. struct device *class_dev = &device->dev;
  1074. int ret;
  1075. int i;
  1076. ret = dev_set_name(class_dev, "%s", device->name);
  1077. if (ret)
  1078. return ret;
  1079. ret = device_add(class_dev);
  1080. if (ret)
  1081. goto err;
  1082. for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
  1083. ret = device_create_file(class_dev, ib_class_attributes[i]);
  1084. if (ret)
  1085. goto err_unregister;
  1086. }
  1087. device->ports_parent = kobject_create_and_add("ports",
  1088. &class_dev->kobj);
  1089. if (!device->ports_parent) {
  1090. ret = -ENOMEM;
  1091. goto err_put;
  1092. }
  1093. if (rdma_cap_ib_switch(device)) {
  1094. ret = add_port(device, 0, port_callback);
  1095. if (ret)
  1096. goto err_put;
  1097. } else {
  1098. for (i = 1; i <= device->phys_port_cnt; ++i) {
  1099. ret = add_port(device, i, port_callback);
  1100. if (ret)
  1101. goto err_put;
  1102. }
  1103. }
  1104. if (device->alloc_hw_stats)
  1105. setup_hw_stats(device, NULL, 0);
  1106. return 0;
  1107. err_put:
  1108. free_port_list_attributes(device);
  1109. err_unregister:
  1110. device_del(class_dev);
  1111. err:
  1112. return ret;
  1113. }
  1114. void ib_device_unregister_sysfs(struct ib_device *device)
  1115. {
  1116. int i;
  1117. /* Hold kobject until ib_dealloc_device() */
  1118. kobject_get(&device->dev.kobj);
  1119. free_port_list_attributes(device);
  1120. if (device->hw_stats) {
  1121. kfree(device->hw_stats);
  1122. free_hsag(&device->dev.kobj, device->hw_stats_ag);
  1123. }
  1124. for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
  1125. device_remove_file(&device->dev, ib_class_attributes[i]);
  1126. device_unregister(&device->dev);
  1127. }