blob: f0c91ba3178a12030a8cc4e38db7a651633759b8 [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
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030071#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070072#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020073#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070074
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030075static const char * const cma_events[] = {
76 [RDMA_CM_EVENT_ADDR_RESOLVED] = "address resolved",
77 [RDMA_CM_EVENT_ADDR_ERROR] = "address error",
78 [RDMA_CM_EVENT_ROUTE_RESOLVED] = "route resolved ",
79 [RDMA_CM_EVENT_ROUTE_ERROR] = "route error",
80 [RDMA_CM_EVENT_CONNECT_REQUEST] = "connect request",
81 [RDMA_CM_EVENT_CONNECT_RESPONSE] = "connect response",
82 [RDMA_CM_EVENT_CONNECT_ERROR] = "connect error",
83 [RDMA_CM_EVENT_UNREACHABLE] = "unreachable",
84 [RDMA_CM_EVENT_REJECTED] = "rejected",
85 [RDMA_CM_EVENT_ESTABLISHED] = "established",
86 [RDMA_CM_EVENT_DISCONNECTED] = "disconnected",
87 [RDMA_CM_EVENT_DEVICE_REMOVAL] = "device removal",
88 [RDMA_CM_EVENT_MULTICAST_JOIN] = "multicast join",
89 [RDMA_CM_EVENT_MULTICAST_ERROR] = "multicast error",
90 [RDMA_CM_EVENT_ADDR_CHANGE] = "address change",
91 [RDMA_CM_EVENT_TIMEWAIT_EXIT] = "timewait exit",
92};
93
Bart Van Asschedb7489e2015-08-03 10:01:52 -070094const char *__attribute_const__ rdma_event_msg(enum rdma_cm_event_type event)
Sagi Grimberg2b1b5b62015-05-18 13:40:28 +030095{
96 size_t index = event;
97
98 return (index < ARRAY_SIZE(cma_events) && cma_events[index]) ?
99 cma_events[index] : "unrecognized event";
100}
101EXPORT_SYMBOL(rdma_event_msg);
102
Sean Heftye51060f2006-06-17 20:37:29 -0700103static void cma_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +0300104static void cma_remove_one(struct ib_device *device, void *client_data);
Sean Heftye51060f2006-06-17 20:37:29 -0700105
106static struct ib_client cma_client = {
107 .name = "cma",
108 .add = cma_add_one,
109 .remove = cma_remove_one
110};
111
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700112static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -0800113static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -0700114static LIST_HEAD(dev_list);
115static LIST_HEAD(listen_any_list);
116static DEFINE_MUTEX(lock);
117static struct workqueue_struct *cma_wq;
Haggai Eran4be74b42015-10-22 15:20:09 +0300118static int cma_pernet_id;
Sean Heftye51060f2006-06-17 20:37:29 -0700119
Haggai Eran4be74b42015-10-22 15:20:09 +0300120struct cma_pernet {
121 struct idr tcp_ps;
122 struct idr udp_ps;
123 struct idr ipoib_ps;
124 struct idr ib_ps;
125};
126
127static struct cma_pernet *cma_pernet(struct net *net)
Haggai Eranaac978e2015-07-30 17:50:20 +0300128{
Haggai Eran4be74b42015-10-22 15:20:09 +0300129 return net_generic(net, cma_pernet_id);
130}
131
132static struct idr *cma_pernet_idr(struct net *net, enum rdma_port_space ps)
133{
134 struct cma_pernet *pernet = cma_pernet(net);
135
Haggai Eranaac978e2015-07-30 17:50:20 +0300136 switch (ps) {
137 case RDMA_PS_TCP:
Haggai Eran4be74b42015-10-22 15:20:09 +0300138 return &pernet->tcp_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300139 case RDMA_PS_UDP:
Haggai Eran4be74b42015-10-22 15:20:09 +0300140 return &pernet->udp_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300141 case RDMA_PS_IPOIB:
Haggai Eran4be74b42015-10-22 15:20:09 +0300142 return &pernet->ipoib_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300143 case RDMA_PS_IB:
Haggai Eran4be74b42015-10-22 15:20:09 +0300144 return &pernet->ib_ps;
Haggai Eranaac978e2015-07-30 17:50:20 +0300145 default:
146 return NULL;
147 }
148}
149
Sean Heftye51060f2006-06-17 20:37:29 -0700150struct cma_device {
151 struct list_head list;
152 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -0700153 struct completion comp;
154 atomic_t refcount;
155 struct list_head id_list;
Matan Barak045959d2015-12-23 14:56:55 +0200156 enum ib_gid_type *default_gid_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700157};
158
Sean Heftye51060f2006-06-17 20:37:29 -0700159struct rdma_bind_list {
Haggai Eranaac978e2015-07-30 17:50:20 +0300160 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -0700161 struct hlist_head owners;
162 unsigned short port;
163};
164
Haggai Eran4be74b42015-10-22 15:20:09 +0300165static int cma_ps_alloc(struct net *net, enum rdma_port_space ps,
Haggai Eranaac978e2015-07-30 17:50:20 +0300166 struct rdma_bind_list *bind_list, int snum)
167{
Haggai Eran4be74b42015-10-22 15:20:09 +0300168 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300169
170 return idr_alloc(idr, bind_list, snum, snum + 1, GFP_KERNEL);
171}
172
Haggai Eran4be74b42015-10-22 15:20:09 +0300173static struct rdma_bind_list *cma_ps_find(struct net *net,
174 enum rdma_port_space ps, int snum)
Haggai Eranaac978e2015-07-30 17:50:20 +0300175{
Haggai Eran4be74b42015-10-22 15:20:09 +0300176 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300177
178 return idr_find(idr, snum);
179}
180
Haggai Eran4be74b42015-10-22 15:20:09 +0300181static void cma_ps_remove(struct net *net, enum rdma_port_space ps, int snum)
Haggai Eranaac978e2015-07-30 17:50:20 +0300182{
Haggai Eran4be74b42015-10-22 15:20:09 +0300183 struct idr *idr = cma_pernet_idr(net, ps);
Haggai Eranaac978e2015-07-30 17:50:20 +0300184
185 idr_remove(idr, snum);
186}
187
Sean Hefty68602122012-06-14 20:31:39 +0000188enum {
189 CMA_OPTION_AFONLY,
190};
191
Matan Barak218a7732015-12-23 14:56:54 +0200192void cma_ref_dev(struct cma_device *cma_dev)
193{
194 atomic_inc(&cma_dev->refcount);
195}
196
Matan Barak045959d2015-12-23 14:56:55 +0200197struct cma_device *cma_enum_devices_by_ibdev(cma_device_filter filter,
198 void *cookie)
199{
200 struct cma_device *cma_dev;
201 struct cma_device *found_cma_dev = NULL;
202
203 mutex_lock(&lock);
204
205 list_for_each_entry(cma_dev, &dev_list, list)
206 if (filter(cma_dev->device, cookie)) {
207 found_cma_dev = cma_dev;
208 break;
209 }
210
211 if (found_cma_dev)
212 cma_ref_dev(found_cma_dev);
213 mutex_unlock(&lock);
214 return found_cma_dev;
215}
216
217int cma_get_default_gid_type(struct cma_device *cma_dev,
218 unsigned int port)
219{
220 if (port < rdma_start_port(cma_dev->device) ||
221 port > rdma_end_port(cma_dev->device))
222 return -EINVAL;
223
224 return cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)];
225}
226
227int cma_set_default_gid_type(struct cma_device *cma_dev,
228 unsigned int port,
229 enum ib_gid_type default_gid_type)
230{
231 unsigned long supported_gids;
232
233 if (port < rdma_start_port(cma_dev->device) ||
234 port > rdma_end_port(cma_dev->device))
235 return -EINVAL;
236
237 supported_gids = roce_gid_type_mask_support(cma_dev->device, port);
238
239 if (!(supported_gids & 1 << default_gid_type))
240 return -EINVAL;
241
242 cma_dev->default_gid_type[port - rdma_start_port(cma_dev->device)] =
243 default_gid_type;
244
245 return 0;
246}
247
248struct ib_device *cma_get_ib_dev(struct cma_device *cma_dev)
249{
250 return cma_dev->device;
251}
252
Sean Heftye51060f2006-06-17 20:37:29 -0700253/*
254 * Device removal can occur at anytime, so we need extra handling to
255 * serialize notifying the user of device removal with other callbacks.
256 * We do this by disabling removal notification while a callback is in process,
257 * and reporting it after the callback completes.
258 */
259struct rdma_id_private {
260 struct rdma_cm_id id;
261
262 struct rdma_bind_list *bind_list;
263 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700264 struct list_head list; /* listen_any_list or cma_device.list */
265 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700266 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800267 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700268
Sean Heftyd02d1f52007-10-09 11:12:34 -0700269 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700270 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700271 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700272 struct mutex qp_mutex;
273
Sean Heftye51060f2006-06-17 20:37:29 -0700274 struct completion comp;
275 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700276 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700277
278 int backlog;
279 int timeout_ms;
280 struct ib_sa_query *query;
281 int query_id;
282 union {
283 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500284 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700285 } cm_id;
286
287 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800288 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700289 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000290 pid_t owner;
Sean Hefty68602122012-06-14 20:31:39 +0000291 u32 options;
Sean Heftye51060f2006-06-17 20:37:29 -0700292 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700293 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700294 u8 reuseaddr;
Sean Hefty5b0ec992012-06-14 20:31:39 +0000295 u8 afonly;
Matan Barakcb57bb82015-12-23 14:56:48 +0200296 enum ib_gid_type gid_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700297};
298
Sean Heftyc8f6a362007-02-15 17:00:18 -0800299struct cma_multicast {
300 struct rdma_id_private *id_priv;
301 union {
302 struct ib_sa_multicast *ib;
303 } multicast;
304 struct list_head list;
305 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700306 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200307 struct kref mcref;
Moni Shouabee3c3c2015-12-23 14:56:57 +0200308 bool igmp_joined;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800309};
310
Sean Heftye51060f2006-06-17 20:37:29 -0700311struct cma_work {
312 struct work_struct work;
313 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700314 enum rdma_cm_state old_state;
315 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700316 struct rdma_cm_event event;
317};
318
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700319struct cma_ndev_work {
320 struct work_struct work;
321 struct rdma_id_private *id;
322 struct rdma_cm_event event;
323};
324
Eli Cohen3c86aa72010-10-13 21:26:51 +0200325struct iboe_mcast_work {
326 struct work_struct work;
327 struct rdma_id_private *id;
328 struct cma_multicast *mc;
329};
330
Sean Heftye51060f2006-06-17 20:37:29 -0700331union cma_ip_addr {
332 struct in6_addr ip6;
333 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000334 __be32 pad[3];
335 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700336 } ip4;
337};
338
339struct cma_hdr {
340 u8 cma_version;
341 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000342 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700343 union cma_ip_addr src_addr;
344 union cma_ip_addr dst_addr;
345};
346
Sean Heftye51060f2006-06-17 20:37:29 -0700347#define CMA_VERSION 0x00
Sean Heftye51060f2006-06-17 20:37:29 -0700348
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300349struct cma_req_info {
350 struct ib_device *device;
351 int port;
352 union ib_gid local_gid;
353 __be64 service_id;
354 u16 pkey;
355 bool has_gid:1;
356};
357
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700358static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700359{
360 unsigned long flags;
361 int ret;
362
363 spin_lock_irqsave(&id_priv->lock, flags);
364 ret = (id_priv->state == comp);
365 spin_unlock_irqrestore(&id_priv->lock, flags);
366 return ret;
367}
368
369static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700370 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700371{
372 unsigned long flags;
373 int ret;
374
375 spin_lock_irqsave(&id_priv->lock, flags);
376 if ((ret = (id_priv->state == comp)))
377 id_priv->state = exch;
378 spin_unlock_irqrestore(&id_priv->lock, flags);
379 return ret;
380}
381
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700382static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
383 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700384{
385 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700386 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700387
388 spin_lock_irqsave(&id_priv->lock, flags);
389 old = id_priv->state;
390 id_priv->state = exch;
391 spin_unlock_irqrestore(&id_priv->lock, flags);
392 return old;
393}
394
Haggai Eran4c21b5b2015-07-30 17:50:22 +0300395static inline u8 cma_get_ip_ver(const struct cma_hdr *hdr)
Sean Heftye51060f2006-06-17 20:37:29 -0700396{
397 return hdr->ip_version >> 4;
398}
399
400static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
401{
402 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
403}
404
Moni Shouabee3c3c2015-12-23 14:56:57 +0200405static int cma_igmp_send(struct net_device *ndev, union ib_gid *mgid, bool join)
Sean Heftye51060f2006-06-17 20:37:29 -0700406{
Moni Shouabee3c3c2015-12-23 14:56:57 +0200407 struct in_device *in_dev = NULL;
408
409 if (ndev) {
410 rtnl_lock();
411 in_dev = __in_dev_get_rtnl(ndev);
412 if (in_dev) {
413 if (join)
414 ip_mc_inc_group(in_dev,
415 *(__be32 *)(mgid->raw + 12));
416 else
417 ip_mc_dec_group(in_dev,
418 *(__be32 *)(mgid->raw + 12));
419 }
420 rtnl_unlock();
421 }
422 return (in_dev) ? 0 : -ENODEV;
423}
424
Matan Barak045959d2015-12-23 14:56:55 +0200425static void _cma_attach_to_dev(struct rdma_id_private *id_priv,
426 struct cma_device *cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700427{
Matan Barak218a7732015-12-23 14:56:54 +0200428 cma_ref_dev(cma_dev);
Sean Heftye51060f2006-06-17 20:37:29 -0700429 id_priv->cma_dev = cma_dev;
Matan Barak045959d2015-12-23 14:56:55 +0200430 id_priv->gid_type = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700431 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200432 id_priv->id.route.addr.dev_addr.transport =
433 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700434 list_add_tail(&id_priv->list, &cma_dev->id_list);
435}
436
Matan Barak045959d2015-12-23 14:56:55 +0200437static void cma_attach_to_dev(struct rdma_id_private *id_priv,
438 struct cma_device *cma_dev)
439{
440 _cma_attach_to_dev(id_priv, cma_dev);
441 id_priv->gid_type =
442 cma_dev->default_gid_type[id_priv->id.port_num -
443 rdma_start_port(cma_dev->device)];
444}
445
Matan Barak218a7732015-12-23 14:56:54 +0200446void cma_deref_dev(struct cma_device *cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700447{
448 if (atomic_dec_and_test(&cma_dev->refcount))
449 complete(&cma_dev->comp);
450}
451
Eli Cohen3c86aa72010-10-13 21:26:51 +0200452static inline void release_mc(struct kref *kref)
453{
454 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
455
456 kfree(mc->multicast.ib);
457 kfree(mc);
458}
459
Sean Heftya396d432011-02-23 09:05:39 -0800460static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700461{
Sean Heftya396d432011-02-23 09:05:39 -0800462 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700463 list_del(&id_priv->list);
464 cma_deref_dev(id_priv->cma_dev);
465 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800466 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700467}
468
Sean Heftyf4753832013-05-29 10:09:14 -0700469static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
470{
471 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
472}
473
474static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
475{
476 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
477}
478
479static inline unsigned short cma_family(struct rdma_id_private *id_priv)
480{
481 return id_priv->id.route.addr.src_addr.ss_family;
482}
483
Sean Hefty5c438132013-05-29 10:09:23 -0700484static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800485{
486 struct ib_sa_mcmember_rec rec;
487 int ret = 0;
488
Sean Hefty5c438132013-05-29 10:09:23 -0700489 if (id_priv->qkey) {
490 if (qkey && id_priv->qkey != qkey)
491 return -EINVAL;
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700492 return 0;
Sean Hefty5c438132013-05-29 10:09:23 -0700493 }
494
495 if (qkey) {
496 id_priv->qkey = qkey;
497 return 0;
498 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700499
500 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800501 case RDMA_PS_UDP:
Sean Hefty5c438132013-05-29 10:09:23 -0700502 case RDMA_PS_IB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700503 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800504 break;
505 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700506 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
507 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
508 id_priv->id.port_num, &rec.mgid,
509 &rec);
510 if (!ret)
511 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800512 break;
513 default:
514 break;
515 }
516 return ret;
517}
518
Sean Hefty680f9202013-05-29 10:09:12 -0700519static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
520{
521 dev_addr->dev_type = ARPHRD_INFINIBAND;
522 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
523 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
524}
525
526static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
527{
528 int ret;
529
530 if (addr->sa_family != AF_IB) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200531 ret = rdma_translate_ip(addr, dev_addr, NULL);
Sean Hefty680f9202013-05-29 10:09:12 -0700532 } else {
533 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
534 ret = 0;
535 }
536
537 return ret;
538}
539
Michael Wang7c111472015-05-05 14:50:30 +0200540static inline int cma_validate_port(struct ib_device *device, u8 port,
Matan Barak045959d2015-12-23 14:56:55 +0200541 enum ib_gid_type gid_type,
Matan Barakabae1b72015-10-15 18:38:49 +0300542 union ib_gid *gid, int dev_type,
543 int bound_if_index)
Michael Wang7c111472015-05-05 14:50:30 +0200544{
Michael Wang7c111472015-05-05 14:50:30 +0200545 int ret = -ENODEV;
Matan Barakabae1b72015-10-15 18:38:49 +0300546 struct net_device *ndev = NULL;
Michael Wang7c111472015-05-05 14:50:30 +0200547
548 if ((dev_type == ARPHRD_INFINIBAND) && !rdma_protocol_ib(device, port))
549 return ret;
550
551 if ((dev_type != ARPHRD_INFINIBAND) && rdma_protocol_ib(device, port))
552 return ret;
553
Matan Barak64936772016-01-07 11:19:29 +0200554 if (dev_type == ARPHRD_ETHER && rdma_protocol_roce(device, port)) {
Matan Barakabae1b72015-10-15 18:38:49 +0300555 ndev = dev_get_by_index(&init_net, bound_if_index);
Matan Barak20029832015-12-23 14:56:53 +0200556 if (ndev && ndev->flags & IFF_LOOPBACK) {
557 pr_info("detected loopback device\n");
558 dev_put(ndev);
Matan Barakabae1b72015-10-15 18:38:49 +0300559
Matan Barak20029832015-12-23 14:56:53 +0200560 if (!device->get_netdev)
561 return -EOPNOTSUPP;
562
563 ndev = device->get_netdev(device, port);
564 if (!ndev)
565 return -ENODEV;
566 }
Matan Barak045959d2015-12-23 14:56:55 +0200567 } else {
568 gid_type = IB_GID_TYPE_IB;
Matan Barak20029832015-12-23 14:56:53 +0200569 }
Matan Barakabae1b72015-10-15 18:38:49 +0300570
Matan Barak045959d2015-12-23 14:56:55 +0200571 ret = ib_find_cached_gid_by_port(device, gid, gid_type, port,
Matan Barakb39ffa12015-12-23 14:56:47 +0200572 ndev, NULL);
Matan Barakabae1b72015-10-15 18:38:49 +0300573
574 if (ndev)
575 dev_put(ndev);
Michael Wang7c111472015-05-05 14:50:30 +0200576
577 return ret;
578}
579
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400580static int cma_acquire_dev(struct rdma_id_private *id_priv,
581 struct rdma_id_private *listen_id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700582{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800583 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700584 struct cma_device *cma_dev;
Michael Wang7c111472015-05-05 14:50:30 +0200585 union ib_gid gid, iboe_gid, *gidp;
Sean Heftye51060f2006-06-17 20:37:29 -0700586 int ret = -ENODEV;
Michael Wang7c111472015-05-05 14:50:30 +0200587 u8 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700588
Michael Wang7c111472015-05-05 14:50:30 +0200589 if (dev_addr->dev_type != ARPHRD_INFINIBAND &&
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000590 id_priv->id.ps == RDMA_PS_IPOIB)
591 return -EINVAL;
592
Sean Heftya396d432011-02-23 09:05:39 -0800593 mutex_lock(&lock);
Moni Shoua7b856272013-12-12 18:03:12 +0200594 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
595 &iboe_gid);
596
Eli Cohen3c86aa72010-10-13 21:26:51 +0200597 memcpy(&gid, dev_addr->src_dev_addr +
598 rdma_addr_gid_offset(dev_addr), sizeof gid);
Michael Wang7c111472015-05-05 14:50:30 +0200599
600 if (listen_id_priv) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400601 cma_dev = listen_id_priv->cma_dev;
602 port = listen_id_priv->id.port_num;
Ira Weiny5d9fb042015-05-14 15:01:46 -0400603 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200604 &iboe_gid : &gid;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400605
Matan Barak045959d2015-12-23 14:56:55 +0200606 ret = cma_validate_port(cma_dev->device, port,
607 rdma_protocol_ib(cma_dev->device, port) ?
608 IB_GID_TYPE_IB :
609 listen_id_priv->gid_type, gidp,
Matan Barakabae1b72015-10-15 18:38:49 +0300610 dev_addr->dev_type,
611 dev_addr->bound_dev_if);
Michael Wang7c111472015-05-05 14:50:30 +0200612 if (!ret) {
613 id_priv->id.port_num = port;
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400614 goto out;
615 }
616 }
Michael Wang7c111472015-05-05 14:50:30 +0200617
Sean Heftye51060f2006-06-17 20:37:29 -0700618 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200619 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400620 if (listen_id_priv &&
621 listen_id_priv->cma_dev == cma_dev &&
622 listen_id_priv->id.port_num == port)
623 continue;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200624
Ira Weiny5d9fb042015-05-14 15:01:46 -0400625 gidp = rdma_protocol_roce(cma_dev->device, port) ?
Michael Wang7c111472015-05-05 14:50:30 +0200626 &iboe_gid : &gid;
627
Matan Barak045959d2015-12-23 14:56:55 +0200628 ret = cma_validate_port(cma_dev->device, port,
629 rdma_protocol_ib(cma_dev->device, port) ?
630 IB_GID_TYPE_IB :
631 cma_dev->default_gid_type[port - 1],
632 gidp, dev_addr->dev_type,
Matan Barakabae1b72015-10-15 18:38:49 +0300633 dev_addr->bound_dev_if);
Michael Wang7c111472015-05-05 14:50:30 +0200634 if (!ret) {
635 id_priv->id.port_num = port;
636 goto out;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200637 }
Sean Heftye51060f2006-06-17 20:37:29 -0700638 }
639 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200640
641out:
642 if (!ret)
643 cma_attach_to_dev(id_priv, cma_dev);
644
Sean Heftya396d432011-02-23 09:05:39 -0800645 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700646 return ret;
647}
648
Sean Heftyf17df3b2013-05-29 10:09:17 -0700649/*
650 * Select the source IB device and address to reach the destination IB address.
651 */
652static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
653{
654 struct cma_device *cma_dev, *cur_dev;
655 struct sockaddr_ib *addr;
656 union ib_gid gid, sgid, *dgid;
657 u16 pkey, index;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700658 u8 p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700659 int i;
660
661 cma_dev = NULL;
662 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
663 dgid = (union ib_gid *) &addr->sib_addr;
664 pkey = ntohs(addr->sib_pkey);
665
666 list_for_each_entry(cur_dev, &dev_list, list) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700667 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
Michael Wang30a74ef2015-05-05 14:50:39 +0200668 if (!rdma_cap_af_ib(cur_dev->device, p))
Michael Wangfef60902015-05-05 14:50:31 +0200669 continue;
670
Sean Heftyf17df3b2013-05-29 10:09:17 -0700671 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
672 continue;
673
Matan Barak55ee3ab2015-10-15 18:38:45 +0300674 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i,
675 &gid, NULL);
676 i++) {
Sean Heftyf17df3b2013-05-29 10:09:17 -0700677 if (!memcmp(&gid, dgid, sizeof(gid))) {
678 cma_dev = cur_dev;
679 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700680 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700681 goto found;
682 }
683
684 if (!cma_dev && (gid.global.subnet_prefix ==
685 dgid->global.subnet_prefix)) {
686 cma_dev = cur_dev;
687 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700688 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700689 }
690 }
691 }
692 }
693
694 if (!cma_dev)
695 return -ENODEV;
696
697found:
698 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf17df3b2013-05-29 10:09:17 -0700699 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
700 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
701 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
702 return 0;
703}
704
Sean Heftye51060f2006-06-17 20:37:29 -0700705static void cma_deref_id(struct rdma_id_private *id_priv)
706{
707 if (atomic_dec_and_test(&id_priv->refcount))
708 complete(&id_priv->comp);
709}
710
Or Gerlitzde910bd2008-07-14 23:48:53 -0700711static int cma_disable_callback(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700712 enum rdma_cm_state state)
Sean Hefty8aa08602007-05-07 11:49:00 -0700713{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700714 mutex_lock(&id_priv->handler_mutex);
715 if (id_priv->state != state) {
716 mutex_unlock(&id_priv->handler_mutex);
717 return -EINVAL;
718 }
719 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700720}
721
Guy Shapirofa201052015-10-22 15:20:10 +0300722struct rdma_cm_id *rdma_create_id(struct net *net,
723 rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000724 void *context, enum rdma_port_space ps,
725 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700726{
727 struct rdma_id_private *id_priv;
728
729 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
730 if (!id_priv)
731 return ERR_PTR(-ENOMEM);
732
Nir Muchtar83e95022011-01-13 13:56:04 +0000733 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700734 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700735 id_priv->id.context = context;
736 id_priv->id.event_handler = event_handler;
737 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000738 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700739 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700740 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700741 init_completion(&id_priv->comp);
742 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700743 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700744 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800745 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700746 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
Guy Shapirofa201052015-10-22 15:20:10 +0300747 id_priv->id.route.addr.dev_addr.net = get_net(net);
Sean Heftye51060f2006-06-17 20:37:29 -0700748
749 return &id_priv->id;
750}
751EXPORT_SYMBOL(rdma_create_id);
752
Sean Heftyc8f6a362007-02-15 17:00:18 -0800753static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700754{
755 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800756 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700757
Sean Heftyc8f6a362007-02-15 17:00:18 -0800758 qp_attr.qp_state = IB_QPS_INIT;
759 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700760 if (ret)
761 return ret;
762
Sean Heftyc8f6a362007-02-15 17:00:18 -0800763 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
764 if (ret)
765 return ret;
766
767 qp_attr.qp_state = IB_QPS_RTR;
768 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
769 if (ret)
770 return ret;
771
772 qp_attr.qp_state = IB_QPS_RTS;
773 qp_attr.sq_psn = 0;
774 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
775
776 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700777}
778
Sean Heftyc8f6a362007-02-15 17:00:18 -0800779static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500780{
781 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800782 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500783
784 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800785 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
786 if (ret)
787 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500788
Sean Heftyc8f6a362007-02-15 17:00:18 -0800789 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500790}
791
Sean Heftye51060f2006-06-17 20:37:29 -0700792int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
793 struct ib_qp_init_attr *qp_init_attr)
794{
795 struct rdma_id_private *id_priv;
796 struct ib_qp *qp;
797 int ret;
798
799 id_priv = container_of(id, struct rdma_id_private, id);
800 if (id->device != pd->device)
801 return -EINVAL;
802
Christoph Hellwig0691a282016-05-03 18:01:03 +0200803 qp_init_attr->port_num = id->port_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700804 qp = ib_create_qp(pd, qp_init_attr);
805 if (IS_ERR(qp))
806 return PTR_ERR(qp);
807
Sean Heftyb26f9b92010-04-01 17:08:41 +0000808 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800809 ret = cma_init_ud_qp(id_priv, qp);
810 else
811 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700812 if (ret)
813 goto err;
814
815 id->qp = qp;
816 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700817 id_priv->srq = (qp->srq != NULL);
818 return 0;
819err:
820 ib_destroy_qp(qp);
821 return ret;
822}
823EXPORT_SYMBOL(rdma_create_qp);
824
825void rdma_destroy_qp(struct rdma_cm_id *id)
826{
Sean Heftyc5483382007-09-24 13:19:09 -0700827 struct rdma_id_private *id_priv;
828
829 id_priv = container_of(id, struct rdma_id_private, id);
830 mutex_lock(&id_priv->qp_mutex);
831 ib_destroy_qp(id_priv->id.qp);
832 id_priv->id.qp = NULL;
833 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700834}
835EXPORT_SYMBOL(rdma_destroy_qp);
836
Sean Hefty5851bb82008-01-04 10:47:12 -0800837static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
838 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700839{
840 struct ib_qp_attr qp_attr;
841 int qp_attr_mask, ret;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200842 union ib_gid sgid;
Sean Heftye51060f2006-06-17 20:37:29 -0700843
Sean Heftyc5483382007-09-24 13:19:09 -0700844 mutex_lock(&id_priv->qp_mutex);
845 if (!id_priv->id.qp) {
846 ret = 0;
847 goto out;
848 }
Sean Heftye51060f2006-06-17 20:37:29 -0700849
850 /* Need to update QP attributes from default values. */
851 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700852 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700853 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700854 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700855
Sean Heftyc5483382007-09-24 13:19:09 -0700856 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700857 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700858 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700859
860 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700861 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700862 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700863 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700864
Matan Barakdd5f03b2013-12-12 18:03:11 +0200865 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
Matan Barak55ee3ab2015-10-15 18:38:45 +0300866 qp_attr.ah_attr.grh.sgid_index, &sgid, NULL);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200867 if (ret)
868 goto out;
869
Michael Wangfef60902015-05-05 14:50:31 +0200870 BUG_ON(id_priv->cma_dev->device != id_priv->id.device);
871
Sean Hefty5851bb82008-01-04 10:47:12 -0800872 if (conn_param)
873 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700874 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
875out:
876 mutex_unlock(&id_priv->qp_mutex);
877 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700878}
879
Sean Hefty5851bb82008-01-04 10:47:12 -0800880static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
881 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700882{
883 struct ib_qp_attr qp_attr;
884 int qp_attr_mask, ret;
885
Sean Heftyc5483382007-09-24 13:19:09 -0700886 mutex_lock(&id_priv->qp_mutex);
887 if (!id_priv->id.qp) {
888 ret = 0;
889 goto out;
890 }
Sean Heftye51060f2006-06-17 20:37:29 -0700891
892 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700893 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700894 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700895 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700896
Sean Hefty5851bb82008-01-04 10:47:12 -0800897 if (conn_param)
898 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700899 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
900out:
901 mutex_unlock(&id_priv->qp_mutex);
902 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700903}
904
Sean Heftyc5483382007-09-24 13:19:09 -0700905static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700906{
907 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700908 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700909
Sean Heftyc5483382007-09-24 13:19:09 -0700910 mutex_lock(&id_priv->qp_mutex);
911 if (!id_priv->id.qp) {
912 ret = 0;
913 goto out;
914 }
Sean Heftye51060f2006-06-17 20:37:29 -0700915
916 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700917 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
918out:
919 mutex_unlock(&id_priv->qp_mutex);
920 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700921}
922
Sean Heftyc8f6a362007-02-15 17:00:18 -0800923static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
924 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
925{
926 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
927 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200928 u16 pkey;
929
Michael Wang227128f2015-05-05 14:50:40 +0200930 if (rdma_cap_eth_ah(id_priv->id.device, id_priv->id.port_num))
Eli Cohen3c86aa72010-10-13 21:26:51 +0200931 pkey = 0xffff;
Michael Wangfef60902015-05-05 14:50:31 +0200932 else
933 pkey = ib_addr_get_pkey(dev_addr);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800934
935 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200936 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800937 if (ret)
938 return ret;
939
940 qp_attr->port_num = id_priv->id.port_num;
941 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
942
Sean Heftyb26f9b92010-04-01 17:08:41 +0000943 if (id_priv->id.qp_type == IB_QPT_UD) {
Sean Hefty5c438132013-05-29 10:09:23 -0700944 ret = cma_set_qkey(id_priv, 0);
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700945 if (ret)
946 return ret;
947
Sean Heftyc8f6a362007-02-15 17:00:18 -0800948 qp_attr->qkey = id_priv->qkey;
949 *qp_attr_mask |= IB_QP_QKEY;
950 } else {
951 qp_attr->qp_access_flags = 0;
952 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
953 }
954 return 0;
955}
956
Sean Heftye51060f2006-06-17 20:37:29 -0700957int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
958 int *qp_attr_mask)
959{
960 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800961 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700962
963 id_priv = container_of(id, struct rdma_id_private, id);
Michael Wang72219cea2015-05-05 14:50:34 +0200964 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +0000965 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800966 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
967 else
968 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
969 qp_attr_mask);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200970
Sean Heftye51060f2006-06-17 20:37:29 -0700971 if (qp_attr->qp_state == IB_QPS_RTR)
972 qp_attr->rq_psn = id_priv->seq_num;
Michael Wang04215332015-05-05 14:50:35 +0200973 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800974 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300975 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800976 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
977 } else
978 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
979 qp_attr_mask);
Michael Wang21655af2015-05-05 14:50:27 +0200980 } else
Sean Heftye51060f2006-06-17 20:37:29 -0700981 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -0700982
983 return ret;
984}
985EXPORT_SYMBOL(rdma_init_qp_attr);
986
987static inline int cma_zero_addr(struct sockaddr *addr)
988{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700989 switch (addr->sa_family) {
990 case AF_INET:
991 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
992 case AF_INET6:
993 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
994 case AF_IB:
995 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
996 default:
997 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700998 }
999}
1000
1001static inline int cma_loopback_addr(struct sockaddr *addr)
1002{
Sean Hefty2e2d1902013-05-29 10:09:09 -07001003 switch (addr->sa_family) {
1004 case AF_INET:
1005 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
1006 case AF_INET6:
1007 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
1008 case AF_IB:
1009 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
1010 default:
1011 return 0;
1012 }
Sean Heftye51060f2006-06-17 20:37:29 -07001013}
1014
1015static inline int cma_any_addr(struct sockaddr *addr)
1016{
1017 return cma_zero_addr(addr) || cma_loopback_addr(addr);
1018}
1019
Hefty, Sean43b752d2011-05-09 22:06:10 -07001020static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
1021{
1022 if (src->sa_family != dst->sa_family)
1023 return -1;
1024
1025 switch (src->sa_family) {
1026 case AF_INET:
1027 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
1028 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
Sean Hefty2e2d1902013-05-29 10:09:09 -07001029 case AF_INET6:
Hefty, Sean43b752d2011-05-09 22:06:10 -07001030 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
1031 &((struct sockaddr_in6 *) dst)->sin6_addr);
Sean Hefty2e2d1902013-05-29 10:09:09 -07001032 default:
1033 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
1034 &((struct sockaddr_ib *) dst)->sib_addr);
Hefty, Sean43b752d2011-05-09 22:06:10 -07001035 }
1036}
1037
Sean Hefty58afdcb2013-05-29 10:09:11 -07001038static __be16 cma_port(struct sockaddr *addr)
Sean Hefty628e5f62006-11-30 16:44:16 -08001039{
Sean Hefty58afdcb2013-05-29 10:09:11 -07001040 struct sockaddr_ib *sib;
1041
1042 switch (addr->sa_family) {
1043 case AF_INET:
Sean Hefty628e5f62006-11-30 16:44:16 -08001044 return ((struct sockaddr_in *) addr)->sin_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -07001045 case AF_INET6:
Sean Hefty628e5f62006-11-30 16:44:16 -08001046 return ((struct sockaddr_in6 *) addr)->sin6_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -07001047 case AF_IB:
1048 sib = (struct sockaddr_ib *) addr;
1049 return htons((u16) (be64_to_cpu(sib->sib_sid) &
1050 be64_to_cpu(sib->sib_sid_mask)));
1051 default:
1052 return 0;
1053 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001054}
1055
Sean Heftye51060f2006-06-17 20:37:29 -07001056static inline int cma_any_port(struct sockaddr *addr)
1057{
Sean Hefty628e5f62006-11-30 16:44:16 -08001058 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -07001059}
1060
Haggai Eran0c505f72015-07-30 17:50:19 +03001061static void cma_save_ib_info(struct sockaddr *src_addr,
1062 struct sockaddr *dst_addr,
1063 struct rdma_cm_id *listen_id,
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001064 struct ib_sa_path_rec *path)
Sean Heftye51060f2006-06-17 20:37:29 -07001065{
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001066 struct sockaddr_ib *listen_ib, *ib;
Sean Heftye51060f2006-06-17 20:37:29 -07001067
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001068 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001069 if (src_addr) {
1070 ib = (struct sockaddr_ib *)src_addr;
1071 ib->sib_family = AF_IB;
1072 if (path) {
1073 ib->sib_pkey = path->pkey;
1074 ib->sib_flowinfo = path->flow_label;
1075 memcpy(&ib->sib_addr, &path->sgid, 16);
1076 ib->sib_sid = path->service_id;
1077 ib->sib_scope_id = 0;
1078 } else {
1079 ib->sib_pkey = listen_ib->sib_pkey;
1080 ib->sib_flowinfo = listen_ib->sib_flowinfo;
1081 ib->sib_addr = listen_ib->sib_addr;
1082 ib->sib_sid = listen_ib->sib_sid;
1083 ib->sib_scope_id = listen_ib->sib_scope_id;
1084 }
1085 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
Matthew Finlayc07678b2015-05-19 00:11:48 -07001086 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001087 if (dst_addr) {
1088 ib = (struct sockaddr_ib *)dst_addr;
1089 ib->sib_family = AF_IB;
1090 if (path) {
1091 ib->sib_pkey = path->pkey;
1092 ib->sib_flowinfo = path->flow_label;
1093 memcpy(&ib->sib_addr, &path->dgid, 16);
1094 }
Matthew Finlayc07678b2015-05-19 00:11:48 -07001095 }
Sean Heftye51060f2006-06-17 20:37:29 -07001096}
1097
Haggai Eran0c505f72015-07-30 17:50:19 +03001098static void cma_save_ip4_info(struct sockaddr *src_addr,
1099 struct sockaddr *dst_addr,
1100 struct cma_hdr *hdr,
1101 __be16 local_port)
Sean Heftye51060f2006-06-17 20:37:29 -07001102{
Jason Gunthorpe28521442015-04-20 14:01:11 -06001103 struct sockaddr_in *ip4;
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001104
Haggai Eran0c505f72015-07-30 17:50:19 +03001105 if (src_addr) {
1106 ip4 = (struct sockaddr_in *)src_addr;
1107 ip4->sin_family = AF_INET;
1108 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr;
1109 ip4->sin_port = local_port;
1110 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001111
Haggai Eran0c505f72015-07-30 17:50:19 +03001112 if (dst_addr) {
1113 ip4 = (struct sockaddr_in *)dst_addr;
1114 ip4->sin_family = AF_INET;
1115 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr;
1116 ip4->sin_port = hdr->port;
1117 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001118}
1119
Haggai Eran0c505f72015-07-30 17:50:19 +03001120static void cma_save_ip6_info(struct sockaddr *src_addr,
1121 struct sockaddr *dst_addr,
1122 struct cma_hdr *hdr,
1123 __be16 local_port)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001124{
Jason Gunthorpe28521442015-04-20 14:01:11 -06001125 struct sockaddr_in6 *ip6;
Sean Heftye51060f2006-06-17 20:37:29 -07001126
Haggai Eran0c505f72015-07-30 17:50:19 +03001127 if (src_addr) {
1128 ip6 = (struct sockaddr_in6 *)src_addr;
1129 ip6->sin6_family = AF_INET6;
1130 ip6->sin6_addr = hdr->dst_addr.ip6;
1131 ip6->sin6_port = local_port;
1132 }
Sean Heftye51060f2006-06-17 20:37:29 -07001133
Haggai Eran0c505f72015-07-30 17:50:19 +03001134 if (dst_addr) {
1135 ip6 = (struct sockaddr_in6 *)dst_addr;
1136 ip6->sin6_family = AF_INET6;
1137 ip6->sin6_addr = hdr->src_addr.ip6;
1138 ip6->sin6_port = hdr->port;
1139 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001140}
1141
Haggai Eran0c505f72015-07-30 17:50:19 +03001142static u16 cma_port_from_service_id(__be64 service_id)
1143{
1144 return (u16)be64_to_cpu(service_id);
1145}
1146
1147static int cma_save_ip_info(struct sockaddr *src_addr,
1148 struct sockaddr *dst_addr,
1149 struct ib_cm_event *ib_event,
1150 __be64 service_id)
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001151{
1152 struct cma_hdr *hdr;
Haggai Eran0c505f72015-07-30 17:50:19 +03001153 __be16 port;
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001154
1155 hdr = ib_event->private_data;
1156 if (hdr->cma_version != CMA_VERSION)
1157 return -EINVAL;
1158
Haggai Eran0c505f72015-07-30 17:50:19 +03001159 port = htons(cma_port_from_service_id(service_id));
1160
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001161 switch (cma_get_ip_ver(hdr)) {
1162 case 4:
Haggai Eran0c505f72015-07-30 17:50:19 +03001163 cma_save_ip4_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001164 break;
1165 case 6:
Haggai Eran0c505f72015-07-30 17:50:19 +03001166 cma_save_ip6_info(src_addr, dst_addr, hdr, port);
Sean Heftye51060f2006-06-17 20:37:29 -07001167 break;
1168 default:
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001169 return -EAFNOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07001170 }
Haggai Eran0c505f72015-07-30 17:50:19 +03001171
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001172 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001173}
1174
Haggai Eran0c505f72015-07-30 17:50:19 +03001175static int cma_save_net_info(struct sockaddr *src_addr,
1176 struct sockaddr *dst_addr,
1177 struct rdma_cm_id *listen_id,
1178 struct ib_cm_event *ib_event,
1179 sa_family_t sa_family, __be64 service_id)
1180{
1181 if (sa_family == AF_IB) {
1182 if (ib_event->event == IB_CM_REQ_RECEIVED)
1183 cma_save_ib_info(src_addr, dst_addr, listen_id,
1184 ib_event->param.req_rcvd.primary_path);
1185 else if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED)
1186 cma_save_ib_info(src_addr, dst_addr, listen_id, NULL);
1187 return 0;
1188 }
1189
1190 return cma_save_ip_info(src_addr, dst_addr, ib_event, service_id);
1191}
1192
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001193static int cma_save_req_info(const struct ib_cm_event *ib_event,
1194 struct cma_req_info *req)
1195{
1196 const struct ib_cm_req_event_param *req_param =
1197 &ib_event->param.req_rcvd;
1198 const struct ib_cm_sidr_req_event_param *sidr_param =
1199 &ib_event->param.sidr_req_rcvd;
1200
1201 switch (ib_event->event) {
1202 case IB_CM_REQ_RECEIVED:
1203 req->device = req_param->listen_id->device;
1204 req->port = req_param->port;
1205 memcpy(&req->local_gid, &req_param->primary_path->sgid,
1206 sizeof(req->local_gid));
1207 req->has_gid = true;
1208 req->service_id = req_param->primary_path->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001209 req->pkey = be16_to_cpu(req_param->primary_path->pkey);
Haggai Eran84424a72016-02-29 15:45:12 +02001210 if (req->pkey != req_param->bth_pkey)
1211 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and primary path P_Key (0x%x)\n"
1212 "RDMA CMA: in the future this may cause the request to be dropped\n",
1213 req_param->bth_pkey, req->pkey);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001214 break;
1215 case IB_CM_SIDR_REQ_RECEIVED:
1216 req->device = sidr_param->listen_id->device;
1217 req->port = sidr_param->port;
1218 req->has_gid = false;
1219 req->service_id = sidr_param->service_id;
Haggai Eranab3964a2015-10-20 09:53:01 +03001220 req->pkey = sidr_param->pkey;
Haggai Eran84424a72016-02-29 15:45:12 +02001221 if (req->pkey != sidr_param->bth_pkey)
1222 pr_warn_ratelimited("RDMA CMA: got different BTH P_Key (0x%x) and SIDR request payload P_Key (0x%x)\n"
1223 "RDMA CMA: in the future this may cause the request to be dropped\n",
1224 sidr_param->bth_pkey, req->pkey);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001225 break;
1226 default:
1227 return -EINVAL;
1228 }
1229
1230 return 0;
1231}
1232
Haggai Eranf887f2a2015-07-30 17:50:23 +03001233static bool validate_ipv4_net_dev(struct net_device *net_dev,
1234 const struct sockaddr_in *dst_addr,
1235 const struct sockaddr_in *src_addr)
1236{
1237 __be32 daddr = dst_addr->sin_addr.s_addr,
1238 saddr = src_addr->sin_addr.s_addr;
1239 struct fib_result res;
1240 struct flowi4 fl4;
1241 int err;
1242 bool ret;
1243
1244 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
1245 ipv4_is_lbcast(daddr) || ipv4_is_zeronet(saddr) ||
1246 ipv4_is_zeronet(daddr) || ipv4_is_loopback(daddr) ||
1247 ipv4_is_loopback(saddr))
1248 return false;
1249
1250 memset(&fl4, 0, sizeof(fl4));
1251 fl4.flowi4_iif = net_dev->ifindex;
1252 fl4.daddr = daddr;
1253 fl4.saddr = saddr;
1254
1255 rcu_read_lock();
1256 err = fib_lookup(dev_net(net_dev), &fl4, &res, 0);
Bart Van Assched3632492015-11-20 11:04:12 -08001257 ret = err == 0 && FIB_RES_DEV(res) == net_dev;
Haggai Eranf887f2a2015-07-30 17:50:23 +03001258 rcu_read_unlock();
1259
1260 return ret;
1261}
1262
1263static bool validate_ipv6_net_dev(struct net_device *net_dev,
1264 const struct sockaddr_in6 *dst_addr,
1265 const struct sockaddr_in6 *src_addr)
1266{
1267#if IS_ENABLED(CONFIG_IPV6)
1268 const int strict = ipv6_addr_type(&dst_addr->sin6_addr) &
1269 IPV6_ADDR_LINKLOCAL;
1270 struct rt6_info *rt = rt6_lookup(dev_net(net_dev), &dst_addr->sin6_addr,
1271 &src_addr->sin6_addr, net_dev->ifindex,
1272 strict);
1273 bool ret;
1274
1275 if (!rt)
1276 return false;
1277
1278 ret = rt->rt6i_idev->dev == net_dev;
1279 ip6_rt_put(rt);
1280
1281 return ret;
1282#else
1283 return false;
1284#endif
1285}
1286
1287static bool validate_net_dev(struct net_device *net_dev,
1288 const struct sockaddr *daddr,
1289 const struct sockaddr *saddr)
1290{
1291 const struct sockaddr_in *daddr4 = (const struct sockaddr_in *)daddr;
1292 const struct sockaddr_in *saddr4 = (const struct sockaddr_in *)saddr;
1293 const struct sockaddr_in6 *daddr6 = (const struct sockaddr_in6 *)daddr;
1294 const struct sockaddr_in6 *saddr6 = (const struct sockaddr_in6 *)saddr;
1295
1296 switch (daddr->sa_family) {
1297 case AF_INET:
1298 return saddr->sa_family == AF_INET &&
1299 validate_ipv4_net_dev(net_dev, daddr4, saddr4);
1300
1301 case AF_INET6:
1302 return saddr->sa_family == AF_INET6 &&
1303 validate_ipv6_net_dev(net_dev, daddr6, saddr6);
1304
1305 default:
1306 return false;
1307 }
1308}
1309
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001310static struct net_device *cma_get_net_dev(struct ib_cm_event *ib_event,
1311 const struct cma_req_info *req)
1312{
Haggai Eranf887f2a2015-07-30 17:50:23 +03001313 struct sockaddr_storage listen_addr_storage, src_addr_storage;
1314 struct sockaddr *listen_addr = (struct sockaddr *)&listen_addr_storage,
1315 *src_addr = (struct sockaddr *)&src_addr_storage;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001316 struct net_device *net_dev;
1317 const union ib_gid *gid = req->has_gid ? &req->local_gid : NULL;
1318 int err;
1319
Haggai Eranf887f2a2015-07-30 17:50:23 +03001320 err = cma_save_ip_info(listen_addr, src_addr, ib_event,
1321 req->service_id);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001322 if (err)
1323 return ERR_PTR(err);
1324
1325 net_dev = ib_get_net_dev_by_params(req->device, req->port, req->pkey,
1326 gid, listen_addr);
1327 if (!net_dev)
1328 return ERR_PTR(-ENODEV);
1329
Haggai Eranf887f2a2015-07-30 17:50:23 +03001330 if (!validate_net_dev(net_dev, listen_addr, src_addr)) {
1331 dev_put(net_dev);
1332 return ERR_PTR(-EHOSTUNREACH);
1333 }
1334
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001335 return net_dev;
1336}
1337
1338static enum rdma_port_space rdma_ps_from_service_id(__be64 service_id)
1339{
1340 return (be64_to_cpu(service_id) >> 16) & 0xffff;
1341}
1342
1343static bool cma_match_private_data(struct rdma_id_private *id_priv,
1344 const struct cma_hdr *hdr)
1345{
1346 struct sockaddr *addr = cma_src_addr(id_priv);
1347 __be32 ip4_addr;
1348 struct in6_addr ip6_addr;
1349
1350 if (cma_any_addr(addr) && !id_priv->afonly)
1351 return true;
1352
1353 switch (addr->sa_family) {
1354 case AF_INET:
1355 ip4_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
1356 if (cma_get_ip_ver(hdr) != 4)
1357 return false;
1358 if (!cma_any_addr(addr) &&
1359 hdr->dst_addr.ip4.addr != ip4_addr)
1360 return false;
1361 break;
1362 case AF_INET6:
1363 ip6_addr = ((struct sockaddr_in6 *)addr)->sin6_addr;
1364 if (cma_get_ip_ver(hdr) != 6)
1365 return false;
1366 if (!cma_any_addr(addr) &&
1367 memcmp(&hdr->dst_addr.ip6, &ip6_addr, sizeof(ip6_addr)))
1368 return false;
1369 break;
1370 case AF_IB:
1371 return true;
1372 default:
1373 return false;
1374 }
1375
1376 return true;
1377}
1378
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001379static bool cma_protocol_roce_dev_port(struct ib_device *device, int port_num)
1380{
1381 enum rdma_link_layer ll = rdma_port_get_link_layer(device, port_num);
1382 enum rdma_transport_type transport =
1383 rdma_node_get_transport(device->node_type);
1384
1385 return ll == IB_LINK_LAYER_ETHERNET && transport == RDMA_TRANSPORT_IB;
1386}
1387
1388static bool cma_protocol_roce(const struct rdma_cm_id *id)
1389{
1390 struct ib_device *device = id->device;
1391 const int port_num = id->port_num ?: rdma_start_port(device);
1392
1393 return cma_protocol_roce_dev_port(device, port_num);
1394}
1395
Matan Barakfac51592015-12-21 17:01:24 +02001396static bool cma_match_net_dev(const struct rdma_cm_id *id,
1397 const struct net_device *net_dev,
1398 u8 port_num)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001399{
Matan Barakfac51592015-12-21 17:01:24 +02001400 const struct rdma_addr *addr = &id->route.addr;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001401
1402 if (!net_dev)
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001403 /* This request is an AF_IB request or a RoCE request */
Matan Barakfac51592015-12-21 17:01:24 +02001404 return (!id->port_num || id->port_num == port_num) &&
1405 (addr->src_addr.ss_family == AF_IB ||
1406 cma_protocol_roce_dev_port(id->device, port_num));
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001407
1408 return !addr->dev_addr.bound_dev_if ||
Guy Shapirofa201052015-10-22 15:20:10 +03001409 (net_eq(dev_net(net_dev), addr->dev_addr.net) &&
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001410 addr->dev_addr.bound_dev_if == net_dev->ifindex);
1411}
1412
1413static struct rdma_id_private *cma_find_listener(
1414 const struct rdma_bind_list *bind_list,
1415 const struct ib_cm_id *cm_id,
1416 const struct ib_cm_event *ib_event,
1417 const struct cma_req_info *req,
1418 const struct net_device *net_dev)
1419{
1420 struct rdma_id_private *id_priv, *id_priv_dev;
1421
1422 if (!bind_list)
1423 return ERR_PTR(-EINVAL);
1424
1425 hlist_for_each_entry(id_priv, &bind_list->owners, node) {
1426 if (cma_match_private_data(id_priv, ib_event->private_data)) {
1427 if (id_priv->id.device == cm_id->device &&
Matan Barakfac51592015-12-21 17:01:24 +02001428 cma_match_net_dev(&id_priv->id, net_dev, req->port))
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001429 return id_priv;
1430 list_for_each_entry(id_priv_dev,
1431 &id_priv->listen_list,
1432 listen_list) {
1433 if (id_priv_dev->id.device == cm_id->device &&
Matan Barakfac51592015-12-21 17:01:24 +02001434 cma_match_net_dev(&id_priv_dev->id, net_dev, req->port))
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001435 return id_priv_dev;
1436 }
1437 }
1438 }
1439
1440 return ERR_PTR(-EINVAL);
1441}
1442
1443static struct rdma_id_private *cma_id_from_event(struct ib_cm_id *cm_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001444 struct ib_cm_event *ib_event,
1445 struct net_device **net_dev)
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001446{
1447 struct cma_req_info req;
1448 struct rdma_bind_list *bind_list;
1449 struct rdma_id_private *id_priv;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001450 int err;
1451
1452 err = cma_save_req_info(ib_event, &req);
1453 if (err)
1454 return ERR_PTR(err);
1455
Haggai Eran0b3ca762015-07-30 17:50:24 +03001456 *net_dev = cma_get_net_dev(ib_event, &req);
1457 if (IS_ERR(*net_dev)) {
1458 if (PTR_ERR(*net_dev) == -EAFNOSUPPORT) {
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001459 /* Assuming the protocol is AF_IB */
Haggai Eran0b3ca762015-07-30 17:50:24 +03001460 *net_dev = NULL;
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001461 } else if (cma_protocol_roce_dev_port(req.device, req.port)) {
1462 /* TODO find the net dev matching the request parameters
1463 * through the RoCE GID table */
1464 *net_dev = NULL;
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001465 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001466 return ERR_CAST(*net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001467 }
1468 }
1469
Guy Shapirofa201052015-10-22 15:20:10 +03001470 bind_list = cma_ps_find(*net_dev ? dev_net(*net_dev) : &init_net,
Haggai Eran4be74b42015-10-22 15:20:09 +03001471 rdma_ps_from_service_id(req.service_id),
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001472 cma_port_from_service_id(req.service_id));
Haggai Eran0b3ca762015-07-30 17:50:24 +03001473 id_priv = cma_find_listener(bind_list, cm_id, ib_event, &req, *net_dev);
Haggai Eranb3b51f92015-09-21 16:02:02 +03001474 if (IS_ERR(id_priv) && *net_dev) {
Haggai Eranbe688192015-08-27 15:55:15 +03001475 dev_put(*net_dev);
1476 *net_dev = NULL;
1477 }
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001478
1479 return id_priv;
1480}
1481
Sean Heftye8160e12013-05-29 10:09:22 -07001482static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07001483{
Sean Heftye8160e12013-05-29 10:09:22 -07001484 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
Sean Heftye51060f2006-06-17 20:37:29 -07001485}
1486
Sean Heftye51060f2006-06-17 20:37:29 -07001487static void cma_cancel_route(struct rdma_id_private *id_priv)
1488{
Michael Wangfe53ba22015-05-05 14:50:36 +02001489 if (rdma_cap_ib_sa(id_priv->id.device, id_priv->id.port_num)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001490 if (id_priv->query)
1491 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001492 }
1493}
1494
Sean Heftye51060f2006-06-17 20:37:29 -07001495static void cma_cancel_listens(struct rdma_id_private *id_priv)
1496{
1497 struct rdma_id_private *dev_id_priv;
1498
Sean Heftyd02d1f52007-10-09 11:12:34 -07001499 /*
1500 * Remove from listen_any_list to prevent added devices from spawning
1501 * additional listen requests.
1502 */
Sean Heftye51060f2006-06-17 20:37:29 -07001503 mutex_lock(&lock);
1504 list_del(&id_priv->list);
1505
1506 while (!list_empty(&id_priv->listen_list)) {
1507 dev_id_priv = list_entry(id_priv->listen_list.next,
1508 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001509 /* sync with device removal to avoid duplicate destruction */
1510 list_del_init(&dev_id_priv->list);
1511 list_del(&dev_id_priv->listen_list);
1512 mutex_unlock(&lock);
1513
1514 rdma_destroy_id(&dev_id_priv->id);
1515 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001516 }
1517 mutex_unlock(&lock);
1518}
1519
1520static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001521 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -07001522{
1523 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001524 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001525 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
1526 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001527 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -07001528 cma_cancel_route(id_priv);
1529 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001530 case RDMA_CM_LISTEN:
Sean Heftyf4753832013-05-29 10:09:14 -07001531 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001532 cma_cancel_listens(id_priv);
1533 break;
1534 default:
1535 break;
1536 }
1537}
1538
1539static void cma_release_port(struct rdma_id_private *id_priv)
1540{
1541 struct rdma_bind_list *bind_list = id_priv->bind_list;
Guy Shapirofa201052015-10-22 15:20:10 +03001542 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07001543
1544 if (!bind_list)
1545 return;
1546
1547 mutex_lock(&lock);
1548 hlist_del(&id_priv->node);
1549 if (hlist_empty(&bind_list->owners)) {
Guy Shapirofa201052015-10-22 15:20:10 +03001550 cma_ps_remove(net, bind_list->ps, bind_list->port);
Sean Heftye51060f2006-06-17 20:37:29 -07001551 kfree(bind_list);
1552 }
1553 mutex_unlock(&lock);
1554}
1555
Sean Heftyc8f6a362007-02-15 17:00:18 -08001556static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1557{
1558 struct cma_multicast *mc;
1559
1560 while (!list_empty(&id_priv->mc_list)) {
1561 mc = container_of(id_priv->mc_list.next,
1562 struct cma_multicast, list);
1563 list_del(&mc->list);
Michael Wanga31ad3b2015-05-05 14:50:37 +02001564 if (rdma_cap_ib_mcast(id_priv->cma_dev->device,
Michael Wang5c9a5282015-05-05 14:50:29 +02001565 id_priv->id.port_num)) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02001566 ib_sa_free_multicast(mc->multicast.ib);
1567 kfree(mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02001568 } else {
1569 if (mc->igmp_joined) {
1570 struct rdma_dev_addr *dev_addr =
1571 &id_priv->id.route.addr.dev_addr;
1572 struct net_device *ndev = NULL;
1573
1574 if (dev_addr->bound_dev_if)
1575 ndev = dev_get_by_index(&init_net,
1576 dev_addr->bound_dev_if);
1577 if (ndev) {
1578 cma_igmp_send(ndev,
1579 &mc->multicast.ib->rec.mgid,
1580 false);
1581 dev_put(ndev);
1582 }
1583 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02001584 kref_put(&mc->mcref, release_mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02001585 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001586 }
1587}
1588
Sean Heftye51060f2006-06-17 20:37:29 -07001589void rdma_destroy_id(struct rdma_cm_id *id)
1590{
1591 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001592 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -07001593
1594 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001595 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001596 cma_cancel_operation(id_priv, state);
1597
Sean Heftya396d432011-02-23 09:05:39 -08001598 /*
1599 * Wait for any active callback to finish. New callbacks will find
1600 * the id_priv state set to destroying and abort.
1601 */
1602 mutex_lock(&id_priv->handler_mutex);
1603 mutex_unlock(&id_priv->handler_mutex);
1604
Sean Heftye51060f2006-06-17 20:37:29 -07001605 if (id_priv->cma_dev) {
Michael Wang72219cea2015-05-05 14:50:34 +02001606 if (rdma_cap_ib_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001607 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07001608 ib_destroy_cm_id(id_priv->cm_id.ib);
Michael Wang04215332015-05-05 14:50:35 +02001609 } else if (rdma_cap_iw_cm(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001610 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -05001611 iw_destroy_cm_id(id_priv->cm_id.iw);
Sean Heftye51060f2006-06-17 20:37:29 -07001612 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001613 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -08001614 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001615 }
1616
1617 cma_release_port(id_priv);
1618 cma_deref_id(id_priv);
1619 wait_for_completion(&id_priv->comp);
1620
Sean Heftyd02d1f52007-10-09 11:12:34 -07001621 if (id_priv->internal_id)
1622 cma_deref_id(id_priv->id.context);
1623
Sean Heftye51060f2006-06-17 20:37:29 -07001624 kfree(id_priv->id.route.path_rec);
Guy Shapirofa201052015-10-22 15:20:10 +03001625 put_net(id_priv->id.route.addr.dev_addr.net);
Sean Heftye51060f2006-06-17 20:37:29 -07001626 kfree(id_priv);
1627}
1628EXPORT_SYMBOL(rdma_destroy_id);
1629
1630static int cma_rep_recv(struct rdma_id_private *id_priv)
1631{
1632 int ret;
1633
Sean Hefty5851bb82008-01-04 10:47:12 -08001634 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001635 if (ret)
1636 goto reject;
1637
Sean Hefty5851bb82008-01-04 10:47:12 -08001638 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001639 if (ret)
1640 goto reject;
1641
1642 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1643 if (ret)
1644 goto reject;
1645
1646 return 0;
1647reject:
Sean Heftyc5483382007-09-24 13:19:09 -07001648 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001649 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1650 NULL, 0, NULL, 0);
1651 return ret;
1652}
1653
Sean Heftya1b1b612006-11-30 16:33:14 -08001654static void cma_set_rep_event_data(struct rdma_cm_event *event,
1655 struct ib_cm_rep_event_param *rep_data,
1656 void *private_data)
1657{
1658 event->param.conn.private_data = private_data;
1659 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1660 event->param.conn.responder_resources = rep_data->responder_resources;
1661 event->param.conn.initiator_depth = rep_data->initiator_depth;
1662 event->param.conn.flow_control = rep_data->flow_control;
1663 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1664 event->param.conn.srq = rep_data->srq;
1665 event->param.conn.qp_num = rep_data->remote_qpn;
1666}
1667
Sean Heftye51060f2006-06-17 20:37:29 -07001668static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1669{
1670 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001671 struct rdma_cm_event event;
1672 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001673
Amir Vadai38ca83a2008-07-22 14:14:23 -07001674 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001675 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001676 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001677 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -07001678 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001679
Sean Heftya1b1b612006-11-30 16:33:14 -08001680 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001681 switch (ib_event->event) {
1682 case IB_CM_REQ_ERROR:
1683 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001684 event.event = RDMA_CM_EVENT_UNREACHABLE;
1685 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001686 break;
1687 case IB_CM_REP_RECEIVED:
Sean Hefty01602f12013-05-29 10:09:20 -07001688 if (id_priv->id.qp) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001689 event.status = cma_rep_recv(id_priv);
1690 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1691 RDMA_CM_EVENT_ESTABLISHED;
Sean Hefty01602f12013-05-29 10:09:20 -07001692 } else {
Sean Heftya1b1b612006-11-30 16:33:14 -08001693 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
Sean Hefty01602f12013-05-29 10:09:20 -07001694 }
Sean Heftya1b1b612006-11-30 16:33:14 -08001695 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1696 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001697 break;
1698 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001699 case IB_CM_USER_ESTABLISHED:
1700 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001701 break;
1702 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001703 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001704 case IB_CM_DREQ_RECEIVED:
1705 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001706 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1707 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001708 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001709 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001710 break;
1711 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001712 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1713 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001714 case IB_CM_MRA_RECEIVED:
1715 /* ignore event */
1716 goto out;
1717 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001718 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001719 event.status = ib_event->param.rej_rcvd.reason;
1720 event.event = RDMA_CM_EVENT_REJECTED;
1721 event.param.conn.private_data = ib_event->private_data;
1722 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001723 break;
1724 default:
Parav Panditaba25a3e2016-03-02 00:50:29 +05301725 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001726 ib_event->event);
1727 goto out;
1728 }
1729
Sean Heftya1b1b612006-11-30 16:33:14 -08001730 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001731 if (ret) {
1732 /* Destroy the CM ID by returning a non-zero value. */
1733 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001734 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001735 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001736 rdma_destroy_id(&id_priv->id);
1737 return ret;
1738 }
1739out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001740 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001741 return ret;
1742}
1743
Sean Hefty628e5f62006-11-30 16:44:16 -08001744static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001745 struct ib_cm_event *ib_event,
1746 struct net_device *net_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001747{
1748 struct rdma_id_private *id_priv;
1749 struct rdma_cm_id *id;
1750 struct rdma_route *rt;
Haggai Eran0c505f72015-07-30 17:50:19 +03001751 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
1752 const __be64 service_id =
1753 ib_event->param.req_rcvd.primary_path->service_id;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001754 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001755
Guy Shapirofa201052015-10-22 15:20:10 +03001756 id = rdma_create_id(listen_id->route.addr.dev_addr.net,
1757 listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001758 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001759 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001760 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001761
Sean Heftyf4753832013-05-29 10:09:14 -07001762 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001763 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1764 (struct sockaddr *)&id->route.addr.dst_addr,
1765 listen_id, ib_event, ss_family, service_id))
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001766 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001767
1768 rt = &id->route;
1769 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1770 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1771 GFP_KERNEL);
1772 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001773 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001774
Sean Heftye51060f2006-06-17 20:37:29 -07001775 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1776 if (rt->num_paths == 2)
1777 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1778
Haggai Eran0b3ca762015-07-30 17:50:24 +03001779 if (net_dev) {
1780 ret = rdma_copy_addr(&rt->addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001781 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001782 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001783 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001784 if (!cma_protocol_roce(listen_id) &&
1785 cma_any_addr(cma_src_addr(id_priv))) {
1786 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1787 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1788 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
1789 } else if (!cma_any_addr(cma_src_addr(id_priv))) {
1790 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
1791 if (ret)
1792 goto err;
1793 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001794 }
1795 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001796
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001797 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001798 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001799
Krishna Kumar3f168d22006-09-29 12:09:51 -07001800err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001801 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001802 return NULL;
1803}
1804
Sean Hefty628e5f62006-11-30 16:44:16 -08001805static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
Haggai Eran0b3ca762015-07-30 17:50:24 +03001806 struct ib_cm_event *ib_event,
1807 struct net_device *net_dev)
Sean Hefty628e5f62006-11-30 16:44:16 -08001808{
1809 struct rdma_id_private *id_priv;
1810 struct rdma_cm_id *id;
Haggai Eran0c505f72015-07-30 17:50:19 +03001811 const sa_family_t ss_family = listen_id->route.addr.src_addr.ss_family;
Guy Shapirofa201052015-10-22 15:20:10 +03001812 struct net *net = listen_id->route.addr.dev_addr.net;
Sean Hefty628e5f62006-11-30 16:44:16 -08001813 int ret;
1814
Guy Shapirofa201052015-10-22 15:20:10 +03001815 id = rdma_create_id(net, listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001816 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001817 if (IS_ERR(id))
1818 return NULL;
1819
Sean Heftyf4753832013-05-29 10:09:14 -07001820 id_priv = container_of(id, struct rdma_id_private, id);
Haggai Eran0c505f72015-07-30 17:50:19 +03001821 if (cma_save_net_info((struct sockaddr *)&id->route.addr.src_addr,
1822 (struct sockaddr *)&id->route.addr.dst_addr,
1823 listen_id, ib_event, ss_family,
1824 ib_event->param.sidr_req_rcvd.service_id))
Sean Hefty628e5f62006-11-30 16:44:16 -08001825 goto err;
1826
Haggai Eran0b3ca762015-07-30 17:50:24 +03001827 if (net_dev) {
1828 ret = rdma_copy_addr(&id->route.addr.dev_addr, net_dev, NULL);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001829 if (ret)
1830 goto err;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001831 } else {
Haggai Eranb8cab5d2015-10-06 17:56:00 +03001832 if (!cma_any_addr(cma_src_addr(id_priv))) {
1833 ret = cma_translate_addr(cma_src_addr(id_priv),
1834 &id->route.addr.dev_addr);
1835 if (ret)
1836 goto err;
1837 }
Sean Hefty6f8372b2009-11-19 13:26:06 -08001838 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001839
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001840 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001841 return id_priv;
1842err:
1843 rdma_destroy_id(id);
1844 return NULL;
1845}
1846
Sean Heftya1b1b612006-11-30 16:33:14 -08001847static void cma_set_req_event_data(struct rdma_cm_event *event,
1848 struct ib_cm_req_event_param *req_data,
1849 void *private_data, int offset)
1850{
1851 event->param.conn.private_data = private_data + offset;
1852 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1853 event->param.conn.responder_resources = req_data->responder_resources;
1854 event->param.conn.initiator_depth = req_data->initiator_depth;
1855 event->param.conn.flow_control = req_data->flow_control;
1856 event->param.conn.retry_count = req_data->retry_count;
1857 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1858 event->param.conn.srq = req_data->srq;
1859 event->param.conn.qp_num = req_data->remote_qpn;
1860}
1861
Hefty, Sean95954802011-10-06 09:32:33 -07001862static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1863{
Sean Hefty4dd81e82012-06-14 20:49:09 +00001864 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
Hefty, Sean95954802011-10-06 09:32:33 -07001865 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1866 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1867 (id->qp_type == IB_QPT_UD)) ||
1868 (!id->qp_type));
1869}
1870
Sean Heftye51060f2006-06-17 20:37:29 -07001871static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1872{
1873 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001874 struct rdma_cm_event event;
Haggai Eran0b3ca762015-07-30 17:50:24 +03001875 struct net_device *net_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07001876 int offset, ret;
1877
Haggai Eran0b3ca762015-07-30 17:50:24 +03001878 listen_id = cma_id_from_event(cm_id, ib_event, &net_dev);
Haggai Eran4c21b5b2015-07-30 17:50:22 +03001879 if (IS_ERR(listen_id))
1880 return PTR_ERR(listen_id);
1881
Haggai Eran0b3ca762015-07-30 17:50:24 +03001882 if (!cma_check_req_qp_type(&listen_id->id, ib_event)) {
1883 ret = -EINVAL;
1884 goto net_dev_put;
1885 }
Hefty, Sean95954802011-10-06 09:32:33 -07001886
Haggai Eran0b3ca762015-07-30 17:50:24 +03001887 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) {
1888 ret = -ECONNABORTED;
1889 goto net_dev_put;
1890 }
Sean Heftye51060f2006-06-17 20:37:29 -07001891
Sean Hefty628e5f62006-11-30 16:44:16 -08001892 memset(&event, 0, sizeof event);
Sean Heftye8160e12013-05-29 10:09:22 -07001893 offset = cma_user_data_offset(listen_id);
Sean Hefty628e5f62006-11-30 16:44:16 -08001894 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001895 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001896 conn_id = cma_new_udp_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001897 event.param.ud.private_data = ib_event->private_data + offset;
1898 event.param.ud.private_data_len =
1899 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1900 } else {
Haggai Eran0b3ca762015-07-30 17:50:24 +03001901 conn_id = cma_new_conn_id(&listen_id->id, ib_event, net_dev);
Sean Hefty628e5f62006-11-30 16:44:16 -08001902 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1903 ib_event->private_data, offset);
1904 }
Sean Heftye51060f2006-06-17 20:37:29 -07001905 if (!conn_id) {
1906 ret = -ENOMEM;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001907 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001908 }
1909
Or Gerlitzde910bd2008-07-14 23:48:53 -07001910 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001911 ret = cma_acquire_dev(conn_id, listen_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301912 if (ret)
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001913 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001914
1915 conn_id->cm_id.ib = cm_id;
1916 cm_id->context = conn_id;
1917 cm_id->cm_handler = cma_ib_handler;
1918
Sean Hefty25ae21a2011-02-23 08:11:32 -08001919 /*
1920 * Protect against the user destroying conn_id from another thread
1921 * until we're done accessing it.
1922 */
1923 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001924 ret = conn_id->id.event_handler(&conn_id->id, &event);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001925 if (ret)
1926 goto err3;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001927 /*
1928 * Acquire mutex to prevent user executing rdma_destroy_id()
1929 * while we're accessing the cm_id.
1930 */
1931 mutex_lock(&lock);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001932 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1933 (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001934 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1935 mutex_unlock(&lock);
1936 mutex_unlock(&conn_id->handler_mutex);
1937 mutex_unlock(&listen_id->handler_mutex);
1938 cma_deref_id(conn_id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001939 if (net_dev)
1940 dev_put(net_dev);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001941 return 0;
1942
1943err3:
1944 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301945 /* Destroy the CM ID by returning a non-zero value. */
1946 conn_id->cm_id.ib = NULL;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001947err2:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001948 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001949 mutex_unlock(&conn_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001950err1:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001951 mutex_unlock(&listen_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001952 if (conn_id)
1953 rdma_destroy_id(&conn_id->id);
Haggai Eran0b3ca762015-07-30 17:50:24 +03001954
1955net_dev_put:
1956 if (net_dev)
1957 dev_put(net_dev);
1958
Sean Heftye51060f2006-06-17 20:37:29 -07001959 return ret;
1960}
1961
Sean Heftycf539362013-05-29 10:09:28 -07001962__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
Sean Heftye51060f2006-06-17 20:37:29 -07001963{
Sean Hefty496ce3c2013-05-29 10:09:19 -07001964 if (addr->sa_family == AF_IB)
1965 return ((struct sockaddr_ib *) addr)->sib_sid;
1966
Sean Heftycf539362013-05-29 10:09:28 -07001967 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001968}
Sean Heftycf539362013-05-29 10:09:28 -07001969EXPORT_SYMBOL(rdma_get_service_id);
Sean Heftye51060f2006-06-17 20:37:29 -07001970
Tom Tucker07ebafb2006-08-03 16:02:42 -05001971static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1972{
1973 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001974 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001975 int ret = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05301976 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1977 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001978
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001979 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001980 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001981
Sean Heftybe65f082007-05-07 11:49:12 -07001982 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001983 switch (iw_event->event) {
1984 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001985 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001986 break;
1987 case IW_CM_EVENT_CONNECT_REPLY:
Steve Wise24d44a32013-07-04 16:10:44 +05301988 memcpy(cma_src_addr(id_priv), laddr,
1989 rdma_addr_size(laddr));
1990 memcpy(cma_dst_addr(id_priv), raddr,
1991 rdma_addr_size(raddr));
Steve Wise881a0452006-12-15 16:50:17 -06001992 switch (iw_event->status) {
1993 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001994 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301995 event.param.conn.initiator_depth = iw_event->ird;
1996 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06001997 break;
1998 case -ECONNRESET:
1999 case -ECONNREFUSED:
2000 event.event = RDMA_CM_EVENT_REJECTED;
2001 break;
2002 case -ETIMEDOUT:
2003 event.event = RDMA_CM_EVENT_UNREACHABLE;
2004 break;
2005 default:
2006 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
2007 break;
2008 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002009 break;
2010 case IW_CM_EVENT_ESTABLISHED:
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;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002014 break;
2015 default:
2016 BUG_ON(1);
2017 }
2018
Sean Heftya1b1b612006-11-30 16:33:14 -08002019 event.status = iw_event->status;
2020 event.param.conn.private_data = iw_event->private_data;
2021 event.param.conn.private_data_len = iw_event->private_data_len;
2022 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002023 if (ret) {
2024 /* Destroy the CM ID by returning a non-zero value. */
2025 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002026 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002027 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002028 rdma_destroy_id(&id_priv->id);
2029 return ret;
2030 }
2031
Or Gerlitzde910bd2008-07-14 23:48:53 -07002032 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002033 return ret;
2034}
2035
2036static int iw_conn_req_handler(struct iw_cm_id *cm_id,
2037 struct iw_cm_event *iw_event)
2038{
2039 struct rdma_cm_id *new_cm_id;
2040 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08002041 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002042 int ret;
Steve Wise24d44a32013-07-04 16:10:44 +05302043 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
2044 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002045
2046 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002047 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07002048 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002049
2050 /* Create a new RDMA id for the new IW CM ID */
Guy Shapirofa201052015-10-22 15:20:10 +03002051 new_cm_id = rdma_create_id(listen_id->id.route.addr.dev_addr.net,
2052 listen_id->id.event_handler,
Tom Tucker07ebafb2006-08-03 16:02:42 -05002053 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00002054 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07002055 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05002056 ret = -ENOMEM;
2057 goto out;
2058 }
2059 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002060 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002061 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002062
Matan Barakdd5f03b2013-12-12 18:03:11 +02002063 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002064 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07002065 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002066 rdma_destroy_id(new_cm_id);
2067 goto out;
2068 }
2069
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002070 ret = cma_acquire_dev(conn_id, listen_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002071 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07002072 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002073 rdma_destroy_id(new_cm_id);
2074 goto out;
2075 }
2076
2077 conn_id->cm_id.iw = cm_id;
2078 cm_id->context = conn_id;
2079 cm_id->cm_handler = cma_iw_handler;
2080
Steve Wise24d44a32013-07-04 16:10:44 +05302081 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
2082 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002083
Sean Heftya1b1b612006-11-30 16:33:14 -08002084 memset(&event, 0, sizeof event);
2085 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
2086 event.param.conn.private_data = iw_event->private_data;
2087 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05302088 event.param.conn.initiator_depth = iw_event->ird;
2089 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08002090
2091 /*
2092 * Protect against the user destroying conn_id from another thread
2093 * until we're done accessing it.
2094 */
2095 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08002096 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002097 if (ret) {
2098 /* User wants to destroy the CM ID */
2099 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002100 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002101 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08002102 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002103 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002104 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002105 }
2106
Or Gerlitzde910bd2008-07-14 23:48:53 -07002107 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08002108 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002109
Tom Tucker07ebafb2006-08-03 16:02:42 -05002110out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002111 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002112 return ret;
2113}
2114
Sean Heftye51060f2006-06-17 20:37:29 -07002115static int cma_ib_listen(struct rdma_id_private *id_priv)
2116{
Sean Heftye51060f2006-06-17 20:37:29 -07002117 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002118 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07002119 __be64 svc_id;
Sean Heftye51060f2006-06-17 20:37:29 -07002120
Sean Heftyf4753832013-05-29 10:09:14 -07002121 addr = cma_src_addr(id_priv);
Sean Heftycf539362013-05-29 10:09:28 -07002122 svc_id = rdma_get_service_id(&id_priv->id, addr);
Haggai Eran51efe392015-07-30 17:50:25 +03002123 id = ib_cm_insert_listen(id_priv->id.device, cma_req_handler, svc_id);
2124 if (IS_ERR(id))
2125 return PTR_ERR(id);
2126 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07002127
Haggai Eran51efe392015-07-30 17:50:25 +03002128 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07002129}
2130
Tom Tucker07ebafb2006-08-03 16:02:42 -05002131static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
2132{
2133 int ret;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002134 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002135
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002136 id = iw_create_cm_id(id_priv->id.device,
2137 iw_conn_req_handler,
2138 id_priv);
2139 if (IS_ERR(id))
2140 return PTR_ERR(id);
2141
Steve Wise68cdba02015-05-18 15:27:10 -05002142 id->tos = id_priv->tos;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002143 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002144
Steve Wise24d44a32013-07-04 16:10:44 +05302145 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
2146 rdma_addr_size(cma_src_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002147
2148 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
2149
2150 if (ret) {
2151 iw_destroy_cm_id(id_priv->cm_id.iw);
2152 id_priv->cm_id.iw = NULL;
2153 }
2154
2155 return ret;
2156}
2157
Sean Heftye51060f2006-06-17 20:37:29 -07002158static int cma_listen_handler(struct rdma_cm_id *id,
2159 struct rdma_cm_event *event)
2160{
2161 struct rdma_id_private *id_priv = id->context;
2162
2163 id->context = id_priv->id.context;
2164 id->event_handler = id_priv->id.event_handler;
2165 return id_priv->id.event_handler(id, event);
2166}
2167
2168static void cma_listen_on_dev(struct rdma_id_private *id_priv,
2169 struct cma_device *cma_dev)
2170{
2171 struct rdma_id_private *dev_id_priv;
2172 struct rdma_cm_id *id;
Guy Shapirofa201052015-10-22 15:20:10 +03002173 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftye51060f2006-06-17 20:37:29 -07002174 int ret;
2175
Michael Wang72219cea2015-05-05 14:50:34 +02002176 if (cma_family(id_priv) == AF_IB && !rdma_cap_ib_cm(cma_dev->device, 1))
Sean Hefty94d0c932013-05-29 10:09:24 -07002177 return;
2178
Guy Shapirofa201052015-10-22 15:20:10 +03002179 id = rdma_create_id(net, cma_listen_handler, id_priv, id_priv->id.ps,
Sean Heftyb26f9b92010-04-01 17:08:41 +00002180 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07002181 if (IS_ERR(id))
2182 return;
2183
2184 dev_id_priv = container_of(id, struct rdma_id_private, id);
2185
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002186 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftyf4753832013-05-29 10:09:14 -07002187 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
2188 rdma_addr_size(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002189
Matan Barak045959d2015-12-23 14:56:55 +02002190 _cma_attach_to_dev(dev_id_priv, cma_dev);
Sean Heftye51060f2006-06-17 20:37:29 -07002191 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07002192 atomic_inc(&id_priv->refcount);
2193 dev_id_priv->internal_id = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002194 dev_id_priv->afonly = id_priv->afonly;
Sean Heftye51060f2006-06-17 20:37:29 -07002195
2196 ret = rdma_listen(id, id_priv->backlog);
2197 if (ret)
Parav Panditaba25a3e2016-03-02 00:50:29 +05302198 pr_warn("RDMA CMA: cma_listen_on_dev, error %d, listening on device %s\n",
2199 ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07002200}
2201
2202static void cma_listen_on_all(struct rdma_id_private *id_priv)
2203{
2204 struct cma_device *cma_dev;
2205
2206 mutex_lock(&lock);
2207 list_add_tail(&id_priv->list, &listen_any_list);
2208 list_for_each_entry(cma_dev, &dev_list, list)
2209 cma_listen_on_dev(id_priv, cma_dev);
2210 mutex_unlock(&lock);
2211}
2212
Sean Heftya81c9942007-08-08 15:51:06 -07002213void rdma_set_service_type(struct rdma_cm_id *id, int tos)
2214{
2215 struct rdma_id_private *id_priv;
2216
2217 id_priv = container_of(id, struct rdma_id_private, id);
2218 id_priv->tos = (u8) tos;
2219}
2220EXPORT_SYMBOL(rdma_set_service_type);
2221
Sean Heftye51060f2006-06-17 20:37:29 -07002222static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
2223 void *context)
2224{
2225 struct cma_work *work = context;
2226 struct rdma_route *route;
2227
2228 route = &work->id->id.route;
2229
2230 if (!status) {
2231 route->num_paths = 1;
2232 *route->path_rec = *path_rec;
2233 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002234 work->old_state = RDMA_CM_ROUTE_QUERY;
2235 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002236 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07002237 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07002238 }
2239
2240 queue_work(cma_wq, &work->work);
2241}
2242
2243static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
2244 struct cma_work *work)
2245{
Sean Heftyf4753832013-05-29 10:09:14 -07002246 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002247 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07002248 ib_sa_comp_mask comp_mask;
2249 struct sockaddr_in6 *sin6;
Sean Heftyf68194c2013-05-29 10:09:18 -07002250 struct sockaddr_ib *sib;
Sean Heftye51060f2006-06-17 20:37:29 -07002251
2252 memset(&path_rec, 0, sizeof path_rec);
Sean Heftyf4753832013-05-29 10:09:14 -07002253 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
2254 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
2255 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07002256 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08002257 path_rec.reversible = 1;
Sean Heftycf539362013-05-29 10:09:28 -07002258 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftya81c9942007-08-08 15:51:06 -07002259
2260 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
2261 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
2262 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
2263
Sean Heftyf68194c2013-05-29 10:09:18 -07002264 switch (cma_family(id_priv)) {
2265 case AF_INET:
Sean Heftya81c9942007-08-08 15:51:06 -07002266 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
2267 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002268 break;
2269 case AF_INET6:
Sean Heftyf4753832013-05-29 10:09:14 -07002270 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
Sean Heftya81c9942007-08-08 15:51:06 -07002271 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
2272 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07002273 break;
2274 case AF_IB:
2275 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
2276 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
2277 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
2278 break;
Sean Heftya81c9942007-08-08 15:51:06 -07002279 }
Sean Heftye51060f2006-06-17 20:37:29 -07002280
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002281 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07002282 id_priv->id.port_num, &path_rec,
2283 comp_mask, timeout_ms,
2284 GFP_KERNEL, cma_query_handler,
2285 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07002286
2287 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
2288}
2289
David Howellsc4028952006-11-22 14:57:56 +00002290static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07002291{
David Howellsc4028952006-11-22 14:57:56 +00002292 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07002293 struct rdma_id_private *id_priv = work->id;
2294 int destroy = 0;
2295
Or Gerlitzde910bd2008-07-14 23:48:53 -07002296 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002297 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
2298 goto out;
2299
2300 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002301 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07002302 destroy = 1;
2303 }
2304out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002305 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002306 cma_deref_id(id_priv);
2307 if (destroy)
2308 rdma_destroy_id(&id_priv->id);
2309 kfree(work);
2310}
2311
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002312static void cma_ndev_work_handler(struct work_struct *_work)
2313{
2314 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
2315 struct rdma_id_private *id_priv = work->id;
2316 int destroy = 0;
2317
2318 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002319 if (id_priv->state == RDMA_CM_DESTROYING ||
2320 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002321 goto out;
2322
2323 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002324 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002325 destroy = 1;
2326 }
2327
2328out:
2329 mutex_unlock(&id_priv->handler_mutex);
2330 cma_deref_id(id_priv);
2331 if (destroy)
2332 rdma_destroy_id(&id_priv->id);
2333 kfree(work);
2334}
2335
Sean Heftye51060f2006-06-17 20:37:29 -07002336static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
2337{
2338 struct rdma_route *route = &id_priv->id.route;
2339 struct cma_work *work;
2340 int ret;
2341
2342 work = kzalloc(sizeof *work, GFP_KERNEL);
2343 if (!work)
2344 return -ENOMEM;
2345
2346 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002347 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002348 work->old_state = RDMA_CM_ROUTE_QUERY;
2349 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002350 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2351
2352 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
2353 if (!route->path_rec) {
2354 ret = -ENOMEM;
2355 goto err1;
2356 }
2357
2358 ret = cma_query_ib_route(id_priv, timeout_ms, work);
2359 if (ret)
2360 goto err2;
2361
2362 return 0;
2363err2:
2364 kfree(route->path_rec);
2365 route->path_rec = NULL;
2366err1:
2367 kfree(work);
2368 return ret;
2369}
2370
2371int rdma_set_ib_paths(struct rdma_cm_id *id,
2372 struct ib_sa_path_rec *path_rec, int num_paths)
2373{
2374 struct rdma_id_private *id_priv;
2375 int ret;
2376
2377 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002378 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2379 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07002380 return -EINVAL;
2381
Julia Lawall9893e742010-05-15 23:22:38 +02002382 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
2383 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002384 if (!id->route.path_rec) {
2385 ret = -ENOMEM;
2386 goto err;
2387 }
2388
Sean Heftyae2d9292010-03-25 19:12:36 +00002389 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07002390 return 0;
2391err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002392 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002393 return ret;
2394}
2395EXPORT_SYMBOL(rdma_set_ib_paths);
2396
Tom Tucker07ebafb2006-08-03 16:02:42 -05002397static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
2398{
2399 struct cma_work *work;
2400
2401 work = kzalloc(sizeof *work, GFP_KERNEL);
2402 if (!work)
2403 return -ENOMEM;
2404
2405 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002406 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002407 work->old_state = RDMA_CM_ROUTE_QUERY;
2408 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002409 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2410 queue_work(cma_wq, &work->work);
2411 return 0;
2412}
2413
Eyal Perryeb072c42013-11-06 15:37:24 +02002414static int iboe_tos_to_sl(struct net_device *ndev, int tos)
2415{
2416 int prio;
2417 struct net_device *dev;
2418
2419 prio = rt_tos2priority(tos);
2420 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
2421 vlan_dev_real_dev(ndev) : ndev;
2422
2423 if (dev->num_tc)
2424 return netdev_get_prio_tc_map(dev, prio);
2425
2426#if IS_ENABLED(CONFIG_VLAN_8021Q)
2427 if (ndev->priv_flags & IFF_802_1Q_VLAN)
2428 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
2429 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
2430#endif
2431 return 0;
2432}
2433
Eli Cohen3c86aa72010-10-13 21:26:51 +02002434static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
2435{
2436 struct rdma_route *route = &id_priv->id.route;
2437 struct rdma_addr *addr = &route->addr;
2438 struct cma_work *work;
2439 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002440 struct net_device *ndev = NULL;
Matan Barakdd5f03b2013-12-12 18:03:11 +02002441
Eli Cohen3c86aa72010-10-13 21:26:51 +02002442
Eli Cohen3c86aa72010-10-13 21:26:51 +02002443 work = kzalloc(sizeof *work, GFP_KERNEL);
2444 if (!work)
2445 return -ENOMEM;
2446
2447 work->id = id_priv;
2448 INIT_WORK(&work->work, cma_work_handler);
2449
2450 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
2451 if (!route->path_rec) {
2452 ret = -ENOMEM;
2453 goto err1;
2454 }
2455
2456 route->num_paths = 1;
2457
Matan Barakabae1b72015-10-15 18:38:49 +03002458 if (addr->dev_addr.bound_dev_if) {
Eli Cohen3c86aa72010-10-13 21:26:51 +02002459 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
Matan Barak20029832015-12-23 14:56:53 +02002460 if (!ndev)
2461 return -ENODEV;
2462
2463 if (ndev->flags & IFF_LOOPBACK) {
2464 dev_put(ndev);
2465 if (!id_priv->id.device->get_netdev)
2466 return -EOPNOTSUPP;
2467
2468 ndev = id_priv->id.device->get_netdev(id_priv->id.device,
2469 id_priv->id.port_num);
2470 if (!ndev)
2471 return -ENODEV;
2472 }
2473
Matan Barakabae1b72015-10-15 18:38:49 +03002474 route->path_rec->net = &init_net;
Matan Barak20029832015-12-23 14:56:53 +02002475 route->path_rec->ifindex = ndev->ifindex;
Matan Barakcb57bb82015-12-23 14:56:48 +02002476 route->path_rec->gid_type = id_priv->gid_type;
Matan Barakabae1b72015-10-15 18:38:49 +03002477 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02002478 if (!ndev) {
2479 ret = -ENODEV;
2480 goto err2;
2481 }
2482
Matan Barakdd5f03b2013-12-12 18:03:11 +02002483 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002484
Moni Shoua7b856272013-12-12 18:03:12 +02002485 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
2486 &route->path_rec->sgid);
2487 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
2488 &route->path_rec->dgid);
Eli Cohenaf7bd462010-08-26 17:18:59 +03002489
Somnath Koturc865f242015-12-23 14:56:51 +02002490 /* Use the hint from IP Stack to select GID Type */
Matan Barakc3efe752016-01-04 10:49:54 +02002491 if (route->path_rec->gid_type < ib_network_to_gid_type(addr->dev_addr.network))
2492 route->path_rec->gid_type = ib_network_to_gid_type(addr->dev_addr.network);
2493 if (((struct sockaddr *)&id_priv->id.route.addr.dst_addr)->sa_family != AF_IB)
Somnath Koturc865f242015-12-23 14:56:51 +02002494 /* TODO: get the hoplimit from the inet/inet6 device */
Matan Barakc3efe752016-01-04 10:49:54 +02002495 route->path_rec->hop_limit = addr->dev_addr.hoplimit;
2496 else
Somnath Koturc865f242015-12-23 14:56:51 +02002497 route->path_rec->hop_limit = 1;
Eli Cohenaf7bd462010-08-26 17:18:59 +03002498 route->path_rec->reversible = 1;
2499 route->path_rec->pkey = cpu_to_be16(0xffff);
2500 route->path_rec->mtu_selector = IB_SA_EQ;
Eyal Perryeb072c42013-11-06 15:37:24 +02002501 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
Eli Cohen3c86aa72010-10-13 21:26:51 +02002502 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
2503 route->path_rec->rate_selector = IB_SA_EQ;
2504 route->path_rec->rate = iboe_get_rate(ndev);
2505 dev_put(ndev);
2506 route->path_rec->packet_life_time_selector = IB_SA_EQ;
2507 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
2508 if (!route->path_rec->mtu) {
2509 ret = -EINVAL;
2510 goto err2;
2511 }
2512
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002513 work->old_state = RDMA_CM_ROUTE_QUERY;
2514 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02002515 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
2516 work->event.status = 0;
2517
2518 queue_work(cma_wq, &work->work);
2519
2520 return 0;
2521
2522err2:
2523 kfree(route->path_rec);
2524 route->path_rec = NULL;
2525err1:
2526 kfree(work);
2527 return ret;
2528}
2529
Sean Heftye51060f2006-06-17 20:37:29 -07002530int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
2531{
2532 struct rdma_id_private *id_priv;
2533 int ret;
2534
2535 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002536 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002537 return -EINVAL;
2538
2539 atomic_inc(&id_priv->refcount);
Michael Wangfe53ba22015-05-05 14:50:36 +02002540 if (rdma_cap_ib_sa(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002541 ret = cma_resolve_ib_route(id_priv, timeout_ms);
Ira Weiny5d9fb042015-05-14 15:01:46 -04002542 else if (rdma_protocol_roce(id->device, id->port_num))
Michael Wangc72f2182015-05-05 14:50:28 +02002543 ret = cma_resolve_iboe_route(id_priv);
2544 else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002545 ret = cma_resolve_iw_route(id_priv, timeout_ms);
Michael Wangc72f2182015-05-05 14:50:28 +02002546 else
Sean Heftye51060f2006-06-17 20:37:29 -07002547 ret = -ENOSYS;
Michael Wangc72f2182015-05-05 14:50:28 +02002548
Sean Heftye51060f2006-06-17 20:37:29 -07002549 if (ret)
2550 goto err;
2551
2552 return 0;
2553err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002554 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002555 cma_deref_id(id_priv);
2556 return ret;
2557}
2558EXPORT_SYMBOL(rdma_resolve_route);
2559
Sean Hefty6a3e3622013-05-29 10:09:13 -07002560static void cma_set_loopback(struct sockaddr *addr)
2561{
2562 switch (addr->sa_family) {
2563 case AF_INET:
2564 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2565 break;
2566 case AF_INET6:
2567 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2568 0, 0, 0, htonl(1));
2569 break;
2570 default:
2571 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2572 0, 0, 0, htonl(1));
2573 break;
2574 }
2575}
2576
Sean Heftye51060f2006-06-17 20:37:29 -07002577static int cma_bind_loopback(struct rdma_id_private *id_priv)
2578{
Sean Heftyb0569e42013-05-29 10:09:15 -07002579 struct cma_device *cma_dev, *cur_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07002580 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002581 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002582 u16 pkey;
2583 int ret;
2584 u8 p;
2585
Sean Heftyb0569e42013-05-29 10:09:15 -07002586 cma_dev = NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07002587 mutex_lock(&lock);
Sean Heftyb0569e42013-05-29 10:09:15 -07002588 list_for_each_entry(cur_dev, &dev_list, list) {
2589 if (cma_family(id_priv) == AF_IB &&
Michael Wang72219cea2015-05-05 14:50:34 +02002590 !rdma_cap_ib_cm(cur_dev->device, 1))
Sean Heftyb0569e42013-05-29 10:09:15 -07002591 continue;
2592
2593 if (!cma_dev)
2594 cma_dev = cur_dev;
2595
2596 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2597 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2598 port_attr.state == IB_PORT_ACTIVE) {
2599 cma_dev = cur_dev;
2600 goto port_found;
2601 }
2602 }
2603 }
2604
2605 if (!cma_dev) {
Sean Heftye51060f2006-06-17 20:37:29 -07002606 ret = -ENODEV;
2607 goto out;
2608 }
Krishna Kumare82153b2006-10-16 10:09:01 +05302609
2610 p = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07002611
2612port_found:
Matan Barak55ee3ab2015-10-15 18:38:45 +03002613 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002614 if (ret)
2615 goto out;
2616
2617 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2618 if (ret)
2619 goto out;
2620
Sean Hefty6f8372b2009-11-19 13:26:06 -08002621 id_priv->id.route.addr.dev_addr.dev_type =
Michael Wang21655af2015-05-05 14:50:27 +02002622 (rdma_protocol_ib(cma_dev->device, p)) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08002623 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2624
2625 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002626 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2627 id_priv->id.port_num = p;
2628 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf4753832013-05-29 10:09:14 -07002629 cma_set_loopback(cma_src_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002630out:
2631 mutex_unlock(&lock);
2632 return ret;
2633}
2634
2635static void addr_handler(int status, struct sockaddr *src_addr,
2636 struct rdma_dev_addr *dev_addr, void *context)
2637{
2638 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08002639 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002640
Sean Heftya1b1b612006-11-30 16:33:14 -08002641 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002642 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002643 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2644 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07002645 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07002646
Moni Shoua7b856272013-12-12 18:03:12 +02002647 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
Sean Hefty61a73c72006-09-01 15:33:55 -07002648 if (!status && !id_priv->cma_dev)
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002649 status = cma_acquire_dev(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002650
2651 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002652 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2653 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002654 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08002655 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2656 event.status = status;
Moni Shoua7b856272013-12-12 18:03:12 +02002657 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08002658 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002659
Sean Heftya1b1b612006-11-30 16:33:14 -08002660 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002661 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002662 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002663 cma_deref_id(id_priv);
2664 rdma_destroy_id(&id_priv->id);
2665 return;
2666 }
2667out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002668 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002669 cma_deref_id(id_priv);
2670}
2671
2672static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2673{
2674 struct cma_work *work;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002675 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002676 int ret;
2677
2678 work = kzalloc(sizeof *work, GFP_KERNEL);
2679 if (!work)
2680 return -ENOMEM;
2681
2682 if (!id_priv->cma_dev) {
2683 ret = cma_bind_loopback(id_priv);
2684 if (ret)
2685 goto err;
2686 }
2687
Sean Hefty6f8372b2009-11-19 13:26:06 -08002688 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2689 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002690
Sean Heftye51060f2006-06-17 20:37:29 -07002691 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002692 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002693 work->old_state = RDMA_CM_ADDR_QUERY;
2694 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002695 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2696 queue_work(cma_wq, &work->work);
2697 return 0;
2698err:
2699 kfree(work);
2700 return ret;
2701}
2702
Sean Heftyf17df3b2013-05-29 10:09:17 -07002703static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2704{
2705 struct cma_work *work;
2706 int ret;
2707
2708 work = kzalloc(sizeof *work, GFP_KERNEL);
2709 if (!work)
2710 return -ENOMEM;
2711
2712 if (!id_priv->cma_dev) {
2713 ret = cma_resolve_ib_dev(id_priv);
2714 if (ret)
2715 goto err;
2716 }
2717
2718 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2719 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2720
2721 work->id = id_priv;
2722 INIT_WORK(&work->work, cma_work_handler);
2723 work->old_state = RDMA_CM_ADDR_QUERY;
2724 work->new_state = RDMA_CM_ADDR_RESOLVED;
2725 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2726 queue_work(cma_wq, &work->work);
2727 return 0;
2728err:
2729 kfree(work);
2730 return ret;
2731}
2732
Sean Heftye51060f2006-06-17 20:37:29 -07002733static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2734 struct sockaddr *dst_addr)
2735{
Sean Heftyd14714d2009-11-19 16:46:25 -08002736 if (!src_addr || !src_addr->sa_family) {
2737 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002738 src_addr->sa_family = dst_addr->sa_family;
2739 if (dst_addr->sa_family == AF_INET6) {
Spencer Baugh6c26a772015-08-13 12:19:10 -07002740 struct sockaddr_in6 *src_addr6 = (struct sockaddr_in6 *) src_addr;
2741 struct sockaddr_in6 *dst_addr6 = (struct sockaddr_in6 *) dst_addr;
2742 src_addr6->sin6_scope_id = dst_addr6->sin6_scope_id;
2743 if (ipv6_addr_type(&dst_addr6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
2744 id->route.addr.dev_addr.bound_dev_if = dst_addr6->sin6_scope_id;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002745 } else if (dst_addr->sa_family == AF_IB) {
2746 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2747 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
Sean Heftyd14714d2009-11-19 16:46:25 -08002748 }
2749 }
2750 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002751}
2752
2753int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2754 struct sockaddr *dst_addr, int timeout_ms)
2755{
2756 struct rdma_id_private *id_priv;
2757 int ret;
2758
2759 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002760 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002761 ret = cma_bind_addr(id, src_addr, dst_addr);
2762 if (ret)
2763 return ret;
2764 }
2765
Sean Hefty4ae71522013-05-29 10:09:16 -07002766 if (cma_family(id_priv) != dst_addr->sa_family)
2767 return -EINVAL;
2768
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002769 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002770 return -EINVAL;
2771
2772 atomic_inc(&id_priv->refcount);
Sean Heftyf4753832013-05-29 10:09:14 -07002773 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
Sean Heftyf17df3b2013-05-29 10:09:17 -07002774 if (cma_any_addr(dst_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002775 ret = cma_resolve_loopback(id_priv);
Sean Heftyf17df3b2013-05-29 10:09:17 -07002776 } else {
2777 if (dst_addr->sa_family == AF_IB) {
2778 ret = cma_resolve_ib_addr(id_priv);
2779 } else {
2780 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2781 dst_addr, &id->route.addr.dev_addr,
2782 timeout_ms, addr_handler, id_priv);
2783 }
2784 }
Sean Heftye51060f2006-06-17 20:37:29 -07002785 if (ret)
2786 goto err;
2787
2788 return 0;
2789err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002790 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002791 cma_deref_id(id_priv);
2792 return ret;
2793}
2794EXPORT_SYMBOL(rdma_resolve_addr);
2795
Hefty, Seana9bb7912011-05-09 22:06:10 -07002796int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2797{
2798 struct rdma_id_private *id_priv;
2799 unsigned long flags;
2800 int ret;
2801
2802 id_priv = container_of(id, struct rdma_id_private, id);
2803 spin_lock_irqsave(&id_priv->lock, flags);
Sean Heftyc8dea2f92013-05-29 10:09:08 -07002804 if (reuse || id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002805 id_priv->reuseaddr = reuse;
2806 ret = 0;
2807 } else {
2808 ret = -EINVAL;
2809 }
2810 spin_unlock_irqrestore(&id_priv->lock, flags);
2811 return ret;
2812}
2813EXPORT_SYMBOL(rdma_set_reuseaddr);
2814
Sean Hefty68602122012-06-14 20:31:39 +00002815int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2816{
2817 struct rdma_id_private *id_priv;
2818 unsigned long flags;
2819 int ret;
2820
2821 id_priv = container_of(id, struct rdma_id_private, id);
2822 spin_lock_irqsave(&id_priv->lock, flags);
2823 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2824 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2825 id_priv->afonly = afonly;
2826 ret = 0;
2827 } else {
2828 ret = -EINVAL;
2829 }
2830 spin_unlock_irqrestore(&id_priv->lock, flags);
2831 return ret;
2832}
2833EXPORT_SYMBOL(rdma_set_afonly);
2834
Sean Heftye51060f2006-06-17 20:37:29 -07002835static void cma_bind_port(struct rdma_bind_list *bind_list,
2836 struct rdma_id_private *id_priv)
2837{
Sean Hefty58afdcb2013-05-29 10:09:11 -07002838 struct sockaddr *addr;
2839 struct sockaddr_ib *sib;
2840 u64 sid, mask;
2841 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07002842
Sean Heftyf4753832013-05-29 10:09:14 -07002843 addr = cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002844 port = htons(bind_list->port);
2845
2846 switch (addr->sa_family) {
2847 case AF_INET:
2848 ((struct sockaddr_in *) addr)->sin_port = port;
2849 break;
2850 case AF_INET6:
2851 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2852 break;
2853 case AF_IB:
2854 sib = (struct sockaddr_ib *) addr;
2855 sid = be64_to_cpu(sib->sib_sid);
2856 mask = be64_to_cpu(sib->sib_sid_mask);
2857 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2858 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2859 break;
2860 }
Sean Heftye51060f2006-06-17 20:37:29 -07002861 id_priv->bind_list = bind_list;
2862 hlist_add_head(&id_priv->node, &bind_list->owners);
2863}
2864
Haggai Eranaac978e2015-07-30 17:50:20 +03002865static int cma_alloc_port(enum rdma_port_space ps,
2866 struct rdma_id_private *id_priv, unsigned short snum)
Sean Heftye51060f2006-06-17 20:37:29 -07002867{
2868 struct rdma_bind_list *bind_list;
Tejun Heo3b069c52013-02-27 17:04:16 -08002869 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002870
Sean Heftycb164b82007-03-05 12:50:17 -08002871 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002872 if (!bind_list)
2873 return -ENOMEM;
2874
Guy Shapirofa201052015-10-22 15:20:10 +03002875 ret = cma_ps_alloc(id_priv->id.route.addr.dev_addr.net, ps, bind_list,
2876 snum);
Tejun Heo3b069c52013-02-27 17:04:16 -08002877 if (ret < 0)
2878 goto err;
Sean Heftye51060f2006-06-17 20:37:29 -07002879
2880 bind_list->ps = ps;
Tejun Heo3b069c52013-02-27 17:04:16 -08002881 bind_list->port = (unsigned short)ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002882 cma_bind_port(bind_list, id_priv);
2883 return 0;
Tejun Heo3b069c52013-02-27 17:04:16 -08002884err:
Sean Heftyaedec082007-01-29 16:41:23 -08002885 kfree(bind_list);
Tejun Heo3b069c52013-02-27 17:04:16 -08002886 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002887}
2888
Haggai Eranaac978e2015-07-30 17:50:20 +03002889static int cma_alloc_any_port(enum rdma_port_space ps,
2890 struct rdma_id_private *id_priv)
Sean Heftyaedec082007-01-29 16:41:23 -08002891{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002892 static unsigned int last_used_port;
2893 int low, high, remaining;
2894 unsigned int rover;
Guy Shapirofa201052015-10-22 15:20:10 +03002895 struct net *net = id_priv->id.route.addr.dev_addr.net;
Sean Heftyaedec082007-01-29 16:41:23 -08002896
Guy Shapirofa201052015-10-22 15:20:10 +03002897 inet_get_local_port_range(net, &low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002898 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002899 rover = prandom_u32() % remaining + low;
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002900retry:
2901 if (last_used_port != rover &&
Guy Shapirofa201052015-10-22 15:20:10 +03002902 !cma_ps_find(net, ps, (unsigned short)rover)) {
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002903 int ret = cma_alloc_port(ps, id_priv, rover);
2904 /*
2905 * Remember previously used port number in order to avoid
2906 * re-using same port immediately after it is closed.
2907 */
2908 if (!ret)
2909 last_used_port = rover;
2910 if (ret != -EADDRNOTAVAIL)
2911 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002912 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002913 if (--remaining) {
2914 rover++;
2915 if ((rover < low) || (rover > high))
2916 rover = low;
2917 goto retry;
2918 }
2919 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002920}
2921
Hefty, Seana9bb7912011-05-09 22:06:10 -07002922/*
2923 * Check that the requested port is available. This is called when trying to
2924 * bind to a specific port, or when trying to listen on a bound port. In
2925 * the latter case, the provided id_priv may already be on the bind_list, but
2926 * we still need to check that it's okay to start listening.
2927 */
2928static int cma_check_port(struct rdma_bind_list *bind_list,
2929 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002930{
2931 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002932 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002933
Sean Heftyf4753832013-05-29 10:09:14 -07002934 addr = cma_src_addr(id_priv);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002935 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002936 if (id_priv == cur_id)
2937 continue;
2938
Sean Hefty5b0ec992012-06-14 20:31:39 +00002939 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2940 cur_id->reuseaddr)
2941 continue;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002942
Sean Heftyf4753832013-05-29 10:09:14 -07002943 cur_addr = cma_src_addr(cur_id);
Sean Hefty5b0ec992012-06-14 20:31:39 +00002944 if (id_priv->afonly && cur_id->afonly &&
2945 (addr->sa_family != cur_addr->sa_family))
2946 continue;
2947
2948 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2949 return -EADDRNOTAVAIL;
2950
2951 if (!cma_addr_cmp(addr, cur_addr))
2952 return -EADDRINUSE;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002953 }
2954 return 0;
2955}
2956
Haggai Eranaac978e2015-07-30 17:50:20 +03002957static int cma_use_port(enum rdma_port_space ps,
2958 struct rdma_id_private *id_priv)
Hefty, Seana9bb7912011-05-09 22:06:10 -07002959{
2960 struct rdma_bind_list *bind_list;
2961 unsigned short snum;
2962 int ret;
2963
Sean Heftyf4753832013-05-29 10:09:14 -07002964 snum = ntohs(cma_port(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002965 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2966 return -EACCES;
2967
Guy Shapirofa201052015-10-22 15:20:10 +03002968 bind_list = cma_ps_find(id_priv->id.route.addr.dev_addr.net, ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002969 if (!bind_list) {
2970 ret = cma_alloc_port(ps, id_priv, snum);
2971 } else {
2972 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2973 if (!ret)
2974 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002975 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07002976 return ret;
2977}
Sean Heftye51060f2006-06-17 20:37:29 -07002978
Hefty, Seana9bb7912011-05-09 22:06:10 -07002979static int cma_bind_listen(struct rdma_id_private *id_priv)
2980{
2981 struct rdma_bind_list *bind_list = id_priv->bind_list;
2982 int ret = 0;
2983
2984 mutex_lock(&lock);
2985 if (bind_list->owners.first->next)
2986 ret = cma_check_port(bind_list, id_priv, 0);
2987 mutex_unlock(&lock);
2988 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002989}
2990
Haggai Eranaac978e2015-07-30 17:50:20 +03002991static enum rdma_port_space cma_select_inet_ps(
2992 struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002993{
2994 switch (id_priv->id.ps) {
Sean Hefty58afdcb2013-05-29 10:09:11 -07002995 case RDMA_PS_TCP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002996 case RDMA_PS_UDP:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002997 case RDMA_PS_IPOIB:
Sean Hefty58afdcb2013-05-29 10:09:11 -07002998 case RDMA_PS_IB:
Haggai Eranaac978e2015-07-30 17:50:20 +03002999 return id_priv->id.ps;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003000 default:
Haggai Eranaac978e2015-07-30 17:50:20 +03003001
3002 return 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003003 }
3004}
3005
Haggai Eranaac978e2015-07-30 17:50:20 +03003006static enum rdma_port_space cma_select_ib_ps(struct rdma_id_private *id_priv)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003007{
Haggai Eranaac978e2015-07-30 17:50:20 +03003008 enum rdma_port_space ps = 0;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003009 struct sockaddr_ib *sib;
3010 u64 sid_ps, mask, sid;
3011
Sean Heftyf4753832013-05-29 10:09:14 -07003012 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07003013 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
3014 sid = be64_to_cpu(sib->sib_sid) & mask;
3015
3016 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
3017 sid_ps = RDMA_IB_IP_PS_IB;
Haggai Eranaac978e2015-07-30 17:50:20 +03003018 ps = RDMA_PS_IB;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003019 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
3020 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
3021 sid_ps = RDMA_IB_IP_PS_TCP;
Haggai Eranaac978e2015-07-30 17:50:20 +03003022 ps = RDMA_PS_TCP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003023 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
3024 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
3025 sid_ps = RDMA_IB_IP_PS_UDP;
Haggai Eranaac978e2015-07-30 17:50:20 +03003026 ps = RDMA_PS_UDP;
Sean Hefty58afdcb2013-05-29 10:09:11 -07003027 }
3028
3029 if (ps) {
3030 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
3031 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
3032 be64_to_cpu(sib->sib_sid_mask));
3033 }
3034 return ps;
3035}
3036
Sean Heftye51060f2006-06-17 20:37:29 -07003037static int cma_get_port(struct rdma_id_private *id_priv)
3038{
Haggai Eranaac978e2015-07-30 17:50:20 +03003039 enum rdma_port_space ps;
Sean Heftye51060f2006-06-17 20:37:29 -07003040 int ret;
3041
Sean Heftyf4753832013-05-29 10:09:14 -07003042 if (cma_family(id_priv) != AF_IB)
Sean Hefty58afdcb2013-05-29 10:09:11 -07003043 ps = cma_select_inet_ps(id_priv);
3044 else
3045 ps = cma_select_ib_ps(id_priv);
3046 if (!ps)
Sean Heftye51060f2006-06-17 20:37:29 -07003047 return -EPROTONOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07003048
3049 mutex_lock(&lock);
Sean Heftyf4753832013-05-29 10:09:14 -07003050 if (cma_any_port(cma_src_addr(id_priv)))
Sean Heftyaedec082007-01-29 16:41:23 -08003051 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003052 else
3053 ret = cma_use_port(ps, id_priv);
3054 mutex_unlock(&lock);
3055
3056 return ret;
3057}
3058
Sean Heftyd14714d2009-11-19 16:46:25 -08003059static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
3060 struct sockaddr *addr)
3061{
Roland Dreierd90f9b32012-07-05 22:39:34 -07003062#if IS_ENABLED(CONFIG_IPV6)
Sean Heftyd14714d2009-11-19 16:46:25 -08003063 struct sockaddr_in6 *sin6;
3064
3065 if (addr->sa_family != AF_INET6)
3066 return 0;
3067
3068 sin6 = (struct sockaddr_in6 *) addr;
Somnath Kotur5462edd2013-11-16 09:30:01 +05303069
3070 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
3071 return 0;
3072
3073 if (!sin6->sin6_scope_id)
Sean Heftyd14714d2009-11-19 16:46:25 -08003074 return -EINVAL;
3075
3076 dev_addr->bound_dev_if = sin6->sin6_scope_id;
3077#endif
3078 return 0;
3079}
3080
Hefty, Seana9bb7912011-05-09 22:06:10 -07003081int rdma_listen(struct rdma_cm_id *id, int backlog)
3082{
3083 struct rdma_id_private *id_priv;
3084 int ret;
3085
3086 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003087 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftyf4753832013-05-29 10:09:14 -07003088 id->route.addr.src_addr.ss_family = AF_INET;
3089 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
Hefty, Seana9bb7912011-05-09 22:06:10 -07003090 if (ret)
3091 return ret;
3092 }
3093
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003094 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07003095 return -EINVAL;
3096
3097 if (id_priv->reuseaddr) {
3098 ret = cma_bind_listen(id_priv);
3099 if (ret)
3100 goto err;
3101 }
3102
3103 id_priv->backlog = backlog;
3104 if (id->device) {
Michael Wang72219cea2015-05-05 14:50:34 +02003105 if (rdma_cap_ib_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003106 ret = cma_ib_listen(id_priv);
3107 if (ret)
3108 goto err;
Michael Wang04215332015-05-05 14:50:35 +02003109 } else if (rdma_cap_iw_cm(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003110 ret = cma_iw_listen(id_priv, backlog);
3111 if (ret)
3112 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02003113 } else {
Hefty, Seana9bb7912011-05-09 22:06:10 -07003114 ret = -ENOSYS;
3115 goto err;
3116 }
3117 } else
3118 cma_listen_on_all(id_priv);
3119
3120 return 0;
3121err:
3122 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003123 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07003124 return ret;
3125}
3126EXPORT_SYMBOL(rdma_listen);
3127
Sean Heftye51060f2006-06-17 20:37:29 -07003128int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
3129{
3130 struct rdma_id_private *id_priv;
3131 int ret;
3132
Sean Hefty680f9202013-05-29 10:09:12 -07003133 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
3134 addr->sa_family != AF_IB)
Sean Heftye51060f2006-06-17 20:37:29 -07003135 return -EAFNOSUPPORT;
3136
3137 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003138 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07003139 return -EINVAL;
3140
Sean Heftyd14714d2009-11-19 16:46:25 -08003141 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
3142 if (ret)
3143 goto err1;
3144
Moni Shoua7b856272013-12-12 18:03:12 +02003145 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
Sean Hefty8523c042010-02-08 16:41:15 -08003146 if (!cma_any_addr(addr)) {
Sean Hefty680f9202013-05-29 10:09:12 -07003147 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07003148 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07003149 goto err1;
3150
Doug Ledfordbe9130c2013-09-24 17:16:28 -04003151 ret = cma_acquire_dev(id_priv, NULL);
Krishna Kumar255d0c12006-10-24 13:22:28 -07003152 if (ret)
3153 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07003154 }
3155
Sean Hefty68602122012-06-14 20:31:39 +00003156 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
3157 if (addr->sa_family == AF_INET)
3158 id_priv->afonly = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00003159#if IS_ENABLED(CONFIG_IPV6)
Guy Shapirofa201052015-10-22 15:20:10 +03003160 else if (addr->sa_family == AF_INET6) {
3161 struct net *net = id_priv->id.route.addr.dev_addr.net;
3162
3163 id_priv->afonly = net->ipv6.sysctl.bindv6only;
3164 }
Sean Hefty5b0ec992012-06-14 20:31:39 +00003165#endif
Sean Hefty68602122012-06-14 20:31:39 +00003166 }
Sean Heftye51060f2006-06-17 20:37:29 -07003167 ret = cma_get_port(id_priv);
3168 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07003169 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07003170
3171 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07003172err2:
Sean Heftya396d432011-02-23 09:05:39 -08003173 if (id_priv->cma_dev)
3174 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07003175err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003176 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07003177 return ret;
3178}
3179EXPORT_SYMBOL(rdma_bind_addr);
3180
Sean Heftyf4753832013-05-29 10:09:14 -07003181static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07003182{
Sean Heftye51060f2006-06-17 20:37:29 -07003183 struct cma_hdr *cma_hdr;
Sean Heftye51060f2006-06-17 20:37:29 -07003184
Sean Hefty01602f12013-05-29 10:09:20 -07003185 cma_hdr = hdr;
3186 cma_hdr->cma_version = CMA_VERSION;
Sean Heftyf4753832013-05-29 10:09:14 -07003187 if (cma_family(id_priv) == AF_INET) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003188 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07003189
Sean Heftyf4753832013-05-29 10:09:14 -07003190 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
3191 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003192
Sean Hefty01602f12013-05-29 10:09:20 -07003193 cma_set_ip_ver(cma_hdr, 4);
3194 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
3195 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
3196 cma_hdr->port = src4->sin_port;
Sean Heftye8160e12013-05-29 10:09:22 -07003197 } else if (cma_family(id_priv) == AF_INET6) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003198 struct sockaddr_in6 *src6, *dst6;
3199
Sean Heftyf4753832013-05-29 10:09:14 -07003200 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
3201 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08003202
Sean Hefty01602f12013-05-29 10:09:20 -07003203 cma_set_ip_ver(cma_hdr, 6);
3204 cma_hdr->src_addr.ip6 = src6->sin6_addr;
3205 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
3206 cma_hdr->port = src6->sin6_port;
Sean Heftye51060f2006-06-17 20:37:29 -07003207 }
3208 return 0;
3209}
3210
Sean Hefty628e5f62006-11-30 16:44:16 -08003211static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
3212 struct ib_cm_event *ib_event)
3213{
3214 struct rdma_id_private *id_priv = cm_id->context;
3215 struct rdma_cm_event event;
3216 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
3217 int ret = 0;
3218
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003219 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07003220 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08003221
Sean Hefty8aa08602007-05-07 11:49:00 -07003222 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08003223 switch (ib_event->event) {
3224 case IB_CM_SIDR_REQ_ERROR:
3225 event.event = RDMA_CM_EVENT_UNREACHABLE;
3226 event.status = -ETIMEDOUT;
3227 break;
3228 case IB_CM_SIDR_REP_RECEIVED:
3229 event.param.ud.private_data = ib_event->private_data;
3230 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
3231 if (rep->status != IB_SIDR_SUCCESS) {
3232 event.event = RDMA_CM_EVENT_UNREACHABLE;
3233 event.status = ib_event->param.sidr_rep_rcvd.status;
3234 break;
3235 }
Sean Hefty5c438132013-05-29 10:09:23 -07003236 ret = cma_set_qkey(id_priv, rep->qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003237 if (ret) {
3238 event.event = RDMA_CM_EVENT_ADDR_ERROR;
Sean Hefty5c438132013-05-29 10:09:23 -07003239 event.status = ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003240 break;
3241 }
3242 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
3243 id_priv->id.route.path_rec,
3244 &event.param.ud.ah_attr);
3245 event.param.ud.qp_num = rep->qpn;
3246 event.param.ud.qkey = rep->qkey;
3247 event.event = RDMA_CM_EVENT_ESTABLISHED;
3248 event.status = 0;
3249 break;
3250 default:
Parav Panditaba25a3e2016-03-02 00:50:29 +05303251 pr_err("RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08003252 ib_event->event);
3253 goto out;
3254 }
3255
3256 ret = id_priv->id.event_handler(&id_priv->id, &event);
3257 if (ret) {
3258 /* Destroy the CM ID by returning a non-zero value. */
3259 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003260 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003261 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003262 rdma_destroy_id(&id_priv->id);
3263 return ret;
3264 }
3265out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07003266 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08003267 return ret;
3268}
3269
3270static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
3271 struct rdma_conn_param *conn_param)
3272{
3273 struct ib_cm_sidr_req_param req;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003274 struct ib_cm_id *id;
Sean Heftye511d1a2013-07-24 15:06:08 -07003275 void *private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003276 int offset, ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003277
Sean Heftye511d1a2013-07-24 15:06:08 -07003278 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003279 offset = cma_user_data_offset(id_priv);
3280 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003281 if (req.private_data_len < conn_param->private_data_len)
3282 return -EINVAL;
3283
Sean Heftye8160e12013-05-29 10:09:22 -07003284 if (req.private_data_len) {
Sean Heftye511d1a2013-07-24 15:06:08 -07003285 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3286 if (!private_data)
Sean Heftye8160e12013-05-29 10:09:22 -07003287 return -ENOMEM;
3288 } else {
Sean Heftye511d1a2013-07-24 15:06:08 -07003289 private_data = NULL;
Sean Heftye8160e12013-05-29 10:09:22 -07003290 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003291
3292 if (conn_param->private_data && conn_param->private_data_len)
Sean Heftye511d1a2013-07-24 15:06:08 -07003293 memcpy(private_data + offset, conn_param->private_data,
3294 conn_param->private_data_len);
Sean Hefty628e5f62006-11-30 16:44:16 -08003295
Sean Heftye511d1a2013-07-24 15:06:08 -07003296 if (private_data) {
3297 ret = cma_format_hdr(private_data, id_priv);
Sean Heftye8160e12013-05-29 10:09:22 -07003298 if (ret)
3299 goto out;
Sean Heftye511d1a2013-07-24 15:06:08 -07003300 req.private_data = private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07003301 }
Sean Hefty628e5f62006-11-30 16:44:16 -08003302
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003303 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
3304 id_priv);
3305 if (IS_ERR(id)) {
3306 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08003307 goto out;
3308 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003309 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08003310
Sean Heftyf4753832013-05-29 10:09:14 -07003311 req.path = id_priv->id.route.path_rec;
Sean Heftycf539362013-05-29 10:09:28 -07003312 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Hefty628e5f62006-11-30 16:44:16 -08003313 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
3314 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3315
3316 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
3317 if (ret) {
3318 ib_destroy_cm_id(id_priv->cm_id.ib);
3319 id_priv->cm_id.ib = NULL;
3320 }
3321out:
Sean Heftye511d1a2013-07-24 15:06:08 -07003322 kfree(private_data);
Sean Hefty628e5f62006-11-30 16:44:16 -08003323 return ret;
3324}
3325
Sean Heftye51060f2006-06-17 20:37:29 -07003326static int cma_connect_ib(struct rdma_id_private *id_priv,
3327 struct rdma_conn_param *conn_param)
3328{
3329 struct ib_cm_req_param req;
3330 struct rdma_route *route;
3331 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003332 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07003333 int offset, ret;
3334
3335 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07003336 offset = cma_user_data_offset(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003337 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00003338 if (req.private_data_len < conn_param->private_data_len)
3339 return -EINVAL;
3340
Sean Heftye8160e12013-05-29 10:09:22 -07003341 if (req.private_data_len) {
3342 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
3343 if (!private_data)
3344 return -ENOMEM;
3345 } else {
3346 private_data = NULL;
3347 }
Sean Heftye51060f2006-06-17 20:37:29 -07003348
3349 if (conn_param->private_data && conn_param->private_data_len)
3350 memcpy(private_data + offset, conn_param->private_data,
3351 conn_param->private_data_len);
3352
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003353 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
3354 if (IS_ERR(id)) {
3355 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07003356 goto out;
3357 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003358 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07003359
3360 route = &id_priv->id.route;
Sean Heftye8160e12013-05-29 10:09:22 -07003361 if (private_data) {
3362 ret = cma_format_hdr(private_data, id_priv);
3363 if (ret)
3364 goto out;
3365 req.private_data = private_data;
3366 }
Sean Heftye51060f2006-06-17 20:37:29 -07003367
3368 req.primary_path = &route->path_rec[0];
3369 if (route->num_paths == 2)
3370 req.alternate_path = &route->path_rec[1];
3371
Sean Heftycf539362013-05-29 10:09:28 -07003372 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07003373 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07003374 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07003375 req.starting_psn = id_priv->seq_num;
3376 req.responder_resources = conn_param->responder_resources;
3377 req.initiator_depth = conn_param->initiator_depth;
3378 req.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003379 req.retry_count = min_t(u8, 7, conn_param->retry_count);
3380 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003381 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3382 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
3383 req.max_cm_retries = CMA_MAX_CM_RETRIES;
3384 req.srq = id_priv->srq ? 1 : 0;
3385
3386 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
3387out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003388 if (ret && !IS_ERR(id)) {
3389 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07003390 id_priv->cm_id.ib = NULL;
3391 }
3392
Sean Heftye51060f2006-06-17 20:37:29 -07003393 kfree(private_data);
3394 return ret;
3395}
3396
Tom Tucker07ebafb2006-08-03 16:02:42 -05003397static int cma_connect_iw(struct rdma_id_private *id_priv,
3398 struct rdma_conn_param *conn_param)
3399{
3400 struct iw_cm_id *cm_id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003401 int ret;
3402 struct iw_cm_conn_param iw_param;
3403
3404 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003405 if (IS_ERR(cm_id))
3406 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003407
Steve Wise68cdba02015-05-18 15:27:10 -05003408 cm_id->tos = id_priv->tos;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003409 id_priv->cm_id.iw = cm_id;
3410
Steve Wise24d44a32013-07-04 16:10:44 +05303411 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
3412 rdma_addr_size(cma_src_addr(id_priv)));
3413 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
3414 rdma_addr_size(cma_dst_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05003415
Sean Hefty5851bb82008-01-04 10:47:12 -08003416 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07003417 if (ret)
3418 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05003419
Hefty, Seanf45ee802011-10-06 09:33:04 -07003420 if (conn_param) {
3421 iw_param.ord = conn_param->initiator_depth;
3422 iw_param.ird = conn_param->responder_resources;
3423 iw_param.private_data = conn_param->private_data;
3424 iw_param.private_data_len = conn_param->private_data_len;
3425 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
3426 } else {
3427 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003428 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07003429 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003430 ret = iw_cm_connect(cm_id, &iw_param);
3431out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003432 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07003433 iw_destroy_cm_id(cm_id);
3434 id_priv->cm_id.iw = NULL;
3435 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05003436 return ret;
3437}
3438
Sean Heftye51060f2006-06-17 20:37:29 -07003439int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3440{
3441 struct rdma_id_private *id_priv;
3442 int ret;
3443
3444 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003445 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003446 return -EINVAL;
3447
3448 if (!id->qp) {
3449 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003450 id_priv->srq = conn_param->srq;
3451 }
3452
Michael Wang72219cea2015-05-05 14:50:34 +02003453 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003454 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08003455 ret = cma_resolve_ib_udp(id_priv, conn_param);
3456 else
3457 ret = cma_connect_ib(id_priv, conn_param);
Michael Wang04215332015-05-05 14:50:35 +02003458 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003459 ret = cma_connect_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003460 else
Sean Heftye51060f2006-06-17 20:37:29 -07003461 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003462 if (ret)
3463 goto err;
3464
3465 return 0;
3466err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003467 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07003468 return ret;
3469}
3470EXPORT_SYMBOL(rdma_connect);
3471
3472static int cma_accept_ib(struct rdma_id_private *id_priv,
3473 struct rdma_conn_param *conn_param)
3474{
3475 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08003476 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003477
Sean Hefty5851bb82008-01-04 10:47:12 -08003478 ret = cma_modify_qp_rtr(id_priv, conn_param);
3479 if (ret)
3480 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08003481
Sean Hefty5851bb82008-01-04 10:47:12 -08003482 ret = cma_modify_qp_rts(id_priv, conn_param);
3483 if (ret)
3484 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003485
3486 memset(&rep, 0, sizeof rep);
3487 rep.qp_num = id_priv->qp_num;
3488 rep.starting_psn = id_priv->seq_num;
3489 rep.private_data = conn_param->private_data;
3490 rep.private_data_len = conn_param->private_data_len;
3491 rep.responder_resources = conn_param->responder_resources;
3492 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07003493 rep.failover_accepted = 0;
3494 rep.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00003495 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07003496 rep.srq = id_priv->srq ? 1 : 0;
3497
Sean Hefty0fe313b2006-11-30 16:37:15 -08003498 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
3499out:
3500 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003501}
3502
Tom Tucker07ebafb2006-08-03 16:02:42 -05003503static int cma_accept_iw(struct rdma_id_private *id_priv,
3504 struct rdma_conn_param *conn_param)
3505{
3506 struct iw_cm_conn_param iw_param;
3507 int ret;
3508
Sean Hefty5851bb82008-01-04 10:47:12 -08003509 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003510 if (ret)
3511 return ret;
3512
3513 iw_param.ord = conn_param->initiator_depth;
3514 iw_param.ird = conn_param->responder_resources;
3515 iw_param.private_data = conn_param->private_data;
3516 iw_param.private_data_len = conn_param->private_data_len;
3517 if (id_priv->id.qp) {
3518 iw_param.qpn = id_priv->qp_num;
3519 } else
3520 iw_param.qpn = conn_param->qp_num;
3521
3522 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
3523}
3524
Sean Hefty628e5f62006-11-30 16:44:16 -08003525static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
Sean Hefty5c438132013-05-29 10:09:23 -07003526 enum ib_cm_sidr_status status, u32 qkey,
Sean Hefty628e5f62006-11-30 16:44:16 -08003527 const void *private_data, int private_data_len)
3528{
3529 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003530 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003531
3532 memset(&rep, 0, sizeof rep);
3533 rep.status = status;
3534 if (status == IB_SIDR_SUCCESS) {
Sean Hefty5c438132013-05-29 10:09:23 -07003535 ret = cma_set_qkey(id_priv, qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07003536 if (ret)
3537 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08003538 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003539 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08003540 }
3541 rep.private_data = private_data;
3542 rep.private_data_len = private_data_len;
3543
3544 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
3545}
3546
Sean Heftye51060f2006-06-17 20:37:29 -07003547int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
3548{
3549 struct rdma_id_private *id_priv;
3550 int ret;
3551
3552 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00003553
3554 id_priv->owner = task_pid_nr(current);
3555
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003556 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07003557 return -EINVAL;
3558
3559 if (!id->qp && conn_param) {
3560 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07003561 id_priv->srq = conn_param->srq;
3562 }
3563
Michael Wang72219cea2015-05-05 14:50:34 +02003564 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Hefty, Seanf45ee802011-10-06 09:33:04 -07003565 if (id->qp_type == IB_QPT_UD) {
3566 if (conn_param)
3567 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003568 conn_param->qkey,
Hefty, Seanf45ee802011-10-06 09:33:04 -07003569 conn_param->private_data,
3570 conn_param->private_data_len);
3571 else
3572 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003573 0, NULL, 0);
Hefty, Seanf45ee802011-10-06 09:33:04 -07003574 } else {
3575 if (conn_param)
3576 ret = cma_accept_ib(id_priv, conn_param);
3577 else
3578 ret = cma_rep_recv(id_priv);
3579 }
Michael Wang04215332015-05-05 14:50:35 +02003580 } else if (rdma_cap_iw_cm(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003581 ret = cma_accept_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003582 else
Sean Heftye51060f2006-06-17 20:37:29 -07003583 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003584
3585 if (ret)
3586 goto reject;
3587
3588 return 0;
3589reject:
Sean Heftyc5483382007-09-24 13:19:09 -07003590 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003591 rdma_reject(id, NULL, 0);
3592 return ret;
3593}
3594EXPORT_SYMBOL(rdma_accept);
3595
Sean Hefty0fe313b2006-11-30 16:37:15 -08003596int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3597{
3598 struct rdma_id_private *id_priv;
3599 int ret;
3600
3601 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003602 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08003603 return -EINVAL;
3604
3605 switch (id->device->node_type) {
3606 case RDMA_NODE_IB_CA:
3607 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3608 break;
3609 default:
3610 ret = 0;
3611 break;
3612 }
3613 return ret;
3614}
3615EXPORT_SYMBOL(rdma_notify);
3616
Sean Heftye51060f2006-06-17 20:37:29 -07003617int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3618 u8 private_data_len)
3619{
3620 struct rdma_id_private *id_priv;
3621 int ret;
3622
3623 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003624 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003625 return -EINVAL;
3626
Michael Wang72219cea2015-05-05 14:50:34 +02003627 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003628 if (id->qp_type == IB_QPT_UD)
Sean Hefty5c438132013-05-29 10:09:23 -07003629 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
Sean Hefty628e5f62006-11-30 16:44:16 -08003630 private_data, private_data_len);
3631 else
3632 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3633 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3634 0, private_data, private_data_len);
Michael Wang04215332015-05-05 14:50:35 +02003635 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003636 ret = iw_cm_reject(id_priv->cm_id.iw,
3637 private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003638 } else
Sean Heftye51060f2006-06-17 20:37:29 -07003639 ret = -ENOSYS;
Michael Wang21655af2015-05-05 14:50:27 +02003640
Sean Heftye51060f2006-06-17 20:37:29 -07003641 return ret;
3642}
3643EXPORT_SYMBOL(rdma_reject);
3644
3645int rdma_disconnect(struct rdma_cm_id *id)
3646{
3647 struct rdma_id_private *id_priv;
3648 int ret;
3649
3650 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003651 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003652 return -EINVAL;
3653
Michael Wang72219cea2015-05-05 14:50:34 +02003654 if (rdma_cap_ib_cm(id->device, id->port_num)) {
Sean Heftyc5483382007-09-24 13:19:09 -07003655 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003656 if (ret)
3657 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003658 /* Initiate or respond to a disconnect. */
3659 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3660 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
Michael Wang04215332015-05-05 14:50:35 +02003661 } else if (rdma_cap_iw_cm(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003662 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003663 } else
Tom Tucker07ebafb2006-08-03 16:02:42 -05003664 ret = -EINVAL;
Michael Wang21655af2015-05-05 14:50:27 +02003665
Sean Heftye51060f2006-06-17 20:37:29 -07003666out:
3667 return ret;
3668}
3669EXPORT_SYMBOL(rdma_disconnect);
3670
Sean Heftyc8f6a362007-02-15 17:00:18 -08003671static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3672{
3673 struct rdma_id_private *id_priv;
3674 struct cma_multicast *mc = multicast->context;
3675 struct rdma_cm_event event;
3676 int ret;
3677
3678 id_priv = mc->id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003679 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
3680 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07003681 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003682
Sean Hefty5c438132013-05-29 10:09:23 -07003683 if (!status)
3684 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
Sean Heftyc5483382007-09-24 13:19:09 -07003685 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003686 if (!status && id_priv->id.qp)
3687 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003688 be16_to_cpu(multicast->rec.mlid));
Sean Heftyc5483382007-09-24 13:19:09 -07003689 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003690
3691 memset(&event, 0, sizeof event);
3692 event.status = status;
3693 event.param.ud.private_data = mc->context;
3694 if (!status) {
Moni Shouabee3c3c2015-12-23 14:56:57 +02003695 struct rdma_dev_addr *dev_addr =
3696 &id_priv->id.route.addr.dev_addr;
3697 struct net_device *ndev =
3698 dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3699 enum ib_gid_type gid_type =
3700 id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3701 rdma_start_port(id_priv->cma_dev->device)];
3702
Sean Heftyc8f6a362007-02-15 17:00:18 -08003703 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3704 ib_init_ah_from_mcmember(id_priv->id.device,
3705 id_priv->id.port_num, &multicast->rec,
Moni Shouabee3c3c2015-12-23 14:56:57 +02003706 ndev, gid_type,
Sean Heftyc8f6a362007-02-15 17:00:18 -08003707 &event.param.ud.ah_attr);
3708 event.param.ud.qp_num = 0xFFFFFF;
3709 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
Moni Shouabee3c3c2015-12-23 14:56:57 +02003710 if (ndev)
3711 dev_put(ndev);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003712 } else
3713 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3714
3715 ret = id_priv->id.event_handler(&id_priv->id, &event);
3716 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003717 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003718 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003719 rdma_destroy_id(&id_priv->id);
3720 return 0;
3721 }
Sean Hefty8aa08602007-05-07 11:49:00 -07003722
Or Gerlitzde910bd2008-07-14 23:48:53 -07003723 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003724 return 0;
3725}
3726
3727static void cma_set_mgid(struct rdma_id_private *id_priv,
3728 struct sockaddr *addr, union ib_gid *mgid)
3729{
3730 unsigned char mc_map[MAX_ADDR_LEN];
3731 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3732 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3733 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3734
3735 if (cma_any_addr(addr)) {
3736 memset(mgid, 0, sizeof *mgid);
3737 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08003738 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08003739 0xFF10A01B)) {
3740 /* IPv6 address is an SA assigned MGID. */
3741 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003742 } else if (addr->sa_family == AF_IB) {
3743 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08003744 } else if ((addr->sa_family == AF_INET6)) {
3745 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3746 if (id_priv->id.ps == RDMA_PS_UDP)
3747 mc_map[7] = 0x01; /* Use RDMA CM signature */
3748 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003749 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07003750 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003751 if (id_priv->id.ps == RDMA_PS_UDP)
3752 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08003753 *mgid = *(union ib_gid *) (mc_map + 4);
3754 }
3755}
3756
3757static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3758 struct cma_multicast *mc)
3759{
3760 struct ib_sa_mcmember_rec rec;
3761 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3762 ib_sa_comp_mask comp_mask;
3763 int ret;
3764
3765 ib_addr_get_mgid(dev_addr, &rec.mgid);
3766 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3767 &rec.mgid, &rec);
3768 if (ret)
3769 return ret;
3770
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003771 ret = cma_set_qkey(id_priv, 0);
3772 if (ret)
3773 return ret;
3774
Roland Dreier3f446752008-08-04 11:02:14 -07003775 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003776 rec.qkey = cpu_to_be32(id_priv->qkey);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003777 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003778 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3779 rec.join_state = 1;
3780
3781 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3782 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3783 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3784 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3785 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3786
Yossi Etigin84adeee2009-04-01 13:55:32 -07003787 if (id_priv->id.ps == RDMA_PS_IPOIB)
3788 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
Dotan Barak2a22fb82012-08-30 09:09:55 +00003789 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3790 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3791 IB_SA_MCMEMBER_REC_MTU |
3792 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Yossi Etigin84adeee2009-04-01 13:55:32 -07003793
Sean Heftyc8f6a362007-02-15 17:00:18 -08003794 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3795 id_priv->id.port_num, &rec,
3796 comp_mask, GFP_KERNEL,
3797 cma_ib_mc_handler, mc);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303798 return PTR_ERR_OR_ZERO(mc->multicast.ib);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003799}
3800
Eli Cohen3c86aa72010-10-13 21:26:51 +02003801static void iboe_mcast_work_handler(struct work_struct *work)
3802{
3803 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3804 struct cma_multicast *mc = mw->mc;
3805 struct ib_sa_multicast *m = mc->multicast.ib;
3806
3807 mc->multicast.ib->context = mc;
3808 cma_ib_mc_handler(0, m);
3809 kref_put(&mc->mcref, release_mc);
3810 kfree(mw);
3811}
3812
3813static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3814{
3815 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3816 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3817
3818 if (cma_any_addr(addr)) {
3819 memset(mgid, 0, sizeof *mgid);
3820 } else if (addr->sa_family == AF_INET6) {
3821 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3822 } else {
3823 mgid->raw[0] = 0xff;
3824 mgid->raw[1] = 0x0e;
3825 mgid->raw[2] = 0;
3826 mgid->raw[3] = 0;
3827 mgid->raw[4] = 0;
3828 mgid->raw[5] = 0;
3829 mgid->raw[6] = 0;
3830 mgid->raw[7] = 0;
3831 mgid->raw[8] = 0;
3832 mgid->raw[9] = 0;
3833 mgid->raw[10] = 0xff;
3834 mgid->raw[11] = 0xff;
3835 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3836 }
3837}
3838
3839static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3840 struct cma_multicast *mc)
3841{
3842 struct iboe_mcast_work *work;
3843 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Moni Shouabee3c3c2015-12-23 14:56:57 +02003844 int err = 0;
Eli Cohen3c86aa72010-10-13 21:26:51 +02003845 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3846 struct net_device *ndev = NULL;
Moni Shouabee3c3c2015-12-23 14:56:57 +02003847 enum ib_gid_type gid_type;
Eli Cohen3c86aa72010-10-13 21:26:51 +02003848
3849 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3850 return -EINVAL;
3851
3852 work = kzalloc(sizeof *work, GFP_KERNEL);
3853 if (!work)
3854 return -ENOMEM;
3855
3856 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3857 if (!mc->multicast.ib) {
3858 err = -ENOMEM;
3859 goto out1;
3860 }
3861
3862 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3863
3864 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3865 if (id_priv->id.ps == RDMA_PS_UDP)
3866 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3867
3868 if (dev_addr->bound_dev_if)
3869 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3870 if (!ndev) {
3871 err = -ENODEV;
3872 goto out2;
3873 }
3874 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3875 mc->multicast.ib->rec.hop_limit = 1;
3876 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
Moni Shouabee3c3c2015-12-23 14:56:57 +02003877
3878 gid_type = id_priv->cma_dev->default_gid_type[id_priv->id.port_num -
3879 rdma_start_port(id_priv->cma_dev->device)];
3880 if (addr->sa_family == AF_INET) {
3881 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
3882 err = cma_igmp_send(ndev, &mc->multicast.ib->rec.mgid,
3883 true);
3884 if (!err) {
3885 mc->igmp_joined = true;
3886 mc->multicast.ib->rec.hop_limit = IPV6_DEFAULT_HOPLIMIT;
3887 }
3888 } else {
3889 if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
3890 err = -ENOTSUPP;
3891 }
Eli Cohen3c86aa72010-10-13 21:26:51 +02003892 dev_put(ndev);
Moni Shouabee3c3c2015-12-23 14:56:57 +02003893 if (err || !mc->multicast.ib->rec.mtu) {
3894 if (!err)
3895 err = -EINVAL;
Eli Cohen3c86aa72010-10-13 21:26:51 +02003896 goto out2;
3897 }
Moni Shoua7b856272013-12-12 18:03:12 +02003898 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3899 &mc->multicast.ib->rec.port_gid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003900 work->id = id_priv;
3901 work->mc = mc;
3902 INIT_WORK(&work->work, iboe_mcast_work_handler);
3903 kref_get(&mc->mcref);
3904 queue_work(cma_wq, &work->work);
3905
3906 return 0;
3907
3908out2:
3909 kfree(mc->multicast.ib);
3910out1:
3911 kfree(work);
3912 return err;
3913}
3914
Sean Heftyc8f6a362007-02-15 17:00:18 -08003915int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3916 void *context)
3917{
3918 struct rdma_id_private *id_priv;
3919 struct cma_multicast *mc;
3920 int ret;
3921
3922 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003923 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3924 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08003925 return -EINVAL;
3926
3927 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3928 if (!mc)
3929 return -ENOMEM;
3930
Sean Heftyef560862013-05-29 10:09:10 -07003931 memcpy(&mc->addr, addr, rdma_addr_size(addr));
Sean Heftyc8f6a362007-02-15 17:00:18 -08003932 mc->context = context;
3933 mc->id_priv = id_priv;
Moni Shouabee3c3c2015-12-23 14:56:57 +02003934 mc->igmp_joined = false;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003935 spin_lock(&id_priv->lock);
3936 list_add(&mc->list, &id_priv->mc_list);
3937 spin_unlock(&id_priv->lock);
3938
Ira Weiny5d9fb042015-05-14 15:01:46 -04003939 if (rdma_protocol_roce(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003940 kref_init(&mc->mcref);
3941 ret = cma_iboe_join_multicast(id_priv, mc);
Michael Wanga31ad3b2015-05-05 14:50:37 +02003942 } else if (rdma_cap_ib_mcast(id->device, id->port_num))
Michael Wang5c9a5282015-05-05 14:50:29 +02003943 ret = cma_join_ib_multicast(id_priv, mc);
3944 else
Sean Heftyc8f6a362007-02-15 17:00:18 -08003945 ret = -ENOSYS;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003946
3947 if (ret) {
3948 spin_lock_irq(&id_priv->lock);
3949 list_del(&mc->list);
3950 spin_unlock_irq(&id_priv->lock);
3951 kfree(mc);
3952 }
3953 return ret;
3954}
3955EXPORT_SYMBOL(rdma_join_multicast);
3956
3957void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3958{
3959 struct rdma_id_private *id_priv;
3960 struct cma_multicast *mc;
3961
3962 id_priv = container_of(id, struct rdma_id_private, id);
3963 spin_lock_irq(&id_priv->lock);
3964 list_for_each_entry(mc, &id_priv->mc_list, list) {
Sean Heftyef560862013-05-29 10:09:10 -07003965 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
Sean Heftyc8f6a362007-02-15 17:00:18 -08003966 list_del(&mc->list);
3967 spin_unlock_irq(&id_priv->lock);
3968
3969 if (id->qp)
3970 ib_detach_mcast(id->qp,
3971 &mc->multicast.ib->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003972 be16_to_cpu(mc->multicast.ib->rec.mlid));
Michael Wang5c9a5282015-05-05 14:50:29 +02003973
3974 BUG_ON(id_priv->cma_dev->device != id->device);
3975
Michael Wanga31ad3b2015-05-05 14:50:37 +02003976 if (rdma_cap_ib_mcast(id->device, id->port_num)) {
Michael Wang5c9a5282015-05-05 14:50:29 +02003977 ib_sa_free_multicast(mc->multicast.ib);
3978 kfree(mc);
Moni Shouabee3c3c2015-12-23 14:56:57 +02003979 } else if (rdma_protocol_roce(id->device, id->port_num)) {
3980 if (mc->igmp_joined) {
3981 struct rdma_dev_addr *dev_addr =
3982 &id->route.addr.dev_addr;
3983 struct net_device *ndev = NULL;
Michael Wang5c9a5282015-05-05 14:50:29 +02003984
Moni Shouabee3c3c2015-12-23 14:56:57 +02003985 if (dev_addr->bound_dev_if)
3986 ndev = dev_get_by_index(&init_net,
3987 dev_addr->bound_dev_if);
3988 if (ndev) {
3989 cma_igmp_send(ndev,
3990 &mc->multicast.ib->rec.mgid,
3991 false);
3992 dev_put(ndev);
3993 }
3994 mc->igmp_joined = false;
3995 }
3996 kref_put(&mc->mcref, release_mc);
3997 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003998 return;
3999 }
4000 }
4001 spin_unlock_irq(&id_priv->lock);
4002}
4003EXPORT_SYMBOL(rdma_leave_multicast);
4004
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004005static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
4006{
4007 struct rdma_dev_addr *dev_addr;
4008 struct cma_ndev_work *work;
4009
4010 dev_addr = &id_priv->id.route.addr.dev_addr;
4011
Sean Hefty6266ed62009-11-19 12:55:22 -08004012 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Guy Shapirofa201052015-10-22 15:20:10 +03004013 (net_eq(dev_net(ndev), dev_addr->net)) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004014 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
Parav Panditaba25a3e2016-03-02 00:50:29 +05304015 pr_info("RDMA CM addr change for ndev %s used by id %p\n",
4016 ndev->name, &id_priv->id);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004017 work = kzalloc(sizeof *work, GFP_KERNEL);
4018 if (!work)
4019 return -ENOMEM;
4020
4021 INIT_WORK(&work->work, cma_ndev_work_handler);
4022 work->id = id_priv;
4023 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
4024 atomic_inc(&id_priv->refcount);
4025 queue_work(cma_wq, &work->work);
4026 }
4027
4028 return 0;
4029}
4030
4031static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00004032 void *ptr)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004033{
Jiri Pirko351638e2013-05-28 01:30:21 +00004034 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004035 struct cma_device *cma_dev;
4036 struct rdma_id_private *id_priv;
4037 int ret = NOTIFY_DONE;
4038
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004039 if (event != NETDEV_BONDING_FAILOVER)
4040 return NOTIFY_DONE;
4041
4042 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
4043 return NOTIFY_DONE;
4044
4045 mutex_lock(&lock);
4046 list_for_each_entry(cma_dev, &dev_list, list)
4047 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4048 ret = cma_netdev_change(ndev, id_priv);
4049 if (ret)
4050 goto out;
4051 }
4052
4053out:
4054 mutex_unlock(&lock);
4055 return ret;
4056}
4057
4058static struct notifier_block cma_nb = {
4059 .notifier_call = cma_netdev_callback
4060};
4061
Sean Heftye51060f2006-06-17 20:37:29 -07004062static void cma_add_one(struct ib_device *device)
4063{
4064 struct cma_device *cma_dev;
4065 struct rdma_id_private *id_priv;
Matan Barak045959d2015-12-23 14:56:55 +02004066 unsigned int i;
4067 unsigned long supported_gids = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07004068
4069 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
4070 if (!cma_dev)
4071 return;
4072
4073 cma_dev->device = device;
Matan Barak045959d2015-12-23 14:56:55 +02004074 cma_dev->default_gid_type = kcalloc(device->phys_port_cnt,
4075 sizeof(*cma_dev->default_gid_type),
4076 GFP_KERNEL);
4077 if (!cma_dev->default_gid_type) {
4078 kfree(cma_dev);
4079 return;
4080 }
4081 for (i = rdma_start_port(device); i <= rdma_end_port(device); i++) {
4082 supported_gids = roce_gid_type_mask_support(device, i);
4083 WARN_ON(!supported_gids);
4084 cma_dev->default_gid_type[i - rdma_start_port(device)] =
4085 find_first_bit(&supported_gids, BITS_PER_LONG);
4086 }
Sean Heftye51060f2006-06-17 20:37:29 -07004087
4088 init_completion(&cma_dev->comp);
4089 atomic_set(&cma_dev->refcount, 1);
4090 INIT_LIST_HEAD(&cma_dev->id_list);
4091 ib_set_client_data(device, &cma_client, cma_dev);
4092
4093 mutex_lock(&lock);
4094 list_add_tail(&cma_dev->list, &dev_list);
4095 list_for_each_entry(id_priv, &listen_any_list, list)
4096 cma_listen_on_dev(id_priv, cma_dev);
4097 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07004098}
4099
4100static int cma_remove_id_dev(struct rdma_id_private *id_priv)
4101{
Sean Heftya1b1b612006-11-30 16:33:14 -08004102 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004103 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07004104 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07004105
4106 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004107 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
4108 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07004109 return 0;
4110
4111 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07004112 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07004113
4114 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07004115 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07004116 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07004117
Sean Heftya1b1b612006-11-30 16:33:14 -08004118 memset(&event, 0, sizeof event);
4119 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07004120 ret = id_priv->id.event_handler(&id_priv->id, &event);
4121out:
4122 mutex_unlock(&id_priv->handler_mutex);
4123 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07004124}
4125
4126static void cma_process_remove(struct cma_device *cma_dev)
4127{
Sean Heftye51060f2006-06-17 20:37:29 -07004128 struct rdma_id_private *id_priv;
4129 int ret;
4130
Sean Heftye51060f2006-06-17 20:37:29 -07004131 mutex_lock(&lock);
4132 while (!list_empty(&cma_dev->id_list)) {
4133 id_priv = list_entry(cma_dev->id_list.next,
4134 struct rdma_id_private, list);
4135
Sean Heftyd02d1f52007-10-09 11:12:34 -07004136 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07004137 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07004138 atomic_inc(&id_priv->refcount);
4139 mutex_unlock(&lock);
4140
Sean Heftyd02d1f52007-10-09 11:12:34 -07004141 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07004142 cma_deref_id(id_priv);
4143 if (ret)
4144 rdma_destroy_id(&id_priv->id);
4145
4146 mutex_lock(&lock);
4147 }
4148 mutex_unlock(&lock);
4149
4150 cma_deref_dev(cma_dev);
4151 wait_for_completion(&cma_dev->comp);
4152}
4153
Haggai Eran7c1eb452015-07-30 17:50:14 +03004154static void cma_remove_one(struct ib_device *device, void *client_data)
Sean Heftye51060f2006-06-17 20:37:29 -07004155{
Haggai Eran7c1eb452015-07-30 17:50:14 +03004156 struct cma_device *cma_dev = client_data;
Sean Heftye51060f2006-06-17 20:37:29 -07004157
Sean Heftye51060f2006-06-17 20:37:29 -07004158 if (!cma_dev)
4159 return;
4160
4161 mutex_lock(&lock);
4162 list_del(&cma_dev->list);
4163 mutex_unlock(&lock);
4164
4165 cma_process_remove(cma_dev);
Matan Barak045959d2015-12-23 14:56:55 +02004166 kfree(cma_dev->default_gid_type);
Sean Heftye51060f2006-06-17 20:37:29 -07004167 kfree(cma_dev);
4168}
4169
Nir Muchtar753f6182011-01-03 15:33:53 +00004170static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
4171{
4172 struct nlmsghdr *nlh;
4173 struct rdma_cm_id_stats *id_stats;
4174 struct rdma_id_private *id_priv;
4175 struct rdma_cm_id *id = NULL;
4176 struct cma_device *cma_dev;
4177 int i_dev = 0, i_id = 0;
4178
4179 /*
4180 * We export all of the IDs as a sequence of messages. Each
4181 * ID gets its own netlink message.
4182 */
4183 mutex_lock(&lock);
4184
4185 list_for_each_entry(cma_dev, &dev_list, list) {
4186 if (i_dev < cb->args[0]) {
4187 i_dev++;
4188 continue;
4189 }
4190
4191 i_id = 0;
4192 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
4193 if (i_id < cb->args[1]) {
4194 i_id++;
4195 continue;
4196 }
4197
4198 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
4199 sizeof *id_stats, RDMA_NL_RDMA_CM,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -05004200 RDMA_NL_RDMA_CM_ID_STATS,
4201 NLM_F_MULTI);
Nir Muchtar753f6182011-01-03 15:33:53 +00004202 if (!id_stats)
4203 goto out;
4204
4205 memset(id_stats, 0, sizeof *id_stats);
4206 id = &id_priv->id;
4207 id_stats->node_type = id->route.addr.dev_addr.dev_type;
4208 id_stats->port_num = id->port_num;
4209 id_stats->bound_dev_if =
4210 id->route.addr.dev_addr.bound_dev_if;
4211
Sean Heftyce117ff2013-05-29 10:09:34 -07004212 if (ibnl_put_attr(skb, nlh,
4213 rdma_addr_size(cma_src_addr(id_priv)),
4214 cma_src_addr(id_priv),
4215 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
4216 goto out;
4217 if (ibnl_put_attr(skb, nlh,
4218 rdma_addr_size(cma_src_addr(id_priv)),
4219 cma_dst_addr(id_priv),
4220 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
4221 goto out;
Nir Muchtar753f6182011-01-03 15:33:53 +00004222
Nir Muchtar83e95022011-01-13 13:56:04 +00004223 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00004224 id_stats->port_space = id->ps;
4225 id_stats->cm_state = id_priv->state;
4226 id_stats->qp_num = id_priv->qp_num;
4227 id_stats->qp_type = id->qp_type;
4228
4229 i_id++;
4230 }
4231
4232 cb->args[1] = 0;
4233 i_dev++;
4234 }
4235
4236out:
4237 mutex_unlock(&lock);
4238 cb->args[0] = i_dev;
4239 cb->args[1] = i_id;
4240
4241 return skb->len;
4242}
4243
4244static const struct ibnl_client_cbs cma_cb_table[] = {
Gao feng809d5fc2012-10-04 20:15:49 +00004245 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
4246 .module = THIS_MODULE },
Nir Muchtar753f6182011-01-03 15:33:53 +00004247};
4248
Haggai Eran4be74b42015-10-22 15:20:09 +03004249static int cma_init_net(struct net *net)
4250{
4251 struct cma_pernet *pernet = cma_pernet(net);
4252
4253 idr_init(&pernet->tcp_ps);
4254 idr_init(&pernet->udp_ps);
4255 idr_init(&pernet->ipoib_ps);
4256 idr_init(&pernet->ib_ps);
4257
4258 return 0;
4259}
4260
4261static void cma_exit_net(struct net *net)
4262{
4263 struct cma_pernet *pernet = cma_pernet(net);
4264
4265 idr_destroy(&pernet->tcp_ps);
4266 idr_destroy(&pernet->udp_ps);
4267 idr_destroy(&pernet->ipoib_ps);
4268 idr_destroy(&pernet->ib_ps);
4269}
4270
4271static struct pernet_operations cma_pernet_operations = {
4272 .init = cma_init_net,
4273 .exit = cma_exit_net,
4274 .id = &cma_pernet_id,
4275 .size = sizeof(struct cma_pernet),
4276};
4277
Peter Huewe716abb12009-06-23 10:38:42 -07004278static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004279{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09004280 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004281
Sean Heftyc7f743a2007-02-01 12:23:37 -08004282 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07004283 if (!cma_wq)
4284 return -ENOMEM;
4285
Haggai Eran4be74b42015-10-22 15:20:09 +03004286 ret = register_pernet_subsys(&cma_pernet_operations);
4287 if (ret)
4288 goto err_wq;
4289
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004290 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08004291 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004292 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004293
Sean Heftye51060f2006-06-17 20:37:29 -07004294 ret = ib_register_client(&cma_client);
4295 if (ret)
4296 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00004297
Mark Bloch2fa2d4f2016-05-06 22:45:26 +03004298 if (ibnl_add_client(RDMA_NL_RDMA_CM, ARRAY_SIZE(cma_cb_table),
4299 cma_cb_table))
Parav Panditaba25a3e2016-03-02 00:50:29 +05304300 pr_warn("RDMA CMA: failed to add netlink callback\n");
Matan Barak045959d2015-12-23 14:56:55 +02004301 cma_configfs_init();
Nir Muchtar753f6182011-01-03 15:33:53 +00004302
Sean Heftye51060f2006-06-17 20:37:29 -07004303 return 0;
4304
4305err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004306 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004307 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004308 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004309err_wq:
Sean Heftye51060f2006-06-17 20:37:29 -07004310 destroy_workqueue(cma_wq);
4311 return ret;
4312}
4313
Peter Huewe716abb12009-06-23 10:38:42 -07004314static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07004315{
Matan Barak045959d2015-12-23 14:56:55 +02004316 cma_configfs_exit();
Nir Muchtar753f6182011-01-03 15:33:53 +00004317 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07004318 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07004319 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08004320 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004321 ib_sa_unregister_client(&sa_client);
Haggai Eran4be74b42015-10-22 15:20:09 +03004322 unregister_pernet_subsys(&cma_pernet_operations);
Sean Heftye51060f2006-06-17 20:37:29 -07004323 destroy_workqueue(cma_wq);
Sean Heftye51060f2006-06-17 20:37:29 -07004324}
4325
4326module_init(cma_init);
4327module_exit(cma_cleanup);