ipath_sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. All rights reserved.
  3. * Copyright (c) 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/ctype.h>
  34. #include <linux/pci.h>
  35. #include "ipath_kernel.h"
  36. #include "ips_common.h"
  37. #include "ipath_layer.h"
  38. /**
  39. * ipath_parse_ushort - parse an unsigned short value in an arbitrary base
  40. * @str: the string containing the number
  41. * @valp: where to put the result
  42. *
  43. * returns the number of bytes consumed, or negative value on error
  44. */
  45. int ipath_parse_ushort(const char *str, unsigned short *valp)
  46. {
  47. unsigned long val;
  48. char *end;
  49. int ret;
  50. if (!isdigit(str[0])) {
  51. ret = -EINVAL;
  52. goto bail;
  53. }
  54. val = simple_strtoul(str, &end, 0);
  55. if (val > 0xffff) {
  56. ret = -EINVAL;
  57. goto bail;
  58. }
  59. *valp = val;
  60. ret = end + 1 - str;
  61. if (ret == 0)
  62. ret = -EINVAL;
  63. bail:
  64. return ret;
  65. }
  66. static ssize_t show_version(struct device_driver *dev, char *buf)
  67. {
  68. /* The string printed here is already newline-terminated. */
  69. return scnprintf(buf, PAGE_SIZE, "%s", ipath_core_version);
  70. }
  71. static ssize_t show_num_units(struct device_driver *dev, char *buf)
  72. {
  73. return scnprintf(buf, PAGE_SIZE, "%d\n",
  74. ipath_count_units(NULL, NULL, NULL));
  75. }
  76. #define DRIVER_STAT(name, attr) \
  77. static ssize_t show_stat_##name(struct device_driver *dev, \
  78. char *buf) \
  79. { \
  80. return scnprintf( \
  81. buf, PAGE_SIZE, "%llu\n", \
  82. (unsigned long long) ipath_stats.sps_ ##attr); \
  83. } \
  84. static DRIVER_ATTR(name, S_IRUGO, show_stat_##name, NULL)
  85. DRIVER_STAT(intrs, ints);
  86. DRIVER_STAT(err_intrs, errints);
  87. DRIVER_STAT(errs, errs);
  88. DRIVER_STAT(pkt_errs, pkterrs);
  89. DRIVER_STAT(crc_errs, crcerrs);
  90. DRIVER_STAT(hw_errs, hwerrs);
  91. DRIVER_STAT(ib_link, iblink);
  92. DRIVER_STAT(port0_pkts, port0pkts);
  93. DRIVER_STAT(ether_spkts, ether_spkts);
  94. DRIVER_STAT(ether_rpkts, ether_rpkts);
  95. DRIVER_STAT(sma_spkts, sma_spkts);
  96. DRIVER_STAT(sma_rpkts, sma_rpkts);
  97. DRIVER_STAT(hdrq_full, hdrqfull);
  98. DRIVER_STAT(etid_full, etidfull);
  99. DRIVER_STAT(no_piobufs, nopiobufs);
  100. DRIVER_STAT(ports, ports);
  101. DRIVER_STAT(pkey0, pkeys[0]);
  102. DRIVER_STAT(pkey1, pkeys[1]);
  103. DRIVER_STAT(pkey2, pkeys[2]);
  104. DRIVER_STAT(pkey3, pkeys[3]);
  105. DRIVER_STAT(nports, nports);
  106. DRIVER_STAT(null_intr, nullintr);
  107. DRIVER_STAT(max_pkts_call, maxpkts_call);
  108. DRIVER_STAT(avg_pkts_call, avgpkts_call);
  109. DRIVER_STAT(page_locks, pagelocks);
  110. DRIVER_STAT(page_unlocks, pageunlocks);
  111. DRIVER_STAT(krdrops, krdrops);
  112. static struct attribute *driver_stat_attributes[] = {
  113. &driver_attr_intrs.attr,
  114. &driver_attr_err_intrs.attr,
  115. &driver_attr_errs.attr,
  116. &driver_attr_pkt_errs.attr,
  117. &driver_attr_crc_errs.attr,
  118. &driver_attr_hw_errs.attr,
  119. &driver_attr_ib_link.attr,
  120. &driver_attr_port0_pkts.attr,
  121. &driver_attr_ether_spkts.attr,
  122. &driver_attr_ether_rpkts.attr,
  123. &driver_attr_sma_spkts.attr,
  124. &driver_attr_sma_rpkts.attr,
  125. &driver_attr_hdrq_full.attr,
  126. &driver_attr_etid_full.attr,
  127. &driver_attr_no_piobufs.attr,
  128. &driver_attr_ports.attr,
  129. &driver_attr_pkey0.attr,
  130. &driver_attr_pkey1.attr,
  131. &driver_attr_pkey2.attr,
  132. &driver_attr_pkey3.attr,
  133. &driver_attr_nports.attr,
  134. &driver_attr_null_intr.attr,
  135. &driver_attr_max_pkts_call.attr,
  136. &driver_attr_avg_pkts_call.attr,
  137. &driver_attr_page_locks.attr,
  138. &driver_attr_page_unlocks.attr,
  139. &driver_attr_krdrops.attr,
  140. NULL
  141. };
  142. static struct attribute_group driver_stat_attr_group = {
  143. .name = "stats",
  144. .attrs = driver_stat_attributes
  145. };
  146. static ssize_t show_status(struct device *dev,
  147. struct device_attribute *attr,
  148. char *buf)
  149. {
  150. struct ipath_devdata *dd = dev_get_drvdata(dev);
  151. ssize_t ret;
  152. if (!dd->ipath_statusp) {
  153. ret = -EINVAL;
  154. goto bail;
  155. }
  156. ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
  157. (unsigned long long) *(dd->ipath_statusp));
  158. bail:
  159. return ret;
  160. }
  161. static const char *ipath_status_str[] = {
  162. "Initted",
  163. "Disabled",
  164. "Admin_Disabled",
  165. "OIB_SMA",
  166. "SMA",
  167. "Present",
  168. "IB_link_up",
  169. "IB_configured",
  170. "NoIBcable",
  171. "Fatal_Hardware_Error",
  172. NULL,
  173. };
  174. static ssize_t show_status_str(struct device *dev,
  175. struct device_attribute *attr,
  176. char *buf)
  177. {
  178. struct ipath_devdata *dd = dev_get_drvdata(dev);
  179. int i, any;
  180. u64 s;
  181. ssize_t ret;
  182. if (!dd->ipath_statusp) {
  183. ret = -EINVAL;
  184. goto bail;
  185. }
  186. s = *(dd->ipath_statusp);
  187. *buf = '\0';
  188. for (any = i = 0; s && ipath_status_str[i]; i++) {
  189. if (s & 1) {
  190. if (any && strlcat(buf, " ", PAGE_SIZE) >=
  191. PAGE_SIZE)
  192. /* overflow */
  193. break;
  194. if (strlcat(buf, ipath_status_str[i],
  195. PAGE_SIZE) >= PAGE_SIZE)
  196. break;
  197. any = 1;
  198. }
  199. s >>= 1;
  200. }
  201. if (any)
  202. strlcat(buf, "\n", PAGE_SIZE);
  203. ret = strlen(buf);
  204. bail:
  205. return ret;
  206. }
  207. static ssize_t show_boardversion(struct device *dev,
  208. struct device_attribute *attr,
  209. char *buf)
  210. {
  211. struct ipath_devdata *dd = dev_get_drvdata(dev);
  212. /* The string printed here is already newline-terminated. */
  213. return scnprintf(buf, PAGE_SIZE, "%s", dd->ipath_boardversion);
  214. }
  215. static ssize_t show_lid(struct device *dev,
  216. struct device_attribute *attr,
  217. char *buf)
  218. {
  219. struct ipath_devdata *dd = dev_get_drvdata(dev);
  220. return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_lid);
  221. }
  222. static ssize_t store_lid(struct device *dev,
  223. struct device_attribute *attr,
  224. const char *buf,
  225. size_t count)
  226. {
  227. struct ipath_devdata *dd = dev_get_drvdata(dev);
  228. u16 lid = 0;
  229. int ret;
  230. ret = ipath_parse_ushort(buf, &lid);
  231. if (ret < 0)
  232. goto invalid;
  233. if (lid == 0 || lid >= IPS_MULTICAST_LID_BASE) {
  234. ret = -EINVAL;
  235. goto invalid;
  236. }
  237. ipath_set_lid(dd, lid, 0);
  238. goto bail;
  239. invalid:
  240. ipath_dev_err(dd, "attempt to set invalid LID 0x%x\n", lid);
  241. bail:
  242. return ret;
  243. }
  244. static ssize_t show_mlid(struct device *dev,
  245. struct device_attribute *attr,
  246. char *buf)
  247. {
  248. struct ipath_devdata *dd = dev_get_drvdata(dev);
  249. return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_mlid);
  250. }
  251. static ssize_t store_mlid(struct device *dev,
  252. struct device_attribute *attr,
  253. const char *buf,
  254. size_t count)
  255. {
  256. struct ipath_devdata *dd = dev_get_drvdata(dev);
  257. int unit;
  258. u16 mlid;
  259. int ret;
  260. ret = ipath_parse_ushort(buf, &mlid);
  261. if (ret < 0 || mlid < IPS_MULTICAST_LID_BASE)
  262. goto invalid;
  263. unit = dd->ipath_unit;
  264. dd->ipath_mlid = mlid;
  265. ipath_layer_intr(dd, IPATH_LAYER_INT_BCAST);
  266. goto bail;
  267. invalid:
  268. ipath_dev_err(dd, "attempt to set invalid MLID\n");
  269. bail:
  270. return ret;
  271. }
  272. static ssize_t show_guid(struct device *dev,
  273. struct device_attribute *attr,
  274. char *buf)
  275. {
  276. struct ipath_devdata *dd = dev_get_drvdata(dev);
  277. u8 *guid;
  278. guid = (u8 *) & (dd->ipath_guid);
  279. return scnprintf(buf, PAGE_SIZE,
  280. "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  281. guid[0], guid[1], guid[2], guid[3],
  282. guid[4], guid[5], guid[6], guid[7]);
  283. }
  284. static ssize_t store_guid(struct device *dev,
  285. struct device_attribute *attr,
  286. const char *buf,
  287. size_t count)
  288. {
  289. struct ipath_devdata *dd = dev_get_drvdata(dev);
  290. ssize_t ret;
  291. unsigned short guid[8];
  292. __be64 nguid;
  293. u8 *ng;
  294. int i;
  295. if (sscanf(buf, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
  296. &guid[0], &guid[1], &guid[2], &guid[3],
  297. &guid[4], &guid[5], &guid[6], &guid[7]) != 8)
  298. goto invalid;
  299. ng = (u8 *) &nguid;
  300. for (i = 0; i < 8; i++) {
  301. if (guid[i] > 0xff)
  302. goto invalid;
  303. ng[i] = guid[i];
  304. }
  305. dd->ipath_guid = nguid;
  306. dd->ipath_nguid = 1;
  307. ret = strlen(buf);
  308. goto bail;
  309. invalid:
  310. ipath_dev_err(dd, "attempt to set invalid GUID\n");
  311. ret = -EINVAL;
  312. bail:
  313. return ret;
  314. }
  315. static ssize_t show_nguid(struct device *dev,
  316. struct device_attribute *attr,
  317. char *buf)
  318. {
  319. struct ipath_devdata *dd = dev_get_drvdata(dev);
  320. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_nguid);
  321. }
  322. static ssize_t show_serial(struct device *dev,
  323. struct device_attribute *attr,
  324. char *buf)
  325. {
  326. struct ipath_devdata *dd = dev_get_drvdata(dev);
  327. buf[sizeof dd->ipath_serial] = '\0';
  328. memcpy(buf, dd->ipath_serial, sizeof dd->ipath_serial);
  329. strcat(buf, "\n");
  330. return strlen(buf);
  331. }
  332. static ssize_t show_unit(struct device *dev,
  333. struct device_attribute *attr,
  334. char *buf)
  335. {
  336. struct ipath_devdata *dd = dev_get_drvdata(dev);
  337. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_unit);
  338. }
  339. #define DEVICE_COUNTER(name, attr) \
  340. static ssize_t show_counter_##name(struct device *dev, \
  341. struct device_attribute *attr, \
  342. char *buf) \
  343. { \
  344. struct ipath_devdata *dd = dev_get_drvdata(dev); \
  345. return scnprintf(\
  346. buf, PAGE_SIZE, "%llu\n", (unsigned long long) \
  347. ipath_snap_cntr( \
  348. dd, offsetof(struct infinipath_counters, \
  349. attr) / sizeof(u64))); \
  350. } \
  351. static DEVICE_ATTR(name, S_IRUGO, show_counter_##name, NULL);
  352. DEVICE_COUNTER(ib_link_downeds, IBLinkDownedCnt);
  353. DEVICE_COUNTER(ib_link_err_recoveries, IBLinkErrRecoveryCnt);
  354. DEVICE_COUNTER(ib_status_changes, IBStatusChangeCnt);
  355. DEVICE_COUNTER(ib_symbol_errs, IBSymbolErrCnt);
  356. DEVICE_COUNTER(lb_flow_stalls, LBFlowStallCnt);
  357. DEVICE_COUNTER(lb_ints, LBIntCnt);
  358. DEVICE_COUNTER(rx_bad_formats, RxBadFormatCnt);
  359. DEVICE_COUNTER(rx_buf_ovfls, RxBufOvflCnt);
  360. DEVICE_COUNTER(rx_data_pkts, RxDataPktCnt);
  361. DEVICE_COUNTER(rx_dropped_pkts, RxDroppedPktCnt);
  362. DEVICE_COUNTER(rx_dwords, RxDwordCnt);
  363. DEVICE_COUNTER(rx_ebps, RxEBPCnt);
  364. DEVICE_COUNTER(rx_flow_ctrl_errs, RxFlowCtrlErrCnt);
  365. DEVICE_COUNTER(rx_flow_pkts, RxFlowPktCnt);
  366. DEVICE_COUNTER(rx_icrc_errs, RxICRCErrCnt);
  367. DEVICE_COUNTER(rx_len_errs, RxLenErrCnt);
  368. DEVICE_COUNTER(rx_link_problems, RxLinkProblemCnt);
  369. DEVICE_COUNTER(rx_lpcrc_errs, RxLPCRCErrCnt);
  370. DEVICE_COUNTER(rx_max_min_len_errs, RxMaxMinLenErrCnt);
  371. DEVICE_COUNTER(rx_p0_hdr_egr_ovfls, RxP0HdrEgrOvflCnt);
  372. DEVICE_COUNTER(rx_p1_hdr_egr_ovfls, RxP1HdrEgrOvflCnt);
  373. DEVICE_COUNTER(rx_p2_hdr_egr_ovfls, RxP2HdrEgrOvflCnt);
  374. DEVICE_COUNTER(rx_p3_hdr_egr_ovfls, RxP3HdrEgrOvflCnt);
  375. DEVICE_COUNTER(rx_p4_hdr_egr_ovfls, RxP4HdrEgrOvflCnt);
  376. DEVICE_COUNTER(rx_p5_hdr_egr_ovfls, RxP5HdrEgrOvflCnt);
  377. DEVICE_COUNTER(rx_p6_hdr_egr_ovfls, RxP6HdrEgrOvflCnt);
  378. DEVICE_COUNTER(rx_p7_hdr_egr_ovfls, RxP7HdrEgrOvflCnt);
  379. DEVICE_COUNTER(rx_p8_hdr_egr_ovfls, RxP8HdrEgrOvflCnt);
  380. DEVICE_COUNTER(rx_pkey_mismatches, RxPKeyMismatchCnt);
  381. DEVICE_COUNTER(rx_tid_full_errs, RxTIDFullErrCnt);
  382. DEVICE_COUNTER(rx_tid_valid_errs, RxTIDValidErrCnt);
  383. DEVICE_COUNTER(rx_vcrc_errs, RxVCRCErrCnt);
  384. DEVICE_COUNTER(tx_data_pkts, TxDataPktCnt);
  385. DEVICE_COUNTER(tx_dropped_pkts, TxDroppedPktCnt);
  386. DEVICE_COUNTER(tx_dwords, TxDwordCnt);
  387. DEVICE_COUNTER(tx_flow_pkts, TxFlowPktCnt);
  388. DEVICE_COUNTER(tx_flow_stalls, TxFlowStallCnt);
  389. DEVICE_COUNTER(tx_len_errs, TxLenErrCnt);
  390. DEVICE_COUNTER(tx_max_min_len_errs, TxMaxMinLenErrCnt);
  391. DEVICE_COUNTER(tx_underruns, TxUnderrunCnt);
  392. DEVICE_COUNTER(tx_unsup_vl_errs, TxUnsupVLErrCnt);
  393. static struct attribute *dev_counter_attributes[] = {
  394. &dev_attr_ib_link_downeds.attr,
  395. &dev_attr_ib_link_err_recoveries.attr,
  396. &dev_attr_ib_status_changes.attr,
  397. &dev_attr_ib_symbol_errs.attr,
  398. &dev_attr_lb_flow_stalls.attr,
  399. &dev_attr_lb_ints.attr,
  400. &dev_attr_rx_bad_formats.attr,
  401. &dev_attr_rx_buf_ovfls.attr,
  402. &dev_attr_rx_data_pkts.attr,
  403. &dev_attr_rx_dropped_pkts.attr,
  404. &dev_attr_rx_dwords.attr,
  405. &dev_attr_rx_ebps.attr,
  406. &dev_attr_rx_flow_ctrl_errs.attr,
  407. &dev_attr_rx_flow_pkts.attr,
  408. &dev_attr_rx_icrc_errs.attr,
  409. &dev_attr_rx_len_errs.attr,
  410. &dev_attr_rx_link_problems.attr,
  411. &dev_attr_rx_lpcrc_errs.attr,
  412. &dev_attr_rx_max_min_len_errs.attr,
  413. &dev_attr_rx_p0_hdr_egr_ovfls.attr,
  414. &dev_attr_rx_p1_hdr_egr_ovfls.attr,
  415. &dev_attr_rx_p2_hdr_egr_ovfls.attr,
  416. &dev_attr_rx_p3_hdr_egr_ovfls.attr,
  417. &dev_attr_rx_p4_hdr_egr_ovfls.attr,
  418. &dev_attr_rx_p5_hdr_egr_ovfls.attr,
  419. &dev_attr_rx_p6_hdr_egr_ovfls.attr,
  420. &dev_attr_rx_p7_hdr_egr_ovfls.attr,
  421. &dev_attr_rx_p8_hdr_egr_ovfls.attr,
  422. &dev_attr_rx_pkey_mismatches.attr,
  423. &dev_attr_rx_tid_full_errs.attr,
  424. &dev_attr_rx_tid_valid_errs.attr,
  425. &dev_attr_rx_vcrc_errs.attr,
  426. &dev_attr_tx_data_pkts.attr,
  427. &dev_attr_tx_dropped_pkts.attr,
  428. &dev_attr_tx_dwords.attr,
  429. &dev_attr_tx_flow_pkts.attr,
  430. &dev_attr_tx_flow_stalls.attr,
  431. &dev_attr_tx_len_errs.attr,
  432. &dev_attr_tx_max_min_len_errs.attr,
  433. &dev_attr_tx_underruns.attr,
  434. &dev_attr_tx_unsup_vl_errs.attr,
  435. NULL
  436. };
  437. static struct attribute_group dev_counter_attr_group = {
  438. .name = "counters",
  439. .attrs = dev_counter_attributes
  440. };
  441. static ssize_t store_reset(struct device *dev,
  442. struct device_attribute *attr,
  443. const char *buf,
  444. size_t count)
  445. {
  446. struct ipath_devdata *dd = dev_get_drvdata(dev);
  447. int ret;
  448. if (count < 5 || memcmp(buf, "reset", 5)) {
  449. ret = -EINVAL;
  450. goto bail;
  451. }
  452. if (dd->ipath_flags & IPATH_DISABLED) {
  453. /*
  454. * post-reset init would re-enable interrupts, etc.
  455. * so don't allow reset on disabled devices. Not
  456. * perfect error, but about the best choice.
  457. */
  458. dev_info(dev,"Unit %d is disabled, can't reset\n",
  459. dd->ipath_unit);
  460. ret = -EINVAL;
  461. }
  462. ret = ipath_reset_device(dd->ipath_unit);
  463. bail:
  464. return ret<0 ? ret : count;
  465. }
  466. static ssize_t store_link_state(struct device *dev,
  467. struct device_attribute *attr,
  468. const char *buf,
  469. size_t count)
  470. {
  471. struct ipath_devdata *dd = dev_get_drvdata(dev);
  472. int ret, r;
  473. u16 state;
  474. ret = ipath_parse_ushort(buf, &state);
  475. if (ret < 0)
  476. goto invalid;
  477. r = ipath_layer_set_linkstate(dd, state);
  478. if (r < 0) {
  479. ret = r;
  480. goto bail;
  481. }
  482. goto bail;
  483. invalid:
  484. ipath_dev_err(dd, "attempt to set invalid link state\n");
  485. bail:
  486. return ret;
  487. }
  488. static ssize_t show_mtu(struct device *dev,
  489. struct device_attribute *attr,
  490. char *buf)
  491. {
  492. struct ipath_devdata *dd = dev_get_drvdata(dev);
  493. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_ibmtu);
  494. }
  495. static ssize_t store_mtu(struct device *dev,
  496. struct device_attribute *attr,
  497. const char *buf,
  498. size_t count)
  499. {
  500. struct ipath_devdata *dd = dev_get_drvdata(dev);
  501. ssize_t ret;
  502. u16 mtu = 0;
  503. int r;
  504. ret = ipath_parse_ushort(buf, &mtu);
  505. if (ret < 0)
  506. goto invalid;
  507. r = ipath_layer_set_mtu(dd, mtu);
  508. if (r < 0)
  509. ret = r;
  510. goto bail;
  511. invalid:
  512. ipath_dev_err(dd, "attempt to set invalid MTU\n");
  513. bail:
  514. return ret;
  515. }
  516. static ssize_t show_enabled(struct device *dev,
  517. struct device_attribute *attr,
  518. char *buf)
  519. {
  520. struct ipath_devdata *dd = dev_get_drvdata(dev);
  521. return scnprintf(buf, PAGE_SIZE, "%u\n",
  522. (dd->ipath_flags & IPATH_DISABLED) ? 0 : 1);
  523. }
  524. static ssize_t store_enabled(struct device *dev,
  525. struct device_attribute *attr,
  526. const char *buf,
  527. size_t count)
  528. {
  529. struct ipath_devdata *dd = dev_get_drvdata(dev);
  530. ssize_t ret;
  531. u16 enable = 0;
  532. ret = ipath_parse_ushort(buf, &enable);
  533. if (ret < 0) {
  534. ipath_dev_err(dd, "attempt to use non-numeric on enable\n");
  535. goto bail;
  536. }
  537. if (enable) {
  538. if (!(dd->ipath_flags & IPATH_DISABLED))
  539. goto bail;
  540. dev_info(dev, "Enabling unit %d\n", dd->ipath_unit);
  541. /* same as post-reset */
  542. ret = ipath_init_chip(dd, 1);
  543. if (ret)
  544. ipath_dev_err(dd, "Failed to enable unit %d\n",
  545. dd->ipath_unit);
  546. else {
  547. dd->ipath_flags &= ~IPATH_DISABLED;
  548. *dd->ipath_statusp &= ~IPATH_STATUS_ADMIN_DISABLED;
  549. }
  550. }
  551. else if (!(dd->ipath_flags & IPATH_DISABLED)) {
  552. dev_info(dev, "Disabling unit %d\n", dd->ipath_unit);
  553. ipath_shutdown_device(dd);
  554. dd->ipath_flags |= IPATH_DISABLED;
  555. *dd->ipath_statusp |= IPATH_STATUS_ADMIN_DISABLED;
  556. }
  557. bail:
  558. return ret;
  559. }
  560. static DRIVER_ATTR(num_units, S_IRUGO, show_num_units, NULL);
  561. static DRIVER_ATTR(version, S_IRUGO, show_version, NULL);
  562. static struct attribute *driver_attributes[] = {
  563. &driver_attr_num_units.attr,
  564. &driver_attr_version.attr,
  565. NULL
  566. };
  567. static struct attribute_group driver_attr_group = {
  568. .attrs = driver_attributes
  569. };
  570. static DEVICE_ATTR(guid, S_IWUSR | S_IRUGO, show_guid, store_guid);
  571. static DEVICE_ATTR(lid, S_IWUSR | S_IRUGO, show_lid, store_lid);
  572. static DEVICE_ATTR(link_state, S_IWUSR, NULL, store_link_state);
  573. static DEVICE_ATTR(mlid, S_IWUSR | S_IRUGO, show_mlid, store_mlid);
  574. static DEVICE_ATTR(mtu, S_IWUSR | S_IRUGO, show_mtu, store_mtu);
  575. static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO, show_enabled, store_enabled);
  576. static DEVICE_ATTR(nguid, S_IRUGO, show_nguid, NULL);
  577. static DEVICE_ATTR(reset, S_IWUSR, NULL, store_reset);
  578. static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
  579. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  580. static DEVICE_ATTR(status_str, S_IRUGO, show_status_str, NULL);
  581. static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
  582. static DEVICE_ATTR(unit, S_IRUGO, show_unit, NULL);
  583. static struct attribute *dev_attributes[] = {
  584. &dev_attr_guid.attr,
  585. &dev_attr_lid.attr,
  586. &dev_attr_link_state.attr,
  587. &dev_attr_mlid.attr,
  588. &dev_attr_mtu.attr,
  589. &dev_attr_nguid.attr,
  590. &dev_attr_serial.attr,
  591. &dev_attr_status.attr,
  592. &dev_attr_status_str.attr,
  593. &dev_attr_boardversion.attr,
  594. &dev_attr_unit.attr,
  595. &dev_attr_enabled.attr,
  596. NULL
  597. };
  598. static struct attribute_group dev_attr_group = {
  599. .attrs = dev_attributes
  600. };
  601. /**
  602. * ipath_expose_reset - create a device reset file
  603. * @dev: the device structure
  604. *
  605. * Only expose a file that lets us reset the device after someone
  606. * enters diag mode. A device reset is quite likely to crash the
  607. * machine entirely, so we don't want to normally make it
  608. * available.
  609. *
  610. * Called with ipath_mutex held.
  611. */
  612. int ipath_expose_reset(struct device *dev)
  613. {
  614. static int exposed;
  615. int ret;
  616. if (!exposed) {
  617. ret = device_create_file(dev, &dev_attr_reset);
  618. exposed = 1;
  619. }
  620. else
  621. ret = 0;
  622. return ret;
  623. }
  624. int ipath_driver_create_group(struct device_driver *drv)
  625. {
  626. int ret;
  627. ret = sysfs_create_group(&drv->kobj, &driver_attr_group);
  628. if (ret)
  629. goto bail;
  630. ret = sysfs_create_group(&drv->kobj, &driver_stat_attr_group);
  631. if (ret)
  632. sysfs_remove_group(&drv->kobj, &driver_attr_group);
  633. bail:
  634. return ret;
  635. }
  636. void ipath_driver_remove_group(struct device_driver *drv)
  637. {
  638. sysfs_remove_group(&drv->kobj, &driver_stat_attr_group);
  639. sysfs_remove_group(&drv->kobj, &driver_attr_group);
  640. }
  641. int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd)
  642. {
  643. int ret;
  644. char unit[5];
  645. ret = sysfs_create_group(&dev->kobj, &dev_attr_group);
  646. if (ret)
  647. goto bail;
  648. ret = sysfs_create_group(&dev->kobj, &dev_counter_attr_group);
  649. if (ret)
  650. goto bail_attrs;
  651. snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
  652. ret = sysfs_create_link(&dev->driver->kobj, &dev->kobj, unit);
  653. if (ret == 0)
  654. goto bail;
  655. sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
  656. bail_attrs:
  657. sysfs_remove_group(&dev->kobj, &dev_attr_group);
  658. bail:
  659. return ret;
  660. }
  661. void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd)
  662. {
  663. char unit[5];
  664. snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
  665. sysfs_remove_link(&dev->driver->kobj, unit);
  666. sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
  667. sysfs_remove_group(&dev->kobj, &dev_attr_group);
  668. device_remove_file(dev, &dev_attr_reset);
  669. }