Merge tag 'parisc-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/parisc-2.6
[cascardo/linux.git] / drivers / infiniband / core / sysfs.c
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
4  * Copyright (c) 2005 Sun Microsystems, 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
35 #include "core_priv.h"
36
37 #include <linux/slab.h>
38 #include <linux/stat.h>
39 #include <linux/string.h>
40
41 #include <rdma/ib_mad.h>
42
43 struct ib_port {
44         struct kobject         kobj;
45         struct ib_device      *ibdev;
46         struct attribute_group gid_group;
47         struct attribute_group pkey_group;
48         u8                     port_num;
49 };
50
51 struct port_attribute {
52         struct attribute attr;
53         ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
54         ssize_t (*store)(struct ib_port *, struct port_attribute *,
55                          const char *buf, size_t count);
56 };
57
58 #define PORT_ATTR(_name, _mode, _show, _store) \
59 struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
60
61 #define PORT_ATTR_RO(_name) \
62 struct port_attribute port_attr_##_name = __ATTR_RO(_name)
63
64 struct port_table_attribute {
65         struct port_attribute   attr;
66         char                    name[8];
67         int                     index;
68 };
69
70 static ssize_t port_attr_show(struct kobject *kobj,
71                               struct attribute *attr, char *buf)
72 {
73         struct port_attribute *port_attr =
74                 container_of(attr, struct port_attribute, attr);
75         struct ib_port *p = container_of(kobj, struct ib_port, kobj);
76
77         if (!port_attr->show)
78                 return -EIO;
79
80         return port_attr->show(p, port_attr, buf);
81 }
82
83 static const struct sysfs_ops port_sysfs_ops = {
84         .show = port_attr_show
85 };
86
87 static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
88                           char *buf)
89 {
90         struct ib_port_attr attr;
91         ssize_t ret;
92
93         static const char *state_name[] = {
94                 [IB_PORT_NOP]           = "NOP",
95                 [IB_PORT_DOWN]          = "DOWN",
96                 [IB_PORT_INIT]          = "INIT",
97                 [IB_PORT_ARMED]         = "ARMED",
98                 [IB_PORT_ACTIVE]        = "ACTIVE",
99                 [IB_PORT_ACTIVE_DEFER]  = "ACTIVE_DEFER"
100         };
101
102         ret = ib_query_port(p->ibdev, p->port_num, &attr);
103         if (ret)
104                 return ret;
105
106         return sprintf(buf, "%d: %s\n", attr.state,
107                        attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
108                        state_name[attr.state] : "UNKNOWN");
109 }
110
111 static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
112                         char *buf)
113 {
114         struct ib_port_attr attr;
115         ssize_t ret;
116
117         ret = ib_query_port(p->ibdev, p->port_num, &attr);
118         if (ret)
119                 return ret;
120
121         return sprintf(buf, "0x%x\n", attr.lid);
122 }
123
124 static ssize_t lid_mask_count_show(struct ib_port *p,
125                                    struct port_attribute *unused,
126                                    char *buf)
127 {
128         struct ib_port_attr attr;
129         ssize_t ret;
130
131         ret = ib_query_port(p->ibdev, p->port_num, &attr);
132         if (ret)
133                 return ret;
134
135         return sprintf(buf, "%d\n", attr.lmc);
136 }
137
138 static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
139                            char *buf)
140 {
141         struct ib_port_attr attr;
142         ssize_t ret;
143
144         ret = ib_query_port(p->ibdev, p->port_num, &attr);
145         if (ret)
146                 return ret;
147
148         return sprintf(buf, "0x%x\n", attr.sm_lid);
149 }
150
151 static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
152                           char *buf)
153 {
154         struct ib_port_attr attr;
155         ssize_t ret;
156
157         ret = ib_query_port(p->ibdev, p->port_num, &attr);
158         if (ret)
159                 return ret;
160
161         return sprintf(buf, "%d\n", attr.sm_sl);
162 }
163
164 static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
165                              char *buf)
166 {
167         struct ib_port_attr attr;
168         ssize_t ret;
169
170         ret = ib_query_port(p->ibdev, p->port_num, &attr);
171         if (ret)
172                 return ret;
173
174         return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
175 }
176
177 static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
178                          char *buf)
179 {
180         struct ib_port_attr attr;
181         char *speed = "";
182         int rate = -1;          /* in deci-Gb/sec */
183         ssize_t ret;
184
185         ret = ib_query_port(p->ibdev, p->port_num, &attr);
186         if (ret)
187                 return ret;
188
189         switch (attr.active_speed) {
190         case IB_SPEED_SDR:
191                 rate = 25;
192                 break;
193         case IB_SPEED_DDR:
194                 speed = " DDR";
195                 rate = 50;
196                 break;
197         case IB_SPEED_QDR:
198                 speed = " QDR";
199                 rate = 100;
200                 break;
201         case IB_SPEED_FDR10:
202                 speed = " FDR10";
203                 rate = 100;
204                 break;
205         case IB_SPEED_FDR:
206                 speed = " FDR";
207                 rate = 140;
208                 break;
209         case IB_SPEED_EDR:
210                 speed = " EDR";
211                 rate = 250;
212                 break;
213         }
214
215         rate *= ib_width_enum_to_int(attr.active_width);
216         if (rate < 0)
217                 return -EINVAL;
218
219         return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
220                        rate / 10, rate % 10 ? ".5" : "",
221                        ib_width_enum_to_int(attr.active_width), speed);
222 }
223
224 static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
225                                char *buf)
226 {
227         struct ib_port_attr attr;
228
229         ssize_t ret;
230
231         ret = ib_query_port(p->ibdev, p->port_num, &attr);
232         if (ret)
233                 return ret;
234
235         switch (attr.phys_state) {
236         case 1:  return sprintf(buf, "1: Sleep\n");
237         case 2:  return sprintf(buf, "2: Polling\n");
238         case 3:  return sprintf(buf, "3: Disabled\n");
239         case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
240         case 5:  return sprintf(buf, "5: LinkUp\n");
241         case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
242         case 7:  return sprintf(buf, "7: Phy Test\n");
243         default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
244         }
245 }
246
247 static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
248                                char *buf)
249 {
250         switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
251         case IB_LINK_LAYER_INFINIBAND:
252                 return sprintf(buf, "%s\n", "InfiniBand");
253         case IB_LINK_LAYER_ETHERNET:
254                 return sprintf(buf, "%s\n", "Ethernet");
255         default:
256                 return sprintf(buf, "%s\n", "Unknown");
257         }
258 }
259
260 static PORT_ATTR_RO(state);
261 static PORT_ATTR_RO(lid);
262 static PORT_ATTR_RO(lid_mask_count);
263 static PORT_ATTR_RO(sm_lid);
264 static PORT_ATTR_RO(sm_sl);
265 static PORT_ATTR_RO(cap_mask);
266 static PORT_ATTR_RO(rate);
267 static PORT_ATTR_RO(phys_state);
268 static PORT_ATTR_RO(link_layer);
269
270 static struct attribute *port_default_attrs[] = {
271         &port_attr_state.attr,
272         &port_attr_lid.attr,
273         &port_attr_lid_mask_count.attr,
274         &port_attr_sm_lid.attr,
275         &port_attr_sm_sl.attr,
276         &port_attr_cap_mask.attr,
277         &port_attr_rate.attr,
278         &port_attr_phys_state.attr,
279         &port_attr_link_layer.attr,
280         NULL
281 };
282
283 static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
284                              char *buf)
285 {
286         struct port_table_attribute *tab_attr =
287                 container_of(attr, struct port_table_attribute, attr);
288         union ib_gid gid;
289         ssize_t ret;
290
291         ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
292         if (ret)
293                 return ret;
294
295         return sprintf(buf, "%pI6\n", gid.raw);
296 }
297
298 static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
299                               char *buf)
300 {
301         struct port_table_attribute *tab_attr =
302                 container_of(attr, struct port_table_attribute, attr);
303         u16 pkey;
304         ssize_t ret;
305
306         ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
307         if (ret)
308                 return ret;
309
310         return sprintf(buf, "0x%04x\n", pkey);
311 }
312
313 #define PORT_PMA_ATTR(_name, _counter, _width, _offset)                 \
314 struct port_table_attribute port_pma_attr_##_name = {                   \
315         .attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),        \
316         .index = (_offset) | ((_width) << 16) | ((_counter) << 24)      \
317 }
318
319 static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
320                                 char *buf)
321 {
322         struct port_table_attribute *tab_attr =
323                 container_of(attr, struct port_table_attribute, attr);
324         int offset = tab_attr->index & 0xffff;
325         int width  = (tab_attr->index >> 16) & 0xff;
326         struct ib_mad *in_mad  = NULL;
327         struct ib_mad *out_mad = NULL;
328         ssize_t ret;
329
330         if (!p->ibdev->process_mad)
331                 return sprintf(buf, "N/A (no PMA)\n");
332
333         in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
334         out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
335         if (!in_mad || !out_mad) {
336                 ret = -ENOMEM;
337                 goto out;
338         }
339
340         in_mad->mad_hdr.base_version  = 1;
341         in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
342         in_mad->mad_hdr.class_version = 1;
343         in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
344         in_mad->mad_hdr.attr_id       = cpu_to_be16(0x12); /* PortCounters */
345
346         in_mad->data[41] = p->port_num; /* PortSelect field */
347
348         if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
349                  p->port_num, NULL, NULL, in_mad, out_mad) &
350              (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
351             (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
352                 ret = -EINVAL;
353                 goto out;
354         }
355
356         switch (width) {
357         case 4:
358                 ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
359                                             (4 - (offset % 8))) & 0xf);
360                 break;
361         case 8:
362                 ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
363                 break;
364         case 16:
365                 ret = sprintf(buf, "%u\n",
366                               be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
367                 break;
368         case 32:
369                 ret = sprintf(buf, "%u\n",
370                               be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
371                 break;
372         default:
373                 ret = 0;
374         }
375
376 out:
377         kfree(in_mad);
378         kfree(out_mad);
379
380         return ret;
381 }
382
383 static PORT_PMA_ATTR(symbol_error                   ,  0, 16,  32);
384 static PORT_PMA_ATTR(link_error_recovery            ,  1,  8,  48);
385 static PORT_PMA_ATTR(link_downed                    ,  2,  8,  56);
386 static PORT_PMA_ATTR(port_rcv_errors                ,  3, 16,  64);
387 static PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
388 static PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
389 static PORT_PMA_ATTR(port_xmit_discards             ,  6, 16, 112);
390 static PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
391 static PORT_PMA_ATTR(port_rcv_constraint_errors     ,  8,  8, 136);
392 static PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
393 static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
394 static PORT_PMA_ATTR(VL15_dropped                   , 11, 16, 176);
395 static PORT_PMA_ATTR(port_xmit_data                 , 12, 32, 192);
396 static PORT_PMA_ATTR(port_rcv_data                  , 13, 32, 224);
397 static PORT_PMA_ATTR(port_xmit_packets              , 14, 32, 256);
398 static PORT_PMA_ATTR(port_rcv_packets               , 15, 32, 288);
399
400 static struct attribute *pma_attrs[] = {
401         &port_pma_attr_symbol_error.attr.attr,
402         &port_pma_attr_link_error_recovery.attr.attr,
403         &port_pma_attr_link_downed.attr.attr,
404         &port_pma_attr_port_rcv_errors.attr.attr,
405         &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
406         &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
407         &port_pma_attr_port_xmit_discards.attr.attr,
408         &port_pma_attr_port_xmit_constraint_errors.attr.attr,
409         &port_pma_attr_port_rcv_constraint_errors.attr.attr,
410         &port_pma_attr_local_link_integrity_errors.attr.attr,
411         &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
412         &port_pma_attr_VL15_dropped.attr.attr,
413         &port_pma_attr_port_xmit_data.attr.attr,
414         &port_pma_attr_port_rcv_data.attr.attr,
415         &port_pma_attr_port_xmit_packets.attr.attr,
416         &port_pma_attr_port_rcv_packets.attr.attr,
417         NULL
418 };
419
420 static struct attribute_group pma_group = {
421         .name  = "counters",
422         .attrs  = pma_attrs
423 };
424
425 static void ib_port_release(struct kobject *kobj)
426 {
427         struct ib_port *p = container_of(kobj, struct ib_port, kobj);
428         struct attribute *a;
429         int i;
430
431         for (i = 0; (a = p->gid_group.attrs[i]); ++i)
432                 kfree(a);
433
434         kfree(p->gid_group.attrs);
435
436         for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
437                 kfree(a);
438
439         kfree(p->pkey_group.attrs);
440
441         kfree(p);
442 }
443
444 static struct kobj_type port_type = {
445         .release       = ib_port_release,
446         .sysfs_ops     = &port_sysfs_ops,
447         .default_attrs = port_default_attrs
448 };
449
450 static void ib_device_release(struct device *device)
451 {
452         struct ib_device *dev = container_of(device, struct ib_device, dev);
453
454         kfree(dev);
455 }
456
457 static int ib_device_uevent(struct device *device,
458                             struct kobj_uevent_env *env)
459 {
460         struct ib_device *dev = container_of(device, struct ib_device, dev);
461
462         if (add_uevent_var(env, "NAME=%s", dev->name))
463                 return -ENOMEM;
464
465         /*
466          * It would be nice to pass the node GUID with the event...
467          */
468
469         return 0;
470 }
471
472 static struct attribute **
473 alloc_group_attrs(ssize_t (*show)(struct ib_port *,
474                                   struct port_attribute *, char *buf),
475                   int len)
476 {
477         struct attribute **tab_attr;
478         struct port_table_attribute *element;
479         int i;
480
481         tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
482         if (!tab_attr)
483                 return NULL;
484
485         for (i = 0; i < len; i++) {
486                 element = kzalloc(sizeof(struct port_table_attribute),
487                                   GFP_KERNEL);
488                 if (!element)
489                         goto err;
490
491                 if (snprintf(element->name, sizeof(element->name),
492                              "%d", i) >= sizeof(element->name)) {
493                         kfree(element);
494                         goto err;
495                 }
496
497                 element->attr.attr.name  = element->name;
498                 element->attr.attr.mode  = S_IRUGO;
499                 element->attr.show       = show;
500                 element->index           = i;
501                 sysfs_attr_init(&element->attr.attr);
502
503                 tab_attr[i] = &element->attr.attr;
504         }
505
506         return tab_attr;
507
508 err:
509         while (--i >= 0)
510                 kfree(tab_attr[i]);
511         kfree(tab_attr);
512         return NULL;
513 }
514
515 static int add_port(struct ib_device *device, int port_num,
516                     int (*port_callback)(struct ib_device *,
517                                          u8, struct kobject *))
518 {
519         struct ib_port *p;
520         struct ib_port_attr attr;
521         int i;
522         int ret;
523
524         ret = ib_query_port(device, port_num, &attr);
525         if (ret)
526                 return ret;
527
528         p = kzalloc(sizeof *p, GFP_KERNEL);
529         if (!p)
530                 return -ENOMEM;
531
532         p->ibdev      = device;
533         p->port_num   = port_num;
534
535         ret = kobject_init_and_add(&p->kobj, &port_type,
536                                    kobject_get(device->ports_parent),
537                                    "%d", port_num);
538         if (ret)
539                 goto err_put;
540
541         ret = sysfs_create_group(&p->kobj, &pma_group);
542         if (ret)
543                 goto err_put;
544
545         p->gid_group.name  = "gids";
546         p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
547         if (!p->gid_group.attrs)
548                 goto err_remove_pma;
549
550         ret = sysfs_create_group(&p->kobj, &p->gid_group);
551         if (ret)
552                 goto err_free_gid;
553
554         p->pkey_group.name  = "pkeys";
555         p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
556                                                 attr.pkey_tbl_len);
557         if (!p->pkey_group.attrs)
558                 goto err_remove_gid;
559
560         ret = sysfs_create_group(&p->kobj, &p->pkey_group);
561         if (ret)
562                 goto err_free_pkey;
563
564         if (port_callback) {
565                 ret = port_callback(device, port_num, &p->kobj);
566                 if (ret)
567                         goto err_remove_pkey;
568         }
569
570         list_add_tail(&p->kobj.entry, &device->port_list);
571
572         kobject_uevent(&p->kobj, KOBJ_ADD);
573         return 0;
574
575 err_remove_pkey:
576         sysfs_remove_group(&p->kobj, &p->pkey_group);
577
578 err_free_pkey:
579         for (i = 0; i < attr.pkey_tbl_len; ++i)
580                 kfree(p->pkey_group.attrs[i]);
581
582         kfree(p->pkey_group.attrs);
583
584 err_remove_gid:
585         sysfs_remove_group(&p->kobj, &p->gid_group);
586
587 err_free_gid:
588         for (i = 0; i < attr.gid_tbl_len; ++i)
589                 kfree(p->gid_group.attrs[i]);
590
591         kfree(p->gid_group.attrs);
592
593 err_remove_pma:
594         sysfs_remove_group(&p->kobj, &pma_group);
595
596 err_put:
597         kobject_put(device->ports_parent);
598         kfree(p);
599         return ret;
600 }
601
602 static ssize_t show_node_type(struct device *device,
603                               struct device_attribute *attr, char *buf)
604 {
605         struct ib_device *dev = container_of(device, struct ib_device, dev);
606
607         switch (dev->node_type) {
608         case RDMA_NODE_IB_CA:     return sprintf(buf, "%d: CA\n", dev->node_type);
609         case RDMA_NODE_RNIC:      return sprintf(buf, "%d: RNIC\n", dev->node_type);
610         case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
611         case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
612         default:                  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
613         }
614 }
615
616 static ssize_t show_sys_image_guid(struct device *device,
617                                    struct device_attribute *dev_attr, char *buf)
618 {
619         struct ib_device *dev = container_of(device, struct ib_device, dev);
620         struct ib_device_attr attr;
621         ssize_t ret;
622
623         ret = ib_query_device(dev, &attr);
624         if (ret)
625                 return ret;
626
627         return sprintf(buf, "%04x:%04x:%04x:%04x\n",
628                        be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
629                        be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
630                        be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
631                        be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
632 }
633
634 static ssize_t show_node_guid(struct device *device,
635                               struct device_attribute *attr, char *buf)
636 {
637         struct ib_device *dev = container_of(device, struct ib_device, dev);
638
639         return sprintf(buf, "%04x:%04x:%04x:%04x\n",
640                        be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
641                        be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
642                        be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
643                        be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
644 }
645
646 static ssize_t show_node_desc(struct device *device,
647                               struct device_attribute *attr, char *buf)
648 {
649         struct ib_device *dev = container_of(device, struct ib_device, dev);
650
651         return sprintf(buf, "%.64s\n", dev->node_desc);
652 }
653
654 static ssize_t set_node_desc(struct device *device,
655                              struct device_attribute *attr,
656                              const char *buf, size_t count)
657 {
658         struct ib_device *dev = container_of(device, struct ib_device, dev);
659         struct ib_device_modify desc = {};
660         int ret;
661
662         if (!dev->modify_device)
663                 return -EIO;
664
665         memcpy(desc.node_desc, buf, min_t(int, count, 64));
666         ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
667         if (ret)
668                 return ret;
669
670         return count;
671 }
672
673 static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
674 static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
675 static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
676 static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
677
678 static struct device_attribute *ib_class_attributes[] = {
679         &dev_attr_node_type,
680         &dev_attr_sys_image_guid,
681         &dev_attr_node_guid,
682         &dev_attr_node_desc
683 };
684
685 static struct class ib_class = {
686         .name    = "infiniband",
687         .dev_release = ib_device_release,
688         .dev_uevent = ib_device_uevent,
689 };
690
691 /* Show a given an attribute in the statistics group */
692 static ssize_t show_protocol_stat(const struct device *device,
693                             struct device_attribute *attr, char *buf,
694                             unsigned offset)
695 {
696         struct ib_device *dev = container_of(device, struct ib_device, dev);
697         union rdma_protocol_stats stats;
698         ssize_t ret;
699
700         ret = dev->get_protocol_stats(dev, &stats);
701         if (ret)
702                 return ret;
703
704         return sprintf(buf, "%llu\n",
705                        (unsigned long long) ((u64 *) &stats)[offset]);
706 }
707
708 /* generate a read-only iwarp statistics attribute */
709 #define IW_STATS_ENTRY(name)                                            \
710 static ssize_t show_##name(struct device *device,                       \
711                            struct device_attribute *attr, char *buf)    \
712 {                                                                       \
713         return show_protocol_stat(device, attr, buf,                    \
714                                   offsetof(struct iw_protocol_stats, name) / \
715                                   sizeof (u64));                        \
716 }                                                                       \
717 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
718
719 IW_STATS_ENTRY(ipInReceives);
720 IW_STATS_ENTRY(ipInHdrErrors);
721 IW_STATS_ENTRY(ipInTooBigErrors);
722 IW_STATS_ENTRY(ipInNoRoutes);
723 IW_STATS_ENTRY(ipInAddrErrors);
724 IW_STATS_ENTRY(ipInUnknownProtos);
725 IW_STATS_ENTRY(ipInTruncatedPkts);
726 IW_STATS_ENTRY(ipInDiscards);
727 IW_STATS_ENTRY(ipInDelivers);
728 IW_STATS_ENTRY(ipOutForwDatagrams);
729 IW_STATS_ENTRY(ipOutRequests);
730 IW_STATS_ENTRY(ipOutDiscards);
731 IW_STATS_ENTRY(ipOutNoRoutes);
732 IW_STATS_ENTRY(ipReasmTimeout);
733 IW_STATS_ENTRY(ipReasmReqds);
734 IW_STATS_ENTRY(ipReasmOKs);
735 IW_STATS_ENTRY(ipReasmFails);
736 IW_STATS_ENTRY(ipFragOKs);
737 IW_STATS_ENTRY(ipFragFails);
738 IW_STATS_ENTRY(ipFragCreates);
739 IW_STATS_ENTRY(ipInMcastPkts);
740 IW_STATS_ENTRY(ipOutMcastPkts);
741 IW_STATS_ENTRY(ipInBcastPkts);
742 IW_STATS_ENTRY(ipOutBcastPkts);
743 IW_STATS_ENTRY(tcpRtoAlgorithm);
744 IW_STATS_ENTRY(tcpRtoMin);
745 IW_STATS_ENTRY(tcpRtoMax);
746 IW_STATS_ENTRY(tcpMaxConn);
747 IW_STATS_ENTRY(tcpActiveOpens);
748 IW_STATS_ENTRY(tcpPassiveOpens);
749 IW_STATS_ENTRY(tcpAttemptFails);
750 IW_STATS_ENTRY(tcpEstabResets);
751 IW_STATS_ENTRY(tcpCurrEstab);
752 IW_STATS_ENTRY(tcpInSegs);
753 IW_STATS_ENTRY(tcpOutSegs);
754 IW_STATS_ENTRY(tcpRetransSegs);
755 IW_STATS_ENTRY(tcpInErrs);
756 IW_STATS_ENTRY(tcpOutRsts);
757
758 static struct attribute *iw_proto_stats_attrs[] = {
759         &dev_attr_ipInReceives.attr,
760         &dev_attr_ipInHdrErrors.attr,
761         &dev_attr_ipInTooBigErrors.attr,
762         &dev_attr_ipInNoRoutes.attr,
763         &dev_attr_ipInAddrErrors.attr,
764         &dev_attr_ipInUnknownProtos.attr,
765         &dev_attr_ipInTruncatedPkts.attr,
766         &dev_attr_ipInDiscards.attr,
767         &dev_attr_ipInDelivers.attr,
768         &dev_attr_ipOutForwDatagrams.attr,
769         &dev_attr_ipOutRequests.attr,
770         &dev_attr_ipOutDiscards.attr,
771         &dev_attr_ipOutNoRoutes.attr,
772         &dev_attr_ipReasmTimeout.attr,
773         &dev_attr_ipReasmReqds.attr,
774         &dev_attr_ipReasmOKs.attr,
775         &dev_attr_ipReasmFails.attr,
776         &dev_attr_ipFragOKs.attr,
777         &dev_attr_ipFragFails.attr,
778         &dev_attr_ipFragCreates.attr,
779         &dev_attr_ipInMcastPkts.attr,
780         &dev_attr_ipOutMcastPkts.attr,
781         &dev_attr_ipInBcastPkts.attr,
782         &dev_attr_ipOutBcastPkts.attr,
783         &dev_attr_tcpRtoAlgorithm.attr,
784         &dev_attr_tcpRtoMin.attr,
785         &dev_attr_tcpRtoMax.attr,
786         &dev_attr_tcpMaxConn.attr,
787         &dev_attr_tcpActiveOpens.attr,
788         &dev_attr_tcpPassiveOpens.attr,
789         &dev_attr_tcpAttemptFails.attr,
790         &dev_attr_tcpEstabResets.attr,
791         &dev_attr_tcpCurrEstab.attr,
792         &dev_attr_tcpInSegs.attr,
793         &dev_attr_tcpOutSegs.attr,
794         &dev_attr_tcpRetransSegs.attr,
795         &dev_attr_tcpInErrs.attr,
796         &dev_attr_tcpOutRsts.attr,
797         NULL
798 };
799
800 static struct attribute_group iw_stats_group = {
801         .name   = "proto_stats",
802         .attrs  = iw_proto_stats_attrs,
803 };
804
805 int ib_device_register_sysfs(struct ib_device *device,
806                              int (*port_callback)(struct ib_device *,
807                                                   u8, struct kobject *))
808 {
809         struct device *class_dev = &device->dev;
810         int ret;
811         int i;
812
813         class_dev->class      = &ib_class;
814         class_dev->parent     = device->dma_device;
815         dev_set_name(class_dev, device->name);
816         dev_set_drvdata(class_dev, device);
817
818         INIT_LIST_HEAD(&device->port_list);
819
820         ret = device_register(class_dev);
821         if (ret)
822                 goto err;
823
824         for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
825                 ret = device_create_file(class_dev, ib_class_attributes[i]);
826                 if (ret)
827                         goto err_unregister;
828         }
829
830         device->ports_parent = kobject_create_and_add("ports",
831                                         kobject_get(&class_dev->kobj));
832         if (!device->ports_parent) {
833                 ret = -ENOMEM;
834                 goto err_put;
835         }
836
837         if (device->node_type == RDMA_NODE_IB_SWITCH) {
838                 ret = add_port(device, 0, port_callback);
839                 if (ret)
840                         goto err_put;
841         } else {
842                 for (i = 1; i <= device->phys_port_cnt; ++i) {
843                         ret = add_port(device, i, port_callback);
844                         if (ret)
845                                 goto err_put;
846                 }
847         }
848
849         if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
850                 ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
851                 if (ret)
852                         goto err_put;
853         }
854
855         return 0;
856
857 err_put:
858         {
859                 struct kobject *p, *t;
860                 struct ib_port *port;
861
862                 list_for_each_entry_safe(p, t, &device->port_list, entry) {
863                         list_del(&p->entry);
864                         port = container_of(p, struct ib_port, kobj);
865                         sysfs_remove_group(p, &pma_group);
866                         sysfs_remove_group(p, &port->pkey_group);
867                         sysfs_remove_group(p, &port->gid_group);
868                         kobject_put(p);
869                 }
870         }
871
872         kobject_put(&class_dev->kobj);
873
874 err_unregister:
875         device_unregister(class_dev);
876
877 err:
878         return ret;
879 }
880
881 void ib_device_unregister_sysfs(struct ib_device *device)
882 {
883         struct kobject *p, *t;
884         struct ib_port *port;
885
886         /* Hold kobject until ib_dealloc_device() */
887         kobject_get(&device->dev.kobj);
888
889         list_for_each_entry_safe(p, t, &device->port_list, entry) {
890                 list_del(&p->entry);
891                 port = container_of(p, struct ib_port, kobj);
892                 sysfs_remove_group(p, &pma_group);
893                 sysfs_remove_group(p, &port->pkey_group);
894                 sysfs_remove_group(p, &port->gid_group);
895                 kobject_put(p);
896         }
897
898         kobject_put(device->ports_parent);
899         device_unregister(&device->dev);
900 }
901
902 int ib_sysfs_setup(void)
903 {
904         return class_register(&ib_class);
905 }
906
907 void ib_sysfs_cleanup(void)
908 {
909         class_unregister(&ib_class);
910 }