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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
34 * $Id: user_mad.c 4010 2005-11-09 23:11:56Z roland $
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/device.h>
40 #include <linux/err.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>
49 #include <asm/uaccess.h>
50 #include <asm/semaphore.h>
52 #include <rdma/ib_mad.h>
53 #include <rdma/ib_user_mad.h>
55 MODULE_AUTHOR("Roland Dreier");
56 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
57 MODULE_LICENSE("Dual BSD/GPL");
60 IB_UMAD_MAX_PORTS = 64,
61 IB_UMAD_MAX_AGENTS = 32,
64 IB_UMAD_MINOR_BASE = 0
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().
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
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.
91 struct class_device *class_dev;
94 struct class_device *sm_class_dev;
95 struct semaphore sm_sem;
97 struct rw_semaphore mutex;
98 struct list_head file_list;
100 struct ib_device *ib_dev;
101 struct ib_umad_device *umad_dev;
106 struct ib_umad_device {
107 int start_port, end_port;
109 struct ib_umad_port port[0];
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];
122 struct ib_umad_packet {
123 struct ib_mad_send_buf *msg;
124 struct list_head list;
126 struct ib_user_mad mad;
129 static struct class *umad_class;
131 static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
133 static DEFINE_SPINLOCK(port_lock);
134 static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS];
135 static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2);
137 static void ib_umad_add_one(struct ib_device *device);
138 static void ib_umad_remove_one(struct ib_device *device);
140 static void ib_umad_release_dev(struct kref *ref)
142 struct ib_umad_device *dev =
143 container_of(ref, struct ib_umad_device, ref);
148 /* caller must hold port->mutex at least for reading */
149 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
151 return file->agents_dead ? NULL : file->agent[id];
154 static int queue_packet(struct ib_umad_file *file,
155 struct ib_mad_agent *agent,
156 struct ib_umad_packet *packet)
160 down_read(&file->port->mutex);
162 for (packet->mad.hdr.id = 0;
163 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
164 packet->mad.hdr.id++)
165 if (agent == __get_agent(file, packet->mad.hdr.id)) {
166 spin_lock_irq(&file->recv_lock);
167 list_add_tail(&packet->list, &file->recv_list);
168 spin_unlock_irq(&file->recv_lock);
169 wake_up_interruptible(&file->recv_wait);
174 up_read(&file->port->mutex);
179 static void send_handler(struct ib_mad_agent *agent,
180 struct ib_mad_send_wc *send_wc)
182 struct ib_umad_file *file = agent->context;
183 struct ib_umad_packet *timeout;
184 struct ib_umad_packet *packet = send_wc->send_buf->context[0];
186 ib_destroy_ah(packet->msg->ah);
187 ib_free_send_mad(packet->msg);
189 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
190 timeout = kzalloc(sizeof *timeout + IB_MGMT_MAD_HDR, GFP_KERNEL);
194 timeout->length = IB_MGMT_MAD_HDR;
195 timeout->mad.hdr.id = packet->mad.hdr.id;
196 timeout->mad.hdr.status = ETIMEDOUT;
197 memcpy(timeout->mad.data, packet->mad.data,
198 sizeof (struct ib_mad_hdr));
200 if (!queue_packet(file, agent, timeout))
207 static void recv_handler(struct ib_mad_agent *agent,
208 struct ib_mad_recv_wc *mad_recv_wc)
210 struct ib_umad_file *file = agent->context;
211 struct ib_umad_packet *packet;
214 if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
217 length = mad_recv_wc->mad_len;
218 packet = kzalloc(sizeof *packet + length, GFP_KERNEL);
222 packet->length = length;
224 ib_coalesce_recv_mad(mad_recv_wc, packet->mad.data);
226 packet->mad.hdr.status = 0;
227 packet->mad.hdr.length = length + sizeof (struct ib_user_mad);
228 packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
229 packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
230 packet->mad.hdr.sl = mad_recv_wc->wc->sl;
231 packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
232 packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
233 if (packet->mad.hdr.grh_present) {
235 packet->mad.hdr.gid_index = 0;
236 packet->mad.hdr.hop_limit = 0;
237 packet->mad.hdr.traffic_class = 0;
238 memset(packet->mad.hdr.gid, 0, 16);
239 packet->mad.hdr.flow_label = 0;
242 if (queue_packet(file, agent, packet))
246 ib_free_recv_mad(mad_recv_wc);
249 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
250 size_t count, loff_t *pos)
252 struct ib_umad_file *file = filp->private_data;
253 struct ib_umad_packet *packet;
256 if (count < sizeof (struct ib_user_mad) + sizeof (struct ib_mad))
259 spin_lock_irq(&file->recv_lock);
261 while (list_empty(&file->recv_list)) {
262 spin_unlock_irq(&file->recv_lock);
264 if (filp->f_flags & O_NONBLOCK)
267 if (wait_event_interruptible(file->recv_wait,
268 !list_empty(&file->recv_list)))
271 spin_lock_irq(&file->recv_lock);
274 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
275 list_del(&packet->list);
277 spin_unlock_irq(&file->recv_lock);
279 if (count < packet->length + sizeof (struct ib_user_mad)) {
280 /* Return length needed (and first RMPP segment) if too small */
281 if (copy_to_user(buf, &packet->mad,
282 sizeof (struct ib_user_mad) + sizeof (struct ib_mad)))
286 } else if (copy_to_user(buf, &packet->mad,
287 packet->length + sizeof (struct ib_user_mad)))
290 ret = packet->length + sizeof (struct ib_user_mad);
293 spin_lock_irq(&file->recv_lock);
294 list_add(&packet->list, &file->recv_list);
295 spin_unlock_irq(&file->recv_lock);
301 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
302 size_t count, loff_t *pos)
304 struct ib_umad_file *file = filp->private_data;
305 struct ib_umad_packet *packet;
306 struct ib_mad_agent *agent;
307 struct ib_ah_attr ah_attr;
309 struct ib_rmpp_mad *rmpp_mad;
312 int ret, length, hdr_len, copy_offset;
315 if (count < sizeof (struct ib_user_mad))
318 length = count - sizeof (struct ib_user_mad);
319 packet = kmalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
323 if (copy_from_user(&packet->mad, buf,
324 sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
329 if (packet->mad.hdr.id < 0 ||
330 packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
335 down_read(&file->port->mutex);
337 agent = __get_agent(file, packet->mad.hdr.id);
343 memset(&ah_attr, 0, sizeof ah_attr);
344 ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
345 ah_attr.sl = packet->mad.hdr.sl;
346 ah_attr.src_path_bits = packet->mad.hdr.path_bits;
347 ah_attr.port_num = file->port->port_num;
348 if (packet->mad.hdr.grh_present) {
349 ah_attr.ah_flags = IB_AH_GRH;
350 memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
351 ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
352 ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
353 ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
356 ah = ib_create_ah(agent->qp->pd, &ah_attr);
362 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
363 if (ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE) {
365 if (!agent->rmpp_version) {
370 /* Validate that the management class can support RMPP */
371 if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
372 hdr_len = IB_MGMT_SA_HDR;
373 } else if ((rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
374 (rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) {
375 hdr_len = IB_MGMT_VENDOR_HDR;
381 copy_offset = IB_MGMT_RMPP_HDR;
383 hdr_len = IB_MGMT_MAD_HDR;
384 copy_offset = IB_MGMT_MAD_HDR;
387 packet->msg = ib_create_send_mad(agent,
388 be32_to_cpu(packet->mad.hdr.qpn),
390 hdr_len, length - hdr_len,
392 if (IS_ERR(packet->msg)) {
393 ret = PTR_ERR(packet->msg);
397 packet->msg->ah = ah;
398 packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
399 packet->msg->retries = packet->mad.hdr.retries;
400 packet->msg->context[0] = packet;
402 /* Copy MAD headers (RMPP header in place) */
403 memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
404 /* Now, copy rest of message from user into send buffer */
405 if (copy_from_user(packet->msg->mad + copy_offset,
406 buf + sizeof (struct ib_user_mad) + copy_offset,
407 length - copy_offset)) {
413 * If userspace is generating a request that will generate a
414 * response, we need to make sure the high-order part of the
415 * transaction ID matches the agent being used to send the
418 method = ((struct ib_mad_hdr *) packet->msg->mad)->method;
420 if (!(method & IB_MGMT_METHOD_RESP) &&
421 method != IB_MGMT_METHOD_TRAP_REPRESS &&
422 method != IB_MGMT_METHOD_SEND) {
423 tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
424 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
425 (be64_to_cpup(tid) & 0xffffffff));
428 ret = ib_post_send_mad(packet->msg, NULL);
432 up_read(&file->port->mutex);
437 ib_free_send_mad(packet->msg);
443 up_read(&file->port->mutex);
450 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
452 struct ib_umad_file *file = filp->private_data;
454 /* we will always be able to post a MAD send */
455 unsigned int mask = POLLOUT | POLLWRNORM;
457 poll_wait(filp, &file->recv_wait, wait);
459 if (!list_empty(&file->recv_list))
460 mask |= POLLIN | POLLRDNORM;
465 static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
467 struct ib_user_mad_reg_req ureq;
468 struct ib_mad_reg_req req;
469 struct ib_mad_agent *agent;
473 down_write(&file->port->mutex);
475 if (!file->port->ib_dev) {
480 if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
485 if (ureq.qpn != 0 && ureq.qpn != 1) {
490 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
491 if (!__get_agent(file, agent_id))
498 if (ureq.mgmt_class) {
499 req.mgmt_class = ureq.mgmt_class;
500 req.mgmt_class_version = ureq.mgmt_class_version;
501 memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
502 memcpy(req.oui, ureq.oui, sizeof req.oui);
505 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
506 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
507 ureq.mgmt_class ? &req : NULL,
509 send_handler, recv_handler, file);
511 ret = PTR_ERR(agent);
515 if (put_user(agent_id,
516 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
518 ib_unregister_mad_agent(agent);
522 file->agent[agent_id] = agent;
526 up_write(&file->port->mutex);
530 static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
532 struct ib_mad_agent *agent = NULL;
536 if (get_user(id, (u32 __user *) arg))
539 down_write(&file->port->mutex);
541 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
546 agent = file->agent[id];
547 file->agent[id] = NULL;
550 up_write(&file->port->mutex);
553 ib_unregister_mad_agent(agent);
558 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
562 case IB_USER_MAD_REGISTER_AGENT:
563 return ib_umad_reg_agent(filp->private_data, arg);
564 case IB_USER_MAD_UNREGISTER_AGENT:
565 return ib_umad_unreg_agent(filp->private_data, arg);
571 static int ib_umad_open(struct inode *inode, struct file *filp)
573 struct ib_umad_port *port;
574 struct ib_umad_file *file;
577 spin_lock(&port_lock);
578 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
580 kref_get(&port->umad_dev->ref);
581 spin_unlock(&port_lock);
586 down_write(&port->mutex);
593 file = kzalloc(sizeof *file, GFP_KERNEL);
595 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
600 spin_lock_init(&file->recv_lock);
601 INIT_LIST_HEAD(&file->recv_list);
602 init_waitqueue_head(&file->recv_wait);
605 filp->private_data = file;
607 list_add_tail(&file->port_list, &port->file_list);
610 up_write(&port->mutex);
614 static int ib_umad_close(struct inode *inode, struct file *filp)
616 struct ib_umad_file *file = filp->private_data;
617 struct ib_umad_device *dev = file->port->umad_dev;
618 struct ib_umad_packet *packet, *tmp;
622 down_write(&file->port->mutex);
624 already_dead = file->agents_dead;
625 file->agents_dead = 1;
627 list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
630 list_del(&file->port_list);
632 downgrade_write(&file->port->mutex);
635 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
637 ib_unregister_mad_agent(file->agent[i]);
639 up_read(&file->port->mutex);
642 kref_put(&dev->ref, ib_umad_release_dev);
647 static struct file_operations umad_fops = {
648 .owner = THIS_MODULE,
649 .read = ib_umad_read,
650 .write = ib_umad_write,
651 .poll = ib_umad_poll,
652 .unlocked_ioctl = ib_umad_ioctl,
653 .compat_ioctl = ib_umad_ioctl,
654 .open = ib_umad_open,
655 .release = ib_umad_close
658 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
660 struct ib_umad_port *port;
661 struct ib_port_modify props = {
662 .set_port_cap_mask = IB_PORT_SM
666 spin_lock(&port_lock);
667 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
669 kref_get(&port->umad_dev->ref);
670 spin_unlock(&port_lock);
675 if (filp->f_flags & O_NONBLOCK) {
676 if (down_trylock(&port->sm_sem)) {
681 if (down_interruptible(&port->sm_sem)) {
687 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
693 filp->private_data = port;
698 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
702 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
704 struct ib_umad_port *port = filp->private_data;
705 struct ib_port_modify props = {
706 .clr_port_cap_mask = IB_PORT_SM
710 down_write(&port->mutex);
712 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
713 up_write(&port->mutex);
717 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
722 static struct file_operations umad_sm_fops = {
723 .owner = THIS_MODULE,
724 .open = ib_umad_sm_open,
725 .release = ib_umad_sm_close
728 static struct ib_client umad_client = {
730 .add = ib_umad_add_one,
731 .remove = ib_umad_remove_one
734 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
736 struct ib_umad_port *port = class_get_devdata(class_dev);
741 return sprintf(buf, "%s\n", port->ib_dev->name);
743 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
745 static ssize_t show_port(struct class_device *class_dev, char *buf)
747 struct ib_umad_port *port = class_get_devdata(class_dev);
752 return sprintf(buf, "%d\n", port->port_num);
754 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
756 static ssize_t show_abi_version(struct class *class, char *buf)
758 return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
760 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
762 static int ib_umad_init_port(struct ib_device *device, int port_num,
763 struct ib_umad_port *port)
765 spin_lock(&port_lock);
766 port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
767 if (port->dev_num >= IB_UMAD_MAX_PORTS) {
768 spin_unlock(&port_lock);
771 set_bit(port->dev_num, dev_map);
772 spin_unlock(&port_lock);
774 port->ib_dev = device;
775 port->port_num = port_num;
776 init_MUTEX(&port->sm_sem);
777 init_rwsem(&port->mutex);
778 INIT_LIST_HEAD(&port->file_list);
780 port->dev = cdev_alloc();
783 port->dev->owner = THIS_MODULE;
784 port->dev->ops = &umad_fops;
785 kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num);
786 if (cdev_add(port->dev, base_dev + port->dev_num, 1))
789 port->class_dev = class_device_create(umad_class, NULL, port->dev->dev,
791 "umad%d", port->dev_num);
792 if (IS_ERR(port->class_dev))
795 if (class_device_create_file(port->class_dev, &class_device_attr_ibdev))
797 if (class_device_create_file(port->class_dev, &class_device_attr_port))
800 port->sm_dev = cdev_alloc();
803 port->sm_dev->owner = THIS_MODULE;
804 port->sm_dev->ops = &umad_sm_fops;
805 kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num);
806 if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
809 port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev,
811 "issm%d", port->dev_num);
812 if (IS_ERR(port->sm_class_dev))
815 class_set_devdata(port->class_dev, port);
816 class_set_devdata(port->sm_class_dev, port);
818 if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev))
820 if (class_device_create_file(port->sm_class_dev, &class_device_attr_port))
823 spin_lock(&port_lock);
824 umad_port[port->dev_num] = port;
825 spin_unlock(&port_lock);
830 class_device_destroy(umad_class, port->sm_dev->dev);
833 cdev_del(port->sm_dev);
836 class_device_destroy(umad_class, port->dev->dev);
840 clear_bit(port->dev_num, dev_map);
845 static void ib_umad_kill_port(struct ib_umad_port *port)
847 struct ib_umad_file *file;
850 class_set_devdata(port->class_dev, NULL);
851 class_set_devdata(port->sm_class_dev, NULL);
853 class_device_destroy(umad_class, port->dev->dev);
854 class_device_destroy(umad_class, port->sm_dev->dev);
857 cdev_del(port->sm_dev);
859 spin_lock(&port_lock);
860 umad_port[port->dev_num] = NULL;
861 spin_unlock(&port_lock);
863 down_write(&port->mutex);
868 * Now go through the list of files attached to this port and
869 * unregister all of their MAD agents. We need to hold
870 * port->mutex while doing this to avoid racing with
871 * ib_umad_close(), but we can't hold the mutex for writing
872 * while calling ib_unregister_mad_agent(), since that might
873 * deadlock by calling back into queue_packet(). So we
874 * downgrade our lock to a read lock, and then drop and
875 * reacquire the write lock for the next iteration.
877 * We do list_del_init() on the file's list_head so that the
878 * list_del in ib_umad_close() is still OK, even after the
879 * file is removed from the list.
881 while (!list_empty(&port->file_list)) {
882 file = list_entry(port->file_list.next, struct ib_umad_file,
885 file->agents_dead = 1;
886 list_del_init(&file->port_list);
888 downgrade_write(&port->mutex);
890 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
892 ib_unregister_mad_agent(file->agent[id]);
894 up_read(&port->mutex);
895 down_write(&port->mutex);
898 up_write(&port->mutex);
900 clear_bit(port->dev_num, dev_map);
903 static void ib_umad_add_one(struct ib_device *device)
905 struct ib_umad_device *umad_dev;
908 if (device->node_type == IB_NODE_SWITCH)
912 e = device->phys_port_cnt;
915 umad_dev = kzalloc(sizeof *umad_dev +
916 (e - s + 1) * sizeof (struct ib_umad_port),
921 kref_init(&umad_dev->ref);
923 umad_dev->start_port = s;
924 umad_dev->end_port = e;
926 for (i = s; i <= e; ++i) {
927 umad_dev->port[i - s].umad_dev = umad_dev;
929 if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
933 ib_set_client_data(device, &umad_client, umad_dev);
939 ib_umad_kill_port(&umad_dev->port[i - s]);
941 kref_put(&umad_dev->ref, ib_umad_release_dev);
944 static void ib_umad_remove_one(struct ib_device *device)
946 struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
952 for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
953 ib_umad_kill_port(&umad_dev->port[i]);
955 kref_put(&umad_dev->ref, ib_umad_release_dev);
958 static int __init ib_umad_init(void)
962 ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
965 printk(KERN_ERR "user_mad: couldn't register device number\n");
969 umad_class = class_create(THIS_MODULE, "infiniband_mad");
970 if (IS_ERR(umad_class)) {
971 ret = PTR_ERR(umad_class);
972 printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
976 ret = class_create_file(umad_class, &class_attr_abi_version);
978 printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
982 ret = ib_register_client(&umad_client);
984 printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
991 class_destroy(umad_class);
994 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1000 static void __exit ib_umad_cleanup(void)
1002 ib_unregister_client(&umad_client);
1003 class_destroy(umad_class);
1004 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1007 module_init(ib_umad_init);
1008 module_exit(ib_umad_cleanup);