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