From patchwork Mon Sep 12 04:49:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9325693 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 C82696048B for ; Mon, 12 Sep 2016 04:51:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE16F28ACC for ; Mon, 12 Sep 2016 04:51:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B289B28B06; Mon, 12 Sep 2016 04:51:22 +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=unavailable 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 C6E0528AD7 for ; Mon, 12 Sep 2016 04:51:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932613AbcILEuw (ORCPT ); Mon, 12 Sep 2016 00:50:52 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:38044 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932354AbcILEuj (ORCPT ); Mon, 12 Sep 2016 00:50:39 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Sun, 11 Sep 2016 21:50:23 -0700 Received: from EX13-CAS-005.vmware.com (ex13-cas-005.vmware.com [10.113.191.55]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id CB5D2404FA; Sun, 11 Sep 2016 21:50:34 -0700 (PDT) Received: from EX13-CAS-001.vmware.com (10.113.191.51) by EX13-MBX-018.vmware.com (10.113.191.38) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Sun, 11 Sep 2016 21:50:34 -0700 Received: from NAM03-BY2-obe.outbound.protection.outlook.com (10.113.170.11) by EX13-CAS-001.vmware.com (10.113.191.51) with Microsoft SMTP Server (TLS) id 15.0.1156.6 via Frontend Transport; Sun, 11 Sep 2016 21:50:34 -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=c6acTn39Q4bIeH3OOcOew3TJVQpVIuvoAyoyXLQUcZ4=; b=FjgBvemOy3FEVsd3NboAQsoCPPim3DjbLtELIzOY4wqEvJ5n31/S0s9hj+G8pp+6+1pht2hQqeHnlOsShxkxHCQlpHF9NCZ/TJkapuR30ZkuEzoKjqi8qUSZ5XIT3i1INwK5lgckUzfF6HM0gXqFR3vUWDOyKdnvmoNOOyJYZTo= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none; redhat.com; dmarc=none action=none header.from=vmware.com; Received: from promb-2s-dhcp95-136.eng.vmware.com (208.91.1.34) by BLUPR0501MB836.namprd05.prod.outlook.com (10.141.251.150) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.619.3; Mon, 12 Sep 2016 04:50:32 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , , , Subject: [PATCH v4 09/16] IB/pvrdma: Add support for Completion Queues Date: Sun, 11 Sep 2016 21:49:19 -0700 Message-ID: <1473655766-31628-10-git-send-email-aditr@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1473655766-31628-1-git-send-email-aditr@vmware.com> References: <1473655766-31628-1-git-send-email-aditr@vmware.com> MIME-Version: 1.0 X-Originating-IP: [208.91.1.34] X-ClientProxiedBy: BY2PR04CA052.namprd04.prod.outlook.com (10.141.249.170) To BLUPR0501MB836.namprd05.prod.outlook.com (10.141.251.150) X-MS-Office365-Filtering-Correlation-Id: 9f15afa4-f726-4e0a-c1f1-08d3dac8545b X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 2:hVTPZ4/flsBbrAj0VJEZZ+BCuMNpGXE5rp8D98KffCVbjXWnba4JMBNS/rYQYKAgxHZAWMmsZXjCny2t06XvMTrEtde8rq6Cw9dG9vLHWKzaSLyq+LKHZuuTphjXEPpdcaUIb2tJFbj6YUZB5C3ysqz5DBIjzc9L4HjZys31cneHxsL/lUV5TDNMudZ1XFYF; 3:FMne/KNd/wqWmKnnGDUJxTUgJyMNXDJU4/K3+UQbWxI95/uA0goY3JoSAXKhG7VJoFyYg0sIVyeTKT/zKXvfG9EYyqAWFV5cTFWQhNJ1kWGHa5e6iY2JAfuhQkmD+8B7 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BLUPR0501MB836; X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 25:g3MHcJS2oM/KgMamb+KgrRFqDEuaeDdbEpV00eLSRyatVjluzU2Mr8Wf3x5mFfS05DeyrZLgcnRJThDIX7lqwNZKQi1waEaIU2XXt1idIDP8u207G0cIYF1TP/i8Jsf+hTepF6MrCh992S6D+IlXV1Q/YlQtQjrl0lleNv3NsoMiMctDRZvTvP8xTNeRQZfeRFYpNsXkqxf6jX7pY7cH1qT5CJj4ZwxXx09SwORuB1mr306QXbKrmfVI7T0tjELyZtmSkWKjqlhXQXa899r4yVjgHqpym+/wISTF14CzmDIbexID4wcq18iWjkHqomS9TFRwuR+7KZkYN+y/+RMppdMqfG9dVbJbJi6UFJs7UAYXWFVuyNcWtrEtyQwp/lwChJPsiPKfANFnOJrvmYePJrrraPYToHH4/HTFVDFPAzUt74VP6kvkXfXA7Ntrgh9Pg5cs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GwBpOApAhvpo0drhV4e9wrFZqIiW9paHgiDhe4hfLfAaLlLWoA6bZ2L+GQoxgaJg9//svlsQVMXMxC8AFSC0Dj1FONBi8n1ZtX9zxnKJ1ns5PUKxVZr1zTJxwBxkmeHY3434Fk2MBfuQrcOGmPjj+wHyAO7DXaKtbU19uA X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(61668805478150)(22074186197030); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046); SRVR:BLUPR0501MB836; BCL:0; PCL:0; RULEID:; SRVR:BLUPR0501MB836; X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 4:bIK/1O/iFdV+SmmYZZSmwc5wayXs/rK+bXc1q8kB6o4QpLv6xwrF3Mnhh5aOks2BPyzm6QS2gFMV2RbRtsK/NxnFfItpwU59HefrHDsFMWPHDFnJeWdzoeyQLQUU9BnmyKph+sp70Tw2P2TFCQFBWyu6Ph3s4HfVv79EVJSmirssfHVQtSboPfwHa+e9rfyG9Dynx8kgasJmVrwl4IuYowPLDdApfULVhTTYZ/1rciysWqzw2MGgnOrMgwaMfByhdZLxs7/5Z9umCbFpYqVYSTfl3YikHigw3nLbdElk5qL7lRm8JYZJIVhF2TxlXi340qs6QwizcGmBxbeVywSf68c9+lT2qIH/TaK9uVyJKVw7abtIh/aeaAwGRlI68Kz9adng74Sh6ESNroNI7I9n5shkmJM7vDoij+mHRBknp9/JtOCok2m2OA3Bf5echZ4QgmxdoSujf2Mjtv80RL+xbuTEhATPOu4PNTJWIhKqaZffJOVPAkiHz7+v+hIdr8tz X-Forefront-PRVS: 006339698F X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(66066001)(107886002)(4001430100002)(5003940100001)(36756003)(47776003)(189998001)(4001450100002)(87386001)(86362001)(48376002)(2201001)(50466002)(2906002)(50226002)(8676002)(81166006)(586003)(4326007)(7846002)(5660300001)(305945005)(42186005)(76176999)(7736002)(50986999)(6116002)(3846002)(15975445007)(4001520100001)(5001770100001)(19580405001)(33646002)(92566002)(19580395003)(2950100001)(77096005)(229853001)(7099028)(5001760100003)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:BLUPR0501MB836; H:promb-2s-dhcp95-136.eng.vmware.com; FPR:; SPF:None; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BLUPR0501MB836; 23:trtr2P5DVvCgqVi2QOyyOe8CCAVKqun2qt653L/4?= =?us-ascii?Q?yegsQ8KxjxfGHiSwZzYom6HU7IAQtWuIdHO4vj7BUTO1SfIkZmU/em+IqPjE?= =?us-ascii?Q?3CoCRfPEtwH8DYXOXhVsT60ZcCBENOeK44WUJyWodwpDpWxMfqcx8iTa4P8J?= =?us-ascii?Q?EGmGHlvl+x8h4l0189J5Dxc1tQ7FBC7/Fdnz9Mc3cuVie606nS2iwenbDNYS?= =?us-ascii?Q?iVfHDheo2wJ5ktqI+Ftwc2mQNMFQZM5EQ9XUXh2ixVrYTAgQrkidD19fvLLB?= =?us-ascii?Q?UtBUd4T5jMnQhf1nXlm8mKhqwQq/Lk91d08V/yUGcw7x2m12XAt7A/GiTK5H?= =?us-ascii?Q?5HdpbXoISJhm6Y/CUPj8JsatDbV61Z5Q0vSAgcKHyogmkTsDixzbBK1DaKtg?= =?us-ascii?Q?Xjm/7OSQxqMsjtogmnXdjBDj6EfUBrx4NRZo/9I/gKYSeASPnec18Q4AtXUa?= =?us-ascii?Q?5wUhHn4aAOt2cUh2i/oYsALld4CoFwAq9sZVkRrfrgYtSfi0mlPDZgvdvjb/?= =?us-ascii?Q?oukm65UoHYSg/iKAaScGr6L6mHEkAdoQz9Iy+PO3u3BQcN9lFfSHnC0aer52?= =?us-ascii?Q?5Tfc2LBQ5NuSSePQbnRdYoNBrM/REIfuQdz5N1twqtMfRVYXNvFnBHiEFWKb?= =?us-ascii?Q?x9aML7rDPk6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vtC200hOwSetFcSGap6IHrr1qtrCg2xEc9YIYN6xaM3K2NBiX3Q==; 24:ECmHd7eDtRKw6L3zr50sF2XtZfB26UV6Wfk2JLIkg7WDLZZ7NIkxr6sSViuZhEAZeXYTcZlyxoZdiwN7yj93LbBBwpcIGvMvJa0LnlwvGso=; 7:5EAi8/L3NOYKwUe/Lj+YfMd1wne/LuI73y/EWvujGMfz+uLq1sUBDadyWQt1620Cu9P/oH64cGMnZ2Bkw+cL0mhgBP/xBM5afJ46cWmivD13avucZ4BPi1GVGH0V+Yltv8O3AS/zG/3wBVKfTpV8X/R8qLI57rVmLxoz/J7ZOPTtfRkjCjyInn1ZJICYYgNREhDLfarYGM/mGBrVNBFhH3GMFKoh1D8h44EYyUSeBl6MCt0HxwrrkX03KRifZWfo SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BLUPR0501MB836; 20:erMrT1S+WKAoVaLoDzVIiJ/mx1c2OZD1QlYAZEfJL5h/HGs5XMRU4vJj7MFLM6q73pQFnVJ4jycs3nkdvJApMkX3rGI/12bD51qrGAkzbAxT1fYU56Iin4Wu6kbDdfKQR0bLtZEJUG40JlXQUuvQyEYLP5I9ELc6ZuofxeAdYSE= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Sep 2016 04:50:32.0896 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLUPR0501MB836 Received-SPF: None (EX13-EDG-OU-001.vmware.com: aditr@vmware.com does not designate permitted sender hosts) 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: Jorgen Hansen Reviewed-by: George Zhang Reviewed-by: Aditya Sarwade Reviewed-by: Bryan Tan Signed-off-by: Adit Ranadive Reviewed-by: Yuval Shaia --- 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 | 428 +++++++++++++++++++++++++++++++ 1 file changed, 428 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..8144b20 --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_cq.c @@ -0,0 +1,428 @@ +/* + * 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" +#include "pvrdma_user.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(-EINVAL); + + 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; + } + + if (cq->is_kernel) { + /* Ring state is always the first page. */ + 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); + + if (ret < 0 || rsp.hdr.ack != PVRDMA_CMD_CREATE_CQ_RESP) { + dev_warn(&dev->pdev->dev, + "could not create completion queue\n"); + 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); + if (ret < 0) + dev_warn(&dev->pdev->dev, + "could not destroy completion queue\n"); + + /* 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; + + /* Pass down POLL to give physical HCA a chance to poll. */ + 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; +}