From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.227 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796400117; b=TCDv2W5cN723S6uxumM9UZtXlTD097Taikh9RPfHfYuDl82Db8nC6DwsOGeRqcl/izrr gsbW6JIOTByCIXxiQFa9xBw3MvvP0TO3eWDaMfnLoulvpxwGLcdluIGOALbiSAewJsrFW XSxWefa8YX8VyBML7k2OmVzhgnMEDmWCX4P3oMS7H8YVfw7u9/EK7dV5W3XkzOm0aWMI0 0oZwzqUlhTCFSWWZpaqPJhox8tSgnI7MzjWjUOSCjtHarYh5SgocVq6KD56cV56qex53R SEx3XMHf8jo7fOVQ0iVNUuJX7usCuBJLAIVH/xM3uchwofqqMBR57qr7Y/4G2nPezBg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796400117; h=DMARC-Filter:Received:X-Google-DKIM-Signature:X-Gm-Message-State: X-Gm-Gg:X-Received:Received:X-Relaying-Domain:X-CFilter-Loop:Received: DKIM-Signature:X-Received:X-Received:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-DetectorID-Processed; bh=ZPdDgMXDoYBYLtj9HlOYLFt9tgA6+OXInWjS1OIL/ic=; b=k2cLXh5WYJPUp0Hc6yhgVV+KqigNV3CShsQ5QkoOIhwfEV79Q5GD6A9ykJCxc6U+avsZ Sj2bgBhEt11NaFo6vZ0A3aWqqL5aoL6JhprNpw/KrqxMDRGDDKpN5/QSXv8l7cha/VpKP 7joe8YHkNF/jPVPyXLnpwsxdVc09aIn6C4B6RkX4wjGSLn0/MO5V+M/WYGzTD+ezS1mQh zCHDa4KSRK5UKqlZ7ymERj/dmynpIDr5BVd5Aj12I47Z/h+p1bqWhIjerA/CVa06WpjqV Na6yM7mZD8APLIah1VA+wLikO+XIBh7ZIKR7PjTV36zqq7QShlwnpQEsFVWCJ0q+5ug== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=broadcom.com; spf=fail smtp.mailfrom=broadcom.com; dkim=pass header.d=broadcom.com header.i=@broadcom.com header.a=rsa-sha256 header.s=google header.b=ZB46gPQn; arc=none smtp.remote-ip=209.85.215.227 Received: from mail-pg1-f227.google.com (mail-pg1-f227.google.com [209.85.215.227]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB311606A322 for ; Fri, 4 Dec 2026 16:01:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB311606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=broadcom.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=broadcom.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=broadcom.com header.i=@broadcom.com header.a=rsa-sha256 header.s=google header.b=ZB46gPQn Received: by mail-pg1-f227.google.com with SMTP id 41be03b00d2f7-cde84cb8803so479051a12.1 for ; Fri, 04 Dec 2026 08:01:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796400115; x=1797004915; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ZPdDgMXDoYBYLtj9HlOYLFt9tgA6+OXInWjS1OIL/ic=; b=dBcT6aHqjGMG0DDErzZ+++3tMErEZG82RPeoX8PR3XcN9jurEM4a00aAsPEmqVbG4U 9oztLyJQb9gdV7XAwadYHuNniK3sWc77ZMeMhJZIRliXbaAPzEQMuEGzc8OEv7liWxba 2iUJ8DGXu8vbE1FZa0EC0SpjEUE2/Ce+wv9x62gri7EJb/+RvA0Uk06tUnpIaRw3EAbW a1uABGoV5Va0xbUZjz5cqXfiRWyHiE9ZKpjdB0sdqv4AadtwbdPvn5XOI/tCsuJrvCfW 9cg4tUohriqbU1Q8PfKWPGmjIf/KemwruHyjd3aQk13oa0aEIMrpqe/J4Ga28Lt+0u6u NQjg== X-Gm-Message-State: AB4q+uHuunIrxdVQ6HqNS+a3btlLL9A9F9XLt9kRUjM7twV4N5op9G1J sebWVwOup/tTLZdye5DjTEP4aIV5HogqAZXXbD7nRpVN5+07lJZDiQxq21vbVnsz76rz4aeNvkV i8v5g/Kab3ggIfnANQkvUwbhod8/ElypeuHn0aXecE5HATkBf3Hqyquz/R0Q07zr2D22LnPnVVu bkf12P3R/27UU4YYM5kLhyJIvh9K2JyX/kBKkTHREyHXvs9/GiwbDWyd7I8ILMbZoCeE6GxzA0u BzPWemJsA== X-Gm-Gg: AZFdTZe0OLLXBk/AUCAmDKzA8dbkz3Ovtx1IUdy6CroWQLVhS1+i4vUF9oK2unjqaeA Oe6g40Ffgp0ON0/QnKY7C+Qe0gA0MpkMYBKWSS0EAC4ILba3jSQMm/OrK85k1eZ8VK/2eqRvMV6 Q5gbhgI7nU5e+96G9ozqYym2CAIc72blZ9L8HiQ7AckljIBo0cODp43bkBHvz8DNQEJgXKoZw6H YmIYVnXtRR2TtoJ5sJ4SSFobggEh2Ip8XzssgfnanNXEIiClfzgoMHdykdDlsEhWzpyOBi0z/gh dg7gjlFqaDBxlLyo4RX6mFAxzkZRvFMrfVXNe7rPrUMvLWskUfA9jyu/hYfpWKZjMGc8HmqoTPs KKyF0e6DAAGe5zpyJgwIMEIQb0HUbPTykC1ABNVnVba8vdrdEhpOFHPG43qEvLTLo6odPh5vJta udYSP6D9cqQUbGOLkWTIn0Ecccyl+XGqNCOaai X-Received: by 2002:a17:90b:3d90:b0:3b4:1926:9e9c with SMTP id 98e67ed59e1d1-3b5e06370damr1926490a91.30.1796400114622; Fri, 04 Dec 2026 08:01:54 -0800 (PST) Received: from smtp-us-east1-p01-i01-si01.dlp.protect.broadcom.com (address-144-49-247-123.dlp.protect.broadcom.com. [144.49.247.123]) by smtp-relay.gmail.com with ESMTPS id 98e67ed59e1d1-3b5c7bb9b71sm4092918a91.5.2026.12.04.08.01.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 04 Dec 2026 08:01:54 -0800 (PST) X-Relaying-Domain: broadcom.com X-CFilter-Loop: Reflected Received: by mail-yw1-f199.google.com with SMTP id 00721157ae682-8b3030bd581so15410217b3.3 for ; Fri, 04 Dec 2026 08:01:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1796400113; x=1797004913; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ZPdDgMXDoYBYLtj9HlOYLFt9tgA6+OXInWjS1OIL/ic=; b=ZB46gPQnoNrhjTmqHHQgJk5oQ2NadqUaeShoiEaOe5cTfYZg3Tsx5iFImABbAIQC69 HSls8/6bf5mCFazdDaLPJSSWDPVsKHwuwGKnzdt3/jW6s+N329a9vTp2U0LhSuH8JQgP 8EKVNbJ4ZI459y5DSQJp2cDbWWxCDbPTsQUH8= X-Received: by 2002:a05:690c:79c:b0:8b4:d9b0:d732 with SMTP id 00721157ae682-8b660651cabmr10071487b3.29.1796400112510; Fri, 04 Dec 2026 08:01:52 -0800 (PST) X-Received: by 2002:a05:690c:79c:b0:8b4:d9b0:d732 with SMTP id 00721157ae682-8b660651cabmr10070847b3.29.1796400111505; Fri, 04 Dec 2026 08:01:51 -0800 (PST) Received: from mgmt5.dhcp.broadcom.net ([192.19.222.250]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-921e05633dfsm25686976d6.11.2026.12.04.08.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 08:01:50 -0800 (PST) From: Siva Reddy Kallam To: leonro@nvidia.com, jgg@nvidia.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, andrew+netdev@lunn.ch, horms@kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, vikas.gupta@broadcom.com, ajit.khaparde@broadcom.com, dharmender.garg@broadcom.com, sachin.holla@broadcom.com, Siva Reddy Kallam Subject: [RDMA v2 08/13] RDMA/bng_re: Add CQ verbs Date: Fri, 4 Dec 2026 07:48:00 -0800 Message-ID: <20261204155249.1002019-9-siva.kallam@broadcom.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20261204155249.1002019-1-siva.kallam@broadcom.com> References: <20261204155249.1002019-1-siva.kallam@broadcom.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-DetectorID-Processed: b00c1d49-9d2e-4205-b15f-d015386d3d5e This patch adds below verbs. - bng_re_create_cq - bng_re_create_user_cq - bng_re_resize_cq - bng_re_req_notify_cq - bng_re_destroy_cq Signed-off-by: Siva Reddy Kallam --- drivers/infiniband/hw/bng_re/Makefile | 1 + drivers/infiniband/hw/bng_re/bng_dev.c | 33 ++ drivers/infiniband/hw/bng_re/bng_fp.c | 310 +++++++++++++++ drivers/infiniband/hw/bng_re/bng_fp.h | 14 + drivers/infiniband/hw/bng_re/bng_fw.h | 19 + drivers/infiniband/hw/bng_re/bng_re.h | 13 +- drivers/infiniband/hw/bng_re/bng_res.h | 99 ++++- drivers/infiniband/hw/bng_re/bng_sp.c | 209 +++++++++- drivers/infiniband/hw/bng_re/bng_sp.h | 26 ++ drivers/infiniband/hw/bng_re/bng_verbs.c | 480 +++++++++++++++++++++++ drivers/infiniband/hw/bng_re/bng_verbs.h | 32 ++ include/uapi/rdma/bng_re-abi.h | 18 + 12 files changed, 1232 insertions(+), 22 deletions(-) create mode 100644 drivers/infiniband/hw/bng_re/bng_fp.c create mode 100644 drivers/infiniband/hw/bng_re/bng_fp.h diff --git a/drivers/infiniband/hw/bng_re/Makefile b/drivers/infiniband/hw/bng_re/Makefile index 1b954a35993b..5e650a35b185 100644 --- a/drivers/infiniband/hw/bng_re/Makefile +++ b/drivers/infiniband/hw/bng_re/Makefile @@ -13,4 +13,5 @@ bng_re-y := \ bng_fw.o \ bng_dev.o \ bng_sp.o \ + bng_fp.o \ bng_verbs.o diff --git a/drivers/infiniband/hw/bng_re/bng_dev.c b/drivers/infiniband/hw/bng_re/bng_dev.c index a301fc8e590e..43bd58d88430 100644 --- a/drivers/infiniband/hw/bng_re/bng_dev.c +++ b/drivers/infiniband/hw/bng_re/bng_dev.c @@ -19,6 +19,7 @@ #include "bng_re_mpc_roce.h" #include "bng_xid.h" #include "bng_verbs.h" +#include "bng_fp.h" MODULE_AUTHOR("Siva Reddy Kallam "); MODULE_DESCRIPTION(BNG_RE_DESC); @@ -50,6 +51,14 @@ static const struct ib_device_ops bng_re_dev_ops = { .reg_user_mr = bng_re_reg_user_mr, .reg_user_mr_dmabuf = bng_re_reg_user_mr_dmabuf, .dereg_mr = bng_re_dereg_mr, + .create_cq = bng_re_create_cq, + .create_user_cq = bng_re_create_user_cq, + .resize_user_cq = bng_re_resize_cq, + .req_notify_cq = bng_re_req_notify_cq, + .pre_destroy_cq = bng_re_pre_destroy_cq, + .destroy_cq = bng_re_destroy_cq, + .poll_cq = bng_re_poll_cq, + INIT_RDMA_OBJ_SIZE(ib_cq, bng_re_cq, ib_cq), INIT_RDMA_OBJ_SIZE(ib_pd, bng_re_pd, ib_pd), INIT_RDMA_OBJ_SIZE(ib_ucontext, bng_re_ucontext, ib_uctx), }; @@ -357,10 +366,15 @@ static void bng_re_dev_uninit(struct bng_re_dev *rdev) /* Remove debugfs first: its handlers dereference MPC/XID state freed below */ bng_re_debugfs_rem_pdev(rdev); + if (rdev->bng_res.dpi_tbl.max) + bng_re_dealloc_dpi(&rdev->bng_res, + &rdev->dpi_privileged); + bng_deinit_mpc(rdev); bng_re_free_xid_tables(&rdev->bng_res); bng_sp_cleanup_sgid_tbl(&rdev->bng_res.sgid_tbl); + bng_fp_free_nq_res(rdev); if (test_and_clear_bit(BNG_RE_FLAG_TBLS_ALLOC_INITED, &rdev->flags)) bng_res_free_tbls(&rdev->bng_res); @@ -512,6 +526,19 @@ static int bng_re_dev_init(struct bng_re_dev *rdev) } set_bit(BNG_RE_FLAG_TBLS_ALLOC_INITED, &rdev->flags); + /* Allocate FP NQ resources */ + rc = bng_fp_alloc_nq_res(rdev); + if (rc) { + ibdev_err(&rdev->ibdev, "Failed to allocate NQ resources"); + goto fp_nq_alloc_fail; + } + + rc = bng_re_alloc_dpi(&rdev->bng_res, + &rdev->dpi_privileged, + rdev, BNG_RE_DPI_TYPE_KERNEL); + if (rc) + goto alloc_dpi_fail; + rc = bng_alloc_init_mpc(rdev); if (rc) { ibdev_err(&rdev->ibdev, @@ -521,9 +548,15 @@ static int bng_re_dev_init(struct bng_re_dev *rdev) bng_re_debugfs_add_mpc(rdev); bng_re_debugfs_add_mpctune(rdev); return 0; + deinit_mpc: + bng_re_dealloc_dpi(&rdev->bng_res, + &rdev->dpi_privileged); bng_re_debugfs_rem_pdev(rdev); bng_deinit_mpc(rdev); +alloc_dpi_fail: + bng_fp_free_nq_res(rdev); +fp_nq_alloc_fail: if (test_and_clear_bit(BNG_RE_FLAG_TBLS_ALLOC_INITED, &rdev->flags)) bng_res_free_tbls(&rdev->bng_res); deinit_fw: diff --git a/drivers/infiniband/hw/bng_re/bng_fp.c b/drivers/infiniband/hw/bng_re/bng_fp.c new file mode 100644 index 000000000000..73b3595940dc --- /dev/null +++ b/drivers/infiniband/hw/bng_re/bng_fp.c @@ -0,0 +1,310 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2026 Broadcom. +// +#include +#include +#include +#include + +#include "bng_res.h" +#include "bng_sp.h" +#include "bng_fp.h" +#include "bng_re.h" +#include "bng_verbs.h" + +void bng_fp_req_notify_cq(struct bng_sp_cq *cq, u32 arm_type) +{ + cq->dbinfo.toggle = cq->toggle; + + if (arm_type) + bng_re_ring_db(&cq->dbinfo, arm_type); +} + +/* + * Called with nq->hwq.lock held. The NQE handle indexes nq->cq_xa, so an + * NQE for an untracked CQ is dropped. + */ +static void bng_fp_process_cqn(struct bng_re_nq *nq, struct nq_cn *nqcne) +{ + struct bng_sp_cq *sp_cq; + struct bng_re_cq *cq; + unsigned long flags; + u64 q_handle; + + q_handle = le32_to_cpu(nqcne->cq_handle_low); + q_handle |= (u64)le32_to_cpu(nqcne->cq_handle_high) << 32; + sp_cq = xa_load(&nq->cq_xa, q_handle); + if (!sp_cq) + return; + cq = container_of(sp_cq, struct bng_re_cq, sp_cq); + + /* + * HW disables arming after it posts a CQ notification. Re-enable it + * with the new toggle, which the next ARM doorbell must carry. Hold + * cq_lock, which serializes dbinfo with req_notify_cq() and with a + * resize switching the CQ ring; drop it before comp_handler(), which + * may re-arm the CQ. + */ + spin_lock_irqsave(&cq->cq_lock, flags); + sp_cq->toggle = (le16_to_cpu(nqcne->type) & NQ_CN_TOGGLE_MASK) >> + NQ_CN_TOGGLE_SFT; + sp_cq->dbinfo.toggle = sp_cq->toggle; + bng_re_armen_db(&sp_cq->dbinfo, DBC_DBC_TYPE_CQ_ARMENA); + spin_unlock_irqrestore(&cq->cq_lock, flags); + + if (!cq->ib_cq.comp_handler) + return; + + /* Call the ULP unlocked; nq_users makes untrack wait for it */ + atomic_inc(&sp_cq->nq_users); + spin_unlock_bh(&nq->hwq.lock); + cq->ib_cq.comp_handler(&cq->ib_cq, cq->ib_cq.cq_context); + if (atomic_dec_and_test(&sp_cq->nq_users)) + wake_up_var(&sp_cq->nq_users); + spin_lock_bh(&nq->hwq.lock); +} + +/* Runs in softirq context, from system_bh_wq */ +static void bng_fp_service_nq(struct work_struct *work) +{ + struct bng_re_nq *nq = container_of(work, struct bng_re_nq, nq_work); + struct bng_re_hwq *hwq = &nq->hwq; + int budget = nq->budget; + struct nq_base *nqe; + u16 type; + + spin_lock_bh(&hwq->lock); + while (budget) { + nqe = bng_re_get_qe(hwq, hwq->cons, NULL); + if (!NQE_CMP_VALID(nqe, nq->nq_db.dbinfo.flags)) + break; + + /* + * The valid test of the entry must be done first before + * reading any further. + */ + dma_rmb(); + + type = le16_to_cpu(nqe->info10_type) & NQ_BASE_TYPE_MASK; + switch (type) { + case NQ_BASE_TYPE_CQ_NOTIFICATION: + bng_fp_process_cqn(nq, (struct nq_cn *)nqe); + break; + default: + dev_dbg(&nq->pdev->dev, + "FP: unhandled NQE type %#x\n", type); + break; + } + bng_re_hwq_incr_cons(hwq->max_elements, &hwq->cons, 1, + &nq->nq_db.dbinfo.flags); + budget--; + } + + if (!nq->requested) { + /* Stopping: update the consumer index, keep the NQ unarmed */ + bng_re_ring_nq_db(&nq->nq_db.dbinfo, nq->res->cctx, false); + } else if (!budget) { + /* Budget exhausted: update the consumer index, poll again */ + bng_re_ring_nq_db(&nq->nq_db.dbinfo, nq->res->cctx, false); + queue_work(system_bh_wq, &nq->nq_work); + } else { + bng_re_ring_nq_db(&nq->nq_db.dbinfo, nq->res->cctx, true); + } + spin_unlock_bh(&hwq->lock); +} + +static irqreturn_t bng_fp_nq_irq(int irq, void *dev_instance) +{ + struct bng_re_nq *nq = dev_instance; + + queue_work(system_bh_wq, &nq->nq_work); + return IRQ_HANDLED; +} + +static void bng_fp_map_nq_db(struct bng_re_nq *nq, u32 reg_offt) +{ + struct bng_re_reg_desc *dbreg = &nq->res->dpi_tbl.ucreg; + struct bng_re_nq_db *nq_db = &nq->nq_db; + + nq_db->reg.bar_id = dbreg->bar_id; + nq_db->reg.bar_base = dbreg->bar_base; + nq_db->reg.bar_reg = dbreg->bar_reg + reg_offt; + nq_db->reg.len = sizeof(u64); + + nq_db->dbinfo.db = nq_db->reg.bar_reg; + nq_db->dbinfo.hwq = &nq->hwq; + nq_db->dbinfo.xid = nq->ring_id; + nq_db->dbinfo.seed = nq->ring_id; + nq_db->dbinfo.flags = 0; + spin_lock_init(&nq_db->dbinfo.lock); + nq_db->dbinfo.res = nq->res; +} + +static int bng_fp_start_nq_irq(struct bng_re_nq *nq, int nq_idx, + int msix_vec) +{ + int rc; + + nq->name = kasprintf(GFP_KERNEL, "bng_re-nq-%d@pci:%s", nq_idx, + pci_name(nq->pdev)); + if (!nq->name) + return -ENOMEM; + + nq->msix_vec = msix_vec; + INIT_WORK(&nq->nq_work, bng_fp_service_nq); + rc = request_irq(nq->msix_vec, bng_fp_nq_irq, 0, nq->name, nq); + if (rc) { + kfree(nq->name); + nq->name = NULL; + return rc; + } + + /* Arm the NQ under hwq.lock, against the NQ work */ + spin_lock_bh(&nq->hwq.lock); + nq->requested = true; + bng_re_ring_nq_db(&nq->nq_db.dbinfo, nq->res->cctx, true); + spin_unlock_bh(&nq->hwq.lock); + return 0; +} + +static void bng_fp_stop_nq_irq(struct bng_re_nq *nq) +{ + if (!nq->requested) + return; + + /* Under hwq.lock, so the NQ work neither re-arms nor requeues */ + spin_lock_bh(&nq->hwq.lock); + nq->requested = false; + bng_re_ring_nq_db(&nq->nq_db.dbinfo, nq->res->cctx, false); + spin_unlock_bh(&nq->hwq.lock); + synchronize_irq(nq->msix_vec); + free_irq(nq->msix_vec, nq); + cancel_work_sync(&nq->nq_work); + kfree(nq->name); + nq->name = NULL; +} + +/* NQ resource management functions */ +static void bng_fp_free_nq(struct bng_re_res *res, struct bng_re_nq *nq) +{ + if (!nq) + return; + + xa_destroy(&nq->cq_xa); + bng_re_free_hwq(res, &nq->hwq); + nq->pdev = NULL; + nq->res = NULL; +} + +static int bng_fp_alloc_nq(struct bng_re_res *res, struct bng_re_nq *nq) +{ + struct bng_re_hwq_attr hwq_attr = {}; + struct bng_re_sg_info sginfo = {}; + + nq->pdev = res->pdev; + nq->res = res; + /* CQ handles start at 1 */ + xa_init_flags(&nq->cq_xa, XA_FLAGS_ALLOC1 | XA_FLAGS_LOCK_BH); + if (!nq->hwq.max_elements || + nq->hwq.max_elements > BNG_NQE_MAX_CNT) + nq->hwq.max_elements = BNG_NQE_MAX_CNT; + + sginfo.pgsize = PAGE_SIZE; + sginfo.pgshft = PAGE_SHIFT; + hwq_attr.res = res; + hwq_attr.sginfo = &sginfo; + hwq_attr.depth = nq->hwq.max_elements; + hwq_attr.stride = sizeof(struct nq_base); + hwq_attr.type = BNG_HWQ_TYPE_QUEUE; + if (bng_re_alloc_init_hwq(&nq->hwq, &hwq_attr)) { + dev_err(&nq->pdev->dev, "FP NQ allocation failed"); + return -ENOMEM; + } + nq->budget = BNG_NQ_BUDGET_DEFAULT; + return 0; +} + +/* Free the NQ memory only once FW has released the ring */ +static void bng_fp_release_nq(struct bng_re_dev *rdev, struct bng_re_nq *nq) +{ + if (bng_re_net_ring_free(rdev, nq->ring_id, + RING_ALLOC_REQ_RING_TYPE_NQ) && + !pci_channel_offline(rdev->bng_res.pdev)) { + ibdev_err(&rdev->ibdev, "Failed to free NQ ring %#x, leaking it", + nq->ring_id); + return; + } + bng_fp_free_nq(&rdev->bng_res, nq); +} + +static void bng_fp_destroy_nq(struct bng_re_dev *rdev, struct bng_re_nq *nq) +{ + bng_fp_stop_nq_irq(nq); + bng_fp_release_nq(rdev, nq); + nq->ring_id = 0xffff; +} + +int bng_fp_alloc_nq_res(struct bng_re_dev *rdev) +{ + struct bng_re_ring_attr rattr = {}; + int num_vec_created = 0; + int rc, i; + u8 type = RING_ALLOC_REQ_RING_TYPE_NQ; + + for (i = 0; i < rdev->nqr->num_msix - BNG_RE_MAX_RSVD_IRQ; i++) { + struct bnge_msix_info *msix; + struct bng_re_nq *nq; + + nq = &rdev->nqr->nq[i]; + msix = &rdev->nqr->msix_entries[i + BNG_RE_MAX_RSVD_IRQ]; + nq->hwq.max_elements = BNG_NQE_MAX_CNT; + rc = bng_fp_alloc_nq(&rdev->bng_res, nq); + if (rc) { + ibdev_err(&rdev->ibdev, "Alloc Failed NQ%d rc:%#x", + i, rc); + goto free_nq; + } + + rattr.dma_arr = nq->hwq.pbl[BNG_PBL_LVL_0].pg_map_arr; + rattr.pages = nq->hwq.pbl[rdev->nqr->nq[i].hwq.level].pg_count; + rattr.type = type; + rattr.mode = RING_ALLOC_REQ_INT_MODE_MSIX; + rattr.depth = BNG_NQE_MAX_CNT - 1; + rattr.lrid = msix->ring_idx; + rc = bng_re_net_ring_alloc(rdev, &rattr, &nq->ring_id); + if (rc) { + ibdev_err(&rdev->ibdev, + "Failed to allocate NQ fw id with rc = 0x%x", + rc); + /* On HWRM timeout (-EBUSY) FW may own the ring */ + if (rc != -EBUSY || + pci_channel_offline(rdev->bng_res.pdev)) + bng_fp_free_nq(&rdev->bng_res, nq); + goto free_nq; + } + + /* Service the NQ so CQ notifications reach comp_handler() */ + bng_fp_map_nq_db(nq, msix->db_offset); + rc = bng_fp_start_nq_irq(nq, i, msix->vector); + if (rc) { + ibdev_err(&rdev->ibdev, + "Failed to request NQ%d irq rc = %d", i, rc); + bng_fp_release_nq(rdev, nq); + goto free_nq; + } + num_vec_created++; + } + return 0; +free_nq: + for (i = num_vec_created - 1; i >= 0; i--) + bng_fp_destroy_nq(rdev, &rdev->nqr->nq[i]); + return rc; +} + +void bng_fp_free_nq_res(struct bng_re_dev *rdev) +{ + int i; + + for (i = 0; i < rdev->nqr->num_msix - BNG_RE_MAX_RSVD_IRQ; i++) + bng_fp_destroy_nq(rdev, &rdev->nqr->nq[i]); +} diff --git a/drivers/infiniband/hw/bng_re/bng_fp.h b/drivers/infiniband/hw/bng_re/bng_fp.h new file mode 100644 index 000000000000..fd5410dc3bdb --- /dev/null +++ b/drivers/infiniband/hw/bng_re/bng_fp.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (c) 2026 Broadcom. */ + +#ifndef __BNG_FP_H__ +#define __BNG_FP_H__ + +struct bng_re_dev; +struct bng_sp_cq; + +int bng_fp_alloc_nq_res(struct bng_re_dev *rdev); +void bng_fp_free_nq_res(struct bng_re_dev *rdev); + +void bng_fp_req_notify_cq(struct bng_sp_cq *cq, u32 arm_type); +#endif /* __BNG_FP_H__ */ diff --git a/drivers/infiniband/hw/bng_re/bng_fw.h b/drivers/infiniband/hw/bng_re/bng_fw.h index f89e4aeb2469..1571bdf61cff 100644 --- a/drivers/infiniband/hw/bng_re/bng_fw.h +++ b/drivers/infiniband/hw/bng_re/bng_fw.h @@ -4,6 +4,8 @@ #ifndef __BNG_FW_H__ #define __BNG_FW_H__ +#include + #include "bng_tlv.h" #include "bnge.h" #include "bnge_auxr.h" @@ -207,6 +209,23 @@ int bng_re_rcfw_start_irq(struct bng_re_rcfw *rcfw, int msix_vector, void bng_re_rcfw_stop_irq(struct bng_re_rcfw *rcfw, bool kill); int bng_re_rcfw_send_message(struct bng_re_rcfw *rcfw, struct bng_re_cmdqmsg *msg); + +/* False once commands are rejected unposted (device gone or FW stalled) */ +static inline bool bng_re_rcfw_alive(struct bng_re_rcfw *rcfw) +{ + return !pci_channel_offline(rcfw->pdev) && + !test_bit(FIRMWARE_STALL_DETECTED, &rcfw->cmdq.flags); +} + +/* + * After a timeout FW may still run the command, so keep its memory. + * Callers check bng_re_rcfw_alive() first, so the command was posted. + */ +static inline bool bng_re_fw_may_own(struct bng_re_rcfw *rcfw, int rc) +{ + return rc == -ETIMEDOUT && !pci_channel_offline(rcfw->pdev); +} + int bng_re_init_rcfw(struct bng_re_rcfw *rcfw, struct bng_re_stats *stats_ctx); int bng_re_deinit_rcfw(struct bng_re_rcfw *rcfw); diff --git a/drivers/infiniband/hw/bng_re/bng_re.h b/drivers/infiniband/hw/bng_re/bng_re.h index b55658f3ab8e..c290f015c82d 100644 --- a/drivers/infiniband/hw/bng_re/bng_re.h +++ b/drivers/infiniband/hw/bng_re/bng_re.h @@ -8,6 +8,10 @@ #include "bnge_auxr.h" #include "bng_res.h" #include "bng_fw.h" +#include "bng_sp.h" +#include +#include +#include #include #define BNG_RE_XID_AVOID_REUSE false @@ -224,12 +228,14 @@ struct bng_re_nq { u16 ring_id; int msix_vec; cpumask_t mask; - struct tasklet_struct nq_tasklet; + /* Services the NQ in softirq context, on system_bh_wq */ + struct work_struct nq_work; bool requested; int budget; u32 load; - - struct workqueue_struct *cqn_wq; + /* CQ notification handle -> struct bng_sp_cq */ + struct xarray cq_xa; + u32 cq_xa_next; }; struct bng_re_nq_record { @@ -293,6 +299,7 @@ struct bng_re_dev { struct bng_re_mpc_roce_creq_info mpc_roce_creq; struct bng_mpc_ctx *mpc; struct bng_re_qpc_limit_info qpc_limit_info; + struct bng_re_dpi dpi_privileged; }; #define to_bng_re_dev(ptr, member) \ diff --git a/drivers/infiniband/hw/bng_re/bng_res.h b/drivers/infiniband/hw/bng_re/bng_res.h index d7643a847204..93df8af08a54 100644 --- a/drivers/infiniband/hw/bng_re/bng_res.h +++ b/drivers/infiniband/hw/bng_re/bng_res.h @@ -41,6 +41,7 @@ #define RCFW_DBR_PCI_BAR_REGION 2 #define BNG_RE_FR_PMR 0x80000000 +#define BNG_NQ_BUDGET_DEFAULT 8 struct bng_re_gid { u8 data[16]; @@ -106,7 +107,6 @@ struct bng_re_drv_modes { u8 wqe_mode; bool db_push; bool dbr_pacing; - u32 toggle_bits; u8 roce_mirror; u8 dbr_primary_pf; bool st_tag_supported; @@ -161,25 +161,28 @@ struct bng_re_hwq_attr { }; struct bng_re_hwq { - struct pci_dev *pdev; /* lock to protect hwq */ spinlock_t lock; - struct bng_re_pbl pbl[BNG_PBL_LVL_MAX + 1]; - /* Valid values: 0, 1, 2 */ - enum bng_re_pbl_lvl level; - /* PBL entries */ - void **pbl_ptr; - /* PBL dma_addr */ - dma_addr_t *pbl_dma_ptr; - u32 max_elements; - /* Leaf level is backed by pinned user memory */ - bool is_user; - u32 depth; - u16 element_size; - u32 prod; - u32 cons; - /* queue entry per page */ - u32 qe_ppg; + /* The ring itself: everything but the lock, so it can be moved */ + struct_group(ring, + struct pci_dev *pdev; + struct bng_re_pbl pbl[BNG_PBL_LVL_MAX + 1]; + /* Valid values: 0, 1, 2 */ + enum bng_re_pbl_lvl level; + /* PBL entries */ + void **pbl_ptr; + /* PBL dma_addr */ + dma_addr_t *pbl_dma_ptr; + u32 max_elements; + /* Leaf level is backed by pinned user memory */ + bool is_user; + u32 depth; + u16 element_size; + u32 prod; + u32 cons; + /* queue entry per page */ + u32 qe_ppg; + ); }; struct bng_re_stats { @@ -257,6 +260,18 @@ static inline void *bng_re_get_qe(struct bng_re_hwq *hwq, (type) | BNG_RE_DBR_VALID) << 32) | (indx) | \ (((u32)(toggle)) << (BNG_RE_DBR_TOGGLE_SHIFT))) +static inline void bng_re_armen_db(struct bng_re_db_info *info, u32 type) +{ + u64 key = 0; + u8 toggle = 0; + + if (type == DBC_DBC_TYPE_CQ_ARMENA || type == DBC_DBC_TYPE_SRQ_ARMENA) + toggle = info->toggle; + /* Index always at 0 */ + key = BNG_RE_INIT_DBHDR(info->xid, type, 0, toggle); + writeq(key, info->priv_db); +} + static inline void bng_re_ring_db(struct bng_re_db_info *info, u32 type) { @@ -334,6 +349,54 @@ static inline bool _is_max_srq_ext_supported(u16 dev_cap_ext_flags_2) return !!(dev_cap_ext_flags_2 & CREQ_QUERY_FUNC_RESP_SB_MAX_SRQ_EXTENDED); } +#define ROCE_PG_SIZE_4K (4 * 1024) +#define ROCE_PG_SIZE_8K (8 * 1024) +#define ROCE_PG_SIZE_64K (64 * 1024) +#define ROCE_PG_SIZE_2M (2 * 1024 * 1024) +#define ROCE_PG_SIZE_8M (8 * 1024 * 1024) +#define ROCE_PG_SIZE_1G (1024 * 1024 * 1024) + +enum bng_re_hwrm_pg_size { + BNG_RE_HWRM_PG_SIZE_4K = 0, + BNG_RE_HWRM_PG_SIZE_8K = 1, + BNG_RE_HWRM_PG_SIZE_64K = 2, + BNG_RE_HWRM_PG_SIZE_2M = 3, + BNG_RE_HWRM_PG_SIZE_8M = 4, + BNG_RE_HWRM_PG_SIZE_1G = 5, +}; + +static inline u8 bng_re_base_pg_size(struct bng_re_hwq *hwq) +{ + u8 pg_size = BNG_RE_HWRM_PG_SIZE_4K; + struct bng_re_pbl *pbl; + + pbl = &hwq->pbl[BNG_PBL_LVL_0]; + switch (pbl->pg_size) { + case ROCE_PG_SIZE_4K: + pg_size = BNG_RE_HWRM_PG_SIZE_4K; + break; + case ROCE_PG_SIZE_8K: + pg_size = BNG_RE_HWRM_PG_SIZE_8K; + break; + case ROCE_PG_SIZE_64K: + pg_size = BNG_RE_HWRM_PG_SIZE_64K; + break; + case ROCE_PG_SIZE_2M: + pg_size = BNG_RE_HWRM_PG_SIZE_2M; + break; + case ROCE_PG_SIZE_8M: + pg_size = BNG_RE_HWRM_PG_SIZE_8M; + break; + case ROCE_PG_SIZE_1G: + pg_size = BNG_RE_HWRM_PG_SIZE_1G; + break; + default: + break; + } + + return pg_size; +} + void bng_re_free_hwq(struct bng_re_res *res, struct bng_re_hwq *hwq); diff --git a/drivers/infiniband/hw/bng_re/bng_sp.c b/drivers/infiniband/hw/bng_re/bng_sp.c index caa5ff44d99c..a4e6692a48ba 100644 --- a/drivers/infiniband/hw/bng_re/bng_sp.c +++ b/drivers/infiniband/hw/bng_re/bng_sp.c @@ -92,7 +92,15 @@ int bng_re_get_dev_attr(struct bng_re_rcfw *rcfw) attr->max_qp_sges = min_t(u32, sb->max_sge_var_wqe, BNG_VAR_MAX_SGE); attr->max_cq = le32_to_cpu(sb->max_cq); - attr->max_cq_wqes = le32_to_cpu(sb->max_cqe); + /* + * Bound by what RESIZE_CQ can encode, less one as the driver sizes + * the ring as cqe + 1. + */ + attr->max_cq_wqes = min_t(u32, le32_to_cpu(sb->max_cqe), + CMDQ_RESIZE_CQ_NEW_CQ_SIZE_MASK >> + CMDQ_RESIZE_CQ_NEW_CQ_SIZE_SFT); + if (attr->max_cq_wqes) + attr->max_cq_wqes--; attr->max_cq_sges = attr->max_qp_sges; attr->max_mr = le32_to_cpu(sb->max_mr); attr->max_mw = le32_to_cpu(sb->max_mw); @@ -559,3 +567,202 @@ int bng_sp_reg_mr(struct bng_re_res *res, struct bng_re_mrw *mr, bng_re_free_hwq(res, &mr->hwq); return rc; } + +/* + * Make the CQ unreachable from the NQ and wait for a running comp_handler(). + * Must not be called from comp_handler(). + */ +void bng_sp_cq_untrack(struct bng_sp_cq *cq) +{ + if (!cq->cq_handle) + return; + + spin_lock_bh(&cq->nq->hwq.lock); + xa_erase_bh(&cq->nq->cq_xa, cq->cq_handle); + spin_unlock_bh(&cq->nq->hwq.lock); + cq->cq_handle = 0; + wait_var_event(&cq->nq_users, !atomic_read(&cq->nq_users)); +} + +int bng_sp_create_cq(struct bng_re_res *res, struct bng_sp_cq *cq) +{ + struct bng_re_rcfw *rcfw = res->rcfw; + struct bng_re_hwq_attr hwq_attr = {}; + struct creq_create_cq_resp resp = {}; + struct bng_re_cmdqmsg msg = {}; + struct cmdq_create_cq req = {}; + struct bng_re_pbl *pbl; + u32 pg_sz_lvl, handle; + int rc; + + if (!cq->dpi) { + dev_err(&rcfw->pdev->dev, + "FP: CREATE_CQ failed due to NULL DPI\n"); + return -EINVAL; + } + + if (!bng_re_rcfw_alive(rcfw)) + return -ENODEV; + + /* Cyclic, so a stale NQE of a destroyed CQ doesn't match a new one */ + rc = xa_alloc_cyclic_bh(&cq->nq->cq_xa, &handle, cq, xa_limit_32b, + &cq->nq->cq_xa_next, GFP_KERNEL); + if (rc < 0) + return rc; + cq->cq_handle = handle; + + cq->dbinfo.flags = 0; + hwq_attr.res = res; + hwq_attr.depth = cq->max_wqe; + hwq_attr.stride = sizeof(struct cq_base); + hwq_attr.type = BNG_HWQ_TYPE_QUEUE; + hwq_attr.sginfo = &cq->sg_info; + rc = bng_re_alloc_init_hwq(&cq->hwq, &hwq_attr); + if (rc) + goto free_handle; + + bng_re_rcfw_cmd_prep((struct cmdq_base *)&req, + CMDQ_BASE_OPCODE_CREATE_CQ, + sizeof(req)); + + req.dpi = cpu_to_le32(cq->dpi->dpi); + req.cq_handle = cpu_to_le64(cq->cq_handle); + req.cq_size = cpu_to_le32(cq->max_wqe); + + pbl = &cq->hwq.pbl[BNG_PBL_LVL_0]; + pg_sz_lvl = (bng_re_base_pg_size(&cq->hwq) << + CMDQ_CREATE_CQ_PG_SIZE_SFT); + pg_sz_lvl |= (cq->hwq.level & CMDQ_CREATE_CQ_LVL_MASK); + req.pg_size_lvl = cpu_to_le32(pg_sz_lvl); + req.pbl = cpu_to_le64(pbl->pg_map_arr[0]); + req.cq_fco_cnq_id = cpu_to_le32( + (cq->cnq_hw_ring_id & CMDQ_CREATE_CQ_CNQ_ID_MASK) << + CMDQ_CREATE_CQ_CNQ_ID_SFT); + bng_re_fill_cmdqmsg(&msg, &req, &resp, NULL, sizeof(req), + sizeof(resp), 0); + rc = bng_re_rcfw_send_message(rcfw, &msg); + if (rc) + goto fail; + + cq->id = le32_to_cpu(resp.xid); + cq->period = BNG_QUEUE_START_PERIOD; + + cq->dbinfo.hwq = &cq->hwq; + cq->dbinfo.xid = cq->id; + cq->dbinfo.db = cq->dpi->dbr; + cq->dbinfo.priv_db = res->dpi_tbl.priv_db; + cq->dbinfo.flags = 0; + cq->dbinfo.toggle = 0; + cq->dbinfo.seed = 0; + spin_lock_init(&cq->dbinfo.lock); + cq->dbinfo.res = res; + cq->dbinfo.is_l2 = false; + + bng_re_armen_db(&cq->dbinfo, DBC_DBC_TYPE_CQ_ARMENA); + + return 0; + +fail: + /* After a timeout FW may still create the CQ on this ring: leak it */ + if (!bng_re_fw_may_own(rcfw, rc)) + bng_re_free_hwq(res, &cq->hwq); +free_handle: + bng_sp_cq_untrack(cq); + return rc; +} + +/* + * Switch to the resized ring; the caller holds cq_lock and frees @old_hwq. + * Only the rings move, each hwq keeps its own lock. + */ +void bng_sp_resize_cq_complete(struct bng_sp_cq *cq, + struct bng_re_hwq *old_hwq) +{ + old_hwq->ring = cq->hwq.ring; + cq->hwq.ring = cq->resize_hwq.ring; + memset(&cq->resize_hwq.ring, 0, sizeof(cq->resize_hwq.ring)); + cq->dbinfo.flags &= ~(1UL << BNG_RE_FLAG_EPOCH_CONS_SHIFT); +} + +/* + * Resize a user CQ. FW completes RESIZE_CQ only after HW has written the + * CUT_OFF CQE to the old ring and switched the CQ context to the new one, + * so HW no longer accesses the old ring once this returns successfully. + */ +int bng_sp_resize_cq(struct bng_re_res *res, struct bng_sp_cq *cq, + int new_cqes) +{ + struct bng_re_hwq_attr hwq_attr = {}; + struct bng_re_rcfw *rcfw = res->rcfw; + struct creq_resize_cq_resp resp = {}; + struct bng_re_cmdqmsg msg = {}; + struct cmdq_resize_cq req = {}; + struct bng_re_pbl *pbl; + u32 pg_sz, lvl, new_sz; + int rc; + + if (!bng_re_rcfw_alive(rcfw)) + return -ENODEV; + + bng_re_rcfw_cmd_prep((struct cmdq_base *)&req, + CMDQ_BASE_OPCODE_RESIZE_CQ, + sizeof(req)); + hwq_attr.sginfo = &cq->sg_info; + hwq_attr.res = res; + hwq_attr.depth = new_cqes; + hwq_attr.stride = sizeof(struct cq_base); + hwq_attr.type = BNG_HWQ_TYPE_QUEUE; + rc = bng_re_alloc_init_hwq(&cq->resize_hwq, &hwq_attr); + if (rc) + return rc; + + req.cq_cid = cpu_to_le32(cq->id); + pbl = &cq->resize_hwq.pbl[BNG_PBL_LVL_0]; + pg_sz = (bng_re_base_pg_size(&cq->resize_hwq) << + CMDQ_RESIZE_CQ_PG_SIZE_SFT) & CMDQ_RESIZE_CQ_PG_SIZE_MASK; + lvl = (cq->resize_hwq.level << CMDQ_RESIZE_CQ_LVL_SFT) & + CMDQ_RESIZE_CQ_LVL_MASK; + new_sz = (new_cqes << CMDQ_RESIZE_CQ_NEW_CQ_SIZE_SFT) & + CMDQ_RESIZE_CQ_NEW_CQ_SIZE_MASK; + req.new_cq_size_pg_size_lvl = cpu_to_le32(new_sz | pg_sz | lvl); + req.new_pbl = cpu_to_le64(pbl->pg_map_arr[0]); + + bng_re_fill_cmdqmsg(&msg, &req, &resp, NULL, sizeof(req), + sizeof(resp), 0); + rc = bng_re_rcfw_send_message(rcfw, &msg); + /* After a timeout FW may still switch to the new ring: keep it */ + if (rc && !bng_re_fw_may_own(rcfw, rc)) + bng_re_free_hwq(res, &cq->resize_hwq); + return rc; +} + +int bng_sp_destroy_cq(struct bng_re_res *res, struct bng_sp_cq *cq) +{ + struct bng_re_rcfw *rcfw = res->rcfw; + struct creq_destroy_cq_resp resp = {}; + struct bng_re_cmdqmsg msg = {}; + struct cmdq_destroy_cq req = {}; + int rc; + + /* Untrack first, so the NQ work never rings ARMENA for a released xid */ + bng_sp_cq_untrack(cq); + + bng_re_rcfw_cmd_prep((struct cmdq_base *)&req, + CMDQ_BASE_OPCODE_DESTROY_CQ, + sizeof(req)); + + req.cq_cid = cpu_to_le32(cq->id); + bng_re_fill_cmdqmsg(&msg, &req, &resp, NULL, sizeof(req), + sizeof(resp), 0); + rc = bng_re_rcfw_send_message(rcfw, &msg); + if (rc) { + /* FW may still DMA into the ring: leave it allocated */ + dev_err(&rcfw->pdev->dev, + "SP: Destroy CQ failed! Check FW health"); + return rc; + } + + bng_re_free_hwq(res, &cq->hwq); + return 0; +} + diff --git a/drivers/infiniband/hw/bng_re/bng_sp.h b/drivers/infiniband/hw/bng_re/bng_sp.h index 688f013eadfd..26a5178d63a5 100644 --- a/drivers/infiniband/hw/bng_re/bng_sp.h +++ b/drivers/infiniband/hw/bng_re/bng_sp.h @@ -86,6 +86,25 @@ struct bng_re_frpl { struct bng_re_hwq hwq; }; +#define BNG_QUEUE_START_PERIOD 0x01 +struct bng_sp_cq { + struct bng_re_dpi *dpi; + struct bng_re_db_info dbinfo; + u32 max_wqe; + u32 id; + u16 period; + struct bng_re_hwq hwq; + struct bng_re_hwq resize_hwq; + u32 cnq_hw_ring_id; + struct bng_re_nq *nq; + struct bng_re_sg_info sg_info; + /* Index in nq->cq_xa; 0 once the CQ is untracked */ + u64 cq_handle; + u8 toggle; + /* comp_handler() calls in flight, see bng_sp_cq_untrack() */ + atomic_t nq_users; +}; + int bng_re_get_dev_attr(struct bng_re_rcfw *rcfw); int bng_sp_alloc_sgid_tbl(struct bng_re_sgid_tbl *sgid_tbl, u16 size); @@ -114,5 +133,12 @@ int bng_sp_free_mrw(struct bng_re_res *res, struct bng_re_mrw *mrw); int bng_sp_reg_mr(struct bng_re_res *res, struct bng_re_mrw *mr, struct ib_umem *umem, u32 num_pbls, u32 buf_pg_size, bool unified_mr); +int bng_sp_create_cq(struct bng_re_res *res, struct bng_sp_cq *cq); +void bng_sp_resize_cq_complete(struct bng_sp_cq *cq, + struct bng_re_hwq *old_hwq); +int bng_sp_resize_cq(struct bng_re_res *res, struct bng_sp_cq *cq, + int new_cqes); +void bng_sp_cq_untrack(struct bng_sp_cq *cq); +int bng_sp_destroy_cq(struct bng_re_res *res, struct bng_sp_cq *cq); #endif diff --git a/drivers/infiniband/hw/bng_re/bng_verbs.c b/drivers/infiniband/hw/bng_re/bng_verbs.c index 4eb69c1bd873..8ec0a19867b5 100644 --- a/drivers/infiniband/hw/bng_re/bng_verbs.c +++ b/drivers/infiniband/hw/bng_re/bng_verbs.c @@ -21,6 +21,7 @@ #include "bnge_auxr.h" #include "bng_re.h" #include "bng_verbs.h" +#include "bng_fp.h" int bng_re_query_device(struct ib_device *ibdev, struct ib_device_attr *ib_attr, @@ -847,3 +848,482 @@ int bng_re_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata) return 0; } + +/* Returns NULL if the device has no fast path NQ to attach a CQ to. */ +static struct bng_re_nq *bng_re_get_nq(struct bng_re_dev *rdev) +{ + int min, indx; + + if (rdev->nqr->num_msix <= BNG_RE_MAX_RSVD_IRQ) + return NULL; + + mutex_lock(&rdev->nqr->load_lock); + for (indx = 0, min = 0; indx < (rdev->nqr->num_msix - BNG_RE_MAX_RSVD_IRQ); + indx++) { + if (rdev->nqr->nq[min].load > rdev->nqr->nq[indx].load) + min = indx; + } + rdev->nqr->nq[min].load++; + mutex_unlock(&rdev->nqr->load_lock); + + return &rdev->nqr->nq[min]; +} + +static void bng_re_put_nq(struct bng_re_dev *rdev, struct bng_re_nq *nq) +{ + mutex_lock(&rdev->nqr->load_lock); + nq->load--; + mutex_unlock(&rdev->nqr->load_lock); +} + +/* Stop NQE delivery and drop the NQ load; only the first call does it */ +static void bng_re_cq_detach_nq(struct bng_re_cq *cq) +{ + if (!cq->sp_cq.cq_handle) + return; + + bng_sp_cq_untrack(&cq->sp_cq); + bng_re_put_nq(cq->rdev, cq->sp_cq.nq); +} + +static int bng_re_setup_sginfo(struct bng_re_dev *rdev, + struct ib_umem *umem, + struct bng_re_sg_info *sginfo) +{ + unsigned long page_size; + + if (!umem) + return -EINVAL; + + page_size = ib_umem_find_best_pgsz(umem, SZ_4K, 0); + if (!page_size || page_size != SZ_4K) + return -EINVAL; + + sginfo->umem = umem; + sginfo->npages = ib_umem_num_dma_blocks(umem, page_size); + sginfo->pgsize = page_size; + sginfo->pgshft = __builtin_ctz(page_size); + return 0; +} + +int bng_re_create_user_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, + struct uverbs_attr_bundle *attrs) +{ + struct bng_re_cq *cq = container_of(ibcq, struct bng_re_cq, ib_cq); + struct bng_re_dev *rdev = to_bng_re_dev(ibcq->device, ibdev); + struct ib_udata *udata = &attrs->driver_udata; + struct bng_re_ucontext *uctx = + rdma_udata_to_drv_context(udata, struct bng_re_ucontext, ib_uctx); + struct bng_re_dev_attr *dev_attr = rdev->dev_attr; + struct bng_re_cq_resp resp = {}; + struct bng_re_cq_req req; + int rc, entries; + + if (attr->flags) + return -EOPNOTSUPP; + + /* Check for valid CQ entries */ + if (attr->cqe > dev_attr->max_cq_wqes) { + ibdev_dbg(&rdev->ibdev, + "Failed to create CQ, cqe:%d max_cq_wqes:%d", + attr->cqe, dev_attr->max_cq_wqes); + return -EINVAL; + } + + cq->rdev = rdev; + spin_lock_init(&cq->cq_lock); + + entries = bng_re_init_depth(attr->cqe + 1, uctx); + /* Power-of-two rounding can exceed the device limit. */ + if (entries > dev_attr->max_cq_wqes + 1) + entries = dev_attr->max_cq_wqes + 1; + + rc = ib_copy_validate_udata_in(udata, req, cq_va); + if (rc) + return rc; + + /* Honors the core CQ buffer attributes (VA/FD/UMEM) and falls back to + * the VA passed in the driver udata. + */ + cq->umem = ib_umem_get_cq_buf_or_va(&rdev->ibdev, attrs, req.cq_va, + entries * sizeof(struct cq_base), + IB_ACCESS_LOCAL_WRITE); + if (IS_ERR(cq->umem)) + return PTR_ERR(cq->umem); + + rc = bng_re_setup_sginfo(rdev, cq->umem, &cq->sp_cq.sg_info); + if (rc) + goto free_umem; + + cq->sp_cq.dpi = &uctx->dpi; + cq->sp_cq.max_wqe = entries; + cq->sp_cq.nq = bng_re_get_nq(rdev); + if (!cq->sp_cq.nq) { + ibdev_err(&rdev->ibdev, "Failed to create CQ, no NQ available"); + rc = -ENODEV; + goto free_umem; + } + cq->sp_cq.cnq_hw_ring_id = cq->sp_cq.nq->ring_id; + rc = bng_sp_create_cq(&rdev->bng_res, &cq->sp_cq); + if (rc) { + ibdev_err(&rdev->ibdev, "Failed to create HW CQ"); + bng_re_put_nq(rdev, cq->sp_cq.nq); + /* After a timeout FW may still DMA: keep the pages pinned */ + if (bng_re_fw_may_own(&rdev->rcfw, rc)) + return rc; + goto free_umem; + } + + mutex_init(&cq->resize_lock); + cq->ib_cq.cqe = entries; + + /* + * CQ toggle page support (the comp_mask flag in the ABI and the + * matching mmap of the toggle page) will be added in the next patch + * series. Until then resp.comp_mask stays 0, so userspace will not + * request the page. + */ + resp.cqid = cq->sp_cq.id; + resp.tail = cq->sp_cq.hwq.cons; + resp.phase = cq->sp_cq.period; + resp.rsvd = 0; + rc = ib_respond_udata(udata, resp); + if (rc) { + /* User-triggerable: a bad response buffer */ + ibdev_dbg(&rdev->ibdev, "Failed to copy CQ udata"); + goto destroy_cq; + } + + return 0; + +destroy_cq: + mutex_destroy(&cq->resize_lock); + bng_re_cq_detach_nq(cq); + /* On failure FW may still DMA: keep the pages pinned */ + if (bng_sp_destroy_cq(&rdev->bng_res, &cq->sp_cq)) + return rc; +free_umem: + ib_umem_release(cq->umem); + return rc; +} + +int bng_re_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, + struct uverbs_attr_bundle *attrs) +{ + struct bng_re_cq *cq = container_of(ibcq, struct bng_re_cq, ib_cq); + struct bng_re_dev *rdev = to_bng_re_dev(ibcq->device, ibdev); + struct bng_re_dev_attr *dev_attr = rdev->dev_attr; + int rc, depth; + + /* + * The ib_wc translation in bng_re_poll_cq() is added in the next + * patch series. Until then a kernel CQ can be created, armed and + * destroyed, but polling it returns no completions. bng_re provides + * no post_send/post_recv yet, so ib-core does not treat it as a + * kernel verbs provider and no in-kernel ULP attaches to it. + */ + ibdev_warn_ratelimited(&rdev->ibdev, + "kernel CQ completions are not supported yet\n"); + + if (attr->flags) { + rc = -EOPNOTSUPP; + goto cq_fail; + } + + /* Check for valid CQ entries */ + if (attr->cqe > dev_attr->max_cq_wqes) { + ibdev_err(&rdev->ibdev, + "Failed to create CQ, cqe:%d max_cq_wqes:%d", + attr->cqe, dev_attr->max_cq_wqes); + rc = -EINVAL; + goto cq_fail; + } + + cq->rdev = rdev; + spin_lock_init(&cq->cq_lock); + + depth = attr->cqe + 1; + cq->sp_cq.sg_info.pgsize = SZ_4K; + cq->sp_cq.dpi = &rdev->dpi_privileged; + cq->sp_cq.max_wqe = depth; + cq->sp_cq.nq = bng_re_get_nq(rdev); + if (!cq->sp_cq.nq) { + ibdev_err(&rdev->ibdev, "Failed to create CQ, no NQ available"); + rc = -ENODEV; + goto cq_fail; + } + cq->sp_cq.cnq_hw_ring_id = cq->sp_cq.nq->ring_id; + + rc = bng_sp_create_cq(&rdev->bng_res, &cq->sp_cq); + if (rc) { + ibdev_err(&rdev->ibdev, "Failed to create HW CQ"); + goto create_cq_fail; + } + + mutex_init(&cq->resize_lock); + cq->ib_cq.cqe = depth; + return 0; + +create_cq_fail: + bng_re_put_nq(rdev, cq->sp_cq.nq); +cq_fail: + return rc; +} + +int bng_re_resize_cq(struct ib_cq *ibcq, unsigned int cqe, struct ib_udata *udata) +{ + struct bng_re_sg_info sg_info = {}; + struct bng_re_ucontext *uctx = NULL; + struct bng_re_dpi *orig_dpi = NULL; + struct bng_re_dev_attr *dev_attr; + struct bng_re_resize_cq_req req; + struct bng_re_dev *rdev; + struct bng_re_cq *cq; + int rc, entries; + + cq = container_of(ibcq, struct bng_re_cq, ib_cq); + rdev = cq->rdev; + dev_attr = rdev->dev_attr; + if (!ibcq->uobject || !udata) { + ibdev_dbg(&rdev->ibdev, "Kernel CQ Resize not supported"); + rc = -EOPNOTSUPP; + goto resize_fail; + } + + rc = ib_copy_validate_udata_in(udata, req, cq_va); + if (rc) + goto resize_fail; + + /* There is no driver response data */ + rc = ib_respond_empty_udata(udata); + if (rc) + goto resize_fail; + + /* All failures below are user-triggerable: log at debug level only */ + mutex_lock(&cq->resize_lock); + if (cq->resize_umem) { + ibdev_dbg(&rdev->ibdev, "Resize CQ %#x failed - Busy", + cq->sp_cq.id); + mutex_unlock(&cq->resize_lock); + rc = -EBUSY; + goto resize_fail; + } + + /* Check the requested cq depth out of supported depth */ + if (cqe < 1 || cqe > dev_attr->max_cq_wqes) { + ibdev_dbg(&rdev->ibdev, "Resize CQ %#x failed - out of range cqe %d", + cq->sp_cq.id, cqe); + mutex_unlock(&cq->resize_lock); + rc = -EINVAL; + goto resize_fail; + } + + uctx = rdma_udata_to_drv_context(udata, struct bng_re_ucontext, ib_uctx); + entries = bng_re_init_depth(cqe + 1, uctx); + if (entries > dev_attr->max_cq_wqes + 1) + entries = dev_attr->max_cq_wqes + 1; + + /* Check to see if the new requested size can be handled by already + * existing CQ + */ + if (entries == cq->ib_cq.cqe) { + ibdev_dbg(&rdev->ibdev, "CQ is already at size %d", cqe); + mutex_unlock(&cq->resize_lock); + return 0; + } + + cq->resize_umem = ib_umem_get_va(&rdev->ibdev, req.cq_va, + entries * sizeof(struct cq_base), + IB_ACCESS_LOCAL_WRITE); + if (IS_ERR(cq->resize_umem)) { + rc = PTR_ERR(cq->resize_umem); + ibdev_dbg(&rdev->ibdev, "ib_umem_get_va failed! rc = %pe\n", + cq->resize_umem); + cq->resize_umem = NULL; + goto udata_fail; + } + + cq->resize_cqe = entries; + memcpy(&sg_info, &cq->sp_cq.sg_info, sizeof(sg_info)); + orig_dpi = cq->sp_cq.dpi; + + rc = bng_re_setup_sginfo(rdev, cq->resize_umem, &cq->sp_cq.sg_info); + if (rc) + goto udata_fail; + cq->sp_cq.dpi = &uctx->dpi; + + rc = bng_sp_resize_cq(&rdev->bng_res, &cq->sp_cq, entries); + if (bng_re_fw_may_own(&rdev->rcfw, rc)) { + /* + * FW may still switch rings: keep the new one pending until + * destroy_cq frees it. + */ + ibdev_err_ratelimited(&rdev->ibdev, "Resize HW CQ %#x timed out", + cq->sp_cq.id); + cq->resize_timedout = true; + memcpy(&cq->sp_cq.sg_info, &sg_info, sizeof(sg_info)); + cq->sp_cq.dpi = orig_dpi; + mutex_unlock(&cq->resize_lock); + return rc; + } + if (rc) { + ibdev_err_ratelimited(&rdev->ibdev, "Resize HW CQ %#x failed!", + cq->sp_cq.id); + goto udata_fail; + } + + cq->ib_cq.cqe = cq->resize_cqe; + mutex_unlock(&cq->resize_lock); + return 0; + +udata_fail: + if (cq->resize_umem) { + ib_umem_release(cq->resize_umem); + cq->resize_umem = NULL; + cq->resize_cqe = 0; + memcpy(&cq->sp_cq.sg_info, &sg_info, sizeof(sg_info)); + cq->sp_cq.dpi = orig_dpi; + } + mutex_unlock(&cq->resize_lock); +resize_fail: + return rc; +} + +int bng_re_req_notify_cq(struct ib_cq *ib_cq, + enum ib_cq_notify_flags ib_cqn_flags) +{ + struct bng_re_cq *cq = container_of(ib_cq, struct bng_re_cq, ib_cq); + unsigned long flags; + int type = 0; + + /* + * A user CQ is armed by the user library through its own doorbell + * page, with the consumer index only it knows. The kernel has no + * valid cons/epoch for a user ring, so don't ring an ARM doorbell + * on its behalf. + */ + if (cq->umem) + return -EOPNOTSUPP; + + /* Trigger on the very next completion */ + if (ib_cqn_flags & IB_CQ_NEXT_COMP) + type = DBC_DBC_TYPE_CQ_ARMALL; + /* Trigger on the next solicited completion */ + else if (ib_cqn_flags & IB_CQ_SOLICITED) + type = DBC_DBC_TYPE_CQ_ARMSE; + + /* + * IB_CQ_REPORT_MISSED_EVENTS is not reported yet: bng_re_poll_cq() + * does not consume kernel CQEs, so a positive return would make + * ib_cq_poll_work() requeue itself forever. + */ + spin_lock_irqsave(&cq->cq_lock, flags); + bng_fp_req_notify_cq(&cq->sp_cq, type); + spin_unlock_irqrestore(&cq->cq_lock, flags); + return 0; +} + +/* Called by ib_free_cq() before it cancels the CQ poll work */ +int bng_re_pre_destroy_cq(struct ib_cq *ib_cq) +{ + bng_re_cq_detach_nq(container_of(ib_cq, struct bng_re_cq, ib_cq)); + return 0; +} + +int bng_re_destroy_cq(struct ib_cq *ib_cq, struct ib_udata *udata) +{ + struct bng_re_dev *rdev; + struct bng_re_cq *cq; + int rc; + + cq = container_of(ib_cq, struct bng_re_cq, ib_cq); + rdev = cq->rdev; + + /* destroy_cq takes no driver request data and returns none */ + rc = ib_no_udata_io(udata); + if (rc) + return rc; + + /* ib_free_cq() did this in pre_destroy_cq(); other destroy paths didn't */ + bng_re_cq_detach_nq(cq); + + /* On failure FW may still DMA into the CQ memory: don't free it */ + rc = bng_sp_destroy_cq(&rdev->bng_res, &cq->sp_cq); + if (rc) { + ibdev_err(&rdev->ibdev, "Failed to destroy HW CQ %#x rc %d", + cq->sp_cq.id, rc); + return rc; + } + + /* HW is done with the CQ now: release what a pending resize left behind */ + mutex_lock(&cq->resize_lock); + if (cq->resize_umem) { + /* resize was issued but never completed from poll_cq */ + bng_re_free_hwq(&rdev->bng_res, &cq->sp_cq.resize_hwq); + ib_umem_release(cq->resize_umem); + cq->resize_umem = NULL; + } + mutex_unlock(&cq->resize_lock); + mutex_destroy(&cq->resize_lock); + + if (cq->umem) + ib_umem_release(cq->umem); + + return 0; +} + +/* Called with resize_lock held and a resize pending */ +static void bng_re_resize_cq_complete(struct bng_re_cq *cq) +{ + struct bng_re_dev *rdev = cq->rdev; + struct bng_re_hwq old_hwq = {}; + unsigned long flags; + + /* cq->hwq and cq->dbinfo are serialized by cq_lock */ + spin_lock_irqsave(&cq->cq_lock, flags); + bng_sp_resize_cq_complete(&cq->sp_cq, &old_hwq); + cq->sp_cq.max_wqe = cq->resize_cqe; + spin_unlock_irqrestore(&cq->cq_lock, flags); + + bng_re_free_hwq(&rdev->bng_res, &old_hwq); + ib_umem_release(cq->umem); + cq->umem = cq->resize_umem; + cq->resize_umem = NULL; + cq->resize_cqe = 0; +} + +int bng_re_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) +{ + struct bng_re_cq *cq = + container_of(ibcq, struct bng_re_cq, ib_cq); + + /* + * User CQ: the only processing done here is to complete a pending + * resize, which the user library requests once it has consumed the + * CUT_OFF CQE of the old ring. Freeing the old ring is safe whenever + * this is called: FW completes RESIZE_CQ only after HW has written + * the CUT_OFF CQE and switched the CQ context to the new ring, so HW + * no longer accesses the old ring. + */ + if (cq->umem) { + /* + * Safe to sleep here: this path only runs for user + * (umem-backed) CQs, whose poll_cq always runs in process + * context via ib_uverbs_poll_cq(). A future kernel CQ poll + * path must stay atomic-context safe - don't block there. + */ + mutex_lock(&cq->resize_lock); + if (cq->resize_umem && !cq->resize_timedout) + bng_re_resize_cq_complete(cq); + mutex_unlock(&cq->resize_lock); + return 0; + } + + /* + * Kernel CQ completions are not supported yet: ib_wc translation is + * added in the next patch series. Until then neither drain the HW ring + * nor report completions: wc[] is never populated, and a positive + * return would make __ib_process_cq() call wr_cqe->done() on garbage. + */ + return 0; +} diff --git a/drivers/infiniband/hw/bng_re/bng_verbs.h b/drivers/infiniband/hw/bng_re/bng_verbs.h index eedb01a7aded..b11294558e8d 100644 --- a/drivers/infiniband/hw/bng_re/bng_verbs.h +++ b/drivers/infiniband/hw/bng_re/bng_verbs.h @@ -44,6 +44,28 @@ struct bng_re_mr { struct bng_re_frpl sp_frpl; }; +struct bng_re_cq { + struct ib_cq ib_cq; + struct bng_re_dev *rdev; + /* Protects sp_cq.dbinfo/toggle and the sp_cq.hwq switch */ + spinlock_t cq_lock; + struct bng_sp_cq sp_cq; + struct ib_umem *umem; + struct ib_umem *resize_umem; + int resize_cqe; + /* RESIZE_CQ timed out: the resize stays pending until destroy */ + bool resize_timedout; + /* Protects umem and the resize_* fields between resize and poll_cq */ + struct mutex resize_lock; +}; + +static inline u32 bng_re_init_depth(u32 ent, struct bng_re_ucontext *uctx) +{ + /* @ent must be non-zero: roundup_pow_of_two(0) is undefined */ + return uctx ? (uctx->cmask & BNG_RE_UCNTX_CMASK_POW2_DISABLED) ? + ent : roundup_pow_of_two(ent) : ent; +} + int bng_re_query_device(struct ib_device *ibdev, struct ib_device_attr *ib_attr, struct ib_udata *udata); int bng_re_modify_device(struct ib_device *ibdev, int device_modify_mask, @@ -81,5 +103,15 @@ struct ib_mr *bng_re_reg_user_mr_dmabuf(struct ib_pd *ib_pd, u64 start, int fd, int mr_access_flags, struct ib_dmah *dmah, struct uverbs_attr_bundle *attrs); +int bng_re_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, + struct uverbs_attr_bundle *attrs); +int bng_re_create_user_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, + struct uverbs_attr_bundle *attrs); +int bng_re_resize_cq(struct ib_cq *ibcq, unsigned int cqe, struct ib_udata *udata); +int bng_re_req_notify_cq(struct ib_cq *ib_cq, + enum ib_cq_notify_flags ib_cqn_flags); +int bng_re_pre_destroy_cq(struct ib_cq *cq); +int bng_re_destroy_cq(struct ib_cq *cq, struct ib_udata *udata); +int bng_re_poll_cq(struct ib_cq *ib_cq, int num_entries, struct ib_wc *wc); #endif /* __BNG_RE_VERBS_H__ */ diff --git a/include/uapi/rdma/bng_re-abi.h b/include/uapi/rdma/bng_re-abi.h index 3f571fd35a23..ebf2ff965fc9 100644 --- a/include/uapi/rdma/bng_re-abi.h +++ b/include/uapi/rdma/bng_re-abi.h @@ -44,4 +44,22 @@ struct bng_re_pd_resp { __u32 comp_mask; }; +struct bng_re_cq_req { + __aligned_u64 cq_va; + /* Not used yet; will be used in a future series */ + __aligned_u64 cq_handle; +}; + +struct bng_re_cq_resp { + __u32 cqid; + __u32 tail; + __u32 phase; + __u32 rsvd; + __aligned_u64 comp_mask; +}; + +struct bng_re_resize_cq_req { + __aligned_u64 cq_va; +}; + #endif /* __BNG_RE_UVERBS_ABI_H__*/ -- 2.43.5