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