blob: 88c9b70eed47e26c137c33dcd673caffa931e61a [file] [log] [blame]
Sean Heftye51060f2006-06-17 20:37:29 -07001/*
2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
6 *
Sean Heftya9474912008-07-14 23:48:43 -07007 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
Sean Heftye51060f2006-06-17 20:37:29 -070012 *
Sean Heftya9474912008-07-14 23:48:43 -070013 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
Sean Heftye51060f2006-06-17 20:37:29 -070016 *
Sean Heftya9474912008-07-14 23:48:43 -070017 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
Sean Heftye51060f2006-06-17 20:37:29 -070020 *
Sean Heftya9474912008-07-14 23:48:43 -070021 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
Sean Heftye51060f2006-06-17 20:37:29 -070025 *
Sean Heftya9474912008-07-14 23:48:43 -070026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
Sean Heftye51060f2006-06-17 20:37:29 -070034 */
35
36#include <linux/completion.h>
37#include <linux/in.h>
38#include <linux/in6.h>
39#include <linux/mutex.h>
40#include <linux/random.h>
41#include <linux/idr.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050042#include <linux/inetdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040044#include <linux/module.h>
Amir Vadai366cddb2012-04-04 21:33:29 +000045#include <net/route.h>
Sean Heftye51060f2006-06-17 20:37:29 -070046
47#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080048#include <net/ipv6.h>
Sean Heftye51060f2006-06-17 20:37:29 -070049
50#include <rdma/rdma_cm.h>
51#include <rdma/rdma_cm_ib.h>
Nir Muchtar753f6182011-01-03 15:33:53 +000052#include <rdma/rdma_netlink.h>
Sean Hefty2e2d1902013-05-29 10:09:09 -070053#include <rdma/ib.h>
Sean Heftye51060f2006-06-17 20:37:29 -070054#include <rdma/ib_cache.h>
55#include <rdma/ib_cm.h>
56#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050057#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070058
59MODULE_AUTHOR("Sean Hefty");
60MODULE_DESCRIPTION("Generic RDMA CM Agent");
61MODULE_LICENSE("Dual BSD/GPL");
62
63#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030064#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070065#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020066#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070067
68static void cma_add_one(struct ib_device *device);
69static void cma_remove_one(struct ib_device *device);
70
71static struct ib_client cma_client = {
72 .name = "cma",
73 .add = cma_add_one,
74 .remove = cma_remove_one
75};
76
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070077static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -080078static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -070079static LIST_HEAD(dev_list);
80static LIST_HEAD(listen_any_list);
81static DEFINE_MUTEX(lock);
82static struct workqueue_struct *cma_wq;
Sean Heftye51060f2006-06-17 20:37:29 -070083static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -080084static DEFINE_IDR(udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -080085static DEFINE_IDR(ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -070086static DEFINE_IDR(ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -070087
88struct cma_device {
89 struct list_head list;
90 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -070091 struct completion comp;
92 atomic_t refcount;
93 struct list_head id_list;
94};
95
Sean Heftye51060f2006-06-17 20:37:29 -070096struct rdma_bind_list {
97 struct idr *ps;
98 struct hlist_head owners;
99 unsigned short port;
100};
101
Sean Hefty68602122012-06-14 20:31:39 +0000102enum {
103 CMA_OPTION_AFONLY,
104};
105
Sean Heftye51060f2006-06-17 20:37:29 -0700106/*
107 * Device removal can occur at anytime, so we need extra handling to
108 * serialize notifying the user of device removal with other callbacks.
109 * We do this by disabling removal notification while a callback is in process,
110 * and reporting it after the callback completes.
111 */
112struct rdma_id_private {
113 struct rdma_cm_id id;
114
115 struct rdma_bind_list *bind_list;
116 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700117 struct list_head list; /* listen_any_list or cma_device.list */
118 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700119 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800120 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700121
Sean Heftyd02d1f52007-10-09 11:12:34 -0700122 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700123 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700124 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700125 struct mutex qp_mutex;
126
Sean Heftye51060f2006-06-17 20:37:29 -0700127 struct completion comp;
128 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700129 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700130
131 int backlog;
132 int timeout_ms;
133 struct ib_sa_query *query;
134 int query_id;
135 union {
136 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500137 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700138 } cm_id;
139
140 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800141 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700142 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000143 pid_t owner;
Sean Hefty68602122012-06-14 20:31:39 +0000144 u32 options;
Sean Heftye51060f2006-06-17 20:37:29 -0700145 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700146 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700147 u8 reuseaddr;
Sean Hefty5b0ec992012-06-14 20:31:39 +0000148 u8 afonly;
Sean Heftye51060f2006-06-17 20:37:29 -0700149};
150
Sean Heftyc8f6a362007-02-15 17:00:18 -0800151struct cma_multicast {
152 struct rdma_id_private *id_priv;
153 union {
154 struct ib_sa_multicast *ib;
155 } multicast;
156 struct list_head list;
157 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700158 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200159 struct kref mcref;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800160};
161
Sean Heftye51060f2006-06-17 20:37:29 -0700162struct cma_work {
163 struct work_struct work;
164 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700165 enum rdma_cm_state old_state;
166 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700167 struct rdma_cm_event event;
168};
169
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700170struct cma_ndev_work {
171 struct work_struct work;
172 struct rdma_id_private *id;
173 struct rdma_cm_event event;
174};
175
Eli Cohen3c86aa72010-10-13 21:26:51 +0200176struct iboe_mcast_work {
177 struct work_struct work;
178 struct rdma_id_private *id;
179 struct cma_multicast *mc;
180};
181
Sean Heftye51060f2006-06-17 20:37:29 -0700182union cma_ip_addr {
183 struct in6_addr ip6;
184 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000185 __be32 pad[3];
186 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700187 } ip4;
188};
189
190struct cma_hdr {
191 u8 cma_version;
192 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000193 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700194 union cma_ip_addr src_addr;
195 union cma_ip_addr dst_addr;
196};
197
Sean Heftye51060f2006-06-17 20:37:29 -0700198#define CMA_VERSION 0x00
Sean Heftye51060f2006-06-17 20:37:29 -0700199
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700200static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700201{
202 unsigned long flags;
203 int ret;
204
205 spin_lock_irqsave(&id_priv->lock, flags);
206 ret = (id_priv->state == comp);
207 spin_unlock_irqrestore(&id_priv->lock, flags);
208 return ret;
209}
210
211static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700212 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700213{
214 unsigned long flags;
215 int ret;
216
217 spin_lock_irqsave(&id_priv->lock, flags);
218 if ((ret = (id_priv->state == comp)))
219 id_priv->state = exch;
220 spin_unlock_irqrestore(&id_priv->lock, flags);
221 return ret;
222}
223
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700224static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
225 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700226{
227 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700228 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700229
230 spin_lock_irqsave(&id_priv->lock, flags);
231 old = id_priv->state;
232 id_priv->state = exch;
233 spin_unlock_irqrestore(&id_priv->lock, flags);
234 return old;
235}
236
237static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
238{
239 return hdr->ip_version >> 4;
240}
241
242static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
243{
244 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
245}
246
Sean Heftye51060f2006-06-17 20:37:29 -0700247static void cma_attach_to_dev(struct rdma_id_private *id_priv,
248 struct cma_device *cma_dev)
249{
250 atomic_inc(&cma_dev->refcount);
251 id_priv->cma_dev = cma_dev;
252 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200253 id_priv->id.route.addr.dev_addr.transport =
254 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700255 list_add_tail(&id_priv->list, &cma_dev->id_list);
256}
257
258static inline void cma_deref_dev(struct cma_device *cma_dev)
259{
260 if (atomic_dec_and_test(&cma_dev->refcount))
261 complete(&cma_dev->comp);
262}
263
Eli Cohen3c86aa72010-10-13 21:26:51 +0200264static inline void release_mc(struct kref *kref)
265{
266 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
267
268 kfree(mc->multicast.ib);
269 kfree(mc);
270}
271
Sean Heftya396d432011-02-23 09:05:39 -0800272static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700273{
Sean Heftya396d432011-02-23 09:05:39 -0800274 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700275 list_del(&id_priv->list);
276 cma_deref_dev(id_priv->cma_dev);
277 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800278 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700279}
280
Sean Heftyf4753832013-05-29 10:09:14 -0700281static inline struct sockaddr *cma_src_addr(struct rdma_id_private *id_priv)
282{
283 return (struct sockaddr *) &id_priv->id.route.addr.src_addr;
284}
285
286static inline struct sockaddr *cma_dst_addr(struct rdma_id_private *id_priv)
287{
288 return (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
289}
290
291static inline unsigned short cma_family(struct rdma_id_private *id_priv)
292{
293 return id_priv->id.route.addr.src_addr.ss_family;
294}
295
Sean Hefty5c438132013-05-29 10:09:23 -0700296static int cma_set_qkey(struct rdma_id_private *id_priv, u32 qkey)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800297{
298 struct ib_sa_mcmember_rec rec;
299 int ret = 0;
300
Sean Hefty5c438132013-05-29 10:09:23 -0700301 if (id_priv->qkey) {
302 if (qkey && id_priv->qkey != qkey)
303 return -EINVAL;
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700304 return 0;
Sean Hefty5c438132013-05-29 10:09:23 -0700305 }
306
307 if (qkey) {
308 id_priv->qkey = qkey;
309 return 0;
310 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700311
312 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800313 case RDMA_PS_UDP:
Sean Hefty5c438132013-05-29 10:09:23 -0700314 case RDMA_PS_IB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700315 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800316 break;
317 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700318 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
319 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
320 id_priv->id.port_num, &rec.mgid,
321 &rec);
322 if (!ret)
323 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800324 break;
325 default:
326 break;
327 }
328 return ret;
329}
330
Sean Hefty680f9202013-05-29 10:09:12 -0700331static void cma_translate_ib(struct sockaddr_ib *sib, struct rdma_dev_addr *dev_addr)
332{
333 dev_addr->dev_type = ARPHRD_INFINIBAND;
334 rdma_addr_set_sgid(dev_addr, (union ib_gid *) &sib->sib_addr);
335 ib_addr_set_pkey(dev_addr, ntohs(sib->sib_pkey));
336}
337
338static int cma_translate_addr(struct sockaddr *addr, struct rdma_dev_addr *dev_addr)
339{
340 int ret;
341
342 if (addr->sa_family != AF_IB) {
Matan Barakdd5f03b2013-12-12 18:03:11 +0200343 ret = rdma_translate_ip(addr, dev_addr, NULL);
Sean Hefty680f9202013-05-29 10:09:12 -0700344 } else {
345 cma_translate_ib((struct sockaddr_ib *) addr, dev_addr);
346 ret = 0;
347 }
348
349 return ret;
350}
351
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400352static int cma_acquire_dev(struct rdma_id_private *id_priv,
353 struct rdma_id_private *listen_id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700354{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800355 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700356 struct cma_device *cma_dev;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200357 union ib_gid gid, iboe_gid;
Sean Heftye51060f2006-06-17 20:37:29 -0700358 int ret = -ENODEV;
Doug Ledford29f27e82013-09-24 17:16:27 -0400359 u8 port, found_port;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200360 enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
361 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
Sean Heftye51060f2006-06-17 20:37:29 -0700362
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000363 if (dev_ll != IB_LINK_LAYER_INFINIBAND &&
364 id_priv->id.ps == RDMA_PS_IPOIB)
365 return -EINVAL;
366
Sean Heftya396d432011-02-23 09:05:39 -0800367 mutex_lock(&lock);
Moni Shoua7b856272013-12-12 18:03:12 +0200368 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
369 &iboe_gid);
370
Eli Cohen3c86aa72010-10-13 21:26:51 +0200371 memcpy(&gid, dev_addr->src_dev_addr +
372 rdma_addr_gid_offset(dev_addr), sizeof gid);
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400373 if (listen_id_priv &&
374 rdma_port_get_link_layer(listen_id_priv->id.device,
375 listen_id_priv->id.port_num) == dev_ll) {
376 cma_dev = listen_id_priv->cma_dev;
377 port = listen_id_priv->id.port_num;
378 if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
379 rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
380 ret = ib_find_cached_gid(cma_dev->device, &iboe_gid,
381 &found_port, NULL);
382 else
383 ret = ib_find_cached_gid(cma_dev->device, &gid,
384 &found_port, NULL);
385
386 if (!ret && (port == found_port)) {
387 id_priv->id.port_num = found_port;
388 goto out;
389 }
390 }
Sean Heftye51060f2006-06-17 20:37:29 -0700391 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200392 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
Doug Ledfordbe9130c2013-09-24 17:16:28 -0400393 if (listen_id_priv &&
394 listen_id_priv->cma_dev == cma_dev &&
395 listen_id_priv->id.port_num == port)
396 continue;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200397 if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
398 if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
399 rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
Doug Ledford29f27e82013-09-24 17:16:27 -0400400 ret = ib_find_cached_gid(cma_dev->device, &iboe_gid, &found_port, NULL);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200401 else
Doug Ledford29f27e82013-09-24 17:16:27 -0400402 ret = ib_find_cached_gid(cma_dev->device, &gid, &found_port, NULL);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200403
Doug Ledford29f27e82013-09-24 17:16:27 -0400404 if (!ret && (port == found_port)) {
405 id_priv->id.port_num = found_port;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200406 goto out;
shefty63f05be2012-11-28 20:39:52 +0000407 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200408 }
Sean Heftye51060f2006-06-17 20:37:29 -0700409 }
410 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200411
412out:
413 if (!ret)
414 cma_attach_to_dev(id_priv, cma_dev);
415
Sean Heftya396d432011-02-23 09:05:39 -0800416 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700417 return ret;
418}
419
Sean Heftyf17df3b2013-05-29 10:09:17 -0700420/*
421 * Select the source IB device and address to reach the destination IB address.
422 */
423static int cma_resolve_ib_dev(struct rdma_id_private *id_priv)
424{
425 struct cma_device *cma_dev, *cur_dev;
426 struct sockaddr_ib *addr;
427 union ib_gid gid, sgid, *dgid;
428 u16 pkey, index;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700429 u8 p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700430 int i;
431
432 cma_dev = NULL;
433 addr = (struct sockaddr_ib *) cma_dst_addr(id_priv);
434 dgid = (union ib_gid *) &addr->sib_addr;
435 pkey = ntohs(addr->sib_pkey);
436
437 list_for_each_entry(cur_dev, &dev_list, list) {
438 if (rdma_node_get_transport(cur_dev->device->node_type) != RDMA_TRANSPORT_IB)
439 continue;
440
441 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
442 if (ib_find_cached_pkey(cur_dev->device, p, pkey, &index))
443 continue;
444
445 for (i = 0; !ib_get_cached_gid(cur_dev->device, p, i, &gid); i++) {
446 if (!memcmp(&gid, dgid, sizeof(gid))) {
447 cma_dev = cur_dev;
448 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700449 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700450 goto found;
451 }
452
453 if (!cma_dev && (gid.global.subnet_prefix ==
454 dgid->global.subnet_prefix)) {
455 cma_dev = cur_dev;
456 sgid = gid;
Paul Bolle8fb488d72013-07-24 15:06:07 -0700457 id_priv->id.port_num = p;
Sean Heftyf17df3b2013-05-29 10:09:17 -0700458 }
459 }
460 }
461 }
462
463 if (!cma_dev)
464 return -ENODEV;
465
466found:
467 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf17df3b2013-05-29 10:09:17 -0700468 addr = (struct sockaddr_ib *) cma_src_addr(id_priv);
469 memcpy(&addr->sib_addr, &sgid, sizeof sgid);
470 cma_translate_ib(addr, &id_priv->id.route.addr.dev_addr);
471 return 0;
472}
473
Sean Heftye51060f2006-06-17 20:37:29 -0700474static void cma_deref_id(struct rdma_id_private *id_priv)
475{
476 if (atomic_dec_and_test(&id_priv->refcount))
477 complete(&id_priv->comp);
478}
479
Or Gerlitzde910bd2008-07-14 23:48:53 -0700480static int cma_disable_callback(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700481 enum rdma_cm_state state)
Sean Hefty8aa08602007-05-07 11:49:00 -0700482{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700483 mutex_lock(&id_priv->handler_mutex);
484 if (id_priv->state != state) {
485 mutex_unlock(&id_priv->handler_mutex);
486 return -EINVAL;
487 }
488 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700489}
490
491struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000492 void *context, enum rdma_port_space ps,
493 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700494{
495 struct rdma_id_private *id_priv;
496
497 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
498 if (!id_priv)
499 return ERR_PTR(-ENOMEM);
500
Nir Muchtar83e95022011-01-13 13:56:04 +0000501 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700502 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700503 id_priv->id.context = context;
504 id_priv->id.event_handler = event_handler;
505 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000506 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700507 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700508 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700509 init_completion(&id_priv->comp);
510 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700511 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700512 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800513 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700514 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
515
516 return &id_priv->id;
517}
518EXPORT_SYMBOL(rdma_create_id);
519
Sean Heftyc8f6a362007-02-15 17:00:18 -0800520static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700521{
522 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800523 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700524
Sean Heftyc8f6a362007-02-15 17:00:18 -0800525 qp_attr.qp_state = IB_QPS_INIT;
526 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700527 if (ret)
528 return ret;
529
Sean Heftyc8f6a362007-02-15 17:00:18 -0800530 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
531 if (ret)
532 return ret;
533
534 qp_attr.qp_state = IB_QPS_RTR;
535 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
536 if (ret)
537 return ret;
538
539 qp_attr.qp_state = IB_QPS_RTS;
540 qp_attr.sq_psn = 0;
541 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
542
543 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700544}
545
Sean Heftyc8f6a362007-02-15 17:00:18 -0800546static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500547{
548 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800549 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500550
551 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800552 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
553 if (ret)
554 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500555
Sean Heftyc8f6a362007-02-15 17:00:18 -0800556 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500557}
558
Sean Heftye51060f2006-06-17 20:37:29 -0700559int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
560 struct ib_qp_init_attr *qp_init_attr)
561{
562 struct rdma_id_private *id_priv;
563 struct ib_qp *qp;
564 int ret;
565
566 id_priv = container_of(id, struct rdma_id_private, id);
567 if (id->device != pd->device)
568 return -EINVAL;
569
570 qp = ib_create_qp(pd, qp_init_attr);
571 if (IS_ERR(qp))
572 return PTR_ERR(qp);
573
Sean Heftyb26f9b92010-04-01 17:08:41 +0000574 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800575 ret = cma_init_ud_qp(id_priv, qp);
576 else
577 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700578 if (ret)
579 goto err;
580
581 id->qp = qp;
582 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700583 id_priv->srq = (qp->srq != NULL);
584 return 0;
585err:
586 ib_destroy_qp(qp);
587 return ret;
588}
589EXPORT_SYMBOL(rdma_create_qp);
590
591void rdma_destroy_qp(struct rdma_cm_id *id)
592{
Sean Heftyc5483382007-09-24 13:19:09 -0700593 struct rdma_id_private *id_priv;
594
595 id_priv = container_of(id, struct rdma_id_private, id);
596 mutex_lock(&id_priv->qp_mutex);
597 ib_destroy_qp(id_priv->id.qp);
598 id_priv->id.qp = NULL;
599 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700600}
601EXPORT_SYMBOL(rdma_destroy_qp);
602
Sean Hefty5851bb82008-01-04 10:47:12 -0800603static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
604 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700605{
606 struct ib_qp_attr qp_attr;
607 int qp_attr_mask, ret;
Matan Barakdd5f03b2013-12-12 18:03:11 +0200608 union ib_gid sgid;
Sean Heftye51060f2006-06-17 20:37:29 -0700609
Sean Heftyc5483382007-09-24 13:19:09 -0700610 mutex_lock(&id_priv->qp_mutex);
611 if (!id_priv->id.qp) {
612 ret = 0;
613 goto out;
614 }
Sean Heftye51060f2006-06-17 20:37:29 -0700615
616 /* Need to update QP attributes from default values. */
617 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700618 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700619 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700620 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700621
Sean Heftyc5483382007-09-24 13:19:09 -0700622 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700623 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700624 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700625
626 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700627 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700628 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700629 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700630
Matan Barakdd5f03b2013-12-12 18:03:11 +0200631 ret = ib_query_gid(id_priv->id.device, id_priv->id.port_num,
632 qp_attr.ah_attr.grh.sgid_index, &sgid);
633 if (ret)
634 goto out;
635
636 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type)
637 == RDMA_TRANSPORT_IB &&
638 rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)
639 == IB_LINK_LAYER_ETHERNET) {
640 ret = rdma_addr_find_smac_by_sgid(&sgid, qp_attr.smac, NULL);
641
642 if (ret)
643 goto out;
644 }
Sean Hefty5851bb82008-01-04 10:47:12 -0800645 if (conn_param)
646 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700647 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
648out:
649 mutex_unlock(&id_priv->qp_mutex);
650 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700651}
652
Sean Hefty5851bb82008-01-04 10:47:12 -0800653static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
654 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700655{
656 struct ib_qp_attr qp_attr;
657 int qp_attr_mask, ret;
658
Sean Heftyc5483382007-09-24 13:19:09 -0700659 mutex_lock(&id_priv->qp_mutex);
660 if (!id_priv->id.qp) {
661 ret = 0;
662 goto out;
663 }
Sean Heftye51060f2006-06-17 20:37:29 -0700664
665 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700666 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700667 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700668 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700669
Sean Hefty5851bb82008-01-04 10:47:12 -0800670 if (conn_param)
671 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700672 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
673out:
674 mutex_unlock(&id_priv->qp_mutex);
675 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700676}
677
Sean Heftyc5483382007-09-24 13:19:09 -0700678static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700679{
680 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700681 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700682
Sean Heftyc5483382007-09-24 13:19:09 -0700683 mutex_lock(&id_priv->qp_mutex);
684 if (!id_priv->id.qp) {
685 ret = 0;
686 goto out;
687 }
Sean Heftye51060f2006-06-17 20:37:29 -0700688
689 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700690 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
691out:
692 mutex_unlock(&id_priv->qp_mutex);
693 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700694}
695
Sean Heftyc8f6a362007-02-15 17:00:18 -0800696static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
697 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
698{
699 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
700 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200701 u16 pkey;
702
703 if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
704 IB_LINK_LAYER_INFINIBAND)
705 pkey = ib_addr_get_pkey(dev_addr);
706 else
707 pkey = 0xffff;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800708
709 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200710 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800711 if (ret)
712 return ret;
713
714 qp_attr->port_num = id_priv->id.port_num;
715 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
716
Sean Heftyb26f9b92010-04-01 17:08:41 +0000717 if (id_priv->id.qp_type == IB_QPT_UD) {
Sean Hefty5c438132013-05-29 10:09:23 -0700718 ret = cma_set_qkey(id_priv, 0);
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700719 if (ret)
720 return ret;
721
Sean Heftyc8f6a362007-02-15 17:00:18 -0800722 qp_attr->qkey = id_priv->qkey;
723 *qp_attr_mask |= IB_QP_QKEY;
724 } else {
725 qp_attr->qp_access_flags = 0;
726 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
727 }
728 return 0;
729}
730
Sean Heftye51060f2006-06-17 20:37:29 -0700731int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
732 int *qp_attr_mask)
733{
734 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800735 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700736
737 id_priv = container_of(id, struct rdma_id_private, id);
Michael Wang21655af2015-05-05 14:50:27 +0200738 if (rdma_ib_or_iboe(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +0000739 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800740 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
741 else
742 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
743 qp_attr_mask);
Matan Barakdd5f03b2013-12-12 18:03:11 +0200744
Sean Heftye51060f2006-06-17 20:37:29 -0700745 if (qp_attr->qp_state == IB_QPS_RTR)
746 qp_attr->rq_psn = id_priv->seq_num;
Michael Wang21655af2015-05-05 14:50:27 +0200747 } else if (rdma_protocol_iwarp(id->device, id->port_num)) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800748 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300749 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800750 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
751 } else
752 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
753 qp_attr_mask);
Michael Wang21655af2015-05-05 14:50:27 +0200754 } else
Sean Heftye51060f2006-06-17 20:37:29 -0700755 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -0700756
757 return ret;
758}
759EXPORT_SYMBOL(rdma_init_qp_attr);
760
761static inline int cma_zero_addr(struct sockaddr *addr)
762{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700763 switch (addr->sa_family) {
764 case AF_INET:
765 return ipv4_is_zeronet(((struct sockaddr_in *)addr)->sin_addr.s_addr);
766 case AF_INET6:
767 return ipv6_addr_any(&((struct sockaddr_in6 *) addr)->sin6_addr);
768 case AF_IB:
769 return ib_addr_any(&((struct sockaddr_ib *) addr)->sib_addr);
770 default:
771 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700772 }
773}
774
775static inline int cma_loopback_addr(struct sockaddr *addr)
776{
Sean Hefty2e2d1902013-05-29 10:09:09 -0700777 switch (addr->sa_family) {
778 case AF_INET:
779 return ipv4_is_loopback(((struct sockaddr_in *) addr)->sin_addr.s_addr);
780 case AF_INET6:
781 return ipv6_addr_loopback(&((struct sockaddr_in6 *) addr)->sin6_addr);
782 case AF_IB:
783 return ib_addr_loopback(&((struct sockaddr_ib *) addr)->sib_addr);
784 default:
785 return 0;
786 }
Sean Heftye51060f2006-06-17 20:37:29 -0700787}
788
789static inline int cma_any_addr(struct sockaddr *addr)
790{
791 return cma_zero_addr(addr) || cma_loopback_addr(addr);
792}
793
Hefty, Sean43b752d2011-05-09 22:06:10 -0700794static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
795{
796 if (src->sa_family != dst->sa_family)
797 return -1;
798
799 switch (src->sa_family) {
800 case AF_INET:
801 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
802 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
Sean Hefty2e2d1902013-05-29 10:09:09 -0700803 case AF_INET6:
Hefty, Sean43b752d2011-05-09 22:06:10 -0700804 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
805 &((struct sockaddr_in6 *) dst)->sin6_addr);
Sean Hefty2e2d1902013-05-29 10:09:09 -0700806 default:
807 return ib_addr_cmp(&((struct sockaddr_ib *) src)->sib_addr,
808 &((struct sockaddr_ib *) dst)->sib_addr);
Hefty, Sean43b752d2011-05-09 22:06:10 -0700809 }
810}
811
Sean Hefty58afdcb2013-05-29 10:09:11 -0700812static __be16 cma_port(struct sockaddr *addr)
Sean Hefty628e5f62006-11-30 16:44:16 -0800813{
Sean Hefty58afdcb2013-05-29 10:09:11 -0700814 struct sockaddr_ib *sib;
815
816 switch (addr->sa_family) {
817 case AF_INET:
Sean Hefty628e5f62006-11-30 16:44:16 -0800818 return ((struct sockaddr_in *) addr)->sin_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700819 case AF_INET6:
Sean Hefty628e5f62006-11-30 16:44:16 -0800820 return ((struct sockaddr_in6 *) addr)->sin6_port;
Sean Hefty58afdcb2013-05-29 10:09:11 -0700821 case AF_IB:
822 sib = (struct sockaddr_ib *) addr;
823 return htons((u16) (be64_to_cpu(sib->sib_sid) &
824 be64_to_cpu(sib->sib_sid_mask)));
825 default:
826 return 0;
827 }
Sean Hefty628e5f62006-11-30 16:44:16 -0800828}
829
Sean Heftye51060f2006-06-17 20:37:29 -0700830static inline int cma_any_port(struct sockaddr *addr)
831{
Sean Hefty628e5f62006-11-30 16:44:16 -0800832 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700833}
834
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700835static void cma_save_ib_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
836 struct ib_sa_path_rec *path)
Sean Heftye51060f2006-06-17 20:37:29 -0700837{
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700838 struct sockaddr_ib *listen_ib, *ib;
Sean Heftye51060f2006-06-17 20:37:29 -0700839
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700840 listen_ib = (struct sockaddr_ib *) &listen_id->route.addr.src_addr;
841 ib = (struct sockaddr_ib *) &id->route.addr.src_addr;
842 ib->sib_family = listen_ib->sib_family;
843 ib->sib_pkey = path->pkey;
844 ib->sib_flowinfo = path->flow_label;
845 memcpy(&ib->sib_addr, &path->sgid, 16);
846 ib->sib_sid = listen_ib->sib_sid;
847 ib->sib_sid_mask = cpu_to_be64(0xffffffffffffffffULL);
848 ib->sib_scope_id = listen_ib->sib_scope_id;
Sean Heftye51060f2006-06-17 20:37:29 -0700849
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700850 ib = (struct sockaddr_ib *) &id->route.addr.dst_addr;
851 ib->sib_family = listen_ib->sib_family;
852 ib->sib_pkey = path->pkey;
853 ib->sib_flowinfo = path->flow_label;
854 memcpy(&ib->sib_addr, &path->dgid, 16);
Sean Heftye51060f2006-06-17 20:37:29 -0700855}
856
Jason Gunthorpe28521442015-04-20 14:01:11 -0600857static __be16 ss_get_port(const struct sockaddr_storage *ss)
858{
859 if (ss->ss_family == AF_INET)
860 return ((struct sockaddr_in *)ss)->sin_port;
861 else if (ss->ss_family == AF_INET6)
862 return ((struct sockaddr_in6 *)ss)->sin6_port;
863 BUG();
864}
865
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700866static void cma_save_ip4_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
867 struct cma_hdr *hdr)
Sean Heftye51060f2006-06-17 20:37:29 -0700868{
Jason Gunthorpe28521442015-04-20 14:01:11 -0600869 struct sockaddr_in *ip4;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700870
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700871 ip4 = (struct sockaddr_in *) &id->route.addr.src_addr;
Jason Gunthorpe28521442015-04-20 14:01:11 -0600872 ip4->sin_family = AF_INET;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700873 ip4->sin_addr.s_addr = hdr->dst_addr.ip4.addr;
Jason Gunthorpe28521442015-04-20 14:01:11 -0600874 ip4->sin_port = ss_get_port(&listen_id->route.addr.src_addr);
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700875
876 ip4 = (struct sockaddr_in *) &id->route.addr.dst_addr;
Jason Gunthorpe28521442015-04-20 14:01:11 -0600877 ip4->sin_family = AF_INET;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700878 ip4->sin_addr.s_addr = hdr->src_addr.ip4.addr;
879 ip4->sin_port = hdr->port;
880}
881
882static void cma_save_ip6_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
883 struct cma_hdr *hdr)
884{
Jason Gunthorpe28521442015-04-20 14:01:11 -0600885 struct sockaddr_in6 *ip6;
Sean Heftye51060f2006-06-17 20:37:29 -0700886
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700887 ip6 = (struct sockaddr_in6 *) &id->route.addr.src_addr;
Jason Gunthorpe28521442015-04-20 14:01:11 -0600888 ip6->sin6_family = AF_INET6;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700889 ip6->sin6_addr = hdr->dst_addr.ip6;
Jason Gunthorpe28521442015-04-20 14:01:11 -0600890 ip6->sin6_port = ss_get_port(&listen_id->route.addr.src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700891
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700892 ip6 = (struct sockaddr_in6 *) &id->route.addr.dst_addr;
Jason Gunthorpe28521442015-04-20 14:01:11 -0600893 ip6->sin6_family = AF_INET6;
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700894 ip6->sin6_addr = hdr->src_addr.ip6;
895 ip6->sin6_port = hdr->port;
896}
897
898static int cma_save_net_info(struct rdma_cm_id *id, struct rdma_cm_id *listen_id,
899 struct ib_cm_event *ib_event)
900{
901 struct cma_hdr *hdr;
902
Sean Hefty5eb695c2013-07-24 15:06:09 -0700903 if ((listen_id->route.addr.src_addr.ss_family == AF_IB) &&
904 (ib_event->event == IB_CM_REQ_RECEIVED)) {
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700905 cma_save_ib_info(id, listen_id, ib_event->param.req_rcvd.primary_path);
906 return 0;
907 }
908
909 hdr = ib_event->private_data;
910 if (hdr->cma_version != CMA_VERSION)
911 return -EINVAL;
912
913 switch (cma_get_ip_ver(hdr)) {
914 case 4:
915 cma_save_ip4_info(id, listen_id, hdr);
Sean Heftye51060f2006-06-17 20:37:29 -0700916 break;
917 case 6:
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700918 cma_save_ip6_info(id, listen_id, hdr);
Sean Heftye51060f2006-06-17 20:37:29 -0700919 break;
920 default:
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700921 return -EINVAL;
Sean Heftye51060f2006-06-17 20:37:29 -0700922 }
Sean Heftyfbaa1a62013-05-29 10:09:21 -0700923 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700924}
925
Sean Heftye8160e12013-05-29 10:09:22 -0700926static inline int cma_user_data_offset(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700927{
Sean Heftye8160e12013-05-29 10:09:22 -0700928 return cma_family(id_priv) == AF_IB ? 0 : sizeof(struct cma_hdr);
Sean Heftye51060f2006-06-17 20:37:29 -0700929}
930
Sean Heftye51060f2006-06-17 20:37:29 -0700931static void cma_cancel_route(struct rdma_id_private *id_priv)
932{
Eli Cohen3c86aa72010-10-13 21:26:51 +0200933 switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
934 case IB_LINK_LAYER_INFINIBAND:
Sean Heftye51060f2006-06-17 20:37:29 -0700935 if (id_priv->query)
936 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
937 break;
938 default:
939 break;
940 }
941}
942
Sean Heftye51060f2006-06-17 20:37:29 -0700943static void cma_cancel_listens(struct rdma_id_private *id_priv)
944{
945 struct rdma_id_private *dev_id_priv;
946
Sean Heftyd02d1f52007-10-09 11:12:34 -0700947 /*
948 * Remove from listen_any_list to prevent added devices from spawning
949 * additional listen requests.
950 */
Sean Heftye51060f2006-06-17 20:37:29 -0700951 mutex_lock(&lock);
952 list_del(&id_priv->list);
953
954 while (!list_empty(&id_priv->listen_list)) {
955 dev_id_priv = list_entry(id_priv->listen_list.next,
956 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -0700957 /* sync with device removal to avoid duplicate destruction */
958 list_del_init(&dev_id_priv->list);
959 list_del(&dev_id_priv->listen_list);
960 mutex_unlock(&lock);
961
962 rdma_destroy_id(&dev_id_priv->id);
963 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700964 }
965 mutex_unlock(&lock);
966}
967
968static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700969 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -0700970{
971 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700972 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -0700973 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
974 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700975 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -0700976 cma_cancel_route(id_priv);
977 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700978 case RDMA_CM_LISTEN:
Sean Heftyf4753832013-05-29 10:09:14 -0700979 if (cma_any_addr(cma_src_addr(id_priv)) && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700980 cma_cancel_listens(id_priv);
981 break;
982 default:
983 break;
984 }
985}
986
987static void cma_release_port(struct rdma_id_private *id_priv)
988{
989 struct rdma_bind_list *bind_list = id_priv->bind_list;
990
991 if (!bind_list)
992 return;
993
994 mutex_lock(&lock);
995 hlist_del(&id_priv->node);
996 if (hlist_empty(&bind_list->owners)) {
997 idr_remove(bind_list->ps, bind_list->port);
998 kfree(bind_list);
999 }
1000 mutex_unlock(&lock);
1001}
1002
Sean Heftyc8f6a362007-02-15 17:00:18 -08001003static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
1004{
1005 struct cma_multicast *mc;
1006
1007 while (!list_empty(&id_priv->mc_list)) {
1008 mc = container_of(id_priv->mc_list.next,
1009 struct cma_multicast, list);
1010 list_del(&mc->list);
Eli Cohen3c86aa72010-10-13 21:26:51 +02001011 switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
1012 case IB_LINK_LAYER_INFINIBAND:
1013 ib_sa_free_multicast(mc->multicast.ib);
1014 kfree(mc);
1015 break;
1016 case IB_LINK_LAYER_ETHERNET:
1017 kref_put(&mc->mcref, release_mc);
1018 break;
1019 default:
1020 break;
1021 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001022 }
1023}
1024
Sean Heftye51060f2006-06-17 20:37:29 -07001025void rdma_destroy_id(struct rdma_cm_id *id)
1026{
1027 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001028 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -07001029
1030 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001031 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001032 cma_cancel_operation(id_priv, state);
1033
Sean Heftya396d432011-02-23 09:05:39 -08001034 /*
1035 * Wait for any active callback to finish. New callbacks will find
1036 * the id_priv state set to destroying and abort.
1037 */
1038 mutex_lock(&id_priv->handler_mutex);
1039 mutex_unlock(&id_priv->handler_mutex);
1040
Sean Heftye51060f2006-06-17 20:37:29 -07001041 if (id_priv->cma_dev) {
Michael Wang21655af2015-05-05 14:50:27 +02001042 if (rdma_ib_or_iboe(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001043 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07001044 ib_destroy_cm_id(id_priv->cm_id.ib);
Michael Wang21655af2015-05-05 14:50:27 +02001045 } else if (rdma_protocol_iwarp(id_priv->id.device, 1)) {
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001046 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -05001047 iw_destroy_cm_id(id_priv->cm_id.iw);
Sean Heftye51060f2006-06-17 20:37:29 -07001048 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08001049 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -08001050 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001051 }
1052
1053 cma_release_port(id_priv);
1054 cma_deref_id(id_priv);
1055 wait_for_completion(&id_priv->comp);
1056
Sean Heftyd02d1f52007-10-09 11:12:34 -07001057 if (id_priv->internal_id)
1058 cma_deref_id(id_priv->id.context);
1059
Sean Heftye51060f2006-06-17 20:37:29 -07001060 kfree(id_priv->id.route.path_rec);
1061 kfree(id_priv);
1062}
1063EXPORT_SYMBOL(rdma_destroy_id);
1064
1065static int cma_rep_recv(struct rdma_id_private *id_priv)
1066{
1067 int ret;
1068
Sean Hefty5851bb82008-01-04 10:47:12 -08001069 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001070 if (ret)
1071 goto reject;
1072
Sean Hefty5851bb82008-01-04 10:47:12 -08001073 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07001074 if (ret)
1075 goto reject;
1076
1077 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
1078 if (ret)
1079 goto reject;
1080
1081 return 0;
1082reject:
Sean Heftyc5483382007-09-24 13:19:09 -07001083 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07001084 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
1085 NULL, 0, NULL, 0);
1086 return ret;
1087}
1088
Sean Heftya1b1b612006-11-30 16:33:14 -08001089static void cma_set_rep_event_data(struct rdma_cm_event *event,
1090 struct ib_cm_rep_event_param *rep_data,
1091 void *private_data)
1092{
1093 event->param.conn.private_data = private_data;
1094 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
1095 event->param.conn.responder_resources = rep_data->responder_resources;
1096 event->param.conn.initiator_depth = rep_data->initiator_depth;
1097 event->param.conn.flow_control = rep_data->flow_control;
1098 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
1099 event->param.conn.srq = rep_data->srq;
1100 event->param.conn.qp_num = rep_data->remote_qpn;
1101}
1102
Sean Heftye51060f2006-06-17 20:37:29 -07001103static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1104{
1105 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001106 struct rdma_cm_event event;
1107 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001108
Amir Vadai38ca83a2008-07-22 14:14:23 -07001109 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001110 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001111 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001112 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -07001113 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001114
Sean Heftya1b1b612006-11-30 16:33:14 -08001115 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001116 switch (ib_event->event) {
1117 case IB_CM_REQ_ERROR:
1118 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001119 event.event = RDMA_CM_EVENT_UNREACHABLE;
1120 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001121 break;
1122 case IB_CM_REP_RECEIVED:
Sean Hefty01602f12013-05-29 10:09:20 -07001123 if (id_priv->id.qp) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001124 event.status = cma_rep_recv(id_priv);
1125 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1126 RDMA_CM_EVENT_ESTABLISHED;
Sean Hefty01602f12013-05-29 10:09:20 -07001127 } else {
Sean Heftya1b1b612006-11-30 16:33:14 -08001128 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
Sean Hefty01602f12013-05-29 10:09:20 -07001129 }
Sean Heftya1b1b612006-11-30 16:33:14 -08001130 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1131 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001132 break;
1133 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001134 case IB_CM_USER_ESTABLISHED:
1135 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001136 break;
1137 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001138 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001139 case IB_CM_DREQ_RECEIVED:
1140 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001141 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1142 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001143 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001144 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001145 break;
1146 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001147 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1148 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001149 case IB_CM_MRA_RECEIVED:
1150 /* ignore event */
1151 goto out;
1152 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001153 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001154 event.status = ib_event->param.rej_rcvd.reason;
1155 event.event = RDMA_CM_EVENT_REJECTED;
1156 event.param.conn.private_data = ib_event->private_data;
1157 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001158 break;
1159 default:
Roland Dreier468f2232008-07-14 23:48:47 -07001160 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001161 ib_event->event);
1162 goto out;
1163 }
1164
Sean Heftya1b1b612006-11-30 16:33:14 -08001165 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001166 if (ret) {
1167 /* Destroy the CM ID by returning a non-zero value. */
1168 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001169 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001170 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001171 rdma_destroy_id(&id_priv->id);
1172 return ret;
1173 }
1174out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001175 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001176 return ret;
1177}
1178
Sean Hefty628e5f62006-11-30 16:44:16 -08001179static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1180 struct ib_cm_event *ib_event)
Sean Heftye51060f2006-06-17 20:37:29 -07001181{
1182 struct rdma_id_private *id_priv;
1183 struct rdma_cm_id *id;
1184 struct rdma_route *rt;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001185 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001186
Krishna Kumar3f168d22006-09-29 12:09:51 -07001187 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001188 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001189 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001190 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001191
Sean Heftyf4753832013-05-29 10:09:14 -07001192 id_priv = container_of(id, struct rdma_id_private, id);
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001193 if (cma_save_net_info(id, listen_id, ib_event))
1194 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001195
1196 rt = &id->route;
1197 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1198 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1199 GFP_KERNEL);
1200 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001201 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001202
Sean Heftye51060f2006-06-17 20:37:29 -07001203 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1204 if (rt->num_paths == 2)
1205 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1206
Sean Heftyf4753832013-05-29 10:09:14 -07001207 if (cma_any_addr(cma_src_addr(id_priv))) {
Sean Hefty6f8372b2009-11-19 13:26:06 -08001208 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1209 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
Sean Hefty46ea5062011-12-06 21:15:18 +00001210 ib_addr_set_pkey(&rt->addr.dev_addr, be16_to_cpu(rt->path_rec[0].pkey));
Sean Hefty6f8372b2009-11-19 13:26:06 -08001211 } else {
Sean Heftyf4753832013-05-29 10:09:14 -07001212 ret = cma_translate_addr(cma_src_addr(id_priv), &rt->addr.dev_addr);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001213 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001214 goto err;
Sean Hefty6f8372b2009-11-19 13:26:06 -08001215 }
1216 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001217
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001218 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001219 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001220
Krishna Kumar3f168d22006-09-29 12:09:51 -07001221err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001222 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001223 return NULL;
1224}
1225
Sean Hefty628e5f62006-11-30 16:44:16 -08001226static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1227 struct ib_cm_event *ib_event)
1228{
1229 struct rdma_id_private *id_priv;
1230 struct rdma_cm_id *id;
Sean Hefty628e5f62006-11-30 16:44:16 -08001231 int ret;
1232
1233 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001234 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001235 if (IS_ERR(id))
1236 return NULL;
1237
Sean Heftyf4753832013-05-29 10:09:14 -07001238 id_priv = container_of(id, struct rdma_id_private, id);
Sean Heftyfbaa1a62013-05-29 10:09:21 -07001239 if (cma_save_net_info(id, listen_id, ib_event))
Sean Hefty628e5f62006-11-30 16:44:16 -08001240 goto err;
1241
Sean Hefty6f8372b2009-11-19 13:26:06 -08001242 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
Sean Heftyf4753832013-05-29 10:09:14 -07001243 ret = cma_translate_addr(cma_src_addr(id_priv), &id->route.addr.dev_addr);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001244 if (ret)
1245 goto err;
1246 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001247
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001248 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001249 return id_priv;
1250err:
1251 rdma_destroy_id(id);
1252 return NULL;
1253}
1254
Sean Heftya1b1b612006-11-30 16:33:14 -08001255static void cma_set_req_event_data(struct rdma_cm_event *event,
1256 struct ib_cm_req_event_param *req_data,
1257 void *private_data, int offset)
1258{
1259 event->param.conn.private_data = private_data + offset;
1260 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1261 event->param.conn.responder_resources = req_data->responder_resources;
1262 event->param.conn.initiator_depth = req_data->initiator_depth;
1263 event->param.conn.flow_control = req_data->flow_control;
1264 event->param.conn.retry_count = req_data->retry_count;
1265 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1266 event->param.conn.srq = req_data->srq;
1267 event->param.conn.qp_num = req_data->remote_qpn;
1268}
1269
Hefty, Sean95954802011-10-06 09:32:33 -07001270static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1271{
Sean Hefty4dd81e82012-06-14 20:49:09 +00001272 return (((ib_event->event == IB_CM_REQ_RECEIVED) &&
Hefty, Sean95954802011-10-06 09:32:33 -07001273 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1274 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1275 (id->qp_type == IB_QPT_UD)) ||
1276 (!id->qp_type));
1277}
1278
Sean Heftye51060f2006-06-17 20:37:29 -07001279static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1280{
1281 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001282 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001283 int offset, ret;
1284
1285 listen_id = cm_id->context;
Hefty, Sean95954802011-10-06 09:32:33 -07001286 if (!cma_check_req_qp_type(&listen_id->id, ib_event))
1287 return -EINVAL;
1288
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001289 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001290 return -ECONNABORTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001291
Sean Hefty628e5f62006-11-30 16:44:16 -08001292 memset(&event, 0, sizeof event);
Sean Heftye8160e12013-05-29 10:09:22 -07001293 offset = cma_user_data_offset(listen_id);
Sean Hefty628e5f62006-11-30 16:44:16 -08001294 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001295 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Sean Hefty628e5f62006-11-30 16:44:16 -08001296 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1297 event.param.ud.private_data = ib_event->private_data + offset;
1298 event.param.ud.private_data_len =
1299 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1300 } else {
1301 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1302 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1303 ib_event->private_data, offset);
1304 }
Sean Heftye51060f2006-06-17 20:37:29 -07001305 if (!conn_id) {
1306 ret = -ENOMEM;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001307 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001308 }
1309
Or Gerlitzde910bd2008-07-14 23:48:53 -07001310 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001311 ret = cma_acquire_dev(conn_id, listen_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301312 if (ret)
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001313 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001314
1315 conn_id->cm_id.ib = cm_id;
1316 cm_id->context = conn_id;
1317 cm_id->cm_handler = cma_ib_handler;
1318
Sean Hefty25ae21a2011-02-23 08:11:32 -08001319 /*
1320 * Protect against the user destroying conn_id from another thread
1321 * until we're done accessing it.
1322 */
1323 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001324 ret = conn_id->id.event_handler(&conn_id->id, &event);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001325 if (ret)
1326 goto err3;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001327 /*
1328 * Acquire mutex to prevent user executing rdma_destroy_id()
1329 * while we're accessing the cm_id.
1330 */
1331 mutex_lock(&lock);
Matan Barakdd5f03b2013-12-12 18:03:11 +02001332 if (cma_comp(conn_id, RDMA_CM_CONNECT) &&
1333 (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001334 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1335 mutex_unlock(&lock);
1336 mutex_unlock(&conn_id->handler_mutex);
1337 mutex_unlock(&listen_id->handler_mutex);
1338 cma_deref_id(conn_id);
1339 return 0;
1340
1341err3:
1342 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301343 /* Destroy the CM ID by returning a non-zero value. */
1344 conn_id->cm_id.ib = NULL;
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001345err2:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001346 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001347 mutex_unlock(&conn_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001348err1:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001349 mutex_unlock(&listen_id->handler_mutex);
Sean Heftyb6cec8a2012-04-25 17:42:35 +00001350 if (conn_id)
1351 rdma_destroy_id(&conn_id->id);
Sean Heftye51060f2006-06-17 20:37:29 -07001352 return ret;
1353}
1354
Sean Heftycf539362013-05-29 10:09:28 -07001355__be64 rdma_get_service_id(struct rdma_cm_id *id, struct sockaddr *addr)
Sean Heftye51060f2006-06-17 20:37:29 -07001356{
Sean Hefty496ce3c2013-05-29 10:09:19 -07001357 if (addr->sa_family == AF_IB)
1358 return ((struct sockaddr_ib *) addr)->sib_sid;
1359
Sean Heftycf539362013-05-29 10:09:28 -07001360 return cpu_to_be64(((u64)id->ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001361}
Sean Heftycf539362013-05-29 10:09:28 -07001362EXPORT_SYMBOL(rdma_get_service_id);
Sean Heftye51060f2006-06-17 20:37:29 -07001363
1364static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1365 struct ib_cm_compare_data *compare)
1366{
1367 struct cma_hdr *cma_data, *cma_mask;
Al Viro1b90c132008-03-29 03:10:28 +00001368 __be32 ip4_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001369 struct in6_addr ip6_addr;
1370
1371 memset(compare, 0, sizeof *compare);
1372 cma_data = (void *) compare->data;
1373 cma_mask = (void *) compare->mask;
Sean Heftye51060f2006-06-17 20:37:29 -07001374
1375 switch (addr->sa_family) {
1376 case AF_INET:
1377 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
Sean Hefty01602f12013-05-29 10:09:20 -07001378 cma_set_ip_ver(cma_data, 4);
1379 cma_set_ip_ver(cma_mask, 0xF);
1380 if (!cma_any_addr(addr)) {
1381 cma_data->dst_addr.ip4.addr = ip4_addr;
1382 cma_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001383 }
1384 break;
1385 case AF_INET6:
1386 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
Sean Hefty01602f12013-05-29 10:09:20 -07001387 cma_set_ip_ver(cma_data, 6);
1388 cma_set_ip_ver(cma_mask, 0xF);
1389 if (!cma_any_addr(addr)) {
1390 cma_data->dst_addr.ip6 = ip6_addr;
1391 memset(&cma_mask->dst_addr.ip6, 0xFF,
1392 sizeof cma_mask->dst_addr.ip6);
Sean Heftye51060f2006-06-17 20:37:29 -07001393 }
1394 break;
1395 default:
1396 break;
1397 }
1398}
1399
Tom Tucker07ebafb2006-08-03 16:02:42 -05001400static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1401{
1402 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001403 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001404 int ret = 0;
Steve Wise24d44a32013-07-04 16:10:44 +05301405 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1406 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001407
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001408 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001409 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001410
Sean Heftybe65f082007-05-07 11:49:12 -07001411 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001412 switch (iw_event->event) {
1413 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001414 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001415 break;
1416 case IW_CM_EVENT_CONNECT_REPLY:
Steve Wise24d44a32013-07-04 16:10:44 +05301417 memcpy(cma_src_addr(id_priv), laddr,
1418 rdma_addr_size(laddr));
1419 memcpy(cma_dst_addr(id_priv), raddr,
1420 rdma_addr_size(raddr));
Steve Wise881a0452006-12-15 16:50:17 -06001421 switch (iw_event->status) {
1422 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001423 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301424 event.param.conn.initiator_depth = iw_event->ird;
1425 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06001426 break;
1427 case -ECONNRESET:
1428 case -ECONNREFUSED:
1429 event.event = RDMA_CM_EVENT_REJECTED;
1430 break;
1431 case -ETIMEDOUT:
1432 event.event = RDMA_CM_EVENT_UNREACHABLE;
1433 break;
1434 default:
1435 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1436 break;
1437 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001438 break;
1439 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001440 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301441 event.param.conn.initiator_depth = iw_event->ird;
1442 event.param.conn.responder_resources = iw_event->ord;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001443 break;
1444 default:
1445 BUG_ON(1);
1446 }
1447
Sean Heftya1b1b612006-11-30 16:33:14 -08001448 event.status = iw_event->status;
1449 event.param.conn.private_data = iw_event->private_data;
1450 event.param.conn.private_data_len = iw_event->private_data_len;
1451 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001452 if (ret) {
1453 /* Destroy the CM ID by returning a non-zero value. */
1454 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001455 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001456 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001457 rdma_destroy_id(&id_priv->id);
1458 return ret;
1459 }
1460
Or Gerlitzde910bd2008-07-14 23:48:53 -07001461 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001462 return ret;
1463}
1464
1465static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1466 struct iw_cm_event *iw_event)
1467{
1468 struct rdma_cm_id *new_cm_id;
1469 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001470 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001471 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001472 struct ib_device_attr attr;
Steve Wise24d44a32013-07-04 16:10:44 +05301473 struct sockaddr *laddr = (struct sockaddr *)&iw_event->local_addr;
1474 struct sockaddr *raddr = (struct sockaddr *)&iw_event->remote_addr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001475
1476 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001477 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001478 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001479
1480 /* Create a new RDMA id for the new IW CM ID */
1481 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1482 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001483 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07001484 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001485 ret = -ENOMEM;
1486 goto out;
1487 }
1488 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001489 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001490 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001491
Matan Barakdd5f03b2013-12-12 18:03:11 +02001492 ret = rdma_translate_ip(laddr, &conn_id->id.route.addr.dev_addr, NULL);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001493 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001494 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001495 rdma_destroy_id(new_cm_id);
1496 goto out;
1497 }
1498
Doug Ledfordbe9130c2013-09-24 17:16:28 -04001499 ret = cma_acquire_dev(conn_id, listen_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001500 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001501 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001502 rdma_destroy_id(new_cm_id);
1503 goto out;
1504 }
1505
1506 conn_id->cm_id.iw = cm_id;
1507 cm_id->context = conn_id;
1508 cm_id->cm_handler = cma_iw_handler;
1509
Steve Wise24d44a32013-07-04 16:10:44 +05301510 memcpy(cma_src_addr(conn_id), laddr, rdma_addr_size(laddr));
1511 memcpy(cma_dst_addr(conn_id), raddr, rdma_addr_size(raddr));
Tom Tucker07ebafb2006-08-03 16:02:42 -05001512
Steve Wise8d8293c2007-10-29 11:34:05 -05001513 ret = ib_query_device(conn_id->id.device, &attr);
1514 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001515 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001516 rdma_destroy_id(new_cm_id);
1517 goto out;
1518 }
1519
Sean Heftya1b1b612006-11-30 16:33:14 -08001520 memset(&event, 0, sizeof event);
1521 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1522 event.param.conn.private_data = iw_event->private_data;
1523 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301524 event.param.conn.initiator_depth = iw_event->ird;
1525 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08001526
1527 /*
1528 * Protect against the user destroying conn_id from another thread
1529 * until we're done accessing it.
1530 */
1531 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001532 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001533 if (ret) {
1534 /* User wants to destroy the CM ID */
1535 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001536 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001537 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001538 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001539 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001540 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001541 }
1542
Or Gerlitzde910bd2008-07-14 23:48:53 -07001543 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001544 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001545
Tom Tucker07ebafb2006-08-03 16:02:42 -05001546out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001547 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001548 return ret;
1549}
1550
Sean Heftye51060f2006-06-17 20:37:29 -07001551static int cma_ib_listen(struct rdma_id_private *id_priv)
1552{
1553 struct ib_cm_compare_data compare_data;
1554 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001555 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07001556 __be64 svc_id;
1557 int ret;
1558
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001559 id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
1560 if (IS_ERR(id))
1561 return PTR_ERR(id);
1562
1563 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07001564
Sean Heftyf4753832013-05-29 10:09:14 -07001565 addr = cma_src_addr(id_priv);
Sean Heftycf539362013-05-29 10:09:28 -07001566 svc_id = rdma_get_service_id(&id_priv->id, addr);
Sean Hefty406b6a22012-06-14 20:31:39 +00001567 if (cma_any_addr(addr) && !id_priv->afonly)
Sean Heftye51060f2006-06-17 20:37:29 -07001568 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1569 else {
1570 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1571 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1572 }
1573
1574 if (ret) {
1575 ib_destroy_cm_id(id_priv->cm_id.ib);
1576 id_priv->cm_id.ib = NULL;
1577 }
1578
1579 return ret;
1580}
1581
Tom Tucker07ebafb2006-08-03 16:02:42 -05001582static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1583{
1584 int ret;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001585 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001586
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001587 id = iw_create_cm_id(id_priv->id.device,
1588 iw_conn_req_handler,
1589 id_priv);
1590 if (IS_ERR(id))
1591 return PTR_ERR(id);
1592
1593 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001594
Steve Wise24d44a32013-07-04 16:10:44 +05301595 memcpy(&id_priv->cm_id.iw->local_addr, cma_src_addr(id_priv),
1596 rdma_addr_size(cma_src_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05001597
1598 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1599
1600 if (ret) {
1601 iw_destroy_cm_id(id_priv->cm_id.iw);
1602 id_priv->cm_id.iw = NULL;
1603 }
1604
1605 return ret;
1606}
1607
Sean Heftye51060f2006-06-17 20:37:29 -07001608static int cma_listen_handler(struct rdma_cm_id *id,
1609 struct rdma_cm_event *event)
1610{
1611 struct rdma_id_private *id_priv = id->context;
1612
1613 id->context = id_priv->id.context;
1614 id->event_handler = id_priv->id.event_handler;
1615 return id_priv->id.event_handler(id, event);
1616}
1617
1618static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1619 struct cma_device *cma_dev)
1620{
1621 struct rdma_id_private *dev_id_priv;
1622 struct rdma_cm_id *id;
1623 int ret;
1624
Sean Hefty94d0c932013-05-29 10:09:24 -07001625 if (cma_family(id_priv) == AF_IB &&
Michael Wang21655af2015-05-05 14:50:27 +02001626 !rdma_ib_or_iboe(cma_dev->device, 1))
Sean Hefty94d0c932013-05-29 10:09:24 -07001627 return;
1628
Sean Heftyb26f9b92010-04-01 17:08:41 +00001629 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
1630 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07001631 if (IS_ERR(id))
1632 return;
1633
1634 dev_id_priv = container_of(id, struct rdma_id_private, id);
1635
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001636 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftyf4753832013-05-29 10:09:14 -07001637 memcpy(cma_src_addr(dev_id_priv), cma_src_addr(id_priv),
1638 rdma_addr_size(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07001639
1640 cma_attach_to_dev(dev_id_priv, cma_dev);
1641 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001642 atomic_inc(&id_priv->refcount);
1643 dev_id_priv->internal_id = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00001644 dev_id_priv->afonly = id_priv->afonly;
Sean Heftye51060f2006-06-17 20:37:29 -07001645
1646 ret = rdma_listen(id, id_priv->backlog);
1647 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07001648 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07001649 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07001650}
1651
1652static void cma_listen_on_all(struct rdma_id_private *id_priv)
1653{
1654 struct cma_device *cma_dev;
1655
1656 mutex_lock(&lock);
1657 list_add_tail(&id_priv->list, &listen_any_list);
1658 list_for_each_entry(cma_dev, &dev_list, list)
1659 cma_listen_on_dev(id_priv, cma_dev);
1660 mutex_unlock(&lock);
1661}
1662
Sean Heftya81c9942007-08-08 15:51:06 -07001663void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1664{
1665 struct rdma_id_private *id_priv;
1666
1667 id_priv = container_of(id, struct rdma_id_private, id);
1668 id_priv->tos = (u8) tos;
1669}
1670EXPORT_SYMBOL(rdma_set_service_type);
1671
Sean Heftye51060f2006-06-17 20:37:29 -07001672static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1673 void *context)
1674{
1675 struct cma_work *work = context;
1676 struct rdma_route *route;
1677
1678 route = &work->id->id.route;
1679
1680 if (!status) {
1681 route->num_paths = 1;
1682 *route->path_rec = *path_rec;
1683 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001684 work->old_state = RDMA_CM_ROUTE_QUERY;
1685 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001686 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07001687 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001688 }
1689
1690 queue_work(cma_wq, &work->work);
1691}
1692
1693static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1694 struct cma_work *work)
1695{
Sean Heftyf4753832013-05-29 10:09:14 -07001696 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001697 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07001698 ib_sa_comp_mask comp_mask;
1699 struct sockaddr_in6 *sin6;
Sean Heftyf68194c2013-05-29 10:09:18 -07001700 struct sockaddr_ib *sib;
Sean Heftye51060f2006-06-17 20:37:29 -07001701
1702 memset(&path_rec, 0, sizeof path_rec);
Sean Heftyf4753832013-05-29 10:09:14 -07001703 rdma_addr_get_sgid(dev_addr, &path_rec.sgid);
1704 rdma_addr_get_dgid(dev_addr, &path_rec.dgid);
1705 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001706 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08001707 path_rec.reversible = 1;
Sean Heftycf539362013-05-29 10:09:28 -07001708 path_rec.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftya81c9942007-08-08 15:51:06 -07001709
1710 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1711 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1712 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1713
Sean Heftyf68194c2013-05-29 10:09:18 -07001714 switch (cma_family(id_priv)) {
1715 case AF_INET:
Sean Heftya81c9942007-08-08 15:51:06 -07001716 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1717 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07001718 break;
1719 case AF_INET6:
Sean Heftyf4753832013-05-29 10:09:14 -07001720 sin6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
Sean Heftya81c9942007-08-08 15:51:06 -07001721 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1722 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
Sean Heftyf68194c2013-05-29 10:09:18 -07001723 break;
1724 case AF_IB:
1725 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
1726 path_rec.traffic_class = (u8) (be32_to_cpu(sib->sib_flowinfo) >> 20);
1727 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1728 break;
Sean Heftya81c9942007-08-08 15:51:06 -07001729 }
Sean Heftye51060f2006-06-17 20:37:29 -07001730
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001731 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07001732 id_priv->id.port_num, &path_rec,
1733 comp_mask, timeout_ms,
1734 GFP_KERNEL, cma_query_handler,
1735 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001736
1737 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1738}
1739
David Howellsc4028952006-11-22 14:57:56 +00001740static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07001741{
David Howellsc4028952006-11-22 14:57:56 +00001742 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07001743 struct rdma_id_private *id_priv = work->id;
1744 int destroy = 0;
1745
Or Gerlitzde910bd2008-07-14 23:48:53 -07001746 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001747 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1748 goto out;
1749
1750 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001751 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001752 destroy = 1;
1753 }
1754out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001755 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001756 cma_deref_id(id_priv);
1757 if (destroy)
1758 rdma_destroy_id(&id_priv->id);
1759 kfree(work);
1760}
1761
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001762static void cma_ndev_work_handler(struct work_struct *_work)
1763{
1764 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1765 struct rdma_id_private *id_priv = work->id;
1766 int destroy = 0;
1767
1768 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001769 if (id_priv->state == RDMA_CM_DESTROYING ||
1770 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001771 goto out;
1772
1773 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001774 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001775 destroy = 1;
1776 }
1777
1778out:
1779 mutex_unlock(&id_priv->handler_mutex);
1780 cma_deref_id(id_priv);
1781 if (destroy)
1782 rdma_destroy_id(&id_priv->id);
1783 kfree(work);
1784}
1785
Sean Heftye51060f2006-06-17 20:37:29 -07001786static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1787{
1788 struct rdma_route *route = &id_priv->id.route;
1789 struct cma_work *work;
1790 int ret;
1791
1792 work = kzalloc(sizeof *work, GFP_KERNEL);
1793 if (!work)
1794 return -ENOMEM;
1795
1796 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001797 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001798 work->old_state = RDMA_CM_ROUTE_QUERY;
1799 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001800 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1801
1802 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1803 if (!route->path_rec) {
1804 ret = -ENOMEM;
1805 goto err1;
1806 }
1807
1808 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1809 if (ret)
1810 goto err2;
1811
1812 return 0;
1813err2:
1814 kfree(route->path_rec);
1815 route->path_rec = NULL;
1816err1:
1817 kfree(work);
1818 return ret;
1819}
1820
1821int rdma_set_ib_paths(struct rdma_cm_id *id,
1822 struct ib_sa_path_rec *path_rec, int num_paths)
1823{
1824 struct rdma_id_private *id_priv;
1825 int ret;
1826
1827 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001828 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1829 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07001830 return -EINVAL;
1831
Julia Lawall9893e742010-05-15 23:22:38 +02001832 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1833 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07001834 if (!id->route.path_rec) {
1835 ret = -ENOMEM;
1836 goto err;
1837 }
1838
Sean Heftyae2d9292010-03-25 19:12:36 +00001839 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07001840 return 0;
1841err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001842 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07001843 return ret;
1844}
1845EXPORT_SYMBOL(rdma_set_ib_paths);
1846
Tom Tucker07ebafb2006-08-03 16:02:42 -05001847static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1848{
1849 struct cma_work *work;
1850
1851 work = kzalloc(sizeof *work, GFP_KERNEL);
1852 if (!work)
1853 return -ENOMEM;
1854
1855 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001856 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001857 work->old_state = RDMA_CM_ROUTE_QUERY;
1858 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001859 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1860 queue_work(cma_wq, &work->work);
1861 return 0;
1862}
1863
Eyal Perryeb072c42013-11-06 15:37:24 +02001864static int iboe_tos_to_sl(struct net_device *ndev, int tos)
1865{
1866 int prio;
1867 struct net_device *dev;
1868
1869 prio = rt_tos2priority(tos);
1870 dev = ndev->priv_flags & IFF_802_1Q_VLAN ?
1871 vlan_dev_real_dev(ndev) : ndev;
1872
1873 if (dev->num_tc)
1874 return netdev_get_prio_tc_map(dev, prio);
1875
1876#if IS_ENABLED(CONFIG_VLAN_8021Q)
1877 if (ndev->priv_flags & IFF_802_1Q_VLAN)
1878 return (vlan_dev_get_egress_qos_mask(ndev, prio) &
1879 VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
1880#endif
1881 return 0;
1882}
1883
Eli Cohen3c86aa72010-10-13 21:26:51 +02001884static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1885{
1886 struct rdma_route *route = &id_priv->id.route;
1887 struct rdma_addr *addr = &route->addr;
1888 struct cma_work *work;
1889 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +02001890 struct net_device *ndev = NULL;
Matan Barakdd5f03b2013-12-12 18:03:11 +02001891
Eli Cohen3c86aa72010-10-13 21:26:51 +02001892
Eli Cohen3c86aa72010-10-13 21:26:51 +02001893 work = kzalloc(sizeof *work, GFP_KERNEL);
1894 if (!work)
1895 return -ENOMEM;
1896
1897 work->id = id_priv;
1898 INIT_WORK(&work->work, cma_work_handler);
1899
1900 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1901 if (!route->path_rec) {
1902 ret = -ENOMEM;
1903 goto err1;
1904 }
1905
1906 route->num_paths = 1;
1907
Eli Cohen3c86aa72010-10-13 21:26:51 +02001908 if (addr->dev_addr.bound_dev_if)
1909 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1910 if (!ndev) {
1911 ret = -ENODEV;
1912 goto err2;
1913 }
1914
Matan Barakdd5f03b2013-12-12 18:03:11 +02001915 route->path_rec->vlan_id = rdma_vlan_dev_vlan_id(ndev);
1916 memcpy(route->path_rec->dmac, addr->dev_addr.dst_dev_addr, ETH_ALEN);
1917 memcpy(route->path_rec->smac, ndev->dev_addr, ndev->addr_len);
Eli Cohenaf7bd462010-08-26 17:18:59 +03001918
Moni Shoua7b856272013-12-12 18:03:12 +02001919 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
1920 &route->path_rec->sgid);
1921 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.dst_addr,
1922 &route->path_rec->dgid);
Eli Cohenaf7bd462010-08-26 17:18:59 +03001923
1924 route->path_rec->hop_limit = 1;
1925 route->path_rec->reversible = 1;
1926 route->path_rec->pkey = cpu_to_be16(0xffff);
1927 route->path_rec->mtu_selector = IB_SA_EQ;
Eyal Perryeb072c42013-11-06 15:37:24 +02001928 route->path_rec->sl = iboe_tos_to_sl(ndev, id_priv->tos);
Eli Cohen3c86aa72010-10-13 21:26:51 +02001929 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1930 route->path_rec->rate_selector = IB_SA_EQ;
1931 route->path_rec->rate = iboe_get_rate(ndev);
1932 dev_put(ndev);
1933 route->path_rec->packet_life_time_selector = IB_SA_EQ;
1934 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1935 if (!route->path_rec->mtu) {
1936 ret = -EINVAL;
1937 goto err2;
1938 }
1939
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001940 work->old_state = RDMA_CM_ROUTE_QUERY;
1941 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02001942 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1943 work->event.status = 0;
1944
1945 queue_work(cma_wq, &work->work);
1946
1947 return 0;
1948
1949err2:
1950 kfree(route->path_rec);
1951 route->path_rec = NULL;
1952err1:
1953 kfree(work);
1954 return ret;
1955}
1956
Sean Heftye51060f2006-06-17 20:37:29 -07001957int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1958{
1959 struct rdma_id_private *id_priv;
1960 int ret;
1961
1962 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001963 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07001964 return -EINVAL;
1965
1966 atomic_inc(&id_priv->refcount);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001967 switch (rdma_node_get_transport(id->device->node_type)) {
1968 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02001969 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1970 case IB_LINK_LAYER_INFINIBAND:
1971 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1972 break;
1973 case IB_LINK_LAYER_ETHERNET:
1974 ret = cma_resolve_iboe_route(id_priv);
1975 break;
1976 default:
1977 ret = -ENOSYS;
1978 }
Sean Heftye51060f2006-06-17 20:37:29 -07001979 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001980 case RDMA_TRANSPORT_IWARP:
1981 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1982 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001983 default:
1984 ret = -ENOSYS;
1985 break;
1986 }
1987 if (ret)
1988 goto err;
1989
1990 return 0;
1991err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001992 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07001993 cma_deref_id(id_priv);
1994 return ret;
1995}
1996EXPORT_SYMBOL(rdma_resolve_route);
1997
Sean Hefty6a3e3622013-05-29 10:09:13 -07001998static void cma_set_loopback(struct sockaddr *addr)
1999{
2000 switch (addr->sa_family) {
2001 case AF_INET:
2002 ((struct sockaddr_in *) addr)->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2003 break;
2004 case AF_INET6:
2005 ipv6_addr_set(&((struct sockaddr_in6 *) addr)->sin6_addr,
2006 0, 0, 0, htonl(1));
2007 break;
2008 default:
2009 ib_addr_set(&((struct sockaddr_ib *) addr)->sib_addr,
2010 0, 0, 0, htonl(1));
2011 break;
2012 }
2013}
2014
Sean Heftye51060f2006-06-17 20:37:29 -07002015static int cma_bind_loopback(struct rdma_id_private *id_priv)
2016{
Sean Heftyb0569e42013-05-29 10:09:15 -07002017 struct cma_device *cma_dev, *cur_dev;
Sean Heftye51060f2006-06-17 20:37:29 -07002018 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002019 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002020 u16 pkey;
2021 int ret;
2022 u8 p;
2023
Sean Heftyb0569e42013-05-29 10:09:15 -07002024 cma_dev = NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07002025 mutex_lock(&lock);
Sean Heftyb0569e42013-05-29 10:09:15 -07002026 list_for_each_entry(cur_dev, &dev_list, list) {
2027 if (cma_family(id_priv) == AF_IB &&
Michael Wang21655af2015-05-05 14:50:27 +02002028 !rdma_ib_or_iboe(cur_dev->device, 1))
Sean Heftyb0569e42013-05-29 10:09:15 -07002029 continue;
2030
2031 if (!cma_dev)
2032 cma_dev = cur_dev;
2033
2034 for (p = 1; p <= cur_dev->device->phys_port_cnt; ++p) {
2035 if (!ib_query_port(cur_dev->device, p, &port_attr) &&
2036 port_attr.state == IB_PORT_ACTIVE) {
2037 cma_dev = cur_dev;
2038 goto port_found;
2039 }
2040 }
2041 }
2042
2043 if (!cma_dev) {
Sean Heftye51060f2006-06-17 20:37:29 -07002044 ret = -ENODEV;
2045 goto out;
2046 }
Krishna Kumare82153b2006-10-16 10:09:01 +05302047
2048 p = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07002049
2050port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002051 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002052 if (ret)
2053 goto out;
2054
2055 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
2056 if (ret)
2057 goto out;
2058
Sean Hefty6f8372b2009-11-19 13:26:06 -08002059 id_priv->id.route.addr.dev_addr.dev_type =
Michael Wang21655af2015-05-05 14:50:27 +02002060 (rdma_protocol_ib(cma_dev->device, p)) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08002061 ARPHRD_INFINIBAND : ARPHRD_ETHER;
2062
2063 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002064 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
2065 id_priv->id.port_num = p;
2066 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftyf4753832013-05-29 10:09:14 -07002067 cma_set_loopback(cma_src_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002068out:
2069 mutex_unlock(&lock);
2070 return ret;
2071}
2072
2073static void addr_handler(int status, struct sockaddr *src_addr,
2074 struct rdma_dev_addr *dev_addr, void *context)
2075{
2076 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08002077 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002078
Sean Heftya1b1b612006-11-30 16:33:14 -08002079 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002080 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002081 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
2082 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07002083 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07002084
Moni Shoua7b856272013-12-12 18:03:12 +02002085 memcpy(cma_src_addr(id_priv), src_addr, rdma_addr_size(src_addr));
Sean Hefty61a73c72006-09-01 15:33:55 -07002086 if (!status && !id_priv->cma_dev)
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002087 status = cma_acquire_dev(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -07002088
2089 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002090 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
2091 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002092 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08002093 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2094 event.status = status;
Moni Shoua7b856272013-12-12 18:03:12 +02002095 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08002096 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002097
Sean Heftya1b1b612006-11-30 16:33:14 -08002098 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002099 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002100 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002101 cma_deref_id(id_priv);
2102 rdma_destroy_id(&id_priv->id);
2103 return;
2104 }
2105out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002106 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002107 cma_deref_id(id_priv);
2108}
2109
2110static int cma_resolve_loopback(struct rdma_id_private *id_priv)
2111{
2112 struct cma_work *work;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07002113 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07002114 int ret;
2115
2116 work = kzalloc(sizeof *work, GFP_KERNEL);
2117 if (!work)
2118 return -ENOMEM;
2119
2120 if (!id_priv->cma_dev) {
2121 ret = cma_bind_loopback(id_priv);
2122 if (ret)
2123 goto err;
2124 }
2125
Sean Hefty6f8372b2009-11-19 13:26:06 -08002126 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2127 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002128
Sean Heftye51060f2006-06-17 20:37:29 -07002129 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002130 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002131 work->old_state = RDMA_CM_ADDR_QUERY;
2132 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002133 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2134 queue_work(cma_wq, &work->work);
2135 return 0;
2136err:
2137 kfree(work);
2138 return ret;
2139}
2140
Sean Heftyf17df3b2013-05-29 10:09:17 -07002141static int cma_resolve_ib_addr(struct rdma_id_private *id_priv)
2142{
2143 struct cma_work *work;
2144 int ret;
2145
2146 work = kzalloc(sizeof *work, GFP_KERNEL);
2147 if (!work)
2148 return -ENOMEM;
2149
2150 if (!id_priv->cma_dev) {
2151 ret = cma_resolve_ib_dev(id_priv);
2152 if (ret)
2153 goto err;
2154 }
2155
2156 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, (union ib_gid *)
2157 &(((struct sockaddr_ib *) &id_priv->id.route.addr.dst_addr)->sib_addr));
2158
2159 work->id = id_priv;
2160 INIT_WORK(&work->work, cma_work_handler);
2161 work->old_state = RDMA_CM_ADDR_QUERY;
2162 work->new_state = RDMA_CM_ADDR_RESOLVED;
2163 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2164 queue_work(cma_wq, &work->work);
2165 return 0;
2166err:
2167 kfree(work);
2168 return ret;
2169}
2170
Sean Heftye51060f2006-06-17 20:37:29 -07002171static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2172 struct sockaddr *dst_addr)
2173{
Sean Heftyd14714d2009-11-19 16:46:25 -08002174 if (!src_addr || !src_addr->sa_family) {
2175 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002176 src_addr->sa_family = dst_addr->sa_family;
2177 if (dst_addr->sa_family == AF_INET6) {
Sean Heftyd14714d2009-11-19 16:46:25 -08002178 ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2179 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
Sean Heftyf17df3b2013-05-29 10:09:17 -07002180 } else if (dst_addr->sa_family == AF_IB) {
2181 ((struct sockaddr_ib *) src_addr)->sib_pkey =
2182 ((struct sockaddr_ib *) dst_addr)->sib_pkey;
Sean Heftyd14714d2009-11-19 16:46:25 -08002183 }
2184 }
2185 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002186}
2187
2188int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2189 struct sockaddr *dst_addr, int timeout_ms)
2190{
2191 struct rdma_id_private *id_priv;
2192 int ret;
2193
2194 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002195 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002196 ret = cma_bind_addr(id, src_addr, dst_addr);
2197 if (ret)
2198 return ret;
2199 }
2200
Sean Hefty4ae71522013-05-29 10:09:16 -07002201 if (cma_family(id_priv) != dst_addr->sa_family)
2202 return -EINVAL;
2203
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002204 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002205 return -EINVAL;
2206
2207 atomic_inc(&id_priv->refcount);
Sean Heftyf4753832013-05-29 10:09:14 -07002208 memcpy(cma_dst_addr(id_priv), dst_addr, rdma_addr_size(dst_addr));
Sean Heftyf17df3b2013-05-29 10:09:17 -07002209 if (cma_any_addr(dst_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002210 ret = cma_resolve_loopback(id_priv);
Sean Heftyf17df3b2013-05-29 10:09:17 -07002211 } else {
2212 if (dst_addr->sa_family == AF_IB) {
2213 ret = cma_resolve_ib_addr(id_priv);
2214 } else {
2215 ret = rdma_resolve_ip(&addr_client, cma_src_addr(id_priv),
2216 dst_addr, &id->route.addr.dev_addr,
2217 timeout_ms, addr_handler, id_priv);
2218 }
2219 }
Sean Heftye51060f2006-06-17 20:37:29 -07002220 if (ret)
2221 goto err;
2222
2223 return 0;
2224err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002225 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002226 cma_deref_id(id_priv);
2227 return ret;
2228}
2229EXPORT_SYMBOL(rdma_resolve_addr);
2230
Hefty, Seana9bb7912011-05-09 22:06:10 -07002231int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2232{
2233 struct rdma_id_private *id_priv;
2234 unsigned long flags;
2235 int ret;
2236
2237 id_priv = container_of(id, struct rdma_id_private, id);
2238 spin_lock_irqsave(&id_priv->lock, flags);
Sean Heftyc8dea2f92013-05-29 10:09:08 -07002239 if (reuse || id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002240 id_priv->reuseaddr = reuse;
2241 ret = 0;
2242 } else {
2243 ret = -EINVAL;
2244 }
2245 spin_unlock_irqrestore(&id_priv->lock, flags);
2246 return ret;
2247}
2248EXPORT_SYMBOL(rdma_set_reuseaddr);
2249
Sean Hefty68602122012-06-14 20:31:39 +00002250int rdma_set_afonly(struct rdma_cm_id *id, int afonly)
2251{
2252 struct rdma_id_private *id_priv;
2253 unsigned long flags;
2254 int ret;
2255
2256 id_priv = container_of(id, struct rdma_id_private, id);
2257 spin_lock_irqsave(&id_priv->lock, flags);
2258 if (id_priv->state == RDMA_CM_IDLE || id_priv->state == RDMA_CM_ADDR_BOUND) {
2259 id_priv->options |= (1 << CMA_OPTION_AFONLY);
2260 id_priv->afonly = afonly;
2261 ret = 0;
2262 } else {
2263 ret = -EINVAL;
2264 }
2265 spin_unlock_irqrestore(&id_priv->lock, flags);
2266 return ret;
2267}
2268EXPORT_SYMBOL(rdma_set_afonly);
2269
Sean Heftye51060f2006-06-17 20:37:29 -07002270static void cma_bind_port(struct rdma_bind_list *bind_list,
2271 struct rdma_id_private *id_priv)
2272{
Sean Hefty58afdcb2013-05-29 10:09:11 -07002273 struct sockaddr *addr;
2274 struct sockaddr_ib *sib;
2275 u64 sid, mask;
2276 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07002277
Sean Heftyf4753832013-05-29 10:09:14 -07002278 addr = cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002279 port = htons(bind_list->port);
2280
2281 switch (addr->sa_family) {
2282 case AF_INET:
2283 ((struct sockaddr_in *) addr)->sin_port = port;
2284 break;
2285 case AF_INET6:
2286 ((struct sockaddr_in6 *) addr)->sin6_port = port;
2287 break;
2288 case AF_IB:
2289 sib = (struct sockaddr_ib *) addr;
2290 sid = be64_to_cpu(sib->sib_sid);
2291 mask = be64_to_cpu(sib->sib_sid_mask);
2292 sib->sib_sid = cpu_to_be64((sid & mask) | (u64) ntohs(port));
2293 sib->sib_sid_mask = cpu_to_be64(~0ULL);
2294 break;
2295 }
Sean Heftye51060f2006-06-17 20:37:29 -07002296 id_priv->bind_list = bind_list;
2297 hlist_add_head(&id_priv->node, &bind_list->owners);
2298}
2299
2300static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2301 unsigned short snum)
2302{
2303 struct rdma_bind_list *bind_list;
Tejun Heo3b069c52013-02-27 17:04:16 -08002304 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002305
Sean Heftycb164b82007-03-05 12:50:17 -08002306 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002307 if (!bind_list)
2308 return -ENOMEM;
2309
Tejun Heo3b069c52013-02-27 17:04:16 -08002310 ret = idr_alloc(ps, bind_list, snum, snum + 1, GFP_KERNEL);
2311 if (ret < 0)
2312 goto err;
Sean Heftye51060f2006-06-17 20:37:29 -07002313
2314 bind_list->ps = ps;
Tejun Heo3b069c52013-02-27 17:04:16 -08002315 bind_list->port = (unsigned short)ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002316 cma_bind_port(bind_list, id_priv);
2317 return 0;
Tejun Heo3b069c52013-02-27 17:04:16 -08002318err:
Sean Heftyaedec082007-01-29 16:41:23 -08002319 kfree(bind_list);
Tejun Heo3b069c52013-02-27 17:04:16 -08002320 return ret == -ENOSPC ? -EADDRNOTAVAIL : ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002321}
2322
2323static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2324{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002325 static unsigned int last_used_port;
2326 int low, high, remaining;
2327 unsigned int rover;
Sean Heftyaedec082007-01-29 16:41:23 -08002328
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07002329 inet_get_local_port_range(&init_net, &low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002330 remaining = (high - low) + 1;
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05002331 rover = prandom_u32() % remaining + low;
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002332retry:
2333 if (last_used_port != rover &&
2334 !idr_find(ps, (unsigned short) rover)) {
2335 int ret = cma_alloc_port(ps, id_priv, rover);
2336 /*
2337 * Remember previously used port number in order to avoid
2338 * re-using same port immediately after it is closed.
2339 */
2340 if (!ret)
2341 last_used_port = rover;
2342 if (ret != -EADDRNOTAVAIL)
2343 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002344 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002345 if (--remaining) {
2346 rover++;
2347 if ((rover < low) || (rover > high))
2348 rover = low;
2349 goto retry;
2350 }
2351 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002352}
2353
Hefty, Seana9bb7912011-05-09 22:06:10 -07002354/*
2355 * Check that the requested port is available. This is called when trying to
2356 * bind to a specific port, or when trying to listen on a bound port. In
2357 * the latter case, the provided id_priv may already be on the bind_list, but
2358 * we still need to check that it's okay to start listening.
2359 */
2360static int cma_check_port(struct rdma_bind_list *bind_list,
2361 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002362{
2363 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002364 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002365
Sean Heftyf4753832013-05-29 10:09:14 -07002366 addr = cma_src_addr(id_priv);
Sasha Levinb67bfe02013-02-27 17:06:00 -08002367 hlist_for_each_entry(cur_id, &bind_list->owners, node) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002368 if (id_priv == cur_id)
2369 continue;
2370
Sean Hefty5b0ec992012-06-14 20:31:39 +00002371 if ((cur_id->state != RDMA_CM_LISTEN) && reuseaddr &&
2372 cur_id->reuseaddr)
2373 continue;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002374
Sean Heftyf4753832013-05-29 10:09:14 -07002375 cur_addr = cma_src_addr(cur_id);
Sean Hefty5b0ec992012-06-14 20:31:39 +00002376 if (id_priv->afonly && cur_id->afonly &&
2377 (addr->sa_family != cur_addr->sa_family))
2378 continue;
2379
2380 if (cma_any_addr(addr) || cma_any_addr(cur_addr))
2381 return -EADDRNOTAVAIL;
2382
2383 if (!cma_addr_cmp(addr, cur_addr))
2384 return -EADDRINUSE;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002385 }
2386 return 0;
2387}
2388
2389static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2390{
2391 struct rdma_bind_list *bind_list;
2392 unsigned short snum;
2393 int ret;
2394
Sean Heftyf4753832013-05-29 10:09:14 -07002395 snum = ntohs(cma_port(cma_src_addr(id_priv)));
Sean Heftye51060f2006-06-17 20:37:29 -07002396 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2397 return -EACCES;
2398
2399 bind_list = idr_find(ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002400 if (!bind_list) {
2401 ret = cma_alloc_port(ps, id_priv, snum);
2402 } else {
2403 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2404 if (!ret)
2405 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002406 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07002407 return ret;
2408}
Sean Heftye51060f2006-06-17 20:37:29 -07002409
Hefty, Seana9bb7912011-05-09 22:06:10 -07002410static int cma_bind_listen(struct rdma_id_private *id_priv)
2411{
2412 struct rdma_bind_list *bind_list = id_priv->bind_list;
2413 int ret = 0;
2414
2415 mutex_lock(&lock);
2416 if (bind_list->owners.first->next)
2417 ret = cma_check_port(bind_list, id_priv, 0);
2418 mutex_unlock(&lock);
2419 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002420}
2421
Sean Hefty58afdcb2013-05-29 10:09:11 -07002422static struct idr *cma_select_inet_ps(struct rdma_id_private *id_priv)
2423{
2424 switch (id_priv->id.ps) {
Sean Hefty58afdcb2013-05-29 10:09:11 -07002425 case RDMA_PS_TCP:
2426 return &tcp_ps;
2427 case RDMA_PS_UDP:
2428 return &udp_ps;
2429 case RDMA_PS_IPOIB:
2430 return &ipoib_ps;
2431 case RDMA_PS_IB:
2432 return &ib_ps;
2433 default:
2434 return NULL;
2435 }
2436}
2437
2438static struct idr *cma_select_ib_ps(struct rdma_id_private *id_priv)
2439{
2440 struct idr *ps = NULL;
2441 struct sockaddr_ib *sib;
2442 u64 sid_ps, mask, sid;
2443
Sean Heftyf4753832013-05-29 10:09:14 -07002444 sib = (struct sockaddr_ib *) cma_src_addr(id_priv);
Sean Hefty58afdcb2013-05-29 10:09:11 -07002445 mask = be64_to_cpu(sib->sib_sid_mask) & RDMA_IB_IP_PS_MASK;
2446 sid = be64_to_cpu(sib->sib_sid) & mask;
2447
2448 if ((id_priv->id.ps == RDMA_PS_IB) && (sid == (RDMA_IB_IP_PS_IB & mask))) {
2449 sid_ps = RDMA_IB_IP_PS_IB;
2450 ps = &ib_ps;
2451 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_TCP)) &&
2452 (sid == (RDMA_IB_IP_PS_TCP & mask))) {
2453 sid_ps = RDMA_IB_IP_PS_TCP;
2454 ps = &tcp_ps;
2455 } else if (((id_priv->id.ps == RDMA_PS_IB) || (id_priv->id.ps == RDMA_PS_UDP)) &&
2456 (sid == (RDMA_IB_IP_PS_UDP & mask))) {
2457 sid_ps = RDMA_IB_IP_PS_UDP;
2458 ps = &udp_ps;
2459 }
2460
2461 if (ps) {
2462 sib->sib_sid = cpu_to_be64(sid_ps | ntohs(cma_port((struct sockaddr *) sib)));
2463 sib->sib_sid_mask = cpu_to_be64(RDMA_IB_IP_PS_MASK |
2464 be64_to_cpu(sib->sib_sid_mask));
2465 }
2466 return ps;
2467}
2468
Sean Heftye51060f2006-06-17 20:37:29 -07002469static int cma_get_port(struct rdma_id_private *id_priv)
2470{
2471 struct idr *ps;
2472 int ret;
2473
Sean Heftyf4753832013-05-29 10:09:14 -07002474 if (cma_family(id_priv) != AF_IB)
Sean Hefty58afdcb2013-05-29 10:09:11 -07002475 ps = cma_select_inet_ps(id_priv);
2476 else
2477 ps = cma_select_ib_ps(id_priv);
2478 if (!ps)
Sean Heftye51060f2006-06-17 20:37:29 -07002479 return -EPROTONOSUPPORT;
Sean Heftye51060f2006-06-17 20:37:29 -07002480
2481 mutex_lock(&lock);
Sean Heftyf4753832013-05-29 10:09:14 -07002482 if (cma_any_port(cma_src_addr(id_priv)))
Sean Heftyaedec082007-01-29 16:41:23 -08002483 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002484 else
2485 ret = cma_use_port(ps, id_priv);
2486 mutex_unlock(&lock);
2487
2488 return ret;
2489}
2490
Sean Heftyd14714d2009-11-19 16:46:25 -08002491static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2492 struct sockaddr *addr)
2493{
Roland Dreierd90f9b32012-07-05 22:39:34 -07002494#if IS_ENABLED(CONFIG_IPV6)
Sean Heftyd14714d2009-11-19 16:46:25 -08002495 struct sockaddr_in6 *sin6;
2496
2497 if (addr->sa_family != AF_INET6)
2498 return 0;
2499
2500 sin6 = (struct sockaddr_in6 *) addr;
Somnath Kotur5462edd2013-11-16 09:30:01 +05302501
2502 if (!(ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL))
2503 return 0;
2504
2505 if (!sin6->sin6_scope_id)
Sean Heftyd14714d2009-11-19 16:46:25 -08002506 return -EINVAL;
2507
2508 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2509#endif
2510 return 0;
2511}
2512
Hefty, Seana9bb7912011-05-09 22:06:10 -07002513int rdma_listen(struct rdma_cm_id *id, int backlog)
2514{
2515 struct rdma_id_private *id_priv;
2516 int ret;
2517
2518 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002519 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftyf4753832013-05-29 10:09:14 -07002520 id->route.addr.src_addr.ss_family = AF_INET;
2521 ret = rdma_bind_addr(id, cma_src_addr(id_priv));
Hefty, Seana9bb7912011-05-09 22:06:10 -07002522 if (ret)
2523 return ret;
2524 }
2525
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002526 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07002527 return -EINVAL;
2528
2529 if (id_priv->reuseaddr) {
2530 ret = cma_bind_listen(id_priv);
2531 if (ret)
2532 goto err;
2533 }
2534
2535 id_priv->backlog = backlog;
2536 if (id->device) {
Michael Wang21655af2015-05-05 14:50:27 +02002537 if (rdma_ib_or_iboe(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002538 ret = cma_ib_listen(id_priv);
2539 if (ret)
2540 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02002541 } else if (rdma_protocol_iwarp(id->device, 1)) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002542 ret = cma_iw_listen(id_priv, backlog);
2543 if (ret)
2544 goto err;
Michael Wang21655af2015-05-05 14:50:27 +02002545 } else {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002546 ret = -ENOSYS;
2547 goto err;
2548 }
2549 } else
2550 cma_listen_on_all(id_priv);
2551
2552 return 0;
2553err:
2554 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002555 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002556 return ret;
2557}
2558EXPORT_SYMBOL(rdma_listen);
2559
Sean Heftye51060f2006-06-17 20:37:29 -07002560int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2561{
2562 struct rdma_id_private *id_priv;
2563 int ret;
2564
Sean Hefty680f9202013-05-29 10:09:12 -07002565 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6 &&
2566 addr->sa_family != AF_IB)
Sean Heftye51060f2006-06-17 20:37:29 -07002567 return -EAFNOSUPPORT;
2568
2569 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002570 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002571 return -EINVAL;
2572
Sean Heftyd14714d2009-11-19 16:46:25 -08002573 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2574 if (ret)
2575 goto err1;
2576
Moni Shoua7b856272013-12-12 18:03:12 +02002577 memcpy(cma_src_addr(id_priv), addr, rdma_addr_size(addr));
Sean Hefty8523c042010-02-08 16:41:15 -08002578 if (!cma_any_addr(addr)) {
Sean Hefty680f9202013-05-29 10:09:12 -07002579 ret = cma_translate_addr(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002580 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002581 goto err1;
2582
Doug Ledfordbe9130c2013-09-24 17:16:28 -04002583 ret = cma_acquire_dev(id_priv, NULL);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002584 if (ret)
2585 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002586 }
2587
Sean Hefty68602122012-06-14 20:31:39 +00002588 if (!(id_priv->options & (1 << CMA_OPTION_AFONLY))) {
2589 if (addr->sa_family == AF_INET)
2590 id_priv->afonly = 1;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002591#if IS_ENABLED(CONFIG_IPV6)
Sean Hefty68602122012-06-14 20:31:39 +00002592 else if (addr->sa_family == AF_INET6)
2593 id_priv->afonly = init_net.ipv6.sysctl.bindv6only;
Sean Hefty5b0ec992012-06-14 20:31:39 +00002594#endif
Sean Hefty68602122012-06-14 20:31:39 +00002595 }
Sean Heftye51060f2006-06-17 20:37:29 -07002596 ret = cma_get_port(id_priv);
2597 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002598 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002599
2600 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07002601err2:
Sean Heftya396d432011-02-23 09:05:39 -08002602 if (id_priv->cma_dev)
2603 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002604err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002605 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07002606 return ret;
2607}
2608EXPORT_SYMBOL(rdma_bind_addr);
2609
Sean Heftyf4753832013-05-29 10:09:14 -07002610static int cma_format_hdr(void *hdr, struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -07002611{
Sean Heftye51060f2006-06-17 20:37:29 -07002612 struct cma_hdr *cma_hdr;
Sean Heftye51060f2006-06-17 20:37:29 -07002613
Sean Hefty01602f12013-05-29 10:09:20 -07002614 cma_hdr = hdr;
2615 cma_hdr->cma_version = CMA_VERSION;
Sean Heftyf4753832013-05-29 10:09:14 -07002616 if (cma_family(id_priv) == AF_INET) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002617 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07002618
Sean Heftyf4753832013-05-29 10:09:14 -07002619 src4 = (struct sockaddr_in *) cma_src_addr(id_priv);
2620 dst4 = (struct sockaddr_in *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002621
Sean Hefty01602f12013-05-29 10:09:20 -07002622 cma_set_ip_ver(cma_hdr, 4);
2623 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2624 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2625 cma_hdr->port = src4->sin_port;
Sean Heftye8160e12013-05-29 10:09:22 -07002626 } else if (cma_family(id_priv) == AF_INET6) {
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002627 struct sockaddr_in6 *src6, *dst6;
2628
Sean Heftyf4753832013-05-29 10:09:14 -07002629 src6 = (struct sockaddr_in6 *) cma_src_addr(id_priv);
2630 dst6 = (struct sockaddr_in6 *) cma_dst_addr(id_priv);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002631
Sean Hefty01602f12013-05-29 10:09:20 -07002632 cma_set_ip_ver(cma_hdr, 6);
2633 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2634 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2635 cma_hdr->port = src6->sin6_port;
Sean Heftye51060f2006-06-17 20:37:29 -07002636 }
2637 return 0;
2638}
2639
Sean Hefty628e5f62006-11-30 16:44:16 -08002640static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2641 struct ib_cm_event *ib_event)
2642{
2643 struct rdma_id_private *id_priv = cm_id->context;
2644 struct rdma_cm_event event;
2645 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2646 int ret = 0;
2647
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002648 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07002649 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08002650
Sean Hefty8aa08602007-05-07 11:49:00 -07002651 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08002652 switch (ib_event->event) {
2653 case IB_CM_SIDR_REQ_ERROR:
2654 event.event = RDMA_CM_EVENT_UNREACHABLE;
2655 event.status = -ETIMEDOUT;
2656 break;
2657 case IB_CM_SIDR_REP_RECEIVED:
2658 event.param.ud.private_data = ib_event->private_data;
2659 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2660 if (rep->status != IB_SIDR_SUCCESS) {
2661 event.event = RDMA_CM_EVENT_UNREACHABLE;
2662 event.status = ib_event->param.sidr_rep_rcvd.status;
2663 break;
2664 }
Sean Hefty5c438132013-05-29 10:09:23 -07002665 ret = cma_set_qkey(id_priv, rep->qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002666 if (ret) {
2667 event.event = RDMA_CM_EVENT_ADDR_ERROR;
Sean Hefty5c438132013-05-29 10:09:23 -07002668 event.status = ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002669 break;
2670 }
2671 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2672 id_priv->id.route.path_rec,
2673 &event.param.ud.ah_attr);
2674 event.param.ud.qp_num = rep->qpn;
2675 event.param.ud.qkey = rep->qkey;
2676 event.event = RDMA_CM_EVENT_ESTABLISHED;
2677 event.status = 0;
2678 break;
2679 default:
Roland Dreier468f2232008-07-14 23:48:47 -07002680 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08002681 ib_event->event);
2682 goto out;
2683 }
2684
2685 ret = id_priv->id.event_handler(&id_priv->id, &event);
2686 if (ret) {
2687 /* Destroy the CM ID by returning a non-zero value. */
2688 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002689 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002690 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002691 rdma_destroy_id(&id_priv->id);
2692 return ret;
2693 }
2694out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002695 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002696 return ret;
2697}
2698
2699static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2700 struct rdma_conn_param *conn_param)
2701{
2702 struct ib_cm_sidr_req_param req;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002703 struct ib_cm_id *id;
Sean Heftye511d1a2013-07-24 15:06:08 -07002704 void *private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07002705 int offset, ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002706
Sean Heftye511d1a2013-07-24 15:06:08 -07002707 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07002708 offset = cma_user_data_offset(id_priv);
2709 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00002710 if (req.private_data_len < conn_param->private_data_len)
2711 return -EINVAL;
2712
Sean Heftye8160e12013-05-29 10:09:22 -07002713 if (req.private_data_len) {
Sean Heftye511d1a2013-07-24 15:06:08 -07002714 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2715 if (!private_data)
Sean Heftye8160e12013-05-29 10:09:22 -07002716 return -ENOMEM;
2717 } else {
Sean Heftye511d1a2013-07-24 15:06:08 -07002718 private_data = NULL;
Sean Heftye8160e12013-05-29 10:09:22 -07002719 }
Sean Hefty628e5f62006-11-30 16:44:16 -08002720
2721 if (conn_param->private_data && conn_param->private_data_len)
Sean Heftye511d1a2013-07-24 15:06:08 -07002722 memcpy(private_data + offset, conn_param->private_data,
2723 conn_param->private_data_len);
Sean Hefty628e5f62006-11-30 16:44:16 -08002724
Sean Heftye511d1a2013-07-24 15:06:08 -07002725 if (private_data) {
2726 ret = cma_format_hdr(private_data, id_priv);
Sean Heftye8160e12013-05-29 10:09:22 -07002727 if (ret)
2728 goto out;
Sean Heftye511d1a2013-07-24 15:06:08 -07002729 req.private_data = private_data;
Sean Heftye8160e12013-05-29 10:09:22 -07002730 }
Sean Hefty628e5f62006-11-30 16:44:16 -08002731
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002732 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
2733 id_priv);
2734 if (IS_ERR(id)) {
2735 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08002736 goto out;
2737 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002738 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08002739
Sean Heftyf4753832013-05-29 10:09:14 -07002740 req.path = id_priv->id.route.path_rec;
Sean Heftycf539362013-05-29 10:09:28 -07002741 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Hefty628e5f62006-11-30 16:44:16 -08002742 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2743 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2744
2745 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2746 if (ret) {
2747 ib_destroy_cm_id(id_priv->cm_id.ib);
2748 id_priv->cm_id.ib = NULL;
2749 }
2750out:
Sean Heftye511d1a2013-07-24 15:06:08 -07002751 kfree(private_data);
Sean Hefty628e5f62006-11-30 16:44:16 -08002752 return ret;
2753}
2754
Sean Heftye51060f2006-06-17 20:37:29 -07002755static int cma_connect_ib(struct rdma_id_private *id_priv,
2756 struct rdma_conn_param *conn_param)
2757{
2758 struct ib_cm_req_param req;
2759 struct rdma_route *route;
2760 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002761 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07002762 int offset, ret;
2763
2764 memset(&req, 0, sizeof req);
Sean Heftye8160e12013-05-29 10:09:22 -07002765 offset = cma_user_data_offset(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002766 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00002767 if (req.private_data_len < conn_param->private_data_len)
2768 return -EINVAL;
2769
Sean Heftye8160e12013-05-29 10:09:22 -07002770 if (req.private_data_len) {
2771 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2772 if (!private_data)
2773 return -ENOMEM;
2774 } else {
2775 private_data = NULL;
2776 }
Sean Heftye51060f2006-06-17 20:37:29 -07002777
2778 if (conn_param->private_data && conn_param->private_data_len)
2779 memcpy(private_data + offset, conn_param->private_data,
2780 conn_param->private_data_len);
2781
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002782 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
2783 if (IS_ERR(id)) {
2784 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07002785 goto out;
2786 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002787 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07002788
2789 route = &id_priv->id.route;
Sean Heftye8160e12013-05-29 10:09:22 -07002790 if (private_data) {
2791 ret = cma_format_hdr(private_data, id_priv);
2792 if (ret)
2793 goto out;
2794 req.private_data = private_data;
2795 }
Sean Heftye51060f2006-06-17 20:37:29 -07002796
2797 req.primary_path = &route->path_rec[0];
2798 if (route->num_paths == 2)
2799 req.alternate_path = &route->path_rec[1];
2800
Sean Heftycf539362013-05-29 10:09:28 -07002801 req.service_id = rdma_get_service_id(&id_priv->id, cma_dst_addr(id_priv));
Sean Heftye51060f2006-06-17 20:37:29 -07002802 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07002803 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07002804 req.starting_psn = id_priv->seq_num;
2805 req.responder_resources = conn_param->responder_resources;
2806 req.initiator_depth = conn_param->initiator_depth;
2807 req.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00002808 req.retry_count = min_t(u8, 7, conn_param->retry_count);
2809 req.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07002810 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2811 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2812 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2813 req.srq = id_priv->srq ? 1 : 0;
2814
2815 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2816out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002817 if (ret && !IS_ERR(id)) {
2818 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07002819 id_priv->cm_id.ib = NULL;
2820 }
2821
Sean Heftye51060f2006-06-17 20:37:29 -07002822 kfree(private_data);
2823 return ret;
2824}
2825
Tom Tucker07ebafb2006-08-03 16:02:42 -05002826static int cma_connect_iw(struct rdma_id_private *id_priv,
2827 struct rdma_conn_param *conn_param)
2828{
2829 struct iw_cm_id *cm_id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002830 int ret;
2831 struct iw_cm_conn_param iw_param;
2832
2833 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002834 if (IS_ERR(cm_id))
2835 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002836
2837 id_priv->cm_id.iw = cm_id;
2838
Steve Wise24d44a32013-07-04 16:10:44 +05302839 memcpy(&cm_id->local_addr, cma_src_addr(id_priv),
2840 rdma_addr_size(cma_src_addr(id_priv)));
2841 memcpy(&cm_id->remote_addr, cma_dst_addr(id_priv),
2842 rdma_addr_size(cma_dst_addr(id_priv)));
Tom Tucker07ebafb2006-08-03 16:02:42 -05002843
Sean Hefty5851bb82008-01-04 10:47:12 -08002844 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07002845 if (ret)
2846 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002847
Hefty, Seanf45ee802011-10-06 09:33:04 -07002848 if (conn_param) {
2849 iw_param.ord = conn_param->initiator_depth;
2850 iw_param.ird = conn_param->responder_resources;
2851 iw_param.private_data = conn_param->private_data;
2852 iw_param.private_data_len = conn_param->private_data_len;
2853 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
2854 } else {
2855 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002856 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07002857 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002858 ret = iw_cm_connect(cm_id, &iw_param);
2859out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002860 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07002861 iw_destroy_cm_id(cm_id);
2862 id_priv->cm_id.iw = NULL;
2863 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002864 return ret;
2865}
2866
Sean Heftye51060f2006-06-17 20:37:29 -07002867int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2868{
2869 struct rdma_id_private *id_priv;
2870 int ret;
2871
2872 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002873 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07002874 return -EINVAL;
2875
2876 if (!id->qp) {
2877 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002878 id_priv->srq = conn_param->srq;
2879 }
2880
Michael Wang21655af2015-05-05 14:50:27 +02002881 if (rdma_ib_or_iboe(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00002882 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08002883 ret = cma_resolve_ib_udp(id_priv, conn_param);
2884 else
2885 ret = cma_connect_ib(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02002886 } else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05002887 ret = cma_connect_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02002888 else
Sean Heftye51060f2006-06-17 20:37:29 -07002889 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07002890 if (ret)
2891 goto err;
2892
2893 return 0;
2894err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002895 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002896 return ret;
2897}
2898EXPORT_SYMBOL(rdma_connect);
2899
2900static int cma_accept_ib(struct rdma_id_private *id_priv,
2901 struct rdma_conn_param *conn_param)
2902{
2903 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08002904 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002905
Sean Hefty5851bb82008-01-04 10:47:12 -08002906 ret = cma_modify_qp_rtr(id_priv, conn_param);
2907 if (ret)
2908 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08002909
Sean Hefty5851bb82008-01-04 10:47:12 -08002910 ret = cma_modify_qp_rts(id_priv, conn_param);
2911 if (ret)
2912 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002913
2914 memset(&rep, 0, sizeof rep);
2915 rep.qp_num = id_priv->qp_num;
2916 rep.starting_psn = id_priv->seq_num;
2917 rep.private_data = conn_param->private_data;
2918 rep.private_data_len = conn_param->private_data_len;
2919 rep.responder_resources = conn_param->responder_resources;
2920 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07002921 rep.failover_accepted = 0;
2922 rep.flow_control = conn_param->flow_control;
Sean Hefty4ede1782012-10-03 23:42:31 +00002923 rep.rnr_retry_count = min_t(u8, 7, conn_param->rnr_retry_count);
Sean Heftye51060f2006-06-17 20:37:29 -07002924 rep.srq = id_priv->srq ? 1 : 0;
2925
Sean Hefty0fe313b2006-11-30 16:37:15 -08002926 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2927out:
2928 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002929}
2930
Tom Tucker07ebafb2006-08-03 16:02:42 -05002931static int cma_accept_iw(struct rdma_id_private *id_priv,
2932 struct rdma_conn_param *conn_param)
2933{
2934 struct iw_cm_conn_param iw_param;
2935 int ret;
2936
Sean Hefty5851bb82008-01-04 10:47:12 -08002937 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002938 if (ret)
2939 return ret;
2940
2941 iw_param.ord = conn_param->initiator_depth;
2942 iw_param.ird = conn_param->responder_resources;
2943 iw_param.private_data = conn_param->private_data;
2944 iw_param.private_data_len = conn_param->private_data_len;
2945 if (id_priv->id.qp) {
2946 iw_param.qpn = id_priv->qp_num;
2947 } else
2948 iw_param.qpn = conn_param->qp_num;
2949
2950 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2951}
2952
Sean Hefty628e5f62006-11-30 16:44:16 -08002953static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
Sean Hefty5c438132013-05-29 10:09:23 -07002954 enum ib_cm_sidr_status status, u32 qkey,
Sean Hefty628e5f62006-11-30 16:44:16 -08002955 const void *private_data, int private_data_len)
2956{
2957 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002958 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002959
2960 memset(&rep, 0, sizeof rep);
2961 rep.status = status;
2962 if (status == IB_SIDR_SUCCESS) {
Sean Hefty5c438132013-05-29 10:09:23 -07002963 ret = cma_set_qkey(id_priv, qkey);
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002964 if (ret)
2965 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002966 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002967 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08002968 }
2969 rep.private_data = private_data;
2970 rep.private_data_len = private_data_len;
2971
2972 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2973}
2974
Sean Heftye51060f2006-06-17 20:37:29 -07002975int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2976{
2977 struct rdma_id_private *id_priv;
2978 int ret;
2979
2980 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00002981
2982 id_priv->owner = task_pid_nr(current);
2983
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002984 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07002985 return -EINVAL;
2986
2987 if (!id->qp && conn_param) {
2988 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002989 id_priv->srq = conn_param->srq;
2990 }
2991
Michael Wang21655af2015-05-05 14:50:27 +02002992 if (rdma_ib_or_iboe(id->device, id->port_num)) {
Hefty, Seanf45ee802011-10-06 09:33:04 -07002993 if (id->qp_type == IB_QPT_UD) {
2994 if (conn_param)
2995 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07002996 conn_param->qkey,
Hefty, Seanf45ee802011-10-06 09:33:04 -07002997 conn_param->private_data,
2998 conn_param->private_data_len);
2999 else
3000 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
Sean Hefty5c438132013-05-29 10:09:23 -07003001 0, NULL, 0);
Hefty, Seanf45ee802011-10-06 09:33:04 -07003002 } else {
3003 if (conn_param)
3004 ret = cma_accept_ib(id_priv, conn_param);
3005 else
3006 ret = cma_rep_recv(id_priv);
3007 }
Michael Wang21655af2015-05-05 14:50:27 +02003008 } else if (rdma_protocol_iwarp(id->device, id->port_num))
Tom Tucker07ebafb2006-08-03 16:02:42 -05003009 ret = cma_accept_iw(id_priv, conn_param);
Michael Wang21655af2015-05-05 14:50:27 +02003010 else
Sean Heftye51060f2006-06-17 20:37:29 -07003011 ret = -ENOSYS;
Sean Heftye51060f2006-06-17 20:37:29 -07003012
3013 if (ret)
3014 goto reject;
3015
3016 return 0;
3017reject:
Sean Heftyc5483382007-09-24 13:19:09 -07003018 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003019 rdma_reject(id, NULL, 0);
3020 return ret;
3021}
3022EXPORT_SYMBOL(rdma_accept);
3023
Sean Hefty0fe313b2006-11-30 16:37:15 -08003024int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
3025{
3026 struct rdma_id_private *id_priv;
3027 int ret;
3028
3029 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003030 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08003031 return -EINVAL;
3032
3033 switch (id->device->node_type) {
3034 case RDMA_NODE_IB_CA:
3035 ret = ib_cm_notify(id_priv->cm_id.ib, event);
3036 break;
3037 default:
3038 ret = 0;
3039 break;
3040 }
3041 return ret;
3042}
3043EXPORT_SYMBOL(rdma_notify);
3044
Sean Heftye51060f2006-06-17 20:37:29 -07003045int rdma_reject(struct rdma_cm_id *id, const void *private_data,
3046 u8 private_data_len)
3047{
3048 struct rdma_id_private *id_priv;
3049 int ret;
3050
3051 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003052 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003053 return -EINVAL;
3054
Michael Wang21655af2015-05-05 14:50:27 +02003055 if (rdma_ib_or_iboe(id->device, id->port_num)) {
Sean Heftyb26f9b92010-04-01 17:08:41 +00003056 if (id->qp_type == IB_QPT_UD)
Sean Hefty5c438132013-05-29 10:09:23 -07003057 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, 0,
Sean Hefty628e5f62006-11-30 16:44:16 -08003058 private_data, private_data_len);
3059 else
3060 ret = ib_send_cm_rej(id_priv->cm_id.ib,
3061 IB_CM_REJ_CONSUMER_DEFINED, NULL,
3062 0, private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003063 } else if (rdma_protocol_iwarp(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003064 ret = iw_cm_reject(id_priv->cm_id.iw,
3065 private_data, private_data_len);
Michael Wang21655af2015-05-05 14:50:27 +02003066 } else
Sean Heftye51060f2006-06-17 20:37:29 -07003067 ret = -ENOSYS;
Michael Wang21655af2015-05-05 14:50:27 +02003068
Sean Heftye51060f2006-06-17 20:37:29 -07003069 return ret;
3070}
3071EXPORT_SYMBOL(rdma_reject);
3072
3073int rdma_disconnect(struct rdma_cm_id *id)
3074{
3075 struct rdma_id_private *id_priv;
3076 int ret;
3077
3078 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00003079 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07003080 return -EINVAL;
3081
Michael Wang21655af2015-05-05 14:50:27 +02003082 if (rdma_ib_or_iboe(id->device, id->port_num)) {
Sean Heftyc5483382007-09-24 13:19:09 -07003083 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05003084 if (ret)
3085 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003086 /* Initiate or respond to a disconnect. */
3087 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
3088 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003089 } else if (rdma_protocol_iwarp(id->device, id->port_num)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05003090 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
Michael Wang21655af2015-05-05 14:50:27 +02003091 } else
Tom Tucker07ebafb2006-08-03 16:02:42 -05003092 ret = -EINVAL;
Michael Wang21655af2015-05-05 14:50:27 +02003093
Sean Heftye51060f2006-06-17 20:37:29 -07003094out:
3095 return ret;
3096}
3097EXPORT_SYMBOL(rdma_disconnect);
3098
Sean Heftyc8f6a362007-02-15 17:00:18 -08003099static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
3100{
3101 struct rdma_id_private *id_priv;
3102 struct cma_multicast *mc = multicast->context;
3103 struct rdma_cm_event event;
3104 int ret;
3105
3106 id_priv = mc->id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003107 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
3108 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07003109 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08003110
Sean Hefty5c438132013-05-29 10:09:23 -07003111 if (!status)
3112 status = cma_set_qkey(id_priv, be32_to_cpu(multicast->rec.qkey));
Sean Heftyc5483382007-09-24 13:19:09 -07003113 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003114 if (!status && id_priv->id.qp)
3115 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003116 be16_to_cpu(multicast->rec.mlid));
Sean Heftyc5483382007-09-24 13:19:09 -07003117 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003118
3119 memset(&event, 0, sizeof event);
3120 event.status = status;
3121 event.param.ud.private_data = mc->context;
3122 if (!status) {
3123 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
3124 ib_init_ah_from_mcmember(id_priv->id.device,
3125 id_priv->id.port_num, &multicast->rec,
3126 &event.param.ud.ah_attr);
3127 event.param.ud.qp_num = 0xFFFFFF;
3128 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
3129 } else
3130 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
3131
3132 ret = id_priv->id.event_handler(&id_priv->id, &event);
3133 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003134 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003135 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003136 rdma_destroy_id(&id_priv->id);
3137 return 0;
3138 }
Sean Hefty8aa08602007-05-07 11:49:00 -07003139
Or Gerlitzde910bd2008-07-14 23:48:53 -07003140 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003141 return 0;
3142}
3143
3144static void cma_set_mgid(struct rdma_id_private *id_priv,
3145 struct sockaddr *addr, union ib_gid *mgid)
3146{
3147 unsigned char mc_map[MAX_ADDR_LEN];
3148 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3149 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
3150 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
3151
3152 if (cma_any_addr(addr)) {
3153 memset(mgid, 0, sizeof *mgid);
3154 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08003155 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08003156 0xFF10A01B)) {
3157 /* IPv6 address is an SA assigned MGID. */
3158 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003159 } else if (addr->sa_family == AF_IB) {
3160 memcpy(mgid, &((struct sockaddr_ib *) addr)->sib_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08003161 } else if ((addr->sa_family == AF_INET6)) {
3162 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
3163 if (id_priv->id.ps == RDMA_PS_UDP)
3164 mc_map[7] = 0x01; /* Use RDMA CM signature */
3165 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003166 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07003167 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003168 if (id_priv->id.ps == RDMA_PS_UDP)
3169 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08003170 *mgid = *(union ib_gid *) (mc_map + 4);
3171 }
3172}
3173
3174static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
3175 struct cma_multicast *mc)
3176{
3177 struct ib_sa_mcmember_rec rec;
3178 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3179 ib_sa_comp_mask comp_mask;
3180 int ret;
3181
3182 ib_addr_get_mgid(dev_addr, &rec.mgid);
3183 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
3184 &rec.mgid, &rec);
3185 if (ret)
3186 return ret;
3187
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003188 ret = cma_set_qkey(id_priv, 0);
3189 if (ret)
3190 return ret;
3191
Roland Dreier3f446752008-08-04 11:02:14 -07003192 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Hefty5bc2b7b2013-05-29 10:09:33 -07003193 rec.qkey = cpu_to_be32(id_priv->qkey);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003194 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003195 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3196 rec.join_state = 1;
3197
3198 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3199 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3200 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3201 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3202 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3203
Yossi Etigin84adeee2009-04-01 13:55:32 -07003204 if (id_priv->id.ps == RDMA_PS_IPOIB)
3205 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
Dotan Barak2a22fb82012-08-30 09:09:55 +00003206 IB_SA_MCMEMBER_REC_RATE_SELECTOR |
3207 IB_SA_MCMEMBER_REC_MTU_SELECTOR |
3208 IB_SA_MCMEMBER_REC_MTU |
3209 IB_SA_MCMEMBER_REC_HOP_LIMIT;
Yossi Etigin84adeee2009-04-01 13:55:32 -07003210
Sean Heftyc8f6a362007-02-15 17:00:18 -08003211 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3212 id_priv->id.port_num, &rec,
3213 comp_mask, GFP_KERNEL,
3214 cma_ib_mc_handler, mc);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303215 return PTR_ERR_OR_ZERO(mc->multicast.ib);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003216}
3217
Eli Cohen3c86aa72010-10-13 21:26:51 +02003218static void iboe_mcast_work_handler(struct work_struct *work)
3219{
3220 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3221 struct cma_multicast *mc = mw->mc;
3222 struct ib_sa_multicast *m = mc->multicast.ib;
3223
3224 mc->multicast.ib->context = mc;
3225 cma_ib_mc_handler(0, m);
3226 kref_put(&mc->mcref, release_mc);
3227 kfree(mw);
3228}
3229
3230static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3231{
3232 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3233 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3234
3235 if (cma_any_addr(addr)) {
3236 memset(mgid, 0, sizeof *mgid);
3237 } else if (addr->sa_family == AF_INET6) {
3238 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3239 } else {
3240 mgid->raw[0] = 0xff;
3241 mgid->raw[1] = 0x0e;
3242 mgid->raw[2] = 0;
3243 mgid->raw[3] = 0;
3244 mgid->raw[4] = 0;
3245 mgid->raw[5] = 0;
3246 mgid->raw[6] = 0;
3247 mgid->raw[7] = 0;
3248 mgid->raw[8] = 0;
3249 mgid->raw[9] = 0;
3250 mgid->raw[10] = 0xff;
3251 mgid->raw[11] = 0xff;
3252 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3253 }
3254}
3255
3256static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3257 struct cma_multicast *mc)
3258{
3259 struct iboe_mcast_work *work;
3260 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3261 int err;
3262 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3263 struct net_device *ndev = NULL;
3264
3265 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3266 return -EINVAL;
3267
3268 work = kzalloc(sizeof *work, GFP_KERNEL);
3269 if (!work)
3270 return -ENOMEM;
3271
3272 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3273 if (!mc->multicast.ib) {
3274 err = -ENOMEM;
3275 goto out1;
3276 }
3277
3278 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3279
3280 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3281 if (id_priv->id.ps == RDMA_PS_UDP)
3282 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3283
3284 if (dev_addr->bound_dev_if)
3285 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3286 if (!ndev) {
3287 err = -ENODEV;
3288 goto out2;
3289 }
3290 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3291 mc->multicast.ib->rec.hop_limit = 1;
3292 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3293 dev_put(ndev);
3294 if (!mc->multicast.ib->rec.mtu) {
3295 err = -EINVAL;
3296 goto out2;
3297 }
Moni Shoua7b856272013-12-12 18:03:12 +02003298 rdma_ip2gid((struct sockaddr *)&id_priv->id.route.addr.src_addr,
3299 &mc->multicast.ib->rec.port_gid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003300 work->id = id_priv;
3301 work->mc = mc;
3302 INIT_WORK(&work->work, iboe_mcast_work_handler);
3303 kref_get(&mc->mcref);
3304 queue_work(cma_wq, &work->work);
3305
3306 return 0;
3307
3308out2:
3309 kfree(mc->multicast.ib);
3310out1:
3311 kfree(work);
3312 return err;
3313}
3314
Sean Heftyc8f6a362007-02-15 17:00:18 -08003315int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3316 void *context)
3317{
3318 struct rdma_id_private *id_priv;
3319 struct cma_multicast *mc;
3320 int ret;
3321
3322 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003323 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3324 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08003325 return -EINVAL;
3326
3327 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3328 if (!mc)
3329 return -ENOMEM;
3330
Sean Heftyef560862013-05-29 10:09:10 -07003331 memcpy(&mc->addr, addr, rdma_addr_size(addr));
Sean Heftyc8f6a362007-02-15 17:00:18 -08003332 mc->context = context;
3333 mc->id_priv = id_priv;
3334
3335 spin_lock(&id_priv->lock);
3336 list_add(&mc->list, &id_priv->mc_list);
3337 spin_unlock(&id_priv->lock);
3338
3339 switch (rdma_node_get_transport(id->device->node_type)) {
3340 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02003341 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3342 case IB_LINK_LAYER_INFINIBAND:
3343 ret = cma_join_ib_multicast(id_priv, mc);
3344 break;
3345 case IB_LINK_LAYER_ETHERNET:
3346 kref_init(&mc->mcref);
3347 ret = cma_iboe_join_multicast(id_priv, mc);
3348 break;
3349 default:
3350 ret = -EINVAL;
3351 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003352 break;
3353 default:
3354 ret = -ENOSYS;
3355 break;
3356 }
3357
3358 if (ret) {
3359 spin_lock_irq(&id_priv->lock);
3360 list_del(&mc->list);
3361 spin_unlock_irq(&id_priv->lock);
3362 kfree(mc);
3363 }
3364 return ret;
3365}
3366EXPORT_SYMBOL(rdma_join_multicast);
3367
3368void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3369{
3370 struct rdma_id_private *id_priv;
3371 struct cma_multicast *mc;
3372
3373 id_priv = container_of(id, struct rdma_id_private, id);
3374 spin_lock_irq(&id_priv->lock);
3375 list_for_each_entry(mc, &id_priv->mc_list, list) {
Sean Heftyef560862013-05-29 10:09:10 -07003376 if (!memcmp(&mc->addr, addr, rdma_addr_size(addr))) {
Sean Heftyc8f6a362007-02-15 17:00:18 -08003377 list_del(&mc->list);
3378 spin_unlock_irq(&id_priv->lock);
3379
3380 if (id->qp)
3381 ib_detach_mcast(id->qp,
3382 &mc->multicast.ib->rec.mgid,
Sean Hefty46ea5062011-12-06 21:15:18 +00003383 be16_to_cpu(mc->multicast.ib->rec.mlid));
Eli Cohen3c86aa72010-10-13 21:26:51 +02003384 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3385 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3386 case IB_LINK_LAYER_INFINIBAND:
3387 ib_sa_free_multicast(mc->multicast.ib);
3388 kfree(mc);
3389 break;
3390 case IB_LINK_LAYER_ETHERNET:
3391 kref_put(&mc->mcref, release_mc);
3392 break;
3393 default:
3394 break;
3395 }
3396 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003397 return;
3398 }
3399 }
3400 spin_unlock_irq(&id_priv->lock);
3401}
3402EXPORT_SYMBOL(rdma_leave_multicast);
3403
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003404static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3405{
3406 struct rdma_dev_addr *dev_addr;
3407 struct cma_ndev_work *work;
3408
3409 dev_addr = &id_priv->id.route.addr.dev_addr;
3410
Sean Hefty6266ed62009-11-19 12:55:22 -08003411 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003412 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3413 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3414 ndev->name, &id_priv->id);
3415 work = kzalloc(sizeof *work, GFP_KERNEL);
3416 if (!work)
3417 return -ENOMEM;
3418
3419 INIT_WORK(&work->work, cma_ndev_work_handler);
3420 work->id = id_priv;
3421 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3422 atomic_inc(&id_priv->refcount);
3423 queue_work(cma_wq, &work->work);
3424 }
3425
3426 return 0;
3427}
3428
3429static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003430 void *ptr)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003431{
Jiri Pirko351638e2013-05-28 01:30:21 +00003432 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003433 struct cma_device *cma_dev;
3434 struct rdma_id_private *id_priv;
3435 int ret = NOTIFY_DONE;
3436
3437 if (dev_net(ndev) != &init_net)
3438 return NOTIFY_DONE;
3439
3440 if (event != NETDEV_BONDING_FAILOVER)
3441 return NOTIFY_DONE;
3442
3443 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3444 return NOTIFY_DONE;
3445
3446 mutex_lock(&lock);
3447 list_for_each_entry(cma_dev, &dev_list, list)
3448 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3449 ret = cma_netdev_change(ndev, id_priv);
3450 if (ret)
3451 goto out;
3452 }
3453
3454out:
3455 mutex_unlock(&lock);
3456 return ret;
3457}
3458
3459static struct notifier_block cma_nb = {
3460 .notifier_call = cma_netdev_callback
3461};
3462
Sean Heftye51060f2006-06-17 20:37:29 -07003463static void cma_add_one(struct ib_device *device)
3464{
3465 struct cma_device *cma_dev;
3466 struct rdma_id_private *id_priv;
3467
3468 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3469 if (!cma_dev)
3470 return;
3471
3472 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07003473
3474 init_completion(&cma_dev->comp);
3475 atomic_set(&cma_dev->refcount, 1);
3476 INIT_LIST_HEAD(&cma_dev->id_list);
3477 ib_set_client_data(device, &cma_client, cma_dev);
3478
3479 mutex_lock(&lock);
3480 list_add_tail(&cma_dev->list, &dev_list);
3481 list_for_each_entry(id_priv, &listen_any_list, list)
3482 cma_listen_on_dev(id_priv, cma_dev);
3483 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07003484}
3485
3486static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3487{
Sean Heftya1b1b612006-11-30 16:33:14 -08003488 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003489 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003490 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07003491
3492 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003493 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3494 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07003495 return 0;
3496
3497 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003498 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07003499
3500 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003501 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07003502 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003503
Sean Heftya1b1b612006-11-30 16:33:14 -08003504 memset(&event, 0, sizeof event);
3505 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003506 ret = id_priv->id.event_handler(&id_priv->id, &event);
3507out:
3508 mutex_unlock(&id_priv->handler_mutex);
3509 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003510}
3511
3512static void cma_process_remove(struct cma_device *cma_dev)
3513{
Sean Heftye51060f2006-06-17 20:37:29 -07003514 struct rdma_id_private *id_priv;
3515 int ret;
3516
Sean Heftye51060f2006-06-17 20:37:29 -07003517 mutex_lock(&lock);
3518 while (!list_empty(&cma_dev->id_list)) {
3519 id_priv = list_entry(cma_dev->id_list.next,
3520 struct rdma_id_private, list);
3521
Sean Heftyd02d1f52007-10-09 11:12:34 -07003522 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07003523 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07003524 atomic_inc(&id_priv->refcount);
3525 mutex_unlock(&lock);
3526
Sean Heftyd02d1f52007-10-09 11:12:34 -07003527 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003528 cma_deref_id(id_priv);
3529 if (ret)
3530 rdma_destroy_id(&id_priv->id);
3531
3532 mutex_lock(&lock);
3533 }
3534 mutex_unlock(&lock);
3535
3536 cma_deref_dev(cma_dev);
3537 wait_for_completion(&cma_dev->comp);
3538}
3539
3540static void cma_remove_one(struct ib_device *device)
3541{
3542 struct cma_device *cma_dev;
3543
3544 cma_dev = ib_get_client_data(device, &cma_client);
3545 if (!cma_dev)
3546 return;
3547
3548 mutex_lock(&lock);
3549 list_del(&cma_dev->list);
3550 mutex_unlock(&lock);
3551
3552 cma_process_remove(cma_dev);
3553 kfree(cma_dev);
3554}
3555
Nir Muchtar753f6182011-01-03 15:33:53 +00003556static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3557{
3558 struct nlmsghdr *nlh;
3559 struct rdma_cm_id_stats *id_stats;
3560 struct rdma_id_private *id_priv;
3561 struct rdma_cm_id *id = NULL;
3562 struct cma_device *cma_dev;
3563 int i_dev = 0, i_id = 0;
3564
3565 /*
3566 * We export all of the IDs as a sequence of messages. Each
3567 * ID gets its own netlink message.
3568 */
3569 mutex_lock(&lock);
3570
3571 list_for_each_entry(cma_dev, &dev_list, list) {
3572 if (i_dev < cb->args[0]) {
3573 i_dev++;
3574 continue;
3575 }
3576
3577 i_id = 0;
3578 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3579 if (i_id < cb->args[1]) {
3580 i_id++;
3581 continue;
3582 }
3583
3584 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3585 sizeof *id_stats, RDMA_NL_RDMA_CM,
Tatyana Nikolova30dc5e62014-03-26 17:07:35 -05003586 RDMA_NL_RDMA_CM_ID_STATS,
3587 NLM_F_MULTI);
Nir Muchtar753f6182011-01-03 15:33:53 +00003588 if (!id_stats)
3589 goto out;
3590
3591 memset(id_stats, 0, sizeof *id_stats);
3592 id = &id_priv->id;
3593 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3594 id_stats->port_num = id->port_num;
3595 id_stats->bound_dev_if =
3596 id->route.addr.dev_addr.bound_dev_if;
3597
Sean Heftyce117ff2013-05-29 10:09:34 -07003598 if (ibnl_put_attr(skb, nlh,
3599 rdma_addr_size(cma_src_addr(id_priv)),
3600 cma_src_addr(id_priv),
3601 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR))
3602 goto out;
3603 if (ibnl_put_attr(skb, nlh,
3604 rdma_addr_size(cma_src_addr(id_priv)),
3605 cma_dst_addr(id_priv),
3606 RDMA_NL_RDMA_CM_ATTR_DST_ADDR))
3607 goto out;
Nir Muchtar753f6182011-01-03 15:33:53 +00003608
Nir Muchtar83e95022011-01-13 13:56:04 +00003609 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00003610 id_stats->port_space = id->ps;
3611 id_stats->cm_state = id_priv->state;
3612 id_stats->qp_num = id_priv->qp_num;
3613 id_stats->qp_type = id->qp_type;
3614
3615 i_id++;
3616 }
3617
3618 cb->args[1] = 0;
3619 i_dev++;
3620 }
3621
3622out:
3623 mutex_unlock(&lock);
3624 cb->args[0] = i_dev;
3625 cb->args[1] = i_id;
3626
3627 return skb->len;
3628}
3629
3630static const struct ibnl_client_cbs cma_cb_table[] = {
Gao feng809d5fc2012-10-04 20:15:49 +00003631 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats,
3632 .module = THIS_MODULE },
Nir Muchtar753f6182011-01-03 15:33:53 +00003633};
3634
Peter Huewe716abb12009-06-23 10:38:42 -07003635static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003636{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09003637 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003638
Sean Heftyc7f743a2007-02-01 12:23:37 -08003639 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07003640 if (!cma_wq)
3641 return -ENOMEM;
3642
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003643 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08003644 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003645 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003646
Sean Heftye51060f2006-06-17 20:37:29 -07003647 ret = ib_register_client(&cma_client);
3648 if (ret)
3649 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00003650
3651 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
3652 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
3653
Sean Heftye51060f2006-06-17 20:37:29 -07003654 return 0;
3655
3656err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003657 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003658 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003659 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003660 destroy_workqueue(cma_wq);
3661 return ret;
3662}
3663
Peter Huewe716abb12009-06-23 10:38:42 -07003664static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003665{
Nir Muchtar753f6182011-01-03 15:33:53 +00003666 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07003667 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003668 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003669 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003670 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003671 destroy_workqueue(cma_wq);
Sean Heftye51060f2006-06-17 20:37:29 -07003672 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08003673 idr_destroy(&udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003674 idr_destroy(&ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -07003675 idr_destroy(&ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07003676}
3677
3678module_init(cma_init);
3679module_exit(cma_cleanup);