]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/infiniband/core/user_mad.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-2.6-omap-h63xx.git] / drivers / infiniband / core / user_mad.c
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.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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.
33  *
34  * $Id: user_mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
35  */
36
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>
47
48 #include <asm/uaccess.h>
49 #include <asm/semaphore.h>
50
51 #include <rdma/ib_mad.h>
52 #include <rdma/ib_user_mad.h>
53
54 MODULE_AUTHOR("Roland Dreier");
55 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
56 MODULE_LICENSE("Dual BSD/GPL");
57
58 enum {
59         IB_UMAD_MAX_PORTS  = 64,
60         IB_UMAD_MAX_AGENTS = 32,
61
62         IB_UMAD_MAJOR      = 231,
63         IB_UMAD_MINOR_BASE = 0
64 };
65
66 /*
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().
74  *
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().
79  *
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.
86  */
87
88 struct ib_umad_port {
89         struct cdev           *dev;
90         struct class_device   *class_dev;
91
92         struct cdev           *sm_dev;
93         struct class_device   *sm_class_dev;
94         struct semaphore       sm_sem;
95
96         struct rw_semaphore    mutex;
97         struct list_head       file_list;
98
99         struct ib_device      *ib_dev;
100         struct ib_umad_device *umad_dev;
101         int                    dev_num;
102         u8                     port_num;
103 };
104
105 struct ib_umad_device {
106         int                  start_port, end_port;
107         struct kref          ref;
108         struct ib_umad_port  port[0];
109 };
110
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;
121 };
122
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;
129 };
130
131 static struct class *umad_class;
132
133 static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
134
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);
138
139 static void ib_umad_add_one(struct ib_device *device);
140 static void ib_umad_remove_one(struct ib_device *device);
141
142 static void ib_umad_release_dev(struct kref *ref)
143 {
144         struct ib_umad_device *dev =
145                 container_of(ref, struct ib_umad_device, ref);
146
147         kfree(dev);
148 }
149
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)
152 {
153         return file->agents_dead ? NULL : file->agent[id];
154 }
155
156 static int queue_packet(struct ib_umad_file *file,
157                         struct ib_mad_agent *agent,
158                         struct ib_umad_packet *packet)
159 {
160         int ret = 1;
161
162         down_read(&file->port->mutex);
163
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;
174                 }
175
176         up_read(&file->port->mutex);
177
178         return ret;
179 }
180
181 static void dequeue_send(struct ib_umad_file *file,
182                          struct ib_umad_packet *packet)
183  {
184         spin_lock_irq(&file->send_lock);
185         list_del(&packet->list);
186         spin_unlock_irq(&file->send_lock);
187  }
188
189 static void send_handler(struct ib_mad_agent *agent,
190                          struct ib_mad_send_wc *send_wc)
191 {
192         struct ib_umad_file *file = agent->context;
193         struct ib_umad_packet *packet = send_wc->send_buf->context[0];
194
195         dequeue_send(file, packet);
196         ib_destroy_ah(packet->msg->ah);
197         ib_free_send_mad(packet->msg);
198
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;
204         }
205         kfree(packet);
206 }
207
208 static void recv_handler(struct ib_mad_agent *agent,
209                          struct ib_mad_recv_wc *mad_recv_wc)
210 {
211         struct ib_umad_file *file = agent->context;
212         struct ib_umad_packet *packet;
213
214         if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
215                 goto err1;
216
217         packet = kzalloc(sizeof *packet, GFP_KERNEL);
218         if (!packet)
219                 goto err1;
220
221         packet->length = mad_recv_wc->mad_len;
222         packet->recv_wc = mad_recv_wc;
223
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;
234
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);
238
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);
244         }
245
246         if (queue_packet(file, agent, packet))
247                 goto err2;
248         return;
249
250 err2:
251         kfree(packet);
252 err1:
253         ib_free_recv_mad(mad_recv_wc);
254 }
255
256 static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
257                              size_t count)
258 {
259         struct ib_mad_recv_buf *recv_buf;
260         int left, seg_payload, offset, max_seg_payload;
261
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;
269
270         if (copy_to_user(buf, &packet->mad, sizeof (packet->mad)))
271                 return -EFAULT;
272
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;
277
278         if (seg_payload < packet->length) {
279                 /*
280                  * Multipacket RMPP MAD message. Copy remainder of message.
281                  * Note that last segment may have a shorter payload.
282                  */
283                 if (count < sizeof (packet->mad) + packet->length) {
284                         /*
285                          * The buffer is too small, return the first RMPP segment,
286                          * which includes the RMPP message length.
287                          */
288                         return -ENOSPC;
289                 }
290                 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
291                 max_seg_payload = sizeof (struct ib_mad) - offset;
292
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;
301                 }
302         }
303         return sizeof (packet->mad) + packet->length;
304 }
305
306 static ssize_t copy_send_mad(char __user *buf, struct ib_umad_packet *packet,
307                              size_t count)
308 {
309         ssize_t size = sizeof (packet->mad) + packet->length;
310
311         if (count < size)
312                 return -EINVAL;
313
314         if (copy_to_user(buf, &packet->mad, size))
315                 return -EFAULT;
316
317         return size;
318 }
319
320 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
321                             size_t count, loff_t *pos)
322 {
323         struct ib_umad_file *file = filp->private_data;
324         struct ib_umad_packet *packet;
325         ssize_t ret;
326
327         if (count < sizeof (struct ib_user_mad))
328                 return -EINVAL;
329
330         spin_lock_irq(&file->recv_lock);
331
332         while (list_empty(&file->recv_list)) {
333                 spin_unlock_irq(&file->recv_lock);
334
335                 if (filp->f_flags & O_NONBLOCK)
336                         return -EAGAIN;
337
338                 if (wait_event_interruptible(file->recv_wait,
339                                              !list_empty(&file->recv_list)))
340                         return -ERESTARTSYS;
341
342                 spin_lock_irq(&file->recv_lock);
343         }
344
345         packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
346         list_del(&packet->list);
347
348         spin_unlock_irq(&file->recv_lock);
349
350         if (packet->recv_wc)
351                 ret = copy_recv_mad(buf, packet, count);
352         else
353                 ret = copy_send_mad(buf, packet, count);
354
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);
364         }
365         return ret;
366 }
367
368 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
369 {
370         int left, seg;
371
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;
377
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;
384         }
385         return 0;
386 }
387
388 static int same_destination(struct ib_user_mad_hdr *hdr1,
389                             struct ib_user_mad_hdr *hdr2)
390 {
391         if (!hdr1->grh_present && !hdr2->grh_present)
392            return (hdr1->lid == hdr2->lid);
393
394         if (hdr1->grh_present && hdr2->grh_present)
395            return !memcmp(hdr1->gid, hdr2->gid, 16);
396
397         return 0;
398 }
399
400 static int is_duplicate(struct ib_umad_file *file,
401                         struct ib_umad_packet *packet)
402 {
403         struct ib_umad_packet *sent_packet;
404         struct ib_mad_hdr *sent_hdr, *hdr;
405
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;
409
410                 if ((hdr->tid != sent_hdr->tid) ||
411                     (hdr->mgmt_class != sent_hdr->mgmt_class))
412                         continue;
413
414                 /*
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.
418                  */
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;
425
426                 if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
427                         return 1;
428         }
429
430         return 0;
431 }
432
433 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
434                              size_t count, loff_t *pos)
435 {
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;
444
445         if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)
446                 return -EINVAL;
447
448         packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
449         if (!packet)
450                 return -ENOMEM;
451
452         if (copy_from_user(&packet->mad, buf,
453                             sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
454                 ret = -EFAULT;
455                 goto err;
456         }
457
458         if (packet->mad.hdr.id < 0 ||
459             packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
460                 ret = -EINVAL;
461                 goto err;
462         }
463
464         down_read(&file->port->mutex);
465
466         agent = __get_agent(file, packet->mad.hdr.id);
467         if (!agent) {
468                 ret = -EINVAL;
469                 goto err_up;
470         }
471
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;
484         }
485
486         ah = ib_create_ah(agent->qp->pd, &ah_attr);
487         if (IS_ERR(ah)) {
488                 ret = PTR_ERR(ah);
489                 goto err_up;
490         }
491
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;
501         }
502
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;
511         }
512
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;
517
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);
521
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;
528                 }
529         } else {
530                 ret = copy_rmpp_mad(packet->msg, buf);
531                 if (ret)
532                         goto err_msg;
533         }
534
535         /*
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.
539          */
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;
545         }
546
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;
555         }
556
557         ret = ib_post_send_mad(packet->msg, NULL);
558         if (ret)
559                 goto err_send;
560
561         up_read(&file->port->mutex);
562         return count;
563
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;
575 }
576
577 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
578 {
579         struct ib_umad_file *file = filp->private_data;
580
581         /* we will always be able to post a MAD send */
582         unsigned int mask = POLLOUT | POLLWRNORM;
583
584         poll_wait(filp, &file->recv_wait, wait);
585
586         if (!list_empty(&file->recv_list))
587                 mask |= POLLIN | POLLRDNORM;
588
589         return mask;
590 }
591
592 static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
593 {
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;
599
600         down_write(&file->port->mutex);
601
602         if (!file->port->ib_dev) {
603                 ret = -EPIPE;
604                 goto out;
605         }
606
607         if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
608                 ret = -EFAULT;
609                 goto out;
610         }
611
612         if (ureq.qpn != 0 && ureq.qpn != 1) {
613                 ret = -EINVAL;
614                 goto out;
615         }
616
617         for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
618                 if (!__get_agent(file, agent_id))
619                         goto found;
620
621         ret = -ENOMEM;
622         goto out;
623
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);
630         }
631
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;
640         }
641
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;
647         }
648
649         file->agent[agent_id] = agent;
650         ret = 0;
651
652 out:
653         up_write(&file->port->mutex);
654         return ret;
655 }
656
657 static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
658 {
659         struct ib_mad_agent *agent = NULL;
660         u32 id;
661         int ret = 0;
662
663         if (get_user(id, (u32 __user *) arg))
664                 return -EFAULT;
665
666         down_write(&file->port->mutex);
667
668         if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
669                 ret = -EINVAL;
670                 goto out;
671         }
672
673         agent = file->agent[id];
674         file->agent[id] = NULL;
675
676 out:
677         up_write(&file->port->mutex);
678
679         if (agent)
680                 ib_unregister_mad_agent(agent);
681
682         return ret;
683 }
684
685 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
686                           unsigned long arg)
687 {
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;
695         }
696 }
697
698 static int ib_umad_open(struct inode *inode, struct file *filp)
699 {
700         struct ib_umad_port *port;
701         struct ib_umad_file *file;
702         int ret = 0;
703
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);
709
710         if (!port)
711                 return -ENXIO;
712
713         down_write(&port->mutex);
714
715         if (!port->ib_dev) {
716                 ret = -ENXIO;
717                 goto out;
718         }
719
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;
725         }
726
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);
732
733         file->port = port;
734         filp->private_data = file;
735
736         list_add_tail(&file->port_list, &port->file_list);
737
738 out:
739         up_write(&port->mutex);
740         return ret;
741 }
742
743 static int ib_umad_close(struct inode *inode, struct file *filp)
744 {
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;
750
751         down_write(&file->port->mutex);
752
753         already_dead = file->agents_dead;
754         file->agents_dead = 1;
755
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);
760         }
761
762         list_del(&file->port_list);
763
764         downgrade_write(&file->port->mutex);
765
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]);
770
771         up_read(&file->port->mutex);
772
773         kfree(file);
774         kref_put(&dev->ref, ib_umad_release_dev);
775
776         return 0;
777 }
778
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
788 };
789
790 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
791 {
792         struct ib_umad_port *port;
793         struct ib_port_modify props = {
794                 .set_port_cap_mask = IB_PORT_SM
795         };
796         int ret;
797
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);
803
804         if (!port)
805                 return -ENXIO;
806
807         if (filp->f_flags & O_NONBLOCK) {
808                 if (down_trylock(&port->sm_sem)) {
809                         ret = -EAGAIN;
810                         goto fail;
811                 }
812         } else {
813                 if (down_interruptible(&port->sm_sem)) {
814                         ret = -ERESTARTSYS;
815                         goto fail;
816                 }
817         }
818
819         ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
820         if (ret) {
821                 up(&port->sm_sem);
822                 goto fail;
823         }
824
825         filp->private_data = port;
826
827         return 0;
828
829 fail:
830         kref_put(&port->umad_dev->ref, ib_umad_release_dev);
831         return ret;
832 }
833
834 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
835 {
836         struct ib_umad_port *port = filp->private_data;
837         struct ib_port_modify props = {
838                 .clr_port_cap_mask = IB_PORT_SM
839         };
840         int ret = 0;
841
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);
846
847         up(&port->sm_sem);
848
849         kref_put(&port->umad_dev->ref, ib_umad_release_dev);
850
851         return ret;
852 }
853
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
858 };
859
860 static struct ib_client umad_client = {
861         .name   = "umad",
862         .add    = ib_umad_add_one,
863         .remove = ib_umad_remove_one
864 };
865
866 static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
867 {
868         struct ib_umad_port *port = class_get_devdata(class_dev);
869
870         if (!port)
871                 return -ENODEV;
872
873         return sprintf(buf, "%s\n", port->ib_dev->name);
874 }
875 static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
876
877 static ssize_t show_port(struct class_device *class_dev, char *buf)
878 {
879         struct ib_umad_port *port = class_get_devdata(class_dev);
880
881         if (!port)
882                 return -ENODEV;
883
884         return sprintf(buf, "%d\n", port->port_num);
885 }
886 static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
887
888 static ssize_t show_abi_version(struct class *class, char *buf)
889 {
890         return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
891 }
892 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
893
894 static int ib_umad_init_port(struct ib_device *device, int port_num,
895                              struct ib_umad_port *port)
896 {
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;
902         }
903         set_bit(port->dev_num, dev_map);
904         spin_unlock(&port_lock);
905
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);
911
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;
920
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;
926
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;
931
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;
940
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;
946
947         class_set_devdata(port->class_dev,    port);
948         class_set_devdata(port->sm_class_dev, port);
949
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;
954
955         spin_lock(&port_lock);
956         umad_port[port->dev_num] = port;
957         spin_unlock(&port_lock);
958
959         return 0;
960
961 err_sm_class:
962         class_device_destroy(umad_class, port->sm_dev->dev);
963
964 err_sm_cdev:
965         cdev_del(port->sm_dev);
966
967 err_class:
968         class_device_destroy(umad_class, port->dev->dev);
969
970 err_cdev:
971         cdev_del(port->dev);
972         clear_bit(port->dev_num, dev_map);
973
974         return -1;
975 }
976
977 static void ib_umad_kill_port(struct ib_umad_port *port)
978 {
979         struct ib_umad_file *file;
980         int id;
981
982         class_set_devdata(port->class_dev,    NULL);
983         class_set_devdata(port->sm_class_dev, NULL);
984
985         class_device_destroy(umad_class, port->dev->dev);
986         class_device_destroy(umad_class, port->sm_dev->dev);
987
988         cdev_del(port->dev);
989         cdev_del(port->sm_dev);
990
991         spin_lock(&port_lock);
992         umad_port[port->dev_num] = NULL;
993         spin_unlock(&port_lock);
994
995         down_write(&port->mutex);
996
997         port->ib_dev = NULL;
998
999         /*
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.
1008          *
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.
1012          */
1013         while (!list_empty(&port->file_list)) {
1014                 file = list_entry(port->file_list.next, struct ib_umad_file,
1015                                   port_list);
1016
1017                 file->agents_dead = 1;
1018                 list_del_init(&file->port_list);
1019
1020                 downgrade_write(&port->mutex);
1021
1022                 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
1023                         if (file->agent[id])
1024                                 ib_unregister_mad_agent(file->agent[id]);
1025
1026                 up_read(&port->mutex);
1027                 down_write(&port->mutex);
1028         }
1029
1030         up_write(&port->mutex);
1031
1032         clear_bit(port->dev_num, dev_map);
1033 }
1034
1035 static void ib_umad_add_one(struct ib_device *device)
1036 {
1037         struct ib_umad_device *umad_dev;
1038         int s, e, i;
1039
1040         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1041                 return;
1042
1043         if (device->node_type == RDMA_NODE_IB_SWITCH)
1044                 s = e = 0;
1045         else {
1046                 s = 1;
1047                 e = device->phys_port_cnt;
1048         }
1049
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;
1055
1056         kref_init(&umad_dev->ref);
1057
1058         umad_dev->start_port = s;
1059         umad_dev->end_port   = e;
1060
1061         for (i = s; i <= e; ++i) {
1062                 umad_dev->port[i - s].umad_dev = umad_dev;
1063
1064                 if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
1065                         goto err;
1066         }
1067
1068         ib_set_client_data(device, &umad_client, umad_dev);
1069
1070         return;
1071
1072 err:
1073         while (--i >= s)
1074                 ib_umad_kill_port(&umad_dev->port[i - s]);
1075
1076         kref_put(&umad_dev->ref, ib_umad_release_dev);
1077 }
1078
1079 static void ib_umad_remove_one(struct ib_device *device)
1080 {
1081         struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
1082         int i;
1083
1084         if (!umad_dev)
1085                 return;
1086
1087         for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
1088                 ib_umad_kill_port(&umad_dev->port[i]);
1089
1090         kref_put(&umad_dev->ref, ib_umad_release_dev);
1091 }
1092
1093 static int __init ib_umad_init(void)
1094 {
1095         int ret;
1096
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;
1102         }
1103
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;
1109         }
1110
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;
1115         }
1116
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;
1121         }
1122
1123         return 0;
1124
1125 out_class:
1126         class_destroy(umad_class);
1127
1128 out_chrdev:
1129         unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1130
1131 out:
1132         return ret;
1133 }
1134
1135 static void __exit ib_umad_cleanup(void)
1136 {
1137         ib_unregister_client(&umad_client);
1138         class_destroy(umad_class);
1139         unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1140 }
1141
1142 module_init(ib_umad_init);
1143 module_exit(ib_umad_cleanup);