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 5596 2006-03-03 01:00:07Z sean.hefty $
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 ib_mad_recv_wc
*recv_wc
;
125 struct list_head list
;
127 struct ib_user_mad mad
;
130 static struct class *umad_class
;
132 static const dev_t base_dev
= MKDEV(IB_UMAD_MAJOR
, IB_UMAD_MINOR_BASE
);
134 static DEFINE_SPINLOCK(port_lock
);
135 static struct ib_umad_port
*umad_port
[IB_UMAD_MAX_PORTS
];
136 static DECLARE_BITMAP(dev_map
, IB_UMAD_MAX_PORTS
* 2);
138 static void ib_umad_add_one(struct ib_device
*device
);
139 static void ib_umad_remove_one(struct ib_device
*device
);
141 static void ib_umad_release_dev(struct kref
*ref
)
143 struct ib_umad_device
*dev
=
144 container_of(ref
, struct ib_umad_device
, ref
);
149 /* caller must hold port->mutex at least for reading */
150 static struct ib_mad_agent
*__get_agent(struct ib_umad_file
*file
, int id
)
152 return file
->agents_dead
? NULL
: file
->agent
[id
];
155 static int queue_packet(struct ib_umad_file
*file
,
156 struct ib_mad_agent
*agent
,
157 struct ib_umad_packet
*packet
)
161 down_read(&file
->port
->mutex
);
163 for (packet
->mad
.hdr
.id
= 0;
164 packet
->mad
.hdr
.id
< IB_UMAD_MAX_AGENTS
;
165 packet
->mad
.hdr
.id
++)
166 if (agent
== __get_agent(file
, packet
->mad
.hdr
.id
)) {
167 spin_lock_irq(&file
->recv_lock
);
168 list_add_tail(&packet
->list
, &file
->recv_list
);
169 spin_unlock_irq(&file
->recv_lock
);
170 wake_up_interruptible(&file
->recv_wait
);
175 up_read(&file
->port
->mutex
);
180 static void send_handler(struct ib_mad_agent
*agent
,
181 struct ib_mad_send_wc
*send_wc
)
183 struct ib_umad_file
*file
= agent
->context
;
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 packet
->length
= IB_MGMT_MAD_HDR
;
191 packet
->mad
.hdr
.status
= ETIMEDOUT
;
192 if (!queue_packet(file
, agent
, packet
))
198 static void recv_handler(struct ib_mad_agent
*agent
,
199 struct ib_mad_recv_wc
*mad_recv_wc
)
201 struct ib_umad_file
*file
= agent
->context
;
202 struct ib_umad_packet
*packet
;
204 if (mad_recv_wc
->wc
->status
!= IB_WC_SUCCESS
)
207 packet
= kzalloc(sizeof *packet
, GFP_KERNEL
);
211 packet
->length
= mad_recv_wc
->mad_len
;
212 packet
->recv_wc
= mad_recv_wc
;
214 packet
->mad
.hdr
.status
= 0;
215 packet
->mad
.hdr
.length
= sizeof (struct ib_user_mad
) +
216 mad_recv_wc
->mad_len
;
217 packet
->mad
.hdr
.qpn
= cpu_to_be32(mad_recv_wc
->wc
->src_qp
);
218 packet
->mad
.hdr
.lid
= cpu_to_be16(mad_recv_wc
->wc
->slid
);
219 packet
->mad
.hdr
.sl
= mad_recv_wc
->wc
->sl
;
220 packet
->mad
.hdr
.path_bits
= mad_recv_wc
->wc
->dlid_path_bits
;
221 packet
->mad
.hdr
.grh_present
= !!(mad_recv_wc
->wc
->wc_flags
& IB_WC_GRH
);
222 if (packet
->mad
.hdr
.grh_present
) {
224 packet
->mad
.hdr
.gid_index
= 0;
225 packet
->mad
.hdr
.hop_limit
= 0;
226 packet
->mad
.hdr
.traffic_class
= 0;
227 memset(packet
->mad
.hdr
.gid
, 0, 16);
228 packet
->mad
.hdr
.flow_label
= 0;
231 if (queue_packet(file
, agent
, packet
))
238 ib_free_recv_mad(mad_recv_wc
);
241 static ssize_t
copy_recv_mad(char __user
*buf
, struct ib_umad_packet
*packet
,
244 struct ib_mad_recv_buf
*recv_buf
;
245 int left
, seg_payload
, offset
, max_seg_payload
;
247 /* We need enough room to copy the first (or only) MAD segment. */
248 recv_buf
= &packet
->recv_wc
->recv_buf
;
249 if ((packet
->length
<= sizeof (*recv_buf
->mad
) &&
250 count
< sizeof (packet
->mad
) + packet
->length
) ||
251 (packet
->length
> sizeof (*recv_buf
->mad
) &&
252 count
< sizeof (packet
->mad
) + sizeof (*recv_buf
->mad
)))
255 if (copy_to_user(buf
, &packet
->mad
, sizeof (packet
->mad
)))
258 buf
+= sizeof (packet
->mad
);
259 seg_payload
= min_t(int, packet
->length
, sizeof (*recv_buf
->mad
));
260 if (copy_to_user(buf
, recv_buf
->mad
, seg_payload
))
263 if (seg_payload
< packet
->length
) {
265 * Multipacket RMPP MAD message. Copy remainder of message.
266 * Note that last segment may have a shorter payload.
268 if (count
< sizeof (packet
->mad
) + packet
->length
) {
270 * The buffer is too small, return the first RMPP segment,
271 * which includes the RMPP message length.
275 offset
= ib_get_mad_data_offset(recv_buf
->mad
->mad_hdr
.mgmt_class
);
276 max_seg_payload
= sizeof (struct ib_mad
) - offset
;
278 for (left
= packet
->length
- seg_payload
, buf
+= seg_payload
;
279 left
; left
-= seg_payload
, buf
+= seg_payload
) {
280 recv_buf
= container_of(recv_buf
->list
.next
,
281 struct ib_mad_recv_buf
, list
);
282 seg_payload
= min(left
, max_seg_payload
);
283 if (copy_to_user(buf
, ((void *) recv_buf
->mad
) + offset
,
288 return sizeof (packet
->mad
) + packet
->length
;
291 static ssize_t
copy_send_mad(char __user
*buf
, struct ib_umad_packet
*packet
,
294 ssize_t size
= sizeof (packet
->mad
) + packet
->length
;
299 if (copy_to_user(buf
, &packet
->mad
, size
))
305 static ssize_t
ib_umad_read(struct file
*filp
, char __user
*buf
,
306 size_t count
, loff_t
*pos
)
308 struct ib_umad_file
*file
= filp
->private_data
;
309 struct ib_umad_packet
*packet
;
312 if (count
< sizeof (struct ib_user_mad
))
315 spin_lock_irq(&file
->recv_lock
);
317 while (list_empty(&file
->recv_list
)) {
318 spin_unlock_irq(&file
->recv_lock
);
320 if (filp
->f_flags
& O_NONBLOCK
)
323 if (wait_event_interruptible(file
->recv_wait
,
324 !list_empty(&file
->recv_list
)))
327 spin_lock_irq(&file
->recv_lock
);
330 packet
= list_entry(file
->recv_list
.next
, struct ib_umad_packet
, list
);
331 list_del(&packet
->list
);
333 spin_unlock_irq(&file
->recv_lock
);
336 ret
= copy_recv_mad(buf
, packet
, count
);
338 ret
= copy_send_mad(buf
, packet
, count
);
342 spin_lock_irq(&file
->recv_lock
);
343 list_add(&packet
->list
, &file
->recv_list
);
344 spin_unlock_irq(&file
->recv_lock
);
347 ib_free_recv_mad(packet
->recv_wc
);
353 static int copy_rmpp_mad(struct ib_mad_send_buf
*msg
, const char __user
*buf
)
357 /* Copy class specific header */
358 if ((msg
->hdr_len
> IB_MGMT_RMPP_HDR
) &&
359 copy_from_user(msg
->mad
+ IB_MGMT_RMPP_HDR
, buf
+ IB_MGMT_RMPP_HDR
,
360 msg
->hdr_len
- IB_MGMT_RMPP_HDR
))
363 /* All headers are in place. Copy data segments. */
364 for (seg
= 1, left
= msg
->data_len
, buf
+= msg
->hdr_len
; left
> 0;
365 seg
++, left
-= msg
->seg_size
, buf
+= msg
->seg_size
) {
366 if (copy_from_user(ib_get_rmpp_segment(msg
, seg
), buf
,
367 min(left
, msg
->seg_size
)))
373 static ssize_t
ib_umad_write(struct file
*filp
, const char __user
*buf
,
374 size_t count
, loff_t
*pos
)
376 struct ib_umad_file
*file
= filp
->private_data
;
377 struct ib_umad_packet
*packet
;
378 struct ib_mad_agent
*agent
;
379 struct ib_ah_attr ah_attr
;
381 struct ib_rmpp_mad
*rmpp_mad
;
384 int ret
, data_len
, hdr_len
, copy_offset
, rmpp_active
;
386 if (count
< sizeof (struct ib_user_mad
) + IB_MGMT_RMPP_HDR
)
389 packet
= kzalloc(sizeof *packet
+ IB_MGMT_RMPP_HDR
, GFP_KERNEL
);
393 if (copy_from_user(&packet
->mad
, buf
,
394 sizeof (struct ib_user_mad
) + IB_MGMT_RMPP_HDR
)) {
399 if (packet
->mad
.hdr
.id
< 0 ||
400 packet
->mad
.hdr
.id
>= IB_UMAD_MAX_AGENTS
) {
405 down_read(&file
->port
->mutex
);
407 agent
= __get_agent(file
, packet
->mad
.hdr
.id
);
413 memset(&ah_attr
, 0, sizeof ah_attr
);
414 ah_attr
.dlid
= be16_to_cpu(packet
->mad
.hdr
.lid
);
415 ah_attr
.sl
= packet
->mad
.hdr
.sl
;
416 ah_attr
.src_path_bits
= packet
->mad
.hdr
.path_bits
;
417 ah_attr
.port_num
= file
->port
->port_num
;
418 if (packet
->mad
.hdr
.grh_present
) {
419 ah_attr
.ah_flags
= IB_AH_GRH
;
420 memcpy(ah_attr
.grh
.dgid
.raw
, packet
->mad
.hdr
.gid
, 16);
421 ah_attr
.grh
.flow_label
= be32_to_cpu(packet
->mad
.hdr
.flow_label
);
422 ah_attr
.grh
.hop_limit
= packet
->mad
.hdr
.hop_limit
;
423 ah_attr
.grh
.traffic_class
= packet
->mad
.hdr
.traffic_class
;
426 ah
= ib_create_ah(agent
->qp
->pd
, &ah_attr
);
432 rmpp_mad
= (struct ib_rmpp_mad
*) packet
->mad
.data
;
433 hdr_len
= ib_get_mad_data_offset(rmpp_mad
->mad_hdr
.mgmt_class
);
434 if (!ib_is_mad_class_rmpp(rmpp_mad
->mad_hdr
.mgmt_class
)) {
435 copy_offset
= IB_MGMT_MAD_HDR
;
438 copy_offset
= IB_MGMT_RMPP_HDR
;
439 rmpp_active
= ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
440 IB_MGMT_RMPP_FLAG_ACTIVE
;
443 data_len
= count
- sizeof (struct ib_user_mad
) - hdr_len
;
444 packet
->msg
= ib_create_send_mad(agent
,
445 be32_to_cpu(packet
->mad
.hdr
.qpn
),
446 0, rmpp_active
, hdr_len
,
447 data_len
, GFP_KERNEL
);
448 if (IS_ERR(packet
->msg
)) {
449 ret
= PTR_ERR(packet
->msg
);
453 packet
->msg
->ah
= ah
;
454 packet
->msg
->timeout_ms
= packet
->mad
.hdr
.timeout_ms
;
455 packet
->msg
->retries
= packet
->mad
.hdr
.retries
;
456 packet
->msg
->context
[0] = packet
;
458 /* Copy MAD header. Any RMPP header is already in place. */
459 memcpy(packet
->msg
->mad
, packet
->mad
.data
, IB_MGMT_MAD_HDR
);
460 buf
+= sizeof (struct ib_user_mad
);
463 if (copy_from_user(packet
->msg
->mad
+ copy_offset
,
465 hdr_len
+ data_len
- copy_offset
)) {
470 ret
= copy_rmpp_mad(packet
->msg
, buf
);
476 * If userspace is generating a request that will generate a
477 * response, we need to make sure the high-order part of the
478 * transaction ID matches the agent being used to send the
481 method
= ((struct ib_mad_hdr
*) packet
->msg
->mad
)->method
;
483 if (!(method
& IB_MGMT_METHOD_RESP
) &&
484 method
!= IB_MGMT_METHOD_TRAP_REPRESS
&&
485 method
!= IB_MGMT_METHOD_SEND
) {
486 tid
= &((struct ib_mad_hdr
*) packet
->msg
->mad
)->tid
;
487 *tid
= cpu_to_be64(((u64
) agent
->hi_tid
) << 32 |
488 (be64_to_cpup(tid
) & 0xffffffff));
491 ret
= ib_post_send_mad(packet
->msg
, NULL
);
495 up_read(&file
->port
->mutex
);
499 ib_free_send_mad(packet
->msg
);
503 up_read(&file
->port
->mutex
);
509 static unsigned int ib_umad_poll(struct file
*filp
, struct poll_table_struct
*wait
)
511 struct ib_umad_file
*file
= filp
->private_data
;
513 /* we will always be able to post a MAD send */
514 unsigned int mask
= POLLOUT
| POLLWRNORM
;
516 poll_wait(filp
, &file
->recv_wait
, wait
);
518 if (!list_empty(&file
->recv_list
))
519 mask
|= POLLIN
| POLLRDNORM
;
524 static int ib_umad_reg_agent(struct ib_umad_file
*file
, unsigned long arg
)
526 struct ib_user_mad_reg_req ureq
;
527 struct ib_mad_reg_req req
;
528 struct ib_mad_agent
*agent
;
532 down_write(&file
->port
->mutex
);
534 if (!file
->port
->ib_dev
) {
539 if (copy_from_user(&ureq
, (void __user
*) arg
, sizeof ureq
)) {
544 if (ureq
.qpn
!= 0 && ureq
.qpn
!= 1) {
549 for (agent_id
= 0; agent_id
< IB_UMAD_MAX_AGENTS
; ++agent_id
)
550 if (!__get_agent(file
, agent_id
))
557 if (ureq
.mgmt_class
) {
558 req
.mgmt_class
= ureq
.mgmt_class
;
559 req
.mgmt_class_version
= ureq
.mgmt_class_version
;
560 memcpy(req
.method_mask
, ureq
.method_mask
, sizeof req
.method_mask
);
561 memcpy(req
.oui
, ureq
.oui
, sizeof req
.oui
);
564 agent
= ib_register_mad_agent(file
->port
->ib_dev
, file
->port
->port_num
,
565 ureq
.qpn
? IB_QPT_GSI
: IB_QPT_SMI
,
566 ureq
.mgmt_class
? &req
: NULL
,
568 send_handler
, recv_handler
, file
);
570 ret
= PTR_ERR(agent
);
574 if (put_user(agent_id
,
575 (u32 __user
*) (arg
+ offsetof(struct ib_user_mad_reg_req
, id
)))) {
577 ib_unregister_mad_agent(agent
);
581 file
->agent
[agent_id
] = agent
;
585 up_write(&file
->port
->mutex
);
589 static int ib_umad_unreg_agent(struct ib_umad_file
*file
, unsigned long arg
)
591 struct ib_mad_agent
*agent
= NULL
;
595 if (get_user(id
, (u32 __user
*) arg
))
598 down_write(&file
->port
->mutex
);
600 if (id
< 0 || id
>= IB_UMAD_MAX_AGENTS
|| !__get_agent(file
, id
)) {
605 agent
= file
->agent
[id
];
606 file
->agent
[id
] = NULL
;
609 up_write(&file
->port
->mutex
);
612 ib_unregister_mad_agent(agent
);
617 static long ib_umad_ioctl(struct file
*filp
, unsigned int cmd
,
621 case IB_USER_MAD_REGISTER_AGENT
:
622 return ib_umad_reg_agent(filp
->private_data
, arg
);
623 case IB_USER_MAD_UNREGISTER_AGENT
:
624 return ib_umad_unreg_agent(filp
->private_data
, arg
);
630 static int ib_umad_open(struct inode
*inode
, struct file
*filp
)
632 struct ib_umad_port
*port
;
633 struct ib_umad_file
*file
;
636 spin_lock(&port_lock
);
637 port
= umad_port
[iminor(inode
) - IB_UMAD_MINOR_BASE
];
639 kref_get(&port
->umad_dev
->ref
);
640 spin_unlock(&port_lock
);
645 down_write(&port
->mutex
);
652 file
= kzalloc(sizeof *file
, GFP_KERNEL
);
654 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
659 spin_lock_init(&file
->recv_lock
);
660 INIT_LIST_HEAD(&file
->recv_list
);
661 init_waitqueue_head(&file
->recv_wait
);
664 filp
->private_data
= file
;
666 list_add_tail(&file
->port_list
, &port
->file_list
);
669 up_write(&port
->mutex
);
673 static int ib_umad_close(struct inode
*inode
, struct file
*filp
)
675 struct ib_umad_file
*file
= filp
->private_data
;
676 struct ib_umad_device
*dev
= file
->port
->umad_dev
;
677 struct ib_umad_packet
*packet
, *tmp
;
681 down_write(&file
->port
->mutex
);
683 already_dead
= file
->agents_dead
;
684 file
->agents_dead
= 1;
686 list_for_each_entry_safe(packet
, tmp
, &file
->recv_list
, list
) {
688 ib_free_recv_mad(packet
->recv_wc
);
692 list_del(&file
->port_list
);
694 downgrade_write(&file
->port
->mutex
);
697 for (i
= 0; i
< IB_UMAD_MAX_AGENTS
; ++i
)
699 ib_unregister_mad_agent(file
->agent
[i
]);
701 up_read(&file
->port
->mutex
);
704 kref_put(&dev
->ref
, ib_umad_release_dev
);
709 static struct file_operations umad_fops
= {
710 .owner
= THIS_MODULE
,
711 .read
= ib_umad_read
,
712 .write
= ib_umad_write
,
713 .poll
= ib_umad_poll
,
714 .unlocked_ioctl
= ib_umad_ioctl
,
715 .compat_ioctl
= ib_umad_ioctl
,
716 .open
= ib_umad_open
,
717 .release
= ib_umad_close
720 static int ib_umad_sm_open(struct inode
*inode
, struct file
*filp
)
722 struct ib_umad_port
*port
;
723 struct ib_port_modify props
= {
724 .set_port_cap_mask
= IB_PORT_SM
728 spin_lock(&port_lock
);
729 port
= umad_port
[iminor(inode
) - IB_UMAD_MINOR_BASE
- IB_UMAD_MAX_PORTS
];
731 kref_get(&port
->umad_dev
->ref
);
732 spin_unlock(&port_lock
);
737 if (filp
->f_flags
& O_NONBLOCK
) {
738 if (down_trylock(&port
->sm_sem
)) {
743 if (down_interruptible(&port
->sm_sem
)) {
749 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
755 filp
->private_data
= port
;
760 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
764 static int ib_umad_sm_close(struct inode
*inode
, struct file
*filp
)
766 struct ib_umad_port
*port
= filp
->private_data
;
767 struct ib_port_modify props
= {
768 .clr_port_cap_mask
= IB_PORT_SM
772 down_write(&port
->mutex
);
774 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
775 up_write(&port
->mutex
);
779 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
784 static struct file_operations umad_sm_fops
= {
785 .owner
= THIS_MODULE
,
786 .open
= ib_umad_sm_open
,
787 .release
= ib_umad_sm_close
790 static struct ib_client umad_client
= {
792 .add
= ib_umad_add_one
,
793 .remove
= ib_umad_remove_one
796 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
798 struct ib_umad_port
*port
= class_get_devdata(class_dev
);
803 return sprintf(buf
, "%s\n", port
->ib_dev
->name
);
805 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
807 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
809 struct ib_umad_port
*port
= class_get_devdata(class_dev
);
814 return sprintf(buf
, "%d\n", port
->port_num
);
816 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
818 static ssize_t
show_abi_version(struct class *class, char *buf
)
820 return sprintf(buf
, "%d\n", IB_USER_MAD_ABI_VERSION
);
822 static CLASS_ATTR(abi_version
, S_IRUGO
, show_abi_version
, NULL
);
824 static int ib_umad_init_port(struct ib_device
*device
, int port_num
,
825 struct ib_umad_port
*port
)
827 spin_lock(&port_lock
);
828 port
->dev_num
= find_first_zero_bit(dev_map
, IB_UMAD_MAX_PORTS
);
829 if (port
->dev_num
>= IB_UMAD_MAX_PORTS
) {
830 spin_unlock(&port_lock
);
833 set_bit(port
->dev_num
, dev_map
);
834 spin_unlock(&port_lock
);
836 port
->ib_dev
= device
;
837 port
->port_num
= port_num
;
838 init_MUTEX(&port
->sm_sem
);
839 init_rwsem(&port
->mutex
);
840 INIT_LIST_HEAD(&port
->file_list
);
842 port
->dev
= cdev_alloc();
845 port
->dev
->owner
= THIS_MODULE
;
846 port
->dev
->ops
= &umad_fops
;
847 kobject_set_name(&port
->dev
->kobj
, "umad%d", port
->dev_num
);
848 if (cdev_add(port
->dev
, base_dev
+ port
->dev_num
, 1))
851 port
->class_dev
= class_device_create(umad_class
, NULL
, port
->dev
->dev
,
853 "umad%d", port
->dev_num
);
854 if (IS_ERR(port
->class_dev
))
857 if (class_device_create_file(port
->class_dev
, &class_device_attr_ibdev
))
859 if (class_device_create_file(port
->class_dev
, &class_device_attr_port
))
862 port
->sm_dev
= cdev_alloc();
865 port
->sm_dev
->owner
= THIS_MODULE
;
866 port
->sm_dev
->ops
= &umad_sm_fops
;
867 kobject_set_name(&port
->sm_dev
->kobj
, "issm%d", port
->dev_num
);
868 if (cdev_add(port
->sm_dev
, base_dev
+ port
->dev_num
+ IB_UMAD_MAX_PORTS
, 1))
871 port
->sm_class_dev
= class_device_create(umad_class
, NULL
, port
->sm_dev
->dev
,
873 "issm%d", port
->dev_num
);
874 if (IS_ERR(port
->sm_class_dev
))
877 class_set_devdata(port
->class_dev
, port
);
878 class_set_devdata(port
->sm_class_dev
, port
);
880 if (class_device_create_file(port
->sm_class_dev
, &class_device_attr_ibdev
))
882 if (class_device_create_file(port
->sm_class_dev
, &class_device_attr_port
))
885 spin_lock(&port_lock
);
886 umad_port
[port
->dev_num
] = port
;
887 spin_unlock(&port_lock
);
892 class_device_destroy(umad_class
, port
->sm_dev
->dev
);
895 cdev_del(port
->sm_dev
);
898 class_device_destroy(umad_class
, port
->dev
->dev
);
902 clear_bit(port
->dev_num
, dev_map
);
907 static void ib_umad_kill_port(struct ib_umad_port
*port
)
909 struct ib_umad_file
*file
;
912 class_set_devdata(port
->class_dev
, NULL
);
913 class_set_devdata(port
->sm_class_dev
, NULL
);
915 class_device_destroy(umad_class
, port
->dev
->dev
);
916 class_device_destroy(umad_class
, port
->sm_dev
->dev
);
919 cdev_del(port
->sm_dev
);
921 spin_lock(&port_lock
);
922 umad_port
[port
->dev_num
] = NULL
;
923 spin_unlock(&port_lock
);
925 down_write(&port
->mutex
);
930 * Now go through the list of files attached to this port and
931 * unregister all of their MAD agents. We need to hold
932 * port->mutex while doing this to avoid racing with
933 * ib_umad_close(), but we can't hold the mutex for writing
934 * while calling ib_unregister_mad_agent(), since that might
935 * deadlock by calling back into queue_packet(). So we
936 * downgrade our lock to a read lock, and then drop and
937 * reacquire the write lock for the next iteration.
939 * We do list_del_init() on the file's list_head so that the
940 * list_del in ib_umad_close() is still OK, even after the
941 * file is removed from the list.
943 while (!list_empty(&port
->file_list
)) {
944 file
= list_entry(port
->file_list
.next
, struct ib_umad_file
,
947 file
->agents_dead
= 1;
948 list_del_init(&file
->port_list
);
950 downgrade_write(&port
->mutex
);
952 for (id
= 0; id
< IB_UMAD_MAX_AGENTS
; ++id
)
954 ib_unregister_mad_agent(file
->agent
[id
]);
956 up_read(&port
->mutex
);
957 down_write(&port
->mutex
);
960 up_write(&port
->mutex
);
962 clear_bit(port
->dev_num
, dev_map
);
965 static void ib_umad_add_one(struct ib_device
*device
)
967 struct ib_umad_device
*umad_dev
;
970 if (device
->node_type
== IB_NODE_SWITCH
)
974 e
= device
->phys_port_cnt
;
977 umad_dev
= kzalloc(sizeof *umad_dev
+
978 (e
- s
+ 1) * sizeof (struct ib_umad_port
),
983 kref_init(&umad_dev
->ref
);
985 umad_dev
->start_port
= s
;
986 umad_dev
->end_port
= e
;
988 for (i
= s
; i
<= e
; ++i
) {
989 umad_dev
->port
[i
- s
].umad_dev
= umad_dev
;
991 if (ib_umad_init_port(device
, i
, &umad_dev
->port
[i
- s
]))
995 ib_set_client_data(device
, &umad_client
, umad_dev
);
1001 ib_umad_kill_port(&umad_dev
->port
[i
- s
]);
1003 kref_put(&umad_dev
->ref
, ib_umad_release_dev
);
1006 static void ib_umad_remove_one(struct ib_device
*device
)
1008 struct ib_umad_device
*umad_dev
= ib_get_client_data(device
, &umad_client
);
1014 for (i
= 0; i
<= umad_dev
->end_port
- umad_dev
->start_port
; ++i
)
1015 ib_umad_kill_port(&umad_dev
->port
[i
]);
1017 kref_put(&umad_dev
->ref
, ib_umad_release_dev
);
1020 static int __init
ib_umad_init(void)
1024 ret
= register_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2,
1027 printk(KERN_ERR
"user_mad: couldn't register device number\n");
1031 umad_class
= class_create(THIS_MODULE
, "infiniband_mad");
1032 if (IS_ERR(umad_class
)) {
1033 ret
= PTR_ERR(umad_class
);
1034 printk(KERN_ERR
"user_mad: couldn't create class infiniband_mad\n");
1038 ret
= class_create_file(umad_class
, &class_attr_abi_version
);
1040 printk(KERN_ERR
"user_mad: couldn't create abi_version attribute\n");
1044 ret
= ib_register_client(&umad_client
);
1046 printk(KERN_ERR
"user_mad: couldn't register ib_umad client\n");
1053 class_destroy(umad_class
);
1056 unregister_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2);
1062 static void __exit
ib_umad_cleanup(void)
1064 ib_unregister_client(&umad_client
);
1065 class_destroy(umad_class
);
1066 unregister_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2);
1069 module_init(ib_umad_init
);
1070 module_exit(ib_umad_cleanup
);