sysfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Copyright(c) 2015, 2016 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 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 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. };
  172. static struct kobj_type port_cc_ktype = {
  173. .release = port_release,
  174. .sysfs_ops = &port_cc_sysfs_ops,
  175. .default_attrs = port_cc_default_attributes
  176. };
  177. /* Start sc2vl */
  178. #define HFI1_SC2VL_ATTR(N) \
  179. static struct hfi1_sc2vl_attr hfi1_sc2vl_attr_##N = { \
  180. .attr = { .name = __stringify(N), .mode = 0444 }, \
  181. .sc = N \
  182. }
  183. struct hfi1_sc2vl_attr {
  184. struct attribute attr;
  185. int sc;
  186. };
  187. HFI1_SC2VL_ATTR(0);
  188. HFI1_SC2VL_ATTR(1);
  189. HFI1_SC2VL_ATTR(2);
  190. HFI1_SC2VL_ATTR(3);
  191. HFI1_SC2VL_ATTR(4);
  192. HFI1_SC2VL_ATTR(5);
  193. HFI1_SC2VL_ATTR(6);
  194. HFI1_SC2VL_ATTR(7);
  195. HFI1_SC2VL_ATTR(8);
  196. HFI1_SC2VL_ATTR(9);
  197. HFI1_SC2VL_ATTR(10);
  198. HFI1_SC2VL_ATTR(11);
  199. HFI1_SC2VL_ATTR(12);
  200. HFI1_SC2VL_ATTR(13);
  201. HFI1_SC2VL_ATTR(14);
  202. HFI1_SC2VL_ATTR(15);
  203. HFI1_SC2VL_ATTR(16);
  204. HFI1_SC2VL_ATTR(17);
  205. HFI1_SC2VL_ATTR(18);
  206. HFI1_SC2VL_ATTR(19);
  207. HFI1_SC2VL_ATTR(20);
  208. HFI1_SC2VL_ATTR(21);
  209. HFI1_SC2VL_ATTR(22);
  210. HFI1_SC2VL_ATTR(23);
  211. HFI1_SC2VL_ATTR(24);
  212. HFI1_SC2VL_ATTR(25);
  213. HFI1_SC2VL_ATTR(26);
  214. HFI1_SC2VL_ATTR(27);
  215. HFI1_SC2VL_ATTR(28);
  216. HFI1_SC2VL_ATTR(29);
  217. HFI1_SC2VL_ATTR(30);
  218. HFI1_SC2VL_ATTR(31);
  219. static struct attribute *sc2vl_default_attributes[] = {
  220. &hfi1_sc2vl_attr_0.attr,
  221. &hfi1_sc2vl_attr_1.attr,
  222. &hfi1_sc2vl_attr_2.attr,
  223. &hfi1_sc2vl_attr_3.attr,
  224. &hfi1_sc2vl_attr_4.attr,
  225. &hfi1_sc2vl_attr_5.attr,
  226. &hfi1_sc2vl_attr_6.attr,
  227. &hfi1_sc2vl_attr_7.attr,
  228. &hfi1_sc2vl_attr_8.attr,
  229. &hfi1_sc2vl_attr_9.attr,
  230. &hfi1_sc2vl_attr_10.attr,
  231. &hfi1_sc2vl_attr_11.attr,
  232. &hfi1_sc2vl_attr_12.attr,
  233. &hfi1_sc2vl_attr_13.attr,
  234. &hfi1_sc2vl_attr_14.attr,
  235. &hfi1_sc2vl_attr_15.attr,
  236. &hfi1_sc2vl_attr_16.attr,
  237. &hfi1_sc2vl_attr_17.attr,
  238. &hfi1_sc2vl_attr_18.attr,
  239. &hfi1_sc2vl_attr_19.attr,
  240. &hfi1_sc2vl_attr_20.attr,
  241. &hfi1_sc2vl_attr_21.attr,
  242. &hfi1_sc2vl_attr_22.attr,
  243. &hfi1_sc2vl_attr_23.attr,
  244. &hfi1_sc2vl_attr_24.attr,
  245. &hfi1_sc2vl_attr_25.attr,
  246. &hfi1_sc2vl_attr_26.attr,
  247. &hfi1_sc2vl_attr_27.attr,
  248. &hfi1_sc2vl_attr_28.attr,
  249. &hfi1_sc2vl_attr_29.attr,
  250. &hfi1_sc2vl_attr_30.attr,
  251. &hfi1_sc2vl_attr_31.attr,
  252. NULL
  253. };
  254. static ssize_t sc2vl_attr_show(struct kobject *kobj, struct attribute *attr,
  255. char *buf)
  256. {
  257. struct hfi1_sc2vl_attr *sattr =
  258. container_of(attr, struct hfi1_sc2vl_attr, attr);
  259. struct hfi1_pportdata *ppd =
  260. container_of(kobj, struct hfi1_pportdata, sc2vl_kobj);
  261. struct hfi1_devdata *dd = ppd->dd;
  262. return sprintf(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
  263. }
  264. static const struct sysfs_ops hfi1_sc2vl_ops = {
  265. .show = sc2vl_attr_show,
  266. };
  267. static struct kobj_type hfi1_sc2vl_ktype = {
  268. .release = port_release,
  269. .sysfs_ops = &hfi1_sc2vl_ops,
  270. .default_attrs = sc2vl_default_attributes
  271. };
  272. /* End sc2vl */
  273. /* Start sl2sc */
  274. #define HFI1_SL2SC_ATTR(N) \
  275. static struct hfi1_sl2sc_attr hfi1_sl2sc_attr_##N = { \
  276. .attr = { .name = __stringify(N), .mode = 0444 }, \
  277. .sl = N \
  278. }
  279. struct hfi1_sl2sc_attr {
  280. struct attribute attr;
  281. int sl;
  282. };
  283. HFI1_SL2SC_ATTR(0);
  284. HFI1_SL2SC_ATTR(1);
  285. HFI1_SL2SC_ATTR(2);
  286. HFI1_SL2SC_ATTR(3);
  287. HFI1_SL2SC_ATTR(4);
  288. HFI1_SL2SC_ATTR(5);
  289. HFI1_SL2SC_ATTR(6);
  290. HFI1_SL2SC_ATTR(7);
  291. HFI1_SL2SC_ATTR(8);
  292. HFI1_SL2SC_ATTR(9);
  293. HFI1_SL2SC_ATTR(10);
  294. HFI1_SL2SC_ATTR(11);
  295. HFI1_SL2SC_ATTR(12);
  296. HFI1_SL2SC_ATTR(13);
  297. HFI1_SL2SC_ATTR(14);
  298. HFI1_SL2SC_ATTR(15);
  299. HFI1_SL2SC_ATTR(16);
  300. HFI1_SL2SC_ATTR(17);
  301. HFI1_SL2SC_ATTR(18);
  302. HFI1_SL2SC_ATTR(19);
  303. HFI1_SL2SC_ATTR(20);
  304. HFI1_SL2SC_ATTR(21);
  305. HFI1_SL2SC_ATTR(22);
  306. HFI1_SL2SC_ATTR(23);
  307. HFI1_SL2SC_ATTR(24);
  308. HFI1_SL2SC_ATTR(25);
  309. HFI1_SL2SC_ATTR(26);
  310. HFI1_SL2SC_ATTR(27);
  311. HFI1_SL2SC_ATTR(28);
  312. HFI1_SL2SC_ATTR(29);
  313. HFI1_SL2SC_ATTR(30);
  314. HFI1_SL2SC_ATTR(31);
  315. static struct attribute *sl2sc_default_attributes[] = {
  316. &hfi1_sl2sc_attr_0.attr,
  317. &hfi1_sl2sc_attr_1.attr,
  318. &hfi1_sl2sc_attr_2.attr,
  319. &hfi1_sl2sc_attr_3.attr,
  320. &hfi1_sl2sc_attr_4.attr,
  321. &hfi1_sl2sc_attr_5.attr,
  322. &hfi1_sl2sc_attr_6.attr,
  323. &hfi1_sl2sc_attr_7.attr,
  324. &hfi1_sl2sc_attr_8.attr,
  325. &hfi1_sl2sc_attr_9.attr,
  326. &hfi1_sl2sc_attr_10.attr,
  327. &hfi1_sl2sc_attr_11.attr,
  328. &hfi1_sl2sc_attr_12.attr,
  329. &hfi1_sl2sc_attr_13.attr,
  330. &hfi1_sl2sc_attr_14.attr,
  331. &hfi1_sl2sc_attr_15.attr,
  332. &hfi1_sl2sc_attr_16.attr,
  333. &hfi1_sl2sc_attr_17.attr,
  334. &hfi1_sl2sc_attr_18.attr,
  335. &hfi1_sl2sc_attr_19.attr,
  336. &hfi1_sl2sc_attr_20.attr,
  337. &hfi1_sl2sc_attr_21.attr,
  338. &hfi1_sl2sc_attr_22.attr,
  339. &hfi1_sl2sc_attr_23.attr,
  340. &hfi1_sl2sc_attr_24.attr,
  341. &hfi1_sl2sc_attr_25.attr,
  342. &hfi1_sl2sc_attr_26.attr,
  343. &hfi1_sl2sc_attr_27.attr,
  344. &hfi1_sl2sc_attr_28.attr,
  345. &hfi1_sl2sc_attr_29.attr,
  346. &hfi1_sl2sc_attr_30.attr,
  347. &hfi1_sl2sc_attr_31.attr,
  348. NULL
  349. };
  350. static ssize_t sl2sc_attr_show(struct kobject *kobj, struct attribute *attr,
  351. char *buf)
  352. {
  353. struct hfi1_sl2sc_attr *sattr =
  354. container_of(attr, struct hfi1_sl2sc_attr, attr);
  355. struct hfi1_pportdata *ppd =
  356. container_of(kobj, struct hfi1_pportdata, sl2sc_kobj);
  357. struct hfi1_ibport *ibp = &ppd->ibport_data;
  358. return sprintf(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
  359. }
  360. static const struct sysfs_ops hfi1_sl2sc_ops = {
  361. .show = sl2sc_attr_show,
  362. };
  363. static struct kobj_type hfi1_sl2sc_ktype = {
  364. .release = port_release,
  365. .sysfs_ops = &hfi1_sl2sc_ops,
  366. .default_attrs = sl2sc_default_attributes
  367. };
  368. /* End sl2sc */
  369. /* Start vl2mtu */
  370. #define HFI1_VL2MTU_ATTR(N) \
  371. static struct hfi1_vl2mtu_attr hfi1_vl2mtu_attr_##N = { \
  372. .attr = { .name = __stringify(N), .mode = 0444 }, \
  373. .vl = N \
  374. }
  375. struct hfi1_vl2mtu_attr {
  376. struct attribute attr;
  377. int vl;
  378. };
  379. HFI1_VL2MTU_ATTR(0);
  380. HFI1_VL2MTU_ATTR(1);
  381. HFI1_VL2MTU_ATTR(2);
  382. HFI1_VL2MTU_ATTR(3);
  383. HFI1_VL2MTU_ATTR(4);
  384. HFI1_VL2MTU_ATTR(5);
  385. HFI1_VL2MTU_ATTR(6);
  386. HFI1_VL2MTU_ATTR(7);
  387. HFI1_VL2MTU_ATTR(8);
  388. HFI1_VL2MTU_ATTR(9);
  389. HFI1_VL2MTU_ATTR(10);
  390. HFI1_VL2MTU_ATTR(11);
  391. HFI1_VL2MTU_ATTR(12);
  392. HFI1_VL2MTU_ATTR(13);
  393. HFI1_VL2MTU_ATTR(14);
  394. HFI1_VL2MTU_ATTR(15);
  395. static struct attribute *vl2mtu_default_attributes[] = {
  396. &hfi1_vl2mtu_attr_0.attr,
  397. &hfi1_vl2mtu_attr_1.attr,
  398. &hfi1_vl2mtu_attr_2.attr,
  399. &hfi1_vl2mtu_attr_3.attr,
  400. &hfi1_vl2mtu_attr_4.attr,
  401. &hfi1_vl2mtu_attr_5.attr,
  402. &hfi1_vl2mtu_attr_6.attr,
  403. &hfi1_vl2mtu_attr_7.attr,
  404. &hfi1_vl2mtu_attr_8.attr,
  405. &hfi1_vl2mtu_attr_9.attr,
  406. &hfi1_vl2mtu_attr_10.attr,
  407. &hfi1_vl2mtu_attr_11.attr,
  408. &hfi1_vl2mtu_attr_12.attr,
  409. &hfi1_vl2mtu_attr_13.attr,
  410. &hfi1_vl2mtu_attr_14.attr,
  411. &hfi1_vl2mtu_attr_15.attr,
  412. NULL
  413. };
  414. static ssize_t vl2mtu_attr_show(struct kobject *kobj, struct attribute *attr,
  415. char *buf)
  416. {
  417. struct hfi1_vl2mtu_attr *vlattr =
  418. container_of(attr, struct hfi1_vl2mtu_attr, attr);
  419. struct hfi1_pportdata *ppd =
  420. container_of(kobj, struct hfi1_pportdata, vl2mtu_kobj);
  421. struct hfi1_devdata *dd = ppd->dd;
  422. return sprintf(buf, "%u\n", dd->vld[vlattr->vl].mtu);
  423. }
  424. static const struct sysfs_ops hfi1_vl2mtu_ops = {
  425. .show = vl2mtu_attr_show,
  426. };
  427. static struct kobj_type hfi1_vl2mtu_ktype = {
  428. .release = port_release,
  429. .sysfs_ops = &hfi1_vl2mtu_ops,
  430. .default_attrs = vl2mtu_default_attributes
  431. };
  432. /* end of per-port file structures and support code */
  433. /*
  434. * Start of per-unit (or driver, in some cases, but replicated
  435. * per unit) functions (these get a device *)
  436. */
  437. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  438. char *buf)
  439. {
  440. struct hfi1_ibdev *dev =
  441. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  442. return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
  443. }
  444. static ssize_t show_hfi(struct device *device, struct device_attribute *attr,
  445. char *buf)
  446. {
  447. struct hfi1_ibdev *dev =
  448. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  449. struct hfi1_devdata *dd = dd_from_dev(dev);
  450. int ret;
  451. if (!dd->boardname)
  452. ret = -EINVAL;
  453. else
  454. ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
  455. return ret;
  456. }
  457. static ssize_t show_boardversion(struct device *device,
  458. struct device_attribute *attr, char *buf)
  459. {
  460. struct hfi1_ibdev *dev =
  461. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  462. struct hfi1_devdata *dd = dd_from_dev(dev);
  463. /* The string printed here is already newline-terminated. */
  464. return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
  465. }
  466. static ssize_t show_nctxts(struct device *device,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct hfi1_ibdev *dev =
  470. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  471. struct hfi1_devdata *dd = dd_from_dev(dev);
  472. /*
  473. * Return the smaller of send and receive contexts.
  474. * Normally, user level applications would require both a send
  475. * and a receive context, so returning the smaller of the two counts
  476. * give a more accurate picture of total contexts available.
  477. */
  478. return scnprintf(buf, PAGE_SIZE, "%u\n",
  479. min(dd->num_rcv_contexts - dd->first_user_ctxt,
  480. (u32)dd->sc_sizes[SC_USER].count));
  481. }
  482. static ssize_t show_nfreectxts(struct device *device,
  483. struct device_attribute *attr, char *buf)
  484. {
  485. struct hfi1_ibdev *dev =
  486. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  487. struct hfi1_devdata *dd = dd_from_dev(dev);
  488. /* Return the number of free user ports (contexts) available. */
  489. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->freectxts);
  490. }
  491. static ssize_t show_serial(struct device *device,
  492. struct device_attribute *attr, char *buf)
  493. {
  494. struct hfi1_ibdev *dev =
  495. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  496. struct hfi1_devdata *dd = dd_from_dev(dev);
  497. return scnprintf(buf, PAGE_SIZE, "%s", dd->serial);
  498. }
  499. static ssize_t store_chip_reset(struct device *device,
  500. struct device_attribute *attr, const char *buf,
  501. size_t count)
  502. {
  503. struct hfi1_ibdev *dev =
  504. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  505. struct hfi1_devdata *dd = dd_from_dev(dev);
  506. int ret;
  507. if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
  508. ret = -EINVAL;
  509. goto bail;
  510. }
  511. ret = hfi1_reset_device(dd->unit);
  512. bail:
  513. return ret < 0 ? ret : count;
  514. }
  515. /*
  516. * Convert the reported temperature from an integer (reported in
  517. * units of 0.25C) to a floating point number.
  518. */
  519. #define temp2str(temp, buf, size, idx) \
  520. scnprintf((buf) + (idx), (size) - (idx), "%u.%02u ", \
  521. ((temp) >> 2), ((temp) & 0x3) * 25)
  522. /*
  523. * Dump tempsense values, in decimal, to ease shell-scripts.
  524. */
  525. static ssize_t show_tempsense(struct device *device,
  526. struct device_attribute *attr, char *buf)
  527. {
  528. struct hfi1_ibdev *dev =
  529. container_of(device, struct hfi1_ibdev, rdi.ibdev.dev);
  530. struct hfi1_devdata *dd = dd_from_dev(dev);
  531. struct hfi1_temp temp;
  532. int ret;
  533. ret = hfi1_tempsense_rd(dd, &temp);
  534. if (!ret) {
  535. int idx = 0;
  536. idx += temp2str(temp.curr, buf, PAGE_SIZE, idx);
  537. idx += temp2str(temp.lo_lim, buf, PAGE_SIZE, idx);
  538. idx += temp2str(temp.hi_lim, buf, PAGE_SIZE, idx);
  539. idx += temp2str(temp.crit_lim, buf, PAGE_SIZE, idx);
  540. idx += scnprintf(buf + idx, PAGE_SIZE - idx,
  541. "%u %u %u\n", temp.triggers & 0x1,
  542. temp.triggers & 0x2, temp.triggers & 0x4);
  543. ret = idx;
  544. }
  545. return ret;
  546. }
  547. /*
  548. * end of per-unit (or driver, in some cases, but replicated
  549. * per unit) functions
  550. */
  551. /* start of per-unit file structures and support code */
  552. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  553. static DEVICE_ATTR(board_id, S_IRUGO, show_hfi, NULL);
  554. static DEVICE_ATTR(nctxts, S_IRUGO, show_nctxts, NULL);
  555. static DEVICE_ATTR(nfreectxts, S_IRUGO, show_nfreectxts, NULL);
  556. static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
  557. static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
  558. static DEVICE_ATTR(tempsense, S_IRUGO, show_tempsense, NULL);
  559. static DEVICE_ATTR(chip_reset, S_IWUSR, NULL, store_chip_reset);
  560. static struct device_attribute *hfi1_attributes[] = {
  561. &dev_attr_hw_rev,
  562. &dev_attr_board_id,
  563. &dev_attr_nctxts,
  564. &dev_attr_nfreectxts,
  565. &dev_attr_serial,
  566. &dev_attr_boardversion,
  567. &dev_attr_tempsense,
  568. &dev_attr_chip_reset,
  569. };
  570. int hfi1_create_port_files(struct ib_device *ibdev, u8 port_num,
  571. struct kobject *kobj)
  572. {
  573. struct hfi1_pportdata *ppd;
  574. struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
  575. int ret;
  576. if (!port_num || port_num > dd->num_pports) {
  577. dd_dev_err(dd,
  578. "Skipping infiniband class with invalid port %u\n",
  579. port_num);
  580. return -ENODEV;
  581. }
  582. ppd = &dd->pport[port_num - 1];
  583. ret = kobject_init_and_add(&ppd->sc2vl_kobj, &hfi1_sc2vl_ktype, kobj,
  584. "sc2vl");
  585. if (ret) {
  586. dd_dev_err(dd,
  587. "Skipping sc2vl sysfs info, (err %d) port %u\n",
  588. ret, port_num);
  589. goto bail;
  590. }
  591. kobject_uevent(&ppd->sc2vl_kobj, KOBJ_ADD);
  592. ret = kobject_init_and_add(&ppd->sl2sc_kobj, &hfi1_sl2sc_ktype, kobj,
  593. "sl2sc");
  594. if (ret) {
  595. dd_dev_err(dd,
  596. "Skipping sl2sc sysfs info, (err %d) port %u\n",
  597. ret, port_num);
  598. goto bail_sc2vl;
  599. }
  600. kobject_uevent(&ppd->sl2sc_kobj, KOBJ_ADD);
  601. ret = kobject_init_and_add(&ppd->vl2mtu_kobj, &hfi1_vl2mtu_ktype, kobj,
  602. "vl2mtu");
  603. if (ret) {
  604. dd_dev_err(dd,
  605. "Skipping vl2mtu sysfs info, (err %d) port %u\n",
  606. ret, port_num);
  607. goto bail_sl2sc;
  608. }
  609. kobject_uevent(&ppd->vl2mtu_kobj, KOBJ_ADD);
  610. ret = kobject_init_and_add(&ppd->pport_cc_kobj, &port_cc_ktype,
  611. kobj, "CCMgtA");
  612. if (ret) {
  613. dd_dev_err(dd,
  614. "Skipping Congestion Control sysfs info, (err %d) port %u\n",
  615. ret, port_num);
  616. goto bail_vl2mtu;
  617. }
  618. kobject_uevent(&ppd->pport_cc_kobj, KOBJ_ADD);
  619. ret = sysfs_create_bin_file(&ppd->pport_cc_kobj, &cc_setting_bin_attr);
  620. if (ret) {
  621. dd_dev_err(dd,
  622. "Skipping Congestion Control setting sysfs info, (err %d) port %u\n",
  623. ret, port_num);
  624. goto bail_cc;
  625. }
  626. ret = sysfs_create_bin_file(&ppd->pport_cc_kobj, &cc_table_bin_attr);
  627. if (ret) {
  628. dd_dev_err(dd,
  629. "Skipping Congestion Control table sysfs info, (err %d) port %u\n",
  630. ret, port_num);
  631. goto bail_cc_entry_bin;
  632. }
  633. dd_dev_info(dd,
  634. "Congestion Control Agent enabled for port %d\n",
  635. port_num);
  636. return 0;
  637. bail_cc_entry_bin:
  638. sysfs_remove_bin_file(&ppd->pport_cc_kobj,
  639. &cc_setting_bin_attr);
  640. bail_cc:
  641. kobject_put(&ppd->pport_cc_kobj);
  642. bail_vl2mtu:
  643. kobject_put(&ppd->vl2mtu_kobj);
  644. bail_sl2sc:
  645. kobject_put(&ppd->sl2sc_kobj);
  646. bail_sc2vl:
  647. kobject_put(&ppd->sc2vl_kobj);
  648. bail:
  649. return ret;
  650. }
  651. struct sde_attribute {
  652. struct attribute attr;
  653. ssize_t (*show)(struct sdma_engine *sde, char *buf);
  654. ssize_t (*store)(struct sdma_engine *sde, const char *buf, size_t cnt);
  655. };
  656. static ssize_t sde_show(struct kobject *kobj, struct attribute *attr, char *buf)
  657. {
  658. struct sde_attribute *sde_attr =
  659. container_of(attr, struct sde_attribute, attr);
  660. struct sdma_engine *sde =
  661. container_of(kobj, struct sdma_engine, kobj);
  662. if (!sde_attr->show)
  663. return -EINVAL;
  664. return sde_attr->show(sde, buf);
  665. }
  666. static ssize_t sde_store(struct kobject *kobj, struct attribute *attr,
  667. const char *buf, size_t count)
  668. {
  669. struct sde_attribute *sde_attr =
  670. container_of(attr, struct sde_attribute, attr);
  671. struct sdma_engine *sde =
  672. container_of(kobj, struct sdma_engine, kobj);
  673. if (!capable(CAP_SYS_ADMIN))
  674. return -EPERM;
  675. if (!sde_attr->store)
  676. return -EINVAL;
  677. return sde_attr->store(sde, buf, count);
  678. }
  679. static const struct sysfs_ops sde_sysfs_ops = {
  680. .show = sde_show,
  681. .store = sde_store,
  682. };
  683. static struct kobj_type sde_ktype = {
  684. .sysfs_ops = &sde_sysfs_ops,
  685. };
  686. #define SDE_ATTR(_name, _mode, _show, _store) \
  687. struct sde_attribute sde_attr_##_name = \
  688. __ATTR(_name, _mode, _show, _store)
  689. static ssize_t sde_show_cpu_to_sde_map(struct sdma_engine *sde, char *buf)
  690. {
  691. return sdma_get_cpu_to_sde_map(sde, buf);
  692. }
  693. static ssize_t sde_store_cpu_to_sde_map(struct sdma_engine *sde,
  694. const char *buf, size_t count)
  695. {
  696. return sdma_set_cpu_to_sde_map(sde, buf, count);
  697. }
  698. static ssize_t sde_show_vl(struct sdma_engine *sde, char *buf)
  699. {
  700. int vl;
  701. vl = sdma_engine_get_vl(sde);
  702. if (vl < 0)
  703. return vl;
  704. return snprintf(buf, PAGE_SIZE, "%d\n", vl);
  705. }
  706. static SDE_ATTR(cpu_list, S_IWUSR | S_IRUGO,
  707. sde_show_cpu_to_sde_map,
  708. sde_store_cpu_to_sde_map);
  709. static SDE_ATTR(vl, S_IRUGO, sde_show_vl, NULL);
  710. static struct sde_attribute *sde_attribs[] = {
  711. &sde_attr_cpu_list,
  712. &sde_attr_vl
  713. };
  714. /*
  715. * Register and create our files in /sys/class/infiniband.
  716. */
  717. int hfi1_verbs_register_sysfs(struct hfi1_devdata *dd)
  718. {
  719. struct ib_device *dev = &dd->verbs_dev.rdi.ibdev;
  720. struct device *class_dev = &dev->dev;
  721. int i, j, ret;
  722. for (i = 0; i < ARRAY_SIZE(hfi1_attributes); ++i) {
  723. ret = device_create_file(&dev->dev, hfi1_attributes[i]);
  724. if (ret)
  725. goto bail;
  726. }
  727. for (i = 0; i < dd->num_sdma; i++) {
  728. ret = kobject_init_and_add(&dd->per_sdma[i].kobj,
  729. &sde_ktype, &class_dev->kobj,
  730. "sdma%d", i);
  731. if (ret)
  732. goto bail;
  733. for (j = 0; j < ARRAY_SIZE(sde_attribs); j++) {
  734. ret = sysfs_create_file(&dd->per_sdma[i].kobj,
  735. &sde_attribs[j]->attr);
  736. if (ret)
  737. goto bail;
  738. }
  739. }
  740. return 0;
  741. bail:
  742. for (i = 0; i < ARRAY_SIZE(hfi1_attributes); ++i)
  743. device_remove_file(&dev->dev, hfi1_attributes[i]);
  744. for (i = 0; i < dd->num_sdma; i++)
  745. kobject_del(&dd->per_sdma[i].kobj);
  746. return ret;
  747. }
  748. /*
  749. * Unregister and remove our files in /sys/class/infiniband.
  750. */
  751. void hfi1_verbs_unregister_sysfs(struct hfi1_devdata *dd)
  752. {
  753. struct hfi1_pportdata *ppd;
  754. int i;
  755. for (i = 0; i < dd->num_pports; i++) {
  756. ppd = &dd->pport[i];
  757. sysfs_remove_bin_file(&ppd->pport_cc_kobj,
  758. &cc_setting_bin_attr);
  759. sysfs_remove_bin_file(&ppd->pport_cc_kobj,
  760. &cc_table_bin_attr);
  761. kobject_put(&ppd->pport_cc_kobj);
  762. kobject_put(&ppd->vl2mtu_kobj);
  763. kobject_put(&ppd->sl2sc_kobj);
  764. kobject_put(&ppd->sc2vl_kobj);
  765. }
  766. }