blob: 6884da24fde1e8338ef572aec075167faf5a73f0 [file] [log] [blame]
Sean Heftye51060f2006-06-17 20:37:29 -07001/*
2 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
3 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
4 * Copyright (c) 1999-2005, Mellanox Technologies, Inc. All rights reserved.
5 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
6 *
Sean Heftya9474912008-07-14 23:48:43 -07007 * This software is available to you under a choice of one of two
8 * licenses. You may choose to be licensed under the terms of the GNU
9 * General Public License (GPL) Version 2, available from the file
10 * COPYING in the main directory of this source tree, or the
11 * OpenIB.org BSD license below:
Sean Heftye51060f2006-06-17 20:37:29 -070012 *
Sean Heftya9474912008-07-14 23:48:43 -070013 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
Sean Heftye51060f2006-06-17 20:37:29 -070016 *
Sean Heftya9474912008-07-14 23:48:43 -070017 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer.
Sean Heftye51060f2006-06-17 20:37:29 -070020 *
Sean Heftya9474912008-07-14 23:48:43 -070021 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials
24 * provided with the distribution.
Sean Heftye51060f2006-06-17 20:37:29 -070025 *
Sean Heftya9474912008-07-14 23:48:43 -070026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * SOFTWARE.
Sean Heftye51060f2006-06-17 20:37:29 -070034 */
35
36#include <linux/completion.h>
37#include <linux/in.h>
38#include <linux/in6.h>
39#include <linux/mutex.h>
40#include <linux/random.h>
41#include <linux/idr.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050042#include <linux/inetdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Sean Heftye51060f2006-06-17 20:37:29 -070044
45#include <net/tcp.h>
Aleksey Senin1f5175a2008-12-24 10:16:45 -080046#include <net/ipv6.h>
Sean Heftye51060f2006-06-17 20:37:29 -070047
48#include <rdma/rdma_cm.h>
49#include <rdma/rdma_cm_ib.h>
50#include <rdma/ib_cache.h>
51#include <rdma/ib_cm.h>
52#include <rdma/ib_sa.h>
Tom Tucker07ebafb2006-08-03 16:02:42 -050053#include <rdma/iw_cm.h>
Sean Heftye51060f2006-06-17 20:37:29 -070054
55MODULE_AUTHOR("Sean Hefty");
56MODULE_DESCRIPTION("Generic RDMA CM Agent");
57MODULE_LICENSE("Dual BSD/GPL");
58
59#define CMA_CM_RESPONSE_TIMEOUT 20
Michael S. Tsirkind5bb7592006-09-13 15:01:54 +030060#define CMA_MAX_CM_RETRIES 15
Sean Heftydcb3f972007-08-01 14:47:16 -070061#define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24)
Eli Cohen3c86aa72010-10-13 21:26:51 +020062#define CMA_IBOE_PACKET_LIFETIME 18
Sean Heftye51060f2006-06-17 20:37:29 -070063
64static void cma_add_one(struct ib_device *device);
65static void cma_remove_one(struct ib_device *device);
66
67static struct ib_client cma_client = {
68 .name = "cma",
69 .add = cma_add_one,
70 .remove = cma_remove_one
71};
72
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070073static struct ib_sa_client sa_client;
Sean Hefty7a118df2006-10-31 11:12:59 -080074static struct rdma_addr_client addr_client;
Sean Heftye51060f2006-06-17 20:37:29 -070075static LIST_HEAD(dev_list);
76static LIST_HEAD(listen_any_list);
77static DEFINE_MUTEX(lock);
78static struct workqueue_struct *cma_wq;
79static DEFINE_IDR(sdp_ps);
80static DEFINE_IDR(tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -080081static DEFINE_IDR(udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -080082static DEFINE_IDR(ipoib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -070083
84struct cma_device {
85 struct list_head list;
86 struct ib_device *device;
Sean Heftye51060f2006-06-17 20:37:29 -070087 struct completion comp;
88 atomic_t refcount;
89 struct list_head id_list;
90};
91
92enum cma_state {
93 CMA_IDLE,
94 CMA_ADDR_QUERY,
95 CMA_ADDR_RESOLVED,
96 CMA_ROUTE_QUERY,
97 CMA_ROUTE_RESOLVED,
98 CMA_CONNECT,
99 CMA_DISCONNECT,
100 CMA_ADDR_BOUND,
101 CMA_LISTEN,
102 CMA_DEVICE_REMOVAL,
103 CMA_DESTROYING
104};
105
106struct rdma_bind_list {
107 struct idr *ps;
108 struct hlist_head owners;
109 unsigned short port;
110};
111
112/*
113 * Device removal can occur at anytime, so we need extra handling to
114 * serialize notifying the user of device removal with other callbacks.
115 * We do this by disabling removal notification while a callback is in process,
116 * and reporting it after the callback completes.
117 */
118struct rdma_id_private {
119 struct rdma_cm_id id;
120
121 struct rdma_bind_list *bind_list;
122 struct hlist_node node;
Sean Heftyd02d1f52007-10-09 11:12:34 -0700123 struct list_head list; /* listen_any_list or cma_device.list */
124 struct list_head listen_list; /* per device listens */
Sean Heftye51060f2006-06-17 20:37:29 -0700125 struct cma_device *cma_dev;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800126 struct list_head mc_list;
Sean Heftye51060f2006-06-17 20:37:29 -0700127
Sean Heftyd02d1f52007-10-09 11:12:34 -0700128 int internal_id;
Sean Heftye51060f2006-06-17 20:37:29 -0700129 enum cma_state state;
130 spinlock_t lock;
Sean Heftyc5483382007-09-24 13:19:09 -0700131 struct mutex qp_mutex;
132
Sean Heftye51060f2006-06-17 20:37:29 -0700133 struct completion comp;
134 atomic_t refcount;
Or Gerlitzde910bd2008-07-14 23:48:53 -0700135 struct mutex handler_mutex;
Sean Heftye51060f2006-06-17 20:37:29 -0700136
137 int backlog;
138 int timeout_ms;
139 struct ib_sa_query *query;
140 int query_id;
141 union {
142 struct ib_cm_id *ib;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500143 struct iw_cm_id *iw;
Sean Heftye51060f2006-06-17 20:37:29 -0700144 } cm_id;
145
146 u32 seq_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800147 u32 qkey;
Sean Heftye51060f2006-06-17 20:37:29 -0700148 u32 qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700149 u8 srq;
Sean Heftya81c9942007-08-08 15:51:06 -0700150 u8 tos;
Sean Heftye51060f2006-06-17 20:37:29 -0700151};
152
Sean Heftyc8f6a362007-02-15 17:00:18 -0800153struct cma_multicast {
154 struct rdma_id_private *id_priv;
155 union {
156 struct ib_sa_multicast *ib;
157 } multicast;
158 struct list_head list;
159 void *context;
Roland Dreier3f446752008-08-04 11:02:14 -0700160 struct sockaddr_storage addr;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200161 struct kref mcref;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800162};
163
Sean Heftye51060f2006-06-17 20:37:29 -0700164struct cma_work {
165 struct work_struct work;
166 struct rdma_id_private *id;
167 enum cma_state old_state;
168 enum cma_state new_state;
169 struct rdma_cm_event event;
170};
171
Or Gerlitzdd5bdff2008-07-22 14:14:22 -0700172struct cma_ndev_work {
173 struct work_struct work;
174 struct rdma_id_private *id;
175 struct rdma_cm_event event;
176};
177
Eli Cohen3c86aa72010-10-13 21:26:51 +0200178struct iboe_mcast_work {
179 struct work_struct work;
180 struct rdma_id_private *id;
181 struct cma_multicast *mc;
182};
183
Sean Heftye51060f2006-06-17 20:37:29 -0700184union cma_ip_addr {
185 struct in6_addr ip6;
186 struct {
Al Viro1b90c132008-03-29 03:10:28 +0000187 __be32 pad[3];
188 __be32 addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700189 } ip4;
190};
191
192struct cma_hdr {
193 u8 cma_version;
194 u8 ip_version; /* IP version: 7:4 */
Al Viro1b90c132008-03-29 03:10:28 +0000195 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -0700196 union cma_ip_addr src_addr;
197 union cma_ip_addr dst_addr;
198};
199
200struct sdp_hh {
201 u8 bsdh[16];
202 u8 sdp_version; /* Major version: 7:4 */
203 u8 ip_version; /* IP version: 7:4 */
204 u8 sdp_specific1[10];
Al Viro1b90c132008-03-29 03:10:28 +0000205 __be16 port;
206 __be16 sdp_specific2;
Sean Heftye51060f2006-06-17 20:37:29 -0700207 union cma_ip_addr src_addr;
208 union cma_ip_addr dst_addr;
209};
210
211struct sdp_hah {
212 u8 bsdh[16];
213 u8 sdp_version;
214};
215
216#define CMA_VERSION 0x00
217#define SDP_MAJ_VERSION 0x2
218
219static int cma_comp(struct rdma_id_private *id_priv, enum cma_state comp)
220{
221 unsigned long flags;
222 int ret;
223
224 spin_lock_irqsave(&id_priv->lock, flags);
225 ret = (id_priv->state == comp);
226 spin_unlock_irqrestore(&id_priv->lock, flags);
227 return ret;
228}
229
230static int cma_comp_exch(struct rdma_id_private *id_priv,
231 enum cma_state comp, enum cma_state exch)
232{
233 unsigned long flags;
234 int ret;
235
236 spin_lock_irqsave(&id_priv->lock, flags);
237 if ((ret = (id_priv->state == comp)))
238 id_priv->state = exch;
239 spin_unlock_irqrestore(&id_priv->lock, flags);
240 return ret;
241}
242
243static enum cma_state cma_exch(struct rdma_id_private *id_priv,
244 enum cma_state exch)
245{
246 unsigned long flags;
247 enum cma_state old;
248
249 spin_lock_irqsave(&id_priv->lock, flags);
250 old = id_priv->state;
251 id_priv->state = exch;
252 spin_unlock_irqrestore(&id_priv->lock, flags);
253 return old;
254}
255
256static inline u8 cma_get_ip_ver(struct cma_hdr *hdr)
257{
258 return hdr->ip_version >> 4;
259}
260
261static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver)
262{
263 hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF);
264}
265
266static inline u8 sdp_get_majv(u8 sdp_version)
267{
268 return sdp_version >> 4;
269}
270
271static inline u8 sdp_get_ip_ver(struct sdp_hh *hh)
272{
273 return hh->ip_version >> 4;
274}
275
276static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver)
277{
278 hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF);
279}
280
Sean Heftyc8f6a362007-02-15 17:00:18 -0800281static inline int cma_is_ud_ps(enum rdma_port_space ps)
282{
283 return (ps == RDMA_PS_UDP || ps == RDMA_PS_IPOIB);
284}
285
Sean Heftye51060f2006-06-17 20:37:29 -0700286static void cma_attach_to_dev(struct rdma_id_private *id_priv,
287 struct cma_device *cma_dev)
288{
289 atomic_inc(&cma_dev->refcount);
290 id_priv->cma_dev = cma_dev;
291 id_priv->id.device = cma_dev->device;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200292 id_priv->id.route.addr.dev_addr.transport =
293 rdma_node_get_transport(cma_dev->device->node_type);
Sean Heftye51060f2006-06-17 20:37:29 -0700294 list_add_tail(&id_priv->list, &cma_dev->id_list);
295}
296
297static inline void cma_deref_dev(struct cma_device *cma_dev)
298{
299 if (atomic_dec_and_test(&cma_dev->refcount))
300 complete(&cma_dev->comp);
301}
302
Eli Cohen3c86aa72010-10-13 21:26:51 +0200303static inline void release_mc(struct kref *kref)
304{
305 struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref);
306
307 kfree(mc->multicast.ib);
308 kfree(mc);
309}
310
Sean Heftye51060f2006-06-17 20:37:29 -0700311static void cma_detach_from_dev(struct rdma_id_private *id_priv)
312{
313 list_del(&id_priv->list);
314 cma_deref_dev(id_priv->cma_dev);
315 id_priv->cma_dev = NULL;
316}
317
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700318static int cma_set_qkey(struct rdma_id_private *id_priv)
Sean Heftyc8f6a362007-02-15 17:00:18 -0800319{
320 struct ib_sa_mcmember_rec rec;
321 int ret = 0;
322
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700323 if (id_priv->qkey)
324 return 0;
325
326 switch (id_priv->id.ps) {
Sean Heftyc8f6a362007-02-15 17:00:18 -0800327 case RDMA_PS_UDP:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700328 id_priv->qkey = RDMA_UDP_QKEY;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800329 break;
330 case RDMA_PS_IPOIB:
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700331 ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid);
332 ret = ib_sa_get_mcmember_rec(id_priv->id.device,
333 id_priv->id.port_num, &rec.mgid,
334 &rec);
335 if (!ret)
336 id_priv->qkey = be32_to_cpu(rec.qkey);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800337 break;
338 default:
339 break;
340 }
341 return ret;
342}
343
Eli Cohen3c86aa72010-10-13 21:26:51 +0200344static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num)
345{
346 int i;
347 int err;
348 struct ib_port_attr props;
349 union ib_gid tmp;
350
351 err = ib_query_port(device, port_num, &props);
352 if (err)
353 return 1;
354
355 for (i = 0; i < props.gid_tbl_len; ++i) {
356 err = ib_query_gid(device, port_num, i, &tmp);
357 if (err)
358 return 1;
359 if (!memcmp(&tmp, gid, sizeof tmp))
360 return 0;
361 }
362
363 return -EAGAIN;
364}
365
Tom Tucker07ebafb2006-08-03 16:02:42 -0500366static int cma_acquire_dev(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700367{
Sean Heftyc8f6a362007-02-15 17:00:18 -0800368 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
Sean Heftye51060f2006-06-17 20:37:29 -0700369 struct cma_device *cma_dev;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200370 union ib_gid gid, iboe_gid;
Sean Heftye51060f2006-06-17 20:37:29 -0700371 int ret = -ENODEV;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200372 u8 port;
373 enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ?
374 IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET;
Sean Heftye51060f2006-06-17 20:37:29 -0700375
Eli Cohen3c86aa72010-10-13 21:26:51 +0200376 iboe_addr_get_sgid(dev_addr, &iboe_gid);
377 memcpy(&gid, dev_addr->src_dev_addr +
378 rdma_addr_gid_offset(dev_addr), sizeof gid);
Sean Heftye51060f2006-06-17 20:37:29 -0700379 list_for_each_entry(cma_dev, &dev_list, list) {
Eli Cohen3c86aa72010-10-13 21:26:51 +0200380 for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) {
381 if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) {
382 if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB &&
383 rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET)
384 ret = find_gid_port(cma_dev->device, &iboe_gid, port);
385 else
386 ret = find_gid_port(cma_dev->device, &gid, port);
387
388 if (!ret) {
389 id_priv->id.port_num = port;
390 goto out;
391 } else if (ret == 1)
392 break;
393 }
Sean Heftye51060f2006-06-17 20:37:29 -0700394 }
395 }
Eli Cohen3c86aa72010-10-13 21:26:51 +0200396
397out:
398 if (!ret)
399 cma_attach_to_dev(id_priv, cma_dev);
400
Sean Heftye51060f2006-06-17 20:37:29 -0700401 return ret;
402}
403
Sean Heftye51060f2006-06-17 20:37:29 -0700404static void cma_deref_id(struct rdma_id_private *id_priv)
405{
406 if (atomic_dec_and_test(&id_priv->refcount))
407 complete(&id_priv->comp);
408}
409
Or Gerlitzde910bd2008-07-14 23:48:53 -0700410static int cma_disable_callback(struct rdma_id_private *id_priv,
Sean Hefty8aa08602007-05-07 11:49:00 -0700411 enum cma_state state)
412{
Or Gerlitzde910bd2008-07-14 23:48:53 -0700413 mutex_lock(&id_priv->handler_mutex);
414 if (id_priv->state != state) {
415 mutex_unlock(&id_priv->handler_mutex);
416 return -EINVAL;
417 }
418 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700419}
420
Sean Hefty6c719f52007-05-07 11:49:27 -0700421static int cma_has_cm_dev(struct rdma_id_private *id_priv)
422{
423 return (id_priv->id.device && id_priv->cm_id.ib);
424}
425
Sean Heftye51060f2006-06-17 20:37:29 -0700426struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler,
427 void *context, enum rdma_port_space ps)
428{
429 struct rdma_id_private *id_priv;
430
431 id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL);
432 if (!id_priv)
433 return ERR_PTR(-ENOMEM);
434
435 id_priv->state = CMA_IDLE;
436 id_priv->id.context = context;
437 id_priv->id.event_handler = event_handler;
438 id_priv->id.ps = ps;
439 spin_lock_init(&id_priv->lock);
Sean Heftyc5483382007-09-24 13:19:09 -0700440 mutex_init(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700441 init_completion(&id_priv->comp);
442 atomic_set(&id_priv->refcount, 1);
Or Gerlitzde910bd2008-07-14 23:48:53 -0700443 mutex_init(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700444 INIT_LIST_HEAD(&id_priv->listen_list);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800445 INIT_LIST_HEAD(&id_priv->mc_list);
Sean Heftye51060f2006-06-17 20:37:29 -0700446 get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num);
447
448 return &id_priv->id;
449}
450EXPORT_SYMBOL(rdma_create_id);
451
Sean Heftyc8f6a362007-02-15 17:00:18 -0800452static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Sean Heftye51060f2006-06-17 20:37:29 -0700453{
454 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800455 int qp_attr_mask, ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700456
Sean Heftyc8f6a362007-02-15 17:00:18 -0800457 qp_attr.qp_state = IB_QPS_INIT;
458 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700459 if (ret)
460 return ret;
461
Sean Heftyc8f6a362007-02-15 17:00:18 -0800462 ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask);
463 if (ret)
464 return ret;
465
466 qp_attr.qp_state = IB_QPS_RTR;
467 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
468 if (ret)
469 return ret;
470
471 qp_attr.qp_state = IB_QPS_RTS;
472 qp_attr.sq_psn = 0;
473 ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
474
475 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700476}
477
Sean Heftyc8f6a362007-02-15 17:00:18 -0800478static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp)
Tom Tucker07ebafb2006-08-03 16:02:42 -0500479{
480 struct ib_qp_attr qp_attr;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800481 int qp_attr_mask, ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500482
483 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800484 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
485 if (ret)
486 return ret;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500487
Sean Heftyc8f6a362007-02-15 17:00:18 -0800488 return ib_modify_qp(qp, &qp_attr, qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500489}
490
Sean Heftye51060f2006-06-17 20:37:29 -0700491int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd,
492 struct ib_qp_init_attr *qp_init_attr)
493{
494 struct rdma_id_private *id_priv;
495 struct ib_qp *qp;
496 int ret;
497
498 id_priv = container_of(id, struct rdma_id_private, id);
499 if (id->device != pd->device)
500 return -EINVAL;
501
502 qp = ib_create_qp(pd, qp_init_attr);
503 if (IS_ERR(qp))
504 return PTR_ERR(qp);
505
Sean Heftyc8f6a362007-02-15 17:00:18 -0800506 if (cma_is_ud_ps(id_priv->id.ps))
507 ret = cma_init_ud_qp(id_priv, qp);
508 else
509 ret = cma_init_conn_qp(id_priv, qp);
Sean Heftye51060f2006-06-17 20:37:29 -0700510 if (ret)
511 goto err;
512
513 id->qp = qp;
514 id_priv->qp_num = qp->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -0700515 id_priv->srq = (qp->srq != NULL);
516 return 0;
517err:
518 ib_destroy_qp(qp);
519 return ret;
520}
521EXPORT_SYMBOL(rdma_create_qp);
522
523void rdma_destroy_qp(struct rdma_cm_id *id)
524{
Sean Heftyc5483382007-09-24 13:19:09 -0700525 struct rdma_id_private *id_priv;
526
527 id_priv = container_of(id, struct rdma_id_private, id);
528 mutex_lock(&id_priv->qp_mutex);
529 ib_destroy_qp(id_priv->id.qp);
530 id_priv->id.qp = NULL;
531 mutex_unlock(&id_priv->qp_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -0700532}
533EXPORT_SYMBOL(rdma_destroy_qp);
534
Sean Hefty5851bb82008-01-04 10:47:12 -0800535static int cma_modify_qp_rtr(struct rdma_id_private *id_priv,
536 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700537{
538 struct ib_qp_attr qp_attr;
539 int qp_attr_mask, ret;
540
Sean Heftyc5483382007-09-24 13:19:09 -0700541 mutex_lock(&id_priv->qp_mutex);
542 if (!id_priv->id.qp) {
543 ret = 0;
544 goto out;
545 }
Sean Heftye51060f2006-06-17 20:37:29 -0700546
547 /* Need to update QP attributes from default values. */
548 qp_attr.qp_state = IB_QPS_INIT;
Sean Heftyc5483382007-09-24 13:19:09 -0700549 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700550 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700551 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700552
Sean Heftyc5483382007-09-24 13:19:09 -0700553 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700554 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700555 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700556
557 qp_attr.qp_state = IB_QPS_RTR;
Sean Heftyc5483382007-09-24 13:19:09 -0700558 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700559 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700560 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700561
Sean Hefty5851bb82008-01-04 10:47:12 -0800562 if (conn_param)
563 qp_attr.max_dest_rd_atomic = conn_param->responder_resources;
Sean Heftyc5483382007-09-24 13:19:09 -0700564 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
565out:
566 mutex_unlock(&id_priv->qp_mutex);
567 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700568}
569
Sean Hefty5851bb82008-01-04 10:47:12 -0800570static int cma_modify_qp_rts(struct rdma_id_private *id_priv,
571 struct rdma_conn_param *conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -0700572{
573 struct ib_qp_attr qp_attr;
574 int qp_attr_mask, ret;
575
Sean Heftyc5483382007-09-24 13:19:09 -0700576 mutex_lock(&id_priv->qp_mutex);
577 if (!id_priv->id.qp) {
578 ret = 0;
579 goto out;
580 }
Sean Heftye51060f2006-06-17 20:37:29 -0700581
582 qp_attr.qp_state = IB_QPS_RTS;
Sean Heftyc5483382007-09-24 13:19:09 -0700583 ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700584 if (ret)
Sean Heftyc5483382007-09-24 13:19:09 -0700585 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -0700586
Sean Hefty5851bb82008-01-04 10:47:12 -0800587 if (conn_param)
588 qp_attr.max_rd_atomic = conn_param->initiator_depth;
Sean Heftyc5483382007-09-24 13:19:09 -0700589 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask);
590out:
591 mutex_unlock(&id_priv->qp_mutex);
592 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700593}
594
Sean Heftyc5483382007-09-24 13:19:09 -0700595static int cma_modify_qp_err(struct rdma_id_private *id_priv)
Sean Heftye51060f2006-06-17 20:37:29 -0700596{
597 struct ib_qp_attr qp_attr;
Sean Heftyc5483382007-09-24 13:19:09 -0700598 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700599
Sean Heftyc5483382007-09-24 13:19:09 -0700600 mutex_lock(&id_priv->qp_mutex);
601 if (!id_priv->id.qp) {
602 ret = 0;
603 goto out;
604 }
Sean Heftye51060f2006-06-17 20:37:29 -0700605
606 qp_attr.qp_state = IB_QPS_ERR;
Sean Heftyc5483382007-09-24 13:19:09 -0700607 ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE);
608out:
609 mutex_unlock(&id_priv->qp_mutex);
610 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -0700611}
612
Sean Heftyc8f6a362007-02-15 17:00:18 -0800613static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv,
614 struct ib_qp_attr *qp_attr, int *qp_attr_mask)
615{
616 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
617 int ret;
Eli Cohen3c86aa72010-10-13 21:26:51 +0200618 u16 pkey;
619
620 if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) ==
621 IB_LINK_LAYER_INFINIBAND)
622 pkey = ib_addr_get_pkey(dev_addr);
623 else
624 pkey = 0xffff;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800625
626 ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num,
Eli Cohen3c86aa72010-10-13 21:26:51 +0200627 pkey, &qp_attr->pkey_index);
Sean Heftyc8f6a362007-02-15 17:00:18 -0800628 if (ret)
629 return ret;
630
631 qp_attr->port_num = id_priv->id.port_num;
632 *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT;
633
634 if (cma_is_ud_ps(id_priv->id.ps)) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -0700635 ret = cma_set_qkey(id_priv);
636 if (ret)
637 return ret;
638
Sean Heftyc8f6a362007-02-15 17:00:18 -0800639 qp_attr->qkey = id_priv->qkey;
640 *qp_attr_mask |= IB_QP_QKEY;
641 } else {
642 qp_attr->qp_access_flags = 0;
643 *qp_attr_mask |= IB_QP_ACCESS_FLAGS;
644 }
645 return 0;
646}
647
Sean Heftye51060f2006-06-17 20:37:29 -0700648int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr,
649 int *qp_attr_mask)
650{
651 struct rdma_id_private *id_priv;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800652 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700653
654 id_priv = container_of(id, struct rdma_id_private, id);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500655 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
656 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800657 if (!id_priv->cm_id.ib || cma_is_ud_ps(id_priv->id.ps))
658 ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask);
659 else
660 ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr,
661 qp_attr_mask);
Sean Heftye51060f2006-06-17 20:37:29 -0700662 if (qp_attr->qp_state == IB_QPS_RTR)
663 qp_attr->rq_psn = id_priv->seq_num;
664 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500665 case RDMA_TRANSPORT_IWARP:
Sean Heftyc8f6a362007-02-15 17:00:18 -0800666 if (!id_priv->cm_id.iw) {
Dotan Barak8f076532007-07-17 17:58:57 +0300667 qp_attr->qp_access_flags = 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -0800668 *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS;
669 } else
670 ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr,
671 qp_attr_mask);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500672 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700673 default:
674 ret = -ENOSYS;
675 break;
676 }
677
678 return ret;
679}
680EXPORT_SYMBOL(rdma_init_qp_attr);
681
682static inline int cma_zero_addr(struct sockaddr *addr)
683{
684 struct in6_addr *ip6;
685
686 if (addr->sa_family == AF_INET)
Joe Perches6360a022007-12-16 13:47:33 -0800687 return ipv4_is_zeronet(
688 ((struct sockaddr_in *)addr)->sin_addr.s_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700689 else {
690 ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr;
691 return (ip6->s6_addr32[0] | ip6->s6_addr32[1] |
Eric Sesterhenn5fd571c2006-06-21 20:56:26 +0200692 ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700693 }
694}
695
696static inline int cma_loopback_addr(struct sockaddr *addr)
697{
Aleksey Senin1f5175a2008-12-24 10:16:45 -0800698 if (addr->sa_family == AF_INET)
699 return ipv4_is_loopback(
700 ((struct sockaddr_in *) addr)->sin_addr.s_addr);
701 else
702 return ipv6_addr_loopback(
703 &((struct sockaddr_in6 *) addr)->sin6_addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700704}
705
706static inline int cma_any_addr(struct sockaddr *addr)
707{
708 return cma_zero_addr(addr) || cma_loopback_addr(addr);
709}
710
Sean Hefty628e5f62006-11-30 16:44:16 -0800711static inline __be16 cma_port(struct sockaddr *addr)
712{
713 if (addr->sa_family == AF_INET)
714 return ((struct sockaddr_in *) addr)->sin_port;
715 else
716 return ((struct sockaddr_in6 *) addr)->sin6_port;
717}
718
Sean Heftye51060f2006-06-17 20:37:29 -0700719static inline int cma_any_port(struct sockaddr *addr)
720{
Sean Hefty628e5f62006-11-30 16:44:16 -0800721 return !cma_port(addr);
Sean Heftye51060f2006-06-17 20:37:29 -0700722}
723
724static int cma_get_net_info(void *hdr, enum rdma_port_space ps,
Al Viro1b90c132008-03-29 03:10:28 +0000725 u8 *ip_ver, __be16 *port,
Sean Heftye51060f2006-06-17 20:37:29 -0700726 union cma_ip_addr **src, union cma_ip_addr **dst)
727{
728 switch (ps) {
729 case RDMA_PS_SDP:
730 if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) !=
731 SDP_MAJ_VERSION)
732 return -EINVAL;
733
734 *ip_ver = sdp_get_ip_ver(hdr);
735 *port = ((struct sdp_hh *) hdr)->port;
736 *src = &((struct sdp_hh *) hdr)->src_addr;
737 *dst = &((struct sdp_hh *) hdr)->dst_addr;
738 break;
739 default:
740 if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION)
741 return -EINVAL;
742
743 *ip_ver = cma_get_ip_ver(hdr);
744 *port = ((struct cma_hdr *) hdr)->port;
745 *src = &((struct cma_hdr *) hdr)->src_addr;
746 *dst = &((struct cma_hdr *) hdr)->dst_addr;
747 break;
748 }
749
750 if (*ip_ver != 4 && *ip_ver != 6)
751 return -EINVAL;
752 return 0;
753}
754
755static void cma_save_net_info(struct rdma_addr *addr,
756 struct rdma_addr *listen_addr,
Al Viro1b90c132008-03-29 03:10:28 +0000757 u8 ip_ver, __be16 port,
Sean Heftye51060f2006-06-17 20:37:29 -0700758 union cma_ip_addr *src, union cma_ip_addr *dst)
759{
760 struct sockaddr_in *listen4, *ip4;
761 struct sockaddr_in6 *listen6, *ip6;
762
763 switch (ip_ver) {
764 case 4:
765 listen4 = (struct sockaddr_in *) &listen_addr->src_addr;
766 ip4 = (struct sockaddr_in *) &addr->src_addr;
767 ip4->sin_family = listen4->sin_family;
768 ip4->sin_addr.s_addr = dst->ip4.addr;
769 ip4->sin_port = listen4->sin_port;
770
771 ip4 = (struct sockaddr_in *) &addr->dst_addr;
772 ip4->sin_family = listen4->sin_family;
773 ip4->sin_addr.s_addr = src->ip4.addr;
774 ip4->sin_port = port;
775 break;
776 case 6:
777 listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr;
778 ip6 = (struct sockaddr_in6 *) &addr->src_addr;
779 ip6->sin6_family = listen6->sin6_family;
780 ip6->sin6_addr = dst->ip6;
781 ip6->sin6_port = listen6->sin6_port;
782
783 ip6 = (struct sockaddr_in6 *) &addr->dst_addr;
784 ip6->sin6_family = listen6->sin6_family;
785 ip6->sin6_addr = src->ip6;
786 ip6->sin6_port = port;
787 break;
788 default:
789 break;
790 }
791}
792
793static inline int cma_user_data_offset(enum rdma_port_space ps)
794{
795 switch (ps) {
796 case RDMA_PS_SDP:
797 return 0;
798 default:
799 return sizeof(struct cma_hdr);
800 }
801}
802
Sean Heftye51060f2006-06-17 20:37:29 -0700803static void cma_cancel_route(struct rdma_id_private *id_priv)
804{
Eli Cohen3c86aa72010-10-13 21:26:51 +0200805 switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) {
806 case IB_LINK_LAYER_INFINIBAND:
Sean Heftye51060f2006-06-17 20:37:29 -0700807 if (id_priv->query)
808 ib_sa_cancel_query(id_priv->query_id, id_priv->query);
809 break;
810 default:
811 break;
812 }
813}
814
Sean Heftye51060f2006-06-17 20:37:29 -0700815static void cma_cancel_listens(struct rdma_id_private *id_priv)
816{
817 struct rdma_id_private *dev_id_priv;
818
Sean Heftyd02d1f52007-10-09 11:12:34 -0700819 /*
820 * Remove from listen_any_list to prevent added devices from spawning
821 * additional listen requests.
822 */
Sean Heftye51060f2006-06-17 20:37:29 -0700823 mutex_lock(&lock);
824 list_del(&id_priv->list);
825
826 while (!list_empty(&id_priv->listen_list)) {
827 dev_id_priv = list_entry(id_priv->listen_list.next,
828 struct rdma_id_private, listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -0700829 /* sync with device removal to avoid duplicate destruction */
830 list_del_init(&dev_id_priv->list);
831 list_del(&dev_id_priv->listen_list);
832 mutex_unlock(&lock);
833
834 rdma_destroy_id(&dev_id_priv->id);
835 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700836 }
837 mutex_unlock(&lock);
838}
839
840static void cma_cancel_operation(struct rdma_id_private *id_priv,
841 enum cma_state state)
842{
843 switch (state) {
844 case CMA_ADDR_QUERY:
845 rdma_addr_cancel(&id_priv->id.route.addr.dev_addr);
846 break;
847 case CMA_ROUTE_QUERY:
848 cma_cancel_route(id_priv);
849 break;
850 case CMA_LISTEN:
Roland Dreier3f446752008-08-04 11:02:14 -0700851 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr)
852 && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -0700853 cma_cancel_listens(id_priv);
854 break;
855 default:
856 break;
857 }
858}
859
860static void cma_release_port(struct rdma_id_private *id_priv)
861{
862 struct rdma_bind_list *bind_list = id_priv->bind_list;
863
864 if (!bind_list)
865 return;
866
867 mutex_lock(&lock);
868 hlist_del(&id_priv->node);
869 if (hlist_empty(&bind_list->owners)) {
870 idr_remove(bind_list->ps, bind_list->port);
871 kfree(bind_list);
872 }
873 mutex_unlock(&lock);
874}
875
Sean Heftyc8f6a362007-02-15 17:00:18 -0800876static void cma_leave_mc_groups(struct rdma_id_private *id_priv)
877{
878 struct cma_multicast *mc;
879
880 while (!list_empty(&id_priv->mc_list)) {
881 mc = container_of(id_priv->mc_list.next,
882 struct cma_multicast, list);
883 list_del(&mc->list);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200884 switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) {
885 case IB_LINK_LAYER_INFINIBAND:
886 ib_sa_free_multicast(mc->multicast.ib);
887 kfree(mc);
888 break;
889 case IB_LINK_LAYER_ETHERNET:
890 kref_put(&mc->mcref, release_mc);
891 break;
892 default:
893 break;
894 }
Sean Heftyc8f6a362007-02-15 17:00:18 -0800895 }
896}
897
Sean Heftye51060f2006-06-17 20:37:29 -0700898void rdma_destroy_id(struct rdma_cm_id *id)
899{
900 struct rdma_id_private *id_priv;
901 enum cma_state state;
902
903 id_priv = container_of(id, struct rdma_id_private, id);
904 state = cma_exch(id_priv, CMA_DESTROYING);
905 cma_cancel_operation(id_priv, state);
906
Sean Hefty61a73c72006-09-01 15:33:55 -0700907 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700908 if (id_priv->cma_dev) {
Sean Hefty61a73c72006-09-01 15:33:55 -0700909 mutex_unlock(&lock);
Eli Cohen3c86aa72010-10-13 21:26:51 +0200910 switch (rdma_node_get_transport(id_priv->id.device->node_type)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500911 case RDMA_TRANSPORT_IB:
Roland Dreier3cd96562006-09-22 15:22:46 -0700912 if (id_priv->cm_id.ib && !IS_ERR(id_priv->cm_id.ib))
Sean Heftye51060f2006-06-17 20:37:29 -0700913 ib_destroy_cm_id(id_priv->cm_id.ib);
914 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -0500915 case RDMA_TRANSPORT_IWARP:
916 if (id_priv->cm_id.iw && !IS_ERR(id_priv->cm_id.iw))
917 iw_destroy_cm_id(id_priv->cm_id.iw);
918 break;
Sean Heftye51060f2006-06-17 20:37:29 -0700919 default:
920 break;
921 }
Sean Heftyc8f6a362007-02-15 17:00:18 -0800922 cma_leave_mc_groups(id_priv);
Roland Dreier3cd96562006-09-22 15:22:46 -0700923 mutex_lock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700924 cma_detach_from_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700925 }
Sean Hefty61a73c72006-09-01 15:33:55 -0700926 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -0700927
928 cma_release_port(id_priv);
929 cma_deref_id(id_priv);
930 wait_for_completion(&id_priv->comp);
931
Sean Heftyd02d1f52007-10-09 11:12:34 -0700932 if (id_priv->internal_id)
933 cma_deref_id(id_priv->id.context);
934
Sean Heftye51060f2006-06-17 20:37:29 -0700935 kfree(id_priv->id.route.path_rec);
936 kfree(id_priv);
937}
938EXPORT_SYMBOL(rdma_destroy_id);
939
940static int cma_rep_recv(struct rdma_id_private *id_priv)
941{
942 int ret;
943
Sean Hefty5851bb82008-01-04 10:47:12 -0800944 ret = cma_modify_qp_rtr(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700945 if (ret)
946 goto reject;
947
Sean Hefty5851bb82008-01-04 10:47:12 -0800948 ret = cma_modify_qp_rts(id_priv, NULL);
Sean Heftye51060f2006-06-17 20:37:29 -0700949 if (ret)
950 goto reject;
951
952 ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0);
953 if (ret)
954 goto reject;
955
956 return 0;
957reject:
Sean Heftyc5483382007-09-24 13:19:09 -0700958 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -0700959 ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED,
960 NULL, 0, NULL, 0);
961 return ret;
962}
963
964static int cma_verify_rep(struct rdma_id_private *id_priv, void *data)
965{
966 if (id_priv->id.ps == RDMA_PS_SDP &&
967 sdp_get_majv(((struct sdp_hah *) data)->sdp_version) !=
968 SDP_MAJ_VERSION)
969 return -EINVAL;
970
971 return 0;
972}
973
Sean Heftya1b1b612006-11-30 16:33:14 -0800974static void cma_set_rep_event_data(struct rdma_cm_event *event,
975 struct ib_cm_rep_event_param *rep_data,
976 void *private_data)
977{
978 event->param.conn.private_data = private_data;
979 event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
980 event->param.conn.responder_resources = rep_data->responder_resources;
981 event->param.conn.initiator_depth = rep_data->initiator_depth;
982 event->param.conn.flow_control = rep_data->flow_control;
983 event->param.conn.rnr_retry_count = rep_data->rnr_retry_count;
984 event->param.conn.srq = rep_data->srq;
985 event->param.conn.qp_num = rep_data->remote_qpn;
986}
987
Sean Heftye51060f2006-06-17 20:37:29 -0700988static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
989{
990 struct rdma_id_private *id_priv = cm_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -0800991 struct rdma_cm_event event;
992 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700993
Amir Vadai38ca83a2008-07-22 14:14:23 -0700994 if ((ib_event->event != IB_CM_TIMEWAIT_EXIT &&
995 cma_disable_callback(id_priv, CMA_CONNECT)) ||
996 (ib_event->event == IB_CM_TIMEWAIT_EXIT &&
997 cma_disable_callback(id_priv, CMA_DISCONNECT)))
Sean Hefty8aa08602007-05-07 11:49:00 -0700998 return 0;
Sean Heftye51060f2006-06-17 20:37:29 -0700999
Sean Heftya1b1b612006-11-30 16:33:14 -08001000 memset(&event, 0, sizeof event);
Sean Heftye51060f2006-06-17 20:37:29 -07001001 switch (ib_event->event) {
1002 case IB_CM_REQ_ERROR:
1003 case IB_CM_REP_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001004 event.event = RDMA_CM_EVENT_UNREACHABLE;
1005 event.status = -ETIMEDOUT;
Sean Heftye51060f2006-06-17 20:37:29 -07001006 break;
1007 case IB_CM_REP_RECEIVED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001008 event.status = cma_verify_rep(id_priv, ib_event->private_data);
1009 if (event.status)
1010 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
Sean Heftye51060f2006-06-17 20:37:29 -07001011 else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) {
Sean Heftya1b1b612006-11-30 16:33:14 -08001012 event.status = cma_rep_recv(id_priv);
1013 event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR :
1014 RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001015 } else
Sean Heftya1b1b612006-11-30 16:33:14 -08001016 event.event = RDMA_CM_EVENT_CONNECT_RESPONSE;
1017 cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd,
1018 ib_event->private_data);
Sean Heftye51060f2006-06-17 20:37:29 -07001019 break;
1020 case IB_CM_RTU_RECEIVED:
Sean Hefty0fe313b2006-11-30 16:37:15 -08001021 case IB_CM_USER_ESTABLISHED:
1022 event.event = RDMA_CM_EVENT_ESTABLISHED;
Sean Heftye51060f2006-06-17 20:37:29 -07001023 break;
1024 case IB_CM_DREQ_ERROR:
Sean Heftya1b1b612006-11-30 16:33:14 -08001025 event.status = -ETIMEDOUT; /* fall through */
Sean Heftye51060f2006-06-17 20:37:29 -07001026 case IB_CM_DREQ_RECEIVED:
1027 case IB_CM_DREP_RECEIVED:
1028 if (!cma_comp_exch(id_priv, CMA_CONNECT, CMA_DISCONNECT))
1029 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001030 event.event = RDMA_CM_EVENT_DISCONNECTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001031 break;
1032 case IB_CM_TIMEWAIT_EXIT:
Amir Vadai38ca83a2008-07-22 14:14:23 -07001033 event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT;
1034 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001035 case IB_CM_MRA_RECEIVED:
1036 /* ignore event */
1037 goto out;
1038 case IB_CM_REJ_RECEIVED:
Sean Heftyc5483382007-09-24 13:19:09 -07001039 cma_modify_qp_err(id_priv);
Sean Heftya1b1b612006-11-30 16:33:14 -08001040 event.status = ib_event->param.rej_rcvd.reason;
1041 event.event = RDMA_CM_EVENT_REJECTED;
1042 event.param.conn.private_data = ib_event->private_data;
1043 event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
Sean Heftye51060f2006-06-17 20:37:29 -07001044 break;
1045 default:
Roland Dreier468f2232008-07-14 23:48:47 -07001046 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Heftye51060f2006-06-17 20:37:29 -07001047 ib_event->event);
1048 goto out;
1049 }
1050
Sean Heftya1b1b612006-11-30 16:33:14 -08001051 ret = id_priv->id.event_handler(&id_priv->id, &event);
Sean Heftye51060f2006-06-17 20:37:29 -07001052 if (ret) {
1053 /* Destroy the CM ID by returning a non-zero value. */
1054 id_priv->cm_id.ib = NULL;
1055 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001056 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001057 rdma_destroy_id(&id_priv->id);
1058 return ret;
1059 }
1060out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001061 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001062 return ret;
1063}
1064
Sean Hefty628e5f62006-11-30 16:44:16 -08001065static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id,
1066 struct ib_cm_event *ib_event)
Sean Heftye51060f2006-06-17 20:37:29 -07001067{
1068 struct rdma_id_private *id_priv;
1069 struct rdma_cm_id *id;
1070 struct rdma_route *rt;
1071 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001072 __be16 port;
Sean Heftye51060f2006-06-17 20:37:29 -07001073 u8 ip_ver;
Or Gerlitz64c5e612008-07-14 23:48:53 -07001074 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07001075
Sean Heftye51060f2006-06-17 20:37:29 -07001076 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1077 &ip_ver, &port, &src, &dst))
1078 goto err;
1079
Krishna Kumar3f168d22006-09-29 12:09:51 -07001080 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1081 listen_id->ps);
1082 if (IS_ERR(id))
1083 goto err;
1084
Sean Heftye51060f2006-06-17 20:37:29 -07001085 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1086 ip_ver, port, src, dst);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001087
1088 rt = &id->route;
1089 rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1;
1090 rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths,
1091 GFP_KERNEL);
1092 if (!rt->path_rec)
1093 goto destroy_id;
1094
Sean Heftye51060f2006-06-17 20:37:29 -07001095 rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path;
1096 if (rt->num_paths == 2)
1097 rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path;
1098
Sean Hefty6f8372b2009-11-19 13:26:06 -08001099 if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) {
1100 rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND;
1101 rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid);
1102 ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey);
1103 } else {
1104 ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr,
1105 &rt->addr.dev_addr);
1106 if (ret)
1107 goto destroy_id;
1108 }
1109 rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid);
Sean Heftye51060f2006-06-17 20:37:29 -07001110
1111 id_priv = container_of(id, struct rdma_id_private, id);
1112 id_priv->state = CMA_CONNECT;
1113 return id_priv;
Krishna Kumar3f168d22006-09-29 12:09:51 -07001114
1115destroy_id:
Sean Heftye51060f2006-06-17 20:37:29 -07001116 rdma_destroy_id(id);
Krishna Kumar3f168d22006-09-29 12:09:51 -07001117err:
Sean Heftye51060f2006-06-17 20:37:29 -07001118 return NULL;
1119}
1120
Sean Hefty628e5f62006-11-30 16:44:16 -08001121static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id,
1122 struct ib_cm_event *ib_event)
1123{
1124 struct rdma_id_private *id_priv;
1125 struct rdma_cm_id *id;
1126 union cma_ip_addr *src, *dst;
Al Viro1b90c132008-03-29 03:10:28 +00001127 __be16 port;
Sean Hefty628e5f62006-11-30 16:44:16 -08001128 u8 ip_ver;
1129 int ret;
1130
1131 id = rdma_create_id(listen_id->event_handler, listen_id->context,
1132 listen_id->ps);
1133 if (IS_ERR(id))
1134 return NULL;
1135
1136
1137 if (cma_get_net_info(ib_event->private_data, listen_id->ps,
1138 &ip_ver, &port, &src, &dst))
1139 goto err;
1140
1141 cma_save_net_info(&id->route.addr, &listen_id->route.addr,
1142 ip_ver, port, src, dst);
1143
Sean Hefty6f8372b2009-11-19 13:26:06 -08001144 if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) {
1145 ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr,
1146 &id->route.addr.dev_addr);
1147 if (ret)
1148 goto err;
1149 }
Sean Hefty628e5f62006-11-30 16:44:16 -08001150
1151 id_priv = container_of(id, struct rdma_id_private, id);
1152 id_priv->state = CMA_CONNECT;
1153 return id_priv;
1154err:
1155 rdma_destroy_id(id);
1156 return NULL;
1157}
1158
Sean Heftya1b1b612006-11-30 16:33:14 -08001159static void cma_set_req_event_data(struct rdma_cm_event *event,
1160 struct ib_cm_req_event_param *req_data,
1161 void *private_data, int offset)
1162{
1163 event->param.conn.private_data = private_data + offset;
1164 event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset;
1165 event->param.conn.responder_resources = req_data->responder_resources;
1166 event->param.conn.initiator_depth = req_data->initiator_depth;
1167 event->param.conn.flow_control = req_data->flow_control;
1168 event->param.conn.retry_count = req_data->retry_count;
1169 event->param.conn.rnr_retry_count = req_data->rnr_retry_count;
1170 event->param.conn.srq = req_data->srq;
1171 event->param.conn.qp_num = req_data->remote_qpn;
1172}
1173
Sean Heftye51060f2006-06-17 20:37:29 -07001174static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event)
1175{
1176 struct rdma_id_private *listen_id, *conn_id;
Sean Heftya1b1b612006-11-30 16:33:14 -08001177 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001178 int offset, ret;
1179
1180 listen_id = cm_id->context;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001181 if (cma_disable_callback(listen_id, CMA_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001182 return -ECONNABORTED;
Sean Heftye51060f2006-06-17 20:37:29 -07001183
Sean Hefty628e5f62006-11-30 16:44:16 -08001184 memset(&event, 0, sizeof event);
1185 offset = cma_user_data_offset(listen_id->id.ps);
1186 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
Sean Heftyc8f6a362007-02-15 17:00:18 -08001187 if (cma_is_ud_ps(listen_id->id.ps)) {
Sean Hefty628e5f62006-11-30 16:44:16 -08001188 conn_id = cma_new_udp_id(&listen_id->id, ib_event);
1189 event.param.ud.private_data = ib_event->private_data + offset;
1190 event.param.ud.private_data_len =
1191 IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset;
1192 } else {
1193 conn_id = cma_new_conn_id(&listen_id->id, ib_event);
1194 cma_set_req_event_data(&event, &ib_event->param.req_rcvd,
1195 ib_event->private_data, offset);
1196 }
Sean Heftye51060f2006-06-17 20:37:29 -07001197 if (!conn_id) {
1198 ret = -ENOMEM;
1199 goto out;
1200 }
1201
Or Gerlitzde910bd2008-07-14 23:48:53 -07001202 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Sean Hefty61a73c72006-09-01 15:33:55 -07001203 mutex_lock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001204 ret = cma_acquire_dev(conn_id);
Sean Hefty61a73c72006-09-01 15:33:55 -07001205 mutex_unlock(&lock);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301206 if (ret)
1207 goto release_conn_id;
Sean Heftye51060f2006-06-17 20:37:29 -07001208
1209 conn_id->cm_id.ib = cm_id;
1210 cm_id->context = conn_id;
1211 cm_id->cm_handler = cma_ib_handler;
1212
Sean Heftya1b1b612006-11-30 16:33:14 -08001213 ret = conn_id->id.event_handler(&conn_id->id, &event);
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001214 if (!ret) {
Sean Heftyead595a2008-02-13 14:33:53 -08001215 /*
1216 * Acquire mutex to prevent user executing rdma_destroy_id()
1217 * while we're accessing the cm_id.
1218 */
1219 mutex_lock(&lock);
1220 if (cma_comp(conn_id, CMA_CONNECT) &&
1221 !cma_is_ud_ps(conn_id->id.ps))
1222 ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0);
1223 mutex_unlock(&lock);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001224 mutex_unlock(&conn_id->handler_mutex);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301225 goto out;
Vladimir Sokolovsky45d94782007-12-07 20:32:03 -08001226 }
Krishna Kumara1a733f2006-10-17 10:09:11 +05301227
1228 /* Destroy the CM ID by returning a non-zero value. */
1229 conn_id->cm_id.ib = NULL;
1230
1231release_conn_id:
1232 cma_exch(conn_id, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001233 mutex_unlock(&conn_id->handler_mutex);
Krishna Kumara1a733f2006-10-17 10:09:11 +05301234 rdma_destroy_id(&conn_id->id);
1235
Sean Heftye51060f2006-06-17 20:37:29 -07001236out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001237 mutex_unlock(&listen_id->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001238 return ret;
1239}
1240
1241static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr)
1242{
Sean Hefty628e5f62006-11-30 16:44:16 -08001243 return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr)));
Sean Heftye51060f2006-06-17 20:37:29 -07001244}
1245
1246static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr,
1247 struct ib_cm_compare_data *compare)
1248{
1249 struct cma_hdr *cma_data, *cma_mask;
1250 struct sdp_hh *sdp_data, *sdp_mask;
Al Viro1b90c132008-03-29 03:10:28 +00001251 __be32 ip4_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001252 struct in6_addr ip6_addr;
1253
1254 memset(compare, 0, sizeof *compare);
1255 cma_data = (void *) compare->data;
1256 cma_mask = (void *) compare->mask;
1257 sdp_data = (void *) compare->data;
1258 sdp_mask = (void *) compare->mask;
1259
1260 switch (addr->sa_family) {
1261 case AF_INET:
1262 ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1263 if (ps == RDMA_PS_SDP) {
1264 sdp_set_ip_ver(sdp_data, 4);
1265 sdp_set_ip_ver(sdp_mask, 0xF);
1266 sdp_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001267 sdp_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001268 } else {
1269 cma_set_ip_ver(cma_data, 4);
1270 cma_set_ip_ver(cma_mask, 0xF);
1271 cma_data->dst_addr.ip4.addr = ip4_addr;
Al Viro1b90c132008-03-29 03:10:28 +00001272 cma_mask->dst_addr.ip4.addr = htonl(~0);
Sean Heftye51060f2006-06-17 20:37:29 -07001273 }
1274 break;
1275 case AF_INET6:
1276 ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr;
1277 if (ps == RDMA_PS_SDP) {
1278 sdp_set_ip_ver(sdp_data, 6);
1279 sdp_set_ip_ver(sdp_mask, 0xF);
1280 sdp_data->dst_addr.ip6 = ip6_addr;
1281 memset(&sdp_mask->dst_addr.ip6, 0xFF,
1282 sizeof sdp_mask->dst_addr.ip6);
1283 } else {
1284 cma_set_ip_ver(cma_data, 6);
1285 cma_set_ip_ver(cma_mask, 0xF);
1286 cma_data->dst_addr.ip6 = ip6_addr;
1287 memset(&cma_mask->dst_addr.ip6, 0xFF,
1288 sizeof cma_mask->dst_addr.ip6);
1289 }
1290 break;
1291 default:
1292 break;
1293 }
1294}
1295
Tom Tucker07ebafb2006-08-03 16:02:42 -05001296static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event)
1297{
1298 struct rdma_id_private *id_priv = iw_id->context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001299 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001300 struct sockaddr_in *sin;
1301 int ret = 0;
1302
Or Gerlitzde910bd2008-07-14 23:48:53 -07001303 if (cma_disable_callback(id_priv, CMA_CONNECT))
Sean Heftybe65f082007-05-07 11:49:12 -07001304 return 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001305
Sean Heftybe65f082007-05-07 11:49:12 -07001306 memset(&event, 0, sizeof event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001307 switch (iw_event->event) {
1308 case IW_CM_EVENT_CLOSE:
Sean Heftya1b1b612006-11-30 16:33:14 -08001309 event.event = RDMA_CM_EVENT_DISCONNECTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001310 break;
1311 case IW_CM_EVENT_CONNECT_REPLY:
1312 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1313 *sin = iw_event->local_addr;
1314 sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr;
1315 *sin = iw_event->remote_addr;
Steve Wise881a0452006-12-15 16:50:17 -06001316 switch (iw_event->status) {
1317 case 0:
Sean Heftya1b1b612006-11-30 16:33:14 -08001318 event.event = RDMA_CM_EVENT_ESTABLISHED;
Steve Wise881a0452006-12-15 16:50:17 -06001319 break;
1320 case -ECONNRESET:
1321 case -ECONNREFUSED:
1322 event.event = RDMA_CM_EVENT_REJECTED;
1323 break;
1324 case -ETIMEDOUT:
1325 event.event = RDMA_CM_EVENT_UNREACHABLE;
1326 break;
1327 default:
1328 event.event = RDMA_CM_EVENT_CONNECT_ERROR;
1329 break;
1330 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05001331 break;
1332 case IW_CM_EVENT_ESTABLISHED:
Sean Heftya1b1b612006-11-30 16:33:14 -08001333 event.event = RDMA_CM_EVENT_ESTABLISHED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001334 break;
1335 default:
1336 BUG_ON(1);
1337 }
1338
Sean Heftya1b1b612006-11-30 16:33:14 -08001339 event.status = iw_event->status;
1340 event.param.conn.private_data = iw_event->private_data;
1341 event.param.conn.private_data_len = iw_event->private_data_len;
1342 ret = id_priv->id.event_handler(&id_priv->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001343 if (ret) {
1344 /* Destroy the CM ID by returning a non-zero value. */
1345 id_priv->cm_id.iw = NULL;
1346 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001347 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001348 rdma_destroy_id(&id_priv->id);
1349 return ret;
1350 }
1351
Or Gerlitzde910bd2008-07-14 23:48:53 -07001352 mutex_unlock(&id_priv->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001353 return ret;
1354}
1355
1356static int iw_conn_req_handler(struct iw_cm_id *cm_id,
1357 struct iw_cm_event *iw_event)
1358{
1359 struct rdma_cm_id *new_cm_id;
1360 struct rdma_id_private *listen_id, *conn_id;
1361 struct sockaddr_in *sin;
1362 struct net_device *dev = NULL;
Sean Heftya1b1b612006-11-30 16:33:14 -08001363 struct rdma_cm_event event;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001364 int ret;
Steve Wise8d8293c2007-10-29 11:34:05 -05001365 struct ib_device_attr attr;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001366
1367 listen_id = cm_id->context;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001368 if (cma_disable_callback(listen_id, CMA_LISTEN))
Sean Hefty8aa08602007-05-07 11:49:00 -07001369 return -ECONNABORTED;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001370
1371 /* Create a new RDMA id for the new IW CM ID */
1372 new_cm_id = rdma_create_id(listen_id->id.event_handler,
1373 listen_id->id.context,
1374 RDMA_PS_TCP);
Julia Lawall10f32062008-04-16 21:09:25 -07001375 if (IS_ERR(new_cm_id)) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001376 ret = -ENOMEM;
1377 goto out;
1378 }
1379 conn_id = container_of(new_cm_id, struct rdma_id_private, id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001380 mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001381 conn_id->state = CMA_CONNECT;
1382
Denis V. Lunev1ab35272008-01-22 22:04:30 -08001383 dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001384 if (!dev) {
1385 ret = -EADDRNOTAVAIL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07001386 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001387 rdma_destroy_id(new_cm_id);
1388 goto out;
1389 }
1390 ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL);
1391 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001392 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001393 rdma_destroy_id(new_cm_id);
1394 goto out;
1395 }
1396
Sean Hefty61a73c72006-09-01 15:33:55 -07001397 mutex_lock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001398 ret = cma_acquire_dev(conn_id);
Sean Hefty61a73c72006-09-01 15:33:55 -07001399 mutex_unlock(&lock);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001400 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001401 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001402 rdma_destroy_id(new_cm_id);
1403 goto out;
1404 }
1405
1406 conn_id->cm_id.iw = cm_id;
1407 cm_id->context = conn_id;
1408 cm_id->cm_handler = cma_iw_handler;
1409
1410 sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr;
1411 *sin = iw_event->local_addr;
1412 sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr;
1413 *sin = iw_event->remote_addr;
1414
Steve Wise8d8293c2007-10-29 11:34:05 -05001415 ret = ib_query_device(conn_id->id.device, &attr);
1416 if (ret) {
Or Gerlitzde910bd2008-07-14 23:48:53 -07001417 mutex_unlock(&conn_id->handler_mutex);
Steve Wise8d8293c2007-10-29 11:34:05 -05001418 rdma_destroy_id(new_cm_id);
1419 goto out;
1420 }
1421
Sean Heftya1b1b612006-11-30 16:33:14 -08001422 memset(&event, 0, sizeof event);
1423 event.event = RDMA_CM_EVENT_CONNECT_REQUEST;
1424 event.param.conn.private_data = iw_event->private_data;
1425 event.param.conn.private_data_len = iw_event->private_data_len;
Steve Wise8d8293c2007-10-29 11:34:05 -05001426 event.param.conn.initiator_depth = attr.max_qp_init_rd_atom;
1427 event.param.conn.responder_resources = attr.max_qp_rd_atom;
Sean Heftya1b1b612006-11-30 16:33:14 -08001428 ret = conn_id->id.event_handler(&conn_id->id, &event);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001429 if (ret) {
1430 /* User wants to destroy the CM ID */
1431 conn_id->cm_id.iw = NULL;
1432 cma_exch(conn_id, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001433 mutex_unlock(&conn_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001434 rdma_destroy_id(&conn_id->id);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001435 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001436 }
1437
Or Gerlitzde910bd2008-07-14 23:48:53 -07001438 mutex_unlock(&conn_id->handler_mutex);
1439
Tom Tucker07ebafb2006-08-03 16:02:42 -05001440out:
1441 if (dev)
1442 dev_put(dev);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001443 mutex_unlock(&listen_id->handler_mutex);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001444 return ret;
1445}
1446
Sean Heftye51060f2006-06-17 20:37:29 -07001447static int cma_ib_listen(struct rdma_id_private *id_priv)
1448{
1449 struct ib_cm_compare_data compare_data;
1450 struct sockaddr *addr;
1451 __be64 svc_id;
1452 int ret;
1453
1454 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_req_handler,
1455 id_priv);
1456 if (IS_ERR(id_priv->cm_id.ib))
1457 return PTR_ERR(id_priv->cm_id.ib);
1458
Roland Dreier3f446752008-08-04 11:02:14 -07001459 addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001460 svc_id = cma_get_service_id(id_priv->id.ps, addr);
1461 if (cma_any_addr(addr))
1462 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL);
1463 else {
1464 cma_set_compare_data(id_priv->id.ps, addr, &compare_data);
1465 ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data);
1466 }
1467
1468 if (ret) {
1469 ib_destroy_cm_id(id_priv->cm_id.ib);
1470 id_priv->cm_id.ib = NULL;
1471 }
1472
1473 return ret;
1474}
1475
Tom Tucker07ebafb2006-08-03 16:02:42 -05001476static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog)
1477{
1478 int ret;
1479 struct sockaddr_in *sin;
1480
1481 id_priv->cm_id.iw = iw_create_cm_id(id_priv->id.device,
1482 iw_conn_req_handler,
1483 id_priv);
1484 if (IS_ERR(id_priv->cm_id.iw))
1485 return PTR_ERR(id_priv->cm_id.iw);
1486
1487 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
1488 id_priv->cm_id.iw->local_addr = *sin;
1489
1490 ret = iw_cm_listen(id_priv->cm_id.iw, backlog);
1491
1492 if (ret) {
1493 iw_destroy_cm_id(id_priv->cm_id.iw);
1494 id_priv->cm_id.iw = NULL;
1495 }
1496
1497 return ret;
1498}
1499
Sean Heftye51060f2006-06-17 20:37:29 -07001500static int cma_listen_handler(struct rdma_cm_id *id,
1501 struct rdma_cm_event *event)
1502{
1503 struct rdma_id_private *id_priv = id->context;
1504
1505 id->context = id_priv->id.context;
1506 id->event_handler = id_priv->id.event_handler;
1507 return id_priv->id.event_handler(id, event);
1508}
1509
1510static void cma_listen_on_dev(struct rdma_id_private *id_priv,
1511 struct cma_device *cma_dev)
1512{
1513 struct rdma_id_private *dev_id_priv;
1514 struct rdma_cm_id *id;
1515 int ret;
1516
1517 id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps);
1518 if (IS_ERR(id))
1519 return;
1520
1521 dev_id_priv = container_of(id, struct rdma_id_private, id);
1522
1523 dev_id_priv->state = CMA_ADDR_BOUND;
1524 memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr,
Roland Dreier3f446752008-08-04 11:02:14 -07001525 ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001526
1527 cma_attach_to_dev(dev_id_priv, cma_dev);
1528 list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list);
Sean Heftyd02d1f52007-10-09 11:12:34 -07001529 atomic_inc(&id_priv->refcount);
1530 dev_id_priv->internal_id = 1;
Sean Heftye51060f2006-06-17 20:37:29 -07001531
1532 ret = rdma_listen(id, id_priv->backlog);
1533 if (ret)
Sean Heftyd02d1f52007-10-09 11:12:34 -07001534 printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, "
Roland Dreier468f2232008-07-14 23:48:47 -07001535 "listening on device %s\n", ret, cma_dev->device->name);
Sean Heftye51060f2006-06-17 20:37:29 -07001536}
1537
1538static void cma_listen_on_all(struct rdma_id_private *id_priv)
1539{
1540 struct cma_device *cma_dev;
1541
1542 mutex_lock(&lock);
1543 list_add_tail(&id_priv->list, &listen_any_list);
1544 list_for_each_entry(cma_dev, &dev_list, list)
1545 cma_listen_on_dev(id_priv, cma_dev);
1546 mutex_unlock(&lock);
1547}
1548
Sean Heftye51060f2006-06-17 20:37:29 -07001549int rdma_listen(struct rdma_cm_id *id, int backlog)
1550{
1551 struct rdma_id_private *id_priv;
1552 int ret;
1553
1554 id_priv = container_of(id, struct rdma_id_private, id);
1555 if (id_priv->state == CMA_IDLE) {
Sean Heftyd14714d2009-11-19 16:46:25 -08001556 ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET;
1557 ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07001558 if (ret)
1559 return ret;
1560 }
1561
1562 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_LISTEN))
1563 return -EINVAL;
1564
1565 id_priv->backlog = backlog;
1566 if (id->device) {
Tom Tucker07ebafb2006-08-03 16:02:42 -05001567 switch (rdma_node_get_transport(id->device->node_type)) {
1568 case RDMA_TRANSPORT_IB:
Sean Heftye51060f2006-06-17 20:37:29 -07001569 ret = cma_ib_listen(id_priv);
1570 if (ret)
1571 goto err;
1572 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001573 case RDMA_TRANSPORT_IWARP:
1574 ret = cma_iw_listen(id_priv, backlog);
1575 if (ret)
1576 goto err;
1577 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001578 default:
1579 ret = -ENOSYS;
1580 goto err;
1581 }
1582 } else
1583 cma_listen_on_all(id_priv);
1584
1585 return 0;
1586err:
1587 id_priv->backlog = 0;
1588 cma_comp_exch(id_priv, CMA_LISTEN, CMA_ADDR_BOUND);
1589 return ret;
1590}
1591EXPORT_SYMBOL(rdma_listen);
1592
Sean Heftya81c9942007-08-08 15:51:06 -07001593void rdma_set_service_type(struct rdma_cm_id *id, int tos)
1594{
1595 struct rdma_id_private *id_priv;
1596
1597 id_priv = container_of(id, struct rdma_id_private, id);
1598 id_priv->tos = (u8) tos;
1599}
1600EXPORT_SYMBOL(rdma_set_service_type);
1601
Sean Heftye51060f2006-06-17 20:37:29 -07001602static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec,
1603 void *context)
1604{
1605 struct cma_work *work = context;
1606 struct rdma_route *route;
1607
1608 route = &work->id->id.route;
1609
1610 if (!status) {
1611 route->num_paths = 1;
1612 *route->path_rec = *path_rec;
1613 } else {
1614 work->old_state = CMA_ROUTE_QUERY;
1615 work->new_state = CMA_ADDR_RESOLVED;
1616 work->event.event = RDMA_CM_EVENT_ROUTE_ERROR;
Sean Hefty8f0472d2006-09-29 11:57:09 -07001617 work->event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001618 }
1619
1620 queue_work(cma_wq, &work->work);
1621}
1622
1623static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms,
1624 struct cma_work *work)
1625{
Sean Heftya81c9942007-08-08 15:51:06 -07001626 struct rdma_addr *addr = &id_priv->id.route.addr;
Sean Heftye51060f2006-06-17 20:37:29 -07001627 struct ib_sa_path_rec path_rec;
Sean Heftya81c9942007-08-08 15:51:06 -07001628 ib_sa_comp_mask comp_mask;
1629 struct sockaddr_in6 *sin6;
Sean Heftye51060f2006-06-17 20:37:29 -07001630
1631 memset(&path_rec, 0, sizeof path_rec);
Sean Hefty6f8372b2009-11-19 13:26:06 -08001632 rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid);
1633 rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid);
Sean Heftya81c9942007-08-08 15:51:06 -07001634 path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr));
Sean Heftye51060f2006-06-17 20:37:29 -07001635 path_rec.numb_path = 1;
Sean Hefty962063e2007-02-21 16:40:44 -08001636 path_rec.reversible = 1;
Roland Dreier3f446752008-08-04 11:02:14 -07001637 path_rec.service_id = cma_get_service_id(id_priv->id.ps,
1638 (struct sockaddr *) &addr->dst_addr);
Sean Heftya81c9942007-08-08 15:51:06 -07001639
1640 comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID |
1641 IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH |
1642 IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID;
1643
Roland Dreier3f446752008-08-04 11:02:14 -07001644 if (addr->src_addr.ss_family == AF_INET) {
Sean Heftya81c9942007-08-08 15:51:06 -07001645 path_rec.qos_class = cpu_to_be16((u16) id_priv->tos);
1646 comp_mask |= IB_SA_PATH_REC_QOS_CLASS;
1647 } else {
1648 sin6 = (struct sockaddr_in6 *) &addr->src_addr;
1649 path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20);
1650 comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS;
1651 }
Sean Heftye51060f2006-06-17 20:37:29 -07001652
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001653 id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device,
Sean Heftya81c9942007-08-08 15:51:06 -07001654 id_priv->id.port_num, &path_rec,
1655 comp_mask, timeout_ms,
1656 GFP_KERNEL, cma_query_handler,
1657 work, &id_priv->query);
Sean Heftye51060f2006-06-17 20:37:29 -07001658
1659 return (id_priv->query_id < 0) ? id_priv->query_id : 0;
1660}
1661
David Howellsc4028952006-11-22 14:57:56 +00001662static void cma_work_handler(struct work_struct *_work)
Sean Heftye51060f2006-06-17 20:37:29 -07001663{
David Howellsc4028952006-11-22 14:57:56 +00001664 struct cma_work *work = container_of(_work, struct cma_work, work);
Sean Heftye51060f2006-06-17 20:37:29 -07001665 struct rdma_id_private *id_priv = work->id;
1666 int destroy = 0;
1667
Or Gerlitzde910bd2008-07-14 23:48:53 -07001668 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001669 if (!cma_comp_exch(id_priv, work->old_state, work->new_state))
1670 goto out;
1671
1672 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1673 cma_exch(id_priv, CMA_DESTROYING);
1674 destroy = 1;
1675 }
1676out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001677 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001678 cma_deref_id(id_priv);
1679 if (destroy)
1680 rdma_destroy_id(&id_priv->id);
1681 kfree(work);
1682}
1683
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07001684static void cma_ndev_work_handler(struct work_struct *_work)
1685{
1686 struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work);
1687 struct rdma_id_private *id_priv = work->id;
1688 int destroy = 0;
1689
1690 mutex_lock(&id_priv->handler_mutex);
1691 if (id_priv->state == CMA_DESTROYING ||
1692 id_priv->state == CMA_DEVICE_REMOVAL)
1693 goto out;
1694
1695 if (id_priv->id.event_handler(&id_priv->id, &work->event)) {
1696 cma_exch(id_priv, CMA_DESTROYING);
1697 destroy = 1;
1698 }
1699
1700out:
1701 mutex_unlock(&id_priv->handler_mutex);
1702 cma_deref_id(id_priv);
1703 if (destroy)
1704 rdma_destroy_id(&id_priv->id);
1705 kfree(work);
1706}
1707
Sean Heftye51060f2006-06-17 20:37:29 -07001708static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms)
1709{
1710 struct rdma_route *route = &id_priv->id.route;
1711 struct cma_work *work;
1712 int ret;
1713
1714 work = kzalloc(sizeof *work, GFP_KERNEL);
1715 if (!work)
1716 return -ENOMEM;
1717
1718 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001719 INIT_WORK(&work->work, cma_work_handler);
Sean Heftye51060f2006-06-17 20:37:29 -07001720 work->old_state = CMA_ROUTE_QUERY;
1721 work->new_state = CMA_ROUTE_RESOLVED;
1722 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1723
1724 route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL);
1725 if (!route->path_rec) {
1726 ret = -ENOMEM;
1727 goto err1;
1728 }
1729
1730 ret = cma_query_ib_route(id_priv, timeout_ms, work);
1731 if (ret)
1732 goto err2;
1733
1734 return 0;
1735err2:
1736 kfree(route->path_rec);
1737 route->path_rec = NULL;
1738err1:
1739 kfree(work);
1740 return ret;
1741}
1742
1743int rdma_set_ib_paths(struct rdma_cm_id *id,
1744 struct ib_sa_path_rec *path_rec, int num_paths)
1745{
1746 struct rdma_id_private *id_priv;
1747 int ret;
1748
1749 id_priv = container_of(id, struct rdma_id_private, id);
1750 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_RESOLVED))
1751 return -EINVAL;
1752
Julia Lawall9893e742010-05-15 23:22:38 +02001753 id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths,
1754 GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07001755 if (!id->route.path_rec) {
1756 ret = -ENOMEM;
1757 goto err;
1758 }
1759
Sean Heftyae2d9292010-03-25 19:12:36 +00001760 id->route.num_paths = num_paths;
Sean Heftye51060f2006-06-17 20:37:29 -07001761 return 0;
1762err:
1763 cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_ADDR_RESOLVED);
1764 return ret;
1765}
1766EXPORT_SYMBOL(rdma_set_ib_paths);
1767
Tom Tucker07ebafb2006-08-03 16:02:42 -05001768static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms)
1769{
1770 struct cma_work *work;
1771
1772 work = kzalloc(sizeof *work, GFP_KERNEL);
1773 if (!work)
1774 return -ENOMEM;
1775
1776 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00001777 INIT_WORK(&work->work, cma_work_handler);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001778 work->old_state = CMA_ROUTE_QUERY;
1779 work->new_state = CMA_ROUTE_RESOLVED;
1780 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1781 queue_work(cma_wq, &work->work);
1782 return 0;
1783}
1784
Eli Cohen3c86aa72010-10-13 21:26:51 +02001785static int cma_resolve_iboe_route(struct rdma_id_private *id_priv)
1786{
1787 struct rdma_route *route = &id_priv->id.route;
1788 struct rdma_addr *addr = &route->addr;
1789 struct cma_work *work;
1790 int ret;
1791 struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr;
1792 struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr;
1793 struct net_device *ndev = NULL;
Eli Cohenaf7bd462010-08-26 17:18:59 +03001794 u16 vid;
Eli Cohen3c86aa72010-10-13 21:26:51 +02001795
1796 if (src_addr->sin_family != dst_addr->sin_family)
1797 return -EINVAL;
1798
1799 work = kzalloc(sizeof *work, GFP_KERNEL);
1800 if (!work)
1801 return -ENOMEM;
1802
1803 work->id = id_priv;
1804 INIT_WORK(&work->work, cma_work_handler);
1805
1806 route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL);
1807 if (!route->path_rec) {
1808 ret = -ENOMEM;
1809 goto err1;
1810 }
1811
1812 route->num_paths = 1;
1813
Eli Cohen3c86aa72010-10-13 21:26:51 +02001814 if (addr->dev_addr.bound_dev_if)
1815 ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if);
1816 if (!ndev) {
1817 ret = -ENODEV;
1818 goto err2;
1819 }
1820
Eli Cohenaf7bd462010-08-26 17:18:59 +03001821 vid = rdma_vlan_dev_vlan_id(ndev);
1822
1823 iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid);
1824 iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid);
1825
1826 route->path_rec->hop_limit = 1;
1827 route->path_rec->reversible = 1;
1828 route->path_rec->pkey = cpu_to_be16(0xffff);
1829 route->path_rec->mtu_selector = IB_SA_EQ;
1830 route->path_rec->sl = id_priv->tos >> 5;
1831
Eli Cohen3c86aa72010-10-13 21:26:51 +02001832 route->path_rec->mtu = iboe_get_mtu(ndev->mtu);
1833 route->path_rec->rate_selector = IB_SA_EQ;
1834 route->path_rec->rate = iboe_get_rate(ndev);
1835 dev_put(ndev);
1836 route->path_rec->packet_life_time_selector = IB_SA_EQ;
1837 route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME;
1838 if (!route->path_rec->mtu) {
1839 ret = -EINVAL;
1840 goto err2;
1841 }
1842
1843 work->old_state = CMA_ROUTE_QUERY;
1844 work->new_state = CMA_ROUTE_RESOLVED;
1845 work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
1846 work->event.status = 0;
1847
1848 queue_work(cma_wq, &work->work);
1849
1850 return 0;
1851
1852err2:
1853 kfree(route->path_rec);
1854 route->path_rec = NULL;
1855err1:
1856 kfree(work);
1857 return ret;
1858}
1859
Sean Heftye51060f2006-06-17 20:37:29 -07001860int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms)
1861{
1862 struct rdma_id_private *id_priv;
1863 int ret;
1864
1865 id_priv = container_of(id, struct rdma_id_private, id);
1866 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ROUTE_QUERY))
1867 return -EINVAL;
1868
1869 atomic_inc(&id_priv->refcount);
Tom Tucker07ebafb2006-08-03 16:02:42 -05001870 switch (rdma_node_get_transport(id->device->node_type)) {
1871 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02001872 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
1873 case IB_LINK_LAYER_INFINIBAND:
1874 ret = cma_resolve_ib_route(id_priv, timeout_ms);
1875 break;
1876 case IB_LINK_LAYER_ETHERNET:
1877 ret = cma_resolve_iboe_route(id_priv);
1878 break;
1879 default:
1880 ret = -ENOSYS;
1881 }
Sean Heftye51060f2006-06-17 20:37:29 -07001882 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001883 case RDMA_TRANSPORT_IWARP:
1884 ret = cma_resolve_iw_route(id_priv, timeout_ms);
1885 break;
Sean Heftye51060f2006-06-17 20:37:29 -07001886 default:
1887 ret = -ENOSYS;
1888 break;
1889 }
1890 if (ret)
1891 goto err;
1892
1893 return 0;
1894err:
1895 cma_comp_exch(id_priv, CMA_ROUTE_QUERY, CMA_ADDR_RESOLVED);
1896 cma_deref_id(id_priv);
1897 return ret;
1898}
1899EXPORT_SYMBOL(rdma_resolve_route);
1900
1901static int cma_bind_loopback(struct rdma_id_private *id_priv)
1902{
1903 struct cma_device *cma_dev;
1904 struct ib_port_attr port_attr;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001905 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001906 u16 pkey;
1907 int ret;
1908 u8 p;
1909
1910 mutex_lock(&lock);
Krishna Kumare82153b2006-10-16 10:09:01 +05301911 if (list_empty(&dev_list)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001912 ret = -ENODEV;
1913 goto out;
1914 }
Krishna Kumare82153b2006-10-16 10:09:01 +05301915 list_for_each_entry(cma_dev, &dev_list, list)
1916 for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p)
1917 if (!ib_query_port(cma_dev->device, p, &port_attr) &&
1918 port_attr.state == IB_PORT_ACTIVE)
1919 goto port_found;
1920
1921 p = 1;
1922 cma_dev = list_entry(dev_list.next, struct cma_device, list);
Sean Heftye51060f2006-06-17 20:37:29 -07001923
1924port_found:
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001925 ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001926 if (ret)
1927 goto out;
1928
1929 ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey);
1930 if (ret)
1931 goto out;
1932
Sean Hefty6f8372b2009-11-19 13:26:06 -08001933 id_priv->id.route.addr.dev_addr.dev_type =
Eli Cohen3c86aa72010-10-13 21:26:51 +02001934 (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ?
Sean Hefty6f8372b2009-11-19 13:26:06 -08001935 ARPHRD_INFINIBAND : ARPHRD_ETHER;
1936
1937 rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07001938 ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey);
1939 id_priv->id.port_num = p;
1940 cma_attach_to_dev(id_priv, cma_dev);
1941out:
1942 mutex_unlock(&lock);
1943 return ret;
1944}
1945
1946static void addr_handler(int status, struct sockaddr *src_addr,
1947 struct rdma_dev_addr *dev_addr, void *context)
1948{
1949 struct rdma_id_private *id_priv = context;
Sean Heftya1b1b612006-11-30 16:33:14 -08001950 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07001951
Sean Heftya1b1b612006-11-30 16:33:14 -08001952 memset(&event, 0, sizeof event);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001953 mutex_lock(&id_priv->handler_mutex);
Sean Hefty61a73c72006-09-01 15:33:55 -07001954
1955 /*
1956 * Grab mutex to block rdma_destroy_id() from removing the device while
1957 * we're trying to acquire it.
1958 */
1959 mutex_lock(&lock);
1960 if (!cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_RESOLVED)) {
1961 mutex_unlock(&lock);
1962 goto out;
1963 }
1964
1965 if (!status && !id_priv->cma_dev)
Sean Heftye51060f2006-06-17 20:37:29 -07001966 status = cma_acquire_dev(id_priv);
Sean Hefty61a73c72006-09-01 15:33:55 -07001967 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07001968
1969 if (status) {
Sean Hefty61a73c72006-09-01 15:33:55 -07001970 if (!cma_comp_exch(id_priv, CMA_ADDR_RESOLVED, CMA_ADDR_BOUND))
Sean Heftye51060f2006-06-17 20:37:29 -07001971 goto out;
Sean Heftya1b1b612006-11-30 16:33:14 -08001972 event.event = RDMA_CM_EVENT_ADDR_ERROR;
1973 event.status = status;
Sean Heftye51060f2006-06-17 20:37:29 -07001974 } else {
Sean Heftye51060f2006-06-17 20:37:29 -07001975 memcpy(&id_priv->id.route.addr.src_addr, src_addr,
1976 ip_addr_size(src_addr));
Sean Heftya1b1b612006-11-30 16:33:14 -08001977 event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
Sean Heftye51060f2006-06-17 20:37:29 -07001978 }
1979
Sean Heftya1b1b612006-11-30 16:33:14 -08001980 if (id_priv->id.event_handler(&id_priv->id, &event)) {
Sean Heftye51060f2006-06-17 20:37:29 -07001981 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07001982 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001983 cma_deref_id(id_priv);
1984 rdma_destroy_id(&id_priv->id);
1985 return;
1986 }
1987out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07001988 mutex_unlock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07001989 cma_deref_id(id_priv);
1990}
1991
1992static int cma_resolve_loopback(struct rdma_id_private *id_priv)
1993{
1994 struct cma_work *work;
Sean Hefty6f8372b2009-11-19 13:26:06 -08001995 struct sockaddr *src, *dst;
Michael S. Tsirkinf0ee3402006-07-14 00:23:52 -07001996 union ib_gid gid;
Sean Heftye51060f2006-06-17 20:37:29 -07001997 int ret;
1998
1999 work = kzalloc(sizeof *work, GFP_KERNEL);
2000 if (!work)
2001 return -ENOMEM;
2002
2003 if (!id_priv->cma_dev) {
2004 ret = cma_bind_loopback(id_priv);
2005 if (ret)
2006 goto err;
2007 }
2008
Sean Hefty6f8372b2009-11-19 13:26:06 -08002009 rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid);
2010 rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid);
Sean Heftye51060f2006-06-17 20:37:29 -07002011
Sean Hefty6f8372b2009-11-19 13:26:06 -08002012 src = (struct sockaddr *) &id_priv->id.route.addr.src_addr;
2013 if (cma_zero_addr(src)) {
2014 dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr;
2015 if ((src->sa_family = dst->sa_family) == AF_INET) {
2016 ((struct sockaddr_in *) src)->sin_addr.s_addr =
2017 ((struct sockaddr_in *) dst)->sin_addr.s_addr;
2018 } else {
2019 ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr,
2020 &((struct sockaddr_in6 *) dst)->sin6_addr);
2021 }
Sean Heftye51060f2006-06-17 20:37:29 -07002022 }
2023
2024 work->id = id_priv;
David Howellsc4028952006-11-22 14:57:56 +00002025 INIT_WORK(&work->work, cma_work_handler);
Sean Heftye51060f2006-06-17 20:37:29 -07002026 work->old_state = CMA_ADDR_QUERY;
2027 work->new_state = CMA_ADDR_RESOLVED;
2028 work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED;
2029 queue_work(cma_wq, &work->work);
2030 return 0;
2031err:
2032 kfree(work);
2033 return ret;
2034}
2035
2036static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2037 struct sockaddr *dst_addr)
2038{
Sean Heftyd14714d2009-11-19 16:46:25 -08002039 if (!src_addr || !src_addr->sa_family) {
2040 src_addr = (struct sockaddr *) &id->route.addr.src_addr;
2041 if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) {
2042 ((struct sockaddr_in6 *) src_addr)->sin6_scope_id =
2043 ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id;
2044 }
2045 }
2046 return rdma_bind_addr(id, src_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002047}
2048
2049int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr,
2050 struct sockaddr *dst_addr, int timeout_ms)
2051{
2052 struct rdma_id_private *id_priv;
2053 int ret;
2054
2055 id_priv = container_of(id, struct rdma_id_private, id);
2056 if (id_priv->state == CMA_IDLE) {
2057 ret = cma_bind_addr(id, src_addr, dst_addr);
2058 if (ret)
2059 return ret;
2060 }
2061
2062 if (!cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_ADDR_QUERY))
2063 return -EINVAL;
2064
2065 atomic_inc(&id_priv->refcount);
2066 memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr));
2067 if (cma_any_addr(dst_addr))
2068 ret = cma_resolve_loopback(id_priv);
2069 else
Roland Dreier3f446752008-08-04 11:02:14 -07002070 ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr,
Sean Hefty7a118df2006-10-31 11:12:59 -08002071 dst_addr, &id->route.addr.dev_addr,
Sean Heftye51060f2006-06-17 20:37:29 -07002072 timeout_ms, addr_handler, id_priv);
2073 if (ret)
2074 goto err;
2075
2076 return 0;
2077err:
2078 cma_comp_exch(id_priv, CMA_ADDR_QUERY, CMA_ADDR_BOUND);
2079 cma_deref_id(id_priv);
2080 return ret;
2081}
2082EXPORT_SYMBOL(rdma_resolve_addr);
2083
2084static void cma_bind_port(struct rdma_bind_list *bind_list,
2085 struct rdma_id_private *id_priv)
2086{
2087 struct sockaddr_in *sin;
2088
2089 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2090 sin->sin_port = htons(bind_list->port);
2091 id_priv->bind_list = bind_list;
2092 hlist_add_head(&id_priv->node, &bind_list->owners);
2093}
2094
2095static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv,
2096 unsigned short snum)
2097{
2098 struct rdma_bind_list *bind_list;
Sean Heftyaedec082007-01-29 16:41:23 -08002099 int port, ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002100
Sean Heftycb164b82007-03-05 12:50:17 -08002101 bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL);
Sean Heftye51060f2006-06-17 20:37:29 -07002102 if (!bind_list)
2103 return -ENOMEM;
2104
Sean Heftye51060f2006-06-17 20:37:29 -07002105 do {
Sean Heftyaedec082007-01-29 16:41:23 -08002106 ret = idr_get_new_above(ps, bind_list, snum, &port);
Sean Heftye51060f2006-06-17 20:37:29 -07002107 } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
2108
2109 if (ret)
Sean Heftyaedec082007-01-29 16:41:23 -08002110 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002111
Sean Heftyaedec082007-01-29 16:41:23 -08002112 if (port != snum) {
Sean Heftye51060f2006-06-17 20:37:29 -07002113 ret = -EADDRNOTAVAIL;
Sean Heftyaedec082007-01-29 16:41:23 -08002114 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002115 }
2116
2117 bind_list->ps = ps;
2118 bind_list->port = (unsigned short) port;
2119 cma_bind_port(bind_list, id_priv);
2120 return 0;
Sean Heftyaedec082007-01-29 16:41:23 -08002121err2:
2122 idr_remove(ps, port);
2123err1:
2124 kfree(bind_list);
2125 return ret;
2126}
2127
2128static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv)
2129{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002130 static unsigned int last_used_port;
2131 int low, high, remaining;
2132 unsigned int rover;
Sean Heftyaedec082007-01-29 16:41:23 -08002133
Stephen Hemminger227b60f2007-10-10 17:30:46 -07002134 inet_get_local_port_range(&low, &high);
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002135 remaining = (high - low) + 1;
2136 rover = net_random() % remaining + low;
2137retry:
2138 if (last_used_port != rover &&
2139 !idr_find(ps, (unsigned short) rover)) {
2140 int ret = cma_alloc_port(ps, id_priv, rover);
2141 /*
2142 * Remember previously used port number in order to avoid
2143 * re-using same port immediately after it is closed.
2144 */
2145 if (!ret)
2146 last_used_port = rover;
2147 if (ret != -EADDRNOTAVAIL)
2148 return ret;
Sean Heftyaedec082007-01-29 16:41:23 -08002149 }
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09002150 if (--remaining) {
2151 rover++;
2152 if ((rover < low) || (rover > high))
2153 rover = low;
2154 goto retry;
2155 }
2156 return -EADDRNOTAVAIL;
Sean Heftye51060f2006-06-17 20:37:29 -07002157}
2158
2159static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv)
2160{
2161 struct rdma_id_private *cur_id;
2162 struct sockaddr_in *sin, *cur_sin;
2163 struct rdma_bind_list *bind_list;
2164 struct hlist_node *node;
2165 unsigned short snum;
2166
2167 sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr;
2168 snum = ntohs(sin->sin_port);
2169 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
2170 return -EACCES;
2171
2172 bind_list = idr_find(ps, snum);
2173 if (!bind_list)
2174 return cma_alloc_port(ps, id_priv, snum);
2175
2176 /*
2177 * We don't support binding to any address if anyone is bound to
2178 * a specific address on the same port.
2179 */
Roland Dreier3f446752008-08-04 11:02:14 -07002180 if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr))
Sean Heftye51060f2006-06-17 20:37:29 -07002181 return -EADDRNOTAVAIL;
2182
2183 hlist_for_each_entry(cur_id, node, &bind_list->owners, node) {
Roland Dreier3f446752008-08-04 11:02:14 -07002184 if (cma_any_addr((struct sockaddr *) &cur_id->id.route.addr.src_addr))
Sean Heftye51060f2006-06-17 20:37:29 -07002185 return -EADDRNOTAVAIL;
Roland Dreier3cd96562006-09-22 15:22:46 -07002186
Sean Heftye51060f2006-06-17 20:37:29 -07002187 cur_sin = (struct sockaddr_in *) &cur_id->id.route.addr.src_addr;
2188 if (sin->sin_addr.s_addr == cur_sin->sin_addr.s_addr)
2189 return -EADDRINUSE;
2190 }
2191
2192 cma_bind_port(bind_list, id_priv);
2193 return 0;
2194}
2195
2196static int cma_get_port(struct rdma_id_private *id_priv)
2197{
2198 struct idr *ps;
2199 int ret;
2200
2201 switch (id_priv->id.ps) {
2202 case RDMA_PS_SDP:
2203 ps = &sdp_ps;
2204 break;
2205 case RDMA_PS_TCP:
2206 ps = &tcp_ps;
2207 break;
Sean Hefty628e5f62006-11-30 16:44:16 -08002208 case RDMA_PS_UDP:
2209 ps = &udp_ps;
2210 break;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002211 case RDMA_PS_IPOIB:
2212 ps = &ipoib_ps;
2213 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002214 default:
2215 return -EPROTONOSUPPORT;
2216 }
2217
2218 mutex_lock(&lock);
Roland Dreier3f446752008-08-04 11:02:14 -07002219 if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr))
Sean Heftyaedec082007-01-29 16:41:23 -08002220 ret = cma_alloc_any_port(ps, id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002221 else
2222 ret = cma_use_port(ps, id_priv);
2223 mutex_unlock(&lock);
2224
2225 return ret;
2226}
2227
Sean Heftyd14714d2009-11-19 16:46:25 -08002228static int cma_check_linklocal(struct rdma_dev_addr *dev_addr,
2229 struct sockaddr *addr)
2230{
Robert P. J. Dayfd4582a2010-01-06 13:16:30 -08002231#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Sean Heftyd14714d2009-11-19 16:46:25 -08002232 struct sockaddr_in6 *sin6;
2233
2234 if (addr->sa_family != AF_INET6)
2235 return 0;
2236
2237 sin6 = (struct sockaddr_in6 *) addr;
2238 if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
2239 !sin6->sin6_scope_id)
2240 return -EINVAL;
2241
2242 dev_addr->bound_dev_if = sin6->sin6_scope_id;
2243#endif
2244 return 0;
2245}
2246
Sean Heftye51060f2006-06-17 20:37:29 -07002247int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr)
2248{
2249 struct rdma_id_private *id_priv;
2250 int ret;
2251
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002252 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Sean Heftye51060f2006-06-17 20:37:29 -07002253 return -EAFNOSUPPORT;
2254
2255 id_priv = container_of(id, struct rdma_id_private, id);
2256 if (!cma_comp_exch(id_priv, CMA_IDLE, CMA_ADDR_BOUND))
2257 return -EINVAL;
2258
Sean Heftyd14714d2009-11-19 16:46:25 -08002259 ret = cma_check_linklocal(&id->route.addr.dev_addr, addr);
2260 if (ret)
2261 goto err1;
2262
Sean Hefty8523c042010-02-08 16:41:15 -08002263 if (!cma_any_addr(addr)) {
Sean Heftye51060f2006-06-17 20:37:29 -07002264 ret = rdma_translate_ip(addr, &id->route.addr.dev_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002265 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002266 goto err1;
2267
2268 mutex_lock(&lock);
2269 ret = cma_acquire_dev(id_priv);
2270 mutex_unlock(&lock);
2271 if (ret)
2272 goto err1;
Sean Heftye51060f2006-06-17 20:37:29 -07002273 }
2274
2275 memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr));
2276 ret = cma_get_port(id_priv);
2277 if (ret)
Krishna Kumar255d0c12006-10-24 13:22:28 -07002278 goto err2;
Sean Heftye51060f2006-06-17 20:37:29 -07002279
2280 return 0;
Krishna Kumar255d0c12006-10-24 13:22:28 -07002281err2:
Sean Hefty6f8372b2009-11-19 13:26:06 -08002282 if (id_priv->cma_dev) {
Krishna Kumar255d0c12006-10-24 13:22:28 -07002283 mutex_lock(&lock);
2284 cma_detach_from_dev(id_priv);
2285 mutex_unlock(&lock);
2286 }
2287err1:
Sean Heftye51060f2006-06-17 20:37:29 -07002288 cma_comp_exch(id_priv, CMA_ADDR_BOUND, CMA_IDLE);
2289 return ret;
2290}
2291EXPORT_SYMBOL(rdma_bind_addr);
2292
2293static int cma_format_hdr(void *hdr, enum rdma_port_space ps,
2294 struct rdma_route *route)
2295{
Sean Heftye51060f2006-06-17 20:37:29 -07002296 struct cma_hdr *cma_hdr;
2297 struct sdp_hh *sdp_hdr;
2298
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002299 if (route->addr.src_addr.ss_family == AF_INET) {
2300 struct sockaddr_in *src4, *dst4;
Sean Heftye51060f2006-06-17 20:37:29 -07002301
Aleksey Senin1f5175a2008-12-24 10:16:45 -08002302 src4 = (struct sockaddr_in *) &route->addr.src_addr;
2303 dst4 = (struct sockaddr_in *) &route->addr.dst_addr;
2304
2305 switch (ps) {
2306 case RDMA_PS_SDP:
2307 sdp_hdr = hdr;
2308 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2309 return -EINVAL;
2310 sdp_set_ip_ver(sdp_hdr, 4);
2311 sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2312 sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2313 sdp_hdr->port = src4->sin_port;
2314 break;
2315 default:
2316 cma_hdr = hdr;
2317 cma_hdr->cma_version = CMA_VERSION;
2318 cma_set_ip_ver(cma_hdr, 4);
2319 cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr;
2320 cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr;
2321 cma_hdr->port = src4->sin_port;
2322 break;
2323 }
2324 } else {
2325 struct sockaddr_in6 *src6, *dst6;
2326
2327 src6 = (struct sockaddr_in6 *) &route->addr.src_addr;
2328 dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr;
2329
2330 switch (ps) {
2331 case RDMA_PS_SDP:
2332 sdp_hdr = hdr;
2333 if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION)
2334 return -EINVAL;
2335 sdp_set_ip_ver(sdp_hdr, 6);
2336 sdp_hdr->src_addr.ip6 = src6->sin6_addr;
2337 sdp_hdr->dst_addr.ip6 = dst6->sin6_addr;
2338 sdp_hdr->port = src6->sin6_port;
2339 break;
2340 default:
2341 cma_hdr = hdr;
2342 cma_hdr->cma_version = CMA_VERSION;
2343 cma_set_ip_ver(cma_hdr, 6);
2344 cma_hdr->src_addr.ip6 = src6->sin6_addr;
2345 cma_hdr->dst_addr.ip6 = dst6->sin6_addr;
2346 cma_hdr->port = src6->sin6_port;
2347 break;
2348 }
Sean Heftye51060f2006-06-17 20:37:29 -07002349 }
2350 return 0;
2351}
2352
Sean Hefty628e5f62006-11-30 16:44:16 -08002353static int cma_sidr_rep_handler(struct ib_cm_id *cm_id,
2354 struct ib_cm_event *ib_event)
2355{
2356 struct rdma_id_private *id_priv = cm_id->context;
2357 struct rdma_cm_event event;
2358 struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd;
2359 int ret = 0;
2360
Or Gerlitzde910bd2008-07-14 23:48:53 -07002361 if (cma_disable_callback(id_priv, CMA_CONNECT))
Sean Hefty8aa08602007-05-07 11:49:00 -07002362 return 0;
Sean Hefty628e5f62006-11-30 16:44:16 -08002363
Sean Hefty8aa08602007-05-07 11:49:00 -07002364 memset(&event, 0, sizeof event);
Sean Hefty628e5f62006-11-30 16:44:16 -08002365 switch (ib_event->event) {
2366 case IB_CM_SIDR_REQ_ERROR:
2367 event.event = RDMA_CM_EVENT_UNREACHABLE;
2368 event.status = -ETIMEDOUT;
2369 break;
2370 case IB_CM_SIDR_REP_RECEIVED:
2371 event.param.ud.private_data = ib_event->private_data;
2372 event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
2373 if (rep->status != IB_SIDR_SUCCESS) {
2374 event.event = RDMA_CM_EVENT_UNREACHABLE;
2375 event.status = ib_event->param.sidr_rep_rcvd.status;
2376 break;
2377 }
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002378 ret = cma_set_qkey(id_priv);
2379 if (ret) {
2380 event.event = RDMA_CM_EVENT_ADDR_ERROR;
2381 event.status = -EINVAL;
2382 break;
2383 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08002384 if (id_priv->qkey != rep->qkey) {
Sean Hefty628e5f62006-11-30 16:44:16 -08002385 event.event = RDMA_CM_EVENT_UNREACHABLE;
2386 event.status = -EINVAL;
2387 break;
2388 }
2389 ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num,
2390 id_priv->id.route.path_rec,
2391 &event.param.ud.ah_attr);
2392 event.param.ud.qp_num = rep->qpn;
2393 event.param.ud.qkey = rep->qkey;
2394 event.event = RDMA_CM_EVENT_ESTABLISHED;
2395 event.status = 0;
2396 break;
2397 default:
Roland Dreier468f2232008-07-14 23:48:47 -07002398 printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n",
Sean Hefty628e5f62006-11-30 16:44:16 -08002399 ib_event->event);
2400 goto out;
2401 }
2402
2403 ret = id_priv->id.event_handler(&id_priv->id, &event);
2404 if (ret) {
2405 /* Destroy the CM ID by returning a non-zero value. */
2406 id_priv->cm_id.ib = NULL;
2407 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002408 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002409 rdma_destroy_id(&id_priv->id);
2410 return ret;
2411 }
2412out:
Or Gerlitzde910bd2008-07-14 23:48:53 -07002413 mutex_unlock(&id_priv->handler_mutex);
Sean Hefty628e5f62006-11-30 16:44:16 -08002414 return ret;
2415}
2416
2417static int cma_resolve_ib_udp(struct rdma_id_private *id_priv,
2418 struct rdma_conn_param *conn_param)
2419{
2420 struct ib_cm_sidr_req_param req;
2421 struct rdma_route *route;
2422 int ret;
2423
2424 req.private_data_len = sizeof(struct cma_hdr) +
2425 conn_param->private_data_len;
2426 req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2427 if (!req.private_data)
2428 return -ENOMEM;
2429
2430 if (conn_param->private_data && conn_param->private_data_len)
2431 memcpy((void *) req.private_data + sizeof(struct cma_hdr),
2432 conn_param->private_data, conn_param->private_data_len);
2433
2434 route = &id_priv->id.route;
2435 ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route);
2436 if (ret)
2437 goto out;
2438
2439 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device,
2440 cma_sidr_rep_handler, id_priv);
2441 if (IS_ERR(id_priv->cm_id.ib)) {
2442 ret = PTR_ERR(id_priv->cm_id.ib);
2443 goto out;
2444 }
2445
2446 req.path = route->path_rec;
2447 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002448 (struct sockaddr *) &route->addr.dst_addr);
Sean Hefty628e5f62006-11-30 16:44:16 -08002449 req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8);
2450 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2451
2452 ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req);
2453 if (ret) {
2454 ib_destroy_cm_id(id_priv->cm_id.ib);
2455 id_priv->cm_id.ib = NULL;
2456 }
2457out:
2458 kfree(req.private_data);
2459 return ret;
2460}
2461
Sean Heftye51060f2006-06-17 20:37:29 -07002462static int cma_connect_ib(struct rdma_id_private *id_priv,
2463 struct rdma_conn_param *conn_param)
2464{
2465 struct ib_cm_req_param req;
2466 struct rdma_route *route;
2467 void *private_data;
2468 int offset, ret;
2469
2470 memset(&req, 0, sizeof req);
2471 offset = cma_user_data_offset(id_priv->id.ps);
2472 req.private_data_len = offset + conn_param->private_data_len;
2473 private_data = kzalloc(req.private_data_len, GFP_ATOMIC);
2474 if (!private_data)
2475 return -ENOMEM;
2476
2477 if (conn_param->private_data && conn_param->private_data_len)
2478 memcpy(private_data + offset, conn_param->private_data,
2479 conn_param->private_data_len);
2480
2481 id_priv->cm_id.ib = ib_create_cm_id(id_priv->id.device, cma_ib_handler,
2482 id_priv);
2483 if (IS_ERR(id_priv->cm_id.ib)) {
2484 ret = PTR_ERR(id_priv->cm_id.ib);
2485 goto out;
2486 }
2487
2488 route = &id_priv->id.route;
2489 ret = cma_format_hdr(private_data, id_priv->id.ps, route);
2490 if (ret)
2491 goto out;
2492 req.private_data = private_data;
2493
2494 req.primary_path = &route->path_rec[0];
2495 if (route->num_paths == 2)
2496 req.alternate_path = &route->path_rec[1];
2497
2498 req.service_id = cma_get_service_id(id_priv->id.ps,
Roland Dreier3f446752008-08-04 11:02:14 -07002499 (struct sockaddr *) &route->addr.dst_addr);
Sean Heftye51060f2006-06-17 20:37:29 -07002500 req.qp_num = id_priv->qp_num;
Sean Hefty9b2e9c02006-11-30 16:30:47 -08002501 req.qp_type = IB_QPT_RC;
Sean Heftye51060f2006-06-17 20:37:29 -07002502 req.starting_psn = id_priv->seq_num;
2503 req.responder_resources = conn_param->responder_resources;
2504 req.initiator_depth = conn_param->initiator_depth;
2505 req.flow_control = conn_param->flow_control;
2506 req.retry_count = conn_param->retry_count;
2507 req.rnr_retry_count = conn_param->rnr_retry_count;
2508 req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2509 req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT;
2510 req.max_cm_retries = CMA_MAX_CM_RETRIES;
2511 req.srq = id_priv->srq ? 1 : 0;
2512
2513 ret = ib_send_cm_req(id_priv->cm_id.ib, &req);
2514out:
Krishna Kumar675a0272006-09-29 11:47:06 -07002515 if (ret && !IS_ERR(id_priv->cm_id.ib)) {
2516 ib_destroy_cm_id(id_priv->cm_id.ib);
2517 id_priv->cm_id.ib = NULL;
2518 }
2519
Sean Heftye51060f2006-06-17 20:37:29 -07002520 kfree(private_data);
2521 return ret;
2522}
2523
Tom Tucker07ebafb2006-08-03 16:02:42 -05002524static int cma_connect_iw(struct rdma_id_private *id_priv,
2525 struct rdma_conn_param *conn_param)
2526{
2527 struct iw_cm_id *cm_id;
2528 struct sockaddr_in* sin;
2529 int ret;
2530 struct iw_cm_conn_param iw_param;
2531
2532 cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv);
2533 if (IS_ERR(cm_id)) {
2534 ret = PTR_ERR(cm_id);
2535 goto out;
2536 }
2537
2538 id_priv->cm_id.iw = cm_id;
2539
2540 sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr;
2541 cm_id->local_addr = *sin;
2542
2543 sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr;
2544 cm_id->remote_addr = *sin;
2545
Sean Hefty5851bb82008-01-04 10:47:12 -08002546 ret = cma_modify_qp_rtr(id_priv, conn_param);
Krishna Kumar675a0272006-09-29 11:47:06 -07002547 if (ret)
2548 goto out;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002549
2550 iw_param.ord = conn_param->initiator_depth;
2551 iw_param.ird = conn_param->responder_resources;
2552 iw_param.private_data = conn_param->private_data;
2553 iw_param.private_data_len = conn_param->private_data_len;
2554 if (id_priv->id.qp)
2555 iw_param.qpn = id_priv->qp_num;
2556 else
2557 iw_param.qpn = conn_param->qp_num;
2558 ret = iw_cm_connect(cm_id, &iw_param);
2559out:
Krishna Kumar675a0272006-09-29 11:47:06 -07002560 if (ret && !IS_ERR(cm_id)) {
2561 iw_destroy_cm_id(cm_id);
2562 id_priv->cm_id.iw = NULL;
2563 }
Tom Tucker07ebafb2006-08-03 16:02:42 -05002564 return ret;
2565}
2566
Sean Heftye51060f2006-06-17 20:37:29 -07002567int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2568{
2569 struct rdma_id_private *id_priv;
2570 int ret;
2571
2572 id_priv = container_of(id, struct rdma_id_private, id);
2573 if (!cma_comp_exch(id_priv, CMA_ROUTE_RESOLVED, CMA_CONNECT))
2574 return -EINVAL;
2575
2576 if (!id->qp) {
2577 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002578 id_priv->srq = conn_param->srq;
2579 }
2580
Tom Tucker07ebafb2006-08-03 16:02:42 -05002581 switch (rdma_node_get_transport(id->device->node_type)) {
2582 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -08002583 if (cma_is_ud_ps(id->ps))
Sean Hefty628e5f62006-11-30 16:44:16 -08002584 ret = cma_resolve_ib_udp(id_priv, conn_param);
2585 else
2586 ret = cma_connect_ib(id_priv, conn_param);
Sean Heftye51060f2006-06-17 20:37:29 -07002587 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002588 case RDMA_TRANSPORT_IWARP:
2589 ret = cma_connect_iw(id_priv, conn_param);
2590 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002591 default:
2592 ret = -ENOSYS;
2593 break;
2594 }
2595 if (ret)
2596 goto err;
2597
2598 return 0;
2599err:
2600 cma_comp_exch(id_priv, CMA_CONNECT, CMA_ROUTE_RESOLVED);
2601 return ret;
2602}
2603EXPORT_SYMBOL(rdma_connect);
2604
2605static int cma_accept_ib(struct rdma_id_private *id_priv,
2606 struct rdma_conn_param *conn_param)
2607{
2608 struct ib_cm_rep_param rep;
Sean Hefty5851bb82008-01-04 10:47:12 -08002609 int ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002610
Sean Hefty5851bb82008-01-04 10:47:12 -08002611 ret = cma_modify_qp_rtr(id_priv, conn_param);
2612 if (ret)
2613 goto out;
Sean Hefty0fe313b2006-11-30 16:37:15 -08002614
Sean Hefty5851bb82008-01-04 10:47:12 -08002615 ret = cma_modify_qp_rts(id_priv, conn_param);
2616 if (ret)
2617 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002618
2619 memset(&rep, 0, sizeof rep);
2620 rep.qp_num = id_priv->qp_num;
2621 rep.starting_psn = id_priv->seq_num;
2622 rep.private_data = conn_param->private_data;
2623 rep.private_data_len = conn_param->private_data_len;
2624 rep.responder_resources = conn_param->responder_resources;
2625 rep.initiator_depth = conn_param->initiator_depth;
Sean Heftye51060f2006-06-17 20:37:29 -07002626 rep.failover_accepted = 0;
2627 rep.flow_control = conn_param->flow_control;
2628 rep.rnr_retry_count = conn_param->rnr_retry_count;
2629 rep.srq = id_priv->srq ? 1 : 0;
2630
Sean Hefty0fe313b2006-11-30 16:37:15 -08002631 ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep);
2632out:
2633 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07002634}
2635
Tom Tucker07ebafb2006-08-03 16:02:42 -05002636static int cma_accept_iw(struct rdma_id_private *id_priv,
2637 struct rdma_conn_param *conn_param)
2638{
2639 struct iw_cm_conn_param iw_param;
2640 int ret;
2641
Sean Hefty5851bb82008-01-04 10:47:12 -08002642 ret = cma_modify_qp_rtr(id_priv, conn_param);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002643 if (ret)
2644 return ret;
2645
2646 iw_param.ord = conn_param->initiator_depth;
2647 iw_param.ird = conn_param->responder_resources;
2648 iw_param.private_data = conn_param->private_data;
2649 iw_param.private_data_len = conn_param->private_data_len;
2650 if (id_priv->id.qp) {
2651 iw_param.qpn = id_priv->qp_num;
2652 } else
2653 iw_param.qpn = conn_param->qp_num;
2654
2655 return iw_cm_accept(id_priv->cm_id.iw, &iw_param);
2656}
2657
Sean Hefty628e5f62006-11-30 16:44:16 -08002658static int cma_send_sidr_rep(struct rdma_id_private *id_priv,
2659 enum ib_cm_sidr_status status,
2660 const void *private_data, int private_data_len)
2661{
2662 struct ib_cm_sidr_rep_param rep;
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002663 int ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002664
2665 memset(&rep, 0, sizeof rep);
2666 rep.status = status;
2667 if (status == IB_SIDR_SUCCESS) {
Yossi Etigind2ca39f2009-04-08 13:42:33 -07002668 ret = cma_set_qkey(id_priv);
2669 if (ret)
2670 return ret;
Sean Hefty628e5f62006-11-30 16:44:16 -08002671 rep.qp_num = id_priv->qp_num;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002672 rep.qkey = id_priv->qkey;
Sean Hefty628e5f62006-11-30 16:44:16 -08002673 }
2674 rep.private_data = private_data;
2675 rep.private_data_len = private_data_len;
2676
2677 return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep);
2678}
2679
Sean Heftye51060f2006-06-17 20:37:29 -07002680int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param)
2681{
2682 struct rdma_id_private *id_priv;
2683 int ret;
2684
2685 id_priv = container_of(id, struct rdma_id_private, id);
2686 if (!cma_comp(id_priv, CMA_CONNECT))
2687 return -EINVAL;
2688
2689 if (!id->qp && conn_param) {
2690 id_priv->qp_num = conn_param->qp_num;
Sean Heftye51060f2006-06-17 20:37:29 -07002691 id_priv->srq = conn_param->srq;
2692 }
2693
Tom Tucker07ebafb2006-08-03 16:02:42 -05002694 switch (rdma_node_get_transport(id->device->node_type)) {
2695 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -08002696 if (cma_is_ud_ps(id->ps))
Sean Hefty628e5f62006-11-30 16:44:16 -08002697 ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS,
2698 conn_param->private_data,
2699 conn_param->private_data_len);
2700 else if (conn_param)
Sean Heftye51060f2006-06-17 20:37:29 -07002701 ret = cma_accept_ib(id_priv, conn_param);
2702 else
2703 ret = cma_rep_recv(id_priv);
2704 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002705 case RDMA_TRANSPORT_IWARP:
2706 ret = cma_accept_iw(id_priv, conn_param);
2707 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002708 default:
2709 ret = -ENOSYS;
2710 break;
2711 }
2712
2713 if (ret)
2714 goto reject;
2715
2716 return 0;
2717reject:
Sean Heftyc5483382007-09-24 13:19:09 -07002718 cma_modify_qp_err(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07002719 rdma_reject(id, NULL, 0);
2720 return ret;
2721}
2722EXPORT_SYMBOL(rdma_accept);
2723
Sean Hefty0fe313b2006-11-30 16:37:15 -08002724int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event)
2725{
2726 struct rdma_id_private *id_priv;
2727 int ret;
2728
2729 id_priv = container_of(id, struct rdma_id_private, id);
Sean Hefty6c719f52007-05-07 11:49:27 -07002730 if (!cma_has_cm_dev(id_priv))
Sean Hefty0fe313b2006-11-30 16:37:15 -08002731 return -EINVAL;
2732
2733 switch (id->device->node_type) {
2734 case RDMA_NODE_IB_CA:
2735 ret = ib_cm_notify(id_priv->cm_id.ib, event);
2736 break;
2737 default:
2738 ret = 0;
2739 break;
2740 }
2741 return ret;
2742}
2743EXPORT_SYMBOL(rdma_notify);
2744
Sean Heftye51060f2006-06-17 20:37:29 -07002745int rdma_reject(struct rdma_cm_id *id, const void *private_data,
2746 u8 private_data_len)
2747{
2748 struct rdma_id_private *id_priv;
2749 int ret;
2750
2751 id_priv = container_of(id, struct rdma_id_private, id);
Sean Hefty6c719f52007-05-07 11:49:27 -07002752 if (!cma_has_cm_dev(id_priv))
Sean Heftye51060f2006-06-17 20:37:29 -07002753 return -EINVAL;
2754
Tom Tucker07ebafb2006-08-03 16:02:42 -05002755 switch (rdma_node_get_transport(id->device->node_type)) {
2756 case RDMA_TRANSPORT_IB:
Sean Heftyc8f6a362007-02-15 17:00:18 -08002757 if (cma_is_ud_ps(id->ps))
Sean Hefty628e5f62006-11-30 16:44:16 -08002758 ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT,
2759 private_data, private_data_len);
2760 else
2761 ret = ib_send_cm_rej(id_priv->cm_id.ib,
2762 IB_CM_REJ_CONSUMER_DEFINED, NULL,
2763 0, private_data, private_data_len);
Sean Heftye51060f2006-06-17 20:37:29 -07002764 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002765 case RDMA_TRANSPORT_IWARP:
2766 ret = iw_cm_reject(id_priv->cm_id.iw,
2767 private_data, private_data_len);
2768 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002769 default:
2770 ret = -ENOSYS;
2771 break;
2772 }
2773 return ret;
2774}
2775EXPORT_SYMBOL(rdma_reject);
2776
2777int rdma_disconnect(struct rdma_cm_id *id)
2778{
2779 struct rdma_id_private *id_priv;
2780 int ret;
2781
2782 id_priv = container_of(id, struct rdma_id_private, id);
Sean Hefty6c719f52007-05-07 11:49:27 -07002783 if (!cma_has_cm_dev(id_priv))
Sean Heftye51060f2006-06-17 20:37:29 -07002784 return -EINVAL;
2785
Tom Tucker07ebafb2006-08-03 16:02:42 -05002786 switch (rdma_node_get_transport(id->device->node_type)) {
2787 case RDMA_TRANSPORT_IB:
Sean Heftyc5483382007-09-24 13:19:09 -07002788 ret = cma_modify_qp_err(id_priv);
Tom Tucker07ebafb2006-08-03 16:02:42 -05002789 if (ret)
2790 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07002791 /* Initiate or respond to a disconnect. */
2792 if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0))
2793 ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0);
2794 break;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002795 case RDMA_TRANSPORT_IWARP:
2796 ret = iw_cm_disconnect(id_priv->cm_id.iw, 0);
2797 break;
Sean Heftye51060f2006-06-17 20:37:29 -07002798 default:
Tom Tucker07ebafb2006-08-03 16:02:42 -05002799 ret = -EINVAL;
Sean Heftye51060f2006-06-17 20:37:29 -07002800 break;
2801 }
2802out:
2803 return ret;
2804}
2805EXPORT_SYMBOL(rdma_disconnect);
2806
Sean Heftyc8f6a362007-02-15 17:00:18 -08002807static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast)
2808{
2809 struct rdma_id_private *id_priv;
2810 struct cma_multicast *mc = multicast->context;
2811 struct rdma_cm_event event;
2812 int ret;
2813
2814 id_priv = mc->id_priv;
Or Gerlitzde910bd2008-07-14 23:48:53 -07002815 if (cma_disable_callback(id_priv, CMA_ADDR_BOUND) &&
2816 cma_disable_callback(id_priv, CMA_ADDR_RESOLVED))
Sean Hefty8aa08602007-05-07 11:49:00 -07002817 return 0;
Sean Heftyc8f6a362007-02-15 17:00:18 -08002818
Sean Heftyc5483382007-09-24 13:19:09 -07002819 mutex_lock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002820 if (!status && id_priv->id.qp)
2821 status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid,
2822 multicast->rec.mlid);
Sean Heftyc5483382007-09-24 13:19:09 -07002823 mutex_unlock(&id_priv->qp_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002824
2825 memset(&event, 0, sizeof event);
2826 event.status = status;
2827 event.param.ud.private_data = mc->context;
2828 if (!status) {
2829 event.event = RDMA_CM_EVENT_MULTICAST_JOIN;
2830 ib_init_ah_from_mcmember(id_priv->id.device,
2831 id_priv->id.port_num, &multicast->rec,
2832 &event.param.ud.ah_attr);
2833 event.param.ud.qp_num = 0xFFFFFF;
2834 event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey);
2835 } else
2836 event.event = RDMA_CM_EVENT_MULTICAST_ERROR;
2837
2838 ret = id_priv->id.event_handler(&id_priv->id, &event);
2839 if (ret) {
2840 cma_exch(id_priv, CMA_DESTROYING);
Or Gerlitzde910bd2008-07-14 23:48:53 -07002841 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002842 rdma_destroy_id(&id_priv->id);
2843 return 0;
2844 }
Sean Hefty8aa08602007-05-07 11:49:00 -07002845
Or Gerlitzde910bd2008-07-14 23:48:53 -07002846 mutex_unlock(&id_priv->handler_mutex);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002847 return 0;
2848}
2849
2850static void cma_set_mgid(struct rdma_id_private *id_priv,
2851 struct sockaddr *addr, union ib_gid *mgid)
2852{
2853 unsigned char mc_map[MAX_ADDR_LEN];
2854 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2855 struct sockaddr_in *sin = (struct sockaddr_in *) addr;
2856 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr;
2857
2858 if (cma_any_addr(addr)) {
2859 memset(mgid, 0, sizeof *mgid);
2860 } else if ((addr->sa_family == AF_INET6) &&
Jason Gunthorpe1c9b2812009-11-19 12:55:21 -08002861 ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) ==
Sean Heftyc8f6a362007-02-15 17:00:18 -08002862 0xFF10A01B)) {
2863 /* IPv6 address is an SA assigned MGID. */
2864 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
Jason Gunthorpee2e62692009-11-19 12:55:22 -08002865 } else if ((addr->sa_family == AF_INET6)) {
2866 ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map);
2867 if (id_priv->id.ps == RDMA_PS_UDP)
2868 mc_map[7] = 0x01; /* Use RDMA CM signature */
2869 *mgid = *(union ib_gid *) (mc_map + 4);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002870 } else {
Rolf Manderscheida9e527e2007-12-10 13:38:41 -07002871 ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002872 if (id_priv->id.ps == RDMA_PS_UDP)
2873 mc_map[7] = 0x01; /* Use RDMA CM signature */
Sean Heftyc8f6a362007-02-15 17:00:18 -08002874 *mgid = *(union ib_gid *) (mc_map + 4);
2875 }
2876}
2877
2878static int cma_join_ib_multicast(struct rdma_id_private *id_priv,
2879 struct cma_multicast *mc)
2880{
2881 struct ib_sa_mcmember_rec rec;
2882 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2883 ib_sa_comp_mask comp_mask;
2884 int ret;
2885
2886 ib_addr_get_mgid(dev_addr, &rec.mgid);
2887 ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num,
2888 &rec.mgid, &rec);
2889 if (ret)
2890 return ret;
2891
Roland Dreier3f446752008-08-04 11:02:14 -07002892 cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002893 if (id_priv->id.ps == RDMA_PS_UDP)
2894 rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
Sean Hefty6f8372b2009-11-19 13:26:06 -08002895 rdma_addr_get_sgid(dev_addr, &rec.port_gid);
Sean Heftyc8f6a362007-02-15 17:00:18 -08002896 rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
2897 rec.join_state = 1;
2898
2899 comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID |
2900 IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE |
2901 IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL |
2902 IB_SA_MCMEMBER_REC_FLOW_LABEL |
2903 IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
2904
Yossi Etigin84adeee2009-04-01 13:55:32 -07002905 if (id_priv->id.ps == RDMA_PS_IPOIB)
2906 comp_mask |= IB_SA_MCMEMBER_REC_RATE |
2907 IB_SA_MCMEMBER_REC_RATE_SELECTOR;
2908
Sean Heftyc8f6a362007-02-15 17:00:18 -08002909 mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device,
2910 id_priv->id.port_num, &rec,
2911 comp_mask, GFP_KERNEL,
2912 cma_ib_mc_handler, mc);
2913 if (IS_ERR(mc->multicast.ib))
2914 return PTR_ERR(mc->multicast.ib);
2915
2916 return 0;
2917}
2918
Eli Cohen3c86aa72010-10-13 21:26:51 +02002919static void iboe_mcast_work_handler(struct work_struct *work)
2920{
2921 struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work);
2922 struct cma_multicast *mc = mw->mc;
2923 struct ib_sa_multicast *m = mc->multicast.ib;
2924
2925 mc->multicast.ib->context = mc;
2926 cma_ib_mc_handler(0, m);
2927 kref_put(&mc->mcref, release_mc);
2928 kfree(mw);
2929}
2930
2931static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid)
2932{
2933 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
2934 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
2935
2936 if (cma_any_addr(addr)) {
2937 memset(mgid, 0, sizeof *mgid);
2938 } else if (addr->sa_family == AF_INET6) {
2939 memcpy(mgid, &sin6->sin6_addr, sizeof *mgid);
2940 } else {
2941 mgid->raw[0] = 0xff;
2942 mgid->raw[1] = 0x0e;
2943 mgid->raw[2] = 0;
2944 mgid->raw[3] = 0;
2945 mgid->raw[4] = 0;
2946 mgid->raw[5] = 0;
2947 mgid->raw[6] = 0;
2948 mgid->raw[7] = 0;
2949 mgid->raw[8] = 0;
2950 mgid->raw[9] = 0;
2951 mgid->raw[10] = 0xff;
2952 mgid->raw[11] = 0xff;
2953 *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr;
2954 }
2955}
2956
2957static int cma_iboe_join_multicast(struct rdma_id_private *id_priv,
2958 struct cma_multicast *mc)
2959{
2960 struct iboe_mcast_work *work;
2961 struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr;
2962 int err;
2963 struct sockaddr *addr = (struct sockaddr *)&mc->addr;
2964 struct net_device *ndev = NULL;
2965
2966 if (cma_zero_addr((struct sockaddr *)&mc->addr))
2967 return -EINVAL;
2968
2969 work = kzalloc(sizeof *work, GFP_KERNEL);
2970 if (!work)
2971 return -ENOMEM;
2972
2973 mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL);
2974 if (!mc->multicast.ib) {
2975 err = -ENOMEM;
2976 goto out1;
2977 }
2978
2979 cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid);
2980
2981 mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff);
2982 if (id_priv->id.ps == RDMA_PS_UDP)
2983 mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY);
2984
2985 if (dev_addr->bound_dev_if)
2986 ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
2987 if (!ndev) {
2988 err = -ENODEV;
2989 goto out2;
2990 }
2991 mc->multicast.ib->rec.rate = iboe_get_rate(ndev);
2992 mc->multicast.ib->rec.hop_limit = 1;
2993 mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu);
2994 dev_put(ndev);
2995 if (!mc->multicast.ib->rec.mtu) {
2996 err = -EINVAL;
2997 goto out2;
2998 }
2999 iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid);
3000 work->id = id_priv;
3001 work->mc = mc;
3002 INIT_WORK(&work->work, iboe_mcast_work_handler);
3003 kref_get(&mc->mcref);
3004 queue_work(cma_wq, &work->work);
3005
3006 return 0;
3007
3008out2:
3009 kfree(mc->multicast.ib);
3010out1:
3011 kfree(work);
3012 return err;
3013}
3014
Sean Heftyc8f6a362007-02-15 17:00:18 -08003015int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr,
3016 void *context)
3017{
3018 struct rdma_id_private *id_priv;
3019 struct cma_multicast *mc;
3020 int ret;
3021
3022 id_priv = container_of(id, struct rdma_id_private, id);
3023 if (!cma_comp(id_priv, CMA_ADDR_BOUND) &&
3024 !cma_comp(id_priv, CMA_ADDR_RESOLVED))
3025 return -EINVAL;
3026
3027 mc = kmalloc(sizeof *mc, GFP_KERNEL);
3028 if (!mc)
3029 return -ENOMEM;
3030
3031 memcpy(&mc->addr, addr, ip_addr_size(addr));
3032 mc->context = context;
3033 mc->id_priv = id_priv;
3034
3035 spin_lock(&id_priv->lock);
3036 list_add(&mc->list, &id_priv->mc_list);
3037 spin_unlock(&id_priv->lock);
3038
3039 switch (rdma_node_get_transport(id->device->node_type)) {
3040 case RDMA_TRANSPORT_IB:
Eli Cohen3c86aa72010-10-13 21:26:51 +02003041 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3042 case IB_LINK_LAYER_INFINIBAND:
3043 ret = cma_join_ib_multicast(id_priv, mc);
3044 break;
3045 case IB_LINK_LAYER_ETHERNET:
3046 kref_init(&mc->mcref);
3047 ret = cma_iboe_join_multicast(id_priv, mc);
3048 break;
3049 default:
3050 ret = -EINVAL;
3051 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003052 break;
3053 default:
3054 ret = -ENOSYS;
3055 break;
3056 }
3057
3058 if (ret) {
3059 spin_lock_irq(&id_priv->lock);
3060 list_del(&mc->list);
3061 spin_unlock_irq(&id_priv->lock);
3062 kfree(mc);
3063 }
3064 return ret;
3065}
3066EXPORT_SYMBOL(rdma_join_multicast);
3067
3068void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr)
3069{
3070 struct rdma_id_private *id_priv;
3071 struct cma_multicast *mc;
3072
3073 id_priv = container_of(id, struct rdma_id_private, id);
3074 spin_lock_irq(&id_priv->lock);
3075 list_for_each_entry(mc, &id_priv->mc_list, list) {
3076 if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) {
3077 list_del(&mc->list);
3078 spin_unlock_irq(&id_priv->lock);
3079
3080 if (id->qp)
3081 ib_detach_mcast(id->qp,
3082 &mc->multicast.ib->rec.mgid,
3083 mc->multicast.ib->rec.mlid);
Eli Cohen3c86aa72010-10-13 21:26:51 +02003084 if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) {
3085 switch (rdma_port_get_link_layer(id->device, id->port_num)) {
3086 case IB_LINK_LAYER_INFINIBAND:
3087 ib_sa_free_multicast(mc->multicast.ib);
3088 kfree(mc);
3089 break;
3090 case IB_LINK_LAYER_ETHERNET:
3091 kref_put(&mc->mcref, release_mc);
3092 break;
3093 default:
3094 break;
3095 }
3096 }
Sean Heftyc8f6a362007-02-15 17:00:18 -08003097 return;
3098 }
3099 }
3100 spin_unlock_irq(&id_priv->lock);
3101}
3102EXPORT_SYMBOL(rdma_leave_multicast);
3103
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003104static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv)
3105{
3106 struct rdma_dev_addr *dev_addr;
3107 struct cma_ndev_work *work;
3108
3109 dev_addr = &id_priv->id.route.addr.dev_addr;
3110
Sean Hefty6266ed62009-11-19 12:55:22 -08003111 if ((dev_addr->bound_dev_if == ndev->ifindex) &&
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003112 memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) {
3113 printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n",
3114 ndev->name, &id_priv->id);
3115 work = kzalloc(sizeof *work, GFP_KERNEL);
3116 if (!work)
3117 return -ENOMEM;
3118
3119 INIT_WORK(&work->work, cma_ndev_work_handler);
3120 work->id = id_priv;
3121 work->event.event = RDMA_CM_EVENT_ADDR_CHANGE;
3122 atomic_inc(&id_priv->refcount);
3123 queue_work(cma_wq, &work->work);
3124 }
3125
3126 return 0;
3127}
3128
3129static int cma_netdev_callback(struct notifier_block *self, unsigned long event,
3130 void *ctx)
3131{
3132 struct net_device *ndev = (struct net_device *)ctx;
3133 struct cma_device *cma_dev;
3134 struct rdma_id_private *id_priv;
3135 int ret = NOTIFY_DONE;
3136
3137 if (dev_net(ndev) != &init_net)
3138 return NOTIFY_DONE;
3139
3140 if (event != NETDEV_BONDING_FAILOVER)
3141 return NOTIFY_DONE;
3142
3143 if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING))
3144 return NOTIFY_DONE;
3145
3146 mutex_lock(&lock);
3147 list_for_each_entry(cma_dev, &dev_list, list)
3148 list_for_each_entry(id_priv, &cma_dev->id_list, list) {
3149 ret = cma_netdev_change(ndev, id_priv);
3150 if (ret)
3151 goto out;
3152 }
3153
3154out:
3155 mutex_unlock(&lock);
3156 return ret;
3157}
3158
3159static struct notifier_block cma_nb = {
3160 .notifier_call = cma_netdev_callback
3161};
3162
Sean Heftye51060f2006-06-17 20:37:29 -07003163static void cma_add_one(struct ib_device *device)
3164{
3165 struct cma_device *cma_dev;
3166 struct rdma_id_private *id_priv;
3167
3168 cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL);
3169 if (!cma_dev)
3170 return;
3171
3172 cma_dev->device = device;
Sean Heftye51060f2006-06-17 20:37:29 -07003173
3174 init_completion(&cma_dev->comp);
3175 atomic_set(&cma_dev->refcount, 1);
3176 INIT_LIST_HEAD(&cma_dev->id_list);
3177 ib_set_client_data(device, &cma_client, cma_dev);
3178
3179 mutex_lock(&lock);
3180 list_add_tail(&cma_dev->list, &dev_list);
3181 list_for_each_entry(id_priv, &listen_any_list, list)
3182 cma_listen_on_dev(id_priv, cma_dev);
3183 mutex_unlock(&lock);
Sean Heftye51060f2006-06-17 20:37:29 -07003184}
3185
3186static int cma_remove_id_dev(struct rdma_id_private *id_priv)
3187{
Sean Heftya1b1b612006-11-30 16:33:14 -08003188 struct rdma_cm_event event;
Sean Heftye51060f2006-06-17 20:37:29 -07003189 enum cma_state state;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003190 int ret = 0;
Sean Heftye51060f2006-06-17 20:37:29 -07003191
3192 /* Record that we want to remove the device */
3193 state = cma_exch(id_priv, CMA_DEVICE_REMOVAL);
3194 if (state == CMA_DESTROYING)
3195 return 0;
3196
3197 cma_cancel_operation(id_priv, state);
Or Gerlitzde910bd2008-07-14 23:48:53 -07003198 mutex_lock(&id_priv->handler_mutex);
Sean Heftye51060f2006-06-17 20:37:29 -07003199
3200 /* Check for destruction from another callback. */
3201 if (!cma_comp(id_priv, CMA_DEVICE_REMOVAL))
Or Gerlitzde910bd2008-07-14 23:48:53 -07003202 goto out;
Sean Heftye51060f2006-06-17 20:37:29 -07003203
Sean Heftya1b1b612006-11-30 16:33:14 -08003204 memset(&event, 0, sizeof event);
3205 event.event = RDMA_CM_EVENT_DEVICE_REMOVAL;
Or Gerlitzde910bd2008-07-14 23:48:53 -07003206 ret = id_priv->id.event_handler(&id_priv->id, &event);
3207out:
3208 mutex_unlock(&id_priv->handler_mutex);
3209 return ret;
Sean Heftye51060f2006-06-17 20:37:29 -07003210}
3211
3212static void cma_process_remove(struct cma_device *cma_dev)
3213{
Sean Heftye51060f2006-06-17 20:37:29 -07003214 struct rdma_id_private *id_priv;
3215 int ret;
3216
Sean Heftye51060f2006-06-17 20:37:29 -07003217 mutex_lock(&lock);
3218 while (!list_empty(&cma_dev->id_list)) {
3219 id_priv = list_entry(cma_dev->id_list.next,
3220 struct rdma_id_private, list);
3221
Sean Heftyd02d1f52007-10-09 11:12:34 -07003222 list_del(&id_priv->listen_list);
Krishna Kumar94de1782006-09-29 12:03:35 -07003223 list_del_init(&id_priv->list);
Sean Heftye51060f2006-06-17 20:37:29 -07003224 atomic_inc(&id_priv->refcount);
3225 mutex_unlock(&lock);
3226
Sean Heftyd02d1f52007-10-09 11:12:34 -07003227 ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv);
Sean Heftye51060f2006-06-17 20:37:29 -07003228 cma_deref_id(id_priv);
3229 if (ret)
3230 rdma_destroy_id(&id_priv->id);
3231
3232 mutex_lock(&lock);
3233 }
3234 mutex_unlock(&lock);
3235
3236 cma_deref_dev(cma_dev);
3237 wait_for_completion(&cma_dev->comp);
3238}
3239
3240static void cma_remove_one(struct ib_device *device)
3241{
3242 struct cma_device *cma_dev;
3243
3244 cma_dev = ib_get_client_data(device, &cma_client);
3245 if (!cma_dev)
3246 return;
3247
3248 mutex_lock(&lock);
3249 list_del(&cma_dev->list);
3250 mutex_unlock(&lock);
3251
3252 cma_process_remove(cma_dev);
3253 kfree(cma_dev);
3254}
3255
Peter Huewe716abb12009-06-23 10:38:42 -07003256static int __init cma_init(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003257{
Tetsuo Handa5d7220e82010-04-15 11:29:04 +09003258 int ret;
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003259
Sean Heftyc7f743a2007-02-01 12:23:37 -08003260 cma_wq = create_singlethread_workqueue("rdma_cm");
Sean Heftye51060f2006-06-17 20:37:29 -07003261 if (!cma_wq)
3262 return -ENOMEM;
3263
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003264 ib_sa_register_client(&sa_client);
Sean Hefty7a118df2006-10-31 11:12:59 -08003265 rdma_addr_register_client(&addr_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003266 register_netdevice_notifier(&cma_nb);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003267
Sean Heftye51060f2006-06-17 20:37:29 -07003268 ret = ib_register_client(&cma_client);
3269 if (ret)
3270 goto err;
3271 return 0;
3272
3273err:
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003274 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003275 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003276 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003277 destroy_workqueue(cma_wq);
3278 return ret;
3279}
3280
Peter Huewe716abb12009-06-23 10:38:42 -07003281static void __exit cma_cleanup(void)
Sean Heftye51060f2006-06-17 20:37:29 -07003282{
3283 ib_unregister_client(&cma_client);
Or Gerlitzdd5bdff2008-07-22 14:14:22 -07003284 unregister_netdevice_notifier(&cma_nb);
Sean Hefty7a118df2006-10-31 11:12:59 -08003285 rdma_addr_unregister_client(&addr_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07003286 ib_sa_unregister_client(&sa_client);
Sean Heftye51060f2006-06-17 20:37:29 -07003287 destroy_workqueue(cma_wq);
3288 idr_destroy(&sdp_ps);
3289 idr_destroy(&tcp_ps);
Sean Hefty628e5f62006-11-30 16:44:16 -08003290 idr_destroy(&udp_ps);
Sean Heftyc8f6a362007-02-15 17:00:18 -08003291 idr_destroy(&ipoib_ps);
Sean Heftye51060f2006-06-17 20:37:29 -07003292}
3293
3294module_init(cma_init);
3295module_exit(cma_cleanup);