blob: 19566907b4d25bac0710bfe7b354e8175e64a5ae [file] [log] [blame]
Parav Panditfe2caef2012-03-21 04:09:06 +05301/*******************************************************************
2 * This file is part of the Emulex RoCE Device Driver for *
3 * RoCE (RDMA over Converged Ethernet) adapters. *
4 * Copyright (C) 2008-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of version 2 of the GNU General *
10 * Public License as published by the Free Software Foundation. *
11 * This program is distributed in the hope that it will be useful. *
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16 * TO BE LEGALLY INVALID. See the GNU General Public License for *
17 * more details, a copy of which can be found in the file COPYING *
18 * included with this package. *
19 *
20 * Contact Information:
21 * linux-drivers@emulex.com
22 *
23 * Emulex
24 * 3333 Susan Street
25 * Costa Mesa, CA 92626
26 *******************************************************************/
27
28#include <linux/dma-mapping.h>
29#include <rdma/ib_verbs.h>
30#include <rdma/ib_user_verbs.h>
31#include <rdma/iw_cm.h>
32#include <rdma/ib_umem.h>
33#include <rdma/ib_addr.h>
34
35#include "ocrdma.h"
36#include "ocrdma_hw.h"
37#include "ocrdma_verbs.h"
38#include "ocrdma_abi.h"
39
40int ocrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
41{
42 if (index > 1)
43 return -EINVAL;
44
45 *pkey = 0xffff;
46 return 0;
47}
48
49int ocrdma_query_gid(struct ib_device *ibdev, u8 port,
50 int index, union ib_gid *sgid)
51{
52 struct ocrdma_dev *dev;
53
54 dev = get_ocrdma_dev(ibdev);
55 memset(sgid, 0, sizeof(*sgid));
Dan Carpenter59a39ca32015-02-16 13:01:36 +030056 if (index >= OCRDMA_MAX_SGID)
Parav Panditfe2caef2012-03-21 04:09:06 +053057 return -EINVAL;
58
59 memcpy(sgid, &dev->sgid_tbl[index], sizeof(*sgid));
60
61 return 0;
62}
63
64int ocrdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr)
65{
66 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
67
68 memset(attr, 0, sizeof *attr);
69 memcpy(&attr->fw_ver, &dev->attr.fw_ver[0],
70 min(sizeof(dev->attr.fw_ver), sizeof(attr->fw_ver)));
71 ocrdma_get_guid(dev, (u8 *)&attr->sys_image_guid);
Mitesh Ahuja033edd42014-06-10 19:32:22 +053072 attr->max_mr_size = dev->attr.max_mr_size;
Parav Panditfe2caef2012-03-21 04:09:06 +053073 attr->page_size_cap = 0xffff000;
74 attr->vendor_id = dev->nic_info.pdev->vendor;
75 attr->vendor_part_id = dev->nic_info.pdev->device;
Mitesh Ahuja96c51ab2014-07-02 11:36:06 +053076 attr->hw_ver = dev->asic_id;
Parav Panditfe2caef2012-03-21 04:09:06 +053077 attr->max_qp = dev->attr.max_qp;
Naresh Gottumukkalad3cb6c02013-08-26 15:27:40 +053078 attr->max_ah = OCRDMA_MAX_AH;
Parav Panditfe2caef2012-03-21 04:09:06 +053079 attr->max_qp_wr = dev->attr.max_wqe;
80
81 attr->device_cap_flags = IB_DEVICE_CURR_QP_STATE_MOD |
82 IB_DEVICE_RC_RNR_NAK_GEN |
83 IB_DEVICE_SHUTDOWN_PORT |
84 IB_DEVICE_SYS_IMAGE_GUID |
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +053085 IB_DEVICE_LOCAL_DMA_LKEY |
86 IB_DEVICE_MEM_MGT_EXTENSIONS;
Mahesh Vardhamanaiah634c5792012-06-08 21:26:11 +053087 attr->max_sge = min(dev->attr.max_send_sge, dev->attr.max_srq_sge);
Naresh Gottumukkalac43e9ab2013-08-26 15:27:46 +053088 attr->max_sge_rd = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +053089 attr->max_cq = dev->attr.max_cq;
90 attr->max_cqe = dev->attr.max_cqe;
91 attr->max_mr = dev->attr.max_mr;
Selvin Xavierac578ae2014-02-04 11:57:04 +053092 attr->max_mw = dev->attr.max_mw;
Parav Panditfe2caef2012-03-21 04:09:06 +053093 attr->max_pd = dev->attr.max_pd;
94 attr->atomic_cap = 0;
95 attr->max_fmr = 0;
96 attr->max_map_per_fmr = 0;
97 attr->max_qp_rd_atom =
98 min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp);
99 attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +0530100 attr->max_srq = dev->attr.max_srq;
Roland Dreierd1e09eb2012-07-07 15:13:47 -0700101 attr->max_srq_sge = dev->attr.max_srq_sge;
Parav Panditfe2caef2012-03-21 04:09:06 +0530102 attr->max_srq_wr = dev->attr.max_rqe;
103 attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay;
Devesh Sharmad6a488f2014-06-09 10:52:37 +0530104 attr->max_fast_reg_page_list_len = dev->attr.max_pages_per_frmr;
Parav Panditfe2caef2012-03-21 04:09:06 +0530105 attr->max_pkeys = 1;
106 return 0;
107}
108
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530109static inline void get_link_speed_and_width(struct ocrdma_dev *dev,
110 u8 *ib_speed, u8 *ib_width)
111{
112 int status;
113 u8 speed;
114
115 status = ocrdma_mbx_get_link_speed(dev, &speed);
116 if (status)
117 speed = OCRDMA_PHYS_LINK_SPEED_ZERO;
118
119 switch (speed) {
120 case OCRDMA_PHYS_LINK_SPEED_1GBPS:
121 *ib_speed = IB_SPEED_SDR;
122 *ib_width = IB_WIDTH_1X;
123 break;
124
125 case OCRDMA_PHYS_LINK_SPEED_10GBPS:
126 *ib_speed = IB_SPEED_QDR;
127 *ib_width = IB_WIDTH_1X;
128 break;
129
130 case OCRDMA_PHYS_LINK_SPEED_20GBPS:
131 *ib_speed = IB_SPEED_DDR;
132 *ib_width = IB_WIDTH_4X;
133 break;
134
135 case OCRDMA_PHYS_LINK_SPEED_40GBPS:
136 *ib_speed = IB_SPEED_QDR;
137 *ib_width = IB_WIDTH_4X;
138 break;
139
140 default:
141 /* Unsupported */
142 *ib_speed = IB_SPEED_SDR;
143 *ib_width = IB_WIDTH_1X;
Joe Perches2b50176d2013-10-08 16:07:22 -0700144 }
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530145}
146
Parav Panditfe2caef2012-03-21 04:09:06 +0530147int ocrdma_query_port(struct ib_device *ibdev,
148 u8 port, struct ib_port_attr *props)
149{
150 enum ib_port_state port_state;
151 struct ocrdma_dev *dev;
152 struct net_device *netdev;
153
154 dev = get_ocrdma_dev(ibdev);
155 if (port > 1) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000156 pr_err("%s(%d) invalid_port=0x%x\n", __func__,
157 dev->id, port);
Parav Panditfe2caef2012-03-21 04:09:06 +0530158 return -EINVAL;
159 }
160 netdev = dev->nic_info.netdev;
161 if (netif_running(netdev) && netif_oper_up(netdev)) {
162 port_state = IB_PORT_ACTIVE;
163 props->phys_state = 5;
164 } else {
165 port_state = IB_PORT_DOWN;
166 props->phys_state = 3;
167 }
168 props->max_mtu = IB_MTU_4096;
169 props->active_mtu = iboe_get_mtu(netdev->mtu);
170 props->lid = 0;
171 props->lmc = 0;
172 props->sm_lid = 0;
173 props->sm_sl = 0;
174 props->state = port_state;
175 props->port_cap_flags =
176 IB_PORT_CM_SUP |
177 IB_PORT_REINIT_SUP |
Moni Shouab4a26a22014-02-09 11:54:34 +0200178 IB_PORT_DEVICE_MGMT_SUP | IB_PORT_VENDOR_CLASS_SUP | IB_PORT_IP_BASED_GIDS;
Parav Panditfe2caef2012-03-21 04:09:06 +0530179 props->gid_tbl_len = OCRDMA_MAX_SGID;
180 props->pkey_tbl_len = 1;
181 props->bad_pkey_cntr = 0;
182 props->qkey_viol_cntr = 0;
Naresh Gottumukkalaf24ceba2013-08-26 15:27:47 +0530183 get_link_speed_and_width(dev, &props->active_speed,
184 &props->active_width);
Parav Panditfe2caef2012-03-21 04:09:06 +0530185 props->max_msg_sz = 0x80000000;
186 props->max_vl_num = 4;
187 return 0;
188}
189
190int ocrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
191 struct ib_port_modify *props)
192{
193 struct ocrdma_dev *dev;
194
195 dev = get_ocrdma_dev(ibdev);
196 if (port > 1) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000197 pr_err("%s(%d) invalid_port=0x%x\n", __func__, dev->id, port);
Parav Panditfe2caef2012-03-21 04:09:06 +0530198 return -EINVAL;
199 }
200 return 0;
201}
202
203static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
204 unsigned long len)
205{
206 struct ocrdma_mm *mm;
207
208 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
209 if (mm == NULL)
210 return -ENOMEM;
211 mm->key.phy_addr = phy_addr;
212 mm->key.len = len;
213 INIT_LIST_HEAD(&mm->entry);
214
215 mutex_lock(&uctx->mm_list_lock);
216 list_add_tail(&mm->entry, &uctx->mm_head);
217 mutex_unlock(&uctx->mm_list_lock);
218 return 0;
219}
220
221static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
222 unsigned long len)
223{
224 struct ocrdma_mm *mm, *tmp;
225
226 mutex_lock(&uctx->mm_list_lock);
227 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530228 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
Parav Panditfe2caef2012-03-21 04:09:06 +0530229 continue;
230
231 list_del(&mm->entry);
232 kfree(mm);
233 break;
234 }
235 mutex_unlock(&uctx->mm_list_lock);
236}
237
238static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
239 unsigned long len)
240{
241 bool found = false;
242 struct ocrdma_mm *mm;
243
244 mutex_lock(&uctx->mm_list_lock);
245 list_for_each_entry(mm, &uctx->mm_head, entry) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530246 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
Parav Panditfe2caef2012-03-21 04:09:06 +0530247 continue;
248
249 found = true;
250 break;
251 }
252 mutex_unlock(&uctx->mm_list_lock);
253 return found;
254}
255
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530256
257static u16 _ocrdma_pd_mgr_get_bitmap(struct ocrdma_dev *dev, bool dpp_pool)
258{
259 u16 pd_bitmap_idx = 0;
260 const unsigned long *pd_bitmap;
261
262 if (dpp_pool) {
263 pd_bitmap = dev->pd_mgr->pd_dpp_bitmap;
264 pd_bitmap_idx = find_first_zero_bit(pd_bitmap,
265 dev->pd_mgr->max_dpp_pd);
266 __set_bit(pd_bitmap_idx, dev->pd_mgr->pd_dpp_bitmap);
267 dev->pd_mgr->pd_dpp_count++;
268 if (dev->pd_mgr->pd_dpp_count > dev->pd_mgr->pd_dpp_thrsh)
269 dev->pd_mgr->pd_dpp_thrsh = dev->pd_mgr->pd_dpp_count;
270 } else {
271 pd_bitmap = dev->pd_mgr->pd_norm_bitmap;
272 pd_bitmap_idx = find_first_zero_bit(pd_bitmap,
273 dev->pd_mgr->max_normal_pd);
274 __set_bit(pd_bitmap_idx, dev->pd_mgr->pd_norm_bitmap);
275 dev->pd_mgr->pd_norm_count++;
276 if (dev->pd_mgr->pd_norm_count > dev->pd_mgr->pd_norm_thrsh)
277 dev->pd_mgr->pd_norm_thrsh = dev->pd_mgr->pd_norm_count;
278 }
279 return pd_bitmap_idx;
280}
281
282static int _ocrdma_pd_mgr_put_bitmap(struct ocrdma_dev *dev, u16 pd_id,
283 bool dpp_pool)
284{
285 u16 pd_count;
286 u16 pd_bit_index;
287
288 pd_count = dpp_pool ? dev->pd_mgr->pd_dpp_count :
289 dev->pd_mgr->pd_norm_count;
290 if (pd_count == 0)
291 return -EINVAL;
292
293 if (dpp_pool) {
294 pd_bit_index = pd_id - dev->pd_mgr->pd_dpp_start;
295 if (pd_bit_index >= dev->pd_mgr->max_dpp_pd) {
296 return -EINVAL;
297 } else {
298 __clear_bit(pd_bit_index, dev->pd_mgr->pd_dpp_bitmap);
299 dev->pd_mgr->pd_dpp_count--;
300 }
301 } else {
302 pd_bit_index = pd_id - dev->pd_mgr->pd_norm_start;
303 if (pd_bit_index >= dev->pd_mgr->max_normal_pd) {
304 return -EINVAL;
305 } else {
306 __clear_bit(pd_bit_index, dev->pd_mgr->pd_norm_bitmap);
307 dev->pd_mgr->pd_norm_count--;
308 }
309 }
310
311 return 0;
312}
313
314static u8 ocrdma_put_pd_num(struct ocrdma_dev *dev, u16 pd_id,
315 bool dpp_pool)
316{
317 int status;
318
319 mutex_lock(&dev->dev_lock);
320 status = _ocrdma_pd_mgr_put_bitmap(dev, pd_id, dpp_pool);
321 mutex_unlock(&dev->dev_lock);
322 return status;
323}
324
325static int ocrdma_get_pd_num(struct ocrdma_dev *dev, struct ocrdma_pd *pd)
326{
327 u16 pd_idx = 0;
328 int status = 0;
329
330 mutex_lock(&dev->dev_lock);
331 if (pd->dpp_enabled) {
332 /* try allocating DPP PD, if not available then normal PD */
333 if (dev->pd_mgr->pd_dpp_count < dev->pd_mgr->max_dpp_pd) {
334 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, true);
335 pd->id = dev->pd_mgr->pd_dpp_start + pd_idx;
336 pd->dpp_page = dev->pd_mgr->dpp_page_index + pd_idx;
337 } else if (dev->pd_mgr->pd_norm_count <
338 dev->pd_mgr->max_normal_pd) {
339 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, false);
340 pd->id = dev->pd_mgr->pd_norm_start + pd_idx;
341 pd->dpp_enabled = false;
342 } else {
343 status = -EINVAL;
344 }
345 } else {
346 if (dev->pd_mgr->pd_norm_count < dev->pd_mgr->max_normal_pd) {
347 pd_idx = _ocrdma_pd_mgr_get_bitmap(dev, false);
348 pd->id = dev->pd_mgr->pd_norm_start + pd_idx;
349 } else {
350 status = -EINVAL;
351 }
352 }
353 mutex_unlock(&dev->dev_lock);
354 return status;
355}
356
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530357static struct ocrdma_pd *_ocrdma_alloc_pd(struct ocrdma_dev *dev,
358 struct ocrdma_ucontext *uctx,
359 struct ib_udata *udata)
360{
361 struct ocrdma_pd *pd = NULL;
362 int status = 0;
363
364 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
365 if (!pd)
366 return ERR_PTR(-ENOMEM);
367
368 if (udata && uctx) {
369 pd->dpp_enabled =
Devesh Sharma21c33912014-02-04 11:56:56 +0530370 ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530371 pd->num_dpp_qp =
Devesh Sharmaa53d77a2014-06-10 19:32:17 +0530372 pd->dpp_enabled ? (dev->nic_info.db_page_size /
373 dev->attr.wqe_size) : 0;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530374 }
375
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530376 if (dev->pd_mgr->pd_prealloc_valid) {
377 status = ocrdma_get_pd_num(dev, pd);
Roland Dreier18eaf1f2015-05-29 23:10:31 -0700378 if (status == 0) {
379 return pd;
380 } else {
381 kfree(pd);
382 return ERR_PTR(status);
383 }
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530384 }
385
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530386retry:
387 status = ocrdma_mbx_alloc_pd(dev, pd);
388 if (status) {
389 if (pd->dpp_enabled) {
390 pd->dpp_enabled = false;
391 pd->num_dpp_qp = 0;
392 goto retry;
393 } else {
394 kfree(pd);
395 return ERR_PTR(status);
396 }
397 }
398
399 return pd;
400}
401
402static inline int is_ucontext_pd(struct ocrdma_ucontext *uctx,
403 struct ocrdma_pd *pd)
404{
405 return (uctx->cntxt_pd == pd ? true : false);
406}
407
408static int _ocrdma_dealloc_pd(struct ocrdma_dev *dev,
409 struct ocrdma_pd *pd)
410{
411 int status = 0;
412
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530413 if (dev->pd_mgr->pd_prealloc_valid)
414 status = ocrdma_put_pd_num(dev, pd->id, pd->dpp_enabled);
415 else
416 status = ocrdma_mbx_dealloc_pd(dev, pd);
417
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530418 kfree(pd);
419 return status;
420}
421
422static int ocrdma_alloc_ucontext_pd(struct ocrdma_dev *dev,
423 struct ocrdma_ucontext *uctx,
424 struct ib_udata *udata)
425{
426 int status = 0;
427
428 uctx->cntxt_pd = _ocrdma_alloc_pd(dev, uctx, udata);
429 if (IS_ERR(uctx->cntxt_pd)) {
430 status = PTR_ERR(uctx->cntxt_pd);
431 uctx->cntxt_pd = NULL;
432 goto err;
433 }
434
435 uctx->cntxt_pd->uctx = uctx;
436 uctx->cntxt_pd->ibpd.device = &dev->ibdev;
437err:
438 return status;
439}
440
441static int ocrdma_dealloc_ucontext_pd(struct ocrdma_ucontext *uctx)
442{
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530443 struct ocrdma_pd *pd = uctx->cntxt_pd;
444 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
445
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530446 if (uctx->pd_in_use) {
447 pr_err("%s(%d) Freeing in use pdid=0x%x.\n",
448 __func__, dev->id, pd->id);
449 }
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530450 uctx->cntxt_pd = NULL;
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530451 (void)_ocrdma_dealloc_pd(dev, pd);
452 return 0;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530453}
454
455static struct ocrdma_pd *ocrdma_get_ucontext_pd(struct ocrdma_ucontext *uctx)
456{
457 struct ocrdma_pd *pd = NULL;
458
459 mutex_lock(&uctx->mm_list_lock);
460 if (!uctx->pd_in_use) {
461 uctx->pd_in_use = true;
462 pd = uctx->cntxt_pd;
463 }
464 mutex_unlock(&uctx->mm_list_lock);
465
466 return pd;
467}
468
469static void ocrdma_release_ucontext_pd(struct ocrdma_ucontext *uctx)
470{
471 mutex_lock(&uctx->mm_list_lock);
472 uctx->pd_in_use = false;
473 mutex_unlock(&uctx->mm_list_lock);
474}
475
Parav Panditfe2caef2012-03-21 04:09:06 +0530476struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
477 struct ib_udata *udata)
478{
479 int status;
480 struct ocrdma_ucontext *ctx;
481 struct ocrdma_alloc_ucontext_resp resp;
482 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
483 struct pci_dev *pdev = dev->nic_info.pdev;
484 u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
485
486 if (!udata)
487 return ERR_PTR(-EFAULT);
488 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
489 if (!ctx)
490 return ERR_PTR(-ENOMEM);
Parav Panditfe2caef2012-03-21 04:09:06 +0530491 INIT_LIST_HEAD(&ctx->mm_head);
492 mutex_init(&ctx->mm_list_lock);
493
494 ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
495 &ctx->ah_tbl.pa, GFP_KERNEL);
496 if (!ctx->ah_tbl.va) {
497 kfree(ctx);
498 return ERR_PTR(-ENOMEM);
499 }
500 memset(ctx->ah_tbl.va, 0, map_len);
501 ctx->ah_tbl.len = map_len;
502
Dan Carpenter63ea3742013-07-29 22:34:29 +0300503 memset(&resp, 0, sizeof(resp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530504 resp.ah_tbl_len = ctx->ah_tbl.len;
Devesh Sharma1b76d382014-09-05 19:35:40 +0530505 resp.ah_tbl_page = virt_to_phys(ctx->ah_tbl.va);
Parav Panditfe2caef2012-03-21 04:09:06 +0530506
507 status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
508 if (status)
509 goto map_err;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530510
511 status = ocrdma_alloc_ucontext_pd(dev, ctx, udata);
512 if (status)
513 goto pd_err;
514
Parav Panditfe2caef2012-03-21 04:09:06 +0530515 resp.dev_id = dev->id;
516 resp.max_inline_data = dev->attr.max_inline_data;
517 resp.wqe_size = dev->attr.wqe_size;
518 resp.rqe_size = dev->attr.rqe_size;
519 resp.dpp_wqe_size = dev->attr.wqe_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530520
521 memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
522 status = ib_copy_to_udata(udata, &resp, sizeof(resp));
523 if (status)
524 goto cpy_err;
525 return &ctx->ibucontext;
526
527cpy_err:
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530528pd_err:
Parav Panditfe2caef2012-03-21 04:09:06 +0530529 ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
530map_err:
531 dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
532 ctx->ah_tbl.pa);
533 kfree(ctx);
534 return ERR_PTR(status);
535}
536
537int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
538{
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530539 int status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530540 struct ocrdma_mm *mm, *tmp;
541 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530542 struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
543 struct pci_dev *pdev = dev->nic_info.pdev;
Parav Panditfe2caef2012-03-21 04:09:06 +0530544
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530545 status = ocrdma_dealloc_ucontext_pd(uctx);
546
Parav Panditfe2caef2012-03-21 04:09:06 +0530547 ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
548 dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
549 uctx->ah_tbl.pa);
550
551 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
552 list_del(&mm->entry);
553 kfree(mm);
554 }
555 kfree(uctx);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530556 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530557}
558
559int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
560{
561 struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530562 struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530563 unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
564 u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
565 unsigned long len = (vma->vm_end - vma->vm_start);
566 int status = 0;
567 bool found;
568
569 if (vma->vm_start & (PAGE_SIZE - 1))
570 return -EINVAL;
571 found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
572 if (!found)
573 return -EINVAL;
574
575 if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
576 dev->nic_info.db_total_size)) &&
577 (len <= dev->nic_info.db_page_size)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530578 if (vma->vm_flags & VM_READ)
579 return -EPERM;
580
581 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Parav Panditfe2caef2012-03-21 04:09:06 +0530582 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
583 len, vma->vm_page_prot);
584 } else if (dev->nic_info.dpp_unmapped_len &&
585 (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
586 (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
587 dev->nic_info.dpp_unmapped_len)) &&
588 (len <= dev->nic_info.dpp_unmapped_len)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530589 if (vma->vm_flags & VM_READ)
590 return -EPERM;
591
Parav Panditfe2caef2012-03-21 04:09:06 +0530592 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
593 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
594 len, vma->vm_page_prot);
595 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +0530596 status = remap_pfn_range(vma, vma->vm_start,
597 vma->vm_pgoff, len, vma->vm_page_prot);
598 }
599 return status;
600}
601
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530602static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
Parav Panditfe2caef2012-03-21 04:09:06 +0530603 struct ib_ucontext *ib_ctx,
604 struct ib_udata *udata)
605{
606 int status;
607 u64 db_page_addr;
Roland Dreierda496432012-04-16 11:32:17 -0700608 u64 dpp_page_addr = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530609 u32 db_page_size;
610 struct ocrdma_alloc_pd_uresp rsp;
611 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
612
Dan Carpenter63ea3742013-07-29 22:34:29 +0300613 memset(&rsp, 0, sizeof(rsp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530614 rsp.id = pd->id;
615 rsp.dpp_enabled = pd->dpp_enabled;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530616 db_page_addr = ocrdma_get_db_addr(dev, pd->id);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530617 db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530618
619 status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
620 if (status)
621 return status;
622
623 if (pd->dpp_enabled) {
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530624 dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530625 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530626 status = ocrdma_add_mmap(uctx, dpp_page_addr,
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530627 PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530628 if (status)
629 goto dpp_map_err;
630 rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
631 rsp.dpp_page_addr_lo = dpp_page_addr;
632 }
633
634 status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
635 if (status)
636 goto ucopy_err;
637
638 pd->uctx = uctx;
639 return 0;
640
641ucopy_err:
Roland Dreierda496432012-04-16 11:32:17 -0700642 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530643 ocrdma_del_mmap(pd->uctx, dpp_page_addr, PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530644dpp_map_err:
645 ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
646 return status;
647}
648
649struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev,
650 struct ib_ucontext *context,
651 struct ib_udata *udata)
652{
653 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
654 struct ocrdma_pd *pd;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530655 struct ocrdma_ucontext *uctx = NULL;
Parav Panditfe2caef2012-03-21 04:09:06 +0530656 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530657 u8 is_uctx_pd = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530658
Parav Panditfe2caef2012-03-21 04:09:06 +0530659 if (udata && context) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530660 uctx = get_ocrdma_ucontext(context);
661 pd = ocrdma_get_ucontext_pd(uctx);
662 if (pd) {
663 is_uctx_pd = true;
664 goto pd_mapping;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530665 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530666 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530667
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530668 pd = _ocrdma_alloc_pd(dev, uctx, udata);
669 if (IS_ERR(pd)) {
670 status = PTR_ERR(pd);
671 goto exit;
672 }
673
674pd_mapping:
Parav Panditfe2caef2012-03-21 04:09:06 +0530675 if (udata && context) {
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530676 status = ocrdma_copy_pd_uresp(dev, pd, context, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +0530677 if (status)
678 goto err;
679 }
680 return &pd->ibpd;
681
682err:
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530683 if (is_uctx_pd) {
684 ocrdma_release_ucontext_pd(uctx);
685 } else {
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530686 status = _ocrdma_dealloc_pd(dev, pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530687 }
688exit:
Parav Panditfe2caef2012-03-21 04:09:06 +0530689 return ERR_PTR(status);
690}
691
692int ocrdma_dealloc_pd(struct ib_pd *ibpd)
693{
694 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530695 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530696 struct ocrdma_ucontext *uctx = NULL;
697 int status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530698 u64 usr_db;
699
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530700 uctx = pd->uctx;
701 if (uctx) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530702 u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530703 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530704 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530705 ocrdma_del_mmap(pd->uctx, dpp_db, PAGE_SIZE);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530706 usr_db = ocrdma_get_db_addr(dev, pd->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530707 ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530708
709 if (is_ucontext_pd(uctx, pd)) {
710 ocrdma_release_ucontext_pd(uctx);
711 return status;
712 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530713 }
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530714 status = _ocrdma_dealloc_pd(dev, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530715 return status;
716}
717
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530718static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
719 u32 pdid, int acc, u32 num_pbls, u32 addr_check)
Parav Panditfe2caef2012-03-21 04:09:06 +0530720{
721 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530722
Parav Panditfe2caef2012-03-21 04:09:06 +0530723 mr->hwmr.fr_mr = 0;
724 mr->hwmr.local_rd = 1;
725 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
726 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
727 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
728 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
729 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
730 mr->hwmr.num_pbls = num_pbls;
731
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530732 status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
733 if (status)
734 return status;
735
Parav Panditfe2caef2012-03-21 04:09:06 +0530736 mr->ibmr.lkey = mr->hwmr.lkey;
737 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
738 mr->ibmr.rkey = mr->hwmr.lkey;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530739 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530740}
741
742struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
743{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530744 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530745 struct ocrdma_mr *mr;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530746 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
747 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530748
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530749 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
750 pr_err("%s err, invalid access rights\n", __func__);
751 return ERR_PTR(-EINVAL);
752 }
753
754 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
755 if (!mr)
756 return ERR_PTR(-ENOMEM);
757
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530758 status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530759 OCRDMA_ADDR_CHECK_DISABLE);
760 if (status) {
761 kfree(mr);
762 return ERR_PTR(status);
763 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530764
765 return &mr->ibmr;
766}
767
768static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
769 struct ocrdma_hw_mr *mr)
770{
771 struct pci_dev *pdev = dev->nic_info.pdev;
772 int i = 0;
773
774 if (mr->pbl_table) {
775 for (i = 0; i < mr->num_pbls; i++) {
776 if (!mr->pbl_table[i].va)
777 continue;
778 dma_free_coherent(&pdev->dev, mr->pbl_size,
779 mr->pbl_table[i].va,
780 mr->pbl_table[i].pa);
781 }
782 kfree(mr->pbl_table);
783 mr->pbl_table = NULL;
784 }
785}
786
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530787static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
788 u32 num_pbes)
Parav Panditfe2caef2012-03-21 04:09:06 +0530789{
790 u32 num_pbls = 0;
791 u32 idx = 0;
792 int status = 0;
793 u32 pbl_size;
794
795 do {
796 pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
797 if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
798 status = -EFAULT;
799 break;
800 }
801 num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
802 num_pbls = num_pbls / (pbl_size / sizeof(u64));
803 idx++;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530804 } while (num_pbls >= dev->attr.max_num_mr_pbl);
Parav Panditfe2caef2012-03-21 04:09:06 +0530805
806 mr->hwmr.num_pbes = num_pbes;
807 mr->hwmr.num_pbls = num_pbls;
808 mr->hwmr.pbl_size = pbl_size;
809 return status;
810}
811
812static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
813{
814 int status = 0;
815 int i;
816 u32 dma_len = mr->pbl_size;
817 struct pci_dev *pdev = dev->nic_info.pdev;
818 void *va;
819 dma_addr_t pa;
820
821 mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) *
822 mr->num_pbls, GFP_KERNEL);
823
824 if (!mr->pbl_table)
825 return -ENOMEM;
826
827 for (i = 0; i < mr->num_pbls; i++) {
828 va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
829 if (!va) {
830 ocrdma_free_mr_pbl_tbl(dev, mr);
831 status = -ENOMEM;
832 break;
833 }
834 memset(va, 0, dma_len);
835 mr->pbl_table[i].va = va;
836 mr->pbl_table[i].pa = pa;
837 }
838 return status;
839}
840
841static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
842 u32 num_pbes)
843{
844 struct ocrdma_pbe *pbe;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200845 struct scatterlist *sg;
Parav Panditfe2caef2012-03-21 04:09:06 +0530846 struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
847 struct ib_umem *umem = mr->umem;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200848 int shift, pg_cnt, pages, pbe_cnt, entry, total_num_pbes = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530849
850 if (!mr->hwmr.num_pbes)
851 return;
852
853 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
854 pbe_cnt = 0;
855
856 shift = ilog2(umem->page_size);
857
Yishai Hadaseeb84612014-01-28 13:40:15 +0200858 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
859 pages = sg_dma_len(sg) >> shift;
860 for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) {
861 /* store the page address in pbe */
862 pbe->pa_lo =
863 cpu_to_le32(sg_dma_address
864 (sg) +
865 (umem->page_size * pg_cnt));
866 pbe->pa_hi =
867 cpu_to_le32(upper_32_bits
868 ((sg_dma_address
869 (sg) +
870 umem->page_size * pg_cnt)));
871 pbe_cnt += 1;
872 total_num_pbes += 1;
873 pbe++;
Parav Panditfe2caef2012-03-21 04:09:06 +0530874
Yishai Hadaseeb84612014-01-28 13:40:15 +0200875 /* if done building pbes, issue the mbx cmd. */
876 if (total_num_pbes == num_pbes)
877 return;
Parav Panditfe2caef2012-03-21 04:09:06 +0530878
Yishai Hadaseeb84612014-01-28 13:40:15 +0200879 /* if the given pbl is full storing the pbes,
880 * move to next pbl.
881 */
882 if (pbe_cnt ==
883 (mr->hwmr.pbl_size / sizeof(u64))) {
884 pbl_tbl++;
885 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
886 pbe_cnt = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530887 }
Yishai Hadaseeb84612014-01-28 13:40:15 +0200888
Parav Panditfe2caef2012-03-21 04:09:06 +0530889 }
890 }
891}
892
893struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
894 u64 usr_addr, int acc, struct ib_udata *udata)
895{
896 int status = -ENOMEM;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530897 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530898 struct ocrdma_mr *mr;
899 struct ocrdma_pd *pd;
Parav Panditfe2caef2012-03-21 04:09:06 +0530900 u32 num_pbes;
901
902 pd = get_ocrdma_pd(ibpd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530903
904 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
905 return ERR_PTR(-EINVAL);
906
907 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
908 if (!mr)
909 return ERR_PTR(status);
Parav Panditfe2caef2012-03-21 04:09:06 +0530910 mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
911 if (IS_ERR(mr->umem)) {
912 status = -EFAULT;
913 goto umem_err;
914 }
915 num_pbes = ib_umem_page_count(mr->umem);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530916 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
Parav Panditfe2caef2012-03-21 04:09:06 +0530917 if (status)
918 goto umem_err;
919
920 mr->hwmr.pbe_size = mr->umem->page_size;
Haggai Eran406f9e52014-12-11 17:04:12 +0200921 mr->hwmr.fbo = ib_umem_offset(mr->umem);
Parav Panditfe2caef2012-03-21 04:09:06 +0530922 mr->hwmr.va = usr_addr;
923 mr->hwmr.len = len;
924 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
925 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
926 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
927 mr->hwmr.local_rd = 1;
928 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
929 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
930 if (status)
931 goto umem_err;
932 build_user_pbes(dev, mr, num_pbes);
933 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
934 if (status)
935 goto mbx_err;
Parav Panditfe2caef2012-03-21 04:09:06 +0530936 mr->ibmr.lkey = mr->hwmr.lkey;
937 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
938 mr->ibmr.rkey = mr->hwmr.lkey;
939
940 return &mr->ibmr;
941
942mbx_err:
943 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
944umem_err:
945 kfree(mr);
946 return ERR_PTR(status);
947}
948
949int ocrdma_dereg_mr(struct ib_mr *ib_mr)
950{
951 struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530952 struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530953
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530954 (void) ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
Parav Panditfe2caef2012-03-21 04:09:06 +0530955
Selvin Xavier9d1878a2014-02-04 11:57:02 +0530956 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
Parav Panditfe2caef2012-03-21 04:09:06 +0530957
Parav Panditfe2caef2012-03-21 04:09:06 +0530958 /* it could be user registered memory. */
959 if (mr->umem)
960 ib_umem_release(mr->umem);
961 kfree(mr);
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530962
963 /* Don't stop cleanup, in case FW is unresponsive */
964 if (dev->mqe_ctx.fw_error_state) {
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530965 pr_err("%s(%d) fw not responding.\n",
966 __func__, dev->id);
967 }
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530968 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530969}
970
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530971static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
972 struct ib_udata *udata,
Parav Panditfe2caef2012-03-21 04:09:06 +0530973 struct ib_ucontext *ib_ctx)
974{
975 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530976 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
Parav Panditfe2caef2012-03-21 04:09:06 +0530977 struct ocrdma_create_cq_uresp uresp;
978
Dan Carpenter63ea3742013-07-29 22:34:29 +0300979 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530980 uresp.cq_id = cq->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530981 uresp.page_size = PAGE_ALIGN(cq->len);
Parav Panditfe2caef2012-03-21 04:09:06 +0530982 uresp.num_pages = 1;
983 uresp.max_hw_cqe = cq->max_hw_cqe;
Devesh Sharma1b76d382014-09-05 19:35:40 +0530984 uresp.page_addr[0] = virt_to_phys(cq->va);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530985 uresp.db_page_addr = ocrdma_get_db_addr(dev, uctx->cntxt_pd->id);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530986 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530987 uresp.phase_change = cq->phase_change ? 1 : 0;
988 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
989 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000990 pr_err("%s(%d) copy error cqid=0x%x.\n",
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530991 __func__, dev->id, cq->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530992 goto err;
993 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530994 status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
995 if (status)
996 goto err;
997 status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
998 if (status) {
999 ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
1000 goto err;
1001 }
1002 cq->ucontext = uctx;
1003err:
1004 return status;
1005}
1006
1007struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector,
1008 struct ib_ucontext *ib_ctx,
1009 struct ib_udata *udata)
1010{
1011 struct ocrdma_cq *cq;
1012 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301013 struct ocrdma_ucontext *uctx = NULL;
1014 u16 pd_id = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301015 int status;
1016 struct ocrdma_create_cq_ureq ureq;
1017
1018 if (udata) {
1019 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1020 return ERR_PTR(-EFAULT);
1021 } else
1022 ureq.dpp_cq = 0;
1023 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
1024 if (!cq)
1025 return ERR_PTR(-ENOMEM);
1026
1027 spin_lock_init(&cq->cq_lock);
1028 spin_lock_init(&cq->comp_handler_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301029 INIT_LIST_HEAD(&cq->sq_head);
1030 INIT_LIST_HEAD(&cq->rq_head);
Devesh Sharmaea617622014-02-04 11:56:54 +05301031 cq->first_arm = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05301032
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301033 if (ib_ctx) {
1034 uctx = get_ocrdma_ucontext(ib_ctx);
1035 pd_id = uctx->cntxt_pd->id;
1036 }
1037
1038 status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq, pd_id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301039 if (status) {
1040 kfree(cq);
1041 return ERR_PTR(status);
1042 }
1043 if (ib_ctx) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301044 status = ocrdma_copy_cq_uresp(dev, cq, udata, ib_ctx);
Parav Panditfe2caef2012-03-21 04:09:06 +05301045 if (status)
1046 goto ctx_err;
1047 }
1048 cq->phase = OCRDMA_CQE_VALID;
Parav Panditfe2caef2012-03-21 04:09:06 +05301049 dev->cq_tbl[cq->id] = cq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301050 return &cq->ibcq;
1051
1052ctx_err:
1053 ocrdma_mbx_destroy_cq(dev, cq);
1054 kfree(cq);
1055 return ERR_PTR(status);
1056}
1057
1058int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
1059 struct ib_udata *udata)
1060{
1061 int status = 0;
1062 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
1063
1064 if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
1065 status = -EINVAL;
1066 return status;
1067 }
1068 ibcq->cqe = new_cnt;
1069 return status;
1070}
1071
Devesh Sharmaea617622014-02-04 11:56:54 +05301072static void ocrdma_flush_cq(struct ocrdma_cq *cq)
1073{
1074 int cqe_cnt;
1075 int valid_count = 0;
1076 unsigned long flags;
1077
1078 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
1079 struct ocrdma_cqe *cqe = NULL;
1080
1081 cqe = cq->va;
1082 cqe_cnt = cq->cqe_cnt;
1083
1084 /* Last irq might have scheduled a polling thread
1085 * sync-up with it before hard flushing.
1086 */
1087 spin_lock_irqsave(&cq->cq_lock, flags);
1088 while (cqe_cnt) {
1089 if (is_cqe_valid(cq, cqe))
1090 valid_count++;
1091 cqe++;
1092 cqe_cnt--;
1093 }
1094 ocrdma_ring_cq_db(dev, cq->id, false, false, valid_count);
1095 spin_unlock_irqrestore(&cq->cq_lock, flags);
1096}
1097
Parav Panditfe2caef2012-03-21 04:09:06 +05301098int ocrdma_destroy_cq(struct ib_cq *ibcq)
1099{
Parav Panditfe2caef2012-03-21 04:09:06 +05301100 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
Devesh Sharmaea617622014-02-04 11:56:54 +05301101 struct ocrdma_eq *eq = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301102 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301103 int pdid = 0;
Devesh Sharmaea617622014-02-04 11:56:54 +05301104 u32 irq, indx;
1105
1106 dev->cq_tbl[cq->id] = NULL;
1107 indx = ocrdma_get_eq_table_index(dev, cq->eqn);
1108 if (indx == -EINVAL)
1109 BUG();
1110
1111 eq = &dev->eq_tbl[indx];
1112 irq = ocrdma_get_irq(dev, eq);
1113 synchronize_irq(irq);
1114 ocrdma_flush_cq(cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301115
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301116 (void)ocrdma_mbx_destroy_cq(dev, cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301117 if (cq->ucontext) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301118 pdid = cq->ucontext->cntxt_pd->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301119 ocrdma_del_mmap(cq->ucontext, (u64) cq->pa,
1120 PAGE_ALIGN(cq->len));
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301121 ocrdma_del_mmap(cq->ucontext,
1122 ocrdma_get_db_addr(dev, pdid),
Parav Panditfe2caef2012-03-21 04:09:06 +05301123 dev->nic_info.db_page_size);
1124 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301125
1126 kfree(cq);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301127 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301128}
1129
1130static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1131{
1132 int status = -EINVAL;
1133
1134 if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
1135 dev->qp_tbl[qp->id] = qp;
1136 status = 0;
1137 }
1138 return status;
1139}
1140
1141static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1142{
1143 dev->qp_tbl[qp->id] = NULL;
1144}
1145
1146static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
1147 struct ib_qp_init_attr *attrs)
1148{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301149 if ((attrs->qp_type != IB_QPT_GSI) &&
1150 (attrs->qp_type != IB_QPT_RC) &&
1151 (attrs->qp_type != IB_QPT_UC) &&
1152 (attrs->qp_type != IB_QPT_UD)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001153 pr_err("%s(%d) unsupported qp type=0x%x requested\n",
1154 __func__, dev->id, attrs->qp_type);
Parav Panditfe2caef2012-03-21 04:09:06 +05301155 return -EINVAL;
1156 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301157 /* Skip the check for QP1 to support CM size of 128 */
1158 if ((attrs->qp_type != IB_QPT_GSI) &&
1159 (attrs->cap.max_send_wr > dev->attr.max_wqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001160 pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
1161 __func__, dev->id, attrs->cap.max_send_wr);
1162 pr_err("%s(%d) supported send_wr=0x%x\n",
1163 __func__, dev->id, dev->attr.max_wqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301164 return -EINVAL;
1165 }
1166 if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001167 pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
1168 __func__, dev->id, attrs->cap.max_recv_wr);
1169 pr_err("%s(%d) supported recv_wr=0x%x\n",
1170 __func__, dev->id, dev->attr.max_rqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301171 return -EINVAL;
1172 }
1173 if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001174 pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
1175 __func__, dev->id, attrs->cap.max_inline_data);
1176 pr_err("%s(%d) supported inline data size=0x%x\n",
1177 __func__, dev->id, dev->attr.max_inline_data);
Parav Panditfe2caef2012-03-21 04:09:06 +05301178 return -EINVAL;
1179 }
1180 if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001181 pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
1182 __func__, dev->id, attrs->cap.max_send_sge);
1183 pr_err("%s(%d) supported send_sge=0x%x\n",
1184 __func__, dev->id, dev->attr.max_send_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301185 return -EINVAL;
1186 }
1187 if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001188 pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
1189 __func__, dev->id, attrs->cap.max_recv_sge);
1190 pr_err("%s(%d) supported recv_sge=0x%x\n",
1191 __func__, dev->id, dev->attr.max_recv_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301192 return -EINVAL;
1193 }
1194 /* unprivileged user space cannot create special QP */
1195 if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001196 pr_err
Parav Panditfe2caef2012-03-21 04:09:06 +05301197 ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
1198 __func__, dev->id, attrs->qp_type);
1199 return -EINVAL;
1200 }
1201 /* allow creating only one GSI type of QP */
1202 if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001203 pr_err("%s(%d) GSI special QPs already created.\n",
1204 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301205 return -EINVAL;
1206 }
1207 /* verify consumer QPs are not trying to use GSI QP's CQ */
1208 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
1209 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301210 (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001211 pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301212 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301213 return -EINVAL;
1214 }
1215 }
1216 return 0;
1217}
1218
1219static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
1220 struct ib_udata *udata, int dpp_offset,
1221 int dpp_credit_lmt, int srq)
1222{
1223 int status = 0;
1224 u64 usr_db;
1225 struct ocrdma_create_qp_uresp uresp;
Parav Panditfe2caef2012-03-21 04:09:06 +05301226 struct ocrdma_pd *pd = qp->pd;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301227 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301228
1229 memset(&uresp, 0, sizeof(uresp));
1230 usr_db = dev->nic_info.unmapped_db +
1231 (pd->id * dev->nic_info.db_page_size);
1232 uresp.qp_id = qp->id;
1233 uresp.sq_dbid = qp->sq.dbid;
1234 uresp.num_sq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301235 uresp.sq_page_size = PAGE_ALIGN(qp->sq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301236 uresp.sq_page_addr[0] = virt_to_phys(qp->sq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301237 uresp.num_wqe_allocated = qp->sq.max_cnt;
1238 if (!srq) {
1239 uresp.rq_dbid = qp->rq.dbid;
1240 uresp.num_rq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301241 uresp.rq_page_size = PAGE_ALIGN(qp->rq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301242 uresp.rq_page_addr[0] = virt_to_phys(qp->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301243 uresp.num_rqe_allocated = qp->rq.max_cnt;
1244 }
1245 uresp.db_page_addr = usr_db;
1246 uresp.db_page_size = dev->nic_info.db_page_size;
Devesh Sharma2df84fa82014-02-04 11:56:55 +05301247 uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
1248 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
1249 uresp.db_shift = OCRDMA_DB_RQ_SHIFT;
Parav Panditfe2caef2012-03-21 04:09:06 +05301250
1251 if (qp->dpp_enabled) {
1252 uresp.dpp_credit = dpp_credit_lmt;
1253 uresp.dpp_offset = dpp_offset;
1254 }
1255 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1256 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001257 pr_err("%s(%d) user copy error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301258 goto err;
1259 }
1260 status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
1261 uresp.sq_page_size);
1262 if (status)
1263 goto err;
1264
1265 if (!srq) {
1266 status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
1267 uresp.rq_page_size);
1268 if (status)
1269 goto rq_map_err;
1270 }
1271 return status;
1272rq_map_err:
1273 ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
1274err:
1275 return status;
1276}
1277
1278static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
1279 struct ocrdma_pd *pd)
1280{
Devesh Sharma21c33912014-02-04 11:56:56 +05301281 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301282 qp->sq_db = dev->nic_info.db +
1283 (pd->id * dev->nic_info.db_page_size) +
1284 OCRDMA_DB_GEN2_SQ_OFFSET;
1285 qp->rq_db = dev->nic_info.db +
1286 (pd->id * dev->nic_info.db_page_size) +
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301287 OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301288 } else {
1289 qp->sq_db = dev->nic_info.db +
1290 (pd->id * dev->nic_info.db_page_size) +
1291 OCRDMA_DB_SQ_OFFSET;
1292 qp->rq_db = dev->nic_info.db +
1293 (pd->id * dev->nic_info.db_page_size) +
1294 OCRDMA_DB_RQ_OFFSET;
1295 }
1296}
1297
1298static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
1299{
1300 qp->wqe_wr_id_tbl =
1301 kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
1302 GFP_KERNEL);
1303 if (qp->wqe_wr_id_tbl == NULL)
1304 return -ENOMEM;
1305 qp->rqe_wr_id_tbl =
1306 kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
1307 if (qp->rqe_wr_id_tbl == NULL)
1308 return -ENOMEM;
1309
1310 return 0;
1311}
1312
1313static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
1314 struct ocrdma_pd *pd,
1315 struct ib_qp_init_attr *attrs)
1316{
1317 qp->pd = pd;
1318 spin_lock_init(&qp->q_lock);
1319 INIT_LIST_HEAD(&qp->sq_entry);
1320 INIT_LIST_HEAD(&qp->rq_entry);
1321
1322 qp->qp_type = attrs->qp_type;
1323 qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
1324 qp->max_inline_data = attrs->cap.max_inline_data;
1325 qp->sq.max_sges = attrs->cap.max_send_sge;
1326 qp->rq.max_sges = attrs->cap.max_recv_sge;
1327 qp->state = OCRDMA_QPS_RST;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05301328 qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
Parav Panditfe2caef2012-03-21 04:09:06 +05301329}
1330
Parav Panditfe2caef2012-03-21 04:09:06 +05301331static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
1332 struct ib_qp_init_attr *attrs)
1333{
1334 if (attrs->qp_type == IB_QPT_GSI) {
1335 dev->gsi_qp_created = 1;
1336 dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
1337 dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
1338 }
1339}
1340
1341struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
1342 struct ib_qp_init_attr *attrs,
1343 struct ib_udata *udata)
1344{
1345 int status;
1346 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1347 struct ocrdma_qp *qp;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301348 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301349 struct ocrdma_create_qp_ureq ureq;
1350 u16 dpp_credit_lmt, dpp_offset;
1351
1352 status = ocrdma_check_qp_params(ibpd, dev, attrs);
1353 if (status)
1354 goto gen_err;
1355
1356 memset(&ureq, 0, sizeof(ureq));
1357 if (udata) {
1358 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1359 return ERR_PTR(-EFAULT);
1360 }
1361 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1362 if (!qp) {
1363 status = -ENOMEM;
1364 goto gen_err;
1365 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301366 ocrdma_set_qp_init_params(qp, pd, attrs);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301367 if (udata == NULL)
1368 qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
1369 OCRDMA_QP_FAST_REG);
Parav Panditfe2caef2012-03-21 04:09:06 +05301370
1371 mutex_lock(&dev->dev_lock);
1372 status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
1373 ureq.dpp_cq_id,
1374 &dpp_offset, &dpp_credit_lmt);
1375 if (status)
1376 goto mbx_err;
1377
1378 /* user space QP's wr_id table are managed in library */
1379 if (udata == NULL) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301380 status = ocrdma_alloc_wr_id_tbl(qp);
1381 if (status)
1382 goto map_err;
1383 }
1384
1385 status = ocrdma_add_qpn_map(dev, qp);
1386 if (status)
1387 goto map_err;
1388 ocrdma_set_qp_db(dev, qp, pd);
1389 if (udata) {
1390 status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
1391 dpp_credit_lmt,
1392 (attrs->srq != NULL));
1393 if (status)
1394 goto cpy_err;
1395 }
1396 ocrdma_store_gsi_qp_cq(dev, attrs);
Gottumukkala, Naresh27159f52013-06-05 08:50:46 +00001397 qp->ibqp.qp_num = qp->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301398 mutex_unlock(&dev->dev_lock);
1399 return &qp->ibqp;
1400
1401cpy_err:
1402 ocrdma_del_qpn_map(dev, qp);
1403map_err:
1404 ocrdma_mbx_destroy_qp(dev, qp);
1405mbx_err:
1406 mutex_unlock(&dev->dev_lock);
1407 kfree(qp->wqe_wr_id_tbl);
1408 kfree(qp->rqe_wr_id_tbl);
1409 kfree(qp);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001410 pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05301411gen_err:
1412 return ERR_PTR(status);
1413}
1414
1415int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1416 int attr_mask)
1417{
1418 int status = 0;
1419 struct ocrdma_qp *qp;
1420 struct ocrdma_dev *dev;
1421 enum ib_qp_state old_qps;
1422
1423 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301424 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301425 if (attr_mask & IB_QP_STATE)
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301426 status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301427 /* if new and previous states are same hw doesn't need to
1428 * know about it.
1429 */
1430 if (status < 0)
1431 return status;
Prarit Bhargavabc1b04a2014-02-19 15:05:16 -05001432 status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask);
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +05301433
Parav Panditfe2caef2012-03-21 04:09:06 +05301434 return status;
1435}
1436
1437int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1438 int attr_mask, struct ib_udata *udata)
1439{
1440 unsigned long flags;
1441 int status = -EINVAL;
1442 struct ocrdma_qp *qp;
1443 struct ocrdma_dev *dev;
1444 enum ib_qp_state old_qps, new_qps;
1445
1446 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301447 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301448
1449 /* syncronize with multiple context trying to change, retrive qps */
1450 mutex_lock(&dev->dev_lock);
1451 /* syncronize with wqe, rqe posting and cqe processing contexts */
1452 spin_lock_irqsave(&qp->q_lock, flags);
1453 old_qps = get_ibqp_state(qp->state);
1454 if (attr_mask & IB_QP_STATE)
1455 new_qps = attr->qp_state;
1456 else
1457 new_qps = old_qps;
1458 spin_unlock_irqrestore(&qp->q_lock, flags);
1459
Matan Barakdd5f03b2013-12-12 18:03:11 +02001460 if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask,
Moni Shoua37721d82013-12-12 18:03:16 +02001461 IB_LINK_LAYER_ETHERNET)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001462 pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
1463 "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
1464 __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
1465 old_qps, new_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301466 goto param_err;
1467 }
1468
1469 status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
1470 if (status > 0)
1471 status = 0;
1472param_err:
1473 mutex_unlock(&dev->dev_lock);
1474 return status;
1475}
1476
1477static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
1478{
1479 switch (mtu) {
1480 case 256:
1481 return IB_MTU_256;
1482 case 512:
1483 return IB_MTU_512;
1484 case 1024:
1485 return IB_MTU_1024;
1486 case 2048:
1487 return IB_MTU_2048;
1488 case 4096:
1489 return IB_MTU_4096;
1490 default:
1491 return IB_MTU_1024;
1492 }
1493}
1494
1495static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
1496{
1497 int ib_qp_acc_flags = 0;
1498
1499 if (qp_cap_flags & OCRDMA_QP_INB_WR)
1500 ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
1501 if (qp_cap_flags & OCRDMA_QP_INB_RD)
1502 ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
1503 return ib_qp_acc_flags;
1504}
1505
1506int ocrdma_query_qp(struct ib_qp *ibqp,
1507 struct ib_qp_attr *qp_attr,
1508 int attr_mask, struct ib_qp_init_attr *qp_init_attr)
1509{
1510 int status;
1511 u32 qp_state;
1512 struct ocrdma_qp_params params;
1513 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301514 struct ocrdma_dev *dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301515
1516 memset(&params, 0, sizeof(params));
1517 mutex_lock(&dev->dev_lock);
1518 status = ocrdma_mbx_query_qp(dev, qp, &params);
1519 mutex_unlock(&dev->dev_lock);
1520 if (status)
1521 goto mbx_err;
Mitesh Ahuja95bf0092014-12-03 11:36:33 +05301522 if (qp->qp_type == IB_QPT_UD)
1523 qp_attr->qkey = params.qkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05301524 qp_attr->path_mtu =
1525 ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
1526 OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
1527 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
1528 qp_attr->path_mig_state = IB_MIG_MIGRATED;
1529 qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
1530 qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
1531 qp_attr->dest_qp_num =
1532 params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
1533
1534 qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
1535 qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
1536 qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
1537 qp_attr->cap.max_send_sge = qp->sq.max_sges;
1538 qp_attr->cap.max_recv_sge = qp->rq.max_sges;
Naresh Gottumukkalac43e9ab2013-08-26 15:27:46 +05301539 qp_attr->cap.max_inline_data = qp->max_inline_data;
Parav Panditfe2caef2012-03-21 04:09:06 +05301540 qp_init_attr->cap = qp_attr->cap;
1541 memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
1542 sizeof(params.dgid));
1543 qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
1544 OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
1545 qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
1546 qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
1547 OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
1548 OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
1549 qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
Devesh Sharmaa61d93d2014-02-10 13:48:58 +05301550 OCRDMA_QP_PARAMS_TCLASS_MASK) >>
Parav Panditfe2caef2012-03-21 04:09:06 +05301551 OCRDMA_QP_PARAMS_TCLASS_SHIFT;
1552
1553 qp_attr->ah_attr.ah_flags = IB_AH_GRH;
1554 qp_attr->ah_attr.port_num = 1;
1555 qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
1556 OCRDMA_QP_PARAMS_SL_MASK) >>
1557 OCRDMA_QP_PARAMS_SL_SHIFT;
1558 qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
1559 OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
1560 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
1561 qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
1562 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
1563 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
1564 qp_attr->retry_cnt =
1565 (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
1566 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
1567 qp_attr->min_rnr_timer = 0;
1568 qp_attr->pkey_index = 0;
1569 qp_attr->port_num = 1;
1570 qp_attr->ah_attr.src_path_bits = 0;
1571 qp_attr->ah_attr.static_rate = 0;
1572 qp_attr->alt_pkey_index = 0;
1573 qp_attr->alt_port_num = 0;
1574 qp_attr->alt_timeout = 0;
1575 memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
1576 qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
1577 OCRDMA_QP_PARAMS_STATE_SHIFT;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301578 qp_attr->qp_state = get_ibqp_state(qp_state);
1579 qp_attr->cur_qp_state = qp_attr->qp_state;
Parav Panditfe2caef2012-03-21 04:09:06 +05301580 qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
1581 qp_attr->max_dest_rd_atomic =
1582 params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
1583 qp_attr->max_rd_atomic =
1584 params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
1585 qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
1586 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301587 /* Sync driver QP state with FW */
1588 ocrdma_qp_state_change(qp, qp_attr->qp_state, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05301589mbx_err:
1590 return status;
1591}
1592
Rasmus Villemoesf3070e72015-01-16 15:39:56 +01001593static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, unsigned int idx)
Parav Panditfe2caef2012-03-21 04:09:06 +05301594{
Rasmus Villemoesf3070e72015-01-16 15:39:56 +01001595 unsigned int i = idx / 32;
1596 u32 mask = (1U << (idx % 32));
Parav Panditfe2caef2012-03-21 04:09:06 +05301597
Rasmus Villemoesba64fdc2015-01-16 15:39:55 +01001598 srq->idx_bit_fields[i] ^= mask;
Parav Panditfe2caef2012-03-21 04:09:06 +05301599}
1600
1601static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
1602{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301603 return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
Parav Panditfe2caef2012-03-21 04:09:06 +05301604}
1605
1606static int is_hw_sq_empty(struct ocrdma_qp *qp)
1607{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301608 return (qp->sq.tail == qp->sq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301609}
1610
1611static int is_hw_rq_empty(struct ocrdma_qp *qp)
1612{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301613 return (qp->rq.tail == qp->rq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301614}
1615
1616static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
1617{
1618 return q->va + (q->head * q->entry_size);
1619}
1620
1621static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
1622 u32 idx)
1623{
1624 return q->va + (idx * q->entry_size);
1625}
1626
1627static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
1628{
1629 q->head = (q->head + 1) & q->max_wqe_idx;
1630}
1631
1632static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
1633{
1634 q->tail = (q->tail + 1) & q->max_wqe_idx;
1635}
1636
1637/* discard the cqe for a given QP */
1638static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
1639{
1640 unsigned long cq_flags;
1641 unsigned long flags;
1642 int discard_cnt = 0;
1643 u32 cur_getp, stop_getp;
1644 struct ocrdma_cqe *cqe;
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301645 u32 qpn = 0, wqe_idx = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301646
1647 spin_lock_irqsave(&cq->cq_lock, cq_flags);
1648
1649 /* traverse through the CQEs in the hw CQ,
1650 * find the matching CQE for a given qp,
1651 * mark the matching one discarded by clearing qpn.
1652 * ring the doorbell in the poll_cq() as
1653 * we don't complete out of order cqe.
1654 */
1655
1656 cur_getp = cq->getp;
1657 /* find upto when do we reap the cq. */
1658 stop_getp = cur_getp;
1659 do {
1660 if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
1661 break;
1662
1663 cqe = cq->va + cur_getp;
1664 /* if (a) done reaping whole hw cq, or
1665 * (b) qp_xq becomes empty.
1666 * then exit
1667 */
1668 qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
1669 /* if previously discarded cqe found, skip that too. */
1670 /* check for matching qp */
1671 if (qpn == 0 || qpn != qp->id)
1672 goto skip_cqe;
1673
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301674 if (is_cqe_for_sq(cqe)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301675 ocrdma_hwq_inc_tail(&qp->sq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301676 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301677 if (qp->srq) {
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301678 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
1679 OCRDMA_CQE_BUFTAG_SHIFT) &
1680 qp->srq->rq.max_wqe_idx;
1681 if (wqe_idx < 1)
1682 BUG();
Parav Panditfe2caef2012-03-21 04:09:06 +05301683 spin_lock_irqsave(&qp->srq->q_lock, flags);
1684 ocrdma_hwq_inc_tail(&qp->srq->rq);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301685 ocrdma_srq_toggle_bit(qp->srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05301686 spin_unlock_irqrestore(&qp->srq->q_lock, flags);
1687
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301688 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301689 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301690 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301691 }
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301692 /* mark cqe discarded so that it is not picked up later
1693 * in the poll_cq().
1694 */
1695 discard_cnt += 1;
1696 cqe->cmn.qpn = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301697skip_cqe:
1698 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
1699 } while (cur_getp != stop_getp);
1700 spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
1701}
1702
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301703void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
Parav Panditfe2caef2012-03-21 04:09:06 +05301704{
1705 int found = false;
1706 unsigned long flags;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301707 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301708 /* sync with any active CQ poll */
1709
1710 spin_lock_irqsave(&dev->flush_q_lock, flags);
1711 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1712 if (found)
1713 list_del(&qp->sq_entry);
1714 if (!qp->srq) {
1715 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1716 if (found)
1717 list_del(&qp->rq_entry);
1718 }
1719 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
1720}
1721
1722int ocrdma_destroy_qp(struct ib_qp *ibqp)
1723{
Parav Panditfe2caef2012-03-21 04:09:06 +05301724 struct ocrdma_pd *pd;
1725 struct ocrdma_qp *qp;
1726 struct ocrdma_dev *dev;
1727 struct ib_qp_attr attrs;
1728 int attr_mask = IB_QP_STATE;
Dan Carpenterd19081e2012-05-02 09:14:47 +03001729 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05301730
1731 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301732 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301733
1734 attrs.qp_state = IB_QPS_ERR;
1735 pd = qp->pd;
1736
1737 /* change the QP state to ERROR */
1738 _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
1739
1740 /* ensure that CQEs for newly created QP (whose id may be same with
1741 * one which just getting destroyed are same), dont get
1742 * discarded until the old CQEs are discarded.
1743 */
1744 mutex_lock(&dev->dev_lock);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301745 (void) ocrdma_mbx_destroy_qp(dev, qp);
Parav Panditfe2caef2012-03-21 04:09:06 +05301746
1747 /*
1748 * acquire CQ lock while destroy is in progress, in order to
1749 * protect against proessing in-flight CQEs for this QP.
1750 */
Dan Carpenterd19081e2012-05-02 09:14:47 +03001751 spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301752 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001753 spin_lock(&qp->rq_cq->cq_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301754
1755 ocrdma_del_qpn_map(dev, qp);
1756
1757 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001758 spin_unlock(&qp->rq_cq->cq_lock);
1759 spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301760
1761 if (!pd->uctx) {
1762 ocrdma_discard_cqes(qp, qp->sq_cq);
1763 ocrdma_discard_cqes(qp, qp->rq_cq);
1764 }
1765 mutex_unlock(&dev->dev_lock);
1766
1767 if (pd->uctx) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301768 ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
1769 PAGE_ALIGN(qp->sq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301770 if (!qp->srq)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301771 ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
1772 PAGE_ALIGN(qp->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301773 }
1774
1775 ocrdma_del_flush_qp(qp);
1776
Parav Panditfe2caef2012-03-21 04:09:06 +05301777 kfree(qp->wqe_wr_id_tbl);
1778 kfree(qp->rqe_wr_id_tbl);
1779 kfree(qp);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301780 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301781}
1782
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301783static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
1784 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301785{
1786 int status;
1787 struct ocrdma_create_srq_uresp uresp;
1788
Dan Carpenter63ea3742013-07-29 22:34:29 +03001789 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +05301790 uresp.rq_dbid = srq->rq.dbid;
1791 uresp.num_rq_pages = 1;
Devesh Sharma1b76d382014-09-05 19:35:40 +05301792 uresp.rq_page_addr[0] = virt_to_phys(srq->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301793 uresp.rq_page_size = srq->rq.len;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301794 uresp.db_page_addr = dev->nic_info.unmapped_db +
1795 (srq->pd->id * dev->nic_info.db_page_size);
1796 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +05301797 uresp.num_rqe_allocated = srq->rq.max_cnt;
Devesh Sharma21c33912014-02-04 11:56:56 +05301798 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301799 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301800 uresp.db_shift = 24;
1801 } else {
1802 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
1803 uresp.db_shift = 16;
1804 }
1805
1806 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1807 if (status)
1808 return status;
1809 status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
1810 uresp.rq_page_size);
1811 if (status)
1812 return status;
1813 return status;
1814}
1815
1816struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
1817 struct ib_srq_init_attr *init_attr,
1818 struct ib_udata *udata)
1819{
1820 int status = -ENOMEM;
1821 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301822 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301823 struct ocrdma_srq *srq;
1824
1825 if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
1826 return ERR_PTR(-EINVAL);
1827 if (init_attr->attr.max_wr > dev->attr.max_rqe)
1828 return ERR_PTR(-EINVAL);
1829
1830 srq = kzalloc(sizeof(*srq), GFP_KERNEL);
1831 if (!srq)
1832 return ERR_PTR(status);
1833
1834 spin_lock_init(&srq->q_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301835 srq->pd = pd;
1836 srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301837 status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +05301838 if (status)
1839 goto err;
1840
1841 if (udata == NULL) {
1842 srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
1843 GFP_KERNEL);
1844 if (srq->rqe_wr_id_tbl == NULL)
1845 goto arm_err;
1846
1847 srq->bit_fields_len = (srq->rq.max_cnt / 32) +
1848 (srq->rq.max_cnt % 32 ? 1 : 0);
1849 srq->idx_bit_fields =
1850 kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
1851 if (srq->idx_bit_fields == NULL)
1852 goto arm_err;
1853 memset(srq->idx_bit_fields, 0xff,
1854 srq->bit_fields_len * sizeof(u32));
1855 }
1856
1857 if (init_attr->attr.srq_limit) {
1858 status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
1859 if (status)
1860 goto arm_err;
1861 }
1862
Parav Panditfe2caef2012-03-21 04:09:06 +05301863 if (udata) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301864 status = ocrdma_copy_srq_uresp(dev, srq, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +05301865 if (status)
1866 goto arm_err;
1867 }
1868
Parav Panditfe2caef2012-03-21 04:09:06 +05301869 return &srq->ibsrq;
1870
1871arm_err:
1872 ocrdma_mbx_destroy_srq(dev, srq);
1873err:
1874 kfree(srq->rqe_wr_id_tbl);
1875 kfree(srq->idx_bit_fields);
1876 kfree(srq);
1877 return ERR_PTR(status);
1878}
1879
1880int ocrdma_modify_srq(struct ib_srq *ibsrq,
1881 struct ib_srq_attr *srq_attr,
1882 enum ib_srq_attr_mask srq_attr_mask,
1883 struct ib_udata *udata)
1884{
1885 int status = 0;
1886 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301887
1888 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301889 if (srq_attr_mask & IB_SRQ_MAX_WR)
1890 status = -EINVAL;
1891 else
1892 status = ocrdma_mbx_modify_srq(srq, srq_attr);
1893 return status;
1894}
1895
1896int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
1897{
1898 int status;
1899 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301900
1901 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301902 status = ocrdma_mbx_query_srq(srq, srq_attr);
1903 return status;
1904}
1905
1906int ocrdma_destroy_srq(struct ib_srq *ibsrq)
1907{
1908 int status;
1909 struct ocrdma_srq *srq;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301910 struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301911
1912 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301913
1914 status = ocrdma_mbx_destroy_srq(dev, srq);
1915
1916 if (srq->pd->uctx)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301917 ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
1918 PAGE_ALIGN(srq->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301919
Parav Panditfe2caef2012-03-21 04:09:06 +05301920 kfree(srq->idx_bit_fields);
1921 kfree(srq->rqe_wr_id_tbl);
1922 kfree(srq);
1923 return status;
1924}
1925
1926/* unprivileged verbs and their support functions. */
1927static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
1928 struct ocrdma_hdr_wqe *hdr,
1929 struct ib_send_wr *wr)
1930{
1931 struct ocrdma_ewqe_ud_hdr *ud_hdr =
1932 (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
1933 struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
1934
1935 ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
1936 if (qp->qp_type == IB_QPT_GSI)
1937 ud_hdr->qkey = qp->qkey;
1938 else
1939 ud_hdr->qkey = wr->wr.ud.remote_qkey;
1940 ud_hdr->rsvd_ahid = ah->id;
Devesh Sharma29565f22014-12-18 14:13:07 +05301941 if (ah->av->valid & OCRDMA_AV_VLAN_VALID)
1942 hdr->cw |= (OCRDMA_FLAG_AH_VLAN_PR << OCRDMA_WQE_FLAGS_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05301943}
1944
1945static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
1946 struct ocrdma_sge *sge, int num_sge,
1947 struct ib_sge *sg_list)
1948{
1949 int i;
1950
1951 for (i = 0; i < num_sge; i++) {
1952 sge[i].lrkey = sg_list[i].lkey;
1953 sge[i].addr_lo = sg_list[i].addr;
1954 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
1955 sge[i].len = sg_list[i].length;
1956 hdr->total_len += sg_list[i].length;
1957 }
1958 if (num_sge == 0)
1959 memset(sge, 0, sizeof(*sge));
1960}
1961
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301962static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
1963{
1964 uint32_t total_len = 0, i;
1965
1966 for (i = 0; i < num_sge; i++)
1967 total_len += sg_list[i].length;
1968 return total_len;
1969}
1970
1971
Parav Panditfe2caef2012-03-21 04:09:06 +05301972static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
1973 struct ocrdma_hdr_wqe *hdr,
1974 struct ocrdma_sge *sge,
1975 struct ib_send_wr *wr, u32 wqe_size)
1976{
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301977 int i;
1978 char *dpp_addr;
1979
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301980 if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301981 hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
1982 if (unlikely(hdr->total_len > qp->max_inline_data)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001983 pr_err("%s() supported_len=0x%x,\n"
Masanari Iida1a84db52014-08-29 23:37:33 +09001984 " unsupported len req=0x%x\n", __func__,
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301985 qp->max_inline_data, hdr->total_len);
Parav Panditfe2caef2012-03-21 04:09:06 +05301986 return -EINVAL;
1987 }
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301988 dpp_addr = (char *)sge;
1989 for (i = 0; i < wr->num_sge; i++) {
1990 memcpy(dpp_addr,
1991 (void *)(unsigned long)wr->sg_list[i].addr,
1992 wr->sg_list[i].length);
1993 dpp_addr += wr->sg_list[i].length;
1994 }
1995
Parav Panditfe2caef2012-03-21 04:09:06 +05301996 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301997 if (0 == hdr->total_len)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301998 wqe_size += sizeof(struct ocrdma_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301999 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
2000 } else {
2001 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2002 if (wr->num_sge)
2003 wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
2004 else
2005 wqe_size += sizeof(struct ocrdma_sge);
2006 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2007 }
2008 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2009 return 0;
2010}
2011
2012static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2013 struct ib_send_wr *wr)
2014{
2015 int status;
2016 struct ocrdma_sge *sge;
2017 u32 wqe_size = sizeof(*hdr);
2018
2019 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2020 ocrdma_build_ud_hdr(qp, hdr, wr);
2021 sge = (struct ocrdma_sge *)(hdr + 2);
2022 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302023 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302024 sge = (struct ocrdma_sge *)(hdr + 1);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302025 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302026
2027 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2028 return status;
2029}
2030
2031static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2032 struct ib_send_wr *wr)
2033{
2034 int status;
2035 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2036 struct ocrdma_sge *sge = ext_rw + 1;
2037 u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
2038
2039 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2040 if (status)
2041 return status;
2042 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
2043 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
2044 ext_rw->lrkey = wr->wr.rdma.rkey;
2045 ext_rw->len = hdr->total_len;
2046 return 0;
2047}
2048
2049static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2050 struct ib_send_wr *wr)
2051{
2052 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2053 struct ocrdma_sge *sge = ext_rw + 1;
2054 u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
2055 sizeof(struct ocrdma_hdr_wqe);
2056
2057 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2058 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2059 hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
2060 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2061
2062 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
2063 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
2064 ext_rw->lrkey = wr->wr.rdma.rkey;
2065 ext_rw->len = hdr->total_len;
2066}
2067
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302068static void build_frmr_pbes(struct ib_send_wr *wr, struct ocrdma_pbl *pbl_tbl,
2069 struct ocrdma_hw_mr *hwmr)
2070{
2071 int i;
2072 u64 buf_addr = 0;
2073 int num_pbes;
2074 struct ocrdma_pbe *pbe;
2075
2076 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2077 num_pbes = 0;
2078
2079 /* go through the OS phy regions & fill hw pbe entries into pbls. */
2080 for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
2081 /* number of pbes can be more for one OS buf, when
2082 * buffers are of different sizes.
2083 * split the ib_buf to one or more pbes.
2084 */
2085 buf_addr = wr->wr.fast_reg.page_list->page_list[i];
2086 pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
2087 pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
2088 num_pbes += 1;
2089 pbe++;
2090
2091 /* if the pbl is full storing the pbes,
2092 * move to next pbl.
2093 */
2094 if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
2095 pbl_tbl++;
2096 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2097 }
2098 }
2099 return;
2100}
2101
2102static int get_encoded_page_size(int pg_sz)
2103{
2104 /* Max size is 256M 4096 << 16 */
2105 int i = 0;
2106 for (; i < 17; i++)
2107 if (pg_sz == (4096 << i))
2108 break;
2109 return i;
2110}
2111
2112
2113static int ocrdma_build_fr(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2114 struct ib_send_wr *wr)
2115{
2116 u64 fbo;
2117 struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
2118 struct ocrdma_mr *mr;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302119 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302120 u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
2121
2122 wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
2123
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302124 if (wr->wr.fast_reg.page_list_len > dev->attr.max_pages_per_frmr)
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302125 return -EINVAL;
2126
2127 hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
2128 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2129
2130 if (wr->wr.fast_reg.page_list_len == 0)
2131 BUG();
2132 if (wr->wr.fast_reg.access_flags & IB_ACCESS_LOCAL_WRITE)
2133 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
2134 if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_WRITE)
2135 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
2136 if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_READ)
2137 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
2138 hdr->lkey = wr->wr.fast_reg.rkey;
2139 hdr->total_len = wr->wr.fast_reg.length;
2140
2141 fbo = wr->wr.fast_reg.iova_start -
2142 (wr->wr.fast_reg.page_list->page_list[0] & PAGE_MASK);
2143
2144 fast_reg->va_hi = upper_32_bits(wr->wr.fast_reg.iova_start);
2145 fast_reg->va_lo = (u32) (wr->wr.fast_reg.iova_start & 0xffffffff);
2146 fast_reg->fbo_hi = upper_32_bits(fbo);
2147 fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
2148 fast_reg->num_sges = wr->wr.fast_reg.page_list_len;
2149 fast_reg->size_sge =
2150 get_encoded_page_size(1 << wr->wr.fast_reg.page_shift);
Roland Dreier7a1e89d2014-03-17 23:14:17 -07002151 mr = (struct ocrdma_mr *) (unsigned long)
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302152 dev->stag_arr[(hdr->lkey >> 8) & (OCRDMA_MAX_STAG - 1)];
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302153 build_frmr_pbes(wr, mr->hwmr.pbl_table, &mr->hwmr);
2154 return 0;
2155}
2156
Parav Panditfe2caef2012-03-21 04:09:06 +05302157static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
2158{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302159 u32 val = qp->sq.dbid | (1 << OCRDMA_DB_SQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302160
2161 iowrite32(val, qp->sq_db);
2162}
2163
2164int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
2165 struct ib_send_wr **bad_wr)
2166{
2167 int status = 0;
2168 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2169 struct ocrdma_hdr_wqe *hdr;
2170 unsigned long flags;
2171
2172 spin_lock_irqsave(&qp->q_lock, flags);
2173 if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
2174 spin_unlock_irqrestore(&qp->q_lock, flags);
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002175 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302176 return -EINVAL;
2177 }
2178
2179 while (wr) {
Mitesh Ahujaf252b5d2014-06-10 19:32:20 +05302180 if (qp->qp_type == IB_QPT_UD &&
2181 (wr->opcode != IB_WR_SEND &&
2182 wr->opcode != IB_WR_SEND_WITH_IMM)) {
2183 *bad_wr = wr;
2184 status = -EINVAL;
2185 break;
2186 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302187 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
2188 wr->num_sge > qp->sq.max_sges) {
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002189 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302190 status = -ENOMEM;
2191 break;
2192 }
2193 hdr = ocrdma_hwq_head(&qp->sq);
2194 hdr->cw = 0;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302195 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302196 hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2197 if (wr->send_flags & IB_SEND_FENCE)
2198 hdr->cw |=
2199 (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
2200 if (wr->send_flags & IB_SEND_SOLICITED)
2201 hdr->cw |=
2202 (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
2203 hdr->total_len = 0;
2204 switch (wr->opcode) {
2205 case IB_WR_SEND_WITH_IMM:
2206 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2207 hdr->immdt = ntohl(wr->ex.imm_data);
2208 case IB_WR_SEND:
2209 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2210 ocrdma_build_send(qp, hdr, wr);
2211 break;
2212 case IB_WR_SEND_WITH_INV:
2213 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
2214 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2215 hdr->lkey = wr->ex.invalidate_rkey;
2216 status = ocrdma_build_send(qp, hdr, wr);
2217 break;
2218 case IB_WR_RDMA_WRITE_WITH_IMM:
2219 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2220 hdr->immdt = ntohl(wr->ex.imm_data);
2221 case IB_WR_RDMA_WRITE:
2222 hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
2223 status = ocrdma_build_write(qp, hdr, wr);
2224 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302225 case IB_WR_RDMA_READ:
2226 ocrdma_build_read(qp, hdr, wr);
2227 break;
2228 case IB_WR_LOCAL_INV:
2229 hdr->cw |=
2230 (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302231 hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
2232 sizeof(struct ocrdma_sge)) /
Parav Panditfe2caef2012-03-21 04:09:06 +05302233 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
2234 hdr->lkey = wr->ex.invalidate_rkey;
2235 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302236 case IB_WR_FAST_REG_MR:
2237 status = ocrdma_build_fr(qp, hdr, wr);
2238 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302239 default:
2240 status = -EINVAL;
2241 break;
2242 }
2243 if (status) {
2244 *bad_wr = wr;
2245 break;
2246 }
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302247 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302248 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
2249 else
2250 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
2251 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
2252 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
2253 OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
2254 /* make sure wqe is written before adapter can access it */
2255 wmb();
2256 /* inform hw to start processing it */
2257 ocrdma_ring_sq_db(qp);
2258
2259 /* update pointer, counter for next wr */
2260 ocrdma_hwq_inc_head(&qp->sq);
2261 wr = wr->next;
2262 }
2263 spin_unlock_irqrestore(&qp->q_lock, flags);
2264 return status;
2265}
2266
2267static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
2268{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302269 u32 val = qp->rq.dbid | (1 << OCRDMA_DB_RQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302270
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302271 iowrite32(val, qp->rq_db);
Parav Panditfe2caef2012-03-21 04:09:06 +05302272}
2273
2274static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
2275 u16 tag)
2276{
2277 u32 wqe_size = 0;
2278 struct ocrdma_sge *sge;
2279 if (wr->num_sge)
2280 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
2281 else
2282 wqe_size = sizeof(*sge) + sizeof(*rqe);
2283
2284 rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
2285 OCRDMA_WQE_SIZE_SHIFT);
2286 rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2287 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2288 rqe->total_len = 0;
2289 rqe->rsvd_tag = tag;
2290 sge = (struct ocrdma_sge *)(rqe + 1);
2291 ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
2292 ocrdma_cpu_to_le32(rqe, wqe_size);
2293}
2294
2295int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2296 struct ib_recv_wr **bad_wr)
2297{
2298 int status = 0;
2299 unsigned long flags;
2300 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2301 struct ocrdma_hdr_wqe *rqe;
2302
2303 spin_lock_irqsave(&qp->q_lock, flags);
2304 if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
2305 spin_unlock_irqrestore(&qp->q_lock, flags);
2306 *bad_wr = wr;
2307 return -EINVAL;
2308 }
2309 while (wr) {
2310 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
2311 wr->num_sge > qp->rq.max_sges) {
2312 *bad_wr = wr;
2313 status = -ENOMEM;
2314 break;
2315 }
2316 rqe = ocrdma_hwq_head(&qp->rq);
2317 ocrdma_build_rqe(rqe, wr, 0);
2318
2319 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
2320 /* make sure rqe is written before adapter can access it */
2321 wmb();
2322
2323 /* inform hw to start processing it */
2324 ocrdma_ring_rq_db(qp);
2325
2326 /* update pointer, counter for next wr */
2327 ocrdma_hwq_inc_head(&qp->rq);
2328 wr = wr->next;
2329 }
2330 spin_unlock_irqrestore(&qp->q_lock, flags);
2331 return status;
2332}
2333
2334/* cqe for srq's rqe can potentially arrive out of order.
2335 * index gives the entry in the shadow table where to store
2336 * the wr_id. tag/index is returned in cqe to reference back
2337 * for a given rqe.
2338 */
2339static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
2340{
2341 int row = 0;
2342 int indx = 0;
2343
2344 for (row = 0; row < srq->bit_fields_len; row++) {
2345 if (srq->idx_bit_fields[row]) {
2346 indx = ffs(srq->idx_bit_fields[row]);
2347 indx = (row * 32) + (indx - 1);
2348 if (indx >= srq->rq.max_cnt)
2349 BUG();
2350 ocrdma_srq_toggle_bit(srq, indx);
2351 break;
2352 }
2353 }
2354
2355 if (row == srq->bit_fields_len)
2356 BUG();
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302357 return indx + 1; /* Use from index 1 */
Parav Panditfe2caef2012-03-21 04:09:06 +05302358}
2359
2360static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
2361{
2362 u32 val = srq->rq.dbid | (1 << 16);
2363
2364 iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
2365}
2366
2367int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
2368 struct ib_recv_wr **bad_wr)
2369{
2370 int status = 0;
2371 unsigned long flags;
2372 struct ocrdma_srq *srq;
2373 struct ocrdma_hdr_wqe *rqe;
2374 u16 tag;
2375
2376 srq = get_ocrdma_srq(ibsrq);
2377
2378 spin_lock_irqsave(&srq->q_lock, flags);
2379 while (wr) {
2380 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
2381 wr->num_sge > srq->rq.max_sges) {
2382 status = -ENOMEM;
2383 *bad_wr = wr;
2384 break;
2385 }
2386 tag = ocrdma_srq_get_idx(srq);
2387 rqe = ocrdma_hwq_head(&srq->rq);
2388 ocrdma_build_rqe(rqe, wr, tag);
2389
2390 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
2391 /* make sure rqe is written before adapter can perform DMA */
2392 wmb();
2393 /* inform hw to start processing it */
2394 ocrdma_ring_srq_db(srq);
2395 /* update pointer, counter for next wr */
2396 ocrdma_hwq_inc_head(&srq->rq);
2397 wr = wr->next;
2398 }
2399 spin_unlock_irqrestore(&srq->q_lock, flags);
2400 return status;
2401}
2402
2403static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
2404{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302405 enum ib_wc_status ibwc_status;
Parav Panditfe2caef2012-03-21 04:09:06 +05302406
2407 switch (status) {
2408 case OCRDMA_CQE_GENERAL_ERR:
2409 ibwc_status = IB_WC_GENERAL_ERR;
2410 break;
2411 case OCRDMA_CQE_LOC_LEN_ERR:
2412 ibwc_status = IB_WC_LOC_LEN_ERR;
2413 break;
2414 case OCRDMA_CQE_LOC_QP_OP_ERR:
2415 ibwc_status = IB_WC_LOC_QP_OP_ERR;
2416 break;
2417 case OCRDMA_CQE_LOC_EEC_OP_ERR:
2418 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
2419 break;
2420 case OCRDMA_CQE_LOC_PROT_ERR:
2421 ibwc_status = IB_WC_LOC_PROT_ERR;
2422 break;
2423 case OCRDMA_CQE_WR_FLUSH_ERR:
2424 ibwc_status = IB_WC_WR_FLUSH_ERR;
2425 break;
2426 case OCRDMA_CQE_MW_BIND_ERR:
2427 ibwc_status = IB_WC_MW_BIND_ERR;
2428 break;
2429 case OCRDMA_CQE_BAD_RESP_ERR:
2430 ibwc_status = IB_WC_BAD_RESP_ERR;
2431 break;
2432 case OCRDMA_CQE_LOC_ACCESS_ERR:
2433 ibwc_status = IB_WC_LOC_ACCESS_ERR;
2434 break;
2435 case OCRDMA_CQE_REM_INV_REQ_ERR:
2436 ibwc_status = IB_WC_REM_INV_REQ_ERR;
2437 break;
2438 case OCRDMA_CQE_REM_ACCESS_ERR:
2439 ibwc_status = IB_WC_REM_ACCESS_ERR;
2440 break;
2441 case OCRDMA_CQE_REM_OP_ERR:
2442 ibwc_status = IB_WC_REM_OP_ERR;
2443 break;
2444 case OCRDMA_CQE_RETRY_EXC_ERR:
2445 ibwc_status = IB_WC_RETRY_EXC_ERR;
2446 break;
2447 case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2448 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2449 break;
2450 case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2451 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2452 break;
2453 case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2454 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2455 break;
2456 case OCRDMA_CQE_REM_ABORT_ERR:
2457 ibwc_status = IB_WC_REM_ABORT_ERR;
2458 break;
2459 case OCRDMA_CQE_INV_EECN_ERR:
2460 ibwc_status = IB_WC_INV_EECN_ERR;
2461 break;
2462 case OCRDMA_CQE_INV_EEC_STATE_ERR:
2463 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2464 break;
2465 case OCRDMA_CQE_FATAL_ERR:
2466 ibwc_status = IB_WC_FATAL_ERR;
2467 break;
2468 case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2469 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2470 break;
2471 default:
2472 ibwc_status = IB_WC_GENERAL_ERR;
2473 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002474 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302475 return ibwc_status;
2476}
2477
2478static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2479 u32 wqe_idx)
2480{
2481 struct ocrdma_hdr_wqe *hdr;
2482 struct ocrdma_sge *rw;
2483 int opcode;
2484
2485 hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2486
2487 ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2488 /* Undo the hdr->cw swap */
2489 opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2490 switch (opcode) {
2491 case OCRDMA_WRITE:
2492 ibwc->opcode = IB_WC_RDMA_WRITE;
2493 break;
2494 case OCRDMA_READ:
2495 rw = (struct ocrdma_sge *)(hdr + 1);
2496 ibwc->opcode = IB_WC_RDMA_READ;
2497 ibwc->byte_len = rw->len;
2498 break;
2499 case OCRDMA_SEND:
2500 ibwc->opcode = IB_WC_SEND;
2501 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302502 case OCRDMA_FR_MR:
2503 ibwc->opcode = IB_WC_FAST_REG_MR;
2504 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302505 case OCRDMA_LKEY_INV:
2506 ibwc->opcode = IB_WC_LOCAL_INV;
2507 break;
2508 default:
2509 ibwc->status = IB_WC_GENERAL_ERR;
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002510 pr_err("%s() invalid opcode received = 0x%x\n",
2511 __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
Parav Panditfe2caef2012-03-21 04:09:06 +05302512 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002513 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302514}
2515
2516static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2517 struct ocrdma_cqe *cqe)
2518{
2519 if (is_cqe_for_sq(cqe)) {
2520 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2521 cqe->flags_status_srcqpn) &
2522 ~OCRDMA_CQE_STATUS_MASK);
2523 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2524 cqe->flags_status_srcqpn) |
2525 (OCRDMA_CQE_WR_FLUSH_ERR <<
2526 OCRDMA_CQE_STATUS_SHIFT));
2527 } else {
2528 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2529 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2530 cqe->flags_status_srcqpn) &
2531 ~OCRDMA_CQE_UD_STATUS_MASK);
2532 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2533 cqe->flags_status_srcqpn) |
2534 (OCRDMA_CQE_WR_FLUSH_ERR <<
2535 OCRDMA_CQE_UD_STATUS_SHIFT));
2536 } else {
2537 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2538 cqe->flags_status_srcqpn) &
2539 ~OCRDMA_CQE_STATUS_MASK);
2540 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2541 cqe->flags_status_srcqpn) |
2542 (OCRDMA_CQE_WR_FLUSH_ERR <<
2543 OCRDMA_CQE_STATUS_SHIFT));
2544 }
2545 }
2546}
2547
2548static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2549 struct ocrdma_qp *qp, int status)
2550{
2551 bool expand = false;
2552
2553 ibwc->byte_len = 0;
2554 ibwc->qp = &qp->ibqp;
2555 ibwc->status = ocrdma_to_ibwc_err(status);
2556
2557 ocrdma_flush_qp(qp);
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05302558 ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05302559
2560 /* if wqe/rqe pending for which cqe needs to be returned,
2561 * trigger inflating it.
2562 */
2563 if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2564 expand = true;
2565 ocrdma_set_cqe_status_flushed(qp, cqe);
2566 }
2567 return expand;
2568}
2569
2570static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2571 struct ocrdma_qp *qp, int status)
2572{
2573 ibwc->opcode = IB_WC_RECV;
2574 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2575 ocrdma_hwq_inc_tail(&qp->rq);
2576
2577 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2578}
2579
2580static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2581 struct ocrdma_qp *qp, int status)
2582{
2583 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2584 ocrdma_hwq_inc_tail(&qp->sq);
2585
2586 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2587}
2588
2589
2590static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2591 struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2592 bool *polled, bool *stop)
2593{
2594 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302595 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302596 int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2597 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302598 if (status < OCRDMA_MAX_CQE_ERR)
2599 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302600
2601 /* when hw sq is empty, but rq is not empty, so we continue
2602 * to keep the cqe in order to get the cq event again.
2603 */
2604 if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2605 /* when cq for rq and sq is same, it is safe to return
2606 * flush cqe for RQEs.
2607 */
2608 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2609 *polled = true;
2610 status = OCRDMA_CQE_WR_FLUSH_ERR;
2611 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2612 } else {
2613 /* stop processing further cqe as this cqe is used for
2614 * triggering cq event on buddy cq of RQ.
2615 * When QP is destroyed, this cqe will be removed
2616 * from the cq's hardware q.
2617 */
2618 *polled = false;
2619 *stop = true;
2620 expand = false;
2621 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302622 } else if (is_hw_sq_empty(qp)) {
2623 /* Do nothing */
2624 expand = false;
2625 *polled = false;
2626 *stop = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302627 } else {
2628 *polled = true;
2629 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2630 }
2631 return expand;
2632}
2633
2634static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2635 struct ocrdma_cqe *cqe,
2636 struct ib_wc *ibwc, bool *polled)
2637{
2638 bool expand = false;
2639 int tail = qp->sq.tail;
2640 u32 wqe_idx;
2641
2642 if (!qp->wqe_wr_id_tbl[tail].signaled) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302643 *polled = false; /* WC cannot be consumed yet */
2644 } else {
2645 ibwc->status = IB_WC_SUCCESS;
2646 ibwc->wc_flags = 0;
2647 ibwc->qp = &qp->ibqp;
2648 ocrdma_update_wc(qp, ibwc, tail);
2649 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302650 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302651 wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
2652 OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
Parav Panditae3bca92012-08-17 14:45:33 +00002653 if (tail != wqe_idx)
2654 expand = true; /* Coalesced CQE can't be consumed yet */
2655
Parav Panditfe2caef2012-03-21 04:09:06 +05302656 ocrdma_hwq_inc_tail(&qp->sq);
2657 return expand;
2658}
2659
2660static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2661 struct ib_wc *ibwc, bool *polled, bool *stop)
2662{
2663 int status;
2664 bool expand;
2665
2666 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2667 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2668
2669 if (status == OCRDMA_CQE_SUCCESS)
2670 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2671 else
2672 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2673 return expand;
2674}
2675
2676static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
2677{
2678 int status;
2679
2680 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2681 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2682 ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2683 OCRDMA_CQE_SRCQP_MASK;
2684 ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
2685 OCRDMA_CQE_PKEY_MASK;
2686 ibwc->wc_flags = IB_WC_GRH;
2687 ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2688 OCRDMA_CQE_UD_XFER_LEN_SHIFT);
2689 return status;
2690}
2691
2692static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2693 struct ocrdma_cqe *cqe,
2694 struct ocrdma_qp *qp)
2695{
2696 unsigned long flags;
2697 struct ocrdma_srq *srq;
2698 u32 wqe_idx;
2699
2700 srq = get_ocrdma_srq(qp->ibqp.srq);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302701 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302702 OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
2703 if (wqe_idx < 1)
2704 BUG();
2705
Parav Panditfe2caef2012-03-21 04:09:06 +05302706 ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2707 spin_lock_irqsave(&srq->q_lock, flags);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302708 ocrdma_srq_toggle_bit(srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05302709 spin_unlock_irqrestore(&srq->q_lock, flags);
2710 ocrdma_hwq_inc_tail(&srq->rq);
2711}
2712
2713static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2714 struct ib_wc *ibwc, bool *polled, bool *stop,
2715 int status)
2716{
2717 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302718 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2719
2720 if (status < OCRDMA_MAX_CQE_ERR)
2721 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302722
2723 /* when hw_rq is empty, but wq is not empty, so continue
2724 * to keep the cqe to get the cq event again.
2725 */
2726 if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2727 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2728 *polled = true;
2729 status = OCRDMA_CQE_WR_FLUSH_ERR;
2730 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2731 } else {
2732 *polled = false;
2733 *stop = true;
2734 expand = false;
2735 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302736 } else if (is_hw_rq_empty(qp)) {
2737 /* Do nothing */
2738 expand = false;
2739 *polled = false;
2740 *stop = false;
Parav Pandita3698a92012-06-11 16:39:20 +05302741 } else {
2742 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302743 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
Parav Pandita3698a92012-06-11 16:39:20 +05302744 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302745 return expand;
2746}
2747
2748static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2749 struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2750{
2751 ibwc->opcode = IB_WC_RECV;
2752 ibwc->qp = &qp->ibqp;
2753 ibwc->status = IB_WC_SUCCESS;
2754
2755 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
2756 ocrdma_update_ud_rcqe(ibwc, cqe);
2757 else
2758 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2759
2760 if (is_cqe_imm(cqe)) {
2761 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2762 ibwc->wc_flags |= IB_WC_WITH_IMM;
2763 } else if (is_cqe_wr_imm(cqe)) {
2764 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2765 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2766 ibwc->wc_flags |= IB_WC_WITH_IMM;
2767 } else if (is_cqe_invalidated(cqe)) {
2768 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2769 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2770 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302771 if (qp->ibqp.srq) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302772 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302773 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302774 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2775 ocrdma_hwq_inc_tail(&qp->rq);
2776 }
2777}
2778
2779static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2780 struct ib_wc *ibwc, bool *polled, bool *stop)
2781{
2782 int status;
2783 bool expand = false;
2784
2785 ibwc->wc_flags = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302786 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302787 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2788 OCRDMA_CQE_UD_STATUS_MASK) >>
2789 OCRDMA_CQE_UD_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302790 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302791 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2792 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302793 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302794
2795 if (status == OCRDMA_CQE_SUCCESS) {
2796 *polled = true;
2797 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2798 } else {
2799 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2800 status);
2801 }
2802 return expand;
2803}
2804
2805static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2806 u16 cur_getp)
2807{
2808 if (cq->phase_change) {
2809 if (cur_getp == 0)
2810 cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302811 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302812 /* clear valid bit */
2813 cqe->flags_status_srcqpn = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302814 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302815}
2816
2817static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2818 struct ib_wc *ibwc)
2819{
2820 u16 qpn = 0;
2821 int i = 0;
2822 bool expand = false;
2823 int polled_hw_cqes = 0;
2824 struct ocrdma_qp *qp = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302825 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302826 struct ocrdma_cqe *cqe;
2827 u16 cur_getp; bool polled = false; bool stop = false;
2828
2829 cur_getp = cq->getp;
2830 while (num_entries) {
2831 cqe = cq->va + cur_getp;
2832 /* check whether valid cqe or not */
2833 if (!is_cqe_valid(cq, cqe))
2834 break;
2835 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2836 /* ignore discarded cqe */
2837 if (qpn == 0)
2838 goto skip_cqe;
2839 qp = dev->qp_tbl[qpn];
2840 BUG_ON(qp == NULL);
2841
2842 if (is_cqe_for_sq(cqe)) {
2843 expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2844 &stop);
2845 } else {
2846 expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2847 &stop);
2848 }
2849 if (expand)
2850 goto expand_cqe;
2851 if (stop)
2852 goto stop_cqe;
2853 /* clear qpn to avoid duplicate processing by discard_cqe() */
2854 cqe->cmn.qpn = 0;
2855skip_cqe:
2856 polled_hw_cqes += 1;
2857 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2858 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2859expand_cqe:
2860 if (polled) {
2861 num_entries -= 1;
2862 i += 1;
2863 ibwc = ibwc + 1;
2864 polled = false;
2865 }
2866 }
2867stop_cqe:
2868 cq->getp = cur_getp;
Devesh Sharmaea617622014-02-04 11:56:54 +05302869 if (cq->deferred_arm) {
2870 ocrdma_ring_cq_db(dev, cq->id, true, cq->deferred_sol,
Parav Panditfe2caef2012-03-21 04:09:06 +05302871 polled_hw_cqes);
Devesh Sharmaea617622014-02-04 11:56:54 +05302872 cq->deferred_arm = false;
2873 cq->deferred_sol = false;
2874 } else {
2875 /* We need to pop the CQE. No need to arm */
2876 ocrdma_ring_cq_db(dev, cq->id, false, cq->deferred_sol,
2877 polled_hw_cqes);
2878 cq->deferred_sol = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302879 }
Devesh Sharmaea617622014-02-04 11:56:54 +05302880
Parav Panditfe2caef2012-03-21 04:09:06 +05302881 return i;
2882}
2883
2884/* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2885static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2886 struct ocrdma_qp *qp, struct ib_wc *ibwc)
2887{
2888 int err_cqes = 0;
2889
2890 while (num_entries) {
2891 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2892 break;
2893 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2894 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2895 ocrdma_hwq_inc_tail(&qp->sq);
2896 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2897 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2898 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302899 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302900 return err_cqes;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302901 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302902 ibwc->byte_len = 0;
2903 ibwc->status = IB_WC_WR_FLUSH_ERR;
2904 ibwc = ibwc + 1;
2905 err_cqes += 1;
2906 num_entries -= 1;
2907 }
2908 return err_cqes;
2909}
2910
2911int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2912{
2913 int cqes_to_poll = num_entries;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302914 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2915 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302916 int num_os_cqe = 0, err_cqes = 0;
2917 struct ocrdma_qp *qp;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302918 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05302919
2920 /* poll cqes from adapter CQ */
2921 spin_lock_irqsave(&cq->cq_lock, flags);
2922 num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2923 spin_unlock_irqrestore(&cq->cq_lock, flags);
2924 cqes_to_poll -= num_os_cqe;
2925
2926 if (cqes_to_poll) {
2927 wc = wc + num_os_cqe;
2928 /* adapter returns single error cqe when qp moves to
2929 * error state. So insert error cqes with wc_status as
2930 * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2931 * respectively which uses this CQ.
2932 */
2933 spin_lock_irqsave(&dev->flush_q_lock, flags);
2934 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2935 if (cqes_to_poll == 0)
2936 break;
2937 err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2938 cqes_to_poll -= err_cqes;
2939 num_os_cqe += err_cqes;
2940 wc = wc + err_cqes;
2941 }
2942 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2943 }
2944 return num_os_cqe;
2945}
2946
2947int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2948{
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302949 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2950 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302951 u16 cq_id;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302952 unsigned long flags;
Devesh Sharmaea617622014-02-04 11:56:54 +05302953 bool arm_needed = false, sol_needed = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302954
Parav Panditfe2caef2012-03-21 04:09:06 +05302955 cq_id = cq->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05302956
2957 spin_lock_irqsave(&cq->cq_lock, flags);
2958 if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302959 arm_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302960 if (cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302961 sol_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302962
Devesh Sharmaea617622014-02-04 11:56:54 +05302963 if (cq->first_arm) {
2964 ocrdma_ring_cq_db(dev, cq_id, arm_needed, sol_needed, 0);
2965 cq->first_arm = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302966 }
Devesh Sharmaea617622014-02-04 11:56:54 +05302967
Devesh Sharmaf93439e2014-06-09 10:52:38 +05302968 cq->deferred_arm = true;
Devesh Sharmaea617622014-02-04 11:56:54 +05302969 cq->deferred_sol = sol_needed;
Parav Panditfe2caef2012-03-21 04:09:06 +05302970 spin_unlock_irqrestore(&cq->cq_lock, flags);
Devesh Sharmaea617622014-02-04 11:56:54 +05302971
Parav Panditfe2caef2012-03-21 04:09:06 +05302972 return 0;
2973}
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302974
2975struct ib_mr *ocrdma_alloc_frmr(struct ib_pd *ibpd, int max_page_list_len)
2976{
2977 int status;
2978 struct ocrdma_mr *mr;
2979 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
2980 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
2981
2982 if (max_page_list_len > dev->attr.max_pages_per_frmr)
2983 return ERR_PTR(-EINVAL);
2984
2985 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2986 if (!mr)
2987 return ERR_PTR(-ENOMEM);
2988
2989 status = ocrdma_get_pbl_info(dev, mr, max_page_list_len);
2990 if (status)
2991 goto pbl_err;
2992 mr->hwmr.fr_mr = 1;
2993 mr->hwmr.remote_rd = 0;
2994 mr->hwmr.remote_wr = 0;
2995 mr->hwmr.local_rd = 0;
2996 mr->hwmr.local_wr = 0;
2997 mr->hwmr.mw_bind = 0;
2998 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
2999 if (status)
3000 goto pbl_err;
3001 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
3002 if (status)
3003 goto mbx_err;
3004 mr->ibmr.rkey = mr->hwmr.lkey;
3005 mr->ibmr.lkey = mr->hwmr.lkey;
Roland Dreier7a1e89d2014-03-17 23:14:17 -07003006 dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] =
3007 (unsigned long) mr;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303008 return &mr->ibmr;
3009mbx_err:
3010 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3011pbl_err:
3012 kfree(mr);
3013 return ERR_PTR(-ENOMEM);
3014}
3015
3016struct ib_fast_reg_page_list *ocrdma_alloc_frmr_page_list(struct ib_device
3017 *ibdev,
3018 int page_list_len)
3019{
3020 struct ib_fast_reg_page_list *frmr_list;
3021 int size;
3022
3023 size = sizeof(*frmr_list) + (page_list_len * sizeof(u64));
3024 frmr_list = kzalloc(size, GFP_KERNEL);
3025 if (!frmr_list)
3026 return ERR_PTR(-ENOMEM);
3027 frmr_list->page_list = (u64 *)(frmr_list + 1);
3028 return frmr_list;
3029}
3030
3031void ocrdma_free_frmr_page_list(struct ib_fast_reg_page_list *page_list)
3032{
3033 kfree(page_list);
3034}
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05303035
3036#define MAX_KERNEL_PBE_SIZE 65536
3037static inline int count_kernel_pbes(struct ib_phys_buf *buf_list,
3038 int buf_cnt, u32 *pbe_size)
3039{
3040 u64 total_size = 0;
3041 u64 buf_size = 0;
3042 int i;
3043 *pbe_size = roundup(buf_list[0].size, PAGE_SIZE);
3044 *pbe_size = roundup_pow_of_two(*pbe_size);
3045
3046 /* find the smallest PBE size that we can have */
3047 for (i = 0; i < buf_cnt; i++) {
3048 /* first addr may not be page aligned, so ignore checking */
3049 if ((i != 0) && ((buf_list[i].addr & ~PAGE_MASK) ||
3050 (buf_list[i].size & ~PAGE_MASK))) {
3051 return 0;
3052 }
3053
3054 /* if configured PBE size is greater then the chosen one,
3055 * reduce the PBE size.
3056 */
3057 buf_size = roundup(buf_list[i].size, PAGE_SIZE);
3058 /* pbe_size has to be even multiple of 4K 1,2,4,8...*/
3059 buf_size = roundup_pow_of_two(buf_size);
3060 if (*pbe_size > buf_size)
3061 *pbe_size = buf_size;
3062
3063 total_size += buf_size;
3064 }
3065 *pbe_size = *pbe_size > MAX_KERNEL_PBE_SIZE ?
3066 (MAX_KERNEL_PBE_SIZE) : (*pbe_size);
3067
3068 /* num_pbes = total_size / (*pbe_size); this is implemented below. */
3069
3070 return total_size >> ilog2(*pbe_size);
3071}
3072
3073static void build_kernel_pbes(struct ib_phys_buf *buf_list, int ib_buf_cnt,
3074 u32 pbe_size, struct ocrdma_pbl *pbl_tbl,
3075 struct ocrdma_hw_mr *hwmr)
3076{
3077 int i;
3078 int idx;
3079 int pbes_per_buf = 0;
3080 u64 buf_addr = 0;
3081 int num_pbes;
3082 struct ocrdma_pbe *pbe;
3083 int total_num_pbes = 0;
3084
3085 if (!hwmr->num_pbes)
3086 return;
3087
3088 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
3089 num_pbes = 0;
3090
3091 /* go through the OS phy regions & fill hw pbe entries into pbls. */
3092 for (i = 0; i < ib_buf_cnt; i++) {
3093 buf_addr = buf_list[i].addr;
3094 pbes_per_buf =
3095 roundup_pow_of_two(roundup(buf_list[i].size, PAGE_SIZE)) /
3096 pbe_size;
3097 hwmr->len += buf_list[i].size;
3098 /* number of pbes can be more for one OS buf, when
3099 * buffers are of different sizes.
3100 * split the ib_buf to one or more pbes.
3101 */
3102 for (idx = 0; idx < pbes_per_buf; idx++) {
3103 /* we program always page aligned addresses,
3104 * first unaligned address is taken care by fbo.
3105 */
3106 if (i == 0) {
3107 /* for non zero fbo, assign the
3108 * start of the page.
3109 */
3110 pbe->pa_lo =
3111 cpu_to_le32((u32) (buf_addr & PAGE_MASK));
3112 pbe->pa_hi =
3113 cpu_to_le32((u32) upper_32_bits(buf_addr));
3114 } else {
3115 pbe->pa_lo =
3116 cpu_to_le32((u32) (buf_addr & 0xffffffff));
3117 pbe->pa_hi =
3118 cpu_to_le32((u32) upper_32_bits(buf_addr));
3119 }
3120 buf_addr += pbe_size;
3121 num_pbes += 1;
3122 total_num_pbes += 1;
3123 pbe++;
3124
3125 if (total_num_pbes == hwmr->num_pbes)
3126 goto mr_tbl_done;
3127 /* if the pbl is full storing the pbes,
3128 * move to next pbl.
3129 */
3130 if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
3131 pbl_tbl++;
3132 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
3133 num_pbes = 0;
3134 }
3135 }
3136 }
3137mr_tbl_done:
3138 return;
3139}
3140
3141struct ib_mr *ocrdma_reg_kernel_mr(struct ib_pd *ibpd,
3142 struct ib_phys_buf *buf_list,
3143 int buf_cnt, int acc, u64 *iova_start)
3144{
3145 int status = -ENOMEM;
3146 struct ocrdma_mr *mr;
3147 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
3148 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
3149 u32 num_pbes;
3150 u32 pbe_size = 0;
3151
3152 if ((acc & IB_ACCESS_REMOTE_WRITE) && !(acc & IB_ACCESS_LOCAL_WRITE))
3153 return ERR_PTR(-EINVAL);
3154
3155 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3156 if (!mr)
3157 return ERR_PTR(status);
3158
3159 num_pbes = count_kernel_pbes(buf_list, buf_cnt, &pbe_size);
3160 if (num_pbes == 0) {
3161 status = -EINVAL;
3162 goto pbl_err;
3163 }
3164 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
3165 if (status)
3166 goto pbl_err;
3167
3168 mr->hwmr.pbe_size = pbe_size;
3169 mr->hwmr.fbo = *iova_start - (buf_list[0].addr & PAGE_MASK);
3170 mr->hwmr.va = *iova_start;
3171 mr->hwmr.local_rd = 1;
3172 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
3173 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
3174 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
3175 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
3176 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
3177
3178 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
3179 if (status)
3180 goto pbl_err;
3181 build_kernel_pbes(buf_list, buf_cnt, pbe_size, mr->hwmr.pbl_table,
3182 &mr->hwmr);
3183 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
3184 if (status)
3185 goto mbx_err;
3186
3187 mr->ibmr.lkey = mr->hwmr.lkey;
3188 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
3189 mr->ibmr.rkey = mr->hwmr.lkey;
3190 return &mr->ibmr;
3191
3192mbx_err:
3193 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3194pbl_err:
3195 kfree(mr);
3196 return ERR_PTR(status);
3197}