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> |
Daniel Jurgens | d291f1a | 2017-05-19 15:48:52 +0300 | [diff] [blame] | 47 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 49 | #include <rdma/ib_verbs.h> |
| 50 | #include <rdma/ib_cache.h> |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 51 | #include <rdma/ib_addr.h> |
Christoph Hellwig | a060b56 | 2016-05-03 18:01:09 +0200 | [diff] [blame] | 52 | #include <rdma/rw.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 54 | #include "core_priv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Parav Pandit | c0348eb | 2017-10-16 08:45:13 +0300 | [diff] [blame] | 56 | static int ib_resolve_eth_dmac(struct ib_device *device, |
| 57 | struct rdma_ah_attr *ah_attr); |
| 58 | |
Sagi Grimberg | 2b1b5b6 | 2015-05-18 13:40:28 +0300 | [diff] [blame] | 59 | static const char * const ib_events[] = { |
| 60 | [IB_EVENT_CQ_ERR] = "CQ error", |
| 61 | [IB_EVENT_QP_FATAL] = "QP fatal error", |
| 62 | [IB_EVENT_QP_REQ_ERR] = "QP request error", |
| 63 | [IB_EVENT_QP_ACCESS_ERR] = "QP access error", |
| 64 | [IB_EVENT_COMM_EST] = "communication established", |
| 65 | [IB_EVENT_SQ_DRAINED] = "send queue drained", |
| 66 | [IB_EVENT_PATH_MIG] = "path migration successful", |
| 67 | [IB_EVENT_PATH_MIG_ERR] = "path migration error", |
| 68 | [IB_EVENT_DEVICE_FATAL] = "device fatal error", |
| 69 | [IB_EVENT_PORT_ACTIVE] = "port active", |
| 70 | [IB_EVENT_PORT_ERR] = "port error", |
| 71 | [IB_EVENT_LID_CHANGE] = "LID change", |
| 72 | [IB_EVENT_PKEY_CHANGE] = "P_key change", |
| 73 | [IB_EVENT_SM_CHANGE] = "SM change", |
| 74 | [IB_EVENT_SRQ_ERR] = "SRQ error", |
| 75 | [IB_EVENT_SRQ_LIMIT_REACHED] = "SRQ limit reached", |
| 76 | [IB_EVENT_QP_LAST_WQE_REACHED] = "last WQE reached", |
| 77 | [IB_EVENT_CLIENT_REREGISTER] = "client reregister", |
| 78 | [IB_EVENT_GID_CHANGE] = "GID changed", |
| 79 | }; |
| 80 | |
Bart Van Assche | db7489e | 2015-08-03 10:01:52 -0700 | [diff] [blame] | 81 | const char *__attribute_const__ ib_event_msg(enum ib_event_type event) |
Sagi Grimberg | 2b1b5b6 | 2015-05-18 13:40:28 +0300 | [diff] [blame] | 82 | { |
| 83 | size_t index = event; |
| 84 | |
| 85 | return (index < ARRAY_SIZE(ib_events) && ib_events[index]) ? |
| 86 | ib_events[index] : "unrecognized event"; |
| 87 | } |
| 88 | EXPORT_SYMBOL(ib_event_msg); |
| 89 | |
| 90 | static const char * const wc_statuses[] = { |
| 91 | [IB_WC_SUCCESS] = "success", |
| 92 | [IB_WC_LOC_LEN_ERR] = "local length error", |
| 93 | [IB_WC_LOC_QP_OP_ERR] = "local QP operation error", |
| 94 | [IB_WC_LOC_EEC_OP_ERR] = "local EE context operation error", |
| 95 | [IB_WC_LOC_PROT_ERR] = "local protection error", |
| 96 | [IB_WC_WR_FLUSH_ERR] = "WR flushed", |
| 97 | [IB_WC_MW_BIND_ERR] = "memory management operation error", |
| 98 | [IB_WC_BAD_RESP_ERR] = "bad response error", |
| 99 | [IB_WC_LOC_ACCESS_ERR] = "local access error", |
| 100 | [IB_WC_REM_INV_REQ_ERR] = "invalid request error", |
| 101 | [IB_WC_REM_ACCESS_ERR] = "remote access error", |
| 102 | [IB_WC_REM_OP_ERR] = "remote operation error", |
| 103 | [IB_WC_RETRY_EXC_ERR] = "transport retry counter exceeded", |
| 104 | [IB_WC_RNR_RETRY_EXC_ERR] = "RNR retry counter exceeded", |
| 105 | [IB_WC_LOC_RDD_VIOL_ERR] = "local RDD violation error", |
| 106 | [IB_WC_REM_INV_RD_REQ_ERR] = "remote invalid RD request", |
| 107 | [IB_WC_REM_ABORT_ERR] = "operation aborted", |
| 108 | [IB_WC_INV_EECN_ERR] = "invalid EE context number", |
| 109 | [IB_WC_INV_EEC_STATE_ERR] = "invalid EE context state", |
| 110 | [IB_WC_FATAL_ERR] = "fatal error", |
| 111 | [IB_WC_RESP_TIMEOUT_ERR] = "response timeout error", |
| 112 | [IB_WC_GENERAL_ERR] = "general error", |
| 113 | }; |
| 114 | |
Bart Van Assche | db7489e | 2015-08-03 10:01:52 -0700 | [diff] [blame] | 115 | 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] | 116 | { |
| 117 | size_t index = status; |
| 118 | |
| 119 | return (index < ARRAY_SIZE(wc_statuses) && wc_statuses[index]) ? |
| 120 | wc_statuses[index] : "unrecognized status"; |
| 121 | } |
| 122 | EXPORT_SYMBOL(ib_wc_status_msg); |
| 123 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 124 | __attribute_const__ int ib_rate_to_mult(enum ib_rate rate) |
Jack Morgenstein | bf6a9e3 | 2006-04-10 09:43:47 -0700 | [diff] [blame] | 125 | { |
| 126 | switch (rate) { |
Hans Westgaard Ry | e2dda36 | 2018-01-02 14:50:40 +0100 | [diff] [blame] | 127 | case IB_RATE_2_5_GBPS: return 1; |
| 128 | case IB_RATE_5_GBPS: return 2; |
| 129 | case IB_RATE_10_GBPS: return 4; |
| 130 | case IB_RATE_20_GBPS: return 8; |
| 131 | case IB_RATE_30_GBPS: return 12; |
| 132 | case IB_RATE_40_GBPS: return 16; |
| 133 | case IB_RATE_60_GBPS: return 24; |
| 134 | case IB_RATE_80_GBPS: return 32; |
| 135 | case IB_RATE_120_GBPS: return 48; |
| 136 | case IB_RATE_14_GBPS: return 6; |
| 137 | case IB_RATE_56_GBPS: return 22; |
| 138 | case IB_RATE_112_GBPS: return 45; |
| 139 | case IB_RATE_168_GBPS: return 67; |
| 140 | case IB_RATE_25_GBPS: return 10; |
| 141 | case IB_RATE_100_GBPS: return 40; |
| 142 | case IB_RATE_200_GBPS: return 80; |
| 143 | case IB_RATE_300_GBPS: return 120; |
| 144 | default: return -1; |
Jack Morgenstein | bf6a9e3 | 2006-04-10 09:43:47 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | EXPORT_SYMBOL(ib_rate_to_mult); |
| 148 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 149 | __attribute_const__ enum ib_rate mult_to_ib_rate(int mult) |
Jack Morgenstein | bf6a9e3 | 2006-04-10 09:43:47 -0700 | [diff] [blame] | 150 | { |
| 151 | switch (mult) { |
Hans Westgaard Ry | e2dda36 | 2018-01-02 14:50:40 +0100 | [diff] [blame] | 152 | case 1: return IB_RATE_2_5_GBPS; |
| 153 | case 2: return IB_RATE_5_GBPS; |
| 154 | case 4: return IB_RATE_10_GBPS; |
| 155 | case 8: return IB_RATE_20_GBPS; |
| 156 | case 12: return IB_RATE_30_GBPS; |
| 157 | case 16: return IB_RATE_40_GBPS; |
| 158 | case 24: return IB_RATE_60_GBPS; |
| 159 | case 32: return IB_RATE_80_GBPS; |
| 160 | case 48: return IB_RATE_120_GBPS; |
| 161 | case 6: return IB_RATE_14_GBPS; |
| 162 | case 22: return IB_RATE_56_GBPS; |
| 163 | case 45: return IB_RATE_112_GBPS; |
| 164 | case 67: return IB_RATE_168_GBPS; |
| 165 | case 10: return IB_RATE_25_GBPS; |
| 166 | case 40: return IB_RATE_100_GBPS; |
| 167 | case 80: return IB_RATE_200_GBPS; |
| 168 | case 120: return IB_RATE_300_GBPS; |
| 169 | default: return IB_RATE_PORT_CURRENT; |
Jack Morgenstein | bf6a9e3 | 2006-04-10 09:43:47 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | EXPORT_SYMBOL(mult_to_ib_rate); |
| 173 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 174 | __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate) |
Marcel Apfelbaum | 71eeba1 | 2011-10-05 14:21:47 +0300 | [diff] [blame] | 175 | { |
| 176 | switch (rate) { |
| 177 | case IB_RATE_2_5_GBPS: return 2500; |
| 178 | case IB_RATE_5_GBPS: return 5000; |
| 179 | case IB_RATE_10_GBPS: return 10000; |
| 180 | case IB_RATE_20_GBPS: return 20000; |
| 181 | case IB_RATE_30_GBPS: return 30000; |
| 182 | case IB_RATE_40_GBPS: return 40000; |
| 183 | case IB_RATE_60_GBPS: return 60000; |
| 184 | case IB_RATE_80_GBPS: return 80000; |
| 185 | case IB_RATE_120_GBPS: return 120000; |
| 186 | case IB_RATE_14_GBPS: return 14062; |
| 187 | case IB_RATE_56_GBPS: return 56250; |
| 188 | case IB_RATE_112_GBPS: return 112500; |
| 189 | case IB_RATE_168_GBPS: return 168750; |
| 190 | case IB_RATE_25_GBPS: return 25781; |
| 191 | case IB_RATE_100_GBPS: return 103125; |
| 192 | case IB_RATE_200_GBPS: return 206250; |
| 193 | case IB_RATE_300_GBPS: return 309375; |
| 194 | default: return -1; |
| 195 | } |
| 196 | } |
| 197 | EXPORT_SYMBOL(ib_rate_to_mbps); |
| 198 | |
Roland Dreier | 8385fd8 | 2014-06-04 10:00:16 -0700 | [diff] [blame] | 199 | __attribute_const__ enum rdma_transport_type |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 200 | rdma_node_get_transport(enum rdma_node_type node_type) |
| 201 | { |
Leon Romanovsky | cdc596d | 2017-08-17 15:50:38 +0300 | [diff] [blame] | 202 | |
| 203 | if (node_type == RDMA_NODE_USNIC) |
Upinder Malhi | 5db5765 | 2014-01-15 17:02:36 -0800 | [diff] [blame] | 204 | return RDMA_TRANSPORT_USNIC; |
Leon Romanovsky | cdc596d | 2017-08-17 15:50:38 +0300 | [diff] [blame] | 205 | if (node_type == RDMA_NODE_USNIC_UDP) |
Upinder Malhi | 248567f | 2014-01-09 14:48:19 -0800 | [diff] [blame] | 206 | return RDMA_TRANSPORT_USNIC_UDP; |
Leon Romanovsky | cdc596d | 2017-08-17 15:50:38 +0300 | [diff] [blame] | 207 | if (node_type == RDMA_NODE_RNIC) |
| 208 | return RDMA_TRANSPORT_IWARP; |
| 209 | |
| 210 | return RDMA_TRANSPORT_IB; |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 211 | } |
| 212 | EXPORT_SYMBOL(rdma_node_get_transport); |
| 213 | |
Eli Cohen | a3f5ada | 2010-09-27 17:51:10 -0700 | [diff] [blame] | 214 | enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device, u8 port_num) |
| 215 | { |
Leon Romanovsky | 82901e3 | 2017-08-17 15:50:39 +0300 | [diff] [blame] | 216 | enum rdma_transport_type lt; |
Eli Cohen | a3f5ada | 2010-09-27 17:51:10 -0700 | [diff] [blame] | 217 | if (device->get_link_layer) |
| 218 | return device->get_link_layer(device, port_num); |
| 219 | |
Leon Romanovsky | 82901e3 | 2017-08-17 15:50:39 +0300 | [diff] [blame] | 220 | lt = rdma_node_get_transport(device->node_type); |
| 221 | if (lt == RDMA_TRANSPORT_IB) |
Eli Cohen | a3f5ada | 2010-09-27 17:51:10 -0700 | [diff] [blame] | 222 | return IB_LINK_LAYER_INFINIBAND; |
Leon Romanovsky | 82901e3 | 2017-08-17 15:50:39 +0300 | [diff] [blame] | 223 | |
| 224 | return IB_LINK_LAYER_ETHERNET; |
Eli Cohen | a3f5ada | 2010-09-27 17:51:10 -0700 | [diff] [blame] | 225 | } |
| 226 | EXPORT_SYMBOL(rdma_port_get_link_layer); |
| 227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | /* Protection domains */ |
| 229 | |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 230 | /** |
| 231 | * ib_alloc_pd - Allocates an unused protection domain. |
| 232 | * @device: The device on which to allocate the protection domain. |
| 233 | * |
| 234 | * A protection domain object provides an association between QPs, shared |
| 235 | * receive queues, address handles, memory regions, and memory windows. |
| 236 | * |
| 237 | * Every PD has a local_dma_lkey which can be used as the lkey value for local |
| 238 | * memory operations. |
| 239 | */ |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 240 | struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags, |
| 241 | const char *caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
| 243 | struct ib_pd *pd; |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 244 | int mr_access_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 246 | pd = device->alloc_pd(device, NULL, NULL); |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 247 | if (IS_ERR(pd)) |
| 248 | return pd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 250 | pd->device = device; |
| 251 | pd->uobject = NULL; |
Christoph Hellwig | 50d4633 | 2016-09-05 12:56:16 +0200 | [diff] [blame] | 252 | pd->__internal_mr = NULL; |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 253 | atomic_set(&pd->usecnt, 0); |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 254 | pd->flags = flags; |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 255 | |
Or Gerlitz | 86bee4c | 2015-12-18 10:59:45 +0200 | [diff] [blame] | 256 | if (device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY) |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 257 | pd->local_dma_lkey = device->local_dma_lkey; |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 258 | else |
| 259 | mr_access_flags |= IB_ACCESS_LOCAL_WRITE; |
| 260 | |
| 261 | if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) { |
| 262 | pr_warn("%s: enabling unsafe global rkey\n", caller); |
| 263 | mr_access_flags |= IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE; |
| 264 | } |
| 265 | |
| 266 | if (mr_access_flags) { |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 267 | struct ib_mr *mr; |
| 268 | |
Christoph Hellwig | 5ef990f | 2016-09-05 12:56:21 +0200 | [diff] [blame] | 269 | mr = pd->device->get_dma_mr(pd, mr_access_flags); |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 270 | if (IS_ERR(mr)) { |
| 271 | ib_dealloc_pd(pd); |
Christoph Hellwig | 5ef990f | 2016-09-05 12:56:21 +0200 | [diff] [blame] | 272 | return ERR_CAST(mr); |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 273 | } |
| 274 | |
Christoph Hellwig | 5ef990f | 2016-09-05 12:56:21 +0200 | [diff] [blame] | 275 | mr->device = pd->device; |
| 276 | mr->pd = pd; |
| 277 | mr->uobject = NULL; |
| 278 | mr->need_inval = false; |
| 279 | |
Christoph Hellwig | 50d4633 | 2016-09-05 12:56:16 +0200 | [diff] [blame] | 280 | pd->__internal_mr = mr; |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 281 | |
| 282 | if (!(device->attrs.device_cap_flags & IB_DEVICE_LOCAL_DMA_LKEY)) |
| 283 | pd->local_dma_lkey = pd->__internal_mr->lkey; |
| 284 | |
| 285 | if (flags & IB_PD_UNSAFE_GLOBAL_RKEY) |
| 286 | pd->unsafe_global_rkey = pd->__internal_mr->rkey; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return pd; |
| 290 | } |
Christoph Hellwig | ed082d3 | 2016-09-05 12:56:17 +0200 | [diff] [blame] | 291 | EXPORT_SYMBOL(__ib_alloc_pd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 293 | /** |
| 294 | * ib_dealloc_pd - Deallocates a protection domain. |
| 295 | * @pd: The protection domain to deallocate. |
| 296 | * |
| 297 | * It is an error to call this function while any resources in the pd still |
| 298 | * exist. The caller is responsible to synchronously destroy them and |
| 299 | * guarantee no new allocations will happen. |
| 300 | */ |
| 301 | void ib_dealloc_pd(struct ib_pd *pd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | { |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 303 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Christoph Hellwig | 50d4633 | 2016-09-05 12:56:16 +0200 | [diff] [blame] | 305 | if (pd->__internal_mr) { |
Christoph Hellwig | 5ef990f | 2016-09-05 12:56:21 +0200 | [diff] [blame] | 306 | ret = pd->device->dereg_mr(pd->__internal_mr); |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 307 | WARN_ON(ret); |
Christoph Hellwig | 50d4633 | 2016-09-05 12:56:16 +0200 | [diff] [blame] | 308 | pd->__internal_mr = NULL; |
Jason Gunthorpe | 96249d7 | 2015-08-05 14:14:45 -0600 | [diff] [blame] | 309 | } |
| 310 | |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 311 | /* uverbs manipulates usecnt with proper locking, while the kabi |
| 312 | requires the caller to guarantee we can't race here. */ |
| 313 | WARN_ON(atomic_read(&pd->usecnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Jason Gunthorpe | 7dd7864 | 2015-08-05 14:34:31 -0600 | [diff] [blame] | 315 | /* Making delalloc_pd a void return is a WIP, no driver should return |
| 316 | an error here. */ |
| 317 | ret = pd->device->dealloc_pd(pd); |
| 318 | WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | EXPORT_SYMBOL(ib_dealloc_pd); |
| 321 | |
| 322 | /* Address handles */ |
| 323 | |
Parav Pandit | 5cda658 | 2017-10-16 08:45:12 +0300 | [diff] [blame] | 324 | static struct ib_ah *_rdma_create_ah(struct ib_pd *pd, |
| 325 | struct rdma_ah_attr *ah_attr, |
| 326 | struct ib_udata *udata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
| 328 | struct ib_ah *ah; |
| 329 | |
Parav Pandit | 5cda658 | 2017-10-16 08:45:12 +0300 | [diff] [blame] | 330 | ah = pd->device->create_ah(pd, ah_attr, udata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | if (!IS_ERR(ah)) { |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 333 | ah->device = pd->device; |
| 334 | ah->pd = pd; |
| 335 | ah->uobject = NULL; |
Dasaratharaman Chandramouli | 44c5848 | 2017-04-29 14:41:29 -0400 | [diff] [blame] | 336 | ah->type = ah_attr->type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | atomic_inc(&pd->usecnt); |
| 338 | } |
| 339 | |
| 340 | return ah; |
| 341 | } |
Parav Pandit | 5cda658 | 2017-10-16 08:45:12 +0300 | [diff] [blame] | 342 | |
| 343 | struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr) |
| 344 | { |
| 345 | return _rdma_create_ah(pd, ah_attr, NULL); |
| 346 | } |
Dasaratharaman Chandramouli | 0a18cfe | 2017-04-29 14:41:19 -0400 | [diff] [blame] | 347 | EXPORT_SYMBOL(rdma_create_ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Parav Pandit | 5cda658 | 2017-10-16 08:45:12 +0300 | [diff] [blame] | 349 | /** |
| 350 | * rdma_create_user_ah - Creates an address handle for the |
| 351 | * given address vector. |
| 352 | * It resolves destination mac address for ah attribute of RoCE type. |
| 353 | * @pd: The protection domain associated with the address handle. |
| 354 | * @ah_attr: The attributes of the address vector. |
| 355 | * @udata: pointer to user's input output buffer information need by |
| 356 | * provider driver. |
| 357 | * |
| 358 | * It returns 0 on success and returns appropriate error code on error. |
| 359 | * The address handle is used to reference a local or global destination |
| 360 | * in all UD QP post sends. |
| 361 | */ |
| 362 | struct ib_ah *rdma_create_user_ah(struct ib_pd *pd, |
| 363 | struct rdma_ah_attr *ah_attr, |
| 364 | struct ib_udata *udata) |
| 365 | { |
| 366 | int err; |
| 367 | |
| 368 | if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) { |
| 369 | err = ib_resolve_eth_dmac(pd->device, ah_attr); |
| 370 | if (err) |
| 371 | return ERR_PTR(err); |
| 372 | } |
| 373 | |
| 374 | return _rdma_create_ah(pd, ah_attr, udata); |
| 375 | } |
| 376 | EXPORT_SYMBOL(rdma_create_user_ah); |
| 377 | |
Moni Shoua | 850d8fd | 2016-11-10 11:30:56 +0200 | [diff] [blame] | 378 | int ib_get_rdma_header_version(const union rdma_network_hdr *hdr) |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 379 | { |
| 380 | const struct iphdr *ip4h = (struct iphdr *)&hdr->roce4grh; |
| 381 | struct iphdr ip4h_checked; |
| 382 | const struct ipv6hdr *ip6h = (struct ipv6hdr *)&hdr->ibgrh; |
| 383 | |
| 384 | /* If it's IPv6, the version must be 6, otherwise, the first |
| 385 | * 20 bytes (before the IPv4 header) are garbled. |
| 386 | */ |
| 387 | if (ip6h->version != 6) |
| 388 | return (ip4h->version == 4) ? 4 : 0; |
| 389 | /* version may be 6 or 4 because the first 20 bytes could be garbled */ |
| 390 | |
| 391 | /* RoCE v2 requires no options, thus header length |
| 392 | * must be 5 words |
| 393 | */ |
| 394 | if (ip4h->ihl != 5) |
| 395 | return 6; |
| 396 | |
| 397 | /* Verify checksum. |
| 398 | * We can't write on scattered buffers so we need to copy to |
| 399 | * temp buffer. |
| 400 | */ |
| 401 | memcpy(&ip4h_checked, ip4h, sizeof(ip4h_checked)); |
| 402 | ip4h_checked.check = 0; |
| 403 | ip4h_checked.check = ip_fast_csum((u8 *)&ip4h_checked, 5); |
| 404 | /* if IPv4 header checksum is OK, believe it */ |
| 405 | if (ip4h->check == ip4h_checked.check) |
| 406 | return 4; |
| 407 | return 6; |
| 408 | } |
Moni Shoua | 850d8fd | 2016-11-10 11:30:56 +0200 | [diff] [blame] | 409 | EXPORT_SYMBOL(ib_get_rdma_header_version); |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 410 | |
| 411 | static enum rdma_network_type ib_get_net_type_by_grh(struct ib_device *device, |
| 412 | u8 port_num, |
| 413 | const struct ib_grh *grh) |
| 414 | { |
| 415 | int grh_version; |
| 416 | |
| 417 | if (rdma_protocol_ib(device, port_num)) |
| 418 | return RDMA_NETWORK_IB; |
| 419 | |
Moni Shoua | 850d8fd | 2016-11-10 11:30:56 +0200 | [diff] [blame] | 420 | grh_version = ib_get_rdma_header_version((union rdma_network_hdr *)grh); |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 421 | |
| 422 | if (grh_version == 4) |
| 423 | return RDMA_NETWORK_IPV4; |
| 424 | |
| 425 | if (grh->next_hdr == IPPROTO_UDP) |
| 426 | return RDMA_NETWORK_IPV6; |
| 427 | |
| 428 | return RDMA_NETWORK_ROCE_V1; |
| 429 | } |
| 430 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 431 | struct find_gid_index_context { |
| 432 | u16 vlan_id; |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 433 | enum ib_gid_type gid_type; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | static bool find_gid_index(const union ib_gid *gid, |
| 437 | const struct ib_gid_attr *gid_attr, |
| 438 | void *context) |
| 439 | { |
Parav Pandit | b0dd0d3 | 2017-11-14 14:52:04 +0200 | [diff] [blame] | 440 | struct find_gid_index_context *ctx = context; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 441 | |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 442 | if (ctx->gid_type != gid_attr->gid_type) |
| 443 | return false; |
| 444 | |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 445 | if ((!!(ctx->vlan_id != 0xffff) == !is_vlan_dev(gid_attr->ndev)) || |
| 446 | (is_vlan_dev(gid_attr->ndev) && |
| 447 | vlan_dev_vlan_id(gid_attr->ndev) != ctx->vlan_id)) |
| 448 | return false; |
| 449 | |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | static int get_sgid_index_from_eth(struct ib_device *device, u8 port_num, |
| 454 | u16 vlan_id, const union ib_gid *sgid, |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 455 | enum ib_gid_type gid_type, |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 456 | u16 *gid_index) |
| 457 | { |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 458 | struct find_gid_index_context context = {.vlan_id = vlan_id, |
| 459 | .gid_type = gid_type}; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 460 | |
| 461 | return ib_find_gid_by_filter(device, sgid, port_num, find_gid_index, |
| 462 | &context, gid_index); |
| 463 | } |
| 464 | |
Moni Shoua | 850d8fd | 2016-11-10 11:30:56 +0200 | [diff] [blame] | 465 | int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr, |
| 466 | enum rdma_network_type net_type, |
| 467 | union ib_gid *sgid, union ib_gid *dgid) |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 468 | { |
| 469 | struct sockaddr_in src_in; |
| 470 | struct sockaddr_in dst_in; |
| 471 | __be32 src_saddr, dst_saddr; |
| 472 | |
| 473 | if (!sgid || !dgid) |
| 474 | return -EINVAL; |
| 475 | |
| 476 | if (net_type == RDMA_NETWORK_IPV4) { |
| 477 | memcpy(&src_in.sin_addr.s_addr, |
| 478 | &hdr->roce4grh.saddr, 4); |
| 479 | memcpy(&dst_in.sin_addr.s_addr, |
| 480 | &hdr->roce4grh.daddr, 4); |
| 481 | src_saddr = src_in.sin_addr.s_addr; |
| 482 | dst_saddr = dst_in.sin_addr.s_addr; |
| 483 | ipv6_addr_set_v4mapped(src_saddr, |
| 484 | (struct in6_addr *)sgid); |
| 485 | ipv6_addr_set_v4mapped(dst_saddr, |
| 486 | (struct in6_addr *)dgid); |
| 487 | return 0; |
| 488 | } else if (net_type == RDMA_NETWORK_IPV6 || |
| 489 | net_type == RDMA_NETWORK_IB) { |
| 490 | *dgid = hdr->ibgrh.dgid; |
| 491 | *sgid = hdr->ibgrh.sgid; |
| 492 | return 0; |
| 493 | } else { |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | } |
Moni Shoua | 850d8fd | 2016-11-10 11:30:56 +0200 | [diff] [blame] | 497 | EXPORT_SYMBOL(ib_get_gids_from_rdma_hdr); |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 498 | |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 499 | /* Resolve destination mac address and hop limit for unicast destination |
| 500 | * GID entry, considering the source GID entry as well. |
| 501 | * ah_attribute must have have valid port_num, sgid_index. |
| 502 | */ |
| 503 | static int ib_resolve_unicast_gid_dmac(struct ib_device *device, |
| 504 | struct rdma_ah_attr *ah_attr) |
| 505 | { |
| 506 | struct ib_gid_attr sgid_attr; |
| 507 | struct ib_global_route *grh; |
| 508 | int hop_limit = 0xff; |
| 509 | union ib_gid sgid; |
| 510 | int ret; |
| 511 | |
| 512 | grh = rdma_ah_retrieve_grh(ah_attr); |
| 513 | |
| 514 | ret = ib_query_gid(device, |
| 515 | rdma_ah_get_port_num(ah_attr), |
| 516 | grh->sgid_index, |
| 517 | &sgid, &sgid_attr); |
| 518 | if (ret || !sgid_attr.ndev) { |
| 519 | if (!ret) |
| 520 | ret = -ENXIO; |
| 521 | return ret; |
| 522 | } |
| 523 | |
Parav Pandit | 56d0a7d9 | 2017-11-14 14:51:50 +0200 | [diff] [blame] | 524 | /* If destination is link local and source GID is RoCEv1, |
| 525 | * IP stack is not used. |
| 526 | */ |
| 527 | if (rdma_link_local_addr((struct in6_addr *)grh->dgid.raw) && |
| 528 | sgid_attr.gid_type == IB_GID_TYPE_ROCE) { |
| 529 | rdma_get_ll_mac((struct in6_addr *)grh->dgid.raw, |
| 530 | ah_attr->roce.dmac); |
| 531 | goto done; |
| 532 | } |
| 533 | |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 534 | ret = rdma_addr_find_l2_eth_by_grh(&sgid, &grh->dgid, |
| 535 | ah_attr->roce.dmac, |
| 536 | sgid_attr.ndev, &hop_limit); |
Parav Pandit | 56d0a7d9 | 2017-11-14 14:51:50 +0200 | [diff] [blame] | 537 | done: |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 538 | dev_put(sgid_attr.ndev); |
| 539 | |
| 540 | grh->hop_limit = hop_limit; |
| 541 | return ret; |
| 542 | } |
| 543 | |
Gustavo A. R. Silva | 28b5b3a | 2017-05-04 20:38:20 -0500 | [diff] [blame] | 544 | /* |
Parav Pandit | f6bdb14 | 2017-11-14 14:52:17 +0200 | [diff] [blame] | 545 | * This function initializes address handle attributes from the incoming packet. |
Gustavo A. R. Silva | 28b5b3a | 2017-05-04 20:38:20 -0500 | [diff] [blame] | 546 | * Incoming packet has dgid of the receiver node on which this code is |
| 547 | * getting executed and, sgid contains the GID of the sender. |
| 548 | * |
| 549 | * When resolving mac address of destination, the arrived dgid is used |
| 550 | * as sgid and, sgid is used as dgid because sgid contains destinations |
| 551 | * GID whom to respond to. |
| 552 | * |
Gustavo A. R. Silva | 28b5b3a | 2017-05-04 20:38:20 -0500 | [diff] [blame] | 553 | */ |
Parav Pandit | f6bdb14 | 2017-11-14 14:52:17 +0200 | [diff] [blame] | 554 | int ib_init_ah_attr_from_wc(struct ib_device *device, u8 port_num, |
| 555 | const struct ib_wc *wc, const struct ib_grh *grh, |
| 556 | struct rdma_ah_attr *ah_attr) |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 557 | { |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 558 | u32 flow_class; |
| 559 | u16 gid_index; |
| 560 | int ret; |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 561 | enum rdma_network_type net_type = RDMA_NETWORK_IB; |
| 562 | enum ib_gid_type gid_type = IB_GID_TYPE_IB; |
Matan Barak | c3efe75 | 2016-01-04 10:49:54 +0200 | [diff] [blame] | 563 | int hoplimit = 0xff; |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 564 | union ib_gid dgid; |
| 565 | union ib_gid sgid; |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 566 | |
Roland Dreier | 7936422 | 2017-08-29 10:34:44 -0700 | [diff] [blame] | 567 | might_sleep(); |
| 568 | |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 569 | memset(ah_attr, 0, sizeof *ah_attr); |
Dasaratharaman Chandramouli | 44c5848 | 2017-04-29 14:41:29 -0400 | [diff] [blame] | 570 | ah_attr->type = rdma_ah_find_type(device, port_num); |
Michael Wang | 227128f | 2015-05-05 14:50:40 +0200 | [diff] [blame] | 571 | if (rdma_cap_eth_ah(device, port_num)) { |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 572 | if (wc->wc_flags & IB_WC_WITH_NETWORK_HDR_TYPE) |
| 573 | net_type = wc->network_hdr_type; |
| 574 | else |
| 575 | net_type = ib_get_net_type_by_grh(device, port_num, grh); |
| 576 | gid_type = ib_network_to_gid_type(net_type); |
| 577 | } |
Moni Shoua | 850d8fd | 2016-11-10 11:30:56 +0200 | [diff] [blame] | 578 | ret = ib_get_gids_from_rdma_hdr((union rdma_network_hdr *)grh, net_type, |
| 579 | &sgid, &dgid); |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 580 | if (ret) |
| 581 | return ret; |
| 582 | |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 583 | rdma_ah_set_sl(ah_attr, wc->sl); |
| 584 | rdma_ah_set_port_num(ah_attr, port_num); |
| 585 | |
Somnath Kotur | c865f24 | 2015-12-23 14:56:51 +0200 | [diff] [blame] | 586 | if (rdma_protocol_roce(device, port_num)) { |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 587 | u16 vlan_id = wc->wc_flags & IB_WC_WITH_VLAN ? |
| 588 | wc->vlan_id : 0xffff; |
| 589 | |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 590 | if (!(wc->wc_flags & IB_WC_GRH)) |
| 591 | return -EPROTOTYPE; |
| 592 | |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 593 | ret = get_sgid_index_from_eth(device, port_num, |
| 594 | vlan_id, &dgid, |
| 595 | gid_type, &gid_index); |
Matan Barak | 2002983 | 2015-12-23 14:56:53 +0200 | [diff] [blame] | 596 | if (ret) |
| 597 | return ret; |
| 598 | |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 599 | flow_class = be32_to_cpu(grh->version_tclass_flow); |
| 600 | rdma_ah_set_grh(ah_attr, &sgid, |
| 601 | flow_class & 0xFFFFF, |
| 602 | (u8)gid_index, hoplimit, |
| 603 | (flow_class >> 20) & 0xFF); |
| 604 | return ib_resolve_unicast_gid_dmac(device, ah_attr); |
| 605 | } else { |
| 606 | rdma_ah_set_dlid(ah_attr, wc->slid); |
| 607 | rdma_ah_set_path_bits(ah_attr, wc->dlid_path_bits); |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 608 | |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 609 | if (wc->wc_flags & IB_WC_GRH) { |
Eli Cohen | b355600 | 2016-06-22 17:27:24 +0300 | [diff] [blame] | 610 | if (dgid.global.interface_id != cpu_to_be64(IB_SA_WELL_KNOWN_GUID)) { |
| 611 | ret = ib_find_cached_gid_by_port(device, &dgid, |
| 612 | IB_GID_TYPE_IB, |
| 613 | port_num, NULL, |
| 614 | &gid_index); |
| 615 | if (ret) |
| 616 | return ret; |
| 617 | } else { |
| 618 | gid_index = 0; |
| 619 | } |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 620 | |
| 621 | flow_class = be32_to_cpu(grh->version_tclass_flow); |
| 622 | rdma_ah_set_grh(ah_attr, &sgid, |
| 623 | flow_class & 0xFFFFF, |
| 624 | (u8)gid_index, hoplimit, |
| 625 | (flow_class >> 20) & 0xFF); |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 626 | } |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 627 | return 0; |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 628 | } |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 629 | } |
Parav Pandit | f6bdb14 | 2017-11-14 14:52:17 +0200 | [diff] [blame] | 630 | EXPORT_SYMBOL(ib_init_ah_attr_from_wc); |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 631 | |
Ira Weiny | 73cdaae | 2015-05-31 17:15:31 -0400 | [diff] [blame] | 632 | struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc, |
| 633 | const struct ib_grh *grh, u8 port_num) |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 634 | { |
Dasaratharaman Chandramouli | 9089885 | 2017-04-29 14:41:18 -0400 | [diff] [blame] | 635 | struct rdma_ah_attr ah_attr; |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 636 | int ret; |
| 637 | |
Parav Pandit | f6bdb14 | 2017-11-14 14:52:17 +0200 | [diff] [blame] | 638 | ret = ib_init_ah_attr_from_wc(pd->device, port_num, wc, grh, &ah_attr); |
Sean Hefty | 4e00d69 | 2006-06-17 20:37:39 -0700 | [diff] [blame] | 639 | if (ret) |
| 640 | return ERR_PTR(ret); |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 641 | |
Dasaratharaman Chandramouli | 0a18cfe | 2017-04-29 14:41:19 -0400 | [diff] [blame] | 642 | return rdma_create_ah(pd, &ah_attr); |
Hal Rosenstock | 513789e | 2005-07-27 11:45:34 -0700 | [diff] [blame] | 643 | } |
| 644 | EXPORT_SYMBOL(ib_create_ah_from_wc); |
| 645 | |
Dasaratharaman Chandramouli | 67b985b | 2017-04-29 14:41:20 -0400 | [diff] [blame] | 646 | int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { |
Dasaratharaman Chandramouli | 44c5848 | 2017-04-29 14:41:29 -0400 | [diff] [blame] | 648 | if (ah->type != ah_attr->type) |
| 649 | return -EINVAL; |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | return ah->device->modify_ah ? |
| 652 | ah->device->modify_ah(ah, ah_attr) : |
| 653 | -ENOSYS; |
| 654 | } |
Dasaratharaman Chandramouli | 67b985b | 2017-04-29 14:41:20 -0400 | [diff] [blame] | 655 | EXPORT_SYMBOL(rdma_modify_ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Dasaratharaman Chandramouli | bfbfd66 | 2017-04-29 14:41:21 -0400 | [diff] [blame] | 657 | int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
| 659 | return ah->device->query_ah ? |
| 660 | ah->device->query_ah(ah, ah_attr) : |
| 661 | -ENOSYS; |
| 662 | } |
Dasaratharaman Chandramouli | bfbfd66 | 2017-04-29 14:41:21 -0400 | [diff] [blame] | 663 | EXPORT_SYMBOL(rdma_query_ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Dasaratharaman Chandramouli | 3652315 | 2017-04-29 14:41:22 -0400 | [diff] [blame] | 665 | int rdma_destroy_ah(struct ib_ah *ah) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
| 667 | struct ib_pd *pd; |
| 668 | int ret; |
| 669 | |
| 670 | pd = ah->pd; |
| 671 | ret = ah->device->destroy_ah(ah); |
| 672 | if (!ret) |
| 673 | atomic_dec(&pd->usecnt); |
| 674 | |
| 675 | return ret; |
| 676 | } |
Dasaratharaman Chandramouli | 3652315 | 2017-04-29 14:41:22 -0400 | [diff] [blame] | 677 | EXPORT_SYMBOL(rdma_destroy_ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 679 | /* Shared receive queues */ |
| 680 | |
| 681 | struct ib_srq *ib_create_srq(struct ib_pd *pd, |
| 682 | struct ib_srq_init_attr *srq_init_attr) |
| 683 | { |
| 684 | struct ib_srq *srq; |
| 685 | |
| 686 | if (!pd->device->create_srq) |
| 687 | return ERR_PTR(-ENOSYS); |
| 688 | |
| 689 | srq = pd->device->create_srq(pd, srq_init_attr, NULL); |
| 690 | |
| 691 | if (!IS_ERR(srq)) { |
| 692 | srq->device = pd->device; |
| 693 | srq->pd = pd; |
| 694 | srq->uobject = NULL; |
| 695 | srq->event_handler = srq_init_attr->event_handler; |
| 696 | srq->srq_context = srq_init_attr->srq_context; |
Sean Hefty | 96104ed | 2011-05-23 16:31:36 -0700 | [diff] [blame] | 697 | srq->srq_type = srq_init_attr->srq_type; |
Artemy Kovalyov | 1a56ff6 | 2017-08-17 15:52:04 +0300 | [diff] [blame] | 698 | if (ib_srq_has_cq(srq->srq_type)) { |
| 699 | srq->ext.cq = srq_init_attr->ext.cq; |
| 700 | atomic_inc(&srq->ext.cq->usecnt); |
| 701 | } |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 702 | if (srq->srq_type == IB_SRQT_XRC) { |
| 703 | srq->ext.xrc.xrcd = srq_init_attr->ext.xrc.xrcd; |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 704 | atomic_inc(&srq->ext.xrc.xrcd->usecnt); |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 705 | } |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 706 | atomic_inc(&pd->usecnt); |
| 707 | atomic_set(&srq->usecnt, 0); |
| 708 | } |
| 709 | |
| 710 | return srq; |
| 711 | } |
| 712 | EXPORT_SYMBOL(ib_create_srq); |
| 713 | |
| 714 | int ib_modify_srq(struct ib_srq *srq, |
| 715 | struct ib_srq_attr *srq_attr, |
| 716 | enum ib_srq_attr_mask srq_attr_mask) |
| 717 | { |
Dotan Barak | 7ce5eac | 2008-04-16 21:09:28 -0700 | [diff] [blame] | 718 | return srq->device->modify_srq ? |
| 719 | srq->device->modify_srq(srq, srq_attr, srq_attr_mask, NULL) : |
| 720 | -ENOSYS; |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 721 | } |
| 722 | EXPORT_SYMBOL(ib_modify_srq); |
| 723 | |
| 724 | int ib_query_srq(struct ib_srq *srq, |
| 725 | struct ib_srq_attr *srq_attr) |
| 726 | { |
| 727 | return srq->device->query_srq ? |
| 728 | srq->device->query_srq(srq, srq_attr) : -ENOSYS; |
| 729 | } |
| 730 | EXPORT_SYMBOL(ib_query_srq); |
| 731 | |
| 732 | int ib_destroy_srq(struct ib_srq *srq) |
| 733 | { |
| 734 | struct ib_pd *pd; |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 735 | enum ib_srq_type srq_type; |
| 736 | struct ib_xrcd *uninitialized_var(xrcd); |
| 737 | struct ib_cq *uninitialized_var(cq); |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 738 | int ret; |
| 739 | |
| 740 | if (atomic_read(&srq->usecnt)) |
| 741 | return -EBUSY; |
| 742 | |
| 743 | pd = srq->pd; |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 744 | srq_type = srq->srq_type; |
Artemy Kovalyov | 1a56ff6 | 2017-08-17 15:52:04 +0300 | [diff] [blame] | 745 | if (ib_srq_has_cq(srq_type)) |
| 746 | cq = srq->ext.cq; |
| 747 | if (srq_type == IB_SRQT_XRC) |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 748 | xrcd = srq->ext.xrc.xrcd; |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 749 | |
| 750 | ret = srq->device->destroy_srq(srq); |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 751 | if (!ret) { |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 752 | atomic_dec(&pd->usecnt); |
Artemy Kovalyov | 1a56ff6 | 2017-08-17 15:52:04 +0300 | [diff] [blame] | 753 | if (srq_type == IB_SRQT_XRC) |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 754 | atomic_dec(&xrcd->usecnt); |
Artemy Kovalyov | 1a56ff6 | 2017-08-17 15:52:04 +0300 | [diff] [blame] | 755 | if (ib_srq_has_cq(srq_type)) |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 756 | atomic_dec(&cq->usecnt); |
Sean Hefty | 418d513 | 2011-05-23 19:42:29 -0700 | [diff] [blame] | 757 | } |
Roland Dreier | d41fcc6 | 2005-08-18 12:23:08 -0700 | [diff] [blame] | 758 | |
| 759 | return ret; |
| 760 | } |
| 761 | EXPORT_SYMBOL(ib_destroy_srq); |
| 762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | /* Queue pairs */ |
| 764 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 765 | static void __ib_shared_qp_event_handler(struct ib_event *event, void *context) |
| 766 | { |
| 767 | struct ib_qp *qp = context; |
Yishai Hadas | 73c40c6 | 2013-08-01 18:49:53 +0300 | [diff] [blame] | 768 | unsigned long flags; |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 769 | |
Yishai Hadas | 73c40c6 | 2013-08-01 18:49:53 +0300 | [diff] [blame] | 770 | spin_lock_irqsave(&qp->device->event_handler_lock, flags); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 771 | list_for_each_entry(event->element.qp, &qp->open_list, open_list) |
Shlomo Pongratz | eec9e29f | 2013-04-10 14:26:46 +0000 | [diff] [blame] | 772 | if (event->element.qp->event_handler) |
| 773 | event->element.qp->event_handler(event, event->element.qp->qp_context); |
Yishai Hadas | 73c40c6 | 2013-08-01 18:49:53 +0300 | [diff] [blame] | 774 | spin_unlock_irqrestore(&qp->device->event_handler_lock, flags); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 777 | static void __ib_insert_xrcd_qp(struct ib_xrcd *xrcd, struct ib_qp *qp) |
| 778 | { |
| 779 | mutex_lock(&xrcd->tgt_qp_mutex); |
| 780 | list_add(&qp->xrcd_list, &xrcd->tgt_qp_list); |
| 781 | mutex_unlock(&xrcd->tgt_qp_mutex); |
| 782 | } |
| 783 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 784 | static struct ib_qp *__ib_open_qp(struct ib_qp *real_qp, |
| 785 | void (*event_handler)(struct ib_event *, void *), |
| 786 | void *qp_context) |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 787 | { |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 788 | struct ib_qp *qp; |
| 789 | unsigned long flags; |
Daniel Jurgens | d291f1a | 2017-05-19 15:48:52 +0300 | [diff] [blame] | 790 | int err; |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 791 | |
| 792 | qp = kzalloc(sizeof *qp, GFP_KERNEL); |
| 793 | if (!qp) |
| 794 | return ERR_PTR(-ENOMEM); |
| 795 | |
| 796 | qp->real_qp = real_qp; |
Daniel Jurgens | d291f1a | 2017-05-19 15:48:52 +0300 | [diff] [blame] | 797 | err = ib_open_shared_qp_security(qp, real_qp->device); |
| 798 | if (err) { |
| 799 | kfree(qp); |
| 800 | return ERR_PTR(err); |
| 801 | } |
| 802 | |
| 803 | qp->real_qp = real_qp; |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 804 | atomic_inc(&real_qp->usecnt); |
| 805 | qp->device = real_qp->device; |
| 806 | qp->event_handler = event_handler; |
| 807 | qp->qp_context = qp_context; |
| 808 | qp->qp_num = real_qp->qp_num; |
| 809 | qp->qp_type = real_qp->qp_type; |
| 810 | |
| 811 | spin_lock_irqsave(&real_qp->device->event_handler_lock, flags); |
| 812 | list_add(&qp->open_list, &real_qp->open_list); |
| 813 | spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags); |
| 814 | |
| 815 | return qp; |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 818 | struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd, |
| 819 | struct ib_qp_open_attr *qp_open_attr) |
| 820 | { |
| 821 | struct ib_qp *qp, *real_qp; |
| 822 | |
| 823 | if (qp_open_attr->qp_type != IB_QPT_XRC_TGT) |
| 824 | return ERR_PTR(-EINVAL); |
| 825 | |
| 826 | qp = ERR_PTR(-EINVAL); |
| 827 | mutex_lock(&xrcd->tgt_qp_mutex); |
| 828 | list_for_each_entry(real_qp, &xrcd->tgt_qp_list, xrcd_list) { |
| 829 | if (real_qp->qp_num == qp_open_attr->qp_num) { |
| 830 | qp = __ib_open_qp(real_qp, qp_open_attr->event_handler, |
| 831 | qp_open_attr->qp_context); |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | mutex_unlock(&xrcd->tgt_qp_mutex); |
| 836 | return qp; |
| 837 | } |
| 838 | EXPORT_SYMBOL(ib_open_qp); |
| 839 | |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 840 | static struct ib_qp *ib_create_xrc_qp(struct ib_qp *qp, |
| 841 | struct ib_qp_init_attr *qp_init_attr) |
| 842 | { |
| 843 | struct ib_qp *real_qp = qp; |
| 844 | |
| 845 | qp->event_handler = __ib_shared_qp_event_handler; |
| 846 | qp->qp_context = qp; |
| 847 | qp->pd = NULL; |
| 848 | qp->send_cq = qp->recv_cq = NULL; |
| 849 | qp->srq = NULL; |
| 850 | qp->xrcd = qp_init_attr->xrcd; |
| 851 | atomic_inc(&qp_init_attr->xrcd->usecnt); |
| 852 | INIT_LIST_HEAD(&qp->open_list); |
| 853 | |
| 854 | qp = __ib_open_qp(real_qp, qp_init_attr->event_handler, |
| 855 | qp_init_attr->qp_context); |
| 856 | if (!IS_ERR(qp)) |
| 857 | __ib_insert_xrcd_qp(qp_init_attr->xrcd, real_qp); |
| 858 | else |
| 859 | real_qp->device->destroy_qp(real_qp); |
| 860 | return qp; |
| 861 | } |
| 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | struct ib_qp *ib_create_qp(struct ib_pd *pd, |
| 864 | struct ib_qp_init_attr *qp_init_attr) |
| 865 | { |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 866 | struct ib_device *device = pd ? pd->device : qp_init_attr->xrcd->device; |
| 867 | struct ib_qp *qp; |
Christoph Hellwig | a060b56 | 2016-05-03 18:01:09 +0200 | [diff] [blame] | 868 | int ret; |
| 869 | |
Yishai Hadas | a9017e2 | 2016-05-23 15:20:54 +0300 | [diff] [blame] | 870 | if (qp_init_attr->rwq_ind_tbl && |
| 871 | (qp_init_attr->recv_cq || |
| 872 | qp_init_attr->srq || qp_init_attr->cap.max_recv_wr || |
| 873 | qp_init_attr->cap.max_recv_sge)) |
| 874 | return ERR_PTR(-EINVAL); |
| 875 | |
Christoph Hellwig | a060b56 | 2016-05-03 18:01:09 +0200 | [diff] [blame] | 876 | /* |
| 877 | * If the callers is using the RDMA API calculate the resources |
| 878 | * needed for the RDMA READ/WRITE operations. |
| 879 | * |
| 880 | * Note that these callers need to pass in a port number. |
| 881 | */ |
| 882 | if (qp_init_attr->cap.max_rdma_ctxs) |
| 883 | rdma_rw_init_qp(device, qp_init_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 885 | qp = device->create_qp(pd, qp_init_attr, NULL); |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 886 | if (IS_ERR(qp)) |
| 887 | return qp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Daniel Jurgens | d291f1a | 2017-05-19 15:48:52 +0300 | [diff] [blame] | 889 | ret = ib_create_qp_security(qp, device); |
| 890 | if (ret) { |
| 891 | ib_destroy_qp(qp); |
| 892 | return ERR_PTR(ret); |
| 893 | } |
| 894 | |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 895 | qp->device = device; |
| 896 | qp->real_qp = qp; |
| 897 | qp->uobject = NULL; |
| 898 | qp->qp_type = qp_init_attr->qp_type; |
Yishai Hadas | a9017e2 | 2016-05-23 15:20:54 +0300 | [diff] [blame] | 899 | qp->rwq_ind_tbl = qp_init_attr->rwq_ind_tbl; |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 900 | |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 901 | atomic_set(&qp->usecnt, 0); |
Christoph Hellwig | fffb038 | 2016-05-03 18:01:07 +0200 | [diff] [blame] | 902 | qp->mrs_used = 0; |
| 903 | spin_lock_init(&qp->mr_lock); |
Christoph Hellwig | a060b56 | 2016-05-03 18:01:09 +0200 | [diff] [blame] | 904 | INIT_LIST_HEAD(&qp->rdma_mrs); |
Christoph Hellwig | 0e353e3 | 2016-05-03 18:01:12 +0200 | [diff] [blame] | 905 | INIT_LIST_HEAD(&qp->sig_mrs); |
Noa Osherovich | 498ca3c | 2017-08-23 08:35:40 +0300 | [diff] [blame] | 906 | qp->port = 0; |
Christoph Hellwig | fffb038 | 2016-05-03 18:01:07 +0200 | [diff] [blame] | 907 | |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 908 | if (qp_init_attr->qp_type == IB_QPT_XRC_TGT) |
| 909 | return ib_create_xrc_qp(qp, qp_init_attr); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 910 | |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 911 | qp->event_handler = qp_init_attr->event_handler; |
| 912 | qp->qp_context = qp_init_attr->qp_context; |
| 913 | if (qp_init_attr->qp_type == IB_QPT_XRC_INI) { |
| 914 | qp->recv_cq = NULL; |
| 915 | qp->srq = NULL; |
| 916 | } else { |
| 917 | qp->recv_cq = qp_init_attr->recv_cq; |
Yishai Hadas | a9017e2 | 2016-05-23 15:20:54 +0300 | [diff] [blame] | 918 | if (qp_init_attr->recv_cq) |
| 919 | atomic_inc(&qp_init_attr->recv_cq->usecnt); |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 920 | qp->srq = qp_init_attr->srq; |
| 921 | if (qp->srq) |
| 922 | atomic_inc(&qp_init_attr->srq->usecnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Christoph Hellwig | 04c41bf | 2016-05-03 18:01:06 +0200 | [diff] [blame] | 925 | qp->pd = pd; |
| 926 | qp->send_cq = qp_init_attr->send_cq; |
| 927 | qp->xrcd = NULL; |
| 928 | |
| 929 | atomic_inc(&pd->usecnt); |
Yishai Hadas | a9017e2 | 2016-05-23 15:20:54 +0300 | [diff] [blame] | 930 | if (qp_init_attr->send_cq) |
| 931 | atomic_inc(&qp_init_attr->send_cq->usecnt); |
| 932 | if (qp_init_attr->rwq_ind_tbl) |
| 933 | atomic_inc(&qp->rwq_ind_tbl->usecnt); |
Christoph Hellwig | a060b56 | 2016-05-03 18:01:09 +0200 | [diff] [blame] | 934 | |
| 935 | if (qp_init_attr->cap.max_rdma_ctxs) { |
| 936 | ret = rdma_rw_init_mrs(qp, qp_init_attr); |
| 937 | if (ret) { |
| 938 | pr_err("failed to init MR pool ret= %d\n", ret); |
| 939 | ib_destroy_qp(qp); |
Steve Wise | b6bc1c7 | 2016-09-29 07:31:33 -0700 | [diff] [blame] | 940 | return ERR_PTR(ret); |
Christoph Hellwig | a060b56 | 2016-05-03 18:01:09 +0200 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | |
Bart Van Assche | 632bc3f | 2016-07-21 13:03:30 -0700 | [diff] [blame] | 944 | /* |
| 945 | * Note: all hw drivers guarantee that max_send_sge is lower than |
| 946 | * the device RDMA WRITE SGE limit but not all hw drivers ensure that |
| 947 | * max_send_sge <= max_sge_rd. |
| 948 | */ |
| 949 | qp->max_write_sge = qp_init_attr->cap.max_send_sge; |
| 950 | qp->max_read_sge = min_t(u32, qp_init_attr->cap.max_send_sge, |
| 951 | device->attrs.max_sge_rd); |
| 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | return qp; |
| 954 | } |
| 955 | EXPORT_SYMBOL(ib_create_qp); |
| 956 | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 957 | static const struct { |
| 958 | int valid; |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 959 | enum ib_qp_attr_mask req_param[IB_QPT_MAX]; |
| 960 | enum ib_qp_attr_mask opt_param[IB_QPT_MAX]; |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 961 | } qp_state_table[IB_QPS_ERR + 1][IB_QPS_ERR + 1] = { |
| 962 | [IB_QPS_RESET] = { |
| 963 | [IB_QPS_RESET] = { .valid = 1 }, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 964 | [IB_QPS_INIT] = { |
| 965 | .valid = 1, |
| 966 | .req_param = { |
| 967 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 968 | IB_QP_PORT | |
| 969 | IB_QP_QKEY), |
Or Gerlitz | c938a61 | 2012-03-01 12:17:51 +0200 | [diff] [blame] | 970 | [IB_QPT_RAW_PACKET] = IB_QP_PORT, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 971 | [IB_QPT_UC] = (IB_QP_PKEY_INDEX | |
| 972 | IB_QP_PORT | |
| 973 | IB_QP_ACCESS_FLAGS), |
| 974 | [IB_QPT_RC] = (IB_QP_PKEY_INDEX | |
| 975 | IB_QP_PORT | |
| 976 | IB_QP_ACCESS_FLAGS), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 977 | [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX | |
| 978 | IB_QP_PORT | |
| 979 | IB_QP_ACCESS_FLAGS), |
| 980 | [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX | |
| 981 | IB_QP_PORT | |
| 982 | IB_QP_ACCESS_FLAGS), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 983 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 984 | IB_QP_QKEY), |
| 985 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 986 | IB_QP_QKEY), |
| 987 | } |
| 988 | }, |
| 989 | }, |
| 990 | [IB_QPS_INIT] = { |
| 991 | [IB_QPS_RESET] = { .valid = 1 }, |
| 992 | [IB_QPS_ERR] = { .valid = 1 }, |
| 993 | [IB_QPS_INIT] = { |
| 994 | .valid = 1, |
| 995 | .opt_param = { |
| 996 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 997 | IB_QP_PORT | |
| 998 | IB_QP_QKEY), |
| 999 | [IB_QPT_UC] = (IB_QP_PKEY_INDEX | |
| 1000 | IB_QP_PORT | |
| 1001 | IB_QP_ACCESS_FLAGS), |
| 1002 | [IB_QPT_RC] = (IB_QP_PKEY_INDEX | |
| 1003 | IB_QP_PORT | |
| 1004 | IB_QP_ACCESS_FLAGS), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1005 | [IB_QPT_XRC_INI] = (IB_QP_PKEY_INDEX | |
| 1006 | IB_QP_PORT | |
| 1007 | IB_QP_ACCESS_FLAGS), |
| 1008 | [IB_QPT_XRC_TGT] = (IB_QP_PKEY_INDEX | |
| 1009 | IB_QP_PORT | |
| 1010 | IB_QP_ACCESS_FLAGS), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1011 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 1012 | IB_QP_QKEY), |
| 1013 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 1014 | IB_QP_QKEY), |
| 1015 | } |
| 1016 | }, |
| 1017 | [IB_QPS_RTR] = { |
| 1018 | .valid = 1, |
| 1019 | .req_param = { |
| 1020 | [IB_QPT_UC] = (IB_QP_AV | |
| 1021 | IB_QP_PATH_MTU | |
| 1022 | IB_QP_DEST_QPN | |
| 1023 | IB_QP_RQ_PSN), |
| 1024 | [IB_QPT_RC] = (IB_QP_AV | |
| 1025 | IB_QP_PATH_MTU | |
| 1026 | IB_QP_DEST_QPN | |
| 1027 | IB_QP_RQ_PSN | |
| 1028 | IB_QP_MAX_DEST_RD_ATOMIC | |
| 1029 | IB_QP_MIN_RNR_TIMER), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1030 | [IB_QPT_XRC_INI] = (IB_QP_AV | |
| 1031 | IB_QP_PATH_MTU | |
| 1032 | IB_QP_DEST_QPN | |
| 1033 | IB_QP_RQ_PSN), |
| 1034 | [IB_QPT_XRC_TGT] = (IB_QP_AV | |
| 1035 | IB_QP_PATH_MTU | |
| 1036 | IB_QP_DEST_QPN | |
| 1037 | IB_QP_RQ_PSN | |
| 1038 | IB_QP_MAX_DEST_RD_ATOMIC | |
| 1039 | IB_QP_MIN_RNR_TIMER), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1040 | }, |
| 1041 | .opt_param = { |
| 1042 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 1043 | IB_QP_QKEY), |
| 1044 | [IB_QPT_UC] = (IB_QP_ALT_PATH | |
| 1045 | IB_QP_ACCESS_FLAGS | |
| 1046 | IB_QP_PKEY_INDEX), |
| 1047 | [IB_QPT_RC] = (IB_QP_ALT_PATH | |
| 1048 | IB_QP_ACCESS_FLAGS | |
| 1049 | IB_QP_PKEY_INDEX), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1050 | [IB_QPT_XRC_INI] = (IB_QP_ALT_PATH | |
| 1051 | IB_QP_ACCESS_FLAGS | |
| 1052 | IB_QP_PKEY_INDEX), |
| 1053 | [IB_QPT_XRC_TGT] = (IB_QP_ALT_PATH | |
| 1054 | IB_QP_ACCESS_FLAGS | |
| 1055 | IB_QP_PKEY_INDEX), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1056 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 1057 | IB_QP_QKEY), |
| 1058 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 1059 | IB_QP_QKEY), |
Matan Barak | dd5f03b | 2013-12-12 18:03:11 +0200 | [diff] [blame] | 1060 | }, |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1061 | }, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1062 | }, |
| 1063 | [IB_QPS_RTR] = { |
| 1064 | [IB_QPS_RESET] = { .valid = 1 }, |
| 1065 | [IB_QPS_ERR] = { .valid = 1 }, |
| 1066 | [IB_QPS_RTS] = { |
| 1067 | .valid = 1, |
| 1068 | .req_param = { |
| 1069 | [IB_QPT_UD] = IB_QP_SQ_PSN, |
| 1070 | [IB_QPT_UC] = IB_QP_SQ_PSN, |
| 1071 | [IB_QPT_RC] = (IB_QP_TIMEOUT | |
| 1072 | IB_QP_RETRY_CNT | |
| 1073 | IB_QP_RNR_RETRY | |
| 1074 | IB_QP_SQ_PSN | |
| 1075 | IB_QP_MAX_QP_RD_ATOMIC), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1076 | [IB_QPT_XRC_INI] = (IB_QP_TIMEOUT | |
| 1077 | IB_QP_RETRY_CNT | |
| 1078 | IB_QP_RNR_RETRY | |
| 1079 | IB_QP_SQ_PSN | |
| 1080 | IB_QP_MAX_QP_RD_ATOMIC), |
| 1081 | [IB_QPT_XRC_TGT] = (IB_QP_TIMEOUT | |
| 1082 | IB_QP_SQ_PSN), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1083 | [IB_QPT_SMI] = IB_QP_SQ_PSN, |
| 1084 | [IB_QPT_GSI] = IB_QP_SQ_PSN, |
| 1085 | }, |
| 1086 | .opt_param = { |
| 1087 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 1088 | IB_QP_QKEY), |
| 1089 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 1090 | IB_QP_ALT_PATH | |
| 1091 | IB_QP_ACCESS_FLAGS | |
| 1092 | IB_QP_PATH_MIG_STATE), |
| 1093 | [IB_QPT_RC] = (IB_QP_CUR_STATE | |
| 1094 | IB_QP_ALT_PATH | |
| 1095 | IB_QP_ACCESS_FLAGS | |
| 1096 | IB_QP_MIN_RNR_TIMER | |
| 1097 | IB_QP_PATH_MIG_STATE), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1098 | [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | |
| 1099 | IB_QP_ALT_PATH | |
| 1100 | IB_QP_ACCESS_FLAGS | |
| 1101 | IB_QP_PATH_MIG_STATE), |
| 1102 | [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | |
| 1103 | IB_QP_ALT_PATH | |
| 1104 | IB_QP_ACCESS_FLAGS | |
| 1105 | IB_QP_MIN_RNR_TIMER | |
| 1106 | IB_QP_PATH_MIG_STATE), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1107 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 1108 | IB_QP_QKEY), |
| 1109 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 1110 | IB_QP_QKEY), |
Bodong Wang | 528e5a1 | 2016-12-01 13:43:14 +0200 | [diff] [blame] | 1111 | [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | }, |
| 1115 | [IB_QPS_RTS] = { |
| 1116 | [IB_QPS_RESET] = { .valid = 1 }, |
| 1117 | [IB_QPS_ERR] = { .valid = 1 }, |
| 1118 | [IB_QPS_RTS] = { |
| 1119 | .valid = 1, |
| 1120 | .opt_param = { |
| 1121 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 1122 | IB_QP_QKEY), |
Dotan Barak | 4546d31 | 2006-03-02 11:22:28 -0800 | [diff] [blame] | 1123 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 1124 | IB_QP_ACCESS_FLAGS | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1125 | IB_QP_ALT_PATH | |
| 1126 | IB_QP_PATH_MIG_STATE), |
Dotan Barak | 4546d31 | 2006-03-02 11:22:28 -0800 | [diff] [blame] | 1127 | [IB_QPT_RC] = (IB_QP_CUR_STATE | |
| 1128 | IB_QP_ACCESS_FLAGS | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1129 | IB_QP_ALT_PATH | |
| 1130 | IB_QP_PATH_MIG_STATE | |
| 1131 | IB_QP_MIN_RNR_TIMER), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1132 | [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | |
| 1133 | IB_QP_ACCESS_FLAGS | |
| 1134 | IB_QP_ALT_PATH | |
| 1135 | IB_QP_PATH_MIG_STATE), |
| 1136 | [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | |
| 1137 | IB_QP_ACCESS_FLAGS | |
| 1138 | IB_QP_ALT_PATH | |
| 1139 | IB_QP_PATH_MIG_STATE | |
| 1140 | IB_QP_MIN_RNR_TIMER), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1141 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 1142 | IB_QP_QKEY), |
| 1143 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 1144 | IB_QP_QKEY), |
Bodong Wang | 528e5a1 | 2016-12-01 13:43:14 +0200 | [diff] [blame] | 1145 | [IB_QPT_RAW_PACKET] = IB_QP_RATE_LIMIT, |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1146 | } |
| 1147 | }, |
| 1148 | [IB_QPS_SQD] = { |
| 1149 | .valid = 1, |
| 1150 | .opt_param = { |
| 1151 | [IB_QPT_UD] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 1152 | [IB_QPT_UC] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 1153 | [IB_QPT_RC] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1154 | [IB_QPT_XRC_INI] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 1155 | [IB_QPT_XRC_TGT] = IB_QP_EN_SQD_ASYNC_NOTIFY, /* ??? */ |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1156 | [IB_QPT_SMI] = IB_QP_EN_SQD_ASYNC_NOTIFY, |
| 1157 | [IB_QPT_GSI] = IB_QP_EN_SQD_ASYNC_NOTIFY |
| 1158 | } |
| 1159 | }, |
| 1160 | }, |
| 1161 | [IB_QPS_SQD] = { |
| 1162 | [IB_QPS_RESET] = { .valid = 1 }, |
| 1163 | [IB_QPS_ERR] = { .valid = 1 }, |
| 1164 | [IB_QPS_RTS] = { |
| 1165 | .valid = 1, |
| 1166 | .opt_param = { |
| 1167 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 1168 | IB_QP_QKEY), |
| 1169 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 1170 | IB_QP_ALT_PATH | |
| 1171 | IB_QP_ACCESS_FLAGS | |
| 1172 | IB_QP_PATH_MIG_STATE), |
| 1173 | [IB_QPT_RC] = (IB_QP_CUR_STATE | |
| 1174 | IB_QP_ALT_PATH | |
| 1175 | IB_QP_ACCESS_FLAGS | |
| 1176 | IB_QP_MIN_RNR_TIMER | |
| 1177 | IB_QP_PATH_MIG_STATE), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1178 | [IB_QPT_XRC_INI] = (IB_QP_CUR_STATE | |
| 1179 | IB_QP_ALT_PATH | |
| 1180 | IB_QP_ACCESS_FLAGS | |
| 1181 | IB_QP_PATH_MIG_STATE), |
| 1182 | [IB_QPT_XRC_TGT] = (IB_QP_CUR_STATE | |
| 1183 | IB_QP_ALT_PATH | |
| 1184 | IB_QP_ACCESS_FLAGS | |
| 1185 | IB_QP_MIN_RNR_TIMER | |
| 1186 | IB_QP_PATH_MIG_STATE), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1187 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 1188 | IB_QP_QKEY), |
| 1189 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 1190 | IB_QP_QKEY), |
| 1191 | } |
| 1192 | }, |
| 1193 | [IB_QPS_SQD] = { |
| 1194 | .valid = 1, |
| 1195 | .opt_param = { |
| 1196 | [IB_QPT_UD] = (IB_QP_PKEY_INDEX | |
| 1197 | IB_QP_QKEY), |
| 1198 | [IB_QPT_UC] = (IB_QP_AV | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1199 | IB_QP_ALT_PATH | |
| 1200 | IB_QP_ACCESS_FLAGS | |
| 1201 | IB_QP_PKEY_INDEX | |
| 1202 | IB_QP_PATH_MIG_STATE), |
| 1203 | [IB_QPT_RC] = (IB_QP_PORT | |
| 1204 | IB_QP_AV | |
| 1205 | IB_QP_TIMEOUT | |
| 1206 | IB_QP_RETRY_CNT | |
| 1207 | IB_QP_RNR_RETRY | |
| 1208 | IB_QP_MAX_QP_RD_ATOMIC | |
| 1209 | IB_QP_MAX_DEST_RD_ATOMIC | |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1210 | IB_QP_ALT_PATH | |
| 1211 | IB_QP_ACCESS_FLAGS | |
| 1212 | IB_QP_PKEY_INDEX | |
| 1213 | IB_QP_MIN_RNR_TIMER | |
| 1214 | IB_QP_PATH_MIG_STATE), |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1215 | [IB_QPT_XRC_INI] = (IB_QP_PORT | |
| 1216 | IB_QP_AV | |
| 1217 | IB_QP_TIMEOUT | |
| 1218 | IB_QP_RETRY_CNT | |
| 1219 | IB_QP_RNR_RETRY | |
| 1220 | IB_QP_MAX_QP_RD_ATOMIC | |
| 1221 | IB_QP_ALT_PATH | |
| 1222 | IB_QP_ACCESS_FLAGS | |
| 1223 | IB_QP_PKEY_INDEX | |
| 1224 | IB_QP_PATH_MIG_STATE), |
| 1225 | [IB_QPT_XRC_TGT] = (IB_QP_PORT | |
| 1226 | IB_QP_AV | |
| 1227 | IB_QP_TIMEOUT | |
| 1228 | IB_QP_MAX_DEST_RD_ATOMIC | |
| 1229 | IB_QP_ALT_PATH | |
| 1230 | IB_QP_ACCESS_FLAGS | |
| 1231 | IB_QP_PKEY_INDEX | |
| 1232 | IB_QP_MIN_RNR_TIMER | |
| 1233 | IB_QP_PATH_MIG_STATE), |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1234 | [IB_QPT_SMI] = (IB_QP_PKEY_INDEX | |
| 1235 | IB_QP_QKEY), |
| 1236 | [IB_QPT_GSI] = (IB_QP_PKEY_INDEX | |
| 1237 | IB_QP_QKEY), |
| 1238 | } |
| 1239 | } |
| 1240 | }, |
| 1241 | [IB_QPS_SQE] = { |
| 1242 | [IB_QPS_RESET] = { .valid = 1 }, |
| 1243 | [IB_QPS_ERR] = { .valid = 1 }, |
| 1244 | [IB_QPS_RTS] = { |
| 1245 | .valid = 1, |
| 1246 | .opt_param = { |
| 1247 | [IB_QPT_UD] = (IB_QP_CUR_STATE | |
| 1248 | IB_QP_QKEY), |
| 1249 | [IB_QPT_UC] = (IB_QP_CUR_STATE | |
| 1250 | IB_QP_ACCESS_FLAGS), |
| 1251 | [IB_QPT_SMI] = (IB_QP_CUR_STATE | |
| 1252 | IB_QP_QKEY), |
| 1253 | [IB_QPT_GSI] = (IB_QP_CUR_STATE | |
| 1254 | IB_QP_QKEY), |
| 1255 | } |
| 1256 | } |
| 1257 | }, |
| 1258 | [IB_QPS_ERR] = { |
| 1259 | [IB_QPS_RESET] = { .valid = 1 }, |
| 1260 | [IB_QPS_ERR] = { .valid = 1 } |
| 1261 | } |
| 1262 | }; |
| 1263 | |
| 1264 | 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] | 1265 | enum ib_qp_type type, enum ib_qp_attr_mask mask, |
| 1266 | enum rdma_link_layer ll) |
Roland Dreier | 8a51866 | 2006-02-13 12:48:12 -0800 | [diff] [blame] | 1267 | { |
| 1268 | enum ib_qp_attr_mask req_param, opt_param; |
| 1269 | |
| 1270 | if (cur_state < 0 || cur_state > IB_QPS_ERR || |
| 1271 | next_state < 0 || next_state > IB_QPS_ERR) |
| 1272 | return 0; |
| 1273 | |
| 1274 | if (mask & IB_QP_CUR_STATE && |
| 1275 | cur_state != IB_QPS_RTR && cur_state != IB_QPS_RTS && |
| 1276 | cur_state != IB_QPS_SQD && cur_state != IB_QPS_SQE) |
| 1277 | return 0; |
| 1278 | |
| 1279 | if (!qp_state_table[cur_state][next_state].valid) |
| 1280 | return 0; |
| 1281 | |
| 1282 | req_param = qp_state_table[cur_state][next_state].req_param[type]; |
| 1283 | opt_param = qp_state_table[cur_state][next_state].opt_param[type]; |
| 1284 | |
| 1285 | if ((mask & req_param) != req_param) |
| 1286 | return 0; |
| 1287 | |
| 1288 | if (mask & ~(req_param | opt_param | IB_QP_STATE)) |
| 1289 | return 0; |
| 1290 | |
| 1291 | return 1; |
| 1292 | } |
| 1293 | EXPORT_SYMBOL(ib_modify_qp_is_ok); |
| 1294 | |
Parav Pandit | c0348eb | 2017-10-16 08:45:13 +0300 | [diff] [blame] | 1295 | static int ib_resolve_eth_dmac(struct ib_device *device, |
| 1296 | struct rdma_ah_attr *ah_attr) |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1297 | { |
| 1298 | int ret = 0; |
Dasaratharaman Chandramouli | d8966fc | 2017-04-29 14:41:28 -0400 | [diff] [blame] | 1299 | struct ib_global_route *grh; |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1300 | |
Dasaratharaman Chandramouli | d8966fc | 2017-04-29 14:41:28 -0400 | [diff] [blame] | 1301 | if (!rdma_is_port_valid(device, rdma_ah_get_port_num(ah_attr))) |
Moni Shoua | c90ea9d | 2016-11-23 08:23:22 +0200 | [diff] [blame] | 1302 | return -EINVAL; |
Matan Barak | dbf727d | 2015-10-15 18:38:51 +0300 | [diff] [blame] | 1303 | |
Dasaratharaman Chandramouli | d8966fc | 2017-04-29 14:41:28 -0400 | [diff] [blame] | 1304 | grh = rdma_ah_retrieve_grh(ah_attr); |
| 1305 | |
Noa Osherovich | 9636a56 | 2017-06-12 11:14:04 +0300 | [diff] [blame] | 1306 | if (rdma_is_multicast_addr((struct in6_addr *)ah_attr->grh.dgid.raw)) { |
| 1307 | if (ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw)) { |
| 1308 | __be32 addr = 0; |
| 1309 | |
| 1310 | memcpy(&addr, ah_attr->grh.dgid.raw + 12, 4); |
| 1311 | ip_eth_mc_map(addr, (char *)ah_attr->roce.dmac); |
| 1312 | } else { |
| 1313 | ipv6_eth_mc_map((struct in6_addr *)ah_attr->grh.dgid.raw, |
| 1314 | (char *)ah_attr->roce.dmac); |
| 1315 | } |
Moni Shoua | c90ea9d | 2016-11-23 08:23:22 +0200 | [diff] [blame] | 1316 | } else { |
Parav Pandit | 1060f86 | 2017-11-14 14:51:49 +0200 | [diff] [blame] | 1317 | ret = ib_resolve_unicast_gid_dmac(device, ah_attr); |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1318 | } |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1319 | return ret; |
| 1320 | } |
Or Gerlitz | ed4c54e | 2013-12-12 18:03:17 +0200 | [diff] [blame] | 1321 | |
Parav Pandit | a512c2f | 2017-05-23 11:26:08 +0300 | [diff] [blame] | 1322 | /** |
Parav Pandit | b96ac05 | 2018-01-09 15:24:51 +0200 | [diff] [blame] | 1323 | * IB core internal function to perform QP attributes modification. |
Parav Pandit | a512c2f | 2017-05-23 11:26:08 +0300 | [diff] [blame] | 1324 | */ |
Parav Pandit | b96ac05 | 2018-01-09 15:24:51 +0200 | [diff] [blame] | 1325 | static int _ib_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr, |
| 1326 | int attr_mask, struct ib_udata *udata) |
Parav Pandit | a512c2f | 2017-05-23 11:26:08 +0300 | [diff] [blame] | 1327 | { |
Majd Dibbiny | 727b7e9 | 2017-11-14 14:51:56 +0200 | [diff] [blame] | 1328 | u8 port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port; |
Parav Pandit | a512c2f | 2017-05-23 11:26:08 +0300 | [diff] [blame] | 1329 | int ret; |
| 1330 | |
Majd Dibbiny | 727b7e9 | 2017-11-14 14:51:56 +0200 | [diff] [blame] | 1331 | if (rdma_ib_or_roce(qp->device, port)) { |
| 1332 | if (attr_mask & IB_QP_RQ_PSN && attr->rq_psn & ~0xffffff) { |
| 1333 | pr_warn("%s: %s rq_psn overflow, masking to 24 bits\n", |
| 1334 | __func__, qp->device->name); |
| 1335 | attr->rq_psn &= 0xffffff; |
| 1336 | } |
| 1337 | |
| 1338 | if (attr_mask & IB_QP_SQ_PSN && attr->sq_psn & ~0xffffff) { |
| 1339 | pr_warn("%s: %s sq_psn overflow, masking to 24 bits\n", |
| 1340 | __func__, qp->device->name); |
| 1341 | attr->sq_psn &= 0xffffff; |
| 1342 | } |
| 1343 | } |
| 1344 | |
Noa Osherovich | 498ca3c | 2017-08-23 08:35:40 +0300 | [diff] [blame] | 1345 | ret = ib_security_modify_qp(qp, attr, attr_mask, udata); |
| 1346 | if (!ret && (attr_mask & IB_QP_PORT)) |
| 1347 | qp->port = attr->port_num; |
| 1348 | |
| 1349 | return ret; |
Parav Pandit | a512c2f | 2017-05-23 11:26:08 +0300 | [diff] [blame] | 1350 | } |
Parav Pandit | b96ac05 | 2018-01-09 15:24:51 +0200 | [diff] [blame] | 1351 | |
Parav Pandit | a6753c4 | 2018-01-09 15:24:53 +0200 | [diff] [blame] | 1352 | static bool is_qp_type_connected(const struct ib_qp *qp) |
| 1353 | { |
| 1354 | return (qp->qp_type == IB_QPT_UC || |
| 1355 | qp->qp_type == IB_QPT_RC || |
| 1356 | qp->qp_type == IB_QPT_XRC_INI || |
| 1357 | qp->qp_type == IB_QPT_XRC_TGT); |
| 1358 | } |
| 1359 | |
Parav Pandit | b96ac05 | 2018-01-09 15:24:51 +0200 | [diff] [blame] | 1360 | /** |
| 1361 | * ib_modify_qp_with_udata - Modifies the attributes for the specified QP. |
| 1362 | * @ib_qp: The QP to modify. |
| 1363 | * @attr: On input, specifies the QP attributes to modify. On output, |
| 1364 | * the current values of selected QP attributes are returned. |
| 1365 | * @attr_mask: A bit-mask used to specify which attributes of the QP |
| 1366 | * are being modified. |
| 1367 | * @udata: pointer to user's input output buffer information |
| 1368 | * are being modified. |
| 1369 | * It returns 0 on success and returns appropriate error code on error. |
| 1370 | */ |
| 1371 | int ib_modify_qp_with_udata(struct ib_qp *ib_qp, struct ib_qp_attr *attr, |
| 1372 | int attr_mask, struct ib_udata *udata) |
| 1373 | { |
| 1374 | struct ib_qp *qp = ib_qp->real_qp; |
| 1375 | int ret; |
| 1376 | |
Parav Pandit | f2290d6 | 2018-01-09 15:24:52 +0200 | [diff] [blame] | 1377 | if (attr_mask & IB_QP_AV && |
Parav Pandit | a6753c4 | 2018-01-09 15:24:53 +0200 | [diff] [blame] | 1378 | attr->ah_attr.type == RDMA_AH_ATTR_TYPE_ROCE && |
| 1379 | is_qp_type_connected(qp)) { |
Parav Pandit | b96ac05 | 2018-01-09 15:24:51 +0200 | [diff] [blame] | 1380 | ret = ib_resolve_eth_dmac(qp->device, &attr->ah_attr); |
| 1381 | if (ret) |
| 1382 | return ret; |
| 1383 | } |
| 1384 | return _ib_modify_qp(qp, attr, attr_mask, udata); |
| 1385 | } |
Parav Pandit | a512c2f | 2017-05-23 11:26:08 +0300 | [diff] [blame] | 1386 | EXPORT_SYMBOL(ib_modify_qp_with_udata); |
| 1387 | |
Yuval Shaia | d418619 | 2017-06-14 23:13:34 +0300 | [diff] [blame] | 1388 | int ib_get_eth_speed(struct ib_device *dev, u8 port_num, u8 *speed, u8 *width) |
| 1389 | { |
| 1390 | int rc; |
| 1391 | u32 netdev_speed; |
| 1392 | struct net_device *netdev; |
| 1393 | struct ethtool_link_ksettings lksettings; |
| 1394 | |
| 1395 | if (rdma_port_get_link_layer(dev, port_num) != IB_LINK_LAYER_ETHERNET) |
| 1396 | return -EINVAL; |
| 1397 | |
| 1398 | if (!dev->get_netdev) |
| 1399 | return -EOPNOTSUPP; |
| 1400 | |
| 1401 | netdev = dev->get_netdev(dev, port_num); |
| 1402 | if (!netdev) |
| 1403 | return -ENODEV; |
| 1404 | |
| 1405 | rtnl_lock(); |
| 1406 | rc = __ethtool_get_link_ksettings(netdev, &lksettings); |
| 1407 | rtnl_unlock(); |
| 1408 | |
| 1409 | dev_put(netdev); |
| 1410 | |
| 1411 | if (!rc) { |
| 1412 | netdev_speed = lksettings.base.speed; |
| 1413 | } else { |
| 1414 | netdev_speed = SPEED_1000; |
| 1415 | pr_warn("%s speed is unknown, defaulting to %d\n", netdev->name, |
| 1416 | netdev_speed); |
| 1417 | } |
| 1418 | |
| 1419 | if (netdev_speed <= SPEED_1000) { |
| 1420 | *width = IB_WIDTH_1X; |
| 1421 | *speed = IB_SPEED_SDR; |
| 1422 | } else if (netdev_speed <= SPEED_10000) { |
| 1423 | *width = IB_WIDTH_1X; |
| 1424 | *speed = IB_SPEED_FDR10; |
| 1425 | } else if (netdev_speed <= SPEED_20000) { |
| 1426 | *width = IB_WIDTH_4X; |
| 1427 | *speed = IB_SPEED_DDR; |
| 1428 | } else if (netdev_speed <= SPEED_25000) { |
| 1429 | *width = IB_WIDTH_1X; |
| 1430 | *speed = IB_SPEED_EDR; |
| 1431 | } else if (netdev_speed <= SPEED_40000) { |
| 1432 | *width = IB_WIDTH_4X; |
| 1433 | *speed = IB_SPEED_FDR10; |
| 1434 | } else { |
| 1435 | *width = IB_WIDTH_4X; |
| 1436 | *speed = IB_SPEED_EDR; |
| 1437 | } |
| 1438 | |
| 1439 | return 0; |
| 1440 | } |
| 1441 | EXPORT_SYMBOL(ib_get_eth_speed); |
| 1442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | int ib_modify_qp(struct ib_qp *qp, |
| 1444 | struct ib_qp_attr *qp_attr, |
| 1445 | int qp_attr_mask) |
| 1446 | { |
Parav Pandit | b96ac05 | 2018-01-09 15:24:51 +0200 | [diff] [blame] | 1447 | return _ib_modify_qp(qp->real_qp, qp_attr, qp_attr_mask, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | } |
| 1449 | EXPORT_SYMBOL(ib_modify_qp); |
| 1450 | |
| 1451 | int ib_query_qp(struct ib_qp *qp, |
| 1452 | struct ib_qp_attr *qp_attr, |
| 1453 | int qp_attr_mask, |
| 1454 | struct ib_qp_init_attr *qp_init_attr) |
| 1455 | { |
| 1456 | return qp->device->query_qp ? |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1457 | 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] | 1458 | -ENOSYS; |
| 1459 | } |
| 1460 | EXPORT_SYMBOL(ib_query_qp); |
| 1461 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1462 | int ib_close_qp(struct ib_qp *qp) |
| 1463 | { |
| 1464 | struct ib_qp *real_qp; |
| 1465 | unsigned long flags; |
| 1466 | |
| 1467 | real_qp = qp->real_qp; |
| 1468 | if (real_qp == qp) |
| 1469 | return -EINVAL; |
| 1470 | |
| 1471 | spin_lock_irqsave(&real_qp->device->event_handler_lock, flags); |
| 1472 | list_del(&qp->open_list); |
| 1473 | spin_unlock_irqrestore(&real_qp->device->event_handler_lock, flags); |
| 1474 | |
| 1475 | atomic_dec(&real_qp->usecnt); |
Moni Shoua | 4a50881 | 2017-12-24 13:54:58 +0200 | [diff] [blame] | 1476 | if (qp->qp_sec) |
| 1477 | ib_close_shared_qp_security(qp->qp_sec); |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1478 | kfree(qp); |
| 1479 | |
| 1480 | return 0; |
| 1481 | } |
| 1482 | EXPORT_SYMBOL(ib_close_qp); |
| 1483 | |
| 1484 | static int __ib_destroy_shared_qp(struct ib_qp *qp) |
| 1485 | { |
| 1486 | struct ib_xrcd *xrcd; |
| 1487 | struct ib_qp *real_qp; |
| 1488 | int ret; |
| 1489 | |
| 1490 | real_qp = qp->real_qp; |
| 1491 | xrcd = real_qp->xrcd; |
| 1492 | |
| 1493 | mutex_lock(&xrcd->tgt_qp_mutex); |
| 1494 | ib_close_qp(qp); |
| 1495 | if (atomic_read(&real_qp->usecnt) == 0) |
| 1496 | list_del(&real_qp->xrcd_list); |
| 1497 | else |
| 1498 | real_qp = NULL; |
| 1499 | mutex_unlock(&xrcd->tgt_qp_mutex); |
| 1500 | |
| 1501 | if (real_qp) { |
| 1502 | ret = ib_destroy_qp(real_qp); |
| 1503 | if (!ret) |
| 1504 | atomic_dec(&xrcd->usecnt); |
| 1505 | else |
| 1506 | __ib_insert_xrcd_qp(xrcd, real_qp); |
| 1507 | } |
| 1508 | |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | int ib_destroy_qp(struct ib_qp *qp) |
| 1513 | { |
| 1514 | struct ib_pd *pd; |
| 1515 | struct ib_cq *scq, *rcq; |
| 1516 | struct ib_srq *srq; |
Yishai Hadas | a9017e2 | 2016-05-23 15:20:54 +0300 | [diff] [blame] | 1517 | struct ib_rwq_ind_table *ind_tbl; |
Daniel Jurgens | d291f1a | 2017-05-19 15:48:52 +0300 | [diff] [blame] | 1518 | struct ib_qp_security *sec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | int ret; |
| 1520 | |
Christoph Hellwig | fffb038 | 2016-05-03 18:01:07 +0200 | [diff] [blame] | 1521 | WARN_ON_ONCE(qp->mrs_used > 0); |
| 1522 | |
Sean Hefty | 0e0ec7e | 2011-08-08 15:31:51 -0700 | [diff] [blame] | 1523 | if (atomic_read(&qp->usecnt)) |
| 1524 | return -EBUSY; |
| 1525 | |
| 1526 | if (qp->real_qp != qp) |
| 1527 | return __ib_destroy_shared_qp(qp); |
| 1528 | |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1529 | pd = qp->pd; |
| 1530 | scq = qp->send_cq; |
| 1531 | rcq = qp->recv_cq; |
| 1532 | srq = qp->srq; |
Yishai Hadas | a9017e2 | 2016-05-23 15:20:54 +0300 | [diff] [blame] | 1533 | ind_tbl = qp->rwq_ind_tbl; |
Daniel Jurgens | d291f1a | 2017-05-19 15:48:52 +0300 | [diff] [blame] | 1534 | sec = qp->qp_sec; |
| 1535 | if (sec) |
| 1536 | ib_destroy_qp_security_begin(sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Christoph Hellwig | a060b56 | 2016-05-03 18:01:09 +0200 | [diff] [blame] | 1538 | if (!qp->uobject) |
| 1539 | rdma_rw_cleanup_mrs(qp); |
| 1540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | ret = qp->device->destroy_qp(qp); |
| 1542 | if (!ret) { |
Sean Hefty | b42b63c | 2011-05-23 19:59:25 -0700 | [diff] [blame] | 1543 | if (pd) |
| 1544 | atomic_dec(&pd->usecnt); |
| 1545 | if (scq) |
| 1546 | atomic_dec(&scq->usecnt); |
| 1547 | if (rcq) |
| 1548 | atomic_dec(&rcq->usecnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | if (srq) |
| 1550 | atomic_dec(&srq->usecnt); |
Yishai Hadas | a9017e2 | 2016-05-23 15:20:54 +0300 | [diff] [blame] | 1551 | if (ind_tbl) |
| 1552 | atomic_dec(&ind_tbl->usecnt); |
Daniel Jurgens | d291f1a | 2017-05-19 15:48:52 +0300 | [diff] [blame] | 1553 | if (sec) |
| 1554 | ib_destroy_qp_security_end(sec); |
| 1555 | } else { |
| 1556 | if (sec) |
| 1557 | ib_destroy_qp_security_abort(sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | return ret; |
| 1561 | } |
| 1562 | EXPORT_SYMBOL(ib_destroy_qp); |
| 1563 | |
| 1564 | /* Completion queues */ |
| 1565 | |
| 1566 | struct ib_cq *ib_create_cq(struct ib_device *device, |
| 1567 | ib_comp_handler comp_handler, |
| 1568 | void (*event_handler)(struct ib_event *, void *), |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 1569 | void *cq_context, |
| 1570 | const struct ib_cq_init_attr *cq_attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | { |
| 1572 | struct ib_cq *cq; |
| 1573 | |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 1574 | cq = device->create_cq(device, cq_attr, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
| 1576 | if (!IS_ERR(cq)) { |
| 1577 | cq->device = device; |
Roland Dreier | b5e81bf | 2005-07-07 17:57:11 -0700 | [diff] [blame] | 1578 | cq->uobject = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | cq->comp_handler = comp_handler; |
| 1580 | cq->event_handler = event_handler; |
| 1581 | cq->cq_context = cq_context; |
| 1582 | atomic_set(&cq->usecnt, 0); |
| 1583 | } |
| 1584 | |
| 1585 | return cq; |
| 1586 | } |
| 1587 | EXPORT_SYMBOL(ib_create_cq); |
| 1588 | |
Leon Romanovsky | 4190b4e | 2017-11-13 10:51:19 +0200 | [diff] [blame] | 1589 | int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period) |
Eli Cohen | 2dd5716 | 2008-04-16 21:09:33 -0700 | [diff] [blame] | 1590 | { |
| 1591 | return cq->device->modify_cq ? |
| 1592 | cq->device->modify_cq(cq, cq_count, cq_period) : -ENOSYS; |
| 1593 | } |
Leon Romanovsky | 4190b4e | 2017-11-13 10:51:19 +0200 | [diff] [blame] | 1594 | EXPORT_SYMBOL(rdma_set_cq_moderation); |
Eli Cohen | 2dd5716 | 2008-04-16 21:09:33 -0700 | [diff] [blame] | 1595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | int ib_destroy_cq(struct ib_cq *cq) |
| 1597 | { |
| 1598 | if (atomic_read(&cq->usecnt)) |
| 1599 | return -EBUSY; |
| 1600 | |
| 1601 | return cq->device->destroy_cq(cq); |
| 1602 | } |
| 1603 | EXPORT_SYMBOL(ib_destroy_cq); |
| 1604 | |
Roland Dreier | a74cd4a | 2006-02-13 16:30:49 -0800 | [diff] [blame] | 1605 | int ib_resize_cq(struct ib_cq *cq, int cqe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | { |
Roland Dreier | 40de2e5 | 2005-11-08 11:10:25 -0800 | [diff] [blame] | 1607 | return cq->device->resize_cq ? |
Roland Dreier | 33b9b3e | 2006-01-30 14:29:21 -0800 | [diff] [blame] | 1608 | cq->device->resize_cq(cq, cqe, NULL) : -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | EXPORT_SYMBOL(ib_resize_cq); |
| 1611 | |
| 1612 | /* Memory regions */ |
| 1613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | int ib_dereg_mr(struct ib_mr *mr) |
| 1615 | { |
Christoph Hellwig | ab67ed8 | 2015-12-23 19:12:54 +0100 | [diff] [blame] | 1616 | struct ib_pd *pd = mr->pd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | int ret; |
| 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | ret = mr->device->dereg_mr(mr); |
| 1620 | if (!ret) |
| 1621 | atomic_dec(&pd->usecnt); |
| 1622 | |
| 1623 | return ret; |
| 1624 | } |
| 1625 | EXPORT_SYMBOL(ib_dereg_mr); |
| 1626 | |
Sagi Grimberg | 9bee178 | 2015-07-30 10:32:35 +0300 | [diff] [blame] | 1627 | /** |
| 1628 | * ib_alloc_mr() - Allocates a memory region |
| 1629 | * @pd: protection domain associated with the region |
| 1630 | * @mr_type: memory region type |
| 1631 | * @max_num_sg: maximum sg entries available for registration. |
| 1632 | * |
| 1633 | * Notes: |
| 1634 | * Memory registeration page/sg lists must not exceed max_num_sg. |
| 1635 | * For mr_type IB_MR_TYPE_MEM_REG, the total length cannot exceed |
| 1636 | * max_num_sg * used_page_size. |
| 1637 | * |
| 1638 | */ |
| 1639 | struct ib_mr *ib_alloc_mr(struct ib_pd *pd, |
| 1640 | enum ib_mr_type mr_type, |
| 1641 | u32 max_num_sg) |
Sagi Grimberg | 17cd3a2 | 2014-02-23 14:19:04 +0200 | [diff] [blame] | 1642 | { |
| 1643 | struct ib_mr *mr; |
| 1644 | |
Sagi Grimberg | d9f272c | 2015-07-30 10:32:48 +0300 | [diff] [blame] | 1645 | if (!pd->device->alloc_mr) |
Sagi Grimberg | 17cd3a2 | 2014-02-23 14:19:04 +0200 | [diff] [blame] | 1646 | return ERR_PTR(-ENOSYS); |
| 1647 | |
Sagi Grimberg | d9f272c | 2015-07-30 10:32:48 +0300 | [diff] [blame] | 1648 | mr = pd->device->alloc_mr(pd, mr_type, max_num_sg); |
Sagi Grimberg | 17cd3a2 | 2014-02-23 14:19:04 +0200 | [diff] [blame] | 1649 | if (!IS_ERR(mr)) { |
| 1650 | mr->device = pd->device; |
| 1651 | mr->pd = pd; |
| 1652 | mr->uobject = NULL; |
| 1653 | atomic_inc(&pd->usecnt); |
Steve Wise | d4a85c3 | 2016-05-03 18:01:08 +0200 | [diff] [blame] | 1654 | mr->need_inval = false; |
Sagi Grimberg | 17cd3a2 | 2014-02-23 14:19:04 +0200 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | return mr; |
| 1658 | } |
Sagi Grimberg | 9bee178 | 2015-07-30 10:32:35 +0300 | [diff] [blame] | 1659 | EXPORT_SYMBOL(ib_alloc_mr); |
Steve Wise | 00f7ec3 | 2008-07-14 23:48:45 -0700 | [diff] [blame] | 1660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | /* "Fast" memory regions */ |
| 1662 | |
| 1663 | struct ib_fmr *ib_alloc_fmr(struct ib_pd *pd, |
| 1664 | int mr_access_flags, |
| 1665 | struct ib_fmr_attr *fmr_attr) |
| 1666 | { |
| 1667 | struct ib_fmr *fmr; |
| 1668 | |
| 1669 | if (!pd->device->alloc_fmr) |
| 1670 | return ERR_PTR(-ENOSYS); |
| 1671 | |
| 1672 | fmr = pd->device->alloc_fmr(pd, mr_access_flags, fmr_attr); |
| 1673 | if (!IS_ERR(fmr)) { |
| 1674 | fmr->device = pd->device; |
| 1675 | fmr->pd = pd; |
| 1676 | atomic_inc(&pd->usecnt); |
| 1677 | } |
| 1678 | |
| 1679 | return fmr; |
| 1680 | } |
| 1681 | EXPORT_SYMBOL(ib_alloc_fmr); |
| 1682 | |
| 1683 | int ib_unmap_fmr(struct list_head *fmr_list) |
| 1684 | { |
| 1685 | struct ib_fmr *fmr; |
| 1686 | |
| 1687 | if (list_empty(fmr_list)) |
| 1688 | return 0; |
| 1689 | |
| 1690 | fmr = list_entry(fmr_list->next, struct ib_fmr, list); |
| 1691 | return fmr->device->unmap_fmr(fmr_list); |
| 1692 | } |
| 1693 | EXPORT_SYMBOL(ib_unmap_fmr); |
| 1694 | |
| 1695 | int ib_dealloc_fmr(struct ib_fmr *fmr) |
| 1696 | { |
| 1697 | struct ib_pd *pd; |
| 1698 | int ret; |
| 1699 | |
| 1700 | pd = fmr->pd; |
| 1701 | ret = fmr->device->dealloc_fmr(fmr); |
| 1702 | if (!ret) |
| 1703 | atomic_dec(&pd->usecnt); |
| 1704 | |
| 1705 | return ret; |
| 1706 | } |
| 1707 | EXPORT_SYMBOL(ib_dealloc_fmr); |
| 1708 | |
| 1709 | /* Multicast groups */ |
| 1710 | |
Noa Osherovich | 5236333 | 2017-06-12 11:14:02 +0300 | [diff] [blame] | 1711 | static bool is_valid_mcast_lid(struct ib_qp *qp, u16 lid) |
| 1712 | { |
| 1713 | struct ib_qp_init_attr init_attr = {}; |
| 1714 | struct ib_qp_attr attr = {}; |
| 1715 | int num_eth_ports = 0; |
| 1716 | int port; |
| 1717 | |
| 1718 | /* If QP state >= init, it is assigned to a port and we can check this |
| 1719 | * port only. |
| 1720 | */ |
| 1721 | if (!ib_query_qp(qp, &attr, IB_QP_STATE | IB_QP_PORT, &init_attr)) { |
| 1722 | if (attr.qp_state >= IB_QPS_INIT) { |
Alex Estrin | e6f9bc3 | 2017-08-31 09:30:34 -0700 | [diff] [blame] | 1723 | if (rdma_port_get_link_layer(qp->device, attr.port_num) != |
Noa Osherovich | 5236333 | 2017-06-12 11:14:02 +0300 | [diff] [blame] | 1724 | IB_LINK_LAYER_INFINIBAND) |
| 1725 | return true; |
| 1726 | goto lid_check; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | /* Can't get a quick answer, iterate over all ports */ |
| 1731 | for (port = 0; port < qp->device->phys_port_cnt; port++) |
Alex Estrin | e6f9bc3 | 2017-08-31 09:30:34 -0700 | [diff] [blame] | 1732 | if (rdma_port_get_link_layer(qp->device, port) != |
Noa Osherovich | 5236333 | 2017-06-12 11:14:02 +0300 | [diff] [blame] | 1733 | IB_LINK_LAYER_INFINIBAND) |
| 1734 | num_eth_ports++; |
| 1735 | |
| 1736 | /* If we have at lease one Ethernet port, RoCE annex declares that |
| 1737 | * multicast LID should be ignored. We can't tell at this step if the |
| 1738 | * QP belongs to an IB or Ethernet port. |
| 1739 | */ |
| 1740 | if (num_eth_ports) |
| 1741 | return true; |
| 1742 | |
| 1743 | /* If all the ports are IB, we can check according to IB spec. */ |
| 1744 | lid_check: |
| 1745 | return !(lid < be16_to_cpu(IB_MULTICAST_LID_BASE) || |
| 1746 | lid == be16_to_cpu(IB_LID_PERMISSIVE)); |
| 1747 | } |
| 1748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid) |
| 1750 | { |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1751 | int ret; |
| 1752 | |
Jack Morgenstein | 0c33aee | 2005-09-26 11:47:53 -0700 | [diff] [blame] | 1753 | if (!qp->device->attach_mcast) |
| 1754 | return -ENOSYS; |
Noa Osherovich | be1d325 | 2017-06-12 11:14:03 +0300 | [diff] [blame] | 1755 | |
| 1756 | if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) || |
| 1757 | qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid)) |
Jack Morgenstein | 0c33aee | 2005-09-26 11:47:53 -0700 | [diff] [blame] | 1758 | return -EINVAL; |
| 1759 | |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1760 | ret = qp->device->attach_mcast(qp, gid, lid); |
| 1761 | if (!ret) |
| 1762 | atomic_inc(&qp->usecnt); |
| 1763 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | } |
| 1765 | EXPORT_SYMBOL(ib_attach_mcast); |
| 1766 | |
| 1767 | int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid) |
| 1768 | { |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1769 | int ret; |
| 1770 | |
Jack Morgenstein | 0c33aee | 2005-09-26 11:47:53 -0700 | [diff] [blame] | 1771 | if (!qp->device->detach_mcast) |
| 1772 | return -ENOSYS; |
Noa Osherovich | be1d325 | 2017-06-12 11:14:03 +0300 | [diff] [blame] | 1773 | |
| 1774 | if (!rdma_is_multicast_addr((struct in6_addr *)gid->raw) || |
| 1775 | qp->qp_type != IB_QPT_UD || !is_valid_mcast_lid(qp, lid)) |
Jack Morgenstein | 0c33aee | 2005-09-26 11:47:53 -0700 | [diff] [blame] | 1776 | return -EINVAL; |
| 1777 | |
Or Gerlitz | c3bccbfb | 2012-04-29 17:04:22 +0300 | [diff] [blame] | 1778 | ret = qp->device->detach_mcast(qp, gid, lid); |
| 1779 | if (!ret) |
| 1780 | atomic_dec(&qp->usecnt); |
| 1781 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } |
| 1783 | EXPORT_SYMBOL(ib_detach_mcast); |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1784 | |
Leon Romanovsky | f66c8ba | 2018-01-28 11:17:19 +0200 | [diff] [blame^] | 1785 | struct ib_xrcd *__ib_alloc_xrcd(struct ib_device *device, const char *caller) |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1786 | { |
| 1787 | struct ib_xrcd *xrcd; |
| 1788 | |
| 1789 | if (!device->alloc_xrcd) |
| 1790 | return ERR_PTR(-ENOSYS); |
| 1791 | |
| 1792 | xrcd = device->alloc_xrcd(device, NULL, NULL); |
| 1793 | if (!IS_ERR(xrcd)) { |
| 1794 | xrcd->device = device; |
Sean Hefty | 53d0bd1 | 2011-05-24 08:33:46 -0700 | [diff] [blame] | 1795 | xrcd->inode = NULL; |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1796 | atomic_set(&xrcd->usecnt, 0); |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 1797 | mutex_init(&xrcd->tgt_qp_mutex); |
| 1798 | INIT_LIST_HEAD(&xrcd->tgt_qp_list); |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | return xrcd; |
| 1802 | } |
Leon Romanovsky | f66c8ba | 2018-01-28 11:17:19 +0200 | [diff] [blame^] | 1803 | EXPORT_SYMBOL(__ib_alloc_xrcd); |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1804 | |
| 1805 | int ib_dealloc_xrcd(struct ib_xrcd *xrcd) |
| 1806 | { |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 1807 | struct ib_qp *qp; |
| 1808 | int ret; |
| 1809 | |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1810 | if (atomic_read(&xrcd->usecnt)) |
| 1811 | return -EBUSY; |
| 1812 | |
Sean Hefty | d3d72d9 | 2011-05-26 23:06:44 -0700 | [diff] [blame] | 1813 | while (!list_empty(&xrcd->tgt_qp_list)) { |
| 1814 | qp = list_entry(xrcd->tgt_qp_list.next, struct ib_qp, xrcd_list); |
| 1815 | ret = ib_destroy_qp(qp); |
| 1816 | if (ret) |
| 1817 | return ret; |
| 1818 | } |
| 1819 | |
Sean Hefty | 59991f9 | 2011-05-23 17:52:46 -0700 | [diff] [blame] | 1820 | return xrcd->device->dealloc_xrcd(xrcd); |
| 1821 | } |
| 1822 | EXPORT_SYMBOL(ib_dealloc_xrcd); |
Hadar Hen Zion | 319a441 | 2013-08-07 14:01:59 +0300 | [diff] [blame] | 1823 | |
Yishai Hadas | 5fd251c | 2016-05-23 15:20:48 +0300 | [diff] [blame] | 1824 | /** |
| 1825 | * ib_create_wq - Creates a WQ associated with the specified protection |
| 1826 | * domain. |
| 1827 | * @pd: The protection domain associated with the WQ. |
Randy Dunlap | 1f58621 | 2018-01-05 16:21:40 -0800 | [diff] [blame] | 1828 | * @wq_attr: A list of initial attributes required to create the |
Yishai Hadas | 5fd251c | 2016-05-23 15:20:48 +0300 | [diff] [blame] | 1829 | * WQ. If WQ creation succeeds, then the attributes are updated to |
| 1830 | * the actual capabilities of the created WQ. |
| 1831 | * |
Randy Dunlap | 1f58621 | 2018-01-05 16:21:40 -0800 | [diff] [blame] | 1832 | * wq_attr->max_wr and wq_attr->max_sge determine |
Yishai Hadas | 5fd251c | 2016-05-23 15:20:48 +0300 | [diff] [blame] | 1833 | * the requested size of the WQ, and set to the actual values allocated |
| 1834 | * on return. |
| 1835 | * If ib_create_wq() succeeds, then max_wr and max_sge will always be |
| 1836 | * at least as large as the requested values. |
| 1837 | */ |
| 1838 | struct ib_wq *ib_create_wq(struct ib_pd *pd, |
| 1839 | struct ib_wq_init_attr *wq_attr) |
| 1840 | { |
| 1841 | struct ib_wq *wq; |
| 1842 | |
| 1843 | if (!pd->device->create_wq) |
| 1844 | return ERR_PTR(-ENOSYS); |
| 1845 | |
| 1846 | wq = pd->device->create_wq(pd, wq_attr, NULL); |
| 1847 | if (!IS_ERR(wq)) { |
| 1848 | wq->event_handler = wq_attr->event_handler; |
| 1849 | wq->wq_context = wq_attr->wq_context; |
| 1850 | wq->wq_type = wq_attr->wq_type; |
| 1851 | wq->cq = wq_attr->cq; |
| 1852 | wq->device = pd->device; |
| 1853 | wq->pd = pd; |
| 1854 | wq->uobject = NULL; |
| 1855 | atomic_inc(&pd->usecnt); |
| 1856 | atomic_inc(&wq_attr->cq->usecnt); |
| 1857 | atomic_set(&wq->usecnt, 0); |
| 1858 | } |
| 1859 | return wq; |
| 1860 | } |
| 1861 | EXPORT_SYMBOL(ib_create_wq); |
| 1862 | |
| 1863 | /** |
| 1864 | * ib_destroy_wq - Destroys the specified WQ. |
| 1865 | * @wq: The WQ to destroy. |
| 1866 | */ |
| 1867 | int ib_destroy_wq(struct ib_wq *wq) |
| 1868 | { |
| 1869 | int err; |
| 1870 | struct ib_cq *cq = wq->cq; |
| 1871 | struct ib_pd *pd = wq->pd; |
| 1872 | |
| 1873 | if (atomic_read(&wq->usecnt)) |
| 1874 | return -EBUSY; |
| 1875 | |
| 1876 | err = wq->device->destroy_wq(wq); |
| 1877 | if (!err) { |
| 1878 | atomic_dec(&pd->usecnt); |
| 1879 | atomic_dec(&cq->usecnt); |
| 1880 | } |
| 1881 | return err; |
| 1882 | } |
| 1883 | EXPORT_SYMBOL(ib_destroy_wq); |
| 1884 | |
| 1885 | /** |
| 1886 | * ib_modify_wq - Modifies the specified WQ. |
| 1887 | * @wq: The WQ to modify. |
| 1888 | * @wq_attr: On input, specifies the WQ attributes to modify. |
| 1889 | * @wq_attr_mask: A bit-mask used to specify which attributes of the WQ |
| 1890 | * are being modified. |
| 1891 | * On output, the current values of selected WQ attributes are returned. |
| 1892 | */ |
| 1893 | int ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr, |
| 1894 | u32 wq_attr_mask) |
| 1895 | { |
| 1896 | int err; |
| 1897 | |
| 1898 | if (!wq->device->modify_wq) |
| 1899 | return -ENOSYS; |
| 1900 | |
| 1901 | err = wq->device->modify_wq(wq, wq_attr, wq_attr_mask, NULL); |
| 1902 | return err; |
| 1903 | } |
| 1904 | EXPORT_SYMBOL(ib_modify_wq); |
| 1905 | |
Yishai Hadas | 6d39786 | 2016-05-23 15:20:51 +0300 | [diff] [blame] | 1906 | /* |
| 1907 | * ib_create_rwq_ind_table - Creates a RQ Indirection Table. |
| 1908 | * @device: The device on which to create the rwq indirection table. |
| 1909 | * @ib_rwq_ind_table_init_attr: A list of initial attributes required to |
| 1910 | * create the Indirection Table. |
| 1911 | * |
| 1912 | * Note: The life time of ib_rwq_ind_table_init_attr->ind_tbl is not less |
| 1913 | * than the created ib_rwq_ind_table object and the caller is responsible |
| 1914 | * for its memory allocation/free. |
| 1915 | */ |
| 1916 | struct ib_rwq_ind_table *ib_create_rwq_ind_table(struct ib_device *device, |
| 1917 | struct ib_rwq_ind_table_init_attr *init_attr) |
| 1918 | { |
| 1919 | struct ib_rwq_ind_table *rwq_ind_table; |
| 1920 | int i; |
| 1921 | u32 table_size; |
| 1922 | |
| 1923 | if (!device->create_rwq_ind_table) |
| 1924 | return ERR_PTR(-ENOSYS); |
| 1925 | |
| 1926 | table_size = (1 << init_attr->log_ind_tbl_size); |
| 1927 | rwq_ind_table = device->create_rwq_ind_table(device, |
| 1928 | init_attr, NULL); |
| 1929 | if (IS_ERR(rwq_ind_table)) |
| 1930 | return rwq_ind_table; |
| 1931 | |
| 1932 | rwq_ind_table->ind_tbl = init_attr->ind_tbl; |
| 1933 | rwq_ind_table->log_ind_tbl_size = init_attr->log_ind_tbl_size; |
| 1934 | rwq_ind_table->device = device; |
| 1935 | rwq_ind_table->uobject = NULL; |
| 1936 | atomic_set(&rwq_ind_table->usecnt, 0); |
| 1937 | |
| 1938 | for (i = 0; i < table_size; i++) |
| 1939 | atomic_inc(&rwq_ind_table->ind_tbl[i]->usecnt); |
| 1940 | |
| 1941 | return rwq_ind_table; |
| 1942 | } |
| 1943 | EXPORT_SYMBOL(ib_create_rwq_ind_table); |
| 1944 | |
| 1945 | /* |
| 1946 | * ib_destroy_rwq_ind_table - Destroys the specified Indirection Table. |
| 1947 | * @wq_ind_table: The Indirection Table to destroy. |
| 1948 | */ |
| 1949 | int ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *rwq_ind_table) |
| 1950 | { |
| 1951 | int err, i; |
| 1952 | u32 table_size = (1 << rwq_ind_table->log_ind_tbl_size); |
| 1953 | struct ib_wq **ind_tbl = rwq_ind_table->ind_tbl; |
| 1954 | |
| 1955 | if (atomic_read(&rwq_ind_table->usecnt)) |
| 1956 | return -EBUSY; |
| 1957 | |
| 1958 | err = rwq_ind_table->device->destroy_rwq_ind_table(rwq_ind_table); |
| 1959 | if (!err) { |
| 1960 | for (i = 0; i < table_size; i++) |
| 1961 | atomic_dec(&ind_tbl[i]->usecnt); |
| 1962 | } |
| 1963 | |
| 1964 | return err; |
| 1965 | } |
| 1966 | EXPORT_SYMBOL(ib_destroy_rwq_ind_table); |
| 1967 | |
Hadar Hen Zion | 319a441 | 2013-08-07 14:01:59 +0300 | [diff] [blame] | 1968 | struct ib_flow *ib_create_flow(struct ib_qp *qp, |
| 1969 | struct ib_flow_attr *flow_attr, |
| 1970 | int domain) |
| 1971 | { |
| 1972 | struct ib_flow *flow_id; |
| 1973 | if (!qp->device->create_flow) |
| 1974 | return ERR_PTR(-ENOSYS); |
| 1975 | |
| 1976 | flow_id = qp->device->create_flow(qp, flow_attr, domain); |
Mark Bloch | 8ecc798 | 2016-10-27 16:36:30 +0300 | [diff] [blame] | 1977 | if (!IS_ERR(flow_id)) { |
Hadar Hen Zion | 319a441 | 2013-08-07 14:01:59 +0300 | [diff] [blame] | 1978 | atomic_inc(&qp->usecnt); |
Mark Bloch | 8ecc798 | 2016-10-27 16:36:30 +0300 | [diff] [blame] | 1979 | flow_id->qp = qp; |
| 1980 | } |
Hadar Hen Zion | 319a441 | 2013-08-07 14:01:59 +0300 | [diff] [blame] | 1981 | return flow_id; |
| 1982 | } |
| 1983 | EXPORT_SYMBOL(ib_create_flow); |
| 1984 | |
| 1985 | int ib_destroy_flow(struct ib_flow *flow_id) |
| 1986 | { |
| 1987 | int err; |
| 1988 | struct ib_qp *qp = flow_id->qp; |
| 1989 | |
| 1990 | err = qp->device->destroy_flow(flow_id); |
| 1991 | if (!err) |
| 1992 | atomic_dec(&qp->usecnt); |
| 1993 | return err; |
| 1994 | } |
| 1995 | EXPORT_SYMBOL(ib_destroy_flow); |
Sagi Grimberg | 1b01d33 | 2014-02-23 14:19:05 +0200 | [diff] [blame] | 1996 | |
| 1997 | int ib_check_mr_status(struct ib_mr *mr, u32 check_mask, |
| 1998 | struct ib_mr_status *mr_status) |
| 1999 | { |
| 2000 | return mr->device->check_mr_status ? |
| 2001 | mr->device->check_mr_status(mr, check_mask, mr_status) : -ENOSYS; |
| 2002 | } |
| 2003 | EXPORT_SYMBOL(ib_check_mr_status); |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2004 | |
Eli Cohen | 50174a7 | 2016-03-11 22:58:38 +0200 | [diff] [blame] | 2005 | int ib_set_vf_link_state(struct ib_device *device, int vf, u8 port, |
| 2006 | int state) |
| 2007 | { |
| 2008 | if (!device->set_vf_link_state) |
| 2009 | return -ENOSYS; |
| 2010 | |
| 2011 | return device->set_vf_link_state(device, vf, port, state); |
| 2012 | } |
| 2013 | EXPORT_SYMBOL(ib_set_vf_link_state); |
| 2014 | |
| 2015 | int ib_get_vf_config(struct ib_device *device, int vf, u8 port, |
| 2016 | struct ifla_vf_info *info) |
| 2017 | { |
| 2018 | if (!device->get_vf_config) |
| 2019 | return -ENOSYS; |
| 2020 | |
| 2021 | return device->get_vf_config(device, vf, port, info); |
| 2022 | } |
| 2023 | EXPORT_SYMBOL(ib_get_vf_config); |
| 2024 | |
| 2025 | int ib_get_vf_stats(struct ib_device *device, int vf, u8 port, |
| 2026 | struct ifla_vf_stats *stats) |
| 2027 | { |
| 2028 | if (!device->get_vf_stats) |
| 2029 | return -ENOSYS; |
| 2030 | |
| 2031 | return device->get_vf_stats(device, vf, port, stats); |
| 2032 | } |
| 2033 | EXPORT_SYMBOL(ib_get_vf_stats); |
| 2034 | |
| 2035 | int ib_set_vf_guid(struct ib_device *device, int vf, u8 port, u64 guid, |
| 2036 | int type) |
| 2037 | { |
| 2038 | if (!device->set_vf_guid) |
| 2039 | return -ENOSYS; |
| 2040 | |
| 2041 | return device->set_vf_guid(device, vf, port, guid, type); |
| 2042 | } |
| 2043 | EXPORT_SYMBOL(ib_set_vf_guid); |
| 2044 | |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2045 | /** |
| 2046 | * ib_map_mr_sg() - Map the largest prefix of a dma mapped SG list |
| 2047 | * and set it the memory region. |
| 2048 | * @mr: memory region |
| 2049 | * @sg: dma mapped scatterlist |
| 2050 | * @sg_nents: number of entries in sg |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2051 | * @sg_offset: offset in bytes into sg |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2052 | * @page_size: page vector desired page size |
| 2053 | * |
| 2054 | * Constraints: |
| 2055 | * - The first sg element is allowed to have an offset. |
Bart Van Assche | 5274612 | 2016-09-26 09:09:42 -0700 | [diff] [blame] | 2056 | * - Each sg element must either be aligned to page_size or virtually |
| 2057 | * contiguous to the previous element. In case an sg element has a |
| 2058 | * non-contiguous offset, the mapping prefix will not include it. |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2059 | * - The last sg element is allowed to have length less than page_size. |
| 2060 | * - If sg_nents total byte length exceeds the mr max_num_sge * page_size |
| 2061 | * then only max_num_sg entries will be mapped. |
Bart Van Assche | 5274612 | 2016-09-26 09:09:42 -0700 | [diff] [blame] | 2062 | * - If the MR was allocated with type IB_MR_TYPE_SG_GAPS, none of these |
Sagi Grimberg | f5aa915 | 2016-02-29 19:07:32 +0200 | [diff] [blame] | 2063 | * constraints holds and the page_size argument is ignored. |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2064 | * |
| 2065 | * Returns the number of sg elements that were mapped to the memory region. |
| 2066 | * |
| 2067 | * After this completes successfully, the memory region |
| 2068 | * is ready for registration. |
| 2069 | */ |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2070 | int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents, |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2071 | unsigned int *sg_offset, unsigned int page_size) |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2072 | { |
| 2073 | if (unlikely(!mr->device->map_mr_sg)) |
| 2074 | return -ENOSYS; |
| 2075 | |
| 2076 | mr->page_size = page_size; |
| 2077 | |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2078 | return mr->device->map_mr_sg(mr, sg, sg_nents, sg_offset); |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2079 | } |
| 2080 | EXPORT_SYMBOL(ib_map_mr_sg); |
| 2081 | |
| 2082 | /** |
| 2083 | * ib_sg_to_pages() - Convert the largest prefix of a sg list |
| 2084 | * to a page vector |
| 2085 | * @mr: memory region |
| 2086 | * @sgl: dma mapped scatterlist |
| 2087 | * @sg_nents: number of entries in sg |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2088 | * @sg_offset_p: IN: start offset in bytes into sg |
| 2089 | * OUT: offset in bytes for element n of the sg of the first |
| 2090 | * byte that has not been processed where n is the return |
| 2091 | * value of this function. |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2092 | * @set_page: driver page assignment function pointer |
| 2093 | * |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 2094 | * Core service helper for drivers to convert the largest |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2095 | * prefix of given sg list to a page vector. The sg list |
| 2096 | * prefix converted is the prefix that meet the requirements |
| 2097 | * of ib_map_mr_sg. |
| 2098 | * |
| 2099 | * Returns the number of sg elements that were assigned to |
| 2100 | * a page vector. |
| 2101 | */ |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2102 | int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents, |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2103 | unsigned int *sg_offset_p, int (*set_page)(struct ib_mr *, u64)) |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2104 | { |
| 2105 | struct scatterlist *sg; |
Bart Van Assche | b6aeb98 | 2015-12-29 10:45:03 +0100 | [diff] [blame] | 2106 | u64 last_end_dma_addr = 0; |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2107 | unsigned int sg_offset = sg_offset_p ? *sg_offset_p : 0; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2108 | unsigned int last_page_off = 0; |
| 2109 | u64 page_mask = ~((u64)mr->page_size - 1); |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 2110 | int i, ret; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2111 | |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2112 | if (unlikely(sg_nents <= 0 || sg_offset > sg_dma_len(&sgl[0]))) |
| 2113 | return -EINVAL; |
| 2114 | |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2115 | mr->iova = sg_dma_address(&sgl[0]) + sg_offset; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2116 | mr->length = 0; |
| 2117 | |
| 2118 | for_each_sg(sgl, sg, sg_nents, i) { |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2119 | u64 dma_addr = sg_dma_address(sg) + sg_offset; |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2120 | u64 prev_addr = dma_addr; |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2121 | unsigned int dma_len = sg_dma_len(sg) - sg_offset; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2122 | u64 end_dma_addr = dma_addr + dma_len; |
| 2123 | u64 page_addr = dma_addr & page_mask; |
| 2124 | |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 2125 | /* |
| 2126 | * For the second and later elements, check whether either the |
| 2127 | * end of element i-1 or the start of element i is not aligned |
| 2128 | * on a page boundary. |
| 2129 | */ |
| 2130 | if (i && (last_page_off != 0 || page_addr != dma_addr)) { |
| 2131 | /* Stop mapping if there is a gap. */ |
| 2132 | if (last_end_dma_addr != dma_addr) |
| 2133 | break; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2134 | |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 2135 | /* |
| 2136 | * Coalesce this element with the last. If it is small |
| 2137 | * enough just update mr->length. Otherwise start |
| 2138 | * mapping from the next page. |
| 2139 | */ |
| 2140 | goto next_page; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | do { |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 2144 | ret = set_page(mr, page_addr); |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2145 | if (unlikely(ret < 0)) { |
| 2146 | sg_offset = prev_addr - sg_dma_address(sg); |
| 2147 | mr->length += prev_addr - dma_addr; |
| 2148 | if (sg_offset_p) |
| 2149 | *sg_offset_p = sg_offset; |
| 2150 | return i || sg_offset ? i : ret; |
| 2151 | } |
| 2152 | prev_addr = page_addr; |
Bart Van Assche | 8f5ba10 | 2015-12-03 16:04:17 -0800 | [diff] [blame] | 2153 | next_page: |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2154 | page_addr += mr->page_size; |
| 2155 | } while (page_addr < end_dma_addr); |
| 2156 | |
| 2157 | mr->length += dma_len; |
| 2158 | last_end_dma_addr = end_dma_addr; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2159 | last_page_off = end_dma_addr & ~page_mask; |
Christoph Hellwig | ff2ba99 | 2016-05-03 18:01:04 +0200 | [diff] [blame] | 2160 | |
| 2161 | sg_offset = 0; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2162 | } |
| 2163 | |
Bart Van Assche | 9aa8b32 | 2016-05-12 10:49:15 -0700 | [diff] [blame] | 2164 | if (sg_offset_p) |
| 2165 | *sg_offset_p = 0; |
Sagi Grimberg | 4c67e2b | 2015-10-13 19:11:24 +0300 | [diff] [blame] | 2166 | return i; |
| 2167 | } |
| 2168 | EXPORT_SYMBOL(ib_sg_to_pages); |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2169 | |
| 2170 | struct ib_drain_cqe { |
| 2171 | struct ib_cqe cqe; |
| 2172 | struct completion done; |
| 2173 | }; |
| 2174 | |
| 2175 | static void ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc) |
| 2176 | { |
| 2177 | struct ib_drain_cqe *cqe = container_of(wc->wr_cqe, struct ib_drain_cqe, |
| 2178 | cqe); |
| 2179 | |
| 2180 | complete(&cqe->done); |
| 2181 | } |
| 2182 | |
| 2183 | /* |
| 2184 | * Post a WR and block until its completion is reaped for the SQ. |
| 2185 | */ |
| 2186 | static void __ib_drain_sq(struct ib_qp *qp) |
| 2187 | { |
Bart Van Assche | f039f44 | 2017-02-14 10:56:35 -0800 | [diff] [blame] | 2188 | struct ib_cq *cq = qp->send_cq; |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2189 | struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR }; |
| 2190 | struct ib_drain_cqe sdrain; |
| 2191 | struct ib_send_wr swr = {}, *bad_swr; |
| 2192 | int ret; |
| 2193 | |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2194 | ret = ib_modify_qp(qp, &attr, IB_QP_STATE); |
| 2195 | if (ret) { |
| 2196 | WARN_ONCE(ret, "failed to drain send queue: %d\n", ret); |
| 2197 | return; |
| 2198 | } |
| 2199 | |
Max Gurtovoy | aaebd37 | 2018-01-14 17:07:48 +0200 | [diff] [blame] | 2200 | swr.wr_cqe = &sdrain.cqe; |
| 2201 | sdrain.cqe.done = ib_drain_qp_done; |
| 2202 | init_completion(&sdrain.done); |
| 2203 | |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2204 | ret = ib_post_send(qp, &swr, &bad_swr); |
| 2205 | if (ret) { |
| 2206 | WARN_ONCE(ret, "failed to drain send queue: %d\n", ret); |
| 2207 | return; |
| 2208 | } |
| 2209 | |
Bart Van Assche | f039f44 | 2017-02-14 10:56:35 -0800 | [diff] [blame] | 2210 | if (cq->poll_ctx == IB_POLL_DIRECT) |
| 2211 | while (wait_for_completion_timeout(&sdrain.done, HZ / 10) <= 0) |
| 2212 | ib_process_cq_direct(cq, -1); |
| 2213 | else |
| 2214 | wait_for_completion(&sdrain.done); |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | /* |
| 2218 | * Post a WR and block until its completion is reaped for the RQ. |
| 2219 | */ |
| 2220 | static void __ib_drain_rq(struct ib_qp *qp) |
| 2221 | { |
Bart Van Assche | f039f44 | 2017-02-14 10:56:35 -0800 | [diff] [blame] | 2222 | struct ib_cq *cq = qp->recv_cq; |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2223 | struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR }; |
| 2224 | struct ib_drain_cqe rdrain; |
| 2225 | struct ib_recv_wr rwr = {}, *bad_rwr; |
| 2226 | int ret; |
| 2227 | |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2228 | ret = ib_modify_qp(qp, &attr, IB_QP_STATE); |
| 2229 | if (ret) { |
| 2230 | WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret); |
| 2231 | return; |
| 2232 | } |
| 2233 | |
Max Gurtovoy | aaebd37 | 2018-01-14 17:07:48 +0200 | [diff] [blame] | 2234 | rwr.wr_cqe = &rdrain.cqe; |
| 2235 | rdrain.cqe.done = ib_drain_qp_done; |
| 2236 | init_completion(&rdrain.done); |
| 2237 | |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2238 | ret = ib_post_recv(qp, &rwr, &bad_rwr); |
| 2239 | if (ret) { |
| 2240 | WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret); |
| 2241 | return; |
| 2242 | } |
| 2243 | |
Bart Van Assche | f039f44 | 2017-02-14 10:56:35 -0800 | [diff] [blame] | 2244 | if (cq->poll_ctx == IB_POLL_DIRECT) |
| 2245 | while (wait_for_completion_timeout(&rdrain.done, HZ / 10) <= 0) |
| 2246 | ib_process_cq_direct(cq, -1); |
| 2247 | else |
| 2248 | wait_for_completion(&rdrain.done); |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | /** |
| 2252 | * ib_drain_sq() - Block until all SQ CQEs have been consumed by the |
| 2253 | * application. |
| 2254 | * @qp: queue pair to drain |
| 2255 | * |
| 2256 | * If the device has a provider-specific drain function, then |
| 2257 | * call that. Otherwise call the generic drain function |
| 2258 | * __ib_drain_sq(). |
| 2259 | * |
| 2260 | * The caller must: |
| 2261 | * |
| 2262 | * ensure there is room in the CQ and SQ for the drain work request and |
| 2263 | * completion. |
| 2264 | * |
Bart Van Assche | f039f44 | 2017-02-14 10:56:35 -0800 | [diff] [blame] | 2265 | * allocate the CQ using ib_alloc_cq(). |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2266 | * |
| 2267 | * ensure that there are no other contexts that are posting WRs concurrently. |
| 2268 | * Otherwise the drain is not guaranteed. |
| 2269 | */ |
| 2270 | void ib_drain_sq(struct ib_qp *qp) |
| 2271 | { |
| 2272 | if (qp->device->drain_sq) |
| 2273 | qp->device->drain_sq(qp); |
| 2274 | else |
| 2275 | __ib_drain_sq(qp); |
| 2276 | } |
| 2277 | EXPORT_SYMBOL(ib_drain_sq); |
| 2278 | |
| 2279 | /** |
| 2280 | * ib_drain_rq() - Block until all RQ CQEs have been consumed by the |
| 2281 | * application. |
| 2282 | * @qp: queue pair to drain |
| 2283 | * |
| 2284 | * If the device has a provider-specific drain function, then |
| 2285 | * call that. Otherwise call the generic drain function |
| 2286 | * __ib_drain_rq(). |
| 2287 | * |
| 2288 | * The caller must: |
| 2289 | * |
| 2290 | * ensure there is room in the CQ and RQ for the drain work request and |
| 2291 | * completion. |
| 2292 | * |
Bart Van Assche | f039f44 | 2017-02-14 10:56:35 -0800 | [diff] [blame] | 2293 | * allocate the CQ using ib_alloc_cq(). |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2294 | * |
| 2295 | * ensure that there are no other contexts that are posting WRs concurrently. |
| 2296 | * Otherwise the drain is not guaranteed. |
| 2297 | */ |
| 2298 | void ib_drain_rq(struct ib_qp *qp) |
| 2299 | { |
| 2300 | if (qp->device->drain_rq) |
| 2301 | qp->device->drain_rq(qp); |
| 2302 | else |
| 2303 | __ib_drain_rq(qp); |
| 2304 | } |
| 2305 | EXPORT_SYMBOL(ib_drain_rq); |
| 2306 | |
| 2307 | /** |
| 2308 | * ib_drain_qp() - Block until all CQEs have been consumed by the |
| 2309 | * application on both the RQ and SQ. |
| 2310 | * @qp: queue pair to drain |
| 2311 | * |
| 2312 | * The caller must: |
| 2313 | * |
| 2314 | * ensure there is room in the CQ(s), SQ, and RQ for drain work requests |
| 2315 | * and completions. |
| 2316 | * |
Bart Van Assche | f039f44 | 2017-02-14 10:56:35 -0800 | [diff] [blame] | 2317 | * allocate the CQs using ib_alloc_cq(). |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2318 | * |
| 2319 | * ensure that there are no other contexts that are posting WRs concurrently. |
| 2320 | * Otherwise the drain is not guaranteed. |
| 2321 | */ |
| 2322 | void ib_drain_qp(struct ib_qp *qp) |
| 2323 | { |
| 2324 | ib_drain_sq(qp); |
Sagi Grimberg | 42235f8 | 2016-04-26 17:55:38 +0300 | [diff] [blame] | 2325 | if (!qp->srq) |
| 2326 | ib_drain_rq(qp); |
Steve Wise | 765d677 | 2016-02-17 08:15:41 -0800 | [diff] [blame] | 2327 | } |
| 2328 | EXPORT_SYMBOL(ib_drain_qp); |