From patchwork Mon Oct 3 02:10:29 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9359923 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E7B9860459 for ; Mon, 3 Oct 2016 02:11:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D8A0C28986 for ; Mon, 3 Oct 2016 02:11:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CBBBF289AD; Mon, 3 Oct 2016 02:11:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFED028904 for ; Mon, 3 Oct 2016 02:11:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752117AbcJCCLT (ORCPT ); Sun, 2 Oct 2016 22:11:19 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:24867 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752065AbcJCCLS (ORCPT ); Sun, 2 Oct 2016 22:11:18 -0400 Received: from sc9-mailhost1.vmware.com (10.113.161.71) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Sun, 2 Oct 2016 19:10:40 -0700 Received: from EX13-CAS-008.vmware.com (smtp-inbound.vmware.com [10.113.191.58]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id E69FD18086; Sun, 2 Oct 2016 19:11:16 -0700 (PDT) Received: from EX13-CAS-003.vmware.com (10.113.191.53) by EX13-MBX-030.vmware.com (10.113.191.50) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Sun, 2 Oct 2016 19:11:16 -0700 Received: from NAM01-SN1-obe.outbound.protection.outlook.com (10.113.170.11) by EX13-CAS-003.vmware.com (10.113.191.53) with Microsoft SMTP Server (TLS) id 15.0.1156.6 via Frontend Transport; Sun, 2 Oct 2016 19:11:16 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onevmw.onmicrosoft.com; s=selector1-vmware-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=3lq+itRRkVUqD+a+a/oVuzeqcm9eEHrRF4p1rTdhAUM=; b=ibBlwOimmhazCOjeod0fS4vc5qCmsgN73Y7IAxtIePwfFzwhWi5kV6r0ip1dAZ4EqQyZraYsn+J/f8zx8zjgbmwvi9Bo8fSpl+yXjUJV6DCCSe6cSHs51u3Z1s6JKivLoTU61dm9s1sFYeWyk89Nn8+VWGuLzqYxeOw1Mbe3FEQ= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=aditr@vmware.com; Received: from promb-2s-dhcp95-136.eng.vmware.com (208.91.1.34) by DM2PR0501MB841.namprd05.prod.outlook.com (10.242.115.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.649.6; Mon, 3 Oct 2016 02:11:04 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , Subject: [PATCH v6 09/16] IB/pvrdma: Add support for Completion Queues Date: Sun, 2 Oct 2016 19:10:29 -0700 Message-ID: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [208.91.1.34] X-ClientProxiedBy: CY1PR1101CA0039.namprd11.prod.outlook.com (10.169.17.49) To DM2PR0501MB841.namprd05.prod.outlook.com (10.242.115.147) X-MS-Office365-Filtering-Correlation-Id: 4365bd22-c66d-47ee-c6d0-08d3eb328808 X-Microsoft-Exchange-Diagnostics: 1; DM2PR0501MB841; 2:Kyl5dSNmyfGHOCxU15pPcTR5Qr9eqW6wuZyaigIZOUPsrmoIt2qhT/v4yyZ8UdUJBDQysnj3R5wMeOA8jU0qTKOF++43p9ASejJwP4iGLw0q1Gqyz5fmH0vEhEhCe63K02+h5Yz5aTFJM0QLjxKOpyLw1ZYwJ8nATr2AutpTQ2WHVk3ejme5Pp1uu6BK3wfK0a5PaADl+W+jBcdCncQhfw==; 3:KRSX9IQgFbtWGUjE7tjuv5dJ2ejy+kWpPwxeZnq5LoG1Jj5fgpmPij8wRRLbjYToi6fUBm3FKMZKxbGjVtAh/FX1Fp/3Gv3zlFQM07D/DpI0IFQZP0iWNbuQxplyWu2dSLdYf/rxNBcf4cQcaDiOag==; 25:NxAPV7ZKi+HjZuMlxNoGBhdOtdERxZcLlv+gpxVf4EBqBRQudeZh2/25FvgP57bwOTnFQJ3Jxs0Owf7h8o/hU2N1eOJZPeSGQsm3mj0+qknaoY/Rors8CxwCrt+QsQpzqrNvqg4CelJ4ahWFfs8IOYkPVX+dT8+deoA3x7ohikGerMOuA+fsufZD7J8T1zKEKklQg99slpKwy6KU7PbCfTkoq+rRapH6sBxUfbuUN0CfWTBH+0u5gEhZTvtFmHyNWYGiA1aE4XCBsEEubX04FQyOZHr5X3YvH807evCDag4wuoqopX/kn1R51tTtsPek5ok4oBVH+aYpS8P7GipSlMTZPID9ISfB/PS0RP4+IleTzaKzpBOVduX94Jtt2VeBaYF/KIzKR/VgdTpC19tq8BkMc2krYvWQL4NcgpBN8eawUjgUxvBDc4FC1gZGUhmW X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM2PR0501MB841; X-LD-Processed: b39138ca-3cee-4b4a-a4d6-cd83d9dd62f0,ExtAddr X-Microsoft-Exchange-Diagnostics: 1; DM2PR0501MB841; 31:9j5co6FLAeRvVWBuPB7fDhmOMu8RdAW5f3R5Tv+wf1DijTUVII3Y2Z0EKd02Bk1nymDHVkSeB3qVNtnLuBCtQE4aPvGEyEYrg9BsmK2eyt8prSRVpuqmPHJ6bH1xrDblwO+9lT9lWCiiXM4ruxvaV87pYJg7q9jkFndzD0ACcznfeZhzvBpjfnl3ikstf7WQAOxl83yAot9uBwqtNOzK7uSow5FkZyHFE4RG6FEEqzpDnPkm0JwZjO8WEn+5mZNj; 20:ZegjJiTxixk4V4CMpL91Y1DmZPkg1H447kaHZPhpnr0yyagTvt+kIDEOI1j392WyA0JaSGbr7RLrVHNTRZEy4Vft4j+C+4+Omqgh8Sj2I7fWIewRQy5U1z00/PNb/XwFS6/kBxYkEiZmBH0Gljv5NJR0oMA9MzyxXPkUM4NeqfJ/CsBF6ugZqEdcuWg5ACUvWcNiy4g2cfCjKymayXUUzORXFOWm3VhCRb/B6B2zCc1IqeR6dbBFu0sd+2iGlgauY4zPekjYeCIZBX9pkguoQli6N4IB7Bx9/CpRz1TvuIIurWPxCOF1/fTUbOQjpBnrRk3ci+m9a+6gZEgNRMxuUvvKH86AxLEtr7e9yAGOG+joBTM34HD9qAobNPRaxa7xuM4rRKSy/0qHvPk6NH60iEXW9JTjPvZYEXq8h1ToL3XwKjgMW4SoY8yc1zNpw1+GZN/5rB0Yhk+YYyw/11NDjVs6KA5Zt9okFvTobd8F27sv7Svhdxslp24x/OIM2Vh6 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(61668805478150)(22074186197030)(146099531331640); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001); SRVR:DM2PR0501MB841; BCL:0; PCL:0; RULEID:; SRVR:DM2PR0501MB841; X-Microsoft-Exchange-Diagnostics: 1; DM2PR0501MB841; 4:1fJrhn5+BOP1ToMI0BRmq8Usc3LtGhocMpHmWiK9Lsyp0Aj+h0RNB0WGPSpxO/7J6uIhObEmATEqJche6iX2L6x3pspsJCW/quiF0DNfiAwJxpPqGL9F8j4PZR3AzZeiXrph+SDLsY9oLnYwWhav9Mo0WBTtqnm0IIz9nrkJaL57OcJvzkplj1dbUuW4tVMcBLk5zwiOZeZqwOj1qq00YG1WwtW1WE1Alj/EBzKIiHQ5X5aXuN5WsudlFVBSk7Bl6lu7unsyCy5+C0rV/yMMFLIcJJ8UYqzxLdLpVCDej+4+gOga/BEstMyg5Oew+qnOOLe66pM30I2u1PrhtfPC5wjwuknE5RqW7mTUl/xtPh+6cyFBJlideEj9f8aK/fkVHemqiWI6Fo8ILBvnWEYMzOaFRRLXDF1Pul/HtFQacb++ckQ/+Fdo4MsvaMMdLBs7Ii/BfIpH9Dpskuo70GrudirqD5gaaQURx75faAi4ighcMkVn5UsfbLrFdZLYz4PjGZoTiTJJLg2cMFM15W1yTw1Y1XnJ6l6bfktfOWCmPu4= X-Forefront-PRVS: 008421A8FF X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(189002)(199003)(97736004)(189998001)(42186005)(19580405001)(47776003)(8676002)(5001770100001)(33646002)(2906002)(19580395003)(15975445007)(77096005)(6862003)(50986999)(76176999)(106356001)(2950100002)(101416001)(5003940100001)(4326007)(5660300001)(107886002)(105586002)(92566002)(7846002)(7736002)(586003)(118296001)(229853001)(68736007)(4001430100002)(305945005)(2201001)(48376002)(6636002)(50466002)(66066001)(86362001)(50226002)(6116002)(3846002)(81156014)(6666003)(36756003)(81166006)(7099028)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:DM2PR0501MB841; H:promb-2s-dhcp95-136.eng.vmware.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (EX13-EDG-OU-001.vmware.com: aditr@vmware.com does not designate permitted sender hosts) Received-SPF: None (protection.outlook.com: vmware.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-asc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nisHbJctPraUx0woMW3ygknlg1bYV6HMXzgL15CghnWFhkswRB/RWoXNZwj1leo6gMjp/68Sq/odIGkLlpRJ4hvGxPQ6aY=; 7:36fjbuMHaYxpSEx/AsmRFcWpXMIGNYbGKN229C/tcMePzzaQCNfSU4sL68wD6uv4rztlyhVEKo61rrn6+7DL/0XJlDZMC+VTvPzXSj1kmJp3eMYHdv/RykKxkP8Qi++1Vyv6vZQx7aJDzThcfSLLOaZdIHe+VlKB5w1qBfhl/Iy7ip2kVA3XLvb1fXmGtWOhjkf9qYSb5GPApS8f0WC0kPUM4F1tdes9dFtCJ6PRH+p1/3P/F1IloTOWiO5QFBMS7poVy+qoEErk85Db8J/zhnpsWkp8k2aSneuBzHwivI9oazFIl9IZqi2/IMvG9uCVi5QSDXW XiFcvmDlKE65cZg== SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM2PR0501MB841; 20:j4ckP5yRjPXgBi9sB3NycCUTLQ/+CBY8own4UqlAS888iIMmNPEnq47XAgxMkrdhaC1olYwP1VTOM59qY2iPmYyXYw4Ik1+n4nrd07HukwDG9xNFIedFC6/ZCUjwlxq3tOVLTpUEfMTNv6O8I8if7of6chC/uVaEKtU2GwWYuP4= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2016 02:11:04.3393 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR0501MB841 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds the support for creating and destroying completion queues on the paravirtual RDMA device. Reviewed-by: Yuval Shaia Reviewed-by: Jorgen Hansen Reviewed-by: George Zhang Reviewed-by: Aditya Sarwade Reviewed-by: Bryan Tan Signed-off-by: Adit Ranadive --- Changes v5->v6: - Removed duplicate include of abi header. Changes v4->v5: - Updated include for headers in UAPI folder. - Changed from EINVAL to ENOMEM if atomic add fails. - Added error code if destroy cq command failed. - Update to pvrdma_cmd_post for creating/destroying CQ. Changes v3->v4: - Added a pvrdma_destroy_cq in the error path. - Renamed pvrdma_flush_cqe to _pvrdma_flush_cqe since we need a lock to be held while calling this. - Updated to use wrapper for UAR write for CQ. - Ensure that poll_cq does not return error values. Changes v2->v3: - Removed boolean from pvrdma_cmd_post. - Return -EAGAIN if qp retrieved from CQE is bogus. - Check for invalid index of ring. --- drivers/infiniband/hw/pvrdma/pvrdma_cq.c | 425 +++++++++++++++++++++++++++++++ 1 file changed, 425 insertions(+) create mode 100644 drivers/infiniband/hw/pvrdma/pvrdma_cq.c diff --git a/drivers/infiniband/hw/pvrdma/pvrdma_cq.c b/drivers/infiniband/hw/pvrdma/pvrdma_cq.c new file mode 100644 index 0000000..e429ca5 --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_cq.c @@ -0,0 +1,425 @@ +/* + * Copyright (c) 2012-2016 VMware, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of EITHER the GNU General Public License + * version 2 as published by the Free Software Foundation or the BSD + * 2-Clause License. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED + * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License version 2 for more details at + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html. + * + * You should have received a copy of the GNU General Public License + * along with this program available in the file COPYING in the main + * directory of this source tree. + * + * The BSD 2-Clause License + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include +#include +#include + +#include "pvrdma.h" + +/** + * pvrdma_req_notify_cq - request notification for a completion queue + * @ibcq: the completion queue + * @notify_flags: notification flags + * + * @return: 0 for success. + */ +int pvrdma_req_notify_cq(struct ib_cq *ibcq, + enum ib_cq_notify_flags notify_flags) +{ + struct pvrdma_dev *dev = to_vdev(ibcq->device); + struct pvrdma_cq *cq = to_vcq(ibcq); + u32 val = cq->cq_handle; + + val |= (notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ? + PVRDMA_UAR_CQ_ARM_SOL : PVRDMA_UAR_CQ_ARM; + + pvrdma_write_uar_cq(dev, val); + + return 0; +} + +/** + * pvrdma_create_cq - create completion queue + * @ibdev: the device + * @attr: completion queue attributes + * @context: user context + * @udata: user data + * + * @return: ib_cq completion queue pointer on success, + * otherwise returns negative errno. + */ +struct ib_cq *pvrdma_create_cq(struct ib_device *ibdev, + const struct ib_cq_init_attr *attr, + struct ib_ucontext *context, + struct ib_udata *udata) +{ + int entries = attr->cqe; + struct pvrdma_dev *dev = to_vdev(ibdev); + struct pvrdma_cq *cq; + int ret; + int npages; + unsigned long flags; + union pvrdma_cmd_req req; + union pvrdma_cmd_resp rsp; + struct pvrdma_cmd_create_cq *cmd = &req.create_cq; + struct pvrdma_cmd_create_cq_resp *resp = &rsp.create_cq_resp; + struct pvrdma_create_cq ucmd; + + BUILD_BUG_ON(sizeof(struct pvrdma_cqe) != 64); + + entries = roundup_pow_of_two(entries); + if (entries < 1 || entries > dev->dsr->caps.max_cqe) + return ERR_PTR(-EINVAL); + + if (!atomic_add_unless(&dev->num_cqs, 1, dev->dsr->caps.max_cq)) + return ERR_PTR(-ENOMEM); + + cq = kzalloc(sizeof(*cq), GFP_KERNEL); + if (!cq) { + atomic_dec(&dev->num_cqs); + return ERR_PTR(-ENOMEM); + } + + cq->ibcq.cqe = entries; + + if (context) { + if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) { + ret = -EFAULT; + goto err_cq; + } + + cq->umem = ib_umem_get(context, ucmd.buf_addr, ucmd.buf_size, + IB_ACCESS_LOCAL_WRITE, 1); + if (IS_ERR(cq->umem)) { + ret = PTR_ERR(cq->umem); + goto err_cq; + } + + npages = ib_umem_page_count(cq->umem); + } else { + cq->is_kernel = true; + + /* One extra page for shared ring state */ + npages = 1 + (entries * sizeof(struct pvrdma_cqe) + + PAGE_SIZE - 1) / PAGE_SIZE; + + /* Skip header page. */ + cq->offset = PAGE_SIZE; + } + + if (npages < 0 || npages > PVRDMA_PAGE_DIR_MAX_PAGES) { + dev_warn(&dev->pdev->dev, + "overflow pages in completion queue\n"); + ret = -EINVAL; + goto err_umem; + } + + ret = pvrdma_page_dir_init(dev, &cq->pdir, npages, cq->is_kernel); + if (ret) { + dev_warn(&dev->pdev->dev, + "could not allocate page directory\n"); + goto err_umem; + } + + /* Ring state is always the first page. Set in library for user cq. */ + if (cq->is_kernel) + cq->ring_state = cq->pdir.pages[0]; + else + pvrdma_page_dir_insert_umem(&cq->pdir, cq->umem, 0); + + atomic_set(&cq->refcnt, 1); + init_waitqueue_head(&cq->wait); + spin_lock_init(&cq->cq_lock); + + memset(cmd, 0, sizeof(*cmd)); + cmd->hdr.cmd = PVRDMA_CMD_CREATE_CQ; + cmd->nchunks = npages; + cmd->ctx_handle = (context) ? + (u64)to_vucontext(context)->ctx_handle : 0; + cmd->cqe = entries; + cmd->pdir_dma = cq->pdir.dir_dma; + ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_CQ_RESP); + if (ret < 0) { + dev_warn(&dev->pdev->dev, + "could not create completion queue, error: %d\n", ret); + goto err_page_dir; + } + + cq->ibcq.cqe = resp->cqe; + cq->cq_handle = resp->cq_handle; + spin_lock_irqsave(&dev->cq_tbl_lock, flags); + dev->cq_tbl[cq->cq_handle % dev->dsr->caps.max_cq] = cq; + spin_unlock_irqrestore(&dev->cq_tbl_lock, flags); + + if (context) { + cq->uar = &(to_vucontext(context)->uar); + + /* Copy udata back. */ + if (ib_copy_to_udata(udata, &cq->cq_handle, sizeof(__u32))) { + dev_warn(&dev->pdev->dev, + "failed to copy back udata\n"); + pvrdma_destroy_cq(&cq->ibcq); + return ERR_PTR(-EINVAL); + } + } + + return &cq->ibcq; + +err_page_dir: + pvrdma_page_dir_cleanup(dev, &cq->pdir); +err_umem: + if (context) + ib_umem_release(cq->umem); +err_cq: + atomic_dec(&dev->num_cqs); + kfree(cq); + + return ERR_PTR(ret); +} + +static void pvrdma_free_cq(struct pvrdma_dev *dev, struct pvrdma_cq *cq) +{ + atomic_dec(&cq->refcnt); + wait_event(cq->wait, !atomic_read(&cq->refcnt)); + + if (!cq->is_kernel) + ib_umem_release(cq->umem); + + pvrdma_page_dir_cleanup(dev, &cq->pdir); + kfree(cq); +} + +/** + * pvrdma_destroy_cq - destroy completion queue + * @cq: the completion queue to destroy. + * + * @return: 0 for success. + */ +int pvrdma_destroy_cq(struct ib_cq *cq) +{ + struct pvrdma_cq *vcq = to_vcq(cq); + union pvrdma_cmd_req req; + struct pvrdma_cmd_destroy_cq *cmd = &req.destroy_cq; + struct pvrdma_dev *dev = to_vdev(cq->device); + unsigned long flags; + int ret; + + memset(cmd, 0, sizeof(*cmd)); + cmd->hdr.cmd = PVRDMA_CMD_DESTROY_CQ; + cmd->cq_handle = vcq->cq_handle; + + ret = pvrdma_cmd_post(dev, &req, NULL, 0); + if (ret < 0) + dev_warn(&dev->pdev->dev, + "could not destroy completion queue, error: %d\n", + ret); + + /* free cq's resources */ + spin_lock_irqsave(&dev->cq_tbl_lock, flags); + dev->cq_tbl[vcq->cq_handle] = NULL; + spin_unlock_irqrestore(&dev->cq_tbl_lock, flags); + + pvrdma_free_cq(dev, vcq); + atomic_dec(&dev->num_cqs); + + return ret; +} + +/** + * pvrdma_modify_cq - modify the CQ moderation parameters + * @ibcq: the CQ to modify + * @cq_count: number of CQEs that will trigger an event + * @cq_period: max period of time in usec before triggering an event + * + * @return: -EOPNOTSUPP as CQ resize is not supported. + */ +int pvrdma_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period) +{ + return -EOPNOTSUPP; +} + +static inline struct pvrdma_cqe *get_cqe(struct pvrdma_cq *cq, int i) +{ + return (struct pvrdma_cqe *)pvrdma_page_dir_get_ptr( + &cq->pdir, + cq->offset + + sizeof(struct pvrdma_cqe) * i); +} + +void _pvrdma_flush_cqe(struct pvrdma_qp *qp, struct pvrdma_cq *cq) +{ + int head; + int has_data; + + if (!cq->is_kernel) + return; + + /* Lock held */ + has_data = pvrdma_idx_ring_has_data(&cq->ring_state->rx, + cq->ibcq.cqe, &head); + if (unlikely(has_data > 0)) { + int items; + int curr; + int tail = pvrdma_idx(&cq->ring_state->rx.prod_tail, + cq->ibcq.cqe); + struct pvrdma_cqe *cqe; + struct pvrdma_cqe *curr_cqe; + + items = (tail > head) ? (tail - head) : + (cq->ibcq.cqe - head + tail); + curr = --tail; + while (items-- > 0) { + if (curr < 0) + curr = cq->ibcq.cqe - 1; + if (tail < 0) + tail = cq->ibcq.cqe - 1; + curr_cqe = get_cqe(cq, curr); + if ((curr_cqe->qp & 0xFFFF) != qp->qp_handle) { + if (curr != tail) { + cqe = get_cqe(cq, tail); + *cqe = *curr_cqe; + } + tail--; + } else { + pvrdma_idx_ring_inc( + &cq->ring_state->rx.cons_head, + cq->ibcq.cqe); + } + curr--; + } + } +} + +static int pvrdma_poll_one(struct pvrdma_cq *cq, struct pvrdma_qp **cur_qp, + struct ib_wc *wc) +{ + struct pvrdma_dev *dev = to_vdev(cq->ibcq.device); + int has_data; + unsigned int head; + bool tried = false; + struct pvrdma_cqe *cqe; + +retry: + has_data = pvrdma_idx_ring_has_data(&cq->ring_state->rx, + cq->ibcq.cqe, &head); + if (has_data == 0) { + if (tried) + return -EAGAIN; + + pvrdma_write_uar_cq(dev, cq->cq_handle | PVRDMA_UAR_CQ_POLL); + + tried = true; + goto retry; + } else if (has_data == PVRDMA_INVALID_IDX) { + dev_err(&dev->pdev->dev, "CQ ring state invalid\n"); + return -EAGAIN; + } + + cqe = get_cqe(cq, head); + + /* Ensure cqe is valid. */ + rmb(); + if (dev->qp_tbl[cqe->qp & 0xffff]) + *cur_qp = (struct pvrdma_qp *)dev->qp_tbl[cqe->qp & 0xffff]; + else + return -EAGAIN; + + wc->opcode = pvrdma_wc_opcode_to_ib(cqe->opcode); + wc->status = pvrdma_wc_status_to_ib(cqe->status); + wc->wr_id = cqe->wr_id; + wc->qp = &(*cur_qp)->ibqp; + wc->byte_len = cqe->byte_len; + wc->ex.imm_data = cqe->imm_data; + wc->src_qp = cqe->src_qp; + wc->wc_flags = pvrdma_wc_flags_to_ib(cqe->wc_flags); + wc->pkey_index = cqe->pkey_index; + wc->slid = cqe->slid; + wc->sl = cqe->sl; + wc->dlid_path_bits = cqe->dlid_path_bits; + wc->port_num = cqe->port_num; + wc->vendor_err = 0; + + /* Update shared ring state */ + pvrdma_idx_ring_inc(&cq->ring_state->rx.cons_head, cq->ibcq.cqe); + + return 0; +} + +/** + * pvrdma_poll_cq - poll for work completion queue entries + * @ibcq: completion queue + * @num_entries: the maximum number of entries + * @entry: pointer to work completion array + * + * @return: number of polled completion entries + */ +int pvrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) +{ + struct pvrdma_cq *cq = to_vcq(ibcq); + struct pvrdma_qp *cur_qp = NULL; + unsigned long flags; + int npolled; + + if (num_entries < 1 || wc == NULL) + return 0; + + spin_lock_irqsave(&cq->cq_lock, flags); + for (npolled = 0; npolled < num_entries; ++npolled) { + if (pvrdma_poll_one(cq, &cur_qp, wc + npolled)) + break; + } + + spin_unlock_irqrestore(&cq->cq_lock, flags); + + /* Ensure we do not return errors from poll_cq */ + return npolled; +} + +/** + * pvrdma_resize_cq - resize CQ + * @ibcq: the completion queue + * @entries: CQ entries + * @udata: user data + * + * @return: -EOPNOTSUPP as CQ resize is not supported. + */ +int pvrdma_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata) +{ + return -EOPNOTSUPP; +}