ipath_sysfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. /* XXX fix the following when dynamic table of devices used */
  106. DRIVER_STAT(lid0, lid[0]);
  107. DRIVER_STAT(lid1, lid[1]);
  108. DRIVER_STAT(lid2, lid[2]);
  109. DRIVER_STAT(lid3, lid[3]);
  110. DRIVER_STAT(nports, nports);
  111. DRIVER_STAT(null_intr, nullintr);
  112. DRIVER_STAT(max_pkts_call, maxpkts_call);
  113. DRIVER_STAT(avg_pkts_call, avgpkts_call);
  114. DRIVER_STAT(page_locks, pagelocks);
  115. DRIVER_STAT(page_unlocks, pageunlocks);
  116. DRIVER_STAT(krdrops, krdrops);
  117. /* XXX fix the following when dynamic table of devices used */
  118. DRIVER_STAT(mlid0, mlid[0]);
  119. DRIVER_STAT(mlid1, mlid[1]);
  120. DRIVER_STAT(mlid2, mlid[2]);
  121. DRIVER_STAT(mlid3, mlid[3]);
  122. static struct attribute *driver_stat_attributes[] = {
  123. &driver_attr_intrs.attr,
  124. &driver_attr_err_intrs.attr,
  125. &driver_attr_errs.attr,
  126. &driver_attr_pkt_errs.attr,
  127. &driver_attr_crc_errs.attr,
  128. &driver_attr_hw_errs.attr,
  129. &driver_attr_ib_link.attr,
  130. &driver_attr_port0_pkts.attr,
  131. &driver_attr_ether_spkts.attr,
  132. &driver_attr_ether_rpkts.attr,
  133. &driver_attr_sma_spkts.attr,
  134. &driver_attr_sma_rpkts.attr,
  135. &driver_attr_hdrq_full.attr,
  136. &driver_attr_etid_full.attr,
  137. &driver_attr_no_piobufs.attr,
  138. &driver_attr_ports.attr,
  139. &driver_attr_pkey0.attr,
  140. &driver_attr_pkey1.attr,
  141. &driver_attr_pkey2.attr,
  142. &driver_attr_pkey3.attr,
  143. &driver_attr_lid0.attr,
  144. &driver_attr_lid1.attr,
  145. &driver_attr_lid2.attr,
  146. &driver_attr_lid3.attr,
  147. &driver_attr_nports.attr,
  148. &driver_attr_null_intr.attr,
  149. &driver_attr_max_pkts_call.attr,
  150. &driver_attr_avg_pkts_call.attr,
  151. &driver_attr_page_locks.attr,
  152. &driver_attr_page_unlocks.attr,
  153. &driver_attr_krdrops.attr,
  154. &driver_attr_mlid0.attr,
  155. &driver_attr_mlid1.attr,
  156. &driver_attr_mlid2.attr,
  157. &driver_attr_mlid3.attr,
  158. NULL
  159. };
  160. static struct attribute_group driver_stat_attr_group = {
  161. .name = "stats",
  162. .attrs = driver_stat_attributes
  163. };
  164. static ssize_t show_status(struct device *dev,
  165. struct device_attribute *attr,
  166. char *buf)
  167. {
  168. struct ipath_devdata *dd = dev_get_drvdata(dev);
  169. ssize_t ret;
  170. if (!dd->ipath_statusp) {
  171. ret = -EINVAL;
  172. goto bail;
  173. }
  174. ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
  175. (unsigned long long) *(dd->ipath_statusp));
  176. bail:
  177. return ret;
  178. }
  179. static const char *ipath_status_str[] = {
  180. "Initted",
  181. "Disabled",
  182. "Admin_Disabled",
  183. "OIB_SMA",
  184. "SMA",
  185. "Present",
  186. "IB_link_up",
  187. "IB_configured",
  188. "NoIBcable",
  189. "Fatal_Hardware_Error",
  190. NULL,
  191. };
  192. static ssize_t show_status_str(struct device *dev,
  193. struct device_attribute *attr,
  194. char *buf)
  195. {
  196. struct ipath_devdata *dd = dev_get_drvdata(dev);
  197. int i, any;
  198. u64 s;
  199. ssize_t ret;
  200. if (!dd->ipath_statusp) {
  201. ret = -EINVAL;
  202. goto bail;
  203. }
  204. s = *(dd->ipath_statusp);
  205. *buf = '\0';
  206. for (any = i = 0; s && ipath_status_str[i]; i++) {
  207. if (s & 1) {
  208. if (any && strlcat(buf, " ", PAGE_SIZE) >=
  209. PAGE_SIZE)
  210. /* overflow */
  211. break;
  212. if (strlcat(buf, ipath_status_str[i],
  213. PAGE_SIZE) >= PAGE_SIZE)
  214. break;
  215. any = 1;
  216. }
  217. s >>= 1;
  218. }
  219. if (any)
  220. strlcat(buf, "\n", PAGE_SIZE);
  221. ret = strlen(buf);
  222. bail:
  223. return ret;
  224. }
  225. static ssize_t show_boardversion(struct device *dev,
  226. struct device_attribute *attr,
  227. char *buf)
  228. {
  229. struct ipath_devdata *dd = dev_get_drvdata(dev);
  230. /* The string printed here is already newline-terminated. */
  231. return scnprintf(buf, PAGE_SIZE, "%s", dd->ipath_boardversion);
  232. }
  233. static ssize_t show_lid(struct device *dev,
  234. struct device_attribute *attr,
  235. char *buf)
  236. {
  237. struct ipath_devdata *dd = dev_get_drvdata(dev);
  238. return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_lid);
  239. }
  240. static ssize_t store_lid(struct device *dev,
  241. struct device_attribute *attr,
  242. const char *buf,
  243. size_t count)
  244. {
  245. struct ipath_devdata *dd = dev_get_drvdata(dev);
  246. u16 lid;
  247. int ret;
  248. ret = ipath_parse_ushort(buf, &lid);
  249. if (ret < 0)
  250. goto invalid;
  251. if (lid == 0 || lid >= 0xc000) {
  252. ret = -EINVAL;
  253. goto invalid;
  254. }
  255. ipath_set_sps_lid(dd, lid, 0);
  256. goto bail;
  257. invalid:
  258. ipath_dev_err(dd, "attempt to set invalid LID\n");
  259. bail:
  260. return ret;
  261. }
  262. static ssize_t show_mlid(struct device *dev,
  263. struct device_attribute *attr,
  264. char *buf)
  265. {
  266. struct ipath_devdata *dd = dev_get_drvdata(dev);
  267. return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_mlid);
  268. }
  269. static ssize_t store_mlid(struct device *dev,
  270. struct device_attribute *attr,
  271. const char *buf,
  272. size_t count)
  273. {
  274. struct ipath_devdata *dd = dev_get_drvdata(dev);
  275. int unit;
  276. u16 mlid;
  277. int ret;
  278. ret = ipath_parse_ushort(buf, &mlid);
  279. if (ret < 0)
  280. goto invalid;
  281. unit = dd->ipath_unit;
  282. dd->ipath_mlid = mlid;
  283. ipath_stats.sps_mlid[unit] = mlid;
  284. ipath_layer_intr(dd, IPATH_LAYER_INT_BCAST);
  285. goto bail;
  286. invalid:
  287. ipath_dev_err(dd, "attempt to set invalid MLID\n");
  288. bail:
  289. return ret;
  290. }
  291. static ssize_t show_guid(struct device *dev,
  292. struct device_attribute *attr,
  293. char *buf)
  294. {
  295. struct ipath_devdata *dd = dev_get_drvdata(dev);
  296. u8 *guid;
  297. guid = (u8 *) & (dd->ipath_guid);
  298. return scnprintf(buf, PAGE_SIZE,
  299. "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  300. guid[0], guid[1], guid[2], guid[3],
  301. guid[4], guid[5], guid[6], guid[7]);
  302. }
  303. static ssize_t store_guid(struct device *dev,
  304. struct device_attribute *attr,
  305. const char *buf,
  306. size_t count)
  307. {
  308. struct ipath_devdata *dd = dev_get_drvdata(dev);
  309. ssize_t ret;
  310. unsigned short guid[8];
  311. __be64 nguid;
  312. u8 *ng;
  313. int i;
  314. if (sscanf(buf, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
  315. &guid[0], &guid[1], &guid[2], &guid[3],
  316. &guid[4], &guid[5], &guid[6], &guid[7]) != 8)
  317. goto invalid;
  318. ng = (u8 *) &nguid;
  319. for (i = 0; i < 8; i++) {
  320. if (guid[i] > 0xff)
  321. goto invalid;
  322. ng[i] = guid[i];
  323. }
  324. dd->ipath_guid = nguid;
  325. dd->ipath_nguid = 1;
  326. ret = strlen(buf);
  327. goto bail;
  328. invalid:
  329. ipath_dev_err(dd, "attempt to set invalid GUID\n");
  330. ret = -EINVAL;
  331. bail:
  332. return ret;
  333. }
  334. static ssize_t show_nguid(struct device *dev,
  335. struct device_attribute *attr,
  336. char *buf)
  337. {
  338. struct ipath_devdata *dd = dev_get_drvdata(dev);
  339. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_nguid);
  340. }
  341. static ssize_t show_serial(struct device *dev,
  342. struct device_attribute *attr,
  343. char *buf)
  344. {
  345. struct ipath_devdata *dd = dev_get_drvdata(dev);
  346. buf[sizeof dd->ipath_serial] = '\0';
  347. memcpy(buf, dd->ipath_serial, sizeof dd->ipath_serial);
  348. strcat(buf, "\n");
  349. return strlen(buf);
  350. }
  351. static ssize_t show_unit(struct device *dev,
  352. struct device_attribute *attr,
  353. char *buf)
  354. {
  355. struct ipath_devdata *dd = dev_get_drvdata(dev);
  356. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_unit);
  357. }
  358. #define DEVICE_COUNTER(name, attr) \
  359. static ssize_t show_counter_##name(struct device *dev, \
  360. struct device_attribute *attr, \
  361. char *buf) \
  362. { \
  363. struct ipath_devdata *dd = dev_get_drvdata(dev); \
  364. return scnprintf(\
  365. buf, PAGE_SIZE, "%llu\n", (unsigned long long) \
  366. ipath_snap_cntr( \
  367. dd, offsetof(struct infinipath_counters, \
  368. attr) / sizeof(u64))); \
  369. } \
  370. static DEVICE_ATTR(name, S_IRUGO, show_counter_##name, NULL);
  371. DEVICE_COUNTER(ib_link_downeds, IBLinkDownedCnt);
  372. DEVICE_COUNTER(ib_link_err_recoveries, IBLinkErrRecoveryCnt);
  373. DEVICE_COUNTER(ib_status_changes, IBStatusChangeCnt);
  374. DEVICE_COUNTER(ib_symbol_errs, IBSymbolErrCnt);
  375. DEVICE_COUNTER(lb_flow_stalls, LBFlowStallCnt);
  376. DEVICE_COUNTER(lb_ints, LBIntCnt);
  377. DEVICE_COUNTER(rx_bad_formats, RxBadFormatCnt);
  378. DEVICE_COUNTER(rx_buf_ovfls, RxBufOvflCnt);
  379. DEVICE_COUNTER(rx_data_pkts, RxDataPktCnt);
  380. DEVICE_COUNTER(rx_dropped_pkts, RxDroppedPktCnt);
  381. DEVICE_COUNTER(rx_dwords, RxDwordCnt);
  382. DEVICE_COUNTER(rx_ebps, RxEBPCnt);
  383. DEVICE_COUNTER(rx_flow_ctrl_errs, RxFlowCtrlErrCnt);
  384. DEVICE_COUNTER(rx_flow_pkts, RxFlowPktCnt);
  385. DEVICE_COUNTER(rx_icrc_errs, RxICRCErrCnt);
  386. DEVICE_COUNTER(rx_len_errs, RxLenErrCnt);
  387. DEVICE_COUNTER(rx_link_problems, RxLinkProblemCnt);
  388. DEVICE_COUNTER(rx_lpcrc_errs, RxLPCRCErrCnt);
  389. DEVICE_COUNTER(rx_max_min_len_errs, RxMaxMinLenErrCnt);
  390. DEVICE_COUNTER(rx_p0_hdr_egr_ovfls, RxP0HdrEgrOvflCnt);
  391. DEVICE_COUNTER(rx_p1_hdr_egr_ovfls, RxP1HdrEgrOvflCnt);
  392. DEVICE_COUNTER(rx_p2_hdr_egr_ovfls, RxP2HdrEgrOvflCnt);
  393. DEVICE_COUNTER(rx_p3_hdr_egr_ovfls, RxP3HdrEgrOvflCnt);
  394. DEVICE_COUNTER(rx_p4_hdr_egr_ovfls, RxP4HdrEgrOvflCnt);
  395. DEVICE_COUNTER(rx_p5_hdr_egr_ovfls, RxP5HdrEgrOvflCnt);
  396. DEVICE_COUNTER(rx_p6_hdr_egr_ovfls, RxP6HdrEgrOvflCnt);
  397. DEVICE_COUNTER(rx_p7_hdr_egr_ovfls, RxP7HdrEgrOvflCnt);
  398. DEVICE_COUNTER(rx_p8_hdr_egr_ovfls, RxP8HdrEgrOvflCnt);
  399. DEVICE_COUNTER(rx_pkey_mismatches, RxPKeyMismatchCnt);
  400. DEVICE_COUNTER(rx_tid_full_errs, RxTIDFullErrCnt);
  401. DEVICE_COUNTER(rx_tid_valid_errs, RxTIDValidErrCnt);
  402. DEVICE_COUNTER(rx_vcrc_errs, RxVCRCErrCnt);
  403. DEVICE_COUNTER(tx_data_pkts, TxDataPktCnt);
  404. DEVICE_COUNTER(tx_dropped_pkts, TxDroppedPktCnt);
  405. DEVICE_COUNTER(tx_dwords, TxDwordCnt);
  406. DEVICE_COUNTER(tx_flow_pkts, TxFlowPktCnt);
  407. DEVICE_COUNTER(tx_flow_stalls, TxFlowStallCnt);
  408. DEVICE_COUNTER(tx_len_errs, TxLenErrCnt);
  409. DEVICE_COUNTER(tx_max_min_len_errs, TxMaxMinLenErrCnt);
  410. DEVICE_COUNTER(tx_underruns, TxUnderrunCnt);
  411. DEVICE_COUNTER(tx_unsup_vl_errs, TxUnsupVLErrCnt);
  412. static struct attribute *dev_counter_attributes[] = {
  413. &dev_attr_ib_link_downeds.attr,
  414. &dev_attr_ib_link_err_recoveries.attr,
  415. &dev_attr_ib_status_changes.attr,
  416. &dev_attr_ib_symbol_errs.attr,
  417. &dev_attr_lb_flow_stalls.attr,
  418. &dev_attr_lb_ints.attr,
  419. &dev_attr_rx_bad_formats.attr,
  420. &dev_attr_rx_buf_ovfls.attr,
  421. &dev_attr_rx_data_pkts.attr,
  422. &dev_attr_rx_dropped_pkts.attr,
  423. &dev_attr_rx_dwords.attr,
  424. &dev_attr_rx_ebps.attr,
  425. &dev_attr_rx_flow_ctrl_errs.attr,
  426. &dev_attr_rx_flow_pkts.attr,
  427. &dev_attr_rx_icrc_errs.attr,
  428. &dev_attr_rx_len_errs.attr,
  429. &dev_attr_rx_link_problems.attr,
  430. &dev_attr_rx_lpcrc_errs.attr,
  431. &dev_attr_rx_max_min_len_errs.attr,
  432. &dev_attr_rx_p0_hdr_egr_ovfls.attr,
  433. &dev_attr_rx_p1_hdr_egr_ovfls.attr,
  434. &dev_attr_rx_p2_hdr_egr_ovfls.attr,
  435. &dev_attr_rx_p3_hdr_egr_ovfls.attr,
  436. &dev_attr_rx_p4_hdr_egr_ovfls.attr,
  437. &dev_attr_rx_p5_hdr_egr_ovfls.attr,
  438. &dev_attr_rx_p6_hdr_egr_ovfls.attr,
  439. &dev_attr_rx_p7_hdr_egr_ovfls.attr,
  440. &dev_attr_rx_p8_hdr_egr_ovfls.attr,
  441. &dev_attr_rx_pkey_mismatches.attr,
  442. &dev_attr_rx_tid_full_errs.attr,
  443. &dev_attr_rx_tid_valid_errs.attr,
  444. &dev_attr_rx_vcrc_errs.attr,
  445. &dev_attr_tx_data_pkts.attr,
  446. &dev_attr_tx_dropped_pkts.attr,
  447. &dev_attr_tx_dwords.attr,
  448. &dev_attr_tx_flow_pkts.attr,
  449. &dev_attr_tx_flow_stalls.attr,
  450. &dev_attr_tx_len_errs.attr,
  451. &dev_attr_tx_max_min_len_errs.attr,
  452. &dev_attr_tx_underruns.attr,
  453. &dev_attr_tx_unsup_vl_errs.attr,
  454. NULL
  455. };
  456. static struct attribute_group dev_counter_attr_group = {
  457. .name = "counters",
  458. .attrs = dev_counter_attributes
  459. };
  460. static ssize_t store_reset(struct device *dev,
  461. struct device_attribute *attr,
  462. const char *buf,
  463. size_t count)
  464. {
  465. struct ipath_devdata *dd = dev_get_drvdata(dev);
  466. int ret;
  467. if (count < 5 || memcmp(buf, "reset", 5)) {
  468. ret = -EINVAL;
  469. goto bail;
  470. }
  471. if (dd->ipath_flags & IPATH_DISABLED) {
  472. /*
  473. * post-reset init would re-enable interrupts, etc.
  474. * so don't allow reset on disabled devices. Not
  475. * perfect error, but about the best choice.
  476. */
  477. dev_info(dev,"Unit %d is disabled, can't reset\n",
  478. dd->ipath_unit);
  479. ret = -EINVAL;
  480. }
  481. ret = ipath_reset_device(dd->ipath_unit);
  482. bail:
  483. return ret<0 ? ret : count;
  484. }
  485. static ssize_t store_link_state(struct device *dev,
  486. struct device_attribute *attr,
  487. const char *buf,
  488. size_t count)
  489. {
  490. struct ipath_devdata *dd = dev_get_drvdata(dev);
  491. int ret, r;
  492. u16 state;
  493. ret = ipath_parse_ushort(buf, &state);
  494. if (ret < 0)
  495. goto invalid;
  496. r = ipath_layer_set_linkstate(dd, state);
  497. if (r < 0) {
  498. ret = r;
  499. goto bail;
  500. }
  501. goto bail;
  502. invalid:
  503. ipath_dev_err(dd, "attempt to set invalid link state\n");
  504. bail:
  505. return ret;
  506. }
  507. static ssize_t show_mtu(struct device *dev,
  508. struct device_attribute *attr,
  509. char *buf)
  510. {
  511. struct ipath_devdata *dd = dev_get_drvdata(dev);
  512. return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_ibmtu);
  513. }
  514. static ssize_t store_mtu(struct device *dev,
  515. struct device_attribute *attr,
  516. const char *buf,
  517. size_t count)
  518. {
  519. struct ipath_devdata *dd = dev_get_drvdata(dev);
  520. ssize_t ret;
  521. u16 mtu = 0;
  522. int r;
  523. ret = ipath_parse_ushort(buf, &mtu);
  524. if (ret < 0)
  525. goto invalid;
  526. r = ipath_layer_set_mtu(dd, mtu);
  527. if (r < 0)
  528. ret = r;
  529. goto bail;
  530. invalid:
  531. ipath_dev_err(dd, "attempt to set invalid MTU\n");
  532. bail:
  533. return ret;
  534. }
  535. static ssize_t show_enabled(struct device *dev,
  536. struct device_attribute *attr,
  537. char *buf)
  538. {
  539. struct ipath_devdata *dd = dev_get_drvdata(dev);
  540. return scnprintf(buf, PAGE_SIZE, "%u\n",
  541. (dd->ipath_flags & IPATH_DISABLED) ? 0 : 1);
  542. }
  543. static ssize_t store_enabled(struct device *dev,
  544. struct device_attribute *attr,
  545. const char *buf,
  546. size_t count)
  547. {
  548. struct ipath_devdata *dd = dev_get_drvdata(dev);
  549. ssize_t ret;
  550. u16 enable = 0;
  551. ret = ipath_parse_ushort(buf, &enable);
  552. if (ret < 0) {
  553. ipath_dev_err(dd, "attempt to use non-numeric on enable\n");
  554. goto bail;
  555. }
  556. if (enable) {
  557. if (!(dd->ipath_flags & IPATH_DISABLED))
  558. goto bail;
  559. dev_info(dev, "Enabling unit %d\n", dd->ipath_unit);
  560. /* same as post-reset */
  561. ret = ipath_init_chip(dd, 1);
  562. if (ret)
  563. ipath_dev_err(dd, "Failed to enable unit %d\n",
  564. dd->ipath_unit);
  565. else {
  566. dd->ipath_flags &= ~IPATH_DISABLED;
  567. *dd->ipath_statusp &= ~IPATH_STATUS_ADMIN_DISABLED;
  568. }
  569. }
  570. else if (!(dd->ipath_flags & IPATH_DISABLED)) {
  571. dev_info(dev, "Disabling unit %d\n", dd->ipath_unit);
  572. ipath_shutdown_device(dd);
  573. dd->ipath_flags |= IPATH_DISABLED;
  574. *dd->ipath_statusp |= IPATH_STATUS_ADMIN_DISABLED;
  575. }
  576. bail:
  577. return ret;
  578. }
  579. static DRIVER_ATTR(num_units, S_IRUGO, show_num_units, NULL);
  580. static DRIVER_ATTR(version, S_IRUGO, show_version, NULL);
  581. static struct attribute *driver_attributes[] = {
  582. &driver_attr_num_units.attr,
  583. &driver_attr_version.attr,
  584. NULL
  585. };
  586. static struct attribute_group driver_attr_group = {
  587. .attrs = driver_attributes
  588. };
  589. static DEVICE_ATTR(guid, S_IWUSR | S_IRUGO, show_guid, store_guid);
  590. static DEVICE_ATTR(lid, S_IWUSR | S_IRUGO, show_lid, store_lid);
  591. static DEVICE_ATTR(link_state, S_IWUSR, NULL, store_link_state);
  592. static DEVICE_ATTR(mlid, S_IWUSR | S_IRUGO, show_mlid, store_mlid);
  593. static DEVICE_ATTR(mtu, S_IWUSR | S_IRUGO, show_mtu, store_mtu);
  594. static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO, show_enabled, store_enabled);
  595. static DEVICE_ATTR(nguid, S_IRUGO, show_nguid, NULL);
  596. static DEVICE_ATTR(reset, S_IWUSR, NULL, store_reset);
  597. static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
  598. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  599. static DEVICE_ATTR(status_str, S_IRUGO, show_status_str, NULL);
  600. static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
  601. static DEVICE_ATTR(unit, S_IRUGO, show_unit, NULL);
  602. static struct attribute *dev_attributes[] = {
  603. &dev_attr_guid.attr,
  604. &dev_attr_lid.attr,
  605. &dev_attr_link_state.attr,
  606. &dev_attr_mlid.attr,
  607. &dev_attr_mtu.attr,
  608. &dev_attr_nguid.attr,
  609. &dev_attr_serial.attr,
  610. &dev_attr_status.attr,
  611. &dev_attr_status_str.attr,
  612. &dev_attr_boardversion.attr,
  613. &dev_attr_unit.attr,
  614. &dev_attr_enabled.attr,
  615. NULL
  616. };
  617. static struct attribute_group dev_attr_group = {
  618. .attrs = dev_attributes
  619. };
  620. /**
  621. * ipath_expose_reset - create a device reset file
  622. * @dev: the device structure
  623. *
  624. * Only expose a file that lets us reset the device after someone
  625. * enters diag mode. A device reset is quite likely to crash the
  626. * machine entirely, so we don't want to normally make it
  627. * available.
  628. *
  629. * Called with ipath_mutex held.
  630. */
  631. int ipath_expose_reset(struct device *dev)
  632. {
  633. static int exposed;
  634. int ret;
  635. if (!exposed) {
  636. ret = device_create_file(dev, &dev_attr_reset);
  637. exposed = 1;
  638. }
  639. else
  640. ret = 0;
  641. return ret;
  642. }
  643. int ipath_driver_create_group(struct device_driver *drv)
  644. {
  645. int ret;
  646. ret = sysfs_create_group(&drv->kobj, &driver_attr_group);
  647. if (ret)
  648. goto bail;
  649. ret = sysfs_create_group(&drv->kobj, &driver_stat_attr_group);
  650. if (ret)
  651. sysfs_remove_group(&drv->kobj, &driver_attr_group);
  652. bail:
  653. return ret;
  654. }
  655. void ipath_driver_remove_group(struct device_driver *drv)
  656. {
  657. sysfs_remove_group(&drv->kobj, &driver_stat_attr_group);
  658. sysfs_remove_group(&drv->kobj, &driver_attr_group);
  659. }
  660. int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd)
  661. {
  662. int ret;
  663. char unit[5];
  664. ret = sysfs_create_group(&dev->kobj, &dev_attr_group);
  665. if (ret)
  666. goto bail;
  667. ret = sysfs_create_group(&dev->kobj, &dev_counter_attr_group);
  668. if (ret)
  669. goto bail_attrs;
  670. snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
  671. ret = sysfs_create_link(&dev->driver->kobj, &dev->kobj, unit);
  672. if (ret == 0)
  673. goto bail;
  674. sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
  675. bail_attrs:
  676. sysfs_remove_group(&dev->kobj, &dev_attr_group);
  677. bail:
  678. return ret;
  679. }
  680. void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd)
  681. {
  682. char unit[5];
  683. snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
  684. sysfs_remove_link(&dev->driver->kobj, unit);
  685. sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
  686. sysfs_remove_group(&dev->kobj, &dev_attr_group);
  687. device_remove_file(dev, &dev_attr_reset);
  688. }