2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/ctype.h>
35 #include <linux/pci.h>
37 #include "ipath_kernel.h"
38 #include "ipath_common.h"
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
45 * returns the number of bytes consumed, or negative value on error
47 int ipath_parse_ushort(const char *str, unsigned short *valp)
53 if (!isdigit(str[0])) {
58 val = simple_strtoul(str, &end, 0);
75 static ssize_t show_version(struct device_driver *dev, char *buf)
77 /* The string printed here is already newline-terminated. */
78 return scnprintf(buf, PAGE_SIZE, "%s", ib_ipath_version);
81 static ssize_t show_num_units(struct device_driver *dev, char *buf)
83 return scnprintf(buf, PAGE_SIZE, "%d\n",
84 ipath_count_units(NULL, NULL, NULL));
87 static ssize_t show_status(struct device *dev,
88 struct device_attribute *attr,
91 struct ipath_devdata *dd = dev_get_drvdata(dev);
94 if (!dd->ipath_statusp) {
99 ret = scnprintf(buf, PAGE_SIZE, "0x%llx\n",
100 (unsigned long long) *(dd->ipath_statusp));
106 static const char *ipath_status_str[] = {
110 "", /* This used to be the old "OIB_SMA" status. */
111 "", /* This used to be the old "SMA" status. */
116 "Fatal_Hardware_Error",
120 static ssize_t show_status_str(struct device *dev,
121 struct device_attribute *attr,
124 struct ipath_devdata *dd = dev_get_drvdata(dev);
129 if (!dd->ipath_statusp) {
134 s = *(dd->ipath_statusp);
136 for (any = i = 0; s && ipath_status_str[i]; i++) {
138 if (any && strlcat(buf, " ", PAGE_SIZE) >=
142 if (strlcat(buf, ipath_status_str[i],
143 PAGE_SIZE) >= PAGE_SIZE)
150 strlcat(buf, "\n", PAGE_SIZE);
158 static ssize_t show_boardversion(struct device *dev,
159 struct device_attribute *attr,
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);
167 static ssize_t show_lid(struct device *dev,
168 struct device_attribute *attr,
171 struct ipath_devdata *dd = dev_get_drvdata(dev);
173 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_lid);
176 static ssize_t store_lid(struct device *dev,
177 struct device_attribute *attr,
181 struct ipath_devdata *dd = dev_get_drvdata(dev);
185 ret = ipath_parse_ushort(buf, &lid);
189 if (lid == 0 || lid >= IPATH_MULTICAST_LID_BASE) {
194 ipath_set_lid(dd, lid, 0);
198 ipath_dev_err(dd, "attempt to set invalid LID 0x%x\n", lid);
203 static ssize_t show_mlid(struct device *dev,
204 struct device_attribute *attr,
207 struct ipath_devdata *dd = dev_get_drvdata(dev);
209 return scnprintf(buf, PAGE_SIZE, "0x%x\n", dd->ipath_mlid);
212 static ssize_t store_mlid(struct device *dev,
213 struct device_attribute *attr,
217 struct ipath_devdata *dd = dev_get_drvdata(dev);
222 ret = ipath_parse_ushort(buf, &mlid);
223 if (ret < 0 || mlid < IPATH_MULTICAST_LID_BASE)
226 unit = dd->ipath_unit;
228 dd->ipath_mlid = mlid;
232 ipath_dev_err(dd, "attempt to set invalid MLID\n");
237 static ssize_t show_guid(struct device *dev,
238 struct device_attribute *attr,
241 struct ipath_devdata *dd = dev_get_drvdata(dev);
244 guid = (u8 *) & (dd->ipath_guid);
246 return scnprintf(buf, PAGE_SIZE,
247 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
248 guid[0], guid[1], guid[2], guid[3],
249 guid[4], guid[5], guid[6], guid[7]);
252 static ssize_t store_guid(struct device *dev,
253 struct device_attribute *attr,
257 struct ipath_devdata *dd = dev_get_drvdata(dev);
259 unsigned short guid[8];
264 if (sscanf(buf, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
265 &guid[0], &guid[1], &guid[2], &guid[3],
266 &guid[4], &guid[5], &guid[6], &guid[7]) != 8)
269 ng = (u8 *) &new_guid;
271 for (i = 0; i < 8; i++) {
280 dd->ipath_guid = new_guid;
287 ipath_dev_err(dd, "attempt to set invalid GUID\n");
294 static ssize_t show_nguid(struct device *dev,
295 struct device_attribute *attr,
298 struct ipath_devdata *dd = dev_get_drvdata(dev);
300 return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_nguid);
303 static ssize_t show_nports(struct device *dev,
304 struct device_attribute *attr,
307 struct ipath_devdata *dd = dev_get_drvdata(dev);
309 /* Return the number of user ports available. */
310 return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_cfgports - 1);
313 static ssize_t show_serial(struct device *dev,
314 struct device_attribute *attr,
317 struct ipath_devdata *dd = dev_get_drvdata(dev);
319 buf[sizeof dd->ipath_serial] = '\0';
320 memcpy(buf, dd->ipath_serial, sizeof dd->ipath_serial);
325 static ssize_t show_unit(struct device *dev,
326 struct device_attribute *attr,
329 struct ipath_devdata *dd = dev_get_drvdata(dev);
331 return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_unit);
334 #define DEVICE_COUNTER(name, attr) \
335 static ssize_t show_counter_##name(struct device *dev, \
336 struct device_attribute *attr, \
339 struct ipath_devdata *dd = dev_get_drvdata(dev); \
341 buf, PAGE_SIZE, "%llu\n", (unsigned long long) \
343 dd, offsetof(struct infinipath_counters, \
344 attr) / sizeof(u64))); \
346 static DEVICE_ATTR(name, S_IRUGO, show_counter_##name, NULL);
348 DEVICE_COUNTER(ib_link_downeds, IBLinkDownedCnt);
349 DEVICE_COUNTER(ib_link_err_recoveries, IBLinkErrRecoveryCnt);
350 DEVICE_COUNTER(ib_status_changes, IBStatusChangeCnt);
351 DEVICE_COUNTER(ib_symbol_errs, IBSymbolErrCnt);
352 DEVICE_COUNTER(lb_flow_stalls, LBFlowStallCnt);
353 DEVICE_COUNTER(lb_ints, LBIntCnt);
354 DEVICE_COUNTER(rx_bad_formats, RxBadFormatCnt);
355 DEVICE_COUNTER(rx_buf_ovfls, RxBufOvflCnt);
356 DEVICE_COUNTER(rx_data_pkts, RxDataPktCnt);
357 DEVICE_COUNTER(rx_dropped_pkts, RxDroppedPktCnt);
358 DEVICE_COUNTER(rx_dwords, RxDwordCnt);
359 DEVICE_COUNTER(rx_ebps, RxEBPCnt);
360 DEVICE_COUNTER(rx_flow_ctrl_errs, RxFlowCtrlErrCnt);
361 DEVICE_COUNTER(rx_flow_pkts, RxFlowPktCnt);
362 DEVICE_COUNTER(rx_icrc_errs, RxICRCErrCnt);
363 DEVICE_COUNTER(rx_len_errs, RxLenErrCnt);
364 DEVICE_COUNTER(rx_link_problems, RxLinkProblemCnt);
365 DEVICE_COUNTER(rx_lpcrc_errs, RxLPCRCErrCnt);
366 DEVICE_COUNTER(rx_max_min_len_errs, RxMaxMinLenErrCnt);
367 DEVICE_COUNTER(rx_p0_hdr_egr_ovfls, RxP0HdrEgrOvflCnt);
368 DEVICE_COUNTER(rx_p1_hdr_egr_ovfls, RxP1HdrEgrOvflCnt);
369 DEVICE_COUNTER(rx_p2_hdr_egr_ovfls, RxP2HdrEgrOvflCnt);
370 DEVICE_COUNTER(rx_p3_hdr_egr_ovfls, RxP3HdrEgrOvflCnt);
371 DEVICE_COUNTER(rx_p4_hdr_egr_ovfls, RxP4HdrEgrOvflCnt);
372 DEVICE_COUNTER(rx_p5_hdr_egr_ovfls, RxP5HdrEgrOvflCnt);
373 DEVICE_COUNTER(rx_p6_hdr_egr_ovfls, RxP6HdrEgrOvflCnt);
374 DEVICE_COUNTER(rx_p7_hdr_egr_ovfls, RxP7HdrEgrOvflCnt);
375 DEVICE_COUNTER(rx_p8_hdr_egr_ovfls, RxP8HdrEgrOvflCnt);
376 DEVICE_COUNTER(rx_pkey_mismatches, RxPKeyMismatchCnt);
377 DEVICE_COUNTER(rx_tid_full_errs, RxTIDFullErrCnt);
378 DEVICE_COUNTER(rx_tid_valid_errs, RxTIDValidErrCnt);
379 DEVICE_COUNTER(rx_vcrc_errs, RxVCRCErrCnt);
380 DEVICE_COUNTER(tx_data_pkts, TxDataPktCnt);
381 DEVICE_COUNTER(tx_dropped_pkts, TxDroppedPktCnt);
382 DEVICE_COUNTER(tx_dwords, TxDwordCnt);
383 DEVICE_COUNTER(tx_flow_pkts, TxFlowPktCnt);
384 DEVICE_COUNTER(tx_flow_stalls, TxFlowStallCnt);
385 DEVICE_COUNTER(tx_len_errs, TxLenErrCnt);
386 DEVICE_COUNTER(tx_max_min_len_errs, TxMaxMinLenErrCnt);
387 DEVICE_COUNTER(tx_underruns, TxUnderrunCnt);
388 DEVICE_COUNTER(tx_unsup_vl_errs, TxUnsupVLErrCnt);
390 static struct attribute *dev_counter_attributes[] = {
391 &dev_attr_ib_link_downeds.attr,
392 &dev_attr_ib_link_err_recoveries.attr,
393 &dev_attr_ib_status_changes.attr,
394 &dev_attr_ib_symbol_errs.attr,
395 &dev_attr_lb_flow_stalls.attr,
396 &dev_attr_lb_ints.attr,
397 &dev_attr_rx_bad_formats.attr,
398 &dev_attr_rx_buf_ovfls.attr,
399 &dev_attr_rx_data_pkts.attr,
400 &dev_attr_rx_dropped_pkts.attr,
401 &dev_attr_rx_dwords.attr,
402 &dev_attr_rx_ebps.attr,
403 &dev_attr_rx_flow_ctrl_errs.attr,
404 &dev_attr_rx_flow_pkts.attr,
405 &dev_attr_rx_icrc_errs.attr,
406 &dev_attr_rx_len_errs.attr,
407 &dev_attr_rx_link_problems.attr,
408 &dev_attr_rx_lpcrc_errs.attr,
409 &dev_attr_rx_max_min_len_errs.attr,
410 &dev_attr_rx_p0_hdr_egr_ovfls.attr,
411 &dev_attr_rx_p1_hdr_egr_ovfls.attr,
412 &dev_attr_rx_p2_hdr_egr_ovfls.attr,
413 &dev_attr_rx_p3_hdr_egr_ovfls.attr,
414 &dev_attr_rx_p4_hdr_egr_ovfls.attr,
415 &dev_attr_rx_p5_hdr_egr_ovfls.attr,
416 &dev_attr_rx_p6_hdr_egr_ovfls.attr,
417 &dev_attr_rx_p7_hdr_egr_ovfls.attr,
418 &dev_attr_rx_p8_hdr_egr_ovfls.attr,
419 &dev_attr_rx_pkey_mismatches.attr,
420 &dev_attr_rx_tid_full_errs.attr,
421 &dev_attr_rx_tid_valid_errs.attr,
422 &dev_attr_rx_vcrc_errs.attr,
423 &dev_attr_tx_data_pkts.attr,
424 &dev_attr_tx_dropped_pkts.attr,
425 &dev_attr_tx_dwords.attr,
426 &dev_attr_tx_flow_pkts.attr,
427 &dev_attr_tx_flow_stalls.attr,
428 &dev_attr_tx_len_errs.attr,
429 &dev_attr_tx_max_min_len_errs.attr,
430 &dev_attr_tx_underruns.attr,
431 &dev_attr_tx_unsup_vl_errs.attr,
435 static struct attribute_group dev_counter_attr_group = {
437 .attrs = dev_counter_attributes
440 static ssize_t store_reset(struct device *dev,
441 struct device_attribute *attr,
445 struct ipath_devdata *dd = dev_get_drvdata(dev);
448 if (count < 5 || memcmp(buf, "reset", 5)) {
453 if (dd->ipath_flags & IPATH_DISABLED) {
455 * post-reset init would re-enable interrupts, etc.
456 * so don't allow reset on disabled devices. Not
457 * perfect error, but about the best choice.
459 dev_info(dev,"Unit %d is disabled, can't reset\n",
463 ret = ipath_reset_device(dd->ipath_unit);
465 return ret<0 ? ret : count;
468 static ssize_t store_link_state(struct device *dev,
469 struct device_attribute *attr,
473 struct ipath_devdata *dd = dev_get_drvdata(dev);
477 ret = ipath_parse_ushort(buf, &state);
481 r = ipath_set_linkstate(dd, state);
489 ipath_dev_err(dd, "attempt to set invalid link state\n");
494 static ssize_t show_mtu(struct device *dev,
495 struct device_attribute *attr,
498 struct ipath_devdata *dd = dev_get_drvdata(dev);
499 return scnprintf(buf, PAGE_SIZE, "%u\n", dd->ipath_ibmtu);
502 static ssize_t store_mtu(struct device *dev,
503 struct device_attribute *attr,
507 struct ipath_devdata *dd = dev_get_drvdata(dev);
512 ret = ipath_parse_ushort(buf, &mtu);
516 r = ipath_set_mtu(dd, mtu);
522 ipath_dev_err(dd, "attempt to set invalid MTU\n");
527 static ssize_t show_enabled(struct device *dev,
528 struct device_attribute *attr,
531 struct ipath_devdata *dd = dev_get_drvdata(dev);
532 return scnprintf(buf, PAGE_SIZE, "%u\n",
533 (dd->ipath_flags & IPATH_DISABLED) ? 0 : 1);
536 static ssize_t store_enabled(struct device *dev,
537 struct device_attribute *attr,
541 struct ipath_devdata *dd = dev_get_drvdata(dev);
545 ret = ipath_parse_ushort(buf, &enable);
547 ipath_dev_err(dd, "attempt to use non-numeric on enable\n");
552 if (!(dd->ipath_flags & IPATH_DISABLED))
555 dev_info(dev, "Enabling unit %d\n", dd->ipath_unit);
556 /* same as post-reset */
557 ret = ipath_init_chip(dd, 1);
559 ipath_dev_err(dd, "Failed to enable unit %d\n",
562 dd->ipath_flags &= ~IPATH_DISABLED;
563 *dd->ipath_statusp &= ~IPATH_STATUS_ADMIN_DISABLED;
566 else if (!(dd->ipath_flags & IPATH_DISABLED)) {
567 dev_info(dev, "Disabling unit %d\n", dd->ipath_unit);
568 ipath_shutdown_device(dd);
569 dd->ipath_flags |= IPATH_DISABLED;
570 *dd->ipath_statusp |= IPATH_STATUS_ADMIN_DISABLED;
577 static ssize_t store_rx_pol_inv(struct device *dev,
578 struct device_attribute *attr,
582 struct ipath_devdata *dd = dev_get_drvdata(dev);
586 ret = ipath_parse_ushort(buf, &val);
590 r = ipath_set_rx_pol_inv(dd, val);
598 ipath_dev_err(dd, "attempt to set invalid Rx Polarity invert\n");
604 static DRIVER_ATTR(num_units, S_IRUGO, show_num_units, NULL);
605 static DRIVER_ATTR(version, S_IRUGO, show_version, NULL);
607 static struct attribute *driver_attributes[] = {
608 &driver_attr_num_units.attr,
609 &driver_attr_version.attr,
613 static struct attribute_group driver_attr_group = {
614 .attrs = driver_attributes
617 static DEVICE_ATTR(guid, S_IWUSR | S_IRUGO, show_guid, store_guid);
618 static DEVICE_ATTR(lid, S_IWUSR | S_IRUGO, show_lid, store_lid);
619 static DEVICE_ATTR(link_state, S_IWUSR, NULL, store_link_state);
620 static DEVICE_ATTR(mlid, S_IWUSR | S_IRUGO, show_mlid, store_mlid);
621 static DEVICE_ATTR(mtu, S_IWUSR | S_IRUGO, show_mtu, store_mtu);
622 static DEVICE_ATTR(enabled, S_IWUSR | S_IRUGO, show_enabled, store_enabled);
623 static DEVICE_ATTR(nguid, S_IRUGO, show_nguid, NULL);
624 static DEVICE_ATTR(nports, S_IRUGO, show_nports, NULL);
625 static DEVICE_ATTR(reset, S_IWUSR, NULL, store_reset);
626 static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
627 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
628 static DEVICE_ATTR(status_str, S_IRUGO, show_status_str, NULL);
629 static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
630 static DEVICE_ATTR(unit, S_IRUGO, show_unit, NULL);
631 static DEVICE_ATTR(rx_pol_inv, S_IWUSR, NULL, store_rx_pol_inv);
633 static struct attribute *dev_attributes[] = {
636 &dev_attr_link_state.attr,
639 &dev_attr_nguid.attr,
640 &dev_attr_nports.attr,
641 &dev_attr_serial.attr,
642 &dev_attr_status.attr,
643 &dev_attr_status_str.attr,
644 &dev_attr_boardversion.attr,
646 &dev_attr_enabled.attr,
647 &dev_attr_rx_pol_inv.attr,
651 static struct attribute_group dev_attr_group = {
652 .attrs = dev_attributes
656 * ipath_expose_reset - create a device reset file
657 * @dev: the device structure
659 * Only expose a file that lets us reset the device after someone
660 * enters diag mode. A device reset is quite likely to crash the
661 * machine entirely, so we don't want to normally make it
664 * Called with ipath_mutex held.
666 int ipath_expose_reset(struct device *dev)
672 ret = device_create_file(dev, &dev_attr_reset);
681 int ipath_driver_create_group(struct device_driver *drv)
685 ret = sysfs_create_group(&drv->kobj, &driver_attr_group);
690 void ipath_driver_remove_group(struct device_driver *drv)
692 sysfs_remove_group(&drv->kobj, &driver_attr_group);
695 int ipath_device_create_group(struct device *dev, struct ipath_devdata *dd)
700 ret = sysfs_create_group(&dev->kobj, &dev_attr_group);
704 ret = sysfs_create_group(&dev->kobj, &dev_counter_attr_group);
708 snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
709 ret = sysfs_create_link(&dev->driver->kobj, &dev->kobj, unit);
713 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
715 sysfs_remove_group(&dev->kobj, &dev_attr_group);
720 void ipath_device_remove_group(struct device *dev, struct ipath_devdata *dd)
724 snprintf(unit, sizeof(unit), "%02d", dd->ipath_unit);
725 sysfs_remove_link(&dev->driver->kobj, unit);
727 sysfs_remove_group(&dev->kobj, &dev_counter_attr_group);
728 sysfs_remove_group(&dev->kobj, &dev_attr_group);
730 device_remove_file(dev, &dev_attr_reset);