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