blob: 944cd90417bcc9b2a51d27f903feffad6f2307ea [file] [log] [blame]
Sean Heftye51060f2006-06-17 20:37:29 -07001/*
2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
6 *
Sean Heftya9474912008-07-14 23:48:43 -07007 * 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:
Sean Heftye51060f2006-06-17 20:37:29 -070012 *
Sean Heftya9474912008-07-14 23:48:43 -070013 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
Sean Heftye51060f2006-06-17 20:37:29 -070016 *
Sean Heftya9474912008-07-14 23:48:43 -070017 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
Sean Heftye51060f2006-06-17 20:37:29 -070020 *
Sean Heftya9474912008-07-14 23:48:43 -070021 * - 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.
Sean Heftye51060f2006-06-17 20:37:29 -070025 *
Sean Heftya9474912008-07-14 23:48:43 -070026 * 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.
Sean Heftye51060f2006-06-17 20:37:29 -070034 */
35
36#include <linux/completion.h>
37#include <linux/in.h>
38#include <linux/in6.h>
39#include <linux/mutex.h>
40#include <linux/random.h>
41#include <linux/idr.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050042#include <linux/inetdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040044#include <linux/module.h>
Amir Vadai366cddb2012-04-04 21:33:29 +000045#include <net/route.h>
Sean Heftye51060f2006-06-17 20:37:29 -070046
Haggai Eran4be74b42015-10-22 15:20:09 +030047#include <net/net_namespace.h>
48#include <net/netns/generic.h>
Sean Heftye51060f2006-06-17 20:37:29 -070049#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080050#include <net/ipv6.h>
Haggai Eranf887f2a2015-07-30 17:50:23 +030051#include <net/ip_fib.h>
52#include <net/ip6_route.h>
Sean Heftye51060f2006-06-17 20:37:29 -070053
54#include <rdma/rdma_cm.h>
55#include <rdma/rdma_cm_ib.h>
Nir Muchtar753f6182011-01-03 15:33:53 +000056#include <rdma/rdma_netlink.h>
Sean Hefty2e2d1902013-05-29 10:09:09 -070057#include <rdma/ib.h>
Sean Heftye51060f2006-06-17 20:37:29 -070058#include <rdma/ib_cache.h>
59#include <rdma/ib_cm.h>
60#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050061#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070062
63MODULE_AUTHOR("Sean Hefty");
64MODULE_DESCRIPTION("Generic RDMA CM Agent");
65MODULE_LICENSE("Dual BSD/GPL");
66
67#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030068#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070069#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020070#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070071
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030072static const char * const cma_events[] = {
73 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
74 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
75 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
76 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
77 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
78 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
79 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
80 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
81 [RDMA_CM_EVENT_REJECTED] = "rejected",
82 [RDMA_CM_EVENT_ESTABLISHED] = "established",
83 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
84 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
85 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
86 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
87 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
88 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
89};
90
Bart Van Asschedb7489e2015-08-03 10:01:52 -070091const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030092{
93 size_t index = event;
94
95 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
96 cma_events[index] : "unrecognized event";
97}
98EXPORT_SYMBOL(rdma_event_msg);
99
Sean Heftye51060f2006-06-17 20:37:29 -0700100static void cma_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300101static void cma_remove_one(struct ib_device *device, void *client_data);
Sean Heftye51060f2006-06-17 20:37:29 -0700102
103static struct ib_client cma_client = {
104 .name = "cma",
105 .add = cma_add_one,
106 .remove = cma_remove_one
107};
108
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700109static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -0800110static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -0700111static LIST_HEAD(dev_list);
112static LIST_HEAD(listen_any_list);
113static DEFINE_MUTEX(lock);
114static struct workqueue_struct *cma_wq;
Haggai Eran4be74b42015-10-22 15:20:09 +0300115static int cma_pernet_id;
Sean Heftye51060f2006-06-17 20:37:29 -0700116
Haggai Eran4be74b42015-10-22 15:20:09 +0300117struct cma_pernet {
118 struct idr tcp_ps;
119 struct idr udp_ps;
120 struct idr ipoib_ps;
121 struct idr ib_ps;
122};
123
124static struct cma_pernet *cma_pernet(struct net *net)
Haggai Eranaac978e2015-07-30 17:50:20 +0300125{
Haggai Eran4be74b42015-10-22 15:20:09 +0300126 return net_generic(net, cma_pernet_id);
127}
128
129static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps)
130{
131 struct cma_pernet *pernet = cma_pernet(net);
132
Haggai Eranaac978e2015-07-30 17:50:20 +0300133 switch (ps) {
134 case RDMA_PS_TCP:
Haggai Eran4be74b42015-10-22 15:20:09 +0300135 return &pernet->tcp_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300136 case RDMA_PS_UDP:
Haggai Eran4be74b42015-10-22 15:20:09 +0300137 return &pernet->udp_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300138 case RDMA_PS_IPOIB:
Haggai Eran4be74b42015-10-22 15:20:09 +0300139 return &pernet->ipoib_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300140 case RDMA_PS_IB:
Haggai Eran4be74b42015-10-22 15:20:09 +0300141 return &pernet->ib_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300142 default:
143 return NULL;
144 }
145}
146
Sean Heftye51060f2006-06-17 20:37:29 -0700147struct cma_device {
148 struct list_head list;
149 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -0700150 struct completion comp;
151 atomic_t refcount;
152 struct list_head id_list;
153};
154
Sean Heftye51060f2006-06-17 20:37:29 -0700155struct rdma_bind_list {
Haggai Eranaac978e2015-07-30 17:50:20 +0300156 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -0700157 struct hlist_head owners;
158 unsigned short port;
159};
160
Haggai Eran4be74b42015-10-22 15:20:09 +0300161static int cma_ps_alloc(struct net *net, enum rdma_port_space ps,
Haggai Eranaac978e2015-07-30 17:50:20 +0300162 struct rdma_bind_list *bind_list, int snum)
163{
Haggai Eran4be74b42015-10-22 15:20:09 +0300164 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300165
166 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
167}
168
Haggai Eran4be74b42015-10-22 15:20:09 +0300169static struct rdma_bind_list *cma_ps_find(struct net *net,
170 enum rdma_port_space ps, int snum)
Haggai Eranaac978e2015-07-30 17:50:20 +0300171{
Haggai Eran4be74b42015-10-22 15:20:09 +0300172 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300173
174 return idr_find(idr, snum);
175}
176
Haggai Eran4be74b42015-10-22 15:20:09 +0300177static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum)
Haggai Eranaac978e2015-07-30 17:50:20 +0300178{
Haggai Eran4be74b42015-10-22 15:20:09 +0300179 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300180
181 idr_remove(idr, snum);
182}
183
Sean Hefty68602122012-06-14 20:31:39 +0000184enum {
185 CMA_OPTION_AFONLY,
186};
187
Sean Heftye51060f2006-06-17 20:37:29 -0700188/*
189 * Device removal can occur at anytime, so we need extra handling to
190 * serialize notifying the user of device removal with other callbacks.
191 * We do this by disabling removal notification while a callback is in process,
192 * and reporting it after the callback completes.
193 */
194struct rdma_id_private {
195 struct rdma_cm_id id;
196
197 struct rdma_bind_list *bind_list;
198 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700199 struct list_head list; /* listen_any_list or cma_device.list */
200 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700201 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800202 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700203
Sean Heftyd02d1f52007-10-09 11:12:34 -0700204 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700205 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700206 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700207 struct mutex qp_mutex;
208
Sean Heftye51060f2006-06-17 20:37:29 -0700209 struct completion comp;
210 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700211 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700212
213 int backlog;
214 int timeout_ms;
215 struct ib_sa_query *query;
216 int query_id;
217 union {
218 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500219 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700220 } cm_id;
221
222 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800223 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700224 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000225 pid_t owner;
Sean Hefty68602122012-06-14 20:31:39 +0000226 u32 options;
Sean Heftye51060f2006-06-17 20:37:29 -0700227 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700228 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700229 u8 reuseaddr;
Sean Hefty5b0ec992012-06-14 20:31:39 +0000230 u8 afonly;
Sean Heftye51060f2006-06-17 20:37:29 -0700231};
232
Sean Heftyc8f6a362007-02-15 17:00:18 -0800233struct cma_multicast {
234 struct rdma_id_private *id_priv;
235 union {
236 struct ib_sa_multicast *ib;
237 } multicast;
238 struct list_head list;
239 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700240 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200241 struct kref mcref;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800242};
243
Sean Heftye51060f2006-06-17 20:37:29 -0700244struct cma_work {
245 struct work_struct work;
246 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700247 enum rdma_cm_state old_state;
248 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700249 struct rdma_cm_event event;
250};
251
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700252struct cma_ndev_work {
253 struct work_struct work;
254 struct rdma_id_private *id;
255 struct rdma_cm_event event;
256};
257
Eli Cohen3c86aa72010-10-13 21:26:51 +0200258struct iboe_mcast_work {
259 struct work_struct work;
260 struct rdma_id_private *id;
261 struct cma_multicast *mc;
262};
263
Sean Heftye51060f2006-06-17 20:37:29 -0700264union cma_ip_addr {
265 struct in6_addr ip6;
266 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000267 __be32 pad[3];
268 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700269 } ip4;
270};
271
272struct cma_hdr {
273 u8 cma_version;
274 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000275 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700276 union cma_ip_addr src_addr;
277 union cma_ip_addr dst_addr;
278};
279
Sean Heftye51060f2006-06-17 20:37:29 -0700280#define CMA_VERSION 0x00
Sean Heftye51060f2006-06-17 20:37:29 -0700281
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300282struct cma_req_info {
283 struct ib_device *device;
284 int port;
285 union ib_gid local_gid;
286 __be64 service_id;
287 u16 pkey;
288 bool has_gid:1;
289};
290
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700291static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700292{
293 unsigned long flags;
294 int ret;
295
296 spin_lock_irqsave(&id_priv->lock, flags);
297 ret = (id_priv->state == comp);
298 spin_unlock_irqrestore(&id_priv->lock, flags);
299 return ret;
300}
301
302static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700303 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700304{
305 unsigned long flags;
306 int ret;
307
308 spin_lock_irqsave(&id_priv->lock, flags);
309 if ((ret = (id_priv->state == comp)))
310 id_priv->state = exch;
311 spin_unlock_irqrestore(&id_priv->lock, flags);
312 return ret;
313}
314
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700315static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
316 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700317{
318 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700319 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700320
321 spin_lock_irqsave(&id_priv->lock, flags);
322 old = id_priv->state;
323 id_priv->state = exch;
324 spin_unlock_irqrestore(&id_priv->lock, flags);
325 return old;
326}
327
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300328static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
Sean Heftye51060f2006-06-17 20:37:29 -0700329{
330 return hdr->ip_version >> 4;
331}
332
333static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
334{
335 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
336}
337
Sean Heftye51060f2006-06-17 20:37:29 -0700338static void cma_attach_to_dev(struct rdma_id_private *id_priv,
339 struct cma_device *cma_dev)
340{
341 atomic_inc(&cma_dev->refcount);
342 id_priv->cma_dev = cma_dev;
343 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200344 id_priv->id.route.addr.dev_addr.transport =
345 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700346 list_add_tail(&id_priv->list, &cma_dev->id_list);
347}
348
349static inline void cma_deref_dev(struct cma_device *cma_dev)
350{
351 if (atomic_dec_and_test(&cma_dev->refcount))
352 complete(&cma_dev->comp);
353}
354
Eli Cohen3c86aa72010-10-13 21:26:51 +0200355static inline void release_mc(struct kref *kref)
356{
357 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
358
359 kfree(mc->multicast.ib);
360 kfree(mc);
361}
362
Sean Heftya396d432011-02-23 09:05:39 -0800363static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700364{
Sean Heftya396d432011-02-23 09:05:39 -0800365 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700366 list_del(&id_priv->list);
367 cma_deref_dev(id_priv->cma_dev);
368 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800369 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700370}
371
Sean Heftyf4753832013-05-29 10:09:14 -0700372static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
373{
374 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
375}
376
377static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
378{
379 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
380}
381
382static inline unsigned short cma_family(struct rdma_id_private *id_priv)
383{
384 return id_priv->id.route.addr.src_addr.ss_family;
385}
386
Sean Hefty5c438132013-05-29 10:09:23 -0700387static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800388{
389 struct ib_sa_mcmember_rec rec;
390 int ret = 0;
391
Sean Hefty5c438132013-05-29 10:09:23 -0700392 if (id_priv->qkey) {
393 if (qkey && id_priv->qkey != qkey)
394 return -EINVAL;
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700395 return 0;
Sean Hefty5c438132013-05-29 10:09:23 -0700396 }
397
398 if (qkey) {
399 id_priv->qkey = qkey;
400 return 0;
401 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700402
403 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800404 case RDMA_PS_UDP:
Sean Hefty5c438132013-05-29 10:09:23 -0700405 case RDMA_PS_IB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700406 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800407 break;
408 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700409 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
410 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
411 id_priv->id.port_num, &rec.mgid,
412 &rec);
413 if (!ret)
414 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800415 break;
416 default:
417 break;
418 }
419 return ret;
420}
421
Sean Hefty680f9202013-05-29 10:09:12 -0700422static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
423{
424 dev_addr->dev_type = ARPHRD_INFINIBAND;
425 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
426 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
427}
428
429static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
430{
431 int ret;
432
433 if (addr->sa_family != AF_IB) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200434 ret = rdma_translate_ip(addr, dev_addr, NULL);
Sean Hefty680f9202013-05-29 10:09:12 -0700435 } else {
436 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
437 ret = 0;
438 }
439
440 return ret;
441}
442
Michael Wang7c111472015-05-05 14:50:30 +0200443static inline int cma_validate_port(struct ib_device *device, u8 port,
Matan Barakabae1b72015-10-15 18:38:49 +0300444 union ib_gid *gid, int dev_type,
445 int bound_if_index)
Michael Wang7c111472015-05-05 14:50:30 +0200446{
Michael Wang7c111472015-05-05 14:50:30 +0200447 int ret = -ENODEV;
Matan Barakabae1b72015-10-15 18:38:49 +0300448 struct net_device *ndev = NULL;
Michael Wang7c111472015-05-05 14:50:30 +0200449
450 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
451 return ret;
452
453 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
454 return ret;
455
Matan Barakabae1b72015-10-15 18:38:49 +0300456 if (dev_type == ARPHRD_ETHER)
457 ndev = dev_get_by_index(&init_net, bound_if_index);
458
459 ret = ib_find_cached_gid_by_port(device, gid, port, ndev, NULL);
460
461 if (ndev)
462 dev_put(ndev);
Michael Wang7c111472015-05-05 14:50:30 +0200463
464 return ret;
465}
466
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400467static int cma_acquire_dev(struct rdma_id_private *id_priv,
468 struct rdma_id_private *listen_id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700469{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800470 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700471 struct cma_device *cma_dev;
Michael Wang7c111472015-05-05 14:50:30 +0200472 union ib_gid gid, iboe_gid, *gidp;
Sean Heftye51060f2006-06-17 20:37:29 -0700473 int ret = -ENODEV;
Michael Wang7c111472015-05-05 14:50:30 +0200474 u8 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700475
Michael Wang7c111472015-05-05 14:50:30 +0200476 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000477 id_priv->id.ps == RDMA_PS_IPOIB)
478 return -EINVAL;
479
Sean Heftya396d432011-02-23 09:05:39 -0800480 mutex_lock(&lock);
Moni Shoua7b856272013-12-12 18:03:12 +0200481 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
482 &iboe_gid);
483
Eli Cohen3c86aa72010-10-13 21:26:51 +0200484 memcpy(&gid, dev_addr->src_dev_addr +
485 rdma_addr_gid_offset(dev_addr), sizeof gid);
Michael Wang7c111472015-05-05 14:50:30 +0200486
487 if (listen_id_priv) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400488 cma_dev = listen_id_priv->cma_dev;
489 port = listen_id_priv->id.port_num;
Ira Weiny5d9fb042015-05-14 15:01:46 -0400490 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200491 &iboe_gid : &gid;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400492
Michael Wang7c111472015-05-05 14:50:30 +0200493 ret = cma_validate_port(cma_dev->device, port, gidp,
Matan Barakabae1b72015-10-15 18:38:49 +0300494 dev_addr->dev_type,
495 dev_addr->bound_dev_if);
Michael Wang7c111472015-05-05 14:50:30 +0200496 if (!ret) {
497 id_priv->id.port_num = port;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400498 goto out;
499 }
500 }
Michael Wang7c111472015-05-05 14:50:30 +0200501
Sean Heftye51060f2006-06-17 20:37:29 -0700502 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200503 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400504 if (listen_id_priv &&
505 listen_id_priv->cma_dev == cma_dev &&
506 listen_id_priv->id.port_num == port)
507 continue;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200508
Ira Weiny5d9fb042015-05-14 15:01:46 -0400509 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200510 &iboe_gid : &gid;
511
512 ret = cma_validate_port(cma_dev->device, port, gidp,
Matan Barakabae1b72015-10-15 18:38:49 +0300513 dev_addr->dev_type,
514 dev_addr->bound_dev_if);
Michael Wang7c111472015-05-05 14:50:30 +0200515 if (!ret) {
516 id_priv->id.port_num = port;
517 goto out;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200518 }
Sean Heftye51060f2006-06-17 20:37:29 -0700519 }
520 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200521
522out:
523 if (!ret)
524 cma_attach_to_dev(id_priv, cma_dev);
525
Sean Heftya396d432011-02-23 09:05:39 -0800526 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700527 return ret;
528}
529
Sean Heftyf17df3b2013-05-29 10:09:17 -0700530/*
531 * Select the source IB device and address to reach the destination IB address.
532 */
533static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
534{
535 struct cma_device *cma_dev, *cur_dev;
536 struct sockaddr_ib *addr;
537 union ib_gid gid, sgid, *dgid;
538 u16 pkey, index;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700539 u8 p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700540 int i;
541
542 cma_dev = NULL;
543 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
544 dgid = (union ib_gid *) &addr->sib_addr;
545 pkey = ntohs(addr->sib_pkey);
546
547 list_for_each_entry(cur_dev, &dev_list, list) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700548 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
Michael Wang30a74ef2015-05-05 14:50:39 +0200549 if (!rdma_cap_af_ib(cur_dev->device, p))
Michael Wangfef60902015-05-05 14:50:31 +0200550 continue;
551
Sean Heftyf17df3b2013-05-29 10:09:17 -0700552 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
553 continue;
554
Matan Barak55ee3ab2015-10-15 18:38:45 +0300555 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
556 &gid, NULL);
557 i++) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700558 if (!memcmp(&gid, dgid, sizeof(gid))) {
559 cma_dev = cur_dev;
560 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700561 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700562 goto found;
563 }
564
565 if (!cma_dev && (gid.global.subnet_prefix ==
566 dgid->global.subnet_prefix)) {
567 cma_dev = cur_dev;
568 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700569 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700570 }
571 }
572 }
573 }
574
575 if (!cma_dev)
576 return -ENODEV;
577
578found:
579 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf17df3b2013-05-29 10:09:17 -0700580 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
581 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
582 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
583 return 0;
584}
585
Sean Heftye51060f2006-06-17 20:37:29 -0700586static void cma_deref_id(struct rdma_id_private *id_priv)
587{
588 if (atomic_dec_and_test(&id_priv->refcount))
589 complete(&id_priv->comp);
590}
591
Or Gerlitzde910bd2008-07-14 23:48:53 -0700592static int cma_disable_callback(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700593 enum rdma_cm_state state)
Sean Hefty8aa08602007-05-07 11:49:00 -0700594{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700595 mutex_lock(&id_priv->handler_mutex);
596 if (id_priv->state != state) {
597 mutex_unlock(&id_priv->handler_mutex);
598 return -EINVAL;
599 }
600 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700601}
602
Guy Shapirofa201052015-10-22 15:20:10 +0300603struct rdma_cm_id *rdma_create_id(struct net *net,
604 rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000605 void *context, enum rdma_port_space ps,
606 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700607{
608 struct rdma_id_private *id_priv;
609
610 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
611 if (!id_priv)
612 return ERR_PTR(-ENOMEM);
613
Nir Muchtar83e95022011-01-13 13:56:04 +0000614 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700615 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700616 id_priv->id.context = context;
617 id_priv->id.event_handler = event_handler;
618 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000619 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700620 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700621 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700622 init_completion(&id_priv->comp);
623 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700624 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700625 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800626 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700627 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
Guy Shapirofa201052015-10-22 15:20:10 +0300628 id_priv->id.route.addr.dev_addr.net = get_net(net);
Sean Heftye51060f2006-06-17 20:37:29 -0700629
630 return &id_priv->id;
631}
632EXPORT_SYMBOL(rdma_create_id);
633
Sean Heftyc8f6a362007-02-15 17:00:18 -0800634static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700635{
636 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800637 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700638
Sean Heftyc8f6a362007-02-15 17:00:18 -0800639 qp_attr.qp_state = IB_QPS_INIT;
640 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700641 if (ret)
642 return ret;
643
Sean Heftyc8f6a362007-02-15 17:00:18 -0800644 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
645 if (ret)
646 return ret;
647
648 qp_attr.qp_state = IB_QPS_RTR;
649 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
650 if (ret)
651 return ret;
652
653 qp_attr.qp_state = IB_QPS_RTS;
654 qp_attr.sq_psn = 0;
655 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
656
657 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700658}
659
Sean Heftyc8f6a362007-02-15 17:00:18 -0800660static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500661{
662 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800663 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500664
665 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800666 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
667 if (ret)
668 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500669
Sean Heftyc8f6a362007-02-15 17:00:18 -0800670 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500671}
672
Sean Heftye51060f2006-06-17 20:37:29 -0700673int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
674 struct ib_qp_init_attr *qp_init_attr)
675{
676 struct rdma_id_private *id_priv;
677 struct ib_qp *qp;
678 int ret;
679
680 id_priv = container_of(id, struct rdma_id_private, id);
681 if (id->device != pd->device)
682 return -EINVAL;
683
684 qp = ib_create_qp(pd, qp_init_attr);
685 if (IS_ERR(qp))
686 return PTR_ERR(qp);
687
Sean Heftyb26f9b92010-04-01 17:08:41 +0000688 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800689 ret = cma_init_ud_qp(id_priv, qp);
690 else
691 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700692 if (ret)
693 goto err;
694
695 id->qp = qp;
696 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700697 id_priv->srq = (qp->srq != NULL);
698 return 0;
699err:
700 ib_destroy_qp(qp);
701 return ret;
702}
703EXPORT_SYMBOL(rdma_create_qp);
704
705void rdma_destroy_qp(struct rdma_cm_id *id)
706{
Sean Heftyc5483382007-09-24 13:19:09 -0700707 struct rdma_id_private *id_priv;
708
709 id_priv = container_of(id, struct rdma_id_private, id);
710 mutex_lock(&id_priv->qp_mutex);
711 ib_destroy_qp(id_priv->id.qp);
712 id_priv->id.qp = NULL;
713 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700714}
715EXPORT_SYMBOL(rdma_destroy_qp);
716
Sean Hefty5851bb82008-01-04 10:47:12 -0800717static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
718 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700719{
720 struct ib_qp_attr qp_attr;
721 int qp_attr_mask, ret;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200722 union ib_gid sgid;
Sean Heftye51060f2006-06-17 20:37:29 -0700723
Sean Heftyc5483382007-09-24 13:19:09 -0700724 mutex_lock(&id_priv->qp_mutex);
725 if (!id_priv->id.qp) {
726 ret = 0;
727 goto out;
728 }
Sean Heftye51060f2006-06-17 20:37:29 -0700729
730 /* Need to update QP attributes from default values. */
731 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700732 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700733 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700734 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700735
Sean Heftyc5483382007-09-24 13:19:09 -0700736 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700737 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700738 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700739
740 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700741 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700742 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700743 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700744
Matan Barakdd5f03b2013-12-12 18:03:11 +0200745 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300746 qp_attr.ah_attr.grh.sgid_index, &sgid, NULL);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200747 if (ret)
748 goto out;
749
Michael Wangfef60902015-05-05 14:50:31 +0200750 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
751
Sean Hefty5851bb82008-01-04 10:47:12 -0800752 if (conn_param)
753 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700754 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
755out:
756 mutex_unlock(&id_priv->qp_mutex);
757 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700758}
759
Sean Hefty5851bb82008-01-04 10:47:12 -0800760static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
761 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700762{
763 struct ib_qp_attr qp_attr;
764 int qp_attr_mask, ret;
765
Sean Heftyc5483382007-09-24 13:19:09 -0700766 mutex_lock(&id_priv->qp_mutex);
767 if (!id_priv->id.qp) {
768 ret = 0;
769 goto out;
770 }
Sean Heftye51060f2006-06-17 20:37:29 -0700771
772 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700773 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700774 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700775 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700776
Sean Hefty5851bb82008-01-04 10:47:12 -0800777 if (conn_param)
778 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700779 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
780out:
781 mutex_unlock(&id_priv->qp_mutex);
782 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700783}
784
Sean Heftyc5483382007-09-24 13:19:09 -0700785static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700786{
787 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700788 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700789
Sean Heftyc5483382007-09-24 13:19:09 -0700790 mutex_lock(&id_priv->qp_mutex);
791 if (!id_priv->id.qp) {
792 ret = 0;
793 goto out;
794 }
Sean Heftye51060f2006-06-17 20:37:29 -0700795
796 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700797 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
798out:
799 mutex_unlock(&id_priv->qp_mutex);
800 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700801}
802
Sean Heftyc8f6a362007-02-15 17:00:18 -0800803static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
804 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
805{
806 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
807 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200808 u16 pkey;
809
Michael Wang227128f2015-05-05 14:50:40 +0200810 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
Eli Cohen3c86aa72010-10-13 21:26:51 +0200811 pkey = 0xffff;
Michael Wangfef60902015-05-05 14:50:31 +0200812 else
813 pkey = ib_addr_get_pkey(dev_addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800814
815 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200816 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800817 if (ret)
818 return ret;
819
820 qp_attr->port_num = id_priv->id.port_num;
821 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
822
Sean Heftyb26f9b92010-04-01 17:08:41 +0000823 if (id_priv->id.qp_type == IB_QPT_UD) {
Sean Hefty5c438132013-05-29 10:09:23 -0700824 ret = cma_set_qkey(id_priv, 0);
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700825 if (ret)
826 return ret;
827
Sean Heftyc8f6a362007-02-15 17:00:18 -0800828 qp_attr->qkey = id_priv->qkey;
829 *qp_attr_mask |= IB_QP_QKEY;
830 } else {
831 qp_attr->qp_access_flags = 0;
832 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
833 }
834 return 0;
835}
836
Sean Heftye51060f2006-06-17 20:37:29 -0700837int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
838 int *qp_attr_mask)
839{
840 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800841 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700842
843 id_priv = container_of(id, struct rdma_id_private, id);
Michael Wang72219cea2015-05-05 14:50:34 +0200844 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +0000845 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800846 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
847 else
848 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
849 qp_attr_mask);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200850
Sean Heftye51060f2006-06-17 20:37:29 -0700851 if (qp_attr->qp_state == IB_QPS_RTR)
852 qp_attr->rq_psn = id_priv->seq_num;
Michael Wang04215332015-05-05 14:50:35 +0200853 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800854 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300855 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800856 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
857 } else
858 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
859 qp_attr_mask);
Michael Wang21655af2015-05-05 14:50:27 +0200860 } else
Sean Heftye51060f2006-06-17 20:37:29 -0700861 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -0700862
863 return ret;
864}
865EXPORT_SYMBOL(rdma_init_qp_attr);
866
867static inline int cma_zero_addr(struct sockaddr *addr)
868{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700869 switch (addr->sa_family) {
870 case AF_INET:
871 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
872 case AF_INET6:
873 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
874 case AF_IB:
875 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
876 default:
877 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700878 }
879}
880
881static inline int cma_loopback_addr(struct sockaddr *addr)
882{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700883 switch (addr->sa_family) {
884 case AF_INET:
885 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
886 case AF_INET6:
887 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
888 case AF_IB:
889 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
890 default:
891 return 0;
892 }
Sean Heftye51060f2006-06-17 20:37:29 -0700893}
894
895static inline int cma_any_addr(struct sockaddr *addr)
896{
897 return cma_zero_addr(addr) || cma_loopback_addr(addr);
898}
899
Hefty, Sean43b752d2011-05-09 22:06:10 -0700900static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
901{
902 if (src->sa_family != dst->sa_family)
903 return -1;
904
905 switch (src->sa_family) {
906 case AF_INET:
907 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
908 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
Sean Hefty2e2d1902013-05-29 10:09:09 -0700909 case AF_INET6:
Hefty, Sean43b752d2011-05-09 22:06:10 -0700910 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
911 &((struct sockaddr_in6 *) dst)->sin6_addr);
Sean Hefty2e2d1902013-05-29 10:09:09 -0700912 default:
913 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
914 &((struct sockaddr_ib *) dst)->sib_addr);
Hefty, Sean43b752d2011-05-09 22:06:10 -0700915 }
916}
917
Sean Hefty58afdcb2013-05-29 10:09:11 -0700918static __be16 cma_port(struct sockaddr *addr)
Sean Hefty628e5f62006-11-30 16:44:16 -0800919{
Sean Hefty58afdcb2013-05-29 10:09:11 -0700920 struct sockaddr_ib *sib;
921
922 switch (addr->sa_family) {
923 case AF_INET:
Sean Hefty628e5f62006-11-30 16:44:16 -0800924 return ((struct sockaddr_in *) addr)->sin_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700925 case AF_INET6:
Sean Hefty628e5f62006-11-30 16:44:16 -0800926 return ((struct sockaddr_in6 *) addr)->sin6_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700927 case AF_IB:
928 sib = (struct sockaddr_ib *) addr;
929 return htons((u16) (be64_to_cpu(sib->sib_sid) &
930 be64_to_cpu(sib->sib_sid_mask)));
931 default:
932 return 0;
933 }
Sean Hefty628e5f62006-11-30 16:44:16 -0800934}
935
Sean Heftye51060f2006-06-17 20:37:29 -0700936static inline int cma_any_port(struct sockaddr *addr)
937{
Sean Hefty628e5f62006-11-30 16:44:16 -0800938 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700939}
940
Haggai Eran0c505f72015-07-30 17:50:19 +0300941static void cma_save_ib_info(struct sockaddr *src_addr,
942 struct sockaddr *dst_addr,
943 struct rdma_cm_id *listen_id,
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700944 struct ib_sa_path_rec *path)
Sean Heftye51060f2006-06-17 20:37:29 -0700945{
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700946 struct sockaddr_ib *listen_ib, *ib;
Sean Heftye51060f2006-06-17 20:37:29 -0700947
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700948 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
Haggai Eran0c505f72015-07-30 17:50:19 +0300949 if (src_addr) {
950 ib = (struct sockaddr_ib *)src_addr;
951 ib->sib_family = AF_IB;
952 if (path) {
953 ib->sib_pkey = path->pkey;
954 ib->sib_flowinfo = path->flow_label;
955 memcpy(&ib->sib_addr, &path->sgid, 16);
956 ib->sib_sid = path->service_id;
957 ib->sib_scope_id = 0;
958 } else {
959 ib->sib_pkey = listen_ib->sib_pkey;
960 ib->sib_flowinfo = listen_ib->sib_flowinfo;
961 ib->sib_addr = listen_ib->sib_addr;
962 ib->sib_sid = listen_ib->sib_sid;
963 ib->sib_scope_id = listen_ib->sib_scope_id;
964 }
965 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
Matthew Finlayc07678b2015-05-19 00:11:48 -0700966 }
Haggai Eran0c505f72015-07-30 17:50:19 +0300967 if (dst_addr) {
968 ib = (struct sockaddr_ib *)dst_addr;
969 ib->sib_family = AF_IB;
970 if (path) {
971 ib->sib_pkey = path->pkey;
972 ib->sib_flowinfo = path->flow_label;
973 memcpy(&ib->sib_addr, &path->dgid, 16);
974 }
Matthew Finlayc07678b2015-05-19 00:11:48 -0700975 }
Sean Heftye51060f2006-06-17 20:37:29 -0700976}
977
Haggai Eran0c505f72015-07-30 17:50:19 +0300978static void cma_save_ip4_info(struct sockaddr *src_addr,
979 struct sockaddr *dst_addr,
980 struct cma_hdr *hdr,
981 __be16 local_port)
Sean Heftye51060f2006-06-17 20:37:29 -0700982{
Jason Gunthorpe28521442015-04-20 14:01:11 -0600983 struct sockaddr_in *ip4;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700984
Haggai Eran0c505f72015-07-30 17:50:19 +0300985 if (src_addr) {
986 ip4 = (struct sockaddr_in *)src_addr;
987 ip4->sin_family = AF_INET;
988 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr;
989 ip4->sin_port = local_port;
990 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700991
Haggai Eran0c505f72015-07-30 17:50:19 +0300992 if (dst_addr) {
993 ip4 = (struct sockaddr_in *)dst_addr;
994 ip4->sin_family = AF_INET;
995 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr;
996 ip4->sin_port = hdr->port;
997 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700998}
999
Haggai Eran0c505f72015-07-30 17:50:19 +03001000static void cma_save_ip6_info(struct sockaddr *src_addr,
1001 struct sockaddr *dst_addr,
1002 struct cma_hdr *hdr,
1003 __be16 local_port)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001004{
Jason Gunthorpe28521442015-04-20 14:01:11 -06001005 struct sockaddr_in6 *ip6;
Sean Heftye51060f2006-06-17 20:37:29 -07001006
Haggai Eran0c505f72015-07-30 17:50:19 +03001007 if (src_addr) {
1008 ip6 = (struct sockaddr_in6 *)src_addr;
1009 ip6->sin6_family = AF_INET6;
1010 ip6->sin6_addr = hdr->dst_addr.ip6;
1011 ip6->sin6_port = local_port;
1012 }
Sean Heftye51060f2006-06-17 20:37:29 -07001013
Haggai Eran0c505f72015-07-30 17:50:19 +03001014 if (dst_addr) {
1015 ip6 = (struct sockaddr_in6 *)dst_addr;
1016 ip6->sin6_family = AF_INET6;
1017 ip6->sin6_addr = hdr->src_addr.ip6;
1018 ip6->sin6_port = hdr->port;
1019 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001020}
1021
Haggai Eran0c505f72015-07-30 17:50:19 +03001022static u16 cma_port_from_service_id(__be64 service_id)
1023{
1024 return (u16)be64_to_cpu(service_id);
1025}
1026
1027static int cma_save_ip_info(struct sockaddr *src_addr,
1028 struct sockaddr *dst_addr,
1029 struct ib_cm_event *ib_event,
1030 __be64 service_id)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001031{
1032 struct cma_hdr *hdr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001033 __be16 port;
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001034
1035 hdr = ib_event->private_data;
1036 if (hdr->cma_version != CMA_VERSION)
1037 return -EINVAL;
1038
Haggai Eran0c505f72015-07-30 17:50:19 +03001039 port = htons(cma_port_from_service_id(service_id));
1040
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001041 switch (cma_get_ip_ver(hdr)) {
1042 case 4:
Haggai Eran0c505f72015-07-30 17:50:19 +03001043 cma_save_ip4_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001044 break;
1045 case 6:
Haggai Eran0c505f72015-07-30 17:50:19 +03001046 cma_save_ip6_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001047 break;
1048 default:
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001049 return -EAFNOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07001050 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001051
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001052 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001053}
1054
Haggai Eran0c505f72015-07-30 17:50:19 +03001055static int cma_save_net_info(struct sockaddr *src_addr,
1056 struct sockaddr *dst_addr,
1057 struct rdma_cm_id *listen_id,
1058 struct ib_cm_event *ib_event,
1059 sa_family_t sa_family, __be64 service_id)
1060{
1061 if (sa_family == AF_IB) {
1062 if (ib_event->event == IB_CM_REQ_RECEIVED)
1063 cma_save_ib_info(src_addr, dst_addr, listen_id,
1064 ib_event->param.req_rcvd.primary_path);
1065 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1066 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1067 return 0;
1068 }
1069
1070 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1071}
1072
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001073static int cma_save_req_info(const struct ib_cm_event *ib_event,
1074 struct cma_req_info *req)
1075{
1076 const struct ib_cm_req_event_param *req_param =
1077 &ib_event->param.req_rcvd;
1078 const struct ib_cm_sidr_req_event_param *sidr_param =
1079 &ib_event->param.sidr_req_rcvd;
1080
1081 switch (ib_event->event) {
1082 case IB_CM_REQ_RECEIVED:
1083 req->device = req_param->listen_id->device;
1084 req->port = req_param->port;
1085 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1086 sizeof(req->local_gid));
1087 req->has_gid = true;
1088 req->service_id = req_param->primary_path->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001089 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001090 break;
1091 case IB_CM_SIDR_REQ_RECEIVED:
1092 req->device = sidr_param->listen_id->device;
1093 req->port = sidr_param->port;
1094 req->has_gid = false;
1095 req->service_id = sidr_param->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001096 req->pkey = sidr_param->pkey;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001097 break;
1098 default:
1099 return -EINVAL;
1100 }
1101
1102 return 0;
1103}
1104
Haggai Eranf887f2a2015-07-30 17:50:23 +03001105static bool validate_ipv4_net_dev(struct net_device *net_dev,
1106 const struct sockaddr_in *dst_addr,
1107 const struct sockaddr_in *src_addr)
1108{
1109 __be32 daddr = dst_addr->sin_addr.s_addr,
1110 saddr = src_addr->sin_addr.s_addr;
1111 struct fib_result res;
1112 struct flowi4 fl4;
1113 int err;
1114 bool ret;
1115
1116 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1117 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1118 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1119 ipv4_is_loopback(saddr))
1120 return false;
1121
1122 memset(&fl4, 0, sizeof(fl4));
1123 fl4.flowi4_iif = net_dev->ifindex;
1124 fl4.daddr = daddr;
1125 fl4.saddr = saddr;
1126
1127 rcu_read_lock();
1128 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1129 if (err)
1130 return false;
1131
1132 ret = FIB_RES_DEV(res) == net_dev;
1133 rcu_read_unlock();
1134
1135 return ret;
1136}
1137
1138static bool validate_ipv6_net_dev(struct net_device *net_dev,
1139 const struct sockaddr_in6 *dst_addr,
1140 const struct sockaddr_in6 *src_addr)
1141{
1142#if IS_ENABLED(CONFIG_IPV6)
1143 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1144 IPV6_ADDR_LINKLOCAL;
1145 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1146 &src_addr->sin6_addr, net_dev->ifindex,
1147 strict);
1148 bool ret;
1149
1150 if (!rt)
1151 return false;
1152
1153 ret = rt->rt6i_idev->dev == net_dev;
1154 ip6_rt_put(rt);
1155
1156 return ret;
1157#else
1158 return false;
1159#endif
1160}
1161
1162static bool validate_net_dev(struct net_device *net_dev,
1163 const struct sockaddr *daddr,
1164 const struct sockaddr *saddr)
1165{
1166 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1167 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1168 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1169 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1170
1171 switch (daddr->sa_family) {
1172 case AF_INET:
1173 return saddr->sa_family == AF_INET &&
1174 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1175
1176 case AF_INET6:
1177 return saddr->sa_family == AF_INET6 &&
1178 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1179
1180 default:
1181 return false;
1182 }
1183}
1184
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001185static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1186 const struct cma_req_info *req)
1187{
Haggai Eranf887f2a2015-07-30 17:50:23 +03001188 struct sockaddr_storage listen_addr_storage, src_addr_storage;
1189 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1190 *src_addr = (struct sockaddr *)&src_addr_storage;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001191 struct net_device *net_dev;
1192 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1193 int err;
1194
Haggai Eranf887f2a2015-07-30 17:50:23 +03001195 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1196 req->service_id);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001197 if (err)
1198 return ERR_PTR(err);
1199
1200 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1201 gid, listen_addr);
1202 if (!net_dev)
1203 return ERR_PTR(-ENODEV);
1204
Haggai Eranf887f2a2015-07-30 17:50:23 +03001205 if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1206 dev_put(net_dev);
1207 return ERR_PTR(-EHOSTUNREACH);
1208 }
1209
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001210 return net_dev;
1211}
1212
1213static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1214{
1215 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1216}
1217
1218static bool cma_match_private_data(struct rdma_id_private *id_priv,
1219 const struct cma_hdr *hdr)
1220{
1221 struct sockaddr *addr = cma_src_addr(id_priv);
1222 __be32 ip4_addr;
1223 struct in6_addr ip6_addr;
1224
1225 if (cma_any_addr(addr) && !id_priv->afonly)
1226 return true;
1227
1228 switch (addr->sa_family) {
1229 case AF_INET:
1230 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1231 if (cma_get_ip_ver(hdr) != 4)
1232 return false;
1233 if (!cma_any_addr(addr) &&
1234 hdr->dst_addr.ip4.addr != ip4_addr)
1235 return false;
1236 break;
1237 case AF_INET6:
1238 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1239 if (cma_get_ip_ver(hdr) != 6)
1240 return false;
1241 if (!cma_any_addr(addr) &&
1242 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1243 return false;
1244 break;
1245 case AF_IB:
1246 return true;
1247 default:
1248 return false;
1249 }
1250
1251 return true;
1252}
1253
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001254static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1255{
1256 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1257 enum rdma_transport_type transport =
1258 rdma_node_get_transport(device->node_type);
1259
1260 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1261}
1262
1263static bool cma_protocol_roce(const struct rdma_cm_id *id)
1264{
1265 struct ib_device *device = id->device;
1266 const int port_num = id->port_num ?: rdma_start_port(device);
1267
1268 return cma_protocol_roce_dev_port(device, port_num);
1269}
1270
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001271static bool cma_match_net_dev(const struct rdma_id_private *id_priv,
1272 const struct net_device *net_dev)
1273{
1274 const struct rdma_addr *addr = &id_priv->id.route.addr;
1275
1276 if (!net_dev)
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001277 /* This request is an AF_IB request or a RoCE request */
1278 return addr->src_addr.ss_family == AF_IB ||
1279 cma_protocol_roce(&id_priv->id);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001280
1281 return !addr->dev_addr.bound_dev_if ||
Guy Shapirofa201052015-10-22 15:20:10 +03001282 (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001283 addr->dev_addr.bound_dev_if == net_dev->ifindex);
1284}
1285
1286static struct rdma_id_private *cma_find_listener(
1287 const struct rdma_bind_list *bind_list,
1288 const struct ib_cm_id *cm_id,
1289 const struct ib_cm_event *ib_event,
1290 const struct cma_req_info *req,
1291 const struct net_device *net_dev)
1292{
1293 struct rdma_id_private *id_priv, *id_priv_dev;
1294
1295 if (!bind_list)
1296 return ERR_PTR(-EINVAL);
1297
1298 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1299 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1300 if (id_priv->id.device == cm_id->device &&
1301 cma_match_net_dev(id_priv, net_dev))
1302 return id_priv;
1303 list_for_each_entry(id_priv_dev,
1304 &id_priv->listen_list,
1305 listen_list) {
1306 if (id_priv_dev->id.device == cm_id->device &&
1307 cma_match_net_dev(id_priv_dev, net_dev))
1308 return id_priv_dev;
1309 }
1310 }
1311 }
1312
1313 return ERR_PTR(-EINVAL);
1314}
1315
1316static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001317 struct ib_cm_event *ib_event,
1318 struct net_device **net_dev)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001319{
1320 struct cma_req_info req;
1321 struct rdma_bind_list *bind_list;
1322 struct rdma_id_private *id_priv;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001323 int err;
1324
1325 err = cma_save_req_info(ib_event, &req);
1326 if (err)
1327 return ERR_PTR(err);
1328
Haggai Eran0b3ca762015-07-30 17:50:24 +03001329 *net_dev = cma_get_net_dev(ib_event, &req);
1330 if (IS_ERR(*net_dev)) {
1331 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001332 /* Assuming the protocol is AF_IB */
Haggai Eran0b3ca762015-07-30 17:50:24 +03001333 *net_dev = NULL;
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001334 } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1335 /* TODO find the net dev matching the request parameters
1336 * through the RoCE GID table */
1337 *net_dev = NULL;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001338 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001339 return ERR_CAST(*net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001340 }
1341 }
1342
Guy Shapirofa201052015-10-22 15:20:10 +03001343 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
Haggai Eran4be74b42015-10-22 15:20:09 +03001344 rdma_ps_from_service_id(req.service_id),
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001345 cma_port_from_service_id(req.service_id));
Haggai Eran0b3ca762015-07-30 17:50:24 +03001346 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
Haggai Eranb3b51f92015-09-21 16:02:02 +03001347 if (IS_ERR(id_priv) && *net_dev) {
Haggai Eranbe688192015-08-27 15:55:15 +03001348 dev_put(*net_dev);
1349 *net_dev = NULL;
1350 }
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001351
1352 return id_priv;
1353}
1354
Sean Heftye8160e12013-05-29 10:09:22 -07001355static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07001356{
Sean Heftye8160e12013-05-29 10:09:22 -07001357 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
Sean Heftye51060f2006-06-17 20:37:29 -07001358}
1359
Sean Heftye51060f2006-06-17 20:37:29 -07001360static void cma_cancel_route(struct rdma_id_private *id_priv)
1361{
Michael Wangfe53ba22015-05-05 14:50:36 +02001362 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001363 if (id_priv->query)
1364 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001365 }
1366}
1367
Sean Heftye51060f2006-06-17 20:37:29 -07001368static void cma_cancel_listens(struct rdma_id_private *id_priv)
1369{
1370 struct rdma_id_private *dev_id_priv;
1371
Sean Heftyd02d1f52007-10-09 11:12:34 -07001372 /*
1373 * Remove from listen_any_list to prevent added devices from spawning
1374 * additional listen requests.
1375 */
Sean Heftye51060f2006-06-17 20:37:29 -07001376 mutex_lock(&lock);
1377 list_del(&id_priv->list);
1378
1379 while (!list_empty(&id_priv->listen_list)) {
1380 dev_id_priv = list_entry(id_priv->listen_list.next,
1381 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001382 /* sync with device removal to avoid duplicate destruction */
1383 list_del_init(&dev_id_priv->list);
1384 list_del(&dev_id_priv->listen_list);
1385 mutex_unlock(&lock);
1386
1387 rdma_destroy_id(&dev_id_priv->id);
1388 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001389 }
1390 mutex_unlock(&lock);
1391}
1392
1393static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001394 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -07001395{
1396 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001397 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001398 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1399 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001400 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001401 cma_cancel_route(id_priv);
1402 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001403 case RDMA_CM_LISTEN:
Sean Heftyf4753832013-05-29 10:09:14 -07001404 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001405 cma_cancel_listens(id_priv);
1406 break;
1407 default:
1408 break;
1409 }
1410}
1411
1412static void cma_release_port(struct rdma_id_private *id_priv)
1413{
1414 struct rdma_bind_list *bind_list = id_priv->bind_list;
Guy Shapirofa201052015-10-22 15:20:10 +03001415 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07001416
1417 if (!bind_list)
1418 return;
1419
1420 mutex_lock(&lock);
1421 hlist_del(&id_priv->node);
1422 if (hlist_empty(&bind_list->owners)) {
Guy Shapirofa201052015-10-22 15:20:10 +03001423 cma_ps_remove(net, bind_list->ps, bind_list->port);
Sean Heftye51060f2006-06-17 20:37:29 -07001424 kfree(bind_list);
1425 }
1426 mutex_unlock(&lock);
1427}
1428
Sean Heftyc8f6a362007-02-15 17:00:18 -08001429static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1430{
1431 struct cma_multicast *mc;
1432
1433 while (!list_empty(&id_priv->mc_list)) {
1434 mc = container_of(id_priv->mc_list.next,
1435 struct cma_multicast, list);
1436 list_del(&mc->list);
Michael Wanga31ad3b2015-05-05 14:50:37 +02001437 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
Michael Wang5c9a5282015-05-05 14:50:29 +02001438 id_priv->id.port_num)) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02001439 ib_sa_free_multicast(mc->multicast.ib);
1440 kfree(mc);
Michael Wang5c9a5282015-05-05 14:50:29 +02001441 } else
Eli Cohen3c86aa72010-10-13 21:26:51 +02001442 kref_put(&mc->mcref, release_mc);
Sean Heftyc8f6a362007-02-15 17:00:18 -08001443 }
1444}
1445
Sean Heftye51060f2006-06-17 20:37:29 -07001446void rdma_destroy_id(struct rdma_cm_id *id)
1447{
1448 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001449 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -07001450
1451 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001452 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001453 cma_cancel_operation(id_priv, state);
1454
Sean Heftya396d432011-02-23 09:05:39 -08001455 /*
1456 * Wait for any active callback to finish. New callbacks will find
1457 * the id_priv state set to destroying and abort.
1458 */
1459 mutex_lock(&id_priv->handler_mutex);
1460 mutex_unlock(&id_priv->handler_mutex);
1461
Sean Heftye51060f2006-06-17 20:37:29 -07001462 if (id_priv->cma_dev) {
Michael Wang72219cea2015-05-05 14:50:34 +02001463 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001464 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07001465 ib_destroy_cm_id(id_priv->cm_id.ib);
Michael Wang04215332015-05-05 14:50:35 +02001466 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001467 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -05001468 iw_destroy_cm_id(id_priv->cm_id.iw);
Sean Heftye51060f2006-06-17 20:37:29 -07001469 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001470 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -08001471 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001472 }
1473
1474 cma_release_port(id_priv);
1475 cma_deref_id(id_priv);
1476 wait_for_completion(&id_priv->comp);
1477
Sean Heftyd02d1f52007-10-09 11:12:34 -07001478 if (id_priv->internal_id)
1479 cma_deref_id(id_priv->id.context);
1480
Sean Heftye51060f2006-06-17 20:37:29 -07001481 kfree(id_priv->id.route.path_rec);
Guy Shapirofa201052015-10-22 15:20:10 +03001482 put_net(id_priv->id.route.addr.dev_addr.net);
Sean Heftye51060f2006-06-17 20:37:29 -07001483 kfree(id_priv);
1484}
1485EXPORT_SYMBOL(rdma_destroy_id);
1486
1487static int cma_rep_recv(struct rdma_id_private *id_priv)
1488{
1489 int ret;
1490
Sean Hefty5851bb82008-01-04 10:47:12 -08001491 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001492 if (ret)
1493 goto reject;
1494
Sean Hefty5851bb82008-01-04 10:47:12 -08001495 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001496 if (ret)
1497 goto reject;
1498
1499 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1500 if (ret)
1501 goto reject;
1502
1503 return 0;
1504reject:
Sean Heftyc5483382007-09-24 13:19:09 -07001505 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001506 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1507 NULL, 0, NULL, 0);
1508 return ret;
1509}
1510
Sean Heftya1b1b612006-11-30 16:33:14 -08001511static void cma_set_rep_event_data(struct rdma_cm_event *event,
1512 struct ib_cm_rep_event_param *rep_data,
1513 void *private_data)
1514{
1515 event->param.conn.private_data = private_data;
1516 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1517 event->param.conn.responder_resources = rep_data->responder_resources;
1518 event->param.conn.initiator_depth = rep_data->initiator_depth;
1519 event->param.conn.flow_control = rep_data->flow_control;
1520 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1521 event->param.conn.srq = rep_data->srq;
1522 event->param.conn.qp_num = rep_data->remote_qpn;
1523}
1524
Sean Heftye51060f2006-06-17 20:37:29 -07001525static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1526{
1527 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001528 struct rdma_cm_event event;
1529 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001530
Amir Vadai38ca83a2008-07-22 14:14:23 -07001531 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001532 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001533 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001534 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -07001535 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001536
Sean Heftya1b1b612006-11-30 16:33:14 -08001537 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001538 switch (ib_event->event) {
1539 case IB_CM_REQ_ERROR:
1540 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001541 event.event = RDMA_CM_EVENT_UNREACHABLE;
1542 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001543 break;
1544 case IB_CM_REP_RECEIVED:
Sean Hefty01602f12013-05-29 10:09:20 -07001545 if (id_priv->id.qp) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001546 event.status = cma_rep_recv(id_priv);
1547 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1548 RDMA_CM_EVENT_ESTABLISHED;
Sean Hefty01602f12013-05-29 10:09:20 -07001549 } else {
Sean Heftya1b1b612006-11-30 16:33:14 -08001550 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
Sean Hefty01602f12013-05-29 10:09:20 -07001551 }
Sean Heftya1b1b612006-11-30 16:33:14 -08001552 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1553 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001554 break;
1555 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001556 case IB_CM_USER_ESTABLISHED:
1557 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001558 break;
1559 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001560 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001561 case IB_CM_DREQ_RECEIVED:
1562 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001563 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1564 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001565 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001566 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001567 break;
1568 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001569 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1570 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001571 case IB_CM_MRA_RECEIVED:
1572 /* ignore event */
1573 goto out;
1574 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001575 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001576 event.status = ib_event->param.rej_rcvd.reason;
1577 event.event = RDMA_CM_EVENT_REJECTED;
1578 event.param.conn.private_data = ib_event->private_data;
1579 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001580 break;
1581 default:
Roland Dreier468f2232008-07-14 23:48:47 -07001582 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001583 ib_event->event);
1584 goto out;
1585 }
1586
Sean Heftya1b1b612006-11-30 16:33:14 -08001587 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001588 if (ret) {
1589 /* Destroy the CM ID by returning a non-zero value. */
1590 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001591 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001592 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001593 rdma_destroy_id(&id_priv->id);
1594 return ret;
1595 }
1596out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001597 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001598 return ret;
1599}
1600
Sean Hefty628e5f62006-11-30 16:44:16 -08001601static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001602 struct ib_cm_event *ib_event,
1603 struct net_device *net_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001604{
1605 struct rdma_id_private *id_priv;
1606 struct rdma_cm_id *id;
1607 struct rdma_route *rt;
Haggai Eran0c505f72015-07-30 17:50:19 +03001608 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1609 const __be64 service_id =
1610 ib_event->param.req_rcvd.primary_path->service_id;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001611 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001612
Guy Shapirofa201052015-10-22 15:20:10 +03001613 id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1614 listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001615 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001616 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001617 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001618
Sean Heftyf4753832013-05-29 10:09:14 -07001619 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001620 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1621 (struct sockaddr *)&id->route.addr.dst_addr,
1622 listen_id, ib_event, ss_family, service_id))
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001623 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001624
1625 rt = &id->route;
1626 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1627 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1628 GFP_KERNEL);
1629 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001630 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001631
Sean Heftye51060f2006-06-17 20:37:29 -07001632 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1633 if (rt->num_paths == 2)
1634 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1635
Haggai Eran0b3ca762015-07-30 17:50:24 +03001636 if (net_dev) {
1637 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001638 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001639 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001640 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001641 if (!cma_protocol_roce(listen_id) &&
1642 cma_any_addr(cma_src_addr(id_priv))) {
1643 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1644 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1645 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1646 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
1647 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1648 if (ret)
1649 goto err;
1650 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001651 }
1652 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001653
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001654 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001655 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001656
Krishna Kumar3f168d22006-09-29 12:09:51 -07001657err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001658 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001659 return NULL;
1660}
1661
Sean Hefty628e5f62006-11-30 16:44:16 -08001662static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001663 struct ib_cm_event *ib_event,
1664 struct net_device *net_dev)
Sean Hefty628e5f62006-11-30 16:44:16 -08001665{
1666 struct rdma_id_private *id_priv;
1667 struct rdma_cm_id *id;
Haggai Eran0c505f72015-07-30 17:50:19 +03001668 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
Guy Shapirofa201052015-10-22 15:20:10 +03001669 struct net *net = listen_id->route.addr.dev_addr.net;
Sean Hefty628e5f62006-11-30 16:44:16 -08001670 int ret;
1671
Guy Shapirofa201052015-10-22 15:20:10 +03001672 id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001673 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001674 if (IS_ERR(id))
1675 return NULL;
1676
Sean Heftyf4753832013-05-29 10:09:14 -07001677 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001678 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1679 (struct sockaddr *)&id->route.addr.dst_addr,
1680 listen_id, ib_event, ss_family,
1681 ib_event->param.sidr_req_rcvd.service_id))
Sean Hefty628e5f62006-11-30 16:44:16 -08001682 goto err;
1683
Haggai Eran0b3ca762015-07-30 17:50:24 +03001684 if (net_dev) {
1685 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001686 if (ret)
1687 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001688 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001689 if (!cma_any_addr(cma_src_addr(id_priv))) {
1690 ret = cma_translate_addr(cma_src_addr(id_priv),
1691 &id->route.addr.dev_addr);
1692 if (ret)
1693 goto err;
1694 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001695 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001696
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001697 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001698 return id_priv;
1699err:
1700 rdma_destroy_id(id);
1701 return NULL;
1702}
1703
Sean Heftya1b1b612006-11-30 16:33:14 -08001704static void cma_set_req_event_data(struct rdma_cm_event *event,
1705 struct ib_cm_req_event_param *req_data,
1706 void *private_data, int offset)
1707{
1708 event->param.conn.private_data = private_data + offset;
1709 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1710 event->param.conn.responder_resources = req_data->responder_resources;
1711 event->param.conn.initiator_depth = req_data->initiator_depth;
1712 event->param.conn.flow_control = req_data->flow_control;
1713 event->param.conn.retry_count = req_data->retry_count;
1714 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1715 event->param.conn.srq = req_data->srq;
1716 event->param.conn.qp_num = req_data->remote_qpn;
1717}
1718
Hefty, Sean95954802011-10-06 09:32:33 -07001719static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1720{
Sean Hefty4dd81e82012-06-14 20:49:09 +00001721 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
Hefty, Sean95954802011-10-06 09:32:33 -07001722 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1723 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1724 (id->qp_type == IB_QPT_UD)) ||
1725 (!id->qp_type));
1726}
1727
Sean Heftye51060f2006-06-17 20:37:29 -07001728static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1729{
1730 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001731 struct rdma_cm_event event;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001732 struct net_device *net_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07001733 int offset, ret;
1734
Haggai Eran0b3ca762015-07-30 17:50:24 +03001735 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001736 if (IS_ERR(listen_id))
1737 return PTR_ERR(listen_id);
1738
Haggai Eran0b3ca762015-07-30 17:50:24 +03001739 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1740 ret = -EINVAL;
1741 goto net_dev_put;
1742 }
Hefty, Sean95954802011-10-06 09:32:33 -07001743
Haggai Eran0b3ca762015-07-30 17:50:24 +03001744 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) {
1745 ret = -ECONNABORTED;
1746 goto net_dev_put;
1747 }
Sean Heftye51060f2006-06-17 20:37:29 -07001748
Sean Hefty628e5f62006-11-30 16:44:16 -08001749 memset(&event, 0, sizeof event);
Sean Heftye8160e12013-05-29 10:09:22 -07001750 offset = cma_user_data_offset(listen_id);
Sean Hefty628e5f62006-11-30 16:44:16 -08001751 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001752 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001753 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001754 event.param.ud.private_data = ib_event->private_data + offset;
1755 event.param.ud.private_data_len =
1756 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1757 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001758 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001759 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1760 ib_event->private_data, offset);
1761 }
Sean Heftye51060f2006-06-17 20:37:29 -07001762 if (!conn_id) {
1763 ret = -ENOMEM;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001764 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001765 }
1766
Or Gerlitzde910bd2008-07-14 23:48:53 -07001767 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001768 ret = cma_acquire_dev(conn_id, listen_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301769 if (ret)
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001770 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001771
1772 conn_id->cm_id.ib = cm_id;
1773 cm_id->context = conn_id;
1774 cm_id->cm_handler = cma_ib_handler;
1775
Sean Hefty25ae21a2011-02-23 08:11:32 -08001776 /*
1777 * Protect against the user destroying conn_id from another thread
1778 * until we're done accessing it.
1779 */
1780 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001781 ret = conn_id->id.event_handler(&conn_id->id, &event);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001782 if (ret)
1783 goto err3;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001784 /*
1785 * Acquire mutex to prevent user executing rdma_destroy_id()
1786 * while we're accessing the cm_id.
1787 */
1788 mutex_lock(&lock);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001789 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1790 (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001791 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1792 mutex_unlock(&lock);
1793 mutex_unlock(&conn_id->handler_mutex);
1794 mutex_unlock(&listen_id->handler_mutex);
1795 cma_deref_id(conn_id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001796 if (net_dev)
1797 dev_put(net_dev);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001798 return 0;
1799
1800err3:
1801 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301802 /* Destroy the CM ID by returning a non-zero value. */
1803 conn_id->cm_id.ib = NULL;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001804err2:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001805 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001806 mutex_unlock(&conn_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001807err1:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001808 mutex_unlock(&listen_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001809 if (conn_id)
1810 rdma_destroy_id(&conn_id->id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001811
1812net_dev_put:
1813 if (net_dev)
1814 dev_put(net_dev);
1815
Sean Heftye51060f2006-06-17 20:37:29 -07001816 return ret;
1817}
1818
Sean Heftycf539362013-05-29 10:09:28 -07001819__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
Sean Heftye51060f2006-06-17 20:37:29 -07001820{
Sean Hefty496ce3c2013-05-29 10:09:19 -07001821 if (addr->sa_family == AF_IB)
1822 return ((struct sockaddr_ib *) addr)->sib_sid;
1823
Sean Heftycf539362013-05-29 10:09:28 -07001824 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001825}
Sean Heftycf539362013-05-29 10:09:28 -07001826EXPORT_SYMBOL(rdma_get_service_id);
Sean Heftye51060f2006-06-17 20:37:29 -07001827
Tom Tucker07ebafb2006-08-03 16:02:42 -05001828static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1829{
1830 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001831 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001832 int ret = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05301833 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1834 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001835
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001836 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001837 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001838
Sean Heftybe65f082007-05-07 11:49:12 -07001839 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001840 switch (iw_event->event) {
1841 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001842 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001843 break;
1844 case IW_CM_EVENT_CONNECT_REPLY:
Steve Wise24d44a32013-07-04 16:10:44 +05301845 memcpy(cma_src_addr(id_priv), laddr,
1846 rdma_addr_size(laddr));
1847 memcpy(cma_dst_addr(id_priv), raddr,
1848 rdma_addr_size(raddr));
Steve Wise881a0452006-12-15 16:50:17 -06001849 switch (iw_event->status) {
1850 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001851 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301852 event.param.conn.initiator_depth = iw_event->ird;
1853 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06001854 break;
1855 case -ECONNRESET:
1856 case -ECONNREFUSED:
1857 event.event = RDMA_CM_EVENT_REJECTED;
1858 break;
1859 case -ETIMEDOUT:
1860 event.event = RDMA_CM_EVENT_UNREACHABLE;
1861 break;
1862 default:
1863 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1864 break;
1865 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001866 break;
1867 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001868 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301869 event.param.conn.initiator_depth = iw_event->ird;
1870 event.param.conn.responder_resources = iw_event->ord;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001871 break;
1872 default:
1873 BUG_ON(1);
1874 }
1875
Sean Heftya1b1b612006-11-30 16:33:14 -08001876 event.status = iw_event->status;
1877 event.param.conn.private_data = iw_event->private_data;
1878 event.param.conn.private_data_len = iw_event->private_data_len;
1879 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001880 if (ret) {
1881 /* Destroy the CM ID by returning a non-zero value. */
1882 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001883 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001884 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001885 rdma_destroy_id(&id_priv->id);
1886 return ret;
1887 }
1888
Or Gerlitzde910bd2008-07-14 23:48:53 -07001889 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001890 return ret;
1891}
1892
1893static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1894 struct iw_cm_event *iw_event)
1895{
1896 struct rdma_cm_id *new_cm_id;
1897 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001898 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001899 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001900 struct ib_device_attr attr;
Steve Wise24d44a32013-07-04 16:10:44 +05301901 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1902 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001903
1904 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001905 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001906 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001907
1908 /* Create a new RDMA id for the new IW CM ID */
Guy Shapirofa201052015-10-22 15:20:10 +03001909 new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
1910 listen_id->id.event_handler,
Tom Tucker07ebafb2006-08-03 16:02:42 -05001911 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001912 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07001913 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001914 ret = -ENOMEM;
1915 goto out;
1916 }
1917 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001918 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001919 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001920
Matan Barakdd5f03b2013-12-12 18:03:11 +02001921 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001922 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001923 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001924 rdma_destroy_id(new_cm_id);
1925 goto out;
1926 }
1927
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001928 ret = cma_acquire_dev(conn_id, listen_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001929 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001930 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001931 rdma_destroy_id(new_cm_id);
1932 goto out;
1933 }
1934
1935 conn_id->cm_id.iw = cm_id;
1936 cm_id->context = conn_id;
1937 cm_id->cm_handler = cma_iw_handler;
1938
Steve Wise24d44a32013-07-04 16:10:44 +05301939 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
1940 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
Tom Tucker07ebafb2006-08-03 16:02:42 -05001941
Steve Wise8d8293c2007-10-29 11:34:05 -05001942 ret = ib_query_device(conn_id->id.device, &attr);
1943 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001944 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001945 rdma_destroy_id(new_cm_id);
1946 goto out;
1947 }
1948
Sean Heftya1b1b612006-11-30 16:33:14 -08001949 memset(&event, 0, sizeof event);
1950 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1951 event.param.conn.private_data = iw_event->private_data;
1952 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301953 event.param.conn.initiator_depth = iw_event->ird;
1954 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08001955
1956 /*
1957 * Protect against the user destroying conn_id from another thread
1958 * until we're done accessing it.
1959 */
1960 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001961 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001962 if (ret) {
1963 /* User wants to destroy the CM ID */
1964 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001965 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001966 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001967 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001968 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001969 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001970 }
1971
Or Gerlitzde910bd2008-07-14 23:48:53 -07001972 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001973 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001974
Tom Tucker07ebafb2006-08-03 16:02:42 -05001975out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001976 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001977 return ret;
1978}
1979
Sean Heftye51060f2006-06-17 20:37:29 -07001980static int cma_ib_listen(struct rdma_id_private *id_priv)
1981{
Sean Heftye51060f2006-06-17 20:37:29 -07001982 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001983 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07001984 __be64 svc_id;
Sean Heftye51060f2006-06-17 20:37:29 -07001985
Sean Heftyf4753832013-05-29 10:09:14 -07001986 addr = cma_src_addr(id_priv);
Sean Heftycf539362013-05-29 10:09:28 -07001987 svc_id = rdma_get_service_id(&id_priv->id, addr);
Haggai Eran51efe392015-07-30 17:50:25 +03001988 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
1989 if (IS_ERR(id))
1990 return PTR_ERR(id);
1991 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07001992
Haggai Eran51efe392015-07-30 17:50:25 +03001993 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001994}
1995
Tom Tucker07ebafb2006-08-03 16:02:42 -05001996static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1997{
1998 int ret;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001999 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002000
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002001 id = iw_create_cm_id(id_priv->id.device,
2002 iw_conn_req_handler,
2003 id_priv);
2004 if (IS_ERR(id))
2005 return PTR_ERR(id);
2006
Steve Wise68cdba02015-05-18 15:27:10 -05002007 id->tos = id_priv->tos;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002008 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002009
Steve Wise24d44a32013-07-04 16:10:44 +05302010 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2011 rdma_addr_size(cma_src_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002012
2013 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2014
2015 if (ret) {
2016 iw_destroy_cm_id(id_priv->cm_id.iw);
2017 id_priv->cm_id.iw = NULL;
2018 }
2019
2020 return ret;
2021}
2022
Sean Heftye51060f2006-06-17 20:37:29 -07002023static int cma_listen_handler(struct rdma_cm_id *id,
2024 struct rdma_cm_event *event)
2025{
2026 struct rdma_id_private *id_priv = id->context;
2027
2028 id->context = id_priv->id.context;
2029 id->event_handler = id_priv->id.event_handler;
2030 return id_priv->id.event_handler(id, event);
2031}
2032
2033static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2034 struct cma_device *cma_dev)
2035{
2036 struct rdma_id_private *dev_id_priv;
2037 struct rdma_cm_id *id;
Guy Shapirofa201052015-10-22 15:20:10 +03002038 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07002039 int ret;
2040
Michael Wang72219cea2015-05-05 14:50:34 +02002041 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
Sean Hefty94d0c932013-05-29 10:09:24 -07002042 return;
2043
Guy Shapirofa201052015-10-22 15:20:10 +03002044 id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
Sean Heftyb26f9b92010-04-01 17:08:41 +00002045 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07002046 if (IS_ERR(id))
2047 return;
2048
2049 dev_id_priv = container_of(id, struct rdma_id_private, id);
2050
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002051 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftyf4753832013-05-29 10:09:14 -07002052 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2053 rdma_addr_size(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002054
2055 cma_attach_to_dev(dev_id_priv, cma_dev);
2056 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07002057 atomic_inc(&id_priv->refcount);
2058 dev_id_priv->internal_id = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002059 dev_id_priv->afonly = id_priv->afonly;
Sean Heftye51060f2006-06-17 20:37:29 -07002060
2061 ret = rdma_listen(id, id_priv->backlog);
2062 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07002063 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07002064 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07002065}
2066
2067static void cma_listen_on_all(struct rdma_id_private *id_priv)
2068{
2069 struct cma_device *cma_dev;
2070
2071 mutex_lock(&lock);
2072 list_add_tail(&id_priv->list, &listen_any_list);
2073 list_for_each_entry(cma_dev, &dev_list, list)
2074 cma_listen_on_dev(id_priv, cma_dev);
2075 mutex_unlock(&lock);
2076}
2077
Sean Heftya81c9942007-08-08 15:51:06 -07002078void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2079{
2080 struct rdma_id_private *id_priv;
2081
2082 id_priv = container_of(id, struct rdma_id_private, id);
2083 id_priv->tos = (u8) tos;
2084}
2085EXPORT_SYMBOL(rdma_set_service_type);
2086
Sean Heftye51060f2006-06-17 20:37:29 -07002087static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2088 void *context)
2089{
2090 struct cma_work *work = context;
2091 struct rdma_route *route;
2092
2093 route = &work->id->id.route;
2094
2095 if (!status) {
2096 route->num_paths = 1;
2097 *route->path_rec = *path_rec;
2098 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002099 work->old_state = RDMA_CM_ROUTE_QUERY;
2100 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002101 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07002102 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07002103 }
2104
2105 queue_work(cma_wq, &work->work);
2106}
2107
2108static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2109 struct cma_work *work)
2110{
Sean Heftyf4753832013-05-29 10:09:14 -07002111 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002112 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07002113 ib_sa_comp_mask comp_mask;
2114 struct sockaddr_in6 *sin6;
Sean Heftyf68194c2013-05-29 10:09:18 -07002115 struct sockaddr_ib *sib;
Sean Heftye51060f2006-06-17 20:37:29 -07002116
2117 memset(&path_rec, 0, sizeof path_rec);
Sean Heftyf4753832013-05-29 10:09:14 -07002118 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2119 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2120 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07002121 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08002122 path_rec.reversible = 1;
Sean Heftycf539362013-05-29 10:09:28 -07002123 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftya81c9942007-08-08 15:51:06 -07002124
2125 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2126 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2127 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2128
Sean Heftyf68194c2013-05-29 10:09:18 -07002129 switch (cma_family(id_priv)) {
2130 case AF_INET:
Sean Heftya81c9942007-08-08 15:51:06 -07002131 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2132 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002133 break;
2134 case AF_INET6:
Sean Heftyf4753832013-05-29 10:09:14 -07002135 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
Sean Heftya81c9942007-08-08 15:51:06 -07002136 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2137 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002138 break;
2139 case AF_IB:
2140 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2141 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2142 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2143 break;
Sean Heftya81c9942007-08-08 15:51:06 -07002144 }
Sean Heftye51060f2006-06-17 20:37:29 -07002145
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002146 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07002147 id_priv->id.port_num, &path_rec,
2148 comp_mask, timeout_ms,
2149 GFP_KERNEL, cma_query_handler,
2150 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07002151
2152 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2153}
2154
David Howellsc4028952006-11-22 14:57:56 +00002155static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07002156{
David Howellsc4028952006-11-22 14:57:56 +00002157 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07002158 struct rdma_id_private *id_priv = work->id;
2159 int destroy = 0;
2160
Or Gerlitzde910bd2008-07-14 23:48:53 -07002161 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002162 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2163 goto out;
2164
2165 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002166 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07002167 destroy = 1;
2168 }
2169out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002170 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002171 cma_deref_id(id_priv);
2172 if (destroy)
2173 rdma_destroy_id(&id_priv->id);
2174 kfree(work);
2175}
2176
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002177static void cma_ndev_work_handler(struct work_struct *_work)
2178{
2179 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2180 struct rdma_id_private *id_priv = work->id;
2181 int destroy = 0;
2182
2183 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002184 if (id_priv->state == RDMA_CM_DESTROYING ||
2185 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002186 goto out;
2187
2188 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002189 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002190 destroy = 1;
2191 }
2192
2193out:
2194 mutex_unlock(&id_priv->handler_mutex);
2195 cma_deref_id(id_priv);
2196 if (destroy)
2197 rdma_destroy_id(&id_priv->id);
2198 kfree(work);
2199}
2200
Sean Heftye51060f2006-06-17 20:37:29 -07002201static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2202{
2203 struct rdma_route *route = &id_priv->id.route;
2204 struct cma_work *work;
2205 int ret;
2206
2207 work = kzalloc(sizeof *work, GFP_KERNEL);
2208 if (!work)
2209 return -ENOMEM;
2210
2211 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002212 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002213 work->old_state = RDMA_CM_ROUTE_QUERY;
2214 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002215 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2216
2217 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2218 if (!route->path_rec) {
2219 ret = -ENOMEM;
2220 goto err1;
2221 }
2222
2223 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2224 if (ret)
2225 goto err2;
2226
2227 return 0;
2228err2:
2229 kfree(route->path_rec);
2230 route->path_rec = NULL;
2231err1:
2232 kfree(work);
2233 return ret;
2234}
2235
2236int rdma_set_ib_paths(struct rdma_cm_id *id,
2237 struct ib_sa_path_rec *path_rec, int num_paths)
2238{
2239 struct rdma_id_private *id_priv;
2240 int ret;
2241
2242 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002243 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2244 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07002245 return -EINVAL;
2246
Julia Lawall9893e742010-05-15 23:22:38 +02002247 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2248 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002249 if (!id->route.path_rec) {
2250 ret = -ENOMEM;
2251 goto err;
2252 }
2253
Sean Heftyae2d9292010-03-25 19:12:36 +00002254 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07002255 return 0;
2256err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002257 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002258 return ret;
2259}
2260EXPORT_SYMBOL(rdma_set_ib_paths);
2261
Tom Tucker07ebafb2006-08-03 16:02:42 -05002262static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2263{
2264 struct cma_work *work;
2265
2266 work = kzalloc(sizeof *work, GFP_KERNEL);
2267 if (!work)
2268 return -ENOMEM;
2269
2270 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002271 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002272 work->old_state = RDMA_CM_ROUTE_QUERY;
2273 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002274 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2275 queue_work(cma_wq, &work->work);
2276 return 0;
2277}
2278
Eyal Perryeb072c42013-11-06 15:37:24 +02002279static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2280{
2281 int prio;
2282 struct net_device *dev;
2283
2284 prio = rt_tos2priority(tos);
2285 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
2286 vlan_dev_real_dev(ndev) : ndev;
2287
2288 if (dev->num_tc)
2289 return netdev_get_prio_tc_map(dev, prio);
2290
2291#if IS_ENABLED(CONFIG_VLAN_8021Q)
2292 if (ndev->priv_flags & IFF_802_1Q_VLAN)
2293 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2294 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2295#endif
2296 return 0;
2297}
2298
Eli Cohen3c86aa72010-10-13 21:26:51 +02002299static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2300{
2301 struct rdma_route *route = &id_priv->id.route;
2302 struct rdma_addr *addr = &route->addr;
2303 struct cma_work *work;
2304 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002305 struct net_device *ndev = NULL;
Matan Barakdd5f03b2013-12-12 18:03:11 +02002306
Eli Cohen3c86aa72010-10-13 21:26:51 +02002307
Eli Cohen3c86aa72010-10-13 21:26:51 +02002308 work = kzalloc(sizeof *work, GFP_KERNEL);
2309 if (!work)
2310 return -ENOMEM;
2311
2312 work->id = id_priv;
2313 INIT_WORK(&work->work, cma_work_handler);
2314
2315 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2316 if (!route->path_rec) {
2317 ret = -ENOMEM;
2318 goto err1;
2319 }
2320
2321 route->num_paths = 1;
2322
Matan Barakabae1b72015-10-15 18:38:49 +03002323 if (addr->dev_addr.bound_dev_if) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02002324 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
Matan Barakabae1b72015-10-15 18:38:49 +03002325 route->path_rec->net = &init_net;
2326 route->path_rec->ifindex = addr->dev_addr.bound_dev_if;
2327 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02002328 if (!ndev) {
2329 ret = -ENODEV;
2330 goto err2;
2331 }
2332
Matan Barakdd5f03b2013-12-12 18:03:11 +02002333 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002334
Moni Shoua7b856272013-12-12 18:03:12 +02002335 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2336 &route->path_rec->sgid);
2337 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2338 &route->path_rec->dgid);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002339
2340 route->path_rec->hop_limit = 1;
2341 route->path_rec->reversible = 1;
2342 route->path_rec->pkey = cpu_to_be16(0xffff);
2343 route->path_rec->mtu_selector = IB_SA_EQ;
Eyal Perryeb072c42013-11-06 15:37:24 +02002344 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
Eli Cohen3c86aa72010-10-13 21:26:51 +02002345 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2346 route->path_rec->rate_selector = IB_SA_EQ;
2347 route->path_rec->rate = iboe_get_rate(ndev);
2348 dev_put(ndev);
2349 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2350 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2351 if (!route->path_rec->mtu) {
2352 ret = -EINVAL;
2353 goto err2;
2354 }
2355
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002356 work->old_state = RDMA_CM_ROUTE_QUERY;
2357 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002358 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2359 work->event.status = 0;
2360
2361 queue_work(cma_wq, &work->work);
2362
2363 return 0;
2364
2365err2:
2366 kfree(route->path_rec);
2367 route->path_rec = NULL;
2368err1:
2369 kfree(work);
2370 return ret;
2371}
2372
Sean Heftye51060f2006-06-17 20:37:29 -07002373int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2374{
2375 struct rdma_id_private *id_priv;
2376 int ret;
2377
2378 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002379 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002380 return -EINVAL;
2381
2382 atomic_inc(&id_priv->refcount);
Michael Wangfe53ba22015-05-05 14:50:36 +02002383 if (rdma_cap_ib_sa(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002384 ret = cma_resolve_ib_route(id_priv, timeout_ms);
Ira Weiny5d9fb042015-05-14 15:01:46 -04002385 else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002386 ret = cma_resolve_iboe_route(id_priv);
2387 else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002388 ret = cma_resolve_iw_route(id_priv, timeout_ms);
Michael Wangc72f2182015-05-05 14:50:28 +02002389 else
Sean Heftye51060f2006-06-17 20:37:29 -07002390 ret = -ENOSYS;
Michael Wangc72f2182015-05-05 14:50:28 +02002391
Sean Heftye51060f2006-06-17 20:37:29 -07002392 if (ret)
2393 goto err;
2394
2395 return 0;
2396err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002397 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002398 cma_deref_id(id_priv);
2399 return ret;
2400}
2401EXPORT_SYMBOL(rdma_resolve_route);
2402
Sean Hefty6a3e3622013-05-29 10:09:13 -07002403static void cma_set_loopback(struct sockaddr *addr)
2404{
2405 switch (addr->sa_family) {
2406 case AF_INET:
2407 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2408 break;
2409 case AF_INET6:
2410 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2411 0, 0, 0, htonl(1));
2412 break;
2413 default:
2414 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2415 0, 0, 0, htonl(1));
2416 break;
2417 }
2418}
2419
Sean Heftye51060f2006-06-17 20:37:29 -07002420static int cma_bind_loopback(struct rdma_id_private *id_priv)
2421{
Sean Heftyb0569e42013-05-29 10:09:15 -07002422 struct cma_device *cma_dev, *cur_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07002423 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002424 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002425 u16 pkey;
2426 int ret;
2427 u8 p;
2428
Sean Heftyb0569e42013-05-29 10:09:15 -07002429 cma_dev = NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07002430 mutex_lock(&lock);
Sean Heftyb0569e42013-05-29 10:09:15 -07002431 list_for_each_entry(cur_dev, &dev_list, list) {
2432 if (cma_family(id_priv) == AF_IB &&
Michael Wang72219cea2015-05-05 14:50:34 +02002433 !rdma_cap_ib_cm(cur_dev->device, 1))
Sean Heftyb0569e42013-05-29 10:09:15 -07002434 continue;
2435
2436 if (!cma_dev)
2437 cma_dev = cur_dev;
2438
2439 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2440 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2441 port_attr.state == IB_PORT_ACTIVE) {
2442 cma_dev = cur_dev;
2443 goto port_found;
2444 }
2445 }
2446 }
2447
2448 if (!cma_dev) {
Sean Heftye51060f2006-06-17 20:37:29 -07002449 ret = -ENODEV;
2450 goto out;
2451 }
Krishna Kumare82153b2006-10-16 10:09:01 +05302452
2453 p = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07002454
2455port_found:
Matan Barak55ee3ab2015-10-15 18:38:45 +03002456 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002457 if (ret)
2458 goto out;
2459
2460 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2461 if (ret)
2462 goto out;
2463
Sean Hefty6f8372b2009-11-19 13:26:06 -08002464 id_priv->id.route.addr.dev_addr.dev_type =
Michael Wang21655af2015-05-05 14:50:27 +02002465 (rdma_protocol_ib(cma_dev->device, p)) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08002466 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2467
2468 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002469 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2470 id_priv->id.port_num = p;
2471 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf4753832013-05-29 10:09:14 -07002472 cma_set_loopback(cma_src_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002473out:
2474 mutex_unlock(&lock);
2475 return ret;
2476}
2477
2478static void addr_handler(int status, struct sockaddr *src_addr,
2479 struct rdma_dev_addr *dev_addr, void *context)
2480{
2481 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08002482 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002483
Sean Heftya1b1b612006-11-30 16:33:14 -08002484 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002485 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002486 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2487 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07002488 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07002489
Moni Shoua7b856272013-12-12 18:03:12 +02002490 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
Sean Hefty61a73c72006-09-01 15:33:55 -07002491 if (!status && !id_priv->cma_dev)
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002492 status = cma_acquire_dev(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002493
2494 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002495 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2496 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002497 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08002498 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2499 event.status = status;
Moni Shoua7b856272013-12-12 18:03:12 +02002500 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08002501 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002502
Sean Heftya1b1b612006-11-30 16:33:14 -08002503 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002504 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002505 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002506 cma_deref_id(id_priv);
2507 rdma_destroy_id(&id_priv->id);
2508 return;
2509 }
2510out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002511 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002512 cma_deref_id(id_priv);
2513}
2514
2515static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2516{
2517 struct cma_work *work;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002518 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002519 int ret;
2520
2521 work = kzalloc(sizeof *work, GFP_KERNEL);
2522 if (!work)
2523 return -ENOMEM;
2524
2525 if (!id_priv->cma_dev) {
2526 ret = cma_bind_loopback(id_priv);
2527 if (ret)
2528 goto err;
2529 }
2530
Sean Hefty6f8372b2009-11-19 13:26:06 -08002531 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2532 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002533
Sean Heftye51060f2006-06-17 20:37:29 -07002534 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002535 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002536 work->old_state = RDMA_CM_ADDR_QUERY;
2537 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002538 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2539 queue_work(cma_wq, &work->work);
2540 return 0;
2541err:
2542 kfree(work);
2543 return ret;
2544}
2545
Sean Heftyf17df3b2013-05-29 10:09:17 -07002546static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2547{
2548 struct cma_work *work;
2549 int ret;
2550
2551 work = kzalloc(sizeof *work, GFP_KERNEL);
2552 if (!work)
2553 return -ENOMEM;
2554
2555 if (!id_priv->cma_dev) {
2556 ret = cma_resolve_ib_dev(id_priv);
2557 if (ret)
2558 goto err;
2559 }
2560
2561 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2562 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2563
2564 work->id = id_priv;
2565 INIT_WORK(&work->work, cma_work_handler);
2566 work->old_state = RDMA_CM_ADDR_QUERY;
2567 work->new_state = RDMA_CM_ADDR_RESOLVED;
2568 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2569 queue_work(cma_wq, &work->work);
2570 return 0;
2571err:
2572 kfree(work);
2573 return ret;
2574}
2575
Sean Heftye51060f2006-06-17 20:37:29 -07002576static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2577 struct sockaddr *dst_addr)
2578{
Sean Heftyd14714d2009-11-19 16:46:25 -08002579 if (!src_addr || !src_addr->sa_family) {
2580 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002581 src_addr->sa_family = dst_addr->sa_family;
2582 if (dst_addr->sa_family == AF_INET6) {
Spencer Baugh6c26a772015-08-13 12:19:10 -07002583 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2584 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2585 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2586 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2587 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002588 } else if (dst_addr->sa_family == AF_IB) {
2589 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2590 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
Sean Heftyd14714d2009-11-19 16:46:25 -08002591 }
2592 }
2593 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002594}
2595
2596int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2597 struct sockaddr *dst_addr, int timeout_ms)
2598{
2599 struct rdma_id_private *id_priv;
2600 int ret;
2601
2602 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002603 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002604 ret = cma_bind_addr(id, src_addr, dst_addr);
2605 if (ret)
2606 return ret;
2607 }
2608
Sean Hefty4ae71522013-05-29 10:09:16 -07002609 if (cma_family(id_priv) != dst_addr->sa_family)
2610 return -EINVAL;
2611
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002612 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002613 return -EINVAL;
2614
2615 atomic_inc(&id_priv->refcount);
Sean Heftyf4753832013-05-29 10:09:14 -07002616 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
Sean Heftyf17df3b2013-05-29 10:09:17 -07002617 if (cma_any_addr(dst_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002618 ret = cma_resolve_loopback(id_priv);
Sean Heftyf17df3b2013-05-29 10:09:17 -07002619 } else {
2620 if (dst_addr->sa_family == AF_IB) {
2621 ret = cma_resolve_ib_addr(id_priv);
2622 } else {
2623 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2624 dst_addr, &id->route.addr.dev_addr,
2625 timeout_ms, addr_handler, id_priv);
2626 }
2627 }
Sean Heftye51060f2006-06-17 20:37:29 -07002628 if (ret)
2629 goto err;
2630
2631 return 0;
2632err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002633 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002634 cma_deref_id(id_priv);
2635 return ret;
2636}
2637EXPORT_SYMBOL(rdma_resolve_addr);
2638
Hefty, Seana9bb7912011-05-09 22:06:10 -07002639int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2640{
2641 struct rdma_id_private *id_priv;
2642 unsigned long flags;
2643 int ret;
2644
2645 id_priv = container_of(id, struct rdma_id_private, id);
2646 spin_lock_irqsave(&id_priv->lock, flags);
Sean Heftyc8dea2f92013-05-29 10:09:08 -07002647 if (reuse || id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002648 id_priv->reuseaddr = reuse;
2649 ret = 0;
2650 } else {
2651 ret = -EINVAL;
2652 }
2653 spin_unlock_irqrestore(&id_priv->lock, flags);
2654 return ret;
2655}
2656EXPORT_SYMBOL(rdma_set_reuseaddr);
2657
Sean Hefty68602122012-06-14 20:31:39 +00002658int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2659{
2660 struct rdma_id_private *id_priv;
2661 unsigned long flags;
2662 int ret;
2663
2664 id_priv = container_of(id, struct rdma_id_private, id);
2665 spin_lock_irqsave(&id_priv->lock, flags);
2666 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2667 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2668 id_priv->afonly = afonly;
2669 ret = 0;
2670 } else {
2671 ret = -EINVAL;
2672 }
2673 spin_unlock_irqrestore(&id_priv->lock, flags);
2674 return ret;
2675}
2676EXPORT_SYMBOL(rdma_set_afonly);
2677
Sean Heftye51060f2006-06-17 20:37:29 -07002678static void cma_bind_port(struct rdma_bind_list *bind_list,
2679 struct rdma_id_private *id_priv)
2680{
Sean Hefty58afdcb2013-05-29 10:09:11 -07002681 struct sockaddr *addr;
2682 struct sockaddr_ib *sib;
2683 u64 sid, mask;
2684 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07002685
Sean Heftyf4753832013-05-29 10:09:14 -07002686 addr = cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002687 port = htons(bind_list->port);
2688
2689 switch (addr->sa_family) {
2690 case AF_INET:
2691 ((struct sockaddr_in *) addr)->sin_port = port;
2692 break;
2693 case AF_INET6:
2694 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2695 break;
2696 case AF_IB:
2697 sib = (struct sockaddr_ib *) addr;
2698 sid = be64_to_cpu(sib->sib_sid);
2699 mask = be64_to_cpu(sib->sib_sid_mask);
2700 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2701 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2702 break;
2703 }
Sean Heftye51060f2006-06-17 20:37:29 -07002704 id_priv->bind_list = bind_list;
2705 hlist_add_head(&id_priv->node, &bind_list->owners);
2706}
2707
Haggai Eranaac978e2015-07-30 17:50:20 +03002708static int cma_alloc_port(enum rdma_port_space ps,
2709 struct rdma_id_private *id_priv, unsigned short snum)
Sean Heftye51060f2006-06-17 20:37:29 -07002710{
2711 struct rdma_bind_list *bind_list;
Tejun Heo3b069c52013-02-27 17:04:16 -08002712 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002713
Sean Heftycb164b82007-03-05 12:50:17 -08002714 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002715 if (!bind_list)
2716 return -ENOMEM;
2717
Guy Shapirofa201052015-10-22 15:20:10 +03002718 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2719 snum);
Tejun Heo3b069c52013-02-27 17:04:16 -08002720 if (ret < 0)
2721 goto err;
Sean Heftye51060f2006-06-17 20:37:29 -07002722
2723 bind_list->ps = ps;
Tejun Heo3b069c52013-02-27 17:04:16 -08002724 bind_list->port = (unsigned short)ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002725 cma_bind_port(bind_list, id_priv);
2726 return 0;
Tejun Heo3b069c52013-02-27 17:04:16 -08002727err:
Sean Heftyaedec082007-01-29 16:41:23 -08002728 kfree(bind_list);
Tejun Heo3b069c52013-02-27 17:04:16 -08002729 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002730}
2731
Haggai Eranaac978e2015-07-30 17:50:20 +03002732static int cma_alloc_any_port(enum rdma_port_space ps,
2733 struct rdma_id_private *id_priv)
Sean Heftyaedec082007-01-29 16:41:23 -08002734{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002735 static unsigned int last_used_port;
2736 int low, high, remaining;
2737 unsigned int rover;
Guy Shapirofa201052015-10-22 15:20:10 +03002738 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftyaedec082007-01-29 16:41:23 -08002739
Guy Shapirofa201052015-10-22 15:20:10 +03002740 inet_get_local_port_range(net, &low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002741 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002742 rover = prandom_u32() % remaining + low;
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002743retry:
2744 if (last_used_port != rover &&
Guy Shapirofa201052015-10-22 15:20:10 +03002745 !cma_ps_find(net, ps, (unsigned short)rover)) {
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002746 int ret = cma_alloc_port(ps, id_priv, rover);
2747 /*
2748 * Remember previously used port number in order to avoid
2749 * re-using same port immediately after it is closed.
2750 */
2751 if (!ret)
2752 last_used_port = rover;
2753 if (ret != -EADDRNOTAVAIL)
2754 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002755 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002756 if (--remaining) {
2757 rover++;
2758 if ((rover < low) || (rover > high))
2759 rover = low;
2760 goto retry;
2761 }
2762 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002763}
2764
Hefty, Seana9bb7912011-05-09 22:06:10 -07002765/*
2766 * Check that the requested port is available. This is called when trying to
2767 * bind to a specific port, or when trying to listen on a bound port. In
2768 * the latter case, the provided id_priv may already be on the bind_list, but
2769 * we still need to check that it's okay to start listening.
2770 */
2771static int cma_check_port(struct rdma_bind_list *bind_list,
2772 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002773{
2774 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002775 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002776
Sean Heftyf4753832013-05-29 10:09:14 -07002777 addr = cma_src_addr(id_priv);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002778 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002779 if (id_priv == cur_id)
2780 continue;
2781
Sean Hefty5b0ec992012-06-14 20:31:39 +00002782 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2783 cur_id->reuseaddr)
2784 continue;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002785
Sean Heftyf4753832013-05-29 10:09:14 -07002786 cur_addr = cma_src_addr(cur_id);
Sean Hefty5b0ec992012-06-14 20:31:39 +00002787 if (id_priv->afonly && cur_id->afonly &&
2788 (addr->sa_family != cur_addr->sa_family))
2789 continue;
2790
2791 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2792 return -EADDRNOTAVAIL;
2793
2794 if (!cma_addr_cmp(addr, cur_addr))
2795 return -EADDRINUSE;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002796 }
2797 return 0;
2798}
2799
Haggai Eranaac978e2015-07-30 17:50:20 +03002800static int cma_use_port(enum rdma_port_space ps,
2801 struct rdma_id_private *id_priv)
Hefty, Seana9bb7912011-05-09 22:06:10 -07002802{
2803 struct rdma_bind_list *bind_list;
2804 unsigned short snum;
2805 int ret;
2806
Sean Heftyf4753832013-05-29 10:09:14 -07002807 snum = ntohs(cma_port(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002808 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2809 return -EACCES;
2810
Guy Shapirofa201052015-10-22 15:20:10 +03002811 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002812 if (!bind_list) {
2813 ret = cma_alloc_port(ps, id_priv, snum);
2814 } else {
2815 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2816 if (!ret)
2817 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002818 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07002819 return ret;
2820}
Sean Heftye51060f2006-06-17 20:37:29 -07002821
Hefty, Seana9bb7912011-05-09 22:06:10 -07002822static int cma_bind_listen(struct rdma_id_private *id_priv)
2823{
2824 struct rdma_bind_list *bind_list = id_priv->bind_list;
2825 int ret = 0;
2826
2827 mutex_lock(&lock);
2828 if (bind_list->owners.first->next)
2829 ret = cma_check_port(bind_list, id_priv, 0);
2830 mutex_unlock(&lock);
2831 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002832}
2833
Haggai Eranaac978e2015-07-30 17:50:20 +03002834static enum rdma_port_space cma_select_inet_ps(
2835 struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002836{
2837 switch (id_priv->id.ps) {
Sean Hefty58afdcb2013-05-29 10:09:11 -07002838 case RDMA_PS_TCP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002839 case RDMA_PS_UDP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002840 case RDMA_PS_IPOIB:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002841 case RDMA_PS_IB:
Haggai Eranaac978e2015-07-30 17:50:20 +03002842 return id_priv->id.ps;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002843 default:
Haggai Eranaac978e2015-07-30 17:50:20 +03002844
2845 return 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002846 }
2847}
2848
Haggai Eranaac978e2015-07-30 17:50:20 +03002849static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002850{
Haggai Eranaac978e2015-07-30 17:50:20 +03002851 enum rdma_port_space ps = 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002852 struct sockaddr_ib *sib;
2853 u64 sid_ps, mask, sid;
2854
Sean Heftyf4753832013-05-29 10:09:14 -07002855 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002856 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
2857 sid = be64_to_cpu(sib->sib_sid) & mask;
2858
2859 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
2860 sid_ps = RDMA_IB_IP_PS_IB;
Haggai Eranaac978e2015-07-30 17:50:20 +03002861 ps = RDMA_PS_IB;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002862 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
2863 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
2864 sid_ps = RDMA_IB_IP_PS_TCP;
Haggai Eranaac978e2015-07-30 17:50:20 +03002865 ps = RDMA_PS_TCP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002866 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
2867 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
2868 sid_ps = RDMA_IB_IP_PS_UDP;
Haggai Eranaac978e2015-07-30 17:50:20 +03002869 ps = RDMA_PS_UDP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002870 }
2871
2872 if (ps) {
2873 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
2874 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
2875 be64_to_cpu(sib->sib_sid_mask));
2876 }
2877 return ps;
2878}
2879
Sean Heftye51060f2006-06-17 20:37:29 -07002880static int cma_get_port(struct rdma_id_private *id_priv)
2881{
Haggai Eranaac978e2015-07-30 17:50:20 +03002882 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -07002883 int ret;
2884
Sean Heftyf4753832013-05-29 10:09:14 -07002885 if (cma_family(id_priv) != AF_IB)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002886 ps = cma_select_inet_ps(id_priv);
2887 else
2888 ps = cma_select_ib_ps(id_priv);
2889 if (!ps)
Sean Heftye51060f2006-06-17 20:37:29 -07002890 return -EPROTONOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07002891
2892 mutex_lock(&lock);
Sean Heftyf4753832013-05-29 10:09:14 -07002893 if (cma_any_port(cma_src_addr(id_priv)))
Sean Heftyaedec082007-01-29 16:41:23 -08002894 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002895 else
2896 ret = cma_use_port(ps, id_priv);
2897 mutex_unlock(&lock);
2898
2899 return ret;
2900}
2901
Sean Heftyd14714d2009-11-19 16:46:25 -08002902static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2903 struct sockaddr *addr)
2904{
Roland Dreierd90f9b32012-07-05 22:39:34 -07002905#if IS_ENABLED(CONFIG_IPV6)
Sean Heftyd14714d2009-11-19 16:46:25 -08002906 struct sockaddr_in6 *sin6;
2907
2908 if (addr->sa_family != AF_INET6)
2909 return 0;
2910
2911 sin6 = (struct sockaddr_in6 *) addr;
Somnath Kotur5462edd2013-11-16 09:30:01 +05302912
2913 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
2914 return 0;
2915
2916 if (!sin6->sin6_scope_id)
Sean Heftyd14714d2009-11-19 16:46:25 -08002917 return -EINVAL;
2918
2919 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2920#endif
2921 return 0;
2922}
2923
Hefty, Seana9bb7912011-05-09 22:06:10 -07002924int rdma_listen(struct rdma_cm_id *id, int backlog)
2925{
2926 struct rdma_id_private *id_priv;
2927 int ret;
2928
2929 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002930 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftyf4753832013-05-29 10:09:14 -07002931 id->route.addr.src_addr.ss_family = AF_INET;
2932 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
Hefty, Seana9bb7912011-05-09 22:06:10 -07002933 if (ret)
2934 return ret;
2935 }
2936
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002937 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07002938 return -EINVAL;
2939
2940 if (id_priv->reuseaddr) {
2941 ret = cma_bind_listen(id_priv);
2942 if (ret)
2943 goto err;
2944 }
2945
2946 id_priv->backlog = backlog;
2947 if (id->device) {
Michael Wang72219cea2015-05-05 14:50:34 +02002948 if (rdma_cap_ib_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002949 ret = cma_ib_listen(id_priv);
2950 if (ret)
2951 goto err;
Michael Wang04215332015-05-05 14:50:35 +02002952 } else if (rdma_cap_iw_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002953 ret = cma_iw_listen(id_priv, backlog);
2954 if (ret)
2955 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02002956 } else {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002957 ret = -ENOSYS;
2958 goto err;
2959 }
2960 } else
2961 cma_listen_on_all(id_priv);
2962
2963 return 0;
2964err:
2965 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002966 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002967 return ret;
2968}
2969EXPORT_SYMBOL(rdma_listen);
2970
Sean Heftye51060f2006-06-17 20:37:29 -07002971int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2972{
2973 struct rdma_id_private *id_priv;
2974 int ret;
2975
Sean Hefty680f9202013-05-29 10:09:12 -07002976 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
2977 addr->sa_family != AF_IB)
Sean Heftye51060f2006-06-17 20:37:29 -07002978 return -EAFNOSUPPORT;
2979
2980 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002981 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002982 return -EINVAL;
2983
Sean Heftyd14714d2009-11-19 16:46:25 -08002984 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2985 if (ret)
2986 goto err1;
2987
Moni Shoua7b856272013-12-12 18:03:12 +02002988 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
Sean Hefty8523c042010-02-08 16:41:15 -08002989 if (!cma_any_addr(addr)) {
Sean Hefty680f9202013-05-29 10:09:12 -07002990 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002991 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002992 goto err1;
2993
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002994 ret = cma_acquire_dev(id_priv, NULL);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002995 if (ret)
2996 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002997 }
2998
Sean Hefty68602122012-06-14 20:31:39 +00002999 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3000 if (addr->sa_family == AF_INET)
3001 id_priv->afonly = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00003002#if IS_ENABLED(CONFIG_IPV6)
Guy Shapirofa201052015-10-22 15:20:10 +03003003 else if (addr->sa_family == AF_INET6) {
3004 struct net *net = id_priv->id.route.addr.dev_addr.net;
3005
3006 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3007 }
Sean Hefty5b0ec992012-06-14 20:31:39 +00003008#endif
Sean Hefty68602122012-06-14 20:31:39 +00003009 }
Sean Heftye51060f2006-06-17 20:37:29 -07003010 ret = cma_get_port(id_priv);
3011 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07003012 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07003013
3014 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07003015err2:
Sean Heftya396d432011-02-23 09:05:39 -08003016 if (id_priv->cma_dev)
3017 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07003018err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003019 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07003020 return ret;
3021}
3022EXPORT_SYMBOL(rdma_bind_addr);
3023
Sean Heftyf4753832013-05-29 10:09:14 -07003024static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07003025{
Sean Heftye51060f2006-06-17 20:37:29 -07003026 struct cma_hdr *cma_hdr;
Sean Heftye51060f2006-06-17 20:37:29 -07003027
Sean Hefty01602f12013-05-29 10:09:20 -07003028 cma_hdr = hdr;
3029 cma_hdr->cma_version = CMA_VERSION;
Sean Heftyf4753832013-05-29 10:09:14 -07003030 if (cma_family(id_priv) == AF_INET) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003031 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07003032
Sean Heftyf4753832013-05-29 10:09:14 -07003033 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3034 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003035
Sean Hefty01602f12013-05-29 10:09:20 -07003036 cma_set_ip_ver(cma_hdr, 4);
3037 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3038 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3039 cma_hdr->port = src4->sin_port;
Sean Heftye8160e12013-05-29 10:09:22 -07003040 } else if (cma_family(id_priv) == AF_INET6) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003041 struct sockaddr_in6 *src6, *dst6;
3042
Sean Heftyf4753832013-05-29 10:09:14 -07003043 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3044 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003045
Sean Hefty01602f12013-05-29 10:09:20 -07003046 cma_set_ip_ver(cma_hdr, 6);
3047 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3048 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3049 cma_hdr->port = src6->sin6_port;
Sean Heftye51060f2006-06-17 20:37:29 -07003050 }
3051 return 0;
3052}
3053
Sean Hefty628e5f62006-11-30 16:44:16 -08003054static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3055 struct ib_cm_event *ib_event)
3056{
3057 struct rdma_id_private *id_priv = cm_id->context;
3058 struct rdma_cm_event event;
3059 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3060 int ret = 0;
3061
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003062 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07003063 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08003064
Sean Hefty8aa08602007-05-07 11:49:00 -07003065 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08003066 switch (ib_event->event) {
3067 case IB_CM_SIDR_REQ_ERROR:
3068 event.event = RDMA_CM_EVENT_UNREACHABLE;
3069 event.status = -ETIMEDOUT;
3070 break;
3071 case IB_CM_SIDR_REP_RECEIVED:
3072 event.param.ud.private_data = ib_event->private_data;
3073 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3074 if (rep->status != IB_SIDR_SUCCESS) {
3075 event.event = RDMA_CM_EVENT_UNREACHABLE;
3076 event.status = ib_event->param.sidr_rep_rcvd.status;
3077 break;
3078 }
Sean Hefty5c438132013-05-29 10:09:23 -07003079 ret = cma_set_qkey(id_priv, rep->qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003080 if (ret) {
3081 event.event = RDMA_CM_EVENT_ADDR_ERROR;
Sean Hefty5c438132013-05-29 10:09:23 -07003082 event.status = ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003083 break;
3084 }
3085 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3086 id_priv->id.route.path_rec,
3087 &event.param.ud.ah_attr);
3088 event.param.ud.qp_num = rep->qpn;
3089 event.param.ud.qkey = rep->qkey;
3090 event.event = RDMA_CM_EVENT_ESTABLISHED;
3091 event.status = 0;
3092 break;
3093 default:
Roland Dreier468f2232008-07-14 23:48:47 -07003094 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08003095 ib_event->event);
3096 goto out;
3097 }
3098
3099 ret = id_priv->id.event_handler(&id_priv->id, &event);
3100 if (ret) {
3101 /* Destroy the CM ID by returning a non-zero value. */
3102 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003103 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003104 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003105 rdma_destroy_id(&id_priv->id);
3106 return ret;
3107 }
3108out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07003109 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003110 return ret;
3111}
3112
3113static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3114 struct rdma_conn_param *conn_param)
3115{
3116 struct ib_cm_sidr_req_param req;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003117 struct ib_cm_id *id;
Sean Heftye511d1a2013-07-24 15:06:08 -07003118 void *private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003119 int offset, ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003120
Sean Heftye511d1a2013-07-24 15:06:08 -07003121 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003122 offset = cma_user_data_offset(id_priv);
3123 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003124 if (req.private_data_len < conn_param->private_data_len)
3125 return -EINVAL;
3126
Sean Heftye8160e12013-05-29 10:09:22 -07003127 if (req.private_data_len) {
Sean Heftye511d1a2013-07-24 15:06:08 -07003128 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3129 if (!private_data)
Sean Heftye8160e12013-05-29 10:09:22 -07003130 return -ENOMEM;
3131 } else {
Sean Heftye511d1a2013-07-24 15:06:08 -07003132 private_data = NULL;
Sean Heftye8160e12013-05-29 10:09:22 -07003133 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003134
3135 if (conn_param->private_data && conn_param->private_data_len)
Sean Heftye511d1a2013-07-24 15:06:08 -07003136 memcpy(private_data + offset, conn_param->private_data,
3137 conn_param->private_data_len);
Sean Hefty628e5f62006-11-30 16:44:16 -08003138
Sean Heftye511d1a2013-07-24 15:06:08 -07003139 if (private_data) {
3140 ret = cma_format_hdr(private_data, id_priv);
Sean Heftye8160e12013-05-29 10:09:22 -07003141 if (ret)
3142 goto out;
Sean Heftye511d1a2013-07-24 15:06:08 -07003143 req.private_data = private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003144 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003145
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003146 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3147 id_priv);
3148 if (IS_ERR(id)) {
3149 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08003150 goto out;
3151 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003152 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08003153
Sean Heftyf4753832013-05-29 10:09:14 -07003154 req.path = id_priv->id.route.path_rec;
Sean Heftycf539362013-05-29 10:09:28 -07003155 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Hefty628e5f62006-11-30 16:44:16 -08003156 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3157 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3158
3159 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3160 if (ret) {
3161 ib_destroy_cm_id(id_priv->cm_id.ib);
3162 id_priv->cm_id.ib = NULL;
3163 }
3164out:
Sean Heftye511d1a2013-07-24 15:06:08 -07003165 kfree(private_data);
Sean Hefty628e5f62006-11-30 16:44:16 -08003166 return ret;
3167}
3168
Sean Heftye51060f2006-06-17 20:37:29 -07003169static int cma_connect_ib(struct rdma_id_private *id_priv,
3170 struct rdma_conn_param *conn_param)
3171{
3172 struct ib_cm_req_param req;
3173 struct rdma_route *route;
3174 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003175 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07003176 int offset, ret;
3177
3178 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003179 offset = cma_user_data_offset(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003180 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003181 if (req.private_data_len < conn_param->private_data_len)
3182 return -EINVAL;
3183
Sean Heftye8160e12013-05-29 10:09:22 -07003184 if (req.private_data_len) {
3185 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3186 if (!private_data)
3187 return -ENOMEM;
3188 } else {
3189 private_data = NULL;
3190 }
Sean Heftye51060f2006-06-17 20:37:29 -07003191
3192 if (conn_param->private_data && conn_param->private_data_len)
3193 memcpy(private_data + offset, conn_param->private_data,
3194 conn_param->private_data_len);
3195
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003196 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3197 if (IS_ERR(id)) {
3198 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07003199 goto out;
3200 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003201 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07003202
3203 route = &id_priv->id.route;
Sean Heftye8160e12013-05-29 10:09:22 -07003204 if (private_data) {
3205 ret = cma_format_hdr(private_data, id_priv);
3206 if (ret)
3207 goto out;
3208 req.private_data = private_data;
3209 }
Sean Heftye51060f2006-06-17 20:37:29 -07003210
3211 req.primary_path = &route->path_rec[0];
3212 if (route->num_paths == 2)
3213 req.alternate_path = &route->path_rec[1];
3214
Sean Heftycf539362013-05-29 10:09:28 -07003215 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07003216 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07003217 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07003218 req.starting_psn = id_priv->seq_num;
3219 req.responder_resources = conn_param->responder_resources;
3220 req.initiator_depth = conn_param->initiator_depth;
3221 req.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003222 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3223 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003224 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3225 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3226 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3227 req.srq = id_priv->srq ? 1 : 0;
3228
3229 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3230out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003231 if (ret && !IS_ERR(id)) {
3232 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07003233 id_priv->cm_id.ib = NULL;
3234 }
3235
Sean Heftye51060f2006-06-17 20:37:29 -07003236 kfree(private_data);
3237 return ret;
3238}
3239
Tom Tucker07ebafb2006-08-03 16:02:42 -05003240static int cma_connect_iw(struct rdma_id_private *id_priv,
3241 struct rdma_conn_param *conn_param)
3242{
3243 struct iw_cm_id *cm_id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003244 int ret;
3245 struct iw_cm_conn_param iw_param;
3246
3247 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003248 if (IS_ERR(cm_id))
3249 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003250
Steve Wise68cdba02015-05-18 15:27:10 -05003251 cm_id->tos = id_priv->tos;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003252 id_priv->cm_id.iw = cm_id;
3253
Steve Wise24d44a32013-07-04 16:10:44 +05303254 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3255 rdma_addr_size(cma_src_addr(id_priv)));
3256 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3257 rdma_addr_size(cma_dst_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05003258
Sean Hefty5851bb82008-01-04 10:47:12 -08003259 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07003260 if (ret)
3261 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003262
Hefty, Seanf45ee802011-10-06 09:33:04 -07003263 if (conn_param) {
3264 iw_param.ord = conn_param->initiator_depth;
3265 iw_param.ird = conn_param->responder_resources;
3266 iw_param.private_data = conn_param->private_data;
3267 iw_param.private_data_len = conn_param->private_data_len;
3268 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3269 } else {
3270 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003271 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07003272 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003273 ret = iw_cm_connect(cm_id, &iw_param);
3274out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003275 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07003276 iw_destroy_cm_id(cm_id);
3277 id_priv->cm_id.iw = NULL;
3278 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003279 return ret;
3280}
3281
Sean Heftye51060f2006-06-17 20:37:29 -07003282int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3283{
3284 struct rdma_id_private *id_priv;
3285 int ret;
3286
3287 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003288 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003289 return -EINVAL;
3290
3291 if (!id->qp) {
3292 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003293 id_priv->srq = conn_param->srq;
3294 }
3295
Michael Wang72219cea2015-05-05 14:50:34 +02003296 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003297 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08003298 ret = cma_resolve_ib_udp(id_priv, conn_param);
3299 else
3300 ret = cma_connect_ib(id_priv, conn_param);
Michael Wang04215332015-05-05 14:50:35 +02003301 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003302 ret = cma_connect_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003303 else
Sean Heftye51060f2006-06-17 20:37:29 -07003304 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003305 if (ret)
3306 goto err;
3307
3308 return 0;
3309err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003310 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07003311 return ret;
3312}
3313EXPORT_SYMBOL(rdma_connect);
3314
3315static int cma_accept_ib(struct rdma_id_private *id_priv,
3316 struct rdma_conn_param *conn_param)
3317{
3318 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08003319 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003320
Sean Hefty5851bb82008-01-04 10:47:12 -08003321 ret = cma_modify_qp_rtr(id_priv, conn_param);
3322 if (ret)
3323 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08003324
Sean Hefty5851bb82008-01-04 10:47:12 -08003325 ret = cma_modify_qp_rts(id_priv, conn_param);
3326 if (ret)
3327 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003328
3329 memset(&rep, 0, sizeof rep);
3330 rep.qp_num = id_priv->qp_num;
3331 rep.starting_psn = id_priv->seq_num;
3332 rep.private_data = conn_param->private_data;
3333 rep.private_data_len = conn_param->private_data_len;
3334 rep.responder_resources = conn_param->responder_resources;
3335 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07003336 rep.failover_accepted = 0;
3337 rep.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003338 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003339 rep.srq = id_priv->srq ? 1 : 0;
3340
Sean Hefty0fe313b2006-11-30 16:37:15 -08003341 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3342out:
3343 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003344}
3345
Tom Tucker07ebafb2006-08-03 16:02:42 -05003346static int cma_accept_iw(struct rdma_id_private *id_priv,
3347 struct rdma_conn_param *conn_param)
3348{
3349 struct iw_cm_conn_param iw_param;
3350 int ret;
3351
Sean Hefty5851bb82008-01-04 10:47:12 -08003352 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003353 if (ret)
3354 return ret;
3355
3356 iw_param.ord = conn_param->initiator_depth;
3357 iw_param.ird = conn_param->responder_resources;
3358 iw_param.private_data = conn_param->private_data;
3359 iw_param.private_data_len = conn_param->private_data_len;
3360 if (id_priv->id.qp) {
3361 iw_param.qpn = id_priv->qp_num;
3362 } else
3363 iw_param.qpn = conn_param->qp_num;
3364
3365 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3366}
3367
Sean Hefty628e5f62006-11-30 16:44:16 -08003368static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
Sean Hefty5c438132013-05-29 10:09:23 -07003369 enum ib_cm_sidr_status status, u32 qkey,
Sean Hefty628e5f62006-11-30 16:44:16 -08003370 const void *private_data, int private_data_len)
3371{
3372 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003373 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003374
3375 memset(&rep, 0, sizeof rep);
3376 rep.status = status;
3377 if (status == IB_SIDR_SUCCESS) {
Sean Hefty5c438132013-05-29 10:09:23 -07003378 ret = cma_set_qkey(id_priv, qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003379 if (ret)
3380 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003381 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003382 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08003383 }
3384 rep.private_data = private_data;
3385 rep.private_data_len = private_data_len;
3386
3387 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3388}
3389
Sean Heftye51060f2006-06-17 20:37:29 -07003390int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3391{
3392 struct rdma_id_private *id_priv;
3393 int ret;
3394
3395 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00003396
3397 id_priv->owner = task_pid_nr(current);
3398
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003399 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003400 return -EINVAL;
3401
3402 if (!id->qp && conn_param) {
3403 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003404 id_priv->srq = conn_param->srq;
3405 }
3406
Michael Wang72219cea2015-05-05 14:50:34 +02003407 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Hefty, Seanf45ee802011-10-06 09:33:04 -07003408 if (id->qp_type == IB_QPT_UD) {
3409 if (conn_param)
3410 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003411 conn_param->qkey,
Hefty, Seanf45ee802011-10-06 09:33:04 -07003412 conn_param->private_data,
3413 conn_param->private_data_len);
3414 else
3415 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003416 0, NULL, 0);
Hefty, Seanf45ee802011-10-06 09:33:04 -07003417 } else {
3418 if (conn_param)
3419 ret = cma_accept_ib(id_priv, conn_param);
3420 else
3421 ret = cma_rep_recv(id_priv);
3422 }
Michael Wang04215332015-05-05 14:50:35 +02003423 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003424 ret = cma_accept_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003425 else
Sean Heftye51060f2006-06-17 20:37:29 -07003426 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003427
3428 if (ret)
3429 goto reject;
3430
3431 return 0;
3432reject:
Sean Heftyc5483382007-09-24 13:19:09 -07003433 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003434 rdma_reject(id, NULL, 0);
3435 return ret;
3436}
3437EXPORT_SYMBOL(rdma_accept);
3438
Sean Hefty0fe313b2006-11-30 16:37:15 -08003439int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3440{
3441 struct rdma_id_private *id_priv;
3442 int ret;
3443
3444 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003445 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08003446 return -EINVAL;
3447
3448 switch (id->device->node_type) {
3449 case RDMA_NODE_IB_CA:
3450 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3451 break;
3452 default:
3453 ret = 0;
3454 break;
3455 }
3456 return ret;
3457}
3458EXPORT_SYMBOL(rdma_notify);
3459
Sean Heftye51060f2006-06-17 20:37:29 -07003460int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3461 u8 private_data_len)
3462{
3463 struct rdma_id_private *id_priv;
3464 int ret;
3465
3466 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003467 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003468 return -EINVAL;
3469
Michael Wang72219cea2015-05-05 14:50:34 +02003470 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003471 if (id->qp_type == IB_QPT_UD)
Sean Hefty5c438132013-05-29 10:09:23 -07003472 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
Sean Hefty628e5f62006-11-30 16:44:16 -08003473 private_data, private_data_len);
3474 else
3475 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3476 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3477 0, private_data, private_data_len);
Michael Wang04215332015-05-05 14:50:35 +02003478 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003479 ret = iw_cm_reject(id_priv->cm_id.iw,
3480 private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003481 } else
Sean Heftye51060f2006-06-17 20:37:29 -07003482 ret = -ENOSYS;
Michael Wang21655af2015-05-05 14:50:27 +02003483
Sean Heftye51060f2006-06-17 20:37:29 -07003484 return ret;
3485}
3486EXPORT_SYMBOL(rdma_reject);
3487
3488int rdma_disconnect(struct rdma_cm_id *id)
3489{
3490 struct rdma_id_private *id_priv;
3491 int ret;
3492
3493 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003494 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003495 return -EINVAL;
3496
Michael Wang72219cea2015-05-05 14:50:34 +02003497 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyc5483382007-09-24 13:19:09 -07003498 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003499 if (ret)
3500 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003501 /* Initiate or respond to a disconnect. */
3502 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3503 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
Michael Wang04215332015-05-05 14:50:35 +02003504 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003505 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003506 } else
Tom Tucker07ebafb2006-08-03 16:02:42 -05003507 ret = -EINVAL;
Michael Wang21655af2015-05-05 14:50:27 +02003508
Sean Heftye51060f2006-06-17 20:37:29 -07003509out:
3510 return ret;
3511}
3512EXPORT_SYMBOL(rdma_disconnect);
3513
Sean Heftyc8f6a362007-02-15 17:00:18 -08003514static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3515{
3516 struct rdma_id_private *id_priv;
3517 struct cma_multicast *mc = multicast->context;
3518 struct rdma_cm_event event;
3519 int ret;
3520
3521 id_priv = mc->id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003522 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
3523 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07003524 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003525
Sean Hefty5c438132013-05-29 10:09:23 -07003526 if (!status)
3527 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
Sean Heftyc5483382007-09-24 13:19:09 -07003528 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003529 if (!status && id_priv->id.qp)
3530 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003531 be16_to_cpu(multicast->rec.mlid));
Sean Heftyc5483382007-09-24 13:19:09 -07003532 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003533
3534 memset(&event, 0, sizeof event);
3535 event.status = status;
3536 event.param.ud.private_data = mc->context;
3537 if (!status) {
3538 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3539 ib_init_ah_from_mcmember(id_priv->id.device,
3540 id_priv->id.port_num, &multicast->rec,
3541 &event.param.ud.ah_attr);
3542 event.param.ud.qp_num = 0xFFFFFF;
3543 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3544 } else
3545 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3546
3547 ret = id_priv->id.event_handler(&id_priv->id, &event);
3548 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003549 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003550 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003551 rdma_destroy_id(&id_priv->id);
3552 return 0;
3553 }
Sean Hefty8aa08602007-05-07 11:49:00 -07003554
Or Gerlitzde910bd2008-07-14 23:48:53 -07003555 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003556 return 0;
3557}
3558
3559static void cma_set_mgid(struct rdma_id_private *id_priv,
3560 struct sockaddr *addr, union ib_gid *mgid)
3561{
3562 unsigned char mc_map[MAX_ADDR_LEN];
3563 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3564 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3565 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3566
3567 if (cma_any_addr(addr)) {
3568 memset(mgid, 0, sizeof *mgid);
3569 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08003570 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08003571 0xFF10A01B)) {
3572 /* IPv6 address is an SA assigned MGID. */
3573 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003574 } else if (addr->sa_family == AF_IB) {
3575 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08003576 } else if ((addr->sa_family == AF_INET6)) {
3577 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3578 if (id_priv->id.ps == RDMA_PS_UDP)
3579 mc_map[7] = 0x01; /* Use RDMA CM signature */
3580 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003581 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07003582 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003583 if (id_priv->id.ps == RDMA_PS_UDP)
3584 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08003585 *mgid = *(union ib_gid *) (mc_map + 4);
3586 }
3587}
3588
3589static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3590 struct cma_multicast *mc)
3591{
3592 struct ib_sa_mcmember_rec rec;
3593 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3594 ib_sa_comp_mask comp_mask;
3595 int ret;
3596
3597 ib_addr_get_mgid(dev_addr, &rec.mgid);
3598 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3599 &rec.mgid, &rec);
3600 if (ret)
3601 return ret;
3602
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003603 ret = cma_set_qkey(id_priv, 0);
3604 if (ret)
3605 return ret;
3606
Roland Dreier3f446752008-08-04 11:02:14 -07003607 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003608 rec.qkey = cpu_to_be32(id_priv->qkey);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003609 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003610 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3611 rec.join_state = 1;
3612
3613 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3614 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3615 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3616 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3617 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3618
Yossi Etigin84adeee2009-04-01 13:55:32 -07003619 if (id_priv->id.ps == RDMA_PS_IPOIB)
3620 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
Dotan Barak2a22fb82012-08-30 09:09:55 +00003621 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3622 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3623 IB_SA_MCMEMBER_REC_MTU |
3624 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Yossi Etigin84adeee2009-04-01 13:55:32 -07003625
Sean Heftyc8f6a362007-02-15 17:00:18 -08003626 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3627 id_priv->id.port_num, &rec,
3628 comp_mask, GFP_KERNEL,
3629 cma_ib_mc_handler, mc);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303630 return PTR_ERR_OR_ZERO(mc->multicast.ib);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003631}
3632
Eli Cohen3c86aa72010-10-13 21:26:51 +02003633static void iboe_mcast_work_handler(struct work_struct *work)
3634{
3635 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3636 struct cma_multicast *mc = mw->mc;
3637 struct ib_sa_multicast *m = mc->multicast.ib;
3638
3639 mc->multicast.ib->context = mc;
3640 cma_ib_mc_handler(0, m);
3641 kref_put(&mc->mcref, release_mc);
3642 kfree(mw);
3643}
3644
3645static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3646{
3647 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3648 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3649
3650 if (cma_any_addr(addr)) {
3651 memset(mgid, 0, sizeof *mgid);
3652 } else if (addr->sa_family == AF_INET6) {
3653 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3654 } else {
3655 mgid->raw[0] = 0xff;
3656 mgid->raw[1] = 0x0e;
3657 mgid->raw[2] = 0;
3658 mgid->raw[3] = 0;
3659 mgid->raw[4] = 0;
3660 mgid->raw[5] = 0;
3661 mgid->raw[6] = 0;
3662 mgid->raw[7] = 0;
3663 mgid->raw[8] = 0;
3664 mgid->raw[9] = 0;
3665 mgid->raw[10] = 0xff;
3666 mgid->raw[11] = 0xff;
3667 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3668 }
3669}
3670
3671static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3672 struct cma_multicast *mc)
3673{
3674 struct iboe_mcast_work *work;
3675 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3676 int err;
3677 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3678 struct net_device *ndev = NULL;
3679
3680 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3681 return -EINVAL;
3682
3683 work = kzalloc(sizeof *work, GFP_KERNEL);
3684 if (!work)
3685 return -ENOMEM;
3686
3687 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3688 if (!mc->multicast.ib) {
3689 err = -ENOMEM;
3690 goto out1;
3691 }
3692
3693 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3694
3695 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3696 if (id_priv->id.ps == RDMA_PS_UDP)
3697 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3698
3699 if (dev_addr->bound_dev_if)
3700 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3701 if (!ndev) {
3702 err = -ENODEV;
3703 goto out2;
3704 }
3705 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3706 mc->multicast.ib->rec.hop_limit = 1;
3707 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3708 dev_put(ndev);
3709 if (!mc->multicast.ib->rec.mtu) {
3710 err = -EINVAL;
3711 goto out2;
3712 }
Moni Shoua7b856272013-12-12 18:03:12 +02003713 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3714 &mc->multicast.ib->rec.port_gid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003715 work->id = id_priv;
3716 work->mc = mc;
3717 INIT_WORK(&work->work, iboe_mcast_work_handler);
3718 kref_get(&mc->mcref);
3719 queue_work(cma_wq, &work->work);
3720
3721 return 0;
3722
3723out2:
3724 kfree(mc->multicast.ib);
3725out1:
3726 kfree(work);
3727 return err;
3728}
3729
Sean Heftyc8f6a362007-02-15 17:00:18 -08003730int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3731 void *context)
3732{
3733 struct rdma_id_private *id_priv;
3734 struct cma_multicast *mc;
3735 int ret;
3736
3737 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003738 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3739 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08003740 return -EINVAL;
3741
3742 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3743 if (!mc)
3744 return -ENOMEM;
3745
Sean Heftyef560862013-05-29 10:09:10 -07003746 memcpy(&mc->addr, addr, rdma_addr_size(addr));
Sean Heftyc8f6a362007-02-15 17:00:18 -08003747 mc->context = context;
3748 mc->id_priv = id_priv;
3749
3750 spin_lock(&id_priv->lock);
3751 list_add(&mc->list, &id_priv->mc_list);
3752 spin_unlock(&id_priv->lock);
3753
Ira Weiny5d9fb042015-05-14 15:01:46 -04003754 if (rdma_protocol_roce(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003755 kref_init(&mc->mcref);
3756 ret = cma_iboe_join_multicast(id_priv, mc);
Michael Wanga31ad3b2015-05-05 14:50:37 +02003757 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02003758 ret = cma_join_ib_multicast(id_priv, mc);
3759 else
Sean Heftyc8f6a362007-02-15 17:00:18 -08003760 ret = -ENOSYS;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003761
3762 if (ret) {
3763 spin_lock_irq(&id_priv->lock);
3764 list_del(&mc->list);
3765 spin_unlock_irq(&id_priv->lock);
3766 kfree(mc);
3767 }
3768 return ret;
3769}
3770EXPORT_SYMBOL(rdma_join_multicast);
3771
3772void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3773{
3774 struct rdma_id_private *id_priv;
3775 struct cma_multicast *mc;
3776
3777 id_priv = container_of(id, struct rdma_id_private, id);
3778 spin_lock_irq(&id_priv->lock);
3779 list_for_each_entry(mc, &id_priv->mc_list, list) {
Sean Heftyef560862013-05-29 10:09:10 -07003780 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
Sean Heftyc8f6a362007-02-15 17:00:18 -08003781 list_del(&mc->list);
3782 spin_unlock_irq(&id_priv->lock);
3783
3784 if (id->qp)
3785 ib_detach_mcast(id->qp,
3786 &mc->multicast.ib->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003787 be16_to_cpu(mc->multicast.ib->rec.mlid));
Michael Wang5c9a5282015-05-05 14:50:29 +02003788
3789 BUG_ON(id_priv->cma_dev->device != id->device);
3790
Michael Wanga31ad3b2015-05-05 14:50:37 +02003791 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003792 ib_sa_free_multicast(mc->multicast.ib);
3793 kfree(mc);
Ira Weiny5d9fb042015-05-14 15:01:46 -04003794 } else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02003795 kref_put(&mc->mcref, release_mc);
3796
Sean Heftyc8f6a362007-02-15 17:00:18 -08003797 return;
3798 }
3799 }
3800 spin_unlock_irq(&id_priv->lock);
3801}
3802EXPORT_SYMBOL(rdma_leave_multicast);
3803
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003804static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3805{
3806 struct rdma_dev_addr *dev_addr;
3807 struct cma_ndev_work *work;
3808
3809 dev_addr = &id_priv->id.route.addr.dev_addr;
3810
Sean Hefty6266ed62009-11-19 12:55:22 -08003811 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Guy Shapirofa201052015-10-22 15:20:10 +03003812 (net_eq(dev_net(ndev), dev_addr->net)) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003813 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3814 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3815 ndev->name, &id_priv->id);
3816 work = kzalloc(sizeof *work, GFP_KERNEL);
3817 if (!work)
3818 return -ENOMEM;
3819
3820 INIT_WORK(&work->work, cma_ndev_work_handler);
3821 work->id = id_priv;
3822 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3823 atomic_inc(&id_priv->refcount);
3824 queue_work(cma_wq, &work->work);
3825 }
3826
3827 return 0;
3828}
3829
3830static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003831 void *ptr)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003832{
Jiri Pirko351638e2013-05-28 01:30:21 +00003833 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003834 struct cma_device *cma_dev;
3835 struct rdma_id_private *id_priv;
3836 int ret = NOTIFY_DONE;
3837
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003838 if (event != NETDEV_BONDING_FAILOVER)
3839 return NOTIFY_DONE;
3840
3841 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3842 return NOTIFY_DONE;
3843
3844 mutex_lock(&lock);
3845 list_for_each_entry(cma_dev, &dev_list, list)
3846 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3847 ret = cma_netdev_change(ndev, id_priv);
3848 if (ret)
3849 goto out;
3850 }
3851
3852out:
3853 mutex_unlock(&lock);
3854 return ret;
3855}
3856
3857static struct notifier_block cma_nb = {
3858 .notifier_call = cma_netdev_callback
3859};
3860
Sean Heftye51060f2006-06-17 20:37:29 -07003861static void cma_add_one(struct ib_device *device)
3862{
3863 struct cma_device *cma_dev;
3864 struct rdma_id_private *id_priv;
3865
3866 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3867 if (!cma_dev)
3868 return;
3869
3870 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07003871
3872 init_completion(&cma_dev->comp);
3873 atomic_set(&cma_dev->refcount, 1);
3874 INIT_LIST_HEAD(&cma_dev->id_list);
3875 ib_set_client_data(device, &cma_client, cma_dev);
3876
3877 mutex_lock(&lock);
3878 list_add_tail(&cma_dev->list, &dev_list);
3879 list_for_each_entry(id_priv, &listen_any_list, list)
3880 cma_listen_on_dev(id_priv, cma_dev);
3881 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07003882}
3883
3884static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3885{
Sean Heftya1b1b612006-11-30 16:33:14 -08003886 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003887 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003888 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07003889
3890 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003891 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3892 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07003893 return 0;
3894
3895 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003896 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07003897
3898 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003899 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07003900 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003901
Sean Heftya1b1b612006-11-30 16:33:14 -08003902 memset(&event, 0, sizeof event);
3903 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003904 ret = id_priv->id.event_handler(&id_priv->id, &event);
3905out:
3906 mutex_unlock(&id_priv->handler_mutex);
3907 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003908}
3909
3910static void cma_process_remove(struct cma_device *cma_dev)
3911{
Sean Heftye51060f2006-06-17 20:37:29 -07003912 struct rdma_id_private *id_priv;
3913 int ret;
3914
Sean Heftye51060f2006-06-17 20:37:29 -07003915 mutex_lock(&lock);
3916 while (!list_empty(&cma_dev->id_list)) {
3917 id_priv = list_entry(cma_dev->id_list.next,
3918 struct rdma_id_private, list);
3919
Sean Heftyd02d1f52007-10-09 11:12:34 -07003920 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07003921 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07003922 atomic_inc(&id_priv->refcount);
3923 mutex_unlock(&lock);
3924
Sean Heftyd02d1f52007-10-09 11:12:34 -07003925 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003926 cma_deref_id(id_priv);
3927 if (ret)
3928 rdma_destroy_id(&id_priv->id);
3929
3930 mutex_lock(&lock);
3931 }
3932 mutex_unlock(&lock);
3933
3934 cma_deref_dev(cma_dev);
3935 wait_for_completion(&cma_dev->comp);
3936}
3937
Haggai Eran7c1eb452015-07-30 17:50:14 +03003938static void cma_remove_one(struct ib_device *device, void *client_data)
Sean Heftye51060f2006-06-17 20:37:29 -07003939{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003940 struct cma_device *cma_dev = client_data;
Sean Heftye51060f2006-06-17 20:37:29 -07003941
Sean Heftye51060f2006-06-17 20:37:29 -07003942 if (!cma_dev)
3943 return;
3944
3945 mutex_lock(&lock);
3946 list_del(&cma_dev->list);
3947 mutex_unlock(&lock);
3948
3949 cma_process_remove(cma_dev);
3950 kfree(cma_dev);
3951}
3952
Nir Muchtar753f6182011-01-03 15:33:53 +00003953static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3954{
3955 struct nlmsghdr *nlh;
3956 struct rdma_cm_id_stats *id_stats;
3957 struct rdma_id_private *id_priv;
3958 struct rdma_cm_id *id = NULL;
3959 struct cma_device *cma_dev;
3960 int i_dev = 0, i_id = 0;
3961
3962 /*
3963 * We export all of the IDs as a sequence of messages. Each
3964 * ID gets its own netlink message.
3965 */
3966 mutex_lock(&lock);
3967
3968 list_for_each_entry(cma_dev, &dev_list, list) {
3969 if (i_dev < cb->args[0]) {
3970 i_dev++;
3971 continue;
3972 }
3973
3974 i_id = 0;
3975 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3976 if (i_id < cb->args[1]) {
3977 i_id++;
3978 continue;
3979 }
3980
3981 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3982 sizeof *id_stats, RDMA_NL_RDMA_CM,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -05003983 RDMA_NL_RDMA_CM_ID_STATS,
3984 NLM_F_MULTI);
Nir Muchtar753f6182011-01-03 15:33:53 +00003985 if (!id_stats)
3986 goto out;
3987
3988 memset(id_stats, 0, sizeof *id_stats);
3989 id = &id_priv->id;
3990 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3991 id_stats->port_num = id->port_num;
3992 id_stats->bound_dev_if =
3993 id->route.addr.dev_addr.bound_dev_if;
3994
Sean Heftyce117ff2013-05-29 10:09:34 -07003995 if (ibnl_put_attr(skb, nlh,
3996 rdma_addr_size(cma_src_addr(id_priv)),
3997 cma_src_addr(id_priv),
3998 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
3999 goto out;
4000 if (ibnl_put_attr(skb, nlh,
4001 rdma_addr_size(cma_src_addr(id_priv)),
4002 cma_dst_addr(id_priv),
4003 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
4004 goto out;
Nir Muchtar753f6182011-01-03 15:33:53 +00004005
Nir Muchtar83e95022011-01-13 13:56:04 +00004006 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00004007 id_stats->port_space = id->ps;
4008 id_stats->cm_state = id_priv->state;
4009 id_stats->qp_num = id_priv->qp_num;
4010 id_stats->qp_type = id->qp_type;
4011
4012 i_id++;
4013 }
4014
4015 cb->args[1] = 0;
4016 i_dev++;
4017 }
4018
4019out:
4020 mutex_unlock(&lock);
4021 cb->args[0] = i_dev;
4022 cb->args[1] = i_id;
4023
4024 return skb->len;
4025}
4026
4027static const struct ibnl_client_cbs cma_cb_table[] = {
Gao feng809d5fc2012-10-04 20:15:49 +00004028 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
4029 .module = THIS_MODULE },
Nir Muchtar753f6182011-01-03 15:33:53 +00004030};
4031
Haggai Eran4be74b42015-10-22 15:20:09 +03004032static int cma_init_net(struct net *net)
4033{
4034 struct cma_pernet *pernet = cma_pernet(net);
4035
4036 idr_init(&pernet->tcp_ps);
4037 idr_init(&pernet->udp_ps);
4038 idr_init(&pernet->ipoib_ps);
4039 idr_init(&pernet->ib_ps);
4040
4041 return 0;
4042}
4043
4044static void cma_exit_net(struct net *net)
4045{
4046 struct cma_pernet *pernet = cma_pernet(net);
4047
4048 idr_destroy(&pernet->tcp_ps);
4049 idr_destroy(&pernet->udp_ps);
4050 idr_destroy(&pernet->ipoib_ps);
4051 idr_destroy(&pernet->ib_ps);
4052}
4053
4054static struct pernet_operations cma_pernet_operations = {
4055 .init = cma_init_net,
4056 .exit = cma_exit_net,
4057 .id = &cma_pernet_id,
4058 .size = sizeof(struct cma_pernet),
4059};
4060
Peter Huewe716abb12009-06-23 10:38:42 -07004061static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004062{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09004063 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004064
Sean Heftyc7f743a2007-02-01 12:23:37 -08004065 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07004066 if (!cma_wq)
4067 return -ENOMEM;
4068
Haggai Eran4be74b42015-10-22 15:20:09 +03004069 ret = register_pernet_subsys(&cma_pernet_operations);
4070 if (ret)
4071 goto err_wq;
4072
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004073 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08004074 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004075 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004076
Sean Heftye51060f2006-06-17 20:37:29 -07004077 ret = ib_register_client(&cma_client);
4078 if (ret)
4079 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00004080
4081 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
4082 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
4083
Sean Heftye51060f2006-06-17 20:37:29 -07004084 return 0;
4085
4086err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004087 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004088 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004089 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004090err_wq:
Sean Heftye51060f2006-06-17 20:37:29 -07004091 destroy_workqueue(cma_wq);
4092 return ret;
4093}
4094
Peter Huewe716abb12009-06-23 10:38:42 -07004095static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004096{
Nir Muchtar753f6182011-01-03 15:33:53 +00004097 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07004098 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004099 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004100 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004101 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004102 unregister_pernet_subsys(&cma_pernet_operations);
Sean Heftye51060f2006-06-17 20:37:29 -07004103 destroy_workqueue(cma_wq);
Sean Heftye51060f2006-06-17 20:37:29 -07004104}
4105
4106module_init(cma_init);
4107module_exit(cma_cleanup);