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