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