sysfs.c 35 KB

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