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 * Copyright (c) 2008 Cisco. All rights reserved. 6 * 7 * This software is available to you under a choice of one of two 8 * licenses. You may choose to be licensed under the terms of the GNU 9 * General Public License (GPL) Version 2, available from the file 10 * COPYING in the main directory of this source tree, or the 11 * OpenIB.org BSD license below: 12 * 13 * Redistribution and use in source and binary forms, with or 14 * without modification, are permitted provided that the following 15 * conditions are met: 16 * 17 * - Redistributions of source code must retain the above 18 * copyright notice, this list of conditions and the following 19 * disclaimer. 20 * 21 * - Redistributions in binary form must reproduce the above 22 * copyright notice, this list of conditions and the following 23 * disclaimer in the documentation and/or other materials 24 * provided with the distribution. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 33 * SOFTWARE. 34 */ 35 36 #include <linux/module.h> 37 #include <linux/init.h> 38 #include <linux/device.h> 39 #include <linux/err.h> 40 #include <linux/fs.h> 41 #include <linux/cdev.h> 42 #include <linux/dma-mapping.h> 43 #include <linux/poll.h> 44 #include <linux/mutex.h> 45 #include <linux/kref.h> 46 #include <linux/compat.h> 47 #include <linux/semaphore.h> 48 49 #include <asm/uaccess.h> 50 51 #include <rdma/ib_mad.h> 52 #include <rdma/ib_user_mad.h> 53 54 MODULE_AUTHOR("Roland Dreier"); 55 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access"); 56 MODULE_LICENSE("Dual BSD/GPL"); 57 58 enum { 59 IB_UMAD_MAX_PORTS = 64, 60 IB_UMAD_MAX_AGENTS = 32, 61 62 IB_UMAD_MAJOR = 231, 63 IB_UMAD_MINOR_BASE = 0 64 }; 65 66 /* 67 * Our lifetime rules for these structs are the following: each time a 68 * device special file is opened, we look up the corresponding struct 69 * ib_umad_port by minor in the umad_port[] table while holding the 70 * port_lock. If this lookup succeeds, we take a reference on the 71 * ib_umad_port's struct ib_umad_device while still holding the 72 * port_lock; if the lookup fails, we fail the open(). We drop these 73 * references in the corresponding close(). 74 * 75 * In addition to references coming from open character devices, there 76 * is one more reference to each ib_umad_device representing the 77 * module's reference taken when allocating the ib_umad_device in 78 * ib_umad_add_one(). 79 * 80 * When destroying an ib_umad_device, we clear all of its 81 * ib_umad_ports from umad_port[] while holding port_lock before 82 * dropping the module's reference to the ib_umad_device. This is 83 * always safe because any open() calls will either succeed and obtain 84 * a reference before we clear the umad_port[] entries, or fail after 85 * we clear the umad_port[] entries. 86 */ 87 88 struct ib_umad_port { 89 struct cdev *cdev; 90 struct device *dev; 91 92 struct cdev *sm_cdev; 93 struct device *sm_dev; 94 struct semaphore sm_sem; 95 96 struct mutex file_mutex; 97 struct list_head file_list; 98 99 struct ib_device *ib_dev; 100 struct ib_umad_device *umad_dev; 101 int dev_num; 102 u8 port_num; 103 }; 104 105 struct ib_umad_device { 106 int start_port, end_port; 107 struct kref ref; 108 struct ib_umad_port port[0]; 109 }; 110 111 struct ib_umad_file { 112 struct mutex mutex; 113 struct ib_umad_port *port; 114 struct list_head recv_list; 115 struct list_head send_list; 116 struct list_head port_list; 117 spinlock_t send_lock; 118 wait_queue_head_t recv_wait; 119 struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS]; 120 int agents_dead; 121 u8 use_pkey_index; 122 u8 already_used; 123 }; 124 125 struct ib_umad_packet { 126 struct ib_mad_send_buf *msg; 127 struct ib_mad_recv_wc *recv_wc; 128 struct list_head list; 129 int length; 130 struct ib_user_mad mad; 131 }; 132 133 static struct class *umad_class; 134 135 static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE); 136 137 static DEFINE_SPINLOCK(port_lock); 138 static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS]; 139 static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS); 140 141 static void ib_umad_add_one(struct ib_device *device); 142 static void ib_umad_remove_one(struct ib_device *device); 143 144 static void ib_umad_release_dev(struct kref *ref) 145 { 146 struct ib_umad_device *dev = 147 container_of(ref, struct ib_umad_device, ref); 148 149 kfree(dev); 150 } 151 152 static int hdr_size(struct ib_umad_file *file) 153 { 154 return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) : 155 sizeof (struct ib_user_mad_hdr_old); 156 } 157 158 /* caller must hold file->mutex */ 159 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id) 160 { 161 return file->agents_dead ? NULL : file->agent[id]; 162 } 163 164 static int queue_packet(struct ib_umad_file *file, 165 struct ib_mad_agent *agent, 166 struct ib_umad_packet *packet) 167 { 168 int ret = 1; 169 170 mutex_lock(&file->mutex); 171 172 for (packet->mad.hdr.id = 0; 173 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS; 174 packet->mad.hdr.id++) 175 if (agent == __get_agent(file, packet->mad.hdr.id)) { 176 list_add_tail(&packet->list, &file->recv_list); 177 wake_up_interruptible(&file->recv_wait); 178 ret = 0; 179 break; 180 } 181 182 mutex_unlock(&file->mutex); 183 184 return ret; 185 } 186 187 static void dequeue_send(struct ib_umad_file *file, 188 struct ib_umad_packet *packet) 189 { 190 spin_lock_irq(&file->send_lock); 191 list_del(&packet->list); 192 spin_unlock_irq(&file->send_lock); 193 } 194 195 static void send_handler(struct ib_mad_agent *agent, 196 struct ib_mad_send_wc *send_wc) 197 { 198 struct ib_umad_file *file = agent->context; 199 struct ib_umad_packet *packet = send_wc->send_buf->context[0]; 200 201 dequeue_send(file, packet); 202 ib_destroy_ah(packet->msg->ah); 203 ib_free_send_mad(packet->msg); 204 205 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) { 206 packet->length = IB_MGMT_MAD_HDR; 207 packet->mad.hdr.status = ETIMEDOUT; 208 if (!queue_packet(file, agent, packet)) 209 return; 210 } 211 kfree(packet); 212 } 213 214 static void recv_handler(struct ib_mad_agent *agent, 215 struct ib_mad_recv_wc *mad_recv_wc) 216 { 217 struct ib_umad_file *file = agent->context; 218 struct ib_umad_packet *packet; 219 220 if (mad_recv_wc->wc->status != IB_WC_SUCCESS) 221 goto err1; 222 223 packet = kzalloc(sizeof *packet, GFP_KERNEL); 224 if (!packet) 225 goto err1; 226 227 packet->length = mad_recv_wc->mad_len; 228 packet->recv_wc = mad_recv_wc; 229 230 packet->mad.hdr.status = 0; 231 packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len; 232 packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp); 233 packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid); 234 packet->mad.hdr.sl = mad_recv_wc->wc->sl; 235 packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits; 236 packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index; 237 packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH); 238 if (packet->mad.hdr.grh_present) { 239 struct ib_ah_attr ah_attr; 240 241 ib_init_ah_from_wc(agent->device, agent->port_num, 242 mad_recv_wc->wc, mad_recv_wc->recv_buf.grh, 243 &ah_attr); 244 245 packet->mad.hdr.gid_index = ah_attr.grh.sgid_index; 246 packet->mad.hdr.hop_limit = ah_attr.grh.hop_limit; 247 packet->mad.hdr.traffic_class = ah_attr.grh.traffic_class; 248 memcpy(packet->mad.hdr.gid, &ah_attr.grh.dgid, 16); 249 packet->mad.hdr.flow_label = cpu_to_be32(ah_attr.grh.flow_label); 250 } 251 252 if (queue_packet(file, agent, packet)) 253 goto err2; 254 return; 255 256 err2: 257 kfree(packet); 258 err1: 259 ib_free_recv_mad(mad_recv_wc); 260 } 261 262 static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf, 263 struct ib_umad_packet *packet, size_t count) 264 { 265 struct ib_mad_recv_buf *recv_buf; 266 int left, seg_payload, offset, max_seg_payload; 267 268 /* We need enough room to copy the first (or only) MAD segment. */ 269 recv_buf = &packet->recv_wc->recv_buf; 270 if ((packet->length <= sizeof (*recv_buf->mad) && 271 count < hdr_size(file) + packet->length) || 272 (packet->length > sizeof (*recv_buf->mad) && 273 count < hdr_size(file) + sizeof (*recv_buf->mad))) 274 return -EINVAL; 275 276 if (copy_to_user(buf, &packet->mad, hdr_size(file))) 277 return -EFAULT; 278 279 buf += hdr_size(file); 280 seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad)); 281 if (copy_to_user(buf, recv_buf->mad, seg_payload)) 282 return -EFAULT; 283 284 if (seg_payload < packet->length) { 285 /* 286 * Multipacket RMPP MAD message. Copy remainder of message. 287 * Note that last segment may have a shorter payload. 288 */ 289 if (count < hdr_size(file) + packet->length) { 290 /* 291 * The buffer is too small, return the first RMPP segment, 292 * which includes the RMPP message length. 293 */ 294 return -ENOSPC; 295 } 296 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class); 297 max_seg_payload = sizeof (struct ib_mad) - offset; 298 299 for (left = packet->length - seg_payload, buf += seg_payload; 300 left; left -= seg_payload, buf += seg_payload) { 301 recv_buf = container_of(recv_buf->list.next, 302 struct ib_mad_recv_buf, list); 303 seg_payload = min(left, max_seg_payload); 304 if (copy_to_user(buf, ((void *) recv_buf->mad) + offset, 305 seg_payload)) 306 return -EFAULT; 307 } 308 } 309 return hdr_size(file) + packet->length; 310 } 311 312 static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf, 313 struct ib_umad_packet *packet, size_t count) 314 { 315 ssize_t size = hdr_size(file) + packet->length; 316 317 if (count < size) 318 return -EINVAL; 319 320 if (copy_to_user(buf, &packet->mad, hdr_size(file))) 321 return -EFAULT; 322 323 buf += hdr_size(file); 324 325 if (copy_to_user(buf, packet->mad.data, packet->length)) 326 return -EFAULT; 327 328 return size; 329 } 330 331 static ssize_t ib_umad_read(struct file *filp, char __user *buf, 332 size_t count, loff_t *pos) 333 { 334 struct ib_umad_file *file = filp->private_data; 335 struct ib_umad_packet *packet; 336 ssize_t ret; 337 338 if (count < hdr_size(file)) 339 return -EINVAL; 340 341 mutex_lock(&file->mutex); 342 343 while (list_empty(&file->recv_list)) { 344 mutex_unlock(&file->mutex); 345 346 if (filp->f_flags & O_NONBLOCK) 347 return -EAGAIN; 348 349 if (wait_event_interruptible(file->recv_wait, 350 !list_empty(&file->recv_list))) 351 return -ERESTARTSYS; 352 353 mutex_lock(&file->mutex); 354 } 355 356 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list); 357 list_del(&packet->list); 358 359 mutex_unlock(&file->mutex); 360 361 if (packet->recv_wc) 362 ret = copy_recv_mad(file, buf, packet, count); 363 else 364 ret = copy_send_mad(file, buf, packet, count); 365 366 if (ret < 0) { 367 /* Requeue packet */ 368 mutex_lock(&file->mutex); 369 list_add(&packet->list, &file->recv_list); 370 mutex_unlock(&file->mutex); 371 } else { 372 if (packet->recv_wc) 373 ib_free_recv_mad(packet->recv_wc); 374 kfree(packet); 375 } 376 return ret; 377 } 378 379 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf) 380 { 381 int left, seg; 382 383 /* Copy class specific header */ 384 if ((msg->hdr_len > IB_MGMT_RMPP_HDR) && 385 copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR, 386 msg->hdr_len - IB_MGMT_RMPP_HDR)) 387 return -EFAULT; 388 389 /* All headers are in place. Copy data segments. */ 390 for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0; 391 seg++, left -= msg->seg_size, buf += msg->seg_size) { 392 if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf, 393 min(left, msg->seg_size))) 394 return -EFAULT; 395 } 396 return 0; 397 } 398 399 static int same_destination(struct ib_user_mad_hdr *hdr1, 400 struct ib_user_mad_hdr *hdr2) 401 { 402 if (!hdr1->grh_present && !hdr2->grh_present) 403 return (hdr1->lid == hdr2->lid); 404 405 if (hdr1->grh_present && hdr2->grh_present) 406 return !memcmp(hdr1->gid, hdr2->gid, 16); 407 408 return 0; 409 } 410 411 static int is_duplicate(struct ib_umad_file *file, 412 struct ib_umad_packet *packet) 413 { 414 struct ib_umad_packet *sent_packet; 415 struct ib_mad_hdr *sent_hdr, *hdr; 416 417 hdr = (struct ib_mad_hdr *) packet->mad.data; 418 list_for_each_entry(sent_packet, &file->send_list, list) { 419 sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data; 420 421 if ((hdr->tid != sent_hdr->tid) || 422 (hdr->mgmt_class != sent_hdr->mgmt_class)) 423 continue; 424 425 /* 426 * No need to be overly clever here. If two new operations have 427 * the same TID, reject the second as a duplicate. This is more 428 * restrictive than required by the spec. 429 */ 430 if (!ib_response_mad((struct ib_mad *) hdr)) { 431 if (!ib_response_mad((struct ib_mad *) sent_hdr)) 432 return 1; 433 continue; 434 } else if (!ib_response_mad((struct ib_mad *) sent_hdr)) 435 continue; 436 437 if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr)) 438 return 1; 439 } 440 441 return 0; 442 } 443 444 static ssize_t ib_umad_write(struct file *filp, const char __user *buf, 445 size_t count, loff_t *pos) 446 { 447 struct ib_umad_file *file = filp->private_data; 448 struct ib_umad_packet *packet; 449 struct ib_mad_agent *agent; 450 struct ib_ah_attr ah_attr; 451 struct ib_ah *ah; 452 struct ib_rmpp_mad *rmpp_mad; 453 __be64 *tid; 454 int ret, data_len, hdr_len, copy_offset, rmpp_active; 455 456 if (count < hdr_size(file) + IB_MGMT_RMPP_HDR) 457 return -EINVAL; 458 459 packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL); 460 if (!packet) 461 return -ENOMEM; 462 463 if (copy_from_user(&packet->mad, buf, hdr_size(file))) { 464 ret = -EFAULT; 465 goto err; 466 } 467 468 if (packet->mad.hdr.id < 0 || 469 packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) { 470 ret = -EINVAL; 471 goto err; 472 } 473 474 buf += hdr_size(file); 475 476 if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) { 477 ret = -EFAULT; 478 goto err; 479 } 480 481 mutex_lock(&file->mutex); 482 483 agent = __get_agent(file, packet->mad.hdr.id); 484 if (!agent) { 485 ret = -EINVAL; 486 goto err_up; 487 } 488 489 memset(&ah_attr, 0, sizeof ah_attr); 490 ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid); 491 ah_attr.sl = packet->mad.hdr.sl; 492 ah_attr.src_path_bits = packet->mad.hdr.path_bits; 493 ah_attr.port_num = file->port->port_num; 494 if (packet->mad.hdr.grh_present) { 495 ah_attr.ah_flags = IB_AH_GRH; 496 memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16); 497 ah_attr.grh.sgid_index = packet->mad.hdr.gid_index; 498 ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label); 499 ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit; 500 ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class; 501 } 502 503 ah = ib_create_ah(agent->qp->pd, &ah_attr); 504 if (IS_ERR(ah)) { 505 ret = PTR_ERR(ah); 506 goto err_up; 507 } 508 509 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data; 510 hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class); 511 if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) { 512 copy_offset = IB_MGMT_MAD_HDR; 513 rmpp_active = 0; 514 } else { 515 copy_offset = IB_MGMT_RMPP_HDR; 516 rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & 517 IB_MGMT_RMPP_FLAG_ACTIVE; 518 } 519 520 data_len = count - hdr_size(file) - hdr_len; 521 packet->msg = ib_create_send_mad(agent, 522 be32_to_cpu(packet->mad.hdr.qpn), 523 packet->mad.hdr.pkey_index, rmpp_active, 524 hdr_len, data_len, GFP_KERNEL); 525 if (IS_ERR(packet->msg)) { 526 ret = PTR_ERR(packet->msg); 527 goto err_ah; 528 } 529 530 packet->msg->ah = ah; 531 packet->msg->timeout_ms = packet->mad.hdr.timeout_ms; 532 packet->msg->retries = packet->mad.hdr.retries; 533 packet->msg->context[0] = packet; 534 535 /* Copy MAD header. Any RMPP header is already in place. */ 536 memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR); 537 538 if (!rmpp_active) { 539 if (copy_from_user(packet->msg->mad + copy_offset, 540 buf + copy_offset, 541 hdr_len + data_len - copy_offset)) { 542 ret = -EFAULT; 543 goto err_msg; 544 } 545 } else { 546 ret = copy_rmpp_mad(packet->msg, buf); 547 if (ret) 548 goto err_msg; 549 } 550 551 /* 552 * Set the high-order part of the transaction ID to make MADs from 553 * different agents unique, and allow routing responses back to the 554 * original requestor. 555 */ 556 if (!ib_response_mad(packet->msg->mad)) { 557 tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid; 558 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 | 559 (be64_to_cpup(tid) & 0xffffffff)); 560 rmpp_mad->mad_hdr.tid = *tid; 561 } 562 563 spin_lock_irq(&file->send_lock); 564 ret = is_duplicate(file, packet); 565 if (!ret) 566 list_add_tail(&packet->list, &file->send_list); 567 spin_unlock_irq(&file->send_lock); 568 if (ret) { 569 ret = -EINVAL; 570 goto err_msg; 571 } 572 573 ret = ib_post_send_mad(packet->msg, NULL); 574 if (ret) 575 goto err_send; 576 577 mutex_unlock(&file->mutex); 578 return count; 579 580 err_send: 581 dequeue_send(file, packet); 582 err_msg: 583 ib_free_send_mad(packet->msg); 584 err_ah: 585 ib_destroy_ah(ah); 586 err_up: 587 mutex_unlock(&file->mutex); 588 err: 589 kfree(packet); 590 return ret; 591 } 592 593 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait) 594 { 595 struct ib_umad_file *file = filp->private_data; 596 597 /* we will always be able to post a MAD send */ 598 unsigned int mask = POLLOUT | POLLWRNORM; 599 600 poll_wait(filp, &file->recv_wait, wait); 601 602 if (!list_empty(&file->recv_list)) 603 mask |= POLLIN | POLLRDNORM; 604 605 return mask; 606 } 607 608 static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg, 609 int compat_method_mask) 610 { 611 struct ib_user_mad_reg_req ureq; 612 struct ib_mad_reg_req req; 613 struct ib_mad_agent *agent = NULL; 614 int agent_id; 615 int ret; 616 617 mutex_lock(&file->port->file_mutex); 618 mutex_lock(&file->mutex); 619 620 if (!file->port->ib_dev) { 621 ret = -EPIPE; 622 goto out; 623 } 624 625 if (copy_from_user(&ureq, arg, sizeof ureq)) { 626 ret = -EFAULT; 627 goto out; 628 } 629 630 if (ureq.qpn != 0 && ureq.qpn != 1) { 631 ret = -EINVAL; 632 goto out; 633 } 634 635 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id) 636 if (!__get_agent(file, agent_id)) 637 goto found; 638 639 ret = -ENOMEM; 640 goto out; 641 642 found: 643 if (ureq.mgmt_class) { 644 req.mgmt_class = ureq.mgmt_class; 645 req.mgmt_class_version = ureq.mgmt_class_version; 646 memcpy(req.oui, ureq.oui, sizeof req.oui); 647 648 if (compat_method_mask) { 649 u32 *umm = (u32 *) ureq.method_mask; 650 int i; 651 652 for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i) 653 req.method_mask[i] = 654 umm[i * 2] | ((u64) umm[i * 2 + 1] << 32); 655 } else 656 memcpy(req.method_mask, ureq.method_mask, 657 sizeof req.method_mask); 658 } 659 660 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num, 661 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI, 662 ureq.mgmt_class ? &req : NULL, 663 ureq.rmpp_version, 664 send_handler, recv_handler, file); 665 if (IS_ERR(agent)) { 666 ret = PTR_ERR(agent); 667 agent = NULL; 668 goto out; 669 } 670 671 if (put_user(agent_id, 672 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) { 673 ret = -EFAULT; 674 goto out; 675 } 676 677 if (!file->already_used) { 678 file->already_used = 1; 679 if (!file->use_pkey_index) { 680 printk(KERN_WARNING "user_mad: process %s did not enable " 681 "P_Key index support.\n", current->comm); 682 printk(KERN_WARNING "user_mad: Documentation/infiniband/user_mad.txt " 683 "has info on the new ABI.\n"); 684 } 685 } 686 687 file->agent[agent_id] = agent; 688 ret = 0; 689 690 out: 691 mutex_unlock(&file->mutex); 692 693 if (ret && agent) 694 ib_unregister_mad_agent(agent); 695 696 mutex_unlock(&file->port->file_mutex); 697 698 return ret; 699 } 700 701 static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg) 702 { 703 struct ib_mad_agent *agent = NULL; 704 u32 id; 705 int ret = 0; 706 707 if (get_user(id, arg)) 708 return -EFAULT; 709 710 mutex_lock(&file->port->file_mutex); 711 mutex_lock(&file->mutex); 712 713 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) { 714 ret = -EINVAL; 715 goto out; 716 } 717 718 agent = file->agent[id]; 719 file->agent[id] = NULL; 720 721 out: 722 mutex_unlock(&file->mutex); 723 724 if (agent) 725 ib_unregister_mad_agent(agent); 726 727 mutex_unlock(&file->port->file_mutex); 728 729 return ret; 730 } 731 732 static long ib_umad_enable_pkey(struct ib_umad_file *file) 733 { 734 int ret = 0; 735 736 mutex_lock(&file->mutex); 737 if (file->already_used) 738 ret = -EINVAL; 739 else 740 file->use_pkey_index = 1; 741 mutex_unlock(&file->mutex); 742 743 return ret; 744 } 745 746 static long ib_umad_ioctl(struct file *filp, unsigned int cmd, 747 unsigned long arg) 748 { 749 switch (cmd) { 750 case IB_USER_MAD_REGISTER_AGENT: 751 return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0); 752 case IB_USER_MAD_UNREGISTER_AGENT: 753 return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg); 754 case IB_USER_MAD_ENABLE_PKEY: 755 return ib_umad_enable_pkey(filp->private_data); 756 default: 757 return -ENOIOCTLCMD; 758 } 759 } 760 761 #ifdef CONFIG_COMPAT 762 static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd, 763 unsigned long arg) 764 { 765 switch (cmd) { 766 case IB_USER_MAD_REGISTER_AGENT: 767 return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1); 768 case IB_USER_MAD_UNREGISTER_AGENT: 769 return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg)); 770 case IB_USER_MAD_ENABLE_PKEY: 771 return ib_umad_enable_pkey(filp->private_data); 772 default: 773 return -ENOIOCTLCMD; 774 } 775 } 776 #endif 777 778 /* 779 * ib_umad_open() does not need the BKL: 780 * 781 * - umad_port[] accesses are protected by port_lock, the 782 * ib_umad_port structures are properly reference counted, and 783 * everything else is purely local to the file being created, so 784 * races against other open calls are not a problem; 785 * - the ioctl method does not affect any global state outside of the 786 * file structure being operated on; 787 * - the port is added to umad_port[] as the last part of module 788 * initialization so the open method will either immediately run 789 * -ENXIO, or all required initialization will be done. 790 */ 791 static int ib_umad_open(struct inode *inode, struct file *filp) 792 { 793 struct ib_umad_port *port; 794 struct ib_umad_file *file; 795 int ret = 0; 796 797 spin_lock(&port_lock); 798 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE]; 799 if (port) 800 kref_get(&port->umad_dev->ref); 801 spin_unlock(&port_lock); 802 803 if (!port) 804 return -ENXIO; 805 806 mutex_lock(&port->file_mutex); 807 808 if (!port->ib_dev) { 809 ret = -ENXIO; 810 goto out; 811 } 812 813 file = kzalloc(sizeof *file, GFP_KERNEL); 814 if (!file) { 815 kref_put(&port->umad_dev->ref, ib_umad_release_dev); 816 ret = -ENOMEM; 817 goto out; 818 } 819 820 mutex_init(&file->mutex); 821 spin_lock_init(&file->send_lock); 822 INIT_LIST_HEAD(&file->recv_list); 823 INIT_LIST_HEAD(&file->send_list); 824 init_waitqueue_head(&file->recv_wait); 825 826 file->port = port; 827 filp->private_data = file; 828 829 list_add_tail(&file->port_list, &port->file_list); 830 831 out: 832 mutex_unlock(&port->file_mutex); 833 return ret; 834 } 835 836 static int ib_umad_close(struct inode *inode, struct file *filp) 837 { 838 struct ib_umad_file *file = filp->private_data; 839 struct ib_umad_device *dev = file->port->umad_dev; 840 struct ib_umad_packet *packet, *tmp; 841 int already_dead; 842 int i; 843 844 mutex_lock(&file->port->file_mutex); 845 mutex_lock(&file->mutex); 846 847 already_dead = file->agents_dead; 848 file->agents_dead = 1; 849 850 list_for_each_entry_safe(packet, tmp, &file->recv_list, list) { 851 if (packet->recv_wc) 852 ib_free_recv_mad(packet->recv_wc); 853 kfree(packet); 854 } 855 856 list_del(&file->port_list); 857 858 mutex_unlock(&file->mutex); 859 860 if (!already_dead) 861 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) 862 if (file->agent[i]) 863 ib_unregister_mad_agent(file->agent[i]); 864 865 mutex_unlock(&file->port->file_mutex); 866 867 kfree(file); 868 kref_put(&dev->ref, ib_umad_release_dev); 869 870 return 0; 871 } 872 873 static const struct file_operations umad_fops = { 874 .owner = THIS_MODULE, 875 .read = ib_umad_read, 876 .write = ib_umad_write, 877 .poll = ib_umad_poll, 878 .unlocked_ioctl = ib_umad_ioctl, 879 #ifdef CONFIG_COMPAT 880 .compat_ioctl = ib_umad_compat_ioctl, 881 #endif 882 .open = ib_umad_open, 883 .release = ib_umad_close 884 }; 885 886 static int ib_umad_sm_open(struct inode *inode, struct file *filp) 887 { 888 struct ib_umad_port *port; 889 struct ib_port_modify props = { 890 .set_port_cap_mask = IB_PORT_SM 891 }; 892 int ret; 893 894 spin_lock(&port_lock); 895 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS]; 896 if (port) 897 kref_get(&port->umad_dev->ref); 898 spin_unlock(&port_lock); 899 900 if (!port) 901 return -ENXIO; 902 903 if (filp->f_flags & O_NONBLOCK) { 904 if (down_trylock(&port->sm_sem)) { 905 ret = -EAGAIN; 906 goto fail; 907 } 908 } else { 909 if (down_interruptible(&port->sm_sem)) { 910 ret = -ERESTARTSYS; 911 goto fail; 912 } 913 } 914 915 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); 916 if (ret) { 917 up(&port->sm_sem); 918 goto fail; 919 } 920 921 filp->private_data = port; 922 923 return 0; 924 925 fail: 926 kref_put(&port->umad_dev->ref, ib_umad_release_dev); 927 return ret; 928 } 929 930 static int ib_umad_sm_close(struct inode *inode, struct file *filp) 931 { 932 struct ib_umad_port *port = filp->private_data; 933 struct ib_port_modify props = { 934 .clr_port_cap_mask = IB_PORT_SM 935 }; 936 int ret = 0; 937 938 mutex_lock(&port->file_mutex); 939 if (port->ib_dev) 940 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); 941 mutex_unlock(&port->file_mutex); 942 943 up(&port->sm_sem); 944 945 kref_put(&port->umad_dev->ref, ib_umad_release_dev); 946 947 return ret; 948 } 949 950 static const struct file_operations umad_sm_fops = { 951 .owner = THIS_MODULE, 952 .open = ib_umad_sm_open, 953 .release = ib_umad_sm_close 954 }; 955 956 static struct ib_client umad_client = { 957 .name = "umad", 958 .add = ib_umad_add_one, 959 .remove = ib_umad_remove_one 960 }; 961 962 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, 963 char *buf) 964 { 965 struct ib_umad_port *port = dev_get_drvdata(dev); 966 967 if (!port) 968 return -ENODEV; 969 970 return sprintf(buf, "%s\n", port->ib_dev->name); 971 } 972 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); 973 974 static ssize_t show_port(struct device *dev, struct device_attribute *attr, 975 char *buf) 976 { 977 struct ib_umad_port *port = dev_get_drvdata(dev); 978 979 if (!port) 980 return -ENODEV; 981 982 return sprintf(buf, "%d\n", port->port_num); 983 } 984 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); 985 986 static ssize_t show_abi_version(struct class *class, char *buf) 987 { 988 return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION); 989 } 990 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); 991 992 static int ib_umad_init_port(struct ib_device *device, int port_num, 993 struct ib_umad_port *port) 994 { 995 spin_lock(&port_lock); 996 port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS); 997 if (port->dev_num >= IB_UMAD_MAX_PORTS) { 998 spin_unlock(&port_lock); 999 return -1; 1000 } 1001 set_bit(port->dev_num, dev_map); 1002 spin_unlock(&port_lock); 1003 1004 port->ib_dev = device; 1005 port->port_num = port_num; 1006 init_MUTEX(&port->sm_sem); 1007 mutex_init(&port->file_mutex); 1008 INIT_LIST_HEAD(&port->file_list); 1009 1010 port->cdev = cdev_alloc(); 1011 if (!port->cdev) 1012 return -1; 1013 port->cdev->owner = THIS_MODULE; 1014 port->cdev->ops = &umad_fops; 1015 kobject_set_name(&port->cdev->kobj, "umad%d", port->dev_num); 1016 if (cdev_add(port->cdev, base_dev + port->dev_num, 1)) 1017 goto err_cdev; 1018 1019 port->dev = device_create(umad_class, device->dma_device, 1020 port->cdev->dev, port, 1021 "umad%d", port->dev_num); 1022 if (IS_ERR(port->dev)) 1023 goto err_cdev; 1024 1025 if (device_create_file(port->dev, &dev_attr_ibdev)) 1026 goto err_dev; 1027 if (device_create_file(port->dev, &dev_attr_port)) 1028 goto err_dev; 1029 1030 port->sm_cdev = cdev_alloc(); 1031 if (!port->sm_cdev) 1032 goto err_dev; 1033 port->sm_cdev->owner = THIS_MODULE; 1034 port->sm_cdev->ops = &umad_sm_fops; 1035 kobject_set_name(&port->sm_cdev->kobj, "issm%d", port->dev_num); 1036 if (cdev_add(port->sm_cdev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1)) 1037 goto err_sm_cdev; 1038 1039 port->sm_dev = device_create(umad_class, device->dma_device, 1040 port->sm_cdev->dev, port, 1041 "issm%d", port->dev_num); 1042 if (IS_ERR(port->sm_dev)) 1043 goto err_sm_cdev; 1044 1045 if (device_create_file(port->sm_dev, &dev_attr_ibdev)) 1046 goto err_sm_dev; 1047 if (device_create_file(port->sm_dev, &dev_attr_port)) 1048 goto err_sm_dev; 1049 1050 spin_lock(&port_lock); 1051 umad_port[port->dev_num] = port; 1052 spin_unlock(&port_lock); 1053 1054 return 0; 1055 1056 err_sm_dev: 1057 device_destroy(umad_class, port->sm_cdev->dev); 1058 1059 err_sm_cdev: 1060 cdev_del(port->sm_cdev); 1061 1062 err_dev: 1063 device_destroy(umad_class, port->cdev->dev); 1064 1065 err_cdev: 1066 cdev_del(port->cdev); 1067 clear_bit(port->dev_num, dev_map); 1068 1069 return -1; 1070 } 1071 1072 static void ib_umad_kill_port(struct ib_umad_port *port) 1073 { 1074 struct ib_umad_file *file; 1075 int already_dead; 1076 int id; 1077 1078 dev_set_drvdata(port->dev, NULL); 1079 dev_set_drvdata(port->sm_dev, NULL); 1080 1081 device_destroy(umad_class, port->cdev->dev); 1082 device_destroy(umad_class, port->sm_cdev->dev); 1083 1084 cdev_del(port->cdev); 1085 cdev_del(port->sm_cdev); 1086 1087 spin_lock(&port_lock); 1088 umad_port[port->dev_num] = NULL; 1089 spin_unlock(&port_lock); 1090 1091 mutex_lock(&port->file_mutex); 1092 1093 port->ib_dev = NULL; 1094 1095 list_for_each_entry(file, &port->file_list, port_list) { 1096 mutex_lock(&file->mutex); 1097 already_dead = file->agents_dead; 1098 file->agents_dead = 1; 1099 mutex_unlock(&file->mutex); 1100 1101 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) 1102 if (file->agent[id]) 1103 ib_unregister_mad_agent(file->agent[id]); 1104 } 1105 1106 mutex_unlock(&port->file_mutex); 1107 1108 clear_bit(port->dev_num, dev_map); 1109 } 1110 1111 static void ib_umad_add_one(struct ib_device *device) 1112 { 1113 struct ib_umad_device *umad_dev; 1114 int s, e, i; 1115 1116 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) 1117 return; 1118 1119 if (device->node_type == RDMA_NODE_IB_SWITCH) 1120 s = e = 0; 1121 else { 1122 s = 1; 1123 e = device->phys_port_cnt; 1124 } 1125 1126 umad_dev = kzalloc(sizeof *umad_dev + 1127 (e - s + 1) * sizeof (struct ib_umad_port), 1128 GFP_KERNEL); 1129 if (!umad_dev) 1130 return; 1131 1132 kref_init(&umad_dev->ref); 1133 1134 umad_dev->start_port = s; 1135 umad_dev->end_port = e; 1136 1137 for (i = s; i <= e; ++i) { 1138 umad_dev->port[i - s].umad_dev = umad_dev; 1139 1140 if (ib_umad_init_port(device, i, &umad_dev->port[i - s])) 1141 goto err; 1142 } 1143 1144 ib_set_client_data(device, &umad_client, umad_dev); 1145 1146 return; 1147 1148 err: 1149 while (--i >= s) 1150 ib_umad_kill_port(&umad_dev->port[i - s]); 1151 1152 kref_put(&umad_dev->ref, ib_umad_release_dev); 1153 } 1154 1155 static void ib_umad_remove_one(struct ib_device *device) 1156 { 1157 struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client); 1158 int i; 1159 1160 if (!umad_dev) 1161 return; 1162 1163 for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) 1164 ib_umad_kill_port(&umad_dev->port[i]); 1165 1166 kref_put(&umad_dev->ref, ib_umad_release_dev); 1167 } 1168 1169 static int __init ib_umad_init(void) 1170 { 1171 int ret; 1172 1173 ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2, 1174 "infiniband_mad"); 1175 if (ret) { 1176 printk(KERN_ERR "user_mad: couldn't register device number\n"); 1177 goto out; 1178 } 1179 1180 umad_class = class_create(THIS_MODULE, "infiniband_mad"); 1181 if (IS_ERR(umad_class)) { 1182 ret = PTR_ERR(umad_class); 1183 printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n"); 1184 goto out_chrdev; 1185 } 1186 1187 ret = class_create_file(umad_class, &class_attr_abi_version); 1188 if (ret) { 1189 printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n"); 1190 goto out_class; 1191 } 1192 1193 ret = ib_register_client(&umad_client); 1194 if (ret) { 1195 printk(KERN_ERR "user_mad: couldn't register ib_umad client\n"); 1196 goto out_class; 1197 } 1198 1199 return 0; 1200 1201 out_class: 1202 class_destroy(umad_class); 1203 1204 out_chrdev: 1205 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); 1206 1207 out: 1208 return ret; 1209 } 1210 1211 static void __exit ib_umad_cleanup(void) 1212 { 1213 ib_unregister_client(&umad_client); 1214 class_destroy(umad_class); 1215 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); 1216 } 1217 1218 module_init(ib_umad_init); 1219 module_exit(ib_umad_cleanup); 1220