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 send_list
;
116 struct list_head port_list
;
117 spinlock_t recv_lock
;
118 spinlock_t send_lock
;
119 wait_queue_head_t recv_wait
;
120 struct ib_mad_agent
*agent
[IB_UMAD_MAX_AGENTS
];
124 struct ib_umad_packet
{
125 struct ib_mad_send_buf
*msg
;
126 struct ib_mad_recv_wc
*recv_wc
;
127 struct list_head list
;
129 struct ib_user_mad mad
;
132 static struct class *umad_class
;
134 static const dev_t base_dev
= MKDEV(IB_UMAD_MAJOR
, IB_UMAD_MINOR_BASE
);
136 static DEFINE_SPINLOCK(port_lock
);
137 static struct ib_umad_port
*umad_port
[IB_UMAD_MAX_PORTS
];
138 static DECLARE_BITMAP(dev_map
, IB_UMAD_MAX_PORTS
);
140 static void ib_umad_add_one(struct ib_device
*device
);
141 static void ib_umad_remove_one(struct ib_device
*device
);
143 static void ib_umad_release_dev(struct kref
*ref
)
145 struct ib_umad_device
*dev
=
146 container_of(ref
, struct ib_umad_device
, ref
);
151 /* caller must hold port->mutex at least for reading */
152 static struct ib_mad_agent
*__get_agent(struct ib_umad_file
*file
, int id
)
154 return file
->agents_dead
? NULL
: file
->agent
[id
];
157 static int queue_packet(struct ib_umad_file
*file
,
158 struct ib_mad_agent
*agent
,
159 struct ib_umad_packet
*packet
)
163 down_read(&file
->port
->mutex
);
165 for (packet
->mad
.hdr
.id
= 0;
166 packet
->mad
.hdr
.id
< IB_UMAD_MAX_AGENTS
;
167 packet
->mad
.hdr
.id
++)
168 if (agent
== __get_agent(file
, packet
->mad
.hdr
.id
)) {
169 spin_lock_irq(&file
->recv_lock
);
170 list_add_tail(&packet
->list
, &file
->recv_list
);
171 spin_unlock_irq(&file
->recv_lock
);
172 wake_up_interruptible(&file
->recv_wait
);
177 up_read(&file
->port
->mutex
);
182 static void dequeue_send(struct ib_umad_file
*file
,
183 struct ib_umad_packet
*packet
)
185 spin_lock_irq(&file
->send_lock
);
186 list_del(&packet
->list
);
187 spin_unlock_irq(&file
->send_lock
);
190 static void send_handler(struct ib_mad_agent
*agent
,
191 struct ib_mad_send_wc
*send_wc
)
193 struct ib_umad_file
*file
= agent
->context
;
194 struct ib_umad_packet
*packet
= send_wc
->send_buf
->context
[0];
196 dequeue_send(file
, packet
);
197 ib_destroy_ah(packet
->msg
->ah
);
198 ib_free_send_mad(packet
->msg
);
200 if (send_wc
->status
== IB_WC_RESP_TIMEOUT_ERR
) {
201 packet
->length
= IB_MGMT_MAD_HDR
;
202 packet
->mad
.hdr
.status
= ETIMEDOUT
;
203 if (!queue_packet(file
, agent
, packet
))
209 static void recv_handler(struct ib_mad_agent
*agent
,
210 struct ib_mad_recv_wc
*mad_recv_wc
)
212 struct ib_umad_file
*file
= agent
->context
;
213 struct ib_umad_packet
*packet
;
215 if (mad_recv_wc
->wc
->status
!= IB_WC_SUCCESS
)
218 packet
= kzalloc(sizeof *packet
, GFP_KERNEL
);
222 packet
->length
= mad_recv_wc
->mad_len
;
223 packet
->recv_wc
= mad_recv_wc
;
225 packet
->mad
.hdr
.status
= 0;
226 packet
->mad
.hdr
.length
= sizeof (struct ib_user_mad
) +
227 mad_recv_wc
->mad_len
;
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
) {
234 struct ib_ah_attr ah_attr
;
236 ib_init_ah_from_wc(agent
->device
, agent
->port_num
,
237 mad_recv_wc
->wc
, mad_recv_wc
->recv_buf
.grh
,
240 packet
->mad
.hdr
.gid_index
= ah_attr
.grh
.sgid_index
;
241 packet
->mad
.hdr
.hop_limit
= ah_attr
.grh
.hop_limit
;
242 packet
->mad
.hdr
.traffic_class
= ah_attr
.grh
.traffic_class
;
243 memcpy(packet
->mad
.hdr
.gid
, &ah_attr
.grh
.dgid
, 16);
244 packet
->mad
.hdr
.flow_label
= cpu_to_be32(ah_attr
.grh
.flow_label
);
247 if (queue_packet(file
, agent
, packet
))
254 ib_free_recv_mad(mad_recv_wc
);
257 static ssize_t
copy_recv_mad(char __user
*buf
, struct ib_umad_packet
*packet
,
260 struct ib_mad_recv_buf
*recv_buf
;
261 int left
, seg_payload
, offset
, max_seg_payload
;
263 /* We need enough room to copy the first (or only) MAD segment. */
264 recv_buf
= &packet
->recv_wc
->recv_buf
;
265 if ((packet
->length
<= sizeof (*recv_buf
->mad
) &&
266 count
< sizeof (packet
->mad
) + packet
->length
) ||
267 (packet
->length
> sizeof (*recv_buf
->mad
) &&
268 count
< sizeof (packet
->mad
) + sizeof (*recv_buf
->mad
)))
271 if (copy_to_user(buf
, &packet
->mad
, sizeof (packet
->mad
)))
274 buf
+= sizeof (packet
->mad
);
275 seg_payload
= min_t(int, packet
->length
, sizeof (*recv_buf
->mad
));
276 if (copy_to_user(buf
, recv_buf
->mad
, seg_payload
))
279 if (seg_payload
< packet
->length
) {
281 * Multipacket RMPP MAD message. Copy remainder of message.
282 * Note that last segment may have a shorter payload.
284 if (count
< sizeof (packet
->mad
) + packet
->length
) {
286 * The buffer is too small, return the first RMPP segment,
287 * which includes the RMPP message length.
291 offset
= ib_get_mad_data_offset(recv_buf
->mad
->mad_hdr
.mgmt_class
);
292 max_seg_payload
= sizeof (struct ib_mad
) - offset
;
294 for (left
= packet
->length
- seg_payload
, buf
+= seg_payload
;
295 left
; left
-= seg_payload
, buf
+= seg_payload
) {
296 recv_buf
= container_of(recv_buf
->list
.next
,
297 struct ib_mad_recv_buf
, list
);
298 seg_payload
= min(left
, max_seg_payload
);
299 if (copy_to_user(buf
, ((void *) recv_buf
->mad
) + offset
,
304 return sizeof (packet
->mad
) + packet
->length
;
307 static ssize_t
copy_send_mad(char __user
*buf
, struct ib_umad_packet
*packet
,
310 ssize_t size
= sizeof (packet
->mad
) + packet
->length
;
315 if (copy_to_user(buf
, &packet
->mad
, size
))
321 static ssize_t
ib_umad_read(struct file
*filp
, char __user
*buf
,
322 size_t count
, loff_t
*pos
)
324 struct ib_umad_file
*file
= filp
->private_data
;
325 struct ib_umad_packet
*packet
;
328 if (count
< sizeof (struct ib_user_mad
))
331 spin_lock_irq(&file
->recv_lock
);
333 while (list_empty(&file
->recv_list
)) {
334 spin_unlock_irq(&file
->recv_lock
);
336 if (filp
->f_flags
& O_NONBLOCK
)
339 if (wait_event_interruptible(file
->recv_wait
,
340 !list_empty(&file
->recv_list
)))
343 spin_lock_irq(&file
->recv_lock
);
346 packet
= list_entry(file
->recv_list
.next
, struct ib_umad_packet
, list
);
347 list_del(&packet
->list
);
349 spin_unlock_irq(&file
->recv_lock
);
352 ret
= copy_recv_mad(buf
, packet
, count
);
354 ret
= copy_send_mad(buf
, packet
, count
);
358 spin_lock_irq(&file
->recv_lock
);
359 list_add(&packet
->list
, &file
->recv_list
);
360 spin_unlock_irq(&file
->recv_lock
);
363 ib_free_recv_mad(packet
->recv_wc
);
369 static int copy_rmpp_mad(struct ib_mad_send_buf
*msg
, const char __user
*buf
)
373 /* Copy class specific header */
374 if ((msg
->hdr_len
> IB_MGMT_RMPP_HDR
) &&
375 copy_from_user(msg
->mad
+ IB_MGMT_RMPP_HDR
, buf
+ IB_MGMT_RMPP_HDR
,
376 msg
->hdr_len
- IB_MGMT_RMPP_HDR
))
379 /* All headers are in place. Copy data segments. */
380 for (seg
= 1, left
= msg
->data_len
, buf
+= msg
->hdr_len
; left
> 0;
381 seg
++, left
-= msg
->seg_size
, buf
+= msg
->seg_size
) {
382 if (copy_from_user(ib_get_rmpp_segment(msg
, seg
), buf
,
383 min(left
, msg
->seg_size
)))
389 static int same_destination(struct ib_user_mad_hdr
*hdr1
,
390 struct ib_user_mad_hdr
*hdr2
)
392 if (!hdr1
->grh_present
&& !hdr2
->grh_present
)
393 return (hdr1
->lid
== hdr2
->lid
);
395 if (hdr1
->grh_present
&& hdr2
->grh_present
)
396 return !memcmp(hdr1
->gid
, hdr2
->gid
, 16);
401 static int is_duplicate(struct ib_umad_file
*file
,
402 struct ib_umad_packet
*packet
)
404 struct ib_umad_packet
*sent_packet
;
405 struct ib_mad_hdr
*sent_hdr
, *hdr
;
407 hdr
= (struct ib_mad_hdr
*) packet
->mad
.data
;
408 list_for_each_entry(sent_packet
, &file
->send_list
, list
) {
409 sent_hdr
= (struct ib_mad_hdr
*) sent_packet
->mad
.data
;
411 if ((hdr
->tid
!= sent_hdr
->tid
) ||
412 (hdr
->mgmt_class
!= sent_hdr
->mgmt_class
))
416 * No need to be overly clever here. If two new operations have
417 * the same TID, reject the second as a duplicate. This is more
418 * restrictive than required by the spec.
420 if (!ib_response_mad((struct ib_mad
*) hdr
)) {
421 if (!ib_response_mad((struct ib_mad
*) sent_hdr
))
424 } else if (!ib_response_mad((struct ib_mad
*) sent_hdr
))
427 if (same_destination(&packet
->mad
.hdr
, &sent_packet
->mad
.hdr
))
434 static ssize_t
ib_umad_write(struct file
*filp
, const char __user
*buf
,
435 size_t count
, loff_t
*pos
)
437 struct ib_umad_file
*file
= filp
->private_data
;
438 struct ib_umad_packet
*packet
;
439 struct ib_mad_agent
*agent
;
440 struct ib_ah_attr ah_attr
;
442 struct ib_rmpp_mad
*rmpp_mad
;
444 int ret
, data_len
, hdr_len
, copy_offset
, rmpp_active
;
446 if (count
< sizeof (struct ib_user_mad
) + IB_MGMT_RMPP_HDR
)
449 packet
= kzalloc(sizeof *packet
+ IB_MGMT_RMPP_HDR
, GFP_KERNEL
);
453 if (copy_from_user(&packet
->mad
, buf
,
454 sizeof (struct ib_user_mad
) + IB_MGMT_RMPP_HDR
)) {
459 if (packet
->mad
.hdr
.id
< 0 ||
460 packet
->mad
.hdr
.id
>= IB_UMAD_MAX_AGENTS
) {
465 down_read(&file
->port
->mutex
);
467 agent
= __get_agent(file
, packet
->mad
.hdr
.id
);
473 memset(&ah_attr
, 0, sizeof ah_attr
);
474 ah_attr
.dlid
= be16_to_cpu(packet
->mad
.hdr
.lid
);
475 ah_attr
.sl
= packet
->mad
.hdr
.sl
;
476 ah_attr
.src_path_bits
= packet
->mad
.hdr
.path_bits
;
477 ah_attr
.port_num
= file
->port
->port_num
;
478 if (packet
->mad
.hdr
.grh_present
) {
479 ah_attr
.ah_flags
= IB_AH_GRH
;
480 memcpy(ah_attr
.grh
.dgid
.raw
, packet
->mad
.hdr
.gid
, 16);
481 ah_attr
.grh
.sgid_index
= packet
->mad
.hdr
.gid_index
;
482 ah_attr
.grh
.flow_label
= be32_to_cpu(packet
->mad
.hdr
.flow_label
);
483 ah_attr
.grh
.hop_limit
= packet
->mad
.hdr
.hop_limit
;
484 ah_attr
.grh
.traffic_class
= packet
->mad
.hdr
.traffic_class
;
487 ah
= ib_create_ah(agent
->qp
->pd
, &ah_attr
);
493 rmpp_mad
= (struct ib_rmpp_mad
*) packet
->mad
.data
;
494 hdr_len
= ib_get_mad_data_offset(rmpp_mad
->mad_hdr
.mgmt_class
);
495 if (!ib_is_mad_class_rmpp(rmpp_mad
->mad_hdr
.mgmt_class
)) {
496 copy_offset
= IB_MGMT_MAD_HDR
;
499 copy_offset
= IB_MGMT_RMPP_HDR
;
500 rmpp_active
= ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
501 IB_MGMT_RMPP_FLAG_ACTIVE
;
504 data_len
= count
- sizeof (struct ib_user_mad
) - hdr_len
;
505 packet
->msg
= ib_create_send_mad(agent
,
506 be32_to_cpu(packet
->mad
.hdr
.qpn
),
507 0, rmpp_active
, hdr_len
,
508 data_len
, GFP_KERNEL
);
509 if (IS_ERR(packet
->msg
)) {
510 ret
= PTR_ERR(packet
->msg
);
514 packet
->msg
->ah
= ah
;
515 packet
->msg
->timeout_ms
= packet
->mad
.hdr
.timeout_ms
;
516 packet
->msg
->retries
= packet
->mad
.hdr
.retries
;
517 packet
->msg
->context
[0] = packet
;
519 /* Copy MAD header. Any RMPP header is already in place. */
520 memcpy(packet
->msg
->mad
, packet
->mad
.data
, IB_MGMT_MAD_HDR
);
521 buf
+= sizeof (struct ib_user_mad
);
524 if (copy_from_user(packet
->msg
->mad
+ copy_offset
,
526 hdr_len
+ data_len
- copy_offset
)) {
531 ret
= copy_rmpp_mad(packet
->msg
, buf
);
537 * Set the high-order part of the transaction ID to make MADs from
538 * different agents unique, and allow routing responses back to the
539 * original requestor.
541 if (!ib_response_mad(packet
->msg
->mad
)) {
542 tid
= &((struct ib_mad_hdr
*) packet
->msg
->mad
)->tid
;
543 *tid
= cpu_to_be64(((u64
) agent
->hi_tid
) << 32 |
544 (be64_to_cpup(tid
) & 0xffffffff));
545 rmpp_mad
->mad_hdr
.tid
= *tid
;
548 spin_lock_irq(&file
->send_lock
);
549 ret
= is_duplicate(file
, packet
);
551 list_add_tail(&packet
->list
, &file
->send_list
);
552 spin_unlock_irq(&file
->send_lock
);
558 ret
= ib_post_send_mad(packet
->msg
, NULL
);
562 up_read(&file
->port
->mutex
);
566 dequeue_send(file
, packet
);
568 ib_free_send_mad(packet
->msg
);
572 up_read(&file
->port
->mutex
);
578 static unsigned int ib_umad_poll(struct file
*filp
, struct poll_table_struct
*wait
)
580 struct ib_umad_file
*file
= filp
->private_data
;
582 /* we will always be able to post a MAD send */
583 unsigned int mask
= POLLOUT
| POLLWRNORM
;
585 poll_wait(filp
, &file
->recv_wait
, wait
);
587 if (!list_empty(&file
->recv_list
))
588 mask
|= POLLIN
| POLLRDNORM
;
593 static int ib_umad_reg_agent(struct ib_umad_file
*file
, unsigned long arg
)
595 struct ib_user_mad_reg_req ureq
;
596 struct ib_mad_reg_req req
;
597 struct ib_mad_agent
*agent
;
601 down_write(&file
->port
->mutex
);
603 if (!file
->port
->ib_dev
) {
608 if (copy_from_user(&ureq
, (void __user
*) arg
, sizeof ureq
)) {
613 if (ureq
.qpn
!= 0 && ureq
.qpn
!= 1) {
618 for (agent_id
= 0; agent_id
< IB_UMAD_MAX_AGENTS
; ++agent_id
)
619 if (!__get_agent(file
, agent_id
))
626 if (ureq
.mgmt_class
) {
627 req
.mgmt_class
= ureq
.mgmt_class
;
628 req
.mgmt_class_version
= ureq
.mgmt_class_version
;
629 memcpy(req
.method_mask
, ureq
.method_mask
, sizeof req
.method_mask
);
630 memcpy(req
.oui
, ureq
.oui
, sizeof req
.oui
);
633 agent
= ib_register_mad_agent(file
->port
->ib_dev
, file
->port
->port_num
,
634 ureq
.qpn
? IB_QPT_GSI
: IB_QPT_SMI
,
635 ureq
.mgmt_class
? &req
: NULL
,
637 send_handler
, recv_handler
, file
);
639 ret
= PTR_ERR(agent
);
643 if (put_user(agent_id
,
644 (u32 __user
*) (arg
+ offsetof(struct ib_user_mad_reg_req
, id
)))) {
646 ib_unregister_mad_agent(agent
);
650 file
->agent
[agent_id
] = agent
;
654 up_write(&file
->port
->mutex
);
658 static int ib_umad_unreg_agent(struct ib_umad_file
*file
, unsigned long arg
)
660 struct ib_mad_agent
*agent
= NULL
;
664 if (get_user(id
, (u32 __user
*) arg
))
667 down_write(&file
->port
->mutex
);
669 if (id
< 0 || id
>= IB_UMAD_MAX_AGENTS
|| !__get_agent(file
, id
)) {
674 agent
= file
->agent
[id
];
675 file
->agent
[id
] = NULL
;
678 up_write(&file
->port
->mutex
);
681 ib_unregister_mad_agent(agent
);
686 static long ib_umad_ioctl(struct file
*filp
, unsigned int cmd
,
690 case IB_USER_MAD_REGISTER_AGENT
:
691 return ib_umad_reg_agent(filp
->private_data
, arg
);
692 case IB_USER_MAD_UNREGISTER_AGENT
:
693 return ib_umad_unreg_agent(filp
->private_data
, arg
);
699 static int ib_umad_open(struct inode
*inode
, struct file
*filp
)
701 struct ib_umad_port
*port
;
702 struct ib_umad_file
*file
;
705 spin_lock(&port_lock
);
706 port
= umad_port
[iminor(inode
) - IB_UMAD_MINOR_BASE
];
708 kref_get(&port
->umad_dev
->ref
);
709 spin_unlock(&port_lock
);
714 down_write(&port
->mutex
);
721 file
= kzalloc(sizeof *file
, GFP_KERNEL
);
723 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
728 spin_lock_init(&file
->recv_lock
);
729 spin_lock_init(&file
->send_lock
);
730 INIT_LIST_HEAD(&file
->recv_list
);
731 INIT_LIST_HEAD(&file
->send_list
);
732 init_waitqueue_head(&file
->recv_wait
);
735 filp
->private_data
= file
;
737 list_add_tail(&file
->port_list
, &port
->file_list
);
740 up_write(&port
->mutex
);
744 static int ib_umad_close(struct inode
*inode
, struct file
*filp
)
746 struct ib_umad_file
*file
= filp
->private_data
;
747 struct ib_umad_device
*dev
= file
->port
->umad_dev
;
748 struct ib_umad_packet
*packet
, *tmp
;
752 down_write(&file
->port
->mutex
);
754 already_dead
= file
->agents_dead
;
755 file
->agents_dead
= 1;
757 list_for_each_entry_safe(packet
, tmp
, &file
->recv_list
, list
) {
759 ib_free_recv_mad(packet
->recv_wc
);
763 list_del(&file
->port_list
);
765 downgrade_write(&file
->port
->mutex
);
768 for (i
= 0; i
< IB_UMAD_MAX_AGENTS
; ++i
)
770 ib_unregister_mad_agent(file
->agent
[i
]);
772 up_read(&file
->port
->mutex
);
775 kref_put(&dev
->ref
, ib_umad_release_dev
);
780 static const struct file_operations umad_fops
= {
781 .owner
= THIS_MODULE
,
782 .read
= ib_umad_read
,
783 .write
= ib_umad_write
,
784 .poll
= ib_umad_poll
,
785 .unlocked_ioctl
= ib_umad_ioctl
,
786 .compat_ioctl
= ib_umad_ioctl
,
787 .open
= ib_umad_open
,
788 .release
= ib_umad_close
791 static int ib_umad_sm_open(struct inode
*inode
, struct file
*filp
)
793 struct ib_umad_port
*port
;
794 struct ib_port_modify props
= {
795 .set_port_cap_mask
= IB_PORT_SM
799 spin_lock(&port_lock
);
800 port
= umad_port
[iminor(inode
) - IB_UMAD_MINOR_BASE
- IB_UMAD_MAX_PORTS
];
802 kref_get(&port
->umad_dev
->ref
);
803 spin_unlock(&port_lock
);
808 if (filp
->f_flags
& O_NONBLOCK
) {
809 if (down_trylock(&port
->sm_sem
)) {
814 if (down_interruptible(&port
->sm_sem
)) {
820 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
826 filp
->private_data
= port
;
831 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
835 static int ib_umad_sm_close(struct inode
*inode
, struct file
*filp
)
837 struct ib_umad_port
*port
= filp
->private_data
;
838 struct ib_port_modify props
= {
839 .clr_port_cap_mask
= IB_PORT_SM
843 down_write(&port
->mutex
);
845 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
846 up_write(&port
->mutex
);
850 kref_put(&port
->umad_dev
->ref
, ib_umad_release_dev
);
855 static const struct file_operations umad_sm_fops
= {
856 .owner
= THIS_MODULE
,
857 .open
= ib_umad_sm_open
,
858 .release
= ib_umad_sm_close
861 static struct ib_client umad_client
= {
863 .add
= ib_umad_add_one
,
864 .remove
= ib_umad_remove_one
867 static ssize_t
show_ibdev(struct class_device
*class_dev
, char *buf
)
869 struct ib_umad_port
*port
= class_get_devdata(class_dev
);
874 return sprintf(buf
, "%s\n", port
->ib_dev
->name
);
876 static CLASS_DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
878 static ssize_t
show_port(struct class_device
*class_dev
, char *buf
)
880 struct ib_umad_port
*port
= class_get_devdata(class_dev
);
885 return sprintf(buf
, "%d\n", port
->port_num
);
887 static CLASS_DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
889 static ssize_t
show_abi_version(struct class *class, char *buf
)
891 return sprintf(buf
, "%d\n", IB_USER_MAD_ABI_VERSION
);
893 static CLASS_ATTR(abi_version
, S_IRUGO
, show_abi_version
, NULL
);
895 static int ib_umad_init_port(struct ib_device
*device
, int port_num
,
896 struct ib_umad_port
*port
)
898 spin_lock(&port_lock
);
899 port
->dev_num
= find_first_zero_bit(dev_map
, IB_UMAD_MAX_PORTS
);
900 if (port
->dev_num
>= IB_UMAD_MAX_PORTS
) {
901 spin_unlock(&port_lock
);
904 set_bit(port
->dev_num
, dev_map
);
905 spin_unlock(&port_lock
);
907 port
->ib_dev
= device
;
908 port
->port_num
= port_num
;
909 init_MUTEX(&port
->sm_sem
);
910 init_rwsem(&port
->mutex
);
911 INIT_LIST_HEAD(&port
->file_list
);
913 port
->dev
= cdev_alloc();
916 port
->dev
->owner
= THIS_MODULE
;
917 port
->dev
->ops
= &umad_fops
;
918 kobject_set_name(&port
->dev
->kobj
, "umad%d", port
->dev_num
);
919 if (cdev_add(port
->dev
, base_dev
+ port
->dev_num
, 1))
922 port
->class_dev
= class_device_create(umad_class
, NULL
, port
->dev
->dev
,
924 "umad%d", port
->dev_num
);
925 if (IS_ERR(port
->class_dev
))
928 if (class_device_create_file(port
->class_dev
, &class_device_attr_ibdev
))
930 if (class_device_create_file(port
->class_dev
, &class_device_attr_port
))
933 port
->sm_dev
= cdev_alloc();
936 port
->sm_dev
->owner
= THIS_MODULE
;
937 port
->sm_dev
->ops
= &umad_sm_fops
;
938 kobject_set_name(&port
->sm_dev
->kobj
, "issm%d", port
->dev_num
);
939 if (cdev_add(port
->sm_dev
, base_dev
+ port
->dev_num
+ IB_UMAD_MAX_PORTS
, 1))
942 port
->sm_class_dev
= class_device_create(umad_class
, NULL
, port
->sm_dev
->dev
,
944 "issm%d", port
->dev_num
);
945 if (IS_ERR(port
->sm_class_dev
))
948 class_set_devdata(port
->class_dev
, port
);
949 class_set_devdata(port
->sm_class_dev
, port
);
951 if (class_device_create_file(port
->sm_class_dev
, &class_device_attr_ibdev
))
953 if (class_device_create_file(port
->sm_class_dev
, &class_device_attr_port
))
956 spin_lock(&port_lock
);
957 umad_port
[port
->dev_num
] = port
;
958 spin_unlock(&port_lock
);
963 class_device_destroy(umad_class
, port
->sm_dev
->dev
);
966 cdev_del(port
->sm_dev
);
969 class_device_destroy(umad_class
, port
->dev
->dev
);
973 clear_bit(port
->dev_num
, dev_map
);
978 static void ib_umad_kill_port(struct ib_umad_port
*port
)
980 struct ib_umad_file
*file
;
983 class_set_devdata(port
->class_dev
, NULL
);
984 class_set_devdata(port
->sm_class_dev
, NULL
);
986 class_device_destroy(umad_class
, port
->dev
->dev
);
987 class_device_destroy(umad_class
, port
->sm_dev
->dev
);
990 cdev_del(port
->sm_dev
);
992 spin_lock(&port_lock
);
993 umad_port
[port
->dev_num
] = NULL
;
994 spin_unlock(&port_lock
);
996 down_write(&port
->mutex
);
1001 * Now go through the list of files attached to this port and
1002 * unregister all of their MAD agents. We need to hold
1003 * port->mutex while doing this to avoid racing with
1004 * ib_umad_close(), but we can't hold the mutex for writing
1005 * while calling ib_unregister_mad_agent(), since that might
1006 * deadlock by calling back into queue_packet(). So we
1007 * downgrade our lock to a read lock, and then drop and
1008 * reacquire the write lock for the next iteration.
1010 * We do list_del_init() on the file's list_head so that the
1011 * list_del in ib_umad_close() is still OK, even after the
1012 * file is removed from the list.
1014 while (!list_empty(&port
->file_list
)) {
1015 file
= list_entry(port
->file_list
.next
, struct ib_umad_file
,
1018 file
->agents_dead
= 1;
1019 list_del_init(&file
->port_list
);
1021 downgrade_write(&port
->mutex
);
1023 for (id
= 0; id
< IB_UMAD_MAX_AGENTS
; ++id
)
1024 if (file
->agent
[id
])
1025 ib_unregister_mad_agent(file
->agent
[id
]);
1027 up_read(&port
->mutex
);
1028 down_write(&port
->mutex
);
1031 up_write(&port
->mutex
);
1033 clear_bit(port
->dev_num
, dev_map
);
1036 static void ib_umad_add_one(struct ib_device
*device
)
1038 struct ib_umad_device
*umad_dev
;
1041 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
1044 if (device
->node_type
== RDMA_NODE_IB_SWITCH
)
1048 e
= device
->phys_port_cnt
;
1051 umad_dev
= kzalloc(sizeof *umad_dev
+
1052 (e
- s
+ 1) * sizeof (struct ib_umad_port
),
1057 kref_init(&umad_dev
->ref
);
1059 umad_dev
->start_port
= s
;
1060 umad_dev
->end_port
= e
;
1062 for (i
= s
; i
<= e
; ++i
) {
1063 umad_dev
->port
[i
- s
].umad_dev
= umad_dev
;
1065 if (ib_umad_init_port(device
, i
, &umad_dev
->port
[i
- s
]))
1069 ib_set_client_data(device
, &umad_client
, umad_dev
);
1075 ib_umad_kill_port(&umad_dev
->port
[i
- s
]);
1077 kref_put(&umad_dev
->ref
, ib_umad_release_dev
);
1080 static void ib_umad_remove_one(struct ib_device
*device
)
1082 struct ib_umad_device
*umad_dev
= ib_get_client_data(device
, &umad_client
);
1088 for (i
= 0; i
<= umad_dev
->end_port
- umad_dev
->start_port
; ++i
)
1089 ib_umad_kill_port(&umad_dev
->port
[i
]);
1091 kref_put(&umad_dev
->ref
, ib_umad_release_dev
);
1094 static int __init
ib_umad_init(void)
1098 ret
= register_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2,
1101 printk(KERN_ERR
"user_mad: couldn't register device number\n");
1105 umad_class
= class_create(THIS_MODULE
, "infiniband_mad");
1106 if (IS_ERR(umad_class
)) {
1107 ret
= PTR_ERR(umad_class
);
1108 printk(KERN_ERR
"user_mad: couldn't create class infiniband_mad\n");
1112 ret
= class_create_file(umad_class
, &class_attr_abi_version
);
1114 printk(KERN_ERR
"user_mad: couldn't create abi_version attribute\n");
1118 ret
= ib_register_client(&umad_client
);
1120 printk(KERN_ERR
"user_mad: couldn't register ib_umad client\n");
1127 class_destroy(umad_class
);
1130 unregister_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2);
1136 static void __exit
ib_umad_cleanup(void)
1138 ib_unregister_client(&umad_client
);
1139 class_destroy(umad_class
);
1140 unregister_chrdev_region(base_dev
, IB_UMAD_MAX_PORTS
* 2);
1143 module_init(ib_umad_init
);
1144 module_exit(ib_umad_cleanup
);