sysfs.c 33 KB

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