Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | * |
| 32 | * $Id: user_mad.c 1389 2004-12-27 22:56:47Z roland $ |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
| 37 | #include <linux/device.h> |
| 38 | #include <linux/err.h> |
| 39 | #include <linux/fs.h> |
| 40 | #include <linux/cdev.h> |
| 41 | #include <linux/pci.h> |
| 42 | #include <linux/dma-mapping.h> |
| 43 | #include <linux/poll.h> |
| 44 | #include <linux/rwsem.h> |
| 45 | #include <linux/kref.h> |
| 46 | |
| 47 | #include <asm/uaccess.h> |
| 48 | #include <asm/semaphore.h> |
| 49 | |
| 50 | #include <ib_mad.h> |
| 51 | #include <ib_user_mad.h> |
| 52 | |
| 53 | MODULE_AUTHOR("Roland Dreier"); |
| 54 | MODULE_DESCRIPTION("InfiniBand userspace MAD packet access"); |
| 55 | MODULE_LICENSE("Dual BSD/GPL"); |
| 56 | |
| 57 | enum { |
| 58 | IB_UMAD_MAX_PORTS = 64, |
| 59 | IB_UMAD_MAX_AGENTS = 32, |
| 60 | |
| 61 | IB_UMAD_MAJOR = 231, |
| 62 | IB_UMAD_MINOR_BASE = 0 |
| 63 | }; |
| 64 | |
| 65 | struct ib_umad_port { |
| 66 | int devnum; |
| 67 | struct cdev dev; |
| 68 | struct class_device class_dev; |
| 69 | |
| 70 | int sm_devnum; |
| 71 | struct cdev sm_dev; |
| 72 | struct class_device sm_class_dev; |
| 73 | struct semaphore sm_sem; |
| 74 | |
| 75 | struct ib_device *ib_dev; |
| 76 | struct ib_umad_device *umad_dev; |
| 77 | u8 port_num; |
| 78 | }; |
| 79 | |
| 80 | struct ib_umad_device { |
| 81 | int start_port, end_port; |
| 82 | struct kref ref; |
| 83 | struct ib_umad_port port[0]; |
| 84 | }; |
| 85 | |
| 86 | struct ib_umad_file { |
| 87 | struct ib_umad_port *port; |
| 88 | spinlock_t recv_lock; |
| 89 | struct list_head recv_list; |
| 90 | wait_queue_head_t recv_wait; |
| 91 | struct rw_semaphore agent_mutex; |
| 92 | struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS]; |
| 93 | struct ib_mr *mr[IB_UMAD_MAX_AGENTS]; |
| 94 | }; |
| 95 | |
| 96 | struct ib_umad_packet { |
| 97 | struct ib_user_mad mad; |
| 98 | struct ib_ah *ah; |
| 99 | struct list_head list; |
| 100 | DECLARE_PCI_UNMAP_ADDR(mapping) |
| 101 | }; |
| 102 | |
| 103 | static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE); |
| 104 | static spinlock_t map_lock; |
| 105 | static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2); |
| 106 | |
| 107 | static void ib_umad_add_one(struct ib_device *device); |
| 108 | static void ib_umad_remove_one(struct ib_device *device); |
| 109 | |
| 110 | static int queue_packet(struct ib_umad_file *file, |
| 111 | struct ib_mad_agent *agent, |
| 112 | struct ib_umad_packet *packet) |
| 113 | { |
| 114 | int ret = 1; |
| 115 | |
| 116 | down_read(&file->agent_mutex); |
| 117 | for (packet->mad.id = 0; |
| 118 | packet->mad.id < IB_UMAD_MAX_AGENTS; |
| 119 | packet->mad.id++) |
| 120 | if (agent == file->agent[packet->mad.id]) { |
| 121 | spin_lock_irq(&file->recv_lock); |
| 122 | list_add_tail(&packet->list, &file->recv_list); |
| 123 | spin_unlock_irq(&file->recv_lock); |
| 124 | wake_up_interruptible(&file->recv_wait); |
| 125 | ret = 0; |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | up_read(&file->agent_mutex); |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static void send_handler(struct ib_mad_agent *agent, |
| 135 | struct ib_mad_send_wc *send_wc) |
| 136 | { |
| 137 | struct ib_umad_file *file = agent->context; |
| 138 | struct ib_umad_packet *packet = |
| 139 | (void *) (unsigned long) send_wc->wr_id; |
| 140 | |
| 141 | dma_unmap_single(agent->device->dma_device, |
| 142 | pci_unmap_addr(packet, mapping), |
| 143 | sizeof packet->mad.data, |
| 144 | DMA_TO_DEVICE); |
| 145 | ib_destroy_ah(packet->ah); |
| 146 | |
| 147 | if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) { |
| 148 | packet->mad.status = ETIMEDOUT; |
| 149 | |
| 150 | if (!queue_packet(file, agent, packet)) |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | kfree(packet); |
| 155 | } |
| 156 | |
| 157 | static void recv_handler(struct ib_mad_agent *agent, |
| 158 | struct ib_mad_recv_wc *mad_recv_wc) |
| 159 | { |
| 160 | struct ib_umad_file *file = agent->context; |
| 161 | struct ib_umad_packet *packet; |
| 162 | |
| 163 | if (mad_recv_wc->wc->status != IB_WC_SUCCESS) |
| 164 | goto out; |
| 165 | |
| 166 | packet = kmalloc(sizeof *packet, GFP_KERNEL); |
| 167 | if (!packet) |
| 168 | goto out; |
| 169 | |
| 170 | memset(packet, 0, sizeof *packet); |
| 171 | |
| 172 | memcpy(packet->mad.data, mad_recv_wc->recv_buf.mad, sizeof packet->mad.data); |
| 173 | packet->mad.status = 0; |
| 174 | packet->mad.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp); |
| 175 | packet->mad.lid = cpu_to_be16(mad_recv_wc->wc->slid); |
| 176 | packet->mad.sl = mad_recv_wc->wc->sl; |
| 177 | packet->mad.path_bits = mad_recv_wc->wc->dlid_path_bits; |
| 178 | packet->mad.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH); |
| 179 | if (packet->mad.grh_present) { |
| 180 | /* XXX parse GRH */ |
| 181 | packet->mad.gid_index = 0; |
| 182 | packet->mad.hop_limit = 0; |
| 183 | packet->mad.traffic_class = 0; |
| 184 | memset(packet->mad.gid, 0, 16); |
| 185 | packet->mad.flow_label = 0; |
| 186 | } |
| 187 | |
| 188 | if (queue_packet(file, agent, packet)) |
| 189 | kfree(packet); |
| 190 | |
| 191 | out: |
| 192 | ib_free_recv_mad(mad_recv_wc); |
| 193 | } |
| 194 | |
| 195 | static ssize_t ib_umad_read(struct file *filp, char __user *buf, |
| 196 | size_t count, loff_t *pos) |
| 197 | { |
| 198 | struct ib_umad_file *file = filp->private_data; |
| 199 | struct ib_umad_packet *packet; |
| 200 | ssize_t ret; |
| 201 | |
| 202 | if (count < sizeof (struct ib_user_mad)) |
| 203 | return -EINVAL; |
| 204 | |
| 205 | spin_lock_irq(&file->recv_lock); |
| 206 | |
| 207 | while (list_empty(&file->recv_list)) { |
| 208 | spin_unlock_irq(&file->recv_lock); |
| 209 | |
| 210 | if (filp->f_flags & O_NONBLOCK) |
| 211 | return -EAGAIN; |
| 212 | |
| 213 | if (wait_event_interruptible(file->recv_wait, |
| 214 | !list_empty(&file->recv_list))) |
| 215 | return -ERESTARTSYS; |
| 216 | |
| 217 | spin_lock_irq(&file->recv_lock); |
| 218 | } |
| 219 | |
| 220 | packet = list_entry(file->recv_list.next, struct ib_umad_packet, list); |
| 221 | list_del(&packet->list); |
| 222 | |
| 223 | spin_unlock_irq(&file->recv_lock); |
| 224 | |
| 225 | if (copy_to_user(buf, &packet->mad, sizeof packet->mad)) |
| 226 | ret = -EFAULT; |
| 227 | else |
| 228 | ret = sizeof packet->mad; |
| 229 | |
| 230 | kfree(packet); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static ssize_t ib_umad_write(struct file *filp, const char __user *buf, |
| 235 | size_t count, loff_t *pos) |
| 236 | { |
| 237 | struct ib_umad_file *file = filp->private_data; |
| 238 | struct ib_umad_packet *packet; |
| 239 | struct ib_mad_agent *agent; |
| 240 | struct ib_ah_attr ah_attr; |
| 241 | struct ib_sge gather_list; |
| 242 | struct ib_send_wr *bad_wr, wr = { |
| 243 | .opcode = IB_WR_SEND, |
| 244 | .sg_list = &gather_list, |
| 245 | .num_sge = 1, |
| 246 | .send_flags = IB_SEND_SIGNALED, |
| 247 | }; |
| 248 | u8 method; |
| 249 | u64 *tid; |
| 250 | int ret; |
| 251 | |
| 252 | if (count < sizeof (struct ib_user_mad)) |
| 253 | return -EINVAL; |
| 254 | |
| 255 | packet = kmalloc(sizeof *packet, GFP_KERNEL); |
| 256 | if (!packet) |
| 257 | return -ENOMEM; |
| 258 | |
| 259 | if (copy_from_user(&packet->mad, buf, sizeof packet->mad)) { |
| 260 | kfree(packet); |
| 261 | return -EFAULT; |
| 262 | } |
| 263 | |
| 264 | if (packet->mad.id < 0 || packet->mad.id >= IB_UMAD_MAX_AGENTS) { |
| 265 | ret = -EINVAL; |
| 266 | goto err; |
| 267 | } |
| 268 | |
| 269 | down_read(&file->agent_mutex); |
| 270 | |
| 271 | agent = file->agent[packet->mad.id]; |
| 272 | if (!agent) { |
| 273 | ret = -EINVAL; |
| 274 | goto err_up; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * If userspace is generating a request that will generate a |
| 279 | * response, we need to make sure the high-order part of the |
| 280 | * transaction ID matches the agent being used to send the |
| 281 | * MAD. |
| 282 | */ |
| 283 | method = ((struct ib_mad_hdr *) packet->mad.data)->method; |
| 284 | |
| 285 | if (!(method & IB_MGMT_METHOD_RESP) && |
| 286 | method != IB_MGMT_METHOD_TRAP_REPRESS && |
| 287 | method != IB_MGMT_METHOD_SEND) { |
| 288 | tid = &((struct ib_mad_hdr *) packet->mad.data)->tid; |
| 289 | *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 | |
| 290 | (be64_to_cpup(tid) & 0xffffffff)); |
| 291 | } |
| 292 | |
| 293 | memset(&ah_attr, 0, sizeof ah_attr); |
| 294 | ah_attr.dlid = be16_to_cpu(packet->mad.lid); |
| 295 | ah_attr.sl = packet->mad.sl; |
| 296 | ah_attr.src_path_bits = packet->mad.path_bits; |
| 297 | ah_attr.port_num = file->port->port_num; |
| 298 | if (packet->mad.grh_present) { |
| 299 | ah_attr.ah_flags = IB_AH_GRH; |
| 300 | memcpy(ah_attr.grh.dgid.raw, packet->mad.gid, 16); |
| 301 | ah_attr.grh.flow_label = packet->mad.flow_label; |
| 302 | ah_attr.grh.hop_limit = packet->mad.hop_limit; |
| 303 | ah_attr.grh.traffic_class = packet->mad.traffic_class; |
| 304 | } |
| 305 | |
| 306 | packet->ah = ib_create_ah(agent->qp->pd, &ah_attr); |
| 307 | if (IS_ERR(packet->ah)) { |
| 308 | ret = PTR_ERR(packet->ah); |
| 309 | goto err_up; |
| 310 | } |
| 311 | |
| 312 | gather_list.addr = dma_map_single(agent->device->dma_device, |
| 313 | packet->mad.data, |
| 314 | sizeof packet->mad.data, |
| 315 | DMA_TO_DEVICE); |
| 316 | gather_list.length = sizeof packet->mad.data; |
| 317 | gather_list.lkey = file->mr[packet->mad.id]->lkey; |
| 318 | pci_unmap_addr_set(packet, mapping, gather_list.addr); |
| 319 | |
| 320 | wr.wr.ud.mad_hdr = (struct ib_mad_hdr *) packet->mad.data; |
| 321 | wr.wr.ud.ah = packet->ah; |
| 322 | wr.wr.ud.remote_qpn = be32_to_cpu(packet->mad.qpn); |
| 323 | wr.wr.ud.remote_qkey = be32_to_cpu(packet->mad.qkey); |
| 324 | wr.wr.ud.timeout_ms = packet->mad.timeout_ms; |
| 325 | |
| 326 | wr.wr_id = (unsigned long) packet; |
| 327 | |
| 328 | ret = ib_post_send_mad(agent, &wr, &bad_wr); |
| 329 | if (ret) { |
| 330 | dma_unmap_single(agent->device->dma_device, |
| 331 | pci_unmap_addr(packet, mapping), |
| 332 | sizeof packet->mad.data, |
| 333 | DMA_TO_DEVICE); |
| 334 | goto err_up; |
| 335 | } |
| 336 | |
| 337 | up_read(&file->agent_mutex); |
| 338 | |
| 339 | return sizeof packet->mad; |
| 340 | |
| 341 | err_up: |
| 342 | up_read(&file->agent_mutex); |
| 343 | |
| 344 | err: |
| 345 | kfree(packet); |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait) |
| 350 | { |
| 351 | struct ib_umad_file *file = filp->private_data; |
| 352 | |
| 353 | /* we will always be able to post a MAD send */ |
| 354 | unsigned int mask = POLLOUT | POLLWRNORM; |
| 355 | |
| 356 | poll_wait(filp, &file->recv_wait, wait); |
| 357 | |
| 358 | if (!list_empty(&file->recv_list)) |
| 359 | mask |= POLLIN | POLLRDNORM; |
| 360 | |
| 361 | return mask; |
| 362 | } |
| 363 | |
| 364 | static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg) |
| 365 | { |
| 366 | struct ib_user_mad_reg_req ureq; |
| 367 | struct ib_mad_reg_req req; |
| 368 | struct ib_mad_agent *agent; |
| 369 | int agent_id; |
| 370 | int ret; |
| 371 | |
| 372 | down_write(&file->agent_mutex); |
| 373 | |
| 374 | if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) { |
| 375 | ret = -EFAULT; |
| 376 | goto out; |
| 377 | } |
| 378 | |
| 379 | if (ureq.qpn != 0 && ureq.qpn != 1) { |
| 380 | ret = -EINVAL; |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id) |
| 385 | if (!file->agent[agent_id]) |
| 386 | goto found; |
| 387 | |
| 388 | ret = -ENOMEM; |
| 389 | goto out; |
| 390 | |
| 391 | found: |
| 392 | req.mgmt_class = ureq.mgmt_class; |
| 393 | req.mgmt_class_version = ureq.mgmt_class_version; |
| 394 | memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask); |
| 395 | memcpy(req.oui, ureq.oui, sizeof req.oui); |
| 396 | |
| 397 | agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num, |
| 398 | ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI, |
| 399 | &req, 0, send_handler, recv_handler, |
| 400 | file); |
| 401 | if (IS_ERR(agent)) { |
| 402 | ret = PTR_ERR(agent); |
| 403 | goto out; |
| 404 | } |
| 405 | |
| 406 | file->agent[agent_id] = agent; |
| 407 | |
| 408 | file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE); |
| 409 | if (IS_ERR(file->mr[agent_id])) { |
| 410 | ret = -ENOMEM; |
| 411 | goto err; |
| 412 | } |
| 413 | |
| 414 | if (put_user(agent_id, |
| 415 | (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) { |
| 416 | ret = -EFAULT; |
| 417 | goto err_mr; |
| 418 | } |
| 419 | |
| 420 | ret = 0; |
| 421 | goto out; |
| 422 | |
| 423 | err_mr: |
| 424 | ib_dereg_mr(file->mr[agent_id]); |
| 425 | |
| 426 | err: |
| 427 | file->agent[agent_id] = NULL; |
| 428 | ib_unregister_mad_agent(agent); |
| 429 | |
| 430 | out: |
| 431 | up_write(&file->agent_mutex); |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg) |
| 436 | { |
| 437 | u32 id; |
| 438 | int ret = 0; |
| 439 | |
| 440 | down_write(&file->agent_mutex); |
| 441 | |
| 442 | if (get_user(id, (u32 __user *) arg)) { |
| 443 | ret = -EFAULT; |
| 444 | goto out; |
| 445 | } |
| 446 | |
| 447 | if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) { |
| 448 | ret = -EINVAL; |
| 449 | goto out; |
| 450 | } |
| 451 | |
| 452 | ib_dereg_mr(file->mr[id]); |
| 453 | ib_unregister_mad_agent(file->agent[id]); |
| 454 | file->agent[id] = NULL; |
| 455 | |
| 456 | out: |
| 457 | up_write(&file->agent_mutex); |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | static long ib_umad_ioctl(struct file *filp, |
| 462 | unsigned int cmd, unsigned long arg) |
| 463 | { |
| 464 | switch (cmd) { |
| 465 | case IB_USER_MAD_REGISTER_AGENT: |
| 466 | return ib_umad_reg_agent(filp->private_data, arg); |
| 467 | case IB_USER_MAD_UNREGISTER_AGENT: |
| 468 | return ib_umad_unreg_agent(filp->private_data, arg); |
| 469 | default: |
| 470 | return -ENOIOCTLCMD; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | static int ib_umad_open(struct inode *inode, struct file *filp) |
| 475 | { |
| 476 | struct ib_umad_port *port = |
| 477 | container_of(inode->i_cdev, struct ib_umad_port, dev); |
| 478 | struct ib_umad_file *file; |
| 479 | |
| 480 | file = kmalloc(sizeof *file, GFP_KERNEL); |
| 481 | if (!file) |
| 482 | return -ENOMEM; |
| 483 | |
| 484 | memset(file, 0, sizeof *file); |
| 485 | |
| 486 | spin_lock_init(&file->recv_lock); |
| 487 | init_rwsem(&file->agent_mutex); |
| 488 | INIT_LIST_HEAD(&file->recv_list); |
| 489 | init_waitqueue_head(&file->recv_wait); |
| 490 | |
| 491 | file->port = port; |
| 492 | filp->private_data = file; |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | static int ib_umad_close(struct inode *inode, struct file *filp) |
| 498 | { |
| 499 | struct ib_umad_file *file = filp->private_data; |
| 500 | int i; |
| 501 | |
| 502 | for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) |
| 503 | if (file->agent[i]) { |
| 504 | ib_dereg_mr(file->mr[i]); |
| 505 | ib_unregister_mad_agent(file->agent[i]); |
| 506 | } |
| 507 | |
| 508 | kfree(file); |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static struct file_operations umad_fops = { |
| 514 | .owner = THIS_MODULE, |
| 515 | .read = ib_umad_read, |
| 516 | .write = ib_umad_write, |
| 517 | .poll = ib_umad_poll, |
| 518 | .unlocked_ioctl = ib_umad_ioctl, |
| 519 | .compat_ioctl = ib_umad_ioctl, |
| 520 | .open = ib_umad_open, |
| 521 | .release = ib_umad_close |
| 522 | }; |
| 523 | |
| 524 | static int ib_umad_sm_open(struct inode *inode, struct file *filp) |
| 525 | { |
| 526 | struct ib_umad_port *port = |
| 527 | container_of(inode->i_cdev, struct ib_umad_port, sm_dev); |
| 528 | struct ib_port_modify props = { |
| 529 | .set_port_cap_mask = IB_PORT_SM |
| 530 | }; |
| 531 | int ret; |
| 532 | |
| 533 | if (filp->f_flags & O_NONBLOCK) { |
| 534 | if (down_trylock(&port->sm_sem)) |
| 535 | return -EAGAIN; |
| 536 | } else { |
| 537 | if (down_interruptible(&port->sm_sem)) |
| 538 | return -ERESTARTSYS; |
| 539 | } |
| 540 | |
| 541 | ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); |
| 542 | if (ret) { |
| 543 | up(&port->sm_sem); |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | filp->private_data = port; |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | static int ib_umad_sm_close(struct inode *inode, struct file *filp) |
| 553 | { |
| 554 | struct ib_umad_port *port = filp->private_data; |
| 555 | struct ib_port_modify props = { |
| 556 | .clr_port_cap_mask = IB_PORT_SM |
| 557 | }; |
| 558 | int ret; |
| 559 | |
| 560 | ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); |
| 561 | up(&port->sm_sem); |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | static struct file_operations umad_sm_fops = { |
| 567 | .owner = THIS_MODULE, |
| 568 | .open = ib_umad_sm_open, |
| 569 | .release = ib_umad_sm_close |
| 570 | }; |
| 571 | |
| 572 | static struct ib_client umad_client = { |
| 573 | .name = "umad", |
| 574 | .add = ib_umad_add_one, |
| 575 | .remove = ib_umad_remove_one |
| 576 | }; |
| 577 | |
| 578 | static ssize_t show_dev(struct class_device *class_dev, char *buf) |
| 579 | { |
| 580 | struct ib_umad_port *port = class_get_devdata(class_dev); |
| 581 | |
| 582 | if (class_dev == &port->class_dev) |
| 583 | return print_dev_t(buf, port->dev.dev); |
| 584 | else |
| 585 | return print_dev_t(buf, port->sm_dev.dev); |
| 586 | } |
| 587 | static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL); |
| 588 | |
| 589 | static ssize_t show_ibdev(struct class_device *class_dev, char *buf) |
| 590 | { |
| 591 | struct ib_umad_port *port = class_get_devdata(class_dev); |
| 592 | |
| 593 | return sprintf(buf, "%s\n", port->ib_dev->name); |
| 594 | } |
| 595 | static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
| 596 | |
| 597 | static ssize_t show_port(struct class_device *class_dev, char *buf) |
| 598 | { |
| 599 | struct ib_umad_port *port = class_get_devdata(class_dev); |
| 600 | |
| 601 | return sprintf(buf, "%d\n", port->port_num); |
| 602 | } |
| 603 | static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL); |
| 604 | |
| 605 | static void ib_umad_release_dev(struct kref *ref) |
| 606 | { |
| 607 | struct ib_umad_device *dev = |
| 608 | container_of(ref, struct ib_umad_device, ref); |
| 609 | |
| 610 | kfree(dev); |
| 611 | } |
| 612 | |
| 613 | static void ib_umad_release_port(struct class_device *class_dev) |
| 614 | { |
| 615 | struct ib_umad_port *port = class_get_devdata(class_dev); |
| 616 | |
| 617 | if (class_dev == &port->class_dev) { |
| 618 | cdev_del(&port->dev); |
| 619 | clear_bit(port->devnum, dev_map); |
| 620 | } else { |
| 621 | cdev_del(&port->sm_dev); |
| 622 | clear_bit(port->sm_devnum, dev_map); |
| 623 | } |
| 624 | |
| 625 | kref_put(&port->umad_dev->ref, ib_umad_release_dev); |
| 626 | } |
| 627 | |
| 628 | static struct class umad_class = { |
| 629 | .name = "infiniband_mad", |
| 630 | .release = ib_umad_release_port |
| 631 | }; |
| 632 | |
| 633 | static ssize_t show_abi_version(struct class *class, char *buf) |
| 634 | { |
| 635 | return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION); |
| 636 | } |
| 637 | static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); |
| 638 | |
| 639 | static int ib_umad_init_port(struct ib_device *device, int port_num, |
| 640 | struct ib_umad_port *port) |
| 641 | { |
| 642 | spin_lock(&map_lock); |
| 643 | port->devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS); |
| 644 | if (port->devnum >= IB_UMAD_MAX_PORTS) { |
| 645 | spin_unlock(&map_lock); |
| 646 | return -1; |
| 647 | } |
| 648 | port->sm_devnum = find_next_zero_bit(dev_map, IB_UMAD_MAX_PORTS * 2, IB_UMAD_MAX_PORTS); |
| 649 | if (port->sm_devnum >= IB_UMAD_MAX_PORTS * 2) { |
| 650 | spin_unlock(&map_lock); |
| 651 | return -1; |
| 652 | } |
| 653 | set_bit(port->devnum, dev_map); |
| 654 | set_bit(port->sm_devnum, dev_map); |
| 655 | spin_unlock(&map_lock); |
| 656 | |
| 657 | port->ib_dev = device; |
| 658 | port->port_num = port_num; |
| 659 | init_MUTEX(&port->sm_sem); |
| 660 | |
| 661 | cdev_init(&port->dev, &umad_fops); |
| 662 | port->dev.owner = THIS_MODULE; |
| 663 | kobject_set_name(&port->dev.kobj, "umad%d", port->devnum); |
| 664 | if (cdev_add(&port->dev, base_dev + port->devnum, 1)) |
| 665 | return -1; |
| 666 | |
| 667 | port->class_dev.class = &umad_class; |
| 668 | port->class_dev.dev = device->dma_device; |
| 669 | |
| 670 | snprintf(port->class_dev.class_id, BUS_ID_SIZE, "umad%d", port->devnum); |
| 671 | |
| 672 | if (class_device_register(&port->class_dev)) |
| 673 | goto err_cdev; |
| 674 | |
| 675 | class_set_devdata(&port->class_dev, port); |
| 676 | kref_get(&port->umad_dev->ref); |
| 677 | |
| 678 | if (class_device_create_file(&port->class_dev, &class_device_attr_dev)) |
| 679 | goto err_class; |
| 680 | if (class_device_create_file(&port->class_dev, &class_device_attr_ibdev)) |
| 681 | goto err_class; |
| 682 | if (class_device_create_file(&port->class_dev, &class_device_attr_port)) |
| 683 | goto err_class; |
| 684 | |
| 685 | cdev_init(&port->sm_dev, &umad_sm_fops); |
| 686 | port->sm_dev.owner = THIS_MODULE; |
| 687 | kobject_set_name(&port->dev.kobj, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS); |
| 688 | if (cdev_add(&port->sm_dev, base_dev + port->sm_devnum, 1)) |
| 689 | return -1; |
| 690 | |
| 691 | port->sm_class_dev.class = &umad_class; |
| 692 | port->sm_class_dev.dev = device->dma_device; |
| 693 | |
| 694 | snprintf(port->sm_class_dev.class_id, BUS_ID_SIZE, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS); |
| 695 | |
| 696 | if (class_device_register(&port->sm_class_dev)) |
| 697 | goto err_sm_cdev; |
| 698 | |
| 699 | class_set_devdata(&port->sm_class_dev, port); |
| 700 | kref_get(&port->umad_dev->ref); |
| 701 | |
| 702 | if (class_device_create_file(&port->sm_class_dev, &class_device_attr_dev)) |
| 703 | goto err_sm_class; |
| 704 | if (class_device_create_file(&port->sm_class_dev, &class_device_attr_ibdev)) |
| 705 | goto err_sm_class; |
| 706 | if (class_device_create_file(&port->sm_class_dev, &class_device_attr_port)) |
| 707 | goto err_sm_class; |
| 708 | |
| 709 | return 0; |
| 710 | |
| 711 | err_sm_class: |
| 712 | class_device_unregister(&port->sm_class_dev); |
| 713 | |
| 714 | err_sm_cdev: |
| 715 | cdev_del(&port->sm_dev); |
| 716 | |
| 717 | err_class: |
| 718 | class_device_unregister(&port->class_dev); |
| 719 | |
| 720 | err_cdev: |
| 721 | cdev_del(&port->dev); |
| 722 | clear_bit(port->devnum, dev_map); |
| 723 | |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | static void ib_umad_add_one(struct ib_device *device) |
| 728 | { |
| 729 | struct ib_umad_device *umad_dev; |
| 730 | int s, e, i; |
| 731 | |
| 732 | if (device->node_type == IB_NODE_SWITCH) |
| 733 | s = e = 0; |
| 734 | else { |
| 735 | s = 1; |
| 736 | e = device->phys_port_cnt; |
| 737 | } |
| 738 | |
| 739 | umad_dev = kmalloc(sizeof *umad_dev + |
| 740 | (e - s + 1) * sizeof (struct ib_umad_port), |
| 741 | GFP_KERNEL); |
| 742 | if (!umad_dev) |
| 743 | return; |
| 744 | |
| 745 | memset(umad_dev, 0, sizeof *umad_dev + |
| 746 | (e - s + 1) * sizeof (struct ib_umad_port)); |
| 747 | |
| 748 | kref_init(&umad_dev->ref); |
| 749 | |
| 750 | umad_dev->start_port = s; |
| 751 | umad_dev->end_port = e; |
| 752 | |
| 753 | for (i = s; i <= e; ++i) { |
| 754 | umad_dev->port[i - s].umad_dev = umad_dev; |
| 755 | |
| 756 | if (ib_umad_init_port(device, i, &umad_dev->port[i - s])) |
| 757 | goto err; |
| 758 | } |
| 759 | |
| 760 | ib_set_client_data(device, &umad_client, umad_dev); |
| 761 | |
| 762 | return; |
| 763 | |
| 764 | err: |
| 765 | while (--i >= s) { |
| 766 | class_device_unregister(&umad_dev->port[i - s].class_dev); |
| 767 | class_device_unregister(&umad_dev->port[i - s].sm_class_dev); |
| 768 | } |
| 769 | |
| 770 | kref_put(&umad_dev->ref, ib_umad_release_dev); |
| 771 | } |
| 772 | |
| 773 | static void ib_umad_remove_one(struct ib_device *device) |
| 774 | { |
| 775 | struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client); |
| 776 | int i; |
| 777 | |
| 778 | if (!umad_dev) |
| 779 | return; |
| 780 | |
| 781 | for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) { |
| 782 | class_device_unregister(&umad_dev->port[i].class_dev); |
| 783 | class_device_unregister(&umad_dev->port[i].sm_class_dev); |
| 784 | } |
| 785 | |
| 786 | kref_put(&umad_dev->ref, ib_umad_release_dev); |
| 787 | } |
| 788 | |
| 789 | static int __init ib_umad_init(void) |
| 790 | { |
| 791 | int ret; |
| 792 | |
| 793 | spin_lock_init(&map_lock); |
| 794 | |
| 795 | ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2, |
| 796 | "infiniband_mad"); |
| 797 | if (ret) { |
| 798 | printk(KERN_ERR "user_mad: couldn't register device number\n"); |
| 799 | goto out; |
| 800 | } |
| 801 | |
| 802 | ret = class_register(&umad_class); |
| 803 | if (ret) { |
| 804 | printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n"); |
| 805 | goto out_chrdev; |
| 806 | } |
| 807 | |
| 808 | ret = class_create_file(&umad_class, &class_attr_abi_version); |
| 809 | if (ret) { |
| 810 | printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n"); |
| 811 | goto out_class; |
| 812 | } |
| 813 | |
| 814 | ret = ib_register_client(&umad_client); |
| 815 | if (ret) { |
| 816 | printk(KERN_ERR "user_mad: couldn't register ib_umad client\n"); |
| 817 | goto out_class; |
| 818 | } |
| 819 | |
| 820 | return 0; |
| 821 | |
| 822 | out_class: |
| 823 | class_unregister(&umad_class); |
| 824 | |
| 825 | out_chrdev: |
| 826 | unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); |
| 827 | |
| 828 | out: |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | static void __exit ib_umad_cleanup(void) |
| 833 | { |
| 834 | ib_unregister_client(&umad_client); |
| 835 | class_unregister(&umad_class); |
| 836 | unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); |
| 837 | } |
| 838 | |
| 839 | module_init(ib_umad_init); |
| 840 | module_exit(ib_umad_cleanup); |