Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, Mellanox Technologies. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the |
| 8 | * OpenIB.org BSD license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or |
| 11 | * without modification, are permitted provided that the following |
| 12 | * conditions are met: |
| 13 | * |
| 14 | * - Redistributions of source code must retain the above |
| 15 | * copyright notice, this list of conditions and the following |
| 16 | * disclaimer. |
| 17 | * |
| 18 | * - Redistributions in binary form must reproduce the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer in the documentation and/or other materials |
| 21 | * provided with the distribution. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | #include "mlx5_ib.h" |
| 34 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 35 | struct mlx5_ib_gsi_wr { |
| 36 | struct ib_cqe cqe; |
| 37 | struct ib_wc wc; |
| 38 | int send_flags; |
| 39 | bool completed:1; |
| 40 | }; |
| 41 | |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 42 | struct mlx5_ib_gsi_qp { |
| 43 | struct ib_qp ibqp; |
| 44 | struct ib_qp *rx_qp; |
| 45 | u8 port_num; |
| 46 | struct ib_qp_cap cap; |
| 47 | enum ib_sig_type sq_sig_type; |
| 48 | /* Serialize qp state modifications */ |
| 49 | struct mutex mutex; |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 50 | struct ib_cq *cq; |
| 51 | struct mlx5_ib_gsi_wr *outstanding_wrs; |
| 52 | u32 outstanding_pi, outstanding_ci; |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 53 | int num_qps; |
| 54 | /* Protects access to the tx_qps. Post send operations synchronize |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 55 | * with tx_qp creation in setup_qp(). Also protects the |
| 56 | * outstanding_wrs array and indices. |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 57 | */ |
| 58 | spinlock_t lock; |
| 59 | struct ib_qp **tx_qps; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static struct mlx5_ib_gsi_qp *gsi_qp(struct ib_qp *qp) |
| 63 | { |
| 64 | return container_of(qp, struct mlx5_ib_gsi_qp, ibqp); |
| 65 | } |
| 66 | |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 67 | static bool mlx5_ib_deth_sqpn_cap(struct mlx5_ib_dev *dev) |
| 68 | { |
| 69 | return MLX5_CAP_GEN(dev->mdev, set_deth_sqpn); |
| 70 | } |
| 71 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 72 | static u32 next_outstanding(struct mlx5_ib_gsi_qp *gsi, u32 index) |
| 73 | { |
| 74 | return ++index % gsi->cap.max_send_wr; |
| 75 | } |
| 76 | |
| 77 | #define for_each_outstanding_wr(gsi, index) \ |
| 78 | for (index = gsi->outstanding_ci; index != gsi->outstanding_pi; \ |
| 79 | index = next_outstanding(gsi, index)) |
| 80 | |
| 81 | /* Call with gsi->lock locked */ |
| 82 | static void generate_completions(struct mlx5_ib_gsi_qp *gsi) |
| 83 | { |
| 84 | struct ib_cq *gsi_cq = gsi->ibqp.send_cq; |
| 85 | struct mlx5_ib_gsi_wr *wr; |
| 86 | u32 index; |
| 87 | |
| 88 | for_each_outstanding_wr(gsi, index) { |
| 89 | wr = &gsi->outstanding_wrs[index]; |
| 90 | |
| 91 | if (!wr->completed) |
| 92 | break; |
| 93 | |
| 94 | if (gsi->sq_sig_type == IB_SIGNAL_ALL_WR || |
| 95 | wr->send_flags & IB_SEND_SIGNALED) |
| 96 | WARN_ON_ONCE(mlx5_ib_generate_wc(gsi_cq, &wr->wc)); |
| 97 | |
| 98 | wr->completed = false; |
| 99 | } |
| 100 | |
| 101 | gsi->outstanding_ci = index; |
| 102 | } |
| 103 | |
| 104 | static void handle_single_completion(struct ib_cq *cq, struct ib_wc *wc) |
| 105 | { |
| 106 | struct mlx5_ib_gsi_qp *gsi = cq->cq_context; |
| 107 | struct mlx5_ib_gsi_wr *wr = |
| 108 | container_of(wc->wr_cqe, struct mlx5_ib_gsi_wr, cqe); |
| 109 | u64 wr_id; |
| 110 | unsigned long flags; |
| 111 | |
| 112 | spin_lock_irqsave(&gsi->lock, flags); |
| 113 | wr->completed = true; |
| 114 | wr_id = wr->wc.wr_id; |
| 115 | wr->wc = *wc; |
| 116 | wr->wc.wr_id = wr_id; |
| 117 | wr->wc.qp = &gsi->ibqp; |
| 118 | |
| 119 | generate_completions(gsi); |
| 120 | spin_unlock_irqrestore(&gsi->lock, flags); |
| 121 | } |
| 122 | |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 123 | struct ib_qp *mlx5_ib_gsi_create_qp(struct ib_pd *pd, |
| 124 | struct ib_qp_init_attr *init_attr) |
| 125 | { |
| 126 | struct mlx5_ib_dev *dev = to_mdev(pd->device); |
| 127 | struct mlx5_ib_gsi_qp *gsi; |
| 128 | struct ib_qp_init_attr hw_init_attr = *init_attr; |
| 129 | const u8 port_num = init_attr->port_num; |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 130 | const int num_pkeys = pd->device->attrs.max_pkeys; |
| 131 | const int num_qps = mlx5_ib_deth_sqpn_cap(dev) ? num_pkeys : 0; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 132 | int ret; |
| 133 | |
| 134 | mlx5_ib_dbg(dev, "creating GSI QP\n"); |
| 135 | |
| 136 | if (port_num > ARRAY_SIZE(dev->devr.ports) || port_num < 1) { |
| 137 | mlx5_ib_warn(dev, |
| 138 | "invalid port number %d during GSI QP creation\n", |
| 139 | port_num); |
| 140 | return ERR_PTR(-EINVAL); |
| 141 | } |
| 142 | |
| 143 | gsi = kzalloc(sizeof(*gsi), GFP_KERNEL); |
| 144 | if (!gsi) |
| 145 | return ERR_PTR(-ENOMEM); |
| 146 | |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 147 | gsi->tx_qps = kcalloc(num_qps, sizeof(*gsi->tx_qps), GFP_KERNEL); |
| 148 | if (!gsi->tx_qps) { |
| 149 | ret = -ENOMEM; |
| 150 | goto err_free; |
| 151 | } |
| 152 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 153 | gsi->outstanding_wrs = kcalloc(init_attr->cap.max_send_wr, |
| 154 | sizeof(*gsi->outstanding_wrs), |
| 155 | GFP_KERNEL); |
| 156 | if (!gsi->outstanding_wrs) { |
| 157 | ret = -ENOMEM; |
| 158 | goto err_free_tx; |
| 159 | } |
| 160 | |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 161 | mutex_init(&gsi->mutex); |
| 162 | |
| 163 | mutex_lock(&dev->devr.mutex); |
| 164 | |
| 165 | if (dev->devr.ports[port_num - 1].gsi) { |
| 166 | mlx5_ib_warn(dev, "GSI QP already exists on port %d\n", |
| 167 | port_num); |
| 168 | ret = -EBUSY; |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 169 | goto err_free_wrs; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 170 | } |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 171 | gsi->num_qps = num_qps; |
| 172 | spin_lock_init(&gsi->lock); |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 173 | |
| 174 | gsi->cap = init_attr->cap; |
| 175 | gsi->sq_sig_type = init_attr->sq_sig_type; |
| 176 | gsi->ibqp.qp_num = 1; |
| 177 | gsi->port_num = port_num; |
| 178 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 179 | gsi->cq = ib_alloc_cq(pd->device, gsi, init_attr->cap.max_send_wr, 0, |
| 180 | IB_POLL_SOFTIRQ); |
| 181 | if (IS_ERR(gsi->cq)) { |
| 182 | mlx5_ib_warn(dev, "unable to create send CQ for GSI QP. error %ld\n", |
| 183 | PTR_ERR(gsi->cq)); |
| 184 | ret = PTR_ERR(gsi->cq); |
| 185 | goto err_free_wrs; |
| 186 | } |
| 187 | |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 188 | hw_init_attr.qp_type = MLX5_IB_QPT_HW_GSI; |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 189 | hw_init_attr.send_cq = gsi->cq; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 190 | gsi->rx_qp = ib_create_qp(pd, &hw_init_attr); |
| 191 | if (IS_ERR(gsi->rx_qp)) { |
| 192 | mlx5_ib_warn(dev, "unable to create hardware GSI QP. error %ld\n", |
| 193 | PTR_ERR(gsi->rx_qp)); |
| 194 | ret = PTR_ERR(gsi->rx_qp); |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 195 | goto err_destroy_cq; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | dev->devr.ports[init_attr->port_num - 1].gsi = gsi; |
| 199 | |
| 200 | mutex_unlock(&dev->devr.mutex); |
| 201 | |
| 202 | return &gsi->ibqp; |
| 203 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 204 | err_destroy_cq: |
| 205 | ib_free_cq(gsi->cq); |
| 206 | err_free_wrs: |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 207 | mutex_unlock(&dev->devr.mutex); |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 208 | kfree(gsi->outstanding_wrs); |
| 209 | err_free_tx: |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 210 | kfree(gsi->tx_qps); |
| 211 | err_free: |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 212 | kfree(gsi); |
| 213 | return ERR_PTR(ret); |
| 214 | } |
| 215 | |
| 216 | int mlx5_ib_gsi_destroy_qp(struct ib_qp *qp) |
| 217 | { |
| 218 | struct mlx5_ib_dev *dev = to_mdev(qp->device); |
| 219 | struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp); |
| 220 | const int port_num = gsi->port_num; |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 221 | int qp_index; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 222 | int ret; |
| 223 | |
| 224 | mlx5_ib_dbg(dev, "destroying GSI QP\n"); |
| 225 | |
| 226 | mutex_lock(&dev->devr.mutex); |
| 227 | ret = ib_destroy_qp(gsi->rx_qp); |
| 228 | if (ret) { |
| 229 | mlx5_ib_warn(dev, "unable to destroy hardware GSI QP. error %d\n", |
| 230 | ret); |
| 231 | mutex_unlock(&dev->devr.mutex); |
| 232 | return ret; |
| 233 | } |
| 234 | dev->devr.ports[port_num - 1].gsi = NULL; |
| 235 | mutex_unlock(&dev->devr.mutex); |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 236 | gsi->rx_qp = NULL; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 237 | |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 238 | for (qp_index = 0; qp_index < gsi->num_qps; ++qp_index) { |
| 239 | if (!gsi->tx_qps[qp_index]) |
| 240 | continue; |
| 241 | WARN_ON_ONCE(ib_destroy_qp(gsi->tx_qps[qp_index])); |
| 242 | gsi->tx_qps[qp_index] = NULL; |
| 243 | } |
| 244 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 245 | ib_free_cq(gsi->cq); |
| 246 | |
| 247 | kfree(gsi->outstanding_wrs); |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 248 | kfree(gsi->tx_qps); |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 249 | kfree(gsi); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 254 | static struct ib_qp *create_gsi_ud_qp(struct mlx5_ib_gsi_qp *gsi) |
| 255 | { |
| 256 | struct ib_pd *pd = gsi->rx_qp->pd; |
| 257 | struct ib_qp_init_attr init_attr = { |
| 258 | .event_handler = gsi->rx_qp->event_handler, |
| 259 | .qp_context = gsi->rx_qp->qp_context, |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 260 | .send_cq = gsi->cq, |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 261 | .recv_cq = gsi->rx_qp->recv_cq, |
| 262 | .cap = { |
| 263 | .max_send_wr = gsi->cap.max_send_wr, |
| 264 | .max_send_sge = gsi->cap.max_send_sge, |
| 265 | .max_inline_data = gsi->cap.max_inline_data, |
| 266 | }, |
| 267 | .sq_sig_type = gsi->sq_sig_type, |
| 268 | .qp_type = IB_QPT_UD, |
| 269 | .create_flags = mlx5_ib_create_qp_sqpn_qp1(), |
| 270 | }; |
| 271 | |
| 272 | return ib_create_qp(pd, &init_attr); |
| 273 | } |
| 274 | |
| 275 | static int modify_to_rts(struct mlx5_ib_gsi_qp *gsi, struct ib_qp *qp, |
| 276 | u16 qp_index) |
| 277 | { |
| 278 | struct mlx5_ib_dev *dev = to_mdev(qp->device); |
| 279 | struct ib_qp_attr attr; |
| 280 | int mask; |
| 281 | int ret; |
| 282 | |
| 283 | mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY | IB_QP_PORT; |
| 284 | attr.qp_state = IB_QPS_INIT; |
| 285 | attr.pkey_index = qp_index; |
| 286 | attr.qkey = IB_QP1_QKEY; |
| 287 | attr.port_num = gsi->port_num; |
| 288 | ret = ib_modify_qp(qp, &attr, mask); |
| 289 | if (ret) { |
| 290 | mlx5_ib_err(dev, "could not change QP%d state to INIT: %d\n", |
| 291 | qp->qp_num, ret); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | attr.qp_state = IB_QPS_RTR; |
| 296 | ret = ib_modify_qp(qp, &attr, IB_QP_STATE); |
| 297 | if (ret) { |
| 298 | mlx5_ib_err(dev, "could not change QP%d state to RTR: %d\n", |
| 299 | qp->qp_num, ret); |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | attr.qp_state = IB_QPS_RTS; |
| 304 | attr.sq_psn = 0; |
| 305 | ret = ib_modify_qp(qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN); |
| 306 | if (ret) { |
| 307 | mlx5_ib_err(dev, "could not change QP%d state to RTS: %d\n", |
| 308 | qp->qp_num, ret); |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static void setup_qp(struct mlx5_ib_gsi_qp *gsi, u16 qp_index) |
| 316 | { |
| 317 | struct ib_device *device = gsi->rx_qp->device; |
| 318 | struct mlx5_ib_dev *dev = to_mdev(device); |
| 319 | struct ib_qp *qp; |
| 320 | unsigned long flags; |
| 321 | u16 pkey; |
| 322 | int ret; |
| 323 | |
| 324 | ret = ib_query_pkey(device, gsi->port_num, qp_index, &pkey); |
| 325 | if (ret) { |
| 326 | mlx5_ib_warn(dev, "unable to read P_Key at port %d, index %d\n", |
| 327 | gsi->port_num, qp_index); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | if (!pkey) { |
| 332 | mlx5_ib_dbg(dev, "invalid P_Key at port %d, index %d. Skipping.\n", |
| 333 | gsi->port_num, qp_index); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | spin_lock_irqsave(&gsi->lock, flags); |
| 338 | qp = gsi->tx_qps[qp_index]; |
| 339 | spin_unlock_irqrestore(&gsi->lock, flags); |
| 340 | if (qp) { |
| 341 | mlx5_ib_dbg(dev, "already existing GSI TX QP at port %d, index %d. Skipping\n", |
| 342 | gsi->port_num, qp_index); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | qp = create_gsi_ud_qp(gsi); |
| 347 | if (IS_ERR(qp)) { |
| 348 | mlx5_ib_warn(dev, "unable to create hardware UD QP for GSI: %ld\n", |
| 349 | PTR_ERR(qp)); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | ret = modify_to_rts(gsi, qp, qp_index); |
| 354 | if (ret) |
| 355 | goto err_destroy_qp; |
| 356 | |
| 357 | spin_lock_irqsave(&gsi->lock, flags); |
| 358 | WARN_ON_ONCE(gsi->tx_qps[qp_index]); |
| 359 | gsi->tx_qps[qp_index] = qp; |
| 360 | spin_unlock_irqrestore(&gsi->lock, flags); |
| 361 | |
| 362 | return; |
| 363 | |
| 364 | err_destroy_qp: |
| 365 | WARN_ON_ONCE(qp); |
| 366 | } |
| 367 | |
| 368 | static void setup_qps(struct mlx5_ib_gsi_qp *gsi) |
| 369 | { |
| 370 | u16 qp_index; |
| 371 | |
| 372 | for (qp_index = 0; qp_index < gsi->num_qps; ++qp_index) |
| 373 | setup_qp(gsi, qp_index); |
| 374 | } |
| 375 | |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 376 | int mlx5_ib_gsi_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr, |
| 377 | int attr_mask) |
| 378 | { |
| 379 | struct mlx5_ib_dev *dev = to_mdev(qp->device); |
| 380 | struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp); |
| 381 | int ret; |
| 382 | |
| 383 | mlx5_ib_dbg(dev, "modifying GSI QP to state %d\n", attr->qp_state); |
| 384 | |
| 385 | mutex_lock(&gsi->mutex); |
| 386 | ret = ib_modify_qp(gsi->rx_qp, attr, attr_mask); |
Haggai Eran | ebab41c | 2016-02-29 15:45:06 +0200 | [diff] [blame] | 387 | if (ret) { |
| 388 | mlx5_ib_warn(dev, "unable to modify GSI rx QP: %d\n", ret); |
| 389 | goto unlock; |
| 390 | } |
| 391 | |
| 392 | if (to_mqp(gsi->rx_qp)->state == IB_QPS_RTS) |
| 393 | setup_qps(gsi); |
| 394 | |
| 395 | unlock: |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 396 | mutex_unlock(&gsi->mutex); |
| 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | int mlx5_ib_gsi_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr, |
| 402 | int qp_attr_mask, |
| 403 | struct ib_qp_init_attr *qp_init_attr) |
| 404 | { |
| 405 | struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp); |
| 406 | int ret; |
| 407 | |
| 408 | mutex_lock(&gsi->mutex); |
| 409 | ret = ib_query_qp(gsi->rx_qp, qp_attr, qp_attr_mask, qp_init_attr); |
| 410 | qp_init_attr->cap = gsi->cap; |
| 411 | mutex_unlock(&gsi->mutex); |
| 412 | |
| 413 | return ret; |
| 414 | } |
| 415 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 416 | /* Call with gsi->lock locked */ |
| 417 | static int mlx5_ib_add_outstanding_wr(struct mlx5_ib_gsi_qp *gsi, |
| 418 | struct ib_ud_wr *wr, struct ib_wc *wc) |
| 419 | { |
| 420 | struct mlx5_ib_dev *dev = to_mdev(gsi->rx_qp->device); |
| 421 | struct mlx5_ib_gsi_wr *gsi_wr; |
| 422 | |
| 423 | if (gsi->outstanding_pi == gsi->outstanding_ci + gsi->cap.max_send_wr) { |
| 424 | mlx5_ib_warn(dev, "no available GSI work request.\n"); |
| 425 | return -ENOMEM; |
| 426 | } |
| 427 | |
| 428 | gsi_wr = &gsi->outstanding_wrs[gsi->outstanding_pi]; |
| 429 | gsi->outstanding_pi = next_outstanding(gsi, gsi->outstanding_pi); |
| 430 | |
| 431 | if (!wc) { |
| 432 | memset(&gsi_wr->wc, 0, sizeof(gsi_wr->wc)); |
| 433 | gsi_wr->wc.pkey_index = wr->pkey_index; |
| 434 | gsi_wr->wc.wr_id = wr->wr.wr_id; |
| 435 | } else { |
| 436 | gsi_wr->wc = *wc; |
| 437 | gsi_wr->completed = true; |
| 438 | } |
| 439 | |
| 440 | gsi_wr->cqe.done = &handle_single_completion; |
| 441 | wr->wr.wr_cqe = &gsi_wr->cqe; |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Haggai Eran | 83cae2a | 2016-02-29 15:45:10 +0200 | [diff] [blame^] | 446 | /* Call with gsi->lock locked */ |
| 447 | static int mlx5_ib_gsi_silent_drop(struct mlx5_ib_gsi_qp *gsi, |
| 448 | struct ib_ud_wr *wr) |
| 449 | { |
| 450 | struct ib_wc wc = { |
| 451 | { .wr_id = wr->wr.wr_id }, |
| 452 | .status = IB_WC_SUCCESS, |
| 453 | .opcode = IB_WC_SEND, |
| 454 | .qp = &gsi->ibqp, |
| 455 | }; |
| 456 | int ret; |
| 457 | |
| 458 | ret = mlx5_ib_add_outstanding_wr(gsi, wr, &wc); |
| 459 | if (ret) |
| 460 | return ret; |
| 461 | |
| 462 | generate_completions(gsi); |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | /* Call with gsi->lock locked */ |
| 468 | static struct ib_qp *get_tx_qp(struct mlx5_ib_gsi_qp *gsi, struct ib_ud_wr *wr) |
| 469 | { |
| 470 | struct mlx5_ib_dev *dev = to_mdev(gsi->rx_qp->device); |
| 471 | int qp_index = wr->pkey_index; |
| 472 | |
| 473 | if (!mlx5_ib_deth_sqpn_cap(dev)) |
| 474 | return gsi->rx_qp; |
| 475 | |
| 476 | if (qp_index >= gsi->num_qps) |
| 477 | return NULL; |
| 478 | |
| 479 | return gsi->tx_qps[qp_index]; |
| 480 | } |
| 481 | |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 482 | int mlx5_ib_gsi_post_send(struct ib_qp *qp, struct ib_send_wr *wr, |
| 483 | struct ib_send_wr **bad_wr) |
| 484 | { |
| 485 | struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp); |
Haggai Eran | 83cae2a | 2016-02-29 15:45:10 +0200 | [diff] [blame^] | 486 | struct ib_qp *tx_qp; |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 487 | unsigned long flags; |
| 488 | int ret; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 489 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 490 | for (; wr; wr = wr->next) { |
| 491 | struct ib_ud_wr cur_wr = *ud_wr(wr); |
| 492 | |
| 493 | cur_wr.wr.next = NULL; |
| 494 | |
| 495 | spin_lock_irqsave(&gsi->lock, flags); |
Haggai Eran | 83cae2a | 2016-02-29 15:45:10 +0200 | [diff] [blame^] | 496 | tx_qp = get_tx_qp(gsi, &cur_wr); |
| 497 | if (!tx_qp) { |
| 498 | ret = mlx5_ib_gsi_silent_drop(gsi, &cur_wr); |
| 499 | if (ret) |
| 500 | goto err; |
| 501 | spin_unlock_irqrestore(&gsi->lock, flags); |
| 502 | continue; |
| 503 | } |
| 504 | |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 505 | ret = mlx5_ib_add_outstanding_wr(gsi, &cur_wr, NULL); |
| 506 | if (ret) |
| 507 | goto err; |
| 508 | |
Haggai Eran | 83cae2a | 2016-02-29 15:45:10 +0200 | [diff] [blame^] | 509 | ret = ib_post_send(tx_qp, &cur_wr.wr, bad_wr); |
Haggai Eran | ea6dc20 | 2016-02-29 15:45:09 +0200 | [diff] [blame] | 510 | if (ret) { |
| 511 | /* Undo the effect of adding the outstanding wr */ |
| 512 | gsi->outstanding_pi = (gsi->outstanding_pi - 1) % |
| 513 | gsi->cap.max_send_wr; |
| 514 | goto err; |
| 515 | } |
| 516 | spin_unlock_irqrestore(&gsi->lock, flags); |
| 517 | } |
| 518 | |
| 519 | return 0; |
| 520 | |
| 521 | err: |
| 522 | spin_unlock_irqrestore(&gsi->lock, flags); |
| 523 | *bad_wr = wr; |
| 524 | return ret; |
Haggai Eran | d16e91d | 2016-02-29 15:45:05 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | int mlx5_ib_gsi_post_recv(struct ib_qp *qp, struct ib_recv_wr *wr, |
| 528 | struct ib_recv_wr **bad_wr) |
| 529 | { |
| 530 | struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp); |
| 531 | |
| 532 | return ib_post_recv(gsi->rx_qp, wr, bad_wr); |
| 533 | } |
Haggai Eran | 7722f47 | 2016-02-29 15:45:07 +0200 | [diff] [blame] | 534 | |
| 535 | void mlx5_ib_gsi_pkey_change(struct mlx5_ib_gsi_qp *gsi) |
| 536 | { |
| 537 | if (!gsi) |
| 538 | return; |
| 539 | |
| 540 | mutex_lock(&gsi->mutex); |
| 541 | setup_qps(gsi); |
| 542 | mutex_unlock(&gsi->mutex); |
| 543 | } |