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> |
| 40 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 41 | #include <rdma/ib_mad.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | struct ib_port { |
| 44 | struct kobject kobj; |
| 45 | struct ib_device *ibdev; |
| 46 | struct attribute_group gid_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct attribute_group pkey_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | u8 port_num; |
| 49 | }; |
| 50 | |
| 51 | struct port_attribute { |
| 52 | struct attribute attr; |
| 53 | ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf); |
| 54 | ssize_t (*store)(struct ib_port *, struct port_attribute *, |
| 55 | const char *buf, size_t count); |
| 56 | }; |
| 57 | |
| 58 | #define PORT_ATTR(_name, _mode, _show, _store) \ |
| 59 | struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store) |
| 60 | |
| 61 | #define PORT_ATTR_RO(_name) \ |
| 62 | struct port_attribute port_attr_##_name = __ATTR_RO(_name) |
| 63 | |
| 64 | struct port_table_attribute { |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 65 | struct port_attribute attr; |
| 66 | char name[8]; |
| 67 | int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static ssize_t port_attr_show(struct kobject *kobj, |
| 71 | struct attribute *attr, char *buf) |
| 72 | { |
| 73 | struct port_attribute *port_attr = |
| 74 | container_of(attr, struct port_attribute, attr); |
| 75 | struct ib_port *p = container_of(kobj, struct ib_port, kobj); |
| 76 | |
| 77 | if (!port_attr->show) |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 78 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | return port_attr->show(p, port_attr, buf); |
| 81 | } |
| 82 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 83 | static const struct sysfs_ops port_sysfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | .show = port_attr_show |
| 85 | }; |
| 86 | |
| 87 | static ssize_t state_show(struct ib_port *p, struct port_attribute *unused, |
| 88 | char *buf) |
| 89 | { |
| 90 | struct ib_port_attr attr; |
| 91 | ssize_t ret; |
| 92 | |
| 93 | static const char *state_name[] = { |
| 94 | [IB_PORT_NOP] = "NOP", |
| 95 | [IB_PORT_DOWN] = "DOWN", |
| 96 | [IB_PORT_INIT] = "INIT", |
| 97 | [IB_PORT_ARMED] = "ARMED", |
| 98 | [IB_PORT_ACTIVE] = "ACTIVE", |
| 99 | [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER" |
| 100 | }; |
| 101 | |
| 102 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
| 106 | return sprintf(buf, "%d: %s\n", attr.state, |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 107 | attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | state_name[attr.state] : "UNKNOWN"); |
| 109 | } |
| 110 | |
| 111 | static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused, |
| 112 | char *buf) |
| 113 | { |
| 114 | struct ib_port_attr attr; |
| 115 | ssize_t ret; |
| 116 | |
| 117 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 118 | if (ret) |
| 119 | return ret; |
| 120 | |
| 121 | return sprintf(buf, "0x%x\n", attr.lid); |
| 122 | } |
| 123 | |
| 124 | static ssize_t lid_mask_count_show(struct ib_port *p, |
| 125 | struct port_attribute *unused, |
| 126 | char *buf) |
| 127 | { |
| 128 | struct ib_port_attr attr; |
| 129 | ssize_t ret; |
| 130 | |
| 131 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 132 | if (ret) |
| 133 | return ret; |
| 134 | |
| 135 | return sprintf(buf, "%d\n", attr.lmc); |
| 136 | } |
| 137 | |
| 138 | static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused, |
| 139 | char *buf) |
| 140 | { |
| 141 | struct ib_port_attr attr; |
| 142 | ssize_t ret; |
| 143 | |
| 144 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | return sprintf(buf, "0x%x\n", attr.sm_lid); |
| 149 | } |
| 150 | |
| 151 | static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused, |
| 152 | char *buf) |
| 153 | { |
| 154 | struct ib_port_attr attr; |
| 155 | ssize_t ret; |
| 156 | |
| 157 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | |
| 161 | return sprintf(buf, "%d\n", attr.sm_sl); |
| 162 | } |
| 163 | |
| 164 | static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused, |
| 165 | char *buf) |
| 166 | { |
| 167 | struct ib_port_attr attr; |
| 168 | ssize_t ret; |
| 169 | |
| 170 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 171 | if (ret) |
| 172 | return ret; |
| 173 | |
| 174 | return sprintf(buf, "0x%08x\n", attr.port_cap_flags); |
| 175 | } |
| 176 | |
| 177 | static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused, |
| 178 | char *buf) |
| 179 | { |
| 180 | struct ib_port_attr attr; |
| 181 | char *speed = ""; |
Roland Dreier | 0559d8d | 2012-04-02 10:57:31 -0700 | [diff] [blame] | 182 | int rate; /* in deci-Gb/sec */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | ssize_t ret; |
| 184 | |
| 185 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | switch (attr.active_speed) { |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 190 | case IB_SPEED_DDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 191 | speed = " DDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 192 | rate = 50; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 193 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 194 | case IB_SPEED_QDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 195 | speed = " QDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 196 | rate = 100; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 197 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 198 | case IB_SPEED_FDR10: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 199 | speed = " FDR10"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 200 | rate = 100; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 201 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 202 | case IB_SPEED_FDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 203 | speed = " FDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 204 | rate = 140; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 205 | break; |
Or Gerlitz | 2e96691 | 2012-02-28 18:49:50 +0200 | [diff] [blame] | 206 | case IB_SPEED_EDR: |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 207 | speed = " EDR"; |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 208 | rate = 250; |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 209 | break; |
Roland Dreier | 0559d8d | 2012-04-02 10:57:31 -0700 | [diff] [blame] | 210 | case IB_SPEED_SDR: |
| 211 | default: /* default to SDR for invalid rates */ |
| 212 | rate = 25; |
| 213 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 216 | rate *= ib_width_enum_to_int(attr.active_width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (rate < 0) |
| 218 | return -EINVAL; |
| 219 | |
| 220 | return sprintf(buf, "%d%s Gb/sec (%dX%s)\n", |
Roland Dreier | e9319b0 | 2012-02-27 09:15:08 -0800 | [diff] [blame] | 221 | rate / 10, rate % 10 ? ".5" : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | ib_width_enum_to_int(attr.active_width), speed); |
| 223 | } |
| 224 | |
| 225 | static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused, |
| 226 | char *buf) |
| 227 | { |
| 228 | struct ib_port_attr attr; |
| 229 | |
| 230 | ssize_t ret; |
| 231 | |
| 232 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 233 | if (ret) |
| 234 | return ret; |
| 235 | |
| 236 | switch (attr.phys_state) { |
| 237 | case 1: return sprintf(buf, "1: Sleep\n"); |
| 238 | case 2: return sprintf(buf, "2: Polling\n"); |
| 239 | case 3: return sprintf(buf, "3: Disabled\n"); |
| 240 | case 4: return sprintf(buf, "4: PortConfigurationTraining\n"); |
| 241 | case 5: return sprintf(buf, "5: LinkUp\n"); |
| 242 | case 6: return sprintf(buf, "6: LinkErrorRecovery\n"); |
| 243 | case 7: return sprintf(buf, "7: Phy Test\n"); |
| 244 | default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state); |
| 245 | } |
| 246 | } |
| 247 | |
Eli Cohen | 8ad330a | 2010-10-22 14:32:05 +0200 | [diff] [blame] | 248 | static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused, |
| 249 | char *buf) |
| 250 | { |
| 251 | switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) { |
| 252 | case IB_LINK_LAYER_INFINIBAND: |
| 253 | return sprintf(buf, "%s\n", "InfiniBand"); |
| 254 | case IB_LINK_LAYER_ETHERNET: |
| 255 | return sprintf(buf, "%s\n", "Ethernet"); |
| 256 | default: |
| 257 | return sprintf(buf, "%s\n", "Unknown"); |
| 258 | } |
| 259 | } |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | static PORT_ATTR_RO(state); |
| 262 | static PORT_ATTR_RO(lid); |
| 263 | static PORT_ATTR_RO(lid_mask_count); |
| 264 | static PORT_ATTR_RO(sm_lid); |
| 265 | static PORT_ATTR_RO(sm_sl); |
| 266 | static PORT_ATTR_RO(cap_mask); |
| 267 | static PORT_ATTR_RO(rate); |
| 268 | static PORT_ATTR_RO(phys_state); |
Eli Cohen | 8ad330a | 2010-10-22 14:32:05 +0200 | [diff] [blame] | 269 | static PORT_ATTR_RO(link_layer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | static struct attribute *port_default_attrs[] = { |
| 272 | &port_attr_state.attr, |
| 273 | &port_attr_lid.attr, |
| 274 | &port_attr_lid_mask_count.attr, |
| 275 | &port_attr_sm_lid.attr, |
| 276 | &port_attr_sm_sl.attr, |
| 277 | &port_attr_cap_mask.attr, |
| 278 | &port_attr_rate.attr, |
| 279 | &port_attr_phys_state.attr, |
Eli Cohen | 8ad330a | 2010-10-22 14:32:05 +0200 | [diff] [blame] | 280 | &port_attr_link_layer.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | NULL |
| 282 | }; |
| 283 | |
| 284 | static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr, |
| 285 | char *buf) |
| 286 | { |
| 287 | struct port_table_attribute *tab_attr = |
| 288 | container_of(attr, struct port_table_attribute, attr); |
| 289 | union ib_gid gid; |
| 290 | ssize_t ret; |
| 291 | |
| 292 | ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid); |
| 293 | if (ret) |
| 294 | return ret; |
| 295 | |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 296 | return sprintf(buf, "%pI6\n", gid.raw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr, |
| 300 | char *buf) |
| 301 | { |
| 302 | struct port_table_attribute *tab_attr = |
| 303 | container_of(attr, struct port_table_attribute, attr); |
| 304 | u16 pkey; |
| 305 | ssize_t ret; |
| 306 | |
| 307 | ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey); |
| 308 | if (ret) |
| 309 | return ret; |
| 310 | |
| 311 | return sprintf(buf, "0x%04x\n", pkey); |
| 312 | } |
| 313 | |
| 314 | #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \ |
| 315 | struct port_table_attribute port_pma_attr_##_name = { \ |
| 316 | .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \ |
| 317 | .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \ |
| 318 | } |
| 319 | |
| 320 | static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr, |
| 321 | char *buf) |
| 322 | { |
| 323 | struct port_table_attribute *tab_attr = |
| 324 | container_of(attr, struct port_table_attribute, attr); |
| 325 | int offset = tab_attr->index & 0xffff; |
| 326 | int width = (tab_attr->index >> 16) & 0xff; |
| 327 | struct ib_mad *in_mad = NULL; |
| 328 | struct ib_mad *out_mad = NULL; |
| 329 | ssize_t ret; |
| 330 | |
| 331 | if (!p->ibdev->process_mad) |
| 332 | return sprintf(buf, "N/A (no PMA)\n"); |
| 333 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 334 | in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); |
Dotan Barak | 856c52a | 2007-07-10 16:55:57 +0300 | [diff] [blame] | 335 | out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (!in_mad || !out_mad) { |
| 337 | ret = -ENOMEM; |
| 338 | goto out; |
| 339 | } |
| 340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | in_mad->mad_hdr.base_version = 1; |
| 342 | in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT; |
| 343 | in_mad->mad_hdr.class_version = 1; |
| 344 | in_mad->mad_hdr.method = IB_MGMT_METHOD_GET; |
| 345 | in_mad->mad_hdr.attr_id = cpu_to_be16(0x12); /* PortCounters */ |
| 346 | |
| 347 | in_mad->data[41] = p->port_num; /* PortSelect field */ |
| 348 | |
| 349 | if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY, |
| 350 | p->port_num, NULL, NULL, in_mad, out_mad) & |
| 351 | (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) != |
| 352 | (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) { |
| 353 | ret = -EINVAL; |
| 354 | goto out; |
| 355 | } |
| 356 | |
| 357 | switch (width) { |
| 358 | case 4: |
| 359 | ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >> |
Ralph Campbell | d8b9f23 | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 360 | (4 - (offset % 8))) & 0xf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | break; |
| 362 | case 8: |
| 363 | ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]); |
| 364 | break; |
| 365 | case 16: |
| 366 | ret = sprintf(buf, "%u\n", |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 367 | be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | break; |
| 369 | case 32: |
| 370 | ret = sprintf(buf, "%u\n", |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 371 | be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | break; |
| 373 | default: |
| 374 | ret = 0; |
| 375 | } |
| 376 | |
| 377 | out: |
| 378 | kfree(in_mad); |
| 379 | kfree(out_mad); |
| 380 | |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | static PORT_PMA_ATTR(symbol_error , 0, 16, 32); |
| 385 | static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48); |
| 386 | static PORT_PMA_ATTR(link_downed , 2, 8, 56); |
| 387 | static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64); |
| 388 | static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80); |
| 389 | static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96); |
| 390 | static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112); |
| 391 | static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128); |
| 392 | static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136); |
| 393 | static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152); |
| 394 | static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156); |
| 395 | static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176); |
| 396 | static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192); |
| 397 | static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224); |
| 398 | static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256); |
| 399 | static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288); |
| 400 | |
| 401 | static struct attribute *pma_attrs[] = { |
| 402 | &port_pma_attr_symbol_error.attr.attr, |
| 403 | &port_pma_attr_link_error_recovery.attr.attr, |
| 404 | &port_pma_attr_link_downed.attr.attr, |
| 405 | &port_pma_attr_port_rcv_errors.attr.attr, |
| 406 | &port_pma_attr_port_rcv_remote_physical_errors.attr.attr, |
| 407 | &port_pma_attr_port_rcv_switch_relay_errors.attr.attr, |
| 408 | &port_pma_attr_port_xmit_discards.attr.attr, |
| 409 | &port_pma_attr_port_xmit_constraint_errors.attr.attr, |
| 410 | &port_pma_attr_port_rcv_constraint_errors.attr.attr, |
| 411 | &port_pma_attr_local_link_integrity_errors.attr.attr, |
| 412 | &port_pma_attr_excessive_buffer_overrun_errors.attr.attr, |
| 413 | &port_pma_attr_VL15_dropped.attr.attr, |
| 414 | &port_pma_attr_port_xmit_data.attr.attr, |
| 415 | &port_pma_attr_port_rcv_data.attr.attr, |
| 416 | &port_pma_attr_port_xmit_packets.attr.attr, |
| 417 | &port_pma_attr_port_rcv_packets.attr.attr, |
| 418 | NULL |
| 419 | }; |
| 420 | |
| 421 | static struct attribute_group pma_group = { |
| 422 | .name = "counters", |
| 423 | .attrs = pma_attrs |
| 424 | }; |
| 425 | |
| 426 | static void ib_port_release(struct kobject *kobj) |
| 427 | { |
| 428 | struct ib_port *p = container_of(kobj, struct ib_port, kobj); |
| 429 | struct attribute *a; |
| 430 | int i; |
| 431 | |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 432 | if (p->gid_group.attrs) { |
| 433 | for (i = 0; (a = p->gid_group.attrs[i]); ++i) |
| 434 | kfree(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 436 | kfree(p->gid_group.attrs); |
| 437 | } |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 438 | |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 439 | if (p->pkey_group.attrs) { |
| 440 | for (i = 0; (a = p->pkey_group.attrs[i]); ++i) |
| 441 | kfree(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 443 | kfree(p->pkey_group.attrs); |
| 444 | } |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | kfree(p); |
| 447 | } |
| 448 | |
| 449 | static struct kobj_type port_type = { |
| 450 | .release = ib_port_release, |
| 451 | .sysfs_ops = &port_sysfs_ops, |
| 452 | .default_attrs = port_default_attrs |
| 453 | }; |
| 454 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 455 | static void ib_device_release(struct device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 457 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | kfree(dev); |
| 460 | } |
| 461 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 462 | static int ib_device_uevent(struct device *device, |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 463 | struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 465 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 467 | if (add_uevent_var(env, "NAME=%s", dev->name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return -ENOMEM; |
| 469 | |
| 470 | /* |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 471 | * It would be nice to pass the node GUID with the event... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | */ |
| 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return 0; |
| 475 | } |
| 476 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 477 | static struct attribute ** |
| 478 | alloc_group_attrs(ssize_t (*show)(struct ib_port *, |
| 479 | struct port_attribute *, char *buf), |
| 480 | int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 482 | struct attribute **tab_attr; |
| 483 | struct port_table_attribute *element; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 486 | tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL); |
| 487 | if (!tab_attr) |
| 488 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 490 | for (i = 0; i < len; i++) { |
Pekka Enberg | 82ca76b | 2005-09-06 15:18:35 -0700 | [diff] [blame] | 491 | element = kzalloc(sizeof(struct port_table_attribute), |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 492 | GFP_KERNEL); |
| 493 | if (!element) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 496 | if (snprintf(element->name, sizeof(element->name), |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 497 | "%d", i) >= sizeof(element->name)) { |
| 498 | kfree(element); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | goto err; |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 502 | element->attr.attr.name = element->name; |
| 503 | element->attr.attr.mode = S_IRUGO; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 504 | element->attr.show = show; |
| 505 | element->index = i; |
Greg Kroah-Hartman | 21e3bde | 2010-03-15 14:01:25 -0700 | [diff] [blame] | 506 | sysfs_attr_init(&element->attr.attr); |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 507 | |
| 508 | tab_attr[i] = &element->attr.attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 511 | return tab_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | err: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 514 | while (--i >= 0) |
| 515 | kfree(tab_attr[i]); |
| 516 | kfree(tab_attr); |
| 517 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 520 | static int add_port(struct ib_device *device, int port_num, |
| 521 | int (*port_callback)(struct ib_device *, |
| 522 | u8, struct kobject *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
| 524 | struct ib_port *p; |
| 525 | struct ib_port_attr attr; |
| 526 | int i; |
| 527 | int ret; |
| 528 | |
| 529 | ret = ib_query_port(device, port_num, &attr); |
| 530 | if (ret) |
| 531 | return ret; |
| 532 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 533 | p = kzalloc(sizeof *p, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (!p) |
| 535 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | p->ibdev = device; |
| 538 | p->port_num = port_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 540 | ret = kobject_init_and_add(&p->kobj, &port_type, |
Haggai Eran | 373c0ea | 2014-05-18 11:12:24 +0300 | [diff] [blame] | 541 | device->ports_parent, |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 542 | "%d", port_num); |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 543 | if (ret) { |
| 544 | kfree(p); |
| 545 | return ret; |
| 546 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | ret = sysfs_create_group(&p->kobj, &pma_group); |
| 549 | if (ret) |
| 550 | goto err_put; |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | p->gid_group.name = "gids"; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 553 | 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] | 554 | if (!p->gid_group.attrs) { |
| 555 | ret = -ENOMEM; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 556 | goto err_remove_pma; |
Wei Yongjun | 80b1504 | 2013-06-21 11:24:27 +0800 | [diff] [blame] | 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | ret = sysfs_create_group(&p->kobj, &p->gid_group); |
| 560 | if (ret) |
| 561 | goto err_free_gid; |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | p->pkey_group.name = "pkeys"; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 564 | p->pkey_group.attrs = alloc_group_attrs(show_port_pkey, |
| 565 | attr.pkey_tbl_len); |
Wei Yongjun | 80b1504 | 2013-06-21 11:24:27 +0800 | [diff] [blame] | 566 | if (!p->pkey_group.attrs) { |
| 567 | ret = -ENOMEM; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 568 | goto err_remove_gid; |
Wei Yongjun | 80b1504 | 2013-06-21 11:24:27 +0800 | [diff] [blame] | 569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
| 571 | ret = sysfs_create_group(&p->kobj, &p->pkey_group); |
| 572 | if (ret) |
| 573 | goto err_free_pkey; |
| 574 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 575 | if (port_callback) { |
| 576 | ret = port_callback(device, port_num, &p->kobj); |
| 577 | if (ret) |
| 578 | goto err_remove_pkey; |
| 579 | } |
| 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | list_add_tail(&p->kobj.entry, &device->port_list); |
| 582 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 583 | kobject_uevent(&p->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return 0; |
| 585 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 586 | err_remove_pkey: |
| 587 | sysfs_remove_group(&p->kobj, &p->pkey_group); |
| 588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | err_free_pkey: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 590 | for (i = 0; i < attr.pkey_tbl_len; ++i) |
| 591 | kfree(p->pkey_group.attrs[i]); |
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 | kfree(p->pkey_group.attrs); |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 594 | p->pkey_group.attrs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | err_remove_gid: |
| 597 | sysfs_remove_group(&p->kobj, &p->gid_group); |
| 598 | |
| 599 | err_free_gid: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 600 | for (i = 0; i < attr.gid_tbl_len; ++i) |
| 601 | kfree(p->gid_group.attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 603 | kfree(p->gid_group.attrs); |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 604 | p->gid_group.attrs = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | err_remove_pma: |
| 607 | sysfs_remove_group(&p->kobj, &pma_group); |
| 608 | |
| 609 | err_put: |
Haggai Eran | cad6d02 | 2014-05-18 11:12:25 +0300 | [diff] [blame] | 610 | kobject_put(&p->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | return ret; |
| 612 | } |
| 613 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 614 | static ssize_t show_node_type(struct device *device, |
| 615 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 617 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | switch (dev->node_type) { |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 620 | case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type); |
| 621 | 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] | 622 | 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] | 623 | 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] | 624 | case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type); |
| 625 | case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type); |
| 626 | default: return sprintf(buf, "%d: <unknown>\n", dev->node_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 630 | static ssize_t show_sys_image_guid(struct device *device, |
| 631 | struct device_attribute *dev_attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 633 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | struct ib_device_attr attr; |
| 635 | ssize_t ret; |
| 636 | |
| 637 | ret = ib_query_device(dev, &attr); |
| 638 | if (ret) |
| 639 | return ret; |
| 640 | |
| 641 | return sprintf(buf, "%04x:%04x:%04x:%04x\n", |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 642 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]), |
| 643 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]), |
| 644 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]), |
| 645 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 648 | static ssize_t show_node_guid(struct device *device, |
| 649 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 651 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return sprintf(buf, "%04x:%04x:%04x:%04x\n", |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 654 | be16_to_cpu(((__be16 *) &dev->node_guid)[0]), |
| 655 | be16_to_cpu(((__be16 *) &dev->node_guid)[1]), |
| 656 | be16_to_cpu(((__be16 *) &dev->node_guid)[2]), |
| 657 | be16_to_cpu(((__be16 *) &dev->node_guid)[3])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 660 | static ssize_t show_node_desc(struct device *device, |
| 661 | struct device_attribute *attr, char *buf) |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 662 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 663 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 664 | |
| 665 | return sprintf(buf, "%.64s\n", dev->node_desc); |
| 666 | } |
| 667 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 668 | static ssize_t set_node_desc(struct device *device, |
| 669 | struct device_attribute *attr, |
| 670 | const char *buf, size_t count) |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 671 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 672 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 673 | struct ib_device_modify desc = {}; |
| 674 | int ret; |
| 675 | |
| 676 | if (!dev->modify_device) |
| 677 | return -EIO; |
| 678 | |
| 679 | memcpy(desc.node_desc, buf, min_t(int, count, 64)); |
| 680 | ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc); |
| 681 | if (ret) |
| 682 | return ret; |
| 683 | |
| 684 | return count; |
| 685 | } |
| 686 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 687 | static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL); |
| 688 | static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL); |
| 689 | static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL); |
| 690 | 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] | 691 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 692 | static struct device_attribute *ib_class_attributes[] = { |
| 693 | &dev_attr_node_type, |
| 694 | &dev_attr_sys_image_guid, |
| 695 | &dev_attr_node_guid, |
| 696 | &dev_attr_node_desc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | }; |
| 698 | |
| 699 | static struct class ib_class = { |
| 700 | .name = "infiniband", |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 701 | .dev_release = ib_device_release, |
| 702 | .dev_uevent = ib_device_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | }; |
| 704 | |
Steve Wise | 7f624d0 | 2008-07-14 23:48:48 -0700 | [diff] [blame] | 705 | /* Show a given an attribute in the statistics group */ |
| 706 | static ssize_t show_protocol_stat(const struct device *device, |
| 707 | struct device_attribute *attr, char *buf, |
| 708 | unsigned offset) |
| 709 | { |
| 710 | struct ib_device *dev = container_of(device, struct ib_device, dev); |
| 711 | union rdma_protocol_stats stats; |
| 712 | ssize_t ret; |
| 713 | |
| 714 | ret = dev->get_protocol_stats(dev, &stats); |
| 715 | if (ret) |
| 716 | return ret; |
| 717 | |
| 718 | return sprintf(buf, "%llu\n", |
| 719 | (unsigned long long) ((u64 *) &stats)[offset]); |
| 720 | } |
| 721 | |
| 722 | /* generate a read-only iwarp statistics attribute */ |
| 723 | #define IW_STATS_ENTRY(name) \ |
| 724 | static ssize_t show_##name(struct device *device, \ |
| 725 | struct device_attribute *attr, char *buf) \ |
| 726 | { \ |
| 727 | return show_protocol_stat(device, attr, buf, \ |
| 728 | offsetof(struct iw_protocol_stats, name) / \ |
| 729 | sizeof (u64)); \ |
| 730 | } \ |
| 731 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 732 | |
| 733 | IW_STATS_ENTRY(ipInReceives); |
| 734 | IW_STATS_ENTRY(ipInHdrErrors); |
| 735 | IW_STATS_ENTRY(ipInTooBigErrors); |
| 736 | IW_STATS_ENTRY(ipInNoRoutes); |
| 737 | IW_STATS_ENTRY(ipInAddrErrors); |
| 738 | IW_STATS_ENTRY(ipInUnknownProtos); |
| 739 | IW_STATS_ENTRY(ipInTruncatedPkts); |
| 740 | IW_STATS_ENTRY(ipInDiscards); |
| 741 | IW_STATS_ENTRY(ipInDelivers); |
| 742 | IW_STATS_ENTRY(ipOutForwDatagrams); |
| 743 | IW_STATS_ENTRY(ipOutRequests); |
| 744 | IW_STATS_ENTRY(ipOutDiscards); |
| 745 | IW_STATS_ENTRY(ipOutNoRoutes); |
| 746 | IW_STATS_ENTRY(ipReasmTimeout); |
| 747 | IW_STATS_ENTRY(ipReasmReqds); |
| 748 | IW_STATS_ENTRY(ipReasmOKs); |
| 749 | IW_STATS_ENTRY(ipReasmFails); |
| 750 | IW_STATS_ENTRY(ipFragOKs); |
| 751 | IW_STATS_ENTRY(ipFragFails); |
| 752 | IW_STATS_ENTRY(ipFragCreates); |
| 753 | IW_STATS_ENTRY(ipInMcastPkts); |
| 754 | IW_STATS_ENTRY(ipOutMcastPkts); |
| 755 | IW_STATS_ENTRY(ipInBcastPkts); |
| 756 | IW_STATS_ENTRY(ipOutBcastPkts); |
| 757 | IW_STATS_ENTRY(tcpRtoAlgorithm); |
| 758 | IW_STATS_ENTRY(tcpRtoMin); |
| 759 | IW_STATS_ENTRY(tcpRtoMax); |
| 760 | IW_STATS_ENTRY(tcpMaxConn); |
| 761 | IW_STATS_ENTRY(tcpActiveOpens); |
| 762 | IW_STATS_ENTRY(tcpPassiveOpens); |
| 763 | IW_STATS_ENTRY(tcpAttemptFails); |
| 764 | IW_STATS_ENTRY(tcpEstabResets); |
| 765 | IW_STATS_ENTRY(tcpCurrEstab); |
| 766 | IW_STATS_ENTRY(tcpInSegs); |
| 767 | IW_STATS_ENTRY(tcpOutSegs); |
| 768 | IW_STATS_ENTRY(tcpRetransSegs); |
| 769 | IW_STATS_ENTRY(tcpInErrs); |
| 770 | IW_STATS_ENTRY(tcpOutRsts); |
| 771 | |
| 772 | static struct attribute *iw_proto_stats_attrs[] = { |
| 773 | &dev_attr_ipInReceives.attr, |
| 774 | &dev_attr_ipInHdrErrors.attr, |
| 775 | &dev_attr_ipInTooBigErrors.attr, |
| 776 | &dev_attr_ipInNoRoutes.attr, |
| 777 | &dev_attr_ipInAddrErrors.attr, |
| 778 | &dev_attr_ipInUnknownProtos.attr, |
| 779 | &dev_attr_ipInTruncatedPkts.attr, |
| 780 | &dev_attr_ipInDiscards.attr, |
| 781 | &dev_attr_ipInDelivers.attr, |
| 782 | &dev_attr_ipOutForwDatagrams.attr, |
| 783 | &dev_attr_ipOutRequests.attr, |
| 784 | &dev_attr_ipOutDiscards.attr, |
| 785 | &dev_attr_ipOutNoRoutes.attr, |
| 786 | &dev_attr_ipReasmTimeout.attr, |
| 787 | &dev_attr_ipReasmReqds.attr, |
| 788 | &dev_attr_ipReasmOKs.attr, |
| 789 | &dev_attr_ipReasmFails.attr, |
| 790 | &dev_attr_ipFragOKs.attr, |
| 791 | &dev_attr_ipFragFails.attr, |
| 792 | &dev_attr_ipFragCreates.attr, |
| 793 | &dev_attr_ipInMcastPkts.attr, |
| 794 | &dev_attr_ipOutMcastPkts.attr, |
| 795 | &dev_attr_ipInBcastPkts.attr, |
| 796 | &dev_attr_ipOutBcastPkts.attr, |
| 797 | &dev_attr_tcpRtoAlgorithm.attr, |
| 798 | &dev_attr_tcpRtoMin.attr, |
| 799 | &dev_attr_tcpRtoMax.attr, |
| 800 | &dev_attr_tcpMaxConn.attr, |
| 801 | &dev_attr_tcpActiveOpens.attr, |
| 802 | &dev_attr_tcpPassiveOpens.attr, |
| 803 | &dev_attr_tcpAttemptFails.attr, |
| 804 | &dev_attr_tcpEstabResets.attr, |
| 805 | &dev_attr_tcpCurrEstab.attr, |
| 806 | &dev_attr_tcpInSegs.attr, |
| 807 | &dev_attr_tcpOutSegs.attr, |
| 808 | &dev_attr_tcpRetransSegs.attr, |
| 809 | &dev_attr_tcpInErrs.attr, |
| 810 | &dev_attr_tcpOutRsts.attr, |
| 811 | NULL |
| 812 | }; |
| 813 | |
| 814 | static struct attribute_group iw_stats_group = { |
| 815 | .name = "proto_stats", |
| 816 | .attrs = iw_proto_stats_attrs, |
| 817 | }; |
| 818 | |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 819 | static void free_port_list_attributes(struct ib_device *device) |
| 820 | { |
| 821 | struct kobject *p, *t; |
| 822 | |
| 823 | list_for_each_entry_safe(p, t, &device->port_list, entry) { |
| 824 | struct ib_port *port = container_of(p, struct ib_port, kobj); |
| 825 | list_del(&p->entry); |
| 826 | sysfs_remove_group(p, &pma_group); |
| 827 | sysfs_remove_group(p, &port->pkey_group); |
| 828 | sysfs_remove_group(p, &port->gid_group); |
| 829 | kobject_put(p); |
| 830 | } |
| 831 | |
| 832 | kobject_put(device->ports_parent); |
| 833 | } |
| 834 | |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 835 | int ib_device_register_sysfs(struct ib_device *device, |
| 836 | int (*port_callback)(struct ib_device *, |
| 837 | u8, struct kobject *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 839 | struct device *class_dev = &device->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | int ret; |
| 841 | int i; |
| 842 | |
| 843 | class_dev->class = &ib_class; |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 844 | class_dev->parent = device->dma_device; |
Kees Cook | 02aa2a3 | 2013-07-03 15:04:56 -0700 | [diff] [blame] | 845 | dev_set_name(class_dev, "%s", device->name); |
Greg Kroah-Hartman | 3f7c58a | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 846 | dev_set_drvdata(class_dev, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | |
| 848 | INIT_LIST_HEAD(&device->port_list); |
| 849 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 850 | ret = device_register(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | if (ret) |
| 852 | goto err; |
| 853 | |
| 854 | for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) { |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 855 | ret = device_create_file(class_dev, ib_class_attributes[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | if (ret) |
| 857 | goto err_unregister; |
| 858 | } |
| 859 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 860 | device->ports_parent = kobject_create_and_add("ports", |
Haggai Eran | 373c0ea | 2014-05-18 11:12:24 +0300 | [diff] [blame] | 861 | &class_dev->kobj); |
Li Zefan | c7482b8 | 2008-02-15 10:24:49 +0800 | [diff] [blame] | 862 | if (!device->ports_parent) { |
| 863 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | goto err_put; |
Li Zefan | c7482b8 | 2008-02-15 10:24:49 +0800 | [diff] [blame] | 865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 867 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 868 | ret = add_port(device, 0, port_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | if (ret) |
| 870 | goto err_put; |
| 871 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | for (i = 1; i <= device->phys_port_cnt; ++i) { |
Ralph Campbell | 9a6edb6 | 2010-05-06 17:03:25 -0700 | [diff] [blame] | 873 | ret = add_port(device, i, port_callback); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | if (ret) |
| 875 | goto err_put; |
| 876 | } |
| 877 | } |
| 878 | |
Steve Wise | 7f624d0 | 2008-07-14 23:48:48 -0700 | [diff] [blame] | 879 | if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) { |
| 880 | ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group); |
| 881 | if (ret) |
| 882 | goto err_put; |
| 883 | } |
| 884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | return 0; |
| 886 | |
| 887 | err_put: |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 888 | free_port_list_attributes(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | err_unregister: |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 891 | device_unregister(class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | err: |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | void ib_device_unregister_sysfs(struct ib_device *device) |
| 898 | { |
Roland Dreier | 9206dff | 2009-02-25 13:27:46 -0800 | [diff] [blame] | 899 | /* Hold kobject until ib_dealloc_device() */ |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 900 | struct kobject *kobj_dev = kobject_get(&device->dev.kobj); |
| 901 | int i; |
Roland Dreier | 9206dff | 2009-02-25 13:27:46 -0800 | [diff] [blame] | 902 | |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 903 | if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) |
| 904 | sysfs_remove_group(kobj_dev, &iw_stats_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Haggai Eran | 584482a | 2014-05-18 11:12:26 +0300 | [diff] [blame] | 906 | free_port_list_attributes(device); |
| 907 | |
| 908 | for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) |
| 909 | device_remove_file(&device->dev, ib_class_attributes[i]); |
| 910 | |
Tony Jones | f4e91eb | 2008-02-22 00:13:36 +0100 | [diff] [blame] | 911 | device_unregister(&device->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | int ib_sysfs_setup(void) |
| 915 | { |
| 916 | return class_register(&ib_class); |
| 917 | } |
| 918 | |
| 919 | void ib_sysfs_cleanup(void) |
| 920 | { |
| 921 | class_unregister(&ib_class); |
| 922 | } |