sysfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. struct ib_port;
  41. struct gid_attr_group {
  42. struct ib_port *port;
  43. struct kobject kobj;
  44. struct attribute_group ndev;
  45. struct attribute_group type;
  46. };
  47. struct ib_port {
  48. struct kobject kobj;
  49. struct ib_device *ibdev;
  50. struct gid_attr_group *gid_attr_group;
  51. struct attribute_group gid_group;
  52. struct attribute_group pkey_group;
  53. u8 port_num;
  54. };
  55. struct port_attribute {
  56. struct attribute attr;
  57. ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
  58. ssize_t (*store)(struct ib_port *, struct port_attribute *,
  59. const char *buf, size_t count);
  60. };
  61. #define PORT_ATTR(_name, _mode, _show, _store) \
  62. struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
  63. #define PORT_ATTR_RO(_name) \
  64. struct port_attribute port_attr_##_name = __ATTR_RO(_name)
  65. struct port_table_attribute {
  66. struct port_attribute attr;
  67. char name[8];
  68. int index;
  69. };
  70. static ssize_t port_attr_show(struct kobject *kobj,
  71. struct attribute *attr, char *buf)
  72. {
  73. struct port_attribute *port_attr =
  74. container_of(attr, struct port_attribute, attr);
  75. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  76. if (!port_attr->show)
  77. return -EIO;
  78. return port_attr->show(p, port_attr, buf);
  79. }
  80. static const struct sysfs_ops port_sysfs_ops = {
  81. .show = port_attr_show
  82. };
  83. static ssize_t gid_attr_show(struct kobject *kobj,
  84. struct attribute *attr, char *buf)
  85. {
  86. struct port_attribute *port_attr =
  87. container_of(attr, struct port_attribute, attr);
  88. struct ib_port *p = container_of(kobj, struct gid_attr_group,
  89. kobj)->port;
  90. if (!port_attr->show)
  91. return -EIO;
  92. return port_attr->show(p, port_attr, buf);
  93. }
  94. static const struct sysfs_ops gid_attr_sysfs_ops = {
  95. .show = gid_attr_show
  96. };
  97. static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
  98. char *buf)
  99. {
  100. struct ib_port_attr attr;
  101. ssize_t ret;
  102. static const char *state_name[] = {
  103. [IB_PORT_NOP] = "NOP",
  104. [IB_PORT_DOWN] = "DOWN",
  105. [IB_PORT_INIT] = "INIT",
  106. [IB_PORT_ARMED] = "ARMED",
  107. [IB_PORT_ACTIVE] = "ACTIVE",
  108. [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
  109. };
  110. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  111. if (ret)
  112. return ret;
  113. return sprintf(buf, "%d: %s\n", attr.state,
  114. attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
  115. state_name[attr.state] : "UNKNOWN");
  116. }
  117. static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
  118. char *buf)
  119. {
  120. struct ib_port_attr attr;
  121. ssize_t ret;
  122. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  123. if (ret)
  124. return ret;
  125. return sprintf(buf, "0x%x\n", attr.lid);
  126. }
  127. static ssize_t lid_mask_count_show(struct ib_port *p,
  128. struct port_attribute *unused,
  129. char *buf)
  130. {
  131. struct ib_port_attr attr;
  132. ssize_t ret;
  133. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  134. if (ret)
  135. return ret;
  136. return sprintf(buf, "%d\n", attr.lmc);
  137. }
  138. static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
  139. char *buf)
  140. {
  141. struct ib_port_attr attr;
  142. ssize_t ret;
  143. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  144. if (ret)
  145. return ret;
  146. return sprintf(buf, "0x%x\n", attr.sm_lid);
  147. }
  148. static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
  149. char *buf)
  150. {
  151. struct ib_port_attr attr;
  152. ssize_t ret;
  153. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  154. if (ret)
  155. return ret;
  156. return sprintf(buf, "%d\n", attr.sm_sl);
  157. }
  158. static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
  159. char *buf)
  160. {
  161. struct ib_port_attr attr;
  162. ssize_t ret;
  163. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  164. if (ret)
  165. return ret;
  166. return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
  167. }
  168. static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
  169. char *buf)
  170. {
  171. struct ib_port_attr attr;
  172. char *speed = "";
  173. int rate; /* in deci-Gb/sec */
  174. ssize_t ret;
  175. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  176. if (ret)
  177. return ret;
  178. switch (attr.active_speed) {
  179. case IB_SPEED_DDR:
  180. speed = " DDR";
  181. rate = 50;
  182. break;
  183. case IB_SPEED_QDR:
  184. speed = " QDR";
  185. rate = 100;
  186. break;
  187. case IB_SPEED_FDR10:
  188. speed = " FDR10";
  189. rate = 100;
  190. break;
  191. case IB_SPEED_FDR:
  192. speed = " FDR";
  193. rate = 140;
  194. break;
  195. case IB_SPEED_EDR:
  196. speed = " EDR";
  197. rate = 250;
  198. break;
  199. case IB_SPEED_SDR:
  200. default: /* default to SDR for invalid rates */
  201. rate = 25;
  202. break;
  203. }
  204. rate *= ib_width_enum_to_int(attr.active_width);
  205. if (rate < 0)
  206. return -EINVAL;
  207. return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
  208. rate / 10, rate % 10 ? ".5" : "",
  209. ib_width_enum_to_int(attr.active_width), speed);
  210. }
  211. static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
  212. char *buf)
  213. {
  214. struct ib_port_attr attr;
  215. ssize_t ret;
  216. ret = ib_query_port(p->ibdev, p->port_num, &attr);
  217. if (ret)
  218. return ret;
  219. switch (attr.phys_state) {
  220. case 1: return sprintf(buf, "1: Sleep\n");
  221. case 2: return sprintf(buf, "2: Polling\n");
  222. case 3: return sprintf(buf, "3: Disabled\n");
  223. case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
  224. case 5: return sprintf(buf, "5: LinkUp\n");
  225. case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
  226. case 7: return sprintf(buf, "7: Phy Test\n");
  227. default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
  228. }
  229. }
  230. static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
  231. char *buf)
  232. {
  233. switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
  234. case IB_LINK_LAYER_INFINIBAND:
  235. return sprintf(buf, "%s\n", "InfiniBand");
  236. case IB_LINK_LAYER_ETHERNET:
  237. return sprintf(buf, "%s\n", "Ethernet");
  238. default:
  239. return sprintf(buf, "%s\n", "Unknown");
  240. }
  241. }
  242. static PORT_ATTR_RO(state);
  243. static PORT_ATTR_RO(lid);
  244. static PORT_ATTR_RO(lid_mask_count);
  245. static PORT_ATTR_RO(sm_lid);
  246. static PORT_ATTR_RO(sm_sl);
  247. static PORT_ATTR_RO(cap_mask);
  248. static PORT_ATTR_RO(rate);
  249. static PORT_ATTR_RO(phys_state);
  250. static PORT_ATTR_RO(link_layer);
  251. static struct attribute *port_default_attrs[] = {
  252. &port_attr_state.attr,
  253. &port_attr_lid.attr,
  254. &port_attr_lid_mask_count.attr,
  255. &port_attr_sm_lid.attr,
  256. &port_attr_sm_sl.attr,
  257. &port_attr_cap_mask.attr,
  258. &port_attr_rate.attr,
  259. &port_attr_phys_state.attr,
  260. &port_attr_link_layer.attr,
  261. NULL
  262. };
  263. static size_t print_ndev(struct ib_gid_attr *gid_attr, char *buf)
  264. {
  265. if (!gid_attr->ndev)
  266. return -EINVAL;
  267. return sprintf(buf, "%s\n", gid_attr->ndev->name);
  268. }
  269. static size_t print_gid_type(struct ib_gid_attr *gid_attr, char *buf)
  270. {
  271. return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
  272. }
  273. static ssize_t _show_port_gid_attr(struct ib_port *p,
  274. struct port_attribute *attr,
  275. char *buf,
  276. size_t (*print)(struct ib_gid_attr *gid_attr,
  277. char *buf))
  278. {
  279. struct port_table_attribute *tab_attr =
  280. container_of(attr, struct port_table_attribute, attr);
  281. union ib_gid gid;
  282. struct ib_gid_attr gid_attr = {};
  283. ssize_t ret;
  284. va_list args;
  285. ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid,
  286. &gid_attr);
  287. if (ret)
  288. goto err;
  289. ret = print(&gid_attr, buf);
  290. err:
  291. if (gid_attr.ndev)
  292. dev_put(gid_attr.ndev);
  293. va_end(args);
  294. return ret;
  295. }
  296. static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
  297. char *buf)
  298. {
  299. struct port_table_attribute *tab_attr =
  300. container_of(attr, struct port_table_attribute, attr);
  301. union ib_gid gid;
  302. ssize_t ret;
  303. ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid, NULL);
  304. if (ret)
  305. return ret;
  306. return sprintf(buf, "%pI6\n", gid.raw);
  307. }
  308. static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
  309. struct port_attribute *attr, char *buf)
  310. {
  311. return _show_port_gid_attr(p, attr, buf, print_ndev);
  312. }
  313. static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
  314. struct port_attribute *attr,
  315. char *buf)
  316. {
  317. return _show_port_gid_attr(p, attr, buf, print_gid_type);
  318. }
  319. static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
  320. char *buf)
  321. {
  322. struct port_table_attribute *tab_attr =
  323. container_of(attr, struct port_table_attribute, attr);
  324. u16 pkey;
  325. ssize_t ret;
  326. ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
  327. if (ret)
  328. return ret;
  329. return sprintf(buf, "0x%04x\n", pkey);
  330. }
  331. #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
  332. struct port_table_attribute port_pma_attr_##_name = { \
  333. .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
  334. .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
  335. }
  336. /*
  337. * Get a Perfmgmt MAD block of data.
  338. * Returns error code or the number of bytes retrieved.
  339. */
  340. static int get_perf_mad(struct ib_device *dev, int port_num, int attr,
  341. void *data, int offset, size_t size)
  342. {
  343. struct ib_mad *in_mad;
  344. struct ib_mad *out_mad;
  345. size_t mad_size = sizeof(*out_mad);
  346. u16 out_mad_pkey_index = 0;
  347. ssize_t ret;
  348. if (!dev->process_mad)
  349. return -ENOSYS;
  350. in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
  351. out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
  352. if (!in_mad || !out_mad) {
  353. ret = -ENOMEM;
  354. goto out;
  355. }
  356. in_mad->mad_hdr.base_version = 1;
  357. in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
  358. in_mad->mad_hdr.class_version = 1;
  359. in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  360. in_mad->mad_hdr.attr_id = attr;
  361. in_mad->data[41] = port_num; /* PortSelect field */
  362. if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY,
  363. port_num, NULL, NULL,
  364. (const struct ib_mad_hdr *)in_mad, mad_size,
  365. (struct ib_mad_hdr *)out_mad, &mad_size,
  366. &out_mad_pkey_index) &
  367. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
  368. (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
  369. ret = -EINVAL;
  370. goto out;
  371. }
  372. memcpy(data, out_mad->data + offset, size);
  373. ret = size;
  374. out:
  375. kfree(in_mad);
  376. kfree(out_mad);
  377. return ret;
  378. }
  379. static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
  380. char *buf)
  381. {
  382. struct port_table_attribute *tab_attr =
  383. container_of(attr, struct port_table_attribute, attr);
  384. int offset = tab_attr->index & 0xffff;
  385. int width = (tab_attr->index >> 16) & 0xff;
  386. ssize_t ret;
  387. u8 data[8];
  388. ret = get_perf_mad(p->ibdev, p->port_num, cpu_to_be16(0x12), &data,
  389. 40 + offset / 8, sizeof(data));
  390. if (ret < 0)
  391. return sprintf(buf, "N/A (no PMA)\n");
  392. switch (width) {
  393. case 4:
  394. ret = sprintf(buf, "%u\n", (*data >>
  395. (4 - (offset % 8))) & 0xf);
  396. break;
  397. case 8:
  398. ret = sprintf(buf, "%u\n", *data);
  399. break;
  400. case 16:
  401. ret = sprintf(buf, "%u\n",
  402. be16_to_cpup((__be16 *)data));
  403. break;
  404. case 32:
  405. ret = sprintf(buf, "%u\n",
  406. be32_to_cpup((__be32 *)data));
  407. break;
  408. default:
  409. ret = 0;
  410. }
  411. return ret;
  412. }
  413. static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
  414. static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
  415. static PORT_PMA_ATTR(link_downed , 2, 8, 56);
  416. static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
  417. static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
  418. static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
  419. static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
  420. static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
  421. static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
  422. static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
  423. static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
  424. static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
  425. static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
  426. static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
  427. static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
  428. static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
  429. static struct attribute *pma_attrs[] = {
  430. &port_pma_attr_symbol_error.attr.attr,
  431. &port_pma_attr_link_error_recovery.attr.attr,
  432. &port_pma_attr_link_downed.attr.attr,
  433. &port_pma_attr_port_rcv_errors.attr.attr,
  434. &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
  435. &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
  436. &port_pma_attr_port_xmit_discards.attr.attr,
  437. &port_pma_attr_port_xmit_constraint_errors.attr.attr,
  438. &port_pma_attr_port_rcv_constraint_errors.attr.attr,
  439. &port_pma_attr_local_link_integrity_errors.attr.attr,
  440. &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
  441. &port_pma_attr_VL15_dropped.attr.attr,
  442. &port_pma_attr_port_xmit_data.attr.attr,
  443. &port_pma_attr_port_rcv_data.attr.attr,
  444. &port_pma_attr_port_xmit_packets.attr.attr,
  445. &port_pma_attr_port_rcv_packets.attr.attr,
  446. NULL
  447. };
  448. static struct attribute_group pma_group = {
  449. .name = "counters",
  450. .attrs = pma_attrs
  451. };
  452. static void ib_port_release(struct kobject *kobj)
  453. {
  454. struct ib_port *p = container_of(kobj, struct ib_port, kobj);
  455. struct attribute *a;
  456. int i;
  457. if (p->gid_group.attrs) {
  458. for (i = 0; (a = p->gid_group.attrs[i]); ++i)
  459. kfree(a);
  460. kfree(p->gid_group.attrs);
  461. }
  462. if (p->pkey_group.attrs) {
  463. for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
  464. kfree(a);
  465. kfree(p->pkey_group.attrs);
  466. }
  467. kfree(p);
  468. }
  469. static void ib_port_gid_attr_release(struct kobject *kobj)
  470. {
  471. struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
  472. kobj);
  473. struct attribute *a;
  474. int i;
  475. if (g->ndev.attrs) {
  476. for (i = 0; (a = g->ndev.attrs[i]); ++i)
  477. kfree(a);
  478. kfree(g->ndev.attrs);
  479. }
  480. if (g->type.attrs) {
  481. for (i = 0; (a = g->type.attrs[i]); ++i)
  482. kfree(a);
  483. kfree(g->type.attrs);
  484. }
  485. kfree(g);
  486. }
  487. static struct kobj_type port_type = {
  488. .release = ib_port_release,
  489. .sysfs_ops = &port_sysfs_ops,
  490. .default_attrs = port_default_attrs
  491. };
  492. static struct kobj_type gid_attr_type = {
  493. .sysfs_ops = &gid_attr_sysfs_ops,
  494. .release = ib_port_gid_attr_release
  495. };
  496. static struct attribute **
  497. alloc_group_attrs(ssize_t (*show)(struct ib_port *,
  498. struct port_attribute *, char *buf),
  499. int len)
  500. {
  501. struct attribute **tab_attr;
  502. struct port_table_attribute *element;
  503. int i;
  504. tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
  505. if (!tab_attr)
  506. return NULL;
  507. for (i = 0; i < len; i++) {
  508. element = kzalloc(sizeof(struct port_table_attribute),
  509. GFP_KERNEL);
  510. if (!element)
  511. goto err;
  512. if (snprintf(element->name, sizeof(element->name),
  513. "%d", i) >= sizeof(element->name)) {
  514. kfree(element);
  515. goto err;
  516. }
  517. element->attr.attr.name = element->name;
  518. element->attr.attr.mode = S_IRUGO;
  519. element->attr.show = show;
  520. element->index = i;
  521. sysfs_attr_init(&element->attr.attr);
  522. tab_attr[i] = &element->attr.attr;
  523. }
  524. return tab_attr;
  525. err:
  526. while (--i >= 0)
  527. kfree(tab_attr[i]);
  528. kfree(tab_attr);
  529. return NULL;
  530. }
  531. static int add_port(struct ib_device *device, int port_num,
  532. int (*port_callback)(struct ib_device *,
  533. u8, struct kobject *))
  534. {
  535. struct ib_port *p;
  536. struct ib_port_attr attr;
  537. int i;
  538. int ret;
  539. ret = ib_query_port(device, port_num, &attr);
  540. if (ret)
  541. return ret;
  542. p = kzalloc(sizeof *p, GFP_KERNEL);
  543. if (!p)
  544. return -ENOMEM;
  545. p->ibdev = device;
  546. p->port_num = port_num;
  547. ret = kobject_init_and_add(&p->kobj, &port_type,
  548. device->ports_parent,
  549. "%d", port_num);
  550. if (ret) {
  551. kfree(p);
  552. return ret;
  553. }
  554. p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
  555. if (!p->gid_attr_group) {
  556. ret = -ENOMEM;
  557. goto err_put;
  558. }
  559. p->gid_attr_group->port = p;
  560. ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
  561. &p->kobj, "gid_attrs");
  562. if (ret) {
  563. kfree(p->gid_attr_group);
  564. goto err_put;
  565. }
  566. ret = sysfs_create_group(&p->kobj, &pma_group);
  567. if (ret)
  568. goto err_put_gid_attrs;
  569. p->gid_group.name = "gids";
  570. p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
  571. if (!p->gid_group.attrs) {
  572. ret = -ENOMEM;
  573. goto err_remove_pma;
  574. }
  575. ret = sysfs_create_group(&p->kobj, &p->gid_group);
  576. if (ret)
  577. goto err_free_gid;
  578. p->gid_attr_group->ndev.name = "ndevs";
  579. p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
  580. attr.gid_tbl_len);
  581. if (!p->gid_attr_group->ndev.attrs) {
  582. ret = -ENOMEM;
  583. goto err_remove_gid;
  584. }
  585. ret = sysfs_create_group(&p->gid_attr_group->kobj,
  586. &p->gid_attr_group->ndev);
  587. if (ret)
  588. goto err_free_gid_ndev;
  589. p->gid_attr_group->type.name = "types";
  590. p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
  591. attr.gid_tbl_len);
  592. if (!p->gid_attr_group->type.attrs) {
  593. ret = -ENOMEM;
  594. goto err_remove_gid_ndev;
  595. }
  596. ret = sysfs_create_group(&p->gid_attr_group->kobj,
  597. &p->gid_attr_group->type);
  598. if (ret)
  599. goto err_free_gid_type;
  600. p->pkey_group.name = "pkeys";
  601. p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
  602. attr.pkey_tbl_len);
  603. if (!p->pkey_group.attrs) {
  604. ret = -ENOMEM;
  605. goto err_remove_gid_type;
  606. }
  607. ret = sysfs_create_group(&p->kobj, &p->pkey_group);
  608. if (ret)
  609. goto err_free_pkey;
  610. if (port_callback) {
  611. ret = port_callback(device, port_num, &p->kobj);
  612. if (ret)
  613. goto err_remove_pkey;
  614. }
  615. list_add_tail(&p->kobj.entry, &device->port_list);
  616. kobject_uevent(&p->kobj, KOBJ_ADD);
  617. return 0;
  618. err_remove_pkey:
  619. sysfs_remove_group(&p->kobj, &p->pkey_group);
  620. err_free_pkey:
  621. for (i = 0; i < attr.pkey_tbl_len; ++i)
  622. kfree(p->pkey_group.attrs[i]);
  623. kfree(p->pkey_group.attrs);
  624. p->pkey_group.attrs = NULL;
  625. err_remove_gid_type:
  626. sysfs_remove_group(&p->gid_attr_group->kobj,
  627. &p->gid_attr_group->type);
  628. err_free_gid_type:
  629. for (i = 0; i < attr.gid_tbl_len; ++i)
  630. kfree(p->gid_attr_group->type.attrs[i]);
  631. kfree(p->gid_attr_group->type.attrs);
  632. p->gid_attr_group->type.attrs = NULL;
  633. err_remove_gid_ndev:
  634. sysfs_remove_group(&p->gid_attr_group->kobj,
  635. &p->gid_attr_group->ndev);
  636. err_free_gid_ndev:
  637. for (i = 0; i < attr.gid_tbl_len; ++i)
  638. kfree(p->gid_attr_group->ndev.attrs[i]);
  639. kfree(p->gid_attr_group->ndev.attrs);
  640. p->gid_attr_group->ndev.attrs = NULL;
  641. err_remove_gid:
  642. sysfs_remove_group(&p->kobj, &p->gid_group);
  643. err_free_gid:
  644. for (i = 0; i < attr.gid_tbl_len; ++i)
  645. kfree(p->gid_group.attrs[i]);
  646. kfree(p->gid_group.attrs);
  647. p->gid_group.attrs = NULL;
  648. err_remove_pma:
  649. sysfs_remove_group(&p->kobj, &pma_group);
  650. err_put_gid_attrs:
  651. kobject_put(&p->gid_attr_group->kobj);
  652. err_put:
  653. kobject_put(&p->kobj);
  654. return ret;
  655. }
  656. static ssize_t show_node_type(struct device *device,
  657. struct device_attribute *attr, char *buf)
  658. {
  659. struct ib_device *dev = container_of(device, struct ib_device, dev);
  660. switch (dev->node_type) {
  661. case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
  662. case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
  663. case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
  664. case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
  665. case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
  666. case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
  667. default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
  668. }
  669. }
  670. static ssize_t show_sys_image_guid(struct device *device,
  671. struct device_attribute *dev_attr, char *buf)
  672. {
  673. struct ib_device *dev = container_of(device, struct ib_device, dev);
  674. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  675. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
  676. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
  677. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
  678. be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
  679. }
  680. static ssize_t show_node_guid(struct device *device,
  681. struct device_attribute *attr, char *buf)
  682. {
  683. struct ib_device *dev = container_of(device, struct ib_device, dev);
  684. return sprintf(buf, "%04x:%04x:%04x:%04x\n",
  685. be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
  686. be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
  687. be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
  688. be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
  689. }
  690. static ssize_t show_node_desc(struct device *device,
  691. struct device_attribute *attr, char *buf)
  692. {
  693. struct ib_device *dev = container_of(device, struct ib_device, dev);
  694. return sprintf(buf, "%.64s\n", dev->node_desc);
  695. }
  696. static ssize_t set_node_desc(struct device *device,
  697. struct device_attribute *attr,
  698. const char *buf, size_t count)
  699. {
  700. struct ib_device *dev = container_of(device, struct ib_device, dev);
  701. struct ib_device_modify desc = {};
  702. int ret;
  703. if (!dev->modify_device)
  704. return -EIO;
  705. memcpy(desc.node_desc, buf, min_t(int, count, 64));
  706. ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
  707. if (ret)
  708. return ret;
  709. return count;
  710. }
  711. static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
  712. static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
  713. static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
  714. static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
  715. static struct device_attribute *ib_class_attributes[] = {
  716. &dev_attr_node_type,
  717. &dev_attr_sys_image_guid,
  718. &dev_attr_node_guid,
  719. &dev_attr_node_desc
  720. };
  721. /* Show a given an attribute in the statistics group */
  722. static ssize_t show_protocol_stat(const struct device *device,
  723. struct device_attribute *attr, char *buf,
  724. unsigned offset)
  725. {
  726. struct ib_device *dev = container_of(device, struct ib_device, dev);
  727. union rdma_protocol_stats stats;
  728. ssize_t ret;
  729. ret = dev->get_protocol_stats(dev, &stats);
  730. if (ret)
  731. return ret;
  732. return sprintf(buf, "%llu\n",
  733. (unsigned long long) ((u64 *) &stats)[offset]);
  734. }
  735. /* generate a read-only iwarp statistics attribute */
  736. #define IW_STATS_ENTRY(name) \
  737. static ssize_t show_##name(struct device *device, \
  738. struct device_attribute *attr, char *buf) \
  739. { \
  740. return show_protocol_stat(device, attr, buf, \
  741. offsetof(struct iw_protocol_stats, name) / \
  742. sizeof (u64)); \
  743. } \
  744. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
  745. IW_STATS_ENTRY(ipInReceives);
  746. IW_STATS_ENTRY(ipInHdrErrors);
  747. IW_STATS_ENTRY(ipInTooBigErrors);
  748. IW_STATS_ENTRY(ipInNoRoutes);
  749. IW_STATS_ENTRY(ipInAddrErrors);
  750. IW_STATS_ENTRY(ipInUnknownProtos);
  751. IW_STATS_ENTRY(ipInTruncatedPkts);
  752. IW_STATS_ENTRY(ipInDiscards);
  753. IW_STATS_ENTRY(ipInDelivers);
  754. IW_STATS_ENTRY(ipOutForwDatagrams);
  755. IW_STATS_ENTRY(ipOutRequests);
  756. IW_STATS_ENTRY(ipOutDiscards);
  757. IW_STATS_ENTRY(ipOutNoRoutes);
  758. IW_STATS_ENTRY(ipReasmTimeout);
  759. IW_STATS_ENTRY(ipReasmReqds);
  760. IW_STATS_ENTRY(ipReasmOKs);
  761. IW_STATS_ENTRY(ipReasmFails);
  762. IW_STATS_ENTRY(ipFragOKs);
  763. IW_STATS_ENTRY(ipFragFails);
  764. IW_STATS_ENTRY(ipFragCreates);
  765. IW_STATS_ENTRY(ipInMcastPkts);
  766. IW_STATS_ENTRY(ipOutMcastPkts);
  767. IW_STATS_ENTRY(ipInBcastPkts);
  768. IW_STATS_ENTRY(ipOutBcastPkts);
  769. IW_STATS_ENTRY(tcpRtoAlgorithm);
  770. IW_STATS_ENTRY(tcpRtoMin);
  771. IW_STATS_ENTRY(tcpRtoMax);
  772. IW_STATS_ENTRY(tcpMaxConn);
  773. IW_STATS_ENTRY(tcpActiveOpens);
  774. IW_STATS_ENTRY(tcpPassiveOpens);
  775. IW_STATS_ENTRY(tcpAttemptFails);
  776. IW_STATS_ENTRY(tcpEstabResets);
  777. IW_STATS_ENTRY(tcpCurrEstab);
  778. IW_STATS_ENTRY(tcpInSegs);
  779. IW_STATS_ENTRY(tcpOutSegs);
  780. IW_STATS_ENTRY(tcpRetransSegs);
  781. IW_STATS_ENTRY(tcpInErrs);
  782. IW_STATS_ENTRY(tcpOutRsts);
  783. static struct attribute *iw_proto_stats_attrs[] = {
  784. &dev_attr_ipInReceives.attr,
  785. &dev_attr_ipInHdrErrors.attr,
  786. &dev_attr_ipInTooBigErrors.attr,
  787. &dev_attr_ipInNoRoutes.attr,
  788. &dev_attr_ipInAddrErrors.attr,
  789. &dev_attr_ipInUnknownProtos.attr,
  790. &dev_attr_ipInTruncatedPkts.attr,
  791. &dev_attr_ipInDiscards.attr,
  792. &dev_attr_ipInDelivers.attr,
  793. &dev_attr_ipOutForwDatagrams.attr,
  794. &dev_attr_ipOutRequests.attr,
  795. &dev_attr_ipOutDiscards.attr,
  796. &dev_attr_ipOutNoRoutes.attr,
  797. &dev_attr_ipReasmTimeout.attr,
  798. &dev_attr_ipReasmReqds.attr,
  799. &dev_attr_ipReasmOKs.attr,
  800. &dev_attr_ipReasmFails.attr,
  801. &dev_attr_ipFragOKs.attr,
  802. &dev_attr_ipFragFails.attr,
  803. &dev_attr_ipFragCreates.attr,
  804. &dev_attr_ipInMcastPkts.attr,
  805. &dev_attr_ipOutMcastPkts.attr,
  806. &dev_attr_ipInBcastPkts.attr,
  807. &dev_attr_ipOutBcastPkts.attr,
  808. &dev_attr_tcpRtoAlgorithm.attr,
  809. &dev_attr_tcpRtoMin.attr,
  810. &dev_attr_tcpRtoMax.attr,
  811. &dev_attr_tcpMaxConn.attr,
  812. &dev_attr_tcpActiveOpens.attr,
  813. &dev_attr_tcpPassiveOpens.attr,
  814. &dev_attr_tcpAttemptFails.attr,
  815. &dev_attr_tcpEstabResets.attr,
  816. &dev_attr_tcpCurrEstab.attr,
  817. &dev_attr_tcpInSegs.attr,
  818. &dev_attr_tcpOutSegs.attr,
  819. &dev_attr_tcpRetransSegs.attr,
  820. &dev_attr_tcpInErrs.attr,
  821. &dev_attr_tcpOutRsts.attr,
  822. NULL
  823. };
  824. static struct attribute_group iw_stats_group = {
  825. .name = "proto_stats",
  826. .attrs = iw_proto_stats_attrs,
  827. };
  828. static void free_port_list_attributes(struct ib_device *device)
  829. {
  830. struct kobject *p, *t;
  831. list_for_each_entry_safe(p, t, &device->port_list, entry) {
  832. struct ib_port *port = container_of(p, struct ib_port, kobj);
  833. list_del(&p->entry);
  834. sysfs_remove_group(p, &pma_group);
  835. sysfs_remove_group(p, &port->pkey_group);
  836. sysfs_remove_group(p, &port->gid_group);
  837. sysfs_remove_group(&port->gid_attr_group->kobj,
  838. &port->gid_attr_group->ndev);
  839. sysfs_remove_group(&port->gid_attr_group->kobj,
  840. &port->gid_attr_group->type);
  841. kobject_put(&port->gid_attr_group->kobj);
  842. kobject_put(p);
  843. }
  844. kobject_put(device->ports_parent);
  845. }
  846. int ib_device_register_sysfs(struct ib_device *device,
  847. int (*port_callback)(struct ib_device *,
  848. u8, struct kobject *))
  849. {
  850. struct device *class_dev = &device->dev;
  851. int ret;
  852. int i;
  853. device->dev.parent = device->dma_device;
  854. ret = dev_set_name(class_dev, "%s", device->name);
  855. if (ret)
  856. return ret;
  857. ret = device_add(class_dev);
  858. if (ret)
  859. goto err;
  860. for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
  861. ret = device_create_file(class_dev, ib_class_attributes[i]);
  862. if (ret)
  863. goto err_unregister;
  864. }
  865. device->ports_parent = kobject_create_and_add("ports",
  866. &class_dev->kobj);
  867. if (!device->ports_parent) {
  868. ret = -ENOMEM;
  869. goto err_put;
  870. }
  871. if (rdma_cap_ib_switch(device)) {
  872. ret = add_port(device, 0, port_callback);
  873. if (ret)
  874. goto err_put;
  875. } else {
  876. for (i = 1; i <= device->phys_port_cnt; ++i) {
  877. ret = add_port(device, i, port_callback);
  878. if (ret)
  879. goto err_put;
  880. }
  881. }
  882. if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
  883. ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
  884. if (ret)
  885. goto err_put;
  886. }
  887. return 0;
  888. err_put:
  889. free_port_list_attributes(device);
  890. err_unregister:
  891. device_unregister(class_dev);
  892. err:
  893. return ret;
  894. }
  895. void ib_device_unregister_sysfs(struct ib_device *device)
  896. {
  897. /* Hold kobject until ib_dealloc_device() */
  898. struct kobject *kobj_dev = kobject_get(&device->dev.kobj);
  899. int i;
  900. if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats)
  901. sysfs_remove_group(kobj_dev, &iw_stats_group);
  902. free_port_list_attributes(device);
  903. for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
  904. device_remove_file(&device->dev, ib_class_attributes[i]);
  905. device_unregister(&device->dev);
  906. }