1 /* 2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved. 4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 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: user_mad.c 5596 2006-03-03 01:00:07Z sean.hefty $ 35 */ 36 37 #include <linux/module.h> 38 #include <linux/init.h> 39 #include <linux/device.h> 40 #include <linux/err.h> 41 #include <linux/fs.h> 42 #include <linux/cdev.h> 43 #include <linux/pci.h> 44 #include <linux/dma-mapping.h> 45 #include <linux/poll.h> 46 #include <linux/rwsem.h> 47 #include <linux/kref.h> 48 49 #include <asm/uaccess.h> 50 #include <asm/semaphore.h> 51 52 #include <rdma/ib_mad.h> 53 #include <rdma/ib_user_mad.h> 54 55 MODULE_AUTHOR("Roland Dreier"); 56 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access"); 57 MODULE_LICENSE("Dual BSD/GPL"); 58 59 enum { 60 IB_UMAD_MAX_PORTS = 64, 61 IB_UMAD_MAX_AGENTS = 32, 62 63 IB_UMAD_MAJOR = 231, 64 IB_UMAD_MINOR_BASE = 0 65 }; 66 67 /* 68 * Our lifetime rules for these structs are the following: each time a 69 * device special file is opened, we look up the corresponding struct 70 * ib_umad_port by minor in the umad_port[] table while holding the 71 * port_lock. If this lookup succeeds, we take a reference on the 72 * ib_umad_port's struct ib_umad_device while still holding the 73 * port_lock; if the lookup fails, we fail the open(). We drop these 74 * references in the corresponding close(). 75 * 76 * In addition to references coming from open character devices, there 77 * is one more reference to each ib_umad_device representing the 78 * module's reference taken when allocating the ib_umad_device in 79 * ib_umad_add_one(). 80 * 81 * When destroying an ib_umad_device, we clear all of its 82 * ib_umad_ports from umad_port[] while holding port_lock before 83 * dropping the module's reference to the ib_umad_device. This is 84 * always safe because any open() calls will either succeed and obtain 85 * a reference before we clear the umad_port[] entries, or fail after 86 * we clear the umad_port[] entries. 87 */ 88 89 struct ib_umad_port { 90 struct cdev *dev; 91 struct class_device *class_dev; 92 93 struct cdev *sm_dev; 94 struct class_device *sm_class_dev; 95 struct semaphore sm_sem; 96 97 struct rw_semaphore mutex; 98 struct list_head file_list; 99 100 struct ib_device *ib_dev; 101 struct ib_umad_device *umad_dev; 102 int dev_num; 103 u8 port_num; 104 }; 105 106 struct ib_umad_device { 107 int start_port, end_port; 108 struct kref ref; 109 struct ib_umad_port port[0]; 110 }; 111 112 struct ib_umad_file { 113 struct ib_umad_port *port; 114 struct list_head recv_list; 115 struct list_head port_list; 116 spinlock_t recv_lock; 117 wait_queue_head_t recv_wait; 118 struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS]; 119 int agents_dead; 120 }; 121 122 struct ib_umad_packet { 123 struct ib_mad_send_buf *msg; 124 struct ib_mad_recv_wc *recv_wc; 125 struct list_head list; 126 int length; 127 struct ib_user_mad mad; 128 }; 129 130 static struct class *umad_class; 131 132 static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE); 133 134 static DEFINE_SPINLOCK(port_lock); 135 static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS]; 136 static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2); 137 138 static void ib_umad_add_one(struct ib_device *device); 139 static void ib_umad_remove_one(struct ib_device *device); 140 141 static void ib_umad_release_dev(struct kref *ref) 142 { 143 struct ib_umad_device *dev = 144 container_of(ref, struct ib_umad_device, ref); 145 146 kfree(dev); 147 } 148 149 /* caller must hold port->mutex at least for reading */ 150 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id) 151 { 152 return file->agents_dead ? NULL : file->agent[id]; 153 } 154 155 static int queue_packet(struct ib_umad_file *file, 156 struct ib_mad_agent *agent, 157 struct ib_umad_packet *packet) 158 { 159 int ret = 1; 160 161 down_read(&file->port->mutex); 162 163 for (packet->mad.hdr.id = 0; 164 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS; 165 packet->mad.hdr.id++) 166 if (agent == __get_agent(file, packet->mad.hdr.id)) { 167 spin_lock_irq(&file->recv_lock); 168 list_add_tail(&packet->list, &file->recv_list); 169 spin_unlock_irq(&file->recv_lock); 170 wake_up_interruptible(&file->recv_wait); 171 ret = 0; 172 break; 173 } 174 175 up_read(&file->port->mutex); 176 177 return ret; 178 } 179 180 static void send_handler(struct ib_mad_agent *agent, 181 struct ib_mad_send_wc *send_wc) 182 { 183 struct ib_umad_file *file = agent->context; 184 struct ib_umad_packet *packet = send_wc->send_buf->context[0]; 185 186 ib_destroy_ah(packet->msg->ah); 187 ib_free_send_mad(packet->msg); 188 189 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) { 190 packet->length = IB_MGMT_MAD_HDR; 191 packet->mad.hdr.status = ETIMEDOUT; 192 if (!queue_packet(file, agent, packet)) 193 return; 194 } 195 kfree(packet); 196 } 197 198 static void recv_handler(struct ib_mad_agent *agent, 199 struct ib_mad_recv_wc *mad_recv_wc) 200 { 201 struct ib_umad_file *file = agent->context; 202 struct ib_umad_packet *packet; 203 204 if (mad_recv_wc->wc->status != IB_WC_SUCCESS) 205 goto err1; 206 207 packet = kzalloc(sizeof *packet, GFP_KERNEL); 208 if (!packet) 209 goto err1; 210 211 packet->length = mad_recv_wc->mad_len; 212 packet->recv_wc = mad_recv_wc; 213 214 packet->mad.hdr.status = 0; 215 packet->mad.hdr.length = sizeof (struct ib_user_mad) + 216 mad_recv_wc->mad_len; 217 packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp); 218 packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid); 219 packet->mad.hdr.sl = mad_recv_wc->wc->sl; 220 packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits; 221 packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH); 222 if (packet->mad.hdr.grh_present) { 223 /* XXX parse GRH */ 224 packet->mad.hdr.gid_index = 0; 225 packet->mad.hdr.hop_limit = 0; 226 packet->mad.hdr.traffic_class = 0; 227 memset(packet->mad.hdr.gid, 0, 16); 228 packet->mad.hdr.flow_label = 0; 229 } 230 231 if (queue_packet(file, agent, packet)) 232 goto err2; 233 return; 234 235 err2: 236 kfree(packet); 237 err1: 238 ib_free_recv_mad(mad_recv_wc); 239 } 240 241 static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet, 242 size_t count) 243 { 244 struct ib_mad_recv_buf *recv_buf; 245 int left, seg_payload, offset, max_seg_payload; 246 247 /* We need enough room to copy the first (or only) MAD segment. */ 248 recv_buf = &packet->recv_wc->recv_buf; 249 if ((packet->length <= sizeof (*recv_buf->mad) && 250 count < sizeof (packet->mad) + packet->length) || 251 (packet->length > sizeof (*recv_buf->mad) && 252 count < sizeof (packet->mad) + sizeof (*recv_buf->mad))) 253 return -EINVAL; 254 255 if (copy_to_user(buf, &packet->mad, sizeof (packet->mad))) 256 return -EFAULT; 257 258 buf += sizeof (packet->mad); 259 seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad)); 260 if (copy_to_user(buf, recv_buf->mad, seg_payload)) 261 return -EFAULT; 262 263 if (seg_payload < packet->length) { 264 /* 265 * Multipacket RMPP MAD message. Copy remainder of message. 266 * Note that last segment may have a shorter payload. 267 */ 268 if (count < sizeof (packet->mad) + packet->length) { 269 /* 270 * The buffer is too small, return the first RMPP segment, 271 * which includes the RMPP message length. 272 */ 273 return -ENOSPC; 274 } 275 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class); 276 max_seg_payload = sizeof (struct ib_mad) - offset; 277 278 for (left = packet->length - seg_payload, buf += seg_payload; 279 left; left -= seg_payload, buf += seg_payload) { 280 recv_buf = container_of(recv_buf->list.next, 281 struct ib_mad_recv_buf, list); 282 seg_payload = min(left, max_seg_payload); 283 if (copy_to_user(buf, ((void *) recv_buf->mad) + offset, 284 seg_payload)) 285 return -EFAULT; 286 } 287 } 288 return sizeof (packet->mad) + packet->length; 289 } 290 291 static ssize_t copy_send_mad(char __user *buf, struct ib_umad_packet *packet, 292 size_t count) 293 { 294 ssize_t size = sizeof (packet->mad) + packet->length; 295 296 if (count < size) 297 return -EINVAL; 298 299 if (copy_to_user(buf, &packet->mad, size)) 300 return -EFAULT; 301 302 return size; 303 } 304 305 static ssize_t ib_umad_read(struct file *filp, char __user *buf, 306 size_t count, loff_t *pos) 307 { 308 struct ib_umad_file *file = filp->private_data; 309 struct ib_umad_packet *packet; 310 ssize_t ret; 311 312 if (count < sizeof (struct ib_user_mad)) 313 return -EINVAL; 314 315 spin_lock_irq(&file->recv_lock); 316 317 while (list_empty(&file->recv_list)) { 318 spin_unlock_irq(&file->recv_lock); 319 320 if (filp->f_flags & O_NONBLOCK) 321 return -EAGAIN; 322 323 if (wait_event_interruptible(file->recv_wait, 324 !list_empty(&file->recv_list))) 325 return -ERESTARTSYS; 326 327 spin_lock_irq(&file->recv_lock); 328 } 329 330 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list); 331 list_del(&packet->list); 332 333 spin_unlock_irq(&file->recv_lock); 334 335 if (packet->recv_wc) 336 ret = copy_recv_mad(buf, packet, count); 337 else 338 ret = copy_send_mad(buf, packet, count); 339 340 if (ret < 0) { 341 /* Requeue packet */ 342 spin_lock_irq(&file->recv_lock); 343 list_add(&packet->list, &file->recv_list); 344 spin_unlock_irq(&file->recv_lock); 345 } else { 346 if (packet->recv_wc) 347 ib_free_recv_mad(packet->recv_wc); 348 kfree(packet); 349 } 350 return ret; 351 } 352 353 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf) 354 { 355 int left, seg; 356 357 /* Copy class specific header */ 358 if ((msg->hdr_len > IB_MGMT_RMPP_HDR) && 359 copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR, 360 msg->hdr_len - IB_MGMT_RMPP_HDR)) 361 return -EFAULT; 362 363 /* All headers are in place. Copy data segments. */ 364 for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0; 365 seg++, left -= msg->seg_size, buf += msg->seg_size) { 366 if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf, 367 min(left, msg->seg_size))) 368 return -EFAULT; 369 } 370 return 0; 371 } 372 373 static ssize_t ib_umad_write(struct file *filp, const char __user *buf, 374 size_t count, loff_t *pos) 375 { 376 struct ib_umad_file *file = filp->private_data; 377 struct ib_umad_packet *packet; 378 struct ib_mad_agent *agent; 379 struct ib_ah_attr ah_attr; 380 struct ib_ah *ah; 381 struct ib_rmpp_mad *rmpp_mad; 382 u8 method; 383 __be64 *tid; 384 int ret, data_len, hdr_len, copy_offset, rmpp_active; 385 386 if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR) 387 return -EINVAL; 388 389 packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL); 390 if (!packet) 391 return -ENOMEM; 392 393 if (copy_from_user(&packet->mad, buf, 394 sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) { 395 ret = -EFAULT; 396 goto err; 397 } 398 399 if (packet->mad.hdr.id < 0 || 400 packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) { 401 ret = -EINVAL; 402 goto err; 403 } 404 405 down_read(&file->port->mutex); 406 407 agent = __get_agent(file, packet->mad.hdr.id); 408 if (!agent) { 409 ret = -EINVAL; 410 goto err_up; 411 } 412 413 memset(&ah_attr, 0, sizeof ah_attr); 414 ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid); 415 ah_attr.sl = packet->mad.hdr.sl; 416 ah_attr.src_path_bits = packet->mad.hdr.path_bits; 417 ah_attr.port_num = file->port->port_num; 418 if (packet->mad.hdr.grh_present) { 419 ah_attr.ah_flags = IB_AH_GRH; 420 memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16); 421 ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label); 422 ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit; 423 ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class; 424 } 425 426 ah = ib_create_ah(agent->qp->pd, &ah_attr); 427 if (IS_ERR(ah)) { 428 ret = PTR_ERR(ah); 429 goto err_up; 430 } 431 432 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data; 433 hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class); 434 if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) { 435 copy_offset = IB_MGMT_MAD_HDR; 436 rmpp_active = 0; 437 } else { 438 copy_offset = IB_MGMT_RMPP_HDR; 439 rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & 440 IB_MGMT_RMPP_FLAG_ACTIVE; 441 } 442 443 data_len = count - sizeof (struct ib_user_mad) - hdr_len; 444 packet->msg = ib_create_send_mad(agent, 445 be32_to_cpu(packet->mad.hdr.qpn), 446 0, rmpp_active, hdr_len, 447 data_len, GFP_KERNEL); 448 if (IS_ERR(packet->msg)) { 449 ret = PTR_ERR(packet->msg); 450 goto err_ah; 451 } 452 453 packet->msg->ah = ah; 454 packet->msg->timeout_ms = packet->mad.hdr.timeout_ms; 455 packet->msg->retries = packet->mad.hdr.retries; 456 packet->msg->context[0] = packet; 457 458 /* Copy MAD header. Any RMPP header is already in place. */ 459 memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR); 460 buf += sizeof (struct ib_user_mad); 461 462 if (!rmpp_active) { 463 if (copy_from_user(packet->msg->mad + copy_offset, 464 buf + copy_offset, 465 hdr_len + data_len - copy_offset)) { 466 ret = -EFAULT; 467 goto err_msg; 468 } 469 } else { 470 ret = copy_rmpp_mad(packet->msg, buf); 471 if (ret) 472 goto err_msg; 473 } 474 475 /* 476 * If userspace is generating a request that will generate a 477 * response, we need to make sure the high-order part of the 478 * transaction ID matches the agent being used to send the 479 * MAD. 480 */ 481 method = ((struct ib_mad_hdr *) packet->msg->mad)->method; 482 483 if (!(method & IB_MGMT_METHOD_RESP) && 484 method != IB_MGMT_METHOD_TRAP_REPRESS && 485 method != IB_MGMT_METHOD_SEND) { 486 tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid; 487 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 | 488 (be64_to_cpup(tid) & 0xffffffff)); 489 } 490 491 ret = ib_post_send_mad(packet->msg, NULL); 492 if (ret) 493 goto err_msg; 494 495 up_read(&file->port->mutex); 496 return count; 497 498 err_msg: 499 ib_free_send_mad(packet->msg); 500 err_ah: 501 ib_destroy_ah(ah); 502 err_up: 503 up_read(&file->port->mutex); 504 err: 505 kfree(packet); 506 return ret; 507 } 508 509 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait) 510 { 511 struct ib_umad_file *file = filp->private_data; 512 513 /* we will always be able to post a MAD send */ 514 unsigned int mask = POLLOUT | POLLWRNORM; 515 516 poll_wait(filp, &file->recv_wait, wait); 517 518 if (!list_empty(&file->recv_list)) 519 mask |= POLLIN | POLLRDNORM; 520 521 return mask; 522 } 523 524 static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg) 525 { 526 struct ib_user_mad_reg_req ureq; 527 struct ib_mad_reg_req req; 528 struct ib_mad_agent *agent; 529 int agent_id; 530 int ret; 531 532 down_write(&file->port->mutex); 533 534 if (!file->port->ib_dev) { 535 ret = -EPIPE; 536 goto out; 537 } 538 539 if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) { 540 ret = -EFAULT; 541 goto out; 542 } 543 544 if (ureq.qpn != 0 && ureq.qpn != 1) { 545 ret = -EINVAL; 546 goto out; 547 } 548 549 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id) 550 if (!__get_agent(file, agent_id)) 551 goto found; 552 553 ret = -ENOMEM; 554 goto out; 555 556 found: 557 if (ureq.mgmt_class) { 558 req.mgmt_class = ureq.mgmt_class; 559 req.mgmt_class_version = ureq.mgmt_class_version; 560 memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask); 561 memcpy(req.oui, ureq.oui, sizeof req.oui); 562 } 563 564 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num, 565 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI, 566 ureq.mgmt_class ? &req : NULL, 567 ureq.rmpp_version, 568 send_handler, recv_handler, file); 569 if (IS_ERR(agent)) { 570 ret = PTR_ERR(agent); 571 goto out; 572 } 573 574 if (put_user(agent_id, 575 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) { 576 ret = -EFAULT; 577 ib_unregister_mad_agent(agent); 578 goto out; 579 } 580 581 file->agent[agent_id] = agent; 582 ret = 0; 583 584 out: 585 up_write(&file->port->mutex); 586 return ret; 587 } 588 589 static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg) 590 { 591 struct ib_mad_agent *agent = NULL; 592 u32 id; 593 int ret = 0; 594 595 if (get_user(id, (u32 __user *) arg)) 596 return -EFAULT; 597 598 down_write(&file->port->mutex); 599 600 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) { 601 ret = -EINVAL; 602 goto out; 603 } 604 605 agent = file->agent[id]; 606 file->agent[id] = NULL; 607 608 out: 609 up_write(&file->port->mutex); 610 611 if (agent) 612 ib_unregister_mad_agent(agent); 613 614 return ret; 615 } 616 617 static long ib_umad_ioctl(struct file *filp, unsigned int cmd, 618 unsigned long arg) 619 { 620 switch (cmd) { 621 case IB_USER_MAD_REGISTER_AGENT: 622 return ib_umad_reg_agent(filp->private_data, arg); 623 case IB_USER_MAD_UNREGISTER_AGENT: 624 return ib_umad_unreg_agent(filp->private_data, arg); 625 default: 626 return -ENOIOCTLCMD; 627 } 628 } 629 630 static int ib_umad_open(struct inode *inode, struct file *filp) 631 { 632 struct ib_umad_port *port; 633 struct ib_umad_file *file; 634 int ret = 0; 635 636 spin_lock(&port_lock); 637 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE]; 638 if (port) 639 kref_get(&port->umad_dev->ref); 640 spin_unlock(&port_lock); 641 642 if (!port) 643 return -ENXIO; 644 645 down_write(&port->mutex); 646 647 if (!port->ib_dev) { 648 ret = -ENXIO; 649 goto out; 650 } 651 652 file = kzalloc(sizeof *file, GFP_KERNEL); 653 if (!file) { 654 kref_put(&port->umad_dev->ref, ib_umad_release_dev); 655 ret = -ENOMEM; 656 goto out; 657 } 658 659 spin_lock_init(&file->recv_lock); 660 INIT_LIST_HEAD(&file->recv_list); 661 init_waitqueue_head(&file->recv_wait); 662 663 file->port = port; 664 filp->private_data = file; 665 666 list_add_tail(&file->port_list, &port->file_list); 667 668 out: 669 up_write(&port->mutex); 670 return ret; 671 } 672 673 static int ib_umad_close(struct inode *inode, struct file *filp) 674 { 675 struct ib_umad_file *file = filp->private_data; 676 struct ib_umad_device *dev = file->port->umad_dev; 677 struct ib_umad_packet *packet, *tmp; 678 int already_dead; 679 int i; 680 681 down_write(&file->port->mutex); 682 683 already_dead = file->agents_dead; 684 file->agents_dead = 1; 685 686 list_for_each_entry_safe(packet, tmp, &file->recv_list, list) { 687 if (packet->recv_wc) 688 ib_free_recv_mad(packet->recv_wc); 689 kfree(packet); 690 } 691 692 list_del(&file->port_list); 693 694 downgrade_write(&file->port->mutex); 695 696 if (!already_dead) 697 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) 698 if (file->agent[i]) 699 ib_unregister_mad_agent(file->agent[i]); 700 701 up_read(&file->port->mutex); 702 703 kfree(file); 704 kref_put(&dev->ref, ib_umad_release_dev); 705 706 return 0; 707 } 708 709 static struct file_operations umad_fops = { 710 .owner = THIS_MODULE, 711 .read = ib_umad_read, 712 .write = ib_umad_write, 713 .poll = ib_umad_poll, 714 .unlocked_ioctl = ib_umad_ioctl, 715 .compat_ioctl = ib_umad_ioctl, 716 .open = ib_umad_open, 717 .release = ib_umad_close 718 }; 719 720 static int ib_umad_sm_open(struct inode *inode, struct file *filp) 721 { 722 struct ib_umad_port *port; 723 struct ib_port_modify props = { 724 .set_port_cap_mask = IB_PORT_SM 725 }; 726 int ret; 727 728 spin_lock(&port_lock); 729 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS]; 730 if (port) 731 kref_get(&port->umad_dev->ref); 732 spin_unlock(&port_lock); 733 734 if (!port) 735 return -ENXIO; 736 737 if (filp->f_flags & O_NONBLOCK) { 738 if (down_trylock(&port->sm_sem)) { 739 ret = -EAGAIN; 740 goto fail; 741 } 742 } else { 743 if (down_interruptible(&port->sm_sem)) { 744 ret = -ERESTARTSYS; 745 goto fail; 746 } 747 } 748 749 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); 750 if (ret) { 751 up(&port->sm_sem); 752 goto fail; 753 } 754 755 filp->private_data = port; 756 757 return 0; 758 759 fail: 760 kref_put(&port->umad_dev->ref, ib_umad_release_dev); 761 return ret; 762 } 763 764 static int ib_umad_sm_close(struct inode *inode, struct file *filp) 765 { 766 struct ib_umad_port *port = filp->private_data; 767 struct ib_port_modify props = { 768 .clr_port_cap_mask = IB_PORT_SM 769 }; 770 int ret = 0; 771 772 down_write(&port->mutex); 773 if (port->ib_dev) 774 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); 775 up_write(&port->mutex); 776 777 up(&port->sm_sem); 778 779 kref_put(&port->umad_dev->ref, ib_umad_release_dev); 780 781 return ret; 782 } 783 784 static struct file_operations umad_sm_fops = { 785 .owner = THIS_MODULE, 786 .open = ib_umad_sm_open, 787 .release = ib_umad_sm_close 788 }; 789 790 static struct ib_client umad_client = { 791 .name = "umad", 792 .add = ib_umad_add_one, 793 .remove = ib_umad_remove_one 794 }; 795 796 static ssize_t show_ibdev(struct class_device *class_dev, char *buf) 797 { 798 struct ib_umad_port *port = class_get_devdata(class_dev); 799 800 if (!port) 801 return -ENODEV; 802 803 return sprintf(buf, "%s\n", port->ib_dev->name); 804 } 805 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 806 807 static ssize_t show_port(struct class_device *class_dev, char *buf) 808 { 809 struct ib_umad_port *port = class_get_devdata(class_dev); 810 811 if (!port) 812 return -ENODEV; 813 814 return sprintf(buf, "%d\n", port->port_num); 815 } 816 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL); 817 818 static ssize_t show_abi_version(struct class *class, char *buf) 819 { 820 return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION); 821 } 822 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); 823 824 static int ib_umad_init_port(struct ib_device *device, int port_num, 825 struct ib_umad_port *port) 826 { 827 spin_lock(&port_lock); 828 port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS); 829 if (port->dev_num >= IB_UMAD_MAX_PORTS) { 830 spin_unlock(&port_lock); 831 return -1; 832 } 833 set_bit(port->dev_num, dev_map); 834 spin_unlock(&port_lock); 835 836 port->ib_dev = device; 837 port->port_num = port_num; 838 init_MUTEX(&port->sm_sem); 839 init_rwsem(&port->mutex); 840 INIT_LIST_HEAD(&port->file_list); 841 842 port->dev = cdev_alloc(); 843 if (!port->dev) 844 return -1; 845 port->dev->owner = THIS_MODULE; 846 port->dev->ops = &umad_fops; 847 kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num); 848 if (cdev_add(port->dev, base_dev + port->dev_num, 1)) 849 goto err_cdev; 850 851 port->class_dev = class_device_create(umad_class, NULL, port->dev->dev, 852 device->dma_device, 853 "umad%d", port->dev_num); 854 if (IS_ERR(port->class_dev)) 855 goto err_cdev; 856 857 if (class_device_create_file(port->class_dev, &class_device_attr_ibdev)) 858 goto err_class; 859 if (class_device_create_file(port->class_dev, &class_device_attr_port)) 860 goto err_class; 861 862 port->sm_dev = cdev_alloc(); 863 if (!port->sm_dev) 864 goto err_class; 865 port->sm_dev->owner = THIS_MODULE; 866 port->sm_dev->ops = &umad_sm_fops; 867 kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num); 868 if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1)) 869 goto err_sm_cdev; 870 871 port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev, 872 device->dma_device, 873 "issm%d", port->dev_num); 874 if (IS_ERR(port->sm_class_dev)) 875 goto err_sm_cdev; 876 877 class_set_devdata(port->class_dev, port); 878 class_set_devdata(port->sm_class_dev, port); 879 880 if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev)) 881 goto err_sm_class; 882 if (class_device_create_file(port->sm_class_dev, &class_device_attr_port)) 883 goto err_sm_class; 884 885 spin_lock(&port_lock); 886 umad_port[port->dev_num] = port; 887 spin_unlock(&port_lock); 888 889 return 0; 890 891 err_sm_class: 892 class_device_destroy(umad_class, port->sm_dev->dev); 893 894 err_sm_cdev: 895 cdev_del(port->sm_dev); 896 897 err_class: 898 class_device_destroy(umad_class, port->dev->dev); 899 900 err_cdev: 901 cdev_del(port->dev); 902 clear_bit(port->dev_num, dev_map); 903 904 return -1; 905 } 906 907 static void ib_umad_kill_port(struct ib_umad_port *port) 908 { 909 struct ib_umad_file *file; 910 int id; 911 912 class_set_devdata(port->class_dev, NULL); 913 class_set_devdata(port->sm_class_dev, NULL); 914 915 class_device_destroy(umad_class, port->dev->dev); 916 class_device_destroy(umad_class, port->sm_dev->dev); 917 918 cdev_del(port->dev); 919 cdev_del(port->sm_dev); 920 921 spin_lock(&port_lock); 922 umad_port[port->dev_num] = NULL; 923 spin_unlock(&port_lock); 924 925 down_write(&port->mutex); 926 927 port->ib_dev = NULL; 928 929 /* 930 * Now go through the list of files attached to this port and 931 * unregister all of their MAD agents. We need to hold 932 * port->mutex while doing this to avoid racing with 933 * ib_umad_close(), but we can't hold the mutex for writing 934 * while calling ib_unregister_mad_agent(), since that might 935 * deadlock by calling back into queue_packet(). So we 936 * downgrade our lock to a read lock, and then drop and 937 * reacquire the write lock for the next iteration. 938 * 939 * We do list_del_init() on the file's list_head so that the 940 * list_del in ib_umad_close() is still OK, even after the 941 * file is removed from the list. 942 */ 943 while (!list_empty(&port->file_list)) { 944 file = list_entry(port->file_list.next, struct ib_umad_file, 945 port_list); 946 947 file->agents_dead = 1; 948 list_del_init(&file->port_list); 949 950 downgrade_write(&port->mutex); 951 952 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) 953 if (file->agent[id]) 954 ib_unregister_mad_agent(file->agent[id]); 955 956 up_read(&port->mutex); 957 down_write(&port->mutex); 958 } 959 960 up_write(&port->mutex); 961 962 clear_bit(port->dev_num, dev_map); 963 } 964 965 static void ib_umad_add_one(struct ib_device *device) 966 { 967 struct ib_umad_device *umad_dev; 968 int s, e, i; 969 970 if (device->node_type == IB_NODE_SWITCH) 971 s = e = 0; 972 else { 973 s = 1; 974 e = device->phys_port_cnt; 975 } 976 977 umad_dev = kzalloc(sizeof *umad_dev + 978 (e - s + 1) * sizeof (struct ib_umad_port), 979 GFP_KERNEL); 980 if (!umad_dev) 981 return; 982 983 kref_init(&umad_dev->ref); 984 985 umad_dev->start_port = s; 986 umad_dev->end_port = e; 987 988 for (i = s; i <= e; ++i) { 989 umad_dev->port[i - s].umad_dev = umad_dev; 990 991 if (ib_umad_init_port(device, i, &umad_dev->port[i - s])) 992 goto err; 993 } 994 995 ib_set_client_data(device, &umad_client, umad_dev); 996 997 return; 998 999 err: 1000 while (--i >= s) 1001 ib_umad_kill_port(&umad_dev->port[i - s]); 1002 1003 kref_put(&umad_dev->ref, ib_umad_release_dev); 1004 } 1005 1006 static void ib_umad_remove_one(struct ib_device *device) 1007 { 1008 struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client); 1009 int i; 1010 1011 if (!umad_dev) 1012 return; 1013 1014 for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) 1015 ib_umad_kill_port(&umad_dev->port[i]); 1016 1017 kref_put(&umad_dev->ref, ib_umad_release_dev); 1018 } 1019 1020 static int __init ib_umad_init(void) 1021 { 1022 int ret; 1023 1024 ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2, 1025 "infiniband_mad"); 1026 if (ret) { 1027 printk(KERN_ERR "user_mad: couldn't register device number\n"); 1028 goto out; 1029 } 1030 1031 umad_class = class_create(THIS_MODULE, "infiniband_mad"); 1032 if (IS_ERR(umad_class)) { 1033 ret = PTR_ERR(umad_class); 1034 printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n"); 1035 goto out_chrdev; 1036 } 1037 1038 ret = class_create_file(umad_class, &class_attr_abi_version); 1039 if (ret) { 1040 printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n"); 1041 goto out_class; 1042 } 1043 1044 ret = ib_register_client(&umad_client); 1045 if (ret) { 1046 printk(KERN_ERR "user_mad: couldn't register ib_umad client\n"); 1047 goto out_class; 1048 } 1049 1050 return 0; 1051 1052 out_class: 1053 class_destroy(umad_class); 1054 1055 out_chrdev: 1056 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); 1057 1058 out: 1059 return ret; 1060 } 1061 1062 static void __exit ib_umad_cleanup(void) 1063 { 1064 ib_unregister_client(&umad_client); 1065 class_destroy(umad_class); 1066 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); 1067 } 1068 1069 module_init(ib_umad_init); 1070 module_exit(ib_umad_cleanup); 1071