3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 Intel Corporation.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 #include <linux/ctype.h>
58 * Start of per-port congestion control structures and support code
62 * Congestion control table size followed by table entries
64 static ssize_t read_cc_table_bin(struct file *filp, struct kobject *kobj,
65 struct bin_attribute *bin_attr,
66 char *buf, loff_t pos, size_t count)
69 struct hfi1_pportdata *ppd =
70 container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
71 struct cc_state *cc_state;
73 ret = ppd->total_cct_entry * sizeof(struct ib_cc_table_entry_shadow)
79 if (count > ret - pos)
86 cc_state = get_cc_state(ppd);
87 if (cc_state == NULL) {
91 memcpy(buf, &cc_state->cct, count);
97 static void port_release(struct kobject *kobj)
99 /* nothing to do since memory is freed by hfi1_free_devdata() */
102 static struct kobj_type port_cc_ktype = {
103 .release = port_release,
106 static struct bin_attribute cc_table_bin_attr = {
107 .attr = {.name = "cc_table_bin", .mode = 0444},
108 .read = read_cc_table_bin,
113 * Congestion settings: port control, control map and an array of 16
114 * entries for the congestion entries - increase, timer, event log
115 * trigger threshold and the minimum injection rate delay.
117 static ssize_t read_cc_setting_bin(struct file *filp, struct kobject *kobj,
118 struct bin_attribute *bin_attr,
119 char *buf, loff_t pos, size_t count)
122 struct hfi1_pportdata *ppd =
123 container_of(kobj, struct hfi1_pportdata, pport_cc_kobj);
124 struct cc_state *cc_state;
126 ret = sizeof(struct opa_congestion_setting_attr_shadow);
130 if (count > ret - pos)
137 cc_state = get_cc_state(ppd);
138 if (cc_state == NULL) {
142 memcpy(buf, &cc_state->cong_setting, count);
148 static struct bin_attribute cc_setting_bin_attr = {
149 .attr = {.name = "cc_settings_bin", .mode = 0444},
150 .read = read_cc_setting_bin,
155 #define HFI1_SC2VL_ATTR(N) \
156 static struct hfi1_sc2vl_attr hfi1_sc2vl_attr_##N = { \
157 .attr = { .name = __stringify(N), .mode = 0444 }, \
161 struct hfi1_sc2vl_attr {
162 struct attribute attr;
200 static struct attribute *sc2vl_default_attributes[] = {
201 &hfi1_sc2vl_attr_0.attr,
202 &hfi1_sc2vl_attr_1.attr,
203 &hfi1_sc2vl_attr_2.attr,
204 &hfi1_sc2vl_attr_3.attr,
205 &hfi1_sc2vl_attr_4.attr,
206 &hfi1_sc2vl_attr_5.attr,
207 &hfi1_sc2vl_attr_6.attr,
208 &hfi1_sc2vl_attr_7.attr,
209 &hfi1_sc2vl_attr_8.attr,
210 &hfi1_sc2vl_attr_9.attr,
211 &hfi1_sc2vl_attr_10.attr,
212 &hfi1_sc2vl_attr_11.attr,
213 &hfi1_sc2vl_attr_12.attr,
214 &hfi1_sc2vl_attr_13.attr,
215 &hfi1_sc2vl_attr_14.attr,
216 &hfi1_sc2vl_attr_15.attr,
217 &hfi1_sc2vl_attr_16.attr,
218 &hfi1_sc2vl_attr_17.attr,
219 &hfi1_sc2vl_attr_18.attr,
220 &hfi1_sc2vl_attr_19.attr,
221 &hfi1_sc2vl_attr_20.attr,
222 &hfi1_sc2vl_attr_21.attr,
223 &hfi1_sc2vl_attr_22.attr,
224 &hfi1_sc2vl_attr_23.attr,
225 &hfi1_sc2vl_attr_24.attr,
226 &hfi1_sc2vl_attr_25.attr,
227 &hfi1_sc2vl_attr_26.attr,
228 &hfi1_sc2vl_attr_27.attr,
229 &hfi1_sc2vl_attr_28.attr,
230 &hfi1_sc2vl_attr_29.attr,
231 &hfi1_sc2vl_attr_30.attr,
232 &hfi1_sc2vl_attr_31.attr,
236 static ssize_t sc2vl_attr_show(struct kobject *kobj, struct attribute *attr,
239 struct hfi1_sc2vl_attr *sattr =
240 container_of(attr, struct hfi1_sc2vl_attr, attr);
241 struct hfi1_pportdata *ppd =
242 container_of(kobj, struct hfi1_pportdata, sc2vl_kobj);
243 struct hfi1_devdata *dd = ppd->dd;
245 return sprintf(buf, "%u\n", *((u8 *)dd->sc2vl + sattr->sc));
248 static const struct sysfs_ops hfi1_sc2vl_ops = {
249 .show = sc2vl_attr_show,
252 static struct kobj_type hfi1_sc2vl_ktype = {
253 .release = port_release,
254 .sysfs_ops = &hfi1_sc2vl_ops,
255 .default_attrs = sc2vl_default_attributes
261 #define HFI1_SL2SC_ATTR(N) \
262 static struct hfi1_sl2sc_attr hfi1_sl2sc_attr_##N = { \
263 .attr = { .name = __stringify(N), .mode = 0444 }, \
267 struct hfi1_sl2sc_attr {
268 struct attribute attr;
306 static struct attribute *sl2sc_default_attributes[] = {
307 &hfi1_sl2sc_attr_0.attr,
308 &hfi1_sl2sc_attr_1.attr,
309 &hfi1_sl2sc_attr_2.attr,
310 &hfi1_sl2sc_attr_3.attr,
311 &hfi1_sl2sc_attr_4.attr,
312 &hfi1_sl2sc_attr_5.attr,
313 &hfi1_sl2sc_attr_6.attr,
314 &hfi1_sl2sc_attr_7.attr,
315 &hfi1_sl2sc_attr_8.attr,
316 &hfi1_sl2sc_attr_9.attr,
317 &hfi1_sl2sc_attr_10.attr,
318 &hfi1_sl2sc_attr_11.attr,
319 &hfi1_sl2sc_attr_12.attr,
320 &hfi1_sl2sc_attr_13.attr,
321 &hfi1_sl2sc_attr_14.attr,
322 &hfi1_sl2sc_attr_15.attr,
323 &hfi1_sl2sc_attr_16.attr,
324 &hfi1_sl2sc_attr_17.attr,
325 &hfi1_sl2sc_attr_18.attr,
326 &hfi1_sl2sc_attr_19.attr,
327 &hfi1_sl2sc_attr_20.attr,
328 &hfi1_sl2sc_attr_21.attr,
329 &hfi1_sl2sc_attr_22.attr,
330 &hfi1_sl2sc_attr_23.attr,
331 &hfi1_sl2sc_attr_24.attr,
332 &hfi1_sl2sc_attr_25.attr,
333 &hfi1_sl2sc_attr_26.attr,
334 &hfi1_sl2sc_attr_27.attr,
335 &hfi1_sl2sc_attr_28.attr,
336 &hfi1_sl2sc_attr_29.attr,
337 &hfi1_sl2sc_attr_30.attr,
338 &hfi1_sl2sc_attr_31.attr,
342 static ssize_t sl2sc_attr_show(struct kobject *kobj, struct attribute *attr,
345 struct hfi1_sl2sc_attr *sattr =
346 container_of(attr, struct hfi1_sl2sc_attr, attr);
347 struct hfi1_pportdata *ppd =
348 container_of(kobj, struct hfi1_pportdata, sl2sc_kobj);
349 struct hfi1_ibport *ibp = &ppd->ibport_data;
351 return sprintf(buf, "%u\n", ibp->sl_to_sc[sattr->sl]);
354 static const struct sysfs_ops hfi1_sl2sc_ops = {
355 .show = sl2sc_attr_show,
358 static struct kobj_type hfi1_sl2sc_ktype = {
359 .release = port_release,
360 .sysfs_ops = &hfi1_sl2sc_ops,
361 .default_attrs = sl2sc_default_attributes
368 #define HFI1_VL2MTU_ATTR(N) \
369 static struct hfi1_vl2mtu_attr hfi1_vl2mtu_attr_##N = { \
370 .attr = { .name = __stringify(N), .mode = 0444 }, \
374 struct hfi1_vl2mtu_attr {
375 struct attribute attr;
389 HFI1_VL2MTU_ATTR(10);
390 HFI1_VL2MTU_ATTR(11);
391 HFI1_VL2MTU_ATTR(12);
392 HFI1_VL2MTU_ATTR(13);
393 HFI1_VL2MTU_ATTR(14);
394 HFI1_VL2MTU_ATTR(15);
396 static struct attribute *vl2mtu_default_attributes[] = {
397 &hfi1_vl2mtu_attr_0.attr,
398 &hfi1_vl2mtu_attr_1.attr,
399 &hfi1_vl2mtu_attr_2.attr,
400 &hfi1_vl2mtu_attr_3.attr,
401 &hfi1_vl2mtu_attr_4.attr,
402 &hfi1_vl2mtu_attr_5.attr,
403 &hfi1_vl2mtu_attr_6.attr,
404 &hfi1_vl2mtu_attr_7.attr,
405 &hfi1_vl2mtu_attr_8.attr,
406 &hfi1_vl2mtu_attr_9.attr,
407 &hfi1_vl2mtu_attr_10.attr,
408 &hfi1_vl2mtu_attr_11.attr,
409 &hfi1_vl2mtu_attr_12.attr,
410 &hfi1_vl2mtu_attr_13.attr,
411 &hfi1_vl2mtu_attr_14.attr,
412 &hfi1_vl2mtu_attr_15.attr,
416 static ssize_t vl2mtu_attr_show(struct kobject *kobj, struct attribute *attr,
419 struct hfi1_vl2mtu_attr *vlattr =
420 container_of(attr, struct hfi1_vl2mtu_attr, attr);
421 struct hfi1_pportdata *ppd =
422 container_of(kobj, struct hfi1_pportdata, vl2mtu_kobj);
423 struct hfi1_devdata *dd = ppd->dd;
425 return sprintf(buf, "%u\n", dd->vld[vlattr->vl].mtu);
428 static const struct sysfs_ops hfi1_vl2mtu_ops = {
429 .show = vl2mtu_attr_show,
432 static struct kobj_type hfi1_vl2mtu_ktype = {
433 .release = port_release,
434 .sysfs_ops = &hfi1_vl2mtu_ops,
435 .default_attrs = vl2mtu_default_attributes
439 /* end of per-port file structures and support code */
442 * Start of per-unit (or driver, in some cases, but replicated
443 * per unit) functions (these get a device *)
445 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
448 struct hfi1_ibdev *dev =
449 container_of(device, struct hfi1_ibdev, ibdev.dev);
451 return sprintf(buf, "%x\n", dd_from_dev(dev)->minrev);
454 static ssize_t show_hfi(struct device *device, struct device_attribute *attr,
457 struct hfi1_ibdev *dev =
458 container_of(device, struct hfi1_ibdev, ibdev.dev);
459 struct hfi1_devdata *dd = dd_from_dev(dev);
465 ret = scnprintf(buf, PAGE_SIZE, "%s\n", dd->boardname);
469 static ssize_t show_boardversion(struct device *device,
470 struct device_attribute *attr, char *buf)
472 struct hfi1_ibdev *dev =
473 container_of(device, struct hfi1_ibdev, ibdev.dev);
474 struct hfi1_devdata *dd = dd_from_dev(dev);
476 /* The string printed here is already newline-terminated. */
477 return scnprintf(buf, PAGE_SIZE, "%s", dd->boardversion);
481 static ssize_t show_nctxts(struct device *device,
482 struct device_attribute *attr, char *buf)
484 struct hfi1_ibdev *dev =
485 container_of(device, struct hfi1_ibdev, ibdev.dev);
486 struct hfi1_devdata *dd = dd_from_dev(dev);
489 * Return the smaller of send and receive contexts.
490 * Normally, user level applications would require both a send
491 * and a receive context, so returning the smaller of the two counts
492 * give a more accurate picture of total contexts available.
494 return scnprintf(buf, PAGE_SIZE, "%u\n",
495 min(dd->num_rcv_contexts - dd->first_user_ctxt,
496 (u32)dd->sc_sizes[SC_USER].count));
499 static ssize_t show_nfreectxts(struct device *device,
500 struct device_attribute *attr, char *buf)
502 struct hfi1_ibdev *dev =
503 container_of(device, struct hfi1_ibdev, ibdev.dev);
504 struct hfi1_devdata *dd = dd_from_dev(dev);
506 /* Return the number of free user ports (contexts) available. */
507 return scnprintf(buf, PAGE_SIZE, "%u\n", dd->freectxts);
510 static ssize_t show_serial(struct device *device,
511 struct device_attribute *attr, char *buf)
513 struct hfi1_ibdev *dev =
514 container_of(device, struct hfi1_ibdev, ibdev.dev);
515 struct hfi1_devdata *dd = dd_from_dev(dev);
517 return scnprintf(buf, PAGE_SIZE, "%s", dd->serial);
521 static ssize_t store_chip_reset(struct device *device,
522 struct device_attribute *attr, const char *buf,
525 struct hfi1_ibdev *dev =
526 container_of(device, struct hfi1_ibdev, ibdev.dev);
527 struct hfi1_devdata *dd = dd_from_dev(dev);
530 if (count < 5 || memcmp(buf, "reset", 5) || !dd->diag_client) {
535 ret = hfi1_reset_device(dd->unit);
537 return ret < 0 ? ret : count;
541 * Convert the reported temperature from an integer (reported in
542 * units of 0.25C) to a floating point number.
544 #define temp2str(temp, buf, size, idx) \
545 scnprintf((buf) + (idx), (size) - (idx), "%u.%02u ", \
546 ((temp) >> 2), ((temp) & 0x3) * 25)
549 * Dump tempsense values, in decimal, to ease shell-scripts.
551 static ssize_t show_tempsense(struct device *device,
552 struct device_attribute *attr, char *buf)
554 struct hfi1_ibdev *dev =
555 container_of(device, struct hfi1_ibdev, ibdev.dev);
556 struct hfi1_devdata *dd = dd_from_dev(dev);
557 struct hfi1_temp temp;
560 ret = hfi1_tempsense_rd(dd, &temp);
564 idx += temp2str(temp.curr, buf, PAGE_SIZE, idx);
565 idx += temp2str(temp.lo_lim, buf, PAGE_SIZE, idx);
566 idx += temp2str(temp.hi_lim, buf, PAGE_SIZE, idx);
567 idx += temp2str(temp.crit_lim, buf, PAGE_SIZE, idx);
568 idx += scnprintf(buf + idx, PAGE_SIZE - idx,
569 "%u %u %u\n", temp.triggers & 0x1,
570 temp.triggers & 0x2, temp.triggers & 0x4);
577 * end of per-unit (or driver, in some cases, but replicated
578 * per unit) functions
581 /* start of per-unit file structures and support code */
582 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
583 static DEVICE_ATTR(board_id, S_IRUGO, show_hfi, NULL);
584 static DEVICE_ATTR(nctxts, S_IRUGO, show_nctxts, NULL);
585 static DEVICE_ATTR(nfreectxts, S_IRUGO, show_nfreectxts, NULL);
586 static DEVICE_ATTR(serial, S_IRUGO, show_serial, NULL);
587 static DEVICE_ATTR(boardversion, S_IRUGO, show_boardversion, NULL);
588 static DEVICE_ATTR(tempsense, S_IRUGO, show_tempsense, NULL);
589 static DEVICE_ATTR(chip_reset, S_IWUSR, NULL, store_chip_reset);
591 static struct device_attribute *hfi1_attributes[] = {
595 &dev_attr_nfreectxts,
597 &dev_attr_boardversion,
599 &dev_attr_chip_reset,
602 int hfi1_create_port_files(struct ib_device *ibdev, u8 port_num,
603 struct kobject *kobj)
605 struct hfi1_pportdata *ppd;
606 struct hfi1_devdata *dd = dd_from_ibdev(ibdev);
609 if (!port_num || port_num > dd->num_pports) {
611 "Skipping infiniband class with invalid port %u\n",
615 ppd = &dd->pport[port_num - 1];
617 ret = kobject_init_and_add(&ppd->sc2vl_kobj, &hfi1_sc2vl_ktype, kobj,
621 "Skipping sc2vl sysfs info, (err %d) port %u\n",
625 kobject_uevent(&ppd->sc2vl_kobj, KOBJ_ADD);
627 ret = kobject_init_and_add(&ppd->sl2sc_kobj, &hfi1_sl2sc_ktype, kobj,
631 "Skipping sl2sc sysfs info, (err %d) port %u\n",
635 kobject_uevent(&ppd->sl2sc_kobj, KOBJ_ADD);
637 ret = kobject_init_and_add(&ppd->vl2mtu_kobj, &hfi1_vl2mtu_ktype, kobj,
641 "Skipping vl2mtu sysfs info, (err %d) port %u\n",
645 kobject_uevent(&ppd->vl2mtu_kobj, KOBJ_ADD);
648 ret = kobject_init_and_add(&ppd->pport_cc_kobj, &port_cc_ktype,
652 "Skipping Congestion Control sysfs info, (err %d) port %u\n",
657 kobject_uevent(&ppd->pport_cc_kobj, KOBJ_ADD);
659 ret = sysfs_create_bin_file(&ppd->pport_cc_kobj,
660 &cc_setting_bin_attr);
663 "Skipping Congestion Control setting sysfs info, (err %d) port %u\n",
668 ret = sysfs_create_bin_file(&ppd->pport_cc_kobj,
672 "Skipping Congestion Control table sysfs info, (err %d) port %u\n",
674 goto bail_cc_entry_bin;
678 "IB%u: Congestion Control Agent enabled for port %d\n",
684 sysfs_remove_bin_file(&ppd->pport_cc_kobj,
685 &cc_setting_bin_attr);
687 kobject_put(&ppd->pport_cc_kobj);
689 kobject_put(&ppd->vl2mtu_kobj);
691 kobject_put(&ppd->sl2sc_kobj);
693 kobject_put(&ppd->sc2vl_kobj);
699 * Register and create our files in /sys/class/infiniband.
701 int hfi1_verbs_register_sysfs(struct hfi1_devdata *dd)
703 struct ib_device *dev = &dd->verbs_dev.ibdev;
706 for (i = 0; i < ARRAY_SIZE(hfi1_attributes); ++i) {
707 ret = device_create_file(&dev->dev, hfi1_attributes[i]);
714 for (i = 0; i < ARRAY_SIZE(hfi1_attributes); ++i)
715 device_remove_file(&dev->dev, hfi1_attributes[i]);
720 * Unregister and remove our files in /sys/class/infiniband.
722 void hfi1_verbs_unregister_sysfs(struct hfi1_devdata *dd)
724 struct hfi1_pportdata *ppd;
727 for (i = 0; i < dd->num_pports; i++) {
730 sysfs_remove_bin_file(&ppd->pport_cc_kobj,
731 &cc_setting_bin_attr);
732 sysfs_remove_bin_file(&ppd->pport_cc_kobj,
734 kobject_put(&ppd->pport_cc_kobj);
735 kobject_put(&ppd->vl2mtu_kobj);
736 kobject_put(&ppd->sl2sc_kobj);
737 kobject_put(&ppd->sc2vl_kobj);