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