blob: 5f656b936de4fc497aa76e555940793b06422bfc [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));
Devesh Sharmafad51b72014-02-04 11:57:10 +053056 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);
378 return (status == 0) ? pd : ERR_PTR(status);
379 }
380
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530381retry:
382 status = ocrdma_mbx_alloc_pd(dev, pd);
383 if (status) {
384 if (pd->dpp_enabled) {
385 pd->dpp_enabled = false;
386 pd->num_dpp_qp = 0;
387 goto retry;
388 } else {
389 kfree(pd);
390 return ERR_PTR(status);
391 }
392 }
393
394 return pd;
395}
396
397static inline int is_ucontext_pd(struct ocrdma_ucontext *uctx,
398 struct ocrdma_pd *pd)
399{
400 return (uctx->cntxt_pd == pd ? true : false);
401}
402
403static int _ocrdma_dealloc_pd(struct ocrdma_dev *dev,
404 struct ocrdma_pd *pd)
405{
406 int status = 0;
407
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530408 if (dev->pd_mgr->pd_prealloc_valid)
409 status = ocrdma_put_pd_num(dev, pd->id, pd->dpp_enabled);
410 else
411 status = ocrdma_mbx_dealloc_pd(dev, pd);
412
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530413 kfree(pd);
414 return status;
415}
416
417static int ocrdma_alloc_ucontext_pd(struct ocrdma_dev *dev,
418 struct ocrdma_ucontext *uctx,
419 struct ib_udata *udata)
420{
421 int status = 0;
422
423 uctx->cntxt_pd = _ocrdma_alloc_pd(dev, uctx, udata);
424 if (IS_ERR(uctx->cntxt_pd)) {
425 status = PTR_ERR(uctx->cntxt_pd);
426 uctx->cntxt_pd = NULL;
427 goto err;
428 }
429
430 uctx->cntxt_pd->uctx = uctx;
431 uctx->cntxt_pd->ibpd.device = &dev->ibdev;
432err:
433 return status;
434}
435
436static int ocrdma_dealloc_ucontext_pd(struct ocrdma_ucontext *uctx)
437{
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530438 struct ocrdma_pd *pd = uctx->cntxt_pd;
439 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
440
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530441 if (uctx->pd_in_use) {
442 pr_err("%s(%d) Freeing in use pdid=0x%x.\n",
443 __func__, dev->id, pd->id);
444 }
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530445 uctx->cntxt_pd = NULL;
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530446 (void)_ocrdma_dealloc_pd(dev, pd);
447 return 0;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530448}
449
450static struct ocrdma_pd *ocrdma_get_ucontext_pd(struct ocrdma_ucontext *uctx)
451{
452 struct ocrdma_pd *pd = NULL;
453
454 mutex_lock(&uctx->mm_list_lock);
455 if (!uctx->pd_in_use) {
456 uctx->pd_in_use = true;
457 pd = uctx->cntxt_pd;
458 }
459 mutex_unlock(&uctx->mm_list_lock);
460
461 return pd;
462}
463
464static void ocrdma_release_ucontext_pd(struct ocrdma_ucontext *uctx)
465{
466 mutex_lock(&uctx->mm_list_lock);
467 uctx->pd_in_use = false;
468 mutex_unlock(&uctx->mm_list_lock);
469}
470
Parav Panditfe2caef2012-03-21 04:09:06 +0530471struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
472 struct ib_udata *udata)
473{
474 int status;
475 struct ocrdma_ucontext *ctx;
476 struct ocrdma_alloc_ucontext_resp resp;
477 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
478 struct pci_dev *pdev = dev->nic_info.pdev;
479 u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
480
481 if (!udata)
482 return ERR_PTR(-EFAULT);
483 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
484 if (!ctx)
485 return ERR_PTR(-ENOMEM);
Parav Panditfe2caef2012-03-21 04:09:06 +0530486 INIT_LIST_HEAD(&ctx->mm_head);
487 mutex_init(&ctx->mm_list_lock);
488
489 ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
490 &ctx->ah_tbl.pa, GFP_KERNEL);
491 if (!ctx->ah_tbl.va) {
492 kfree(ctx);
493 return ERR_PTR(-ENOMEM);
494 }
495 memset(ctx->ah_tbl.va, 0, map_len);
496 ctx->ah_tbl.len = map_len;
497
Dan Carpenter63ea3742013-07-29 22:34:29 +0300498 memset(&resp, 0, sizeof(resp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530499 resp.ah_tbl_len = ctx->ah_tbl.len;
Devesh Sharma1b76d382014-09-05 19:35:40 +0530500 resp.ah_tbl_page = virt_to_phys(ctx->ah_tbl.va);
Parav Panditfe2caef2012-03-21 04:09:06 +0530501
502 status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
503 if (status)
504 goto map_err;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530505
506 status = ocrdma_alloc_ucontext_pd(dev, ctx, udata);
507 if (status)
508 goto pd_err;
509
Parav Panditfe2caef2012-03-21 04:09:06 +0530510 resp.dev_id = dev->id;
511 resp.max_inline_data = dev->attr.max_inline_data;
512 resp.wqe_size = dev->attr.wqe_size;
513 resp.rqe_size = dev->attr.rqe_size;
514 resp.dpp_wqe_size = dev->attr.wqe_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530515
516 memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
517 status = ib_copy_to_udata(udata, &resp, sizeof(resp));
518 if (status)
519 goto cpy_err;
520 return &ctx->ibucontext;
521
522cpy_err:
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530523pd_err:
Parav Panditfe2caef2012-03-21 04:09:06 +0530524 ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
525map_err:
526 dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
527 ctx->ah_tbl.pa);
528 kfree(ctx);
529 return ERR_PTR(status);
530}
531
532int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
533{
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530534 int status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530535 struct ocrdma_mm *mm, *tmp;
536 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530537 struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
538 struct pci_dev *pdev = dev->nic_info.pdev;
Parav Panditfe2caef2012-03-21 04:09:06 +0530539
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530540 status = ocrdma_dealloc_ucontext_pd(uctx);
541
Parav Panditfe2caef2012-03-21 04:09:06 +0530542 ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
543 dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
544 uctx->ah_tbl.pa);
545
546 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
547 list_del(&mm->entry);
548 kfree(mm);
549 }
550 kfree(uctx);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530551 return status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530552}
553
554int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
555{
556 struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530557 struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530558 unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
559 u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
560 unsigned long len = (vma->vm_end - vma->vm_start);
561 int status = 0;
562 bool found;
563
564 if (vma->vm_start & (PAGE_SIZE - 1))
565 return -EINVAL;
566 found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
567 if (!found)
568 return -EINVAL;
569
570 if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
571 dev->nic_info.db_total_size)) &&
572 (len <= dev->nic_info.db_page_size)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530573 if (vma->vm_flags & VM_READ)
574 return -EPERM;
575
576 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
Parav Panditfe2caef2012-03-21 04:09:06 +0530577 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
578 len, vma->vm_page_prot);
579 } else if (dev->nic_info.dpp_unmapped_len &&
580 (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
581 (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
582 dev->nic_info.dpp_unmapped_len)) &&
583 (len <= dev->nic_info.dpp_unmapped_len)) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530584 if (vma->vm_flags & VM_READ)
585 return -EPERM;
586
Parav Panditfe2caef2012-03-21 04:09:06 +0530587 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
588 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
589 len, vma->vm_page_prot);
590 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +0530591 status = remap_pfn_range(vma, vma->vm_start,
592 vma->vm_pgoff, len, vma->vm_page_prot);
593 }
594 return status;
595}
596
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530597static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
Parav Panditfe2caef2012-03-21 04:09:06 +0530598 struct ib_ucontext *ib_ctx,
599 struct ib_udata *udata)
600{
601 int status;
602 u64 db_page_addr;
Roland Dreierda496432012-04-16 11:32:17 -0700603 u64 dpp_page_addr = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530604 u32 db_page_size;
605 struct ocrdma_alloc_pd_uresp rsp;
606 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
607
Dan Carpenter63ea3742013-07-29 22:34:29 +0300608 memset(&rsp, 0, sizeof(rsp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530609 rsp.id = pd->id;
610 rsp.dpp_enabled = pd->dpp_enabled;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530611 db_page_addr = ocrdma_get_db_addr(dev, pd->id);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530612 db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530613
614 status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
615 if (status)
616 return status;
617
618 if (pd->dpp_enabled) {
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530619 dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530620 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530621 status = ocrdma_add_mmap(uctx, dpp_page_addr,
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530622 PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530623 if (status)
624 goto dpp_map_err;
625 rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
626 rsp.dpp_page_addr_lo = dpp_page_addr;
627 }
628
629 status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
630 if (status)
631 goto ucopy_err;
632
633 pd->uctx = uctx;
634 return 0;
635
636ucopy_err:
Roland Dreierda496432012-04-16 11:32:17 -0700637 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530638 ocrdma_del_mmap(pd->uctx, dpp_page_addr, PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530639dpp_map_err:
640 ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
641 return status;
642}
643
644struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev,
645 struct ib_ucontext *context,
646 struct ib_udata *udata)
647{
648 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
649 struct ocrdma_pd *pd;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530650 struct ocrdma_ucontext *uctx = NULL;
Parav Panditfe2caef2012-03-21 04:09:06 +0530651 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530652 u8 is_uctx_pd = false;
Parav Panditfe2caef2012-03-21 04:09:06 +0530653
Parav Panditfe2caef2012-03-21 04:09:06 +0530654 if (udata && context) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530655 uctx = get_ocrdma_ucontext(context);
656 pd = ocrdma_get_ucontext_pd(uctx);
657 if (pd) {
658 is_uctx_pd = true;
659 goto pd_mapping;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530660 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530661 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530662
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530663 pd = _ocrdma_alloc_pd(dev, uctx, udata);
664 if (IS_ERR(pd)) {
665 status = PTR_ERR(pd);
666 goto exit;
667 }
668
669pd_mapping:
Parav Panditfe2caef2012-03-21 04:09:06 +0530670 if (udata && context) {
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530671 status = ocrdma_copy_pd_uresp(dev, pd, context, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +0530672 if (status)
673 goto err;
674 }
675 return &pd->ibpd;
676
677err:
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530678 if (is_uctx_pd) {
679 ocrdma_release_ucontext_pd(uctx);
680 } else {
Mitesh Ahuja9ba13772014-12-18 14:12:57 +0530681 status = _ocrdma_dealloc_pd(dev, pd);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530682 kfree(pd);
683 }
684exit:
Parav Panditfe2caef2012-03-21 04:09:06 +0530685 return ERR_PTR(status);
686}
687
688int ocrdma_dealloc_pd(struct ib_pd *ibpd)
689{
690 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530691 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530692 struct ocrdma_ucontext *uctx = NULL;
693 int status = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530694 u64 usr_db;
695
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530696 uctx = pd->uctx;
697 if (uctx) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530698 u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530699 (pd->id * PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530700 if (pd->dpp_enabled)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530701 ocrdma_del_mmap(pd->uctx, dpp_db, PAGE_SIZE);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530702 usr_db = ocrdma_get_db_addr(dev, pd->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530703 ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530704
705 if (is_ucontext_pd(uctx, pd)) {
706 ocrdma_release_ucontext_pd(uctx);
707 return status;
708 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530709 }
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530710 status = _ocrdma_dealloc_pd(dev, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530711 return status;
712}
713
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530714static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
715 u32 pdid, int acc, u32 num_pbls, u32 addr_check)
Parav Panditfe2caef2012-03-21 04:09:06 +0530716{
717 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530718
Parav Panditfe2caef2012-03-21 04:09:06 +0530719 mr->hwmr.fr_mr = 0;
720 mr->hwmr.local_rd = 1;
721 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
722 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
723 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
724 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
725 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
726 mr->hwmr.num_pbls = num_pbls;
727
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530728 status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
729 if (status)
730 return status;
731
Parav Panditfe2caef2012-03-21 04:09:06 +0530732 mr->ibmr.lkey = mr->hwmr.lkey;
733 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
734 mr->ibmr.rkey = mr->hwmr.lkey;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530735 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530736}
737
738struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
739{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530740 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530741 struct ocrdma_mr *mr;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530742 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
743 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530744
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530745 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
746 pr_err("%s err, invalid access rights\n", __func__);
747 return ERR_PTR(-EINVAL);
748 }
749
750 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
751 if (!mr)
752 return ERR_PTR(-ENOMEM);
753
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530754 status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530755 OCRDMA_ADDR_CHECK_DISABLE);
756 if (status) {
757 kfree(mr);
758 return ERR_PTR(status);
759 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530760
761 return &mr->ibmr;
762}
763
764static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
765 struct ocrdma_hw_mr *mr)
766{
767 struct pci_dev *pdev = dev->nic_info.pdev;
768 int i = 0;
769
770 if (mr->pbl_table) {
771 for (i = 0; i < mr->num_pbls; i++) {
772 if (!mr->pbl_table[i].va)
773 continue;
774 dma_free_coherent(&pdev->dev, mr->pbl_size,
775 mr->pbl_table[i].va,
776 mr->pbl_table[i].pa);
777 }
778 kfree(mr->pbl_table);
779 mr->pbl_table = NULL;
780 }
781}
782
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530783static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
784 u32 num_pbes)
Parav Panditfe2caef2012-03-21 04:09:06 +0530785{
786 u32 num_pbls = 0;
787 u32 idx = 0;
788 int status = 0;
789 u32 pbl_size;
790
791 do {
792 pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
793 if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
794 status = -EFAULT;
795 break;
796 }
797 num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
798 num_pbls = num_pbls / (pbl_size / sizeof(u64));
799 idx++;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530800 } while (num_pbls >= dev->attr.max_num_mr_pbl);
Parav Panditfe2caef2012-03-21 04:09:06 +0530801
802 mr->hwmr.num_pbes = num_pbes;
803 mr->hwmr.num_pbls = num_pbls;
804 mr->hwmr.pbl_size = pbl_size;
805 return status;
806}
807
808static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
809{
810 int status = 0;
811 int i;
812 u32 dma_len = mr->pbl_size;
813 struct pci_dev *pdev = dev->nic_info.pdev;
814 void *va;
815 dma_addr_t pa;
816
817 mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) *
818 mr->num_pbls, GFP_KERNEL);
819
820 if (!mr->pbl_table)
821 return -ENOMEM;
822
823 for (i = 0; i < mr->num_pbls; i++) {
824 va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
825 if (!va) {
826 ocrdma_free_mr_pbl_tbl(dev, mr);
827 status = -ENOMEM;
828 break;
829 }
830 memset(va, 0, dma_len);
831 mr->pbl_table[i].va = va;
832 mr->pbl_table[i].pa = pa;
833 }
834 return status;
835}
836
837static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
838 u32 num_pbes)
839{
840 struct ocrdma_pbe *pbe;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200841 struct scatterlist *sg;
Parav Panditfe2caef2012-03-21 04:09:06 +0530842 struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
843 struct ib_umem *umem = mr->umem;
Yishai Hadaseeb84612014-01-28 13:40:15 +0200844 int shift, pg_cnt, pages, pbe_cnt, entry, total_num_pbes = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530845
846 if (!mr->hwmr.num_pbes)
847 return;
848
849 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
850 pbe_cnt = 0;
851
852 shift = ilog2(umem->page_size);
853
Yishai Hadaseeb84612014-01-28 13:40:15 +0200854 for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
855 pages = sg_dma_len(sg) >> shift;
856 for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) {
857 /* store the page address in pbe */
858 pbe->pa_lo =
859 cpu_to_le32(sg_dma_address
860 (sg) +
861 (umem->page_size * pg_cnt));
862 pbe->pa_hi =
863 cpu_to_le32(upper_32_bits
864 ((sg_dma_address
865 (sg) +
866 umem->page_size * pg_cnt)));
867 pbe_cnt += 1;
868 total_num_pbes += 1;
869 pbe++;
Parav Panditfe2caef2012-03-21 04:09:06 +0530870
Yishai Hadaseeb84612014-01-28 13:40:15 +0200871 /* if done building pbes, issue the mbx cmd. */
872 if (total_num_pbes == num_pbes)
873 return;
Parav Panditfe2caef2012-03-21 04:09:06 +0530874
Yishai Hadaseeb84612014-01-28 13:40:15 +0200875 /* if the given pbl is full storing the pbes,
876 * move to next pbl.
877 */
878 if (pbe_cnt ==
879 (mr->hwmr.pbl_size / sizeof(u64))) {
880 pbl_tbl++;
881 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
882 pbe_cnt = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530883 }
Yishai Hadaseeb84612014-01-28 13:40:15 +0200884
Parav Panditfe2caef2012-03-21 04:09:06 +0530885 }
886 }
887}
888
889struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
890 u64 usr_addr, int acc, struct ib_udata *udata)
891{
892 int status = -ENOMEM;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530893 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530894 struct ocrdma_mr *mr;
895 struct ocrdma_pd *pd;
Parav Panditfe2caef2012-03-21 04:09:06 +0530896 u32 num_pbes;
897
898 pd = get_ocrdma_pd(ibpd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530899
900 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
901 return ERR_PTR(-EINVAL);
902
903 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
904 if (!mr)
905 return ERR_PTR(status);
Parav Panditfe2caef2012-03-21 04:09:06 +0530906 mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
907 if (IS_ERR(mr->umem)) {
908 status = -EFAULT;
909 goto umem_err;
910 }
911 num_pbes = ib_umem_page_count(mr->umem);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530912 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
Parav Panditfe2caef2012-03-21 04:09:06 +0530913 if (status)
914 goto umem_err;
915
916 mr->hwmr.pbe_size = mr->umem->page_size;
Haggai Eran406f9e52014-12-11 17:04:12 +0200917 mr->hwmr.fbo = ib_umem_offset(mr->umem);
Parav Panditfe2caef2012-03-21 04:09:06 +0530918 mr->hwmr.va = usr_addr;
919 mr->hwmr.len = len;
920 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
921 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
922 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
923 mr->hwmr.local_rd = 1;
924 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
925 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
926 if (status)
927 goto umem_err;
928 build_user_pbes(dev, mr, num_pbes);
929 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
930 if (status)
931 goto mbx_err;
Parav Panditfe2caef2012-03-21 04:09:06 +0530932 mr->ibmr.lkey = mr->hwmr.lkey;
933 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
934 mr->ibmr.rkey = mr->hwmr.lkey;
935
936 return &mr->ibmr;
937
938mbx_err:
939 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
940umem_err:
941 kfree(mr);
942 return ERR_PTR(status);
943}
944
945int ocrdma_dereg_mr(struct ib_mr *ib_mr)
946{
947 struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530948 struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530949
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530950 (void) ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
Parav Panditfe2caef2012-03-21 04:09:06 +0530951
Selvin Xavier9d1878a2014-02-04 11:57:02 +0530952 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
Parav Panditfe2caef2012-03-21 04:09:06 +0530953
Parav Panditfe2caef2012-03-21 04:09:06 +0530954 /* it could be user registered memory. */
955 if (mr->umem)
956 ib_umem_release(mr->umem);
957 kfree(mr);
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530958
959 /* Don't stop cleanup, in case FW is unresponsive */
960 if (dev->mqe_ctx.fw_error_state) {
Mitesh Ahuja6dab0262014-06-10 19:32:21 +0530961 pr_err("%s(%d) fw not responding.\n",
962 __func__, dev->id);
963 }
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +0530964 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530965}
966
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530967static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
968 struct ib_udata *udata,
Parav Panditfe2caef2012-03-21 04:09:06 +0530969 struct ib_ucontext *ib_ctx)
970{
971 int status;
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530972 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
Parav Panditfe2caef2012-03-21 04:09:06 +0530973 struct ocrdma_create_cq_uresp uresp;
974
Dan Carpenter63ea3742013-07-29 22:34:29 +0300975 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530976 uresp.cq_id = cq->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +0530977 uresp.page_size = PAGE_ALIGN(cq->len);
Parav Panditfe2caef2012-03-21 04:09:06 +0530978 uresp.num_pages = 1;
979 uresp.max_hw_cqe = cq->max_hw_cqe;
Devesh Sharma1b76d382014-09-05 19:35:40 +0530980 uresp.page_addr[0] = virt_to_phys(cq->va);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +0530981 uresp.db_page_addr = ocrdma_get_db_addr(dev, uctx->cntxt_pd->id);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530982 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530983 uresp.phase_change = cq->phase_change ? 1 : 0;
984 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
985 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000986 pr_err("%s(%d) copy error cqid=0x%x.\n",
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530987 __func__, dev->id, cq->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530988 goto err;
989 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530990 status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
991 if (status)
992 goto err;
993 status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
994 if (status) {
995 ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
996 goto err;
997 }
998 cq->ucontext = uctx;
999err:
1000 return status;
1001}
1002
1003struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector,
1004 struct ib_ucontext *ib_ctx,
1005 struct ib_udata *udata)
1006{
1007 struct ocrdma_cq *cq;
1008 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301009 struct ocrdma_ucontext *uctx = NULL;
1010 u16 pd_id = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301011 int status;
1012 struct ocrdma_create_cq_ureq ureq;
1013
1014 if (udata) {
1015 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1016 return ERR_PTR(-EFAULT);
1017 } else
1018 ureq.dpp_cq = 0;
1019 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
1020 if (!cq)
1021 return ERR_PTR(-ENOMEM);
1022
1023 spin_lock_init(&cq->cq_lock);
1024 spin_lock_init(&cq->comp_handler_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301025 INIT_LIST_HEAD(&cq->sq_head);
1026 INIT_LIST_HEAD(&cq->rq_head);
Devesh Sharmaea617622014-02-04 11:56:54 +05301027 cq->first_arm = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05301028
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301029 if (ib_ctx) {
1030 uctx = get_ocrdma_ucontext(ib_ctx);
1031 pd_id = uctx->cntxt_pd->id;
1032 }
1033
1034 status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq, pd_id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301035 if (status) {
1036 kfree(cq);
1037 return ERR_PTR(status);
1038 }
1039 if (ib_ctx) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301040 status = ocrdma_copy_cq_uresp(dev, cq, udata, ib_ctx);
Parav Panditfe2caef2012-03-21 04:09:06 +05301041 if (status)
1042 goto ctx_err;
1043 }
1044 cq->phase = OCRDMA_CQE_VALID;
Parav Panditfe2caef2012-03-21 04:09:06 +05301045 dev->cq_tbl[cq->id] = cq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301046 return &cq->ibcq;
1047
1048ctx_err:
1049 ocrdma_mbx_destroy_cq(dev, cq);
1050 kfree(cq);
1051 return ERR_PTR(status);
1052}
1053
1054int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
1055 struct ib_udata *udata)
1056{
1057 int status = 0;
1058 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
1059
1060 if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
1061 status = -EINVAL;
1062 return status;
1063 }
1064 ibcq->cqe = new_cnt;
1065 return status;
1066}
1067
Devesh Sharmaea617622014-02-04 11:56:54 +05301068static void ocrdma_flush_cq(struct ocrdma_cq *cq)
1069{
1070 int cqe_cnt;
1071 int valid_count = 0;
1072 unsigned long flags;
1073
1074 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
1075 struct ocrdma_cqe *cqe = NULL;
1076
1077 cqe = cq->va;
1078 cqe_cnt = cq->cqe_cnt;
1079
1080 /* Last irq might have scheduled a polling thread
1081 * sync-up with it before hard flushing.
1082 */
1083 spin_lock_irqsave(&cq->cq_lock, flags);
1084 while (cqe_cnt) {
1085 if (is_cqe_valid(cq, cqe))
1086 valid_count++;
1087 cqe++;
1088 cqe_cnt--;
1089 }
1090 ocrdma_ring_cq_db(dev, cq->id, false, false, valid_count);
1091 spin_unlock_irqrestore(&cq->cq_lock, flags);
1092}
1093
Parav Panditfe2caef2012-03-21 04:09:06 +05301094int ocrdma_destroy_cq(struct ib_cq *ibcq)
1095{
Parav Panditfe2caef2012-03-21 04:09:06 +05301096 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
Devesh Sharmaea617622014-02-04 11:56:54 +05301097 struct ocrdma_eq *eq = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301098 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301099 int pdid = 0;
Devesh Sharmaea617622014-02-04 11:56:54 +05301100 u32 irq, indx;
1101
1102 dev->cq_tbl[cq->id] = NULL;
1103 indx = ocrdma_get_eq_table_index(dev, cq->eqn);
1104 if (indx == -EINVAL)
1105 BUG();
1106
1107 eq = &dev->eq_tbl[indx];
1108 irq = ocrdma_get_irq(dev, eq);
1109 synchronize_irq(irq);
1110 ocrdma_flush_cq(cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301111
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301112 (void)ocrdma_mbx_destroy_cq(dev, cq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301113 if (cq->ucontext) {
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301114 pdid = cq->ucontext->cntxt_pd->id;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301115 ocrdma_del_mmap(cq->ucontext, (u64) cq->pa,
1116 PAGE_ALIGN(cq->len));
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05301117 ocrdma_del_mmap(cq->ucontext,
1118 ocrdma_get_db_addr(dev, pdid),
Parav Panditfe2caef2012-03-21 04:09:06 +05301119 dev->nic_info.db_page_size);
1120 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301121
1122 kfree(cq);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301123 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301124}
1125
1126static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1127{
1128 int status = -EINVAL;
1129
1130 if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
1131 dev->qp_tbl[qp->id] = qp;
1132 status = 0;
1133 }
1134 return status;
1135}
1136
1137static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1138{
1139 dev->qp_tbl[qp->id] = NULL;
1140}
1141
1142static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
1143 struct ib_qp_init_attr *attrs)
1144{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301145 if ((attrs->qp_type != IB_QPT_GSI) &&
1146 (attrs->qp_type != IB_QPT_RC) &&
1147 (attrs->qp_type != IB_QPT_UC) &&
1148 (attrs->qp_type != IB_QPT_UD)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001149 pr_err("%s(%d) unsupported qp type=0x%x requested\n",
1150 __func__, dev->id, attrs->qp_type);
Parav Panditfe2caef2012-03-21 04:09:06 +05301151 return -EINVAL;
1152 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301153 /* Skip the check for QP1 to support CM size of 128 */
1154 if ((attrs->qp_type != IB_QPT_GSI) &&
1155 (attrs->cap.max_send_wr > dev->attr.max_wqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001156 pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
1157 __func__, dev->id, attrs->cap.max_send_wr);
1158 pr_err("%s(%d) supported send_wr=0x%x\n",
1159 __func__, dev->id, dev->attr.max_wqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301160 return -EINVAL;
1161 }
1162 if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001163 pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
1164 __func__, dev->id, attrs->cap.max_recv_wr);
1165 pr_err("%s(%d) supported recv_wr=0x%x\n",
1166 __func__, dev->id, dev->attr.max_rqe);
Parav Panditfe2caef2012-03-21 04:09:06 +05301167 return -EINVAL;
1168 }
1169 if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001170 pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
1171 __func__, dev->id, attrs->cap.max_inline_data);
1172 pr_err("%s(%d) supported inline data size=0x%x\n",
1173 __func__, dev->id, dev->attr.max_inline_data);
Parav Panditfe2caef2012-03-21 04:09:06 +05301174 return -EINVAL;
1175 }
1176 if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001177 pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
1178 __func__, dev->id, attrs->cap.max_send_sge);
1179 pr_err("%s(%d) supported send_sge=0x%x\n",
1180 __func__, dev->id, dev->attr.max_send_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301181 return -EINVAL;
1182 }
1183 if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001184 pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
1185 __func__, dev->id, attrs->cap.max_recv_sge);
1186 pr_err("%s(%d) supported recv_sge=0x%x\n",
1187 __func__, dev->id, dev->attr.max_recv_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301188 return -EINVAL;
1189 }
1190 /* unprivileged user space cannot create special QP */
1191 if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001192 pr_err
Parav Panditfe2caef2012-03-21 04:09:06 +05301193 ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
1194 __func__, dev->id, attrs->qp_type);
1195 return -EINVAL;
1196 }
1197 /* allow creating only one GSI type of QP */
1198 if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001199 pr_err("%s(%d) GSI special QPs already created.\n",
1200 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301201 return -EINVAL;
1202 }
1203 /* verify consumer QPs are not trying to use GSI QP's CQ */
1204 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
1205 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301206 (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001207 pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301208 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301209 return -EINVAL;
1210 }
1211 }
1212 return 0;
1213}
1214
1215static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
1216 struct ib_udata *udata, int dpp_offset,
1217 int dpp_credit_lmt, int srq)
1218{
1219 int status = 0;
1220 u64 usr_db;
1221 struct ocrdma_create_qp_uresp uresp;
Parav Panditfe2caef2012-03-21 04:09:06 +05301222 struct ocrdma_pd *pd = qp->pd;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301223 struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301224
1225 memset(&uresp, 0, sizeof(uresp));
1226 usr_db = dev->nic_info.unmapped_db +
1227 (pd->id * dev->nic_info.db_page_size);
1228 uresp.qp_id = qp->id;
1229 uresp.sq_dbid = qp->sq.dbid;
1230 uresp.num_sq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301231 uresp.sq_page_size = PAGE_ALIGN(qp->sq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301232 uresp.sq_page_addr[0] = virt_to_phys(qp->sq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301233 uresp.num_wqe_allocated = qp->sq.max_cnt;
1234 if (!srq) {
1235 uresp.rq_dbid = qp->rq.dbid;
1236 uresp.num_rq_pages = 1;
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301237 uresp.rq_page_size = PAGE_ALIGN(qp->rq.len);
Devesh Sharma1b76d382014-09-05 19:35:40 +05301238 uresp.rq_page_addr[0] = virt_to_phys(qp->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301239 uresp.num_rqe_allocated = qp->rq.max_cnt;
1240 }
1241 uresp.db_page_addr = usr_db;
1242 uresp.db_page_size = dev->nic_info.db_page_size;
Devesh Sharma2df84fa82014-02-04 11:56:55 +05301243 uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
1244 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
1245 uresp.db_shift = OCRDMA_DB_RQ_SHIFT;
Parav Panditfe2caef2012-03-21 04:09:06 +05301246
1247 if (qp->dpp_enabled) {
1248 uresp.dpp_credit = dpp_credit_lmt;
1249 uresp.dpp_offset = dpp_offset;
1250 }
1251 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1252 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001253 pr_err("%s(%d) user copy error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +05301254 goto err;
1255 }
1256 status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
1257 uresp.sq_page_size);
1258 if (status)
1259 goto err;
1260
1261 if (!srq) {
1262 status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
1263 uresp.rq_page_size);
1264 if (status)
1265 goto rq_map_err;
1266 }
1267 return status;
1268rq_map_err:
1269 ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
1270err:
1271 return status;
1272}
1273
1274static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
1275 struct ocrdma_pd *pd)
1276{
Devesh Sharma21c33912014-02-04 11:56:56 +05301277 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301278 qp->sq_db = dev->nic_info.db +
1279 (pd->id * dev->nic_info.db_page_size) +
1280 OCRDMA_DB_GEN2_SQ_OFFSET;
1281 qp->rq_db = dev->nic_info.db +
1282 (pd->id * dev->nic_info.db_page_size) +
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301283 OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301284 } else {
1285 qp->sq_db = dev->nic_info.db +
1286 (pd->id * dev->nic_info.db_page_size) +
1287 OCRDMA_DB_SQ_OFFSET;
1288 qp->rq_db = dev->nic_info.db +
1289 (pd->id * dev->nic_info.db_page_size) +
1290 OCRDMA_DB_RQ_OFFSET;
1291 }
1292}
1293
1294static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
1295{
1296 qp->wqe_wr_id_tbl =
1297 kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
1298 GFP_KERNEL);
1299 if (qp->wqe_wr_id_tbl == NULL)
1300 return -ENOMEM;
1301 qp->rqe_wr_id_tbl =
1302 kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
1303 if (qp->rqe_wr_id_tbl == NULL)
1304 return -ENOMEM;
1305
1306 return 0;
1307}
1308
1309static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
1310 struct ocrdma_pd *pd,
1311 struct ib_qp_init_attr *attrs)
1312{
1313 qp->pd = pd;
1314 spin_lock_init(&qp->q_lock);
1315 INIT_LIST_HEAD(&qp->sq_entry);
1316 INIT_LIST_HEAD(&qp->rq_entry);
1317
1318 qp->qp_type = attrs->qp_type;
1319 qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
1320 qp->max_inline_data = attrs->cap.max_inline_data;
1321 qp->sq.max_sges = attrs->cap.max_send_sge;
1322 qp->rq.max_sges = attrs->cap.max_recv_sge;
1323 qp->state = OCRDMA_QPS_RST;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05301324 qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
Parav Panditfe2caef2012-03-21 04:09:06 +05301325}
1326
Parav Panditfe2caef2012-03-21 04:09:06 +05301327static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
1328 struct ib_qp_init_attr *attrs)
1329{
1330 if (attrs->qp_type == IB_QPT_GSI) {
1331 dev->gsi_qp_created = 1;
1332 dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
1333 dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
1334 }
1335}
1336
1337struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
1338 struct ib_qp_init_attr *attrs,
1339 struct ib_udata *udata)
1340{
1341 int status;
1342 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1343 struct ocrdma_qp *qp;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301344 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301345 struct ocrdma_create_qp_ureq ureq;
1346 u16 dpp_credit_lmt, dpp_offset;
1347
1348 status = ocrdma_check_qp_params(ibpd, dev, attrs);
1349 if (status)
1350 goto gen_err;
1351
1352 memset(&ureq, 0, sizeof(ureq));
1353 if (udata) {
1354 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1355 return ERR_PTR(-EFAULT);
1356 }
1357 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1358 if (!qp) {
1359 status = -ENOMEM;
1360 goto gen_err;
1361 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301362 ocrdma_set_qp_init_params(qp, pd, attrs);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301363 if (udata == NULL)
1364 qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
1365 OCRDMA_QP_FAST_REG);
Parav Panditfe2caef2012-03-21 04:09:06 +05301366
1367 mutex_lock(&dev->dev_lock);
1368 status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
1369 ureq.dpp_cq_id,
1370 &dpp_offset, &dpp_credit_lmt);
1371 if (status)
1372 goto mbx_err;
1373
1374 /* user space QP's wr_id table are managed in library */
1375 if (udata == NULL) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301376 status = ocrdma_alloc_wr_id_tbl(qp);
1377 if (status)
1378 goto map_err;
1379 }
1380
1381 status = ocrdma_add_qpn_map(dev, qp);
1382 if (status)
1383 goto map_err;
1384 ocrdma_set_qp_db(dev, qp, pd);
1385 if (udata) {
1386 status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
1387 dpp_credit_lmt,
1388 (attrs->srq != NULL));
1389 if (status)
1390 goto cpy_err;
1391 }
1392 ocrdma_store_gsi_qp_cq(dev, attrs);
Gottumukkala, Naresh27159f52013-06-05 08:50:46 +00001393 qp->ibqp.qp_num = qp->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301394 mutex_unlock(&dev->dev_lock);
1395 return &qp->ibqp;
1396
1397cpy_err:
1398 ocrdma_del_qpn_map(dev, qp);
1399map_err:
1400 ocrdma_mbx_destroy_qp(dev, qp);
1401mbx_err:
1402 mutex_unlock(&dev->dev_lock);
1403 kfree(qp->wqe_wr_id_tbl);
1404 kfree(qp->rqe_wr_id_tbl);
1405 kfree(qp);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001406 pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05301407gen_err:
1408 return ERR_PTR(status);
1409}
1410
1411int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1412 int attr_mask)
1413{
1414 int status = 0;
1415 struct ocrdma_qp *qp;
1416 struct ocrdma_dev *dev;
1417 enum ib_qp_state old_qps;
1418
1419 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301420 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301421 if (attr_mask & IB_QP_STATE)
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301422 status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301423 /* if new and previous states are same hw doesn't need to
1424 * know about it.
1425 */
1426 if (status < 0)
1427 return status;
Prarit Bhargavabc1b04a2014-02-19 15:05:16 -05001428 status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask);
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +05301429
Parav Panditfe2caef2012-03-21 04:09:06 +05301430 return status;
1431}
1432
1433int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1434 int attr_mask, struct ib_udata *udata)
1435{
1436 unsigned long flags;
1437 int status = -EINVAL;
1438 struct ocrdma_qp *qp;
1439 struct ocrdma_dev *dev;
1440 enum ib_qp_state old_qps, new_qps;
1441
1442 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301443 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301444
1445 /* syncronize with multiple context trying to change, retrive qps */
1446 mutex_lock(&dev->dev_lock);
1447 /* syncronize with wqe, rqe posting and cqe processing contexts */
1448 spin_lock_irqsave(&qp->q_lock, flags);
1449 old_qps = get_ibqp_state(qp->state);
1450 if (attr_mask & IB_QP_STATE)
1451 new_qps = attr->qp_state;
1452 else
1453 new_qps = old_qps;
1454 spin_unlock_irqrestore(&qp->q_lock, flags);
1455
Matan Barakdd5f03b2013-12-12 18:03:11 +02001456 if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask,
Moni Shoua37721d82013-12-12 18:03:16 +02001457 IB_LINK_LAYER_ETHERNET)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001458 pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
1459 "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
1460 __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
1461 old_qps, new_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301462 goto param_err;
1463 }
1464
1465 status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
1466 if (status > 0)
1467 status = 0;
1468param_err:
1469 mutex_unlock(&dev->dev_lock);
1470 return status;
1471}
1472
1473static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
1474{
1475 switch (mtu) {
1476 case 256:
1477 return IB_MTU_256;
1478 case 512:
1479 return IB_MTU_512;
1480 case 1024:
1481 return IB_MTU_1024;
1482 case 2048:
1483 return IB_MTU_2048;
1484 case 4096:
1485 return IB_MTU_4096;
1486 default:
1487 return IB_MTU_1024;
1488 }
1489}
1490
1491static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
1492{
1493 int ib_qp_acc_flags = 0;
1494
1495 if (qp_cap_flags & OCRDMA_QP_INB_WR)
1496 ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
1497 if (qp_cap_flags & OCRDMA_QP_INB_RD)
1498 ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
1499 return ib_qp_acc_flags;
1500}
1501
1502int ocrdma_query_qp(struct ib_qp *ibqp,
1503 struct ib_qp_attr *qp_attr,
1504 int attr_mask, struct ib_qp_init_attr *qp_init_attr)
1505{
1506 int status;
1507 u32 qp_state;
1508 struct ocrdma_qp_params params;
1509 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301510 struct ocrdma_dev *dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301511
1512 memset(&params, 0, sizeof(params));
1513 mutex_lock(&dev->dev_lock);
1514 status = ocrdma_mbx_query_qp(dev, qp, &params);
1515 mutex_unlock(&dev->dev_lock);
1516 if (status)
1517 goto mbx_err;
Mitesh Ahuja95bf0092014-12-03 11:36:33 +05301518 if (qp->qp_type == IB_QPT_UD)
1519 qp_attr->qkey = params.qkey;
Parav Panditfe2caef2012-03-21 04:09:06 +05301520 qp_attr->path_mtu =
1521 ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
1522 OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
1523 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
1524 qp_attr->path_mig_state = IB_MIG_MIGRATED;
1525 qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
1526 qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
1527 qp_attr->dest_qp_num =
1528 params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
1529
1530 qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
1531 qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
1532 qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
1533 qp_attr->cap.max_send_sge = qp->sq.max_sges;
1534 qp_attr->cap.max_recv_sge = qp->rq.max_sges;
Naresh Gottumukkalac43e9ab2013-08-26 15:27:46 +05301535 qp_attr->cap.max_inline_data = qp->max_inline_data;
Parav Panditfe2caef2012-03-21 04:09:06 +05301536 qp_init_attr->cap = qp_attr->cap;
1537 memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
1538 sizeof(params.dgid));
1539 qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
1540 OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
1541 qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
1542 qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
1543 OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
1544 OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
1545 qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
Devesh Sharmaa61d93d2014-02-10 13:48:58 +05301546 OCRDMA_QP_PARAMS_TCLASS_MASK) >>
Parav Panditfe2caef2012-03-21 04:09:06 +05301547 OCRDMA_QP_PARAMS_TCLASS_SHIFT;
1548
1549 qp_attr->ah_attr.ah_flags = IB_AH_GRH;
1550 qp_attr->ah_attr.port_num = 1;
1551 qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
1552 OCRDMA_QP_PARAMS_SL_MASK) >>
1553 OCRDMA_QP_PARAMS_SL_SHIFT;
1554 qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
1555 OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
1556 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
1557 qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
1558 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
1559 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
1560 qp_attr->retry_cnt =
1561 (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
1562 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
1563 qp_attr->min_rnr_timer = 0;
1564 qp_attr->pkey_index = 0;
1565 qp_attr->port_num = 1;
1566 qp_attr->ah_attr.src_path_bits = 0;
1567 qp_attr->ah_attr.static_rate = 0;
1568 qp_attr->alt_pkey_index = 0;
1569 qp_attr->alt_port_num = 0;
1570 qp_attr->alt_timeout = 0;
1571 memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
1572 qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
1573 OCRDMA_QP_PARAMS_STATE_SHIFT;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301574 qp_attr->qp_state = get_ibqp_state(qp_state);
1575 qp_attr->cur_qp_state = qp_attr->qp_state;
Parav Panditfe2caef2012-03-21 04:09:06 +05301576 qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
1577 qp_attr->max_dest_rd_atomic =
1578 params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
1579 qp_attr->max_rd_atomic =
1580 params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
1581 qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
1582 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
Padmanabh Ratnakar43c706b2014-12-18 14:13:00 +05301583 /* Sync driver QP state with FW */
1584 ocrdma_qp_state_change(qp, qp_attr->qp_state, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05301585mbx_err:
1586 return status;
1587}
1588
1589static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx)
1590{
1591 int i = idx / 32;
1592 unsigned int mask = (1 << (idx % 32));
1593
1594 if (srq->idx_bit_fields[i] & mask)
1595 srq->idx_bit_fields[i] &= ~mask;
1596 else
1597 srq->idx_bit_fields[i] |= mask;
1598}
1599
1600static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
1601{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301602 return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
Parav Panditfe2caef2012-03-21 04:09:06 +05301603}
1604
1605static int is_hw_sq_empty(struct ocrdma_qp *qp)
1606{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301607 return (qp->sq.tail == qp->sq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301608}
1609
1610static int is_hw_rq_empty(struct ocrdma_qp *qp)
1611{
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301612 return (qp->rq.tail == qp->rq.head);
Parav Panditfe2caef2012-03-21 04:09:06 +05301613}
1614
1615static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
1616{
1617 return q->va + (q->head * q->entry_size);
1618}
1619
1620static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
1621 u32 idx)
1622{
1623 return q->va + (idx * q->entry_size);
1624}
1625
1626static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
1627{
1628 q->head = (q->head + 1) & q->max_wqe_idx;
1629}
1630
1631static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
1632{
1633 q->tail = (q->tail + 1) & q->max_wqe_idx;
1634}
1635
1636/* discard the cqe for a given QP */
1637static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
1638{
1639 unsigned long cq_flags;
1640 unsigned long flags;
1641 int discard_cnt = 0;
1642 u32 cur_getp, stop_getp;
1643 struct ocrdma_cqe *cqe;
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301644 u32 qpn = 0, wqe_idx = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301645
1646 spin_lock_irqsave(&cq->cq_lock, cq_flags);
1647
1648 /* traverse through the CQEs in the hw CQ,
1649 * find the matching CQE for a given qp,
1650 * mark the matching one discarded by clearing qpn.
1651 * ring the doorbell in the poll_cq() as
1652 * we don't complete out of order cqe.
1653 */
1654
1655 cur_getp = cq->getp;
1656 /* find upto when do we reap the cq. */
1657 stop_getp = cur_getp;
1658 do {
1659 if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
1660 break;
1661
1662 cqe = cq->va + cur_getp;
1663 /* if (a) done reaping whole hw cq, or
1664 * (b) qp_xq becomes empty.
1665 * then exit
1666 */
1667 qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
1668 /* if previously discarded cqe found, skip that too. */
1669 /* check for matching qp */
1670 if (qpn == 0 || qpn != qp->id)
1671 goto skip_cqe;
1672
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301673 if (is_cqe_for_sq(cqe)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301674 ocrdma_hwq_inc_tail(&qp->sq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301675 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301676 if (qp->srq) {
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301677 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
1678 OCRDMA_CQE_BUFTAG_SHIFT) &
1679 qp->srq->rq.max_wqe_idx;
1680 if (wqe_idx < 1)
1681 BUG();
Parav Panditfe2caef2012-03-21 04:09:06 +05301682 spin_lock_irqsave(&qp->srq->q_lock, flags);
1683 ocrdma_hwq_inc_tail(&qp->srq->rq);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301684 ocrdma_srq_toggle_bit(qp->srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05301685 spin_unlock_irqrestore(&qp->srq->q_lock, flags);
1686
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301687 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301688 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301689 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301690 }
Selvin Xaviercf5788a2014-02-04 11:57:03 +05301691 /* mark cqe discarded so that it is not picked up later
1692 * in the poll_cq().
1693 */
1694 discard_cnt += 1;
1695 cqe->cmn.qpn = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301696skip_cqe:
1697 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
1698 } while (cur_getp != stop_getp);
1699 spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
1700}
1701
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301702void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
Parav Panditfe2caef2012-03-21 04:09:06 +05301703{
1704 int found = false;
1705 unsigned long flags;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301706 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301707 /* sync with any active CQ poll */
1708
1709 spin_lock_irqsave(&dev->flush_q_lock, flags);
1710 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1711 if (found)
1712 list_del(&qp->sq_entry);
1713 if (!qp->srq) {
1714 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1715 if (found)
1716 list_del(&qp->rq_entry);
1717 }
1718 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
1719}
1720
1721int ocrdma_destroy_qp(struct ib_qp *ibqp)
1722{
Parav Panditfe2caef2012-03-21 04:09:06 +05301723 struct ocrdma_pd *pd;
1724 struct ocrdma_qp *qp;
1725 struct ocrdma_dev *dev;
1726 struct ib_qp_attr attrs;
1727 int attr_mask = IB_QP_STATE;
Dan Carpenterd19081e2012-05-02 09:14:47 +03001728 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05301729
1730 qp = get_ocrdma_qp(ibqp);
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05301731 dev = get_ocrdma_dev(ibqp->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301732
1733 attrs.qp_state = IB_QPS_ERR;
1734 pd = qp->pd;
1735
1736 /* change the QP state to ERROR */
1737 _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
1738
1739 /* ensure that CQEs for newly created QP (whose id may be same with
1740 * one which just getting destroyed are same), dont get
1741 * discarded until the old CQEs are discarded.
1742 */
1743 mutex_lock(&dev->dev_lock);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301744 (void) ocrdma_mbx_destroy_qp(dev, qp);
Parav Panditfe2caef2012-03-21 04:09:06 +05301745
1746 /*
1747 * acquire CQ lock while destroy is in progress, in order to
1748 * protect against proessing in-flight CQEs for this QP.
1749 */
Dan Carpenterd19081e2012-05-02 09:14:47 +03001750 spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301751 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001752 spin_lock(&qp->rq_cq->cq_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301753
1754 ocrdma_del_qpn_map(dev, qp);
1755
1756 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001757 spin_unlock(&qp->rq_cq->cq_lock);
1758 spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301759
1760 if (!pd->uctx) {
1761 ocrdma_discard_cqes(qp, qp->sq_cq);
1762 ocrdma_discard_cqes(qp, qp->rq_cq);
1763 }
1764 mutex_unlock(&dev->dev_lock);
1765
1766 if (pd->uctx) {
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301767 ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
1768 PAGE_ALIGN(qp->sq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301769 if (!qp->srq)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301770 ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
1771 PAGE_ALIGN(qp->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301772 }
1773
1774 ocrdma_del_flush_qp(qp);
1775
Parav Panditfe2caef2012-03-21 04:09:06 +05301776 kfree(qp->wqe_wr_id_tbl);
1777 kfree(qp->rqe_wr_id_tbl);
1778 kfree(qp);
Mitesh Ahuja4b8180a2014-12-18 14:13:01 +05301779 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +05301780}
1781
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301782static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
1783 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301784{
1785 int status;
1786 struct ocrdma_create_srq_uresp uresp;
1787
Dan Carpenter63ea3742013-07-29 22:34:29 +03001788 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +05301789 uresp.rq_dbid = srq->rq.dbid;
1790 uresp.num_rq_pages = 1;
Devesh Sharma1b76d382014-09-05 19:35:40 +05301791 uresp.rq_page_addr[0] = virt_to_phys(srq->rq.va);
Parav Panditfe2caef2012-03-21 04:09:06 +05301792 uresp.rq_page_size = srq->rq.len;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301793 uresp.db_page_addr = dev->nic_info.unmapped_db +
1794 (srq->pd->id * dev->nic_info.db_page_size);
1795 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +05301796 uresp.num_rqe_allocated = srq->rq.max_cnt;
Devesh Sharma21c33912014-02-04 11:56:56 +05301797 if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
Naresh Gottumukkalaf11220e2013-08-26 15:27:42 +05301798 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
Parav Panditfe2caef2012-03-21 04:09:06 +05301799 uresp.db_shift = 24;
1800 } else {
1801 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
1802 uresp.db_shift = 16;
1803 }
1804
1805 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1806 if (status)
1807 return status;
1808 status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
1809 uresp.rq_page_size);
1810 if (status)
1811 return status;
1812 return status;
1813}
1814
1815struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
1816 struct ib_srq_init_attr *init_attr,
1817 struct ib_udata *udata)
1818{
1819 int status = -ENOMEM;
1820 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301821 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301822 struct ocrdma_srq *srq;
1823
1824 if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
1825 return ERR_PTR(-EINVAL);
1826 if (init_attr->attr.max_wr > dev->attr.max_rqe)
1827 return ERR_PTR(-EINVAL);
1828
1829 srq = kzalloc(sizeof(*srq), GFP_KERNEL);
1830 if (!srq)
1831 return ERR_PTR(status);
1832
1833 spin_lock_init(&srq->q_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301834 srq->pd = pd;
1835 srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301836 status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +05301837 if (status)
1838 goto err;
1839
1840 if (udata == NULL) {
1841 srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
1842 GFP_KERNEL);
1843 if (srq->rqe_wr_id_tbl == NULL)
1844 goto arm_err;
1845
1846 srq->bit_fields_len = (srq->rq.max_cnt / 32) +
1847 (srq->rq.max_cnt % 32 ? 1 : 0);
1848 srq->idx_bit_fields =
1849 kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
1850 if (srq->idx_bit_fields == NULL)
1851 goto arm_err;
1852 memset(srq->idx_bit_fields, 0xff,
1853 srq->bit_fields_len * sizeof(u32));
1854 }
1855
1856 if (init_attr->attr.srq_limit) {
1857 status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
1858 if (status)
1859 goto arm_err;
1860 }
1861
Parav Panditfe2caef2012-03-21 04:09:06 +05301862 if (udata) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301863 status = ocrdma_copy_srq_uresp(dev, srq, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +05301864 if (status)
1865 goto arm_err;
1866 }
1867
Parav Panditfe2caef2012-03-21 04:09:06 +05301868 return &srq->ibsrq;
1869
1870arm_err:
1871 ocrdma_mbx_destroy_srq(dev, srq);
1872err:
1873 kfree(srq->rqe_wr_id_tbl);
1874 kfree(srq->idx_bit_fields);
1875 kfree(srq);
1876 return ERR_PTR(status);
1877}
1878
1879int ocrdma_modify_srq(struct ib_srq *ibsrq,
1880 struct ib_srq_attr *srq_attr,
1881 enum ib_srq_attr_mask srq_attr_mask,
1882 struct ib_udata *udata)
1883{
1884 int status = 0;
1885 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301886
1887 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301888 if (srq_attr_mask & IB_SRQ_MAX_WR)
1889 status = -EINVAL;
1890 else
1891 status = ocrdma_mbx_modify_srq(srq, srq_attr);
1892 return status;
1893}
1894
1895int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
1896{
1897 int status;
1898 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301899
1900 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301901 status = ocrdma_mbx_query_srq(srq, srq_attr);
1902 return status;
1903}
1904
1905int ocrdma_destroy_srq(struct ib_srq *ibsrq)
1906{
1907 int status;
1908 struct ocrdma_srq *srq;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301909 struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301910
1911 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301912
1913 status = ocrdma_mbx_destroy_srq(dev, srq);
1914
1915 if (srq->pd->uctx)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301916 ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
1917 PAGE_ALIGN(srq->rq.len));
Parav Panditfe2caef2012-03-21 04:09:06 +05301918
Parav Panditfe2caef2012-03-21 04:09:06 +05301919 kfree(srq->idx_bit_fields);
1920 kfree(srq->rqe_wr_id_tbl);
1921 kfree(srq);
1922 return status;
1923}
1924
1925/* unprivileged verbs and their support functions. */
1926static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
1927 struct ocrdma_hdr_wqe *hdr,
1928 struct ib_send_wr *wr)
1929{
1930 struct ocrdma_ewqe_ud_hdr *ud_hdr =
1931 (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
1932 struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
1933
1934 ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
1935 if (qp->qp_type == IB_QPT_GSI)
1936 ud_hdr->qkey = qp->qkey;
1937 else
1938 ud_hdr->qkey = wr->wr.ud.remote_qkey;
1939 ud_hdr->rsvd_ahid = ah->id;
Devesh Sharma29565f22014-12-18 14:13:07 +05301940 if (ah->av->valid & OCRDMA_AV_VLAN_VALID)
1941 hdr->cw |= (OCRDMA_FLAG_AH_VLAN_PR << OCRDMA_WQE_FLAGS_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05301942}
1943
1944static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
1945 struct ocrdma_sge *sge, int num_sge,
1946 struct ib_sge *sg_list)
1947{
1948 int i;
1949
1950 for (i = 0; i < num_sge; i++) {
1951 sge[i].lrkey = sg_list[i].lkey;
1952 sge[i].addr_lo = sg_list[i].addr;
1953 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
1954 sge[i].len = sg_list[i].length;
1955 hdr->total_len += sg_list[i].length;
1956 }
1957 if (num_sge == 0)
1958 memset(sge, 0, sizeof(*sge));
1959}
1960
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301961static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
1962{
1963 uint32_t total_len = 0, i;
1964
1965 for (i = 0; i < num_sge; i++)
1966 total_len += sg_list[i].length;
1967 return total_len;
1968}
1969
1970
Parav Panditfe2caef2012-03-21 04:09:06 +05301971static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
1972 struct ocrdma_hdr_wqe *hdr,
1973 struct ocrdma_sge *sge,
1974 struct ib_send_wr *wr, u32 wqe_size)
1975{
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301976 int i;
1977 char *dpp_addr;
1978
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301979 if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301980 hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
1981 if (unlikely(hdr->total_len > qp->max_inline_data)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001982 pr_err("%s() supported_len=0x%x,\n"
Masanari Iida1a84db52014-08-29 23:37:33 +09001983 " unsupported len req=0x%x\n", __func__,
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301984 qp->max_inline_data, hdr->total_len);
Parav Panditfe2caef2012-03-21 04:09:06 +05301985 return -EINVAL;
1986 }
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301987 dpp_addr = (char *)sge;
1988 for (i = 0; i < wr->num_sge; i++) {
1989 memcpy(dpp_addr,
1990 (void *)(unsigned long)wr->sg_list[i].addr,
1991 wr->sg_list[i].length);
1992 dpp_addr += wr->sg_list[i].length;
1993 }
1994
Parav Panditfe2caef2012-03-21 04:09:06 +05301995 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301996 if (0 == hdr->total_len)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301997 wqe_size += sizeof(struct ocrdma_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301998 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
1999 } else {
2000 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2001 if (wr->num_sge)
2002 wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
2003 else
2004 wqe_size += sizeof(struct ocrdma_sge);
2005 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2006 }
2007 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2008 return 0;
2009}
2010
2011static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2012 struct ib_send_wr *wr)
2013{
2014 int status;
2015 struct ocrdma_sge *sge;
2016 u32 wqe_size = sizeof(*hdr);
2017
2018 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2019 ocrdma_build_ud_hdr(qp, hdr, wr);
2020 sge = (struct ocrdma_sge *)(hdr + 2);
2021 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302022 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302023 sge = (struct ocrdma_sge *)(hdr + 1);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302024 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302025
2026 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2027 return status;
2028}
2029
2030static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2031 struct ib_send_wr *wr)
2032{
2033 int status;
2034 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2035 struct ocrdma_sge *sge = ext_rw + 1;
2036 u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
2037
2038 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2039 if (status)
2040 return status;
2041 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
2042 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
2043 ext_rw->lrkey = wr->wr.rdma.rkey;
2044 ext_rw->len = hdr->total_len;
2045 return 0;
2046}
2047
2048static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2049 struct ib_send_wr *wr)
2050{
2051 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2052 struct ocrdma_sge *sge = ext_rw + 1;
2053 u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
2054 sizeof(struct ocrdma_hdr_wqe);
2055
2056 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2057 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2058 hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
2059 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2060
2061 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
2062 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
2063 ext_rw->lrkey = wr->wr.rdma.rkey;
2064 ext_rw->len = hdr->total_len;
2065}
2066
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302067static void build_frmr_pbes(struct ib_send_wr *wr, struct ocrdma_pbl *pbl_tbl,
2068 struct ocrdma_hw_mr *hwmr)
2069{
2070 int i;
2071 u64 buf_addr = 0;
2072 int num_pbes;
2073 struct ocrdma_pbe *pbe;
2074
2075 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2076 num_pbes = 0;
2077
2078 /* go through the OS phy regions & fill hw pbe entries into pbls. */
2079 for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
2080 /* number of pbes can be more for one OS buf, when
2081 * buffers are of different sizes.
2082 * split the ib_buf to one or more pbes.
2083 */
2084 buf_addr = wr->wr.fast_reg.page_list->page_list[i];
2085 pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
2086 pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
2087 num_pbes += 1;
2088 pbe++;
2089
2090 /* if the pbl is full storing the pbes,
2091 * move to next pbl.
2092 */
2093 if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
2094 pbl_tbl++;
2095 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2096 }
2097 }
2098 return;
2099}
2100
2101static int get_encoded_page_size(int pg_sz)
2102{
2103 /* Max size is 256M 4096 << 16 */
2104 int i = 0;
2105 for (; i < 17; i++)
2106 if (pg_sz == (4096 << i))
2107 break;
2108 return i;
2109}
2110
2111
2112static int ocrdma_build_fr(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2113 struct ib_send_wr *wr)
2114{
2115 u64 fbo;
2116 struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
2117 struct ocrdma_mr *mr;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302118 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302119 u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
2120
2121 wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
2122
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302123 if (wr->wr.fast_reg.page_list_len > dev->attr.max_pages_per_frmr)
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302124 return -EINVAL;
2125
2126 hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
2127 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2128
2129 if (wr->wr.fast_reg.page_list_len == 0)
2130 BUG();
2131 if (wr->wr.fast_reg.access_flags & IB_ACCESS_LOCAL_WRITE)
2132 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
2133 if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_WRITE)
2134 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
2135 if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_READ)
2136 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
2137 hdr->lkey = wr->wr.fast_reg.rkey;
2138 hdr->total_len = wr->wr.fast_reg.length;
2139
2140 fbo = wr->wr.fast_reg.iova_start -
2141 (wr->wr.fast_reg.page_list->page_list[0] & PAGE_MASK);
2142
2143 fast_reg->va_hi = upper_32_bits(wr->wr.fast_reg.iova_start);
2144 fast_reg->va_lo = (u32) (wr->wr.fast_reg.iova_start & 0xffffffff);
2145 fast_reg->fbo_hi = upper_32_bits(fbo);
2146 fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
2147 fast_reg->num_sges = wr->wr.fast_reg.page_list_len;
2148 fast_reg->size_sge =
2149 get_encoded_page_size(1 << wr->wr.fast_reg.page_shift);
Roland Dreier7a1e89d2014-03-17 23:14:17 -07002150 mr = (struct ocrdma_mr *) (unsigned long)
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302151 dev->stag_arr[(hdr->lkey >> 8) & (OCRDMA_MAX_STAG - 1)];
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302152 build_frmr_pbes(wr, mr->hwmr.pbl_table, &mr->hwmr);
2153 return 0;
2154}
2155
Parav Panditfe2caef2012-03-21 04:09:06 +05302156static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
2157{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302158 u32 val = qp->sq.dbid | (1 << OCRDMA_DB_SQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302159
2160 iowrite32(val, qp->sq_db);
2161}
2162
2163int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
2164 struct ib_send_wr **bad_wr)
2165{
2166 int status = 0;
2167 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2168 struct ocrdma_hdr_wqe *hdr;
2169 unsigned long flags;
2170
2171 spin_lock_irqsave(&qp->q_lock, flags);
2172 if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
2173 spin_unlock_irqrestore(&qp->q_lock, flags);
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002174 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302175 return -EINVAL;
2176 }
2177
2178 while (wr) {
Mitesh Ahujaf252b5d2014-06-10 19:32:20 +05302179 if (qp->qp_type == IB_QPT_UD &&
2180 (wr->opcode != IB_WR_SEND &&
2181 wr->opcode != IB_WR_SEND_WITH_IMM)) {
2182 *bad_wr = wr;
2183 status = -EINVAL;
2184 break;
2185 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302186 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
2187 wr->num_sge > qp->sq.max_sges) {
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002188 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302189 status = -ENOMEM;
2190 break;
2191 }
2192 hdr = ocrdma_hwq_head(&qp->sq);
2193 hdr->cw = 0;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302194 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302195 hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2196 if (wr->send_flags & IB_SEND_FENCE)
2197 hdr->cw |=
2198 (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
2199 if (wr->send_flags & IB_SEND_SOLICITED)
2200 hdr->cw |=
2201 (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
2202 hdr->total_len = 0;
2203 switch (wr->opcode) {
2204 case IB_WR_SEND_WITH_IMM:
2205 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2206 hdr->immdt = ntohl(wr->ex.imm_data);
2207 case IB_WR_SEND:
2208 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2209 ocrdma_build_send(qp, hdr, wr);
2210 break;
2211 case IB_WR_SEND_WITH_INV:
2212 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
2213 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2214 hdr->lkey = wr->ex.invalidate_rkey;
2215 status = ocrdma_build_send(qp, hdr, wr);
2216 break;
2217 case IB_WR_RDMA_WRITE_WITH_IMM:
2218 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2219 hdr->immdt = ntohl(wr->ex.imm_data);
2220 case IB_WR_RDMA_WRITE:
2221 hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
2222 status = ocrdma_build_write(qp, hdr, wr);
2223 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302224 case IB_WR_RDMA_READ:
2225 ocrdma_build_read(qp, hdr, wr);
2226 break;
2227 case IB_WR_LOCAL_INV:
2228 hdr->cw |=
2229 (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302230 hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
2231 sizeof(struct ocrdma_sge)) /
Parav Panditfe2caef2012-03-21 04:09:06 +05302232 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
2233 hdr->lkey = wr->ex.invalidate_rkey;
2234 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302235 case IB_WR_FAST_REG_MR:
2236 status = ocrdma_build_fr(qp, hdr, wr);
2237 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302238 default:
2239 status = -EINVAL;
2240 break;
2241 }
2242 if (status) {
2243 *bad_wr = wr;
2244 break;
2245 }
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302246 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302247 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
2248 else
2249 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
2250 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
2251 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
2252 OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
2253 /* make sure wqe is written before adapter can access it */
2254 wmb();
2255 /* inform hw to start processing it */
2256 ocrdma_ring_sq_db(qp);
2257
2258 /* update pointer, counter for next wr */
2259 ocrdma_hwq_inc_head(&qp->sq);
2260 wr = wr->next;
2261 }
2262 spin_unlock_irqrestore(&qp->q_lock, flags);
2263 return status;
2264}
2265
2266static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
2267{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302268 u32 val = qp->rq.dbid | (1 << OCRDMA_DB_RQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302269
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302270 iowrite32(val, qp->rq_db);
Parav Panditfe2caef2012-03-21 04:09:06 +05302271}
2272
2273static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
2274 u16 tag)
2275{
2276 u32 wqe_size = 0;
2277 struct ocrdma_sge *sge;
2278 if (wr->num_sge)
2279 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
2280 else
2281 wqe_size = sizeof(*sge) + sizeof(*rqe);
2282
2283 rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
2284 OCRDMA_WQE_SIZE_SHIFT);
2285 rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2286 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2287 rqe->total_len = 0;
2288 rqe->rsvd_tag = tag;
2289 sge = (struct ocrdma_sge *)(rqe + 1);
2290 ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
2291 ocrdma_cpu_to_le32(rqe, wqe_size);
2292}
2293
2294int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2295 struct ib_recv_wr **bad_wr)
2296{
2297 int status = 0;
2298 unsigned long flags;
2299 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2300 struct ocrdma_hdr_wqe *rqe;
2301
2302 spin_lock_irqsave(&qp->q_lock, flags);
2303 if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
2304 spin_unlock_irqrestore(&qp->q_lock, flags);
2305 *bad_wr = wr;
2306 return -EINVAL;
2307 }
2308 while (wr) {
2309 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
2310 wr->num_sge > qp->rq.max_sges) {
2311 *bad_wr = wr;
2312 status = -ENOMEM;
2313 break;
2314 }
2315 rqe = ocrdma_hwq_head(&qp->rq);
2316 ocrdma_build_rqe(rqe, wr, 0);
2317
2318 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
2319 /* make sure rqe is written before adapter can access it */
2320 wmb();
2321
2322 /* inform hw to start processing it */
2323 ocrdma_ring_rq_db(qp);
2324
2325 /* update pointer, counter for next wr */
2326 ocrdma_hwq_inc_head(&qp->rq);
2327 wr = wr->next;
2328 }
2329 spin_unlock_irqrestore(&qp->q_lock, flags);
2330 return status;
2331}
2332
2333/* cqe for srq's rqe can potentially arrive out of order.
2334 * index gives the entry in the shadow table where to store
2335 * the wr_id. tag/index is returned in cqe to reference back
2336 * for a given rqe.
2337 */
2338static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
2339{
2340 int row = 0;
2341 int indx = 0;
2342
2343 for (row = 0; row < srq->bit_fields_len; row++) {
2344 if (srq->idx_bit_fields[row]) {
2345 indx = ffs(srq->idx_bit_fields[row]);
2346 indx = (row * 32) + (indx - 1);
2347 if (indx >= srq->rq.max_cnt)
2348 BUG();
2349 ocrdma_srq_toggle_bit(srq, indx);
2350 break;
2351 }
2352 }
2353
2354 if (row == srq->bit_fields_len)
2355 BUG();
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302356 return indx + 1; /* Use from index 1 */
Parav Panditfe2caef2012-03-21 04:09:06 +05302357}
2358
2359static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
2360{
2361 u32 val = srq->rq.dbid | (1 << 16);
2362
2363 iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
2364}
2365
2366int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
2367 struct ib_recv_wr **bad_wr)
2368{
2369 int status = 0;
2370 unsigned long flags;
2371 struct ocrdma_srq *srq;
2372 struct ocrdma_hdr_wqe *rqe;
2373 u16 tag;
2374
2375 srq = get_ocrdma_srq(ibsrq);
2376
2377 spin_lock_irqsave(&srq->q_lock, flags);
2378 while (wr) {
2379 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
2380 wr->num_sge > srq->rq.max_sges) {
2381 status = -ENOMEM;
2382 *bad_wr = wr;
2383 break;
2384 }
2385 tag = ocrdma_srq_get_idx(srq);
2386 rqe = ocrdma_hwq_head(&srq->rq);
2387 ocrdma_build_rqe(rqe, wr, tag);
2388
2389 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
2390 /* make sure rqe is written before adapter can perform DMA */
2391 wmb();
2392 /* inform hw to start processing it */
2393 ocrdma_ring_srq_db(srq);
2394 /* update pointer, counter for next wr */
2395 ocrdma_hwq_inc_head(&srq->rq);
2396 wr = wr->next;
2397 }
2398 spin_unlock_irqrestore(&srq->q_lock, flags);
2399 return status;
2400}
2401
2402static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
2403{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302404 enum ib_wc_status ibwc_status;
Parav Panditfe2caef2012-03-21 04:09:06 +05302405
2406 switch (status) {
2407 case OCRDMA_CQE_GENERAL_ERR:
2408 ibwc_status = IB_WC_GENERAL_ERR;
2409 break;
2410 case OCRDMA_CQE_LOC_LEN_ERR:
2411 ibwc_status = IB_WC_LOC_LEN_ERR;
2412 break;
2413 case OCRDMA_CQE_LOC_QP_OP_ERR:
2414 ibwc_status = IB_WC_LOC_QP_OP_ERR;
2415 break;
2416 case OCRDMA_CQE_LOC_EEC_OP_ERR:
2417 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
2418 break;
2419 case OCRDMA_CQE_LOC_PROT_ERR:
2420 ibwc_status = IB_WC_LOC_PROT_ERR;
2421 break;
2422 case OCRDMA_CQE_WR_FLUSH_ERR:
2423 ibwc_status = IB_WC_WR_FLUSH_ERR;
2424 break;
2425 case OCRDMA_CQE_MW_BIND_ERR:
2426 ibwc_status = IB_WC_MW_BIND_ERR;
2427 break;
2428 case OCRDMA_CQE_BAD_RESP_ERR:
2429 ibwc_status = IB_WC_BAD_RESP_ERR;
2430 break;
2431 case OCRDMA_CQE_LOC_ACCESS_ERR:
2432 ibwc_status = IB_WC_LOC_ACCESS_ERR;
2433 break;
2434 case OCRDMA_CQE_REM_INV_REQ_ERR:
2435 ibwc_status = IB_WC_REM_INV_REQ_ERR;
2436 break;
2437 case OCRDMA_CQE_REM_ACCESS_ERR:
2438 ibwc_status = IB_WC_REM_ACCESS_ERR;
2439 break;
2440 case OCRDMA_CQE_REM_OP_ERR:
2441 ibwc_status = IB_WC_REM_OP_ERR;
2442 break;
2443 case OCRDMA_CQE_RETRY_EXC_ERR:
2444 ibwc_status = IB_WC_RETRY_EXC_ERR;
2445 break;
2446 case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2447 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2448 break;
2449 case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2450 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2451 break;
2452 case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2453 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2454 break;
2455 case OCRDMA_CQE_REM_ABORT_ERR:
2456 ibwc_status = IB_WC_REM_ABORT_ERR;
2457 break;
2458 case OCRDMA_CQE_INV_EECN_ERR:
2459 ibwc_status = IB_WC_INV_EECN_ERR;
2460 break;
2461 case OCRDMA_CQE_INV_EEC_STATE_ERR:
2462 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2463 break;
2464 case OCRDMA_CQE_FATAL_ERR:
2465 ibwc_status = IB_WC_FATAL_ERR;
2466 break;
2467 case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2468 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2469 break;
2470 default:
2471 ibwc_status = IB_WC_GENERAL_ERR;
2472 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002473 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302474 return ibwc_status;
2475}
2476
2477static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2478 u32 wqe_idx)
2479{
2480 struct ocrdma_hdr_wqe *hdr;
2481 struct ocrdma_sge *rw;
2482 int opcode;
2483
2484 hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2485
2486 ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2487 /* Undo the hdr->cw swap */
2488 opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2489 switch (opcode) {
2490 case OCRDMA_WRITE:
2491 ibwc->opcode = IB_WC_RDMA_WRITE;
2492 break;
2493 case OCRDMA_READ:
2494 rw = (struct ocrdma_sge *)(hdr + 1);
2495 ibwc->opcode = IB_WC_RDMA_READ;
2496 ibwc->byte_len = rw->len;
2497 break;
2498 case OCRDMA_SEND:
2499 ibwc->opcode = IB_WC_SEND;
2500 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302501 case OCRDMA_FR_MR:
2502 ibwc->opcode = IB_WC_FAST_REG_MR;
2503 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302504 case OCRDMA_LKEY_INV:
2505 ibwc->opcode = IB_WC_LOCAL_INV;
2506 break;
2507 default:
2508 ibwc->status = IB_WC_GENERAL_ERR;
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002509 pr_err("%s() invalid opcode received = 0x%x\n",
2510 __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
Parav Panditfe2caef2012-03-21 04:09:06 +05302511 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002512 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302513}
2514
2515static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2516 struct ocrdma_cqe *cqe)
2517{
2518 if (is_cqe_for_sq(cqe)) {
2519 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2520 cqe->flags_status_srcqpn) &
2521 ~OCRDMA_CQE_STATUS_MASK);
2522 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2523 cqe->flags_status_srcqpn) |
2524 (OCRDMA_CQE_WR_FLUSH_ERR <<
2525 OCRDMA_CQE_STATUS_SHIFT));
2526 } else {
2527 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2528 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2529 cqe->flags_status_srcqpn) &
2530 ~OCRDMA_CQE_UD_STATUS_MASK);
2531 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2532 cqe->flags_status_srcqpn) |
2533 (OCRDMA_CQE_WR_FLUSH_ERR <<
2534 OCRDMA_CQE_UD_STATUS_SHIFT));
2535 } else {
2536 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2537 cqe->flags_status_srcqpn) &
2538 ~OCRDMA_CQE_STATUS_MASK);
2539 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2540 cqe->flags_status_srcqpn) |
2541 (OCRDMA_CQE_WR_FLUSH_ERR <<
2542 OCRDMA_CQE_STATUS_SHIFT));
2543 }
2544 }
2545}
2546
2547static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2548 struct ocrdma_qp *qp, int status)
2549{
2550 bool expand = false;
2551
2552 ibwc->byte_len = 0;
2553 ibwc->qp = &qp->ibqp;
2554 ibwc->status = ocrdma_to_ibwc_err(status);
2555
2556 ocrdma_flush_qp(qp);
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05302557 ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05302558
2559 /* if wqe/rqe pending for which cqe needs to be returned,
2560 * trigger inflating it.
2561 */
2562 if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2563 expand = true;
2564 ocrdma_set_cqe_status_flushed(qp, cqe);
2565 }
2566 return expand;
2567}
2568
2569static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2570 struct ocrdma_qp *qp, int status)
2571{
2572 ibwc->opcode = IB_WC_RECV;
2573 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2574 ocrdma_hwq_inc_tail(&qp->rq);
2575
2576 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2577}
2578
2579static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2580 struct ocrdma_qp *qp, int status)
2581{
2582 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2583 ocrdma_hwq_inc_tail(&qp->sq);
2584
2585 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2586}
2587
2588
2589static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2590 struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2591 bool *polled, bool *stop)
2592{
2593 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302594 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302595 int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2596 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302597 if (status < OCRDMA_MAX_CQE_ERR)
2598 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302599
2600 /* when hw sq is empty, but rq is not empty, so we continue
2601 * to keep the cqe in order to get the cq event again.
2602 */
2603 if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2604 /* when cq for rq and sq is same, it is safe to return
2605 * flush cqe for RQEs.
2606 */
2607 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2608 *polled = true;
2609 status = OCRDMA_CQE_WR_FLUSH_ERR;
2610 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2611 } else {
2612 /* stop processing further cqe as this cqe is used for
2613 * triggering cq event on buddy cq of RQ.
2614 * When QP is destroyed, this cqe will be removed
2615 * from the cq's hardware q.
2616 */
2617 *polled = false;
2618 *stop = true;
2619 expand = false;
2620 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302621 } else if (is_hw_sq_empty(qp)) {
2622 /* Do nothing */
2623 expand = false;
2624 *polled = false;
2625 *stop = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302626 } else {
2627 *polled = true;
2628 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2629 }
2630 return expand;
2631}
2632
2633static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2634 struct ocrdma_cqe *cqe,
2635 struct ib_wc *ibwc, bool *polled)
2636{
2637 bool expand = false;
2638 int tail = qp->sq.tail;
2639 u32 wqe_idx;
2640
2641 if (!qp->wqe_wr_id_tbl[tail].signaled) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302642 *polled = false; /* WC cannot be consumed yet */
2643 } else {
2644 ibwc->status = IB_WC_SUCCESS;
2645 ibwc->wc_flags = 0;
2646 ibwc->qp = &qp->ibqp;
2647 ocrdma_update_wc(qp, ibwc, tail);
2648 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302649 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302650 wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
2651 OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
Parav Panditae3bca92012-08-17 14:45:33 +00002652 if (tail != wqe_idx)
2653 expand = true; /* Coalesced CQE can't be consumed yet */
2654
Parav Panditfe2caef2012-03-21 04:09:06 +05302655 ocrdma_hwq_inc_tail(&qp->sq);
2656 return expand;
2657}
2658
2659static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2660 struct ib_wc *ibwc, bool *polled, bool *stop)
2661{
2662 int status;
2663 bool expand;
2664
2665 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2666 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2667
2668 if (status == OCRDMA_CQE_SUCCESS)
2669 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2670 else
2671 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2672 return expand;
2673}
2674
2675static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
2676{
2677 int status;
2678
2679 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2680 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2681 ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2682 OCRDMA_CQE_SRCQP_MASK;
2683 ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
2684 OCRDMA_CQE_PKEY_MASK;
2685 ibwc->wc_flags = IB_WC_GRH;
2686 ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2687 OCRDMA_CQE_UD_XFER_LEN_SHIFT);
2688 return status;
2689}
2690
2691static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2692 struct ocrdma_cqe *cqe,
2693 struct ocrdma_qp *qp)
2694{
2695 unsigned long flags;
2696 struct ocrdma_srq *srq;
2697 u32 wqe_idx;
2698
2699 srq = get_ocrdma_srq(qp->ibqp.srq);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302700 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302701 OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
2702 if (wqe_idx < 1)
2703 BUG();
2704
Parav Panditfe2caef2012-03-21 04:09:06 +05302705 ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2706 spin_lock_irqsave(&srq->q_lock, flags);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302707 ocrdma_srq_toggle_bit(srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05302708 spin_unlock_irqrestore(&srq->q_lock, flags);
2709 ocrdma_hwq_inc_tail(&srq->rq);
2710}
2711
2712static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2713 struct ib_wc *ibwc, bool *polled, bool *stop,
2714 int status)
2715{
2716 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302717 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2718
2719 if (status < OCRDMA_MAX_CQE_ERR)
2720 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302721
2722 /* when hw_rq is empty, but wq is not empty, so continue
2723 * to keep the cqe to get the cq event again.
2724 */
2725 if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2726 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2727 *polled = true;
2728 status = OCRDMA_CQE_WR_FLUSH_ERR;
2729 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2730 } else {
2731 *polled = false;
2732 *stop = true;
2733 expand = false;
2734 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302735 } else if (is_hw_rq_empty(qp)) {
2736 /* Do nothing */
2737 expand = false;
2738 *polled = false;
2739 *stop = false;
Parav Pandita3698a92012-06-11 16:39:20 +05302740 } else {
2741 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302742 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
Parav Pandita3698a92012-06-11 16:39:20 +05302743 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302744 return expand;
2745}
2746
2747static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2748 struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2749{
2750 ibwc->opcode = IB_WC_RECV;
2751 ibwc->qp = &qp->ibqp;
2752 ibwc->status = IB_WC_SUCCESS;
2753
2754 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
2755 ocrdma_update_ud_rcqe(ibwc, cqe);
2756 else
2757 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2758
2759 if (is_cqe_imm(cqe)) {
2760 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2761 ibwc->wc_flags |= IB_WC_WITH_IMM;
2762 } else if (is_cqe_wr_imm(cqe)) {
2763 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2764 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2765 ibwc->wc_flags |= IB_WC_WITH_IMM;
2766 } else if (is_cqe_invalidated(cqe)) {
2767 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2768 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2769 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302770 if (qp->ibqp.srq) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302771 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302772 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302773 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2774 ocrdma_hwq_inc_tail(&qp->rq);
2775 }
2776}
2777
2778static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2779 struct ib_wc *ibwc, bool *polled, bool *stop)
2780{
2781 int status;
2782 bool expand = false;
2783
2784 ibwc->wc_flags = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302785 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302786 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2787 OCRDMA_CQE_UD_STATUS_MASK) >>
2788 OCRDMA_CQE_UD_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302789 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302790 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2791 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302792 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302793
2794 if (status == OCRDMA_CQE_SUCCESS) {
2795 *polled = true;
2796 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2797 } else {
2798 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2799 status);
2800 }
2801 return expand;
2802}
2803
2804static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2805 u16 cur_getp)
2806{
2807 if (cq->phase_change) {
2808 if (cur_getp == 0)
2809 cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302810 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302811 /* clear valid bit */
2812 cqe->flags_status_srcqpn = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302813 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302814}
2815
2816static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2817 struct ib_wc *ibwc)
2818{
2819 u16 qpn = 0;
2820 int i = 0;
2821 bool expand = false;
2822 int polled_hw_cqes = 0;
2823 struct ocrdma_qp *qp = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302824 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302825 struct ocrdma_cqe *cqe;
2826 u16 cur_getp; bool polled = false; bool stop = false;
2827
2828 cur_getp = cq->getp;
2829 while (num_entries) {
2830 cqe = cq->va + cur_getp;
2831 /* check whether valid cqe or not */
2832 if (!is_cqe_valid(cq, cqe))
2833 break;
2834 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2835 /* ignore discarded cqe */
2836 if (qpn == 0)
2837 goto skip_cqe;
2838 qp = dev->qp_tbl[qpn];
2839 BUG_ON(qp == NULL);
2840
2841 if (is_cqe_for_sq(cqe)) {
2842 expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2843 &stop);
2844 } else {
2845 expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2846 &stop);
2847 }
2848 if (expand)
2849 goto expand_cqe;
2850 if (stop)
2851 goto stop_cqe;
2852 /* clear qpn to avoid duplicate processing by discard_cqe() */
2853 cqe->cmn.qpn = 0;
2854skip_cqe:
2855 polled_hw_cqes += 1;
2856 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2857 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2858expand_cqe:
2859 if (polled) {
2860 num_entries -= 1;
2861 i += 1;
2862 ibwc = ibwc + 1;
2863 polled = false;
2864 }
2865 }
2866stop_cqe:
2867 cq->getp = cur_getp;
Devesh Sharmaea617622014-02-04 11:56:54 +05302868 if (cq->deferred_arm) {
2869 ocrdma_ring_cq_db(dev, cq->id, true, cq->deferred_sol,
Parav Panditfe2caef2012-03-21 04:09:06 +05302870 polled_hw_cqes);
Devesh Sharmaea617622014-02-04 11:56:54 +05302871 cq->deferred_arm = false;
2872 cq->deferred_sol = false;
2873 } else {
2874 /* We need to pop the CQE. No need to arm */
2875 ocrdma_ring_cq_db(dev, cq->id, false, cq->deferred_sol,
2876 polled_hw_cqes);
2877 cq->deferred_sol = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302878 }
Devesh Sharmaea617622014-02-04 11:56:54 +05302879
Parav Panditfe2caef2012-03-21 04:09:06 +05302880 return i;
2881}
2882
2883/* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2884static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2885 struct ocrdma_qp *qp, struct ib_wc *ibwc)
2886{
2887 int err_cqes = 0;
2888
2889 while (num_entries) {
2890 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2891 break;
2892 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2893 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2894 ocrdma_hwq_inc_tail(&qp->sq);
2895 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2896 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2897 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302898 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302899 return err_cqes;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302900 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302901 ibwc->byte_len = 0;
2902 ibwc->status = IB_WC_WR_FLUSH_ERR;
2903 ibwc = ibwc + 1;
2904 err_cqes += 1;
2905 num_entries -= 1;
2906 }
2907 return err_cqes;
2908}
2909
2910int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2911{
2912 int cqes_to_poll = num_entries;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302913 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2914 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302915 int num_os_cqe = 0, err_cqes = 0;
2916 struct ocrdma_qp *qp;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302917 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05302918
2919 /* poll cqes from adapter CQ */
2920 spin_lock_irqsave(&cq->cq_lock, flags);
2921 num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2922 spin_unlock_irqrestore(&cq->cq_lock, flags);
2923 cqes_to_poll -= num_os_cqe;
2924
2925 if (cqes_to_poll) {
2926 wc = wc + num_os_cqe;
2927 /* adapter returns single error cqe when qp moves to
2928 * error state. So insert error cqes with wc_status as
2929 * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2930 * respectively which uses this CQ.
2931 */
2932 spin_lock_irqsave(&dev->flush_q_lock, flags);
2933 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2934 if (cqes_to_poll == 0)
2935 break;
2936 err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2937 cqes_to_poll -= err_cqes;
2938 num_os_cqe += err_cqes;
2939 wc = wc + err_cqes;
2940 }
2941 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2942 }
2943 return num_os_cqe;
2944}
2945
2946int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2947{
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302948 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2949 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302950 u16 cq_id;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302951 unsigned long flags;
Devesh Sharmaea617622014-02-04 11:56:54 +05302952 bool arm_needed = false, sol_needed = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302953
Parav Panditfe2caef2012-03-21 04:09:06 +05302954 cq_id = cq->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05302955
2956 spin_lock_irqsave(&cq->cq_lock, flags);
2957 if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302958 arm_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302959 if (cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302960 sol_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302961
Devesh Sharmaea617622014-02-04 11:56:54 +05302962 if (cq->first_arm) {
2963 ocrdma_ring_cq_db(dev, cq_id, arm_needed, sol_needed, 0);
2964 cq->first_arm = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302965 }
Devesh Sharmaea617622014-02-04 11:56:54 +05302966
Devesh Sharmaf93439e2014-06-09 10:52:38 +05302967 cq->deferred_arm = true;
Devesh Sharmaea617622014-02-04 11:56:54 +05302968 cq->deferred_sol = sol_needed;
Parav Panditfe2caef2012-03-21 04:09:06 +05302969 spin_unlock_irqrestore(&cq->cq_lock, flags);
Devesh Sharmaea617622014-02-04 11:56:54 +05302970
Parav Panditfe2caef2012-03-21 04:09:06 +05302971 return 0;
2972}
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302973
2974struct ib_mr *ocrdma_alloc_frmr(struct ib_pd *ibpd, int max_page_list_len)
2975{
2976 int status;
2977 struct ocrdma_mr *mr;
2978 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
2979 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
2980
2981 if (max_page_list_len > dev->attr.max_pages_per_frmr)
2982 return ERR_PTR(-EINVAL);
2983
2984 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2985 if (!mr)
2986 return ERR_PTR(-ENOMEM);
2987
2988 status = ocrdma_get_pbl_info(dev, mr, max_page_list_len);
2989 if (status)
2990 goto pbl_err;
2991 mr->hwmr.fr_mr = 1;
2992 mr->hwmr.remote_rd = 0;
2993 mr->hwmr.remote_wr = 0;
2994 mr->hwmr.local_rd = 0;
2995 mr->hwmr.local_wr = 0;
2996 mr->hwmr.mw_bind = 0;
2997 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
2998 if (status)
2999 goto pbl_err;
3000 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
3001 if (status)
3002 goto mbx_err;
3003 mr->ibmr.rkey = mr->hwmr.lkey;
3004 mr->ibmr.lkey = mr->hwmr.lkey;
Roland Dreier7a1e89d2014-03-17 23:14:17 -07003005 dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] =
3006 (unsigned long) mr;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303007 return &mr->ibmr;
3008mbx_err:
3009 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3010pbl_err:
3011 kfree(mr);
3012 return ERR_PTR(-ENOMEM);
3013}
3014
3015struct ib_fast_reg_page_list *ocrdma_alloc_frmr_page_list(struct ib_device
3016 *ibdev,
3017 int page_list_len)
3018{
3019 struct ib_fast_reg_page_list *frmr_list;
3020 int size;
3021
3022 size = sizeof(*frmr_list) + (page_list_len * sizeof(u64));
3023 frmr_list = kzalloc(size, GFP_KERNEL);
3024 if (!frmr_list)
3025 return ERR_PTR(-ENOMEM);
3026 frmr_list->page_list = (u64 *)(frmr_list + 1);
3027 return frmr_list;
3028}
3029
3030void ocrdma_free_frmr_page_list(struct ib_fast_reg_page_list *page_list)
3031{
3032 kfree(page_list);
3033}
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05303034
3035#define MAX_KERNEL_PBE_SIZE 65536
3036static inline int count_kernel_pbes(struct ib_phys_buf *buf_list,
3037 int buf_cnt, u32 *pbe_size)
3038{
3039 u64 total_size = 0;
3040 u64 buf_size = 0;
3041 int i;
3042 *pbe_size = roundup(buf_list[0].size, PAGE_SIZE);
3043 *pbe_size = roundup_pow_of_two(*pbe_size);
3044
3045 /* find the smallest PBE size that we can have */
3046 for (i = 0; i < buf_cnt; i++) {
3047 /* first addr may not be page aligned, so ignore checking */
3048 if ((i != 0) && ((buf_list[i].addr & ~PAGE_MASK) ||
3049 (buf_list[i].size & ~PAGE_MASK))) {
3050 return 0;
3051 }
3052
3053 /* if configured PBE size is greater then the chosen one,
3054 * reduce the PBE size.
3055 */
3056 buf_size = roundup(buf_list[i].size, PAGE_SIZE);
3057 /* pbe_size has to be even multiple of 4K 1,2,4,8...*/
3058 buf_size = roundup_pow_of_two(buf_size);
3059 if (*pbe_size > buf_size)
3060 *pbe_size = buf_size;
3061
3062 total_size += buf_size;
3063 }
3064 *pbe_size = *pbe_size > MAX_KERNEL_PBE_SIZE ?
3065 (MAX_KERNEL_PBE_SIZE) : (*pbe_size);
3066
3067 /* num_pbes = total_size / (*pbe_size); this is implemented below. */
3068
3069 return total_size >> ilog2(*pbe_size);
3070}
3071
3072static void build_kernel_pbes(struct ib_phys_buf *buf_list, int ib_buf_cnt,
3073 u32 pbe_size, struct ocrdma_pbl *pbl_tbl,
3074 struct ocrdma_hw_mr *hwmr)
3075{
3076 int i;
3077 int idx;
3078 int pbes_per_buf = 0;
3079 u64 buf_addr = 0;
3080 int num_pbes;
3081 struct ocrdma_pbe *pbe;
3082 int total_num_pbes = 0;
3083
3084 if (!hwmr->num_pbes)
3085 return;
3086
3087 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
3088 num_pbes = 0;
3089
3090 /* go through the OS phy regions & fill hw pbe entries into pbls. */
3091 for (i = 0; i < ib_buf_cnt; i++) {
3092 buf_addr = buf_list[i].addr;
3093 pbes_per_buf =
3094 roundup_pow_of_two(roundup(buf_list[i].size, PAGE_SIZE)) /
3095 pbe_size;
3096 hwmr->len += buf_list[i].size;
3097 /* number of pbes can be more for one OS buf, when
3098 * buffers are of different sizes.
3099 * split the ib_buf to one or more pbes.
3100 */
3101 for (idx = 0; idx < pbes_per_buf; idx++) {
3102 /* we program always page aligned addresses,
3103 * first unaligned address is taken care by fbo.
3104 */
3105 if (i == 0) {
3106 /* for non zero fbo, assign the
3107 * start of the page.
3108 */
3109 pbe->pa_lo =
3110 cpu_to_le32((u32) (buf_addr & PAGE_MASK));
3111 pbe->pa_hi =
3112 cpu_to_le32((u32) upper_32_bits(buf_addr));
3113 } else {
3114 pbe->pa_lo =
3115 cpu_to_le32((u32) (buf_addr & 0xffffffff));
3116 pbe->pa_hi =
3117 cpu_to_le32((u32) upper_32_bits(buf_addr));
3118 }
3119 buf_addr += pbe_size;
3120 num_pbes += 1;
3121 total_num_pbes += 1;
3122 pbe++;
3123
3124 if (total_num_pbes == hwmr->num_pbes)
3125 goto mr_tbl_done;
3126 /* if the pbl is full storing the pbes,
3127 * move to next pbl.
3128 */
3129 if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
3130 pbl_tbl++;
3131 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
3132 num_pbes = 0;
3133 }
3134 }
3135 }
3136mr_tbl_done:
3137 return;
3138}
3139
3140struct ib_mr *ocrdma_reg_kernel_mr(struct ib_pd *ibpd,
3141 struct ib_phys_buf *buf_list,
3142 int buf_cnt, int acc, u64 *iova_start)
3143{
3144 int status = -ENOMEM;
3145 struct ocrdma_mr *mr;
3146 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
3147 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
3148 u32 num_pbes;
3149 u32 pbe_size = 0;
3150
3151 if ((acc & IB_ACCESS_REMOTE_WRITE) && !(acc & IB_ACCESS_LOCAL_WRITE))
3152 return ERR_PTR(-EINVAL);
3153
3154 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3155 if (!mr)
3156 return ERR_PTR(status);
3157
3158 num_pbes = count_kernel_pbes(buf_list, buf_cnt, &pbe_size);
3159 if (num_pbes == 0) {
3160 status = -EINVAL;
3161 goto pbl_err;
3162 }
3163 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
3164 if (status)
3165 goto pbl_err;
3166
3167 mr->hwmr.pbe_size = pbe_size;
3168 mr->hwmr.fbo = *iova_start - (buf_list[0].addr & PAGE_MASK);
3169 mr->hwmr.va = *iova_start;
3170 mr->hwmr.local_rd = 1;
3171 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
3172 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
3173 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
3174 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
3175 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
3176
3177 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
3178 if (status)
3179 goto pbl_err;
3180 build_kernel_pbes(buf_list, buf_cnt, pbe_size, mr->hwmr.pbl_table,
3181 &mr->hwmr);
3182 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
3183 if (status)
3184 goto mbx_err;
3185
3186 mr->ibmr.lkey = mr->hwmr.lkey;
3187 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
3188 mr->ibmr.rkey = mr->hwmr.lkey;
3189 return &mr->ibmr;
3190
3191mbx_err:
3192 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3193pbl_err:
3194 kfree(mr);
3195 return ERR_PTR(status);
3196}