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