Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / infiniband / core / user_mad.c
blobd97ded25c4ff98bb7c8e68fa299f569bcfec1eb3
1 /*
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
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
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
32 * SOFTWARE.
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>
41 #include <linux/fs.h>
42 #include <linux/cdev.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/poll.h>
45 #include <linux/rwsem.h>
46 #include <linux/kref.h>
48 #include <asm/uaccess.h>
49 #include <asm/semaphore.h>
51 #include <rdma/ib_mad.h>
52 #include <rdma/ib_user_mad.h>
54 MODULE_AUTHOR("Roland Dreier");
55 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
56 MODULE_LICENSE("Dual BSD/GPL");
58 enum {
59 IB_UMAD_MAX_PORTS = 64,
60 IB_UMAD_MAX_AGENTS = 32,
62 IB_UMAD_MAJOR = 231,
63 IB_UMAD_MINOR_BASE = 0
67 * Our lifetime rules for these structs are the following: each time a
68 * device special file is opened, we look up the corresponding struct
69 * ib_umad_port by minor in the umad_port[] table while holding the
70 * port_lock. If this lookup succeeds, we take a reference on the
71 * ib_umad_port's struct ib_umad_device while still holding the
72 * port_lock; if the lookup fails, we fail the open(). We drop these
73 * references in the corresponding close().
75 * In addition to references coming from open character devices, there
76 * is one more reference to each ib_umad_device representing the
77 * module's reference taken when allocating the ib_umad_device in
78 * ib_umad_add_one().
80 * When destroying an ib_umad_device, we clear all of its
81 * ib_umad_ports from umad_port[] while holding port_lock before
82 * dropping the module's reference to the ib_umad_device. This is
83 * always safe because any open() calls will either succeed and obtain
84 * a reference before we clear the umad_port[] entries, or fail after
85 * we clear the umad_port[] entries.
88 struct ib_umad_port {
89 struct cdev *dev;
90 struct class_device *class_dev;
92 struct cdev *sm_dev;
93 struct class_device *sm_class_dev;
94 struct semaphore sm_sem;
96 struct rw_semaphore mutex;
97 struct list_head file_list;
99 struct ib_device *ib_dev;
100 struct ib_umad_device *umad_dev;
101 int dev_num;
102 u8 port_num;
105 struct ib_umad_device {
106 int start_port, end_port;
107 struct kref ref;
108 struct ib_umad_port port[0];
111 struct ib_umad_file {
112 struct ib_umad_port *port;
113 struct list_head recv_list;
114 struct list_head send_list;
115 struct list_head port_list;
116 spinlock_t recv_lock;
117 spinlock_t send_lock;
118 wait_queue_head_t recv_wait;
119 struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
120 int agents_dead;
123 struct ib_umad_packet {
124 struct ib_mad_send_buf *msg;
125 struct ib_mad_recv_wc *recv_wc;
126 struct list_head list;
127 int length;
128 struct ib_user_mad mad;
131 static struct class *umad_class;
133 static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
135 static DEFINE_SPINLOCK(port_lock);
136 static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS];
137 static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS);
139 static void ib_umad_add_one(struct ib_device *device);
140 static void ib_umad_remove_one(struct ib_device *device);
142 static void ib_umad_release_dev(struct kref *ref)
144 struct ib_umad_device *dev =
145 container_of(ref, struct ib_umad_device, ref);
147 kfree(dev);
150 /* caller must hold port->mutex at least for reading */
151 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
153 return file->agents_dead ? NULL : file->agent[id];
156 static int queue_packet(struct ib_umad_file *file,
157 struct ib_mad_agent *agent,
158 struct ib_umad_packet *packet)
160 int ret = 1;
162 down_read(&file->port->mutex);
164 for (packet->mad.hdr.id = 0;
165 packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
166 packet->mad.hdr.id++)
167 if (agent == __get_agent(file, packet->mad.hdr.id)) {
168 spin_lock_irq(&file->recv_lock);
169 list_add_tail(&packet->list, &file->recv_list);
170 spin_unlock_irq(&file->recv_lock);
171 wake_up_interruptible(&file->recv_wait);
172 ret = 0;
173 break;
176 up_read(&file->port->mutex);
178 return ret;
181 static void dequeue_send(struct ib_umad_file *file,
182 struct ib_umad_packet *packet)
184 spin_lock_irq(&file->send_lock);
185 list_del(&packet->list);
186 spin_unlock_irq(&file->send_lock);
189 static void send_handler(struct ib_mad_agent *agent,
190 struct ib_mad_send_wc *send_wc)
192 struct ib_umad_file *file = agent->context;
193 struct ib_umad_packet *packet = send_wc->send_buf->context[0];
195 dequeue_send(file, packet);
196 ib_destroy_ah(packet->msg->ah);
197 ib_free_send_mad(packet->msg);
199 if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
200 packet->length = IB_MGMT_MAD_HDR;
201 packet->mad.hdr.status = ETIMEDOUT;
202 if (!queue_packet(file, agent, packet))
203 return;
205 kfree(packet);
208 static void recv_handler(struct ib_mad_agent *agent,
209 struct ib_mad_recv_wc *mad_recv_wc)
211 struct ib_umad_file *file = agent->context;
212 struct ib_umad_packet *packet;
214 if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
215 goto err1;
217 packet = kzalloc(sizeof *packet, GFP_KERNEL);
218 if (!packet)
219 goto err1;
221 packet->length = mad_recv_wc->mad_len;
222 packet->recv_wc = mad_recv_wc;
224 packet->mad.hdr.status = 0;
225 packet->mad.hdr.length = sizeof (struct ib_user_mad) +
226 mad_recv_wc->mad_len;
227 packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
228 packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
229 packet->mad.hdr.sl = mad_recv_wc->wc->sl;
230 packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
231 packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
232 if (packet->mad.hdr.grh_present) {
233 struct ib_ah_attr ah_attr;
235 ib_init_ah_from_wc(agent->device, agent->port_num,
236 mad_recv_wc->wc, mad_recv_wc->recv_buf.grh,
237 &ah_attr);
239 packet->mad.hdr.gid_index = ah_attr.grh.sgid_index;
240 packet->mad.hdr.hop_limit = ah_attr.grh.hop_limit;
241 packet->mad.hdr.traffic_class = ah_attr.grh.traffic_class;
242 memcpy(packet->mad.hdr.gid, &ah_attr.grh.dgid, 16);
243 packet->mad.hdr.flow_label = cpu_to_be32(ah_attr.grh.flow_label);
246 if (queue_packet(file, agent, packet))
247 goto err2;
248 return;
250 err2:
251 kfree(packet);
252 err1:
253 ib_free_recv_mad(mad_recv_wc);
256 static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
257 size_t count)
259 struct ib_mad_recv_buf *recv_buf;
260 int left, seg_payload, offset, max_seg_payload;
262 /* We need enough room to copy the first (or only) MAD segment. */
263 recv_buf = &packet->recv_wc->recv_buf;
264 if ((packet->length <= sizeof (*recv_buf->mad) &&
265 count < sizeof (packet->mad) + packet->length) ||
266 (packet->length > sizeof (*recv_buf->mad) &&
267 count < sizeof (packet->mad) + sizeof (*recv_buf->mad)))
268 return -EINVAL;
270 if (copy_to_user(buf, &packet->mad, sizeof (packet->mad)))
271 return -EFAULT;
273 buf += sizeof (packet->mad);
274 seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
275 if (copy_to_user(buf, recv_buf->mad, seg_payload))
276 return -EFAULT;
278 if (seg_payload < packet->length) {
280 * Multipacket RMPP MAD message. Copy remainder of message.
281 * Note that last segment may have a shorter payload.
283 if (count < sizeof (packet->mad) + packet->length) {
285 * The buffer is too small, return the first RMPP segment,
286 * which includes the RMPP message length.
288 return -ENOSPC;
290 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
291 max_seg_payload = sizeof (struct ib_mad) - offset;
293 for (left = packet->length - seg_payload, buf += seg_payload;
294 left; left -= seg_payload, buf += seg_payload) {
295 recv_buf = container_of(recv_buf->list.next,
296 struct ib_mad_recv_buf, list);
297 seg_payload = min(left, max_seg_payload);
298 if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
299 seg_payload))
300 return -EFAULT;
303 return sizeof (packet->mad) + packet->length;
306 static ssize_t copy_send_mad(char __user *buf, struct ib_umad_packet *packet,
307 size_t count)
309 ssize_t size = sizeof (packet->mad) + packet->length;
311 if (count < size)
312 return -EINVAL;
314 if (copy_to_user(buf, &packet->mad, size))
315 return -EFAULT;
317 return size;
320 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
321 size_t count, loff_t *pos)
323 struct ib_umad_file *file = filp->private_data;
324 struct ib_umad_packet *packet;
325 ssize_t ret;
327 if (count < sizeof (struct ib_user_mad))
328 return -EINVAL;
330 spin_lock_irq(&file->recv_lock);
332 while (list_empty(&file->recv_list)) {
333 spin_unlock_irq(&file->recv_lock);
335 if (filp->f_flags & O_NONBLOCK)
336 return -EAGAIN;
338 if (wait_event_interruptible(file->recv_wait,
339 !list_empty(&file->recv_list)))
340 return -ERESTARTSYS;
342 spin_lock_irq(&file->recv_lock);
345 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
346 list_del(&packet->list);
348 spin_unlock_irq(&file->recv_lock);
350 if (packet->recv_wc)
351 ret = copy_recv_mad(buf, packet, count);
352 else
353 ret = copy_send_mad(buf, packet, count);
355 if (ret < 0) {
356 /* Requeue packet */
357 spin_lock_irq(&file->recv_lock);
358 list_add(&packet->list, &file->recv_list);
359 spin_unlock_irq(&file->recv_lock);
360 } else {
361 if (packet->recv_wc)
362 ib_free_recv_mad(packet->recv_wc);
363 kfree(packet);
365 return ret;
368 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
370 int left, seg;
372 /* Copy class specific header */
373 if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
374 copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
375 msg->hdr_len - IB_MGMT_RMPP_HDR))
376 return -EFAULT;
378 /* All headers are in place. Copy data segments. */
379 for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
380 seg++, left -= msg->seg_size, buf += msg->seg_size) {
381 if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
382 min(left, msg->seg_size)))
383 return -EFAULT;
385 return 0;
388 static int same_destination(struct ib_user_mad_hdr *hdr1,
389 struct ib_user_mad_hdr *hdr2)
391 if (!hdr1->grh_present && !hdr2->grh_present)
392 return (hdr1->lid == hdr2->lid);
394 if (hdr1->grh_present && hdr2->grh_present)
395 return !memcmp(hdr1->gid, hdr2->gid, 16);
397 return 0;
400 static int is_duplicate(struct ib_umad_file *file,
401 struct ib_umad_packet *packet)
403 struct ib_umad_packet *sent_packet;
404 struct ib_mad_hdr *sent_hdr, *hdr;
406 hdr = (struct ib_mad_hdr *) packet->mad.data;
407 list_for_each_entry(sent_packet, &file->send_list, list) {
408 sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
410 if ((hdr->tid != sent_hdr->tid) ||
411 (hdr->mgmt_class != sent_hdr->mgmt_class))
412 continue;
415 * No need to be overly clever here. If two new operations have
416 * the same TID, reject the second as a duplicate. This is more
417 * restrictive than required by the spec.
419 if (!ib_response_mad((struct ib_mad *) hdr)) {
420 if (!ib_response_mad((struct ib_mad *) sent_hdr))
421 return 1;
422 continue;
423 } else if (!ib_response_mad((struct ib_mad *) sent_hdr))
424 continue;
426 if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
427 return 1;
430 return 0;
433 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
434 size_t count, loff_t *pos)
436 struct ib_umad_file *file = filp->private_data;
437 struct ib_umad_packet *packet;
438 struct ib_mad_agent *agent;
439 struct ib_ah_attr ah_attr;
440 struct ib_ah *ah;
441 struct ib_rmpp_mad *rmpp_mad;
442 __be64 *tid;
443 int ret, data_len, hdr_len, copy_offset, rmpp_active;
445 if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)
446 return -EINVAL;
448 packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
449 if (!packet)
450 return -ENOMEM;
452 if (copy_from_user(&packet->mad, buf,
453 sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
454 ret = -EFAULT;
455 goto err;
458 if (packet->mad.hdr.id < 0 ||
459 packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
460 ret = -EINVAL;
461 goto err;
464 down_read(&file->port->mutex);
466 agent = __get_agent(file, packet->mad.hdr.id);
467 if (!agent) {
468 ret = -EINVAL;
469 goto err_up;
472 memset(&ah_attr, 0, sizeof ah_attr);
473 ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
474 ah_attr.sl = packet->mad.hdr.sl;
475 ah_attr.src_path_bits = packet->mad.hdr.path_bits;
476 ah_attr.port_num = file->port->port_num;
477 if (packet->mad.hdr.grh_present) {
478 ah_attr.ah_flags = IB_AH_GRH;
479 memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
480 ah_attr.grh.sgid_index = packet->mad.hdr.gid_index;
481 ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
482 ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
483 ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
486 ah = ib_create_ah(agent->qp->pd, &ah_attr);
487 if (IS_ERR(ah)) {
488 ret = PTR_ERR(ah);
489 goto err_up;
492 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
493 hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
494 if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) {
495 copy_offset = IB_MGMT_MAD_HDR;
496 rmpp_active = 0;
497 } else {
498 copy_offset = IB_MGMT_RMPP_HDR;
499 rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
500 IB_MGMT_RMPP_FLAG_ACTIVE;
503 data_len = count - sizeof (struct ib_user_mad) - hdr_len;
504 packet->msg = ib_create_send_mad(agent,
505 be32_to_cpu(packet->mad.hdr.qpn),
506 0, rmpp_active, hdr_len,
507 data_len, GFP_KERNEL);
508 if (IS_ERR(packet->msg)) {
509 ret = PTR_ERR(packet->msg);
510 goto err_ah;
513 packet->msg->ah = ah;
514 packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
515 packet->msg->retries = packet->mad.hdr.retries;
516 packet->msg->context[0] = packet;
518 /* Copy MAD header. Any RMPP header is already in place. */
519 memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
520 buf += sizeof (struct ib_user_mad);
522 if (!rmpp_active) {
523 if (copy_from_user(packet->msg->mad + copy_offset,
524 buf + copy_offset,
525 hdr_len + data_len - copy_offset)) {
526 ret = -EFAULT;
527 goto err_msg;
529 } else {
530 ret = copy_rmpp_mad(packet->msg, buf);
531 if (ret)
532 goto err_msg;
536 * Set the high-order part of the transaction ID to make MADs from
537 * different agents unique, and allow routing responses back to the
538 * original requestor.
540 if (!ib_response_mad(packet->msg->mad)) {
541 tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
542 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
543 (be64_to_cpup(tid) & 0xffffffff));
544 rmpp_mad->mad_hdr.tid = *tid;
547 spin_lock_irq(&file->send_lock);
548 ret = is_duplicate(file, packet);
549 if (!ret)
550 list_add_tail(&packet->list, &file->send_list);
551 spin_unlock_irq(&file->send_lock);
552 if (ret) {
553 ret = -EINVAL;
554 goto err_msg;
557 ret = ib_post_send_mad(packet->msg, NULL);
558 if (ret)
559 goto err_send;
561 up_read(&file->port->mutex);
562 return count;
564 err_send:
565 dequeue_send(file, packet);
566 err_msg:
567 ib_free_send_mad(packet->msg);
568 err_ah:
569 ib_destroy_ah(ah);
570 err_up:
571 up_read(&file->port->mutex);
572 err:
573 kfree(packet);
574 return ret;
577 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
579 struct ib_umad_file *file = filp->private_data;
581 /* we will always be able to post a MAD send */
582 unsigned int mask = POLLOUT | POLLWRNORM;
584 poll_wait(filp, &file->recv_wait, wait);
586 if (!list_empty(&file->recv_list))
587 mask |= POLLIN | POLLRDNORM;
589 return mask;
592 static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
594 struct ib_user_mad_reg_req ureq;
595 struct ib_mad_reg_req req;
596 struct ib_mad_agent *agent;
597 int agent_id;
598 int ret;
600 down_write(&file->port->mutex);
602 if (!file->port->ib_dev) {
603 ret = -EPIPE;
604 goto out;
607 if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
608 ret = -EFAULT;
609 goto out;
612 if (ureq.qpn != 0 && ureq.qpn != 1) {
613 ret = -EINVAL;
614 goto out;
617 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
618 if (!__get_agent(file, agent_id))
619 goto found;
621 ret = -ENOMEM;
622 goto out;
624 found:
625 if (ureq.mgmt_class) {
626 req.mgmt_class = ureq.mgmt_class;
627 req.mgmt_class_version = ureq.mgmt_class_version;
628 memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
629 memcpy(req.oui, ureq.oui, sizeof req.oui);
632 agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
633 ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
634 ureq.mgmt_class ? &req : NULL,
635 ureq.rmpp_version,
636 send_handler, recv_handler, file);
637 if (IS_ERR(agent)) {
638 ret = PTR_ERR(agent);
639 goto out;
642 if (put_user(agent_id,
643 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
644 ret = -EFAULT;
645 ib_unregister_mad_agent(agent);
646 goto out;
649 file->agent[agent_id] = agent;
650 ret = 0;
652 out:
653 up_write(&file->port->mutex);
654 return ret;
657 static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
659 struct ib_mad_agent *agent = NULL;
660 u32 id;
661 int ret = 0;
663 if (get_user(id, (u32 __user *) arg))
664 return -EFAULT;
666 down_write(&file->port->mutex);
668 if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
669 ret = -EINVAL;
670 goto out;
673 agent = file->agent[id];
674 file->agent[id] = NULL;
676 out:
677 up_write(&file->port->mutex);
679 if (agent)
680 ib_unregister_mad_agent(agent);
682 return ret;
685 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
686 unsigned long arg)
688 switch (cmd) {
689 case IB_USER_MAD_REGISTER_AGENT:
690 return ib_umad_reg_agent(filp->private_data, arg);
691 case IB_USER_MAD_UNREGISTER_AGENT:
692 return ib_umad_unreg_agent(filp->private_data, arg);
693 default:
694 return -ENOIOCTLCMD;
698 static int ib_umad_open(struct inode *inode, struct file *filp)
700 struct ib_umad_port *port;
701 struct ib_umad_file *file;
702 int ret = 0;
704 spin_lock(&port_lock);
705 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
706 if (port)
707 kref_get(&port->umad_dev->ref);
708 spin_unlock(&port_lock);
710 if (!port)
711 return -ENXIO;
713 down_write(&port->mutex);
715 if (!port->ib_dev) {
716 ret = -ENXIO;
717 goto out;
720 file = kzalloc(sizeof *file, GFP_KERNEL);
721 if (!file) {
722 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
723 ret = -ENOMEM;
724 goto out;
727 spin_lock_init(&file->recv_lock);
728 spin_lock_init(&file->send_lock);
729 INIT_LIST_HEAD(&file->recv_list);
730 INIT_LIST_HEAD(&file->send_list);
731 init_waitqueue_head(&file->recv_wait);
733 file->port = port;
734 filp->private_data = file;
736 list_add_tail(&file->port_list, &port->file_list);
738 out:
739 up_write(&port->mutex);
740 return ret;
743 static int ib_umad_close(struct inode *inode, struct file *filp)
745 struct ib_umad_file *file = filp->private_data;
746 struct ib_umad_device *dev = file->port->umad_dev;
747 struct ib_umad_packet *packet, *tmp;
748 int already_dead;
749 int i;
751 down_write(&file->port->mutex);
753 already_dead = file->agents_dead;
754 file->agents_dead = 1;
756 list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
757 if (packet->recv_wc)
758 ib_free_recv_mad(packet->recv_wc);
759 kfree(packet);
762 list_del(&file->port_list);
764 downgrade_write(&file->port->mutex);
766 if (!already_dead)
767 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
768 if (file->agent[i])
769 ib_unregister_mad_agent(file->agent[i]);
771 up_read(&file->port->mutex);
773 kfree(file);
774 kref_put(&dev->ref, ib_umad_release_dev);
776 return 0;
779 static const struct file_operations umad_fops = {
780 .owner = THIS_MODULE,
781 .read = ib_umad_read,
782 .write = ib_umad_write,
783 .poll = ib_umad_poll,
784 .unlocked_ioctl = ib_umad_ioctl,
785 .compat_ioctl = ib_umad_ioctl,
786 .open = ib_umad_open,
787 .release = ib_umad_close
790 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
792 struct ib_umad_port *port;
793 struct ib_port_modify props = {
794 .set_port_cap_mask = IB_PORT_SM
796 int ret;
798 spin_lock(&port_lock);
799 port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
800 if (port)
801 kref_get(&port->umad_dev->ref);
802 spin_unlock(&port_lock);
804 if (!port)
805 return -ENXIO;
807 if (filp->f_flags & O_NONBLOCK) {
808 if (down_trylock(&port->sm_sem)) {
809 ret = -EAGAIN;
810 goto fail;
812 } else {
813 if (down_interruptible(&port->sm_sem)) {
814 ret = -ERESTARTSYS;
815 goto fail;
819 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
820 if (ret) {
821 up(&port->sm_sem);
822 goto fail;
825 filp->private_data = port;
827 return 0;
829 fail:
830 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
831 return ret;
834 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
836 struct ib_umad_port *port = filp->private_data;
837 struct ib_port_modify props = {
838 .clr_port_cap_mask = IB_PORT_SM
840 int ret = 0;
842 down_write(&port->mutex);
843 if (port->ib_dev)
844 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
845 up_write(&port->mutex);
847 up(&port->sm_sem);
849 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
851 return ret;
854 static const struct file_operations umad_sm_fops = {
855 .owner = THIS_MODULE,
856 .open = ib_umad_sm_open,
857 .release = ib_umad_sm_close
860 static struct ib_client umad_client = {
861 .name = "umad",
862 .add = ib_umad_add_one,
863 .remove = ib_umad_remove_one
866 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
868 struct ib_umad_port *port = class_get_devdata(class_dev);
870 if (!port)
871 return -ENODEV;
873 return sprintf(buf, "%s\n", port->ib_dev->name);
875 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
877 static ssize_t show_port(struct class_device *class_dev, char *buf)
879 struct ib_umad_port *port = class_get_devdata(class_dev);
881 if (!port)
882 return -ENODEV;
884 return sprintf(buf, "%d\n", port->port_num);
886 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
888 static ssize_t show_abi_version(struct class *class, char *buf)
890 return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
892 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
894 static int ib_umad_init_port(struct ib_device *device, int port_num,
895 struct ib_umad_port *port)
897 spin_lock(&port_lock);
898 port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
899 if (port->dev_num >= IB_UMAD_MAX_PORTS) {
900 spin_unlock(&port_lock);
901 return -1;
903 set_bit(port->dev_num, dev_map);
904 spin_unlock(&port_lock);
906 port->ib_dev = device;
907 port->port_num = port_num;
908 init_MUTEX(&port->sm_sem);
909 init_rwsem(&port->mutex);
910 INIT_LIST_HEAD(&port->file_list);
912 port->dev = cdev_alloc();
913 if (!port->dev)
914 return -1;
915 port->dev->owner = THIS_MODULE;
916 port->dev->ops = &umad_fops;
917 kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num);
918 if (cdev_add(port->dev, base_dev + port->dev_num, 1))
919 goto err_cdev;
921 port->class_dev = class_device_create(umad_class, NULL, port->dev->dev,
922 device->dma_device,
923 "umad%d", port->dev_num);
924 if (IS_ERR(port->class_dev))
925 goto err_cdev;
927 if (class_device_create_file(port->class_dev, &class_device_attr_ibdev))
928 goto err_class;
929 if (class_device_create_file(port->class_dev, &class_device_attr_port))
930 goto err_class;
932 port->sm_dev = cdev_alloc();
933 if (!port->sm_dev)
934 goto err_class;
935 port->sm_dev->owner = THIS_MODULE;
936 port->sm_dev->ops = &umad_sm_fops;
937 kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num);
938 if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
939 goto err_sm_cdev;
941 port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev,
942 device->dma_device,
943 "issm%d", port->dev_num);
944 if (IS_ERR(port->sm_class_dev))
945 goto err_sm_cdev;
947 class_set_devdata(port->class_dev, port);
948 class_set_devdata(port->sm_class_dev, port);
950 if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev))
951 goto err_sm_class;
952 if (class_device_create_file(port->sm_class_dev, &class_device_attr_port))
953 goto err_sm_class;
955 spin_lock(&port_lock);
956 umad_port[port->dev_num] = port;
957 spin_unlock(&port_lock);
959 return 0;
961 err_sm_class:
962 class_device_destroy(umad_class, port->sm_dev->dev);
964 err_sm_cdev:
965 cdev_del(port->sm_dev);
967 err_class:
968 class_device_destroy(umad_class, port->dev->dev);
970 err_cdev:
971 cdev_del(port->dev);
972 clear_bit(port->dev_num, dev_map);
974 return -1;
977 static void ib_umad_kill_port(struct ib_umad_port *port)
979 struct ib_umad_file *file;
980 int id;
982 class_set_devdata(port->class_dev, NULL);
983 class_set_devdata(port->sm_class_dev, NULL);
985 class_device_destroy(umad_class, port->dev->dev);
986 class_device_destroy(umad_class, port->sm_dev->dev);
988 cdev_del(port->dev);
989 cdev_del(port->sm_dev);
991 spin_lock(&port_lock);
992 umad_port[port->dev_num] = NULL;
993 spin_unlock(&port_lock);
995 down_write(&port->mutex);
997 port->ib_dev = NULL;
1000 * Now go through the list of files attached to this port and
1001 * unregister all of their MAD agents. We need to hold
1002 * port->mutex while doing this to avoid racing with
1003 * ib_umad_close(), but we can't hold the mutex for writing
1004 * while calling ib_unregister_mad_agent(), since that might
1005 * deadlock by calling back into queue_packet(). So we
1006 * downgrade our lock to a read lock, and then drop and
1007 * reacquire the write lock for the next iteration.
1009 * We do list_del_init() on the file's list_head so that the
1010 * list_del in ib_umad_close() is still OK, even after the
1011 * file is removed from the list.
1013 while (!list_empty(&port->file_list)) {
1014 file = list_entry(port->file_list.next, struct ib_umad_file,
1015 port_list);
1017 file->agents_dead = 1;
1018 list_del_init(&file->port_list);
1020 downgrade_write(&port->mutex);
1022 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
1023 if (file->agent[id])
1024 ib_unregister_mad_agent(file->agent[id]);
1026 up_read(&port->mutex);
1027 down_write(&port->mutex);
1030 up_write(&port->mutex);
1032 clear_bit(port->dev_num, dev_map);
1035 static void ib_umad_add_one(struct ib_device *device)
1037 struct ib_umad_device *umad_dev;
1038 int s, e, i;
1040 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1041 return;
1043 if (device->node_type == RDMA_NODE_IB_SWITCH)
1044 s = e = 0;
1045 else {
1046 s = 1;
1047 e = device->phys_port_cnt;
1050 umad_dev = kzalloc(sizeof *umad_dev +
1051 (e - s + 1) * sizeof (struct ib_umad_port),
1052 GFP_KERNEL);
1053 if (!umad_dev)
1054 return;
1056 kref_init(&umad_dev->ref);
1058 umad_dev->start_port = s;
1059 umad_dev->end_port = e;
1061 for (i = s; i <= e; ++i) {
1062 umad_dev->port[i - s].umad_dev = umad_dev;
1064 if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
1065 goto err;
1068 ib_set_client_data(device, &umad_client, umad_dev);
1070 return;
1072 err:
1073 while (--i >= s)
1074 ib_umad_kill_port(&umad_dev->port[i - s]);
1076 kref_put(&umad_dev->ref, ib_umad_release_dev);
1079 static void ib_umad_remove_one(struct ib_device *device)
1081 struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
1082 int i;
1084 if (!umad_dev)
1085 return;
1087 for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
1088 ib_umad_kill_port(&umad_dev->port[i]);
1090 kref_put(&umad_dev->ref, ib_umad_release_dev);
1093 static int __init ib_umad_init(void)
1095 int ret;
1097 ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
1098 "infiniband_mad");
1099 if (ret) {
1100 printk(KERN_ERR "user_mad: couldn't register device number\n");
1101 goto out;
1104 umad_class = class_create(THIS_MODULE, "infiniband_mad");
1105 if (IS_ERR(umad_class)) {
1106 ret = PTR_ERR(umad_class);
1107 printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
1108 goto out_chrdev;
1111 ret = class_create_file(umad_class, &class_attr_abi_version);
1112 if (ret) {
1113 printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
1114 goto out_class;
1117 ret = ib_register_client(&umad_client);
1118 if (ret) {
1119 printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
1120 goto out_class;
1123 return 0;
1125 out_class:
1126 class_destroy(umad_class);
1128 out_chrdev:
1129 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1131 out:
1132 return ret;
1135 static void __exit ib_umad_cleanup(void)
1137 ib_unregister_client(&umad_client);
1138 class_destroy(umad_class);
1139 unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1142 module_init(ib_umad_init);
1143 module_exit(ib_umad_cleanup);