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.
5 * Copyright (c) 2008 Cisco. All rights reserved.
7 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #define pr_fmt(fmt) "user_mad: " fmt
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/err.h>
43 #include <linux/cdev.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/poll.h>
46 #include <linux/mutex.h>
47 #include <linux/kref.h>
48 #include <linux/compat.h>
49 #include <linux/sched.h>
50 #include <linux/semaphore.h>
51 #include <linux/slab.h>
52 #include <linux/nospec.h>
54 #include <linux/uaccess.h>
56 #include <rdma/ib_mad.h>
57 #include <rdma/ib_user_mad.h>
59 #include "core_priv.h"
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
63 MODULE_LICENSE("Dual BSD/GPL");
66 IB_UMAD_MAX_PORTS
= RDMA_MAX_PORTS
,
67 IB_UMAD_MAX_AGENTS
= 32,
70 IB_UMAD_MINOR_BASE
= 0,
71 IB_UMAD_NUM_FIXED_MINOR
= 64,
72 IB_UMAD_NUM_DYNAMIC_MINOR
= IB_UMAD_MAX_PORTS
- IB_UMAD_NUM_FIXED_MINOR
,
73 IB_ISSM_MINOR_BASE
= IB_UMAD_NUM_FIXED_MINOR
,
77 * Our lifetime rules for these structs are the following:
78 * device special file is opened, we take a reference on the
79 * ib_umad_port's struct ib_umad_device. We drop these
80 * references in the corresponding close().
82 * In addition to references coming from open character devices, there
83 * is one more reference to each ib_umad_device representing the
84 * module's reference taken when allocating the ib_umad_device in
87 * When destroying an ib_umad_device, we drop the module's reference.
95 struct device
*sm_dev
;
96 struct semaphore sm_sem
;
98 struct mutex file_mutex
;
99 struct list_head file_list
;
101 struct ib_device
*ib_dev
;
102 struct ib_umad_device
*umad_dev
;
107 struct ib_umad_device
{
109 struct ib_umad_port port
[0];
112 struct ib_umad_file
{
114 struct ib_umad_port
*port
;
115 struct list_head recv_list
;
116 struct list_head send_list
;
117 struct list_head port_list
;
118 spinlock_t send_lock
;
119 wait_queue_head_t recv_wait
;
120 struct ib_mad_agent
*agent
[IB_UMAD_MAX_AGENTS
];
126 struct ib_umad_packet
{
127 struct ib_mad_send_buf
*msg
;
128 struct ib_mad_recv_wc
*recv_wc
;
129 struct list_head list
;
131 struct ib_user_mad mad
;
134 static struct class *umad_class
;
136 static const dev_t base_umad_dev
= MKDEV(IB_UMAD_MAJOR
, IB_UMAD_MINOR_BASE
);
137 static const dev_t base_issm_dev
= MKDEV(IB_UMAD_MAJOR
, IB_UMAD_MINOR_BASE
) +
138 IB_UMAD_NUM_FIXED_MINOR
;
139 static dev_t dynamic_umad_dev
;
140 static dev_t dynamic_issm_dev
;
142 static DECLARE_BITMAP(dev_map
, IB_UMAD_MAX_PORTS
);
144 static void ib_umad_add_one(struct ib_device
*device
);
145 static void ib_umad_remove_one(struct ib_device
*device
, void *client_data
);
147 static void ib_umad_release_dev(struct kobject
*kobj
)
149 struct ib_umad_device
*dev
=
150 container_of(kobj
, struct ib_umad_device
, kobj
);
155 static struct kobj_type ib_umad_dev_ktype
= {
156 .release
= ib_umad_release_dev
,
159 static int hdr_size(struct ib_umad_file
*file
)
161 return file
->use_pkey_index
? sizeof (struct ib_user_mad_hdr
) :
162 sizeof (struct ib_user_mad_hdr_old
);
165 /* caller must hold file->mutex */
166 static struct ib_mad_agent
*__get_agent(struct ib_umad_file
*file
, int id
)
168 return file
->agents_dead
? NULL
: file
->agent
[id
];
171 static int queue_packet(struct ib_umad_file
*file
,
172 struct ib_mad_agent
*agent
,
173 struct ib_umad_packet
*packet
)
177 mutex_lock(&file
->mutex
);
179 for (packet
->mad
.hdr
.id
= 0;
180 packet
->mad
.hdr
.id
< IB_UMAD_MAX_AGENTS
;
181 packet
->mad
.hdr
.id
++)
182 if (agent
== __get_agent(file
, packet
->mad
.hdr
.id
)) {
183 list_add_tail(&packet
->list
, &file
->recv_list
);
184 wake_up_interruptible(&file
->recv_wait
);
189 mutex_unlock(&file
->mutex
);
194 static void dequeue_send(struct ib_umad_file
*file
,
195 struct ib_umad_packet
*packet
)
197 spin_lock_irq(&file
->send_lock
);
198 list_del(&packet
->list
);
199 spin_unlock_irq(&file
->send_lock
);
202 static void send_handler(struct ib_mad_agent
*agent
,
203 struct ib_mad_send_wc
*send_wc
)
205 struct ib_umad_file
*file
= agent
->context
;
206 struct ib_umad_packet
*packet
= send_wc
->send_buf
->context
[0];
208 dequeue_send(file
, packet
);
209 rdma_destroy_ah(packet
->msg
->ah
);
210 ib_free_send_mad(packet
->msg
);
212 if (send_wc
->status
== IB_WC_RESP_TIMEOUT_ERR
) {
213 packet
->length
= IB_MGMT_MAD_HDR
;
214 packet
->mad
.hdr
.status
= ETIMEDOUT
;
215 if (!queue_packet(file
, agent
, packet
))
221 static void recv_handler(struct ib_mad_agent
*agent
,
222 struct ib_mad_send_buf
*send_buf
,
223 struct ib_mad_recv_wc
*mad_recv_wc
)
225 struct ib_umad_file
*file
= agent
->context
;
226 struct ib_umad_packet
*packet
;
228 if (mad_recv_wc
->wc
->status
!= IB_WC_SUCCESS
)
231 packet
= kzalloc(sizeof *packet
, GFP_KERNEL
);
235 packet
->length
= mad_recv_wc
->mad_len
;
236 packet
->recv_wc
= mad_recv_wc
;
238 packet
->mad
.hdr
.status
= 0;
239 packet
->mad
.hdr
.length
= hdr_size(file
) + mad_recv_wc
->mad_len
;
240 packet
->mad
.hdr
.qpn
= cpu_to_be32(mad_recv_wc
->wc
->src_qp
);
242 * On OPA devices it is okay to lose the upper 16 bits of LID as this
243 * information is obtained elsewhere. Mask off the upper 16 bits.
245 if (rdma_cap_opa_mad(agent
->device
, agent
->port_num
))
246 packet
->mad
.hdr
.lid
= ib_lid_be16(0xFFFF &
247 mad_recv_wc
->wc
->slid
);
249 packet
->mad
.hdr
.lid
= ib_lid_be16(mad_recv_wc
->wc
->slid
);
250 packet
->mad
.hdr
.sl
= mad_recv_wc
->wc
->sl
;
251 packet
->mad
.hdr
.path_bits
= mad_recv_wc
->wc
->dlid_path_bits
;
252 packet
->mad
.hdr
.pkey_index
= mad_recv_wc
->wc
->pkey_index
;
253 packet
->mad
.hdr
.grh_present
= !!(mad_recv_wc
->wc
->wc_flags
& IB_WC_GRH
);
254 if (packet
->mad
.hdr
.grh_present
) {
255 struct rdma_ah_attr ah_attr
;
256 const struct ib_global_route
*grh
;
259 ret
= ib_init_ah_attr_from_wc(agent
->device
, agent
->port_num
,
261 mad_recv_wc
->recv_buf
.grh
,
266 grh
= rdma_ah_read_grh(&ah_attr
);
267 packet
->mad
.hdr
.gid_index
= grh
->sgid_index
;
268 packet
->mad
.hdr
.hop_limit
= grh
->hop_limit
;
269 packet
->mad
.hdr
.traffic_class
= grh
->traffic_class
;
270 memcpy(packet
->mad
.hdr
.gid
, &grh
->dgid
, 16);
271 packet
->mad
.hdr
.flow_label
= cpu_to_be32(grh
->flow_label
);
272 rdma_destroy_ah_attr(&ah_attr
);
275 if (queue_packet(file
, agent
, packet
))
282 ib_free_recv_mad(mad_recv_wc
);
285 static ssize_t
copy_recv_mad(struct ib_umad_file
*file
, char __user
*buf
,
286 struct ib_umad_packet
*packet
, size_t count
)
288 struct ib_mad_recv_buf
*recv_buf
;
289 int left
, seg_payload
, offset
, max_seg_payload
;
292 recv_buf
= &packet
->recv_wc
->recv_buf
;
293 seg_size
= packet
->recv_wc
->mad_seg_size
;
295 /* We need enough room to copy the first (or only) MAD segment. */
296 if ((packet
->length
<= seg_size
&&
297 count
< hdr_size(file
) + packet
->length
) ||
298 (packet
->length
> seg_size
&&
299 count
< hdr_size(file
) + seg_size
))
302 if (copy_to_user(buf
, &packet
->mad
, hdr_size(file
)))
305 buf
+= hdr_size(file
);
306 seg_payload
= min_t(int, packet
->length
, seg_size
);
307 if (copy_to_user(buf
, recv_buf
->mad
, seg_payload
))
310 if (seg_payload
< packet
->length
) {
312 * Multipacket RMPP MAD message. Copy remainder of message.
313 * Note that last segment may have a shorter payload.
315 if (count
< hdr_size(file
) + packet
->length
) {
317 * The buffer is too small, return the first RMPP segment,
318 * which includes the RMPP message length.
322 offset
= ib_get_mad_data_offset(recv_buf
->mad
->mad_hdr
.mgmt_class
);
323 max_seg_payload
= seg_size
- offset
;
325 for (left
= packet
->length
- seg_payload
, buf
+= seg_payload
;
326 left
; left
-= seg_payload
, buf
+= seg_payload
) {
327 recv_buf
= container_of(recv_buf
->list
.next
,
328 struct ib_mad_recv_buf
, list
);
329 seg_payload
= min(left
, max_seg_payload
);
330 if (copy_to_user(buf
, ((void *) recv_buf
->mad
) + offset
,
335 return hdr_size(file
) + packet
->length
;
338 static ssize_t
copy_send_mad(struct ib_umad_file
*file
, char __user
*buf
,
339 struct ib_umad_packet
*packet
, size_t count
)
341 ssize_t size
= hdr_size(file
) + packet
->length
;
346 if (copy_to_user(buf
, &packet
->mad
, hdr_size(file
)))
349 buf
+= hdr_size(file
);
351 if (copy_to_user(buf
, packet
->mad
.data
, packet
->length
))
357 static ssize_t
ib_umad_read(struct file
*filp
, char __user
*buf
,
358 size_t count
, loff_t
*pos
)
360 struct ib_umad_file
*file
= filp
->private_data
;
361 struct ib_umad_packet
*packet
;
364 if (count
< hdr_size(file
))
367 mutex_lock(&file
->mutex
);
369 while (list_empty(&file
->recv_list
)) {
370 mutex_unlock(&file
->mutex
);
372 if (filp
->f_flags
& O_NONBLOCK
)
375 if (wait_event_interruptible(file
->recv_wait
,
376 !list_empty(&file
->recv_list
)))
379 mutex_lock(&file
->mutex
);
382 packet
= list_entry(file
->recv_list
.next
, struct ib_umad_packet
, list
);
383 list_del(&packet
->list
);
385 mutex_unlock(&file
->mutex
);
388 ret
= copy_recv_mad(file
, buf
, packet
, count
);
390 ret
= copy_send_mad(file
, buf
, packet
, count
);
394 mutex_lock(&file
->mutex
);
395 list_add(&packet
->list
, &file
->recv_list
);
396 mutex_unlock(&file
->mutex
);
399 ib_free_recv_mad(packet
->recv_wc
);
405 static int copy_rmpp_mad(struct ib_mad_send_buf
*msg
, const char __user
*buf
)
409 /* Copy class specific header */
410 if ((msg
->hdr_len
> IB_MGMT_RMPP_HDR
) &&
411 copy_from_user(msg
->mad
+ IB_MGMT_RMPP_HDR
, buf
+ IB_MGMT_RMPP_HDR
,
412 msg
->hdr_len
- IB_MGMT_RMPP_HDR
))
415 /* All headers are in place. Copy data segments. */
416 for (seg
= 1, left
= msg
->data_len
, buf
+= msg
->hdr_len
; left
> 0;
417 seg
++, left
-= msg
->seg_size
, buf
+= msg
->seg_size
) {
418 if (copy_from_user(ib_get_rmpp_segment(msg
, seg
), buf
,
419 min(left
, msg
->seg_size
)))
425 static int same_destination(struct ib_user_mad_hdr
*hdr1
,
426 struct ib_user_mad_hdr
*hdr2
)
428 if (!hdr1
->grh_present
&& !hdr2
->grh_present
)
429 return (hdr1
->lid
== hdr2
->lid
);
431 if (hdr1
->grh_present
&& hdr2
->grh_present
)
432 return !memcmp(hdr1
->gid
, hdr2
->gid
, 16);
437 static int is_duplicate(struct ib_umad_file
*file
,
438 struct ib_umad_packet
*packet
)
440 struct ib_umad_packet
*sent_packet
;
441 struct ib_mad_hdr
*sent_hdr
, *hdr
;
443 hdr
= (struct ib_mad_hdr
*) packet
->mad
.data
;
444 list_for_each_entry(sent_packet
, &file
->send_list
, list
) {
445 sent_hdr
= (struct ib_mad_hdr
*) sent_packet
->mad
.data
;
447 if ((hdr
->tid
!= sent_hdr
->tid
) ||
448 (hdr
->mgmt_class
!= sent_hdr
->mgmt_class
))
452 * No need to be overly clever here. If two new operations have
453 * the same TID, reject the second as a duplicate. This is more
454 * restrictive than required by the spec.
456 if (!ib_response_mad(hdr
)) {
457 if (!ib_response_mad(sent_hdr
))
460 } else if (!ib_response_mad(sent_hdr
))
463 if (same_destination(&packet
->mad
.hdr
, &sent_packet
->mad
.hdr
))
470 static ssize_t
ib_umad_write(struct file
*filp
, const char __user
*buf
,
471 size_t count
, loff_t
*pos
)
473 struct ib_umad_file
*file
= filp
->private_data
;
474 struct ib_umad_packet
*packet
;
475 struct ib_mad_agent
*agent
;
476 struct rdma_ah_attr ah_attr
;
478 struct ib_rmpp_mad
*rmpp_mad
;
480 int ret
, data_len
, hdr_len
, copy_offset
, rmpp_active
;
483 if (count
< hdr_size(file
) + IB_MGMT_RMPP_HDR
)
486 packet
= kzalloc(sizeof *packet
+ IB_MGMT_RMPP_HDR
, GFP_KERNEL
);
490 if (copy_from_user(&packet
->mad
, buf
, hdr_size(file
))) {
495 if (packet
->mad
.hdr
.id
>= IB_UMAD_MAX_AGENTS
) {
500 buf
+= hdr_size(file
);
502 if (copy_from_user(packet
->mad
.data
, buf
, IB_MGMT_RMPP_HDR
)) {
507 mutex_lock(&file
->mutex
);
509 agent
= __get_agent(file
, packet
->mad
.hdr
.id
);
515 memset(&ah_attr
, 0, sizeof ah_attr
);
516 ah_attr
.type
= rdma_ah_find_type(agent
->device
,
517 file
->port
->port_num
);
518 rdma_ah_set_dlid(&ah_attr
, be16_to_cpu(packet
->mad
.hdr
.lid
));
519 rdma_ah_set_sl(&ah_attr
, packet
->mad
.hdr
.sl
);
520 rdma_ah_set_path_bits(&ah_attr
, packet
->mad
.hdr
.path_bits
);
521 rdma_ah_set_port_num(&ah_attr
, file
->port
->port_num
);
522 if (packet
->mad
.hdr
.grh_present
) {
523 rdma_ah_set_grh(&ah_attr
, NULL
,
524 be32_to_cpu(packet
->mad
.hdr
.flow_label
),
525 packet
->mad
.hdr
.gid_index
,
526 packet
->mad
.hdr
.hop_limit
,
527 packet
->mad
.hdr
.traffic_class
);
528 rdma_ah_set_dgid_raw(&ah_attr
, packet
->mad
.hdr
.gid
);
531 ah
= rdma_create_user_ah(agent
->qp
->pd
, &ah_attr
, NULL
);
537 rmpp_mad
= (struct ib_rmpp_mad
*) packet
->mad
.data
;
538 hdr_len
= ib_get_mad_data_offset(rmpp_mad
->mad_hdr
.mgmt_class
);
540 if (ib_is_mad_class_rmpp(rmpp_mad
->mad_hdr
.mgmt_class
)
541 && ib_mad_kernel_rmpp_agent(agent
)) {
542 copy_offset
= IB_MGMT_RMPP_HDR
;
543 rmpp_active
= ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
544 IB_MGMT_RMPP_FLAG_ACTIVE
;
546 copy_offset
= IB_MGMT_MAD_HDR
;
550 base_version
= ((struct ib_mad_hdr
*)&packet
->mad
.data
)->base_version
;
551 data_len
= count
- hdr_size(file
) - hdr_len
;
552 packet
->msg
= ib_create_send_mad(agent
,
553 be32_to_cpu(packet
->mad
.hdr
.qpn
),
554 packet
->mad
.hdr
.pkey_index
, rmpp_active
,
555 hdr_len
, data_len
, GFP_KERNEL
,
557 if (IS_ERR(packet
->msg
)) {
558 ret
= PTR_ERR(packet
->msg
);
562 packet
->msg
->ah
= ah
;
563 packet
->msg
->timeout_ms
= packet
->mad
.hdr
.timeout_ms
;
564 packet
->msg
->retries
= packet
->mad
.hdr
.retries
;
565 packet
->msg
->context
[0] = packet
;
567 /* Copy MAD header. Any RMPP header is already in place. */
568 memcpy(packet
->msg
->mad
, packet
->mad
.data
, IB_MGMT_MAD_HDR
);
571 if (copy_from_user(packet
->msg
->mad
+ copy_offset
,
573 hdr_len
+ data_len
- copy_offset
)) {
578 ret
= copy_rmpp_mad(packet
->msg
, buf
);
584 * Set the high-order part of the transaction ID to make MADs from
585 * different agents unique, and allow routing responses back to the
586 * original requestor.
588 if (!ib_response_mad(packet
->msg
->mad
)) {
589 tid
= &((struct ib_mad_hdr
*) packet
->msg
->mad
)->tid
;
590 *tid
= cpu_to_be64(((u64
) agent
->hi_tid
) << 32 |
591 (be64_to_cpup(tid
) & 0xffffffff));
592 rmpp_mad
->mad_hdr
.tid
= *tid
;
595 if (!ib_mad_kernel_rmpp_agent(agent
)
596 && ib_is_mad_class_rmpp(rmpp_mad
->mad_hdr
.mgmt_class
)
597 && (ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) & IB_MGMT_RMPP_FLAG_ACTIVE
)) {
598 spin_lock_irq(&file
->send_lock
);
599 list_add_tail(&packet
->list
, &file
->send_list
);
600 spin_unlock_irq(&file
->send_lock
);
602 spin_lock_irq(&file
->send_lock
);
603 ret
= is_duplicate(file
, packet
);
605 list_add_tail(&packet
->list
, &file
->send_list
);
606 spin_unlock_irq(&file
->send_lock
);
613 ret
= ib_post_send_mad(packet
->msg
, NULL
);
617 mutex_unlock(&file
->mutex
);
621 dequeue_send(file
, packet
);
623 ib_free_send_mad(packet
->msg
);
627 mutex_unlock(&file
->mutex
);
633 static __poll_t
ib_umad_poll(struct file
*filp
, struct poll_table_struct
*wait
)
635 struct ib_umad_file
*file
= filp
->private_data
;
637 /* we will always be able to post a MAD send */
638 __poll_t mask
= EPOLLOUT
| EPOLLWRNORM
;
640 poll_wait(filp
, &file
->recv_wait
, wait
);
642 if (!list_empty(&file
->recv_list
))
643 mask
|= EPOLLIN
| EPOLLRDNORM
;
648 static int ib_umad_reg_agent(struct ib_umad_file
*file
, void __user
*arg
,
649 int compat_method_mask
)
651 struct ib_user_mad_reg_req ureq
;
652 struct ib_mad_reg_req req
;
653 struct ib_mad_agent
*agent
= NULL
;
657 mutex_lock(&file
->port
->file_mutex
);
658 mutex_lock(&file
->mutex
);
660 if (!file
->port
->ib_dev
) {
661 dev_notice(file
->port
->dev
,
662 "ib_umad_reg_agent: invalid device\n");
667 if (copy_from_user(&ureq
, arg
, sizeof ureq
)) {
672 if (ureq
.qpn
!= 0 && ureq
.qpn
!= 1) {
673 dev_notice(file
->port
->dev
,
674 "ib_umad_reg_agent: invalid QPN %d specified\n",
680 for (agent_id
= 0; agent_id
< IB_UMAD_MAX_AGENTS
; ++agent_id
)
681 if (!__get_agent(file
, agent_id
))
684 dev_notice(file
->port
->dev
,
685 "ib_umad_reg_agent: Max Agents (%u) reached\n",
691 if (ureq
.mgmt_class
) {
692 memset(&req
, 0, sizeof(req
));
693 req
.mgmt_class
= ureq
.mgmt_class
;
694 req
.mgmt_class_version
= ureq
.mgmt_class_version
;
695 memcpy(req
.oui
, ureq
.oui
, sizeof req
.oui
);
697 if (compat_method_mask
) {
698 u32
*umm
= (u32
*) ureq
.method_mask
;
701 for (i
= 0; i
< BITS_TO_LONGS(IB_MGMT_MAX_METHODS
); ++i
)
703 umm
[i
* 2] | ((u64
) umm
[i
* 2 + 1] << 32);
705 memcpy(req
.method_mask
, ureq
.method_mask
,
706 sizeof req
.method_mask
);
709 agent
= ib_register_mad_agent(file
->port
->ib_dev
, file
->port
->port_num
,
710 ureq
.qpn
? IB_QPT_GSI
: IB_QPT_SMI
,
711 ureq
.mgmt_class
? &req
: NULL
,
713 send_handler
, recv_handler
, file
, 0);
715 ret
= PTR_ERR(agent
);
720 if (put_user(agent_id
,
721 (u32 __user
*) (arg
+ offsetof(struct ib_user_mad_reg_req
, id
)))) {
726 if (!file
->already_used
) {
727 file
->already_used
= 1;
728 if (!file
->use_pkey_index
) {
729 dev_warn(file
->port
->dev
,
730 "process %s did not enable P_Key index support.\n",
732 dev_warn(file
->port
->dev
,
733 " Documentation/infiniband/user_mad.txt has info on the new ABI.\n");
737 file
->agent
[agent_id
] = agent
;
741 mutex_unlock(&file
->mutex
);
744 ib_unregister_mad_agent(agent
);
746 mutex_unlock(&file
->port
->file_mutex
);
751 static int ib_umad_reg_agent2(struct ib_umad_file
*file
, void __user
*arg
)
753 struct ib_user_mad_reg_req2 ureq
;
754 struct ib_mad_reg_req req
;
755 struct ib_mad_agent
*agent
= NULL
;
759 mutex_lock(&file
->port
->file_mutex
);
760 mutex_lock(&file
->mutex
);
762 if (!file
->port
->ib_dev
) {
763 dev_notice(file
->port
->dev
,
764 "ib_umad_reg_agent2: invalid device\n");
769 if (copy_from_user(&ureq
, arg
, sizeof(ureq
))) {
774 if (ureq
.qpn
!= 0 && ureq
.qpn
!= 1) {
775 dev_notice(file
->port
->dev
,
776 "ib_umad_reg_agent2: invalid QPN %d specified\n",
782 if (ureq
.flags
& ~IB_USER_MAD_REG_FLAGS_CAP
) {
783 dev_notice(file
->port
->dev
,
784 "ib_umad_reg_agent2 failed: invalid registration flags specified 0x%x; supported 0x%x\n",
785 ureq
.flags
, IB_USER_MAD_REG_FLAGS_CAP
);
788 if (put_user((u32
)IB_USER_MAD_REG_FLAGS_CAP
,
789 (u32 __user
*) (arg
+ offsetof(struct
790 ib_user_mad_reg_req2
, flags
))))
796 for (agent_id
= 0; agent_id
< IB_UMAD_MAX_AGENTS
; ++agent_id
)
797 if (!__get_agent(file
, agent_id
))
800 dev_notice(file
->port
->dev
,
801 "ib_umad_reg_agent2: Max Agents (%u) reached\n",
807 if (ureq
.mgmt_class
) {
808 memset(&req
, 0, sizeof(req
));
809 req
.mgmt_class
= ureq
.mgmt_class
;
810 req
.mgmt_class_version
= ureq
.mgmt_class_version
;
811 if (ureq
.oui
& 0xff000000) {
812 dev_notice(file
->port
->dev
,
813 "ib_umad_reg_agent2 failed: oui invalid 0x%08x\n",
818 req
.oui
[2] = ureq
.oui
& 0x0000ff;
819 req
.oui
[1] = (ureq
.oui
& 0x00ff00) >> 8;
820 req
.oui
[0] = (ureq
.oui
& 0xff0000) >> 16;
821 memcpy(req
.method_mask
, ureq
.method_mask
,
822 sizeof(req
.method_mask
));
825 agent
= ib_register_mad_agent(file
->port
->ib_dev
, file
->port
->port_num
,
826 ureq
.qpn
? IB_QPT_GSI
: IB_QPT_SMI
,
827 ureq
.mgmt_class
? &req
: NULL
,
829 send_handler
, recv_handler
, file
,
832 ret
= PTR_ERR(agent
);
837 if (put_user(agent_id
,
839 offsetof(struct ib_user_mad_reg_req2
, id
)))) {
844 if (!file
->already_used
) {
845 file
->already_used
= 1;
846 file
->use_pkey_index
= 1;
849 file
->agent
[agent_id
] = agent
;
853 mutex_unlock(&file
->mutex
);
856 ib_unregister_mad_agent(agent
);
858 mutex_unlock(&file
->port
->file_mutex
);
864 static int ib_umad_unreg_agent(struct ib_umad_file
*file
, u32 __user
*arg
)
866 struct ib_mad_agent
*agent
= NULL
;
870 if (get_user(id
, arg
))
872 if (id
>= IB_UMAD_MAX_AGENTS
)
875 mutex_lock(&file
->port
->file_mutex
);
876 mutex_lock(&file
->mutex
);
878 id
= array_index_nospec(id
, IB_UMAD_MAX_AGENTS
);
879 if (!__get_agent(file
, id
)) {
884 agent
= file
->agent
[id
];
885 file
->agent
[id
] = NULL
;
888 mutex_unlock(&file
->mutex
);
891 ib_unregister_mad_agent(agent
);
893 mutex_unlock(&file
->port
->file_mutex
);
898 static long ib_umad_enable_pkey(struct ib_umad_file
*file
)
902 mutex_lock(&file
->mutex
);
903 if (file
->already_used
)
906 file
->use_pkey_index
= 1;
907 mutex_unlock(&file
->mutex
);
912 static long ib_umad_ioctl(struct file
*filp
, unsigned int cmd
,
916 case IB_USER_MAD_REGISTER_AGENT
:
917 return ib_umad_reg_agent(filp
->private_data
, (void __user
*) arg
, 0);
918 case IB_USER_MAD_UNREGISTER_AGENT
:
919 return ib_umad_unreg_agent(filp
->private_data
, (__u32 __user
*) arg
);
920 case IB_USER_MAD_ENABLE_PKEY
:
921 return ib_umad_enable_pkey(filp
->private_data
);
922 case IB_USER_MAD_REGISTER_AGENT2
:
923 return ib_umad_reg_agent2(filp
->private_data
, (void __user
*) arg
);
930 static long ib_umad_compat_ioctl(struct file
*filp
, unsigned int cmd
,
934 case IB_USER_MAD_REGISTER_AGENT
:
935 return ib_umad_reg_agent(filp
->private_data
, compat_ptr(arg
), 1);
936 case IB_USER_MAD_UNREGISTER_AGENT
:
937 return ib_umad_unreg_agent(filp
->private_data
, compat_ptr(arg
));
938 case IB_USER_MAD_ENABLE_PKEY
:
939 return ib_umad_enable_pkey(filp
->private_data
);
940 case IB_USER_MAD_REGISTER_AGENT2
:
941 return ib_umad_reg_agent2(filp
->private_data
, compat_ptr(arg
));
949 * ib_umad_open() does not need the BKL:
951 * - the ib_umad_port structures are properly reference counted, and
952 * everything else is purely local to the file being created, so
953 * races against other open calls are not a problem;
954 * - the ioctl method does not affect any global state outside of the
955 * file structure being operated on;
957 static int ib_umad_open(struct inode
*inode
, struct file
*filp
)
959 struct ib_umad_port
*port
;
960 struct ib_umad_file
*file
;
963 port
= container_of(inode
->i_cdev
, struct ib_umad_port
, cdev
);
965 mutex_lock(&port
->file_mutex
);
971 file
= kzalloc(sizeof *file
, GFP_KERNEL
);
975 mutex_init(&file
->mutex
);
976 spin_lock_init(&file
->send_lock
);
977 INIT_LIST_HEAD(&file
->recv_list
);
978 INIT_LIST_HEAD(&file
->send_list
);
979 init_waitqueue_head(&file
->recv_wait
);
982 filp
->private_data
= file
;
984 list_add_tail(&file
->port_list
, &port
->file_list
);
986 ret
= nonseekable_open(inode
, filp
);
988 list_del(&file
->port_list
);
993 kobject_get(&port
->umad_dev
->kobj
);
996 mutex_unlock(&port
->file_mutex
);
1000 static int ib_umad_close(struct inode
*inode
, struct file
*filp
)
1002 struct ib_umad_file
*file
= filp
->private_data
;
1003 struct ib_umad_device
*dev
= file
->port
->umad_dev
;
1004 struct ib_umad_packet
*packet
, *tmp
;
1008 mutex_lock(&file
->port
->file_mutex
);
1009 mutex_lock(&file
->mutex
);
1011 already_dead
= file
->agents_dead
;
1012 file
->agents_dead
= 1;
1014 list_for_each_entry_safe(packet
, tmp
, &file
->recv_list
, list
) {
1015 if (packet
->recv_wc
)
1016 ib_free_recv_mad(packet
->recv_wc
);
1020 list_del(&file
->port_list
);
1022 mutex_unlock(&file
->mutex
);
1025 for (i
= 0; i
< IB_UMAD_MAX_AGENTS
; ++i
)
1027 ib_unregister_mad_agent(file
->agent
[i
]);
1029 mutex_unlock(&file
->port
->file_mutex
);
1032 kobject_put(&dev
->kobj
);
1037 static const struct file_operations umad_fops
= {
1038 .owner
= THIS_MODULE
,
1039 .read
= ib_umad_read
,
1040 .write
= ib_umad_write
,
1041 .poll
= ib_umad_poll
,
1042 .unlocked_ioctl
= ib_umad_ioctl
,
1043 #ifdef CONFIG_COMPAT
1044 .compat_ioctl
= ib_umad_compat_ioctl
,
1046 .open
= ib_umad_open
,
1047 .release
= ib_umad_close
,
1048 .llseek
= no_llseek
,
1051 static int ib_umad_sm_open(struct inode
*inode
, struct file
*filp
)
1053 struct ib_umad_port
*port
;
1054 struct ib_port_modify props
= {
1055 .set_port_cap_mask
= IB_PORT_SM
1059 port
= container_of(inode
->i_cdev
, struct ib_umad_port
, sm_cdev
);
1061 if (filp
->f_flags
& O_NONBLOCK
) {
1062 if (down_trylock(&port
->sm_sem
)) {
1067 if (down_interruptible(&port
->sm_sem
)) {
1073 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
1077 filp
->private_data
= port
;
1079 ret
= nonseekable_open(inode
, filp
);
1081 goto err_clr_sm_cap
;
1083 kobject_get(&port
->umad_dev
->kobj
);
1088 swap(props
.set_port_cap_mask
, props
.clr_port_cap_mask
);
1089 ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
1098 static int ib_umad_sm_close(struct inode
*inode
, struct file
*filp
)
1100 struct ib_umad_port
*port
= filp
->private_data
;
1101 struct ib_port_modify props
= {
1102 .clr_port_cap_mask
= IB_PORT_SM
1106 mutex_lock(&port
->file_mutex
);
1108 ret
= ib_modify_port(port
->ib_dev
, port
->port_num
, 0, &props
);
1109 mutex_unlock(&port
->file_mutex
);
1113 kobject_put(&port
->umad_dev
->kobj
);
1118 static const struct file_operations umad_sm_fops
= {
1119 .owner
= THIS_MODULE
,
1120 .open
= ib_umad_sm_open
,
1121 .release
= ib_umad_sm_close
,
1122 .llseek
= no_llseek
,
1125 static struct ib_client umad_client
= {
1127 .add
= ib_umad_add_one
,
1128 .remove
= ib_umad_remove_one
1131 static ssize_t
show_ibdev(struct device
*dev
, struct device_attribute
*attr
,
1134 struct ib_umad_port
*port
= dev_get_drvdata(dev
);
1139 return sprintf(buf
, "%s\n", port
->ib_dev
->name
);
1141 static DEVICE_ATTR(ibdev
, S_IRUGO
, show_ibdev
, NULL
);
1143 static ssize_t
show_port(struct device
*dev
, struct device_attribute
*attr
,
1146 struct ib_umad_port
*port
= dev_get_drvdata(dev
);
1151 return sprintf(buf
, "%d\n", port
->port_num
);
1153 static DEVICE_ATTR(port
, S_IRUGO
, show_port
, NULL
);
1155 static CLASS_ATTR_STRING(abi_version
, S_IRUGO
,
1156 __stringify(IB_USER_MAD_ABI_VERSION
));
1158 static int ib_umad_init_port(struct ib_device
*device
, int port_num
,
1159 struct ib_umad_device
*umad_dev
,
1160 struct ib_umad_port
*port
)
1166 devnum
= find_first_zero_bit(dev_map
, IB_UMAD_MAX_PORTS
);
1167 if (devnum
>= IB_UMAD_MAX_PORTS
)
1169 port
->dev_num
= devnum
;
1170 set_bit(devnum
, dev_map
);
1171 if (devnum
>= IB_UMAD_NUM_FIXED_MINOR
) {
1172 base_umad
= dynamic_umad_dev
+ devnum
- IB_UMAD_NUM_FIXED_MINOR
;
1173 base_issm
= dynamic_issm_dev
+ devnum
- IB_UMAD_NUM_FIXED_MINOR
;
1175 base_umad
= devnum
+ base_umad_dev
;
1176 base_issm
= devnum
+ base_issm_dev
;
1179 port
->ib_dev
= device
;
1180 port
->port_num
= port_num
;
1181 sema_init(&port
->sm_sem
, 1);
1182 mutex_init(&port
->file_mutex
);
1183 INIT_LIST_HEAD(&port
->file_list
);
1185 cdev_init(&port
->cdev
, &umad_fops
);
1186 port
->cdev
.owner
= THIS_MODULE
;
1187 cdev_set_parent(&port
->cdev
, &umad_dev
->kobj
);
1188 kobject_set_name(&port
->cdev
.kobj
, "umad%d", port
->dev_num
);
1189 if (cdev_add(&port
->cdev
, base_umad
, 1))
1192 port
->dev
= device_create(umad_class
, device
->dev
.parent
,
1193 port
->cdev
.dev
, port
,
1194 "umad%d", port
->dev_num
);
1195 if (IS_ERR(port
->dev
))
1198 if (device_create_file(port
->dev
, &dev_attr_ibdev
))
1200 if (device_create_file(port
->dev
, &dev_attr_port
))
1203 cdev_init(&port
->sm_cdev
, &umad_sm_fops
);
1204 port
->sm_cdev
.owner
= THIS_MODULE
;
1205 cdev_set_parent(&port
->sm_cdev
, &umad_dev
->kobj
);
1206 kobject_set_name(&port
->sm_cdev
.kobj
, "issm%d", port
->dev_num
);
1207 if (cdev_add(&port
->sm_cdev
, base_issm
, 1))
1210 port
->sm_dev
= device_create(umad_class
, device
->dev
.parent
,
1211 port
->sm_cdev
.dev
, port
,
1212 "issm%d", port
->dev_num
);
1213 if (IS_ERR(port
->sm_dev
))
1216 if (device_create_file(port
->sm_dev
, &dev_attr_ibdev
))
1218 if (device_create_file(port
->sm_dev
, &dev_attr_port
))
1224 device_destroy(umad_class
, port
->sm_cdev
.dev
);
1227 cdev_del(&port
->sm_cdev
);
1230 device_destroy(umad_class
, port
->cdev
.dev
);
1233 cdev_del(&port
->cdev
);
1234 clear_bit(devnum
, dev_map
);
1239 static void ib_umad_kill_port(struct ib_umad_port
*port
)
1241 struct ib_umad_file
*file
;
1244 dev_set_drvdata(port
->dev
, NULL
);
1245 dev_set_drvdata(port
->sm_dev
, NULL
);
1247 device_destroy(umad_class
, port
->cdev
.dev
);
1248 device_destroy(umad_class
, port
->sm_cdev
.dev
);
1250 cdev_del(&port
->cdev
);
1251 cdev_del(&port
->sm_cdev
);
1253 mutex_lock(&port
->file_mutex
);
1255 port
->ib_dev
= NULL
;
1257 list_for_each_entry(file
, &port
->file_list
, port_list
) {
1258 mutex_lock(&file
->mutex
);
1259 file
->agents_dead
= 1;
1260 mutex_unlock(&file
->mutex
);
1262 for (id
= 0; id
< IB_UMAD_MAX_AGENTS
; ++id
)
1263 if (file
->agent
[id
])
1264 ib_unregister_mad_agent(file
->agent
[id
]);
1267 mutex_unlock(&port
->file_mutex
);
1268 clear_bit(port
->dev_num
, dev_map
);
1271 static void ib_umad_add_one(struct ib_device
*device
)
1273 struct ib_umad_device
*umad_dev
;
1277 s
= rdma_start_port(device
);
1278 e
= rdma_end_port(device
);
1280 umad_dev
= kzalloc(sizeof *umad_dev
+
1281 (e
- s
+ 1) * sizeof (struct ib_umad_port
),
1286 kobject_init(&umad_dev
->kobj
, &ib_umad_dev_ktype
);
1288 for (i
= s
; i
<= e
; ++i
) {
1289 if (!rdma_cap_ib_mad(device
, i
))
1292 umad_dev
->port
[i
- s
].umad_dev
= umad_dev
;
1294 if (ib_umad_init_port(device
, i
, umad_dev
,
1295 &umad_dev
->port
[i
- s
]))
1304 ib_set_client_data(device
, &umad_client
, umad_dev
);
1310 if (!rdma_cap_ib_mad(device
, i
))
1313 ib_umad_kill_port(&umad_dev
->port
[i
- s
]);
1316 kobject_put(&umad_dev
->kobj
);
1319 static void ib_umad_remove_one(struct ib_device
*device
, void *client_data
)
1321 struct ib_umad_device
*umad_dev
= client_data
;
1327 for (i
= 0; i
<= rdma_end_port(device
) - rdma_start_port(device
); ++i
) {
1328 if (rdma_cap_ib_mad(device
, i
+ rdma_start_port(device
)))
1329 ib_umad_kill_port(&umad_dev
->port
[i
]);
1332 kobject_put(&umad_dev
->kobj
);
1335 static char *umad_devnode(struct device
*dev
, umode_t
*mode
)
1337 return kasprintf(GFP_KERNEL
, "infiniband/%s", dev_name(dev
));
1340 static int __init
ib_umad_init(void)
1344 ret
= register_chrdev_region(base_umad_dev
,
1345 IB_UMAD_NUM_FIXED_MINOR
* 2,
1348 pr_err("couldn't register device number\n");
1352 ret
= alloc_chrdev_region(&dynamic_umad_dev
, 0,
1353 IB_UMAD_NUM_DYNAMIC_MINOR
* 2,
1356 pr_err("couldn't register dynamic device number\n");
1359 dynamic_issm_dev
= dynamic_umad_dev
+ IB_UMAD_NUM_DYNAMIC_MINOR
;
1361 umad_class
= class_create(THIS_MODULE
, "infiniband_mad");
1362 if (IS_ERR(umad_class
)) {
1363 ret
= PTR_ERR(umad_class
);
1364 pr_err("couldn't create class infiniband_mad\n");
1368 umad_class
->devnode
= umad_devnode
;
1370 ret
= class_create_file(umad_class
, &class_attr_abi_version
.attr
);
1372 pr_err("couldn't create abi_version attribute\n");
1376 ret
= ib_register_client(&umad_client
);
1378 pr_err("couldn't register ib_umad client\n");
1385 class_destroy(umad_class
);
1388 unregister_chrdev_region(dynamic_umad_dev
,
1389 IB_UMAD_NUM_DYNAMIC_MINOR
* 2);
1392 unregister_chrdev_region(base_umad_dev
,
1393 IB_UMAD_NUM_FIXED_MINOR
* 2);
1399 static void __exit
ib_umad_cleanup(void)
1401 ib_unregister_client(&umad_client
);
1402 class_destroy(umad_class
);
1403 unregister_chrdev_region(base_umad_dev
,
1404 IB_UMAD_NUM_FIXED_MINOR
* 2);
1405 unregister_chrdev_region(dynamic_umad_dev
,
1406 IB_UMAD_NUM_DYNAMIC_MINOR
* 2);
1409 module_init(ib_umad_init
);
1410 module_exit(ib_umad_cleanup
);