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
;
313 int rmpp_active
, has_rmpp_header
;
315 if (count
< sizeof (struct ib_user_mad
) + IB_MGMT_RMPP_HDR
)
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 (rmpp_mad
->mad_hdr
.mgmt_class
== IB_MGMT_CLASS_SUBN_ADM
) {
364 hdr_len
= IB_MGMT_SA_HDR
;
365 copy_offset
= IB_MGMT_RMPP_HDR
;
367 } else if (rmpp_mad
->mad_hdr
.mgmt_class
>= IB_MGMT_CLASS_VENDOR_RANGE2_START
&&
368 rmpp_mad
->mad_hdr
.mgmt_class
<= IB_MGMT_CLASS_VENDOR_RANGE2_END
) {
369 hdr_len
= IB_MGMT_VENDOR_HDR
;
370 copy_offset
= IB_MGMT_RMPP_HDR
;
373 hdr_len
= IB_MGMT_MAD_HDR
;
374 copy_offset
= IB_MGMT_MAD_HDR
;
379 rmpp_active
= ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
380 IB_MGMT_RMPP_FLAG_ACTIVE
;
384 /* Validate that the management class can support RMPP */
385 if (rmpp_active
&& !agent
->rmpp_version
) {
390 packet
->msg
= ib_create_send_mad(agent
,
391 be32_to_cpu(packet
->mad
.hdr
.qpn
),
393 hdr_len
, length
- hdr_len
,
395 if (IS_ERR(packet
->msg
)) {
396 ret
= PTR_ERR(packet
->msg
);
400 packet
->msg
->ah
= ah
;
401 packet
->msg
->timeout_ms
= packet
->mad
.hdr
.timeout_ms
;
402 packet
->msg
->retries
= packet
->mad
.hdr
.retries
;
403 packet
->msg
->context
[0] = packet
;
405 /* Copy MAD headers (RMPP header in place) */
406 memcpy(packet
->msg
->mad
, packet
->mad
.data
, IB_MGMT_MAD_HDR
);
407 /* Now, copy rest of message from user into send buffer */
408 if (copy_from_user(packet
->msg
->mad
+ copy_offset
,
409 buf
+ sizeof (struct ib_user_mad
) + copy_offset
,
410 length
- copy_offset
)) {
416 * If userspace is generating a request that will generate a
417 * response, we need to make sure the high-order part of the
418 * transaction ID matches the agent being used to send the
421 method
= ((struct ib_mad_hdr
*) packet
->msg
->mad
)->method
;
423 if (!(method
& IB_MGMT_METHOD_RESP
) &&
424 method
!= IB_MGMT_METHOD_TRAP_REPRESS
&&
425 method
!= IB_MGMT_METHOD_SEND
) {
426 tid
= &((struct ib_mad_hdr
*) packet
->msg
->mad
)->tid
;
427 *tid
= cpu_to_be64(((u64
) agent
->hi_tid
) << 32 |
428 (be64_to_cpup(tid
) & 0xffffffff));
431 ret
= ib_post_send_mad(packet
->msg
, NULL
);
435 up_read(&file
->port
->mutex
);
440 ib_free_send_mad(packet
->msg
);
446 up_read(&file
->port
->mutex
);
453 static unsigned int ib_umad_poll(struct file
*filp
, struct poll_table_struct
*wait
)
455 struct ib_umad_file
*file
= filp
->private_data
;
457 /* we will always be able to post a MAD send */
458 unsigned int mask
= POLLOUT
| POLLWRNORM
;
460 poll_wait(filp
, &file
->recv_wait
, wait
);
462 if (!list_empty(&file
->recv_list
))
463 mask
|= POLLIN
| POLLRDNORM
;
468 static int ib_umad_reg_agent(struct ib_umad_file
*file
, unsigned long arg
)
470 struct ib_user_mad_reg_req ureq
;
471 struct ib_mad_reg_req req
;
472 struct ib_mad_agent
*agent
;
476 down_write(&file
->port
->mutex
);
478 if (!file
->port
->ib_dev
) {
483 if (copy_from_user(&ureq
, (void __user
*) arg
, sizeof ureq
)) {
488 if (ureq
.qpn
!= 0 && ureq
.qpn
!= 1) {
493 for (agent_id
= 0; agent_id
< IB_UMAD_MAX_AGENTS
; ++agent_id
)
494 if (!__get_agent(file
, agent_id
))
501 if (ureq
.mgmt_class
) {
502 req
.mgmt_class
= ureq
.mgmt_class
;
503 req
.mgmt_class_version
= ureq
.mgmt_class_version
;
504 memcpy(req
.method_mask
, ureq
.method_mask
, sizeof req
.method_mask
);
505 memcpy(req
.oui
, ureq
.oui
, sizeof req
.oui
);
508 agent
= ib_register_mad_agent(file
->port
->ib_dev
, file
->port
->port_num
,
509 ureq
.qpn
? IB_QPT_GSI
: IB_QPT_SMI
,
510 ureq
.mgmt_class
? &req
: NULL
,
512 send_handler
, recv_handler
, file
);
514 ret
= PTR_ERR(agent
);
518 if (put_user(agent_id
,
519 (u32 __user
*) (arg
+ offsetof(struct ib_user_mad_reg_req
, id
)))) {
521 ib_unregister_mad_agent(agent
);
525 file
->agent
[agent_id
] = agent
;
529 up_write(&file
->port
->mutex
);
533 static int ib_umad_unreg_agent(struct ib_umad_file
*file
, unsigned long arg
)
535 struct ib_mad_agent
*agent
= NULL
;
539 if (get_user(id
, (u32 __user
*) arg
))
542 down_write(&file
->port
->mutex
);
544 if (id
< 0 || id
>= IB_UMAD_MAX_AGENTS
|| !__get_agent(file
, id
)) {
549 agent
= file
->agent
[id
];
550 file
->agent
[id
] = NULL
;
553 up_write(&file
->port
->mutex
);
556 ib_unregister_mad_agent(agent
);
561 static long ib_umad_ioctl(struct file
*filp
, unsigned int cmd
,
565 case IB_USER_MAD_REGISTER_AGENT
:
566 return ib_umad_reg_agent(filp
->private_data
, arg
);
567 case IB_USER_MAD_UNREGISTER_AGENT
:
568 return ib_umad_unreg_agent(filp
->private_data
, arg
);
574 static int ib_umad_open(struct inode
*inode
, struct file
*filp
)
576 struct ib_umad_port
*port
;
577 struct ib_umad_file
*file
;
580 spin_lock(&port_lock
);
581 port
= umad_port
[iminor(inode
) - IB_UMAD_MINOR_BASE
];
583 kref_get(&port
->umad_dev
->ref
);
584 spin_unlock(&port_lock
);
589 down_write(&port
->mutex
);
596 file
= kzalloc(sizeof *file
, GFP_KERNEL
);
598 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
603 spin_lock_init(&file
->recv_lock
);
604 INIT_LIST_HEAD(&file
->recv_list
);
605 init_waitqueue_head(&file
->recv_wait
);
608 filp
->private_data
= file
;
610 list_add_tail(&file
->port_list
, &port
->file_list
);
613 up_write(&port
->mutex
);
617 static int ib_umad_close(struct inode
*inode
, struct file
*filp
)
619 struct ib_umad_file
*file
= filp
->private_data
;
620 struct ib_umad_device
*dev
= file
->port
->umad_dev
;
621 struct ib_umad_packet
*packet
, *tmp
;
625 down_write(&file
->port
->mutex
);
627 already_dead
= file
->agents_dead
;
628 file
->agents_dead
= 1;
630 list_for_each_entry_safe(packet
, tmp
, &file
->recv_list
, list
)
633 list_del(&file
->port_list
);
635 downgrade_write(&file
->port
->mutex
);
638 for (i
= 0; i
< IB_UMAD_MAX_AGENTS
; ++i
)
640 ib_unregister_mad_agent(file
->agent
[i
]);
642 up_read(&file
->port
->mutex
);
645 kref_put(&dev
->ref
, ib_umad_release_dev
);
650 static struct file_operations umad_fops
= {
651 .owner
= THIS_MODULE
,
652 .read
= ib_umad_read
,
653 .write
= ib_umad_write
,
654 .poll
= ib_umad_poll
,
655 .unlocked_ioctl
= ib_umad_ioctl
,
656 .compat_ioctl
= ib_umad_ioctl
,
657 .open
= ib_umad_open
,
658 .release
= ib_umad_close
661 static int ib_umad_sm_open(struct inode
*inode
, struct file
*filp
)
663 struct ib_umad_port
*port
;
664 struct ib_port_modify props
= {
665 .set_port_cap_mask
= IB_PORT_SM
669 spin_lock(&port_lock
);
670 port
= umad_port
[iminor(inode
) - IB_UMAD_MINOR_BASE
- IB_UMAD_MAX_PORTS
];
672 kref_get(&port
->umad_dev
->ref
);
673 spin_unlock(&port_lock
);
678 if (filp
->f_flags
& O_NONBLOCK
) {
679 if (down_trylock(&port
->sm_sem
)) {
684 if (down_interruptible(&port
->sm_sem
)) {
690 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
696 filp
->private_data
= port
;
701 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
705 static int ib_umad_sm_close(struct inode
*inode
, struct file
*filp
)
707 struct ib_umad_port
*port
= filp
->private_data
;
708 struct ib_port_modify props
= {
709 .clr_port_cap_mask
= IB_PORT_SM
713 down_write(&port
->mutex
);
715 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
716 up_write(&port
->mutex
);
720 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
725 static struct file_operations umad_sm_fops
= {
726 .owner
= THIS_MODULE
,
727 .open
= ib_umad_sm_open
,
728 .release
= ib_umad_sm_close
731 static struct ib_client umad_client
= {
733 .add
= ib_umad_add_one
,
734 .remove
= ib_umad_remove_one
737 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
739 struct ib_umad_port
*port
= class_get_devdata(class_dev
);
744 return sprintf(buf
, "%s\n", port
->ib_dev
->name
);
746 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
748 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
750 struct ib_umad_port
*port
= class_get_devdata(class_dev
);
755 return sprintf(buf
, "%d\n", port
->port_num
);
757 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
759 static ssize_t
show_abi_version(struct class *class, char *buf
)
761 return sprintf(buf
, "%d\n", IB_USER_MAD_ABI_VERSION
);
763 static CLASS_ATTR(abi_version
, S_IRUGO
, show_abi_version
, NULL
);
765 static int ib_umad_init_port(struct ib_device
*device
, int port_num
,
766 struct ib_umad_port
*port
)
768 spin_lock(&port_lock
);
769 port
->dev_num
= find_first_zero_bit(dev_map
, IB_UMAD_MAX_PORTS
);
770 if (port
->dev_num
>= IB_UMAD_MAX_PORTS
) {
771 spin_unlock(&port_lock
);
774 set_bit(port
->dev_num
, dev_map
);
775 spin_unlock(&port_lock
);
777 port
->ib_dev
= device
;
778 port
->port_num
= port_num
;
779 init_MUTEX(&port
->sm_sem
);
780 init_rwsem(&port
->mutex
);
781 INIT_LIST_HEAD(&port
->file_list
);
783 port
->dev
= cdev_alloc();
786 port
->dev
->owner
= THIS_MODULE
;
787 port
->dev
->ops
= &umad_fops
;
788 kobject_set_name(&port
->dev
->kobj
, "umad%d", port
->dev_num
);
789 if (cdev_add(port
->dev
, base_dev
+ port
->dev_num
, 1))
792 port
->class_dev
= class_device_create(umad_class
, NULL
, port
->dev
->dev
,
794 "umad%d", port
->dev_num
);
795 if (IS_ERR(port
->class_dev
))
798 if (class_device_create_file(port
->class_dev
, &class_device_attr_ibdev
))
800 if (class_device_create_file(port
->class_dev
, &class_device_attr_port
))
803 port
->sm_dev
= cdev_alloc();
806 port
->sm_dev
->owner
= THIS_MODULE
;
807 port
->sm_dev
->ops
= &umad_sm_fops
;
808 kobject_set_name(&port
->sm_dev
->kobj
, "issm%d", port
->dev_num
);
809 if (cdev_add(port
->sm_dev
, base_dev
+ port
->dev_num
+ IB_UMAD_MAX_PORTS
, 1))
812 port
->sm_class_dev
= class_device_create(umad_class
, NULL
, port
->sm_dev
->dev
,
814 "issm%d", port
->dev_num
);
815 if (IS_ERR(port
->sm_class_dev
))
818 class_set_devdata(port
->class_dev
, port
);
819 class_set_devdata(port
->sm_class_dev
, port
);
821 if (class_device_create_file(port
->sm_class_dev
, &class_device_attr_ibdev
))
823 if (class_device_create_file(port
->sm_class_dev
, &class_device_attr_port
))
826 spin_lock(&port_lock
);
827 umad_port
[port
->dev_num
] = port
;
828 spin_unlock(&port_lock
);
833 class_device_destroy(umad_class
, port
->sm_dev
->dev
);
836 cdev_del(port
->sm_dev
);
839 class_device_destroy(umad_class
, port
->dev
->dev
);
843 clear_bit(port
->dev_num
, dev_map
);
848 static void ib_umad_kill_port(struct ib_umad_port
*port
)
850 struct ib_umad_file
*file
;
853 class_set_devdata(port
->class_dev
, NULL
);
854 class_set_devdata(port
->sm_class_dev
, NULL
);
856 class_device_destroy(umad_class
, port
->dev
->dev
);
857 class_device_destroy(umad_class
, port
->sm_dev
->dev
);
860 cdev_del(port
->sm_dev
);
862 spin_lock(&port_lock
);
863 umad_port
[port
->dev_num
] = NULL
;
864 spin_unlock(&port_lock
);
866 down_write(&port
->mutex
);
871 * Now go through the list of files attached to this port and
872 * unregister all of their MAD agents. We need to hold
873 * port->mutex while doing this to avoid racing with
874 * ib_umad_close(), but we can't hold the mutex for writing
875 * while calling ib_unregister_mad_agent(), since that might
876 * deadlock by calling back into queue_packet(). So we
877 * downgrade our lock to a read lock, and then drop and
878 * reacquire the write lock for the next iteration.
880 * We do list_del_init() on the file's list_head so that the
881 * list_del in ib_umad_close() is still OK, even after the
882 * file is removed from the list.
884 while (!list_empty(&port
->file_list
)) {
885 file
= list_entry(port
->file_list
.next
, struct ib_umad_file
,
888 file
->agents_dead
= 1;
889 list_del_init(&file
->port_list
);
891 downgrade_write(&port
->mutex
);
893 for (id
= 0; id
< IB_UMAD_MAX_AGENTS
; ++id
)
895 ib_unregister_mad_agent(file
->agent
[id
]);
897 up_read(&port
->mutex
);
898 down_write(&port
->mutex
);
901 up_write(&port
->mutex
);
903 clear_bit(port
->dev_num
, dev_map
);
906 static void ib_umad_add_one(struct ib_device
*device
)
908 struct ib_umad_device
*umad_dev
;
911 if (device
->node_type
== IB_NODE_SWITCH
)
915 e
= device
->phys_port_cnt
;
918 umad_dev
= kzalloc(sizeof *umad_dev
+
919 (e
- s
+ 1) * sizeof (struct ib_umad_port
),
924 kref_init(&umad_dev
->ref
);
926 umad_dev
->start_port
= s
;
927 umad_dev
->end_port
= e
;
929 for (i
= s
; i
<= e
; ++i
) {
930 umad_dev
->port
[i
- s
].umad_dev
= umad_dev
;
932 if (ib_umad_init_port(device
, i
, &umad_dev
->port
[i
- s
]))
936 ib_set_client_data(device
, &umad_client
, umad_dev
);
942 ib_umad_kill_port(&umad_dev
->port
[i
- s
]);
944 kref_put(&umad_dev
->ref
, ib_umad_release_dev
);
947 static void ib_umad_remove_one(struct ib_device
*device
)
949 struct ib_umad_device
*umad_dev
= ib_get_client_data(device
, &umad_client
);
955 for (i
= 0; i
<= umad_dev
->end_port
- umad_dev
->start_port
; ++i
)
956 ib_umad_kill_port(&umad_dev
->port
[i
]);
958 kref_put(&umad_dev
->ref
, ib_umad_release_dev
);
961 static int __init
ib_umad_init(void)
965 ret
= register_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2,
968 printk(KERN_ERR
"user_mad: couldn't register device number\n");
972 umad_class
= class_create(THIS_MODULE
, "infiniband_mad");
973 if (IS_ERR(umad_class
)) {
974 ret
= PTR_ERR(umad_class
);
975 printk(KERN_ERR
"user_mad: couldn't create class infiniband_mad\n");
979 ret
= class_create_file(umad_class
, &class_attr_abi_version
);
981 printk(KERN_ERR
"user_mad: couldn't create abi_version attribute\n");
985 ret
= ib_register_client(&umad_client
);
987 printk(KERN_ERR
"user_mad: couldn't register ib_umad client\n");
994 class_destroy(umad_class
);
997 unregister_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2);
1003 static void __exit
ib_umad_cleanup(void)
1005 ib_unregister_client(&umad_client
);
1006 class_destroy(umad_class
);
1007 unregister_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2);
1010 module_init(ib_umad_init
);
1011 module_exit(ib_umad_cleanup
);