Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved. |
| 4 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software is available to you under a choice of one of two |
| 7 | * licenses. You may choose to be licensed under the terms of the GNU |
| 8 | * General Public License (GPL) Version 2, available from the file |
| 9 | * COPYING in the main directory of this source tree, or the |
| 10 | * OpenIB.org BSD license below: |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or |
| 13 | * without modification, are permitted provided that the following |
| 14 | * conditions are met: |
| 15 | * |
| 16 | * - Redistributions of source code must retain the above |
| 17 | * copyright notice, this list of conditions and the following |
| 18 | * disclaimer. |
| 19 | * |
| 20 | * - Redistributions in binary form must reproduce the above |
| 21 | * copyright notice, this list of conditions and the following |
| 22 | * disclaimer in the documentation and/or other materials |
| 23 | * provided with the distribution. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 29 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 30 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 31 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | * SOFTWARE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include "core_priv.h" |
| 36 | |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 37 | #include <linux/slab.h> |
Paul Gortmaker | fc87af7 | 2011-05-27 13:27:45 -0400 | [diff] [blame] | 38 | #include <linux/stat.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 39 | #include <linux/string.h> |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 40 | #include <linux/netdevice.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 41 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 42 | #include <rdma/ib_mad.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 44 | struct ib_port; |
| 45 | |
| 46 | struct gid_attr_group { |
| 47 | struct ib_port *port; |
| 48 | struct kobject kobj; |
| 49 | struct attribute_group ndev; |
| 50 | struct attribute_group type; |
| 51 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | struct ib_port { |
| 53 | struct kobject kobj; |
| 54 | struct ib_device *ibdev; |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 55 | struct gid_attr_group *gid_attr_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct attribute_group gid_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | struct attribute_group pkey_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | u8 port_num; |
| 59 | }; |
| 60 | |
| 61 | struct 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) \ |
| 69 | struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store) |
| 70 | |
| 71 | #define PORT_ATTR_RO(_name) \ |
| 72 | struct port_attribute port_attr_##_name = __ATTR_RO(_name) |
| 73 | |
| 74 | struct port_table_attribute { |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 75 | struct port_attribute attr; |
| 76 | char name[8]; |
| 77 | int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static 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 Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 88 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | return port_attr->show(p, port_attr, buf); |
| 91 | } |
| 92 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 93 | static const struct sysfs_ops port_sysfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | .show = port_attr_show |
| 95 | }; |
| 96 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 97 | static 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 | |
| 111 | static const struct sysfs_ops gid_attr_sysfs_ops = { |
| 112 | .show = gid_attr_show |
| 113 | }; |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | static 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 Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 135 | attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | state_name[attr.state] : "UNKNOWN"); |
| 137 | } |
| 138 | |
| 139 | static 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 | |
| 152 | static 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 | |
| 166 | static 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 | |
| 179 | static 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 | |
| 192 | static 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 | |
| 205 | static 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 Dreier | 0559d8d | 2012-04-02 10:57:31 -0700 | [diff] [blame] | 210 | int rate; /* in deci-Gb/sec */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | 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 Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 218 | case IB_SPEED_DDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 219 | speed = " DDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 220 | rate = 50; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 221 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 222 | case IB_SPEED_QDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 223 | speed = " QDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 224 | rate = 100; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 225 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 226 | case IB_SPEED_FDR10: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 227 | speed = " FDR10"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 228 | rate = 100; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 229 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 230 | case IB_SPEED_FDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 231 | speed = " FDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 232 | rate = 140; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 233 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 234 | case IB_SPEED_EDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 235 | speed = " EDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 236 | rate = 250; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 237 | break; |
Roland Dreier | 0559d8d | 2012-04-02 10:57:31 -0700 | [diff] [blame] | 238 | case IB_SPEED_SDR: |
| 239 | default: /* default to SDR for invalid rates */ |
| 240 | rate = 25; |
| 241 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 244 | rate *= ib_width_enum_to_int(attr.active_width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (rate < 0) |
| 246 | return -EINVAL; |
| 247 | |
| 248 | return sprintf(buf, "%d%s Gb/sec (%dX%s)\n", |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 249 | rate / 10, rate % 10 ? ".5" : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | ib_width_enum_to_int(attr.active_width), speed); |
| 251 | } |
| 252 | |
| 253 | static 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 Cohen | 8ad330a | 2010-10-22 14:32:05 +0200 | [diff] [blame] | 276 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | static PORT_ATTR_RO(state); |
| 290 | static PORT_ATTR_RO(lid); |
| 291 | static PORT_ATTR_RO(lid_mask_count); |
| 292 | static PORT_ATTR_RO(sm_lid); |
| 293 | static PORT_ATTR_RO(sm_sl); |
| 294 | static PORT_ATTR_RO(cap_mask); |
| 295 | static PORT_ATTR_RO(rate); |
| 296 | static PORT_ATTR_RO(phys_state); |
Eli Cohen | 8ad330a | 2010-10-22 14:32:05 +0200 | [diff] [blame] | 297 | static PORT_ATTR_RO(link_layer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | static 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 Cohen | 8ad330a | 2010-10-22 14:32:05 +0200 | [diff] [blame] | 308 | &port_attr_link_layer.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | NULL |
| 310 | }; |
| 311 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 312 | static 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 | |
| 320 | static 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 | |
| 325 | static 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 | |
| 345 | err: |
| 346 | if (gid_attr.ndev) |
| 347 | dev_put(gid_attr.ndev); |
| 348 | va_end(args); |
| 349 | return ret; |
| 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | static 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 Barak | 55ee3ab | 2015-10-15 18:38:45 +0300 | [diff] [blame] | 360 | ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | if (ret) |
| 362 | return ret; |
| 363 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 364 | return sprintf(buf, "%pI6\n", gid.raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 367 | static 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 | |
| 373 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | static 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) \ |
| 396 | struct 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 Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 401 | /* |
| 402 | * Get a Perfmgmt MAD block of data. |
| 403 | * Returns error code or the number of bytes retrieved. |
| 404 | */ |
| 405 | static int get_perf_mad(struct ib_device *dev, int port_num, int attr, |
| 406 | void *data, int offset, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 408 | struct ib_mad *in_mad; |
| 409 | struct ib_mad *out_mad; |
Ira Weiny | 4cd7c94 | 2015-06-06 14:38:31 -0400 | [diff] [blame] | 410 | size_t mad_size = sizeof(*out_mad); |
| 411 | u16 out_mad_pkey_index = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | ssize_t ret; |
| 413 | |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 414 | if (!dev->process_mad) |
| 415 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 417 | in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); |
Dotan Barak | 856c52a | 2007-07-10 16:55:57 +0300 | [diff] [blame] | 418 | out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | if (!in_mad || !out_mad) { |
| 420 | ret = -ENOMEM; |
| 421 | goto out; |
| 422 | } |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | 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 Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 428 | in_mad->mad_hdr.attr_id = attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 430 | in_mad->data[41] = port_num; /* PortSelect field */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 432 | if ((dev->process_mad(dev, IB_MAD_IGNORE_MKEY, |
| 433 | port_num, NULL, NULL, |
Ira Weiny | 4cd7c94 | 2015-06-06 14:38:31 -0400 | [diff] [blame] | 434 | (const struct ib_mad_hdr *)in_mad, mad_size, |
| 435 | (struct ib_mad_hdr *)out_mad, &mad_size, |
| 436 | &out_mad_pkey_index) & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | (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 Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 442 | memcpy(data, out_mad->data + offset, size); |
| 443 | ret = size; |
| 444 | out: |
| 445 | kfree(in_mad); |
| 446 | kfree(out_mad); |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | switch (width) { |
| 466 | case 4: |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 467 | ret = sprintf(buf, "%u\n", (*data >> |
Ralph Campbell | d8b9f23 | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 468 | (4 - (offset % 8))) & 0xf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | break; |
| 470 | case 8: |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 471 | ret = sprintf(buf, "%u\n", *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | break; |
| 473 | case 16: |
| 474 | ret = sprintf(buf, "%u\n", |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 475 | be16_to_cpup((__be16 *)data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | break; |
| 477 | case 32: |
| 478 | ret = sprintf(buf, "%u\n", |
Christoph Lameter | 35c4cbb | 2015-12-21 08:20:27 -0600 | [diff] [blame^] | 479 | be32_to_cpup((__be32 *)data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | break; |
| 481 | default: |
| 482 | ret = 0; |
| 483 | } |
| 484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | static PORT_PMA_ATTR(symbol_error , 0, 16, 32); |
| 489 | static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48); |
| 490 | static PORT_PMA_ATTR(link_downed , 2, 8, 56); |
| 491 | static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64); |
| 492 | static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80); |
| 493 | static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96); |
| 494 | static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112); |
| 495 | static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128); |
| 496 | static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136); |
| 497 | static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152); |
| 498 | static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156); |
| 499 | static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176); |
| 500 | static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192); |
| 501 | static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224); |
| 502 | static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256); |
| 503 | static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288); |
| 504 | |
| 505 | static 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 | |
| 525 | static struct attribute_group pma_group = { |
| 526 | .name = "counters", |
| 527 | .attrs = pma_attrs |
| 528 | }; |
| 529 | |
| 530 | static 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 Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 536 | if (p->gid_group.attrs) { |
| 537 | for (i = 0; (a = p->gid_group.attrs[i]); ++i) |
| 538 | kfree(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 540 | kfree(p->gid_group.attrs); |
| 541 | } |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 542 | |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 543 | if (p->pkey_group.attrs) { |
| 544 | for (i = 0; (a = p->pkey_group.attrs[i]); ++i) |
| 545 | kfree(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 547 | kfree(p->pkey_group.attrs); |
| 548 | } |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | kfree(p); |
| 551 | } |
| 552 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 553 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | static 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 Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 583 | static struct kobj_type gid_attr_type = { |
| 584 | .sysfs_ops = &gid_attr_sysfs_ops, |
| 585 | .release = ib_port_gid_attr_release |
| 586 | }; |
| 587 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 588 | static struct attribute ** |
| 589 | alloc_group_attrs(ssize_t (*show)(struct ib_port *, |
| 590 | struct port_attribute *, char *buf), |
| 591 | int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 593 | struct attribute **tab_attr; |
| 594 | struct port_table_attribute *element; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 597 | tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL); |
| 598 | if (!tab_attr) |
| 599 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 601 | for (i = 0; i < len; i++) { |
Pekka Enberg | 82ca76b | 2005-09-06 15:18:35 -0700 | [diff] [blame] | 602 | element = kzalloc(sizeof(struct port_table_attribute), |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 603 | GFP_KERNEL); |
| 604 | if (!element) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 607 | if (snprintf(element->name, sizeof(element->name), |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 608 | "%d", i) >= sizeof(element->name)) { |
| 609 | kfree(element); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | goto err; |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 613 | element->attr.attr.name = element->name; |
| 614 | element->attr.attr.mode = S_IRUGO; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 615 | element->attr.show = show; |
| 616 | element->index = i; |
Greg Kroah-Hartman | 21e3bde | 2010-03-15 14:01:25 -0700 | [diff] [blame] | 617 | sysfs_attr_init(&element->attr.attr); |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 618 | |
| 619 | tab_attr[i] = &element->attr.attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 622 | return tab_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | err: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 625 | while (--i >= 0) |
| 626 | kfree(tab_attr[i]); |
| 627 | kfree(tab_attr); |
| 628 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 631 | static int add_port(struct ib_device *device, int port_num, |
| 632 | int (*port_callback)(struct ib_device *, |
| 633 | u8, struct kobject *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
| 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 Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 644 | p = kzalloc(sizeof *p, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (!p) |
| 646 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | p->ibdev = device; |
| 649 | p->port_num = port_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 651 | ret = kobject_init_and_add(&p->kobj, &port_type, |
Haggai Eran | 373c0ea | 2014-05-18 11:12:24 +0300 | [diff] [blame] | 652 | device->ports_parent, |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 653 | "%d", port_num); |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 654 | if (ret) { |
| 655 | kfree(p); |
| 656 | return ret; |
| 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 659 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | ret = sysfs_create_group(&p->kobj, &pma_group); |
| 674 | if (ret) |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 675 | goto err_put_gid_attrs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | p->gid_group.name = "gids"; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 678 | p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len); |
Wei Yongjun | 80b1504 | 2013-06-21 11:24:27 +0800 | [diff] [blame] | 679 | if (!p->gid_group.attrs) { |
| 680 | ret = -ENOMEM; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 681 | goto err_remove_pma; |
Wei Yongjun | 80b1504 | 2013-06-21 11:24:27 +0800 | [diff] [blame] | 682 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
| 684 | ret = sysfs_create_group(&p->kobj, &p->gid_group); |
| 685 | if (ret) |
| 686 | goto err_free_gid; |
| 687 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 688 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | p->pkey_group.name = "pkeys"; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 715 | p->pkey_group.attrs = alloc_group_attrs(show_port_pkey, |
| 716 | attr.pkey_tbl_len); |
Wei Yongjun | 80b1504 | 2013-06-21 11:24:27 +0800 | [diff] [blame] | 717 | if (!p->pkey_group.attrs) { |
| 718 | ret = -ENOMEM; |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 719 | goto err_remove_gid_type; |
Wei Yongjun | 80b1504 | 2013-06-21 11:24:27 +0800 | [diff] [blame] | 720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
| 722 | ret = sysfs_create_group(&p->kobj, &p->pkey_group); |
| 723 | if (ret) |
| 724 | goto err_free_pkey; |
| 725 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 726 | if (port_callback) { |
| 727 | ret = port_callback(device, port_num, &p->kobj); |
| 728 | if (ret) |
| 729 | goto err_remove_pkey; |
| 730 | } |
| 731 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | list_add_tail(&p->kobj.entry, &device->port_list); |
| 733 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 734 | kobject_uevent(&p->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 737 | err_remove_pkey: |
| 738 | sysfs_remove_group(&p->kobj, &p->pkey_group); |
| 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | err_free_pkey: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 741 | for (i = 0; i < attr.pkey_tbl_len; ++i) |
| 742 | kfree(p->pkey_group.attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 744 | kfree(p->pkey_group.attrs); |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 745 | p->pkey_group.attrs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 747 | err_remove_gid_type: |
| 748 | sysfs_remove_group(&p->gid_attr_group->kobj, |
| 749 | &p->gid_attr_group->type); |
| 750 | |
| 751 | err_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 | |
| 758 | err_remove_gid_ndev: |
| 759 | sysfs_remove_group(&p->gid_attr_group->kobj, |
| 760 | &p->gid_attr_group->ndev); |
| 761 | |
| 762 | err_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | err_remove_gid: |
| 770 | sysfs_remove_group(&p->kobj, &p->gid_group); |
| 771 | |
| 772 | err_free_gid: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 773 | for (i = 0; i < attr.gid_tbl_len; ++i) |
| 774 | kfree(p->gid_group.attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 776 | kfree(p->gid_group.attrs); |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 777 | p->gid_group.attrs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | err_remove_pma: |
| 780 | sysfs_remove_group(&p->kobj, &pma_group); |
| 781 | |
Matan Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 782 | err_put_gid_attrs: |
| 783 | kobject_put(&p->gid_attr_group->kobj); |
| 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | err_put: |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 786 | kobject_put(&p->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return ret; |
| 788 | } |
| 789 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 790 | static ssize_t show_node_type(struct device *device, |
| 791 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 793 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
| 795 | switch (dev->node_type) { |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 796 | 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\) | 180771a | 2013-09-10 03:36:59 +0000 | [diff] [blame] | 798 | case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type); |
Upinder Malhi | 5db5765 | 2014-01-15 17:02:36 -0800 | [diff] [blame] | 799 | case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 800 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| 805 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 806 | static ssize_t show_sys_image_guid(struct device *device, |
| 807 | struct device_attribute *dev_attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 809 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
| 811 | return sprintf(buf, "%04x:%04x:%04x:%04x\n", |
Or Gerlitz | 86bee4c | 2015-12-18 10:59:45 +0200 | [diff] [blame] | 812 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 818 | static ssize_t show_node_guid(struct device *device, |
| 819 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 821 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | return sprintf(buf, "%04x:%04x:%04x:%04x\n", |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 824 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 830 | static ssize_t show_node_desc(struct device *device, |
| 831 | struct device_attribute *attr, char *buf) |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 832 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 833 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 834 | |
| 835 | return sprintf(buf, "%.64s\n", dev->node_desc); |
| 836 | } |
| 837 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 838 | static ssize_t set_node_desc(struct device *device, |
| 839 | struct device_attribute *attr, |
| 840 | const char *buf, size_t count) |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 841 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 842 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 843 | 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 Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 857 | static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL); |
| 858 | static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL); |
| 859 | static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL); |
| 860 | static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 862 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | }; |
| 868 | |
Steve Wise | 7f624d0 | 2008-07-14 23:48:48 -0700 | [diff] [blame] | 869 | /* Show a given an attribute in the statistics group */ |
| 870 | static 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) \ |
| 888 | static 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 | } \ |
| 895 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 896 | |
| 897 | IW_STATS_ENTRY(ipInReceives); |
| 898 | IW_STATS_ENTRY(ipInHdrErrors); |
| 899 | IW_STATS_ENTRY(ipInTooBigErrors); |
| 900 | IW_STATS_ENTRY(ipInNoRoutes); |
| 901 | IW_STATS_ENTRY(ipInAddrErrors); |
| 902 | IW_STATS_ENTRY(ipInUnknownProtos); |
| 903 | IW_STATS_ENTRY(ipInTruncatedPkts); |
| 904 | IW_STATS_ENTRY(ipInDiscards); |
| 905 | IW_STATS_ENTRY(ipInDelivers); |
| 906 | IW_STATS_ENTRY(ipOutForwDatagrams); |
| 907 | IW_STATS_ENTRY(ipOutRequests); |
| 908 | IW_STATS_ENTRY(ipOutDiscards); |
| 909 | IW_STATS_ENTRY(ipOutNoRoutes); |
| 910 | IW_STATS_ENTRY(ipReasmTimeout); |
| 911 | IW_STATS_ENTRY(ipReasmReqds); |
| 912 | IW_STATS_ENTRY(ipReasmOKs); |
| 913 | IW_STATS_ENTRY(ipReasmFails); |
| 914 | IW_STATS_ENTRY(ipFragOKs); |
| 915 | IW_STATS_ENTRY(ipFragFails); |
| 916 | IW_STATS_ENTRY(ipFragCreates); |
| 917 | IW_STATS_ENTRY(ipInMcastPkts); |
| 918 | IW_STATS_ENTRY(ipOutMcastPkts); |
| 919 | IW_STATS_ENTRY(ipInBcastPkts); |
| 920 | IW_STATS_ENTRY(ipOutBcastPkts); |
| 921 | IW_STATS_ENTRY(tcpRtoAlgorithm); |
| 922 | IW_STATS_ENTRY(tcpRtoMin); |
| 923 | IW_STATS_ENTRY(tcpRtoMax); |
| 924 | IW_STATS_ENTRY(tcpMaxConn); |
| 925 | IW_STATS_ENTRY(tcpActiveOpens); |
| 926 | IW_STATS_ENTRY(tcpPassiveOpens); |
| 927 | IW_STATS_ENTRY(tcpAttemptFails); |
| 928 | IW_STATS_ENTRY(tcpEstabResets); |
| 929 | IW_STATS_ENTRY(tcpCurrEstab); |
| 930 | IW_STATS_ENTRY(tcpInSegs); |
| 931 | IW_STATS_ENTRY(tcpOutSegs); |
| 932 | IW_STATS_ENTRY(tcpRetransSegs); |
| 933 | IW_STATS_ENTRY(tcpInErrs); |
| 934 | IW_STATS_ENTRY(tcpOutRsts); |
| 935 | |
| 936 | static 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 | |
| 978 | static struct attribute_group iw_stats_group = { |
| 979 | .name = "proto_stats", |
| 980 | .attrs = iw_proto_stats_attrs, |
| 981 | }; |
| 982 | |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 983 | static 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 Barak | 470be51 | 2015-12-23 14:56:49 +0200 | [diff] [blame] | 993 | 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 Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 998 | kobject_put(p); |
| 999 | } |
| 1000 | |
| 1001 | kobject_put(device->ports_parent); |
| 1002 | } |
| 1003 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 1004 | int ib_device_register_sysfs(struct ib_device *device, |
| 1005 | int (*port_callback)(struct ib_device *, |
| 1006 | u8, struct kobject *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1008 | struct device *class_dev = &device->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | int ret; |
| 1010 | int i; |
| 1011 | |
Jason Gunthorpe | 55aeed0 | 2015-08-04 15:23:34 -0600 | [diff] [blame] | 1012 | device->dev.parent = device->dma_device; |
| 1013 | ret = dev_set_name(class_dev, "%s", device->name); |
| 1014 | if (ret) |
| 1015 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
Jason Gunthorpe | 55aeed0 | 2015-08-04 15:23:34 -0600 | [diff] [blame] | 1017 | ret = device_add(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | if (ret) |
| 1019 | goto err; |
| 1020 | |
| 1021 | for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1022 | ret = device_create_file(class_dev, ib_class_attributes[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | if (ret) |
| 1024 | goto err_unregister; |
| 1025 | } |
| 1026 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 1027 | device->ports_parent = kobject_create_and_add("ports", |
Haggai Eran | 373c0ea | 2014-05-18 11:12:24 +0300 | [diff] [blame] | 1028 | &class_dev->kobj); |
Li Zefan | c7482b8 | 2008-02-15 10:24:49 +0800 | [diff] [blame] | 1029 | if (!device->ports_parent) { |
| 1030 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | goto err_put; |
Li Zefan | c7482b8 | 2008-02-15 10:24:49 +0800 | [diff] [blame] | 1032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Hal Rosenstock | 4139032 | 2015-06-29 09:57:00 -0400 | [diff] [blame] | 1034 | if (rdma_cap_ib_switch(device)) { |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 1035 | ret = add_port(device, 0, port_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (ret) |
| 1037 | goto err_put; |
| 1038 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | for (i = 1; i <= device->phys_port_cnt; ++i) { |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 1040 | ret = add_port(device, i, port_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | if (ret) |
| 1042 | goto err_put; |
| 1043 | } |
| 1044 | } |
| 1045 | |
Steve Wise | 7f624d0 | 2008-07-14 23:48:48 -0700 | [diff] [blame] | 1046 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | |
| 1054 | err_put: |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 1055 | free_port_list_attributes(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | err_unregister: |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1058 | device_unregister(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
| 1060 | err: |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | void ib_device_unregister_sysfs(struct ib_device *device) |
| 1065 | { |
Roland Dreier | 9206dff | 2009-02-25 13:27:46 -0800 | [diff] [blame] | 1066 | /* Hold kobject until ib_dealloc_device() */ |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 1067 | struct kobject *kobj_dev = kobject_get(&device->dev.kobj); |
| 1068 | int i; |
Roland Dreier | 9206dff | 2009-02-25 13:27:46 -0800 | [diff] [blame] | 1069 | |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 1070 | if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) |
| 1071 | sysfs_remove_group(kobj_dev, &iw_stats_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 1073 | 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 Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 1078 | device_unregister(&device->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |