blob: 09cee09e521222ee1b62879ace42946fa36b3050 [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>
Christoph Lameterb2788ce2015-12-21 08:20:28 -060043#include <rdma/ib_pma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Matan Barak470be512015-12-23 14:56:49 +020045struct ib_port;
46
47struct gid_attr_group {
48 struct ib_port *port;
49 struct kobject kobj;
50 struct attribute_group ndev;
51 struct attribute_group type;
52};
Linus Torvalds1da177e2005-04-16 15:20:36 -070053struct ib_port {
54 struct kobject kobj;
55 struct ib_device *ibdev;
Matan Barak470be512015-12-23 14:56:49 +020056 struct gid_attr_group *gid_attr_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 struct attribute_group gid_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 struct attribute_group pkey_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 u8 port_num;
60};
61
62struct port_attribute {
63 struct attribute attr;
64 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
65 ssize_t (*store)(struct ib_port *, struct port_attribute *,
66 const char *buf, size_t count);
67};
68
69#define PORT_ATTR(_name, _mode, _show, _store) \
70struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
71
72#define PORT_ATTR_RO(_name) \
73struct port_attribute port_attr_##_name = __ATTR_RO(_name)
74
75struct port_table_attribute {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -050076 struct port_attribute attr;
77 char name[8];
78 int index;
Christoph Lameterb2788ce2015-12-21 08:20:28 -060079 int attr_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080};
81
82static ssize_t port_attr_show(struct kobject *kobj,
83 struct attribute *attr, char *buf)
84{
85 struct port_attribute *port_attr =
86 container_of(attr, struct port_attribute, attr);
87 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
88
89 if (!port_attr->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -050090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 return port_attr->show(p, port_attr, buf);
93}
94
Emese Revfy52cf25d2010-01-19 02:58:23 +010095static const struct sysfs_ops port_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .show = port_attr_show
97};
98
Matan Barak470be512015-12-23 14:56:49 +020099static ssize_t gid_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 gid_attr_group,
105 kobj)->port;
106
107 if (!port_attr->show)
108 return -EIO;
109
110 return port_attr->show(p, port_attr, buf);
111}
112
113static const struct sysfs_ops gid_attr_sysfs_ops = {
114 .show = gid_attr_show
115};
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
118 char *buf)
119{
120 struct ib_port_attr attr;
121 ssize_t ret;
122
123 static const char *state_name[] = {
124 [IB_PORT_NOP] = "NOP",
125 [IB_PORT_DOWN] = "DOWN",
126 [IB_PORT_INIT] = "INIT",
127 [IB_PORT_ARMED] = "ARMED",
128 [IB_PORT_ACTIVE] = "ACTIVE",
129 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
130 };
131
132 ret = ib_query_port(p->ibdev, p->port_num, &attr);
133 if (ret)
134 return ret;
135
136 return sprintf(buf, "%d: %s\n", attr.state,
Roland Dreier048975a2006-03-20 10:08:25 -0800137 attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 state_name[attr.state] : "UNKNOWN");
139}
140
141static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
142 char *buf)
143{
144 struct ib_port_attr attr;
145 ssize_t ret;
146
147 ret = ib_query_port(p->ibdev, p->port_num, &attr);
148 if (ret)
149 return ret;
150
151 return sprintf(buf, "0x%x\n", attr.lid);
152}
153
154static ssize_t lid_mask_count_show(struct ib_port *p,
155 struct port_attribute *unused,
156 char *buf)
157{
158 struct ib_port_attr attr;
159 ssize_t ret;
160
161 ret = ib_query_port(p->ibdev, p->port_num, &attr);
162 if (ret)
163 return ret;
164
165 return sprintf(buf, "%d\n", attr.lmc);
166}
167
168static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
169 char *buf)
170{
171 struct ib_port_attr attr;
172 ssize_t ret;
173
174 ret = ib_query_port(p->ibdev, p->port_num, &attr);
175 if (ret)
176 return ret;
177
178 return sprintf(buf, "0x%x\n", attr.sm_lid);
179}
180
181static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
182 char *buf)
183{
184 struct ib_port_attr attr;
185 ssize_t ret;
186
187 ret = ib_query_port(p->ibdev, p->port_num, &attr);
188 if (ret)
189 return ret;
190
191 return sprintf(buf, "%d\n", attr.sm_sl);
192}
193
194static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
195 char *buf)
196{
197 struct ib_port_attr attr;
198 ssize_t ret;
199
200 ret = ib_query_port(p->ibdev, p->port_num, &attr);
201 if (ret)
202 return ret;
203
204 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
205}
206
207static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
208 char *buf)
209{
210 struct ib_port_attr attr;
211 char *speed = "";
Roland Dreier0559d8d2012-04-02 10:57:31 -0700212 int rate; /* in deci-Gb/sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 ssize_t ret;
214
215 ret = ib_query_port(p->ibdev, p->port_num, &attr);
216 if (ret)
217 return ret;
218
219 switch (attr.active_speed) {
Or Gerlitz2e966912012-02-28 18:49:50 +0200220 case IB_SPEED_DDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300221 speed = " DDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800222 rate = 50;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300223 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200224 case IB_SPEED_QDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300225 speed = " QDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800226 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300227 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200228 case IB_SPEED_FDR10:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300229 speed = " FDR10";
Roland Dreiere9319b02012-02-27 09:15:08 -0800230 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300231 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200232 case IB_SPEED_FDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300233 speed = " FDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800234 rate = 140;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300235 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200236 case IB_SPEED_EDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300237 speed = " EDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800238 rate = 250;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300239 break;
Roland Dreier0559d8d2012-04-02 10:57:31 -0700240 case IB_SPEED_SDR:
241 default: /* default to SDR for invalid rates */
242 rate = 25;
243 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300246 rate *= ib_width_enum_to_int(attr.active_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (rate < 0)
248 return -EINVAL;
249
250 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
Roland Dreiere9319b02012-02-27 09:15:08 -0800251 rate / 10, rate % 10 ? ".5" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ib_width_enum_to_int(attr.active_width), speed);
253}
254
255static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
256 char *buf)
257{
258 struct ib_port_attr attr;
259
260 ssize_t ret;
261
262 ret = ib_query_port(p->ibdev, p->port_num, &attr);
263 if (ret)
264 return ret;
265
266 switch (attr.phys_state) {
267 case 1: return sprintf(buf, "1: Sleep\n");
268 case 2: return sprintf(buf, "2: Polling\n");
269 case 3: return sprintf(buf, "3: Disabled\n");
270 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
271 case 5: return sprintf(buf, "5: LinkUp\n");
272 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
273 case 7: return sprintf(buf, "7: Phy Test\n");
274 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
275 }
276}
277
Eli Cohen8ad330a2010-10-22 14:32:05 +0200278static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
279 char *buf)
280{
281 switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
282 case IB_LINK_LAYER_INFINIBAND:
283 return sprintf(buf, "%s\n", "InfiniBand");
284 case IB_LINK_LAYER_ETHERNET:
285 return sprintf(buf, "%s\n", "Ethernet");
286 default:
287 return sprintf(buf, "%s\n", "Unknown");
288 }
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static PORT_ATTR_RO(state);
292static PORT_ATTR_RO(lid);
293static PORT_ATTR_RO(lid_mask_count);
294static PORT_ATTR_RO(sm_lid);
295static PORT_ATTR_RO(sm_sl);
296static PORT_ATTR_RO(cap_mask);
297static PORT_ATTR_RO(rate);
298static PORT_ATTR_RO(phys_state);
Eli Cohen8ad330a2010-10-22 14:32:05 +0200299static PORT_ATTR_RO(link_layer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301static struct attribute *port_default_attrs[] = {
302 &port_attr_state.attr,
303 &port_attr_lid.attr,
304 &port_attr_lid_mask_count.attr,
305 &port_attr_sm_lid.attr,
306 &port_attr_sm_sl.attr,
307 &port_attr_cap_mask.attr,
308 &port_attr_rate.attr,
309 &port_attr_phys_state.attr,
Eli Cohen8ad330a2010-10-22 14:32:05 +0200310 &port_attr_link_layer.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 NULL
312};
313
Matan Barak470be512015-12-23 14:56:49 +0200314static size_t print_ndev(struct ib_gid_attr *gid_attr, char *buf)
315{
316 if (!gid_attr->ndev)
317 return -EINVAL;
318
319 return sprintf(buf, "%s\n", gid_attr->ndev->name);
320}
321
322static size_t print_gid_type(struct ib_gid_attr *gid_attr, char *buf)
323{
324 return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
325}
326
327static ssize_t _show_port_gid_attr(struct ib_port *p,
328 struct port_attribute *attr,
329 char *buf,
330 size_t (*print)(struct ib_gid_attr *gid_attr,
331 char *buf))
332{
333 struct port_table_attribute *tab_attr =
334 container_of(attr, struct port_table_attribute, attr);
335 union ib_gid gid;
336 struct ib_gid_attr gid_attr = {};
337 ssize_t ret;
338 va_list args;
339
340 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid,
341 &gid_attr);
342 if (ret)
343 goto err;
344
345 ret = print(&gid_attr, buf);
346
347err:
348 if (gid_attr.ndev)
349 dev_put(gid_attr.ndev);
350 va_end(args);
351 return ret;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
355 char *buf)
356{
357 struct port_table_attribute *tab_attr =
358 container_of(attr, struct port_table_attribute, attr);
359 union ib_gid gid;
360 ssize_t ret;
361
Matan Barak55ee3ab2015-10-15 18:38:45 +0300362 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (ret)
364 return ret;
365
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700366 return sprintf(buf, "%pI6\n", gid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Matan Barak470be512015-12-23 14:56:49 +0200369static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
370 struct port_attribute *attr, char *buf)
371{
372 return _show_port_gid_attr(p, attr, buf, print_ndev);
373}
374
375static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
376 struct port_attribute *attr,
377 char *buf)
378{
379 return _show_port_gid_attr(p, attr, buf, print_gid_type);
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
383 char *buf)
384{
385 struct port_table_attribute *tab_attr =
386 container_of(attr, struct port_table_attribute, attr);
387 u16 pkey;
388 ssize_t ret;
389
390 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
391 if (ret)
392 return ret;
393
394 return sprintf(buf, "0x%04x\n", pkey);
395}
396
397#define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
398struct port_table_attribute port_pma_attr_##_name = { \
399 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
Christoph Lameterb2788ce2015-12-21 08:20:28 -0600400 .index = (_offset) | ((_width) << 16) | ((_counter) << 24), \
401 .attr_id = IB_PMA_PORT_COUNTERS , \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600404/*
405 * Get a Perfmgmt MAD block of data.
406 * Returns error code or the number of bytes retrieved.
407 */
408static int get_perf_mad(struct ib_device *dev, int port_num, int attr,
409 void *data, int offset, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600411 struct ib_mad *in_mad;
412 struct ib_mad *out_mad;
Ira Weiny4cd7c942015-06-06 14:38:31 -0400413 size_t mad_size = sizeof(*out_mad);
414 u16 out_mad_pkey_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ssize_t ret;
416
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600417 if (!dev->process_mad)
418 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Roland Dreierde6eb662005-11-02 07:23:14 -0800420 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Dotan Barak856c52a2007-07-10 16:55:57 +0300421 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!in_mad || !out_mad) {
423 ret = -ENOMEM;
424 goto out;
425 }
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 in_mad->mad_hdr.base_version = 1;
428 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
429 in_mad->mad_hdr.class_version = 1;
430 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600431 in_mad->mad_hdr.attr_id = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600433 in_mad->data[41] = port_num; /* PortSelect field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600435 if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY,
436 port_num, NULL, NULL,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400437 (const struct ib_mad_hdr *)in_mad, mad_size,
438 (struct ib_mad_hdr *)out_mad, &mad_size,
439 &out_mad_pkey_index) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
441 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
442 ret = -EINVAL;
443 goto out;
444 }
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600445 memcpy(data, out_mad->data + offset, size);
446 ret = size;
447out:
448 kfree(in_mad);
449 kfree(out_mad);
450 return ret;
451}
452
453static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
454 char *buf)
455{
456 struct port_table_attribute *tab_attr =
457 container_of(attr, struct port_table_attribute, attr);
458 int offset = tab_attr->index & 0xffff;
459 int width = (tab_attr->index >> 16) & 0xff;
460 ssize_t ret;
461 u8 data[8];
462
Christoph Lameterb2788ce2015-12-21 08:20:28 -0600463 ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600464 40 + offset / 8, sizeof(data));
465 if (ret < 0)
466 return sprintf(buf, "N/A (no PMA)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 switch (width) {
469 case 4:
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600470 ret = sprintf(buf, "%u\n", (*data >>
Ralph Campbelld8b9f232006-05-09 10:50:28 -0700471 (4 - (offset % 8))) & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case 8:
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600474 ret = sprintf(buf, "%u\n", *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 case 16:
477 ret = sprintf(buf, "%u\n",
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600478 be16_to_cpup((__be16 *)data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case 32:
481 ret = sprintf(buf, "%u\n",
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600482 be32_to_cpup((__be32 *)data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 default:
485 ret = 0;
486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return ret;
489}
490
491static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
492static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
493static PORT_PMA_ATTR(link_downed , 2, 8, 56);
494static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
495static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
496static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
497static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
498static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
499static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
500static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
501static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
502static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
503static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
504static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
505static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
506static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
507
508static struct attribute *pma_attrs[] = {
509 &port_pma_attr_symbol_error.attr.attr,
510 &port_pma_attr_link_error_recovery.attr.attr,
511 &port_pma_attr_link_downed.attr.attr,
512 &port_pma_attr_port_rcv_errors.attr.attr,
513 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
514 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
515 &port_pma_attr_port_xmit_discards.attr.attr,
516 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
517 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
518 &port_pma_attr_local_link_integrity_errors.attr.attr,
519 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
520 &port_pma_attr_VL15_dropped.attr.attr,
521 &port_pma_attr_port_xmit_data.attr.attr,
522 &port_pma_attr_port_rcv_data.attr.attr,
523 &port_pma_attr_port_xmit_packets.attr.attr,
524 &port_pma_attr_port_rcv_packets.attr.attr,
525 NULL
526};
527
528static struct attribute_group pma_group = {
529 .name = "counters",
530 .attrs = pma_attrs
531};
532
533static void ib_port_release(struct kobject *kobj)
534{
535 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
536 struct attribute *a;
537 int i;
538
Haggai Erancad6d022014-05-18 11:12:25 +0300539 if (p->gid_group.attrs) {
540 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
541 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Haggai Erancad6d022014-05-18 11:12:25 +0300543 kfree(p->gid_group.attrs);
544 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500545
Haggai Erancad6d022014-05-18 11:12:25 +0300546 if (p->pkey_group.attrs) {
547 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
548 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Haggai Erancad6d022014-05-18 11:12:25 +0300550 kfree(p->pkey_group.attrs);
551 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 kfree(p);
554}
555
Matan Barak470be512015-12-23 14:56:49 +0200556static void ib_port_gid_attr_release(struct kobject *kobj)
557{
558 struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
559 kobj);
560 struct attribute *a;
561 int i;
562
563 if (g->ndev.attrs) {
564 for (i = 0; (a = g->ndev.attrs[i]); ++i)
565 kfree(a);
566
567 kfree(g->ndev.attrs);
568 }
569
570 if (g->type.attrs) {
571 for (i = 0; (a = g->type.attrs[i]); ++i)
572 kfree(a);
573
574 kfree(g->type.attrs);
575 }
576
577 kfree(g);
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static struct kobj_type port_type = {
581 .release = ib_port_release,
582 .sysfs_ops = &port_sysfs_ops,
583 .default_attrs = port_default_attrs
584};
585
Matan Barak470be512015-12-23 14:56:49 +0200586static struct kobj_type gid_attr_type = {
587 .sysfs_ops = &gid_attr_sysfs_ops,
588 .release = ib_port_gid_attr_release
589};
590
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500591static struct attribute **
592alloc_group_attrs(ssize_t (*show)(struct ib_port *,
593 struct port_attribute *, char *buf),
594 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500596 struct attribute **tab_attr;
597 struct port_table_attribute *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500600 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
601 if (!tab_attr)
602 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500604 for (i = 0; i < len; i++) {
Pekka Enberg82ca76b2005-09-06 15:18:35 -0700605 element = kzalloc(sizeof(struct port_table_attribute),
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500606 GFP_KERNEL);
607 if (!element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500610 if (snprintf(element->name, sizeof(element->name),
Roland Dreier048975a2006-03-20 10:08:25 -0800611 "%d", i) >= sizeof(element->name)) {
612 kfree(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 goto err;
Roland Dreier048975a2006-03-20 10:08:25 -0800614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500616 element->attr.attr.name = element->name;
617 element->attr.attr.mode = S_IRUGO;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500618 element->attr.show = show;
619 element->index = i;
Greg Kroah-Hartman21e3bde2010-03-15 14:01:25 -0700620 sysfs_attr_init(&element->attr.attr);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500621
622 tab_attr[i] = &element->attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500625 return tab_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627err:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500628 while (--i >= 0)
629 kfree(tab_attr[i]);
630 kfree(tab_attr);
631 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700634static int add_port(struct ib_device *device, int port_num,
635 int (*port_callback)(struct ib_device *,
636 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct ib_port *p;
639 struct ib_port_attr attr;
640 int i;
641 int ret;
642
643 ret = ib_query_port(device, port_num, &attr);
644 if (ret)
645 return ret;
646
Roland Dreierde6eb662005-11-02 07:23:14 -0800647 p = kzalloc(sizeof *p, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!p)
649 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 p->ibdev = device;
652 p->port_num = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400654 ret = kobject_init_and_add(&p->kobj, &port_type,
Haggai Eran373c0ea2014-05-18 11:12:24 +0300655 device->ports_parent,
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400656 "%d", port_num);
Haggai Erancad6d022014-05-18 11:12:25 +0300657 if (ret) {
658 kfree(p);
659 return ret;
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Matan Barak470be512015-12-23 14:56:49 +0200662 p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
663 if (!p->gid_attr_group) {
664 ret = -ENOMEM;
665 goto err_put;
666 }
667
668 p->gid_attr_group->port = p;
669 ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
670 &p->kobj, "gid_attrs");
671 if (ret) {
672 kfree(p->gid_attr_group);
673 goto err_put;
674 }
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 ret = sysfs_create_group(&p->kobj, &pma_group);
677 if (ret)
Matan Barak470be512015-12-23 14:56:49 +0200678 goto err_put_gid_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 p->gid_group.name = "gids";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500681 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800682 if (!p->gid_group.attrs) {
683 ret = -ENOMEM;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500684 goto err_remove_pma;
Wei Yongjun80b15042013-06-21 11:24:27 +0800685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 ret = sysfs_create_group(&p->kobj, &p->gid_group);
688 if (ret)
689 goto err_free_gid;
690
Matan Barak470be512015-12-23 14:56:49 +0200691 p->gid_attr_group->ndev.name = "ndevs";
692 p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
693 attr.gid_tbl_len);
694 if (!p->gid_attr_group->ndev.attrs) {
695 ret = -ENOMEM;
696 goto err_remove_gid;
697 }
698
699 ret = sysfs_create_group(&p->gid_attr_group->kobj,
700 &p->gid_attr_group->ndev);
701 if (ret)
702 goto err_free_gid_ndev;
703
704 p->gid_attr_group->type.name = "types";
705 p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
706 attr.gid_tbl_len);
707 if (!p->gid_attr_group->type.attrs) {
708 ret = -ENOMEM;
709 goto err_remove_gid_ndev;
710 }
711
712 ret = sysfs_create_group(&p->gid_attr_group->kobj,
713 &p->gid_attr_group->type);
714 if (ret)
715 goto err_free_gid_type;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 p->pkey_group.name = "pkeys";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500718 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
719 attr.pkey_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800720 if (!p->pkey_group.attrs) {
721 ret = -ENOMEM;
Matan Barak470be512015-12-23 14:56:49 +0200722 goto err_remove_gid_type;
Wei Yongjun80b15042013-06-21 11:24:27 +0800723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
726 if (ret)
727 goto err_free_pkey;
728
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700729 if (port_callback) {
730 ret = port_callback(device, port_num, &p->kobj);
731 if (ret)
732 goto err_remove_pkey;
733 }
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 list_add_tail(&p->kobj.entry, &device->port_list);
736
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400737 kobject_uevent(&p->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 0;
739
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700740err_remove_pkey:
741 sysfs_remove_group(&p->kobj, &p->pkey_group);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743err_free_pkey:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500744 for (i = 0; i < attr.pkey_tbl_len; ++i)
745 kfree(p->pkey_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500747 kfree(p->pkey_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300748 p->pkey_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Matan Barak470be512015-12-23 14:56:49 +0200750err_remove_gid_type:
751 sysfs_remove_group(&p->gid_attr_group->kobj,
752 &p->gid_attr_group->type);
753
754err_free_gid_type:
755 for (i = 0; i < attr.gid_tbl_len; ++i)
756 kfree(p->gid_attr_group->type.attrs[i]);
757
758 kfree(p->gid_attr_group->type.attrs);
759 p->gid_attr_group->type.attrs = NULL;
760
761err_remove_gid_ndev:
762 sysfs_remove_group(&p->gid_attr_group->kobj,
763 &p->gid_attr_group->ndev);
764
765err_free_gid_ndev:
766 for (i = 0; i < attr.gid_tbl_len; ++i)
767 kfree(p->gid_attr_group->ndev.attrs[i]);
768
769 kfree(p->gid_attr_group->ndev.attrs);
770 p->gid_attr_group->ndev.attrs = NULL;
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772err_remove_gid:
773 sysfs_remove_group(&p->kobj, &p->gid_group);
774
775err_free_gid:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500776 for (i = 0; i < attr.gid_tbl_len; ++i)
777 kfree(p->gid_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500779 kfree(p->gid_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300780 p->gid_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782err_remove_pma:
783 sysfs_remove_group(&p->kobj, &pma_group);
784
Matan Barak470be512015-12-23 14:56:49 +0200785err_put_gid_attrs:
786 kobject_put(&p->gid_attr_group->kobj);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788err_put:
Haggai Erancad6d022014-05-18 11:12:25 +0300789 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return ret;
791}
792
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100793static ssize_t show_node_type(struct device *device,
794 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100796 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 switch (dev->node_type) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500799 case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
800 case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
Upinder Malhi \(umalhi\)180771a2013-09-10 03:36:59 +0000801 case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
Upinder Malhi5db57652014-01-15 17:02:36 -0800802 case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500803 case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
804 case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
805 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807}
808
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100809static ssize_t show_sys_image_guid(struct device *device,
810 struct device_attribute *dev_attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100812 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Or Gerlitz86bee4c2015-12-18 10:59:45 +0200815 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
816 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
817 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
818 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100821static ssize_t show_node_guid(struct device *device,
822 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100824 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Sean Heftycf311cd2006-01-10 07:39:34 -0800827 be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
828 be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
829 be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
830 be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100833static ssize_t show_node_desc(struct device *device,
834 struct device_attribute *attr, char *buf)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800835{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100836 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800837
838 return sprintf(buf, "%.64s\n", dev->node_desc);
839}
840
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100841static ssize_t set_node_desc(struct device *device,
842 struct device_attribute *attr,
843 const char *buf, size_t count)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800844{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100845 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800846 struct ib_device_modify desc = {};
847 int ret;
848
849 if (!dev->modify_device)
850 return -EIO;
851
852 memcpy(desc.node_desc, buf, min_t(int, count, 64));
853 ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
854 if (ret)
855 return ret;
856
857 return count;
858}
859
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100860static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
861static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
862static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
863static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100865static struct device_attribute *ib_class_attributes[] = {
866 &dev_attr_node_type,
867 &dev_attr_sys_image_guid,
868 &dev_attr_node_guid,
869 &dev_attr_node_desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870};
871
Steve Wise7f624d02008-07-14 23:48:48 -0700872/* Show a given an attribute in the statistics group */
873static ssize_t show_protocol_stat(const struct device *device,
874 struct device_attribute *attr, char *buf,
875 unsigned offset)
876{
877 struct ib_device *dev = container_of(device, struct ib_device, dev);
878 union rdma_protocol_stats stats;
879 ssize_t ret;
880
881 ret = dev->get_protocol_stats(dev, &stats);
882 if (ret)
883 return ret;
884
885 return sprintf(buf, "%llu\n",
886 (unsigned long long) ((u64 *) &stats)[offset]);
887}
888
889/* generate a read-only iwarp statistics attribute */
890#define IW_STATS_ENTRY(name) \
891static ssize_t show_##name(struct device *device, \
892 struct device_attribute *attr, char *buf) \
893{ \
894 return show_protocol_stat(device, attr, buf, \
895 offsetof(struct iw_protocol_stats, name) / \
896 sizeof (u64)); \
897} \
898static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
899
900IW_STATS_ENTRY(ipInReceives);
901IW_STATS_ENTRY(ipInHdrErrors);
902IW_STATS_ENTRY(ipInTooBigErrors);
903IW_STATS_ENTRY(ipInNoRoutes);
904IW_STATS_ENTRY(ipInAddrErrors);
905IW_STATS_ENTRY(ipInUnknownProtos);
906IW_STATS_ENTRY(ipInTruncatedPkts);
907IW_STATS_ENTRY(ipInDiscards);
908IW_STATS_ENTRY(ipInDelivers);
909IW_STATS_ENTRY(ipOutForwDatagrams);
910IW_STATS_ENTRY(ipOutRequests);
911IW_STATS_ENTRY(ipOutDiscards);
912IW_STATS_ENTRY(ipOutNoRoutes);
913IW_STATS_ENTRY(ipReasmTimeout);
914IW_STATS_ENTRY(ipReasmReqds);
915IW_STATS_ENTRY(ipReasmOKs);
916IW_STATS_ENTRY(ipReasmFails);
917IW_STATS_ENTRY(ipFragOKs);
918IW_STATS_ENTRY(ipFragFails);
919IW_STATS_ENTRY(ipFragCreates);
920IW_STATS_ENTRY(ipInMcastPkts);
921IW_STATS_ENTRY(ipOutMcastPkts);
922IW_STATS_ENTRY(ipInBcastPkts);
923IW_STATS_ENTRY(ipOutBcastPkts);
924IW_STATS_ENTRY(tcpRtoAlgorithm);
925IW_STATS_ENTRY(tcpRtoMin);
926IW_STATS_ENTRY(tcpRtoMax);
927IW_STATS_ENTRY(tcpMaxConn);
928IW_STATS_ENTRY(tcpActiveOpens);
929IW_STATS_ENTRY(tcpPassiveOpens);
930IW_STATS_ENTRY(tcpAttemptFails);
931IW_STATS_ENTRY(tcpEstabResets);
932IW_STATS_ENTRY(tcpCurrEstab);
933IW_STATS_ENTRY(tcpInSegs);
934IW_STATS_ENTRY(tcpOutSegs);
935IW_STATS_ENTRY(tcpRetransSegs);
936IW_STATS_ENTRY(tcpInErrs);
937IW_STATS_ENTRY(tcpOutRsts);
938
939static struct attribute *iw_proto_stats_attrs[] = {
940 &dev_attr_ipInReceives.attr,
941 &dev_attr_ipInHdrErrors.attr,
942 &dev_attr_ipInTooBigErrors.attr,
943 &dev_attr_ipInNoRoutes.attr,
944 &dev_attr_ipInAddrErrors.attr,
945 &dev_attr_ipInUnknownProtos.attr,
946 &dev_attr_ipInTruncatedPkts.attr,
947 &dev_attr_ipInDiscards.attr,
948 &dev_attr_ipInDelivers.attr,
949 &dev_attr_ipOutForwDatagrams.attr,
950 &dev_attr_ipOutRequests.attr,
951 &dev_attr_ipOutDiscards.attr,
952 &dev_attr_ipOutNoRoutes.attr,
953 &dev_attr_ipReasmTimeout.attr,
954 &dev_attr_ipReasmReqds.attr,
955 &dev_attr_ipReasmOKs.attr,
956 &dev_attr_ipReasmFails.attr,
957 &dev_attr_ipFragOKs.attr,
958 &dev_attr_ipFragFails.attr,
959 &dev_attr_ipFragCreates.attr,
960 &dev_attr_ipInMcastPkts.attr,
961 &dev_attr_ipOutMcastPkts.attr,
962 &dev_attr_ipInBcastPkts.attr,
963 &dev_attr_ipOutBcastPkts.attr,
964 &dev_attr_tcpRtoAlgorithm.attr,
965 &dev_attr_tcpRtoMin.attr,
966 &dev_attr_tcpRtoMax.attr,
967 &dev_attr_tcpMaxConn.attr,
968 &dev_attr_tcpActiveOpens.attr,
969 &dev_attr_tcpPassiveOpens.attr,
970 &dev_attr_tcpAttemptFails.attr,
971 &dev_attr_tcpEstabResets.attr,
972 &dev_attr_tcpCurrEstab.attr,
973 &dev_attr_tcpInSegs.attr,
974 &dev_attr_tcpOutSegs.attr,
975 &dev_attr_tcpRetransSegs.attr,
976 &dev_attr_tcpInErrs.attr,
977 &dev_attr_tcpOutRsts.attr,
978 NULL
979};
980
981static struct attribute_group iw_stats_group = {
982 .name = "proto_stats",
983 .attrs = iw_proto_stats_attrs,
984};
985
Haggai Eran584482a2014-05-18 11:12:26 +0300986static void free_port_list_attributes(struct ib_device *device)
987{
988 struct kobject *p, *t;
989
990 list_for_each_entry_safe(p, t, &device->port_list, entry) {
991 struct ib_port *port = container_of(p, struct ib_port, kobj);
992 list_del(&p->entry);
993 sysfs_remove_group(p, &pma_group);
994 sysfs_remove_group(p, &port->pkey_group);
995 sysfs_remove_group(p, &port->gid_group);
Matan Barak470be512015-12-23 14:56:49 +0200996 sysfs_remove_group(&port->gid_attr_group->kobj,
997 &port->gid_attr_group->ndev);
998 sysfs_remove_group(&port->gid_attr_group->kobj,
999 &port->gid_attr_group->type);
1000 kobject_put(&port->gid_attr_group->kobj);
Haggai Eran584482a2014-05-18 11:12:26 +03001001 kobject_put(p);
1002 }
1003
1004 kobject_put(device->ports_parent);
1005}
1006
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001007int ib_device_register_sysfs(struct ib_device *device,
1008 int (*port_callback)(struct ib_device *,
1009 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001011 struct device *class_dev = &device->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int ret;
1013 int i;
1014
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001015 device->dev.parent = device->dma_device;
1016 ret = dev_set_name(class_dev, "%s", device->name);
1017 if (ret)
1018 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001020 ret = device_add(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (ret)
1022 goto err;
1023
1024 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001025 ret = device_create_file(class_dev, ib_class_attributes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (ret)
1027 goto err_unregister;
1028 }
1029
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -04001030 device->ports_parent = kobject_create_and_add("ports",
Haggai Eran373c0ea2014-05-18 11:12:24 +03001031 &class_dev->kobj);
Li Zefanc7482b82008-02-15 10:24:49 +08001032 if (!device->ports_parent) {
1033 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto err_put;
Li Zefanc7482b82008-02-15 10:24:49 +08001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Hal Rosenstock41390322015-06-29 09:57:00 -04001037 if (rdma_cap_ib_switch(device)) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001038 ret = add_port(device, 0, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (ret)
1040 goto err_put;
1041 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 for (i = 1; i <= device->phys_port_cnt; ++i) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001043 ret = add_port(device, i, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (ret)
1045 goto err_put;
1046 }
1047 }
1048
Steve Wise7f624d02008-07-14 23:48:48 -07001049 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
1050 ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
1051 if (ret)
1052 goto err_put;
1053 }
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return 0;
1056
1057err_put:
Haggai Eran584482a2014-05-18 11:12:26 +03001058 free_port_list_attributes(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060err_unregister:
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001061 device_unregister(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063err:
1064 return ret;
1065}
1066
1067void ib_device_unregister_sysfs(struct ib_device *device)
1068{
Roland Dreier9206dff2009-02-25 13:27:46 -08001069 /* Hold kobject until ib_dealloc_device() */
Haggai Eran584482a2014-05-18 11:12:26 +03001070 struct kobject *kobj_dev = kobject_get(&device->dev.kobj);
1071 int i;
Roland Dreier9206dff2009-02-25 13:27:46 -08001072
Haggai Eran584482a2014-05-18 11:12:26 +03001073 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats)
1074 sysfs_remove_group(kobj_dev, &iw_stats_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Haggai Eran584482a2014-05-18 11:12:26 +03001076 free_port_list_attributes(device);
1077
1078 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
1079 device_remove_file(&device->dev, ib_class_attributes[i]);
1080
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001081 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}