Linux 4.19.133
[linux/fpc-iii.git] / drivers / infiniband / core / user_mad.c
bloba18f3f8ad77fe91944b54157a7003b84565d8f7d
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.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
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.
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.
36 #define pr_fmt(fmt) "user_mad: " fmt
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>
54 #include <linux/uaccess.h>
56 #include <rdma/ib_mad.h>
57 #include <rdma/ib_user_mad.h>
59 #include "core_priv.h"
61 MODULE_AUTHOR("Roland Dreier");
62 MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
63 MODULE_LICENSE("Dual BSD/GPL");
65 enum {
66 IB_UMAD_MAX_PORTS = RDMA_MAX_PORTS,
67 IB_UMAD_MAX_AGENTS = 32,
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,
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().
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().
87 * When destroying an ib_umad_device, we drop the module's reference.
90 struct ib_umad_port {
91 struct cdev cdev;
92 struct device *dev;
94 struct cdev sm_cdev;
95 struct device *sm_dev;
96 struct semaphore sm_sem;
98 struct mutex file_mutex;
99 struct list_head file_list;
101 struct ib_device *ib_dev;
102 struct ib_umad_device *umad_dev;
103 int dev_num;
104 u8 port_num;
107 struct ib_umad_device {
108 struct kobject kobj;
109 struct ib_umad_port port[0];
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;
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;
134 static struct class *umad_class;
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;
142 static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS);
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);
147 static void ib_umad_release_dev(struct kobject *kobj)
149 struct ib_umad_device *dev =
150 container_of(kobj, struct ib_umad_device, kobj);
152 kfree(dev);
155 static struct kobj_type ib_umad_dev_ktype = {
156 .release = ib_umad_release_dev,
159 static int hdr_size(struct ib_umad_file *file)
161 return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
162 sizeof (struct ib_user_mad_hdr_old);
165 /* caller must hold file->mutex */
166 static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
168 return file->agents_dead ? NULL : file->agent[id];
171 static int queue_packet(struct ib_umad_file *file,
172 struct ib_mad_agent *agent,
173 struct ib_umad_packet *packet)
175 int ret = 1;
177 mutex_lock(&file->mutex);
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;
189 mutex_unlock(&file->mutex);
191 return ret;
194 static void dequeue_send(struct ib_umad_file *file,
195 struct ib_umad_packet *packet)
197 spin_lock_irq(&file->send_lock);
198 list_del(&packet->list);
199 spin_unlock_irq(&file->send_lock);
202 static void send_handler(struct ib_mad_agent *agent,
203 struct ib_mad_send_wc *send_wc)
205 struct ib_umad_file *file = agent->context;
206 struct ib_umad_packet *packet = send_wc->send_buf->context[0];
208 dequeue_send(file, packet);
209 rdma_destroy_ah(packet->msg->ah);
210 ib_free_send_mad(packet->msg);
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;
218 kfree(packet);
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)
225 struct ib_umad_file *file = agent->context;
226 struct ib_umad_packet *packet;
228 if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
229 goto err1;
231 packet = kzalloc(sizeof *packet, GFP_KERNEL);
232 if (!packet)
233 goto err1;
235 packet->length = mad_recv_wc->mad_len;
236 packet->recv_wc = mad_recv_wc;
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);
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.
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;
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;
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);
275 if (queue_packet(file, agent, packet))
276 goto err2;
277 return;
279 err2:
280 kfree(packet);
281 err1:
282 ib_free_recv_mad(mad_recv_wc);
285 static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
286 struct ib_umad_packet *packet, size_t count)
288 struct ib_mad_recv_buf *recv_buf;
289 int left, seg_payload, offset, max_seg_payload;
290 size_t seg_size;
292 recv_buf = &packet->recv_wc->recv_buf;
293 seg_size = packet->recv_wc->mad_seg_size;
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;
302 if (copy_to_user(buf, &packet->mad, hdr_size(file)))
303 return -EFAULT;
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;
310 if (seg_payload < packet->length) {
312 * Multipacket RMPP MAD message. Copy remainder of message.
313 * Note that last segment may have a shorter payload.
315 if (count < hdr_size(file) + packet->length) {
317 * The buffer is too small, return the first RMPP segment,
318 * which includes the RMPP message length.
320 return -ENOSPC;
322 offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
323 max_seg_payload = seg_size - offset;
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;
335 return hdr_size(file) + packet->length;
338 static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
339 struct ib_umad_packet *packet, size_t count)
341 ssize_t size = hdr_size(file) + packet->length;
343 if (count < size)
344 return -EINVAL;
346 if (copy_to_user(buf, &packet->mad, hdr_size(file)))
347 return -EFAULT;
349 buf += hdr_size(file);
351 if (copy_to_user(buf, packet->mad.data, packet->length))
352 return -EFAULT;
354 return size;
357 static ssize_t ib_umad_read(struct file *filp, char __user *buf,
358 size_t count, loff_t *pos)
360 struct ib_umad_file *file = filp->private_data;
361 struct ib_umad_packet *packet;
362 ssize_t ret;
364 if (count < hdr_size(file))
365 return -EINVAL;
367 mutex_lock(&file->mutex);
369 while (list_empty(&file->recv_list)) {
370 mutex_unlock(&file->mutex);
372 if (filp->f_flags & O_NONBLOCK)
373 return -EAGAIN;
375 if (wait_event_interruptible(file->recv_wait,
376 !list_empty(&file->recv_list)))
377 return -ERESTARTSYS;
379 mutex_lock(&file->mutex);
382 packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
383 list_del(&packet->list);
385 mutex_unlock(&file->mutex);
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);
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);
402 return ret;
405 static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
407 int left, seg;
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;
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;
422 return 0;
425 static int same_destination(struct ib_user_mad_hdr *hdr1,
426 struct ib_user_mad_hdr *hdr2)
428 if (!hdr1->grh_present && !hdr2->grh_present)
429 return (hdr1->lid == hdr2->lid);
431 if (hdr1->grh_present && hdr2->grh_present)
432 return !memcmp(hdr1->gid, hdr2->gid, 16);
434 return 0;
437 static int is_duplicate(struct ib_umad_file *file,
438 struct ib_umad_packet *packet)
440 struct ib_umad_packet *sent_packet;
441 struct ib_mad_hdr *sent_hdr, *hdr;
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;
447 if ((hdr->tid != sent_hdr->tid) ||
448 (hdr->mgmt_class != sent_hdr->mgmt_class))
449 continue;
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.
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;
463 if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
464 return 1;
467 return 0;
470 static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
471 size_t count, loff_t *pos)
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;
483 if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
484 return -EINVAL;
486 packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
487 if (!packet)
488 return -ENOMEM;
490 if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
491 ret = -EFAULT;
492 goto err;
495 if (packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
496 ret = -EINVAL;
497 goto err;
500 buf += hdr_size(file);
502 if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
503 ret = -EFAULT;
504 goto err;
507 mutex_lock(&file->mutex);
509 agent = __get_agent(file, packet->mad.hdr.id);
510 if (!agent) {
511 ret = -EINVAL;
512 goto err_up;
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);
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;
537 rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
538 hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
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;
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;
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;
567 /* Copy MAD header. Any RMPP header is already in place. */
568 memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
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;
577 } else {
578 ret = copy_rmpp_mad(packet->msg, buf);
579 if (ret)
580 goto err_msg;
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.
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;
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;
613 ret = ib_post_send_mad(packet->msg, NULL);
614 if (ret)
615 goto err_send;
617 mutex_unlock(&file->mutex);
618 return count;
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;
633 static __poll_t ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
635 struct ib_umad_file *file = filp->private_data;
637 /* we will always be able to post a MAD send */
638 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
640 poll_wait(filp, &file->recv_wait, wait);
642 if (!list_empty(&file->recv_list))
643 mask |= EPOLLIN | EPOLLRDNORM;
645 return mask;
648 static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
649 int compat_method_mask)
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;
657 mutex_lock(&file->port->file_mutex);
658 mutex_lock(&file->mutex);
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;
667 if (copy_from_user(&ureq, arg, sizeof ureq)) {
668 ret = -EFAULT;
669 goto out;
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;
680 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
681 if (!__get_agent(file, agent_id))
682 goto found;
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;
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);
697 if (compat_method_mask) {
698 u32 *umm = (u32 *) ureq.method_mask;
699 int i;
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);
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;
720 if (put_user(agent_id,
721 (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
722 ret = -EFAULT;
723 goto out;
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");
737 file->agent[agent_id] = agent;
738 ret = 0;
740 out:
741 mutex_unlock(&file->mutex);
743 if (ret && agent)
744 ib_unregister_mad_agent(agent);
746 mutex_unlock(&file->port->file_mutex);
748 return ret;
751 static int ib_umad_reg_agent2(struct ib_umad_file *file, void __user *arg)
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;
759 mutex_lock(&file->port->file_mutex);
760 mutex_lock(&file->mutex);
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;
769 if (copy_from_user(&ureq, arg, sizeof(ureq))) {
770 ret = -EFAULT;
771 goto out;
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;
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;
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;
793 goto out;
796 for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
797 if (!__get_agent(file, agent_id))
798 goto found;
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;
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;
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));
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;
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;
844 if (!file->already_used) {
845 file->already_used = 1;
846 file->use_pkey_index = 1;
849 file->agent[agent_id] = agent;
850 ret = 0;
852 out:
853 mutex_unlock(&file->mutex);
855 if (ret && agent)
856 ib_unregister_mad_agent(agent);
858 mutex_unlock(&file->port->file_mutex);
860 return ret;
864 static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
866 struct ib_mad_agent *agent = NULL;
867 u32 id;
868 int ret = 0;
870 if (get_user(id, arg))
871 return -EFAULT;
872 if (id >= IB_UMAD_MAX_AGENTS)
873 return -EINVAL;
875 mutex_lock(&file->port->file_mutex);
876 mutex_lock(&file->mutex);
878 id = array_index_nospec(id, IB_UMAD_MAX_AGENTS);
879 if (!__get_agent(file, id)) {
880 ret = -EINVAL;
881 goto out;
884 agent = file->agent[id];
885 file->agent[id] = NULL;
887 out:
888 mutex_unlock(&file->mutex);
890 if (agent)
891 ib_unregister_mad_agent(agent);
893 mutex_unlock(&file->port->file_mutex);
895 return ret;
898 static long ib_umad_enable_pkey(struct ib_umad_file *file)
900 int ret = 0;
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);
909 return ret;
912 static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
913 unsigned long arg)
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;
929 #ifdef CONFIG_COMPAT
930 static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
931 unsigned long arg)
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;
946 #endif
949 * ib_umad_open() does not need the BKL:
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;
957 static int ib_umad_open(struct inode *inode, struct file *filp)
959 struct ib_umad_port *port;
960 struct ib_umad_file *file;
961 int ret = -ENXIO;
963 port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
965 mutex_lock(&port->file_mutex);
967 if (!port->ib_dev)
968 goto out;
970 ret = -ENOMEM;
971 file = kzalloc(sizeof *file, GFP_KERNEL);
972 if (!file)
973 goto out;
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);
981 file->port = port;
982 filp->private_data = file;
984 list_add_tail(&file->port_list, &port->file_list);
986 ret = nonseekable_open(inode, filp);
987 if (ret) {
988 list_del(&file->port_list);
989 kfree(file);
990 goto out;
993 kobject_get(&port->umad_dev->kobj);
995 out:
996 mutex_unlock(&port->file_mutex);
997 return ret;
1000 static int ib_umad_close(struct inode *inode, struct file *filp)
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;
1008 mutex_lock(&file->port->file_mutex);
1009 mutex_lock(&file->mutex);
1011 already_dead = file->agents_dead;
1012 file->agents_dead = 1;
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);
1020 list_del(&file->port_list);
1022 mutex_unlock(&file->mutex);
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]);
1029 mutex_unlock(&file->port->file_mutex);
1031 kfree(file);
1032 kobject_put(&dev->kobj);
1034 return 0;
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,
1051 static int ib_umad_sm_open(struct inode *inode, struct file *filp)
1053 struct ib_umad_port *port;
1054 struct ib_port_modify props = {
1055 .set_port_cap_mask = IB_PORT_SM
1057 int ret;
1059 port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
1061 if (filp->f_flags & O_NONBLOCK) {
1062 if (down_trylock(&port->sm_sem)) {
1063 ret = -EAGAIN;
1064 goto fail;
1066 } else {
1067 if (down_interruptible(&port->sm_sem)) {
1068 ret = -ERESTARTSYS;
1069 goto fail;
1073 ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
1074 if (ret)
1075 goto err_up_sem;
1077 filp->private_data = port;
1079 ret = nonseekable_open(inode, filp);
1080 if (ret)
1081 goto err_clr_sm_cap;
1083 kobject_get(&port->umad_dev->kobj);
1085 return 0;
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);
1091 err_up_sem:
1092 up(&port->sm_sem);
1094 fail:
1095 return ret;
1098 static int ib_umad_sm_close(struct inode *inode, struct file *filp)
1100 struct ib_umad_port *port = filp->private_data;
1101 struct ib_port_modify props = {
1102 .clr_port_cap_mask = IB_PORT_SM
1104 int ret = 0;
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);
1111 up(&port->sm_sem);
1113 kobject_put(&port->umad_dev->kobj);
1115 return ret;
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,
1125 static struct ib_client umad_client = {
1126 .name = "umad",
1127 .add = ib_umad_add_one,
1128 .remove = ib_umad_remove_one
1131 static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
1132 char *buf)
1134 struct ib_umad_port *port = dev_get_drvdata(dev);
1136 if (!port)
1137 return -ENODEV;
1139 return sprintf(buf, "%s\n", port->ib_dev->name);
1141 static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1143 static ssize_t show_port(struct device *dev, struct device_attribute *attr,
1144 char *buf)
1146 struct ib_umad_port *port = dev_get_drvdata(dev);
1148 if (!port)
1149 return -ENODEV;
1151 return sprintf(buf, "%d\n", port->port_num);
1153 static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1155 static CLASS_ATTR_STRING(abi_version, S_IRUGO,
1156 __stringify(IB_USER_MAD_ABI_VERSION));
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)
1162 int devnum;
1163 dev_t base_umad;
1164 dev_t base_issm;
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;
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);
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;
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;
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;
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;
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;
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;
1221 return 0;
1223 err_sm_dev:
1224 device_destroy(umad_class, port->sm_cdev.dev);
1226 err_sm_cdev:
1227 cdev_del(&port->sm_cdev);
1229 err_dev:
1230 device_destroy(umad_class, port->cdev.dev);
1232 err_cdev:
1233 cdev_del(&port->cdev);
1234 clear_bit(devnum, dev_map);
1236 return -1;
1239 static void ib_umad_kill_port(struct ib_umad_port *port)
1241 struct ib_umad_file *file;
1242 int id;
1244 dev_set_drvdata(port->dev, NULL);
1245 dev_set_drvdata(port->sm_dev, NULL);
1247 device_destroy(umad_class, port->cdev.dev);
1248 device_destroy(umad_class, port->sm_cdev.dev);
1250 cdev_del(&port->cdev);
1251 cdev_del(&port->sm_cdev);
1253 mutex_lock(&port->file_mutex);
1255 port->ib_dev = NULL;
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);
1262 for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
1263 if (file->agent[id])
1264 ib_unregister_mad_agent(file->agent[id]);
1267 mutex_unlock(&port->file_mutex);
1268 clear_bit(port->dev_num, dev_map);
1271 static void ib_umad_add_one(struct ib_device *device)
1273 struct ib_umad_device *umad_dev;
1274 int s, e, i;
1275 int count = 0;
1277 s = rdma_start_port(device);
1278 e = rdma_end_port(device);
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;
1286 kobject_init(&umad_dev->kobj, &ib_umad_dev_ktype);
1288 for (i = s; i <= e; ++i) {
1289 if (!rdma_cap_ib_mad(device, i))
1290 continue;
1292 umad_dev->port[i - s].umad_dev = umad_dev;
1294 if (ib_umad_init_port(device, i, umad_dev,
1295 &umad_dev->port[i - s]))
1296 goto err;
1298 count++;
1301 if (!count)
1302 goto free;
1304 ib_set_client_data(device, &umad_client, umad_dev);
1306 return;
1308 err:
1309 while (--i >= s) {
1310 if (!rdma_cap_ib_mad(device, i))
1311 continue;
1313 ib_umad_kill_port(&umad_dev->port[i - s]);
1315 free:
1316 kobject_put(&umad_dev->kobj);
1319 static void ib_umad_remove_one(struct ib_device *device, void *client_data)
1321 struct ib_umad_device *umad_dev = client_data;
1322 int i;
1324 if (!umad_dev)
1325 return;
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]);
1332 kobject_put(&umad_dev->kobj);
1335 static char *umad_devnode(struct device *dev, umode_t *mode)
1337 return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1340 static int __init ib_umad_init(void)
1342 int ret;
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;
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;
1359 dynamic_issm_dev = dynamic_umad_dev + IB_UMAD_NUM_DYNAMIC_MINOR;
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;
1368 umad_class->devnode = umad_devnode;
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;
1376 ret = ib_register_client(&umad_client);
1377 if (ret) {
1378 pr_err("couldn't register ib_umad client\n");
1379 goto out_class;
1382 return 0;
1384 out_class:
1385 class_destroy(umad_class);
1387 out_chrdev:
1388 unregister_chrdev_region(dynamic_umad_dev,
1389 IB_UMAD_NUM_DYNAMIC_MINOR * 2);
1391 out_alloc:
1392 unregister_chrdev_region(base_umad_dev,
1393 IB_UMAD_NUM_FIXED_MINOR * 2);
1395 out:
1396 return ret;
1399 static void __exit ib_umad_cleanup(void)
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);
1409 module_init(ib_umad_init);
1410 module_exit(ib_umad_cleanup);