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 #include <linux/netdevice.h>
41 #include <linux/ethtool.h>
42
43 #include <rdma/ib_mad.h>
44 #include <rdma/ib_pma.h>
45 #include <rdma/ib_cache.h>
46
47 struct ib_port;
48
49 struct gid_attr_group {
50 struct ib_port *port;
51 struct kobject kobj;
52 struct attribute_group ndev;
53 struct attribute_group type;
54 };
55 struct ib_port {
56 struct kobject kobj;
57 struct ib_device *ibdev;
58 struct gid_attr_group *gid_attr_group;
59 struct attribute_group gid_group;
60 struct attribute_group pkey_group;
61 struct attribute_group *pma_table;
62 struct attribute_group *hw_stats_ag;
63 struct rdma_hw_stats *hw_stats;
64 u8 port_num;
65 };
66
67 struct port_attribute {
68 struct attribute attr;
69 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
70 ssize_t (*store)(struct ib_port *, struct port_attribute *,
71 const char *buf, size_t count);
72 };
73
74 #define PORT_ATTR(_name, _mode, _show, _store) \
75 struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
76
77 #define PORT_ATTR_RO(_name) \
78 struct port_attribute port_attr_##_name = __ATTR_RO(_name)
79
80 struct port_table_attribute {
81 struct port_attribute attr;
82 char name[8];
83 int index;
84 __be16 attr_id;
85 };
86
87 struct hw_stats_attribute {
88 struct attribute attr;
89 ssize_t (*show)(struct kobject *kobj,
90 struct attribute *attr, char *buf);
91 ssize_t (*store)(struct kobject *kobj,
92 struct attribute *attr,
93 const char *buf,
94 size_t count);
95 int index;
96 u8 port_num;
97 };
98
port_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)99 static ssize_t port_attr_show(struct kobject *kobj,
100 struct attribute *attr, char *buf)
101 {
102 struct port_attribute *port_attr =
103 container_of(attr, struct port_attribute, attr);
104 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
105
106 if (!port_attr->show)
107 return -EIO;
108
109 return port_attr->show(p, port_attr, buf);
110 }
111
port_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)112 static ssize_t port_attr_store(struct kobject *kobj,
113 struct attribute *attr,
114 const char *buf, size_t count)
115 {
116 struct port_attribute *port_attr =
117 container_of(attr, struct port_attribute, attr);
118 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
119
120 if (!port_attr->store)
121 return -EIO;
122 return port_attr->store(p, port_attr, buf, count);
123 }
124
125 static const struct sysfs_ops port_sysfs_ops = {
126 .show = port_attr_show,
127 .store = port_attr_store
128 };
129
gid_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)130 static ssize_t gid_attr_show(struct kobject *kobj,
131 struct attribute *attr, char *buf)
132 {
133 struct port_attribute *port_attr =
134 container_of(attr, struct port_attribute, attr);
135 struct ib_port *p = container_of(kobj, struct gid_attr_group,
136 kobj)->port;
137
138 if (!port_attr->show)
139 return -EIO;
140
141 return port_attr->show(p, port_attr, buf);
142 }
143
144 static const struct sysfs_ops gid_attr_sysfs_ops = {
145 .show = gid_attr_show
146 };
147
state_show(struct ib_port * p,struct port_attribute * unused,char * buf)148 static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
149 char *buf)
150 {
151 struct ib_port_attr attr;
152 ssize_t ret;
153
154 static const char *state_name[] = {
155 [IB_PORT_NOP] = "NOP",
156 [IB_PORT_DOWN] = "DOWN",
157 [IB_PORT_INIT] = "INIT",
158 [IB_PORT_ARMED] = "ARMED",
159 [IB_PORT_ACTIVE] = "ACTIVE",
160 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
161 };
162
163 ret = ib_query_port(p->ibdev, p->port_num, &attr);
164 if (ret)
165 return ret;
166
167 return sprintf(buf, "%d: %s\n", attr.state,
168 attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
169 state_name[attr.state] : "UNKNOWN");
170 }
171
lid_show(struct ib_port * p,struct port_attribute * unused,char * buf)172 static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
173 char *buf)
174 {
175 struct ib_port_attr attr;
176 ssize_t ret;
177
178 ret = ib_query_port(p->ibdev, p->port_num, &attr);
179 if (ret)
180 return ret;
181
182 return sprintf(buf, "0x%x\n", attr.lid);
183 }
184
lid_mask_count_show(struct ib_port * p,struct port_attribute * unused,char * buf)185 static ssize_t lid_mask_count_show(struct ib_port *p,
186 struct port_attribute *unused,
187 char *buf)
188 {
189 struct ib_port_attr attr;
190 ssize_t ret;
191
192 ret = ib_query_port(p->ibdev, p->port_num, &attr);
193 if (ret)
194 return ret;
195
196 return sprintf(buf, "%d\n", attr.lmc);
197 }
198
sm_lid_show(struct ib_port * p,struct port_attribute * unused,char * buf)199 static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
200 char *buf)
201 {
202 struct ib_port_attr attr;
203 ssize_t ret;
204
205 ret = ib_query_port(p->ibdev, p->port_num, &attr);
206 if (ret)
207 return ret;
208
209 return sprintf(buf, "0x%x\n", attr.sm_lid);
210 }
211
sm_sl_show(struct ib_port * p,struct port_attribute * unused,char * buf)212 static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
213 char *buf)
214 {
215 struct ib_port_attr attr;
216 ssize_t ret;
217
218 ret = ib_query_port(p->ibdev, p->port_num, &attr);
219 if (ret)
220 return ret;
221
222 return sprintf(buf, "%d\n", attr.sm_sl);
223 }
224
cap_mask_show(struct ib_port * p,struct port_attribute * unused,char * buf)225 static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
226 char *buf)
227 {
228 struct ib_port_attr attr;
229 ssize_t ret;
230
231 ret = ib_query_port(p->ibdev, p->port_num, &attr);
232 if (ret)
233 return ret;
234
235 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
236 }
237
rate_show(struct ib_port * p,struct port_attribute * unused,char * buf)238 static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
239 char *buf)
240 {
241 struct ib_port_attr attr;
242 char *speed = "";
243 int rate; /* in deci-Gb/sec */
244 ssize_t ret;
245
246 ret = ib_query_port(p->ibdev, p->port_num, &attr);
247 if (ret)
248 return ret;
249
250 switch (attr.active_speed) {
251 case IB_SPEED_DDR:
252 speed = " DDR";
253 rate = 50;
254 break;
255 case IB_SPEED_QDR:
256 speed = " QDR";
257 rate = 100;
258 break;
259 case IB_SPEED_FDR10:
260 speed = " FDR10";
261 rate = 100;
262 break;
263 case IB_SPEED_FDR:
264 speed = " FDR";
265 rate = 140;
266 break;
267 case IB_SPEED_EDR:
268 speed = " EDR";
269 rate = 250;
270 break;
271 case IB_SPEED_HDR:
272 speed = " HDR";
273 rate = 500;
274 break;
275 case IB_SPEED_SDR:
276 default: /* default to SDR for invalid rates */
277 speed = " SDR";
278 rate = 25;
279 break;
280 }
281
282 rate *= ib_width_enum_to_int(attr.active_width);
283 if (rate < 0)
284 return -EINVAL;
285
286 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
287 rate / 10, rate % 10 ? ".5" : "",
288 ib_width_enum_to_int(attr.active_width), speed);
289 }
290
phys_state_show(struct ib_port * p,struct port_attribute * unused,char * buf)291 static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
292 char *buf)
293 {
294 struct ib_port_attr attr;
295
296 ssize_t ret;
297
298 ret = ib_query_port(p->ibdev, p->port_num, &attr);
299 if (ret)
300 return ret;
301
302 switch (attr.phys_state) {
303 case 1: return sprintf(buf, "1: Sleep\n");
304 case 2: return sprintf(buf, "2: Polling\n");
305 case 3: return sprintf(buf, "3: Disabled\n");
306 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
307 case 5: return sprintf(buf, "5: LinkUp\n");
308 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
309 case 7: return sprintf(buf, "7: Phy Test\n");
310 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
311 }
312 }
313
link_layer_show(struct ib_port * p,struct port_attribute * unused,char * buf)314 static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
315 char *buf)
316 {
317 switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
318 case IB_LINK_LAYER_INFINIBAND:
319 return sprintf(buf, "%s\n", "InfiniBand");
320 case IB_LINK_LAYER_ETHERNET:
321 return sprintf(buf, "%s\n", "Ethernet");
322 default:
323 return sprintf(buf, "%s\n", "Unknown");
324 }
325 }
326
327 static PORT_ATTR_RO(state);
328 static PORT_ATTR_RO(lid);
329 static PORT_ATTR_RO(lid_mask_count);
330 static PORT_ATTR_RO(sm_lid);
331 static PORT_ATTR_RO(sm_sl);
332 static PORT_ATTR_RO(cap_mask);
333 static PORT_ATTR_RO(rate);
334 static PORT_ATTR_RO(phys_state);
335 static PORT_ATTR_RO(link_layer);
336
337 static struct attribute *port_default_attrs[] = {
338 &port_attr_state.attr,
339 &port_attr_lid.attr,
340 &port_attr_lid_mask_count.attr,
341 &port_attr_sm_lid.attr,
342 &port_attr_sm_sl.attr,
343 &port_attr_cap_mask.attr,
344 &port_attr_rate.attr,
345 &port_attr_phys_state.attr,
346 &port_attr_link_layer.attr,
347 NULL
348 };
349
print_ndev(const struct ib_gid_attr * gid_attr,char * buf)350 static size_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
351 {
352 if (!gid_attr->ndev)
353 return -EINVAL;
354
355 return sprintf(buf, "%s\n", gid_attr->ndev->name);
356 }
357
print_gid_type(const struct ib_gid_attr * gid_attr,char * buf)358 static size_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
359 {
360 return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
361 }
362
_show_port_gid_attr(struct ib_port * p,struct port_attribute * attr,char * buf,size_t (* print)(const struct ib_gid_attr * gid_attr,char * buf))363 static ssize_t _show_port_gid_attr(
364 struct ib_port *p, struct port_attribute *attr, char *buf,
365 size_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
366 {
367 struct port_table_attribute *tab_attr =
368 container_of(attr, struct port_table_attribute, attr);
369 const struct ib_gid_attr *gid_attr;
370 ssize_t ret;
371
372 gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
373 if (IS_ERR(gid_attr))
374 return PTR_ERR(gid_attr);
375
376 ret = print(gid_attr, buf);
377 rdma_put_gid_attr(gid_attr);
378 return ret;
379 }
380
show_port_gid(struct ib_port * p,struct port_attribute * attr,char * buf)381 static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
382 char *buf)
383 {
384 struct port_table_attribute *tab_attr =
385 container_of(attr, struct port_table_attribute, attr);
386 const struct ib_gid_attr *gid_attr;
387 ssize_t ret;
388
389 gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
390 if (IS_ERR(gid_attr)) {
391 const union ib_gid zgid = {};
392
393 /* If reading GID fails, it is likely due to GID entry being
394 * empty (invalid) or reserved GID in the table. User space
395 * expects to read GID table entries as long as it given index
396 * is within GID table size. Administrative/debugging tool
397 * fails to query rest of the GID entries if it hits error
398 * while querying a GID of the given index. To avoid user
399 * space throwing such error on fail to read gid, return zero
400 * GID as before. This maintains backward compatibility.
401 */
402 return sprintf(buf, "%pI6\n", zgid.raw);
403 }
404
405 ret = sprintf(buf, "%pI6\n", gid_attr->gid.raw);
406 rdma_put_gid_attr(gid_attr);
407 return ret;
408 }
409
show_port_gid_attr_ndev(struct ib_port * p,struct port_attribute * attr,char * buf)410 static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
411 struct port_attribute *attr, char *buf)
412 {
413 return _show_port_gid_attr(p, attr, buf, print_ndev);
414 }
415
show_port_gid_attr_gid_type(struct ib_port * p,struct port_attribute * attr,char * buf)416 static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
417 struct port_attribute *attr,
418 char *buf)
419 {
420 return _show_port_gid_attr(p, attr, buf, print_gid_type);
421 }
422
show_port_pkey(struct ib_port * p,struct port_attribute * attr,char * buf)423 static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
424 char *buf)
425 {
426 struct port_table_attribute *tab_attr =
427 container_of(attr, struct port_table_attribute, attr);
428 u16 pkey;
429 ssize_t ret;
430
431 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
432 if (ret)
433 return ret;
434
435 return sprintf(buf, "0x%04x\n", pkey);
436 }
437
438 #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
439 struct port_table_attribute port_pma_attr_##_name = { \
440 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
441 .index = (_offset) | ((_width) << 16) | ((_counter) << 24), \
442 .attr_id = IB_PMA_PORT_COUNTERS , \
443 }
444
445 #define PORT_PMA_ATTR_EXT(_name, _width, _offset) \
446 struct port_table_attribute port_pma_attr_ext_##_name = { \
447 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
448 .index = (_offset) | ((_width) << 16), \
449 .attr_id = IB_PMA_PORT_COUNTERS_EXT , \
450 }
451
452 /*
453 * Get a Perfmgmt MAD block of data.
454 * Returns error code or the number of bytes retrieved.
455 */
get_perf_mad(struct ib_device * dev,int port_num,__be16 attr,void * data,int offset,size_t size)456 static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
457 void *data, int offset, size_t size)
458 {
459 struct ib_mad *in_mad;
460 struct ib_mad *out_mad;
461 size_t mad_size = sizeof(*out_mad);
462 u16 out_mad_pkey_index = 0;
463 ssize_t ret;
464
465 if (!dev->process_mad)
466 return -ENOSYS;
467
468 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
469 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
470 if (!in_mad || !out_mad) {
471 ret = -ENOMEM;
472 goto out;
473 }
474
475 in_mad->mad_hdr.base_version = 1;
476 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
477 in_mad->mad_hdr.class_version = 1;
478 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
479 in_mad->mad_hdr.attr_id = attr;
480
481 if (attr != IB_PMA_CLASS_PORT_INFO)
482 in_mad->data[41] = port_num; /* PortSelect field */
483
484 if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY,
485 port_num, NULL, NULL,
486 (const struct ib_mad_hdr *)in_mad, mad_size,
487 (struct ib_mad_hdr *)out_mad, &mad_size,
488 &out_mad_pkey_index) &
489 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
490 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
491 ret = -EINVAL;
492 goto out;
493 }
494 memcpy(data, out_mad->data + offset, size);
495 ret = size;
496 out:
497 kfree(in_mad);
498 kfree(out_mad);
499 return ret;
500 }
501
show_pma_counter(struct ib_port * p,struct port_attribute * attr,char * buf)502 static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
503 char *buf)
504 {
505 struct port_table_attribute *tab_attr =
506 container_of(attr, struct port_table_attribute, attr);
507 int offset = tab_attr->index & 0xffff;
508 int width = (tab_attr->index >> 16) & 0xff;
509 ssize_t ret;
510 u8 data[8];
511
512 ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
513 40 + offset / 8, sizeof(data));
514 if (ret < 0)
515 return ret;
516
517 switch (width) {
518 case 4:
519 ret = sprintf(buf, "%u\n", (*data >>
520 (4 - (offset % 8))) & 0xf);
521 break;
522 case 8:
523 ret = sprintf(buf, "%u\n", *data);
524 break;
525 case 16:
526 ret = sprintf(buf, "%u\n",
527 be16_to_cpup((__be16 *)data));
528 break;
529 case 32:
530 ret = sprintf(buf, "%u\n",
531 be32_to_cpup((__be32 *)data));
532 break;
533 case 64:
534 ret = sprintf(buf, "%llu\n",
535 be64_to_cpup((__be64 *)data));
536 break;
537
538 default:
539 ret = 0;
540 }
541
542 return ret;
543 }
544
545 static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
546 static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
547 static PORT_PMA_ATTR(link_downed , 2, 8, 56);
548 static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
549 static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
550 static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
551 static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
552 static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
553 static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
554 static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
555 static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
556 static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
557 static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
558 static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
559 static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
560 static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
561 static PORT_PMA_ATTR(port_xmit_wait , 0, 32, 320);
562
563 /*
564 * Counters added by extended set
565 */
566 static PORT_PMA_ATTR_EXT(port_xmit_data , 64, 64);
567 static PORT_PMA_ATTR_EXT(port_rcv_data , 64, 128);
568 static PORT_PMA_ATTR_EXT(port_xmit_packets , 64, 192);
569 static PORT_PMA_ATTR_EXT(port_rcv_packets , 64, 256);
570 static PORT_PMA_ATTR_EXT(unicast_xmit_packets , 64, 320);
571 static PORT_PMA_ATTR_EXT(unicast_rcv_packets , 64, 384);
572 static PORT_PMA_ATTR_EXT(multicast_xmit_packets , 64, 448);
573 static PORT_PMA_ATTR_EXT(multicast_rcv_packets , 64, 512);
574
575 static struct attribute *pma_attrs[] = {
576 &port_pma_attr_symbol_error.attr.attr,
577 &port_pma_attr_link_error_recovery.attr.attr,
578 &port_pma_attr_link_downed.attr.attr,
579 &port_pma_attr_port_rcv_errors.attr.attr,
580 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
581 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
582 &port_pma_attr_port_xmit_discards.attr.attr,
583 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
584 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
585 &port_pma_attr_local_link_integrity_errors.attr.attr,
586 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
587 &port_pma_attr_VL15_dropped.attr.attr,
588 &port_pma_attr_port_xmit_data.attr.attr,
589 &port_pma_attr_port_rcv_data.attr.attr,
590 &port_pma_attr_port_xmit_packets.attr.attr,
591 &port_pma_attr_port_rcv_packets.attr.attr,
592 &port_pma_attr_port_xmit_wait.attr.attr,
593 NULL
594 };
595
596 static struct attribute *pma_attrs_ext[] = {
597 &port_pma_attr_symbol_error.attr.attr,
598 &port_pma_attr_link_error_recovery.attr.attr,
599 &port_pma_attr_link_downed.attr.attr,
600 &port_pma_attr_port_rcv_errors.attr.attr,
601 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
602 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
603 &port_pma_attr_port_xmit_discards.attr.attr,
604 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
605 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
606 &port_pma_attr_local_link_integrity_errors.attr.attr,
607 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
608 &port_pma_attr_VL15_dropped.attr.attr,
609 &port_pma_attr_ext_port_xmit_data.attr.attr,
610 &port_pma_attr_ext_port_rcv_data.attr.attr,
611 &port_pma_attr_ext_port_xmit_packets.attr.attr,
612 &port_pma_attr_port_xmit_wait.attr.attr,
613 &port_pma_attr_ext_port_rcv_packets.attr.attr,
614 &port_pma_attr_ext_unicast_rcv_packets.attr.attr,
615 &port_pma_attr_ext_unicast_xmit_packets.attr.attr,
616 &port_pma_attr_ext_multicast_rcv_packets.attr.attr,
617 &port_pma_attr_ext_multicast_xmit_packets.attr.attr,
618 NULL
619 };
620
621 static struct attribute *pma_attrs_noietf[] = {
622 &port_pma_attr_symbol_error.attr.attr,
623 &port_pma_attr_link_error_recovery.attr.attr,
624 &port_pma_attr_link_downed.attr.attr,
625 &port_pma_attr_port_rcv_errors.attr.attr,
626 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
627 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
628 &port_pma_attr_port_xmit_discards.attr.attr,
629 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
630 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
631 &port_pma_attr_local_link_integrity_errors.attr.attr,
632 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
633 &port_pma_attr_VL15_dropped.attr.attr,
634 &port_pma_attr_ext_port_xmit_data.attr.attr,
635 &port_pma_attr_ext_port_rcv_data.attr.attr,
636 &port_pma_attr_ext_port_xmit_packets.attr.attr,
637 &port_pma_attr_ext_port_rcv_packets.attr.attr,
638 &port_pma_attr_port_xmit_wait.attr.attr,
639 NULL
640 };
641
642 static struct attribute_group pma_group = {
643 .name = "counters",
644 .attrs = pma_attrs
645 };
646
647 static struct attribute_group pma_group_ext = {
648 .name = "counters",
649 .attrs = pma_attrs_ext
650 };
651
652 static struct attribute_group pma_group_noietf = {
653 .name = "counters",
654 .attrs = pma_attrs_noietf
655 };
656
ib_port_release(struct kobject * kobj)657 static void ib_port_release(struct kobject *kobj)
658 {
659 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
660 struct attribute *a;
661 int i;
662
663 if (p->gid_group.attrs) {
664 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
665 kfree(a);
666
667 kfree(p->gid_group.attrs);
668 }
669
670 if (p->pkey_group.attrs) {
671 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
672 kfree(a);
673
674 kfree(p->pkey_group.attrs);
675 }
676
677 kfree(p);
678 }
679
ib_port_gid_attr_release(struct kobject * kobj)680 static void ib_port_gid_attr_release(struct kobject *kobj)
681 {
682 struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
683 kobj);
684 struct attribute *a;
685 int i;
686
687 if (g->ndev.attrs) {
688 for (i = 0; (a = g->ndev.attrs[i]); ++i)
689 kfree(a);
690
691 kfree(g->ndev.attrs);
692 }
693
694 if (g->type.attrs) {
695 for (i = 0; (a = g->type.attrs[i]); ++i)
696 kfree(a);
697
698 kfree(g->type.attrs);
699 }
700
701 kfree(g);
702 }
703
704 static struct kobj_type port_type = {
705 .release = ib_port_release,
706 .sysfs_ops = &port_sysfs_ops,
707 .default_attrs = port_default_attrs
708 };
709
710 static struct kobj_type gid_attr_type = {
711 .sysfs_ops = &gid_attr_sysfs_ops,
712 .release = ib_port_gid_attr_release
713 };
714
715 static struct attribute **
alloc_group_attrs(ssize_t (* show)(struct ib_port *,struct port_attribute *,char * buf),int len)716 alloc_group_attrs(ssize_t (*show)(struct ib_port *,
717 struct port_attribute *, char *buf),
718 int len)
719 {
720 struct attribute **tab_attr;
721 struct port_table_attribute *element;
722 int i;
723
724 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
725 if (!tab_attr)
726 return NULL;
727
728 for (i = 0; i < len; i++) {
729 element = kzalloc(sizeof(struct port_table_attribute),
730 GFP_KERNEL);
731 if (!element)
732 goto err;
733
734 if (snprintf(element->name, sizeof(element->name),
735 "%d", i) >= sizeof(element->name)) {
736 kfree(element);
737 goto err;
738 }
739
740 element->attr.attr.name = element->name;
741 element->attr.attr.mode = S_IRUGO;
742 element->attr.show = show;
743 element->index = i;
744 sysfs_attr_init(&element->attr.attr);
745
746 tab_attr[i] = &element->attr.attr;
747 }
748
749 return tab_attr;
750
751 err:
752 while (--i >= 0)
753 kfree(tab_attr[i]);
754 kfree(tab_attr);
755 return NULL;
756 }
757
758 /*
759 * Figure out which counter table to use depending on
760 * the device capabilities.
761 */
get_counter_table(struct ib_device * dev,int port_num)762 static struct attribute_group *get_counter_table(struct ib_device *dev,
763 int port_num)
764 {
765 struct ib_class_port_info cpi;
766
767 if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
768 &cpi, 40, sizeof(cpi)) >= 0) {
769 if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH)
770 /* We have extended counters */
771 return &pma_group_ext;
772
773 if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
774 /* But not the IETF ones */
775 return &pma_group_noietf;
776 }
777
778 /* Fall back to normal counters */
779 return &pma_group;
780 }
781
update_hw_stats(struct ib_device * dev,struct rdma_hw_stats * stats,u8 port_num,int index)782 static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
783 u8 port_num, int index)
784 {
785 int ret;
786
787 if (time_is_after_eq_jiffies(stats->timestamp + stats->lifespan))
788 return 0;
789 ret = dev->get_hw_stats(dev, stats, port_num, index);
790 if (ret < 0)
791 return ret;
792 if (ret == stats->num_counters)
793 stats->timestamp = jiffies;
794
795 return 0;
796 }
797
print_hw_stat(struct rdma_hw_stats * stats,int index,char * buf)798 static ssize_t print_hw_stat(struct rdma_hw_stats *stats, int index, char *buf)
799 {
800 return sprintf(buf, "%llu\n", stats->value[index]);
801 }
802
show_hw_stats(struct kobject * kobj,struct attribute * attr,char * buf)803 static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
804 char *buf)
805 {
806 struct ib_device *dev;
807 struct ib_port *port;
808 struct hw_stats_attribute *hsa;
809 struct rdma_hw_stats *stats;
810 int ret;
811
812 hsa = container_of(attr, struct hw_stats_attribute, attr);
813 if (!hsa->port_num) {
814 dev = container_of((struct device *)kobj,
815 struct ib_device, dev);
816 stats = dev->hw_stats;
817 } else {
818 port = container_of(kobj, struct ib_port, kobj);
819 dev = port->ibdev;
820 stats = port->hw_stats;
821 }
822 mutex_lock(&stats->lock);
823 ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index);
824 if (ret)
825 goto unlock;
826 ret = print_hw_stat(stats, hsa->index, buf);
827 unlock:
828 mutex_unlock(&stats->lock);
829
830 return ret;
831 }
832
show_stats_lifespan(struct kobject * kobj,struct attribute * attr,char * buf)833 static ssize_t show_stats_lifespan(struct kobject *kobj,
834 struct attribute *attr,
835 char *buf)
836 {
837 struct hw_stats_attribute *hsa;
838 struct rdma_hw_stats *stats;
839 int msecs;
840
841 hsa = container_of(attr, struct hw_stats_attribute, attr);
842 if (!hsa->port_num) {
843 struct ib_device *dev = container_of((struct device *)kobj,
844 struct ib_device, dev);
845
846 stats = dev->hw_stats;
847 } else {
848 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
849
850 stats = p->hw_stats;
851 }
852
853 mutex_lock(&stats->lock);
854 msecs = jiffies_to_msecs(stats->lifespan);
855 mutex_unlock(&stats->lock);
856
857 return sprintf(buf, "%d\n", msecs);
858 }
859
set_stats_lifespan(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)860 static ssize_t set_stats_lifespan(struct kobject *kobj,
861 struct attribute *attr,
862 const char *buf, size_t count)
863 {
864 struct hw_stats_attribute *hsa;
865 struct rdma_hw_stats *stats;
866 int msecs;
867 int jiffies;
868 int ret;
869
870 ret = kstrtoint(buf, 10, &msecs);
871 if (ret)
872 return ret;
873 if (msecs < 0 || msecs > 10000)
874 return -EINVAL;
875 jiffies = msecs_to_jiffies(msecs);
876 hsa = container_of(attr, struct hw_stats_attribute, attr);
877 if (!hsa->port_num) {
878 struct ib_device *dev = container_of((struct device *)kobj,
879 struct ib_device, dev);
880
881 stats = dev->hw_stats;
882 } else {
883 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
884
885 stats = p->hw_stats;
886 }
887
888 mutex_lock(&stats->lock);
889 stats->lifespan = jiffies;
890 mutex_unlock(&stats->lock);
891
892 return count;
893 }
894
free_hsag(struct kobject * kobj,struct attribute_group * attr_group)895 static void free_hsag(struct kobject *kobj, struct attribute_group *attr_group)
896 {
897 struct attribute **attr;
898
899 sysfs_remove_group(kobj, attr_group);
900
901 for (attr = attr_group->attrs; *attr; attr++)
902 kfree(*attr);
903 kfree(attr_group);
904 }
905
alloc_hsa(int index,u8 port_num,const char * name)906 static struct attribute *alloc_hsa(int index, u8 port_num, const char *name)
907 {
908 struct hw_stats_attribute *hsa;
909
910 hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
911 if (!hsa)
912 return NULL;
913
914 hsa->attr.name = (char *)name;
915 hsa->attr.mode = S_IRUGO;
916 hsa->show = show_hw_stats;
917 hsa->store = NULL;
918 hsa->index = index;
919 hsa->port_num = port_num;
920
921 return &hsa->attr;
922 }
923
alloc_hsa_lifespan(char * name,u8 port_num)924 static struct attribute *alloc_hsa_lifespan(char *name, u8 port_num)
925 {
926 struct hw_stats_attribute *hsa;
927
928 hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
929 if (!hsa)
930 return NULL;
931
932 hsa->attr.name = name;
933 hsa->attr.mode = S_IWUSR | S_IRUGO;
934 hsa->show = show_stats_lifespan;
935 hsa->store = set_stats_lifespan;
936 hsa->index = 0;
937 hsa->port_num = port_num;
938
939 return &hsa->attr;
940 }
941
setup_hw_stats(struct ib_device * device,struct ib_port * port,u8 port_num)942 static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
943 u8 port_num)
944 {
945 struct attribute_group *hsag;
946 struct rdma_hw_stats *stats;
947 int i, ret;
948
949 stats = device->alloc_hw_stats(device, port_num);
950
951 if (!stats)
952 return;
953
954 if (!stats->names || stats->num_counters <= 0)
955 goto err_free_stats;
956
957 /*
958 * Two extra attribue elements here, one for the lifespan entry and
959 * one to NULL terminate the list for the sysfs core code
960 */
961 hsag = kzalloc(sizeof(*hsag) +
962 sizeof(void *) * (stats->num_counters + 2),
963 GFP_KERNEL);
964 if (!hsag)
965 goto err_free_stats;
966
967 ret = device->get_hw_stats(device, stats, port_num,
968 stats->num_counters);
969 if (ret != stats->num_counters)
970 goto err_free_hsag;
971
972 stats->timestamp = jiffies;
973
974 hsag->name = "hw_counters";
975 hsag->attrs = (void *)hsag + sizeof(*hsag);
976
977 for (i = 0; i < stats->num_counters; i++) {
978 hsag->attrs[i] = alloc_hsa(i, port_num, stats->names[i]);
979 if (!hsag->attrs[i])
980 goto err;
981 sysfs_attr_init(hsag->attrs[i]);
982 }
983
984 mutex_init(&stats->lock);
985 /* treat an error here as non-fatal */
986 hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num);
987 if (hsag->attrs[i])
988 sysfs_attr_init(hsag->attrs[i]);
989
990 if (port) {
991 struct kobject *kobj = &port->kobj;
992 ret = sysfs_create_group(kobj, hsag);
993 if (ret)
994 goto err;
995 port->hw_stats_ag = hsag;
996 port->hw_stats = stats;
997 } else {
998 struct kobject *kobj = &device->dev.kobj;
999 ret = sysfs_create_group(kobj, hsag);
1000 if (ret)
1001 goto err;
1002 device->hw_stats_ag = hsag;
1003 device->hw_stats = stats;
1004 }
1005
1006 return;
1007
1008 err:
1009 for (; i >= 0; i--)
1010 kfree(hsag->attrs[i]);
1011 err_free_hsag:
1012 kfree(hsag);
1013 err_free_stats:
1014 kfree(stats);
1015 return;
1016 }
1017
add_port(struct ib_device * device,int port_num,int (* port_callback)(struct ib_device *,u8,struct kobject *))1018 static int add_port(struct ib_device *device, int port_num,
1019 int (*port_callback)(struct ib_device *,
1020 u8, struct kobject *))
1021 {
1022 struct ib_port *p;
1023 struct ib_port_attr attr;
1024 int i;
1025 int ret;
1026
1027 ret = ib_query_port(device, port_num, &attr);
1028 if (ret)
1029 return ret;
1030
1031 p = kzalloc(sizeof *p, GFP_KERNEL);
1032 if (!p)
1033 return -ENOMEM;
1034
1035 p->ibdev = device;
1036 p->port_num = port_num;
1037
1038 ret = kobject_init_and_add(&p->kobj, &port_type,
1039 device->ports_parent,
1040 "%d", port_num);
1041 if (ret) {
1042 kfree(p);
1043 return ret;
1044 }
1045
1046 p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
1047 if (!p->gid_attr_group) {
1048 ret = -ENOMEM;
1049 goto err_put;
1050 }
1051
1052 p->gid_attr_group->port = p;
1053 ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
1054 &p->kobj, "gid_attrs");
1055 if (ret) {
1056 kfree(p->gid_attr_group);
1057 goto err_put;
1058 }
1059
1060 if (device->process_mad) {
1061 p->pma_table = get_counter_table(device, port_num);
1062 ret = sysfs_create_group(&p->kobj, p->pma_table);
1063 if (ret)
1064 goto err_put_gid_attrs;
1065 }
1066
1067 p->gid_group.name = "gids";
1068 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
1069 if (!p->gid_group.attrs) {
1070 ret = -ENOMEM;
1071 goto err_remove_pma;
1072 }
1073
1074 ret = sysfs_create_group(&p->kobj, &p->gid_group);
1075 if (ret)
1076 goto err_free_gid;
1077
1078 p->gid_attr_group->ndev.name = "ndevs";
1079 p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
1080 attr.gid_tbl_len);
1081 if (!p->gid_attr_group->ndev.attrs) {
1082 ret = -ENOMEM;
1083 goto err_remove_gid;
1084 }
1085
1086 ret = sysfs_create_group(&p->gid_attr_group->kobj,
1087 &p->gid_attr_group->ndev);
1088 if (ret)
1089 goto err_free_gid_ndev;
1090
1091 p->gid_attr_group->type.name = "types";
1092 p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
1093 attr.gid_tbl_len);
1094 if (!p->gid_attr_group->type.attrs) {
1095 ret = -ENOMEM;
1096 goto err_remove_gid_ndev;
1097 }
1098
1099 ret = sysfs_create_group(&p->gid_attr_group->kobj,
1100 &p->gid_attr_group->type);
1101 if (ret)
1102 goto err_free_gid_type;
1103
1104 p->pkey_group.name = "pkeys";
1105 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
1106 attr.pkey_tbl_len);
1107 if (!p->pkey_group.attrs) {
1108 ret = -ENOMEM;
1109 goto err_remove_gid_type;
1110 }
1111
1112 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
1113 if (ret)
1114 goto err_free_pkey;
1115
1116 if (port_callback) {
1117 ret = port_callback(device, port_num, &p->kobj);
1118 if (ret)
1119 goto err_remove_pkey;
1120 }
1121
1122 /*
1123 * If port == 0, it means we have only one port and the parent
1124 * device, not this port device, should be the holder of the
1125 * hw_counters
1126 */
1127 if (device->alloc_hw_stats && port_num)
1128 setup_hw_stats(device, p, port_num);
1129
1130 list_add_tail(&p->kobj.entry, &device->port_list);
1131
1132 kobject_uevent(&p->kobj, KOBJ_ADD);
1133 return 0;
1134
1135 err_remove_pkey:
1136 sysfs_remove_group(&p->kobj, &p->pkey_group);
1137
1138 err_free_pkey:
1139 for (i = 0; i < attr.pkey_tbl_len; ++i)
1140 kfree(p->pkey_group.attrs[i]);
1141
1142 kfree(p->pkey_group.attrs);
1143 p->pkey_group.attrs = NULL;
1144
1145 err_remove_gid_type:
1146 sysfs_remove_group(&p->gid_attr_group->kobj,
1147 &p->gid_attr_group->type);
1148
1149 err_free_gid_type:
1150 for (i = 0; i < attr.gid_tbl_len; ++i)
1151 kfree(p->gid_attr_group->type.attrs[i]);
1152
1153 kfree(p->gid_attr_group->type.attrs);
1154 p->gid_attr_group->type.attrs = NULL;
1155
1156 err_remove_gid_ndev:
1157 sysfs_remove_group(&p->gid_attr_group->kobj,
1158 &p->gid_attr_group->ndev);
1159
1160 err_free_gid_ndev:
1161 for (i = 0; i < attr.gid_tbl_len; ++i)
1162 kfree(p->gid_attr_group->ndev.attrs[i]);
1163
1164 kfree(p->gid_attr_group->ndev.attrs);
1165 p->gid_attr_group->ndev.attrs = NULL;
1166
1167 err_remove_gid:
1168 sysfs_remove_group(&p->kobj, &p->gid_group);
1169
1170 err_free_gid:
1171 for (i = 0; i < attr.gid_tbl_len; ++i)
1172 kfree(p->gid_group.attrs[i]);
1173
1174 kfree(p->gid_group.attrs);
1175 p->gid_group.attrs = NULL;
1176
1177 err_remove_pma:
1178 if (p->pma_table)
1179 sysfs_remove_group(&p->kobj, p->pma_table);
1180
1181 err_put_gid_attrs:
1182 kobject_put(&p->gid_attr_group->kobj);
1183
1184 err_put:
1185 kobject_put(&p->kobj);
1186 return ret;
1187 }
1188
show_node_type(struct device * device,struct device_attribute * attr,char * buf)1189 static ssize_t show_node_type(struct device *device,
1190 struct device_attribute *attr, char *buf)
1191 {
1192 struct ib_device *dev = container_of(device, struct ib_device, dev);
1193
1194 switch (dev->node_type) {
1195 case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
1196 case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
1197 case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
1198 case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
1199 case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
1200 case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
1201 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
1202 }
1203 }
1204
show_sys_image_guid(struct device * device,struct device_attribute * dev_attr,char * buf)1205 static ssize_t show_sys_image_guid(struct device *device,
1206 struct device_attribute *dev_attr, char *buf)
1207 {
1208 struct ib_device *dev = container_of(device, struct ib_device, dev);
1209
1210 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
1211 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
1212 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
1213 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
1214 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
1215 }
1216
show_node_guid(struct device * device,struct device_attribute * attr,char * buf)1217 static ssize_t show_node_guid(struct device *device,
1218 struct device_attribute *attr, char *buf)
1219 {
1220 struct ib_device *dev = container_of(device, struct ib_device, dev);
1221
1222 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
1223 be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
1224 be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
1225 be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
1226 be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
1227 }
1228
show_node_desc(struct device * device,struct device_attribute * attr,char * buf)1229 static ssize_t show_node_desc(struct device *device,
1230 struct device_attribute *attr, char *buf)
1231 {
1232 struct ib_device *dev = container_of(device, struct ib_device, dev);
1233
1234 return sprintf(buf, "%.64s\n", dev->node_desc);
1235 }
1236
set_node_desc(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1237 static ssize_t set_node_desc(struct device *device,
1238 struct device_attribute *attr,
1239 const char *buf, size_t count)
1240 {
1241 struct ib_device *dev = container_of(device, struct ib_device, dev);
1242 struct ib_device_modify desc = {};
1243 int ret;
1244
1245 if (!dev->modify_device)
1246 return -EIO;
1247
1248 memcpy(desc.node_desc, buf, min_t(int, count, IB_DEVICE_NODE_DESC_MAX));
1249 ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
1250 if (ret)
1251 return ret;
1252
1253 return count;
1254 }
1255
show_fw_ver(struct device * device,struct device_attribute * attr,char * buf)1256 static ssize_t show_fw_ver(struct device *device, struct device_attribute *attr,
1257 char *buf)
1258 {
1259 struct ib_device *dev = container_of(device, struct ib_device, dev);
1260
1261 ib_get_device_fw_str(dev, buf);
1262 strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
1263 return strlen(buf);
1264 }
1265
1266 static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
1267 static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
1268 static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
1269 static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
1270 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
1271
1272 static struct device_attribute *ib_class_attributes[] = {
1273 &dev_attr_node_type,
1274 &dev_attr_sys_image_guid,
1275 &dev_attr_node_guid,
1276 &dev_attr_node_desc,
1277 &dev_attr_fw_ver,
1278 };
1279
free_port_list_attributes(struct ib_device * device)1280 static void free_port_list_attributes(struct ib_device *device)
1281 {
1282 struct kobject *p, *t;
1283
1284 list_for_each_entry_safe(p, t, &device->port_list, entry) {
1285 struct ib_port *port = container_of(p, struct ib_port, kobj);
1286 list_del(&p->entry);
1287 if (port->hw_stats) {
1288 kfree(port->hw_stats);
1289 free_hsag(&port->kobj, port->hw_stats_ag);
1290 }
1291
1292 if (port->pma_table)
1293 sysfs_remove_group(p, port->pma_table);
1294 sysfs_remove_group(p, &port->pkey_group);
1295 sysfs_remove_group(p, &port->gid_group);
1296 sysfs_remove_group(&port->gid_attr_group->kobj,
1297 &port->gid_attr_group->ndev);
1298 sysfs_remove_group(&port->gid_attr_group->kobj,
1299 &port->gid_attr_group->type);
1300 kobject_put(&port->gid_attr_group->kobj);
1301 kobject_put(p);
1302 }
1303
1304 kobject_put(device->ports_parent);
1305 }
1306
ib_device_register_sysfs(struct ib_device * device,int (* port_callback)(struct ib_device *,u8,struct kobject *))1307 int ib_device_register_sysfs(struct ib_device *device,
1308 int (*port_callback)(struct ib_device *,
1309 u8, struct kobject *))
1310 {
1311 struct device *class_dev = &device->dev;
1312 int ret;
1313 int i;
1314
1315 ret = dev_set_name(class_dev, "%s", device->name);
1316 if (ret)
1317 return ret;
1318
1319 ret = device_add(class_dev);
1320 if (ret)
1321 goto err;
1322
1323 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
1324 ret = device_create_file(class_dev, ib_class_attributes[i]);
1325 if (ret)
1326 goto err_unregister;
1327 }
1328
1329 device->ports_parent = kobject_create_and_add("ports",
1330 &class_dev->kobj);
1331 if (!device->ports_parent) {
1332 ret = -ENOMEM;
1333 goto err_put;
1334 }
1335
1336 if (rdma_cap_ib_switch(device)) {
1337 ret = add_port(device, 0, port_callback);
1338 if (ret)
1339 goto err_put;
1340 } else {
1341 for (i = 1; i <= device->phys_port_cnt; ++i) {
1342 ret = add_port(device, i, port_callback);
1343 if (ret)
1344 goto err_put;
1345 }
1346 }
1347
1348 if (device->alloc_hw_stats)
1349 setup_hw_stats(device, NULL, 0);
1350
1351 return 0;
1352
1353 err_put:
1354 free_port_list_attributes(device);
1355
1356 err_unregister:
1357 device_del(class_dev);
1358
1359 err:
1360 return ret;
1361 }
1362
ib_device_unregister_sysfs(struct ib_device * device)1363 void ib_device_unregister_sysfs(struct ib_device *device)
1364 {
1365 int i;
1366
1367 /* Hold kobject until ib_dealloc_device() */
1368 kobject_get(&device->dev.kobj);
1369
1370 free_port_list_attributes(device);
1371
1372 if (device->hw_stats) {
1373 kfree(device->hw_stats);
1374 free_hsag(&device->dev.kobj, device->hw_stats_ag);
1375 }
1376
1377 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
1378 device_remove_file(&device->dev, ib_class_attributes[i]);
1379
1380 device_unregister(&device->dev);
1381 }
1382