blob: 3eeb24527cce93197471140b96a5f29d4df07090 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include "core_priv.h"
36
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080037#include <linux/slab.h>
Paul Gortmakerfc87af72011-05-27 13:27:45 -040038#include <linux/stat.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080039#include <linux/string.h>
Matan Barak470be512015-12-23 14:56:49 +020040#include <linux/netdevice.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080041
Roland Dreiera4d61e82005-08-25 13:40:04 -070042#include <rdma/ib_mad.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Matan Barak470be512015-12-23 14:56:49 +020044struct ib_port;
45
46struct gid_attr_group {
47 struct ib_port *port;
48 struct kobject kobj;
49 struct attribute_group ndev;
50 struct attribute_group type;
51};
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct ib_port {
53 struct kobject kobj;
54 struct ib_device *ibdev;
Matan Barak470be512015-12-23 14:56:49 +020055 struct gid_attr_group *gid_attr_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct attribute_group gid_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct attribute_group pkey_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 u8 port_num;
59};
60
61struct port_attribute {
62 struct attribute attr;
63 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
64 ssize_t (*store)(struct ib_port *, struct port_attribute *,
65 const char *buf, size_t count);
66};
67
68#define PORT_ATTR(_name, _mode, _show, _store) \
69struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
70
71#define PORT_ATTR_RO(_name) \
72struct port_attribute port_attr_##_name = __ATTR_RO(_name)
73
74struct port_table_attribute {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -050075 struct port_attribute attr;
76 char name[8];
77 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80static ssize_t port_attr_show(struct kobject *kobj,
81 struct attribute *attr, char *buf)
82{
83 struct port_attribute *port_attr =
84 container_of(attr, struct port_attribute, attr);
85 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
86
87 if (!port_attr->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -050088 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 return port_attr->show(p, port_attr, buf);
91}
92
Emese Revfy52cf25d2010-01-19 02:58:23 +010093static const struct sysfs_ops port_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 .show = port_attr_show
95};
96
Matan Barak470be512015-12-23 14:56:49 +020097static ssize_t gid_attr_show(struct kobject *kobj,
98 struct attribute *attr, char *buf)
99{
100 struct port_attribute *port_attr =
101 container_of(attr, struct port_attribute, attr);
102 struct ib_port *p = container_of(kobj, struct gid_attr_group,
103 kobj)->port;
104
105 if (!port_attr->show)
106 return -EIO;
107
108 return port_attr->show(p, port_attr, buf);
109}
110
111static const struct sysfs_ops gid_attr_sysfs_ops = {
112 .show = gid_attr_show
113};
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
116 char *buf)
117{
118 struct ib_port_attr attr;
119 ssize_t ret;
120
121 static const char *state_name[] = {
122 [IB_PORT_NOP] = "NOP",
123 [IB_PORT_DOWN] = "DOWN",
124 [IB_PORT_INIT] = "INIT",
125 [IB_PORT_ARMED] = "ARMED",
126 [IB_PORT_ACTIVE] = "ACTIVE",
127 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
128 };
129
130 ret = ib_query_port(p->ibdev, p->port_num, &attr);
131 if (ret)
132 return ret;
133
134 return sprintf(buf, "%d: %s\n", attr.state,
Roland Dreier048975a2006-03-20 10:08:25 -0800135 attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 state_name[attr.state] : "UNKNOWN");
137}
138
139static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
140 char *buf)
141{
142 struct ib_port_attr attr;
143 ssize_t ret;
144
145 ret = ib_query_port(p->ibdev, p->port_num, &attr);
146 if (ret)
147 return ret;
148
149 return sprintf(buf, "0x%x\n", attr.lid);
150}
151
152static ssize_t lid_mask_count_show(struct ib_port *p,
153 struct port_attribute *unused,
154 char *buf)
155{
156 struct ib_port_attr attr;
157 ssize_t ret;
158
159 ret = ib_query_port(p->ibdev, p->port_num, &attr);
160 if (ret)
161 return ret;
162
163 return sprintf(buf, "%d\n", attr.lmc);
164}
165
166static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
167 char *buf)
168{
169 struct ib_port_attr attr;
170 ssize_t ret;
171
172 ret = ib_query_port(p->ibdev, p->port_num, &attr);
173 if (ret)
174 return ret;
175
176 return sprintf(buf, "0x%x\n", attr.sm_lid);
177}
178
179static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
180 char *buf)
181{
182 struct ib_port_attr attr;
183 ssize_t ret;
184
185 ret = ib_query_port(p->ibdev, p->port_num, &attr);
186 if (ret)
187 return ret;
188
189 return sprintf(buf, "%d\n", attr.sm_sl);
190}
191
192static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
193 char *buf)
194{
195 struct ib_port_attr attr;
196 ssize_t ret;
197
198 ret = ib_query_port(p->ibdev, p->port_num, &attr);
199 if (ret)
200 return ret;
201
202 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
203}
204
205static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
206 char *buf)
207{
208 struct ib_port_attr attr;
209 char *speed = "";
Roland Dreier0559d8d2012-04-02 10:57:31 -0700210 int rate; /* in deci-Gb/sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ssize_t ret;
212
213 ret = ib_query_port(p->ibdev, p->port_num, &attr);
214 if (ret)
215 return ret;
216
217 switch (attr.active_speed) {
Or Gerlitz2e966912012-02-28 18:49:50 +0200218 case IB_SPEED_DDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300219 speed = " DDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800220 rate = 50;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300221 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200222 case IB_SPEED_QDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300223 speed = " QDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800224 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300225 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200226 case IB_SPEED_FDR10:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300227 speed = " FDR10";
Roland Dreiere9319b02012-02-27 09:15:08 -0800228 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300229 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200230 case IB_SPEED_FDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300231 speed = " FDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800232 rate = 140;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300233 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200234 case IB_SPEED_EDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300235 speed = " EDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800236 rate = 250;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300237 break;
Roland Dreier0559d8d2012-04-02 10:57:31 -0700238 case IB_SPEED_SDR:
239 default: /* default to SDR for invalid rates */
240 rate = 25;
241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300244 rate *= ib_width_enum_to_int(attr.active_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (rate < 0)
246 return -EINVAL;
247
248 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
Roland Dreiere9319b02012-02-27 09:15:08 -0800249 rate / 10, rate % 10 ? ".5" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 ib_width_enum_to_int(attr.active_width), speed);
251}
252
253static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
254 char *buf)
255{
256 struct ib_port_attr attr;
257
258 ssize_t ret;
259
260 ret = ib_query_port(p->ibdev, p->port_num, &attr);
261 if (ret)
262 return ret;
263
264 switch (attr.phys_state) {
265 case 1: return sprintf(buf, "1: Sleep\n");
266 case 2: return sprintf(buf, "2: Polling\n");
267 case 3: return sprintf(buf, "3: Disabled\n");
268 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
269 case 5: return sprintf(buf, "5: LinkUp\n");
270 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
271 case 7: return sprintf(buf, "7: Phy Test\n");
272 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
273 }
274}
275
Eli Cohen8ad330a2010-10-22 14:32:05 +0200276static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
277 char *buf)
278{
279 switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
280 case IB_LINK_LAYER_INFINIBAND:
281 return sprintf(buf, "%s\n", "InfiniBand");
282 case IB_LINK_LAYER_ETHERNET:
283 return sprintf(buf, "%s\n", "Ethernet");
284 default:
285 return sprintf(buf, "%s\n", "Unknown");
286 }
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static PORT_ATTR_RO(state);
290static PORT_ATTR_RO(lid);
291static PORT_ATTR_RO(lid_mask_count);
292static PORT_ATTR_RO(sm_lid);
293static PORT_ATTR_RO(sm_sl);
294static PORT_ATTR_RO(cap_mask);
295static PORT_ATTR_RO(rate);
296static PORT_ATTR_RO(phys_state);
Eli Cohen8ad330a2010-10-22 14:32:05 +0200297static PORT_ATTR_RO(link_layer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299static struct attribute *port_default_attrs[] = {
300 &port_attr_state.attr,
301 &port_attr_lid.attr,
302 &port_attr_lid_mask_count.attr,
303 &port_attr_sm_lid.attr,
304 &port_attr_sm_sl.attr,
305 &port_attr_cap_mask.attr,
306 &port_attr_rate.attr,
307 &port_attr_phys_state.attr,
Eli Cohen8ad330a2010-10-22 14:32:05 +0200308 &port_attr_link_layer.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 NULL
310};
311
Matan Barak470be512015-12-23 14:56:49 +0200312static size_t print_ndev(struct ib_gid_attr *gid_attr, char *buf)
313{
314 if (!gid_attr->ndev)
315 return -EINVAL;
316
317 return sprintf(buf, "%s\n", gid_attr->ndev->name);
318}
319
320static size_t print_gid_type(struct ib_gid_attr *gid_attr, char *buf)
321{
322 return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
323}
324
325static ssize_t _show_port_gid_attr(struct ib_port *p,
326 struct port_attribute *attr,
327 char *buf,
328 size_t (*print)(struct ib_gid_attr *gid_attr,
329 char *buf))
330{
331 struct port_table_attribute *tab_attr =
332 container_of(attr, struct port_table_attribute, attr);
333 union ib_gid gid;
334 struct ib_gid_attr gid_attr = {};
335 ssize_t ret;
336 va_list args;
337
338 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid,
339 &gid_attr);
340 if (ret)
341 goto err;
342
343 ret = print(&gid_attr, buf);
344
345err:
346 if (gid_attr.ndev)
347 dev_put(gid_attr.ndev);
348 va_end(args);
349 return ret;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
353 char *buf)
354{
355 struct port_table_attribute *tab_attr =
356 container_of(attr, struct port_table_attribute, attr);
357 union ib_gid gid;
358 ssize_t ret;
359
Matan Barak55ee3ab2015-10-15 18:38:45 +0300360 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (ret)
362 return ret;
363
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700364 return sprintf(buf, "%pI6\n", gid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Matan Barak470be512015-12-23 14:56:49 +0200367static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
368 struct port_attribute *attr, char *buf)
369{
370 return _show_port_gid_attr(p, attr, buf, print_ndev);
371}
372
373static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
374 struct port_attribute *attr,
375 char *buf)
376{
377 return _show_port_gid_attr(p, attr, buf, print_gid_type);
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
381 char *buf)
382{
383 struct port_table_attribute *tab_attr =
384 container_of(attr, struct port_table_attribute, attr);
385 u16 pkey;
386 ssize_t ret;
387
388 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
389 if (ret)
390 return ret;
391
392 return sprintf(buf, "0x%04x\n", pkey);
393}
394
395#define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
396struct port_table_attribute port_pma_attr_##_name = { \
397 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
398 .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
399}
400
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600401/*
402 * Get a Perfmgmt MAD block of data.
403 * Returns error code or the number of bytes retrieved.
404 */
405static int get_perf_mad(struct ib_device *dev, int port_num, int attr,
406 void *data, int offset, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600408 struct ib_mad *in_mad;
409 struct ib_mad *out_mad;
Ira Weiny4cd7c942015-06-06 14:38:31 -0400410 size_t mad_size = sizeof(*out_mad);
411 u16 out_mad_pkey_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ssize_t ret;
413
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600414 if (!dev->process_mad)
415 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Roland Dreierde6eb662005-11-02 07:23:14 -0800417 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Dotan Barak856c52a2007-07-10 16:55:57 +0300418 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!in_mad || !out_mad) {
420 ret = -ENOMEM;
421 goto out;
422 }
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 in_mad->mad_hdr.base_version = 1;
425 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
426 in_mad->mad_hdr.class_version = 1;
427 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600428 in_mad->mad_hdr.attr_id = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600430 in_mad->data[41] = port_num; /* PortSelect field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600432 if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY,
433 port_num, NULL, NULL,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400434 (const struct ib_mad_hdr *)in_mad, mad_size,
435 (struct ib_mad_hdr *)out_mad, &mad_size,
436 &out_mad_pkey_index) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
438 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
439 ret = -EINVAL;
440 goto out;
441 }
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600442 memcpy(data, out_mad->data + offset, size);
443 ret = size;
444out:
445 kfree(in_mad);
446 kfree(out_mad);
447 return ret;
448}
449
450static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
451 char *buf)
452{
453 struct port_table_attribute *tab_attr =
454 container_of(attr, struct port_table_attribute, attr);
455 int offset = tab_attr->index & 0xffff;
456 int width = (tab_attr->index >> 16) & 0xff;
457 ssize_t ret;
458 u8 data[8];
459
460 ret = get_perf_mad(p->ibdev, p->port_num, cpu_to_be16(0x12), &data,
461 40 + offset / 8, sizeof(data));
462 if (ret < 0)
463 return sprintf(buf, "N/A (no PMA)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 switch (width) {
466 case 4:
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600467 ret = sprintf(buf, "%u\n", (*data >>
Ralph Campbelld8b9f232006-05-09 10:50:28 -0700468 (4 - (offset % 8))) & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470 case 8:
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600471 ret = sprintf(buf, "%u\n", *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case 16:
474 ret = sprintf(buf, "%u\n",
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600475 be16_to_cpup((__be16 *)data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case 32:
478 ret = sprintf(buf, "%u\n",
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600479 be32_to_cpup((__be32 *)data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 default:
482 ret = 0;
483 }
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return ret;
486}
487
488static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
489static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
490static PORT_PMA_ATTR(link_downed , 2, 8, 56);
491static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
492static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
493static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
494static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
495static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
496static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
497static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
498static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
499static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
500static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
501static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
502static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
503static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
504
505static struct attribute *pma_attrs[] = {
506 &port_pma_attr_symbol_error.attr.attr,
507 &port_pma_attr_link_error_recovery.attr.attr,
508 &port_pma_attr_link_downed.attr.attr,
509 &port_pma_attr_port_rcv_errors.attr.attr,
510 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
511 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
512 &port_pma_attr_port_xmit_discards.attr.attr,
513 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
514 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
515 &port_pma_attr_local_link_integrity_errors.attr.attr,
516 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
517 &port_pma_attr_VL15_dropped.attr.attr,
518 &port_pma_attr_port_xmit_data.attr.attr,
519 &port_pma_attr_port_rcv_data.attr.attr,
520 &port_pma_attr_port_xmit_packets.attr.attr,
521 &port_pma_attr_port_rcv_packets.attr.attr,
522 NULL
523};
524
525static struct attribute_group pma_group = {
526 .name = "counters",
527 .attrs = pma_attrs
528};
529
530static void ib_port_release(struct kobject *kobj)
531{
532 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
533 struct attribute *a;
534 int i;
535
Haggai Erancad6d022014-05-18 11:12:25 +0300536 if (p->gid_group.attrs) {
537 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
538 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Haggai Erancad6d022014-05-18 11:12:25 +0300540 kfree(p->gid_group.attrs);
541 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500542
Haggai Erancad6d022014-05-18 11:12:25 +0300543 if (p->pkey_group.attrs) {
544 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
545 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Haggai Erancad6d022014-05-18 11:12:25 +0300547 kfree(p->pkey_group.attrs);
548 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 kfree(p);
551}
552
Matan Barak470be512015-12-23 14:56:49 +0200553static void ib_port_gid_attr_release(struct kobject *kobj)
554{
555 struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
556 kobj);
557 struct attribute *a;
558 int i;
559
560 if (g->ndev.attrs) {
561 for (i = 0; (a = g->ndev.attrs[i]); ++i)
562 kfree(a);
563
564 kfree(g->ndev.attrs);
565 }
566
567 if (g->type.attrs) {
568 for (i = 0; (a = g->type.attrs[i]); ++i)
569 kfree(a);
570
571 kfree(g->type.attrs);
572 }
573
574 kfree(g);
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static struct kobj_type port_type = {
578 .release = ib_port_release,
579 .sysfs_ops = &port_sysfs_ops,
580 .default_attrs = port_default_attrs
581};
582
Matan Barak470be512015-12-23 14:56:49 +0200583static struct kobj_type gid_attr_type = {
584 .sysfs_ops = &gid_attr_sysfs_ops,
585 .release = ib_port_gid_attr_release
586};
587
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500588static struct attribute **
589alloc_group_attrs(ssize_t (*show)(struct ib_port *,
590 struct port_attribute *, char *buf),
591 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500593 struct attribute **tab_attr;
594 struct port_table_attribute *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500597 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
598 if (!tab_attr)
599 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500601 for (i = 0; i < len; i++) {
Pekka Enberg82ca76b2005-09-06 15:18:35 -0700602 element = kzalloc(sizeof(struct port_table_attribute),
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500603 GFP_KERNEL);
604 if (!element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500607 if (snprintf(element->name, sizeof(element->name),
Roland Dreier048975a2006-03-20 10:08:25 -0800608 "%d", i) >= sizeof(element->name)) {
609 kfree(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto err;
Roland Dreier048975a2006-03-20 10:08:25 -0800611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500613 element->attr.attr.name = element->name;
614 element->attr.attr.mode = S_IRUGO;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500615 element->attr.show = show;
616 element->index = i;
Greg Kroah-Hartman21e3bde2010-03-15 14:01:25 -0700617 sysfs_attr_init(&element->attr.attr);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500618
619 tab_attr[i] = &element->attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500622 return tab_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624err:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500625 while (--i >= 0)
626 kfree(tab_attr[i]);
627 kfree(tab_attr);
628 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700631static int add_port(struct ib_device *device, int port_num,
632 int (*port_callback)(struct ib_device *,
633 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 struct ib_port *p;
636 struct ib_port_attr attr;
637 int i;
638 int ret;
639
640 ret = ib_query_port(device, port_num, &attr);
641 if (ret)
642 return ret;
643
Roland Dreierde6eb662005-11-02 07:23:14 -0800644 p = kzalloc(sizeof *p, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!p)
646 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 p->ibdev = device;
649 p->port_num = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400651 ret = kobject_init_and_add(&p->kobj, &port_type,
Haggai Eran373c0ea2014-05-18 11:12:24 +0300652 device->ports_parent,
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400653 "%d", port_num);
Haggai Erancad6d022014-05-18 11:12:25 +0300654 if (ret) {
655 kfree(p);
656 return ret;
657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Matan Barak470be512015-12-23 14:56:49 +0200659 p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
660 if (!p->gid_attr_group) {
661 ret = -ENOMEM;
662 goto err_put;
663 }
664
665 p->gid_attr_group->port = p;
666 ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
667 &p->kobj, "gid_attrs");
668 if (ret) {
669 kfree(p->gid_attr_group);
670 goto err_put;
671 }
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 ret = sysfs_create_group(&p->kobj, &pma_group);
674 if (ret)
Matan Barak470be512015-12-23 14:56:49 +0200675 goto err_put_gid_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 p->gid_group.name = "gids";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500678 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800679 if (!p->gid_group.attrs) {
680 ret = -ENOMEM;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500681 goto err_remove_pma;
Wei Yongjun80b15042013-06-21 11:24:27 +0800682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 ret = sysfs_create_group(&p->kobj, &p->gid_group);
685 if (ret)
686 goto err_free_gid;
687
Matan Barak470be512015-12-23 14:56:49 +0200688 p->gid_attr_group->ndev.name = "ndevs";
689 p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
690 attr.gid_tbl_len);
691 if (!p->gid_attr_group->ndev.attrs) {
692 ret = -ENOMEM;
693 goto err_remove_gid;
694 }
695
696 ret = sysfs_create_group(&p->gid_attr_group->kobj,
697 &p->gid_attr_group->ndev);
698 if (ret)
699 goto err_free_gid_ndev;
700
701 p->gid_attr_group->type.name = "types";
702 p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
703 attr.gid_tbl_len);
704 if (!p->gid_attr_group->type.attrs) {
705 ret = -ENOMEM;
706 goto err_remove_gid_ndev;
707 }
708
709 ret = sysfs_create_group(&p->gid_attr_group->kobj,
710 &p->gid_attr_group->type);
711 if (ret)
712 goto err_free_gid_type;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 p->pkey_group.name = "pkeys";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500715 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
716 attr.pkey_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800717 if (!p->pkey_group.attrs) {
718 ret = -ENOMEM;
Matan Barak470be512015-12-23 14:56:49 +0200719 goto err_remove_gid_type;
Wei Yongjun80b15042013-06-21 11:24:27 +0800720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
723 if (ret)
724 goto err_free_pkey;
725
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700726 if (port_callback) {
727 ret = port_callback(device, port_num, &p->kobj);
728 if (ret)
729 goto err_remove_pkey;
730 }
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 list_add_tail(&p->kobj.entry, &device->port_list);
733
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400734 kobject_uevent(&p->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700737err_remove_pkey:
738 sysfs_remove_group(&p->kobj, &p->pkey_group);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740err_free_pkey:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500741 for (i = 0; i < attr.pkey_tbl_len; ++i)
742 kfree(p->pkey_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500744 kfree(p->pkey_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300745 p->pkey_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Matan Barak470be512015-12-23 14:56:49 +0200747err_remove_gid_type:
748 sysfs_remove_group(&p->gid_attr_group->kobj,
749 &p->gid_attr_group->type);
750
751err_free_gid_type:
752 for (i = 0; i < attr.gid_tbl_len; ++i)
753 kfree(p->gid_attr_group->type.attrs[i]);
754
755 kfree(p->gid_attr_group->type.attrs);
756 p->gid_attr_group->type.attrs = NULL;
757
758err_remove_gid_ndev:
759 sysfs_remove_group(&p->gid_attr_group->kobj,
760 &p->gid_attr_group->ndev);
761
762err_free_gid_ndev:
763 for (i = 0; i < attr.gid_tbl_len; ++i)
764 kfree(p->gid_attr_group->ndev.attrs[i]);
765
766 kfree(p->gid_attr_group->ndev.attrs);
767 p->gid_attr_group->ndev.attrs = NULL;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769err_remove_gid:
770 sysfs_remove_group(&p->kobj, &p->gid_group);
771
772err_free_gid:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500773 for (i = 0; i < attr.gid_tbl_len; ++i)
774 kfree(p->gid_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500776 kfree(p->gid_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300777 p->gid_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779err_remove_pma:
780 sysfs_remove_group(&p->kobj, &pma_group);
781
Matan Barak470be512015-12-23 14:56:49 +0200782err_put_gid_attrs:
783 kobject_put(&p->gid_attr_group->kobj);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785err_put:
Haggai Erancad6d022014-05-18 11:12:25 +0300786 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return ret;
788}
789
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100790static ssize_t show_node_type(struct device *device,
791 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100793 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 switch (dev->node_type) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500796 case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
797 case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
Upinder Malhi \(umalhi\)180771a2013-09-10 03:36:59 +0000798 case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
Upinder Malhi5db57652014-01-15 17:02:36 -0800799 case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500800 case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
801 case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
802 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804}
805
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100806static ssize_t show_sys_image_guid(struct device *device,
807 struct device_attribute *dev_attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100809 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Or Gerlitz86bee4c2015-12-18 10:59:45 +0200812 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
813 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
814 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
815 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100818static ssize_t show_node_guid(struct device *device,
819 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100821 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Sean Heftycf311cd2006-01-10 07:39:34 -0800824 be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
825 be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
826 be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
827 be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100830static ssize_t show_node_desc(struct device *device,
831 struct device_attribute *attr, char *buf)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800832{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100833 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800834
835 return sprintf(buf, "%.64s\n", dev->node_desc);
836}
837
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100838static ssize_t set_node_desc(struct device *device,
839 struct device_attribute *attr,
840 const char *buf, size_t count)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800841{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100842 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800843 struct ib_device_modify desc = {};
844 int ret;
845
846 if (!dev->modify_device)
847 return -EIO;
848
849 memcpy(desc.node_desc, buf, min_t(int, count, 64));
850 ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
851 if (ret)
852 return ret;
853
854 return count;
855}
856
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100857static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
858static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
859static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
860static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100862static struct device_attribute *ib_class_attributes[] = {
863 &dev_attr_node_type,
864 &dev_attr_sys_image_guid,
865 &dev_attr_node_guid,
866 &dev_attr_node_desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867};
868
Steve Wise7f624d02008-07-14 23:48:48 -0700869/* Show a given an attribute in the statistics group */
870static ssize_t show_protocol_stat(const struct device *device,
871 struct device_attribute *attr, char *buf,
872 unsigned offset)
873{
874 struct ib_device *dev = container_of(device, struct ib_device, dev);
875 union rdma_protocol_stats stats;
876 ssize_t ret;
877
878 ret = dev->get_protocol_stats(dev, &stats);
879 if (ret)
880 return ret;
881
882 return sprintf(buf, "%llu\n",
883 (unsigned long long) ((u64 *) &stats)[offset]);
884}
885
886/* generate a read-only iwarp statistics attribute */
887#define IW_STATS_ENTRY(name) \
888static ssize_t show_##name(struct device *device, \
889 struct device_attribute *attr, char *buf) \
890{ \
891 return show_protocol_stat(device, attr, buf, \
892 offsetof(struct iw_protocol_stats, name) / \
893 sizeof (u64)); \
894} \
895static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
896
897IW_STATS_ENTRY(ipInReceives);
898IW_STATS_ENTRY(ipInHdrErrors);
899IW_STATS_ENTRY(ipInTooBigErrors);
900IW_STATS_ENTRY(ipInNoRoutes);
901IW_STATS_ENTRY(ipInAddrErrors);
902IW_STATS_ENTRY(ipInUnknownProtos);
903IW_STATS_ENTRY(ipInTruncatedPkts);
904IW_STATS_ENTRY(ipInDiscards);
905IW_STATS_ENTRY(ipInDelivers);
906IW_STATS_ENTRY(ipOutForwDatagrams);
907IW_STATS_ENTRY(ipOutRequests);
908IW_STATS_ENTRY(ipOutDiscards);
909IW_STATS_ENTRY(ipOutNoRoutes);
910IW_STATS_ENTRY(ipReasmTimeout);
911IW_STATS_ENTRY(ipReasmReqds);
912IW_STATS_ENTRY(ipReasmOKs);
913IW_STATS_ENTRY(ipReasmFails);
914IW_STATS_ENTRY(ipFragOKs);
915IW_STATS_ENTRY(ipFragFails);
916IW_STATS_ENTRY(ipFragCreates);
917IW_STATS_ENTRY(ipInMcastPkts);
918IW_STATS_ENTRY(ipOutMcastPkts);
919IW_STATS_ENTRY(ipInBcastPkts);
920IW_STATS_ENTRY(ipOutBcastPkts);
921IW_STATS_ENTRY(tcpRtoAlgorithm);
922IW_STATS_ENTRY(tcpRtoMin);
923IW_STATS_ENTRY(tcpRtoMax);
924IW_STATS_ENTRY(tcpMaxConn);
925IW_STATS_ENTRY(tcpActiveOpens);
926IW_STATS_ENTRY(tcpPassiveOpens);
927IW_STATS_ENTRY(tcpAttemptFails);
928IW_STATS_ENTRY(tcpEstabResets);
929IW_STATS_ENTRY(tcpCurrEstab);
930IW_STATS_ENTRY(tcpInSegs);
931IW_STATS_ENTRY(tcpOutSegs);
932IW_STATS_ENTRY(tcpRetransSegs);
933IW_STATS_ENTRY(tcpInErrs);
934IW_STATS_ENTRY(tcpOutRsts);
935
936static struct attribute *iw_proto_stats_attrs[] = {
937 &dev_attr_ipInReceives.attr,
938 &dev_attr_ipInHdrErrors.attr,
939 &dev_attr_ipInTooBigErrors.attr,
940 &dev_attr_ipInNoRoutes.attr,
941 &dev_attr_ipInAddrErrors.attr,
942 &dev_attr_ipInUnknownProtos.attr,
943 &dev_attr_ipInTruncatedPkts.attr,
944 &dev_attr_ipInDiscards.attr,
945 &dev_attr_ipInDelivers.attr,
946 &dev_attr_ipOutForwDatagrams.attr,
947 &dev_attr_ipOutRequests.attr,
948 &dev_attr_ipOutDiscards.attr,
949 &dev_attr_ipOutNoRoutes.attr,
950 &dev_attr_ipReasmTimeout.attr,
951 &dev_attr_ipReasmReqds.attr,
952 &dev_attr_ipReasmOKs.attr,
953 &dev_attr_ipReasmFails.attr,
954 &dev_attr_ipFragOKs.attr,
955 &dev_attr_ipFragFails.attr,
956 &dev_attr_ipFragCreates.attr,
957 &dev_attr_ipInMcastPkts.attr,
958 &dev_attr_ipOutMcastPkts.attr,
959 &dev_attr_ipInBcastPkts.attr,
960 &dev_attr_ipOutBcastPkts.attr,
961 &dev_attr_tcpRtoAlgorithm.attr,
962 &dev_attr_tcpRtoMin.attr,
963 &dev_attr_tcpRtoMax.attr,
964 &dev_attr_tcpMaxConn.attr,
965 &dev_attr_tcpActiveOpens.attr,
966 &dev_attr_tcpPassiveOpens.attr,
967 &dev_attr_tcpAttemptFails.attr,
968 &dev_attr_tcpEstabResets.attr,
969 &dev_attr_tcpCurrEstab.attr,
970 &dev_attr_tcpInSegs.attr,
971 &dev_attr_tcpOutSegs.attr,
972 &dev_attr_tcpRetransSegs.attr,
973 &dev_attr_tcpInErrs.attr,
974 &dev_attr_tcpOutRsts.attr,
975 NULL
976};
977
978static struct attribute_group iw_stats_group = {
979 .name = "proto_stats",
980 .attrs = iw_proto_stats_attrs,
981};
982
Haggai Eran584482a2014-05-18 11:12:26 +0300983static void free_port_list_attributes(struct ib_device *device)
984{
985 struct kobject *p, *t;
986
987 list_for_each_entry_safe(p, t, &device->port_list, entry) {
988 struct ib_port *port = container_of(p, struct ib_port, kobj);
989 list_del(&p->entry);
990 sysfs_remove_group(p, &pma_group);
991 sysfs_remove_group(p, &port->pkey_group);
992 sysfs_remove_group(p, &port->gid_group);
Matan Barak470be512015-12-23 14:56:49 +0200993 sysfs_remove_group(&port->gid_attr_group->kobj,
994 &port->gid_attr_group->ndev);
995 sysfs_remove_group(&port->gid_attr_group->kobj,
996 &port->gid_attr_group->type);
997 kobject_put(&port->gid_attr_group->kobj);
Haggai Eran584482a2014-05-18 11:12:26 +0300998 kobject_put(p);
999 }
1000
1001 kobject_put(device->ports_parent);
1002}
1003
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001004int ib_device_register_sysfs(struct ib_device *device,
1005 int (*port_callback)(struct ib_device *,
1006 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001008 struct device *class_dev = &device->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int ret;
1010 int i;
1011
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001012 device->dev.parent = device->dma_device;
1013 ret = dev_set_name(class_dev, "%s", device->name);
1014 if (ret)
1015 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001017 ret = device_add(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (ret)
1019 goto err;
1020
1021 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001022 ret = device_create_file(class_dev, ib_class_attributes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (ret)
1024 goto err_unregister;
1025 }
1026
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -04001027 device->ports_parent = kobject_create_and_add("ports",
Haggai Eran373c0ea2014-05-18 11:12:24 +03001028 &class_dev->kobj);
Li Zefanc7482b82008-02-15 10:24:49 +08001029 if (!device->ports_parent) {
1030 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 goto err_put;
Li Zefanc7482b82008-02-15 10:24:49 +08001032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Hal Rosenstock41390322015-06-29 09:57:00 -04001034 if (rdma_cap_ib_switch(device)) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001035 ret = add_port(device, 0, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (ret)
1037 goto err_put;
1038 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 for (i = 1; i <= device->phys_port_cnt; ++i) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001040 ret = add_port(device, i, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (ret)
1042 goto err_put;
1043 }
1044 }
1045
Steve Wise7f624d02008-07-14 23:48:48 -07001046 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
1047 ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
1048 if (ret)
1049 goto err_put;
1050 }
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
1053
1054err_put:
Haggai Eran584482a2014-05-18 11:12:26 +03001055 free_port_list_attributes(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057err_unregister:
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001058 device_unregister(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060err:
1061 return ret;
1062}
1063
1064void ib_device_unregister_sysfs(struct ib_device *device)
1065{
Roland Dreier9206dff2009-02-25 13:27:46 -08001066 /* Hold kobject until ib_dealloc_device() */
Haggai Eran584482a2014-05-18 11:12:26 +03001067 struct kobject *kobj_dev = kobject_get(&device->dev.kobj);
1068 int i;
Roland Dreier9206dff2009-02-25 13:27:46 -08001069
Haggai Eran584482a2014-05-18 11:12:26 +03001070 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats)
1071 sysfs_remove_group(kobj_dev, &iw_stats_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Haggai Eran584482a2014-05-18 11:12:26 +03001073 free_port_list_attributes(device);
1074
1075 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
1076 device_remove_file(&device->dev, ib_class_attributes[i]);
1077
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001078 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}