Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / drivers / infiniband / hw / ipath / ipath_sysfs.c
1 /*
2  * Copyright (c) 2006, 2007 QLogic Corporation. 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
34 #include <linux/ctype.h>
35
36 #include "ipath_kernel.h"
37 #include "ipath_common.h"
38
39 /**
40  * ipath_parse_ushort - parse an unsigned short value in an arbitrary base
41  * @str: the string containing the number
42  * @valp: where to put the result
43  *
44  * returns the number of bytes consumed, or negative value on error
45  */
46 int ipath_parse_ushort(const char *str, unsigned short *valp)
47 {
48         unsigned long val;
49         char *end;
50         int ret;
51
52         if (!isdigit(str[0])) {
53                 ret = -EINVAL;
54                 goto bail;
55         }
56
57         val = simple_strtoul(str, &end, 0);
58
59         if (val > 0xffff) {
60                 ret = -EINVAL;
61                 goto bail;
62         }
63
64         *valp = val;
65
66         ret = end + 1 - str;
67         if (ret == 0)
68                 ret = -EINVAL;
69
70 bail:
71         return ret;
72 }
73
74 static ssize_t show_version(struct device_driver *dev, char *buf)
75 {
76         /* The string printed here is already newline-terminated. */
77         return scnprintf(buf, PAGE_SIZE, "%s", ib_ipath_version);
78 }
79
80 static ssize_t show_num_units(struct device_driver *dev, char *buf)
81 {
82         return scnprintf(buf, PAGE_SIZE, "%d\n",
83                          ipath_count_units(NULL, NULL, NULL));
84 }
85
86 static ssize_t show_status(struct device *dev,
87                            struct device_attribute *attr,
88                            char *buf)
89 {
90         struct ipath_devdata *dd = dev_get_drvdata(dev);
91         ssize_t ret;
92
93         if (!dd->ipath_statusp) {
94                 ret = -EINVAL;
95                 goto bail;
96         }
97
98         ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
99                         (unsigned long long) *(dd->ipath_statusp));
100
101 bail:
102         return ret;
103 }
104
105 static const char *ipath_status_str[] = {
106         "Initted",
107         "Disabled",
108         "Admin_Disabled",
109         "", /* This used to be the old "OIB_SMA" status. */
110         "", /* This used to be the old "SMA" status. */
111         "Present",
112         "IB_link_up",
113         "IB_configured",
114         "NoIBcable",
115         "Fatal_Hardware_Error",
116         NULL,
117 };
118
119 static ssize_t show_status_str(struct device *dev,
120                                struct device_attribute *attr,
121                                char *buf)
122 {
123         struct ipath_devdata *dd = dev_get_drvdata(dev);
124         int i, any;
125         u64 s;
126         ssize_t ret;
127
128         if (!dd->ipath_statusp) {
129                 ret = -EINVAL;
130                 goto bail;
131         }
132
133         s = *(dd->ipath_statusp);
134         *buf = '\0';
135         for (any = i = 0; s && ipath_status_str[i]; i++) {
136                 if (s & 1) {
137                         if (any && strlcat(buf, " ", PAGE_SIZE) >=
138                             PAGE_SIZE)
139                                 /* overflow */
140                                 break;
141                         if (strlcat(buf, ipath_status_str[i],
142                                     PAGE_SIZE) >= PAGE_SIZE)
143                                 break;
144                         any = 1;
145                 }
146                 s >>= 1;
147         }
148         if (any)
149                 strlcat(buf, "\n", PAGE_SIZE);
150
151         ret = strlen(buf);
152
153 bail:
154         return ret;
155 }
156
157 static ssize_t show_boardversion(struct device *dev,
158                                struct device_attribute *attr,
159                                char *buf)
160 {
161         struct ipath_devdata *dd = dev_get_drvdata(dev);
162         /* The string printed here is already newline-terminated. */
163         return scnprintf(buf, PAGE_SIZE, "%s", dd->ipath_boardversion);
164 }
165
166 static ssize_t show_lmc(struct device *dev,
167                         struct device_attribute *attr,
168                         char *buf)
169 {
170         struct ipath_devdata *dd = dev_get_drvdata(dev);
171
172         return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_lmc);
173 }
174
175 static ssize_t store_lmc(struct device *dev,
176                          struct device_attribute *attr,
177                          const char *buf,
178                          size_t count)
179 {
180         struct ipath_devdata *dd = dev_get_drvdata(dev);
181         u16 lmc = 0;
182         int ret;
183
184         ret = ipath_parse_ushort(buf, &lmc);
185         if (ret < 0)
186                 goto invalid;
187
188         if (lmc > 7) {
189                 ret = -EINVAL;
190                 goto invalid;
191         }
192
193         ipath_set_lid(dd, dd->ipath_lid, lmc);
194
195         goto bail;
196 invalid:
197         ipath_dev_err(dd, "attempt to set invalid LMC %u\n", lmc);
198 bail:
199         return ret;
200 }
201
202 static ssize_t show_lid(struct device *dev,
203                         struct device_attribute *attr,
204                         char *buf)
205 {
206         struct ipath_devdata *dd = dev_get_drvdata(dev);
207
208         return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_lid);
209 }
210
211 static ssize_t store_lid(struct device *dev,
212                          struct device_attribute *attr,
213                           const char *buf,
214                           size_t count)
215 {
216         struct ipath_devdata *dd = dev_get_drvdata(dev);
217         u16 lid = 0;
218         int ret;
219
220         ret = ipath_parse_ushort(buf, &lid);
221         if (ret < 0)
222                 goto invalid;
223
224         if (lid == 0 || lid >= IPATH_MULTICAST_LID_BASE) {
225                 ret = -EINVAL;
226                 goto invalid;
227         }
228
229         ipath_set_lid(dd, lid, dd->ipath_lmc);
230
231         goto bail;
232 invalid:
233         ipath_dev_err(dd, "attempt to set invalid LID 0x%x\n", lid);
234 bail:
235         return ret;
236 }
237
238 static ssize_t show_mlid(struct device *dev,
239                          struct device_attribute *attr,
240                          char *buf)
241 {
242         struct ipath_devdata *dd = dev_get_drvdata(dev);
243
244         return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_mlid);
245 }
246
247 static ssize_t store_mlid(struct device *dev,
248                          struct device_attribute *attr,
249                           const char *buf,
250                           size_t count)
251 {
252         struct ipath_devdata *dd = dev_get_drvdata(dev);
253         u16 mlid;
254         int ret;
255
256         ret = ipath_parse_ushort(buf, &mlid);
257         if (ret < 0 || mlid < IPATH_MULTICAST_LID_BASE)
258                 goto invalid;
259
260         dd->ipath_mlid = mlid;
261
262         goto bail;
263 invalid:
264         ipath_dev_err(dd, "attempt to set invalid MLID\n");
265 bail:
266         return ret;
267 }
268
269 static ssize_t show_guid(struct device *dev,
270                          struct device_attribute *attr,
271                          char *buf)
272 {
273         struct ipath_devdata *dd = dev_get_drvdata(dev);
274         u8 *guid;
275
276         guid = (u8 *) & (dd->ipath_guid);
277
278         return scnprintf(buf, PAGE_SIZE,
279                          "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
280                          guid[0], guid[1], guid[2], guid[3],
281                          guid[4], guid[5], guid[6], guid[7]);
282 }
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 new_guid;
293         u8 *ng;
294         int i;
295
296         if (sscanf(buf, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
297                    &guid[0], &guid[1], &guid[2], &guid[3],
298                    &guid[4], &guid[5], &guid[6], &guid[7]) != 8)
299                 goto invalid;
300
301         ng = (u8 *) &new_guid;
302
303         for (i = 0; i < 8; i++) {
304                 if (guid[i] > 0xff)
305                         goto invalid;
306                 ng[i] = guid[i];
307         }
308
309         if (new_guid == 0)
310                 goto invalid;
311
312         dd->ipath_guid = new_guid;
313         dd->ipath_nguid = 1;
314
315         ret = strlen(buf);
316         goto bail;
317
318 invalid:
319         ipath_dev_err(dd, "attempt to set invalid GUID\n");
320         ret = -EINVAL;
321
322 bail:
323         return ret;
324 }
325
326 static ssize_t show_nguid(struct device *dev,
327                           struct device_attribute *attr,
328                           char *buf)
329 {
330         struct ipath_devdata *dd = dev_get_drvdata(dev);
331
332         return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_nguid);
333 }
334
335 static ssize_t show_nports(struct device *dev,
336                            struct device_attribute *attr,
337                            char *buf)
338 {
339         struct ipath_devdata *dd = dev_get_drvdata(dev);
340
341         /* Return the number of user ports available. */
342         return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_cfgports - 1);
343 }
344
345 static ssize_t show_serial(struct device *dev,
346                            struct device_attribute *attr,
347                            char *buf)
348 {
349         struct ipath_devdata *dd = dev_get_drvdata(dev);
350
351         buf[sizeof dd->ipath_serial] = '\0';
352         memcpy(buf, dd->ipath_serial, sizeof dd->ipath_serial);
353         strcat(buf, "\n");
354         return strlen(buf);
355 }
356
357 static ssize_t show_unit(struct device *dev,
358                          struct device_attribute *attr,
359                          char *buf)
360 {
361         struct ipath_devdata *dd = dev_get_drvdata(dev);
362
363         return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_unit);
364 }
365
366 static ssize_t show_jint_max_packets(struct device *dev,
367                                      struct device_attribute *attr,
368                                      char *buf)
369 {
370         struct ipath_devdata *dd = dev_get_drvdata(dev);
371
372         return scnprintf(buf, PAGE_SIZE, "%hu\n", dd->ipath_jint_max_packets);
373 }
374
375 static ssize_t store_jint_max_packets(struct device *dev,
376                                       struct device_attribute *attr,
377                                       const char *buf,
378                                       size_t count)
379 {
380         struct ipath_devdata *dd = dev_get_drvdata(dev);
381         u16 v = 0;
382         int ret;
383
384         ret = ipath_parse_ushort(buf, &v);
385         if (ret < 0)
386                 ipath_dev_err(dd, "invalid jint_max_packets.\n");
387         else
388                 dd->ipath_f_config_jint(dd, dd->ipath_jint_idle_ticks, v);
389
390         return ret;
391 }
392
393 static ssize_t show_jint_idle_ticks(struct device *dev,
394                                     struct device_attribute *attr,
395                                     char *buf)
396 {
397         struct ipath_devdata *dd = dev_get_drvdata(dev);
398
399         return scnprintf(buf, PAGE_SIZE, "%hu\n", dd->ipath_jint_idle_ticks);
400 }
401
402 static ssize_t store_jint_idle_ticks(struct device *dev,
403                                      struct device_attribute *attr,
404                                      const char *buf,
405                                      size_t count)
406 {
407         struct ipath_devdata *dd = dev_get_drvdata(dev);
408         u16 v = 0;
409         int ret;
410
411         ret = ipath_parse_ushort(buf, &v);
412         if (ret < 0)
413                 ipath_dev_err(dd, "invalid jint_idle_ticks.\n");
414         else
415                 dd->ipath_f_config_jint(dd, v, dd->ipath_jint_max_packets);
416
417         return ret;
418 }
419
420 #define DEVICE_COUNTER(name, attr) \
421         static ssize_t show_counter_##name(struct device *dev, \
422                                            struct device_attribute *attr, \
423                                            char *buf) \
424         { \
425                 struct ipath_devdata *dd = dev_get_drvdata(dev); \
426                 return scnprintf(\
427                         buf, PAGE_SIZE, "%llu\n", (unsigned long long) \
428                         ipath_snap_cntr( \
429                                 dd, offsetof(struct infinipath_counters, \
430                                              attr) / sizeof(u64)));     \
431         } \
432         static DEVICE_ATTR(name, S_IRUGO, show_counter_##name, NULL);
433
434 DEVICE_COUNTER(ib_link_downeds, IBLinkDownedCnt);
435 DEVICE_COUNTER(ib_link_err_recoveries, IBLinkErrRecoveryCnt);
436 DEVICE_COUNTER(ib_status_changes, IBStatusChangeCnt);
437 DEVICE_COUNTER(ib_symbol_errs, IBSymbolErrCnt);
438 DEVICE_COUNTER(lb_flow_stalls, LBFlowStallCnt);
439 DEVICE_COUNTER(lb_ints, LBIntCnt);
440 DEVICE_COUNTER(rx_bad_formats, RxBadFormatCnt);
441 DEVICE_COUNTER(rx_buf_ovfls, RxBufOvflCnt);
442 DEVICE_COUNTER(rx_data_pkts, RxDataPktCnt);
443 DEVICE_COUNTER(rx_dropped_pkts, RxDroppedPktCnt);
444 DEVICE_COUNTER(rx_dwords, RxDwordCnt);
445 DEVICE_COUNTER(rx_ebps, RxEBPCnt);
446 DEVICE_COUNTER(rx_flow_ctrl_errs, RxFlowCtrlErrCnt);
447 DEVICE_COUNTER(rx_flow_pkts, RxFlowPktCnt);
448 DEVICE_COUNTER(rx_icrc_errs, RxICRCErrCnt);
449 DEVICE_COUNTER(rx_len_errs, RxLenErrCnt);
450 DEVICE_COUNTER(rx_link_problems, RxLinkProblemCnt);
451 DEVICE_COUNTER(rx_lpcrc_errs, RxLPCRCErrCnt);
452 DEVICE_COUNTER(rx_max_min_len_errs, RxMaxMinLenErrCnt);
453 DEVICE_COUNTER(rx_p0_hdr_egr_ovfls, RxP0HdrEgrOvflCnt);
454 DEVICE_COUNTER(rx_p1_hdr_egr_ovfls, RxP1HdrEgrOvflCnt);
455 DEVICE_COUNTER(rx_p2_hdr_egr_ovfls, RxP2HdrEgrOvflCnt);
456 DEVICE_COUNTER(rx_p3_hdr_egr_ovfls, RxP3HdrEgrOvflCnt);
457 DEVICE_COUNTER(rx_p4_hdr_egr_ovfls, RxP4HdrEgrOvflCnt);
458 DEVICE_COUNTER(rx_p5_hdr_egr_ovfls, RxP5HdrEgrOvflCnt);
459 DEVICE_COUNTER(rx_p6_hdr_egr_ovfls, RxP6HdrEgrOvflCnt);
460 DEVICE_COUNTER(rx_p7_hdr_egr_ovfls, RxP7HdrEgrOvflCnt);
461 DEVICE_COUNTER(rx_p8_hdr_egr_ovfls, RxP8HdrEgrOvflCnt);
462 DEVICE_COUNTER(rx_pkey_mismatches, RxPKeyMismatchCnt);
463 DEVICE_COUNTER(rx_tid_full_errs, RxTIDFullErrCnt);
464 DEVICE_COUNTER(rx_tid_valid_errs, RxTIDValidErrCnt);
465 DEVICE_COUNTER(rx_vcrc_errs, RxVCRCErrCnt);
466 DEVICE_COUNTER(tx_data_pkts, TxDataPktCnt);
467 DEVICE_COUNTER(tx_dropped_pkts, TxDroppedPktCnt);
468 DEVICE_COUNTER(tx_dwords, TxDwordCnt);
469 DEVICE_COUNTER(tx_flow_pkts, TxFlowPktCnt);
470 DEVICE_COUNTER(tx_flow_stalls, TxFlowStallCnt);
471 DEVICE_COUNTER(tx_len_errs, TxLenErrCnt);
472 DEVICE_COUNTER(tx_max_min_len_errs, TxMaxMinLenErrCnt);
473 DEVICE_COUNTER(tx_underruns, TxUnderrunCnt);
474 DEVICE_COUNTER(tx_unsup_vl_errs, TxUnsupVLErrCnt);
475
476 static struct attribute *dev_counter_attributes[] = {
477         &dev_attr_ib_link_downeds.attr,
478         &dev_attr_ib_link_err_recoveries.attr,
479         &dev_attr_ib_status_changes.attr,
480         &dev_attr_ib_symbol_errs.attr,
481         &dev_attr_lb_flow_stalls.attr,
482         &dev_attr_lb_ints.attr,
483         &dev_attr_rx_bad_formats.attr,
484         &dev_attr_rx_buf_ovfls.attr,
485         &dev_attr_rx_data_pkts.attr,
486         &dev_attr_rx_dropped_pkts.attr,
487         &dev_attr_rx_dwords.attr,
488         &dev_attr_rx_ebps.attr,
489         &dev_attr_rx_flow_ctrl_errs.attr,
490         &dev_attr_rx_flow_pkts.attr,
491         &dev_attr_rx_icrc_errs.attr,
492         &dev_attr_rx_len_errs.attr,
493         &dev_attr_rx_link_problems.attr,
494         &dev_attr_rx_lpcrc_errs.attr,
495         &dev_attr_rx_max_min_len_errs.attr,
496         &dev_attr_rx_p0_hdr_egr_ovfls.attr,
497         &dev_attr_rx_p1_hdr_egr_ovfls.attr,
498         &dev_attr_rx_p2_hdr_egr_ovfls.attr,
499         &dev_attr_rx_p3_hdr_egr_ovfls.attr,
500         &dev_attr_rx_p4_hdr_egr_ovfls.attr,
501         &dev_attr_rx_p5_hdr_egr_ovfls.attr,
502         &dev_attr_rx_p6_hdr_egr_ovfls.attr,
503         &dev_attr_rx_p7_hdr_egr_ovfls.attr,
504         &dev_attr_rx_p8_hdr_egr_ovfls.attr,
505         &dev_attr_rx_pkey_mismatches.attr,
506         &dev_attr_rx_tid_full_errs.attr,
507         &dev_attr_rx_tid_valid_errs.attr,
508         &dev_attr_rx_vcrc_errs.attr,
509         &dev_attr_tx_data_pkts.attr,
510         &dev_attr_tx_dropped_pkts.attr,
511         &dev_attr_tx_dwords.attr,
512         &dev_attr_tx_flow_pkts.attr,
513         &dev_attr_tx_flow_stalls.attr,
514         &dev_attr_tx_len_errs.attr,
515         &dev_attr_tx_max_min_len_errs.attr,
516         &dev_attr_tx_underruns.attr,
517         &dev_attr_tx_unsup_vl_errs.attr,
518         NULL
519 };
520
521 static struct attribute_group dev_counter_attr_group = {
522         .name = "counters",
523         .attrs = dev_counter_attributes
524 };
525
526 static ssize_t store_reset(struct device *dev,
527                          struct device_attribute *attr,
528                           const char *buf,
529                           size_t count)
530 {
531         struct ipath_devdata *dd = dev_get_drvdata(dev);
532         int ret;
533
534         if (count < 5 || memcmp(buf, "reset", 5)) {
535                 ret = -EINVAL;
536                 goto bail;
537         }
538
539         if (dd->ipath_flags & IPATH_DISABLED) {
540                 /*
541                  * post-reset init would re-enable interrupts, etc.
542                  * so don't allow reset on disabled devices.  Not
543                  * perfect error, but about the best choice.
544                  */
545                 dev_info(dev,"Unit %d is disabled, can't reset\n",
546                          dd->ipath_unit);
547                 ret = -EINVAL;
548         }
549         ret = ipath_reset_device(dd->ipath_unit);
550 bail:
551         return ret<0 ? ret : count;
552 }
553
554 static ssize_t store_link_state(struct device *dev,
555                          struct device_attribute *attr,
556                           const char *buf,
557                           size_t count)
558 {
559         struct ipath_devdata *dd = dev_get_drvdata(dev);
560         int ret, r;
561         u16 state;
562
563         ret = ipath_parse_ushort(buf, &state);
564         if (ret < 0)
565                 goto invalid;
566
567         r = ipath_set_linkstate(dd, state);
568         if (r < 0) {
569                 ret = r;
570                 goto bail;
571         }
572
573         goto bail;
574 invalid:
575         ipath_dev_err(dd, "attempt to set invalid link state\n");
576 bail:
577         return ret;
578 }
579
580 static ssize_t show_mtu(struct device *dev,
581                          struct device_attribute *attr,
582                          char *buf)
583 {
584         struct ipath_devdata *dd = dev_get_drvdata(dev);
585         return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_ibmtu);
586 }
587
588 static ssize_t store_mtu(struct device *dev,
589                          struct device_attribute *attr,
590                           const char *buf,
591                           size_t count)
592 {
593         struct ipath_devdata *dd = dev_get_drvdata(dev);
594         ssize_t ret;
595         u16 mtu = 0;
596         int r;
597
598         ret = ipath_parse_ushort(buf, &mtu);
599         if (ret < 0)
600                 goto invalid;
601
602         r = ipath_set_mtu(dd, mtu);
603         if (r < 0)
604                 ret = r;
605
606         goto bail;
607 invalid:
608         ipath_dev_err(dd, "attempt to set invalid MTU\n");
609 bail:
610         return ret;
611 }
612
613 static ssize_t show_enabled(struct device *dev,
614                          struct device_attribute *attr,
615                          char *buf)
616 {
617         struct ipath_devdata *dd = dev_get_drvdata(dev);
618         return scnprintf(buf, PAGE_SIZE, "%u\n",
619                          (dd->ipath_flags & IPATH_DISABLED) ? 0 : 1);
620 }
621
622 static ssize_t store_enabled(struct device *dev,
623                          struct device_attribute *attr,
624                           const char *buf,
625                           size_t count)
626 {
627         struct ipath_devdata *dd = dev_get_drvdata(dev);
628         ssize_t ret;
629         u16 enable = 0;
630
631         ret = ipath_parse_ushort(buf, &enable);
632         if (ret < 0) {
633                 ipath_dev_err(dd, "attempt to use non-numeric on enable\n");
634                 goto bail;
635         }
636
637         if (enable) {
638                 if (!(dd->ipath_flags & IPATH_DISABLED))
639                         goto bail;
640
641                 dev_info(dev, "Enabling unit %d\n", dd->ipath_unit);
642                 /* same as post-reset */
643                 ret = ipath_init_chip(dd, 1);
644                 if (ret)
645                         ipath_dev_err(dd, "Failed to enable unit %d\n",
646                                       dd->ipath_unit);
647                 else {
648                         dd->ipath_flags &= ~IPATH_DISABLED;
649                         *dd->ipath_statusp &= ~IPATH_STATUS_ADMIN_DISABLED;
650                 }
651         }
652         else if (!(dd->ipath_flags & IPATH_DISABLED)) {
653                 dev_info(dev, "Disabling unit %d\n", dd->ipath_unit);
654                 ipath_shutdown_device(dd);
655                 dd->ipath_flags |= IPATH_DISABLED;
656                 *dd->ipath_statusp |= IPATH_STATUS_ADMIN_DISABLED;
657         }
658
659 bail:
660         return ret;
661 }
662
663 static ssize_t store_rx_pol_inv(struct device *dev,
664                           struct device_attribute *attr,
665                           const char *buf,
666                           size_t count)
667 {
668         struct ipath_devdata *dd = dev_get_drvdata(dev);
669         int ret, r;
670         u16 val;
671
672         ret = ipath_parse_ushort(buf, &val);
673         if (ret < 0)
674                 goto invalid;
675
676         r = ipath_set_rx_pol_inv(dd, val);
677         if (r < 0) {
678                 ret = r;
679                 goto bail;
680         }
681
682         goto bail;
683 invalid:
684         ipath_dev_err(dd, "attempt to set invalid Rx Polarity invert\n");
685 bail:
686         return ret;
687 }
688
689 static ssize_t store_led_override(struct device *dev,
690                           struct device_attribute *attr,
691                           const char *buf,
692                           size_t count)
693 {
694         struct ipath_devdata *dd = dev_get_drvdata(dev);
695         int ret;
696         u16 val;
697
698         ret = ipath_parse_ushort(buf, &val);
699         if (ret > 0)
700                 ipath_set_led_override(dd, val);
701         else
702                 ipath_dev_err(dd, "attempt to set invalid LED override\n");
703         return ret;
704 }
705
706 static ssize_t show_logged_errs(struct device *dev,
707                                 struct device_attribute *attr,
708                                 char *buf)
709 {
710         struct ipath_devdata *dd = dev_get_drvdata(dev);
711         int idx, count;
712
713         /* force consistency with actual EEPROM */
714         if (ipath_update_eeprom_log(dd) != 0)
715                 return -ENXIO;
716
717         count = 0;
718         for (idx = 0; idx < IPATH_EEP_LOG_CNT; ++idx) {
719                 count += scnprintf(buf + count, PAGE_SIZE - count, "%d%c",
720                         dd->ipath_eep_st_errs[idx],
721                         idx == (IPATH_EEP_LOG_CNT - 1) ? '\n' : ' ');
722         }
723
724         return count;
725 }
726
727 /*
728  * New sysfs entries to control various IB config. These all turn into
729  * accesses via ipath_f_get/set_ib_cfg.
730  *
731  * Get/Set heartbeat enable. Or of 1=enabled, 2=auto
732  */
733 static ssize_t show_hrtbt_enb(struct device *dev,
734                          struct device_attribute *attr,
735                          char *buf)
736 {
737         struct ipath_devdata *dd = dev_get_drvdata(dev);
738         int ret;
739
740         ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_HRTBT);
741         if (ret >= 0)
742                 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
743         return ret;
744 }
745
746 static ssize_t store_hrtbt_enb(struct device *dev,
747                           struct device_attribute *attr,
748                           const char *buf,
749                           size_t count)
750 {
751         struct ipath_devdata *dd = dev_get_drvdata(dev);
752         int ret, r;
753         u16 val;
754
755         ret = ipath_parse_ushort(buf, &val);
756         if (ret >= 0 && val > 3)
757                 ret = -EINVAL;
758         if (ret < 0) {
759                 ipath_dev_err(dd, "attempt to set invalid Heartbeat enable\n");
760                 goto bail;
761         }
762
763         /*
764          * Set the "intentional" heartbeat enable per either of
765          * "Enable" and "Auto", as these are normally set together.
766          * This bit is consulted when leaving loopback mode,
767          * because entering loopback mode overrides it and automatically
768          * disables heartbeat.
769          */
770         r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_HRTBT, val);
771         if (r < 0)
772                 ret = r;
773         else if (val == IPATH_IB_HRTBT_OFF)
774                 dd->ipath_flags |= IPATH_NO_HRTBT;
775         else
776                 dd->ipath_flags &= ~IPATH_NO_HRTBT;
777
778 bail:
779         return ret;
780 }
781
782 /*
783  * Get/Set Link-widths enabled. Or of 1=1x, 2=4x (this is human/IB centric,
784  * _not_ the particular encoding of any given chip)
785  */
786 static ssize_t show_lwid_enb(struct device *dev,
787                          struct device_attribute *attr,
788                          char *buf)
789 {
790         struct ipath_devdata *dd = dev_get_drvdata(dev);
791         int ret;
792
793         ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_LWID_ENB);
794         if (ret >= 0)
795                 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
796         return ret;
797 }
798
799 static ssize_t store_lwid_enb(struct device *dev,
800                           struct device_attribute *attr,
801                           const char *buf,
802                           size_t count)
803 {
804         struct ipath_devdata *dd = dev_get_drvdata(dev);
805         int ret, r;
806         u16 val;
807
808         ret = ipath_parse_ushort(buf, &val);
809         if (ret >= 0 && (val == 0 || val > 3))
810                 ret = -EINVAL;
811         if (ret < 0) {
812                 ipath_dev_err(dd,
813                         "attempt to set invalid Link Width (enable)\n");
814                 goto bail;
815         }
816
817         r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_LWID_ENB, val);
818         if (r < 0)
819                 ret = r;
820
821 bail:
822         return ret;
823 }
824
825 /* Get current link width */
826 static ssize_t show_lwid(struct device *dev,
827                          struct device_attribute *attr,
828                          char *buf)
829
830 {
831         struct ipath_devdata *dd = dev_get_drvdata(dev);
832         int ret;
833
834         ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_LWID);
835         if (ret >= 0)
836                 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
837         return ret;
838 }
839
840 /*
841  * Get/Set Link-speeds enabled. Or of 1=SDR 2=DDR.
842  */
843 static ssize_t show_spd_enb(struct device *dev,
844                          struct device_attribute *attr,
845                          char *buf)
846 {
847         struct ipath_devdata *dd = dev_get_drvdata(dev);
848         int ret;
849
850         ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_SPD_ENB);
851         if (ret >= 0)
852                 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
853         return ret;
854 }
855
856 static ssize_t store_spd_enb(struct device *dev,
857                           struct device_attribute *attr,
858                           const char *buf,
859                           size_t count)
860 {
861         struct ipath_devdata *dd = dev_get_drvdata(dev);
862         int ret, r;
863         u16 val;
864
865         ret = ipath_parse_ushort(buf, &val);
866         if (ret >= 0 && (val == 0 || val > (IPATH_IB_SDR | IPATH_IB_DDR)))
867                 ret = -EINVAL;
868         if (ret < 0) {
869                 ipath_dev_err(dd,
870                         "attempt to set invalid Link Speed (enable)\n");
871                 goto bail;
872         }
873
874         r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_SPD_ENB, val);
875         if (r < 0)
876                 ret = r;
877
878 bail:
879         return ret;
880 }
881
882 /* Get current link speed */
883 static ssize_t show_spd(struct device *dev,
884                          struct device_attribute *attr,
885                          char *buf)
886 {
887         struct ipath_devdata *dd = dev_get_drvdata(dev);
888         int ret;
889
890         ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_SPD);
891         if (ret >= 0)
892                 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
893         return ret;
894 }
895
896 /*
897  * Get/Set RX polarity-invert enable. 0=no, 1=yes.
898  */
899 static ssize_t show_rx_polinv_enb(struct device *dev,
900                          struct device_attribute *attr,
901                          char *buf)
902 {
903         struct ipath_devdata *dd = dev_get_drvdata(dev);
904         int ret;
905
906         ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_RXPOL_ENB);
907         if (ret >= 0)
908                 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
909         return ret;
910 }
911
912 static ssize_t store_rx_polinv_enb(struct device *dev,
913                           struct device_attribute *attr,
914                           const char *buf,
915                           size_t count)
916 {
917         struct ipath_devdata *dd = dev_get_drvdata(dev);
918         int ret, r;
919         u16 val;
920
921         ret = ipath_parse_ushort(buf, &val);
922         if (ret < 0 || val > 1)
923                 goto invalid;
924
925         r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_RXPOL_ENB, val);
926         if (r < 0) {
927                 ret = r;
928                 goto bail;
929         }
930
931         goto bail;
932 invalid:
933         ipath_dev_err(dd, "attempt to set invalid Rx Polarity (enable)\n");
934 bail:
935         return ret;
936 }
937 /*
938  * Get/Set RX lane-reversal enable. 0=no, 1=yes.
939  */
940 static ssize_t show_lanerev_enb(struct device *dev,
941                          struct device_attribute *attr,
942                          char *buf)
943 {
944         struct ipath_devdata *dd = dev_get_drvdata(dev);
945         int ret;
946
947         ret = dd->ipath_f_get_ib_cfg(dd, IPATH_IB_CFG_LREV_ENB);
948         if (ret >= 0)
949                 ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
950         return ret;
951 }
952
953 static ssize_t store_lanerev_enb(struct device *dev,
954                           struct device_attribute *attr,
955                           const char *buf,
956                           size_t count)
957 {
958         struct ipath_devdata *dd = dev_get_drvdata(dev);
959         int ret, r;
960         u16 val;
961
962         ret = ipath_parse_ushort(buf, &val);
963         if (ret >= 0 && val > 1) {
964                 ret = -EINVAL;
965                 ipath_dev_err(dd,
966                         "attempt to set invalid Lane reversal (enable)\n");
967                 goto bail;
968         }
969
970         r = dd->ipath_f_set_ib_cfg(dd, IPATH_IB_CFG_LREV_ENB, val);
971         if (r < 0)
972                 ret = r;
973
974 bail:
975         return ret;
976 }
977
978 static DRIVER_ATTR(num_units, S_IRUGO, show_num_units, NULL);
979 static DRIVER_ATTR(version, S_IRUGO, show_version, NULL);
980
981 static struct attribute *driver_attributes[] = {
982         &driver_attr_num_units.attr,
983         &driver_attr_version.attr,
984         NULL
985 };
986
987 static struct attribute_group driver_attr_group = {
988         .attrs = driver_attributes
989 };
990
991 struct attribute_group *ipath_driver_attr_groups[] = {
992         &driver_attr_group,
993         NULL,
994 };
995
996 static DEVICE_ATTR(guid, S_IWUSR | S_IRUGO, show_guid, store_guid);
997 static DEVICE_ATTR(lmc, S_IWUSR | S_IRUGO, show_lmc, store_lmc);
998 static DEVICE_ATTR(lid, S_IWUSR | S_IRUGO, show_lid, store_lid);
999 static DEVICE_ATTR(link_state, S_IWUSR, NULL, store_link_state);
1000 static DEVICE_ATTR(mlid, S_IWUSR | S_IRUGO, show_mlid, store_mlid);
1001 static DEVICE_ATTR(mtu, S_IWUSR | S_IRUGO, show_mtu, store_mtu);
1002 static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO, show_enabled, store_enabled);
1003 static DEVICE_ATTR(nguid, S_IRUGO, show_nguid, NULL);
1004 static DEVICE_ATTR(nports, S_IRUGO, show_nports, NULL);
1005 static DEVICE_ATTR(reset, S_IWUSR, NULL, store_reset);
1006 static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
1007 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
1008 static DEVICE_ATTR(status_str, S_IRUGO, show_status_str, NULL);
1009 static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
1010 static DEVICE_ATTR(unit, S_IRUGO, show_unit, NULL);
1011 static DEVICE_ATTR(rx_pol_inv, S_IWUSR, NULL, store_rx_pol_inv);
1012 static DEVICE_ATTR(led_override, S_IWUSR, NULL, store_led_override);
1013 static DEVICE_ATTR(logged_errors, S_IRUGO, show_logged_errs, NULL);
1014 static DEVICE_ATTR(jint_max_packets, S_IWUSR | S_IRUGO,
1015                    show_jint_max_packets, store_jint_max_packets);
1016 static DEVICE_ATTR(jint_idle_ticks, S_IWUSR | S_IRUGO,
1017                    show_jint_idle_ticks, store_jint_idle_ticks);
1018
1019 static struct attribute *dev_attributes[] = {
1020         &dev_attr_guid.attr,
1021         &dev_attr_lmc.attr,
1022         &dev_attr_lid.attr,
1023         &dev_attr_link_state.attr,
1024         &dev_attr_mlid.attr,
1025         &dev_attr_mtu.attr,
1026         &dev_attr_nguid.attr,
1027         &dev_attr_nports.attr,
1028         &dev_attr_serial.attr,
1029         &dev_attr_status.attr,
1030         &dev_attr_status_str.attr,
1031         &dev_attr_boardversion.attr,
1032         &dev_attr_unit.attr,
1033         &dev_attr_enabled.attr,
1034         &dev_attr_rx_pol_inv.attr,
1035         &dev_attr_led_override.attr,
1036         &dev_attr_logged_errors.attr,
1037         NULL
1038 };
1039
1040 static struct attribute_group dev_attr_group = {
1041         .attrs = dev_attributes
1042 };
1043
1044 static DEVICE_ATTR(hrtbt_enable, S_IWUSR | S_IRUGO, show_hrtbt_enb,
1045                    store_hrtbt_enb);
1046 static DEVICE_ATTR(link_width_enable, S_IWUSR | S_IRUGO, show_lwid_enb,
1047                    store_lwid_enb);
1048 static DEVICE_ATTR(link_width, S_IRUGO, show_lwid, NULL);
1049 static DEVICE_ATTR(link_speed_enable, S_IWUSR | S_IRUGO, show_spd_enb,
1050                    store_spd_enb);
1051 static DEVICE_ATTR(link_speed, S_IRUGO, show_spd, NULL);
1052 static DEVICE_ATTR(rx_pol_inv_enable, S_IWUSR | S_IRUGO, show_rx_polinv_enb,
1053                    store_rx_polinv_enb);
1054 static DEVICE_ATTR(rx_lane_rev_enable, S_IWUSR | S_IRUGO, show_lanerev_enb,
1055                    store_lanerev_enb);
1056
1057 static struct attribute *dev_ibcfg_attributes[] = {
1058         &dev_attr_hrtbt_enable.attr,
1059         &dev_attr_link_width_enable.attr,
1060         &dev_attr_link_width.attr,
1061         &dev_attr_link_speed_enable.attr,
1062         &dev_attr_link_speed.attr,
1063         &dev_attr_rx_pol_inv_enable.attr,
1064         &dev_attr_rx_lane_rev_enable.attr,
1065         NULL
1066 };
1067
1068 static struct attribute_group dev_ibcfg_attr_group = {
1069         .attrs = dev_ibcfg_attributes
1070 };
1071
1072 /**
1073  * ipath_expose_reset - create a device reset file
1074  * @dev: the device structure
1075  *
1076  * Only expose a file that lets us reset the device after someone
1077  * enters diag mode.  A device reset is quite likely to crash the
1078  * machine entirely, so we don't want to normally make it
1079  * available.
1080  *
1081  * Called with ipath_mutex held.
1082  */
1083 int ipath_expose_reset(struct device *dev)
1084 {
1085         static int exposed;
1086         int ret;
1087
1088         if (!exposed) {
1089                 ret = device_create_file(dev, &dev_attr_reset);
1090                 exposed = 1;
1091         }
1092         else
1093                 ret = 0;
1094
1095         return ret;
1096 }
1097
1098 int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd)
1099 {
1100         int ret;
1101
1102         ret = sysfs_create_group(&dev->kobj, &dev_attr_group);
1103         if (ret)
1104                 goto bail;
1105
1106         ret = sysfs_create_group(&dev->kobj, &dev_counter_attr_group);
1107         if (ret)
1108                 goto bail_attrs;
1109
1110         if (dd->ipath_flags & IPATH_HAS_MULT_IB_SPEED) {
1111                 ret = device_create_file(dev, &dev_attr_jint_idle_ticks);
1112                 if (ret)
1113                         goto bail_counter;
1114                 ret = device_create_file(dev, &dev_attr_jint_max_packets);
1115                 if (ret)
1116                         goto bail_idle;
1117
1118                 ret = sysfs_create_group(&dev->kobj, &dev_ibcfg_attr_group);
1119                 if (ret)
1120                         goto bail_max;
1121         }
1122
1123         return 0;
1124
1125 bail_max:
1126         device_remove_file(dev, &dev_attr_jint_max_packets);
1127 bail_idle:
1128         device_remove_file(dev, &dev_attr_jint_idle_ticks);
1129 bail_counter:
1130         sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
1131 bail_attrs:
1132         sysfs_remove_group(&dev->kobj, &dev_attr_group);
1133 bail:
1134         return ret;
1135 }
1136
1137 void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd)
1138 {
1139         sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
1140
1141         if (dd->ipath_flags & IPATH_HAS_MULT_IB_SPEED) {
1142                 sysfs_remove_group(&dev->kobj, &dev_ibcfg_attr_group);
1143                 device_remove_file(dev, &dev_attr_jint_idle_ticks);
1144                 device_remove_file(dev, &dev_attr_jint_max_packets);
1145         }
1146
1147         sysfs_remove_group(&dev->kobj, &dev_attr_group);
1148
1149         device_remove_file(dev, &dev_attr_reset);
1150 }