blob: d0d4aa9f480245097b2fab1bd05380c3fe6a01d6 [file] [log] [blame]
Sean Heftye51060f2006-06-17 20:37:29 -07001/*
2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
6 *
Sean Heftya9474912008-07-14 23:48:43 -07007 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
Sean Heftye51060f2006-06-17 20:37:29 -070012 *
Sean Heftya9474912008-07-14 23:48:43 -070013 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
Sean Heftye51060f2006-06-17 20:37:29 -070016 *
Sean Heftya9474912008-07-14 23:48:43 -070017 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
Sean Heftye51060f2006-06-17 20:37:29 -070020 *
Sean Heftya9474912008-07-14 23:48:43 -070021 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
Sean Heftye51060f2006-06-17 20:37:29 -070025 *
Sean Heftya9474912008-07-14 23:48:43 -070026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
Sean Heftye51060f2006-06-17 20:37:29 -070034 */
35
36#include <linux/completion.h>
37#include <linux/in.h>
38#include <linux/in6.h>
39#include <linux/mutex.h>
40#include <linux/random.h>
41#include <linux/idr.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050042#include <linux/inetdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Paul Gortmakere4dd23d2011-05-27 15:35:46 -040044#include <linux/module.h>
Sean Heftye51060f2006-06-17 20:37:29 -070045
46#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080047#include <net/ipv6.h>
Sean Heftye51060f2006-06-17 20:37:29 -070048
49#include <rdma/rdma_cm.h>
50#include <rdma/rdma_cm_ib.h>
Nir Muchtar753f6182011-01-03 15:33:53 +000051#include <rdma/rdma_netlink.h>
Sean Heftye51060f2006-06-17 20:37:29 -070052#include <rdma/ib_cache.h>
53#include <rdma/ib_cm.h>
54#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050055#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070056
57MODULE_AUTHOR("Sean Hefty");
58MODULE_DESCRIPTION("Generic RDMA CM Agent");
59MODULE_LICENSE("Dual BSD/GPL");
60
61#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030062#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070063#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020064#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070065
66static void cma_add_one(struct ib_device *device);
67static void cma_remove_one(struct ib_device *device);
68
69static struct ib_client cma_client = {
70 .name = "cma",
71 .add = cma_add_one,
72 .remove = cma_remove_one
73};
74
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070075static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -080076static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -070077static LIST_HEAD(dev_list);
78static LIST_HEAD(listen_any_list);
79static DEFINE_MUTEX(lock);
80static struct workqueue_struct *cma_wq;
81static DEFINE_IDR(sdp_ps);
82static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -080083static DEFINE_IDR(udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -080084static DEFINE_IDR(ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -070085static DEFINE_IDR(ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -070086
87struct cma_device {
88 struct list_head list;
89 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -070090 struct completion comp;
91 atomic_t refcount;
92 struct list_head id_list;
93};
94
Sean Heftye51060f2006-06-17 20:37:29 -070095struct rdma_bind_list {
96 struct idr *ps;
97 struct hlist_head owners;
98 unsigned short port;
99};
100
101/*
102 * Device removal can occur at anytime, so we need extra handling to
103 * serialize notifying the user of device removal with other callbacks.
104 * We do this by disabling removal notification while a callback is in process,
105 * and reporting it after the callback completes.
106 */
107struct rdma_id_private {
108 struct rdma_cm_id id;
109
110 struct rdma_bind_list *bind_list;
111 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700112 struct list_head list; /* listen_any_list or cma_device.list */
113 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700114 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800115 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700116
Sean Heftyd02d1f52007-10-09 11:12:34 -0700117 int internal_id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700118 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700119 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700120 struct mutex qp_mutex;
121
Sean Heftye51060f2006-06-17 20:37:29 -0700122 struct completion comp;
123 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700124 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700125
126 int backlog;
127 int timeout_ms;
128 struct ib_sa_query *query;
129 int query_id;
130 union {
131 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500132 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700133 } cm_id;
134
135 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800136 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700137 u32 qp_num;
Nir Muchtar83e95022011-01-13 13:56:04 +0000138 pid_t owner;
Sean Heftye51060f2006-06-17 20:37:29 -0700139 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700140 u8 tos;
Hefty, Seana9bb7912011-05-09 22:06:10 -0700141 u8 reuseaddr;
Sean Heftye51060f2006-06-17 20:37:29 -0700142};
143
Sean Heftyc8f6a362007-02-15 17:00:18 -0800144struct cma_multicast {
145 struct rdma_id_private *id_priv;
146 union {
147 struct ib_sa_multicast *ib;
148 } multicast;
149 struct list_head list;
150 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700151 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200152 struct kref mcref;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800153};
154
Sean Heftye51060f2006-06-17 20:37:29 -0700155struct cma_work {
156 struct work_struct work;
157 struct rdma_id_private *id;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700158 enum rdma_cm_state old_state;
159 enum rdma_cm_state new_state;
Sean Heftye51060f2006-06-17 20:37:29 -0700160 struct rdma_cm_event event;
161};
162
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700163struct cma_ndev_work {
164 struct work_struct work;
165 struct rdma_id_private *id;
166 struct rdma_cm_event event;
167};
168
Eli Cohen3c86aa72010-10-13 21:26:51 +0200169struct iboe_mcast_work {
170 struct work_struct work;
171 struct rdma_id_private *id;
172 struct cma_multicast *mc;
173};
174
Sean Heftye51060f2006-06-17 20:37:29 -0700175union cma_ip_addr {
176 struct in6_addr ip6;
177 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000178 __be32 pad[3];
179 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700180 } ip4;
181};
182
183struct cma_hdr {
184 u8 cma_version;
185 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000186 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700187 union cma_ip_addr src_addr;
188 union cma_ip_addr dst_addr;
189};
190
191struct sdp_hh {
192 u8 bsdh[16];
193 u8 sdp_version; /* Major version: 7:4 */
194 u8 ip_version; /* IP version: 7:4 */
195 u8 sdp_specific1[10];
Al Viro1b90c132008-03-29 03:10:28 +0000196 __be16 port;
197 __be16 sdp_specific2;
Sean Heftye51060f2006-06-17 20:37:29 -0700198 union cma_ip_addr src_addr;
199 union cma_ip_addr dst_addr;
200};
201
202struct sdp_hah {
203 u8 bsdh[16];
204 u8 sdp_version;
205};
206
207#define CMA_VERSION 0x00
208#define SDP_MAJ_VERSION 0x2
209
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700210static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp)
Sean Heftye51060f2006-06-17 20:37:29 -0700211{
212 unsigned long flags;
213 int ret;
214
215 spin_lock_irqsave(&id_priv->lock, flags);
216 ret = (id_priv->state == comp);
217 spin_unlock_irqrestore(&id_priv->lock, flags);
218 return ret;
219}
220
221static int cma_comp_exch(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700222 enum rdma_cm_state comp, enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700223{
224 unsigned long flags;
225 int ret;
226
227 spin_lock_irqsave(&id_priv->lock, flags);
228 if ((ret = (id_priv->state == comp)))
229 id_priv->state = exch;
230 spin_unlock_irqrestore(&id_priv->lock, flags);
231 return ret;
232}
233
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700234static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv,
235 enum rdma_cm_state exch)
Sean Heftye51060f2006-06-17 20:37:29 -0700236{
237 unsigned long flags;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700238 enum rdma_cm_state old;
Sean Heftye51060f2006-06-17 20:37:29 -0700239
240 spin_lock_irqsave(&id_priv->lock, flags);
241 old = id_priv->state;
242 id_priv->state = exch;
243 spin_unlock_irqrestore(&id_priv->lock, flags);
244 return old;
245}
246
247static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
248{
249 return hdr->ip_version >> 4;
250}
251
252static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
253{
254 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
255}
256
257static inline u8 sdp_get_majv(u8 sdp_version)
258{
259 return sdp_version >> 4;
260}
261
262static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
263{
264 return hh->ip_version >> 4;
265}
266
267static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
268{
269 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
270}
271
272static void cma_attach_to_dev(struct rdma_id_private *id_priv,
273 struct cma_device *cma_dev)
274{
275 atomic_inc(&cma_dev->refcount);
276 id_priv->cma_dev = cma_dev;
277 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200278 id_priv->id.route.addr.dev_addr.transport =
279 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700280 list_add_tail(&id_priv->list, &cma_dev->id_list);
281}
282
283static inline void cma_deref_dev(struct cma_device *cma_dev)
284{
285 if (atomic_dec_and_test(&cma_dev->refcount))
286 complete(&cma_dev->comp);
287}
288
Eli Cohen3c86aa72010-10-13 21:26:51 +0200289static inline void release_mc(struct kref *kref)
290{
291 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
292
293 kfree(mc->multicast.ib);
294 kfree(mc);
295}
296
Sean Heftya396d432011-02-23 09:05:39 -0800297static void cma_release_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700298{
Sean Heftya396d432011-02-23 09:05:39 -0800299 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700300 list_del(&id_priv->list);
301 cma_deref_dev(id_priv->cma_dev);
302 id_priv->cma_dev = NULL;
Sean Heftya396d432011-02-23 09:05:39 -0800303 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700304}
305
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700306static int cma_set_qkey(struct rdma_id_private *id_priv)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800307{
308 struct ib_sa_mcmember_rec rec;
309 int ret = 0;
310
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700311 if (id_priv->qkey)
312 return 0;
313
314 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800315 case RDMA_PS_UDP:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700316 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800317 break;
318 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700319 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
320 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
321 id_priv->id.port_num, &rec.mgid,
322 &rec);
323 if (!ret)
324 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800325 break;
326 default:
327 break;
328 }
329 return ret;
330}
331
Eli Cohen3c86aa72010-10-13 21:26:51 +0200332static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
333{
334 int i;
335 int err;
336 struct ib_port_attr props;
337 union ib_gid tmp;
338
339 err = ib_query_port(device, port_num, &props);
340 if (err)
341 return 1;
342
343 for (i = 0; i < props.gid_tbl_len; ++i) {
344 err = ib_query_gid(device, port_num, i, &tmp);
345 if (err)
346 return 1;
347 if (!memcmp(&tmp, gid, sizeof tmp))
348 return 0;
349 }
350
351 return -EAGAIN;
352}
353
Tom Tucker07ebafb2006-08-03 16:02:42 -0500354static int cma_acquire_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700355{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800356 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700357 struct cma_device *cma_dev;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200358 union ib_gid gid, iboe_gid;
Sean Heftye51060f2006-06-17 20:37:29 -0700359 int ret = -ENODEV;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200360 u8 port;
361 enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
362 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
Sean Heftye51060f2006-06-17 20:37:29 -0700363
Moni Shoua2efdd6a2011-07-12 11:23:15 +0000364 if (dev_ll != IB_LINK_LAYER_INFINIBAND &&
365 id_priv->id.ps == RDMA_PS_IPOIB)
366 return -EINVAL;
367
Sean Heftya396d432011-02-23 09:05:39 -0800368 mutex_lock(&lock);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200369 iboe_addr_get_sgid(dev_addr, &iboe_gid);
370 memcpy(&gid, dev_addr->src_dev_addr +
371 rdma_addr_gid_offset(dev_addr), sizeof gid);
Sean Heftye51060f2006-06-17 20:37:29 -0700372 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200373 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
374 if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
375 if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
376 rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
377 ret = find_gid_port(cma_dev->device, &iboe_gid, port);
378 else
379 ret = find_gid_port(cma_dev->device, &gid, port);
380
381 if (!ret) {
382 id_priv->id.port_num = port;
383 goto out;
384 } else if (ret == 1)
385 break;
386 }
Sean Heftye51060f2006-06-17 20:37:29 -0700387 }
388 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200389
390out:
391 if (!ret)
392 cma_attach_to_dev(id_priv, cma_dev);
393
Sean Heftya396d432011-02-23 09:05:39 -0800394 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700395 return ret;
396}
397
Sean Heftye51060f2006-06-17 20:37:29 -0700398static void cma_deref_id(struct rdma_id_private *id_priv)
399{
400 if (atomic_dec_and_test(&id_priv->refcount))
401 complete(&id_priv->comp);
402}
403
Or Gerlitzde910bd2008-07-14 23:48:53 -0700404static int cma_disable_callback(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700405 enum rdma_cm_state state)
Sean Hefty8aa08602007-05-07 11:49:00 -0700406{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700407 mutex_lock(&id_priv->handler_mutex);
408 if (id_priv->state != state) {
409 mutex_unlock(&id_priv->handler_mutex);
410 return -EINVAL;
411 }
412 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700413}
414
415struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
Sean Heftyb26f9b92010-04-01 17:08:41 +0000416 void *context, enum rdma_port_space ps,
417 enum ib_qp_type qp_type)
Sean Heftye51060f2006-06-17 20:37:29 -0700418{
419 struct rdma_id_private *id_priv;
420
421 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
422 if (!id_priv)
423 return ERR_PTR(-ENOMEM);
424
Nir Muchtar83e95022011-01-13 13:56:04 +0000425 id_priv->owner = task_pid_nr(current);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700426 id_priv->state = RDMA_CM_IDLE;
Sean Heftye51060f2006-06-17 20:37:29 -0700427 id_priv->id.context = context;
428 id_priv->id.event_handler = event_handler;
429 id_priv->id.ps = ps;
Sean Heftyb26f9b92010-04-01 17:08:41 +0000430 id_priv->id.qp_type = qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -0700431 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700432 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700433 init_completion(&id_priv->comp);
434 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700435 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700436 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800437 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700438 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
439
440 return &id_priv->id;
441}
442EXPORT_SYMBOL(rdma_create_id);
443
Sean Heftyc8f6a362007-02-15 17:00:18 -0800444static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700445{
446 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800447 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700448
Sean Heftyc8f6a362007-02-15 17:00:18 -0800449 qp_attr.qp_state = IB_QPS_INIT;
450 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700451 if (ret)
452 return ret;
453
Sean Heftyc8f6a362007-02-15 17:00:18 -0800454 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
455 if (ret)
456 return ret;
457
458 qp_attr.qp_state = IB_QPS_RTR;
459 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
460 if (ret)
461 return ret;
462
463 qp_attr.qp_state = IB_QPS_RTS;
464 qp_attr.sq_psn = 0;
465 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
466
467 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700468}
469
Sean Heftyc8f6a362007-02-15 17:00:18 -0800470static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500471{
472 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800473 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500474
475 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800476 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
477 if (ret)
478 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500479
Sean Heftyc8f6a362007-02-15 17:00:18 -0800480 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500481}
482
Sean Heftye51060f2006-06-17 20:37:29 -0700483int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
484 struct ib_qp_init_attr *qp_init_attr)
485{
486 struct rdma_id_private *id_priv;
487 struct ib_qp *qp;
488 int ret;
489
490 id_priv = container_of(id, struct rdma_id_private, id);
491 if (id->device != pd->device)
492 return -EINVAL;
493
494 qp = ib_create_qp(pd, qp_init_attr);
495 if (IS_ERR(qp))
496 return PTR_ERR(qp);
497
Sean Heftyb26f9b92010-04-01 17:08:41 +0000498 if (id->qp_type == IB_QPT_UD)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800499 ret = cma_init_ud_qp(id_priv, qp);
500 else
501 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700502 if (ret)
503 goto err;
504
505 id->qp = qp;
506 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700507 id_priv->srq = (qp->srq != NULL);
508 return 0;
509err:
510 ib_destroy_qp(qp);
511 return ret;
512}
513EXPORT_SYMBOL(rdma_create_qp);
514
515void rdma_destroy_qp(struct rdma_cm_id *id)
516{
Sean Heftyc5483382007-09-24 13:19:09 -0700517 struct rdma_id_private *id_priv;
518
519 id_priv = container_of(id, struct rdma_id_private, id);
520 mutex_lock(&id_priv->qp_mutex);
521 ib_destroy_qp(id_priv->id.qp);
522 id_priv->id.qp = NULL;
523 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700524}
525EXPORT_SYMBOL(rdma_destroy_qp);
526
Sean Hefty5851bb82008-01-04 10:47:12 -0800527static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
528 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700529{
530 struct ib_qp_attr qp_attr;
531 int qp_attr_mask, ret;
532
Sean Heftyc5483382007-09-24 13:19:09 -0700533 mutex_lock(&id_priv->qp_mutex);
534 if (!id_priv->id.qp) {
535 ret = 0;
536 goto out;
537 }
Sean Heftye51060f2006-06-17 20:37:29 -0700538
539 /* Need to update QP attributes from default values. */
540 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700541 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700542 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700543 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700544
Sean Heftyc5483382007-09-24 13:19:09 -0700545 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700546 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700547 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700548
549 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700550 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700551 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700552 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700553
Sean Hefty5851bb82008-01-04 10:47:12 -0800554 if (conn_param)
555 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700556 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
557out:
558 mutex_unlock(&id_priv->qp_mutex);
559 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700560}
561
Sean Hefty5851bb82008-01-04 10:47:12 -0800562static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
563 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700564{
565 struct ib_qp_attr qp_attr;
566 int qp_attr_mask, ret;
567
Sean Heftyc5483382007-09-24 13:19:09 -0700568 mutex_lock(&id_priv->qp_mutex);
569 if (!id_priv->id.qp) {
570 ret = 0;
571 goto out;
572 }
Sean Heftye51060f2006-06-17 20:37:29 -0700573
574 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700575 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700576 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700577 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700578
Sean Hefty5851bb82008-01-04 10:47:12 -0800579 if (conn_param)
580 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700581 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
582out:
583 mutex_unlock(&id_priv->qp_mutex);
584 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700585}
586
Sean Heftyc5483382007-09-24 13:19:09 -0700587static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700588{
589 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700590 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700591
Sean Heftyc5483382007-09-24 13:19:09 -0700592 mutex_lock(&id_priv->qp_mutex);
593 if (!id_priv->id.qp) {
594 ret = 0;
595 goto out;
596 }
Sean Heftye51060f2006-06-17 20:37:29 -0700597
598 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700599 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
600out:
601 mutex_unlock(&id_priv->qp_mutex);
602 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700603}
604
Sean Heftyc8f6a362007-02-15 17:00:18 -0800605static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
606 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
607{
608 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
609 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200610 u16 pkey;
611
612 if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
613 IB_LINK_LAYER_INFINIBAND)
614 pkey = ib_addr_get_pkey(dev_addr);
615 else
616 pkey = 0xffff;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800617
618 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200619 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800620 if (ret)
621 return ret;
622
623 qp_attr->port_num = id_priv->id.port_num;
624 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
625
Sean Heftyb26f9b92010-04-01 17:08:41 +0000626 if (id_priv->id.qp_type == IB_QPT_UD) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700627 ret = cma_set_qkey(id_priv);
628 if (ret)
629 return ret;
630
Sean Heftyc8f6a362007-02-15 17:00:18 -0800631 qp_attr->qkey = id_priv->qkey;
632 *qp_attr_mask |= IB_QP_QKEY;
633 } else {
634 qp_attr->qp_access_flags = 0;
635 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
636 }
637 return 0;
638}
639
Sean Heftye51060f2006-06-17 20:37:29 -0700640int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
641 int *qp_attr_mask)
642{
643 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800644 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700645
646 id_priv = container_of(id, struct rdma_id_private, id);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500647 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
648 case RDMA_TRANSPORT_IB:
Sean Heftyb26f9b92010-04-01 17:08:41 +0000649 if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD))
Sean Heftyc8f6a362007-02-15 17:00:18 -0800650 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
651 else
652 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
653 qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700654 if (qp_attr->qp_state == IB_QPS_RTR)
655 qp_attr->rq_psn = id_priv->seq_num;
656 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500657 case RDMA_TRANSPORT_IWARP:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800658 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300659 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800660 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
661 } else
662 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
663 qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500664 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700665 default:
666 ret = -ENOSYS;
667 break;
668 }
669
670 return ret;
671}
672EXPORT_SYMBOL(rdma_init_qp_attr);
673
674static inline int cma_zero_addr(struct sockaddr *addr)
675{
676 struct in6_addr *ip6;
677
678 if (addr->sa_family == AF_INET)
Joe Perches6360a022007-12-16 13:47:33 -0800679 return ipv4_is_zeronet(
680 ((struct sockaddr_in *)addr)->sin_addr.s_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700681 else {
682 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
683 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
Eric Sesterhenn5fd571c2006-06-21 20:56:26 +0200684 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700685 }
686}
687
688static inline int cma_loopback_addr(struct sockaddr *addr)
689{
Aleksey Senin1f5175a2008-12-24 10:16:45 -0800690 if (addr->sa_family == AF_INET)
691 return ipv4_is_loopback(
692 ((struct sockaddr_in *) addr)->sin_addr.s_addr);
693 else
694 return ipv6_addr_loopback(
695 &((struct sockaddr_in6 *) addr)->sin6_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700696}
697
698static inline int cma_any_addr(struct sockaddr *addr)
699{
700 return cma_zero_addr(addr) || cma_loopback_addr(addr);
701}
702
Hefty, Sean43b752d2011-05-09 22:06:10 -0700703static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst)
704{
705 if (src->sa_family != dst->sa_family)
706 return -1;
707
708 switch (src->sa_family) {
709 case AF_INET:
710 return ((struct sockaddr_in *) src)->sin_addr.s_addr !=
711 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
712 default:
713 return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr,
714 &((struct sockaddr_in6 *) dst)->sin6_addr);
715 }
716}
717
Sean Hefty628e5f62006-11-30 16:44:16 -0800718static inline __be16 cma_port(struct sockaddr *addr)
719{
720 if (addr->sa_family == AF_INET)
721 return ((struct sockaddr_in *) addr)->sin_port;
722 else
723 return ((struct sockaddr_in6 *) addr)->sin6_port;
724}
725
Sean Heftye51060f2006-06-17 20:37:29 -0700726static inline int cma_any_port(struct sockaddr *addr)
727{
Sean Hefty628e5f62006-11-30 16:44:16 -0800728 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700729}
730
731static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
Al Viro1b90c132008-03-29 03:10:28 +0000732 u8 *ip_ver, __be16 *port,
Sean Heftye51060f2006-06-17 20:37:29 -0700733 union cma_ip_addr **src, union cma_ip_addr **dst)
734{
735 switch (ps) {
736 case RDMA_PS_SDP:
737 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
738 SDP_MAJ_VERSION)
739 return -EINVAL;
740
741 *ip_ver = sdp_get_ip_ver(hdr);
742 *port = ((struct sdp_hh *) hdr)->port;
743 *src = &((struct sdp_hh *) hdr)->src_addr;
744 *dst = &((struct sdp_hh *) hdr)->dst_addr;
745 break;
746 default:
747 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
748 return -EINVAL;
749
750 *ip_ver = cma_get_ip_ver(hdr);
751 *port = ((struct cma_hdr *) hdr)->port;
752 *src = &((struct cma_hdr *) hdr)->src_addr;
753 *dst = &((struct cma_hdr *) hdr)->dst_addr;
754 break;
755 }
756
757 if (*ip_ver != 4 && *ip_ver != 6)
758 return -EINVAL;
759 return 0;
760}
761
762static void cma_save_net_info(struct rdma_addr *addr,
763 struct rdma_addr *listen_addr,
Al Viro1b90c132008-03-29 03:10:28 +0000764 u8 ip_ver, __be16 port,
Sean Heftye51060f2006-06-17 20:37:29 -0700765 union cma_ip_addr *src, union cma_ip_addr *dst)
766{
767 struct sockaddr_in *listen4, *ip4;
768 struct sockaddr_in6 *listen6, *ip6;
769
770 switch (ip_ver) {
771 case 4:
772 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
773 ip4 = (struct sockaddr_in *) &addr->src_addr;
774 ip4->sin_family = listen4->sin_family;
775 ip4->sin_addr.s_addr = dst->ip4.addr;
776 ip4->sin_port = listen4->sin_port;
777
778 ip4 = (struct sockaddr_in *) &addr->dst_addr;
779 ip4->sin_family = listen4->sin_family;
780 ip4->sin_addr.s_addr = src->ip4.addr;
781 ip4->sin_port = port;
782 break;
783 case 6:
784 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
785 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
786 ip6->sin6_family = listen6->sin6_family;
787 ip6->sin6_addr = dst->ip6;
788 ip6->sin6_port = listen6->sin6_port;
789
790 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
791 ip6->sin6_family = listen6->sin6_family;
792 ip6->sin6_addr = src->ip6;
793 ip6->sin6_port = port;
794 break;
795 default:
796 break;
797 }
798}
799
800static inline int cma_user_data_offset(enum rdma_port_space ps)
801{
802 switch (ps) {
803 case RDMA_PS_SDP:
804 return 0;
805 default:
806 return sizeof(struct cma_hdr);
807 }
808}
809
Sean Heftye51060f2006-06-17 20:37:29 -0700810static void cma_cancel_route(struct rdma_id_private *id_priv)
811{
Eli Cohen3c86aa72010-10-13 21:26:51 +0200812 switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
813 case IB_LINK_LAYER_INFINIBAND:
Sean Heftye51060f2006-06-17 20:37:29 -0700814 if (id_priv->query)
815 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
816 break;
817 default:
818 break;
819 }
820}
821
Sean Heftye51060f2006-06-17 20:37:29 -0700822static void cma_cancel_listens(struct rdma_id_private *id_priv)
823{
824 struct rdma_id_private *dev_id_priv;
825
Sean Heftyd02d1f52007-10-09 11:12:34 -0700826 /*
827 * Remove from listen_any_list to prevent added devices from spawning
828 * additional listen requests.
829 */
Sean Heftye51060f2006-06-17 20:37:29 -0700830 mutex_lock(&lock);
831 list_del(&id_priv->list);
832
833 while (!list_empty(&id_priv->listen_list)) {
834 dev_id_priv = list_entry(id_priv->listen_list.next,
835 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -0700836 /* sync with device removal to avoid duplicate destruction */
837 list_del_init(&dev_id_priv->list);
838 list_del(&dev_id_priv->listen_list);
839 mutex_unlock(&lock);
840
841 rdma_destroy_id(&dev_id_priv->id);
842 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700843 }
844 mutex_unlock(&lock);
845}
846
847static void cma_cancel_operation(struct rdma_id_private *id_priv,
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700848 enum rdma_cm_state state)
Sean Heftye51060f2006-06-17 20:37:29 -0700849{
850 switch (state) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700851 case RDMA_CM_ADDR_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -0700852 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
853 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700854 case RDMA_CM_ROUTE_QUERY:
Sean Heftye51060f2006-06-17 20:37:29 -0700855 cma_cancel_route(id_priv);
856 break;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700857 case RDMA_CM_LISTEN:
Roland Dreier3f446752008-08-04 11:02:14 -0700858 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
859 && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700860 cma_cancel_listens(id_priv);
861 break;
862 default:
863 break;
864 }
865}
866
867static void cma_release_port(struct rdma_id_private *id_priv)
868{
869 struct rdma_bind_list *bind_list = id_priv->bind_list;
870
871 if (!bind_list)
872 return;
873
874 mutex_lock(&lock);
875 hlist_del(&id_priv->node);
876 if (hlist_empty(&bind_list->owners)) {
877 idr_remove(bind_list->ps, bind_list->port);
878 kfree(bind_list);
879 }
880 mutex_unlock(&lock);
881}
882
Sean Heftyc8f6a362007-02-15 17:00:18 -0800883static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
884{
885 struct cma_multicast *mc;
886
887 while (!list_empty(&id_priv->mc_list)) {
888 mc = container_of(id_priv->mc_list.next,
889 struct cma_multicast, list);
890 list_del(&mc->list);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200891 switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
892 case IB_LINK_LAYER_INFINIBAND:
893 ib_sa_free_multicast(mc->multicast.ib);
894 kfree(mc);
895 break;
896 case IB_LINK_LAYER_ETHERNET:
897 kref_put(&mc->mcref, release_mc);
898 break;
899 default:
900 break;
901 }
Sean Heftyc8f6a362007-02-15 17:00:18 -0800902 }
903}
904
Sean Heftye51060f2006-06-17 20:37:29 -0700905void rdma_destroy_id(struct rdma_cm_id *id)
906{
907 struct rdma_id_private *id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700908 enum rdma_cm_state state;
Sean Heftye51060f2006-06-17 20:37:29 -0700909
910 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -0700911 state = cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -0700912 cma_cancel_operation(id_priv, state);
913
Sean Heftya396d432011-02-23 09:05:39 -0800914 /*
915 * Wait for any active callback to finish. New callbacks will find
916 * the id_priv state set to destroying and abort.
917 */
918 mutex_lock(&id_priv->handler_mutex);
919 mutex_unlock(&id_priv->handler_mutex);
920
Sean Heftye51060f2006-06-17 20:37:29 -0700921 if (id_priv->cma_dev) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200922 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500923 case RDMA_TRANSPORT_IB:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +0000924 if (id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -0700925 ib_destroy_cm_id(id_priv->cm_id.ib);
926 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500927 case RDMA_TRANSPORT_IWARP:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +0000928 if (id_priv->cm_id.iw)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500929 iw_destroy_cm_id(id_priv->cm_id.iw);
930 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700931 default:
932 break;
933 }
Sean Heftyc8f6a362007-02-15 17:00:18 -0800934 cma_leave_mc_groups(id_priv);
Sean Heftya396d432011-02-23 09:05:39 -0800935 cma_release_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700936 }
937
938 cma_release_port(id_priv);
939 cma_deref_id(id_priv);
940 wait_for_completion(&id_priv->comp);
941
Sean Heftyd02d1f52007-10-09 11:12:34 -0700942 if (id_priv->internal_id)
943 cma_deref_id(id_priv->id.context);
944
Sean Heftye51060f2006-06-17 20:37:29 -0700945 kfree(id_priv->id.route.path_rec);
946 kfree(id_priv);
947}
948EXPORT_SYMBOL(rdma_destroy_id);
949
950static int cma_rep_recv(struct rdma_id_private *id_priv)
951{
952 int ret;
953
Sean Hefty5851bb82008-01-04 10:47:12 -0800954 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700955 if (ret)
956 goto reject;
957
Sean Hefty5851bb82008-01-04 10:47:12 -0800958 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700959 if (ret)
960 goto reject;
961
962 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
963 if (ret)
964 goto reject;
965
966 return 0;
967reject:
Sean Heftyc5483382007-09-24 13:19:09 -0700968 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700969 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
970 NULL, 0, NULL, 0);
971 return ret;
972}
973
974static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
975{
976 if (id_priv->id.ps == RDMA_PS_SDP &&
977 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
978 SDP_MAJ_VERSION)
979 return -EINVAL;
980
981 return 0;
982}
983
Sean Heftya1b1b612006-11-30 16:33:14 -0800984static void cma_set_rep_event_data(struct rdma_cm_event *event,
985 struct ib_cm_rep_event_param *rep_data,
986 void *private_data)
987{
988 event->param.conn.private_data = private_data;
989 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
990 event->param.conn.responder_resources = rep_data->responder_resources;
991 event->param.conn.initiator_depth = rep_data->initiator_depth;
992 event->param.conn.flow_control = rep_data->flow_control;
993 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
994 event->param.conn.srq = rep_data->srq;
995 event->param.conn.qp_num = rep_data->remote_qpn;
996}
997
Sean Heftye51060f2006-06-17 20:37:29 -0700998static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
999{
1000 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001001 struct rdma_cm_event event;
1002 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001003
Amir Vadai38ca83a2008-07-22 14:14:23 -07001004 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001005 cma_disable_callback(id_priv, RDMA_CM_CONNECT)) ||
Amir Vadai38ca83a2008-07-22 14:14:23 -07001006 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001007 cma_disable_callback(id_priv, RDMA_CM_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -07001008 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -07001009
Sean Heftya1b1b612006-11-30 16:33:14 -08001010 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001011 switch (ib_event->event) {
1012 case IB_CM_REQ_ERROR:
1013 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001014 event.event = RDMA_CM_EVENT_UNREACHABLE;
1015 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001016 break;
1017 case IB_CM_REP_RECEIVED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001018 event.status = cma_verify_rep(id_priv, ib_event->private_data);
1019 if (event.status)
1020 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
Sean Heftye51060f2006-06-17 20:37:29 -07001021 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001022 event.status = cma_rep_recv(id_priv);
1023 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1024 RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001025 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08001026 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1027 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1028 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001029 break;
1030 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001031 case IB_CM_USER_ESTABLISHED:
1032 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001033 break;
1034 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001035 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001036 case IB_CM_DREQ_RECEIVED:
1037 case IB_CM_DREP_RECEIVED:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001038 if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT,
1039 RDMA_CM_DISCONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07001040 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001041 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001042 break;
1043 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001044 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1045 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001046 case IB_CM_MRA_RECEIVED:
1047 /* ignore event */
1048 goto out;
1049 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001050 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001051 event.status = ib_event->param.rej_rcvd.reason;
1052 event.event = RDMA_CM_EVENT_REJECTED;
1053 event.param.conn.private_data = ib_event->private_data;
1054 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001055 break;
1056 default:
Roland Dreier468f2232008-07-14 23:48:47 -07001057 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001058 ib_event->event);
1059 goto out;
1060 }
1061
Sean Heftya1b1b612006-11-30 16:33:14 -08001062 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001063 if (ret) {
1064 /* Destroy the CM ID by returning a non-zero value. */
1065 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001066 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001067 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001068 rdma_destroy_id(&id_priv->id);
1069 return ret;
1070 }
1071out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001072 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001073 return ret;
1074}
1075
Sean Hefty628e5f62006-11-30 16:44:16 -08001076static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1077 struct ib_cm_event *ib_event)
Sean Heftye51060f2006-06-17 20:37:29 -07001078{
1079 struct rdma_id_private *id_priv;
1080 struct rdma_cm_id *id;
1081 struct rdma_route *rt;
1082 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001083 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07001084 u8 ip_ver;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001085 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001086
Sean Heftye51060f2006-06-17 20:37:29 -07001087 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1088 &ip_ver, &port, &src, &dst))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001089 return NULL;
Sean Heftye51060f2006-06-17 20:37:29 -07001090
Krishna Kumar3f168d22006-09-29 12:09:51 -07001091 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001092 listen_id->ps, ib_event->param.req_rcvd.qp_type);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001093 if (IS_ERR(id))
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001094 return NULL;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001095
Sean Heftye51060f2006-06-17 20:37:29 -07001096 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1097 ip_ver, port, src, dst);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001098
1099 rt = &id->route;
1100 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1101 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1102 GFP_KERNEL);
1103 if (!rt->path_rec)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001104 goto err;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001105
Sean Heftye51060f2006-06-17 20:37:29 -07001106 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1107 if (rt->num_paths == 2)
1108 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1109
Sean Hefty6f8372b2009-11-19 13:26:06 -08001110 if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) {
1111 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1112 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1113 ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey);
1114 } else {
1115 ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr,
1116 &rt->addr.dev_addr);
1117 if (ret)
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001118 goto err;
Sean Hefty6f8372b2009-11-19 13:26:06 -08001119 }
1120 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001121
1122 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001123 id_priv->state = RDMA_CM_CONNECT;
Sean Heftye51060f2006-06-17 20:37:29 -07001124 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001125
Krishna Kumar3f168d22006-09-29 12:09:51 -07001126err:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001127 rdma_destroy_id(id);
Sean Heftye51060f2006-06-17 20:37:29 -07001128 return NULL;
1129}
1130
Sean Hefty628e5f62006-11-30 16:44:16 -08001131static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1132 struct ib_cm_event *ib_event)
1133{
1134 struct rdma_id_private *id_priv;
1135 struct rdma_cm_id *id;
1136 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001137 __be16 port;
Sean Hefty628e5f62006-11-30 16:44:16 -08001138 u8 ip_ver;
1139 int ret;
1140
1141 id = rdma_create_id(listen_id->event_handler, listen_id->context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001142 listen_id->ps, IB_QPT_UD);
Sean Hefty628e5f62006-11-30 16:44:16 -08001143 if (IS_ERR(id))
1144 return NULL;
1145
1146
1147 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1148 &ip_ver, &port, &src, &dst))
1149 goto err;
1150
1151 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1152 ip_ver, port, src, dst);
1153
Sean Hefty6f8372b2009-11-19 13:26:06 -08001154 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
1155 ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
1156 &id->route.addr.dev_addr);
1157 if (ret)
1158 goto err;
1159 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001160
1161 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001162 id_priv->state = RDMA_CM_CONNECT;
Sean Hefty628e5f62006-11-30 16:44:16 -08001163 return id_priv;
1164err:
1165 rdma_destroy_id(id);
1166 return NULL;
1167}
1168
Sean Heftya1b1b612006-11-30 16:33:14 -08001169static void cma_set_req_event_data(struct rdma_cm_event *event,
1170 struct ib_cm_req_event_param *req_data,
1171 void *private_data, int offset)
1172{
1173 event->param.conn.private_data = private_data + offset;
1174 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1175 event->param.conn.responder_resources = req_data->responder_resources;
1176 event->param.conn.initiator_depth = req_data->initiator_depth;
1177 event->param.conn.flow_control = req_data->flow_control;
1178 event->param.conn.retry_count = req_data->retry_count;
1179 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1180 event->param.conn.srq = req_data->srq;
1181 event->param.conn.qp_num = req_data->remote_qpn;
1182}
1183
Hefty, Sean95954802011-10-06 09:32:33 -07001184static int cma_check_req_qp_type(struct rdma_cm_id *id, struct ib_cm_event *ib_event)
1185{
1186 return (((ib_event->event == IB_CM_REQ_RECEIVED) ||
1187 (ib_event->param.req_rcvd.qp_type == id->qp_type)) ||
1188 ((ib_event->event == IB_CM_SIDR_REQ_RECEIVED) &&
1189 (id->qp_type == IB_QPT_UD)) ||
1190 (!id->qp_type));
1191}
1192
Sean Heftye51060f2006-06-17 20:37:29 -07001193static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1194{
1195 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001196 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001197 int offset, ret;
1198
1199 listen_id = cm_id->context;
Hefty, Sean95954802011-10-06 09:32:33 -07001200 if (!cma_check_req_qp_type(&listen_id->id, ib_event))
1201 return -EINVAL;
1202
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001203 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001204 return -ECONNABORTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001205
Sean Hefty628e5f62006-11-30 16:44:16 -08001206 memset(&event, 0, sizeof event);
1207 offset = cma_user_data_offset(listen_id->id.ps);
1208 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Hefty, Sean95954802011-10-06 09:32:33 -07001209 if (ib_event->event == IB_CM_SIDR_REQ_RECEIVED) {
Sean Hefty628e5f62006-11-30 16:44:16 -08001210 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1211 event.param.ud.private_data = ib_event->private_data + offset;
1212 event.param.ud.private_data_len =
1213 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1214 } else {
1215 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1216 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1217 ib_event->private_data, offset);
1218 }
Sean Heftye51060f2006-06-17 20:37:29 -07001219 if (!conn_id) {
1220 ret = -ENOMEM;
1221 goto out;
1222 }
1223
Or Gerlitzde910bd2008-07-14 23:48:53 -07001224 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001225 ret = cma_acquire_dev(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301226 if (ret)
1227 goto release_conn_id;
Sean Heftye51060f2006-06-17 20:37:29 -07001228
1229 conn_id->cm_id.ib = cm_id;
1230 cm_id->context = conn_id;
1231 cm_id->cm_handler = cma_ib_handler;
1232
Sean Hefty25ae21a2011-02-23 08:11:32 -08001233 /*
1234 * Protect against the user destroying conn_id from another thread
1235 * until we're done accessing it.
1236 */
1237 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001238 ret = conn_id->id.event_handler(&conn_id->id, &event);
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001239 if (!ret) {
Sean Heftyead595a2008-02-13 14:33:53 -08001240 /*
1241 * Acquire mutex to prevent user executing rdma_destroy_id()
1242 * while we're accessing the cm_id.
1243 */
1244 mutex_lock(&lock);
Sean Heftyb26f9b92010-04-01 17:08:41 +00001245 if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD))
Sean Heftyead595a2008-02-13 14:33:53 -08001246 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1247 mutex_unlock(&lock);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001248 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001249 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301250 goto out;
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001251 }
Sean Hefty25ae21a2011-02-23 08:11:32 -08001252 cma_deref_id(conn_id);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301253
1254 /* Destroy the CM ID by returning a non-zero value. */
1255 conn_id->cm_id.ib = NULL;
1256
1257release_conn_id:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001258 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001259 mutex_unlock(&conn_id->handler_mutex);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301260 rdma_destroy_id(&conn_id->id);
1261
Sean Heftye51060f2006-06-17 20:37:29 -07001262out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001263 mutex_unlock(&listen_id->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001264 return ret;
1265}
1266
1267static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1268{
Sean Hefty628e5f62006-11-30 16:44:16 -08001269 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001270}
1271
1272static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1273 struct ib_cm_compare_data *compare)
1274{
1275 struct cma_hdr *cma_data, *cma_mask;
1276 struct sdp_hh *sdp_data, *sdp_mask;
Al Viro1b90c132008-03-29 03:10:28 +00001277 __be32 ip4_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001278 struct in6_addr ip6_addr;
1279
1280 memset(compare, 0, sizeof *compare);
1281 cma_data = (void *) compare->data;
1282 cma_mask = (void *) compare->mask;
1283 sdp_data = (void *) compare->data;
1284 sdp_mask = (void *) compare->mask;
1285
1286 switch (addr->sa_family) {
1287 case AF_INET:
1288 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1289 if (ps == RDMA_PS_SDP) {
1290 sdp_set_ip_ver(sdp_data, 4);
1291 sdp_set_ip_ver(sdp_mask, 0xF);
1292 sdp_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001293 sdp_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001294 } else {
1295 cma_set_ip_ver(cma_data, 4);
1296 cma_set_ip_ver(cma_mask, 0xF);
1297 cma_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001298 cma_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001299 }
1300 break;
1301 case AF_INET6:
1302 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1303 if (ps == RDMA_PS_SDP) {
1304 sdp_set_ip_ver(sdp_data, 6);
1305 sdp_set_ip_ver(sdp_mask, 0xF);
1306 sdp_data->dst_addr.ip6 = ip6_addr;
1307 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1308 sizeof sdp_mask->dst_addr.ip6);
1309 } else {
1310 cma_set_ip_ver(cma_data, 6);
1311 cma_set_ip_ver(cma_mask, 0xF);
1312 cma_data->dst_addr.ip6 = ip6_addr;
1313 memset(&cma_mask->dst_addr.ip6, 0xFF,
1314 sizeof cma_mask->dst_addr.ip6);
1315 }
1316 break;
1317 default:
1318 break;
1319 }
1320}
1321
Tom Tucker07ebafb2006-08-03 16:02:42 -05001322static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1323{
1324 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001325 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001326 struct sockaddr_in *sin;
1327 int ret = 0;
1328
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001329 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001330 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001331
Sean Heftybe65f082007-05-07 11:49:12 -07001332 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001333 switch (iw_event->event) {
1334 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001335 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001336 break;
1337 case IW_CM_EVENT_CONNECT_REPLY:
1338 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1339 *sin = iw_event->local_addr;
1340 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1341 *sin = iw_event->remote_addr;
Steve Wise881a0452006-12-15 16:50:17 -06001342 switch (iw_event->status) {
1343 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001344 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301345 event.param.conn.initiator_depth = iw_event->ird;
1346 event.param.conn.responder_resources = iw_event->ord;
Steve Wise881a0452006-12-15 16:50:17 -06001347 break;
1348 case -ECONNRESET:
1349 case -ECONNREFUSED:
1350 event.event = RDMA_CM_EVENT_REJECTED;
1351 break;
1352 case -ETIMEDOUT:
1353 event.event = RDMA_CM_EVENT_UNREACHABLE;
1354 break;
1355 default:
1356 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1357 break;
1358 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001359 break;
1360 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001361 event.event = RDMA_CM_EVENT_ESTABLISHED;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301362 event.param.conn.initiator_depth = iw_event->ird;
1363 event.param.conn.responder_resources = iw_event->ord;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001364 break;
1365 default:
1366 BUG_ON(1);
1367 }
1368
Sean Heftya1b1b612006-11-30 16:33:14 -08001369 event.status = iw_event->status;
1370 event.param.conn.private_data = iw_event->private_data;
1371 event.param.conn.private_data_len = iw_event->private_data_len;
1372 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001373 if (ret) {
1374 /* Destroy the CM ID by returning a non-zero value. */
1375 id_priv->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001376 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001377 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001378 rdma_destroy_id(&id_priv->id);
1379 return ret;
1380 }
1381
Or Gerlitzde910bd2008-07-14 23:48:53 -07001382 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001383 return ret;
1384}
1385
1386static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1387 struct iw_cm_event *iw_event)
1388{
1389 struct rdma_cm_id *new_cm_id;
1390 struct rdma_id_private *listen_id, *conn_id;
1391 struct sockaddr_in *sin;
1392 struct net_device *dev = NULL;
Sean Heftya1b1b612006-11-30 16:33:14 -08001393 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001394 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001395 struct ib_device_attr attr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001396
1397 listen_id = cm_id->context;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001398 if (cma_disable_callback(listen_id, RDMA_CM_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001399 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001400
1401 /* Create a new RDMA id for the new IW CM ID */
1402 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1403 listen_id->id.context,
Sean Heftyb26f9b92010-04-01 17:08:41 +00001404 RDMA_PS_TCP, IB_QPT_RC);
Julia Lawall10f32062008-04-16 21:09:25 -07001405 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001406 ret = -ENOMEM;
1407 goto out;
1408 }
1409 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001410 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001411 conn_id->state = RDMA_CM_CONNECT;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001412
Denis V. Lunev1ab35272008-01-22 22:04:30 -08001413 dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001414 if (!dev) {
1415 ret = -EADDRNOTAVAIL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001416 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001417 rdma_destroy_id(new_cm_id);
1418 goto out;
1419 }
1420 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1421 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001422 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001423 rdma_destroy_id(new_cm_id);
1424 goto out;
1425 }
1426
1427 ret = cma_acquire_dev(conn_id);
1428 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001429 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001430 rdma_destroy_id(new_cm_id);
1431 goto out;
1432 }
1433
1434 conn_id->cm_id.iw = cm_id;
1435 cm_id->context = conn_id;
1436 cm_id->cm_handler = cma_iw_handler;
1437
1438 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1439 *sin = iw_event->local_addr;
1440 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1441 *sin = iw_event->remote_addr;
1442
Steve Wise8d8293c2007-10-29 11:34:05 -05001443 ret = ib_query_device(conn_id->id.device, &attr);
1444 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001445 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001446 rdma_destroy_id(new_cm_id);
1447 goto out;
1448 }
1449
Sean Heftya1b1b612006-11-30 16:33:14 -08001450 memset(&event, 0, sizeof event);
1451 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1452 event.param.conn.private_data = iw_event->private_data;
1453 event.param.conn.private_data_len = iw_event->private_data_len;
Kumar Sanghvi3ebeebc2011-09-25 20:17:43 +05301454 event.param.conn.initiator_depth = iw_event->ird;
1455 event.param.conn.responder_resources = iw_event->ord;
Sean Hefty25ae21a2011-02-23 08:11:32 -08001456
1457 /*
1458 * Protect against the user destroying conn_id from another thread
1459 * until we're done accessing it.
1460 */
1461 atomic_inc(&conn_id->refcount);
Sean Heftya1b1b612006-11-30 16:33:14 -08001462 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001463 if (ret) {
1464 /* User wants to destroy the CM ID */
1465 conn_id->cm_id.iw = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001466 cma_exch(conn_id, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001467 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001468 cma_deref_id(conn_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001469 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001470 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001471 }
1472
Or Gerlitzde910bd2008-07-14 23:48:53 -07001473 mutex_unlock(&conn_id->handler_mutex);
Sean Hefty25ae21a2011-02-23 08:11:32 -08001474 cma_deref_id(conn_id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001475
Tom Tucker07ebafb2006-08-03 16:02:42 -05001476out:
1477 if (dev)
1478 dev_put(dev);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001479 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001480 return ret;
1481}
1482
Sean Heftye51060f2006-06-17 20:37:29 -07001483static int cma_ib_listen(struct rdma_id_private *id_priv)
1484{
1485 struct ib_cm_compare_data compare_data;
1486 struct sockaddr *addr;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001487 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07001488 __be64 svc_id;
1489 int ret;
1490
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001491 id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv);
1492 if (IS_ERR(id))
1493 return PTR_ERR(id);
1494
1495 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07001496
Roland Dreier3f446752008-08-04 11:02:14 -07001497 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001498 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1499 if (cma_any_addr(addr))
1500 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1501 else {
1502 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1503 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1504 }
1505
1506 if (ret) {
1507 ib_destroy_cm_id(id_priv->cm_id.ib);
1508 id_priv->cm_id.ib = NULL;
1509 }
1510
1511 return ret;
1512}
1513
Tom Tucker07ebafb2006-08-03 16:02:42 -05001514static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1515{
1516 int ret;
1517 struct sockaddr_in *sin;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001518 struct iw_cm_id *id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001519
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00001520 id = iw_create_cm_id(id_priv->id.device,
1521 iw_conn_req_handler,
1522 id_priv);
1523 if (IS_ERR(id))
1524 return PTR_ERR(id);
1525
1526 id_priv->cm_id.iw = id;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001527
1528 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1529 id_priv->cm_id.iw->local_addr = *sin;
1530
1531 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1532
1533 if (ret) {
1534 iw_destroy_cm_id(id_priv->cm_id.iw);
1535 id_priv->cm_id.iw = NULL;
1536 }
1537
1538 return ret;
1539}
1540
Sean Heftye51060f2006-06-17 20:37:29 -07001541static int cma_listen_handler(struct rdma_cm_id *id,
1542 struct rdma_cm_event *event)
1543{
1544 struct rdma_id_private *id_priv = id->context;
1545
1546 id->context = id_priv->id.context;
1547 id->event_handler = id_priv->id.event_handler;
1548 return id_priv->id.event_handler(id, event);
1549}
1550
1551static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1552 struct cma_device *cma_dev)
1553{
1554 struct rdma_id_private *dev_id_priv;
1555 struct rdma_cm_id *id;
1556 int ret;
1557
Sean Heftyb26f9b92010-04-01 17:08:41 +00001558 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps,
1559 id_priv->id.qp_type);
Sean Heftye51060f2006-06-17 20:37:29 -07001560 if (IS_ERR(id))
1561 return;
1562
1563 dev_id_priv = container_of(id, struct rdma_id_private, id);
1564
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001565 dev_id_priv->state = RDMA_CM_ADDR_BOUND;
Sean Heftye51060f2006-06-17 20:37:29 -07001566 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
Roland Dreier3f446752008-08-04 11:02:14 -07001567 ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001568
1569 cma_attach_to_dev(dev_id_priv, cma_dev);
1570 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001571 atomic_inc(&id_priv->refcount);
1572 dev_id_priv->internal_id = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07001573
1574 ret = rdma_listen(id, id_priv->backlog);
1575 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07001576 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07001577 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07001578}
1579
1580static void cma_listen_on_all(struct rdma_id_private *id_priv)
1581{
1582 struct cma_device *cma_dev;
1583
1584 mutex_lock(&lock);
1585 list_add_tail(&id_priv->list, &listen_any_list);
1586 list_for_each_entry(cma_dev, &dev_list, list)
1587 cma_listen_on_dev(id_priv, cma_dev);
1588 mutex_unlock(&lock);
1589}
1590
Sean Heftya81c9942007-08-08 15:51:06 -07001591void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1592{
1593 struct rdma_id_private *id_priv;
1594
1595 id_priv = container_of(id, struct rdma_id_private, id);
1596 id_priv->tos = (u8) tos;
1597}
1598EXPORT_SYMBOL(rdma_set_service_type);
1599
Sean Heftye51060f2006-06-17 20:37:29 -07001600static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1601 void *context)
1602{
1603 struct cma_work *work = context;
1604 struct rdma_route *route;
1605
1606 route = &work->id->id.route;
1607
1608 if (!status) {
1609 route->num_paths = 1;
1610 *route->path_rec = *path_rec;
1611 } else {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001612 work->old_state = RDMA_CM_ROUTE_QUERY;
1613 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001614 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07001615 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001616 }
1617
1618 queue_work(cma_wq, &work->work);
1619}
1620
1621static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1622 struct cma_work *work)
1623{
Sean Heftya81c9942007-08-08 15:51:06 -07001624 struct rdma_addr *addr = &id_priv->id.route.addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001625 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07001626 ib_sa_comp_mask comp_mask;
1627 struct sockaddr_in6 *sin6;
Sean Heftye51060f2006-06-17 20:37:29 -07001628
1629 memset(&path_rec, 0, sizeof path_rec);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001630 rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
1631 rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
Sean Heftya81c9942007-08-08 15:51:06 -07001632 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001633 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08001634 path_rec.reversible = 1;
Roland Dreier3f446752008-08-04 11:02:14 -07001635 path_rec.service_id = cma_get_service_id(id_priv->id.ps,
1636 (struct sockaddr *) &addr->dst_addr);
Sean Heftya81c9942007-08-08 15:51:06 -07001637
1638 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1639 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1640 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1641
Roland Dreier3f446752008-08-04 11:02:14 -07001642 if (addr->src_addr.ss_family == AF_INET) {
Sean Heftya81c9942007-08-08 15:51:06 -07001643 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1644 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1645 } else {
1646 sin6 = (struct sockaddr_in6 *) &addr->src_addr;
1647 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1648 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1649 }
Sean Heftye51060f2006-06-17 20:37:29 -07001650
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001651 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07001652 id_priv->id.port_num, &path_rec,
1653 comp_mask, timeout_ms,
1654 GFP_KERNEL, cma_query_handler,
1655 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001656
1657 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1658}
1659
David Howellsc4028952006-11-22 14:57:56 +00001660static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07001661{
David Howellsc4028952006-11-22 14:57:56 +00001662 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07001663 struct rdma_id_private *id_priv = work->id;
1664 int destroy = 0;
1665
Or Gerlitzde910bd2008-07-14 23:48:53 -07001666 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001667 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1668 goto out;
1669
1670 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001671 cma_exch(id_priv, RDMA_CM_DESTROYING);
Sean Heftye51060f2006-06-17 20:37:29 -07001672 destroy = 1;
1673 }
1674out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001675 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001676 cma_deref_id(id_priv);
1677 if (destroy)
1678 rdma_destroy_id(&id_priv->id);
1679 kfree(work);
1680}
1681
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001682static void cma_ndev_work_handler(struct work_struct *_work)
1683{
1684 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1685 struct rdma_id_private *id_priv = work->id;
1686 int destroy = 0;
1687
1688 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001689 if (id_priv->state == RDMA_CM_DESTROYING ||
1690 id_priv->state == RDMA_CM_DEVICE_REMOVAL)
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001691 goto out;
1692
1693 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001694 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001695 destroy = 1;
1696 }
1697
1698out:
1699 mutex_unlock(&id_priv->handler_mutex);
1700 cma_deref_id(id_priv);
1701 if (destroy)
1702 rdma_destroy_id(&id_priv->id);
1703 kfree(work);
1704}
1705
Sean Heftye51060f2006-06-17 20:37:29 -07001706static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1707{
1708 struct rdma_route *route = &id_priv->id.route;
1709 struct cma_work *work;
1710 int ret;
1711
1712 work = kzalloc(sizeof *work, GFP_KERNEL);
1713 if (!work)
1714 return -ENOMEM;
1715
1716 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001717 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001718 work->old_state = RDMA_CM_ROUTE_QUERY;
1719 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001720 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1721
1722 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1723 if (!route->path_rec) {
1724 ret = -ENOMEM;
1725 goto err1;
1726 }
1727
1728 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1729 if (ret)
1730 goto err2;
1731
1732 return 0;
1733err2:
1734 kfree(route->path_rec);
1735 route->path_rec = NULL;
1736err1:
1737 kfree(work);
1738 return ret;
1739}
1740
1741int rdma_set_ib_paths(struct rdma_cm_id *id,
1742 struct ib_sa_path_rec *path_rec, int num_paths)
1743{
1744 struct rdma_id_private *id_priv;
1745 int ret;
1746
1747 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001748 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1749 RDMA_CM_ROUTE_RESOLVED))
Sean Heftye51060f2006-06-17 20:37:29 -07001750 return -EINVAL;
1751
Julia Lawall9893e742010-05-15 23:22:38 +02001752 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1753 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07001754 if (!id->route.path_rec) {
1755 ret = -ENOMEM;
1756 goto err;
1757 }
1758
Sean Heftyae2d9292010-03-25 19:12:36 +00001759 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07001760 return 0;
1761err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001762 cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07001763 return ret;
1764}
1765EXPORT_SYMBOL(rdma_set_ib_paths);
1766
Tom Tucker07ebafb2006-08-03 16:02:42 -05001767static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1768{
1769 struct cma_work *work;
1770
1771 work = kzalloc(sizeof *work, GFP_KERNEL);
1772 if (!work)
1773 return -ENOMEM;
1774
1775 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001776 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001777 work->old_state = RDMA_CM_ROUTE_QUERY;
1778 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001779 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1780 queue_work(cma_wq, &work->work);
1781 return 0;
1782}
1783
Eli Cohen3c86aa72010-10-13 21:26:51 +02001784static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1785{
1786 struct rdma_route *route = &id_priv->id.route;
1787 struct rdma_addr *addr = &route->addr;
1788 struct cma_work *work;
1789 int ret;
1790 struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr;
1791 struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr;
1792 struct net_device *ndev = NULL;
Eli Cohenaf7bd462010-08-26 17:18:59 +03001793 u16 vid;
Eli Cohen3c86aa72010-10-13 21:26:51 +02001794
1795 if (src_addr->sin_family != dst_addr->sin_family)
1796 return -EINVAL;
1797
1798 work = kzalloc(sizeof *work, GFP_KERNEL);
1799 if (!work)
1800 return -ENOMEM;
1801
1802 work->id = id_priv;
1803 INIT_WORK(&work->work, cma_work_handler);
1804
1805 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1806 if (!route->path_rec) {
1807 ret = -ENOMEM;
1808 goto err1;
1809 }
1810
1811 route->num_paths = 1;
1812
Eli Cohen3c86aa72010-10-13 21:26:51 +02001813 if (addr->dev_addr.bound_dev_if)
1814 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1815 if (!ndev) {
1816 ret = -ENODEV;
1817 goto err2;
1818 }
1819
Eli Cohenaf7bd462010-08-26 17:18:59 +03001820 vid = rdma_vlan_dev_vlan_id(ndev);
1821
1822 iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1823 iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1824
1825 route->path_rec->hop_limit = 1;
1826 route->path_rec->reversible = 1;
1827 route->path_rec->pkey = cpu_to_be16(0xffff);
1828 route->path_rec->mtu_selector = IB_SA_EQ;
1829 route->path_rec->sl = id_priv->tos >> 5;
1830
Eli Cohen3c86aa72010-10-13 21:26:51 +02001831 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1832 route->path_rec->rate_selector = IB_SA_EQ;
1833 route->path_rec->rate = iboe_get_rate(ndev);
1834 dev_put(ndev);
1835 route->path_rec->packet_life_time_selector = IB_SA_EQ;
1836 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1837 if (!route->path_rec->mtu) {
1838 ret = -EINVAL;
1839 goto err2;
1840 }
1841
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001842 work->old_state = RDMA_CM_ROUTE_QUERY;
1843 work->new_state = RDMA_CM_ROUTE_RESOLVED;
Eli Cohen3c86aa72010-10-13 21:26:51 +02001844 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1845 work->event.status = 0;
1846
1847 queue_work(cma_wq, &work->work);
1848
1849 return 0;
1850
1851err2:
1852 kfree(route->path_rec);
1853 route->path_rec = NULL;
1854err1:
1855 kfree(work);
1856 return ret;
1857}
1858
Sean Heftye51060f2006-06-17 20:37:29 -07001859int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1860{
1861 struct rdma_id_private *id_priv;
1862 int ret;
1863
1864 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001865 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07001866 return -EINVAL;
1867
1868 atomic_inc(&id_priv->refcount);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001869 switch (rdma_node_get_transport(id->device->node_type)) {
1870 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02001871 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1872 case IB_LINK_LAYER_INFINIBAND:
1873 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1874 break;
1875 case IB_LINK_LAYER_ETHERNET:
1876 ret = cma_resolve_iboe_route(id_priv);
1877 break;
1878 default:
1879 ret = -ENOSYS;
1880 }
Sean Heftye51060f2006-06-17 20:37:29 -07001881 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001882 case RDMA_TRANSPORT_IWARP:
1883 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1884 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001885 default:
1886 ret = -ENOSYS;
1887 break;
1888 }
1889 if (ret)
1890 goto err;
1891
1892 return 0;
1893err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001894 cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07001895 cma_deref_id(id_priv);
1896 return ret;
1897}
1898EXPORT_SYMBOL(rdma_resolve_route);
1899
1900static int cma_bind_loopback(struct rdma_id_private *id_priv)
1901{
1902 struct cma_device *cma_dev;
1903 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001904 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001905 u16 pkey;
1906 int ret;
1907 u8 p;
1908
1909 mutex_lock(&lock);
Krishna Kumare82153b2006-10-16 10:09:01 +05301910 if (list_empty(&dev_list)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001911 ret = -ENODEV;
1912 goto out;
1913 }
Krishna Kumare82153b2006-10-16 10:09:01 +05301914 list_for_each_entry(cma_dev, &dev_list, list)
1915 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1916 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1917 port_attr.state == IB_PORT_ACTIVE)
1918 goto port_found;
1919
1920 p = 1;
1921 cma_dev = list_entry(dev_list.next, struct cma_device, list);
Sean Heftye51060f2006-06-17 20:37:29 -07001922
1923port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001924 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001925 if (ret)
1926 goto out;
1927
1928 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1929 if (ret)
1930 goto out;
1931
Sean Hefty6f8372b2009-11-19 13:26:06 -08001932 id_priv->id.route.addr.dev_addr.dev_type =
Eli Cohen3c86aa72010-10-13 21:26:51 +02001933 (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08001934 ARPHRD_INFINIBAND : ARPHRD_ETHER;
1935
1936 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001937 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1938 id_priv->id.port_num = p;
1939 cma_attach_to_dev(id_priv, cma_dev);
1940out:
1941 mutex_unlock(&lock);
1942 return ret;
1943}
1944
1945static void addr_handler(int status, struct sockaddr *src_addr,
1946 struct rdma_dev_addr *dev_addr, void *context)
1947{
1948 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001949 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001950
Sean Heftya1b1b612006-11-30 16:33:14 -08001951 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001952 mutex_lock(&id_priv->handler_mutex);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001953 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY,
1954 RDMA_CM_ADDR_RESOLVED))
Sean Hefty61a73c72006-09-01 15:33:55 -07001955 goto out;
Sean Hefty61a73c72006-09-01 15:33:55 -07001956
1957 if (!status && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001958 status = cma_acquire_dev(id_priv);
1959
1960 if (status) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001961 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED,
1962 RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07001963 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001964 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1965 event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001966 } else {
Sean Heftye51060f2006-06-17 20:37:29 -07001967 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1968 ip_addr_size(src_addr));
Sean Heftya1b1b612006-11-30 16:33:14 -08001969 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001970 }
1971
Sean Heftya1b1b612006-11-30 16:33:14 -08001972 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07001973 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001974 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001975 cma_deref_id(id_priv);
1976 rdma_destroy_id(&id_priv->id);
1977 return;
1978 }
1979out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001980 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001981 cma_deref_id(id_priv);
1982}
1983
1984static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1985{
1986 struct cma_work *work;
Sean Hefty6f8372b2009-11-19 13:26:06 -08001987 struct sockaddr *src, *dst;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001988 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001989 int ret;
1990
1991 work = kzalloc(sizeof *work, GFP_KERNEL);
1992 if (!work)
1993 return -ENOMEM;
1994
1995 if (!id_priv->cma_dev) {
1996 ret = cma_bind_loopback(id_priv);
1997 if (ret)
1998 goto err;
1999 }
2000
Sean Hefty6f8372b2009-11-19 13:26:06 -08002001 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2002 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002003
Sean Hefty6f8372b2009-11-19 13:26:06 -08002004 src = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
2005 if (cma_zero_addr(src)) {
2006 dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
2007 if ((src->sa_family = dst->sa_family) == AF_INET) {
2008 ((struct sockaddr_in *) src)->sin_addr.s_addr =
2009 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
2010 } else {
2011 ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr,
2012 &((struct sockaddr_in6 *) dst)->sin6_addr);
2013 }
Sean Heftye51060f2006-06-17 20:37:29 -07002014 }
2015
2016 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002017 INIT_WORK(&work->work, cma_work_handler);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002018 work->old_state = RDMA_CM_ADDR_QUERY;
2019 work->new_state = RDMA_CM_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07002020 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2021 queue_work(cma_wq, &work->work);
2022 return 0;
2023err:
2024 kfree(work);
2025 return ret;
2026}
2027
2028static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2029 struct sockaddr *dst_addr)
2030{
Sean Heftyd14714d2009-11-19 16:46:25 -08002031 if (!src_addr || !src_addr->sa_family) {
2032 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2033 if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) {
2034 ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2035 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
2036 }
2037 }
2038 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002039}
2040
2041int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2042 struct sockaddr *dst_addr, int timeout_ms)
2043{
2044 struct rdma_id_private *id_priv;
2045 int ret;
2046
2047 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002048 if (id_priv->state == RDMA_CM_IDLE) {
Sean Heftye51060f2006-06-17 20:37:29 -07002049 ret = cma_bind_addr(id, src_addr, dst_addr);
2050 if (ret)
2051 return ret;
2052 }
2053
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002054 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY))
Sean Heftye51060f2006-06-17 20:37:29 -07002055 return -EINVAL;
2056
2057 atomic_inc(&id_priv->refcount);
2058 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
2059 if (cma_any_addr(dst_addr))
2060 ret = cma_resolve_loopback(id_priv);
2061 else
Roland Dreier3f446752008-08-04 11:02:14 -07002062 ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
Sean Hefty7a118df2006-10-31 11:12:59 -08002063 dst_addr, &id->route.addr.dev_addr,
Sean Heftye51060f2006-06-17 20:37:29 -07002064 timeout_ms, addr_handler, id_priv);
2065 if (ret)
2066 goto err;
2067
2068 return 0;
2069err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002070 cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND);
Sean Heftye51060f2006-06-17 20:37:29 -07002071 cma_deref_id(id_priv);
2072 return ret;
2073}
2074EXPORT_SYMBOL(rdma_resolve_addr);
2075
Hefty, Seana9bb7912011-05-09 22:06:10 -07002076int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse)
2077{
2078 struct rdma_id_private *id_priv;
2079 unsigned long flags;
2080 int ret;
2081
2082 id_priv = container_of(id, struct rdma_id_private, id);
2083 spin_lock_irqsave(&id_priv->lock, flags);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002084 if (id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002085 id_priv->reuseaddr = reuse;
2086 ret = 0;
2087 } else {
2088 ret = -EINVAL;
2089 }
2090 spin_unlock_irqrestore(&id_priv->lock, flags);
2091 return ret;
2092}
2093EXPORT_SYMBOL(rdma_set_reuseaddr);
2094
Sean Heftye51060f2006-06-17 20:37:29 -07002095static void cma_bind_port(struct rdma_bind_list *bind_list,
2096 struct rdma_id_private *id_priv)
2097{
2098 struct sockaddr_in *sin;
2099
2100 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2101 sin->sin_port = htons(bind_list->port);
2102 id_priv->bind_list = bind_list;
2103 hlist_add_head(&id_priv->node, &bind_list->owners);
2104}
2105
2106static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2107 unsigned short snum)
2108{
2109 struct rdma_bind_list *bind_list;
Sean Heftyaedec082007-01-29 16:41:23 -08002110 int port, ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002111
Sean Heftycb164b82007-03-05 12:50:17 -08002112 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002113 if (!bind_list)
2114 return -ENOMEM;
2115
Sean Heftye51060f2006-06-17 20:37:29 -07002116 do {
Sean Heftyaedec082007-01-29 16:41:23 -08002117 ret = idr_get_new_above(ps, bind_list, snum, &port);
Sean Heftye51060f2006-06-17 20:37:29 -07002118 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
2119
2120 if (ret)
Sean Heftyaedec082007-01-29 16:41:23 -08002121 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002122
Sean Heftyaedec082007-01-29 16:41:23 -08002123 if (port != snum) {
Sean Heftye51060f2006-06-17 20:37:29 -07002124 ret = -EADDRNOTAVAIL;
Sean Heftyaedec082007-01-29 16:41:23 -08002125 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002126 }
2127
2128 bind_list->ps = ps;
2129 bind_list->port = (unsigned short) port;
2130 cma_bind_port(bind_list, id_priv);
2131 return 0;
Sean Heftyaedec082007-01-29 16:41:23 -08002132err2:
2133 idr_remove(ps, port);
2134err1:
2135 kfree(bind_list);
2136 return ret;
2137}
2138
2139static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2140{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002141 static unsigned int last_used_port;
2142 int low, high, remaining;
2143 unsigned int rover;
Sean Heftyaedec082007-01-29 16:41:23 -08002144
Stephen Hemminger227b60f2007-10-10 17:30:46 -07002145 inet_get_local_port_range(&low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002146 remaining = (high - low) + 1;
2147 rover = net_random() % remaining + low;
2148retry:
2149 if (last_used_port != rover &&
2150 !idr_find(ps, (unsigned short) rover)) {
2151 int ret = cma_alloc_port(ps, id_priv, rover);
2152 /*
2153 * Remember previously used port number in order to avoid
2154 * re-using same port immediately after it is closed.
2155 */
2156 if (!ret)
2157 last_used_port = rover;
2158 if (ret != -EADDRNOTAVAIL)
2159 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002160 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002161 if (--remaining) {
2162 rover++;
2163 if ((rover < low) || (rover > high))
2164 rover = low;
2165 goto retry;
2166 }
2167 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002168}
2169
Hefty, Seana9bb7912011-05-09 22:06:10 -07002170/*
2171 * Check that the requested port is available. This is called when trying to
2172 * bind to a specific port, or when trying to listen on a bound port. In
2173 * the latter case, the provided id_priv may already be on the bind_list, but
2174 * we still need to check that it's okay to start listening.
2175 */
2176static int cma_check_port(struct rdma_bind_list *bind_list,
2177 struct rdma_id_private *id_priv, uint8_t reuseaddr)
Sean Heftye51060f2006-06-17 20:37:29 -07002178{
2179 struct rdma_id_private *cur_id;
Hefty, Sean43b752d2011-05-09 22:06:10 -07002180 struct sockaddr *addr, *cur_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07002181 struct hlist_node *node;
Sean Heftye51060f2006-06-17 20:37:29 -07002182
Hefty, Sean43b752d2011-05-09 22:06:10 -07002183 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
Hefty, Seana9bb7912011-05-09 22:06:10 -07002184 if (cma_any_addr(addr) && !reuseaddr)
2185 return -EADDRNOTAVAIL;
2186
2187 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
2188 if (id_priv == cur_id)
2189 continue;
2190
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002191 if ((cur_id->state == RDMA_CM_LISTEN) ||
Hefty, Seana9bb7912011-05-09 22:06:10 -07002192 !reuseaddr || !cur_id->reuseaddr) {
2193 cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr;
2194 if (cma_any_addr(cur_addr))
2195 return -EADDRNOTAVAIL;
2196
2197 if (!cma_addr_cmp(addr, cur_addr))
2198 return -EADDRINUSE;
2199 }
2200 }
2201 return 0;
2202}
2203
2204static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2205{
2206 struct rdma_bind_list *bind_list;
2207 unsigned short snum;
2208 int ret;
2209
2210 snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07002211 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2212 return -EACCES;
2213
2214 bind_list = idr_find(ps, snum);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002215 if (!bind_list) {
2216 ret = cma_alloc_port(ps, id_priv, snum);
2217 } else {
2218 ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr);
2219 if (!ret)
2220 cma_bind_port(bind_list, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002221 }
Hefty, Seana9bb7912011-05-09 22:06:10 -07002222 return ret;
2223}
Sean Heftye51060f2006-06-17 20:37:29 -07002224
Hefty, Seana9bb7912011-05-09 22:06:10 -07002225static int cma_bind_listen(struct rdma_id_private *id_priv)
2226{
2227 struct rdma_bind_list *bind_list = id_priv->bind_list;
2228 int ret = 0;
2229
2230 mutex_lock(&lock);
2231 if (bind_list->owners.first->next)
2232 ret = cma_check_port(bind_list, id_priv, 0);
2233 mutex_unlock(&lock);
2234 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002235}
2236
2237static int cma_get_port(struct rdma_id_private *id_priv)
2238{
2239 struct idr *ps;
2240 int ret;
2241
2242 switch (id_priv->id.ps) {
2243 case RDMA_PS_SDP:
2244 ps = &sdp_ps;
2245 break;
2246 case RDMA_PS_TCP:
2247 ps = &tcp_ps;
2248 break;
Sean Hefty628e5f62006-11-30 16:44:16 -08002249 case RDMA_PS_UDP:
2250 ps = &udp_ps;
2251 break;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002252 case RDMA_PS_IPOIB:
2253 ps = &ipoib_ps;
2254 break;
Sean Hefty2d2e9412011-05-28 21:56:39 -07002255 case RDMA_PS_IB:
2256 ps = &ib_ps;
2257 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002258 default:
2259 return -EPROTONOSUPPORT;
2260 }
2261
2262 mutex_lock(&lock);
Roland Dreier3f446752008-08-04 11:02:14 -07002263 if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
Sean Heftyaedec082007-01-29 16:41:23 -08002264 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002265 else
2266 ret = cma_use_port(ps, id_priv);
2267 mutex_unlock(&lock);
2268
2269 return ret;
2270}
2271
Sean Heftyd14714d2009-11-19 16:46:25 -08002272static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2273 struct sockaddr *addr)
2274{
Robert P. J. Dayfd4582a2010-01-06 13:16:30 -08002275#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Sean Heftyd14714d2009-11-19 16:46:25 -08002276 struct sockaddr_in6 *sin6;
2277
2278 if (addr->sa_family != AF_INET6)
2279 return 0;
2280
2281 sin6 = (struct sockaddr_in6 *) addr;
2282 if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2283 !sin6->sin6_scope_id)
2284 return -EINVAL;
2285
2286 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2287#endif
2288 return 0;
2289}
2290
Hefty, Seana9bb7912011-05-09 22:06:10 -07002291int rdma_listen(struct rdma_cm_id *id, int backlog)
2292{
2293 struct rdma_id_private *id_priv;
2294 int ret;
2295
2296 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002297 if (id_priv->state == RDMA_CM_IDLE) {
Hefty, Seana9bb7912011-05-09 22:06:10 -07002298 ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
2299 ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
2300 if (ret)
2301 return ret;
2302 }
2303
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002304 if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN))
Hefty, Seana9bb7912011-05-09 22:06:10 -07002305 return -EINVAL;
2306
2307 if (id_priv->reuseaddr) {
2308 ret = cma_bind_listen(id_priv);
2309 if (ret)
2310 goto err;
2311 }
2312
2313 id_priv->backlog = backlog;
2314 if (id->device) {
2315 switch (rdma_node_get_transport(id->device->node_type)) {
2316 case RDMA_TRANSPORT_IB:
2317 ret = cma_ib_listen(id_priv);
2318 if (ret)
2319 goto err;
2320 break;
2321 case RDMA_TRANSPORT_IWARP:
2322 ret = cma_iw_listen(id_priv, backlog);
2323 if (ret)
2324 goto err;
2325 break;
2326 default:
2327 ret = -ENOSYS;
2328 goto err;
2329 }
2330 } else
2331 cma_listen_on_all(id_priv);
2332
2333 return 0;
2334err:
2335 id_priv->backlog = 0;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002336 cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND);
Hefty, Seana9bb7912011-05-09 22:06:10 -07002337 return ret;
2338}
2339EXPORT_SYMBOL(rdma_listen);
2340
Sean Heftye51060f2006-06-17 20:37:29 -07002341int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2342{
2343 struct rdma_id_private *id_priv;
2344 int ret;
2345
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002346 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Sean Heftye51060f2006-06-17 20:37:29 -07002347 return -EAFNOSUPPORT;
2348
2349 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002350 if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07002351 return -EINVAL;
2352
Sean Heftyd14714d2009-11-19 16:46:25 -08002353 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2354 if (ret)
2355 goto err1;
2356
Sean Hefty8523c042010-02-08 16:41:15 -08002357 if (!cma_any_addr(addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002358 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002359 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002360 goto err1;
2361
Krishna Kumar255d0c12006-10-24 13:22:28 -07002362 ret = cma_acquire_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002363 if (ret)
2364 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002365 }
2366
2367 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2368 ret = cma_get_port(id_priv);
2369 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002370 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002371
2372 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07002373err2:
Sean Heftya396d432011-02-23 09:05:39 -08002374 if (id_priv->cma_dev)
2375 cma_release_dev(id_priv);
Krishna Kumar255d0c12006-10-24 13:22:28 -07002376err1:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002377 cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE);
Sean Heftye51060f2006-06-17 20:37:29 -07002378 return ret;
2379}
2380EXPORT_SYMBOL(rdma_bind_addr);
2381
2382static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2383 struct rdma_route *route)
2384{
Sean Heftye51060f2006-06-17 20:37:29 -07002385 struct cma_hdr *cma_hdr;
2386 struct sdp_hh *sdp_hdr;
2387
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002388 if (route->addr.src_addr.ss_family == AF_INET) {
2389 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07002390
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002391 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2392 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2393
2394 switch (ps) {
2395 case RDMA_PS_SDP:
2396 sdp_hdr = hdr;
2397 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2398 return -EINVAL;
2399 sdp_set_ip_ver(sdp_hdr, 4);
2400 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2401 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2402 sdp_hdr->port = src4->sin_port;
2403 break;
2404 default:
2405 cma_hdr = hdr;
2406 cma_hdr->cma_version = CMA_VERSION;
2407 cma_set_ip_ver(cma_hdr, 4);
2408 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2409 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2410 cma_hdr->port = src4->sin_port;
2411 break;
2412 }
2413 } else {
2414 struct sockaddr_in6 *src6, *dst6;
2415
2416 src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
2417 dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
2418
2419 switch (ps) {
2420 case RDMA_PS_SDP:
2421 sdp_hdr = hdr;
2422 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2423 return -EINVAL;
2424 sdp_set_ip_ver(sdp_hdr, 6);
2425 sdp_hdr->src_addr.ip6 = src6->sin6_addr;
2426 sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
2427 sdp_hdr->port = src6->sin6_port;
2428 break;
2429 default:
2430 cma_hdr = hdr;
2431 cma_hdr->cma_version = CMA_VERSION;
2432 cma_set_ip_ver(cma_hdr, 6);
2433 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2434 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2435 cma_hdr->port = src6->sin6_port;
2436 break;
2437 }
Sean Heftye51060f2006-06-17 20:37:29 -07002438 }
2439 return 0;
2440}
2441
Sean Hefty628e5f62006-11-30 16:44:16 -08002442static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2443 struct ib_cm_event *ib_event)
2444{
2445 struct rdma_id_private *id_priv = cm_id->context;
2446 struct rdma_cm_event event;
2447 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2448 int ret = 0;
2449
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002450 if (cma_disable_callback(id_priv, RDMA_CM_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07002451 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08002452
Sean Hefty8aa08602007-05-07 11:49:00 -07002453 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08002454 switch (ib_event->event) {
2455 case IB_CM_SIDR_REQ_ERROR:
2456 event.event = RDMA_CM_EVENT_UNREACHABLE;
2457 event.status = -ETIMEDOUT;
2458 break;
2459 case IB_CM_SIDR_REP_RECEIVED:
2460 event.param.ud.private_data = ib_event->private_data;
2461 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2462 if (rep->status != IB_SIDR_SUCCESS) {
2463 event.event = RDMA_CM_EVENT_UNREACHABLE;
2464 event.status = ib_event->param.sidr_rep_rcvd.status;
2465 break;
2466 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002467 ret = cma_set_qkey(id_priv);
2468 if (ret) {
2469 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2470 event.status = -EINVAL;
2471 break;
2472 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08002473 if (id_priv->qkey != rep->qkey) {
Sean Hefty628e5f62006-11-30 16:44:16 -08002474 event.event = RDMA_CM_EVENT_UNREACHABLE;
2475 event.status = -EINVAL;
2476 break;
2477 }
2478 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2479 id_priv->id.route.path_rec,
2480 &event.param.ud.ah_attr);
2481 event.param.ud.qp_num = rep->qpn;
2482 event.param.ud.qkey = rep->qkey;
2483 event.event = RDMA_CM_EVENT_ESTABLISHED;
2484 event.status = 0;
2485 break;
2486 default:
Roland Dreier468f2232008-07-14 23:48:47 -07002487 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08002488 ib_event->event);
2489 goto out;
2490 }
2491
2492 ret = id_priv->id.event_handler(&id_priv->id, &event);
2493 if (ret) {
2494 /* Destroy the CM ID by returning a non-zero value. */
2495 id_priv->cm_id.ib = NULL;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002496 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002497 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002498 rdma_destroy_id(&id_priv->id);
2499 return ret;
2500 }
2501out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002502 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002503 return ret;
2504}
2505
2506static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2507 struct rdma_conn_param *conn_param)
2508{
2509 struct ib_cm_sidr_req_param req;
2510 struct rdma_route *route;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002511 struct ib_cm_id *id;
Sean Hefty628e5f62006-11-30 16:44:16 -08002512 int ret;
2513
2514 req.private_data_len = sizeof(struct cma_hdr) +
2515 conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00002516 if (req.private_data_len < conn_param->private_data_len)
2517 return -EINVAL;
2518
Sean Hefty628e5f62006-11-30 16:44:16 -08002519 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2520 if (!req.private_data)
2521 return -ENOMEM;
2522
2523 if (conn_param->private_data && conn_param->private_data_len)
2524 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2525 conn_param->private_data, conn_param->private_data_len);
2526
2527 route = &id_priv->id.route;
2528 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2529 if (ret)
2530 goto out;
2531
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002532 id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler,
2533 id_priv);
2534 if (IS_ERR(id)) {
2535 ret = PTR_ERR(id);
Sean Hefty628e5f62006-11-30 16:44:16 -08002536 goto out;
2537 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002538 id_priv->cm_id.ib = id;
Sean Hefty628e5f62006-11-30 16:44:16 -08002539
2540 req.path = route->path_rec;
2541 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002542 (struct sockaddr *) &route->addr.dst_addr);
Sean Hefty628e5f62006-11-30 16:44:16 -08002543 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2544 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2545
2546 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2547 if (ret) {
2548 ib_destroy_cm_id(id_priv->cm_id.ib);
2549 id_priv->cm_id.ib = NULL;
2550 }
2551out:
2552 kfree(req.private_data);
2553 return ret;
2554}
2555
Sean Heftye51060f2006-06-17 20:37:29 -07002556static int cma_connect_ib(struct rdma_id_private *id_priv,
2557 struct rdma_conn_param *conn_param)
2558{
2559 struct ib_cm_req_param req;
2560 struct rdma_route *route;
2561 void *private_data;
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002562 struct ib_cm_id *id;
Sean Heftye51060f2006-06-17 20:37:29 -07002563 int offset, ret;
2564
2565 memset(&req, 0, sizeof req);
2566 offset = cma_user_data_offset(id_priv->id.ps);
2567 req.private_data_len = offset + conn_param->private_data_len;
Sean Hefty04ded162011-12-06 21:17:11 +00002568 if (req.private_data_len < conn_param->private_data_len)
2569 return -EINVAL;
2570
Sean Heftye51060f2006-06-17 20:37:29 -07002571 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2572 if (!private_data)
2573 return -ENOMEM;
2574
2575 if (conn_param->private_data && conn_param->private_data_len)
2576 memcpy(private_data + offset, conn_param->private_data,
2577 conn_param->private_data_len);
2578
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002579 id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv);
2580 if (IS_ERR(id)) {
2581 ret = PTR_ERR(id);
Sean Heftye51060f2006-06-17 20:37:29 -07002582 goto out;
2583 }
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002584 id_priv->cm_id.ib = id;
Sean Heftye51060f2006-06-17 20:37:29 -07002585
2586 route = &id_priv->id.route;
2587 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2588 if (ret)
2589 goto out;
2590 req.private_data = private_data;
2591
2592 req.primary_path = &route->path_rec[0];
2593 if (route->num_paths == 2)
2594 req.alternate_path = &route->path_rec[1];
2595
2596 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002597 (struct sockaddr *) &route->addr.dst_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002598 req.qp_num = id_priv->qp_num;
Sean Hefty18c441a2011-05-28 23:26:06 -07002599 req.qp_type = id_priv->id.qp_type;
Sean Heftye51060f2006-06-17 20:37:29 -07002600 req.starting_psn = id_priv->seq_num;
2601 req.responder_resources = conn_param->responder_resources;
2602 req.initiator_depth = conn_param->initiator_depth;
2603 req.flow_control = conn_param->flow_control;
2604 req.retry_count = conn_param->retry_count;
2605 req.rnr_retry_count = conn_param->rnr_retry_count;
2606 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2607 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2608 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2609 req.srq = id_priv->srq ? 1 : 0;
2610
2611 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2612out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002613 if (ret && !IS_ERR(id)) {
2614 ib_destroy_cm_id(id);
Krishna Kumar675a0272006-09-29 11:47:06 -07002615 id_priv->cm_id.ib = NULL;
2616 }
2617
Sean Heftye51060f2006-06-17 20:37:29 -07002618 kfree(private_data);
2619 return ret;
2620}
2621
Tom Tucker07ebafb2006-08-03 16:02:42 -05002622static int cma_connect_iw(struct rdma_id_private *id_priv,
2623 struct rdma_conn_param *conn_param)
2624{
2625 struct iw_cm_id *cm_id;
2626 struct sockaddr_in* sin;
2627 int ret;
2628 struct iw_cm_conn_param iw_param;
2629
2630 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002631 if (IS_ERR(cm_id))
2632 return PTR_ERR(cm_id);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002633
2634 id_priv->cm_id.iw = cm_id;
2635
2636 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2637 cm_id->local_addr = *sin;
2638
2639 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2640 cm_id->remote_addr = *sin;
2641
Sean Hefty5851bb82008-01-04 10:47:12 -08002642 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07002643 if (ret)
2644 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002645
Hefty, Seanf45ee802011-10-06 09:33:04 -07002646 if (conn_param) {
2647 iw_param.ord = conn_param->initiator_depth;
2648 iw_param.ird = conn_param->responder_resources;
2649 iw_param.private_data = conn_param->private_data;
2650 iw_param.private_data_len = conn_param->private_data_len;
2651 iw_param.qpn = id_priv->id.qp ? id_priv->qp_num : conn_param->qp_num;
2652 } else {
2653 memset(&iw_param, 0, sizeof iw_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002654 iw_param.qpn = id_priv->qp_num;
Hefty, Seanf45ee802011-10-06 09:33:04 -07002655 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002656 ret = iw_cm_connect(cm_id, &iw_param);
2657out:
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002658 if (ret) {
Krishna Kumar675a0272006-09-29 11:47:06 -07002659 iw_destroy_cm_id(cm_id);
2660 id_priv->cm_id.iw = NULL;
2661 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002662 return ret;
2663}
2664
Sean Heftye51060f2006-06-17 20:37:29 -07002665int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2666{
2667 struct rdma_id_private *id_priv;
2668 int ret;
2669
2670 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002671 if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07002672 return -EINVAL;
2673
2674 if (!id->qp) {
2675 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002676 id_priv->srq = conn_param->srq;
2677 }
2678
Tom Tucker07ebafb2006-08-03 16:02:42 -05002679 switch (rdma_node_get_transport(id->device->node_type)) {
2680 case RDMA_TRANSPORT_IB:
Sean Heftyb26f9b92010-04-01 17:08:41 +00002681 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08002682 ret = cma_resolve_ib_udp(id_priv, conn_param);
2683 else
2684 ret = cma_connect_ib(id_priv, conn_param);
Sean Heftye51060f2006-06-17 20:37:29 -07002685 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002686 case RDMA_TRANSPORT_IWARP:
2687 ret = cma_connect_iw(id_priv, conn_param);
2688 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002689 default:
2690 ret = -ENOSYS;
2691 break;
2692 }
2693 if (ret)
2694 goto err;
2695
2696 return 0;
2697err:
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002698 cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED);
Sean Heftye51060f2006-06-17 20:37:29 -07002699 return ret;
2700}
2701EXPORT_SYMBOL(rdma_connect);
2702
2703static int cma_accept_ib(struct rdma_id_private *id_priv,
2704 struct rdma_conn_param *conn_param)
2705{
2706 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08002707 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002708
Sean Hefty5851bb82008-01-04 10:47:12 -08002709 ret = cma_modify_qp_rtr(id_priv, conn_param);
2710 if (ret)
2711 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08002712
Sean Hefty5851bb82008-01-04 10:47:12 -08002713 ret = cma_modify_qp_rts(id_priv, conn_param);
2714 if (ret)
2715 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002716
2717 memset(&rep, 0, sizeof rep);
2718 rep.qp_num = id_priv->qp_num;
2719 rep.starting_psn = id_priv->seq_num;
2720 rep.private_data = conn_param->private_data;
2721 rep.private_data_len = conn_param->private_data_len;
2722 rep.responder_resources = conn_param->responder_resources;
2723 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07002724 rep.failover_accepted = 0;
2725 rep.flow_control = conn_param->flow_control;
2726 rep.rnr_retry_count = conn_param->rnr_retry_count;
2727 rep.srq = id_priv->srq ? 1 : 0;
2728
Sean Hefty0fe313b2006-11-30 16:37:15 -08002729 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2730out:
2731 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002732}
2733
Tom Tucker07ebafb2006-08-03 16:02:42 -05002734static int cma_accept_iw(struct rdma_id_private *id_priv,
2735 struct rdma_conn_param *conn_param)
2736{
2737 struct iw_cm_conn_param iw_param;
2738 int ret;
2739
Sean Hefty5851bb82008-01-04 10:47:12 -08002740 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002741 if (ret)
2742 return ret;
2743
2744 iw_param.ord = conn_param->initiator_depth;
2745 iw_param.ird = conn_param->responder_resources;
2746 iw_param.private_data = conn_param->private_data;
2747 iw_param.private_data_len = conn_param->private_data_len;
2748 if (id_priv->id.qp) {
2749 iw_param.qpn = id_priv->qp_num;
2750 } else
2751 iw_param.qpn = conn_param->qp_num;
2752
2753 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2754}
2755
Sean Hefty628e5f62006-11-30 16:44:16 -08002756static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2757 enum ib_cm_sidr_status status,
2758 const void *private_data, int private_data_len)
2759{
2760 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002761 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002762
2763 memset(&rep, 0, sizeof rep);
2764 rep.status = status;
2765 if (status == IB_SIDR_SUCCESS) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002766 ret = cma_set_qkey(id_priv);
2767 if (ret)
2768 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002769 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002770 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08002771 }
2772 rep.private_data = private_data;
2773 rep.private_data_len = private_data_len;
2774
2775 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2776}
2777
Sean Heftye51060f2006-06-17 20:37:29 -07002778int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2779{
2780 struct rdma_id_private *id_priv;
2781 int ret;
2782
2783 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar83e95022011-01-13 13:56:04 +00002784
2785 id_priv->owner = task_pid_nr(current);
2786
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002787 if (!cma_comp(id_priv, RDMA_CM_CONNECT))
Sean Heftye51060f2006-06-17 20:37:29 -07002788 return -EINVAL;
2789
2790 if (!id->qp && conn_param) {
2791 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002792 id_priv->srq = conn_param->srq;
2793 }
2794
Tom Tucker07ebafb2006-08-03 16:02:42 -05002795 switch (rdma_node_get_transport(id->device->node_type)) {
2796 case RDMA_TRANSPORT_IB:
Hefty, Seanf45ee802011-10-06 09:33:04 -07002797 if (id->qp_type == IB_QPT_UD) {
2798 if (conn_param)
2799 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2800 conn_param->private_data,
2801 conn_param->private_data_len);
2802 else
2803 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2804 NULL, 0);
2805 } else {
2806 if (conn_param)
2807 ret = cma_accept_ib(id_priv, conn_param);
2808 else
2809 ret = cma_rep_recv(id_priv);
2810 }
Sean Heftye51060f2006-06-17 20:37:29 -07002811 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002812 case RDMA_TRANSPORT_IWARP:
2813 ret = cma_accept_iw(id_priv, conn_param);
2814 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002815 default:
2816 ret = -ENOSYS;
2817 break;
2818 }
2819
2820 if (ret)
2821 goto reject;
2822
2823 return 0;
2824reject:
Sean Heftyc5483382007-09-24 13:19:09 -07002825 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002826 rdma_reject(id, NULL, 0);
2827 return ret;
2828}
2829EXPORT_SYMBOL(rdma_accept);
2830
Sean Hefty0fe313b2006-11-30 16:37:15 -08002831int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2832{
2833 struct rdma_id_private *id_priv;
2834 int ret;
2835
2836 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002837 if (!id_priv->cm_id.ib)
Sean Hefty0fe313b2006-11-30 16:37:15 -08002838 return -EINVAL;
2839
2840 switch (id->device->node_type) {
2841 case RDMA_NODE_IB_CA:
2842 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2843 break;
2844 default:
2845 ret = 0;
2846 break;
2847 }
2848 return ret;
2849}
2850EXPORT_SYMBOL(rdma_notify);
2851
Sean Heftye51060f2006-06-17 20:37:29 -07002852int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2853 u8 private_data_len)
2854{
2855 struct rdma_id_private *id_priv;
2856 int ret;
2857
2858 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002859 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07002860 return -EINVAL;
2861
Tom Tucker07ebafb2006-08-03 16:02:42 -05002862 switch (rdma_node_get_transport(id->device->node_type)) {
2863 case RDMA_TRANSPORT_IB:
Sean Heftyb26f9b92010-04-01 17:08:41 +00002864 if (id->qp_type == IB_QPT_UD)
Sean Hefty628e5f62006-11-30 16:44:16 -08002865 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2866 private_data, private_data_len);
2867 else
2868 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2869 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2870 0, private_data, private_data_len);
Sean Heftye51060f2006-06-17 20:37:29 -07002871 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002872 case RDMA_TRANSPORT_IWARP:
2873 ret = iw_cm_reject(id_priv->cm_id.iw,
2874 private_data, private_data_len);
2875 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002876 default:
2877 ret = -ENOSYS;
2878 break;
2879 }
2880 return ret;
2881}
2882EXPORT_SYMBOL(rdma_reject);
2883
2884int rdma_disconnect(struct rdma_cm_id *id)
2885{
2886 struct rdma_id_private *id_priv;
2887 int ret;
2888
2889 id_priv = container_of(id, struct rdma_id_private, id);
Jack Morgenstein0c9361f2011-07-17 10:46:47 +00002890 if (!id_priv->cm_id.ib)
Sean Heftye51060f2006-06-17 20:37:29 -07002891 return -EINVAL;
2892
Tom Tucker07ebafb2006-08-03 16:02:42 -05002893 switch (rdma_node_get_transport(id->device->node_type)) {
2894 case RDMA_TRANSPORT_IB:
Sean Heftyc5483382007-09-24 13:19:09 -07002895 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002896 if (ret)
2897 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002898 /* Initiate or respond to a disconnect. */
2899 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2900 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2901 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002902 case RDMA_TRANSPORT_IWARP:
2903 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2904 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002905 default:
Tom Tucker07ebafb2006-08-03 16:02:42 -05002906 ret = -EINVAL;
Sean Heftye51060f2006-06-17 20:37:29 -07002907 break;
2908 }
2909out:
2910 return ret;
2911}
2912EXPORT_SYMBOL(rdma_disconnect);
2913
Sean Heftyc8f6a362007-02-15 17:00:18 -08002914static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2915{
2916 struct rdma_id_private *id_priv;
2917 struct cma_multicast *mc = multicast->context;
2918 struct rdma_cm_event event;
2919 int ret;
2920
2921 id_priv = mc->id_priv;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002922 if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) &&
2923 cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07002924 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002925
Sean Heftyc5483382007-09-24 13:19:09 -07002926 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002927 if (!status && id_priv->id.qp)
2928 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2929 multicast->rec.mlid);
Sean Heftyc5483382007-09-24 13:19:09 -07002930 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002931
2932 memset(&event, 0, sizeof event);
2933 event.status = status;
2934 event.param.ud.private_data = mc->context;
2935 if (!status) {
2936 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2937 ib_init_ah_from_mcmember(id_priv->id.device,
2938 id_priv->id.port_num, &multicast->rec,
2939 &event.param.ud.ah_attr);
2940 event.param.ud.qp_num = 0xFFFFFF;
2941 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2942 } else
2943 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2944
2945 ret = id_priv->id.event_handler(&id_priv->id, &event);
2946 if (ret) {
Nir Muchtar550e5ca2011-05-20 11:46:11 -07002947 cma_exch(id_priv, RDMA_CM_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002948 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002949 rdma_destroy_id(&id_priv->id);
2950 return 0;
2951 }
Sean Hefty8aa08602007-05-07 11:49:00 -07002952
Or Gerlitzde910bd2008-07-14 23:48:53 -07002953 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002954 return 0;
2955}
2956
2957static void cma_set_mgid(struct rdma_id_private *id_priv,
2958 struct sockaddr *addr, union ib_gid *mgid)
2959{
2960 unsigned char mc_map[MAX_ADDR_LEN];
2961 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2962 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2963 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2964
2965 if (cma_any_addr(addr)) {
2966 memset(mgid, 0, sizeof *mgid);
2967 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08002968 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08002969 0xFF10A01B)) {
2970 /* IPv6 address is an SA assigned MGID. */
2971 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08002972 } else if ((addr->sa_family == AF_INET6)) {
2973 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
2974 if (id_priv->id.ps == RDMA_PS_UDP)
2975 mc_map[7] = 0x01; /* Use RDMA CM signature */
2976 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002977 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07002978 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002979 if (id_priv->id.ps == RDMA_PS_UDP)
2980 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08002981 *mgid = *(union ib_gid *) (mc_map + 4);
2982 }
2983}
2984
2985static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2986 struct cma_multicast *mc)
2987{
2988 struct ib_sa_mcmember_rec rec;
2989 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2990 ib_sa_comp_mask comp_mask;
2991 int ret;
2992
2993 ib_addr_get_mgid(dev_addr, &rec.mgid);
2994 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2995 &rec.mgid, &rec);
2996 if (ret)
2997 return ret;
2998
Roland Dreier3f446752008-08-04 11:02:14 -07002999 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003000 if (id_priv->id.ps == RDMA_PS_UDP)
3001 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
Sean Hefty6f8372b2009-11-19 13:26:06 -08003002 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003003 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
3004 rec.join_state = 1;
3005
3006 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
3007 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
3008 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
3009 IB_SA_MCMEMBER_REC_FLOW_LABEL |
3010 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
3011
Yossi Etigin84adeee2009-04-01 13:55:32 -07003012 if (id_priv->id.ps == RDMA_PS_IPOIB)
3013 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
3014 IB_SA_MCMEMBER_REC_RATE_SELECTOR;
3015
Sean Heftyc8f6a362007-02-15 17:00:18 -08003016 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
3017 id_priv->id.port_num, &rec,
3018 comp_mask, GFP_KERNEL,
3019 cma_ib_mc_handler, mc);
3020 if (IS_ERR(mc->multicast.ib))
3021 return PTR_ERR(mc->multicast.ib);
3022
3023 return 0;
3024}
3025
Eli Cohen3c86aa72010-10-13 21:26:51 +02003026static void iboe_mcast_work_handler(struct work_struct *work)
3027{
3028 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
3029 struct cma_multicast *mc = mw->mc;
3030 struct ib_sa_multicast *m = mc->multicast.ib;
3031
3032 mc->multicast.ib->context = mc;
3033 cma_ib_mc_handler(0, m);
3034 kref_put(&mc->mcref, release_mc);
3035 kfree(mw);
3036}
3037
3038static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
3039{
3040 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
3041 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
3042
3043 if (cma_any_addr(addr)) {
3044 memset(mgid, 0, sizeof *mgid);
3045 } else if (addr->sa_family == AF_INET6) {
3046 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
3047 } else {
3048 mgid->raw[0] = 0xff;
3049 mgid->raw[1] = 0x0e;
3050 mgid->raw[2] = 0;
3051 mgid->raw[3] = 0;
3052 mgid->raw[4] = 0;
3053 mgid->raw[5] = 0;
3054 mgid->raw[6] = 0;
3055 mgid->raw[7] = 0;
3056 mgid->raw[8] = 0;
3057 mgid->raw[9] = 0;
3058 mgid->raw[10] = 0xff;
3059 mgid->raw[11] = 0xff;
3060 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
3061 }
3062}
3063
3064static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
3065 struct cma_multicast *mc)
3066{
3067 struct iboe_mcast_work *work;
3068 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
3069 int err;
3070 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
3071 struct net_device *ndev = NULL;
3072
3073 if (cma_zero_addr((struct sockaddr *)&mc->addr))
3074 return -EINVAL;
3075
3076 work = kzalloc(sizeof *work, GFP_KERNEL);
3077 if (!work)
3078 return -ENOMEM;
3079
3080 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
3081 if (!mc->multicast.ib) {
3082 err = -ENOMEM;
3083 goto out1;
3084 }
3085
3086 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
3087
3088 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
3089 if (id_priv->id.ps == RDMA_PS_UDP)
3090 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
3091
3092 if (dev_addr->bound_dev_if)
3093 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
3094 if (!ndev) {
3095 err = -ENODEV;
3096 goto out2;
3097 }
3098 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
3099 mc->multicast.ib->rec.hop_limit = 1;
3100 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
3101 dev_put(ndev);
3102 if (!mc->multicast.ib->rec.mtu) {
3103 err = -EINVAL;
3104 goto out2;
3105 }
3106 iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3107 work->id = id_priv;
3108 work->mc = mc;
3109 INIT_WORK(&work->work, iboe_mcast_work_handler);
3110 kref_get(&mc->mcref);
3111 queue_work(cma_wq, &work->work);
3112
3113 return 0;
3114
3115out2:
3116 kfree(mc->multicast.ib);
3117out1:
3118 kfree(work);
3119 return err;
3120}
3121
Sean Heftyc8f6a362007-02-15 17:00:18 -08003122int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3123 void *context)
3124{
3125 struct rdma_id_private *id_priv;
3126 struct cma_multicast *mc;
3127 int ret;
3128
3129 id_priv = container_of(id, struct rdma_id_private, id);
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003130 if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) &&
3131 !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED))
Sean Heftyc8f6a362007-02-15 17:00:18 -08003132 return -EINVAL;
3133
3134 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3135 if (!mc)
3136 return -ENOMEM;
3137
3138 memcpy(&mc->addr, addr, ip_addr_size(addr));
3139 mc->context = context;
3140 mc->id_priv = id_priv;
3141
3142 spin_lock(&id_priv->lock);
3143 list_add(&mc->list, &id_priv->mc_list);
3144 spin_unlock(&id_priv->lock);
3145
3146 switch (rdma_node_get_transport(id->device->node_type)) {
3147 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02003148 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3149 case IB_LINK_LAYER_INFINIBAND:
3150 ret = cma_join_ib_multicast(id_priv, mc);
3151 break;
3152 case IB_LINK_LAYER_ETHERNET:
3153 kref_init(&mc->mcref);
3154 ret = cma_iboe_join_multicast(id_priv, mc);
3155 break;
3156 default:
3157 ret = -EINVAL;
3158 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003159 break;
3160 default:
3161 ret = -ENOSYS;
3162 break;
3163 }
3164
3165 if (ret) {
3166 spin_lock_irq(&id_priv->lock);
3167 list_del(&mc->list);
3168 spin_unlock_irq(&id_priv->lock);
3169 kfree(mc);
3170 }
3171 return ret;
3172}
3173EXPORT_SYMBOL(rdma_join_multicast);
3174
3175void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3176{
3177 struct rdma_id_private *id_priv;
3178 struct cma_multicast *mc;
3179
3180 id_priv = container_of(id, struct rdma_id_private, id);
3181 spin_lock_irq(&id_priv->lock);
3182 list_for_each_entry(mc, &id_priv->mc_list, list) {
3183 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
3184 list_del(&mc->list);
3185 spin_unlock_irq(&id_priv->lock);
3186
3187 if (id->qp)
3188 ib_detach_mcast(id->qp,
3189 &mc->multicast.ib->rec.mgid,
3190 mc->multicast.ib->rec.mlid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003191 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3192 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3193 case IB_LINK_LAYER_INFINIBAND:
3194 ib_sa_free_multicast(mc->multicast.ib);
3195 kfree(mc);
3196 break;
3197 case IB_LINK_LAYER_ETHERNET:
3198 kref_put(&mc->mcref, release_mc);
3199 break;
3200 default:
3201 break;
3202 }
3203 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003204 return;
3205 }
3206 }
3207 spin_unlock_irq(&id_priv->lock);
3208}
3209EXPORT_SYMBOL(rdma_leave_multicast);
3210
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003211static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3212{
3213 struct rdma_dev_addr *dev_addr;
3214 struct cma_ndev_work *work;
3215
3216 dev_addr = &id_priv->id.route.addr.dev_addr;
3217
Sean Hefty6266ed62009-11-19 12:55:22 -08003218 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003219 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3220 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3221 ndev->name, &id_priv->id);
3222 work = kzalloc(sizeof *work, GFP_KERNEL);
3223 if (!work)
3224 return -ENOMEM;
3225
3226 INIT_WORK(&work->work, cma_ndev_work_handler);
3227 work->id = id_priv;
3228 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3229 atomic_inc(&id_priv->refcount);
3230 queue_work(cma_wq, &work->work);
3231 }
3232
3233 return 0;
3234}
3235
3236static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
3237 void *ctx)
3238{
3239 struct net_device *ndev = (struct net_device *)ctx;
3240 struct cma_device *cma_dev;
3241 struct rdma_id_private *id_priv;
3242 int ret = NOTIFY_DONE;
3243
3244 if (dev_net(ndev) != &init_net)
3245 return NOTIFY_DONE;
3246
3247 if (event != NETDEV_BONDING_FAILOVER)
3248 return NOTIFY_DONE;
3249
3250 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3251 return NOTIFY_DONE;
3252
3253 mutex_lock(&lock);
3254 list_for_each_entry(cma_dev, &dev_list, list)
3255 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3256 ret = cma_netdev_change(ndev, id_priv);
3257 if (ret)
3258 goto out;
3259 }
3260
3261out:
3262 mutex_unlock(&lock);
3263 return ret;
3264}
3265
3266static struct notifier_block cma_nb = {
3267 .notifier_call = cma_netdev_callback
3268};
3269
Sean Heftye51060f2006-06-17 20:37:29 -07003270static void cma_add_one(struct ib_device *device)
3271{
3272 struct cma_device *cma_dev;
3273 struct rdma_id_private *id_priv;
3274
3275 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3276 if (!cma_dev)
3277 return;
3278
3279 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07003280
3281 init_completion(&cma_dev->comp);
3282 atomic_set(&cma_dev->refcount, 1);
3283 INIT_LIST_HEAD(&cma_dev->id_list);
3284 ib_set_client_data(device, &cma_client, cma_dev);
3285
3286 mutex_lock(&lock);
3287 list_add_tail(&cma_dev->list, &dev_list);
3288 list_for_each_entry(id_priv, &listen_any_list, list)
3289 cma_listen_on_dev(id_priv, cma_dev);
3290 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07003291}
3292
3293static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3294{
Sean Heftya1b1b612006-11-30 16:33:14 -08003295 struct rdma_cm_event event;
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003296 enum rdma_cm_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003297 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07003298
3299 /* Record that we want to remove the device */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003300 state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL);
3301 if (state == RDMA_CM_DESTROYING)
Sean Heftye51060f2006-06-17 20:37:29 -07003302 return 0;
3303
3304 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003305 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07003306
3307 /* Check for destruction from another callback. */
Nir Muchtar550e5ca2011-05-20 11:46:11 -07003308 if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07003309 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003310
Sean Heftya1b1b612006-11-30 16:33:14 -08003311 memset(&event, 0, sizeof event);
3312 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003313 ret = id_priv->id.event_handler(&id_priv->id, &event);
3314out:
3315 mutex_unlock(&id_priv->handler_mutex);
3316 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003317}
3318
3319static void cma_process_remove(struct cma_device *cma_dev)
3320{
Sean Heftye51060f2006-06-17 20:37:29 -07003321 struct rdma_id_private *id_priv;
3322 int ret;
3323
Sean Heftye51060f2006-06-17 20:37:29 -07003324 mutex_lock(&lock);
3325 while (!list_empty(&cma_dev->id_list)) {
3326 id_priv = list_entry(cma_dev->id_list.next,
3327 struct rdma_id_private, list);
3328
Sean Heftyd02d1f52007-10-09 11:12:34 -07003329 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07003330 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07003331 atomic_inc(&id_priv->refcount);
3332 mutex_unlock(&lock);
3333
Sean Heftyd02d1f52007-10-09 11:12:34 -07003334 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003335 cma_deref_id(id_priv);
3336 if (ret)
3337 rdma_destroy_id(&id_priv->id);
3338
3339 mutex_lock(&lock);
3340 }
3341 mutex_unlock(&lock);
3342
3343 cma_deref_dev(cma_dev);
3344 wait_for_completion(&cma_dev->comp);
3345}
3346
3347static void cma_remove_one(struct ib_device *device)
3348{
3349 struct cma_device *cma_dev;
3350
3351 cma_dev = ib_get_client_data(device, &cma_client);
3352 if (!cma_dev)
3353 return;
3354
3355 mutex_lock(&lock);
3356 list_del(&cma_dev->list);
3357 mutex_unlock(&lock);
3358
3359 cma_process_remove(cma_dev);
3360 kfree(cma_dev);
3361}
3362
Nir Muchtar753f6182011-01-03 15:33:53 +00003363static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb)
3364{
3365 struct nlmsghdr *nlh;
3366 struct rdma_cm_id_stats *id_stats;
3367 struct rdma_id_private *id_priv;
3368 struct rdma_cm_id *id = NULL;
3369 struct cma_device *cma_dev;
3370 int i_dev = 0, i_id = 0;
3371
3372 /*
3373 * We export all of the IDs as a sequence of messages. Each
3374 * ID gets its own netlink message.
3375 */
3376 mutex_lock(&lock);
3377
3378 list_for_each_entry(cma_dev, &dev_list, list) {
3379 if (i_dev < cb->args[0]) {
3380 i_dev++;
3381 continue;
3382 }
3383
3384 i_id = 0;
3385 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3386 if (i_id < cb->args[1]) {
3387 i_id++;
3388 continue;
3389 }
3390
3391 id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq,
3392 sizeof *id_stats, RDMA_NL_RDMA_CM,
3393 RDMA_NL_RDMA_CM_ID_STATS);
3394 if (!id_stats)
3395 goto out;
3396
3397 memset(id_stats, 0, sizeof *id_stats);
3398 id = &id_priv->id;
3399 id_stats->node_type = id->route.addr.dev_addr.dev_type;
3400 id_stats->port_num = id->port_num;
3401 id_stats->bound_dev_if =
3402 id->route.addr.dev_addr.bound_dev_if;
3403
3404 if (id->route.addr.src_addr.ss_family == AF_INET) {
3405 if (ibnl_put_attr(skb, nlh,
3406 sizeof(struct sockaddr_in),
3407 &id->route.addr.src_addr,
3408 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3409 goto out;
3410 }
3411 if (ibnl_put_attr(skb, nlh,
3412 sizeof(struct sockaddr_in),
3413 &id->route.addr.dst_addr,
3414 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3415 goto out;
3416 }
3417 } else if (id->route.addr.src_addr.ss_family == AF_INET6) {
3418 if (ibnl_put_attr(skb, nlh,
3419 sizeof(struct sockaddr_in6),
3420 &id->route.addr.src_addr,
3421 RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) {
3422 goto out;
3423 }
3424 if (ibnl_put_attr(skb, nlh,
3425 sizeof(struct sockaddr_in6),
3426 &id->route.addr.dst_addr,
3427 RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) {
3428 goto out;
3429 }
3430 }
3431
Nir Muchtar83e95022011-01-13 13:56:04 +00003432 id_stats->pid = id_priv->owner;
Nir Muchtar753f6182011-01-03 15:33:53 +00003433 id_stats->port_space = id->ps;
3434 id_stats->cm_state = id_priv->state;
3435 id_stats->qp_num = id_priv->qp_num;
3436 id_stats->qp_type = id->qp_type;
3437
3438 i_id++;
3439 }
3440
3441 cb->args[1] = 0;
3442 i_dev++;
3443 }
3444
3445out:
3446 mutex_unlock(&lock);
3447 cb->args[0] = i_dev;
3448 cb->args[1] = i_id;
3449
3450 return skb->len;
3451}
3452
3453static const struct ibnl_client_cbs cma_cb_table[] = {
3454 [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats },
3455};
3456
Peter Huewe716abb12009-06-23 10:38:42 -07003457static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003458{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09003459 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003460
Sean Heftyc7f743a2007-02-01 12:23:37 -08003461 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07003462 if (!cma_wq)
3463 return -ENOMEM;
3464
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003465 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08003466 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003467 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003468
Sean Heftye51060f2006-06-17 20:37:29 -07003469 ret = ib_register_client(&cma_client);
3470 if (ret)
3471 goto err;
Nir Muchtar753f6182011-01-03 15:33:53 +00003472
3473 if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table))
3474 printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n");
3475
Sean Heftye51060f2006-06-17 20:37:29 -07003476 return 0;
3477
3478err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003479 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003480 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003481 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003482 destroy_workqueue(cma_wq);
3483 return ret;
3484}
3485
Peter Huewe716abb12009-06-23 10:38:42 -07003486static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003487{
Nir Muchtar753f6182011-01-03 15:33:53 +00003488 ibnl_remove_client(RDMA_NL_RDMA_CM);
Sean Heftye51060f2006-06-17 20:37:29 -07003489 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003490 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003491 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003492 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003493 destroy_workqueue(cma_wq);
3494 idr_destroy(&sdp_ps);
3495 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08003496 idr_destroy(&udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003497 idr_destroy(&ipoib_ps);
Sean Hefty2d2e9412011-05-28 21:56:39 -07003498 idr_destroy(&ib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07003499}
3500
3501module_init(cma_init);
3502module_exit(cma_cleanup);