18e6e287fSHans Petter Selasky /*-
2b633e08cSHans Petter Selasky * Copyright (c) 2016-2020, Mellanox Technologies, Ltd. All rights reserved.
38e6e287fSHans Petter Selasky *
48e6e287fSHans Petter Selasky * Redistribution and use in source and binary forms, with or without
58e6e287fSHans Petter Selasky * modification, are permitted provided that the following conditions
68e6e287fSHans Petter Selasky * are met:
78e6e287fSHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
88e6e287fSHans Petter Selasky * notice, this list of conditions and the following disclaimer.
98e6e287fSHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
108e6e287fSHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
118e6e287fSHans Petter Selasky * documentation and/or other materials provided with the distribution.
128e6e287fSHans Petter Selasky *
138e6e287fSHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
148e6e287fSHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
158e6e287fSHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
168e6e287fSHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
178e6e287fSHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
188e6e287fSHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
198e6e287fSHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
208e6e287fSHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
218e6e287fSHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
228e6e287fSHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
238e6e287fSHans Petter Selasky * SUCH DAMAGE.
248e6e287fSHans Petter Selasky */
258e6e287fSHans Petter Selasky
2670600979SKonstantin Belousov #include "opt_rss.h"
2770600979SKonstantin Belousov #include "opt_ratelimit.h"
2870600979SKonstantin Belousov
29*028130b8SKonstantin Belousov #include <dev/mlx5/mlx5_ib/mlx5_ib.h>
308e6e287fSHans Petter Selasky
318e6e287fSHans Petter Selasky struct mlx5_ib_gsi_wr {
328e6e287fSHans Petter Selasky struct ib_cqe cqe;
338e6e287fSHans Petter Selasky struct ib_wc wc;
348e6e287fSHans Petter Selasky int send_flags;
358e6e287fSHans Petter Selasky bool completed:1;
368e6e287fSHans Petter Selasky };
378e6e287fSHans Petter Selasky
388e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp {
398e6e287fSHans Petter Selasky struct ib_qp ibqp;
408e6e287fSHans Petter Selasky struct ib_qp *rx_qp;
418e6e287fSHans Petter Selasky u8 port_num;
428e6e287fSHans Petter Selasky struct ib_qp_cap cap;
438e6e287fSHans Petter Selasky enum ib_sig_type sq_sig_type;
448e6e287fSHans Petter Selasky /* Serialize qp state modifications */
458e6e287fSHans Petter Selasky struct mutex mutex;
468e6e287fSHans Petter Selasky struct ib_cq *cq;
478e6e287fSHans Petter Selasky struct mlx5_ib_gsi_wr *outstanding_wrs;
488e6e287fSHans Petter Selasky u32 outstanding_pi, outstanding_ci;
498e6e287fSHans Petter Selasky int num_qps;
508e6e287fSHans Petter Selasky /* Protects access to the tx_qps. Post send operations synchronize
518e6e287fSHans Petter Selasky * with tx_qp creation in setup_qp(). Also protects the
528e6e287fSHans Petter Selasky * outstanding_wrs array and indices.
538e6e287fSHans Petter Selasky */
548e6e287fSHans Petter Selasky spinlock_t lock;
558e6e287fSHans Petter Selasky struct ib_qp **tx_qps;
568e6e287fSHans Petter Selasky };
578e6e287fSHans Petter Selasky
gsi_qp(struct ib_qp * qp)588e6e287fSHans Petter Selasky static struct mlx5_ib_gsi_qp *gsi_qp(struct ib_qp *qp)
598e6e287fSHans Petter Selasky {
608e6e287fSHans Petter Selasky return container_of(qp, struct mlx5_ib_gsi_qp, ibqp);
618e6e287fSHans Petter Selasky }
628e6e287fSHans Petter Selasky
mlx5_ib_deth_sqpn_cap(struct mlx5_ib_dev * dev)638e6e287fSHans Petter Selasky static bool mlx5_ib_deth_sqpn_cap(struct mlx5_ib_dev *dev)
648e6e287fSHans Petter Selasky {
658e6e287fSHans Petter Selasky return MLX5_CAP_GEN(dev->mdev, set_deth_sqpn);
668e6e287fSHans Petter Selasky }
678e6e287fSHans Petter Selasky
688e6e287fSHans Petter Selasky /* Call with gsi->lock locked */
generate_completions(struct mlx5_ib_gsi_qp * gsi)698e6e287fSHans Petter Selasky static void generate_completions(struct mlx5_ib_gsi_qp *gsi)
708e6e287fSHans Petter Selasky {
718e6e287fSHans Petter Selasky struct ib_cq *gsi_cq = gsi->ibqp.send_cq;
728e6e287fSHans Petter Selasky struct mlx5_ib_gsi_wr *wr;
738e6e287fSHans Petter Selasky u32 index;
748e6e287fSHans Petter Selasky
758e6e287fSHans Petter Selasky for (index = gsi->outstanding_ci; index != gsi->outstanding_pi;
768e6e287fSHans Petter Selasky index++) {
778e6e287fSHans Petter Selasky wr = &gsi->outstanding_wrs[index % gsi->cap.max_send_wr];
788e6e287fSHans Petter Selasky
798e6e287fSHans Petter Selasky if (!wr->completed)
808e6e287fSHans Petter Selasky break;
818e6e287fSHans Petter Selasky
828e6e287fSHans Petter Selasky if (gsi->sq_sig_type == IB_SIGNAL_ALL_WR ||
838e6e287fSHans Petter Selasky wr->send_flags & IB_SEND_SIGNALED)
848e6e287fSHans Petter Selasky WARN_ON_ONCE(mlx5_ib_generate_wc(gsi_cq, &wr->wc));
858e6e287fSHans Petter Selasky
868e6e287fSHans Petter Selasky wr->completed = false;
878e6e287fSHans Petter Selasky }
888e6e287fSHans Petter Selasky
898e6e287fSHans Petter Selasky gsi->outstanding_ci = index;
908e6e287fSHans Petter Selasky }
918e6e287fSHans Petter Selasky
handle_single_completion(struct ib_cq * cq,struct ib_wc * wc)928e6e287fSHans Petter Selasky static void handle_single_completion(struct ib_cq *cq, struct ib_wc *wc)
938e6e287fSHans Petter Selasky {
948e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp *gsi = cq->cq_context;
958e6e287fSHans Petter Selasky struct mlx5_ib_gsi_wr *wr =
968e6e287fSHans Petter Selasky container_of(wc->wr_cqe, struct mlx5_ib_gsi_wr, cqe);
978e6e287fSHans Petter Selasky u64 wr_id;
988e6e287fSHans Petter Selasky unsigned long flags;
998e6e287fSHans Petter Selasky
1008e6e287fSHans Petter Selasky spin_lock_irqsave(&gsi->lock, flags);
1018e6e287fSHans Petter Selasky wr->completed = true;
1028e6e287fSHans Petter Selasky wr_id = wr->wc.wr_id;
1038e6e287fSHans Petter Selasky wr->wc = *wc;
1048e6e287fSHans Petter Selasky wr->wc.wr_id = wr_id;
1058e6e287fSHans Petter Selasky wr->wc.qp = &gsi->ibqp;
1068e6e287fSHans Petter Selasky
1078e6e287fSHans Petter Selasky generate_completions(gsi);
1088e6e287fSHans Petter Selasky spin_unlock_irqrestore(&gsi->lock, flags);
1098e6e287fSHans Petter Selasky }
1108e6e287fSHans Petter Selasky
mlx5_ib_gsi_create_qp(struct ib_pd * pd,struct ib_qp_init_attr * init_attr)1118e6e287fSHans Petter Selasky struct ib_qp *mlx5_ib_gsi_create_qp(struct ib_pd *pd,
1128e6e287fSHans Petter Selasky struct ib_qp_init_attr *init_attr)
1138e6e287fSHans Petter Selasky {
1148e6e287fSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(pd->device);
1158e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp *gsi;
1168e6e287fSHans Petter Selasky struct ib_qp_init_attr hw_init_attr = *init_attr;
1178e6e287fSHans Petter Selasky const u8 port_num = init_attr->port_num;
1188e6e287fSHans Petter Selasky const int num_pkeys = pd->device->attrs.max_pkeys;
1198e6e287fSHans Petter Selasky const int num_qps = mlx5_ib_deth_sqpn_cap(dev) ? num_pkeys : 0;
1208e6e287fSHans Petter Selasky int ret;
1218e6e287fSHans Petter Selasky
1228e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "creating GSI QP\n");
1238e6e287fSHans Petter Selasky
1248e6e287fSHans Petter Selasky if (port_num > ARRAY_SIZE(dev->devr.ports) || port_num < 1) {
1258e6e287fSHans Petter Selasky mlx5_ib_warn(dev,
1268e6e287fSHans Petter Selasky "invalid port number %d during GSI QP creation\n",
1278e6e287fSHans Petter Selasky port_num);
1288e6e287fSHans Petter Selasky return ERR_PTR(-EINVAL);
1298e6e287fSHans Petter Selasky }
1308e6e287fSHans Petter Selasky
1318e6e287fSHans Petter Selasky gsi = kzalloc(sizeof(*gsi), GFP_KERNEL);
1328e6e287fSHans Petter Selasky if (!gsi)
1338e6e287fSHans Petter Selasky return ERR_PTR(-ENOMEM);
1348e6e287fSHans Petter Selasky
1358e6e287fSHans Petter Selasky gsi->tx_qps = kcalloc(num_qps, sizeof(*gsi->tx_qps), GFP_KERNEL);
1368e6e287fSHans Petter Selasky if (!gsi->tx_qps) {
1378e6e287fSHans Petter Selasky ret = -ENOMEM;
1388e6e287fSHans Petter Selasky goto err_free;
1398e6e287fSHans Petter Selasky }
1408e6e287fSHans Petter Selasky
1418e6e287fSHans Petter Selasky gsi->outstanding_wrs = kcalloc(init_attr->cap.max_send_wr,
1428e6e287fSHans Petter Selasky sizeof(*gsi->outstanding_wrs),
1438e6e287fSHans Petter Selasky GFP_KERNEL);
1448e6e287fSHans Petter Selasky if (!gsi->outstanding_wrs) {
1458e6e287fSHans Petter Selasky ret = -ENOMEM;
1468e6e287fSHans Petter Selasky goto err_free_tx;
1478e6e287fSHans Petter Selasky }
1488e6e287fSHans Petter Selasky
1498e6e287fSHans Petter Selasky mutex_init(&gsi->mutex);
1508e6e287fSHans Petter Selasky
1518e6e287fSHans Petter Selasky mutex_lock(&dev->devr.mutex);
1528e6e287fSHans Petter Selasky
1538e6e287fSHans Petter Selasky if (dev->devr.ports[port_num - 1].gsi) {
1548e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "GSI QP already exists on port %d\n",
1558e6e287fSHans Petter Selasky port_num);
1568e6e287fSHans Petter Selasky ret = -EBUSY;
1578e6e287fSHans Petter Selasky goto err_free_wrs;
1588e6e287fSHans Petter Selasky }
1598e6e287fSHans Petter Selasky gsi->num_qps = num_qps;
1608e6e287fSHans Petter Selasky spin_lock_init(&gsi->lock);
1618e6e287fSHans Petter Selasky
1628e6e287fSHans Petter Selasky gsi->cap = init_attr->cap;
1638e6e287fSHans Petter Selasky gsi->sq_sig_type = init_attr->sq_sig_type;
1648e6e287fSHans Petter Selasky gsi->ibqp.qp_num = 1;
1658e6e287fSHans Petter Selasky gsi->port_num = port_num;
1668e6e287fSHans Petter Selasky
1678e6e287fSHans Petter Selasky gsi->cq = ib_alloc_cq(pd->device, gsi, init_attr->cap.max_send_wr, 0,
1688e6e287fSHans Petter Selasky IB_POLL_SOFTIRQ);
1698e6e287fSHans Petter Selasky if (IS_ERR(gsi->cq)) {
1708e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "unable to create send CQ for GSI QP. error %ld\n",
1718e6e287fSHans Petter Selasky PTR_ERR(gsi->cq));
1728e6e287fSHans Petter Selasky ret = PTR_ERR(gsi->cq);
1738e6e287fSHans Petter Selasky goto err_free_wrs;
1748e6e287fSHans Petter Selasky }
1758e6e287fSHans Petter Selasky
1768e6e287fSHans Petter Selasky hw_init_attr.qp_type = MLX5_IB_QPT_HW_GSI;
1778e6e287fSHans Petter Selasky hw_init_attr.send_cq = gsi->cq;
1788e6e287fSHans Petter Selasky if (num_qps) {
1798e6e287fSHans Petter Selasky hw_init_attr.cap.max_send_wr = 0;
1808e6e287fSHans Petter Selasky hw_init_attr.cap.max_send_sge = 0;
1818e6e287fSHans Petter Selasky hw_init_attr.cap.max_inline_data = 0;
1828e6e287fSHans Petter Selasky }
1838e6e287fSHans Petter Selasky gsi->rx_qp = ib_create_qp(pd, &hw_init_attr);
1848e6e287fSHans Petter Selasky if (IS_ERR(gsi->rx_qp)) {
1858e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "unable to create hardware GSI QP. error %ld\n",
1868e6e287fSHans Petter Selasky PTR_ERR(gsi->rx_qp));
1878e6e287fSHans Petter Selasky ret = PTR_ERR(gsi->rx_qp);
1888e6e287fSHans Petter Selasky goto err_destroy_cq;
1898e6e287fSHans Petter Selasky }
1908e6e287fSHans Petter Selasky
1918e6e287fSHans Petter Selasky dev->devr.ports[init_attr->port_num - 1].gsi = gsi;
1928e6e287fSHans Petter Selasky
1938e6e287fSHans Petter Selasky mutex_unlock(&dev->devr.mutex);
1948e6e287fSHans Petter Selasky
1958e6e287fSHans Petter Selasky return &gsi->ibqp;
1968e6e287fSHans Petter Selasky
1978e6e287fSHans Petter Selasky err_destroy_cq:
1988e6e287fSHans Petter Selasky ib_free_cq(gsi->cq);
1998e6e287fSHans Petter Selasky err_free_wrs:
2008e6e287fSHans Petter Selasky mutex_unlock(&dev->devr.mutex);
2018e6e287fSHans Petter Selasky kfree(gsi->outstanding_wrs);
2028e6e287fSHans Petter Selasky err_free_tx:
2038e6e287fSHans Petter Selasky kfree(gsi->tx_qps);
2048e6e287fSHans Petter Selasky err_free:
2058e6e287fSHans Petter Selasky kfree(gsi);
2068e6e287fSHans Petter Selasky return ERR_PTR(ret);
2078e6e287fSHans Petter Selasky }
2088e6e287fSHans Petter Selasky
mlx5_ib_gsi_destroy_qp(struct ib_qp * qp)2098e6e287fSHans Petter Selasky int mlx5_ib_gsi_destroy_qp(struct ib_qp *qp)
2108e6e287fSHans Petter Selasky {
2118e6e287fSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(qp->device);
2128e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp);
2138e6e287fSHans Petter Selasky const int port_num = gsi->port_num;
2148e6e287fSHans Petter Selasky int qp_index;
2158e6e287fSHans Petter Selasky int ret;
2168e6e287fSHans Petter Selasky
2178e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "destroying GSI QP\n");
2188e6e287fSHans Petter Selasky
2198e6e287fSHans Petter Selasky mutex_lock(&dev->devr.mutex);
2208e6e287fSHans Petter Selasky ret = ib_destroy_qp(gsi->rx_qp);
2218e6e287fSHans Petter Selasky if (ret) {
2228e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "unable to destroy hardware GSI QP. error %d\n",
2238e6e287fSHans Petter Selasky ret);
2248e6e287fSHans Petter Selasky mutex_unlock(&dev->devr.mutex);
2258e6e287fSHans Petter Selasky return ret;
2268e6e287fSHans Petter Selasky }
2278e6e287fSHans Petter Selasky dev->devr.ports[port_num - 1].gsi = NULL;
2288e6e287fSHans Petter Selasky mutex_unlock(&dev->devr.mutex);
2298e6e287fSHans Petter Selasky gsi->rx_qp = NULL;
2308e6e287fSHans Petter Selasky
2318e6e287fSHans Petter Selasky for (qp_index = 0; qp_index < gsi->num_qps; ++qp_index) {
2328e6e287fSHans Petter Selasky if (!gsi->tx_qps[qp_index])
2338e6e287fSHans Petter Selasky continue;
2348e6e287fSHans Petter Selasky WARN_ON_ONCE(ib_destroy_qp(gsi->tx_qps[qp_index]));
2358e6e287fSHans Petter Selasky gsi->tx_qps[qp_index] = NULL;
2368e6e287fSHans Petter Selasky }
2378e6e287fSHans Petter Selasky
2388e6e287fSHans Petter Selasky ib_free_cq(gsi->cq);
2398e6e287fSHans Petter Selasky
2408e6e287fSHans Petter Selasky kfree(gsi->outstanding_wrs);
2418e6e287fSHans Petter Selasky kfree(gsi->tx_qps);
2428e6e287fSHans Petter Selasky kfree(gsi);
2438e6e287fSHans Petter Selasky
2448e6e287fSHans Petter Selasky return 0;
2458e6e287fSHans Petter Selasky }
2468e6e287fSHans Petter Selasky
create_gsi_ud_qp(struct mlx5_ib_gsi_qp * gsi)2478e6e287fSHans Petter Selasky static struct ib_qp *create_gsi_ud_qp(struct mlx5_ib_gsi_qp *gsi)
2488e6e287fSHans Petter Selasky {
2498e6e287fSHans Petter Selasky struct ib_pd *pd = gsi->rx_qp->pd;
2508e6e287fSHans Petter Selasky struct ib_qp_init_attr init_attr = {
2518e6e287fSHans Petter Selasky .event_handler = gsi->rx_qp->event_handler,
2528e6e287fSHans Petter Selasky .qp_context = gsi->rx_qp->qp_context,
2538e6e287fSHans Petter Selasky .send_cq = gsi->cq,
2548e6e287fSHans Petter Selasky .recv_cq = gsi->rx_qp->recv_cq,
2558e6e287fSHans Petter Selasky .cap = {
2568e6e287fSHans Petter Selasky .max_send_wr = gsi->cap.max_send_wr,
2578e6e287fSHans Petter Selasky .max_send_sge = gsi->cap.max_send_sge,
2588e6e287fSHans Petter Selasky .max_inline_data = gsi->cap.max_inline_data,
2598e6e287fSHans Petter Selasky },
2608e6e287fSHans Petter Selasky .sq_sig_type = gsi->sq_sig_type,
2618e6e287fSHans Petter Selasky .qp_type = IB_QPT_UD,
262f8f5b459SHans Petter Selasky .create_flags = MLX5_IB_QP_CREATE_SQPN_QP1,
2638e6e287fSHans Petter Selasky };
2648e6e287fSHans Petter Selasky
2658e6e287fSHans Petter Selasky return ib_create_qp(pd, &init_attr);
2668e6e287fSHans Petter Selasky }
2678e6e287fSHans Petter Selasky
modify_to_rts(struct mlx5_ib_gsi_qp * gsi,struct ib_qp * qp,u16 qp_index)2688e6e287fSHans Petter Selasky static int modify_to_rts(struct mlx5_ib_gsi_qp *gsi, struct ib_qp *qp,
2698e6e287fSHans Petter Selasky u16 qp_index)
2708e6e287fSHans Petter Selasky {
2718e6e287fSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(qp->device);
2728e6e287fSHans Petter Selasky struct ib_qp_attr attr;
2738e6e287fSHans Petter Selasky int mask;
2748e6e287fSHans Petter Selasky int ret;
2758e6e287fSHans Petter Selasky
2768e6e287fSHans Petter Selasky mask = IB_QP_STATE | IB_QP_PKEY_INDEX | IB_QP_QKEY | IB_QP_PORT;
2778e6e287fSHans Petter Selasky attr.qp_state = IB_QPS_INIT;
2788e6e287fSHans Petter Selasky attr.pkey_index = qp_index;
2798e6e287fSHans Petter Selasky attr.qkey = IB_QP1_QKEY;
2808e6e287fSHans Petter Selasky attr.port_num = gsi->port_num;
2818e6e287fSHans Petter Selasky ret = ib_modify_qp(qp, &attr, mask);
2828e6e287fSHans Petter Selasky if (ret) {
2838e6e287fSHans Petter Selasky mlx5_ib_err(dev, "could not change QP%d state to INIT: %d\n",
2848e6e287fSHans Petter Selasky qp->qp_num, ret);
2858e6e287fSHans Petter Selasky return ret;
2868e6e287fSHans Petter Selasky }
2878e6e287fSHans Petter Selasky
2888e6e287fSHans Petter Selasky attr.qp_state = IB_QPS_RTR;
2898e6e287fSHans Petter Selasky ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
2908e6e287fSHans Petter Selasky if (ret) {
2918e6e287fSHans Petter Selasky mlx5_ib_err(dev, "could not change QP%d state to RTR: %d\n",
2928e6e287fSHans Petter Selasky qp->qp_num, ret);
2938e6e287fSHans Petter Selasky return ret;
2948e6e287fSHans Petter Selasky }
2958e6e287fSHans Petter Selasky
2968e6e287fSHans Petter Selasky attr.qp_state = IB_QPS_RTS;
2978e6e287fSHans Petter Selasky attr.sq_psn = 0;
2988e6e287fSHans Petter Selasky ret = ib_modify_qp(qp, &attr, IB_QP_STATE | IB_QP_SQ_PSN);
2998e6e287fSHans Petter Selasky if (ret) {
3008e6e287fSHans Petter Selasky mlx5_ib_err(dev, "could not change QP%d state to RTS: %d\n",
3018e6e287fSHans Petter Selasky qp->qp_num, ret);
3028e6e287fSHans Petter Selasky return ret;
3038e6e287fSHans Petter Selasky }
3048e6e287fSHans Petter Selasky
3058e6e287fSHans Petter Selasky return 0;
3068e6e287fSHans Petter Selasky }
3078e6e287fSHans Petter Selasky
setup_qp(struct mlx5_ib_gsi_qp * gsi,u16 qp_index)3088e6e287fSHans Petter Selasky static void setup_qp(struct mlx5_ib_gsi_qp *gsi, u16 qp_index)
3098e6e287fSHans Petter Selasky {
3108e6e287fSHans Petter Selasky struct ib_device *device = gsi->rx_qp->device;
3118e6e287fSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(device);
3128e6e287fSHans Petter Selasky struct ib_qp *qp;
3138e6e287fSHans Petter Selasky unsigned long flags;
3148e6e287fSHans Petter Selasky u16 pkey;
3158e6e287fSHans Petter Selasky int ret;
3168e6e287fSHans Petter Selasky
3178e6e287fSHans Petter Selasky ret = ib_query_pkey(device, gsi->port_num, qp_index, &pkey);
3188e6e287fSHans Petter Selasky if (ret) {
3198e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "unable to read P_Key at port %d, index %d\n",
3208e6e287fSHans Petter Selasky gsi->port_num, qp_index);
3218e6e287fSHans Petter Selasky return;
3228e6e287fSHans Petter Selasky }
3238e6e287fSHans Petter Selasky
3248e6e287fSHans Petter Selasky if (!pkey) {
3258e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "invalid P_Key at port %d, index %d. Skipping.\n",
3268e6e287fSHans Petter Selasky gsi->port_num, qp_index);
3278e6e287fSHans Petter Selasky return;
3288e6e287fSHans Petter Selasky }
3298e6e287fSHans Petter Selasky
3308e6e287fSHans Petter Selasky spin_lock_irqsave(&gsi->lock, flags);
3318e6e287fSHans Petter Selasky qp = gsi->tx_qps[qp_index];
3328e6e287fSHans Petter Selasky spin_unlock_irqrestore(&gsi->lock, flags);
3338e6e287fSHans Petter Selasky if (qp) {
3348e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "already existing GSI TX QP at port %d, index %d. Skipping\n",
3358e6e287fSHans Petter Selasky gsi->port_num, qp_index);
3368e6e287fSHans Petter Selasky return;
3378e6e287fSHans Petter Selasky }
3388e6e287fSHans Petter Selasky
3398e6e287fSHans Petter Selasky qp = create_gsi_ud_qp(gsi);
3408e6e287fSHans Petter Selasky if (IS_ERR(qp)) {
3418e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "unable to create hardware UD QP for GSI: %ld\n",
3428e6e287fSHans Petter Selasky PTR_ERR(qp));
3438e6e287fSHans Petter Selasky return;
3448e6e287fSHans Petter Selasky }
3458e6e287fSHans Petter Selasky
3468e6e287fSHans Petter Selasky ret = modify_to_rts(gsi, qp, qp_index);
3478e6e287fSHans Petter Selasky if (ret)
3488e6e287fSHans Petter Selasky goto err_destroy_qp;
3498e6e287fSHans Petter Selasky
3508e6e287fSHans Petter Selasky spin_lock_irqsave(&gsi->lock, flags);
3518e6e287fSHans Petter Selasky WARN_ON_ONCE(gsi->tx_qps[qp_index]);
3528e6e287fSHans Petter Selasky gsi->tx_qps[qp_index] = qp;
3538e6e287fSHans Petter Selasky spin_unlock_irqrestore(&gsi->lock, flags);
3548e6e287fSHans Petter Selasky
3558e6e287fSHans Petter Selasky return;
3568e6e287fSHans Petter Selasky
3578e6e287fSHans Petter Selasky err_destroy_qp:
3588e6e287fSHans Petter Selasky WARN_ON_ONCE(qp);
3598e6e287fSHans Petter Selasky }
3608e6e287fSHans Petter Selasky
setup_qps(struct mlx5_ib_gsi_qp * gsi)3618e6e287fSHans Petter Selasky static void setup_qps(struct mlx5_ib_gsi_qp *gsi)
3628e6e287fSHans Petter Selasky {
3638e6e287fSHans Petter Selasky u16 qp_index;
3648e6e287fSHans Petter Selasky
3658e6e287fSHans Petter Selasky for (qp_index = 0; qp_index < gsi->num_qps; ++qp_index)
3668e6e287fSHans Petter Selasky setup_qp(gsi, qp_index);
3678e6e287fSHans Petter Selasky }
3688e6e287fSHans Petter Selasky
mlx5_ib_gsi_modify_qp(struct ib_qp * qp,struct ib_qp_attr * attr,int attr_mask)3698e6e287fSHans Petter Selasky int mlx5_ib_gsi_modify_qp(struct ib_qp *qp, struct ib_qp_attr *attr,
3708e6e287fSHans Petter Selasky int attr_mask)
3718e6e287fSHans Petter Selasky {
3728e6e287fSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(qp->device);
3738e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp);
3748e6e287fSHans Petter Selasky int ret;
3758e6e287fSHans Petter Selasky
3768e6e287fSHans Petter Selasky mlx5_ib_dbg(dev, "modifying GSI QP to state %d\n", attr->qp_state);
3778e6e287fSHans Petter Selasky
3788e6e287fSHans Petter Selasky mutex_lock(&gsi->mutex);
3798e6e287fSHans Petter Selasky ret = ib_modify_qp(gsi->rx_qp, attr, attr_mask);
3808e6e287fSHans Petter Selasky if (ret) {
3818e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "unable to modify GSI rx QP: %d\n", ret);
3828e6e287fSHans Petter Selasky goto unlock;
3838e6e287fSHans Petter Selasky }
3848e6e287fSHans Petter Selasky
3858e6e287fSHans Petter Selasky if (to_mqp(gsi->rx_qp)->state == IB_QPS_RTS)
3868e6e287fSHans Petter Selasky setup_qps(gsi);
3878e6e287fSHans Petter Selasky
3888e6e287fSHans Petter Selasky unlock:
3898e6e287fSHans Petter Selasky mutex_unlock(&gsi->mutex);
3908e6e287fSHans Petter Selasky
3918e6e287fSHans Petter Selasky return ret;
3928e6e287fSHans Petter Selasky }
3938e6e287fSHans Petter Selasky
mlx5_ib_gsi_query_qp(struct ib_qp * qp,struct ib_qp_attr * qp_attr,int qp_attr_mask,struct ib_qp_init_attr * qp_init_attr)3948e6e287fSHans Petter Selasky int mlx5_ib_gsi_query_qp(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
3958e6e287fSHans Petter Selasky int qp_attr_mask,
3968e6e287fSHans Petter Selasky struct ib_qp_init_attr *qp_init_attr)
3978e6e287fSHans Petter Selasky {
3988e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp);
3998e6e287fSHans Petter Selasky int ret;
4008e6e287fSHans Petter Selasky
4018e6e287fSHans Petter Selasky mutex_lock(&gsi->mutex);
4028e6e287fSHans Petter Selasky ret = ib_query_qp(gsi->rx_qp, qp_attr, qp_attr_mask, qp_init_attr);
4038e6e287fSHans Petter Selasky qp_init_attr->cap = gsi->cap;
4048e6e287fSHans Petter Selasky mutex_unlock(&gsi->mutex);
4058e6e287fSHans Petter Selasky
4068e6e287fSHans Petter Selasky return ret;
4078e6e287fSHans Petter Selasky }
4088e6e287fSHans Petter Selasky
4098e6e287fSHans Petter Selasky /* Call with gsi->lock locked */
mlx5_ib_add_outstanding_wr(struct mlx5_ib_gsi_qp * gsi,struct ib_ud_wr * wr,struct ib_wc * wc)4108e6e287fSHans Petter Selasky static int mlx5_ib_add_outstanding_wr(struct mlx5_ib_gsi_qp *gsi,
4118e6e287fSHans Petter Selasky struct ib_ud_wr *wr, struct ib_wc *wc)
4128e6e287fSHans Petter Selasky {
4138e6e287fSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(gsi->rx_qp->device);
4148e6e287fSHans Petter Selasky struct mlx5_ib_gsi_wr *gsi_wr;
4158e6e287fSHans Petter Selasky
4168e6e287fSHans Petter Selasky if (gsi->outstanding_pi == gsi->outstanding_ci + gsi->cap.max_send_wr) {
4178e6e287fSHans Petter Selasky mlx5_ib_warn(dev, "no available GSI work request.\n");
4188e6e287fSHans Petter Selasky return -ENOMEM;
4198e6e287fSHans Petter Selasky }
4208e6e287fSHans Petter Selasky
4218e6e287fSHans Petter Selasky gsi_wr = &gsi->outstanding_wrs[gsi->outstanding_pi %
4228e6e287fSHans Petter Selasky gsi->cap.max_send_wr];
4238e6e287fSHans Petter Selasky gsi->outstanding_pi++;
4248e6e287fSHans Petter Selasky
4258e6e287fSHans Petter Selasky if (!wc) {
4268e6e287fSHans Petter Selasky memset(&gsi_wr->wc, 0, sizeof(gsi_wr->wc));
4278e6e287fSHans Petter Selasky gsi_wr->wc.pkey_index = wr->pkey_index;
4288e6e287fSHans Petter Selasky gsi_wr->wc.wr_id = wr->wr.wr_id;
4298e6e287fSHans Petter Selasky } else {
4308e6e287fSHans Petter Selasky gsi_wr->wc = *wc;
4318e6e287fSHans Petter Selasky gsi_wr->completed = true;
4328e6e287fSHans Petter Selasky }
4338e6e287fSHans Petter Selasky
4348e6e287fSHans Petter Selasky gsi_wr->cqe.done = &handle_single_completion;
4358e6e287fSHans Petter Selasky wr->wr.wr_cqe = &gsi_wr->cqe;
4368e6e287fSHans Petter Selasky
4378e6e287fSHans Petter Selasky return 0;
4388e6e287fSHans Petter Selasky }
4398e6e287fSHans Petter Selasky
4408e6e287fSHans Petter Selasky /* Call with gsi->lock locked */
mlx5_ib_gsi_silent_drop(struct mlx5_ib_gsi_qp * gsi,struct ib_ud_wr * wr)4418e6e287fSHans Petter Selasky static int mlx5_ib_gsi_silent_drop(struct mlx5_ib_gsi_qp *gsi,
4428e6e287fSHans Petter Selasky struct ib_ud_wr *wr)
4438e6e287fSHans Petter Selasky {
4448e6e287fSHans Petter Selasky struct ib_wc wc = {
4458e6e287fSHans Petter Selasky { .wr_id = wr->wr.wr_id },
4468e6e287fSHans Petter Selasky .status = IB_WC_SUCCESS,
4478e6e287fSHans Petter Selasky .opcode = IB_WC_SEND,
4488e6e287fSHans Petter Selasky .qp = &gsi->ibqp,
4498e6e287fSHans Petter Selasky };
4508e6e287fSHans Petter Selasky int ret;
4518e6e287fSHans Petter Selasky
4528e6e287fSHans Petter Selasky ret = mlx5_ib_add_outstanding_wr(gsi, wr, &wc);
4538e6e287fSHans Petter Selasky if (ret)
4548e6e287fSHans Petter Selasky return ret;
4558e6e287fSHans Petter Selasky
4568e6e287fSHans Petter Selasky generate_completions(gsi);
4578e6e287fSHans Petter Selasky
4588e6e287fSHans Petter Selasky return 0;
4598e6e287fSHans Petter Selasky }
4608e6e287fSHans Petter Selasky
4618e6e287fSHans Petter Selasky /* Call with gsi->lock locked */
get_tx_qp(struct mlx5_ib_gsi_qp * gsi,struct ib_ud_wr * wr)4628e6e287fSHans Petter Selasky static struct ib_qp *get_tx_qp(struct mlx5_ib_gsi_qp *gsi, struct ib_ud_wr *wr)
4638e6e287fSHans Petter Selasky {
4648e6e287fSHans Petter Selasky struct mlx5_ib_dev *dev = to_mdev(gsi->rx_qp->device);
4658e6e287fSHans Petter Selasky int qp_index = wr->pkey_index;
4668e6e287fSHans Petter Selasky
4678e6e287fSHans Petter Selasky if (!mlx5_ib_deth_sqpn_cap(dev))
4688e6e287fSHans Petter Selasky return gsi->rx_qp;
4698e6e287fSHans Petter Selasky
4708e6e287fSHans Petter Selasky if (qp_index >= gsi->num_qps)
4718e6e287fSHans Petter Selasky return NULL;
4728e6e287fSHans Petter Selasky
4738e6e287fSHans Petter Selasky return gsi->tx_qps[qp_index];
4748e6e287fSHans Petter Selasky }
4758e6e287fSHans Petter Selasky
mlx5_ib_gsi_post_send(struct ib_qp * qp,const struct ib_send_wr * wr,const struct ib_send_wr ** bad_wr)476c3987b8eSHans Petter Selasky int mlx5_ib_gsi_post_send(struct ib_qp *qp, const struct ib_send_wr *wr,
477c3987b8eSHans Petter Selasky const struct ib_send_wr **bad_wr)
4788e6e287fSHans Petter Selasky {
4798e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp);
4808e6e287fSHans Petter Selasky struct ib_qp *tx_qp;
4818e6e287fSHans Petter Selasky unsigned long flags;
4828e6e287fSHans Petter Selasky int ret;
4838e6e287fSHans Petter Selasky
4848e6e287fSHans Petter Selasky for (; wr; wr = wr->next) {
4858e6e287fSHans Petter Selasky struct ib_ud_wr cur_wr = *ud_wr(wr);
4868e6e287fSHans Petter Selasky
4878e6e287fSHans Petter Selasky cur_wr.wr.next = NULL;
4888e6e287fSHans Petter Selasky
4898e6e287fSHans Petter Selasky spin_lock_irqsave(&gsi->lock, flags);
4908e6e287fSHans Petter Selasky tx_qp = get_tx_qp(gsi, &cur_wr);
4918e6e287fSHans Petter Selasky if (!tx_qp) {
4928e6e287fSHans Petter Selasky ret = mlx5_ib_gsi_silent_drop(gsi, &cur_wr);
4938e6e287fSHans Petter Selasky if (ret)
4948e6e287fSHans Petter Selasky goto err;
4958e6e287fSHans Petter Selasky spin_unlock_irqrestore(&gsi->lock, flags);
4968e6e287fSHans Petter Selasky continue;
4978e6e287fSHans Petter Selasky }
4988e6e287fSHans Petter Selasky
4998e6e287fSHans Petter Selasky ret = mlx5_ib_add_outstanding_wr(gsi, &cur_wr, NULL);
5008e6e287fSHans Petter Selasky if (ret)
5018e6e287fSHans Petter Selasky goto err;
5028e6e287fSHans Petter Selasky
5038e6e287fSHans Petter Selasky ret = ib_post_send(tx_qp, &cur_wr.wr, bad_wr);
5048e6e287fSHans Petter Selasky if (ret) {
5058e6e287fSHans Petter Selasky /* Undo the effect of adding the outstanding wr */
5068e6e287fSHans Petter Selasky gsi->outstanding_pi = (gsi->outstanding_pi - 1) %
5078e6e287fSHans Petter Selasky gsi->cap.max_send_wr;
5088e6e287fSHans Petter Selasky goto err;
5098e6e287fSHans Petter Selasky }
5108e6e287fSHans Petter Selasky spin_unlock_irqrestore(&gsi->lock, flags);
5118e6e287fSHans Petter Selasky }
5128e6e287fSHans Petter Selasky
5138e6e287fSHans Petter Selasky return 0;
5148e6e287fSHans Petter Selasky
5158e6e287fSHans Petter Selasky err:
5168e6e287fSHans Petter Selasky spin_unlock_irqrestore(&gsi->lock, flags);
5178e6e287fSHans Petter Selasky *bad_wr = wr;
5188e6e287fSHans Petter Selasky return ret;
5198e6e287fSHans Petter Selasky }
5208e6e287fSHans Petter Selasky
mlx5_ib_gsi_post_recv(struct ib_qp * qp,const struct ib_recv_wr * wr,const struct ib_recv_wr ** bad_wr)521c3987b8eSHans Petter Selasky int mlx5_ib_gsi_post_recv(struct ib_qp *qp, const struct ib_recv_wr *wr,
522c3987b8eSHans Petter Selasky const struct ib_recv_wr **bad_wr)
5238e6e287fSHans Petter Selasky {
5248e6e287fSHans Petter Selasky struct mlx5_ib_gsi_qp *gsi = gsi_qp(qp);
5258e6e287fSHans Petter Selasky
5268e6e287fSHans Petter Selasky return ib_post_recv(gsi->rx_qp, wr, bad_wr);
5278e6e287fSHans Petter Selasky }
5288e6e287fSHans Petter Selasky
mlx5_ib_gsi_pkey_change(struct mlx5_ib_gsi_qp * gsi)5298e6e287fSHans Petter Selasky void mlx5_ib_gsi_pkey_change(struct mlx5_ib_gsi_qp *gsi)
5308e6e287fSHans Petter Selasky {
5318e6e287fSHans Petter Selasky if (!gsi)
5328e6e287fSHans Petter Selasky return;
5338e6e287fSHans Petter Selasky
5348e6e287fSHans Petter Selasky mutex_lock(&gsi->mutex);
5358e6e287fSHans Petter Selasky setup_qps(gsi);
5368e6e287fSHans Petter Selasky mutex_unlock(&gsi->mutex);
5378e6e287fSHans Petter Selasky }
538