sysfs.c 34 KB

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