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 2814 2005-07-06 19:14:09Z halr $
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];
119 struct ib_mr *mr[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 static int queue_packet(struct ib_umad_file *file,
149 struct ib_mad_agent *agent,
150 struct ib_umad_packet *packet)
154 down_read(&file->port->mutex);
155 for (packet->mad.hdr.id = 0;
156 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
157 packet->mad.hdr.id++)
158 if (agent == file->agent[packet->mad.hdr.id]) {
159 spin_lock_irq(&file->recv_lock);
160 list_add_tail(&packet->list, &file->recv_list);
161 spin_unlock_irq(&file->recv_lock);
162 wake_up_interruptible(&file->recv_wait);
167 up_read(&file->port->mutex);
172 static void send_handler(struct ib_mad_agent *agent,
173 struct ib_mad_send_wc *send_wc)
175 struct ib_umad_file *file = agent->context;
176 struct ib_umad_packet *timeout;
177 struct ib_umad_packet *packet = send_wc->send_buf->context[0];
179 ib_destroy_ah(packet->msg->ah);
180 ib_free_send_mad(packet->msg);
182 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
183 timeout = kzalloc(sizeof *timeout + IB_MGMT_MAD_HDR, GFP_KERNEL);
187 timeout->length = IB_MGMT_MAD_HDR;
188 timeout->mad.hdr.id = packet->mad.hdr.id;
189 timeout->mad.hdr.status = ETIMEDOUT;
190 memcpy(timeout->mad.data, packet->mad.data,
191 sizeof (struct ib_mad_hdr));
193 if (!queue_packet(file, agent, timeout))
200 static void recv_handler(struct ib_mad_agent *agent,
201 struct ib_mad_recv_wc *mad_recv_wc)
203 struct ib_umad_file *file = agent->context;
204 struct ib_umad_packet *packet;
207 if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
210 length = mad_recv_wc->mad_len;
211 packet = kzalloc(sizeof *packet + length, GFP_KERNEL);
215 packet->length = length;
217 ib_coalesce_recv_mad(mad_recv_wc, packet->mad.data);
219 packet->mad.hdr.status = 0;
220 packet->mad.hdr.length = length + sizeof (struct ib_user_mad);
221 packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
222 packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
223 packet->mad.hdr.sl = mad_recv_wc->wc->sl;
224 packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
225 packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
226 if (packet->mad.hdr.grh_present) {
228 packet->mad.hdr.gid_index = 0;
229 packet->mad.hdr.hop_limit = 0;
230 packet->mad.hdr.traffic_class = 0;
231 memset(packet->mad.hdr.gid, 0, 16);
232 packet->mad.hdr.flow_label = 0;
235 if (queue_packet(file, agent, packet))
239 ib_free_recv_mad(mad_recv_wc);
242 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
243 size_t count, loff_t *pos)
245 struct ib_umad_file *file = filp->private_data;
246 struct ib_umad_packet *packet;
249 if (count < sizeof (struct ib_user_mad) + sizeof (struct ib_mad))
252 spin_lock_irq(&file->recv_lock);
254 while (list_empty(&file->recv_list)) {
255 spin_unlock_irq(&file->recv_lock);
257 if (filp->f_flags & O_NONBLOCK)
260 if (wait_event_interruptible(file->recv_wait,
261 !list_empty(&file->recv_list)))
264 spin_lock_irq(&file->recv_lock);
267 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
268 list_del(&packet->list);
270 spin_unlock_irq(&file->recv_lock);
272 if (count < packet->length + sizeof (struct ib_user_mad)) {
273 /* Return length needed (and first RMPP segment) if too small */
274 if (copy_to_user(buf, &packet->mad,
275 sizeof (struct ib_user_mad) + sizeof (struct ib_mad)))
279 } else if (copy_to_user(buf, &packet->mad,
280 packet->length + sizeof (struct ib_user_mad)))
283 ret = packet->length + sizeof (struct ib_user_mad);
286 spin_lock_irq(&file->recv_lock);
287 list_add(&packet->list, &file->recv_list);
288 spin_unlock_irq(&file->recv_lock);
294 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
295 size_t count, loff_t *pos)
297 struct ib_umad_file *file = filp->private_data;
298 struct ib_umad_packet *packet;
299 struct ib_mad_agent *agent;
300 struct ib_ah_attr ah_attr;
302 struct ib_rmpp_mad *rmpp_mad;
305 int ret, length, hdr_len, copy_offset;
308 if (count < sizeof (struct ib_user_mad))
311 length = count - sizeof (struct ib_user_mad);
312 packet = kmalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
316 if (copy_from_user(&packet->mad, buf,
317 sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
322 if (packet->mad.hdr.id < 0 ||
323 packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
328 down_read(&file->port->mutex);
330 agent = file->agent[packet->mad.hdr.id];
336 memset(&ah_attr, 0, sizeof ah_attr);
337 ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
338 ah_attr.sl = packet->mad.hdr.sl;
339 ah_attr.src_path_bits = packet->mad.hdr.path_bits;
340 ah_attr.port_num = file->port->port_num;
341 if (packet->mad.hdr.grh_present) {
342 ah_attr.ah_flags = IB_AH_GRH;
343 memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
344 ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
345 ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
346 ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
349 ah = ib_create_ah(agent->qp->pd, &ah_attr);
355 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
356 if (ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE) {
358 if (!agent->rmpp_version) {
363 /* Validate that the management class can support RMPP */
364 if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
365 hdr_len = IB_MGMT_SA_HDR;
366 } else if ((rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
367 (rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) {
368 hdr_len = IB_MGMT_VENDOR_HDR;
374 copy_offset = IB_MGMT_RMPP_HDR;
376 hdr_len = IB_MGMT_MAD_HDR;
377 copy_offset = IB_MGMT_MAD_HDR;
380 packet->msg = ib_create_send_mad(agent,
381 be32_to_cpu(packet->mad.hdr.qpn),
383 hdr_len, length - hdr_len,
385 if (IS_ERR(packet->msg)) {
386 ret = PTR_ERR(packet->msg);
390 packet->msg->ah = ah;
391 packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
392 packet->msg->retries = packet->mad.hdr.retries;
393 packet->msg->context[0] = packet;
395 /* Copy MAD headers (RMPP header in place) */
396 memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
397 /* Now, copy rest of message from user into send buffer */
398 if (copy_from_user(packet->msg->mad + copy_offset,
399 buf + sizeof (struct ib_user_mad) + copy_offset,
400 length - copy_offset)) {
406 * If userspace is generating a request that will generate a
407 * response, we need to make sure the high-order part of the
408 * transaction ID matches the agent being used to send the
411 method = ((struct ib_mad_hdr *) packet->msg->mad)->method;
413 if (!(method & IB_MGMT_METHOD_RESP) &&
414 method != IB_MGMT_METHOD_TRAP_REPRESS &&
415 method != IB_MGMT_METHOD_SEND) {
416 tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
417 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
418 (be64_to_cpup(tid) & 0xffffffff));
421 ret = ib_post_send_mad(packet->msg, NULL);
425 up_read(&file->port->mutex);
430 ib_free_send_mad(packet->msg);
436 up_read(&file->port->mutex);
443 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
445 struct ib_umad_file *file = filp->private_data;
447 /* we will always be able to post a MAD send */
448 unsigned int mask = POLLOUT | POLLWRNORM;
450 poll_wait(filp, &file->recv_wait, wait);
452 if (!list_empty(&file->recv_list))
453 mask |= POLLIN | POLLRDNORM;
458 static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
460 struct ib_user_mad_reg_req ureq;
461 struct ib_mad_reg_req req;
462 struct ib_mad_agent *agent;
466 down_write(&file->port->mutex);
468 if (!file->port->ib_dev) {
473 if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
478 if (ureq.qpn != 0 && ureq.qpn != 1) {
483 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
484 if (!file->agent[agent_id])
491 if (ureq.mgmt_class) {
492 req.mgmt_class = ureq.mgmt_class;
493 req.mgmt_class_version = ureq.mgmt_class_version;
494 memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
495 memcpy(req.oui, ureq.oui, sizeof req.oui);
498 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
499 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
500 ureq.mgmt_class ? &req : NULL,
502 send_handler, recv_handler, file);
504 ret = PTR_ERR(agent);
508 file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE);
509 if (IS_ERR(file->mr[agent_id])) {
514 if (put_user(agent_id,
515 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
520 file->agent[agent_id] = agent;
526 ib_dereg_mr(file->mr[agent_id]);
529 ib_unregister_mad_agent(agent);
532 up_write(&file->port->mutex);
536 static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
538 struct ib_mad_agent *agent = NULL;
539 struct ib_mr *mr = NULL;
543 if (get_user(id, (u32 __user *) arg))
546 down_write(&file->port->mutex);
548 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) {
553 agent = file->agent[id];
555 file->agent[id] = NULL;
558 up_write(&file->port->mutex);
561 ib_unregister_mad_agent(agent);
568 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
572 case IB_USER_MAD_REGISTER_AGENT:
573 return ib_umad_reg_agent(filp->private_data, arg);
574 case IB_USER_MAD_UNREGISTER_AGENT:
575 return ib_umad_unreg_agent(filp->private_data, arg);
581 static int ib_umad_open(struct inode *inode, struct file *filp)
583 struct ib_umad_port *port;
584 struct ib_umad_file *file;
587 spin_lock(&port_lock);
588 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
590 kref_get(&port->umad_dev->ref);
591 spin_unlock(&port_lock);
596 down_write(&port->mutex);
603 file = kzalloc(sizeof *file, GFP_KERNEL);
605 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
610 spin_lock_init(&file->recv_lock);
611 INIT_LIST_HEAD(&file->recv_list);
612 init_waitqueue_head(&file->recv_wait);
615 filp->private_data = file;
617 list_add_tail(&file->port_list, &port->file_list);
620 up_write(&port->mutex);
624 static int ib_umad_close(struct inode *inode, struct file *filp)
626 struct ib_umad_file *file = filp->private_data;
627 struct ib_umad_device *dev = file->port->umad_dev;
628 struct ib_umad_packet *packet, *tmp;
631 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
632 if (file->agent[i]) {
633 ib_unregister_mad_agent(file->agent[i]);
634 ib_dereg_mr(file->mr[i]);
637 list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
640 down_write(&file->port->mutex);
641 list_del(&file->port_list);
642 up_write(&file->port->mutex);
646 kref_put(&dev->ref, ib_umad_release_dev);
651 static struct file_operations umad_fops = {
652 .owner = THIS_MODULE,
653 .read = ib_umad_read,
654 .write = ib_umad_write,
655 .poll = ib_umad_poll,
656 .unlocked_ioctl = ib_umad_ioctl,
657 .compat_ioctl = ib_umad_ioctl,
658 .open = ib_umad_open,
659 .release = ib_umad_close
662 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
664 struct ib_umad_port *port;
665 struct ib_port_modify props = {
666 .set_port_cap_mask = IB_PORT_SM
670 spin_lock(&port_lock);
671 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
673 kref_get(&port->umad_dev->ref);
674 spin_unlock(&port_lock);
679 if (filp->f_flags & O_NONBLOCK) {
680 if (down_trylock(&port->sm_sem)) {
685 if (down_interruptible(&port->sm_sem)) {
691 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
697 filp->private_data = port;
702 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
706 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
708 struct ib_umad_port *port = filp->private_data;
709 struct ib_port_modify props = {
710 .clr_port_cap_mask = IB_PORT_SM
714 down_write(&port->mutex);
716 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
717 up_write(&port->mutex);
721 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
726 static struct file_operations umad_sm_fops = {
727 .owner = THIS_MODULE,
728 .open = ib_umad_sm_open,
729 .release = ib_umad_sm_close
732 static struct ib_client umad_client = {
734 .add = ib_umad_add_one,
735 .remove = ib_umad_remove_one
738 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
740 struct ib_umad_port *port = class_get_devdata(class_dev);
745 return sprintf(buf, "%s\n", port->ib_dev->name);
747 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
749 static ssize_t show_port(struct class_device *class_dev, char *buf)
751 struct ib_umad_port *port = class_get_devdata(class_dev);
756 return sprintf(buf, "%d\n", port->port_num);
758 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
760 static ssize_t show_abi_version(struct class *class, char *buf)
762 return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
764 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
766 static int ib_umad_init_port(struct ib_device *device, int port_num,
767 struct ib_umad_port *port)
769 spin_lock(&port_lock);
770 port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
771 if (port->dev_num >= IB_UMAD_MAX_PORTS) {
772 spin_unlock(&port_lock);
775 set_bit(port->dev_num, dev_map);
776 spin_unlock(&port_lock);
778 port->ib_dev = device;
779 port->port_num = port_num;
780 init_MUTEX(&port->sm_sem);
781 init_rwsem(&port->mutex);
782 INIT_LIST_HEAD(&port->file_list);
784 port->dev = cdev_alloc();
787 port->dev->owner = THIS_MODULE;
788 port->dev->ops = &umad_fops;
789 kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num);
790 if (cdev_add(port->dev, base_dev + port->dev_num, 1))
793 port->class_dev = class_device_create(umad_class, NULL, port->dev->dev,
795 "umad%d", port->dev_num);
796 if (IS_ERR(port->class_dev))
799 if (class_device_create_file(port->class_dev, &class_device_attr_ibdev))
801 if (class_device_create_file(port->class_dev, &class_device_attr_port))
804 port->sm_dev = cdev_alloc();
807 port->sm_dev->owner = THIS_MODULE;
808 port->sm_dev->ops = &umad_sm_fops;
809 kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num);
810 if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
813 port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev,
815 "issm%d", port->dev_num);
816 if (IS_ERR(port->sm_class_dev))
819 class_set_devdata(port->class_dev, port);
820 class_set_devdata(port->sm_class_dev, port);
822 if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev))
824 if (class_device_create_file(port->sm_class_dev, &class_device_attr_port))
827 spin_lock(&port_lock);
828 umad_port[port->dev_num] = port;
829 spin_unlock(&port_lock);
834 class_device_destroy(umad_class, port->sm_dev->dev);
837 cdev_del(port->sm_dev);
840 class_device_destroy(umad_class, port->dev->dev);
844 clear_bit(port->dev_num, dev_map);
849 static void ib_umad_kill_port(struct ib_umad_port *port)
851 struct ib_umad_file *file;
854 class_set_devdata(port->class_dev, NULL);
855 class_set_devdata(port->sm_class_dev, NULL);
857 class_device_destroy(umad_class, port->dev->dev);
858 class_device_destroy(umad_class, port->sm_dev->dev);
861 cdev_del(port->sm_dev);
863 spin_lock(&port_lock);
864 umad_port[port->dev_num] = NULL;
865 spin_unlock(&port_lock);
867 down_write(&port->mutex);
871 list_for_each_entry(file, &port->file_list, port_list)
872 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) {
873 if (!file->agent[id])
875 ib_dereg_mr(file->mr[id]);
876 ib_unregister_mad_agent(file->agent[id]);
877 file->agent[id] = NULL;
880 up_write(&port->mutex);
882 clear_bit(port->dev_num, dev_map);
885 static void ib_umad_add_one(struct ib_device *device)
887 struct ib_umad_device *umad_dev;
890 if (device->node_type == IB_NODE_SWITCH)
894 e = device->phys_port_cnt;
897 umad_dev = kzalloc(sizeof *umad_dev +
898 (e - s + 1) * sizeof (struct ib_umad_port),
903 kref_init(&umad_dev->ref);
905 umad_dev->start_port = s;
906 umad_dev->end_port = e;
908 for (i = s; i <= e; ++i) {
909 umad_dev->port[i - s].umad_dev = umad_dev;
911 if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
915 ib_set_client_data(device, &umad_client, umad_dev);
921 ib_umad_kill_port(&umad_dev->port[i - s]);
923 kref_put(&umad_dev->ref, ib_umad_release_dev);
926 static void ib_umad_remove_one(struct ib_device *device)
928 struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
934 for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
935 ib_umad_kill_port(&umad_dev->port[i]);
937 kref_put(&umad_dev->ref, ib_umad_release_dev);
940 static int __init ib_umad_init(void)
944 ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
947 printk(KERN_ERR "user_mad: couldn't register device number\n");
951 umad_class = class_create(THIS_MODULE, "infiniband_mad");
952 if (IS_ERR(umad_class)) {
953 ret = PTR_ERR(umad_class);
954 printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
958 ret = class_create_file(umad_class, &class_attr_abi_version);
960 printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
964 ret = ib_register_client(&umad_client);
966 printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
973 class_destroy(umad_class);
976 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
982 static void __exit ib_umad_cleanup(void)
984 ib_unregister_client(&umad_client);
985 class_destroy(umad_class);
986 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
989 module_init(ib_umad_init);
990 module_exit(ib_umad_cleanup);