sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright(c) 2015-2017 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/ctype.h>
  48. #include "hfi.h"
  49. #include "mad.h"
  50. #include "trace.h"
  51. /*
  52. * Start of per-port congestion control structures and support code
  53. */
  54. /*
  55. * Congestion control table size followed by table entries
  56. */
  57. static ssize_t read_cc_table_bin(struct file *filp, struct kobject *kobj,
  58. struct bin_attribute *bin_attr,
  59. char *buf, loff_t pos, size_t count)
  60. {
  61. int ret;
  62. struct hfi1_pportdata *ppd =
  63. container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
  64. struct cc_state *cc_state;
  65. ret = ppd->total_cct_entry * sizeof(struct ib_cc_table_entry_shadow)
  66. + sizeof(__be16);
  67. if (pos > ret)
  68. return -EINVAL;
  69. if (count > ret - pos)
  70. count = ret - pos;
  71. if (!count)
  72. return count;
  73. rcu_read_lock();
  74. cc_state = get_cc_state(ppd);
  75. if (!cc_state) {
  76. rcu_read_unlock();
  77. return -EINVAL;
  78. }
  79. memcpy(buf, (void *)&cc_state->cct + pos, count);
  80. rcu_read_unlock();
  81. return count;
  82. }
  83. static void port_release(struct kobject *kobj)
  84. {
  85. /* nothing to do since memory is freed by hfi1_free_devdata() */
  86. }
  87. static const struct bin_attribute cc_table_bin_attr = {
  88. .attr = {.name = "cc_table_bin", .mode = 0444},
  89. .read = read_cc_table_bin,
  90. .size = PAGE_SIZE,
  91. };
  92. /*
  93. * Congestion settings: port control, control map and an array of 16
  94. * entries for the congestion entries - increase, timer, event log
  95. * trigger threshold and the minimum injection rate delay.
  96. */
  97. static ssize_t read_cc_setting_bin(struct file *filp, struct kobject *kobj,
  98. struct bin_attribute *bin_attr,
  99. char *buf, loff_t pos, size_t count)
  100. {
  101. int ret;
  102. struct hfi1_pportdata *ppd =
  103. container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
  104. struct cc_state *cc_state;
  105. ret = sizeof(struct opa_congestion_setting_attr_shadow);
  106. if (pos > ret)
  107. return -EINVAL;
  108. if (count > ret - pos)
  109. count = ret - pos;
  110. if (!count)
  111. return count;
  112. rcu_read_lock();
  113. cc_state = get_cc_state(ppd);
  114. if (!cc_state) {
  115. rcu_read_unlock();
  116. return -EINVAL;
  117. }
  118. memcpy(buf, (void *)&cc_state->cong_setting + pos, count);
  119. rcu_read_unlock();
  120. return count;
  121. }
  122. static const struct bin_attribute cc_setting_bin_attr = {
  123. .attr = {.name = "cc_settings_bin", .mode = 0444},
  124. .read = read_cc_setting_bin,
  125. .size = PAGE_SIZE,
  126. };
  127. struct hfi1_port_attr {
  128. struct attribute attr;
  129. ssize_t (*show)(struct hfi1_pportdata *, char *);
  130. ssize_t (*store)(struct hfi1_pportdata *, const char *, size_t);
  131. };
  132. static ssize_t cc_prescan_show(struct hfi1_pportdata *ppd, char *buf)
  133. {
  134. return sprintf(buf, "%s\n", ppd->cc_prescan ? "on" : "off");
  135. }
  136. static ssize_t cc_prescan_store(struct hfi1_pportdata *ppd, const char *buf,
  137. size_t count)
  138. {
  139. if (!memcmp(buf, "on", 2))
  140. ppd->cc_prescan = true;
  141. else if (!memcmp(buf, "off", 3))
  142. ppd->cc_prescan = false;
  143. return count;
  144. }
  145. static struct hfi1_port_attr cc_prescan_attr =
  146. __ATTR(cc_prescan, 0600, cc_prescan_show, cc_prescan_store);
  147. static ssize_t cc_attr_show(struct kobject *kobj, struct attribute *attr,
  148. char *buf)
  149. {
  150. struct hfi1_port_attr *port_attr =
  151. container_of(attr, struct hfi1_port_attr, attr);
  152. struct hfi1_pportdata *ppd =
  153. container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
  154. return port_attr->show(ppd, buf);
  155. }
  156. static ssize_t cc_attr_store(struct kobject *kobj, struct attribute *attr,
  157. const char *buf, size_t count)
  158. {
  159. struct hfi1_port_attr *port_attr =
  160. container_of(attr, struct hfi1_port_attr, attr);
  161. struct hfi1_pportdata *ppd =
  162. container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
  163. return port_attr->store(ppd, buf, count);
  164. }
  165. static const struct sysfs_ops port_cc_sysfs_ops = {
  166. .show = cc_attr_show,
  167. .store = cc_attr_store
  168. };
  169. static struct attribute *port_cc_default_attributes[] = {
  170. &cc_prescan_attr.attr,
  171. NULL
  172. };
  173. static struct kobj_type port_cc_ktype = {
  174. .release = port_release,
  175. .sysfs_ops = &port_cc_sysfs_ops,
  176. .default_attrs = port_cc_default_attributes
  177. };
  178. /* Start sc2vl */
  179. #define HFI1_SC2VL_ATTR(N) \
  180. static struct hfi1_sc2vl_attr hfi1_sc2vl_attr_##N = { \
  181. .attr = { .name = __stringify(N), .mode = 0444 }, \
  182. .sc = N \
  183. }
  184. struct hfi1_sc2vl_attr {
  185. struct attribute attr;
  186. int sc;
  187. };
  188. HFI1_SC2VL_ATTR(0);
  189. HFI1_SC2VL_ATTR(1);
  190. HFI1_SC2VL_ATTR(2);
  191. HFI1_SC2VL_ATTR(3);
  192. HFI1_SC2VL_ATTR(4);
  193. HFI1_SC2VL_ATTR(5);
  194. HFI1_SC2VL_ATTR(6);
  195. HFI1_SC2VL_ATTR(7);
  196. HFI1_SC2VL_ATTR(8);
  197. HFI1_SC2VL_ATTR(9);
  198. HFI1_SC2VL_ATTR(10);
  199. HFI1_SC2VL_ATTR(11);
  200. HFI1_SC2VL_ATTR(12);
  201. HFI1_SC2VL_ATTR(13);
  202. HFI1_SC2VL_ATTR(14);
  203. HFI1_SC2VL_ATTR(15);
  204. HFI1_SC2VL_ATTR(16);
  205. HFI1_SC2VL_ATTR(17);
  206. HFI1_SC2VL_ATTR(18);
  207. HFI1_SC2VL_ATTR(19);
  208. HFI1_SC2VL_ATTR(20);
  209. HFI1_SC2VL_ATTR(21);
  210. HFI1_SC2VL_ATTR(22);
  211. HFI1_SC2VL_ATTR(23);
  212. HFI1_SC2VL_ATTR(24);
  213. HFI1_SC2VL_ATTR(25);
  214. HFI1_SC2VL_ATTR(26);
  215. HFI1_SC2VL_ATTR(27);
  216. HFI1_SC2VL_ATTR(28);
  217. HFI1_SC2VL_ATTR(29);
  218. HFI1_SC2VL_ATTR(30);
  219. HFI1_SC2VL_ATTR(31);
  220. static struct attribute *sc2vl_default_attributes[] = {
  221. &hfi1_sc2vl_attr_0.attr,
  222. &hfi1_sc2vl_attr_1.attr,
  223. &hfi1_sc2vl_attr_2.attr,
  224. &hfi1_sc2vl_attr_3.attr,
  225. &hfi1_sc2vl_attr_4.attr,
  226. &hfi1_sc2vl_attr_5.attr,
  227. &hfi1_sc2vl_attr_6.attr,
  228. &hfi1_sc2vl_attr_7.attr,
  229. &hfi1_sc2vl_attr_8.attr,
  230. &hfi1_sc2vl_attr_9.attr,
  231. &hfi1_sc2vl_attr_10.attr,
  232. &hfi1_sc2vl_attr_11.attr,
  233. &hfi1_sc2vl_attr_12.attr,
  234. &hfi1_sc2vl_attr_13.attr,
  235. &hfi1_sc2vl_attr_14.attr,
  236. &hfi1_sc2vl_attr_15.attr,
  237. &hfi1_sc2vl_attr_16.attr,
  238. &hfi1_sc2vl_attr_17.attr,
  239. &hfi1_sc2vl_attr_18.attr,
  240. &hfi1_sc2vl_attr_19.attr,
  241. &hfi1_sc2vl_attr_20.attr,
  242. &hfi1_sc2vl_attr_21.attr,
  243. &hfi1_sc2vl_attr_22.attr,
  244. &hfi1_sc2vl_attr_23.attr,
  245. &hfi1_sc2vl_attr_24.attr,
  246. &hfi1_sc2vl_attr_25.attr,
  247. &hfi1_sc2vl_attr_26.attr,
  248. &hfi1_sc2vl_attr_27.attr,
  249. &hfi1_sc2vl_attr_28.attr,
  250. &hfi1_sc2vl_attr_29.attr,
  251. &hfi1_sc2vl_attr_30.attr,
  252. &hfi1_sc2vl_attr_31.attr,
  253. NULL
  254. };
  255. static ssize_t sc2vl_attr_show(struct kobject *kobj, struct attribute *attr,
  256. char *buf)
  257. {
  258. struct hfi1_sc2vl_attr *sattr =
  259. container_of(attr, struct hfi1_sc2vl_attr, attr);
  260. struct hfi1_pportdata *ppd =
  261. container_of(kobj, struct hfi1_pportdata, sc2vl_kobj);
  262. struct hfi1_devdata *dd = ppd->dd;
  263. return sprintf(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
  264. }
  265. static const struct sysfs_ops hfi1_sc2vl_ops = {
  266. .show = sc2vl_attr_show,
  267. };
  268. static struct kobj_type hfi1_sc2vl_ktype = {
  269. .release = port_release,
  270. .sysfs_ops = &hfi1_sc2vl_ops,
  271. .default_attrs = sc2vl_default_attributes
  272. };
  273. /* End sc2vl */
  274. /* Start sl2sc */
  275. #define HFI1_SL2SC_ATTR(N) \
  276. static struct hfi1_sl2sc_attr hfi1_sl2sc_attr_##N = { \
  277. .attr = { .name = __stringify(N), .mode = 0444 }, \
  278. .sl = N \
  279. }
  280. struct hfi1_sl2sc_attr {
  281. struct attribute attr;
  282. int sl;
  283. };
  284. HFI1_SL2SC_ATTR(0);
  285. HFI1_SL2SC_ATTR(1);
  286. HFI1_SL2SC_ATTR(2);
  287. HFI1_SL2SC_ATTR(3);
  288. HFI1_SL2SC_ATTR(4);
  289. HFI1_SL2SC_ATTR(5);
  290. HFI1_SL2SC_ATTR(6);
  291. HFI1_SL2SC_ATTR(7);
  292. HFI1_SL2SC_ATTR(8);
  293. HFI1_SL2SC_ATTR(9);
  294. HFI1_SL2SC_ATTR(10);
  295. HFI1_SL2SC_ATTR(11);
  296. HFI1_SL2SC_ATTR(12);
  297. HFI1_SL2SC_ATTR(13);
  298. HFI1_SL2SC_ATTR(14);
  299. HFI1_SL2SC_ATTR(15);
  300. HFI1_SL2SC_ATTR(16);
  301. HFI1_SL2SC_ATTR(17);
  302. HFI1_SL2SC_ATTR(18);
  303. HFI1_SL2SC_ATTR(19);
  304. HFI1_SL2SC_ATTR(20);
  305. HFI1_SL2SC_ATTR(21);
  306. HFI1_SL2SC_ATTR(22);
  307. HFI1_SL2SC_ATTR(23);
  308. HFI1_SL2SC_ATTR(24);
  309. HFI1_SL2SC_ATTR(25);
  310. HFI1_SL2SC_ATTR(26);
  311. HFI1_SL2SC_ATTR(27);
  312. HFI1_SL2SC_ATTR(28);
  313. HFI1_SL2SC_ATTR(29);
  314. HFI1_SL2SC_ATTR(30);
  315. HFI1_SL2SC_ATTR(31);
  316. static struct attribute *sl2sc_default_attributes[] = {
  317. &hfi1_sl2sc_attr_0.attr,
  318. &hfi1_sl2sc_attr_1.attr,
  319. &hfi1_sl2sc_attr_2.attr,
  320. &hfi1_sl2sc_attr_3.attr,
  321. &hfi1_sl2sc_attr_4.attr,
  322. &hfi1_sl2sc_attr_5.attr,
  323. &hfi1_sl2sc_attr_6.attr,
  324. &hfi1_sl2sc_attr_7.attr,
  325. &hfi1_sl2sc_attr_8.attr,
  326. &hfi1_sl2sc_attr_9.attr,
  327. &hfi1_sl2sc_attr_10.attr,
  328. &hfi1_sl2sc_attr_11.attr,
  329. &hfi1_sl2sc_attr_12.attr,
  330. &hfi1_sl2sc_attr_13.attr,
  331. &hfi1_sl2sc_attr_14.attr,
  332. &hfi1_sl2sc_attr_15.attr,
  333. &hfi1_sl2sc_attr_16.attr,
  334. &hfi1_sl2sc_attr_17.attr,
  335. &hfi1_sl2sc_attr_18.attr,
  336. &hfi1_sl2sc_attr_19.attr,
  337. &hfi1_sl2sc_attr_20.attr,
  338. &hfi1_sl2sc_attr_21.attr,
  339. &hfi1_sl2sc_attr_22.attr,
  340. &hfi1_sl2sc_attr_23.attr,
  341. &hfi1_sl2sc_attr_24.attr,
  342. &hfi1_sl2sc_attr_25.attr,
  343. &hfi1_sl2sc_attr_26.attr,
  344. &hfi1_sl2sc_attr_27.attr,
  345. &hfi1_sl2sc_attr_28.attr,
  346. &hfi1_sl2sc_attr_29.attr,
  347. &hfi1_sl2sc_attr_30.attr,
  348. &hfi1_sl2sc_attr_31.attr,
  349. NULL
  350. };
  351. static ssize_t sl2sc_attr_show(struct kobject *kobj, struct attribute *attr,
  352. char *buf)
  353. {
  354. struct hfi1_sl2sc_attr *sattr =
  355. container_of(attr, struct hfi1_sl2sc_attr, attr);
  356. struct hfi1_pportdata *ppd =
  357. container_of(kobj, struct hfi1_pportdata, sl2sc_kobj);
  358. struct hfi1_ibport *ibp = &ppd->ibport_data;
  359. return sprintf(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
  360. }
  361. static const struct sysfs_ops hfi1_sl2sc_ops = {
  362. .show = sl2sc_attr_show,
  363. };
  364. static struct kobj_type hfi1_sl2sc_ktype = {
  365. .release = port_release,
  366. .sysfs_ops = &hfi1_sl2sc_ops,
  367. .default_attrs = sl2sc_default_attributes
  368. };
  369. /* End sl2sc */
  370. /* Start vl2mtu */
  371. #define HFI1_VL2MTU_ATTR(N) \
  372. static struct hfi1_vl2mtu_attr hfi1_vl2mtu_attr_##N = { \
  373. .attr = { .name = __stringify(N), .mode = 0444 }, \
  374. .vl = N \
  375. }
  376. struct hfi1_vl2mtu_attr {
  377. struct attribute attr;
  378. int vl;
  379. };
  380. HFI1_VL2MTU_ATTR(0);
  381. HFI1_VL2MTU_ATTR(1);
  382. HFI1_VL2MTU_ATTR(2);
  383. HFI1_VL2MTU_ATTR(3);
  384. HFI1_VL2MTU_ATTR(4);
  385. HFI1_VL2MTU_ATTR(5);
  386. HFI1_VL2MTU_ATTR(6);
  387. HFI1_VL2MTU_ATTR(7);
  388. HFI1_VL2MTU_ATTR(8);
  389. HFI1_VL2MTU_ATTR(9);
  390. HFI1_VL2MTU_ATTR(10);
  391. HFI1_VL2MTU_ATTR(11);
  392. HFI1_VL2MTU_ATTR(12);
  393. HFI1_VL2MTU_ATTR(13);
  394. HFI1_VL2MTU_ATTR(14);
  395. HFI1_VL2MTU_ATTR(15);
  396. static struct attribute *vl2mtu_default_attributes[] = {
  397. &hfi1_vl2mtu_attr_0.attr,
  398. &hfi1_vl2mtu_attr_1.attr,
  399. &hfi1_vl2mtu_attr_2.attr,
  400. &hfi1_vl2mtu_attr_3.attr,
  401. &hfi1_vl2mtu_attr_4.attr,
  402. &hfi1_vl2mtu_attr_5.attr,
  403. &hfi1_vl2mtu_attr_6.attr,
  404. &hfi1_vl2mtu_attr_7.attr,
  405. &hfi1_vl2mtu_attr_8.attr,
  406. &hfi1_vl2mtu_attr_9.attr,
  407. &hfi1_vl2mtu_attr_10.attr,
  408. &hfi1_vl2mtu_attr_11.attr,
  409. &hfi1_vl2mtu_attr_12.attr,
  410. &hfi1_vl2mtu_attr_13.attr,
  411. &hfi1_vl2mtu_attr_14.attr,
  412. &hfi1_vl2mtu_attr_15.attr,
  413. NULL
  414. };
  415. static ssize_t vl2mtu_attr_show(struct kobject *kobj, struct attribute *attr,
  416. char *buf)
  417. {
  418. struct hfi1_vl2mtu_attr *vlattr =
  419. container_of(attr, struct hfi1_vl2mtu_attr, attr);
  420. struct hfi1_pportdata *ppd =
  421. container_of(kobj, struct hfi1_pportdata, vl2mtu_kobj);
  422. struct hfi1_devdata *dd = ppd->dd;
  423. return sprintf(buf, "%u\n", dd->vld[vlattr->vl].mtu);
  424. }
  425. static const struct sysfs_ops hfi1_vl2mtu_ops = {
  426. .show = vl2mtu_attr_show,
  427. };
  428. static struct kobj_type hfi1_vl2mtu_ktype = {
  429. .release = port_release,
  430. .sysfs_ops = &hfi1_vl2mtu_ops,
  431. .default_attrs = vl2mtu_default_attributes
  432. };
  433. /* end of per-port file structures and support code */
  434. /*
  435. * Start of per-unit (or driver, in some cases, but replicated
  436. * per unit) functions (these get a device *)
  437. */
  438. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  439. char *buf)
  440. {
  441. struct hfi1_ibdev *dev =
  442. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  443. return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
  444. }
  445. static ssize_t show_hfi(struct device *device, struct device_attribute *attr,
  446. char *buf)
  447. {
  448. struct hfi1_ibdev *dev =
  449. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  450. struct hfi1_devdata *dd = dd_from_dev(dev);
  451. int ret;
  452. if (!dd->boardname)
  453. ret = -EINVAL;
  454. else
  455. ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
  456. return ret;
  457. }
  458. static ssize_t show_boardversion(struct device *device,
  459. struct device_attribute *attr, char *buf)
  460. {
  461. struct hfi1_ibdev *dev =
  462. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  463. struct hfi1_devdata *dd = dd_from_dev(dev);
  464. /* The string printed here is already newline-terminated. */
  465. return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
  466. }
  467. static ssize_t show_nctxts(struct device *device,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. struct hfi1_ibdev *dev =
  471. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  472. struct hfi1_devdata *dd = dd_from_dev(dev);
  473. /*
  474. * Return the smaller of send and receive contexts.
  475. * Normally, user level applications would require both a send
  476. * and a receive context, so returning the smaller of the two counts
  477. * give a more accurate picture of total contexts available.
  478. */
  479. return scnprintf(buf, PAGE_SIZE, "%u\n",
  480. min(dd->num_user_contexts,
  481. (u32)dd->sc_sizes[SC_USER].count));
  482. }
  483. static ssize_t show_nfreectxts(struct device *device,
  484. struct device_attribute *attr, char *buf)
  485. {
  486. struct hfi1_ibdev *dev =
  487. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  488. struct hfi1_devdata *dd = dd_from_dev(dev);
  489. /* Return the number of free user ports (contexts) available. */
  490. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->freectxts);
  491. }
  492. static ssize_t show_serial(struct device *device,
  493. struct device_attribute *attr, char *buf)
  494. {
  495. struct hfi1_ibdev *dev =
  496. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  497. struct hfi1_devdata *dd = dd_from_dev(dev);
  498. return scnprintf(buf, PAGE_SIZE, "%s", dd->serial);
  499. }
  500. static ssize_t store_chip_reset(struct device *device,
  501. struct device_attribute *attr, const char *buf,
  502. size_t count)
  503. {
  504. struct hfi1_ibdev *dev =
  505. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  506. struct hfi1_devdata *dd = dd_from_dev(dev);
  507. int ret;
  508. if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
  509. ret = -EINVAL;
  510. goto bail;
  511. }
  512. ret = hfi1_reset_device(dd->unit);
  513. bail:
  514. return ret < 0 ? ret : count;
  515. }
  516. /*
  517. * Convert the reported temperature from an integer (reported in
  518. * units of 0.25C) to a floating point number.
  519. */
  520. #define temp2str(temp, buf, size, idx) \
  521. scnprintf((buf) + (idx), (size) - (idx), "%u.%02u ", \
  522. ((temp) >> 2), ((temp) & 0x3) * 25)
  523. /*
  524. * Dump tempsense values, in decimal, to ease shell-scripts.
  525. */
  526. static ssize_t show_tempsense(struct device *device,
  527. struct device_attribute *attr, char *buf)
  528. {
  529. struct hfi1_ibdev *dev =
  530. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  531. struct hfi1_devdata *dd = dd_from_dev(dev);
  532. struct hfi1_temp temp;
  533. int ret;
  534. ret = hfi1_tempsense_rd(dd, &temp);
  535. if (!ret) {
  536. int idx = 0;
  537. idx += temp2str(temp.curr, buf, PAGE_SIZE, idx);
  538. idx += temp2str(temp.lo_lim, buf, PAGE_SIZE, idx);
  539. idx += temp2str(temp.hi_lim, buf, PAGE_SIZE, idx);
  540. idx += temp2str(temp.crit_lim, buf, PAGE_SIZE, idx);
  541. idx += scnprintf(buf + idx, PAGE_SIZE - idx,
  542. "%u %u %u\n", temp.triggers & 0x1,
  543. temp.triggers & 0x2, temp.triggers & 0x4);
  544. ret = idx;
  545. }
  546. return ret;
  547. }
  548. /*
  549. * end of per-unit (or driver, in some cases, but replicated
  550. * per unit) functions
  551. */
  552. /* start of per-unit file structures and support code */
  553. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  554. static DEVICE_ATTR(board_id, S_IRUGO, show_hfi, NULL);
  555. static DEVICE_ATTR(nctxts, S_IRUGO, show_nctxts, NULL);
  556. static DEVICE_ATTR(nfreectxts, S_IRUGO, show_nfreectxts, NULL);
  557. static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
  558. static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
  559. static DEVICE_ATTR(tempsense, S_IRUGO, show_tempsense, NULL);
  560. static DEVICE_ATTR(chip_reset, S_IWUSR, NULL, store_chip_reset);
  561. static struct device_attribute *hfi1_attributes[] = {
  562. &dev_attr_hw_rev,
  563. &dev_attr_board_id,
  564. &dev_attr_nctxts,
  565. &dev_attr_nfreectxts,
  566. &dev_attr_serial,
  567. &dev_attr_boardversion,
  568. &dev_attr_tempsense,
  569. &dev_attr_chip_reset,
  570. };
  571. int hfi1_create_port_files(struct ib_device *ibdev, u8 port_num,
  572. struct kobject *kobj)
  573. {
  574. struct hfi1_pportdata *ppd;
  575. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  576. int ret;
  577. if (!port_num || port_num > dd->num_pports) {
  578. dd_dev_err(dd,
  579. "Skipping infiniband class with invalid port %u\n",
  580. port_num);
  581. return -ENODEV;
  582. }
  583. ppd = &dd->pport[port_num - 1];
  584. ret = kobject_init_and_add(&ppd->sc2vl_kobj, &hfi1_sc2vl_ktype, kobj,
  585. "sc2vl");
  586. if (ret) {
  587. dd_dev_err(dd,
  588. "Skipping sc2vl sysfs info, (err %d) port %u\n",
  589. ret, port_num);
  590. /*
  591. * Based on the documentation for kobject_init_and_add(), the
  592. * caller should call kobject_put even if this call fails.
  593. */
  594. goto bail_sc2vl;
  595. }
  596. kobject_uevent(&ppd->sc2vl_kobj, KOBJ_ADD);
  597. ret = kobject_init_and_add(&ppd->sl2sc_kobj, &hfi1_sl2sc_ktype, kobj,
  598. "sl2sc");
  599. if (ret) {
  600. dd_dev_err(dd,
  601. "Skipping sl2sc sysfs info, (err %d) port %u\n",
  602. ret, port_num);
  603. goto bail_sl2sc;
  604. }
  605. kobject_uevent(&ppd->sl2sc_kobj, KOBJ_ADD);
  606. ret = kobject_init_and_add(&ppd->vl2mtu_kobj, &hfi1_vl2mtu_ktype, kobj,
  607. "vl2mtu");
  608. if (ret) {
  609. dd_dev_err(dd,
  610. "Skipping vl2mtu sysfs info, (err %d) port %u\n",
  611. ret, port_num);
  612. goto bail_vl2mtu;
  613. }
  614. kobject_uevent(&ppd->vl2mtu_kobj, KOBJ_ADD);
  615. ret = kobject_init_and_add(&ppd->pport_cc_kobj, &port_cc_ktype,
  616. kobj, "CCMgtA");
  617. if (ret) {
  618. dd_dev_err(dd,
  619. "Skipping Congestion Control sysfs info, (err %d) port %u\n",
  620. ret, port_num);
  621. goto bail_cc;
  622. }
  623. kobject_uevent(&ppd->pport_cc_kobj, KOBJ_ADD);
  624. ret = sysfs_create_bin_file(&ppd->pport_cc_kobj, &cc_setting_bin_attr);
  625. if (ret) {
  626. dd_dev_err(dd,
  627. "Skipping Congestion Control setting sysfs info, (err %d) port %u\n",
  628. ret, port_num);
  629. goto bail_cc;
  630. }
  631. ret = sysfs_create_bin_file(&ppd->pport_cc_kobj, &cc_table_bin_attr);
  632. if (ret) {
  633. dd_dev_err(dd,
  634. "Skipping Congestion Control table sysfs info, (err %d) port %u\n",
  635. ret, port_num);
  636. goto bail_cc_entry_bin;
  637. }
  638. dd_dev_info(dd,
  639. "Congestion Control Agent enabled for port %d\n",
  640. port_num);
  641. return 0;
  642. bail_cc_entry_bin:
  643. sysfs_remove_bin_file(&ppd->pport_cc_kobj,
  644. &cc_setting_bin_attr);
  645. bail_cc:
  646. kobject_put(&ppd->pport_cc_kobj);
  647. bail_vl2mtu:
  648. kobject_put(&ppd->vl2mtu_kobj);
  649. bail_sl2sc:
  650. kobject_put(&ppd->sl2sc_kobj);
  651. bail_sc2vl:
  652. kobject_put(&ppd->sc2vl_kobj);
  653. return ret;
  654. }
  655. struct sde_attribute {
  656. struct attribute attr;
  657. ssize_t (*show)(struct sdma_engine *sde, char *buf);
  658. ssize_t (*store)(struct sdma_engine *sde, const char *buf, size_t cnt);
  659. };
  660. static ssize_t sde_show(struct kobject *kobj, struct attribute *attr, char *buf)
  661. {
  662. struct sde_attribute *sde_attr =
  663. container_of(attr, struct sde_attribute, attr);
  664. struct sdma_engine *sde =
  665. container_of(kobj, struct sdma_engine, kobj);
  666. if (!sde_attr->show)
  667. return -EINVAL;
  668. return sde_attr->show(sde, buf);
  669. }
  670. static ssize_t sde_store(struct kobject *kobj, struct attribute *attr,
  671. const char *buf, size_t count)
  672. {
  673. struct sde_attribute *sde_attr =
  674. container_of(attr, struct sde_attribute, attr);
  675. struct sdma_engine *sde =
  676. container_of(kobj, struct sdma_engine, kobj);
  677. if (!capable(CAP_SYS_ADMIN))
  678. return -EPERM;
  679. if (!sde_attr->store)
  680. return -EINVAL;
  681. return sde_attr->store(sde, buf, count);
  682. }
  683. static const struct sysfs_ops sde_sysfs_ops = {
  684. .show = sde_show,
  685. .store = sde_store,
  686. };
  687. static struct kobj_type sde_ktype = {
  688. .sysfs_ops = &sde_sysfs_ops,
  689. };
  690. #define SDE_ATTR(_name, _mode, _show, _store) \
  691. struct sde_attribute sde_attr_##_name = \
  692. __ATTR(_name, _mode, _show, _store)
  693. static ssize_t sde_show_cpu_to_sde_map(struct sdma_engine *sde, char *buf)
  694. {
  695. return sdma_get_cpu_to_sde_map(sde, buf);
  696. }
  697. static ssize_t sde_store_cpu_to_sde_map(struct sdma_engine *sde,
  698. const char *buf, size_t count)
  699. {
  700. return sdma_set_cpu_to_sde_map(sde, buf, count);
  701. }
  702. static ssize_t sde_show_vl(struct sdma_engine *sde, char *buf)
  703. {
  704. int vl;
  705. vl = sdma_engine_get_vl(sde);
  706. if (vl < 0)
  707. return vl;
  708. return snprintf(buf, PAGE_SIZE, "%d\n", vl);
  709. }
  710. static SDE_ATTR(cpu_list, S_IWUSR | S_IRUGO,
  711. sde_show_cpu_to_sde_map,
  712. sde_store_cpu_to_sde_map);
  713. static SDE_ATTR(vl, S_IRUGO, sde_show_vl, NULL);
  714. static struct sde_attribute *sde_attribs[] = {
  715. &sde_attr_cpu_list,
  716. &sde_attr_vl
  717. };
  718. /*
  719. * Register and create our files in /sys/class/infiniband.
  720. */
  721. int hfi1_verbs_register_sysfs(struct hfi1_devdata *dd)
  722. {
  723. struct ib_device *dev = &dd->verbs_dev.rdi.ibdev;
  724. struct device *class_dev = &dev->dev;
  725. int i, j, ret;
  726. for (i = 0; i < ARRAY_SIZE(hfi1_attributes); ++i) {
  727. ret = device_create_file(&dev->dev, hfi1_attributes[i]);
  728. if (ret)
  729. goto bail;
  730. }
  731. for (i = 0; i < dd->num_sdma; i++) {
  732. ret = kobject_init_and_add(&dd->per_sdma[i].kobj,
  733. &sde_ktype, &class_dev->kobj,
  734. "sdma%d", i);
  735. if (ret)
  736. goto bail;
  737. for (j = 0; j < ARRAY_SIZE(sde_attribs); j++) {
  738. ret = sysfs_create_file(&dd->per_sdma[i].kobj,
  739. &sde_attribs[j]->attr);
  740. if (ret)
  741. goto bail;
  742. }
  743. }
  744. return 0;
  745. bail:
  746. for (i = 0; i < ARRAY_SIZE(hfi1_attributes); ++i)
  747. device_remove_file(&dev->dev, hfi1_attributes[i]);
  748. /*
  749. * The function kobject_put() will call kobject_del() if the kobject
  750. * has been added successfully. The sysfs files created under the
  751. * kobject directory will also be removed during the process.
  752. */
  753. for (; i >= 0; i--)
  754. kobject_put(&dd->per_sdma[i].kobj);
  755. return ret;
  756. }
  757. /*
  758. * Unregister and remove our files in /sys/class/infiniband.
  759. */
  760. void hfi1_verbs_unregister_sysfs(struct hfi1_devdata *dd)
  761. {
  762. struct hfi1_pportdata *ppd;
  763. int i;
  764. /* Unwind operations in hfi1_verbs_register_sysfs() */
  765. for (i = 0; i < dd->num_sdma; i++)
  766. kobject_put(&dd->per_sdma[i].kobj);
  767. for (i = 0; i < dd->num_pports; i++) {
  768. ppd = &dd->pport[i];
  769. sysfs_remove_bin_file(&ppd->pport_cc_kobj,
  770. &cc_setting_bin_attr);
  771. sysfs_remove_bin_file(&ppd->pport_cc_kobj,
  772. &cc_table_bin_attr);
  773. kobject_put(&ppd->pport_cc_kobj);
  774. kobject_put(&ppd->vl2mtu_kobj);
  775. kobject_put(&ppd->sl2sc_kobj);
  776. kobject_put(&ppd->sc2vl_kobj);
  777. }
  778. }