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