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