GNU Linux-libre 4.4.287-gnu1
[releases.git] / drivers / infiniband / hw / qib / qib_sysfs.c
1 /*
2  * Copyright (c) 2012 Intel Corporation.  All rights reserved.
3  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4  * Copyright (c) 2006 PathScale, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 #include <linux/ctype.h>
35
36 #include "qib.h"
37 #include "qib_mad.h"
38
39 /* start of per-port functions */
40 /*
41  * Get/Set heartbeat enable. OR of 1=enabled, 2=auto
42  */
43 static ssize_t show_hrtbt_enb(struct qib_pportdata *ppd, char *buf)
44 {
45         struct qib_devdata *dd = ppd->dd;
46         int ret;
47
48         ret = dd->f_get_ib_cfg(ppd, QIB_IB_CFG_HRTBT);
49         ret = scnprintf(buf, PAGE_SIZE, "%d\n", ret);
50         return ret;
51 }
52
53 static ssize_t store_hrtbt_enb(struct qib_pportdata *ppd, const char *buf,
54                                size_t count)
55 {
56         struct qib_devdata *dd = ppd->dd;
57         int ret;
58         u16 val;
59
60         ret = kstrtou16(buf, 0, &val);
61         if (ret) {
62                 qib_dev_err(dd, "attempt to set invalid Heartbeat enable\n");
63                 return ret;
64         }
65
66         /*
67          * Set the "intentional" heartbeat enable per either of
68          * "Enable" and "Auto", as these are normally set together.
69          * This bit is consulted when leaving loopback mode,
70          * because entering loopback mode overrides it and automatically
71          * disables heartbeat.
72          */
73         ret = dd->f_set_ib_cfg(ppd, QIB_IB_CFG_HRTBT, val);
74         return ret < 0 ? ret : count;
75 }
76
77 static ssize_t store_loopback(struct qib_pportdata *ppd, const char *buf,
78                               size_t count)
79 {
80         struct qib_devdata *dd = ppd->dd;
81         int ret = count, r;
82
83         r = dd->f_set_ib_loopback(ppd, buf);
84         if (r < 0)
85                 ret = r;
86
87         return ret;
88 }
89
90 static ssize_t store_led_override(struct qib_pportdata *ppd, const char *buf,
91                                   size_t count)
92 {
93         struct qib_devdata *dd = ppd->dd;
94         int ret;
95         u16 val;
96
97         ret = kstrtou16(buf, 0, &val);
98         if (ret) {
99                 qib_dev_err(dd, "attempt to set invalid LED override\n");
100                 return ret;
101         }
102
103         qib_set_led_override(ppd, val);
104         return count;
105 }
106
107 static ssize_t show_status(struct qib_pportdata *ppd, char *buf)
108 {
109         ssize_t ret;
110
111         if (!ppd->statusp)
112                 ret = -EINVAL;
113         else
114                 ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
115                                 (unsigned long long) *(ppd->statusp));
116         return ret;
117 }
118
119 /*
120  * For userland compatibility, these offsets must remain fixed.
121  * They are strings for QIB_STATUS_*
122  */
123 static const char * const qib_status_str[] = {
124         "Initted",
125         "",
126         "",
127         "",
128         "",
129         "Present",
130         "IB_link_up",
131         "IB_configured",
132         "",
133         "Fatal_Hardware_Error",
134         NULL,
135 };
136
137 static ssize_t show_status_str(struct qib_pportdata *ppd, char *buf)
138 {
139         int i, any;
140         u64 s;
141         ssize_t ret;
142
143         if (!ppd->statusp) {
144                 ret = -EINVAL;
145                 goto bail;
146         }
147
148         s = *(ppd->statusp);
149         *buf = '\0';
150         for (any = i = 0; s && qib_status_str[i]; i++) {
151                 if (s & 1) {
152                         /* if overflow */
153                         if (any && strlcat(buf, " ", PAGE_SIZE) >= PAGE_SIZE)
154                                 break;
155                         if (strlcat(buf, qib_status_str[i], PAGE_SIZE) >=
156                                         PAGE_SIZE)
157                                 break;
158                         any = 1;
159                 }
160                 s >>= 1;
161         }
162         if (any)
163                 strlcat(buf, "\n", PAGE_SIZE);
164
165         ret = strlen(buf);
166
167 bail:
168         return ret;
169 }
170
171 /* end of per-port functions */
172
173 /*
174  * Start of per-port file structures and support code
175  * Because we are fitting into other infrastructure, we have to supply the
176  * full set of kobject/sysfs_ops structures and routines.
177  */
178 #define QIB_PORT_ATTR(name, mode, show, store) \
179         static struct qib_port_attr qib_port_attr_##name = \
180                 __ATTR(name, mode, show, store)
181
182 struct qib_port_attr {
183         struct attribute attr;
184         ssize_t (*show)(struct qib_pportdata *, char *);
185         ssize_t (*store)(struct qib_pportdata *, const char *, size_t);
186 };
187
188 QIB_PORT_ATTR(loopback, S_IWUSR, NULL, store_loopback);
189 QIB_PORT_ATTR(led_override, S_IWUSR, NULL, store_led_override);
190 QIB_PORT_ATTR(hrtbt_enable, S_IWUSR | S_IRUGO, show_hrtbt_enb,
191               store_hrtbt_enb);
192 QIB_PORT_ATTR(status, S_IRUGO, show_status, NULL);
193 QIB_PORT_ATTR(status_str, S_IRUGO, show_status_str, NULL);
194
195 static struct attribute *port_default_attributes[] = {
196         &qib_port_attr_loopback.attr,
197         &qib_port_attr_led_override.attr,
198         &qib_port_attr_hrtbt_enable.attr,
199         &qib_port_attr_status.attr,
200         &qib_port_attr_status_str.attr,
201         NULL
202 };
203
204 /*
205  * Start of per-port congestion control structures and support code
206  */
207
208 /*
209  * Congestion control table size followed by table entries
210  */
211 static ssize_t read_cc_table_bin(struct file *filp, struct kobject *kobj,
212                 struct bin_attribute *bin_attr,
213                 char *buf, loff_t pos, size_t count)
214 {
215         int ret;
216         struct qib_pportdata *ppd =
217                 container_of(kobj, struct qib_pportdata, pport_cc_kobj);
218
219         if (!qib_cc_table_size || !ppd->ccti_entries_shadow)
220                 return -EINVAL;
221
222         ret = ppd->total_cct_entry * sizeof(struct ib_cc_table_entry_shadow)
223                  + sizeof(__be16);
224
225         if (pos > ret)
226                 return -EINVAL;
227
228         if (count > ret - pos)
229                 count = ret - pos;
230
231         if (!count)
232                 return count;
233
234         spin_lock(&ppd->cc_shadow_lock);
235         memcpy(buf, ppd->ccti_entries_shadow, count);
236         spin_unlock(&ppd->cc_shadow_lock);
237
238         return count;
239 }
240
241 static void qib_port_release(struct kobject *kobj)
242 {
243         /* nothing to do since memory is freed by qib_free_devdata() */
244 }
245
246 static struct kobj_type qib_port_cc_ktype = {
247         .release = qib_port_release,
248 };
249
250 static struct bin_attribute cc_table_bin_attr = {
251         .attr = {.name = "cc_table_bin", .mode = 0444},
252         .read = read_cc_table_bin,
253         .size = PAGE_SIZE,
254 };
255
256 /*
257  * Congestion settings: port control, control map and an array of 16
258  * entries for the congestion entries - increase, timer, event log
259  * trigger threshold and the minimum injection rate delay.
260  */
261 static ssize_t read_cc_setting_bin(struct file *filp, struct kobject *kobj,
262                 struct bin_attribute *bin_attr,
263                 char *buf, loff_t pos, size_t count)
264 {
265         int ret;
266         struct qib_pportdata *ppd =
267                 container_of(kobj, struct qib_pportdata, pport_cc_kobj);
268
269         if (!qib_cc_table_size || !ppd->congestion_entries_shadow)
270                 return -EINVAL;
271
272         ret = sizeof(struct ib_cc_congestion_setting_attr_shadow);
273
274         if (pos > ret)
275                 return -EINVAL;
276         if (count > ret - pos)
277                 count = ret - pos;
278
279         if (!count)
280                 return count;
281
282         spin_lock(&ppd->cc_shadow_lock);
283         memcpy(buf, ppd->congestion_entries_shadow, count);
284         spin_unlock(&ppd->cc_shadow_lock);
285
286         return count;
287 }
288
289 static struct bin_attribute cc_setting_bin_attr = {
290         .attr = {.name = "cc_settings_bin", .mode = 0444},
291         .read = read_cc_setting_bin,
292         .size = PAGE_SIZE,
293 };
294
295
296 static ssize_t qib_portattr_show(struct kobject *kobj,
297         struct attribute *attr, char *buf)
298 {
299         struct qib_port_attr *pattr =
300                 container_of(attr, struct qib_port_attr, attr);
301         struct qib_pportdata *ppd =
302                 container_of(kobj, struct qib_pportdata, pport_kobj);
303
304         if (!pattr->show)
305                 return -EIO;
306
307         return pattr->show(ppd, buf);
308 }
309
310 static ssize_t qib_portattr_store(struct kobject *kobj,
311         struct attribute *attr, const char *buf, size_t len)
312 {
313         struct qib_port_attr *pattr =
314                 container_of(attr, struct qib_port_attr, attr);
315         struct qib_pportdata *ppd =
316                 container_of(kobj, struct qib_pportdata, pport_kobj);
317
318         if (!pattr->store)
319                 return -EIO;
320
321         return pattr->store(ppd, buf, len);
322 }
323
324
325 static const struct sysfs_ops qib_port_ops = {
326         .show = qib_portattr_show,
327         .store = qib_portattr_store,
328 };
329
330 static struct kobj_type qib_port_ktype = {
331         .release = qib_port_release,
332         .sysfs_ops = &qib_port_ops,
333         .default_attrs = port_default_attributes
334 };
335
336 /* Start sl2vl */
337
338 #define QIB_SL2VL_ATTR(N) \
339         static struct qib_sl2vl_attr qib_sl2vl_attr_##N = { \
340                 .attr = { .name = __stringify(N), .mode = 0444 }, \
341                 .sl = N \
342         }
343
344 struct qib_sl2vl_attr {
345         struct attribute attr;
346         int sl;
347 };
348
349 QIB_SL2VL_ATTR(0);
350 QIB_SL2VL_ATTR(1);
351 QIB_SL2VL_ATTR(2);
352 QIB_SL2VL_ATTR(3);
353 QIB_SL2VL_ATTR(4);
354 QIB_SL2VL_ATTR(5);
355 QIB_SL2VL_ATTR(6);
356 QIB_SL2VL_ATTR(7);
357 QIB_SL2VL_ATTR(8);
358 QIB_SL2VL_ATTR(9);
359 QIB_SL2VL_ATTR(10);
360 QIB_SL2VL_ATTR(11);
361 QIB_SL2VL_ATTR(12);
362 QIB_SL2VL_ATTR(13);
363 QIB_SL2VL_ATTR(14);
364 QIB_SL2VL_ATTR(15);
365
366 static struct attribute *sl2vl_default_attributes[] = {
367         &qib_sl2vl_attr_0.attr,
368         &qib_sl2vl_attr_1.attr,
369         &qib_sl2vl_attr_2.attr,
370         &qib_sl2vl_attr_3.attr,
371         &qib_sl2vl_attr_4.attr,
372         &qib_sl2vl_attr_5.attr,
373         &qib_sl2vl_attr_6.attr,
374         &qib_sl2vl_attr_7.attr,
375         &qib_sl2vl_attr_8.attr,
376         &qib_sl2vl_attr_9.attr,
377         &qib_sl2vl_attr_10.attr,
378         &qib_sl2vl_attr_11.attr,
379         &qib_sl2vl_attr_12.attr,
380         &qib_sl2vl_attr_13.attr,
381         &qib_sl2vl_attr_14.attr,
382         &qib_sl2vl_attr_15.attr,
383         NULL
384 };
385
386 static ssize_t sl2vl_attr_show(struct kobject *kobj, struct attribute *attr,
387                                char *buf)
388 {
389         struct qib_sl2vl_attr *sattr =
390                 container_of(attr, struct qib_sl2vl_attr, attr);
391         struct qib_pportdata *ppd =
392                 container_of(kobj, struct qib_pportdata, sl2vl_kobj);
393         struct qib_ibport *qibp = &ppd->ibport_data;
394
395         return sprintf(buf, "%u\n", qibp->sl_to_vl[sattr->sl]);
396 }
397
398 static const struct sysfs_ops qib_sl2vl_ops = {
399         .show = sl2vl_attr_show,
400 };
401
402 static struct kobj_type qib_sl2vl_ktype = {
403         .release = qib_port_release,
404         .sysfs_ops = &qib_sl2vl_ops,
405         .default_attrs = sl2vl_default_attributes
406 };
407
408 /* End sl2vl */
409
410 /* Start diag_counters */
411
412 #define QIB_DIAGC_ATTR(N) \
413         static struct qib_diagc_attr qib_diagc_attr_##N = { \
414                 .attr = { .name = __stringify(N), .mode = 0664 }, \
415                 .counter = offsetof(struct qib_ibport, n_##N) \
416         }
417
418 struct qib_diagc_attr {
419         struct attribute attr;
420         size_t counter;
421 };
422
423 QIB_DIAGC_ATTR(rc_resends);
424 QIB_DIAGC_ATTR(rc_acks);
425 QIB_DIAGC_ATTR(rc_qacks);
426 QIB_DIAGC_ATTR(rc_delayed_comp);
427 QIB_DIAGC_ATTR(seq_naks);
428 QIB_DIAGC_ATTR(rdma_seq);
429 QIB_DIAGC_ATTR(rnr_naks);
430 QIB_DIAGC_ATTR(other_naks);
431 QIB_DIAGC_ATTR(rc_timeouts);
432 QIB_DIAGC_ATTR(loop_pkts);
433 QIB_DIAGC_ATTR(pkt_drops);
434 QIB_DIAGC_ATTR(dmawait);
435 QIB_DIAGC_ATTR(unaligned);
436 QIB_DIAGC_ATTR(rc_dupreq);
437 QIB_DIAGC_ATTR(rc_seqnak);
438
439 static struct attribute *diagc_default_attributes[] = {
440         &qib_diagc_attr_rc_resends.attr,
441         &qib_diagc_attr_rc_acks.attr,
442         &qib_diagc_attr_rc_qacks.attr,
443         &qib_diagc_attr_rc_delayed_comp.attr,
444         &qib_diagc_attr_seq_naks.attr,
445         &qib_diagc_attr_rdma_seq.attr,
446         &qib_diagc_attr_rnr_naks.attr,
447         &qib_diagc_attr_other_naks.attr,
448         &qib_diagc_attr_rc_timeouts.attr,
449         &qib_diagc_attr_loop_pkts.attr,
450         &qib_diagc_attr_pkt_drops.attr,
451         &qib_diagc_attr_dmawait.attr,
452         &qib_diagc_attr_unaligned.attr,
453         &qib_diagc_attr_rc_dupreq.attr,
454         &qib_diagc_attr_rc_seqnak.attr,
455         NULL
456 };
457
458 static ssize_t diagc_attr_show(struct kobject *kobj, struct attribute *attr,
459                                char *buf)
460 {
461         struct qib_diagc_attr *dattr =
462                 container_of(attr, struct qib_diagc_attr, attr);
463         struct qib_pportdata *ppd =
464                 container_of(kobj, struct qib_pportdata, diagc_kobj);
465         struct qib_ibport *qibp = &ppd->ibport_data;
466
467         return sprintf(buf, "%u\n", *(u32 *)((char *)qibp + dattr->counter));
468 }
469
470 static ssize_t diagc_attr_store(struct kobject *kobj, struct attribute *attr,
471                                 const char *buf, size_t size)
472 {
473         struct qib_diagc_attr *dattr =
474                 container_of(attr, struct qib_diagc_attr, attr);
475         struct qib_pportdata *ppd =
476                 container_of(kobj, struct qib_pportdata, diagc_kobj);
477         struct qib_ibport *qibp = &ppd->ibport_data;
478         u32 val;
479         int ret;
480
481         ret = kstrtou32(buf, 0, &val);
482         if (ret)
483                 return ret;
484         *(u32 *)((char *) qibp + dattr->counter) = val;
485         return size;
486 }
487
488 static const struct sysfs_ops qib_diagc_ops = {
489         .show = diagc_attr_show,
490         .store = diagc_attr_store,
491 };
492
493 static struct kobj_type qib_diagc_ktype = {
494         .release = qib_port_release,
495         .sysfs_ops = &qib_diagc_ops,
496         .default_attrs = diagc_default_attributes
497 };
498
499 /* End diag_counters */
500
501 /* end of per-port file structures and support code */
502
503 /*
504  * Start of per-unit (or driver, in some cases, but replicated
505  * per unit) functions (these get a device *)
506  */
507 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
508                         char *buf)
509 {
510         struct qib_ibdev *dev =
511                 container_of(device, struct qib_ibdev, ibdev.dev);
512
513         return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
514 }
515
516 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
517                         char *buf)
518 {
519         struct qib_ibdev *dev =
520                 container_of(device, struct qib_ibdev, ibdev.dev);
521         struct qib_devdata *dd = dd_from_dev(dev);
522         int ret;
523
524         if (!dd->boardname)
525                 ret = -EINVAL;
526         else
527                 ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
528         return ret;
529 }
530
531 static ssize_t show_version(struct device *device,
532                             struct device_attribute *attr, char *buf)
533 {
534         /* The string printed here is already newline-terminated. */
535         return scnprintf(buf, PAGE_SIZE, "%s", (char *)ib_qib_version);
536 }
537
538 static ssize_t show_boardversion(struct device *device,
539                                  struct device_attribute *attr, char *buf)
540 {
541         struct qib_ibdev *dev =
542                 container_of(device, struct qib_ibdev, ibdev.dev);
543         struct qib_devdata *dd = dd_from_dev(dev);
544
545         /* The string printed here is already newline-terminated. */
546         return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
547 }
548
549
550 static ssize_t show_localbus_info(struct device *device,
551                                   struct device_attribute *attr, char *buf)
552 {
553         struct qib_ibdev *dev =
554                 container_of(device, struct qib_ibdev, ibdev.dev);
555         struct qib_devdata *dd = dd_from_dev(dev);
556
557         /* The string printed here is already newline-terminated. */
558         return scnprintf(buf, PAGE_SIZE, "%s", dd->lbus_info);
559 }
560
561
562 static ssize_t show_nctxts(struct device *device,
563                            struct device_attribute *attr, char *buf)
564 {
565         struct qib_ibdev *dev =
566                 container_of(device, struct qib_ibdev, ibdev.dev);
567         struct qib_devdata *dd = dd_from_dev(dev);
568
569         /* Return the number of user ports (contexts) available. */
570         /* The calculation below deals with a special case where
571          * cfgctxts is set to 1 on a single-port board. */
572         return scnprintf(buf, PAGE_SIZE, "%u\n",
573                         (dd->first_user_ctxt > dd->cfgctxts) ? 0 :
574                         (dd->cfgctxts - dd->first_user_ctxt));
575 }
576
577 static ssize_t show_nfreectxts(struct device *device,
578                            struct device_attribute *attr, char *buf)
579 {
580         struct qib_ibdev *dev =
581                 container_of(device, struct qib_ibdev, ibdev.dev);
582         struct qib_devdata *dd = dd_from_dev(dev);
583
584         /* Return the number of free user ports (contexts) available. */
585         return scnprintf(buf, PAGE_SIZE, "%u\n", dd->freectxts);
586 }
587
588 static ssize_t show_serial(struct device *device,
589                            struct device_attribute *attr, char *buf)
590 {
591         struct qib_ibdev *dev =
592                 container_of(device, struct qib_ibdev, ibdev.dev);
593         struct qib_devdata *dd = dd_from_dev(dev);
594
595         buf[sizeof(dd->serial)] = '\0';
596         memcpy(buf, dd->serial, sizeof(dd->serial));
597         strcat(buf, "\n");
598         return strlen(buf);
599 }
600
601 static ssize_t store_chip_reset(struct device *device,
602                                 struct device_attribute *attr, const char *buf,
603                                 size_t count)
604 {
605         struct qib_ibdev *dev =
606                 container_of(device, struct qib_ibdev, ibdev.dev);
607         struct qib_devdata *dd = dd_from_dev(dev);
608         int ret;
609
610         if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
611                 ret = -EINVAL;
612                 goto bail;
613         }
614
615         ret = qib_reset_device(dd->unit);
616 bail:
617         return ret < 0 ? ret : count;
618 }
619
620 /*
621  * Dump tempsense regs. in decimal, to ease shell-scripts.
622  */
623 static ssize_t show_tempsense(struct device *device,
624                               struct device_attribute *attr, char *buf)
625 {
626         struct qib_ibdev *dev =
627                 container_of(device, struct qib_ibdev, ibdev.dev);
628         struct qib_devdata *dd = dd_from_dev(dev);
629         int ret;
630         int idx;
631         u8 regvals[8];
632
633         ret = -ENXIO;
634         for (idx = 0; idx < 8; ++idx) {
635                 if (idx == 6)
636                         continue;
637                 ret = dd->f_tempsense_rd(dd, idx);
638                 if (ret < 0)
639                         break;
640                 regvals[idx] = ret;
641         }
642         if (idx == 8)
643                 ret = scnprintf(buf, PAGE_SIZE, "%d %d %02X %02X %d %d\n",
644                                 *(signed char *)(regvals),
645                                 *(signed char *)(regvals + 1),
646                                 regvals[2], regvals[3],
647                                 *(signed char *)(regvals + 5),
648                                 *(signed char *)(regvals + 7));
649         return ret;
650 }
651
652 /*
653  * end of per-unit (or driver, in some cases, but replicated
654  * per unit) functions
655  */
656
657 /* start of per-unit file structures and support code */
658 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
659 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
660 static DEVICE_ATTR(board_id, S_IRUGO, show_hca, NULL);
661 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
662 static DEVICE_ATTR(nctxts, S_IRUGO, show_nctxts, NULL);
663 static DEVICE_ATTR(nfreectxts, S_IRUGO, show_nfreectxts, NULL);
664 static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
665 static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
666 static DEVICE_ATTR(tempsense, S_IRUGO, show_tempsense, NULL);
667 static DEVICE_ATTR(localbus_info, S_IRUGO, show_localbus_info, NULL);
668 static DEVICE_ATTR(chip_reset, S_IWUSR, NULL, store_chip_reset);
669
670 static struct device_attribute *qib_attributes[] = {
671         &dev_attr_hw_rev,
672         &dev_attr_hca_type,
673         &dev_attr_board_id,
674         &dev_attr_version,
675         &dev_attr_nctxts,
676         &dev_attr_nfreectxts,
677         &dev_attr_serial,
678         &dev_attr_boardversion,
679         &dev_attr_tempsense,
680         &dev_attr_localbus_info,
681         &dev_attr_chip_reset,
682 };
683
684 int qib_create_port_files(struct ib_device *ibdev, u8 port_num,
685                           struct kobject *kobj)
686 {
687         struct qib_pportdata *ppd;
688         struct qib_devdata *dd = dd_from_ibdev(ibdev);
689         int ret;
690
691         if (!port_num || port_num > dd->num_pports) {
692                 qib_dev_err(dd,
693                         "Skipping infiniband class with invalid port %u\n",
694                         port_num);
695                 ret = -ENODEV;
696                 goto bail;
697         }
698         ppd = &dd->pport[port_num - 1];
699
700         ret = kobject_init_and_add(&ppd->pport_kobj, &qib_port_ktype, kobj,
701                                    "linkcontrol");
702         if (ret) {
703                 qib_dev_err(dd,
704                         "Skipping linkcontrol sysfs info, (err %d) port %u\n",
705                         ret, port_num);
706                 goto bail_link;
707         }
708         kobject_uevent(&ppd->pport_kobj, KOBJ_ADD);
709
710         ret = kobject_init_and_add(&ppd->sl2vl_kobj, &qib_sl2vl_ktype, kobj,
711                                    "sl2vl");
712         if (ret) {
713                 qib_dev_err(dd,
714                         "Skipping sl2vl sysfs info, (err %d) port %u\n",
715                         ret, port_num);
716                 goto bail_sl;
717         }
718         kobject_uevent(&ppd->sl2vl_kobj, KOBJ_ADD);
719
720         ret = kobject_init_and_add(&ppd->diagc_kobj, &qib_diagc_ktype, kobj,
721                                    "diag_counters");
722         if (ret) {
723                 qib_dev_err(dd,
724                         "Skipping diag_counters sysfs info, (err %d) port %u\n",
725                         ret, port_num);
726                 goto bail_diagc;
727         }
728         kobject_uevent(&ppd->diagc_kobj, KOBJ_ADD);
729
730         if (!qib_cc_table_size || !ppd->congestion_entries_shadow)
731                 return 0;
732
733         ret = kobject_init_and_add(&ppd->pport_cc_kobj, &qib_port_cc_ktype,
734                                 kobj, "CCMgtA");
735         if (ret) {
736                 qib_dev_err(dd,
737                  "Skipping Congestion Control sysfs info, (err %d) port %u\n",
738                  ret, port_num);
739                 goto bail_cc;
740         }
741
742         kobject_uevent(&ppd->pport_cc_kobj, KOBJ_ADD);
743
744         ret = sysfs_create_bin_file(&ppd->pport_cc_kobj,
745                                 &cc_setting_bin_attr);
746         if (ret) {
747                 qib_dev_err(dd,
748                  "Skipping Congestion Control setting sysfs info, (err %d) port %u\n",
749                  ret, port_num);
750                 goto bail_cc;
751         }
752
753         ret = sysfs_create_bin_file(&ppd->pport_cc_kobj,
754                                 &cc_table_bin_attr);
755         if (ret) {
756                 qib_dev_err(dd,
757                  "Skipping Congestion Control table sysfs info, (err %d) port %u\n",
758                  ret, port_num);
759                 goto bail_cc_entry_bin;
760         }
761
762         qib_devinfo(dd->pcidev,
763                 "IB%u: Congestion Control Agent enabled for port %d\n",
764                 dd->unit, port_num);
765
766         return 0;
767
768 bail_cc_entry_bin:
769         sysfs_remove_bin_file(&ppd->pport_cc_kobj, &cc_setting_bin_attr);
770 bail_cc:
771         kobject_put(&ppd->pport_cc_kobj);
772 bail_diagc:
773         kobject_put(&ppd->diagc_kobj);
774 bail_sl:
775         kobject_put(&ppd->sl2vl_kobj);
776 bail_link:
777         kobject_put(&ppd->pport_kobj);
778 bail:
779         return ret;
780 }
781
782 /*
783  * Register and create our files in /sys/class/infiniband.
784  */
785 int qib_verbs_register_sysfs(struct qib_devdata *dd)
786 {
787         struct ib_device *dev = &dd->verbs_dev.ibdev;
788         int i, ret;
789
790         for (i = 0; i < ARRAY_SIZE(qib_attributes); ++i) {
791                 ret = device_create_file(&dev->dev, qib_attributes[i]);
792                 if (ret)
793                         goto bail;
794         }
795
796         return 0;
797 bail:
798         for (i = 0; i < ARRAY_SIZE(qib_attributes); ++i)
799                 device_remove_file(&dev->dev, qib_attributes[i]);
800         return ret;
801 }
802
803 /*
804  * Unregister and remove our files in /sys/class/infiniband.
805  */
806 void qib_verbs_unregister_sysfs(struct qib_devdata *dd)
807 {
808         struct qib_pportdata *ppd;
809         int i;
810
811         for (i = 0; i < dd->num_pports; i++) {
812                 ppd = &dd->pport[i];
813                 if (qib_cc_table_size &&
814                         ppd->congestion_entries_shadow) {
815                         sysfs_remove_bin_file(&ppd->pport_cc_kobj,
816                                 &cc_setting_bin_attr);
817                         sysfs_remove_bin_file(&ppd->pport_cc_kobj,
818                                 &cc_table_bin_attr);
819                         kobject_put(&ppd->pport_cc_kobj);
820                 }
821                 kobject_put(&ppd->diagc_kobj);
822                 kobject_put(&ppd->sl2vl_kobj);
823                 kobject_put(&ppd->pport_kobj);
824         }
825 }