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