From patchwork Wed Jul 6 06:14:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adit Ranadive X-Patchwork-Id: 9215523 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 0DA33607D9 for ; Wed, 6 Jul 2016 06:31:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F068D286FA for ; Wed, 6 Jul 2016 06:31:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E5342286FD; Wed, 6 Jul 2016 06:31:26 +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 B28A9286FA for ; Wed, 6 Jul 2016 06:31:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750883AbcGFGbY (ORCPT ); Wed, 6 Jul 2016 02:31:24 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:53388 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750779AbcGFGbX (ORCPT ); Wed, 6 Jul 2016 02:31:23 -0400 Received: from sc9-mailhost2.vmware.com (10.113.161.72) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Tue, 5 Jul 2016 23:15:04 -0700 Received: from EX13-CAS-003.vmware.com (ex13-cas-003.vmware.com [10.113.191.53]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 1E53EB05A3; Tue, 5 Jul 2016 23:15:20 -0700 (PDT) Received: from EX13-CAS-001.vmware.com (10.113.191.51) by EX13-MBX-019.vmware.com (10.113.191.39) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Tue, 5 Jul 2016 23:15:19 -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; Tue, 5 Jul 2016 23:15:19 -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=jA1nOyK02pBSizvNtIFQ6Xdor+K2XKzCtGTlUrKlGHE=; b=P6ibDxcRByltjiAP4fN/V5NxdxnpOFomEGj3vnzu81Zj77YKe2Y9cZD3/hD1ECbHOEuy3FPsr001/RjP45L8KzW+kPEySs2dNfDJIfzNhXK7k8dpu61fbw6BCsM9WIEd3nwAPdAJ9hHrfO709DbvcQjjH9H7WNlQm7/4SdzKdzg= 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 SN2PR0501MB848.namprd05.prod.outlook.com (10.160.14.146) with Microsoft SMTP Server (TLS) id 15.1.528.16; Wed, 6 Jul 2016 06:15:14 +0000 From: Adit Ranadive To: , , CC: Adit Ranadive , , , , Subject: [PATCH v1 03/15] IB/pvrdma: Add support for Completion Queues Date: Tue, 5 Jul 2016 23:14:36 -0700 Message-ID: <1467785688-23229-4-git-send-email-aditr@vmware.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1467785688-23229-1-git-send-email-aditr@vmware.com> References: <1467785688-23229-1-git-send-email-aditr@vmware.com> MIME-Version: 1.0 X-Originating-IP: [208.91.1.34] X-ClientProxiedBy: CO2PR11CA0028.namprd11.prod.outlook.com (10.141.242.166) To SN2PR0501MB848.namprd05.prod.outlook.com (10.160.14.146) X-MS-Office365-Filtering-Correlation-Id: 6ceea17f-dd30-4b6b-cf84-08d3a564e57f X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 2:g2oD6HCB8tmcV3XKIC5dOZu6HptISOx2jAPevujSGNBTbfxv84uLglfSu6B4HFWUJpj/yp9kQmqxfUH6XDUD3TRfyWB7542aUR5pf0IfA7S++swlAcORU9J7CM3ndB01larZVk4RDRP34gOQTYGqp0hFRw0DeSqd38HDk1TuU7DEH6wxu85UyxX9M85vvOpL; 3:W9uD0ENyCrwYAmwyHGCiSXs8stcQZ9QVzIxKd8CiPYW7SivJ8x4TSzD5ifWxCvUYBoiPNwEH4A/MMSs07NwVRoqEWePBQw+LLs7WL/eshcUMEFTpKtVdVVLWXmqBOR9/ X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN2PR0501MB848; X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 25:LHQzCzwGoepXWIK9sKNIBBzS7hPSzO946n3LwwiMbUG1yTnrMzkpjz8fC3OV8Pg4ozVbI4ugA7ij3mB1CQXPUk3yN+xd44Gwcvah2xib3vsVYzD7PpQUUjz8cXursWXz2IvJIVpIA/BJoZBxlixbkJ2YMq2g1DYcCLaTGtAAJjWotF0Zrl0sH+ll3ERH579J9b65MPjUsilUyIqKN0UsXC35dfrmGh+7m6mKK7l0irdqWA7F846dG/06q0f1CFlN14gfbkMy6uSmboG+fYAca918+XPILhSasfMK14njHhn96wWuN0Ehye6/E85fAJs3QqGj2E/nr1XSRY0LF1jMRDQovsvmPRYh71XSfZdS5p1Kjbf1vKEX/ums6gvz9Q++kajNoLSEpNE1Aly9CdWiggb3ijVKdxF2S++vCf/ePHhJNY9zBPpCW4iqnRVkVlU2wqNf78PuTbOMaWYFkgJiRkXb/FQ4Ij5tRt8YeCWjwIjxFRHVa8KclweciaLY/MflGN7+YL9iAYLYhols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EU+DvgVC4RvdJTFROwTfP3I+cdbb/KF6qhbFGKLk2blOYp5PL6JMcKmIq1L0pAiQsA0mvDbbtwnZ5hjUE+bxvXpKf7XCc3qbzQZHgPDFm9qrfjSh7hJkePSze7FAdRg7nTuog1psqPQJyU5AH9Eej3H6aj650qUe179sIviyK2TDQv+BEbrPHHCoeOh7wNYFpY1Ad 1X9HQna3ngUINFn3osRcA0LQT/OAs2zDg Q= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(250305191791016)(61668805478150)(22074186197030); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001); SRVR:SN2PR0501MB848; BCL:0; PCL:0; RULEID:; SRVR:SN2PR0501MB848; X-Forefront-PRVS: 0995196AA2 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(7916002)(189002)(199003)(81166006)(81156014)(4001450100002)(92566002)(86362001)(19580395003)(4001430100002)(19580405001)(7846002)(7736002)(305945005)(8676002)(105586002)(2201001)(586003)(42186005)(33646002)(106356001)(3846002)(5003940100001)(6116002)(48376002)(36756003)(50466002)(5001770100001)(97736004)(189998001)(229853001)(47776003)(66066001)(76176999)(107886002)(50986999)(68736007)(101416001)(50226002)(4326007)(2950100001)(15975445007)(77096005)(2906002)(7099028)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:SN2PR0501MB848; 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-ascii?Q?1; SN2PR0501MB848; 23:Jh7s2jDZfzDw0TaxjhULI1qjKKPrYiS+tt6OlcJw?= =?us-ascii?Q?UNOGVbAms1dkK6xUtDUeTaAQrnz0jZD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n4Y1LFa23nPJJjBGjh14Qf7b1yLDLeYb16EpP4Q7u0l7oy4mksIl2TbkkKx8vTo9JBDFk+rIuBrb+GFz2yK/AsimiXZBNobUYpsMmZf8sYOSCUv8O+vuydQXCLJm03Wc863+vIrwgHN7AyWKlM2GDrIR+0PDwD4QlX+3Ne SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; SN2PR0501MB848; 20:lkGQjYx5QjDxLPF48YjvBbhB3QBr2j0CO09Z3mNCbrAzNmKrfk1JkIgNB4vQ5thkOoJBovsUEntNFwK2iwyZnZZDMg5FgE/G8s9MhRXsGs6CaHYQTvxmAD8jn0YfjoYyu76fziDKp7qnG0eqtN9XpZz0VfE8TN2p0Cm6x4QiSYo= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jul 2016 06:15:14.6876 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN2PR0501MB848 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 --- drivers/infiniband/hw/pvrdma/pvrdma_cq.c | 436 +++++++++++++++++++++++++++++++ 1 file changed, 436 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..9a4b42c --- /dev/null +++ b/drivers/infiniband/hw/pvrdma/pvrdma_cq.c @@ -0,0 +1,436 @@ +/* + * 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; + + writel(cpu_to_le32(val), dev->driver_uar.map + PVRDMA_UAR_CQ_OFFSET); + + 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, true, &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"); + ret = -EINVAL; + goto err_page_dir; + } + } + + 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; + union pvrdma_cmd_resp rsp; + 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, false, &rsp); + 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) { + u32 val; + + if (tried) + return -EAGAIN; + + /* Pass down POLL to give physical HCA a chance to poll. */ + val = cq->cq_handle | PVRDMA_UAR_CQ_POLL; + writel(cpu_to_le32(val), + dev->driver_uar.map + PVRDMA_UAR_CQ_OFFSET); + + tried = true; + goto retry; + } else if (has_data == -1) { + return -EINVAL; + } + + 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 -EINVAL; + + 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; + int ret; + + if (num_entries < 1) + return -EINVAL; + + spin_lock_irqsave(&cq->cq_lock, flags); + for (npolled = 0; npolled < num_entries; ++npolled) { + ret = pvrdma_poll_one(cq, &cur_qp, wc + npolled); + if (ret) + break; + } + + spin_unlock_irqrestore(&cq->cq_lock, flags); + + if ((ret == 0) || (ret == -EAGAIN)) + return npolled; + else + return ret; +} + +/** + * 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; +}