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