blob: 59a2dafc8c574df13b5d186f9b0987c06c8e58c6 [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>
Haggai Eranf887f2a2015-07-30 17:50:23 +030049#include <net/ip_fib.h>
50#include <net/ip6_route.h>
Sean Heftye51060f2006-06-17 20:37:29 -070051
52#include <rdma/rdma_cm.h>
53#include <rdma/rdma_cm_ib.h>
Nir Muchtar753f6182011-01-03 15:33:53 +000054#include <rdma/rdma_netlink.h>
Sean Hefty2e2d1902013-05-29 10:09:09 -070055#include <rdma/ib.h>
Sean Heftye51060f2006-06-17 20:37:29 -070056#include <rdma/ib_cache.h>
57#include <rdma/ib_cm.h>
58#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050059#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070060
61MODULE_AUTHOR("Sean Hefty");
62MODULE_DESCRIPTION("Generic RDMA CM Agent");
63MODULE_LICENSE("Dual BSD/GPL");
64
65#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030066#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070067#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020068#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070069
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030070static const char * const cma_events[] = {
71 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
72 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
73 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
74 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
75 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
76 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
77 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
78 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
79 [RDMA_CM_EVENT_REJECTED] = "rejected",
80 [RDMA_CM_EVENT_ESTABLISHED] = "established",
81 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
82 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
83 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
84 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
85 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
86 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
87};
88
89const char *rdma_event_msg(enum rdma_cm_event_type event)
90{
91 size_t index = event;
92
93 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
94 cma_events[index] : "unrecognized event";
95}
96EXPORT_SYMBOL(rdma_event_msg);
97
Sean Heftye51060f2006-06-17 20:37:29 -070098static void cma_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +030099static void cma_remove_one(struct ib_device *device, void *client_data);
Sean Heftye51060f2006-06-17 20:37:29 -0700100
101static struct ib_client cma_client = {
102 .name = "cma",
103 .add = cma_add_one,
104 .remove = cma_remove_one
105};
106
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700107static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -0800108static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -0700109static LIST_HEAD(dev_list);
110static LIST_HEAD(listen_any_list);
111static DEFINE_MUTEX(lock);
112static struct workqueue_struct *cma_wq;
Sean Heftye51060f2006-06-17 20:37:29 -0700113static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -0800114static DEFINE_IDR(udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800115static DEFINE_IDR(ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -0700116static DEFINE_IDR(ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -0700117
Haggai Eranaac978e2015-07-30 17:50:20 +0300118static struct idr *cma_idr(enum rdma_port_space ps)
119{
120 switch (ps) {
121 case RDMA_PS_TCP:
122 return &tcp_ps;
123 case RDMA_PS_UDP:
124 return &udp_ps;
125 case RDMA_PS_IPOIB:
126 return &ipoib_ps;
127 case RDMA_PS_IB:
128 return &ib_ps;
129 default:
130 return NULL;
131 }
132}
133
Sean Heftye51060f2006-06-17 20:37:29 -0700134struct cma_device {
135 struct list_head list;
136 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -0700137 struct completion comp;
138 atomic_t refcount;
139 struct list_head id_list;
140};
141
Sean Heftye51060f2006-06-17 20:37:29 -0700142struct rdma_bind_list {
Haggai Eranaac978e2015-07-30 17:50:20 +0300143 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -0700144 struct hlist_head owners;
145 unsigned short port;
146};
147
Haggai Eranaac978e2015-07-30 17:50:20 +0300148static int cma_ps_alloc(enum rdma_port_space ps,
149 struct rdma_bind_list *bind_list, int snum)
150{
151 struct idr *idr = cma_idr(ps);
152
153 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
154}
155
156static struct rdma_bind_list *cma_ps_find(enum rdma_port_space ps, int snum)
157{
158 struct idr *idr = cma_idr(ps);
159
160 return idr_find(idr, snum);
161}
162
163static void cma_ps_remove(enum rdma_port_space ps, int snum)
164{
165 struct idr *idr = cma_idr(ps);
166
167 idr_remove(idr, snum);
168}
169
Sean Hefty68602122012-06-14 20:31:39 +0000170enum {
171 CMA_OPTION_AFONLY,
172};
173
Sean Heftye51060f2006-06-17 20:37:29 -0700174/*
175 * Device removal can occur at anytime, so we need extra handling to
176 * serialize notifying the user of device removal with other callbacks.
177 * We do this by disabling removal notification while a callback is in process,
178 * and reporting it after the callback completes.
179 */
180struct rdma_id_private {
181 struct rdma_cm_id id;
182
183 struct rdma_bind_list *bind_list;
184 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700185 struct list_head list; /* listen_any_list or cma_device.list */
186 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700187 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800188 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700189
Sean Heftyd02d1f52007-10-09 11:12:34 -0700190 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700191 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700192 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700193 struct mutex qp_mutex;
194
Sean Heftye51060f2006-06-17 20:37:29 -0700195 struct completion comp;
196 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700197 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700198
199 int backlog;
200 int timeout_ms;
201 struct ib_sa_query *query;
202 int query_id;
203 union {
204 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500205 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700206 } cm_id;
207
208 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800209 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700210 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000211 pid_t owner;
Sean Hefty68602122012-06-14 20:31:39 +0000212 u32 options;
Sean Heftye51060f2006-06-17 20:37:29 -0700213 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700214 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700215 u8 reuseaddr;
Sean Hefty5b0ec992012-06-14 20:31:39 +0000216 u8 afonly;
Sean Heftye51060f2006-06-17 20:37:29 -0700217};
218
Sean Heftyc8f6a362007-02-15 17:00:18 -0800219struct cma_multicast {
220 struct rdma_id_private *id_priv;
221 union {
222 struct ib_sa_multicast *ib;
223 } multicast;
224 struct list_head list;
225 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700226 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200227 struct kref mcref;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800228};
229
Sean Heftye51060f2006-06-17 20:37:29 -0700230struct cma_work {
231 struct work_struct work;
232 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700233 enum rdma_cm_state old_state;
234 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700235 struct rdma_cm_event event;
236};
237
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700238struct cma_ndev_work {
239 struct work_struct work;
240 struct rdma_id_private *id;
241 struct rdma_cm_event event;
242};
243
Eli Cohen3c86aa72010-10-13 21:26:51 +0200244struct iboe_mcast_work {
245 struct work_struct work;
246 struct rdma_id_private *id;
247 struct cma_multicast *mc;
248};
249
Sean Heftye51060f2006-06-17 20:37:29 -0700250union cma_ip_addr {
251 struct in6_addr ip6;
252 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000253 __be32 pad[3];
254 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700255 } ip4;
256};
257
258struct cma_hdr {
259 u8 cma_version;
260 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000261 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700262 union cma_ip_addr src_addr;
263 union cma_ip_addr dst_addr;
264};
265
Sean Heftye51060f2006-06-17 20:37:29 -0700266#define CMA_VERSION 0x00
Sean Heftye51060f2006-06-17 20:37:29 -0700267
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300268struct cma_req_info {
269 struct ib_device *device;
270 int port;
271 union ib_gid local_gid;
272 __be64 service_id;
273 u16 pkey;
274 bool has_gid:1;
275};
276
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700277static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700278{
279 unsigned long flags;
280 int ret;
281
282 spin_lock_irqsave(&id_priv->lock, flags);
283 ret = (id_priv->state == comp);
284 spin_unlock_irqrestore(&id_priv->lock, flags);
285 return ret;
286}
287
288static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700289 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700290{
291 unsigned long flags;
292 int ret;
293
294 spin_lock_irqsave(&id_priv->lock, flags);
295 if ((ret = (id_priv->state == comp)))
296 id_priv->state = exch;
297 spin_unlock_irqrestore(&id_priv->lock, flags);
298 return ret;
299}
300
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700301static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
302 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700303{
304 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700305 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700306
307 spin_lock_irqsave(&id_priv->lock, flags);
308 old = id_priv->state;
309 id_priv->state = exch;
310 spin_unlock_irqrestore(&id_priv->lock, flags);
311 return old;
312}
313
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300314static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
Sean Heftye51060f2006-06-17 20:37:29 -0700315{
316 return hdr->ip_version >> 4;
317}
318
319static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
320{
321 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
322}
323
Sean Heftye51060f2006-06-17 20:37:29 -0700324static void cma_attach_to_dev(struct rdma_id_private *id_priv,
325 struct cma_device *cma_dev)
326{
327 atomic_inc(&cma_dev->refcount);
328 id_priv->cma_dev = cma_dev;
329 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200330 id_priv->id.route.addr.dev_addr.transport =
331 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700332 list_add_tail(&id_priv->list, &cma_dev->id_list);
333}
334
335static inline void cma_deref_dev(struct cma_device *cma_dev)
336{
337 if (atomic_dec_and_test(&cma_dev->refcount))
338 complete(&cma_dev->comp);
339}
340
Eli Cohen3c86aa72010-10-13 21:26:51 +0200341static inline void release_mc(struct kref *kref)
342{
343 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
344
345 kfree(mc->multicast.ib);
346 kfree(mc);
347}
348
Sean Heftya396d432011-02-23 09:05:39 -0800349static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700350{
Sean Heftya396d432011-02-23 09:05:39 -0800351 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700352 list_del(&id_priv->list);
353 cma_deref_dev(id_priv->cma_dev);
354 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800355 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700356}
357
Sean Heftyf4753832013-05-29 10:09:14 -0700358static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
359{
360 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
361}
362
363static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
364{
365 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
366}
367
368static inline unsigned short cma_family(struct rdma_id_private *id_priv)
369{
370 return id_priv->id.route.addr.src_addr.ss_family;
371}
372
Sean Hefty5c438132013-05-29 10:09:23 -0700373static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800374{
375 struct ib_sa_mcmember_rec rec;
376 int ret = 0;
377
Sean Hefty5c438132013-05-29 10:09:23 -0700378 if (id_priv->qkey) {
379 if (qkey && id_priv->qkey != qkey)
380 return -EINVAL;
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700381 return 0;
Sean Hefty5c438132013-05-29 10:09:23 -0700382 }
383
384 if (qkey) {
385 id_priv->qkey = qkey;
386 return 0;
387 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700388
389 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800390 case RDMA_PS_UDP:
Sean Hefty5c438132013-05-29 10:09:23 -0700391 case RDMA_PS_IB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700392 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800393 break;
394 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700395 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
396 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
397 id_priv->id.port_num, &rec.mgid,
398 &rec);
399 if (!ret)
400 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800401 break;
402 default:
403 break;
404 }
405 return ret;
406}
407
Sean Hefty680f9202013-05-29 10:09:12 -0700408static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
409{
410 dev_addr->dev_type = ARPHRD_INFINIBAND;
411 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
412 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
413}
414
415static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
416{
417 int ret;
418
419 if (addr->sa_family != AF_IB) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200420 ret = rdma_translate_ip(addr, dev_addr, NULL);
Sean Hefty680f9202013-05-29 10:09:12 -0700421 } else {
422 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
423 ret = 0;
424 }
425
426 return ret;
427}
428
Michael Wang7c111472015-05-05 14:50:30 +0200429static inline int cma_validate_port(struct ib_device *device, u8 port,
430 union ib_gid *gid, int dev_type)
431{
432 u8 found_port;
433 int ret = -ENODEV;
434
435 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
436 return ret;
437
438 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
439 return ret;
440
441 ret = ib_find_cached_gid(device, gid, &found_port, NULL);
442 if (port != found_port)
443 return -ENODEV;
444
445 return ret;
446}
447
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400448static int cma_acquire_dev(struct rdma_id_private *id_priv,
449 struct rdma_id_private *listen_id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700450{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800451 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700452 struct cma_device *cma_dev;
Michael Wang7c111472015-05-05 14:50:30 +0200453 union ib_gid gid, iboe_gid, *gidp;
Sean Heftye51060f2006-06-17 20:37:29 -0700454 int ret = -ENODEV;
Michael Wang7c111472015-05-05 14:50:30 +0200455 u8 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700456
Michael Wang7c111472015-05-05 14:50:30 +0200457 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000458 id_priv->id.ps == RDMA_PS_IPOIB)
459 return -EINVAL;
460
Sean Heftya396d432011-02-23 09:05:39 -0800461 mutex_lock(&lock);
Moni Shoua7b856272013-12-12 18:03:12 +0200462 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
463 &iboe_gid);
464
Eli Cohen3c86aa72010-10-13 21:26:51 +0200465 memcpy(&gid, dev_addr->src_dev_addr +
466 rdma_addr_gid_offset(dev_addr), sizeof gid);
Michael Wang7c111472015-05-05 14:50:30 +0200467
468 if (listen_id_priv) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400469 cma_dev = listen_id_priv->cma_dev;
470 port = listen_id_priv->id.port_num;
Ira Weiny5d9fb042015-05-14 15:01:46 -0400471 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200472 &iboe_gid : &gid;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400473
Michael Wang7c111472015-05-05 14:50:30 +0200474 ret = cma_validate_port(cma_dev->device, port, gidp,
475 dev_addr->dev_type);
476 if (!ret) {
477 id_priv->id.port_num = port;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400478 goto out;
479 }
480 }
Michael Wang7c111472015-05-05 14:50:30 +0200481
Sean Heftye51060f2006-06-17 20:37:29 -0700482 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200483 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400484 if (listen_id_priv &&
485 listen_id_priv->cma_dev == cma_dev &&
486 listen_id_priv->id.port_num == port)
487 continue;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200488
Ira Weiny5d9fb042015-05-14 15:01:46 -0400489 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200490 &iboe_gid : &gid;
491
492 ret = cma_validate_port(cma_dev->device, port, gidp,
493 dev_addr->dev_type);
494 if (!ret) {
495 id_priv->id.port_num = port;
496 goto out;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200497 }
Sean Heftye51060f2006-06-17 20:37:29 -0700498 }
499 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200500
501out:
502 if (!ret)
503 cma_attach_to_dev(id_priv, cma_dev);
504
Sean Heftya396d432011-02-23 09:05:39 -0800505 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700506 return ret;
507}
508
Sean Heftyf17df3b2013-05-29 10:09:17 -0700509/*
510 * Select the source IB device and address to reach the destination IB address.
511 */
512static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
513{
514 struct cma_device *cma_dev, *cur_dev;
515 struct sockaddr_ib *addr;
516 union ib_gid gid, sgid, *dgid;
517 u16 pkey, index;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700518 u8 p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700519 int i;
520
521 cma_dev = NULL;
522 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
523 dgid = (union ib_gid *) &addr->sib_addr;
524 pkey = ntohs(addr->sib_pkey);
525
526 list_for_each_entry(cur_dev, &dev_list, list) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700527 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
Michael Wang30a74ef2015-05-05 14:50:39 +0200528 if (!rdma_cap_af_ib(cur_dev->device, p))
Michael Wangfef60902015-05-05 14:50:31 +0200529 continue;
530
Sean Heftyf17df3b2013-05-29 10:09:17 -0700531 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
532 continue;
533
534 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i, &gid); i++) {
535 if (!memcmp(&gid, dgid, sizeof(gid))) {
536 cma_dev = cur_dev;
537 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700538 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700539 goto found;
540 }
541
542 if (!cma_dev && (gid.global.subnet_prefix ==
543 dgid->global.subnet_prefix)) {
544 cma_dev = cur_dev;
545 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700546 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700547 }
548 }
549 }
550 }
551
552 if (!cma_dev)
553 return -ENODEV;
554
555found:
556 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf17df3b2013-05-29 10:09:17 -0700557 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
558 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
559 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
560 return 0;
561}
562
Sean Heftye51060f2006-06-17 20:37:29 -0700563static void cma_deref_id(struct rdma_id_private *id_priv)
564{
565 if (atomic_dec_and_test(&id_priv->refcount))
566 complete(&id_priv->comp);
567}
568
Or Gerlitzde910bd2008-07-14 23:48:53 -0700569static int cma_disable_callback(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700570 enum rdma_cm_state state)
Sean Hefty8aa08602007-05-07 11:49:00 -0700571{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700572 mutex_lock(&id_priv->handler_mutex);
573 if (id_priv->state != state) {
574 mutex_unlock(&id_priv->handler_mutex);
575 return -EINVAL;
576 }
577 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700578}
579
580struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000581 void *context, enum rdma_port_space ps,
582 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700583{
584 struct rdma_id_private *id_priv;
585
586 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
587 if (!id_priv)
588 return ERR_PTR(-ENOMEM);
589
Nir Muchtar83e95022011-01-13 13:56:04 +0000590 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700591 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700592 id_priv->id.context = context;
593 id_priv->id.event_handler = event_handler;
594 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000595 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700596 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700597 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700598 init_completion(&id_priv->comp);
599 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700600 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700601 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800602 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700603 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
604
605 return &id_priv->id;
606}
607EXPORT_SYMBOL(rdma_create_id);
608
Sean Heftyc8f6a362007-02-15 17:00:18 -0800609static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700610{
611 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800612 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700613
Sean Heftyc8f6a362007-02-15 17:00:18 -0800614 qp_attr.qp_state = IB_QPS_INIT;
615 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700616 if (ret)
617 return ret;
618
Sean Heftyc8f6a362007-02-15 17:00:18 -0800619 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
620 if (ret)
621 return ret;
622
623 qp_attr.qp_state = IB_QPS_RTR;
624 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
625 if (ret)
626 return ret;
627
628 qp_attr.qp_state = IB_QPS_RTS;
629 qp_attr.sq_psn = 0;
630 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
631
632 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700633}
634
Sean Heftyc8f6a362007-02-15 17:00:18 -0800635static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500636{
637 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800638 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500639
640 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800641 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
642 if (ret)
643 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500644
Sean Heftyc8f6a362007-02-15 17:00:18 -0800645 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500646}
647
Sean Heftye51060f2006-06-17 20:37:29 -0700648int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
649 struct ib_qp_init_attr *qp_init_attr)
650{
651 struct rdma_id_private *id_priv;
652 struct ib_qp *qp;
653 int ret;
654
655 id_priv = container_of(id, struct rdma_id_private, id);
656 if (id->device != pd->device)
657 return -EINVAL;
658
659 qp = ib_create_qp(pd, qp_init_attr);
660 if (IS_ERR(qp))
661 return PTR_ERR(qp);
662
Sean Heftyb26f9b92010-04-01 17:08:41 +0000663 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800664 ret = cma_init_ud_qp(id_priv, qp);
665 else
666 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700667 if (ret)
668 goto err;
669
670 id->qp = qp;
671 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700672 id_priv->srq = (qp->srq != NULL);
673 return 0;
674err:
675 ib_destroy_qp(qp);
676 return ret;
677}
678EXPORT_SYMBOL(rdma_create_qp);
679
680void rdma_destroy_qp(struct rdma_cm_id *id)
681{
Sean Heftyc5483382007-09-24 13:19:09 -0700682 struct rdma_id_private *id_priv;
683
684 id_priv = container_of(id, struct rdma_id_private, id);
685 mutex_lock(&id_priv->qp_mutex);
686 ib_destroy_qp(id_priv->id.qp);
687 id_priv->id.qp = NULL;
688 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700689}
690EXPORT_SYMBOL(rdma_destroy_qp);
691
Sean Hefty5851bb82008-01-04 10:47:12 -0800692static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
693 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700694{
695 struct ib_qp_attr qp_attr;
696 int qp_attr_mask, ret;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200697 union ib_gid sgid;
Sean Heftye51060f2006-06-17 20:37:29 -0700698
Sean Heftyc5483382007-09-24 13:19:09 -0700699 mutex_lock(&id_priv->qp_mutex);
700 if (!id_priv->id.qp) {
701 ret = 0;
702 goto out;
703 }
Sean Heftye51060f2006-06-17 20:37:29 -0700704
705 /* Need to update QP attributes from default values. */
706 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700707 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700708 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700709 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700710
Sean Heftyc5483382007-09-24 13:19:09 -0700711 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700712 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700713 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700714
715 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700716 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700717 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700718 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700719
Matan Barakdd5f03b2013-12-12 18:03:11 +0200720 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
721 qp_attr.ah_attr.grh.sgid_index, &sgid);
722 if (ret)
723 goto out;
724
Michael Wangfef60902015-05-05 14:50:31 +0200725 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
726
Ira Weiny5d9fb042015-05-14 15:01:46 -0400727 if (rdma_protocol_roce(id_priv->id.device, id_priv->id.port_num)) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200728 ret = rdma_addr_find_smac_by_sgid(&sgid, qp_attr.smac, NULL);
729
730 if (ret)
731 goto out;
732 }
Sean Hefty5851bb82008-01-04 10:47:12 -0800733 if (conn_param)
734 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700735 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
736out:
737 mutex_unlock(&id_priv->qp_mutex);
738 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700739}
740
Sean Hefty5851bb82008-01-04 10:47:12 -0800741static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
742 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700743{
744 struct ib_qp_attr qp_attr;
745 int qp_attr_mask, ret;
746
Sean Heftyc5483382007-09-24 13:19:09 -0700747 mutex_lock(&id_priv->qp_mutex);
748 if (!id_priv->id.qp) {
749 ret = 0;
750 goto out;
751 }
Sean Heftye51060f2006-06-17 20:37:29 -0700752
753 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700754 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700755 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700756 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700757
Sean Hefty5851bb82008-01-04 10:47:12 -0800758 if (conn_param)
759 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700760 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
761out:
762 mutex_unlock(&id_priv->qp_mutex);
763 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700764}
765
Sean Heftyc5483382007-09-24 13:19:09 -0700766static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700767{
768 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700769 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700770
Sean Heftyc5483382007-09-24 13:19:09 -0700771 mutex_lock(&id_priv->qp_mutex);
772 if (!id_priv->id.qp) {
773 ret = 0;
774 goto out;
775 }
Sean Heftye51060f2006-06-17 20:37:29 -0700776
777 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700778 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
779out:
780 mutex_unlock(&id_priv->qp_mutex);
781 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700782}
783
Sean Heftyc8f6a362007-02-15 17:00:18 -0800784static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
785 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
786{
787 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
788 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200789 u16 pkey;
790
Michael Wang227128f2015-05-05 14:50:40 +0200791 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
Eli Cohen3c86aa72010-10-13 21:26:51 +0200792 pkey = 0xffff;
Michael Wangfef60902015-05-05 14:50:31 +0200793 else
794 pkey = ib_addr_get_pkey(dev_addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800795
796 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200797 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800798 if (ret)
799 return ret;
800
801 qp_attr->port_num = id_priv->id.port_num;
802 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
803
Sean Heftyb26f9b92010-04-01 17:08:41 +0000804 if (id_priv->id.qp_type == IB_QPT_UD) {
Sean Hefty5c438132013-05-29 10:09:23 -0700805 ret = cma_set_qkey(id_priv, 0);
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700806 if (ret)
807 return ret;
808
Sean Heftyc8f6a362007-02-15 17:00:18 -0800809 qp_attr->qkey = id_priv->qkey;
810 *qp_attr_mask |= IB_QP_QKEY;
811 } else {
812 qp_attr->qp_access_flags = 0;
813 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
814 }
815 return 0;
816}
817
Sean Heftye51060f2006-06-17 20:37:29 -0700818int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
819 int *qp_attr_mask)
820{
821 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800822 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700823
824 id_priv = container_of(id, struct rdma_id_private, id);
Michael Wang72219cea2015-05-05 14:50:34 +0200825 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +0000826 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800827 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
828 else
829 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
830 qp_attr_mask);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200831
Sean Heftye51060f2006-06-17 20:37:29 -0700832 if (qp_attr->qp_state == IB_QPS_RTR)
833 qp_attr->rq_psn = id_priv->seq_num;
Michael Wang04215332015-05-05 14:50:35 +0200834 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800835 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300836 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800837 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
838 } else
839 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
840 qp_attr_mask);
Michael Wang21655af2015-05-05 14:50:27 +0200841 } else
Sean Heftye51060f2006-06-17 20:37:29 -0700842 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -0700843
844 return ret;
845}
846EXPORT_SYMBOL(rdma_init_qp_attr);
847
848static inline int cma_zero_addr(struct sockaddr *addr)
849{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700850 switch (addr->sa_family) {
851 case AF_INET:
852 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
853 case AF_INET6:
854 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
855 case AF_IB:
856 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
857 default:
858 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700859 }
860}
861
862static inline int cma_loopback_addr(struct sockaddr *addr)
863{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700864 switch (addr->sa_family) {
865 case AF_INET:
866 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
867 case AF_INET6:
868 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
869 case AF_IB:
870 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
871 default:
872 return 0;
873 }
Sean Heftye51060f2006-06-17 20:37:29 -0700874}
875
876static inline int cma_any_addr(struct sockaddr *addr)
877{
878 return cma_zero_addr(addr) || cma_loopback_addr(addr);
879}
880
Hefty, Sean43b752d2011-05-09 22:06:10 -0700881static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
882{
883 if (src->sa_family != dst->sa_family)
884 return -1;
885
886 switch (src->sa_family) {
887 case AF_INET:
888 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
889 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
Sean Hefty2e2d1902013-05-29 10:09:09 -0700890 case AF_INET6:
Hefty, Sean43b752d2011-05-09 22:06:10 -0700891 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
892 &((struct sockaddr_in6 *) dst)->sin6_addr);
Sean Hefty2e2d1902013-05-29 10:09:09 -0700893 default:
894 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
895 &((struct sockaddr_ib *) dst)->sib_addr);
Hefty, Sean43b752d2011-05-09 22:06:10 -0700896 }
897}
898
Sean Hefty58afdcb2013-05-29 10:09:11 -0700899static __be16 cma_port(struct sockaddr *addr)
Sean Hefty628e5f62006-11-30 16:44:16 -0800900{
Sean Hefty58afdcb2013-05-29 10:09:11 -0700901 struct sockaddr_ib *sib;
902
903 switch (addr->sa_family) {
904 case AF_INET:
Sean Hefty628e5f62006-11-30 16:44:16 -0800905 return ((struct sockaddr_in *) addr)->sin_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700906 case AF_INET6:
Sean Hefty628e5f62006-11-30 16:44:16 -0800907 return ((struct sockaddr_in6 *) addr)->sin6_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700908 case AF_IB:
909 sib = (struct sockaddr_ib *) addr;
910 return htons((u16) (be64_to_cpu(sib->sib_sid) &
911 be64_to_cpu(sib->sib_sid_mask)));
912 default:
913 return 0;
914 }
Sean Hefty628e5f62006-11-30 16:44:16 -0800915}
916
Sean Heftye51060f2006-06-17 20:37:29 -0700917static inline int cma_any_port(struct sockaddr *addr)
918{
Sean Hefty628e5f62006-11-30 16:44:16 -0800919 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700920}
921
Haggai Eran0c505f72015-07-30 17:50:19 +0300922static void cma_save_ib_info(struct sockaddr *src_addr,
923 struct sockaddr *dst_addr,
924 struct rdma_cm_id *listen_id,
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700925 struct ib_sa_path_rec *path)
Sean Heftye51060f2006-06-17 20:37:29 -0700926{
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700927 struct sockaddr_ib *listen_ib, *ib;
Sean Heftye51060f2006-06-17 20:37:29 -0700928
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700929 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
Haggai Eran0c505f72015-07-30 17:50:19 +0300930 if (src_addr) {
931 ib = (struct sockaddr_ib *)src_addr;
932 ib->sib_family = AF_IB;
933 if (path) {
934 ib->sib_pkey = path->pkey;
935 ib->sib_flowinfo = path->flow_label;
936 memcpy(&ib->sib_addr, &path->sgid, 16);
937 ib->sib_sid = path->service_id;
938 ib->sib_scope_id = 0;
939 } else {
940 ib->sib_pkey = listen_ib->sib_pkey;
941 ib->sib_flowinfo = listen_ib->sib_flowinfo;
942 ib->sib_addr = listen_ib->sib_addr;
943 ib->sib_sid = listen_ib->sib_sid;
944 ib->sib_scope_id = listen_ib->sib_scope_id;
945 }
946 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
Matthew Finlayc07678b2015-05-19 00:11:48 -0700947 }
Haggai Eran0c505f72015-07-30 17:50:19 +0300948 if (dst_addr) {
949 ib = (struct sockaddr_ib *)dst_addr;
950 ib->sib_family = AF_IB;
951 if (path) {
952 ib->sib_pkey = path->pkey;
953 ib->sib_flowinfo = path->flow_label;
954 memcpy(&ib->sib_addr, &path->dgid, 16);
955 }
Matthew Finlayc07678b2015-05-19 00:11:48 -0700956 }
Sean Heftye51060f2006-06-17 20:37:29 -0700957}
958
Haggai Eran0c505f72015-07-30 17:50:19 +0300959static void cma_save_ip4_info(struct sockaddr *src_addr,
960 struct sockaddr *dst_addr,
961 struct cma_hdr *hdr,
962 __be16 local_port)
Sean Heftye51060f2006-06-17 20:37:29 -0700963{
Jason Gunthorpe28521442015-04-20 14:01:11 -0600964 struct sockaddr_in *ip4;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700965
Haggai Eran0c505f72015-07-30 17:50:19 +0300966 if (src_addr) {
967 ip4 = (struct sockaddr_in *)src_addr;
968 ip4->sin_family = AF_INET;
969 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr;
970 ip4->sin_port = local_port;
971 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700972
Haggai Eran0c505f72015-07-30 17:50:19 +0300973 if (dst_addr) {
974 ip4 = (struct sockaddr_in *)dst_addr;
975 ip4->sin_family = AF_INET;
976 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr;
977 ip4->sin_port = hdr->port;
978 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700979}
980
Haggai Eran0c505f72015-07-30 17:50:19 +0300981static void cma_save_ip6_info(struct sockaddr *src_addr,
982 struct sockaddr *dst_addr,
983 struct cma_hdr *hdr,
984 __be16 local_port)
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700985{
Jason Gunthorpe28521442015-04-20 14:01:11 -0600986 struct sockaddr_in6 *ip6;
Sean Heftye51060f2006-06-17 20:37:29 -0700987
Haggai Eran0c505f72015-07-30 17:50:19 +0300988 if (src_addr) {
989 ip6 = (struct sockaddr_in6 *)src_addr;
990 ip6->sin6_family = AF_INET6;
991 ip6->sin6_addr = hdr->dst_addr.ip6;
992 ip6->sin6_port = local_port;
993 }
Sean Heftye51060f2006-06-17 20:37:29 -0700994
Haggai Eran0c505f72015-07-30 17:50:19 +0300995 if (dst_addr) {
996 ip6 = (struct sockaddr_in6 *)dst_addr;
997 ip6->sin6_family = AF_INET6;
998 ip6->sin6_addr = hdr->src_addr.ip6;
999 ip6->sin6_port = hdr->port;
1000 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001001}
1002
Haggai Eran0c505f72015-07-30 17:50:19 +03001003static u16 cma_port_from_service_id(__be64 service_id)
1004{
1005 return (u16)be64_to_cpu(service_id);
1006}
1007
1008static int cma_save_ip_info(struct sockaddr *src_addr,
1009 struct sockaddr *dst_addr,
1010 struct ib_cm_event *ib_event,
1011 __be64 service_id)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001012{
1013 struct cma_hdr *hdr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001014 __be16 port;
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001015
1016 hdr = ib_event->private_data;
1017 if (hdr->cma_version != CMA_VERSION)
1018 return -EINVAL;
1019
Haggai Eran0c505f72015-07-30 17:50:19 +03001020 port = htons(cma_port_from_service_id(service_id));
1021
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001022 switch (cma_get_ip_ver(hdr)) {
1023 case 4:
Haggai Eran0c505f72015-07-30 17:50:19 +03001024 cma_save_ip4_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001025 break;
1026 case 6:
Haggai Eran0c505f72015-07-30 17:50:19 +03001027 cma_save_ip6_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001028 break;
1029 default:
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001030 return -EAFNOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07001031 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001032
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001033 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001034}
1035
Haggai Eran0c505f72015-07-30 17:50:19 +03001036static int cma_save_net_info(struct sockaddr *src_addr,
1037 struct sockaddr *dst_addr,
1038 struct rdma_cm_id *listen_id,
1039 struct ib_cm_event *ib_event,
1040 sa_family_t sa_family, __be64 service_id)
1041{
1042 if (sa_family == AF_IB) {
1043 if (ib_event->event == IB_CM_REQ_RECEIVED)
1044 cma_save_ib_info(src_addr, dst_addr, listen_id,
1045 ib_event->param.req_rcvd.primary_path);
1046 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1047 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1048 return 0;
1049 }
1050
1051 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1052}
1053
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001054static int cma_save_req_info(const struct ib_cm_event *ib_event,
1055 struct cma_req_info *req)
1056{
1057 const struct ib_cm_req_event_param *req_param =
1058 &ib_event->param.req_rcvd;
1059 const struct ib_cm_sidr_req_event_param *sidr_param =
1060 &ib_event->param.sidr_req_rcvd;
1061
1062 switch (ib_event->event) {
1063 case IB_CM_REQ_RECEIVED:
1064 req->device = req_param->listen_id->device;
1065 req->port = req_param->port;
1066 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1067 sizeof(req->local_gid));
1068 req->has_gid = true;
1069 req->service_id = req_param->primary_path->service_id;
1070 req->pkey = req_param->bth_pkey;
1071 break;
1072 case IB_CM_SIDR_REQ_RECEIVED:
1073 req->device = sidr_param->listen_id->device;
1074 req->port = sidr_param->port;
1075 req->has_gid = false;
1076 req->service_id = sidr_param->service_id;
1077 req->pkey = sidr_param->bth_pkey;
1078 break;
1079 default:
1080 return -EINVAL;
1081 }
1082
1083 return 0;
1084}
1085
Haggai Eranf887f2a2015-07-30 17:50:23 +03001086static bool validate_ipv4_net_dev(struct net_device *net_dev,
1087 const struct sockaddr_in *dst_addr,
1088 const struct sockaddr_in *src_addr)
1089{
1090 __be32 daddr = dst_addr->sin_addr.s_addr,
1091 saddr = src_addr->sin_addr.s_addr;
1092 struct fib_result res;
1093 struct flowi4 fl4;
1094 int err;
1095 bool ret;
1096
1097 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1098 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1099 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1100 ipv4_is_loopback(saddr))
1101 return false;
1102
1103 memset(&fl4, 0, sizeof(fl4));
1104 fl4.flowi4_iif = net_dev->ifindex;
1105 fl4.daddr = daddr;
1106 fl4.saddr = saddr;
1107
1108 rcu_read_lock();
1109 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
1110 if (err)
1111 return false;
1112
1113 ret = FIB_RES_DEV(res) == net_dev;
1114 rcu_read_unlock();
1115
1116 return ret;
1117}
1118
1119static bool validate_ipv6_net_dev(struct net_device *net_dev,
1120 const struct sockaddr_in6 *dst_addr,
1121 const struct sockaddr_in6 *src_addr)
1122{
1123#if IS_ENABLED(CONFIG_IPV6)
1124 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1125 IPV6_ADDR_LINKLOCAL;
1126 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1127 &src_addr->sin6_addr, net_dev->ifindex,
1128 strict);
1129 bool ret;
1130
1131 if (!rt)
1132 return false;
1133
1134 ret = rt->rt6i_idev->dev == net_dev;
1135 ip6_rt_put(rt);
1136
1137 return ret;
1138#else
1139 return false;
1140#endif
1141}
1142
1143static bool validate_net_dev(struct net_device *net_dev,
1144 const struct sockaddr *daddr,
1145 const struct sockaddr *saddr)
1146{
1147 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1148 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1149 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1150 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1151
1152 switch (daddr->sa_family) {
1153 case AF_INET:
1154 return saddr->sa_family == AF_INET &&
1155 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1156
1157 case AF_INET6:
1158 return saddr->sa_family == AF_INET6 &&
1159 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1160
1161 default:
1162 return false;
1163 }
1164}
1165
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001166static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1167 const struct cma_req_info *req)
1168{
Haggai Eranf887f2a2015-07-30 17:50:23 +03001169 struct sockaddr_storage listen_addr_storage, src_addr_storage;
1170 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1171 *src_addr = (struct sockaddr *)&src_addr_storage;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001172 struct net_device *net_dev;
1173 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1174 int err;
1175
Haggai Eranf887f2a2015-07-30 17:50:23 +03001176 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1177 req->service_id);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001178 if (err)
1179 return ERR_PTR(err);
1180
1181 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1182 gid, listen_addr);
1183 if (!net_dev)
1184 return ERR_PTR(-ENODEV);
1185
Haggai Eranf887f2a2015-07-30 17:50:23 +03001186 if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1187 dev_put(net_dev);
1188 return ERR_PTR(-EHOSTUNREACH);
1189 }
1190
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001191 return net_dev;
1192}
1193
1194static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1195{
1196 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1197}
1198
1199static bool cma_match_private_data(struct rdma_id_private *id_priv,
1200 const struct cma_hdr *hdr)
1201{
1202 struct sockaddr *addr = cma_src_addr(id_priv);
1203 __be32 ip4_addr;
1204 struct in6_addr ip6_addr;
1205
1206 if (cma_any_addr(addr) && !id_priv->afonly)
1207 return true;
1208
1209 switch (addr->sa_family) {
1210 case AF_INET:
1211 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1212 if (cma_get_ip_ver(hdr) != 4)
1213 return false;
1214 if (!cma_any_addr(addr) &&
1215 hdr->dst_addr.ip4.addr != ip4_addr)
1216 return false;
1217 break;
1218 case AF_INET6:
1219 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1220 if (cma_get_ip_ver(hdr) != 6)
1221 return false;
1222 if (!cma_any_addr(addr) &&
1223 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1224 return false;
1225 break;
1226 case AF_IB:
1227 return true;
1228 default:
1229 return false;
1230 }
1231
1232 return true;
1233}
1234
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001235static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1236{
1237 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1238 enum rdma_transport_type transport =
1239 rdma_node_get_transport(device->node_type);
1240
1241 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1242}
1243
1244static bool cma_protocol_roce(const struct rdma_cm_id *id)
1245{
1246 struct ib_device *device = id->device;
1247 const int port_num = id->port_num ?: rdma_start_port(device);
1248
1249 return cma_protocol_roce_dev_port(device, port_num);
1250}
1251
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001252static bool cma_match_net_dev(const struct rdma_id_private *id_priv,
1253 const struct net_device *net_dev)
1254{
1255 const struct rdma_addr *addr = &id_priv->id.route.addr;
1256
1257 if (!net_dev)
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001258 /* This request is an AF_IB request or a RoCE request */
1259 return addr->src_addr.ss_family == AF_IB ||
1260 cma_protocol_roce(&id_priv->id);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001261
1262 return !addr->dev_addr.bound_dev_if ||
1263 (net_eq(dev_net(net_dev), &init_net) &&
1264 addr->dev_addr.bound_dev_if == net_dev->ifindex);
1265}
1266
1267static struct rdma_id_private *cma_find_listener(
1268 const struct rdma_bind_list *bind_list,
1269 const struct ib_cm_id *cm_id,
1270 const struct ib_cm_event *ib_event,
1271 const struct cma_req_info *req,
1272 const struct net_device *net_dev)
1273{
1274 struct rdma_id_private *id_priv, *id_priv_dev;
1275
1276 if (!bind_list)
1277 return ERR_PTR(-EINVAL);
1278
1279 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1280 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1281 if (id_priv->id.device == cm_id->device &&
1282 cma_match_net_dev(id_priv, net_dev))
1283 return id_priv;
1284 list_for_each_entry(id_priv_dev,
1285 &id_priv->listen_list,
1286 listen_list) {
1287 if (id_priv_dev->id.device == cm_id->device &&
1288 cma_match_net_dev(id_priv_dev, net_dev))
1289 return id_priv_dev;
1290 }
1291 }
1292 }
1293
1294 return ERR_PTR(-EINVAL);
1295}
1296
1297static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001298 struct ib_cm_event *ib_event,
1299 struct net_device **net_dev)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001300{
1301 struct cma_req_info req;
1302 struct rdma_bind_list *bind_list;
1303 struct rdma_id_private *id_priv;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001304 int err;
1305
1306 err = cma_save_req_info(ib_event, &req);
1307 if (err)
1308 return ERR_PTR(err);
1309
Haggai Eran0b3ca762015-07-30 17:50:24 +03001310 *net_dev = cma_get_net_dev(ib_event, &req);
1311 if (IS_ERR(*net_dev)) {
1312 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001313 /* Assuming the protocol is AF_IB */
Haggai Eran0b3ca762015-07-30 17:50:24 +03001314 *net_dev = NULL;
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001315 } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1316 /* TODO find the net dev matching the request parameters
1317 * through the RoCE GID table */
1318 *net_dev = NULL;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001319 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001320 return ERR_CAST(*net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001321 }
1322 }
1323
1324 bind_list = cma_ps_find(rdma_ps_from_service_id(req.service_id),
1325 cma_port_from_service_id(req.service_id));
Haggai Eran0b3ca762015-07-30 17:50:24 +03001326 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
Haggai Eranbe688192015-08-27 15:55:15 +03001327 if (IS_ERR(id_priv)) {
1328 dev_put(*net_dev);
1329 *net_dev = NULL;
1330 }
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001331
1332 return id_priv;
1333}
1334
Sean Heftye8160e12013-05-29 10:09:22 -07001335static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07001336{
Sean Heftye8160e12013-05-29 10:09:22 -07001337 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
Sean Heftye51060f2006-06-17 20:37:29 -07001338}
1339
Sean Heftye51060f2006-06-17 20:37:29 -07001340static void cma_cancel_route(struct rdma_id_private *id_priv)
1341{
Michael Wangfe53ba22015-05-05 14:50:36 +02001342 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001343 if (id_priv->query)
1344 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001345 }
1346}
1347
Sean Heftye51060f2006-06-17 20:37:29 -07001348static void cma_cancel_listens(struct rdma_id_private *id_priv)
1349{
1350 struct rdma_id_private *dev_id_priv;
1351
Sean Heftyd02d1f52007-10-09 11:12:34 -07001352 /*
1353 * Remove from listen_any_list to prevent added devices from spawning
1354 * additional listen requests.
1355 */
Sean Heftye51060f2006-06-17 20:37:29 -07001356 mutex_lock(&lock);
1357 list_del(&id_priv->list);
1358
1359 while (!list_empty(&id_priv->listen_list)) {
1360 dev_id_priv = list_entry(id_priv->listen_list.next,
1361 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001362 /* sync with device removal to avoid duplicate destruction */
1363 list_del_init(&dev_id_priv->list);
1364 list_del(&dev_id_priv->listen_list);
1365 mutex_unlock(&lock);
1366
1367 rdma_destroy_id(&dev_id_priv->id);
1368 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001369 }
1370 mutex_unlock(&lock);
1371}
1372
1373static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001374 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -07001375{
1376 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001377 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001378 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1379 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001380 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001381 cma_cancel_route(id_priv);
1382 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001383 case RDMA_CM_LISTEN:
Sean Heftyf4753832013-05-29 10:09:14 -07001384 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001385 cma_cancel_listens(id_priv);
1386 break;
1387 default:
1388 break;
1389 }
1390}
1391
1392static void cma_release_port(struct rdma_id_private *id_priv)
1393{
1394 struct rdma_bind_list *bind_list = id_priv->bind_list;
1395
1396 if (!bind_list)
1397 return;
1398
1399 mutex_lock(&lock);
1400 hlist_del(&id_priv->node);
1401 if (hlist_empty(&bind_list->owners)) {
Haggai Eranaac978e2015-07-30 17:50:20 +03001402 cma_ps_remove(bind_list->ps, bind_list->port);
Sean Heftye51060f2006-06-17 20:37:29 -07001403 kfree(bind_list);
1404 }
1405 mutex_unlock(&lock);
1406}
1407
Sean Heftyc8f6a362007-02-15 17:00:18 -08001408static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1409{
1410 struct cma_multicast *mc;
1411
1412 while (!list_empty(&id_priv->mc_list)) {
1413 mc = container_of(id_priv->mc_list.next,
1414 struct cma_multicast, list);
1415 list_del(&mc->list);
Michael Wanga31ad3b2015-05-05 14:50:37 +02001416 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
Michael Wang5c9a5282015-05-05 14:50:29 +02001417 id_priv->id.port_num)) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02001418 ib_sa_free_multicast(mc->multicast.ib);
1419 kfree(mc);
Michael Wang5c9a5282015-05-05 14:50:29 +02001420 } else
Eli Cohen3c86aa72010-10-13 21:26:51 +02001421 kref_put(&mc->mcref, release_mc);
Sean Heftyc8f6a362007-02-15 17:00:18 -08001422 }
1423}
1424
Sean Heftye51060f2006-06-17 20:37:29 -07001425void rdma_destroy_id(struct rdma_cm_id *id)
1426{
1427 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001428 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -07001429
1430 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001431 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001432 cma_cancel_operation(id_priv, state);
1433
Sean Heftya396d432011-02-23 09:05:39 -08001434 /*
1435 * Wait for any active callback to finish. New callbacks will find
1436 * the id_priv state set to destroying and abort.
1437 */
1438 mutex_lock(&id_priv->handler_mutex);
1439 mutex_unlock(&id_priv->handler_mutex);
1440
Sean Heftye51060f2006-06-17 20:37:29 -07001441 if (id_priv->cma_dev) {
Michael Wang72219cea2015-05-05 14:50:34 +02001442 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001443 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07001444 ib_destroy_cm_id(id_priv->cm_id.ib);
Michael Wang04215332015-05-05 14:50:35 +02001445 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001446 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -05001447 iw_destroy_cm_id(id_priv->cm_id.iw);
Sean Heftye51060f2006-06-17 20:37:29 -07001448 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001449 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -08001450 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001451 }
1452
1453 cma_release_port(id_priv);
1454 cma_deref_id(id_priv);
1455 wait_for_completion(&id_priv->comp);
1456
Sean Heftyd02d1f52007-10-09 11:12:34 -07001457 if (id_priv->internal_id)
1458 cma_deref_id(id_priv->id.context);
1459
Sean Heftye51060f2006-06-17 20:37:29 -07001460 kfree(id_priv->id.route.path_rec);
1461 kfree(id_priv);
1462}
1463EXPORT_SYMBOL(rdma_destroy_id);
1464
1465static int cma_rep_recv(struct rdma_id_private *id_priv)
1466{
1467 int ret;
1468
Sean Hefty5851bb82008-01-04 10:47:12 -08001469 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001470 if (ret)
1471 goto reject;
1472
Sean Hefty5851bb82008-01-04 10:47:12 -08001473 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001474 if (ret)
1475 goto reject;
1476
1477 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1478 if (ret)
1479 goto reject;
1480
1481 return 0;
1482reject:
Sean Heftyc5483382007-09-24 13:19:09 -07001483 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001484 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1485 NULL, 0, NULL, 0);
1486 return ret;
1487}
1488
Sean Heftya1b1b612006-11-30 16:33:14 -08001489static void cma_set_rep_event_data(struct rdma_cm_event *event,
1490 struct ib_cm_rep_event_param *rep_data,
1491 void *private_data)
1492{
1493 event->param.conn.private_data = private_data;
1494 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1495 event->param.conn.responder_resources = rep_data->responder_resources;
1496 event->param.conn.initiator_depth = rep_data->initiator_depth;
1497 event->param.conn.flow_control = rep_data->flow_control;
1498 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1499 event->param.conn.srq = rep_data->srq;
1500 event->param.conn.qp_num = rep_data->remote_qpn;
1501}
1502
Sean Heftye51060f2006-06-17 20:37:29 -07001503static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1504{
1505 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001506 struct rdma_cm_event event;
1507 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001508
Amir Vadai38ca83a2008-07-22 14:14:23 -07001509 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001510 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001511 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001512 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -07001513 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001514
Sean Heftya1b1b612006-11-30 16:33:14 -08001515 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001516 switch (ib_event->event) {
1517 case IB_CM_REQ_ERROR:
1518 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001519 event.event = RDMA_CM_EVENT_UNREACHABLE;
1520 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001521 break;
1522 case IB_CM_REP_RECEIVED:
Sean Hefty01602f12013-05-29 10:09:20 -07001523 if (id_priv->id.qp) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001524 event.status = cma_rep_recv(id_priv);
1525 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1526 RDMA_CM_EVENT_ESTABLISHED;
Sean Hefty01602f12013-05-29 10:09:20 -07001527 } else {
Sean Heftya1b1b612006-11-30 16:33:14 -08001528 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
Sean Hefty01602f12013-05-29 10:09:20 -07001529 }
Sean Heftya1b1b612006-11-30 16:33:14 -08001530 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1531 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001532 break;
1533 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001534 case IB_CM_USER_ESTABLISHED:
1535 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001536 break;
1537 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001538 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001539 case IB_CM_DREQ_RECEIVED:
1540 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001541 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1542 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001543 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001544 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001545 break;
1546 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001547 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1548 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001549 case IB_CM_MRA_RECEIVED:
1550 /* ignore event */
1551 goto out;
1552 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001553 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001554 event.status = ib_event->param.rej_rcvd.reason;
1555 event.event = RDMA_CM_EVENT_REJECTED;
1556 event.param.conn.private_data = ib_event->private_data;
1557 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001558 break;
1559 default:
Roland Dreier468f2232008-07-14 23:48:47 -07001560 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001561 ib_event->event);
1562 goto out;
1563 }
1564
Sean Heftya1b1b612006-11-30 16:33:14 -08001565 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001566 if (ret) {
1567 /* Destroy the CM ID by returning a non-zero value. */
1568 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001569 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001570 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001571 rdma_destroy_id(&id_priv->id);
1572 return ret;
1573 }
1574out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001575 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001576 return ret;
1577}
1578
Sean Hefty628e5f62006-11-30 16:44:16 -08001579static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001580 struct ib_cm_event *ib_event,
1581 struct net_device *net_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001582{
1583 struct rdma_id_private *id_priv;
1584 struct rdma_cm_id *id;
1585 struct rdma_route *rt;
Haggai Eran0c505f72015-07-30 17:50:19 +03001586 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1587 const __be64 service_id =
1588 ib_event->param.req_rcvd.primary_path->service_id;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001589 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001590
Krishna Kumar3f168d22006-09-29 12:09:51 -07001591 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001592 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001593 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001594 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001595
Sean Heftyf4753832013-05-29 10:09:14 -07001596 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001597 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1598 (struct sockaddr *)&id->route.addr.dst_addr,
1599 listen_id, ib_event, ss_family, service_id))
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001600 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001601
1602 rt = &id->route;
1603 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1604 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1605 GFP_KERNEL);
1606 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001607 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001608
Sean Heftye51060f2006-06-17 20:37:29 -07001609 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1610 if (rt->num_paths == 2)
1611 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1612
Haggai Eran0b3ca762015-07-30 17:50:24 +03001613 if (net_dev) {
1614 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001615 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001616 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001617 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001618 if (!cma_protocol_roce(listen_id) &&
1619 cma_any_addr(cma_src_addr(id_priv))) {
1620 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1621 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1622 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1623 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
1624 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1625 if (ret)
1626 goto err;
1627 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001628 }
1629 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001630
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001631 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001632 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001633
Krishna Kumar3f168d22006-09-29 12:09:51 -07001634err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001635 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001636 return NULL;
1637}
1638
Sean Hefty628e5f62006-11-30 16:44:16 -08001639static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001640 struct ib_cm_event *ib_event,
1641 struct net_device *net_dev)
Sean Hefty628e5f62006-11-30 16:44:16 -08001642{
1643 struct rdma_id_private *id_priv;
1644 struct rdma_cm_id *id;
Haggai Eran0c505f72015-07-30 17:50:19 +03001645 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
Sean Hefty628e5f62006-11-30 16:44:16 -08001646 int ret;
1647
1648 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001649 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001650 if (IS_ERR(id))
1651 return NULL;
1652
Sean Heftyf4753832013-05-29 10:09:14 -07001653 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001654 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1655 (struct sockaddr *)&id->route.addr.dst_addr,
1656 listen_id, ib_event, ss_family,
1657 ib_event->param.sidr_req_rcvd.service_id))
Sean Hefty628e5f62006-11-30 16:44:16 -08001658 goto err;
1659
Haggai Eran0b3ca762015-07-30 17:50:24 +03001660 if (net_dev) {
1661 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001662 if (ret)
1663 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001664 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001665 if (!cma_any_addr(cma_src_addr(id_priv))) {
1666 ret = cma_translate_addr(cma_src_addr(id_priv),
1667 &id->route.addr.dev_addr);
1668 if (ret)
1669 goto err;
1670 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001671 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001672
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001673 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001674 return id_priv;
1675err:
1676 rdma_destroy_id(id);
1677 return NULL;
1678}
1679
Sean Heftya1b1b612006-11-30 16:33:14 -08001680static void cma_set_req_event_data(struct rdma_cm_event *event,
1681 struct ib_cm_req_event_param *req_data,
1682 void *private_data, int offset)
1683{
1684 event->param.conn.private_data = private_data + offset;
1685 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1686 event->param.conn.responder_resources = req_data->responder_resources;
1687 event->param.conn.initiator_depth = req_data->initiator_depth;
1688 event->param.conn.flow_control = req_data->flow_control;
1689 event->param.conn.retry_count = req_data->retry_count;
1690 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1691 event->param.conn.srq = req_data->srq;
1692 event->param.conn.qp_num = req_data->remote_qpn;
1693}
1694
Hefty, Sean95954802011-10-06 09:32:33 -07001695static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1696{
Sean Hefty4dd81e82012-06-14 20:49:09 +00001697 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
Hefty, Sean95954802011-10-06 09:32:33 -07001698 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1699 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1700 (id->qp_type == IB_QPT_UD)) ||
1701 (!id->qp_type));
1702}
1703
Sean Heftye51060f2006-06-17 20:37:29 -07001704static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1705{
1706 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001707 struct rdma_cm_event event;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001708 struct net_device *net_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07001709 int offset, ret;
1710
Haggai Eran0b3ca762015-07-30 17:50:24 +03001711 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001712 if (IS_ERR(listen_id))
1713 return PTR_ERR(listen_id);
1714
Haggai Eran0b3ca762015-07-30 17:50:24 +03001715 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1716 ret = -EINVAL;
1717 goto net_dev_put;
1718 }
Hefty, Sean95954802011-10-06 09:32:33 -07001719
Haggai Eran0b3ca762015-07-30 17:50:24 +03001720 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) {
1721 ret = -ECONNABORTED;
1722 goto net_dev_put;
1723 }
Sean Heftye51060f2006-06-17 20:37:29 -07001724
Sean Hefty628e5f62006-11-30 16:44:16 -08001725 memset(&event, 0, sizeof event);
Sean Heftye8160e12013-05-29 10:09:22 -07001726 offset = cma_user_data_offset(listen_id);
Sean Hefty628e5f62006-11-30 16:44:16 -08001727 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001728 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001729 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001730 event.param.ud.private_data = ib_event->private_data + offset;
1731 event.param.ud.private_data_len =
1732 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1733 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001734 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001735 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1736 ib_event->private_data, offset);
1737 }
Sean Heftye51060f2006-06-17 20:37:29 -07001738 if (!conn_id) {
1739 ret = -ENOMEM;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001740 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001741 }
1742
Or Gerlitzde910bd2008-07-14 23:48:53 -07001743 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001744 ret = cma_acquire_dev(conn_id, listen_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301745 if (ret)
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001746 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001747
1748 conn_id->cm_id.ib = cm_id;
1749 cm_id->context = conn_id;
1750 cm_id->cm_handler = cma_ib_handler;
1751
Sean Hefty25ae21a2011-02-23 08:11:32 -08001752 /*
1753 * Protect against the user destroying conn_id from another thread
1754 * until we're done accessing it.
1755 */
1756 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001757 ret = conn_id->id.event_handler(&conn_id->id, &event);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001758 if (ret)
1759 goto err3;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001760 /*
1761 * Acquire mutex to prevent user executing rdma_destroy_id()
1762 * while we're accessing the cm_id.
1763 */
1764 mutex_lock(&lock);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001765 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1766 (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001767 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1768 mutex_unlock(&lock);
1769 mutex_unlock(&conn_id->handler_mutex);
1770 mutex_unlock(&listen_id->handler_mutex);
1771 cma_deref_id(conn_id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001772 if (net_dev)
1773 dev_put(net_dev);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001774 return 0;
1775
1776err3:
1777 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301778 /* Destroy the CM ID by returning a non-zero value. */
1779 conn_id->cm_id.ib = NULL;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001780err2:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001781 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001782 mutex_unlock(&conn_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001783err1:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001784 mutex_unlock(&listen_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001785 if (conn_id)
1786 rdma_destroy_id(&conn_id->id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001787
1788net_dev_put:
1789 if (net_dev)
1790 dev_put(net_dev);
1791
Sean Heftye51060f2006-06-17 20:37:29 -07001792 return ret;
1793}
1794
Sean Heftycf539362013-05-29 10:09:28 -07001795__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
Sean Heftye51060f2006-06-17 20:37:29 -07001796{
Sean Hefty496ce3c2013-05-29 10:09:19 -07001797 if (addr->sa_family == AF_IB)
1798 return ((struct sockaddr_ib *) addr)->sib_sid;
1799
Sean Heftycf539362013-05-29 10:09:28 -07001800 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001801}
Sean Heftycf539362013-05-29 10:09:28 -07001802EXPORT_SYMBOL(rdma_get_service_id);
Sean Heftye51060f2006-06-17 20:37:29 -07001803
Tom Tucker07ebafb2006-08-03 16:02:42 -05001804static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1805{
1806 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001807 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001808 int ret = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05301809 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1810 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001811
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001812 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001813 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001814
Sean Heftybe65f082007-05-07 11:49:12 -07001815 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001816 switch (iw_event->event) {
1817 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001818 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001819 break;
1820 case IW_CM_EVENT_CONNECT_REPLY:
Steve Wise24d44a32013-07-04 16:10:44 +05301821 memcpy(cma_src_addr(id_priv), laddr,
1822 rdma_addr_size(laddr));
1823 memcpy(cma_dst_addr(id_priv), raddr,
1824 rdma_addr_size(raddr));
Steve Wise881a0452006-12-15 16:50:17 -06001825 switch (iw_event->status) {
1826 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001827 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301828 event.param.conn.initiator_depth = iw_event->ird;
1829 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06001830 break;
1831 case -ECONNRESET:
1832 case -ECONNREFUSED:
1833 event.event = RDMA_CM_EVENT_REJECTED;
1834 break;
1835 case -ETIMEDOUT:
1836 event.event = RDMA_CM_EVENT_UNREACHABLE;
1837 break;
1838 default:
1839 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1840 break;
1841 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001842 break;
1843 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001844 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301845 event.param.conn.initiator_depth = iw_event->ird;
1846 event.param.conn.responder_resources = iw_event->ord;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001847 break;
1848 default:
1849 BUG_ON(1);
1850 }
1851
Sean Heftya1b1b612006-11-30 16:33:14 -08001852 event.status = iw_event->status;
1853 event.param.conn.private_data = iw_event->private_data;
1854 event.param.conn.private_data_len = iw_event->private_data_len;
1855 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001856 if (ret) {
1857 /* Destroy the CM ID by returning a non-zero value. */
1858 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001859 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001860 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001861 rdma_destroy_id(&id_priv->id);
1862 return ret;
1863 }
1864
Or Gerlitzde910bd2008-07-14 23:48:53 -07001865 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001866 return ret;
1867}
1868
1869static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1870 struct iw_cm_event *iw_event)
1871{
1872 struct rdma_cm_id *new_cm_id;
1873 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001874 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001875 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001876 struct ib_device_attr attr;
Steve Wise24d44a32013-07-04 16:10:44 +05301877 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1878 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001879
1880 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001881 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001882 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001883
1884 /* Create a new RDMA id for the new IW CM ID */
1885 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1886 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001887 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07001888 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001889 ret = -ENOMEM;
1890 goto out;
1891 }
1892 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001893 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001894 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001895
Matan Barakdd5f03b2013-12-12 18:03:11 +02001896 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001897 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001898 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001899 rdma_destroy_id(new_cm_id);
1900 goto out;
1901 }
1902
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001903 ret = cma_acquire_dev(conn_id, listen_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001904 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001905 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001906 rdma_destroy_id(new_cm_id);
1907 goto out;
1908 }
1909
1910 conn_id->cm_id.iw = cm_id;
1911 cm_id->context = conn_id;
1912 cm_id->cm_handler = cma_iw_handler;
1913
Steve Wise24d44a32013-07-04 16:10:44 +05301914 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
1915 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
Tom Tucker07ebafb2006-08-03 16:02:42 -05001916
Steve Wise8d8293c2007-10-29 11:34:05 -05001917 ret = ib_query_device(conn_id->id.device, &attr);
1918 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001919 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001920 rdma_destroy_id(new_cm_id);
1921 goto out;
1922 }
1923
Sean Heftya1b1b612006-11-30 16:33:14 -08001924 memset(&event, 0, sizeof event);
1925 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1926 event.param.conn.private_data = iw_event->private_data;
1927 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301928 event.param.conn.initiator_depth = iw_event->ird;
1929 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08001930
1931 /*
1932 * Protect against the user destroying conn_id from another thread
1933 * until we're done accessing it.
1934 */
1935 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001936 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001937 if (ret) {
1938 /* User wants to destroy the CM ID */
1939 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001940 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001941 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001942 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001943 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001944 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001945 }
1946
Or Gerlitzde910bd2008-07-14 23:48:53 -07001947 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001948 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001949
Tom Tucker07ebafb2006-08-03 16:02:42 -05001950out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001951 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001952 return ret;
1953}
1954
Sean Heftye51060f2006-06-17 20:37:29 -07001955static int cma_ib_listen(struct rdma_id_private *id_priv)
1956{
Sean Heftye51060f2006-06-17 20:37:29 -07001957 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001958 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07001959 __be64 svc_id;
Sean Heftye51060f2006-06-17 20:37:29 -07001960
Sean Heftyf4753832013-05-29 10:09:14 -07001961 addr = cma_src_addr(id_priv);
Sean Heftycf539362013-05-29 10:09:28 -07001962 svc_id = rdma_get_service_id(&id_priv->id, addr);
Haggai Eran51efe392015-07-30 17:50:25 +03001963 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
1964 if (IS_ERR(id))
1965 return PTR_ERR(id);
1966 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07001967
Haggai Eran51efe392015-07-30 17:50:25 +03001968 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001969}
1970
Tom Tucker07ebafb2006-08-03 16:02:42 -05001971static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1972{
1973 int ret;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001974 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001975
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001976 id = iw_create_cm_id(id_priv->id.device,
1977 iw_conn_req_handler,
1978 id_priv);
1979 if (IS_ERR(id))
1980 return PTR_ERR(id);
1981
Steve Wise68cdba02015-05-18 15:27:10 -05001982 id->tos = id_priv->tos;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001983 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001984
Steve Wise24d44a32013-07-04 16:10:44 +05301985 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
1986 rdma_addr_size(cma_src_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05001987
1988 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1989
1990 if (ret) {
1991 iw_destroy_cm_id(id_priv->cm_id.iw);
1992 id_priv->cm_id.iw = NULL;
1993 }
1994
1995 return ret;
1996}
1997
Sean Heftye51060f2006-06-17 20:37:29 -07001998static int cma_listen_handler(struct rdma_cm_id *id,
1999 struct rdma_cm_event *event)
2000{
2001 struct rdma_id_private *id_priv = id->context;
2002
2003 id->context = id_priv->id.context;
2004 id->event_handler = id_priv->id.event_handler;
2005 return id_priv->id.event_handler(id, event);
2006}
2007
2008static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2009 struct cma_device *cma_dev)
2010{
2011 struct rdma_id_private *dev_id_priv;
2012 struct rdma_cm_id *id;
2013 int ret;
2014
Michael Wang72219cea2015-05-05 14:50:34 +02002015 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
Sean Hefty94d0c932013-05-29 10:09:24 -07002016 return;
2017
Sean Heftyb26f9b92010-04-01 17:08:41 +00002018 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
2019 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07002020 if (IS_ERR(id))
2021 return;
2022
2023 dev_id_priv = container_of(id, struct rdma_id_private, id);
2024
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002025 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftyf4753832013-05-29 10:09:14 -07002026 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2027 rdma_addr_size(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002028
2029 cma_attach_to_dev(dev_id_priv, cma_dev);
2030 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07002031 atomic_inc(&id_priv->refcount);
2032 dev_id_priv->internal_id = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002033 dev_id_priv->afonly = id_priv->afonly;
Sean Heftye51060f2006-06-17 20:37:29 -07002034
2035 ret = rdma_listen(id, id_priv->backlog);
2036 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07002037 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07002038 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07002039}
2040
2041static void cma_listen_on_all(struct rdma_id_private *id_priv)
2042{
2043 struct cma_device *cma_dev;
2044
2045 mutex_lock(&lock);
2046 list_add_tail(&id_priv->list, &listen_any_list);
2047 list_for_each_entry(cma_dev, &dev_list, list)
2048 cma_listen_on_dev(id_priv, cma_dev);
2049 mutex_unlock(&lock);
2050}
2051
Sean Heftya81c9942007-08-08 15:51:06 -07002052void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2053{
2054 struct rdma_id_private *id_priv;
2055
2056 id_priv = container_of(id, struct rdma_id_private, id);
2057 id_priv->tos = (u8) tos;
2058}
2059EXPORT_SYMBOL(rdma_set_service_type);
2060
Sean Heftye51060f2006-06-17 20:37:29 -07002061static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2062 void *context)
2063{
2064 struct cma_work *work = context;
2065 struct rdma_route *route;
2066
2067 route = &work->id->id.route;
2068
2069 if (!status) {
2070 route->num_paths = 1;
2071 *route->path_rec = *path_rec;
2072 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002073 work->old_state = RDMA_CM_ROUTE_QUERY;
2074 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002075 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07002076 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07002077 }
2078
2079 queue_work(cma_wq, &work->work);
2080}
2081
2082static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2083 struct cma_work *work)
2084{
Sean Heftyf4753832013-05-29 10:09:14 -07002085 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002086 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07002087 ib_sa_comp_mask comp_mask;
2088 struct sockaddr_in6 *sin6;
Sean Heftyf68194c2013-05-29 10:09:18 -07002089 struct sockaddr_ib *sib;
Sean Heftye51060f2006-06-17 20:37:29 -07002090
2091 memset(&path_rec, 0, sizeof path_rec);
Sean Heftyf4753832013-05-29 10:09:14 -07002092 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2093 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2094 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07002095 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08002096 path_rec.reversible = 1;
Sean Heftycf539362013-05-29 10:09:28 -07002097 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftya81c9942007-08-08 15:51:06 -07002098
2099 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2100 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2101 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2102
Sean Heftyf68194c2013-05-29 10:09:18 -07002103 switch (cma_family(id_priv)) {
2104 case AF_INET:
Sean Heftya81c9942007-08-08 15:51:06 -07002105 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2106 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002107 break;
2108 case AF_INET6:
Sean Heftyf4753832013-05-29 10:09:14 -07002109 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
Sean Heftya81c9942007-08-08 15:51:06 -07002110 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2111 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002112 break;
2113 case AF_IB:
2114 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2115 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2116 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2117 break;
Sean Heftya81c9942007-08-08 15:51:06 -07002118 }
Sean Heftye51060f2006-06-17 20:37:29 -07002119
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002120 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07002121 id_priv->id.port_num, &path_rec,
2122 comp_mask, timeout_ms,
2123 GFP_KERNEL, cma_query_handler,
2124 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07002125
2126 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2127}
2128
David Howellsc4028952006-11-22 14:57:56 +00002129static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07002130{
David Howellsc4028952006-11-22 14:57:56 +00002131 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07002132 struct rdma_id_private *id_priv = work->id;
2133 int destroy = 0;
2134
Or Gerlitzde910bd2008-07-14 23:48:53 -07002135 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002136 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2137 goto out;
2138
2139 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002140 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07002141 destroy = 1;
2142 }
2143out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002144 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002145 cma_deref_id(id_priv);
2146 if (destroy)
2147 rdma_destroy_id(&id_priv->id);
2148 kfree(work);
2149}
2150
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002151static void cma_ndev_work_handler(struct work_struct *_work)
2152{
2153 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2154 struct rdma_id_private *id_priv = work->id;
2155 int destroy = 0;
2156
2157 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002158 if (id_priv->state == RDMA_CM_DESTROYING ||
2159 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002160 goto out;
2161
2162 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002163 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002164 destroy = 1;
2165 }
2166
2167out:
2168 mutex_unlock(&id_priv->handler_mutex);
2169 cma_deref_id(id_priv);
2170 if (destroy)
2171 rdma_destroy_id(&id_priv->id);
2172 kfree(work);
2173}
2174
Sean Heftye51060f2006-06-17 20:37:29 -07002175static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2176{
2177 struct rdma_route *route = &id_priv->id.route;
2178 struct cma_work *work;
2179 int ret;
2180
2181 work = kzalloc(sizeof *work, GFP_KERNEL);
2182 if (!work)
2183 return -ENOMEM;
2184
2185 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002186 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002187 work->old_state = RDMA_CM_ROUTE_QUERY;
2188 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002189 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2190
2191 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2192 if (!route->path_rec) {
2193 ret = -ENOMEM;
2194 goto err1;
2195 }
2196
2197 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2198 if (ret)
2199 goto err2;
2200
2201 return 0;
2202err2:
2203 kfree(route->path_rec);
2204 route->path_rec = NULL;
2205err1:
2206 kfree(work);
2207 return ret;
2208}
2209
2210int rdma_set_ib_paths(struct rdma_cm_id *id,
2211 struct ib_sa_path_rec *path_rec, int num_paths)
2212{
2213 struct rdma_id_private *id_priv;
2214 int ret;
2215
2216 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002217 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2218 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07002219 return -EINVAL;
2220
Julia Lawall9893e742010-05-15 23:22:38 +02002221 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2222 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002223 if (!id->route.path_rec) {
2224 ret = -ENOMEM;
2225 goto err;
2226 }
2227
Sean Heftyae2d9292010-03-25 19:12:36 +00002228 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07002229 return 0;
2230err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002231 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002232 return ret;
2233}
2234EXPORT_SYMBOL(rdma_set_ib_paths);
2235
Tom Tucker07ebafb2006-08-03 16:02:42 -05002236static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2237{
2238 struct cma_work *work;
2239
2240 work = kzalloc(sizeof *work, GFP_KERNEL);
2241 if (!work)
2242 return -ENOMEM;
2243
2244 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002245 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002246 work->old_state = RDMA_CM_ROUTE_QUERY;
2247 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002248 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2249 queue_work(cma_wq, &work->work);
2250 return 0;
2251}
2252
Eyal Perryeb072c42013-11-06 15:37:24 +02002253static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2254{
2255 int prio;
2256 struct net_device *dev;
2257
2258 prio = rt_tos2priority(tos);
2259 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
2260 vlan_dev_real_dev(ndev) : ndev;
2261
2262 if (dev->num_tc)
2263 return netdev_get_prio_tc_map(dev, prio);
2264
2265#if IS_ENABLED(CONFIG_VLAN_8021Q)
2266 if (ndev->priv_flags & IFF_802_1Q_VLAN)
2267 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2268 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2269#endif
2270 return 0;
2271}
2272
Eli Cohen3c86aa72010-10-13 21:26:51 +02002273static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2274{
2275 struct rdma_route *route = &id_priv->id.route;
2276 struct rdma_addr *addr = &route->addr;
2277 struct cma_work *work;
2278 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002279 struct net_device *ndev = NULL;
Matan Barakdd5f03b2013-12-12 18:03:11 +02002280
Eli Cohen3c86aa72010-10-13 21:26:51 +02002281
Eli Cohen3c86aa72010-10-13 21:26:51 +02002282 work = kzalloc(sizeof *work, GFP_KERNEL);
2283 if (!work)
2284 return -ENOMEM;
2285
2286 work->id = id_priv;
2287 INIT_WORK(&work->work, cma_work_handler);
2288
2289 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2290 if (!route->path_rec) {
2291 ret = -ENOMEM;
2292 goto err1;
2293 }
2294
2295 route->num_paths = 1;
2296
Eli Cohen3c86aa72010-10-13 21:26:51 +02002297 if (addr->dev_addr.bound_dev_if)
2298 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
2299 if (!ndev) {
2300 ret = -ENODEV;
2301 goto err2;
2302 }
2303
Matan Barakdd5f03b2013-12-12 18:03:11 +02002304 route->path_rec->vlan_id = rdma_vlan_dev_vlan_id(ndev);
2305 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
2306 memcpy(route->path_rec->smac, ndev->dev_addr, ndev->addr_len);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002307
Moni Shoua7b856272013-12-12 18:03:12 +02002308 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2309 &route->path_rec->sgid);
2310 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2311 &route->path_rec->dgid);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002312
2313 route->path_rec->hop_limit = 1;
2314 route->path_rec->reversible = 1;
2315 route->path_rec->pkey = cpu_to_be16(0xffff);
2316 route->path_rec->mtu_selector = IB_SA_EQ;
Eyal Perryeb072c42013-11-06 15:37:24 +02002317 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
Eli Cohen3c86aa72010-10-13 21:26:51 +02002318 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2319 route->path_rec->rate_selector = IB_SA_EQ;
2320 route->path_rec->rate = iboe_get_rate(ndev);
2321 dev_put(ndev);
2322 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2323 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2324 if (!route->path_rec->mtu) {
2325 ret = -EINVAL;
2326 goto err2;
2327 }
2328
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002329 work->old_state = RDMA_CM_ROUTE_QUERY;
2330 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002331 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2332 work->event.status = 0;
2333
2334 queue_work(cma_wq, &work->work);
2335
2336 return 0;
2337
2338err2:
2339 kfree(route->path_rec);
2340 route->path_rec = NULL;
2341err1:
2342 kfree(work);
2343 return ret;
2344}
2345
Sean Heftye51060f2006-06-17 20:37:29 -07002346int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2347{
2348 struct rdma_id_private *id_priv;
2349 int ret;
2350
2351 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002352 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002353 return -EINVAL;
2354
2355 atomic_inc(&id_priv->refcount);
Michael Wangfe53ba22015-05-05 14:50:36 +02002356 if (rdma_cap_ib_sa(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002357 ret = cma_resolve_ib_route(id_priv, timeout_ms);
Ira Weiny5d9fb042015-05-14 15:01:46 -04002358 else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002359 ret = cma_resolve_iboe_route(id_priv);
2360 else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002361 ret = cma_resolve_iw_route(id_priv, timeout_ms);
Michael Wangc72f2182015-05-05 14:50:28 +02002362 else
Sean Heftye51060f2006-06-17 20:37:29 -07002363 ret = -ENOSYS;
Michael Wangc72f2182015-05-05 14:50:28 +02002364
Sean Heftye51060f2006-06-17 20:37:29 -07002365 if (ret)
2366 goto err;
2367
2368 return 0;
2369err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002370 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002371 cma_deref_id(id_priv);
2372 return ret;
2373}
2374EXPORT_SYMBOL(rdma_resolve_route);
2375
Sean Hefty6a3e3622013-05-29 10:09:13 -07002376static void cma_set_loopback(struct sockaddr *addr)
2377{
2378 switch (addr->sa_family) {
2379 case AF_INET:
2380 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2381 break;
2382 case AF_INET6:
2383 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2384 0, 0, 0, htonl(1));
2385 break;
2386 default:
2387 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2388 0, 0, 0, htonl(1));
2389 break;
2390 }
2391}
2392
Sean Heftye51060f2006-06-17 20:37:29 -07002393static int cma_bind_loopback(struct rdma_id_private *id_priv)
2394{
Sean Heftyb0569e42013-05-29 10:09:15 -07002395 struct cma_device *cma_dev, *cur_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07002396 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002397 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002398 u16 pkey;
2399 int ret;
2400 u8 p;
2401
Sean Heftyb0569e42013-05-29 10:09:15 -07002402 cma_dev = NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07002403 mutex_lock(&lock);
Sean Heftyb0569e42013-05-29 10:09:15 -07002404 list_for_each_entry(cur_dev, &dev_list, list) {
2405 if (cma_family(id_priv) == AF_IB &&
Michael Wang72219cea2015-05-05 14:50:34 +02002406 !rdma_cap_ib_cm(cur_dev->device, 1))
Sean Heftyb0569e42013-05-29 10:09:15 -07002407 continue;
2408
2409 if (!cma_dev)
2410 cma_dev = cur_dev;
2411
2412 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2413 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2414 port_attr.state == IB_PORT_ACTIVE) {
2415 cma_dev = cur_dev;
2416 goto port_found;
2417 }
2418 }
2419 }
2420
2421 if (!cma_dev) {
Sean Heftye51060f2006-06-17 20:37:29 -07002422 ret = -ENODEV;
2423 goto out;
2424 }
Krishna Kumare82153b2006-10-16 10:09:01 +05302425
2426 p = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07002427
2428port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002429 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002430 if (ret)
2431 goto out;
2432
2433 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2434 if (ret)
2435 goto out;
2436
Sean Hefty6f8372b2009-11-19 13:26:06 -08002437 id_priv->id.route.addr.dev_addr.dev_type =
Michael Wang21655af2015-05-05 14:50:27 +02002438 (rdma_protocol_ib(cma_dev->device, p)) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08002439 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2440
2441 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002442 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2443 id_priv->id.port_num = p;
2444 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf4753832013-05-29 10:09:14 -07002445 cma_set_loopback(cma_src_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002446out:
2447 mutex_unlock(&lock);
2448 return ret;
2449}
2450
2451static void addr_handler(int status, struct sockaddr *src_addr,
2452 struct rdma_dev_addr *dev_addr, void *context)
2453{
2454 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08002455 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002456
Sean Heftya1b1b612006-11-30 16:33:14 -08002457 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002458 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002459 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2460 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07002461 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07002462
Moni Shoua7b856272013-12-12 18:03:12 +02002463 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
Sean Hefty61a73c72006-09-01 15:33:55 -07002464 if (!status && !id_priv->cma_dev)
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002465 status = cma_acquire_dev(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002466
2467 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002468 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2469 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002470 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08002471 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2472 event.status = status;
Moni Shoua7b856272013-12-12 18:03:12 +02002473 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08002474 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002475
Sean Heftya1b1b612006-11-30 16:33:14 -08002476 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002477 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002478 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002479 cma_deref_id(id_priv);
2480 rdma_destroy_id(&id_priv->id);
2481 return;
2482 }
2483out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002484 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002485 cma_deref_id(id_priv);
2486}
2487
2488static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2489{
2490 struct cma_work *work;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002491 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002492 int ret;
2493
2494 work = kzalloc(sizeof *work, GFP_KERNEL);
2495 if (!work)
2496 return -ENOMEM;
2497
2498 if (!id_priv->cma_dev) {
2499 ret = cma_bind_loopback(id_priv);
2500 if (ret)
2501 goto err;
2502 }
2503
Sean Hefty6f8372b2009-11-19 13:26:06 -08002504 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2505 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002506
Sean Heftye51060f2006-06-17 20:37:29 -07002507 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002508 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002509 work->old_state = RDMA_CM_ADDR_QUERY;
2510 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002511 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2512 queue_work(cma_wq, &work->work);
2513 return 0;
2514err:
2515 kfree(work);
2516 return ret;
2517}
2518
Sean Heftyf17df3b2013-05-29 10:09:17 -07002519static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2520{
2521 struct cma_work *work;
2522 int ret;
2523
2524 work = kzalloc(sizeof *work, GFP_KERNEL);
2525 if (!work)
2526 return -ENOMEM;
2527
2528 if (!id_priv->cma_dev) {
2529 ret = cma_resolve_ib_dev(id_priv);
2530 if (ret)
2531 goto err;
2532 }
2533
2534 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2535 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2536
2537 work->id = id_priv;
2538 INIT_WORK(&work->work, cma_work_handler);
2539 work->old_state = RDMA_CM_ADDR_QUERY;
2540 work->new_state = RDMA_CM_ADDR_RESOLVED;
2541 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2542 queue_work(cma_wq, &work->work);
2543 return 0;
2544err:
2545 kfree(work);
2546 return ret;
2547}
2548
Sean Heftye51060f2006-06-17 20:37:29 -07002549static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2550 struct sockaddr *dst_addr)
2551{
Sean Heftyd14714d2009-11-19 16:46:25 -08002552 if (!src_addr || !src_addr->sa_family) {
2553 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002554 src_addr->sa_family = dst_addr->sa_family;
2555 if (dst_addr->sa_family == AF_INET6) {
Spencer Baugh6c26a772015-08-13 12:19:10 -07002556 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2557 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2558 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2559 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2560 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002561 } else if (dst_addr->sa_family == AF_IB) {
2562 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2563 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
Sean Heftyd14714d2009-11-19 16:46:25 -08002564 }
2565 }
2566 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002567}
2568
2569int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2570 struct sockaddr *dst_addr, int timeout_ms)
2571{
2572 struct rdma_id_private *id_priv;
2573 int ret;
2574
2575 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002576 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002577 ret = cma_bind_addr(id, src_addr, dst_addr);
2578 if (ret)
2579 return ret;
2580 }
2581
Sean Hefty4ae71522013-05-29 10:09:16 -07002582 if (cma_family(id_priv) != dst_addr->sa_family)
2583 return -EINVAL;
2584
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002585 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002586 return -EINVAL;
2587
2588 atomic_inc(&id_priv->refcount);
Sean Heftyf4753832013-05-29 10:09:14 -07002589 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
Sean Heftyf17df3b2013-05-29 10:09:17 -07002590 if (cma_any_addr(dst_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002591 ret = cma_resolve_loopback(id_priv);
Sean Heftyf17df3b2013-05-29 10:09:17 -07002592 } else {
2593 if (dst_addr->sa_family == AF_IB) {
2594 ret = cma_resolve_ib_addr(id_priv);
2595 } else {
2596 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2597 dst_addr, &id->route.addr.dev_addr,
2598 timeout_ms, addr_handler, id_priv);
2599 }
2600 }
Sean Heftye51060f2006-06-17 20:37:29 -07002601 if (ret)
2602 goto err;
2603
2604 return 0;
2605err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002606 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002607 cma_deref_id(id_priv);
2608 return ret;
2609}
2610EXPORT_SYMBOL(rdma_resolve_addr);
2611
Hefty, Seana9bb7912011-05-09 22:06:10 -07002612int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2613{
2614 struct rdma_id_private *id_priv;
2615 unsigned long flags;
2616 int ret;
2617
2618 id_priv = container_of(id, struct rdma_id_private, id);
2619 spin_lock_irqsave(&id_priv->lock, flags);
Sean Heftyc8dea2f92013-05-29 10:09:08 -07002620 if (reuse || id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002621 id_priv->reuseaddr = reuse;
2622 ret = 0;
2623 } else {
2624 ret = -EINVAL;
2625 }
2626 spin_unlock_irqrestore(&id_priv->lock, flags);
2627 return ret;
2628}
2629EXPORT_SYMBOL(rdma_set_reuseaddr);
2630
Sean Hefty68602122012-06-14 20:31:39 +00002631int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2632{
2633 struct rdma_id_private *id_priv;
2634 unsigned long flags;
2635 int ret;
2636
2637 id_priv = container_of(id, struct rdma_id_private, id);
2638 spin_lock_irqsave(&id_priv->lock, flags);
2639 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2640 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2641 id_priv->afonly = afonly;
2642 ret = 0;
2643 } else {
2644 ret = -EINVAL;
2645 }
2646 spin_unlock_irqrestore(&id_priv->lock, flags);
2647 return ret;
2648}
2649EXPORT_SYMBOL(rdma_set_afonly);
2650
Sean Heftye51060f2006-06-17 20:37:29 -07002651static void cma_bind_port(struct rdma_bind_list *bind_list,
2652 struct rdma_id_private *id_priv)
2653{
Sean Hefty58afdcb2013-05-29 10:09:11 -07002654 struct sockaddr *addr;
2655 struct sockaddr_ib *sib;
2656 u64 sid, mask;
2657 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07002658
Sean Heftyf4753832013-05-29 10:09:14 -07002659 addr = cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002660 port = htons(bind_list->port);
2661
2662 switch (addr->sa_family) {
2663 case AF_INET:
2664 ((struct sockaddr_in *) addr)->sin_port = port;
2665 break;
2666 case AF_INET6:
2667 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2668 break;
2669 case AF_IB:
2670 sib = (struct sockaddr_ib *) addr;
2671 sid = be64_to_cpu(sib->sib_sid);
2672 mask = be64_to_cpu(sib->sib_sid_mask);
2673 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2674 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2675 break;
2676 }
Sean Heftye51060f2006-06-17 20:37:29 -07002677 id_priv->bind_list = bind_list;
2678 hlist_add_head(&id_priv->node, &bind_list->owners);
2679}
2680
Haggai Eranaac978e2015-07-30 17:50:20 +03002681static int cma_alloc_port(enum rdma_port_space ps,
2682 struct rdma_id_private *id_priv, unsigned short snum)
Sean Heftye51060f2006-06-17 20:37:29 -07002683{
2684 struct rdma_bind_list *bind_list;
Tejun Heo3b069c52013-02-27 17:04:16 -08002685 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002686
Sean Heftycb164b82007-03-05 12:50:17 -08002687 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002688 if (!bind_list)
2689 return -ENOMEM;
2690
Haggai Eranaac978e2015-07-30 17:50:20 +03002691 ret = cma_ps_alloc(ps, bind_list, snum);
Tejun Heo3b069c52013-02-27 17:04:16 -08002692 if (ret < 0)
2693 goto err;
Sean Heftye51060f2006-06-17 20:37:29 -07002694
2695 bind_list->ps = ps;
Tejun Heo3b069c52013-02-27 17:04:16 -08002696 bind_list->port = (unsigned short)ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002697 cma_bind_port(bind_list, id_priv);
2698 return 0;
Tejun Heo3b069c52013-02-27 17:04:16 -08002699err:
Sean Heftyaedec082007-01-29 16:41:23 -08002700 kfree(bind_list);
Tejun Heo3b069c52013-02-27 17:04:16 -08002701 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002702}
2703
Haggai Eranaac978e2015-07-30 17:50:20 +03002704static int cma_alloc_any_port(enum rdma_port_space ps,
2705 struct rdma_id_private *id_priv)
Sean Heftyaedec082007-01-29 16:41:23 -08002706{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002707 static unsigned int last_used_port;
2708 int low, high, remaining;
2709 unsigned int rover;
Sean Heftyaedec082007-01-29 16:41:23 -08002710
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07002711 inet_get_local_port_range(&init_net, &low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002712 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002713 rover = prandom_u32() % remaining + low;
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002714retry:
2715 if (last_used_port != rover &&
Haggai Eranaac978e2015-07-30 17:50:20 +03002716 !cma_ps_find(ps, (unsigned short)rover)) {
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002717 int ret = cma_alloc_port(ps, id_priv, rover);
2718 /*
2719 * Remember previously used port number in order to avoid
2720 * re-using same port immediately after it is closed.
2721 */
2722 if (!ret)
2723 last_used_port = rover;
2724 if (ret != -EADDRNOTAVAIL)
2725 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002726 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002727 if (--remaining) {
2728 rover++;
2729 if ((rover < low) || (rover > high))
2730 rover = low;
2731 goto retry;
2732 }
2733 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002734}
2735
Hefty, Seana9bb7912011-05-09 22:06:10 -07002736/*
2737 * Check that the requested port is available. This is called when trying to
2738 * bind to a specific port, or when trying to listen on a bound port. In
2739 * the latter case, the provided id_priv may already be on the bind_list, but
2740 * we still need to check that it's okay to start listening.
2741 */
2742static int cma_check_port(struct rdma_bind_list *bind_list,
2743 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002744{
2745 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002746 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002747
Sean Heftyf4753832013-05-29 10:09:14 -07002748 addr = cma_src_addr(id_priv);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002749 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002750 if (id_priv == cur_id)
2751 continue;
2752
Sean Hefty5b0ec992012-06-14 20:31:39 +00002753 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2754 cur_id->reuseaddr)
2755 continue;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002756
Sean Heftyf4753832013-05-29 10:09:14 -07002757 cur_addr = cma_src_addr(cur_id);
Sean Hefty5b0ec992012-06-14 20:31:39 +00002758 if (id_priv->afonly && cur_id->afonly &&
2759 (addr->sa_family != cur_addr->sa_family))
2760 continue;
2761
2762 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2763 return -EADDRNOTAVAIL;
2764
2765 if (!cma_addr_cmp(addr, cur_addr))
2766 return -EADDRINUSE;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002767 }
2768 return 0;
2769}
2770
Haggai Eranaac978e2015-07-30 17:50:20 +03002771static int cma_use_port(enum rdma_port_space ps,
2772 struct rdma_id_private *id_priv)
Hefty, Seana9bb7912011-05-09 22:06:10 -07002773{
2774 struct rdma_bind_list *bind_list;
2775 unsigned short snum;
2776 int ret;
2777
Sean Heftyf4753832013-05-29 10:09:14 -07002778 snum = ntohs(cma_port(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002779 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2780 return -EACCES;
2781
Haggai Eranaac978e2015-07-30 17:50:20 +03002782 bind_list = cma_ps_find(ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002783 if (!bind_list) {
2784 ret = cma_alloc_port(ps, id_priv, snum);
2785 } else {
2786 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2787 if (!ret)
2788 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002789 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07002790 return ret;
2791}
Sean Heftye51060f2006-06-17 20:37:29 -07002792
Hefty, Seana9bb7912011-05-09 22:06:10 -07002793static int cma_bind_listen(struct rdma_id_private *id_priv)
2794{
2795 struct rdma_bind_list *bind_list = id_priv->bind_list;
2796 int ret = 0;
2797
2798 mutex_lock(&lock);
2799 if (bind_list->owners.first->next)
2800 ret = cma_check_port(bind_list, id_priv, 0);
2801 mutex_unlock(&lock);
2802 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002803}
2804
Haggai Eranaac978e2015-07-30 17:50:20 +03002805static enum rdma_port_space cma_select_inet_ps(
2806 struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002807{
2808 switch (id_priv->id.ps) {
Sean Hefty58afdcb2013-05-29 10:09:11 -07002809 case RDMA_PS_TCP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002810 case RDMA_PS_UDP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002811 case RDMA_PS_IPOIB:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002812 case RDMA_PS_IB:
Haggai Eranaac978e2015-07-30 17:50:20 +03002813 return id_priv->id.ps;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002814 default:
Haggai Eranaac978e2015-07-30 17:50:20 +03002815
2816 return 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002817 }
2818}
2819
Haggai Eranaac978e2015-07-30 17:50:20 +03002820static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002821{
Haggai Eranaac978e2015-07-30 17:50:20 +03002822 enum rdma_port_space ps = 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002823 struct sockaddr_ib *sib;
2824 u64 sid_ps, mask, sid;
2825
Sean Heftyf4753832013-05-29 10:09:14 -07002826 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002827 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
2828 sid = be64_to_cpu(sib->sib_sid) & mask;
2829
2830 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
2831 sid_ps = RDMA_IB_IP_PS_IB;
Haggai Eranaac978e2015-07-30 17:50:20 +03002832 ps = RDMA_PS_IB;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002833 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
2834 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
2835 sid_ps = RDMA_IB_IP_PS_TCP;
Haggai Eranaac978e2015-07-30 17:50:20 +03002836 ps = RDMA_PS_TCP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002837 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
2838 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
2839 sid_ps = RDMA_IB_IP_PS_UDP;
Haggai Eranaac978e2015-07-30 17:50:20 +03002840 ps = RDMA_PS_UDP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07002841 }
2842
2843 if (ps) {
2844 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
2845 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
2846 be64_to_cpu(sib->sib_sid_mask));
2847 }
2848 return ps;
2849}
2850
Sean Heftye51060f2006-06-17 20:37:29 -07002851static int cma_get_port(struct rdma_id_private *id_priv)
2852{
Haggai Eranaac978e2015-07-30 17:50:20 +03002853 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -07002854 int ret;
2855
Sean Heftyf4753832013-05-29 10:09:14 -07002856 if (cma_family(id_priv) != AF_IB)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002857 ps = cma_select_inet_ps(id_priv);
2858 else
2859 ps = cma_select_ib_ps(id_priv);
2860 if (!ps)
Sean Heftye51060f2006-06-17 20:37:29 -07002861 return -EPROTONOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07002862
2863 mutex_lock(&lock);
Sean Heftyf4753832013-05-29 10:09:14 -07002864 if (cma_any_port(cma_src_addr(id_priv)))
Sean Heftyaedec082007-01-29 16:41:23 -08002865 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002866 else
2867 ret = cma_use_port(ps, id_priv);
2868 mutex_unlock(&lock);
2869
2870 return ret;
2871}
2872
Sean Heftyd14714d2009-11-19 16:46:25 -08002873static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2874 struct sockaddr *addr)
2875{
Roland Dreierd90f9b32012-07-05 22:39:34 -07002876#if IS_ENABLED(CONFIG_IPV6)
Sean Heftyd14714d2009-11-19 16:46:25 -08002877 struct sockaddr_in6 *sin6;
2878
2879 if (addr->sa_family != AF_INET6)
2880 return 0;
2881
2882 sin6 = (struct sockaddr_in6 *) addr;
Somnath Kotur5462edd2013-11-16 09:30:01 +05302883
2884 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
2885 return 0;
2886
2887 if (!sin6->sin6_scope_id)
Sean Heftyd14714d2009-11-19 16:46:25 -08002888 return -EINVAL;
2889
2890 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2891#endif
2892 return 0;
2893}
2894
Hefty, Seana9bb7912011-05-09 22:06:10 -07002895int rdma_listen(struct rdma_cm_id *id, int backlog)
2896{
2897 struct rdma_id_private *id_priv;
2898 int ret;
2899
2900 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002901 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftyf4753832013-05-29 10:09:14 -07002902 id->route.addr.src_addr.ss_family = AF_INET;
2903 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
Hefty, Seana9bb7912011-05-09 22:06:10 -07002904 if (ret)
2905 return ret;
2906 }
2907
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002908 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07002909 return -EINVAL;
2910
2911 if (id_priv->reuseaddr) {
2912 ret = cma_bind_listen(id_priv);
2913 if (ret)
2914 goto err;
2915 }
2916
2917 id_priv->backlog = backlog;
2918 if (id->device) {
Michael Wang72219cea2015-05-05 14:50:34 +02002919 if (rdma_cap_ib_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002920 ret = cma_ib_listen(id_priv);
2921 if (ret)
2922 goto err;
Michael Wang04215332015-05-05 14:50:35 +02002923 } else if (rdma_cap_iw_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002924 ret = cma_iw_listen(id_priv, backlog);
2925 if (ret)
2926 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02002927 } else {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002928 ret = -ENOSYS;
2929 goto err;
2930 }
2931 } else
2932 cma_listen_on_all(id_priv);
2933
2934 return 0;
2935err:
2936 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002937 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002938 return ret;
2939}
2940EXPORT_SYMBOL(rdma_listen);
2941
Sean Heftye51060f2006-06-17 20:37:29 -07002942int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2943{
2944 struct rdma_id_private *id_priv;
2945 int ret;
2946
Sean Hefty680f9202013-05-29 10:09:12 -07002947 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
2948 addr->sa_family != AF_IB)
Sean Heftye51060f2006-06-17 20:37:29 -07002949 return -EAFNOSUPPORT;
2950
2951 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002952 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002953 return -EINVAL;
2954
Sean Heftyd14714d2009-11-19 16:46:25 -08002955 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2956 if (ret)
2957 goto err1;
2958
Moni Shoua7b856272013-12-12 18:03:12 +02002959 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
Sean Hefty8523c042010-02-08 16:41:15 -08002960 if (!cma_any_addr(addr)) {
Sean Hefty680f9202013-05-29 10:09:12 -07002961 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002962 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002963 goto err1;
2964
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002965 ret = cma_acquire_dev(id_priv, NULL);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002966 if (ret)
2967 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002968 }
2969
Sean Hefty68602122012-06-14 20:31:39 +00002970 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
2971 if (addr->sa_family == AF_INET)
2972 id_priv->afonly = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002973#if IS_ENABLED(CONFIG_IPV6)
Sean Hefty68602122012-06-14 20:31:39 +00002974 else if (addr->sa_family == AF_INET6)
2975 id_priv->afonly = init_net.ipv6.sysctl.bindv6only;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002976#endif
Sean Hefty68602122012-06-14 20:31:39 +00002977 }
Sean Heftye51060f2006-06-17 20:37:29 -07002978 ret = cma_get_port(id_priv);
2979 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002980 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002981
2982 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07002983err2:
Sean Heftya396d432011-02-23 09:05:39 -08002984 if (id_priv->cma_dev)
2985 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002986err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002987 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07002988 return ret;
2989}
2990EXPORT_SYMBOL(rdma_bind_addr);
2991
Sean Heftyf4753832013-05-29 10:09:14 -07002992static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07002993{
Sean Heftye51060f2006-06-17 20:37:29 -07002994 struct cma_hdr *cma_hdr;
Sean Heftye51060f2006-06-17 20:37:29 -07002995
Sean Hefty01602f12013-05-29 10:09:20 -07002996 cma_hdr = hdr;
2997 cma_hdr->cma_version = CMA_VERSION;
Sean Heftyf4753832013-05-29 10:09:14 -07002998 if (cma_family(id_priv) == AF_INET) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002999 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07003000
Sean Heftyf4753832013-05-29 10:09:14 -07003001 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3002 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003003
Sean Hefty01602f12013-05-29 10:09:20 -07003004 cma_set_ip_ver(cma_hdr, 4);
3005 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3006 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3007 cma_hdr->port = src4->sin_port;
Sean Heftye8160e12013-05-29 10:09:22 -07003008 } else if (cma_family(id_priv) == AF_INET6) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003009 struct sockaddr_in6 *src6, *dst6;
3010
Sean Heftyf4753832013-05-29 10:09:14 -07003011 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3012 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003013
Sean Hefty01602f12013-05-29 10:09:20 -07003014 cma_set_ip_ver(cma_hdr, 6);
3015 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3016 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3017 cma_hdr->port = src6->sin6_port;
Sean Heftye51060f2006-06-17 20:37:29 -07003018 }
3019 return 0;
3020}
3021
Sean Hefty628e5f62006-11-30 16:44:16 -08003022static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3023 struct ib_cm_event *ib_event)
3024{
3025 struct rdma_id_private *id_priv = cm_id->context;
3026 struct rdma_cm_event event;
3027 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3028 int ret = 0;
3029
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003030 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07003031 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08003032
Sean Hefty8aa08602007-05-07 11:49:00 -07003033 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08003034 switch (ib_event->event) {
3035 case IB_CM_SIDR_REQ_ERROR:
3036 event.event = RDMA_CM_EVENT_UNREACHABLE;
3037 event.status = -ETIMEDOUT;
3038 break;
3039 case IB_CM_SIDR_REP_RECEIVED:
3040 event.param.ud.private_data = ib_event->private_data;
3041 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3042 if (rep->status != IB_SIDR_SUCCESS) {
3043 event.event = RDMA_CM_EVENT_UNREACHABLE;
3044 event.status = ib_event->param.sidr_rep_rcvd.status;
3045 break;
3046 }
Sean Hefty5c438132013-05-29 10:09:23 -07003047 ret = cma_set_qkey(id_priv, rep->qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003048 if (ret) {
3049 event.event = RDMA_CM_EVENT_ADDR_ERROR;
Sean Hefty5c438132013-05-29 10:09:23 -07003050 event.status = ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003051 break;
3052 }
3053 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3054 id_priv->id.route.path_rec,
3055 &event.param.ud.ah_attr);
3056 event.param.ud.qp_num = rep->qpn;
3057 event.param.ud.qkey = rep->qkey;
3058 event.event = RDMA_CM_EVENT_ESTABLISHED;
3059 event.status = 0;
3060 break;
3061 default:
Roland Dreier468f2232008-07-14 23:48:47 -07003062 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08003063 ib_event->event);
3064 goto out;
3065 }
3066
3067 ret = id_priv->id.event_handler(&id_priv->id, &event);
3068 if (ret) {
3069 /* Destroy the CM ID by returning a non-zero value. */
3070 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003071 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003072 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003073 rdma_destroy_id(&id_priv->id);
3074 return ret;
3075 }
3076out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07003077 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003078 return ret;
3079}
3080
3081static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3082 struct rdma_conn_param *conn_param)
3083{
3084 struct ib_cm_sidr_req_param req;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003085 struct ib_cm_id *id;
Sean Heftye511d1a2013-07-24 15:06:08 -07003086 void *private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003087 int offset, ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003088
Sean Heftye511d1a2013-07-24 15:06:08 -07003089 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003090 offset = cma_user_data_offset(id_priv);
3091 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003092 if (req.private_data_len < conn_param->private_data_len)
3093 return -EINVAL;
3094
Sean Heftye8160e12013-05-29 10:09:22 -07003095 if (req.private_data_len) {
Sean Heftye511d1a2013-07-24 15:06:08 -07003096 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3097 if (!private_data)
Sean Heftye8160e12013-05-29 10:09:22 -07003098 return -ENOMEM;
3099 } else {
Sean Heftye511d1a2013-07-24 15:06:08 -07003100 private_data = NULL;
Sean Heftye8160e12013-05-29 10:09:22 -07003101 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003102
3103 if (conn_param->private_data && conn_param->private_data_len)
Sean Heftye511d1a2013-07-24 15:06:08 -07003104 memcpy(private_data + offset, conn_param->private_data,
3105 conn_param->private_data_len);
Sean Hefty628e5f62006-11-30 16:44:16 -08003106
Sean Heftye511d1a2013-07-24 15:06:08 -07003107 if (private_data) {
3108 ret = cma_format_hdr(private_data, id_priv);
Sean Heftye8160e12013-05-29 10:09:22 -07003109 if (ret)
3110 goto out;
Sean Heftye511d1a2013-07-24 15:06:08 -07003111 req.private_data = private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003112 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003113
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003114 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3115 id_priv);
3116 if (IS_ERR(id)) {
3117 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08003118 goto out;
3119 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003120 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08003121
Sean Heftyf4753832013-05-29 10:09:14 -07003122 req.path = id_priv->id.route.path_rec;
Sean Heftycf539362013-05-29 10:09:28 -07003123 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Hefty628e5f62006-11-30 16:44:16 -08003124 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3125 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3126
3127 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3128 if (ret) {
3129 ib_destroy_cm_id(id_priv->cm_id.ib);
3130 id_priv->cm_id.ib = NULL;
3131 }
3132out:
Sean Heftye511d1a2013-07-24 15:06:08 -07003133 kfree(private_data);
Sean Hefty628e5f62006-11-30 16:44:16 -08003134 return ret;
3135}
3136
Sean Heftye51060f2006-06-17 20:37:29 -07003137static int cma_connect_ib(struct rdma_id_private *id_priv,
3138 struct rdma_conn_param *conn_param)
3139{
3140 struct ib_cm_req_param req;
3141 struct rdma_route *route;
3142 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003143 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07003144 int offset, ret;
3145
3146 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003147 offset = cma_user_data_offset(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003148 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003149 if (req.private_data_len < conn_param->private_data_len)
3150 return -EINVAL;
3151
Sean Heftye8160e12013-05-29 10:09:22 -07003152 if (req.private_data_len) {
3153 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3154 if (!private_data)
3155 return -ENOMEM;
3156 } else {
3157 private_data = NULL;
3158 }
Sean Heftye51060f2006-06-17 20:37:29 -07003159
3160 if (conn_param->private_data && conn_param->private_data_len)
3161 memcpy(private_data + offset, conn_param->private_data,
3162 conn_param->private_data_len);
3163
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003164 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3165 if (IS_ERR(id)) {
3166 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07003167 goto out;
3168 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003169 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07003170
3171 route = &id_priv->id.route;
Sean Heftye8160e12013-05-29 10:09:22 -07003172 if (private_data) {
3173 ret = cma_format_hdr(private_data, id_priv);
3174 if (ret)
3175 goto out;
3176 req.private_data = private_data;
3177 }
Sean Heftye51060f2006-06-17 20:37:29 -07003178
3179 req.primary_path = &route->path_rec[0];
3180 if (route->num_paths == 2)
3181 req.alternate_path = &route->path_rec[1];
3182
Sean Heftycf539362013-05-29 10:09:28 -07003183 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07003184 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07003185 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07003186 req.starting_psn = id_priv->seq_num;
3187 req.responder_resources = conn_param->responder_resources;
3188 req.initiator_depth = conn_param->initiator_depth;
3189 req.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003190 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3191 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003192 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3193 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3194 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3195 req.srq = id_priv->srq ? 1 : 0;
3196
3197 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3198out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003199 if (ret && !IS_ERR(id)) {
3200 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07003201 id_priv->cm_id.ib = NULL;
3202 }
3203
Sean Heftye51060f2006-06-17 20:37:29 -07003204 kfree(private_data);
3205 return ret;
3206}
3207
Tom Tucker07ebafb2006-08-03 16:02:42 -05003208static int cma_connect_iw(struct rdma_id_private *id_priv,
3209 struct rdma_conn_param *conn_param)
3210{
3211 struct iw_cm_id *cm_id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003212 int ret;
3213 struct iw_cm_conn_param iw_param;
3214
3215 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003216 if (IS_ERR(cm_id))
3217 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003218
Steve Wise68cdba02015-05-18 15:27:10 -05003219 cm_id->tos = id_priv->tos;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003220 id_priv->cm_id.iw = cm_id;
3221
Steve Wise24d44a32013-07-04 16:10:44 +05303222 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3223 rdma_addr_size(cma_src_addr(id_priv)));
3224 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3225 rdma_addr_size(cma_dst_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05003226
Sean Hefty5851bb82008-01-04 10:47:12 -08003227 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07003228 if (ret)
3229 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003230
Hefty, Seanf45ee802011-10-06 09:33:04 -07003231 if (conn_param) {
3232 iw_param.ord = conn_param->initiator_depth;
3233 iw_param.ird = conn_param->responder_resources;
3234 iw_param.private_data = conn_param->private_data;
3235 iw_param.private_data_len = conn_param->private_data_len;
3236 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3237 } else {
3238 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003239 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07003240 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003241 ret = iw_cm_connect(cm_id, &iw_param);
3242out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003243 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07003244 iw_destroy_cm_id(cm_id);
3245 id_priv->cm_id.iw = NULL;
3246 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003247 return ret;
3248}
3249
Sean Heftye51060f2006-06-17 20:37:29 -07003250int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3251{
3252 struct rdma_id_private *id_priv;
3253 int ret;
3254
3255 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003256 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003257 return -EINVAL;
3258
3259 if (!id->qp) {
3260 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003261 id_priv->srq = conn_param->srq;
3262 }
3263
Michael Wang72219cea2015-05-05 14:50:34 +02003264 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003265 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08003266 ret = cma_resolve_ib_udp(id_priv, conn_param);
3267 else
3268 ret = cma_connect_ib(id_priv, conn_param);
Michael Wang04215332015-05-05 14:50:35 +02003269 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003270 ret = cma_connect_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003271 else
Sean Heftye51060f2006-06-17 20:37:29 -07003272 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003273 if (ret)
3274 goto err;
3275
3276 return 0;
3277err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003278 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07003279 return ret;
3280}
3281EXPORT_SYMBOL(rdma_connect);
3282
3283static int cma_accept_ib(struct rdma_id_private *id_priv,
3284 struct rdma_conn_param *conn_param)
3285{
3286 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08003287 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003288
Sean Hefty5851bb82008-01-04 10:47:12 -08003289 ret = cma_modify_qp_rtr(id_priv, conn_param);
3290 if (ret)
3291 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08003292
Sean Hefty5851bb82008-01-04 10:47:12 -08003293 ret = cma_modify_qp_rts(id_priv, conn_param);
3294 if (ret)
3295 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003296
3297 memset(&rep, 0, sizeof rep);
3298 rep.qp_num = id_priv->qp_num;
3299 rep.starting_psn = id_priv->seq_num;
3300 rep.private_data = conn_param->private_data;
3301 rep.private_data_len = conn_param->private_data_len;
3302 rep.responder_resources = conn_param->responder_resources;
3303 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07003304 rep.failover_accepted = 0;
3305 rep.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003306 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003307 rep.srq = id_priv->srq ? 1 : 0;
3308
Sean Hefty0fe313b2006-11-30 16:37:15 -08003309 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3310out:
3311 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003312}
3313
Tom Tucker07ebafb2006-08-03 16:02:42 -05003314static int cma_accept_iw(struct rdma_id_private *id_priv,
3315 struct rdma_conn_param *conn_param)
3316{
3317 struct iw_cm_conn_param iw_param;
3318 int ret;
3319
Sean Hefty5851bb82008-01-04 10:47:12 -08003320 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003321 if (ret)
3322 return ret;
3323
3324 iw_param.ord = conn_param->initiator_depth;
3325 iw_param.ird = conn_param->responder_resources;
3326 iw_param.private_data = conn_param->private_data;
3327 iw_param.private_data_len = conn_param->private_data_len;
3328 if (id_priv->id.qp) {
3329 iw_param.qpn = id_priv->qp_num;
3330 } else
3331 iw_param.qpn = conn_param->qp_num;
3332
3333 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3334}
3335
Sean Hefty628e5f62006-11-30 16:44:16 -08003336static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
Sean Hefty5c438132013-05-29 10:09:23 -07003337 enum ib_cm_sidr_status status, u32 qkey,
Sean Hefty628e5f62006-11-30 16:44:16 -08003338 const void *private_data, int private_data_len)
3339{
3340 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003341 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003342
3343 memset(&rep, 0, sizeof rep);
3344 rep.status = status;
3345 if (status == IB_SIDR_SUCCESS) {
Sean Hefty5c438132013-05-29 10:09:23 -07003346 ret = cma_set_qkey(id_priv, qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003347 if (ret)
3348 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003349 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003350 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08003351 }
3352 rep.private_data = private_data;
3353 rep.private_data_len = private_data_len;
3354
3355 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3356}
3357
Sean Heftye51060f2006-06-17 20:37:29 -07003358int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3359{
3360 struct rdma_id_private *id_priv;
3361 int ret;
3362
3363 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00003364
3365 id_priv->owner = task_pid_nr(current);
3366
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003367 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003368 return -EINVAL;
3369
3370 if (!id->qp && conn_param) {
3371 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003372 id_priv->srq = conn_param->srq;
3373 }
3374
Michael Wang72219cea2015-05-05 14:50:34 +02003375 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Hefty, Seanf45ee802011-10-06 09:33:04 -07003376 if (id->qp_type == IB_QPT_UD) {
3377 if (conn_param)
3378 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003379 conn_param->qkey,
Hefty, Seanf45ee802011-10-06 09:33:04 -07003380 conn_param->private_data,
3381 conn_param->private_data_len);
3382 else
3383 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003384 0, NULL, 0);
Hefty, Seanf45ee802011-10-06 09:33:04 -07003385 } else {
3386 if (conn_param)
3387 ret = cma_accept_ib(id_priv, conn_param);
3388 else
3389 ret = cma_rep_recv(id_priv);
3390 }
Michael Wang04215332015-05-05 14:50:35 +02003391 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003392 ret = cma_accept_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003393 else
Sean Heftye51060f2006-06-17 20:37:29 -07003394 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003395
3396 if (ret)
3397 goto reject;
3398
3399 return 0;
3400reject:
Sean Heftyc5483382007-09-24 13:19:09 -07003401 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003402 rdma_reject(id, NULL, 0);
3403 return ret;
3404}
3405EXPORT_SYMBOL(rdma_accept);
3406
Sean Hefty0fe313b2006-11-30 16:37:15 -08003407int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3408{
3409 struct rdma_id_private *id_priv;
3410 int ret;
3411
3412 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003413 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08003414 return -EINVAL;
3415
3416 switch (id->device->node_type) {
3417 case RDMA_NODE_IB_CA:
3418 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3419 break;
3420 default:
3421 ret = 0;
3422 break;
3423 }
3424 return ret;
3425}
3426EXPORT_SYMBOL(rdma_notify);
3427
Sean Heftye51060f2006-06-17 20:37:29 -07003428int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3429 u8 private_data_len)
3430{
3431 struct rdma_id_private *id_priv;
3432 int ret;
3433
3434 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003435 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003436 return -EINVAL;
3437
Michael Wang72219cea2015-05-05 14:50:34 +02003438 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003439 if (id->qp_type == IB_QPT_UD)
Sean Hefty5c438132013-05-29 10:09:23 -07003440 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
Sean Hefty628e5f62006-11-30 16:44:16 -08003441 private_data, private_data_len);
3442 else
3443 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3444 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3445 0, private_data, private_data_len);
Michael Wang04215332015-05-05 14:50:35 +02003446 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003447 ret = iw_cm_reject(id_priv->cm_id.iw,
3448 private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003449 } else
Sean Heftye51060f2006-06-17 20:37:29 -07003450 ret = -ENOSYS;
Michael Wang21655af2015-05-05 14:50:27 +02003451
Sean Heftye51060f2006-06-17 20:37:29 -07003452 return ret;
3453}
3454EXPORT_SYMBOL(rdma_reject);
3455
3456int rdma_disconnect(struct rdma_cm_id *id)
3457{
3458 struct rdma_id_private *id_priv;
3459 int ret;
3460
3461 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003462 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003463 return -EINVAL;
3464
Michael Wang72219cea2015-05-05 14:50:34 +02003465 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyc5483382007-09-24 13:19:09 -07003466 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003467 if (ret)
3468 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003469 /* Initiate or respond to a disconnect. */
3470 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3471 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
Michael Wang04215332015-05-05 14:50:35 +02003472 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003473 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003474 } else
Tom Tucker07ebafb2006-08-03 16:02:42 -05003475 ret = -EINVAL;
Michael Wang21655af2015-05-05 14:50:27 +02003476
Sean Heftye51060f2006-06-17 20:37:29 -07003477out:
3478 return ret;
3479}
3480EXPORT_SYMBOL(rdma_disconnect);
3481
Sean Heftyc8f6a362007-02-15 17:00:18 -08003482static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3483{
3484 struct rdma_id_private *id_priv;
3485 struct cma_multicast *mc = multicast->context;
3486 struct rdma_cm_event event;
3487 int ret;
3488
3489 id_priv = mc->id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003490 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
3491 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07003492 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003493
Sean Hefty5c438132013-05-29 10:09:23 -07003494 if (!status)
3495 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
Sean Heftyc5483382007-09-24 13:19:09 -07003496 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003497 if (!status && id_priv->id.qp)
3498 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003499 be16_to_cpu(multicast->rec.mlid));
Sean Heftyc5483382007-09-24 13:19:09 -07003500 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003501
3502 memset(&event, 0, sizeof event);
3503 event.status = status;
3504 event.param.ud.private_data = mc->context;
3505 if (!status) {
3506 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3507 ib_init_ah_from_mcmember(id_priv->id.device,
3508 id_priv->id.port_num, &multicast->rec,
3509 &event.param.ud.ah_attr);
3510 event.param.ud.qp_num = 0xFFFFFF;
3511 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3512 } else
3513 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3514
3515 ret = id_priv->id.event_handler(&id_priv->id, &event);
3516 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003517 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003518 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003519 rdma_destroy_id(&id_priv->id);
3520 return 0;
3521 }
Sean Hefty8aa08602007-05-07 11:49:00 -07003522
Or Gerlitzde910bd2008-07-14 23:48:53 -07003523 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003524 return 0;
3525}
3526
3527static void cma_set_mgid(struct rdma_id_private *id_priv,
3528 struct sockaddr *addr, union ib_gid *mgid)
3529{
3530 unsigned char mc_map[MAX_ADDR_LEN];
3531 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3532 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3533 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3534
3535 if (cma_any_addr(addr)) {
3536 memset(mgid, 0, sizeof *mgid);
3537 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08003538 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08003539 0xFF10A01B)) {
3540 /* IPv6 address is an SA assigned MGID. */
3541 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003542 } else if (addr->sa_family == AF_IB) {
3543 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08003544 } else if ((addr->sa_family == AF_INET6)) {
3545 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3546 if (id_priv->id.ps == RDMA_PS_UDP)
3547 mc_map[7] = 0x01; /* Use RDMA CM signature */
3548 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003549 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07003550 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003551 if (id_priv->id.ps == RDMA_PS_UDP)
3552 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08003553 *mgid = *(union ib_gid *) (mc_map + 4);
3554 }
3555}
3556
3557static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3558 struct cma_multicast *mc)
3559{
3560 struct ib_sa_mcmember_rec rec;
3561 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3562 ib_sa_comp_mask comp_mask;
3563 int ret;
3564
3565 ib_addr_get_mgid(dev_addr, &rec.mgid);
3566 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3567 &rec.mgid, &rec);
3568 if (ret)
3569 return ret;
3570
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003571 ret = cma_set_qkey(id_priv, 0);
3572 if (ret)
3573 return ret;
3574
Roland Dreier3f446752008-08-04 11:02:14 -07003575 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003576 rec.qkey = cpu_to_be32(id_priv->qkey);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003577 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003578 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3579 rec.join_state = 1;
3580
3581 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3582 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3583 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3584 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3585 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3586
Yossi Etigin84adeee2009-04-01 13:55:32 -07003587 if (id_priv->id.ps == RDMA_PS_IPOIB)
3588 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
Dotan Barak2a22fb82012-08-30 09:09:55 +00003589 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3590 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3591 IB_SA_MCMEMBER_REC_MTU |
3592 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Yossi Etigin84adeee2009-04-01 13:55:32 -07003593
Sean Heftyc8f6a362007-02-15 17:00:18 -08003594 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3595 id_priv->id.port_num, &rec,
3596 comp_mask, GFP_KERNEL,
3597 cma_ib_mc_handler, mc);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303598 return PTR_ERR_OR_ZERO(mc->multicast.ib);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003599}
3600
Eli Cohen3c86aa72010-10-13 21:26:51 +02003601static void iboe_mcast_work_handler(struct work_struct *work)
3602{
3603 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3604 struct cma_multicast *mc = mw->mc;
3605 struct ib_sa_multicast *m = mc->multicast.ib;
3606
3607 mc->multicast.ib->context = mc;
3608 cma_ib_mc_handler(0, m);
3609 kref_put(&mc->mcref, release_mc);
3610 kfree(mw);
3611}
3612
3613static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3614{
3615 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3616 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3617
3618 if (cma_any_addr(addr)) {
3619 memset(mgid, 0, sizeof *mgid);
3620 } else if (addr->sa_family == AF_INET6) {
3621 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3622 } else {
3623 mgid->raw[0] = 0xff;
3624 mgid->raw[1] = 0x0e;
3625 mgid->raw[2] = 0;
3626 mgid->raw[3] = 0;
3627 mgid->raw[4] = 0;
3628 mgid->raw[5] = 0;
3629 mgid->raw[6] = 0;
3630 mgid->raw[7] = 0;
3631 mgid->raw[8] = 0;
3632 mgid->raw[9] = 0;
3633 mgid->raw[10] = 0xff;
3634 mgid->raw[11] = 0xff;
3635 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3636 }
3637}
3638
3639static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3640 struct cma_multicast *mc)
3641{
3642 struct iboe_mcast_work *work;
3643 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3644 int err;
3645 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3646 struct net_device *ndev = NULL;
3647
3648 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3649 return -EINVAL;
3650
3651 work = kzalloc(sizeof *work, GFP_KERNEL);
3652 if (!work)
3653 return -ENOMEM;
3654
3655 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3656 if (!mc->multicast.ib) {
3657 err = -ENOMEM;
3658 goto out1;
3659 }
3660
3661 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3662
3663 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3664 if (id_priv->id.ps == RDMA_PS_UDP)
3665 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3666
3667 if (dev_addr->bound_dev_if)
3668 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3669 if (!ndev) {
3670 err = -ENODEV;
3671 goto out2;
3672 }
3673 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3674 mc->multicast.ib->rec.hop_limit = 1;
3675 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3676 dev_put(ndev);
3677 if (!mc->multicast.ib->rec.mtu) {
3678 err = -EINVAL;
3679 goto out2;
3680 }
Moni Shoua7b856272013-12-12 18:03:12 +02003681 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3682 &mc->multicast.ib->rec.port_gid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003683 work->id = id_priv;
3684 work->mc = mc;
3685 INIT_WORK(&work->work, iboe_mcast_work_handler);
3686 kref_get(&mc->mcref);
3687 queue_work(cma_wq, &work->work);
3688
3689 return 0;
3690
3691out2:
3692 kfree(mc->multicast.ib);
3693out1:
3694 kfree(work);
3695 return err;
3696}
3697
Sean Heftyc8f6a362007-02-15 17:00:18 -08003698int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3699 void *context)
3700{
3701 struct rdma_id_private *id_priv;
3702 struct cma_multicast *mc;
3703 int ret;
3704
3705 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003706 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3707 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08003708 return -EINVAL;
3709
3710 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3711 if (!mc)
3712 return -ENOMEM;
3713
Sean Heftyef560862013-05-29 10:09:10 -07003714 memcpy(&mc->addr, addr, rdma_addr_size(addr));
Sean Heftyc8f6a362007-02-15 17:00:18 -08003715 mc->context = context;
3716 mc->id_priv = id_priv;
3717
3718 spin_lock(&id_priv->lock);
3719 list_add(&mc->list, &id_priv->mc_list);
3720 spin_unlock(&id_priv->lock);
3721
Ira Weiny5d9fb042015-05-14 15:01:46 -04003722 if (rdma_protocol_roce(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003723 kref_init(&mc->mcref);
3724 ret = cma_iboe_join_multicast(id_priv, mc);
Michael Wanga31ad3b2015-05-05 14:50:37 +02003725 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02003726 ret = cma_join_ib_multicast(id_priv, mc);
3727 else
Sean Heftyc8f6a362007-02-15 17:00:18 -08003728 ret = -ENOSYS;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003729
3730 if (ret) {
3731 spin_lock_irq(&id_priv->lock);
3732 list_del(&mc->list);
3733 spin_unlock_irq(&id_priv->lock);
3734 kfree(mc);
3735 }
3736 return ret;
3737}
3738EXPORT_SYMBOL(rdma_join_multicast);
3739
3740void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3741{
3742 struct rdma_id_private *id_priv;
3743 struct cma_multicast *mc;
3744
3745 id_priv = container_of(id, struct rdma_id_private, id);
3746 spin_lock_irq(&id_priv->lock);
3747 list_for_each_entry(mc, &id_priv->mc_list, list) {
Sean Heftyef560862013-05-29 10:09:10 -07003748 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
Sean Heftyc8f6a362007-02-15 17:00:18 -08003749 list_del(&mc->list);
3750 spin_unlock_irq(&id_priv->lock);
3751
3752 if (id->qp)
3753 ib_detach_mcast(id->qp,
3754 &mc->multicast.ib->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003755 be16_to_cpu(mc->multicast.ib->rec.mlid));
Michael Wang5c9a5282015-05-05 14:50:29 +02003756
3757 BUG_ON(id_priv->cma_dev->device != id->device);
3758
Michael Wanga31ad3b2015-05-05 14:50:37 +02003759 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003760 ib_sa_free_multicast(mc->multicast.ib);
3761 kfree(mc);
Ira Weiny5d9fb042015-05-14 15:01:46 -04003762 } else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02003763 kref_put(&mc->mcref, release_mc);
3764
Sean Heftyc8f6a362007-02-15 17:00:18 -08003765 return;
3766 }
3767 }
3768 spin_unlock_irq(&id_priv->lock);
3769}
3770EXPORT_SYMBOL(rdma_leave_multicast);
3771
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003772static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3773{
3774 struct rdma_dev_addr *dev_addr;
3775 struct cma_ndev_work *work;
3776
3777 dev_addr = &id_priv->id.route.addr.dev_addr;
3778
Sean Hefty6266ed62009-11-19 12:55:22 -08003779 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003780 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3781 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3782 ndev->name, &id_priv->id);
3783 work = kzalloc(sizeof *work, GFP_KERNEL);
3784 if (!work)
3785 return -ENOMEM;
3786
3787 INIT_WORK(&work->work, cma_ndev_work_handler);
3788 work->id = id_priv;
3789 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3790 atomic_inc(&id_priv->refcount);
3791 queue_work(cma_wq, &work->work);
3792 }
3793
3794 return 0;
3795}
3796
3797static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003798 void *ptr)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003799{
Jiri Pirko351638e2013-05-28 01:30:21 +00003800 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003801 struct cma_device *cma_dev;
3802 struct rdma_id_private *id_priv;
3803 int ret = NOTIFY_DONE;
3804
3805 if (dev_net(ndev) != &init_net)
3806 return NOTIFY_DONE;
3807
3808 if (event != NETDEV_BONDING_FAILOVER)
3809 return NOTIFY_DONE;
3810
3811 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3812 return NOTIFY_DONE;
3813
3814 mutex_lock(&lock);
3815 list_for_each_entry(cma_dev, &dev_list, list)
3816 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3817 ret = cma_netdev_change(ndev, id_priv);
3818 if (ret)
3819 goto out;
3820 }
3821
3822out:
3823 mutex_unlock(&lock);
3824 return ret;
3825}
3826
3827static struct notifier_block cma_nb = {
3828 .notifier_call = cma_netdev_callback
3829};
3830
Sean Heftye51060f2006-06-17 20:37:29 -07003831static void cma_add_one(struct ib_device *device)
3832{
3833 struct cma_device *cma_dev;
3834 struct rdma_id_private *id_priv;
3835
3836 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3837 if (!cma_dev)
3838 return;
3839
3840 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07003841
3842 init_completion(&cma_dev->comp);
3843 atomic_set(&cma_dev->refcount, 1);
3844 INIT_LIST_HEAD(&cma_dev->id_list);
3845 ib_set_client_data(device, &cma_client, cma_dev);
3846
3847 mutex_lock(&lock);
3848 list_add_tail(&cma_dev->list, &dev_list);
3849 list_for_each_entry(id_priv, &listen_any_list, list)
3850 cma_listen_on_dev(id_priv, cma_dev);
3851 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07003852}
3853
3854static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3855{
Sean Heftya1b1b612006-11-30 16:33:14 -08003856 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003857 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003858 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07003859
3860 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003861 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3862 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07003863 return 0;
3864
3865 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003866 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07003867
3868 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003869 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07003870 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003871
Sean Heftya1b1b612006-11-30 16:33:14 -08003872 memset(&event, 0, sizeof event);
3873 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003874 ret = id_priv->id.event_handler(&id_priv->id, &event);
3875out:
3876 mutex_unlock(&id_priv->handler_mutex);
3877 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003878}
3879
3880static void cma_process_remove(struct cma_device *cma_dev)
3881{
Sean Heftye51060f2006-06-17 20:37:29 -07003882 struct rdma_id_private *id_priv;
3883 int ret;
3884
Sean Heftye51060f2006-06-17 20:37:29 -07003885 mutex_lock(&lock);
3886 while (!list_empty(&cma_dev->id_list)) {
3887 id_priv = list_entry(cma_dev->id_list.next,
3888 struct rdma_id_private, list);
3889
Sean Heftyd02d1f52007-10-09 11:12:34 -07003890 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07003891 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07003892 atomic_inc(&id_priv->refcount);
3893 mutex_unlock(&lock);
3894
Sean Heftyd02d1f52007-10-09 11:12:34 -07003895 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003896 cma_deref_id(id_priv);
3897 if (ret)
3898 rdma_destroy_id(&id_priv->id);
3899
3900 mutex_lock(&lock);
3901 }
3902 mutex_unlock(&lock);
3903
3904 cma_deref_dev(cma_dev);
3905 wait_for_completion(&cma_dev->comp);
3906}
3907
Haggai Eran7c1eb452015-07-30 17:50:14 +03003908static void cma_remove_one(struct ib_device *device, void *client_data)
Sean Heftye51060f2006-06-17 20:37:29 -07003909{
Haggai Eran7c1eb452015-07-30 17:50:14 +03003910 struct cma_device *cma_dev = client_data;
Sean Heftye51060f2006-06-17 20:37:29 -07003911
Sean Heftye51060f2006-06-17 20:37:29 -07003912 if (!cma_dev)
3913 return;
3914
3915 mutex_lock(&lock);
3916 list_del(&cma_dev->list);
3917 mutex_unlock(&lock);
3918
3919 cma_process_remove(cma_dev);
3920 kfree(cma_dev);
3921}
3922
Nir Muchtar753f6182011-01-03 15:33:53 +00003923static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3924{
3925 struct nlmsghdr *nlh;
3926 struct rdma_cm_id_stats *id_stats;
3927 struct rdma_id_private *id_priv;
3928 struct rdma_cm_id *id = NULL;
3929 struct cma_device *cma_dev;
3930 int i_dev = 0, i_id = 0;
3931
3932 /*
3933 * We export all of the IDs as a sequence of messages. Each
3934 * ID gets its own netlink message.
3935 */
3936 mutex_lock(&lock);
3937
3938 list_for_each_entry(cma_dev, &dev_list, list) {
3939 if (i_dev < cb->args[0]) {
3940 i_dev++;
3941 continue;
3942 }
3943
3944 i_id = 0;
3945 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3946 if (i_id < cb->args[1]) {
3947 i_id++;
3948 continue;
3949 }
3950
3951 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3952 sizeof *id_stats, RDMA_NL_RDMA_CM,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -05003953 RDMA_NL_RDMA_CM_ID_STATS,
3954 NLM_F_MULTI);
Nir Muchtar753f6182011-01-03 15:33:53 +00003955 if (!id_stats)
3956 goto out;
3957
3958 memset(id_stats, 0, sizeof *id_stats);
3959 id = &id_priv->id;
3960 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3961 id_stats->port_num = id->port_num;
3962 id_stats->bound_dev_if =
3963 id->route.addr.dev_addr.bound_dev_if;
3964
Sean Heftyce117ff2013-05-29 10:09:34 -07003965 if (ibnl_put_attr(skb, nlh,
3966 rdma_addr_size(cma_src_addr(id_priv)),
3967 cma_src_addr(id_priv),
3968 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
3969 goto out;
3970 if (ibnl_put_attr(skb, nlh,
3971 rdma_addr_size(cma_src_addr(id_priv)),
3972 cma_dst_addr(id_priv),
3973 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
3974 goto out;
Nir Muchtar753f6182011-01-03 15:33:53 +00003975
Nir Muchtar83e95022011-01-13 13:56:04 +00003976 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00003977 id_stats->port_space = id->ps;
3978 id_stats->cm_state = id_priv->state;
3979 id_stats->qp_num = id_priv->qp_num;
3980 id_stats->qp_type = id->qp_type;
3981
3982 i_id++;
3983 }
3984
3985 cb->args[1] = 0;
3986 i_dev++;
3987 }
3988
3989out:
3990 mutex_unlock(&lock);
3991 cb->args[0] = i_dev;
3992 cb->args[1] = i_id;
3993
3994 return skb->len;
3995}
3996
3997static const struct ibnl_client_cbs cma_cb_table[] = {
Gao feng809d5fc2012-10-04 20:15:49 +00003998 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
3999 .module = THIS_MODULE },
Nir Muchtar753f6182011-01-03 15:33:53 +00004000};
4001
Peter Huewe716abb12009-06-23 10:38:42 -07004002static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004003{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09004004 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004005
Sean Heftyc7f743a2007-02-01 12:23:37 -08004006 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07004007 if (!cma_wq)
4008 return -ENOMEM;
4009
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004010 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08004011 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004012 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004013
Sean Heftye51060f2006-06-17 20:37:29 -07004014 ret = ib_register_client(&cma_client);
4015 if (ret)
4016 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00004017
4018 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
4019 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
4020
Sean Heftye51060f2006-06-17 20:37:29 -07004021 return 0;
4022
4023err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004024 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004025 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004026 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07004027 destroy_workqueue(cma_wq);
4028 return ret;
4029}
4030
Peter Huewe716abb12009-06-23 10:38:42 -07004031static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004032{
Nir Muchtar753f6182011-01-03 15:33:53 +00004033 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07004034 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004035 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004036 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004037 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07004038 destroy_workqueue(cma_wq);
Sean Heftye51060f2006-06-17 20:37:29 -07004039 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08004040 idr_destroy(&udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -08004041 idr_destroy(&ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -07004042 idr_destroy(&ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07004043}
4044
4045module_init(cma_init);
4046module_exit(cma_cleanup);