]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/infiniband/core/user_mad.c
infiniband: more BKL pushdown
[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  * Copyright (c) 2008 Cisco. All rights reserved.
6  *
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:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
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.
25  *
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
33  * SOFTWARE.
34  *
35  * $Id: user_mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
36  */
37
38 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/device.h>
41 #include <linux/err.h>
42 #include <linux/fs.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>
51
52 #include <asm/uaccess.h>
53
54 #include <rdma/ib_mad.h>
55 #include <rdma/ib_user_mad.h>
56
57 MODULE_AUTHOR("Roland Dreier");
58 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
59 MODULE_LICENSE("Dual BSD/GPL");
60
61 enum {
62         IB_UMAD_MAX_PORTS  = 64,
63         IB_UMAD_MAX_AGENTS = 32,
64
65         IB_UMAD_MAJOR      = 231,
66         IB_UMAD_MINOR_BASE = 0
67 };
68
69 /*
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().
77  *
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
81  * ib_umad_add_one().
82  *
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.
89  */
90
91 struct ib_umad_port {
92         struct cdev           *cdev;
93         struct device         *dev;
94
95         struct cdev           *sm_cdev;
96         struct device         *sm_dev;
97         struct semaphore       sm_sem;
98
99         struct mutex           file_mutex;
100         struct list_head       file_list;
101
102         struct ib_device      *ib_dev;
103         struct ib_umad_device *umad_dev;
104         int                    dev_num;
105         u8                     port_num;
106 };
107
108 struct ib_umad_device {
109         int                  start_port, end_port;
110         struct kref          ref;
111         struct ib_umad_port  port[0];
112 };
113
114 struct ib_umad_file {
115         struct mutex            mutex;
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];
123         int                     agents_dead;
124         u8                      use_pkey_index;
125         u8                      already_used;
126 };
127
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;
132         int                length;
133         struct ib_user_mad mad;
134 };
135
136 static struct class *umad_class;
137
138 static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
139
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);
143
144 static void ib_umad_add_one(struct ib_device *device);
145 static void ib_umad_remove_one(struct ib_device *device);
146
147 static void ib_umad_release_dev(struct kref *ref)
148 {
149         struct ib_umad_device *dev =
150                 container_of(ref, struct ib_umad_device, ref);
151
152         kfree(dev);
153 }
154
155 static int hdr_size(struct ib_umad_file *file)
156 {
157         return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
158                 sizeof (struct ib_user_mad_hdr_old);
159 }
160
161 /* caller must hold file->mutex */
162 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
163 {
164         return file->agents_dead ? NULL : file->agent[id];
165 }
166
167 static int queue_packet(struct ib_umad_file *file,
168                         struct ib_mad_agent *agent,
169                         struct ib_umad_packet *packet)
170 {
171         int ret = 1;
172
173         mutex_lock(&file->mutex);
174
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);
181                         ret = 0;
182                         break;
183                 }
184
185         mutex_unlock(&file->mutex);
186
187         return ret;
188 }
189
190 static void dequeue_send(struct ib_umad_file *file,
191                          struct ib_umad_packet *packet)
192 {
193         spin_lock_irq(&file->send_lock);
194         list_del(&packet->list);
195         spin_unlock_irq(&file->send_lock);
196 }
197
198 static void send_handler(struct ib_mad_agent *agent,
199                          struct ib_mad_send_wc *send_wc)
200 {
201         struct ib_umad_file *file = agent->context;
202         struct ib_umad_packet *packet = send_wc->send_buf->context[0];
203
204         dequeue_send(file, packet);
205         ib_destroy_ah(packet->msg->ah);
206         ib_free_send_mad(packet->msg);
207
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))
212                         return;
213         }
214         kfree(packet);
215 }
216
217 static void recv_handler(struct ib_mad_agent *agent,
218                          struct ib_mad_recv_wc *mad_recv_wc)
219 {
220         struct ib_umad_file *file = agent->context;
221         struct ib_umad_packet *packet;
222
223         if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
224                 goto err1;
225
226         packet = kzalloc(sizeof *packet, GFP_KERNEL);
227         if (!packet)
228                 goto err1;
229
230         packet->length = mad_recv_wc->mad_len;
231         packet->recv_wc = mad_recv_wc;
232
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;
243
244                 ib_init_ah_from_wc(agent->device, agent->port_num,
245                                    mad_recv_wc->wc, mad_recv_wc->recv_buf.grh,
246                                    &ah_attr);
247
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);
253         }
254
255         if (queue_packet(file, agent, packet))
256                 goto err2;
257         return;
258
259 err2:
260         kfree(packet);
261 err1:
262         ib_free_recv_mad(mad_recv_wc);
263 }
264
265 static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
266                              struct ib_umad_packet *packet, size_t count)
267 {
268         struct ib_mad_recv_buf *recv_buf;
269         int left, seg_payload, offset, max_seg_payload;
270
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)))
277                 return -EINVAL;
278
279         if (copy_to_user(buf, &packet->mad, hdr_size(file)))
280                 return -EFAULT;
281
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))
285                 return -EFAULT;
286
287         if (seg_payload < packet->length) {
288                 /*
289                  * Multipacket RMPP MAD message. Copy remainder of message.
290                  * Note that last segment may have a shorter payload.
291                  */
292                 if (count < hdr_size(file) + packet->length) {
293                         /*
294                          * The buffer is too small, return the first RMPP segment,
295                          * which includes the RMPP message length.
296                          */
297                         return -ENOSPC;
298                 }
299                 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
300                 max_seg_payload = sizeof (struct ib_mad) - offset;
301
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,
308                                          seg_payload))
309                                 return -EFAULT;
310                 }
311         }
312         return hdr_size(file) + packet->length;
313 }
314
315 static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
316                              struct ib_umad_packet *packet, size_t count)
317 {
318         ssize_t size = hdr_size(file) + packet->length;
319
320         if (count < size)
321                 return -EINVAL;
322
323         if (copy_to_user(buf, &packet->mad, hdr_size(file)))
324                 return -EFAULT;
325
326         buf += hdr_size(file);
327
328         if (copy_to_user(buf, packet->mad.data, packet->length))
329                 return -EFAULT;
330
331         return size;
332 }
333
334 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
335                             size_t count, loff_t *pos)
336 {
337         struct ib_umad_file *file = filp->private_data;
338         struct ib_umad_packet *packet;
339         ssize_t ret;
340
341         if (count < hdr_size(file))
342                 return -EINVAL;
343
344         mutex_lock(&file->mutex);
345
346         while (list_empty(&file->recv_list)) {
347                 mutex_unlock(&file->mutex);
348
349                 if (filp->f_flags & O_NONBLOCK)
350                         return -EAGAIN;
351
352                 if (wait_event_interruptible(file->recv_wait,
353                                              !list_empty(&file->recv_list)))
354                         return -ERESTARTSYS;
355
356                 mutex_lock(&file->mutex);
357         }
358
359         packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
360         list_del(&packet->list);
361
362         mutex_unlock(&file->mutex);
363
364         if (packet->recv_wc)
365                 ret = copy_recv_mad(file, buf, packet, count);
366         else
367                 ret = copy_send_mad(file, buf, packet, count);
368
369         if (ret < 0) {
370                 /* Requeue packet */
371                 mutex_lock(&file->mutex);
372                 list_add(&packet->list, &file->recv_list);
373                 mutex_unlock(&file->mutex);
374         } else {
375                 if (packet->recv_wc)
376                         ib_free_recv_mad(packet->recv_wc);
377                 kfree(packet);
378         }
379         return ret;
380 }
381
382 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
383 {
384         int left, seg;
385
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))
390                 return -EFAULT;
391
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)))
397                         return -EFAULT;
398         }
399         return 0;
400 }
401
402 static int same_destination(struct ib_user_mad_hdr *hdr1,
403                             struct ib_user_mad_hdr *hdr2)
404 {
405         if (!hdr1->grh_present && !hdr2->grh_present)
406            return (hdr1->lid == hdr2->lid);
407
408         if (hdr1->grh_present && hdr2->grh_present)
409            return !memcmp(hdr1->gid, hdr2->gid, 16);
410
411         return 0;
412 }
413
414 static int is_duplicate(struct ib_umad_file *file,
415                         struct ib_umad_packet *packet)
416 {
417         struct ib_umad_packet *sent_packet;
418         struct ib_mad_hdr *sent_hdr, *hdr;
419
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;
423
424                 if ((hdr->tid != sent_hdr->tid) ||
425                     (hdr->mgmt_class != sent_hdr->mgmt_class))
426                         continue;
427
428                 /*
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.
432                  */
433                 if (!ib_response_mad((struct ib_mad *) hdr)) {
434                         if (!ib_response_mad((struct ib_mad *) sent_hdr))
435                                 return 1;
436                         continue;
437                 } else if (!ib_response_mad((struct ib_mad *) sent_hdr))
438                         continue;
439
440                 if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
441                         return 1;
442         }
443
444         return 0;
445 }
446
447 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
448                              size_t count, loff_t *pos)
449 {
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;
454         struct ib_ah *ah;
455         struct ib_rmpp_mad *rmpp_mad;
456         __be64 *tid;
457         int ret, data_len, hdr_len, copy_offset, rmpp_active;
458
459         if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
460                 return -EINVAL;
461
462         packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
463         if (!packet)
464                 return -ENOMEM;
465
466         if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
467                 ret = -EFAULT;
468                 goto err;
469         }
470
471         if (packet->mad.hdr.id < 0 ||
472             packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
473                 ret = -EINVAL;
474                 goto err;
475         }
476
477         buf += hdr_size(file);
478
479         if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
480                 ret = -EFAULT;
481                 goto err;
482         }
483
484         mutex_lock(&file->mutex);
485
486         agent = __get_agent(file, packet->mad.hdr.id);
487         if (!agent) {
488                 ret = -EINVAL;
489                 goto err_up;
490         }
491
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;
504         }
505
506         ah = ib_create_ah(agent->qp->pd, &ah_attr);
507         if (IS_ERR(ah)) {
508                 ret = PTR_ERR(ah);
509                 goto err_up;
510         }
511
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;
516                 rmpp_active = 0;
517         } else {
518                 copy_offset = IB_MGMT_RMPP_HDR;
519                 rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
520                               IB_MGMT_RMPP_FLAG_ACTIVE;
521         }
522
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);
530                 goto err_ah;
531         }
532
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;
537
538         /* Copy MAD header.  Any RMPP header is already in place. */
539         memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
540
541         if (!rmpp_active) {
542                 if (copy_from_user(packet->msg->mad + copy_offset,
543                                    buf + copy_offset,
544                                    hdr_len + data_len - copy_offset)) {
545                         ret = -EFAULT;
546                         goto err_msg;
547                 }
548         } else {
549                 ret = copy_rmpp_mad(packet->msg, buf);
550                 if (ret)
551                         goto err_msg;
552         }
553
554         /*
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.
558          */
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;
564         }
565
566         spin_lock_irq(&file->send_lock);
567         ret = is_duplicate(file, packet);
568         if (!ret)
569                 list_add_tail(&packet->list, &file->send_list);
570         spin_unlock_irq(&file->send_lock);
571         if (ret) {
572                 ret = -EINVAL;
573                 goto err_msg;
574         }
575
576         ret = ib_post_send_mad(packet->msg, NULL);
577         if (ret)
578                 goto err_send;
579
580         mutex_unlock(&file->mutex);
581         return count;
582
583 err_send:
584         dequeue_send(file, packet);
585 err_msg:
586         ib_free_send_mad(packet->msg);
587 err_ah:
588         ib_destroy_ah(ah);
589 err_up:
590         mutex_unlock(&file->mutex);
591 err:
592         kfree(packet);
593         return ret;
594 }
595
596 static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
597 {
598         struct ib_umad_file *file = filp->private_data;
599
600         /* we will always be able to post a MAD send */
601         unsigned int mask = POLLOUT | POLLWRNORM;
602
603         poll_wait(filp, &file->recv_wait, wait);
604
605         if (!list_empty(&file->recv_list))
606                 mask |= POLLIN | POLLRDNORM;
607
608         return mask;
609 }
610
611 static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
612                              int compat_method_mask)
613 {
614         struct ib_user_mad_reg_req ureq;
615         struct ib_mad_reg_req req;
616         struct ib_mad_agent *agent = NULL;
617         int agent_id;
618         int ret;
619
620         mutex_lock(&file->port->file_mutex);
621         mutex_lock(&file->mutex);
622
623         if (!file->port->ib_dev) {
624                 ret = -EPIPE;
625                 goto out;
626         }
627
628         if (copy_from_user(&ureq, arg, sizeof ureq)) {
629                 ret = -EFAULT;
630                 goto out;
631         }
632
633         if (ureq.qpn != 0 && ureq.qpn != 1) {
634                 ret = -EINVAL;
635                 goto out;
636         }
637
638         for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
639                 if (!__get_agent(file, agent_id))
640                         goto found;
641
642         ret = -ENOMEM;
643         goto out;
644
645 found:
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);
650
651                 if (compat_method_mask) {
652                         u32 *umm = (u32 *) ureq.method_mask;
653                         int i;
654
655                         for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
656                                 req.method_mask[i] =
657                                         umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
658                 } else
659                         memcpy(req.method_mask, ureq.method_mask,
660                                sizeof req.method_mask);
661         }
662
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,
666                                       ureq.rmpp_version,
667                                       send_handler, recv_handler, file);
668         if (IS_ERR(agent)) {
669                 ret = PTR_ERR(agent);
670                 agent = NULL;
671                 goto out;
672         }
673
674         if (put_user(agent_id,
675                      (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
676                 ret = -EFAULT;
677                 goto out;
678         }
679
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");
687                 }
688         }
689
690         file->agent[agent_id] = agent;
691         ret = 0;
692
693 out:
694         mutex_unlock(&file->mutex);
695
696         if (ret && agent)
697                 ib_unregister_mad_agent(agent);
698
699         mutex_unlock(&file->port->file_mutex);
700
701         return ret;
702 }
703
704 static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
705 {
706         struct ib_mad_agent *agent = NULL;
707         u32 id;
708         int ret = 0;
709
710         if (get_user(id, arg))
711                 return -EFAULT;
712
713         mutex_lock(&file->port->file_mutex);
714         mutex_lock(&file->mutex);
715
716         if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
717                 ret = -EINVAL;
718                 goto out;
719         }
720
721         agent = file->agent[id];
722         file->agent[id] = NULL;
723
724 out:
725         mutex_unlock(&file->mutex);
726
727         if (agent)
728                 ib_unregister_mad_agent(agent);
729
730         mutex_unlock(&file->port->file_mutex);
731
732         return ret;
733 }
734
735 static long ib_umad_enable_pkey(struct ib_umad_file *file)
736 {
737         int ret = 0;
738
739         mutex_lock(&file->mutex);
740         if (file->already_used)
741                 ret = -EINVAL;
742         else
743                 file->use_pkey_index = 1;
744         mutex_unlock(&file->mutex);
745
746         return ret;
747 }
748
749 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
750                           unsigned long arg)
751 {
752         switch (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);
759         default:
760                 return -ENOIOCTLCMD;
761         }
762 }
763
764 #ifdef CONFIG_COMPAT
765 static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
766                                  unsigned long arg)
767 {
768         switch (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);
775         default:
776                 return -ENOIOCTLCMD;
777         }
778 }
779 #endif
780
781 static int ib_umad_open(struct inode *inode, struct file *filp)
782 {
783         struct ib_umad_port *port;
784         struct ib_umad_file *file;
785         int ret = 0;
786
787         lock_kernel();
788         spin_lock(&port_lock);
789         port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
790         if (port)
791                 kref_get(&port->umad_dev->ref);
792         spin_unlock(&port_lock);
793
794         if (!port) {
795                 unlock_kernel();
796                 return -ENXIO;
797         }
798
799         mutex_lock(&port->file_mutex);
800
801         if (!port->ib_dev) {
802                 ret = -ENXIO;
803                 goto out;
804         }
805
806         file = kzalloc(sizeof *file, GFP_KERNEL);
807         if (!file) {
808                 kref_put(&port->umad_dev->ref, ib_umad_release_dev);
809                 ret = -ENOMEM;
810                 goto out;
811         }
812
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);
818
819         file->port = port;
820         filp->private_data = file;
821
822         list_add_tail(&file->port_list, &port->file_list);
823
824 out:
825         mutex_unlock(&port->file_mutex);
826         unlock_kernel();
827         return ret;
828 }
829
830 static int ib_umad_close(struct inode *inode, struct file *filp)
831 {
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;
835         int already_dead;
836         int i;
837
838         mutex_lock(&file->port->file_mutex);
839         mutex_lock(&file->mutex);
840
841         already_dead = file->agents_dead;
842         file->agents_dead = 1;
843
844         list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
845                 if (packet->recv_wc)
846                         ib_free_recv_mad(packet->recv_wc);
847                 kfree(packet);
848         }
849
850         list_del(&file->port_list);
851
852         mutex_unlock(&file->mutex);
853
854         if (!already_dead)
855                 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
856                         if (file->agent[i])
857                                 ib_unregister_mad_agent(file->agent[i]);
858
859         mutex_unlock(&file->port->file_mutex);
860
861         kfree(file);
862         kref_put(&dev->ref, ib_umad_release_dev);
863
864         return 0;
865 }
866
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,
873 #ifdef CONFIG_COMPAT
874         .compat_ioctl   = ib_umad_compat_ioctl,
875 #endif
876         .open           = ib_umad_open,
877         .release        = ib_umad_close
878 };
879
880 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
881 {
882         struct ib_umad_port *port;
883         struct ib_port_modify props = {
884                 .set_port_cap_mask = IB_PORT_SM
885         };
886         int ret;
887
888         spin_lock(&port_lock);
889         port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
890         if (port)
891                 kref_get(&port->umad_dev->ref);
892         spin_unlock(&port_lock);
893
894         if (!port)
895                 return -ENXIO;
896
897         if (filp->f_flags & O_NONBLOCK) {
898                 if (down_trylock(&port->sm_sem)) {
899                         ret = -EAGAIN;
900                         goto fail;
901                 }
902         } else {
903                 if (down_interruptible(&port->sm_sem)) {
904                         ret = -ERESTARTSYS;
905                         goto fail;
906                 }
907         }
908
909         ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
910         if (ret) {
911                 up(&port->sm_sem);
912                 goto fail;
913         }
914
915         filp->private_data = port;
916
917         return 0;
918
919 fail:
920         kref_put(&port->umad_dev->ref, ib_umad_release_dev);
921         return ret;
922 }
923
924 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
925 {
926         struct ib_umad_port *port = filp->private_data;
927         struct ib_port_modify props = {
928                 .clr_port_cap_mask = IB_PORT_SM
929         };
930         int ret = 0;
931
932         mutex_lock(&port->file_mutex);
933         if (port->ib_dev)
934                 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
935         mutex_unlock(&port->file_mutex);
936
937         up(&port->sm_sem);
938
939         kref_put(&port->umad_dev->ref, ib_umad_release_dev);
940
941         return ret;
942 }
943
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
948 };
949
950 static struct ib_client umad_client = {
951         .name   = "umad",
952         .add    = ib_umad_add_one,
953         .remove = ib_umad_remove_one
954 };
955
956 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
957                           char *buf)
958 {
959         struct ib_umad_port *port = dev_get_drvdata(dev);
960
961         if (!port)
962                 return -ENODEV;
963
964         return sprintf(buf, "%s\n", port->ib_dev->name);
965 }
966 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
967
968 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
969                          char *buf)
970 {
971         struct ib_umad_port *port = dev_get_drvdata(dev);
972
973         if (!port)
974                 return -ENODEV;
975
976         return sprintf(buf, "%d\n", port->port_num);
977 }
978 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
979
980 static ssize_t show_abi_version(struct class *class, char *buf)
981 {
982         return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
983 }
984 static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
985
986 static int ib_umad_init_port(struct ib_device *device, int port_num,
987                              struct ib_umad_port *port)
988 {
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);
993                 return -1;
994         }
995         set_bit(port->dev_num, dev_map);
996         spin_unlock(&port_lock);
997
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);
1003
1004         port->cdev = cdev_alloc();
1005         if (!port->cdev)
1006                 return -1;
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))
1011                 goto err_cdev;
1012
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))
1016                 goto err_cdev;
1017
1018         if (device_create_file(port->dev, &dev_attr_ibdev))
1019                 goto err_dev;
1020         if (device_create_file(port->dev, &dev_attr_port))
1021                 goto err_dev;
1022
1023         port->sm_cdev = cdev_alloc();
1024         if (!port->sm_cdev)
1025                 goto err_dev;
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))
1030                 goto err_sm_cdev;
1031
1032         port->sm_dev = device_create(umad_class, device->dma_device,
1033                                      port->sm_cdev->dev,
1034                                      "issm%d", port->dev_num);
1035         if (IS_ERR(port->sm_dev))
1036                 goto err_sm_cdev;
1037
1038         dev_set_drvdata(port->dev,    port);
1039         dev_set_drvdata(port->sm_dev, port);
1040
1041         if (device_create_file(port->sm_dev, &dev_attr_ibdev))
1042                 goto err_sm_dev;
1043         if (device_create_file(port->sm_dev, &dev_attr_port))
1044                 goto err_sm_dev;
1045
1046         spin_lock(&port_lock);
1047         umad_port[port->dev_num] = port;
1048         spin_unlock(&port_lock);
1049
1050         return 0;
1051
1052 err_sm_dev:
1053         device_destroy(umad_class, port->sm_cdev->dev);
1054
1055 err_sm_cdev:
1056         cdev_del(port->sm_cdev);
1057
1058 err_dev:
1059         device_destroy(umad_class, port->cdev->dev);
1060
1061 err_cdev:
1062         cdev_del(port->cdev);
1063         clear_bit(port->dev_num, dev_map);
1064
1065         return -1;
1066 }
1067
1068 static void ib_umad_kill_port(struct ib_umad_port *port)
1069 {
1070         struct ib_umad_file *file;
1071         int already_dead;
1072         int id;
1073
1074         dev_set_drvdata(port->dev,    NULL);
1075         dev_set_drvdata(port->sm_dev, NULL);
1076
1077         device_destroy(umad_class, port->cdev->dev);
1078         device_destroy(umad_class, port->sm_cdev->dev);
1079
1080         cdev_del(port->cdev);
1081         cdev_del(port->sm_cdev);
1082
1083         spin_lock(&port_lock);
1084         umad_port[port->dev_num] = NULL;
1085         spin_unlock(&port_lock);
1086
1087         mutex_lock(&port->file_mutex);
1088
1089         port->ib_dev = NULL;
1090
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);
1096
1097                 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
1098                         if (file->agent[id])
1099                                 ib_unregister_mad_agent(file->agent[id]);
1100         }
1101
1102         mutex_unlock(&port->file_mutex);
1103
1104         clear_bit(port->dev_num, dev_map);
1105 }
1106
1107 static void ib_umad_add_one(struct ib_device *device)
1108 {
1109         struct ib_umad_device *umad_dev;
1110         int s, e, i;
1111
1112         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1113                 return;
1114
1115         if (device->node_type == RDMA_NODE_IB_SWITCH)
1116                 s = e = 0;
1117         else {
1118                 s = 1;
1119                 e = device->phys_port_cnt;
1120         }
1121
1122         umad_dev = kzalloc(sizeof *umad_dev +
1123                            (e - s + 1) * sizeof (struct ib_umad_port),
1124                            GFP_KERNEL);
1125         if (!umad_dev)
1126                 return;
1127
1128         kref_init(&umad_dev->ref);
1129
1130         umad_dev->start_port = s;
1131         umad_dev->end_port   = e;
1132
1133         for (i = s; i <= e; ++i) {
1134                 umad_dev->port[i - s].umad_dev = umad_dev;
1135
1136                 if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
1137                         goto err;
1138         }
1139
1140         ib_set_client_data(device, &umad_client, umad_dev);
1141
1142         return;
1143
1144 err:
1145         while (--i >= s)
1146                 ib_umad_kill_port(&umad_dev->port[i - s]);
1147
1148         kref_put(&umad_dev->ref, ib_umad_release_dev);
1149 }
1150
1151 static void ib_umad_remove_one(struct ib_device *device)
1152 {
1153         struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
1154         int i;
1155
1156         if (!umad_dev)
1157                 return;
1158
1159         for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
1160                 ib_umad_kill_port(&umad_dev->port[i]);
1161
1162         kref_put(&umad_dev->ref, ib_umad_release_dev);
1163 }
1164
1165 static int __init ib_umad_init(void)
1166 {
1167         int ret;
1168
1169         ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
1170                                      "infiniband_mad");
1171         if (ret) {
1172                 printk(KERN_ERR "user_mad: couldn't register device number\n");
1173                 goto out;
1174         }
1175
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");
1180                 goto out_chrdev;
1181         }
1182
1183         ret = class_create_file(umad_class, &class_attr_abi_version);
1184         if (ret) {
1185                 printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
1186                 goto out_class;
1187         }
1188
1189         ret = ib_register_client(&umad_client);
1190         if (ret) {
1191                 printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
1192                 goto out_class;
1193         }
1194
1195         return 0;
1196
1197 out_class:
1198         class_destroy(umad_class);
1199
1200 out_chrdev:
1201         unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1202
1203 out:
1204         return ret;
1205 }
1206
1207 static void __exit ib_umad_cleanup(void)
1208 {
1209         ib_unregister_client(&umad_client);
1210         class_destroy(umad_class);
1211         unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
1212 }
1213
1214 module_init(ib_umad_init);
1215 module_exit(ib_umad_cleanup);