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. |
| 33 | * |
| 34 | * $Id: sysfs.c 1349 2004-12-16 21:09:43Z roland $ |
| 35 | */ |
| 36 | |
| 37 | #include "core_priv.h" |
| 38 | |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 39 | #include <linux/slab.h> |
| 40 | #include <linux/string.h> |
| 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 | |
| 44 | struct ib_port { |
| 45 | struct kobject kobj; |
| 46 | struct ib_device *ibdev; |
| 47 | struct attribute_group gid_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct attribute_group pkey_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | u8 port_num; |
| 50 | }; |
| 51 | |
| 52 | struct port_attribute { |
| 53 | struct attribute attr; |
| 54 | ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf); |
| 55 | ssize_t (*store)(struct ib_port *, struct port_attribute *, |
| 56 | const char *buf, size_t count); |
| 57 | }; |
| 58 | |
| 59 | #define PORT_ATTR(_name, _mode, _show, _store) \ |
| 60 | struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store) |
| 61 | |
| 62 | #define PORT_ATTR_RO(_name) \ |
| 63 | struct port_attribute port_attr_##_name = __ATTR_RO(_name) |
| 64 | |
| 65 | struct port_table_attribute { |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 66 | struct port_attribute attr; |
| 67 | char name[8]; |
| 68 | int index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 71 | static inline int ibdev_is_alive(const struct ib_device *dev) |
Roland Dreier | ba8e931 | 2005-10-18 14:14:56 -0700 | [diff] [blame] | 72 | { |
| 73 | return dev->reg_state == IB_DEV_REGISTERED; |
| 74 | } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | static ssize_t port_attr_show(struct kobject *kobj, |
| 77 | struct attribute *attr, char *buf) |
| 78 | { |
| 79 | struct port_attribute *port_attr = |
| 80 | container_of(attr, struct port_attribute, attr); |
| 81 | struct ib_port *p = container_of(kobj, struct ib_port, kobj); |
| 82 | |
| 83 | if (!port_attr->show) |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 84 | return -EIO; |
Roland Dreier | ba8e931 | 2005-10-18 14:14:56 -0700 | [diff] [blame] | 85 | if (!ibdev_is_alive(p->ibdev)) |
| 86 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | return port_attr->show(p, port_attr, buf); |
| 89 | } |
| 90 | |
| 91 | static struct sysfs_ops port_sysfs_ops = { |
| 92 | .show = port_attr_show |
| 93 | }; |
| 94 | |
| 95 | static ssize_t state_show(struct ib_port *p, struct port_attribute *unused, |
| 96 | char *buf) |
| 97 | { |
| 98 | struct ib_port_attr attr; |
| 99 | ssize_t ret; |
| 100 | |
| 101 | static const char *state_name[] = { |
| 102 | [IB_PORT_NOP] = "NOP", |
| 103 | [IB_PORT_DOWN] = "DOWN", |
| 104 | [IB_PORT_INIT] = "INIT", |
| 105 | [IB_PORT_ARMED] = "ARMED", |
| 106 | [IB_PORT_ACTIVE] = "ACTIVE", |
| 107 | [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER" |
| 108 | }; |
| 109 | |
| 110 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 111 | if (ret) |
| 112 | return ret; |
| 113 | |
| 114 | return sprintf(buf, "%d: %s\n", attr.state, |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 115 | attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | state_name[attr.state] : "UNKNOWN"); |
| 117 | } |
| 118 | |
| 119 | static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused, |
| 120 | char *buf) |
| 121 | { |
| 122 | struct ib_port_attr attr; |
| 123 | ssize_t ret; |
| 124 | |
| 125 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 126 | if (ret) |
| 127 | return ret; |
| 128 | |
| 129 | return sprintf(buf, "0x%x\n", attr.lid); |
| 130 | } |
| 131 | |
| 132 | static ssize_t lid_mask_count_show(struct ib_port *p, |
| 133 | struct port_attribute *unused, |
| 134 | char *buf) |
| 135 | { |
| 136 | struct ib_port_attr attr; |
| 137 | ssize_t ret; |
| 138 | |
| 139 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 140 | if (ret) |
| 141 | return ret; |
| 142 | |
| 143 | return sprintf(buf, "%d\n", attr.lmc); |
| 144 | } |
| 145 | |
| 146 | static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused, |
| 147 | char *buf) |
| 148 | { |
| 149 | struct ib_port_attr attr; |
| 150 | ssize_t ret; |
| 151 | |
| 152 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | return sprintf(buf, "0x%x\n", attr.sm_lid); |
| 157 | } |
| 158 | |
| 159 | static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused, |
| 160 | char *buf) |
| 161 | { |
| 162 | struct ib_port_attr attr; |
| 163 | ssize_t ret; |
| 164 | |
| 165 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | |
| 169 | return sprintf(buf, "%d\n", attr.sm_sl); |
| 170 | } |
| 171 | |
| 172 | static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused, |
| 173 | char *buf) |
| 174 | { |
| 175 | struct ib_port_attr attr; |
| 176 | ssize_t ret; |
| 177 | |
| 178 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 179 | if (ret) |
| 180 | return ret; |
| 181 | |
| 182 | return sprintf(buf, "0x%08x\n", attr.port_cap_flags); |
| 183 | } |
| 184 | |
| 185 | static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused, |
| 186 | char *buf) |
| 187 | { |
| 188 | struct ib_port_attr attr; |
| 189 | char *speed = ""; |
| 190 | int rate; |
| 191 | ssize_t ret; |
| 192 | |
| 193 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | |
| 197 | switch (attr.active_speed) { |
| 198 | case 2: speed = " DDR"; break; |
| 199 | case 4: speed = " QDR"; break; |
| 200 | } |
| 201 | |
| 202 | rate = 25 * ib_width_enum_to_int(attr.active_width) * attr.active_speed; |
| 203 | if (rate < 0) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | return sprintf(buf, "%d%s Gb/sec (%dX%s)\n", |
| 207 | rate / 10, rate % 10 ? ".5" : "", |
| 208 | ib_width_enum_to_int(attr.active_width), speed); |
| 209 | } |
| 210 | |
| 211 | static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused, |
| 212 | char *buf) |
| 213 | { |
| 214 | struct ib_port_attr attr; |
| 215 | |
| 216 | ssize_t ret; |
| 217 | |
| 218 | ret = ib_query_port(p->ibdev, p->port_num, &attr); |
| 219 | if (ret) |
| 220 | return ret; |
| 221 | |
| 222 | switch (attr.phys_state) { |
| 223 | case 1: return sprintf(buf, "1: Sleep\n"); |
| 224 | case 2: return sprintf(buf, "2: Polling\n"); |
| 225 | case 3: return sprintf(buf, "3: Disabled\n"); |
| 226 | case 4: return sprintf(buf, "4: PortConfigurationTraining\n"); |
| 227 | case 5: return sprintf(buf, "5: LinkUp\n"); |
| 228 | case 6: return sprintf(buf, "6: LinkErrorRecovery\n"); |
| 229 | case 7: return sprintf(buf, "7: Phy Test\n"); |
| 230 | default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static PORT_ATTR_RO(state); |
| 235 | static PORT_ATTR_RO(lid); |
| 236 | static PORT_ATTR_RO(lid_mask_count); |
| 237 | static PORT_ATTR_RO(sm_lid); |
| 238 | static PORT_ATTR_RO(sm_sl); |
| 239 | static PORT_ATTR_RO(cap_mask); |
| 240 | static PORT_ATTR_RO(rate); |
| 241 | static PORT_ATTR_RO(phys_state); |
| 242 | |
| 243 | static struct attribute *port_default_attrs[] = { |
| 244 | &port_attr_state.attr, |
| 245 | &port_attr_lid.attr, |
| 246 | &port_attr_lid_mask_count.attr, |
| 247 | &port_attr_sm_lid.attr, |
| 248 | &port_attr_sm_sl.attr, |
| 249 | &port_attr_cap_mask.attr, |
| 250 | &port_attr_rate.attr, |
| 251 | &port_attr_phys_state.attr, |
| 252 | NULL |
| 253 | }; |
| 254 | |
| 255 | static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr, |
| 256 | char *buf) |
| 257 | { |
| 258 | struct port_table_attribute *tab_attr = |
| 259 | container_of(attr, struct port_table_attribute, attr); |
| 260 | union ib_gid gid; |
| 261 | ssize_t ret; |
| 262 | |
| 263 | ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 268 | be16_to_cpu(((__be16 *) gid.raw)[0]), |
| 269 | be16_to_cpu(((__be16 *) gid.raw)[1]), |
| 270 | be16_to_cpu(((__be16 *) gid.raw)[2]), |
| 271 | be16_to_cpu(((__be16 *) gid.raw)[3]), |
| 272 | be16_to_cpu(((__be16 *) gid.raw)[4]), |
| 273 | be16_to_cpu(((__be16 *) gid.raw)[5]), |
| 274 | be16_to_cpu(((__be16 *) gid.raw)[6]), |
| 275 | be16_to_cpu(((__be16 *) gid.raw)[7])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr, |
| 279 | char *buf) |
| 280 | { |
| 281 | struct port_table_attribute *tab_attr = |
| 282 | container_of(attr, struct port_table_attribute, attr); |
| 283 | u16 pkey; |
| 284 | ssize_t ret; |
| 285 | |
| 286 | ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | |
| 290 | return sprintf(buf, "0x%04x\n", pkey); |
| 291 | } |
| 292 | |
| 293 | #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \ |
| 294 | struct port_table_attribute port_pma_attr_##_name = { \ |
| 295 | .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \ |
| 296 | .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \ |
| 297 | } |
| 298 | |
| 299 | static ssize_t show_pma_counter(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 | int offset = tab_attr->index & 0xffff; |
| 305 | int width = (tab_attr->index >> 16) & 0xff; |
| 306 | struct ib_mad *in_mad = NULL; |
| 307 | struct ib_mad *out_mad = NULL; |
| 308 | ssize_t ret; |
| 309 | |
| 310 | if (!p->ibdev->process_mad) |
| 311 | return sprintf(buf, "N/A (no PMA)\n"); |
| 312 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 313 | in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL); |
Dotan Barak | 856c52a | 2007-07-10 16:55:57 +0300 | [diff] [blame] | 314 | out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | if (!in_mad || !out_mad) { |
| 316 | ret = -ENOMEM; |
| 317 | goto out; |
| 318 | } |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | in_mad->mad_hdr.base_version = 1; |
| 321 | in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT; |
| 322 | in_mad->mad_hdr.class_version = 1; |
| 323 | in_mad->mad_hdr.method = IB_MGMT_METHOD_GET; |
| 324 | in_mad->mad_hdr.attr_id = cpu_to_be16(0x12); /* PortCounters */ |
| 325 | |
| 326 | in_mad->data[41] = p->port_num; /* PortSelect field */ |
| 327 | |
| 328 | if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY, |
| 329 | p->port_num, NULL, NULL, in_mad, out_mad) & |
| 330 | (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) != |
| 331 | (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) { |
| 332 | ret = -EINVAL; |
| 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | switch (width) { |
| 337 | case 4: |
| 338 | ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >> |
Ralph Campbell | d8b9f23 | 2006-05-09 10:50:28 -0700 | [diff] [blame] | 339 | (4 - (offset % 8))) & 0xf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | break; |
| 341 | case 8: |
| 342 | ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]); |
| 343 | break; |
| 344 | case 16: |
| 345 | ret = sprintf(buf, "%u\n", |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 346 | be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | break; |
| 348 | case 32: |
| 349 | ret = sprintf(buf, "%u\n", |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 350 | be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | break; |
| 352 | default: |
| 353 | ret = 0; |
| 354 | } |
| 355 | |
| 356 | out: |
| 357 | kfree(in_mad); |
| 358 | kfree(out_mad); |
| 359 | |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static PORT_PMA_ATTR(symbol_error , 0, 16, 32); |
| 364 | static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48); |
| 365 | static PORT_PMA_ATTR(link_downed , 2, 8, 56); |
| 366 | static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64); |
| 367 | static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80); |
| 368 | static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96); |
| 369 | static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112); |
| 370 | static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128); |
| 371 | static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136); |
| 372 | static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152); |
| 373 | static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156); |
| 374 | static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176); |
| 375 | static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192); |
| 376 | static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224); |
| 377 | static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256); |
| 378 | static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288); |
| 379 | |
| 380 | static struct attribute *pma_attrs[] = { |
| 381 | &port_pma_attr_symbol_error.attr.attr, |
| 382 | &port_pma_attr_link_error_recovery.attr.attr, |
| 383 | &port_pma_attr_link_downed.attr.attr, |
| 384 | &port_pma_attr_port_rcv_errors.attr.attr, |
| 385 | &port_pma_attr_port_rcv_remote_physical_errors.attr.attr, |
| 386 | &port_pma_attr_port_rcv_switch_relay_errors.attr.attr, |
| 387 | &port_pma_attr_port_xmit_discards.attr.attr, |
| 388 | &port_pma_attr_port_xmit_constraint_errors.attr.attr, |
| 389 | &port_pma_attr_port_rcv_constraint_errors.attr.attr, |
| 390 | &port_pma_attr_local_link_integrity_errors.attr.attr, |
| 391 | &port_pma_attr_excessive_buffer_overrun_errors.attr.attr, |
| 392 | &port_pma_attr_VL15_dropped.attr.attr, |
| 393 | &port_pma_attr_port_xmit_data.attr.attr, |
| 394 | &port_pma_attr_port_rcv_data.attr.attr, |
| 395 | &port_pma_attr_port_xmit_packets.attr.attr, |
| 396 | &port_pma_attr_port_rcv_packets.attr.attr, |
| 397 | NULL |
| 398 | }; |
| 399 | |
| 400 | static struct attribute_group pma_group = { |
| 401 | .name = "counters", |
| 402 | .attrs = pma_attrs |
| 403 | }; |
| 404 | |
| 405 | static void ib_port_release(struct kobject *kobj) |
| 406 | { |
| 407 | struct ib_port *p = container_of(kobj, struct ib_port, kobj); |
| 408 | struct attribute *a; |
| 409 | int i; |
| 410 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 411 | for (i = 0; (a = p->gid_group.attrs[i]); ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | kfree(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 414 | kfree(p->gid_group.attrs); |
| 415 | |
| 416 | for (i = 0; (a = p->pkey_group.attrs[i]); ++i) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | kfree(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 419 | kfree(p->pkey_group.attrs); |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | kfree(p); |
| 422 | } |
| 423 | |
| 424 | static struct kobj_type port_type = { |
| 425 | .release = ib_port_release, |
| 426 | .sysfs_ops = &port_sysfs_ops, |
| 427 | .default_attrs = port_default_attrs |
| 428 | }; |
| 429 | |
| 430 | static void ib_device_release(struct class_device *cdev) |
| 431 | { |
| 432 | struct ib_device *dev = container_of(cdev, struct ib_device, class_dev); |
| 433 | |
| 434 | kfree(dev); |
| 435 | } |
| 436 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 437 | static int ib_device_uevent(struct class_device *cdev, |
| 438 | struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
| 440 | struct ib_device *dev = container_of(cdev, struct ib_device, class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 442 | if (add_uevent_var(env, "NAME=%s", dev->name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return -ENOMEM; |
| 444 | |
| 445 | /* |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 446 | * It would be nice to pass the node GUID with the event... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | */ |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 452 | static struct attribute ** |
| 453 | alloc_group_attrs(ssize_t (*show)(struct ib_port *, |
| 454 | struct port_attribute *, char *buf), |
| 455 | int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 457 | struct attribute **tab_attr; |
| 458 | struct port_table_attribute *element; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 461 | tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL); |
| 462 | if (!tab_attr) |
| 463 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 465 | for (i = 0; i < len; i++) { |
Pekka Enberg | 82ca76b | 2005-09-06 15:18:35 -0700 | [diff] [blame] | 466 | element = kzalloc(sizeof(struct port_table_attribute), |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 467 | GFP_KERNEL); |
| 468 | if (!element) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 471 | if (snprintf(element->name, sizeof(element->name), |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 472 | "%d", i) >= sizeof(element->name)) { |
| 473 | kfree(element); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | goto err; |
Roland Dreier | 048975a | 2006-03-20 10:08:25 -0800 | [diff] [blame] | 475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 477 | element->attr.attr.name = element->name; |
| 478 | element->attr.attr.mode = S_IRUGO; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 479 | element->attr.show = show; |
| 480 | element->index = i; |
| 481 | |
| 482 | tab_attr[i] = &element->attr.attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 485 | return tab_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | err: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 488 | while (--i >= 0) |
| 489 | kfree(tab_attr[i]); |
| 490 | kfree(tab_attr); |
| 491 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static int add_port(struct ib_device *device, int port_num) |
| 495 | { |
| 496 | struct ib_port *p; |
| 497 | struct ib_port_attr attr; |
| 498 | int i; |
| 499 | int ret; |
| 500 | |
| 501 | ret = ib_query_port(device, port_num, &attr); |
| 502 | if (ret) |
| 503 | return ret; |
| 504 | |
Roland Dreier | de6eb66 | 2005-11-02 07:23:14 -0800 | [diff] [blame] | 505 | p = kzalloc(sizeof *p, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (!p) |
| 507 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
| 509 | p->ibdev = device; |
| 510 | p->port_num = port_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 512 | ret = kobject_init_and_add(&p->kobj, &port_type, |
| 513 | kobject_get(device->ports_parent), |
| 514 | "%d", port_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (ret) |
| 516 | goto err_put; |
| 517 | |
| 518 | ret = sysfs_create_group(&p->kobj, &pma_group); |
| 519 | if (ret) |
| 520 | goto err_put; |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | p->gid_group.name = "gids"; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 523 | p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len); |
| 524 | if (!p->gid_group.attrs) |
| 525 | goto err_remove_pma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | ret = sysfs_create_group(&p->kobj, &p->gid_group); |
| 528 | if (ret) |
| 529 | goto err_free_gid; |
| 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | p->pkey_group.name = "pkeys"; |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 532 | p->pkey_group.attrs = alloc_group_attrs(show_port_pkey, |
| 533 | attr.pkey_tbl_len); |
| 534 | if (!p->pkey_group.attrs) |
| 535 | goto err_remove_gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | ret = sysfs_create_group(&p->kobj, &p->pkey_group); |
| 538 | if (ret) |
| 539 | goto err_free_pkey; |
| 540 | |
| 541 | list_add_tail(&p->kobj.entry, &device->port_list); |
| 542 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 543 | kobject_uevent(&p->kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | return 0; |
| 545 | |
| 546 | err_free_pkey: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 547 | for (i = 0; i < attr.pkey_tbl_len; ++i) |
| 548 | kfree(p->pkey_group.attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 550 | kfree(p->pkey_group.attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
| 552 | err_remove_gid: |
| 553 | sysfs_remove_group(&p->kobj, &p->gid_group); |
| 554 | |
| 555 | err_free_gid: |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 556 | for (i = 0; i < attr.gid_tbl_len; ++i) |
| 557 | kfree(p->gid_group.attrs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Dmitry Torokhov | d48593b | 2005-04-29 00:58:46 -0500 | [diff] [blame] | 559 | kfree(p->gid_group.attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | err_remove_pma: |
| 562 | sysfs_remove_group(&p->kobj, &pma_group); |
| 563 | |
| 564 | err_put: |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 565 | kobject_put(device->ports_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | kfree(p); |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | static ssize_t show_node_type(struct class_device *cdev, char *buf) |
| 571 | { |
| 572 | struct ib_device *dev = container_of(cdev, struct ib_device, class_dev); |
| 573 | |
Roland Dreier | ba8e931 | 2005-10-18 14:14:56 -0700 | [diff] [blame] | 574 | if (!ibdev_is_alive(dev)) |
| 575 | return -ENODEV; |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | switch (dev->node_type) { |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 578 | case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type); |
| 579 | case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type); |
| 580 | case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type); |
| 581 | case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type); |
| 582 | default: return sprintf(buf, "%d: <unknown>\n", dev->node_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
| 586 | static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf) |
| 587 | { |
| 588 | struct ib_device *dev = container_of(cdev, struct ib_device, class_dev); |
| 589 | struct ib_device_attr attr; |
| 590 | ssize_t ret; |
| 591 | |
Roland Dreier | ba8e931 | 2005-10-18 14:14:56 -0700 | [diff] [blame] | 592 | if (!ibdev_is_alive(dev)) |
| 593 | return -ENODEV; |
| 594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | ret = ib_query_device(dev, &attr); |
| 596 | if (ret) |
| 597 | return ret; |
| 598 | |
| 599 | return sprintf(buf, "%04x:%04x:%04x:%04x\n", |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 600 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]), |
| 601 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]), |
| 602 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]), |
| 603 | be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static ssize_t show_node_guid(struct class_device *cdev, char *buf) |
| 607 | { |
| 608 | struct ib_device *dev = container_of(cdev, struct ib_device, class_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Roland Dreier | ba8e931 | 2005-10-18 14:14:56 -0700 | [diff] [blame] | 610 | if (!ibdev_is_alive(dev)) |
| 611 | return -ENODEV; |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | return sprintf(buf, "%04x:%04x:%04x:%04x\n", |
Sean Hefty | cf311cd | 2006-01-10 07:39:34 -0800 | [diff] [blame] | 614 | be16_to_cpu(((__be16 *) &dev->node_guid)[0]), |
| 615 | be16_to_cpu(((__be16 *) &dev->node_guid)[1]), |
| 616 | be16_to_cpu(((__be16 *) &dev->node_guid)[2]), |
| 617 | be16_to_cpu(((__be16 *) &dev->node_guid)[3])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 620 | static ssize_t show_node_desc(struct class_device *cdev, char *buf) |
| 621 | { |
| 622 | struct ib_device *dev = container_of(cdev, struct ib_device, class_dev); |
| 623 | |
| 624 | return sprintf(buf, "%.64s\n", dev->node_desc); |
| 625 | } |
| 626 | |
| 627 | static ssize_t set_node_desc(struct class_device *cdev, const char *buf, |
| 628 | size_t count) |
| 629 | { |
| 630 | struct ib_device *dev = container_of(cdev, struct ib_device, class_dev); |
| 631 | struct ib_device_modify desc = {}; |
| 632 | int ret; |
| 633 | |
| 634 | if (!dev->modify_device) |
| 635 | return -EIO; |
| 636 | |
| 637 | memcpy(desc.node_desc, buf, min_t(int, count, 64)); |
| 638 | ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc); |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | |
| 642 | return count; |
| 643 | } |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | static CLASS_DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL); |
| 646 | static CLASS_DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL); |
| 647 | static CLASS_DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL); |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 648 | static CLASS_DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, |
| 649 | set_node_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
| 651 | static struct class_device_attribute *ib_class_attributes[] = { |
| 652 | &class_device_attr_node_type, |
| 653 | &class_device_attr_sys_image_guid, |
Roland Dreier | c5bcbbb | 2006-02-02 09:47:14 -0800 | [diff] [blame] | 654 | &class_device_attr_node_guid, |
| 655 | &class_device_attr_node_desc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | }; |
| 657 | |
| 658 | static struct class ib_class = { |
| 659 | .name = "infiniband", |
| 660 | .release = ib_device_release, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 661 | .uevent = ib_device_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | int ib_device_register_sysfs(struct ib_device *device) |
| 665 | { |
| 666 | struct class_device *class_dev = &device->class_dev; |
| 667 | int ret; |
| 668 | int i; |
| 669 | |
| 670 | class_dev->class = &ib_class; |
| 671 | class_dev->class_data = device; |
Joachim Fenkes | 1912ffb | 2007-04-23 18:20:27 +0200 | [diff] [blame] | 672 | class_dev->dev = device->dma_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | strlcpy(class_dev->class_id, device->name, BUS_ID_SIZE); |
| 674 | |
| 675 | INIT_LIST_HEAD(&device->port_list); |
| 676 | |
| 677 | ret = class_device_register(class_dev); |
| 678 | if (ret) |
| 679 | goto err; |
| 680 | |
| 681 | for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) { |
| 682 | ret = class_device_create_file(class_dev, ib_class_attributes[i]); |
| 683 | if (ret) |
| 684 | goto err_unregister; |
| 685 | } |
| 686 | |
Greg Kroah-Hartman | 35be068 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 687 | device->ports_parent = kobject_create_and_add("ports", |
| 688 | kobject_get(&class_dev->kobj)); |
Li Zefan | c7482b8 | 2008-02-15 10:24:49 +0800 | [diff] [blame^] | 689 | if (!device->ports_parent) { |
| 690 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | goto err_put; |
Li Zefan | c7482b8 | 2008-02-15 10:24:49 +0800 | [diff] [blame^] | 692 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 694 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | ret = add_port(device, 0); |
| 696 | if (ret) |
| 697 | goto err_put; |
| 698 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | for (i = 1; i <= device->phys_port_cnt; ++i) { |
| 700 | ret = add_port(device, i); |
| 701 | if (ret) |
| 702 | goto err_put; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | |
| 708 | err_put: |
| 709 | { |
| 710 | struct kobject *p, *t; |
| 711 | struct ib_port *port; |
| 712 | |
| 713 | list_for_each_entry_safe(p, t, &device->port_list, entry) { |
| 714 | list_del(&p->entry); |
| 715 | port = container_of(p, struct ib_port, kobj); |
| 716 | sysfs_remove_group(p, &pma_group); |
| 717 | sysfs_remove_group(p, &port->pkey_group); |
| 718 | sysfs_remove_group(p, &port->gid_group); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 719 | kobject_put(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | kobject_put(&class_dev->kobj); |
| 724 | |
| 725 | err_unregister: |
| 726 | class_device_unregister(class_dev); |
| 727 | |
| 728 | err: |
| 729 | return ret; |
| 730 | } |
| 731 | |
| 732 | void ib_device_unregister_sysfs(struct ib_device *device) |
| 733 | { |
| 734 | struct kobject *p, *t; |
| 735 | struct ib_port *port; |
| 736 | |
| 737 | list_for_each_entry_safe(p, t, &device->port_list, entry) { |
| 738 | list_del(&p->entry); |
| 739 | port = container_of(p, struct ib_port, kobj); |
| 740 | sysfs_remove_group(p, &pma_group); |
| 741 | sysfs_remove_group(p, &port->pkey_group); |
| 742 | sysfs_remove_group(p, &port->gid_group); |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 743 | kobject_put(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Greg Kroah-Hartman | c10997f | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 746 | kobject_put(device->ports_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | class_device_unregister(&device->class_dev); |
| 748 | } |
| 749 | |
| 750 | int ib_sysfs_setup(void) |
| 751 | { |
| 752 | return class_register(&ib_class); |
| 753 | } |
| 754 | |
| 755 | void ib_sysfs_cleanup(void) |
| 756 | { |
| 757 | class_unregister(&ib_class); |
| 758 | } |