Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved. |
| 3 | * Copyright (c) 2004 Infinicon Corporation. All rights reserved. |
| 4 | * Copyright (c) 2004 Intel Corporation. All rights reserved. |
| 5 | * Copyright (c) 2004 Topspin Corporation. All rights reserved. |
| 6 | * Copyright (c) 2004 Voltaire Corporation. All rights reserved. |
Roland Dreier | 2a1d9b7 | 2005-08-10 23:03:10 -0700 | [diff] [blame] | 7 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
Roland Dreier | 33b9b3e | 2006-01-30 14:29:21 -0800 | [diff] [blame] | 8 | * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * This software is available to you under a choice of one of two |
| 11 | * licenses. You may choose to be licensed under the terms of the GNU |
| 12 | * General Public License (GPL) Version 2, available from the file |
| 13 | * COPYING in the main directory of this source tree, or the |
| 14 | * OpenIB.org BSD license below: |
| 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or |
| 17 | * without modification, are permitted provided that the following |
| 18 | * conditions are met: |
| 19 | * |
| 20 | * - Redistributions of source code must retain the above |
| 21 | * copyright notice, this list of conditions and the following |
| 22 | * disclaimer. |
| 23 | * |
| 24 | * - Redistributions in binary form must reproduce the above |
| 25 | * copyright notice, this list of conditions and the following |
| 26 | * disclaimer in the documentation and/or other materials |
| 27 | * provided with the distribution. |
| 28 | * |
| 29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 33 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 34 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 35 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | * SOFTWARE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | #include <linux/errno.h> |
| 40 | #include <linux/err.h> |
Paul Gortmaker | b108d97 | 2011-05-27 15:29:33 -0400 | [diff] [blame] | 41 | #include <linux/export.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 43 | #include <linux/slab.h> |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 44 | #include <linux/in.h> |
| 45 | #include <linux/in6.h> |
| 46 | #include <net/addrconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 48 | #include <rdma/ib_verbs.h> |
| 49 | #include <rdma/ib_cache.h> |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 50 | #include <rdma/ib_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 52 | #include "core_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Sagi Grimberg | 2b1b5b6 | 2015-05-18 13:40:28 +0300 | [diff] [blame] | 54 | static const char * const ib_events[] = { |
| 55 | [IB_EVENT_CQ_ERR] = "CQ error", |
| 56 | [IB_EVENT_QP_FATAL] = "QP fatal error", |
| 57 | [IB_EVENT_QP_REQ_ERR] = "QP request error", |
| 58 | [IB_EVENT_QP_ACCESS_ERR] = "QP access error", |
| 59 | [IB_EVENT_COMM_EST] = "communication established", |
| 60 | [IB_EVENT_SQ_DRAINED] = "send queue drained", |
| 61 | [IB_EVENT_PATH_MIG] = "path migration successful", |
| 62 | [IB_EVENT_PATH_MIG_ERR] = "path migration error", |
| 63 | [IB_EVENT_DEVICE_FATAL] = "device fatal error", |
| 64 | [IB_EVENT_PORT_ACTIVE] = "port active", |
| 65 | [IB_EVENT_PORT_ERR] = "port error", |
| 66 | [IB_EVENT_LID_CHANGE] = "LID change", |
| 67 | [IB_EVENT_PKEY_CHANGE] = "P_key change", |
| 68 | [IB_EVENT_SM_CHANGE] = "SM change", |
| 69 | [IB_EVENT_SRQ_ERR] = "SRQ error", |
| 70 | [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached", |
| 71 | [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached", |
| 72 | [IB_EVENT_CLIENT_REREGISTER] = "client reregister", |
| 73 | [IB_EVENT_GID_CHANGE] = "GID changed", |
| 74 | }; |
| 75 | |
Bart Van Assche | db7489e | 2015-08-03 10:01:52 -0700 | [diff] [blame] | 76 | const char *__attribute_const__ ib_event_msg(enum ib_event_type event) |
Sagi Grimberg | 2b1b5b6 | 2015-05-18 13:40:28 +0300 | [diff] [blame] | 77 | { |
| 78 | size_t index = event; |
| 79 | |
| 80 | return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ? |
| 81 | ib_events[index] : "unrecognized event"; |
| 82 | } |
| 83 | EXPORT_SYMBOL(ib_event_msg); |
| 84 | |
| 85 | static const char * const wc_statuses[] = { |
| 86 | [IB_WC_SUCCESS] = "success", |
| 87 | [IB_WC_LOC_LEN_ERR] = "local length error", |
| 88 | [IB_WC_LOC_QP_OP_ERR] = "local QP operation error", |
| 89 | [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error", |
| 90 | [IB_WC_LOC_PROT_ERR] = "local protection error", |
| 91 | [IB_WC_WR_FLUSH_ERR] = "WR flushed", |
| 92 | [IB_WC_MW_BIND_ERR] = "memory management operation error", |
| 93 | [IB_WC_BAD_RESP_ERR] = "bad response error", |
| 94 | [IB_WC_LOC_ACCESS_ERR] = "local access error", |
| 95 | [IB_WC_REM_INV_REQ_ERR] = "invalid request error", |
| 96 | [IB_WC_REM_ACCESS_ERR] = "remote access error", |
| 97 | [IB_WC_REM_OP_ERR] = "remote operation error", |
| 98 | [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded", |
| 99 | [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded", |
| 100 | [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error", |
| 101 | [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request", |
| 102 | [IB_WC_REM_ABORT_ERR] = "operation aborted", |
| 103 | [IB_WC_INV_EECN_ERR] = "invalid EE context number", |
| 104 | [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state", |
| 105 | [IB_WC_FATAL_ERR] = "fatal error", |
| 106 | [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error", |
| 107 | [IB_WC_GENERAL_ERR] = "general error", |
| 108 | }; |
| 109 | |
Bart Van Assche | db7489e | 2015-08-03 10:01:52 -0700 | [diff] [blame] | 110 | const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status) |
Sagi Grimberg | 2b1b5b6 | 2015-05-18 13:40:28 +0300 | [diff] [blame] | 111 | { |
| 112 | size_t index = status; |
| 113 | |
| 114 | return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ? |
| 115 | wc_statuses[index] : "unrecognized status"; |
| 116 | } |
| 117 | EXPORT_SYMBOL(ib_wc_status_msg); |
| 118 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 119 | __attribute_const__ int ib_rate_to_mult(enum ib_rate rate) |
Jack Morgenstein | bf6a9e3 | 2006-04-10 09:43:47 -0700 | [diff] [blame] | 120 | { |
| 121 | switch (rate) { |
| 122 | case IB_RATE_2_5_GBPS: return 1; |
| 123 | case IB_RATE_5_GBPS: return 2; |
| 124 | case IB_RATE_10_GBPS: return 4; |
| 125 | case IB_RATE_20_GBPS: return 8; |
| 126 | case IB_RATE_30_GBPS: return 12; |
| 127 | case IB_RATE_40_GBPS: return 16; |
| 128 | case IB_RATE_60_GBPS: return 24; |
| 129 | case IB_RATE_80_GBPS: return 32; |
| 130 | case IB_RATE_120_GBPS: return 48; |
| 131 | default: return -1; |
| 132 | } |
| 133 | } |
| 134 | EXPORT_SYMBOL(ib_rate_to_mult); |
| 135 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 136 | __attribute_const__ enum ib_rate mult_to_ib_rate(int mult) |
Jack Morgenstein | bf6a9e3 | 2006-04-10 09:43:47 -0700 | [diff] [blame] | 137 | { |
| 138 | switch (mult) { |
| 139 | case 1: return IB_RATE_2_5_GBPS; |
| 140 | case 2: return IB_RATE_5_GBPS; |
| 141 | case 4: return IB_RATE_10_GBPS; |
| 142 | case 8: return IB_RATE_20_GBPS; |
| 143 | case 12: return IB_RATE_30_GBPS; |
| 144 | case 16: return IB_RATE_40_GBPS; |
| 145 | case 24: return IB_RATE_60_GBPS; |
| 146 | case 32: return IB_RATE_80_GBPS; |
| 147 | case 48: return IB_RATE_120_GBPS; |
| 148 | default: return IB_RATE_PORT_CURRENT; |
| 149 | } |
| 150 | } |
| 151 | EXPORT_SYMBOL(mult_to_ib_rate); |
| 152 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 153 | __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate) |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 154 | { |
| 155 | switch (rate) { |
| 156 | case IB_RATE_2_5_GBPS: return 2500; |
| 157 | case IB_RATE_5_GBPS: return 5000; |
| 158 | case IB_RATE_10_GBPS: return 10000; |
| 159 | case IB_RATE_20_GBPS: return 20000; |
| 160 | case IB_RATE_30_GBPS: return 30000; |
| 161 | case IB_RATE_40_GBPS: return 40000; |
| 162 | case IB_RATE_60_GBPS: return 60000; |
| 163 | case IB_RATE_80_GBPS: return 80000; |
| 164 | case IB_RATE_120_GBPS: return 120000; |
| 165 | case IB_RATE_14_GBPS: return 14062; |
| 166 | case IB_RATE_56_GBPS: return 56250; |
| 167 | case IB_RATE_112_GBPS: return 112500; |
| 168 | case IB_RATE_168_GBPS: return 168750; |
| 169 | case IB_RATE_25_GBPS: return 25781; |
| 170 | case IB_RATE_100_GBPS: return 103125; |
| 171 | case IB_RATE_200_GBPS: return 206250; |
| 172 | case IB_RATE_300_GBPS: return 309375; |
| 173 | default: return -1; |
| 174 | } |
| 175 | } |
| 176 | EXPORT_SYMBOL(ib_rate_to_mbps); |
| 177 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 178 | __attribute_const__ enum rdma_transport_type |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 179 | rdma_node_get_transport(enum rdma_node_type node_type) |
| 180 | { |
| 181 | switch (node_type) { |
| 182 | case RDMA_NODE_IB_CA: |
| 183 | case RDMA_NODE_IB_SWITCH: |
| 184 | case RDMA_NODE_IB_ROUTER: |
| 185 | return RDMA_TRANSPORT_IB; |
| 186 | case RDMA_NODE_RNIC: |
| 187 | return RDMA_TRANSPORT_IWARP; |
Upinder Malhi \(umalhi\) | 180771a | 2013-09-10 03:36:59 +0000 | [diff] [blame] | 188 | case RDMA_NODE_USNIC: |
Upinder Malhi | 5db5765 | 2014-01-15 17:02:36 -0800 | [diff] [blame] | 189 | return RDMA_TRANSPORT_USNIC; |
| 190 | case RDMA_NODE_USNIC_UDP: |
Upinder Malhi | 248567f | 2014-01-09 14:48:19 -0800 | [diff] [blame] | 191 | return RDMA_TRANSPORT_USNIC_UDP; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 192 | default: |
| 193 | BUG(); |
| 194 | return 0; |
| 195 | } |
| 196 | } |
| 197 | EXPORT_SYMBOL(rdma_node_get_transport); |
| 198 | |
Eli Cohen | a3f5ada | 2010-09-27 17:51:10 -0700 | [diff] [blame] | 199 | enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num) |
| 200 | { |
| 201 | if (device->get_link_layer) |
| 202 | return device->get_link_layer(device, port_num); |
| 203 | |
| 204 | switch (rdma_node_get_transport(device->node_type)) { |
| 205 | case RDMA_TRANSPORT_IB: |
| 206 | return IB_LINK_LAYER_INFINIBAND; |
| 207 | case RDMA_TRANSPORT_IWARP: |
Upinder Malhi \(umalhi\) | 180771a | 2013-09-10 03:36:59 +0000 | [diff] [blame] | 208 | case RDMA_TRANSPORT_USNIC: |
Upinder Malhi | 248567f | 2014-01-09 14:48:19 -0800 | [diff] [blame] | 209 | case RDMA_TRANSPORT_USNIC_UDP: |
Eli Cohen | a3f5ada | 2010-09-27 17:51:10 -0700 | [diff] [blame] | 210 | return IB_LINK_LAYER_ETHERNET; |
| 211 | default: |
| 212 | return IB_LINK_LAYER_UNSPECIFIED; |
| 213 | } |
| 214 | } |
| 215 | EXPORT_SYMBOL(rdma_port_get_link_layer); |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* Protection domains */ |
| 218 | |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 219 | /** |
| 220 | * ib_alloc_pd - Allocates an unused protection domain. |
| 221 | * @device: The device on which to allocate the protection domain. |
| 222 | * |
| 223 | * A protection domain object provides an association between QPs, shared |
| 224 | * receive queues, address handles, memory regions, and memory windows. |
| 225 | * |
| 226 | * Every PD has a local_dma_lkey which can be used as the lkey value for local |
| 227 | * memory operations. |
| 228 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | struct ib_pd *ib_alloc_pd(struct ib_device *device) |
| 230 | { |
| 231 | struct ib_pd *pd; |
| 232 | |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 233 | pd = device->alloc_pd(device, NULL, NULL); |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 234 | if (IS_ERR(pd)) |
| 235 | return pd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 237 | pd->device = device; |
| 238 | pd->uobject = NULL; |
| 239 | pd->local_mr = NULL; |
| 240 | atomic_set(&pd->usecnt, 0); |
| 241 | |
Or Gerlitz | 86bee4c | 2015-12-18 10:59:45 +0200 | [diff] [blame] | 242 | if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 243 | pd->local_dma_lkey = device->local_dma_lkey; |
| 244 | else { |
| 245 | struct ib_mr *mr; |
| 246 | |
| 247 | mr = ib_get_dma_mr(pd, IB_ACCESS_LOCAL_WRITE); |
| 248 | if (IS_ERR(mr)) { |
| 249 | ib_dealloc_pd(pd); |
| 250 | return (struct ib_pd *)mr; |
| 251 | } |
| 252 | |
| 253 | pd->local_mr = mr; |
| 254 | pd->local_dma_lkey = pd->local_mr->lkey; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | return pd; |
| 257 | } |
| 258 | EXPORT_SYMBOL(ib_alloc_pd); |
| 259 | |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 260 | /** |
| 261 | * ib_dealloc_pd - Deallocates a protection domain. |
| 262 | * @pd: The protection domain to deallocate. |
| 263 | * |
| 264 | * It is an error to call this function while any resources in the pd still |
| 265 | * exist. The caller is responsible to synchronously destroy them and |
| 266 | * guarantee no new allocations will happen. |
| 267 | */ |
| 268 | void ib_dealloc_pd(struct ib_pd *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 270 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 272 | if (pd->local_mr) { |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 273 | ret = ib_dereg_mr(pd->local_mr); |
| 274 | WARN_ON(ret); |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 275 | pd->local_mr = NULL; |
| 276 | } |
| 277 | |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 278 | /* uverbs manipulates usecnt with proper locking, while the kabi |
| 279 | requires the caller to guarantee we can't race here. */ |
| 280 | WARN_ON(atomic_read(&pd->usecnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 282 | /* Making delalloc_pd a void return is a WIP, no driver should return |
| 283 | an error here. */ |
| 284 | ret = pd->device->dealloc_pd(pd); |
| 285 | WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | EXPORT_SYMBOL(ib_dealloc_pd); |
| 288 | |
| 289 | /* Address handles */ |
| 290 | |
| 291 | struct ib_ah *ib_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr) |
| 292 | { |
| 293 | struct ib_ah *ah; |
| 294 | |
| 295 | ah = pd->device->create_ah(pd, ah_attr); |
| 296 | |
| 297 | if (!IS_ERR(ah)) { |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 298 | ah->device = pd->device; |
| 299 | ah->pd = pd; |
| 300 | ah->uobject = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | atomic_inc(&pd->usecnt); |
| 302 | } |
| 303 | |
| 304 | return ah; |
| 305 | } |
| 306 | EXPORT_SYMBOL(ib_create_ah); |
| 307 | |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 308 | static int ib_get_header_version(const union rdma_network_hdr *hdr) |
| 309 | { |
| 310 | const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh; |
| 311 | struct iphdr ip4h_checked; |
| 312 | const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh; |
| 313 | |
| 314 | /* If it's IPv6, the version must be 6, otherwise, the first |
| 315 | * 20 bytes (before the IPv4 header) are garbled. |
| 316 | */ |
| 317 | if (ip6h->version != 6) |
| 318 | return (ip4h->version == 4) ? 4 : 0; |
| 319 | /* version may be 6 or 4 because the first 20 bytes could be garbled */ |
| 320 | |
| 321 | /* RoCE v2 requires no options, thus header length |
| 322 | * must be 5 words |
| 323 | */ |
| 324 | if (ip4h->ihl != 5) |
| 325 | return 6; |
| 326 | |
| 327 | /* Verify checksum. |
| 328 | * We can't write on scattered buffers so we need to copy to |
| 329 | * temp buffer. |
| 330 | */ |
| 331 | memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked)); |
| 332 | ip4h_checked.check = 0; |
| 333 | ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5); |
| 334 | /* if IPv4 header checksum is OK, believe it */ |
| 335 | if (ip4h->check == ip4h_checked.check) |
| 336 | return 4; |
| 337 | return 6; |
| 338 | } |
| 339 | |
| 340 | static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device, |
| 341 | u8 port_num, |
| 342 | const struct ib_grh *grh) |
| 343 | { |
| 344 | int grh_version; |
| 345 | |
| 346 | if (rdma_protocol_ib(device, port_num)) |
| 347 | return RDMA_NETWORK_IB; |
| 348 | |
| 349 | grh_version = ib_get_header_version((union rdma_network_hdr *)grh); |
| 350 | |
| 351 | if (grh_version == 4) |
| 352 | return RDMA_NETWORK_IPV4; |
| 353 | |
| 354 | if (grh->next_hdr == IPPROTO_UDP) |
| 355 | return RDMA_NETWORK_IPV6; |
| 356 | |
| 357 | return RDMA_NETWORK_ROCE_V1; |
| 358 | } |
| 359 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 360 | struct find_gid_index_context { |
| 361 | u16 vlan_id; |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 362 | enum ib_gid_type gid_type; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | static bool find_gid_index(const union ib_gid *gid, |
| 366 | const struct ib_gid_attr *gid_attr, |
| 367 | void *context) |
| 368 | { |
| 369 | struct find_gid_index_context *ctx = |
| 370 | (struct find_gid_index_context *)context; |
| 371 | |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 372 | if (ctx->gid_type != gid_attr->gid_type) |
| 373 | return false; |
| 374 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 375 | if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) || |
| 376 | (is_vlan_dev(gid_attr->ndev) && |
| 377 | vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id)) |
| 378 | return false; |
| 379 | |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num, |
| 384 | u16 vlan_id, const union ib_gid *sgid, |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 385 | enum ib_gid_type gid_type, |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 386 | u16 *gid_index) |
| 387 | { |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 388 | struct find_gid_index_context context = {.vlan_id = vlan_id, |
| 389 | .gid_type = gid_type}; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 390 | |
| 391 | return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index, |
| 392 | &context, gid_index); |
| 393 | } |
| 394 | |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 395 | static int get_gids_from_rdma_hdr(union rdma_network_hdr *hdr, |
| 396 | enum rdma_network_type net_type, |
| 397 | union ib_gid *sgid, union ib_gid *dgid) |
| 398 | { |
| 399 | struct sockaddr_in src_in; |
| 400 | struct sockaddr_in dst_in; |
| 401 | __be32 src_saddr, dst_saddr; |
| 402 | |
| 403 | if (!sgid || !dgid) |
| 404 | return -EINVAL; |
| 405 | |
| 406 | if (net_type == RDMA_NETWORK_IPV4) { |
| 407 | memcpy(&src_in.sin_addr.s_addr, |
| 408 | &hdr->roce4grh.saddr, 4); |
| 409 | memcpy(&dst_in.sin_addr.s_addr, |
| 410 | &hdr->roce4grh.daddr, 4); |
| 411 | src_saddr = src_in.sin_addr.s_addr; |
| 412 | dst_saddr = dst_in.sin_addr.s_addr; |
| 413 | ipv6_addr_set_v4mapped(src_saddr, |
| 414 | (struct in6_addr *)sgid); |
| 415 | ipv6_addr_set_v4mapped(dst_saddr, |
| 416 | (struct in6_addr *)dgid); |
| 417 | return 0; |
| 418 | } else if (net_type == RDMA_NETWORK_IPV6 || |
| 419 | net_type == RDMA_NETWORK_IB) { |
| 420 | *dgid = hdr->ibgrh.dgid; |
| 421 | *sgid = hdr->ibgrh.sgid; |
| 422 | return 0; |
| 423 | } else { |
| 424 | return -EINVAL; |
| 425 | } |
| 426 | } |
| 427 | |
Ira Weiny | 73cdaae | 2015-05-31 17:15:31 -0400 | [diff] [blame] | 428 | int ib_init_ah_from_wc(struct ib_device *device, u8 port_num, |
| 429 | const struct ib_wc *wc, const struct ib_grh *grh, |
| 430 | struct ib_ah_attr *ah_attr) |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 431 | { |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 432 | u32 flow_class; |
| 433 | u16 gid_index; |
| 434 | int ret; |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 435 | enum rdma_network_type net_type = RDMA_NETWORK_IB; |
| 436 | enum ib_gid_type gid_type = IB_GID_TYPE_IB; |
| 437 | union ib_gid dgid; |
| 438 | union ib_gid sgid; |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 439 | |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 440 | memset(ah_attr, 0, sizeof *ah_attr); |
Michael Wang | 227128f | 2015-05-05 14:50:40 +0200 | [diff] [blame] | 441 | if (rdma_cap_eth_ah(device, port_num)) { |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 442 | if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE) |
| 443 | net_type = wc->network_hdr_type; |
| 444 | else |
| 445 | net_type = ib_get_net_type_by_grh(device, port_num, grh); |
| 446 | gid_type = ib_network_to_gid_type(net_type); |
| 447 | } |
| 448 | ret = get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type, |
| 449 | &sgid, &dgid); |
| 450 | if (ret) |
| 451 | return ret; |
| 452 | |
| 453 | if (rdma_protocol_roce(device, port_num)) { |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 454 | u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ? |
| 455 | wc->vlan_id : 0xffff; |
| 456 | |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 457 | if (!(wc->wc_flags & IB_WC_GRH)) |
| 458 | return -EPROTOTYPE; |
| 459 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 460 | if (!(wc->wc_flags & IB_WC_WITH_SMAC) || |
| 461 | !(wc->wc_flags & IB_WC_WITH_VLAN)) { |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 462 | ret = rdma_addr_find_dmac_by_grh(&dgid, &sgid, |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 463 | ah_attr->dmac, |
| 464 | wc->wc_flags & IB_WC_WITH_VLAN ? |
| 465 | NULL : &vlan_id, |
| 466 | 0); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 467 | if (ret) |
| 468 | return ret; |
| 469 | } |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 470 | |
| 471 | ret = get_sgid_index_from_eth(device, port_num, vlan_id, |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 472 | &dgid, gid_type, &gid_index); |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 473 | if (ret) |
| 474 | return ret; |
| 475 | |
| 476 | if (wc->wc_flags & IB_WC_WITH_SMAC) |
| 477 | memcpy(ah_attr->dmac, wc->smac, ETH_ALEN); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 480 | ah_attr->dlid = wc->slid; |
| 481 | ah_attr->sl = wc->sl; |
| 482 | ah_attr->src_path_bits = wc->dlid_path_bits; |
| 483 | ah_attr->port_num = port_num; |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 484 | |
| 485 | if (wc->wc_flags & IB_WC_GRH) { |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 486 | ah_attr->ah_flags = IB_AH_GRH; |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 487 | ah_attr->grh.dgid = sgid; |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 488 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 489 | if (!rdma_cap_eth_ah(device, port_num)) { |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 490 | ret = ib_find_cached_gid_by_port(device, &dgid, |
Matan Barak | b39ffa1 | 2015-12-23 14:56:47 +0200 | [diff] [blame] | 491 | IB_GID_TYPE_IB, |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 492 | port_num, NULL, |
| 493 | &gid_index); |
| 494 | if (ret) |
| 495 | return ret; |
| 496 | } |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 497 | |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 498 | ah_attr->grh.sgid_index = (u8) gid_index; |
Hal Rosenstock | 497677a | 2005-07-27 11:45:35 -0700 | [diff] [blame] | 499 | flow_class = be32_to_cpu(grh->version_tclass_flow); |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 500 | ah_attr->grh.flow_label = flow_class & 0xFFFFF; |
Sean Hefty | 47645d8 | 2007-02-21 16:37:31 -0800 | [diff] [blame] | 501 | ah_attr->grh.hop_limit = 0xFF; |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 502 | ah_attr->grh.traffic_class = (flow_class >> 20) & 0xFF; |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 503 | } |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | EXPORT_SYMBOL(ib_init_ah_from_wc); |
| 507 | |
Ira Weiny | 73cdaae | 2015-05-31 17:15:31 -0400 | [diff] [blame] | 508 | struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc, |
| 509 | const struct ib_grh *grh, u8 port_num) |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 510 | { |
| 511 | struct ib_ah_attr ah_attr; |
| 512 | int ret; |
| 513 | |
| 514 | ret = ib_init_ah_from_wc(pd->device, port_num, wc, grh, &ah_attr); |
| 515 | if (ret) |
| 516 | return ERR_PTR(ret); |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 517 | |
| 518 | return ib_create_ah(pd, &ah_attr); |
| 519 | } |
| 520 | EXPORT_SYMBOL(ib_create_ah_from_wc); |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | int ib_modify_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr) |
| 523 | { |
| 524 | return ah->device->modify_ah ? |
| 525 | ah->device->modify_ah(ah, ah_attr) : |
| 526 | -ENOSYS; |
| 527 | } |
| 528 | EXPORT_SYMBOL(ib_modify_ah); |
| 529 | |
| 530 | int ib_query_ah(struct ib_ah *ah, struct ib_ah_attr *ah_attr) |
| 531 | { |
| 532 | return ah->device->query_ah ? |
| 533 | ah->device->query_ah(ah, ah_attr) : |
| 534 | -ENOSYS; |
| 535 | } |
| 536 | EXPORT_SYMBOL(ib_query_ah); |
| 537 | |
| 538 | int ib_destroy_ah(struct ib_ah *ah) |
| 539 | { |
| 540 | struct ib_pd *pd; |
| 541 | int ret; |
| 542 | |
| 543 | pd = ah->pd; |
| 544 | ret = ah->device->destroy_ah(ah); |
| 545 | if (!ret) |
| 546 | atomic_dec(&pd->usecnt); |
| 547 | |
| 548 | return ret; |
| 549 | } |
| 550 | EXPORT_SYMBOL(ib_destroy_ah); |
| 551 | |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 552 | /* Shared receive queues */ |
| 553 | |
| 554 | struct ib_srq *ib_create_srq(struct ib_pd *pd, |
| 555 | struct ib_srq_init_attr *srq_init_attr) |
| 556 | { |
| 557 | struct ib_srq *srq; |
| 558 | |
| 559 | if (!pd->device->create_srq) |
| 560 | return ERR_PTR(-ENOSYS); |
| 561 | |
| 562 | srq = pd->device->create_srq(pd, srq_init_attr, NULL); |
| 563 | |
| 564 | if (!IS_ERR(srq)) { |
| 565 | srq->device = pd->device; |
| 566 | srq->pd = pd; |
| 567 | srq->uobject = NULL; |
| 568 | srq->event_handler = srq_init_attr->event_handler; |
| 569 | srq->srq_context = srq_init_attr->srq_context; |
Sean Hefty | 96104ed | 2011-05-23 16:31:36 -0700 | [diff] [blame] | 570 | srq->srq_type = srq_init_attr->srq_type; |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 571 | if (srq->srq_type == IB_SRQT_XRC) { |
| 572 | srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd; |
| 573 | srq->ext.xrc.cq = srq_init_attr->ext.xrc.cq; |
| 574 | atomic_inc(&srq->ext.xrc.xrcd->usecnt); |
| 575 | atomic_inc(&srq->ext.xrc.cq->usecnt); |
| 576 | } |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 577 | atomic_inc(&pd->usecnt); |
| 578 | atomic_set(&srq->usecnt, 0); |
| 579 | } |
| 580 | |
| 581 | return srq; |
| 582 | } |
| 583 | EXPORT_SYMBOL(ib_create_srq); |
| 584 | |
| 585 | int ib_modify_srq(struct ib_srq *srq, |
| 586 | struct ib_srq_attr *srq_attr, |
| 587 | enum ib_srq_attr_mask srq_attr_mask) |
| 588 | { |
Dotan Barak | 7ce5eac | 2008-04-16 21:09:28 -0700 | [diff] [blame] | 589 | return srq->device->modify_srq ? |
| 590 | srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) : |
| 591 | -ENOSYS; |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 592 | } |
| 593 | EXPORT_SYMBOL(ib_modify_srq); |
| 594 | |
| 595 | int ib_query_srq(struct ib_srq *srq, |
| 596 | struct ib_srq_attr *srq_attr) |
| 597 | { |
| 598 | return srq->device->query_srq ? |
| 599 | srq->device->query_srq(srq, srq_attr) : -ENOSYS; |
| 600 | } |
| 601 | EXPORT_SYMBOL(ib_query_srq); |
| 602 | |
| 603 | int ib_destroy_srq(struct ib_srq *srq) |
| 604 | { |
| 605 | struct ib_pd *pd; |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 606 | enum ib_srq_type srq_type; |
| 607 | struct ib_xrcd *uninitialized_var(xrcd); |
| 608 | struct ib_cq *uninitialized_var(cq); |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 609 | int ret; |
| 610 | |
| 611 | if (atomic_read(&srq->usecnt)) |
| 612 | return -EBUSY; |
| 613 | |
| 614 | pd = srq->pd; |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 615 | srq_type = srq->srq_type; |
| 616 | if (srq_type == IB_SRQT_XRC) { |
| 617 | xrcd = srq->ext.xrc.xrcd; |
| 618 | cq = srq->ext.xrc.cq; |
| 619 | } |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 620 | |
| 621 | ret = srq->device->destroy_srq(srq); |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 622 | if (!ret) { |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 623 | atomic_dec(&pd->usecnt); |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 624 | if (srq_type == IB_SRQT_XRC) { |
| 625 | atomic_dec(&xrcd->usecnt); |
| 626 | atomic_dec(&cq->usecnt); |
| 627 | } |
| 628 | } |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 629 | |
| 630 | return ret; |
| 631 | } |
| 632 | EXPORT_SYMBOL(ib_destroy_srq); |
| 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | /* Queue pairs */ |
| 635 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 636 | static void __ib_shared_qp_event_handler(struct ib_event *event, void *context) |
| 637 | { |
| 638 | struct ib_qp *qp = context; |
Yishai Hadas | 73c40c6 | 2013-08-01 18:49:53 +0300 | [diff] [blame] | 639 | unsigned long flags; |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 640 | |
Yishai Hadas | 73c40c6 | 2013-08-01 18:49:53 +0300 | [diff] [blame] | 641 | spin_lock_irqsave(&qp->device->event_handler_lock, flags); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 642 | list_for_each_entry(event->element.qp, &qp->open_list, open_list) |
Shlomo Pongratz | eec9e29f | 2013-04-10 14:26:46 +0000 | [diff] [blame] | 643 | if (event->element.qp->event_handler) |
| 644 | event->element.qp->event_handler(event, event->element.qp->qp_context); |
Yishai Hadas | 73c40c6 | 2013-08-01 18:49:53 +0300 | [diff] [blame] | 645 | spin_unlock_irqrestore(&qp->device->event_handler_lock, flags); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 648 | static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp) |
| 649 | { |
| 650 | mutex_lock(&xrcd->tgt_qp_mutex); |
| 651 | list_add(&qp->xrcd_list, &xrcd->tgt_qp_list); |
| 652 | mutex_unlock(&xrcd->tgt_qp_mutex); |
| 653 | } |
| 654 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 655 | static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp, |
| 656 | void (*event_handler)(struct ib_event *, void *), |
| 657 | void *qp_context) |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 658 | { |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 659 | struct ib_qp *qp; |
| 660 | unsigned long flags; |
| 661 | |
| 662 | qp = kzalloc(sizeof *qp, GFP_KERNEL); |
| 663 | if (!qp) |
| 664 | return ERR_PTR(-ENOMEM); |
| 665 | |
| 666 | qp->real_qp = real_qp; |
| 667 | atomic_inc(&real_qp->usecnt); |
| 668 | qp->device = real_qp->device; |
| 669 | qp->event_handler = event_handler; |
| 670 | qp->qp_context = qp_context; |
| 671 | qp->qp_num = real_qp->qp_num; |
| 672 | qp->qp_type = real_qp->qp_type; |
| 673 | |
| 674 | spin_lock_irqsave(&real_qp->device->event_handler_lock, flags); |
| 675 | list_add(&qp->open_list, &real_qp->open_list); |
| 676 | spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags); |
| 677 | |
| 678 | return qp; |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 681 | struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd, |
| 682 | struct ib_qp_open_attr *qp_open_attr) |
| 683 | { |
| 684 | struct ib_qp *qp, *real_qp; |
| 685 | |
| 686 | if (qp_open_attr->qp_type != IB_QPT_XRC_TGT) |
| 687 | return ERR_PTR(-EINVAL); |
| 688 | |
| 689 | qp = ERR_PTR(-EINVAL); |
| 690 | mutex_lock(&xrcd->tgt_qp_mutex); |
| 691 | list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) { |
| 692 | if (real_qp->qp_num == qp_open_attr->qp_num) { |
| 693 | qp = __ib_open_qp(real_qp, qp_open_attr->event_handler, |
| 694 | qp_open_attr->qp_context); |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | mutex_unlock(&xrcd->tgt_qp_mutex); |
| 699 | return qp; |
| 700 | } |
| 701 | EXPORT_SYMBOL(ib_open_qp); |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | struct ib_qp *ib_create_qp(struct ib_pd *pd, |
| 704 | struct ib_qp_init_attr *qp_init_attr) |
| 705 | { |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 706 | struct ib_qp *qp, *real_qp; |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 707 | struct ib_device *device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 709 | device = pd ? pd->device : qp_init_attr->xrcd->device; |
| 710 | qp = device->create_qp(pd, qp_init_attr, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | if (!IS_ERR(qp)) { |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 713 | qp->device = device; |
| 714 | qp->real_qp = qp; |
| 715 | qp->uobject = NULL; |
| 716 | qp->qp_type = qp_init_attr->qp_type; |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 717 | |
Bernd Schubert | e47e321 | 2012-01-20 18:43:54 +0000 | [diff] [blame] | 718 | atomic_set(&qp->usecnt, 0); |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 719 | if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) { |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 720 | qp->event_handler = __ib_shared_qp_event_handler; |
| 721 | qp->qp_context = qp; |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 722 | qp->pd = NULL; |
| 723 | qp->send_cq = qp->recv_cq = NULL; |
| 724 | qp->srq = NULL; |
| 725 | qp->xrcd = qp_init_attr->xrcd; |
| 726 | atomic_inc(&qp_init_attr->xrcd->usecnt); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 727 | INIT_LIST_HEAD(&qp->open_list); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 728 | |
| 729 | real_qp = qp; |
| 730 | qp = __ib_open_qp(real_qp, qp_init_attr->event_handler, |
| 731 | qp_init_attr->qp_context); |
| 732 | if (!IS_ERR(qp)) |
| 733 | __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp); |
| 734 | else |
| 735 | real_qp->device->destroy_qp(real_qp); |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 736 | } else { |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 737 | qp->event_handler = qp_init_attr->event_handler; |
| 738 | qp->qp_context = qp_init_attr->qp_context; |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 739 | if (qp_init_attr->qp_type == IB_QPT_XRC_INI) { |
| 740 | qp->recv_cq = NULL; |
| 741 | qp->srq = NULL; |
| 742 | } else { |
| 743 | qp->recv_cq = qp_init_attr->recv_cq; |
| 744 | atomic_inc(&qp_init_attr->recv_cq->usecnt); |
| 745 | qp->srq = qp_init_attr->srq; |
| 746 | if (qp->srq) |
| 747 | atomic_inc(&qp_init_attr->srq->usecnt); |
| 748 | } |
| 749 | |
| 750 | qp->pd = pd; |
| 751 | qp->send_cq = qp_init_attr->send_cq; |
| 752 | qp->xrcd = NULL; |
| 753 | |
| 754 | atomic_inc(&pd->usecnt); |
| 755 | atomic_inc(&qp_init_attr->send_cq->usecnt); |
| 756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | return qp; |
| 760 | } |
| 761 | EXPORT_SYMBOL(ib_create_qp); |
| 762 | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 763 | static const struct { |
| 764 | int valid; |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 765 | enum ib_qp_attr_mask req_param[IB_QPT_MAX]; |
| 766 | enum ib_qp_attr_mask opt_param[IB_QPT_MAX]; |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 767 | } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = { |
| 768 | [IB_QPS_RESET] = { |
| 769 | [IB_QPS_RESET] = { .valid = 1 }, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 770 | [IB_QPS_INIT] = { |
| 771 | .valid = 1, |
| 772 | .req_param = { |
| 773 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 774 | IB_QP_PORT | |
| 775 | IB_QP_QKEY), |
Or Gerlitz | c938a61 | 2012-03-01 12:17:51 +0200 | [diff] [blame] | 776 | [IB_QPT_RAW_PACKET] = IB_QP_PORT, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 777 | [IB_QPT_UC] = (IB_QP_PKEY_INDEX | |
| 778 | IB_QP_PORT | |
| 779 | IB_QP_ACCESS_FLAGS), |
| 780 | [IB_QPT_RC] = (IB_QP_PKEY_INDEX | |
| 781 | IB_QP_PORT | |
| 782 | IB_QP_ACCESS_FLAGS), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 783 | [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX | |
| 784 | IB_QP_PORT | |
| 785 | IB_QP_ACCESS_FLAGS), |
| 786 | [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX | |
| 787 | IB_QP_PORT | |
| 788 | IB_QP_ACCESS_FLAGS), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 789 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 790 | IB_QP_QKEY), |
| 791 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 792 | IB_QP_QKEY), |
| 793 | } |
| 794 | }, |
| 795 | }, |
| 796 | [IB_QPS_INIT] = { |
| 797 | [IB_QPS_RESET] = { .valid = 1 }, |
| 798 | [IB_QPS_ERR] = { .valid = 1 }, |
| 799 | [IB_QPS_INIT] = { |
| 800 | .valid = 1, |
| 801 | .opt_param = { |
| 802 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 803 | IB_QP_PORT | |
| 804 | IB_QP_QKEY), |
| 805 | [IB_QPT_UC] = (IB_QP_PKEY_INDEX | |
| 806 | IB_QP_PORT | |
| 807 | IB_QP_ACCESS_FLAGS), |
| 808 | [IB_QPT_RC] = (IB_QP_PKEY_INDEX | |
| 809 | IB_QP_PORT | |
| 810 | IB_QP_ACCESS_FLAGS), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 811 | [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX | |
| 812 | IB_QP_PORT | |
| 813 | IB_QP_ACCESS_FLAGS), |
| 814 | [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX | |
| 815 | IB_QP_PORT | |
| 816 | IB_QP_ACCESS_FLAGS), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 817 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 818 | IB_QP_QKEY), |
| 819 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 820 | IB_QP_QKEY), |
| 821 | } |
| 822 | }, |
| 823 | [IB_QPS_RTR] = { |
| 824 | .valid = 1, |
| 825 | .req_param = { |
| 826 | [IB_QPT_UC] = (IB_QP_AV | |
| 827 | IB_QP_PATH_MTU | |
| 828 | IB_QP_DEST_QPN | |
| 829 | IB_QP_RQ_PSN), |
| 830 | [IB_QPT_RC] = (IB_QP_AV | |
| 831 | IB_QP_PATH_MTU | |
| 832 | IB_QP_DEST_QPN | |
| 833 | IB_QP_RQ_PSN | |
| 834 | IB_QP_MAX_DEST_RD_ATOMIC | |
| 835 | IB_QP_MIN_RNR_TIMER), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 836 | [IB_QPT_XRC_INI] = (IB_QP_AV | |
| 837 | IB_QP_PATH_MTU | |
| 838 | IB_QP_DEST_QPN | |
| 839 | IB_QP_RQ_PSN), |
| 840 | [IB_QPT_XRC_TGT] = (IB_QP_AV | |
| 841 | IB_QP_PATH_MTU | |
| 842 | IB_QP_DEST_QPN | |
| 843 | IB_QP_RQ_PSN | |
| 844 | IB_QP_MAX_DEST_RD_ATOMIC | |
| 845 | IB_QP_MIN_RNR_TIMER), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 846 | }, |
| 847 | .opt_param = { |
| 848 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 849 | IB_QP_QKEY), |
| 850 | [IB_QPT_UC] = (IB_QP_ALT_PATH | |
| 851 | IB_QP_ACCESS_FLAGS | |
| 852 | IB_QP_PKEY_INDEX), |
| 853 | [IB_QPT_RC] = (IB_QP_ALT_PATH | |
| 854 | IB_QP_ACCESS_FLAGS | |
| 855 | IB_QP_PKEY_INDEX), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 856 | [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH | |
| 857 | IB_QP_ACCESS_FLAGS | |
| 858 | IB_QP_PKEY_INDEX), |
| 859 | [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH | |
| 860 | IB_QP_ACCESS_FLAGS | |
| 861 | IB_QP_PKEY_INDEX), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 862 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 863 | IB_QP_QKEY), |
| 864 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 865 | IB_QP_QKEY), |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 866 | }, |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 867 | }, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 868 | }, |
| 869 | [IB_QPS_RTR] = { |
| 870 | [IB_QPS_RESET] = { .valid = 1 }, |
| 871 | [IB_QPS_ERR] = { .valid = 1 }, |
| 872 | [IB_QPS_RTS] = { |
| 873 | .valid = 1, |
| 874 | .req_param = { |
| 875 | [IB_QPT_UD] = IB_QP_SQ_PSN, |
| 876 | [IB_QPT_UC] = IB_QP_SQ_PSN, |
| 877 | [IB_QPT_RC] = (IB_QP_TIMEOUT | |
| 878 | IB_QP_RETRY_CNT | |
| 879 | IB_QP_RNR_RETRY | |
| 880 | IB_QP_SQ_PSN | |
| 881 | IB_QP_MAX_QP_RD_ATOMIC), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 882 | [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT | |
| 883 | IB_QP_RETRY_CNT | |
| 884 | IB_QP_RNR_RETRY | |
| 885 | IB_QP_SQ_PSN | |
| 886 | IB_QP_MAX_QP_RD_ATOMIC), |
| 887 | [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT | |
| 888 | IB_QP_SQ_PSN), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 889 | [IB_QPT_SMI] = IB_QP_SQ_PSN, |
| 890 | [IB_QPT_GSI] = IB_QP_SQ_PSN, |
| 891 | }, |
| 892 | .opt_param = { |
| 893 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 894 | IB_QP_QKEY), |
| 895 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 896 | IB_QP_ALT_PATH | |
| 897 | IB_QP_ACCESS_FLAGS | |
| 898 | IB_QP_PATH_MIG_STATE), |
| 899 | [IB_QPT_RC] = (IB_QP_CUR_STATE | |
| 900 | IB_QP_ALT_PATH | |
| 901 | IB_QP_ACCESS_FLAGS | |
| 902 | IB_QP_MIN_RNR_TIMER | |
| 903 | IB_QP_PATH_MIG_STATE), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 904 | [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | |
| 905 | IB_QP_ALT_PATH | |
| 906 | IB_QP_ACCESS_FLAGS | |
| 907 | IB_QP_PATH_MIG_STATE), |
| 908 | [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | |
| 909 | IB_QP_ALT_PATH | |
| 910 | IB_QP_ACCESS_FLAGS | |
| 911 | IB_QP_MIN_RNR_TIMER | |
| 912 | IB_QP_PATH_MIG_STATE), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 913 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 914 | IB_QP_QKEY), |
| 915 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 916 | IB_QP_QKEY), |
| 917 | } |
| 918 | } |
| 919 | }, |
| 920 | [IB_QPS_RTS] = { |
| 921 | [IB_QPS_RESET] = { .valid = 1 }, |
| 922 | [IB_QPS_ERR] = { .valid = 1 }, |
| 923 | [IB_QPS_RTS] = { |
| 924 | .valid = 1, |
| 925 | .opt_param = { |
| 926 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 927 | IB_QP_QKEY), |
Dotan Barak | 4546d31 | 2006-03-02 11:22:28 -0800 | [diff] [blame] | 928 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 929 | IB_QP_ACCESS_FLAGS | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 930 | IB_QP_ALT_PATH | |
| 931 | IB_QP_PATH_MIG_STATE), |
Dotan Barak | 4546d31 | 2006-03-02 11:22:28 -0800 | [diff] [blame] | 932 | [IB_QPT_RC] = (IB_QP_CUR_STATE | |
| 933 | IB_QP_ACCESS_FLAGS | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 934 | IB_QP_ALT_PATH | |
| 935 | IB_QP_PATH_MIG_STATE | |
| 936 | IB_QP_MIN_RNR_TIMER), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 937 | [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | |
| 938 | IB_QP_ACCESS_FLAGS | |
| 939 | IB_QP_ALT_PATH | |
| 940 | IB_QP_PATH_MIG_STATE), |
| 941 | [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | |
| 942 | IB_QP_ACCESS_FLAGS | |
| 943 | IB_QP_ALT_PATH | |
| 944 | IB_QP_PATH_MIG_STATE | |
| 945 | IB_QP_MIN_RNR_TIMER), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 946 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 947 | IB_QP_QKEY), |
| 948 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 949 | IB_QP_QKEY), |
| 950 | } |
| 951 | }, |
| 952 | [IB_QPS_SQD] = { |
| 953 | .valid = 1, |
| 954 | .opt_param = { |
| 955 | [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 956 | [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 957 | [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 958 | [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 959 | [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */ |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 960 | [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 961 | [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY |
| 962 | } |
| 963 | }, |
| 964 | }, |
| 965 | [IB_QPS_SQD] = { |
| 966 | [IB_QPS_RESET] = { .valid = 1 }, |
| 967 | [IB_QPS_ERR] = { .valid = 1 }, |
| 968 | [IB_QPS_RTS] = { |
| 969 | .valid = 1, |
| 970 | .opt_param = { |
| 971 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 972 | IB_QP_QKEY), |
| 973 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 974 | IB_QP_ALT_PATH | |
| 975 | IB_QP_ACCESS_FLAGS | |
| 976 | IB_QP_PATH_MIG_STATE), |
| 977 | [IB_QPT_RC] = (IB_QP_CUR_STATE | |
| 978 | IB_QP_ALT_PATH | |
| 979 | IB_QP_ACCESS_FLAGS | |
| 980 | IB_QP_MIN_RNR_TIMER | |
| 981 | IB_QP_PATH_MIG_STATE), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 982 | [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | |
| 983 | IB_QP_ALT_PATH | |
| 984 | IB_QP_ACCESS_FLAGS | |
| 985 | IB_QP_PATH_MIG_STATE), |
| 986 | [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | |
| 987 | IB_QP_ALT_PATH | |
| 988 | IB_QP_ACCESS_FLAGS | |
| 989 | IB_QP_MIN_RNR_TIMER | |
| 990 | IB_QP_PATH_MIG_STATE), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 991 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 992 | IB_QP_QKEY), |
| 993 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 994 | IB_QP_QKEY), |
| 995 | } |
| 996 | }, |
| 997 | [IB_QPS_SQD] = { |
| 998 | .valid = 1, |
| 999 | .opt_param = { |
| 1000 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 1001 | IB_QP_QKEY), |
| 1002 | [IB_QPT_UC] = (IB_QP_AV | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1003 | IB_QP_ALT_PATH | |
| 1004 | IB_QP_ACCESS_FLAGS | |
| 1005 | IB_QP_PKEY_INDEX | |
| 1006 | IB_QP_PATH_MIG_STATE), |
| 1007 | [IB_QPT_RC] = (IB_QP_PORT | |
| 1008 | IB_QP_AV | |
| 1009 | IB_QP_TIMEOUT | |
| 1010 | IB_QP_RETRY_CNT | |
| 1011 | IB_QP_RNR_RETRY | |
| 1012 | IB_QP_MAX_QP_RD_ATOMIC | |
| 1013 | IB_QP_MAX_DEST_RD_ATOMIC | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1014 | IB_QP_ALT_PATH | |
| 1015 | IB_QP_ACCESS_FLAGS | |
| 1016 | IB_QP_PKEY_INDEX | |
| 1017 | IB_QP_MIN_RNR_TIMER | |
| 1018 | IB_QP_PATH_MIG_STATE), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1019 | [IB_QPT_XRC_INI] = (IB_QP_PORT | |
| 1020 | IB_QP_AV | |
| 1021 | IB_QP_TIMEOUT | |
| 1022 | IB_QP_RETRY_CNT | |
| 1023 | IB_QP_RNR_RETRY | |
| 1024 | IB_QP_MAX_QP_RD_ATOMIC | |
| 1025 | IB_QP_ALT_PATH | |
| 1026 | IB_QP_ACCESS_FLAGS | |
| 1027 | IB_QP_PKEY_INDEX | |
| 1028 | IB_QP_PATH_MIG_STATE), |
| 1029 | [IB_QPT_XRC_TGT] = (IB_QP_PORT | |
| 1030 | IB_QP_AV | |
| 1031 | IB_QP_TIMEOUT | |
| 1032 | IB_QP_MAX_DEST_RD_ATOMIC | |
| 1033 | IB_QP_ALT_PATH | |
| 1034 | IB_QP_ACCESS_FLAGS | |
| 1035 | IB_QP_PKEY_INDEX | |
| 1036 | IB_QP_MIN_RNR_TIMER | |
| 1037 | IB_QP_PATH_MIG_STATE), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1038 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 1039 | IB_QP_QKEY), |
| 1040 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 1041 | IB_QP_QKEY), |
| 1042 | } |
| 1043 | } |
| 1044 | }, |
| 1045 | [IB_QPS_SQE] = { |
| 1046 | [IB_QPS_RESET] = { .valid = 1 }, |
| 1047 | [IB_QPS_ERR] = { .valid = 1 }, |
| 1048 | [IB_QPS_RTS] = { |
| 1049 | .valid = 1, |
| 1050 | .opt_param = { |
| 1051 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 1052 | IB_QP_QKEY), |
| 1053 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 1054 | IB_QP_ACCESS_FLAGS), |
| 1055 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 1056 | IB_QP_QKEY), |
| 1057 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 1058 | IB_QP_QKEY), |
| 1059 | } |
| 1060 | } |
| 1061 | }, |
| 1062 | [IB_QPS_ERR] = { |
| 1063 | [IB_QPS_RESET] = { .valid = 1 }, |
| 1064 | [IB_QPS_ERR] = { .valid = 1 } |
| 1065 | } |
| 1066 | }; |
| 1067 | |
| 1068 | int ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state, |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 1069 | enum ib_qp_type type, enum ib_qp_attr_mask mask, |
| 1070 | enum rdma_link_layer ll) |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1071 | { |
| 1072 | enum ib_qp_attr_mask req_param, opt_param; |
| 1073 | |
| 1074 | if (cur_state < 0 || cur_state > IB_QPS_ERR || |
| 1075 | next_state < 0 || next_state > IB_QPS_ERR) |
| 1076 | return 0; |
| 1077 | |
| 1078 | if (mask & IB_QP_CUR_STATE && |
| 1079 | cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS && |
| 1080 | cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE) |
| 1081 | return 0; |
| 1082 | |
| 1083 | if (!qp_state_table[cur_state][next_state].valid) |
| 1084 | return 0; |
| 1085 | |
| 1086 | req_param = qp_state_table[cur_state][next_state].req_param[type]; |
| 1087 | opt_param = qp_state_table[cur_state][next_state].opt_param[type]; |
| 1088 | |
| 1089 | if ((mask & req_param) != req_param) |
| 1090 | return 0; |
| 1091 | |
| 1092 | if (mask & ~(req_param | opt_param | IB_QP_STATE)) |
| 1093 | return 0; |
| 1094 | |
| 1095 | return 1; |
| 1096 | } |
| 1097 | EXPORT_SYMBOL(ib_modify_qp_is_ok); |
| 1098 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1099 | int ib_resolve_eth_dmac(struct ib_qp *qp, |
| 1100 | struct ib_qp_attr *qp_attr, int *qp_attr_mask) |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1101 | { |
| 1102 | int ret = 0; |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1103 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1104 | if (*qp_attr_mask & IB_QP_AV) { |
| 1105 | if (qp_attr->ah_attr.port_num < rdma_start_port(qp->device) || |
| 1106 | qp_attr->ah_attr.port_num > rdma_end_port(qp->device)) |
| 1107 | return -EINVAL; |
| 1108 | |
| 1109 | if (!rdma_cap_eth_ah(qp->device, qp_attr->ah_attr.port_num)) |
| 1110 | return 0; |
| 1111 | |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1112 | if (rdma_link_local_addr((struct in6_addr *)qp_attr->ah_attr.grh.dgid.raw)) { |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1113 | rdma_get_ll_mac((struct in6_addr *)qp_attr->ah_attr.grh.dgid.raw, |
| 1114 | qp_attr->ah_attr.dmac); |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1115 | } else { |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1116 | union ib_gid sgid; |
| 1117 | struct ib_gid_attr sgid_attr; |
| 1118 | int ifindex; |
| 1119 | |
| 1120 | ret = ib_query_gid(qp->device, |
| 1121 | qp_attr->ah_attr.port_num, |
| 1122 | qp_attr->ah_attr.grh.sgid_index, |
| 1123 | &sgid, &sgid_attr); |
| 1124 | |
| 1125 | if (ret || !sgid_attr.ndev) { |
| 1126 | if (!ret) |
| 1127 | ret = -ENXIO; |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1128 | goto out; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1129 | } |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame^] | 1130 | if (sgid_attr.gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) |
| 1131 | /* TODO: get the hoplimit from the inet/inet6 |
| 1132 | * device |
| 1133 | */ |
| 1134 | qp_attr->ah_attr.grh.hop_limit = |
| 1135 | IPV6_DEFAULT_HOPLIMIT; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1136 | |
| 1137 | ifindex = sgid_attr.ndev->ifindex; |
| 1138 | |
| 1139 | ret = rdma_addr_find_dmac_by_grh(&sgid, |
| 1140 | &qp_attr->ah_attr.grh.dgid, |
| 1141 | qp_attr->ah_attr.dmac, |
| 1142 | NULL, ifindex); |
| 1143 | |
| 1144 | dev_put(sgid_attr.ndev); |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1145 | } |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1146 | } |
| 1147 | out: |
| 1148 | return ret; |
| 1149 | } |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1150 | EXPORT_SYMBOL(ib_resolve_eth_dmac); |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1151 | |
| 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | int ib_modify_qp(struct ib_qp *qp, |
| 1154 | struct ib_qp_attr *qp_attr, |
| 1155 | int qp_attr_mask) |
| 1156 | { |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1157 | int ret; |
| 1158 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1159 | ret = ib_resolve_eth_dmac(qp, qp_attr, &qp_attr_mask); |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1160 | if (ret) |
| 1161 | return ret; |
| 1162 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1163 | return qp->device->modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | } |
| 1165 | EXPORT_SYMBOL(ib_modify_qp); |
| 1166 | |
| 1167 | int ib_query_qp(struct ib_qp *qp, |
| 1168 | struct ib_qp_attr *qp_attr, |
| 1169 | int qp_attr_mask, |
| 1170 | struct ib_qp_init_attr *qp_init_attr) |
| 1171 | { |
| 1172 | return qp->device->query_qp ? |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1173 | qp->device->query_qp(qp->real_qp, qp_attr, qp_attr_mask, qp_init_attr) : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | -ENOSYS; |
| 1175 | } |
| 1176 | EXPORT_SYMBOL(ib_query_qp); |
| 1177 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1178 | int ib_close_qp(struct ib_qp *qp) |
| 1179 | { |
| 1180 | struct ib_qp *real_qp; |
| 1181 | unsigned long flags; |
| 1182 | |
| 1183 | real_qp = qp->real_qp; |
| 1184 | if (real_qp == qp) |
| 1185 | return -EINVAL; |
| 1186 | |
| 1187 | spin_lock_irqsave(&real_qp->device->event_handler_lock, flags); |
| 1188 | list_del(&qp->open_list); |
| 1189 | spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags); |
| 1190 | |
| 1191 | atomic_dec(&real_qp->usecnt); |
| 1192 | kfree(qp); |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | EXPORT_SYMBOL(ib_close_qp); |
| 1197 | |
| 1198 | static int __ib_destroy_shared_qp(struct ib_qp *qp) |
| 1199 | { |
| 1200 | struct ib_xrcd *xrcd; |
| 1201 | struct ib_qp *real_qp; |
| 1202 | int ret; |
| 1203 | |
| 1204 | real_qp = qp->real_qp; |
| 1205 | xrcd = real_qp->xrcd; |
| 1206 | |
| 1207 | mutex_lock(&xrcd->tgt_qp_mutex); |
| 1208 | ib_close_qp(qp); |
| 1209 | if (atomic_read(&real_qp->usecnt) == 0) |
| 1210 | list_del(&real_qp->xrcd_list); |
| 1211 | else |
| 1212 | real_qp = NULL; |
| 1213 | mutex_unlock(&xrcd->tgt_qp_mutex); |
| 1214 | |
| 1215 | if (real_qp) { |
| 1216 | ret = ib_destroy_qp(real_qp); |
| 1217 | if (!ret) |
| 1218 | atomic_dec(&xrcd->usecnt); |
| 1219 | else |
| 1220 | __ib_insert_xrcd_qp(xrcd, real_qp); |
| 1221 | } |
| 1222 | |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | int ib_destroy_qp(struct ib_qp *qp) |
| 1227 | { |
| 1228 | struct ib_pd *pd; |
| 1229 | struct ib_cq *scq, *rcq; |
| 1230 | struct ib_srq *srq; |
| 1231 | int ret; |
| 1232 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1233 | if (atomic_read(&qp->usecnt)) |
| 1234 | return -EBUSY; |
| 1235 | |
| 1236 | if (qp->real_qp != qp) |
| 1237 | return __ib_destroy_shared_qp(qp); |
| 1238 | |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1239 | pd = qp->pd; |
| 1240 | scq = qp->send_cq; |
| 1241 | rcq = qp->recv_cq; |
| 1242 | srq = qp->srq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | |
| 1244 | ret = qp->device->destroy_qp(qp); |
| 1245 | if (!ret) { |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1246 | if (pd) |
| 1247 | atomic_dec(&pd->usecnt); |
| 1248 | if (scq) |
| 1249 | atomic_dec(&scq->usecnt); |
| 1250 | if (rcq) |
| 1251 | atomic_dec(&rcq->usecnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | if (srq) |
| 1253 | atomic_dec(&srq->usecnt); |
| 1254 | } |
| 1255 | |
| 1256 | return ret; |
| 1257 | } |
| 1258 | EXPORT_SYMBOL(ib_destroy_qp); |
| 1259 | |
| 1260 | /* Completion queues */ |
| 1261 | |
| 1262 | struct ib_cq *ib_create_cq(struct ib_device *device, |
| 1263 | ib_comp_handler comp_handler, |
| 1264 | void (*event_handler)(struct ib_event *, void *), |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 1265 | void *cq_context, |
| 1266 | const struct ib_cq_init_attr *cq_attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | { |
| 1268 | struct ib_cq *cq; |
| 1269 | |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 1270 | cq = device->create_cq(device, cq_attr, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
| 1272 | if (!IS_ERR(cq)) { |
| 1273 | cq->device = device; |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 1274 | cq->uobject = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | cq->comp_handler = comp_handler; |
| 1276 | cq->event_handler = event_handler; |
| 1277 | cq->cq_context = cq_context; |
| 1278 | atomic_set(&cq->usecnt, 0); |
| 1279 | } |
| 1280 | |
| 1281 | return cq; |
| 1282 | } |
| 1283 | EXPORT_SYMBOL(ib_create_cq); |
| 1284 | |
Eli Cohen | 2dd5716 | 2008-04-16 21:09:33 -0700 | [diff] [blame] | 1285 | int ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period) |
| 1286 | { |
| 1287 | return cq->device->modify_cq ? |
| 1288 | cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS; |
| 1289 | } |
| 1290 | EXPORT_SYMBOL(ib_modify_cq); |
| 1291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | int ib_destroy_cq(struct ib_cq *cq) |
| 1293 | { |
| 1294 | if (atomic_read(&cq->usecnt)) |
| 1295 | return -EBUSY; |
| 1296 | |
| 1297 | return cq->device->destroy_cq(cq); |
| 1298 | } |
| 1299 | EXPORT_SYMBOL(ib_destroy_cq); |
| 1300 | |
Roland Dreier | a74cd4a | 2006-02-13 16:30:49 -0800 | [diff] [blame] | 1301 | int ib_resize_cq(struct ib_cq *cq, int cqe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | { |
Roland Dreier | 40de2e5 | 2005-11-08 11:10:25 -0800 | [diff] [blame] | 1303 | return cq->device->resize_cq ? |
Roland Dreier | 33b9b3e | 2006-01-30 14:29:21 -0800 | [diff] [blame] | 1304 | cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | } |
| 1306 | EXPORT_SYMBOL(ib_resize_cq); |
| 1307 | |
| 1308 | /* Memory regions */ |
| 1309 | |
| 1310 | struct ib_mr *ib_get_dma_mr(struct ib_pd *pd, int mr_access_flags) |
| 1311 | { |
| 1312 | struct ib_mr *mr; |
Eli Cohen | 1c636f8 | 2013-10-31 15:26:32 +0200 | [diff] [blame] | 1313 | int err; |
| 1314 | |
| 1315 | err = ib_check_mr_access(mr_access_flags); |
| 1316 | if (err) |
| 1317 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
| 1319 | mr = pd->device->get_dma_mr(pd, mr_access_flags); |
| 1320 | |
| 1321 | if (!IS_ERR(mr)) { |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 1322 | mr->device = pd->device; |
| 1323 | mr->pd = pd; |
| 1324 | mr->uobject = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | atomic_inc(&pd->usecnt); |
| 1326 | atomic_set(&mr->usecnt, 0); |
| 1327 | } |
| 1328 | |
| 1329 | return mr; |
| 1330 | } |
| 1331 | EXPORT_SYMBOL(ib_get_dma_mr); |
| 1332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | int ib_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr) |
| 1334 | { |
| 1335 | return mr->device->query_mr ? |
| 1336 | mr->device->query_mr(mr, mr_attr) : -ENOSYS; |
| 1337 | } |
| 1338 | EXPORT_SYMBOL(ib_query_mr); |
| 1339 | |
| 1340 | int ib_dereg_mr(struct ib_mr *mr) |
| 1341 | { |
| 1342 | struct ib_pd *pd; |
| 1343 | int ret; |
| 1344 | |
| 1345 | if (atomic_read(&mr->usecnt)) |
| 1346 | return -EBUSY; |
| 1347 | |
| 1348 | pd = mr->pd; |
| 1349 | ret = mr->device->dereg_mr(mr); |
| 1350 | if (!ret) |
| 1351 | atomic_dec(&pd->usecnt); |
| 1352 | |
| 1353 | return ret; |
| 1354 | } |
| 1355 | EXPORT_SYMBOL(ib_dereg_mr); |
| 1356 | |
Sagi Grimberg | 9bee178 | 2015-07-30 10:32:35 +0300 | [diff] [blame] | 1357 | /** |
| 1358 | * ib_alloc_mr() - Allocates a memory region |
| 1359 | * @pd: protection domain associated with the region |
| 1360 | * @mr_type: memory region type |
| 1361 | * @max_num_sg: maximum sg entries available for registration. |
| 1362 | * |
| 1363 | * Notes: |
| 1364 | * Memory registeration page/sg lists must not exceed max_num_sg. |
| 1365 | * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed |
| 1366 | * max_num_sg * used_page_size. |
| 1367 | * |
| 1368 | */ |
| 1369 | struct ib_mr *ib_alloc_mr(struct ib_pd *pd, |
| 1370 | enum ib_mr_type mr_type, |
| 1371 | u32 max_num_sg) |
Sagi Grimberg | 17cd3a2 | 2014-02-23 14:19:04 +0200 | [diff] [blame] | 1372 | { |
| 1373 | struct ib_mr *mr; |
| 1374 | |
Sagi Grimberg | d9f272c | 2015-07-30 10:32:48 +0300 | [diff] [blame] | 1375 | if (!pd->device->alloc_mr) |
Sagi Grimberg | 17cd3a2 | 2014-02-23 14:19:04 +0200 | [diff] [blame] | 1376 | return ERR_PTR(-ENOSYS); |
| 1377 | |
Sagi Grimberg | d9f272c | 2015-07-30 10:32:48 +0300 | [diff] [blame] | 1378 | mr = pd->device->alloc_mr(pd, mr_type, max_num_sg); |
Sagi Grimberg | 17cd3a2 | 2014-02-23 14:19:04 +0200 | [diff] [blame] | 1379 | if (!IS_ERR(mr)) { |
| 1380 | mr->device = pd->device; |
| 1381 | mr->pd = pd; |
| 1382 | mr->uobject = NULL; |
| 1383 | atomic_inc(&pd->usecnt); |
| 1384 | atomic_set(&mr->usecnt, 0); |
| 1385 | } |
| 1386 | |
| 1387 | return mr; |
| 1388 | } |
Sagi Grimberg | 9bee178 | 2015-07-30 10:32:35 +0300 | [diff] [blame] | 1389 | EXPORT_SYMBOL(ib_alloc_mr); |
Steve Wise | 00f7ec3 | 2008-07-14 23:48:45 -0700 | [diff] [blame] | 1390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | /* Memory windows */ |
| 1392 | |
Shani Michaeli | 7083e42 | 2013-02-06 16:19:12 +0000 | [diff] [blame] | 1393 | struct ib_mw *ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | { |
| 1395 | struct ib_mw *mw; |
| 1396 | |
| 1397 | if (!pd->device->alloc_mw) |
| 1398 | return ERR_PTR(-ENOSYS); |
| 1399 | |
Shani Michaeli | 7083e42 | 2013-02-06 16:19:12 +0000 | [diff] [blame] | 1400 | mw = pd->device->alloc_mw(pd, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | if (!IS_ERR(mw)) { |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 1402 | mw->device = pd->device; |
| 1403 | mw->pd = pd; |
| 1404 | mw->uobject = NULL; |
Shani Michaeli | 7083e42 | 2013-02-06 16:19:12 +0000 | [diff] [blame] | 1405 | mw->type = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | atomic_inc(&pd->usecnt); |
| 1407 | } |
| 1408 | |
| 1409 | return mw; |
| 1410 | } |
| 1411 | EXPORT_SYMBOL(ib_alloc_mw); |
| 1412 | |
| 1413 | int ib_dealloc_mw(struct ib_mw *mw) |
| 1414 | { |
| 1415 | struct ib_pd *pd; |
| 1416 | int ret; |
| 1417 | |
| 1418 | pd = mw->pd; |
| 1419 | ret = mw->device->dealloc_mw(mw); |
| 1420 | if (!ret) |
| 1421 | atomic_dec(&pd->usecnt); |
| 1422 | |
| 1423 | return ret; |
| 1424 | } |
| 1425 | EXPORT_SYMBOL(ib_dealloc_mw); |
| 1426 | |
| 1427 | /* "Fast" memory regions */ |
| 1428 | |
| 1429 | struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd, |
| 1430 | int mr_access_flags, |
| 1431 | struct ib_fmr_attr *fmr_attr) |
| 1432 | { |
| 1433 | struct ib_fmr *fmr; |
| 1434 | |
| 1435 | if (!pd->device->alloc_fmr) |
| 1436 | return ERR_PTR(-ENOSYS); |
| 1437 | |
| 1438 | fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr); |
| 1439 | if (!IS_ERR(fmr)) { |
| 1440 | fmr->device = pd->device; |
| 1441 | fmr->pd = pd; |
| 1442 | atomic_inc(&pd->usecnt); |
| 1443 | } |
| 1444 | |
| 1445 | return fmr; |
| 1446 | } |
| 1447 | EXPORT_SYMBOL(ib_alloc_fmr); |
| 1448 | |
| 1449 | int ib_unmap_fmr(struct list_head *fmr_list) |
| 1450 | { |
| 1451 | struct ib_fmr *fmr; |
| 1452 | |
| 1453 | if (list_empty(fmr_list)) |
| 1454 | return 0; |
| 1455 | |
| 1456 | fmr = list_entry(fmr_list->next, struct ib_fmr, list); |
| 1457 | return fmr->device->unmap_fmr(fmr_list); |
| 1458 | } |
| 1459 | EXPORT_SYMBOL(ib_unmap_fmr); |
| 1460 | |
| 1461 | int ib_dealloc_fmr(struct ib_fmr *fmr) |
| 1462 | { |
| 1463 | struct ib_pd *pd; |
| 1464 | int ret; |
| 1465 | |
| 1466 | pd = fmr->pd; |
| 1467 | ret = fmr->device->dealloc_fmr(fmr); |
| 1468 | if (!ret) |
| 1469 | atomic_dec(&pd->usecnt); |
| 1470 | |
| 1471 | return ret; |
| 1472 | } |
| 1473 | EXPORT_SYMBOL(ib_dealloc_fmr); |
| 1474 | |
| 1475 | /* Multicast groups */ |
| 1476 | |
| 1477 | int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid) |
| 1478 | { |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1479 | int ret; |
| 1480 | |
Jack Morgenstein | 0c33aee | 2005-09-26 11:47:53 -0700 | [diff] [blame] | 1481 | if (!qp->device->attach_mcast) |
| 1482 | return -ENOSYS; |
| 1483 | if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD) |
| 1484 | return -EINVAL; |
| 1485 | |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1486 | ret = qp->device->attach_mcast(qp, gid, lid); |
| 1487 | if (!ret) |
| 1488 | atomic_inc(&qp->usecnt); |
| 1489 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | EXPORT_SYMBOL(ib_attach_mcast); |
| 1492 | |
| 1493 | int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid) |
| 1494 | { |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1495 | int ret; |
| 1496 | |
Jack Morgenstein | 0c33aee | 2005-09-26 11:47:53 -0700 | [diff] [blame] | 1497 | if (!qp->device->detach_mcast) |
| 1498 | return -ENOSYS; |
| 1499 | if (gid->raw[0] != 0xff || qp->qp_type != IB_QPT_UD) |
| 1500 | return -EINVAL; |
| 1501 | |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1502 | ret = qp->device->detach_mcast(qp, gid, lid); |
| 1503 | if (!ret) |
| 1504 | atomic_dec(&qp->usecnt); |
| 1505 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | } |
| 1507 | EXPORT_SYMBOL(ib_detach_mcast); |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1508 | |
| 1509 | struct ib_xrcd *ib_alloc_xrcd(struct ib_device *device) |
| 1510 | { |
| 1511 | struct ib_xrcd *xrcd; |
| 1512 | |
| 1513 | if (!device->alloc_xrcd) |
| 1514 | return ERR_PTR(-ENOSYS); |
| 1515 | |
| 1516 | xrcd = device->alloc_xrcd(device, NULL, NULL); |
| 1517 | if (!IS_ERR(xrcd)) { |
| 1518 | xrcd->device = device; |
Sean Hefty | 53d0bd1 | 2011-05-24 08:33:46 -0700 | [diff] [blame] | 1519 | xrcd->inode = NULL; |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1520 | atomic_set(&xrcd->usecnt, 0); |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 1521 | mutex_init(&xrcd->tgt_qp_mutex); |
| 1522 | INIT_LIST_HEAD(&xrcd->tgt_qp_list); |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | return xrcd; |
| 1526 | } |
| 1527 | EXPORT_SYMBOL(ib_alloc_xrcd); |
| 1528 | |
| 1529 | int ib_dealloc_xrcd(struct ib_xrcd *xrcd) |
| 1530 | { |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 1531 | struct ib_qp *qp; |
| 1532 | int ret; |
| 1533 | |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1534 | if (atomic_read(&xrcd->usecnt)) |
| 1535 | return -EBUSY; |
| 1536 | |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 1537 | while (!list_empty(&xrcd->tgt_qp_list)) { |
| 1538 | qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list); |
| 1539 | ret = ib_destroy_qp(qp); |
| 1540 | if (ret) |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1544 | return xrcd->device->dealloc_xrcd(xrcd); |
| 1545 | } |
| 1546 | EXPORT_SYMBOL(ib_dealloc_xrcd); |
Hadar Hen Zion | 319a441 | 2013-08-07 14:01:59 +0300 | [diff] [blame] | 1547 | |
| 1548 | struct ib_flow *ib_create_flow(struct ib_qp *qp, |
| 1549 | struct ib_flow_attr *flow_attr, |
| 1550 | int domain) |
| 1551 | { |
| 1552 | struct ib_flow *flow_id; |
| 1553 | if (!qp->device->create_flow) |
| 1554 | return ERR_PTR(-ENOSYS); |
| 1555 | |
| 1556 | flow_id = qp->device->create_flow(qp, flow_attr, domain); |
| 1557 | if (!IS_ERR(flow_id)) |
| 1558 | atomic_inc(&qp->usecnt); |
| 1559 | return flow_id; |
| 1560 | } |
| 1561 | EXPORT_SYMBOL(ib_create_flow); |
| 1562 | |
| 1563 | int ib_destroy_flow(struct ib_flow *flow_id) |
| 1564 | { |
| 1565 | int err; |
| 1566 | struct ib_qp *qp = flow_id->qp; |
| 1567 | |
| 1568 | err = qp->device->destroy_flow(flow_id); |
| 1569 | if (!err) |
| 1570 | atomic_dec(&qp->usecnt); |
| 1571 | return err; |
| 1572 | } |
| 1573 | EXPORT_SYMBOL(ib_destroy_flow); |
Sagi Grimberg | 1b01d33 | 2014-02-23 14:19:05 +0200 | [diff] [blame] | 1574 | |
| 1575 | int ib_check_mr_status(struct ib_mr *mr, u32 check_mask, |
| 1576 | struct ib_mr_status *mr_status) |
| 1577 | { |
| 1578 | return mr->device->check_mr_status ? |
| 1579 | mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS; |
| 1580 | } |
| 1581 | EXPORT_SYMBOL(ib_check_mr_status); |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 1582 | |
| 1583 | /** |
| 1584 | * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list |
| 1585 | * and set it the memory region. |
| 1586 | * @mr: memory region |
| 1587 | * @sg: dma mapped scatterlist |
| 1588 | * @sg_nents: number of entries in sg |
| 1589 | * @page_size: page vector desired page size |
| 1590 | * |
| 1591 | * Constraints: |
| 1592 | * - The first sg element is allowed to have an offset. |
| 1593 | * - Each sg element must be aligned to page_size (or physically |
| 1594 | * contiguous to the previous element). In case an sg element has a |
| 1595 | * non contiguous offset, the mapping prefix will not include it. |
| 1596 | * - The last sg element is allowed to have length less than page_size. |
| 1597 | * - If sg_nents total byte length exceeds the mr max_num_sge * page_size |
| 1598 | * then only max_num_sg entries will be mapped. |
| 1599 | * |
| 1600 | * Returns the number of sg elements that were mapped to the memory region. |
| 1601 | * |
| 1602 | * After this completes successfully, the memory region |
| 1603 | * is ready for registration. |
| 1604 | */ |
| 1605 | int ib_map_mr_sg(struct ib_mr *mr, |
| 1606 | struct scatterlist *sg, |
| 1607 | int sg_nents, |
| 1608 | unsigned int page_size) |
| 1609 | { |
| 1610 | if (unlikely(!mr->device->map_mr_sg)) |
| 1611 | return -ENOSYS; |
| 1612 | |
| 1613 | mr->page_size = page_size; |
| 1614 | |
| 1615 | return mr->device->map_mr_sg(mr, sg, sg_nents); |
| 1616 | } |
| 1617 | EXPORT_SYMBOL(ib_map_mr_sg); |
| 1618 | |
| 1619 | /** |
| 1620 | * ib_sg_to_pages() - Convert the largest prefix of a sg list |
| 1621 | * to a page vector |
| 1622 | * @mr: memory region |
| 1623 | * @sgl: dma mapped scatterlist |
| 1624 | * @sg_nents: number of entries in sg |
| 1625 | * @set_page: driver page assignment function pointer |
| 1626 | * |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 1627 | * Core service helper for drivers to convert the largest |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 1628 | * prefix of given sg list to a page vector. The sg list |
| 1629 | * prefix converted is the prefix that meet the requirements |
| 1630 | * of ib_map_mr_sg. |
| 1631 | * |
| 1632 | * Returns the number of sg elements that were assigned to |
| 1633 | * a page vector. |
| 1634 | */ |
| 1635 | int ib_sg_to_pages(struct ib_mr *mr, |
| 1636 | struct scatterlist *sgl, |
| 1637 | int sg_nents, |
| 1638 | int (*set_page)(struct ib_mr *, u64)) |
| 1639 | { |
| 1640 | struct scatterlist *sg; |
| 1641 | u64 last_end_dma_addr = 0, last_page_addr = 0; |
| 1642 | unsigned int last_page_off = 0; |
| 1643 | u64 page_mask = ~((u64)mr->page_size - 1); |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 1644 | int i, ret; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 1645 | |
| 1646 | mr->iova = sg_dma_address(&sgl[0]); |
| 1647 | mr->length = 0; |
| 1648 | |
| 1649 | for_each_sg(sgl, sg, sg_nents, i) { |
| 1650 | u64 dma_addr = sg_dma_address(sg); |
| 1651 | unsigned int dma_len = sg_dma_len(sg); |
| 1652 | u64 end_dma_addr = dma_addr + dma_len; |
| 1653 | u64 page_addr = dma_addr & page_mask; |
| 1654 | |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 1655 | /* |
| 1656 | * For the second and later elements, check whether either the |
| 1657 | * end of element i-1 or the start of element i is not aligned |
| 1658 | * on a page boundary. |
| 1659 | */ |
| 1660 | if (i && (last_page_off != 0 || page_addr != dma_addr)) { |
| 1661 | /* Stop mapping if there is a gap. */ |
| 1662 | if (last_end_dma_addr != dma_addr) |
| 1663 | break; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 1664 | |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 1665 | /* |
| 1666 | * Coalesce this element with the last. If it is small |
| 1667 | * enough just update mr->length. Otherwise start |
| 1668 | * mapping from the next page. |
| 1669 | */ |
| 1670 | goto next_page; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | do { |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 1674 | ret = set_page(mr, page_addr); |
| 1675 | if (unlikely(ret < 0)) |
| 1676 | return i ? : ret; |
| 1677 | next_page: |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 1678 | page_addr += mr->page_size; |
| 1679 | } while (page_addr < end_dma_addr); |
| 1680 | |
| 1681 | mr->length += dma_len; |
| 1682 | last_end_dma_addr = end_dma_addr; |
| 1683 | last_page_addr = end_dma_addr & page_mask; |
| 1684 | last_page_off = end_dma_addr & ~page_mask; |
| 1685 | } |
| 1686 | |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 1687 | return i; |
| 1688 | } |
| 1689 | EXPORT_SYMBOL(ib_sg_to_pages); |