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