sysfs.c 37 KB

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