blob: 3757f1b8cbfc67641477b56a43114c06e0593b14 [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>
Parav Panditfe2caef2012-03-21 04:09:06 +053050
51#include "ocrdma.h"
52#include "ocrdma_hw.h"
53#include "ocrdma_verbs.h"
Leon Romanovskya7fe7382016-09-22 17:31:15 +030054#include <rdma/ocrdma-abi.h>
Parav Panditfe2caef2012-03-21 04:09:06 +053055
56int ocrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
57{
58 if (index > 1)
59 return -EINVAL;
60
61 *pkey = 0xffff;
62 return 0;
63}
64
65int ocrdma_query_gid(struct ib_device *ibdev, u8 port,
66 int index, union ib_gid *sgid)
67{
Somnath Koturcc369292015-07-30 18:33:31 +030068 int ret;
Parav Panditfe2caef2012-03-21 04:09:06 +053069 struct ocrdma_dev *dev;
70
71 dev = get_ocrdma_dev(ibdev);
72 memset(sgid, 0, sizeof(*sgid));
Dan Carpenter59a39ca32015-02-16 13:01:36 +030073 if (index >= OCRDMA_MAX_SGID)
Parav Panditfe2caef2012-03-21 04:09:06 +053074 return -EINVAL;
75
Matan Barak55ee3ab2015-10-15 18:38:45 +030076 ret = ib_get_cached_gid(ibdev, port, index, sgid, NULL);
Somnath Koturcc369292015-07-30 18:33:31 +030077 if (ret == -EAGAIN) {
78 memcpy(sgid, &zgid, sizeof(*sgid));
79 return 0;
80 }
Parav Panditfe2caef2012-03-21 04:09:06 +053081
Somnath Koturcc369292015-07-30 18:33:31 +030082 return ret;
83}
84
85int ocrdma_add_gid(struct ib_device *device,
86 u8 port_num,
87 unsigned int index,
88 const union ib_gid *gid,
89 const struct ib_gid_attr *attr,
90 void **context) {
91 return 0;
92}
93
94int ocrdma_del_gid(struct ib_device *device,
95 u8 port_num,
96 unsigned int index,
97 void **context) {
Parav Panditfe2caef2012-03-21 04:09:06 +053098 return 0;
99}
100
Matan Barak2528e332015-06-11 16:35:25 +0300101int ocrdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr,
102 struct ib_udata *uhw)
Parav Panditfe2caef2012-03-21 04:09:06 +0530103{
104 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
105
Matan Barak2528e332015-06-11 16:35:25 +0300106 if (uhw->inlen || uhw->outlen)
107 return -EINVAL;
108
Parav Panditfe2caef2012-03-21 04:09:06 +0530109 memset(attr, 0, sizeof *attr);
110 memcpy(&attr->fw_ver, &dev->attr.fw_ver[0],
111 min(sizeof(dev->attr.fw_ver), sizeof(attr->fw_ver)));
112 ocrdma_get_guid(dev, (u8 *)&attr->sys_image_guid);
Mitesh Ahuja033edd42014-06-10 19:32:22 +0530113 attr->max_mr_size = dev->attr.max_mr_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530114 attr->page_size_cap = 0xffff000;
115 attr->vendor_id = dev->nic_info.pdev->vendor;
116 attr->vendor_part_id = dev->nic_info.pdev->device;
Mitesh Ahuja96c51ab2014-07-02 11:36:06 +0530117 attr->hw_ver = dev->asic_id;
Parav Panditfe2caef2012-03-21 04:09:06 +0530118 attr->max_qp = dev->attr.max_qp;
Naresh Gottumukkalad3cb6c02013-08-26 15:27:40 +0530119 attr->max_ah = OCRDMA_MAX_AH;
Parav Panditfe2caef2012-03-21 04:09:06 +0530120 attr->max_qp_wr = dev->attr.max_wqe;
121
122 attr->device_cap_flags = IB_DEVICE_CURR_QP_STATE_MOD |
123 IB_DEVICE_RC_RNR_NAK_GEN |
124 IB_DEVICE_SHUTDOWN_PORT |
125 IB_DEVICE_SYS_IMAGE_GUID |
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +0530126 IB_DEVICE_LOCAL_DMA_LKEY |
127 IB_DEVICE_MEM_MGT_EXTENSIONS;
Selvin Xavier3c199b42016-08-24 01:17:41 -0400128 attr->max_sge = min(dev->attr.max_send_sge, dev->attr.max_recv_sge);
129 attr->max_sge_rd = dev->attr.max_rdma_sge;
Parav Panditfe2caef2012-03-21 04:09:06 +0530130 attr->max_cq = dev->attr.max_cq;
131 attr->max_cqe = dev->attr.max_cqe;
132 attr->max_mr = dev->attr.max_mr;
Selvin Xavierac578ae2014-02-04 11:57:04 +0530133 attr->max_mw = dev->attr.max_mw;
Parav Panditfe2caef2012-03-21 04:09:06 +0530134 attr->max_pd = dev->attr.max_pd;
135 attr->atomic_cap = 0;
136 attr->max_fmr = 0;
137 attr->max_map_per_fmr = 0;
138 attr->max_qp_rd_atom =
139 min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp);
140 attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +0530141 attr->max_srq = dev->attr.max_srq;
Roland Dreierd1e09eb2012-07-07 15:13:47 -0700142 attr->max_srq_sge = dev->attr.max_srq_sge;
Parav Panditfe2caef2012-03-21 04:09:06 +0530143 attr->max_srq_wr = dev->attr.max_rqe;
144 attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay;
Devesh Sharmad6a488f2014-06-09 10:52:37 +0530145 attr->max_fast_reg_page_list_len = dev->attr.max_pages_per_frmr;
Parav Panditfe2caef2012-03-21 04:09:06 +0530146 attr->max_pkeys = 1;
147 return 0;
148}
149
Somnath Koturcc369292015-07-30 18:33:31 +0300150struct net_device *ocrdma_get_netdev(struct ib_device *ibdev, u8 port_num)
151{
152 struct ocrdma_dev *dev;
153 struct net_device *ndev = NULL;
154
155 rcu_read_lock();
156
157 dev = get_ocrdma_dev(ibdev);
158 if (dev)
159 ndev = dev->nic_info.netdev;
160 if (ndev)
161 dev_hold(ndev);
162
163 rcu_read_unlock();
164
165 return ndev;
166}
167
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530168static inline void get_link_speed_and_width(struct ocrdma_dev *dev,
169 u8 *ib_speed, u8 *ib_width)
170{
171 int status;
172 u8 speed;
173
Devesh Sharma3b1ea432015-12-24 13:14:07 -0500174 status = ocrdma_mbx_get_link_speed(dev, &speed, NULL);
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530175 if (status)
176 speed = OCRDMA_PHYS_LINK_SPEED_ZERO;
177
178 switch (speed) {
179 case OCRDMA_PHYS_LINK_SPEED_1GBPS:
180 *ib_speed = IB_SPEED_SDR;
181 *ib_width = IB_WIDTH_1X;
182 break;
183
184 case OCRDMA_PHYS_LINK_SPEED_10GBPS:
185 *ib_speed = IB_SPEED_QDR;
186 *ib_width = IB_WIDTH_1X;
187 break;
188
189 case OCRDMA_PHYS_LINK_SPEED_20GBPS:
190 *ib_speed = IB_SPEED_DDR;
191 *ib_width = IB_WIDTH_4X;
192 break;
193
194 case OCRDMA_PHYS_LINK_SPEED_40GBPS:
195 *ib_speed = IB_SPEED_QDR;
196 *ib_width = IB_WIDTH_4X;
197 break;
198
199 default:
200 /* Unsupported */
201 *ib_speed = IB_SPEED_SDR;
202 *ib_width = IB_WIDTH_1X;
Joe Perches2b50176d2013-10-08 16:07:22 -0700203 }
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530204}
205
Parav Panditfe2caef2012-03-21 04:09:06 +0530206int ocrdma_query_port(struct ib_device *ibdev,
207 u8 port, struct ib_port_attr *props)
208{
209 enum ib_port_state port_state;
210 struct ocrdma_dev *dev;
211 struct net_device *netdev;
212
Or Gerlitzc4550c62017-01-24 13:02:39 +0200213 /* props being zeroed by the caller, avoid zeroing it here */
Parav Panditfe2caef2012-03-21 04:09:06 +0530214 dev = get_ocrdma_dev(ibdev);
215 if (port > 1) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000216 pr_err("%s(%d) invalid_port=0x%x\n", __func__,
217 dev->id, port);
Parav Panditfe2caef2012-03-21 04:09:06 +0530218 return -EINVAL;
219 }
220 netdev = dev->nic_info.netdev;
221 if (netif_running(netdev) && netif_oper_up(netdev)) {
222 port_state = IB_PORT_ACTIVE;
223 props->phys_state = 5;
224 } else {
225 port_state = IB_PORT_DOWN;
226 props->phys_state = 3;
227 }
228 props->max_mtu = IB_MTU_4096;
229 props->active_mtu = iboe_get_mtu(netdev->mtu);
230 props->lid = 0;
231 props->lmc = 0;
232 props->sm_lid = 0;
233 props->sm_sl = 0;
234 props->state = port_state;
235 props->port_cap_flags =
236 IB_PORT_CM_SUP |
237 IB_PORT_REINIT_SUP |
Somnath Koturcc369292015-07-30 18:33:31 +0300238 IB_PORT_DEVICE_MGMT_SUP | IB_PORT_VENDOR_CLASS_SUP |
239 IB_PORT_IP_BASED_GIDS;
Parav Panditfe2caef2012-03-21 04:09:06 +0530240 props->gid_tbl_len = OCRDMA_MAX_SGID;
241 props->pkey_tbl_len = 1;
242 props->bad_pkey_cntr = 0;
243 props->qkey_viol_cntr = 0;
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530244 get_link_speed_and_width(dev, &props->active_speed,
245 &props->active_width);
Parav Panditfe2caef2012-03-21 04:09:06 +0530246 props->max_msg_sz = 0x80000000;
247 props->max_vl_num = 4;
248 return 0;
249}
250
251int ocrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
252 struct ib_port_modify *props)
253{
254 struct ocrdma_dev *dev;
255
256 dev = get_ocrdma_dev(ibdev);
257 if (port > 1) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000258 pr_err("%s(%d) invalid_port=0x%x\n", __func__, dev->id, port);
Parav Panditfe2caef2012-03-21 04:09:06 +0530259 return -EINVAL;
260 }
261 return 0;
262}
263
264static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
265 unsigned long len)
266{
267 struct ocrdma_mm *mm;
268
269 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
270 if (mm == NULL)
271 return -ENOMEM;
272 mm->key.phy_addr = phy_addr;
273 mm->key.len = len;
274 INIT_LIST_HEAD(&mm->entry);
275
276 mutex_lock(&uctx->mm_list_lock);
277 list_add_tail(&mm->entry, &uctx->mm_head);
278 mutex_unlock(&uctx->mm_list_lock);
279 return 0;
280}
281
282static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
283 unsigned long len)
284{
285 struct ocrdma_mm *mm, *tmp;
286
287 mutex_lock(&uctx->mm_list_lock);
288 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530289 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
Parav Panditfe2caef2012-03-21 04:09:06 +0530290 continue;
291
292 list_del(&mm->entry);
293 kfree(mm);
294 break;
295 }
296 mutex_unlock(&uctx->mm_list_lock);
297}
298
299static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
300 unsigned long len)
301{
302 bool found = false;
303 struct ocrdma_mm *mm;
304
305 mutex_lock(&uctx->mm_list_lock);
306 list_for_each_entry(mm, &uctx->mm_head, entry) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530307 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
Parav Panditfe2caef2012-03-21 04:09:06 +0530308 continue;
309
310 found = true;
311 break;
312 }
313 mutex_unlock(&uctx->mm_list_lock);
314 return found;
315}
316
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530317
318static u16 _ocrdma_pd_mgr_get_bitmap(struct ocrdma_dev *dev, bool dpp_pool)
319{
320 u16 pd_bitmap_idx = 0;
321 const unsigned long *pd_bitmap;
322
323 if (dpp_pool) {
324 pd_bitmap = dev->pd_mgr->pd_dpp_bitmap;
325 pd_bitmap_idx = find_first_zero_bit(pd_bitmap,
326 dev->pd_mgr->max_dpp_pd);
327 __set_bit(pd_bitmap_idx, dev->pd_mgr->pd_dpp_bitmap);
328 dev->pd_mgr->pd_dpp_count++;
329 if (dev->pd_mgr->pd_dpp_count > dev->pd_mgr->pd_dpp_thrsh)
330 dev->pd_mgr->pd_dpp_thrsh = dev->pd_mgr->pd_dpp_count;
331 } else {
332 pd_bitmap = dev->pd_mgr->pd_norm_bitmap;
333 pd_bitmap_idx = find_first_zero_bit(pd_bitmap,
334 dev->pd_mgr->max_normal_pd);
335 __set_bit(pd_bitmap_idx, dev->pd_mgr->pd_norm_bitmap);
336 dev->pd_mgr->pd_norm_count++;
337 if (dev->pd_mgr->pd_norm_count > dev->pd_mgr->pd_norm_thrsh)
338 dev->pd_mgr->pd_norm_thrsh = dev->pd_mgr->pd_norm_count;
339 }
340 return pd_bitmap_idx;
341}
342
343static int _ocrdma_pd_mgr_put_bitmap(struct ocrdma_dev *dev, u16 pd_id,
344 bool dpp_pool)
345{
346 u16 pd_count;
347 u16 pd_bit_index;
348
349 pd_count = dpp_pool ? dev->pd_mgr->pd_dpp_count :
350 dev->pd_mgr->pd_norm_count;
351 if (pd_count == 0)
352 return -EINVAL;
353
354 if (dpp_pool) {
355 pd_bit_index = pd_id - dev->pd_mgr->pd_dpp_start;
356 if (pd_bit_index >= dev->pd_mgr->max_dpp_pd) {
357 return -EINVAL;
358 } else {
359 __clear_bit(pd_bit_index, dev->pd_mgr->pd_dpp_bitmap);
360 dev->pd_mgr->pd_dpp_count--;
361 }
362 } else {
363 pd_bit_index = pd_id - dev->pd_mgr->pd_norm_start;
364 if (pd_bit_index >= dev->pd_mgr->max_normal_pd) {
365 return -EINVAL;
366 } else {
367 __clear_bit(pd_bit_index, dev->pd_mgr->pd_norm_bitmap);
368 dev->pd_mgr->pd_norm_count--;
369 }
370 }
371
372 return 0;
373}
374
Dan Carpenter004d18e2017-02-23 13:40:16 +0300375static int ocrdma_put_pd_num(struct ocrdma_dev *dev, u16 pd_id,
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530376 bool dpp_pool)
377{
378 int status;
379
380 mutex_lock(&dev->dev_lock);
381 status = _ocrdma_pd_mgr_put_bitmap(dev, pd_id, dpp_pool);
382 mutex_unlock(&dev->dev_lock);
383 return status;
384}
385
386static int ocrdma_get_pd_num(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
387{
388 u16 pd_idx = 0;
389 int status = 0;
390
391 mutex_lock(&dev->dev_lock);
392 if (pd->dpp_enabled) {
393 /* try allocating DPP PD, if not available then normal PD */
394 if (dev->pd_mgr->pd_dpp_count < dev->pd_mgr->max_dpp_pd) {
395 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, true);
396 pd->id = dev->pd_mgr->pd_dpp_start + pd_idx;
397 pd->dpp_page = dev->pd_mgr->dpp_page_index + pd_idx;
398 } else if (dev->pd_mgr->pd_norm_count <
399 dev->pd_mgr->max_normal_pd) {
400 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, false);
401 pd->id = dev->pd_mgr->pd_norm_start + pd_idx;
402 pd->dpp_enabled = false;
403 } else {
404 status = -EINVAL;
405 }
406 } else {
407 if (dev->pd_mgr->pd_norm_count < dev->pd_mgr->max_normal_pd) {
408 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, false);
409 pd->id = dev->pd_mgr->pd_norm_start + pd_idx;
410 } else {
411 status = -EINVAL;
412 }
413 }
414 mutex_unlock(&dev->dev_lock);
415 return status;
416}
417
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530418static struct ocrdma_pd *_ocrdma_alloc_pd(struct ocrdma_dev *dev,
419 struct ocrdma_ucontext *uctx,
420 struct ib_udata *udata)
421{
422 struct ocrdma_pd *pd = NULL;
Markus Elfring0ca4c392015-12-26 18:18:18 +0100423 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530424
425 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
426 if (!pd)
427 return ERR_PTR(-ENOMEM);
428
Mitesh Ahuja59582d82015-05-19 11:32:37 +0530429 if (udata && uctx && dev->attr.max_dpp_pds) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530430 pd->dpp_enabled =
Devesh Sharma21c33912014-02-04 11:56:56 +0530431 ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530432 pd->num_dpp_qp =
Devesh Sharmaa53d77a2014-06-10 19:32:17 +0530433 pd->dpp_enabled ? (dev->nic_info.db_page_size /
434 dev->attr.wqe_size) : 0;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530435 }
436
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530437 if (dev->pd_mgr->pd_prealloc_valid) {
438 status = ocrdma_get_pd_num(dev, pd);
Roland Dreier18eaf1f2015-05-29 23:10:31 -0700439 if (status == 0) {
440 return pd;
441 } else {
442 kfree(pd);
443 return ERR_PTR(status);
444 }
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530445 }
446
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530447retry:
448 status = ocrdma_mbx_alloc_pd(dev, pd);
449 if (status) {
450 if (pd->dpp_enabled) {
451 pd->dpp_enabled = false;
452 pd->num_dpp_qp = 0;
453 goto retry;
454 } else {
455 kfree(pd);
456 return ERR_PTR(status);
457 }
458 }
459
460 return pd;
461}
462
463static inline int is_ucontext_pd(struct ocrdma_ucontext *uctx,
464 struct ocrdma_pd *pd)
465{
466 return (uctx->cntxt_pd == pd ? true : false);
467}
468
469static int _ocrdma_dealloc_pd(struct ocrdma_dev *dev,
470 struct ocrdma_pd *pd)
471{
Markus Elfring0ca4c392015-12-26 18:18:18 +0100472 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530473
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530474 if (dev->pd_mgr->pd_prealloc_valid)
475 status = ocrdma_put_pd_num(dev, pd->id, pd->dpp_enabled);
476 else
477 status = ocrdma_mbx_dealloc_pd(dev, pd);
478
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530479 kfree(pd);
480 return status;
481}
482
483static int ocrdma_alloc_ucontext_pd(struct ocrdma_dev *dev,
484 struct ocrdma_ucontext *uctx,
485 struct ib_udata *udata)
486{
487 int status = 0;
488
489 uctx->cntxt_pd = _ocrdma_alloc_pd(dev, uctx, udata);
490 if (IS_ERR(uctx->cntxt_pd)) {
491 status = PTR_ERR(uctx->cntxt_pd);
492 uctx->cntxt_pd = NULL;
493 goto err;
494 }
495
496 uctx->cntxt_pd->uctx = uctx;
497 uctx->cntxt_pd->ibpd.device = &dev->ibdev;
498err:
499 return status;
500}
501
502static int ocrdma_dealloc_ucontext_pd(struct ocrdma_ucontext *uctx)
503{
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530504 struct ocrdma_pd *pd = uctx->cntxt_pd;
505 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
506
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530507 if (uctx->pd_in_use) {
508 pr_err("%s(%d) Freeing in use pdid=0x%x.\n",
509 __func__, dev->id, pd->id);
510 }
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530511 uctx->cntxt_pd = NULL;
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530512 (void)_ocrdma_dealloc_pd(dev, pd);
513 return 0;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530514}
515
516static struct ocrdma_pd *ocrdma_get_ucontext_pd(struct ocrdma_ucontext *uctx)
517{
518 struct ocrdma_pd *pd = NULL;
519
520 mutex_lock(&uctx->mm_list_lock);
521 if (!uctx->pd_in_use) {
522 uctx->pd_in_use = true;
523 pd = uctx->cntxt_pd;
524 }
525 mutex_unlock(&uctx->mm_list_lock);
526
527 return pd;
528}
529
530static void ocrdma_release_ucontext_pd(struct ocrdma_ucontext *uctx)
531{
532 mutex_lock(&uctx->mm_list_lock);
533 uctx->pd_in_use = false;
534 mutex_unlock(&uctx->mm_list_lock);
535}
536
Parav Panditfe2caef2012-03-21 04:09:06 +0530537struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
538 struct ib_udata *udata)
539{
540 int status;
541 struct ocrdma_ucontext *ctx;
542 struct ocrdma_alloc_ucontext_resp resp;
543 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
544 struct pci_dev *pdev = dev->nic_info.pdev;
545 u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
546
547 if (!udata)
548 return ERR_PTR(-EFAULT);
549 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
550 if (!ctx)
551 return ERR_PTR(-ENOMEM);
Parav Panditfe2caef2012-03-21 04:09:06 +0530552 INIT_LIST_HEAD(&ctx->mm_head);
553 mutex_init(&ctx->mm_list_lock);
554
555 ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
556 &ctx->ah_tbl.pa, GFP_KERNEL);
557 if (!ctx->ah_tbl.va) {
558 kfree(ctx);
559 return ERR_PTR(-ENOMEM);
560 }
561 memset(ctx->ah_tbl.va, 0, map_len);
562 ctx->ah_tbl.len = map_len;
563
Dan Carpenter63ea3742013-07-29 22:34:29 +0300564 memset(&resp, 0, sizeof(resp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530565 resp.ah_tbl_len = ctx->ah_tbl.len;
Devesh Sharma1b76d382014-09-05 19:35:40 +0530566 resp.ah_tbl_page = virt_to_phys(ctx->ah_tbl.va);
Parav Panditfe2caef2012-03-21 04:09:06 +0530567
568 status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
569 if (status)
570 goto map_err;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530571
572 status = ocrdma_alloc_ucontext_pd(dev, ctx, udata);
573 if (status)
574 goto pd_err;
575
Parav Panditfe2caef2012-03-21 04:09:06 +0530576 resp.dev_id = dev->id;
577 resp.max_inline_data = dev->attr.max_inline_data;
578 resp.wqe_size = dev->attr.wqe_size;
579 resp.rqe_size = dev->attr.rqe_size;
580 resp.dpp_wqe_size = dev->attr.wqe_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530581
582 memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
583 status = ib_copy_to_udata(udata, &resp, sizeof(resp));
584 if (status)
585 goto cpy_err;
586 return &ctx->ibucontext;
587
588cpy_err:
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530589pd_err:
Parav Panditfe2caef2012-03-21 04:09:06 +0530590 ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
591map_err:
592 dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
593 ctx->ah_tbl.pa);
594 kfree(ctx);
595 return ERR_PTR(status);
596}
597
598int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
599{
Markus Elfring0ca4c392015-12-26 18:18:18 +0100600 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530601 struct ocrdma_mm *mm, *tmp;
602 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530603 struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
604 struct pci_dev *pdev = dev->nic_info.pdev;
Parav Panditfe2caef2012-03-21 04:09:06 +0530605
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530606 status = ocrdma_dealloc_ucontext_pd(uctx);
607
Parav Panditfe2caef2012-03-21 04:09:06 +0530608 ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
609 dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
610 uctx->ah_tbl.pa);
611
612 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
613 list_del(&mm->entry);
614 kfree(mm);
615 }
616 kfree(uctx);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530617 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530618}
619
620int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
621{
622 struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530623 struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530624 unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
625 u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
626 unsigned long len = (vma->vm_end - vma->vm_start);
Markus Elfring0ca4c392015-12-26 18:18:18 +0100627 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530628 bool found;
629
630 if (vma->vm_start & (PAGE_SIZE - 1))
631 return -EINVAL;
632 found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
633 if (!found)
634 return -EINVAL;
635
636 if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
637 dev->nic_info.db_total_size)) &&
638 (len <= dev->nic_info.db_page_size)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530639 if (vma->vm_flags & VM_READ)
640 return -EPERM;
641
642 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Parav Panditfe2caef2012-03-21 04:09:06 +0530643 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
644 len, vma->vm_page_prot);
645 } else if (dev->nic_info.dpp_unmapped_len &&
646 (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
647 (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
648 dev->nic_info.dpp_unmapped_len)) &&
649 (len <= dev->nic_info.dpp_unmapped_len)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530650 if (vma->vm_flags & VM_READ)
651 return -EPERM;
652
Parav Panditfe2caef2012-03-21 04:09:06 +0530653 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
654 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
655 len, vma->vm_page_prot);
656 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +0530657 status = remap_pfn_range(vma, vma->vm_start,
658 vma->vm_pgoff, len, vma->vm_page_prot);
659 }
660 return status;
661}
662
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530663static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
Parav Panditfe2caef2012-03-21 04:09:06 +0530664 struct ib_ucontext *ib_ctx,
665 struct ib_udata *udata)
666{
667 int status;
668 u64 db_page_addr;
Roland Dreierda496432012-04-16 11:32:17 -0700669 u64 dpp_page_addr = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530670 u32 db_page_size;
671 struct ocrdma_alloc_pd_uresp rsp;
672 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
673
Dan Carpenter63ea3742013-07-29 22:34:29 +0300674 memset(&rsp, 0, sizeof(rsp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530675 rsp.id = pd->id;
676 rsp.dpp_enabled = pd->dpp_enabled;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530677 db_page_addr = ocrdma_get_db_addr(dev, pd->id);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530678 db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530679
680 status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
681 if (status)
682 return status;
683
684 if (pd->dpp_enabled) {
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530685 dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530686 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530687 status = ocrdma_add_mmap(uctx, dpp_page_addr,
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530688 PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530689 if (status)
690 goto dpp_map_err;
691 rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
692 rsp.dpp_page_addr_lo = dpp_page_addr;
693 }
694
695 status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
696 if (status)
697 goto ucopy_err;
698
699 pd->uctx = uctx;
700 return 0;
701
702ucopy_err:
Roland Dreierda496432012-04-16 11:32:17 -0700703 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530704 ocrdma_del_mmap(pd->uctx, dpp_page_addr, PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530705dpp_map_err:
706 ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
707 return status;
708}
709
710struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev,
711 struct ib_ucontext *context,
712 struct ib_udata *udata)
713{
714 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
715 struct ocrdma_pd *pd;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530716 struct ocrdma_ucontext *uctx = NULL;
Parav Panditfe2caef2012-03-21 04:09:06 +0530717 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530718 u8 is_uctx_pd = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530719
Parav Panditfe2caef2012-03-21 04:09:06 +0530720 if (udata && context) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530721 uctx = get_ocrdma_ucontext(context);
722 pd = ocrdma_get_ucontext_pd(uctx);
723 if (pd) {
724 is_uctx_pd = true;
725 goto pd_mapping;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530726 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530727 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530728
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530729 pd = _ocrdma_alloc_pd(dev, uctx, udata);
730 if (IS_ERR(pd)) {
731 status = PTR_ERR(pd);
732 goto exit;
733 }
734
735pd_mapping:
Parav Panditfe2caef2012-03-21 04:09:06 +0530736 if (udata && context) {
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530737 status = ocrdma_copy_pd_uresp(dev, pd, context, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +0530738 if (status)
739 goto err;
740 }
741 return &pd->ibpd;
742
743err:
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530744 if (is_uctx_pd) {
745 ocrdma_release_ucontext_pd(uctx);
746 } else {
Dan Carpenterdd75cfa2017-07-13 10:46:14 +0300747 if (_ocrdma_dealloc_pd(dev, pd))
748 pr_err("%s: _ocrdma_dealloc_pd() failed\n", __func__);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530749 }
750exit:
Parav Panditfe2caef2012-03-21 04:09:06 +0530751 return ERR_PTR(status);
752}
753
754int ocrdma_dealloc_pd(struct ib_pd *ibpd)
755{
756 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530757 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530758 struct ocrdma_ucontext *uctx = NULL;
759 int status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530760 u64 usr_db;
761
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530762 uctx = pd->uctx;
763 if (uctx) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530764 u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530765 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530766 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530767 ocrdma_del_mmap(pd->uctx, dpp_db, PAGE_SIZE);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530768 usr_db = ocrdma_get_db_addr(dev, pd->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530769 ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530770
771 if (is_ucontext_pd(uctx, pd)) {
772 ocrdma_release_ucontext_pd(uctx);
773 return status;
774 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530775 }
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530776 status = _ocrdma_dealloc_pd(dev, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530777 return status;
778}
779
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530780static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
781 u32 pdid, int acc, u32 num_pbls, u32 addr_check)
Parav Panditfe2caef2012-03-21 04:09:06 +0530782{
783 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530784
Parav Panditfe2caef2012-03-21 04:09:06 +0530785 mr->hwmr.fr_mr = 0;
786 mr->hwmr.local_rd = 1;
787 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
788 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
789 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
790 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
791 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
792 mr->hwmr.num_pbls = num_pbls;
793
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530794 status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
795 if (status)
796 return status;
797
Parav Panditfe2caef2012-03-21 04:09:06 +0530798 mr->ibmr.lkey = mr->hwmr.lkey;
799 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
800 mr->ibmr.rkey = mr->hwmr.lkey;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530801 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530802}
803
804struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
805{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530806 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530807 struct ocrdma_mr *mr;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530808 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
809 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530810
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530811 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
812 pr_err("%s err, invalid access rights\n", __func__);
813 return ERR_PTR(-EINVAL);
814 }
815
816 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
817 if (!mr)
818 return ERR_PTR(-ENOMEM);
819
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530820 status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530821 OCRDMA_ADDR_CHECK_DISABLE);
822 if (status) {
823 kfree(mr);
824 return ERR_PTR(status);
825 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530826
827 return &mr->ibmr;
828}
829
830static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
831 struct ocrdma_hw_mr *mr)
832{
833 struct pci_dev *pdev = dev->nic_info.pdev;
834 int i = 0;
835
836 if (mr->pbl_table) {
837 for (i = 0; i < mr->num_pbls; i++) {
838 if (!mr->pbl_table[i].va)
839 continue;
840 dma_free_coherent(&pdev->dev, mr->pbl_size,
841 mr->pbl_table[i].va,
842 mr->pbl_table[i].pa);
843 }
844 kfree(mr->pbl_table);
845 mr->pbl_table = NULL;
846 }
847}
848
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530849static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
850 u32 num_pbes)
Parav Panditfe2caef2012-03-21 04:09:06 +0530851{
852 u32 num_pbls = 0;
853 u32 idx = 0;
854 int status = 0;
855 u32 pbl_size;
856
857 do {
858 pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
859 if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
860 status = -EFAULT;
861 break;
862 }
863 num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
864 num_pbls = num_pbls / (pbl_size / sizeof(u64));
865 idx++;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530866 } while (num_pbls >= dev->attr.max_num_mr_pbl);
Parav Panditfe2caef2012-03-21 04:09:06 +0530867
868 mr->hwmr.num_pbes = num_pbes;
869 mr->hwmr.num_pbls = num_pbls;
870 mr->hwmr.pbl_size = pbl_size;
871 return status;
872}
873
874static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
875{
876 int status = 0;
877 int i;
878 u32 dma_len = mr->pbl_size;
879 struct pci_dev *pdev = dev->nic_info.pdev;
880 void *va;
881 dma_addr_t pa;
882
883 mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) *
884 mr->num_pbls, GFP_KERNEL);
885
886 if (!mr->pbl_table)
887 return -ENOMEM;
888
889 for (i = 0; i < mr->num_pbls; i++) {
890 va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
891 if (!va) {
892 ocrdma_free_mr_pbl_tbl(dev, mr);
893 status = -ENOMEM;
894 break;
895 }
896 memset(va, 0, dma_len);
897 mr->pbl_table[i].va = va;
898 mr->pbl_table[i].pa = pa;
899 }
900 return status;
901}
902
903static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
904 u32 num_pbes)
905{
906 struct ocrdma_pbe *pbe;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200907 struct scatterlist *sg;
Parav Panditfe2caef2012-03-21 04:09:06 +0530908 struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
909 struct ib_umem *umem = mr->umem;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200910 int shift, pg_cnt, pages, pbe_cnt, entry, total_num_pbes = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530911
912 if (!mr->hwmr.num_pbes)
913 return;
914
915 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
916 pbe_cnt = 0;
917
Artemy Kovalyov3e7e1192017-04-05 09:23:50 +0300918 shift = umem->page_shift;
Parav Panditfe2caef2012-03-21 04:09:06 +0530919
Yishai Hadaseeb84612014-01-28 13:40:15 +0200920 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
921 pages = sg_dma_len(sg) >> shift;
922 for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) {
923 /* store the page address in pbe */
924 pbe->pa_lo =
Artemy Kovalyov3e7e1192017-04-05 09:23:50 +0300925 cpu_to_le32(sg_dma_address(sg) +
926 (pg_cnt << shift));
Yishai Hadaseeb84612014-01-28 13:40:15 +0200927 pbe->pa_hi =
Artemy Kovalyov3e7e1192017-04-05 09:23:50 +0300928 cpu_to_le32(upper_32_bits(sg_dma_address(sg) +
929 (pg_cnt << shift)));
Yishai Hadaseeb84612014-01-28 13:40:15 +0200930 pbe_cnt += 1;
931 total_num_pbes += 1;
932 pbe++;
Parav Panditfe2caef2012-03-21 04:09:06 +0530933
Yishai Hadaseeb84612014-01-28 13:40:15 +0200934 /* if done building pbes, issue the mbx cmd. */
935 if (total_num_pbes == num_pbes)
936 return;
Parav Panditfe2caef2012-03-21 04:09:06 +0530937
Yishai Hadaseeb84612014-01-28 13:40:15 +0200938 /* if the given pbl is full storing the pbes,
939 * move to next pbl.
940 */
941 if (pbe_cnt ==
942 (mr->hwmr.pbl_size / sizeof(u64))) {
943 pbl_tbl++;
944 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
945 pbe_cnt = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530946 }
Yishai Hadaseeb84612014-01-28 13:40:15 +0200947
Parav Panditfe2caef2012-03-21 04:09:06 +0530948 }
949 }
950}
951
952struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
953 u64 usr_addr, int acc, struct ib_udata *udata)
954{
955 int status = -ENOMEM;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530956 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530957 struct ocrdma_mr *mr;
958 struct ocrdma_pd *pd;
Parav Panditfe2caef2012-03-21 04:09:06 +0530959 u32 num_pbes;
960
961 pd = get_ocrdma_pd(ibpd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530962
963 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
964 return ERR_PTR(-EINVAL);
965
966 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
967 if (!mr)
968 return ERR_PTR(status);
Parav Panditfe2caef2012-03-21 04:09:06 +0530969 mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
970 if (IS_ERR(mr->umem)) {
971 status = -EFAULT;
972 goto umem_err;
973 }
974 num_pbes = ib_umem_page_count(mr->umem);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530975 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
Parav Panditfe2caef2012-03-21 04:09:06 +0530976 if (status)
977 goto umem_err;
978
Artemy Kovalyov3e7e1192017-04-05 09:23:50 +0300979 mr->hwmr.pbe_size = BIT(mr->umem->page_shift);
Haggai Eran406f9e52014-12-11 17:04:12 +0200980 mr->hwmr.fbo = ib_umem_offset(mr->umem);
Parav Panditfe2caef2012-03-21 04:09:06 +0530981 mr->hwmr.va = usr_addr;
982 mr->hwmr.len = len;
983 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
984 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
985 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
986 mr->hwmr.local_rd = 1;
987 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
988 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
989 if (status)
990 goto umem_err;
991 build_user_pbes(dev, mr, num_pbes);
992 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
993 if (status)
994 goto mbx_err;
Parav Panditfe2caef2012-03-21 04:09:06 +0530995 mr->ibmr.lkey = mr->hwmr.lkey;
996 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
997 mr->ibmr.rkey = mr->hwmr.lkey;
998
999 return &mr->ibmr;
1000
1001mbx_err:
1002 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
1003umem_err:
1004 kfree(mr);
1005 return ERR_PTR(status);
1006}
1007
1008int ocrdma_dereg_mr(struct ib_mr *ib_mr)
1009{
1010 struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301011 struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301012
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301013 (void) ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
Parav Panditfe2caef2012-03-21 04:09:06 +05301014
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03001015 kfree(mr->pages);
Selvin Xavier9d1878a2014-02-04 11:57:02 +05301016 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
Parav Panditfe2caef2012-03-21 04:09:06 +05301017
Parav Panditfe2caef2012-03-21 04:09:06 +05301018 /* it could be user registered memory. */
1019 if (mr->umem)
1020 ib_umem_release(mr->umem);
1021 kfree(mr);
Mitesh Ahuja6dab0262014-06-10 19:32:21 +05301022
1023 /* Don't stop cleanup, in case FW is unresponsive */
1024 if (dev->mqe_ctx.fw_error_state) {
Mitesh Ahuja6dab0262014-06-10 19:32:21 +05301025 pr_err("%s(%d) fw not responding.\n",
1026 __func__, dev->id);
1027 }
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301028 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301029}
1030
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301031static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
1032 struct ib_udata *udata,
Parav Panditfe2caef2012-03-21 04:09:06 +05301033 struct ib_ucontext *ib_ctx)
1034{
1035 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301036 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
Parav Panditfe2caef2012-03-21 04:09:06 +05301037 struct ocrdma_create_cq_uresp uresp;
1038
Dan Carpenter63ea3742013-07-29 22:34:29 +03001039 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +05301040 uresp.cq_id = cq->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301041 uresp.page_size = PAGE_ALIGN(cq->len);
Parav Panditfe2caef2012-03-21 04:09:06 +05301042 uresp.num_pages = 1;
1043 uresp.max_hw_cqe = cq->max_hw_cqe;
Devesh Sharma1b76d382014-09-05 19:35:40 +05301044 uresp.page_addr[0] = virt_to_phys(cq->va);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301045 uresp.db_page_addr = ocrdma_get_db_addr(dev, uctx->cntxt_pd->id);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301046 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +05301047 uresp.phase_change = cq->phase_change ? 1 : 0;
1048 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1049 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001050 pr_err("%s(%d) copy error cqid=0x%x.\n",
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301051 __func__, dev->id, cq->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301052 goto err;
1053 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301054 status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
1055 if (status)
1056 goto err;
1057 status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
1058 if (status) {
1059 ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
1060 goto err;
1061 }
1062 cq->ucontext = uctx;
1063err:
1064 return status;
1065}
1066
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001067struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev,
1068 const struct ib_cq_init_attr *attr,
Parav Panditfe2caef2012-03-21 04:09:06 +05301069 struct ib_ucontext *ib_ctx,
1070 struct ib_udata *udata)
1071{
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001072 int entries = attr->cqe;
Parav Panditfe2caef2012-03-21 04:09:06 +05301073 struct ocrdma_cq *cq;
1074 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301075 struct ocrdma_ucontext *uctx = NULL;
1076 u16 pd_id = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301077 int status;
1078 struct ocrdma_create_cq_ureq ureq;
1079
Matan Barakbcf4c1e2015-06-11 16:35:20 +03001080 if (attr->flags)
1081 return ERR_PTR(-EINVAL);
1082
Parav Panditfe2caef2012-03-21 04:09:06 +05301083 if (udata) {
1084 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1085 return ERR_PTR(-EFAULT);
1086 } else
1087 ureq.dpp_cq = 0;
1088 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
1089 if (!cq)
1090 return ERR_PTR(-ENOMEM);
1091
1092 spin_lock_init(&cq->cq_lock);
1093 spin_lock_init(&cq->comp_handler_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301094 INIT_LIST_HEAD(&cq->sq_head);
1095 INIT_LIST_HEAD(&cq->rq_head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301096
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301097 if (ib_ctx) {
1098 uctx = get_ocrdma_ucontext(ib_ctx);
1099 pd_id = uctx->cntxt_pd->id;
1100 }
1101
1102 status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq, pd_id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301103 if (status) {
1104 kfree(cq);
1105 return ERR_PTR(status);
1106 }
1107 if (ib_ctx) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301108 status = ocrdma_copy_cq_uresp(dev, cq, udata, ib_ctx);
Parav Panditfe2caef2012-03-21 04:09:06 +05301109 if (status)
1110 goto ctx_err;
1111 }
1112 cq->phase = OCRDMA_CQE_VALID;
Parav Panditfe2caef2012-03-21 04:09:06 +05301113 dev->cq_tbl[cq->id] = cq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301114 return &cq->ibcq;
1115
1116ctx_err:
1117 ocrdma_mbx_destroy_cq(dev, cq);
1118 kfree(cq);
1119 return ERR_PTR(status);
1120}
1121
1122int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
1123 struct ib_udata *udata)
1124{
1125 int status = 0;
1126 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
1127
1128 if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
1129 status = -EINVAL;
1130 return status;
1131 }
1132 ibcq->cqe = new_cnt;
1133 return status;
1134}
1135
Devesh Sharmaea617622014-02-04 11:56:54 +05301136static void ocrdma_flush_cq(struct ocrdma_cq *cq)
1137{
1138 int cqe_cnt;
1139 int valid_count = 0;
1140 unsigned long flags;
1141
1142 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
1143 struct ocrdma_cqe *cqe = NULL;
1144
1145 cqe = cq->va;
1146 cqe_cnt = cq->cqe_cnt;
1147
1148 /* Last irq might have scheduled a polling thread
1149 * sync-up with it before hard flushing.
1150 */
1151 spin_lock_irqsave(&cq->cq_lock, flags);
1152 while (cqe_cnt) {
1153 if (is_cqe_valid(cq, cqe))
1154 valid_count++;
1155 cqe++;
1156 cqe_cnt--;
1157 }
1158 ocrdma_ring_cq_db(dev, cq->id, false, false, valid_count);
1159 spin_unlock_irqrestore(&cq->cq_lock, flags);
1160}
1161
Parav Panditfe2caef2012-03-21 04:09:06 +05301162int ocrdma_destroy_cq(struct ib_cq *ibcq)
1163{
Parav Panditfe2caef2012-03-21 04:09:06 +05301164 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
Devesh Sharmaea617622014-02-04 11:56:54 +05301165 struct ocrdma_eq *eq = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301166 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301167 int pdid = 0;
Devesh Sharmaea617622014-02-04 11:56:54 +05301168 u32 irq, indx;
1169
1170 dev->cq_tbl[cq->id] = NULL;
1171 indx = ocrdma_get_eq_table_index(dev, cq->eqn);
ssh10db287ec2016-12-24 21:50:06 +05301172 BUG_ON(indx == -EINVAL);
Devesh Sharmaea617622014-02-04 11:56:54 +05301173
1174 eq = &dev->eq_tbl[indx];
1175 irq = ocrdma_get_irq(dev, eq);
1176 synchronize_irq(irq);
1177 ocrdma_flush_cq(cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301178
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301179 (void)ocrdma_mbx_destroy_cq(dev, cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301180 if (cq->ucontext) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301181 pdid = cq->ucontext->cntxt_pd->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301182 ocrdma_del_mmap(cq->ucontext, (u64) cq->pa,
1183 PAGE_ALIGN(cq->len));
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301184 ocrdma_del_mmap(cq->ucontext,
1185 ocrdma_get_db_addr(dev, pdid),
Parav Panditfe2caef2012-03-21 04:09:06 +05301186 dev->nic_info.db_page_size);
1187 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301188
1189 kfree(cq);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301190 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301191}
1192
1193static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1194{
1195 int status = -EINVAL;
1196
1197 if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
1198 dev->qp_tbl[qp->id] = qp;
1199 status = 0;
1200 }
1201 return status;
1202}
1203
1204static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1205{
1206 dev->qp_tbl[qp->id] = NULL;
1207}
1208
1209static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
1210 struct ib_qp_init_attr *attrs)
1211{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301212 if ((attrs->qp_type != IB_QPT_GSI) &&
1213 (attrs->qp_type != IB_QPT_RC) &&
1214 (attrs->qp_type != IB_QPT_UC) &&
1215 (attrs->qp_type != IB_QPT_UD)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001216 pr_err("%s(%d) unsupported qp type=0x%x requested\n",
1217 __func__, dev->id, attrs->qp_type);
Parav Panditfe2caef2012-03-21 04:09:06 +05301218 return -EINVAL;
1219 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301220 /* Skip the check for QP1 to support CM size of 128 */
1221 if ((attrs->qp_type != IB_QPT_GSI) &&
1222 (attrs->cap.max_send_wr > dev->attr.max_wqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001223 pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
1224 __func__, dev->id, attrs->cap.max_send_wr);
1225 pr_err("%s(%d) supported send_wr=0x%x\n",
1226 __func__, dev->id, dev->attr.max_wqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301227 return -EINVAL;
1228 }
1229 if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001230 pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
1231 __func__, dev->id, attrs->cap.max_recv_wr);
1232 pr_err("%s(%d) supported recv_wr=0x%x\n",
1233 __func__, dev->id, dev->attr.max_rqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301234 return -EINVAL;
1235 }
1236 if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001237 pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
1238 __func__, dev->id, attrs->cap.max_inline_data);
1239 pr_err("%s(%d) supported inline data size=0x%x\n",
1240 __func__, dev->id, dev->attr.max_inline_data);
Parav Panditfe2caef2012-03-21 04:09:06 +05301241 return -EINVAL;
1242 }
1243 if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001244 pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
1245 __func__, dev->id, attrs->cap.max_send_sge);
1246 pr_err("%s(%d) supported send_sge=0x%x\n",
1247 __func__, dev->id, dev->attr.max_send_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301248 return -EINVAL;
1249 }
1250 if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001251 pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
1252 __func__, dev->id, attrs->cap.max_recv_sge);
1253 pr_err("%s(%d) supported recv_sge=0x%x\n",
1254 __func__, dev->id, dev->attr.max_recv_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301255 return -EINVAL;
1256 }
1257 /* unprivileged user space cannot create special QP */
1258 if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001259 pr_err
Parav Panditfe2caef2012-03-21 04:09:06 +05301260 ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
1261 __func__, dev->id, attrs->qp_type);
1262 return -EINVAL;
1263 }
1264 /* allow creating only one GSI type of QP */
1265 if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001266 pr_err("%s(%d) GSI special QPs already created.\n",
1267 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301268 return -EINVAL;
1269 }
1270 /* verify consumer QPs are not trying to use GSI QP's CQ */
1271 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
1272 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301273 (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001274 pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301275 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301276 return -EINVAL;
1277 }
1278 }
1279 return 0;
1280}
1281
1282static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
1283 struct ib_udata *udata, int dpp_offset,
1284 int dpp_credit_lmt, int srq)
1285{
Markus Elfring0ca4c392015-12-26 18:18:18 +01001286 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301287 u64 usr_db;
1288 struct ocrdma_create_qp_uresp uresp;
Parav Panditfe2caef2012-03-21 04:09:06 +05301289 struct ocrdma_pd *pd = qp->pd;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301290 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301291
1292 memset(&uresp, 0, sizeof(uresp));
1293 usr_db = dev->nic_info.unmapped_db +
1294 (pd->id * dev->nic_info.db_page_size);
1295 uresp.qp_id = qp->id;
1296 uresp.sq_dbid = qp->sq.dbid;
1297 uresp.num_sq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301298 uresp.sq_page_size = PAGE_ALIGN(qp->sq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301299 uresp.sq_page_addr[0] = virt_to_phys(qp->sq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301300 uresp.num_wqe_allocated = qp->sq.max_cnt;
1301 if (!srq) {
1302 uresp.rq_dbid = qp->rq.dbid;
1303 uresp.num_rq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301304 uresp.rq_page_size = PAGE_ALIGN(qp->rq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301305 uresp.rq_page_addr[0] = virt_to_phys(qp->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301306 uresp.num_rqe_allocated = qp->rq.max_cnt;
1307 }
1308 uresp.db_page_addr = usr_db;
1309 uresp.db_page_size = dev->nic_info.db_page_size;
Devesh Sharma2df84fa82014-02-04 11:56:55 +05301310 uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
1311 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
1312 uresp.db_shift = OCRDMA_DB_RQ_SHIFT;
Parav Panditfe2caef2012-03-21 04:09:06 +05301313
1314 if (qp->dpp_enabled) {
1315 uresp.dpp_credit = dpp_credit_lmt;
1316 uresp.dpp_offset = dpp_offset;
1317 }
1318 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1319 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001320 pr_err("%s(%d) user copy error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301321 goto err;
1322 }
1323 status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
1324 uresp.sq_page_size);
1325 if (status)
1326 goto err;
1327
1328 if (!srq) {
1329 status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
1330 uresp.rq_page_size);
1331 if (status)
1332 goto rq_map_err;
1333 }
1334 return status;
1335rq_map_err:
1336 ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
1337err:
1338 return status;
1339}
1340
1341static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
1342 struct ocrdma_pd *pd)
1343{
Devesh Sharma21c33912014-02-04 11:56:56 +05301344 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301345 qp->sq_db = dev->nic_info.db +
1346 (pd->id * dev->nic_info.db_page_size) +
1347 OCRDMA_DB_GEN2_SQ_OFFSET;
1348 qp->rq_db = dev->nic_info.db +
1349 (pd->id * dev->nic_info.db_page_size) +
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301350 OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301351 } else {
1352 qp->sq_db = dev->nic_info.db +
1353 (pd->id * dev->nic_info.db_page_size) +
1354 OCRDMA_DB_SQ_OFFSET;
1355 qp->rq_db = dev->nic_info.db +
1356 (pd->id * dev->nic_info.db_page_size) +
1357 OCRDMA_DB_RQ_OFFSET;
1358 }
1359}
1360
1361static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
1362{
1363 qp->wqe_wr_id_tbl =
1364 kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
1365 GFP_KERNEL);
1366 if (qp->wqe_wr_id_tbl == NULL)
1367 return -ENOMEM;
1368 qp->rqe_wr_id_tbl =
1369 kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
1370 if (qp->rqe_wr_id_tbl == NULL)
1371 return -ENOMEM;
1372
1373 return 0;
1374}
1375
1376static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
1377 struct ocrdma_pd *pd,
1378 struct ib_qp_init_attr *attrs)
1379{
1380 qp->pd = pd;
1381 spin_lock_init(&qp->q_lock);
1382 INIT_LIST_HEAD(&qp->sq_entry);
1383 INIT_LIST_HEAD(&qp->rq_entry);
1384
1385 qp->qp_type = attrs->qp_type;
1386 qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
1387 qp->max_inline_data = attrs->cap.max_inline_data;
1388 qp->sq.max_sges = attrs->cap.max_send_sge;
1389 qp->rq.max_sges = attrs->cap.max_recv_sge;
1390 qp->state = OCRDMA_QPS_RST;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05301391 qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
Parav Panditfe2caef2012-03-21 04:09:06 +05301392}
1393
Parav Panditfe2caef2012-03-21 04:09:06 +05301394static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
1395 struct ib_qp_init_attr *attrs)
1396{
1397 if (attrs->qp_type == IB_QPT_GSI) {
1398 dev->gsi_qp_created = 1;
1399 dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
1400 dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
1401 }
1402}
1403
1404struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
1405 struct ib_qp_init_attr *attrs,
1406 struct ib_udata *udata)
1407{
1408 int status;
1409 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1410 struct ocrdma_qp *qp;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301411 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301412 struct ocrdma_create_qp_ureq ureq;
1413 u16 dpp_credit_lmt, dpp_offset;
1414
1415 status = ocrdma_check_qp_params(ibpd, dev, attrs);
1416 if (status)
1417 goto gen_err;
1418
1419 memset(&ureq, 0, sizeof(ureq));
1420 if (udata) {
1421 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1422 return ERR_PTR(-EFAULT);
1423 }
1424 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1425 if (!qp) {
1426 status = -ENOMEM;
1427 goto gen_err;
1428 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301429 ocrdma_set_qp_init_params(qp, pd, attrs);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301430 if (udata == NULL)
1431 qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
1432 OCRDMA_QP_FAST_REG);
Parav Panditfe2caef2012-03-21 04:09:06 +05301433
1434 mutex_lock(&dev->dev_lock);
1435 status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
1436 ureq.dpp_cq_id,
1437 &dpp_offset, &dpp_credit_lmt);
1438 if (status)
1439 goto mbx_err;
1440
1441 /* user space QP's wr_id table are managed in library */
1442 if (udata == NULL) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301443 status = ocrdma_alloc_wr_id_tbl(qp);
1444 if (status)
1445 goto map_err;
1446 }
1447
1448 status = ocrdma_add_qpn_map(dev, qp);
1449 if (status)
1450 goto map_err;
1451 ocrdma_set_qp_db(dev, qp, pd);
1452 if (udata) {
1453 status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
1454 dpp_credit_lmt,
1455 (attrs->srq != NULL));
1456 if (status)
1457 goto cpy_err;
1458 }
1459 ocrdma_store_gsi_qp_cq(dev, attrs);
Gottumukkala, Naresh27159f52013-06-05 08:50:46 +00001460 qp->ibqp.qp_num = qp->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301461 mutex_unlock(&dev->dev_lock);
1462 return &qp->ibqp;
1463
1464cpy_err:
1465 ocrdma_del_qpn_map(dev, qp);
1466map_err:
1467 ocrdma_mbx_destroy_qp(dev, qp);
1468mbx_err:
1469 mutex_unlock(&dev->dev_lock);
1470 kfree(qp->wqe_wr_id_tbl);
1471 kfree(qp->rqe_wr_id_tbl);
1472 kfree(qp);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001473 pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05301474gen_err:
1475 return ERR_PTR(status);
1476}
1477
1478int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1479 int attr_mask)
1480{
1481 int status = 0;
1482 struct ocrdma_qp *qp;
1483 struct ocrdma_dev *dev;
1484 enum ib_qp_state old_qps;
1485
1486 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301487 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301488 if (attr_mask & IB_QP_STATE)
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301489 status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301490 /* if new and previous states are same hw doesn't need to
1491 * know about it.
1492 */
1493 if (status < 0)
1494 return status;
Markus Elfring95f60bb2015-12-26 18:40:43 +01001495 return ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask);
Parav Panditfe2caef2012-03-21 04:09:06 +05301496}
1497
1498int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1499 int attr_mask, struct ib_udata *udata)
1500{
1501 unsigned long flags;
1502 int status = -EINVAL;
1503 struct ocrdma_qp *qp;
1504 struct ocrdma_dev *dev;
1505 enum ib_qp_state old_qps, new_qps;
1506
1507 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301508 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301509
1510 /* syncronize with multiple context trying to change, retrive qps */
1511 mutex_lock(&dev->dev_lock);
1512 /* syncronize with wqe, rqe posting and cqe processing contexts */
1513 spin_lock_irqsave(&qp->q_lock, flags);
1514 old_qps = get_ibqp_state(qp->state);
1515 if (attr_mask & IB_QP_STATE)
1516 new_qps = attr->qp_state;
1517 else
1518 new_qps = old_qps;
1519 spin_unlock_irqrestore(&qp->q_lock, flags);
1520
Matan Barakdd5f03b2013-12-12 18:03:11 +02001521 if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask,
Moni Shoua37721d82013-12-12 18:03:16 +02001522 IB_LINK_LAYER_ETHERNET)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001523 pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
1524 "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
1525 __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
1526 old_qps, new_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301527 goto param_err;
1528 }
1529
1530 status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
1531 if (status > 0)
1532 status = 0;
1533param_err:
1534 mutex_unlock(&dev->dev_lock);
1535 return status;
1536}
1537
1538static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
1539{
1540 switch (mtu) {
1541 case 256:
1542 return IB_MTU_256;
1543 case 512:
1544 return IB_MTU_512;
1545 case 1024:
1546 return IB_MTU_1024;
1547 case 2048:
1548 return IB_MTU_2048;
1549 case 4096:
1550 return IB_MTU_4096;
1551 default:
1552 return IB_MTU_1024;
1553 }
1554}
1555
1556static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
1557{
1558 int ib_qp_acc_flags = 0;
1559
1560 if (qp_cap_flags & OCRDMA_QP_INB_WR)
1561 ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
1562 if (qp_cap_flags & OCRDMA_QP_INB_RD)
1563 ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
1564 return ib_qp_acc_flags;
1565}
1566
1567int ocrdma_query_qp(struct ib_qp *ibqp,
1568 struct ib_qp_attr *qp_attr,
1569 int attr_mask, struct ib_qp_init_attr *qp_init_attr)
1570{
1571 int status;
1572 u32 qp_state;
1573 struct ocrdma_qp_params params;
1574 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301575 struct ocrdma_dev *dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301576
1577 memset(&params, 0, sizeof(params));
1578 mutex_lock(&dev->dev_lock);
1579 status = ocrdma_mbx_query_qp(dev, qp, &params);
1580 mutex_unlock(&dev->dev_lock);
1581 if (status)
1582 goto mbx_err;
Mitesh Ahuja95bf0092014-12-03 11:36:33 +05301583 if (qp->qp_type == IB_QPT_UD)
1584 qp_attr->qkey = params.qkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05301585 qp_attr->path_mtu =
1586 ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
1587 OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
1588 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
1589 qp_attr->path_mig_state = IB_MIG_MIGRATED;
1590 qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
1591 qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
1592 qp_attr->dest_qp_num =
1593 params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
1594
1595 qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
1596 qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
1597 qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
1598 qp_attr->cap.max_send_sge = qp->sq.max_sges;
1599 qp_attr->cap.max_recv_sge = qp->rq.max_sges;
Naresh Gottumukkalac43e9ab2013-08-26 15:27:46 +05301600 qp_attr->cap.max_inline_data = qp->max_inline_data;
Parav Panditfe2caef2012-03-21 04:09:06 +05301601 qp_init_attr->cap = qp_attr->cap;
Dasaratharaman Chandramouli44c58482017-04-29 14:41:29 -04001602 qp_attr->ah_attr.type = RDMA_AH_ATTR_TYPE_ROCE;
Parav Panditfe2caef2012-03-21 04:09:06 +05301603
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001604 rdma_ah_set_grh(&qp_attr->ah_attr, NULL,
1605 params.rnt_rc_sl_fl &
1606 OCRDMA_QP_PARAMS_FLOW_LABEL_MASK,
1607 qp->sgid_idx,
1608 (params.hop_lmt_rq_psn &
1609 OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
1610 OCRDMA_QP_PARAMS_HOP_LMT_SHIFT,
1611 (params.tclass_sq_psn &
1612 OCRDMA_QP_PARAMS_TCLASS_MASK) >>
1613 OCRDMA_QP_PARAMS_TCLASS_SHIFT);
1614 rdma_ah_set_dgid_raw(&qp_attr->ah_attr, &params.dgid[0]);
1615
1616 rdma_ah_set_port_num(&qp_attr->ah_attr, 1);
1617 rdma_ah_set_sl(&qp_attr->ah_attr, (params.rnt_rc_sl_fl &
1618 OCRDMA_QP_PARAMS_SL_MASK) >>
1619 OCRDMA_QP_PARAMS_SL_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05301620 qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
1621 OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
1622 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
1623 qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
1624 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
1625 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
1626 qp_attr->retry_cnt =
1627 (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
1628 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
1629 qp_attr->min_rnr_timer = 0;
1630 qp_attr->pkey_index = 0;
1631 qp_attr->port_num = 1;
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -04001632 rdma_ah_set_path_bits(&qp_attr->ah_attr, 0);
1633 rdma_ah_set_static_rate(&qp_attr->ah_attr, 0);
Parav Panditfe2caef2012-03-21 04:09:06 +05301634 qp_attr->alt_pkey_index = 0;
1635 qp_attr->alt_port_num = 0;
1636 qp_attr->alt_timeout = 0;
1637 memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
1638 qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
1639 OCRDMA_QP_PARAMS_STATE_SHIFT;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301640 qp_attr->qp_state = get_ibqp_state(qp_state);
1641 qp_attr->cur_qp_state = qp_attr->qp_state;
Parav Panditfe2caef2012-03-21 04:09:06 +05301642 qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
1643 qp_attr->max_dest_rd_atomic =
1644 params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
1645 qp_attr->max_rd_atomic =
1646 params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
1647 qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
1648 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301649 /* Sync driver QP state with FW */
1650 ocrdma_qp_state_change(qp, qp_attr->qp_state, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05301651mbx_err:
1652 return status;
1653}
1654
Rasmus Villemoesf3070e72015-01-16 15:39:56 +01001655static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, unsigned int idx)
Parav Panditfe2caef2012-03-21 04:09:06 +05301656{
Rasmus Villemoesf3070e72015-01-16 15:39:56 +01001657 unsigned int i = idx / 32;
1658 u32 mask = (1U << (idx % 32));
Parav Panditfe2caef2012-03-21 04:09:06 +05301659
Rasmus Villemoesba64fdc2015-01-16 15:39:55 +01001660 srq->idx_bit_fields[i] ^= mask;
Parav Panditfe2caef2012-03-21 04:09:06 +05301661}
1662
1663static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
1664{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301665 return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
Parav Panditfe2caef2012-03-21 04:09:06 +05301666}
1667
1668static int is_hw_sq_empty(struct ocrdma_qp *qp)
1669{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301670 return (qp->sq.tail == qp->sq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301671}
1672
1673static int is_hw_rq_empty(struct ocrdma_qp *qp)
1674{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301675 return (qp->rq.tail == qp->rq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301676}
1677
1678static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
1679{
1680 return q->va + (q->head * q->entry_size);
1681}
1682
1683static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
1684 u32 idx)
1685{
1686 return q->va + (idx * q->entry_size);
1687}
1688
1689static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
1690{
1691 q->head = (q->head + 1) & q->max_wqe_idx;
1692}
1693
1694static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
1695{
1696 q->tail = (q->tail + 1) & q->max_wqe_idx;
1697}
1698
1699/* discard the cqe for a given QP */
1700static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
1701{
1702 unsigned long cq_flags;
1703 unsigned long flags;
1704 int discard_cnt = 0;
1705 u32 cur_getp, stop_getp;
1706 struct ocrdma_cqe *cqe;
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301707 u32 qpn = 0, wqe_idx = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301708
1709 spin_lock_irqsave(&cq->cq_lock, cq_flags);
1710
1711 /* traverse through the CQEs in the hw CQ,
1712 * find the matching CQE for a given qp,
1713 * mark the matching one discarded by clearing qpn.
1714 * ring the doorbell in the poll_cq() as
1715 * we don't complete out of order cqe.
1716 */
1717
1718 cur_getp = cq->getp;
1719 /* find upto when do we reap the cq. */
1720 stop_getp = cur_getp;
1721 do {
1722 if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
1723 break;
1724
1725 cqe = cq->va + cur_getp;
1726 /* if (a) done reaping whole hw cq, or
1727 * (b) qp_xq becomes empty.
1728 * then exit
1729 */
1730 qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
1731 /* if previously discarded cqe found, skip that too. */
1732 /* check for matching qp */
1733 if (qpn == 0 || qpn != qp->id)
1734 goto skip_cqe;
1735
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301736 if (is_cqe_for_sq(cqe)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301737 ocrdma_hwq_inc_tail(&qp->sq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301738 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301739 if (qp->srq) {
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301740 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
1741 OCRDMA_CQE_BUFTAG_SHIFT) &
1742 qp->srq->rq.max_wqe_idx;
ssh10db287ec2016-12-24 21:50:06 +05301743 BUG_ON(wqe_idx < 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05301744 spin_lock_irqsave(&qp->srq->q_lock, flags);
1745 ocrdma_hwq_inc_tail(&qp->srq->rq);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301746 ocrdma_srq_toggle_bit(qp->srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05301747 spin_unlock_irqrestore(&qp->srq->q_lock, flags);
1748
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301749 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301750 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301751 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301752 }
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301753 /* mark cqe discarded so that it is not picked up later
1754 * in the poll_cq().
1755 */
1756 discard_cnt += 1;
1757 cqe->cmn.qpn = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301758skip_cqe:
1759 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
1760 } while (cur_getp != stop_getp);
1761 spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
1762}
1763
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301764void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
Parav Panditfe2caef2012-03-21 04:09:06 +05301765{
1766 int found = false;
1767 unsigned long flags;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301768 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301769 /* sync with any active CQ poll */
1770
1771 spin_lock_irqsave(&dev->flush_q_lock, flags);
1772 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1773 if (found)
1774 list_del(&qp->sq_entry);
1775 if (!qp->srq) {
1776 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1777 if (found)
1778 list_del(&qp->rq_entry);
1779 }
1780 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
1781}
1782
1783int ocrdma_destroy_qp(struct ib_qp *ibqp)
1784{
Parav Panditfe2caef2012-03-21 04:09:06 +05301785 struct ocrdma_pd *pd;
1786 struct ocrdma_qp *qp;
1787 struct ocrdma_dev *dev;
1788 struct ib_qp_attr attrs;
Devesh Sharmafe488222015-05-19 11:32:34 +05301789 int attr_mask;
Dan Carpenterd19081e2012-05-02 09:14:47 +03001790 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05301791
1792 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301793 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301794
Parav Panditfe2caef2012-03-21 04:09:06 +05301795 pd = qp->pd;
1796
1797 /* change the QP state to ERROR */
Devesh Sharmafe488222015-05-19 11:32:34 +05301798 if (qp->state != OCRDMA_QPS_RST) {
1799 attrs.qp_state = IB_QPS_ERR;
1800 attr_mask = IB_QP_STATE;
1801 _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
1802 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301803 /* ensure that CQEs for newly created QP (whose id may be same with
1804 * one which just getting destroyed are same), dont get
1805 * discarded until the old CQEs are discarded.
1806 */
1807 mutex_lock(&dev->dev_lock);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301808 (void) ocrdma_mbx_destroy_qp(dev, qp);
Parav Panditfe2caef2012-03-21 04:09:06 +05301809
1810 /*
1811 * acquire CQ lock while destroy is in progress, in order to
1812 * protect against proessing in-flight CQEs for this QP.
1813 */
Dan Carpenterd19081e2012-05-02 09:14:47 +03001814 spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301815 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001816 spin_lock(&qp->rq_cq->cq_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301817
1818 ocrdma_del_qpn_map(dev, qp);
1819
1820 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001821 spin_unlock(&qp->rq_cq->cq_lock);
1822 spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301823
1824 if (!pd->uctx) {
1825 ocrdma_discard_cqes(qp, qp->sq_cq);
1826 ocrdma_discard_cqes(qp, qp->rq_cq);
1827 }
1828 mutex_unlock(&dev->dev_lock);
1829
1830 if (pd->uctx) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301831 ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
1832 PAGE_ALIGN(qp->sq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301833 if (!qp->srq)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301834 ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
1835 PAGE_ALIGN(qp->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301836 }
1837
1838 ocrdma_del_flush_qp(qp);
1839
Parav Panditfe2caef2012-03-21 04:09:06 +05301840 kfree(qp->wqe_wr_id_tbl);
1841 kfree(qp->rqe_wr_id_tbl);
1842 kfree(qp);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301843 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301844}
1845
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301846static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
1847 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301848{
1849 int status;
1850 struct ocrdma_create_srq_uresp uresp;
1851
Dan Carpenter63ea3742013-07-29 22:34:29 +03001852 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +05301853 uresp.rq_dbid = srq->rq.dbid;
1854 uresp.num_rq_pages = 1;
Devesh Sharma1b76d382014-09-05 19:35:40 +05301855 uresp.rq_page_addr[0] = virt_to_phys(srq->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301856 uresp.rq_page_size = srq->rq.len;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301857 uresp.db_page_addr = dev->nic_info.unmapped_db +
1858 (srq->pd->id * dev->nic_info.db_page_size);
1859 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +05301860 uresp.num_rqe_allocated = srq->rq.max_cnt;
Devesh Sharma21c33912014-02-04 11:56:56 +05301861 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301862 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301863 uresp.db_shift = 24;
1864 } else {
1865 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
1866 uresp.db_shift = 16;
1867 }
1868
1869 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1870 if (status)
1871 return status;
1872 status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
1873 uresp.rq_page_size);
1874 if (status)
1875 return status;
1876 return status;
1877}
1878
1879struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
1880 struct ib_srq_init_attr *init_attr,
1881 struct ib_udata *udata)
1882{
1883 int status = -ENOMEM;
1884 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301885 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301886 struct ocrdma_srq *srq;
1887
1888 if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
1889 return ERR_PTR(-EINVAL);
1890 if (init_attr->attr.max_wr > dev->attr.max_rqe)
1891 return ERR_PTR(-EINVAL);
1892
1893 srq = kzalloc(sizeof(*srq), GFP_KERNEL);
1894 if (!srq)
1895 return ERR_PTR(status);
1896
1897 spin_lock_init(&srq->q_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301898 srq->pd = pd;
1899 srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301900 status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +05301901 if (status)
1902 goto err;
1903
1904 if (udata == NULL) {
Dan Carpenterf0c6e882017-07-13 10:46:49 +03001905 status = -ENOMEM;
Parav Panditfe2caef2012-03-21 04:09:06 +05301906 srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
1907 GFP_KERNEL);
1908 if (srq->rqe_wr_id_tbl == NULL)
1909 goto arm_err;
1910
1911 srq->bit_fields_len = (srq->rq.max_cnt / 32) +
1912 (srq->rq.max_cnt % 32 ? 1 : 0);
1913 srq->idx_bit_fields =
1914 kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
1915 if (srq->idx_bit_fields == NULL)
1916 goto arm_err;
1917 memset(srq->idx_bit_fields, 0xff,
1918 srq->bit_fields_len * sizeof(u32));
1919 }
1920
1921 if (init_attr->attr.srq_limit) {
1922 status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
1923 if (status)
1924 goto arm_err;
1925 }
1926
Parav Panditfe2caef2012-03-21 04:09:06 +05301927 if (udata) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301928 status = ocrdma_copy_srq_uresp(dev, srq, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +05301929 if (status)
1930 goto arm_err;
1931 }
1932
Parav Panditfe2caef2012-03-21 04:09:06 +05301933 return &srq->ibsrq;
1934
1935arm_err:
1936 ocrdma_mbx_destroy_srq(dev, srq);
1937err:
1938 kfree(srq->rqe_wr_id_tbl);
1939 kfree(srq->idx_bit_fields);
1940 kfree(srq);
1941 return ERR_PTR(status);
1942}
1943
1944int ocrdma_modify_srq(struct ib_srq *ibsrq,
1945 struct ib_srq_attr *srq_attr,
1946 enum ib_srq_attr_mask srq_attr_mask,
1947 struct ib_udata *udata)
1948{
Markus Elfring0ca4c392015-12-26 18:18:18 +01001949 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301950 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301951
1952 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301953 if (srq_attr_mask & IB_SRQ_MAX_WR)
1954 status = -EINVAL;
1955 else
1956 status = ocrdma_mbx_modify_srq(srq, srq_attr);
1957 return status;
1958}
1959
1960int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
1961{
1962 int status;
1963 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301964
1965 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301966 status = ocrdma_mbx_query_srq(srq, srq_attr);
1967 return status;
1968}
1969
1970int ocrdma_destroy_srq(struct ib_srq *ibsrq)
1971{
1972 int status;
1973 struct ocrdma_srq *srq;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301974 struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301975
1976 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301977
1978 status = ocrdma_mbx_destroy_srq(dev, srq);
1979
1980 if (srq->pd->uctx)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301981 ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
1982 PAGE_ALIGN(srq->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301983
Parav Panditfe2caef2012-03-21 04:09:06 +05301984 kfree(srq->idx_bit_fields);
1985 kfree(srq->rqe_wr_id_tbl);
1986 kfree(srq);
1987 return status;
1988}
1989
1990/* unprivileged verbs and their support functions. */
1991static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
1992 struct ocrdma_hdr_wqe *hdr,
1993 struct ib_send_wr *wr)
1994{
1995 struct ocrdma_ewqe_ud_hdr *ud_hdr =
1996 (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001997 struct ocrdma_ah *ah = get_ocrdma_ah(ud_wr(wr)->ah);
Parav Panditfe2caef2012-03-21 04:09:06 +05301998
Christoph Hellwige622f2f2015-10-08 09:16:33 +01001999 ud_hdr->rsvd_dest_qpn = ud_wr(wr)->remote_qpn;
Parav Panditfe2caef2012-03-21 04:09:06 +05302000 if (qp->qp_type == IB_QPT_GSI)
2001 ud_hdr->qkey = qp->qkey;
2002 else
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002003 ud_hdr->qkey = ud_wr(wr)->remote_qkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05302004 ud_hdr->rsvd_ahid = ah->id;
Devesh Sharma6b062662016-01-28 08:59:57 -05002005 ud_hdr->hdr_type = ah->hdr_type;
Devesh Sharma29565f22014-12-18 14:13:07 +05302006 if (ah->av->valid & OCRDMA_AV_VLAN_VALID)
2007 hdr->cw |= (OCRDMA_FLAG_AH_VLAN_PR << OCRDMA_WQE_FLAGS_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302008}
2009
2010static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
2011 struct ocrdma_sge *sge, int num_sge,
2012 struct ib_sge *sg_list)
2013{
2014 int i;
2015
2016 for (i = 0; i < num_sge; i++) {
2017 sge[i].lrkey = sg_list[i].lkey;
2018 sge[i].addr_lo = sg_list[i].addr;
2019 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
2020 sge[i].len = sg_list[i].length;
2021 hdr->total_len += sg_list[i].length;
2022 }
2023 if (num_sge == 0)
2024 memset(sge, 0, sizeof(*sge));
2025}
2026
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05302027static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
2028{
2029 uint32_t total_len = 0, i;
2030
2031 for (i = 0; i < num_sge; i++)
2032 total_len += sg_list[i].length;
2033 return total_len;
2034}
2035
2036
Parav Panditfe2caef2012-03-21 04:09:06 +05302037static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
2038 struct ocrdma_hdr_wqe *hdr,
2039 struct ocrdma_sge *sge,
2040 struct ib_send_wr *wr, u32 wqe_size)
2041{
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05302042 int i;
2043 char *dpp_addr;
2044
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302045 if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05302046 hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
2047 if (unlikely(hdr->total_len > qp->max_inline_data)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002048 pr_err("%s() supported_len=0x%x,\n"
Masanari Iida1a84db52014-08-29 23:37:33 +09002049 " unsupported len req=0x%x\n", __func__,
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05302050 qp->max_inline_data, hdr->total_len);
Parav Panditfe2caef2012-03-21 04:09:06 +05302051 return -EINVAL;
2052 }
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05302053 dpp_addr = (char *)sge;
2054 for (i = 0; i < wr->num_sge; i++) {
2055 memcpy(dpp_addr,
2056 (void *)(unsigned long)wr->sg_list[i].addr,
2057 wr->sg_list[i].length);
2058 dpp_addr += wr->sg_list[i].length;
2059 }
2060
Parav Panditfe2caef2012-03-21 04:09:06 +05302061 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05302062 if (0 == hdr->total_len)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302063 wqe_size += sizeof(struct ocrdma_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05302064 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
2065 } else {
2066 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2067 if (wr->num_sge)
2068 wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
2069 else
2070 wqe_size += sizeof(struct ocrdma_sge);
2071 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2072 }
2073 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2074 return 0;
2075}
2076
2077static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2078 struct ib_send_wr *wr)
2079{
2080 int status;
2081 struct ocrdma_sge *sge;
2082 u32 wqe_size = sizeof(*hdr);
2083
2084 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2085 ocrdma_build_ud_hdr(qp, hdr, wr);
2086 sge = (struct ocrdma_sge *)(hdr + 2);
2087 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302088 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302089 sge = (struct ocrdma_sge *)(hdr + 1);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302090 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302091
2092 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2093 return status;
2094}
2095
2096static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2097 struct ib_send_wr *wr)
2098{
2099 int status;
2100 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2101 struct ocrdma_sge *sge = ext_rw + 1;
2102 u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
2103
2104 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2105 if (status)
2106 return status;
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002107 ext_rw->addr_lo = rdma_wr(wr)->remote_addr;
2108 ext_rw->addr_hi = upper_32_bits(rdma_wr(wr)->remote_addr);
2109 ext_rw->lrkey = rdma_wr(wr)->rkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05302110 ext_rw->len = hdr->total_len;
2111 return 0;
2112}
2113
2114static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2115 struct ib_send_wr *wr)
2116{
2117 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2118 struct ocrdma_sge *sge = ext_rw + 1;
2119 u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
2120 sizeof(struct ocrdma_hdr_wqe);
2121
2122 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2123 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2124 hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
2125 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2126
Christoph Hellwige622f2f2015-10-08 09:16:33 +01002127 ext_rw->addr_lo = rdma_wr(wr)->remote_addr;
2128 ext_rw->addr_hi = upper_32_bits(rdma_wr(wr)->remote_addr);
2129 ext_rw->lrkey = rdma_wr(wr)->rkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05302130 ext_rw->len = hdr->total_len;
2131}
2132
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302133static int get_encoded_page_size(int pg_sz)
2134{
2135 /* Max size is 256M 4096 << 16 */
2136 int i = 0;
2137 for (; i < 17; i++)
2138 if (pg_sz == (4096 << i))
2139 break;
2140 return i;
2141}
2142
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002143static int ocrdma_build_reg(struct ocrdma_qp *qp,
2144 struct ocrdma_hdr_wqe *hdr,
2145 struct ib_reg_wr *wr)
2146{
2147 u64 fbo;
2148 struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
2149 struct ocrdma_mr *mr = get_ocrdma_mr(wr->mr);
2150 struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
2151 struct ocrdma_pbe *pbe;
2152 u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
2153 int num_pbes = 0, i;
2154
2155 wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
2156
2157 hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
2158 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2159
2160 if (wr->access & IB_ACCESS_LOCAL_WRITE)
2161 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
2162 if (wr->access & IB_ACCESS_REMOTE_WRITE)
2163 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
2164 if (wr->access & IB_ACCESS_REMOTE_READ)
2165 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
2166 hdr->lkey = wr->key;
2167 hdr->total_len = mr->ibmr.length;
2168
2169 fbo = mr->ibmr.iova - mr->pages[0];
2170
2171 fast_reg->va_hi = upper_32_bits(mr->ibmr.iova);
2172 fast_reg->va_lo = (u32) (mr->ibmr.iova & 0xffffffff);
2173 fast_reg->fbo_hi = upper_32_bits(fbo);
2174 fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
2175 fast_reg->num_sges = mr->npages;
2176 fast_reg->size_sge = get_encoded_page_size(mr->ibmr.page_size);
2177
2178 pbe = pbl_tbl->va;
2179 for (i = 0; i < mr->npages; i++) {
2180 u64 buf_addr = mr->pages[i];
2181
2182 pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
2183 pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
2184 num_pbes += 1;
2185 pbe++;
2186
2187 /* if the pbl is full storing the pbes,
2188 * move to next pbl.
2189 */
2190 if (num_pbes == (mr->hwmr.pbl_size/sizeof(u64))) {
2191 pbl_tbl++;
2192 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2193 }
2194 }
2195
2196 return 0;
2197}
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302198
Parav Panditfe2caef2012-03-21 04:09:06 +05302199static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
2200{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302201 u32 val = qp->sq.dbid | (1 << OCRDMA_DB_SQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302202
2203 iowrite32(val, qp->sq_db);
2204}
2205
2206int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
2207 struct ib_send_wr **bad_wr)
2208{
2209 int status = 0;
2210 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2211 struct ocrdma_hdr_wqe *hdr;
2212 unsigned long flags;
2213
2214 spin_lock_irqsave(&qp->q_lock, flags);
2215 if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
2216 spin_unlock_irqrestore(&qp->q_lock, flags);
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002217 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302218 return -EINVAL;
2219 }
2220
2221 while (wr) {
Mitesh Ahujaf252b5d2014-06-10 19:32:20 +05302222 if (qp->qp_type == IB_QPT_UD &&
2223 (wr->opcode != IB_WR_SEND &&
2224 wr->opcode != IB_WR_SEND_WITH_IMM)) {
2225 *bad_wr = wr;
2226 status = -EINVAL;
2227 break;
2228 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302229 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
2230 wr->num_sge > qp->sq.max_sges) {
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002231 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302232 status = -ENOMEM;
2233 break;
2234 }
2235 hdr = ocrdma_hwq_head(&qp->sq);
2236 hdr->cw = 0;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302237 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302238 hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2239 if (wr->send_flags & IB_SEND_FENCE)
2240 hdr->cw |=
2241 (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
2242 if (wr->send_flags & IB_SEND_SOLICITED)
2243 hdr->cw |=
2244 (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
2245 hdr->total_len = 0;
2246 switch (wr->opcode) {
2247 case IB_WR_SEND_WITH_IMM:
2248 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2249 hdr->immdt = ntohl(wr->ex.imm_data);
Bart Van Assche705dec32017-10-11 10:49:14 -07002250 /* fall through */
Parav Panditfe2caef2012-03-21 04:09:06 +05302251 case IB_WR_SEND:
2252 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2253 ocrdma_build_send(qp, hdr, wr);
2254 break;
2255 case IB_WR_SEND_WITH_INV:
2256 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
2257 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2258 hdr->lkey = wr->ex.invalidate_rkey;
2259 status = ocrdma_build_send(qp, hdr, wr);
2260 break;
2261 case IB_WR_RDMA_WRITE_WITH_IMM:
2262 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2263 hdr->immdt = ntohl(wr->ex.imm_data);
Bart Van Assche705dec32017-10-11 10:49:14 -07002264 /* fall through */
Parav Panditfe2caef2012-03-21 04:09:06 +05302265 case IB_WR_RDMA_WRITE:
2266 hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
2267 status = ocrdma_build_write(qp, hdr, wr);
2268 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302269 case IB_WR_RDMA_READ:
2270 ocrdma_build_read(qp, hdr, wr);
2271 break;
2272 case IB_WR_LOCAL_INV:
2273 hdr->cw |=
2274 (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302275 hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
2276 sizeof(struct ocrdma_sge)) /
Parav Panditfe2caef2012-03-21 04:09:06 +05302277 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
2278 hdr->lkey = wr->ex.invalidate_rkey;
2279 break;
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03002280 case IB_WR_REG_MR:
2281 status = ocrdma_build_reg(qp, hdr, reg_wr(wr));
2282 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302283 default:
2284 status = -EINVAL;
2285 break;
2286 }
2287 if (status) {
2288 *bad_wr = wr;
2289 break;
2290 }
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302291 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302292 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
2293 else
2294 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
2295 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
2296 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
2297 OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
2298 /* make sure wqe is written before adapter can access it */
2299 wmb();
2300 /* inform hw to start processing it */
2301 ocrdma_ring_sq_db(qp);
2302
2303 /* update pointer, counter for next wr */
2304 ocrdma_hwq_inc_head(&qp->sq);
2305 wr = wr->next;
2306 }
2307 spin_unlock_irqrestore(&qp->q_lock, flags);
2308 return status;
2309}
2310
2311static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
2312{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302313 u32 val = qp->rq.dbid | (1 << OCRDMA_DB_RQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302314
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302315 iowrite32(val, qp->rq_db);
Parav Panditfe2caef2012-03-21 04:09:06 +05302316}
2317
2318static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
2319 u16 tag)
2320{
2321 u32 wqe_size = 0;
2322 struct ocrdma_sge *sge;
2323 if (wr->num_sge)
2324 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
2325 else
2326 wqe_size = sizeof(*sge) + sizeof(*rqe);
2327
2328 rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
2329 OCRDMA_WQE_SIZE_SHIFT);
2330 rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2331 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2332 rqe->total_len = 0;
2333 rqe->rsvd_tag = tag;
2334 sge = (struct ocrdma_sge *)(rqe + 1);
2335 ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
2336 ocrdma_cpu_to_le32(rqe, wqe_size);
2337}
2338
2339int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2340 struct ib_recv_wr **bad_wr)
2341{
2342 int status = 0;
2343 unsigned long flags;
2344 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2345 struct ocrdma_hdr_wqe *rqe;
2346
2347 spin_lock_irqsave(&qp->q_lock, flags);
2348 if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
2349 spin_unlock_irqrestore(&qp->q_lock, flags);
2350 *bad_wr = wr;
2351 return -EINVAL;
2352 }
2353 while (wr) {
2354 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
2355 wr->num_sge > qp->rq.max_sges) {
2356 *bad_wr = wr;
2357 status = -ENOMEM;
2358 break;
2359 }
2360 rqe = ocrdma_hwq_head(&qp->rq);
2361 ocrdma_build_rqe(rqe, wr, 0);
2362
2363 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
2364 /* make sure rqe is written before adapter can access it */
2365 wmb();
2366
2367 /* inform hw to start processing it */
2368 ocrdma_ring_rq_db(qp);
2369
2370 /* update pointer, counter for next wr */
2371 ocrdma_hwq_inc_head(&qp->rq);
2372 wr = wr->next;
2373 }
2374 spin_unlock_irqrestore(&qp->q_lock, flags);
2375 return status;
2376}
2377
2378/* cqe for srq's rqe can potentially arrive out of order.
2379 * index gives the entry in the shadow table where to store
2380 * the wr_id. tag/index is returned in cqe to reference back
2381 * for a given rqe.
2382 */
2383static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
2384{
2385 int row = 0;
2386 int indx = 0;
2387
2388 for (row = 0; row < srq->bit_fields_len; row++) {
2389 if (srq->idx_bit_fields[row]) {
2390 indx = ffs(srq->idx_bit_fields[row]);
2391 indx = (row * 32) + (indx - 1);
ssh10db287ec2016-12-24 21:50:06 +05302392 BUG_ON(indx >= srq->rq.max_cnt);
Parav Panditfe2caef2012-03-21 04:09:06 +05302393 ocrdma_srq_toggle_bit(srq, indx);
2394 break;
2395 }
2396 }
2397
ssh10db287ec2016-12-24 21:50:06 +05302398 BUG_ON(row == srq->bit_fields_len);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302399 return indx + 1; /* Use from index 1 */
Parav Panditfe2caef2012-03-21 04:09:06 +05302400}
2401
2402static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
2403{
2404 u32 val = srq->rq.dbid | (1 << 16);
2405
2406 iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
2407}
2408
2409int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
2410 struct ib_recv_wr **bad_wr)
2411{
2412 int status = 0;
2413 unsigned long flags;
2414 struct ocrdma_srq *srq;
2415 struct ocrdma_hdr_wqe *rqe;
2416 u16 tag;
2417
2418 srq = get_ocrdma_srq(ibsrq);
2419
2420 spin_lock_irqsave(&srq->q_lock, flags);
2421 while (wr) {
2422 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
2423 wr->num_sge > srq->rq.max_sges) {
2424 status = -ENOMEM;
2425 *bad_wr = wr;
2426 break;
2427 }
2428 tag = ocrdma_srq_get_idx(srq);
2429 rqe = ocrdma_hwq_head(&srq->rq);
2430 ocrdma_build_rqe(rqe, wr, tag);
2431
2432 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
2433 /* make sure rqe is written before adapter can perform DMA */
2434 wmb();
2435 /* inform hw to start processing it */
2436 ocrdma_ring_srq_db(srq);
2437 /* update pointer, counter for next wr */
2438 ocrdma_hwq_inc_head(&srq->rq);
2439 wr = wr->next;
2440 }
2441 spin_unlock_irqrestore(&srq->q_lock, flags);
2442 return status;
2443}
2444
2445static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
2446{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302447 enum ib_wc_status ibwc_status;
Parav Panditfe2caef2012-03-21 04:09:06 +05302448
2449 switch (status) {
2450 case OCRDMA_CQE_GENERAL_ERR:
2451 ibwc_status = IB_WC_GENERAL_ERR;
2452 break;
2453 case OCRDMA_CQE_LOC_LEN_ERR:
2454 ibwc_status = IB_WC_LOC_LEN_ERR;
2455 break;
2456 case OCRDMA_CQE_LOC_QP_OP_ERR:
2457 ibwc_status = IB_WC_LOC_QP_OP_ERR;
2458 break;
2459 case OCRDMA_CQE_LOC_EEC_OP_ERR:
2460 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
2461 break;
2462 case OCRDMA_CQE_LOC_PROT_ERR:
2463 ibwc_status = IB_WC_LOC_PROT_ERR;
2464 break;
2465 case OCRDMA_CQE_WR_FLUSH_ERR:
2466 ibwc_status = IB_WC_WR_FLUSH_ERR;
2467 break;
2468 case OCRDMA_CQE_MW_BIND_ERR:
2469 ibwc_status = IB_WC_MW_BIND_ERR;
2470 break;
2471 case OCRDMA_CQE_BAD_RESP_ERR:
2472 ibwc_status = IB_WC_BAD_RESP_ERR;
2473 break;
2474 case OCRDMA_CQE_LOC_ACCESS_ERR:
2475 ibwc_status = IB_WC_LOC_ACCESS_ERR;
2476 break;
2477 case OCRDMA_CQE_REM_INV_REQ_ERR:
2478 ibwc_status = IB_WC_REM_INV_REQ_ERR;
2479 break;
2480 case OCRDMA_CQE_REM_ACCESS_ERR:
2481 ibwc_status = IB_WC_REM_ACCESS_ERR;
2482 break;
2483 case OCRDMA_CQE_REM_OP_ERR:
2484 ibwc_status = IB_WC_REM_OP_ERR;
2485 break;
2486 case OCRDMA_CQE_RETRY_EXC_ERR:
2487 ibwc_status = IB_WC_RETRY_EXC_ERR;
2488 break;
2489 case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2490 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2491 break;
2492 case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2493 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2494 break;
2495 case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2496 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2497 break;
2498 case OCRDMA_CQE_REM_ABORT_ERR:
2499 ibwc_status = IB_WC_REM_ABORT_ERR;
2500 break;
2501 case OCRDMA_CQE_INV_EECN_ERR:
2502 ibwc_status = IB_WC_INV_EECN_ERR;
2503 break;
2504 case OCRDMA_CQE_INV_EEC_STATE_ERR:
2505 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2506 break;
2507 case OCRDMA_CQE_FATAL_ERR:
2508 ibwc_status = IB_WC_FATAL_ERR;
2509 break;
2510 case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2511 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2512 break;
2513 default:
2514 ibwc_status = IB_WC_GENERAL_ERR;
2515 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002516 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302517 return ibwc_status;
2518}
2519
2520static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2521 u32 wqe_idx)
2522{
2523 struct ocrdma_hdr_wqe *hdr;
2524 struct ocrdma_sge *rw;
2525 int opcode;
2526
2527 hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2528
2529 ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2530 /* Undo the hdr->cw swap */
2531 opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2532 switch (opcode) {
2533 case OCRDMA_WRITE:
2534 ibwc->opcode = IB_WC_RDMA_WRITE;
2535 break;
2536 case OCRDMA_READ:
2537 rw = (struct ocrdma_sge *)(hdr + 1);
2538 ibwc->opcode = IB_WC_RDMA_READ;
2539 ibwc->byte_len = rw->len;
2540 break;
2541 case OCRDMA_SEND:
2542 ibwc->opcode = IB_WC_SEND;
2543 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302544 case OCRDMA_FR_MR:
Sagi Grimberg191cfed2015-10-13 19:11:44 +03002545 ibwc->opcode = IB_WC_REG_MR;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302546 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302547 case OCRDMA_LKEY_INV:
2548 ibwc->opcode = IB_WC_LOCAL_INV;
2549 break;
2550 default:
2551 ibwc->status = IB_WC_GENERAL_ERR;
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002552 pr_err("%s() invalid opcode received = 0x%x\n",
2553 __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
Parav Panditfe2caef2012-03-21 04:09:06 +05302554 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002555 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302556}
2557
2558static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2559 struct ocrdma_cqe *cqe)
2560{
2561 if (is_cqe_for_sq(cqe)) {
2562 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2563 cqe->flags_status_srcqpn) &
2564 ~OCRDMA_CQE_STATUS_MASK);
2565 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2566 cqe->flags_status_srcqpn) |
2567 (OCRDMA_CQE_WR_FLUSH_ERR <<
2568 OCRDMA_CQE_STATUS_SHIFT));
2569 } else {
2570 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2571 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2572 cqe->flags_status_srcqpn) &
2573 ~OCRDMA_CQE_UD_STATUS_MASK);
2574 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2575 cqe->flags_status_srcqpn) |
2576 (OCRDMA_CQE_WR_FLUSH_ERR <<
2577 OCRDMA_CQE_UD_STATUS_SHIFT));
2578 } else {
2579 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2580 cqe->flags_status_srcqpn) &
2581 ~OCRDMA_CQE_STATUS_MASK);
2582 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2583 cqe->flags_status_srcqpn) |
2584 (OCRDMA_CQE_WR_FLUSH_ERR <<
2585 OCRDMA_CQE_STATUS_SHIFT));
2586 }
2587 }
2588}
2589
2590static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2591 struct ocrdma_qp *qp, int status)
2592{
2593 bool expand = false;
2594
2595 ibwc->byte_len = 0;
2596 ibwc->qp = &qp->ibqp;
2597 ibwc->status = ocrdma_to_ibwc_err(status);
2598
2599 ocrdma_flush_qp(qp);
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05302600 ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05302601
2602 /* if wqe/rqe pending for which cqe needs to be returned,
2603 * trigger inflating it.
2604 */
2605 if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2606 expand = true;
2607 ocrdma_set_cqe_status_flushed(qp, cqe);
2608 }
2609 return expand;
2610}
2611
2612static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2613 struct ocrdma_qp *qp, int status)
2614{
2615 ibwc->opcode = IB_WC_RECV;
2616 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2617 ocrdma_hwq_inc_tail(&qp->rq);
2618
2619 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2620}
2621
2622static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2623 struct ocrdma_qp *qp, int status)
2624{
2625 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2626 ocrdma_hwq_inc_tail(&qp->sq);
2627
2628 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2629}
2630
2631
2632static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2633 struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2634 bool *polled, bool *stop)
2635{
2636 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302637 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302638 int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2639 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302640 if (status < OCRDMA_MAX_CQE_ERR)
2641 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302642
2643 /* when hw sq is empty, but rq is not empty, so we continue
2644 * to keep the cqe in order to get the cq event again.
2645 */
2646 if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2647 /* when cq for rq and sq is same, it is safe to return
2648 * flush cqe for RQEs.
2649 */
2650 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2651 *polled = true;
2652 status = OCRDMA_CQE_WR_FLUSH_ERR;
2653 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2654 } else {
2655 /* stop processing further cqe as this cqe is used for
2656 * triggering cq event on buddy cq of RQ.
2657 * When QP is destroyed, this cqe will be removed
2658 * from the cq's hardware q.
2659 */
2660 *polled = false;
2661 *stop = true;
2662 expand = false;
2663 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302664 } else if (is_hw_sq_empty(qp)) {
2665 /* Do nothing */
2666 expand = false;
2667 *polled = false;
2668 *stop = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302669 } else {
2670 *polled = true;
2671 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2672 }
2673 return expand;
2674}
2675
2676static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2677 struct ocrdma_cqe *cqe,
2678 struct ib_wc *ibwc, bool *polled)
2679{
2680 bool expand = false;
2681 int tail = qp->sq.tail;
2682 u32 wqe_idx;
2683
2684 if (!qp->wqe_wr_id_tbl[tail].signaled) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302685 *polled = false; /* WC cannot be consumed yet */
2686 } else {
2687 ibwc->status = IB_WC_SUCCESS;
2688 ibwc->wc_flags = 0;
2689 ibwc->qp = &qp->ibqp;
2690 ocrdma_update_wc(qp, ibwc, tail);
2691 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302692 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302693 wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
2694 OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
Parav Panditae3bca92012-08-17 14:45:33 +00002695 if (tail != wqe_idx)
2696 expand = true; /* Coalesced CQE can't be consumed yet */
2697
Parav Panditfe2caef2012-03-21 04:09:06 +05302698 ocrdma_hwq_inc_tail(&qp->sq);
2699 return expand;
2700}
2701
2702static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2703 struct ib_wc *ibwc, bool *polled, bool *stop)
2704{
2705 int status;
2706 bool expand;
2707
2708 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2709 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2710
2711 if (status == OCRDMA_CQE_SUCCESS)
2712 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2713 else
2714 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2715 return expand;
2716}
2717
Devesh Sharma6b062662016-01-28 08:59:57 -05002718static int ocrdma_update_ud_rcqe(struct ocrdma_dev *dev, struct ib_wc *ibwc,
2719 struct ocrdma_cqe *cqe)
Parav Panditfe2caef2012-03-21 04:09:06 +05302720{
2721 int status;
Devesh Sharma6b062662016-01-28 08:59:57 -05002722 u16 hdr_type = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302723
2724 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2725 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2726 ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2727 OCRDMA_CQE_SRCQP_MASK;
Selvin Xavieraff3ead2016-02-05 20:06:39 +05302728 ibwc->pkey_index = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05302729 ibwc->wc_flags = IB_WC_GRH;
2730 ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
Devesh Sharma6b062662016-01-28 08:59:57 -05002731 OCRDMA_CQE_UD_XFER_LEN_SHIFT) &
2732 OCRDMA_CQE_UD_XFER_LEN_MASK;
2733
2734 if (ocrdma_is_udp_encap_supported(dev)) {
2735 hdr_type = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2736 OCRDMA_CQE_UD_L3TYPE_SHIFT) &
2737 OCRDMA_CQE_UD_L3TYPE_MASK;
2738 ibwc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
2739 ibwc->network_hdr_type = hdr_type;
2740 }
2741
Parav Panditfe2caef2012-03-21 04:09:06 +05302742 return status;
2743}
2744
2745static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2746 struct ocrdma_cqe *cqe,
2747 struct ocrdma_qp *qp)
2748{
2749 unsigned long flags;
2750 struct ocrdma_srq *srq;
2751 u32 wqe_idx;
2752
2753 srq = get_ocrdma_srq(qp->ibqp.srq);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302754 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302755 OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
ssh10db287ec2016-12-24 21:50:06 +05302756 BUG_ON(wqe_idx < 1);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302757
Parav Panditfe2caef2012-03-21 04:09:06 +05302758 ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2759 spin_lock_irqsave(&srq->q_lock, flags);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302760 ocrdma_srq_toggle_bit(srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05302761 spin_unlock_irqrestore(&srq->q_lock, flags);
2762 ocrdma_hwq_inc_tail(&srq->rq);
2763}
2764
2765static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2766 struct ib_wc *ibwc, bool *polled, bool *stop,
2767 int status)
2768{
2769 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302770 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2771
2772 if (status < OCRDMA_MAX_CQE_ERR)
2773 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302774
2775 /* when hw_rq is empty, but wq is not empty, so continue
2776 * to keep the cqe to get the cq event again.
2777 */
2778 if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2779 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2780 *polled = true;
2781 status = OCRDMA_CQE_WR_FLUSH_ERR;
2782 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2783 } else {
2784 *polled = false;
2785 *stop = true;
2786 expand = false;
2787 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302788 } else if (is_hw_rq_empty(qp)) {
2789 /* Do nothing */
2790 expand = false;
2791 *polled = false;
2792 *stop = false;
Parav Pandita3698a92012-06-11 16:39:20 +05302793 } else {
2794 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302795 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
Parav Pandita3698a92012-06-11 16:39:20 +05302796 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302797 return expand;
2798}
2799
2800static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2801 struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2802{
Devesh Sharma6b062662016-01-28 08:59:57 -05002803 struct ocrdma_dev *dev;
2804
2805 dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302806 ibwc->opcode = IB_WC_RECV;
2807 ibwc->qp = &qp->ibqp;
2808 ibwc->status = IB_WC_SUCCESS;
2809
2810 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
Devesh Sharma6b062662016-01-28 08:59:57 -05002811 ocrdma_update_ud_rcqe(dev, ibwc, cqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05302812 else
2813 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2814
2815 if (is_cqe_imm(cqe)) {
2816 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2817 ibwc->wc_flags |= IB_WC_WITH_IMM;
2818 } else if (is_cqe_wr_imm(cqe)) {
2819 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2820 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2821 ibwc->wc_flags |= IB_WC_WITH_IMM;
2822 } else if (is_cqe_invalidated(cqe)) {
2823 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2824 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2825 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302826 if (qp->ibqp.srq) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302827 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302828 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302829 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2830 ocrdma_hwq_inc_tail(&qp->rq);
2831 }
2832}
2833
2834static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2835 struct ib_wc *ibwc, bool *polled, bool *stop)
2836{
2837 int status;
2838 bool expand = false;
2839
2840 ibwc->wc_flags = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302841 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302842 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2843 OCRDMA_CQE_UD_STATUS_MASK) >>
2844 OCRDMA_CQE_UD_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302845 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302846 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2847 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302848 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302849
2850 if (status == OCRDMA_CQE_SUCCESS) {
2851 *polled = true;
2852 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2853 } else {
2854 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2855 status);
2856 }
2857 return expand;
2858}
2859
2860static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2861 u16 cur_getp)
2862{
2863 if (cq->phase_change) {
2864 if (cur_getp == 0)
2865 cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302866 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302867 /* clear valid bit */
2868 cqe->flags_status_srcqpn = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302869 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302870}
2871
2872static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2873 struct ib_wc *ibwc)
2874{
2875 u16 qpn = 0;
2876 int i = 0;
2877 bool expand = false;
2878 int polled_hw_cqes = 0;
2879 struct ocrdma_qp *qp = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302880 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302881 struct ocrdma_cqe *cqe;
2882 u16 cur_getp; bool polled = false; bool stop = false;
2883
2884 cur_getp = cq->getp;
2885 while (num_entries) {
2886 cqe = cq->va + cur_getp;
2887 /* check whether valid cqe or not */
2888 if (!is_cqe_valid(cq, cqe))
2889 break;
2890 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2891 /* ignore discarded cqe */
2892 if (qpn == 0)
2893 goto skip_cqe;
2894 qp = dev->qp_tbl[qpn];
2895 BUG_ON(qp == NULL);
2896
2897 if (is_cqe_for_sq(cqe)) {
2898 expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2899 &stop);
2900 } else {
2901 expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2902 &stop);
2903 }
2904 if (expand)
2905 goto expand_cqe;
2906 if (stop)
2907 goto stop_cqe;
2908 /* clear qpn to avoid duplicate processing by discard_cqe() */
2909 cqe->cmn.qpn = 0;
2910skip_cqe:
2911 polled_hw_cqes += 1;
2912 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2913 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2914expand_cqe:
2915 if (polled) {
2916 num_entries -= 1;
2917 i += 1;
2918 ibwc = ibwc + 1;
2919 polled = false;
2920 }
2921 }
2922stop_cqe:
2923 cq->getp = cur_getp;
Devesh Sharmab41f7852016-02-11 00:21:52 -05002924
2925 if (polled_hw_cqes)
2926 ocrdma_ring_cq_db(dev, cq->id, false, false, polled_hw_cqes);
Devesh Sharmaea617622014-02-04 11:56:54 +05302927
Parav Panditfe2caef2012-03-21 04:09:06 +05302928 return i;
2929}
2930
2931/* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2932static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2933 struct ocrdma_qp *qp, struct ib_wc *ibwc)
2934{
2935 int err_cqes = 0;
2936
2937 while (num_entries) {
2938 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2939 break;
2940 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2941 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2942 ocrdma_hwq_inc_tail(&qp->sq);
2943 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2944 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2945 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302946 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302947 return err_cqes;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302948 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302949 ibwc->byte_len = 0;
2950 ibwc->status = IB_WC_WR_FLUSH_ERR;
2951 ibwc = ibwc + 1;
2952 err_cqes += 1;
2953 num_entries -= 1;
2954 }
2955 return err_cqes;
2956}
2957
2958int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2959{
2960 int cqes_to_poll = num_entries;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302961 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2962 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302963 int num_os_cqe = 0, err_cqes = 0;
2964 struct ocrdma_qp *qp;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302965 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05302966
2967 /* poll cqes from adapter CQ */
2968 spin_lock_irqsave(&cq->cq_lock, flags);
2969 num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2970 spin_unlock_irqrestore(&cq->cq_lock, flags);
2971 cqes_to_poll -= num_os_cqe;
2972
2973 if (cqes_to_poll) {
2974 wc = wc + num_os_cqe;
2975 /* adapter returns single error cqe when qp moves to
2976 * error state. So insert error cqes with wc_status as
2977 * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2978 * respectively which uses this CQ.
2979 */
2980 spin_lock_irqsave(&dev->flush_q_lock, flags);
2981 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2982 if (cqes_to_poll == 0)
2983 break;
2984 err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2985 cqes_to_poll -= err_cqes;
2986 num_os_cqe += err_cqes;
2987 wc = wc + err_cqes;
2988 }
2989 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2990 }
2991 return num_os_cqe;
2992}
2993
2994int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2995{
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302996 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2997 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302998 u16 cq_id;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302999 unsigned long flags;
Devesh Sharmaea617622014-02-04 11:56:54 +05303000 bool arm_needed = false, sol_needed = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05303001
Parav Panditfe2caef2012-03-21 04:09:06 +05303002 cq_id = cq->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05303003
3004 spin_lock_irqsave(&cq->cq_lock, flags);
3005 if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05303006 arm_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05303007 if (cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05303008 sol_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05303009
Devesh Sharmab41f7852016-02-11 00:21:52 -05003010 ocrdma_ring_cq_db(dev, cq_id, arm_needed, sol_needed, 0);
Parav Panditfe2caef2012-03-21 04:09:06 +05303011 spin_unlock_irqrestore(&cq->cq_lock, flags);
Devesh Sharmaea617622014-02-04 11:56:54 +05303012
Parav Panditfe2caef2012-03-21 04:09:06 +05303013 return 0;
3014}
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303015
Sagi Grimbergcacb7d52015-07-30 10:32:43 +03003016struct ib_mr *ocrdma_alloc_mr(struct ib_pd *ibpd,
3017 enum ib_mr_type mr_type,
3018 u32 max_num_sg)
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303019{
3020 int status;
3021 struct ocrdma_mr *mr;
3022 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
3023 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
3024
Sagi Grimbergcacb7d52015-07-30 10:32:43 +03003025 if (mr_type != IB_MR_TYPE_MEM_REG)
3026 return ERR_PTR(-EINVAL);
3027
3028 if (max_num_sg > dev->attr.max_pages_per_frmr)
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303029 return ERR_PTR(-EINVAL);
3030
3031 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3032 if (!mr)
3033 return ERR_PTR(-ENOMEM);
3034
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03003035 mr->pages = kcalloc(max_num_sg, sizeof(u64), GFP_KERNEL);
3036 if (!mr->pages) {
3037 status = -ENOMEM;
3038 goto pl_err;
3039 }
3040
Sagi Grimbergcacb7d52015-07-30 10:32:43 +03003041 status = ocrdma_get_pbl_info(dev, mr, max_num_sg);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303042 if (status)
3043 goto pbl_err;
3044 mr->hwmr.fr_mr = 1;
3045 mr->hwmr.remote_rd = 0;
3046 mr->hwmr.remote_wr = 0;
3047 mr->hwmr.local_rd = 0;
3048 mr->hwmr.local_wr = 0;
3049 mr->hwmr.mw_bind = 0;
3050 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
3051 if (status)
3052 goto pbl_err;
3053 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
3054 if (status)
3055 goto mbx_err;
3056 mr->ibmr.rkey = mr->hwmr.lkey;
3057 mr->ibmr.lkey = mr->hwmr.lkey;
Roland Dreier7a1e89d2014-03-17 23:14:17 -07003058 dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] =
3059 (unsigned long) mr;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303060 return &mr->ibmr;
3061mbx_err:
3062 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3063pbl_err:
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03003064 kfree(mr->pages);
3065pl_err:
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303066 kfree(mr);
3067 return ERR_PTR(-ENOMEM);
3068}
3069
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03003070static int ocrdma_set_page(struct ib_mr *ibmr, u64 addr)
3071{
3072 struct ocrdma_mr *mr = get_ocrdma_mr(ibmr);
3073
3074 if (unlikely(mr->npages == mr->hwmr.num_pbes))
3075 return -ENOMEM;
3076
3077 mr->pages[mr->npages++] = addr;
3078
3079 return 0;
3080}
3081
Christoph Hellwigff2ba992016-05-03 18:01:04 +02003082int ocrdma_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
Bart Van Assche9aa8b322016-05-12 10:49:15 -07003083 unsigned int *sg_offset)
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03003084{
3085 struct ocrdma_mr *mr = get_ocrdma_mr(ibmr);
3086
3087 mr->npages = 0;
3088
Christoph Hellwigff2ba992016-05-03 18:01:04 +02003089 return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, ocrdma_set_page);
Sagi Grimberg2eaa1c52015-10-13 19:11:28 +03003090}