sysfs.c 34 KB

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