blob: 6cdbec13756af5ac7741202fcf38cc54ed5d6dbe [file] [log] [blame]
Devesh Sharma71ee6732015-07-24 05:03:59 +05301/* This file is part of the Emulex RoCE Device Driver for
2 * RoCE (RDMA over Converged Ethernet) adapters.
3 * Copyright (C) 2012-2015 Emulex. All rights reserved.
4 * EMULEX and SLI are trademarks of Emulex.
5 * www.emulex.com
6 *
7 * This software is available to you under a choice of one of two licenses.
8 * You may choose to be licensed under the terms of the GNU General Public
9 * License (GPL) Version 2, available from the file COPYING in the main
10 * directory of this source tree, or the BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * - Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Parav Panditfe2caef2012-03-21 04:09:06 +053034 *
35 * Contact Information:
36 * linux-drivers@emulex.com
37 *
38 * Emulex
39 * 3333 Susan Street
40 * Costa Mesa, CA 92626
Devesh Sharma71ee6732015-07-24 05:03:59 +053041 */
Parav Panditfe2caef2012-03-21 04:09:06 +053042
43#include <linux/dma-mapping.h>
44#include <rdma/ib_verbs.h>
45#include <rdma/ib_user_verbs.h>
46#include <rdma/iw_cm.h>
47#include <rdma/ib_umem.h>
48#include <rdma/ib_addr.h>
Somnath Koturcc369292015-07-30 18:33:31 +030049#include <rdma/ib_cache.h>
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +030050#include <rdma/uverbs_ioctl.h>
Parav Panditfe2caef2012-03-21 04:09:06 +053051
52#include "ocrdma.h"
53#include "ocrdma_hw.h"
54#include "ocrdma_verbs.h"
Leon Romanovskya7fe7382016-09-22 17:31:15 +030055#include <rdma/ocrdma-abi.h>
Parav Panditfe2caef2012-03-21 04:09:06 +053056
57int ocrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
58{
Gal Pressmanb1889402019-01-07 17:27:55 +020059 if (index > 0)
Parav Panditfe2caef2012-03-21 04:09:06 +053060 return -EINVAL;
61
62 *pkey = 0xffff;
63 return 0;
64}
65
Matan Barak2528e332015-06-11 16:35:25 +030066int ocrdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr,
67 struct ib_udata *uhw)
Parav Panditfe2caef2012-03-21 04:09:06 +053068{
69 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
70
Matan Barak2528e332015-06-11 16:35:25 +030071 if (uhw->inlen || uhw->outlen)
72 return -EINVAL;
73
Parav Panditfe2caef2012-03-21 04:09:06 +053074 memset(attr, 0, sizeof *attr);
75 memcpy(&attr->fw_ver, &dev->attr.fw_ver[0],
76 min(sizeof(dev->attr.fw_ver), sizeof(attr->fw_ver)));
77 ocrdma_get_guid(dev, (u8 *)&attr->sys_image_guid);
Mitesh Ahuja033edd42014-06-10 19:32:22 +053078 attr->max_mr_size = dev->attr.max_mr_size;
Parav Panditfe2caef2012-03-21 04:09:06 +053079 attr->page_size_cap = 0xffff000;
80 attr->vendor_id = dev->nic_info.pdev->vendor;
81 attr->vendor_part_id = dev->nic_info.pdev->device;
Mitesh Ahuja96c51ab2014-07-02 11:36:06 +053082 attr->hw_ver = dev->asic_id;
Parav Panditfe2caef2012-03-21 04:09:06 +053083 attr->max_qp = dev->attr.max_qp;
Naresh Gottumukkalad3cb6c02013-08-26 15:27:40 +053084 attr->max_ah = OCRDMA_MAX_AH;
Parav Panditfe2caef2012-03-21 04:09:06 +053085 attr->max_qp_wr = dev->attr.max_wqe;
86
87 attr->device_cap_flags = IB_DEVICE_CURR_QP_STATE_MOD |
88 IB_DEVICE_RC_RNR_NAK_GEN |
89 IB_DEVICE_SHUTDOWN_PORT |
90 IB_DEVICE_SYS_IMAGE_GUID |
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +053091 IB_DEVICE_LOCAL_DMA_LKEY |
92 IB_DEVICE_MEM_MGT_EXTENSIONS;
Steve Wise33023fb2018-06-18 08:05:26 -070093 attr->max_send_sge = dev->attr.max_send_sge;
94 attr->max_recv_sge = dev->attr.max_recv_sge;
Selvin Xavier3c199b42016-08-24 01:17:41 -040095 attr->max_sge_rd = dev->attr.max_rdma_sge;
Parav Panditfe2caef2012-03-21 04:09:06 +053096 attr->max_cq = dev->attr.max_cq;
97 attr->max_cqe = dev->attr.max_cqe;
98 attr->max_mr = dev->attr.max_mr;
Selvin Xavierac578ae2014-02-04 11:57:04 +053099 attr->max_mw = dev->attr.max_mw;
Parav Panditfe2caef2012-03-21 04:09:06 +0530100 attr->max_pd = dev->attr.max_pd;
101 attr->atomic_cap = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530102 attr->max_qp_rd_atom =
103 min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp);
104 attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +0530105 attr->max_srq = dev->attr.max_srq;
Roland Dreierd1e09eb2012-07-07 15:13:47 -0700106 attr->max_srq_sge = dev->attr.max_srq_sge;
Parav Panditfe2caef2012-03-21 04:09:06 +0530107 attr->max_srq_wr = dev->attr.max_rqe;
108 attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay;
Devesh Sharmad6a488f2014-06-09 10:52:37 +0530109 attr->max_fast_reg_page_list_len = dev->attr.max_pages_per_frmr;
Parav Panditfe2caef2012-03-21 04:09:06 +0530110 attr->max_pkeys = 1;
111 return 0;
112}
113
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530114static inline void get_link_speed_and_width(struct ocrdma_dev *dev,
115 u8 *ib_speed, u8 *ib_width)
116{
117 int status;
118 u8 speed;
119
Devesh Sharma3b1ea432015-12-24 13:14:07 -0500120 status = ocrdma_mbx_get_link_speed(dev, &speed, NULL);
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530121 if (status)
122 speed = OCRDMA_PHYS_LINK_SPEED_ZERO;
123
124 switch (speed) {
125 case OCRDMA_PHYS_LINK_SPEED_1GBPS:
126 *ib_speed = IB_SPEED_SDR;
127 *ib_width = IB_WIDTH_1X;
128 break;
129
130 case OCRDMA_PHYS_LINK_SPEED_10GBPS:
131 *ib_speed = IB_SPEED_QDR;
132 *ib_width = IB_WIDTH_1X;
133 break;
134
135 case OCRDMA_PHYS_LINK_SPEED_20GBPS:
136 *ib_speed = IB_SPEED_DDR;
137 *ib_width = IB_WIDTH_4X;
138 break;
139
140 case OCRDMA_PHYS_LINK_SPEED_40GBPS:
141 *ib_speed = IB_SPEED_QDR;
142 *ib_width = IB_WIDTH_4X;
143 break;
144
145 default:
146 /* Unsupported */
147 *ib_speed = IB_SPEED_SDR;
148 *ib_width = IB_WIDTH_1X;
Joe Perches2b50176d2013-10-08 16:07:22 -0700149 }
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530150}
151
Parav Panditfe2caef2012-03-21 04:09:06 +0530152int ocrdma_query_port(struct ib_device *ibdev,
153 u8 port, struct ib_port_attr *props)
154{
155 enum ib_port_state port_state;
156 struct ocrdma_dev *dev;
157 struct net_device *netdev;
158
Or Gerlitzc4550c62017-01-24 13:02:39 +0200159 /* props being zeroed by the caller, avoid zeroing it here */
Parav Panditfe2caef2012-03-21 04:09:06 +0530160 dev = get_ocrdma_dev(ibdev);
Parav Panditfe2caef2012-03-21 04:09:06 +0530161 netdev = dev->nic_info.netdev;
162 if (netif_running(netdev) && netif_oper_up(netdev)) {
163 port_state = IB_PORT_ACTIVE;
Kamal Heib72a77202019-08-07 13:31:35 +0300164 props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
Parav Panditfe2caef2012-03-21 04:09:06 +0530165 } else {
166 port_state = IB_PORT_DOWN;
Kamal Heib72a77202019-08-07 13:31:35 +0300167 props->phys_state = IB_PORT_PHYS_STATE_DISABLED;
Parav Panditfe2caef2012-03-21 04:09:06 +0530168 }
169 props->max_mtu = IB_MTU_4096;
170 props->active_mtu = iboe_get_mtu(netdev->mtu);
171 props->lid = 0;
172 props->lmc = 0;
173 props->sm_lid = 0;
174 props->sm_sl = 0;
175 props->state = port_state;
Jason Gunthorpe2f944c02018-07-04 15:57:48 +0300176 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
177 IB_PORT_DEVICE_MGMT_SUP |
178 IB_PORT_VENDOR_CLASS_SUP;
179 props->ip_gids = true;
Parav Panditfe2caef2012-03-21 04:09:06 +0530180 props->gid_tbl_len = OCRDMA_MAX_SGID;
181 props->pkey_tbl_len = 1;
182 props->bad_pkey_cntr = 0;
183 props->qkey_viol_cntr = 0;
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530184 get_link_speed_and_width(dev, &props->active_speed,
185 &props->active_width);
Parav Panditfe2caef2012-03-21 04:09:06 +0530186 props->max_msg_sz = 0x80000000;
187 props->max_vl_num = 4;
188 return 0;
189}
190
Parav Panditfe2caef2012-03-21 04:09:06 +0530191static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
192 unsigned long len)
193{
194 struct ocrdma_mm *mm;
195
196 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
197 if (mm == NULL)
198 return -ENOMEM;
199 mm->key.phy_addr = phy_addr;
200 mm->key.len = len;
201 INIT_LIST_HEAD(&mm->entry);
202
203 mutex_lock(&uctx->mm_list_lock);
204 list_add_tail(&mm->entry, &uctx->mm_head);
205 mutex_unlock(&uctx->mm_list_lock);
206 return 0;
207}
208
209static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
210 unsigned long len)
211{
212 struct ocrdma_mm *mm, *tmp;
213
214 mutex_lock(&uctx->mm_list_lock);
215 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530216 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
Parav Panditfe2caef2012-03-21 04:09:06 +0530217 continue;
218
219 list_del(&mm->entry);
220 kfree(mm);
221 break;
222 }
223 mutex_unlock(&uctx->mm_list_lock);
224}
225
226static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
227 unsigned long len)
228{
229 bool found = false;
230 struct ocrdma_mm *mm;
231
232 mutex_lock(&uctx->mm_list_lock);
233 list_for_each_entry(mm, &uctx->mm_head, entry) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530234 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
Parav Panditfe2caef2012-03-21 04:09:06 +0530235 continue;
236
237 found = true;
238 break;
239 }
240 mutex_unlock(&uctx->mm_list_lock);
241 return found;
242}
243
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530244
245static u16 _ocrdma_pd_mgr_get_bitmap(struct ocrdma_dev *dev, bool dpp_pool)
246{
247 u16 pd_bitmap_idx = 0;
248 const unsigned long *pd_bitmap;
249
250 if (dpp_pool) {
251 pd_bitmap = dev->pd_mgr->pd_dpp_bitmap;
252 pd_bitmap_idx = find_first_zero_bit(pd_bitmap,
253 dev->pd_mgr->max_dpp_pd);
254 __set_bit(pd_bitmap_idx, dev->pd_mgr->pd_dpp_bitmap);
255 dev->pd_mgr->pd_dpp_count++;
256 if (dev->pd_mgr->pd_dpp_count > dev->pd_mgr->pd_dpp_thrsh)
257 dev->pd_mgr->pd_dpp_thrsh = dev->pd_mgr->pd_dpp_count;
258 } else {
259 pd_bitmap = dev->pd_mgr->pd_norm_bitmap;
260 pd_bitmap_idx = find_first_zero_bit(pd_bitmap,
261 dev->pd_mgr->max_normal_pd);
262 __set_bit(pd_bitmap_idx, dev->pd_mgr->pd_norm_bitmap);
263 dev->pd_mgr->pd_norm_count++;
264 if (dev->pd_mgr->pd_norm_count > dev->pd_mgr->pd_norm_thrsh)
265 dev->pd_mgr->pd_norm_thrsh = dev->pd_mgr->pd_norm_count;
266 }
267 return pd_bitmap_idx;
268}
269
270static int _ocrdma_pd_mgr_put_bitmap(struct ocrdma_dev *dev, u16 pd_id,
271 bool dpp_pool)
272{
273 u16 pd_count;
274 u16 pd_bit_index;
275
276 pd_count = dpp_pool ? dev->pd_mgr->pd_dpp_count :
277 dev->pd_mgr->pd_norm_count;
278 if (pd_count == 0)
279 return -EINVAL;
280
281 if (dpp_pool) {
282 pd_bit_index = pd_id - dev->pd_mgr->pd_dpp_start;
283 if (pd_bit_index >= dev->pd_mgr->max_dpp_pd) {
284 return -EINVAL;
285 } else {
286 __clear_bit(pd_bit_index, dev->pd_mgr->pd_dpp_bitmap);
287 dev->pd_mgr->pd_dpp_count--;
288 }
289 } else {
290 pd_bit_index = pd_id - dev->pd_mgr->pd_norm_start;
291 if (pd_bit_index >= dev->pd_mgr->max_normal_pd) {
292 return -EINVAL;
293 } else {
294 __clear_bit(pd_bit_index, dev->pd_mgr->pd_norm_bitmap);
295 dev->pd_mgr->pd_norm_count--;
296 }
297 }
298
299 return 0;
300}
301
Dan Carpenter004d18e2017-02-23 13:40:16 +0300302static int ocrdma_put_pd_num(struct ocrdma_dev *dev, u16 pd_id,
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530303 bool dpp_pool)
304{
305 int status;
306
307 mutex_lock(&dev->dev_lock);
308 status = _ocrdma_pd_mgr_put_bitmap(dev, pd_id, dpp_pool);
309 mutex_unlock(&dev->dev_lock);
310 return status;
311}
312
313static int ocrdma_get_pd_num(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
314{
315 u16 pd_idx = 0;
316 int status = 0;
317
318 mutex_lock(&dev->dev_lock);
319 if (pd->dpp_enabled) {
320 /* try allocating DPP PD, if not available then normal PD */
321 if (dev->pd_mgr->pd_dpp_count < dev->pd_mgr->max_dpp_pd) {
322 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, true);
323 pd->id = dev->pd_mgr->pd_dpp_start + pd_idx;
324 pd->dpp_page = dev->pd_mgr->dpp_page_index + pd_idx;
325 } else if (dev->pd_mgr->pd_norm_count <
326 dev->pd_mgr->max_normal_pd) {
327 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, false);
328 pd->id = dev->pd_mgr->pd_norm_start + pd_idx;
329 pd->dpp_enabled = false;
330 } else {
331 status = -EINVAL;
332 }
333 } else {
334 if (dev->pd_mgr->pd_norm_count < dev->pd_mgr->max_normal_pd) {
335 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, false);
336 pd->id = dev->pd_mgr->pd_norm_start + pd_idx;
337 } else {
338 status = -EINVAL;
339 }
340 }
341 mutex_unlock(&dev->dev_lock);
342 return status;
343}
344
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300345/*
346 * NOTE:
347 *
348 * ocrdma_ucontext must be used here because this function is also
349 * called from ocrdma_alloc_ucontext where ib_udata does not have
350 * valid ib_ucontext pointer. ib_uverbs_get_context does not call
351 * uobj_{alloc|get_xxx} helpers which are used to store the
352 * ib_ucontext in uverbs_attr_bundle wrapping the ib_udata. so
353 * ib_udata does NOT imply valid ib_ucontext here!
354 */
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200355static int _ocrdma_alloc_pd(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
356 struct ocrdma_ucontext *uctx,
357 struct ib_udata *udata)
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530358{
Markus Elfring0ca4c392015-12-26 18:18:18 +0100359 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530360
Mitesh Ahuja59582d82015-05-19 11:32:37 +0530361 if (udata && uctx && dev->attr.max_dpp_pds) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530362 pd->dpp_enabled =
Devesh Sharma21c33912014-02-04 11:56:56 +0530363 ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530364 pd->num_dpp_qp =
Devesh Sharmaa53d77a2014-06-10 19:32:17 +0530365 pd->dpp_enabled ? (dev->nic_info.db_page_size /
366 dev->attr.wqe_size) : 0;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530367 }
368
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200369 if (dev->pd_mgr->pd_prealloc_valid)
370 return ocrdma_get_pd_num(dev, pd);
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530371
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530372retry:
373 status = ocrdma_mbx_alloc_pd(dev, pd);
374 if (status) {
375 if (pd->dpp_enabled) {
376 pd->dpp_enabled = false;
377 pd->num_dpp_qp = 0;
378 goto retry;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530379 }
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200380 return status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530381 }
382
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200383 return 0;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530384}
385
386static inline int is_ucontext_pd(struct ocrdma_ucontext *uctx,
387 struct ocrdma_pd *pd)
388{
Andrew F. Davis8b0c05d2017-12-05 13:15:53 -0600389 return (uctx->cntxt_pd == pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530390}
391
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200392static void _ocrdma_dealloc_pd(struct ocrdma_dev *dev,
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530393 struct ocrdma_pd *pd)
394{
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530395 if (dev->pd_mgr->pd_prealloc_valid)
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200396 ocrdma_put_pd_num(dev, pd->id, pd->dpp_enabled);
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530397 else
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200398 ocrdma_mbx_dealloc_pd(dev, pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530399}
400
401static int ocrdma_alloc_ucontext_pd(struct ocrdma_dev *dev,
402 struct ocrdma_ucontext *uctx,
403 struct ib_udata *udata)
404{
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200405 struct ib_device *ibdev = &dev->ibdev;
406 struct ib_pd *pd;
407 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530408
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200409 pd = rdma_zalloc_drv_obj(ibdev, ib_pd);
410 if (!pd)
411 return -ENOMEM;
412
413 pd->device = ibdev;
414 uctx->cntxt_pd = get_ocrdma_pd(pd);
415
416 status = _ocrdma_alloc_pd(dev, uctx->cntxt_pd, uctx, udata);
417 if (status) {
418 kfree(uctx->cntxt_pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530419 goto err;
420 }
421
422 uctx->cntxt_pd->uctx = uctx;
423 uctx->cntxt_pd->ibpd.device = &dev->ibdev;
424err:
425 return status;
426}
427
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200428static void ocrdma_dealloc_ucontext_pd(struct ocrdma_ucontext *uctx)
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530429{
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530430 struct ocrdma_pd *pd = uctx->cntxt_pd;
431 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
432
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530433 if (uctx->pd_in_use) {
434 pr_err("%s(%d) Freeing in use pdid=0x%x.\n",
435 __func__, dev->id, pd->id);
436 }
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200437 kfree(uctx->cntxt_pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530438 uctx->cntxt_pd = NULL;
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200439 _ocrdma_dealloc_pd(dev, pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530440}
441
442static struct ocrdma_pd *ocrdma_get_ucontext_pd(struct ocrdma_ucontext *uctx)
443{
444 struct ocrdma_pd *pd = NULL;
445
446 mutex_lock(&uctx->mm_list_lock);
447 if (!uctx->pd_in_use) {
448 uctx->pd_in_use = true;
449 pd = uctx->cntxt_pd;
450 }
451 mutex_unlock(&uctx->mm_list_lock);
452
453 return pd;
454}
455
456static void ocrdma_release_ucontext_pd(struct ocrdma_ucontext *uctx)
457{
458 mutex_lock(&uctx->mm_list_lock);
459 uctx->pd_in_use = false;
460 mutex_unlock(&uctx->mm_list_lock);
461}
462
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200463int ocrdma_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +0530464{
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200465 struct ib_device *ibdev = uctx->device;
Parav Panditfe2caef2012-03-21 04:09:06 +0530466 int status;
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200467 struct ocrdma_ucontext *ctx = get_ocrdma_ucontext(uctx);
468 struct ocrdma_alloc_ucontext_resp resp = {};
Parav Panditfe2caef2012-03-21 04:09:06 +0530469 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
470 struct pci_dev *pdev = dev->nic_info.pdev;
471 u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
472
473 if (!udata)
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200474 return -EFAULT;
Parav Panditfe2caef2012-03-21 04:09:06 +0530475 INIT_LIST_HEAD(&ctx->mm_head);
476 mutex_init(&ctx->mm_list_lock);
477
Luis Chamberlain750afb02019-01-04 09:23:09 +0100478 ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
479 &ctx->ah_tbl.pa, GFP_KERNEL);
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200480 if (!ctx->ah_tbl.va)
481 return -ENOMEM;
482
Parav Panditfe2caef2012-03-21 04:09:06 +0530483 ctx->ah_tbl.len = map_len;
484
485 resp.ah_tbl_len = ctx->ah_tbl.len;
Devesh Sharma1b76d382014-09-05 19:35:40 +0530486 resp.ah_tbl_page = virt_to_phys(ctx->ah_tbl.va);
Parav Panditfe2caef2012-03-21 04:09:06 +0530487
488 status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
489 if (status)
490 goto map_err;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530491
492 status = ocrdma_alloc_ucontext_pd(dev, ctx, udata);
493 if (status)
494 goto pd_err;
495
Parav Panditfe2caef2012-03-21 04:09:06 +0530496 resp.dev_id = dev->id;
497 resp.max_inline_data = dev->attr.max_inline_data;
498 resp.wqe_size = dev->attr.wqe_size;
499 resp.rqe_size = dev->attr.rqe_size;
500 resp.dpp_wqe_size = dev->attr.wqe_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530501
502 memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
503 status = ib_copy_to_udata(udata, &resp, sizeof(resp));
504 if (status)
505 goto cpy_err;
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200506 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530507
508cpy_err:
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200509 ocrdma_dealloc_ucontext_pd(ctx);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530510pd_err:
Parav Panditfe2caef2012-03-21 04:09:06 +0530511 ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
512map_err:
513 dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
514 ctx->ah_tbl.pa);
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200515 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530516}
517
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200518void ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
Parav Panditfe2caef2012-03-21 04:09:06 +0530519{
520 struct ocrdma_mm *mm, *tmp;
521 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530522 struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
523 struct pci_dev *pdev = dev->nic_info.pdev;
Parav Panditfe2caef2012-03-21 04:09:06 +0530524
Leon Romanovskya2a074e2019-02-12 20:39:16 +0200525 ocrdma_dealloc_ucontext_pd(uctx);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530526
Parav Panditfe2caef2012-03-21 04:09:06 +0530527 ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
528 dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
529 uctx->ah_tbl.pa);
530
531 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
532 list_del(&mm->entry);
533 kfree(mm);
534 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530535}
536
537int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
538{
539 struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530540 struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530541 unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
542 u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
543 unsigned long len = (vma->vm_end - vma->vm_start);
Markus Elfring0ca4c392015-12-26 18:18:18 +0100544 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530545 bool found;
546
547 if (vma->vm_start & (PAGE_SIZE - 1))
548 return -EINVAL;
549 found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
550 if (!found)
551 return -EINVAL;
552
553 if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
554 dev->nic_info.db_total_size)) &&
555 (len <= dev->nic_info.db_page_size)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530556 if (vma->vm_flags & VM_READ)
557 return -EPERM;
558
559 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Parav Panditfe2caef2012-03-21 04:09:06 +0530560 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
561 len, vma->vm_page_prot);
562 } else if (dev->nic_info.dpp_unmapped_len &&
563 (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
564 (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
565 dev->nic_info.dpp_unmapped_len)) &&
566 (len <= dev->nic_info.dpp_unmapped_len)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530567 if (vma->vm_flags & VM_READ)
568 return -EPERM;
569
Parav Panditfe2caef2012-03-21 04:09:06 +0530570 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
571 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
572 len, vma->vm_page_prot);
573 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +0530574 status = remap_pfn_range(vma, vma->vm_start,
575 vma->vm_pgoff, len, vma->vm_page_prot);
576 }
577 return status;
578}
579
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530580static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
Parav Panditfe2caef2012-03-21 04:09:06 +0530581 struct ib_udata *udata)
582{
583 int status;
584 u64 db_page_addr;
Roland Dreierda496432012-04-16 11:32:17 -0700585 u64 dpp_page_addr = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530586 u32 db_page_size;
587 struct ocrdma_alloc_pd_uresp rsp;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300588 struct ocrdma_ucontext *uctx = rdma_udata_to_drv_context(
589 udata, struct ocrdma_ucontext, ibucontext);
Parav Panditfe2caef2012-03-21 04:09:06 +0530590
Dan Carpenter63ea3742013-07-29 22:34:29 +0300591 memset(&rsp, 0, sizeof(rsp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530592 rsp.id = pd->id;
593 rsp.dpp_enabled = pd->dpp_enabled;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530594 db_page_addr = ocrdma_get_db_addr(dev, pd->id);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530595 db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530596
597 status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
598 if (status)
599 return status;
600
601 if (pd->dpp_enabled) {
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530602 dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530603 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530604 status = ocrdma_add_mmap(uctx, dpp_page_addr,
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530605 PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530606 if (status)
607 goto dpp_map_err;
608 rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
609 rsp.dpp_page_addr_lo = dpp_page_addr;
610 }
611
612 status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
613 if (status)
614 goto ucopy_err;
615
616 pd->uctx = uctx;
617 return 0;
618
619ucopy_err:
Roland Dreierda496432012-04-16 11:32:17 -0700620 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530621 ocrdma_del_mmap(pd->uctx, dpp_page_addr, PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530622dpp_map_err:
623 ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
624 return status;
625}
626
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300627int ocrdma_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +0530628{
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200629 struct ib_device *ibdev = ibpd->device;
Parav Panditfe2caef2012-03-21 04:09:06 +0530630 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
631 struct ocrdma_pd *pd;
632 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530633 u8 is_uctx_pd = false;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300634 struct ocrdma_ucontext *uctx = rdma_udata_to_drv_context(
635 udata, struct ocrdma_ucontext, ibucontext);
Parav Panditfe2caef2012-03-21 04:09:06 +0530636
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300637 if (udata) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530638 pd = ocrdma_get_ucontext_pd(uctx);
639 if (pd) {
640 is_uctx_pd = true;
641 goto pd_mapping;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530642 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530643 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530644
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200645 pd = get_ocrdma_pd(ibpd);
646 status = _ocrdma_alloc_pd(dev, pd, uctx, udata);
647 if (status)
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530648 goto exit;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530649
650pd_mapping:
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300651 if (udata) {
652 status = ocrdma_copy_pd_uresp(dev, pd, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +0530653 if (status)
654 goto err;
655 }
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200656 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530657
658err:
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200659 if (is_uctx_pd)
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530660 ocrdma_release_ucontext_pd(uctx);
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200661 else
662 _ocrdma_dealloc_pd(dev, pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530663exit:
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200664 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530665}
666
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +0300667void ocrdma_dealloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +0530668{
669 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530670 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530671 struct ocrdma_ucontext *uctx = NULL;
Parav Panditfe2caef2012-03-21 04:09:06 +0530672 u64 usr_db;
673
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530674 uctx = pd->uctx;
675 if (uctx) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530676 u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530677 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530678 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530679 ocrdma_del_mmap(pd->uctx, dpp_db, PAGE_SIZE);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530680 usr_db = ocrdma_get_db_addr(dev, pd->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530681 ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530682
683 if (is_ucontext_pd(uctx, pd)) {
684 ocrdma_release_ucontext_pd(uctx);
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200685 return;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530686 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530687 }
Leon Romanovsky21a428a2019-02-03 14:55:51 +0200688 _ocrdma_dealloc_pd(dev, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530689}
690
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530691static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
692 u32 pdid, int acc, u32 num_pbls, u32 addr_check)
Parav Panditfe2caef2012-03-21 04:09:06 +0530693{
694 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530695
Parav Panditfe2caef2012-03-21 04:09:06 +0530696 mr->hwmr.fr_mr = 0;
697 mr->hwmr.local_rd = 1;
698 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
699 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
700 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
701 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
702 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
703 mr->hwmr.num_pbls = num_pbls;
704
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530705 status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
706 if (status)
707 return status;
708
Parav Panditfe2caef2012-03-21 04:09:06 +0530709 mr->ibmr.lkey = mr->hwmr.lkey;
710 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
711 mr->ibmr.rkey = mr->hwmr.lkey;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530712 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530713}
714
715struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
716{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530717 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530718 struct ocrdma_mr *mr;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530719 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
720 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530721
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530722 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
723 pr_err("%s err, invalid access rights\n", __func__);
724 return ERR_PTR(-EINVAL);
725 }
726
727 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
728 if (!mr)
729 return ERR_PTR(-ENOMEM);
730
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530731 status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530732 OCRDMA_ADDR_CHECK_DISABLE);
733 if (status) {
734 kfree(mr);
735 return ERR_PTR(status);
736 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530737
738 return &mr->ibmr;
739}
740
741static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
742 struct ocrdma_hw_mr *mr)
743{
744 struct pci_dev *pdev = dev->nic_info.pdev;
745 int i = 0;
746
747 if (mr->pbl_table) {
748 for (i = 0; i < mr->num_pbls; i++) {
749 if (!mr->pbl_table[i].va)
750 continue;
751 dma_free_coherent(&pdev->dev, mr->pbl_size,
752 mr->pbl_table[i].va,
753 mr->pbl_table[i].pa);
754 }
755 kfree(mr->pbl_table);
756 mr->pbl_table = NULL;
757 }
758}
759
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530760static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
761 u32 num_pbes)
Parav Panditfe2caef2012-03-21 04:09:06 +0530762{
763 u32 num_pbls = 0;
764 u32 idx = 0;
765 int status = 0;
766 u32 pbl_size;
767
768 do {
769 pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
770 if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
771 status = -EFAULT;
772 break;
773 }
774 num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
775 num_pbls = num_pbls / (pbl_size / sizeof(u64));
776 idx++;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530777 } while (num_pbls >= dev->attr.max_num_mr_pbl);
Parav Panditfe2caef2012-03-21 04:09:06 +0530778
779 mr->hwmr.num_pbes = num_pbes;
780 mr->hwmr.num_pbls = num_pbls;
781 mr->hwmr.pbl_size = pbl_size;
782 return status;
783}
784
785static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
786{
787 int status = 0;
788 int i;
789 u32 dma_len = mr->pbl_size;
790 struct pci_dev *pdev = dev->nic_info.pdev;
791 void *va;
792 dma_addr_t pa;
793
Kees Cook6396bb22018-06-12 14:03:40 -0700794 mr->pbl_table = kcalloc(mr->num_pbls, sizeof(struct ocrdma_pbl),
795 GFP_KERNEL);
Parav Panditfe2caef2012-03-21 04:09:06 +0530796
797 if (!mr->pbl_table)
798 return -ENOMEM;
799
800 for (i = 0; i < mr->num_pbls; i++) {
Luis Chamberlain750afb02019-01-04 09:23:09 +0100801 va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
Parav Panditfe2caef2012-03-21 04:09:06 +0530802 if (!va) {
803 ocrdma_free_mr_pbl_tbl(dev, mr);
804 status = -ENOMEM;
805 break;
806 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530807 mr->pbl_table[i].va = va;
808 mr->pbl_table[i].pa = pa;
809 }
810 return status;
811}
812
813static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
814 u32 num_pbes)
815{
816 struct ocrdma_pbe *pbe;
Shiraz, Saleembe8c4562019-02-11 09:25:05 -0600817 struct sg_dma_page_iter sg_iter;
Parav Panditfe2caef2012-03-21 04:09:06 +0530818 struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
819 struct ib_umem *umem = mr->umem;
Shiraz, Saleembe8c4562019-02-11 09:25:05 -0600820 int pbe_cnt, total_num_pbes = 0;
821 u64 pg_addr;
Parav Panditfe2caef2012-03-21 04:09:06 +0530822
823 if (!mr->hwmr.num_pbes)
824 return;
825
826 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
827 pbe_cnt = 0;
828
Shiraz, Saleembe8c4562019-02-11 09:25:05 -0600829 for_each_sg_dma_page (umem->sg_head.sgl, &sg_iter, umem->nmap, 0) {
830 /* store the page address in pbe */
831 pg_addr = sg_page_iter_dma_address(&sg_iter);
832 pbe->pa_lo = cpu_to_le32(pg_addr);
833 pbe->pa_hi = cpu_to_le32(upper_32_bits(pg_addr));
834 pbe_cnt += 1;
835 total_num_pbes += 1;
836 pbe++;
Parav Panditfe2caef2012-03-21 04:09:06 +0530837
Shiraz, Saleembe8c4562019-02-11 09:25:05 -0600838 /* if done building pbes, issue the mbx cmd. */
839 if (total_num_pbes == num_pbes)
840 return;
Parav Panditfe2caef2012-03-21 04:09:06 +0530841
Shiraz, Saleembe8c4562019-02-11 09:25:05 -0600842 /* if the given pbl is full storing the pbes,
843 * move to next pbl.
844 */
845 if (pbe_cnt == (mr->hwmr.pbl_size / sizeof(u64))) {
846 pbl_tbl++;
847 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
848 pbe_cnt = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530849 }
850 }
851}
852
853struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
854 u64 usr_addr, int acc, struct ib_udata *udata)
855{
856 int status = -ENOMEM;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530857 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530858 struct ocrdma_mr *mr;
859 struct ocrdma_pd *pd;
Parav Panditfe2caef2012-03-21 04:09:06 +0530860 u32 num_pbes;
861
862 pd = get_ocrdma_pd(ibpd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530863
864 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
865 return ERR_PTR(-EINVAL);
866
867 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
868 if (!mr)
869 return ERR_PTR(status);
Moni Shouac320e522020-01-15 14:43:31 +0200870 mr->umem = ib_umem_get(ibpd->device, start, len, acc);
Parav Panditfe2caef2012-03-21 04:09:06 +0530871 if (IS_ERR(mr->umem)) {
872 status = -EFAULT;
873 goto umem_err;
874 }
875 num_pbes = ib_umem_page_count(mr->umem);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530876 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
Parav Panditfe2caef2012-03-21 04:09:06 +0530877 if (status)
878 goto umem_err;
879
Shiraz, Saleembe8c4562019-02-11 09:25:05 -0600880 mr->hwmr.pbe_size = PAGE_SIZE;
Haggai Eran406f9e52014-12-11 17:04:12 +0200881 mr->hwmr.fbo = ib_umem_offset(mr->umem);
Parav Panditfe2caef2012-03-21 04:09:06 +0530882 mr->hwmr.va = usr_addr;
883 mr->hwmr.len = len;
884 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
885 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
886 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
887 mr->hwmr.local_rd = 1;
888 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
889 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
890 if (status)
891 goto umem_err;
892 build_user_pbes(dev, mr, num_pbes);
893 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
894 if (status)
895 goto mbx_err;
Parav Panditfe2caef2012-03-21 04:09:06 +0530896 mr->ibmr.lkey = mr->hwmr.lkey;
897 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
898 mr->ibmr.rkey = mr->hwmr.lkey;
899
900 return &mr->ibmr;
901
902mbx_err:
903 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
904umem_err:
905 kfree(mr);
906 return ERR_PTR(status);
907}
908
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +0300909int ocrdma_dereg_mr(struct ib_mr *ib_mr, struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +0530910{
911 struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530912 struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530913
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530914 (void) ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
Parav Panditfe2caef2012-03-21 04:09:06 +0530915
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +0300916 kfree(mr->pages);
Selvin Xavier9d1878a2014-02-04 11:57:02 +0530917 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
Parav Panditfe2caef2012-03-21 04:09:06 +0530918
Parav Panditfe2caef2012-03-21 04:09:06 +0530919 /* it could be user registered memory. */
Leon Romanovsky836a0fb2019-06-16 15:05:20 +0300920 ib_umem_release(mr->umem);
Parav Panditfe2caef2012-03-21 04:09:06 +0530921 kfree(mr);
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530922
923 /* Don't stop cleanup, in case FW is unresponsive */
924 if (dev->mqe_ctx.fw_error_state) {
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530925 pr_err("%s(%d) fw not responding.\n",
926 __func__, dev->id);
927 }
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530928 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530929}
930
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530931static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300932 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +0530933{
934 int status;
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300935 struct ocrdma_ucontext *uctx = rdma_udata_to_drv_context(
936 udata, struct ocrdma_ucontext, ibucontext);
Parav Panditfe2caef2012-03-21 04:09:06 +0530937 struct ocrdma_create_cq_uresp uresp;
938
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300939 /* this must be user flow! */
940 if (!udata)
941 return -EINVAL;
942
Dan Carpenter63ea3742013-07-29 22:34:29 +0300943 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530944 uresp.cq_id = cq->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530945 uresp.page_size = PAGE_ALIGN(cq->len);
Parav Panditfe2caef2012-03-21 04:09:06 +0530946 uresp.num_pages = 1;
947 uresp.max_hw_cqe = cq->max_hw_cqe;
Devesh Sharma1b76d382014-09-05 19:35:40 +0530948 uresp.page_addr[0] = virt_to_phys(cq->va);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530949 uresp.db_page_addr = ocrdma_get_db_addr(dev, uctx->cntxt_pd->id);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530950 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530951 uresp.phase_change = cq->phase_change ? 1 : 0;
952 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
953 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000954 pr_err("%s(%d) copy error cqid=0x%x.\n",
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530955 __func__, dev->id, cq->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530956 goto err;
957 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530958 status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
959 if (status)
960 goto err;
961 status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
962 if (status) {
963 ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
964 goto err;
965 }
966 cq->ucontext = uctx;
967err:
968 return status;
969}
970
Leon Romanovskye39afe32019-05-28 14:37:29 +0300971int ocrdma_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
972 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +0530973{
Leon Romanovskye39afe32019-05-28 14:37:29 +0300974 struct ib_device *ibdev = ibcq->device;
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300975 int entries = attr->cqe;
Leon Romanovskye39afe32019-05-28 14:37:29 +0300976 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
Parav Panditfe2caef2012-03-21 04:09:06 +0530977 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300978 struct ocrdma_ucontext *uctx = rdma_udata_to_drv_context(
979 udata, struct ocrdma_ucontext, ibucontext);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530980 u16 pd_id = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530981 int status;
982 struct ocrdma_create_cq_ureq ureq;
983
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300984 if (attr->flags)
Leon Romanovskye39afe32019-05-28 14:37:29 +0300985 return -EINVAL;
Matan Barakbcf4c1e2015-06-11 16:35:20 +0300986
Parav Panditfe2caef2012-03-21 04:09:06 +0530987 if (udata) {
988 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
Leon Romanovskye39afe32019-05-28 14:37:29 +0300989 return -EFAULT;
Parav Panditfe2caef2012-03-21 04:09:06 +0530990 } else
991 ureq.dpp_cq = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530992
993 spin_lock_init(&cq->cq_lock);
994 spin_lock_init(&cq->comp_handler_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +0530995 INIT_LIST_HEAD(&cq->sq_head);
996 INIT_LIST_HEAD(&cq->rq_head);
Parav Panditfe2caef2012-03-21 04:09:06 +0530997
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +0300998 if (udata)
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530999 pd_id = uctx->cntxt_pd->id;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301000
1001 status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq, pd_id);
Leon Romanovskye39afe32019-05-28 14:37:29 +03001002 if (status)
1003 return status;
1004
Shamir Rabinovitchff23dfa2019-03-31 19:10:07 +03001005 if (udata) {
1006 status = ocrdma_copy_cq_uresp(dev, cq, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +05301007 if (status)
1008 goto ctx_err;
1009 }
1010 cq->phase = OCRDMA_CQE_VALID;
Parav Panditfe2caef2012-03-21 04:09:06 +05301011 dev->cq_tbl[cq->id] = cq;
Leon Romanovskye39afe32019-05-28 14:37:29 +03001012 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301013
1014ctx_err:
1015 ocrdma_mbx_destroy_cq(dev, cq);
Leon Romanovskye39afe32019-05-28 14:37:29 +03001016 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301017}
1018
1019int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
1020 struct ib_udata *udata)
1021{
1022 int status = 0;
1023 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
1024
1025 if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
1026 status = -EINVAL;
1027 return status;
1028 }
1029 ibcq->cqe = new_cnt;
1030 return status;
1031}
1032
Devesh Sharmaea617622014-02-04 11:56:54 +05301033static void ocrdma_flush_cq(struct ocrdma_cq *cq)
1034{
1035 int cqe_cnt;
1036 int valid_count = 0;
1037 unsigned long flags;
1038
1039 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
1040 struct ocrdma_cqe *cqe = NULL;
1041
1042 cqe = cq->va;
1043 cqe_cnt = cq->cqe_cnt;
1044
1045 /* Last irq might have scheduled a polling thread
1046 * sync-up with it before hard flushing.
1047 */
1048 spin_lock_irqsave(&cq->cq_lock, flags);
1049 while (cqe_cnt) {
1050 if (is_cqe_valid(cq, cqe))
1051 valid_count++;
1052 cqe++;
1053 cqe_cnt--;
1054 }
1055 ocrdma_ring_cq_db(dev, cq->id, false, false, valid_count);
1056 spin_unlock_irqrestore(&cq->cq_lock, flags);
1057}
1058
Leon Romanovskya52c8e22019-05-28 14:37:28 +03001059void ocrdma_destroy_cq(struct ib_cq *ibcq, struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301060{
Parav Panditfe2caef2012-03-21 04:09:06 +05301061 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
Devesh Sharmaea617622014-02-04 11:56:54 +05301062 struct ocrdma_eq *eq = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301063 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301064 int pdid = 0;
Devesh Sharmaea617622014-02-04 11:56:54 +05301065 u32 irq, indx;
1066
1067 dev->cq_tbl[cq->id] = NULL;
1068 indx = ocrdma_get_eq_table_index(dev, cq->eqn);
Devesh Sharmaea617622014-02-04 11:56:54 +05301069
1070 eq = &dev->eq_tbl[indx];
1071 irq = ocrdma_get_irq(dev, eq);
1072 synchronize_irq(irq);
1073 ocrdma_flush_cq(cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301074
Leon Romanovskya52c8e22019-05-28 14:37:28 +03001075 ocrdma_mbx_destroy_cq(dev, cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301076 if (cq->ucontext) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301077 pdid = cq->ucontext->cntxt_pd->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301078 ocrdma_del_mmap(cq->ucontext, (u64) cq->pa,
1079 PAGE_ALIGN(cq->len));
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301080 ocrdma_del_mmap(cq->ucontext,
1081 ocrdma_get_db_addr(dev, pdid),
Parav Panditfe2caef2012-03-21 04:09:06 +05301082 dev->nic_info.db_page_size);
1083 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301084}
1085
1086static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1087{
1088 int status = -EINVAL;
1089
1090 if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
1091 dev->qp_tbl[qp->id] = qp;
1092 status = 0;
1093 }
1094 return status;
1095}
1096
1097static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1098{
1099 dev->qp_tbl[qp->id] = NULL;
1100}
1101
1102static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02001103 struct ib_qp_init_attr *attrs,
1104 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301105{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301106 if ((attrs->qp_type != IB_QPT_GSI) &&
1107 (attrs->qp_type != IB_QPT_RC) &&
1108 (attrs->qp_type != IB_QPT_UC) &&
1109 (attrs->qp_type != IB_QPT_UD)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001110 pr_err("%s(%d) unsupported qp type=0x%x requested\n",
1111 __func__, dev->id, attrs->qp_type);
Kamal Heibbb8865f2020-01-30 10:20:49 +02001112 return -EOPNOTSUPP;
Parav Panditfe2caef2012-03-21 04:09:06 +05301113 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301114 /* Skip the check for QP1 to support CM size of 128 */
1115 if ((attrs->qp_type != IB_QPT_GSI) &&
1116 (attrs->cap.max_send_wr > dev->attr.max_wqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001117 pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
1118 __func__, dev->id, attrs->cap.max_send_wr);
1119 pr_err("%s(%d) supported send_wr=0x%x\n",
1120 __func__, dev->id, dev->attr.max_wqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301121 return -EINVAL;
1122 }
1123 if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001124 pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
1125 __func__, dev->id, attrs->cap.max_recv_wr);
1126 pr_err("%s(%d) supported recv_wr=0x%x\n",
1127 __func__, dev->id, dev->attr.max_rqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301128 return -EINVAL;
1129 }
1130 if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001131 pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
1132 __func__, dev->id, attrs->cap.max_inline_data);
1133 pr_err("%s(%d) supported inline data size=0x%x\n",
1134 __func__, dev->id, dev->attr.max_inline_data);
Parav Panditfe2caef2012-03-21 04:09:06 +05301135 return -EINVAL;
1136 }
1137 if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001138 pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
1139 __func__, dev->id, attrs->cap.max_send_sge);
1140 pr_err("%s(%d) supported send_sge=0x%x\n",
1141 __func__, dev->id, dev->attr.max_send_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301142 return -EINVAL;
1143 }
1144 if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001145 pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
1146 __func__, dev->id, attrs->cap.max_recv_sge);
1147 pr_err("%s(%d) supported recv_sge=0x%x\n",
1148 __func__, dev->id, dev->attr.max_recv_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301149 return -EINVAL;
1150 }
1151 /* unprivileged user space cannot create special QP */
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02001152 if (udata && attrs->qp_type == IB_QPT_GSI) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001153 pr_err
Parav Panditfe2caef2012-03-21 04:09:06 +05301154 ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
1155 __func__, dev->id, attrs->qp_type);
1156 return -EINVAL;
1157 }
1158 /* allow creating only one GSI type of QP */
1159 if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001160 pr_err("%s(%d) GSI special QPs already created.\n",
1161 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301162 return -EINVAL;
1163 }
1164 /* verify consumer QPs are not trying to use GSI QP's CQ */
1165 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
1166 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301167 (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001168 pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301169 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301170 return -EINVAL;
1171 }
1172 }
1173 return 0;
1174}
1175
1176static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
1177 struct ib_udata *udata, int dpp_offset,
1178 int dpp_credit_lmt, int srq)
1179{
Markus Elfring0ca4c392015-12-26 18:18:18 +01001180 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301181 u64 usr_db;
1182 struct ocrdma_create_qp_uresp uresp;
Parav Panditfe2caef2012-03-21 04:09:06 +05301183 struct ocrdma_pd *pd = qp->pd;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301184 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301185
1186 memset(&uresp, 0, sizeof(uresp));
1187 usr_db = dev->nic_info.unmapped_db +
1188 (pd->id * dev->nic_info.db_page_size);
1189 uresp.qp_id = qp->id;
1190 uresp.sq_dbid = qp->sq.dbid;
1191 uresp.num_sq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301192 uresp.sq_page_size = PAGE_ALIGN(qp->sq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301193 uresp.sq_page_addr[0] = virt_to_phys(qp->sq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301194 uresp.num_wqe_allocated = qp->sq.max_cnt;
1195 if (!srq) {
1196 uresp.rq_dbid = qp->rq.dbid;
1197 uresp.num_rq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301198 uresp.rq_page_size = PAGE_ALIGN(qp->rq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301199 uresp.rq_page_addr[0] = virt_to_phys(qp->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301200 uresp.num_rqe_allocated = qp->rq.max_cnt;
1201 }
1202 uresp.db_page_addr = usr_db;
1203 uresp.db_page_size = dev->nic_info.db_page_size;
Devesh Sharma2df84fa82014-02-04 11:56:55 +05301204 uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
1205 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
1206 uresp.db_shift = OCRDMA_DB_RQ_SHIFT;
Parav Panditfe2caef2012-03-21 04:09:06 +05301207
1208 if (qp->dpp_enabled) {
1209 uresp.dpp_credit = dpp_credit_lmt;
1210 uresp.dpp_offset = dpp_offset;
1211 }
1212 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1213 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001214 pr_err("%s(%d) user copy error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301215 goto err;
1216 }
1217 status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
1218 uresp.sq_page_size);
1219 if (status)
1220 goto err;
1221
1222 if (!srq) {
1223 status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
1224 uresp.rq_page_size);
1225 if (status)
1226 goto rq_map_err;
1227 }
1228 return status;
1229rq_map_err:
1230 ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
1231err:
1232 return status;
1233}
1234
1235static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
1236 struct ocrdma_pd *pd)
1237{
Devesh Sharma21c33912014-02-04 11:56:56 +05301238 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301239 qp->sq_db = dev->nic_info.db +
1240 (pd->id * dev->nic_info.db_page_size) +
1241 OCRDMA_DB_GEN2_SQ_OFFSET;
1242 qp->rq_db = dev->nic_info.db +
1243 (pd->id * dev->nic_info.db_page_size) +
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301244 OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301245 } else {
1246 qp->sq_db = dev->nic_info.db +
1247 (pd->id * dev->nic_info.db_page_size) +
1248 OCRDMA_DB_SQ_OFFSET;
1249 qp->rq_db = dev->nic_info.db +
1250 (pd->id * dev->nic_info.db_page_size) +
1251 OCRDMA_DB_RQ_OFFSET;
1252 }
1253}
1254
1255static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
1256{
1257 qp->wqe_wr_id_tbl =
Kees Cook6396bb22018-06-12 14:03:40 -07001258 kcalloc(qp->sq.max_cnt, sizeof(*(qp->wqe_wr_id_tbl)),
Parav Panditfe2caef2012-03-21 04:09:06 +05301259 GFP_KERNEL);
1260 if (qp->wqe_wr_id_tbl == NULL)
1261 return -ENOMEM;
1262 qp->rqe_wr_id_tbl =
Kees Cook6396bb22018-06-12 14:03:40 -07001263 kcalloc(qp->rq.max_cnt, sizeof(u64), GFP_KERNEL);
Parav Panditfe2caef2012-03-21 04:09:06 +05301264 if (qp->rqe_wr_id_tbl == NULL)
1265 return -ENOMEM;
1266
1267 return 0;
1268}
1269
1270static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
1271 struct ocrdma_pd *pd,
1272 struct ib_qp_init_attr *attrs)
1273{
1274 qp->pd = pd;
1275 spin_lock_init(&qp->q_lock);
1276 INIT_LIST_HEAD(&qp->sq_entry);
1277 INIT_LIST_HEAD(&qp->rq_entry);
1278
1279 qp->qp_type = attrs->qp_type;
1280 qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
1281 qp->max_inline_data = attrs->cap.max_inline_data;
1282 qp->sq.max_sges = attrs->cap.max_send_sge;
1283 qp->rq.max_sges = attrs->cap.max_recv_sge;
1284 qp->state = OCRDMA_QPS_RST;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05301285 qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
Parav Panditfe2caef2012-03-21 04:09:06 +05301286}
1287
Parav Panditfe2caef2012-03-21 04:09:06 +05301288static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
1289 struct ib_qp_init_attr *attrs)
1290{
1291 if (attrs->qp_type == IB_QPT_GSI) {
1292 dev->gsi_qp_created = 1;
1293 dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
1294 dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
1295 }
1296}
1297
1298struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
1299 struct ib_qp_init_attr *attrs,
1300 struct ib_udata *udata)
1301{
1302 int status;
1303 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1304 struct ocrdma_qp *qp;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301305 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301306 struct ocrdma_create_qp_ureq ureq;
1307 u16 dpp_credit_lmt, dpp_offset;
1308
Shamir Rabinovitche00b64f2018-12-17 17:15:18 +02001309 status = ocrdma_check_qp_params(ibpd, dev, attrs, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +05301310 if (status)
1311 goto gen_err;
1312
1313 memset(&ureq, 0, sizeof(ureq));
1314 if (udata) {
1315 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1316 return ERR_PTR(-EFAULT);
1317 }
1318 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1319 if (!qp) {
1320 status = -ENOMEM;
1321 goto gen_err;
1322 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301323 ocrdma_set_qp_init_params(qp, pd, attrs);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301324 if (udata == NULL)
1325 qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
1326 OCRDMA_QP_FAST_REG);
Parav Panditfe2caef2012-03-21 04:09:06 +05301327
1328 mutex_lock(&dev->dev_lock);
1329 status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
1330 ureq.dpp_cq_id,
1331 &dpp_offset, &dpp_credit_lmt);
1332 if (status)
1333 goto mbx_err;
1334
1335 /* user space QP's wr_id table are managed in library */
1336 if (udata == NULL) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301337 status = ocrdma_alloc_wr_id_tbl(qp);
1338 if (status)
1339 goto map_err;
1340 }
1341
1342 status = ocrdma_add_qpn_map(dev, qp);
1343 if (status)
1344 goto map_err;
1345 ocrdma_set_qp_db(dev, qp, pd);
1346 if (udata) {
1347 status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
1348 dpp_credit_lmt,
1349 (attrs->srq != NULL));
1350 if (status)
1351 goto cpy_err;
1352 }
1353 ocrdma_store_gsi_qp_cq(dev, attrs);
Gottumukkala, Naresh27159f52013-06-05 08:50:46 +00001354 qp->ibqp.qp_num = qp->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301355 mutex_unlock(&dev->dev_lock);
1356 return &qp->ibqp;
1357
1358cpy_err:
1359 ocrdma_del_qpn_map(dev, qp);
1360map_err:
1361 ocrdma_mbx_destroy_qp(dev, qp);
1362mbx_err:
1363 mutex_unlock(&dev->dev_lock);
1364 kfree(qp->wqe_wr_id_tbl);
1365 kfree(qp->rqe_wr_id_tbl);
1366 kfree(qp);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001367 pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05301368gen_err:
1369 return ERR_PTR(status);
1370}
1371
1372int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1373 int attr_mask)
1374{
1375 int status = 0;
1376 struct ocrdma_qp *qp;
1377 struct ocrdma_dev *dev;
1378 enum ib_qp_state old_qps;
1379
1380 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301381 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301382 if (attr_mask & IB_QP_STATE)
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301383 status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301384 /* if new and previous states are same hw doesn't need to
1385 * know about it.
1386 */
1387 if (status < 0)
1388 return status;
Markus Elfring95f60bb2015-12-26 18:40:43 +01001389 return ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask);
Parav Panditfe2caef2012-03-21 04:09:06 +05301390}
1391
1392int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1393 int attr_mask, struct ib_udata *udata)
1394{
1395 unsigned long flags;
1396 int status = -EINVAL;
1397 struct ocrdma_qp *qp;
1398 struct ocrdma_dev *dev;
1399 enum ib_qp_state old_qps, new_qps;
1400
1401 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301402 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301403
1404 /* syncronize with multiple context trying to change, retrive qps */
1405 mutex_lock(&dev->dev_lock);
1406 /* syncronize with wqe, rqe posting and cqe processing contexts */
1407 spin_lock_irqsave(&qp->q_lock, flags);
1408 old_qps = get_ibqp_state(qp->state);
1409 if (attr_mask & IB_QP_STATE)
1410 new_qps = attr->qp_state;
1411 else
1412 new_qps = old_qps;
1413 spin_unlock_irqrestore(&qp->q_lock, flags);
1414
Kamal Heibd31131b2018-10-02 16:11:21 +03001415 if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001416 pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
1417 "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
1418 __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
1419 old_qps, new_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301420 goto param_err;
1421 }
1422
1423 status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
1424 if (status > 0)
1425 status = 0;
1426param_err:
1427 mutex_unlock(&dev->dev_lock);
1428 return status;
1429}
1430
1431static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
1432{
1433 switch (mtu) {
1434 case 256:
1435 return IB_MTU_256;
1436 case 512:
1437 return IB_MTU_512;
1438 case 1024:
1439 return IB_MTU_1024;
1440 case 2048:
1441 return IB_MTU_2048;
1442 case 4096:
1443 return IB_MTU_4096;
1444 default:
1445 return IB_MTU_1024;
1446 }
1447}
1448
1449static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
1450{
1451 int ib_qp_acc_flags = 0;
1452
1453 if (qp_cap_flags & OCRDMA_QP_INB_WR)
1454 ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
1455 if (qp_cap_flags & OCRDMA_QP_INB_RD)
1456 ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
1457 return ib_qp_acc_flags;
1458}
1459
1460int ocrdma_query_qp(struct ib_qp *ibqp,
1461 struct ib_qp_attr *qp_attr,
1462 int attr_mask, struct ib_qp_init_attr *qp_init_attr)
1463{
1464 int status;
1465 u32 qp_state;
1466 struct ocrdma_qp_params params;
1467 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301468 struct ocrdma_dev *dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301469
1470 memset(&params, 0, sizeof(params));
1471 mutex_lock(&dev->dev_lock);
1472 status = ocrdma_mbx_query_qp(dev, qp, &params);
1473 mutex_unlock(&dev->dev_lock);
1474 if (status)
1475 goto mbx_err;
Mitesh Ahuja95bf0092014-12-03 11:36:33 +05301476 if (qp->qp_type == IB_QPT_UD)
1477 qp_attr->qkey = params.qkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05301478 qp_attr->path_mtu =
1479 ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
1480 OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
1481 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
1482 qp_attr->path_mig_state = IB_MIG_MIGRATED;
1483 qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
1484 qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
1485 qp_attr->dest_qp_num =
1486 params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
1487
1488 qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
1489 qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
1490 qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
1491 qp_attr->cap.max_send_sge = qp->sq.max_sges;
1492 qp_attr->cap.max_recv_sge = qp->rq.max_sges;
Naresh Gottumukkalac43e9ab2013-08-26 15:27:46 +05301493 qp_attr->cap.max_inline_data = qp->max_inline_data;
Parav Panditfe2caef2012-03-21 04:09:06 +05301494 qp_init_attr->cap = qp_attr->cap;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04001495 qp_attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
Parav Panditfe2caef2012-03-21 04:09:06 +05301496
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001497 rdma_ah_set_grh(&qp_attr->ah_attr, NULL,
1498 params.rnt_rc_sl_fl &
1499 OCRDMA_QP_PARAMS_FLOW_LABEL_MASK,
1500 qp->sgid_idx,
1501 (params.hop_lmt_rq_psn &
1502 OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
1503 OCRDMA_QP_PARAMS_HOP_LMT_SHIFT,
1504 (params.tclass_sq_psn &
1505 OCRDMA_QP_PARAMS_TCLASS_MASK) >>
1506 OCRDMA_QP_PARAMS_TCLASS_SHIFT);
1507 rdma_ah_set_dgid_raw(&qp_attr->ah_attr, &params.dgid[0]);
1508
1509 rdma_ah_set_port_num(&qp_attr->ah_attr, 1);
1510 rdma_ah_set_sl(&qp_attr->ah_attr, (params.rnt_rc_sl_fl &
1511 OCRDMA_QP_PARAMS_SL_MASK) >>
1512 OCRDMA_QP_PARAMS_SL_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05301513 qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
1514 OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
1515 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
1516 qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
1517 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
1518 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
1519 qp_attr->retry_cnt =
1520 (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
1521 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
1522 qp_attr->min_rnr_timer = 0;
1523 qp_attr->pkey_index = 0;
1524 qp_attr->port_num = 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001525 rdma_ah_set_path_bits(&qp_attr->ah_attr, 0);
1526 rdma_ah_set_static_rate(&qp_attr->ah_attr, 0);
Parav Panditfe2caef2012-03-21 04:09:06 +05301527 qp_attr->alt_pkey_index = 0;
1528 qp_attr->alt_port_num = 0;
1529 qp_attr->alt_timeout = 0;
1530 memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
1531 qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
1532 OCRDMA_QP_PARAMS_STATE_SHIFT;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301533 qp_attr->qp_state = get_ibqp_state(qp_state);
1534 qp_attr->cur_qp_state = qp_attr->qp_state;
Parav Panditfe2caef2012-03-21 04:09:06 +05301535 qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
1536 qp_attr->max_dest_rd_atomic =
1537 params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
1538 qp_attr->max_rd_atomic =
1539 params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
1540 qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
1541 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301542 /* Sync driver QP state with FW */
1543 ocrdma_qp_state_change(qp, qp_attr->qp_state, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05301544mbx_err:
1545 return status;
1546}
1547
Rasmus Villemoesf3070e72015-01-16 15:39:56 +01001548static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, unsigned int idx)
Parav Panditfe2caef2012-03-21 04:09:06 +05301549{
Rasmus Villemoesf3070e72015-01-16 15:39:56 +01001550 unsigned int i = idx / 32;
1551 u32 mask = (1U << (idx % 32));
Parav Panditfe2caef2012-03-21 04:09:06 +05301552
Rasmus Villemoesba64fdc2015-01-16 15:39:55 +01001553 srq->idx_bit_fields[i] ^= mask;
Parav Panditfe2caef2012-03-21 04:09:06 +05301554}
1555
1556static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
1557{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301558 return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
Parav Panditfe2caef2012-03-21 04:09:06 +05301559}
1560
1561static int is_hw_sq_empty(struct ocrdma_qp *qp)
1562{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301563 return (qp->sq.tail == qp->sq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301564}
1565
1566static int is_hw_rq_empty(struct ocrdma_qp *qp)
1567{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301568 return (qp->rq.tail == qp->rq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301569}
1570
1571static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
1572{
1573 return q->va + (q->head * q->entry_size);
1574}
1575
1576static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
1577 u32 idx)
1578{
1579 return q->va + (idx * q->entry_size);
1580}
1581
1582static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
1583{
1584 q->head = (q->head + 1) & q->max_wqe_idx;
1585}
1586
1587static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
1588{
1589 q->tail = (q->tail + 1) & q->max_wqe_idx;
1590}
1591
1592/* discard the cqe for a given QP */
1593static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
1594{
1595 unsigned long cq_flags;
1596 unsigned long flags;
1597 int discard_cnt = 0;
1598 u32 cur_getp, stop_getp;
1599 struct ocrdma_cqe *cqe;
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301600 u32 qpn = 0, wqe_idx = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301601
1602 spin_lock_irqsave(&cq->cq_lock, cq_flags);
1603
1604 /* traverse through the CQEs in the hw CQ,
1605 * find the matching CQE for a given qp,
1606 * mark the matching one discarded by clearing qpn.
1607 * ring the doorbell in the poll_cq() as
1608 * we don't complete out of order cqe.
1609 */
1610
1611 cur_getp = cq->getp;
1612 /* find upto when do we reap the cq. */
1613 stop_getp = cur_getp;
1614 do {
1615 if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
1616 break;
1617
1618 cqe = cq->va + cur_getp;
1619 /* if (a) done reaping whole hw cq, or
1620 * (b) qp_xq becomes empty.
1621 * then exit
1622 */
1623 qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
1624 /* if previously discarded cqe found, skip that too. */
1625 /* check for matching qp */
1626 if (qpn == 0 || qpn != qp->id)
1627 goto skip_cqe;
1628
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301629 if (is_cqe_for_sq(cqe)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301630 ocrdma_hwq_inc_tail(&qp->sq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301631 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301632 if (qp->srq) {
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301633 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
1634 OCRDMA_CQE_BUFTAG_SHIFT) &
1635 qp->srq->rq.max_wqe_idx;
ssh10db287ec2016-12-24 21:50:06 +05301636 BUG_ON(wqe_idx < 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05301637 spin_lock_irqsave(&qp->srq->q_lock, flags);
1638 ocrdma_hwq_inc_tail(&qp->srq->rq);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301639 ocrdma_srq_toggle_bit(qp->srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05301640 spin_unlock_irqrestore(&qp->srq->q_lock, flags);
1641
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301642 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301643 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301644 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301645 }
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301646 /* mark cqe discarded so that it is not picked up later
1647 * in the poll_cq().
1648 */
1649 discard_cnt += 1;
1650 cqe->cmn.qpn = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301651skip_cqe:
1652 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
1653 } while (cur_getp != stop_getp);
1654 spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
1655}
1656
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301657void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
Parav Panditfe2caef2012-03-21 04:09:06 +05301658{
1659 int found = false;
1660 unsigned long flags;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301661 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301662 /* sync with any active CQ poll */
1663
1664 spin_lock_irqsave(&dev->flush_q_lock, flags);
1665 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1666 if (found)
1667 list_del(&qp->sq_entry);
1668 if (!qp->srq) {
1669 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1670 if (found)
1671 list_del(&qp->rq_entry);
1672 }
1673 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
1674}
1675
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03001676int ocrdma_destroy_qp(struct ib_qp *ibqp, struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301677{
Parav Panditfe2caef2012-03-21 04:09:06 +05301678 struct ocrdma_pd *pd;
1679 struct ocrdma_qp *qp;
1680 struct ocrdma_dev *dev;
1681 struct ib_qp_attr attrs;
Devesh Sharmafe488222015-05-19 11:32:34 +05301682 int attr_mask;
Dan Carpenterd19081e2012-05-02 09:14:47 +03001683 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05301684
1685 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301686 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301687
Parav Panditfe2caef2012-03-21 04:09:06 +05301688 pd = qp->pd;
1689
1690 /* change the QP state to ERROR */
Devesh Sharmafe488222015-05-19 11:32:34 +05301691 if (qp->state != OCRDMA_QPS_RST) {
1692 attrs.qp_state = IB_QPS_ERR;
1693 attr_mask = IB_QP_STATE;
1694 _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
1695 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301696 /* ensure that CQEs for newly created QP (whose id may be same with
1697 * one which just getting destroyed are same), dont get
1698 * discarded until the old CQEs are discarded.
1699 */
1700 mutex_lock(&dev->dev_lock);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301701 (void) ocrdma_mbx_destroy_qp(dev, qp);
Parav Panditfe2caef2012-03-21 04:09:06 +05301702
1703 /*
1704 * acquire CQ lock while destroy is in progress, in order to
1705 * protect against proessing in-flight CQEs for this QP.
1706 */
Dan Carpenterd19081e2012-05-02 09:14:47 +03001707 spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
Bart Van Asschebeae9eb2018-07-06 13:04:30 -07001708 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq)) {
Dan Carpenterd19081e2012-05-02 09:14:47 +03001709 spin_lock(&qp->rq_cq->cq_lock);
Bart Van Asschebeae9eb2018-07-06 13:04:30 -07001710 ocrdma_del_qpn_map(dev, qp);
Dan Carpenterd19081e2012-05-02 09:14:47 +03001711 spin_unlock(&qp->rq_cq->cq_lock);
Bart Van Asschebeae9eb2018-07-06 13:04:30 -07001712 } else {
1713 ocrdma_del_qpn_map(dev, qp);
1714 }
Dan Carpenterd19081e2012-05-02 09:14:47 +03001715 spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301716
1717 if (!pd->uctx) {
1718 ocrdma_discard_cqes(qp, qp->sq_cq);
1719 ocrdma_discard_cqes(qp, qp->rq_cq);
1720 }
1721 mutex_unlock(&dev->dev_lock);
1722
1723 if (pd->uctx) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301724 ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
1725 PAGE_ALIGN(qp->sq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301726 if (!qp->srq)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301727 ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
1728 PAGE_ALIGN(qp->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301729 }
1730
1731 ocrdma_del_flush_qp(qp);
1732
Parav Panditfe2caef2012-03-21 04:09:06 +05301733 kfree(qp->wqe_wr_id_tbl);
1734 kfree(qp->rqe_wr_id_tbl);
1735 kfree(qp);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301736 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301737}
1738
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301739static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
1740 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301741{
1742 int status;
1743 struct ocrdma_create_srq_uresp uresp;
1744
Dan Carpenter63ea3742013-07-29 22:34:29 +03001745 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +05301746 uresp.rq_dbid = srq->rq.dbid;
1747 uresp.num_rq_pages = 1;
Devesh Sharma1b76d382014-09-05 19:35:40 +05301748 uresp.rq_page_addr[0] = virt_to_phys(srq->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301749 uresp.rq_page_size = srq->rq.len;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301750 uresp.db_page_addr = dev->nic_info.unmapped_db +
1751 (srq->pd->id * dev->nic_info.db_page_size);
1752 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +05301753 uresp.num_rqe_allocated = srq->rq.max_cnt;
Devesh Sharma21c33912014-02-04 11:56:56 +05301754 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301755 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301756 uresp.db_shift = 24;
1757 } else {
1758 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
1759 uresp.db_shift = 16;
1760 }
1761
1762 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1763 if (status)
1764 return status;
1765 status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
1766 uresp.rq_page_size);
1767 if (status)
1768 return status;
1769 return status;
1770}
1771
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001772int ocrdma_create_srq(struct ib_srq *ibsrq, struct ib_srq_init_attr *init_attr,
1773 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301774{
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001775 int status;
1776 struct ocrdma_pd *pd = get_ocrdma_pd(ibsrq->pd);
1777 struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
1778 struct ocrdma_srq *srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301779
1780 if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001781 return -EINVAL;
Parav Panditfe2caef2012-03-21 04:09:06 +05301782 if (init_attr->attr.max_wr > dev->attr.max_rqe)
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001783 return -EINVAL;
Parav Panditfe2caef2012-03-21 04:09:06 +05301784
1785 spin_lock_init(&srq->q_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301786 srq->pd = pd;
1787 srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301788 status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +05301789 if (status)
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001790 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301791
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001792 if (!udata) {
Kees Cook6396bb22018-06-12 14:03:40 -07001793 srq->rqe_wr_id_tbl = kcalloc(srq->rq.max_cnt, sizeof(u64),
1794 GFP_KERNEL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001795 if (!srq->rqe_wr_id_tbl) {
1796 status = -ENOMEM;
Parav Panditfe2caef2012-03-21 04:09:06 +05301797 goto arm_err;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001798 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301799
1800 srq->bit_fields_len = (srq->rq.max_cnt / 32) +
1801 (srq->rq.max_cnt % 32 ? 1 : 0);
1802 srq->idx_bit_fields =
Kees Cook6da2ec52018-06-12 13:55:00 -07001803 kmalloc_array(srq->bit_fields_len, sizeof(u32),
1804 GFP_KERNEL);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001805 if (!srq->idx_bit_fields) {
1806 status = -ENOMEM;
Parav Panditfe2caef2012-03-21 04:09:06 +05301807 goto arm_err;
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001808 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301809 memset(srq->idx_bit_fields, 0xff,
1810 srq->bit_fields_len * sizeof(u32));
1811 }
1812
1813 if (init_attr->attr.srq_limit) {
1814 status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
1815 if (status)
1816 goto arm_err;
1817 }
1818
Parav Panditfe2caef2012-03-21 04:09:06 +05301819 if (udata) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301820 status = ocrdma_copy_srq_uresp(dev, srq, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +05301821 if (status)
1822 goto arm_err;
1823 }
1824
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001825 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301826
1827arm_err:
1828 ocrdma_mbx_destroy_srq(dev, srq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301829 kfree(srq->rqe_wr_id_tbl);
1830 kfree(srq->idx_bit_fields);
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001831 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301832}
1833
1834int ocrdma_modify_srq(struct ib_srq *ibsrq,
1835 struct ib_srq_attr *srq_attr,
1836 enum ib_srq_attr_mask srq_attr_mask,
1837 struct ib_udata *udata)
1838{
Markus Elfring0ca4c392015-12-26 18:18:18 +01001839 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301840 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301841
1842 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301843 if (srq_attr_mask & IB_SRQ_MAX_WR)
1844 status = -EINVAL;
1845 else
1846 status = ocrdma_mbx_modify_srq(srq, srq_attr);
1847 return status;
1848}
1849
1850int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
1851{
1852 int status;
1853 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301854
1855 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301856 status = ocrdma_mbx_query_srq(srq, srq_attr);
1857 return status;
1858}
1859
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001860void ocrdma_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301861{
Parav Panditfe2caef2012-03-21 04:09:06 +05301862 struct ocrdma_srq *srq;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301863 struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301864
1865 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301866
Leon Romanovsky68e326d2019-04-03 16:42:43 +03001867 ocrdma_mbx_destroy_srq(dev, srq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301868
1869 if (srq->pd->uctx)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301870 ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
1871 PAGE_ALIGN(srq->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301872
Parav Panditfe2caef2012-03-21 04:09:06 +05301873 kfree(srq->idx_bit_fields);
1874 kfree(srq->rqe_wr_id_tbl);
Parav Panditfe2caef2012-03-21 04:09:06 +05301875}
1876
1877/* unprivileged verbs and their support functions. */
1878static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
1879 struct ocrdma_hdr_wqe *hdr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07001880 const struct ib_send_wr *wr)
Parav Panditfe2caef2012-03-21 04:09:06 +05301881{
1882 struct ocrdma_ewqe_ud_hdr *ud_hdr =
1883 (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001884 struct ocrdma_ah *ah = get_ocrdma_ah(ud_wr(wr)->ah);
Parav Panditfe2caef2012-03-21 04:09:06 +05301885
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001886 ud_hdr->rsvd_dest_qpn = ud_wr(wr)->remote_qpn;
Parav Panditfe2caef2012-03-21 04:09:06 +05301887 if (qp->qp_type == IB_QPT_GSI)
1888 ud_hdr->qkey = qp->qkey;
1889 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001890 ud_hdr->qkey = ud_wr(wr)->remote_qkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05301891 ud_hdr->rsvd_ahid = ah->id;
Devesh Sharma6b062662016-01-28 08:59:57 -05001892 ud_hdr->hdr_type = ah->hdr_type;
Devesh Sharma29565f22014-12-18 14:13:07 +05301893 if (ah->av->valid & OCRDMA_AV_VLAN_VALID)
1894 hdr->cw |= (OCRDMA_FLAG_AH_VLAN_PR << OCRDMA_WQE_FLAGS_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05301895}
1896
1897static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
1898 struct ocrdma_sge *sge, int num_sge,
1899 struct ib_sge *sg_list)
1900{
1901 int i;
1902
1903 for (i = 0; i < num_sge; i++) {
1904 sge[i].lrkey = sg_list[i].lkey;
1905 sge[i].addr_lo = sg_list[i].addr;
1906 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
1907 sge[i].len = sg_list[i].length;
1908 hdr->total_len += sg_list[i].length;
1909 }
1910 if (num_sge == 0)
1911 memset(sge, 0, sizeof(*sge));
1912}
1913
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301914static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
1915{
1916 uint32_t total_len = 0, i;
1917
1918 for (i = 0; i < num_sge; i++)
1919 total_len += sg_list[i].length;
1920 return total_len;
1921}
1922
1923
Parav Panditfe2caef2012-03-21 04:09:06 +05301924static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
1925 struct ocrdma_hdr_wqe *hdr,
1926 struct ocrdma_sge *sge,
Bart Van Asschef696bf62018-07-18 09:25:14 -07001927 const struct ib_send_wr *wr, u32 wqe_size)
Parav Panditfe2caef2012-03-21 04:09:06 +05301928{
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301929 int i;
1930 char *dpp_addr;
1931
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301932 if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301933 hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
1934 if (unlikely(hdr->total_len > qp->max_inline_data)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001935 pr_err("%s() supported_len=0x%x,\n"
Masanari Iida1a84db52014-08-29 23:37:33 +09001936 " unsupported len req=0x%x\n", __func__,
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301937 qp->max_inline_data, hdr->total_len);
Parav Panditfe2caef2012-03-21 04:09:06 +05301938 return -EINVAL;
1939 }
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301940 dpp_addr = (char *)sge;
1941 for (i = 0; i < wr->num_sge; i++) {
1942 memcpy(dpp_addr,
1943 (void *)(unsigned long)wr->sg_list[i].addr,
1944 wr->sg_list[i].length);
1945 dpp_addr += wr->sg_list[i].length;
1946 }
1947
Parav Panditfe2caef2012-03-21 04:09:06 +05301948 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301949 if (0 == hdr->total_len)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301950 wqe_size += sizeof(struct ocrdma_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301951 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
1952 } else {
1953 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1954 if (wr->num_sge)
1955 wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
1956 else
1957 wqe_size += sizeof(struct ocrdma_sge);
1958 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1959 }
1960 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
1961 return 0;
1962}
1963
1964static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07001965 const struct ib_send_wr *wr)
Parav Panditfe2caef2012-03-21 04:09:06 +05301966{
1967 int status;
1968 struct ocrdma_sge *sge;
1969 u32 wqe_size = sizeof(*hdr);
1970
1971 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
1972 ocrdma_build_ud_hdr(qp, hdr, wr);
1973 sge = (struct ocrdma_sge *)(hdr + 2);
1974 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301975 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301976 sge = (struct ocrdma_sge *)(hdr + 1);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301977 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301978
1979 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1980 return status;
1981}
1982
1983static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07001984 const struct ib_send_wr *wr)
Parav Panditfe2caef2012-03-21 04:09:06 +05301985{
1986 int status;
1987 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
1988 struct ocrdma_sge *sge = ext_rw + 1;
1989 u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
1990
1991 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1992 if (status)
1993 return status;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001994 ext_rw->addr_lo = rdma_wr(wr)->remote_addr;
1995 ext_rw->addr_hi = upper_32_bits(rdma_wr(wr)->remote_addr);
1996 ext_rw->lrkey = rdma_wr(wr)->rkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05301997 ext_rw->len = hdr->total_len;
1998 return 0;
1999}
2000
2001static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07002002 const struct ib_send_wr *wr)
Parav Panditfe2caef2012-03-21 04:09:06 +05302003{
2004 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2005 struct ocrdma_sge *sge = ext_rw + 1;
2006 u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
2007 sizeof(struct ocrdma_hdr_wqe);
2008
2009 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2010 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2011 hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
2012 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2013
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002014 ext_rw->addr_lo = rdma_wr(wr)->remote_addr;
2015 ext_rw->addr_hi = upper_32_bits(rdma_wr(wr)->remote_addr);
2016 ext_rw->lrkey = rdma_wr(wr)->rkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05302017 ext_rw->len = hdr->total_len;
2018}
2019
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302020static int get_encoded_page_size(int pg_sz)
2021{
2022 /* Max size is 256M 4096 << 16 */
2023 int i = 0;
2024 for (; i < 17; i++)
2025 if (pg_sz == (4096 << i))
2026 break;
2027 return i;
2028}
2029
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002030static int ocrdma_build_reg(struct ocrdma_qp *qp,
2031 struct ocrdma_hdr_wqe *hdr,
Bart Van Asschef696bf62018-07-18 09:25:14 -07002032 const struct ib_reg_wr *wr)
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002033{
2034 u64 fbo;
2035 struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
2036 struct ocrdma_mr *mr = get_ocrdma_mr(wr->mr);
2037 struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
2038 struct ocrdma_pbe *pbe;
2039 u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
2040 int num_pbes = 0, i;
2041
2042 wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
2043
2044 hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
2045 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2046
2047 if (wr->access & IB_ACCESS_LOCAL_WRITE)
2048 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
2049 if (wr->access & IB_ACCESS_REMOTE_WRITE)
2050 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
2051 if (wr->access & IB_ACCESS_REMOTE_READ)
2052 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
2053 hdr->lkey = wr->key;
2054 hdr->total_len = mr->ibmr.length;
2055
2056 fbo = mr->ibmr.iova - mr->pages[0];
2057
2058 fast_reg->va_hi = upper_32_bits(mr->ibmr.iova);
2059 fast_reg->va_lo = (u32) (mr->ibmr.iova & 0xffffffff);
2060 fast_reg->fbo_hi = upper_32_bits(fbo);
2061 fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
2062 fast_reg->num_sges = mr->npages;
2063 fast_reg->size_sge = get_encoded_page_size(mr->ibmr.page_size);
2064
2065 pbe = pbl_tbl->va;
2066 for (i = 0; i < mr->npages; i++) {
2067 u64 buf_addr = mr->pages[i];
2068
2069 pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
2070 pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
2071 num_pbes += 1;
2072 pbe++;
2073
2074 /* if the pbl is full storing the pbes,
2075 * move to next pbl.
2076 */
2077 if (num_pbes == (mr->hwmr.pbl_size/sizeof(u64))) {
2078 pbl_tbl++;
2079 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2080 }
2081 }
2082
2083 return 0;
2084}
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302085
Parav Panditfe2caef2012-03-21 04:09:06 +05302086static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
2087{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302088 u32 val = qp->sq.dbid | (1 << OCRDMA_DB_SQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302089
2090 iowrite32(val, qp->sq_db);
2091}
2092
Bart Van Assched34ac5c2018-07-18 09:25:32 -07002093int ocrdma_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
2094 const struct ib_send_wr **bad_wr)
Parav Panditfe2caef2012-03-21 04:09:06 +05302095{
2096 int status = 0;
2097 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2098 struct ocrdma_hdr_wqe *hdr;
2099 unsigned long flags;
2100
2101 spin_lock_irqsave(&qp->q_lock, flags);
2102 if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
2103 spin_unlock_irqrestore(&qp->q_lock, flags);
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002104 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302105 return -EINVAL;
2106 }
2107
2108 while (wr) {
Mitesh Ahujaf252b5d2014-06-10 19:32:20 +05302109 if (qp->qp_type == IB_QPT_UD &&
2110 (wr->opcode != IB_WR_SEND &&
2111 wr->opcode != IB_WR_SEND_WITH_IMM)) {
2112 *bad_wr = wr;
2113 status = -EINVAL;
2114 break;
2115 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302116 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
2117 wr->num_sge > qp->sq.max_sges) {
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002118 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302119 status = -ENOMEM;
2120 break;
2121 }
2122 hdr = ocrdma_hwq_head(&qp->sq);
2123 hdr->cw = 0;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302124 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302125 hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2126 if (wr->send_flags & IB_SEND_FENCE)
2127 hdr->cw |=
2128 (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
2129 if (wr->send_flags & IB_SEND_SOLICITED)
2130 hdr->cw |=
2131 (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
2132 hdr->total_len = 0;
2133 switch (wr->opcode) {
2134 case IB_WR_SEND_WITH_IMM:
2135 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2136 hdr->immdt = ntohl(wr->ex.imm_data);
Bart Van Assche705dec32017-10-11 10:49:14 -07002137 /* fall through */
Parav Panditfe2caef2012-03-21 04:09:06 +05302138 case IB_WR_SEND:
2139 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2140 ocrdma_build_send(qp, hdr, wr);
2141 break;
2142 case IB_WR_SEND_WITH_INV:
2143 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
2144 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2145 hdr->lkey = wr->ex.invalidate_rkey;
2146 status = ocrdma_build_send(qp, hdr, wr);
2147 break;
2148 case IB_WR_RDMA_WRITE_WITH_IMM:
2149 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2150 hdr->immdt = ntohl(wr->ex.imm_data);
Bart Van Assche705dec32017-10-11 10:49:14 -07002151 /* fall through */
Parav Panditfe2caef2012-03-21 04:09:06 +05302152 case IB_WR_RDMA_WRITE:
2153 hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
2154 status = ocrdma_build_write(qp, hdr, wr);
2155 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302156 case IB_WR_RDMA_READ:
2157 ocrdma_build_read(qp, hdr, wr);
2158 break;
2159 case IB_WR_LOCAL_INV:
2160 hdr->cw |=
2161 (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302162 hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
2163 sizeof(struct ocrdma_sge)) /
Parav Panditfe2caef2012-03-21 04:09:06 +05302164 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
2165 hdr->lkey = wr->ex.invalidate_rkey;
2166 break;
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002167 case IB_WR_REG_MR:
2168 status = ocrdma_build_reg(qp, hdr, reg_wr(wr));
2169 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302170 default:
2171 status = -EINVAL;
2172 break;
2173 }
2174 if (status) {
2175 *bad_wr = wr;
2176 break;
2177 }
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302178 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302179 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
2180 else
2181 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
2182 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
2183 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
2184 OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
2185 /* make sure wqe is written before adapter can access it */
2186 wmb();
2187 /* inform hw to start processing it */
2188 ocrdma_ring_sq_db(qp);
2189
2190 /* update pointer, counter for next wr */
2191 ocrdma_hwq_inc_head(&qp->sq);
2192 wr = wr->next;
2193 }
2194 spin_unlock_irqrestore(&qp->q_lock, flags);
2195 return status;
2196}
2197
2198static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
2199{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302200 u32 val = qp->rq.dbid | (1 << OCRDMA_DB_RQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302201
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302202 iowrite32(val, qp->rq_db);
Parav Panditfe2caef2012-03-21 04:09:06 +05302203}
2204
Bart Van Assched34ac5c2018-07-18 09:25:32 -07002205static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe,
2206 const struct ib_recv_wr *wr, u16 tag)
Parav Panditfe2caef2012-03-21 04:09:06 +05302207{
2208 u32 wqe_size = 0;
2209 struct ocrdma_sge *sge;
2210 if (wr->num_sge)
2211 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
2212 else
2213 wqe_size = sizeof(*sge) + sizeof(*rqe);
2214
2215 rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
2216 OCRDMA_WQE_SIZE_SHIFT);
2217 rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2218 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2219 rqe->total_len = 0;
2220 rqe->rsvd_tag = tag;
2221 sge = (struct ocrdma_sge *)(rqe + 1);
2222 ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
2223 ocrdma_cpu_to_le32(rqe, wqe_size);
2224}
2225
Bart Van Assched34ac5c2018-07-18 09:25:32 -07002226int ocrdma_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
2227 const struct ib_recv_wr **bad_wr)
Parav Panditfe2caef2012-03-21 04:09:06 +05302228{
2229 int status = 0;
2230 unsigned long flags;
2231 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2232 struct ocrdma_hdr_wqe *rqe;
2233
2234 spin_lock_irqsave(&qp->q_lock, flags);
2235 if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
2236 spin_unlock_irqrestore(&qp->q_lock, flags);
2237 *bad_wr = wr;
2238 return -EINVAL;
2239 }
2240 while (wr) {
2241 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
2242 wr->num_sge > qp->rq.max_sges) {
2243 *bad_wr = wr;
2244 status = -ENOMEM;
2245 break;
2246 }
2247 rqe = ocrdma_hwq_head(&qp->rq);
2248 ocrdma_build_rqe(rqe, wr, 0);
2249
2250 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
2251 /* make sure rqe is written before adapter can access it */
2252 wmb();
2253
2254 /* inform hw to start processing it */
2255 ocrdma_ring_rq_db(qp);
2256
2257 /* update pointer, counter for next wr */
2258 ocrdma_hwq_inc_head(&qp->rq);
2259 wr = wr->next;
2260 }
2261 spin_unlock_irqrestore(&qp->q_lock, flags);
2262 return status;
2263}
2264
2265/* cqe for srq's rqe can potentially arrive out of order.
2266 * index gives the entry in the shadow table where to store
2267 * the wr_id. tag/index is returned in cqe to reference back
2268 * for a given rqe.
2269 */
2270static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
2271{
2272 int row = 0;
2273 int indx = 0;
2274
2275 for (row = 0; row < srq->bit_fields_len; row++) {
2276 if (srq->idx_bit_fields[row]) {
2277 indx = ffs(srq->idx_bit_fields[row]);
2278 indx = (row * 32) + (indx - 1);
ssh10db287ec2016-12-24 21:50:06 +05302279 BUG_ON(indx >= srq->rq.max_cnt);
Parav Panditfe2caef2012-03-21 04:09:06 +05302280 ocrdma_srq_toggle_bit(srq, indx);
2281 break;
2282 }
2283 }
2284
ssh10db287ec2016-12-24 21:50:06 +05302285 BUG_ON(row == srq->bit_fields_len);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302286 return indx + 1; /* Use from index 1 */
Parav Panditfe2caef2012-03-21 04:09:06 +05302287}
2288
2289static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
2290{
2291 u32 val = srq->rq.dbid | (1 << 16);
2292
2293 iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
2294}
2295
Bart Van Assched34ac5c2018-07-18 09:25:32 -07002296int ocrdma_post_srq_recv(struct ib_srq *ibsrq, const struct ib_recv_wr *wr,
2297 const struct ib_recv_wr **bad_wr)
Parav Panditfe2caef2012-03-21 04:09:06 +05302298{
2299 int status = 0;
2300 unsigned long flags;
2301 struct ocrdma_srq *srq;
2302 struct ocrdma_hdr_wqe *rqe;
2303 u16 tag;
2304
2305 srq = get_ocrdma_srq(ibsrq);
2306
2307 spin_lock_irqsave(&srq->q_lock, flags);
2308 while (wr) {
2309 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
2310 wr->num_sge > srq->rq.max_sges) {
2311 status = -ENOMEM;
2312 *bad_wr = wr;
2313 break;
2314 }
2315 tag = ocrdma_srq_get_idx(srq);
2316 rqe = ocrdma_hwq_head(&srq->rq);
2317 ocrdma_build_rqe(rqe, wr, tag);
2318
2319 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
2320 /* make sure rqe is written before adapter can perform DMA */
2321 wmb();
2322 /* inform hw to start processing it */
2323 ocrdma_ring_srq_db(srq);
2324 /* update pointer, counter for next wr */
2325 ocrdma_hwq_inc_head(&srq->rq);
2326 wr = wr->next;
2327 }
2328 spin_unlock_irqrestore(&srq->q_lock, flags);
2329 return status;
2330}
2331
2332static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
2333{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302334 enum ib_wc_status ibwc_status;
Parav Panditfe2caef2012-03-21 04:09:06 +05302335
2336 switch (status) {
2337 case OCRDMA_CQE_GENERAL_ERR:
2338 ibwc_status = IB_WC_GENERAL_ERR;
2339 break;
2340 case OCRDMA_CQE_LOC_LEN_ERR:
2341 ibwc_status = IB_WC_LOC_LEN_ERR;
2342 break;
2343 case OCRDMA_CQE_LOC_QP_OP_ERR:
2344 ibwc_status = IB_WC_LOC_QP_OP_ERR;
2345 break;
2346 case OCRDMA_CQE_LOC_EEC_OP_ERR:
2347 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
2348 break;
2349 case OCRDMA_CQE_LOC_PROT_ERR:
2350 ibwc_status = IB_WC_LOC_PROT_ERR;
2351 break;
2352 case OCRDMA_CQE_WR_FLUSH_ERR:
2353 ibwc_status = IB_WC_WR_FLUSH_ERR;
2354 break;
2355 case OCRDMA_CQE_MW_BIND_ERR:
2356 ibwc_status = IB_WC_MW_BIND_ERR;
2357 break;
2358 case OCRDMA_CQE_BAD_RESP_ERR:
2359 ibwc_status = IB_WC_BAD_RESP_ERR;
2360 break;
2361 case OCRDMA_CQE_LOC_ACCESS_ERR:
2362 ibwc_status = IB_WC_LOC_ACCESS_ERR;
2363 break;
2364 case OCRDMA_CQE_REM_INV_REQ_ERR:
2365 ibwc_status = IB_WC_REM_INV_REQ_ERR;
2366 break;
2367 case OCRDMA_CQE_REM_ACCESS_ERR:
2368 ibwc_status = IB_WC_REM_ACCESS_ERR;
2369 break;
2370 case OCRDMA_CQE_REM_OP_ERR:
2371 ibwc_status = IB_WC_REM_OP_ERR;
2372 break;
2373 case OCRDMA_CQE_RETRY_EXC_ERR:
2374 ibwc_status = IB_WC_RETRY_EXC_ERR;
2375 break;
2376 case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2377 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2378 break;
2379 case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2380 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2381 break;
2382 case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2383 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2384 break;
2385 case OCRDMA_CQE_REM_ABORT_ERR:
2386 ibwc_status = IB_WC_REM_ABORT_ERR;
2387 break;
2388 case OCRDMA_CQE_INV_EECN_ERR:
2389 ibwc_status = IB_WC_INV_EECN_ERR;
2390 break;
2391 case OCRDMA_CQE_INV_EEC_STATE_ERR:
2392 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2393 break;
2394 case OCRDMA_CQE_FATAL_ERR:
2395 ibwc_status = IB_WC_FATAL_ERR;
2396 break;
2397 case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2398 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2399 break;
2400 default:
2401 ibwc_status = IB_WC_GENERAL_ERR;
2402 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002403 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302404 return ibwc_status;
2405}
2406
2407static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2408 u32 wqe_idx)
2409{
2410 struct ocrdma_hdr_wqe *hdr;
2411 struct ocrdma_sge *rw;
2412 int opcode;
2413
2414 hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2415
2416 ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2417 /* Undo the hdr->cw swap */
2418 opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2419 switch (opcode) {
2420 case OCRDMA_WRITE:
2421 ibwc->opcode = IB_WC_RDMA_WRITE;
2422 break;
2423 case OCRDMA_READ:
2424 rw = (struct ocrdma_sge *)(hdr + 1);
2425 ibwc->opcode = IB_WC_RDMA_READ;
2426 ibwc->byte_len = rw->len;
2427 break;
2428 case OCRDMA_SEND:
2429 ibwc->opcode = IB_WC_SEND;
2430 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302431 case OCRDMA_FR_MR:
Sagi Grimberg191cfed2015-10-13 19:11:44 +03002432 ibwc->opcode = IB_WC_REG_MR;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302433 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302434 case OCRDMA_LKEY_INV:
2435 ibwc->opcode = IB_WC_LOCAL_INV;
2436 break;
2437 default:
2438 ibwc->status = IB_WC_GENERAL_ERR;
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002439 pr_err("%s() invalid opcode received = 0x%x\n",
2440 __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
Parav Panditfe2caef2012-03-21 04:09:06 +05302441 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002442 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302443}
2444
2445static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2446 struct ocrdma_cqe *cqe)
2447{
2448 if (is_cqe_for_sq(cqe)) {
2449 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2450 cqe->flags_status_srcqpn) &
2451 ~OCRDMA_CQE_STATUS_MASK);
2452 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2453 cqe->flags_status_srcqpn) |
2454 (OCRDMA_CQE_WR_FLUSH_ERR <<
2455 OCRDMA_CQE_STATUS_SHIFT));
2456 } else {
2457 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2458 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2459 cqe->flags_status_srcqpn) &
2460 ~OCRDMA_CQE_UD_STATUS_MASK);
2461 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2462 cqe->flags_status_srcqpn) |
2463 (OCRDMA_CQE_WR_FLUSH_ERR <<
2464 OCRDMA_CQE_UD_STATUS_SHIFT));
2465 } else {
2466 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2467 cqe->flags_status_srcqpn) &
2468 ~OCRDMA_CQE_STATUS_MASK);
2469 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2470 cqe->flags_status_srcqpn) |
2471 (OCRDMA_CQE_WR_FLUSH_ERR <<
2472 OCRDMA_CQE_STATUS_SHIFT));
2473 }
2474 }
2475}
2476
2477static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2478 struct ocrdma_qp *qp, int status)
2479{
2480 bool expand = false;
2481
2482 ibwc->byte_len = 0;
2483 ibwc->qp = &qp->ibqp;
2484 ibwc->status = ocrdma_to_ibwc_err(status);
2485
2486 ocrdma_flush_qp(qp);
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05302487 ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05302488
2489 /* if wqe/rqe pending for which cqe needs to be returned,
2490 * trigger inflating it.
2491 */
2492 if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2493 expand = true;
2494 ocrdma_set_cqe_status_flushed(qp, cqe);
2495 }
2496 return expand;
2497}
2498
2499static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2500 struct ocrdma_qp *qp, int status)
2501{
2502 ibwc->opcode = IB_WC_RECV;
2503 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2504 ocrdma_hwq_inc_tail(&qp->rq);
2505
2506 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2507}
2508
2509static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2510 struct ocrdma_qp *qp, int status)
2511{
2512 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2513 ocrdma_hwq_inc_tail(&qp->sq);
2514
2515 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2516}
2517
2518
2519static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2520 struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2521 bool *polled, bool *stop)
2522{
2523 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302524 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302525 int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2526 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302527 if (status < OCRDMA_MAX_CQE_ERR)
2528 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302529
2530 /* when hw sq is empty, but rq is not empty, so we continue
2531 * to keep the cqe in order to get the cq event again.
2532 */
2533 if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2534 /* when cq for rq and sq is same, it is safe to return
2535 * flush cqe for RQEs.
2536 */
2537 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2538 *polled = true;
2539 status = OCRDMA_CQE_WR_FLUSH_ERR;
2540 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2541 } else {
2542 /* stop processing further cqe as this cqe is used for
2543 * triggering cq event on buddy cq of RQ.
2544 * When QP is destroyed, this cqe will be removed
2545 * from the cq's hardware q.
2546 */
2547 *polled = false;
2548 *stop = true;
2549 expand = false;
2550 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302551 } else if (is_hw_sq_empty(qp)) {
2552 /* Do nothing */
2553 expand = false;
2554 *polled = false;
2555 *stop = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302556 } else {
2557 *polled = true;
2558 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2559 }
2560 return expand;
2561}
2562
2563static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2564 struct ocrdma_cqe *cqe,
2565 struct ib_wc *ibwc, bool *polled)
2566{
2567 bool expand = false;
2568 int tail = qp->sq.tail;
2569 u32 wqe_idx;
2570
2571 if (!qp->wqe_wr_id_tbl[tail].signaled) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302572 *polled = false; /* WC cannot be consumed yet */
2573 } else {
2574 ibwc->status = IB_WC_SUCCESS;
2575 ibwc->wc_flags = 0;
2576 ibwc->qp = &qp->ibqp;
2577 ocrdma_update_wc(qp, ibwc, tail);
2578 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302579 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302580 wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
2581 OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
Parav Panditae3bca92012-08-17 14:45:33 +00002582 if (tail != wqe_idx)
2583 expand = true; /* Coalesced CQE can't be consumed yet */
2584
Parav Panditfe2caef2012-03-21 04:09:06 +05302585 ocrdma_hwq_inc_tail(&qp->sq);
2586 return expand;
2587}
2588
2589static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2590 struct ib_wc *ibwc, bool *polled, bool *stop)
2591{
2592 int status;
2593 bool expand;
2594
2595 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2596 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2597
2598 if (status == OCRDMA_CQE_SUCCESS)
2599 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2600 else
2601 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2602 return expand;
2603}
2604
Devesh Sharma6b062662016-01-28 08:59:57 -05002605static int ocrdma_update_ud_rcqe(struct ocrdma_dev *dev, struct ib_wc *ibwc,
2606 struct ocrdma_cqe *cqe)
Parav Panditfe2caef2012-03-21 04:09:06 +05302607{
2608 int status;
Devesh Sharma6b062662016-01-28 08:59:57 -05002609 u16 hdr_type = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302610
2611 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2612 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2613 ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2614 OCRDMA_CQE_SRCQP_MASK;
Selvin Xavieraff3ead2016-02-05 20:06:39 +05302615 ibwc->pkey_index = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302616 ibwc->wc_flags = IB_WC_GRH;
2617 ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
Devesh Sharma6b062662016-01-28 08:59:57 -05002618 OCRDMA_CQE_UD_XFER_LEN_SHIFT) &
2619 OCRDMA_CQE_UD_XFER_LEN_MASK;
2620
2621 if (ocrdma_is_udp_encap_supported(dev)) {
2622 hdr_type = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2623 OCRDMA_CQE_UD_L3TYPE_SHIFT) &
2624 OCRDMA_CQE_UD_L3TYPE_MASK;
2625 ibwc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
2626 ibwc->network_hdr_type = hdr_type;
2627 }
2628
Parav Panditfe2caef2012-03-21 04:09:06 +05302629 return status;
2630}
2631
2632static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2633 struct ocrdma_cqe *cqe,
2634 struct ocrdma_qp *qp)
2635{
2636 unsigned long flags;
2637 struct ocrdma_srq *srq;
2638 u32 wqe_idx;
2639
2640 srq = get_ocrdma_srq(qp->ibqp.srq);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302641 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302642 OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
ssh10db287ec2016-12-24 21:50:06 +05302643 BUG_ON(wqe_idx < 1);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302644
Parav Panditfe2caef2012-03-21 04:09:06 +05302645 ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2646 spin_lock_irqsave(&srq->q_lock, flags);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302647 ocrdma_srq_toggle_bit(srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05302648 spin_unlock_irqrestore(&srq->q_lock, flags);
2649 ocrdma_hwq_inc_tail(&srq->rq);
2650}
2651
2652static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2653 struct ib_wc *ibwc, bool *polled, bool *stop,
2654 int status)
2655{
2656 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302657 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2658
2659 if (status < OCRDMA_MAX_CQE_ERR)
2660 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302661
2662 /* when hw_rq is empty, but wq is not empty, so continue
2663 * to keep the cqe to get the cq event again.
2664 */
2665 if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2666 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2667 *polled = true;
2668 status = OCRDMA_CQE_WR_FLUSH_ERR;
2669 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2670 } else {
2671 *polled = false;
2672 *stop = true;
2673 expand = false;
2674 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302675 } else if (is_hw_rq_empty(qp)) {
2676 /* Do nothing */
2677 expand = false;
2678 *polled = false;
2679 *stop = false;
Parav Pandita3698a92012-06-11 16:39:20 +05302680 } else {
2681 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302682 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
Parav Pandita3698a92012-06-11 16:39:20 +05302683 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302684 return expand;
2685}
2686
2687static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2688 struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2689{
Devesh Sharma6b062662016-01-28 08:59:57 -05002690 struct ocrdma_dev *dev;
2691
2692 dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302693 ibwc->opcode = IB_WC_RECV;
2694 ibwc->qp = &qp->ibqp;
2695 ibwc->status = IB_WC_SUCCESS;
2696
2697 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
Devesh Sharma6b062662016-01-28 08:59:57 -05002698 ocrdma_update_ud_rcqe(dev, ibwc, cqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05302699 else
2700 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2701
2702 if (is_cqe_imm(cqe)) {
2703 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2704 ibwc->wc_flags |= IB_WC_WITH_IMM;
2705 } else if (is_cqe_wr_imm(cqe)) {
2706 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2707 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2708 ibwc->wc_flags |= IB_WC_WITH_IMM;
2709 } else if (is_cqe_invalidated(cqe)) {
2710 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2711 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2712 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302713 if (qp->ibqp.srq) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302714 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302715 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302716 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2717 ocrdma_hwq_inc_tail(&qp->rq);
2718 }
2719}
2720
2721static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2722 struct ib_wc *ibwc, bool *polled, bool *stop)
2723{
2724 int status;
2725 bool expand = false;
2726
2727 ibwc->wc_flags = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302728 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302729 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2730 OCRDMA_CQE_UD_STATUS_MASK) >>
2731 OCRDMA_CQE_UD_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302732 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302733 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2734 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302735 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302736
2737 if (status == OCRDMA_CQE_SUCCESS) {
2738 *polled = true;
2739 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2740 } else {
2741 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2742 status);
2743 }
2744 return expand;
2745}
2746
2747static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2748 u16 cur_getp)
2749{
2750 if (cq->phase_change) {
2751 if (cur_getp == 0)
2752 cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302753 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302754 /* clear valid bit */
2755 cqe->flags_status_srcqpn = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302756 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302757}
2758
2759static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2760 struct ib_wc *ibwc)
2761{
2762 u16 qpn = 0;
2763 int i = 0;
2764 bool expand = false;
2765 int polled_hw_cqes = 0;
2766 struct ocrdma_qp *qp = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302767 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302768 struct ocrdma_cqe *cqe;
2769 u16 cur_getp; bool polled = false; bool stop = false;
2770
2771 cur_getp = cq->getp;
2772 while (num_entries) {
2773 cqe = cq->va + cur_getp;
2774 /* check whether valid cqe or not */
2775 if (!is_cqe_valid(cq, cqe))
2776 break;
2777 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2778 /* ignore discarded cqe */
2779 if (qpn == 0)
2780 goto skip_cqe;
2781 qp = dev->qp_tbl[qpn];
2782 BUG_ON(qp == NULL);
2783
2784 if (is_cqe_for_sq(cqe)) {
2785 expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2786 &stop);
2787 } else {
2788 expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2789 &stop);
2790 }
2791 if (expand)
2792 goto expand_cqe;
2793 if (stop)
2794 goto stop_cqe;
2795 /* clear qpn to avoid duplicate processing by discard_cqe() */
2796 cqe->cmn.qpn = 0;
2797skip_cqe:
2798 polled_hw_cqes += 1;
2799 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2800 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2801expand_cqe:
2802 if (polled) {
2803 num_entries -= 1;
2804 i += 1;
2805 ibwc = ibwc + 1;
2806 polled = false;
2807 }
2808 }
2809stop_cqe:
2810 cq->getp = cur_getp;
Devesh Sharmab41f7852016-02-11 00:21:52 -05002811
2812 if (polled_hw_cqes)
2813 ocrdma_ring_cq_db(dev, cq->id, false, false, polled_hw_cqes);
Devesh Sharmaea617622014-02-04 11:56:54 +05302814
Parav Panditfe2caef2012-03-21 04:09:06 +05302815 return i;
2816}
2817
2818/* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2819static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2820 struct ocrdma_qp *qp, struct ib_wc *ibwc)
2821{
2822 int err_cqes = 0;
2823
2824 while (num_entries) {
2825 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2826 break;
2827 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2828 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2829 ocrdma_hwq_inc_tail(&qp->sq);
2830 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2831 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2832 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302833 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302834 return err_cqes;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302835 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302836 ibwc->byte_len = 0;
2837 ibwc->status = IB_WC_WR_FLUSH_ERR;
2838 ibwc = ibwc + 1;
2839 err_cqes += 1;
2840 num_entries -= 1;
2841 }
2842 return err_cqes;
2843}
2844
2845int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2846{
2847 int cqes_to_poll = num_entries;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302848 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2849 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302850 int num_os_cqe = 0, err_cqes = 0;
2851 struct ocrdma_qp *qp;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302852 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05302853
2854 /* poll cqes from adapter CQ */
2855 spin_lock_irqsave(&cq->cq_lock, flags);
2856 num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2857 spin_unlock_irqrestore(&cq->cq_lock, flags);
2858 cqes_to_poll -= num_os_cqe;
2859
2860 if (cqes_to_poll) {
2861 wc = wc + num_os_cqe;
2862 /* adapter returns single error cqe when qp moves to
2863 * error state. So insert error cqes with wc_status as
2864 * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2865 * respectively which uses this CQ.
2866 */
2867 spin_lock_irqsave(&dev->flush_q_lock, flags);
2868 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2869 if (cqes_to_poll == 0)
2870 break;
2871 err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2872 cqes_to_poll -= err_cqes;
2873 num_os_cqe += err_cqes;
2874 wc = wc + err_cqes;
2875 }
2876 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2877 }
2878 return num_os_cqe;
2879}
2880
2881int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2882{
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302883 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2884 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302885 u16 cq_id;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302886 unsigned long flags;
Devesh Sharmaea617622014-02-04 11:56:54 +05302887 bool arm_needed = false, sol_needed = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302888
Parav Panditfe2caef2012-03-21 04:09:06 +05302889 cq_id = cq->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05302890
2891 spin_lock_irqsave(&cq->cq_lock, flags);
2892 if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302893 arm_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302894 if (cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302895 sol_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302896
Devesh Sharmab41f7852016-02-11 00:21:52 -05002897 ocrdma_ring_cq_db(dev, cq_id, arm_needed, sol_needed, 0);
Parav Panditfe2caef2012-03-21 04:09:06 +05302898 spin_unlock_irqrestore(&cq->cq_lock, flags);
Devesh Sharmaea617622014-02-04 11:56:54 +05302899
Parav Panditfe2caef2012-03-21 04:09:06 +05302900 return 0;
2901}
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302902
Shamir Rabinovitchc4367a22019-03-31 19:10:05 +03002903struct ib_mr *ocrdma_alloc_mr(struct ib_pd *ibpd, enum ib_mr_type mr_type,
Gal Pressman42a3b152020-07-06 15:03:43 +03002904 u32 max_num_sg)
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302905{
2906 int status;
2907 struct ocrdma_mr *mr;
2908 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
2909 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
2910
Sagi Grimbergcacb7d52015-07-30 10:32:43 +03002911 if (mr_type != IB_MR_TYPE_MEM_REG)
2912 return ERR_PTR(-EINVAL);
2913
2914 if (max_num_sg > dev->attr.max_pages_per_frmr)
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302915 return ERR_PTR(-EINVAL);
2916
2917 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2918 if (!mr)
2919 return ERR_PTR(-ENOMEM);
2920
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002921 mr->pages = kcalloc(max_num_sg, sizeof(u64), GFP_KERNEL);
2922 if (!mr->pages) {
2923 status = -ENOMEM;
2924 goto pl_err;
2925 }
2926
Sagi Grimbergcacb7d52015-07-30 10:32:43 +03002927 status = ocrdma_get_pbl_info(dev, mr, max_num_sg);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302928 if (status)
2929 goto pbl_err;
2930 mr->hwmr.fr_mr = 1;
2931 mr->hwmr.remote_rd = 0;
2932 mr->hwmr.remote_wr = 0;
2933 mr->hwmr.local_rd = 0;
2934 mr->hwmr.local_wr = 0;
2935 mr->hwmr.mw_bind = 0;
2936 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
2937 if (status)
2938 goto pbl_err;
2939 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
2940 if (status)
2941 goto mbx_err;
2942 mr->ibmr.rkey = mr->hwmr.lkey;
2943 mr->ibmr.lkey = mr->hwmr.lkey;
Roland Dreier7a1e89d2014-03-17 23:14:17 -07002944 dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] =
2945 (unsigned long) mr;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302946 return &mr->ibmr;
2947mbx_err:
2948 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
2949pbl_err:
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002950 kfree(mr->pages);
2951pl_err:
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302952 kfree(mr);
2953 return ERR_PTR(-ENOMEM);
2954}
2955
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002956static int ocrdma_set_page(struct ib_mr *ibmr, u64 addr)
2957{
2958 struct ocrdma_mr *mr = get_ocrdma_mr(ibmr);
2959
2960 if (unlikely(mr->npages == mr->hwmr.num_pbes))
2961 return -ENOMEM;
2962
2963 mr->pages[mr->npages++] = addr;
2964
2965 return 0;
2966}
2967
Christoph Hellwigff2ba992016-05-03 18:01:04 +02002968int ocrdma_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
Bart Van Assche9aa8b322016-05-12 10:49:15 -07002969 unsigned int *sg_offset)
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002970{
2971 struct ocrdma_mr *mr = get_ocrdma_mr(ibmr);
2972
2973 mr->npages = 0;
2974
Christoph Hellwigff2ba992016-05-03 18:01:04 +02002975 return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, ocrdma_set_page);
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002976}