blob: 85d4ef319c905ee8112bc0f9100a5669a46f9e93 [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
Parav Pandit422013e2018-08-30 08:35:19 +0300676 mutex_lock(&lock);
Sean Heftyf17df3b2013-05-29 10:09:17 -0700677 list_for_each_entry(cur_dev, &dev_list, list) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700678 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
Michael Wang30a74ef2015-05-05 14:50:39 +0200679 if (!rdma_cap_af_ib(cur_dev->device, p))
Michael Wangfef60902015-05-05 14:50:31 +0200680 continue;
681
Sean Heftyf17df3b2013-05-29 10:09:17 -0700682 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
683 continue;
684
Matan Barak55ee3ab2015-10-15 18:38:45 +0300685 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
686 &gid, NULL);
687 i++) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700688 if (!memcmp(&gid, dgid, sizeof(gid))) {
689 cma_dev = cur_dev;
690 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700691 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700692 goto found;
693 }
694
695 if (!cma_dev && (gid.global.subnet_prefix ==
696 dgid->global.subnet_prefix)) {
697 cma_dev = cur_dev;
698 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700699 id_priv->id.port_num = p;
Parav Pandit422013e2018-08-30 08:35:19 +0300700 goto found;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700701 }
702 }
703 }
704 }
Parav Pandit422013e2018-08-30 08:35:19 +0300705 mutex_unlock(&lock);
706 return -ENODEV;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700707
708found:
709 cma_attach_to_dev(id_priv, cma_dev);
Parav Pandit422013e2018-08-30 08:35:19 +0300710 mutex_unlock(&lock);
711 addr = (struct sockaddr_ib *)cma_src_addr(id_priv);
712 memcpy(&addr->sib_addr, &sgid, sizeof(sgid));
Sean Heftyf17df3b2013-05-29 10:09:17 -0700713 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
714 return 0;
715}
716
Sean Heftye51060f2006-06-17 20:37:29 -0700717static void cma_deref_id(struct rdma_id_private *id_priv)
718{
719 if (atomic_dec_and_test(&id_priv->refcount))
720 complete(&id_priv->comp);
721}
722
Guy Shapirofa201052015-10-22 15:20:10 +0300723struct rdma_cm_id *rdma_create_id(struct net *net,
724 rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000725 void *context, enum rdma_port_space ps,
726 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700727{
728 struct rdma_id_private *id_priv;
729
730 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
731 if (!id_priv)
732 return ERR_PTR(-ENOMEM);
733
Nir Muchtar83e95022011-01-13 13:56:04 +0000734 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700735 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700736 id_priv->id.context = context;
737 id_priv->id.event_handler = event_handler;
738 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000739 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700740 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700741 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700742 init_completion(&id_priv->comp);
743 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700744 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700745 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800746 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700747 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
Guy Shapirofa201052015-10-22 15:20:10 +0300748 id_priv->id.route.addr.dev_addr.net = get_net(net);
Moni Shoua7fb8b5d2017-11-26 20:23:54 +0200749 id_priv->seq_num &= 0x00ffffff;
Sean Heftye51060f2006-06-17 20:37:29 -0700750
751 return &id_priv->id;
752}
753EXPORT_SYMBOL(rdma_create_id);
754
Sean Heftyc8f6a362007-02-15 17:00:18 -0800755static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700756{
757 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800758 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700759
Sean Heftyc8f6a362007-02-15 17:00:18 -0800760 qp_attr.qp_state = IB_QPS_INIT;
761 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700762 if (ret)
763 return ret;
764
Sean Heftyc8f6a362007-02-15 17:00:18 -0800765 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
766 if (ret)
767 return ret;
768
769 qp_attr.qp_state = IB_QPS_RTR;
770 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
771 if (ret)
772 return ret;
773
774 qp_attr.qp_state = IB_QPS_RTS;
775 qp_attr.sq_psn = 0;
776 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
777
778 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700779}
780
Sean Heftyc8f6a362007-02-15 17:00:18 -0800781static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500782{
783 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800784 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500785
786 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800787 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
788 if (ret)
789 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500790
Sean Heftyc8f6a362007-02-15 17:00:18 -0800791 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500792}
793
Sean Heftye51060f2006-06-17 20:37:29 -0700794int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
795 struct ib_qp_init_attr *qp_init_attr)
796{
797 struct rdma_id_private *id_priv;
798 struct ib_qp *qp;
799 int ret;
800
801 id_priv = container_of(id, struct rdma_id_private, id);
802 if (id->device != pd->device)
803 return -EINVAL;
804
Christoph Hellwig0691a282016-05-03 18:01:03 +0200805 qp_init_attr->port_num = id->port_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700806 qp = ib_create_qp(pd, qp_init_attr);
807 if (IS_ERR(qp))
808 return PTR_ERR(qp);
809
Sean Heftyb26f9b92010-04-01 17:08:41 +0000810 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800811 ret = cma_init_ud_qp(id_priv, qp);
812 else
813 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700814 if (ret)
815 goto err;
816
817 id->qp = qp;
818 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700819 id_priv->srq = (qp->srq != NULL);
820 return 0;
821err:
822 ib_destroy_qp(qp);
823 return ret;
824}
825EXPORT_SYMBOL(rdma_create_qp);
826
827void rdma_destroy_qp(struct rdma_cm_id *id)
828{
Sean Heftyc5483382007-09-24 13:19:09 -0700829 struct rdma_id_private *id_priv;
830
831 id_priv = container_of(id, struct rdma_id_private, id);
832 mutex_lock(&id_priv->qp_mutex);
833 ib_destroy_qp(id_priv->id.qp);
834 id_priv->id.qp = NULL;
835 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700836}
837EXPORT_SYMBOL(rdma_destroy_qp);
838
Sean Hefty5851bb82008-01-04 10:47:12 -0800839static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
840 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700841{
842 struct ib_qp_attr qp_attr;
843 int qp_attr_mask, ret;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200844 union ib_gid sgid;
Sean Heftye51060f2006-06-17 20:37:29 -0700845
Sean Heftyc5483382007-09-24 13:19:09 -0700846 mutex_lock(&id_priv->qp_mutex);
847 if (!id_priv->id.qp) {
848 ret = 0;
849 goto out;
850 }
Sean Heftye51060f2006-06-17 20:37:29 -0700851
852 /* Need to update QP attributes from default values. */
853 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700854 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700855 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700856 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700857
Sean Heftyc5483382007-09-24 13:19:09 -0700858 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700859 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700860 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700861
862 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700863 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700864 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700865 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700866
Matan Barakdd5f03b2013-12-12 18:03:11 +0200867 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300868 qp_attr.ah_attr.grh.sgid_index, &sgid, NULL);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200869 if (ret)
870 goto out;
871
Michael Wangfef60902015-05-05 14:50:31 +0200872 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
873
Sean Hefty5851bb82008-01-04 10:47:12 -0800874 if (conn_param)
875 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700876 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
877out:
878 mutex_unlock(&id_priv->qp_mutex);
879 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700880}
881
Sean Hefty5851bb82008-01-04 10:47:12 -0800882static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
883 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700884{
885 struct ib_qp_attr qp_attr;
886 int qp_attr_mask, ret;
887
Sean Heftyc5483382007-09-24 13:19:09 -0700888 mutex_lock(&id_priv->qp_mutex);
889 if (!id_priv->id.qp) {
890 ret = 0;
891 goto out;
892 }
Sean Heftye51060f2006-06-17 20:37:29 -0700893
894 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700895 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700896 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700897 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700898
Sean Hefty5851bb82008-01-04 10:47:12 -0800899 if (conn_param)
900 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700901 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
902out:
903 mutex_unlock(&id_priv->qp_mutex);
904 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700905}
906
Sean Heftyc5483382007-09-24 13:19:09 -0700907static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700908{
909 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700910 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700911
Sean Heftyc5483382007-09-24 13:19:09 -0700912 mutex_lock(&id_priv->qp_mutex);
913 if (!id_priv->id.qp) {
914 ret = 0;
915 goto out;
916 }
Sean Heftye51060f2006-06-17 20:37:29 -0700917
918 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700919 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
920out:
921 mutex_unlock(&id_priv->qp_mutex);
922 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700923}
924
Sean Heftyc8f6a362007-02-15 17:00:18 -0800925static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
926 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
927{
928 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
929 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200930 u16 pkey;
931
Michael Wang227128f2015-05-05 14:50:40 +0200932 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
Eli Cohen3c86aa72010-10-13 21:26:51 +0200933 pkey = 0xffff;
Michael Wangfef60902015-05-05 14:50:31 +0200934 else
935 pkey = ib_addr_get_pkey(dev_addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800936
937 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200938 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800939 if (ret)
940 return ret;
941
942 qp_attr->port_num = id_priv->id.port_num;
943 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
944
Sean Heftyb26f9b92010-04-01 17:08:41 +0000945 if (id_priv->id.qp_type == IB_QPT_UD) {
Sean Hefty5c438132013-05-29 10:09:23 -0700946 ret = cma_set_qkey(id_priv, 0);
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700947 if (ret)
948 return ret;
949
Sean Heftyc8f6a362007-02-15 17:00:18 -0800950 qp_attr->qkey = id_priv->qkey;
951 *qp_attr_mask |= IB_QP_QKEY;
952 } else {
953 qp_attr->qp_access_flags = 0;
954 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
955 }
956 return 0;
957}
958
Sean Heftye51060f2006-06-17 20:37:29 -0700959int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
960 int *qp_attr_mask)
961{
962 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800963 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700964
965 id_priv = container_of(id, struct rdma_id_private, id);
Michael Wang72219cea2015-05-05 14:50:34 +0200966 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +0000967 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800968 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
969 else
970 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
971 qp_attr_mask);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200972
Sean Heftye51060f2006-06-17 20:37:29 -0700973 if (qp_attr->qp_state == IB_QPS_RTR)
974 qp_attr->rq_psn = id_priv->seq_num;
Michael Wang04215332015-05-05 14:50:35 +0200975 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800976 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300977 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800978 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
979 } else
980 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
981 qp_attr_mask);
Ismail, Mustafadb297532017-07-14 09:41:31 -0500982 qp_attr->port_num = id_priv->id.port_num;
983 *qp_attr_mask |= IB_QP_PORT;
Michael Wang21655af2015-05-05 14:50:27 +0200984 } else
Sean Heftye51060f2006-06-17 20:37:29 -0700985 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -0700986
987 return ret;
988}
989EXPORT_SYMBOL(rdma_init_qp_attr);
990
991static inline int cma_zero_addr(struct sockaddr *addr)
992{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700993 switch (addr->sa_family) {
994 case AF_INET:
995 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
996 case AF_INET6:
997 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
998 case AF_IB:
999 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
1000 default:
1001 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001002 }
1003}
1004
1005static inline int cma_loopback_addr(struct sockaddr *addr)
1006{
Sean Hefty2e2d1902013-05-29 10:09:09 -07001007 switch (addr->sa_family) {
1008 case AF_INET:
1009 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
1010 case AF_INET6:
1011 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
1012 case AF_IB:
1013 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
1014 default:
1015 return 0;
1016 }
Sean Heftye51060f2006-06-17 20:37:29 -07001017}
1018
1019static inline int cma_any_addr(struct sockaddr *addr)
1020{
1021 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1022}
1023
Hefty, Sean43b752d2011-05-09 22:06:10 -07001024static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
1025{
1026 if (src->sa_family != dst->sa_family)
1027 return -1;
1028
1029 switch (src->sa_family) {
1030 case AF_INET:
1031 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
1032 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
Sean Hefty2e2d1902013-05-29 10:09:09 -07001033 case AF_INET6:
Hefty, Sean43b752d2011-05-09 22:06:10 -07001034 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
1035 &((struct sockaddr_in6 *) dst)->sin6_addr);
Sean Hefty2e2d1902013-05-29 10:09:09 -07001036 default:
1037 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1038 &((struct sockaddr_ib *) dst)->sib_addr);
Hefty, Sean43b752d2011-05-09 22:06:10 -07001039 }
1040}
1041
Sean Hefty58afdcb2013-05-29 10:09:11 -07001042static __be16 cma_port(struct sockaddr *addr)
Sean Hefty628e5f62006-11-30 16:44:16 -08001043{
Sean Hefty58afdcb2013-05-29 10:09:11 -07001044 struct sockaddr_ib *sib;
1045
1046 switch (addr->sa_family) {
1047 case AF_INET:
Sean Hefty628e5f62006-11-30 16:44:16 -08001048 return ((struct sockaddr_in *) addr)->sin_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -07001049 case AF_INET6:
Sean Hefty628e5f62006-11-30 16:44:16 -08001050 return ((struct sockaddr_in6 *) addr)->sin6_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -07001051 case AF_IB:
1052 sib = (struct sockaddr_ib *) addr;
1053 return htons((u16) (be64_to_cpu(sib->sib_sid) &
1054 be64_to_cpu(sib->sib_sid_mask)));
1055 default:
1056 return 0;
1057 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001058}
1059
Sean Heftye51060f2006-06-17 20:37:29 -07001060static inline int cma_any_port(struct sockaddr *addr)
1061{
Sean Hefty628e5f62006-11-30 16:44:16 -08001062 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -07001063}
1064
Haggai Eran0c505f72015-07-30 17:50:19 +03001065static void cma_save_ib_info(struct sockaddr *src_addr,
1066 struct sockaddr *dst_addr,
1067 struct rdma_cm_id *listen_id,
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001068 struct ib_sa_path_rec *path)
Sean Heftye51060f2006-06-17 20:37:29 -07001069{
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001070 struct sockaddr_ib *listen_ib, *ib;
Sean Heftye51060f2006-06-17 20:37:29 -07001071
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001072 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001073 if (src_addr) {
1074 ib = (struct sockaddr_ib *)src_addr;
1075 ib->sib_family = AF_IB;
1076 if (path) {
1077 ib->sib_pkey = path->pkey;
1078 ib->sib_flowinfo = path->flow_label;
1079 memcpy(&ib->sib_addr, &path->sgid, 16);
1080 ib->sib_sid = path->service_id;
1081 ib->sib_scope_id = 0;
1082 } else {
1083 ib->sib_pkey = listen_ib->sib_pkey;
1084 ib->sib_flowinfo = listen_ib->sib_flowinfo;
1085 ib->sib_addr = listen_ib->sib_addr;
1086 ib->sib_sid = listen_ib->sib_sid;
1087 ib->sib_scope_id = listen_ib->sib_scope_id;
1088 }
1089 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
Matthew Finlayc07678b2015-05-19 00:11:48 -07001090 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001091 if (dst_addr) {
1092 ib = (struct sockaddr_ib *)dst_addr;
1093 ib->sib_family = AF_IB;
1094 if (path) {
1095 ib->sib_pkey = path->pkey;
1096 ib->sib_flowinfo = path->flow_label;
1097 memcpy(&ib->sib_addr, &path->dgid, 16);
1098 }
Matthew Finlayc07678b2015-05-19 00:11:48 -07001099 }
Sean Heftye51060f2006-06-17 20:37:29 -07001100}
1101
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001102static void cma_save_ip4_info(struct sockaddr_in *src_addr,
1103 struct sockaddr_in *dst_addr,
Haggai Eran0c505f72015-07-30 17:50:19 +03001104 struct cma_hdr *hdr,
1105 __be16 local_port)
Sean Heftye51060f2006-06-17 20:37:29 -07001106{
Haggai Eran0c505f72015-07-30 17:50:19 +03001107 if (src_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001108 *src_addr = (struct sockaddr_in) {
1109 .sin_family = AF_INET,
1110 .sin_addr.s_addr = hdr->dst_addr.ip4.addr,
1111 .sin_port = local_port,
1112 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001113 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001114
Haggai Eran0c505f72015-07-30 17:50:19 +03001115 if (dst_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001116 *dst_addr = (struct sockaddr_in) {
1117 .sin_family = AF_INET,
1118 .sin_addr.s_addr = hdr->src_addr.ip4.addr,
1119 .sin_port = hdr->port,
1120 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001121 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001122}
1123
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001124static void cma_save_ip6_info(struct sockaddr_in6 *src_addr,
1125 struct sockaddr_in6 *dst_addr,
Haggai Eran0c505f72015-07-30 17:50:19 +03001126 struct cma_hdr *hdr,
1127 __be16 local_port)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001128{
Haggai Eran0c505f72015-07-30 17:50:19 +03001129 if (src_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001130 *src_addr = (struct sockaddr_in6) {
1131 .sin6_family = AF_INET6,
1132 .sin6_addr = hdr->dst_addr.ip6,
1133 .sin6_port = local_port,
1134 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001135 }
Sean Heftye51060f2006-06-17 20:37:29 -07001136
Haggai Eran0c505f72015-07-30 17:50:19 +03001137 if (dst_addr) {
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001138 *dst_addr = (struct sockaddr_in6) {
1139 .sin6_family = AF_INET6,
1140 .sin6_addr = hdr->src_addr.ip6,
1141 .sin6_port = hdr->port,
1142 };
Haggai Eran0c505f72015-07-30 17:50:19 +03001143 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001144}
1145
Haggai Eran0c505f72015-07-30 17:50:19 +03001146static u16 cma_port_from_service_id(__be64 service_id)
1147{
1148 return (u16)be64_to_cpu(service_id);
1149}
1150
1151static int cma_save_ip_info(struct sockaddr *src_addr,
1152 struct sockaddr *dst_addr,
1153 struct ib_cm_event *ib_event,
1154 __be64 service_id)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001155{
1156 struct cma_hdr *hdr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001157 __be16 port;
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001158
1159 hdr = ib_event->private_data;
1160 if (hdr->cma_version != CMA_VERSION)
1161 return -EINVAL;
1162
Haggai Eran0c505f72015-07-30 17:50:19 +03001163 port = htons(cma_port_from_service_id(service_id));
1164
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001165 switch (cma_get_ip_ver(hdr)) {
1166 case 4:
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001167 cma_save_ip4_info((struct sockaddr_in *)src_addr,
1168 (struct sockaddr_in *)dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001169 break;
1170 case 6:
Arnd Bergmannc50e90d2016-11-10 17:44:52 +01001171 cma_save_ip6_info((struct sockaddr_in6 *)src_addr,
1172 (struct sockaddr_in6 *)dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001173 break;
1174 default:
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001175 return -EAFNOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07001176 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001177
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001178 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001179}
1180
Haggai Eran0c505f72015-07-30 17:50:19 +03001181static int cma_save_net_info(struct sockaddr *src_addr,
1182 struct sockaddr *dst_addr,
1183 struct rdma_cm_id *listen_id,
1184 struct ib_cm_event *ib_event,
1185 sa_family_t sa_family, __be64 service_id)
1186{
1187 if (sa_family == AF_IB) {
1188 if (ib_event->event == IB_CM_REQ_RECEIVED)
1189 cma_save_ib_info(src_addr, dst_addr, listen_id,
1190 ib_event->param.req_rcvd.primary_path);
1191 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1192 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1193 return 0;
1194 }
1195
1196 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1197}
1198
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001199static int cma_save_req_info(const struct ib_cm_event *ib_event,
1200 struct cma_req_info *req)
1201{
1202 const struct ib_cm_req_event_param *req_param =
1203 &ib_event->param.req_rcvd;
1204 const struct ib_cm_sidr_req_event_param *sidr_param =
1205 &ib_event->param.sidr_req_rcvd;
1206
1207 switch (ib_event->event) {
1208 case IB_CM_REQ_RECEIVED:
1209 req->device = req_param->listen_id->device;
1210 req->port = req_param->port;
1211 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1212 sizeof(req->local_gid));
1213 req->has_gid = true;
1214 req->service_id = req_param->primary_path->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001215 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
Haggai Eran84424a72016-02-29 15:45:12 +02001216 if (req->pkey != req_param->bth_pkey)
1217 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1218 "RDMA CMA: in the future this may cause the request to be dropped\n",
1219 req_param->bth_pkey, req->pkey);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001220 break;
1221 case IB_CM_SIDR_REQ_RECEIVED:
1222 req->device = sidr_param->listen_id->device;
1223 req->port = sidr_param->port;
1224 req->has_gid = false;
1225 req->service_id = sidr_param->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001226 req->pkey = sidr_param->pkey;
Haggai Eran84424a72016-02-29 15:45:12 +02001227 if (req->pkey != sidr_param->bth_pkey)
1228 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1229 "RDMA CMA: in the future this may cause the request to be dropped\n",
1230 sidr_param->bth_pkey, req->pkey);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001231 break;
1232 default:
1233 return -EINVAL;
1234 }
1235
1236 return 0;
1237}
1238
Haggai Eranf887f2a2015-07-30 17:50:23 +03001239static bool validate_ipv4_net_dev(struct net_device *net_dev,
1240 const struct sockaddr_in *dst_addr,
1241 const struct sockaddr_in *src_addr)
1242{
1243 __be32 daddr = dst_addr->sin_addr.s_addr,
1244 saddr = src_addr->sin_addr.s_addr;
1245 struct fib_result res;
1246 struct flowi4 fl4;
1247 int err;
1248 bool ret;
1249
1250 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1251 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1252 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1253 ipv4_is_loopback(saddr))
1254 return false;
1255
1256 memset(&fl4, 0, sizeof(fl4));
1257 fl4.flowi4_iif = net_dev->ifindex;
1258 fl4.daddr = daddr;
1259 fl4.saddr = saddr;
1260
1261 rcu_read_lock();
1262 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
Bart Van Assched3632492015-11-20 11:04:12 -08001263 ret = err == 0 && FIB_RES_DEV(res) == net_dev;
Haggai Eranf887f2a2015-07-30 17:50:23 +03001264 rcu_read_unlock();
1265
1266 return ret;
1267}
1268
1269static bool validate_ipv6_net_dev(struct net_device *net_dev,
1270 const struct sockaddr_in6 *dst_addr,
1271 const struct sockaddr_in6 *src_addr)
1272{
1273#if IS_ENABLED(CONFIG_IPV6)
1274 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1275 IPV6_ADDR_LINKLOCAL;
1276 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1277 &src_addr->sin6_addr, net_dev->ifindex,
1278 strict);
1279 bool ret;
1280
1281 if (!rt)
1282 return false;
1283
1284 ret = rt->rt6i_idev->dev == net_dev;
1285 ip6_rt_put(rt);
1286
1287 return ret;
1288#else
1289 return false;
1290#endif
1291}
1292
1293static bool validate_net_dev(struct net_device *net_dev,
1294 const struct sockaddr *daddr,
1295 const struct sockaddr *saddr)
1296{
1297 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1298 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1299 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1300 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1301
1302 switch (daddr->sa_family) {
1303 case AF_INET:
1304 return saddr->sa_family == AF_INET &&
1305 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1306
1307 case AF_INET6:
1308 return saddr->sa_family == AF_INET6 &&
1309 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1310
1311 default:
1312 return false;
1313 }
1314}
1315
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001316static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1317 const struct cma_req_info *req)
1318{
Haggai Eranf887f2a2015-07-30 17:50:23 +03001319 struct sockaddr_storage listen_addr_storage, src_addr_storage;
1320 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1321 *src_addr = (struct sockaddr *)&src_addr_storage;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001322 struct net_device *net_dev;
1323 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1324 int err;
1325
Haggai Eranf887f2a2015-07-30 17:50:23 +03001326 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1327 req->service_id);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001328 if (err)
1329 return ERR_PTR(err);
1330
1331 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1332 gid, listen_addr);
1333 if (!net_dev)
1334 return ERR_PTR(-ENODEV);
1335
Haggai Eranf887f2a2015-07-30 17:50:23 +03001336 if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1337 dev_put(net_dev);
1338 return ERR_PTR(-EHOSTUNREACH);
1339 }
1340
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001341 return net_dev;
1342}
1343
1344static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1345{
1346 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1347}
1348
1349static bool cma_match_private_data(struct rdma_id_private *id_priv,
1350 const struct cma_hdr *hdr)
1351{
1352 struct sockaddr *addr = cma_src_addr(id_priv);
1353 __be32 ip4_addr;
1354 struct in6_addr ip6_addr;
1355
1356 if (cma_any_addr(addr) && !id_priv->afonly)
1357 return true;
1358
1359 switch (addr->sa_family) {
1360 case AF_INET:
1361 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1362 if (cma_get_ip_ver(hdr) != 4)
1363 return false;
1364 if (!cma_any_addr(addr) &&
1365 hdr->dst_addr.ip4.addr != ip4_addr)
1366 return false;
1367 break;
1368 case AF_INET6:
1369 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1370 if (cma_get_ip_ver(hdr) != 6)
1371 return false;
1372 if (!cma_any_addr(addr) &&
1373 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1374 return false;
1375 break;
1376 case AF_IB:
1377 return true;
1378 default:
1379 return false;
1380 }
1381
1382 return true;
1383}
1384
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001385static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1386{
1387 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1388 enum rdma_transport_type transport =
1389 rdma_node_get_transport(device->node_type);
1390
1391 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1392}
1393
1394static bool cma_protocol_roce(const struct rdma_cm_id *id)
1395{
1396 struct ib_device *device = id->device;
1397 const int port_num = id->port_num ?: rdma_start_port(device);
1398
1399 return cma_protocol_roce_dev_port(device, port_num);
1400}
1401
Matan Barakfac51592015-12-21 17:01:24 +02001402static bool cma_match_net_dev(const struct rdma_cm_id *id,
1403 const struct net_device *net_dev,
1404 u8 port_num)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001405{
Matan Barakfac51592015-12-21 17:01:24 +02001406 const struct rdma_addr *addr = &id->route.addr;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001407
1408 if (!net_dev)
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001409 /* This request is an AF_IB request or a RoCE request */
Matan Barakfac51592015-12-21 17:01:24 +02001410 return (!id->port_num || id->port_num == port_num) &&
1411 (addr->src_addr.ss_family == AF_IB ||
1412 cma_protocol_roce_dev_port(id->device, port_num));
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001413
Parav Pandit64b40132018-07-16 11:50:13 +03001414 /*
1415 * Net namespaces must match, and if the listner is listening
1416 * on a specific netdevice than netdevice must match as well.
1417 */
1418 if (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
1419 (!!addr->dev_addr.bound_dev_if ==
1420 (addr->dev_addr.bound_dev_if == net_dev->ifindex)))
1421 return true;
1422 else
1423 return false;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001424}
1425
1426static struct rdma_id_private *cma_find_listener(
1427 const struct rdma_bind_list *bind_list,
1428 const struct ib_cm_id *cm_id,
1429 const struct ib_cm_event *ib_event,
1430 const struct cma_req_info *req,
1431 const struct net_device *net_dev)
1432{
1433 struct rdma_id_private *id_priv, *id_priv_dev;
1434
1435 if (!bind_list)
1436 return ERR_PTR(-EINVAL);
1437
1438 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1439 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1440 if (id_priv->id.device == cm_id->device &&
Matan Barakfac51592015-12-21 17:01:24 +02001441 cma_match_net_dev(&id_priv->id, net_dev, req->port))
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001442 return id_priv;
1443 list_for_each_entry(id_priv_dev,
1444 &id_priv->listen_list,
1445 listen_list) {
1446 if (id_priv_dev->id.device == cm_id->device &&
Matan Barakfac51592015-12-21 17:01:24 +02001447 cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001448 return id_priv_dev;
1449 }
1450 }
1451 }
1452
1453 return ERR_PTR(-EINVAL);
1454}
1455
1456static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001457 struct ib_cm_event *ib_event,
1458 struct net_device **net_dev)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001459{
1460 struct cma_req_info req;
1461 struct rdma_bind_list *bind_list;
1462 struct rdma_id_private *id_priv;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001463 int err;
1464
1465 err = cma_save_req_info(ib_event, &req);
1466 if (err)
1467 return ERR_PTR(err);
1468
Haggai Eran0b3ca762015-07-30 17:50:24 +03001469 *net_dev = cma_get_net_dev(ib_event, &req);
1470 if (IS_ERR(*net_dev)) {
1471 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001472 /* Assuming the protocol is AF_IB */
Haggai Eran0b3ca762015-07-30 17:50:24 +03001473 *net_dev = NULL;
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001474 } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1475 /* TODO find the net dev matching the request parameters
1476 * through the RoCE GID table */
1477 *net_dev = NULL;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001478 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001479 return ERR_CAST(*net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001480 }
1481 }
1482
Guy Shapirofa201052015-10-22 15:20:10 +03001483 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
Haggai Eran4be74b42015-10-22 15:20:09 +03001484 rdma_ps_from_service_id(req.service_id),
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001485 cma_port_from_service_id(req.service_id));
Haggai Eran0b3ca762015-07-30 17:50:24 +03001486 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
Haggai Eranb3b51f92015-09-21 16:02:02 +03001487 if (IS_ERR(id_priv) && *net_dev) {
Haggai Eranbe688192015-08-27 15:55:15 +03001488 dev_put(*net_dev);
1489 *net_dev = NULL;
1490 }
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001491
1492 return id_priv;
1493}
1494
Bart Van Assche0c70b352017-10-11 10:48:45 -07001495static inline u8 cma_user_data_offset(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07001496{
Sean Heftye8160e12013-05-29 10:09:22 -07001497 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
Sean Heftye51060f2006-06-17 20:37:29 -07001498}
1499
Sean Heftye51060f2006-06-17 20:37:29 -07001500static void cma_cancel_route(struct rdma_id_private *id_priv)
1501{
Michael Wangfe53ba22015-05-05 14:50:36 +02001502 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001503 if (id_priv->query)
1504 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001505 }
1506}
1507
Sean Heftye51060f2006-06-17 20:37:29 -07001508static void cma_cancel_listens(struct rdma_id_private *id_priv)
1509{
1510 struct rdma_id_private *dev_id_priv;
1511
Sean Heftyd02d1f52007-10-09 11:12:34 -07001512 /*
1513 * Remove from listen_any_list to prevent added devices from spawning
1514 * additional listen requests.
1515 */
Sean Heftye51060f2006-06-17 20:37:29 -07001516 mutex_lock(&lock);
1517 list_del(&id_priv->list);
1518
1519 while (!list_empty(&id_priv->listen_list)) {
1520 dev_id_priv = list_entry(id_priv->listen_list.next,
1521 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001522 /* sync with device removal to avoid duplicate destruction */
1523 list_del_init(&dev_id_priv->list);
1524 list_del(&dev_id_priv->listen_list);
1525 mutex_unlock(&lock);
1526
1527 rdma_destroy_id(&dev_id_priv->id);
1528 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001529 }
1530 mutex_unlock(&lock);
1531}
1532
1533static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001534 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -07001535{
1536 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001537 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001538 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1539 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001540 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001541 cma_cancel_route(id_priv);
1542 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001543 case RDMA_CM_LISTEN:
Sean Heftyf4753832013-05-29 10:09:14 -07001544 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001545 cma_cancel_listens(id_priv);
1546 break;
1547 default:
1548 break;
1549 }
1550}
1551
1552static void cma_release_port(struct rdma_id_private *id_priv)
1553{
1554 struct rdma_bind_list *bind_list = id_priv->bind_list;
Guy Shapirofa201052015-10-22 15:20:10 +03001555 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07001556
1557 if (!bind_list)
1558 return;
1559
1560 mutex_lock(&lock);
1561 hlist_del(&id_priv->node);
1562 if (hlist_empty(&bind_list->owners)) {
Guy Shapirofa201052015-10-22 15:20:10 +03001563 cma_ps_remove(net, bind_list->ps, bind_list->port);
Sean Heftye51060f2006-06-17 20:37:29 -07001564 kfree(bind_list);
1565 }
1566 mutex_unlock(&lock);
1567}
1568
Sean Heftyc8f6a362007-02-15 17:00:18 -08001569static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1570{
1571 struct cma_multicast *mc;
1572
1573 while (!list_empty(&id_priv->mc_list)) {
1574 mc = container_of(id_priv->mc_list.next,
1575 struct cma_multicast, list);
1576 list_del(&mc->list);
Michael Wanga31ad3b2015-05-05 14:50:37 +02001577 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
Michael Wang5c9a5282015-05-05 14:50:29 +02001578 id_priv->id.port_num)) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02001579 ib_sa_free_multicast(mc->multicast.ib);
1580 kfree(mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02001581 } else {
1582 if (mc->igmp_joined) {
1583 struct rdma_dev_addr *dev_addr =
1584 &id_priv->id.route.addr.dev_addr;
1585 struct net_device *ndev = NULL;
1586
1587 if (dev_addr->bound_dev_if)
1588 ndev = dev_get_by_index(&init_net,
1589 dev_addr->bound_dev_if);
1590 if (ndev) {
1591 cma_igmp_send(ndev,
1592 &mc->multicast.ib->rec.mgid,
1593 false);
1594 dev_put(ndev);
1595 }
1596 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02001597 kref_put(&mc->mcref, release_mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02001598 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001599 }
1600}
1601
Sean Heftye51060f2006-06-17 20:37:29 -07001602void rdma_destroy_id(struct rdma_cm_id *id)
1603{
1604 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001605 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -07001606
1607 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001608 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001609 cma_cancel_operation(id_priv, state);
1610
Sean Heftya396d432011-02-23 09:05:39 -08001611 /*
1612 * Wait for any active callback to finish. New callbacks will find
1613 * the id_priv state set to destroying and abort.
1614 */
1615 mutex_lock(&id_priv->handler_mutex);
1616 mutex_unlock(&id_priv->handler_mutex);
1617
Sean Heftye51060f2006-06-17 20:37:29 -07001618 if (id_priv->cma_dev) {
Michael Wang72219cea2015-05-05 14:50:34 +02001619 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001620 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07001621 ib_destroy_cm_id(id_priv->cm_id.ib);
Michael Wang04215332015-05-05 14:50:35 +02001622 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001623 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -05001624 iw_destroy_cm_id(id_priv->cm_id.iw);
Sean Heftye51060f2006-06-17 20:37:29 -07001625 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001626 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -08001627 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001628 }
1629
1630 cma_release_port(id_priv);
1631 cma_deref_id(id_priv);
1632 wait_for_completion(&id_priv->comp);
1633
Sean Heftyd02d1f52007-10-09 11:12:34 -07001634 if (id_priv->internal_id)
1635 cma_deref_id(id_priv->id.context);
1636
Sean Heftye51060f2006-06-17 20:37:29 -07001637 kfree(id_priv->id.route.path_rec);
Guy Shapirofa201052015-10-22 15:20:10 +03001638 put_net(id_priv->id.route.addr.dev_addr.net);
Sean Heftye51060f2006-06-17 20:37:29 -07001639 kfree(id_priv);
1640}
1641EXPORT_SYMBOL(rdma_destroy_id);
1642
1643static int cma_rep_recv(struct rdma_id_private *id_priv)
1644{
1645 int ret;
1646
Sean Hefty5851bb82008-01-04 10:47:12 -08001647 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001648 if (ret)
1649 goto reject;
1650
Sean Hefty5851bb82008-01-04 10:47:12 -08001651 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001652 if (ret)
1653 goto reject;
1654
1655 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1656 if (ret)
1657 goto reject;
1658
1659 return 0;
1660reject:
Sean Heftyc5483382007-09-24 13:19:09 -07001661 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001662 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1663 NULL, 0, NULL, 0);
1664 return ret;
1665}
1666
Sean Heftya1b1b612006-11-30 16:33:14 -08001667static void cma_set_rep_event_data(struct rdma_cm_event *event,
1668 struct ib_cm_rep_event_param *rep_data,
1669 void *private_data)
1670{
1671 event->param.conn.private_data = private_data;
1672 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1673 event->param.conn.responder_resources = rep_data->responder_resources;
1674 event->param.conn.initiator_depth = rep_data->initiator_depth;
1675 event->param.conn.flow_control = rep_data->flow_control;
1676 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1677 event->param.conn.srq = rep_data->srq;
1678 event->param.conn.qp_num = rep_data->remote_qpn;
1679}
1680
Sean Heftye51060f2006-06-17 20:37:29 -07001681static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1682{
1683 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001684 struct rdma_cm_event event;
1685 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001686
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001687 mutex_lock(&id_priv->handler_mutex);
Amir Vadai38ca83a2008-07-22 14:14:23 -07001688 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001689 id_priv->state != RDMA_CM_CONNECT) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001690 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001691 id_priv->state != RDMA_CM_DISCONNECT))
1692 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07001693
Sean Heftya1b1b612006-11-30 16:33:14 -08001694 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001695 switch (ib_event->event) {
1696 case IB_CM_REQ_ERROR:
1697 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001698 event.event = RDMA_CM_EVENT_UNREACHABLE;
1699 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001700 break;
1701 case IB_CM_REP_RECEIVED:
Sean Hefty01602f12013-05-29 10:09:20 -07001702 if (id_priv->id.qp) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001703 event.status = cma_rep_recv(id_priv);
1704 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1705 RDMA_CM_EVENT_ESTABLISHED;
Sean Hefty01602f12013-05-29 10:09:20 -07001706 } else {
Sean Heftya1b1b612006-11-30 16:33:14 -08001707 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
Sean Hefty01602f12013-05-29 10:09:20 -07001708 }
Sean Heftya1b1b612006-11-30 16:33:14 -08001709 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1710 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001711 break;
1712 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001713 case IB_CM_USER_ESTABLISHED:
1714 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001715 break;
1716 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001717 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001718 case IB_CM_DREQ_RECEIVED:
1719 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001720 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1721 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001722 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001723 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001724 break;
1725 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001726 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1727 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001728 case IB_CM_MRA_RECEIVED:
1729 /* ignore event */
1730 goto out;
1731 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001732 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001733 event.status = ib_event->param.rej_rcvd.reason;
1734 event.event = RDMA_CM_EVENT_REJECTED;
1735 event.param.conn.private_data = ib_event->private_data;
1736 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001737 break;
1738 default:
Parav Panditaba25a3e2016-03-02 00:50:29 +05301739 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001740 ib_event->event);
1741 goto out;
1742 }
1743
Sean Heftya1b1b612006-11-30 16:33:14 -08001744 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001745 if (ret) {
1746 /* Destroy the CM ID by returning a non-zero value. */
1747 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001748 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001749 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001750 rdma_destroy_id(&id_priv->id);
1751 return ret;
1752 }
1753out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001754 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001755 return ret;
1756}
1757
Sean Hefty628e5f62006-11-30 16:44:16 -08001758static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001759 struct ib_cm_event *ib_event,
1760 struct net_device *net_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001761{
1762 struct rdma_id_private *id_priv;
1763 struct rdma_cm_id *id;
1764 struct rdma_route *rt;
Haggai Eran0c505f72015-07-30 17:50:19 +03001765 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1766 const __be64 service_id =
1767 ib_event->param.req_rcvd.primary_path->service_id;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001768 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001769
Guy Shapirofa201052015-10-22 15:20:10 +03001770 id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1771 listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001772 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001773 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001774 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001775
Sean Heftyf4753832013-05-29 10:09:14 -07001776 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001777 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1778 (struct sockaddr *)&id->route.addr.dst_addr,
1779 listen_id, ib_event, ss_family, service_id))
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001780 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001781
1782 rt = &id->route;
1783 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1784 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1785 GFP_KERNEL);
1786 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001787 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001788
Sean Heftye51060f2006-06-17 20:37:29 -07001789 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1790 if (rt->num_paths == 2)
1791 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1792
Haggai Eran0b3ca762015-07-30 17:50:24 +03001793 if (net_dev) {
1794 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001795 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001796 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001797 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001798 if (!cma_protocol_roce(listen_id) &&
1799 cma_any_addr(cma_src_addr(id_priv))) {
1800 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1801 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1802 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1803 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
1804 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1805 if (ret)
1806 goto err;
1807 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001808 }
1809 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001810
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001811 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001812 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001813
Krishna Kumar3f168d22006-09-29 12:09:51 -07001814err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001815 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001816 return NULL;
1817}
1818
Sean Hefty628e5f62006-11-30 16:44:16 -08001819static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001820 struct ib_cm_event *ib_event,
1821 struct net_device *net_dev)
Sean Hefty628e5f62006-11-30 16:44:16 -08001822{
1823 struct rdma_id_private *id_priv;
1824 struct rdma_cm_id *id;
Haggai Eran0c505f72015-07-30 17:50:19 +03001825 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
Guy Shapirofa201052015-10-22 15:20:10 +03001826 struct net *net = listen_id->route.addr.dev_addr.net;
Sean Hefty628e5f62006-11-30 16:44:16 -08001827 int ret;
1828
Guy Shapirofa201052015-10-22 15:20:10 +03001829 id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001830 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001831 if (IS_ERR(id))
1832 return NULL;
1833
Sean Heftyf4753832013-05-29 10:09:14 -07001834 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001835 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1836 (struct sockaddr *)&id->route.addr.dst_addr,
1837 listen_id, ib_event, ss_family,
1838 ib_event->param.sidr_req_rcvd.service_id))
Sean Hefty628e5f62006-11-30 16:44:16 -08001839 goto err;
1840
Haggai Eran0b3ca762015-07-30 17:50:24 +03001841 if (net_dev) {
1842 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001843 if (ret)
1844 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001845 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001846 if (!cma_any_addr(cma_src_addr(id_priv))) {
1847 ret = cma_translate_addr(cma_src_addr(id_priv),
1848 &id->route.addr.dev_addr);
1849 if (ret)
1850 goto err;
1851 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001852 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001853
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001854 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001855 return id_priv;
1856err:
1857 rdma_destroy_id(id);
1858 return NULL;
1859}
1860
Sean Heftya1b1b612006-11-30 16:33:14 -08001861static void cma_set_req_event_data(struct rdma_cm_event *event,
1862 struct ib_cm_req_event_param *req_data,
1863 void *private_data, int offset)
1864{
1865 event->param.conn.private_data = private_data + offset;
1866 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1867 event->param.conn.responder_resources = req_data->responder_resources;
1868 event->param.conn.initiator_depth = req_data->initiator_depth;
1869 event->param.conn.flow_control = req_data->flow_control;
1870 event->param.conn.retry_count = req_data->retry_count;
1871 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1872 event->param.conn.srq = req_data->srq;
1873 event->param.conn.qp_num = req_data->remote_qpn;
1874}
1875
Hefty, Sean95954802011-10-06 09:32:33 -07001876static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1877{
Sean Hefty4dd81e82012-06-14 20:49:09 +00001878 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
Hefty, Sean95954802011-10-06 09:32:33 -07001879 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1880 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1881 (id->qp_type == IB_QPT_UD)) ||
1882 (!id->qp_type));
1883}
1884
Sean Heftye51060f2006-06-17 20:37:29 -07001885static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1886{
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001887 struct rdma_id_private *listen_id, *conn_id = NULL;
Sean Heftya1b1b612006-11-30 16:33:14 -08001888 struct rdma_cm_event event;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001889 struct net_device *net_dev;
Bart Van Assche0c70b352017-10-11 10:48:45 -07001890 u8 offset;
1891 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001892
Haggai Eran0b3ca762015-07-30 17:50:24 +03001893 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001894 if (IS_ERR(listen_id))
1895 return PTR_ERR(listen_id);
1896
Haggai Eran0b3ca762015-07-30 17:50:24 +03001897 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1898 ret = -EINVAL;
1899 goto net_dev_put;
1900 }
Hefty, Sean95954802011-10-06 09:32:33 -07001901
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001902 mutex_lock(&listen_id->handler_mutex);
1903 if (listen_id->state != RDMA_CM_LISTEN) {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001904 ret = -ECONNABORTED;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001905 goto err1;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001906 }
Sean Heftye51060f2006-06-17 20:37:29 -07001907
Sean Hefty628e5f62006-11-30 16:44:16 -08001908 memset(&event, 0, sizeof event);
Sean Heftye8160e12013-05-29 10:09:22 -07001909 offset = cma_user_data_offset(listen_id);
Sean Hefty628e5f62006-11-30 16:44:16 -08001910 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001911 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001912 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001913 event.param.ud.private_data = ib_event->private_data + offset;
1914 event.param.ud.private_data_len =
1915 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1916 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001917 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001918 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1919 ib_event->private_data, offset);
1920 }
Sean Heftye51060f2006-06-17 20:37:29 -07001921 if (!conn_id) {
1922 ret = -ENOMEM;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001923 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001924 }
1925
Or Gerlitzde910bd2008-07-14 23:48:53 -07001926 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001927 ret = cma_acquire_dev(conn_id, listen_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301928 if (ret)
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001929 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001930
1931 conn_id->cm_id.ib = cm_id;
1932 cm_id->context = conn_id;
1933 cm_id->cm_handler = cma_ib_handler;
1934
Sean Hefty25ae21a2011-02-23 08:11:32 -08001935 /*
1936 * Protect against the user destroying conn_id from another thread
1937 * until we're done accessing it.
1938 */
1939 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001940 ret = conn_id->id.event_handler(&conn_id->id, &event);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001941 if (ret)
1942 goto err3;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001943 /*
1944 * Acquire mutex to prevent user executing rdma_destroy_id()
1945 * while we're accessing the cm_id.
1946 */
1947 mutex_lock(&lock);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001948 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1949 (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001950 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1951 mutex_unlock(&lock);
1952 mutex_unlock(&conn_id->handler_mutex);
1953 mutex_unlock(&listen_id->handler_mutex);
1954 cma_deref_id(conn_id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001955 if (net_dev)
1956 dev_put(net_dev);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001957 return 0;
1958
1959err3:
1960 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301961 /* Destroy the CM ID by returning a non-zero value. */
1962 conn_id->cm_id.ib = NULL;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001963err2:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001964 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001965 mutex_unlock(&conn_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001966err1:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001967 mutex_unlock(&listen_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001968 if (conn_id)
1969 rdma_destroy_id(&conn_id->id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001970
1971net_dev_put:
1972 if (net_dev)
1973 dev_put(net_dev);
1974
Sean Heftye51060f2006-06-17 20:37:29 -07001975 return ret;
1976}
1977
Sean Heftycf539362013-05-29 10:09:28 -07001978__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
Sean Heftye51060f2006-06-17 20:37:29 -07001979{
Sean Hefty496ce3c2013-05-29 10:09:19 -07001980 if (addr->sa_family == AF_IB)
1981 return ((struct sockaddr_ib *) addr)->sib_sid;
1982
Sean Heftycf539362013-05-29 10:09:28 -07001983 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001984}
Sean Heftycf539362013-05-29 10:09:28 -07001985EXPORT_SYMBOL(rdma_get_service_id);
Sean Heftye51060f2006-06-17 20:37:29 -07001986
Tom Tucker07ebafb2006-08-03 16:02:42 -05001987static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1988{
1989 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001990 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001991 int ret = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05301992 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1993 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001994
Bart Van Assche37e07cd2016-06-10 11:08:25 -07001995 mutex_lock(&id_priv->handler_mutex);
1996 if (id_priv->state != RDMA_CM_CONNECT)
1997 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001998
Sean Heftybe65f082007-05-07 11:49:12 -07001999 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002000 switch (iw_event->event) {
2001 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08002002 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002003 break;
2004 case IW_CM_EVENT_CONNECT_REPLY:
Steve Wise24d44a32013-07-04 16:10:44 +05302005 memcpy(cma_src_addr(id_priv), laddr,
2006 rdma_addr_size(laddr));
2007 memcpy(cma_dst_addr(id_priv), raddr,
2008 rdma_addr_size(raddr));
Steve Wise881a0452006-12-15 16:50:17 -06002009 switch (iw_event->status) {
2010 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08002011 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05302012 event.param.conn.initiator_depth = iw_event->ird;
2013 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06002014 break;
2015 case -ECONNRESET:
2016 case -ECONNREFUSED:
2017 event.event = RDMA_CM_EVENT_REJECTED;
2018 break;
2019 case -ETIMEDOUT:
2020 event.event = RDMA_CM_EVENT_UNREACHABLE;
2021 break;
2022 default:
2023 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2024 break;
2025 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002026 break;
2027 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08002028 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05302029 event.param.conn.initiator_depth = iw_event->ird;
2030 event.param.conn.responder_resources = iw_event->ord;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002031 break;
2032 default:
2033 BUG_ON(1);
2034 }
2035
Sean Heftya1b1b612006-11-30 16:33:14 -08002036 event.status = iw_event->status;
2037 event.param.conn.private_data = iw_event->private_data;
2038 event.param.conn.private_data_len = iw_event->private_data_len;
2039 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002040 if (ret) {
2041 /* Destroy the CM ID by returning a non-zero value. */
2042 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002043 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002044 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002045 rdma_destroy_id(&id_priv->id);
2046 return ret;
2047 }
2048
Bart Van Assche37e07cd2016-06-10 11:08:25 -07002049out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002050 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002051 return ret;
2052}
2053
2054static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2055 struct iw_cm_event *iw_event)
2056{
2057 struct rdma_cm_id *new_cm_id;
2058 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08002059 struct rdma_cm_event event;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07002060 int ret = -ECONNABORTED;
Steve Wise24d44a32013-07-04 16:10:44 +05302061 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2062 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002063
2064 listen_id = cm_id->context;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07002065
2066 mutex_lock(&listen_id->handler_mutex);
2067 if (listen_id->state != RDMA_CM_LISTEN)
2068 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002069
2070 /* Create a new RDMA id for the new IW CM ID */
Guy Shapirofa201052015-10-22 15:20:10 +03002071 new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2072 listen_id->id.event_handler,
Tom Tucker07ebafb2006-08-03 16:02:42 -05002073 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00002074 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07002075 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05002076 ret = -ENOMEM;
2077 goto out;
2078 }
2079 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002080 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002081 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002082
Matan Barakdd5f03b2013-12-12 18:03:11 +02002083 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002084 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07002085 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002086 rdma_destroy_id(new_cm_id);
2087 goto out;
2088 }
2089
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002090 ret = cma_acquire_dev(conn_id, listen_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002091 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07002092 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002093 rdma_destroy_id(new_cm_id);
2094 goto out;
2095 }
2096
2097 conn_id->cm_id.iw = cm_id;
2098 cm_id->context = conn_id;
2099 cm_id->cm_handler = cma_iw_handler;
2100
Steve Wise24d44a32013-07-04 16:10:44 +05302101 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2102 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002103
Sean Heftya1b1b612006-11-30 16:33:14 -08002104 memset(&event, 0, sizeof event);
2105 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2106 event.param.conn.private_data = iw_event->private_data;
2107 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05302108 event.param.conn.initiator_depth = iw_event->ird;
2109 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08002110
2111 /*
2112 * Protect against the user destroying conn_id from another thread
2113 * until we're done accessing it.
2114 */
2115 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08002116 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002117 if (ret) {
2118 /* User wants to destroy the CM ID */
2119 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002120 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002121 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08002122 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002123 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002124 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002125 }
2126
Or Gerlitzde910bd2008-07-14 23:48:53 -07002127 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08002128 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002129
Tom Tucker07ebafb2006-08-03 16:02:42 -05002130out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002131 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002132 return ret;
2133}
2134
Sean Heftye51060f2006-06-17 20:37:29 -07002135static int cma_ib_listen(struct rdma_id_private *id_priv)
2136{
Sean Heftye51060f2006-06-17 20:37:29 -07002137 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002138 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07002139 __be64 svc_id;
Sean Heftye51060f2006-06-17 20:37:29 -07002140
Sean Heftyf4753832013-05-29 10:09:14 -07002141 addr = cma_src_addr(id_priv);
Sean Heftycf539362013-05-29 10:09:28 -07002142 svc_id = rdma_get_service_id(&id_priv->id, addr);
Haggai Eran51efe392015-07-30 17:50:25 +03002143 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
2144 if (IS_ERR(id))
2145 return PTR_ERR(id);
2146 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07002147
Haggai Eran51efe392015-07-30 17:50:25 +03002148 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07002149}
2150
Tom Tucker07ebafb2006-08-03 16:02:42 -05002151static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2152{
2153 int ret;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002154 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002155
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002156 id = iw_create_cm_id(id_priv->id.device,
2157 iw_conn_req_handler,
2158 id_priv);
2159 if (IS_ERR(id))
2160 return PTR_ERR(id);
2161
Steve Wise68cdba02015-05-18 15:27:10 -05002162 id->tos = id_priv->tos;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002163 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002164
Steve Wise24d44a32013-07-04 16:10:44 +05302165 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2166 rdma_addr_size(cma_src_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002167
2168 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2169
2170 if (ret) {
2171 iw_destroy_cm_id(id_priv->cm_id.iw);
2172 id_priv->cm_id.iw = NULL;
2173 }
2174
2175 return ret;
2176}
2177
Sean Heftye51060f2006-06-17 20:37:29 -07002178static int cma_listen_handler(struct rdma_cm_id *id,
2179 struct rdma_cm_event *event)
2180{
2181 struct rdma_id_private *id_priv = id->context;
2182
2183 id->context = id_priv->id.context;
2184 id->event_handler = id_priv->id.event_handler;
2185 return id_priv->id.event_handler(id, event);
2186}
2187
2188static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2189 struct cma_device *cma_dev)
2190{
2191 struct rdma_id_private *dev_id_priv;
2192 struct rdma_cm_id *id;
Guy Shapirofa201052015-10-22 15:20:10 +03002193 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07002194 int ret;
2195
Michael Wang72219cea2015-05-05 14:50:34 +02002196 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
Sean Hefty94d0c932013-05-29 10:09:24 -07002197 return;
2198
Guy Shapirofa201052015-10-22 15:20:10 +03002199 id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
Sean Heftyb26f9b92010-04-01 17:08:41 +00002200 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07002201 if (IS_ERR(id))
2202 return;
2203
2204 dev_id_priv = container_of(id, struct rdma_id_private, id);
2205
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002206 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftyf4753832013-05-29 10:09:14 -07002207 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2208 rdma_addr_size(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002209
Matan Barak045959d2015-12-23 14:56:55 +02002210 _cma_attach_to_dev(dev_id_priv, cma_dev);
Sean Heftye51060f2006-06-17 20:37:29 -07002211 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07002212 atomic_inc(&id_priv->refcount);
2213 dev_id_priv->internal_id = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002214 dev_id_priv->afonly = id_priv->afonly;
Sean Heftye51060f2006-06-17 20:37:29 -07002215
2216 ret = rdma_listen(id, id_priv->backlog);
2217 if (ret)
Parav Panditaba25a3e2016-03-02 00:50:29 +05302218 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2219 ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07002220}
2221
2222static void cma_listen_on_all(struct rdma_id_private *id_priv)
2223{
2224 struct cma_device *cma_dev;
2225
2226 mutex_lock(&lock);
2227 list_add_tail(&id_priv->list, &listen_any_list);
2228 list_for_each_entry(cma_dev, &dev_list, list)
2229 cma_listen_on_dev(id_priv, cma_dev);
2230 mutex_unlock(&lock);
2231}
2232
Sean Heftya81c9942007-08-08 15:51:06 -07002233void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2234{
2235 struct rdma_id_private *id_priv;
2236
2237 id_priv = container_of(id, struct rdma_id_private, id);
2238 id_priv->tos = (u8) tos;
2239}
2240EXPORT_SYMBOL(rdma_set_service_type);
2241
Sean Heftye51060f2006-06-17 20:37:29 -07002242static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2243 void *context)
2244{
2245 struct cma_work *work = context;
2246 struct rdma_route *route;
2247
2248 route = &work->id->id.route;
2249
2250 if (!status) {
2251 route->num_paths = 1;
2252 *route->path_rec = *path_rec;
2253 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002254 work->old_state = RDMA_CM_ROUTE_QUERY;
2255 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002256 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07002257 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07002258 }
2259
2260 queue_work(cma_wq, &work->work);
2261}
2262
2263static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2264 struct cma_work *work)
2265{
Sean Heftyf4753832013-05-29 10:09:14 -07002266 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002267 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07002268 ib_sa_comp_mask comp_mask;
2269 struct sockaddr_in6 *sin6;
Sean Heftyf68194c2013-05-29 10:09:18 -07002270 struct sockaddr_ib *sib;
Sean Heftye51060f2006-06-17 20:37:29 -07002271
2272 memset(&path_rec, 0, sizeof path_rec);
Sean Heftyf4753832013-05-29 10:09:14 -07002273 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2274 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2275 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07002276 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08002277 path_rec.reversible = 1;
Sean Heftycf539362013-05-29 10:09:28 -07002278 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftya81c9942007-08-08 15:51:06 -07002279
2280 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2281 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2282 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2283
Sean Heftyf68194c2013-05-29 10:09:18 -07002284 switch (cma_family(id_priv)) {
2285 case AF_INET:
Sean Heftya81c9942007-08-08 15:51:06 -07002286 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2287 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002288 break;
2289 case AF_INET6:
Sean Heftyf4753832013-05-29 10:09:14 -07002290 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
Sean Heftya81c9942007-08-08 15:51:06 -07002291 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2292 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002293 break;
2294 case AF_IB:
2295 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2296 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2297 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2298 break;
Sean Heftya81c9942007-08-08 15:51:06 -07002299 }
Sean Heftye51060f2006-06-17 20:37:29 -07002300
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002301 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07002302 id_priv->id.port_num, &path_rec,
2303 comp_mask, timeout_ms,
2304 GFP_KERNEL, cma_query_handler,
2305 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07002306
2307 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2308}
2309
David Howellsc4028952006-11-22 14:57:56 +00002310static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07002311{
David Howellsc4028952006-11-22 14:57:56 +00002312 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07002313 struct rdma_id_private *id_priv = work->id;
2314 int destroy = 0;
2315
Or Gerlitzde910bd2008-07-14 23:48:53 -07002316 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002317 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2318 goto out;
2319
2320 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002321 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07002322 destroy = 1;
2323 }
2324out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002325 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002326 cma_deref_id(id_priv);
2327 if (destroy)
2328 rdma_destroy_id(&id_priv->id);
2329 kfree(work);
2330}
2331
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002332static void cma_ndev_work_handler(struct work_struct *_work)
2333{
2334 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2335 struct rdma_id_private *id_priv = work->id;
2336 int destroy = 0;
2337
2338 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002339 if (id_priv->state == RDMA_CM_DESTROYING ||
2340 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002341 goto out;
2342
2343 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002344 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002345 destroy = 1;
2346 }
2347
2348out:
2349 mutex_unlock(&id_priv->handler_mutex);
2350 cma_deref_id(id_priv);
2351 if (destroy)
2352 rdma_destroy_id(&id_priv->id);
2353 kfree(work);
2354}
2355
Sean Heftye51060f2006-06-17 20:37:29 -07002356static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2357{
2358 struct rdma_route *route = &id_priv->id.route;
2359 struct cma_work *work;
2360 int ret;
2361
2362 work = kzalloc(sizeof *work, GFP_KERNEL);
2363 if (!work)
2364 return -ENOMEM;
2365
2366 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002367 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002368 work->old_state = RDMA_CM_ROUTE_QUERY;
2369 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002370 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2371
2372 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2373 if (!route->path_rec) {
2374 ret = -ENOMEM;
2375 goto err1;
2376 }
2377
2378 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2379 if (ret)
2380 goto err2;
2381
2382 return 0;
2383err2:
2384 kfree(route->path_rec);
2385 route->path_rec = NULL;
2386err1:
2387 kfree(work);
2388 return ret;
2389}
2390
2391int rdma_set_ib_paths(struct rdma_cm_id *id,
2392 struct ib_sa_path_rec *path_rec, int num_paths)
2393{
2394 struct rdma_id_private *id_priv;
2395 int ret;
2396
2397 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002398 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2399 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07002400 return -EINVAL;
2401
Julia Lawall9893e742010-05-15 23:22:38 +02002402 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2403 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002404 if (!id->route.path_rec) {
2405 ret = -ENOMEM;
2406 goto err;
2407 }
2408
Sean Heftyae2d9292010-03-25 19:12:36 +00002409 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07002410 return 0;
2411err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002412 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002413 return ret;
2414}
2415EXPORT_SYMBOL(rdma_set_ib_paths);
2416
Tom Tucker07ebafb2006-08-03 16:02:42 -05002417static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2418{
2419 struct cma_work *work;
2420
2421 work = kzalloc(sizeof *work, GFP_KERNEL);
2422 if (!work)
2423 return -ENOMEM;
2424
2425 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002426 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002427 work->old_state = RDMA_CM_ROUTE_QUERY;
2428 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002429 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2430 queue_work(cma_wq, &work->work);
2431 return 0;
2432}
2433
Eyal Perryeb072c42013-11-06 15:37:24 +02002434static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2435{
2436 int prio;
2437 struct net_device *dev;
2438
2439 prio = rt_tos2priority(tos);
2440 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
2441 vlan_dev_real_dev(ndev) : ndev;
2442
2443 if (dev->num_tc)
2444 return netdev_get_prio_tc_map(dev, prio);
2445
2446#if IS_ENABLED(CONFIG_VLAN_8021Q)
2447 if (ndev->priv_flags & IFF_802_1Q_VLAN)
2448 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2449 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2450#endif
2451 return 0;
2452}
2453
Leon Romanovskyaeb76df2016-10-31 07:50:56 +02002454static enum ib_gid_type cma_route_gid_type(enum rdma_network_type network_type,
2455 unsigned long supported_gids,
2456 enum ib_gid_type default_gid)
2457{
2458 if ((network_type == RDMA_NETWORK_IPV4 ||
2459 network_type == RDMA_NETWORK_IPV6) &&
2460 test_bit(IB_GID_TYPE_ROCE_UDP_ENCAP, &supported_gids))
2461 return IB_GID_TYPE_ROCE_UDP_ENCAP;
2462
2463 return default_gid;
2464}
2465
Eli Cohen3c86aa72010-10-13 21:26:51 +02002466static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2467{
2468 struct rdma_route *route = &id_priv->id.route;
2469 struct rdma_addr *addr = &route->addr;
2470 struct cma_work *work;
2471 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002472 struct net_device *ndev = NULL;
Matan Barakdd5f03b2013-12-12 18:03:11 +02002473
Eli Cohen3c86aa72010-10-13 21:26:51 +02002474
Eli Cohen3c86aa72010-10-13 21:26:51 +02002475 work = kzalloc(sizeof *work, GFP_KERNEL);
2476 if (!work)
2477 return -ENOMEM;
2478
2479 work->id = id_priv;
2480 INIT_WORK(&work->work, cma_work_handler);
2481
2482 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2483 if (!route->path_rec) {
2484 ret = -ENOMEM;
2485 goto err1;
2486 }
2487
2488 route->num_paths = 1;
2489
Matan Barakabae1b72015-10-15 18:38:49 +03002490 if (addr->dev_addr.bound_dev_if) {
Leon Romanovskyaeb76df2016-10-31 07:50:56 +02002491 unsigned long supported_gids;
2492
Eli Cohen3c86aa72010-10-13 21:26:51 +02002493 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
Wei Yongjun23d70502016-08-05 13:46:49 +00002494 if (!ndev) {
2495 ret = -ENODEV;
2496 goto err2;
2497 }
Matan Barak20029832015-12-23 14:56:53 +02002498
2499 if (ndev->flags & IFF_LOOPBACK) {
2500 dev_put(ndev);
Wei Yongjun23d70502016-08-05 13:46:49 +00002501 if (!id_priv->id.device->get_netdev) {
2502 ret = -EOPNOTSUPP;
2503 goto err2;
2504 }
Matan Barak20029832015-12-23 14:56:53 +02002505
2506 ndev = id_priv->id.device->get_netdev(id_priv->id.device,
2507 id_priv->id.port_num);
Wei Yongjun23d70502016-08-05 13:46:49 +00002508 if (!ndev) {
2509 ret = -ENODEV;
2510 goto err2;
2511 }
Matan Barak20029832015-12-23 14:56:53 +02002512 }
2513
Matan Barakabae1b72015-10-15 18:38:49 +03002514 route->path_rec->net = &init_net;
Matan Barak20029832015-12-23 14:56:53 +02002515 route->path_rec->ifindex = ndev->ifindex;
Leon Romanovskyaeb76df2016-10-31 07:50:56 +02002516 supported_gids = roce_gid_type_mask_support(id_priv->id.device,
2517 id_priv->id.port_num);
2518 route->path_rec->gid_type =
2519 cma_route_gid_type(addr->dev_addr.network,
2520 supported_gids,
2521 id_priv->gid_type);
Matan Barakabae1b72015-10-15 18:38:49 +03002522 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02002523 if (!ndev) {
2524 ret = -ENODEV;
2525 goto err2;
2526 }
2527
Matan Barakdd5f03b2013-12-12 18:03:11 +02002528 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002529
Moni Shoua7b856272013-12-12 18:03:12 +02002530 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2531 &route->path_rec->sgid);
2532 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2533 &route->path_rec->dgid);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002534
Somnath Koturc865f242015-12-23 14:56:51 +02002535 /* Use the hint from IP Stack to select GID Type */
Matan Barakc3efe752016-01-04 10:49:54 +02002536 if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2537 route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2538 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
Somnath Koturc865f242015-12-23 14:56:51 +02002539 /* TODO: get the hoplimit from the inet/inet6 device */
Matan Barakc3efe752016-01-04 10:49:54 +02002540 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2541 else
Somnath Koturc865f242015-12-23 14:56:51 +02002542 route->path_rec->hop_limit = 1;
Eli Cohenaf7bd462010-08-26 17:18:59 +03002543 route->path_rec->reversible = 1;
2544 route->path_rec->pkey = cpu_to_be16(0xffff);
2545 route->path_rec->mtu_selector = IB_SA_EQ;
Eyal Perryeb072c42013-11-06 15:37:24 +02002546 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
Eli Cohen3c86aa72010-10-13 21:26:51 +02002547 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2548 route->path_rec->rate_selector = IB_SA_EQ;
2549 route->path_rec->rate = iboe_get_rate(ndev);
2550 dev_put(ndev);
2551 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2552 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2553 if (!route->path_rec->mtu) {
2554 ret = -EINVAL;
2555 goto err2;
2556 }
2557
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002558 work->old_state = RDMA_CM_ROUTE_QUERY;
2559 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002560 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2561 work->event.status = 0;
2562
2563 queue_work(cma_wq, &work->work);
2564
2565 return 0;
2566
2567err2:
2568 kfree(route->path_rec);
2569 route->path_rec = NULL;
2570err1:
2571 kfree(work);
2572 return ret;
2573}
2574
Sean Heftye51060f2006-06-17 20:37:29 -07002575int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2576{
2577 struct rdma_id_private *id_priv;
2578 int ret;
2579
2580 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002581 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002582 return -EINVAL;
2583
2584 atomic_inc(&id_priv->refcount);
Michael Wangfe53ba22015-05-05 14:50:36 +02002585 if (rdma_cap_ib_sa(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002586 ret = cma_resolve_ib_route(id_priv, timeout_ms);
Ira Weiny5d9fb042015-05-14 15:01:46 -04002587 else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002588 ret = cma_resolve_iboe_route(id_priv);
2589 else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002590 ret = cma_resolve_iw_route(id_priv, timeout_ms);
Michael Wangc72f2182015-05-05 14:50:28 +02002591 else
Sean Heftye51060f2006-06-17 20:37:29 -07002592 ret = -ENOSYS;
Michael Wangc72f2182015-05-05 14:50:28 +02002593
Sean Heftye51060f2006-06-17 20:37:29 -07002594 if (ret)
2595 goto err;
2596
2597 return 0;
2598err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002599 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002600 cma_deref_id(id_priv);
2601 return ret;
2602}
2603EXPORT_SYMBOL(rdma_resolve_route);
2604
Sean Hefty6a3e3622013-05-29 10:09:13 -07002605static void cma_set_loopback(struct sockaddr *addr)
2606{
2607 switch (addr->sa_family) {
2608 case AF_INET:
2609 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2610 break;
2611 case AF_INET6:
2612 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2613 0, 0, 0, htonl(1));
2614 break;
2615 default:
2616 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2617 0, 0, 0, htonl(1));
2618 break;
2619 }
2620}
2621
Sean Heftye51060f2006-06-17 20:37:29 -07002622static int cma_bind_loopback(struct rdma_id_private *id_priv)
2623{
Sean Heftyb0569e42013-05-29 10:09:15 -07002624 struct cma_device *cma_dev, *cur_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07002625 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002626 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002627 u16 pkey;
2628 int ret;
2629 u8 p;
2630
Sean Heftyb0569e42013-05-29 10:09:15 -07002631 cma_dev = NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07002632 mutex_lock(&lock);
Sean Heftyb0569e42013-05-29 10:09:15 -07002633 list_for_each_entry(cur_dev, &dev_list, list) {
2634 if (cma_family(id_priv) == AF_IB &&
Michael Wang72219cea2015-05-05 14:50:34 +02002635 !rdma_cap_ib_cm(cur_dev->device, 1))
Sean Heftyb0569e42013-05-29 10:09:15 -07002636 continue;
2637
2638 if (!cma_dev)
2639 cma_dev = cur_dev;
2640
2641 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2642 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2643 port_attr.state == IB_PORT_ACTIVE) {
2644 cma_dev = cur_dev;
2645 goto port_found;
2646 }
2647 }
2648 }
2649
2650 if (!cma_dev) {
Sean Heftye51060f2006-06-17 20:37:29 -07002651 ret = -ENODEV;
2652 goto out;
2653 }
Krishna Kumare82153b2006-10-16 10:09:01 +05302654
2655 p = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07002656
2657port_found:
Matan Barak55ee3ab2015-10-15 18:38:45 +03002658 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002659 if (ret)
2660 goto out;
2661
2662 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2663 if (ret)
2664 goto out;
2665
Sean Hefty6f8372b2009-11-19 13:26:06 -08002666 id_priv->id.route.addr.dev_addr.dev_type =
Michael Wang21655af2015-05-05 14:50:27 +02002667 (rdma_protocol_ib(cma_dev->device, p)) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08002668 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2669
2670 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002671 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2672 id_priv->id.port_num = p;
2673 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf4753832013-05-29 10:09:14 -07002674 cma_set_loopback(cma_src_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002675out:
2676 mutex_unlock(&lock);
2677 return ret;
2678}
2679
2680static void addr_handler(int status, struct sockaddr *src_addr,
2681 struct rdma_dev_addr *dev_addr, void *context)
2682{
2683 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08002684 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002685
Sean Heftya1b1b612006-11-30 16:33:14 -08002686 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002687 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002688 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2689 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07002690 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07002691
Moni Shoua7b856272013-12-12 18:03:12 +02002692 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
Sean Hefty61a73c72006-09-01 15:33:55 -07002693 if (!status && !id_priv->cma_dev)
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002694 status = cma_acquire_dev(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002695
2696 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002697 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2698 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002699 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08002700 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2701 event.status = status;
Moni Shoua7b856272013-12-12 18:03:12 +02002702 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08002703 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002704
Sean Heftya1b1b612006-11-30 16:33:14 -08002705 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002706 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002707 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002708 cma_deref_id(id_priv);
2709 rdma_destroy_id(&id_priv->id);
2710 return;
2711 }
2712out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002713 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002714 cma_deref_id(id_priv);
2715}
2716
2717static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2718{
2719 struct cma_work *work;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002720 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002721 int ret;
2722
2723 work = kzalloc(sizeof *work, GFP_KERNEL);
2724 if (!work)
2725 return -ENOMEM;
2726
2727 if (!id_priv->cma_dev) {
2728 ret = cma_bind_loopback(id_priv);
2729 if (ret)
2730 goto err;
2731 }
2732
Sean Hefty6f8372b2009-11-19 13:26:06 -08002733 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2734 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002735
Sean Heftye51060f2006-06-17 20:37:29 -07002736 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002737 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002738 work->old_state = RDMA_CM_ADDR_QUERY;
2739 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002740 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2741 queue_work(cma_wq, &work->work);
2742 return 0;
2743err:
2744 kfree(work);
2745 return ret;
2746}
2747
Sean Heftyf17df3b2013-05-29 10:09:17 -07002748static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2749{
2750 struct cma_work *work;
2751 int ret;
2752
2753 work = kzalloc(sizeof *work, GFP_KERNEL);
2754 if (!work)
2755 return -ENOMEM;
2756
2757 if (!id_priv->cma_dev) {
2758 ret = cma_resolve_ib_dev(id_priv);
2759 if (ret)
2760 goto err;
2761 }
2762
2763 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2764 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2765
2766 work->id = id_priv;
2767 INIT_WORK(&work->work, cma_work_handler);
2768 work->old_state = RDMA_CM_ADDR_QUERY;
2769 work->new_state = RDMA_CM_ADDR_RESOLVED;
2770 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2771 queue_work(cma_wq, &work->work);
2772 return 0;
2773err:
2774 kfree(work);
2775 return ret;
2776}
2777
Sean Heftye51060f2006-06-17 20:37:29 -07002778static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2779 struct sockaddr *dst_addr)
2780{
Sean Heftyd14714d2009-11-19 16:46:25 -08002781 if (!src_addr || !src_addr->sa_family) {
2782 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002783 src_addr->sa_family = dst_addr->sa_family;
Jack Morgenstein97a2e392017-01-15 20:15:00 +02002784 if (IS_ENABLED(CONFIG_IPV6) &&
2785 dst_addr->sa_family == AF_INET6) {
Spencer Baugh6c26a772015-08-13 12:19:10 -07002786 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2787 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2788 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2789 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2790 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002791 } else if (dst_addr->sa_family == AF_IB) {
2792 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2793 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
Sean Heftyd14714d2009-11-19 16:46:25 -08002794 }
2795 }
2796 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002797}
2798
2799int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2800 struct sockaddr *dst_addr, int timeout_ms)
2801{
2802 struct rdma_id_private *id_priv;
2803 int ret;
2804
2805 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002806 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002807 ret = cma_bind_addr(id, src_addr, dst_addr);
2808 if (ret)
2809 return ret;
2810 }
2811
Sean Hefty4ae71522013-05-29 10:09:16 -07002812 if (cma_family(id_priv) != dst_addr->sa_family)
2813 return -EINVAL;
2814
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002815 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002816 return -EINVAL;
2817
2818 atomic_inc(&id_priv->refcount);
Sean Heftyf4753832013-05-29 10:09:14 -07002819 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
Sean Heftyf17df3b2013-05-29 10:09:17 -07002820 if (cma_any_addr(dst_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002821 ret = cma_resolve_loopback(id_priv);
Sean Heftyf17df3b2013-05-29 10:09:17 -07002822 } else {
2823 if (dst_addr->sa_family == AF_IB) {
2824 ret = cma_resolve_ib_addr(id_priv);
2825 } else {
2826 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2827 dst_addr, &id->route.addr.dev_addr,
2828 timeout_ms, addr_handler, id_priv);
2829 }
2830 }
Sean Heftye51060f2006-06-17 20:37:29 -07002831 if (ret)
2832 goto err;
2833
2834 return 0;
2835err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002836 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002837 cma_deref_id(id_priv);
2838 return ret;
2839}
2840EXPORT_SYMBOL(rdma_resolve_addr);
2841
Hefty, Seana9bb7912011-05-09 22:06:10 -07002842int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2843{
2844 struct rdma_id_private *id_priv;
2845 unsigned long flags;
2846 int ret;
2847
2848 id_priv = container_of(id, struct rdma_id_private, id);
2849 spin_lock_irqsave(&id_priv->lock, flags);
Sean Heftyc8dea2f92013-05-29 10:09:08 -07002850 if (reuse || id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002851 id_priv->reuseaddr = reuse;
2852 ret = 0;
2853 } else {
2854 ret = -EINVAL;
2855 }
2856 spin_unlock_irqrestore(&id_priv->lock, flags);
2857 return ret;
2858}
2859EXPORT_SYMBOL(rdma_set_reuseaddr);
2860
Sean Hefty68602122012-06-14 20:31:39 +00002861int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2862{
2863 struct rdma_id_private *id_priv;
2864 unsigned long flags;
2865 int ret;
2866
2867 id_priv = container_of(id, struct rdma_id_private, id);
2868 spin_lock_irqsave(&id_priv->lock, flags);
2869 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2870 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2871 id_priv->afonly = afonly;
2872 ret = 0;
2873 } else {
2874 ret = -EINVAL;
2875 }
2876 spin_unlock_irqrestore(&id_priv->lock, flags);
2877 return ret;
2878}
2879EXPORT_SYMBOL(rdma_set_afonly);
2880
Sean Heftye51060f2006-06-17 20:37:29 -07002881static void cma_bind_port(struct rdma_bind_list *bind_list,
2882 struct rdma_id_private *id_priv)
2883{
Sean Hefty58afdcb2013-05-29 10:09:11 -07002884 struct sockaddr *addr;
2885 struct sockaddr_ib *sib;
2886 u64 sid, mask;
2887 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07002888
Sean Heftyf4753832013-05-29 10:09:14 -07002889 addr = cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002890 port = htons(bind_list->port);
2891
2892 switch (addr->sa_family) {
2893 case AF_INET:
2894 ((struct sockaddr_in *) addr)->sin_port = port;
2895 break;
2896 case AF_INET6:
2897 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2898 break;
2899 case AF_IB:
2900 sib = (struct sockaddr_ib *) addr;
2901 sid = be64_to_cpu(sib->sib_sid);
2902 mask = be64_to_cpu(sib->sib_sid_mask);
2903 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2904 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2905 break;
2906 }
Sean Heftye51060f2006-06-17 20:37:29 -07002907 id_priv->bind_list = bind_list;
2908 hlist_add_head(&id_priv->node, &bind_list->owners);
2909}
2910
Haggai Eranaac978e2015-07-30 17:50:20 +03002911static int cma_alloc_port(enum rdma_port_space ps,
2912 struct rdma_id_private *id_priv, unsigned short snum)
Sean Heftye51060f2006-06-17 20:37:29 -07002913{
2914 struct rdma_bind_list *bind_list;
Tejun Heo3b069c52013-02-27 17:04:16 -08002915 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002916
Sean Heftycb164b82007-03-05 12:50:17 -08002917 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002918 if (!bind_list)
2919 return -ENOMEM;
2920
Guy Shapirofa201052015-10-22 15:20:10 +03002921 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2922 snum);
Tejun Heo3b069c52013-02-27 17:04:16 -08002923 if (ret < 0)
2924 goto err;
Sean Heftye51060f2006-06-17 20:37:29 -07002925
2926 bind_list->ps = ps;
Tejun Heo3b069c52013-02-27 17:04:16 -08002927 bind_list->port = (unsigned short)ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002928 cma_bind_port(bind_list, id_priv);
2929 return 0;
Tejun Heo3b069c52013-02-27 17:04:16 -08002930err:
Sean Heftyaedec082007-01-29 16:41:23 -08002931 kfree(bind_list);
Tejun Heo3b069c52013-02-27 17:04:16 -08002932 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002933}
2934
Haggai Eranaac978e2015-07-30 17:50:20 +03002935static int cma_alloc_any_port(enum rdma_port_space ps,
2936 struct rdma_id_private *id_priv)
Sean Heftyaedec082007-01-29 16:41:23 -08002937{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002938 static unsigned int last_used_port;
2939 int low, high, remaining;
2940 unsigned int rover;
Guy Shapirofa201052015-10-22 15:20:10 +03002941 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftyaedec082007-01-29 16:41:23 -08002942
Guy Shapirofa201052015-10-22 15:20:10 +03002943 inet_get_local_port_range(net, &low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002944 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002945 rover = prandom_u32() % remaining + low;
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002946retry:
2947 if (last_used_port != rover &&
Guy Shapirofa201052015-10-22 15:20:10 +03002948 !cma_ps_find(net, ps, (unsigned short)rover)) {
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002949 int ret = cma_alloc_port(ps, id_priv, rover);
2950 /*
2951 * Remember previously used port number in order to avoid
2952 * re-using same port immediately after it is closed.
2953 */
2954 if (!ret)
2955 last_used_port = rover;
2956 if (ret != -EADDRNOTAVAIL)
2957 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002958 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002959 if (--remaining) {
2960 rover++;
2961 if ((rover < low) || (rover > high))
2962 rover = low;
2963 goto retry;
2964 }
2965 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002966}
2967
Hefty, Seana9bb7912011-05-09 22:06:10 -07002968/*
2969 * Check that the requested port is available. This is called when trying to
2970 * bind to a specific port, or when trying to listen on a bound port. In
2971 * the latter case, the provided id_priv may already be on the bind_list, but
2972 * we still need to check that it's okay to start listening.
2973 */
2974static int cma_check_port(struct rdma_bind_list *bind_list,
2975 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002976{
2977 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002978 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002979
Sean Heftyf4753832013-05-29 10:09:14 -07002980 addr = cma_src_addr(id_priv);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002981 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002982 if (id_priv == cur_id)
2983 continue;
2984
Sean Hefty5b0ec992012-06-14 20:31:39 +00002985 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2986 cur_id->reuseaddr)
2987 continue;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002988
Sean Heftyf4753832013-05-29 10:09:14 -07002989 cur_addr = cma_src_addr(cur_id);
Sean Hefty5b0ec992012-06-14 20:31:39 +00002990 if (id_priv->afonly && cur_id->afonly &&
2991 (addr->sa_family != cur_addr->sa_family))
2992 continue;
2993
2994 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2995 return -EADDRNOTAVAIL;
2996
2997 if (!cma_addr_cmp(addr, cur_addr))
2998 return -EADDRINUSE;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002999 }
3000 return 0;
3001}
3002
Haggai Eranaac978e2015-07-30 17:50:20 +03003003static int cma_use_port(enum rdma_port_space ps,
3004 struct rdma_id_private *id_priv)
Hefty, Seana9bb7912011-05-09 22:06:10 -07003005{
3006 struct rdma_bind_list *bind_list;
3007 unsigned short snum;
3008 int ret;
3009
Sean Heftyf4753832013-05-29 10:09:14 -07003010 snum = ntohs(cma_port(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07003011 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
3012 return -EACCES;
3013
Guy Shapirofa201052015-10-22 15:20:10 +03003014 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07003015 if (!bind_list) {
3016 ret = cma_alloc_port(ps, id_priv, snum);
3017 } else {
3018 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
3019 if (!ret)
3020 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003021 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07003022 return ret;
3023}
Sean Heftye51060f2006-06-17 20:37:29 -07003024
Hefty, Seana9bb7912011-05-09 22:06:10 -07003025static int cma_bind_listen(struct rdma_id_private *id_priv)
3026{
3027 struct rdma_bind_list *bind_list = id_priv->bind_list;
3028 int ret = 0;
3029
3030 mutex_lock(&lock);
3031 if (bind_list->owners.first->next)
3032 ret = cma_check_port(bind_list, id_priv, 0);
3033 mutex_unlock(&lock);
3034 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003035}
3036
Haggai Eranaac978e2015-07-30 17:50:20 +03003037static enum rdma_port_space cma_select_inet_ps(
3038 struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003039{
3040 switch (id_priv->id.ps) {
Sean Hefty58afdcb2013-05-29 10:09:11 -07003041 case RDMA_PS_TCP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07003042 case RDMA_PS_UDP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07003043 case RDMA_PS_IPOIB:
Sean Hefty58afdcb2013-05-29 10:09:11 -07003044 case RDMA_PS_IB:
Haggai Eranaac978e2015-07-30 17:50:20 +03003045 return id_priv->id.ps;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003046 default:
Haggai Eranaac978e2015-07-30 17:50:20 +03003047
3048 return 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003049 }
3050}
3051
Haggai Eranaac978e2015-07-30 17:50:20 +03003052static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003053{
Haggai Eranaac978e2015-07-30 17:50:20 +03003054 enum rdma_port_space ps = 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003055 struct sockaddr_ib *sib;
3056 u64 sid_ps, mask, sid;
3057
Sean Heftyf4753832013-05-29 10:09:14 -07003058 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07003059 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3060 sid = be64_to_cpu(sib->sib_sid) & mask;
3061
3062 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3063 sid_ps = RDMA_IB_IP_PS_IB;
Haggai Eranaac978e2015-07-30 17:50:20 +03003064 ps = RDMA_PS_IB;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003065 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3066 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3067 sid_ps = RDMA_IB_IP_PS_TCP;
Haggai Eranaac978e2015-07-30 17:50:20 +03003068 ps = RDMA_PS_TCP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003069 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3070 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3071 sid_ps = RDMA_IB_IP_PS_UDP;
Haggai Eranaac978e2015-07-30 17:50:20 +03003072 ps = RDMA_PS_UDP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003073 }
3074
3075 if (ps) {
3076 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3077 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3078 be64_to_cpu(sib->sib_sid_mask));
3079 }
3080 return ps;
3081}
3082
Sean Heftye51060f2006-06-17 20:37:29 -07003083static int cma_get_port(struct rdma_id_private *id_priv)
3084{
Haggai Eranaac978e2015-07-30 17:50:20 +03003085 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -07003086 int ret;
3087
Sean Heftyf4753832013-05-29 10:09:14 -07003088 if (cma_family(id_priv) != AF_IB)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003089 ps = cma_select_inet_ps(id_priv);
3090 else
3091 ps = cma_select_ib_ps(id_priv);
3092 if (!ps)
Sean Heftye51060f2006-06-17 20:37:29 -07003093 return -EPROTONOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07003094
3095 mutex_lock(&lock);
Sean Heftyf4753832013-05-29 10:09:14 -07003096 if (cma_any_port(cma_src_addr(id_priv)))
Sean Heftyaedec082007-01-29 16:41:23 -08003097 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003098 else
3099 ret = cma_use_port(ps, id_priv);
3100 mutex_unlock(&lock);
3101
3102 return ret;
3103}
3104
Sean Heftyd14714d2009-11-19 16:46:25 -08003105static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3106 struct sockaddr *addr)
3107{
Roland Dreierd90f9b32012-07-05 22:39:34 -07003108#if IS_ENABLED(CONFIG_IPV6)
Sean Heftyd14714d2009-11-19 16:46:25 -08003109 struct sockaddr_in6 *sin6;
3110
3111 if (addr->sa_family != AF_INET6)
3112 return 0;
3113
3114 sin6 = (struct sockaddr_in6 *) addr;
Somnath Kotur5462edd2013-11-16 09:30:01 +05303115
3116 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3117 return 0;
3118
3119 if (!sin6->sin6_scope_id)
Sean Heftyd14714d2009-11-19 16:46:25 -08003120 return -EINVAL;
3121
3122 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3123#endif
3124 return 0;
3125}
3126
Hefty, Seana9bb7912011-05-09 22:06:10 -07003127int rdma_listen(struct rdma_cm_id *id, int backlog)
3128{
3129 struct rdma_id_private *id_priv;
3130 int ret;
3131
3132 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003133 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftyf4753832013-05-29 10:09:14 -07003134 id->route.addr.src_addr.ss_family = AF_INET;
3135 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
Hefty, Seana9bb7912011-05-09 22:06:10 -07003136 if (ret)
3137 return ret;
3138 }
3139
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003140 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07003141 return -EINVAL;
3142
3143 if (id_priv->reuseaddr) {
3144 ret = cma_bind_listen(id_priv);
3145 if (ret)
3146 goto err;
3147 }
3148
3149 id_priv->backlog = backlog;
3150 if (id->device) {
Michael Wang72219cea2015-05-05 14:50:34 +02003151 if (rdma_cap_ib_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003152 ret = cma_ib_listen(id_priv);
3153 if (ret)
3154 goto err;
Michael Wang04215332015-05-05 14:50:35 +02003155 } else if (rdma_cap_iw_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003156 ret = cma_iw_listen(id_priv, backlog);
3157 if (ret)
3158 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02003159 } else {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003160 ret = -ENOSYS;
3161 goto err;
3162 }
3163 } else
3164 cma_listen_on_all(id_priv);
3165
3166 return 0;
3167err:
3168 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003169 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07003170 return ret;
3171}
3172EXPORT_SYMBOL(rdma_listen);
3173
Sean Heftye51060f2006-06-17 20:37:29 -07003174int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3175{
3176 struct rdma_id_private *id_priv;
3177 int ret;
3178
Sean Hefty680f9202013-05-29 10:09:12 -07003179 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3180 addr->sa_family != AF_IB)
Sean Heftye51060f2006-06-17 20:37:29 -07003181 return -EAFNOSUPPORT;
3182
3183 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003184 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07003185 return -EINVAL;
3186
Sean Heftyd14714d2009-11-19 16:46:25 -08003187 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3188 if (ret)
3189 goto err1;
3190
Moni Shoua7b856272013-12-12 18:03:12 +02003191 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
Sean Hefty8523c042010-02-08 16:41:15 -08003192 if (!cma_any_addr(addr)) {
Sean Hefty680f9202013-05-29 10:09:12 -07003193 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07003194 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07003195 goto err1;
3196
Doug Ledfordbe9130c2013-09-24 17:16:28 -04003197 ret = cma_acquire_dev(id_priv, NULL);
Krishna Kumar255d0c12006-10-24 13:22:28 -07003198 if (ret)
3199 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07003200 }
3201
Sean Hefty68602122012-06-14 20:31:39 +00003202 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3203 if (addr->sa_family == AF_INET)
3204 id_priv->afonly = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00003205#if IS_ENABLED(CONFIG_IPV6)
Guy Shapirofa201052015-10-22 15:20:10 +03003206 else if (addr->sa_family == AF_INET6) {
3207 struct net *net = id_priv->id.route.addr.dev_addr.net;
3208
3209 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3210 }
Sean Hefty5b0ec992012-06-14 20:31:39 +00003211#endif
Sean Hefty68602122012-06-14 20:31:39 +00003212 }
Sean Heftye51060f2006-06-17 20:37:29 -07003213 ret = cma_get_port(id_priv);
3214 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07003215 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07003216
3217 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07003218err2:
Sean Heftya396d432011-02-23 09:05:39 -08003219 if (id_priv->cma_dev)
3220 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07003221err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003222 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07003223 return ret;
3224}
3225EXPORT_SYMBOL(rdma_bind_addr);
3226
Sean Heftyf4753832013-05-29 10:09:14 -07003227static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07003228{
Sean Heftye51060f2006-06-17 20:37:29 -07003229 struct cma_hdr *cma_hdr;
Sean Heftye51060f2006-06-17 20:37:29 -07003230
Sean Hefty01602f12013-05-29 10:09:20 -07003231 cma_hdr = hdr;
3232 cma_hdr->cma_version = CMA_VERSION;
Sean Heftyf4753832013-05-29 10:09:14 -07003233 if (cma_family(id_priv) == AF_INET) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003234 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07003235
Sean Heftyf4753832013-05-29 10:09:14 -07003236 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3237 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003238
Sean Hefty01602f12013-05-29 10:09:20 -07003239 cma_set_ip_ver(cma_hdr, 4);
3240 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3241 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3242 cma_hdr->port = src4->sin_port;
Sean Heftye8160e12013-05-29 10:09:22 -07003243 } else if (cma_family(id_priv) == AF_INET6) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003244 struct sockaddr_in6 *src6, *dst6;
3245
Sean Heftyf4753832013-05-29 10:09:14 -07003246 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3247 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003248
Sean Hefty01602f12013-05-29 10:09:20 -07003249 cma_set_ip_ver(cma_hdr, 6);
3250 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3251 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3252 cma_hdr->port = src6->sin6_port;
Sean Heftye51060f2006-06-17 20:37:29 -07003253 }
3254 return 0;
3255}
3256
Sean Hefty628e5f62006-11-30 16:44:16 -08003257static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3258 struct ib_cm_event *ib_event)
3259{
3260 struct rdma_id_private *id_priv = cm_id->context;
3261 struct rdma_cm_event event;
3262 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3263 int ret = 0;
3264
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003265 mutex_lock(&id_priv->handler_mutex);
3266 if (id_priv->state != RDMA_CM_CONNECT)
3267 goto out;
Sean Hefty628e5f62006-11-30 16:44:16 -08003268
Sean Hefty8aa08602007-05-07 11:49:00 -07003269 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08003270 switch (ib_event->event) {
3271 case IB_CM_SIDR_REQ_ERROR:
3272 event.event = RDMA_CM_EVENT_UNREACHABLE;
3273 event.status = -ETIMEDOUT;
3274 break;
3275 case IB_CM_SIDR_REP_RECEIVED:
3276 event.param.ud.private_data = ib_event->private_data;
3277 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3278 if (rep->status != IB_SIDR_SUCCESS) {
3279 event.event = RDMA_CM_EVENT_UNREACHABLE;
3280 event.status = ib_event->param.sidr_rep_rcvd.status;
3281 break;
3282 }
Sean Hefty5c438132013-05-29 10:09:23 -07003283 ret = cma_set_qkey(id_priv, rep->qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003284 if (ret) {
3285 event.event = RDMA_CM_EVENT_ADDR_ERROR;
Sean Hefty5c438132013-05-29 10:09:23 -07003286 event.status = ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003287 break;
3288 }
3289 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3290 id_priv->id.route.path_rec,
3291 &event.param.ud.ah_attr);
3292 event.param.ud.qp_num = rep->qpn;
3293 event.param.ud.qkey = rep->qkey;
3294 event.event = RDMA_CM_EVENT_ESTABLISHED;
3295 event.status = 0;
3296 break;
3297 default:
Parav Panditaba25a3e2016-03-02 00:50:29 +05303298 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08003299 ib_event->event);
3300 goto out;
3301 }
3302
3303 ret = id_priv->id.event_handler(&id_priv->id, &event);
3304 if (ret) {
3305 /* Destroy the CM ID by returning a non-zero value. */
3306 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003307 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003308 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003309 rdma_destroy_id(&id_priv->id);
3310 return ret;
3311 }
3312out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07003313 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003314 return ret;
3315}
3316
3317static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3318 struct rdma_conn_param *conn_param)
3319{
3320 struct ib_cm_sidr_req_param req;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003321 struct ib_cm_id *id;
Sean Heftye511d1a2013-07-24 15:06:08 -07003322 void *private_data;
Bart Van Assche0c70b352017-10-11 10:48:45 -07003323 u8 offset;
3324 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003325
Sean Heftye511d1a2013-07-24 15:06:08 -07003326 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003327 offset = cma_user_data_offset(id_priv);
3328 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003329 if (req.private_data_len < conn_param->private_data_len)
3330 return -EINVAL;
3331
Sean Heftye8160e12013-05-29 10:09:22 -07003332 if (req.private_data_len) {
Sean Heftye511d1a2013-07-24 15:06:08 -07003333 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3334 if (!private_data)
Sean Heftye8160e12013-05-29 10:09:22 -07003335 return -ENOMEM;
3336 } else {
Sean Heftye511d1a2013-07-24 15:06:08 -07003337 private_data = NULL;
Sean Heftye8160e12013-05-29 10:09:22 -07003338 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003339
3340 if (conn_param->private_data && conn_param->private_data_len)
Sean Heftye511d1a2013-07-24 15:06:08 -07003341 memcpy(private_data + offset, conn_param->private_data,
3342 conn_param->private_data_len);
Sean Hefty628e5f62006-11-30 16:44:16 -08003343
Sean Heftye511d1a2013-07-24 15:06:08 -07003344 if (private_data) {
3345 ret = cma_format_hdr(private_data, id_priv);
Sean Heftye8160e12013-05-29 10:09:22 -07003346 if (ret)
3347 goto out;
Sean Heftye511d1a2013-07-24 15:06:08 -07003348 req.private_data = private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003349 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003350
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003351 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3352 id_priv);
3353 if (IS_ERR(id)) {
3354 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08003355 goto out;
3356 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003357 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08003358
Sean Heftyf4753832013-05-29 10:09:14 -07003359 req.path = id_priv->id.route.path_rec;
Sean Heftycf539362013-05-29 10:09:28 -07003360 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Hefty628e5f62006-11-30 16:44:16 -08003361 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3362 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3363
3364 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3365 if (ret) {
3366 ib_destroy_cm_id(id_priv->cm_id.ib);
3367 id_priv->cm_id.ib = NULL;
3368 }
3369out:
Sean Heftye511d1a2013-07-24 15:06:08 -07003370 kfree(private_data);
Sean Hefty628e5f62006-11-30 16:44:16 -08003371 return ret;
3372}
3373
Sean Heftye51060f2006-06-17 20:37:29 -07003374static int cma_connect_ib(struct rdma_id_private *id_priv,
3375 struct rdma_conn_param *conn_param)
3376{
3377 struct ib_cm_req_param req;
3378 struct rdma_route *route;
3379 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003380 struct ib_cm_id *id;
Bart Van Assche0c70b352017-10-11 10:48:45 -07003381 u8 offset;
3382 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003383
3384 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003385 offset = cma_user_data_offset(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003386 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003387 if (req.private_data_len < conn_param->private_data_len)
3388 return -EINVAL;
3389
Sean Heftye8160e12013-05-29 10:09:22 -07003390 if (req.private_data_len) {
3391 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3392 if (!private_data)
3393 return -ENOMEM;
3394 } else {
3395 private_data = NULL;
3396 }
Sean Heftye51060f2006-06-17 20:37:29 -07003397
3398 if (conn_param->private_data && conn_param->private_data_len)
3399 memcpy(private_data + offset, conn_param->private_data,
3400 conn_param->private_data_len);
3401
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003402 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3403 if (IS_ERR(id)) {
3404 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07003405 goto out;
3406 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003407 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07003408
3409 route = &id_priv->id.route;
Sean Heftye8160e12013-05-29 10:09:22 -07003410 if (private_data) {
3411 ret = cma_format_hdr(private_data, id_priv);
3412 if (ret)
3413 goto out;
3414 req.private_data = private_data;
3415 }
Sean Heftye51060f2006-06-17 20:37:29 -07003416
3417 req.primary_path = &route->path_rec[0];
3418 if (route->num_paths == 2)
3419 req.alternate_path = &route->path_rec[1];
3420
Sean Heftycf539362013-05-29 10:09:28 -07003421 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07003422 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07003423 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07003424 req.starting_psn = id_priv->seq_num;
3425 req.responder_resources = conn_param->responder_resources;
3426 req.initiator_depth = conn_param->initiator_depth;
3427 req.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003428 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3429 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003430 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3431 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3432 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3433 req.srq = id_priv->srq ? 1 : 0;
3434
3435 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3436out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003437 if (ret && !IS_ERR(id)) {
3438 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07003439 id_priv->cm_id.ib = NULL;
3440 }
3441
Sean Heftye51060f2006-06-17 20:37:29 -07003442 kfree(private_data);
3443 return ret;
3444}
3445
Tom Tucker07ebafb2006-08-03 16:02:42 -05003446static int cma_connect_iw(struct rdma_id_private *id_priv,
3447 struct rdma_conn_param *conn_param)
3448{
3449 struct iw_cm_id *cm_id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003450 int ret;
3451 struct iw_cm_conn_param iw_param;
3452
3453 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003454 if (IS_ERR(cm_id))
3455 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003456
Steve Wise68cdba02015-05-18 15:27:10 -05003457 cm_id->tos = id_priv->tos;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003458 id_priv->cm_id.iw = cm_id;
3459
Steve Wise24d44a32013-07-04 16:10:44 +05303460 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3461 rdma_addr_size(cma_src_addr(id_priv)));
3462 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3463 rdma_addr_size(cma_dst_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05003464
Sean Hefty5851bb82008-01-04 10:47:12 -08003465 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07003466 if (ret)
3467 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003468
Hefty, Seanf45ee802011-10-06 09:33:04 -07003469 if (conn_param) {
3470 iw_param.ord = conn_param->initiator_depth;
3471 iw_param.ird = conn_param->responder_resources;
3472 iw_param.private_data = conn_param->private_data;
3473 iw_param.private_data_len = conn_param->private_data_len;
3474 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3475 } else {
3476 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003477 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07003478 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003479 ret = iw_cm_connect(cm_id, &iw_param);
3480out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003481 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07003482 iw_destroy_cm_id(cm_id);
3483 id_priv->cm_id.iw = NULL;
3484 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003485 return ret;
3486}
3487
Sean Heftye51060f2006-06-17 20:37:29 -07003488int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3489{
3490 struct rdma_id_private *id_priv;
3491 int ret;
3492
3493 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003494 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003495 return -EINVAL;
3496
3497 if (!id->qp) {
3498 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003499 id_priv->srq = conn_param->srq;
3500 }
3501
Michael Wang72219cea2015-05-05 14:50:34 +02003502 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003503 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08003504 ret = cma_resolve_ib_udp(id_priv, conn_param);
3505 else
3506 ret = cma_connect_ib(id_priv, conn_param);
Michael Wang04215332015-05-05 14:50:35 +02003507 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003508 ret = cma_connect_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003509 else
Sean Heftye51060f2006-06-17 20:37:29 -07003510 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003511 if (ret)
3512 goto err;
3513
3514 return 0;
3515err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003516 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07003517 return ret;
3518}
3519EXPORT_SYMBOL(rdma_connect);
3520
3521static int cma_accept_ib(struct rdma_id_private *id_priv,
3522 struct rdma_conn_param *conn_param)
3523{
3524 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08003525 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003526
Sean Hefty5851bb82008-01-04 10:47:12 -08003527 ret = cma_modify_qp_rtr(id_priv, conn_param);
3528 if (ret)
3529 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08003530
Sean Hefty5851bb82008-01-04 10:47:12 -08003531 ret = cma_modify_qp_rts(id_priv, conn_param);
3532 if (ret)
3533 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003534
3535 memset(&rep, 0, sizeof rep);
3536 rep.qp_num = id_priv->qp_num;
3537 rep.starting_psn = id_priv->seq_num;
3538 rep.private_data = conn_param->private_data;
3539 rep.private_data_len = conn_param->private_data_len;
3540 rep.responder_resources = conn_param->responder_resources;
3541 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07003542 rep.failover_accepted = 0;
3543 rep.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003544 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003545 rep.srq = id_priv->srq ? 1 : 0;
3546
Sean Hefty0fe313b2006-11-30 16:37:15 -08003547 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3548out:
3549 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003550}
3551
Tom Tucker07ebafb2006-08-03 16:02:42 -05003552static int cma_accept_iw(struct rdma_id_private *id_priv,
3553 struct rdma_conn_param *conn_param)
3554{
3555 struct iw_cm_conn_param iw_param;
3556 int ret;
3557
Steve Wisec8cdd922017-02-21 11:21:57 -08003558 if (!conn_param)
3559 return -EINVAL;
3560
Sean Hefty5851bb82008-01-04 10:47:12 -08003561 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003562 if (ret)
3563 return ret;
3564
3565 iw_param.ord = conn_param->initiator_depth;
3566 iw_param.ird = conn_param->responder_resources;
3567 iw_param.private_data = conn_param->private_data;
3568 iw_param.private_data_len = conn_param->private_data_len;
3569 if (id_priv->id.qp) {
3570 iw_param.qpn = id_priv->qp_num;
3571 } else
3572 iw_param.qpn = conn_param->qp_num;
3573
3574 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3575}
3576
Sean Hefty628e5f62006-11-30 16:44:16 -08003577static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
Sean Hefty5c438132013-05-29 10:09:23 -07003578 enum ib_cm_sidr_status status, u32 qkey,
Sean Hefty628e5f62006-11-30 16:44:16 -08003579 const void *private_data, int private_data_len)
3580{
3581 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003582 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003583
3584 memset(&rep, 0, sizeof rep);
3585 rep.status = status;
3586 if (status == IB_SIDR_SUCCESS) {
Sean Hefty5c438132013-05-29 10:09:23 -07003587 ret = cma_set_qkey(id_priv, qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003588 if (ret)
3589 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003590 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003591 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08003592 }
3593 rep.private_data = private_data;
3594 rep.private_data_len = private_data_len;
3595
3596 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3597}
3598
Sean Heftye51060f2006-06-17 20:37:29 -07003599int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3600{
3601 struct rdma_id_private *id_priv;
3602 int ret;
3603
3604 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00003605
3606 id_priv->owner = task_pid_nr(current);
3607
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003608 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003609 return -EINVAL;
3610
3611 if (!id->qp && conn_param) {
3612 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003613 id_priv->srq = conn_param->srq;
3614 }
3615
Michael Wang72219cea2015-05-05 14:50:34 +02003616 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Hefty, Seanf45ee802011-10-06 09:33:04 -07003617 if (id->qp_type == IB_QPT_UD) {
3618 if (conn_param)
3619 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003620 conn_param->qkey,
Hefty, Seanf45ee802011-10-06 09:33:04 -07003621 conn_param->private_data,
3622 conn_param->private_data_len);
3623 else
3624 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003625 0, NULL, 0);
Hefty, Seanf45ee802011-10-06 09:33:04 -07003626 } else {
3627 if (conn_param)
3628 ret = cma_accept_ib(id_priv, conn_param);
3629 else
3630 ret = cma_rep_recv(id_priv);
3631 }
Michael Wang04215332015-05-05 14:50:35 +02003632 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003633 ret = cma_accept_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003634 else
Sean Heftye51060f2006-06-17 20:37:29 -07003635 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003636
3637 if (ret)
3638 goto reject;
3639
3640 return 0;
3641reject:
Sean Heftyc5483382007-09-24 13:19:09 -07003642 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003643 rdma_reject(id, NULL, 0);
3644 return ret;
3645}
3646EXPORT_SYMBOL(rdma_accept);
3647
Sean Hefty0fe313b2006-11-30 16:37:15 -08003648int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3649{
3650 struct rdma_id_private *id_priv;
3651 int ret;
3652
3653 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003654 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08003655 return -EINVAL;
3656
3657 switch (id->device->node_type) {
3658 case RDMA_NODE_IB_CA:
3659 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3660 break;
3661 default:
3662 ret = 0;
3663 break;
3664 }
3665 return ret;
3666}
3667EXPORT_SYMBOL(rdma_notify);
3668
Sean Heftye51060f2006-06-17 20:37:29 -07003669int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3670 u8 private_data_len)
3671{
3672 struct rdma_id_private *id_priv;
3673 int ret;
3674
3675 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003676 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003677 return -EINVAL;
3678
Michael Wang72219cea2015-05-05 14:50:34 +02003679 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003680 if (id->qp_type == IB_QPT_UD)
Sean Hefty5c438132013-05-29 10:09:23 -07003681 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
Sean Hefty628e5f62006-11-30 16:44:16 -08003682 private_data, private_data_len);
3683 else
3684 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3685 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3686 0, private_data, private_data_len);
Michael Wang04215332015-05-05 14:50:35 +02003687 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003688 ret = iw_cm_reject(id_priv->cm_id.iw,
3689 private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003690 } else
Sean Heftye51060f2006-06-17 20:37:29 -07003691 ret = -ENOSYS;
Michael Wang21655af2015-05-05 14:50:27 +02003692
Sean Heftye51060f2006-06-17 20:37:29 -07003693 return ret;
3694}
3695EXPORT_SYMBOL(rdma_reject);
3696
3697int rdma_disconnect(struct rdma_cm_id *id)
3698{
3699 struct rdma_id_private *id_priv;
3700 int ret;
3701
3702 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003703 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003704 return -EINVAL;
3705
Michael Wang72219cea2015-05-05 14:50:34 +02003706 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyc5483382007-09-24 13:19:09 -07003707 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003708 if (ret)
3709 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003710 /* Initiate or respond to a disconnect. */
3711 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3712 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
Michael Wang04215332015-05-05 14:50:35 +02003713 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003714 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003715 } else
Tom Tucker07ebafb2006-08-03 16:02:42 -05003716 ret = -EINVAL;
Michael Wang21655af2015-05-05 14:50:27 +02003717
Sean Heftye51060f2006-06-17 20:37:29 -07003718out:
3719 return ret;
3720}
3721EXPORT_SYMBOL(rdma_disconnect);
3722
Sean Heftyc8f6a362007-02-15 17:00:18 -08003723static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3724{
3725 struct rdma_id_private *id_priv;
3726 struct cma_multicast *mc = multicast->context;
3727 struct rdma_cm_event event;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003728 int ret = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003729
3730 id_priv = mc->id_priv;
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003731 mutex_lock(&id_priv->handler_mutex);
3732 if (id_priv->state != RDMA_CM_ADDR_BOUND &&
3733 id_priv->state != RDMA_CM_ADDR_RESOLVED)
3734 goto out;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003735
Sean Hefty5c438132013-05-29 10:09:23 -07003736 if (!status)
3737 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
Sean Heftyc5483382007-09-24 13:19:09 -07003738 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003739 if (!status && id_priv->id.qp)
3740 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003741 be16_to_cpu(multicast->rec.mlid));
Sean Heftyc5483382007-09-24 13:19:09 -07003742 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003743
3744 memset(&event, 0, sizeof event);
3745 event.status = status;
3746 event.param.ud.private_data = mc->context;
3747 if (!status) {
Moni Shouabee3c3c2015-12-23 14:56:57 +02003748 struct rdma_dev_addr *dev_addr =
3749 &id_priv->id.route.addr.dev_addr;
3750 struct net_device *ndev =
3751 dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3752 enum ib_gid_type gid_type =
3753 id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3754 rdma_start_port(id_priv->cma_dev->device)];
3755
Sean Heftyc8f6a362007-02-15 17:00:18 -08003756 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3757 ib_init_ah_from_mcmember(id_priv->id.device,
3758 id_priv->id.port_num, &multicast->rec,
Moni Shouabee3c3c2015-12-23 14:56:57 +02003759 ndev, gid_type,
Sean Heftyc8f6a362007-02-15 17:00:18 -08003760 &event.param.ud.ah_attr);
3761 event.param.ud.qp_num = 0xFFFFFF;
3762 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
Moni Shouabee3c3c2015-12-23 14:56:57 +02003763 if (ndev)
3764 dev_put(ndev);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003765 } else
3766 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3767
3768 ret = id_priv->id.event_handler(&id_priv->id, &event);
3769 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003770 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003771 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003772 rdma_destroy_id(&id_priv->id);
3773 return 0;
3774 }
Sean Hefty8aa08602007-05-07 11:49:00 -07003775
Bart Van Assche37e07cd2016-06-10 11:08:25 -07003776out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07003777 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003778 return 0;
3779}
3780
3781static void cma_set_mgid(struct rdma_id_private *id_priv,
3782 struct sockaddr *addr, union ib_gid *mgid)
3783{
3784 unsigned char mc_map[MAX_ADDR_LEN];
3785 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3786 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3787 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3788
3789 if (cma_any_addr(addr)) {
3790 memset(mgid, 0, sizeof *mgid);
3791 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08003792 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08003793 0xFF10A01B)) {
3794 /* IPv6 address is an SA assigned MGID. */
3795 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003796 } else if (addr->sa_family == AF_IB) {
3797 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08003798 } else if ((addr->sa_family == AF_INET6)) {
3799 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3800 if (id_priv->id.ps == RDMA_PS_UDP)
3801 mc_map[7] = 0x01; /* Use RDMA CM signature */
3802 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003803 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07003804 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003805 if (id_priv->id.ps == RDMA_PS_UDP)
3806 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08003807 *mgid = *(union ib_gid *) (mc_map + 4);
3808 }
3809}
3810
Alex Veskerab15c952016-07-06 16:36:35 +03003811static void cma_query_sa_classport_info_cb(int status,
3812 struct ib_class_port_info *rec,
3813 void *context)
3814{
3815 struct class_port_info_context *cb_ctx = context;
3816
3817 WARN_ON(!context);
3818
3819 if (status || !rec) {
3820 pr_debug("RDMA CM: %s port %u failed query ClassPortInfo status: %d\n",
3821 cb_ctx->device->name, cb_ctx->port_num, status);
3822 goto out;
3823 }
3824
3825 memcpy(cb_ctx->class_port_info, rec, sizeof(struct ib_class_port_info));
3826
3827out:
3828 complete(&cb_ctx->done);
3829}
3830
3831static int cma_query_sa_classport_info(struct ib_device *device, u8 port_num,
3832 struct ib_class_port_info *class_port_info)
3833{
3834 struct class_port_info_context *cb_ctx;
3835 int ret;
3836
3837 cb_ctx = kmalloc(sizeof(*cb_ctx), GFP_KERNEL);
3838 if (!cb_ctx)
3839 return -ENOMEM;
3840
3841 cb_ctx->device = device;
3842 cb_ctx->class_port_info = class_port_info;
3843 cb_ctx->port_num = port_num;
3844 init_completion(&cb_ctx->done);
3845
3846 ret = ib_sa_classport_info_rec_query(&sa_client, device, port_num,
3847 CMA_QUERY_CLASSPORT_INFO_TIMEOUT,
3848 GFP_KERNEL, cma_query_sa_classport_info_cb,
3849 cb_ctx, &cb_ctx->sa_query);
3850 if (ret < 0) {
3851 pr_err("RDMA CM: %s port %u failed to send ClassPortInfo query, ret: %d\n",
3852 device->name, port_num, ret);
3853 goto out;
3854 }
3855
3856 wait_for_completion(&cb_ctx->done);
3857
3858out:
3859 kfree(cb_ctx);
3860 return ret;
3861}
3862
Sean Heftyc8f6a362007-02-15 17:00:18 -08003863static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3864 struct cma_multicast *mc)
3865{
3866 struct ib_sa_mcmember_rec rec;
Alex Veskerab15c952016-07-06 16:36:35 +03003867 struct ib_class_port_info class_port_info;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003868 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3869 ib_sa_comp_mask comp_mask;
3870 int ret;
3871
3872 ib_addr_get_mgid(dev_addr, &rec.mgid);
3873 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3874 &rec.mgid, &rec);
3875 if (ret)
3876 return ret;
3877
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003878 ret = cma_set_qkey(id_priv, 0);
3879 if (ret)
3880 return ret;
3881
Roland Dreier3f446752008-08-04 11:02:14 -07003882 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003883 rec.qkey = cpu_to_be32(id_priv->qkey);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003884 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003885 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Alex Veskerab15c952016-07-06 16:36:35 +03003886 rec.join_state = mc->join_state;
3887
3888 if (rec.join_state == BIT(SENDONLY_FULLMEMBER_JOIN)) {
3889 ret = cma_query_sa_classport_info(id_priv->id.device,
3890 id_priv->id.port_num,
3891 &class_port_info);
3892
3893 if (ret)
3894 return ret;
3895
3896 if (!(ib_get_cpi_capmask2(&class_port_info) &
3897 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT)) {
3898 pr_warn("RDMA CM: %s port %u Unable to multicast join\n"
3899 "RDMA CM: SM doesn't support Send Only Full Member option\n",
3900 id_priv->id.device->name, id_priv->id.port_num);
3901 return -EOPNOTSUPP;
3902 }
3903 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003904
3905 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3906 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3907 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3908 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3909 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3910
Yossi Etigin84adeee2009-04-01 13:55:32 -07003911 if (id_priv->id.ps == RDMA_PS_IPOIB)
3912 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
Dotan Barak2a22fb82012-08-30 09:09:55 +00003913 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3914 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3915 IB_SA_MCMEMBER_REC_MTU |
3916 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Yossi Etigin84adeee2009-04-01 13:55:32 -07003917
Sean Heftyc8f6a362007-02-15 17:00:18 -08003918 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3919 id_priv->id.port_num, &rec,
3920 comp_mask, GFP_KERNEL,
3921 cma_ib_mc_handler, mc);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303922 return PTR_ERR_OR_ZERO(mc->multicast.ib);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003923}
3924
Eli Cohen3c86aa72010-10-13 21:26:51 +02003925static void iboe_mcast_work_handler(struct work_struct *work)
3926{
3927 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3928 struct cma_multicast *mc = mw->mc;
3929 struct ib_sa_multicast *m = mc->multicast.ib;
3930
3931 mc->multicast.ib->context = mc;
3932 cma_ib_mc_handler(0, m);
3933 kref_put(&mc->mcref, release_mc);
3934 kfree(mw);
3935}
3936
3937static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3938{
3939 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3940 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3941
3942 if (cma_any_addr(addr)) {
3943 memset(mgid, 0, sizeof *mgid);
3944 } else if (addr->sa_family == AF_INET6) {
3945 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3946 } else {
3947 mgid->raw[0] = 0xff;
3948 mgid->raw[1] = 0x0e;
3949 mgid->raw[2] = 0;
3950 mgid->raw[3] = 0;
3951 mgid->raw[4] = 0;
3952 mgid->raw[5] = 0;
3953 mgid->raw[6] = 0;
3954 mgid->raw[7] = 0;
3955 mgid->raw[8] = 0;
3956 mgid->raw[9] = 0;
3957 mgid->raw[10] = 0xff;
3958 mgid->raw[11] = 0xff;
3959 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3960 }
3961}
3962
3963static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3964 struct cma_multicast *mc)
3965{
3966 struct iboe_mcast_work *work;
3967 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Moni Shouabee3c3c2015-12-23 14:56:57 +02003968 int err = 0;
Eli Cohen3c86aa72010-10-13 21:26:51 +02003969 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3970 struct net_device *ndev = NULL;
Moni Shouabee3c3c2015-12-23 14:56:57 +02003971 enum ib_gid_type gid_type;
Alex Veskerab15c952016-07-06 16:36:35 +03003972 bool send_only;
3973
3974 send_only = mc->join_state == BIT(SENDONLY_FULLMEMBER_JOIN);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003975
3976 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3977 return -EINVAL;
3978
3979 work = kzalloc(sizeof *work, GFP_KERNEL);
3980 if (!work)
3981 return -ENOMEM;
3982
3983 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3984 if (!mc->multicast.ib) {
3985 err = -ENOMEM;
3986 goto out1;
3987 }
3988
3989 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3990
3991 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3992 if (id_priv->id.ps == RDMA_PS_UDP)
3993 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3994
3995 if (dev_addr->bound_dev_if)
3996 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3997 if (!ndev) {
3998 err = -ENODEV;
3999 goto out2;
4000 }
4001 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
4002 mc->multicast.ib->rec.hop_limit = 1;
4003 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
Moni Shouabee3c3c2015-12-23 14:56:57 +02004004
4005 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
4006 rdma_start_port(id_priv->cma_dev->device)];
4007 if (addr->sa_family == AF_INET) {
Alex Veskerc65f6c52016-06-22 17:27:23 +03004008 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) {
4009 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
Alex Veskerab15c952016-07-06 16:36:35 +03004010 if (!send_only) {
4011 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
4012 true);
4013 if (!err)
4014 mc->igmp_joined = true;
4015 }
Moni Shouabee3c3c2015-12-23 14:56:57 +02004016 }
4017 } else {
4018 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
4019 err = -ENOTSUPP;
4020 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02004021 dev_put(ndev);
Moni Shouabee3c3c2015-12-23 14:56:57 +02004022 if (err || !mc->multicast.ib->rec.mtu) {
4023 if (!err)
4024 err = -EINVAL;
Eli Cohen3c86aa72010-10-13 21:26:51 +02004025 goto out2;
4026 }
Moni Shoua7b856272013-12-12 18:03:12 +02004027 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
4028 &mc->multicast.ib->rec.port_gid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02004029 work->id = id_priv;
4030 work->mc = mc;
4031 INIT_WORK(&work->work, iboe_mcast_work_handler);
4032 kref_get(&mc->mcref);
4033 queue_work(cma_wq, &work->work);
4034
4035 return 0;
4036
4037out2:
4038 kfree(mc->multicast.ib);
4039out1:
4040 kfree(work);
4041 return err;
4042}
4043
Sean Heftyc8f6a362007-02-15 17:00:18 -08004044int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
Alex Veskerab15c952016-07-06 16:36:35 +03004045 u8 join_state, void *context)
Sean Heftyc8f6a362007-02-15 17:00:18 -08004046{
4047 struct rdma_id_private *id_priv;
4048 struct cma_multicast *mc;
4049 int ret;
4050
Leon Romanovskye3fb6522018-03-13 11:43:23 +02004051 if (!id->device)
4052 return -EINVAL;
4053
Sean Heftyc8f6a362007-02-15 17:00:18 -08004054 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004055 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
4056 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08004057 return -EINVAL;
4058
4059 mc = kmalloc(sizeof *mc, GFP_KERNEL);
4060 if (!mc)
4061 return -ENOMEM;
4062
Sean Heftyef560862013-05-29 10:09:10 -07004063 memcpy(&mc->addr, addr, rdma_addr_size(addr));
Sean Heftyc8f6a362007-02-15 17:00:18 -08004064 mc->context = context;
4065 mc->id_priv = id_priv;
Moni Shouabee3c3c2015-12-23 14:56:57 +02004066 mc->igmp_joined = false;
Alex Veskerab15c952016-07-06 16:36:35 +03004067 mc->join_state = join_state;
Sean Heftyc8f6a362007-02-15 17:00:18 -08004068 spin_lock(&id_priv->lock);
4069 list_add(&mc->list, &id_priv->mc_list);
4070 spin_unlock(&id_priv->lock);
4071
Ira Weiny5d9fb042015-05-14 15:01:46 -04004072 if (rdma_protocol_roce(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02004073 kref_init(&mc->mcref);
4074 ret = cma_iboe_join_multicast(id_priv, mc);
Michael Wanga31ad3b2015-05-05 14:50:37 +02004075 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02004076 ret = cma_join_ib_multicast(id_priv, mc);
4077 else
Sean Heftyc8f6a362007-02-15 17:00:18 -08004078 ret = -ENOSYS;
Sean Heftyc8f6a362007-02-15 17:00:18 -08004079
4080 if (ret) {
4081 spin_lock_irq(&id_priv->lock);
4082 list_del(&mc->list);
4083 spin_unlock_irq(&id_priv->lock);
4084 kfree(mc);
4085 }
4086 return ret;
4087}
4088EXPORT_SYMBOL(rdma_join_multicast);
4089
4090void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
4091{
4092 struct rdma_id_private *id_priv;
4093 struct cma_multicast *mc;
4094
4095 id_priv = container_of(id, struct rdma_id_private, id);
4096 spin_lock_irq(&id_priv->lock);
4097 list_for_each_entry(mc, &id_priv->mc_list, list) {
Sean Heftyef560862013-05-29 10:09:10 -07004098 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
Sean Heftyc8f6a362007-02-15 17:00:18 -08004099 list_del(&mc->list);
4100 spin_unlock_irq(&id_priv->lock);
4101
4102 if (id->qp)
4103 ib_detach_mcast(id->qp,
4104 &mc->multicast.ib->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00004105 be16_to_cpu(mc->multicast.ib->rec.mlid));
Michael Wang5c9a5282015-05-05 14:50:29 +02004106
4107 BUG_ON(id_priv->cma_dev->device != id->device);
4108
Michael Wanga31ad3b2015-05-05 14:50:37 +02004109 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02004110 ib_sa_free_multicast(mc->multicast.ib);
4111 kfree(mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02004112 } else if (rdma_protocol_roce(id->device, id->port_num)) {
4113 if (mc->igmp_joined) {
4114 struct rdma_dev_addr *dev_addr =
4115 &id->route.addr.dev_addr;
4116 struct net_device *ndev = NULL;
Michael Wang5c9a5282015-05-05 14:50:29 +02004117
Moni Shouabee3c3c2015-12-23 14:56:57 +02004118 if (dev_addr->bound_dev_if)
4119 ndev = dev_get_by_index(&init_net,
4120 dev_addr->bound_dev_if);
4121 if (ndev) {
4122 cma_igmp_send(ndev,
4123 &mc->multicast.ib->rec.mgid,
4124 false);
4125 dev_put(ndev);
4126 }
4127 mc->igmp_joined = false;
4128 }
4129 kref_put(&mc->mcref, release_mc);
4130 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08004131 return;
4132 }
4133 }
4134 spin_unlock_irq(&id_priv->lock);
4135}
4136EXPORT_SYMBOL(rdma_leave_multicast);
4137
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004138static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4139{
4140 struct rdma_dev_addr *dev_addr;
4141 struct cma_ndev_work *work;
4142
4143 dev_addr = &id_priv->id.route.addr.dev_addr;
4144
Sean Hefty6266ed62009-11-19 12:55:22 -08004145 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Guy Shapirofa201052015-10-22 15:20:10 +03004146 (net_eq(dev_net(ndev), dev_addr->net)) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004147 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05304148 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4149 ndev->name, &id_priv->id);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004150 work = kzalloc(sizeof *work, GFP_KERNEL);
4151 if (!work)
4152 return -ENOMEM;
4153
4154 INIT_WORK(&work->work, cma_ndev_work_handler);
4155 work->id = id_priv;
4156 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4157 atomic_inc(&id_priv->refcount);
4158 queue_work(cma_wq, &work->work);
4159 }
4160
4161 return 0;
4162}
4163
4164static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00004165 void *ptr)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004166{
Jiri Pirko351638e2013-05-28 01:30:21 +00004167 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004168 struct cma_device *cma_dev;
4169 struct rdma_id_private *id_priv;
4170 int ret = NOTIFY_DONE;
4171
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004172 if (event != NETDEV_BONDING_FAILOVER)
4173 return NOTIFY_DONE;
4174
4175 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
4176 return NOTIFY_DONE;
4177
4178 mutex_lock(&lock);
4179 list_for_each_entry(cma_dev, &dev_list, list)
4180 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4181 ret = cma_netdev_change(ndev, id_priv);
4182 if (ret)
4183 goto out;
4184 }
4185
4186out:
4187 mutex_unlock(&lock);
4188 return ret;
4189}
4190
4191static struct notifier_block cma_nb = {
4192 .notifier_call = cma_netdev_callback
4193};
4194
Sean Heftye51060f2006-06-17 20:37:29 -07004195static void cma_add_one(struct ib_device *device)
4196{
4197 struct cma_device *cma_dev;
4198 struct rdma_id_private *id_priv;
Matan Barak045959d2015-12-23 14:56:55 +02004199 unsigned int i;
4200 unsigned long supported_gids = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07004201
4202 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4203 if (!cma_dev)
4204 return;
4205
4206 cma_dev->device = device;
Matan Barak045959d2015-12-23 14:56:55 +02004207 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4208 sizeof(*cma_dev->default_gid_type),
4209 GFP_KERNEL);
4210 if (!cma_dev->default_gid_type) {
4211 kfree(cma_dev);
4212 return;
4213 }
4214 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4215 supported_gids = roce_gid_type_mask_support(device, i);
4216 WARN_ON(!supported_gids);
4217 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4218 find_first_bit(&supported_gids, BITS_PER_LONG);
4219 }
Sean Heftye51060f2006-06-17 20:37:29 -07004220
4221 init_completion(&cma_dev->comp);
4222 atomic_set(&cma_dev->refcount, 1);
4223 INIT_LIST_HEAD(&cma_dev->id_list);
4224 ib_set_client_data(device, &cma_client, cma_dev);
4225
4226 mutex_lock(&lock);
4227 list_add_tail(&cma_dev->list, &dev_list);
4228 list_for_each_entry(id_priv, &listen_any_list, list)
4229 cma_listen_on_dev(id_priv, cma_dev);
4230 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07004231}
4232
4233static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4234{
Sean Heftya1b1b612006-11-30 16:33:14 -08004235 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004236 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07004237 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07004238
4239 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004240 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4241 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07004242 return 0;
4243
4244 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07004245 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07004246
4247 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004248 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07004249 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07004250
Sean Heftya1b1b612006-11-30 16:33:14 -08004251 memset(&event, 0, sizeof event);
4252 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07004253 ret = id_priv->id.event_handler(&id_priv->id, &event);
4254out:
4255 mutex_unlock(&id_priv->handler_mutex);
4256 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07004257}
4258
4259static void cma_process_remove(struct cma_device *cma_dev)
4260{
Sean Heftye51060f2006-06-17 20:37:29 -07004261 struct rdma_id_private *id_priv;
4262 int ret;
4263
Sean Heftye51060f2006-06-17 20:37:29 -07004264 mutex_lock(&lock);
4265 while (!list_empty(&cma_dev->id_list)) {
4266 id_priv = list_entry(cma_dev->id_list.next,
4267 struct rdma_id_private, list);
4268
Sean Heftyd02d1f52007-10-09 11:12:34 -07004269 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07004270 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07004271 atomic_inc(&id_priv->refcount);
4272 mutex_unlock(&lock);
4273
Sean Heftyd02d1f52007-10-09 11:12:34 -07004274 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07004275 cma_deref_id(id_priv);
4276 if (ret)
4277 rdma_destroy_id(&id_priv->id);
4278
4279 mutex_lock(&lock);
4280 }
4281 mutex_unlock(&lock);
4282
4283 cma_deref_dev(cma_dev);
4284 wait_for_completion(&cma_dev->comp);
4285}
4286
Haggai Eran7c1eb452015-07-30 17:50:14 +03004287static void cma_remove_one(struct ib_device *device, void *client_data)
Sean Heftye51060f2006-06-17 20:37:29 -07004288{
Haggai Eran7c1eb452015-07-30 17:50:14 +03004289 struct cma_device *cma_dev = client_data;
Sean Heftye51060f2006-06-17 20:37:29 -07004290
Sean Heftye51060f2006-06-17 20:37:29 -07004291 if (!cma_dev)
4292 return;
4293
4294 mutex_lock(&lock);
4295 list_del(&cma_dev->list);
4296 mutex_unlock(&lock);
4297
4298 cma_process_remove(cma_dev);
Matan Barak045959d2015-12-23 14:56:55 +02004299 kfree(cma_dev->default_gid_type);
Sean Heftye51060f2006-06-17 20:37:29 -07004300 kfree(cma_dev);
4301}
4302
Nir Muchtar753f6182011-01-03 15:33:53 +00004303static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
4304{
4305 struct nlmsghdr *nlh;
4306 struct rdma_cm_id_stats *id_stats;
4307 struct rdma_id_private *id_priv;
4308 struct rdma_cm_id *id = NULL;
4309 struct cma_device *cma_dev;
4310 int i_dev = 0, i_id = 0;
4311
4312 /*
4313 * We export all of the IDs as a sequence of messages. Each
4314 * ID gets its own netlink message.
4315 */
4316 mutex_lock(&lock);
4317
4318 list_for_each_entry(cma_dev, &dev_list, list) {
4319 if (i_dev < cb->args[0]) {
4320 i_dev++;
4321 continue;
4322 }
4323
4324 i_id = 0;
4325 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4326 if (i_id < cb->args[1]) {
4327 i_id++;
4328 continue;
4329 }
4330
4331 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
4332 sizeof *id_stats, RDMA_NL_RDMA_CM,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -05004333 RDMA_NL_RDMA_CM_ID_STATS,
4334 NLM_F_MULTI);
Nir Muchtar753f6182011-01-03 15:33:53 +00004335 if (!id_stats)
4336 goto out;
4337
4338 memset(id_stats, 0, sizeof *id_stats);
4339 id = &id_priv->id;
4340 id_stats->node_type = id->route.addr.dev_addr.dev_type;
4341 id_stats->port_num = id->port_num;
4342 id_stats->bound_dev_if =
4343 id->route.addr.dev_addr.bound_dev_if;
4344
Sean Heftyce117ff2013-05-29 10:09:34 -07004345 if (ibnl_put_attr(skb, nlh,
4346 rdma_addr_size(cma_src_addr(id_priv)),
4347 cma_src_addr(id_priv),
4348 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
4349 goto out;
4350 if (ibnl_put_attr(skb, nlh,
Parav Pandit211a1e92017-11-14 14:51:55 +02004351 rdma_addr_size(cma_dst_addr(id_priv)),
Sean Heftyce117ff2013-05-29 10:09:34 -07004352 cma_dst_addr(id_priv),
4353 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
4354 goto out;
Nir Muchtar753f6182011-01-03 15:33:53 +00004355
Nir Muchtar83e95022011-01-13 13:56:04 +00004356 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00004357 id_stats->port_space = id->ps;
4358 id_stats->cm_state = id_priv->state;
4359 id_stats->qp_num = id_priv->qp_num;
4360 id_stats->qp_type = id->qp_type;
4361
4362 i_id++;
4363 }
4364
4365 cb->args[1] = 0;
4366 i_dev++;
4367 }
4368
4369out:
4370 mutex_unlock(&lock);
4371 cb->args[0] = i_dev;
4372 cb->args[1] = i_id;
4373
4374 return skb->len;
4375}
4376
4377static const struct ibnl_client_cbs cma_cb_table[] = {
Gao feng809d5fc2012-10-04 20:15:49 +00004378 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
4379 .module = THIS_MODULE },
Nir Muchtar753f6182011-01-03 15:33:53 +00004380};
4381
Haggai Eran4be74b42015-10-22 15:20:09 +03004382static int cma_init_net(struct net *net)
4383{
4384 struct cma_pernet *pernet = cma_pernet(net);
4385
4386 idr_init(&pernet->tcp_ps);
4387 idr_init(&pernet->udp_ps);
4388 idr_init(&pernet->ipoib_ps);
4389 idr_init(&pernet->ib_ps);
4390
4391 return 0;
4392}
4393
4394static void cma_exit_net(struct net *net)
4395{
4396 struct cma_pernet *pernet = cma_pernet(net);
4397
4398 idr_destroy(&pernet->tcp_ps);
4399 idr_destroy(&pernet->udp_ps);
4400 idr_destroy(&pernet->ipoib_ps);
4401 idr_destroy(&pernet->ib_ps);
4402}
4403
4404static struct pernet_operations cma_pernet_operations = {
4405 .init = cma_init_net,
4406 .exit = cma_exit_net,
4407 .id = &cma_pernet_id,
4408 .size = sizeof(struct cma_pernet),
4409};
4410
Peter Huewe716abb12009-06-23 10:38:42 -07004411static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004412{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09004413 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004414
Bhaktipriya Shridhardee9acb2016-08-15 23:29:35 +05304415 cma_wq = alloc_ordered_workqueue("rdma_cm", WQ_MEM_RECLAIM);
Sean Heftye51060f2006-06-17 20:37:29 -07004416 if (!cma_wq)
4417 return -ENOMEM;
4418
Haggai Eran4be74b42015-10-22 15:20:09 +03004419 ret = register_pernet_subsys(&cma_pernet_operations);
4420 if (ret)
4421 goto err_wq;
4422
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004423 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08004424 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004425 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004426
Sean Heftye51060f2006-06-17 20:37:29 -07004427 ret = ib_register_client(&cma_client);
4428 if (ret)
4429 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00004430
Mark Bloch2fa2d4f2016-05-06 22:45:26 +03004431 if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table),
4432 cma_cb_table))
Parav Panditaba25a3e2016-03-02 00:50:29 +05304433 pr_warn("RDMA CMA: failed to add netlink callback\n");
Matan Barak045959d2015-12-23 14:56:55 +02004434 cma_configfs_init();
Nir Muchtar753f6182011-01-03 15:33:53 +00004435
Sean Heftye51060f2006-06-17 20:37:29 -07004436 return 0;
4437
4438err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004439 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004440 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004441 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004442err_wq:
Sean Heftye51060f2006-06-17 20:37:29 -07004443 destroy_workqueue(cma_wq);
4444 return ret;
4445}
4446
Peter Huewe716abb12009-06-23 10:38:42 -07004447static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004448{
Matan Barak045959d2015-12-23 14:56:55 +02004449 cma_configfs_exit();
Nir Muchtar753f6182011-01-03 15:33:53 +00004450 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07004451 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004452 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004453 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004454 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004455 unregister_pernet_subsys(&cma_pernet_operations);
Sean Heftye51060f2006-06-17 20:37:29 -07004456 destroy_workqueue(cma_wq);
Sean Heftye51060f2006-06-17 20:37:29 -07004457}
4458
4459module_init(cma_init);
4460module_exit(cma_cleanup);