GNU Linux-libre 4.19.264-gnu1
[releases.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
36 #define pr_fmt(fmt) "user_mad: " fmt
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/sched.h>
50 #include <linux/semaphore.h>
51 #include <linux/slab.h>
52 #include <linux/nospec.h>
53
54 #include <linux/uaccess.h>
55
56 #include <rdma/ib_mad.h>
57 #include <rdma/ib_user_mad.h>
58
59 #include "core_priv.h"
60
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
63 MODULE_LICENSE("Dual BSD/GPL");
64
65 enum {
66         IB_UMAD_MAX_PORTS  = RDMA_MAX_PORTS,
67         IB_UMAD_MAX_AGENTS = 32,
68
69         IB_UMAD_MAJOR      = 231,
70         IB_UMAD_MINOR_BASE = 0,
71         IB_UMAD_NUM_FIXED_MINOR = 64,
72         IB_UMAD_NUM_DYNAMIC_MINOR = IB_UMAD_MAX_PORTS - IB_UMAD_NUM_FIXED_MINOR,
73         IB_ISSM_MINOR_BASE        = IB_UMAD_NUM_FIXED_MINOR,
74 };
75
76 /*
77  * Our lifetime rules for these structs are the following:
78  * device special file is opened, we take a reference on the
79  * ib_umad_port's struct ib_umad_device. We drop these
80  * references in the corresponding close().
81  *
82  * In addition to references coming from open character devices, there
83  * is one more reference to each ib_umad_device representing the
84  * module's reference taken when allocating the ib_umad_device in
85  * ib_umad_add_one().
86  *
87  * When destroying an ib_umad_device, we drop the module's reference.
88  */
89
90 struct ib_umad_port {
91         struct cdev           cdev;
92         struct device         *dev;
93
94         struct cdev           sm_cdev;
95         struct device         *sm_dev;
96         struct semaphore       sm_sem;
97
98         struct mutex           file_mutex;
99         struct list_head       file_list;
100
101         struct ib_device      *ib_dev;
102         struct ib_umad_device *umad_dev;
103         int                    dev_num;
104         u8                     port_num;
105 };
106
107 struct ib_umad_device {
108         struct kobject       kobj;
109         struct ib_umad_port  port[0];
110 };
111
112 struct ib_umad_file {
113         struct mutex            mutex;
114         struct ib_umad_port    *port;
115         struct list_head        recv_list;
116         struct list_head        send_list;
117         struct list_head        port_list;
118         spinlock_t              send_lock;
119         wait_queue_head_t       recv_wait;
120         struct ib_mad_agent    *agent[IB_UMAD_MAX_AGENTS];
121         int                     agents_dead;
122         u8                      use_pkey_index;
123         u8                      already_used;
124 };
125
126 struct ib_umad_packet {
127         struct ib_mad_send_buf *msg;
128         struct ib_mad_recv_wc  *recv_wc;
129         struct list_head   list;
130         int                length;
131         struct ib_user_mad mad;
132 };
133
134 static struct class *umad_class;
135
136 static const dev_t base_umad_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
137 static const dev_t base_issm_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE) +
138                                    IB_UMAD_NUM_FIXED_MINOR;
139 static dev_t dynamic_umad_dev;
140 static dev_t dynamic_issm_dev;
141
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, void *client_data);
146
147 static void ib_umad_release_dev(struct kobject *kobj)
148 {
149         struct ib_umad_device *dev =
150                 container_of(kobj, struct ib_umad_device, kobj);
151
152         kfree(dev);
153 }
154
155 static struct kobj_type ib_umad_dev_ktype = {
156         .release = ib_umad_release_dev,
157 };
158
159 static int hdr_size(struct ib_umad_file *file)
160 {
161         return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
162                 sizeof (struct ib_user_mad_hdr_old);
163 }
164
165 /* caller must hold file->mutex */
166 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
167 {
168         return file->agents_dead ? NULL : file->agent[id];
169 }
170
171 static int queue_packet(struct ib_umad_file *file,
172                         struct ib_mad_agent *agent,
173                         struct ib_umad_packet *packet)
174 {
175         int ret = 1;
176
177         mutex_lock(&file->mutex);
178
179         for (packet->mad.hdr.id = 0;
180              packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
181              packet->mad.hdr.id++)
182                 if (agent == __get_agent(file, packet->mad.hdr.id)) {
183                         list_add_tail(&packet->list, &file->recv_list);
184                         wake_up_interruptible(&file->recv_wait);
185                         ret = 0;
186                         break;
187                 }
188
189         mutex_unlock(&file->mutex);
190
191         return ret;
192 }
193
194 static void dequeue_send(struct ib_umad_file *file,
195                          struct ib_umad_packet *packet)
196 {
197         spin_lock_irq(&file->send_lock);
198         list_del(&packet->list);
199         spin_unlock_irq(&file->send_lock);
200 }
201
202 static void send_handler(struct ib_mad_agent *agent,
203                          struct ib_mad_send_wc *send_wc)
204 {
205         struct ib_umad_file *file = agent->context;
206         struct ib_umad_packet *packet = send_wc->send_buf->context[0];
207
208         dequeue_send(file, packet);
209         rdma_destroy_ah(packet->msg->ah);
210         ib_free_send_mad(packet->msg);
211
212         if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
213                 packet->length = IB_MGMT_MAD_HDR;
214                 packet->mad.hdr.status = ETIMEDOUT;
215                 if (!queue_packet(file, agent, packet))
216                         return;
217         }
218         kfree(packet);
219 }
220
221 static void recv_handler(struct ib_mad_agent *agent,
222                          struct ib_mad_send_buf *send_buf,
223                          struct ib_mad_recv_wc *mad_recv_wc)
224 {
225         struct ib_umad_file *file = agent->context;
226         struct ib_umad_packet *packet;
227
228         if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
229                 goto err1;
230
231         packet = kzalloc(sizeof *packet, GFP_KERNEL);
232         if (!packet)
233                 goto err1;
234
235         packet->length = mad_recv_wc->mad_len;
236         packet->recv_wc = mad_recv_wc;
237
238         packet->mad.hdr.status     = 0;
239         packet->mad.hdr.length     = hdr_size(file) + mad_recv_wc->mad_len;
240         packet->mad.hdr.qpn        = cpu_to_be32(mad_recv_wc->wc->src_qp);
241         /*
242          * On OPA devices it is okay to lose the upper 16 bits of LID as this
243          * information is obtained elsewhere. Mask off the upper 16 bits.
244          */
245         if (rdma_cap_opa_mad(agent->device, agent->port_num))
246                 packet->mad.hdr.lid = ib_lid_be16(0xFFFF &
247                                                   mad_recv_wc->wc->slid);
248         else
249                 packet->mad.hdr.lid = ib_lid_be16(mad_recv_wc->wc->slid);
250         packet->mad.hdr.sl         = mad_recv_wc->wc->sl;
251         packet->mad.hdr.path_bits  = mad_recv_wc->wc->dlid_path_bits;
252         packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
253         packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
254         if (packet->mad.hdr.grh_present) {
255                 struct rdma_ah_attr ah_attr;
256                 const struct ib_global_route *grh;
257                 int ret;
258
259                 ret = ib_init_ah_attr_from_wc(agent->device, agent->port_num,
260                                               mad_recv_wc->wc,
261                                               mad_recv_wc->recv_buf.grh,
262                                               &ah_attr);
263                 if (ret)
264                         goto err2;
265
266                 grh = rdma_ah_read_grh(&ah_attr);
267                 packet->mad.hdr.gid_index = grh->sgid_index;
268                 packet->mad.hdr.hop_limit = grh->hop_limit;
269                 packet->mad.hdr.traffic_class = grh->traffic_class;
270                 memcpy(packet->mad.hdr.gid, &grh->dgid, 16);
271                 packet->mad.hdr.flow_label = cpu_to_be32(grh->flow_label);
272                 rdma_destroy_ah_attr(&ah_attr);
273         }
274
275         if (queue_packet(file, agent, packet))
276                 goto err2;
277         return;
278
279 err2:
280         kfree(packet);
281 err1:
282         ib_free_recv_mad(mad_recv_wc);
283 }
284
285 static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
286                              struct ib_umad_packet *packet, size_t count)
287 {
288         struct ib_mad_recv_buf *recv_buf;
289         int left, seg_payload, offset, max_seg_payload;
290         size_t seg_size;
291
292         recv_buf = &packet->recv_wc->recv_buf;
293         seg_size = packet->recv_wc->mad_seg_size;
294
295         /* We need enough room to copy the first (or only) MAD segment. */
296         if ((packet->length <= seg_size &&
297              count < hdr_size(file) + packet->length) ||
298             (packet->length > seg_size &&
299              count < hdr_size(file) + seg_size))
300                 return -EINVAL;
301
302         if (copy_to_user(buf, &packet->mad, hdr_size(file)))
303                 return -EFAULT;
304
305         buf += hdr_size(file);
306         seg_payload = min_t(int, packet->length, seg_size);
307         if (copy_to_user(buf, recv_buf->mad, seg_payload))
308                 return -EFAULT;
309
310         if (seg_payload < packet->length) {
311                 /*
312                  * Multipacket RMPP MAD message. Copy remainder of message.
313                  * Note that last segment may have a shorter payload.
314                  */
315                 if (count < hdr_size(file) + packet->length) {
316                         /*
317                          * The buffer is too small, return the first RMPP segment,
318                          * which includes the RMPP message length.
319                          */
320                         return -ENOSPC;
321                 }
322                 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
323                 max_seg_payload = seg_size - offset;
324
325                 for (left = packet->length - seg_payload, buf += seg_payload;
326                      left; left -= seg_payload, buf += seg_payload) {
327                         recv_buf = container_of(recv_buf->list.next,
328                                                 struct ib_mad_recv_buf, list);
329                         seg_payload = min(left, max_seg_payload);
330                         if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
331                                          seg_payload))
332                                 return -EFAULT;
333                 }
334         }
335         return hdr_size(file) + packet->length;
336 }
337
338 static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
339                              struct ib_umad_packet *packet, size_t count)
340 {
341         ssize_t size = hdr_size(file) + packet->length;
342
343         if (count < size)
344                 return -EINVAL;
345
346         if (copy_to_user(buf, &packet->mad, hdr_size(file)))
347                 return -EFAULT;
348
349         buf += hdr_size(file);
350
351         if (copy_to_user(buf, packet->mad.data, packet->length))
352                 return -EFAULT;
353
354         return size;
355 }
356
357 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
358                             size_t count, loff_t *pos)
359 {
360         struct ib_umad_file *file = filp->private_data;
361         struct ib_umad_packet *packet;
362         ssize_t ret;
363
364         if (count < hdr_size(file))
365                 return -EINVAL;
366
367         mutex_lock(&file->mutex);
368
369         if (file->agents_dead) {
370                 mutex_unlock(&file->mutex);
371                 return -EIO;
372         }
373
374         while (list_empty(&file->recv_list)) {
375                 mutex_unlock(&file->mutex);
376
377                 if (filp->f_flags & O_NONBLOCK)
378                         return -EAGAIN;
379
380                 if (wait_event_interruptible(file->recv_wait,
381                                              !list_empty(&file->recv_list)))
382                         return -ERESTARTSYS;
383
384                 mutex_lock(&file->mutex);
385         }
386
387         if (file->agents_dead) {
388                 mutex_unlock(&file->mutex);
389                 return -EIO;
390         }
391
392         packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
393         list_del(&packet->list);
394
395         mutex_unlock(&file->mutex);
396
397         if (packet->recv_wc)
398                 ret = copy_recv_mad(file, buf, packet, count);
399         else
400                 ret = copy_send_mad(file, buf, packet, count);
401
402         if (ret < 0) {
403                 /* Requeue packet */
404                 mutex_lock(&file->mutex);
405                 list_add(&packet->list, &file->recv_list);
406                 mutex_unlock(&file->mutex);
407         } else {
408                 if (packet->recv_wc)
409                         ib_free_recv_mad(packet->recv_wc);
410                 kfree(packet);
411         }
412         return ret;
413 }
414
415 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
416 {
417         int left, seg;
418
419         /* Copy class specific header */
420         if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
421             copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
422                            msg->hdr_len - IB_MGMT_RMPP_HDR))
423                 return -EFAULT;
424
425         /* All headers are in place.  Copy data segments. */
426         for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
427              seg++, left -= msg->seg_size, buf += msg->seg_size) {
428                 if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
429                                    min(left, msg->seg_size)))
430                         return -EFAULT;
431         }
432         return 0;
433 }
434
435 static int same_destination(struct ib_user_mad_hdr *hdr1,
436                             struct ib_user_mad_hdr *hdr2)
437 {
438         if (!hdr1->grh_present && !hdr2->grh_present)
439            return (hdr1->lid == hdr2->lid);
440
441         if (hdr1->grh_present && hdr2->grh_present)
442            return !memcmp(hdr1->gid, hdr2->gid, 16);
443
444         return 0;
445 }
446
447 static int is_duplicate(struct ib_umad_file *file,
448                         struct ib_umad_packet *packet)
449 {
450         struct ib_umad_packet *sent_packet;
451         struct ib_mad_hdr *sent_hdr, *hdr;
452
453         hdr = (struct ib_mad_hdr *) packet->mad.data;
454         list_for_each_entry(sent_packet, &file->send_list, list) {
455                 sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
456
457                 if ((hdr->tid != sent_hdr->tid) ||
458                     (hdr->mgmt_class != sent_hdr->mgmt_class))
459                         continue;
460
461                 /*
462                  * No need to be overly clever here.  If two new operations have
463                  * the same TID, reject the second as a duplicate.  This is more
464                  * restrictive than required by the spec.
465                  */
466                 if (!ib_response_mad(hdr)) {
467                         if (!ib_response_mad(sent_hdr))
468                                 return 1;
469                         continue;
470                 } else if (!ib_response_mad(sent_hdr))
471                         continue;
472
473                 if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
474                         return 1;
475         }
476
477         return 0;
478 }
479
480 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
481                              size_t count, loff_t *pos)
482 {
483         struct ib_umad_file *file = filp->private_data;
484         struct ib_umad_packet *packet;
485         struct ib_mad_agent *agent;
486         struct rdma_ah_attr ah_attr;
487         struct ib_ah *ah;
488         struct ib_rmpp_mad *rmpp_mad;
489         __be64 *tid;
490         int ret, data_len, hdr_len, copy_offset, rmpp_active;
491         u8 base_version;
492
493         if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
494                 return -EINVAL;
495
496         packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
497         if (!packet)
498                 return -ENOMEM;
499
500         if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
501                 ret = -EFAULT;
502                 goto err;
503         }
504
505         if (packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
506                 ret = -EINVAL;
507                 goto err;
508         }
509
510         buf += hdr_size(file);
511
512         if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
513                 ret = -EFAULT;
514                 goto err;
515         }
516
517         mutex_lock(&file->mutex);
518
519         agent = __get_agent(file, packet->mad.hdr.id);
520         if (!agent) {
521                 ret = -EIO;
522                 goto err_up;
523         }
524
525         memset(&ah_attr, 0, sizeof ah_attr);
526         ah_attr.type = rdma_ah_find_type(agent->device,
527                                          file->port->port_num);
528         rdma_ah_set_dlid(&ah_attr, be16_to_cpu(packet->mad.hdr.lid));
529         rdma_ah_set_sl(&ah_attr, packet->mad.hdr.sl);
530         rdma_ah_set_path_bits(&ah_attr, packet->mad.hdr.path_bits);
531         rdma_ah_set_port_num(&ah_attr, file->port->port_num);
532         if (packet->mad.hdr.grh_present) {
533                 rdma_ah_set_grh(&ah_attr, NULL,
534                                 be32_to_cpu(packet->mad.hdr.flow_label),
535                                 packet->mad.hdr.gid_index,
536                                 packet->mad.hdr.hop_limit,
537                                 packet->mad.hdr.traffic_class);
538                 rdma_ah_set_dgid_raw(&ah_attr, packet->mad.hdr.gid);
539         }
540
541         ah = rdma_create_user_ah(agent->qp->pd, &ah_attr, NULL);
542         if (IS_ERR(ah)) {
543                 ret = PTR_ERR(ah);
544                 goto err_up;
545         }
546
547         rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
548         hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
549
550         if (ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)
551             && ib_mad_kernel_rmpp_agent(agent)) {
552                 copy_offset = IB_MGMT_RMPP_HDR;
553                 rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
554                                                 IB_MGMT_RMPP_FLAG_ACTIVE;
555         } else {
556                 copy_offset = IB_MGMT_MAD_HDR;
557                 rmpp_active = 0;
558         }
559
560         base_version = ((struct ib_mad_hdr *)&packet->mad.data)->base_version;
561         data_len = count - hdr_size(file) - hdr_len;
562         packet->msg = ib_create_send_mad(agent,
563                                          be32_to_cpu(packet->mad.hdr.qpn),
564                                          packet->mad.hdr.pkey_index, rmpp_active,
565                                          hdr_len, data_len, GFP_KERNEL,
566                                          base_version);
567         if (IS_ERR(packet->msg)) {
568                 ret = PTR_ERR(packet->msg);
569                 goto err_ah;
570         }
571
572         packet->msg->ah         = ah;
573         packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
574         packet->msg->retries    = packet->mad.hdr.retries;
575         packet->msg->context[0] = packet;
576
577         /* Copy MAD header.  Any RMPP header is already in place. */
578         memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
579
580         if (!rmpp_active) {
581                 if (copy_from_user(packet->msg->mad + copy_offset,
582                                    buf + copy_offset,
583                                    hdr_len + data_len - copy_offset)) {
584                         ret = -EFAULT;
585                         goto err_msg;
586                 }
587         } else {
588                 ret = copy_rmpp_mad(packet->msg, buf);
589                 if (ret)
590                         goto err_msg;
591         }
592
593         /*
594          * Set the high-order part of the transaction ID to make MADs from
595          * different agents unique, and allow routing responses back to the
596          * original requestor.
597          */
598         if (!ib_response_mad(packet->msg->mad)) {
599                 tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
600                 *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
601                                    (be64_to_cpup(tid) & 0xffffffff));
602                 rmpp_mad->mad_hdr.tid = *tid;
603         }
604
605         if (!ib_mad_kernel_rmpp_agent(agent)
606            && ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)
607            && (ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE)) {
608                 spin_lock_irq(&file->send_lock);
609                 list_add_tail(&packet->list, &file->send_list);
610                 spin_unlock_irq(&file->send_lock);
611         } else {
612                 spin_lock_irq(&file->send_lock);
613                 ret = is_duplicate(file, packet);
614                 if (!ret)
615                         list_add_tail(&packet->list, &file->send_list);
616                 spin_unlock_irq(&file->send_lock);
617                 if (ret) {
618                         ret = -EINVAL;
619                         goto err_msg;
620                 }
621         }
622
623         ret = ib_post_send_mad(packet->msg, NULL);
624         if (ret)
625                 goto err_send;
626
627         mutex_unlock(&file->mutex);
628         return count;
629
630 err_send:
631         dequeue_send(file, packet);
632 err_msg:
633         ib_free_send_mad(packet->msg);
634 err_ah:
635         rdma_destroy_ah(ah);
636 err_up:
637         mutex_unlock(&file->mutex);
638 err:
639         kfree(packet);
640         return ret;
641 }
642
643 static __poll_t ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
644 {
645         struct ib_umad_file *file = filp->private_data;
646
647         /* we will always be able to post a MAD send */
648         __poll_t mask = EPOLLOUT | EPOLLWRNORM;
649
650         mutex_lock(&file->mutex);
651         poll_wait(filp, &file->recv_wait, wait);
652
653         if (!list_empty(&file->recv_list))
654                 mask |= EPOLLIN | EPOLLRDNORM;
655         if (file->agents_dead)
656                 mask = EPOLLERR;
657         mutex_unlock(&file->mutex);
658
659         return mask;
660 }
661
662 static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
663                              int compat_method_mask)
664 {
665         struct ib_user_mad_reg_req ureq;
666         struct ib_mad_reg_req req;
667         struct ib_mad_agent *agent = NULL;
668         int agent_id;
669         int ret;
670
671         mutex_lock(&file->port->file_mutex);
672         mutex_lock(&file->mutex);
673
674         if (!file->port->ib_dev) {
675                 dev_notice(file->port->dev,
676                            "ib_umad_reg_agent: invalid device\n");
677                 ret = -EPIPE;
678                 goto out;
679         }
680
681         if (copy_from_user(&ureq, arg, sizeof ureq)) {
682                 ret = -EFAULT;
683                 goto out;
684         }
685
686         if (ureq.qpn != 0 && ureq.qpn != 1) {
687                 dev_notice(file->port->dev,
688                            "ib_umad_reg_agent: invalid QPN %d specified\n",
689                            ureq.qpn);
690                 ret = -EINVAL;
691                 goto out;
692         }
693
694         for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
695                 if (!__get_agent(file, agent_id))
696                         goto found;
697
698         dev_notice(file->port->dev,
699                    "ib_umad_reg_agent: Max Agents (%u) reached\n",
700                    IB_UMAD_MAX_AGENTS);
701         ret = -ENOMEM;
702         goto out;
703
704 found:
705         if (ureq.mgmt_class) {
706                 memset(&req, 0, sizeof(req));
707                 req.mgmt_class         = ureq.mgmt_class;
708                 req.mgmt_class_version = ureq.mgmt_class_version;
709                 memcpy(req.oui, ureq.oui, sizeof req.oui);
710
711                 if (compat_method_mask) {
712                         u32 *umm = (u32 *) ureq.method_mask;
713                         int i;
714
715                         for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
716                                 req.method_mask[i] =
717                                         umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
718                 } else
719                         memcpy(req.method_mask, ureq.method_mask,
720                                sizeof req.method_mask);
721         }
722
723         agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
724                                       ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
725                                       ureq.mgmt_class ? &req : NULL,
726                                       ureq.rmpp_version,
727                                       send_handler, recv_handler, file, 0);
728         if (IS_ERR(agent)) {
729                 ret = PTR_ERR(agent);
730                 agent = NULL;
731                 goto out;
732         }
733
734         if (put_user(agent_id,
735                      (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
736                 ret = -EFAULT;
737                 goto out;
738         }
739
740         if (!file->already_used) {
741                 file->already_used = 1;
742                 if (!file->use_pkey_index) {
743                         dev_warn(file->port->dev,
744                                 "process %s did not enable P_Key index support.\n",
745                                 current->comm);
746                         dev_warn(file->port->dev,
747                                 "   Documentation/infiniband/user_mad.txt has info on the new ABI.\n");
748                 }
749         }
750
751         file->agent[agent_id] = agent;
752         ret = 0;
753
754 out:
755         mutex_unlock(&file->mutex);
756
757         if (ret && agent)
758                 ib_unregister_mad_agent(agent);
759
760         mutex_unlock(&file->port->file_mutex);
761
762         return ret;
763 }
764
765 static int ib_umad_reg_agent2(struct ib_umad_file *file, void __user *arg)
766 {
767         struct ib_user_mad_reg_req2 ureq;
768         struct ib_mad_reg_req req;
769         struct ib_mad_agent *agent = NULL;
770         int agent_id;
771         int ret;
772
773         mutex_lock(&file->port->file_mutex);
774         mutex_lock(&file->mutex);
775
776         if (!file->port->ib_dev) {
777                 dev_notice(file->port->dev,
778                            "ib_umad_reg_agent2: invalid device\n");
779                 ret = -EPIPE;
780                 goto out;
781         }
782
783         if (copy_from_user(&ureq, arg, sizeof(ureq))) {
784                 ret = -EFAULT;
785                 goto out;
786         }
787
788         if (ureq.qpn != 0 && ureq.qpn != 1) {
789                 dev_notice(file->port->dev,
790                            "ib_umad_reg_agent2: invalid QPN %d specified\n",
791                            ureq.qpn);
792                 ret = -EINVAL;
793                 goto out;
794         }
795
796         if (ureq.flags & ~IB_USER_MAD_REG_FLAGS_CAP) {
797                 dev_notice(file->port->dev,
798                            "ib_umad_reg_agent2 failed: invalid registration flags specified 0x%x; supported 0x%x\n",
799                            ureq.flags, IB_USER_MAD_REG_FLAGS_CAP);
800                 ret = -EINVAL;
801
802                 if (put_user((u32)IB_USER_MAD_REG_FLAGS_CAP,
803                                 (u32 __user *) (arg + offsetof(struct
804                                 ib_user_mad_reg_req2, flags))))
805                         ret = -EFAULT;
806
807                 goto out;
808         }
809
810         for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
811                 if (!__get_agent(file, agent_id))
812                         goto found;
813
814         dev_notice(file->port->dev,
815                    "ib_umad_reg_agent2: Max Agents (%u) reached\n",
816                    IB_UMAD_MAX_AGENTS);
817         ret = -ENOMEM;
818         goto out;
819
820 found:
821         if (ureq.mgmt_class) {
822                 memset(&req, 0, sizeof(req));
823                 req.mgmt_class         = ureq.mgmt_class;
824                 req.mgmt_class_version = ureq.mgmt_class_version;
825                 if (ureq.oui & 0xff000000) {
826                         dev_notice(file->port->dev,
827                                    "ib_umad_reg_agent2 failed: oui invalid 0x%08x\n",
828                                    ureq.oui);
829                         ret = -EINVAL;
830                         goto out;
831                 }
832                 req.oui[2] =  ureq.oui & 0x0000ff;
833                 req.oui[1] = (ureq.oui & 0x00ff00) >> 8;
834                 req.oui[0] = (ureq.oui & 0xff0000) >> 16;
835                 memcpy(req.method_mask, ureq.method_mask,
836                         sizeof(req.method_mask));
837         }
838
839         agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
840                                       ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
841                                       ureq.mgmt_class ? &req : NULL,
842                                       ureq.rmpp_version,
843                                       send_handler, recv_handler, file,
844                                       ureq.flags);
845         if (IS_ERR(agent)) {
846                 ret = PTR_ERR(agent);
847                 agent = NULL;
848                 goto out;
849         }
850
851         if (put_user(agent_id,
852                      (u32 __user *)(arg +
853                                 offsetof(struct ib_user_mad_reg_req2, id)))) {
854                 ret = -EFAULT;
855                 goto out;
856         }
857
858         if (!file->already_used) {
859                 file->already_used = 1;
860                 file->use_pkey_index = 1;
861         }
862
863         file->agent[agent_id] = agent;
864         ret = 0;
865
866 out:
867         mutex_unlock(&file->mutex);
868
869         if (ret && agent)
870                 ib_unregister_mad_agent(agent);
871
872         mutex_unlock(&file->port->file_mutex);
873
874         return ret;
875 }
876
877
878 static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
879 {
880         struct ib_mad_agent *agent = NULL;
881         u32 id;
882         int ret = 0;
883
884         if (get_user(id, arg))
885                 return -EFAULT;
886         if (id >= IB_UMAD_MAX_AGENTS)
887                 return -EINVAL;
888
889         mutex_lock(&file->port->file_mutex);
890         mutex_lock(&file->mutex);
891
892         id = array_index_nospec(id, IB_UMAD_MAX_AGENTS);
893         if (!__get_agent(file, id)) {
894                 ret = -EINVAL;
895                 goto out;
896         }
897
898         agent = file->agent[id];
899         file->agent[id] = NULL;
900
901 out:
902         mutex_unlock(&file->mutex);
903
904         if (agent)
905                 ib_unregister_mad_agent(agent);
906
907         mutex_unlock(&file->port->file_mutex);
908
909         return ret;
910 }
911
912 static long ib_umad_enable_pkey(struct ib_umad_file *file)
913 {
914         int ret = 0;
915
916         mutex_lock(&file->mutex);
917         if (file->already_used)
918                 ret = -EINVAL;
919         else
920                 file->use_pkey_index = 1;
921         mutex_unlock(&file->mutex);
922
923         return ret;
924 }
925
926 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
927                           unsigned long arg)
928 {
929         switch (cmd) {
930         case IB_USER_MAD_REGISTER_AGENT:
931                 return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
932         case IB_USER_MAD_UNREGISTER_AGENT:
933                 return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
934         case IB_USER_MAD_ENABLE_PKEY:
935                 return ib_umad_enable_pkey(filp->private_data);
936         case IB_USER_MAD_REGISTER_AGENT2:
937                 return ib_umad_reg_agent2(filp->private_data, (void __user *) arg);
938         default:
939                 return -ENOIOCTLCMD;
940         }
941 }
942
943 #ifdef CONFIG_COMPAT
944 static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
945                                  unsigned long arg)
946 {
947         switch (cmd) {
948         case IB_USER_MAD_REGISTER_AGENT:
949                 return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
950         case IB_USER_MAD_UNREGISTER_AGENT:
951                 return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
952         case IB_USER_MAD_ENABLE_PKEY:
953                 return ib_umad_enable_pkey(filp->private_data);
954         case IB_USER_MAD_REGISTER_AGENT2:
955                 return ib_umad_reg_agent2(filp->private_data, compat_ptr(arg));
956         default:
957                 return -ENOIOCTLCMD;
958         }
959 }
960 #endif
961
962 /*
963  * ib_umad_open() does not need the BKL:
964  *
965  *  - the ib_umad_port structures are properly reference counted, and
966  *    everything else is purely local to the file being created, so
967  *    races against other open calls are not a problem;
968  *  - the ioctl method does not affect any global state outside of the
969  *    file structure being operated on;
970  */
971 static int ib_umad_open(struct inode *inode, struct file *filp)
972 {
973         struct ib_umad_port *port;
974         struct ib_umad_file *file;
975         int ret = -ENXIO;
976
977         port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
978
979         mutex_lock(&port->file_mutex);
980
981         if (!port->ib_dev)
982                 goto out;
983
984         ret = -ENOMEM;
985         file = kzalloc(sizeof *file, GFP_KERNEL);
986         if (!file)
987                 goto out;
988
989         mutex_init(&file->mutex);
990         spin_lock_init(&file->send_lock);
991         INIT_LIST_HEAD(&file->recv_list);
992         INIT_LIST_HEAD(&file->send_list);
993         init_waitqueue_head(&file->recv_wait);
994
995         file->port = port;
996         filp->private_data = file;
997
998         list_add_tail(&file->port_list, &port->file_list);
999
1000         ret = nonseekable_open(inode, filp);
1001         if (ret) {
1002                 list_del(&file->port_list);
1003                 kfree(file);
1004                 goto out;
1005         }
1006
1007         kobject_get(&port->umad_dev->kobj);
1008
1009 out:
1010         mutex_unlock(&port->file_mutex);
1011         return ret;
1012 }
1013
1014 static int ib_umad_close(struct inode *inode, struct file *filp)
1015 {
1016         struct ib_umad_file *file = filp->private_data;
1017         struct ib_umad_device *dev = file->port->umad_dev;
1018         struct ib_umad_packet *packet, *tmp;
1019         int already_dead;
1020         int i;
1021
1022         mutex_lock(&file->port->file_mutex);
1023         mutex_lock(&file->mutex);
1024
1025         already_dead = file->agents_dead;
1026         file->agents_dead = 1;
1027
1028         list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
1029                 if (packet->recv_wc)
1030                         ib_free_recv_mad(packet->recv_wc);
1031                 kfree(packet);
1032         }
1033
1034         list_del(&file->port_list);
1035
1036         mutex_unlock(&file->mutex);
1037
1038         if (!already_dead)
1039                 for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
1040                         if (file->agent[i])
1041                                 ib_unregister_mad_agent(file->agent[i]);
1042
1043         mutex_unlock(&file->port->file_mutex);
1044
1045         kfree(file);
1046         kobject_put(&dev->kobj);
1047
1048         return 0;
1049 }
1050
1051 static const struct file_operations umad_fops = {
1052         .owner          = THIS_MODULE,
1053         .read           = ib_umad_read,
1054         .write          = ib_umad_write,
1055         .poll           = ib_umad_poll,
1056         .unlocked_ioctl = ib_umad_ioctl,
1057 #ifdef CONFIG_COMPAT
1058         .compat_ioctl   = ib_umad_compat_ioctl,
1059 #endif
1060         .open           = ib_umad_open,
1061         .release        = ib_umad_close,
1062         .llseek         = no_llseek,
1063 };
1064
1065 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
1066 {
1067         struct ib_umad_port *port;
1068         struct ib_port_modify props = {
1069                 .set_port_cap_mask = IB_PORT_SM
1070         };
1071         int ret;
1072
1073         port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
1074
1075         if (filp->f_flags & O_NONBLOCK) {
1076                 if (down_trylock(&port->sm_sem)) {
1077                         ret = -EAGAIN;
1078                         goto fail;
1079                 }
1080         } else {
1081                 if (down_interruptible(&port->sm_sem)) {
1082                         ret = -ERESTARTSYS;
1083                         goto fail;
1084                 }
1085         }
1086
1087         ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
1088         if (ret)
1089                 goto err_up_sem;
1090
1091         filp->private_data = port;
1092
1093         ret = nonseekable_open(inode, filp);
1094         if (ret)
1095                 goto err_clr_sm_cap;
1096
1097         kobject_get(&port->umad_dev->kobj);
1098
1099         return 0;
1100
1101 err_clr_sm_cap:
1102         swap(props.set_port_cap_mask, props.clr_port_cap_mask);
1103         ib_modify_port(port->ib_dev, port->port_num, 0, &props);
1104
1105 err_up_sem:
1106         up(&port->sm_sem);
1107
1108 fail:
1109         return ret;
1110 }
1111
1112 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
1113 {
1114         struct ib_umad_port *port = filp->private_data;
1115         struct ib_port_modify props = {
1116                 .clr_port_cap_mask = IB_PORT_SM
1117         };
1118         int ret = 0;
1119
1120         mutex_lock(&port->file_mutex);
1121         if (port->ib_dev)
1122                 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
1123         mutex_unlock(&port->file_mutex);
1124
1125         up(&port->sm_sem);
1126
1127         kobject_put(&port->umad_dev->kobj);
1128
1129         return ret;
1130 }
1131
1132 static const struct file_operations umad_sm_fops = {
1133         .owner   = THIS_MODULE,
1134         .open    = ib_umad_sm_open,
1135         .release = ib_umad_sm_close,
1136         .llseek  = no_llseek,
1137 };
1138
1139 static struct ib_client umad_client = {
1140         .name   = "umad",
1141         .add    = ib_umad_add_one,
1142         .remove = ib_umad_remove_one
1143 };
1144
1145 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1146                           char *buf)
1147 {
1148         struct ib_umad_port *port = dev_get_drvdata(dev);
1149
1150         if (!port)
1151                 return -ENODEV;
1152
1153         return sprintf(buf, "%s\n", port->ib_dev->name);
1154 }
1155 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1156
1157 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1158                          char *buf)
1159 {
1160         struct ib_umad_port *port = dev_get_drvdata(dev);
1161
1162         if (!port)
1163                 return -ENODEV;
1164
1165         return sprintf(buf, "%d\n", port->port_num);
1166 }
1167 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1168
1169 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1170                          __stringify(IB_USER_MAD_ABI_VERSION));
1171
1172 static int ib_umad_init_port(struct ib_device *device, int port_num,
1173                              struct ib_umad_device *umad_dev,
1174                              struct ib_umad_port *port)
1175 {
1176         int devnum;
1177         dev_t base_umad;
1178         dev_t base_issm;
1179
1180         devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
1181         if (devnum >= IB_UMAD_MAX_PORTS)
1182                 return -1;
1183         port->dev_num = devnum;
1184         set_bit(devnum, dev_map);
1185         if (devnum >= IB_UMAD_NUM_FIXED_MINOR) {
1186                 base_umad = dynamic_umad_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
1187                 base_issm = dynamic_issm_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
1188         } else {
1189                 base_umad = devnum + base_umad_dev;
1190                 base_issm = devnum + base_issm_dev;
1191         }
1192
1193         port->ib_dev   = device;
1194         port->port_num = port_num;
1195         sema_init(&port->sm_sem, 1);
1196         mutex_init(&port->file_mutex);
1197         INIT_LIST_HEAD(&port->file_list);
1198
1199         cdev_init(&port->cdev, &umad_fops);
1200         port->cdev.owner = THIS_MODULE;
1201         cdev_set_parent(&port->cdev, &umad_dev->kobj);
1202         kobject_set_name(&port->cdev.kobj, "umad%d", port->dev_num);
1203         if (cdev_add(&port->cdev, base_umad, 1))
1204                 goto err_cdev;
1205
1206         port->dev = device_create(umad_class, device->dev.parent,
1207                                   port->cdev.dev, port,
1208                                   "umad%d", port->dev_num);
1209         if (IS_ERR(port->dev))
1210                 goto err_cdev;
1211
1212         if (device_create_file(port->dev, &dev_attr_ibdev))
1213                 goto err_dev;
1214         if (device_create_file(port->dev, &dev_attr_port))
1215                 goto err_dev;
1216
1217         cdev_init(&port->sm_cdev, &umad_sm_fops);
1218         port->sm_cdev.owner = THIS_MODULE;
1219         cdev_set_parent(&port->sm_cdev, &umad_dev->kobj);
1220         kobject_set_name(&port->sm_cdev.kobj, "issm%d", port->dev_num);
1221         if (cdev_add(&port->sm_cdev, base_issm, 1))
1222                 goto err_sm_cdev;
1223
1224         port->sm_dev = device_create(umad_class, device->dev.parent,
1225                                      port->sm_cdev.dev, port,
1226                                      "issm%d", port->dev_num);
1227         if (IS_ERR(port->sm_dev))
1228                 goto err_sm_cdev;
1229
1230         if (device_create_file(port->sm_dev, &dev_attr_ibdev))
1231                 goto err_sm_dev;
1232         if (device_create_file(port->sm_dev, &dev_attr_port))
1233                 goto err_sm_dev;
1234
1235         return 0;
1236
1237 err_sm_dev:
1238         device_destroy(umad_class, port->sm_cdev.dev);
1239
1240 err_sm_cdev:
1241         cdev_del(&port->sm_cdev);
1242
1243 err_dev:
1244         device_destroy(umad_class, port->cdev.dev);
1245
1246 err_cdev:
1247         cdev_del(&port->cdev);
1248         clear_bit(devnum, dev_map);
1249
1250         return -1;
1251 }
1252
1253 static void ib_umad_kill_port(struct ib_umad_port *port)
1254 {
1255         struct ib_umad_file *file;
1256         int id;
1257
1258         dev_set_drvdata(port->dev,    NULL);
1259         dev_set_drvdata(port->sm_dev, NULL);
1260
1261         device_destroy(umad_class, port->cdev.dev);
1262         device_destroy(umad_class, port->sm_cdev.dev);
1263
1264         cdev_del(&port->cdev);
1265         cdev_del(&port->sm_cdev);
1266
1267         mutex_lock(&port->file_mutex);
1268
1269         port->ib_dev = NULL;
1270
1271         list_for_each_entry(file, &port->file_list, port_list) {
1272                 mutex_lock(&file->mutex);
1273                 file->agents_dead = 1;
1274                 wake_up_interruptible(&file->recv_wait);
1275                 mutex_unlock(&file->mutex);
1276
1277                 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
1278                         if (file->agent[id])
1279                                 ib_unregister_mad_agent(file->agent[id]);
1280         }
1281
1282         mutex_unlock(&port->file_mutex);
1283         clear_bit(port->dev_num, dev_map);
1284 }
1285
1286 static void ib_umad_add_one(struct ib_device *device)
1287 {
1288         struct ib_umad_device *umad_dev;
1289         int s, e, i;
1290         int count = 0;
1291
1292         s = rdma_start_port(device);
1293         e = rdma_end_port(device);
1294
1295         umad_dev = kzalloc(sizeof *umad_dev +
1296                            (e - s + 1) * sizeof (struct ib_umad_port),
1297                            GFP_KERNEL);
1298         if (!umad_dev)
1299                 return;
1300
1301         kobject_init(&umad_dev->kobj, &ib_umad_dev_ktype);
1302
1303         for (i = s; i <= e; ++i) {
1304                 if (!rdma_cap_ib_mad(device, i))
1305                         continue;
1306
1307                 umad_dev->port[i - s].umad_dev = umad_dev;
1308
1309                 if (ib_umad_init_port(device, i, umad_dev,
1310                                       &umad_dev->port[i - s]))
1311                         goto err;
1312
1313                 count++;
1314         }
1315
1316         if (!count)
1317                 goto free;
1318
1319         ib_set_client_data(device, &umad_client, umad_dev);
1320
1321         return;
1322
1323 err:
1324         while (--i >= s) {
1325                 if (!rdma_cap_ib_mad(device, i))
1326                         continue;
1327
1328                 ib_umad_kill_port(&umad_dev->port[i - s]);
1329         }
1330 free:
1331         kobject_put(&umad_dev->kobj);
1332 }
1333
1334 static void ib_umad_remove_one(struct ib_device *device, void *client_data)
1335 {
1336         struct ib_umad_device *umad_dev = client_data;
1337         int i;
1338
1339         if (!umad_dev)
1340                 return;
1341
1342         for (i = 0; i <= rdma_end_port(device) - rdma_start_port(device); ++i) {
1343                 if (rdma_cap_ib_mad(device, i + rdma_start_port(device)))
1344                         ib_umad_kill_port(&umad_dev->port[i]);
1345         }
1346
1347         kobject_put(&umad_dev->kobj);
1348 }
1349
1350 static char *umad_devnode(struct device *dev, umode_t *mode)
1351 {
1352         return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1353 }
1354
1355 static int __init ib_umad_init(void)
1356 {
1357         int ret;
1358
1359         ret = register_chrdev_region(base_umad_dev,
1360                                      IB_UMAD_NUM_FIXED_MINOR * 2,
1361                                      "infiniband_mad");
1362         if (ret) {
1363                 pr_err("couldn't register device number\n");
1364                 goto out;
1365         }
1366
1367         ret = alloc_chrdev_region(&dynamic_umad_dev, 0,
1368                                   IB_UMAD_NUM_DYNAMIC_MINOR * 2,
1369                                   "infiniband_mad");
1370         if (ret) {
1371                 pr_err("couldn't register dynamic device number\n");
1372                 goto out_alloc;
1373         }
1374         dynamic_issm_dev = dynamic_umad_dev + IB_UMAD_NUM_DYNAMIC_MINOR;
1375
1376         umad_class = class_create(THIS_MODULE, "infiniband_mad");
1377         if (IS_ERR(umad_class)) {
1378                 ret = PTR_ERR(umad_class);
1379                 pr_err("couldn't create class infiniband_mad\n");
1380                 goto out_chrdev;
1381         }
1382
1383         umad_class->devnode = umad_devnode;
1384
1385         ret = class_create_file(umad_class, &class_attr_abi_version.attr);
1386         if (ret) {
1387                 pr_err("couldn't create abi_version attribute\n");
1388                 goto out_class;
1389         }
1390
1391         ret = ib_register_client(&umad_client);
1392         if (ret) {
1393                 pr_err("couldn't register ib_umad client\n");
1394                 goto out_class;
1395         }
1396
1397         return 0;
1398
1399 out_class:
1400         class_destroy(umad_class);
1401
1402 out_chrdev:
1403         unregister_chrdev_region(dynamic_umad_dev,
1404                                  IB_UMAD_NUM_DYNAMIC_MINOR * 2);
1405
1406 out_alloc:
1407         unregister_chrdev_region(base_umad_dev,
1408                                  IB_UMAD_NUM_FIXED_MINOR * 2);
1409
1410 out:
1411         return ret;
1412 }
1413
1414 static void __exit ib_umad_cleanup(void)
1415 {
1416         ib_unregister_client(&umad_client);
1417         class_destroy(umad_class);
1418         unregister_chrdev_region(base_umad_dev,
1419                                  IB_UMAD_NUM_FIXED_MINOR * 2);
1420         unregister_chrdev_region(dynamic_umad_dev,
1421                                  IB_UMAD_NUM_DYNAMIC_MINOR * 2);
1422 }
1423
1424 module_init(ib_umad_init);
1425 module_exit(ib_umad_cleanup);