blob: 075317884b5309f31ee3b8f7fac4821ea5622040 [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>
Sean Heftye51060f2006-06-17 20:37:29 -070043
44#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080045#include <net/ipv6.h>
Sean Heftye51060f2006-06-17 20:37:29 -070046
47#include <rdma/rdma_cm.h>
48#include <rdma/rdma_cm_ib.h>
49#include <rdma/ib_cache.h>
50#include <rdma/ib_cm.h>
51#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050052#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070053
54MODULE_AUTHOR("Sean Hefty");
55MODULE_DESCRIPTION("Generic RDMA CM Agent");
56MODULE_LICENSE("Dual BSD/GPL");
57
58#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030059#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070060#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Sean Heftye51060f2006-06-17 20:37:29 -070061
62static void cma_add_one(struct ib_device *device);
63static void cma_remove_one(struct ib_device *device);
64
65static struct ib_client cma_client = {
66 .name = "cma",
67 .add = cma_add_one,
68 .remove = cma_remove_one
69};
70
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070071static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -080072static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -070073static LIST_HEAD(dev_list);
74static LIST_HEAD(listen_any_list);
75static DEFINE_MUTEX(lock);
76static struct workqueue_struct *cma_wq;
77static DEFINE_IDR(sdp_ps);
78static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -080079static DEFINE_IDR(udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -080080static DEFINE_IDR(ipoib_ps);
Sean Heftyaedec082007-01-29 16:41:23 -080081static int next_port;
Sean Heftye51060f2006-06-17 20:37:29 -070082
83struct cma_device {
84 struct list_head list;
85 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -070086 struct completion comp;
87 atomic_t refcount;
88 struct list_head id_list;
89};
90
91enum cma_state {
92 CMA_IDLE,
93 CMA_ADDR_QUERY,
94 CMA_ADDR_RESOLVED,
95 CMA_ROUTE_QUERY,
96 CMA_ROUTE_RESOLVED,
97 CMA_CONNECT,
98 CMA_DISCONNECT,
99 CMA_ADDR_BOUND,
100 CMA_LISTEN,
101 CMA_DEVICE_REMOVAL,
102 CMA_DESTROYING
103};
104
105struct rdma_bind_list {
106 struct idr *ps;
107 struct hlist_head owners;
108 unsigned short port;
109};
110
111/*
112 * Device removal can occur at anytime, so we need extra handling to
113 * serialize notifying the user of device removal with other callbacks.
114 * We do this by disabling removal notification while a callback is in process,
115 * and reporting it after the callback completes.
116 */
117struct rdma_id_private {
118 struct rdma_cm_id id;
119
120 struct rdma_bind_list *bind_list;
121 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700122 struct list_head list; /* listen_any_list or cma_device.list */
123 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700124 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800125 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700126
Sean Heftyd02d1f52007-10-09 11:12:34 -0700127 int internal_id;
Sean Heftye51060f2006-06-17 20:37:29 -0700128 enum cma_state state;
129 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700130 struct mutex qp_mutex;
131
Sean Heftye51060f2006-06-17 20:37:29 -0700132 struct completion comp;
133 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700134 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700135
136 int backlog;
137 int timeout_ms;
138 struct ib_sa_query *query;
139 int query_id;
140 union {
141 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500142 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700143 } cm_id;
144
145 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800146 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700147 u32 qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700148 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700149 u8 tos;
Sean Heftye51060f2006-06-17 20:37:29 -0700150};
151
Sean Heftyc8f6a362007-02-15 17:00:18 -0800152struct cma_multicast {
153 struct rdma_id_private *id_priv;
154 union {
155 struct ib_sa_multicast *ib;
156 } multicast;
157 struct list_head list;
158 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700159 struct sockaddr_storage addr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800160};
161
Sean Heftye51060f2006-06-17 20:37:29 -0700162struct cma_work {
163 struct work_struct work;
164 struct rdma_id_private *id;
165 enum cma_state old_state;
166 enum cma_state new_state;
167 struct rdma_cm_event event;
168};
169
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700170struct cma_ndev_work {
171 struct work_struct work;
172 struct rdma_id_private *id;
173 struct rdma_cm_event event;
174};
175
Sean Heftye51060f2006-06-17 20:37:29 -0700176union cma_ip_addr {
177 struct in6_addr ip6;
178 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000179 __be32 pad[3];
180 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700181 } ip4;
182};
183
184struct cma_hdr {
185 u8 cma_version;
186 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000187 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700188 union cma_ip_addr src_addr;
189 union cma_ip_addr dst_addr;
190};
191
192struct sdp_hh {
193 u8 bsdh[16];
194 u8 sdp_version; /* Major version: 7:4 */
195 u8 ip_version; /* IP version: 7:4 */
196 u8 sdp_specific1[10];
Al Viro1b90c132008-03-29 03:10:28 +0000197 __be16 port;
198 __be16 sdp_specific2;
Sean Heftye51060f2006-06-17 20:37:29 -0700199 union cma_ip_addr src_addr;
200 union cma_ip_addr dst_addr;
201};
202
203struct sdp_hah {
204 u8 bsdh[16];
205 u8 sdp_version;
206};
207
208#define CMA_VERSION 0x00
209#define SDP_MAJ_VERSION 0x2
210
211static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
212{
213 unsigned long flags;
214 int ret;
215
216 spin_lock_irqsave(&id_priv->lock, flags);
217 ret = (id_priv->state == comp);
218 spin_unlock_irqrestore(&id_priv->lock, flags);
219 return ret;
220}
221
222static int cma_comp_exch(struct rdma_id_private *id_priv,
223 enum cma_state comp, enum cma_state exch)
224{
225 unsigned long flags;
226 int ret;
227
228 spin_lock_irqsave(&id_priv->lock, flags);
229 if ((ret = (id_priv->state == comp)))
230 id_priv->state = exch;
231 spin_unlock_irqrestore(&id_priv->lock, flags);
232 return ret;
233}
234
235static enum cma_state cma_exch(struct rdma_id_private *id_priv,
236 enum cma_state exch)
237{
238 unsigned long flags;
239 enum cma_state old;
240
241 spin_lock_irqsave(&id_priv->lock, flags);
242 old = id_priv->state;
243 id_priv->state = exch;
244 spin_unlock_irqrestore(&id_priv->lock, flags);
245 return old;
246}
247
248static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
249{
250 return hdr->ip_version >> 4;
251}
252
253static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
254{
255 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
256}
257
258static inline u8 sdp_get_majv(u8 sdp_version)
259{
260 return sdp_version >> 4;
261}
262
263static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
264{
265 return hh->ip_version >> 4;
266}
267
268static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
269{
270 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
271}
272
Sean Heftyc8f6a362007-02-15 17:00:18 -0800273static inline int cma_is_ud_ps(enum rdma_port_space ps)
274{
275 return (ps == RDMA_PS_UDP || ps == RDMA_PS_IPOIB);
276}
277
Sean Heftye51060f2006-06-17 20:37:29 -0700278static void cma_attach_to_dev(struct rdma_id_private *id_priv,
279 struct cma_device *cma_dev)
280{
281 atomic_inc(&cma_dev->refcount);
282 id_priv->cma_dev = cma_dev;
283 id_priv->id.device = cma_dev->device;
284 list_add_tail(&id_priv->list, &cma_dev->id_list);
285}
286
287static inline void cma_deref_dev(struct cma_device *cma_dev)
288{
289 if (atomic_dec_and_test(&cma_dev->refcount))
290 complete(&cma_dev->comp);
291}
292
293static void cma_detach_from_dev(struct rdma_id_private *id_priv)
294{
295 list_del(&id_priv->list);
296 cma_deref_dev(id_priv->cma_dev);
297 id_priv->cma_dev = NULL;
298}
299
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700300static int cma_set_qkey(struct rdma_id_private *id_priv)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800301{
302 struct ib_sa_mcmember_rec rec;
303 int ret = 0;
304
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700305 if (id_priv->qkey)
306 return 0;
307
308 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800309 case RDMA_PS_UDP:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700310 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800311 break;
312 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700313 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
314 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
315 id_priv->id.port_num, &rec.mgid,
316 &rec);
317 if (!ret)
318 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800319 break;
320 default:
321 break;
322 }
323 return ret;
324}
325
Tom Tucker07ebafb2006-08-03 16:02:42 -0500326static int cma_acquire_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700327{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800328 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700329 struct cma_device *cma_dev;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -0700330 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -0700331 int ret = -ENODEV;
332
Sean Heftyc8f6a362007-02-15 17:00:18 -0800333 switch (rdma_node_get_transport(dev_addr->dev_type)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500334 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800335 ib_addr_get_sgid(dev_addr, &gid);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500336 break;
337 case RDMA_TRANSPORT_IWARP:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800338 iw_addr_get_sgid(dev_addr, &gid);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500339 break;
340 default:
341 return -ENODEV;
342 }
Sean Hefty61a73c72006-09-01 15:33:55 -0700343
Sean Heftye51060f2006-06-17 20:37:29 -0700344 list_for_each_entry(cma_dev, &dev_list, list) {
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -0700345 ret = ib_find_cached_gid(cma_dev->device, &gid,
Sean Heftye51060f2006-06-17 20:37:29 -0700346 &id_priv->id.port_num, NULL);
347 if (!ret) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700348 cma_attach_to_dev(id_priv, cma_dev);
Sean Heftye51060f2006-06-17 20:37:29 -0700349 break;
350 }
351 }
Sean Heftye51060f2006-06-17 20:37:29 -0700352 return ret;
353}
354
Sean Heftye51060f2006-06-17 20:37:29 -0700355static void cma_deref_id(struct rdma_id_private *id_priv)
356{
357 if (atomic_dec_and_test(&id_priv->refcount))
358 complete(&id_priv->comp);
359}
360
Or Gerlitzde910bd2008-07-14 23:48:53 -0700361static int cma_disable_callback(struct rdma_id_private *id_priv,
Sean Hefty8aa08602007-05-07 11:49:00 -0700362 enum cma_state state)
363{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700364 mutex_lock(&id_priv->handler_mutex);
365 if (id_priv->state != state) {
366 mutex_unlock(&id_priv->handler_mutex);
367 return -EINVAL;
368 }
369 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700370}
371
Sean Hefty6c719f52007-05-07 11:49:27 -0700372static int cma_has_cm_dev(struct rdma_id_private *id_priv)
373{
374 return (id_priv->id.device && id_priv->cm_id.ib);
375}
376
Sean Heftye51060f2006-06-17 20:37:29 -0700377struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
378 void *context, enum rdma_port_space ps)
379{
380 struct rdma_id_private *id_priv;
381
382 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
383 if (!id_priv)
384 return ERR_PTR(-ENOMEM);
385
386 id_priv->state = CMA_IDLE;
387 id_priv->id.context = context;
388 id_priv->id.event_handler = event_handler;
389 id_priv->id.ps = ps;
390 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700391 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700392 init_completion(&id_priv->comp);
393 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700394 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700395 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800396 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700397 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
398
399 return &id_priv->id;
400}
401EXPORT_SYMBOL(rdma_create_id);
402
Sean Heftyc8f6a362007-02-15 17:00:18 -0800403static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700404{
405 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800406 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700407
Sean Heftyc8f6a362007-02-15 17:00:18 -0800408 qp_attr.qp_state = IB_QPS_INIT;
409 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700410 if (ret)
411 return ret;
412
Sean Heftyc8f6a362007-02-15 17:00:18 -0800413 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
414 if (ret)
415 return ret;
416
417 qp_attr.qp_state = IB_QPS_RTR;
418 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
419 if (ret)
420 return ret;
421
422 qp_attr.qp_state = IB_QPS_RTS;
423 qp_attr.sq_psn = 0;
424 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
425
426 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700427}
428
Sean Heftyc8f6a362007-02-15 17:00:18 -0800429static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500430{
431 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800432 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500433
434 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800435 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
436 if (ret)
437 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500438
Sean Heftyc8f6a362007-02-15 17:00:18 -0800439 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500440}
441
Sean Heftye51060f2006-06-17 20:37:29 -0700442int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
443 struct ib_qp_init_attr *qp_init_attr)
444{
445 struct rdma_id_private *id_priv;
446 struct ib_qp *qp;
447 int ret;
448
449 id_priv = container_of(id, struct rdma_id_private, id);
450 if (id->device != pd->device)
451 return -EINVAL;
452
453 qp = ib_create_qp(pd, qp_init_attr);
454 if (IS_ERR(qp))
455 return PTR_ERR(qp);
456
Sean Heftyc8f6a362007-02-15 17:00:18 -0800457 if (cma_is_ud_ps(id_priv->id.ps))
458 ret = cma_init_ud_qp(id_priv, qp);
459 else
460 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700461 if (ret)
462 goto err;
463
464 id->qp = qp;
465 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700466 id_priv->srq = (qp->srq != NULL);
467 return 0;
468err:
469 ib_destroy_qp(qp);
470 return ret;
471}
472EXPORT_SYMBOL(rdma_create_qp);
473
474void rdma_destroy_qp(struct rdma_cm_id *id)
475{
Sean Heftyc5483382007-09-24 13:19:09 -0700476 struct rdma_id_private *id_priv;
477
478 id_priv = container_of(id, struct rdma_id_private, id);
479 mutex_lock(&id_priv->qp_mutex);
480 ib_destroy_qp(id_priv->id.qp);
481 id_priv->id.qp = NULL;
482 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700483}
484EXPORT_SYMBOL(rdma_destroy_qp);
485
Sean Hefty5851bb82008-01-04 10:47:12 -0800486static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
487 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700488{
489 struct ib_qp_attr qp_attr;
490 int qp_attr_mask, ret;
491
Sean Heftyc5483382007-09-24 13:19:09 -0700492 mutex_lock(&id_priv->qp_mutex);
493 if (!id_priv->id.qp) {
494 ret = 0;
495 goto out;
496 }
Sean Heftye51060f2006-06-17 20:37:29 -0700497
498 /* Need to update QP attributes from default values. */
499 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700500 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700501 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700502 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700503
Sean Heftyc5483382007-09-24 13:19:09 -0700504 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700505 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700506 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700507
508 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700509 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700510 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700511 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700512
Sean Hefty5851bb82008-01-04 10:47:12 -0800513 if (conn_param)
514 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700515 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
516out:
517 mutex_unlock(&id_priv->qp_mutex);
518 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700519}
520
Sean Hefty5851bb82008-01-04 10:47:12 -0800521static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
522 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700523{
524 struct ib_qp_attr qp_attr;
525 int qp_attr_mask, ret;
526
Sean Heftyc5483382007-09-24 13:19:09 -0700527 mutex_lock(&id_priv->qp_mutex);
528 if (!id_priv->id.qp) {
529 ret = 0;
530 goto out;
531 }
Sean Heftye51060f2006-06-17 20:37:29 -0700532
533 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700534 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700535 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700536 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700537
Sean Hefty5851bb82008-01-04 10:47:12 -0800538 if (conn_param)
539 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700540 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
541out:
542 mutex_unlock(&id_priv->qp_mutex);
543 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700544}
545
Sean Heftyc5483382007-09-24 13:19:09 -0700546static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700547{
548 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700549 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700550
Sean Heftyc5483382007-09-24 13:19:09 -0700551 mutex_lock(&id_priv->qp_mutex);
552 if (!id_priv->id.qp) {
553 ret = 0;
554 goto out;
555 }
Sean Heftye51060f2006-06-17 20:37:29 -0700556
557 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700558 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
559out:
560 mutex_unlock(&id_priv->qp_mutex);
561 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700562}
563
Sean Heftyc8f6a362007-02-15 17:00:18 -0800564static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
565 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
566{
567 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
568 int ret;
569
570 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
571 ib_addr_get_pkey(dev_addr),
572 &qp_attr->pkey_index);
573 if (ret)
574 return ret;
575
576 qp_attr->port_num = id_priv->id.port_num;
577 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
578
579 if (cma_is_ud_ps(id_priv->id.ps)) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700580 ret = cma_set_qkey(id_priv);
581 if (ret)
582 return ret;
583
Sean Heftyc8f6a362007-02-15 17:00:18 -0800584 qp_attr->qkey = id_priv->qkey;
585 *qp_attr_mask |= IB_QP_QKEY;
586 } else {
587 qp_attr->qp_access_flags = 0;
588 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
589 }
590 return 0;
591}
592
Sean Heftye51060f2006-06-17 20:37:29 -0700593int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
594 int *qp_attr_mask)
595{
596 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800597 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700598
599 id_priv = container_of(id, struct rdma_id_private, id);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500600 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
601 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800602 if (!id_priv->cm_id.ib || cma_is_ud_ps(id_priv->id.ps))
603 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
604 else
605 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
606 qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700607 if (qp_attr->qp_state == IB_QPS_RTR)
608 qp_attr->rq_psn = id_priv->seq_num;
609 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500610 case RDMA_TRANSPORT_IWARP:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800611 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300612 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800613 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
614 } else
615 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
616 qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500617 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700618 default:
619 ret = -ENOSYS;
620 break;
621 }
622
623 return ret;
624}
625EXPORT_SYMBOL(rdma_init_qp_attr);
626
627static inline int cma_zero_addr(struct sockaddr *addr)
628{
629 struct in6_addr *ip6;
630
631 if (addr->sa_family == AF_INET)
Joe Perches6360a022007-12-16 13:47:33 -0800632 return ipv4_is_zeronet(
633 ((struct sockaddr_in *)addr)->sin_addr.s_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700634 else {
635 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
636 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
Eric Sesterhenn5fd571c2006-06-21 20:56:26 +0200637 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700638 }
639}
640
641static inline int cma_loopback_addr(struct sockaddr *addr)
642{
Aleksey Senin1f5175a2008-12-24 10:16:45 -0800643 if (addr->sa_family == AF_INET)
644 return ipv4_is_loopback(
645 ((struct sockaddr_in *) addr)->sin_addr.s_addr);
646 else
647 return ipv6_addr_loopback(
648 &((struct sockaddr_in6 *) addr)->sin6_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700649}
650
651static inline int cma_any_addr(struct sockaddr *addr)
652{
653 return cma_zero_addr(addr) || cma_loopback_addr(addr);
654}
655
Sean Hefty628e5f62006-11-30 16:44:16 -0800656static inline __be16 cma_port(struct sockaddr *addr)
657{
658 if (addr->sa_family == AF_INET)
659 return ((struct sockaddr_in *) addr)->sin_port;
660 else
661 return ((struct sockaddr_in6 *) addr)->sin6_port;
662}
663
Sean Heftye51060f2006-06-17 20:37:29 -0700664static inline int cma_any_port(struct sockaddr *addr)
665{
Sean Hefty628e5f62006-11-30 16:44:16 -0800666 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700667}
668
669static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
Al Viro1b90c132008-03-29 03:10:28 +0000670 u8 *ip_ver, __be16 *port,
Sean Heftye51060f2006-06-17 20:37:29 -0700671 union cma_ip_addr **src, union cma_ip_addr **dst)
672{
673 switch (ps) {
674 case RDMA_PS_SDP:
675 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
676 SDP_MAJ_VERSION)
677 return -EINVAL;
678
679 *ip_ver = sdp_get_ip_ver(hdr);
680 *port = ((struct sdp_hh *) hdr)->port;
681 *src = &((struct sdp_hh *) hdr)->src_addr;
682 *dst = &((struct sdp_hh *) hdr)->dst_addr;
683 break;
684 default:
685 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
686 return -EINVAL;
687
688 *ip_ver = cma_get_ip_ver(hdr);
689 *port = ((struct cma_hdr *) hdr)->port;
690 *src = &((struct cma_hdr *) hdr)->src_addr;
691 *dst = &((struct cma_hdr *) hdr)->dst_addr;
692 break;
693 }
694
695 if (*ip_ver != 4 && *ip_ver != 6)
696 return -EINVAL;
697 return 0;
698}
699
700static void cma_save_net_info(struct rdma_addr *addr,
701 struct rdma_addr *listen_addr,
Al Viro1b90c132008-03-29 03:10:28 +0000702 u8 ip_ver, __be16 port,
Sean Heftye51060f2006-06-17 20:37:29 -0700703 union cma_ip_addr *src, union cma_ip_addr *dst)
704{
705 struct sockaddr_in *listen4, *ip4;
706 struct sockaddr_in6 *listen6, *ip6;
707
708 switch (ip_ver) {
709 case 4:
710 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
711 ip4 = (struct sockaddr_in *) &addr->src_addr;
712 ip4->sin_family = listen4->sin_family;
713 ip4->sin_addr.s_addr = dst->ip4.addr;
714 ip4->sin_port = listen4->sin_port;
715
716 ip4 = (struct sockaddr_in *) &addr->dst_addr;
717 ip4->sin_family = listen4->sin_family;
718 ip4->sin_addr.s_addr = src->ip4.addr;
719 ip4->sin_port = port;
720 break;
721 case 6:
722 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
723 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
724 ip6->sin6_family = listen6->sin6_family;
725 ip6->sin6_addr = dst->ip6;
726 ip6->sin6_port = listen6->sin6_port;
727
728 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
729 ip6->sin6_family = listen6->sin6_family;
730 ip6->sin6_addr = src->ip6;
731 ip6->sin6_port = port;
732 break;
733 default:
734 break;
735 }
736}
737
738static inline int cma_user_data_offset(enum rdma_port_space ps)
739{
740 switch (ps) {
741 case RDMA_PS_SDP:
742 return 0;
743 default:
744 return sizeof(struct cma_hdr);
745 }
746}
747
Sean Heftye51060f2006-06-17 20:37:29 -0700748static void cma_cancel_route(struct rdma_id_private *id_priv)
749{
Tom Tucker07ebafb2006-08-03 16:02:42 -0500750 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
751 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -0700752 if (id_priv->query)
753 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
754 break;
755 default:
756 break;
757 }
758}
759
Sean Heftye51060f2006-06-17 20:37:29 -0700760static void cma_cancel_listens(struct rdma_id_private *id_priv)
761{
762 struct rdma_id_private *dev_id_priv;
763
Sean Heftyd02d1f52007-10-09 11:12:34 -0700764 /*
765 * Remove from listen_any_list to prevent added devices from spawning
766 * additional listen requests.
767 */
Sean Heftye51060f2006-06-17 20:37:29 -0700768 mutex_lock(&lock);
769 list_del(&id_priv->list);
770
771 while (!list_empty(&id_priv->listen_list)) {
772 dev_id_priv = list_entry(id_priv->listen_list.next,
773 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -0700774 /* sync with device removal to avoid duplicate destruction */
775 list_del_init(&dev_id_priv->list);
776 list_del(&dev_id_priv->listen_list);
777 mutex_unlock(&lock);
778
779 rdma_destroy_id(&dev_id_priv->id);
780 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700781 }
782 mutex_unlock(&lock);
783}
784
785static void cma_cancel_operation(struct rdma_id_private *id_priv,
786 enum cma_state state)
787{
788 switch (state) {
789 case CMA_ADDR_QUERY:
790 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
791 break;
792 case CMA_ROUTE_QUERY:
793 cma_cancel_route(id_priv);
794 break;
795 case CMA_LISTEN:
Roland Dreier3f446752008-08-04 11:02:14 -0700796 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
797 && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700798 cma_cancel_listens(id_priv);
799 break;
800 default:
801 break;
802 }
803}
804
805static void cma_release_port(struct rdma_id_private *id_priv)
806{
807 struct rdma_bind_list *bind_list = id_priv->bind_list;
808
809 if (!bind_list)
810 return;
811
812 mutex_lock(&lock);
813 hlist_del(&id_priv->node);
814 if (hlist_empty(&bind_list->owners)) {
815 idr_remove(bind_list->ps, bind_list->port);
816 kfree(bind_list);
817 }
818 mutex_unlock(&lock);
819}
820
Sean Heftyc8f6a362007-02-15 17:00:18 -0800821static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
822{
823 struct cma_multicast *mc;
824
825 while (!list_empty(&id_priv->mc_list)) {
826 mc = container_of(id_priv->mc_list.next,
827 struct cma_multicast, list);
828 list_del(&mc->list);
829 ib_sa_free_multicast(mc->multicast.ib);
830 kfree(mc);
831 }
832}
833
Sean Heftye51060f2006-06-17 20:37:29 -0700834void rdma_destroy_id(struct rdma_cm_id *id)
835{
836 struct rdma_id_private *id_priv;
837 enum cma_state state;
838
839 id_priv = container_of(id, struct rdma_id_private, id);
840 state = cma_exch(id_priv, CMA_DESTROYING);
841 cma_cancel_operation(id_priv, state);
842
Sean Hefty61a73c72006-09-01 15:33:55 -0700843 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700844 if (id_priv->cma_dev) {
Sean Hefty61a73c72006-09-01 15:33:55 -0700845 mutex_unlock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500846 switch (rdma_node_get_transport(id->device->node_type)) {
847 case RDMA_TRANSPORT_IB:
Roland Dreier3cd96562006-09-22 15:22:46 -0700848 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
Sean Heftye51060f2006-06-17 20:37:29 -0700849 ib_destroy_cm_id(id_priv->cm_id.ib);
850 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500851 case RDMA_TRANSPORT_IWARP:
852 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
853 iw_destroy_cm_id(id_priv->cm_id.iw);
854 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700855 default:
856 break;
857 }
Sean Heftyc8f6a362007-02-15 17:00:18 -0800858 cma_leave_mc_groups(id_priv);
Roland Dreier3cd96562006-09-22 15:22:46 -0700859 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700860 cma_detach_from_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700861 }
Sean Hefty61a73c72006-09-01 15:33:55 -0700862 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700863
864 cma_release_port(id_priv);
865 cma_deref_id(id_priv);
866 wait_for_completion(&id_priv->comp);
867
Sean Heftyd02d1f52007-10-09 11:12:34 -0700868 if (id_priv->internal_id)
869 cma_deref_id(id_priv->id.context);
870
Sean Heftye51060f2006-06-17 20:37:29 -0700871 kfree(id_priv->id.route.path_rec);
872 kfree(id_priv);
873}
874EXPORT_SYMBOL(rdma_destroy_id);
875
876static int cma_rep_recv(struct rdma_id_private *id_priv)
877{
878 int ret;
879
Sean Hefty5851bb82008-01-04 10:47:12 -0800880 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700881 if (ret)
882 goto reject;
883
Sean Hefty5851bb82008-01-04 10:47:12 -0800884 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700885 if (ret)
886 goto reject;
887
888 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
889 if (ret)
890 goto reject;
891
892 return 0;
893reject:
Sean Heftyc5483382007-09-24 13:19:09 -0700894 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700895 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
896 NULL, 0, NULL, 0);
897 return ret;
898}
899
900static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
901{
902 if (id_priv->id.ps == RDMA_PS_SDP &&
903 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
904 SDP_MAJ_VERSION)
905 return -EINVAL;
906
907 return 0;
908}
909
Sean Heftya1b1b612006-11-30 16:33:14 -0800910static void cma_set_rep_event_data(struct rdma_cm_event *event,
911 struct ib_cm_rep_event_param *rep_data,
912 void *private_data)
913{
914 event->param.conn.private_data = private_data;
915 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
916 event->param.conn.responder_resources = rep_data->responder_resources;
917 event->param.conn.initiator_depth = rep_data->initiator_depth;
918 event->param.conn.flow_control = rep_data->flow_control;
919 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
920 event->param.conn.srq = rep_data->srq;
921 event->param.conn.qp_num = rep_data->remote_qpn;
922}
923
Sean Heftye51060f2006-06-17 20:37:29 -0700924static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
925{
926 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -0800927 struct rdma_cm_event event;
928 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700929
Amir Vadai38ca83a2008-07-22 14:14:23 -0700930 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
931 cma_disable_callback(id_priv, CMA_CONNECT)) ||
932 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
933 cma_disable_callback(id_priv, CMA_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -0700934 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700935
Sean Heftya1b1b612006-11-30 16:33:14 -0800936 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -0700937 switch (ib_event->event) {
938 case IB_CM_REQ_ERROR:
939 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -0800940 event.event = RDMA_CM_EVENT_UNREACHABLE;
941 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -0700942 break;
943 case IB_CM_REP_RECEIVED:
Sean Heftya1b1b612006-11-30 16:33:14 -0800944 event.status = cma_verify_rep(id_priv, ib_event->private_data);
945 if (event.status)
946 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
Sean Heftye51060f2006-06-17 20:37:29 -0700947 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
Sean Heftya1b1b612006-11-30 16:33:14 -0800948 event.status = cma_rep_recv(id_priv);
949 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
950 RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -0700951 } else
Sean Heftya1b1b612006-11-30 16:33:14 -0800952 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
953 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
954 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -0700955 break;
956 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -0800957 case IB_CM_USER_ESTABLISHED:
958 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -0700959 break;
960 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -0800961 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -0700962 case IB_CM_DREQ_RECEIVED:
963 case IB_CM_DREP_RECEIVED:
964 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
965 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -0800966 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -0700967 break;
968 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -0700969 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
970 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700971 case IB_CM_MRA_RECEIVED:
972 /* ignore event */
973 goto out;
974 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -0700975 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -0800976 event.status = ib_event->param.rej_rcvd.reason;
977 event.event = RDMA_CM_EVENT_REJECTED;
978 event.param.conn.private_data = ib_event->private_data;
979 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -0700980 break;
981 default:
Roland Dreier468f2232008-07-14 23:48:47 -0700982 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -0700983 ib_event->event);
984 goto out;
985 }
986
Sean Heftya1b1b612006-11-30 16:33:14 -0800987 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -0700988 if (ret) {
989 /* Destroy the CM ID by returning a non-zero value. */
990 id_priv->cm_id.ib = NULL;
991 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700992 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700993 rdma_destroy_id(&id_priv->id);
994 return ret;
995 }
996out:
Or Gerlitzde910bd2008-07-14 23:48:53 -0700997 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700998 return ret;
999}
1000
Sean Hefty628e5f62006-11-30 16:44:16 -08001001static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1002 struct ib_cm_event *ib_event)
Sean Heftye51060f2006-06-17 20:37:29 -07001003{
1004 struct rdma_id_private *id_priv;
1005 struct rdma_cm_id *id;
1006 struct rdma_route *rt;
1007 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001008 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07001009 u8 ip_ver;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001010 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001011
Sean Heftye51060f2006-06-17 20:37:29 -07001012 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1013 &ip_ver, &port, &src, &dst))
1014 goto err;
1015
Krishna Kumar3f168d22006-09-29 12:09:51 -07001016 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1017 listen_id->ps);
1018 if (IS_ERR(id))
1019 goto err;
1020
Sean Heftye51060f2006-06-17 20:37:29 -07001021 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1022 ip_ver, port, src, dst);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001023
1024 rt = &id->route;
1025 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1026 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1027 GFP_KERNEL);
1028 if (!rt->path_rec)
1029 goto destroy_id;
1030
Sean Heftye51060f2006-06-17 20:37:29 -07001031 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1032 if (rt->num_paths == 2)
1033 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1034
Sean Heftye51060f2006-06-17 20:37:29 -07001035 ib_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Roland Dreier3f446752008-08-04 11:02:14 -07001036 ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
Or Gerlitz64c5e612008-07-14 23:48:53 -07001037 &id->route.addr.dev_addr);
1038 if (ret)
1039 goto destroy_id;
Sean Heftye51060f2006-06-17 20:37:29 -07001040
1041 id_priv = container_of(id, struct rdma_id_private, id);
1042 id_priv->state = CMA_CONNECT;
1043 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001044
1045destroy_id:
Sean Heftye51060f2006-06-17 20:37:29 -07001046 rdma_destroy_id(id);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001047err:
Sean Heftye51060f2006-06-17 20:37:29 -07001048 return NULL;
1049}
1050
Sean Hefty628e5f62006-11-30 16:44:16 -08001051static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1052 struct ib_cm_event *ib_event)
1053{
1054 struct rdma_id_private *id_priv;
1055 struct rdma_cm_id *id;
1056 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001057 __be16 port;
Sean Hefty628e5f62006-11-30 16:44:16 -08001058 u8 ip_ver;
1059 int ret;
1060
1061 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1062 listen_id->ps);
1063 if (IS_ERR(id))
1064 return NULL;
1065
1066
1067 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1068 &ip_ver, &port, &src, &dst))
1069 goto err;
1070
1071 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1072 ip_ver, port, src, dst);
1073
Roland Dreier3f446752008-08-04 11:02:14 -07001074 ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
Sean Hefty628e5f62006-11-30 16:44:16 -08001075 &id->route.addr.dev_addr);
1076 if (ret)
1077 goto err;
1078
1079 id_priv = container_of(id, struct rdma_id_private, id);
1080 id_priv->state = CMA_CONNECT;
1081 return id_priv;
1082err:
1083 rdma_destroy_id(id);
1084 return NULL;
1085}
1086
Sean Heftya1b1b612006-11-30 16:33:14 -08001087static void cma_set_req_event_data(struct rdma_cm_event *event,
1088 struct ib_cm_req_event_param *req_data,
1089 void *private_data, int offset)
1090{
1091 event->param.conn.private_data = private_data + offset;
1092 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1093 event->param.conn.responder_resources = req_data->responder_resources;
1094 event->param.conn.initiator_depth = req_data->initiator_depth;
1095 event->param.conn.flow_control = req_data->flow_control;
1096 event->param.conn.retry_count = req_data->retry_count;
1097 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1098 event->param.conn.srq = req_data->srq;
1099 event->param.conn.qp_num = req_data->remote_qpn;
1100}
1101
Sean Heftye51060f2006-06-17 20:37:29 -07001102static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1103{
1104 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001105 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001106 int offset, ret;
1107
1108 listen_id = cm_id->context;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001109 if (cma_disable_callback(listen_id, CMA_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001110 return -ECONNABORTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001111
Sean Hefty628e5f62006-11-30 16:44:16 -08001112 memset(&event, 0, sizeof event);
1113 offset = cma_user_data_offset(listen_id->id.ps);
1114 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Sean Heftyc8f6a362007-02-15 17:00:18 -08001115 if (cma_is_ud_ps(listen_id->id.ps)) {
Sean Hefty628e5f62006-11-30 16:44:16 -08001116 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1117 event.param.ud.private_data = ib_event->private_data + offset;
1118 event.param.ud.private_data_len =
1119 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1120 } else {
1121 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1122 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1123 ib_event->private_data, offset);
1124 }
Sean Heftye51060f2006-06-17 20:37:29 -07001125 if (!conn_id) {
1126 ret = -ENOMEM;
1127 goto out;
1128 }
1129
Or Gerlitzde910bd2008-07-14 23:48:53 -07001130 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Sean Hefty61a73c72006-09-01 15:33:55 -07001131 mutex_lock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001132 ret = cma_acquire_dev(conn_id);
Sean Hefty61a73c72006-09-01 15:33:55 -07001133 mutex_unlock(&lock);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301134 if (ret)
1135 goto release_conn_id;
Sean Heftye51060f2006-06-17 20:37:29 -07001136
1137 conn_id->cm_id.ib = cm_id;
1138 cm_id->context = conn_id;
1139 cm_id->cm_handler = cma_ib_handler;
1140
Sean Heftya1b1b612006-11-30 16:33:14 -08001141 ret = conn_id->id.event_handler(&conn_id->id, &event);
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001142 if (!ret) {
Sean Heftyead595a2008-02-13 14:33:53 -08001143 /*
1144 * Acquire mutex to prevent user executing rdma_destroy_id()
1145 * while we're accessing the cm_id.
1146 */
1147 mutex_lock(&lock);
1148 if (cma_comp(conn_id, CMA_CONNECT) &&
1149 !cma_is_ud_ps(conn_id->id.ps))
1150 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1151 mutex_unlock(&lock);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001152 mutex_unlock(&conn_id->handler_mutex);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301153 goto out;
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001154 }
Krishna Kumara1a733f2006-10-17 10:09:11 +05301155
1156 /* Destroy the CM ID by returning a non-zero value. */
1157 conn_id->cm_id.ib = NULL;
1158
1159release_conn_id:
1160 cma_exch(conn_id, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001161 mutex_unlock(&conn_id->handler_mutex);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301162 rdma_destroy_id(&conn_id->id);
1163
Sean Heftye51060f2006-06-17 20:37:29 -07001164out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001165 mutex_unlock(&listen_id->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001166 return ret;
1167}
1168
1169static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1170{
Sean Hefty628e5f62006-11-30 16:44:16 -08001171 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001172}
1173
1174static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1175 struct ib_cm_compare_data *compare)
1176{
1177 struct cma_hdr *cma_data, *cma_mask;
1178 struct sdp_hh *sdp_data, *sdp_mask;
Al Viro1b90c132008-03-29 03:10:28 +00001179 __be32 ip4_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001180 struct in6_addr ip6_addr;
1181
1182 memset(compare, 0, sizeof *compare);
1183 cma_data = (void *) compare->data;
1184 cma_mask = (void *) compare->mask;
1185 sdp_data = (void *) compare->data;
1186 sdp_mask = (void *) compare->mask;
1187
1188 switch (addr->sa_family) {
1189 case AF_INET:
1190 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1191 if (ps == RDMA_PS_SDP) {
1192 sdp_set_ip_ver(sdp_data, 4);
1193 sdp_set_ip_ver(sdp_mask, 0xF);
1194 sdp_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001195 sdp_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001196 } else {
1197 cma_set_ip_ver(cma_data, 4);
1198 cma_set_ip_ver(cma_mask, 0xF);
1199 cma_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001200 cma_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001201 }
1202 break;
1203 case AF_INET6:
1204 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1205 if (ps == RDMA_PS_SDP) {
1206 sdp_set_ip_ver(sdp_data, 6);
1207 sdp_set_ip_ver(sdp_mask, 0xF);
1208 sdp_data->dst_addr.ip6 = ip6_addr;
1209 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1210 sizeof sdp_mask->dst_addr.ip6);
1211 } else {
1212 cma_set_ip_ver(cma_data, 6);
1213 cma_set_ip_ver(cma_mask, 0xF);
1214 cma_data->dst_addr.ip6 = ip6_addr;
1215 memset(&cma_mask->dst_addr.ip6, 0xFF,
1216 sizeof cma_mask->dst_addr.ip6);
1217 }
1218 break;
1219 default:
1220 break;
1221 }
1222}
1223
Tom Tucker07ebafb2006-08-03 16:02:42 -05001224static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1225{
1226 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001227 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001228 struct sockaddr_in *sin;
1229 int ret = 0;
1230
Or Gerlitzde910bd2008-07-14 23:48:53 -07001231 if (cma_disable_callback(id_priv, CMA_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001232 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001233
Sean Heftybe65f082007-05-07 11:49:12 -07001234 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001235 switch (iw_event->event) {
1236 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001237 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001238 break;
1239 case IW_CM_EVENT_CONNECT_REPLY:
1240 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1241 *sin = iw_event->local_addr;
1242 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1243 *sin = iw_event->remote_addr;
Steve Wise881a0452006-12-15 16:50:17 -06001244 switch (iw_event->status) {
1245 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001246 event.event = RDMA_CM_EVENT_ESTABLISHED;
Steve Wise881a0452006-12-15 16:50:17 -06001247 break;
1248 case -ECONNRESET:
1249 case -ECONNREFUSED:
1250 event.event = RDMA_CM_EVENT_REJECTED;
1251 break;
1252 case -ETIMEDOUT:
1253 event.event = RDMA_CM_EVENT_UNREACHABLE;
1254 break;
1255 default:
1256 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1257 break;
1258 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001259 break;
1260 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001261 event.event = RDMA_CM_EVENT_ESTABLISHED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001262 break;
1263 default:
1264 BUG_ON(1);
1265 }
1266
Sean Heftya1b1b612006-11-30 16:33:14 -08001267 event.status = iw_event->status;
1268 event.param.conn.private_data = iw_event->private_data;
1269 event.param.conn.private_data_len = iw_event->private_data_len;
1270 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001271 if (ret) {
1272 /* Destroy the CM ID by returning a non-zero value. */
1273 id_priv->cm_id.iw = NULL;
1274 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001275 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001276 rdma_destroy_id(&id_priv->id);
1277 return ret;
1278 }
1279
Or Gerlitzde910bd2008-07-14 23:48:53 -07001280 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001281 return ret;
1282}
1283
1284static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1285 struct iw_cm_event *iw_event)
1286{
1287 struct rdma_cm_id *new_cm_id;
1288 struct rdma_id_private *listen_id, *conn_id;
1289 struct sockaddr_in *sin;
1290 struct net_device *dev = NULL;
Sean Heftya1b1b612006-11-30 16:33:14 -08001291 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001292 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001293 struct ib_device_attr attr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001294
1295 listen_id = cm_id->context;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001296 if (cma_disable_callback(listen_id, CMA_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001297 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001298
1299 /* Create a new RDMA id for the new IW CM ID */
1300 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1301 listen_id->id.context,
1302 RDMA_PS_TCP);
Julia Lawall10f32062008-04-16 21:09:25 -07001303 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001304 ret = -ENOMEM;
1305 goto out;
1306 }
1307 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001308 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001309 conn_id->state = CMA_CONNECT;
1310
Denis V. Lunev1ab35272008-01-22 22:04:30 -08001311 dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001312 if (!dev) {
1313 ret = -EADDRNOTAVAIL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001314 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001315 rdma_destroy_id(new_cm_id);
1316 goto out;
1317 }
1318 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1319 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001320 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001321 rdma_destroy_id(new_cm_id);
1322 goto out;
1323 }
1324
Sean Hefty61a73c72006-09-01 15:33:55 -07001325 mutex_lock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001326 ret = cma_acquire_dev(conn_id);
Sean Hefty61a73c72006-09-01 15:33:55 -07001327 mutex_unlock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001328 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001329 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001330 rdma_destroy_id(new_cm_id);
1331 goto out;
1332 }
1333
1334 conn_id->cm_id.iw = cm_id;
1335 cm_id->context = conn_id;
1336 cm_id->cm_handler = cma_iw_handler;
1337
1338 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1339 *sin = iw_event->local_addr;
1340 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1341 *sin = iw_event->remote_addr;
1342
Steve Wise8d8293c2007-10-29 11:34:05 -05001343 ret = ib_query_device(conn_id->id.device, &attr);
1344 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001345 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001346 rdma_destroy_id(new_cm_id);
1347 goto out;
1348 }
1349
Sean Heftya1b1b612006-11-30 16:33:14 -08001350 memset(&event, 0, sizeof event);
1351 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1352 event.param.conn.private_data = iw_event->private_data;
1353 event.param.conn.private_data_len = iw_event->private_data_len;
Steve Wise8d8293c2007-10-29 11:34:05 -05001354 event.param.conn.initiator_depth = attr.max_qp_init_rd_atom;
1355 event.param.conn.responder_resources = attr.max_qp_rd_atom;
Sean Heftya1b1b612006-11-30 16:33:14 -08001356 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001357 if (ret) {
1358 /* User wants to destroy the CM ID */
1359 conn_id->cm_id.iw = NULL;
1360 cma_exch(conn_id, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001361 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001362 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001363 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001364 }
1365
Or Gerlitzde910bd2008-07-14 23:48:53 -07001366 mutex_unlock(&conn_id->handler_mutex);
1367
Tom Tucker07ebafb2006-08-03 16:02:42 -05001368out:
1369 if (dev)
1370 dev_put(dev);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001371 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001372 return ret;
1373}
1374
Sean Heftye51060f2006-06-17 20:37:29 -07001375static int cma_ib_listen(struct rdma_id_private *id_priv)
1376{
1377 struct ib_cm_compare_data compare_data;
1378 struct sockaddr *addr;
1379 __be64 svc_id;
1380 int ret;
1381
1382 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1383 id_priv);
1384 if (IS_ERR(id_priv->cm_id.ib))
1385 return PTR_ERR(id_priv->cm_id.ib);
1386
Roland Dreier3f446752008-08-04 11:02:14 -07001387 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001388 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1389 if (cma_any_addr(addr))
1390 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1391 else {
1392 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1393 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1394 }
1395
1396 if (ret) {
1397 ib_destroy_cm_id(id_priv->cm_id.ib);
1398 id_priv->cm_id.ib = NULL;
1399 }
1400
1401 return ret;
1402}
1403
Tom Tucker07ebafb2006-08-03 16:02:42 -05001404static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1405{
1406 int ret;
1407 struct sockaddr_in *sin;
1408
1409 id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1410 iw_conn_req_handler,
1411 id_priv);
1412 if (IS_ERR(id_priv->cm_id.iw))
1413 return PTR_ERR(id_priv->cm_id.iw);
1414
1415 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1416 id_priv->cm_id.iw->local_addr = *sin;
1417
1418 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1419
1420 if (ret) {
1421 iw_destroy_cm_id(id_priv->cm_id.iw);
1422 id_priv->cm_id.iw = NULL;
1423 }
1424
1425 return ret;
1426}
1427
Sean Heftye51060f2006-06-17 20:37:29 -07001428static int cma_listen_handler(struct rdma_cm_id *id,
1429 struct rdma_cm_event *event)
1430{
1431 struct rdma_id_private *id_priv = id->context;
1432
1433 id->context = id_priv->id.context;
1434 id->event_handler = id_priv->id.event_handler;
1435 return id_priv->id.event_handler(id, event);
1436}
1437
1438static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1439 struct cma_device *cma_dev)
1440{
1441 struct rdma_id_private *dev_id_priv;
1442 struct rdma_cm_id *id;
1443 int ret;
1444
1445 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1446 if (IS_ERR(id))
1447 return;
1448
1449 dev_id_priv = container_of(id, struct rdma_id_private, id);
1450
1451 dev_id_priv->state = CMA_ADDR_BOUND;
1452 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
Roland Dreier3f446752008-08-04 11:02:14 -07001453 ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001454
1455 cma_attach_to_dev(dev_id_priv, cma_dev);
1456 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001457 atomic_inc(&id_priv->refcount);
1458 dev_id_priv->internal_id = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07001459
1460 ret = rdma_listen(id, id_priv->backlog);
1461 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07001462 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07001463 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07001464}
1465
1466static void cma_listen_on_all(struct rdma_id_private *id_priv)
1467{
1468 struct cma_device *cma_dev;
1469
1470 mutex_lock(&lock);
1471 list_add_tail(&id_priv->list, &listen_any_list);
1472 list_for_each_entry(cma_dev, &dev_list, list)
1473 cma_listen_on_dev(id_priv, cma_dev);
1474 mutex_unlock(&lock);
1475}
1476
1477static int cma_bind_any(struct rdma_cm_id *id, sa_family_t af)
1478{
Aleksey Senin1f5175a2008-12-24 10:16:45 -08001479 struct sockaddr_storage addr_in;
Sean Heftye51060f2006-06-17 20:37:29 -07001480
1481 memset(&addr_in, 0, sizeof addr_in);
Aleksey Senin1f5175a2008-12-24 10:16:45 -08001482 addr_in.ss_family = af;
Sean Heftye51060f2006-06-17 20:37:29 -07001483 return rdma_bind_addr(id, (struct sockaddr *) &addr_in);
1484}
1485
1486int rdma_listen(struct rdma_cm_id *id, int backlog)
1487{
1488 struct rdma_id_private *id_priv;
1489 int ret;
1490
1491 id_priv = container_of(id, struct rdma_id_private, id);
1492 if (id_priv->state == CMA_IDLE) {
1493 ret = cma_bind_any(id, AF_INET);
1494 if (ret)
1495 return ret;
1496 }
1497
1498 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1499 return -EINVAL;
1500
1501 id_priv->backlog = backlog;
1502 if (id->device) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001503 switch (rdma_node_get_transport(id->device->node_type)) {
1504 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -07001505 ret = cma_ib_listen(id_priv);
1506 if (ret)
1507 goto err;
1508 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001509 case RDMA_TRANSPORT_IWARP:
1510 ret = cma_iw_listen(id_priv, backlog);
1511 if (ret)
1512 goto err;
1513 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001514 default:
1515 ret = -ENOSYS;
1516 goto err;
1517 }
1518 } else
1519 cma_listen_on_all(id_priv);
1520
1521 return 0;
1522err:
1523 id_priv->backlog = 0;
1524 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1525 return ret;
1526}
1527EXPORT_SYMBOL(rdma_listen);
1528
Sean Heftya81c9942007-08-08 15:51:06 -07001529void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1530{
1531 struct rdma_id_private *id_priv;
1532
1533 id_priv = container_of(id, struct rdma_id_private, id);
1534 id_priv->tos = (u8) tos;
1535}
1536EXPORT_SYMBOL(rdma_set_service_type);
1537
Sean Heftye51060f2006-06-17 20:37:29 -07001538static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1539 void *context)
1540{
1541 struct cma_work *work = context;
1542 struct rdma_route *route;
1543
1544 route = &work->id->id.route;
1545
1546 if (!status) {
1547 route->num_paths = 1;
1548 *route->path_rec = *path_rec;
1549 } else {
1550 work->old_state = CMA_ROUTE_QUERY;
1551 work->new_state = CMA_ADDR_RESOLVED;
1552 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07001553 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001554 }
1555
1556 queue_work(cma_wq, &work->work);
1557}
1558
1559static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1560 struct cma_work *work)
1561{
Sean Heftya81c9942007-08-08 15:51:06 -07001562 struct rdma_addr *addr = &id_priv->id.route.addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001563 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07001564 ib_sa_comp_mask comp_mask;
1565 struct sockaddr_in6 *sin6;
Sean Heftye51060f2006-06-17 20:37:29 -07001566
1567 memset(&path_rec, 0, sizeof path_rec);
Sean Heftya81c9942007-08-08 15:51:06 -07001568 ib_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
1569 ib_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
1570 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001571 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08001572 path_rec.reversible = 1;
Roland Dreier3f446752008-08-04 11:02:14 -07001573 path_rec.service_id = cma_get_service_id(id_priv->id.ps,
1574 (struct sockaddr *) &addr->dst_addr);
Sean Heftya81c9942007-08-08 15:51:06 -07001575
1576 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1577 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1578 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1579
Roland Dreier3f446752008-08-04 11:02:14 -07001580 if (addr->src_addr.ss_family == AF_INET) {
Sean Heftya81c9942007-08-08 15:51:06 -07001581 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1582 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1583 } else {
1584 sin6 = (struct sockaddr_in6 *) &addr->src_addr;
1585 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1586 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1587 }
Sean Heftye51060f2006-06-17 20:37:29 -07001588
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001589 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07001590 id_priv->id.port_num, &path_rec,
1591 comp_mask, timeout_ms,
1592 GFP_KERNEL, cma_query_handler,
1593 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001594
1595 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1596}
1597
David Howellsc4028952006-11-22 14:57:56 +00001598static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07001599{
David Howellsc4028952006-11-22 14:57:56 +00001600 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07001601 struct rdma_id_private *id_priv = work->id;
1602 int destroy = 0;
1603
Or Gerlitzde910bd2008-07-14 23:48:53 -07001604 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001605 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1606 goto out;
1607
1608 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1609 cma_exch(id_priv, CMA_DESTROYING);
1610 destroy = 1;
1611 }
1612out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001613 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001614 cma_deref_id(id_priv);
1615 if (destroy)
1616 rdma_destroy_id(&id_priv->id);
1617 kfree(work);
1618}
1619
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001620static void cma_ndev_work_handler(struct work_struct *_work)
1621{
1622 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1623 struct rdma_id_private *id_priv = work->id;
1624 int destroy = 0;
1625
1626 mutex_lock(&id_priv->handler_mutex);
1627 if (id_priv->state == CMA_DESTROYING ||
1628 id_priv->state == CMA_DEVICE_REMOVAL)
1629 goto out;
1630
1631 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1632 cma_exch(id_priv, CMA_DESTROYING);
1633 destroy = 1;
1634 }
1635
1636out:
1637 mutex_unlock(&id_priv->handler_mutex);
1638 cma_deref_id(id_priv);
1639 if (destroy)
1640 rdma_destroy_id(&id_priv->id);
1641 kfree(work);
1642}
1643
Sean Heftye51060f2006-06-17 20:37:29 -07001644static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1645{
1646 struct rdma_route *route = &id_priv->id.route;
1647 struct cma_work *work;
1648 int ret;
1649
1650 work = kzalloc(sizeof *work, GFP_KERNEL);
1651 if (!work)
1652 return -ENOMEM;
1653
1654 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001655 INIT_WORK(&work->work, cma_work_handler);
Sean Heftye51060f2006-06-17 20:37:29 -07001656 work->old_state = CMA_ROUTE_QUERY;
1657 work->new_state = CMA_ROUTE_RESOLVED;
1658 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1659
1660 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1661 if (!route->path_rec) {
1662 ret = -ENOMEM;
1663 goto err1;
1664 }
1665
1666 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1667 if (ret)
1668 goto err2;
1669
1670 return 0;
1671err2:
1672 kfree(route->path_rec);
1673 route->path_rec = NULL;
1674err1:
1675 kfree(work);
1676 return ret;
1677}
1678
1679int rdma_set_ib_paths(struct rdma_cm_id *id,
1680 struct ib_sa_path_rec *path_rec, int num_paths)
1681{
1682 struct rdma_id_private *id_priv;
1683 int ret;
1684
1685 id_priv = container_of(id, struct rdma_id_private, id);
1686 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1687 return -EINVAL;
1688
1689 id->route.path_rec = kmalloc(sizeof *path_rec * num_paths, GFP_KERNEL);
1690 if (!id->route.path_rec) {
1691 ret = -ENOMEM;
1692 goto err;
1693 }
1694
1695 memcpy(id->route.path_rec, path_rec, sizeof *path_rec * num_paths);
1696 return 0;
1697err:
1698 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1699 return ret;
1700}
1701EXPORT_SYMBOL(rdma_set_ib_paths);
1702
Tom Tucker07ebafb2006-08-03 16:02:42 -05001703static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1704{
1705 struct cma_work *work;
1706
1707 work = kzalloc(sizeof *work, GFP_KERNEL);
1708 if (!work)
1709 return -ENOMEM;
1710
1711 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001712 INIT_WORK(&work->work, cma_work_handler);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001713 work->old_state = CMA_ROUTE_QUERY;
1714 work->new_state = CMA_ROUTE_RESOLVED;
1715 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1716 queue_work(cma_wq, &work->work);
1717 return 0;
1718}
1719
Sean Heftye51060f2006-06-17 20:37:29 -07001720int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1721{
1722 struct rdma_id_private *id_priv;
1723 int ret;
1724
1725 id_priv = container_of(id, struct rdma_id_private, id);
1726 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1727 return -EINVAL;
1728
1729 atomic_inc(&id_priv->refcount);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001730 switch (rdma_node_get_transport(id->device->node_type)) {
1731 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -07001732 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1733 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001734 case RDMA_TRANSPORT_IWARP:
1735 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1736 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001737 default:
1738 ret = -ENOSYS;
1739 break;
1740 }
1741 if (ret)
1742 goto err;
1743
1744 return 0;
1745err:
1746 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1747 cma_deref_id(id_priv);
1748 return ret;
1749}
1750EXPORT_SYMBOL(rdma_resolve_route);
1751
1752static int cma_bind_loopback(struct rdma_id_private *id_priv)
1753{
1754 struct cma_device *cma_dev;
1755 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001756 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001757 u16 pkey;
1758 int ret;
1759 u8 p;
1760
1761 mutex_lock(&lock);
Krishna Kumare82153b2006-10-16 10:09:01 +05301762 if (list_empty(&dev_list)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001763 ret = -ENODEV;
1764 goto out;
1765 }
Krishna Kumare82153b2006-10-16 10:09:01 +05301766 list_for_each_entry(cma_dev, &dev_list, list)
1767 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1768 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1769 port_attr.state == IB_PORT_ACTIVE)
1770 goto port_found;
1771
1772 p = 1;
1773 cma_dev = list_entry(dev_list.next, struct cma_device, list);
Sean Heftye51060f2006-06-17 20:37:29 -07001774
1775port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001776 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001777 if (ret)
1778 goto out;
1779
1780 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1781 if (ret)
1782 goto out;
1783
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001784 ib_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001785 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1786 id_priv->id.port_num = p;
1787 cma_attach_to_dev(id_priv, cma_dev);
1788out:
1789 mutex_unlock(&lock);
1790 return ret;
1791}
1792
1793static void addr_handler(int status, struct sockaddr *src_addr,
1794 struct rdma_dev_addr *dev_addr, void *context)
1795{
1796 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001797 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001798
Sean Heftya1b1b612006-11-30 16:33:14 -08001799 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001800 mutex_lock(&id_priv->handler_mutex);
Sean Hefty61a73c72006-09-01 15:33:55 -07001801
1802 /*
1803 * Grab mutex to block rdma_destroy_id() from removing the device while
1804 * we're trying to acquire it.
1805 */
1806 mutex_lock(&lock);
1807 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1808 mutex_unlock(&lock);
1809 goto out;
1810 }
1811
1812 if (!status && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001813 status = cma_acquire_dev(id_priv);
Sean Hefty61a73c72006-09-01 15:33:55 -07001814 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001815
1816 if (status) {
Sean Hefty61a73c72006-09-01 15:33:55 -07001817 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07001818 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001819 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1820 event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001821 } else {
Sean Heftye51060f2006-06-17 20:37:29 -07001822 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1823 ip_addr_size(src_addr));
Sean Heftya1b1b612006-11-30 16:33:14 -08001824 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001825 }
1826
Sean Heftya1b1b612006-11-30 16:33:14 -08001827 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001828 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001829 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001830 cma_deref_id(id_priv);
1831 rdma_destroy_id(&id_priv->id);
1832 return;
1833 }
1834out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001835 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001836 cma_deref_id(id_priv);
1837}
1838
1839static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1840{
1841 struct cma_work *work;
1842 struct sockaddr_in *src_in, *dst_in;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001843 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001844 int ret;
1845
1846 work = kzalloc(sizeof *work, GFP_KERNEL);
1847 if (!work)
1848 return -ENOMEM;
1849
1850 if (!id_priv->cma_dev) {
1851 ret = cma_bind_loopback(id_priv);
1852 if (ret)
1853 goto err;
1854 }
1855
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001856 ib_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
1857 ib_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001858
Roland Dreier3f446752008-08-04 11:02:14 -07001859 if (cma_zero_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001860 src_in = (struct sockaddr_in *)&id_priv->id.route.addr.src_addr;
1861 dst_in = (struct sockaddr_in *)&id_priv->id.route.addr.dst_addr;
1862 src_in->sin_family = dst_in->sin_family;
1863 src_in->sin_addr.s_addr = dst_in->sin_addr.s_addr;
1864 }
1865
1866 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001867 INIT_WORK(&work->work, cma_work_handler);
Sean Heftye51060f2006-06-17 20:37:29 -07001868 work->old_state = CMA_ADDR_QUERY;
1869 work->new_state = CMA_ADDR_RESOLVED;
1870 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
1871 queue_work(cma_wq, &work->work);
1872 return 0;
1873err:
1874 kfree(work);
1875 return ret;
1876}
1877
1878static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1879 struct sockaddr *dst_addr)
1880{
1881 if (src_addr && src_addr->sa_family)
1882 return rdma_bind_addr(id, src_addr);
1883 else
1884 return cma_bind_any(id, dst_addr->sa_family);
1885}
1886
1887int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
1888 struct sockaddr *dst_addr, int timeout_ms)
1889{
1890 struct rdma_id_private *id_priv;
1891 int ret;
1892
1893 id_priv = container_of(id, struct rdma_id_private, id);
1894 if (id_priv->state == CMA_IDLE) {
1895 ret = cma_bind_addr(id, src_addr, dst_addr);
1896 if (ret)
1897 return ret;
1898 }
1899
1900 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
1901 return -EINVAL;
1902
1903 atomic_inc(&id_priv->refcount);
1904 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
1905 if (cma_any_addr(dst_addr))
1906 ret = cma_resolve_loopback(id_priv);
1907 else
Roland Dreier3f446752008-08-04 11:02:14 -07001908 ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
Sean Hefty7a118df2006-10-31 11:12:59 -08001909 dst_addr, &id->route.addr.dev_addr,
Sean Heftye51060f2006-06-17 20:37:29 -07001910 timeout_ms, addr_handler, id_priv);
1911 if (ret)
1912 goto err;
1913
1914 return 0;
1915err:
1916 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
1917 cma_deref_id(id_priv);
1918 return ret;
1919}
1920EXPORT_SYMBOL(rdma_resolve_addr);
1921
1922static void cma_bind_port(struct rdma_bind_list *bind_list,
1923 struct rdma_id_private *id_priv)
1924{
1925 struct sockaddr_in *sin;
1926
1927 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1928 sin->sin_port = htons(bind_list->port);
1929 id_priv->bind_list = bind_list;
1930 hlist_add_head(&id_priv->node, &bind_list->owners);
1931}
1932
1933static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
1934 unsigned short snum)
1935{
1936 struct rdma_bind_list *bind_list;
Sean Heftyaedec082007-01-29 16:41:23 -08001937 int port, ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001938
Sean Heftycb164b82007-03-05 12:50:17 -08001939 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07001940 if (!bind_list)
1941 return -ENOMEM;
1942
Sean Heftye51060f2006-06-17 20:37:29 -07001943 do {
Sean Heftyaedec082007-01-29 16:41:23 -08001944 ret = idr_get_new_above(ps, bind_list, snum, &port);
Sean Heftye51060f2006-06-17 20:37:29 -07001945 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1946
1947 if (ret)
Sean Heftyaedec082007-01-29 16:41:23 -08001948 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07001949
Sean Heftyaedec082007-01-29 16:41:23 -08001950 if (port != snum) {
Sean Heftye51060f2006-06-17 20:37:29 -07001951 ret = -EADDRNOTAVAIL;
Sean Heftyaedec082007-01-29 16:41:23 -08001952 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07001953 }
1954
1955 bind_list->ps = ps;
1956 bind_list->port = (unsigned short) port;
1957 cma_bind_port(bind_list, id_priv);
1958 return 0;
Sean Heftyaedec082007-01-29 16:41:23 -08001959err2:
1960 idr_remove(ps, port);
1961err1:
1962 kfree(bind_list);
1963 return ret;
1964}
1965
1966static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
1967{
1968 struct rdma_bind_list *bind_list;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07001969 int port, ret, low, high;
Sean Heftyaedec082007-01-29 16:41:23 -08001970
1971 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
1972 if (!bind_list)
1973 return -ENOMEM;
1974
1975retry:
Stephen Hemminger227b60f2007-10-10 17:30:46 -07001976 /* FIXME: add proper port randomization per like inet_csk_get_port */
Sean Heftyaedec082007-01-29 16:41:23 -08001977 do {
1978 ret = idr_get_new_above(ps, bind_list, next_port, &port);
1979 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
1980
1981 if (ret)
1982 goto err1;
1983
Stephen Hemminger227b60f2007-10-10 17:30:46 -07001984 inet_get_local_port_range(&low, &high);
1985 if (port > high) {
1986 if (next_port != low) {
Sean Heftyaedec082007-01-29 16:41:23 -08001987 idr_remove(ps, port);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07001988 next_port = low;
Sean Heftyaedec082007-01-29 16:41:23 -08001989 goto retry;
1990 }
1991 ret = -EADDRNOTAVAIL;
1992 goto err2;
1993 }
1994
Stephen Hemminger227b60f2007-10-10 17:30:46 -07001995 if (port == high)
1996 next_port = low;
Sean Heftyaedec082007-01-29 16:41:23 -08001997 else
1998 next_port = port + 1;
1999
2000 bind_list->ps = ps;
2001 bind_list->port = (unsigned short) port;
2002 cma_bind_port(bind_list, id_priv);
2003 return 0;
2004err2:
2005 idr_remove(ps, port);
2006err1:
Sean Heftye51060f2006-06-17 20:37:29 -07002007 kfree(bind_list);
2008 return ret;
2009}
2010
2011static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2012{
2013 struct rdma_id_private *cur_id;
2014 struct sockaddr_in *sin, *cur_sin;
2015 struct rdma_bind_list *bind_list;
2016 struct hlist_node *node;
2017 unsigned short snum;
2018
2019 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2020 snum = ntohs(sin->sin_port);
2021 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2022 return -EACCES;
2023
2024 bind_list = idr_find(ps, snum);
2025 if (!bind_list)
2026 return cma_alloc_port(ps, id_priv, snum);
2027
2028 /*
2029 * We don't support binding to any address if anyone is bound to
2030 * a specific address on the same port.
2031 */
Roland Dreier3f446752008-08-04 11:02:14 -07002032 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr))
Sean Heftye51060f2006-06-17 20:37:29 -07002033 return -EADDRNOTAVAIL;
2034
2035 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
Roland Dreier3f446752008-08-04 11:02:14 -07002036 if (cma_any_addr((struct sockaddr *) &cur_id->id.route.addr.src_addr))
Sean Heftye51060f2006-06-17 20:37:29 -07002037 return -EADDRNOTAVAIL;
Roland Dreier3cd96562006-09-22 15:22:46 -07002038
Sean Heftye51060f2006-06-17 20:37:29 -07002039 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
2040 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
2041 return -EADDRINUSE;
2042 }
2043
2044 cma_bind_port(bind_list, id_priv);
2045 return 0;
2046}
2047
2048static int cma_get_port(struct rdma_id_private *id_priv)
2049{
2050 struct idr *ps;
2051 int ret;
2052
2053 switch (id_priv->id.ps) {
2054 case RDMA_PS_SDP:
2055 ps = &sdp_ps;
2056 break;
2057 case RDMA_PS_TCP:
2058 ps = &tcp_ps;
2059 break;
Sean Hefty628e5f62006-11-30 16:44:16 -08002060 case RDMA_PS_UDP:
2061 ps = &udp_ps;
2062 break;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002063 case RDMA_PS_IPOIB:
2064 ps = &ipoib_ps;
2065 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002066 default:
2067 return -EPROTONOSUPPORT;
2068 }
2069
2070 mutex_lock(&lock);
Roland Dreier3f446752008-08-04 11:02:14 -07002071 if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
Sean Heftyaedec082007-01-29 16:41:23 -08002072 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002073 else
2074 ret = cma_use_port(ps, id_priv);
2075 mutex_unlock(&lock);
2076
2077 return ret;
2078}
2079
2080int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2081{
2082 struct rdma_id_private *id_priv;
2083 int ret;
2084
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002085 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Sean Heftye51060f2006-06-17 20:37:29 -07002086 return -EAFNOSUPPORT;
2087
2088 id_priv = container_of(id, struct rdma_id_private, id);
2089 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
2090 return -EINVAL;
2091
2092 if (!cma_any_addr(addr)) {
2093 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002094 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002095 goto err1;
2096
2097 mutex_lock(&lock);
2098 ret = cma_acquire_dev(id_priv);
2099 mutex_unlock(&lock);
2100 if (ret)
2101 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002102 }
2103
2104 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2105 ret = cma_get_port(id_priv);
2106 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002107 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002108
2109 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07002110err2:
2111 if (!cma_any_addr(addr)) {
2112 mutex_lock(&lock);
2113 cma_detach_from_dev(id_priv);
2114 mutex_unlock(&lock);
2115 }
2116err1:
Sean Heftye51060f2006-06-17 20:37:29 -07002117 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2118 return ret;
2119}
2120EXPORT_SYMBOL(rdma_bind_addr);
2121
2122static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2123 struct rdma_route *route)
2124{
Sean Heftye51060f2006-06-17 20:37:29 -07002125 struct cma_hdr *cma_hdr;
2126 struct sdp_hh *sdp_hdr;
2127
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002128 if (route->addr.src_addr.ss_family == AF_INET) {
2129 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07002130
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002131 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2132 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2133
2134 switch (ps) {
2135 case RDMA_PS_SDP:
2136 sdp_hdr = hdr;
2137 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2138 return -EINVAL;
2139 sdp_set_ip_ver(sdp_hdr, 4);
2140 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2141 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2142 sdp_hdr->port = src4->sin_port;
2143 break;
2144 default:
2145 cma_hdr = hdr;
2146 cma_hdr->cma_version = CMA_VERSION;
2147 cma_set_ip_ver(cma_hdr, 4);
2148 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2149 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2150 cma_hdr->port = src4->sin_port;
2151 break;
2152 }
2153 } else {
2154 struct sockaddr_in6 *src6, *dst6;
2155
2156 src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
2157 dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
2158
2159 switch (ps) {
2160 case RDMA_PS_SDP:
2161 sdp_hdr = hdr;
2162 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2163 return -EINVAL;
2164 sdp_set_ip_ver(sdp_hdr, 6);
2165 sdp_hdr->src_addr.ip6 = src6->sin6_addr;
2166 sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
2167 sdp_hdr->port = src6->sin6_port;
2168 break;
2169 default:
2170 cma_hdr = hdr;
2171 cma_hdr->cma_version = CMA_VERSION;
2172 cma_set_ip_ver(cma_hdr, 6);
2173 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2174 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2175 cma_hdr->port = src6->sin6_port;
2176 break;
2177 }
Sean Heftye51060f2006-06-17 20:37:29 -07002178 }
2179 return 0;
2180}
2181
Sean Hefty628e5f62006-11-30 16:44:16 -08002182static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2183 struct ib_cm_event *ib_event)
2184{
2185 struct rdma_id_private *id_priv = cm_id->context;
2186 struct rdma_cm_event event;
2187 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2188 int ret = 0;
2189
Or Gerlitzde910bd2008-07-14 23:48:53 -07002190 if (cma_disable_callback(id_priv, CMA_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07002191 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08002192
Sean Hefty8aa08602007-05-07 11:49:00 -07002193 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08002194 switch (ib_event->event) {
2195 case IB_CM_SIDR_REQ_ERROR:
2196 event.event = RDMA_CM_EVENT_UNREACHABLE;
2197 event.status = -ETIMEDOUT;
2198 break;
2199 case IB_CM_SIDR_REP_RECEIVED:
2200 event.param.ud.private_data = ib_event->private_data;
2201 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2202 if (rep->status != IB_SIDR_SUCCESS) {
2203 event.event = RDMA_CM_EVENT_UNREACHABLE;
2204 event.status = ib_event->param.sidr_rep_rcvd.status;
2205 break;
2206 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002207 ret = cma_set_qkey(id_priv);
2208 if (ret) {
2209 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2210 event.status = -EINVAL;
2211 break;
2212 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08002213 if (id_priv->qkey != rep->qkey) {
Sean Hefty628e5f62006-11-30 16:44:16 -08002214 event.event = RDMA_CM_EVENT_UNREACHABLE;
2215 event.status = -EINVAL;
2216 break;
2217 }
2218 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2219 id_priv->id.route.path_rec,
2220 &event.param.ud.ah_attr);
2221 event.param.ud.qp_num = rep->qpn;
2222 event.param.ud.qkey = rep->qkey;
2223 event.event = RDMA_CM_EVENT_ESTABLISHED;
2224 event.status = 0;
2225 break;
2226 default:
Roland Dreier468f2232008-07-14 23:48:47 -07002227 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08002228 ib_event->event);
2229 goto out;
2230 }
2231
2232 ret = id_priv->id.event_handler(&id_priv->id, &event);
2233 if (ret) {
2234 /* Destroy the CM ID by returning a non-zero value. */
2235 id_priv->cm_id.ib = NULL;
2236 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002237 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002238 rdma_destroy_id(&id_priv->id);
2239 return ret;
2240 }
2241out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002242 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002243 return ret;
2244}
2245
2246static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2247 struct rdma_conn_param *conn_param)
2248{
2249 struct ib_cm_sidr_req_param req;
2250 struct rdma_route *route;
2251 int ret;
2252
2253 req.private_data_len = sizeof(struct cma_hdr) +
2254 conn_param->private_data_len;
2255 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2256 if (!req.private_data)
2257 return -ENOMEM;
2258
2259 if (conn_param->private_data && conn_param->private_data_len)
2260 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2261 conn_param->private_data, conn_param->private_data_len);
2262
2263 route = &id_priv->id.route;
2264 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2265 if (ret)
2266 goto out;
2267
2268 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2269 cma_sidr_rep_handler, id_priv);
2270 if (IS_ERR(id_priv->cm_id.ib)) {
2271 ret = PTR_ERR(id_priv->cm_id.ib);
2272 goto out;
2273 }
2274
2275 req.path = route->path_rec;
2276 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002277 (struct sockaddr *) &route->addr.dst_addr);
Sean Hefty628e5f62006-11-30 16:44:16 -08002278 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2279 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2280
2281 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2282 if (ret) {
2283 ib_destroy_cm_id(id_priv->cm_id.ib);
2284 id_priv->cm_id.ib = NULL;
2285 }
2286out:
2287 kfree(req.private_data);
2288 return ret;
2289}
2290
Sean Heftye51060f2006-06-17 20:37:29 -07002291static int cma_connect_ib(struct rdma_id_private *id_priv,
2292 struct rdma_conn_param *conn_param)
2293{
2294 struct ib_cm_req_param req;
2295 struct rdma_route *route;
2296 void *private_data;
2297 int offset, ret;
2298
2299 memset(&req, 0, sizeof req);
2300 offset = cma_user_data_offset(id_priv->id.ps);
2301 req.private_data_len = offset + conn_param->private_data_len;
2302 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2303 if (!private_data)
2304 return -ENOMEM;
2305
2306 if (conn_param->private_data && conn_param->private_data_len)
2307 memcpy(private_data + offset, conn_param->private_data,
2308 conn_param->private_data_len);
2309
2310 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2311 id_priv);
2312 if (IS_ERR(id_priv->cm_id.ib)) {
2313 ret = PTR_ERR(id_priv->cm_id.ib);
2314 goto out;
2315 }
2316
2317 route = &id_priv->id.route;
2318 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2319 if (ret)
2320 goto out;
2321 req.private_data = private_data;
2322
2323 req.primary_path = &route->path_rec[0];
2324 if (route->num_paths == 2)
2325 req.alternate_path = &route->path_rec[1];
2326
2327 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002328 (struct sockaddr *) &route->addr.dst_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002329 req.qp_num = id_priv->qp_num;
Sean Hefty9b2e9c02006-11-30 16:30:47 -08002330 req.qp_type = IB_QPT_RC;
Sean Heftye51060f2006-06-17 20:37:29 -07002331 req.starting_psn = id_priv->seq_num;
2332 req.responder_resources = conn_param->responder_resources;
2333 req.initiator_depth = conn_param->initiator_depth;
2334 req.flow_control = conn_param->flow_control;
2335 req.retry_count = conn_param->retry_count;
2336 req.rnr_retry_count = conn_param->rnr_retry_count;
2337 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2338 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2339 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2340 req.srq = id_priv->srq ? 1 : 0;
2341
2342 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2343out:
Krishna Kumar675a0272006-09-29 11:47:06 -07002344 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2345 ib_destroy_cm_id(id_priv->cm_id.ib);
2346 id_priv->cm_id.ib = NULL;
2347 }
2348
Sean Heftye51060f2006-06-17 20:37:29 -07002349 kfree(private_data);
2350 return ret;
2351}
2352
Tom Tucker07ebafb2006-08-03 16:02:42 -05002353static int cma_connect_iw(struct rdma_id_private *id_priv,
2354 struct rdma_conn_param *conn_param)
2355{
2356 struct iw_cm_id *cm_id;
2357 struct sockaddr_in* sin;
2358 int ret;
2359 struct iw_cm_conn_param iw_param;
2360
2361 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2362 if (IS_ERR(cm_id)) {
2363 ret = PTR_ERR(cm_id);
2364 goto out;
2365 }
2366
2367 id_priv->cm_id.iw = cm_id;
2368
2369 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2370 cm_id->local_addr = *sin;
2371
2372 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2373 cm_id->remote_addr = *sin;
2374
Sean Hefty5851bb82008-01-04 10:47:12 -08002375 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07002376 if (ret)
2377 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002378
2379 iw_param.ord = conn_param->initiator_depth;
2380 iw_param.ird = conn_param->responder_resources;
2381 iw_param.private_data = conn_param->private_data;
2382 iw_param.private_data_len = conn_param->private_data_len;
2383 if (id_priv->id.qp)
2384 iw_param.qpn = id_priv->qp_num;
2385 else
2386 iw_param.qpn = conn_param->qp_num;
2387 ret = iw_cm_connect(cm_id, &iw_param);
2388out:
Krishna Kumar675a0272006-09-29 11:47:06 -07002389 if (ret && !IS_ERR(cm_id)) {
2390 iw_destroy_cm_id(cm_id);
2391 id_priv->cm_id.iw = NULL;
2392 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002393 return ret;
2394}
2395
Sean Heftye51060f2006-06-17 20:37:29 -07002396int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2397{
2398 struct rdma_id_private *id_priv;
2399 int ret;
2400
2401 id_priv = container_of(id, struct rdma_id_private, id);
2402 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2403 return -EINVAL;
2404
2405 if (!id->qp) {
2406 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002407 id_priv->srq = conn_param->srq;
2408 }
2409
Tom Tucker07ebafb2006-08-03 16:02:42 -05002410 switch (rdma_node_get_transport(id->device->node_type)) {
2411 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -08002412 if (cma_is_ud_ps(id->ps))
Sean Hefty628e5f62006-11-30 16:44:16 -08002413 ret = cma_resolve_ib_udp(id_priv, conn_param);
2414 else
2415 ret = cma_connect_ib(id_priv, conn_param);
Sean Heftye51060f2006-06-17 20:37:29 -07002416 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002417 case RDMA_TRANSPORT_IWARP:
2418 ret = cma_connect_iw(id_priv, conn_param);
2419 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002420 default:
2421 ret = -ENOSYS;
2422 break;
2423 }
2424 if (ret)
2425 goto err;
2426
2427 return 0;
2428err:
2429 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2430 return ret;
2431}
2432EXPORT_SYMBOL(rdma_connect);
2433
2434static int cma_accept_ib(struct rdma_id_private *id_priv,
2435 struct rdma_conn_param *conn_param)
2436{
2437 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08002438 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002439
Sean Hefty5851bb82008-01-04 10:47:12 -08002440 ret = cma_modify_qp_rtr(id_priv, conn_param);
2441 if (ret)
2442 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08002443
Sean Hefty5851bb82008-01-04 10:47:12 -08002444 ret = cma_modify_qp_rts(id_priv, conn_param);
2445 if (ret)
2446 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002447
2448 memset(&rep, 0, sizeof rep);
2449 rep.qp_num = id_priv->qp_num;
2450 rep.starting_psn = id_priv->seq_num;
2451 rep.private_data = conn_param->private_data;
2452 rep.private_data_len = conn_param->private_data_len;
2453 rep.responder_resources = conn_param->responder_resources;
2454 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07002455 rep.failover_accepted = 0;
2456 rep.flow_control = conn_param->flow_control;
2457 rep.rnr_retry_count = conn_param->rnr_retry_count;
2458 rep.srq = id_priv->srq ? 1 : 0;
2459
Sean Hefty0fe313b2006-11-30 16:37:15 -08002460 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2461out:
2462 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002463}
2464
Tom Tucker07ebafb2006-08-03 16:02:42 -05002465static int cma_accept_iw(struct rdma_id_private *id_priv,
2466 struct rdma_conn_param *conn_param)
2467{
2468 struct iw_cm_conn_param iw_param;
2469 int ret;
2470
Sean Hefty5851bb82008-01-04 10:47:12 -08002471 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002472 if (ret)
2473 return ret;
2474
2475 iw_param.ord = conn_param->initiator_depth;
2476 iw_param.ird = conn_param->responder_resources;
2477 iw_param.private_data = conn_param->private_data;
2478 iw_param.private_data_len = conn_param->private_data_len;
2479 if (id_priv->id.qp) {
2480 iw_param.qpn = id_priv->qp_num;
2481 } else
2482 iw_param.qpn = conn_param->qp_num;
2483
2484 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2485}
2486
Sean Hefty628e5f62006-11-30 16:44:16 -08002487static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2488 enum ib_cm_sidr_status status,
2489 const void *private_data, int private_data_len)
2490{
2491 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002492 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002493
2494 memset(&rep, 0, sizeof rep);
2495 rep.status = status;
2496 if (status == IB_SIDR_SUCCESS) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002497 ret = cma_set_qkey(id_priv);
2498 if (ret)
2499 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002500 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002501 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08002502 }
2503 rep.private_data = private_data;
2504 rep.private_data_len = private_data_len;
2505
2506 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2507}
2508
Sean Heftye51060f2006-06-17 20:37:29 -07002509int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2510{
2511 struct rdma_id_private *id_priv;
2512 int ret;
2513
2514 id_priv = container_of(id, struct rdma_id_private, id);
2515 if (!cma_comp(id_priv, CMA_CONNECT))
2516 return -EINVAL;
2517
2518 if (!id->qp && conn_param) {
2519 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002520 id_priv->srq = conn_param->srq;
2521 }
2522
Tom Tucker07ebafb2006-08-03 16:02:42 -05002523 switch (rdma_node_get_transport(id->device->node_type)) {
2524 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -08002525 if (cma_is_ud_ps(id->ps))
Sean Hefty628e5f62006-11-30 16:44:16 -08002526 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2527 conn_param->private_data,
2528 conn_param->private_data_len);
2529 else if (conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -07002530 ret = cma_accept_ib(id_priv, conn_param);
2531 else
2532 ret = cma_rep_recv(id_priv);
2533 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002534 case RDMA_TRANSPORT_IWARP:
2535 ret = cma_accept_iw(id_priv, conn_param);
2536 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002537 default:
2538 ret = -ENOSYS;
2539 break;
2540 }
2541
2542 if (ret)
2543 goto reject;
2544
2545 return 0;
2546reject:
Sean Heftyc5483382007-09-24 13:19:09 -07002547 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002548 rdma_reject(id, NULL, 0);
2549 return ret;
2550}
2551EXPORT_SYMBOL(rdma_accept);
2552
Sean Hefty0fe313b2006-11-30 16:37:15 -08002553int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2554{
2555 struct rdma_id_private *id_priv;
2556 int ret;
2557
2558 id_priv = container_of(id, struct rdma_id_private, id);
Sean Hefty6c719f52007-05-07 11:49:27 -07002559 if (!cma_has_cm_dev(id_priv))
Sean Hefty0fe313b2006-11-30 16:37:15 -08002560 return -EINVAL;
2561
2562 switch (id->device->node_type) {
2563 case RDMA_NODE_IB_CA:
2564 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2565 break;
2566 default:
2567 ret = 0;
2568 break;
2569 }
2570 return ret;
2571}
2572EXPORT_SYMBOL(rdma_notify);
2573
Sean Heftye51060f2006-06-17 20:37:29 -07002574int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2575 u8 private_data_len)
2576{
2577 struct rdma_id_private *id_priv;
2578 int ret;
2579
2580 id_priv = container_of(id, struct rdma_id_private, id);
Sean Hefty6c719f52007-05-07 11:49:27 -07002581 if (!cma_has_cm_dev(id_priv))
Sean Heftye51060f2006-06-17 20:37:29 -07002582 return -EINVAL;
2583
Tom Tucker07ebafb2006-08-03 16:02:42 -05002584 switch (rdma_node_get_transport(id->device->node_type)) {
2585 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -08002586 if (cma_is_ud_ps(id->ps))
Sean Hefty628e5f62006-11-30 16:44:16 -08002587 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2588 private_data, private_data_len);
2589 else
2590 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2591 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2592 0, private_data, private_data_len);
Sean Heftye51060f2006-06-17 20:37:29 -07002593 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002594 case RDMA_TRANSPORT_IWARP:
2595 ret = iw_cm_reject(id_priv->cm_id.iw,
2596 private_data, private_data_len);
2597 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002598 default:
2599 ret = -ENOSYS;
2600 break;
2601 }
2602 return ret;
2603}
2604EXPORT_SYMBOL(rdma_reject);
2605
2606int rdma_disconnect(struct rdma_cm_id *id)
2607{
2608 struct rdma_id_private *id_priv;
2609 int ret;
2610
2611 id_priv = container_of(id, struct rdma_id_private, id);
Sean Hefty6c719f52007-05-07 11:49:27 -07002612 if (!cma_has_cm_dev(id_priv))
Sean Heftye51060f2006-06-17 20:37:29 -07002613 return -EINVAL;
2614
Tom Tucker07ebafb2006-08-03 16:02:42 -05002615 switch (rdma_node_get_transport(id->device->node_type)) {
2616 case RDMA_TRANSPORT_IB:
Sean Heftyc5483382007-09-24 13:19:09 -07002617 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002618 if (ret)
2619 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002620 /* Initiate or respond to a disconnect. */
2621 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2622 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2623 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002624 case RDMA_TRANSPORT_IWARP:
2625 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2626 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002627 default:
Tom Tucker07ebafb2006-08-03 16:02:42 -05002628 ret = -EINVAL;
Sean Heftye51060f2006-06-17 20:37:29 -07002629 break;
2630 }
2631out:
2632 return ret;
2633}
2634EXPORT_SYMBOL(rdma_disconnect);
2635
Sean Heftyc8f6a362007-02-15 17:00:18 -08002636static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2637{
2638 struct rdma_id_private *id_priv;
2639 struct cma_multicast *mc = multicast->context;
2640 struct rdma_cm_event event;
2641 int ret;
2642
2643 id_priv = mc->id_priv;
Or Gerlitzde910bd2008-07-14 23:48:53 -07002644 if (cma_disable_callback(id_priv, CMA_ADDR_BOUND) &&
2645 cma_disable_callback(id_priv, CMA_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07002646 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002647
Sean Heftyc5483382007-09-24 13:19:09 -07002648 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002649 if (!status && id_priv->id.qp)
2650 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2651 multicast->rec.mlid);
Sean Heftyc5483382007-09-24 13:19:09 -07002652 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002653
2654 memset(&event, 0, sizeof event);
2655 event.status = status;
2656 event.param.ud.private_data = mc->context;
2657 if (!status) {
2658 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2659 ib_init_ah_from_mcmember(id_priv->id.device,
2660 id_priv->id.port_num, &multicast->rec,
2661 &event.param.ud.ah_attr);
2662 event.param.ud.qp_num = 0xFFFFFF;
2663 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2664 } else
2665 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2666
2667 ret = id_priv->id.event_handler(&id_priv->id, &event);
2668 if (ret) {
2669 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002670 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002671 rdma_destroy_id(&id_priv->id);
2672 return 0;
2673 }
Sean Hefty8aa08602007-05-07 11:49:00 -07002674
Or Gerlitzde910bd2008-07-14 23:48:53 -07002675 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002676 return 0;
2677}
2678
2679static void cma_set_mgid(struct rdma_id_private *id_priv,
2680 struct sockaddr *addr, union ib_gid *mgid)
2681{
2682 unsigned char mc_map[MAX_ADDR_LEN];
2683 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2684 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2685 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2686
2687 if (cma_any_addr(addr)) {
2688 memset(mgid, 0, sizeof *mgid);
2689 } else if ((addr->sa_family == AF_INET6) &&
2690 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFF10A01B) ==
2691 0xFF10A01B)) {
2692 /* IPv6 address is an SA assigned MGID. */
2693 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2694 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07002695 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002696 if (id_priv->id.ps == RDMA_PS_UDP)
2697 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08002698 *mgid = *(union ib_gid *) (mc_map + 4);
2699 }
2700}
2701
2702static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2703 struct cma_multicast *mc)
2704{
2705 struct ib_sa_mcmember_rec rec;
2706 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2707 ib_sa_comp_mask comp_mask;
2708 int ret;
2709
2710 ib_addr_get_mgid(dev_addr, &rec.mgid);
2711 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2712 &rec.mgid, &rec);
2713 if (ret)
2714 return ret;
2715
Roland Dreier3f446752008-08-04 11:02:14 -07002716 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002717 if (id_priv->id.ps == RDMA_PS_UDP)
2718 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2719 ib_addr_get_sgid(dev_addr, &rec.port_gid);
2720 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2721 rec.join_state = 1;
2722
2723 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2724 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2725 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2726 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2727 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2728
Yossi Etigin84adeee2009-04-01 13:55:32 -07002729 if (id_priv->id.ps == RDMA_PS_IPOIB)
2730 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
2731 IB_SA_MCMEMBER_REC_RATE_SELECTOR;
2732
Sean Heftyc8f6a362007-02-15 17:00:18 -08002733 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2734 id_priv->id.port_num, &rec,
2735 comp_mask, GFP_KERNEL,
2736 cma_ib_mc_handler, mc);
2737 if (IS_ERR(mc->multicast.ib))
2738 return PTR_ERR(mc->multicast.ib);
2739
2740 return 0;
2741}
2742
2743int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
2744 void *context)
2745{
2746 struct rdma_id_private *id_priv;
2747 struct cma_multicast *mc;
2748 int ret;
2749
2750 id_priv = container_of(id, struct rdma_id_private, id);
2751 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
2752 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
2753 return -EINVAL;
2754
2755 mc = kmalloc(sizeof *mc, GFP_KERNEL);
2756 if (!mc)
2757 return -ENOMEM;
2758
2759 memcpy(&mc->addr, addr, ip_addr_size(addr));
2760 mc->context = context;
2761 mc->id_priv = id_priv;
2762
2763 spin_lock(&id_priv->lock);
2764 list_add(&mc->list, &id_priv->mc_list);
2765 spin_unlock(&id_priv->lock);
2766
2767 switch (rdma_node_get_transport(id->device->node_type)) {
2768 case RDMA_TRANSPORT_IB:
2769 ret = cma_join_ib_multicast(id_priv, mc);
2770 break;
2771 default:
2772 ret = -ENOSYS;
2773 break;
2774 }
2775
2776 if (ret) {
2777 spin_lock_irq(&id_priv->lock);
2778 list_del(&mc->list);
2779 spin_unlock_irq(&id_priv->lock);
2780 kfree(mc);
2781 }
2782 return ret;
2783}
2784EXPORT_SYMBOL(rdma_join_multicast);
2785
2786void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
2787{
2788 struct rdma_id_private *id_priv;
2789 struct cma_multicast *mc;
2790
2791 id_priv = container_of(id, struct rdma_id_private, id);
2792 spin_lock_irq(&id_priv->lock);
2793 list_for_each_entry(mc, &id_priv->mc_list, list) {
2794 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
2795 list_del(&mc->list);
2796 spin_unlock_irq(&id_priv->lock);
2797
2798 if (id->qp)
2799 ib_detach_mcast(id->qp,
2800 &mc->multicast.ib->rec.mgid,
2801 mc->multicast.ib->rec.mlid);
2802 ib_sa_free_multicast(mc->multicast.ib);
2803 kfree(mc);
2804 return;
2805 }
2806 }
2807 spin_unlock_irq(&id_priv->lock);
2808}
2809EXPORT_SYMBOL(rdma_leave_multicast);
2810
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002811static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
2812{
2813 struct rdma_dev_addr *dev_addr;
2814 struct cma_ndev_work *work;
2815
2816 dev_addr = &id_priv->id.route.addr.dev_addr;
2817
2818 if ((dev_addr->src_dev == ndev) &&
2819 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
2820 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
2821 ndev->name, &id_priv->id);
2822 work = kzalloc(sizeof *work, GFP_KERNEL);
2823 if (!work)
2824 return -ENOMEM;
2825
2826 INIT_WORK(&work->work, cma_ndev_work_handler);
2827 work->id = id_priv;
2828 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
2829 atomic_inc(&id_priv->refcount);
2830 queue_work(cma_wq, &work->work);
2831 }
2832
2833 return 0;
2834}
2835
2836static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
2837 void *ctx)
2838{
2839 struct net_device *ndev = (struct net_device *)ctx;
2840 struct cma_device *cma_dev;
2841 struct rdma_id_private *id_priv;
2842 int ret = NOTIFY_DONE;
2843
2844 if (dev_net(ndev) != &init_net)
2845 return NOTIFY_DONE;
2846
2847 if (event != NETDEV_BONDING_FAILOVER)
2848 return NOTIFY_DONE;
2849
2850 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
2851 return NOTIFY_DONE;
2852
2853 mutex_lock(&lock);
2854 list_for_each_entry(cma_dev, &dev_list, list)
2855 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
2856 ret = cma_netdev_change(ndev, id_priv);
2857 if (ret)
2858 goto out;
2859 }
2860
2861out:
2862 mutex_unlock(&lock);
2863 return ret;
2864}
2865
2866static struct notifier_block cma_nb = {
2867 .notifier_call = cma_netdev_callback
2868};
2869
Sean Heftye51060f2006-06-17 20:37:29 -07002870static void cma_add_one(struct ib_device *device)
2871{
2872 struct cma_device *cma_dev;
2873 struct rdma_id_private *id_priv;
2874
2875 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
2876 if (!cma_dev)
2877 return;
2878
2879 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07002880
2881 init_completion(&cma_dev->comp);
2882 atomic_set(&cma_dev->refcount, 1);
2883 INIT_LIST_HEAD(&cma_dev->id_list);
2884 ib_set_client_data(device, &cma_client, cma_dev);
2885
2886 mutex_lock(&lock);
2887 list_add_tail(&cma_dev->list, &dev_list);
2888 list_for_each_entry(id_priv, &listen_any_list, list)
2889 cma_listen_on_dev(id_priv, cma_dev);
2890 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07002891}
2892
2893static int cma_remove_id_dev(struct rdma_id_private *id_priv)
2894{
Sean Heftya1b1b612006-11-30 16:33:14 -08002895 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07002896 enum cma_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07002897 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07002898
2899 /* Record that we want to remove the device */
2900 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
2901 if (state == CMA_DESTROYING)
2902 return 0;
2903
2904 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002905 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07002906
2907 /* Check for destruction from another callback. */
2908 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07002909 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002910
Sean Heftya1b1b612006-11-30 16:33:14 -08002911 memset(&event, 0, sizeof event);
2912 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07002913 ret = id_priv->id.event_handler(&id_priv->id, &event);
2914out:
2915 mutex_unlock(&id_priv->handler_mutex);
2916 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002917}
2918
2919static void cma_process_remove(struct cma_device *cma_dev)
2920{
Sean Heftye51060f2006-06-17 20:37:29 -07002921 struct rdma_id_private *id_priv;
2922 int ret;
2923
Sean Heftye51060f2006-06-17 20:37:29 -07002924 mutex_lock(&lock);
2925 while (!list_empty(&cma_dev->id_list)) {
2926 id_priv = list_entry(cma_dev->id_list.next,
2927 struct rdma_id_private, list);
2928
Sean Heftyd02d1f52007-10-09 11:12:34 -07002929 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07002930 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07002931 atomic_inc(&id_priv->refcount);
2932 mutex_unlock(&lock);
2933
Sean Heftyd02d1f52007-10-09 11:12:34 -07002934 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002935 cma_deref_id(id_priv);
2936 if (ret)
2937 rdma_destroy_id(&id_priv->id);
2938
2939 mutex_lock(&lock);
2940 }
2941 mutex_unlock(&lock);
2942
2943 cma_deref_dev(cma_dev);
2944 wait_for_completion(&cma_dev->comp);
2945}
2946
2947static void cma_remove_one(struct ib_device *device)
2948{
2949 struct cma_device *cma_dev;
2950
2951 cma_dev = ib_get_client_data(device, &cma_client);
2952 if (!cma_dev)
2953 return;
2954
2955 mutex_lock(&lock);
2956 list_del(&cma_dev->list);
2957 mutex_unlock(&lock);
2958
2959 cma_process_remove(cma_dev);
2960 kfree(cma_dev);
2961}
2962
Peter Huewe716abb12009-06-23 10:38:42 -07002963static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07002964{
Anton Arapova25de532007-10-18 22:00:17 -07002965 int ret, low, high, remaining;
Sean Heftye51060f2006-06-17 20:37:29 -07002966
Sean Heftyaedec082007-01-29 16:41:23 -08002967 get_random_bytes(&next_port, sizeof next_port);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07002968 inet_get_local_port_range(&low, &high);
Anton Arapova25de532007-10-18 22:00:17 -07002969 remaining = (high - low) + 1;
2970 next_port = ((unsigned int) next_port % remaining) + low;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07002971
Sean Heftyc7f743a2007-02-01 12:23:37 -08002972 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07002973 if (!cma_wq)
2974 return -ENOMEM;
2975
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002976 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08002977 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002978 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002979
Sean Heftye51060f2006-06-17 20:37:29 -07002980 ret = ib_register_client(&cma_client);
2981 if (ret)
2982 goto err;
2983 return 0;
2984
2985err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002986 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08002987 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002988 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07002989 destroy_workqueue(cma_wq);
2990 return ret;
2991}
2992
Peter Huewe716abb12009-06-23 10:38:42 -07002993static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07002994{
2995 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07002996 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08002997 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002998 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07002999 destroy_workqueue(cma_wq);
3000 idr_destroy(&sdp_ps);
3001 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08003002 idr_destroy(&udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003003 idr_destroy(&ipoib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07003004}
3005
3006module_init(cma_init);
3007module_exit(cma_cleanup);