blob: ca4c5dcd7133365f09dc60d83f623d4b5412ec4d [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>
Sean Heftye51060f2006-06-17 20:37:29 -070044
45#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080046#include <net/ipv6.h>
Sean Heftye51060f2006-06-17 20:37:29 -070047
48#include <rdma/rdma_cm.h>
49#include <rdma/rdma_cm_ib.h>
Nir Muchtar753f6182011-01-03 15:33:53 +000050#include <rdma/rdma_netlink.h>
Sean Heftye51060f2006-06-17 20:37:29 -070051#include <rdma/ib_cache.h>
52#include <rdma/ib_cm.h>
53#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050054#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070055
56MODULE_AUTHOR("Sean Hefty");
57MODULE_DESCRIPTION("Generic RDMA CM Agent");
58MODULE_LICENSE("Dual BSD/GPL");
59
60#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030061#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070062#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020063#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070064
65static void cma_add_one(struct ib_device *device);
66static void cma_remove_one(struct ib_device *device);
67
68static struct ib_client cma_client = {
69 .name = "cma",
70 .add = cma_add_one,
71 .remove = cma_remove_one
72};
73
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070074static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -080075static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -070076static LIST_HEAD(dev_list);
77static LIST_HEAD(listen_any_list);
78static DEFINE_MUTEX(lock);
79static struct workqueue_struct *cma_wq;
80static DEFINE_IDR(sdp_ps);
81static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -080082static DEFINE_IDR(udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -080083static DEFINE_IDR(ipoib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -070084
85struct cma_device {
86 struct list_head list;
87 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -070088 struct completion comp;
89 atomic_t refcount;
90 struct list_head id_list;
91};
92
Sean Heftye51060f2006-06-17 20:37:29 -070093struct rdma_bind_list {
94 struct idr *ps;
95 struct hlist_head owners;
96 unsigned short port;
97};
98
99/*
100 * Device removal can occur at anytime, so we need extra handling to
101 * serialize notifying the user of device removal with other callbacks.
102 * We do this by disabling removal notification while a callback is in process,
103 * and reporting it after the callback completes.
104 */
105struct rdma_id_private {
106 struct rdma_cm_id id;
107
108 struct rdma_bind_list *bind_list;
109 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700110 struct list_head list; /* listen_any_list or cma_device.list */
111 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700112 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800113 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700114
Sean Heftyd02d1f52007-10-09 11:12:34 -0700115 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700116 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700117 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700118 struct mutex qp_mutex;
119
Sean Heftye51060f2006-06-17 20:37:29 -0700120 struct completion comp;
121 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700122 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700123
124 int backlog;
125 int timeout_ms;
126 struct ib_sa_query *query;
127 int query_id;
128 union {
129 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500130 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700131 } cm_id;
132
133 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800134 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700135 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000136 pid_t owner;
Sean Heftye51060f2006-06-17 20:37:29 -0700137 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700138 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700139 u8 reuseaddr;
Sean Heftye51060f2006-06-17 20:37:29 -0700140};
141
Sean Heftyc8f6a362007-02-15 17:00:18 -0800142struct cma_multicast {
143 struct rdma_id_private *id_priv;
144 union {
145 struct ib_sa_multicast *ib;
146 } multicast;
147 struct list_head list;
148 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700149 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200150 struct kref mcref;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800151};
152
Sean Heftye51060f2006-06-17 20:37:29 -0700153struct cma_work {
154 struct work_struct work;
155 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700156 enum rdma_cm_state old_state;
157 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700158 struct rdma_cm_event event;
159};
160
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700161struct cma_ndev_work {
162 struct work_struct work;
163 struct rdma_id_private *id;
164 struct rdma_cm_event event;
165};
166
Eli Cohen3c86aa72010-10-13 21:26:51 +0200167struct iboe_mcast_work {
168 struct work_struct work;
169 struct rdma_id_private *id;
170 struct cma_multicast *mc;
171};
172
Sean Heftye51060f2006-06-17 20:37:29 -0700173union cma_ip_addr {
174 struct in6_addr ip6;
175 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000176 __be32 pad[3];
177 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700178 } ip4;
179};
180
181struct cma_hdr {
182 u8 cma_version;
183 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000184 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700185 union cma_ip_addr src_addr;
186 union cma_ip_addr dst_addr;
187};
188
189struct sdp_hh {
190 u8 bsdh[16];
191 u8 sdp_version; /* Major version: 7:4 */
192 u8 ip_version; /* IP version: 7:4 */
193 u8 sdp_specific1[10];
Al Viro1b90c132008-03-29 03:10:28 +0000194 __be16 port;
195 __be16 sdp_specific2;
Sean Heftye51060f2006-06-17 20:37:29 -0700196 union cma_ip_addr src_addr;
197 union cma_ip_addr dst_addr;
198};
199
200struct sdp_hah {
201 u8 bsdh[16];
202 u8 sdp_version;
203};
204
205#define CMA_VERSION 0x00
206#define SDP_MAJ_VERSION 0x2
207
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700208static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700209{
210 unsigned long flags;
211 int ret;
212
213 spin_lock_irqsave(&id_priv->lock, flags);
214 ret = (id_priv->state == comp);
215 spin_unlock_irqrestore(&id_priv->lock, flags);
216 return ret;
217}
218
219static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700220 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700221{
222 unsigned long flags;
223 int ret;
224
225 spin_lock_irqsave(&id_priv->lock, flags);
226 if ((ret = (id_priv->state == comp)))
227 id_priv->state = exch;
228 spin_unlock_irqrestore(&id_priv->lock, flags);
229 return ret;
230}
231
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700232static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
233 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700234{
235 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700236 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700237
238 spin_lock_irqsave(&id_priv->lock, flags);
239 old = id_priv->state;
240 id_priv->state = exch;
241 spin_unlock_irqrestore(&id_priv->lock, flags);
242 return old;
243}
244
245static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
246{
247 return hdr->ip_version >> 4;
248}
249
250static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
251{
252 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
253}
254
255static inline u8 sdp_get_majv(u8 sdp_version)
256{
257 return sdp_version >> 4;
258}
259
260static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
261{
262 return hh->ip_version >> 4;
263}
264
265static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
266{
267 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
268}
269
270static void cma_attach_to_dev(struct rdma_id_private *id_priv,
271 struct cma_device *cma_dev)
272{
273 atomic_inc(&cma_dev->refcount);
274 id_priv->cma_dev = cma_dev;
275 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200276 id_priv->id.route.addr.dev_addr.transport =
277 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700278 list_add_tail(&id_priv->list, &cma_dev->id_list);
279}
280
281static inline void cma_deref_dev(struct cma_device *cma_dev)
282{
283 if (atomic_dec_and_test(&cma_dev->refcount))
284 complete(&cma_dev->comp);
285}
286
Eli Cohen3c86aa72010-10-13 21:26:51 +0200287static inline void release_mc(struct kref *kref)
288{
289 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
290
291 kfree(mc->multicast.ib);
292 kfree(mc);
293}
294
Sean Heftya396d432011-02-23 09:05:39 -0800295static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700296{
Sean Heftya396d432011-02-23 09:05:39 -0800297 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700298 list_del(&id_priv->list);
299 cma_deref_dev(id_priv->cma_dev);
300 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800301 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700302}
303
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700304static int cma_set_qkey(struct rdma_id_private *id_priv)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800305{
306 struct ib_sa_mcmember_rec rec;
307 int ret = 0;
308
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700309 if (id_priv->qkey)
310 return 0;
311
312 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800313 case RDMA_PS_UDP:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700314 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800315 break;
316 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700317 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
318 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
319 id_priv->id.port_num, &rec.mgid,
320 &rec);
321 if (!ret)
322 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800323 break;
324 default:
325 break;
326 }
327 return ret;
328}
329
Eli Cohen3c86aa72010-10-13 21:26:51 +0200330static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
331{
332 int i;
333 int err;
334 struct ib_port_attr props;
335 union ib_gid tmp;
336
337 err = ib_query_port(device, port_num, &props);
338 if (err)
339 return 1;
340
341 for (i = 0; i < props.gid_tbl_len; ++i) {
342 err = ib_query_gid(device, port_num, i, &tmp);
343 if (err)
344 return 1;
345 if (!memcmp(&tmp, gid, sizeof tmp))
346 return 0;
347 }
348
349 return -EAGAIN;
350}
351
Tom Tucker07ebafb2006-08-03 16:02:42 -0500352static int cma_acquire_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700353{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800354 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700355 struct cma_device *cma_dev;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200356 union ib_gid gid, iboe_gid;
Sean Heftye51060f2006-06-17 20:37:29 -0700357 int ret = -ENODEV;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200358 u8 port;
359 enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
360 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
Sean Heftye51060f2006-06-17 20:37:29 -0700361
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000362 if (dev_ll != IB_LINK_LAYER_INFINIBAND &&
363 id_priv->id.ps == RDMA_PS_IPOIB)
364 return -EINVAL;
365
Sean Heftya396d432011-02-23 09:05:39 -0800366 mutex_lock(&lock);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200367 iboe_addr_get_sgid(dev_addr, &iboe_gid);
368 memcpy(&gid, dev_addr->src_dev_addr +
369 rdma_addr_gid_offset(dev_addr), sizeof gid);
Sean Heftye51060f2006-06-17 20:37:29 -0700370 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200371 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
372 if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
373 if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
374 rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
375 ret = find_gid_port(cma_dev->device, &iboe_gid, port);
376 else
377 ret = find_gid_port(cma_dev->device, &gid, port);
378
379 if (!ret) {
380 id_priv->id.port_num = port;
381 goto out;
382 } else if (ret == 1)
383 break;
384 }
Sean Heftye51060f2006-06-17 20:37:29 -0700385 }
386 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200387
388out:
389 if (!ret)
390 cma_attach_to_dev(id_priv, cma_dev);
391
Sean Heftya396d432011-02-23 09:05:39 -0800392 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700393 return ret;
394}
395
Sean Heftye51060f2006-06-17 20:37:29 -0700396static void cma_deref_id(struct rdma_id_private *id_priv)
397{
398 if (atomic_dec_and_test(&id_priv->refcount))
399 complete(&id_priv->comp);
400}
401
Or Gerlitzde910bd2008-07-14 23:48:53 -0700402static int cma_disable_callback(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700403 enum rdma_cm_state state)
Sean Hefty8aa08602007-05-07 11:49:00 -0700404{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700405 mutex_lock(&id_priv->handler_mutex);
406 if (id_priv->state != state) {
407 mutex_unlock(&id_priv->handler_mutex);
408 return -EINVAL;
409 }
410 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700411}
412
413struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000414 void *context, enum rdma_port_space ps,
415 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700416{
417 struct rdma_id_private *id_priv;
418
419 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
420 if (!id_priv)
421 return ERR_PTR(-ENOMEM);
422
Nir Muchtar83e95022011-01-13 13:56:04 +0000423 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700424 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700425 id_priv->id.context = context;
426 id_priv->id.event_handler = event_handler;
427 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000428 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700429 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700430 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700431 init_completion(&id_priv->comp);
432 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700433 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700434 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800435 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700436 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
437
438 return &id_priv->id;
439}
440EXPORT_SYMBOL(rdma_create_id);
441
Sean Heftyc8f6a362007-02-15 17:00:18 -0800442static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700443{
444 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800445 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700446
Sean Heftyc8f6a362007-02-15 17:00:18 -0800447 qp_attr.qp_state = IB_QPS_INIT;
448 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700449 if (ret)
450 return ret;
451
Sean Heftyc8f6a362007-02-15 17:00:18 -0800452 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
453 if (ret)
454 return ret;
455
456 qp_attr.qp_state = IB_QPS_RTR;
457 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
458 if (ret)
459 return ret;
460
461 qp_attr.qp_state = IB_QPS_RTS;
462 qp_attr.sq_psn = 0;
463 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
464
465 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700466}
467
Sean Heftyc8f6a362007-02-15 17:00:18 -0800468static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500469{
470 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800471 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500472
473 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800474 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
475 if (ret)
476 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500477
Sean Heftyc8f6a362007-02-15 17:00:18 -0800478 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500479}
480
Sean Heftye51060f2006-06-17 20:37:29 -0700481int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
482 struct ib_qp_init_attr *qp_init_attr)
483{
484 struct rdma_id_private *id_priv;
485 struct ib_qp *qp;
486 int ret;
487
488 id_priv = container_of(id, struct rdma_id_private, id);
489 if (id->device != pd->device)
490 return -EINVAL;
491
492 qp = ib_create_qp(pd, qp_init_attr);
493 if (IS_ERR(qp))
494 return PTR_ERR(qp);
495
Sean Heftyb26f9b92010-04-01 17:08:41 +0000496 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800497 ret = cma_init_ud_qp(id_priv, qp);
498 else
499 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700500 if (ret)
501 goto err;
502
503 id->qp = qp;
504 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700505 id_priv->srq = (qp->srq != NULL);
506 return 0;
507err:
508 ib_destroy_qp(qp);
509 return ret;
510}
511EXPORT_SYMBOL(rdma_create_qp);
512
513void rdma_destroy_qp(struct rdma_cm_id *id)
514{
Sean Heftyc5483382007-09-24 13:19:09 -0700515 struct rdma_id_private *id_priv;
516
517 id_priv = container_of(id, struct rdma_id_private, id);
518 mutex_lock(&id_priv->qp_mutex);
519 ib_destroy_qp(id_priv->id.qp);
520 id_priv->id.qp = NULL;
521 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700522}
523EXPORT_SYMBOL(rdma_destroy_qp);
524
Sean Hefty5851bb82008-01-04 10:47:12 -0800525static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
526 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700527{
528 struct ib_qp_attr qp_attr;
529 int qp_attr_mask, ret;
530
Sean Heftyc5483382007-09-24 13:19:09 -0700531 mutex_lock(&id_priv->qp_mutex);
532 if (!id_priv->id.qp) {
533 ret = 0;
534 goto out;
535 }
Sean Heftye51060f2006-06-17 20:37:29 -0700536
537 /* Need to update QP attributes from default values. */
538 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700539 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700540 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700541 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700542
Sean Heftyc5483382007-09-24 13:19:09 -0700543 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700544 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700545 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700546
547 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700548 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700549 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700550 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700551
Sean Hefty5851bb82008-01-04 10:47:12 -0800552 if (conn_param)
553 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700554 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
555out:
556 mutex_unlock(&id_priv->qp_mutex);
557 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700558}
559
Sean Hefty5851bb82008-01-04 10:47:12 -0800560static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
561 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700562{
563 struct ib_qp_attr qp_attr;
564 int qp_attr_mask, ret;
565
Sean Heftyc5483382007-09-24 13:19:09 -0700566 mutex_lock(&id_priv->qp_mutex);
567 if (!id_priv->id.qp) {
568 ret = 0;
569 goto out;
570 }
Sean Heftye51060f2006-06-17 20:37:29 -0700571
572 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700573 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700574 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700575 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700576
Sean Hefty5851bb82008-01-04 10:47:12 -0800577 if (conn_param)
578 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700579 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
580out:
581 mutex_unlock(&id_priv->qp_mutex);
582 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700583}
584
Sean Heftyc5483382007-09-24 13:19:09 -0700585static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700586{
587 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700588 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700589
Sean Heftyc5483382007-09-24 13:19:09 -0700590 mutex_lock(&id_priv->qp_mutex);
591 if (!id_priv->id.qp) {
592 ret = 0;
593 goto out;
594 }
Sean Heftye51060f2006-06-17 20:37:29 -0700595
596 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700597 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
598out:
599 mutex_unlock(&id_priv->qp_mutex);
600 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700601}
602
Sean Heftyc8f6a362007-02-15 17:00:18 -0800603static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
604 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
605{
606 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
607 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200608 u16 pkey;
609
610 if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
611 IB_LINK_LAYER_INFINIBAND)
612 pkey = ib_addr_get_pkey(dev_addr);
613 else
614 pkey = 0xffff;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800615
616 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200617 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800618 if (ret)
619 return ret;
620
621 qp_attr->port_num = id_priv->id.port_num;
622 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
623
Sean Heftyb26f9b92010-04-01 17:08:41 +0000624 if (id_priv->id.qp_type == IB_QPT_UD) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700625 ret = cma_set_qkey(id_priv);
626 if (ret)
627 return ret;
628
Sean Heftyc8f6a362007-02-15 17:00:18 -0800629 qp_attr->qkey = id_priv->qkey;
630 *qp_attr_mask |= IB_QP_QKEY;
631 } else {
632 qp_attr->qp_access_flags = 0;
633 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
634 }
635 return 0;
636}
637
Sean Heftye51060f2006-06-17 20:37:29 -0700638int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
639 int *qp_attr_mask)
640{
641 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800642 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700643
644 id_priv = container_of(id, struct rdma_id_private, id);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500645 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
646 case RDMA_TRANSPORT_IB:
Sean Heftyb26f9b92010-04-01 17:08:41 +0000647 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800648 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
649 else
650 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
651 qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700652 if (qp_attr->qp_state == IB_QPS_RTR)
653 qp_attr->rq_psn = id_priv->seq_num;
654 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500655 case RDMA_TRANSPORT_IWARP:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800656 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300657 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800658 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
659 } else
660 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
661 qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500662 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700663 default:
664 ret = -ENOSYS;
665 break;
666 }
667
668 return ret;
669}
670EXPORT_SYMBOL(rdma_init_qp_attr);
671
672static inline int cma_zero_addr(struct sockaddr *addr)
673{
674 struct in6_addr *ip6;
675
676 if (addr->sa_family == AF_INET)
Joe Perches6360a022007-12-16 13:47:33 -0800677 return ipv4_is_zeronet(
678 ((struct sockaddr_in *)addr)->sin_addr.s_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700679 else {
680 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
681 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
Eric Sesterhenn5fd571c2006-06-21 20:56:26 +0200682 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700683 }
684}
685
686static inline int cma_loopback_addr(struct sockaddr *addr)
687{
Aleksey Senin1f5175a2008-12-24 10:16:45 -0800688 if (addr->sa_family == AF_INET)
689 return ipv4_is_loopback(
690 ((struct sockaddr_in *) addr)->sin_addr.s_addr);
691 else
692 return ipv6_addr_loopback(
693 &((struct sockaddr_in6 *) addr)->sin6_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700694}
695
696static inline int cma_any_addr(struct sockaddr *addr)
697{
698 return cma_zero_addr(addr) || cma_loopback_addr(addr);
699}
700
Hefty, Sean43b752d2011-05-09 22:06:10 -0700701static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
702{
703 if (src->sa_family != dst->sa_family)
704 return -1;
705
706 switch (src->sa_family) {
707 case AF_INET:
708 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
709 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
710 default:
711 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
712 &((struct sockaddr_in6 *) dst)->sin6_addr);
713 }
714}
715
Sean Hefty628e5f62006-11-30 16:44:16 -0800716static inline __be16 cma_port(struct sockaddr *addr)
717{
718 if (addr->sa_family == AF_INET)
719 return ((struct sockaddr_in *) addr)->sin_port;
720 else
721 return ((struct sockaddr_in6 *) addr)->sin6_port;
722}
723
Sean Heftye51060f2006-06-17 20:37:29 -0700724static inline int cma_any_port(struct sockaddr *addr)
725{
Sean Hefty628e5f62006-11-30 16:44:16 -0800726 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700727}
728
729static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
Al Viro1b90c132008-03-29 03:10:28 +0000730 u8 *ip_ver, __be16 *port,
Sean Heftye51060f2006-06-17 20:37:29 -0700731 union cma_ip_addr **src, union cma_ip_addr **dst)
732{
733 switch (ps) {
734 case RDMA_PS_SDP:
735 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
736 SDP_MAJ_VERSION)
737 return -EINVAL;
738
739 *ip_ver = sdp_get_ip_ver(hdr);
740 *port = ((struct sdp_hh *) hdr)->port;
741 *src = &((struct sdp_hh *) hdr)->src_addr;
742 *dst = &((struct sdp_hh *) hdr)->dst_addr;
743 break;
744 default:
745 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
746 return -EINVAL;
747
748 *ip_ver = cma_get_ip_ver(hdr);
749 *port = ((struct cma_hdr *) hdr)->port;
750 *src = &((struct cma_hdr *) hdr)->src_addr;
751 *dst = &((struct cma_hdr *) hdr)->dst_addr;
752 break;
753 }
754
755 if (*ip_ver != 4 && *ip_ver != 6)
756 return -EINVAL;
757 return 0;
758}
759
760static void cma_save_net_info(struct rdma_addr *addr,
761 struct rdma_addr *listen_addr,
Al Viro1b90c132008-03-29 03:10:28 +0000762 u8 ip_ver, __be16 port,
Sean Heftye51060f2006-06-17 20:37:29 -0700763 union cma_ip_addr *src, union cma_ip_addr *dst)
764{
765 struct sockaddr_in *listen4, *ip4;
766 struct sockaddr_in6 *listen6, *ip6;
767
768 switch (ip_ver) {
769 case 4:
770 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
771 ip4 = (struct sockaddr_in *) &addr->src_addr;
772 ip4->sin_family = listen4->sin_family;
773 ip4->sin_addr.s_addr = dst->ip4.addr;
774 ip4->sin_port = listen4->sin_port;
775
776 ip4 = (struct sockaddr_in *) &addr->dst_addr;
777 ip4->sin_family = listen4->sin_family;
778 ip4->sin_addr.s_addr = src->ip4.addr;
779 ip4->sin_port = port;
780 break;
781 case 6:
782 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
783 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
784 ip6->sin6_family = listen6->sin6_family;
785 ip6->sin6_addr = dst->ip6;
786 ip6->sin6_port = listen6->sin6_port;
787
788 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
789 ip6->sin6_family = listen6->sin6_family;
790 ip6->sin6_addr = src->ip6;
791 ip6->sin6_port = port;
792 break;
793 default:
794 break;
795 }
796}
797
798static inline int cma_user_data_offset(enum rdma_port_space ps)
799{
800 switch (ps) {
801 case RDMA_PS_SDP:
802 return 0;
803 default:
804 return sizeof(struct cma_hdr);
805 }
806}
807
Sean Heftye51060f2006-06-17 20:37:29 -0700808static void cma_cancel_route(struct rdma_id_private *id_priv)
809{
Eli Cohen3c86aa72010-10-13 21:26:51 +0200810 switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
811 case IB_LINK_LAYER_INFINIBAND:
Sean Heftye51060f2006-06-17 20:37:29 -0700812 if (id_priv->query)
813 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
814 break;
815 default:
816 break;
817 }
818}
819
Sean Heftye51060f2006-06-17 20:37:29 -0700820static void cma_cancel_listens(struct rdma_id_private *id_priv)
821{
822 struct rdma_id_private *dev_id_priv;
823
Sean Heftyd02d1f52007-10-09 11:12:34 -0700824 /*
825 * Remove from listen_any_list to prevent added devices from spawning
826 * additional listen requests.
827 */
Sean Heftye51060f2006-06-17 20:37:29 -0700828 mutex_lock(&lock);
829 list_del(&id_priv->list);
830
831 while (!list_empty(&id_priv->listen_list)) {
832 dev_id_priv = list_entry(id_priv->listen_list.next,
833 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -0700834 /* sync with device removal to avoid duplicate destruction */
835 list_del_init(&dev_id_priv->list);
836 list_del(&dev_id_priv->listen_list);
837 mutex_unlock(&lock);
838
839 rdma_destroy_id(&dev_id_priv->id);
840 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700841 }
842 mutex_unlock(&lock);
843}
844
845static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700846 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -0700847{
848 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700849 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -0700850 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
851 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700852 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -0700853 cma_cancel_route(id_priv);
854 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700855 case RDMA_CM_LISTEN:
Roland Dreier3f446752008-08-04 11:02:14 -0700856 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
857 && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700858 cma_cancel_listens(id_priv);
859 break;
860 default:
861 break;
862 }
863}
864
865static void cma_release_port(struct rdma_id_private *id_priv)
866{
867 struct rdma_bind_list *bind_list = id_priv->bind_list;
868
869 if (!bind_list)
870 return;
871
872 mutex_lock(&lock);
873 hlist_del(&id_priv->node);
874 if (hlist_empty(&bind_list->owners)) {
875 idr_remove(bind_list->ps, bind_list->port);
876 kfree(bind_list);
877 }
878 mutex_unlock(&lock);
879}
880
Sean Heftyc8f6a362007-02-15 17:00:18 -0800881static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
882{
883 struct cma_multicast *mc;
884
885 while (!list_empty(&id_priv->mc_list)) {
886 mc = container_of(id_priv->mc_list.next,
887 struct cma_multicast, list);
888 list_del(&mc->list);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200889 switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
890 case IB_LINK_LAYER_INFINIBAND:
891 ib_sa_free_multicast(mc->multicast.ib);
892 kfree(mc);
893 break;
894 case IB_LINK_LAYER_ETHERNET:
895 kref_put(&mc->mcref, release_mc);
896 break;
897 default:
898 break;
899 }
Sean Heftyc8f6a362007-02-15 17:00:18 -0800900 }
901}
902
Sean Heftye51060f2006-06-17 20:37:29 -0700903void rdma_destroy_id(struct rdma_cm_id *id)
904{
905 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700906 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700907
908 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700909 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -0700910 cma_cancel_operation(id_priv, state);
911
Sean Heftya396d432011-02-23 09:05:39 -0800912 /*
913 * Wait for any active callback to finish. New callbacks will find
914 * the id_priv state set to destroying and abort.
915 */
916 mutex_lock(&id_priv->handler_mutex);
917 mutex_unlock(&id_priv->handler_mutex);
918
Sean Heftye51060f2006-06-17 20:37:29 -0700919 if (id_priv->cma_dev) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200920 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500921 case RDMA_TRANSPORT_IB:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +0000922 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -0700923 ib_destroy_cm_id(id_priv->cm_id.ib);
924 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500925 case RDMA_TRANSPORT_IWARP:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +0000926 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500927 iw_destroy_cm_id(id_priv->cm_id.iw);
928 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700929 default:
930 break;
931 }
Sean Heftyc8f6a362007-02-15 17:00:18 -0800932 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -0800933 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700934 }
935
936 cma_release_port(id_priv);
937 cma_deref_id(id_priv);
938 wait_for_completion(&id_priv->comp);
939
Sean Heftyd02d1f52007-10-09 11:12:34 -0700940 if (id_priv->internal_id)
941 cma_deref_id(id_priv->id.context);
942
Sean Heftye51060f2006-06-17 20:37:29 -0700943 kfree(id_priv->id.route.path_rec);
944 kfree(id_priv);
945}
946EXPORT_SYMBOL(rdma_destroy_id);
947
948static int cma_rep_recv(struct rdma_id_private *id_priv)
949{
950 int ret;
951
Sean Hefty5851bb82008-01-04 10:47:12 -0800952 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700953 if (ret)
954 goto reject;
955
Sean Hefty5851bb82008-01-04 10:47:12 -0800956 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700957 if (ret)
958 goto reject;
959
960 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
961 if (ret)
962 goto reject;
963
964 return 0;
965reject:
Sean Heftyc5483382007-09-24 13:19:09 -0700966 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700967 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
968 NULL, 0, NULL, 0);
969 return ret;
970}
971
972static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
973{
974 if (id_priv->id.ps == RDMA_PS_SDP &&
975 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
976 SDP_MAJ_VERSION)
977 return -EINVAL;
978
979 return 0;
980}
981
Sean Heftya1b1b612006-11-30 16:33:14 -0800982static void cma_set_rep_event_data(struct rdma_cm_event *event,
983 struct ib_cm_rep_event_param *rep_data,
984 void *private_data)
985{
986 event->param.conn.private_data = private_data;
987 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
988 event->param.conn.responder_resources = rep_data->responder_resources;
989 event->param.conn.initiator_depth = rep_data->initiator_depth;
990 event->param.conn.flow_control = rep_data->flow_control;
991 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
992 event->param.conn.srq = rep_data->srq;
993 event->param.conn.qp_num = rep_data->remote_qpn;
994}
995
Sean Heftye51060f2006-06-17 20:37:29 -0700996static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
997{
998 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -0800999 struct rdma_cm_event event;
1000 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001001
Amir Vadai38ca83a2008-07-22 14:14:23 -07001002 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001003 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001004 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001005 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -07001006 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001007
Sean Heftya1b1b612006-11-30 16:33:14 -08001008 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001009 switch (ib_event->event) {
1010 case IB_CM_REQ_ERROR:
1011 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001012 event.event = RDMA_CM_EVENT_UNREACHABLE;
1013 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001014 break;
1015 case IB_CM_REP_RECEIVED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001016 event.status = cma_verify_rep(id_priv, ib_event->private_data);
1017 if (event.status)
1018 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
Sean Heftye51060f2006-06-17 20:37:29 -07001019 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001020 event.status = cma_rep_recv(id_priv);
1021 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1022 RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001023 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08001024 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1025 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1026 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001027 break;
1028 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001029 case IB_CM_USER_ESTABLISHED:
1030 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001031 break;
1032 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001033 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001034 case IB_CM_DREQ_RECEIVED:
1035 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001036 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1037 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001038 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001039 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001040 break;
1041 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001042 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1043 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001044 case IB_CM_MRA_RECEIVED:
1045 /* ignore event */
1046 goto out;
1047 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001048 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001049 event.status = ib_event->param.rej_rcvd.reason;
1050 event.event = RDMA_CM_EVENT_REJECTED;
1051 event.param.conn.private_data = ib_event->private_data;
1052 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001053 break;
1054 default:
Roland Dreier468f2232008-07-14 23:48:47 -07001055 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001056 ib_event->event);
1057 goto out;
1058 }
1059
Sean Heftya1b1b612006-11-30 16:33:14 -08001060 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001061 if (ret) {
1062 /* Destroy the CM ID by returning a non-zero value. */
1063 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001064 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001065 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001066 rdma_destroy_id(&id_priv->id);
1067 return ret;
1068 }
1069out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001070 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001071 return ret;
1072}
1073
Sean Hefty628e5f62006-11-30 16:44:16 -08001074static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1075 struct ib_cm_event *ib_event)
Sean Heftye51060f2006-06-17 20:37:29 -07001076{
1077 struct rdma_id_private *id_priv;
1078 struct rdma_cm_id *id;
1079 struct rdma_route *rt;
1080 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001081 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07001082 u8 ip_ver;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001083 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001084
Sean Heftye51060f2006-06-17 20:37:29 -07001085 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1086 &ip_ver, &port, &src, &dst))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001087 return NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07001088
Krishna Kumar3f168d22006-09-29 12:09:51 -07001089 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001090 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001091 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001092 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001093
Sean Heftye51060f2006-06-17 20:37:29 -07001094 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1095 ip_ver, port, src, dst);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001096
1097 rt = &id->route;
1098 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1099 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1100 GFP_KERNEL);
1101 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001102 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001103
Sean Heftye51060f2006-06-17 20:37:29 -07001104 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1105 if (rt->num_paths == 2)
1106 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1107
Sean Hefty6f8372b2009-11-19 13:26:06 -08001108 if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) {
1109 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1110 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1111 ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey);
1112 } else {
1113 ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr,
1114 &rt->addr.dev_addr);
1115 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001116 goto err;
Sean Hefty6f8372b2009-11-19 13:26:06 -08001117 }
1118 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001119
1120 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001121 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001122 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001123
Krishna Kumar3f168d22006-09-29 12:09:51 -07001124err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001125 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001126 return NULL;
1127}
1128
Sean Hefty628e5f62006-11-30 16:44:16 -08001129static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1130 struct ib_cm_event *ib_event)
1131{
1132 struct rdma_id_private *id_priv;
1133 struct rdma_cm_id *id;
1134 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001135 __be16 port;
Sean Hefty628e5f62006-11-30 16:44:16 -08001136 u8 ip_ver;
1137 int ret;
1138
1139 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001140 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001141 if (IS_ERR(id))
1142 return NULL;
1143
1144
1145 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1146 &ip_ver, &port, &src, &dst))
1147 goto err;
1148
1149 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1150 ip_ver, port, src, dst);
1151
Sean Hefty6f8372b2009-11-19 13:26:06 -08001152 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
1153 ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
1154 &id->route.addr.dev_addr);
1155 if (ret)
1156 goto err;
1157 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001158
1159 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001160 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001161 return id_priv;
1162err:
1163 rdma_destroy_id(id);
1164 return NULL;
1165}
1166
Sean Heftya1b1b612006-11-30 16:33:14 -08001167static void cma_set_req_event_data(struct rdma_cm_event *event,
1168 struct ib_cm_req_event_param *req_data,
1169 void *private_data, int offset)
1170{
1171 event->param.conn.private_data = private_data + offset;
1172 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1173 event->param.conn.responder_resources = req_data->responder_resources;
1174 event->param.conn.initiator_depth = req_data->initiator_depth;
1175 event->param.conn.flow_control = req_data->flow_control;
1176 event->param.conn.retry_count = req_data->retry_count;
1177 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1178 event->param.conn.srq = req_data->srq;
1179 event->param.conn.qp_num = req_data->remote_qpn;
1180}
1181
Sean Heftye51060f2006-06-17 20:37:29 -07001182static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1183{
1184 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001185 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001186 int offset, ret;
1187
1188 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001189 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001190 return -ECONNABORTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001191
Sean Hefty628e5f62006-11-30 16:44:16 -08001192 memset(&event, 0, sizeof event);
1193 offset = cma_user_data_offset(listen_id->id.ps);
1194 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Sean Heftyb26f9b92010-04-01 17:08:41 +00001195 if (listen_id->id.qp_type == IB_QPT_UD) {
Sean Hefty628e5f62006-11-30 16:44:16 -08001196 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1197 event.param.ud.private_data = ib_event->private_data + offset;
1198 event.param.ud.private_data_len =
1199 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1200 } else {
1201 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1202 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1203 ib_event->private_data, offset);
1204 }
Sean Heftye51060f2006-06-17 20:37:29 -07001205 if (!conn_id) {
1206 ret = -ENOMEM;
1207 goto out;
1208 }
1209
Or Gerlitzde910bd2008-07-14 23:48:53 -07001210 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001211 ret = cma_acquire_dev(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301212 if (ret)
1213 goto release_conn_id;
Sean Heftye51060f2006-06-17 20:37:29 -07001214
1215 conn_id->cm_id.ib = cm_id;
1216 cm_id->context = conn_id;
1217 cm_id->cm_handler = cma_ib_handler;
1218
Sean Hefty25ae21a2011-02-23 08:11:32 -08001219 /*
1220 * Protect against the user destroying conn_id from another thread
1221 * until we're done accessing it.
1222 */
1223 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001224 ret = conn_id->id.event_handler(&conn_id->id, &event);
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001225 if (!ret) {
Sean Heftyead595a2008-02-13 14:33:53 -08001226 /*
1227 * Acquire mutex to prevent user executing rdma_destroy_id()
1228 * while we're accessing the cm_id.
1229 */
1230 mutex_lock(&lock);
Sean Heftyb26f9b92010-04-01 17:08:41 +00001231 if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyead595a2008-02-13 14:33:53 -08001232 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1233 mutex_unlock(&lock);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001234 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001235 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301236 goto out;
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001237 }
Sean Hefty25ae21a2011-02-23 08:11:32 -08001238 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301239
1240 /* Destroy the CM ID by returning a non-zero value. */
1241 conn_id->cm_id.ib = NULL;
1242
1243release_conn_id:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001244 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001245 mutex_unlock(&conn_id->handler_mutex);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301246 rdma_destroy_id(&conn_id->id);
1247
Sean Heftye51060f2006-06-17 20:37:29 -07001248out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001249 mutex_unlock(&listen_id->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001250 return ret;
1251}
1252
1253static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1254{
Sean Hefty628e5f62006-11-30 16:44:16 -08001255 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001256}
1257
1258static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1259 struct ib_cm_compare_data *compare)
1260{
1261 struct cma_hdr *cma_data, *cma_mask;
1262 struct sdp_hh *sdp_data, *sdp_mask;
Al Viro1b90c132008-03-29 03:10:28 +00001263 __be32 ip4_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001264 struct in6_addr ip6_addr;
1265
1266 memset(compare, 0, sizeof *compare);
1267 cma_data = (void *) compare->data;
1268 cma_mask = (void *) compare->mask;
1269 sdp_data = (void *) compare->data;
1270 sdp_mask = (void *) compare->mask;
1271
1272 switch (addr->sa_family) {
1273 case AF_INET:
1274 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1275 if (ps == RDMA_PS_SDP) {
1276 sdp_set_ip_ver(sdp_data, 4);
1277 sdp_set_ip_ver(sdp_mask, 0xF);
1278 sdp_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001279 sdp_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001280 } else {
1281 cma_set_ip_ver(cma_data, 4);
1282 cma_set_ip_ver(cma_mask, 0xF);
1283 cma_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001284 cma_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001285 }
1286 break;
1287 case AF_INET6:
1288 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1289 if (ps == RDMA_PS_SDP) {
1290 sdp_set_ip_ver(sdp_data, 6);
1291 sdp_set_ip_ver(sdp_mask, 0xF);
1292 sdp_data->dst_addr.ip6 = ip6_addr;
1293 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1294 sizeof sdp_mask->dst_addr.ip6);
1295 } else {
1296 cma_set_ip_ver(cma_data, 6);
1297 cma_set_ip_ver(cma_mask, 0xF);
1298 cma_data->dst_addr.ip6 = ip6_addr;
1299 memset(&cma_mask->dst_addr.ip6, 0xFF,
1300 sizeof cma_mask->dst_addr.ip6);
1301 }
1302 break;
1303 default:
1304 break;
1305 }
1306}
1307
Tom Tucker07ebafb2006-08-03 16:02:42 -05001308static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1309{
1310 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001311 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001312 struct sockaddr_in *sin;
1313 int ret = 0;
1314
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001315 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001316 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001317
Sean Heftybe65f082007-05-07 11:49:12 -07001318 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001319 switch (iw_event->event) {
1320 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001321 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001322 break;
1323 case IW_CM_EVENT_CONNECT_REPLY:
1324 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1325 *sin = iw_event->local_addr;
1326 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1327 *sin = iw_event->remote_addr;
Steve Wise881a0452006-12-15 16:50:17 -06001328 switch (iw_event->status) {
1329 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001330 event.event = RDMA_CM_EVENT_ESTABLISHED;
Steve Wise881a0452006-12-15 16:50:17 -06001331 break;
1332 case -ECONNRESET:
1333 case -ECONNREFUSED:
1334 event.event = RDMA_CM_EVENT_REJECTED;
1335 break;
1336 case -ETIMEDOUT:
1337 event.event = RDMA_CM_EVENT_UNREACHABLE;
1338 break;
1339 default:
1340 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1341 break;
1342 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001343 break;
1344 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001345 event.event = RDMA_CM_EVENT_ESTABLISHED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001346 break;
1347 default:
1348 BUG_ON(1);
1349 }
1350
Sean Heftya1b1b612006-11-30 16:33:14 -08001351 event.status = iw_event->status;
1352 event.param.conn.private_data = iw_event->private_data;
1353 event.param.conn.private_data_len = iw_event->private_data_len;
1354 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001355 if (ret) {
1356 /* Destroy the CM ID by returning a non-zero value. */
1357 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001358 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001359 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001360 rdma_destroy_id(&id_priv->id);
1361 return ret;
1362 }
1363
Or Gerlitzde910bd2008-07-14 23:48:53 -07001364 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001365 return ret;
1366}
1367
1368static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1369 struct iw_cm_event *iw_event)
1370{
1371 struct rdma_cm_id *new_cm_id;
1372 struct rdma_id_private *listen_id, *conn_id;
1373 struct sockaddr_in *sin;
1374 struct net_device *dev = NULL;
Sean Heftya1b1b612006-11-30 16:33:14 -08001375 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001376 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001377 struct ib_device_attr attr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001378
1379 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001380 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001381 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001382
1383 /* Create a new RDMA id for the new IW CM ID */
1384 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1385 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001386 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07001387 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001388 ret = -ENOMEM;
1389 goto out;
1390 }
1391 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001392 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001393 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001394
Denis V. Lunev1ab35272008-01-22 22:04:30 -08001395 dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001396 if (!dev) {
1397 ret = -EADDRNOTAVAIL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001398 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001399 rdma_destroy_id(new_cm_id);
1400 goto out;
1401 }
1402 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1403 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001404 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001405 rdma_destroy_id(new_cm_id);
1406 goto out;
1407 }
1408
1409 ret = cma_acquire_dev(conn_id);
1410 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001411 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001412 rdma_destroy_id(new_cm_id);
1413 goto out;
1414 }
1415
1416 conn_id->cm_id.iw = cm_id;
1417 cm_id->context = conn_id;
1418 cm_id->cm_handler = cma_iw_handler;
1419
1420 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1421 *sin = iw_event->local_addr;
1422 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1423 *sin = iw_event->remote_addr;
1424
Steve Wise8d8293c2007-10-29 11:34:05 -05001425 ret = ib_query_device(conn_id->id.device, &attr);
1426 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001427 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001428 rdma_destroy_id(new_cm_id);
1429 goto out;
1430 }
1431
Sean Heftya1b1b612006-11-30 16:33:14 -08001432 memset(&event, 0, sizeof event);
1433 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1434 event.param.conn.private_data = iw_event->private_data;
1435 event.param.conn.private_data_len = iw_event->private_data_len;
Steve Wise8d8293c2007-10-29 11:34:05 -05001436 event.param.conn.initiator_depth = attr.max_qp_init_rd_atom;
1437 event.param.conn.responder_resources = attr.max_qp_rd_atom;
Sean Hefty25ae21a2011-02-23 08:11:32 -08001438
1439 /*
1440 * Protect against the user destroying conn_id from another thread
1441 * until we're done accessing it.
1442 */
1443 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001444 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001445 if (ret) {
1446 /* User wants to destroy the CM ID */
1447 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001448 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001449 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001450 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001451 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001452 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001453 }
1454
Or Gerlitzde910bd2008-07-14 23:48:53 -07001455 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001456 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001457
Tom Tucker07ebafb2006-08-03 16:02:42 -05001458out:
1459 if (dev)
1460 dev_put(dev);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001461 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001462 return ret;
1463}
1464
Sean Heftye51060f2006-06-17 20:37:29 -07001465static int cma_ib_listen(struct rdma_id_private *id_priv)
1466{
1467 struct ib_cm_compare_data compare_data;
1468 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001469 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07001470 __be64 svc_id;
1471 int ret;
1472
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001473 id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
1474 if (IS_ERR(id))
1475 return PTR_ERR(id);
1476
1477 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07001478
Roland Dreier3f446752008-08-04 11:02:14 -07001479 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001480 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1481 if (cma_any_addr(addr))
1482 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1483 else {
1484 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1485 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1486 }
1487
1488 if (ret) {
1489 ib_destroy_cm_id(id_priv->cm_id.ib);
1490 id_priv->cm_id.ib = NULL;
1491 }
1492
1493 return ret;
1494}
1495
Tom Tucker07ebafb2006-08-03 16:02:42 -05001496static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1497{
1498 int ret;
1499 struct sockaddr_in *sin;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001500 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001501
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001502 id = iw_create_cm_id(id_priv->id.device,
1503 iw_conn_req_handler,
1504 id_priv);
1505 if (IS_ERR(id))
1506 return PTR_ERR(id);
1507
1508 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001509
1510 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1511 id_priv->cm_id.iw->local_addr = *sin;
1512
1513 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1514
1515 if (ret) {
1516 iw_destroy_cm_id(id_priv->cm_id.iw);
1517 id_priv->cm_id.iw = NULL;
1518 }
1519
1520 return ret;
1521}
1522
Sean Heftye51060f2006-06-17 20:37:29 -07001523static int cma_listen_handler(struct rdma_cm_id *id,
1524 struct rdma_cm_event *event)
1525{
1526 struct rdma_id_private *id_priv = id->context;
1527
1528 id->context = id_priv->id.context;
1529 id->event_handler = id_priv->id.event_handler;
1530 return id_priv->id.event_handler(id, event);
1531}
1532
1533static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1534 struct cma_device *cma_dev)
1535{
1536 struct rdma_id_private *dev_id_priv;
1537 struct rdma_cm_id *id;
1538 int ret;
1539
Sean Heftyb26f9b92010-04-01 17:08:41 +00001540 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
1541 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07001542 if (IS_ERR(id))
1543 return;
1544
1545 dev_id_priv = container_of(id, struct rdma_id_private, id);
1546
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001547 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftye51060f2006-06-17 20:37:29 -07001548 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
Roland Dreier3f446752008-08-04 11:02:14 -07001549 ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001550
1551 cma_attach_to_dev(dev_id_priv, cma_dev);
1552 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001553 atomic_inc(&id_priv->refcount);
1554 dev_id_priv->internal_id = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07001555
1556 ret = rdma_listen(id, id_priv->backlog);
1557 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07001558 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07001559 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07001560}
1561
1562static void cma_listen_on_all(struct rdma_id_private *id_priv)
1563{
1564 struct cma_device *cma_dev;
1565
1566 mutex_lock(&lock);
1567 list_add_tail(&id_priv->list, &listen_any_list);
1568 list_for_each_entry(cma_dev, &dev_list, list)
1569 cma_listen_on_dev(id_priv, cma_dev);
1570 mutex_unlock(&lock);
1571}
1572
Sean Heftya81c9942007-08-08 15:51:06 -07001573void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1574{
1575 struct rdma_id_private *id_priv;
1576
1577 id_priv = container_of(id, struct rdma_id_private, id);
1578 id_priv->tos = (u8) tos;
1579}
1580EXPORT_SYMBOL(rdma_set_service_type);
1581
Sean Heftye51060f2006-06-17 20:37:29 -07001582static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1583 void *context)
1584{
1585 struct cma_work *work = context;
1586 struct rdma_route *route;
1587
1588 route = &work->id->id.route;
1589
1590 if (!status) {
1591 route->num_paths = 1;
1592 *route->path_rec = *path_rec;
1593 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001594 work->old_state = RDMA_CM_ROUTE_QUERY;
1595 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001596 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07001597 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001598 }
1599
1600 queue_work(cma_wq, &work->work);
1601}
1602
1603static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1604 struct cma_work *work)
1605{
Sean Heftya81c9942007-08-08 15:51:06 -07001606 struct rdma_addr *addr = &id_priv->id.route.addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001607 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07001608 ib_sa_comp_mask comp_mask;
1609 struct sockaddr_in6 *sin6;
Sean Heftye51060f2006-06-17 20:37:29 -07001610
1611 memset(&path_rec, 0, sizeof path_rec);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001612 rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
1613 rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
Sean Heftya81c9942007-08-08 15:51:06 -07001614 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001615 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08001616 path_rec.reversible = 1;
Roland Dreier3f446752008-08-04 11:02:14 -07001617 path_rec.service_id = cma_get_service_id(id_priv->id.ps,
1618 (struct sockaddr *) &addr->dst_addr);
Sean Heftya81c9942007-08-08 15:51:06 -07001619
1620 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1621 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1622 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1623
Roland Dreier3f446752008-08-04 11:02:14 -07001624 if (addr->src_addr.ss_family == AF_INET) {
Sean Heftya81c9942007-08-08 15:51:06 -07001625 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1626 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1627 } else {
1628 sin6 = (struct sockaddr_in6 *) &addr->src_addr;
1629 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1630 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1631 }
Sean Heftye51060f2006-06-17 20:37:29 -07001632
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001633 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07001634 id_priv->id.port_num, &path_rec,
1635 comp_mask, timeout_ms,
1636 GFP_KERNEL, cma_query_handler,
1637 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001638
1639 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1640}
1641
David Howellsc4028952006-11-22 14:57:56 +00001642static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07001643{
David Howellsc4028952006-11-22 14:57:56 +00001644 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07001645 struct rdma_id_private *id_priv = work->id;
1646 int destroy = 0;
1647
Or Gerlitzde910bd2008-07-14 23:48:53 -07001648 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001649 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1650 goto out;
1651
1652 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001653 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001654 destroy = 1;
1655 }
1656out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001657 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001658 cma_deref_id(id_priv);
1659 if (destroy)
1660 rdma_destroy_id(&id_priv->id);
1661 kfree(work);
1662}
1663
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001664static void cma_ndev_work_handler(struct work_struct *_work)
1665{
1666 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1667 struct rdma_id_private *id_priv = work->id;
1668 int destroy = 0;
1669
1670 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001671 if (id_priv->state == RDMA_CM_DESTROYING ||
1672 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001673 goto out;
1674
1675 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001676 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001677 destroy = 1;
1678 }
1679
1680out:
1681 mutex_unlock(&id_priv->handler_mutex);
1682 cma_deref_id(id_priv);
1683 if (destroy)
1684 rdma_destroy_id(&id_priv->id);
1685 kfree(work);
1686}
1687
Sean Heftye51060f2006-06-17 20:37:29 -07001688static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1689{
1690 struct rdma_route *route = &id_priv->id.route;
1691 struct cma_work *work;
1692 int ret;
1693
1694 work = kzalloc(sizeof *work, GFP_KERNEL);
1695 if (!work)
1696 return -ENOMEM;
1697
1698 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001699 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001700 work->old_state = RDMA_CM_ROUTE_QUERY;
1701 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001702 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1703
1704 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1705 if (!route->path_rec) {
1706 ret = -ENOMEM;
1707 goto err1;
1708 }
1709
1710 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1711 if (ret)
1712 goto err2;
1713
1714 return 0;
1715err2:
1716 kfree(route->path_rec);
1717 route->path_rec = NULL;
1718err1:
1719 kfree(work);
1720 return ret;
1721}
1722
1723int rdma_set_ib_paths(struct rdma_cm_id *id,
1724 struct ib_sa_path_rec *path_rec, int num_paths)
1725{
1726 struct rdma_id_private *id_priv;
1727 int ret;
1728
1729 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001730 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1731 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07001732 return -EINVAL;
1733
Julia Lawall9893e742010-05-15 23:22:38 +02001734 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1735 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07001736 if (!id->route.path_rec) {
1737 ret = -ENOMEM;
1738 goto err;
1739 }
1740
Sean Heftyae2d9292010-03-25 19:12:36 +00001741 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07001742 return 0;
1743err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001744 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07001745 return ret;
1746}
1747EXPORT_SYMBOL(rdma_set_ib_paths);
1748
Tom Tucker07ebafb2006-08-03 16:02:42 -05001749static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1750{
1751 struct cma_work *work;
1752
1753 work = kzalloc(sizeof *work, GFP_KERNEL);
1754 if (!work)
1755 return -ENOMEM;
1756
1757 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001758 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001759 work->old_state = RDMA_CM_ROUTE_QUERY;
1760 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001761 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1762 queue_work(cma_wq, &work->work);
1763 return 0;
1764}
1765
Eli Cohen3c86aa72010-10-13 21:26:51 +02001766static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1767{
1768 struct rdma_route *route = &id_priv->id.route;
1769 struct rdma_addr *addr = &route->addr;
1770 struct cma_work *work;
1771 int ret;
1772 struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr;
1773 struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr;
1774 struct net_device *ndev = NULL;
Eli Cohenaf7bd462010-08-26 17:18:59 +03001775 u16 vid;
Eli Cohen3c86aa72010-10-13 21:26:51 +02001776
1777 if (src_addr->sin_family != dst_addr->sin_family)
1778 return -EINVAL;
1779
1780 work = kzalloc(sizeof *work, GFP_KERNEL);
1781 if (!work)
1782 return -ENOMEM;
1783
1784 work->id = id_priv;
1785 INIT_WORK(&work->work, cma_work_handler);
1786
1787 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1788 if (!route->path_rec) {
1789 ret = -ENOMEM;
1790 goto err1;
1791 }
1792
1793 route->num_paths = 1;
1794
Eli Cohen3c86aa72010-10-13 21:26:51 +02001795 if (addr->dev_addr.bound_dev_if)
1796 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1797 if (!ndev) {
1798 ret = -ENODEV;
1799 goto err2;
1800 }
1801
Eli Cohenaf7bd462010-08-26 17:18:59 +03001802 vid = rdma_vlan_dev_vlan_id(ndev);
1803
1804 iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1805 iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1806
1807 route->path_rec->hop_limit = 1;
1808 route->path_rec->reversible = 1;
1809 route->path_rec->pkey = cpu_to_be16(0xffff);
1810 route->path_rec->mtu_selector = IB_SA_EQ;
1811 route->path_rec->sl = id_priv->tos >> 5;
1812
Eli Cohen3c86aa72010-10-13 21:26:51 +02001813 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1814 route->path_rec->rate_selector = IB_SA_EQ;
1815 route->path_rec->rate = iboe_get_rate(ndev);
1816 dev_put(ndev);
1817 route->path_rec->packet_life_time_selector = IB_SA_EQ;
1818 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1819 if (!route->path_rec->mtu) {
1820 ret = -EINVAL;
1821 goto err2;
1822 }
1823
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001824 work->old_state = RDMA_CM_ROUTE_QUERY;
1825 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02001826 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1827 work->event.status = 0;
1828
1829 queue_work(cma_wq, &work->work);
1830
1831 return 0;
1832
1833err2:
1834 kfree(route->path_rec);
1835 route->path_rec = NULL;
1836err1:
1837 kfree(work);
1838 return ret;
1839}
1840
Sean Heftye51060f2006-06-17 20:37:29 -07001841int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1842{
1843 struct rdma_id_private *id_priv;
1844 int ret;
1845
1846 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001847 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07001848 return -EINVAL;
1849
1850 atomic_inc(&id_priv->refcount);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001851 switch (rdma_node_get_transport(id->device->node_type)) {
1852 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02001853 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1854 case IB_LINK_LAYER_INFINIBAND:
1855 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1856 break;
1857 case IB_LINK_LAYER_ETHERNET:
1858 ret = cma_resolve_iboe_route(id_priv);
1859 break;
1860 default:
1861 ret = -ENOSYS;
1862 }
Sean Heftye51060f2006-06-17 20:37:29 -07001863 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001864 case RDMA_TRANSPORT_IWARP:
1865 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1866 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001867 default:
1868 ret = -ENOSYS;
1869 break;
1870 }
1871 if (ret)
1872 goto err;
1873
1874 return 0;
1875err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001876 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07001877 cma_deref_id(id_priv);
1878 return ret;
1879}
1880EXPORT_SYMBOL(rdma_resolve_route);
1881
1882static int cma_bind_loopback(struct rdma_id_private *id_priv)
1883{
1884 struct cma_device *cma_dev;
1885 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001886 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001887 u16 pkey;
1888 int ret;
1889 u8 p;
1890
1891 mutex_lock(&lock);
Krishna Kumare82153b2006-10-16 10:09:01 +05301892 if (list_empty(&dev_list)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001893 ret = -ENODEV;
1894 goto out;
1895 }
Krishna Kumare82153b2006-10-16 10:09:01 +05301896 list_for_each_entry(cma_dev, &dev_list, list)
1897 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1898 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1899 port_attr.state == IB_PORT_ACTIVE)
1900 goto port_found;
1901
1902 p = 1;
1903 cma_dev = list_entry(dev_list.next, struct cma_device, list);
Sean Heftye51060f2006-06-17 20:37:29 -07001904
1905port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001906 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001907 if (ret)
1908 goto out;
1909
1910 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1911 if (ret)
1912 goto out;
1913
Sean Hefty6f8372b2009-11-19 13:26:06 -08001914 id_priv->id.route.addr.dev_addr.dev_type =
Eli Cohen3c86aa72010-10-13 21:26:51 +02001915 (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08001916 ARPHRD_INFINIBAND : ARPHRD_ETHER;
1917
1918 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001919 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1920 id_priv->id.port_num = p;
1921 cma_attach_to_dev(id_priv, cma_dev);
1922out:
1923 mutex_unlock(&lock);
1924 return ret;
1925}
1926
1927static void addr_handler(int status, struct sockaddr *src_addr,
1928 struct rdma_dev_addr *dev_addr, void *context)
1929{
1930 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001931 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001932
Sean Heftya1b1b612006-11-30 16:33:14 -08001933 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001934 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001935 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
1936 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07001937 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07001938
1939 if (!status && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001940 status = cma_acquire_dev(id_priv);
1941
1942 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001943 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1944 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07001945 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001946 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1947 event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001948 } else {
Sean Heftye51060f2006-06-17 20:37:29 -07001949 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1950 ip_addr_size(src_addr));
Sean Heftya1b1b612006-11-30 16:33:14 -08001951 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001952 }
1953
Sean Heftya1b1b612006-11-30 16:33:14 -08001954 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001955 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001956 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001957 cma_deref_id(id_priv);
1958 rdma_destroy_id(&id_priv->id);
1959 return;
1960 }
1961out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001962 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001963 cma_deref_id(id_priv);
1964}
1965
1966static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1967{
1968 struct cma_work *work;
Sean Hefty6f8372b2009-11-19 13:26:06 -08001969 struct sockaddr *src, *dst;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001970 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001971 int ret;
1972
1973 work = kzalloc(sizeof *work, GFP_KERNEL);
1974 if (!work)
1975 return -ENOMEM;
1976
1977 if (!id_priv->cma_dev) {
1978 ret = cma_bind_loopback(id_priv);
1979 if (ret)
1980 goto err;
1981 }
1982
Sean Hefty6f8372b2009-11-19 13:26:06 -08001983 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1984 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001985
Sean Hefty6f8372b2009-11-19 13:26:06 -08001986 src = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
1987 if (cma_zero_addr(src)) {
1988 dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
1989 if ((src->sa_family = dst->sa_family) == AF_INET) {
1990 ((struct sockaddr_in *) src)->sin_addr.s_addr =
1991 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
1992 } else {
1993 ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr,
1994 &((struct sockaddr_in6 *) dst)->sin6_addr);
1995 }
Sean Heftye51060f2006-06-17 20:37:29 -07001996 }
1997
1998 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001999 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002000 work->old_state = RDMA_CM_ADDR_QUERY;
2001 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002002 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2003 queue_work(cma_wq, &work->work);
2004 return 0;
2005err:
2006 kfree(work);
2007 return ret;
2008}
2009
2010static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2011 struct sockaddr *dst_addr)
2012{
Sean Heftyd14714d2009-11-19 16:46:25 -08002013 if (!src_addr || !src_addr->sa_family) {
2014 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2015 if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) {
2016 ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2017 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
2018 }
2019 }
2020 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002021}
2022
2023int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2024 struct sockaddr *dst_addr, int timeout_ms)
2025{
2026 struct rdma_id_private *id_priv;
2027 int ret;
2028
2029 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002030 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002031 ret = cma_bind_addr(id, src_addr, dst_addr);
2032 if (ret)
2033 return ret;
2034 }
2035
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002036 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002037 return -EINVAL;
2038
2039 atomic_inc(&id_priv->refcount);
2040 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
2041 if (cma_any_addr(dst_addr))
2042 ret = cma_resolve_loopback(id_priv);
2043 else
Roland Dreier3f446752008-08-04 11:02:14 -07002044 ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
Sean Hefty7a118df2006-10-31 11:12:59 -08002045 dst_addr, &id->route.addr.dev_addr,
Sean Heftye51060f2006-06-17 20:37:29 -07002046 timeout_ms, addr_handler, id_priv);
2047 if (ret)
2048 goto err;
2049
2050 return 0;
2051err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002052 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002053 cma_deref_id(id_priv);
2054 return ret;
2055}
2056EXPORT_SYMBOL(rdma_resolve_addr);
2057
Hefty, Seana9bb7912011-05-09 22:06:10 -07002058int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2059{
2060 struct rdma_id_private *id_priv;
2061 unsigned long flags;
2062 int ret;
2063
2064 id_priv = container_of(id, struct rdma_id_private, id);
2065 spin_lock_irqsave(&id_priv->lock, flags);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002066 if (id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002067 id_priv->reuseaddr = reuse;
2068 ret = 0;
2069 } else {
2070 ret = -EINVAL;
2071 }
2072 spin_unlock_irqrestore(&id_priv->lock, flags);
2073 return ret;
2074}
2075EXPORT_SYMBOL(rdma_set_reuseaddr);
2076
Sean Heftye51060f2006-06-17 20:37:29 -07002077static void cma_bind_port(struct rdma_bind_list *bind_list,
2078 struct rdma_id_private *id_priv)
2079{
2080 struct sockaddr_in *sin;
2081
2082 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2083 sin->sin_port = htons(bind_list->port);
2084 id_priv->bind_list = bind_list;
2085 hlist_add_head(&id_priv->node, &bind_list->owners);
2086}
2087
2088static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2089 unsigned short snum)
2090{
2091 struct rdma_bind_list *bind_list;
Sean Heftyaedec082007-01-29 16:41:23 -08002092 int port, ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002093
Sean Heftycb164b82007-03-05 12:50:17 -08002094 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002095 if (!bind_list)
2096 return -ENOMEM;
2097
Sean Heftye51060f2006-06-17 20:37:29 -07002098 do {
Sean Heftyaedec082007-01-29 16:41:23 -08002099 ret = idr_get_new_above(ps, bind_list, snum, &port);
Sean Heftye51060f2006-06-17 20:37:29 -07002100 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
2101
2102 if (ret)
Sean Heftyaedec082007-01-29 16:41:23 -08002103 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002104
Sean Heftyaedec082007-01-29 16:41:23 -08002105 if (port != snum) {
Sean Heftye51060f2006-06-17 20:37:29 -07002106 ret = -EADDRNOTAVAIL;
Sean Heftyaedec082007-01-29 16:41:23 -08002107 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002108 }
2109
2110 bind_list->ps = ps;
2111 bind_list->port = (unsigned short) port;
2112 cma_bind_port(bind_list, id_priv);
2113 return 0;
Sean Heftyaedec082007-01-29 16:41:23 -08002114err2:
2115 idr_remove(ps, port);
2116err1:
2117 kfree(bind_list);
2118 return ret;
2119}
2120
2121static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2122{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002123 static unsigned int last_used_port;
2124 int low, high, remaining;
2125 unsigned int rover;
Sean Heftyaedec082007-01-29 16:41:23 -08002126
Stephen Hemminger227b60f2007-10-10 17:30:46 -07002127 inet_get_local_port_range(&low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002128 remaining = (high - low) + 1;
2129 rover = net_random() % remaining + low;
2130retry:
2131 if (last_used_port != rover &&
2132 !idr_find(ps, (unsigned short) rover)) {
2133 int ret = cma_alloc_port(ps, id_priv, rover);
2134 /*
2135 * Remember previously used port number in order to avoid
2136 * re-using same port immediately after it is closed.
2137 */
2138 if (!ret)
2139 last_used_port = rover;
2140 if (ret != -EADDRNOTAVAIL)
2141 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002142 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002143 if (--remaining) {
2144 rover++;
2145 if ((rover < low) || (rover > high))
2146 rover = low;
2147 goto retry;
2148 }
2149 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002150}
2151
Hefty, Seana9bb7912011-05-09 22:06:10 -07002152/*
2153 * Check that the requested port is available. This is called when trying to
2154 * bind to a specific port, or when trying to listen on a bound port. In
2155 * the latter case, the provided id_priv may already be on the bind_list, but
2156 * we still need to check that it's okay to start listening.
2157 */
2158static int cma_check_port(struct rdma_bind_list *bind_list,
2159 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002160{
2161 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002162 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002163 struct hlist_node *node;
Sean Heftye51060f2006-06-17 20:37:29 -07002164
Hefty, Sean43b752d2011-05-09 22:06:10 -07002165 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002166 if (cma_any_addr(addr) && !reuseaddr)
2167 return -EADDRNOTAVAIL;
2168
2169 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
2170 if (id_priv == cur_id)
2171 continue;
2172
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002173 if ((cur_id->state == RDMA_CM_LISTEN) ||
Hefty, Seana9bb7912011-05-09 22:06:10 -07002174 !reuseaddr || !cur_id->reuseaddr) {
2175 cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr;
2176 if (cma_any_addr(cur_addr))
2177 return -EADDRNOTAVAIL;
2178
2179 if (!cma_addr_cmp(addr, cur_addr))
2180 return -EADDRINUSE;
2181 }
2182 }
2183 return 0;
2184}
2185
2186static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2187{
2188 struct rdma_bind_list *bind_list;
2189 unsigned short snum;
2190 int ret;
2191
2192 snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07002193 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2194 return -EACCES;
2195
2196 bind_list = idr_find(ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002197 if (!bind_list) {
2198 ret = cma_alloc_port(ps, id_priv, snum);
2199 } else {
2200 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2201 if (!ret)
2202 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002203 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07002204 return ret;
2205}
Sean Heftye51060f2006-06-17 20:37:29 -07002206
Hefty, Seana9bb7912011-05-09 22:06:10 -07002207static int cma_bind_listen(struct rdma_id_private *id_priv)
2208{
2209 struct rdma_bind_list *bind_list = id_priv->bind_list;
2210 int ret = 0;
2211
2212 mutex_lock(&lock);
2213 if (bind_list->owners.first->next)
2214 ret = cma_check_port(bind_list, id_priv, 0);
2215 mutex_unlock(&lock);
2216 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002217}
2218
2219static int cma_get_port(struct rdma_id_private *id_priv)
2220{
2221 struct idr *ps;
2222 int ret;
2223
2224 switch (id_priv->id.ps) {
2225 case RDMA_PS_SDP:
2226 ps = &sdp_ps;
2227 break;
2228 case RDMA_PS_TCP:
2229 ps = &tcp_ps;
2230 break;
Sean Hefty628e5f62006-11-30 16:44:16 -08002231 case RDMA_PS_UDP:
2232 ps = &udp_ps;
2233 break;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002234 case RDMA_PS_IPOIB:
2235 ps = &ipoib_ps;
2236 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002237 default:
2238 return -EPROTONOSUPPORT;
2239 }
2240
2241 mutex_lock(&lock);
Roland Dreier3f446752008-08-04 11:02:14 -07002242 if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
Sean Heftyaedec082007-01-29 16:41:23 -08002243 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002244 else
2245 ret = cma_use_port(ps, id_priv);
2246 mutex_unlock(&lock);
2247
2248 return ret;
2249}
2250
Sean Heftyd14714d2009-11-19 16:46:25 -08002251static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2252 struct sockaddr *addr)
2253{
Robert P. J. Dayfd4582a2010-01-06 13:16:30 -08002254#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Sean Heftyd14714d2009-11-19 16:46:25 -08002255 struct sockaddr_in6 *sin6;
2256
2257 if (addr->sa_family != AF_INET6)
2258 return 0;
2259
2260 sin6 = (struct sockaddr_in6 *) addr;
2261 if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2262 !sin6->sin6_scope_id)
2263 return -EINVAL;
2264
2265 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2266#endif
2267 return 0;
2268}
2269
Hefty, Seana9bb7912011-05-09 22:06:10 -07002270int rdma_listen(struct rdma_cm_id *id, int backlog)
2271{
2272 struct rdma_id_private *id_priv;
2273 int ret;
2274
2275 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002276 if (id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002277 ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
2278 ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
2279 if (ret)
2280 return ret;
2281 }
2282
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002283 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07002284 return -EINVAL;
2285
2286 if (id_priv->reuseaddr) {
2287 ret = cma_bind_listen(id_priv);
2288 if (ret)
2289 goto err;
2290 }
2291
2292 id_priv->backlog = backlog;
2293 if (id->device) {
2294 switch (rdma_node_get_transport(id->device->node_type)) {
2295 case RDMA_TRANSPORT_IB:
2296 ret = cma_ib_listen(id_priv);
2297 if (ret)
2298 goto err;
2299 break;
2300 case RDMA_TRANSPORT_IWARP:
2301 ret = cma_iw_listen(id_priv, backlog);
2302 if (ret)
2303 goto err;
2304 break;
2305 default:
2306 ret = -ENOSYS;
2307 goto err;
2308 }
2309 } else
2310 cma_listen_on_all(id_priv);
2311
2312 return 0;
2313err:
2314 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002315 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002316 return ret;
2317}
2318EXPORT_SYMBOL(rdma_listen);
2319
Sean Heftye51060f2006-06-17 20:37:29 -07002320int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2321{
2322 struct rdma_id_private *id_priv;
2323 int ret;
2324
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002325 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Sean Heftye51060f2006-06-17 20:37:29 -07002326 return -EAFNOSUPPORT;
2327
2328 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002329 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002330 return -EINVAL;
2331
Sean Heftyd14714d2009-11-19 16:46:25 -08002332 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2333 if (ret)
2334 goto err1;
2335
Sean Hefty8523c042010-02-08 16:41:15 -08002336 if (!cma_any_addr(addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002337 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002338 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002339 goto err1;
2340
Krishna Kumar255d0c12006-10-24 13:22:28 -07002341 ret = cma_acquire_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002342 if (ret)
2343 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002344 }
2345
2346 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2347 ret = cma_get_port(id_priv);
2348 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002349 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002350
2351 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07002352err2:
Sean Heftya396d432011-02-23 09:05:39 -08002353 if (id_priv->cma_dev)
2354 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002355err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002356 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07002357 return ret;
2358}
2359EXPORT_SYMBOL(rdma_bind_addr);
2360
2361static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2362 struct rdma_route *route)
2363{
Sean Heftye51060f2006-06-17 20:37:29 -07002364 struct cma_hdr *cma_hdr;
2365 struct sdp_hh *sdp_hdr;
2366
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002367 if (route->addr.src_addr.ss_family == AF_INET) {
2368 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07002369
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002370 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2371 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2372
2373 switch (ps) {
2374 case RDMA_PS_SDP:
2375 sdp_hdr = hdr;
2376 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2377 return -EINVAL;
2378 sdp_set_ip_ver(sdp_hdr, 4);
2379 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2380 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2381 sdp_hdr->port = src4->sin_port;
2382 break;
2383 default:
2384 cma_hdr = hdr;
2385 cma_hdr->cma_version = CMA_VERSION;
2386 cma_set_ip_ver(cma_hdr, 4);
2387 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2388 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2389 cma_hdr->port = src4->sin_port;
2390 break;
2391 }
2392 } else {
2393 struct sockaddr_in6 *src6, *dst6;
2394
2395 src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
2396 dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
2397
2398 switch (ps) {
2399 case RDMA_PS_SDP:
2400 sdp_hdr = hdr;
2401 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2402 return -EINVAL;
2403 sdp_set_ip_ver(sdp_hdr, 6);
2404 sdp_hdr->src_addr.ip6 = src6->sin6_addr;
2405 sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
2406 sdp_hdr->port = src6->sin6_port;
2407 break;
2408 default:
2409 cma_hdr = hdr;
2410 cma_hdr->cma_version = CMA_VERSION;
2411 cma_set_ip_ver(cma_hdr, 6);
2412 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2413 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2414 cma_hdr->port = src6->sin6_port;
2415 break;
2416 }
Sean Heftye51060f2006-06-17 20:37:29 -07002417 }
2418 return 0;
2419}
2420
Sean Hefty628e5f62006-11-30 16:44:16 -08002421static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2422 struct ib_cm_event *ib_event)
2423{
2424 struct rdma_id_private *id_priv = cm_id->context;
2425 struct rdma_cm_event event;
2426 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2427 int ret = 0;
2428
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002429 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07002430 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08002431
Sean Hefty8aa08602007-05-07 11:49:00 -07002432 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08002433 switch (ib_event->event) {
2434 case IB_CM_SIDR_REQ_ERROR:
2435 event.event = RDMA_CM_EVENT_UNREACHABLE;
2436 event.status = -ETIMEDOUT;
2437 break;
2438 case IB_CM_SIDR_REP_RECEIVED:
2439 event.param.ud.private_data = ib_event->private_data;
2440 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2441 if (rep->status != IB_SIDR_SUCCESS) {
2442 event.event = RDMA_CM_EVENT_UNREACHABLE;
2443 event.status = ib_event->param.sidr_rep_rcvd.status;
2444 break;
2445 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002446 ret = cma_set_qkey(id_priv);
2447 if (ret) {
2448 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2449 event.status = -EINVAL;
2450 break;
2451 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08002452 if (id_priv->qkey != rep->qkey) {
Sean Hefty628e5f62006-11-30 16:44:16 -08002453 event.event = RDMA_CM_EVENT_UNREACHABLE;
2454 event.status = -EINVAL;
2455 break;
2456 }
2457 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2458 id_priv->id.route.path_rec,
2459 &event.param.ud.ah_attr);
2460 event.param.ud.qp_num = rep->qpn;
2461 event.param.ud.qkey = rep->qkey;
2462 event.event = RDMA_CM_EVENT_ESTABLISHED;
2463 event.status = 0;
2464 break;
2465 default:
Roland Dreier468f2232008-07-14 23:48:47 -07002466 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08002467 ib_event->event);
2468 goto out;
2469 }
2470
2471 ret = id_priv->id.event_handler(&id_priv->id, &event);
2472 if (ret) {
2473 /* Destroy the CM ID by returning a non-zero value. */
2474 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002475 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002476 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002477 rdma_destroy_id(&id_priv->id);
2478 return ret;
2479 }
2480out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002481 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002482 return ret;
2483}
2484
2485static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2486 struct rdma_conn_param *conn_param)
2487{
2488 struct ib_cm_sidr_req_param req;
2489 struct rdma_route *route;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002490 struct ib_cm_id *id;
Sean Hefty628e5f62006-11-30 16:44:16 -08002491 int ret;
2492
2493 req.private_data_len = sizeof(struct cma_hdr) +
2494 conn_param->private_data_len;
2495 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2496 if (!req.private_data)
2497 return -ENOMEM;
2498
2499 if (conn_param->private_data && conn_param->private_data_len)
2500 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2501 conn_param->private_data, conn_param->private_data_len);
2502
2503 route = &id_priv->id.route;
2504 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2505 if (ret)
2506 goto out;
2507
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002508 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
2509 id_priv);
2510 if (IS_ERR(id)) {
2511 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08002512 goto out;
2513 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002514 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08002515
2516 req.path = route->path_rec;
2517 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002518 (struct sockaddr *) &route->addr.dst_addr);
Sean Hefty628e5f62006-11-30 16:44:16 -08002519 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2520 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2521
2522 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2523 if (ret) {
2524 ib_destroy_cm_id(id_priv->cm_id.ib);
2525 id_priv->cm_id.ib = NULL;
2526 }
2527out:
2528 kfree(req.private_data);
2529 return ret;
2530}
2531
Sean Heftye51060f2006-06-17 20:37:29 -07002532static int cma_connect_ib(struct rdma_id_private *id_priv,
2533 struct rdma_conn_param *conn_param)
2534{
2535 struct ib_cm_req_param req;
2536 struct rdma_route *route;
2537 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002538 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07002539 int offset, ret;
2540
2541 memset(&req, 0, sizeof req);
2542 offset = cma_user_data_offset(id_priv->id.ps);
2543 req.private_data_len = offset + conn_param->private_data_len;
2544 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2545 if (!private_data)
2546 return -ENOMEM;
2547
2548 if (conn_param->private_data && conn_param->private_data_len)
2549 memcpy(private_data + offset, conn_param->private_data,
2550 conn_param->private_data_len);
2551
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002552 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
2553 if (IS_ERR(id)) {
2554 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07002555 goto out;
2556 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002557 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07002558
2559 route = &id_priv->id.route;
2560 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2561 if (ret)
2562 goto out;
2563 req.private_data = private_data;
2564
2565 req.primary_path = &route->path_rec[0];
2566 if (route->num_paths == 2)
2567 req.alternate_path = &route->path_rec[1];
2568
2569 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002570 (struct sockaddr *) &route->addr.dst_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002571 req.qp_num = id_priv->qp_num;
Sean Hefty9b2e9c02006-11-30 16:30:47 -08002572 req.qp_type = IB_QPT_RC;
Sean Heftye51060f2006-06-17 20:37:29 -07002573 req.starting_psn = id_priv->seq_num;
2574 req.responder_resources = conn_param->responder_resources;
2575 req.initiator_depth = conn_param->initiator_depth;
2576 req.flow_control = conn_param->flow_control;
2577 req.retry_count = conn_param->retry_count;
2578 req.rnr_retry_count = conn_param->rnr_retry_count;
2579 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2580 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2581 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2582 req.srq = id_priv->srq ? 1 : 0;
2583
2584 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2585out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002586 if (ret && !IS_ERR(id)) {
2587 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07002588 id_priv->cm_id.ib = NULL;
2589 }
2590
Sean Heftye51060f2006-06-17 20:37:29 -07002591 kfree(private_data);
2592 return ret;
2593}
2594
Tom Tucker07ebafb2006-08-03 16:02:42 -05002595static int cma_connect_iw(struct rdma_id_private *id_priv,
2596 struct rdma_conn_param *conn_param)
2597{
2598 struct iw_cm_id *cm_id;
2599 struct sockaddr_in* sin;
2600 int ret;
2601 struct iw_cm_conn_param iw_param;
2602
2603 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002604 if (IS_ERR(cm_id))
2605 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002606
2607 id_priv->cm_id.iw = cm_id;
2608
2609 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2610 cm_id->local_addr = *sin;
2611
2612 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2613 cm_id->remote_addr = *sin;
2614
Sean Hefty5851bb82008-01-04 10:47:12 -08002615 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07002616 if (ret)
2617 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002618
2619 iw_param.ord = conn_param->initiator_depth;
2620 iw_param.ird = conn_param->responder_resources;
2621 iw_param.private_data = conn_param->private_data;
2622 iw_param.private_data_len = conn_param->private_data_len;
2623 if (id_priv->id.qp)
2624 iw_param.qpn = id_priv->qp_num;
2625 else
2626 iw_param.qpn = conn_param->qp_num;
2627 ret = iw_cm_connect(cm_id, &iw_param);
2628out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002629 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07002630 iw_destroy_cm_id(cm_id);
2631 id_priv->cm_id.iw = NULL;
2632 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002633 return ret;
2634}
2635
Sean Heftye51060f2006-06-17 20:37:29 -07002636int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2637{
2638 struct rdma_id_private *id_priv;
2639 int ret;
2640
2641 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002642 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07002643 return -EINVAL;
2644
2645 if (!id->qp) {
2646 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002647 id_priv->srq = conn_param->srq;
2648 }
2649
Tom Tucker07ebafb2006-08-03 16:02:42 -05002650 switch (rdma_node_get_transport(id->device->node_type)) {
2651 case RDMA_TRANSPORT_IB:
Sean Heftyb26f9b92010-04-01 17:08:41 +00002652 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08002653 ret = cma_resolve_ib_udp(id_priv, conn_param);
2654 else
2655 ret = cma_connect_ib(id_priv, conn_param);
Sean Heftye51060f2006-06-17 20:37:29 -07002656 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002657 case RDMA_TRANSPORT_IWARP:
2658 ret = cma_connect_iw(id_priv, conn_param);
2659 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002660 default:
2661 ret = -ENOSYS;
2662 break;
2663 }
2664 if (ret)
2665 goto err;
2666
2667 return 0;
2668err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002669 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002670 return ret;
2671}
2672EXPORT_SYMBOL(rdma_connect);
2673
2674static int cma_accept_ib(struct rdma_id_private *id_priv,
2675 struct rdma_conn_param *conn_param)
2676{
2677 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08002678 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002679
Sean Hefty5851bb82008-01-04 10:47:12 -08002680 ret = cma_modify_qp_rtr(id_priv, conn_param);
2681 if (ret)
2682 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08002683
Sean Hefty5851bb82008-01-04 10:47:12 -08002684 ret = cma_modify_qp_rts(id_priv, conn_param);
2685 if (ret)
2686 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002687
2688 memset(&rep, 0, sizeof rep);
2689 rep.qp_num = id_priv->qp_num;
2690 rep.starting_psn = id_priv->seq_num;
2691 rep.private_data = conn_param->private_data;
2692 rep.private_data_len = conn_param->private_data_len;
2693 rep.responder_resources = conn_param->responder_resources;
2694 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07002695 rep.failover_accepted = 0;
2696 rep.flow_control = conn_param->flow_control;
2697 rep.rnr_retry_count = conn_param->rnr_retry_count;
2698 rep.srq = id_priv->srq ? 1 : 0;
2699
Sean Hefty0fe313b2006-11-30 16:37:15 -08002700 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2701out:
2702 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002703}
2704
Tom Tucker07ebafb2006-08-03 16:02:42 -05002705static int cma_accept_iw(struct rdma_id_private *id_priv,
2706 struct rdma_conn_param *conn_param)
2707{
2708 struct iw_cm_conn_param iw_param;
2709 int ret;
2710
Sean Hefty5851bb82008-01-04 10:47:12 -08002711 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002712 if (ret)
2713 return ret;
2714
2715 iw_param.ord = conn_param->initiator_depth;
2716 iw_param.ird = conn_param->responder_resources;
2717 iw_param.private_data = conn_param->private_data;
2718 iw_param.private_data_len = conn_param->private_data_len;
2719 if (id_priv->id.qp) {
2720 iw_param.qpn = id_priv->qp_num;
2721 } else
2722 iw_param.qpn = conn_param->qp_num;
2723
2724 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2725}
2726
Sean Hefty628e5f62006-11-30 16:44:16 -08002727static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2728 enum ib_cm_sidr_status status,
2729 const void *private_data, int private_data_len)
2730{
2731 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002732 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002733
2734 memset(&rep, 0, sizeof rep);
2735 rep.status = status;
2736 if (status == IB_SIDR_SUCCESS) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002737 ret = cma_set_qkey(id_priv);
2738 if (ret)
2739 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002740 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002741 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08002742 }
2743 rep.private_data = private_data;
2744 rep.private_data_len = private_data_len;
2745
2746 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2747}
2748
Sean Heftye51060f2006-06-17 20:37:29 -07002749int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2750{
2751 struct rdma_id_private *id_priv;
2752 int ret;
2753
2754 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00002755
2756 id_priv->owner = task_pid_nr(current);
2757
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002758 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07002759 return -EINVAL;
2760
2761 if (!id->qp && conn_param) {
2762 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002763 id_priv->srq = conn_param->srq;
2764 }
2765
Tom Tucker07ebafb2006-08-03 16:02:42 -05002766 switch (rdma_node_get_transport(id->device->node_type)) {
2767 case RDMA_TRANSPORT_IB:
Sean Heftyb26f9b92010-04-01 17:08:41 +00002768 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08002769 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2770 conn_param->private_data,
2771 conn_param->private_data_len);
2772 else if (conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -07002773 ret = cma_accept_ib(id_priv, conn_param);
2774 else
2775 ret = cma_rep_recv(id_priv);
2776 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002777 case RDMA_TRANSPORT_IWARP:
2778 ret = cma_accept_iw(id_priv, conn_param);
2779 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002780 default:
2781 ret = -ENOSYS;
2782 break;
2783 }
2784
2785 if (ret)
2786 goto reject;
2787
2788 return 0;
2789reject:
Sean Heftyc5483382007-09-24 13:19:09 -07002790 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002791 rdma_reject(id, NULL, 0);
2792 return ret;
2793}
2794EXPORT_SYMBOL(rdma_accept);
2795
Sean Hefty0fe313b2006-11-30 16:37:15 -08002796int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2797{
2798 struct rdma_id_private *id_priv;
2799 int ret;
2800
2801 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002802 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08002803 return -EINVAL;
2804
2805 switch (id->device->node_type) {
2806 case RDMA_NODE_IB_CA:
2807 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2808 break;
2809 default:
2810 ret = 0;
2811 break;
2812 }
2813 return ret;
2814}
2815EXPORT_SYMBOL(rdma_notify);
2816
Sean Heftye51060f2006-06-17 20:37:29 -07002817int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2818 u8 private_data_len)
2819{
2820 struct rdma_id_private *id_priv;
2821 int ret;
2822
2823 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002824 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07002825 return -EINVAL;
2826
Tom Tucker07ebafb2006-08-03 16:02:42 -05002827 switch (rdma_node_get_transport(id->device->node_type)) {
2828 case RDMA_TRANSPORT_IB:
Sean Heftyb26f9b92010-04-01 17:08:41 +00002829 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08002830 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2831 private_data, private_data_len);
2832 else
2833 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2834 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2835 0, private_data, private_data_len);
Sean Heftye51060f2006-06-17 20:37:29 -07002836 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002837 case RDMA_TRANSPORT_IWARP:
2838 ret = iw_cm_reject(id_priv->cm_id.iw,
2839 private_data, private_data_len);
2840 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002841 default:
2842 ret = -ENOSYS;
2843 break;
2844 }
2845 return ret;
2846}
2847EXPORT_SYMBOL(rdma_reject);
2848
2849int rdma_disconnect(struct rdma_cm_id *id)
2850{
2851 struct rdma_id_private *id_priv;
2852 int ret;
2853
2854 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002855 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07002856 return -EINVAL;
2857
Tom Tucker07ebafb2006-08-03 16:02:42 -05002858 switch (rdma_node_get_transport(id->device->node_type)) {
2859 case RDMA_TRANSPORT_IB:
Sean Heftyc5483382007-09-24 13:19:09 -07002860 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002861 if (ret)
2862 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002863 /* Initiate or respond to a disconnect. */
2864 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2865 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2866 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002867 case RDMA_TRANSPORT_IWARP:
2868 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2869 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002870 default:
Tom Tucker07ebafb2006-08-03 16:02:42 -05002871 ret = -EINVAL;
Sean Heftye51060f2006-06-17 20:37:29 -07002872 break;
2873 }
2874out:
2875 return ret;
2876}
2877EXPORT_SYMBOL(rdma_disconnect);
2878
Sean Heftyc8f6a362007-02-15 17:00:18 -08002879static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2880{
2881 struct rdma_id_private *id_priv;
2882 struct cma_multicast *mc = multicast->context;
2883 struct rdma_cm_event event;
2884 int ret;
2885
2886 id_priv = mc->id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002887 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
2888 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07002889 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002890
Sean Heftyc5483382007-09-24 13:19:09 -07002891 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002892 if (!status && id_priv->id.qp)
2893 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2894 multicast->rec.mlid);
Sean Heftyc5483382007-09-24 13:19:09 -07002895 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002896
2897 memset(&event, 0, sizeof event);
2898 event.status = status;
2899 event.param.ud.private_data = mc->context;
2900 if (!status) {
2901 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2902 ib_init_ah_from_mcmember(id_priv->id.device,
2903 id_priv->id.port_num, &multicast->rec,
2904 &event.param.ud.ah_attr);
2905 event.param.ud.qp_num = 0xFFFFFF;
2906 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2907 } else
2908 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2909
2910 ret = id_priv->id.event_handler(&id_priv->id, &event);
2911 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002912 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002913 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002914 rdma_destroy_id(&id_priv->id);
2915 return 0;
2916 }
Sean Hefty8aa08602007-05-07 11:49:00 -07002917
Or Gerlitzde910bd2008-07-14 23:48:53 -07002918 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002919 return 0;
2920}
2921
2922static void cma_set_mgid(struct rdma_id_private *id_priv,
2923 struct sockaddr *addr, union ib_gid *mgid)
2924{
2925 unsigned char mc_map[MAX_ADDR_LEN];
2926 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2927 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2928 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2929
2930 if (cma_any_addr(addr)) {
2931 memset(mgid, 0, sizeof *mgid);
2932 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08002933 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08002934 0xFF10A01B)) {
2935 /* IPv6 address is an SA assigned MGID. */
2936 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08002937 } else if ((addr->sa_family == AF_INET6)) {
2938 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
2939 if (id_priv->id.ps == RDMA_PS_UDP)
2940 mc_map[7] = 0x01; /* Use RDMA CM signature */
2941 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002942 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07002943 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002944 if (id_priv->id.ps == RDMA_PS_UDP)
2945 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08002946 *mgid = *(union ib_gid *) (mc_map + 4);
2947 }
2948}
2949
2950static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2951 struct cma_multicast *mc)
2952{
2953 struct ib_sa_mcmember_rec rec;
2954 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2955 ib_sa_comp_mask comp_mask;
2956 int ret;
2957
2958 ib_addr_get_mgid(dev_addr, &rec.mgid);
2959 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2960 &rec.mgid, &rec);
2961 if (ret)
2962 return ret;
2963
Roland Dreier3f446752008-08-04 11:02:14 -07002964 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002965 if (id_priv->id.ps == RDMA_PS_UDP)
2966 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
Sean Hefty6f8372b2009-11-19 13:26:06 -08002967 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002968 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2969 rec.join_state = 1;
2970
2971 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2972 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2973 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2974 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2975 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2976
Yossi Etigin84adeee2009-04-01 13:55:32 -07002977 if (id_priv->id.ps == RDMA_PS_IPOIB)
2978 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
2979 IB_SA_MCMEMBER_REC_RATE_SELECTOR;
2980
Sean Heftyc8f6a362007-02-15 17:00:18 -08002981 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2982 id_priv->id.port_num, &rec,
2983 comp_mask, GFP_KERNEL,
2984 cma_ib_mc_handler, mc);
2985 if (IS_ERR(mc->multicast.ib))
2986 return PTR_ERR(mc->multicast.ib);
2987
2988 return 0;
2989}
2990
Eli Cohen3c86aa72010-10-13 21:26:51 +02002991static void iboe_mcast_work_handler(struct work_struct *work)
2992{
2993 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
2994 struct cma_multicast *mc = mw->mc;
2995 struct ib_sa_multicast *m = mc->multicast.ib;
2996
2997 mc->multicast.ib->context = mc;
2998 cma_ib_mc_handler(0, m);
2999 kref_put(&mc->mcref, release_mc);
3000 kfree(mw);
3001}
3002
3003static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3004{
3005 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3006 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3007
3008 if (cma_any_addr(addr)) {
3009 memset(mgid, 0, sizeof *mgid);
3010 } else if (addr->sa_family == AF_INET6) {
3011 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3012 } else {
3013 mgid->raw[0] = 0xff;
3014 mgid->raw[1] = 0x0e;
3015 mgid->raw[2] = 0;
3016 mgid->raw[3] = 0;
3017 mgid->raw[4] = 0;
3018 mgid->raw[5] = 0;
3019 mgid->raw[6] = 0;
3020 mgid->raw[7] = 0;
3021 mgid->raw[8] = 0;
3022 mgid->raw[9] = 0;
3023 mgid->raw[10] = 0xff;
3024 mgid->raw[11] = 0xff;
3025 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3026 }
3027}
3028
3029static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3030 struct cma_multicast *mc)
3031{
3032 struct iboe_mcast_work *work;
3033 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3034 int err;
3035 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3036 struct net_device *ndev = NULL;
3037
3038 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3039 return -EINVAL;
3040
3041 work = kzalloc(sizeof *work, GFP_KERNEL);
3042 if (!work)
3043 return -ENOMEM;
3044
3045 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3046 if (!mc->multicast.ib) {
3047 err = -ENOMEM;
3048 goto out1;
3049 }
3050
3051 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3052
3053 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3054 if (id_priv->id.ps == RDMA_PS_UDP)
3055 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3056
3057 if (dev_addr->bound_dev_if)
3058 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3059 if (!ndev) {
3060 err = -ENODEV;
3061 goto out2;
3062 }
3063 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3064 mc->multicast.ib->rec.hop_limit = 1;
3065 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3066 dev_put(ndev);
3067 if (!mc->multicast.ib->rec.mtu) {
3068 err = -EINVAL;
3069 goto out2;
3070 }
3071 iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3072 work->id = id_priv;
3073 work->mc = mc;
3074 INIT_WORK(&work->work, iboe_mcast_work_handler);
3075 kref_get(&mc->mcref);
3076 queue_work(cma_wq, &work->work);
3077
3078 return 0;
3079
3080out2:
3081 kfree(mc->multicast.ib);
3082out1:
3083 kfree(work);
3084 return err;
3085}
3086
Sean Heftyc8f6a362007-02-15 17:00:18 -08003087int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3088 void *context)
3089{
3090 struct rdma_id_private *id_priv;
3091 struct cma_multicast *mc;
3092 int ret;
3093
3094 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003095 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3096 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08003097 return -EINVAL;
3098
3099 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3100 if (!mc)
3101 return -ENOMEM;
3102
3103 memcpy(&mc->addr, addr, ip_addr_size(addr));
3104 mc->context = context;
3105 mc->id_priv = id_priv;
3106
3107 spin_lock(&id_priv->lock);
3108 list_add(&mc->list, &id_priv->mc_list);
3109 spin_unlock(&id_priv->lock);
3110
3111 switch (rdma_node_get_transport(id->device->node_type)) {
3112 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02003113 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3114 case IB_LINK_LAYER_INFINIBAND:
3115 ret = cma_join_ib_multicast(id_priv, mc);
3116 break;
3117 case IB_LINK_LAYER_ETHERNET:
3118 kref_init(&mc->mcref);
3119 ret = cma_iboe_join_multicast(id_priv, mc);
3120 break;
3121 default:
3122 ret = -EINVAL;
3123 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003124 break;
3125 default:
3126 ret = -ENOSYS;
3127 break;
3128 }
3129
3130 if (ret) {
3131 spin_lock_irq(&id_priv->lock);
3132 list_del(&mc->list);
3133 spin_unlock_irq(&id_priv->lock);
3134 kfree(mc);
3135 }
3136 return ret;
3137}
3138EXPORT_SYMBOL(rdma_join_multicast);
3139
3140void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3141{
3142 struct rdma_id_private *id_priv;
3143 struct cma_multicast *mc;
3144
3145 id_priv = container_of(id, struct rdma_id_private, id);
3146 spin_lock_irq(&id_priv->lock);
3147 list_for_each_entry(mc, &id_priv->mc_list, list) {
3148 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
3149 list_del(&mc->list);
3150 spin_unlock_irq(&id_priv->lock);
3151
3152 if (id->qp)
3153 ib_detach_mcast(id->qp,
3154 &mc->multicast.ib->rec.mgid,
3155 mc->multicast.ib->rec.mlid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003156 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3157 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3158 case IB_LINK_LAYER_INFINIBAND:
3159 ib_sa_free_multicast(mc->multicast.ib);
3160 kfree(mc);
3161 break;
3162 case IB_LINK_LAYER_ETHERNET:
3163 kref_put(&mc->mcref, release_mc);
3164 break;
3165 default:
3166 break;
3167 }
3168 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003169 return;
3170 }
3171 }
3172 spin_unlock_irq(&id_priv->lock);
3173}
3174EXPORT_SYMBOL(rdma_leave_multicast);
3175
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003176static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3177{
3178 struct rdma_dev_addr *dev_addr;
3179 struct cma_ndev_work *work;
3180
3181 dev_addr = &id_priv->id.route.addr.dev_addr;
3182
Sean Hefty6266ed62009-11-19 12:55:22 -08003183 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003184 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3185 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3186 ndev->name, &id_priv->id);
3187 work = kzalloc(sizeof *work, GFP_KERNEL);
3188 if (!work)
3189 return -ENOMEM;
3190
3191 INIT_WORK(&work->work, cma_ndev_work_handler);
3192 work->id = id_priv;
3193 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3194 atomic_inc(&id_priv->refcount);
3195 queue_work(cma_wq, &work->work);
3196 }
3197
3198 return 0;
3199}
3200
3201static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
3202 void *ctx)
3203{
3204 struct net_device *ndev = (struct net_device *)ctx;
3205 struct cma_device *cma_dev;
3206 struct rdma_id_private *id_priv;
3207 int ret = NOTIFY_DONE;
3208
3209 if (dev_net(ndev) != &init_net)
3210 return NOTIFY_DONE;
3211
3212 if (event != NETDEV_BONDING_FAILOVER)
3213 return NOTIFY_DONE;
3214
3215 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3216 return NOTIFY_DONE;
3217
3218 mutex_lock(&lock);
3219 list_for_each_entry(cma_dev, &dev_list, list)
3220 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3221 ret = cma_netdev_change(ndev, id_priv);
3222 if (ret)
3223 goto out;
3224 }
3225
3226out:
3227 mutex_unlock(&lock);
3228 return ret;
3229}
3230
3231static struct notifier_block cma_nb = {
3232 .notifier_call = cma_netdev_callback
3233};
3234
Sean Heftye51060f2006-06-17 20:37:29 -07003235static void cma_add_one(struct ib_device *device)
3236{
3237 struct cma_device *cma_dev;
3238 struct rdma_id_private *id_priv;
3239
3240 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3241 if (!cma_dev)
3242 return;
3243
3244 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07003245
3246 init_completion(&cma_dev->comp);
3247 atomic_set(&cma_dev->refcount, 1);
3248 INIT_LIST_HEAD(&cma_dev->id_list);
3249 ib_set_client_data(device, &cma_client, cma_dev);
3250
3251 mutex_lock(&lock);
3252 list_add_tail(&cma_dev->list, &dev_list);
3253 list_for_each_entry(id_priv, &listen_any_list, list)
3254 cma_listen_on_dev(id_priv, cma_dev);
3255 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07003256}
3257
3258static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3259{
Sean Heftya1b1b612006-11-30 16:33:14 -08003260 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003261 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003262 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07003263
3264 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003265 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3266 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07003267 return 0;
3268
3269 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003270 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07003271
3272 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003273 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07003274 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003275
Sean Heftya1b1b612006-11-30 16:33:14 -08003276 memset(&event, 0, sizeof event);
3277 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003278 ret = id_priv->id.event_handler(&id_priv->id, &event);
3279out:
3280 mutex_unlock(&id_priv->handler_mutex);
3281 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003282}
3283
3284static void cma_process_remove(struct cma_device *cma_dev)
3285{
Sean Heftye51060f2006-06-17 20:37:29 -07003286 struct rdma_id_private *id_priv;
3287 int ret;
3288
Sean Heftye51060f2006-06-17 20:37:29 -07003289 mutex_lock(&lock);
3290 while (!list_empty(&cma_dev->id_list)) {
3291 id_priv = list_entry(cma_dev->id_list.next,
3292 struct rdma_id_private, list);
3293
Sean Heftyd02d1f52007-10-09 11:12:34 -07003294 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07003295 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07003296 atomic_inc(&id_priv->refcount);
3297 mutex_unlock(&lock);
3298
Sean Heftyd02d1f52007-10-09 11:12:34 -07003299 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003300 cma_deref_id(id_priv);
3301 if (ret)
3302 rdma_destroy_id(&id_priv->id);
3303
3304 mutex_lock(&lock);
3305 }
3306 mutex_unlock(&lock);
3307
3308 cma_deref_dev(cma_dev);
3309 wait_for_completion(&cma_dev->comp);
3310}
3311
3312static void cma_remove_one(struct ib_device *device)
3313{
3314 struct cma_device *cma_dev;
3315
3316 cma_dev = ib_get_client_data(device, &cma_client);
3317 if (!cma_dev)
3318 return;
3319
3320 mutex_lock(&lock);
3321 list_del(&cma_dev->list);
3322 mutex_unlock(&lock);
3323
3324 cma_process_remove(cma_dev);
3325 kfree(cma_dev);
3326}
3327
Nir Muchtar753f6182011-01-03 15:33:53 +00003328static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3329{
3330 struct nlmsghdr *nlh;
3331 struct rdma_cm_id_stats *id_stats;
3332 struct rdma_id_private *id_priv;
3333 struct rdma_cm_id *id = NULL;
3334 struct cma_device *cma_dev;
3335 int i_dev = 0, i_id = 0;
3336
3337 /*
3338 * We export all of the IDs as a sequence of messages. Each
3339 * ID gets its own netlink message.
3340 */
3341 mutex_lock(&lock);
3342
3343 list_for_each_entry(cma_dev, &dev_list, list) {
3344 if (i_dev < cb->args[0]) {
3345 i_dev++;
3346 continue;
3347 }
3348
3349 i_id = 0;
3350 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3351 if (i_id < cb->args[1]) {
3352 i_id++;
3353 continue;
3354 }
3355
3356 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3357 sizeof *id_stats, RDMA_NL_RDMA_CM,
3358 RDMA_NL_RDMA_CM_ID_STATS);
3359 if (!id_stats)
3360 goto out;
3361
3362 memset(id_stats, 0, sizeof *id_stats);
3363 id = &id_priv->id;
3364 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3365 id_stats->port_num = id->port_num;
3366 id_stats->bound_dev_if =
3367 id->route.addr.dev_addr.bound_dev_if;
3368
3369 if (id->route.addr.src_addr.ss_family == AF_INET) {
3370 if (ibnl_put_attr(skb, nlh,
3371 sizeof(struct sockaddr_in),
3372 &id->route.addr.src_addr,
3373 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3374 goto out;
3375 }
3376 if (ibnl_put_attr(skb, nlh,
3377 sizeof(struct sockaddr_in),
3378 &id->route.addr.dst_addr,
3379 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3380 goto out;
3381 }
3382 } else if (id->route.addr.src_addr.ss_family == AF_INET6) {
3383 if (ibnl_put_attr(skb, nlh,
3384 sizeof(struct sockaddr_in6),
3385 &id->route.addr.src_addr,
3386 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3387 goto out;
3388 }
3389 if (ibnl_put_attr(skb, nlh,
3390 sizeof(struct sockaddr_in6),
3391 &id->route.addr.dst_addr,
3392 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3393 goto out;
3394 }
3395 }
3396
Nir Muchtar83e95022011-01-13 13:56:04 +00003397 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00003398 id_stats->port_space = id->ps;
3399 id_stats->cm_state = id_priv->state;
3400 id_stats->qp_num = id_priv->qp_num;
3401 id_stats->qp_type = id->qp_type;
3402
3403 i_id++;
3404 }
3405
3406 cb->args[1] = 0;
3407 i_dev++;
3408 }
3409
3410out:
3411 mutex_unlock(&lock);
3412 cb->args[0] = i_dev;
3413 cb->args[1] = i_id;
3414
3415 return skb->len;
3416}
3417
3418static const struct ibnl_client_cbs cma_cb_table[] = {
3419 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats },
3420};
3421
Peter Huewe716abb12009-06-23 10:38:42 -07003422static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003423{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09003424 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003425
Sean Heftyc7f743a2007-02-01 12:23:37 -08003426 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07003427 if (!cma_wq)
3428 return -ENOMEM;
3429
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003430 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08003431 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003432 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003433
Sean Heftye51060f2006-06-17 20:37:29 -07003434 ret = ib_register_client(&cma_client);
3435 if (ret)
3436 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00003437
3438 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
3439 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
3440
Sean Heftye51060f2006-06-17 20:37:29 -07003441 return 0;
3442
3443err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003444 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003445 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003446 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003447 destroy_workqueue(cma_wq);
3448 return ret;
3449}
3450
Peter Huewe716abb12009-06-23 10:38:42 -07003451static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003452{
Nir Muchtar753f6182011-01-03 15:33:53 +00003453 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07003454 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003455 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003456 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003457 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003458 destroy_workqueue(cma_wq);
3459 idr_destroy(&sdp_ps);
3460 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08003461 idr_destroy(&udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003462 idr_destroy(&ipoib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07003463}
3464
3465module_init(cma_init);
3466module_exit(cma_cleanup);