sysfs.c 33 KB

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