blob: 3de93517efe43b097d7f58e37cfbe36bd8ab0f65 [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;
Christoph Lameter145d9c52015-12-21 08:20:29 -060060 struct attribute_group *pma_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
63struct port_attribute {
64 struct attribute attr;
65 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
66 ssize_t (*store)(struct ib_port *, struct port_attribute *,
67 const char *buf, size_t count);
68};
69
70#define PORT_ATTR(_name, _mode, _show, _store) \
71struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
72
73#define PORT_ATTR_RO(_name) \
74struct port_attribute port_attr_##_name = __ATTR_RO(_name)
75
76struct port_table_attribute {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -050077 struct port_attribute attr;
78 char name[8];
79 int index;
Ira Weiny65487fd2016-01-03 22:44:25 -050080 __be16 attr_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83static ssize_t port_attr_show(struct kobject *kobj,
84 struct attribute *attr, char *buf)
85{
86 struct port_attribute *port_attr =
87 container_of(attr, struct port_attribute, attr);
88 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
89
90 if (!port_attr->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -050091 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 return port_attr->show(p, port_attr, buf);
94}
95
Emese Revfy52cf25d2010-01-19 02:58:23 +010096static const struct sysfs_ops port_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .show = port_attr_show
98};
99
Matan Barak470be512015-12-23 14:56:49 +0200100static ssize_t gid_attr_show(struct kobject *kobj,
101 struct attribute *attr, char *buf)
102{
103 struct port_attribute *port_attr =
104 container_of(attr, struct port_attribute, attr);
105 struct ib_port *p = container_of(kobj, struct gid_attr_group,
106 kobj)->port;
107
108 if (!port_attr->show)
109 return -EIO;
110
111 return port_attr->show(p, port_attr, buf);
112}
113
114static const struct sysfs_ops gid_attr_sysfs_ops = {
115 .show = gid_attr_show
116};
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
119 char *buf)
120{
121 struct ib_port_attr attr;
122 ssize_t ret;
123
124 static const char *state_name[] = {
125 [IB_PORT_NOP] = "NOP",
126 [IB_PORT_DOWN] = "DOWN",
127 [IB_PORT_INIT] = "INIT",
128 [IB_PORT_ARMED] = "ARMED",
129 [IB_PORT_ACTIVE] = "ACTIVE",
130 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
131 };
132
133 ret = ib_query_port(p->ibdev, p->port_num, &attr);
134 if (ret)
135 return ret;
136
137 return sprintf(buf, "%d: %s\n", attr.state,
Roland Dreier048975a2006-03-20 10:08:25 -0800138 attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 state_name[attr.state] : "UNKNOWN");
140}
141
142static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
143 char *buf)
144{
145 struct ib_port_attr attr;
146 ssize_t ret;
147
148 ret = ib_query_port(p->ibdev, p->port_num, &attr);
149 if (ret)
150 return ret;
151
152 return sprintf(buf, "0x%x\n", attr.lid);
153}
154
155static ssize_t lid_mask_count_show(struct ib_port *p,
156 struct port_attribute *unused,
157 char *buf)
158{
159 struct ib_port_attr attr;
160 ssize_t ret;
161
162 ret = ib_query_port(p->ibdev, p->port_num, &attr);
163 if (ret)
164 return ret;
165
166 return sprintf(buf, "%d\n", attr.lmc);
167}
168
169static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
170 char *buf)
171{
172 struct ib_port_attr attr;
173 ssize_t ret;
174
175 ret = ib_query_port(p->ibdev, p->port_num, &attr);
176 if (ret)
177 return ret;
178
179 return sprintf(buf, "0x%x\n", attr.sm_lid);
180}
181
182static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
183 char *buf)
184{
185 struct ib_port_attr attr;
186 ssize_t ret;
187
188 ret = ib_query_port(p->ibdev, p->port_num, &attr);
189 if (ret)
190 return ret;
191
192 return sprintf(buf, "%d\n", attr.sm_sl);
193}
194
195static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
196 char *buf)
197{
198 struct ib_port_attr attr;
199 ssize_t ret;
200
201 ret = ib_query_port(p->ibdev, p->port_num, &attr);
202 if (ret)
203 return ret;
204
205 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
206}
207
208static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
209 char *buf)
210{
211 struct ib_port_attr attr;
212 char *speed = "";
Roland Dreier0559d8d2012-04-02 10:57:31 -0700213 int rate; /* in deci-Gb/sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ssize_t ret;
215
216 ret = ib_query_port(p->ibdev, p->port_num, &attr);
217 if (ret)
218 return ret;
219
220 switch (attr.active_speed) {
Or Gerlitz2e966912012-02-28 18:49:50 +0200221 case IB_SPEED_DDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300222 speed = " DDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800223 rate = 50;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300224 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200225 case IB_SPEED_QDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300226 speed = " QDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800227 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300228 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200229 case IB_SPEED_FDR10:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300230 speed = " FDR10";
Roland Dreiere9319b02012-02-27 09:15:08 -0800231 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300232 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200233 case IB_SPEED_FDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300234 speed = " FDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800235 rate = 140;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300236 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200237 case IB_SPEED_EDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300238 speed = " EDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800239 rate = 250;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300240 break;
Roland Dreier0559d8d2012-04-02 10:57:31 -0700241 case IB_SPEED_SDR:
242 default: /* default to SDR for invalid rates */
243 rate = 25;
244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300247 rate *= ib_width_enum_to_int(attr.active_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (rate < 0)
249 return -EINVAL;
250
251 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
Roland Dreiere9319b02012-02-27 09:15:08 -0800252 rate / 10, rate % 10 ? ".5" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ib_width_enum_to_int(attr.active_width), speed);
254}
255
256static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
257 char *buf)
258{
259 struct ib_port_attr attr;
260
261 ssize_t ret;
262
263 ret = ib_query_port(p->ibdev, p->port_num, &attr);
264 if (ret)
265 return ret;
266
267 switch (attr.phys_state) {
268 case 1: return sprintf(buf, "1: Sleep\n");
269 case 2: return sprintf(buf, "2: Polling\n");
270 case 3: return sprintf(buf, "3: Disabled\n");
271 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
272 case 5: return sprintf(buf, "5: LinkUp\n");
273 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
274 case 7: return sprintf(buf, "7: Phy Test\n");
275 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
276 }
277}
278
Eli Cohen8ad330a2010-10-22 14:32:05 +0200279static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
280 char *buf)
281{
282 switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
283 case IB_LINK_LAYER_INFINIBAND:
284 return sprintf(buf, "%s\n", "InfiniBand");
285 case IB_LINK_LAYER_ETHERNET:
286 return sprintf(buf, "%s\n", "Ethernet");
287 default:
288 return sprintf(buf, "%s\n", "Unknown");
289 }
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static PORT_ATTR_RO(state);
293static PORT_ATTR_RO(lid);
294static PORT_ATTR_RO(lid_mask_count);
295static PORT_ATTR_RO(sm_lid);
296static PORT_ATTR_RO(sm_sl);
297static PORT_ATTR_RO(cap_mask);
298static PORT_ATTR_RO(rate);
299static PORT_ATTR_RO(phys_state);
Eli Cohen8ad330a2010-10-22 14:32:05 +0200300static PORT_ATTR_RO(link_layer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302static struct attribute *port_default_attrs[] = {
303 &port_attr_state.attr,
304 &port_attr_lid.attr,
305 &port_attr_lid_mask_count.attr,
306 &port_attr_sm_lid.attr,
307 &port_attr_sm_sl.attr,
308 &port_attr_cap_mask.attr,
309 &port_attr_rate.attr,
310 &port_attr_phys_state.attr,
Eli Cohen8ad330a2010-10-22 14:32:05 +0200311 &port_attr_link_layer.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 NULL
313};
314
Matan Barak470be512015-12-23 14:56:49 +0200315static size_t print_ndev(struct ib_gid_attr *gid_attr, char *buf)
316{
317 if (!gid_attr->ndev)
318 return -EINVAL;
319
320 return sprintf(buf, "%s\n", gid_attr->ndev->name);
321}
322
323static size_t print_gid_type(struct ib_gid_attr *gid_attr, char *buf)
324{
325 return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
326}
327
328static ssize_t _show_port_gid_attr(struct ib_port *p,
329 struct port_attribute *attr,
330 char *buf,
331 size_t (*print)(struct ib_gid_attr *gid_attr,
332 char *buf))
333{
334 struct port_table_attribute *tab_attr =
335 container_of(attr, struct port_table_attribute, attr);
336 union ib_gid gid;
337 struct ib_gid_attr gid_attr = {};
338 ssize_t ret;
339 va_list args;
340
341 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid,
342 &gid_attr);
343 if (ret)
344 goto err;
345
346 ret = print(&gid_attr, buf);
347
348err:
349 if (gid_attr.ndev)
350 dev_put(gid_attr.ndev);
351 va_end(args);
352 return ret;
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
356 char *buf)
357{
358 struct port_table_attribute *tab_attr =
359 container_of(attr, struct port_table_attribute, attr);
360 union ib_gid gid;
361 ssize_t ret;
362
Matan Barak55ee3ab2015-10-15 18:38:45 +0300363 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (ret)
365 return ret;
366
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700367 return sprintf(buf, "%pI6\n", gid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Matan Barak470be512015-12-23 14:56:49 +0200370static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
371 struct port_attribute *attr, char *buf)
372{
373 return _show_port_gid_attr(p, attr, buf, print_ndev);
374}
375
376static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
377 struct port_attribute *attr,
378 char *buf)
379{
380 return _show_port_gid_attr(p, attr, buf, print_gid_type);
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
384 char *buf)
385{
386 struct port_table_attribute *tab_attr =
387 container_of(attr, struct port_table_attribute, attr);
388 u16 pkey;
389 ssize_t ret;
390
391 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
392 if (ret)
393 return ret;
394
395 return sprintf(buf, "0x%04x\n", pkey);
396}
397
398#define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
399struct port_table_attribute port_pma_attr_##_name = { \
400 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
Christoph Lameterb2788ce2015-12-21 08:20:28 -0600401 .index = (_offset) | ((_width) << 16) | ((_counter) << 24), \
402 .attr_id = IB_PMA_PORT_COUNTERS , \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Christoph Lameter145d9c52015-12-21 08:20:29 -0600405#define PORT_PMA_ATTR_EXT(_name, _width, _offset) \
406struct port_table_attribute port_pma_attr_ext_##_name = { \
407 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
408 .index = (_offset) | ((_width) << 16), \
409 .attr_id = IB_PMA_PORT_COUNTERS_EXT , \
410}
411
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600412/*
413 * Get a Perfmgmt MAD block of data.
414 * Returns error code or the number of bytes retrieved.
415 */
Ira Weiny65487fd2016-01-03 22:44:25 -0500416static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600417 void *data, int offset, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600419 struct ib_mad *in_mad;
420 struct ib_mad *out_mad;
Ira Weiny4cd7c942015-06-06 14:38:31 -0400421 size_t mad_size = sizeof(*out_mad);
422 u16 out_mad_pkey_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 ssize_t ret;
424
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600425 if (!dev->process_mad)
426 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Roland Dreierde6eb662005-11-02 07:23:14 -0800428 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Dotan Barak856c52a2007-07-10 16:55:57 +0300429 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (!in_mad || !out_mad) {
431 ret = -ENOMEM;
432 goto out;
433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 in_mad->mad_hdr.base_version = 1;
436 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
437 in_mad->mad_hdr.class_version = 1;
438 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600439 in_mad->mad_hdr.attr_id = attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Hal Rosenstock6e2a51a2015-12-29 05:43:43 -0500441 if (attr != IB_PMA_CLASS_PORT_INFO)
442 in_mad->data[41] = port_num; /* PortSelect field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600444 if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY,
445 port_num, NULL, NULL,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400446 (const struct ib_mad_hdr *)in_mad, mad_size,
447 (struct ib_mad_hdr *)out_mad, &mad_size,
448 &out_mad_pkey_index) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
450 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
451 ret = -EINVAL;
452 goto out;
453 }
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600454 memcpy(data, out_mad->data + offset, size);
455 ret = size;
456out:
457 kfree(in_mad);
458 kfree(out_mad);
459 return ret;
460}
461
462static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
463 char *buf)
464{
465 struct port_table_attribute *tab_attr =
466 container_of(attr, struct port_table_attribute, attr);
467 int offset = tab_attr->index & 0xffff;
468 int width = (tab_attr->index >> 16) & 0xff;
469 ssize_t ret;
470 u8 data[8];
471
Christoph Lameterb2788ce2015-12-21 08:20:28 -0600472 ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600473 40 + offset / 8, sizeof(data));
474 if (ret < 0)
475 return sprintf(buf, "N/A (no PMA)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 switch (width) {
478 case 4:
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600479 ret = sprintf(buf, "%u\n", (*data >>
Ralph Campbelld8b9f232006-05-09 10:50:28 -0700480 (4 - (offset % 8))) & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482 case 8:
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600483 ret = sprintf(buf, "%u\n", *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 case 16:
486 ret = sprintf(buf, "%u\n",
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600487 be16_to_cpup((__be16 *)data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 case 32:
490 ret = sprintf(buf, "%u\n",
Christoph Lameter35c4cbb2015-12-21 08:20:27 -0600491 be32_to_cpup((__be32 *)data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 break;
Christoph Lameter145d9c52015-12-21 08:20:29 -0600493 case 64:
494 ret = sprintf(buf, "%llu\n",
495 be64_to_cpup((__be64 *)data));
496 break;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 default:
499 ret = 0;
500 }
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return ret;
503}
504
505static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
506static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
507static PORT_PMA_ATTR(link_downed , 2, 8, 56);
508static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
509static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
510static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
511static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
512static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
513static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
514static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
515static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
516static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
517static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
518static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
519static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
520static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
521
Christoph Lameter145d9c52015-12-21 08:20:29 -0600522/*
523 * Counters added by extended set
524 */
525static PORT_PMA_ATTR_EXT(port_xmit_data , 64, 64);
526static PORT_PMA_ATTR_EXT(port_rcv_data , 64, 128);
527static PORT_PMA_ATTR_EXT(port_xmit_packets , 64, 192);
528static PORT_PMA_ATTR_EXT(port_rcv_packets , 64, 256);
529static PORT_PMA_ATTR_EXT(unicast_xmit_packets , 64, 320);
530static PORT_PMA_ATTR_EXT(unicast_rcv_packets , 64, 384);
531static PORT_PMA_ATTR_EXT(multicast_xmit_packets , 64, 448);
532static PORT_PMA_ATTR_EXT(multicast_rcv_packets , 64, 512);
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534static struct attribute *pma_attrs[] = {
535 &port_pma_attr_symbol_error.attr.attr,
536 &port_pma_attr_link_error_recovery.attr.attr,
537 &port_pma_attr_link_downed.attr.attr,
538 &port_pma_attr_port_rcv_errors.attr.attr,
539 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
540 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
541 &port_pma_attr_port_xmit_discards.attr.attr,
542 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
543 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
544 &port_pma_attr_local_link_integrity_errors.attr.attr,
545 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
546 &port_pma_attr_VL15_dropped.attr.attr,
547 &port_pma_attr_port_xmit_data.attr.attr,
548 &port_pma_attr_port_rcv_data.attr.attr,
549 &port_pma_attr_port_xmit_packets.attr.attr,
550 &port_pma_attr_port_rcv_packets.attr.attr,
551 NULL
552};
553
Christoph Lameter145d9c52015-12-21 08:20:29 -0600554static struct attribute *pma_attrs_ext[] = {
555 &port_pma_attr_symbol_error.attr.attr,
556 &port_pma_attr_link_error_recovery.attr.attr,
557 &port_pma_attr_link_downed.attr.attr,
558 &port_pma_attr_port_rcv_errors.attr.attr,
559 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
560 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
561 &port_pma_attr_port_xmit_discards.attr.attr,
562 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
563 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
564 &port_pma_attr_local_link_integrity_errors.attr.attr,
565 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
566 &port_pma_attr_VL15_dropped.attr.attr,
567 &port_pma_attr_ext_port_xmit_data.attr.attr,
568 &port_pma_attr_ext_port_rcv_data.attr.attr,
569 &port_pma_attr_ext_port_xmit_packets.attr.attr,
570 &port_pma_attr_ext_port_rcv_packets.attr.attr,
571 &port_pma_attr_ext_unicast_rcv_packets.attr.attr,
572 &port_pma_attr_ext_unicast_xmit_packets.attr.attr,
573 &port_pma_attr_ext_multicast_rcv_packets.attr.attr,
574 &port_pma_attr_ext_multicast_xmit_packets.attr.attr,
575 NULL
576};
577
578static struct attribute *pma_attrs_noietf[] = {
579 &port_pma_attr_symbol_error.attr.attr,
580 &port_pma_attr_link_error_recovery.attr.attr,
581 &port_pma_attr_link_downed.attr.attr,
582 &port_pma_attr_port_rcv_errors.attr.attr,
583 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
584 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
585 &port_pma_attr_port_xmit_discards.attr.attr,
586 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
587 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
588 &port_pma_attr_local_link_integrity_errors.attr.attr,
589 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
590 &port_pma_attr_VL15_dropped.attr.attr,
591 &port_pma_attr_ext_port_xmit_data.attr.attr,
592 &port_pma_attr_ext_port_rcv_data.attr.attr,
593 &port_pma_attr_ext_port_xmit_packets.attr.attr,
594 &port_pma_attr_ext_port_rcv_packets.attr.attr,
595 NULL
596};
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static struct attribute_group pma_group = {
599 .name = "counters",
600 .attrs = pma_attrs
601};
602
Christoph Lameter145d9c52015-12-21 08:20:29 -0600603static struct attribute_group pma_group_ext = {
604 .name = "counters",
605 .attrs = pma_attrs_ext
606};
607
608static struct attribute_group pma_group_noietf = {
609 .name = "counters",
610 .attrs = pma_attrs_noietf
611};
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613static void ib_port_release(struct kobject *kobj)
614{
615 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
616 struct attribute *a;
617 int i;
618
Haggai Erancad6d022014-05-18 11:12:25 +0300619 if (p->gid_group.attrs) {
620 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
621 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Haggai Erancad6d022014-05-18 11:12:25 +0300623 kfree(p->gid_group.attrs);
624 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500625
Haggai Erancad6d022014-05-18 11:12:25 +0300626 if (p->pkey_group.attrs) {
627 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
628 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Haggai Erancad6d022014-05-18 11:12:25 +0300630 kfree(p->pkey_group.attrs);
631 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 kfree(p);
634}
635
Matan Barak470be512015-12-23 14:56:49 +0200636static void ib_port_gid_attr_release(struct kobject *kobj)
637{
638 struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
639 kobj);
640 struct attribute *a;
641 int i;
642
643 if (g->ndev.attrs) {
644 for (i = 0; (a = g->ndev.attrs[i]); ++i)
645 kfree(a);
646
647 kfree(g->ndev.attrs);
648 }
649
650 if (g->type.attrs) {
651 for (i = 0; (a = g->type.attrs[i]); ++i)
652 kfree(a);
653
654 kfree(g->type.attrs);
655 }
656
657 kfree(g);
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static struct kobj_type port_type = {
661 .release = ib_port_release,
662 .sysfs_ops = &port_sysfs_ops,
663 .default_attrs = port_default_attrs
664};
665
Matan Barak470be512015-12-23 14:56:49 +0200666static struct kobj_type gid_attr_type = {
667 .sysfs_ops = &gid_attr_sysfs_ops,
668 .release = ib_port_gid_attr_release
669};
670
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500671static struct attribute **
672alloc_group_attrs(ssize_t (*show)(struct ib_port *,
673 struct port_attribute *, char *buf),
674 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500676 struct attribute **tab_attr;
677 struct port_table_attribute *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500680 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
681 if (!tab_attr)
682 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500684 for (i = 0; i < len; i++) {
Pekka Enberg82ca76b2005-09-06 15:18:35 -0700685 element = kzalloc(sizeof(struct port_table_attribute),
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500686 GFP_KERNEL);
687 if (!element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500690 if (snprintf(element->name, sizeof(element->name),
Roland Dreier048975a2006-03-20 10:08:25 -0800691 "%d", i) >= sizeof(element->name)) {
692 kfree(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goto err;
Roland Dreier048975a2006-03-20 10:08:25 -0800694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500696 element->attr.attr.name = element->name;
697 element->attr.attr.mode = S_IRUGO;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500698 element->attr.show = show;
699 element->index = i;
Greg Kroah-Hartman21e3bde2010-03-15 14:01:25 -0700700 sysfs_attr_init(&element->attr.attr);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500701
702 tab_attr[i] = &element->attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
704
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500705 return tab_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707err:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500708 while (--i >= 0)
709 kfree(tab_attr[i]);
710 kfree(tab_attr);
711 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Christoph Lameter145d9c52015-12-21 08:20:29 -0600714/*
715 * Figure out which counter table to use depending on
716 * the device capabilities.
717 */
Hal Rosenstock6e2a51a2015-12-29 05:43:43 -0500718static struct attribute_group *get_counter_table(struct ib_device *dev,
719 int port_num)
Christoph Lameter145d9c52015-12-21 08:20:29 -0600720{
721 struct ib_class_port_info cpi;
722
Hal Rosenstock6e2a51a2015-12-29 05:43:43 -0500723 if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
Christoph Lameter145d9c52015-12-21 08:20:29 -0600724 &cpi, 40, sizeof(cpi)) >= 0) {
725
726 if (cpi.capability_mask && IB_PMA_CLASS_CAP_EXT_WIDTH)
727 /* We have extended counters */
728 return &pma_group_ext;
729
730 if (cpi.capability_mask && IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
731 /* But not the IETF ones */
732 return &pma_group_noietf;
733 }
734
735 /* Fall back to normal counters */
736 return &pma_group;
737}
738
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700739static int add_port(struct ib_device *device, int port_num,
740 int (*port_callback)(struct ib_device *,
741 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct ib_port *p;
744 struct ib_port_attr attr;
745 int i;
746 int ret;
747
748 ret = ib_query_port(device, port_num, &attr);
749 if (ret)
750 return ret;
751
Roland Dreierde6eb662005-11-02 07:23:14 -0800752 p = kzalloc(sizeof *p, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!p)
754 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 p->ibdev = device;
757 p->port_num = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400759 ret = kobject_init_and_add(&p->kobj, &port_type,
Haggai Eran373c0ea2014-05-18 11:12:24 +0300760 device->ports_parent,
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400761 "%d", port_num);
Haggai Erancad6d022014-05-18 11:12:25 +0300762 if (ret) {
763 kfree(p);
764 return ret;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Matan Barak470be512015-12-23 14:56:49 +0200767 p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
768 if (!p->gid_attr_group) {
769 ret = -ENOMEM;
770 goto err_put;
771 }
772
773 p->gid_attr_group->port = p;
774 ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
775 &p->kobj, "gid_attrs");
776 if (ret) {
777 kfree(p->gid_attr_group);
778 goto err_put;
779 }
780
Hal Rosenstock6e2a51a2015-12-29 05:43:43 -0500781 p->pma_table = get_counter_table(device, port_num);
Christoph Lameter145d9c52015-12-21 08:20:29 -0600782 ret = sysfs_create_group(&p->kobj, p->pma_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (ret)
Matan Barak470be512015-12-23 14:56:49 +0200784 goto err_put_gid_attrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 p->gid_group.name = "gids";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500787 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800788 if (!p->gid_group.attrs) {
789 ret = -ENOMEM;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500790 goto err_remove_pma;
Wei Yongjun80b15042013-06-21 11:24:27 +0800791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 ret = sysfs_create_group(&p->kobj, &p->gid_group);
794 if (ret)
795 goto err_free_gid;
796
Matan Barak470be512015-12-23 14:56:49 +0200797 p->gid_attr_group->ndev.name = "ndevs";
798 p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
799 attr.gid_tbl_len);
800 if (!p->gid_attr_group->ndev.attrs) {
801 ret = -ENOMEM;
802 goto err_remove_gid;
803 }
804
805 ret = sysfs_create_group(&p->gid_attr_group->kobj,
806 &p->gid_attr_group->ndev);
807 if (ret)
808 goto err_free_gid_ndev;
809
810 p->gid_attr_group->type.name = "types";
811 p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
812 attr.gid_tbl_len);
813 if (!p->gid_attr_group->type.attrs) {
814 ret = -ENOMEM;
815 goto err_remove_gid_ndev;
816 }
817
818 ret = sysfs_create_group(&p->gid_attr_group->kobj,
819 &p->gid_attr_group->type);
820 if (ret)
821 goto err_free_gid_type;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 p->pkey_group.name = "pkeys";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500824 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
825 attr.pkey_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800826 if (!p->pkey_group.attrs) {
827 ret = -ENOMEM;
Matan Barak470be512015-12-23 14:56:49 +0200828 goto err_remove_gid_type;
Wei Yongjun80b15042013-06-21 11:24:27 +0800829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
832 if (ret)
833 goto err_free_pkey;
834
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700835 if (port_callback) {
836 ret = port_callback(device, port_num, &p->kobj);
837 if (ret)
838 goto err_remove_pkey;
839 }
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 list_add_tail(&p->kobj.entry, &device->port_list);
842
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400843 kobject_uevent(&p->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700846err_remove_pkey:
847 sysfs_remove_group(&p->kobj, &p->pkey_group);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849err_free_pkey:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500850 for (i = 0; i < attr.pkey_tbl_len; ++i)
851 kfree(p->pkey_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500853 kfree(p->pkey_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300854 p->pkey_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Matan Barak470be512015-12-23 14:56:49 +0200856err_remove_gid_type:
857 sysfs_remove_group(&p->gid_attr_group->kobj,
858 &p->gid_attr_group->type);
859
860err_free_gid_type:
861 for (i = 0; i < attr.gid_tbl_len; ++i)
862 kfree(p->gid_attr_group->type.attrs[i]);
863
864 kfree(p->gid_attr_group->type.attrs);
865 p->gid_attr_group->type.attrs = NULL;
866
867err_remove_gid_ndev:
868 sysfs_remove_group(&p->gid_attr_group->kobj,
869 &p->gid_attr_group->ndev);
870
871err_free_gid_ndev:
872 for (i = 0; i < attr.gid_tbl_len; ++i)
873 kfree(p->gid_attr_group->ndev.attrs[i]);
874
875 kfree(p->gid_attr_group->ndev.attrs);
876 p->gid_attr_group->ndev.attrs = NULL;
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878err_remove_gid:
879 sysfs_remove_group(&p->kobj, &p->gid_group);
880
881err_free_gid:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500882 for (i = 0; i < attr.gid_tbl_len; ++i)
883 kfree(p->gid_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500885 kfree(p->gid_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300886 p->gid_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888err_remove_pma:
Christoph Lameter145d9c52015-12-21 08:20:29 -0600889 sysfs_remove_group(&p->kobj, p->pma_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Matan Barak470be512015-12-23 14:56:49 +0200891err_put_gid_attrs:
892 kobject_put(&p->gid_attr_group->kobj);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894err_put:
Haggai Erancad6d022014-05-18 11:12:25 +0300895 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return ret;
897}
898
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100899static ssize_t show_node_type(struct device *device,
900 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100902 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 switch (dev->node_type) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500905 case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
906 case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
Upinder Malhi \(umalhi\)180771a2013-09-10 03:36:59 +0000907 case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
Upinder Malhi5db57652014-01-15 17:02:36 -0800908 case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500909 case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
910 case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
911 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913}
914
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100915static ssize_t show_sys_image_guid(struct device *device,
916 struct device_attribute *dev_attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100918 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Or Gerlitz86bee4c2015-12-18 10:59:45 +0200921 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
922 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
923 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
924 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100927static ssize_t show_node_guid(struct device *device,
928 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100930 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Sean Heftycf311cd2006-01-10 07:39:34 -0800933 be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
934 be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
935 be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
936 be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100939static ssize_t show_node_desc(struct device *device,
940 struct device_attribute *attr, char *buf)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800941{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100942 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800943
944 return sprintf(buf, "%.64s\n", dev->node_desc);
945}
946
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100947static ssize_t set_node_desc(struct device *device,
948 struct device_attribute *attr,
949 const char *buf, size_t count)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800950{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100951 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800952 struct ib_device_modify desc = {};
953 int ret;
954
955 if (!dev->modify_device)
956 return -EIO;
957
958 memcpy(desc.node_desc, buf, min_t(int, count, 64));
959 ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
960 if (ret)
961 return ret;
962
963 return count;
964}
965
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100966static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
967static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
968static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
969static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100971static struct device_attribute *ib_class_attributes[] = {
972 &dev_attr_node_type,
973 &dev_attr_sys_image_guid,
974 &dev_attr_node_guid,
975 &dev_attr_node_desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976};
977
Steve Wise7f624d02008-07-14 23:48:48 -0700978/* Show a given an attribute in the statistics group */
979static ssize_t show_protocol_stat(const struct device *device,
980 struct device_attribute *attr, char *buf,
981 unsigned offset)
982{
983 struct ib_device *dev = container_of(device, struct ib_device, dev);
984 union rdma_protocol_stats stats;
985 ssize_t ret;
986
987 ret = dev->get_protocol_stats(dev, &stats);
988 if (ret)
989 return ret;
990
991 return sprintf(buf, "%llu\n",
992 (unsigned long long) ((u64 *) &stats)[offset]);
993}
994
995/* generate a read-only iwarp statistics attribute */
996#define IW_STATS_ENTRY(name) \
997static ssize_t show_##name(struct device *device, \
998 struct device_attribute *attr, char *buf) \
999{ \
1000 return show_protocol_stat(device, attr, buf, \
1001 offsetof(struct iw_protocol_stats, name) / \
1002 sizeof (u64)); \
1003} \
1004static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
1005
1006IW_STATS_ENTRY(ipInReceives);
1007IW_STATS_ENTRY(ipInHdrErrors);
1008IW_STATS_ENTRY(ipInTooBigErrors);
1009IW_STATS_ENTRY(ipInNoRoutes);
1010IW_STATS_ENTRY(ipInAddrErrors);
1011IW_STATS_ENTRY(ipInUnknownProtos);
1012IW_STATS_ENTRY(ipInTruncatedPkts);
1013IW_STATS_ENTRY(ipInDiscards);
1014IW_STATS_ENTRY(ipInDelivers);
1015IW_STATS_ENTRY(ipOutForwDatagrams);
1016IW_STATS_ENTRY(ipOutRequests);
1017IW_STATS_ENTRY(ipOutDiscards);
1018IW_STATS_ENTRY(ipOutNoRoutes);
1019IW_STATS_ENTRY(ipReasmTimeout);
1020IW_STATS_ENTRY(ipReasmReqds);
1021IW_STATS_ENTRY(ipReasmOKs);
1022IW_STATS_ENTRY(ipReasmFails);
1023IW_STATS_ENTRY(ipFragOKs);
1024IW_STATS_ENTRY(ipFragFails);
1025IW_STATS_ENTRY(ipFragCreates);
1026IW_STATS_ENTRY(ipInMcastPkts);
1027IW_STATS_ENTRY(ipOutMcastPkts);
1028IW_STATS_ENTRY(ipInBcastPkts);
1029IW_STATS_ENTRY(ipOutBcastPkts);
1030IW_STATS_ENTRY(tcpRtoAlgorithm);
1031IW_STATS_ENTRY(tcpRtoMin);
1032IW_STATS_ENTRY(tcpRtoMax);
1033IW_STATS_ENTRY(tcpMaxConn);
1034IW_STATS_ENTRY(tcpActiveOpens);
1035IW_STATS_ENTRY(tcpPassiveOpens);
1036IW_STATS_ENTRY(tcpAttemptFails);
1037IW_STATS_ENTRY(tcpEstabResets);
1038IW_STATS_ENTRY(tcpCurrEstab);
1039IW_STATS_ENTRY(tcpInSegs);
1040IW_STATS_ENTRY(tcpOutSegs);
1041IW_STATS_ENTRY(tcpRetransSegs);
1042IW_STATS_ENTRY(tcpInErrs);
1043IW_STATS_ENTRY(tcpOutRsts);
1044
1045static struct attribute *iw_proto_stats_attrs[] = {
1046 &dev_attr_ipInReceives.attr,
1047 &dev_attr_ipInHdrErrors.attr,
1048 &dev_attr_ipInTooBigErrors.attr,
1049 &dev_attr_ipInNoRoutes.attr,
1050 &dev_attr_ipInAddrErrors.attr,
1051 &dev_attr_ipInUnknownProtos.attr,
1052 &dev_attr_ipInTruncatedPkts.attr,
1053 &dev_attr_ipInDiscards.attr,
1054 &dev_attr_ipInDelivers.attr,
1055 &dev_attr_ipOutForwDatagrams.attr,
1056 &dev_attr_ipOutRequests.attr,
1057 &dev_attr_ipOutDiscards.attr,
1058 &dev_attr_ipOutNoRoutes.attr,
1059 &dev_attr_ipReasmTimeout.attr,
1060 &dev_attr_ipReasmReqds.attr,
1061 &dev_attr_ipReasmOKs.attr,
1062 &dev_attr_ipReasmFails.attr,
1063 &dev_attr_ipFragOKs.attr,
1064 &dev_attr_ipFragFails.attr,
1065 &dev_attr_ipFragCreates.attr,
1066 &dev_attr_ipInMcastPkts.attr,
1067 &dev_attr_ipOutMcastPkts.attr,
1068 &dev_attr_ipInBcastPkts.attr,
1069 &dev_attr_ipOutBcastPkts.attr,
1070 &dev_attr_tcpRtoAlgorithm.attr,
1071 &dev_attr_tcpRtoMin.attr,
1072 &dev_attr_tcpRtoMax.attr,
1073 &dev_attr_tcpMaxConn.attr,
1074 &dev_attr_tcpActiveOpens.attr,
1075 &dev_attr_tcpPassiveOpens.attr,
1076 &dev_attr_tcpAttemptFails.attr,
1077 &dev_attr_tcpEstabResets.attr,
1078 &dev_attr_tcpCurrEstab.attr,
1079 &dev_attr_tcpInSegs.attr,
1080 &dev_attr_tcpOutSegs.attr,
1081 &dev_attr_tcpRetransSegs.attr,
1082 &dev_attr_tcpInErrs.attr,
1083 &dev_attr_tcpOutRsts.attr,
1084 NULL
1085};
1086
1087static struct attribute_group iw_stats_group = {
1088 .name = "proto_stats",
1089 .attrs = iw_proto_stats_attrs,
1090};
1091
Haggai Eran584482a2014-05-18 11:12:26 +03001092static void free_port_list_attributes(struct ib_device *device)
1093{
1094 struct kobject *p, *t;
1095
1096 list_for_each_entry_safe(p, t, &device->port_list, entry) {
1097 struct ib_port *port = container_of(p, struct ib_port, kobj);
1098 list_del(&p->entry);
Christoph Lameter145d9c52015-12-21 08:20:29 -06001099 sysfs_remove_group(p, port->pma_table);
Haggai Eran584482a2014-05-18 11:12:26 +03001100 sysfs_remove_group(p, &port->pkey_group);
1101 sysfs_remove_group(p, &port->gid_group);
Matan Barak470be512015-12-23 14:56:49 +02001102 sysfs_remove_group(&port->gid_attr_group->kobj,
1103 &port->gid_attr_group->ndev);
1104 sysfs_remove_group(&port->gid_attr_group->kobj,
1105 &port->gid_attr_group->type);
1106 kobject_put(&port->gid_attr_group->kobj);
Haggai Eran584482a2014-05-18 11:12:26 +03001107 kobject_put(p);
1108 }
1109
1110 kobject_put(device->ports_parent);
1111}
1112
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001113int ib_device_register_sysfs(struct ib_device *device,
1114 int (*port_callback)(struct ib_device *,
1115 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001117 struct device *class_dev = &device->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int ret;
1119 int i;
1120
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001121 device->dev.parent = device->dma_device;
1122 ret = dev_set_name(class_dev, "%s", device->name);
1123 if (ret)
1124 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Jason Gunthorpe55aeed02015-08-04 15:23:34 -06001126 ret = device_add(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (ret)
1128 goto err;
1129
1130 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001131 ret = device_create_file(class_dev, ib_class_attributes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (ret)
1133 goto err_unregister;
1134 }
1135
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -04001136 device->ports_parent = kobject_create_and_add("ports",
Haggai Eran373c0ea2014-05-18 11:12:24 +03001137 &class_dev->kobj);
Li Zefanc7482b82008-02-15 10:24:49 +08001138 if (!device->ports_parent) {
1139 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 goto err_put;
Li Zefanc7482b82008-02-15 10:24:49 +08001141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Hal Rosenstock41390322015-06-29 09:57:00 -04001143 if (rdma_cap_ib_switch(device)) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001144 ret = add_port(device, 0, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (ret)
1146 goto err_put;
1147 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 for (i = 1; i <= device->phys_port_cnt; ++i) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -07001149 ret = add_port(device, i, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (ret)
1151 goto err_put;
1152 }
1153 }
1154
Steve Wise7f624d02008-07-14 23:48:48 -07001155 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
1156 ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
1157 if (ret)
1158 goto err_put;
1159 }
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return 0;
1162
1163err_put:
Haggai Eran584482a2014-05-18 11:12:26 +03001164 free_port_list_attributes(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166err_unregister:
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001167 device_unregister(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169err:
1170 return ret;
1171}
1172
1173void ib_device_unregister_sysfs(struct ib_device *device)
1174{
Roland Dreier9206dff2009-02-25 13:27:46 -08001175 /* Hold kobject until ib_dealloc_device() */
Haggai Eran584482a2014-05-18 11:12:26 +03001176 struct kobject *kobj_dev = kobject_get(&device->dev.kobj);
1177 int i;
Roland Dreier9206dff2009-02-25 13:27:46 -08001178
Haggai Eran584482a2014-05-18 11:12:26 +03001179 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats)
1180 sysfs_remove_group(kobj_dev, &iw_stats_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Haggai Eran584482a2014-05-18 11:12:26 +03001182 free_port_list_attributes(device);
1183
1184 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
1185 device_remove_file(&device->dev, ib_class_attributes[i]);
1186
Tony Jonesf4e91eb2008-02-22 00:13:36 +01001187 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}