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