Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 7 | * 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 Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 12 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 13 | * Redistribution and use in source and binary forms, with or |
| 14 | * without modification, are permitted provided that the following |
| 15 | * conditions are met: |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 16 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 17 | * - Redistributions of source code must retain the above |
| 18 | * copyright notice, this list of conditions and the following |
| 19 | * disclaimer. |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 20 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 21 | * - 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 Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 25 | * |
Sean Hefty | a947491 | 2008-07-14 23:48:43 -0700 | [diff] [blame] | 26 | * 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 Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 34 | */ |
| 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 Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 42 | #include <linux/inetdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 44 | |
| 45 | #include <net/tcp.h> |
Aleksey Senin | 1f5175a | 2008-12-24 10:16:45 -0800 | [diff] [blame] | 46 | #include <net/ipv6.h> |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 47 | |
| 48 | #include <rdma/rdma_cm.h> |
| 49 | #include <rdma/rdma_cm_ib.h> |
Nir Muchtar | 753f618 | 2011-01-03 15:33:53 +0000 | [diff] [blame] | 50 | #include <rdma/rdma_netlink.h> |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 51 | #include <rdma/ib_cache.h> |
| 52 | #include <rdma/ib_cm.h> |
| 53 | #include <rdma/ib_sa.h> |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 54 | #include <rdma/iw_cm.h> |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 55 | |
| 56 | MODULE_AUTHOR("Sean Hefty"); |
| 57 | MODULE_DESCRIPTION("Generic RDMA CM Agent"); |
| 58 | MODULE_LICENSE("Dual BSD/GPL"); |
| 59 | |
| 60 | #define CMA_CM_RESPONSE_TIMEOUT 20 |
Michael S. Tsirkin | d5bb759 | 2006-09-13 15:01:54 +0300 | [diff] [blame] | 61 | #define CMA_MAX_CM_RETRIES 15 |
Sean Hefty | dcb3f97 | 2007-08-01 14:47:16 -0700 | [diff] [blame] | 62 | #define CMA_CM_MRA_SETTING (IB_CM_MRA_FLAG_DELAY | 24) |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 63 | #define CMA_IBOE_PACKET_LIFETIME 18 |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 64 | |
| 65 | static void cma_add_one(struct ib_device *device); |
| 66 | static void cma_remove_one(struct ib_device *device); |
| 67 | |
| 68 | static struct ib_client cma_client = { |
| 69 | .name = "cma", |
| 70 | .add = cma_add_one, |
| 71 | .remove = cma_remove_one |
| 72 | }; |
| 73 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 74 | static struct ib_sa_client sa_client; |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 75 | static struct rdma_addr_client addr_client; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 76 | static LIST_HEAD(dev_list); |
| 77 | static LIST_HEAD(listen_any_list); |
| 78 | static DEFINE_MUTEX(lock); |
| 79 | static struct workqueue_struct *cma_wq; |
| 80 | static DEFINE_IDR(sdp_ps); |
| 81 | static DEFINE_IDR(tcp_ps); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 82 | static DEFINE_IDR(udp_ps); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 83 | static DEFINE_IDR(ipoib_ps); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 84 | |
| 85 | struct cma_device { |
| 86 | struct list_head list; |
| 87 | struct ib_device *device; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 88 | struct completion comp; |
| 89 | atomic_t refcount; |
| 90 | struct list_head id_list; |
| 91 | }; |
| 92 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 93 | struct rdma_bind_list { |
| 94 | struct idr *ps; |
| 95 | struct hlist_head owners; |
| 96 | unsigned short port; |
| 97 | }; |
| 98 | |
| 99 | /* |
| 100 | * Device removal can occur at anytime, so we need extra handling to |
| 101 | * serialize notifying the user of device removal with other callbacks. |
| 102 | * We do this by disabling removal notification while a callback is in process, |
| 103 | * and reporting it after the callback completes. |
| 104 | */ |
| 105 | struct rdma_id_private { |
| 106 | struct rdma_cm_id id; |
| 107 | |
| 108 | struct rdma_bind_list *bind_list; |
| 109 | struct hlist_node node; |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 110 | struct list_head list; /* listen_any_list or cma_device.list */ |
| 111 | struct list_head listen_list; /* per device listens */ |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 112 | struct cma_device *cma_dev; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 113 | struct list_head mc_list; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 114 | |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 115 | int internal_id; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 116 | enum rdma_cm_state state; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 117 | spinlock_t lock; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 118 | struct mutex qp_mutex; |
| 119 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 120 | struct completion comp; |
| 121 | atomic_t refcount; |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 122 | struct mutex handler_mutex; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 123 | |
| 124 | int backlog; |
| 125 | int timeout_ms; |
| 126 | struct ib_sa_query *query; |
| 127 | int query_id; |
| 128 | union { |
| 129 | struct ib_cm_id *ib; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 130 | struct iw_cm_id *iw; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 131 | } cm_id; |
| 132 | |
| 133 | u32 seq_num; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 134 | u32 qkey; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 135 | u32 qp_num; |
Nir Muchtar | 83e9502 | 2011-01-13 13:56:04 +0000 | [diff] [blame] | 136 | pid_t owner; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 137 | u8 srq; |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 138 | u8 tos; |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 139 | u8 reuseaddr; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 142 | struct cma_multicast { |
| 143 | struct rdma_id_private *id_priv; |
| 144 | union { |
| 145 | struct ib_sa_multicast *ib; |
| 146 | } multicast; |
| 147 | struct list_head list; |
| 148 | void *context; |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 149 | struct sockaddr_storage addr; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 150 | struct kref mcref; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 153 | struct cma_work { |
| 154 | struct work_struct work; |
| 155 | struct rdma_id_private *id; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 156 | enum rdma_cm_state old_state; |
| 157 | enum rdma_cm_state new_state; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 158 | struct rdma_cm_event event; |
| 159 | }; |
| 160 | |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 161 | struct cma_ndev_work { |
| 162 | struct work_struct work; |
| 163 | struct rdma_id_private *id; |
| 164 | struct rdma_cm_event event; |
| 165 | }; |
| 166 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 167 | struct iboe_mcast_work { |
| 168 | struct work_struct work; |
| 169 | struct rdma_id_private *id; |
| 170 | struct cma_multicast *mc; |
| 171 | }; |
| 172 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 173 | union cma_ip_addr { |
| 174 | struct in6_addr ip6; |
| 175 | struct { |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 176 | __be32 pad[3]; |
| 177 | __be32 addr; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 178 | } ip4; |
| 179 | }; |
| 180 | |
| 181 | struct cma_hdr { |
| 182 | u8 cma_version; |
| 183 | u8 ip_version; /* IP version: 7:4 */ |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 184 | __be16 port; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 185 | union cma_ip_addr src_addr; |
| 186 | union cma_ip_addr dst_addr; |
| 187 | }; |
| 188 | |
| 189 | struct sdp_hh { |
| 190 | u8 bsdh[16]; |
| 191 | u8 sdp_version; /* Major version: 7:4 */ |
| 192 | u8 ip_version; /* IP version: 7:4 */ |
| 193 | u8 sdp_specific1[10]; |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 194 | __be16 port; |
| 195 | __be16 sdp_specific2; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 196 | union cma_ip_addr src_addr; |
| 197 | union cma_ip_addr dst_addr; |
| 198 | }; |
| 199 | |
| 200 | struct sdp_hah { |
| 201 | u8 bsdh[16]; |
| 202 | u8 sdp_version; |
| 203 | }; |
| 204 | |
| 205 | #define CMA_VERSION 0x00 |
| 206 | #define SDP_MAJ_VERSION 0x2 |
| 207 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 208 | static int cma_comp(struct rdma_id_private *id_priv, enum rdma_cm_state comp) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 209 | { |
| 210 | unsigned long flags; |
| 211 | int ret; |
| 212 | |
| 213 | spin_lock_irqsave(&id_priv->lock, flags); |
| 214 | ret = (id_priv->state == comp); |
| 215 | spin_unlock_irqrestore(&id_priv->lock, flags); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static int cma_comp_exch(struct rdma_id_private *id_priv, |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 220 | enum rdma_cm_state comp, enum rdma_cm_state exch) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 221 | { |
| 222 | unsigned long flags; |
| 223 | int ret; |
| 224 | |
| 225 | spin_lock_irqsave(&id_priv->lock, flags); |
| 226 | if ((ret = (id_priv->state == comp))) |
| 227 | id_priv->state = exch; |
| 228 | spin_unlock_irqrestore(&id_priv->lock, flags); |
| 229 | return ret; |
| 230 | } |
| 231 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 232 | static enum rdma_cm_state cma_exch(struct rdma_id_private *id_priv, |
| 233 | enum rdma_cm_state exch) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 234 | { |
| 235 | unsigned long flags; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 236 | enum rdma_cm_state old; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 237 | |
| 238 | spin_lock_irqsave(&id_priv->lock, flags); |
| 239 | old = id_priv->state; |
| 240 | id_priv->state = exch; |
| 241 | spin_unlock_irqrestore(&id_priv->lock, flags); |
| 242 | return old; |
| 243 | } |
| 244 | |
| 245 | static inline u8 cma_get_ip_ver(struct cma_hdr *hdr) |
| 246 | { |
| 247 | return hdr->ip_version >> 4; |
| 248 | } |
| 249 | |
| 250 | static inline void cma_set_ip_ver(struct cma_hdr *hdr, u8 ip_ver) |
| 251 | { |
| 252 | hdr->ip_version = (ip_ver << 4) | (hdr->ip_version & 0xF); |
| 253 | } |
| 254 | |
| 255 | static inline u8 sdp_get_majv(u8 sdp_version) |
| 256 | { |
| 257 | return sdp_version >> 4; |
| 258 | } |
| 259 | |
| 260 | static inline u8 sdp_get_ip_ver(struct sdp_hh *hh) |
| 261 | { |
| 262 | return hh->ip_version >> 4; |
| 263 | } |
| 264 | |
| 265 | static inline void sdp_set_ip_ver(struct sdp_hh *hh, u8 ip_ver) |
| 266 | { |
| 267 | hh->ip_version = (ip_ver << 4) | (hh->ip_version & 0xF); |
| 268 | } |
| 269 | |
| 270 | static void cma_attach_to_dev(struct rdma_id_private *id_priv, |
| 271 | struct cma_device *cma_dev) |
| 272 | { |
| 273 | atomic_inc(&cma_dev->refcount); |
| 274 | id_priv->cma_dev = cma_dev; |
| 275 | id_priv->id.device = cma_dev->device; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 276 | id_priv->id.route.addr.dev_addr.transport = |
| 277 | rdma_node_get_transport(cma_dev->device->node_type); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 278 | list_add_tail(&id_priv->list, &cma_dev->id_list); |
| 279 | } |
| 280 | |
| 281 | static inline void cma_deref_dev(struct cma_device *cma_dev) |
| 282 | { |
| 283 | if (atomic_dec_and_test(&cma_dev->refcount)) |
| 284 | complete(&cma_dev->comp); |
| 285 | } |
| 286 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 287 | static inline void release_mc(struct kref *kref) |
| 288 | { |
| 289 | struct cma_multicast *mc = container_of(kref, struct cma_multicast, mcref); |
| 290 | |
| 291 | kfree(mc->multicast.ib); |
| 292 | kfree(mc); |
| 293 | } |
| 294 | |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 295 | static void cma_release_dev(struct rdma_id_private *id_priv) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 296 | { |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 297 | mutex_lock(&lock); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 298 | list_del(&id_priv->list); |
| 299 | cma_deref_dev(id_priv->cma_dev); |
| 300 | id_priv->cma_dev = NULL; |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 301 | mutex_unlock(&lock); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 304 | static int cma_set_qkey(struct rdma_id_private *id_priv) |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 305 | { |
| 306 | struct ib_sa_mcmember_rec rec; |
| 307 | int ret = 0; |
| 308 | |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 309 | if (id_priv->qkey) |
| 310 | return 0; |
| 311 | |
| 312 | switch (id_priv->id.ps) { |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 313 | case RDMA_PS_UDP: |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 314 | id_priv->qkey = RDMA_UDP_QKEY; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 315 | break; |
| 316 | case RDMA_PS_IPOIB: |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 317 | ib_addr_get_mgid(&id_priv->id.route.addr.dev_addr, &rec.mgid); |
| 318 | ret = ib_sa_get_mcmember_rec(id_priv->id.device, |
| 319 | id_priv->id.port_num, &rec.mgid, |
| 320 | &rec); |
| 321 | if (!ret) |
| 322 | id_priv->qkey = be32_to_cpu(rec.qkey); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 323 | break; |
| 324 | default: |
| 325 | break; |
| 326 | } |
| 327 | return ret; |
| 328 | } |
| 329 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 330 | static int find_gid_port(struct ib_device *device, union ib_gid *gid, u8 port_num) |
| 331 | { |
| 332 | int i; |
| 333 | int err; |
| 334 | struct ib_port_attr props; |
| 335 | union ib_gid tmp; |
| 336 | |
| 337 | err = ib_query_port(device, port_num, &props); |
| 338 | if (err) |
| 339 | return 1; |
| 340 | |
| 341 | for (i = 0; i < props.gid_tbl_len; ++i) { |
| 342 | err = ib_query_gid(device, port_num, i, &tmp); |
| 343 | if (err) |
| 344 | return 1; |
| 345 | if (!memcmp(&tmp, gid, sizeof tmp)) |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | return -EAGAIN; |
| 350 | } |
| 351 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 352 | static int cma_acquire_dev(struct rdma_id_private *id_priv) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 353 | { |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 354 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 355 | struct cma_device *cma_dev; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 356 | union ib_gid gid, iboe_gid; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 357 | int ret = -ENODEV; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 358 | u8 port; |
| 359 | enum rdma_link_layer dev_ll = dev_addr->dev_type == ARPHRD_INFINIBAND ? |
| 360 | IB_LINK_LAYER_INFINIBAND : IB_LINK_LAYER_ETHERNET; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 361 | |
Moni Shoua | 2efdd6a | 2011-07-12 11:23:15 +0000 | [diff] [blame] | 362 | if (dev_ll != IB_LINK_LAYER_INFINIBAND && |
| 363 | id_priv->id.ps == RDMA_PS_IPOIB) |
| 364 | return -EINVAL; |
| 365 | |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 366 | mutex_lock(&lock); |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 367 | iboe_addr_get_sgid(dev_addr, &iboe_gid); |
| 368 | memcpy(&gid, dev_addr->src_dev_addr + |
| 369 | rdma_addr_gid_offset(dev_addr), sizeof gid); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 370 | list_for_each_entry(cma_dev, &dev_list, list) { |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 371 | for (port = 1; port <= cma_dev->device->phys_port_cnt; ++port) { |
| 372 | if (rdma_port_get_link_layer(cma_dev->device, port) == dev_ll) { |
| 373 | if (rdma_node_get_transport(cma_dev->device->node_type) == RDMA_TRANSPORT_IB && |
| 374 | rdma_port_get_link_layer(cma_dev->device, port) == IB_LINK_LAYER_ETHERNET) |
| 375 | ret = find_gid_port(cma_dev->device, &iboe_gid, port); |
| 376 | else |
| 377 | ret = find_gid_port(cma_dev->device, &gid, port); |
| 378 | |
| 379 | if (!ret) { |
| 380 | id_priv->id.port_num = port; |
| 381 | goto out; |
| 382 | } else if (ret == 1) |
| 383 | break; |
| 384 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 387 | |
| 388 | out: |
| 389 | if (!ret) |
| 390 | cma_attach_to_dev(id_priv, cma_dev); |
| 391 | |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 392 | mutex_unlock(&lock); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 393 | return ret; |
| 394 | } |
| 395 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 396 | static void cma_deref_id(struct rdma_id_private *id_priv) |
| 397 | { |
| 398 | if (atomic_dec_and_test(&id_priv->refcount)) |
| 399 | complete(&id_priv->comp); |
| 400 | } |
| 401 | |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 402 | static int cma_disable_callback(struct rdma_id_private *id_priv, |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 403 | enum rdma_cm_state state) |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 404 | { |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 405 | mutex_lock(&id_priv->handler_mutex); |
| 406 | if (id_priv->state != state) { |
| 407 | mutex_unlock(&id_priv->handler_mutex); |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | return 0; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | struct rdma_cm_id *rdma_create_id(rdma_cm_event_handler event_handler, |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 414 | void *context, enum rdma_port_space ps, |
| 415 | enum ib_qp_type qp_type) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 416 | { |
| 417 | struct rdma_id_private *id_priv; |
| 418 | |
| 419 | id_priv = kzalloc(sizeof *id_priv, GFP_KERNEL); |
| 420 | if (!id_priv) |
| 421 | return ERR_PTR(-ENOMEM); |
| 422 | |
Nir Muchtar | 83e9502 | 2011-01-13 13:56:04 +0000 | [diff] [blame] | 423 | id_priv->owner = task_pid_nr(current); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 424 | id_priv->state = RDMA_CM_IDLE; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 425 | id_priv->id.context = context; |
| 426 | id_priv->id.event_handler = event_handler; |
| 427 | id_priv->id.ps = ps; |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 428 | id_priv->id.qp_type = qp_type; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 429 | spin_lock_init(&id_priv->lock); |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 430 | mutex_init(&id_priv->qp_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 431 | init_completion(&id_priv->comp); |
| 432 | atomic_set(&id_priv->refcount, 1); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 433 | mutex_init(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 434 | INIT_LIST_HEAD(&id_priv->listen_list); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 435 | INIT_LIST_HEAD(&id_priv->mc_list); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 436 | get_random_bytes(&id_priv->seq_num, sizeof id_priv->seq_num); |
| 437 | |
| 438 | return &id_priv->id; |
| 439 | } |
| 440 | EXPORT_SYMBOL(rdma_create_id); |
| 441 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 442 | static int cma_init_ud_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 443 | { |
| 444 | struct ib_qp_attr qp_attr; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 445 | int qp_attr_mask, ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 446 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 447 | qp_attr.qp_state = IB_QPS_INIT; |
| 448 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 449 | if (ret) |
| 450 | return ret; |
| 451 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 452 | ret = ib_modify_qp(qp, &qp_attr, qp_attr_mask); |
| 453 | if (ret) |
| 454 | return ret; |
| 455 | |
| 456 | qp_attr.qp_state = IB_QPS_RTR; |
| 457 | ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE); |
| 458 | if (ret) |
| 459 | return ret; |
| 460 | |
| 461 | qp_attr.qp_state = IB_QPS_RTS; |
| 462 | qp_attr.sq_psn = 0; |
| 463 | ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN); |
| 464 | |
| 465 | return ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 468 | static int cma_init_conn_qp(struct rdma_id_private *id_priv, struct ib_qp *qp) |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 469 | { |
| 470 | struct ib_qp_attr qp_attr; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 471 | int qp_attr_mask, ret; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 472 | |
| 473 | qp_attr.qp_state = IB_QPS_INIT; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 474 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
| 475 | if (ret) |
| 476 | return ret; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 477 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 478 | return ib_modify_qp(qp, &qp_attr, qp_attr_mask); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 479 | } |
| 480 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 481 | int rdma_create_qp(struct rdma_cm_id *id, struct ib_pd *pd, |
| 482 | struct ib_qp_init_attr *qp_init_attr) |
| 483 | { |
| 484 | struct rdma_id_private *id_priv; |
| 485 | struct ib_qp *qp; |
| 486 | int ret; |
| 487 | |
| 488 | id_priv = container_of(id, struct rdma_id_private, id); |
| 489 | if (id->device != pd->device) |
| 490 | return -EINVAL; |
| 491 | |
| 492 | qp = ib_create_qp(pd, qp_init_attr); |
| 493 | if (IS_ERR(qp)) |
| 494 | return PTR_ERR(qp); |
| 495 | |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 496 | if (id->qp_type == IB_QPT_UD) |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 497 | ret = cma_init_ud_qp(id_priv, qp); |
| 498 | else |
| 499 | ret = cma_init_conn_qp(id_priv, qp); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 500 | if (ret) |
| 501 | goto err; |
| 502 | |
| 503 | id->qp = qp; |
| 504 | id_priv->qp_num = qp->qp_num; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 505 | id_priv->srq = (qp->srq != NULL); |
| 506 | return 0; |
| 507 | err: |
| 508 | ib_destroy_qp(qp); |
| 509 | return ret; |
| 510 | } |
| 511 | EXPORT_SYMBOL(rdma_create_qp); |
| 512 | |
| 513 | void rdma_destroy_qp(struct rdma_cm_id *id) |
| 514 | { |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 515 | struct rdma_id_private *id_priv; |
| 516 | |
| 517 | id_priv = container_of(id, struct rdma_id_private, id); |
| 518 | mutex_lock(&id_priv->qp_mutex); |
| 519 | ib_destroy_qp(id_priv->id.qp); |
| 520 | id_priv->id.qp = NULL; |
| 521 | mutex_unlock(&id_priv->qp_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 522 | } |
| 523 | EXPORT_SYMBOL(rdma_destroy_qp); |
| 524 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 525 | static int cma_modify_qp_rtr(struct rdma_id_private *id_priv, |
| 526 | struct rdma_conn_param *conn_param) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 527 | { |
| 528 | struct ib_qp_attr qp_attr; |
| 529 | int qp_attr_mask, ret; |
| 530 | |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 531 | mutex_lock(&id_priv->qp_mutex); |
| 532 | if (!id_priv->id.qp) { |
| 533 | ret = 0; |
| 534 | goto out; |
| 535 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 536 | |
| 537 | /* Need to update QP attributes from default values. */ |
| 538 | qp_attr.qp_state = IB_QPS_INIT; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 539 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 540 | if (ret) |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 541 | goto out; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 542 | |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 543 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 544 | if (ret) |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 545 | goto out; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 546 | |
| 547 | qp_attr.qp_state = IB_QPS_RTR; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 548 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 549 | if (ret) |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 550 | goto out; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 551 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 552 | if (conn_param) |
| 553 | qp_attr.max_dest_rd_atomic = conn_param->responder_resources; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 554 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
| 555 | out: |
| 556 | mutex_unlock(&id_priv->qp_mutex); |
| 557 | return ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 560 | static int cma_modify_qp_rts(struct rdma_id_private *id_priv, |
| 561 | struct rdma_conn_param *conn_param) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 562 | { |
| 563 | struct ib_qp_attr qp_attr; |
| 564 | int qp_attr_mask, ret; |
| 565 | |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 566 | mutex_lock(&id_priv->qp_mutex); |
| 567 | if (!id_priv->id.qp) { |
| 568 | ret = 0; |
| 569 | goto out; |
| 570 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 571 | |
| 572 | qp_attr.qp_state = IB_QPS_RTS; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 573 | ret = rdma_init_qp_attr(&id_priv->id, &qp_attr, &qp_attr_mask); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 574 | if (ret) |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 575 | goto out; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 576 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 577 | if (conn_param) |
| 578 | qp_attr.max_rd_atomic = conn_param->initiator_depth; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 579 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, qp_attr_mask); |
| 580 | out: |
| 581 | mutex_unlock(&id_priv->qp_mutex); |
| 582 | return ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 585 | static int cma_modify_qp_err(struct rdma_id_private *id_priv) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 586 | { |
| 587 | struct ib_qp_attr qp_attr; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 588 | int ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 589 | |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 590 | mutex_lock(&id_priv->qp_mutex); |
| 591 | if (!id_priv->id.qp) { |
| 592 | ret = 0; |
| 593 | goto out; |
| 594 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 595 | |
| 596 | qp_attr.qp_state = IB_QPS_ERR; |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 597 | ret = ib_modify_qp(id_priv->id.qp, &qp_attr, IB_QP_STATE); |
| 598 | out: |
| 599 | mutex_unlock(&id_priv->qp_mutex); |
| 600 | return ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 603 | static int cma_ib_init_qp_attr(struct rdma_id_private *id_priv, |
| 604 | struct ib_qp_attr *qp_attr, int *qp_attr_mask) |
| 605 | { |
| 606 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
| 607 | int ret; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 608 | u16 pkey; |
| 609 | |
| 610 | if (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num) == |
| 611 | IB_LINK_LAYER_INFINIBAND) |
| 612 | pkey = ib_addr_get_pkey(dev_addr); |
| 613 | else |
| 614 | pkey = 0xffff; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 615 | |
| 616 | ret = ib_find_cached_pkey(id_priv->id.device, id_priv->id.port_num, |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 617 | pkey, &qp_attr->pkey_index); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 618 | if (ret) |
| 619 | return ret; |
| 620 | |
| 621 | qp_attr->port_num = id_priv->id.port_num; |
| 622 | *qp_attr_mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_PORT; |
| 623 | |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 624 | if (id_priv->id.qp_type == IB_QPT_UD) { |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 625 | ret = cma_set_qkey(id_priv); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 629 | qp_attr->qkey = id_priv->qkey; |
| 630 | *qp_attr_mask |= IB_QP_QKEY; |
| 631 | } else { |
| 632 | qp_attr->qp_access_flags = 0; |
| 633 | *qp_attr_mask |= IB_QP_ACCESS_FLAGS; |
| 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 638 | int rdma_init_qp_attr(struct rdma_cm_id *id, struct ib_qp_attr *qp_attr, |
| 639 | int *qp_attr_mask) |
| 640 | { |
| 641 | struct rdma_id_private *id_priv; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 642 | int ret = 0; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 643 | |
| 644 | id_priv = container_of(id, struct rdma_id_private, id); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 645 | switch (rdma_node_get_transport(id_priv->id.device->node_type)) { |
| 646 | case RDMA_TRANSPORT_IB: |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 647 | if (!id_priv->cm_id.ib || (id_priv->id.qp_type == IB_QPT_UD)) |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 648 | ret = cma_ib_init_qp_attr(id_priv, qp_attr, qp_attr_mask); |
| 649 | else |
| 650 | ret = ib_cm_init_qp_attr(id_priv->cm_id.ib, qp_attr, |
| 651 | qp_attr_mask); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 652 | if (qp_attr->qp_state == IB_QPS_RTR) |
| 653 | qp_attr->rq_psn = id_priv->seq_num; |
| 654 | break; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 655 | case RDMA_TRANSPORT_IWARP: |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 656 | if (!id_priv->cm_id.iw) { |
Dotan Barak | 8f07653 | 2007-07-17 17:58:57 +0300 | [diff] [blame] | 657 | qp_attr->qp_access_flags = 0; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 658 | *qp_attr_mask = IB_QP_STATE | IB_QP_ACCESS_FLAGS; |
| 659 | } else |
| 660 | ret = iw_cm_init_qp_attr(id_priv->cm_id.iw, qp_attr, |
| 661 | qp_attr_mask); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 662 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 663 | default: |
| 664 | ret = -ENOSYS; |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | return ret; |
| 669 | } |
| 670 | EXPORT_SYMBOL(rdma_init_qp_attr); |
| 671 | |
| 672 | static inline int cma_zero_addr(struct sockaddr *addr) |
| 673 | { |
| 674 | struct in6_addr *ip6; |
| 675 | |
| 676 | if (addr->sa_family == AF_INET) |
Joe Perches | 6360a02 | 2007-12-16 13:47:33 -0800 | [diff] [blame] | 677 | return ipv4_is_zeronet( |
| 678 | ((struct sockaddr_in *)addr)->sin_addr.s_addr); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 679 | else { |
| 680 | ip6 = &((struct sockaddr_in6 *) addr)->sin6_addr; |
| 681 | return (ip6->s6_addr32[0] | ip6->s6_addr32[1] | |
Eric Sesterhenn | 5fd571c | 2006-06-21 20:56:26 +0200 | [diff] [blame] | 682 | ip6->s6_addr32[2] | ip6->s6_addr32[3]) == 0; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
| 686 | static inline int cma_loopback_addr(struct sockaddr *addr) |
| 687 | { |
Aleksey Senin | 1f5175a | 2008-12-24 10:16:45 -0800 | [diff] [blame] | 688 | if (addr->sa_family == AF_INET) |
| 689 | return ipv4_is_loopback( |
| 690 | ((struct sockaddr_in *) addr)->sin_addr.s_addr); |
| 691 | else |
| 692 | return ipv6_addr_loopback( |
| 693 | &((struct sockaddr_in6 *) addr)->sin6_addr); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static inline int cma_any_addr(struct sockaddr *addr) |
| 697 | { |
| 698 | return cma_zero_addr(addr) || cma_loopback_addr(addr); |
| 699 | } |
| 700 | |
Hefty, Sean | 43b752d | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 701 | static int cma_addr_cmp(struct sockaddr *src, struct sockaddr *dst) |
| 702 | { |
| 703 | if (src->sa_family != dst->sa_family) |
| 704 | return -1; |
| 705 | |
| 706 | switch (src->sa_family) { |
| 707 | case AF_INET: |
| 708 | return ((struct sockaddr_in *) src)->sin_addr.s_addr != |
| 709 | ((struct sockaddr_in *) dst)->sin_addr.s_addr; |
| 710 | default: |
| 711 | return ipv6_addr_cmp(&((struct sockaddr_in6 *) src)->sin6_addr, |
| 712 | &((struct sockaddr_in6 *) dst)->sin6_addr); |
| 713 | } |
| 714 | } |
| 715 | |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 716 | static inline __be16 cma_port(struct sockaddr *addr) |
| 717 | { |
| 718 | if (addr->sa_family == AF_INET) |
| 719 | return ((struct sockaddr_in *) addr)->sin_port; |
| 720 | else |
| 721 | return ((struct sockaddr_in6 *) addr)->sin6_port; |
| 722 | } |
| 723 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 724 | static inline int cma_any_port(struct sockaddr *addr) |
| 725 | { |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 726 | return !cma_port(addr); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static int cma_get_net_info(void *hdr, enum rdma_port_space ps, |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 730 | u8 *ip_ver, __be16 *port, |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 731 | union cma_ip_addr **src, union cma_ip_addr **dst) |
| 732 | { |
| 733 | switch (ps) { |
| 734 | case RDMA_PS_SDP: |
| 735 | if (sdp_get_majv(((struct sdp_hh *) hdr)->sdp_version) != |
| 736 | SDP_MAJ_VERSION) |
| 737 | return -EINVAL; |
| 738 | |
| 739 | *ip_ver = sdp_get_ip_ver(hdr); |
| 740 | *port = ((struct sdp_hh *) hdr)->port; |
| 741 | *src = &((struct sdp_hh *) hdr)->src_addr; |
| 742 | *dst = &((struct sdp_hh *) hdr)->dst_addr; |
| 743 | break; |
| 744 | default: |
| 745 | if (((struct cma_hdr *) hdr)->cma_version != CMA_VERSION) |
| 746 | return -EINVAL; |
| 747 | |
| 748 | *ip_ver = cma_get_ip_ver(hdr); |
| 749 | *port = ((struct cma_hdr *) hdr)->port; |
| 750 | *src = &((struct cma_hdr *) hdr)->src_addr; |
| 751 | *dst = &((struct cma_hdr *) hdr)->dst_addr; |
| 752 | break; |
| 753 | } |
| 754 | |
| 755 | if (*ip_ver != 4 && *ip_ver != 6) |
| 756 | return -EINVAL; |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static void cma_save_net_info(struct rdma_addr *addr, |
| 761 | struct rdma_addr *listen_addr, |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 762 | u8 ip_ver, __be16 port, |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 763 | union cma_ip_addr *src, union cma_ip_addr *dst) |
| 764 | { |
| 765 | struct sockaddr_in *listen4, *ip4; |
| 766 | struct sockaddr_in6 *listen6, *ip6; |
| 767 | |
| 768 | switch (ip_ver) { |
| 769 | case 4: |
| 770 | listen4 = (struct sockaddr_in *) &listen_addr->src_addr; |
| 771 | ip4 = (struct sockaddr_in *) &addr->src_addr; |
| 772 | ip4->sin_family = listen4->sin_family; |
| 773 | ip4->sin_addr.s_addr = dst->ip4.addr; |
| 774 | ip4->sin_port = listen4->sin_port; |
| 775 | |
| 776 | ip4 = (struct sockaddr_in *) &addr->dst_addr; |
| 777 | ip4->sin_family = listen4->sin_family; |
| 778 | ip4->sin_addr.s_addr = src->ip4.addr; |
| 779 | ip4->sin_port = port; |
| 780 | break; |
| 781 | case 6: |
| 782 | listen6 = (struct sockaddr_in6 *) &listen_addr->src_addr; |
| 783 | ip6 = (struct sockaddr_in6 *) &addr->src_addr; |
| 784 | ip6->sin6_family = listen6->sin6_family; |
| 785 | ip6->sin6_addr = dst->ip6; |
| 786 | ip6->sin6_port = listen6->sin6_port; |
| 787 | |
| 788 | ip6 = (struct sockaddr_in6 *) &addr->dst_addr; |
| 789 | ip6->sin6_family = listen6->sin6_family; |
| 790 | ip6->sin6_addr = src->ip6; |
| 791 | ip6->sin6_port = port; |
| 792 | break; |
| 793 | default: |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | static inline int cma_user_data_offset(enum rdma_port_space ps) |
| 799 | { |
| 800 | switch (ps) { |
| 801 | case RDMA_PS_SDP: |
| 802 | return 0; |
| 803 | default: |
| 804 | return sizeof(struct cma_hdr); |
| 805 | } |
| 806 | } |
| 807 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 808 | static void cma_cancel_route(struct rdma_id_private *id_priv) |
| 809 | { |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 810 | switch (rdma_port_get_link_layer(id_priv->id.device, id_priv->id.port_num)) { |
| 811 | case IB_LINK_LAYER_INFINIBAND: |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 812 | if (id_priv->query) |
| 813 | ib_sa_cancel_query(id_priv->query_id, id_priv->query); |
| 814 | break; |
| 815 | default: |
| 816 | break; |
| 817 | } |
| 818 | } |
| 819 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 820 | static void cma_cancel_listens(struct rdma_id_private *id_priv) |
| 821 | { |
| 822 | struct rdma_id_private *dev_id_priv; |
| 823 | |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 824 | /* |
| 825 | * Remove from listen_any_list to prevent added devices from spawning |
| 826 | * additional listen requests. |
| 827 | */ |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 828 | mutex_lock(&lock); |
| 829 | list_del(&id_priv->list); |
| 830 | |
| 831 | while (!list_empty(&id_priv->listen_list)) { |
| 832 | dev_id_priv = list_entry(id_priv->listen_list.next, |
| 833 | struct rdma_id_private, listen_list); |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 834 | /* sync with device removal to avoid duplicate destruction */ |
| 835 | list_del_init(&dev_id_priv->list); |
| 836 | list_del(&dev_id_priv->listen_list); |
| 837 | mutex_unlock(&lock); |
| 838 | |
| 839 | rdma_destroy_id(&dev_id_priv->id); |
| 840 | mutex_lock(&lock); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 841 | } |
| 842 | mutex_unlock(&lock); |
| 843 | } |
| 844 | |
| 845 | static void cma_cancel_operation(struct rdma_id_private *id_priv, |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 846 | enum rdma_cm_state state) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 847 | { |
| 848 | switch (state) { |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 849 | case RDMA_CM_ADDR_QUERY: |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 850 | rdma_addr_cancel(&id_priv->id.route.addr.dev_addr); |
| 851 | break; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 852 | case RDMA_CM_ROUTE_QUERY: |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 853 | cma_cancel_route(id_priv); |
| 854 | break; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 855 | case RDMA_CM_LISTEN: |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 856 | if (cma_any_addr((struct sockaddr *) &id_priv->id.route.addr.src_addr) |
| 857 | && !id_priv->cma_dev) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 858 | cma_cancel_listens(id_priv); |
| 859 | break; |
| 860 | default: |
| 861 | break; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | static void cma_release_port(struct rdma_id_private *id_priv) |
| 866 | { |
| 867 | struct rdma_bind_list *bind_list = id_priv->bind_list; |
| 868 | |
| 869 | if (!bind_list) |
| 870 | return; |
| 871 | |
| 872 | mutex_lock(&lock); |
| 873 | hlist_del(&id_priv->node); |
| 874 | if (hlist_empty(&bind_list->owners)) { |
| 875 | idr_remove(bind_list->ps, bind_list->port); |
| 876 | kfree(bind_list); |
| 877 | } |
| 878 | mutex_unlock(&lock); |
| 879 | } |
| 880 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 881 | static void cma_leave_mc_groups(struct rdma_id_private *id_priv) |
| 882 | { |
| 883 | struct cma_multicast *mc; |
| 884 | |
| 885 | while (!list_empty(&id_priv->mc_list)) { |
| 886 | mc = container_of(id_priv->mc_list.next, |
| 887 | struct cma_multicast, list); |
| 888 | list_del(&mc->list); |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 889 | switch (rdma_port_get_link_layer(id_priv->cma_dev->device, id_priv->id.port_num)) { |
| 890 | case IB_LINK_LAYER_INFINIBAND: |
| 891 | ib_sa_free_multicast(mc->multicast.ib); |
| 892 | kfree(mc); |
| 893 | break; |
| 894 | case IB_LINK_LAYER_ETHERNET: |
| 895 | kref_put(&mc->mcref, release_mc); |
| 896 | break; |
| 897 | default: |
| 898 | break; |
| 899 | } |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 903 | void rdma_destroy_id(struct rdma_cm_id *id) |
| 904 | { |
| 905 | struct rdma_id_private *id_priv; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 906 | enum rdma_cm_state state; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 907 | |
| 908 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 909 | state = cma_exch(id_priv, RDMA_CM_DESTROYING); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 910 | cma_cancel_operation(id_priv, state); |
| 911 | |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 912 | /* |
| 913 | * Wait for any active callback to finish. New callbacks will find |
| 914 | * the id_priv state set to destroying and abort. |
| 915 | */ |
| 916 | mutex_lock(&id_priv->handler_mutex); |
| 917 | mutex_unlock(&id_priv->handler_mutex); |
| 918 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 919 | if (id_priv->cma_dev) { |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 920 | switch (rdma_node_get_transport(id_priv->id.device->node_type)) { |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 921 | case RDMA_TRANSPORT_IB: |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 922 | if (id_priv->cm_id.ib) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 923 | ib_destroy_cm_id(id_priv->cm_id.ib); |
| 924 | break; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 925 | case RDMA_TRANSPORT_IWARP: |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 926 | if (id_priv->cm_id.iw) |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 927 | iw_destroy_cm_id(id_priv->cm_id.iw); |
| 928 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 929 | default: |
| 930 | break; |
| 931 | } |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 932 | cma_leave_mc_groups(id_priv); |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 933 | cma_release_dev(id_priv); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | cma_release_port(id_priv); |
| 937 | cma_deref_id(id_priv); |
| 938 | wait_for_completion(&id_priv->comp); |
| 939 | |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 940 | if (id_priv->internal_id) |
| 941 | cma_deref_id(id_priv->id.context); |
| 942 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 943 | kfree(id_priv->id.route.path_rec); |
| 944 | kfree(id_priv); |
| 945 | } |
| 946 | EXPORT_SYMBOL(rdma_destroy_id); |
| 947 | |
| 948 | static int cma_rep_recv(struct rdma_id_private *id_priv) |
| 949 | { |
| 950 | int ret; |
| 951 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 952 | ret = cma_modify_qp_rtr(id_priv, NULL); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 953 | if (ret) |
| 954 | goto reject; |
| 955 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 956 | ret = cma_modify_qp_rts(id_priv, NULL); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 957 | if (ret) |
| 958 | goto reject; |
| 959 | |
| 960 | ret = ib_send_cm_rtu(id_priv->cm_id.ib, NULL, 0); |
| 961 | if (ret) |
| 962 | goto reject; |
| 963 | |
| 964 | return 0; |
| 965 | reject: |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 966 | cma_modify_qp_err(id_priv); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 967 | ib_send_cm_rej(id_priv->cm_id.ib, IB_CM_REJ_CONSUMER_DEFINED, |
| 968 | NULL, 0, NULL, 0); |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | static int cma_verify_rep(struct rdma_id_private *id_priv, void *data) |
| 973 | { |
| 974 | if (id_priv->id.ps == RDMA_PS_SDP && |
| 975 | sdp_get_majv(((struct sdp_hah *) data)->sdp_version) != |
| 976 | SDP_MAJ_VERSION) |
| 977 | return -EINVAL; |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 982 | static void cma_set_rep_event_data(struct rdma_cm_event *event, |
| 983 | struct ib_cm_rep_event_param *rep_data, |
| 984 | void *private_data) |
| 985 | { |
| 986 | event->param.conn.private_data = private_data; |
| 987 | event->param.conn.private_data_len = IB_CM_REP_PRIVATE_DATA_SIZE; |
| 988 | event->param.conn.responder_resources = rep_data->responder_resources; |
| 989 | event->param.conn.initiator_depth = rep_data->initiator_depth; |
| 990 | event->param.conn.flow_control = rep_data->flow_control; |
| 991 | event->param.conn.rnr_retry_count = rep_data->rnr_retry_count; |
| 992 | event->param.conn.srq = rep_data->srq; |
| 993 | event->param.conn.qp_num = rep_data->remote_qpn; |
| 994 | } |
| 995 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 996 | static int cma_ib_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) |
| 997 | { |
| 998 | struct rdma_id_private *id_priv = cm_id->context; |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 999 | struct rdma_cm_event event; |
| 1000 | int ret = 0; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1001 | |
Amir Vadai | 38ca83a | 2008-07-22 14:14:23 -0700 | [diff] [blame] | 1002 | if ((ib_event->event != IB_CM_TIMEWAIT_EXIT && |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1003 | cma_disable_callback(id_priv, RDMA_CM_CONNECT)) || |
Amir Vadai | 38ca83a | 2008-07-22 14:14:23 -0700 | [diff] [blame] | 1004 | (ib_event->event == IB_CM_TIMEWAIT_EXIT && |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1005 | cma_disable_callback(id_priv, RDMA_CM_DISCONNECT))) |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 1006 | return 0; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1007 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1008 | memset(&event, 0, sizeof event); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1009 | switch (ib_event->event) { |
| 1010 | case IB_CM_REQ_ERROR: |
| 1011 | case IB_CM_REP_ERROR: |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1012 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
| 1013 | event.status = -ETIMEDOUT; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1014 | break; |
| 1015 | case IB_CM_REP_RECEIVED: |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1016 | event.status = cma_verify_rep(id_priv, ib_event->private_data); |
| 1017 | if (event.status) |
| 1018 | event.event = RDMA_CM_EVENT_CONNECT_ERROR; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1019 | else if (id_priv->id.qp && id_priv->id.ps != RDMA_PS_SDP) { |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1020 | event.status = cma_rep_recv(id_priv); |
| 1021 | event.event = event.status ? RDMA_CM_EVENT_CONNECT_ERROR : |
| 1022 | RDMA_CM_EVENT_ESTABLISHED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1023 | } else |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1024 | event.event = RDMA_CM_EVENT_CONNECT_RESPONSE; |
| 1025 | cma_set_rep_event_data(&event, &ib_event->param.rep_rcvd, |
| 1026 | ib_event->private_data); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1027 | break; |
| 1028 | case IB_CM_RTU_RECEIVED: |
Sean Hefty | 0fe313b | 2006-11-30 16:37:15 -0800 | [diff] [blame] | 1029 | case IB_CM_USER_ESTABLISHED: |
| 1030 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1031 | break; |
| 1032 | case IB_CM_DREQ_ERROR: |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1033 | event.status = -ETIMEDOUT; /* fall through */ |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1034 | case IB_CM_DREQ_RECEIVED: |
| 1035 | case IB_CM_DREP_RECEIVED: |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1036 | if (!cma_comp_exch(id_priv, RDMA_CM_CONNECT, |
| 1037 | RDMA_CM_DISCONNECT)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1038 | goto out; |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1039 | event.event = RDMA_CM_EVENT_DISCONNECTED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1040 | break; |
| 1041 | case IB_CM_TIMEWAIT_EXIT: |
Amir Vadai | 38ca83a | 2008-07-22 14:14:23 -0700 | [diff] [blame] | 1042 | event.event = RDMA_CM_EVENT_TIMEWAIT_EXIT; |
| 1043 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1044 | case IB_CM_MRA_RECEIVED: |
| 1045 | /* ignore event */ |
| 1046 | goto out; |
| 1047 | case IB_CM_REJ_RECEIVED: |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 1048 | cma_modify_qp_err(id_priv); |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1049 | event.status = ib_event->param.rej_rcvd.reason; |
| 1050 | event.event = RDMA_CM_EVENT_REJECTED; |
| 1051 | event.param.conn.private_data = ib_event->private_data; |
| 1052 | event.param.conn.private_data_len = IB_CM_REJ_PRIVATE_DATA_SIZE; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1053 | break; |
| 1054 | default: |
Roland Dreier | 468f223 | 2008-07-14 23:48:47 -0700 | [diff] [blame] | 1055 | printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n", |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1056 | ib_event->event); |
| 1057 | goto out; |
| 1058 | } |
| 1059 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1060 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1061 | if (ret) { |
| 1062 | /* Destroy the CM ID by returning a non-zero value. */ |
| 1063 | id_priv->cm_id.ib = NULL; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1064 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1065 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1066 | rdma_destroy_id(&id_priv->id); |
| 1067 | return ret; |
| 1068 | } |
| 1069 | out: |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1070 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1071 | return ret; |
| 1072 | } |
| 1073 | |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1074 | static struct rdma_id_private *cma_new_conn_id(struct rdma_cm_id *listen_id, |
| 1075 | struct ib_cm_event *ib_event) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1076 | { |
| 1077 | struct rdma_id_private *id_priv; |
| 1078 | struct rdma_cm_id *id; |
| 1079 | struct rdma_route *rt; |
| 1080 | union cma_ip_addr *src, *dst; |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 1081 | __be16 port; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1082 | u8 ip_ver; |
Or Gerlitz | 64c5e61 | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1083 | int ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1084 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1085 | if (cma_get_net_info(ib_event->private_data, listen_id->ps, |
| 1086 | &ip_ver, &port, &src, &dst)) |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1087 | return NULL; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1088 | |
Krishna Kumar | 3f168d2 | 2006-09-29 12:09:51 -0700 | [diff] [blame] | 1089 | id = rdma_create_id(listen_id->event_handler, listen_id->context, |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 1090 | listen_id->ps, ib_event->param.req_rcvd.qp_type); |
Krishna Kumar | 3f168d2 | 2006-09-29 12:09:51 -0700 | [diff] [blame] | 1091 | if (IS_ERR(id)) |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1092 | return NULL; |
Krishna Kumar | 3f168d2 | 2006-09-29 12:09:51 -0700 | [diff] [blame] | 1093 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1094 | cma_save_net_info(&id->route.addr, &listen_id->route.addr, |
| 1095 | ip_ver, port, src, dst); |
Krishna Kumar | 3f168d2 | 2006-09-29 12:09:51 -0700 | [diff] [blame] | 1096 | |
| 1097 | rt = &id->route; |
| 1098 | rt->num_paths = ib_event->param.req_rcvd.alternate_path ? 2 : 1; |
| 1099 | rt->path_rec = kmalloc(sizeof *rt->path_rec * rt->num_paths, |
| 1100 | GFP_KERNEL); |
| 1101 | if (!rt->path_rec) |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1102 | goto err; |
Krishna Kumar | 3f168d2 | 2006-09-29 12:09:51 -0700 | [diff] [blame] | 1103 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1104 | rt->path_rec[0] = *ib_event->param.req_rcvd.primary_path; |
| 1105 | if (rt->num_paths == 2) |
| 1106 | rt->path_rec[1] = *ib_event->param.req_rcvd.alternate_path; |
| 1107 | |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1108 | if (cma_any_addr((struct sockaddr *) &rt->addr.src_addr)) { |
| 1109 | rt->addr.dev_addr.dev_type = ARPHRD_INFINIBAND; |
| 1110 | rdma_addr_set_sgid(&rt->addr.dev_addr, &rt->path_rec[0].sgid); |
| 1111 | ib_addr_set_pkey(&rt->addr.dev_addr, rt->path_rec[0].pkey); |
| 1112 | } else { |
| 1113 | ret = rdma_translate_ip((struct sockaddr *) &rt->addr.src_addr, |
| 1114 | &rt->addr.dev_addr); |
| 1115 | if (ret) |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1116 | goto err; |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1117 | } |
| 1118 | rdma_addr_set_dgid(&rt->addr.dev_addr, &rt->path_rec[0].dgid); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1119 | |
| 1120 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1121 | id_priv->state = RDMA_CM_CONNECT; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1122 | return id_priv; |
Krishna Kumar | 3f168d2 | 2006-09-29 12:09:51 -0700 | [diff] [blame] | 1123 | |
Krishna Kumar | 3f168d2 | 2006-09-29 12:09:51 -0700 | [diff] [blame] | 1124 | err: |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1125 | rdma_destroy_id(id); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1126 | return NULL; |
| 1127 | } |
| 1128 | |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1129 | static struct rdma_id_private *cma_new_udp_id(struct rdma_cm_id *listen_id, |
| 1130 | struct ib_cm_event *ib_event) |
| 1131 | { |
| 1132 | struct rdma_id_private *id_priv; |
| 1133 | struct rdma_cm_id *id; |
| 1134 | union cma_ip_addr *src, *dst; |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 1135 | __be16 port; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1136 | u8 ip_ver; |
| 1137 | int ret; |
| 1138 | |
| 1139 | id = rdma_create_id(listen_id->event_handler, listen_id->context, |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 1140 | listen_id->ps, IB_QPT_UD); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1141 | if (IS_ERR(id)) |
| 1142 | return NULL; |
| 1143 | |
| 1144 | |
| 1145 | if (cma_get_net_info(ib_event->private_data, listen_id->ps, |
| 1146 | &ip_ver, &port, &src, &dst)) |
| 1147 | goto err; |
| 1148 | |
| 1149 | cma_save_net_info(&id->route.addr, &listen_id->route.addr, |
| 1150 | ip_ver, port, src, dst); |
| 1151 | |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1152 | if (!cma_any_addr((struct sockaddr *) &id->route.addr.src_addr)) { |
| 1153 | ret = rdma_translate_ip((struct sockaddr *) &id->route.addr.src_addr, |
| 1154 | &id->route.addr.dev_addr); |
| 1155 | if (ret) |
| 1156 | goto err; |
| 1157 | } |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1158 | |
| 1159 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1160 | id_priv->state = RDMA_CM_CONNECT; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1161 | return id_priv; |
| 1162 | err: |
| 1163 | rdma_destroy_id(id); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1167 | static void cma_set_req_event_data(struct rdma_cm_event *event, |
| 1168 | struct ib_cm_req_event_param *req_data, |
| 1169 | void *private_data, int offset) |
| 1170 | { |
| 1171 | event->param.conn.private_data = private_data + offset; |
| 1172 | event->param.conn.private_data_len = IB_CM_REQ_PRIVATE_DATA_SIZE - offset; |
| 1173 | event->param.conn.responder_resources = req_data->responder_resources; |
| 1174 | event->param.conn.initiator_depth = req_data->initiator_depth; |
| 1175 | event->param.conn.flow_control = req_data->flow_control; |
| 1176 | event->param.conn.retry_count = req_data->retry_count; |
| 1177 | event->param.conn.rnr_retry_count = req_data->rnr_retry_count; |
| 1178 | event->param.conn.srq = req_data->srq; |
| 1179 | event->param.conn.qp_num = req_data->remote_qpn; |
| 1180 | } |
| 1181 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1182 | static int cma_req_handler(struct ib_cm_id *cm_id, struct ib_cm_event *ib_event) |
| 1183 | { |
| 1184 | struct rdma_id_private *listen_id, *conn_id; |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1185 | struct rdma_cm_event event; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1186 | int offset, ret; |
| 1187 | |
| 1188 | listen_id = cm_id->context; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1189 | if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 1190 | return -ECONNABORTED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1191 | |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1192 | memset(&event, 0, sizeof event); |
| 1193 | offset = cma_user_data_offset(listen_id->id.ps); |
| 1194 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 1195 | if (listen_id->id.qp_type == IB_QPT_UD) { |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1196 | conn_id = cma_new_udp_id(&listen_id->id, ib_event); |
| 1197 | event.param.ud.private_data = ib_event->private_data + offset; |
| 1198 | event.param.ud.private_data_len = |
| 1199 | IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE - offset; |
| 1200 | } else { |
| 1201 | conn_id = cma_new_conn_id(&listen_id->id, ib_event); |
| 1202 | cma_set_req_event_data(&event, &ib_event->param.req_rcvd, |
| 1203 | ib_event->private_data, offset); |
| 1204 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1205 | if (!conn_id) { |
| 1206 | ret = -ENOMEM; |
| 1207 | goto out; |
| 1208 | } |
| 1209 | |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1210 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1211 | ret = cma_acquire_dev(conn_id); |
Krishna Kumar | a1a733f | 2006-10-17 10:09:11 +0530 | [diff] [blame] | 1212 | if (ret) |
| 1213 | goto release_conn_id; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1214 | |
| 1215 | conn_id->cm_id.ib = cm_id; |
| 1216 | cm_id->context = conn_id; |
| 1217 | cm_id->cm_handler = cma_ib_handler; |
| 1218 | |
Sean Hefty | 25ae21a | 2011-02-23 08:11:32 -0800 | [diff] [blame] | 1219 | /* |
| 1220 | * Protect against the user destroying conn_id from another thread |
| 1221 | * until we're done accessing it. |
| 1222 | */ |
| 1223 | atomic_inc(&conn_id->refcount); |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1224 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
Vladimir Sokolovsky | 45d9478 | 2007-12-07 20:32:03 -0800 | [diff] [blame] | 1225 | if (!ret) { |
Sean Hefty | ead595a | 2008-02-13 14:33:53 -0800 | [diff] [blame] | 1226 | /* |
| 1227 | * Acquire mutex to prevent user executing rdma_destroy_id() |
| 1228 | * while we're accessing the cm_id. |
| 1229 | */ |
| 1230 | mutex_lock(&lock); |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 1231 | if (cma_comp(conn_id, RDMA_CM_CONNECT) && (conn_id->id.qp_type != IB_QPT_UD)) |
Sean Hefty | ead595a | 2008-02-13 14:33:53 -0800 | [diff] [blame] | 1232 | ib_send_cm_mra(cm_id, CMA_CM_MRA_SETTING, NULL, 0); |
| 1233 | mutex_unlock(&lock); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1234 | mutex_unlock(&conn_id->handler_mutex); |
Sean Hefty | 25ae21a | 2011-02-23 08:11:32 -0800 | [diff] [blame] | 1235 | cma_deref_id(conn_id); |
Krishna Kumar | a1a733f | 2006-10-17 10:09:11 +0530 | [diff] [blame] | 1236 | goto out; |
Vladimir Sokolovsky | 45d9478 | 2007-12-07 20:32:03 -0800 | [diff] [blame] | 1237 | } |
Sean Hefty | 25ae21a | 2011-02-23 08:11:32 -0800 | [diff] [blame] | 1238 | cma_deref_id(conn_id); |
Krishna Kumar | a1a733f | 2006-10-17 10:09:11 +0530 | [diff] [blame] | 1239 | |
| 1240 | /* Destroy the CM ID by returning a non-zero value. */ |
| 1241 | conn_id->cm_id.ib = NULL; |
| 1242 | |
| 1243 | release_conn_id: |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1244 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1245 | mutex_unlock(&conn_id->handler_mutex); |
Krishna Kumar | a1a733f | 2006-10-17 10:09:11 +0530 | [diff] [blame] | 1246 | rdma_destroy_id(&conn_id->id); |
| 1247 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1248 | out: |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1249 | mutex_unlock(&listen_id->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1250 | return ret; |
| 1251 | } |
| 1252 | |
| 1253 | static __be64 cma_get_service_id(enum rdma_port_space ps, struct sockaddr *addr) |
| 1254 | { |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 1255 | return cpu_to_be64(((u64)ps << 16) + be16_to_cpu(cma_port(addr))); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | static void cma_set_compare_data(enum rdma_port_space ps, struct sockaddr *addr, |
| 1259 | struct ib_cm_compare_data *compare) |
| 1260 | { |
| 1261 | struct cma_hdr *cma_data, *cma_mask; |
| 1262 | struct sdp_hh *sdp_data, *sdp_mask; |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 1263 | __be32 ip4_addr; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1264 | struct in6_addr ip6_addr; |
| 1265 | |
| 1266 | memset(compare, 0, sizeof *compare); |
| 1267 | cma_data = (void *) compare->data; |
| 1268 | cma_mask = (void *) compare->mask; |
| 1269 | sdp_data = (void *) compare->data; |
| 1270 | sdp_mask = (void *) compare->mask; |
| 1271 | |
| 1272 | switch (addr->sa_family) { |
| 1273 | case AF_INET: |
| 1274 | ip4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr; |
| 1275 | if (ps == RDMA_PS_SDP) { |
| 1276 | sdp_set_ip_ver(sdp_data, 4); |
| 1277 | sdp_set_ip_ver(sdp_mask, 0xF); |
| 1278 | sdp_data->dst_addr.ip4.addr = ip4_addr; |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 1279 | sdp_mask->dst_addr.ip4.addr = htonl(~0); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1280 | } else { |
| 1281 | cma_set_ip_ver(cma_data, 4); |
| 1282 | cma_set_ip_ver(cma_mask, 0xF); |
| 1283 | cma_data->dst_addr.ip4.addr = ip4_addr; |
Al Viro | 1b90c13 | 2008-03-29 03:10:28 +0000 | [diff] [blame] | 1284 | cma_mask->dst_addr.ip4.addr = htonl(~0); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1285 | } |
| 1286 | break; |
| 1287 | case AF_INET6: |
| 1288 | ip6_addr = ((struct sockaddr_in6 *) addr)->sin6_addr; |
| 1289 | if (ps == RDMA_PS_SDP) { |
| 1290 | sdp_set_ip_ver(sdp_data, 6); |
| 1291 | sdp_set_ip_ver(sdp_mask, 0xF); |
| 1292 | sdp_data->dst_addr.ip6 = ip6_addr; |
| 1293 | memset(&sdp_mask->dst_addr.ip6, 0xFF, |
| 1294 | sizeof sdp_mask->dst_addr.ip6); |
| 1295 | } else { |
| 1296 | cma_set_ip_ver(cma_data, 6); |
| 1297 | cma_set_ip_ver(cma_mask, 0xF); |
| 1298 | cma_data->dst_addr.ip6 = ip6_addr; |
| 1299 | memset(&cma_mask->dst_addr.ip6, 0xFF, |
| 1300 | sizeof cma_mask->dst_addr.ip6); |
| 1301 | } |
| 1302 | break; |
| 1303 | default: |
| 1304 | break; |
| 1305 | } |
| 1306 | } |
| 1307 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1308 | static int cma_iw_handler(struct iw_cm_id *iw_id, struct iw_cm_event *iw_event) |
| 1309 | { |
| 1310 | struct rdma_id_private *id_priv = iw_id->context; |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1311 | struct rdma_cm_event event; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1312 | struct sockaddr_in *sin; |
| 1313 | int ret = 0; |
| 1314 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1315 | if (cma_disable_callback(id_priv, RDMA_CM_CONNECT)) |
Sean Hefty | be65f08 | 2007-05-07 11:49:12 -0700 | [diff] [blame] | 1316 | return 0; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1317 | |
Sean Hefty | be65f08 | 2007-05-07 11:49:12 -0700 | [diff] [blame] | 1318 | memset(&event, 0, sizeof event); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1319 | switch (iw_event->event) { |
| 1320 | case IW_CM_EVENT_CLOSE: |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1321 | event.event = RDMA_CM_EVENT_DISCONNECTED; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1322 | break; |
| 1323 | case IW_CM_EVENT_CONNECT_REPLY: |
| 1324 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; |
| 1325 | *sin = iw_event->local_addr; |
| 1326 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.dst_addr; |
| 1327 | *sin = iw_event->remote_addr; |
Steve Wise | 881a045 | 2006-12-15 16:50:17 -0600 | [diff] [blame] | 1328 | switch (iw_event->status) { |
| 1329 | case 0: |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1330 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
Steve Wise | 881a045 | 2006-12-15 16:50:17 -0600 | [diff] [blame] | 1331 | break; |
| 1332 | case -ECONNRESET: |
| 1333 | case -ECONNREFUSED: |
| 1334 | event.event = RDMA_CM_EVENT_REJECTED; |
| 1335 | break; |
| 1336 | case -ETIMEDOUT: |
| 1337 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
| 1338 | break; |
| 1339 | default: |
| 1340 | event.event = RDMA_CM_EVENT_CONNECT_ERROR; |
| 1341 | break; |
| 1342 | } |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1343 | break; |
| 1344 | case IW_CM_EVENT_ESTABLISHED: |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1345 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1346 | break; |
| 1347 | default: |
| 1348 | BUG_ON(1); |
| 1349 | } |
| 1350 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1351 | event.status = iw_event->status; |
| 1352 | event.param.conn.private_data = iw_event->private_data; |
| 1353 | event.param.conn.private_data_len = iw_event->private_data_len; |
| 1354 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1355 | if (ret) { |
| 1356 | /* Destroy the CM ID by returning a non-zero value. */ |
| 1357 | id_priv->cm_id.iw = NULL; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1358 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1359 | mutex_unlock(&id_priv->handler_mutex); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1360 | rdma_destroy_id(&id_priv->id); |
| 1361 | return ret; |
| 1362 | } |
| 1363 | |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1364 | mutex_unlock(&id_priv->handler_mutex); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1365 | return ret; |
| 1366 | } |
| 1367 | |
| 1368 | static int iw_conn_req_handler(struct iw_cm_id *cm_id, |
| 1369 | struct iw_cm_event *iw_event) |
| 1370 | { |
| 1371 | struct rdma_cm_id *new_cm_id; |
| 1372 | struct rdma_id_private *listen_id, *conn_id; |
| 1373 | struct sockaddr_in *sin; |
| 1374 | struct net_device *dev = NULL; |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1375 | struct rdma_cm_event event; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1376 | int ret; |
Steve Wise | 8d8293c | 2007-10-29 11:34:05 -0500 | [diff] [blame] | 1377 | struct ib_device_attr attr; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1378 | |
| 1379 | listen_id = cm_id->context; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1380 | if (cma_disable_callback(listen_id, RDMA_CM_LISTEN)) |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 1381 | return -ECONNABORTED; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1382 | |
| 1383 | /* Create a new RDMA id for the new IW CM ID */ |
| 1384 | new_cm_id = rdma_create_id(listen_id->id.event_handler, |
| 1385 | listen_id->id.context, |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 1386 | RDMA_PS_TCP, IB_QPT_RC); |
Julia Lawall | 10f3206 | 2008-04-16 21:09:25 -0700 | [diff] [blame] | 1387 | if (IS_ERR(new_cm_id)) { |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1388 | ret = -ENOMEM; |
| 1389 | goto out; |
| 1390 | } |
| 1391 | conn_id = container_of(new_cm_id, struct rdma_id_private, id); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1392 | mutex_lock_nested(&conn_id->handler_mutex, SINGLE_DEPTH_NESTING); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1393 | conn_id->state = RDMA_CM_CONNECT; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1394 | |
Denis V. Lunev | 1ab3527 | 2008-01-22 22:04:30 -0800 | [diff] [blame] | 1395 | dev = ip_dev_find(&init_net, iw_event->local_addr.sin_addr.s_addr); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1396 | if (!dev) { |
| 1397 | ret = -EADDRNOTAVAIL; |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1398 | mutex_unlock(&conn_id->handler_mutex); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1399 | rdma_destroy_id(new_cm_id); |
| 1400 | goto out; |
| 1401 | } |
| 1402 | ret = rdma_copy_addr(&conn_id->id.route.addr.dev_addr, dev, NULL); |
| 1403 | if (ret) { |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1404 | mutex_unlock(&conn_id->handler_mutex); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1405 | rdma_destroy_id(new_cm_id); |
| 1406 | goto out; |
| 1407 | } |
| 1408 | |
| 1409 | ret = cma_acquire_dev(conn_id); |
| 1410 | if (ret) { |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1411 | mutex_unlock(&conn_id->handler_mutex); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1412 | rdma_destroy_id(new_cm_id); |
| 1413 | goto out; |
| 1414 | } |
| 1415 | |
| 1416 | conn_id->cm_id.iw = cm_id; |
| 1417 | cm_id->context = conn_id; |
| 1418 | cm_id->cm_handler = cma_iw_handler; |
| 1419 | |
| 1420 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.src_addr; |
| 1421 | *sin = iw_event->local_addr; |
| 1422 | sin = (struct sockaddr_in *) &new_cm_id->route.addr.dst_addr; |
| 1423 | *sin = iw_event->remote_addr; |
| 1424 | |
Steve Wise | 8d8293c | 2007-10-29 11:34:05 -0500 | [diff] [blame] | 1425 | ret = ib_query_device(conn_id->id.device, &attr); |
| 1426 | if (ret) { |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1427 | mutex_unlock(&conn_id->handler_mutex); |
Steve Wise | 8d8293c | 2007-10-29 11:34:05 -0500 | [diff] [blame] | 1428 | rdma_destroy_id(new_cm_id); |
| 1429 | goto out; |
| 1430 | } |
| 1431 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1432 | memset(&event, 0, sizeof event); |
| 1433 | event.event = RDMA_CM_EVENT_CONNECT_REQUEST; |
| 1434 | event.param.conn.private_data = iw_event->private_data; |
| 1435 | event.param.conn.private_data_len = iw_event->private_data_len; |
Steve Wise | 8d8293c | 2007-10-29 11:34:05 -0500 | [diff] [blame] | 1436 | event.param.conn.initiator_depth = attr.max_qp_init_rd_atom; |
| 1437 | event.param.conn.responder_resources = attr.max_qp_rd_atom; |
Sean Hefty | 25ae21a | 2011-02-23 08:11:32 -0800 | [diff] [blame] | 1438 | |
| 1439 | /* |
| 1440 | * Protect against the user destroying conn_id from another thread |
| 1441 | * until we're done accessing it. |
| 1442 | */ |
| 1443 | atomic_inc(&conn_id->refcount); |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1444 | ret = conn_id->id.event_handler(&conn_id->id, &event); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1445 | if (ret) { |
| 1446 | /* User wants to destroy the CM ID */ |
| 1447 | conn_id->cm_id.iw = NULL; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1448 | cma_exch(conn_id, RDMA_CM_DESTROYING); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1449 | mutex_unlock(&conn_id->handler_mutex); |
Sean Hefty | 25ae21a | 2011-02-23 08:11:32 -0800 | [diff] [blame] | 1450 | cma_deref_id(conn_id); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1451 | rdma_destroy_id(&conn_id->id); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1452 | goto out; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1453 | } |
| 1454 | |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1455 | mutex_unlock(&conn_id->handler_mutex); |
Sean Hefty | 25ae21a | 2011-02-23 08:11:32 -0800 | [diff] [blame] | 1456 | cma_deref_id(conn_id); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1457 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1458 | out: |
| 1459 | if (dev) |
| 1460 | dev_put(dev); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1461 | mutex_unlock(&listen_id->handler_mutex); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1462 | return ret; |
| 1463 | } |
| 1464 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1465 | static int cma_ib_listen(struct rdma_id_private *id_priv) |
| 1466 | { |
| 1467 | struct ib_cm_compare_data compare_data; |
| 1468 | struct sockaddr *addr; |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1469 | struct ib_cm_id *id; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1470 | __be64 svc_id; |
| 1471 | int ret; |
| 1472 | |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1473 | id = ib_create_cm_id(id_priv->id.device, cma_req_handler, id_priv); |
| 1474 | if (IS_ERR(id)) |
| 1475 | return PTR_ERR(id); |
| 1476 | |
| 1477 | id_priv->cm_id.ib = id; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1478 | |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 1479 | addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1480 | svc_id = cma_get_service_id(id_priv->id.ps, addr); |
| 1481 | if (cma_any_addr(addr)) |
| 1482 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, NULL); |
| 1483 | else { |
| 1484 | cma_set_compare_data(id_priv->id.ps, addr, &compare_data); |
| 1485 | ret = ib_cm_listen(id_priv->cm_id.ib, svc_id, 0, &compare_data); |
| 1486 | } |
| 1487 | |
| 1488 | if (ret) { |
| 1489 | ib_destroy_cm_id(id_priv->cm_id.ib); |
| 1490 | id_priv->cm_id.ib = NULL; |
| 1491 | } |
| 1492 | |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1496 | static int cma_iw_listen(struct rdma_id_private *id_priv, int backlog) |
| 1497 | { |
| 1498 | int ret; |
| 1499 | struct sockaddr_in *sin; |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1500 | struct iw_cm_id *id; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1501 | |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 1502 | id = iw_create_cm_id(id_priv->id.device, |
| 1503 | iw_conn_req_handler, |
| 1504 | id_priv); |
| 1505 | if (IS_ERR(id)) |
| 1506 | return PTR_ERR(id); |
| 1507 | |
| 1508 | id_priv->cm_id.iw = id; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1509 | |
| 1510 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; |
| 1511 | id_priv->cm_id.iw->local_addr = *sin; |
| 1512 | |
| 1513 | ret = iw_cm_listen(id_priv->cm_id.iw, backlog); |
| 1514 | |
| 1515 | if (ret) { |
| 1516 | iw_destroy_cm_id(id_priv->cm_id.iw); |
| 1517 | id_priv->cm_id.iw = NULL; |
| 1518 | } |
| 1519 | |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1523 | static int cma_listen_handler(struct rdma_cm_id *id, |
| 1524 | struct rdma_cm_event *event) |
| 1525 | { |
| 1526 | struct rdma_id_private *id_priv = id->context; |
| 1527 | |
| 1528 | id->context = id_priv->id.context; |
| 1529 | id->event_handler = id_priv->id.event_handler; |
| 1530 | return id_priv->id.event_handler(id, event); |
| 1531 | } |
| 1532 | |
| 1533 | static void cma_listen_on_dev(struct rdma_id_private *id_priv, |
| 1534 | struct cma_device *cma_dev) |
| 1535 | { |
| 1536 | struct rdma_id_private *dev_id_priv; |
| 1537 | struct rdma_cm_id *id; |
| 1538 | int ret; |
| 1539 | |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 1540 | id = rdma_create_id(cma_listen_handler, id_priv, id_priv->id.ps, |
| 1541 | id_priv->id.qp_type); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1542 | if (IS_ERR(id)) |
| 1543 | return; |
| 1544 | |
| 1545 | dev_id_priv = container_of(id, struct rdma_id_private, id); |
| 1546 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1547 | dev_id_priv->state = RDMA_CM_ADDR_BOUND; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1548 | memcpy(&id->route.addr.src_addr, &id_priv->id.route.addr.src_addr, |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 1549 | ip_addr_size((struct sockaddr *) &id_priv->id.route.addr.src_addr)); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1550 | |
| 1551 | cma_attach_to_dev(dev_id_priv, cma_dev); |
| 1552 | list_add_tail(&dev_id_priv->listen_list, &id_priv->listen_list); |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 1553 | atomic_inc(&id_priv->refcount); |
| 1554 | dev_id_priv->internal_id = 1; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1555 | |
| 1556 | ret = rdma_listen(id, id_priv->backlog); |
| 1557 | if (ret) |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 1558 | printk(KERN_WARNING "RDMA CMA: cma_listen_on_dev, error %d, " |
Roland Dreier | 468f223 | 2008-07-14 23:48:47 -0700 | [diff] [blame] | 1559 | "listening on device %s\n", ret, cma_dev->device->name); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | static void cma_listen_on_all(struct rdma_id_private *id_priv) |
| 1563 | { |
| 1564 | struct cma_device *cma_dev; |
| 1565 | |
| 1566 | mutex_lock(&lock); |
| 1567 | list_add_tail(&id_priv->list, &listen_any_list); |
| 1568 | list_for_each_entry(cma_dev, &dev_list, list) |
| 1569 | cma_listen_on_dev(id_priv, cma_dev); |
| 1570 | mutex_unlock(&lock); |
| 1571 | } |
| 1572 | |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 1573 | void rdma_set_service_type(struct rdma_cm_id *id, int tos) |
| 1574 | { |
| 1575 | struct rdma_id_private *id_priv; |
| 1576 | |
| 1577 | id_priv = container_of(id, struct rdma_id_private, id); |
| 1578 | id_priv->tos = (u8) tos; |
| 1579 | } |
| 1580 | EXPORT_SYMBOL(rdma_set_service_type); |
| 1581 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1582 | static void cma_query_handler(int status, struct ib_sa_path_rec *path_rec, |
| 1583 | void *context) |
| 1584 | { |
| 1585 | struct cma_work *work = context; |
| 1586 | struct rdma_route *route; |
| 1587 | |
| 1588 | route = &work->id->id.route; |
| 1589 | |
| 1590 | if (!status) { |
| 1591 | route->num_paths = 1; |
| 1592 | *route->path_rec = *path_rec; |
| 1593 | } else { |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1594 | work->old_state = RDMA_CM_ROUTE_QUERY; |
| 1595 | work->new_state = RDMA_CM_ADDR_RESOLVED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1596 | work->event.event = RDMA_CM_EVENT_ROUTE_ERROR; |
Sean Hefty | 8f0472d | 2006-09-29 11:57:09 -0700 | [diff] [blame] | 1597 | work->event.status = status; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | queue_work(cma_wq, &work->work); |
| 1601 | } |
| 1602 | |
| 1603 | static int cma_query_ib_route(struct rdma_id_private *id_priv, int timeout_ms, |
| 1604 | struct cma_work *work) |
| 1605 | { |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 1606 | struct rdma_addr *addr = &id_priv->id.route.addr; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1607 | struct ib_sa_path_rec path_rec; |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 1608 | ib_sa_comp_mask comp_mask; |
| 1609 | struct sockaddr_in6 *sin6; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1610 | |
| 1611 | memset(&path_rec, 0, sizeof path_rec); |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1612 | rdma_addr_get_sgid(&addr->dev_addr, &path_rec.sgid); |
| 1613 | rdma_addr_get_dgid(&addr->dev_addr, &path_rec.dgid); |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 1614 | path_rec.pkey = cpu_to_be16(ib_addr_get_pkey(&addr->dev_addr)); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1615 | path_rec.numb_path = 1; |
Sean Hefty | 962063e | 2007-02-21 16:40:44 -0800 | [diff] [blame] | 1616 | path_rec.reversible = 1; |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 1617 | path_rec.service_id = cma_get_service_id(id_priv->id.ps, |
| 1618 | (struct sockaddr *) &addr->dst_addr); |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 1619 | |
| 1620 | comp_mask = IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID | |
| 1621 | IB_SA_PATH_REC_PKEY | IB_SA_PATH_REC_NUMB_PATH | |
| 1622 | IB_SA_PATH_REC_REVERSIBLE | IB_SA_PATH_REC_SERVICE_ID; |
| 1623 | |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 1624 | if (addr->src_addr.ss_family == AF_INET) { |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 1625 | path_rec.qos_class = cpu_to_be16((u16) id_priv->tos); |
| 1626 | comp_mask |= IB_SA_PATH_REC_QOS_CLASS; |
| 1627 | } else { |
| 1628 | sin6 = (struct sockaddr_in6 *) &addr->src_addr; |
| 1629 | path_rec.traffic_class = (u8) (be32_to_cpu(sin6->sin6_flowinfo) >> 20); |
| 1630 | comp_mask |= IB_SA_PATH_REC_TRAFFIC_CLASS; |
| 1631 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1632 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 1633 | id_priv->query_id = ib_sa_path_rec_get(&sa_client, id_priv->id.device, |
Sean Hefty | a81c994 | 2007-08-08 15:51:06 -0700 | [diff] [blame] | 1634 | id_priv->id.port_num, &path_rec, |
| 1635 | comp_mask, timeout_ms, |
| 1636 | GFP_KERNEL, cma_query_handler, |
| 1637 | work, &id_priv->query); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1638 | |
| 1639 | return (id_priv->query_id < 0) ? id_priv->query_id : 0; |
| 1640 | } |
| 1641 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1642 | static void cma_work_handler(struct work_struct *_work) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1643 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1644 | struct cma_work *work = container_of(_work, struct cma_work, work); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1645 | struct rdma_id_private *id_priv = work->id; |
| 1646 | int destroy = 0; |
| 1647 | |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1648 | mutex_lock(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1649 | if (!cma_comp_exch(id_priv, work->old_state, work->new_state)) |
| 1650 | goto out; |
| 1651 | |
| 1652 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1653 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1654 | destroy = 1; |
| 1655 | } |
| 1656 | out: |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1657 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1658 | cma_deref_id(id_priv); |
| 1659 | if (destroy) |
| 1660 | rdma_destroy_id(&id_priv->id); |
| 1661 | kfree(work); |
| 1662 | } |
| 1663 | |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 1664 | static void cma_ndev_work_handler(struct work_struct *_work) |
| 1665 | { |
| 1666 | struct cma_ndev_work *work = container_of(_work, struct cma_ndev_work, work); |
| 1667 | struct rdma_id_private *id_priv = work->id; |
| 1668 | int destroy = 0; |
| 1669 | |
| 1670 | mutex_lock(&id_priv->handler_mutex); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1671 | if (id_priv->state == RDMA_CM_DESTROYING || |
| 1672 | id_priv->state == RDMA_CM_DEVICE_REMOVAL) |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 1673 | goto out; |
| 1674 | |
| 1675 | if (id_priv->id.event_handler(&id_priv->id, &work->event)) { |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1676 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 1677 | destroy = 1; |
| 1678 | } |
| 1679 | |
| 1680 | out: |
| 1681 | mutex_unlock(&id_priv->handler_mutex); |
| 1682 | cma_deref_id(id_priv); |
| 1683 | if (destroy) |
| 1684 | rdma_destroy_id(&id_priv->id); |
| 1685 | kfree(work); |
| 1686 | } |
| 1687 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1688 | static int cma_resolve_ib_route(struct rdma_id_private *id_priv, int timeout_ms) |
| 1689 | { |
| 1690 | struct rdma_route *route = &id_priv->id.route; |
| 1691 | struct cma_work *work; |
| 1692 | int ret; |
| 1693 | |
| 1694 | work = kzalloc(sizeof *work, GFP_KERNEL); |
| 1695 | if (!work) |
| 1696 | return -ENOMEM; |
| 1697 | |
| 1698 | work->id = id_priv; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1699 | INIT_WORK(&work->work, cma_work_handler); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1700 | work->old_state = RDMA_CM_ROUTE_QUERY; |
| 1701 | work->new_state = RDMA_CM_ROUTE_RESOLVED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1702 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
| 1703 | |
| 1704 | route->path_rec = kmalloc(sizeof *route->path_rec, GFP_KERNEL); |
| 1705 | if (!route->path_rec) { |
| 1706 | ret = -ENOMEM; |
| 1707 | goto err1; |
| 1708 | } |
| 1709 | |
| 1710 | ret = cma_query_ib_route(id_priv, timeout_ms, work); |
| 1711 | if (ret) |
| 1712 | goto err2; |
| 1713 | |
| 1714 | return 0; |
| 1715 | err2: |
| 1716 | kfree(route->path_rec); |
| 1717 | route->path_rec = NULL; |
| 1718 | err1: |
| 1719 | kfree(work); |
| 1720 | return ret; |
| 1721 | } |
| 1722 | |
| 1723 | int rdma_set_ib_paths(struct rdma_cm_id *id, |
| 1724 | struct ib_sa_path_rec *path_rec, int num_paths) |
| 1725 | { |
| 1726 | struct rdma_id_private *id_priv; |
| 1727 | int ret; |
| 1728 | |
| 1729 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1730 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
| 1731 | RDMA_CM_ROUTE_RESOLVED)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1732 | return -EINVAL; |
| 1733 | |
Julia Lawall | 9893e74 | 2010-05-15 23:22:38 +0200 | [diff] [blame] | 1734 | id->route.path_rec = kmemdup(path_rec, sizeof *path_rec * num_paths, |
| 1735 | GFP_KERNEL); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1736 | if (!id->route.path_rec) { |
| 1737 | ret = -ENOMEM; |
| 1738 | goto err; |
| 1739 | } |
| 1740 | |
Sean Hefty | ae2d929 | 2010-03-25 19:12:36 +0000 | [diff] [blame] | 1741 | id->route.num_paths = num_paths; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1742 | return 0; |
| 1743 | err: |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1744 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_ADDR_RESOLVED); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1745 | return ret; |
| 1746 | } |
| 1747 | EXPORT_SYMBOL(rdma_set_ib_paths); |
| 1748 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1749 | static int cma_resolve_iw_route(struct rdma_id_private *id_priv, int timeout_ms) |
| 1750 | { |
| 1751 | struct cma_work *work; |
| 1752 | |
| 1753 | work = kzalloc(sizeof *work, GFP_KERNEL); |
| 1754 | if (!work) |
| 1755 | return -ENOMEM; |
| 1756 | |
| 1757 | work->id = id_priv; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1758 | INIT_WORK(&work->work, cma_work_handler); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1759 | work->old_state = RDMA_CM_ROUTE_QUERY; |
| 1760 | work->new_state = RDMA_CM_ROUTE_RESOLVED; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1761 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
| 1762 | queue_work(cma_wq, &work->work); |
| 1763 | return 0; |
| 1764 | } |
| 1765 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 1766 | static int cma_resolve_iboe_route(struct rdma_id_private *id_priv) |
| 1767 | { |
| 1768 | struct rdma_route *route = &id_priv->id.route; |
| 1769 | struct rdma_addr *addr = &route->addr; |
| 1770 | struct cma_work *work; |
| 1771 | int ret; |
| 1772 | struct sockaddr_in *src_addr = (struct sockaddr_in *)&route->addr.src_addr; |
| 1773 | struct sockaddr_in *dst_addr = (struct sockaddr_in *)&route->addr.dst_addr; |
| 1774 | struct net_device *ndev = NULL; |
Eli Cohen | af7bd46 | 2010-08-26 17:18:59 +0300 | [diff] [blame] | 1775 | u16 vid; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 1776 | |
| 1777 | if (src_addr->sin_family != dst_addr->sin_family) |
| 1778 | return -EINVAL; |
| 1779 | |
| 1780 | work = kzalloc(sizeof *work, GFP_KERNEL); |
| 1781 | if (!work) |
| 1782 | return -ENOMEM; |
| 1783 | |
| 1784 | work->id = id_priv; |
| 1785 | INIT_WORK(&work->work, cma_work_handler); |
| 1786 | |
| 1787 | route->path_rec = kzalloc(sizeof *route->path_rec, GFP_KERNEL); |
| 1788 | if (!route->path_rec) { |
| 1789 | ret = -ENOMEM; |
| 1790 | goto err1; |
| 1791 | } |
| 1792 | |
| 1793 | route->num_paths = 1; |
| 1794 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 1795 | if (addr->dev_addr.bound_dev_if) |
| 1796 | ndev = dev_get_by_index(&init_net, addr->dev_addr.bound_dev_if); |
| 1797 | if (!ndev) { |
| 1798 | ret = -ENODEV; |
| 1799 | goto err2; |
| 1800 | } |
| 1801 | |
Eli Cohen | af7bd46 | 2010-08-26 17:18:59 +0300 | [diff] [blame] | 1802 | vid = rdma_vlan_dev_vlan_id(ndev); |
| 1803 | |
| 1804 | iboe_mac_vlan_to_ll(&route->path_rec->sgid, addr->dev_addr.src_dev_addr, vid); |
| 1805 | iboe_mac_vlan_to_ll(&route->path_rec->dgid, addr->dev_addr.dst_dev_addr, vid); |
| 1806 | |
| 1807 | route->path_rec->hop_limit = 1; |
| 1808 | route->path_rec->reversible = 1; |
| 1809 | route->path_rec->pkey = cpu_to_be16(0xffff); |
| 1810 | route->path_rec->mtu_selector = IB_SA_EQ; |
| 1811 | route->path_rec->sl = id_priv->tos >> 5; |
| 1812 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 1813 | route->path_rec->mtu = iboe_get_mtu(ndev->mtu); |
| 1814 | route->path_rec->rate_selector = IB_SA_EQ; |
| 1815 | route->path_rec->rate = iboe_get_rate(ndev); |
| 1816 | dev_put(ndev); |
| 1817 | route->path_rec->packet_life_time_selector = IB_SA_EQ; |
| 1818 | route->path_rec->packet_life_time = CMA_IBOE_PACKET_LIFETIME; |
| 1819 | if (!route->path_rec->mtu) { |
| 1820 | ret = -EINVAL; |
| 1821 | goto err2; |
| 1822 | } |
| 1823 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1824 | work->old_state = RDMA_CM_ROUTE_QUERY; |
| 1825 | work->new_state = RDMA_CM_ROUTE_RESOLVED; |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 1826 | work->event.event = RDMA_CM_EVENT_ROUTE_RESOLVED; |
| 1827 | work->event.status = 0; |
| 1828 | |
| 1829 | queue_work(cma_wq, &work->work); |
| 1830 | |
| 1831 | return 0; |
| 1832 | |
| 1833 | err2: |
| 1834 | kfree(route->path_rec); |
| 1835 | route->path_rec = NULL; |
| 1836 | err1: |
| 1837 | kfree(work); |
| 1838 | return ret; |
| 1839 | } |
| 1840 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1841 | int rdma_resolve_route(struct rdma_cm_id *id, int timeout_ms) |
| 1842 | { |
| 1843 | struct rdma_id_private *id_priv; |
| 1844 | int ret; |
| 1845 | |
| 1846 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1847 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, RDMA_CM_ROUTE_QUERY)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1848 | return -EINVAL; |
| 1849 | |
| 1850 | atomic_inc(&id_priv->refcount); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1851 | switch (rdma_node_get_transport(id->device->node_type)) { |
| 1852 | case RDMA_TRANSPORT_IB: |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 1853 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { |
| 1854 | case IB_LINK_LAYER_INFINIBAND: |
| 1855 | ret = cma_resolve_ib_route(id_priv, timeout_ms); |
| 1856 | break; |
| 1857 | case IB_LINK_LAYER_ETHERNET: |
| 1858 | ret = cma_resolve_iboe_route(id_priv); |
| 1859 | break; |
| 1860 | default: |
| 1861 | ret = -ENOSYS; |
| 1862 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1863 | break; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1864 | case RDMA_TRANSPORT_IWARP: |
| 1865 | ret = cma_resolve_iw_route(id_priv, timeout_ms); |
| 1866 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1867 | default: |
| 1868 | ret = -ENOSYS; |
| 1869 | break; |
| 1870 | } |
| 1871 | if (ret) |
| 1872 | goto err; |
| 1873 | |
| 1874 | return 0; |
| 1875 | err: |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1876 | cma_comp_exch(id_priv, RDMA_CM_ROUTE_QUERY, RDMA_CM_ADDR_RESOLVED); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1877 | cma_deref_id(id_priv); |
| 1878 | return ret; |
| 1879 | } |
| 1880 | EXPORT_SYMBOL(rdma_resolve_route); |
| 1881 | |
| 1882 | static int cma_bind_loopback(struct rdma_id_private *id_priv) |
| 1883 | { |
| 1884 | struct cma_device *cma_dev; |
| 1885 | struct ib_port_attr port_attr; |
Michael S. Tsirkin | f0ee340 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 1886 | union ib_gid gid; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1887 | u16 pkey; |
| 1888 | int ret; |
| 1889 | u8 p; |
| 1890 | |
| 1891 | mutex_lock(&lock); |
Krishna Kumar | e82153b | 2006-10-16 10:09:01 +0530 | [diff] [blame] | 1892 | if (list_empty(&dev_list)) { |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1893 | ret = -ENODEV; |
| 1894 | goto out; |
| 1895 | } |
Krishna Kumar | e82153b | 2006-10-16 10:09:01 +0530 | [diff] [blame] | 1896 | list_for_each_entry(cma_dev, &dev_list, list) |
| 1897 | for (p = 1; p <= cma_dev->device->phys_port_cnt; ++p) |
| 1898 | if (!ib_query_port(cma_dev->device, p, &port_attr) && |
| 1899 | port_attr.state == IB_PORT_ACTIVE) |
| 1900 | goto port_found; |
| 1901 | |
| 1902 | p = 1; |
| 1903 | cma_dev = list_entry(dev_list.next, struct cma_device, list); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1904 | |
| 1905 | port_found: |
Michael S. Tsirkin | f0ee340 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 1906 | ret = ib_get_cached_gid(cma_dev->device, p, 0, &gid); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1907 | if (ret) |
| 1908 | goto out; |
| 1909 | |
| 1910 | ret = ib_get_cached_pkey(cma_dev->device, p, 0, &pkey); |
| 1911 | if (ret) |
| 1912 | goto out; |
| 1913 | |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1914 | id_priv->id.route.addr.dev_addr.dev_type = |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 1915 | (rdma_port_get_link_layer(cma_dev->device, p) == IB_LINK_LAYER_INFINIBAND) ? |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1916 | ARPHRD_INFINIBAND : ARPHRD_ETHER; |
| 1917 | |
| 1918 | rdma_addr_set_sgid(&id_priv->id.route.addr.dev_addr, &gid); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1919 | ib_addr_set_pkey(&id_priv->id.route.addr.dev_addr, pkey); |
| 1920 | id_priv->id.port_num = p; |
| 1921 | cma_attach_to_dev(id_priv, cma_dev); |
| 1922 | out: |
| 1923 | mutex_unlock(&lock); |
| 1924 | return ret; |
| 1925 | } |
| 1926 | |
| 1927 | static void addr_handler(int status, struct sockaddr *src_addr, |
| 1928 | struct rdma_dev_addr *dev_addr, void *context) |
| 1929 | { |
| 1930 | struct rdma_id_private *id_priv = context; |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1931 | struct rdma_cm_event event; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1932 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1933 | memset(&event, 0, sizeof event); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1934 | mutex_lock(&id_priv->handler_mutex); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1935 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, |
| 1936 | RDMA_CM_ADDR_RESOLVED)) |
Sean Hefty | 61a73c7 | 2006-09-01 15:33:55 -0700 | [diff] [blame] | 1937 | goto out; |
Sean Hefty | 61a73c7 | 2006-09-01 15:33:55 -0700 | [diff] [blame] | 1938 | |
| 1939 | if (!status && !id_priv->cma_dev) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1940 | status = cma_acquire_dev(id_priv); |
| 1941 | |
| 1942 | if (status) { |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1943 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_RESOLVED, |
| 1944 | RDMA_CM_ADDR_BOUND)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1945 | goto out; |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1946 | event.event = RDMA_CM_EVENT_ADDR_ERROR; |
| 1947 | event.status = status; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1948 | } else { |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1949 | memcpy(&id_priv->id.route.addr.src_addr, src_addr, |
| 1950 | ip_addr_size(src_addr)); |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1951 | event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 1954 | if (id_priv->id.event_handler(&id_priv->id, &event)) { |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 1955 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1956 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1957 | cma_deref_id(id_priv); |
| 1958 | rdma_destroy_id(&id_priv->id); |
| 1959 | return; |
| 1960 | } |
| 1961 | out: |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 1962 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1963 | cma_deref_id(id_priv); |
| 1964 | } |
| 1965 | |
| 1966 | static int cma_resolve_loopback(struct rdma_id_private *id_priv) |
| 1967 | { |
| 1968 | struct cma_work *work; |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1969 | struct sockaddr *src, *dst; |
Michael S. Tsirkin | f0ee340 | 2006-07-14 00:23:52 -0700 | [diff] [blame] | 1970 | union ib_gid gid; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1971 | int ret; |
| 1972 | |
| 1973 | work = kzalloc(sizeof *work, GFP_KERNEL); |
| 1974 | if (!work) |
| 1975 | return -ENOMEM; |
| 1976 | |
| 1977 | if (!id_priv->cma_dev) { |
| 1978 | ret = cma_bind_loopback(id_priv); |
| 1979 | if (ret) |
| 1980 | goto err; |
| 1981 | } |
| 1982 | |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1983 | rdma_addr_get_sgid(&id_priv->id.route.addr.dev_addr, &gid); |
| 1984 | rdma_addr_set_dgid(&id_priv->id.route.addr.dev_addr, &gid); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1985 | |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 1986 | src = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
| 1987 | if (cma_zero_addr(src)) { |
| 1988 | dst = (struct sockaddr *) &id_priv->id.route.addr.dst_addr; |
| 1989 | if ((src->sa_family = dst->sa_family) == AF_INET) { |
| 1990 | ((struct sockaddr_in *) src)->sin_addr.s_addr = |
| 1991 | ((struct sockaddr_in *) dst)->sin_addr.s_addr; |
| 1992 | } else { |
| 1993 | ipv6_addr_copy(&((struct sockaddr_in6 *) src)->sin6_addr, |
| 1994 | &((struct sockaddr_in6 *) dst)->sin6_addr); |
| 1995 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | work->id = id_priv; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1999 | INIT_WORK(&work->work, cma_work_handler); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2000 | work->old_state = RDMA_CM_ADDR_QUERY; |
| 2001 | work->new_state = RDMA_CM_ADDR_RESOLVED; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2002 | work->event.event = RDMA_CM_EVENT_ADDR_RESOLVED; |
| 2003 | queue_work(cma_wq, &work->work); |
| 2004 | return 0; |
| 2005 | err: |
| 2006 | kfree(work); |
| 2007 | return ret; |
| 2008 | } |
| 2009 | |
| 2010 | static int cma_bind_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, |
| 2011 | struct sockaddr *dst_addr) |
| 2012 | { |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 2013 | if (!src_addr || !src_addr->sa_family) { |
| 2014 | src_addr = (struct sockaddr *) &id->route.addr.src_addr; |
| 2015 | if ((src_addr->sa_family = dst_addr->sa_family) == AF_INET6) { |
| 2016 | ((struct sockaddr_in6 *) src_addr)->sin6_scope_id = |
| 2017 | ((struct sockaddr_in6 *) dst_addr)->sin6_scope_id; |
| 2018 | } |
| 2019 | } |
| 2020 | return rdma_bind_addr(id, src_addr); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | int rdma_resolve_addr(struct rdma_cm_id *id, struct sockaddr *src_addr, |
| 2024 | struct sockaddr *dst_addr, int timeout_ms) |
| 2025 | { |
| 2026 | struct rdma_id_private *id_priv; |
| 2027 | int ret; |
| 2028 | |
| 2029 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2030 | if (id_priv->state == RDMA_CM_IDLE) { |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2031 | ret = cma_bind_addr(id, src_addr, dst_addr); |
| 2032 | if (ret) |
| 2033 | return ret; |
| 2034 | } |
| 2035 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2036 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_ADDR_QUERY)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2037 | return -EINVAL; |
| 2038 | |
| 2039 | atomic_inc(&id_priv->refcount); |
| 2040 | memcpy(&id->route.addr.dst_addr, dst_addr, ip_addr_size(dst_addr)); |
| 2041 | if (cma_any_addr(dst_addr)) |
| 2042 | ret = cma_resolve_loopback(id_priv); |
| 2043 | else |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 2044 | ret = rdma_resolve_ip(&addr_client, (struct sockaddr *) &id->route.addr.src_addr, |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 2045 | dst_addr, &id->route.addr.dev_addr, |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2046 | timeout_ms, addr_handler, id_priv); |
| 2047 | if (ret) |
| 2048 | goto err; |
| 2049 | |
| 2050 | return 0; |
| 2051 | err: |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2052 | cma_comp_exch(id_priv, RDMA_CM_ADDR_QUERY, RDMA_CM_ADDR_BOUND); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2053 | cma_deref_id(id_priv); |
| 2054 | return ret; |
| 2055 | } |
| 2056 | EXPORT_SYMBOL(rdma_resolve_addr); |
| 2057 | |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2058 | int rdma_set_reuseaddr(struct rdma_cm_id *id, int reuse) |
| 2059 | { |
| 2060 | struct rdma_id_private *id_priv; |
| 2061 | unsigned long flags; |
| 2062 | int ret; |
| 2063 | |
| 2064 | id_priv = container_of(id, struct rdma_id_private, id); |
| 2065 | spin_lock_irqsave(&id_priv->lock, flags); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2066 | if (id_priv->state == RDMA_CM_IDLE) { |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2067 | id_priv->reuseaddr = reuse; |
| 2068 | ret = 0; |
| 2069 | } else { |
| 2070 | ret = -EINVAL; |
| 2071 | } |
| 2072 | spin_unlock_irqrestore(&id_priv->lock, flags); |
| 2073 | return ret; |
| 2074 | } |
| 2075 | EXPORT_SYMBOL(rdma_set_reuseaddr); |
| 2076 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2077 | static void cma_bind_port(struct rdma_bind_list *bind_list, |
| 2078 | struct rdma_id_private *id_priv) |
| 2079 | { |
| 2080 | struct sockaddr_in *sin; |
| 2081 | |
| 2082 | sin = (struct sockaddr_in *) &id_priv->id.route.addr.src_addr; |
| 2083 | sin->sin_port = htons(bind_list->port); |
| 2084 | id_priv->bind_list = bind_list; |
| 2085 | hlist_add_head(&id_priv->node, &bind_list->owners); |
| 2086 | } |
| 2087 | |
| 2088 | static int cma_alloc_port(struct idr *ps, struct rdma_id_private *id_priv, |
| 2089 | unsigned short snum) |
| 2090 | { |
| 2091 | struct rdma_bind_list *bind_list; |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2092 | int port, ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2093 | |
Sean Hefty | cb164b8 | 2007-03-05 12:50:17 -0800 | [diff] [blame] | 2094 | bind_list = kzalloc(sizeof *bind_list, GFP_KERNEL); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2095 | if (!bind_list) |
| 2096 | return -ENOMEM; |
| 2097 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2098 | do { |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2099 | ret = idr_get_new_above(ps, bind_list, snum, &port); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2100 | } while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL)); |
| 2101 | |
| 2102 | if (ret) |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2103 | goto err1; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2104 | |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2105 | if (port != snum) { |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2106 | ret = -EADDRNOTAVAIL; |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2107 | goto err2; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | bind_list->ps = ps; |
| 2111 | bind_list->port = (unsigned short) port; |
| 2112 | cma_bind_port(bind_list, id_priv); |
| 2113 | return 0; |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2114 | err2: |
| 2115 | idr_remove(ps, port); |
| 2116 | err1: |
| 2117 | kfree(bind_list); |
| 2118 | return ret; |
| 2119 | } |
| 2120 | |
| 2121 | static int cma_alloc_any_port(struct idr *ps, struct rdma_id_private *id_priv) |
| 2122 | { |
Tetsuo Handa | 5d7220e8 | 2010-04-15 11:29:04 +0900 | [diff] [blame] | 2123 | static unsigned int last_used_port; |
| 2124 | int low, high, remaining; |
| 2125 | unsigned int rover; |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2126 | |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 2127 | inet_get_local_port_range(&low, &high); |
Tetsuo Handa | 5d7220e8 | 2010-04-15 11:29:04 +0900 | [diff] [blame] | 2128 | remaining = (high - low) + 1; |
| 2129 | rover = net_random() % remaining + low; |
| 2130 | retry: |
| 2131 | if (last_used_port != rover && |
| 2132 | !idr_find(ps, (unsigned short) rover)) { |
| 2133 | int ret = cma_alloc_port(ps, id_priv, rover); |
| 2134 | /* |
| 2135 | * Remember previously used port number in order to avoid |
| 2136 | * re-using same port immediately after it is closed. |
| 2137 | */ |
| 2138 | if (!ret) |
| 2139 | last_used_port = rover; |
| 2140 | if (ret != -EADDRNOTAVAIL) |
| 2141 | return ret; |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2142 | } |
Tetsuo Handa | 5d7220e8 | 2010-04-15 11:29:04 +0900 | [diff] [blame] | 2143 | if (--remaining) { |
| 2144 | rover++; |
| 2145 | if ((rover < low) || (rover > high)) |
| 2146 | rover = low; |
| 2147 | goto retry; |
| 2148 | } |
| 2149 | return -EADDRNOTAVAIL; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2152 | /* |
| 2153 | * Check that the requested port is available. This is called when trying to |
| 2154 | * bind to a specific port, or when trying to listen on a bound port. In |
| 2155 | * the latter case, the provided id_priv may already be on the bind_list, but |
| 2156 | * we still need to check that it's okay to start listening. |
| 2157 | */ |
| 2158 | static int cma_check_port(struct rdma_bind_list *bind_list, |
| 2159 | struct rdma_id_private *id_priv, uint8_t reuseaddr) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2160 | { |
| 2161 | struct rdma_id_private *cur_id; |
Hefty, Sean | 43b752d | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2162 | struct sockaddr *addr, *cur_addr; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2163 | struct hlist_node *node; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2164 | |
Hefty, Sean | 43b752d | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2165 | addr = (struct sockaddr *) &id_priv->id.route.addr.src_addr; |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2166 | if (cma_any_addr(addr) && !reuseaddr) |
| 2167 | return -EADDRNOTAVAIL; |
| 2168 | |
| 2169 | hlist_for_each_entry(cur_id, node, &bind_list->owners, node) { |
| 2170 | if (id_priv == cur_id) |
| 2171 | continue; |
| 2172 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2173 | if ((cur_id->state == RDMA_CM_LISTEN) || |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2174 | !reuseaddr || !cur_id->reuseaddr) { |
| 2175 | cur_addr = (struct sockaddr *) &cur_id->id.route.addr.src_addr; |
| 2176 | if (cma_any_addr(cur_addr)) |
| 2177 | return -EADDRNOTAVAIL; |
| 2178 | |
| 2179 | if (!cma_addr_cmp(addr, cur_addr)) |
| 2180 | return -EADDRINUSE; |
| 2181 | } |
| 2182 | } |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | static int cma_use_port(struct idr *ps, struct rdma_id_private *id_priv) |
| 2187 | { |
| 2188 | struct rdma_bind_list *bind_list; |
| 2189 | unsigned short snum; |
| 2190 | int ret; |
| 2191 | |
| 2192 | snum = ntohs(cma_port((struct sockaddr *) &id_priv->id.route.addr.src_addr)); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2193 | if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE)) |
| 2194 | return -EACCES; |
| 2195 | |
| 2196 | bind_list = idr_find(ps, snum); |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2197 | if (!bind_list) { |
| 2198 | ret = cma_alloc_port(ps, id_priv, snum); |
| 2199 | } else { |
| 2200 | ret = cma_check_port(bind_list, id_priv, id_priv->reuseaddr); |
| 2201 | if (!ret) |
| 2202 | cma_bind_port(bind_list, id_priv); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2203 | } |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2204 | return ret; |
| 2205 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2206 | |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2207 | static int cma_bind_listen(struct rdma_id_private *id_priv) |
| 2208 | { |
| 2209 | struct rdma_bind_list *bind_list = id_priv->bind_list; |
| 2210 | int ret = 0; |
| 2211 | |
| 2212 | mutex_lock(&lock); |
| 2213 | if (bind_list->owners.first->next) |
| 2214 | ret = cma_check_port(bind_list, id_priv, 0); |
| 2215 | mutex_unlock(&lock); |
| 2216 | return ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | static int cma_get_port(struct rdma_id_private *id_priv) |
| 2220 | { |
| 2221 | struct idr *ps; |
| 2222 | int ret; |
| 2223 | |
| 2224 | switch (id_priv->id.ps) { |
| 2225 | case RDMA_PS_SDP: |
| 2226 | ps = &sdp_ps; |
| 2227 | break; |
| 2228 | case RDMA_PS_TCP: |
| 2229 | ps = &tcp_ps; |
| 2230 | break; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2231 | case RDMA_PS_UDP: |
| 2232 | ps = &udp_ps; |
| 2233 | break; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2234 | case RDMA_PS_IPOIB: |
| 2235 | ps = &ipoib_ps; |
| 2236 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2237 | default: |
| 2238 | return -EPROTONOSUPPORT; |
| 2239 | } |
| 2240 | |
| 2241 | mutex_lock(&lock); |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 2242 | if (cma_any_port((struct sockaddr *) &id_priv->id.route.addr.src_addr)) |
Sean Hefty | aedec08 | 2007-01-29 16:41:23 -0800 | [diff] [blame] | 2243 | ret = cma_alloc_any_port(ps, id_priv); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2244 | else |
| 2245 | ret = cma_use_port(ps, id_priv); |
| 2246 | mutex_unlock(&lock); |
| 2247 | |
| 2248 | return ret; |
| 2249 | } |
| 2250 | |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 2251 | static int cma_check_linklocal(struct rdma_dev_addr *dev_addr, |
| 2252 | struct sockaddr *addr) |
| 2253 | { |
Robert P. J. Day | fd4582a | 2010-01-06 13:16:30 -0800 | [diff] [blame] | 2254 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 2255 | struct sockaddr_in6 *sin6; |
| 2256 | |
| 2257 | if (addr->sa_family != AF_INET6) |
| 2258 | return 0; |
| 2259 | |
| 2260 | sin6 = (struct sockaddr_in6 *) addr; |
| 2261 | if ((ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) && |
| 2262 | !sin6->sin6_scope_id) |
| 2263 | return -EINVAL; |
| 2264 | |
| 2265 | dev_addr->bound_dev_if = sin6->sin6_scope_id; |
| 2266 | #endif |
| 2267 | return 0; |
| 2268 | } |
| 2269 | |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2270 | int rdma_listen(struct rdma_cm_id *id, int backlog) |
| 2271 | { |
| 2272 | struct rdma_id_private *id_priv; |
| 2273 | int ret; |
| 2274 | |
| 2275 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2276 | if (id_priv->state == RDMA_CM_IDLE) { |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2277 | ((struct sockaddr *) &id->route.addr.src_addr)->sa_family = AF_INET; |
| 2278 | ret = rdma_bind_addr(id, (struct sockaddr *) &id->route.addr.src_addr); |
| 2279 | if (ret) |
| 2280 | return ret; |
| 2281 | } |
| 2282 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2283 | if (!cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_LISTEN)) |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2284 | return -EINVAL; |
| 2285 | |
| 2286 | if (id_priv->reuseaddr) { |
| 2287 | ret = cma_bind_listen(id_priv); |
| 2288 | if (ret) |
| 2289 | goto err; |
| 2290 | } |
| 2291 | |
| 2292 | id_priv->backlog = backlog; |
| 2293 | if (id->device) { |
| 2294 | switch (rdma_node_get_transport(id->device->node_type)) { |
| 2295 | case RDMA_TRANSPORT_IB: |
| 2296 | ret = cma_ib_listen(id_priv); |
| 2297 | if (ret) |
| 2298 | goto err; |
| 2299 | break; |
| 2300 | case RDMA_TRANSPORT_IWARP: |
| 2301 | ret = cma_iw_listen(id_priv, backlog); |
| 2302 | if (ret) |
| 2303 | goto err; |
| 2304 | break; |
| 2305 | default: |
| 2306 | ret = -ENOSYS; |
| 2307 | goto err; |
| 2308 | } |
| 2309 | } else |
| 2310 | cma_listen_on_all(id_priv); |
| 2311 | |
| 2312 | return 0; |
| 2313 | err: |
| 2314 | id_priv->backlog = 0; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2315 | cma_comp_exch(id_priv, RDMA_CM_LISTEN, RDMA_CM_ADDR_BOUND); |
Hefty, Sean | a9bb791 | 2011-05-09 22:06:10 -0700 | [diff] [blame] | 2316 | return ret; |
| 2317 | } |
| 2318 | EXPORT_SYMBOL(rdma_listen); |
| 2319 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2320 | int rdma_bind_addr(struct rdma_cm_id *id, struct sockaddr *addr) |
| 2321 | { |
| 2322 | struct rdma_id_private *id_priv; |
| 2323 | int ret; |
| 2324 | |
Aleksey Senin | 1f5175a | 2008-12-24 10:16:45 -0800 | [diff] [blame] | 2325 | if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2326 | return -EAFNOSUPPORT; |
| 2327 | |
| 2328 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2329 | if (!cma_comp_exch(id_priv, RDMA_CM_IDLE, RDMA_CM_ADDR_BOUND)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2330 | return -EINVAL; |
| 2331 | |
Sean Hefty | d14714d | 2009-11-19 16:46:25 -0800 | [diff] [blame] | 2332 | ret = cma_check_linklocal(&id->route.addr.dev_addr, addr); |
| 2333 | if (ret) |
| 2334 | goto err1; |
| 2335 | |
Sean Hefty | 8523c04 | 2010-02-08 16:41:15 -0800 | [diff] [blame] | 2336 | if (!cma_any_addr(addr)) { |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2337 | ret = rdma_translate_ip(addr, &id->route.addr.dev_addr); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2338 | if (ret) |
Krishna Kumar | 255d0c1 | 2006-10-24 13:22:28 -0700 | [diff] [blame] | 2339 | goto err1; |
| 2340 | |
Krishna Kumar | 255d0c1 | 2006-10-24 13:22:28 -0700 | [diff] [blame] | 2341 | ret = cma_acquire_dev(id_priv); |
Krishna Kumar | 255d0c1 | 2006-10-24 13:22:28 -0700 | [diff] [blame] | 2342 | if (ret) |
| 2343 | goto err1; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | memcpy(&id->route.addr.src_addr, addr, ip_addr_size(addr)); |
| 2347 | ret = cma_get_port(id_priv); |
| 2348 | if (ret) |
Krishna Kumar | 255d0c1 | 2006-10-24 13:22:28 -0700 | [diff] [blame] | 2349 | goto err2; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2350 | |
| 2351 | return 0; |
Krishna Kumar | 255d0c1 | 2006-10-24 13:22:28 -0700 | [diff] [blame] | 2352 | err2: |
Sean Hefty | a396d43 | 2011-02-23 09:05:39 -0800 | [diff] [blame] | 2353 | if (id_priv->cma_dev) |
| 2354 | cma_release_dev(id_priv); |
Krishna Kumar | 255d0c1 | 2006-10-24 13:22:28 -0700 | [diff] [blame] | 2355 | err1: |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2356 | cma_comp_exch(id_priv, RDMA_CM_ADDR_BOUND, RDMA_CM_IDLE); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2357 | return ret; |
| 2358 | } |
| 2359 | EXPORT_SYMBOL(rdma_bind_addr); |
| 2360 | |
| 2361 | static int cma_format_hdr(void *hdr, enum rdma_port_space ps, |
| 2362 | struct rdma_route *route) |
| 2363 | { |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2364 | struct cma_hdr *cma_hdr; |
| 2365 | struct sdp_hh *sdp_hdr; |
| 2366 | |
Aleksey Senin | 1f5175a | 2008-12-24 10:16:45 -0800 | [diff] [blame] | 2367 | if (route->addr.src_addr.ss_family == AF_INET) { |
| 2368 | struct sockaddr_in *src4, *dst4; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2369 | |
Aleksey Senin | 1f5175a | 2008-12-24 10:16:45 -0800 | [diff] [blame] | 2370 | src4 = (struct sockaddr_in *) &route->addr.src_addr; |
| 2371 | dst4 = (struct sockaddr_in *) &route->addr.dst_addr; |
| 2372 | |
| 2373 | switch (ps) { |
| 2374 | case RDMA_PS_SDP: |
| 2375 | sdp_hdr = hdr; |
| 2376 | if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION) |
| 2377 | return -EINVAL; |
| 2378 | sdp_set_ip_ver(sdp_hdr, 4); |
| 2379 | sdp_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; |
| 2380 | sdp_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; |
| 2381 | sdp_hdr->port = src4->sin_port; |
| 2382 | break; |
| 2383 | default: |
| 2384 | cma_hdr = hdr; |
| 2385 | cma_hdr->cma_version = CMA_VERSION; |
| 2386 | cma_set_ip_ver(cma_hdr, 4); |
| 2387 | cma_hdr->src_addr.ip4.addr = src4->sin_addr.s_addr; |
| 2388 | cma_hdr->dst_addr.ip4.addr = dst4->sin_addr.s_addr; |
| 2389 | cma_hdr->port = src4->sin_port; |
| 2390 | break; |
| 2391 | } |
| 2392 | } else { |
| 2393 | struct sockaddr_in6 *src6, *dst6; |
| 2394 | |
| 2395 | src6 = (struct sockaddr_in6 *) &route->addr.src_addr; |
| 2396 | dst6 = (struct sockaddr_in6 *) &route->addr.dst_addr; |
| 2397 | |
| 2398 | switch (ps) { |
| 2399 | case RDMA_PS_SDP: |
| 2400 | sdp_hdr = hdr; |
| 2401 | if (sdp_get_majv(sdp_hdr->sdp_version) != SDP_MAJ_VERSION) |
| 2402 | return -EINVAL; |
| 2403 | sdp_set_ip_ver(sdp_hdr, 6); |
| 2404 | sdp_hdr->src_addr.ip6 = src6->sin6_addr; |
| 2405 | sdp_hdr->dst_addr.ip6 = dst6->sin6_addr; |
| 2406 | sdp_hdr->port = src6->sin6_port; |
| 2407 | break; |
| 2408 | default: |
| 2409 | cma_hdr = hdr; |
| 2410 | cma_hdr->cma_version = CMA_VERSION; |
| 2411 | cma_set_ip_ver(cma_hdr, 6); |
| 2412 | cma_hdr->src_addr.ip6 = src6->sin6_addr; |
| 2413 | cma_hdr->dst_addr.ip6 = dst6->sin6_addr; |
| 2414 | cma_hdr->port = src6->sin6_port; |
| 2415 | break; |
| 2416 | } |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2417 | } |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2421 | static int cma_sidr_rep_handler(struct ib_cm_id *cm_id, |
| 2422 | struct ib_cm_event *ib_event) |
| 2423 | { |
| 2424 | struct rdma_id_private *id_priv = cm_id->context; |
| 2425 | struct rdma_cm_event event; |
| 2426 | struct ib_cm_sidr_rep_event_param *rep = &ib_event->param.sidr_rep_rcvd; |
| 2427 | int ret = 0; |
| 2428 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2429 | if (cma_disable_callback(id_priv, RDMA_CM_CONNECT)) |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 2430 | return 0; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2431 | |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 2432 | memset(&event, 0, sizeof event); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2433 | switch (ib_event->event) { |
| 2434 | case IB_CM_SIDR_REQ_ERROR: |
| 2435 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
| 2436 | event.status = -ETIMEDOUT; |
| 2437 | break; |
| 2438 | case IB_CM_SIDR_REP_RECEIVED: |
| 2439 | event.param.ud.private_data = ib_event->private_data; |
| 2440 | event.param.ud.private_data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE; |
| 2441 | if (rep->status != IB_SIDR_SUCCESS) { |
| 2442 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
| 2443 | event.status = ib_event->param.sidr_rep_rcvd.status; |
| 2444 | break; |
| 2445 | } |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 2446 | ret = cma_set_qkey(id_priv); |
| 2447 | if (ret) { |
| 2448 | event.event = RDMA_CM_EVENT_ADDR_ERROR; |
| 2449 | event.status = -EINVAL; |
| 2450 | break; |
| 2451 | } |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2452 | if (id_priv->qkey != rep->qkey) { |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2453 | event.event = RDMA_CM_EVENT_UNREACHABLE; |
| 2454 | event.status = -EINVAL; |
| 2455 | break; |
| 2456 | } |
| 2457 | ib_init_ah_from_path(id_priv->id.device, id_priv->id.port_num, |
| 2458 | id_priv->id.route.path_rec, |
| 2459 | &event.param.ud.ah_attr); |
| 2460 | event.param.ud.qp_num = rep->qpn; |
| 2461 | event.param.ud.qkey = rep->qkey; |
| 2462 | event.event = RDMA_CM_EVENT_ESTABLISHED; |
| 2463 | event.status = 0; |
| 2464 | break; |
| 2465 | default: |
Roland Dreier | 468f223 | 2008-07-14 23:48:47 -0700 | [diff] [blame] | 2466 | printk(KERN_ERR "RDMA CMA: unexpected IB CM event: %d\n", |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2467 | ib_event->event); |
| 2468 | goto out; |
| 2469 | } |
| 2470 | |
| 2471 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
| 2472 | if (ret) { |
| 2473 | /* Destroy the CM ID by returning a non-zero value. */ |
| 2474 | id_priv->cm_id.ib = NULL; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2475 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 2476 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2477 | rdma_destroy_id(&id_priv->id); |
| 2478 | return ret; |
| 2479 | } |
| 2480 | out: |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 2481 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2482 | return ret; |
| 2483 | } |
| 2484 | |
| 2485 | static int cma_resolve_ib_udp(struct rdma_id_private *id_priv, |
| 2486 | struct rdma_conn_param *conn_param) |
| 2487 | { |
| 2488 | struct ib_cm_sidr_req_param req; |
| 2489 | struct rdma_route *route; |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2490 | struct ib_cm_id *id; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2491 | int ret; |
| 2492 | |
| 2493 | req.private_data_len = sizeof(struct cma_hdr) + |
| 2494 | conn_param->private_data_len; |
| 2495 | req.private_data = kzalloc(req.private_data_len, GFP_ATOMIC); |
| 2496 | if (!req.private_data) |
| 2497 | return -ENOMEM; |
| 2498 | |
| 2499 | if (conn_param->private_data && conn_param->private_data_len) |
| 2500 | memcpy((void *) req.private_data + sizeof(struct cma_hdr), |
| 2501 | conn_param->private_data, conn_param->private_data_len); |
| 2502 | |
| 2503 | route = &id_priv->id.route; |
| 2504 | ret = cma_format_hdr((void *) req.private_data, id_priv->id.ps, route); |
| 2505 | if (ret) |
| 2506 | goto out; |
| 2507 | |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2508 | id = ib_create_cm_id(id_priv->id.device, cma_sidr_rep_handler, |
| 2509 | id_priv); |
| 2510 | if (IS_ERR(id)) { |
| 2511 | ret = PTR_ERR(id); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2512 | goto out; |
| 2513 | } |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2514 | id_priv->cm_id.ib = id; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2515 | |
| 2516 | req.path = route->path_rec; |
| 2517 | req.service_id = cma_get_service_id(id_priv->id.ps, |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 2518 | (struct sockaddr *) &route->addr.dst_addr); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2519 | req.timeout_ms = 1 << (CMA_CM_RESPONSE_TIMEOUT - 8); |
| 2520 | req.max_cm_retries = CMA_MAX_CM_RETRIES; |
| 2521 | |
| 2522 | ret = ib_send_cm_sidr_req(id_priv->cm_id.ib, &req); |
| 2523 | if (ret) { |
| 2524 | ib_destroy_cm_id(id_priv->cm_id.ib); |
| 2525 | id_priv->cm_id.ib = NULL; |
| 2526 | } |
| 2527 | out: |
| 2528 | kfree(req.private_data); |
| 2529 | return ret; |
| 2530 | } |
| 2531 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2532 | static int cma_connect_ib(struct rdma_id_private *id_priv, |
| 2533 | struct rdma_conn_param *conn_param) |
| 2534 | { |
| 2535 | struct ib_cm_req_param req; |
| 2536 | struct rdma_route *route; |
| 2537 | void *private_data; |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2538 | struct ib_cm_id *id; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2539 | int offset, ret; |
| 2540 | |
| 2541 | memset(&req, 0, sizeof req); |
| 2542 | offset = cma_user_data_offset(id_priv->id.ps); |
| 2543 | req.private_data_len = offset + conn_param->private_data_len; |
| 2544 | private_data = kzalloc(req.private_data_len, GFP_ATOMIC); |
| 2545 | if (!private_data) |
| 2546 | return -ENOMEM; |
| 2547 | |
| 2548 | if (conn_param->private_data && conn_param->private_data_len) |
| 2549 | memcpy(private_data + offset, conn_param->private_data, |
| 2550 | conn_param->private_data_len); |
| 2551 | |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2552 | id = ib_create_cm_id(id_priv->id.device, cma_ib_handler, id_priv); |
| 2553 | if (IS_ERR(id)) { |
| 2554 | ret = PTR_ERR(id); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2555 | goto out; |
| 2556 | } |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2557 | id_priv->cm_id.ib = id; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2558 | |
| 2559 | route = &id_priv->id.route; |
| 2560 | ret = cma_format_hdr(private_data, id_priv->id.ps, route); |
| 2561 | if (ret) |
| 2562 | goto out; |
| 2563 | req.private_data = private_data; |
| 2564 | |
| 2565 | req.primary_path = &route->path_rec[0]; |
| 2566 | if (route->num_paths == 2) |
| 2567 | req.alternate_path = &route->path_rec[1]; |
| 2568 | |
| 2569 | req.service_id = cma_get_service_id(id_priv->id.ps, |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 2570 | (struct sockaddr *) &route->addr.dst_addr); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2571 | req.qp_num = id_priv->qp_num; |
Sean Hefty | 9b2e9c0 | 2006-11-30 16:30:47 -0800 | [diff] [blame] | 2572 | req.qp_type = IB_QPT_RC; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2573 | req.starting_psn = id_priv->seq_num; |
| 2574 | req.responder_resources = conn_param->responder_resources; |
| 2575 | req.initiator_depth = conn_param->initiator_depth; |
| 2576 | req.flow_control = conn_param->flow_control; |
| 2577 | req.retry_count = conn_param->retry_count; |
| 2578 | req.rnr_retry_count = conn_param->rnr_retry_count; |
| 2579 | req.remote_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; |
| 2580 | req.local_cm_response_timeout = CMA_CM_RESPONSE_TIMEOUT; |
| 2581 | req.max_cm_retries = CMA_MAX_CM_RETRIES; |
| 2582 | req.srq = id_priv->srq ? 1 : 0; |
| 2583 | |
| 2584 | ret = ib_send_cm_req(id_priv->cm_id.ib, &req); |
| 2585 | out: |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2586 | if (ret && !IS_ERR(id)) { |
| 2587 | ib_destroy_cm_id(id); |
Krishna Kumar | 675a027 | 2006-09-29 11:47:06 -0700 | [diff] [blame] | 2588 | id_priv->cm_id.ib = NULL; |
| 2589 | } |
| 2590 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2591 | kfree(private_data); |
| 2592 | return ret; |
| 2593 | } |
| 2594 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2595 | static int cma_connect_iw(struct rdma_id_private *id_priv, |
| 2596 | struct rdma_conn_param *conn_param) |
| 2597 | { |
| 2598 | struct iw_cm_id *cm_id; |
| 2599 | struct sockaddr_in* sin; |
| 2600 | int ret; |
| 2601 | struct iw_cm_conn_param iw_param; |
| 2602 | |
| 2603 | cm_id = iw_create_cm_id(id_priv->id.device, cma_iw_handler, id_priv); |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2604 | if (IS_ERR(cm_id)) |
| 2605 | return PTR_ERR(cm_id); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2606 | |
| 2607 | id_priv->cm_id.iw = cm_id; |
| 2608 | |
| 2609 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.src_addr; |
| 2610 | cm_id->local_addr = *sin; |
| 2611 | |
| 2612 | sin = (struct sockaddr_in*) &id_priv->id.route.addr.dst_addr; |
| 2613 | cm_id->remote_addr = *sin; |
| 2614 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 2615 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
Krishna Kumar | 675a027 | 2006-09-29 11:47:06 -0700 | [diff] [blame] | 2616 | if (ret) |
| 2617 | goto out; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2618 | |
| 2619 | iw_param.ord = conn_param->initiator_depth; |
| 2620 | iw_param.ird = conn_param->responder_resources; |
| 2621 | iw_param.private_data = conn_param->private_data; |
| 2622 | iw_param.private_data_len = conn_param->private_data_len; |
| 2623 | if (id_priv->id.qp) |
| 2624 | iw_param.qpn = id_priv->qp_num; |
| 2625 | else |
| 2626 | iw_param.qpn = conn_param->qp_num; |
| 2627 | ret = iw_cm_connect(cm_id, &iw_param); |
| 2628 | out: |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2629 | if (ret) { |
Krishna Kumar | 675a027 | 2006-09-29 11:47:06 -0700 | [diff] [blame] | 2630 | iw_destroy_cm_id(cm_id); |
| 2631 | id_priv->cm_id.iw = NULL; |
| 2632 | } |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2633 | return ret; |
| 2634 | } |
| 2635 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2636 | int rdma_connect(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
| 2637 | { |
| 2638 | struct rdma_id_private *id_priv; |
| 2639 | int ret; |
| 2640 | |
| 2641 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2642 | if (!cma_comp_exch(id_priv, RDMA_CM_ROUTE_RESOLVED, RDMA_CM_CONNECT)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2643 | return -EINVAL; |
| 2644 | |
| 2645 | if (!id->qp) { |
| 2646 | id_priv->qp_num = conn_param->qp_num; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2647 | id_priv->srq = conn_param->srq; |
| 2648 | } |
| 2649 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2650 | switch (rdma_node_get_transport(id->device->node_type)) { |
| 2651 | case RDMA_TRANSPORT_IB: |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 2652 | if (id->qp_type == IB_QPT_UD) |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2653 | ret = cma_resolve_ib_udp(id_priv, conn_param); |
| 2654 | else |
| 2655 | ret = cma_connect_ib(id_priv, conn_param); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2656 | break; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2657 | case RDMA_TRANSPORT_IWARP: |
| 2658 | ret = cma_connect_iw(id_priv, conn_param); |
| 2659 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2660 | default: |
| 2661 | ret = -ENOSYS; |
| 2662 | break; |
| 2663 | } |
| 2664 | if (ret) |
| 2665 | goto err; |
| 2666 | |
| 2667 | return 0; |
| 2668 | err: |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2669 | cma_comp_exch(id_priv, RDMA_CM_CONNECT, RDMA_CM_ROUTE_RESOLVED); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2670 | return ret; |
| 2671 | } |
| 2672 | EXPORT_SYMBOL(rdma_connect); |
| 2673 | |
| 2674 | static int cma_accept_ib(struct rdma_id_private *id_priv, |
| 2675 | struct rdma_conn_param *conn_param) |
| 2676 | { |
| 2677 | struct ib_cm_rep_param rep; |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 2678 | int ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2679 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 2680 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
| 2681 | if (ret) |
| 2682 | goto out; |
Sean Hefty | 0fe313b | 2006-11-30 16:37:15 -0800 | [diff] [blame] | 2683 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 2684 | ret = cma_modify_qp_rts(id_priv, conn_param); |
| 2685 | if (ret) |
| 2686 | goto out; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2687 | |
| 2688 | memset(&rep, 0, sizeof rep); |
| 2689 | rep.qp_num = id_priv->qp_num; |
| 2690 | rep.starting_psn = id_priv->seq_num; |
| 2691 | rep.private_data = conn_param->private_data; |
| 2692 | rep.private_data_len = conn_param->private_data_len; |
| 2693 | rep.responder_resources = conn_param->responder_resources; |
| 2694 | rep.initiator_depth = conn_param->initiator_depth; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2695 | rep.failover_accepted = 0; |
| 2696 | rep.flow_control = conn_param->flow_control; |
| 2697 | rep.rnr_retry_count = conn_param->rnr_retry_count; |
| 2698 | rep.srq = id_priv->srq ? 1 : 0; |
| 2699 | |
Sean Hefty | 0fe313b | 2006-11-30 16:37:15 -0800 | [diff] [blame] | 2700 | ret = ib_send_cm_rep(id_priv->cm_id.ib, &rep); |
| 2701 | out: |
| 2702 | return ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2705 | static int cma_accept_iw(struct rdma_id_private *id_priv, |
| 2706 | struct rdma_conn_param *conn_param) |
| 2707 | { |
| 2708 | struct iw_cm_conn_param iw_param; |
| 2709 | int ret; |
| 2710 | |
Sean Hefty | 5851bb8 | 2008-01-04 10:47:12 -0800 | [diff] [blame] | 2711 | ret = cma_modify_qp_rtr(id_priv, conn_param); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2712 | if (ret) |
| 2713 | return ret; |
| 2714 | |
| 2715 | iw_param.ord = conn_param->initiator_depth; |
| 2716 | iw_param.ird = conn_param->responder_resources; |
| 2717 | iw_param.private_data = conn_param->private_data; |
| 2718 | iw_param.private_data_len = conn_param->private_data_len; |
| 2719 | if (id_priv->id.qp) { |
| 2720 | iw_param.qpn = id_priv->qp_num; |
| 2721 | } else |
| 2722 | iw_param.qpn = conn_param->qp_num; |
| 2723 | |
| 2724 | return iw_cm_accept(id_priv->cm_id.iw, &iw_param); |
| 2725 | } |
| 2726 | |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2727 | static int cma_send_sidr_rep(struct rdma_id_private *id_priv, |
| 2728 | enum ib_cm_sidr_status status, |
| 2729 | const void *private_data, int private_data_len) |
| 2730 | { |
| 2731 | struct ib_cm_sidr_rep_param rep; |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 2732 | int ret; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2733 | |
| 2734 | memset(&rep, 0, sizeof rep); |
| 2735 | rep.status = status; |
| 2736 | if (status == IB_SIDR_SUCCESS) { |
Yossi Etigin | d2ca39f | 2009-04-08 13:42:33 -0700 | [diff] [blame] | 2737 | ret = cma_set_qkey(id_priv); |
| 2738 | if (ret) |
| 2739 | return ret; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2740 | rep.qp_num = id_priv->qp_num; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2741 | rep.qkey = id_priv->qkey; |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2742 | } |
| 2743 | rep.private_data = private_data; |
| 2744 | rep.private_data_len = private_data_len; |
| 2745 | |
| 2746 | return ib_send_cm_sidr_rep(id_priv->cm_id.ib, &rep); |
| 2747 | } |
| 2748 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2749 | int rdma_accept(struct rdma_cm_id *id, struct rdma_conn_param *conn_param) |
| 2750 | { |
| 2751 | struct rdma_id_private *id_priv; |
| 2752 | int ret; |
| 2753 | |
| 2754 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 83e9502 | 2011-01-13 13:56:04 +0000 | [diff] [blame] | 2755 | |
| 2756 | id_priv->owner = task_pid_nr(current); |
| 2757 | |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2758 | if (!cma_comp(id_priv, RDMA_CM_CONNECT)) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2759 | return -EINVAL; |
| 2760 | |
| 2761 | if (!id->qp && conn_param) { |
| 2762 | id_priv->qp_num = conn_param->qp_num; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2763 | id_priv->srq = conn_param->srq; |
| 2764 | } |
| 2765 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2766 | switch (rdma_node_get_transport(id->device->node_type)) { |
| 2767 | case RDMA_TRANSPORT_IB: |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 2768 | if (id->qp_type == IB_QPT_UD) |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2769 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_SUCCESS, |
| 2770 | conn_param->private_data, |
| 2771 | conn_param->private_data_len); |
| 2772 | else if (conn_param) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2773 | ret = cma_accept_ib(id_priv, conn_param); |
| 2774 | else |
| 2775 | ret = cma_rep_recv(id_priv); |
| 2776 | break; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2777 | case RDMA_TRANSPORT_IWARP: |
| 2778 | ret = cma_accept_iw(id_priv, conn_param); |
| 2779 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2780 | default: |
| 2781 | ret = -ENOSYS; |
| 2782 | break; |
| 2783 | } |
| 2784 | |
| 2785 | if (ret) |
| 2786 | goto reject; |
| 2787 | |
| 2788 | return 0; |
| 2789 | reject: |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 2790 | cma_modify_qp_err(id_priv); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2791 | rdma_reject(id, NULL, 0); |
| 2792 | return ret; |
| 2793 | } |
| 2794 | EXPORT_SYMBOL(rdma_accept); |
| 2795 | |
Sean Hefty | 0fe313b | 2006-11-30 16:37:15 -0800 | [diff] [blame] | 2796 | int rdma_notify(struct rdma_cm_id *id, enum ib_event_type event) |
| 2797 | { |
| 2798 | struct rdma_id_private *id_priv; |
| 2799 | int ret; |
| 2800 | |
| 2801 | id_priv = container_of(id, struct rdma_id_private, id); |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2802 | if (!id_priv->cm_id.ib) |
Sean Hefty | 0fe313b | 2006-11-30 16:37:15 -0800 | [diff] [blame] | 2803 | return -EINVAL; |
| 2804 | |
| 2805 | switch (id->device->node_type) { |
| 2806 | case RDMA_NODE_IB_CA: |
| 2807 | ret = ib_cm_notify(id_priv->cm_id.ib, event); |
| 2808 | break; |
| 2809 | default: |
| 2810 | ret = 0; |
| 2811 | break; |
| 2812 | } |
| 2813 | return ret; |
| 2814 | } |
| 2815 | EXPORT_SYMBOL(rdma_notify); |
| 2816 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2817 | int rdma_reject(struct rdma_cm_id *id, const void *private_data, |
| 2818 | u8 private_data_len) |
| 2819 | { |
| 2820 | struct rdma_id_private *id_priv; |
| 2821 | int ret; |
| 2822 | |
| 2823 | id_priv = container_of(id, struct rdma_id_private, id); |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2824 | if (!id_priv->cm_id.ib) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2825 | return -EINVAL; |
| 2826 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2827 | switch (rdma_node_get_transport(id->device->node_type)) { |
| 2828 | case RDMA_TRANSPORT_IB: |
Sean Hefty | b26f9b9 | 2010-04-01 17:08:41 +0000 | [diff] [blame] | 2829 | if (id->qp_type == IB_QPT_UD) |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 2830 | ret = cma_send_sidr_rep(id_priv, IB_SIDR_REJECT, |
| 2831 | private_data, private_data_len); |
| 2832 | else |
| 2833 | ret = ib_send_cm_rej(id_priv->cm_id.ib, |
| 2834 | IB_CM_REJ_CONSUMER_DEFINED, NULL, |
| 2835 | 0, private_data, private_data_len); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2836 | break; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2837 | case RDMA_TRANSPORT_IWARP: |
| 2838 | ret = iw_cm_reject(id_priv->cm_id.iw, |
| 2839 | private_data, private_data_len); |
| 2840 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2841 | default: |
| 2842 | ret = -ENOSYS; |
| 2843 | break; |
| 2844 | } |
| 2845 | return ret; |
| 2846 | } |
| 2847 | EXPORT_SYMBOL(rdma_reject); |
| 2848 | |
| 2849 | int rdma_disconnect(struct rdma_cm_id *id) |
| 2850 | { |
| 2851 | struct rdma_id_private *id_priv; |
| 2852 | int ret; |
| 2853 | |
| 2854 | id_priv = container_of(id, struct rdma_id_private, id); |
Jack Morgenstein | 0c9361f | 2011-07-17 10:46:47 +0000 | [diff] [blame] | 2855 | if (!id_priv->cm_id.ib) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2856 | return -EINVAL; |
| 2857 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2858 | switch (rdma_node_get_transport(id->device->node_type)) { |
| 2859 | case RDMA_TRANSPORT_IB: |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 2860 | ret = cma_modify_qp_err(id_priv); |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2861 | if (ret) |
| 2862 | goto out; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2863 | /* Initiate or respond to a disconnect. */ |
| 2864 | if (ib_send_cm_dreq(id_priv->cm_id.ib, NULL, 0)) |
| 2865 | ib_send_cm_drep(id_priv->cm_id.ib, NULL, 0); |
| 2866 | break; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2867 | case RDMA_TRANSPORT_IWARP: |
| 2868 | ret = iw_cm_disconnect(id_priv->cm_id.iw, 0); |
| 2869 | break; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2870 | default: |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 2871 | ret = -EINVAL; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 2872 | break; |
| 2873 | } |
| 2874 | out: |
| 2875 | return ret; |
| 2876 | } |
| 2877 | EXPORT_SYMBOL(rdma_disconnect); |
| 2878 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2879 | static int cma_ib_mc_handler(int status, struct ib_sa_multicast *multicast) |
| 2880 | { |
| 2881 | struct rdma_id_private *id_priv; |
| 2882 | struct cma_multicast *mc = multicast->context; |
| 2883 | struct rdma_cm_event event; |
| 2884 | int ret; |
| 2885 | |
| 2886 | id_priv = mc->id_priv; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2887 | if (cma_disable_callback(id_priv, RDMA_CM_ADDR_BOUND) && |
| 2888 | cma_disable_callback(id_priv, RDMA_CM_ADDR_RESOLVED)) |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 2889 | return 0; |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2890 | |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 2891 | mutex_lock(&id_priv->qp_mutex); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2892 | if (!status && id_priv->id.qp) |
| 2893 | status = ib_attach_mcast(id_priv->id.qp, &multicast->rec.mgid, |
| 2894 | multicast->rec.mlid); |
Sean Hefty | c548338 | 2007-09-24 13:19:09 -0700 | [diff] [blame] | 2895 | mutex_unlock(&id_priv->qp_mutex); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2896 | |
| 2897 | memset(&event, 0, sizeof event); |
| 2898 | event.status = status; |
| 2899 | event.param.ud.private_data = mc->context; |
| 2900 | if (!status) { |
| 2901 | event.event = RDMA_CM_EVENT_MULTICAST_JOIN; |
| 2902 | ib_init_ah_from_mcmember(id_priv->id.device, |
| 2903 | id_priv->id.port_num, &multicast->rec, |
| 2904 | &event.param.ud.ah_attr); |
| 2905 | event.param.ud.qp_num = 0xFFFFFF; |
| 2906 | event.param.ud.qkey = be32_to_cpu(multicast->rec.qkey); |
| 2907 | } else |
| 2908 | event.event = RDMA_CM_EVENT_MULTICAST_ERROR; |
| 2909 | |
| 2910 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
| 2911 | if (ret) { |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 2912 | cma_exch(id_priv, RDMA_CM_DESTROYING); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 2913 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2914 | rdma_destroy_id(&id_priv->id); |
| 2915 | return 0; |
| 2916 | } |
Sean Hefty | 8aa0860 | 2007-05-07 11:49:00 -0700 | [diff] [blame] | 2917 | |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 2918 | mutex_unlock(&id_priv->handler_mutex); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2919 | return 0; |
| 2920 | } |
| 2921 | |
| 2922 | static void cma_set_mgid(struct rdma_id_private *id_priv, |
| 2923 | struct sockaddr *addr, union ib_gid *mgid) |
| 2924 | { |
| 2925 | unsigned char mc_map[MAX_ADDR_LEN]; |
| 2926 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
| 2927 | struct sockaddr_in *sin = (struct sockaddr_in *) addr; |
| 2928 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) addr; |
| 2929 | |
| 2930 | if (cma_any_addr(addr)) { |
| 2931 | memset(mgid, 0, sizeof *mgid); |
| 2932 | } else if ((addr->sa_family == AF_INET6) && |
Jason Gunthorpe | 1c9b281 | 2009-11-19 12:55:21 -0800 | [diff] [blame] | 2933 | ((be32_to_cpu(sin6->sin6_addr.s6_addr32[0]) & 0xFFF0FFFF) == |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2934 | 0xFF10A01B)) { |
| 2935 | /* IPv6 address is an SA assigned MGID. */ |
| 2936 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); |
Jason Gunthorpe | e2e6269 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 2937 | } else if ((addr->sa_family == AF_INET6)) { |
| 2938 | ipv6_ib_mc_map(&sin6->sin6_addr, dev_addr->broadcast, mc_map); |
| 2939 | if (id_priv->id.ps == RDMA_PS_UDP) |
| 2940 | mc_map[7] = 0x01; /* Use RDMA CM signature */ |
| 2941 | *mgid = *(union ib_gid *) (mc_map + 4); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2942 | } else { |
Rolf Manderscheid | a9e527e | 2007-12-10 13:38:41 -0700 | [diff] [blame] | 2943 | ip_ib_mc_map(sin->sin_addr.s_addr, dev_addr->broadcast, mc_map); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2944 | if (id_priv->id.ps == RDMA_PS_UDP) |
| 2945 | mc_map[7] = 0x01; /* Use RDMA CM signature */ |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2946 | *mgid = *(union ib_gid *) (mc_map + 4); |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | static int cma_join_ib_multicast(struct rdma_id_private *id_priv, |
| 2951 | struct cma_multicast *mc) |
| 2952 | { |
| 2953 | struct ib_sa_mcmember_rec rec; |
| 2954 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
| 2955 | ib_sa_comp_mask comp_mask; |
| 2956 | int ret; |
| 2957 | |
| 2958 | ib_addr_get_mgid(dev_addr, &rec.mgid); |
| 2959 | ret = ib_sa_get_mcmember_rec(id_priv->id.device, id_priv->id.port_num, |
| 2960 | &rec.mgid, &rec); |
| 2961 | if (ret) |
| 2962 | return ret; |
| 2963 | |
Roland Dreier | 3f44675 | 2008-08-04 11:02:14 -0700 | [diff] [blame] | 2964 | cma_set_mgid(id_priv, (struct sockaddr *) &mc->addr, &rec.mgid); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2965 | if (id_priv->id.ps == RDMA_PS_UDP) |
| 2966 | rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); |
Sean Hefty | 6f8372b | 2009-11-19 13:26:06 -0800 | [diff] [blame] | 2967 | rdma_addr_get_sgid(dev_addr, &rec.port_gid); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2968 | rec.pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr)); |
| 2969 | rec.join_state = 1; |
| 2970 | |
| 2971 | comp_mask = IB_SA_MCMEMBER_REC_MGID | IB_SA_MCMEMBER_REC_PORT_GID | |
| 2972 | IB_SA_MCMEMBER_REC_PKEY | IB_SA_MCMEMBER_REC_JOIN_STATE | |
| 2973 | IB_SA_MCMEMBER_REC_QKEY | IB_SA_MCMEMBER_REC_SL | |
| 2974 | IB_SA_MCMEMBER_REC_FLOW_LABEL | |
| 2975 | IB_SA_MCMEMBER_REC_TRAFFIC_CLASS; |
| 2976 | |
Yossi Etigin | 84adeee | 2009-04-01 13:55:32 -0700 | [diff] [blame] | 2977 | if (id_priv->id.ps == RDMA_PS_IPOIB) |
| 2978 | comp_mask |= IB_SA_MCMEMBER_REC_RATE | |
| 2979 | IB_SA_MCMEMBER_REC_RATE_SELECTOR; |
| 2980 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 2981 | mc->multicast.ib = ib_sa_join_multicast(&sa_client, id_priv->id.device, |
| 2982 | id_priv->id.port_num, &rec, |
| 2983 | comp_mask, GFP_KERNEL, |
| 2984 | cma_ib_mc_handler, mc); |
| 2985 | if (IS_ERR(mc->multicast.ib)) |
| 2986 | return PTR_ERR(mc->multicast.ib); |
| 2987 | |
| 2988 | return 0; |
| 2989 | } |
| 2990 | |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 2991 | static void iboe_mcast_work_handler(struct work_struct *work) |
| 2992 | { |
| 2993 | struct iboe_mcast_work *mw = container_of(work, struct iboe_mcast_work, work); |
| 2994 | struct cma_multicast *mc = mw->mc; |
| 2995 | struct ib_sa_multicast *m = mc->multicast.ib; |
| 2996 | |
| 2997 | mc->multicast.ib->context = mc; |
| 2998 | cma_ib_mc_handler(0, m); |
| 2999 | kref_put(&mc->mcref, release_mc); |
| 3000 | kfree(mw); |
| 3001 | } |
| 3002 | |
| 3003 | static void cma_iboe_set_mgid(struct sockaddr *addr, union ib_gid *mgid) |
| 3004 | { |
| 3005 | struct sockaddr_in *sin = (struct sockaddr_in *)addr; |
| 3006 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr; |
| 3007 | |
| 3008 | if (cma_any_addr(addr)) { |
| 3009 | memset(mgid, 0, sizeof *mgid); |
| 3010 | } else if (addr->sa_family == AF_INET6) { |
| 3011 | memcpy(mgid, &sin6->sin6_addr, sizeof *mgid); |
| 3012 | } else { |
| 3013 | mgid->raw[0] = 0xff; |
| 3014 | mgid->raw[1] = 0x0e; |
| 3015 | mgid->raw[2] = 0; |
| 3016 | mgid->raw[3] = 0; |
| 3017 | mgid->raw[4] = 0; |
| 3018 | mgid->raw[5] = 0; |
| 3019 | mgid->raw[6] = 0; |
| 3020 | mgid->raw[7] = 0; |
| 3021 | mgid->raw[8] = 0; |
| 3022 | mgid->raw[9] = 0; |
| 3023 | mgid->raw[10] = 0xff; |
| 3024 | mgid->raw[11] = 0xff; |
| 3025 | *(__be32 *)(&mgid->raw[12]) = sin->sin_addr.s_addr; |
| 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | static int cma_iboe_join_multicast(struct rdma_id_private *id_priv, |
| 3030 | struct cma_multicast *mc) |
| 3031 | { |
| 3032 | struct iboe_mcast_work *work; |
| 3033 | struct rdma_dev_addr *dev_addr = &id_priv->id.route.addr.dev_addr; |
| 3034 | int err; |
| 3035 | struct sockaddr *addr = (struct sockaddr *)&mc->addr; |
| 3036 | struct net_device *ndev = NULL; |
| 3037 | |
| 3038 | if (cma_zero_addr((struct sockaddr *)&mc->addr)) |
| 3039 | return -EINVAL; |
| 3040 | |
| 3041 | work = kzalloc(sizeof *work, GFP_KERNEL); |
| 3042 | if (!work) |
| 3043 | return -ENOMEM; |
| 3044 | |
| 3045 | mc->multicast.ib = kzalloc(sizeof(struct ib_sa_multicast), GFP_KERNEL); |
| 3046 | if (!mc->multicast.ib) { |
| 3047 | err = -ENOMEM; |
| 3048 | goto out1; |
| 3049 | } |
| 3050 | |
| 3051 | cma_iboe_set_mgid(addr, &mc->multicast.ib->rec.mgid); |
| 3052 | |
| 3053 | mc->multicast.ib->rec.pkey = cpu_to_be16(0xffff); |
| 3054 | if (id_priv->id.ps == RDMA_PS_UDP) |
| 3055 | mc->multicast.ib->rec.qkey = cpu_to_be32(RDMA_UDP_QKEY); |
| 3056 | |
| 3057 | if (dev_addr->bound_dev_if) |
| 3058 | ndev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); |
| 3059 | if (!ndev) { |
| 3060 | err = -ENODEV; |
| 3061 | goto out2; |
| 3062 | } |
| 3063 | mc->multicast.ib->rec.rate = iboe_get_rate(ndev); |
| 3064 | mc->multicast.ib->rec.hop_limit = 1; |
| 3065 | mc->multicast.ib->rec.mtu = iboe_get_mtu(ndev->mtu); |
| 3066 | dev_put(ndev); |
| 3067 | if (!mc->multicast.ib->rec.mtu) { |
| 3068 | err = -EINVAL; |
| 3069 | goto out2; |
| 3070 | } |
| 3071 | iboe_addr_get_sgid(dev_addr, &mc->multicast.ib->rec.port_gid); |
| 3072 | work->id = id_priv; |
| 3073 | work->mc = mc; |
| 3074 | INIT_WORK(&work->work, iboe_mcast_work_handler); |
| 3075 | kref_get(&mc->mcref); |
| 3076 | queue_work(cma_wq, &work->work); |
| 3077 | |
| 3078 | return 0; |
| 3079 | |
| 3080 | out2: |
| 3081 | kfree(mc->multicast.ib); |
| 3082 | out1: |
| 3083 | kfree(work); |
| 3084 | return err; |
| 3085 | } |
| 3086 | |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 3087 | int rdma_join_multicast(struct rdma_cm_id *id, struct sockaddr *addr, |
| 3088 | void *context) |
| 3089 | { |
| 3090 | struct rdma_id_private *id_priv; |
| 3091 | struct cma_multicast *mc; |
| 3092 | int ret; |
| 3093 | |
| 3094 | id_priv = container_of(id, struct rdma_id_private, id); |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 3095 | if (!cma_comp(id_priv, RDMA_CM_ADDR_BOUND) && |
| 3096 | !cma_comp(id_priv, RDMA_CM_ADDR_RESOLVED)) |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 3097 | return -EINVAL; |
| 3098 | |
| 3099 | mc = kmalloc(sizeof *mc, GFP_KERNEL); |
| 3100 | if (!mc) |
| 3101 | return -ENOMEM; |
| 3102 | |
| 3103 | memcpy(&mc->addr, addr, ip_addr_size(addr)); |
| 3104 | mc->context = context; |
| 3105 | mc->id_priv = id_priv; |
| 3106 | |
| 3107 | spin_lock(&id_priv->lock); |
| 3108 | list_add(&mc->list, &id_priv->mc_list); |
| 3109 | spin_unlock(&id_priv->lock); |
| 3110 | |
| 3111 | switch (rdma_node_get_transport(id->device->node_type)) { |
| 3112 | case RDMA_TRANSPORT_IB: |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 3113 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { |
| 3114 | case IB_LINK_LAYER_INFINIBAND: |
| 3115 | ret = cma_join_ib_multicast(id_priv, mc); |
| 3116 | break; |
| 3117 | case IB_LINK_LAYER_ETHERNET: |
| 3118 | kref_init(&mc->mcref); |
| 3119 | ret = cma_iboe_join_multicast(id_priv, mc); |
| 3120 | break; |
| 3121 | default: |
| 3122 | ret = -EINVAL; |
| 3123 | } |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 3124 | break; |
| 3125 | default: |
| 3126 | ret = -ENOSYS; |
| 3127 | break; |
| 3128 | } |
| 3129 | |
| 3130 | if (ret) { |
| 3131 | spin_lock_irq(&id_priv->lock); |
| 3132 | list_del(&mc->list); |
| 3133 | spin_unlock_irq(&id_priv->lock); |
| 3134 | kfree(mc); |
| 3135 | } |
| 3136 | return ret; |
| 3137 | } |
| 3138 | EXPORT_SYMBOL(rdma_join_multicast); |
| 3139 | |
| 3140 | void rdma_leave_multicast(struct rdma_cm_id *id, struct sockaddr *addr) |
| 3141 | { |
| 3142 | struct rdma_id_private *id_priv; |
| 3143 | struct cma_multicast *mc; |
| 3144 | |
| 3145 | id_priv = container_of(id, struct rdma_id_private, id); |
| 3146 | spin_lock_irq(&id_priv->lock); |
| 3147 | list_for_each_entry(mc, &id_priv->mc_list, list) { |
| 3148 | if (!memcmp(&mc->addr, addr, ip_addr_size(addr))) { |
| 3149 | list_del(&mc->list); |
| 3150 | spin_unlock_irq(&id_priv->lock); |
| 3151 | |
| 3152 | if (id->qp) |
| 3153 | ib_detach_mcast(id->qp, |
| 3154 | &mc->multicast.ib->rec.mgid, |
| 3155 | mc->multicast.ib->rec.mlid); |
Eli Cohen | 3c86aa7 | 2010-10-13 21:26:51 +0200 | [diff] [blame] | 3156 | if (rdma_node_get_transport(id_priv->cma_dev->device->node_type) == RDMA_TRANSPORT_IB) { |
| 3157 | switch (rdma_port_get_link_layer(id->device, id->port_num)) { |
| 3158 | case IB_LINK_LAYER_INFINIBAND: |
| 3159 | ib_sa_free_multicast(mc->multicast.ib); |
| 3160 | kfree(mc); |
| 3161 | break; |
| 3162 | case IB_LINK_LAYER_ETHERNET: |
| 3163 | kref_put(&mc->mcref, release_mc); |
| 3164 | break; |
| 3165 | default: |
| 3166 | break; |
| 3167 | } |
| 3168 | } |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 3169 | return; |
| 3170 | } |
| 3171 | } |
| 3172 | spin_unlock_irq(&id_priv->lock); |
| 3173 | } |
| 3174 | EXPORT_SYMBOL(rdma_leave_multicast); |
| 3175 | |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 3176 | static int cma_netdev_change(struct net_device *ndev, struct rdma_id_private *id_priv) |
| 3177 | { |
| 3178 | struct rdma_dev_addr *dev_addr; |
| 3179 | struct cma_ndev_work *work; |
| 3180 | |
| 3181 | dev_addr = &id_priv->id.route.addr.dev_addr; |
| 3182 | |
Sean Hefty | 6266ed6 | 2009-11-19 12:55:22 -0800 | [diff] [blame] | 3183 | if ((dev_addr->bound_dev_if == ndev->ifindex) && |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 3184 | memcmp(dev_addr->src_dev_addr, ndev->dev_addr, ndev->addr_len)) { |
| 3185 | printk(KERN_INFO "RDMA CM addr change for ndev %s used by id %p\n", |
| 3186 | ndev->name, &id_priv->id); |
| 3187 | work = kzalloc(sizeof *work, GFP_KERNEL); |
| 3188 | if (!work) |
| 3189 | return -ENOMEM; |
| 3190 | |
| 3191 | INIT_WORK(&work->work, cma_ndev_work_handler); |
| 3192 | work->id = id_priv; |
| 3193 | work->event.event = RDMA_CM_EVENT_ADDR_CHANGE; |
| 3194 | atomic_inc(&id_priv->refcount); |
| 3195 | queue_work(cma_wq, &work->work); |
| 3196 | } |
| 3197 | |
| 3198 | return 0; |
| 3199 | } |
| 3200 | |
| 3201 | static int cma_netdev_callback(struct notifier_block *self, unsigned long event, |
| 3202 | void *ctx) |
| 3203 | { |
| 3204 | struct net_device *ndev = (struct net_device *)ctx; |
| 3205 | struct cma_device *cma_dev; |
| 3206 | struct rdma_id_private *id_priv; |
| 3207 | int ret = NOTIFY_DONE; |
| 3208 | |
| 3209 | if (dev_net(ndev) != &init_net) |
| 3210 | return NOTIFY_DONE; |
| 3211 | |
| 3212 | if (event != NETDEV_BONDING_FAILOVER) |
| 3213 | return NOTIFY_DONE; |
| 3214 | |
| 3215 | if (!(ndev->flags & IFF_MASTER) || !(ndev->priv_flags & IFF_BONDING)) |
| 3216 | return NOTIFY_DONE; |
| 3217 | |
| 3218 | mutex_lock(&lock); |
| 3219 | list_for_each_entry(cma_dev, &dev_list, list) |
| 3220 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { |
| 3221 | ret = cma_netdev_change(ndev, id_priv); |
| 3222 | if (ret) |
| 3223 | goto out; |
| 3224 | } |
| 3225 | |
| 3226 | out: |
| 3227 | mutex_unlock(&lock); |
| 3228 | return ret; |
| 3229 | } |
| 3230 | |
| 3231 | static struct notifier_block cma_nb = { |
| 3232 | .notifier_call = cma_netdev_callback |
| 3233 | }; |
| 3234 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3235 | static void cma_add_one(struct ib_device *device) |
| 3236 | { |
| 3237 | struct cma_device *cma_dev; |
| 3238 | struct rdma_id_private *id_priv; |
| 3239 | |
| 3240 | cma_dev = kmalloc(sizeof *cma_dev, GFP_KERNEL); |
| 3241 | if (!cma_dev) |
| 3242 | return; |
| 3243 | |
| 3244 | cma_dev->device = device; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3245 | |
| 3246 | init_completion(&cma_dev->comp); |
| 3247 | atomic_set(&cma_dev->refcount, 1); |
| 3248 | INIT_LIST_HEAD(&cma_dev->id_list); |
| 3249 | ib_set_client_data(device, &cma_client, cma_dev); |
| 3250 | |
| 3251 | mutex_lock(&lock); |
| 3252 | list_add_tail(&cma_dev->list, &dev_list); |
| 3253 | list_for_each_entry(id_priv, &listen_any_list, list) |
| 3254 | cma_listen_on_dev(id_priv, cma_dev); |
| 3255 | mutex_unlock(&lock); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | static int cma_remove_id_dev(struct rdma_id_private *id_priv) |
| 3259 | { |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 3260 | struct rdma_cm_event event; |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 3261 | enum rdma_cm_state state; |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 3262 | int ret = 0; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3263 | |
| 3264 | /* Record that we want to remove the device */ |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 3265 | state = cma_exch(id_priv, RDMA_CM_DEVICE_REMOVAL); |
| 3266 | if (state == RDMA_CM_DESTROYING) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3267 | return 0; |
| 3268 | |
| 3269 | cma_cancel_operation(id_priv, state); |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 3270 | mutex_lock(&id_priv->handler_mutex); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3271 | |
| 3272 | /* Check for destruction from another callback. */ |
Nir Muchtar | 550e5ca | 2011-05-20 11:46:11 -0700 | [diff] [blame] | 3273 | if (!cma_comp(id_priv, RDMA_CM_DEVICE_REMOVAL)) |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 3274 | goto out; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3275 | |
Sean Hefty | a1b1b61 | 2006-11-30 16:33:14 -0800 | [diff] [blame] | 3276 | memset(&event, 0, sizeof event); |
| 3277 | event.event = RDMA_CM_EVENT_DEVICE_REMOVAL; |
Or Gerlitz | de910bd | 2008-07-14 23:48:53 -0700 | [diff] [blame] | 3278 | ret = id_priv->id.event_handler(&id_priv->id, &event); |
| 3279 | out: |
| 3280 | mutex_unlock(&id_priv->handler_mutex); |
| 3281 | return ret; |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | static void cma_process_remove(struct cma_device *cma_dev) |
| 3285 | { |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3286 | struct rdma_id_private *id_priv; |
| 3287 | int ret; |
| 3288 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3289 | mutex_lock(&lock); |
| 3290 | while (!list_empty(&cma_dev->id_list)) { |
| 3291 | id_priv = list_entry(cma_dev->id_list.next, |
| 3292 | struct rdma_id_private, list); |
| 3293 | |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 3294 | list_del(&id_priv->listen_list); |
Krishna Kumar | 94de178 | 2006-09-29 12:03:35 -0700 | [diff] [blame] | 3295 | list_del_init(&id_priv->list); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3296 | atomic_inc(&id_priv->refcount); |
| 3297 | mutex_unlock(&lock); |
| 3298 | |
Sean Hefty | d02d1f5 | 2007-10-09 11:12:34 -0700 | [diff] [blame] | 3299 | ret = id_priv->internal_id ? 1 : cma_remove_id_dev(id_priv); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3300 | cma_deref_id(id_priv); |
| 3301 | if (ret) |
| 3302 | rdma_destroy_id(&id_priv->id); |
| 3303 | |
| 3304 | mutex_lock(&lock); |
| 3305 | } |
| 3306 | mutex_unlock(&lock); |
| 3307 | |
| 3308 | cma_deref_dev(cma_dev); |
| 3309 | wait_for_completion(&cma_dev->comp); |
| 3310 | } |
| 3311 | |
| 3312 | static void cma_remove_one(struct ib_device *device) |
| 3313 | { |
| 3314 | struct cma_device *cma_dev; |
| 3315 | |
| 3316 | cma_dev = ib_get_client_data(device, &cma_client); |
| 3317 | if (!cma_dev) |
| 3318 | return; |
| 3319 | |
| 3320 | mutex_lock(&lock); |
| 3321 | list_del(&cma_dev->list); |
| 3322 | mutex_unlock(&lock); |
| 3323 | |
| 3324 | cma_process_remove(cma_dev); |
| 3325 | kfree(cma_dev); |
| 3326 | } |
| 3327 | |
Nir Muchtar | 753f618 | 2011-01-03 15:33:53 +0000 | [diff] [blame] | 3328 | static int cma_get_id_stats(struct sk_buff *skb, struct netlink_callback *cb) |
| 3329 | { |
| 3330 | struct nlmsghdr *nlh; |
| 3331 | struct rdma_cm_id_stats *id_stats; |
| 3332 | struct rdma_id_private *id_priv; |
| 3333 | struct rdma_cm_id *id = NULL; |
| 3334 | struct cma_device *cma_dev; |
| 3335 | int i_dev = 0, i_id = 0; |
| 3336 | |
| 3337 | /* |
| 3338 | * We export all of the IDs as a sequence of messages. Each |
| 3339 | * ID gets its own netlink message. |
| 3340 | */ |
| 3341 | mutex_lock(&lock); |
| 3342 | |
| 3343 | list_for_each_entry(cma_dev, &dev_list, list) { |
| 3344 | if (i_dev < cb->args[0]) { |
| 3345 | i_dev++; |
| 3346 | continue; |
| 3347 | } |
| 3348 | |
| 3349 | i_id = 0; |
| 3350 | list_for_each_entry(id_priv, &cma_dev->id_list, list) { |
| 3351 | if (i_id < cb->args[1]) { |
| 3352 | i_id++; |
| 3353 | continue; |
| 3354 | } |
| 3355 | |
| 3356 | id_stats = ibnl_put_msg(skb, &nlh, cb->nlh->nlmsg_seq, |
| 3357 | sizeof *id_stats, RDMA_NL_RDMA_CM, |
| 3358 | RDMA_NL_RDMA_CM_ID_STATS); |
| 3359 | if (!id_stats) |
| 3360 | goto out; |
| 3361 | |
| 3362 | memset(id_stats, 0, sizeof *id_stats); |
| 3363 | id = &id_priv->id; |
| 3364 | id_stats->node_type = id->route.addr.dev_addr.dev_type; |
| 3365 | id_stats->port_num = id->port_num; |
| 3366 | id_stats->bound_dev_if = |
| 3367 | id->route.addr.dev_addr.bound_dev_if; |
| 3368 | |
| 3369 | if (id->route.addr.src_addr.ss_family == AF_INET) { |
| 3370 | if (ibnl_put_attr(skb, nlh, |
| 3371 | sizeof(struct sockaddr_in), |
| 3372 | &id->route.addr.src_addr, |
| 3373 | RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) { |
| 3374 | goto out; |
| 3375 | } |
| 3376 | if (ibnl_put_attr(skb, nlh, |
| 3377 | sizeof(struct sockaddr_in), |
| 3378 | &id->route.addr.dst_addr, |
| 3379 | RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) { |
| 3380 | goto out; |
| 3381 | } |
| 3382 | } else if (id->route.addr.src_addr.ss_family == AF_INET6) { |
| 3383 | if (ibnl_put_attr(skb, nlh, |
| 3384 | sizeof(struct sockaddr_in6), |
| 3385 | &id->route.addr.src_addr, |
| 3386 | RDMA_NL_RDMA_CM_ATTR_SRC_ADDR)) { |
| 3387 | goto out; |
| 3388 | } |
| 3389 | if (ibnl_put_attr(skb, nlh, |
| 3390 | sizeof(struct sockaddr_in6), |
| 3391 | &id->route.addr.dst_addr, |
| 3392 | RDMA_NL_RDMA_CM_ATTR_DST_ADDR)) { |
| 3393 | goto out; |
| 3394 | } |
| 3395 | } |
| 3396 | |
Nir Muchtar | 83e9502 | 2011-01-13 13:56:04 +0000 | [diff] [blame] | 3397 | id_stats->pid = id_priv->owner; |
Nir Muchtar | 753f618 | 2011-01-03 15:33:53 +0000 | [diff] [blame] | 3398 | id_stats->port_space = id->ps; |
| 3399 | id_stats->cm_state = id_priv->state; |
| 3400 | id_stats->qp_num = id_priv->qp_num; |
| 3401 | id_stats->qp_type = id->qp_type; |
| 3402 | |
| 3403 | i_id++; |
| 3404 | } |
| 3405 | |
| 3406 | cb->args[1] = 0; |
| 3407 | i_dev++; |
| 3408 | } |
| 3409 | |
| 3410 | out: |
| 3411 | mutex_unlock(&lock); |
| 3412 | cb->args[0] = i_dev; |
| 3413 | cb->args[1] = i_id; |
| 3414 | |
| 3415 | return skb->len; |
| 3416 | } |
| 3417 | |
| 3418 | static const struct ibnl_client_cbs cma_cb_table[] = { |
| 3419 | [RDMA_NL_RDMA_CM_ID_STATS] = { .dump = cma_get_id_stats }, |
| 3420 | }; |
| 3421 | |
Peter Huewe | 716abb1 | 2009-06-23 10:38:42 -0700 | [diff] [blame] | 3422 | static int __init cma_init(void) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3423 | { |
Tetsuo Handa | 5d7220e8 | 2010-04-15 11:29:04 +0900 | [diff] [blame] | 3424 | int ret; |
Stephen Hemminger | 227b60f | 2007-10-10 17:30:46 -0700 | [diff] [blame] | 3425 | |
Sean Hefty | c7f743a | 2007-02-01 12:23:37 -0800 | [diff] [blame] | 3426 | cma_wq = create_singlethread_workqueue("rdma_cm"); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3427 | if (!cma_wq) |
| 3428 | return -ENOMEM; |
| 3429 | |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 3430 | ib_sa_register_client(&sa_client); |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 3431 | rdma_addr_register_client(&addr_client); |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 3432 | register_netdevice_notifier(&cma_nb); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 3433 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3434 | ret = ib_register_client(&cma_client); |
| 3435 | if (ret) |
| 3436 | goto err; |
Nir Muchtar | 753f618 | 2011-01-03 15:33:53 +0000 | [diff] [blame] | 3437 | |
| 3438 | if (ibnl_add_client(RDMA_NL_RDMA_CM, RDMA_NL_RDMA_CM_NUM_OPS, cma_cb_table)) |
| 3439 | printk(KERN_WARNING "RDMA CMA: failed to add netlink callback\n"); |
| 3440 | |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3441 | return 0; |
| 3442 | |
| 3443 | err: |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 3444 | unregister_netdevice_notifier(&cma_nb); |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 3445 | rdma_addr_unregister_client(&addr_client); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 3446 | ib_sa_unregister_client(&sa_client); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3447 | destroy_workqueue(cma_wq); |
| 3448 | return ret; |
| 3449 | } |
| 3450 | |
Peter Huewe | 716abb1 | 2009-06-23 10:38:42 -0700 | [diff] [blame] | 3451 | static void __exit cma_cleanup(void) |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3452 | { |
Nir Muchtar | 753f618 | 2011-01-03 15:33:53 +0000 | [diff] [blame] | 3453 | ibnl_remove_client(RDMA_NL_RDMA_CM); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3454 | ib_unregister_client(&cma_client); |
Or Gerlitz | dd5bdff | 2008-07-22 14:14:22 -0700 | [diff] [blame] | 3455 | unregister_netdevice_notifier(&cma_nb); |
Sean Hefty | 7a118df | 2006-10-31 11:12:59 -0800 | [diff] [blame] | 3456 | rdma_addr_unregister_client(&addr_client); |
Michael S. Tsirkin | c1a0b23 | 2006-08-21 16:40:12 -0700 | [diff] [blame] | 3457 | ib_sa_unregister_client(&sa_client); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3458 | destroy_workqueue(cma_wq); |
| 3459 | idr_destroy(&sdp_ps); |
| 3460 | idr_destroy(&tcp_ps); |
Sean Hefty | 628e5f6 | 2006-11-30 16:44:16 -0800 | [diff] [blame] | 3461 | idr_destroy(&udp_ps); |
Sean Hefty | c8f6a36 | 2007-02-15 17:00:18 -0800 | [diff] [blame] | 3462 | idr_destroy(&ipoib_ps); |
Sean Hefty | e51060f | 2006-06-17 20:37:29 -0700 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | module_init(cma_init); |
| 3466 | module_exit(cma_cleanup); |