blob: a7886acba0724b3987324cdb8c6b79107ba1738e [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
47#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080048#include <net/ipv6.h>
Sean Heftye51060f2006-06-17 20:37:29 -070049
50#include <rdma/rdma_cm.h>
51#include <rdma/rdma_cm_ib.h>
Nir Muchtar753f6182011-01-03 15:33:53 +000052#include <rdma/rdma_netlink.h>
Sean Hefty2e2d1902013-05-29 10:09:09 -070053#include <rdma/ib.h>
Sean Heftye51060f2006-06-17 20:37:29 -070054#include <rdma/ib_cache.h>
55#include <rdma/ib_cm.h>
56#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050057#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070058
59MODULE_AUTHOR("Sean Hefty");
60MODULE_DESCRIPTION("Generic RDMA CM Agent");
61MODULE_LICENSE("Dual BSD/GPL");
62
63#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030064#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070065#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020066#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070067
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030068static const char * const cma_events[] = {
69 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
70 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
71 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
72 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
73 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
74 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
75 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
76 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
77 [RDMA_CM_EVENT_REJECTED] = "rejected",
78 [RDMA_CM_EVENT_ESTABLISHED] = "established",
79 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
80 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
81 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
82 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
83 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
84 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
85};
86
87const char *rdma_event_msg(enum rdma_cm_event_type event)
88{
89 size_t index = event;
90
91 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
92 cma_events[index] : "unrecognized event";
93}
94EXPORT_SYMBOL(rdma_event_msg);
95
Sean Heftye51060f2006-06-17 20:37:29 -070096static void cma_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +030097static void cma_remove_one(struct ib_device *device, void *client_data);
Sean Heftye51060f2006-06-17 20:37:29 -070098
99static struct ib_client cma_client = {
100 .name = "cma",
101 .add = cma_add_one,
102 .remove = cma_remove_one
103};
104
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700105static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -0800106static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -0700107static LIST_HEAD(dev_list);
108static LIST_HEAD(listen_any_list);
109static DEFINE_MUTEX(lock);
110static struct workqueue_struct *cma_wq;
Sean Heftye51060f2006-06-17 20:37:29 -0700111static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -0800112static DEFINE_IDR(udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800113static DEFINE_IDR(ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -0700114static DEFINE_IDR(ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -0700115
Haggai Eranaac978e2015-07-30 17:50:20 +0300116static struct idr *cma_idr(enum rdma_port_space ps)
117{
118 switch (ps) {
119 case RDMA_PS_TCP:
120 return &tcp_ps;
121 case RDMA_PS_UDP:
122 return &udp_ps;
123 case RDMA_PS_IPOIB:
124 return &ipoib_ps;
125 case RDMA_PS_IB:
126 return &ib_ps;
127 default:
128 return NULL;
129 }
130}
131
Sean Heftye51060f2006-06-17 20:37:29 -0700132struct cma_device {
133 struct list_head list;
134 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -0700135 struct completion comp;
136 atomic_t refcount;
137 struct list_head id_list;
138};
139
Sean Heftye51060f2006-06-17 20:37:29 -0700140struct rdma_bind_list {
Haggai Eranaac978e2015-07-30 17:50:20 +0300141 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -0700142 struct hlist_head owners;
143 unsigned short port;
144};
145
Haggai Eranaac978e2015-07-30 17:50:20 +0300146static int cma_ps_alloc(enum rdma_port_space ps,
147 struct rdma_bind_list *bind_list, int snum)
148{
149 struct idr *idr = cma_idr(ps);
150
151 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
152}
153
154static struct rdma_bind_list *cma_ps_find(enum rdma_port_space ps, int snum)
155{
156 struct idr *idr = cma_idr(ps);
157
158 return idr_find(idr, snum);
159}
160
161static void cma_ps_remove(enum rdma_port_space ps, int snum)
162{
163 struct idr *idr = cma_idr(ps);
164
165 idr_remove(idr, snum);
166}
167
Sean Hefty68602122012-06-14 20:31:39 +0000168enum {
169 CMA_OPTION_AFONLY,
170};
171
Sean Heftye51060f2006-06-17 20:37:29 -0700172/*
173 * Device removal can occur at anytime, so we need extra handling to
174 * serialize notifying the user of device removal with other callbacks.
175 * We do this by disabling removal notification while a callback is in process,
176 * and reporting it after the callback completes.
177 */
178struct rdma_id_private {
179 struct rdma_cm_id id;
180
181 struct rdma_bind_list *bind_list;
182 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700183 struct list_head list; /* listen_any_list or cma_device.list */
184 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700185 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800186 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700187
Sean Heftyd02d1f52007-10-09 11:12:34 -0700188 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700189 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700190 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700191 struct mutex qp_mutex;
192
Sean Heftye51060f2006-06-17 20:37:29 -0700193 struct completion comp;
194 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700195 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700196
197 int backlog;
198 int timeout_ms;
199 struct ib_sa_query *query;
200 int query_id;
201 union {
202 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500203 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700204 } cm_id;
205
206 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800207 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700208 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000209 pid_t owner;
Sean Hefty68602122012-06-14 20:31:39 +0000210 u32 options;
Sean Heftye51060f2006-06-17 20:37:29 -0700211 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700212 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700213 u8 reuseaddr;
Sean Hefty5b0ec992012-06-14 20:31:39 +0000214 u8 afonly;
Sean Heftye51060f2006-06-17 20:37:29 -0700215};
216
Sean Heftyc8f6a362007-02-15 17:00:18 -0800217struct cma_multicast {
218 struct rdma_id_private *id_priv;
219 union {
220 struct ib_sa_multicast *ib;
221 } multicast;
222 struct list_head list;
223 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700224 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200225 struct kref mcref;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800226};
227
Sean Heftye51060f2006-06-17 20:37:29 -0700228struct cma_work {
229 struct work_struct work;
230 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700231 enum rdma_cm_state old_state;
232 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700233 struct rdma_cm_event event;
234};
235
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700236struct cma_ndev_work {
237 struct work_struct work;
238 struct rdma_id_private *id;
239 struct rdma_cm_event event;
240};
241
Eli Cohen3c86aa72010-10-13 21:26:51 +0200242struct iboe_mcast_work {
243 struct work_struct work;
244 struct rdma_id_private *id;
245 struct cma_multicast *mc;
246};
247
Sean Heftye51060f2006-06-17 20:37:29 -0700248union cma_ip_addr {
249 struct in6_addr ip6;
250 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000251 __be32 pad[3];
252 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700253 } ip4;
254};
255
256struct cma_hdr {
257 u8 cma_version;
258 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000259 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700260 union cma_ip_addr src_addr;
261 union cma_ip_addr dst_addr;
262};
263
Sean Heftye51060f2006-06-17 20:37:29 -0700264#define CMA_VERSION 0x00
Sean Heftye51060f2006-06-17 20:37:29 -0700265
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700266static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700267{
268 unsigned long flags;
269 int ret;
270
271 spin_lock_irqsave(&id_priv->lock, flags);
272 ret = (id_priv->state == comp);
273 spin_unlock_irqrestore(&id_priv->lock, flags);
274 return ret;
275}
276
277static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700278 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700279{
280 unsigned long flags;
281 int ret;
282
283 spin_lock_irqsave(&id_priv->lock, flags);
284 if ((ret = (id_priv->state == comp)))
285 id_priv->state = exch;
286 spin_unlock_irqrestore(&id_priv->lock, flags);
287 return ret;
288}
289
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700290static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
291 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700292{
293 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700294 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700295
296 spin_lock_irqsave(&id_priv->lock, flags);
297 old = id_priv->state;
298 id_priv->state = exch;
299 spin_unlock_irqrestore(&id_priv->lock, flags);
300 return old;
301}
302
303static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
304{
305 return hdr->ip_version >> 4;
306}
307
308static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
309{
310 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
311}
312
Sean Heftye51060f2006-06-17 20:37:29 -0700313static void cma_attach_to_dev(struct rdma_id_private *id_priv,
314 struct cma_device *cma_dev)
315{
316 atomic_inc(&cma_dev->refcount);
317 id_priv->cma_dev = cma_dev;
318 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200319 id_priv->id.route.addr.dev_addr.transport =
320 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700321 list_add_tail(&id_priv->list, &cma_dev->id_list);
322}
323
324static inline void cma_deref_dev(struct cma_device *cma_dev)
325{
326 if (atomic_dec_and_test(&cma_dev->refcount))
327 complete(&cma_dev->comp);
328}
329
Eli Cohen3c86aa72010-10-13 21:26:51 +0200330static inline void release_mc(struct kref *kref)
331{
332 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
333
334 kfree(mc->multicast.ib);
335 kfree(mc);
336}
337
Sean Heftya396d432011-02-23 09:05:39 -0800338static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700339{
Sean Heftya396d432011-02-23 09:05:39 -0800340 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700341 list_del(&id_priv->list);
342 cma_deref_dev(id_priv->cma_dev);
343 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800344 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700345}
346
Sean Heftyf4753832013-05-29 10:09:14 -0700347static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
348{
349 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
350}
351
352static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
353{
354 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
355}
356
357static inline unsigned short cma_family(struct rdma_id_private *id_priv)
358{
359 return id_priv->id.route.addr.src_addr.ss_family;
360}
361
Sean Hefty5c438132013-05-29 10:09:23 -0700362static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800363{
364 struct ib_sa_mcmember_rec rec;
365 int ret = 0;
366
Sean Hefty5c438132013-05-29 10:09:23 -0700367 if (id_priv->qkey) {
368 if (qkey && id_priv->qkey != qkey)
369 return -EINVAL;
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700370 return 0;
Sean Hefty5c438132013-05-29 10:09:23 -0700371 }
372
373 if (qkey) {
374 id_priv->qkey = qkey;
375 return 0;
376 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700377
378 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800379 case RDMA_PS_UDP:
Sean Hefty5c438132013-05-29 10:09:23 -0700380 case RDMA_PS_IB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700381 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800382 break;
383 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700384 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
385 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
386 id_priv->id.port_num, &rec.mgid,
387 &rec);
388 if (!ret)
389 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800390 break;
391 default:
392 break;
393 }
394 return ret;
395}
396
Sean Hefty680f9202013-05-29 10:09:12 -0700397static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
398{
399 dev_addr->dev_type = ARPHRD_INFINIBAND;
400 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
401 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
402}
403
404static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
405{
406 int ret;
407
408 if (addr->sa_family != AF_IB) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200409 ret = rdma_translate_ip(addr, dev_addr, NULL);
Sean Hefty680f9202013-05-29 10:09:12 -0700410 } else {
411 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
412 ret = 0;
413 }
414
415 return ret;
416}
417
Michael Wang7c111472015-05-05 14:50:30 +0200418static inline int cma_validate_port(struct ib_device *device, u8 port,
419 union ib_gid *gid, int dev_type)
420{
421 u8 found_port;
422 int ret = -ENODEV;
423
424 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
425 return ret;
426
427 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
428 return ret;
429
430 ret = ib_find_cached_gid(device, gid, &found_port, NULL);
431 if (port != found_port)
432 return -ENODEV;
433
434 return ret;
435}
436
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400437static int cma_acquire_dev(struct rdma_id_private *id_priv,
438 struct rdma_id_private *listen_id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700439{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800440 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700441 struct cma_device *cma_dev;
Michael Wang7c111472015-05-05 14:50:30 +0200442 union ib_gid gid, iboe_gid, *gidp;
Sean Heftye51060f2006-06-17 20:37:29 -0700443 int ret = -ENODEV;
Michael Wang7c111472015-05-05 14:50:30 +0200444 u8 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700445
Michael Wang7c111472015-05-05 14:50:30 +0200446 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000447 id_priv->id.ps == RDMA_PS_IPOIB)
448 return -EINVAL;
449
Sean Heftya396d432011-02-23 09:05:39 -0800450 mutex_lock(&lock);
Moni Shoua7b856272013-12-12 18:03:12 +0200451 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
452 &iboe_gid);
453
Eli Cohen3c86aa72010-10-13 21:26:51 +0200454 memcpy(&gid, dev_addr->src_dev_addr +
455 rdma_addr_gid_offset(dev_addr), sizeof gid);
Michael Wang7c111472015-05-05 14:50:30 +0200456
457 if (listen_id_priv) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400458 cma_dev = listen_id_priv->cma_dev;
459 port = listen_id_priv->id.port_num;
Ira Weiny5d9fb042015-05-14 15:01:46 -0400460 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200461 &iboe_gid : &gid;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400462
Michael Wang7c111472015-05-05 14:50:30 +0200463 ret = cma_validate_port(cma_dev->device, port, gidp,
464 dev_addr->dev_type);
465 if (!ret) {
466 id_priv->id.port_num = port;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400467 goto out;
468 }
469 }
Michael Wang7c111472015-05-05 14:50:30 +0200470
Sean Heftye51060f2006-06-17 20:37:29 -0700471 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200472 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400473 if (listen_id_priv &&
474 listen_id_priv->cma_dev == cma_dev &&
475 listen_id_priv->id.port_num == port)
476 continue;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200477
Ira Weiny5d9fb042015-05-14 15:01:46 -0400478 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200479 &iboe_gid : &gid;
480
481 ret = cma_validate_port(cma_dev->device, port, gidp,
482 dev_addr->dev_type);
483 if (!ret) {
484 id_priv->id.port_num = port;
485 goto out;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200486 }
Sean Heftye51060f2006-06-17 20:37:29 -0700487 }
488 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200489
490out:
491 if (!ret)
492 cma_attach_to_dev(id_priv, cma_dev);
493
Sean Heftya396d432011-02-23 09:05:39 -0800494 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700495 return ret;
496}
497
Sean Heftyf17df3b2013-05-29 10:09:17 -0700498/*
499 * Select the source IB device and address to reach the destination IB address.
500 */
501static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
502{
503 struct cma_device *cma_dev, *cur_dev;
504 struct sockaddr_ib *addr;
505 union ib_gid gid, sgid, *dgid;
506 u16 pkey, index;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700507 u8 p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700508 int i;
509
510 cma_dev = NULL;
511 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
512 dgid = (union ib_gid *) &addr->sib_addr;
513 pkey = ntohs(addr->sib_pkey);
514
515 list_for_each_entry(cur_dev, &dev_list, list) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700516 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
Michael Wang30a74ef2015-05-05 14:50:39 +0200517 if (!rdma_cap_af_ib(cur_dev->device, p))
Michael Wangfef60902015-05-05 14:50:31 +0200518 continue;
519
Sean Heftyf17df3b2013-05-29 10:09:17 -0700520 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
521 continue;
522
523 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i, &gid); i++) {
524 if (!memcmp(&gid, dgid, sizeof(gid))) {
525 cma_dev = cur_dev;
526 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700527 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700528 goto found;
529 }
530
531 if (!cma_dev && (gid.global.subnet_prefix ==
532 dgid->global.subnet_prefix)) {
533 cma_dev = cur_dev;
534 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700535 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700536 }
537 }
538 }
539 }
540
541 if (!cma_dev)
542 return -ENODEV;
543
544found:
545 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf17df3b2013-05-29 10:09:17 -0700546 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
547 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
548 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
549 return 0;
550}
551
Sean Heftye51060f2006-06-17 20:37:29 -0700552static void cma_deref_id(struct rdma_id_private *id_priv)
553{
554 if (atomic_dec_and_test(&id_priv->refcount))
555 complete(&id_priv->comp);
556}
557
Or Gerlitzde910bd2008-07-14 23:48:53 -0700558static int cma_disable_callback(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700559 enum rdma_cm_state state)
Sean Hefty8aa08602007-05-07 11:49:00 -0700560{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700561 mutex_lock(&id_priv->handler_mutex);
562 if (id_priv->state != state) {
563 mutex_unlock(&id_priv->handler_mutex);
564 return -EINVAL;
565 }
566 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700567}
568
569struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000570 void *context, enum rdma_port_space ps,
571 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700572{
573 struct rdma_id_private *id_priv;
574
575 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
576 if (!id_priv)
577 return ERR_PTR(-ENOMEM);
578
Nir Muchtar83e95022011-01-13 13:56:04 +0000579 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700580 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700581 id_priv->id.context = context;
582 id_priv->id.event_handler = event_handler;
583 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000584 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700585 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700586 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700587 init_completion(&id_priv->comp);
588 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700589 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700590 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800591 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700592 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
593
594 return &id_priv->id;
595}
596EXPORT_SYMBOL(rdma_create_id);
597
Sean Heftyc8f6a362007-02-15 17:00:18 -0800598static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700599{
600 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800601 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700602
Sean Heftyc8f6a362007-02-15 17:00:18 -0800603 qp_attr.qp_state = IB_QPS_INIT;
604 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700605 if (ret)
606 return ret;
607
Sean Heftyc8f6a362007-02-15 17:00:18 -0800608 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
609 if (ret)
610 return ret;
611
612 qp_attr.qp_state = IB_QPS_RTR;
613 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
614 if (ret)
615 return ret;
616
617 qp_attr.qp_state = IB_QPS_RTS;
618 qp_attr.sq_psn = 0;
619 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
620
621 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700622}
623
Sean Heftyc8f6a362007-02-15 17:00:18 -0800624static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500625{
626 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800627 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500628
629 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800630 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
631 if (ret)
632 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500633
Sean Heftyc8f6a362007-02-15 17:00:18 -0800634 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500635}
636
Sean Heftye51060f2006-06-17 20:37:29 -0700637int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
638 struct ib_qp_init_attr *qp_init_attr)
639{
640 struct rdma_id_private *id_priv;
641 struct ib_qp *qp;
642 int ret;
643
644 id_priv = container_of(id, struct rdma_id_private, id);
645 if (id->device != pd->device)
646 return -EINVAL;
647
648 qp = ib_create_qp(pd, qp_init_attr);
649 if (IS_ERR(qp))
650 return PTR_ERR(qp);
651
Sean Heftyb26f9b92010-04-01 17:08:41 +0000652 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800653 ret = cma_init_ud_qp(id_priv, qp);
654 else
655 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700656 if (ret)
657 goto err;
658
659 id->qp = qp;
660 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700661 id_priv->srq = (qp->srq != NULL);
662 return 0;
663err:
664 ib_destroy_qp(qp);
665 return ret;
666}
667EXPORT_SYMBOL(rdma_create_qp);
668
669void rdma_destroy_qp(struct rdma_cm_id *id)
670{
Sean Heftyc5483382007-09-24 13:19:09 -0700671 struct rdma_id_private *id_priv;
672
673 id_priv = container_of(id, struct rdma_id_private, id);
674 mutex_lock(&id_priv->qp_mutex);
675 ib_destroy_qp(id_priv->id.qp);
676 id_priv->id.qp = NULL;
677 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700678}
679EXPORT_SYMBOL(rdma_destroy_qp);
680
Sean Hefty5851bb82008-01-04 10:47:12 -0800681static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
682 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700683{
684 struct ib_qp_attr qp_attr;
685 int qp_attr_mask, ret;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200686 union ib_gid sgid;
Sean Heftye51060f2006-06-17 20:37:29 -0700687
Sean Heftyc5483382007-09-24 13:19:09 -0700688 mutex_lock(&id_priv->qp_mutex);
689 if (!id_priv->id.qp) {
690 ret = 0;
691 goto out;
692 }
Sean Heftye51060f2006-06-17 20:37:29 -0700693
694 /* Need to update QP attributes from default values. */
695 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700696 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700697 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700698 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700699
Sean Heftyc5483382007-09-24 13:19:09 -0700700 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700701 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700702 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700703
704 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700705 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700706 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700707 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700708
Matan Barakdd5f03b2013-12-12 18:03:11 +0200709 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
710 qp_attr.ah_attr.grh.sgid_index, &sgid);
711 if (ret)
712 goto out;
713
Michael Wangfef60902015-05-05 14:50:31 +0200714 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
715
Ira Weiny5d9fb042015-05-14 15:01:46 -0400716 if (rdma_protocol_roce(id_priv->id.device, id_priv->id.port_num)) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200717 ret = rdma_addr_find_smac_by_sgid(&sgid, qp_attr.smac, NULL);
718
719 if (ret)
720 goto out;
721 }
Sean Hefty5851bb82008-01-04 10:47:12 -0800722 if (conn_param)
723 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700724 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
725out:
726 mutex_unlock(&id_priv->qp_mutex);
727 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700728}
729
Sean Hefty5851bb82008-01-04 10:47:12 -0800730static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
731 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700732{
733 struct ib_qp_attr qp_attr;
734 int qp_attr_mask, ret;
735
Sean Heftyc5483382007-09-24 13:19:09 -0700736 mutex_lock(&id_priv->qp_mutex);
737 if (!id_priv->id.qp) {
738 ret = 0;
739 goto out;
740 }
Sean Heftye51060f2006-06-17 20:37:29 -0700741
742 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700743 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700744 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700745 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700746
Sean Hefty5851bb82008-01-04 10:47:12 -0800747 if (conn_param)
748 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700749 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
750out:
751 mutex_unlock(&id_priv->qp_mutex);
752 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700753}
754
Sean Heftyc5483382007-09-24 13:19:09 -0700755static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700756{
757 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700758 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700759
Sean Heftyc5483382007-09-24 13:19:09 -0700760 mutex_lock(&id_priv->qp_mutex);
761 if (!id_priv->id.qp) {
762 ret = 0;
763 goto out;
764 }
Sean Heftye51060f2006-06-17 20:37:29 -0700765
766 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700767 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
768out:
769 mutex_unlock(&id_priv->qp_mutex);
770 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700771}
772
Sean Heftyc8f6a362007-02-15 17:00:18 -0800773static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
774 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
775{
776 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
777 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200778 u16 pkey;
779
Michael Wang227128f2015-05-05 14:50:40 +0200780 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
Eli Cohen3c86aa72010-10-13 21:26:51 +0200781 pkey = 0xffff;
Michael Wangfef60902015-05-05 14:50:31 +0200782 else
783 pkey = ib_addr_get_pkey(dev_addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800784
785 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200786 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800787 if (ret)
788 return ret;
789
790 qp_attr->port_num = id_priv->id.port_num;
791 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
792
Sean Heftyb26f9b92010-04-01 17:08:41 +0000793 if (id_priv->id.qp_type == IB_QPT_UD) {
Sean Hefty5c438132013-05-29 10:09:23 -0700794 ret = cma_set_qkey(id_priv, 0);
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700795 if (ret)
796 return ret;
797
Sean Heftyc8f6a362007-02-15 17:00:18 -0800798 qp_attr->qkey = id_priv->qkey;
799 *qp_attr_mask |= IB_QP_QKEY;
800 } else {
801 qp_attr->qp_access_flags = 0;
802 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
803 }
804 return 0;
805}
806
Sean Heftye51060f2006-06-17 20:37:29 -0700807int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
808 int *qp_attr_mask)
809{
810 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800811 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700812
813 id_priv = container_of(id, struct rdma_id_private, id);
Michael Wang72219cea2015-05-05 14:50:34 +0200814 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +0000815 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800816 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
817 else
818 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
819 qp_attr_mask);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200820
Sean Heftye51060f2006-06-17 20:37:29 -0700821 if (qp_attr->qp_state == IB_QPS_RTR)
822 qp_attr->rq_psn = id_priv->seq_num;
Michael Wang04215332015-05-05 14:50:35 +0200823 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800824 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300825 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800826 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
827 } else
828 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
829 qp_attr_mask);
Michael Wang21655af2015-05-05 14:50:27 +0200830 } else
Sean Heftye51060f2006-06-17 20:37:29 -0700831 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -0700832
833 return ret;
834}
835EXPORT_SYMBOL(rdma_init_qp_attr);
836
837static inline int cma_zero_addr(struct sockaddr *addr)
838{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700839 switch (addr->sa_family) {
840 case AF_INET:
841 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
842 case AF_INET6:
843 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
844 case AF_IB:
845 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
846 default:
847 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700848 }
849}
850
851static inline int cma_loopback_addr(struct sockaddr *addr)
852{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700853 switch (addr->sa_family) {
854 case AF_INET:
855 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
856 case AF_INET6:
857 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
858 case AF_IB:
859 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
860 default:
861 return 0;
862 }
Sean Heftye51060f2006-06-17 20:37:29 -0700863}
864
865static inline int cma_any_addr(struct sockaddr *addr)
866{
867 return cma_zero_addr(addr) || cma_loopback_addr(addr);
868}
869
Hefty, Sean43b752d2011-05-09 22:06:10 -0700870static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
871{
872 if (src->sa_family != dst->sa_family)
873 return -1;
874
875 switch (src->sa_family) {
876 case AF_INET:
877 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
878 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
Sean Hefty2e2d1902013-05-29 10:09:09 -0700879 case AF_INET6:
Hefty, Sean43b752d2011-05-09 22:06:10 -0700880 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
881 &((struct sockaddr_in6 *) dst)->sin6_addr);
Sean Hefty2e2d1902013-05-29 10:09:09 -0700882 default:
883 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
884 &((struct sockaddr_ib *) dst)->sib_addr);
Hefty, Sean43b752d2011-05-09 22:06:10 -0700885 }
886}
887
Sean Hefty58afdcb2013-05-29 10:09:11 -0700888static __be16 cma_port(struct sockaddr *addr)
Sean Hefty628e5f62006-11-30 16:44:16 -0800889{
Sean Hefty58afdcb2013-05-29 10:09:11 -0700890 struct sockaddr_ib *sib;
891
892 switch (addr->sa_family) {
893 case AF_INET:
Sean Hefty628e5f62006-11-30 16:44:16 -0800894 return ((struct sockaddr_in *) addr)->sin_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700895 case AF_INET6:
Sean Hefty628e5f62006-11-30 16:44:16 -0800896 return ((struct sockaddr_in6 *) addr)->sin6_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700897 case AF_IB:
898 sib = (struct sockaddr_ib *) addr;
899 return htons((u16) (be64_to_cpu(sib->sib_sid) &
900 be64_to_cpu(sib->sib_sid_mask)));
901 default:
902 return 0;
903 }
Sean Hefty628e5f62006-11-30 16:44:16 -0800904}
905
Sean Heftye51060f2006-06-17 20:37:29 -0700906static inline int cma_any_port(struct sockaddr *addr)
907{
Sean Hefty628e5f62006-11-30 16:44:16 -0800908 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700909}
910
Haggai Eran0c505f72015-07-30 17:50:19 +0300911static void cma_save_ib_info(struct sockaddr *src_addr,
912 struct sockaddr *dst_addr,
913 struct rdma_cm_id *listen_id,
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700914 struct ib_sa_path_rec *path)
Sean Heftye51060f2006-06-17 20:37:29 -0700915{
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700916 struct sockaddr_ib *listen_ib, *ib;
Sean Heftye51060f2006-06-17 20:37:29 -0700917
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700918 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
Haggai Eran0c505f72015-07-30 17:50:19 +0300919 if (src_addr) {
920 ib = (struct sockaddr_ib *)src_addr;
921 ib->sib_family = AF_IB;
922 if (path) {
923 ib->sib_pkey = path->pkey;
924 ib->sib_flowinfo = path->flow_label;
925 memcpy(&ib->sib_addr, &path->sgid, 16);
926 ib->sib_sid = path->service_id;
927 ib->sib_scope_id = 0;
928 } else {
929 ib->sib_pkey = listen_ib->sib_pkey;
930 ib->sib_flowinfo = listen_ib->sib_flowinfo;
931 ib->sib_addr = listen_ib->sib_addr;
932 ib->sib_sid = listen_ib->sib_sid;
933 ib->sib_scope_id = listen_ib->sib_scope_id;
934 }
935 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
Matthew Finlayc07678b2015-05-19 00:11:48 -0700936 }
Haggai Eran0c505f72015-07-30 17:50:19 +0300937 if (dst_addr) {
938 ib = (struct sockaddr_ib *)dst_addr;
939 ib->sib_family = AF_IB;
940 if (path) {
941 ib->sib_pkey = path->pkey;
942 ib->sib_flowinfo = path->flow_label;
943 memcpy(&ib->sib_addr, &path->dgid, 16);
944 }
Matthew Finlayc07678b2015-05-19 00:11:48 -0700945 }
Sean Heftye51060f2006-06-17 20:37:29 -0700946}
947
Haggai Eran0c505f72015-07-30 17:50:19 +0300948static void cma_save_ip4_info(struct sockaddr *src_addr,
949 struct sockaddr *dst_addr,
950 struct cma_hdr *hdr,
951 __be16 local_port)
Sean Heftye51060f2006-06-17 20:37:29 -0700952{
Jason Gunthorpe28521442015-04-20 14:01:11 -0600953 struct sockaddr_in *ip4;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700954
Haggai Eran0c505f72015-07-30 17:50:19 +0300955 if (src_addr) {
956 ip4 = (struct sockaddr_in *)src_addr;
957 ip4->sin_family = AF_INET;
958 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr;
959 ip4->sin_port = local_port;
960 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700961
Haggai Eran0c505f72015-07-30 17:50:19 +0300962 if (dst_addr) {
963 ip4 = (struct sockaddr_in *)dst_addr;
964 ip4->sin_family = AF_INET;
965 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr;
966 ip4->sin_port = hdr->port;
967 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700968}
969
Haggai Eran0c505f72015-07-30 17:50:19 +0300970static void cma_save_ip6_info(struct sockaddr *src_addr,
971 struct sockaddr *dst_addr,
972 struct cma_hdr *hdr,
973 __be16 local_port)
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700974{
Jason Gunthorpe28521442015-04-20 14:01:11 -0600975 struct sockaddr_in6 *ip6;
Sean Heftye51060f2006-06-17 20:37:29 -0700976
Haggai Eran0c505f72015-07-30 17:50:19 +0300977 if (src_addr) {
978 ip6 = (struct sockaddr_in6 *)src_addr;
979 ip6->sin6_family = AF_INET6;
980 ip6->sin6_addr = hdr->dst_addr.ip6;
981 ip6->sin6_port = local_port;
982 }
Sean Heftye51060f2006-06-17 20:37:29 -0700983
Haggai Eran0c505f72015-07-30 17:50:19 +0300984 if (dst_addr) {
985 ip6 = (struct sockaddr_in6 *)dst_addr;
986 ip6->sin6_family = AF_INET6;
987 ip6->sin6_addr = hdr->src_addr.ip6;
988 ip6->sin6_port = hdr->port;
989 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700990}
991
Haggai Eran0c505f72015-07-30 17:50:19 +0300992static u16 cma_port_from_service_id(__be64 service_id)
993{
994 return (u16)be64_to_cpu(service_id);
995}
996
997static int cma_save_ip_info(struct sockaddr *src_addr,
998 struct sockaddr *dst_addr,
999 struct ib_cm_event *ib_event,
1000 __be64 service_id)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001001{
1002 struct cma_hdr *hdr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001003 __be16 port;
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001004
1005 hdr = ib_event->private_data;
1006 if (hdr->cma_version != CMA_VERSION)
1007 return -EINVAL;
1008
Haggai Eran0c505f72015-07-30 17:50:19 +03001009 port = htons(cma_port_from_service_id(service_id));
1010
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001011 switch (cma_get_ip_ver(hdr)) {
1012 case 4:
Haggai Eran0c505f72015-07-30 17:50:19 +03001013 cma_save_ip4_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001014 break;
1015 case 6:
Haggai Eran0c505f72015-07-30 17:50:19 +03001016 cma_save_ip6_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001017 break;
1018 default:
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001019 return -EINVAL;
Sean Heftye51060f2006-06-17 20:37:29 -07001020 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001021
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001022 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001023}
1024
Haggai Eran0c505f72015-07-30 17:50:19 +03001025static int cma_save_net_info(struct sockaddr *src_addr,
1026 struct sockaddr *dst_addr,
1027 struct rdma_cm_id *listen_id,
1028 struct ib_cm_event *ib_event,
1029 sa_family_t sa_family, __be64 service_id)
1030{
1031 if (sa_family == AF_IB) {
1032 if (ib_event->event == IB_CM_REQ_RECEIVED)
1033 cma_save_ib_info(src_addr, dst_addr, listen_id,
1034 ib_event->param.req_rcvd.primary_path);
1035 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1036 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1037 return 0;
1038 }
1039
1040 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1041}
1042
Sean Heftye8160e12013-05-29 10:09:22 -07001043static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07001044{
Sean Heftye8160e12013-05-29 10:09:22 -07001045 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
Sean Heftye51060f2006-06-17 20:37:29 -07001046}
1047
Sean Heftye51060f2006-06-17 20:37:29 -07001048static void cma_cancel_route(struct rdma_id_private *id_priv)
1049{
Michael Wangfe53ba22015-05-05 14:50:36 +02001050 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001051 if (id_priv->query)
1052 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001053 }
1054}
1055
Sean Heftye51060f2006-06-17 20:37:29 -07001056static void cma_cancel_listens(struct rdma_id_private *id_priv)
1057{
1058 struct rdma_id_private *dev_id_priv;
1059
Sean Heftyd02d1f52007-10-09 11:12:34 -07001060 /*
1061 * Remove from listen_any_list to prevent added devices from spawning
1062 * additional listen requests.
1063 */
Sean Heftye51060f2006-06-17 20:37:29 -07001064 mutex_lock(&lock);
1065 list_del(&id_priv->list);
1066
1067 while (!list_empty(&id_priv->listen_list)) {
1068 dev_id_priv = list_entry(id_priv->listen_list.next,
1069 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001070 /* sync with device removal to avoid duplicate destruction */
1071 list_del_init(&dev_id_priv->list);
1072 list_del(&dev_id_priv->listen_list);
1073 mutex_unlock(&lock);
1074
1075 rdma_destroy_id(&dev_id_priv->id);
1076 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001077 }
1078 mutex_unlock(&lock);
1079}
1080
1081static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001082 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -07001083{
1084 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001085 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001086 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1087 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001088 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001089 cma_cancel_route(id_priv);
1090 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001091 case RDMA_CM_LISTEN:
Sean Heftyf4753832013-05-29 10:09:14 -07001092 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001093 cma_cancel_listens(id_priv);
1094 break;
1095 default:
1096 break;
1097 }
1098}
1099
1100static void cma_release_port(struct rdma_id_private *id_priv)
1101{
1102 struct rdma_bind_list *bind_list = id_priv->bind_list;
1103
1104 if (!bind_list)
1105 return;
1106
1107 mutex_lock(&lock);
1108 hlist_del(&id_priv->node);
1109 if (hlist_empty(&bind_list->owners)) {
Haggai Eranaac978e2015-07-30 17:50:20 +03001110 cma_ps_remove(bind_list->ps, bind_list->port);
Sean Heftye51060f2006-06-17 20:37:29 -07001111 kfree(bind_list);
1112 }
1113 mutex_unlock(&lock);
1114}
1115
Sean Heftyc8f6a362007-02-15 17:00:18 -08001116static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1117{
1118 struct cma_multicast *mc;
1119
1120 while (!list_empty(&id_priv->mc_list)) {
1121 mc = container_of(id_priv->mc_list.next,
1122 struct cma_multicast, list);
1123 list_del(&mc->list);
Michael Wanga31ad3b2015-05-05 14:50:37 +02001124 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
Michael Wang5c9a5282015-05-05 14:50:29 +02001125 id_priv->id.port_num)) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02001126 ib_sa_free_multicast(mc->multicast.ib);
1127 kfree(mc);
Michael Wang5c9a5282015-05-05 14:50:29 +02001128 } else
Eli Cohen3c86aa72010-10-13 21:26:51 +02001129 kref_put(&mc->mcref, release_mc);
Sean Heftyc8f6a362007-02-15 17:00:18 -08001130 }
1131}
1132
Sean Heftye51060f2006-06-17 20:37:29 -07001133void rdma_destroy_id(struct rdma_cm_id *id)
1134{
1135 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001136 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -07001137
1138 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001139 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001140 cma_cancel_operation(id_priv, state);
1141
Sean Heftya396d432011-02-23 09:05:39 -08001142 /*
1143 * Wait for any active callback to finish. New callbacks will find
1144 * the id_priv state set to destroying and abort.
1145 */
1146 mutex_lock(&id_priv->handler_mutex);
1147 mutex_unlock(&id_priv->handler_mutex);
1148
Sean Heftye51060f2006-06-17 20:37:29 -07001149 if (id_priv->cma_dev) {
Michael Wang72219cea2015-05-05 14:50:34 +02001150 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001151 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07001152 ib_destroy_cm_id(id_priv->cm_id.ib);
Michael Wang04215332015-05-05 14:50:35 +02001153 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001154 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -05001155 iw_destroy_cm_id(id_priv->cm_id.iw);
Sean Heftye51060f2006-06-17 20:37:29 -07001156 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001157 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -08001158 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001159 }
1160
1161 cma_release_port(id_priv);
1162 cma_deref_id(id_priv);
1163 wait_for_completion(&id_priv->comp);
1164
Sean Heftyd02d1f52007-10-09 11:12:34 -07001165 if (id_priv->internal_id)
1166 cma_deref_id(id_priv->id.context);
1167
Sean Heftye51060f2006-06-17 20:37:29 -07001168 kfree(id_priv->id.route.path_rec);
1169 kfree(id_priv);
1170}
1171EXPORT_SYMBOL(rdma_destroy_id);
1172
1173static int cma_rep_recv(struct rdma_id_private *id_priv)
1174{
1175 int ret;
1176
Sean Hefty5851bb82008-01-04 10:47:12 -08001177 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001178 if (ret)
1179 goto reject;
1180
Sean Hefty5851bb82008-01-04 10:47:12 -08001181 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001182 if (ret)
1183 goto reject;
1184
1185 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1186 if (ret)
1187 goto reject;
1188
1189 return 0;
1190reject:
Sean Heftyc5483382007-09-24 13:19:09 -07001191 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001192 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1193 NULL, 0, NULL, 0);
1194 return ret;
1195}
1196
Sean Heftya1b1b612006-11-30 16:33:14 -08001197static void cma_set_rep_event_data(struct rdma_cm_event *event,
1198 struct ib_cm_rep_event_param *rep_data,
1199 void *private_data)
1200{
1201 event->param.conn.private_data = private_data;
1202 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1203 event->param.conn.responder_resources = rep_data->responder_resources;
1204 event->param.conn.initiator_depth = rep_data->initiator_depth;
1205 event->param.conn.flow_control = rep_data->flow_control;
1206 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1207 event->param.conn.srq = rep_data->srq;
1208 event->param.conn.qp_num = rep_data->remote_qpn;
1209}
1210
Sean Heftye51060f2006-06-17 20:37:29 -07001211static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1212{
1213 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001214 struct rdma_cm_event event;
1215 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001216
Amir Vadai38ca83a2008-07-22 14:14:23 -07001217 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001218 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001219 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001220 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -07001221 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001222
Sean Heftya1b1b612006-11-30 16:33:14 -08001223 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001224 switch (ib_event->event) {
1225 case IB_CM_REQ_ERROR:
1226 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001227 event.event = RDMA_CM_EVENT_UNREACHABLE;
1228 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001229 break;
1230 case IB_CM_REP_RECEIVED:
Sean Hefty01602f12013-05-29 10:09:20 -07001231 if (id_priv->id.qp) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001232 event.status = cma_rep_recv(id_priv);
1233 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1234 RDMA_CM_EVENT_ESTABLISHED;
Sean Hefty01602f12013-05-29 10:09:20 -07001235 } else {
Sean Heftya1b1b612006-11-30 16:33:14 -08001236 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
Sean Hefty01602f12013-05-29 10:09:20 -07001237 }
Sean Heftya1b1b612006-11-30 16:33:14 -08001238 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1239 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001240 break;
1241 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001242 case IB_CM_USER_ESTABLISHED:
1243 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001244 break;
1245 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001246 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001247 case IB_CM_DREQ_RECEIVED:
1248 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001249 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1250 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001251 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001252 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001253 break;
1254 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001255 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1256 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001257 case IB_CM_MRA_RECEIVED:
1258 /* ignore event */
1259 goto out;
1260 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001261 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001262 event.status = ib_event->param.rej_rcvd.reason;
1263 event.event = RDMA_CM_EVENT_REJECTED;
1264 event.param.conn.private_data = ib_event->private_data;
1265 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001266 break;
1267 default:
Roland Dreier468f2232008-07-14 23:48:47 -07001268 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001269 ib_event->event);
1270 goto out;
1271 }
1272
Sean Heftya1b1b612006-11-30 16:33:14 -08001273 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001274 if (ret) {
1275 /* Destroy the CM ID by returning a non-zero value. */
1276 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001277 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001278 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001279 rdma_destroy_id(&id_priv->id);
1280 return ret;
1281 }
1282out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001283 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001284 return ret;
1285}
1286
Sean Hefty628e5f62006-11-30 16:44:16 -08001287static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1288 struct ib_cm_event *ib_event)
Sean Heftye51060f2006-06-17 20:37:29 -07001289{
1290 struct rdma_id_private *id_priv;
1291 struct rdma_cm_id *id;
1292 struct rdma_route *rt;
Haggai Eran0c505f72015-07-30 17:50:19 +03001293 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1294 const __be64 service_id =
1295 ib_event->param.req_rcvd.primary_path->service_id;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001296 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001297
Krishna Kumar3f168d22006-09-29 12:09:51 -07001298 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001299 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001300 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001301 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001302
Sean Heftyf4753832013-05-29 10:09:14 -07001303 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001304 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1305 (struct sockaddr *)&id->route.addr.dst_addr,
1306 listen_id, ib_event, ss_family, service_id))
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001307 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001308
1309 rt = &id->route;
1310 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1311 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1312 GFP_KERNEL);
1313 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001314 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001315
Sean Heftye51060f2006-06-17 20:37:29 -07001316 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1317 if (rt->num_paths == 2)
1318 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1319
Sean Heftyf4753832013-05-29 10:09:14 -07001320 if (cma_any_addr(cma_src_addr(id_priv))) {
Sean Hefty6f8372b2009-11-19 13:26:06 -08001321 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1322 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
Sean Hefty46ea5062011-12-06 21:15:18 +00001323 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
Sean Hefty6f8372b2009-11-19 13:26:06 -08001324 } else {
Sean Heftyf4753832013-05-29 10:09:14 -07001325 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001326 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001327 goto err;
Sean Hefty6f8372b2009-11-19 13:26:06 -08001328 }
1329 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001330
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001331 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001332 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001333
Krishna Kumar3f168d22006-09-29 12:09:51 -07001334err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001335 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001336 return NULL;
1337}
1338
Sean Hefty628e5f62006-11-30 16:44:16 -08001339static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1340 struct ib_cm_event *ib_event)
1341{
1342 struct rdma_id_private *id_priv;
1343 struct rdma_cm_id *id;
Haggai Eran0c505f72015-07-30 17:50:19 +03001344 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
Sean Hefty628e5f62006-11-30 16:44:16 -08001345 int ret;
1346
1347 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001348 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001349 if (IS_ERR(id))
1350 return NULL;
1351
Sean Heftyf4753832013-05-29 10:09:14 -07001352 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001353 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1354 (struct sockaddr *)&id->route.addr.dst_addr,
1355 listen_id, ib_event, ss_family,
1356 ib_event->param.sidr_req_rcvd.service_id))
Sean Hefty628e5f62006-11-30 16:44:16 -08001357 goto err;
1358
Sean Hefty6f8372b2009-11-19 13:26:06 -08001359 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
Sean Heftyf4753832013-05-29 10:09:14 -07001360 ret = cma_translate_addr(cma_src_addr(id_priv), &id->route.addr.dev_addr);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001361 if (ret)
1362 goto err;
1363 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001364
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001365 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001366 return id_priv;
1367err:
1368 rdma_destroy_id(id);
1369 return NULL;
1370}
1371
Sean Heftya1b1b612006-11-30 16:33:14 -08001372static void cma_set_req_event_data(struct rdma_cm_event *event,
1373 struct ib_cm_req_event_param *req_data,
1374 void *private_data, int offset)
1375{
1376 event->param.conn.private_data = private_data + offset;
1377 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1378 event->param.conn.responder_resources = req_data->responder_resources;
1379 event->param.conn.initiator_depth = req_data->initiator_depth;
1380 event->param.conn.flow_control = req_data->flow_control;
1381 event->param.conn.retry_count = req_data->retry_count;
1382 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1383 event->param.conn.srq = req_data->srq;
1384 event->param.conn.qp_num = req_data->remote_qpn;
1385}
1386
Hefty, Sean95954802011-10-06 09:32:33 -07001387static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1388{
Sean Hefty4dd81e82012-06-14 20:49:09 +00001389 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
Hefty, Sean95954802011-10-06 09:32:33 -07001390 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1391 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1392 (id->qp_type == IB_QPT_UD)) ||
1393 (!id->qp_type));
1394}
1395
Sean Heftye51060f2006-06-17 20:37:29 -07001396static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1397{
1398 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001399 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001400 int offset, ret;
1401
1402 listen_id = cm_id->context;
Hefty, Sean95954802011-10-06 09:32:33 -07001403 if (!cma_check_req_qp_type(&listen_id->id, ib_event))
1404 return -EINVAL;
1405
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001406 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001407 return -ECONNABORTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001408
Sean Hefty628e5f62006-11-30 16:44:16 -08001409 memset(&event, 0, sizeof event);
Sean Heftye8160e12013-05-29 10:09:22 -07001410 offset = cma_user_data_offset(listen_id);
Sean Hefty628e5f62006-11-30 16:44:16 -08001411 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001412 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Sean Hefty628e5f62006-11-30 16:44:16 -08001413 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1414 event.param.ud.private_data = ib_event->private_data + offset;
1415 event.param.ud.private_data_len =
1416 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1417 } else {
1418 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1419 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1420 ib_event->private_data, offset);
1421 }
Sean Heftye51060f2006-06-17 20:37:29 -07001422 if (!conn_id) {
1423 ret = -ENOMEM;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001424 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001425 }
1426
Or Gerlitzde910bd2008-07-14 23:48:53 -07001427 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001428 ret = cma_acquire_dev(conn_id, listen_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301429 if (ret)
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001430 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001431
1432 conn_id->cm_id.ib = cm_id;
1433 cm_id->context = conn_id;
1434 cm_id->cm_handler = cma_ib_handler;
1435
Sean Hefty25ae21a2011-02-23 08:11:32 -08001436 /*
1437 * Protect against the user destroying conn_id from another thread
1438 * until we're done accessing it.
1439 */
1440 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001441 ret = conn_id->id.event_handler(&conn_id->id, &event);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001442 if (ret)
1443 goto err3;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001444 /*
1445 * Acquire mutex to prevent user executing rdma_destroy_id()
1446 * while we're accessing the cm_id.
1447 */
1448 mutex_lock(&lock);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001449 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1450 (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001451 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1452 mutex_unlock(&lock);
1453 mutex_unlock(&conn_id->handler_mutex);
1454 mutex_unlock(&listen_id->handler_mutex);
1455 cma_deref_id(conn_id);
1456 return 0;
1457
1458err3:
1459 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301460 /* Destroy the CM ID by returning a non-zero value. */
1461 conn_id->cm_id.ib = NULL;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001462err2:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001463 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001464 mutex_unlock(&conn_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001465err1:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001466 mutex_unlock(&listen_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001467 if (conn_id)
1468 rdma_destroy_id(&conn_id->id);
Sean Heftye51060f2006-06-17 20:37:29 -07001469 return ret;
1470}
1471
Sean Heftycf539362013-05-29 10:09:28 -07001472__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
Sean Heftye51060f2006-06-17 20:37:29 -07001473{
Sean Hefty496ce3c2013-05-29 10:09:19 -07001474 if (addr->sa_family == AF_IB)
1475 return ((struct sockaddr_ib *) addr)->sib_sid;
1476
Sean Heftycf539362013-05-29 10:09:28 -07001477 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001478}
Sean Heftycf539362013-05-29 10:09:28 -07001479EXPORT_SYMBOL(rdma_get_service_id);
Sean Heftye51060f2006-06-17 20:37:29 -07001480
1481static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1482 struct ib_cm_compare_data *compare)
1483{
1484 struct cma_hdr *cma_data, *cma_mask;
Al Viro1b90c132008-03-29 03:10:28 +00001485 __be32 ip4_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001486 struct in6_addr ip6_addr;
1487
1488 memset(compare, 0, sizeof *compare);
1489 cma_data = (void *) compare->data;
1490 cma_mask = (void *) compare->mask;
Sean Heftye51060f2006-06-17 20:37:29 -07001491
1492 switch (addr->sa_family) {
1493 case AF_INET:
1494 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
Sean Hefty01602f12013-05-29 10:09:20 -07001495 cma_set_ip_ver(cma_data, 4);
1496 cma_set_ip_ver(cma_mask, 0xF);
1497 if (!cma_any_addr(addr)) {
1498 cma_data->dst_addr.ip4.addr = ip4_addr;
1499 cma_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001500 }
1501 break;
1502 case AF_INET6:
1503 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
Sean Hefty01602f12013-05-29 10:09:20 -07001504 cma_set_ip_ver(cma_data, 6);
1505 cma_set_ip_ver(cma_mask, 0xF);
1506 if (!cma_any_addr(addr)) {
1507 cma_data->dst_addr.ip6 = ip6_addr;
1508 memset(&cma_mask->dst_addr.ip6, 0xFF,
1509 sizeof cma_mask->dst_addr.ip6);
Sean Heftye51060f2006-06-17 20:37:29 -07001510 }
1511 break;
1512 default:
1513 break;
1514 }
1515}
1516
Tom Tucker07ebafb2006-08-03 16:02:42 -05001517static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1518{
1519 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001520 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001521 int ret = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05301522 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1523 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001524
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001525 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001526 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001527
Sean Heftybe65f082007-05-07 11:49:12 -07001528 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001529 switch (iw_event->event) {
1530 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001531 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001532 break;
1533 case IW_CM_EVENT_CONNECT_REPLY:
Steve Wise24d44a32013-07-04 16:10:44 +05301534 memcpy(cma_src_addr(id_priv), laddr,
1535 rdma_addr_size(laddr));
1536 memcpy(cma_dst_addr(id_priv), raddr,
1537 rdma_addr_size(raddr));
Steve Wise881a0452006-12-15 16:50:17 -06001538 switch (iw_event->status) {
1539 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001540 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301541 event.param.conn.initiator_depth = iw_event->ird;
1542 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06001543 break;
1544 case -ECONNRESET:
1545 case -ECONNREFUSED:
1546 event.event = RDMA_CM_EVENT_REJECTED;
1547 break;
1548 case -ETIMEDOUT:
1549 event.event = RDMA_CM_EVENT_UNREACHABLE;
1550 break;
1551 default:
1552 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1553 break;
1554 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001555 break;
1556 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001557 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301558 event.param.conn.initiator_depth = iw_event->ird;
1559 event.param.conn.responder_resources = iw_event->ord;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001560 break;
1561 default:
1562 BUG_ON(1);
1563 }
1564
Sean Heftya1b1b612006-11-30 16:33:14 -08001565 event.status = iw_event->status;
1566 event.param.conn.private_data = iw_event->private_data;
1567 event.param.conn.private_data_len = iw_event->private_data_len;
1568 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001569 if (ret) {
1570 /* Destroy the CM ID by returning a non-zero value. */
1571 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001572 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001573 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001574 rdma_destroy_id(&id_priv->id);
1575 return ret;
1576 }
1577
Or Gerlitzde910bd2008-07-14 23:48:53 -07001578 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001579 return ret;
1580}
1581
1582static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1583 struct iw_cm_event *iw_event)
1584{
1585 struct rdma_cm_id *new_cm_id;
1586 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001587 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001588 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001589 struct ib_device_attr attr;
Steve Wise24d44a32013-07-04 16:10:44 +05301590 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1591 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001592
1593 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001594 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001595 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001596
1597 /* Create a new RDMA id for the new IW CM ID */
1598 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1599 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001600 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07001601 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001602 ret = -ENOMEM;
1603 goto out;
1604 }
1605 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001606 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001607 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001608
Matan Barakdd5f03b2013-12-12 18:03:11 +02001609 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001610 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001611 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001612 rdma_destroy_id(new_cm_id);
1613 goto out;
1614 }
1615
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001616 ret = cma_acquire_dev(conn_id, listen_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001617 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001618 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001619 rdma_destroy_id(new_cm_id);
1620 goto out;
1621 }
1622
1623 conn_id->cm_id.iw = cm_id;
1624 cm_id->context = conn_id;
1625 cm_id->cm_handler = cma_iw_handler;
1626
Steve Wise24d44a32013-07-04 16:10:44 +05301627 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
1628 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
Tom Tucker07ebafb2006-08-03 16:02:42 -05001629
Steve Wise8d8293c2007-10-29 11:34:05 -05001630 ret = ib_query_device(conn_id->id.device, &attr);
1631 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001632 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001633 rdma_destroy_id(new_cm_id);
1634 goto out;
1635 }
1636
Sean Heftya1b1b612006-11-30 16:33:14 -08001637 memset(&event, 0, sizeof event);
1638 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1639 event.param.conn.private_data = iw_event->private_data;
1640 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301641 event.param.conn.initiator_depth = iw_event->ird;
1642 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08001643
1644 /*
1645 * Protect against the user destroying conn_id from another thread
1646 * until we're done accessing it.
1647 */
1648 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001649 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001650 if (ret) {
1651 /* User wants to destroy the CM ID */
1652 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001653 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001654 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001655 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001656 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001657 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001658 }
1659
Or Gerlitzde910bd2008-07-14 23:48:53 -07001660 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001661 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001662
Tom Tucker07ebafb2006-08-03 16:02:42 -05001663out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001664 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001665 return ret;
1666}
1667
Sean Heftye51060f2006-06-17 20:37:29 -07001668static int cma_ib_listen(struct rdma_id_private *id_priv)
1669{
1670 struct ib_cm_compare_data compare_data;
1671 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001672 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07001673 __be64 svc_id;
1674 int ret;
1675
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001676 id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
1677 if (IS_ERR(id))
1678 return PTR_ERR(id);
1679
1680 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07001681
Sean Heftyf4753832013-05-29 10:09:14 -07001682 addr = cma_src_addr(id_priv);
Sean Heftycf539362013-05-29 10:09:28 -07001683 svc_id = rdma_get_service_id(&id_priv->id, addr);
Sean Hefty406b6a22012-06-14 20:31:39 +00001684 if (cma_any_addr(addr) && !id_priv->afonly)
Sean Heftye51060f2006-06-17 20:37:29 -07001685 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1686 else {
1687 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1688 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1689 }
1690
1691 if (ret) {
1692 ib_destroy_cm_id(id_priv->cm_id.ib);
1693 id_priv->cm_id.ib = NULL;
1694 }
1695
1696 return ret;
1697}
1698
Tom Tucker07ebafb2006-08-03 16:02:42 -05001699static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1700{
1701 int ret;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001702 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001703
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001704 id = iw_create_cm_id(id_priv->id.device,
1705 iw_conn_req_handler,
1706 id_priv);
1707 if (IS_ERR(id))
1708 return PTR_ERR(id);
1709
Steve Wise68cdba02015-05-18 15:27:10 -05001710 id->tos = id_priv->tos;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001711 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001712
Steve Wise24d44a32013-07-04 16:10:44 +05301713 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
1714 rdma_addr_size(cma_src_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05001715
1716 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1717
1718 if (ret) {
1719 iw_destroy_cm_id(id_priv->cm_id.iw);
1720 id_priv->cm_id.iw = NULL;
1721 }
1722
1723 return ret;
1724}
1725
Sean Heftye51060f2006-06-17 20:37:29 -07001726static int cma_listen_handler(struct rdma_cm_id *id,
1727 struct rdma_cm_event *event)
1728{
1729 struct rdma_id_private *id_priv = id->context;
1730
1731 id->context = id_priv->id.context;
1732 id->event_handler = id_priv->id.event_handler;
1733 return id_priv->id.event_handler(id, event);
1734}
1735
1736static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1737 struct cma_device *cma_dev)
1738{
1739 struct rdma_id_private *dev_id_priv;
1740 struct rdma_cm_id *id;
1741 int ret;
1742
Michael Wang72219cea2015-05-05 14:50:34 +02001743 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
Sean Hefty94d0c932013-05-29 10:09:24 -07001744 return;
1745
Sean Heftyb26f9b92010-04-01 17:08:41 +00001746 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
1747 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07001748 if (IS_ERR(id))
1749 return;
1750
1751 dev_id_priv = container_of(id, struct rdma_id_private, id);
1752
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001753 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftyf4753832013-05-29 10:09:14 -07001754 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
1755 rdma_addr_size(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07001756
1757 cma_attach_to_dev(dev_id_priv, cma_dev);
1758 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001759 atomic_inc(&id_priv->refcount);
1760 dev_id_priv->internal_id = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00001761 dev_id_priv->afonly = id_priv->afonly;
Sean Heftye51060f2006-06-17 20:37:29 -07001762
1763 ret = rdma_listen(id, id_priv->backlog);
1764 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07001765 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07001766 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07001767}
1768
1769static void cma_listen_on_all(struct rdma_id_private *id_priv)
1770{
1771 struct cma_device *cma_dev;
1772
1773 mutex_lock(&lock);
1774 list_add_tail(&id_priv->list, &listen_any_list);
1775 list_for_each_entry(cma_dev, &dev_list, list)
1776 cma_listen_on_dev(id_priv, cma_dev);
1777 mutex_unlock(&lock);
1778}
1779
Sean Heftya81c9942007-08-08 15:51:06 -07001780void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1781{
1782 struct rdma_id_private *id_priv;
1783
1784 id_priv = container_of(id, struct rdma_id_private, id);
1785 id_priv->tos = (u8) tos;
1786}
1787EXPORT_SYMBOL(rdma_set_service_type);
1788
Sean Heftye51060f2006-06-17 20:37:29 -07001789static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1790 void *context)
1791{
1792 struct cma_work *work = context;
1793 struct rdma_route *route;
1794
1795 route = &work->id->id.route;
1796
1797 if (!status) {
1798 route->num_paths = 1;
1799 *route->path_rec = *path_rec;
1800 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001801 work->old_state = RDMA_CM_ROUTE_QUERY;
1802 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001803 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07001804 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001805 }
1806
1807 queue_work(cma_wq, &work->work);
1808}
1809
1810static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1811 struct cma_work *work)
1812{
Sean Heftyf4753832013-05-29 10:09:14 -07001813 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001814 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07001815 ib_sa_comp_mask comp_mask;
1816 struct sockaddr_in6 *sin6;
Sean Heftyf68194c2013-05-29 10:09:18 -07001817 struct sockaddr_ib *sib;
Sean Heftye51060f2006-06-17 20:37:29 -07001818
1819 memset(&path_rec, 0, sizeof path_rec);
Sean Heftyf4753832013-05-29 10:09:14 -07001820 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
1821 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
1822 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001823 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08001824 path_rec.reversible = 1;
Sean Heftycf539362013-05-29 10:09:28 -07001825 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftya81c9942007-08-08 15:51:06 -07001826
1827 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1828 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1829 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1830
Sean Heftyf68194c2013-05-29 10:09:18 -07001831 switch (cma_family(id_priv)) {
1832 case AF_INET:
Sean Heftya81c9942007-08-08 15:51:06 -07001833 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1834 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07001835 break;
1836 case AF_INET6:
Sean Heftyf4753832013-05-29 10:09:14 -07001837 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
Sean Heftya81c9942007-08-08 15:51:06 -07001838 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1839 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07001840 break;
1841 case AF_IB:
1842 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
1843 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
1844 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1845 break;
Sean Heftya81c9942007-08-08 15:51:06 -07001846 }
Sean Heftye51060f2006-06-17 20:37:29 -07001847
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001848 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07001849 id_priv->id.port_num, &path_rec,
1850 comp_mask, timeout_ms,
1851 GFP_KERNEL, cma_query_handler,
1852 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001853
1854 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1855}
1856
David Howellsc4028952006-11-22 14:57:56 +00001857static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07001858{
David Howellsc4028952006-11-22 14:57:56 +00001859 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07001860 struct rdma_id_private *id_priv = work->id;
1861 int destroy = 0;
1862
Or Gerlitzde910bd2008-07-14 23:48:53 -07001863 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001864 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1865 goto out;
1866
1867 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001868 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001869 destroy = 1;
1870 }
1871out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001872 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001873 cma_deref_id(id_priv);
1874 if (destroy)
1875 rdma_destroy_id(&id_priv->id);
1876 kfree(work);
1877}
1878
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001879static void cma_ndev_work_handler(struct work_struct *_work)
1880{
1881 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1882 struct rdma_id_private *id_priv = work->id;
1883 int destroy = 0;
1884
1885 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001886 if (id_priv->state == RDMA_CM_DESTROYING ||
1887 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001888 goto out;
1889
1890 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001891 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001892 destroy = 1;
1893 }
1894
1895out:
1896 mutex_unlock(&id_priv->handler_mutex);
1897 cma_deref_id(id_priv);
1898 if (destroy)
1899 rdma_destroy_id(&id_priv->id);
1900 kfree(work);
1901}
1902
Sean Heftye51060f2006-06-17 20:37:29 -07001903static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1904{
1905 struct rdma_route *route = &id_priv->id.route;
1906 struct cma_work *work;
1907 int ret;
1908
1909 work = kzalloc(sizeof *work, GFP_KERNEL);
1910 if (!work)
1911 return -ENOMEM;
1912
1913 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001914 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001915 work->old_state = RDMA_CM_ROUTE_QUERY;
1916 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001917 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1918
1919 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1920 if (!route->path_rec) {
1921 ret = -ENOMEM;
1922 goto err1;
1923 }
1924
1925 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1926 if (ret)
1927 goto err2;
1928
1929 return 0;
1930err2:
1931 kfree(route->path_rec);
1932 route->path_rec = NULL;
1933err1:
1934 kfree(work);
1935 return ret;
1936}
1937
1938int rdma_set_ib_paths(struct rdma_cm_id *id,
1939 struct ib_sa_path_rec *path_rec, int num_paths)
1940{
1941 struct rdma_id_private *id_priv;
1942 int ret;
1943
1944 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001945 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1946 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07001947 return -EINVAL;
1948
Julia Lawall9893e742010-05-15 23:22:38 +02001949 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1950 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07001951 if (!id->route.path_rec) {
1952 ret = -ENOMEM;
1953 goto err;
1954 }
1955
Sean Heftyae2d9292010-03-25 19:12:36 +00001956 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07001957 return 0;
1958err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001959 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07001960 return ret;
1961}
1962EXPORT_SYMBOL(rdma_set_ib_paths);
1963
Tom Tucker07ebafb2006-08-03 16:02:42 -05001964static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1965{
1966 struct cma_work *work;
1967
1968 work = kzalloc(sizeof *work, GFP_KERNEL);
1969 if (!work)
1970 return -ENOMEM;
1971
1972 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001973 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001974 work->old_state = RDMA_CM_ROUTE_QUERY;
1975 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001976 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1977 queue_work(cma_wq, &work->work);
1978 return 0;
1979}
1980
Eyal Perryeb072c42013-11-06 15:37:24 +02001981static int iboe_tos_to_sl(struct net_device *ndev, int tos)
1982{
1983 int prio;
1984 struct net_device *dev;
1985
1986 prio = rt_tos2priority(tos);
1987 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
1988 vlan_dev_real_dev(ndev) : ndev;
1989
1990 if (dev->num_tc)
1991 return netdev_get_prio_tc_map(dev, prio);
1992
1993#if IS_ENABLED(CONFIG_VLAN_8021Q)
1994 if (ndev->priv_flags & IFF_802_1Q_VLAN)
1995 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
1996 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1997#endif
1998 return 0;
1999}
2000
Eli Cohen3c86aa72010-10-13 21:26:51 +02002001static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2002{
2003 struct rdma_route *route = &id_priv->id.route;
2004 struct rdma_addr *addr = &route->addr;
2005 struct cma_work *work;
2006 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002007 struct net_device *ndev = NULL;
Matan Barakdd5f03b2013-12-12 18:03:11 +02002008
Eli Cohen3c86aa72010-10-13 21:26:51 +02002009
Eli Cohen3c86aa72010-10-13 21:26:51 +02002010 work = kzalloc(sizeof *work, GFP_KERNEL);
2011 if (!work)
2012 return -ENOMEM;
2013
2014 work->id = id_priv;
2015 INIT_WORK(&work->work, cma_work_handler);
2016
2017 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2018 if (!route->path_rec) {
2019 ret = -ENOMEM;
2020 goto err1;
2021 }
2022
2023 route->num_paths = 1;
2024
Eli Cohen3c86aa72010-10-13 21:26:51 +02002025 if (addr->dev_addr.bound_dev_if)
2026 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
2027 if (!ndev) {
2028 ret = -ENODEV;
2029 goto err2;
2030 }
2031
Matan Barakdd5f03b2013-12-12 18:03:11 +02002032 route->path_rec->vlan_id = rdma_vlan_dev_vlan_id(ndev);
2033 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
2034 memcpy(route->path_rec->smac, ndev->dev_addr, ndev->addr_len);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002035
Moni Shoua7b856272013-12-12 18:03:12 +02002036 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2037 &route->path_rec->sgid);
2038 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2039 &route->path_rec->dgid);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002040
2041 route->path_rec->hop_limit = 1;
2042 route->path_rec->reversible = 1;
2043 route->path_rec->pkey = cpu_to_be16(0xffff);
2044 route->path_rec->mtu_selector = IB_SA_EQ;
Eyal Perryeb072c42013-11-06 15:37:24 +02002045 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
Eli Cohen3c86aa72010-10-13 21:26:51 +02002046 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2047 route->path_rec->rate_selector = IB_SA_EQ;
2048 route->path_rec->rate = iboe_get_rate(ndev);
2049 dev_put(ndev);
2050 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2051 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2052 if (!route->path_rec->mtu) {
2053 ret = -EINVAL;
2054 goto err2;
2055 }
2056
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002057 work->old_state = RDMA_CM_ROUTE_QUERY;
2058 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002059 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2060 work->event.status = 0;
2061
2062 queue_work(cma_wq, &work->work);
2063
2064 return 0;
2065
2066err2:
2067 kfree(route->path_rec);
2068 route->path_rec = NULL;
2069err1:
2070 kfree(work);
2071 return ret;
2072}
2073
Sean Heftye51060f2006-06-17 20:37:29 -07002074int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2075{
2076 struct rdma_id_private *id_priv;
2077 int ret;
2078
2079 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002080 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002081 return -EINVAL;
2082
2083 atomic_inc(&id_priv->refcount);
Michael Wangfe53ba22015-05-05 14:50:36 +02002084 if (rdma_cap_ib_sa(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002085 ret = cma_resolve_ib_route(id_priv, timeout_ms);
Ira Weiny5d9fb042015-05-14 15:01:46 -04002086 else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002087 ret = cma_resolve_iboe_route(id_priv);
2088 else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002089 ret = cma_resolve_iw_route(id_priv, timeout_ms);
Michael Wangc72f2182015-05-05 14:50:28 +02002090 else
Sean Heftye51060f2006-06-17 20:37:29 -07002091 ret = -ENOSYS;
Michael Wangc72f2182015-05-05 14:50:28 +02002092
Sean Heftye51060f2006-06-17 20:37:29 -07002093 if (ret)
2094 goto err;
2095
2096 return 0;
2097err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002098 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002099 cma_deref_id(id_priv);
2100 return ret;
2101}
2102EXPORT_SYMBOL(rdma_resolve_route);
2103
Sean Hefty6a3e3622013-05-29 10:09:13 -07002104static void cma_set_loopback(struct sockaddr *addr)
2105{
2106 switch (addr->sa_family) {
2107 case AF_INET:
2108 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2109 break;
2110 case AF_INET6:
2111 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2112 0, 0, 0, htonl(1));
2113 break;
2114 default:
2115 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2116 0, 0, 0, htonl(1));
2117 break;
2118 }
2119}
2120
Sean Heftye51060f2006-06-17 20:37:29 -07002121static int cma_bind_loopback(struct rdma_id_private *id_priv)
2122{
Sean Heftyb0569e42013-05-29 10:09:15 -07002123 struct cma_device *cma_dev, *cur_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07002124 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002125 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002126 u16 pkey;
2127 int ret;
2128 u8 p;
2129
Sean Heftyb0569e42013-05-29 10:09:15 -07002130 cma_dev = NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07002131 mutex_lock(&lock);
Sean Heftyb0569e42013-05-29 10:09:15 -07002132 list_for_each_entry(cur_dev, &dev_list, list) {
2133 if (cma_family(id_priv) == AF_IB &&
Michael Wang72219cea2015-05-05 14:50:34 +02002134 !rdma_cap_ib_cm(cur_dev->device, 1))
Sean Heftyb0569e42013-05-29 10:09:15 -07002135 continue;
2136
2137 if (!cma_dev)
2138 cma_dev = cur_dev;
2139
2140 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2141 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2142 port_attr.state == IB_PORT_ACTIVE) {
2143 cma_dev = cur_dev;
2144 goto port_found;
2145 }
2146 }
2147 }
2148
2149 if (!cma_dev) {
Sean Heftye51060f2006-06-17 20:37:29 -07002150 ret = -ENODEV;
2151 goto out;
2152 }
Krishna Kumare82153b2006-10-16 10:09:01 +05302153
2154 p = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07002155
2156port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002157 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002158 if (ret)
2159 goto out;
2160
2161 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2162 if (ret)
2163 goto out;
2164
Sean Hefty6f8372b2009-11-19 13:26:06 -08002165 id_priv->id.route.addr.dev_addr.dev_type =
Michael Wang21655af2015-05-05 14:50:27 +02002166 (rdma_protocol_ib(cma_dev->device, p)) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08002167 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2168
2169 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002170 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2171 id_priv->id.port_num = p;
2172 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf4753832013-05-29 10:09:14 -07002173 cma_set_loopback(cma_src_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002174out:
2175 mutex_unlock(&lock);
2176 return ret;
2177}
2178
2179static void addr_handler(int status, struct sockaddr *src_addr,
2180 struct rdma_dev_addr *dev_addr, void *context)
2181{
2182 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08002183 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002184
Sean Heftya1b1b612006-11-30 16:33:14 -08002185 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002186 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002187 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2188 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07002189 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07002190
Moni Shoua7b856272013-12-12 18:03:12 +02002191 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
Sean Hefty61a73c72006-09-01 15:33:55 -07002192 if (!status && !id_priv->cma_dev)
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002193 status = cma_acquire_dev(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002194
2195 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002196 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2197 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002198 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08002199 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2200 event.status = status;
Moni Shoua7b856272013-12-12 18:03:12 +02002201 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08002202 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002203
Sean Heftya1b1b612006-11-30 16:33:14 -08002204 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002205 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002206 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002207 cma_deref_id(id_priv);
2208 rdma_destroy_id(&id_priv->id);
2209 return;
2210 }
2211out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002212 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002213 cma_deref_id(id_priv);
2214}
2215
2216static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2217{
2218 struct cma_work *work;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002219 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002220 int ret;
2221
2222 work = kzalloc(sizeof *work, GFP_KERNEL);
2223 if (!work)
2224 return -ENOMEM;
2225
2226 if (!id_priv->cma_dev) {
2227 ret = cma_bind_loopback(id_priv);
2228 if (ret)
2229 goto err;
2230 }
2231
Sean Hefty6f8372b2009-11-19 13:26:06 -08002232 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2233 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002234
Sean Heftye51060f2006-06-17 20:37:29 -07002235 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002236 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002237 work->old_state = RDMA_CM_ADDR_QUERY;
2238 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002239 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2240 queue_work(cma_wq, &work->work);
2241 return 0;
2242err:
2243 kfree(work);
2244 return ret;
2245}
2246
Sean Heftyf17df3b2013-05-29 10:09:17 -07002247static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2248{
2249 struct cma_work *work;
2250 int ret;
2251
2252 work = kzalloc(sizeof *work, GFP_KERNEL);
2253 if (!work)
2254 return -ENOMEM;
2255
2256 if (!id_priv->cma_dev) {
2257 ret = cma_resolve_ib_dev(id_priv);
2258 if (ret)
2259 goto err;
2260 }
2261
2262 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2263 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2264
2265 work->id = id_priv;
2266 INIT_WORK(&work->work, cma_work_handler);
2267 work->old_state = RDMA_CM_ADDR_QUERY;
2268 work->new_state = RDMA_CM_ADDR_RESOLVED;
2269 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2270 queue_work(cma_wq, &work->work);
2271 return 0;
2272err:
2273 kfree(work);
2274 return ret;
2275}
2276
Sean Heftye51060f2006-06-17 20:37:29 -07002277static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2278 struct sockaddr *dst_addr)
2279{
Sean Heftyd14714d2009-11-19 16:46:25 -08002280 if (!src_addr || !src_addr->sa_family) {
2281 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002282 src_addr->sa_family = dst_addr->sa_family;
2283 if (dst_addr->sa_family == AF_INET6) {
Spencer Baugh6c26a772015-08-13 12:19:10 -07002284 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2285 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2286 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2287 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2288 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002289 } else if (dst_addr->sa_family == AF_IB) {
2290 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2291 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
Sean Heftyd14714d2009-11-19 16:46:25 -08002292 }
2293 }
2294 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002295}
2296
2297int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2298 struct sockaddr *dst_addr, int timeout_ms)
2299{
2300 struct rdma_id_private *id_priv;
2301 int ret;
2302
2303 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002304 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002305 ret = cma_bind_addr(id, src_addr, dst_addr);
2306 if (ret)
2307 return ret;
2308 }
2309
Sean Hefty4ae71522013-05-29 10:09:16 -07002310 if (cma_family(id_priv) != dst_addr->sa_family)
2311 return -EINVAL;
2312
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002313 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002314 return -EINVAL;
2315
2316 atomic_inc(&id_priv->refcount);
Sean Heftyf4753832013-05-29 10:09:14 -07002317 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
Sean Heftyf17df3b2013-05-29 10:09:17 -07002318 if (cma_any_addr(dst_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002319 ret = cma_resolve_loopback(id_priv);
Sean Heftyf17df3b2013-05-29 10:09:17 -07002320 } else {
2321 if (dst_addr->sa_family == AF_IB) {
2322 ret = cma_resolve_ib_addr(id_priv);
2323 } else {
2324 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2325 dst_addr, &id->route.addr.dev_addr,
2326 timeout_ms, addr_handler, id_priv);
2327 }
2328 }
Sean Heftye51060f2006-06-17 20:37:29 -07002329 if (ret)
2330 goto err;
2331
2332 return 0;
2333err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002334 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002335 cma_deref_id(id_priv);
2336 return ret;
2337}
2338EXPORT_SYMBOL(rdma_resolve_addr);
2339
Hefty, Seana9bb7912011-05-09 22:06:10 -07002340int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2341{
2342 struct rdma_id_private *id_priv;
2343 unsigned long flags;
2344 int ret;
2345
2346 id_priv = container_of(id, struct rdma_id_private, id);
2347 spin_lock_irqsave(&id_priv->lock, flags);
Sean Heftyc8dea2f92013-05-29 10:09:08 -07002348 if (reuse || id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002349 id_priv->reuseaddr = reuse;
2350 ret = 0;
2351 } else {
2352 ret = -EINVAL;
2353 }
2354 spin_unlock_irqrestore(&id_priv->lock, flags);
2355 return ret;
2356}
2357EXPORT_SYMBOL(rdma_set_reuseaddr);
2358
Sean Hefty68602122012-06-14 20:31:39 +00002359int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2360{
2361 struct rdma_id_private *id_priv;
2362 unsigned long flags;
2363 int ret;
2364
2365 id_priv = container_of(id, struct rdma_id_private, id);
2366 spin_lock_irqsave(&id_priv->lock, flags);
2367 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2368 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2369 id_priv->afonly = afonly;
2370 ret = 0;
2371 } else {
2372 ret = -EINVAL;
2373 }
2374 spin_unlock_irqrestore(&id_priv->lock, flags);
2375 return ret;
2376}
2377EXPORT_SYMBOL(rdma_set_afonly);
2378
Sean Heftye51060f2006-06-17 20:37:29 -07002379static void cma_bind_port(struct rdma_bind_list *bind_list,
2380 struct rdma_id_private *id_priv)
2381{
Sean Hefty58afdcb2013-05-29 10:09:11 -07002382 struct sockaddr *addr;
2383 struct sockaddr_ib *sib;
2384 u64 sid, mask;
2385 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07002386
Sean Heftyf4753832013-05-29 10:09:14 -07002387 addr = cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002388 port = htons(bind_list->port);
2389
2390 switch (addr->sa_family) {
2391 case AF_INET:
2392 ((struct sockaddr_in *) addr)->sin_port = port;
2393 break;
2394 case AF_INET6:
2395 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2396 break;
2397 case AF_IB:
2398 sib = (struct sockaddr_ib *) addr;
2399 sid = be64_to_cpu(sib->sib_sid);
2400 mask = be64_to_cpu(sib->sib_sid_mask);
2401 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2402 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2403 break;
2404 }
Sean Heftye51060f2006-06-17 20:37:29 -07002405 id_priv->bind_list = bind_list;
2406 hlist_add_head(&id_priv->node, &bind_list->owners);
2407}
2408
Haggai Eranaac978e2015-07-30 17:50:20 +03002409static int cma_alloc_port(enum rdma_port_space ps,
2410 struct rdma_id_private *id_priv, unsigned short snum)
Sean Heftye51060f2006-06-17 20:37:29 -07002411{
2412 struct rdma_bind_list *bind_list;
Tejun Heo3b069c52013-02-27 17:04:16 -08002413 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002414
Sean Heftycb164b82007-03-05 12:50:17 -08002415 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002416 if (!bind_list)
2417 return -ENOMEM;
2418
Haggai Eranaac978e2015-07-30 17:50:20 +03002419 ret = cma_ps_alloc(ps, bind_list, snum);
Tejun Heo3b069c52013-02-27 17:04:16 -08002420 if (ret < 0)
2421 goto err;
Sean Heftye51060f2006-06-17 20:37:29 -07002422
2423 bind_list->ps = ps;
Tejun Heo3b069c52013-02-27 17:04:16 -08002424 bind_list->port = (unsigned short)ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002425 cma_bind_port(bind_list, id_priv);
2426 return 0;
Tejun Heo3b069c52013-02-27 17:04:16 -08002427err:
Sean Heftyaedec082007-01-29 16:41:23 -08002428 kfree(bind_list);
Tejun Heo3b069c52013-02-27 17:04:16 -08002429 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002430}
2431
Haggai Eranaac978e2015-07-30 17:50:20 +03002432static int cma_alloc_any_port(enum rdma_port_space ps,
2433 struct rdma_id_private *id_priv)
Sean Heftyaedec082007-01-29 16:41:23 -08002434{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002435 static unsigned int last_used_port;
2436 int low, high, remaining;
2437 unsigned int rover;
Sean Heftyaedec082007-01-29 16:41:23 -08002438
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07002439 inet_get_local_port_range(&init_net, &low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002440 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002441 rover = prandom_u32() % remaining + low;
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002442retry:
2443 if (last_used_port != rover &&
Haggai Eranaac978e2015-07-30 17:50:20 +03002444 !cma_ps_find(ps, (unsigned short)rover)) {
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002445 int ret = cma_alloc_port(ps, id_priv, rover);
2446 /*
2447 * Remember previously used port number in order to avoid
2448 * re-using same port immediately after it is closed.
2449 */
2450 if (!ret)
2451 last_used_port = rover;
2452 if (ret != -EADDRNOTAVAIL)
2453 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002454 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002455 if (--remaining) {
2456 rover++;
2457 if ((rover < low) || (rover > high))
2458 rover = low;
2459 goto retry;
2460 }
2461 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002462}
2463
Hefty, Seana9bb7912011-05-09 22:06:10 -07002464/*
2465 * Check that the requested port is available. This is called when trying to
2466 * bind to a specific port, or when trying to listen on a bound port. In
2467 * the latter case, the provided id_priv may already be on the bind_list, but
2468 * we still need to check that it's okay to start listening.
2469 */
2470static int cma_check_port(struct rdma_bind_list *bind_list,
2471 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002472{
2473 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002474 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002475
Sean Heftyf4753832013-05-29 10:09:14 -07002476 addr = cma_src_addr(id_priv);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002477 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002478 if (id_priv == cur_id)
2479 continue;
2480
Sean Hefty5b0ec992012-06-14 20:31:39 +00002481 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2482 cur_id->reuseaddr)
2483 continue;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002484
Sean Heftyf4753832013-05-29 10:09:14 -07002485 cur_addr = cma_src_addr(cur_id);
Sean Hefty5b0ec992012-06-14 20:31:39 +00002486 if (id_priv->afonly && cur_id->afonly &&
2487 (addr->sa_family != cur_addr->sa_family))
2488 continue;
2489
2490 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2491 return -EADDRNOTAVAIL;
2492
2493 if (!cma_addr_cmp(addr, cur_addr))
2494 return -EADDRINUSE;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002495 }
2496 return 0;
2497}
2498
Haggai Eranaac978e2015-07-30 17:50:20 +03002499static int cma_use_port(enum rdma_port_space ps,
2500 struct rdma_id_private *id_priv)
Hefty, Seana9bb7912011-05-09 22:06:10 -07002501{
2502 struct rdma_bind_list *bind_list;
2503 unsigned short snum;
2504 int ret;
2505
Sean Heftyf4753832013-05-29 10:09:14 -07002506 snum = ntohs(cma_port(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002507 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2508 return -EACCES;
2509
Haggai Eranaac978e2015-07-30 17:50:20 +03002510 bind_list = cma_ps_find(ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002511 if (!bind_list) {
2512 ret = cma_alloc_port(ps, id_priv, snum);
2513 } else {
2514 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2515 if (!ret)
2516 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002517 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07002518 return ret;
2519}
Sean Heftye51060f2006-06-17 20:37:29 -07002520
Hefty, Seana9bb7912011-05-09 22:06:10 -07002521static int cma_bind_listen(struct rdma_id_private *id_priv)
2522{
2523 struct rdma_bind_list *bind_list = id_priv->bind_list;
2524 int ret = 0;
2525
2526 mutex_lock(&lock);
2527 if (bind_list->owners.first->next)
2528 ret = cma_check_port(bind_list, id_priv, 0);
2529 mutex_unlock(&lock);
2530 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002531}
2532
Haggai Eranaac978e2015-07-30 17:50:20 +03002533static enum rdma_port_space cma_select_inet_ps(
2534 struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002535{
2536 switch (id_priv->id.ps) {
Sean Hefty58afdcb2013-05-29 10:09:11 -07002537 case RDMA_PS_TCP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002538 case RDMA_PS_UDP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002539 case RDMA_PS_IPOIB:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002540 case RDMA_PS_IB:
Haggai Eranaac978e2015-07-30 17:50:20 +03002541 return id_priv->id.ps;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002542 default:
Haggai Eranaac978e2015-07-30 17:50:20 +03002543
2544 return 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002545 }
2546}
2547
Haggai Eranaac978e2015-07-30 17:50:20 +03002548static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002549{
Haggai Eranaac978e2015-07-30 17:50:20 +03002550 enum rdma_port_space ps = 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002551 struct sockaddr_ib *sib;
2552 u64 sid_ps, mask, sid;
2553
Sean Heftyf4753832013-05-29 10:09:14 -07002554 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002555 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
2556 sid = be64_to_cpu(sib->sib_sid) & mask;
2557
2558 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
2559 sid_ps = RDMA_IB_IP_PS_IB;
Haggai Eranaac978e2015-07-30 17:50:20 +03002560 ps = RDMA_PS_IB;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002561 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
2562 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
2563 sid_ps = RDMA_IB_IP_PS_TCP;
Haggai Eranaac978e2015-07-30 17:50:20 +03002564 ps = RDMA_PS_TCP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002565 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
2566 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
2567 sid_ps = RDMA_IB_IP_PS_UDP;
Haggai Eranaac978e2015-07-30 17:50:20 +03002568 ps = RDMA_PS_UDP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002569 }
2570
2571 if (ps) {
2572 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
2573 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
2574 be64_to_cpu(sib->sib_sid_mask));
2575 }
2576 return ps;
2577}
2578
Sean Heftye51060f2006-06-17 20:37:29 -07002579static int cma_get_port(struct rdma_id_private *id_priv)
2580{
Haggai Eranaac978e2015-07-30 17:50:20 +03002581 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -07002582 int ret;
2583
Sean Heftyf4753832013-05-29 10:09:14 -07002584 if (cma_family(id_priv) != AF_IB)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002585 ps = cma_select_inet_ps(id_priv);
2586 else
2587 ps = cma_select_ib_ps(id_priv);
2588 if (!ps)
Sean Heftye51060f2006-06-17 20:37:29 -07002589 return -EPROTONOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07002590
2591 mutex_lock(&lock);
Sean Heftyf4753832013-05-29 10:09:14 -07002592 if (cma_any_port(cma_src_addr(id_priv)))
Sean Heftyaedec082007-01-29 16:41:23 -08002593 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002594 else
2595 ret = cma_use_port(ps, id_priv);
2596 mutex_unlock(&lock);
2597
2598 return ret;
2599}
2600
Sean Heftyd14714d2009-11-19 16:46:25 -08002601static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2602 struct sockaddr *addr)
2603{
Roland Dreierd90f9b32012-07-05 22:39:34 -07002604#if IS_ENABLED(CONFIG_IPV6)
Sean Heftyd14714d2009-11-19 16:46:25 -08002605 struct sockaddr_in6 *sin6;
2606
2607 if (addr->sa_family != AF_INET6)
2608 return 0;
2609
2610 sin6 = (struct sockaddr_in6 *) addr;
Somnath Kotur5462edd2013-11-16 09:30:01 +05302611
2612 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
2613 return 0;
2614
2615 if (!sin6->sin6_scope_id)
Sean Heftyd14714d2009-11-19 16:46:25 -08002616 return -EINVAL;
2617
2618 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2619#endif
2620 return 0;
2621}
2622
Hefty, Seana9bb7912011-05-09 22:06:10 -07002623int rdma_listen(struct rdma_cm_id *id, int backlog)
2624{
2625 struct rdma_id_private *id_priv;
2626 int ret;
2627
2628 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002629 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftyf4753832013-05-29 10:09:14 -07002630 id->route.addr.src_addr.ss_family = AF_INET;
2631 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
Hefty, Seana9bb7912011-05-09 22:06:10 -07002632 if (ret)
2633 return ret;
2634 }
2635
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002636 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07002637 return -EINVAL;
2638
2639 if (id_priv->reuseaddr) {
2640 ret = cma_bind_listen(id_priv);
2641 if (ret)
2642 goto err;
2643 }
2644
2645 id_priv->backlog = backlog;
2646 if (id->device) {
Michael Wang72219cea2015-05-05 14:50:34 +02002647 if (rdma_cap_ib_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002648 ret = cma_ib_listen(id_priv);
2649 if (ret)
2650 goto err;
Michael Wang04215332015-05-05 14:50:35 +02002651 } else if (rdma_cap_iw_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002652 ret = cma_iw_listen(id_priv, backlog);
2653 if (ret)
2654 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02002655 } else {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002656 ret = -ENOSYS;
2657 goto err;
2658 }
2659 } else
2660 cma_listen_on_all(id_priv);
2661
2662 return 0;
2663err:
2664 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002665 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002666 return ret;
2667}
2668EXPORT_SYMBOL(rdma_listen);
2669
Sean Heftye51060f2006-06-17 20:37:29 -07002670int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2671{
2672 struct rdma_id_private *id_priv;
2673 int ret;
2674
Sean Hefty680f9202013-05-29 10:09:12 -07002675 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
2676 addr->sa_family != AF_IB)
Sean Heftye51060f2006-06-17 20:37:29 -07002677 return -EAFNOSUPPORT;
2678
2679 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002680 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002681 return -EINVAL;
2682
Sean Heftyd14714d2009-11-19 16:46:25 -08002683 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2684 if (ret)
2685 goto err1;
2686
Moni Shoua7b856272013-12-12 18:03:12 +02002687 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
Sean Hefty8523c042010-02-08 16:41:15 -08002688 if (!cma_any_addr(addr)) {
Sean Hefty680f9202013-05-29 10:09:12 -07002689 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002690 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002691 goto err1;
2692
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002693 ret = cma_acquire_dev(id_priv, NULL);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002694 if (ret)
2695 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002696 }
2697
Sean Hefty68602122012-06-14 20:31:39 +00002698 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
2699 if (addr->sa_family == AF_INET)
2700 id_priv->afonly = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002701#if IS_ENABLED(CONFIG_IPV6)
Sean Hefty68602122012-06-14 20:31:39 +00002702 else if (addr->sa_family == AF_INET6)
2703 id_priv->afonly = init_net.ipv6.sysctl.bindv6only;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002704#endif
Sean Hefty68602122012-06-14 20:31:39 +00002705 }
Sean Heftye51060f2006-06-17 20:37:29 -07002706 ret = cma_get_port(id_priv);
2707 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002708 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002709
2710 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07002711err2:
Sean Heftya396d432011-02-23 09:05:39 -08002712 if (id_priv->cma_dev)
2713 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002714err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002715 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07002716 return ret;
2717}
2718EXPORT_SYMBOL(rdma_bind_addr);
2719
Sean Heftyf4753832013-05-29 10:09:14 -07002720static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07002721{
Sean Heftye51060f2006-06-17 20:37:29 -07002722 struct cma_hdr *cma_hdr;
Sean Heftye51060f2006-06-17 20:37:29 -07002723
Sean Hefty01602f12013-05-29 10:09:20 -07002724 cma_hdr = hdr;
2725 cma_hdr->cma_version = CMA_VERSION;
Sean Heftyf4753832013-05-29 10:09:14 -07002726 if (cma_family(id_priv) == AF_INET) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002727 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07002728
Sean Heftyf4753832013-05-29 10:09:14 -07002729 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
2730 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002731
Sean Hefty01602f12013-05-29 10:09:20 -07002732 cma_set_ip_ver(cma_hdr, 4);
2733 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2734 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2735 cma_hdr->port = src4->sin_port;
Sean Heftye8160e12013-05-29 10:09:22 -07002736 } else if (cma_family(id_priv) == AF_INET6) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002737 struct sockaddr_in6 *src6, *dst6;
2738
Sean Heftyf4753832013-05-29 10:09:14 -07002739 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2740 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002741
Sean Hefty01602f12013-05-29 10:09:20 -07002742 cma_set_ip_ver(cma_hdr, 6);
2743 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2744 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2745 cma_hdr->port = src6->sin6_port;
Sean Heftye51060f2006-06-17 20:37:29 -07002746 }
2747 return 0;
2748}
2749
Sean Hefty628e5f62006-11-30 16:44:16 -08002750static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2751 struct ib_cm_event *ib_event)
2752{
2753 struct rdma_id_private *id_priv = cm_id->context;
2754 struct rdma_cm_event event;
2755 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2756 int ret = 0;
2757
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002758 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07002759 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08002760
Sean Hefty8aa08602007-05-07 11:49:00 -07002761 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08002762 switch (ib_event->event) {
2763 case IB_CM_SIDR_REQ_ERROR:
2764 event.event = RDMA_CM_EVENT_UNREACHABLE;
2765 event.status = -ETIMEDOUT;
2766 break;
2767 case IB_CM_SIDR_REP_RECEIVED:
2768 event.param.ud.private_data = ib_event->private_data;
2769 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2770 if (rep->status != IB_SIDR_SUCCESS) {
2771 event.event = RDMA_CM_EVENT_UNREACHABLE;
2772 event.status = ib_event->param.sidr_rep_rcvd.status;
2773 break;
2774 }
Sean Hefty5c438132013-05-29 10:09:23 -07002775 ret = cma_set_qkey(id_priv, rep->qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002776 if (ret) {
2777 event.event = RDMA_CM_EVENT_ADDR_ERROR;
Sean Hefty5c438132013-05-29 10:09:23 -07002778 event.status = ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002779 break;
2780 }
2781 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2782 id_priv->id.route.path_rec,
2783 &event.param.ud.ah_attr);
2784 event.param.ud.qp_num = rep->qpn;
2785 event.param.ud.qkey = rep->qkey;
2786 event.event = RDMA_CM_EVENT_ESTABLISHED;
2787 event.status = 0;
2788 break;
2789 default:
Roland Dreier468f2232008-07-14 23:48:47 -07002790 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08002791 ib_event->event);
2792 goto out;
2793 }
2794
2795 ret = id_priv->id.event_handler(&id_priv->id, &event);
2796 if (ret) {
2797 /* Destroy the CM ID by returning a non-zero value. */
2798 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002799 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002800 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002801 rdma_destroy_id(&id_priv->id);
2802 return ret;
2803 }
2804out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002805 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002806 return ret;
2807}
2808
2809static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2810 struct rdma_conn_param *conn_param)
2811{
2812 struct ib_cm_sidr_req_param req;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002813 struct ib_cm_id *id;
Sean Heftye511d1a2013-07-24 15:06:08 -07002814 void *private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07002815 int offset, ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002816
Sean Heftye511d1a2013-07-24 15:06:08 -07002817 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07002818 offset = cma_user_data_offset(id_priv);
2819 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00002820 if (req.private_data_len < conn_param->private_data_len)
2821 return -EINVAL;
2822
Sean Heftye8160e12013-05-29 10:09:22 -07002823 if (req.private_data_len) {
Sean Heftye511d1a2013-07-24 15:06:08 -07002824 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2825 if (!private_data)
Sean Heftye8160e12013-05-29 10:09:22 -07002826 return -ENOMEM;
2827 } else {
Sean Heftye511d1a2013-07-24 15:06:08 -07002828 private_data = NULL;
Sean Heftye8160e12013-05-29 10:09:22 -07002829 }
Sean Hefty628e5f62006-11-30 16:44:16 -08002830
2831 if (conn_param->private_data && conn_param->private_data_len)
Sean Heftye511d1a2013-07-24 15:06:08 -07002832 memcpy(private_data + offset, conn_param->private_data,
2833 conn_param->private_data_len);
Sean Hefty628e5f62006-11-30 16:44:16 -08002834
Sean Heftye511d1a2013-07-24 15:06:08 -07002835 if (private_data) {
2836 ret = cma_format_hdr(private_data, id_priv);
Sean Heftye8160e12013-05-29 10:09:22 -07002837 if (ret)
2838 goto out;
Sean Heftye511d1a2013-07-24 15:06:08 -07002839 req.private_data = private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07002840 }
Sean Hefty628e5f62006-11-30 16:44:16 -08002841
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002842 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
2843 id_priv);
2844 if (IS_ERR(id)) {
2845 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08002846 goto out;
2847 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002848 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08002849
Sean Heftyf4753832013-05-29 10:09:14 -07002850 req.path = id_priv->id.route.path_rec;
Sean Heftycf539362013-05-29 10:09:28 -07002851 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Hefty628e5f62006-11-30 16:44:16 -08002852 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2853 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2854
2855 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2856 if (ret) {
2857 ib_destroy_cm_id(id_priv->cm_id.ib);
2858 id_priv->cm_id.ib = NULL;
2859 }
2860out:
Sean Heftye511d1a2013-07-24 15:06:08 -07002861 kfree(private_data);
Sean Hefty628e5f62006-11-30 16:44:16 -08002862 return ret;
2863}
2864
Sean Heftye51060f2006-06-17 20:37:29 -07002865static int cma_connect_ib(struct rdma_id_private *id_priv,
2866 struct rdma_conn_param *conn_param)
2867{
2868 struct ib_cm_req_param req;
2869 struct rdma_route *route;
2870 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002871 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07002872 int offset, ret;
2873
2874 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07002875 offset = cma_user_data_offset(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002876 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00002877 if (req.private_data_len < conn_param->private_data_len)
2878 return -EINVAL;
2879
Sean Heftye8160e12013-05-29 10:09:22 -07002880 if (req.private_data_len) {
2881 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2882 if (!private_data)
2883 return -ENOMEM;
2884 } else {
2885 private_data = NULL;
2886 }
Sean Heftye51060f2006-06-17 20:37:29 -07002887
2888 if (conn_param->private_data && conn_param->private_data_len)
2889 memcpy(private_data + offset, conn_param->private_data,
2890 conn_param->private_data_len);
2891
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002892 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
2893 if (IS_ERR(id)) {
2894 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07002895 goto out;
2896 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002897 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07002898
2899 route = &id_priv->id.route;
Sean Heftye8160e12013-05-29 10:09:22 -07002900 if (private_data) {
2901 ret = cma_format_hdr(private_data, id_priv);
2902 if (ret)
2903 goto out;
2904 req.private_data = private_data;
2905 }
Sean Heftye51060f2006-06-17 20:37:29 -07002906
2907 req.primary_path = &route->path_rec[0];
2908 if (route->num_paths == 2)
2909 req.alternate_path = &route->path_rec[1];
2910
Sean Heftycf539362013-05-29 10:09:28 -07002911 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002912 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07002913 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07002914 req.starting_psn = id_priv->seq_num;
2915 req.responder_resources = conn_param->responder_resources;
2916 req.initiator_depth = conn_param->initiator_depth;
2917 req.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00002918 req.retry_count = min_t(u8, 7, conn_param->retry_count);
2919 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07002920 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2921 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2922 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2923 req.srq = id_priv->srq ? 1 : 0;
2924
2925 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2926out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002927 if (ret && !IS_ERR(id)) {
2928 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07002929 id_priv->cm_id.ib = NULL;
2930 }
2931
Sean Heftye51060f2006-06-17 20:37:29 -07002932 kfree(private_data);
2933 return ret;
2934}
2935
Tom Tucker07ebafb2006-08-03 16:02:42 -05002936static int cma_connect_iw(struct rdma_id_private *id_priv,
2937 struct rdma_conn_param *conn_param)
2938{
2939 struct iw_cm_id *cm_id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002940 int ret;
2941 struct iw_cm_conn_param iw_param;
2942
2943 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002944 if (IS_ERR(cm_id))
2945 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002946
Steve Wise68cdba02015-05-18 15:27:10 -05002947 cm_id->tos = id_priv->tos;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002948 id_priv->cm_id.iw = cm_id;
2949
Steve Wise24d44a32013-07-04 16:10:44 +05302950 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
2951 rdma_addr_size(cma_src_addr(id_priv)));
2952 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
2953 rdma_addr_size(cma_dst_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002954
Sean Hefty5851bb82008-01-04 10:47:12 -08002955 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07002956 if (ret)
2957 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002958
Hefty, Seanf45ee802011-10-06 09:33:04 -07002959 if (conn_param) {
2960 iw_param.ord = conn_param->initiator_depth;
2961 iw_param.ird = conn_param->responder_resources;
2962 iw_param.private_data = conn_param->private_data;
2963 iw_param.private_data_len = conn_param->private_data_len;
2964 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
2965 } else {
2966 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002967 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07002968 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002969 ret = iw_cm_connect(cm_id, &iw_param);
2970out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002971 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07002972 iw_destroy_cm_id(cm_id);
2973 id_priv->cm_id.iw = NULL;
2974 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002975 return ret;
2976}
2977
Sean Heftye51060f2006-06-17 20:37:29 -07002978int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2979{
2980 struct rdma_id_private *id_priv;
2981 int ret;
2982
2983 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002984 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07002985 return -EINVAL;
2986
2987 if (!id->qp) {
2988 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002989 id_priv->srq = conn_param->srq;
2990 }
2991
Michael Wang72219cea2015-05-05 14:50:34 +02002992 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00002993 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08002994 ret = cma_resolve_ib_udp(id_priv, conn_param);
2995 else
2996 ret = cma_connect_ib(id_priv, conn_param);
Michael Wang04215332015-05-05 14:50:35 +02002997 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002998 ret = cma_connect_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02002999 else
Sean Heftye51060f2006-06-17 20:37:29 -07003000 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003001 if (ret)
3002 goto err;
3003
3004 return 0;
3005err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003006 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07003007 return ret;
3008}
3009EXPORT_SYMBOL(rdma_connect);
3010
3011static int cma_accept_ib(struct rdma_id_private *id_priv,
3012 struct rdma_conn_param *conn_param)
3013{
3014 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08003015 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003016
Sean Hefty5851bb82008-01-04 10:47:12 -08003017 ret = cma_modify_qp_rtr(id_priv, conn_param);
3018 if (ret)
3019 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08003020
Sean Hefty5851bb82008-01-04 10:47:12 -08003021 ret = cma_modify_qp_rts(id_priv, conn_param);
3022 if (ret)
3023 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003024
3025 memset(&rep, 0, sizeof rep);
3026 rep.qp_num = id_priv->qp_num;
3027 rep.starting_psn = id_priv->seq_num;
3028 rep.private_data = conn_param->private_data;
3029 rep.private_data_len = conn_param->private_data_len;
3030 rep.responder_resources = conn_param->responder_resources;
3031 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07003032 rep.failover_accepted = 0;
3033 rep.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003034 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003035 rep.srq = id_priv->srq ? 1 : 0;
3036
Sean Hefty0fe313b2006-11-30 16:37:15 -08003037 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3038out:
3039 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003040}
3041
Tom Tucker07ebafb2006-08-03 16:02:42 -05003042static int cma_accept_iw(struct rdma_id_private *id_priv,
3043 struct rdma_conn_param *conn_param)
3044{
3045 struct iw_cm_conn_param iw_param;
3046 int ret;
3047
Sean Hefty5851bb82008-01-04 10:47:12 -08003048 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003049 if (ret)
3050 return ret;
3051
3052 iw_param.ord = conn_param->initiator_depth;
3053 iw_param.ird = conn_param->responder_resources;
3054 iw_param.private_data = conn_param->private_data;
3055 iw_param.private_data_len = conn_param->private_data_len;
3056 if (id_priv->id.qp) {
3057 iw_param.qpn = id_priv->qp_num;
3058 } else
3059 iw_param.qpn = conn_param->qp_num;
3060
3061 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3062}
3063
Sean Hefty628e5f62006-11-30 16:44:16 -08003064static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
Sean Hefty5c438132013-05-29 10:09:23 -07003065 enum ib_cm_sidr_status status, u32 qkey,
Sean Hefty628e5f62006-11-30 16:44:16 -08003066 const void *private_data, int private_data_len)
3067{
3068 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003069 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003070
3071 memset(&rep, 0, sizeof rep);
3072 rep.status = status;
3073 if (status == IB_SIDR_SUCCESS) {
Sean Hefty5c438132013-05-29 10:09:23 -07003074 ret = cma_set_qkey(id_priv, qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003075 if (ret)
3076 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003077 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003078 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08003079 }
3080 rep.private_data = private_data;
3081 rep.private_data_len = private_data_len;
3082
3083 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3084}
3085
Sean Heftye51060f2006-06-17 20:37:29 -07003086int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3087{
3088 struct rdma_id_private *id_priv;
3089 int ret;
3090
3091 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00003092
3093 id_priv->owner = task_pid_nr(current);
3094
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003095 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003096 return -EINVAL;
3097
3098 if (!id->qp && conn_param) {
3099 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003100 id_priv->srq = conn_param->srq;
3101 }
3102
Michael Wang72219cea2015-05-05 14:50:34 +02003103 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Hefty, Seanf45ee802011-10-06 09:33:04 -07003104 if (id->qp_type == IB_QPT_UD) {
3105 if (conn_param)
3106 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003107 conn_param->qkey,
Hefty, Seanf45ee802011-10-06 09:33:04 -07003108 conn_param->private_data,
3109 conn_param->private_data_len);
3110 else
3111 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003112 0, NULL, 0);
Hefty, Seanf45ee802011-10-06 09:33:04 -07003113 } else {
3114 if (conn_param)
3115 ret = cma_accept_ib(id_priv, conn_param);
3116 else
3117 ret = cma_rep_recv(id_priv);
3118 }
Michael Wang04215332015-05-05 14:50:35 +02003119 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003120 ret = cma_accept_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003121 else
Sean Heftye51060f2006-06-17 20:37:29 -07003122 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003123
3124 if (ret)
3125 goto reject;
3126
3127 return 0;
3128reject:
Sean Heftyc5483382007-09-24 13:19:09 -07003129 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003130 rdma_reject(id, NULL, 0);
3131 return ret;
3132}
3133EXPORT_SYMBOL(rdma_accept);
3134
Sean Hefty0fe313b2006-11-30 16:37:15 -08003135int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3136{
3137 struct rdma_id_private *id_priv;
3138 int ret;
3139
3140 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003141 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08003142 return -EINVAL;
3143
3144 switch (id->device->node_type) {
3145 case RDMA_NODE_IB_CA:
3146 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3147 break;
3148 default:
3149 ret = 0;
3150 break;
3151 }
3152 return ret;
3153}
3154EXPORT_SYMBOL(rdma_notify);
3155
Sean Heftye51060f2006-06-17 20:37:29 -07003156int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3157 u8 private_data_len)
3158{
3159 struct rdma_id_private *id_priv;
3160 int ret;
3161
3162 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003163 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003164 return -EINVAL;
3165
Michael Wang72219cea2015-05-05 14:50:34 +02003166 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003167 if (id->qp_type == IB_QPT_UD)
Sean Hefty5c438132013-05-29 10:09:23 -07003168 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
Sean Hefty628e5f62006-11-30 16:44:16 -08003169 private_data, private_data_len);
3170 else
3171 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3172 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3173 0, private_data, private_data_len);
Michael Wang04215332015-05-05 14:50:35 +02003174 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003175 ret = iw_cm_reject(id_priv->cm_id.iw,
3176 private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003177 } else
Sean Heftye51060f2006-06-17 20:37:29 -07003178 ret = -ENOSYS;
Michael Wang21655af2015-05-05 14:50:27 +02003179
Sean Heftye51060f2006-06-17 20:37:29 -07003180 return ret;
3181}
3182EXPORT_SYMBOL(rdma_reject);
3183
3184int rdma_disconnect(struct rdma_cm_id *id)
3185{
3186 struct rdma_id_private *id_priv;
3187 int ret;
3188
3189 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003190 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003191 return -EINVAL;
3192
Michael Wang72219cea2015-05-05 14:50:34 +02003193 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyc5483382007-09-24 13:19:09 -07003194 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003195 if (ret)
3196 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003197 /* Initiate or respond to a disconnect. */
3198 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3199 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
Michael Wang04215332015-05-05 14:50:35 +02003200 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003201 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003202 } else
Tom Tucker07ebafb2006-08-03 16:02:42 -05003203 ret = -EINVAL;
Michael Wang21655af2015-05-05 14:50:27 +02003204
Sean Heftye51060f2006-06-17 20:37:29 -07003205out:
3206 return ret;
3207}
3208EXPORT_SYMBOL(rdma_disconnect);
3209
Sean Heftyc8f6a362007-02-15 17:00:18 -08003210static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3211{
3212 struct rdma_id_private *id_priv;
3213 struct cma_multicast *mc = multicast->context;
3214 struct rdma_cm_event event;
3215 int ret;
3216
3217 id_priv = mc->id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003218 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
3219 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07003220 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003221
Sean Hefty5c438132013-05-29 10:09:23 -07003222 if (!status)
3223 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
Sean Heftyc5483382007-09-24 13:19:09 -07003224 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003225 if (!status && id_priv->id.qp)
3226 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003227 be16_to_cpu(multicast->rec.mlid));
Sean Heftyc5483382007-09-24 13:19:09 -07003228 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003229
3230 memset(&event, 0, sizeof event);
3231 event.status = status;
3232 event.param.ud.private_data = mc->context;
3233 if (!status) {
3234 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3235 ib_init_ah_from_mcmember(id_priv->id.device,
3236 id_priv->id.port_num, &multicast->rec,
3237 &event.param.ud.ah_attr);
3238 event.param.ud.qp_num = 0xFFFFFF;
3239 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3240 } else
3241 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3242
3243 ret = id_priv->id.event_handler(&id_priv->id, &event);
3244 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003245 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003246 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003247 rdma_destroy_id(&id_priv->id);
3248 return 0;
3249 }
Sean Hefty8aa08602007-05-07 11:49:00 -07003250
Or Gerlitzde910bd2008-07-14 23:48:53 -07003251 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003252 return 0;
3253}
3254
3255static void cma_set_mgid(struct rdma_id_private *id_priv,
3256 struct sockaddr *addr, union ib_gid *mgid)
3257{
3258 unsigned char mc_map[MAX_ADDR_LEN];
3259 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3260 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3261 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3262
3263 if (cma_any_addr(addr)) {
3264 memset(mgid, 0, sizeof *mgid);
3265 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08003266 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08003267 0xFF10A01B)) {
3268 /* IPv6 address is an SA assigned MGID. */
3269 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003270 } else if (addr->sa_family == AF_IB) {
3271 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08003272 } else if ((addr->sa_family == AF_INET6)) {
3273 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3274 if (id_priv->id.ps == RDMA_PS_UDP)
3275 mc_map[7] = 0x01; /* Use RDMA CM signature */
3276 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003277 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07003278 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003279 if (id_priv->id.ps == RDMA_PS_UDP)
3280 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08003281 *mgid = *(union ib_gid *) (mc_map + 4);
3282 }
3283}
3284
3285static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3286 struct cma_multicast *mc)
3287{
3288 struct ib_sa_mcmember_rec rec;
3289 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3290 ib_sa_comp_mask comp_mask;
3291 int ret;
3292
3293 ib_addr_get_mgid(dev_addr, &rec.mgid);
3294 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3295 &rec.mgid, &rec);
3296 if (ret)
3297 return ret;
3298
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003299 ret = cma_set_qkey(id_priv, 0);
3300 if (ret)
3301 return ret;
3302
Roland Dreier3f446752008-08-04 11:02:14 -07003303 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003304 rec.qkey = cpu_to_be32(id_priv->qkey);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003305 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003306 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3307 rec.join_state = 1;
3308
3309 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3310 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3311 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3312 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3313 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3314
Yossi Etigin84adeee2009-04-01 13:55:32 -07003315 if (id_priv->id.ps == RDMA_PS_IPOIB)
3316 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
Dotan Barak2a22fb82012-08-30 09:09:55 +00003317 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3318 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3319 IB_SA_MCMEMBER_REC_MTU |
3320 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Yossi Etigin84adeee2009-04-01 13:55:32 -07003321
Sean Heftyc8f6a362007-02-15 17:00:18 -08003322 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3323 id_priv->id.port_num, &rec,
3324 comp_mask, GFP_KERNEL,
3325 cma_ib_mc_handler, mc);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303326 return PTR_ERR_OR_ZERO(mc->multicast.ib);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003327}
3328
Eli Cohen3c86aa72010-10-13 21:26:51 +02003329static void iboe_mcast_work_handler(struct work_struct *work)
3330{
3331 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3332 struct cma_multicast *mc = mw->mc;
3333 struct ib_sa_multicast *m = mc->multicast.ib;
3334
3335 mc->multicast.ib->context = mc;
3336 cma_ib_mc_handler(0, m);
3337 kref_put(&mc->mcref, release_mc);
3338 kfree(mw);
3339}
3340
3341static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3342{
3343 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3344 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3345
3346 if (cma_any_addr(addr)) {
3347 memset(mgid, 0, sizeof *mgid);
3348 } else if (addr->sa_family == AF_INET6) {
3349 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3350 } else {
3351 mgid->raw[0] = 0xff;
3352 mgid->raw[1] = 0x0e;
3353 mgid->raw[2] = 0;
3354 mgid->raw[3] = 0;
3355 mgid->raw[4] = 0;
3356 mgid->raw[5] = 0;
3357 mgid->raw[6] = 0;
3358 mgid->raw[7] = 0;
3359 mgid->raw[8] = 0;
3360 mgid->raw[9] = 0;
3361 mgid->raw[10] = 0xff;
3362 mgid->raw[11] = 0xff;
3363 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3364 }
3365}
3366
3367static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3368 struct cma_multicast *mc)
3369{
3370 struct iboe_mcast_work *work;
3371 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3372 int err;
3373 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3374 struct net_device *ndev = NULL;
3375
3376 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3377 return -EINVAL;
3378
3379 work = kzalloc(sizeof *work, GFP_KERNEL);
3380 if (!work)
3381 return -ENOMEM;
3382
3383 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3384 if (!mc->multicast.ib) {
3385 err = -ENOMEM;
3386 goto out1;
3387 }
3388
3389 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3390
3391 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3392 if (id_priv->id.ps == RDMA_PS_UDP)
3393 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3394
3395 if (dev_addr->bound_dev_if)
3396 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3397 if (!ndev) {
3398 err = -ENODEV;
3399 goto out2;
3400 }
3401 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3402 mc->multicast.ib->rec.hop_limit = 1;
3403 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3404 dev_put(ndev);
3405 if (!mc->multicast.ib->rec.mtu) {
3406 err = -EINVAL;
3407 goto out2;
3408 }
Moni Shoua7b856272013-12-12 18:03:12 +02003409 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3410 &mc->multicast.ib->rec.port_gid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003411 work->id = id_priv;
3412 work->mc = mc;
3413 INIT_WORK(&work->work, iboe_mcast_work_handler);
3414 kref_get(&mc->mcref);
3415 queue_work(cma_wq, &work->work);
3416
3417 return 0;
3418
3419out2:
3420 kfree(mc->multicast.ib);
3421out1:
3422 kfree(work);
3423 return err;
3424}
3425
Sean Heftyc8f6a362007-02-15 17:00:18 -08003426int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3427 void *context)
3428{
3429 struct rdma_id_private *id_priv;
3430 struct cma_multicast *mc;
3431 int ret;
3432
3433 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003434 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3435 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08003436 return -EINVAL;
3437
3438 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3439 if (!mc)
3440 return -ENOMEM;
3441
Sean Heftyef560862013-05-29 10:09:10 -07003442 memcpy(&mc->addr, addr, rdma_addr_size(addr));
Sean Heftyc8f6a362007-02-15 17:00:18 -08003443 mc->context = context;
3444 mc->id_priv = id_priv;
3445
3446 spin_lock(&id_priv->lock);
3447 list_add(&mc->list, &id_priv->mc_list);
3448 spin_unlock(&id_priv->lock);
3449
Ira Weiny5d9fb042015-05-14 15:01:46 -04003450 if (rdma_protocol_roce(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003451 kref_init(&mc->mcref);
3452 ret = cma_iboe_join_multicast(id_priv, mc);
Michael Wanga31ad3b2015-05-05 14:50:37 +02003453 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02003454 ret = cma_join_ib_multicast(id_priv, mc);
3455 else
Sean Heftyc8f6a362007-02-15 17:00:18 -08003456 ret = -ENOSYS;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003457
3458 if (ret) {
3459 spin_lock_irq(&id_priv->lock);
3460 list_del(&mc->list);
3461 spin_unlock_irq(&id_priv->lock);
3462 kfree(mc);
3463 }
3464 return ret;
3465}
3466EXPORT_SYMBOL(rdma_join_multicast);
3467
3468void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3469{
3470 struct rdma_id_private *id_priv;
3471 struct cma_multicast *mc;
3472
3473 id_priv = container_of(id, struct rdma_id_private, id);
3474 spin_lock_irq(&id_priv->lock);
3475 list_for_each_entry(mc, &id_priv->mc_list, list) {
Sean Heftyef560862013-05-29 10:09:10 -07003476 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
Sean Heftyc8f6a362007-02-15 17:00:18 -08003477 list_del(&mc->list);
3478 spin_unlock_irq(&id_priv->lock);
3479
3480 if (id->qp)
3481 ib_detach_mcast(id->qp,
3482 &mc->multicast.ib->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003483 be16_to_cpu(mc->multicast.ib->rec.mlid));
Michael Wang5c9a5282015-05-05 14:50:29 +02003484
3485 BUG_ON(id_priv->cma_dev->device != id->device);
3486
Michael Wanga31ad3b2015-05-05 14:50:37 +02003487 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003488 ib_sa_free_multicast(mc->multicast.ib);
3489 kfree(mc);
Ira Weiny5d9fb042015-05-14 15:01:46 -04003490 } else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02003491 kref_put(&mc->mcref, release_mc);
3492
Sean Heftyc8f6a362007-02-15 17:00:18 -08003493 return;
3494 }
3495 }
3496 spin_unlock_irq(&id_priv->lock);
3497}
3498EXPORT_SYMBOL(rdma_leave_multicast);
3499
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003500static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3501{
3502 struct rdma_dev_addr *dev_addr;
3503 struct cma_ndev_work *work;
3504
3505 dev_addr = &id_priv->id.route.addr.dev_addr;
3506
Sean Hefty6266ed62009-11-19 12:55:22 -08003507 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003508 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3509 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3510 ndev->name, &id_priv->id);
3511 work = kzalloc(sizeof *work, GFP_KERNEL);
3512 if (!work)
3513 return -ENOMEM;
3514
3515 INIT_WORK(&work->work, cma_ndev_work_handler);
3516 work->id = id_priv;
3517 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3518 atomic_inc(&id_priv->refcount);
3519 queue_work(cma_wq, &work->work);
3520 }
3521
3522 return 0;
3523}
3524
3525static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003526 void *ptr)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003527{
Jiri Pirko351638e2013-05-28 01:30:21 +00003528 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003529 struct cma_device *cma_dev;
3530 struct rdma_id_private *id_priv;
3531 int ret = NOTIFY_DONE;
3532
3533 if (dev_net(ndev) != &init_net)
3534 return NOTIFY_DONE;
3535
3536 if (event != NETDEV_BONDING_FAILOVER)
3537 return NOTIFY_DONE;
3538
3539 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3540 return NOTIFY_DONE;
3541
3542 mutex_lock(&lock);
3543 list_for_each_entry(cma_dev, &dev_list, list)
3544 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3545 ret = cma_netdev_change(ndev, id_priv);
3546 if (ret)
3547 goto out;
3548 }
3549
3550out:
3551 mutex_unlock(&lock);
3552 return ret;
3553}
3554
3555static struct notifier_block cma_nb = {
3556 .notifier_call = cma_netdev_callback
3557};
3558
Sean Heftye51060f2006-06-17 20:37:29 -07003559static void cma_add_one(struct ib_device *device)
3560{
3561 struct cma_device *cma_dev;
3562 struct rdma_id_private *id_priv;
3563
3564 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3565 if (!cma_dev)
3566 return;
3567
3568 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07003569
3570 init_completion(&cma_dev->comp);
3571 atomic_set(&cma_dev->refcount, 1);
3572 INIT_LIST_HEAD(&cma_dev->id_list);
3573 ib_set_client_data(device, &cma_client, cma_dev);
3574
3575 mutex_lock(&lock);
3576 list_add_tail(&cma_dev->list, &dev_list);
3577 list_for_each_entry(id_priv, &listen_any_list, list)
3578 cma_listen_on_dev(id_priv, cma_dev);
3579 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07003580}
3581
3582static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3583{
Sean Heftya1b1b612006-11-30 16:33:14 -08003584 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003585 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003586 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07003587
3588 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003589 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3590 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07003591 return 0;
3592
3593 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003594 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07003595
3596 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003597 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07003598 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003599
Sean Heftya1b1b612006-11-30 16:33:14 -08003600 memset(&event, 0, sizeof event);
3601 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003602 ret = id_priv->id.event_handler(&id_priv->id, &event);
3603out:
3604 mutex_unlock(&id_priv->handler_mutex);
3605 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003606}
3607
3608static void cma_process_remove(struct cma_device *cma_dev)
3609{
Sean Heftye51060f2006-06-17 20:37:29 -07003610 struct rdma_id_private *id_priv;
3611 int ret;
3612
Sean Heftye51060f2006-06-17 20:37:29 -07003613 mutex_lock(&lock);
3614 while (!list_empty(&cma_dev->id_list)) {
3615 id_priv = list_entry(cma_dev->id_list.next,
3616 struct rdma_id_private, list);
3617
Sean Heftyd02d1f52007-10-09 11:12:34 -07003618 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07003619 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07003620 atomic_inc(&id_priv->refcount);
3621 mutex_unlock(&lock);
3622
Sean Heftyd02d1f52007-10-09 11:12:34 -07003623 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003624 cma_deref_id(id_priv);
3625 if (ret)
3626 rdma_destroy_id(&id_priv->id);
3627
3628 mutex_lock(&lock);
3629 }
3630 mutex_unlock(&lock);
3631
3632 cma_deref_dev(cma_dev);
3633 wait_for_completion(&cma_dev->comp);
3634}
3635
Haggai Eran7c1eb452015-07-30 17:50:14 +03003636static void cma_remove_one(struct ib_device *device, void *client_data)
Sean Heftye51060f2006-06-17 20:37:29 -07003637{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003638 struct cma_device *cma_dev = client_data;
Sean Heftye51060f2006-06-17 20:37:29 -07003639
Sean Heftye51060f2006-06-17 20:37:29 -07003640 if (!cma_dev)
3641 return;
3642
3643 mutex_lock(&lock);
3644 list_del(&cma_dev->list);
3645 mutex_unlock(&lock);
3646
3647 cma_process_remove(cma_dev);
3648 kfree(cma_dev);
3649}
3650
Nir Muchtar753f6182011-01-03 15:33:53 +00003651static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3652{
3653 struct nlmsghdr *nlh;
3654 struct rdma_cm_id_stats *id_stats;
3655 struct rdma_id_private *id_priv;
3656 struct rdma_cm_id *id = NULL;
3657 struct cma_device *cma_dev;
3658 int i_dev = 0, i_id = 0;
3659
3660 /*
3661 * We export all of the IDs as a sequence of messages. Each
3662 * ID gets its own netlink message.
3663 */
3664 mutex_lock(&lock);
3665
3666 list_for_each_entry(cma_dev, &dev_list, list) {
3667 if (i_dev < cb->args[0]) {
3668 i_dev++;
3669 continue;
3670 }
3671
3672 i_id = 0;
3673 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3674 if (i_id < cb->args[1]) {
3675 i_id++;
3676 continue;
3677 }
3678
3679 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3680 sizeof *id_stats, RDMA_NL_RDMA_CM,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -05003681 RDMA_NL_RDMA_CM_ID_STATS,
3682 NLM_F_MULTI);
Nir Muchtar753f6182011-01-03 15:33:53 +00003683 if (!id_stats)
3684 goto out;
3685
3686 memset(id_stats, 0, sizeof *id_stats);
3687 id = &id_priv->id;
3688 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3689 id_stats->port_num = id->port_num;
3690 id_stats->bound_dev_if =
3691 id->route.addr.dev_addr.bound_dev_if;
3692
Sean Heftyce117ff2013-05-29 10:09:34 -07003693 if (ibnl_put_attr(skb, nlh,
3694 rdma_addr_size(cma_src_addr(id_priv)),
3695 cma_src_addr(id_priv),
3696 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
3697 goto out;
3698 if (ibnl_put_attr(skb, nlh,
3699 rdma_addr_size(cma_src_addr(id_priv)),
3700 cma_dst_addr(id_priv),
3701 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
3702 goto out;
Nir Muchtar753f6182011-01-03 15:33:53 +00003703
Nir Muchtar83e95022011-01-13 13:56:04 +00003704 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00003705 id_stats->port_space = id->ps;
3706 id_stats->cm_state = id_priv->state;
3707 id_stats->qp_num = id_priv->qp_num;
3708 id_stats->qp_type = id->qp_type;
3709
3710 i_id++;
3711 }
3712
3713 cb->args[1] = 0;
3714 i_dev++;
3715 }
3716
3717out:
3718 mutex_unlock(&lock);
3719 cb->args[0] = i_dev;
3720 cb->args[1] = i_id;
3721
3722 return skb->len;
3723}
3724
3725static const struct ibnl_client_cbs cma_cb_table[] = {
Gao feng809d5fc2012-10-04 20:15:49 +00003726 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
3727 .module = THIS_MODULE },
Nir Muchtar753f6182011-01-03 15:33:53 +00003728};
3729
Peter Huewe716abb12009-06-23 10:38:42 -07003730static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003731{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09003732 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003733
Sean Heftyc7f743a2007-02-01 12:23:37 -08003734 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07003735 if (!cma_wq)
3736 return -ENOMEM;
3737
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003738 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08003739 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003740 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003741
Sean Heftye51060f2006-06-17 20:37:29 -07003742 ret = ib_register_client(&cma_client);
3743 if (ret)
3744 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00003745
3746 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
3747 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
3748
Sean Heftye51060f2006-06-17 20:37:29 -07003749 return 0;
3750
3751err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003752 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003753 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003754 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003755 destroy_workqueue(cma_wq);
3756 return ret;
3757}
3758
Peter Huewe716abb12009-06-23 10:38:42 -07003759static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003760{
Nir Muchtar753f6182011-01-03 15:33:53 +00003761 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07003762 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003763 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003764 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003765 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003766 destroy_workqueue(cma_wq);
Sean Heftye51060f2006-06-17 20:37:29 -07003767 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08003768 idr_destroy(&udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003769 idr_destroy(&ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -07003770 idr_destroy(&ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07003771}
3772
3773module_init(cma_init);
3774module_exit(cma_cleanup);