blob: f2d40c05ef9efb99596ffc41c69d88382d9d57fa [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>
Moni Shouabee3c3c2015-12-23 14:56:57 +020041#include <linux/igmp.h>
Sean Heftye51060f2006-06-17 20:37:29 -070042#include <linux/idr.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050043#include <linux/inetdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040045#include <linux/module.h>
Amir Vadai366cddb2012-04-04 21:33:29 +000046#include <net/route.h>
Sean Heftye51060f2006-06-17 20:37:29 -070047
Haggai Eran4be74b42015-10-22 15:20:09 +030048#include <net/net_namespace.h>
49#include <net/netns/generic.h>
Sean Heftye51060f2006-06-17 20:37:29 -070050#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080051#include <net/ipv6.h>
Haggai Eranf887f2a2015-07-30 17:50:23 +030052#include <net/ip_fib.h>
53#include <net/ip6_route.h>
Sean Heftye51060f2006-06-17 20:37:29 -070054
55#include <rdma/rdma_cm.h>
56#include <rdma/rdma_cm_ib.h>
Nir Muchtar753f6182011-01-03 15:33:53 +000057#include <rdma/rdma_netlink.h>
Sean Hefty2e2d1902013-05-29 10:09:09 -070058#include <rdma/ib.h>
Sean Heftye51060f2006-06-17 20:37:29 -070059#include <rdma/ib_cache.h>
60#include <rdma/ib_cm.h>
61#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050062#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070063
Matan Barak218a7732015-12-23 14:56:54 +020064#include "core_priv.h"
65
Sean Heftye51060f2006-06-17 20:37:29 -070066MODULE_AUTHOR("Sean Hefty");
67MODULE_DESCRIPTION("Generic RDMA CM Agent");
68MODULE_LICENSE("Dual BSD/GPL");
69
70#define CMA_CM_RESPONSE_TIMEOUT 20
Alex Veskerab15c952016-07-06 16:36:35 +030071#define CMA_QUERY_CLASSPORT_INFO_TIMEOUT 3000
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030072#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070073#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020074#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070075
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030076static const char * const cma_events[] = {
77 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
78 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
79 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
80 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
81 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
82 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
83 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
84 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
85 [RDMA_CM_EVENT_REJECTED] = "rejected",
86 [RDMA_CM_EVENT_ESTABLISHED] = "established",
87 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
88 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
89 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
90 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
91 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
92 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
93};
94
Bart Van Asschedb7489e2015-08-03 10:01:52 -070095const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030096{
97 size_t index = event;
98
99 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
100 cma_events[index] : "unrecognized event";
101}
102EXPORT_SYMBOL(rdma_event_msg);
103
Sean Heftye51060f2006-06-17 20:37:29 -0700104static void cma_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300105static void cma_remove_one(struct ib_device *device, void *client_data);
Sean Heftye51060f2006-06-17 20:37:29 -0700106
107static struct ib_client cma_client = {
108 .name = "cma",
109 .add = cma_add_one,
110 .remove = cma_remove_one
111};
112
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700113static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -0800114static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -0700115static LIST_HEAD(dev_list);
116static LIST_HEAD(listen_any_list);
117static DEFINE_MUTEX(lock);
118static struct workqueue_struct *cma_wq;
Haggai Eran4be74b42015-10-22 15:20:09 +0300119static int cma_pernet_id;
Sean Heftye51060f2006-06-17 20:37:29 -0700120
Haggai Eran4be74b42015-10-22 15:20:09 +0300121struct cma_pernet {
122 struct idr tcp_ps;
123 struct idr udp_ps;
124 struct idr ipoib_ps;
125 struct idr ib_ps;
126};
127
128static struct cma_pernet *cma_pernet(struct net *net)
Haggai Eranaac978e2015-07-30 17:50:20 +0300129{
Haggai Eran4be74b42015-10-22 15:20:09 +0300130 return net_generic(net, cma_pernet_id);
131}
132
133static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps)
134{
135 struct cma_pernet *pernet = cma_pernet(net);
136
Haggai Eranaac978e2015-07-30 17:50:20 +0300137 switch (ps) {
138 case RDMA_PS_TCP:
Haggai Eran4be74b42015-10-22 15:20:09 +0300139 return &pernet->tcp_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300140 case RDMA_PS_UDP:
Haggai Eran4be74b42015-10-22 15:20:09 +0300141 return &pernet->udp_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300142 case RDMA_PS_IPOIB:
Haggai Eran4be74b42015-10-22 15:20:09 +0300143 return &pernet->ipoib_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300144 case RDMA_PS_IB:
Haggai Eran4be74b42015-10-22 15:20:09 +0300145 return &pernet->ib_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300146 default:
147 return NULL;
148 }
149}
150
Sean Heftye51060f2006-06-17 20:37:29 -0700151struct cma_device {
152 struct list_head list;
153 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -0700154 struct completion comp;
155 atomic_t refcount;
156 struct list_head id_list;
Matan Barak045959d2015-12-23 14:56:55 +0200157 enum ib_gid_type *default_gid_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700158};
159
Sean Heftye51060f2006-06-17 20:37:29 -0700160struct rdma_bind_list {
Haggai Eranaac978e2015-07-30 17:50:20 +0300161 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -0700162 struct hlist_head owners;
163 unsigned short port;
164};
165
Alex Veskerab15c952016-07-06 16:36:35 +0300166struct class_port_info_context {
167 struct ib_class_port_info *class_port_info;
168 struct ib_device *device;
169 struct completion done;
170 struct ib_sa_query *sa_query;
171 u8 port_num;
172};
173
Haggai Eran4be74b42015-10-22 15:20:09 +0300174static int cma_ps_alloc(struct net *net, enum rdma_port_space ps,
Haggai Eranaac978e2015-07-30 17:50:20 +0300175 struct rdma_bind_list *bind_list, int snum)
176{
Haggai Eran4be74b42015-10-22 15:20:09 +0300177 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300178
179 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
180}
181
Haggai Eran4be74b42015-10-22 15:20:09 +0300182static struct rdma_bind_list *cma_ps_find(struct net *net,
183 enum rdma_port_space ps, int snum)
Haggai Eranaac978e2015-07-30 17:50:20 +0300184{
Haggai Eran4be74b42015-10-22 15:20:09 +0300185 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300186
187 return idr_find(idr, snum);
188}
189
Haggai Eran4be74b42015-10-22 15:20:09 +0300190static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum)
Haggai Eranaac978e2015-07-30 17:50:20 +0300191{
Haggai Eran4be74b42015-10-22 15:20:09 +0300192 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300193
194 idr_remove(idr, snum);
195}
196
Sean Hefty68602122012-06-14 20:31:39 +0000197enum {
198 CMA_OPTION_AFONLY,
199};
200
Matan Barak218a7732015-12-23 14:56:54 +0200201void cma_ref_dev(struct cma_device *cma_dev)
202{
203 atomic_inc(&cma_dev->refcount);
204}
205
Matan Barak045959d2015-12-23 14:56:55 +0200206struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
207 void *cookie)
208{
209 struct cma_device *cma_dev;
210 struct cma_device *found_cma_dev = NULL;
211
212 mutex_lock(&lock);
213
214 list_for_each_entry(cma_dev, &dev_list, list)
215 if (filter(cma_dev->device, cookie)) {
216 found_cma_dev = cma_dev;
217 break;
218 }
219
220 if (found_cma_dev)
221 cma_ref_dev(found_cma_dev);
222 mutex_unlock(&lock);
223 return found_cma_dev;
224}
225
226int cma_get_default_gid_type(struct cma_device *cma_dev,
227 unsigned int port)
228{
229 if (port < rdma_start_port(cma_dev->device) ||
230 port > rdma_end_port(cma_dev->device))
231 return -EINVAL;
232
233 return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
234}
235
236int cma_set_default_gid_type(struct cma_device *cma_dev,
237 unsigned int port,
238 enum ib_gid_type default_gid_type)
239{
240 unsigned long supported_gids;
241
242 if (port < rdma_start_port(cma_dev->device) ||
243 port > rdma_end_port(cma_dev->device))
244 return -EINVAL;
245
246 supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
247
248 if (!(supported_gids & 1 << default_gid_type))
249 return -EINVAL;
250
251 cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
252 default_gid_type;
253
254 return 0;
255}
256
257struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
258{
259 return cma_dev->device;
260}
261
Sean Heftye51060f2006-06-17 20:37:29 -0700262/*
263 * Device removal can occur at anytime, so we need extra handling to
264 * serialize notifying the user of device removal with other callbacks.
265 * We do this by disabling removal notification while a callback is in process,
266 * and reporting it after the callback completes.
267 */
268struct rdma_id_private {
269 struct rdma_cm_id id;
270
271 struct rdma_bind_list *bind_list;
272 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700273 struct list_head list; /* listen_any_list or cma_device.list */
274 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700275 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800276 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700277
Sean Heftyd02d1f52007-10-09 11:12:34 -0700278 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700279 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700280 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700281 struct mutex qp_mutex;
282
Sean Heftye51060f2006-06-17 20:37:29 -0700283 struct completion comp;
284 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700285 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700286
287 int backlog;
288 int timeout_ms;
289 struct ib_sa_query *query;
290 int query_id;
291 union {
292 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500293 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700294 } cm_id;
295
296 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800297 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700298 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000299 pid_t owner;
Sean Hefty68602122012-06-14 20:31:39 +0000300 u32 options;
Sean Heftye51060f2006-06-17 20:37:29 -0700301 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700302 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700303 u8 reuseaddr;
Sean Hefty5b0ec992012-06-14 20:31:39 +0000304 u8 afonly;
Matan Barakcb57bb82015-12-23 14:56:48 +0200305 enum ib_gid_type gid_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700306};
307
Sean Heftyc8f6a362007-02-15 17:00:18 -0800308struct cma_multicast {
309 struct rdma_id_private *id_priv;
310 union {
311 struct ib_sa_multicast *ib;
312 } multicast;
313 struct list_head list;
314 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700315 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200316 struct kref mcref;
Moni Shouabee3c3c2015-12-23 14:56:57 +0200317 bool igmp_joined;
Alex Veskerab15c952016-07-06 16:36:35 +0300318 u8 join_state;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800319};
320
Sean Heftye51060f2006-06-17 20:37:29 -0700321struct cma_work {
322 struct work_struct work;
323 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700324 enum rdma_cm_state old_state;
325 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700326 struct rdma_cm_event event;
327};
328
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700329struct cma_ndev_work {
330 struct work_struct work;
331 struct rdma_id_private *id;
332 struct rdma_cm_event event;
333};
334
Eli Cohen3c86aa72010-10-13 21:26:51 +0200335struct iboe_mcast_work {
336 struct work_struct work;
337 struct rdma_id_private *id;
338 struct cma_multicast *mc;
339};
340
Sean Heftye51060f2006-06-17 20:37:29 -0700341union cma_ip_addr {
342 struct in6_addr ip6;
343 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000344 __be32 pad[3];
345 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700346 } ip4;
347};
348
349struct cma_hdr {
350 u8 cma_version;
351 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000352 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700353 union cma_ip_addr src_addr;
354 union cma_ip_addr dst_addr;
355};
356
Sean Heftye51060f2006-06-17 20:37:29 -0700357#define CMA_VERSION 0x00
Sean Heftye51060f2006-06-17 20:37:29 -0700358
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300359struct cma_req_info {
360 struct ib_device *device;
361 int port;
362 union ib_gid local_gid;
363 __be64 service_id;
364 u16 pkey;
365 bool has_gid:1;
366};
367
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700368static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700369{
370 unsigned long flags;
371 int ret;
372
373 spin_lock_irqsave(&id_priv->lock, flags);
374 ret = (id_priv->state == comp);
375 spin_unlock_irqrestore(&id_priv->lock, flags);
376 return ret;
377}
378
379static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700380 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700381{
382 unsigned long flags;
383 int ret;
384
385 spin_lock_irqsave(&id_priv->lock, flags);
386 if ((ret = (id_priv->state == comp)))
387 id_priv->state = exch;
388 spin_unlock_irqrestore(&id_priv->lock, flags);
389 return ret;
390}
391
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700392static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
393 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700394{
395 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700396 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700397
398 spin_lock_irqsave(&id_priv->lock, flags);
399 old = id_priv->state;
400 id_priv->state = exch;
401 spin_unlock_irqrestore(&id_priv->lock, flags);
402 return old;
403}
404
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300405static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
Sean Heftye51060f2006-06-17 20:37:29 -0700406{
407 return hdr->ip_version >> 4;
408}
409
410static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
411{
412 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
413}
414
Moni Shouabee3c3c2015-12-23 14:56:57 +0200415static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
Sean Heftye51060f2006-06-17 20:37:29 -0700416{
Moni Shouabee3c3c2015-12-23 14:56:57 +0200417 struct in_device *in_dev = NULL;
418
419 if (ndev) {
420 rtnl_lock();
421 in_dev = __in_dev_get_rtnl(ndev);
422 if (in_dev) {
423 if (join)
424 ip_mc_inc_group(in_dev,
425 *(__be32 *)(mgid->raw + 12));
426 else
427 ip_mc_dec_group(in_dev,
428 *(__be32 *)(mgid->raw + 12));
429 }
430 rtnl_unlock();
431 }
432 return (in_dev) ? 0 : -ENODEV;
433}
434
Matan Barak045959d2015-12-23 14:56:55 +0200435static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
436 struct cma_device *cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700437{
Matan Barak218a7732015-12-23 14:56:54 +0200438 cma_ref_dev(cma_dev);
Sean Heftye51060f2006-06-17 20:37:29 -0700439 id_priv->cma_dev = cma_dev;
Matan Barak045959d2015-12-23 14:56:55 +0200440 id_priv->gid_type = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700441 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200442 id_priv->id.route.addr.dev_addr.transport =
443 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700444 list_add_tail(&id_priv->list, &cma_dev->id_list);
445}
446
Matan Barak045959d2015-12-23 14:56:55 +0200447static void cma_attach_to_dev(struct rdma_id_private *id_priv,
448 struct cma_device *cma_dev)
449{
450 _cma_attach_to_dev(id_priv, cma_dev);
451 id_priv->gid_type =
452 cma_dev->default_gid_type[id_priv->id.port_num -
453 rdma_start_port(cma_dev->device)];
454}
455
Matan Barak218a7732015-12-23 14:56:54 +0200456void cma_deref_dev(struct cma_device *cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700457{
458 if (atomic_dec_and_test(&cma_dev->refcount))
459 complete(&cma_dev->comp);
460}
461
Eli Cohen3c86aa72010-10-13 21:26:51 +0200462static inline void release_mc(struct kref *kref)
463{
464 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
465
466 kfree(mc->multicast.ib);
467 kfree(mc);
468}
469
Sean Heftya396d432011-02-23 09:05:39 -0800470static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700471{
Sean Heftya396d432011-02-23 09:05:39 -0800472 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700473 list_del(&id_priv->list);
474 cma_deref_dev(id_priv->cma_dev);
475 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800476 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700477}
478
Sean Heftyf4753832013-05-29 10:09:14 -0700479static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
480{
481 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
482}
483
484static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
485{
486 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
487}
488
489static inline unsigned short cma_family(struct rdma_id_private *id_priv)
490{
491 return id_priv->id.route.addr.src_addr.ss_family;
492}
493
Sean Hefty5c438132013-05-29 10:09:23 -0700494static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800495{
496 struct ib_sa_mcmember_rec rec;
497 int ret = 0;
498
Sean Hefty5c438132013-05-29 10:09:23 -0700499 if (id_priv->qkey) {
500 if (qkey && id_priv->qkey != qkey)
501 return -EINVAL;
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700502 return 0;
Sean Hefty5c438132013-05-29 10:09:23 -0700503 }
504
505 if (qkey) {
506 id_priv->qkey = qkey;
507 return 0;
508 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700509
510 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800511 case RDMA_PS_UDP:
Sean Hefty5c438132013-05-29 10:09:23 -0700512 case RDMA_PS_IB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700513 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800514 break;
515 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700516 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
517 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
518 id_priv->id.port_num, &rec.mgid,
519 &rec);
520 if (!ret)
521 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800522 break;
523 default:
524 break;
525 }
526 return ret;
527}
528
Sean Hefty680f9202013-05-29 10:09:12 -0700529static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
530{
531 dev_addr->dev_type = ARPHRD_INFINIBAND;
532 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
533 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
534}
535
536static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
537{
538 int ret;
539
540 if (addr->sa_family != AF_IB) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200541 ret = rdma_translate_ip(addr, dev_addr, NULL);
Sean Hefty680f9202013-05-29 10:09:12 -0700542 } else {
543 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
544 ret = 0;
545 }
546
547 return ret;
548}
549
Michael Wang7c111472015-05-05 14:50:30 +0200550static inline int cma_validate_port(struct ib_device *device, u8 port,
Matan Barak045959d2015-12-23 14:56:55 +0200551 enum ib_gid_type gid_type,
Matan Barakabae1b72015-10-15 18:38:49 +0300552 union ib_gid *gid, int dev_type,
553 int bound_if_index)
Michael Wang7c111472015-05-05 14:50:30 +0200554{
Michael Wang7c111472015-05-05 14:50:30 +0200555 int ret = -ENODEV;
Matan Barakabae1b72015-10-15 18:38:49 +0300556 struct net_device *ndev = NULL;
Michael Wang7c111472015-05-05 14:50:30 +0200557
558 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
559 return ret;
560
561 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
562 return ret;
563
Matan Barak64936772016-01-07 11:19:29 +0200564 if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
Matan Barakabae1b72015-10-15 18:38:49 +0300565 ndev = dev_get_by_index(&init_net, bound_if_index);
Matan Barak20029832015-12-23 14:56:53 +0200566 if (ndev && ndev->flags & IFF_LOOPBACK) {
567 pr_info("detected loopback device\n");
568 dev_put(ndev);
Matan Barakabae1b72015-10-15 18:38:49 +0300569
Matan Barak20029832015-12-23 14:56:53 +0200570 if (!device->get_netdev)
571 return -EOPNOTSUPP;
572
573 ndev = device->get_netdev(device, port);
574 if (!ndev)
575 return -ENODEV;
576 }
Matan Barak045959d2015-12-23 14:56:55 +0200577 } else {
578 gid_type = IB_GID_TYPE_IB;
Matan Barak20029832015-12-23 14:56:53 +0200579 }
Matan Barakabae1b72015-10-15 18:38:49 +0300580
Matan Barak045959d2015-12-23 14:56:55 +0200581 ret = ib_find_cached_gid_by_port(device, gid, gid_type, port,
Matan Barakb39ffa12015-12-23 14:56:47 +0200582 ndev, NULL);
Matan Barakabae1b72015-10-15 18:38:49 +0300583
584 if (ndev)
585 dev_put(ndev);
Michael Wang7c111472015-05-05 14:50:30 +0200586
587 return ret;
588}
589
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400590static int cma_acquire_dev(struct rdma_id_private *id_priv,
591 struct rdma_id_private *listen_id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700592{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800593 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700594 struct cma_device *cma_dev;
Michael Wang7c111472015-05-05 14:50:30 +0200595 union ib_gid gid, iboe_gid, *gidp;
Sean Heftye51060f2006-06-17 20:37:29 -0700596 int ret = -ENODEV;
Michael Wang7c111472015-05-05 14:50:30 +0200597 u8 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700598
Michael Wang7c111472015-05-05 14:50:30 +0200599 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000600 id_priv->id.ps == RDMA_PS_IPOIB)
601 return -EINVAL;
602
Sean Heftya396d432011-02-23 09:05:39 -0800603 mutex_lock(&lock);
Moni Shoua7b856272013-12-12 18:03:12 +0200604 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
605 &iboe_gid);
606
Eli Cohen3c86aa72010-10-13 21:26:51 +0200607 memcpy(&gid, dev_addr->src_dev_addr +
608 rdma_addr_gid_offset(dev_addr), sizeof gid);
Michael Wang7c111472015-05-05 14:50:30 +0200609
610 if (listen_id_priv) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400611 cma_dev = listen_id_priv->cma_dev;
612 port = listen_id_priv->id.port_num;
Ira Weiny5d9fb042015-05-14 15:01:46 -0400613 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200614 &iboe_gid : &gid;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400615
Matan Barak045959d2015-12-23 14:56:55 +0200616 ret = cma_validate_port(cma_dev->device, port,
617 rdma_protocol_ib(cma_dev->device, port) ?
618 IB_GID_TYPE_IB :
619 listen_id_priv->gid_type, gidp,
Matan Barakabae1b72015-10-15 18:38:49 +0300620 dev_addr->dev_type,
621 dev_addr->bound_dev_if);
Michael Wang7c111472015-05-05 14:50:30 +0200622 if (!ret) {
623 id_priv->id.port_num = port;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400624 goto out;
625 }
626 }
Michael Wang7c111472015-05-05 14:50:30 +0200627
Sean Heftye51060f2006-06-17 20:37:29 -0700628 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200629 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400630 if (listen_id_priv &&
631 listen_id_priv->cma_dev == cma_dev &&
632 listen_id_priv->id.port_num == port)
633 continue;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200634
Ira Weiny5d9fb042015-05-14 15:01:46 -0400635 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200636 &iboe_gid : &gid;
637
Matan Barak045959d2015-12-23 14:56:55 +0200638 ret = cma_validate_port(cma_dev->device, port,
639 rdma_protocol_ib(cma_dev->device, port) ?
640 IB_GID_TYPE_IB :
641 cma_dev->default_gid_type[port - 1],
642 gidp, dev_addr->dev_type,
Matan Barakabae1b72015-10-15 18:38:49 +0300643 dev_addr->bound_dev_if);
Michael Wang7c111472015-05-05 14:50:30 +0200644 if (!ret) {
645 id_priv->id.port_num = port;
646 goto out;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200647 }
Sean Heftye51060f2006-06-17 20:37:29 -0700648 }
649 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200650
651out:
652 if (!ret)
653 cma_attach_to_dev(id_priv, cma_dev);
654
Sean Heftya396d432011-02-23 09:05:39 -0800655 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700656 return ret;
657}
658
Sean Heftyf17df3b2013-05-29 10:09:17 -0700659/*
660 * Select the source IB device and address to reach the destination IB address.
661 */
662static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
663{
664 struct cma_device *cma_dev, *cur_dev;
665 struct sockaddr_ib *addr;
666 union ib_gid gid, sgid, *dgid;
667 u16 pkey, index;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700668 u8 p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700669 int i;
670
671 cma_dev = NULL;
672 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
673 dgid = (union ib_gid *) &addr->sib_addr;
674 pkey = ntohs(addr->sib_pkey);
675
676 list_for_each_entry(cur_dev, &dev_list, list) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700677 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
Michael Wang30a74ef2015-05-05 14:50:39 +0200678 if (!rdma_cap_af_ib(cur_dev->device, p))
Michael Wangfef60902015-05-05 14:50:31 +0200679 continue;
680
Sean Heftyf17df3b2013-05-29 10:09:17 -0700681 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
682 continue;
683
Matan Barak55ee3ab2015-10-15 18:38:45 +0300684 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
685 &gid, NULL);
686 i++) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700687 if (!memcmp(&gid, dgid, sizeof(gid))) {
688 cma_dev = cur_dev;
689 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700690 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700691 goto found;
692 }
693
694 if (!cma_dev && (gid.global.subnet_prefix ==
695 dgid->global.subnet_prefix)) {
696 cma_dev = cur_dev;
697 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700698 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700699 }
700 }
701 }
702 }
703
704 if (!cma_dev)
705 return -ENODEV;
706
707found:
708 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf17df3b2013-05-29 10:09:17 -0700709 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
710 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
711 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
712 return 0;
713}
714
Sean Heftye51060f2006-06-17 20:37:29 -0700715static void cma_deref_id(struct rdma_id_private *id_priv)
716{
717 if (atomic_dec_and_test(&id_priv->refcount))
718 complete(&id_priv->comp);
719}
720
Guy Shapirofa201052015-10-22 15:20:10 +0300721struct rdma_cm_id *rdma_create_id(struct net *net,
722 rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000723 void *context, enum rdma_port_space ps,
724 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700725{
726 struct rdma_id_private *id_priv;
727
728 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
729 if (!id_priv)
730 return ERR_PTR(-ENOMEM);
731
Nir Muchtar83e95022011-01-13 13:56:04 +0000732 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700733 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700734 id_priv->id.context = context;
735 id_priv->id.event_handler = event_handler;
736 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000737 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700738 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700739 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700740 init_completion(&id_priv->comp);
741 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700742 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700743 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800744 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700745 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
Guy Shapirofa201052015-10-22 15:20:10 +0300746 id_priv->id.route.addr.dev_addr.net = get_net(net);
Sean Heftye51060f2006-06-17 20:37:29 -0700747
748 return &id_priv->id;
749}
750EXPORT_SYMBOL(rdma_create_id);
751
Sean Heftyc8f6a362007-02-15 17:00:18 -0800752static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700753{
754 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800755 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700756
Sean Heftyc8f6a362007-02-15 17:00:18 -0800757 qp_attr.qp_state = IB_QPS_INIT;
758 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700759 if (ret)
760 return ret;
761
Sean Heftyc8f6a362007-02-15 17:00:18 -0800762 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
763 if (ret)
764 return ret;
765
766 qp_attr.qp_state = IB_QPS_RTR;
767 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
768 if (ret)
769 return ret;
770
771 qp_attr.qp_state = IB_QPS_RTS;
772 qp_attr.sq_psn = 0;
773 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
774
775 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700776}
777
Sean Heftyc8f6a362007-02-15 17:00:18 -0800778static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500779{
780 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800781 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500782
783 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800784 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
785 if (ret)
786 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500787
Sean Heftyc8f6a362007-02-15 17:00:18 -0800788 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500789}
790
Sean Heftye51060f2006-06-17 20:37:29 -0700791int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
792 struct ib_qp_init_attr *qp_init_attr)
793{
794 struct rdma_id_private *id_priv;
795 struct ib_qp *qp;
796 int ret;
797
798 id_priv = container_of(id, struct rdma_id_private, id);
799 if (id->device != pd->device)
800 return -EINVAL;
801
Christoph Hellwig0691a282016-05-03 18:01:03 +0200802 qp_init_attr->port_num = id->port_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700803 qp = ib_create_qp(pd, qp_init_attr);
804 if (IS_ERR(qp))
805 return PTR_ERR(qp);
806
Sean Heftyb26f9b92010-04-01 17:08:41 +0000807 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800808 ret = cma_init_ud_qp(id_priv, qp);
809 else
810 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700811 if (ret)
812 goto err;
813
814 id->qp = qp;
815 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700816 id_priv->srq = (qp->srq != NULL);
817 return 0;
818err:
819 ib_destroy_qp(qp);
820 return ret;
821}
822EXPORT_SYMBOL(rdma_create_qp);
823
824void rdma_destroy_qp(struct rdma_cm_id *id)
825{
Sean Heftyc5483382007-09-24 13:19:09 -0700826 struct rdma_id_private *id_priv;
827
828 id_priv = container_of(id, struct rdma_id_private, id);
829 mutex_lock(&id_priv->qp_mutex);
830 ib_destroy_qp(id_priv->id.qp);
831 id_priv->id.qp = NULL;
832 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700833}
834EXPORT_SYMBOL(rdma_destroy_qp);
835
Sean Hefty5851bb82008-01-04 10:47:12 -0800836static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
837 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700838{
839 struct ib_qp_attr qp_attr;
840 int qp_attr_mask, ret;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200841 union ib_gid sgid;
Sean Heftye51060f2006-06-17 20:37:29 -0700842
Sean Heftyc5483382007-09-24 13:19:09 -0700843 mutex_lock(&id_priv->qp_mutex);
844 if (!id_priv->id.qp) {
845 ret = 0;
846 goto out;
847 }
Sean Heftye51060f2006-06-17 20:37:29 -0700848
849 /* Need to update QP attributes from default values. */
850 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700851 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700852 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700853 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700854
Sean Heftyc5483382007-09-24 13:19:09 -0700855 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700856 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700857 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700858
859 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700860 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700861 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700862 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700863
Matan Barakdd5f03b2013-12-12 18:03:11 +0200864 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300865 qp_attr.ah_attr.grh.sgid_index, &sgid, NULL);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200866 if (ret)
867 goto out;
868
Michael Wangfef60902015-05-05 14:50:31 +0200869 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
870
Sean Hefty5851bb82008-01-04 10:47:12 -0800871 if (conn_param)
872 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700873 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
874out:
875 mutex_unlock(&id_priv->qp_mutex);
876 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700877}
878
Sean Hefty5851bb82008-01-04 10:47:12 -0800879static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
880 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700881{
882 struct ib_qp_attr qp_attr;
883 int qp_attr_mask, ret;
884
Sean Heftyc5483382007-09-24 13:19:09 -0700885 mutex_lock(&id_priv->qp_mutex);
886 if (!id_priv->id.qp) {
887 ret = 0;
888 goto out;
889 }
Sean Heftye51060f2006-06-17 20:37:29 -0700890
891 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700892 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700893 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700894 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700895
Sean Hefty5851bb82008-01-04 10:47:12 -0800896 if (conn_param)
897 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700898 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
899out:
900 mutex_unlock(&id_priv->qp_mutex);
901 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700902}
903
Sean Heftyc5483382007-09-24 13:19:09 -0700904static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700905{
906 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700907 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700908
Sean Heftyc5483382007-09-24 13:19:09 -0700909 mutex_lock(&id_priv->qp_mutex);
910 if (!id_priv->id.qp) {
911 ret = 0;
912 goto out;
913 }
Sean Heftye51060f2006-06-17 20:37:29 -0700914
915 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700916 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
917out:
918 mutex_unlock(&id_priv->qp_mutex);
919 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700920}
921
Sean Heftyc8f6a362007-02-15 17:00:18 -0800922static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
923 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
924{
925 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
926 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200927 u16 pkey;
928
Michael Wang227128f2015-05-05 14:50:40 +0200929 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
Eli Cohen3c86aa72010-10-13 21:26:51 +0200930 pkey = 0xffff;
Michael Wangfef60902015-05-05 14:50:31 +0200931 else
932 pkey = ib_addr_get_pkey(dev_addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800933
934 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200935 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800936 if (ret)
937 return ret;
938
939 qp_attr->port_num = id_priv->id.port_num;
940 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
941
Sean Heftyb26f9b92010-04-01 17:08:41 +0000942 if (id_priv->id.qp_type == IB_QPT_UD) {
Sean Hefty5c438132013-05-29 10:09:23 -0700943 ret = cma_set_qkey(id_priv, 0);
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700944 if (ret)
945 return ret;
946
Sean Heftyc8f6a362007-02-15 17:00:18 -0800947 qp_attr->qkey = id_priv->qkey;
948 *qp_attr_mask |= IB_QP_QKEY;
949 } else {
950 qp_attr->qp_access_flags = 0;
951 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
952 }
953 return 0;
954}
955
Sean Heftye51060f2006-06-17 20:37:29 -0700956int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
957 int *qp_attr_mask)
958{
959 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800960 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700961
962 id_priv = container_of(id, struct rdma_id_private, id);
Michael Wang72219cea2015-05-05 14:50:34 +0200963 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +0000964 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800965 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
966 else
967 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
968 qp_attr_mask);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200969
Sean Heftye51060f2006-06-17 20:37:29 -0700970 if (qp_attr->qp_state == IB_QPS_RTR)
971 qp_attr->rq_psn = id_priv->seq_num;
Michael Wang04215332015-05-05 14:50:35 +0200972 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800973 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300974 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800975 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
976 } else
977 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
978 qp_attr_mask);
Michael Wang21655af2015-05-05 14:50:27 +0200979 } else
Sean Heftye51060f2006-06-17 20:37:29 -0700980 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -0700981
982 return ret;
983}
984EXPORT_SYMBOL(rdma_init_qp_attr);
985
986static inline int cma_zero_addr(struct sockaddr *addr)
987{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700988 switch (addr->sa_family) {
989 case AF_INET:
990 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
991 case AF_INET6:
992 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
993 case AF_IB:
994 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
995 default:
996 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700997 }
998}
999
1000static inline int cma_loopback_addr(struct sockaddr *addr)
1001{
Sean Hefty2e2d1902013-05-29 10:09:09 -07001002 switch (addr->sa_family) {
1003 case AF_INET:
1004 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
1005 case AF_INET6:
1006 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
1007 case AF_IB:
1008 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
1009 default:
1010 return 0;
1011 }
Sean Heftye51060f2006-06-17 20:37:29 -07001012}
1013
1014static inline int cma_any_addr(struct sockaddr *addr)
1015{
1016 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1017}
1018
Hefty, Sean43b752d2011-05-09 22:06:10 -07001019static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
1020{
1021 if (src->sa_family != dst->sa_family)
1022 return -1;
1023
1024 switch (src->sa_family) {
1025 case AF_INET:
1026 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
1027 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
Sean Hefty2e2d1902013-05-29 10:09:09 -07001028 case AF_INET6:
Hefty, Sean43b752d2011-05-09 22:06:10 -07001029 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
1030 &((struct sockaddr_in6 *) dst)->sin6_addr);
Sean Hefty2e2d1902013-05-29 10:09:09 -07001031 default:
1032 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1033 &((struct sockaddr_ib *) dst)->sib_addr);
Hefty, Sean43b752d2011-05-09 22:06:10 -07001034 }
1035}
1036
Sean Hefty58afdcb2013-05-29 10:09:11 -07001037static __be16 cma_port(struct sockaddr *addr)
Sean Hefty628e5f62006-11-30 16:44:16 -08001038{
Sean Hefty58afdcb2013-05-29 10:09:11 -07001039 struct sockaddr_ib *sib;
1040
1041 switch (addr->sa_family) {
1042 case AF_INET:
Sean Hefty628e5f62006-11-30 16:44:16 -08001043 return ((struct sockaddr_in *) addr)->sin_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -07001044 case AF_INET6:
Sean Hefty628e5f62006-11-30 16:44:16 -08001045 return ((struct sockaddr_in6 *) addr)->sin6_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -07001046 case AF_IB:
1047 sib = (struct sockaddr_ib *) addr;
1048 return htons((u16) (be64_to_cpu(sib->sib_sid) &
1049 be64_to_cpu(sib->sib_sid_mask)));
1050 default:
1051 return 0;
1052 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001053}
1054
Sean Heftye51060f2006-06-17 20:37:29 -07001055static inline int cma_any_port(struct sockaddr *addr)
1056{
Sean Hefty628e5f62006-11-30 16:44:16 -08001057 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -07001058}
1059
Haggai Eran0c505f72015-07-30 17:50:19 +03001060static void cma_save_ib_info(struct sockaddr *src_addr,
1061 struct sockaddr *dst_addr,
1062 struct rdma_cm_id *listen_id,
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001063 struct ib_sa_path_rec *path)
Sean Heftye51060f2006-06-17 20:37:29 -07001064{
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001065 struct sockaddr_ib *listen_ib, *ib;
Sean Heftye51060f2006-06-17 20:37:29 -07001066
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001067 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001068 if (src_addr) {
1069 ib = (struct sockaddr_ib *)src_addr;
1070 ib->sib_family = AF_IB;
1071 if (path) {
1072 ib->sib_pkey = path->pkey;
1073 ib->sib_flowinfo = path->flow_label;
1074 memcpy(&ib->sib_addr, &path->sgid, 16);
1075 ib->sib_sid = path->service_id;
1076 ib->sib_scope_id = 0;
1077 } else {
1078 ib->sib_pkey = listen_ib->sib_pkey;
1079 ib->sib_flowinfo = listen_ib->sib_flowinfo;
1080 ib->sib_addr = listen_ib->sib_addr;
1081 ib->sib_sid = listen_ib->sib_sid;
1082 ib->sib_scope_id = listen_ib->sib_scope_id;
1083 }
1084 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
Matthew Finlayc07678b2015-05-19 00:11:48 -07001085 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001086 if (dst_addr) {
1087 ib = (struct sockaddr_ib *)dst_addr;
1088 ib->sib_family = AF_IB;
1089 if (path) {
1090 ib->sib_pkey = path->pkey;
1091 ib->sib_flowinfo = path->flow_label;
1092 memcpy(&ib->sib_addr, &path->dgid, 16);
1093 }
Matthew Finlayc07678b2015-05-19 00:11:48 -07001094 }
Sean Heftye51060f2006-06-17 20:37:29 -07001095}
1096
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001097static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1098 struct sockaddr_in *dst_addr,
Haggai Eran0c505f72015-07-30 17:50:19 +03001099 struct cma_hdr *hdr,
1100 __be16 local_port)
Sean Heftye51060f2006-06-17 20:37:29 -07001101{
Haggai Eran0c505f72015-07-30 17:50:19 +03001102 if (src_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001103 *src_addr = (struct sockaddr_in) {
1104 .sin_family = AF_INET,
1105 .sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1106 .sin_port = local_port,
1107 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001108 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001109
Haggai Eran0c505f72015-07-30 17:50:19 +03001110 if (dst_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001111 *dst_addr = (struct sockaddr_in) {
1112 .sin_family = AF_INET,
1113 .sin_addr.s_addr = hdr->src_addr.ip4.addr,
1114 .sin_port = hdr->port,
1115 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001116 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001117}
1118
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001119static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1120 struct sockaddr_in6 *dst_addr,
Haggai Eran0c505f72015-07-30 17:50:19 +03001121 struct cma_hdr *hdr,
1122 __be16 local_port)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001123{
Haggai Eran0c505f72015-07-30 17:50:19 +03001124 if (src_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001125 *src_addr = (struct sockaddr_in6) {
1126 .sin6_family = AF_INET6,
1127 .sin6_addr = hdr->dst_addr.ip6,
1128 .sin6_port = local_port,
1129 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001130 }
Sean Heftye51060f2006-06-17 20:37:29 -07001131
Haggai Eran0c505f72015-07-30 17:50:19 +03001132 if (dst_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001133 *dst_addr = (struct sockaddr_in6) {
1134 .sin6_family = AF_INET6,
1135 .sin6_addr = hdr->src_addr.ip6,
1136 .sin6_port = hdr->port,
1137 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001138 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001139}
1140
Haggai Eran0c505f72015-07-30 17:50:19 +03001141static u16 cma_port_from_service_id(__be64 service_id)
1142{
1143 return (u16)be64_to_cpu(service_id);
1144}
1145
1146static int cma_save_ip_info(struct sockaddr *src_addr,
1147 struct sockaddr *dst_addr,
1148 struct ib_cm_event *ib_event,
1149 __be64 service_id)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001150{
1151 struct cma_hdr *hdr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001152 __be16 port;
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001153
1154 hdr = ib_event->private_data;
1155 if (hdr->cma_version != CMA_VERSION)
1156 return -EINVAL;
1157
Haggai Eran0c505f72015-07-30 17:50:19 +03001158 port = htons(cma_port_from_service_id(service_id));
1159
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001160 switch (cma_get_ip_ver(hdr)) {
1161 case 4:
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001162 cma_save_ip4_info((struct sockaddr_in *)src_addr,
1163 (struct sockaddr_in *)dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001164 break;
1165 case 6:
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001166 cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1167 (struct sockaddr_in6 *)dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001168 break;
1169 default:
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001170 return -EAFNOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07001171 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001172
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001173 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001174}
1175
Haggai Eran0c505f72015-07-30 17:50:19 +03001176static int cma_save_net_info(struct sockaddr *src_addr,
1177 struct sockaddr *dst_addr,
1178 struct rdma_cm_id *listen_id,
1179 struct ib_cm_event *ib_event,
1180 sa_family_t sa_family, __be64 service_id)
1181{
1182 if (sa_family == AF_IB) {
1183 if (ib_event->event == IB_CM_REQ_RECEIVED)
1184 cma_save_ib_info(src_addr, dst_addr, listen_id,
1185 ib_event->param.req_rcvd.primary_path);
1186 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1187 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1188 return 0;
1189 }
1190
1191 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1192}
1193
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001194static int cma_save_req_info(const struct ib_cm_event *ib_event,
1195 struct cma_req_info *req)
1196{
1197 const struct ib_cm_req_event_param *req_param =
1198 &ib_event->param.req_rcvd;
1199 const struct ib_cm_sidr_req_event_param *sidr_param =
1200 &ib_event->param.sidr_req_rcvd;
1201
1202 switch (ib_event->event) {
1203 case IB_CM_REQ_RECEIVED:
1204 req->device = req_param->listen_id->device;
1205 req->port = req_param->port;
1206 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1207 sizeof(req->local_gid));
1208 req->has_gid = true;
1209 req->service_id = req_param->primary_path->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001210 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
Haggai Eran84424a72016-02-29 15:45:12 +02001211 if (req->pkey != req_param->bth_pkey)
1212 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1213 "RDMA CMA: in the future this may cause the request to be dropped\n",
1214 req_param->bth_pkey, req->pkey);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001215 break;
1216 case IB_CM_SIDR_REQ_RECEIVED:
1217 req->device = sidr_param->listen_id->device;
1218 req->port = sidr_param->port;
1219 req->has_gid = false;
1220 req->service_id = sidr_param->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001221 req->pkey = sidr_param->pkey;
Haggai Eran84424a72016-02-29 15:45:12 +02001222 if (req->pkey != sidr_param->bth_pkey)
1223 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1224 "RDMA CMA: in the future this may cause the request to be dropped\n",
1225 sidr_param->bth_pkey, req->pkey);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001226 break;
1227 default:
1228 return -EINVAL;
1229 }
1230
1231 return 0;
1232}
1233
Haggai Eranf887f2a2015-07-30 17:50:23 +03001234static bool validate_ipv4_net_dev(struct net_device *net_dev,
1235 const struct sockaddr_in *dst_addr,
1236 const struct sockaddr_in *src_addr)
1237{
1238 __be32 daddr = dst_addr->sin_addr.s_addr,
1239 saddr = src_addr->sin_addr.s_addr;
1240 struct fib_result res;
1241 struct flowi4 fl4;
1242 int err;
1243 bool ret;
1244
1245 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1246 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1247 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1248 ipv4_is_loopback(saddr))
1249 return false;
1250
1251 memset(&fl4, 0, sizeof(fl4));
1252 fl4.flowi4_iif = net_dev->ifindex;
1253 fl4.daddr = daddr;
1254 fl4.saddr = saddr;
1255
1256 rcu_read_lock();
1257 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
Bart Van Assched3632492015-11-20 11:04:12 -08001258 ret = err == 0 && FIB_RES_DEV(res) == net_dev;
Haggai Eranf887f2a2015-07-30 17:50:23 +03001259 rcu_read_unlock();
1260
1261 return ret;
1262}
1263
1264static bool validate_ipv6_net_dev(struct net_device *net_dev,
1265 const struct sockaddr_in6 *dst_addr,
1266 const struct sockaddr_in6 *src_addr)
1267{
1268#if IS_ENABLED(CONFIG_IPV6)
1269 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1270 IPV6_ADDR_LINKLOCAL;
1271 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1272 &src_addr->sin6_addr, net_dev->ifindex,
1273 strict);
1274 bool ret;
1275
1276 if (!rt)
1277 return false;
1278
1279 ret = rt->rt6i_idev->dev == net_dev;
1280 ip6_rt_put(rt);
1281
1282 return ret;
1283#else
1284 return false;
1285#endif
1286}
1287
1288static bool validate_net_dev(struct net_device *net_dev,
1289 const struct sockaddr *daddr,
1290 const struct sockaddr *saddr)
1291{
1292 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1293 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1294 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1295 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1296
1297 switch (daddr->sa_family) {
1298 case AF_INET:
1299 return saddr->sa_family == AF_INET &&
1300 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1301
1302 case AF_INET6:
1303 return saddr->sa_family == AF_INET6 &&
1304 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1305
1306 default:
1307 return false;
1308 }
1309}
1310
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001311static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1312 const struct cma_req_info *req)
1313{
Haggai Eranf887f2a2015-07-30 17:50:23 +03001314 struct sockaddr_storage listen_addr_storage, src_addr_storage;
1315 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1316 *src_addr = (struct sockaddr *)&src_addr_storage;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001317 struct net_device *net_dev;
1318 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1319 int err;
1320
Haggai Eranf887f2a2015-07-30 17:50:23 +03001321 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1322 req->service_id);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001323 if (err)
1324 return ERR_PTR(err);
1325
1326 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1327 gid, listen_addr);
1328 if (!net_dev)
1329 return ERR_PTR(-ENODEV);
1330
Haggai Eranf887f2a2015-07-30 17:50:23 +03001331 if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1332 dev_put(net_dev);
1333 return ERR_PTR(-EHOSTUNREACH);
1334 }
1335
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001336 return net_dev;
1337}
1338
1339static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1340{
1341 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1342}
1343
1344static bool cma_match_private_data(struct rdma_id_private *id_priv,
1345 const struct cma_hdr *hdr)
1346{
1347 struct sockaddr *addr = cma_src_addr(id_priv);
1348 __be32 ip4_addr;
1349 struct in6_addr ip6_addr;
1350
1351 if (cma_any_addr(addr) && !id_priv->afonly)
1352 return true;
1353
1354 switch (addr->sa_family) {
1355 case AF_INET:
1356 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1357 if (cma_get_ip_ver(hdr) != 4)
1358 return false;
1359 if (!cma_any_addr(addr) &&
1360 hdr->dst_addr.ip4.addr != ip4_addr)
1361 return false;
1362 break;
1363 case AF_INET6:
1364 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1365 if (cma_get_ip_ver(hdr) != 6)
1366 return false;
1367 if (!cma_any_addr(addr) &&
1368 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1369 return false;
1370 break;
1371 case AF_IB:
1372 return true;
1373 default:
1374 return false;
1375 }
1376
1377 return true;
1378}
1379
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001380static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1381{
1382 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1383 enum rdma_transport_type transport =
1384 rdma_node_get_transport(device->node_type);
1385
1386 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1387}
1388
1389static bool cma_protocol_roce(const struct rdma_cm_id *id)
1390{
1391 struct ib_device *device = id->device;
1392 const int port_num = id->port_num ?: rdma_start_port(device);
1393
1394 return cma_protocol_roce_dev_port(device, port_num);
1395}
1396
Matan Barakfac51592015-12-21 17:01:24 +02001397static bool cma_match_net_dev(const struct rdma_cm_id *id,
1398 const struct net_device *net_dev,
1399 u8 port_num)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001400{
Matan Barakfac51592015-12-21 17:01:24 +02001401 const struct rdma_addr *addr = &id->route.addr;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001402
1403 if (!net_dev)
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001404 /* This request is an AF_IB request or a RoCE request */
Matan Barakfac51592015-12-21 17:01:24 +02001405 return (!id->port_num || id->port_num == port_num) &&
1406 (addr->src_addr.ss_family == AF_IB ||
1407 cma_protocol_roce_dev_port(id->device, port_num));
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001408
1409 return !addr->dev_addr.bound_dev_if ||
Guy Shapirofa201052015-10-22 15:20:10 +03001410 (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001411 addr->dev_addr.bound_dev_if == net_dev->ifindex);
1412}
1413
1414static struct rdma_id_private *cma_find_listener(
1415 const struct rdma_bind_list *bind_list,
1416 const struct ib_cm_id *cm_id,
1417 const struct ib_cm_event *ib_event,
1418 const struct cma_req_info *req,
1419 const struct net_device *net_dev)
1420{
1421 struct rdma_id_private *id_priv, *id_priv_dev;
1422
1423 if (!bind_list)
1424 return ERR_PTR(-EINVAL);
1425
1426 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1427 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1428 if (id_priv->id.device == cm_id->device &&
Matan Barakfac51592015-12-21 17:01:24 +02001429 cma_match_net_dev(&id_priv->id, net_dev, req->port))
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001430 return id_priv;
1431 list_for_each_entry(id_priv_dev,
1432 &id_priv->listen_list,
1433 listen_list) {
1434 if (id_priv_dev->id.device == cm_id->device &&
Matan Barakfac51592015-12-21 17:01:24 +02001435 cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001436 return id_priv_dev;
1437 }
1438 }
1439 }
1440
1441 return ERR_PTR(-EINVAL);
1442}
1443
1444static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001445 struct ib_cm_event *ib_event,
1446 struct net_device **net_dev)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001447{
1448 struct cma_req_info req;
1449 struct rdma_bind_list *bind_list;
1450 struct rdma_id_private *id_priv;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001451 int err;
1452
1453 err = cma_save_req_info(ib_event, &req);
1454 if (err)
1455 return ERR_PTR(err);
1456
Haggai Eran0b3ca762015-07-30 17:50:24 +03001457 *net_dev = cma_get_net_dev(ib_event, &req);
1458 if (IS_ERR(*net_dev)) {
1459 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001460 /* Assuming the protocol is AF_IB */
Haggai Eran0b3ca762015-07-30 17:50:24 +03001461 *net_dev = NULL;
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001462 } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1463 /* TODO find the net dev matching the request parameters
1464 * through the RoCE GID table */
1465 *net_dev = NULL;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001466 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001467 return ERR_CAST(*net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001468 }
1469 }
1470
Guy Shapirofa201052015-10-22 15:20:10 +03001471 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
Haggai Eran4be74b42015-10-22 15:20:09 +03001472 rdma_ps_from_service_id(req.service_id),
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001473 cma_port_from_service_id(req.service_id));
Haggai Eran0b3ca762015-07-30 17:50:24 +03001474 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
Haggai Eranb3b51f92015-09-21 16:02:02 +03001475 if (IS_ERR(id_priv) && *net_dev) {
Haggai Eranbe688192015-08-27 15:55:15 +03001476 dev_put(*net_dev);
1477 *net_dev = NULL;
1478 }
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001479
1480 return id_priv;
1481}
1482
Sean Heftye8160e12013-05-29 10:09:22 -07001483static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07001484{
Sean Heftye8160e12013-05-29 10:09:22 -07001485 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
Sean Heftye51060f2006-06-17 20:37:29 -07001486}
1487
Sean Heftye51060f2006-06-17 20:37:29 -07001488static void cma_cancel_route(struct rdma_id_private *id_priv)
1489{
Michael Wangfe53ba22015-05-05 14:50:36 +02001490 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001491 if (id_priv->query)
1492 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001493 }
1494}
1495
Sean Heftye51060f2006-06-17 20:37:29 -07001496static void cma_cancel_listens(struct rdma_id_private *id_priv)
1497{
1498 struct rdma_id_private *dev_id_priv;
1499
Sean Heftyd02d1f52007-10-09 11:12:34 -07001500 /*
1501 * Remove from listen_any_list to prevent added devices from spawning
1502 * additional listen requests.
1503 */
Sean Heftye51060f2006-06-17 20:37:29 -07001504 mutex_lock(&lock);
1505 list_del(&id_priv->list);
1506
1507 while (!list_empty(&id_priv->listen_list)) {
1508 dev_id_priv = list_entry(id_priv->listen_list.next,
1509 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001510 /* sync with device removal to avoid duplicate destruction */
1511 list_del_init(&dev_id_priv->list);
1512 list_del(&dev_id_priv->listen_list);
1513 mutex_unlock(&lock);
1514
1515 rdma_destroy_id(&dev_id_priv->id);
1516 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001517 }
1518 mutex_unlock(&lock);
1519}
1520
1521static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001522 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -07001523{
1524 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001525 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001526 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1527 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001528 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001529 cma_cancel_route(id_priv);
1530 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001531 case RDMA_CM_LISTEN:
Sean Heftyf4753832013-05-29 10:09:14 -07001532 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001533 cma_cancel_listens(id_priv);
1534 break;
1535 default:
1536 break;
1537 }
1538}
1539
1540static void cma_release_port(struct rdma_id_private *id_priv)
1541{
1542 struct rdma_bind_list *bind_list = id_priv->bind_list;
Guy Shapirofa201052015-10-22 15:20:10 +03001543 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07001544
1545 if (!bind_list)
1546 return;
1547
1548 mutex_lock(&lock);
1549 hlist_del(&id_priv->node);
1550 if (hlist_empty(&bind_list->owners)) {
Guy Shapirofa201052015-10-22 15:20:10 +03001551 cma_ps_remove(net, bind_list->ps, bind_list->port);
Sean Heftye51060f2006-06-17 20:37:29 -07001552 kfree(bind_list);
1553 }
1554 mutex_unlock(&lock);
1555}
1556
Sean Heftyc8f6a362007-02-15 17:00:18 -08001557static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1558{
1559 struct cma_multicast *mc;
1560
1561 while (!list_empty(&id_priv->mc_list)) {
1562 mc = container_of(id_priv->mc_list.next,
1563 struct cma_multicast, list);
1564 list_del(&mc->list);
Michael Wanga31ad3b2015-05-05 14:50:37 +02001565 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
Michael Wang5c9a5282015-05-05 14:50:29 +02001566 id_priv->id.port_num)) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02001567 ib_sa_free_multicast(mc->multicast.ib);
1568 kfree(mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02001569 } else {
1570 if (mc->igmp_joined) {
1571 struct rdma_dev_addr *dev_addr =
1572 &id_priv->id.route.addr.dev_addr;
1573 struct net_device *ndev = NULL;
1574
1575 if (dev_addr->bound_dev_if)
1576 ndev = dev_get_by_index(&init_net,
1577 dev_addr->bound_dev_if);
1578 if (ndev) {
1579 cma_igmp_send(ndev,
1580 &mc->multicast.ib->rec.mgid,
1581 false);
1582 dev_put(ndev);
1583 }
1584 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02001585 kref_put(&mc->mcref, release_mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02001586 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001587 }
1588}
1589
Sean Heftye51060f2006-06-17 20:37:29 -07001590void rdma_destroy_id(struct rdma_cm_id *id)
1591{
1592 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001593 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -07001594
1595 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001596 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001597 cma_cancel_operation(id_priv, state);
1598
Sean Heftya396d432011-02-23 09:05:39 -08001599 /*
1600 * Wait for any active callback to finish. New callbacks will find
1601 * the id_priv state set to destroying and abort.
1602 */
1603 mutex_lock(&id_priv->handler_mutex);
1604 mutex_unlock(&id_priv->handler_mutex);
1605
Sean Heftye51060f2006-06-17 20:37:29 -07001606 if (id_priv->cma_dev) {
Michael Wang72219cea2015-05-05 14:50:34 +02001607 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001608 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07001609 ib_destroy_cm_id(id_priv->cm_id.ib);
Michael Wang04215332015-05-05 14:50:35 +02001610 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001611 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -05001612 iw_destroy_cm_id(id_priv->cm_id.iw);
Sean Heftye51060f2006-06-17 20:37:29 -07001613 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001614 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -08001615 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001616 }
1617
1618 cma_release_port(id_priv);
1619 cma_deref_id(id_priv);
1620 wait_for_completion(&id_priv->comp);
1621
Sean Heftyd02d1f52007-10-09 11:12:34 -07001622 if (id_priv->internal_id)
1623 cma_deref_id(id_priv->id.context);
1624
Sean Heftye51060f2006-06-17 20:37:29 -07001625 kfree(id_priv->id.route.path_rec);
Guy Shapirofa201052015-10-22 15:20:10 +03001626 put_net(id_priv->id.route.addr.dev_addr.net);
Sean Heftye51060f2006-06-17 20:37:29 -07001627 kfree(id_priv);
1628}
1629EXPORT_SYMBOL(rdma_destroy_id);
1630
1631static int cma_rep_recv(struct rdma_id_private *id_priv)
1632{
1633 int ret;
1634
Sean Hefty5851bb82008-01-04 10:47:12 -08001635 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001636 if (ret)
1637 goto reject;
1638
Sean Hefty5851bb82008-01-04 10:47:12 -08001639 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001640 if (ret)
1641 goto reject;
1642
1643 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1644 if (ret)
1645 goto reject;
1646
1647 return 0;
1648reject:
Sean Heftyc5483382007-09-24 13:19:09 -07001649 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001650 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1651 NULL, 0, NULL, 0);
1652 return ret;
1653}
1654
Sean Heftya1b1b612006-11-30 16:33:14 -08001655static void cma_set_rep_event_data(struct rdma_cm_event *event,
1656 struct ib_cm_rep_event_param *rep_data,
1657 void *private_data)
1658{
1659 event->param.conn.private_data = private_data;
1660 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1661 event->param.conn.responder_resources = rep_data->responder_resources;
1662 event->param.conn.initiator_depth = rep_data->initiator_depth;
1663 event->param.conn.flow_control = rep_data->flow_control;
1664 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1665 event->param.conn.srq = rep_data->srq;
1666 event->param.conn.qp_num = rep_data->remote_qpn;
1667}
1668
Sean Heftye51060f2006-06-17 20:37:29 -07001669static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1670{
1671 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001672 struct rdma_cm_event event;
1673 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001674
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001675 mutex_lock(&id_priv->handler_mutex);
Amir Vadai38ca83a2008-07-22 14:14:23 -07001676 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001677 id_priv->state != RDMA_CM_CONNECT) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001678 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001679 id_priv->state != RDMA_CM_DISCONNECT))
1680 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07001681
Sean Heftya1b1b612006-11-30 16:33:14 -08001682 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001683 switch (ib_event->event) {
1684 case IB_CM_REQ_ERROR:
1685 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001686 event.event = RDMA_CM_EVENT_UNREACHABLE;
1687 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001688 break;
1689 case IB_CM_REP_RECEIVED:
Sean Hefty01602f12013-05-29 10:09:20 -07001690 if (id_priv->id.qp) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001691 event.status = cma_rep_recv(id_priv);
1692 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1693 RDMA_CM_EVENT_ESTABLISHED;
Sean Hefty01602f12013-05-29 10:09:20 -07001694 } else {
Sean Heftya1b1b612006-11-30 16:33:14 -08001695 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
Sean Hefty01602f12013-05-29 10:09:20 -07001696 }
Sean Heftya1b1b612006-11-30 16:33:14 -08001697 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1698 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001699 break;
1700 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001701 case IB_CM_USER_ESTABLISHED:
1702 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001703 break;
1704 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001705 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001706 case IB_CM_DREQ_RECEIVED:
1707 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001708 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1709 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001710 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001711 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001712 break;
1713 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001714 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1715 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001716 case IB_CM_MRA_RECEIVED:
1717 /* ignore event */
1718 goto out;
1719 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001720 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001721 event.status = ib_event->param.rej_rcvd.reason;
1722 event.event = RDMA_CM_EVENT_REJECTED;
1723 event.param.conn.private_data = ib_event->private_data;
1724 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001725 break;
1726 default:
Parav Panditaba25a3e2016-03-02 00:50:29 +05301727 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001728 ib_event->event);
1729 goto out;
1730 }
1731
Sean Heftya1b1b612006-11-30 16:33:14 -08001732 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001733 if (ret) {
1734 /* Destroy the CM ID by returning a non-zero value. */
1735 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001736 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001737 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001738 rdma_destroy_id(&id_priv->id);
1739 return ret;
1740 }
1741out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001742 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001743 return ret;
1744}
1745
Sean Hefty628e5f62006-11-30 16:44:16 -08001746static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001747 struct ib_cm_event *ib_event,
1748 struct net_device *net_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001749{
1750 struct rdma_id_private *id_priv;
1751 struct rdma_cm_id *id;
1752 struct rdma_route *rt;
Haggai Eran0c505f72015-07-30 17:50:19 +03001753 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1754 const __be64 service_id =
1755 ib_event->param.req_rcvd.primary_path->service_id;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001756 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001757
Guy Shapirofa201052015-10-22 15:20:10 +03001758 id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1759 listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001760 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001761 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001762 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001763
Sean Heftyf4753832013-05-29 10:09:14 -07001764 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001765 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1766 (struct sockaddr *)&id->route.addr.dst_addr,
1767 listen_id, ib_event, ss_family, service_id))
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001768 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001769
1770 rt = &id->route;
1771 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1772 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1773 GFP_KERNEL);
1774 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001775 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001776
Sean Heftye51060f2006-06-17 20:37:29 -07001777 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1778 if (rt->num_paths == 2)
1779 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1780
Haggai Eran0b3ca762015-07-30 17:50:24 +03001781 if (net_dev) {
1782 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001783 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001784 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001785 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001786 if (!cma_protocol_roce(listen_id) &&
1787 cma_any_addr(cma_src_addr(id_priv))) {
1788 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1789 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1790 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1791 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
1792 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1793 if (ret)
1794 goto err;
1795 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001796 }
1797 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001798
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001799 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001800 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001801
Krishna Kumar3f168d22006-09-29 12:09:51 -07001802err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001803 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001804 return NULL;
1805}
1806
Sean Hefty628e5f62006-11-30 16:44:16 -08001807static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001808 struct ib_cm_event *ib_event,
1809 struct net_device *net_dev)
Sean Hefty628e5f62006-11-30 16:44:16 -08001810{
1811 struct rdma_id_private *id_priv;
1812 struct rdma_cm_id *id;
Haggai Eran0c505f72015-07-30 17:50:19 +03001813 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
Guy Shapirofa201052015-10-22 15:20:10 +03001814 struct net *net = listen_id->route.addr.dev_addr.net;
Sean Hefty628e5f62006-11-30 16:44:16 -08001815 int ret;
1816
Guy Shapirofa201052015-10-22 15:20:10 +03001817 id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001818 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001819 if (IS_ERR(id))
1820 return NULL;
1821
Sean Heftyf4753832013-05-29 10:09:14 -07001822 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001823 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1824 (struct sockaddr *)&id->route.addr.dst_addr,
1825 listen_id, ib_event, ss_family,
1826 ib_event->param.sidr_req_rcvd.service_id))
Sean Hefty628e5f62006-11-30 16:44:16 -08001827 goto err;
1828
Haggai Eran0b3ca762015-07-30 17:50:24 +03001829 if (net_dev) {
1830 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001831 if (ret)
1832 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001833 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001834 if (!cma_any_addr(cma_src_addr(id_priv))) {
1835 ret = cma_translate_addr(cma_src_addr(id_priv),
1836 &id->route.addr.dev_addr);
1837 if (ret)
1838 goto err;
1839 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001840 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001841
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001842 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001843 return id_priv;
1844err:
1845 rdma_destroy_id(id);
1846 return NULL;
1847}
1848
Sean Heftya1b1b612006-11-30 16:33:14 -08001849static void cma_set_req_event_data(struct rdma_cm_event *event,
1850 struct ib_cm_req_event_param *req_data,
1851 void *private_data, int offset)
1852{
1853 event->param.conn.private_data = private_data + offset;
1854 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1855 event->param.conn.responder_resources = req_data->responder_resources;
1856 event->param.conn.initiator_depth = req_data->initiator_depth;
1857 event->param.conn.flow_control = req_data->flow_control;
1858 event->param.conn.retry_count = req_data->retry_count;
1859 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1860 event->param.conn.srq = req_data->srq;
1861 event->param.conn.qp_num = req_data->remote_qpn;
1862}
1863
Hefty, Sean95954802011-10-06 09:32:33 -07001864static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1865{
Sean Hefty4dd81e82012-06-14 20:49:09 +00001866 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
Hefty, Sean95954802011-10-06 09:32:33 -07001867 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1868 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1869 (id->qp_type == IB_QPT_UD)) ||
1870 (!id->qp_type));
1871}
1872
Sean Heftye51060f2006-06-17 20:37:29 -07001873static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1874{
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001875 struct rdma_id_private *listen_id, *conn_id = NULL;
Sean Heftya1b1b612006-11-30 16:33:14 -08001876 struct rdma_cm_event event;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001877 struct net_device *net_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07001878 int offset, ret;
1879
Haggai Eran0b3ca762015-07-30 17:50:24 +03001880 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001881 if (IS_ERR(listen_id))
1882 return PTR_ERR(listen_id);
1883
Haggai Eran0b3ca762015-07-30 17:50:24 +03001884 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1885 ret = -EINVAL;
1886 goto net_dev_put;
1887 }
Hefty, Sean95954802011-10-06 09:32:33 -07001888
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001889 mutex_lock(&listen_id->handler_mutex);
1890 if (listen_id->state != RDMA_CM_LISTEN) {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001891 ret = -ECONNABORTED;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001892 goto err1;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001893 }
Sean Heftye51060f2006-06-17 20:37:29 -07001894
Sean Hefty628e5f62006-11-30 16:44:16 -08001895 memset(&event, 0, sizeof event);
Sean Heftye8160e12013-05-29 10:09:22 -07001896 offset = cma_user_data_offset(listen_id);
Sean Hefty628e5f62006-11-30 16:44:16 -08001897 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001898 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001899 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001900 event.param.ud.private_data = ib_event->private_data + offset;
1901 event.param.ud.private_data_len =
1902 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1903 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001904 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001905 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1906 ib_event->private_data, offset);
1907 }
Sean Heftye51060f2006-06-17 20:37:29 -07001908 if (!conn_id) {
1909 ret = -ENOMEM;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001910 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001911 }
1912
Or Gerlitzde910bd2008-07-14 23:48:53 -07001913 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001914 ret = cma_acquire_dev(conn_id, listen_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301915 if (ret)
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001916 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001917
1918 conn_id->cm_id.ib = cm_id;
1919 cm_id->context = conn_id;
1920 cm_id->cm_handler = cma_ib_handler;
1921
Sean Hefty25ae21a2011-02-23 08:11:32 -08001922 /*
1923 * Protect against the user destroying conn_id from another thread
1924 * until we're done accessing it.
1925 */
1926 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001927 ret = conn_id->id.event_handler(&conn_id->id, &event);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001928 if (ret)
1929 goto err3;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001930 /*
1931 * Acquire mutex to prevent user executing rdma_destroy_id()
1932 * while we're accessing the cm_id.
1933 */
1934 mutex_lock(&lock);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001935 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1936 (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001937 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1938 mutex_unlock(&lock);
1939 mutex_unlock(&conn_id->handler_mutex);
1940 mutex_unlock(&listen_id->handler_mutex);
1941 cma_deref_id(conn_id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001942 if (net_dev)
1943 dev_put(net_dev);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001944 return 0;
1945
1946err3:
1947 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301948 /* Destroy the CM ID by returning a non-zero value. */
1949 conn_id->cm_id.ib = NULL;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001950err2:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001951 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001952 mutex_unlock(&conn_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001953err1:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001954 mutex_unlock(&listen_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001955 if (conn_id)
1956 rdma_destroy_id(&conn_id->id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001957
1958net_dev_put:
1959 if (net_dev)
1960 dev_put(net_dev);
1961
Sean Heftye51060f2006-06-17 20:37:29 -07001962 return ret;
1963}
1964
Sean Heftycf539362013-05-29 10:09:28 -07001965__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
Sean Heftye51060f2006-06-17 20:37:29 -07001966{
Sean Hefty496ce3c2013-05-29 10:09:19 -07001967 if (addr->sa_family == AF_IB)
1968 return ((struct sockaddr_ib *) addr)->sib_sid;
1969
Sean Heftycf539362013-05-29 10:09:28 -07001970 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001971}
Sean Heftycf539362013-05-29 10:09:28 -07001972EXPORT_SYMBOL(rdma_get_service_id);
Sean Heftye51060f2006-06-17 20:37:29 -07001973
Tom Tucker07ebafb2006-08-03 16:02:42 -05001974static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1975{
1976 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001977 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001978 int ret = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05301979 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1980 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001981
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001982 mutex_lock(&id_priv->handler_mutex);
1983 if (id_priv->state != RDMA_CM_CONNECT)
1984 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001985
Sean Heftybe65f082007-05-07 11:49:12 -07001986 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001987 switch (iw_event->event) {
1988 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001989 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001990 break;
1991 case IW_CM_EVENT_CONNECT_REPLY:
Steve Wise24d44a32013-07-04 16:10:44 +05301992 memcpy(cma_src_addr(id_priv), laddr,
1993 rdma_addr_size(laddr));
1994 memcpy(cma_dst_addr(id_priv), raddr,
1995 rdma_addr_size(raddr));
Steve Wise881a0452006-12-15 16:50:17 -06001996 switch (iw_event->status) {
1997 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001998 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301999 event.param.conn.initiator_depth = iw_event->ird;
2000 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06002001 break;
2002 case -ECONNRESET:
2003 case -ECONNREFUSED:
2004 event.event = RDMA_CM_EVENT_REJECTED;
2005 break;
2006 case -ETIMEDOUT:
2007 event.event = RDMA_CM_EVENT_UNREACHABLE;
2008 break;
2009 default:
2010 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2011 break;
2012 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002013 break;
2014 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08002015 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05302016 event.param.conn.initiator_depth = iw_event->ird;
2017 event.param.conn.responder_resources = iw_event->ord;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002018 break;
2019 default:
2020 BUG_ON(1);
2021 }
2022
Sean Heftya1b1b612006-11-30 16:33:14 -08002023 event.status = iw_event->status;
2024 event.param.conn.private_data = iw_event->private_data;
2025 event.param.conn.private_data_len = iw_event->private_data_len;
2026 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002027 if (ret) {
2028 /* Destroy the CM ID by returning a non-zero value. */
2029 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002030 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002031 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002032 rdma_destroy_id(&id_priv->id);
2033 return ret;
2034 }
2035
Bart Van Assche37e07cd2016-06-10 11:08:25 -07002036out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002037 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002038 return ret;
2039}
2040
2041static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2042 struct iw_cm_event *iw_event)
2043{
2044 struct rdma_cm_id *new_cm_id;
2045 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08002046 struct rdma_cm_event event;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07002047 int ret = -ECONNABORTED;
Steve Wise24d44a32013-07-04 16:10:44 +05302048 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2049 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002050
2051 listen_id = cm_id->context;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07002052
2053 mutex_lock(&listen_id->handler_mutex);
2054 if (listen_id->state != RDMA_CM_LISTEN)
2055 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002056
2057 /* Create a new RDMA id for the new IW CM ID */
Guy Shapirofa201052015-10-22 15:20:10 +03002058 new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2059 listen_id->id.event_handler,
Tom Tucker07ebafb2006-08-03 16:02:42 -05002060 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00002061 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07002062 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05002063 ret = -ENOMEM;
2064 goto out;
2065 }
2066 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002067 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002068 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002069
Matan Barakdd5f03b2013-12-12 18:03:11 +02002070 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002071 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07002072 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002073 rdma_destroy_id(new_cm_id);
2074 goto out;
2075 }
2076
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002077 ret = cma_acquire_dev(conn_id, listen_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002078 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07002079 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002080 rdma_destroy_id(new_cm_id);
2081 goto out;
2082 }
2083
2084 conn_id->cm_id.iw = cm_id;
2085 cm_id->context = conn_id;
2086 cm_id->cm_handler = cma_iw_handler;
2087
Steve Wise24d44a32013-07-04 16:10:44 +05302088 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2089 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002090
Sean Heftya1b1b612006-11-30 16:33:14 -08002091 memset(&event, 0, sizeof event);
2092 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2093 event.param.conn.private_data = iw_event->private_data;
2094 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05302095 event.param.conn.initiator_depth = iw_event->ird;
2096 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08002097
2098 /*
2099 * Protect against the user destroying conn_id from another thread
2100 * until we're done accessing it.
2101 */
2102 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08002103 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002104 if (ret) {
2105 /* User wants to destroy the CM ID */
2106 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002107 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002108 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08002109 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002110 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002111 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002112 }
2113
Or Gerlitzde910bd2008-07-14 23:48:53 -07002114 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08002115 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002116
Tom Tucker07ebafb2006-08-03 16:02:42 -05002117out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002118 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002119 return ret;
2120}
2121
Sean Heftye51060f2006-06-17 20:37:29 -07002122static int cma_ib_listen(struct rdma_id_private *id_priv)
2123{
Sean Heftye51060f2006-06-17 20:37:29 -07002124 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002125 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07002126 __be64 svc_id;
Sean Heftye51060f2006-06-17 20:37:29 -07002127
Sean Heftyf4753832013-05-29 10:09:14 -07002128 addr = cma_src_addr(id_priv);
Sean Heftycf539362013-05-29 10:09:28 -07002129 svc_id = rdma_get_service_id(&id_priv->id, addr);
Haggai Eran51efe392015-07-30 17:50:25 +03002130 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
2131 if (IS_ERR(id))
2132 return PTR_ERR(id);
2133 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07002134
Haggai Eran51efe392015-07-30 17:50:25 +03002135 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07002136}
2137
Tom Tucker07ebafb2006-08-03 16:02:42 -05002138static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2139{
2140 int ret;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002141 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002142
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002143 id = iw_create_cm_id(id_priv->id.device,
2144 iw_conn_req_handler,
2145 id_priv);
2146 if (IS_ERR(id))
2147 return PTR_ERR(id);
2148
Steve Wise68cdba02015-05-18 15:27:10 -05002149 id->tos = id_priv->tos;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002150 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002151
Steve Wise24d44a32013-07-04 16:10:44 +05302152 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2153 rdma_addr_size(cma_src_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002154
2155 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2156
2157 if (ret) {
2158 iw_destroy_cm_id(id_priv->cm_id.iw);
2159 id_priv->cm_id.iw = NULL;
2160 }
2161
2162 return ret;
2163}
2164
Sean Heftye51060f2006-06-17 20:37:29 -07002165static int cma_listen_handler(struct rdma_cm_id *id,
2166 struct rdma_cm_event *event)
2167{
2168 struct rdma_id_private *id_priv = id->context;
2169
2170 id->context = id_priv->id.context;
2171 id->event_handler = id_priv->id.event_handler;
2172 return id_priv->id.event_handler(id, event);
2173}
2174
2175static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2176 struct cma_device *cma_dev)
2177{
2178 struct rdma_id_private *dev_id_priv;
2179 struct rdma_cm_id *id;
Guy Shapirofa201052015-10-22 15:20:10 +03002180 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07002181 int ret;
2182
Michael Wang72219cea2015-05-05 14:50:34 +02002183 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
Sean Hefty94d0c932013-05-29 10:09:24 -07002184 return;
2185
Guy Shapirofa201052015-10-22 15:20:10 +03002186 id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
Sean Heftyb26f9b92010-04-01 17:08:41 +00002187 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07002188 if (IS_ERR(id))
2189 return;
2190
2191 dev_id_priv = container_of(id, struct rdma_id_private, id);
2192
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002193 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftyf4753832013-05-29 10:09:14 -07002194 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2195 rdma_addr_size(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002196
Matan Barak045959d2015-12-23 14:56:55 +02002197 _cma_attach_to_dev(dev_id_priv, cma_dev);
Sean Heftye51060f2006-06-17 20:37:29 -07002198 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07002199 atomic_inc(&id_priv->refcount);
2200 dev_id_priv->internal_id = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002201 dev_id_priv->afonly = id_priv->afonly;
Sean Heftye51060f2006-06-17 20:37:29 -07002202
2203 ret = rdma_listen(id, id_priv->backlog);
2204 if (ret)
Parav Panditaba25a3e2016-03-02 00:50:29 +05302205 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2206 ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07002207}
2208
2209static void cma_listen_on_all(struct rdma_id_private *id_priv)
2210{
2211 struct cma_device *cma_dev;
2212
2213 mutex_lock(&lock);
2214 list_add_tail(&id_priv->list, &listen_any_list);
2215 list_for_each_entry(cma_dev, &dev_list, list)
2216 cma_listen_on_dev(id_priv, cma_dev);
2217 mutex_unlock(&lock);
2218}
2219
Sean Heftya81c9942007-08-08 15:51:06 -07002220void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2221{
2222 struct rdma_id_private *id_priv;
2223
2224 id_priv = container_of(id, struct rdma_id_private, id);
2225 id_priv->tos = (u8) tos;
2226}
2227EXPORT_SYMBOL(rdma_set_service_type);
2228
Sean Heftye51060f2006-06-17 20:37:29 -07002229static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2230 void *context)
2231{
2232 struct cma_work *work = context;
2233 struct rdma_route *route;
2234
2235 route = &work->id->id.route;
2236
2237 if (!status) {
2238 route->num_paths = 1;
2239 *route->path_rec = *path_rec;
2240 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002241 work->old_state = RDMA_CM_ROUTE_QUERY;
2242 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002243 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07002244 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07002245 }
2246
2247 queue_work(cma_wq, &work->work);
2248}
2249
2250static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2251 struct cma_work *work)
2252{
Sean Heftyf4753832013-05-29 10:09:14 -07002253 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002254 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07002255 ib_sa_comp_mask comp_mask;
2256 struct sockaddr_in6 *sin6;
Sean Heftyf68194c2013-05-29 10:09:18 -07002257 struct sockaddr_ib *sib;
Sean Heftye51060f2006-06-17 20:37:29 -07002258
2259 memset(&path_rec, 0, sizeof path_rec);
Sean Heftyf4753832013-05-29 10:09:14 -07002260 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2261 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2262 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07002263 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08002264 path_rec.reversible = 1;
Sean Heftycf539362013-05-29 10:09:28 -07002265 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftya81c9942007-08-08 15:51:06 -07002266
2267 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2268 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2269 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2270
Sean Heftyf68194c2013-05-29 10:09:18 -07002271 switch (cma_family(id_priv)) {
2272 case AF_INET:
Sean Heftya81c9942007-08-08 15:51:06 -07002273 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2274 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002275 break;
2276 case AF_INET6:
Sean Heftyf4753832013-05-29 10:09:14 -07002277 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
Sean Heftya81c9942007-08-08 15:51:06 -07002278 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2279 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002280 break;
2281 case AF_IB:
2282 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2283 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2284 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2285 break;
Sean Heftya81c9942007-08-08 15:51:06 -07002286 }
Sean Heftye51060f2006-06-17 20:37:29 -07002287
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002288 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07002289 id_priv->id.port_num, &path_rec,
2290 comp_mask, timeout_ms,
2291 GFP_KERNEL, cma_query_handler,
2292 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07002293
2294 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2295}
2296
David Howellsc4028952006-11-22 14:57:56 +00002297static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07002298{
David Howellsc4028952006-11-22 14:57:56 +00002299 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07002300 struct rdma_id_private *id_priv = work->id;
2301 int destroy = 0;
2302
Or Gerlitzde910bd2008-07-14 23:48:53 -07002303 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002304 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2305 goto out;
2306
2307 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002308 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07002309 destroy = 1;
2310 }
2311out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002312 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002313 cma_deref_id(id_priv);
2314 if (destroy)
2315 rdma_destroy_id(&id_priv->id);
2316 kfree(work);
2317}
2318
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002319static void cma_ndev_work_handler(struct work_struct *_work)
2320{
2321 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2322 struct rdma_id_private *id_priv = work->id;
2323 int destroy = 0;
2324
2325 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002326 if (id_priv->state == RDMA_CM_DESTROYING ||
2327 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002328 goto out;
2329
2330 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002331 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002332 destroy = 1;
2333 }
2334
2335out:
2336 mutex_unlock(&id_priv->handler_mutex);
2337 cma_deref_id(id_priv);
2338 if (destroy)
2339 rdma_destroy_id(&id_priv->id);
2340 kfree(work);
2341}
2342
Sean Heftye51060f2006-06-17 20:37:29 -07002343static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2344{
2345 struct rdma_route *route = &id_priv->id.route;
2346 struct cma_work *work;
2347 int ret;
2348
2349 work = kzalloc(sizeof *work, GFP_KERNEL);
2350 if (!work)
2351 return -ENOMEM;
2352
2353 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002354 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002355 work->old_state = RDMA_CM_ROUTE_QUERY;
2356 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002357 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2358
2359 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2360 if (!route->path_rec) {
2361 ret = -ENOMEM;
2362 goto err1;
2363 }
2364
2365 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2366 if (ret)
2367 goto err2;
2368
2369 return 0;
2370err2:
2371 kfree(route->path_rec);
2372 route->path_rec = NULL;
2373err1:
2374 kfree(work);
2375 return ret;
2376}
2377
2378int rdma_set_ib_paths(struct rdma_cm_id *id,
2379 struct ib_sa_path_rec *path_rec, int num_paths)
2380{
2381 struct rdma_id_private *id_priv;
2382 int ret;
2383
2384 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002385 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2386 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07002387 return -EINVAL;
2388
Julia Lawall9893e742010-05-15 23:22:38 +02002389 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2390 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002391 if (!id->route.path_rec) {
2392 ret = -ENOMEM;
2393 goto err;
2394 }
2395
Sean Heftyae2d9292010-03-25 19:12:36 +00002396 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07002397 return 0;
2398err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002399 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002400 return ret;
2401}
2402EXPORT_SYMBOL(rdma_set_ib_paths);
2403
Tom Tucker07ebafb2006-08-03 16:02:42 -05002404static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2405{
2406 struct cma_work *work;
2407
2408 work = kzalloc(sizeof *work, GFP_KERNEL);
2409 if (!work)
2410 return -ENOMEM;
2411
2412 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002413 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002414 work->old_state = RDMA_CM_ROUTE_QUERY;
2415 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002416 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2417 queue_work(cma_wq, &work->work);
2418 return 0;
2419}
2420
Eyal Perryeb072c42013-11-06 15:37:24 +02002421static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2422{
2423 int prio;
2424 struct net_device *dev;
2425
2426 prio = rt_tos2priority(tos);
2427 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
2428 vlan_dev_real_dev(ndev) : ndev;
2429
2430 if (dev->num_tc)
2431 return netdev_get_prio_tc_map(dev, prio);
2432
2433#if IS_ENABLED(CONFIG_VLAN_8021Q)
2434 if (ndev->priv_flags & IFF_802_1Q_VLAN)
2435 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2436 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2437#endif
2438 return 0;
2439}
2440
Leon Romanovskyaeb76df2016-10-31 07:50:56 +02002441static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2442 unsigned long supported_gids,
2443 enum ib_gid_type default_gid)
2444{
2445 if ((network_type == RDMA_NETWORK_IPV4 ||
2446 network_type == RDMA_NETWORK_IPV6) &&
2447 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2448 return IB_GID_TYPE_ROCE_UDP_ENCAP;
2449
2450 return default_gid;
2451}
2452
Eli Cohen3c86aa72010-10-13 21:26:51 +02002453static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2454{
2455 struct rdma_route *route = &id_priv->id.route;
2456 struct rdma_addr *addr = &route->addr;
2457 struct cma_work *work;
2458 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002459 struct net_device *ndev = NULL;
Matan Barakdd5f03b2013-12-12 18:03:11 +02002460
Eli Cohen3c86aa72010-10-13 21:26:51 +02002461
Eli Cohen3c86aa72010-10-13 21:26:51 +02002462 work = kzalloc(sizeof *work, GFP_KERNEL);
2463 if (!work)
2464 return -ENOMEM;
2465
2466 work->id = id_priv;
2467 INIT_WORK(&work->work, cma_work_handler);
2468
2469 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2470 if (!route->path_rec) {
2471 ret = -ENOMEM;
2472 goto err1;
2473 }
2474
2475 route->num_paths = 1;
2476
Matan Barakabae1b72015-10-15 18:38:49 +03002477 if (addr->dev_addr.bound_dev_if) {
Leon Romanovskyaeb76df2016-10-31 07:50:56 +02002478 unsigned long supported_gids;
2479
Eli Cohen3c86aa72010-10-13 21:26:51 +02002480 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
Wei Yongjun23d70502016-08-05 13:46:49 +00002481 if (!ndev) {
2482 ret = -ENODEV;
2483 goto err2;
2484 }
Matan Barak20029832015-12-23 14:56:53 +02002485
2486 if (ndev->flags & IFF_LOOPBACK) {
2487 dev_put(ndev);
Wei Yongjun23d70502016-08-05 13:46:49 +00002488 if (!id_priv->id.device->get_netdev) {
2489 ret = -EOPNOTSUPP;
2490 goto err2;
2491 }
Matan Barak20029832015-12-23 14:56:53 +02002492
2493 ndev = id_priv->id.device->get_netdev(id_priv->id.device,
2494 id_priv->id.port_num);
Wei Yongjun23d70502016-08-05 13:46:49 +00002495 if (!ndev) {
2496 ret = -ENODEV;
2497 goto err2;
2498 }
Matan Barak20029832015-12-23 14:56:53 +02002499 }
2500
Matan Barakabae1b72015-10-15 18:38:49 +03002501 route->path_rec->net = &init_net;
Matan Barak20029832015-12-23 14:56:53 +02002502 route->path_rec->ifindex = ndev->ifindex;
Leon Romanovskyaeb76df2016-10-31 07:50:56 +02002503 supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2504 id_priv->id.port_num);
2505 route->path_rec->gid_type =
2506 cma_route_gid_type(addr->dev_addr.network,
2507 supported_gids,
2508 id_priv->gid_type);
Matan Barakabae1b72015-10-15 18:38:49 +03002509 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02002510 if (!ndev) {
2511 ret = -ENODEV;
2512 goto err2;
2513 }
2514
Matan Barakdd5f03b2013-12-12 18:03:11 +02002515 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002516
Moni Shoua7b856272013-12-12 18:03:12 +02002517 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2518 &route->path_rec->sgid);
2519 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2520 &route->path_rec->dgid);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002521
Somnath Koturc865f242015-12-23 14:56:51 +02002522 /* Use the hint from IP Stack to select GID Type */
Matan Barakc3efe752016-01-04 10:49:54 +02002523 if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2524 route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2525 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
Somnath Koturc865f242015-12-23 14:56:51 +02002526 /* TODO: get the hoplimit from the inet/inet6 device */
Matan Barakc3efe752016-01-04 10:49:54 +02002527 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2528 else
Somnath Koturc865f242015-12-23 14:56:51 +02002529 route->path_rec->hop_limit = 1;
Eli Cohenaf7bd462010-08-26 17:18:59 +03002530 route->path_rec->reversible = 1;
2531 route->path_rec->pkey = cpu_to_be16(0xffff);
2532 route->path_rec->mtu_selector = IB_SA_EQ;
Eyal Perryeb072c42013-11-06 15:37:24 +02002533 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
Eli Cohen3c86aa72010-10-13 21:26:51 +02002534 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2535 route->path_rec->rate_selector = IB_SA_EQ;
2536 route->path_rec->rate = iboe_get_rate(ndev);
2537 dev_put(ndev);
2538 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2539 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2540 if (!route->path_rec->mtu) {
2541 ret = -EINVAL;
2542 goto err2;
2543 }
2544
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002545 work->old_state = RDMA_CM_ROUTE_QUERY;
2546 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002547 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2548 work->event.status = 0;
2549
2550 queue_work(cma_wq, &work->work);
2551
2552 return 0;
2553
2554err2:
2555 kfree(route->path_rec);
2556 route->path_rec = NULL;
2557err1:
2558 kfree(work);
2559 return ret;
2560}
2561
Sean Heftye51060f2006-06-17 20:37:29 -07002562int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2563{
2564 struct rdma_id_private *id_priv;
2565 int ret;
2566
2567 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002568 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002569 return -EINVAL;
2570
2571 atomic_inc(&id_priv->refcount);
Michael Wangfe53ba22015-05-05 14:50:36 +02002572 if (rdma_cap_ib_sa(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002573 ret = cma_resolve_ib_route(id_priv, timeout_ms);
Ira Weiny5d9fb042015-05-14 15:01:46 -04002574 else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002575 ret = cma_resolve_iboe_route(id_priv);
2576 else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002577 ret = cma_resolve_iw_route(id_priv, timeout_ms);
Michael Wangc72f2182015-05-05 14:50:28 +02002578 else
Sean Heftye51060f2006-06-17 20:37:29 -07002579 ret = -ENOSYS;
Michael Wangc72f2182015-05-05 14:50:28 +02002580
Sean Heftye51060f2006-06-17 20:37:29 -07002581 if (ret)
2582 goto err;
2583
2584 return 0;
2585err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002586 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002587 cma_deref_id(id_priv);
2588 return ret;
2589}
2590EXPORT_SYMBOL(rdma_resolve_route);
2591
Sean Hefty6a3e3622013-05-29 10:09:13 -07002592static void cma_set_loopback(struct sockaddr *addr)
2593{
2594 switch (addr->sa_family) {
2595 case AF_INET:
2596 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2597 break;
2598 case AF_INET6:
2599 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2600 0, 0, 0, htonl(1));
2601 break;
2602 default:
2603 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2604 0, 0, 0, htonl(1));
2605 break;
2606 }
2607}
2608
Sean Heftye51060f2006-06-17 20:37:29 -07002609static int cma_bind_loopback(struct rdma_id_private *id_priv)
2610{
Sean Heftyb0569e42013-05-29 10:09:15 -07002611 struct cma_device *cma_dev, *cur_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07002612 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002613 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002614 u16 pkey;
2615 int ret;
2616 u8 p;
2617
Sean Heftyb0569e42013-05-29 10:09:15 -07002618 cma_dev = NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07002619 mutex_lock(&lock);
Sean Heftyb0569e42013-05-29 10:09:15 -07002620 list_for_each_entry(cur_dev, &dev_list, list) {
2621 if (cma_family(id_priv) == AF_IB &&
Michael Wang72219cea2015-05-05 14:50:34 +02002622 !rdma_cap_ib_cm(cur_dev->device, 1))
Sean Heftyb0569e42013-05-29 10:09:15 -07002623 continue;
2624
2625 if (!cma_dev)
2626 cma_dev = cur_dev;
2627
2628 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2629 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2630 port_attr.state == IB_PORT_ACTIVE) {
2631 cma_dev = cur_dev;
2632 goto port_found;
2633 }
2634 }
2635 }
2636
2637 if (!cma_dev) {
Sean Heftye51060f2006-06-17 20:37:29 -07002638 ret = -ENODEV;
2639 goto out;
2640 }
Krishna Kumare82153b2006-10-16 10:09:01 +05302641
2642 p = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07002643
2644port_found:
Matan Barak55ee3ab2015-10-15 18:38:45 +03002645 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002646 if (ret)
2647 goto out;
2648
2649 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2650 if (ret)
2651 goto out;
2652
Sean Hefty6f8372b2009-11-19 13:26:06 -08002653 id_priv->id.route.addr.dev_addr.dev_type =
Michael Wang21655af2015-05-05 14:50:27 +02002654 (rdma_protocol_ib(cma_dev->device, p)) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08002655 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2656
2657 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002658 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2659 id_priv->id.port_num = p;
2660 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf4753832013-05-29 10:09:14 -07002661 cma_set_loopback(cma_src_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002662out:
2663 mutex_unlock(&lock);
2664 return ret;
2665}
2666
2667static void addr_handler(int status, struct sockaddr *src_addr,
2668 struct rdma_dev_addr *dev_addr, void *context)
2669{
2670 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08002671 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002672
Sean Heftya1b1b612006-11-30 16:33:14 -08002673 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002674 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002675 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2676 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07002677 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07002678
Moni Shoua7b856272013-12-12 18:03:12 +02002679 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
Sean Hefty61a73c72006-09-01 15:33:55 -07002680 if (!status && !id_priv->cma_dev)
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002681 status = cma_acquire_dev(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002682
2683 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002684 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2685 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002686 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08002687 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2688 event.status = status;
Moni Shoua7b856272013-12-12 18:03:12 +02002689 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08002690 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002691
Sean Heftya1b1b612006-11-30 16:33:14 -08002692 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002693 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002694 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002695 cma_deref_id(id_priv);
2696 rdma_destroy_id(&id_priv->id);
2697 return;
2698 }
2699out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002700 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002701 cma_deref_id(id_priv);
2702}
2703
2704static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2705{
2706 struct cma_work *work;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002707 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002708 int ret;
2709
2710 work = kzalloc(sizeof *work, GFP_KERNEL);
2711 if (!work)
2712 return -ENOMEM;
2713
2714 if (!id_priv->cma_dev) {
2715 ret = cma_bind_loopback(id_priv);
2716 if (ret)
2717 goto err;
2718 }
2719
Sean Hefty6f8372b2009-11-19 13:26:06 -08002720 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2721 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002722
Sean Heftye51060f2006-06-17 20:37:29 -07002723 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002724 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002725 work->old_state = RDMA_CM_ADDR_QUERY;
2726 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002727 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2728 queue_work(cma_wq, &work->work);
2729 return 0;
2730err:
2731 kfree(work);
2732 return ret;
2733}
2734
Sean Heftyf17df3b2013-05-29 10:09:17 -07002735static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2736{
2737 struct cma_work *work;
2738 int ret;
2739
2740 work = kzalloc(sizeof *work, GFP_KERNEL);
2741 if (!work)
2742 return -ENOMEM;
2743
2744 if (!id_priv->cma_dev) {
2745 ret = cma_resolve_ib_dev(id_priv);
2746 if (ret)
2747 goto err;
2748 }
2749
2750 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2751 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2752
2753 work->id = id_priv;
2754 INIT_WORK(&work->work, cma_work_handler);
2755 work->old_state = RDMA_CM_ADDR_QUERY;
2756 work->new_state = RDMA_CM_ADDR_RESOLVED;
2757 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2758 queue_work(cma_wq, &work->work);
2759 return 0;
2760err:
2761 kfree(work);
2762 return ret;
2763}
2764
Sean Heftye51060f2006-06-17 20:37:29 -07002765static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2766 struct sockaddr *dst_addr)
2767{
Sean Heftyd14714d2009-11-19 16:46:25 -08002768 if (!src_addr || !src_addr->sa_family) {
2769 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002770 src_addr->sa_family = dst_addr->sa_family;
Jack Morgenstein97a2e392017-01-15 20:15:00 +02002771 if (IS_ENABLED(CONFIG_IPV6) &&
2772 dst_addr->sa_family == AF_INET6) {
Spencer Baugh6c26a772015-08-13 12:19:10 -07002773 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2774 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2775 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2776 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2777 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002778 } else if (dst_addr->sa_family == AF_IB) {
2779 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2780 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
Sean Heftyd14714d2009-11-19 16:46:25 -08002781 }
2782 }
2783 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002784}
2785
2786int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2787 struct sockaddr *dst_addr, int timeout_ms)
2788{
2789 struct rdma_id_private *id_priv;
2790 int ret;
2791
2792 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002793 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002794 ret = cma_bind_addr(id, src_addr, dst_addr);
2795 if (ret)
2796 return ret;
2797 }
2798
Sean Hefty4ae71522013-05-29 10:09:16 -07002799 if (cma_family(id_priv) != dst_addr->sa_family)
2800 return -EINVAL;
2801
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002802 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002803 return -EINVAL;
2804
2805 atomic_inc(&id_priv->refcount);
Sean Heftyf4753832013-05-29 10:09:14 -07002806 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
Sean Heftyf17df3b2013-05-29 10:09:17 -07002807 if (cma_any_addr(dst_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002808 ret = cma_resolve_loopback(id_priv);
Sean Heftyf17df3b2013-05-29 10:09:17 -07002809 } else {
2810 if (dst_addr->sa_family == AF_IB) {
2811 ret = cma_resolve_ib_addr(id_priv);
2812 } else {
2813 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2814 dst_addr, &id->route.addr.dev_addr,
2815 timeout_ms, addr_handler, id_priv);
2816 }
2817 }
Sean Heftye51060f2006-06-17 20:37:29 -07002818 if (ret)
2819 goto err;
2820
2821 return 0;
2822err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002823 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002824 cma_deref_id(id_priv);
2825 return ret;
2826}
2827EXPORT_SYMBOL(rdma_resolve_addr);
2828
Hefty, Seana9bb7912011-05-09 22:06:10 -07002829int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2830{
2831 struct rdma_id_private *id_priv;
2832 unsigned long flags;
2833 int ret;
2834
2835 id_priv = container_of(id, struct rdma_id_private, id);
2836 spin_lock_irqsave(&id_priv->lock, flags);
Sean Heftyc8dea2f92013-05-29 10:09:08 -07002837 if (reuse || id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002838 id_priv->reuseaddr = reuse;
2839 ret = 0;
2840 } else {
2841 ret = -EINVAL;
2842 }
2843 spin_unlock_irqrestore(&id_priv->lock, flags);
2844 return ret;
2845}
2846EXPORT_SYMBOL(rdma_set_reuseaddr);
2847
Sean Hefty68602122012-06-14 20:31:39 +00002848int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2849{
2850 struct rdma_id_private *id_priv;
2851 unsigned long flags;
2852 int ret;
2853
2854 id_priv = container_of(id, struct rdma_id_private, id);
2855 spin_lock_irqsave(&id_priv->lock, flags);
2856 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2857 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2858 id_priv->afonly = afonly;
2859 ret = 0;
2860 } else {
2861 ret = -EINVAL;
2862 }
2863 spin_unlock_irqrestore(&id_priv->lock, flags);
2864 return ret;
2865}
2866EXPORT_SYMBOL(rdma_set_afonly);
2867
Sean Heftye51060f2006-06-17 20:37:29 -07002868static void cma_bind_port(struct rdma_bind_list *bind_list,
2869 struct rdma_id_private *id_priv)
2870{
Sean Hefty58afdcb2013-05-29 10:09:11 -07002871 struct sockaddr *addr;
2872 struct sockaddr_ib *sib;
2873 u64 sid, mask;
2874 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07002875
Sean Heftyf4753832013-05-29 10:09:14 -07002876 addr = cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002877 port = htons(bind_list->port);
2878
2879 switch (addr->sa_family) {
2880 case AF_INET:
2881 ((struct sockaddr_in *) addr)->sin_port = port;
2882 break;
2883 case AF_INET6:
2884 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2885 break;
2886 case AF_IB:
2887 sib = (struct sockaddr_ib *) addr;
2888 sid = be64_to_cpu(sib->sib_sid);
2889 mask = be64_to_cpu(sib->sib_sid_mask);
2890 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2891 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2892 break;
2893 }
Sean Heftye51060f2006-06-17 20:37:29 -07002894 id_priv->bind_list = bind_list;
2895 hlist_add_head(&id_priv->node, &bind_list->owners);
2896}
2897
Haggai Eranaac978e2015-07-30 17:50:20 +03002898static int cma_alloc_port(enum rdma_port_space ps,
2899 struct rdma_id_private *id_priv, unsigned short snum)
Sean Heftye51060f2006-06-17 20:37:29 -07002900{
2901 struct rdma_bind_list *bind_list;
Tejun Heo3b069c52013-02-27 17:04:16 -08002902 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002903
Sean Heftycb164b82007-03-05 12:50:17 -08002904 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002905 if (!bind_list)
2906 return -ENOMEM;
2907
Guy Shapirofa201052015-10-22 15:20:10 +03002908 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2909 snum);
Tejun Heo3b069c52013-02-27 17:04:16 -08002910 if (ret < 0)
2911 goto err;
Sean Heftye51060f2006-06-17 20:37:29 -07002912
2913 bind_list->ps = ps;
Tejun Heo3b069c52013-02-27 17:04:16 -08002914 bind_list->port = (unsigned short)ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002915 cma_bind_port(bind_list, id_priv);
2916 return 0;
Tejun Heo3b069c52013-02-27 17:04:16 -08002917err:
Sean Heftyaedec082007-01-29 16:41:23 -08002918 kfree(bind_list);
Tejun Heo3b069c52013-02-27 17:04:16 -08002919 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002920}
2921
Haggai Eranaac978e2015-07-30 17:50:20 +03002922static int cma_alloc_any_port(enum rdma_port_space ps,
2923 struct rdma_id_private *id_priv)
Sean Heftyaedec082007-01-29 16:41:23 -08002924{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002925 static unsigned int last_used_port;
2926 int low, high, remaining;
2927 unsigned int rover;
Guy Shapirofa201052015-10-22 15:20:10 +03002928 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftyaedec082007-01-29 16:41:23 -08002929
Guy Shapirofa201052015-10-22 15:20:10 +03002930 inet_get_local_port_range(net, &low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002931 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002932 rover = prandom_u32() % remaining + low;
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002933retry:
2934 if (last_used_port != rover &&
Guy Shapirofa201052015-10-22 15:20:10 +03002935 !cma_ps_find(net, ps, (unsigned short)rover)) {
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002936 int ret = cma_alloc_port(ps, id_priv, rover);
2937 /*
2938 * Remember previously used port number in order to avoid
2939 * re-using same port immediately after it is closed.
2940 */
2941 if (!ret)
2942 last_used_port = rover;
2943 if (ret != -EADDRNOTAVAIL)
2944 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002945 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002946 if (--remaining) {
2947 rover++;
2948 if ((rover < low) || (rover > high))
2949 rover = low;
2950 goto retry;
2951 }
2952 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002953}
2954
Hefty, Seana9bb7912011-05-09 22:06:10 -07002955/*
2956 * Check that the requested port is available. This is called when trying to
2957 * bind to a specific port, or when trying to listen on a bound port. In
2958 * the latter case, the provided id_priv may already be on the bind_list, but
2959 * we still need to check that it's okay to start listening.
2960 */
2961static int cma_check_port(struct rdma_bind_list *bind_list,
2962 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002963{
2964 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002965 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002966
Sean Heftyf4753832013-05-29 10:09:14 -07002967 addr = cma_src_addr(id_priv);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002968 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002969 if (id_priv == cur_id)
2970 continue;
2971
Sean Hefty5b0ec992012-06-14 20:31:39 +00002972 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2973 cur_id->reuseaddr)
2974 continue;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002975
Sean Heftyf4753832013-05-29 10:09:14 -07002976 cur_addr = cma_src_addr(cur_id);
Sean Hefty5b0ec992012-06-14 20:31:39 +00002977 if (id_priv->afonly && cur_id->afonly &&
2978 (addr->sa_family != cur_addr->sa_family))
2979 continue;
2980
2981 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2982 return -EADDRNOTAVAIL;
2983
2984 if (!cma_addr_cmp(addr, cur_addr))
2985 return -EADDRINUSE;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002986 }
2987 return 0;
2988}
2989
Haggai Eranaac978e2015-07-30 17:50:20 +03002990static int cma_use_port(enum rdma_port_space ps,
2991 struct rdma_id_private *id_priv)
Hefty, Seana9bb7912011-05-09 22:06:10 -07002992{
2993 struct rdma_bind_list *bind_list;
2994 unsigned short snum;
2995 int ret;
2996
Sean Heftyf4753832013-05-29 10:09:14 -07002997 snum = ntohs(cma_port(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002998 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2999 return -EACCES;
3000
Guy Shapirofa201052015-10-22 15:20:10 +03003001 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07003002 if (!bind_list) {
3003 ret = cma_alloc_port(ps, id_priv, snum);
3004 } else {
3005 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3006 if (!ret)
3007 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003008 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07003009 return ret;
3010}
Sean Heftye51060f2006-06-17 20:37:29 -07003011
Hefty, Seana9bb7912011-05-09 22:06:10 -07003012static int cma_bind_listen(struct rdma_id_private *id_priv)
3013{
3014 struct rdma_bind_list *bind_list = id_priv->bind_list;
3015 int ret = 0;
3016
3017 mutex_lock(&lock);
3018 if (bind_list->owners.first->next)
3019 ret = cma_check_port(bind_list, id_priv, 0);
3020 mutex_unlock(&lock);
3021 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003022}
3023
Haggai Eranaac978e2015-07-30 17:50:20 +03003024static enum rdma_port_space cma_select_inet_ps(
3025 struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003026{
3027 switch (id_priv->id.ps) {
Sean Hefty58afdcb2013-05-29 10:09:11 -07003028 case RDMA_PS_TCP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07003029 case RDMA_PS_UDP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07003030 case RDMA_PS_IPOIB:
Sean Hefty58afdcb2013-05-29 10:09:11 -07003031 case RDMA_PS_IB:
Haggai Eranaac978e2015-07-30 17:50:20 +03003032 return id_priv->id.ps;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003033 default:
Haggai Eranaac978e2015-07-30 17:50:20 +03003034
3035 return 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003036 }
3037}
3038
Haggai Eranaac978e2015-07-30 17:50:20 +03003039static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003040{
Haggai Eranaac978e2015-07-30 17:50:20 +03003041 enum rdma_port_space ps = 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003042 struct sockaddr_ib *sib;
3043 u64 sid_ps, mask, sid;
3044
Sean Heftyf4753832013-05-29 10:09:14 -07003045 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07003046 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3047 sid = be64_to_cpu(sib->sib_sid) & mask;
3048
3049 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3050 sid_ps = RDMA_IB_IP_PS_IB;
Haggai Eranaac978e2015-07-30 17:50:20 +03003051 ps = RDMA_PS_IB;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003052 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3053 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3054 sid_ps = RDMA_IB_IP_PS_TCP;
Haggai Eranaac978e2015-07-30 17:50:20 +03003055 ps = RDMA_PS_TCP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003056 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3057 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3058 sid_ps = RDMA_IB_IP_PS_UDP;
Haggai Eranaac978e2015-07-30 17:50:20 +03003059 ps = RDMA_PS_UDP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003060 }
3061
3062 if (ps) {
3063 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3064 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3065 be64_to_cpu(sib->sib_sid_mask));
3066 }
3067 return ps;
3068}
3069
Sean Heftye51060f2006-06-17 20:37:29 -07003070static int cma_get_port(struct rdma_id_private *id_priv)
3071{
Haggai Eranaac978e2015-07-30 17:50:20 +03003072 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -07003073 int ret;
3074
Sean Heftyf4753832013-05-29 10:09:14 -07003075 if (cma_family(id_priv) != AF_IB)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003076 ps = cma_select_inet_ps(id_priv);
3077 else
3078 ps = cma_select_ib_ps(id_priv);
3079 if (!ps)
Sean Heftye51060f2006-06-17 20:37:29 -07003080 return -EPROTONOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07003081
3082 mutex_lock(&lock);
Sean Heftyf4753832013-05-29 10:09:14 -07003083 if (cma_any_port(cma_src_addr(id_priv)))
Sean Heftyaedec082007-01-29 16:41:23 -08003084 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003085 else
3086 ret = cma_use_port(ps, id_priv);
3087 mutex_unlock(&lock);
3088
3089 return ret;
3090}
3091
Sean Heftyd14714d2009-11-19 16:46:25 -08003092static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3093 struct sockaddr *addr)
3094{
Roland Dreierd90f9b32012-07-05 22:39:34 -07003095#if IS_ENABLED(CONFIG_IPV6)
Sean Heftyd14714d2009-11-19 16:46:25 -08003096 struct sockaddr_in6 *sin6;
3097
3098 if (addr->sa_family != AF_INET6)
3099 return 0;
3100
3101 sin6 = (struct sockaddr_in6 *) addr;
Somnath Kotur5462edd2013-11-16 09:30:01 +05303102
3103 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3104 return 0;
3105
3106 if (!sin6->sin6_scope_id)
Sean Heftyd14714d2009-11-19 16:46:25 -08003107 return -EINVAL;
3108
3109 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3110#endif
3111 return 0;
3112}
3113
Hefty, Seana9bb7912011-05-09 22:06:10 -07003114int rdma_listen(struct rdma_cm_id *id, int backlog)
3115{
3116 struct rdma_id_private *id_priv;
3117 int ret;
3118
3119 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003120 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftyf4753832013-05-29 10:09:14 -07003121 id->route.addr.src_addr.ss_family = AF_INET;
3122 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
Hefty, Seana9bb7912011-05-09 22:06:10 -07003123 if (ret)
3124 return ret;
3125 }
3126
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003127 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07003128 return -EINVAL;
3129
3130 if (id_priv->reuseaddr) {
3131 ret = cma_bind_listen(id_priv);
3132 if (ret)
3133 goto err;
3134 }
3135
3136 id_priv->backlog = backlog;
3137 if (id->device) {
Michael Wang72219cea2015-05-05 14:50:34 +02003138 if (rdma_cap_ib_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003139 ret = cma_ib_listen(id_priv);
3140 if (ret)
3141 goto err;
Michael Wang04215332015-05-05 14:50:35 +02003142 } else if (rdma_cap_iw_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003143 ret = cma_iw_listen(id_priv, backlog);
3144 if (ret)
3145 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02003146 } else {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003147 ret = -ENOSYS;
3148 goto err;
3149 }
3150 } else
3151 cma_listen_on_all(id_priv);
3152
3153 return 0;
3154err:
3155 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003156 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07003157 return ret;
3158}
3159EXPORT_SYMBOL(rdma_listen);
3160
Sean Heftye51060f2006-06-17 20:37:29 -07003161int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3162{
3163 struct rdma_id_private *id_priv;
3164 int ret;
3165
Sean Hefty680f9202013-05-29 10:09:12 -07003166 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3167 addr->sa_family != AF_IB)
Sean Heftye51060f2006-06-17 20:37:29 -07003168 return -EAFNOSUPPORT;
3169
3170 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003171 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07003172 return -EINVAL;
3173
Sean Heftyd14714d2009-11-19 16:46:25 -08003174 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3175 if (ret)
3176 goto err1;
3177
Moni Shoua7b856272013-12-12 18:03:12 +02003178 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
Sean Hefty8523c042010-02-08 16:41:15 -08003179 if (!cma_any_addr(addr)) {
Sean Hefty680f9202013-05-29 10:09:12 -07003180 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07003181 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07003182 goto err1;
3183
Doug Ledfordbe9130c2013-09-24 17:16:28 -04003184 ret = cma_acquire_dev(id_priv, NULL);
Krishna Kumar255d0c12006-10-24 13:22:28 -07003185 if (ret)
3186 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07003187 }
3188
Sean Hefty68602122012-06-14 20:31:39 +00003189 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3190 if (addr->sa_family == AF_INET)
3191 id_priv->afonly = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00003192#if IS_ENABLED(CONFIG_IPV6)
Guy Shapirofa201052015-10-22 15:20:10 +03003193 else if (addr->sa_family == AF_INET6) {
3194 struct net *net = id_priv->id.route.addr.dev_addr.net;
3195
3196 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3197 }
Sean Hefty5b0ec992012-06-14 20:31:39 +00003198#endif
Sean Hefty68602122012-06-14 20:31:39 +00003199 }
Sean Heftye51060f2006-06-17 20:37:29 -07003200 ret = cma_get_port(id_priv);
3201 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07003202 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07003203
3204 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07003205err2:
Sean Heftya396d432011-02-23 09:05:39 -08003206 if (id_priv->cma_dev)
3207 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07003208err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003209 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07003210 return ret;
3211}
3212EXPORT_SYMBOL(rdma_bind_addr);
3213
Sean Heftyf4753832013-05-29 10:09:14 -07003214static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07003215{
Sean Heftye51060f2006-06-17 20:37:29 -07003216 struct cma_hdr *cma_hdr;
Sean Heftye51060f2006-06-17 20:37:29 -07003217
Sean Hefty01602f12013-05-29 10:09:20 -07003218 cma_hdr = hdr;
3219 cma_hdr->cma_version = CMA_VERSION;
Sean Heftyf4753832013-05-29 10:09:14 -07003220 if (cma_family(id_priv) == AF_INET) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003221 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07003222
Sean Heftyf4753832013-05-29 10:09:14 -07003223 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3224 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003225
Sean Hefty01602f12013-05-29 10:09:20 -07003226 cma_set_ip_ver(cma_hdr, 4);
3227 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3228 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3229 cma_hdr->port = src4->sin_port;
Sean Heftye8160e12013-05-29 10:09:22 -07003230 } else if (cma_family(id_priv) == AF_INET6) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003231 struct sockaddr_in6 *src6, *dst6;
3232
Sean Heftyf4753832013-05-29 10:09:14 -07003233 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3234 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003235
Sean Hefty01602f12013-05-29 10:09:20 -07003236 cma_set_ip_ver(cma_hdr, 6);
3237 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3238 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3239 cma_hdr->port = src6->sin6_port;
Sean Heftye51060f2006-06-17 20:37:29 -07003240 }
3241 return 0;
3242}
3243
Sean Hefty628e5f62006-11-30 16:44:16 -08003244static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3245 struct ib_cm_event *ib_event)
3246{
3247 struct rdma_id_private *id_priv = cm_id->context;
3248 struct rdma_cm_event event;
3249 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3250 int ret = 0;
3251
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003252 mutex_lock(&id_priv->handler_mutex);
3253 if (id_priv->state != RDMA_CM_CONNECT)
3254 goto out;
Sean Hefty628e5f62006-11-30 16:44:16 -08003255
Sean Hefty8aa08602007-05-07 11:49:00 -07003256 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08003257 switch (ib_event->event) {
3258 case IB_CM_SIDR_REQ_ERROR:
3259 event.event = RDMA_CM_EVENT_UNREACHABLE;
3260 event.status = -ETIMEDOUT;
3261 break;
3262 case IB_CM_SIDR_REP_RECEIVED:
3263 event.param.ud.private_data = ib_event->private_data;
3264 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3265 if (rep->status != IB_SIDR_SUCCESS) {
3266 event.event = RDMA_CM_EVENT_UNREACHABLE;
3267 event.status = ib_event->param.sidr_rep_rcvd.status;
3268 break;
3269 }
Sean Hefty5c438132013-05-29 10:09:23 -07003270 ret = cma_set_qkey(id_priv, rep->qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003271 if (ret) {
3272 event.event = RDMA_CM_EVENT_ADDR_ERROR;
Sean Hefty5c438132013-05-29 10:09:23 -07003273 event.status = ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003274 break;
3275 }
3276 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3277 id_priv->id.route.path_rec,
3278 &event.param.ud.ah_attr);
3279 event.param.ud.qp_num = rep->qpn;
3280 event.param.ud.qkey = rep->qkey;
3281 event.event = RDMA_CM_EVENT_ESTABLISHED;
3282 event.status = 0;
3283 break;
3284 default:
Parav Panditaba25a3e2016-03-02 00:50:29 +05303285 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08003286 ib_event->event);
3287 goto out;
3288 }
3289
3290 ret = id_priv->id.event_handler(&id_priv->id, &event);
3291 if (ret) {
3292 /* Destroy the CM ID by returning a non-zero value. */
3293 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003294 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003295 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003296 rdma_destroy_id(&id_priv->id);
3297 return ret;
3298 }
3299out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07003300 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003301 return ret;
3302}
3303
3304static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3305 struct rdma_conn_param *conn_param)
3306{
3307 struct ib_cm_sidr_req_param req;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003308 struct ib_cm_id *id;
Sean Heftye511d1a2013-07-24 15:06:08 -07003309 void *private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003310 int offset, ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003311
Sean Heftye511d1a2013-07-24 15:06:08 -07003312 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003313 offset = cma_user_data_offset(id_priv);
3314 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003315 if (req.private_data_len < conn_param->private_data_len)
3316 return -EINVAL;
3317
Sean Heftye8160e12013-05-29 10:09:22 -07003318 if (req.private_data_len) {
Sean Heftye511d1a2013-07-24 15:06:08 -07003319 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3320 if (!private_data)
Sean Heftye8160e12013-05-29 10:09:22 -07003321 return -ENOMEM;
3322 } else {
Sean Heftye511d1a2013-07-24 15:06:08 -07003323 private_data = NULL;
Sean Heftye8160e12013-05-29 10:09:22 -07003324 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003325
3326 if (conn_param->private_data && conn_param->private_data_len)
Sean Heftye511d1a2013-07-24 15:06:08 -07003327 memcpy(private_data + offset, conn_param->private_data,
3328 conn_param->private_data_len);
Sean Hefty628e5f62006-11-30 16:44:16 -08003329
Sean Heftye511d1a2013-07-24 15:06:08 -07003330 if (private_data) {
3331 ret = cma_format_hdr(private_data, id_priv);
Sean Heftye8160e12013-05-29 10:09:22 -07003332 if (ret)
3333 goto out;
Sean Heftye511d1a2013-07-24 15:06:08 -07003334 req.private_data = private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003335 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003336
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003337 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3338 id_priv);
3339 if (IS_ERR(id)) {
3340 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08003341 goto out;
3342 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003343 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08003344
Sean Heftyf4753832013-05-29 10:09:14 -07003345 req.path = id_priv->id.route.path_rec;
Sean Heftycf539362013-05-29 10:09:28 -07003346 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Hefty628e5f62006-11-30 16:44:16 -08003347 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3348 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3349
3350 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3351 if (ret) {
3352 ib_destroy_cm_id(id_priv->cm_id.ib);
3353 id_priv->cm_id.ib = NULL;
3354 }
3355out:
Sean Heftye511d1a2013-07-24 15:06:08 -07003356 kfree(private_data);
Sean Hefty628e5f62006-11-30 16:44:16 -08003357 return ret;
3358}
3359
Sean Heftye51060f2006-06-17 20:37:29 -07003360static int cma_connect_ib(struct rdma_id_private *id_priv,
3361 struct rdma_conn_param *conn_param)
3362{
3363 struct ib_cm_req_param req;
3364 struct rdma_route *route;
3365 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003366 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07003367 int offset, ret;
3368
3369 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003370 offset = cma_user_data_offset(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003371 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003372 if (req.private_data_len < conn_param->private_data_len)
3373 return -EINVAL;
3374
Sean Heftye8160e12013-05-29 10:09:22 -07003375 if (req.private_data_len) {
3376 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3377 if (!private_data)
3378 return -ENOMEM;
3379 } else {
3380 private_data = NULL;
3381 }
Sean Heftye51060f2006-06-17 20:37:29 -07003382
3383 if (conn_param->private_data && conn_param->private_data_len)
3384 memcpy(private_data + offset, conn_param->private_data,
3385 conn_param->private_data_len);
3386
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003387 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3388 if (IS_ERR(id)) {
3389 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07003390 goto out;
3391 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003392 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07003393
3394 route = &id_priv->id.route;
Sean Heftye8160e12013-05-29 10:09:22 -07003395 if (private_data) {
3396 ret = cma_format_hdr(private_data, id_priv);
3397 if (ret)
3398 goto out;
3399 req.private_data = private_data;
3400 }
Sean Heftye51060f2006-06-17 20:37:29 -07003401
3402 req.primary_path = &route->path_rec[0];
3403 if (route->num_paths == 2)
3404 req.alternate_path = &route->path_rec[1];
3405
Sean Heftycf539362013-05-29 10:09:28 -07003406 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07003407 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07003408 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07003409 req.starting_psn = id_priv->seq_num;
3410 req.responder_resources = conn_param->responder_resources;
3411 req.initiator_depth = conn_param->initiator_depth;
3412 req.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003413 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3414 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003415 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3416 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3417 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3418 req.srq = id_priv->srq ? 1 : 0;
3419
3420 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3421out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003422 if (ret && !IS_ERR(id)) {
3423 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07003424 id_priv->cm_id.ib = NULL;
3425 }
3426
Sean Heftye51060f2006-06-17 20:37:29 -07003427 kfree(private_data);
3428 return ret;
3429}
3430
Tom Tucker07ebafb2006-08-03 16:02:42 -05003431static int cma_connect_iw(struct rdma_id_private *id_priv,
3432 struct rdma_conn_param *conn_param)
3433{
3434 struct iw_cm_id *cm_id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003435 int ret;
3436 struct iw_cm_conn_param iw_param;
3437
3438 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003439 if (IS_ERR(cm_id))
3440 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003441
Steve Wise68cdba02015-05-18 15:27:10 -05003442 cm_id->tos = id_priv->tos;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003443 id_priv->cm_id.iw = cm_id;
3444
Steve Wise24d44a32013-07-04 16:10:44 +05303445 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3446 rdma_addr_size(cma_src_addr(id_priv)));
3447 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3448 rdma_addr_size(cma_dst_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05003449
Sean Hefty5851bb82008-01-04 10:47:12 -08003450 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07003451 if (ret)
3452 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003453
Hefty, Seanf45ee802011-10-06 09:33:04 -07003454 if (conn_param) {
3455 iw_param.ord = conn_param->initiator_depth;
3456 iw_param.ird = conn_param->responder_resources;
3457 iw_param.private_data = conn_param->private_data;
3458 iw_param.private_data_len = conn_param->private_data_len;
3459 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3460 } else {
3461 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003462 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07003463 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003464 ret = iw_cm_connect(cm_id, &iw_param);
3465out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003466 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07003467 iw_destroy_cm_id(cm_id);
3468 id_priv->cm_id.iw = NULL;
3469 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003470 return ret;
3471}
3472
Sean Heftye51060f2006-06-17 20:37:29 -07003473int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3474{
3475 struct rdma_id_private *id_priv;
3476 int ret;
3477
3478 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003479 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003480 return -EINVAL;
3481
3482 if (!id->qp) {
3483 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003484 id_priv->srq = conn_param->srq;
3485 }
3486
Michael Wang72219cea2015-05-05 14:50:34 +02003487 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003488 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08003489 ret = cma_resolve_ib_udp(id_priv, conn_param);
3490 else
3491 ret = cma_connect_ib(id_priv, conn_param);
Michael Wang04215332015-05-05 14:50:35 +02003492 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003493 ret = cma_connect_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003494 else
Sean Heftye51060f2006-06-17 20:37:29 -07003495 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003496 if (ret)
3497 goto err;
3498
3499 return 0;
3500err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003501 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07003502 return ret;
3503}
3504EXPORT_SYMBOL(rdma_connect);
3505
3506static int cma_accept_ib(struct rdma_id_private *id_priv,
3507 struct rdma_conn_param *conn_param)
3508{
3509 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08003510 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003511
Sean Hefty5851bb82008-01-04 10:47:12 -08003512 ret = cma_modify_qp_rtr(id_priv, conn_param);
3513 if (ret)
3514 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08003515
Sean Hefty5851bb82008-01-04 10:47:12 -08003516 ret = cma_modify_qp_rts(id_priv, conn_param);
3517 if (ret)
3518 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003519
3520 memset(&rep, 0, sizeof rep);
3521 rep.qp_num = id_priv->qp_num;
3522 rep.starting_psn = id_priv->seq_num;
3523 rep.private_data = conn_param->private_data;
3524 rep.private_data_len = conn_param->private_data_len;
3525 rep.responder_resources = conn_param->responder_resources;
3526 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07003527 rep.failover_accepted = 0;
3528 rep.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003529 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003530 rep.srq = id_priv->srq ? 1 : 0;
3531
Sean Hefty0fe313b2006-11-30 16:37:15 -08003532 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3533out:
3534 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003535}
3536
Tom Tucker07ebafb2006-08-03 16:02:42 -05003537static int cma_accept_iw(struct rdma_id_private *id_priv,
3538 struct rdma_conn_param *conn_param)
3539{
3540 struct iw_cm_conn_param iw_param;
3541 int ret;
3542
Steve Wisec8cdd922017-02-21 11:21:57 -08003543 if (!conn_param)
3544 return -EINVAL;
3545
Sean Hefty5851bb82008-01-04 10:47:12 -08003546 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003547 if (ret)
3548 return ret;
3549
3550 iw_param.ord = conn_param->initiator_depth;
3551 iw_param.ird = conn_param->responder_resources;
3552 iw_param.private_data = conn_param->private_data;
3553 iw_param.private_data_len = conn_param->private_data_len;
3554 if (id_priv->id.qp) {
3555 iw_param.qpn = id_priv->qp_num;
3556 } else
3557 iw_param.qpn = conn_param->qp_num;
3558
3559 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3560}
3561
Sean Hefty628e5f62006-11-30 16:44:16 -08003562static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
Sean Hefty5c438132013-05-29 10:09:23 -07003563 enum ib_cm_sidr_status status, u32 qkey,
Sean Hefty628e5f62006-11-30 16:44:16 -08003564 const void *private_data, int private_data_len)
3565{
3566 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003567 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003568
3569 memset(&rep, 0, sizeof rep);
3570 rep.status = status;
3571 if (status == IB_SIDR_SUCCESS) {
Sean Hefty5c438132013-05-29 10:09:23 -07003572 ret = cma_set_qkey(id_priv, qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003573 if (ret)
3574 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003575 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003576 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08003577 }
3578 rep.private_data = private_data;
3579 rep.private_data_len = private_data_len;
3580
3581 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3582}
3583
Sean Heftye51060f2006-06-17 20:37:29 -07003584int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3585{
3586 struct rdma_id_private *id_priv;
3587 int ret;
3588
3589 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00003590
3591 id_priv->owner = task_pid_nr(current);
3592
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003593 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003594 return -EINVAL;
3595
3596 if (!id->qp && conn_param) {
3597 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003598 id_priv->srq = conn_param->srq;
3599 }
3600
Michael Wang72219cea2015-05-05 14:50:34 +02003601 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Hefty, Seanf45ee802011-10-06 09:33:04 -07003602 if (id->qp_type == IB_QPT_UD) {
3603 if (conn_param)
3604 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003605 conn_param->qkey,
Hefty, Seanf45ee802011-10-06 09:33:04 -07003606 conn_param->private_data,
3607 conn_param->private_data_len);
3608 else
3609 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003610 0, NULL, 0);
Hefty, Seanf45ee802011-10-06 09:33:04 -07003611 } else {
3612 if (conn_param)
3613 ret = cma_accept_ib(id_priv, conn_param);
3614 else
3615 ret = cma_rep_recv(id_priv);
3616 }
Michael Wang04215332015-05-05 14:50:35 +02003617 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003618 ret = cma_accept_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003619 else
Sean Heftye51060f2006-06-17 20:37:29 -07003620 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003621
3622 if (ret)
3623 goto reject;
3624
3625 return 0;
3626reject:
Sean Heftyc5483382007-09-24 13:19:09 -07003627 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003628 rdma_reject(id, NULL, 0);
3629 return ret;
3630}
3631EXPORT_SYMBOL(rdma_accept);
3632
Sean Hefty0fe313b2006-11-30 16:37:15 -08003633int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3634{
3635 struct rdma_id_private *id_priv;
3636 int ret;
3637
3638 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003639 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08003640 return -EINVAL;
3641
3642 switch (id->device->node_type) {
3643 case RDMA_NODE_IB_CA:
3644 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3645 break;
3646 default:
3647 ret = 0;
3648 break;
3649 }
3650 return ret;
3651}
3652EXPORT_SYMBOL(rdma_notify);
3653
Sean Heftye51060f2006-06-17 20:37:29 -07003654int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3655 u8 private_data_len)
3656{
3657 struct rdma_id_private *id_priv;
3658 int ret;
3659
3660 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003661 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003662 return -EINVAL;
3663
Michael Wang72219cea2015-05-05 14:50:34 +02003664 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003665 if (id->qp_type == IB_QPT_UD)
Sean Hefty5c438132013-05-29 10:09:23 -07003666 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
Sean Hefty628e5f62006-11-30 16:44:16 -08003667 private_data, private_data_len);
3668 else
3669 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3670 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3671 0, private_data, private_data_len);
Michael Wang04215332015-05-05 14:50:35 +02003672 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003673 ret = iw_cm_reject(id_priv->cm_id.iw,
3674 private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003675 } else
Sean Heftye51060f2006-06-17 20:37:29 -07003676 ret = -ENOSYS;
Michael Wang21655af2015-05-05 14:50:27 +02003677
Sean Heftye51060f2006-06-17 20:37:29 -07003678 return ret;
3679}
3680EXPORT_SYMBOL(rdma_reject);
3681
3682int rdma_disconnect(struct rdma_cm_id *id)
3683{
3684 struct rdma_id_private *id_priv;
3685 int ret;
3686
3687 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003688 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003689 return -EINVAL;
3690
Michael Wang72219cea2015-05-05 14:50:34 +02003691 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyc5483382007-09-24 13:19:09 -07003692 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003693 if (ret)
3694 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003695 /* Initiate or respond to a disconnect. */
3696 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3697 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
Michael Wang04215332015-05-05 14:50:35 +02003698 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003699 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003700 } else
Tom Tucker07ebafb2006-08-03 16:02:42 -05003701 ret = -EINVAL;
Michael Wang21655af2015-05-05 14:50:27 +02003702
Sean Heftye51060f2006-06-17 20:37:29 -07003703out:
3704 return ret;
3705}
3706EXPORT_SYMBOL(rdma_disconnect);
3707
Sean Heftyc8f6a362007-02-15 17:00:18 -08003708static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3709{
3710 struct rdma_id_private *id_priv;
3711 struct cma_multicast *mc = multicast->context;
3712 struct rdma_cm_event event;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003713 int ret = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003714
3715 id_priv = mc->id_priv;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003716 mutex_lock(&id_priv->handler_mutex);
3717 if (id_priv->state != RDMA_CM_ADDR_BOUND &&
3718 id_priv->state != RDMA_CM_ADDR_RESOLVED)
3719 goto out;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003720
Sean Hefty5c438132013-05-29 10:09:23 -07003721 if (!status)
3722 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
Sean Heftyc5483382007-09-24 13:19:09 -07003723 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003724 if (!status && id_priv->id.qp)
3725 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003726 be16_to_cpu(multicast->rec.mlid));
Sean Heftyc5483382007-09-24 13:19:09 -07003727 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003728
3729 memset(&event, 0, sizeof event);
3730 event.status = status;
3731 event.param.ud.private_data = mc->context;
3732 if (!status) {
Moni Shouabee3c3c2015-12-23 14:56:57 +02003733 struct rdma_dev_addr *dev_addr =
3734 &id_priv->id.route.addr.dev_addr;
3735 struct net_device *ndev =
3736 dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3737 enum ib_gid_type gid_type =
3738 id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3739 rdma_start_port(id_priv->cma_dev->device)];
3740
Sean Heftyc8f6a362007-02-15 17:00:18 -08003741 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3742 ib_init_ah_from_mcmember(id_priv->id.device,
3743 id_priv->id.port_num, &multicast->rec,
Moni Shouabee3c3c2015-12-23 14:56:57 +02003744 ndev, gid_type,
Sean Heftyc8f6a362007-02-15 17:00:18 -08003745 &event.param.ud.ah_attr);
3746 event.param.ud.qp_num = 0xFFFFFF;
3747 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
Moni Shouabee3c3c2015-12-23 14:56:57 +02003748 if (ndev)
3749 dev_put(ndev);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003750 } else
3751 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3752
3753 ret = id_priv->id.event_handler(&id_priv->id, &event);
3754 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003755 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003756 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003757 rdma_destroy_id(&id_priv->id);
3758 return 0;
3759 }
Sean Hefty8aa08602007-05-07 11:49:00 -07003760
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003761out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07003762 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003763 return 0;
3764}
3765
3766static void cma_set_mgid(struct rdma_id_private *id_priv,
3767 struct sockaddr *addr, union ib_gid *mgid)
3768{
3769 unsigned char mc_map[MAX_ADDR_LEN];
3770 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3771 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3772 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3773
3774 if (cma_any_addr(addr)) {
3775 memset(mgid, 0, sizeof *mgid);
3776 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08003777 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08003778 0xFF10A01B)) {
3779 /* IPv6 address is an SA assigned MGID. */
3780 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003781 } else if (addr->sa_family == AF_IB) {
3782 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08003783 } else if ((addr->sa_family == AF_INET6)) {
3784 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3785 if (id_priv->id.ps == RDMA_PS_UDP)
3786 mc_map[7] = 0x01; /* Use RDMA CM signature */
3787 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003788 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07003789 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003790 if (id_priv->id.ps == RDMA_PS_UDP)
3791 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08003792 *mgid = *(union ib_gid *) (mc_map + 4);
3793 }
3794}
3795
Alex Veskerab15c952016-07-06 16:36:35 +03003796static void cma_query_sa_classport_info_cb(int status,
3797 struct ib_class_port_info *rec,
3798 void *context)
3799{
3800 struct class_port_info_context *cb_ctx = context;
3801
3802 WARN_ON(!context);
3803
3804 if (status || !rec) {
3805 pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
3806 cb_ctx->device->name, cb_ctx->port_num, status);
3807 goto out;
3808 }
3809
3810 memcpy(cb_ctx->class_port_info, rec, sizeof(struct ib_class_port_info));
3811
3812out:
3813 complete(&cb_ctx->done);
3814}
3815
3816static int cma_query_sa_classport_info(struct ib_device *device, u8 port_num,
3817 struct ib_class_port_info *class_port_info)
3818{
3819 struct class_port_info_context *cb_ctx;
3820 int ret;
3821
3822 cb_ctx = kmalloc(sizeof(*cb_ctx), GFP_KERNEL);
3823 if (!cb_ctx)
3824 return -ENOMEM;
3825
3826 cb_ctx->device = device;
3827 cb_ctx->class_port_info = class_port_info;
3828 cb_ctx->port_num = port_num;
3829 init_completion(&cb_ctx->done);
3830
3831 ret = ib_sa_classport_info_rec_query(&sa_client, device, port_num,
3832 CMA_QUERY_CLASSPORT_INFO_TIMEOUT,
3833 GFP_KERNEL, cma_query_sa_classport_info_cb,
3834 cb_ctx, &cb_ctx->sa_query);
3835 if (ret < 0) {
3836 pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
3837 device->name, port_num, ret);
3838 goto out;
3839 }
3840
3841 wait_for_completion(&cb_ctx->done);
3842
3843out:
3844 kfree(cb_ctx);
3845 return ret;
3846}
3847
Sean Heftyc8f6a362007-02-15 17:00:18 -08003848static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3849 struct cma_multicast *mc)
3850{
3851 struct ib_sa_mcmember_rec rec;
Alex Veskerab15c952016-07-06 16:36:35 +03003852 struct ib_class_port_info class_port_info;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003853 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3854 ib_sa_comp_mask comp_mask;
3855 int ret;
3856
3857 ib_addr_get_mgid(dev_addr, &rec.mgid);
3858 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3859 &rec.mgid, &rec);
3860 if (ret)
3861 return ret;
3862
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003863 ret = cma_set_qkey(id_priv, 0);
3864 if (ret)
3865 return ret;
3866
Roland Dreier3f446752008-08-04 11:02:14 -07003867 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003868 rec.qkey = cpu_to_be32(id_priv->qkey);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003869 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003870 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Alex Veskerab15c952016-07-06 16:36:35 +03003871 rec.join_state = mc->join_state;
3872
3873 if (rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) {
3874 ret = cma_query_sa_classport_info(id_priv->id.device,
3875 id_priv->id.port_num,
3876 &class_port_info);
3877
3878 if (ret)
3879 return ret;
3880
3881 if (!(ib_get_cpi_capmask2(&class_port_info) &
3882 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT)) {
3883 pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3884 "RDMA CM: SM doesn't support Send Only Full Member option\n",
3885 id_priv->id.device->name, id_priv->id.port_num);
3886 return -EOPNOTSUPP;
3887 }
3888 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003889
3890 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3891 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3892 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3893 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3894 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3895
Yossi Etigin84adeee2009-04-01 13:55:32 -07003896 if (id_priv->id.ps == RDMA_PS_IPOIB)
3897 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
Dotan Barak2a22fb82012-08-30 09:09:55 +00003898 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3899 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3900 IB_SA_MCMEMBER_REC_MTU |
3901 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Yossi Etigin84adeee2009-04-01 13:55:32 -07003902
Sean Heftyc8f6a362007-02-15 17:00:18 -08003903 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3904 id_priv->id.port_num, &rec,
3905 comp_mask, GFP_KERNEL,
3906 cma_ib_mc_handler, mc);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303907 return PTR_ERR_OR_ZERO(mc->multicast.ib);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003908}
3909
Eli Cohen3c86aa72010-10-13 21:26:51 +02003910static void iboe_mcast_work_handler(struct work_struct *work)
3911{
3912 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3913 struct cma_multicast *mc = mw->mc;
3914 struct ib_sa_multicast *m = mc->multicast.ib;
3915
3916 mc->multicast.ib->context = mc;
3917 cma_ib_mc_handler(0, m);
3918 kref_put(&mc->mcref, release_mc);
3919 kfree(mw);
3920}
3921
3922static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3923{
3924 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3925 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3926
3927 if (cma_any_addr(addr)) {
3928 memset(mgid, 0, sizeof *mgid);
3929 } else if (addr->sa_family == AF_INET6) {
3930 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3931 } else {
3932 mgid->raw[0] = 0xff;
3933 mgid->raw[1] = 0x0e;
3934 mgid->raw[2] = 0;
3935 mgid->raw[3] = 0;
3936 mgid->raw[4] = 0;
3937 mgid->raw[5] = 0;
3938 mgid->raw[6] = 0;
3939 mgid->raw[7] = 0;
3940 mgid->raw[8] = 0;
3941 mgid->raw[9] = 0;
3942 mgid->raw[10] = 0xff;
3943 mgid->raw[11] = 0xff;
3944 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3945 }
3946}
3947
3948static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3949 struct cma_multicast *mc)
3950{
3951 struct iboe_mcast_work *work;
3952 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Moni Shouabee3c3c2015-12-23 14:56:57 +02003953 int err = 0;
Eli Cohen3c86aa72010-10-13 21:26:51 +02003954 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3955 struct net_device *ndev = NULL;
Moni Shouabee3c3c2015-12-23 14:56:57 +02003956 enum ib_gid_type gid_type;
Alex Veskerab15c952016-07-06 16:36:35 +03003957 bool send_only;
3958
3959 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003960
3961 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3962 return -EINVAL;
3963
3964 work = kzalloc(sizeof *work, GFP_KERNEL);
3965 if (!work)
3966 return -ENOMEM;
3967
3968 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3969 if (!mc->multicast.ib) {
3970 err = -ENOMEM;
3971 goto out1;
3972 }
3973
3974 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3975
3976 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3977 if (id_priv->id.ps == RDMA_PS_UDP)
3978 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3979
3980 if (dev_addr->bound_dev_if)
3981 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3982 if (!ndev) {
3983 err = -ENODEV;
3984 goto out2;
3985 }
3986 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3987 mc->multicast.ib->rec.hop_limit = 1;
3988 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
Moni Shouabee3c3c2015-12-23 14:56:57 +02003989
3990 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3991 rdma_start_port(id_priv->cma_dev->device)];
3992 if (addr->sa_family == AF_INET) {
Alex Veskerc65f6c52016-06-22 17:27:23 +03003993 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
3994 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
Alex Veskerab15c952016-07-06 16:36:35 +03003995 if (!send_only) {
3996 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
3997 true);
3998 if (!err)
3999 mc->igmp_joined = true;
4000 }
Moni Shouabee3c3c2015-12-23 14:56:57 +02004001 }
4002 } else {
4003 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4004 err = -ENOTSUPP;
4005 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02004006 dev_put(ndev);
Moni Shouabee3c3c2015-12-23 14:56:57 +02004007 if (err || !mc->multicast.ib->rec.mtu) {
4008 if (!err)
4009 err = -EINVAL;
Eli Cohen3c86aa72010-10-13 21:26:51 +02004010 goto out2;
4011 }
Moni Shoua7b856272013-12-12 18:03:12 +02004012 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4013 &mc->multicast.ib->rec.port_gid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02004014 work->id = id_priv;
4015 work->mc = mc;
4016 INIT_WORK(&work->work, iboe_mcast_work_handler);
4017 kref_get(&mc->mcref);
4018 queue_work(cma_wq, &work->work);
4019
4020 return 0;
4021
4022out2:
4023 kfree(mc->multicast.ib);
4024out1:
4025 kfree(work);
4026 return err;
4027}
4028
Sean Heftyc8f6a362007-02-15 17:00:18 -08004029int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
Alex Veskerab15c952016-07-06 16:36:35 +03004030 u8 join_state, void *context)
Sean Heftyc8f6a362007-02-15 17:00:18 -08004031{
4032 struct rdma_id_private *id_priv;
4033 struct cma_multicast *mc;
4034 int ret;
4035
4036 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004037 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4038 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08004039 return -EINVAL;
4040
4041 mc = kmalloc(sizeof *mc, GFP_KERNEL);
4042 if (!mc)
4043 return -ENOMEM;
4044
Sean Heftyef560862013-05-29 10:09:10 -07004045 memcpy(&mc->addr, addr, rdma_addr_size(addr));
Sean Heftyc8f6a362007-02-15 17:00:18 -08004046 mc->context = context;
4047 mc->id_priv = id_priv;
Moni Shouabee3c3c2015-12-23 14:56:57 +02004048 mc->igmp_joined = false;
Alex Veskerab15c952016-07-06 16:36:35 +03004049 mc->join_state = join_state;
Sean Heftyc8f6a362007-02-15 17:00:18 -08004050 spin_lock(&id_priv->lock);
4051 list_add(&mc->list, &id_priv->mc_list);
4052 spin_unlock(&id_priv->lock);
4053
Ira Weiny5d9fb042015-05-14 15:01:46 -04004054 if (rdma_protocol_roce(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02004055 kref_init(&mc->mcref);
4056 ret = cma_iboe_join_multicast(id_priv, mc);
Michael Wanga31ad3b2015-05-05 14:50:37 +02004057 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02004058 ret = cma_join_ib_multicast(id_priv, mc);
4059 else
Sean Heftyc8f6a362007-02-15 17:00:18 -08004060 ret = -ENOSYS;
Sean Heftyc8f6a362007-02-15 17:00:18 -08004061
4062 if (ret) {
4063 spin_lock_irq(&id_priv->lock);
4064 list_del(&mc->list);
4065 spin_unlock_irq(&id_priv->lock);
4066 kfree(mc);
4067 }
4068 return ret;
4069}
4070EXPORT_SYMBOL(rdma_join_multicast);
4071
4072void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4073{
4074 struct rdma_id_private *id_priv;
4075 struct cma_multicast *mc;
4076
4077 id_priv = container_of(id, struct rdma_id_private, id);
4078 spin_lock_irq(&id_priv->lock);
4079 list_for_each_entry(mc, &id_priv->mc_list, list) {
Sean Heftyef560862013-05-29 10:09:10 -07004080 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
Sean Heftyc8f6a362007-02-15 17:00:18 -08004081 list_del(&mc->list);
4082 spin_unlock_irq(&id_priv->lock);
4083
4084 if (id->qp)
4085 ib_detach_mcast(id->qp,
4086 &mc->multicast.ib->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00004087 be16_to_cpu(mc->multicast.ib->rec.mlid));
Michael Wang5c9a5282015-05-05 14:50:29 +02004088
4089 BUG_ON(id_priv->cma_dev->device != id->device);
4090
Michael Wanga31ad3b2015-05-05 14:50:37 +02004091 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02004092 ib_sa_free_multicast(mc->multicast.ib);
4093 kfree(mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02004094 } else if (rdma_protocol_roce(id->device, id->port_num)) {
4095 if (mc->igmp_joined) {
4096 struct rdma_dev_addr *dev_addr =
4097 &id->route.addr.dev_addr;
4098 struct net_device *ndev = NULL;
Michael Wang5c9a5282015-05-05 14:50:29 +02004099
Moni Shouabee3c3c2015-12-23 14:56:57 +02004100 if (dev_addr->bound_dev_if)
4101 ndev = dev_get_by_index(&init_net,
4102 dev_addr->bound_dev_if);
4103 if (ndev) {
4104 cma_igmp_send(ndev,
4105 &mc->multicast.ib->rec.mgid,
4106 false);
4107 dev_put(ndev);
4108 }
4109 mc->igmp_joined = false;
4110 }
4111 kref_put(&mc->mcref, release_mc);
4112 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08004113 return;
4114 }
4115 }
4116 spin_unlock_irq(&id_priv->lock);
4117}
4118EXPORT_SYMBOL(rdma_leave_multicast);
4119
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004120static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4121{
4122 struct rdma_dev_addr *dev_addr;
4123 struct cma_ndev_work *work;
4124
4125 dev_addr = &id_priv->id.route.addr.dev_addr;
4126
Sean Hefty6266ed62009-11-19 12:55:22 -08004127 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Guy Shapirofa201052015-10-22 15:20:10 +03004128 (net_eq(dev_net(ndev), dev_addr->net)) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004129 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05304130 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4131 ndev->name, &id_priv->id);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004132 work = kzalloc(sizeof *work, GFP_KERNEL);
4133 if (!work)
4134 return -ENOMEM;
4135
4136 INIT_WORK(&work->work, cma_ndev_work_handler);
4137 work->id = id_priv;
4138 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4139 atomic_inc(&id_priv->refcount);
4140 queue_work(cma_wq, &work->work);
4141 }
4142
4143 return 0;
4144}
4145
4146static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00004147 void *ptr)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004148{
Jiri Pirko351638e2013-05-28 01:30:21 +00004149 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004150 struct cma_device *cma_dev;
4151 struct rdma_id_private *id_priv;
4152 int ret = NOTIFY_DONE;
4153
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004154 if (event != NETDEV_BONDING_FAILOVER)
4155 return NOTIFY_DONE;
4156
4157 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
4158 return NOTIFY_DONE;
4159
4160 mutex_lock(&lock);
4161 list_for_each_entry(cma_dev, &dev_list, list)
4162 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4163 ret = cma_netdev_change(ndev, id_priv);
4164 if (ret)
4165 goto out;
4166 }
4167
4168out:
4169 mutex_unlock(&lock);
4170 return ret;
4171}
4172
4173static struct notifier_block cma_nb = {
4174 .notifier_call = cma_netdev_callback
4175};
4176
Sean Heftye51060f2006-06-17 20:37:29 -07004177static void cma_add_one(struct ib_device *device)
4178{
4179 struct cma_device *cma_dev;
4180 struct rdma_id_private *id_priv;
Matan Barak045959d2015-12-23 14:56:55 +02004181 unsigned int i;
4182 unsigned long supported_gids = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07004183
4184 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4185 if (!cma_dev)
4186 return;
4187
4188 cma_dev->device = device;
Matan Barak045959d2015-12-23 14:56:55 +02004189 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4190 sizeof(*cma_dev->default_gid_type),
4191 GFP_KERNEL);
4192 if (!cma_dev->default_gid_type) {
4193 kfree(cma_dev);
4194 return;
4195 }
4196 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4197 supported_gids = roce_gid_type_mask_support(device, i);
4198 WARN_ON(!supported_gids);
4199 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4200 find_first_bit(&supported_gids, BITS_PER_LONG);
4201 }
Sean Heftye51060f2006-06-17 20:37:29 -07004202
4203 init_completion(&cma_dev->comp);
4204 atomic_set(&cma_dev->refcount, 1);
4205 INIT_LIST_HEAD(&cma_dev->id_list);
4206 ib_set_client_data(device, &cma_client, cma_dev);
4207
4208 mutex_lock(&lock);
4209 list_add_tail(&cma_dev->list, &dev_list);
4210 list_for_each_entry(id_priv, &listen_any_list, list)
4211 cma_listen_on_dev(id_priv, cma_dev);
4212 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07004213}
4214
4215static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4216{
Sean Heftya1b1b612006-11-30 16:33:14 -08004217 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004218 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07004219 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07004220
4221 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004222 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4223 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07004224 return 0;
4225
4226 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07004227 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07004228
4229 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004230 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07004231 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07004232
Sean Heftya1b1b612006-11-30 16:33:14 -08004233 memset(&event, 0, sizeof event);
4234 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07004235 ret = id_priv->id.event_handler(&id_priv->id, &event);
4236out:
4237 mutex_unlock(&id_priv->handler_mutex);
4238 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07004239}
4240
4241static void cma_process_remove(struct cma_device *cma_dev)
4242{
Sean Heftye51060f2006-06-17 20:37:29 -07004243 struct rdma_id_private *id_priv;
4244 int ret;
4245
Sean Heftye51060f2006-06-17 20:37:29 -07004246 mutex_lock(&lock);
4247 while (!list_empty(&cma_dev->id_list)) {
4248 id_priv = list_entry(cma_dev->id_list.next,
4249 struct rdma_id_private, list);
4250
Sean Heftyd02d1f52007-10-09 11:12:34 -07004251 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07004252 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07004253 atomic_inc(&id_priv->refcount);
4254 mutex_unlock(&lock);
4255
Sean Heftyd02d1f52007-10-09 11:12:34 -07004256 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07004257 cma_deref_id(id_priv);
4258 if (ret)
4259 rdma_destroy_id(&id_priv->id);
4260
4261 mutex_lock(&lock);
4262 }
4263 mutex_unlock(&lock);
4264
4265 cma_deref_dev(cma_dev);
4266 wait_for_completion(&cma_dev->comp);
4267}
4268
Haggai Eran7c1eb452015-07-30 17:50:14 +03004269static void cma_remove_one(struct ib_device *device, void *client_data)
Sean Heftye51060f2006-06-17 20:37:29 -07004270{
Haggai Eran7c1eb452015-07-30 17:50:14 +03004271 struct cma_device *cma_dev = client_data;
Sean Heftye51060f2006-06-17 20:37:29 -07004272
Sean Heftye51060f2006-06-17 20:37:29 -07004273 if (!cma_dev)
4274 return;
4275
4276 mutex_lock(&lock);
4277 list_del(&cma_dev->list);
4278 mutex_unlock(&lock);
4279
4280 cma_process_remove(cma_dev);
Matan Barak045959d2015-12-23 14:56:55 +02004281 kfree(cma_dev->default_gid_type);
Sean Heftye51060f2006-06-17 20:37:29 -07004282 kfree(cma_dev);
4283}
4284
Nir Muchtar753f6182011-01-03 15:33:53 +00004285static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
4286{
4287 struct nlmsghdr *nlh;
4288 struct rdma_cm_id_stats *id_stats;
4289 struct rdma_id_private *id_priv;
4290 struct rdma_cm_id *id = NULL;
4291 struct cma_device *cma_dev;
4292 int i_dev = 0, i_id = 0;
4293
4294 /*
4295 * We export all of the IDs as a sequence of messages. Each
4296 * ID gets its own netlink message.
4297 */
4298 mutex_lock(&lock);
4299
4300 list_for_each_entry(cma_dev, &dev_list, list) {
4301 if (i_dev < cb->args[0]) {
4302 i_dev++;
4303 continue;
4304 }
4305
4306 i_id = 0;
4307 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4308 if (i_id < cb->args[1]) {
4309 i_id++;
4310 continue;
4311 }
4312
4313 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
4314 sizeof *id_stats, RDMA_NL_RDMA_CM,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -05004315 RDMA_NL_RDMA_CM_ID_STATS,
4316 NLM_F_MULTI);
Nir Muchtar753f6182011-01-03 15:33:53 +00004317 if (!id_stats)
4318 goto out;
4319
4320 memset(id_stats, 0, sizeof *id_stats);
4321 id = &id_priv->id;
4322 id_stats->node_type = id->route.addr.dev_addr.dev_type;
4323 id_stats->port_num = id->port_num;
4324 id_stats->bound_dev_if =
4325 id->route.addr.dev_addr.bound_dev_if;
4326
Sean Heftyce117ff2013-05-29 10:09:34 -07004327 if (ibnl_put_attr(skb, nlh,
4328 rdma_addr_size(cma_src_addr(id_priv)),
4329 cma_src_addr(id_priv),
4330 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
4331 goto out;
4332 if (ibnl_put_attr(skb, nlh,
4333 rdma_addr_size(cma_src_addr(id_priv)),
4334 cma_dst_addr(id_priv),
4335 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
4336 goto out;
Nir Muchtar753f6182011-01-03 15:33:53 +00004337
Nir Muchtar83e95022011-01-13 13:56:04 +00004338 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00004339 id_stats->port_space = id->ps;
4340 id_stats->cm_state = id_priv->state;
4341 id_stats->qp_num = id_priv->qp_num;
4342 id_stats->qp_type = id->qp_type;
4343
4344 i_id++;
4345 }
4346
4347 cb->args[1] = 0;
4348 i_dev++;
4349 }
4350
4351out:
4352 mutex_unlock(&lock);
4353 cb->args[0] = i_dev;
4354 cb->args[1] = i_id;
4355
4356 return skb->len;
4357}
4358
4359static const struct ibnl_client_cbs cma_cb_table[] = {
Gao feng809d5fc2012-10-04 20:15:49 +00004360 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
4361 .module = THIS_MODULE },
Nir Muchtar753f6182011-01-03 15:33:53 +00004362};
4363
Haggai Eran4be74b42015-10-22 15:20:09 +03004364static int cma_init_net(struct net *net)
4365{
4366 struct cma_pernet *pernet = cma_pernet(net);
4367
4368 idr_init(&pernet->tcp_ps);
4369 idr_init(&pernet->udp_ps);
4370 idr_init(&pernet->ipoib_ps);
4371 idr_init(&pernet->ib_ps);
4372
4373 return 0;
4374}
4375
4376static void cma_exit_net(struct net *net)
4377{
4378 struct cma_pernet *pernet = cma_pernet(net);
4379
4380 idr_destroy(&pernet->tcp_ps);
4381 idr_destroy(&pernet->udp_ps);
4382 idr_destroy(&pernet->ipoib_ps);
4383 idr_destroy(&pernet->ib_ps);
4384}
4385
4386static struct pernet_operations cma_pernet_operations = {
4387 .init = cma_init_net,
4388 .exit = cma_exit_net,
4389 .id = &cma_pernet_id,
4390 .size = sizeof(struct cma_pernet),
4391};
4392
Peter Huewe716abb12009-06-23 10:38:42 -07004393static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004394{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09004395 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004396
Bhaktipriya Shridhardee9acb2016-08-15 23:29:35 +05304397 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
Sean Heftye51060f2006-06-17 20:37:29 -07004398 if (!cma_wq)
4399 return -ENOMEM;
4400
Haggai Eran4be74b42015-10-22 15:20:09 +03004401 ret = register_pernet_subsys(&cma_pernet_operations);
4402 if (ret)
4403 goto err_wq;
4404
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004405 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08004406 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004407 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004408
Sean Heftye51060f2006-06-17 20:37:29 -07004409 ret = ib_register_client(&cma_client);
4410 if (ret)
4411 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00004412
Mark Bloch2fa2d4f2016-05-06 22:45:26 +03004413 if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table),
4414 cma_cb_table))
Parav Panditaba25a3e2016-03-02 00:50:29 +05304415 pr_warn("RDMA CMA: failed to add netlink callback\n");
Matan Barak045959d2015-12-23 14:56:55 +02004416 cma_configfs_init();
Nir Muchtar753f6182011-01-03 15:33:53 +00004417
Sean Heftye51060f2006-06-17 20:37:29 -07004418 return 0;
4419
4420err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004421 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004422 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004423 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004424err_wq:
Sean Heftye51060f2006-06-17 20:37:29 -07004425 destroy_workqueue(cma_wq);
4426 return ret;
4427}
4428
Peter Huewe716abb12009-06-23 10:38:42 -07004429static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004430{
Matan Barak045959d2015-12-23 14:56:55 +02004431 cma_configfs_exit();
Nir Muchtar753f6182011-01-03 15:33:53 +00004432 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07004433 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004434 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004435 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004436 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004437 unregister_pernet_subsys(&cma_pernet_operations);
Sean Heftye51060f2006-06-17 20:37:29 -07004438 destroy_workqueue(cma_wq);
Sean Heftye51060f2006-06-17 20:37:29 -07004439}
4440
4441module_init(cma_init);
4442module_exit(cma_cleanup);