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
35 * $Id: user_mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
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/semaphore.h>
50 #include <linux/smp_lock.h>
52 #include <asm/uaccess.h>
54 #include <rdma/ib_mad.h>
55 #include <rdma/ib_user_mad.h>
57 MODULE_AUTHOR("Roland Dreier");
58 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
59 MODULE_LICENSE("Dual BSD/GPL");
62 IB_UMAD_MAX_PORTS = 64,
63 IB_UMAD_MAX_AGENTS = 32,
66 IB_UMAD_MINOR_BASE = 0
70 * Our lifetime rules for these structs are the following: each time a
71 * device special file is opened, we look up the corresponding struct
72 * ib_umad_port by minor in the umad_port[] table while holding the
73 * port_lock. If this lookup succeeds, we take a reference on the
74 * ib_umad_port's struct ib_umad_device while still holding the
75 * port_lock; if the lookup fails, we fail the open(). We drop these
76 * references in the corresponding close().
78 * In addition to references coming from open character devices, there
79 * is one more reference to each ib_umad_device representing the
80 * module's reference taken when allocating the ib_umad_device in
83 * When destroying an ib_umad_device, we clear all of its
84 * ib_umad_ports from umad_port[] while holding port_lock before
85 * dropping the module's reference to the ib_umad_device. This is
86 * always safe because any open() calls will either succeed and obtain
87 * a reference before we clear the umad_port[] entries, or fail after
88 * we clear the umad_port[] entries.
96 struct device *sm_dev;
97 struct semaphore sm_sem;
99 struct mutex file_mutex;
100 struct list_head file_list;
102 struct ib_device *ib_dev;
103 struct ib_umad_device *umad_dev;
108 struct ib_umad_device {
109 int start_port, end_port;
111 struct ib_umad_port port[0];
114 struct ib_umad_file {
116 struct ib_umad_port *port;
117 struct list_head recv_list;
118 struct list_head send_list;
119 struct list_head port_list;
120 spinlock_t send_lock;
121 wait_queue_head_t recv_wait;
122 struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
128 struct ib_umad_packet {
129 struct ib_mad_send_buf *msg;
130 struct ib_mad_recv_wc *recv_wc;
131 struct list_head list;
133 struct ib_user_mad mad;
136 static struct class *umad_class;
138 static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
140 static DEFINE_SPINLOCK(port_lock);
141 static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS];
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);
147 static void ib_umad_release_dev(struct kref *ref)
149 struct ib_umad_device *dev =
150 container_of(ref, struct ib_umad_device, ref);
155 static int hdr_size(struct ib_umad_file *file)
157 return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
158 sizeof (struct ib_user_mad_hdr_old);
161 /* caller must hold file->mutex */
162 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
164 return file->agents_dead ? NULL : file->agent[id];
167 static int queue_packet(struct ib_umad_file *file,
168 struct ib_mad_agent *agent,
169 struct ib_umad_packet *packet)
173 mutex_lock(&file->mutex);
175 for (packet->mad.hdr.id = 0;
176 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
177 packet->mad.hdr.id++)
178 if (agent == __get_agent(file, packet->mad.hdr.id)) {
179 list_add_tail(&packet->list, &file->recv_list);
180 wake_up_interruptible(&file->recv_wait);
185 mutex_unlock(&file->mutex);
190 static void dequeue_send(struct ib_umad_file *file,
191 struct ib_umad_packet *packet)
193 spin_lock_irq(&file->send_lock);
194 list_del(&packet->list);
195 spin_unlock_irq(&file->send_lock);
198 static void send_handler(struct ib_mad_agent *agent,
199 struct ib_mad_send_wc *send_wc)
201 struct ib_umad_file *file = agent->context;
202 struct ib_umad_packet *packet = send_wc->send_buf->context[0];
204 dequeue_send(file, packet);
205 ib_destroy_ah(packet->msg->ah);
206 ib_free_send_mad(packet->msg);
208 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
209 packet->length = IB_MGMT_MAD_HDR;
210 packet->mad.hdr.status = ETIMEDOUT;
211 if (!queue_packet(file, agent, packet))
217 static void recv_handler(struct ib_mad_agent *agent,
218 struct ib_mad_recv_wc *mad_recv_wc)
220 struct ib_umad_file *file = agent->context;
221 struct ib_umad_packet *packet;
223 if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
226 packet = kzalloc(sizeof *packet, GFP_KERNEL);
230 packet->length = mad_recv_wc->mad_len;
231 packet->recv_wc = mad_recv_wc;
233 packet->mad.hdr.status = 0;
234 packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len;
235 packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
236 packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
237 packet->mad.hdr.sl = mad_recv_wc->wc->sl;
238 packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
239 packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
240 packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
241 if (packet->mad.hdr.grh_present) {
242 struct ib_ah_attr ah_attr;
244 ib_init_ah_from_wc(agent->device, agent->port_num,
245 mad_recv_wc->wc, mad_recv_wc->recv_buf.grh,
248 packet->mad.hdr.gid_index = ah_attr.grh.sgid_index;
249 packet->mad.hdr.hop_limit = ah_attr.grh.hop_limit;
250 packet->mad.hdr.traffic_class = ah_attr.grh.traffic_class;
251 memcpy(packet->mad.hdr.gid, &ah_attr.grh.dgid, 16);
252 packet->mad.hdr.flow_label = cpu_to_be32(ah_attr.grh.flow_label);
255 if (queue_packet(file, agent, packet))
262 ib_free_recv_mad(mad_recv_wc);
265 static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
266 struct ib_umad_packet *packet, size_t count)
268 struct ib_mad_recv_buf *recv_buf;
269 int left, seg_payload, offset, max_seg_payload;
271 /* We need enough room to copy the first (or only) MAD segment. */
272 recv_buf = &packet->recv_wc->recv_buf;
273 if ((packet->length <= sizeof (*recv_buf->mad) &&
274 count < hdr_size(file) + packet->length) ||
275 (packet->length > sizeof (*recv_buf->mad) &&
276 count < hdr_size(file) + sizeof (*recv_buf->mad)))
279 if (copy_to_user(buf, &packet->mad, hdr_size(file)))
282 buf += hdr_size(file);
283 seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
284 if (copy_to_user(buf, recv_buf->mad, seg_payload))
287 if (seg_payload < packet->length) {
289 * Multipacket RMPP MAD message. Copy remainder of message.
290 * Note that last segment may have a shorter payload.
292 if (count < hdr_size(file) + packet->length) {
294 * The buffer is too small, return the first RMPP segment,
295 * which includes the RMPP message length.
299 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
300 max_seg_payload = sizeof (struct ib_mad) - offset;
302 for (left = packet->length - seg_payload, buf += seg_payload;
303 left; left -= seg_payload, buf += seg_payload) {
304 recv_buf = container_of(recv_buf->list.next,
305 struct ib_mad_recv_buf, list);
306 seg_payload = min(left, max_seg_payload);
307 if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
312 return hdr_size(file) + packet->length;
315 static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
316 struct ib_umad_packet *packet, size_t count)
318 ssize_t size = hdr_size(file) + packet->length;
323 if (copy_to_user(buf, &packet->mad, hdr_size(file)))
326 buf += hdr_size(file);
328 if (copy_to_user(buf, packet->mad.data, packet->length))
334 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
335 size_t count, loff_t *pos)
337 struct ib_umad_file *file = filp->private_data;
338 struct ib_umad_packet *packet;
341 if (count < hdr_size(file))
344 mutex_lock(&file->mutex);
346 while (list_empty(&file->recv_list)) {
347 mutex_unlock(&file->mutex);
349 if (filp->f_flags & O_NONBLOCK)
352 if (wait_event_interruptible(file->recv_wait,
353 !list_empty(&file->recv_list)))
356 mutex_lock(&file->mutex);
359 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
360 list_del(&packet->list);
362 mutex_unlock(&file->mutex);
365 ret = copy_recv_mad(file, buf, packet, count);
367 ret = copy_send_mad(file, buf, packet, count);
371 mutex_lock(&file->mutex);
372 list_add(&packet->list, &file->recv_list);
373 mutex_unlock(&file->mutex);
376 ib_free_recv_mad(packet->recv_wc);
382 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
386 /* Copy class specific header */
387 if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
388 copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
389 msg->hdr_len - IB_MGMT_RMPP_HDR))
392 /* All headers are in place. Copy data segments. */
393 for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
394 seg++, left -= msg->seg_size, buf += msg->seg_size) {
395 if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
396 min(left, msg->seg_size)))
402 static int same_destination(struct ib_user_mad_hdr *hdr1,
403 struct ib_user_mad_hdr *hdr2)
405 if (!hdr1->grh_present && !hdr2->grh_present)
406 return (hdr1->lid == hdr2->lid);
408 if (hdr1->grh_present && hdr2->grh_present)
409 return !memcmp(hdr1->gid, hdr2->gid, 16);
414 static int is_duplicate(struct ib_umad_file *file,
415 struct ib_umad_packet *packet)
417 struct ib_umad_packet *sent_packet;
418 struct ib_mad_hdr *sent_hdr, *hdr;
420 hdr = (struct ib_mad_hdr *) packet->mad.data;
421 list_for_each_entry(sent_packet, &file->send_list, list) {
422 sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
424 if ((hdr->tid != sent_hdr->tid) ||
425 (hdr->mgmt_class != sent_hdr->mgmt_class))
429 * No need to be overly clever here. If two new operations have
430 * the same TID, reject the second as a duplicate. This is more
431 * restrictive than required by the spec.
433 if (!ib_response_mad((struct ib_mad *) hdr)) {
434 if (!ib_response_mad((struct ib_mad *) sent_hdr))
437 } else if (!ib_response_mad((struct ib_mad *) sent_hdr))
440 if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
447 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
448 size_t count, loff_t *pos)
450 struct ib_umad_file *file = filp->private_data;
451 struct ib_umad_packet *packet;
452 struct ib_mad_agent *agent;
453 struct ib_ah_attr ah_attr;
455 struct ib_rmpp_mad *rmpp_mad;
457 int ret, data_len, hdr_len, copy_offset, rmpp_active;
459 if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
462 packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
466 if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
471 if (packet->mad.hdr.id < 0 ||
472 packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
477 buf += hdr_size(file);
479 if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
484 mutex_lock(&file->mutex);
486 agent = __get_agent(file, packet->mad.hdr.id);
492 memset(&ah_attr, 0, sizeof ah_attr);
493 ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
494 ah_attr.sl = packet->mad.hdr.sl;
495 ah_attr.src_path_bits = packet->mad.hdr.path_bits;
496 ah_attr.port_num = file->port->port_num;
497 if (packet->mad.hdr.grh_present) {
498 ah_attr.ah_flags = IB_AH_GRH;
499 memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
500 ah_attr.grh.sgid_index = packet->mad.hdr.gid_index;
501 ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
502 ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
503 ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
506 ah = ib_create_ah(agent->qp->pd, &ah_attr);
512 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
513 hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
514 if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) {
515 copy_offset = IB_MGMT_MAD_HDR;
518 copy_offset = IB_MGMT_RMPP_HDR;
519 rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
520 IB_MGMT_RMPP_FLAG_ACTIVE;
523 data_len = count - hdr_size(file) - hdr_len;
524 packet->msg = ib_create_send_mad(agent,
525 be32_to_cpu(packet->mad.hdr.qpn),
526 packet->mad.hdr.pkey_index, rmpp_active,
527 hdr_len, data_len, GFP_KERNEL);
528 if (IS_ERR(packet->msg)) {
529 ret = PTR_ERR(packet->msg);
533 packet->msg->ah = ah;
534 packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
535 packet->msg->retries = packet->mad.hdr.retries;
536 packet->msg->context[0] = packet;
538 /* Copy MAD header. Any RMPP header is already in place. */
539 memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
542 if (copy_from_user(packet->msg->mad + copy_offset,
544 hdr_len + data_len - copy_offset)) {
549 ret = copy_rmpp_mad(packet->msg, buf);
555 * Set the high-order part of the transaction ID to make MADs from
556 * different agents unique, and allow routing responses back to the
557 * original requestor.
559 if (!ib_response_mad(packet->msg->mad)) {
560 tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
561 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
562 (be64_to_cpup(tid) & 0xffffffff));
563 rmpp_mad->mad_hdr.tid = *tid;
566 spin_lock_irq(&file->send_lock);
567 ret = is_duplicate(file, packet);
569 list_add_tail(&packet->list, &file->send_list);
570 spin_unlock_irq(&file->send_lock);
576 ret = ib_post_send_mad(packet->msg, NULL);
580 mutex_unlock(&file->mutex);
584 dequeue_send(file, packet);
586 ib_free_send_mad(packet->msg);
590 mutex_unlock(&file->mutex);
596 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
598 struct ib_umad_file *file = filp->private_data;
600 /* we will always be able to post a MAD send */
601 unsigned int mask = POLLOUT | POLLWRNORM;
603 poll_wait(filp, &file->recv_wait, wait);
605 if (!list_empty(&file->recv_list))
606 mask |= POLLIN | POLLRDNORM;
611 static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
612 int compat_method_mask)
614 struct ib_user_mad_reg_req ureq;
615 struct ib_mad_reg_req req;
616 struct ib_mad_agent *agent = NULL;
620 mutex_lock(&file->port->file_mutex);
621 mutex_lock(&file->mutex);
623 if (!file->port->ib_dev) {
628 if (copy_from_user(&ureq, arg, sizeof ureq)) {
633 if (ureq.qpn != 0 && ureq.qpn != 1) {
638 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
639 if (!__get_agent(file, agent_id))
646 if (ureq.mgmt_class) {
647 req.mgmt_class = ureq.mgmt_class;
648 req.mgmt_class_version = ureq.mgmt_class_version;
649 memcpy(req.oui, ureq.oui, sizeof req.oui);
651 if (compat_method_mask) {
652 u32 *umm = (u32 *) ureq.method_mask;
655 for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
657 umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
659 memcpy(req.method_mask, ureq.method_mask,
660 sizeof req.method_mask);
663 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
664 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
665 ureq.mgmt_class ? &req : NULL,
667 send_handler, recv_handler, file);
669 ret = PTR_ERR(agent);
674 if (put_user(agent_id,
675 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
680 if (!file->already_used) {
681 file->already_used = 1;
682 if (!file->use_pkey_index) {
683 printk(KERN_WARNING "user_mad: process %s did not enable "
684 "P_Key index support.\n", current->comm);
685 printk(KERN_WARNING "user_mad: Documentation/infiniband/user_mad.txt "
686 "has info on the new ABI.\n");
690 file->agent[agent_id] = agent;
694 mutex_unlock(&file->mutex);
697 ib_unregister_mad_agent(agent);
699 mutex_unlock(&file->port->file_mutex);
704 static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
706 struct ib_mad_agent *agent = NULL;
710 if (get_user(id, arg))
713 mutex_lock(&file->port->file_mutex);
714 mutex_lock(&file->mutex);
716 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
721 agent = file->agent[id];
722 file->agent[id] = NULL;
725 mutex_unlock(&file->mutex);
728 ib_unregister_mad_agent(agent);
730 mutex_unlock(&file->port->file_mutex);
735 static long ib_umad_enable_pkey(struct ib_umad_file *file)
739 mutex_lock(&file->mutex);
740 if (file->already_used)
743 file->use_pkey_index = 1;
744 mutex_unlock(&file->mutex);
749 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
753 case IB_USER_MAD_REGISTER_AGENT:
754 return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
755 case IB_USER_MAD_UNREGISTER_AGENT:
756 return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
757 case IB_USER_MAD_ENABLE_PKEY:
758 return ib_umad_enable_pkey(filp->private_data);
765 static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
769 case IB_USER_MAD_REGISTER_AGENT:
770 return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
771 case IB_USER_MAD_UNREGISTER_AGENT:
772 return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
773 case IB_USER_MAD_ENABLE_PKEY:
774 return ib_umad_enable_pkey(filp->private_data);
781 static int ib_umad_open(struct inode *inode, struct file *filp)
783 struct ib_umad_port *port;
784 struct ib_umad_file *file;
788 spin_lock(&port_lock);
789 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
791 kref_get(&port->umad_dev->ref);
792 spin_unlock(&port_lock);
799 mutex_lock(&port->file_mutex);
806 file = kzalloc(sizeof *file, GFP_KERNEL);
808 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
813 mutex_init(&file->mutex);
814 spin_lock_init(&file->send_lock);
815 INIT_LIST_HEAD(&file->recv_list);
816 INIT_LIST_HEAD(&file->send_list);
817 init_waitqueue_head(&file->recv_wait);
820 filp->private_data = file;
822 list_add_tail(&file->port_list, &port->file_list);
825 mutex_unlock(&port->file_mutex);
830 static int ib_umad_close(struct inode *inode, struct file *filp)
832 struct ib_umad_file *file = filp->private_data;
833 struct ib_umad_device *dev = file->port->umad_dev;
834 struct ib_umad_packet *packet, *tmp;
838 mutex_lock(&file->port->file_mutex);
839 mutex_lock(&file->mutex);
841 already_dead = file->agents_dead;
842 file->agents_dead = 1;
844 list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
846 ib_free_recv_mad(packet->recv_wc);
850 list_del(&file->port_list);
852 mutex_unlock(&file->mutex);
855 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
857 ib_unregister_mad_agent(file->agent[i]);
859 mutex_unlock(&file->port->file_mutex);
862 kref_put(&dev->ref, ib_umad_release_dev);
867 static const struct file_operations umad_fops = {
868 .owner = THIS_MODULE,
869 .read = ib_umad_read,
870 .write = ib_umad_write,
871 .poll = ib_umad_poll,
872 .unlocked_ioctl = ib_umad_ioctl,
874 .compat_ioctl = ib_umad_compat_ioctl,
876 .open = ib_umad_open,
877 .release = ib_umad_close
880 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
882 struct ib_umad_port *port;
883 struct ib_port_modify props = {
884 .set_port_cap_mask = IB_PORT_SM
888 spin_lock(&port_lock);
889 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
891 kref_get(&port->umad_dev->ref);
892 spin_unlock(&port_lock);
897 if (filp->f_flags & O_NONBLOCK) {
898 if (down_trylock(&port->sm_sem)) {
903 if (down_interruptible(&port->sm_sem)) {
909 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
915 filp->private_data = port;
920 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
924 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
926 struct ib_umad_port *port = filp->private_data;
927 struct ib_port_modify props = {
928 .clr_port_cap_mask = IB_PORT_SM
932 mutex_lock(&port->file_mutex);
934 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
935 mutex_unlock(&port->file_mutex);
939 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
944 static const struct file_operations umad_sm_fops = {
945 .owner = THIS_MODULE,
946 .open = ib_umad_sm_open,
947 .release = ib_umad_sm_close
950 static struct ib_client umad_client = {
952 .add = ib_umad_add_one,
953 .remove = ib_umad_remove_one
956 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
959 struct ib_umad_port *port = dev_get_drvdata(dev);
964 return sprintf(buf, "%s\n", port->ib_dev->name);
966 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
968 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
971 struct ib_umad_port *port = dev_get_drvdata(dev);
976 return sprintf(buf, "%d\n", port->port_num);
978 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
980 static ssize_t show_abi_version(struct class *class, char *buf)
982 return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
984 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
986 static int ib_umad_init_port(struct ib_device *device, int port_num,
987 struct ib_umad_port *port)
989 spin_lock(&port_lock);
990 port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
991 if (port->dev_num >= IB_UMAD_MAX_PORTS) {
992 spin_unlock(&port_lock);
995 set_bit(port->dev_num, dev_map);
996 spin_unlock(&port_lock);
998 port->ib_dev = device;
999 port->port_num = port_num;
1000 init_MUTEX(&port->sm_sem);
1001 mutex_init(&port->file_mutex);
1002 INIT_LIST_HEAD(&port->file_list);
1004 port->cdev = cdev_alloc();
1007 port->cdev->owner = THIS_MODULE;
1008 port->cdev->ops = &umad_fops;
1009 kobject_set_name(&port->cdev->kobj, "umad%d", port->dev_num);
1010 if (cdev_add(port->cdev, base_dev + port->dev_num, 1))
1013 port->dev = device_create(umad_class, device->dma_device,
1014 port->cdev->dev, "umad%d", port->dev_num);
1015 if (IS_ERR(port->dev))
1018 if (device_create_file(port->dev, &dev_attr_ibdev))
1020 if (device_create_file(port->dev, &dev_attr_port))
1023 port->sm_cdev = cdev_alloc();
1026 port->sm_cdev->owner = THIS_MODULE;
1027 port->sm_cdev->ops = &umad_sm_fops;
1028 kobject_set_name(&port->sm_cdev->kobj, "issm%d", port->dev_num);
1029 if (cdev_add(port->sm_cdev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
1032 port->sm_dev = device_create(umad_class, device->dma_device,
1034 "issm%d", port->dev_num);
1035 if (IS_ERR(port->sm_dev))
1038 dev_set_drvdata(port->dev, port);
1039 dev_set_drvdata(port->sm_dev, port);
1041 if (device_create_file(port->sm_dev, &dev_attr_ibdev))
1043 if (device_create_file(port->sm_dev, &dev_attr_port))
1046 spin_lock(&port_lock);
1047 umad_port[port->dev_num] = port;
1048 spin_unlock(&port_lock);
1053 device_destroy(umad_class, port->sm_cdev->dev);
1056 cdev_del(port->sm_cdev);
1059 device_destroy(umad_class, port->cdev->dev);
1062 cdev_del(port->cdev);
1063 clear_bit(port->dev_num, dev_map);
1068 static void ib_umad_kill_port(struct ib_umad_port *port)
1070 struct ib_umad_file *file;
1074 dev_set_drvdata(port->dev, NULL);
1075 dev_set_drvdata(port->sm_dev, NULL);
1077 device_destroy(umad_class, port->cdev->dev);
1078 device_destroy(umad_class, port->sm_cdev->dev);
1080 cdev_del(port->cdev);
1081 cdev_del(port->sm_cdev);
1083 spin_lock(&port_lock);
1084 umad_port[port->dev_num] = NULL;
1085 spin_unlock(&port_lock);
1087 mutex_lock(&port->file_mutex);
1089 port->ib_dev = NULL;
1091 list_for_each_entry(file, &port->file_list, port_list) {
1092 mutex_lock(&file->mutex);
1093 already_dead = file->agents_dead;
1094 file->agents_dead = 1;
1095 mutex_unlock(&file->mutex);
1097 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
1098 if (file->agent[id])
1099 ib_unregister_mad_agent(file->agent[id]);
1102 mutex_unlock(&port->file_mutex);
1104 clear_bit(port->dev_num, dev_map);
1107 static void ib_umad_add_one(struct ib_device *device)
1109 struct ib_umad_device *umad_dev;
1112 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1115 if (device->node_type == RDMA_NODE_IB_SWITCH)
1119 e = device->phys_port_cnt;
1122 umad_dev = kzalloc(sizeof *umad_dev +
1123 (e - s + 1) * sizeof (struct ib_umad_port),
1128 kref_init(&umad_dev->ref);
1130 umad_dev->start_port = s;
1131 umad_dev->end_port = e;
1133 for (i = s; i <= e; ++i) {
1134 umad_dev->port[i - s].umad_dev = umad_dev;
1136 if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
1140 ib_set_client_data(device, &umad_client, umad_dev);
1146 ib_umad_kill_port(&umad_dev->port[i - s]);
1148 kref_put(&umad_dev->ref, ib_umad_release_dev);
1151 static void ib_umad_remove_one(struct ib_device *device)
1153 struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
1159 for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
1160 ib_umad_kill_port(&umad_dev->port[i]);
1162 kref_put(&umad_dev->ref, ib_umad_release_dev);
1165 static int __init ib_umad_init(void)
1169 ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
1172 printk(KERN_ERR "user_mad: couldn't register device number\n");
1176 umad_class = class_create(THIS_MODULE, "infiniband_mad");
1177 if (IS_ERR(umad_class)) {
1178 ret = PTR_ERR(umad_class);
1179 printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
1183 ret = class_create_file(umad_class, &class_attr_abi_version);
1185 printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
1189 ret = ib_register_client(&umad_client);
1191 printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
1198 class_destroy(umad_class);
1201 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1207 static void __exit ib_umad_cleanup(void)
1209 ib_unregister_client(&umad_client);
1210 class_destroy(umad_class);
1211 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1214 module_init(ib_umad_init);
1215 module_exit(ib_umad_cleanup);