11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds * Copyright (c) 2004 Topspin Communications. All rights reserved.
3cb183a06SHal Rosenstock * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
4cb183a06SHal Rosenstock * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
52fe7e6f7SRoland Dreier * Copyright (c) 2008 Cisco. All rights reserved.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * This software is available to you under a choice of one of two
81da177e4SLinus Torvalds * licenses. You may choose to be licensed under the terms of the GNU
91da177e4SLinus Torvalds * General Public License (GPL) Version 2, available from the file
101da177e4SLinus Torvalds * COPYING in the main directory of this source tree, or the
111da177e4SLinus Torvalds * OpenIB.org BSD license below:
121da177e4SLinus Torvalds *
131da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or
141da177e4SLinus Torvalds * without modification, are permitted provided that the following
151da177e4SLinus Torvalds * conditions are met:
161da177e4SLinus Torvalds *
171da177e4SLinus Torvalds * - Redistributions of source code must retain the above
181da177e4SLinus Torvalds * copyright notice, this list of conditions and the following
191da177e4SLinus Torvalds * disclaimer.
201da177e4SLinus Torvalds *
211da177e4SLinus Torvalds * - Redistributions in binary form must reproduce the above
221da177e4SLinus Torvalds * copyright notice, this list of conditions and the following
231da177e4SLinus Torvalds * disclaimer in the documentation and/or other materials
241da177e4SLinus Torvalds * provided with the distribution.
251da177e4SLinus Torvalds *
261da177e4SLinus Torvalds * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
271da177e4SLinus Torvalds * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
281da177e4SLinus Torvalds * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
291da177e4SLinus Torvalds * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
301da177e4SLinus Torvalds * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
311da177e4SLinus Torvalds * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
321da177e4SLinus Torvalds * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
331da177e4SLinus Torvalds * SOFTWARE.
341da177e4SLinus Torvalds */
351da177e4SLinus Torvalds
36f426a40eSIra Weiny #define pr_fmt(fmt) "user_mad: " fmt
37f426a40eSIra Weiny
381da177e4SLinus Torvalds #include <linux/module.h>
391da177e4SLinus Torvalds #include <linux/init.h>
401da177e4SLinus Torvalds #include <linux/device.h>
411da177e4SLinus Torvalds #include <linux/err.h>
421da177e4SLinus Torvalds #include <linux/fs.h>
431da177e4SLinus Torvalds #include <linux/cdev.h>
441da177e4SLinus Torvalds #include <linux/dma-mapping.h>
451da177e4SLinus Torvalds #include <linux/poll.h>
462fe7e6f7SRoland Dreier #include <linux/mutex.h>
471da177e4SLinus Torvalds #include <linux/kref.h>
48a394f83bSRoland Dreier #include <linux/compat.h>
49a99bbaf5SAlexey Dobriyan #include <linux/sched.h>
506188e10dSMatthew Wilcox #include <linux/semaphore.h>
515a0e3ad6STejun Heo #include <linux/slab.h>
5261f25982SLuck, Tony #include <linux/nospec.h>
531da177e4SLinus Torvalds
547c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
551da177e4SLinus Torvalds
56a4d61e84SRoland Dreier #include <rdma/ib_mad.h>
57a4d61e84SRoland Dreier #include <rdma/ib_user_mad.h>
588f71bb00SJason Gunthorpe #include <rdma/rdma_netlink.h>
591da177e4SLinus Torvalds
608cf12d77SHuy Nguyen #include "core_priv.h"
618cf12d77SHuy Nguyen
621da177e4SLinus Torvalds MODULE_AUTHOR("Roland Dreier");
631da177e4SLinus Torvalds MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
641da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL");
651da177e4SLinus Torvalds
66*b8c5f635SMichael Guralnik #define MAX_UMAD_RECV_LIST_SIZE 200000
67*b8c5f635SMichael Guralnik
681da177e4SLinus Torvalds enum {
698cf12d77SHuy Nguyen IB_UMAD_MAX_PORTS = RDMA_MAX_PORTS,
701da177e4SLinus Torvalds IB_UMAD_MAX_AGENTS = 32,
711da177e4SLinus Torvalds
721da177e4SLinus Torvalds IB_UMAD_MAJOR = 231,
738cf12d77SHuy Nguyen IB_UMAD_MINOR_BASE = 0,
748cf12d77SHuy Nguyen IB_UMAD_NUM_FIXED_MINOR = 64,
758cf12d77SHuy Nguyen IB_UMAD_NUM_DYNAMIC_MINOR = IB_UMAD_MAX_PORTS - IB_UMAD_NUM_FIXED_MINOR,
768cf12d77SHuy Nguyen IB_ISSM_MINOR_BASE = IB_UMAD_NUM_FIXED_MINOR,
771da177e4SLinus Torvalds };
781da177e4SLinus Torvalds
79a74968f8SRoland Dreier /*
806aa2a86eSAlexander Chiang * Our lifetime rules for these structs are the following:
816aa2a86eSAlexander Chiang * device special file is opened, we take a reference on the
826aa2a86eSAlexander Chiang * ib_umad_port's struct ib_umad_device. We drop these
83a74968f8SRoland Dreier * references in the corresponding close().
84a74968f8SRoland Dreier *
85a74968f8SRoland Dreier * In addition to references coming from open character devices, there
86a74968f8SRoland Dreier * is one more reference to each ib_umad_device representing the
87a74968f8SRoland Dreier * module's reference taken when allocating the ib_umad_device in
88a74968f8SRoland Dreier * ib_umad_add_one().
89a74968f8SRoland Dreier *
906aa2a86eSAlexander Chiang * When destroying an ib_umad_device, we drop the module's reference.
91a74968f8SRoland Dreier */
921da177e4SLinus Torvalds
93a74968f8SRoland Dreier struct ib_umad_port {
942b937afcSAlexander Chiang struct cdev cdev;
95e9dd5dafSParav Pandit struct device dev;
962b937afcSAlexander Chiang struct cdev sm_cdev;
97e9dd5dafSParav Pandit struct device sm_dev;
981da177e4SLinus Torvalds struct semaphore sm_sem;
991da177e4SLinus Torvalds
1002fe7e6f7SRoland Dreier struct mutex file_mutex;
1010c99cb6dSRoland Dreier struct list_head file_list;
1020c99cb6dSRoland Dreier
1031da177e4SLinus Torvalds struct ib_device *ib_dev;
1041da177e4SLinus Torvalds struct ib_umad_device *umad_dev;
105a74968f8SRoland Dreier int dev_num;
1061fb7f897SMark Bloch u32 port_num;
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds struct ib_umad_device {
110e9dd5dafSParav Pandit struct kref kref;
111e9dd5dafSParav Pandit struct ib_umad_port ports[];
1121da177e4SLinus Torvalds };
1131da177e4SLinus Torvalds
1141da177e4SLinus Torvalds struct ib_umad_file {
1152fe7e6f7SRoland Dreier struct mutex mutex;
1161da177e4SLinus Torvalds struct ib_umad_port *port;
1171da177e4SLinus Torvalds struct list_head recv_list;
118*b8c5f635SMichael Guralnik atomic_t recv_list_size;
1192527e681SSean Hefty struct list_head send_list;
1200c99cb6dSRoland Dreier struct list_head port_list;
1212527e681SSean Hefty spinlock_t send_lock;
1221da177e4SLinus Torvalds wait_queue_head_t recv_wait;
1231da177e4SLinus Torvalds struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
12494382f35SRoland Dreier int agents_dead;
1252be8e3eeSRoland Dreier u8 use_pkey_index;
1262be8e3eeSRoland Dreier u8 already_used;
1271da177e4SLinus Torvalds };
1281da177e4SLinus Torvalds
1291da177e4SLinus Torvalds struct ib_umad_packet {
130cb183a06SHal Rosenstock struct ib_mad_send_buf *msg;
131f36e1793SJack Morgenstein struct ib_mad_recv_wc *recv_wc;
1321da177e4SLinus Torvalds struct list_head list;
133cb183a06SHal Rosenstock int length;
134cb183a06SHal Rosenstock struct ib_user_mad mad;
1351da177e4SLinus Torvalds };
1361da177e4SLinus Torvalds
137aa4d540bSGustavo A. R. Silva struct ib_rmpp_mad_hdr {
138aa4d540bSGustavo A. R. Silva struct ib_mad_hdr mad_hdr;
139aa4d540bSGustavo A. R. Silva struct ib_rmpp_hdr rmpp_hdr;
140aa4d540bSGustavo A. R. Silva } __packed;
141aa4d540bSGustavo A. R. Silva
14205653319SIra Weiny #define CREATE_TRACE_POINTS
14305653319SIra Weiny #include <trace/events/ib_umad.h>
14405653319SIra Weiny
1458cf12d77SHuy Nguyen static const dev_t base_umad_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
1468cf12d77SHuy Nguyen static const dev_t base_issm_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE) +
1478cf12d77SHuy Nguyen IB_UMAD_NUM_FIXED_MINOR;
1488cf12d77SHuy Nguyen static dev_t dynamic_umad_dev;
1498cf12d77SHuy Nguyen static dev_t dynamic_issm_dev;
150a74968f8SRoland Dreier
151551d315eSLeon Romanovsky static DEFINE_IDA(umad_ida);
1521da177e4SLinus Torvalds
15311a0ae4cSJason Gunthorpe static int ib_umad_add_one(struct ib_device *device);
1547c1eb45aSHaggai Eran static void ib_umad_remove_one(struct ib_device *device, void *client_data);
1551da177e4SLinus Torvalds
ib_umad_dev_free(struct kref * kref)156e9dd5dafSParav Pandit static void ib_umad_dev_free(struct kref *kref)
157a74968f8SRoland Dreier {
158a74968f8SRoland Dreier struct ib_umad_device *dev =
159e9dd5dafSParav Pandit container_of(kref, struct ib_umad_device, kref);
160a74968f8SRoland Dreier
161a74968f8SRoland Dreier kfree(dev);
162a74968f8SRoland Dreier }
163a74968f8SRoland Dreier
ib_umad_dev_get(struct ib_umad_device * dev)164e9dd5dafSParav Pandit static void ib_umad_dev_get(struct ib_umad_device *dev)
165e9dd5dafSParav Pandit {
166e9dd5dafSParav Pandit kref_get(&dev->kref);
167e9dd5dafSParav Pandit }
168e9dd5dafSParav Pandit
ib_umad_dev_put(struct ib_umad_device * dev)169e9dd5dafSParav Pandit static void ib_umad_dev_put(struct ib_umad_device *dev)
170e9dd5dafSParav Pandit {
171e9dd5dafSParav Pandit kref_put(&dev->kref, ib_umad_dev_free);
172e9dd5dafSParav Pandit }
17360e1751cSBart Van Assche
hdr_size(struct ib_umad_file * file)1742be8e3eeSRoland Dreier static int hdr_size(struct ib_umad_file *file)
1752be8e3eeSRoland Dreier {
1762be8e3eeSRoland Dreier return file->use_pkey_index ? sizeof(struct ib_user_mad_hdr) :
1772be8e3eeSRoland Dreier sizeof(struct ib_user_mad_hdr_old);
1782be8e3eeSRoland Dreier }
1792be8e3eeSRoland Dreier
1802fe7e6f7SRoland Dreier /* caller must hold file->mutex */
__get_agent(struct ib_umad_file * file,int id)18194382f35SRoland Dreier static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
18294382f35SRoland Dreier {
18394382f35SRoland Dreier return file->agents_dead ? NULL : file->agent[id];
18494382f35SRoland Dreier }
18594382f35SRoland Dreier
queue_packet(struct ib_umad_file * file,struct ib_mad_agent * agent,struct ib_umad_packet * packet,bool is_recv_mad)186*b8c5f635SMichael Guralnik static int queue_packet(struct ib_umad_file *file, struct ib_mad_agent *agent,
187*b8c5f635SMichael Guralnik struct ib_umad_packet *packet, bool is_recv_mad)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds int ret = 1;
1901da177e4SLinus Torvalds
1912fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
19294382f35SRoland Dreier
193*b8c5f635SMichael Guralnik if (is_recv_mad &&
194*b8c5f635SMichael Guralnik atomic_read(&file->recv_list_size) > MAX_UMAD_RECV_LIST_SIZE)
195*b8c5f635SMichael Guralnik goto unlock;
196*b8c5f635SMichael Guralnik
197cb183a06SHal Rosenstock for (packet->mad.hdr.id = 0;
198cb183a06SHal Rosenstock packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
199cb183a06SHal Rosenstock packet->mad.hdr.id++)
20094382f35SRoland Dreier if (agent == __get_agent(file, packet->mad.hdr.id)) {
2011da177e4SLinus Torvalds list_add_tail(&packet->list, &file->recv_list);
202*b8c5f635SMichael Guralnik atomic_inc(&file->recv_list_size);
2031da177e4SLinus Torvalds wake_up_interruptible(&file->recv_wait);
2041da177e4SLinus Torvalds ret = 0;
2051da177e4SLinus Torvalds break;
2061da177e4SLinus Torvalds }
207*b8c5f635SMichael Guralnik unlock:
2082fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
2091da177e4SLinus Torvalds
2101da177e4SLinus Torvalds return ret;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds
dequeue_send(struct ib_umad_file * file,struct ib_umad_packet * packet)2132527e681SSean Hefty static void dequeue_send(struct ib_umad_file *file,
2142527e681SSean Hefty struct ib_umad_packet *packet)
2152527e681SSean Hefty {
2162527e681SSean Hefty spin_lock_irq(&file->send_lock);
2172527e681SSean Hefty list_del(&packet->list);
2182527e681SSean Hefty spin_unlock_irq(&file->send_lock);
2192527e681SSean Hefty }
2202527e681SSean Hefty
send_handler(struct ib_mad_agent * agent,struct ib_mad_send_wc * send_wc)2211da177e4SLinus Torvalds static void send_handler(struct ib_mad_agent *agent,
2221da177e4SLinus Torvalds struct ib_mad_send_wc *send_wc)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds struct ib_umad_file *file = agent->context;
22534816ad9SSean Hefty struct ib_umad_packet *packet = send_wc->send_buf->context[0];
2261da177e4SLinus Torvalds
2272527e681SSean Hefty dequeue_send(file, packet);
2282553ba21SGal Pressman rdma_destroy_ah(packet->msg->ah, RDMA_DESTROY_AH_SLEEPABLE);
229cb183a06SHal Rosenstock ib_free_send_mad(packet->msg);
2301da177e4SLinus Torvalds
2311da177e4SLinus Torvalds if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
232f36e1793SJack Morgenstein packet->length = IB_MGMT_MAD_HDR;
233f36e1793SJack Morgenstein packet->mad.hdr.status = ETIMEDOUT;
234*b8c5f635SMichael Guralnik if (!queue_packet(file, agent, packet, false))
235f36e1793SJack Morgenstein return;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds kfree(packet);
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds
recv_handler(struct ib_mad_agent * agent,struct ib_mad_send_buf * send_buf,struct ib_mad_recv_wc * mad_recv_wc)2401da177e4SLinus Torvalds static void recv_handler(struct ib_mad_agent *agent,
241ca281265SChristoph Hellwig struct ib_mad_send_buf *send_buf,
2421da177e4SLinus Torvalds struct ib_mad_recv_wc *mad_recv_wc)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds struct ib_umad_file *file = agent->context;
2451da177e4SLinus Torvalds struct ib_umad_packet *packet;
2461da177e4SLinus Torvalds
2471da177e4SLinus Torvalds if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
248f36e1793SJack Morgenstein goto err1;
2491da177e4SLinus Torvalds
250f36e1793SJack Morgenstein packet = kzalloc(sizeof *packet, GFP_KERNEL);
2511da177e4SLinus Torvalds if (!packet)
252f36e1793SJack Morgenstein goto err1;
2531da177e4SLinus Torvalds
254f36e1793SJack Morgenstein packet->length = mad_recv_wc->mad_len;
255f36e1793SJack Morgenstein packet->recv_wc = mad_recv_wc;
256cb183a06SHal Rosenstock
257cb183a06SHal Rosenstock packet->mad.hdr.status = 0;
2582be8e3eeSRoland Dreier packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len;
259cb183a06SHal Rosenstock packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
2606588e412SDon Hiatt /*
2616588e412SDon Hiatt * On OPA devices it is okay to lose the upper 16 bits of LID as this
2626588e412SDon Hiatt * information is obtained elsewhere. Mask off the upper 16 bits.
2636588e412SDon Hiatt */
2642ef7f2e2SDon Hiatt if (rdma_cap_opa_mad(agent->device, agent->port_num))
2656588e412SDon Hiatt packet->mad.hdr.lid = ib_lid_be16(0xFFFF &
2666588e412SDon Hiatt mad_recv_wc->wc->slid);
2676588e412SDon Hiatt else
26862ede777SHiatt, Don packet->mad.hdr.lid = ib_lid_be16(mad_recv_wc->wc->slid);
269cb183a06SHal Rosenstock packet->mad.hdr.sl = mad_recv_wc->wc->sl;
270cb183a06SHal Rosenstock packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
2712be8e3eeSRoland Dreier packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
272cb183a06SHal Rosenstock packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
273cb183a06SHal Rosenstock if (packet->mad.hdr.grh_present) {
27490898850SDasaratharaman Chandramouli struct rdma_ah_attr ah_attr;
275d8966fcdSDasaratharaman Chandramouli const struct ib_global_route *grh;
2760c4386ecSParav Pandit int ret;
277aeba84a9SSean Hefty
278f6bdb142SParav Pandit ret = ib_init_ah_attr_from_wc(agent->device, agent->port_num,
2790c4386ecSParav Pandit mad_recv_wc->wc,
2800c4386ecSParav Pandit mad_recv_wc->recv_buf.grh,
281aeba84a9SSean Hefty &ah_attr);
2820c4386ecSParav Pandit if (ret)
2830c4386ecSParav Pandit goto err2;
284aeba84a9SSean Hefty
285d8966fcdSDasaratharaman Chandramouli grh = rdma_ah_read_grh(&ah_attr);
286d8966fcdSDasaratharaman Chandramouli packet->mad.hdr.gid_index = grh->sgid_index;
287d8966fcdSDasaratharaman Chandramouli packet->mad.hdr.hop_limit = grh->hop_limit;
288d8966fcdSDasaratharaman Chandramouli packet->mad.hdr.traffic_class = grh->traffic_class;
289d8966fcdSDasaratharaman Chandramouli memcpy(packet->mad.hdr.gid, &grh->dgid, 16);
290d8966fcdSDasaratharaman Chandramouli packet->mad.hdr.flow_label = cpu_to_be32(grh->flow_label);
291b7403217SParav Pandit rdma_destroy_ah_attr(&ah_attr);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds
294*b8c5f635SMichael Guralnik if (queue_packet(file, agent, packet, true))
295f36e1793SJack Morgenstein goto err2;
296f36e1793SJack Morgenstein return;
2971da177e4SLinus Torvalds
298f36e1793SJack Morgenstein err2:
299f36e1793SJack Morgenstein kfree(packet);
300f36e1793SJack Morgenstein err1:
3011da177e4SLinus Torvalds ib_free_recv_mad(mad_recv_wc);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds
copy_recv_mad(struct ib_umad_file * file,char __user * buf,struct ib_umad_packet * packet,size_t count)3042be8e3eeSRoland Dreier static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
3052be8e3eeSRoland Dreier struct ib_umad_packet *packet, size_t count)
306f36e1793SJack Morgenstein {
307f36e1793SJack Morgenstein struct ib_mad_recv_buf *recv_buf;
308f36e1793SJack Morgenstein int left, seg_payload, offset, max_seg_payload;
3098e4349d1SIra Weiny size_t seg_size;
3108e4349d1SIra Weiny
3118e4349d1SIra Weiny recv_buf = &packet->recv_wc->recv_buf;
3128e4349d1SIra Weiny seg_size = packet->recv_wc->mad_seg_size;
313f36e1793SJack Morgenstein
314f36e1793SJack Morgenstein /* We need enough room to copy the first (or only) MAD segment. */
3158e4349d1SIra Weiny if ((packet->length <= seg_size &&
3162be8e3eeSRoland Dreier count < hdr_size(file) + packet->length) ||
3178e4349d1SIra Weiny (packet->length > seg_size &&
3188e4349d1SIra Weiny count < hdr_size(file) + seg_size))
319f36e1793SJack Morgenstein return -EINVAL;
320f36e1793SJack Morgenstein
3212be8e3eeSRoland Dreier if (copy_to_user(buf, &packet->mad, hdr_size(file)))
322f36e1793SJack Morgenstein return -EFAULT;
323f36e1793SJack Morgenstein
3242be8e3eeSRoland Dreier buf += hdr_size(file);
3258e4349d1SIra Weiny seg_payload = min_t(int, packet->length, seg_size);
326f36e1793SJack Morgenstein if (copy_to_user(buf, recv_buf->mad, seg_payload))
327f36e1793SJack Morgenstein return -EFAULT;
328f36e1793SJack Morgenstein
329f36e1793SJack Morgenstein if (seg_payload < packet->length) {
330f36e1793SJack Morgenstein /*
331f36e1793SJack Morgenstein * Multipacket RMPP MAD message. Copy remainder of message.
332f36e1793SJack Morgenstein * Note that last segment may have a shorter payload.
333f36e1793SJack Morgenstein */
3342be8e3eeSRoland Dreier if (count < hdr_size(file) + packet->length) {
335f36e1793SJack Morgenstein /*
336f36e1793SJack Morgenstein * The buffer is too small, return the first RMPP segment,
337f36e1793SJack Morgenstein * which includes the RMPP message length.
338f36e1793SJack Morgenstein */
339f36e1793SJack Morgenstein return -ENOSPC;
340f36e1793SJack Morgenstein }
341618a3c03SHal Rosenstock offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
3428e4349d1SIra Weiny max_seg_payload = seg_size - offset;
343f36e1793SJack Morgenstein
344f36e1793SJack Morgenstein for (left = packet->length - seg_payload, buf += seg_payload;
345f36e1793SJack Morgenstein left; left -= seg_payload, buf += seg_payload) {
346f36e1793SJack Morgenstein recv_buf = container_of(recv_buf->list.next,
347f36e1793SJack Morgenstein struct ib_mad_recv_buf, list);
348f36e1793SJack Morgenstein seg_payload = min(left, max_seg_payload);
349f36e1793SJack Morgenstein if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
350f36e1793SJack Morgenstein seg_payload))
351f36e1793SJack Morgenstein return -EFAULT;
352f36e1793SJack Morgenstein }
353f36e1793SJack Morgenstein }
35405653319SIra Weiny
35505653319SIra Weiny trace_ib_umad_read_recv(file, &packet->mad.hdr, &recv_buf->mad->mad_hdr);
35605653319SIra Weiny
3572be8e3eeSRoland Dreier return hdr_size(file) + packet->length;
358f36e1793SJack Morgenstein }
359f36e1793SJack Morgenstein
copy_send_mad(struct ib_umad_file * file,char __user * buf,struct ib_umad_packet * packet,size_t count)3602be8e3eeSRoland Dreier static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
3612be8e3eeSRoland Dreier struct ib_umad_packet *packet, size_t count)
362f36e1793SJack Morgenstein {
3632be8e3eeSRoland Dreier ssize_t size = hdr_size(file) + packet->length;
364f36e1793SJack Morgenstein
365f36e1793SJack Morgenstein if (count < size)
366f36e1793SJack Morgenstein return -EINVAL;
367f36e1793SJack Morgenstein
3682be8e3eeSRoland Dreier if (copy_to_user(buf, &packet->mad, hdr_size(file)))
3692be8e3eeSRoland Dreier return -EFAULT;
3702be8e3eeSRoland Dreier
3712be8e3eeSRoland Dreier buf += hdr_size(file);
3722be8e3eeSRoland Dreier
3732be8e3eeSRoland Dreier if (copy_to_user(buf, packet->mad.data, packet->length))
374f36e1793SJack Morgenstein return -EFAULT;
375f36e1793SJack Morgenstein
37605653319SIra Weiny trace_ib_umad_read_send(file, &packet->mad.hdr,
37705653319SIra Weiny (struct ib_mad_hdr *)&packet->mad.data);
37805653319SIra Weiny
379f36e1793SJack Morgenstein return size;
380f36e1793SJack Morgenstein }
381f36e1793SJack Morgenstein
ib_umad_read(struct file * filp,char __user * buf,size_t count,loff_t * pos)3821da177e4SLinus Torvalds static ssize_t ib_umad_read(struct file *filp, char __user *buf,
3831da177e4SLinus Torvalds size_t count, loff_t *pos)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds struct ib_umad_file *file = filp->private_data;
3861da177e4SLinus Torvalds struct ib_umad_packet *packet;
3871da177e4SLinus Torvalds ssize_t ret;
3881da177e4SLinus Torvalds
3892be8e3eeSRoland Dreier if (count < hdr_size(file))
3901da177e4SLinus Torvalds return -EINVAL;
3911da177e4SLinus Torvalds
3922fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
3931da177e4SLinus Torvalds
3944fc54618SShay Drory if (file->agents_dead) {
3954fc54618SShay Drory mutex_unlock(&file->mutex);
3964fc54618SShay Drory return -EIO;
3974fc54618SShay Drory }
3984fc54618SShay Drory
3991da177e4SLinus Torvalds while (list_empty(&file->recv_list)) {
4002fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
4011da177e4SLinus Torvalds
4021da177e4SLinus Torvalds if (filp->f_flags & O_NONBLOCK)
4031da177e4SLinus Torvalds return -EAGAIN;
4041da177e4SLinus Torvalds
4051da177e4SLinus Torvalds if (wait_event_interruptible(file->recv_wait,
4061da177e4SLinus Torvalds !list_empty(&file->recv_list)))
4071da177e4SLinus Torvalds return -ERESTARTSYS;
4081da177e4SLinus Torvalds
4092fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds
412def4cd43SShay Drory if (file->agents_dead) {
413def4cd43SShay Drory mutex_unlock(&file->mutex);
414def4cd43SShay Drory return -EIO;
415def4cd43SShay Drory }
416def4cd43SShay Drory
4171da177e4SLinus Torvalds packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
4181da177e4SLinus Torvalds list_del(&packet->list);
419*b8c5f635SMichael Guralnik atomic_dec(&file->recv_list_size);
4201da177e4SLinus Torvalds
4212fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
4221da177e4SLinus Torvalds
423f36e1793SJack Morgenstein if (packet->recv_wc)
4242be8e3eeSRoland Dreier ret = copy_recv_mad(file, buf, packet, count);
4251da177e4SLinus Torvalds else
4262be8e3eeSRoland Dreier ret = copy_send_mad(file, buf, packet, count);
427f36e1793SJack Morgenstein
428cb183a06SHal Rosenstock if (ret < 0) {
429cb183a06SHal Rosenstock /* Requeue packet */
4302fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
431cb183a06SHal Rosenstock list_add(&packet->list, &file->recv_list);
432*b8c5f635SMichael Guralnik atomic_inc(&file->recv_list_size);
4332fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
434f36e1793SJack Morgenstein } else {
435f36e1793SJack Morgenstein if (packet->recv_wc)
436f36e1793SJack Morgenstein ib_free_recv_mad(packet->recv_wc);
4371da177e4SLinus Torvalds kfree(packet);
438f36e1793SJack Morgenstein }
4391da177e4SLinus Torvalds return ret;
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds
copy_rmpp_mad(struct ib_mad_send_buf * msg,const char __user * buf)442f36e1793SJack Morgenstein static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
443f36e1793SJack Morgenstein {
444f36e1793SJack Morgenstein int left, seg;
445f36e1793SJack Morgenstein
446f36e1793SJack Morgenstein /* Copy class specific header */
447f36e1793SJack Morgenstein if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
448f36e1793SJack Morgenstein copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
449f36e1793SJack Morgenstein msg->hdr_len - IB_MGMT_RMPP_HDR))
450f36e1793SJack Morgenstein return -EFAULT;
451f36e1793SJack Morgenstein
452f36e1793SJack Morgenstein /* All headers are in place. Copy data segments. */
453f36e1793SJack Morgenstein for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
454f36e1793SJack Morgenstein seg++, left -= msg->seg_size, buf += msg->seg_size) {
455f36e1793SJack Morgenstein if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
456f36e1793SJack Morgenstein min(left, msg->seg_size)))
457f36e1793SJack Morgenstein return -EFAULT;
458f36e1793SJack Morgenstein }
459f36e1793SJack Morgenstein return 0;
460f36e1793SJack Morgenstein }
461f36e1793SJack Morgenstein
same_destination(struct ib_user_mad_hdr * hdr1,struct ib_user_mad_hdr * hdr2)4622527e681SSean Hefty static int same_destination(struct ib_user_mad_hdr *hdr1,
4632527e681SSean Hefty struct ib_user_mad_hdr *hdr2)
4642527e681SSean Hefty {
4652527e681SSean Hefty if (!hdr1->grh_present && !hdr2->grh_present)
4662527e681SSean Hefty return (hdr1->lid == hdr2->lid);
4672527e681SSean Hefty
4682527e681SSean Hefty if (hdr1->grh_present && hdr2->grh_present)
4692527e681SSean Hefty return !memcmp(hdr1->gid, hdr2->gid, 16);
4702527e681SSean Hefty
4712527e681SSean Hefty return 0;
4722527e681SSean Hefty }
4732527e681SSean Hefty
is_duplicate(struct ib_umad_file * file,struct ib_umad_packet * packet)4742527e681SSean Hefty static int is_duplicate(struct ib_umad_file *file,
4752527e681SSean Hefty struct ib_umad_packet *packet)
4762527e681SSean Hefty {
4772527e681SSean Hefty struct ib_umad_packet *sent_packet;
4782527e681SSean Hefty struct ib_mad_hdr *sent_hdr, *hdr;
4792527e681SSean Hefty
4802527e681SSean Hefty hdr = (struct ib_mad_hdr *) packet->mad.data;
4812527e681SSean Hefty list_for_each_entry(sent_packet, &file->send_list, list) {
4822527e681SSean Hefty sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
4832527e681SSean Hefty
4842527e681SSean Hefty if ((hdr->tid != sent_hdr->tid) ||
4852527e681SSean Hefty (hdr->mgmt_class != sent_hdr->mgmt_class))
4862527e681SSean Hefty continue;
4872527e681SSean Hefty
4882527e681SSean Hefty /*
4892527e681SSean Hefty * No need to be overly clever here. If two new operations have
4902527e681SSean Hefty * the same TID, reject the second as a duplicate. This is more
4912527e681SSean Hefty * restrictive than required by the spec.
4922527e681SSean Hefty */
49396909308SIra Weiny if (!ib_response_mad(hdr)) {
49496909308SIra Weiny if (!ib_response_mad(sent_hdr))
4952527e681SSean Hefty return 1;
4962527e681SSean Hefty continue;
49796909308SIra Weiny } else if (!ib_response_mad(sent_hdr))
4982527e681SSean Hefty continue;
4992527e681SSean Hefty
5002527e681SSean Hefty if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
5012527e681SSean Hefty return 1;
5022527e681SSean Hefty }
5032527e681SSean Hefty
5042527e681SSean Hefty return 0;
5052527e681SSean Hefty }
5062527e681SSean Hefty
ib_umad_write(struct file * filp,const char __user * buf,size_t count,loff_t * pos)5071da177e4SLinus Torvalds static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
5081da177e4SLinus Torvalds size_t count, loff_t *pos)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds struct ib_umad_file *file = filp->private_data;
511aa4d540bSGustavo A. R. Silva struct ib_rmpp_mad_hdr *rmpp_mad_hdr;
5121da177e4SLinus Torvalds struct ib_umad_packet *packet;
5131da177e4SLinus Torvalds struct ib_mad_agent *agent;
51490898850SDasaratharaman Chandramouli struct rdma_ah_attr ah_attr;
51534816ad9SSean Hefty struct ib_ah *ah;
51697f52eb4SSean Hefty __be64 *tid;
517f36e1793SJack Morgenstein int ret, data_len, hdr_len, copy_offset, rmpp_active;
5188e4349d1SIra Weiny u8 base_version;
5191da177e4SLinus Torvalds
5202be8e3eeSRoland Dreier if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
5211da177e4SLinus Torvalds return -EINVAL;
5221da177e4SLinus Torvalds
523aa4d540bSGustavo A. R. Silva packet = kzalloc(sizeof(*packet) + IB_MGMT_RMPP_HDR, GFP_KERNEL);
5241da177e4SLinus Torvalds if (!packet)
5251da177e4SLinus Torvalds return -ENOMEM;
5261da177e4SLinus Torvalds
5272be8e3eeSRoland Dreier if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
528cb183a06SHal Rosenstock ret = -EFAULT;
529cb183a06SHal Rosenstock goto err;
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds
532caf6e3f2SHefty, Sean if (packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
5331da177e4SLinus Torvalds ret = -EINVAL;
5341da177e4SLinus Torvalds goto err;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds
5372be8e3eeSRoland Dreier buf += hdr_size(file);
5382be8e3eeSRoland Dreier
5392be8e3eeSRoland Dreier if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
5402be8e3eeSRoland Dreier ret = -EFAULT;
5412be8e3eeSRoland Dreier goto err;
5422be8e3eeSRoland Dreier }
5432be8e3eeSRoland Dreier
5442fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
5451da177e4SLinus Torvalds
54605653319SIra Weiny trace_ib_umad_write(file, &packet->mad.hdr,
54705653319SIra Weiny (struct ib_mad_hdr *)&packet->mad.data);
54805653319SIra Weiny
54994382f35SRoland Dreier agent = __get_agent(file, packet->mad.hdr.id);
5501da177e4SLinus Torvalds if (!agent) {
5514fc54618SShay Drory ret = -EIO;
5521da177e4SLinus Torvalds goto err_up;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds
5551da177e4SLinus Torvalds memset(&ah_attr, 0, sizeof ah_attr);
556f23a5350SJack Morgenstein ah_attr.type = rdma_ah_find_type(agent->device,
55744c58487SDasaratharaman Chandramouli file->port->port_num);
558d8966fcdSDasaratharaman Chandramouli rdma_ah_set_dlid(&ah_attr, be16_to_cpu(packet->mad.hdr.lid));
559d8966fcdSDasaratharaman Chandramouli rdma_ah_set_sl(&ah_attr, packet->mad.hdr.sl);
560d8966fcdSDasaratharaman Chandramouli rdma_ah_set_path_bits(&ah_attr, packet->mad.hdr.path_bits);
561d8966fcdSDasaratharaman Chandramouli rdma_ah_set_port_num(&ah_attr, file->port->port_num);
562cb183a06SHal Rosenstock if (packet->mad.hdr.grh_present) {
563d8966fcdSDasaratharaman Chandramouli rdma_ah_set_grh(&ah_attr, NULL,
564d8966fcdSDasaratharaman Chandramouli be32_to_cpu(packet->mad.hdr.flow_label),
565d8966fcdSDasaratharaman Chandramouli packet->mad.hdr.gid_index,
566d8966fcdSDasaratharaman Chandramouli packet->mad.hdr.hop_limit,
567d8966fcdSDasaratharaman Chandramouli packet->mad.hdr.traffic_class);
568d8966fcdSDasaratharaman Chandramouli rdma_ah_set_dgid_raw(&ah_attr, packet->mad.hdr.gid);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds
5715cda6587SParav Pandit ah = rdma_create_user_ah(agent->qp->pd, &ah_attr, NULL);
57234816ad9SSean Hefty if (IS_ERR(ah)) {
57334816ad9SSean Hefty ret = PTR_ERR(ah);
5741da177e4SLinus Torvalds goto err_up;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds
577aa4d540bSGustavo A. R. Silva rmpp_mad_hdr = (struct ib_rmpp_mad_hdr *)packet->mad.data;
578aa4d540bSGustavo A. R. Silva hdr_len = ib_get_mad_data_offset(rmpp_mad_hdr->mad_hdr.mgmt_class);
5791471cb6cSIra Weiny
580aa4d540bSGustavo A. R. Silva if (ib_is_mad_class_rmpp(rmpp_mad_hdr->mad_hdr.mgmt_class)
5811471cb6cSIra Weiny && ib_mad_kernel_rmpp_agent(agent)) {
582618a3c03SHal Rosenstock copy_offset = IB_MGMT_RMPP_HDR;
583aa4d540bSGustavo A. R. Silva rmpp_active = ib_get_rmpp_flags(&rmpp_mad_hdr->rmpp_hdr) &
584618a3c03SHal Rosenstock IB_MGMT_RMPP_FLAG_ACTIVE;
5851471cb6cSIra Weiny } else {
5861471cb6cSIra Weiny copy_offset = IB_MGMT_MAD_HDR;
5871471cb6cSIra Weiny rmpp_active = 0;
588cb183a06SHal Rosenstock }
589cb183a06SHal Rosenstock
5908e4349d1SIra Weiny base_version = ((struct ib_mad_hdr *)&packet->mad.data)->base_version;
5912be8e3eeSRoland Dreier data_len = count - hdr_size(file) - hdr_len;
592cb183a06SHal Rosenstock packet->msg = ib_create_send_mad(agent,
593cb183a06SHal Rosenstock be32_to_cpu(packet->mad.hdr.qpn),
5942be8e3eeSRoland Dreier packet->mad.hdr.pkey_index, rmpp_active,
595da2dfaa3SIra Weiny hdr_len, data_len, GFP_KERNEL,
5968e4349d1SIra Weiny base_version);
597cb183a06SHal Rosenstock if (IS_ERR(packet->msg)) {
598cb183a06SHal Rosenstock ret = PTR_ERR(packet->msg);
599cb183a06SHal Rosenstock goto err_ah;
600cb183a06SHal Rosenstock }
601cb183a06SHal Rosenstock
60234816ad9SSean Hefty packet->msg->ah = ah;
60334816ad9SSean Hefty packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
60434816ad9SSean Hefty packet->msg->retries = packet->mad.hdr.retries;
60534816ad9SSean Hefty packet->msg->context[0] = packet;
606cb183a06SHal Rosenstock
607f36e1793SJack Morgenstein /* Copy MAD header. Any RMPP header is already in place. */
608cb0f0910SSean Hefty memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
609f36e1793SJack Morgenstein
610f36e1793SJack Morgenstein if (!rmpp_active) {
611cb0f0910SSean Hefty if (copy_from_user(packet->msg->mad + copy_offset,
612f36e1793SJack Morgenstein buf + copy_offset,
613f36e1793SJack Morgenstein hdr_len + data_len - copy_offset)) {
614cb183a06SHal Rosenstock ret = -EFAULT;
615cb183a06SHal Rosenstock goto err_msg;
616cb183a06SHal Rosenstock }
617f36e1793SJack Morgenstein } else {
618f36e1793SJack Morgenstein ret = copy_rmpp_mad(packet->msg, buf);
619f36e1793SJack Morgenstein if (ret)
620f36e1793SJack Morgenstein goto err_msg;
621f36e1793SJack Morgenstein }
622cb183a06SHal Rosenstock
623cb183a06SHal Rosenstock /*
6242527e681SSean Hefty * Set the high-order part of the transaction ID to make MADs from
6252527e681SSean Hefty * different agents unique, and allow routing responses back to the
6262527e681SSean Hefty * original requestor.
627cb183a06SHal Rosenstock */
6282527e681SSean Hefty if (!ib_response_mad(packet->msg->mad)) {
629089a1bedSRoland Dreier tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
630cb183a06SHal Rosenstock *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
631cb183a06SHal Rosenstock (be64_to_cpup(tid) & 0xffffffff));
632aa4d540bSGustavo A. R. Silva rmpp_mad_hdr->mad_hdr.tid = *tid;
6332527e681SSean Hefty }
6342527e681SSean Hefty
6351471cb6cSIra Weiny if (!ib_mad_kernel_rmpp_agent(agent)
636aa4d540bSGustavo A. R. Silva && ib_is_mad_class_rmpp(rmpp_mad_hdr->mad_hdr.mgmt_class)
637aa4d540bSGustavo A. R. Silva && (ib_get_rmpp_flags(&rmpp_mad_hdr->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE)) {
6381471cb6cSIra Weiny spin_lock_irq(&file->send_lock);
6391471cb6cSIra Weiny list_add_tail(&packet->list, &file->send_list);
6401471cb6cSIra Weiny spin_unlock_irq(&file->send_lock);
6411471cb6cSIra Weiny } else {
6422527e681SSean Hefty spin_lock_irq(&file->send_lock);
6432527e681SSean Hefty ret = is_duplicate(file, packet);
6442527e681SSean Hefty if (!ret)
6452527e681SSean Hefty list_add_tail(&packet->list, &file->send_list);
6462527e681SSean Hefty spin_unlock_irq(&file->send_lock);
6472527e681SSean Hefty if (ret) {
6482527e681SSean Hefty ret = -EINVAL;
6492527e681SSean Hefty goto err_msg;
650cb183a06SHal Rosenstock }
6511471cb6cSIra Weiny }
652cb183a06SHal Rosenstock
65334816ad9SSean Hefty ret = ib_post_send_mad(packet->msg, NULL);
654cb183a06SHal Rosenstock if (ret)
6552527e681SSean Hefty goto err_send;
6561da177e4SLinus Torvalds
6572fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
658cb0f0910SSean Hefty return count;
659cb183a06SHal Rosenstock
6602527e681SSean Hefty err_send:
6612527e681SSean Hefty dequeue_send(file, packet);
662cb183a06SHal Rosenstock err_msg:
663cb183a06SHal Rosenstock ib_free_send_mad(packet->msg);
664cb183a06SHal Rosenstock err_ah:
6652553ba21SGal Pressman rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
6661da177e4SLinus Torvalds err_up:
6672fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
6681da177e4SLinus Torvalds err:
6691da177e4SLinus Torvalds kfree(packet);
6701da177e4SLinus Torvalds return ret;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds
ib_umad_poll(struct file * filp,struct poll_table_struct * wait)673afc9a42bSAl Viro static __poll_t ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds struct ib_umad_file *file = filp->private_data;
6761da177e4SLinus Torvalds
6771da177e4SLinus Torvalds /* we will always be able to post a MAD send */
678a9a08845SLinus Torvalds __poll_t mask = EPOLLOUT | EPOLLWRNORM;
6791da177e4SLinus Torvalds
680def4cd43SShay Drory mutex_lock(&file->mutex);
6811da177e4SLinus Torvalds poll_wait(filp, &file->recv_wait, wait);
6821da177e4SLinus Torvalds
6831da177e4SLinus Torvalds if (!list_empty(&file->recv_list))
684a9a08845SLinus Torvalds mask |= EPOLLIN | EPOLLRDNORM;
685def4cd43SShay Drory if (file->agents_dead)
686def4cd43SShay Drory mask = EPOLLERR;
687def4cd43SShay Drory mutex_unlock(&file->mutex);
6881da177e4SLinus Torvalds
6891da177e4SLinus Torvalds return mask;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds
ib_umad_reg_agent(struct ib_umad_file * file,void __user * arg,int compat_method_mask)692a394f83bSRoland Dreier static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
693a394f83bSRoland Dreier int compat_method_mask)
6941da177e4SLinus Torvalds {
6951da177e4SLinus Torvalds struct ib_user_mad_reg_req ureq;
6961da177e4SLinus Torvalds struct ib_mad_reg_req req;
6972fe7e6f7SRoland Dreier struct ib_mad_agent *agent = NULL;
6981da177e4SLinus Torvalds int agent_id;
6991da177e4SLinus Torvalds int ret;
7001da177e4SLinus Torvalds
7012fe7e6f7SRoland Dreier mutex_lock(&file->port->file_mutex);
7022fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
7030c99cb6dSRoland Dreier
7040c99cb6dSRoland Dreier if (!file->port->ib_dev) {
705ab27f45fSWenpeng Liang dev_notice(&file->port->dev, "%s: invalid device\n", __func__);
7060c99cb6dSRoland Dreier ret = -EPIPE;
7070c99cb6dSRoland Dreier goto out;
7080c99cb6dSRoland Dreier }
7091da177e4SLinus Torvalds
710a394f83bSRoland Dreier if (copy_from_user(&ureq, arg, sizeof ureq)) {
7111da177e4SLinus Torvalds ret = -EFAULT;
7121da177e4SLinus Torvalds goto out;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds
7151da177e4SLinus Torvalds if (ureq.qpn != 0 && ureq.qpn != 1) {
716e9dd5dafSParav Pandit dev_notice(&file->port->dev,
7173cea7b4aSWenpeng Liang "%s: invalid QPN %u specified\n", __func__,
7189ad13a42SIra Weiny ureq.qpn);
7191da177e4SLinus Torvalds ret = -EINVAL;
7201da177e4SLinus Torvalds goto out;
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds
7231da177e4SLinus Torvalds for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
72494382f35SRoland Dreier if (!__get_agent(file, agent_id))
7251da177e4SLinus Torvalds goto found;
7261da177e4SLinus Torvalds
727ab27f45fSWenpeng Liang dev_notice(&file->port->dev, "%s: Max Agents (%u) reached\n", __func__,
7289ad13a42SIra Weiny IB_UMAD_MAX_AGENTS);
729ab27f45fSWenpeng Liang
7301da177e4SLinus Torvalds ret = -ENOMEM;
7311da177e4SLinus Torvalds goto out;
7321da177e4SLinus Torvalds
7331da177e4SLinus Torvalds found:
7340df3bb13SRoland Dreier if (ureq.mgmt_class) {
7350f29b46dSIra Weiny memset(&req, 0, sizeof(req));
7361da177e4SLinus Torvalds req.mgmt_class = ureq.mgmt_class;
7371da177e4SLinus Torvalds req.mgmt_class_version = ureq.mgmt_class_version;
7381da177e4SLinus Torvalds memcpy(req.oui, ureq.oui, sizeof req.oui);
739a394f83bSRoland Dreier
740a394f83bSRoland Dreier if (compat_method_mask) {
741a394f83bSRoland Dreier u32 *umm = (u32 *) ureq.method_mask;
742a394f83bSRoland Dreier int i;
743a394f83bSRoland Dreier
744a394f83bSRoland Dreier for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
745a394f83bSRoland Dreier req.method_mask[i] =
746a394f83bSRoland Dreier umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
747a394f83bSRoland Dreier } else
748a394f83bSRoland Dreier memcpy(req.method_mask, ureq.method_mask,
749a394f83bSRoland Dreier sizeof req.method_mask);
7500df3bb13SRoland Dreier }
7511da177e4SLinus Torvalds
7521da177e4SLinus Torvalds agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
7531da177e4SLinus Torvalds ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
7540df3bb13SRoland Dreier ureq.mgmt_class ? &req : NULL,
755cb183a06SHal Rosenstock ureq.rmpp_version,
7560f29b46dSIra Weiny send_handler, recv_handler, file, 0);
7571da177e4SLinus Torvalds if (IS_ERR(agent)) {
7581da177e4SLinus Torvalds ret = PTR_ERR(agent);
7592fe7e6f7SRoland Dreier agent = NULL;
7601da177e4SLinus Torvalds goto out;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds
7631da177e4SLinus Torvalds if (put_user(agent_id,
7641da177e4SLinus Torvalds (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
7651da177e4SLinus Torvalds ret = -EFAULT;
766ec914c52SRoland Dreier goto out;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds
7692be8e3eeSRoland Dreier if (!file->already_used) {
7702be8e3eeSRoland Dreier file->already_used = 1;
7712be8e3eeSRoland Dreier if (!file->use_pkey_index) {
772e9dd5dafSParav Pandit dev_warn(&file->port->dev,
773f426a40eSIra Weiny "process %s did not enable P_Key index support.\n",
774f426a40eSIra Weiny current->comm);
775e9dd5dafSParav Pandit dev_warn(&file->port->dev,
77697162a1eSMauro Carvalho Chehab " Documentation/infiniband/user_mad.rst has info on the new ABI.\n");
7772be8e3eeSRoland Dreier }
7782be8e3eeSRoland Dreier }
7792be8e3eeSRoland Dreier
7802f76e829SRoland Dreier file->agent[agent_id] = agent;
7811da177e4SLinus Torvalds ret = 0;
7822f76e829SRoland Dreier
7831da177e4SLinus Torvalds out:
7842fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
7852fe7e6f7SRoland Dreier
7862fe7e6f7SRoland Dreier if (ret && agent)
7872fe7e6f7SRoland Dreier ib_unregister_mad_agent(agent);
7882fe7e6f7SRoland Dreier
7892fe7e6f7SRoland Dreier mutex_unlock(&file->port->file_mutex);
7902fe7e6f7SRoland Dreier
7911da177e4SLinus Torvalds return ret;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds
ib_umad_reg_agent2(struct ib_umad_file * file,void __user * arg)7940f29b46dSIra Weiny static int ib_umad_reg_agent2(struct ib_umad_file *file, void __user *arg)
7950f29b46dSIra Weiny {
7960f29b46dSIra Weiny struct ib_user_mad_reg_req2 ureq;
7970f29b46dSIra Weiny struct ib_mad_reg_req req;
7980f29b46dSIra Weiny struct ib_mad_agent *agent = NULL;
7990f29b46dSIra Weiny int agent_id;
8000f29b46dSIra Weiny int ret;
8010f29b46dSIra Weiny
8020f29b46dSIra Weiny mutex_lock(&file->port->file_mutex);
8030f29b46dSIra Weiny mutex_lock(&file->mutex);
8040f29b46dSIra Weiny
8050f29b46dSIra Weiny if (!file->port->ib_dev) {
806ab27f45fSWenpeng Liang dev_notice(&file->port->dev, "%s: invalid device\n", __func__);
8070f29b46dSIra Weiny ret = -EPIPE;
8080f29b46dSIra Weiny goto out;
8090f29b46dSIra Weiny }
8100f29b46dSIra Weiny
8110f29b46dSIra Weiny if (copy_from_user(&ureq, arg, sizeof(ureq))) {
8120f29b46dSIra Weiny ret = -EFAULT;
8130f29b46dSIra Weiny goto out;
8140f29b46dSIra Weiny }
8150f29b46dSIra Weiny
8160f29b46dSIra Weiny if (ureq.qpn != 0 && ureq.qpn != 1) {
8173cea7b4aSWenpeng Liang dev_notice(&file->port->dev, "%s: invalid QPN %u specified\n",
818ab27f45fSWenpeng Liang __func__, ureq.qpn);
8190f29b46dSIra Weiny ret = -EINVAL;
8200f29b46dSIra Weiny goto out;
8210f29b46dSIra Weiny }
8220f29b46dSIra Weiny
8230f29b46dSIra Weiny if (ureq.flags & ~IB_USER_MAD_REG_FLAGS_CAP) {
824e9dd5dafSParav Pandit dev_notice(&file->port->dev,
825ab27f45fSWenpeng Liang "%s failed: invalid registration flags specified 0x%x; supported 0x%x\n",
826ab27f45fSWenpeng Liang __func__, ureq.flags, IB_USER_MAD_REG_FLAGS_CAP);
8270f29b46dSIra Weiny ret = -EINVAL;
8280f29b46dSIra Weiny
8290f29b46dSIra Weiny if (put_user((u32)IB_USER_MAD_REG_FLAGS_CAP,
8300f29b46dSIra Weiny (u32 __user *) (arg + offsetof(struct
8310f29b46dSIra Weiny ib_user_mad_reg_req2, flags))))
8320f29b46dSIra Weiny ret = -EFAULT;
8330f29b46dSIra Weiny
8340f29b46dSIra Weiny goto out;
8350f29b46dSIra Weiny }
8360f29b46dSIra Weiny
8370f29b46dSIra Weiny for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
8380f29b46dSIra Weiny if (!__get_agent(file, agent_id))
8390f29b46dSIra Weiny goto found;
8400f29b46dSIra Weiny
841ab27f45fSWenpeng Liang dev_notice(&file->port->dev, "%s: Max Agents (%u) reached\n", __func__,
8420f29b46dSIra Weiny IB_UMAD_MAX_AGENTS);
8430f29b46dSIra Weiny ret = -ENOMEM;
8440f29b46dSIra Weiny goto out;
8450f29b46dSIra Weiny
8460f29b46dSIra Weiny found:
8470f29b46dSIra Weiny if (ureq.mgmt_class) {
8480f29b46dSIra Weiny memset(&req, 0, sizeof(req));
8490f29b46dSIra Weiny req.mgmt_class = ureq.mgmt_class;
8500f29b46dSIra Weiny req.mgmt_class_version = ureq.mgmt_class_version;
8510f29b46dSIra Weiny if (ureq.oui & 0xff000000) {
852e9dd5dafSParav Pandit dev_notice(&file->port->dev,
853ab27f45fSWenpeng Liang "%s failed: oui invalid 0x%08x\n", __func__,
8540f29b46dSIra Weiny ureq.oui);
8550f29b46dSIra Weiny ret = -EINVAL;
8560f29b46dSIra Weiny goto out;
8570f29b46dSIra Weiny }
8580f29b46dSIra Weiny req.oui[2] = ureq.oui & 0x0000ff;
8590f29b46dSIra Weiny req.oui[1] = (ureq.oui & 0x00ff00) >> 8;
8600f29b46dSIra Weiny req.oui[0] = (ureq.oui & 0xff0000) >> 16;
8610f29b46dSIra Weiny memcpy(req.method_mask, ureq.method_mask,
8620f29b46dSIra Weiny sizeof(req.method_mask));
8630f29b46dSIra Weiny }
8640f29b46dSIra Weiny
8650f29b46dSIra Weiny agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
8660f29b46dSIra Weiny ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
8670f29b46dSIra Weiny ureq.mgmt_class ? &req : NULL,
8680f29b46dSIra Weiny ureq.rmpp_version,
8690f29b46dSIra Weiny send_handler, recv_handler, file,
8700f29b46dSIra Weiny ureq.flags);
8710f29b46dSIra Weiny if (IS_ERR(agent)) {
8720f29b46dSIra Weiny ret = PTR_ERR(agent);
8730f29b46dSIra Weiny agent = NULL;
8740f29b46dSIra Weiny goto out;
8750f29b46dSIra Weiny }
8760f29b46dSIra Weiny
8770f29b46dSIra Weiny if (put_user(agent_id,
8780f29b46dSIra Weiny (u32 __user *)(arg +
8790f29b46dSIra Weiny offsetof(struct ib_user_mad_reg_req2, id)))) {
8800f29b46dSIra Weiny ret = -EFAULT;
8810f29b46dSIra Weiny goto out;
8820f29b46dSIra Weiny }
8830f29b46dSIra Weiny
8840f29b46dSIra Weiny if (!file->already_used) {
8850f29b46dSIra Weiny file->already_used = 1;
8860f29b46dSIra Weiny file->use_pkey_index = 1;
8870f29b46dSIra Weiny }
8880f29b46dSIra Weiny
8890f29b46dSIra Weiny file->agent[agent_id] = agent;
8900f29b46dSIra Weiny ret = 0;
8910f29b46dSIra Weiny
8920f29b46dSIra Weiny out:
8930f29b46dSIra Weiny mutex_unlock(&file->mutex);
8940f29b46dSIra Weiny
8950f29b46dSIra Weiny if (ret && agent)
8960f29b46dSIra Weiny ib_unregister_mad_agent(agent);
8970f29b46dSIra Weiny
8980f29b46dSIra Weiny mutex_unlock(&file->port->file_mutex);
8990f29b46dSIra Weiny
9000f29b46dSIra Weiny return ret;
9010f29b46dSIra Weiny }
9020f29b46dSIra Weiny
9030f29b46dSIra Weiny
ib_umad_unreg_agent(struct ib_umad_file * file,u32 __user * arg)904a394f83bSRoland Dreier static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
9051da177e4SLinus Torvalds {
9062f76e829SRoland Dreier struct ib_mad_agent *agent = NULL;
9071da177e4SLinus Torvalds u32 id;
9081da177e4SLinus Torvalds int ret = 0;
9091da177e4SLinus Torvalds
910a394f83bSRoland Dreier if (get_user(id, arg))
9112f76e829SRoland Dreier return -EFAULT;
91261f25982SLuck, Tony if (id >= IB_UMAD_MAX_AGENTS)
91361f25982SLuck, Tony return -EINVAL;
9141da177e4SLinus Torvalds
9152fe7e6f7SRoland Dreier mutex_lock(&file->port->file_mutex);
9162fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
9171da177e4SLinus Torvalds
91861f25982SLuck, Tony id = array_index_nospec(id, IB_UMAD_MAX_AGENTS);
91961f25982SLuck, Tony if (!__get_agent(file, id)) {
9201da177e4SLinus Torvalds ret = -EINVAL;
9211da177e4SLinus Torvalds goto out;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds
9242f76e829SRoland Dreier agent = file->agent[id];
9251da177e4SLinus Torvalds file->agent[id] = NULL;
9261da177e4SLinus Torvalds
9271da177e4SLinus Torvalds out:
9282fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
9292f76e829SRoland Dreier
930ec914c52SRoland Dreier if (agent)
9312f76e829SRoland Dreier ib_unregister_mad_agent(agent);
9322f76e829SRoland Dreier
9332fe7e6f7SRoland Dreier mutex_unlock(&file->port->file_mutex);
9342fe7e6f7SRoland Dreier
9351da177e4SLinus Torvalds return ret;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds
ib_umad_enable_pkey(struct ib_umad_file * file)9382be8e3eeSRoland Dreier static long ib_umad_enable_pkey(struct ib_umad_file *file)
9392be8e3eeSRoland Dreier {
9402be8e3eeSRoland Dreier int ret = 0;
9412be8e3eeSRoland Dreier
9422fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
9432be8e3eeSRoland Dreier if (file->already_used)
9442be8e3eeSRoland Dreier ret = -EINVAL;
9452be8e3eeSRoland Dreier else
9462be8e3eeSRoland Dreier file->use_pkey_index = 1;
9472fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
9482be8e3eeSRoland Dreier
9492be8e3eeSRoland Dreier return ret;
9502be8e3eeSRoland Dreier }
9512be8e3eeSRoland Dreier
ib_umad_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)952cb183a06SHal Rosenstock static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
953cb183a06SHal Rosenstock unsigned long arg)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds switch (cmd) {
9561da177e4SLinus Torvalds case IB_USER_MAD_REGISTER_AGENT:
957a394f83bSRoland Dreier return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
9581da177e4SLinus Torvalds case IB_USER_MAD_UNREGISTER_AGENT:
959a394f83bSRoland Dreier return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
9602be8e3eeSRoland Dreier case IB_USER_MAD_ENABLE_PKEY:
9612be8e3eeSRoland Dreier return ib_umad_enable_pkey(filp->private_data);
9620f29b46dSIra Weiny case IB_USER_MAD_REGISTER_AGENT2:
9630f29b46dSIra Weiny return ib_umad_reg_agent2(filp->private_data, (void __user *) arg);
9641da177e4SLinus Torvalds default:
9651da177e4SLinus Torvalds return -ENOIOCTLCMD;
9661da177e4SLinus Torvalds }
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds
969a394f83bSRoland Dreier #ifdef CONFIG_COMPAT
ib_umad_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)970a394f83bSRoland Dreier static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
971a394f83bSRoland Dreier unsigned long arg)
972a394f83bSRoland Dreier {
973a394f83bSRoland Dreier switch (cmd) {
974a394f83bSRoland Dreier case IB_USER_MAD_REGISTER_AGENT:
975a394f83bSRoland Dreier return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
976a394f83bSRoland Dreier case IB_USER_MAD_UNREGISTER_AGENT:
977a394f83bSRoland Dreier return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
978a394f83bSRoland Dreier case IB_USER_MAD_ENABLE_PKEY:
979a394f83bSRoland Dreier return ib_umad_enable_pkey(filp->private_data);
9800f29b46dSIra Weiny case IB_USER_MAD_REGISTER_AGENT2:
9810f29b46dSIra Weiny return ib_umad_reg_agent2(filp->private_data, compat_ptr(arg));
982a394f83bSRoland Dreier default:
983a394f83bSRoland Dreier return -ENOIOCTLCMD;
984a394f83bSRoland Dreier }
985a394f83bSRoland Dreier }
986a394f83bSRoland Dreier #endif
987a394f83bSRoland Dreier
988feae1ef1SRoland Dreier /*
989feae1ef1SRoland Dreier * ib_umad_open() does not need the BKL:
990feae1ef1SRoland Dreier *
9916aa2a86eSAlexander Chiang * - the ib_umad_port structures are properly reference counted, and
992feae1ef1SRoland Dreier * everything else is purely local to the file being created, so
993feae1ef1SRoland Dreier * races against other open calls are not a problem;
994feae1ef1SRoland Dreier * - the ioctl method does not affect any global state outside of the
995feae1ef1SRoland Dreier * file structure being operated on;
996feae1ef1SRoland Dreier */
ib_umad_open(struct inode * inode,struct file * filp)9971da177e4SLinus Torvalds static int ib_umad_open(struct inode *inode, struct file *filp)
9981da177e4SLinus Torvalds {
999a74968f8SRoland Dreier struct ib_umad_port *port;
10001da177e4SLinus Torvalds struct ib_umad_file *file;
1001039d713aSParav Pandit int ret = 0;
10021da177e4SLinus Torvalds
10036aa2a86eSAlexander Chiang port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
1004a74968f8SRoland Dreier
10052fe7e6f7SRoland Dreier mutex_lock(&port->file_mutex);
10060c99cb6dSRoland Dreier
1007039d713aSParav Pandit if (!port->ib_dev) {
1008039d713aSParav Pandit ret = -ENXIO;
10090c99cb6dSRoland Dreier goto out;
1010039d713aSParav Pandit }
10110c99cb6dSRoland Dreier
101241c61401SParav Pandit if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
101341c61401SParav Pandit ret = -EPERM;
101441c61401SParav Pandit goto out;
101541c61401SParav Pandit }
101641c61401SParav Pandit
1017039d713aSParav Pandit file = kzalloc(sizeof(*file), GFP_KERNEL);
1018039d713aSParav Pandit if (!file) {
10190c99cb6dSRoland Dreier ret = -ENOMEM;
10200c99cb6dSRoland Dreier goto out;
1021039d713aSParav Pandit }
10221da177e4SLinus Torvalds
10232fe7e6f7SRoland Dreier mutex_init(&file->mutex);
10242527e681SSean Hefty spin_lock_init(&file->send_lock);
10251da177e4SLinus Torvalds INIT_LIST_HEAD(&file->recv_list);
10262527e681SSean Hefty INIT_LIST_HEAD(&file->send_list);
10271da177e4SLinus Torvalds init_waitqueue_head(&file->recv_wait);
10281da177e4SLinus Torvalds
10291da177e4SLinus Torvalds file->port = port;
10301da177e4SLinus Torvalds filp->private_data = file;
10311da177e4SLinus Torvalds
10320c99cb6dSRoland Dreier list_add_tail(&file->port_list, &port->file_list);
10330c99cb6dSRoland Dreier
1034c5bf68feSKirill Smelkov stream_open(inode, filp);
10350c99cb6dSRoland Dreier out:
10362fe7e6f7SRoland Dreier mutex_unlock(&port->file_mutex);
10370c99cb6dSRoland Dreier return ret;
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds
ib_umad_close(struct inode * inode,struct file * filp)10401da177e4SLinus Torvalds static int ib_umad_close(struct inode *inode, struct file *filp)
10411da177e4SLinus Torvalds {
10421da177e4SLinus Torvalds struct ib_umad_file *file = filp->private_data;
1043561e148eSRoland Dreier struct ib_umad_packet *packet, *tmp;
104494382f35SRoland Dreier int already_dead;
10451da177e4SLinus Torvalds int i;
10461da177e4SLinus Torvalds
10472fe7e6f7SRoland Dreier mutex_lock(&file->port->file_mutex);
10482fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
104994382f35SRoland Dreier
105094382f35SRoland Dreier already_dead = file->agents_dead;
105194382f35SRoland Dreier file->agents_dead = 1;
10521da177e4SLinus Torvalds
1053f36e1793SJack Morgenstein list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
1054f36e1793SJack Morgenstein if (packet->recv_wc)
1055f36e1793SJack Morgenstein ib_free_recv_mad(packet->recv_wc);
1056561e148eSRoland Dreier kfree(packet);
1057f36e1793SJack Morgenstein }
1058561e148eSRoland Dreier
10590c99cb6dSRoland Dreier list_del(&file->port_list);
106094382f35SRoland Dreier
10612fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
106294382f35SRoland Dreier
106394382f35SRoland Dreier if (!already_dead)
106494382f35SRoland Dreier for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
106594382f35SRoland Dreier if (file->agent[i])
106694382f35SRoland Dreier ib_unregister_mad_agent(file->agent[i]);
106794382f35SRoland Dreier
10682fe7e6f7SRoland Dreier mutex_unlock(&file->port->file_mutex);
106956594ae1SParav Pandit mutex_destroy(&file->mutex);
10701da177e4SLinus Torvalds kfree(file);
10711da177e4SLinus Torvalds return 0;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds
10742b8693c0SArjan van de Ven static const struct file_operations umad_fops = {
10751da177e4SLinus Torvalds .owner = THIS_MODULE,
10761da177e4SLinus Torvalds .read = ib_umad_read,
10771da177e4SLinus Torvalds .write = ib_umad_write,
10781da177e4SLinus Torvalds .poll = ib_umad_poll,
10791da177e4SLinus Torvalds .unlocked_ioctl = ib_umad_ioctl,
1080a394f83bSRoland Dreier #ifdef CONFIG_COMPAT
1081a394f83bSRoland Dreier .compat_ioctl = ib_umad_compat_ioctl,
1082a394f83bSRoland Dreier #endif
10831da177e4SLinus Torvalds .open = ib_umad_open,
1084bc1db9afSRoland Dreier .release = ib_umad_close,
1085bc1db9afSRoland Dreier .llseek = no_llseek,
10861da177e4SLinus Torvalds };
10871da177e4SLinus Torvalds
ib_umad_sm_open(struct inode * inode,struct file * filp)10881da177e4SLinus Torvalds static int ib_umad_sm_open(struct inode *inode, struct file *filp)
10891da177e4SLinus Torvalds {
1090a74968f8SRoland Dreier struct ib_umad_port *port;
10911da177e4SLinus Torvalds struct ib_port_modify props = {
10921da177e4SLinus Torvalds .set_port_cap_mask = IB_PORT_SM
10931da177e4SLinus Torvalds };
10941da177e4SLinus Torvalds int ret;
10951da177e4SLinus Torvalds
10966aa2a86eSAlexander Chiang port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
1097a74968f8SRoland Dreier
10981da177e4SLinus Torvalds if (filp->f_flags & O_NONBLOCK) {
1099a74968f8SRoland Dreier if (down_trylock(&port->sm_sem)) {
1100a74968f8SRoland Dreier ret = -EAGAIN;
1101a74968f8SRoland Dreier goto fail;
1102a74968f8SRoland Dreier }
11031da177e4SLinus Torvalds } else {
1104a74968f8SRoland Dreier if (down_interruptible(&port->sm_sem)) {
1105a74968f8SRoland Dreier ret = -ERESTARTSYS;
1106a74968f8SRoland Dreier goto fail;
1107a74968f8SRoland Dreier }
11081da177e4SLinus Torvalds }
11091da177e4SLinus Torvalds
111041c61401SParav Pandit if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
111141c61401SParav Pandit ret = -EPERM;
111241c61401SParav Pandit goto err_up_sem;
111341c61401SParav Pandit }
111441c61401SParav Pandit
11151da177e4SLinus Torvalds ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
11168ec0a0e6SBart Van Assche if (ret)
11178ec0a0e6SBart Van Assche goto err_up_sem;
11181da177e4SLinus Torvalds
11191da177e4SLinus Torvalds filp->private_data = port;
11201da177e4SLinus Torvalds
1121039d713aSParav Pandit nonseekable_open(inode, filp);
11228ec0a0e6SBart Van Assche return 0;
11238ec0a0e6SBart Van Assche
11248ec0a0e6SBart Van Assche err_up_sem:
11258ec0a0e6SBart Van Assche up(&port->sm_sem);
1126a74968f8SRoland Dreier
1127a74968f8SRoland Dreier fail:
1128a74968f8SRoland Dreier return ret;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds
ib_umad_sm_close(struct inode * inode,struct file * filp)11311da177e4SLinus Torvalds static int ib_umad_sm_close(struct inode *inode, struct file *filp)
11321da177e4SLinus Torvalds {
11331da177e4SLinus Torvalds struct ib_umad_port *port = filp->private_data;
11341da177e4SLinus Torvalds struct ib_port_modify props = {
11351da177e4SLinus Torvalds .clr_port_cap_mask = IB_PORT_SM
11361da177e4SLinus Torvalds };
11370c99cb6dSRoland Dreier int ret = 0;
11381da177e4SLinus Torvalds
11392fe7e6f7SRoland Dreier mutex_lock(&port->file_mutex);
11400c99cb6dSRoland Dreier if (port->ib_dev)
11411da177e4SLinus Torvalds ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
11422fe7e6f7SRoland Dreier mutex_unlock(&port->file_mutex);
11430c99cb6dSRoland Dreier
11441da177e4SLinus Torvalds up(&port->sm_sem);
11451da177e4SLinus Torvalds
11461da177e4SLinus Torvalds return ret;
11471da177e4SLinus Torvalds }
11481da177e4SLinus Torvalds
11492b8693c0SArjan van de Ven static const struct file_operations umad_sm_fops = {
11501da177e4SLinus Torvalds .owner = THIS_MODULE,
11511da177e4SLinus Torvalds .open = ib_umad_sm_open,
1152bc1db9afSRoland Dreier .release = ib_umad_sm_close,
1153bc1db9afSRoland Dreier .llseek = no_llseek,
11541da177e4SLinus Torvalds };
11551da177e4SLinus Torvalds
get_port(struct ib_device * ibdev,struct ib_umad_device * umad_dev,u32 port)11565bdfa854SJason Gunthorpe static struct ib_umad_port *get_port(struct ib_device *ibdev,
11575bdfa854SJason Gunthorpe struct ib_umad_device *umad_dev,
11581fb7f897SMark Bloch u32 port)
11595bdfa854SJason Gunthorpe {
11605bdfa854SJason Gunthorpe if (!umad_dev)
11615bdfa854SJason Gunthorpe return ERR_PTR(-EOPNOTSUPP);
11625bdfa854SJason Gunthorpe if (!rdma_is_port_valid(ibdev, port))
11635bdfa854SJason Gunthorpe return ERR_PTR(-EINVAL);
11645bdfa854SJason Gunthorpe if (!rdma_cap_ib_mad(ibdev, port))
11655bdfa854SJason Gunthorpe return ERR_PTR(-EOPNOTSUPP);
11665bdfa854SJason Gunthorpe
11675bdfa854SJason Gunthorpe return &umad_dev->ports[port - rdma_start_port(ibdev)];
11685bdfa854SJason Gunthorpe }
11695bdfa854SJason Gunthorpe
ib_umad_get_nl_info(struct ib_device * ibdev,void * client_data,struct ib_client_nl_info * res)11708f71bb00SJason Gunthorpe static int ib_umad_get_nl_info(struct ib_device *ibdev, void *client_data,
11718f71bb00SJason Gunthorpe struct ib_client_nl_info *res)
11728f71bb00SJason Gunthorpe {
11735bdfa854SJason Gunthorpe struct ib_umad_port *port = get_port(ibdev, client_data, res->port);
11748f71bb00SJason Gunthorpe
11755bdfa854SJason Gunthorpe if (IS_ERR(port))
11765bdfa854SJason Gunthorpe return PTR_ERR(port);
11778f71bb00SJason Gunthorpe
11788f71bb00SJason Gunthorpe res->abi = IB_USER_MAD_ABI_VERSION;
11795bdfa854SJason Gunthorpe res->cdev = &port->dev;
11808f71bb00SJason Gunthorpe return 0;
11818f71bb00SJason Gunthorpe }
11828f71bb00SJason Gunthorpe
11831da177e4SLinus Torvalds static struct ib_client umad_client = {
11841da177e4SLinus Torvalds .name = "umad",
11851da177e4SLinus Torvalds .add = ib_umad_add_one,
11868f71bb00SJason Gunthorpe .remove = ib_umad_remove_one,
11878f71bb00SJason Gunthorpe .get_nl_info = ib_umad_get_nl_info,
11881da177e4SLinus Torvalds };
11898f71bb00SJason Gunthorpe MODULE_ALIAS_RDMA_CLIENT("umad");
11908f71bb00SJason Gunthorpe
ib_issm_get_nl_info(struct ib_device * ibdev,void * client_data,struct ib_client_nl_info * res)11918f71bb00SJason Gunthorpe static int ib_issm_get_nl_info(struct ib_device *ibdev, void *client_data,
11928f71bb00SJason Gunthorpe struct ib_client_nl_info *res)
11938f71bb00SJason Gunthorpe {
11945bdfa854SJason Gunthorpe struct ib_umad_port *port = get_port(ibdev, client_data, res->port);
11958f71bb00SJason Gunthorpe
11965bdfa854SJason Gunthorpe if (IS_ERR(port))
11975bdfa854SJason Gunthorpe return PTR_ERR(port);
11988f71bb00SJason Gunthorpe
11998f71bb00SJason Gunthorpe res->abi = IB_USER_MAD_ABI_VERSION;
12005bdfa854SJason Gunthorpe res->cdev = &port->sm_dev;
12018f71bb00SJason Gunthorpe return 0;
12028f71bb00SJason Gunthorpe }
12038f71bb00SJason Gunthorpe
12048f71bb00SJason Gunthorpe static struct ib_client issm_client = {
12058f71bb00SJason Gunthorpe .name = "issm",
12068f71bb00SJason Gunthorpe .get_nl_info = ib_issm_get_nl_info,
12078f71bb00SJason Gunthorpe };
12088f71bb00SJason Gunthorpe MODULE_ALIAS_RDMA_CLIENT("issm");
12091da177e4SLinus Torvalds
ibdev_show(struct device * dev,struct device_attribute * attr,char * buf)121075bf8a2aSParav Pandit static ssize_t ibdev_show(struct device *dev, struct device_attribute *attr,
1211f4e91eb4STony Jones char *buf)
12121da177e4SLinus Torvalds {
1213f4e91eb4STony Jones struct ib_umad_port *port = dev_get_drvdata(dev);
12141da177e4SLinus Torvalds
1215a74968f8SRoland Dreier if (!port)
1216a74968f8SRoland Dreier return -ENODEV;
1217a74968f8SRoland Dreier
12181c7fd726SJoe Perches return sysfs_emit(buf, "%s\n", dev_name(&port->ib_dev->dev));
12191da177e4SLinus Torvalds }
122075bf8a2aSParav Pandit static DEVICE_ATTR_RO(ibdev);
12211da177e4SLinus Torvalds
port_show(struct device * dev,struct device_attribute * attr,char * buf)122275bf8a2aSParav Pandit static ssize_t port_show(struct device *dev, struct device_attribute *attr,
1223f4e91eb4STony Jones char *buf)
12241da177e4SLinus Torvalds {
1225f4e91eb4STony Jones struct ib_umad_port *port = dev_get_drvdata(dev);
12261da177e4SLinus Torvalds
1227a74968f8SRoland Dreier if (!port)
1228a74968f8SRoland Dreier return -ENODEV;
1229a74968f8SRoland Dreier
12301c7fd726SJoe Perches return sysfs_emit(buf, "%d\n", port->port_num);
12311da177e4SLinus Torvalds }
123275bf8a2aSParav Pandit static DEVICE_ATTR_RO(port);
123375bf8a2aSParav Pandit
123475bf8a2aSParav Pandit static struct attribute *umad_class_dev_attrs[] = {
123575bf8a2aSParav Pandit &dev_attr_ibdev.attr,
123675bf8a2aSParav Pandit &dev_attr_port.attr,
123775bf8a2aSParav Pandit NULL,
123875bf8a2aSParav Pandit };
123975bf8a2aSParav Pandit ATTRIBUTE_GROUPS(umad_class_dev);
12401da177e4SLinus Torvalds
umad_devnode(const struct device * dev,umode_t * mode)1241ff62b8e6SGreg Kroah-Hartman static char *umad_devnode(const struct device *dev, umode_t *mode)
1242900d07c1SParav Pandit {
1243900d07c1SParav Pandit return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
1244900d07c1SParav Pandit }
1245900d07c1SParav Pandit
abi_version_show(const struct class * class,const struct class_attribute * attr,char * buf)124675a2d422SGreg Kroah-Hartman static ssize_t abi_version_show(const struct class *class,
124775a2d422SGreg Kroah-Hartman const struct class_attribute *attr, char *buf)
1248cdb53b65SParav Pandit {
1249e28bf1f0SJoe Perches return sysfs_emit(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
1250cdb53b65SParav Pandit }
1251cdb53b65SParav Pandit static CLASS_ATTR_RO(abi_version);
1252cdb53b65SParav Pandit
1253cdb53b65SParav Pandit static struct attribute *umad_class_attrs[] = {
1254cdb53b65SParav Pandit &class_attr_abi_version.attr,
1255cdb53b65SParav Pandit NULL,
1256cdb53b65SParav Pandit };
1257cdb53b65SParav Pandit ATTRIBUTE_GROUPS(umad_class);
1258cdb53b65SParav Pandit
1259900d07c1SParav Pandit static struct class umad_class = {
1260900d07c1SParav Pandit .name = "infiniband_mad",
1261900d07c1SParav Pandit .devnode = umad_devnode,
1262cdb53b65SParav Pandit .class_groups = umad_class_groups,
126375bf8a2aSParav Pandit .dev_groups = umad_class_dev_groups,
1264900d07c1SParav Pandit };
1265900d07c1SParav Pandit
ib_umad_release_port(struct device * device)1266e9dd5dafSParav Pandit static void ib_umad_release_port(struct device *device)
1267e9dd5dafSParav Pandit {
1268e9dd5dafSParav Pandit struct ib_umad_port *port = dev_get_drvdata(device);
1269e9dd5dafSParav Pandit struct ib_umad_device *umad_dev = port->umad_dev;
1270e9dd5dafSParav Pandit
1271e9dd5dafSParav Pandit ib_umad_dev_put(umad_dev);
1272e9dd5dafSParav Pandit }
1273e9dd5dafSParav Pandit
ib_umad_init_port_dev(struct device * dev,struct ib_umad_port * port,const struct ib_device * device)1274e9dd5dafSParav Pandit static void ib_umad_init_port_dev(struct device *dev,
1275e9dd5dafSParav Pandit struct ib_umad_port *port,
1276e9dd5dafSParav Pandit const struct ib_device *device)
1277e9dd5dafSParav Pandit {
1278e9dd5dafSParav Pandit device_initialize(dev);
1279e9dd5dafSParav Pandit ib_umad_dev_get(port->umad_dev);
1280e9dd5dafSParav Pandit dev->class = &umad_class;
1281e9dd5dafSParav Pandit dev->parent = device->dev.parent;
1282e9dd5dafSParav Pandit dev_set_drvdata(dev, port);
1283e9dd5dafSParav Pandit dev->release = ib_umad_release_port;
1284e9dd5dafSParav Pandit }
1285e9dd5dafSParav Pandit
ib_umad_init_port(struct ib_device * device,int port_num,struct ib_umad_device * umad_dev,struct ib_umad_port * port)12861da177e4SLinus Torvalds static int ib_umad_init_port(struct ib_device *device, int port_num,
128760e1751cSBart Van Assche struct ib_umad_device *umad_dev,
12881da177e4SLinus Torvalds struct ib_umad_port *port)
12891da177e4SLinus Torvalds {
1290d451b8dfSAlexander Chiang int devnum;
12918cf12d77SHuy Nguyen dev_t base_umad;
12928cf12d77SHuy Nguyen dev_t base_issm;
1293e9dd5dafSParav Pandit int ret;
1294d451b8dfSAlexander Chiang
1295551d315eSLeon Romanovsky devnum = ida_alloc_max(&umad_ida, IB_UMAD_MAX_PORTS - 1, GFP_KERNEL);
1296551d315eSLeon Romanovsky if (devnum < 0)
12971da177e4SLinus Torvalds return -1;
1298d451b8dfSAlexander Chiang port->dev_num = devnum;
12998cf12d77SHuy Nguyen if (devnum >= IB_UMAD_NUM_FIXED_MINOR) {
13008cf12d77SHuy Nguyen base_umad = dynamic_umad_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
13018cf12d77SHuy Nguyen base_issm = dynamic_issm_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
13028cf12d77SHuy Nguyen } else {
13038cf12d77SHuy Nguyen base_umad = devnum + base_umad_dev;
13048cf12d77SHuy Nguyen base_issm = devnum + base_issm_dev;
13058698d3feSAlexander Chiang }
13061da177e4SLinus Torvalds
13071da177e4SLinus Torvalds port->ib_dev = device;
1308e9dd5dafSParav Pandit port->umad_dev = umad_dev;
13091da177e4SLinus Torvalds port->port_num = port_num;
1310557d0540SThomas Gleixner sema_init(&port->sm_sem, 1);
13112fe7e6f7SRoland Dreier mutex_init(&port->file_mutex);
13120c99cb6dSRoland Dreier INIT_LIST_HEAD(&port->file_list);
13131da177e4SLinus Torvalds
1314e9dd5dafSParav Pandit ib_umad_init_port_dev(&port->dev, port, device);
1315e9dd5dafSParav Pandit port->dev.devt = base_umad;
1316e9dd5dafSParav Pandit dev_set_name(&port->dev, "umad%d", port->dev_num);
13172b937afcSAlexander Chiang cdev_init(&port->cdev, &umad_fops);
13182b937afcSAlexander Chiang port->cdev.owner = THIS_MODULE;
1319e9dd5dafSParav Pandit
1320e9dd5dafSParav Pandit ret = cdev_device_add(&port->cdev, &port->dev);
1321e9dd5dafSParav Pandit if (ret)
13221da177e4SLinus Torvalds goto err_cdev;
13231da177e4SLinus Torvalds
1324e9dd5dafSParav Pandit ib_umad_init_port_dev(&port->sm_dev, port, device);
1325e9dd5dafSParav Pandit port->sm_dev.devt = base_issm;
1326e9dd5dafSParav Pandit dev_set_name(&port->sm_dev, "issm%d", port->dev_num);
13272b937afcSAlexander Chiang cdev_init(&port->sm_cdev, &umad_sm_fops);
13282b937afcSAlexander Chiang port->sm_cdev.owner = THIS_MODULE;
13291da177e4SLinus Torvalds
1330e9dd5dafSParav Pandit ret = cdev_device_add(&port->sm_cdev, &port->sm_dev);
1331e9dd5dafSParav Pandit if (ret)
1332e9dd5dafSParav Pandit goto err_dev;
13331da177e4SLinus Torvalds
13341da177e4SLinus Torvalds return 0;
13351da177e4SLinus Torvalds
1336f4e91eb4STony Jones err_dev:
1337e9dd5dafSParav Pandit put_device(&port->sm_dev);
1338e9dd5dafSParav Pandit cdev_device_del(&port->cdev, &port->dev);
13391da177e4SLinus Torvalds err_cdev:
1340e9dd5dafSParav Pandit put_device(&port->dev);
1341551d315eSLeon Romanovsky ida_free(&umad_ida, devnum);
1342e9dd5dafSParav Pandit return ret;
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds
ib_umad_kill_port(struct ib_umad_port * port)1345a74968f8SRoland Dreier static void ib_umad_kill_port(struct ib_umad_port *port)
1346a74968f8SRoland Dreier {
13470c99cb6dSRoland Dreier struct ib_umad_file *file;
13480c99cb6dSRoland Dreier int id;
13490c99cb6dSRoland Dreier
13509ea04d0dSYonatan Cohen cdev_device_del(&port->sm_cdev, &port->sm_dev);
13519ea04d0dSYonatan Cohen cdev_device_del(&port->cdev, &port->dev);
13529ea04d0dSYonatan Cohen
13532fe7e6f7SRoland Dreier mutex_lock(&port->file_mutex);
13540c99cb6dSRoland Dreier
1355cf7ad303SParav Pandit /* Mark ib_dev NULL and block ioctl or other file ops to progress
1356cf7ad303SParav Pandit * further.
1357cf7ad303SParav Pandit */
13580c99cb6dSRoland Dreier port->ib_dev = NULL;
13590c99cb6dSRoland Dreier
13602fe7e6f7SRoland Dreier list_for_each_entry(file, &port->file_list, port_list) {
13612fe7e6f7SRoland Dreier mutex_lock(&file->mutex);
136294382f35SRoland Dreier file->agents_dead = 1;
1363def4cd43SShay Drory wake_up_interruptible(&file->recv_wait);
13642fe7e6f7SRoland Dreier mutex_unlock(&file->mutex);
136594382f35SRoland Dreier
136694382f35SRoland Dreier for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
136794382f35SRoland Dreier if (file->agent[id])
13680c99cb6dSRoland Dreier ib_unregister_mad_agent(file->agent[id]);
13690c99cb6dSRoland Dreier }
13700c99cb6dSRoland Dreier
13712fe7e6f7SRoland Dreier mutex_unlock(&port->file_mutex);
1372cf7ad303SParav Pandit
1373039d713aSParav Pandit ida_free(&umad_ida, port->dev_num);
1374039d713aSParav Pandit
1375ee848721SParav Pandit /* balances device_initialize() */
1376e9dd5dafSParav Pandit put_device(&port->sm_dev);
1377e9dd5dafSParav Pandit put_device(&port->dev);
1378a74968f8SRoland Dreier }
1379a74968f8SRoland Dreier
ib_umad_add_one(struct ib_device * device)138011a0ae4cSJason Gunthorpe static int ib_umad_add_one(struct ib_device *device)
13811da177e4SLinus Torvalds {
13821da177e4SLinus Torvalds struct ib_umad_device *umad_dev;
13831da177e4SLinus Torvalds int s, e, i;
1384827f2a8bSMichael Wang int count = 0;
138511a0ae4cSJason Gunthorpe int ret;
13861da177e4SLinus Torvalds
1387ab8be619SIra Weiny s = rdma_start_port(device);
1388ab8be619SIra Weiny e = rdma_end_port(device);
13891da177e4SLinus Torvalds
1390119d4350SGustavo A. R. Silva umad_dev = kzalloc(struct_size(umad_dev, ports,
1391119d4350SGustavo A. R. Silva size_add(size_sub(e, s), 1)),
1392119d4350SGustavo A. R. Silva GFP_KERNEL);
13931da177e4SLinus Torvalds if (!umad_dev)
139411a0ae4cSJason Gunthorpe return -ENOMEM;
13951da177e4SLinus Torvalds
1396e9dd5dafSParav Pandit kref_init(&umad_dev->kref);
13971da177e4SLinus Torvalds for (i = s; i <= e; ++i) {
1398c757dea8SMichael Wang if (!rdma_cap_ib_mad(device, i))
1399827f2a8bSMichael Wang continue;
1400827f2a8bSMichael Wang
140111a0ae4cSJason Gunthorpe ret = ib_umad_init_port(device, i, umad_dev,
140211a0ae4cSJason Gunthorpe &umad_dev->ports[i - s]);
140311a0ae4cSJason Gunthorpe if (ret)
14041da177e4SLinus Torvalds goto err;
1405827f2a8bSMichael Wang
1406827f2a8bSMichael Wang count++;
14071da177e4SLinus Torvalds }
14081da177e4SLinus Torvalds
140911a0ae4cSJason Gunthorpe if (!count) {
141011a0ae4cSJason Gunthorpe ret = -EOPNOTSUPP;
1411827f2a8bSMichael Wang goto free;
141211a0ae4cSJason Gunthorpe }
1413827f2a8bSMichael Wang
14141da177e4SLinus Torvalds ib_set_client_data(device, &umad_client, umad_dev);
14151da177e4SLinus Torvalds
141611a0ae4cSJason Gunthorpe return 0;
14171da177e4SLinus Torvalds
14181da177e4SLinus Torvalds err:
1419827f2a8bSMichael Wang while (--i >= s) {
1420c757dea8SMichael Wang if (!rdma_cap_ib_mad(device, i))
1421827f2a8bSMichael Wang continue;
14221da177e4SLinus Torvalds
1423e9dd5dafSParav Pandit ib_umad_kill_port(&umad_dev->ports[i - s]);
1424827f2a8bSMichael Wang }
1425827f2a8bSMichael Wang free:
1426ee848721SParav Pandit /* balances kref_init */
1427e9dd5dafSParav Pandit ib_umad_dev_put(umad_dev);
142811a0ae4cSJason Gunthorpe return ret;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds
ib_umad_remove_one(struct ib_device * device,void * client_data)14317c1eb45aSHaggai Eran static void ib_umad_remove_one(struct ib_device *device, void *client_data)
14321da177e4SLinus Torvalds {
14337c1eb45aSHaggai Eran struct ib_umad_device *umad_dev = client_data;
1434ea1075edSJason Gunthorpe unsigned int i;
14351da177e4SLinus Torvalds
1436ea1075edSJason Gunthorpe rdma_for_each_port (device, i) {
1437ea1075edSJason Gunthorpe if (rdma_cap_ib_mad(device, i))
1438ea1075edSJason Gunthorpe ib_umad_kill_port(
1439ea1075edSJason Gunthorpe &umad_dev->ports[i - rdma_start_port(device)]);
1440827f2a8bSMichael Wang }
1441ee848721SParav Pandit /* balances kref_init() */
1442e9dd5dafSParav Pandit ib_umad_dev_put(umad_dev);
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds
ib_umad_init(void)14451da177e4SLinus Torvalds static int __init ib_umad_init(void)
14461da177e4SLinus Torvalds {
14471da177e4SLinus Torvalds int ret;
14481da177e4SLinus Torvalds
14498cf12d77SHuy Nguyen ret = register_chrdev_region(base_umad_dev,
14508cf12d77SHuy Nguyen IB_UMAD_NUM_FIXED_MINOR * 2,
1451e9dd5dafSParav Pandit umad_class.name);
14521da177e4SLinus Torvalds if (ret) {
1453f426a40eSIra Weiny pr_err("couldn't register device number\n");
14541da177e4SLinus Torvalds goto out;
14551da177e4SLinus Torvalds }
14561da177e4SLinus Torvalds
14578cf12d77SHuy Nguyen ret = alloc_chrdev_region(&dynamic_umad_dev, 0,
14588cf12d77SHuy Nguyen IB_UMAD_NUM_DYNAMIC_MINOR * 2,
1459e9dd5dafSParav Pandit umad_class.name);
14608cf12d77SHuy Nguyen if (ret) {
14618cf12d77SHuy Nguyen pr_err("couldn't register dynamic device number\n");
14628cf12d77SHuy Nguyen goto out_alloc;
14638cf12d77SHuy Nguyen }
14648cf12d77SHuy Nguyen dynamic_issm_dev = dynamic_umad_dev + IB_UMAD_NUM_DYNAMIC_MINOR;
14658cf12d77SHuy Nguyen
1466900d07c1SParav Pandit ret = class_register(&umad_class);
1467900d07c1SParav Pandit if (ret) {
1468f426a40eSIra Weiny pr_err("couldn't create class infiniband_mad\n");
14691da177e4SLinus Torvalds goto out_chrdev;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds
14721da177e4SLinus Torvalds ret = ib_register_client(&umad_client);
14738f71bb00SJason Gunthorpe if (ret)
14741da177e4SLinus Torvalds goto out_class;
14758f71bb00SJason Gunthorpe
14768f71bb00SJason Gunthorpe ret = ib_register_client(&issm_client);
14778f71bb00SJason Gunthorpe if (ret)
14788f71bb00SJason Gunthorpe goto out_client;
14791da177e4SLinus Torvalds
14801da177e4SLinus Torvalds return 0;
14811da177e4SLinus Torvalds
14828f71bb00SJason Gunthorpe out_client:
14838f71bb00SJason Gunthorpe ib_unregister_client(&umad_client);
14841da177e4SLinus Torvalds out_class:
1485900d07c1SParav Pandit class_unregister(&umad_class);
14861da177e4SLinus Torvalds
14871da177e4SLinus Torvalds out_chrdev:
14888cf12d77SHuy Nguyen unregister_chrdev_region(dynamic_umad_dev,
14898cf12d77SHuy Nguyen IB_UMAD_NUM_DYNAMIC_MINOR * 2);
14908cf12d77SHuy Nguyen
14918cf12d77SHuy Nguyen out_alloc:
14928cf12d77SHuy Nguyen unregister_chrdev_region(base_umad_dev,
14938cf12d77SHuy Nguyen IB_UMAD_NUM_FIXED_MINOR * 2);
14941da177e4SLinus Torvalds
14951da177e4SLinus Torvalds out:
14961da177e4SLinus Torvalds return ret;
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds
ib_umad_cleanup(void)14991da177e4SLinus Torvalds static void __exit ib_umad_cleanup(void)
15001da177e4SLinus Torvalds {
15018f71bb00SJason Gunthorpe ib_unregister_client(&issm_client);
15021da177e4SLinus Torvalds ib_unregister_client(&umad_client);
1503900d07c1SParav Pandit class_unregister(&umad_class);
15048cf12d77SHuy Nguyen unregister_chrdev_region(base_umad_dev,
15058cf12d77SHuy Nguyen IB_UMAD_NUM_FIXED_MINOR * 2);
15068cf12d77SHuy Nguyen unregister_chrdev_region(dynamic_umad_dev,
15078cf12d77SHuy Nguyen IB_UMAD_NUM_DYNAMIC_MINOR * 2);
15081da177e4SLinus Torvalds }
15091da177e4SLinus Torvalds
15101da177e4SLinus Torvalds module_init(ib_umad_init);
15111da177e4SLinus Torvalds module_exit(ib_umad_cleanup);
1512