sysfs.c 34 KB

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