sysfs.c 35 KB

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