blob: 4d5f581af49a70543043abcc86f94423da0c87fa [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;
1940}
1941
1942static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
1943 struct ocrdma_sge *sge, int num_sge,
1944 struct ib_sge *sg_list)
1945{
1946 int i;
1947
1948 for (i = 0; i < num_sge; i++) {
1949 sge[i].lrkey = sg_list[i].lkey;
1950 sge[i].addr_lo = sg_list[i].addr;
1951 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
1952 sge[i].len = sg_list[i].length;
1953 hdr->total_len += sg_list[i].length;
1954 }
1955 if (num_sge == 0)
1956 memset(sge, 0, sizeof(*sge));
1957}
1958
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301959static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
1960{
1961 uint32_t total_len = 0, i;
1962
1963 for (i = 0; i < num_sge; i++)
1964 total_len += sg_list[i].length;
1965 return total_len;
1966}
1967
1968
Parav Panditfe2caef2012-03-21 04:09:06 +05301969static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
1970 struct ocrdma_hdr_wqe *hdr,
1971 struct ocrdma_sge *sge,
1972 struct ib_send_wr *wr, u32 wqe_size)
1973{
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301974 int i;
1975 char *dpp_addr;
1976
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301977 if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301978 hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
1979 if (unlikely(hdr->total_len > qp->max_inline_data)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001980 pr_err("%s() supported_len=0x%x,\n"
Masanari Iida1a84db52014-08-29 23:37:33 +09001981 " unsupported len req=0x%x\n", __func__,
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301982 qp->max_inline_data, hdr->total_len);
Parav Panditfe2caef2012-03-21 04:09:06 +05301983 return -EINVAL;
1984 }
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301985 dpp_addr = (char *)sge;
1986 for (i = 0; i < wr->num_sge; i++) {
1987 memcpy(dpp_addr,
1988 (void *)(unsigned long)wr->sg_list[i].addr,
1989 wr->sg_list[i].length);
1990 dpp_addr += wr->sg_list[i].length;
1991 }
1992
Parav Panditfe2caef2012-03-21 04:09:06 +05301993 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
Naresh Gottumukkala117e6dd2013-08-26 15:27:48 +05301994 if (0 == hdr->total_len)
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05301995 wqe_size += sizeof(struct ocrdma_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +05301996 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
1997 } else {
1998 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1999 if (wr->num_sge)
2000 wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
2001 else
2002 wqe_size += sizeof(struct ocrdma_sge);
2003 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2004 }
2005 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2006 return 0;
2007}
2008
2009static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2010 struct ib_send_wr *wr)
2011{
2012 int status;
2013 struct ocrdma_sge *sge;
2014 u32 wqe_size = sizeof(*hdr);
2015
2016 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2017 ocrdma_build_ud_hdr(qp, hdr, wr);
2018 sge = (struct ocrdma_sge *)(hdr + 2);
2019 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302020 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302021 sge = (struct ocrdma_sge *)(hdr + 1);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302022 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302023
2024 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2025 return status;
2026}
2027
2028static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2029 struct ib_send_wr *wr)
2030{
2031 int status;
2032 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2033 struct ocrdma_sge *sge = ext_rw + 1;
2034 u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
2035
2036 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
2037 if (status)
2038 return status;
2039 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
2040 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
2041 ext_rw->lrkey = wr->wr.rdma.rkey;
2042 ext_rw->len = hdr->total_len;
2043 return 0;
2044}
2045
2046static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2047 struct ib_send_wr *wr)
2048{
2049 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
2050 struct ocrdma_sge *sge = ext_rw + 1;
2051 u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
2052 sizeof(struct ocrdma_hdr_wqe);
2053
2054 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
2055 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2056 hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
2057 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2058
2059 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
2060 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
2061 ext_rw->lrkey = wr->wr.rdma.rkey;
2062 ext_rw->len = hdr->total_len;
2063}
2064
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302065static void build_frmr_pbes(struct ib_send_wr *wr, struct ocrdma_pbl *pbl_tbl,
2066 struct ocrdma_hw_mr *hwmr)
2067{
2068 int i;
2069 u64 buf_addr = 0;
2070 int num_pbes;
2071 struct ocrdma_pbe *pbe;
2072
2073 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2074 num_pbes = 0;
2075
2076 /* go through the OS phy regions & fill hw pbe entries into pbls. */
2077 for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
2078 /* number of pbes can be more for one OS buf, when
2079 * buffers are of different sizes.
2080 * split the ib_buf to one or more pbes.
2081 */
2082 buf_addr = wr->wr.fast_reg.page_list->page_list[i];
2083 pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
2084 pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
2085 num_pbes += 1;
2086 pbe++;
2087
2088 /* if the pbl is full storing the pbes,
2089 * move to next pbl.
2090 */
2091 if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
2092 pbl_tbl++;
2093 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2094 }
2095 }
2096 return;
2097}
2098
2099static int get_encoded_page_size(int pg_sz)
2100{
2101 /* Max size is 256M 4096 << 16 */
2102 int i = 0;
2103 for (; i < 17; i++)
2104 if (pg_sz == (4096 << i))
2105 break;
2106 return i;
2107}
2108
2109
2110static int ocrdma_build_fr(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2111 struct ib_send_wr *wr)
2112{
2113 u64 fbo;
2114 struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
2115 struct ocrdma_mr *mr;
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302116 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302117 u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
2118
2119 wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
2120
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302121 if (wr->wr.fast_reg.page_list_len > dev->attr.max_pages_per_frmr)
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302122 return -EINVAL;
2123
2124 hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
2125 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2126
2127 if (wr->wr.fast_reg.page_list_len == 0)
2128 BUG();
2129 if (wr->wr.fast_reg.access_flags & IB_ACCESS_LOCAL_WRITE)
2130 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
2131 if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_WRITE)
2132 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
2133 if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_READ)
2134 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
2135 hdr->lkey = wr->wr.fast_reg.rkey;
2136 hdr->total_len = wr->wr.fast_reg.length;
2137
2138 fbo = wr->wr.fast_reg.iova_start -
2139 (wr->wr.fast_reg.page_list->page_list[0] & PAGE_MASK);
2140
2141 fast_reg->va_hi = upper_32_bits(wr->wr.fast_reg.iova_start);
2142 fast_reg->va_lo = (u32) (wr->wr.fast_reg.iova_start & 0xffffffff);
2143 fast_reg->fbo_hi = upper_32_bits(fbo);
2144 fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
2145 fast_reg->num_sges = wr->wr.fast_reg.page_list_len;
2146 fast_reg->size_sge =
2147 get_encoded_page_size(1 << wr->wr.fast_reg.page_shift);
Roland Dreier7a1e89d2014-03-17 23:14:17 -07002148 mr = (struct ocrdma_mr *) (unsigned long)
Mitesh Ahujad2b8f7b2014-12-18 14:13:06 +05302149 dev->stag_arr[(hdr->lkey >> 8) & (OCRDMA_MAX_STAG - 1)];
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302150 build_frmr_pbes(wr, mr->hwmr.pbl_table, &mr->hwmr);
2151 return 0;
2152}
2153
Parav Panditfe2caef2012-03-21 04:09:06 +05302154static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
2155{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302156 u32 val = qp->sq.dbid | (1 << OCRDMA_DB_SQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302157
2158 iowrite32(val, qp->sq_db);
2159}
2160
2161int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
2162 struct ib_send_wr **bad_wr)
2163{
2164 int status = 0;
2165 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2166 struct ocrdma_hdr_wqe *hdr;
2167 unsigned long flags;
2168
2169 spin_lock_irqsave(&qp->q_lock, flags);
2170 if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
2171 spin_unlock_irqrestore(&qp->q_lock, flags);
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002172 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302173 return -EINVAL;
2174 }
2175
2176 while (wr) {
Mitesh Ahujaf252b5d2014-06-10 19:32:20 +05302177 if (qp->qp_type == IB_QPT_UD &&
2178 (wr->opcode != IB_WR_SEND &&
2179 wr->opcode != IB_WR_SEND_WITH_IMM)) {
2180 *bad_wr = wr;
2181 status = -EINVAL;
2182 break;
2183 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302184 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
2185 wr->num_sge > qp->sq.max_sges) {
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00002186 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05302187 status = -ENOMEM;
2188 break;
2189 }
2190 hdr = ocrdma_hwq_head(&qp->sq);
2191 hdr->cw = 0;
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302192 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302193 hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2194 if (wr->send_flags & IB_SEND_FENCE)
2195 hdr->cw |=
2196 (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
2197 if (wr->send_flags & IB_SEND_SOLICITED)
2198 hdr->cw |=
2199 (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
2200 hdr->total_len = 0;
2201 switch (wr->opcode) {
2202 case IB_WR_SEND_WITH_IMM:
2203 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2204 hdr->immdt = ntohl(wr->ex.imm_data);
2205 case IB_WR_SEND:
2206 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2207 ocrdma_build_send(qp, hdr, wr);
2208 break;
2209 case IB_WR_SEND_WITH_INV:
2210 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
2211 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2212 hdr->lkey = wr->ex.invalidate_rkey;
2213 status = ocrdma_build_send(qp, hdr, wr);
2214 break;
2215 case IB_WR_RDMA_WRITE_WITH_IMM:
2216 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2217 hdr->immdt = ntohl(wr->ex.imm_data);
2218 case IB_WR_RDMA_WRITE:
2219 hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
2220 status = ocrdma_build_write(qp, hdr, wr);
2221 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302222 case IB_WR_RDMA_READ:
2223 ocrdma_build_read(qp, hdr, wr);
2224 break;
2225 case IB_WR_LOCAL_INV:
2226 hdr->cw |=
2227 (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302228 hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
2229 sizeof(struct ocrdma_sge)) /
Parav Panditfe2caef2012-03-21 04:09:06 +05302230 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
2231 hdr->lkey = wr->ex.invalidate_rkey;
2232 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302233 case IB_WR_FAST_REG_MR:
2234 status = ocrdma_build_fr(qp, hdr, wr);
2235 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302236 default:
2237 status = -EINVAL;
2238 break;
2239 }
2240 if (status) {
2241 *bad_wr = wr;
2242 break;
2243 }
Naresh Gottumukkala2b51a9b2013-08-26 15:27:43 +05302244 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
Parav Panditfe2caef2012-03-21 04:09:06 +05302245 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
2246 else
2247 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
2248 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
2249 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
2250 OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
2251 /* make sure wqe is written before adapter can access it */
2252 wmb();
2253 /* inform hw to start processing it */
2254 ocrdma_ring_sq_db(qp);
2255
2256 /* update pointer, counter for next wr */
2257 ocrdma_hwq_inc_head(&qp->sq);
2258 wr = wr->next;
2259 }
2260 spin_unlock_irqrestore(&qp->q_lock, flags);
2261 return status;
2262}
2263
2264static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
2265{
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302266 u32 val = qp->rq.dbid | (1 << OCRDMA_DB_RQ_SHIFT);
Parav Panditfe2caef2012-03-21 04:09:06 +05302267
Devesh Sharma2df84fa82014-02-04 11:56:55 +05302268 iowrite32(val, qp->rq_db);
Parav Panditfe2caef2012-03-21 04:09:06 +05302269}
2270
2271static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
2272 u16 tag)
2273{
2274 u32 wqe_size = 0;
2275 struct ocrdma_sge *sge;
2276 if (wr->num_sge)
2277 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
2278 else
2279 wqe_size = sizeof(*sge) + sizeof(*rqe);
2280
2281 rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
2282 OCRDMA_WQE_SIZE_SHIFT);
2283 rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2284 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2285 rqe->total_len = 0;
2286 rqe->rsvd_tag = tag;
2287 sge = (struct ocrdma_sge *)(rqe + 1);
2288 ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
2289 ocrdma_cpu_to_le32(rqe, wqe_size);
2290}
2291
2292int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2293 struct ib_recv_wr **bad_wr)
2294{
2295 int status = 0;
2296 unsigned long flags;
2297 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2298 struct ocrdma_hdr_wqe *rqe;
2299
2300 spin_lock_irqsave(&qp->q_lock, flags);
2301 if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
2302 spin_unlock_irqrestore(&qp->q_lock, flags);
2303 *bad_wr = wr;
2304 return -EINVAL;
2305 }
2306 while (wr) {
2307 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
2308 wr->num_sge > qp->rq.max_sges) {
2309 *bad_wr = wr;
2310 status = -ENOMEM;
2311 break;
2312 }
2313 rqe = ocrdma_hwq_head(&qp->rq);
2314 ocrdma_build_rqe(rqe, wr, 0);
2315
2316 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
2317 /* make sure rqe is written before adapter can access it */
2318 wmb();
2319
2320 /* inform hw to start processing it */
2321 ocrdma_ring_rq_db(qp);
2322
2323 /* update pointer, counter for next wr */
2324 ocrdma_hwq_inc_head(&qp->rq);
2325 wr = wr->next;
2326 }
2327 spin_unlock_irqrestore(&qp->q_lock, flags);
2328 return status;
2329}
2330
2331/* cqe for srq's rqe can potentially arrive out of order.
2332 * index gives the entry in the shadow table where to store
2333 * the wr_id. tag/index is returned in cqe to reference back
2334 * for a given rqe.
2335 */
2336static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
2337{
2338 int row = 0;
2339 int indx = 0;
2340
2341 for (row = 0; row < srq->bit_fields_len; row++) {
2342 if (srq->idx_bit_fields[row]) {
2343 indx = ffs(srq->idx_bit_fields[row]);
2344 indx = (row * 32) + (indx - 1);
2345 if (indx >= srq->rq.max_cnt)
2346 BUG();
2347 ocrdma_srq_toggle_bit(srq, indx);
2348 break;
2349 }
2350 }
2351
2352 if (row == srq->bit_fields_len)
2353 BUG();
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302354 return indx + 1; /* Use from index 1 */
Parav Panditfe2caef2012-03-21 04:09:06 +05302355}
2356
2357static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
2358{
2359 u32 val = srq->rq.dbid | (1 << 16);
2360
2361 iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
2362}
2363
2364int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
2365 struct ib_recv_wr **bad_wr)
2366{
2367 int status = 0;
2368 unsigned long flags;
2369 struct ocrdma_srq *srq;
2370 struct ocrdma_hdr_wqe *rqe;
2371 u16 tag;
2372
2373 srq = get_ocrdma_srq(ibsrq);
2374
2375 spin_lock_irqsave(&srq->q_lock, flags);
2376 while (wr) {
2377 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
2378 wr->num_sge > srq->rq.max_sges) {
2379 status = -ENOMEM;
2380 *bad_wr = wr;
2381 break;
2382 }
2383 tag = ocrdma_srq_get_idx(srq);
2384 rqe = ocrdma_hwq_head(&srq->rq);
2385 ocrdma_build_rqe(rqe, wr, tag);
2386
2387 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
2388 /* make sure rqe is written before adapter can perform DMA */
2389 wmb();
2390 /* inform hw to start processing it */
2391 ocrdma_ring_srq_db(srq);
2392 /* update pointer, counter for next wr */
2393 ocrdma_hwq_inc_head(&srq->rq);
2394 wr = wr->next;
2395 }
2396 spin_unlock_irqrestore(&srq->q_lock, flags);
2397 return status;
2398}
2399
2400static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
2401{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302402 enum ib_wc_status ibwc_status;
Parav Panditfe2caef2012-03-21 04:09:06 +05302403
2404 switch (status) {
2405 case OCRDMA_CQE_GENERAL_ERR:
2406 ibwc_status = IB_WC_GENERAL_ERR;
2407 break;
2408 case OCRDMA_CQE_LOC_LEN_ERR:
2409 ibwc_status = IB_WC_LOC_LEN_ERR;
2410 break;
2411 case OCRDMA_CQE_LOC_QP_OP_ERR:
2412 ibwc_status = IB_WC_LOC_QP_OP_ERR;
2413 break;
2414 case OCRDMA_CQE_LOC_EEC_OP_ERR:
2415 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
2416 break;
2417 case OCRDMA_CQE_LOC_PROT_ERR:
2418 ibwc_status = IB_WC_LOC_PROT_ERR;
2419 break;
2420 case OCRDMA_CQE_WR_FLUSH_ERR:
2421 ibwc_status = IB_WC_WR_FLUSH_ERR;
2422 break;
2423 case OCRDMA_CQE_MW_BIND_ERR:
2424 ibwc_status = IB_WC_MW_BIND_ERR;
2425 break;
2426 case OCRDMA_CQE_BAD_RESP_ERR:
2427 ibwc_status = IB_WC_BAD_RESP_ERR;
2428 break;
2429 case OCRDMA_CQE_LOC_ACCESS_ERR:
2430 ibwc_status = IB_WC_LOC_ACCESS_ERR;
2431 break;
2432 case OCRDMA_CQE_REM_INV_REQ_ERR:
2433 ibwc_status = IB_WC_REM_INV_REQ_ERR;
2434 break;
2435 case OCRDMA_CQE_REM_ACCESS_ERR:
2436 ibwc_status = IB_WC_REM_ACCESS_ERR;
2437 break;
2438 case OCRDMA_CQE_REM_OP_ERR:
2439 ibwc_status = IB_WC_REM_OP_ERR;
2440 break;
2441 case OCRDMA_CQE_RETRY_EXC_ERR:
2442 ibwc_status = IB_WC_RETRY_EXC_ERR;
2443 break;
2444 case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2445 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2446 break;
2447 case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2448 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2449 break;
2450 case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2451 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2452 break;
2453 case OCRDMA_CQE_REM_ABORT_ERR:
2454 ibwc_status = IB_WC_REM_ABORT_ERR;
2455 break;
2456 case OCRDMA_CQE_INV_EECN_ERR:
2457 ibwc_status = IB_WC_INV_EECN_ERR;
2458 break;
2459 case OCRDMA_CQE_INV_EEC_STATE_ERR:
2460 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2461 break;
2462 case OCRDMA_CQE_FATAL_ERR:
2463 ibwc_status = IB_WC_FATAL_ERR;
2464 break;
2465 case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2466 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2467 break;
2468 default:
2469 ibwc_status = IB_WC_GENERAL_ERR;
2470 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002471 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302472 return ibwc_status;
2473}
2474
2475static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2476 u32 wqe_idx)
2477{
2478 struct ocrdma_hdr_wqe *hdr;
2479 struct ocrdma_sge *rw;
2480 int opcode;
2481
2482 hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2483
2484 ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2485 /* Undo the hdr->cw swap */
2486 opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2487 switch (opcode) {
2488 case OCRDMA_WRITE:
2489 ibwc->opcode = IB_WC_RDMA_WRITE;
2490 break;
2491 case OCRDMA_READ:
2492 rw = (struct ocrdma_sge *)(hdr + 1);
2493 ibwc->opcode = IB_WC_RDMA_READ;
2494 ibwc->byte_len = rw->len;
2495 break;
2496 case OCRDMA_SEND:
2497 ibwc->opcode = IB_WC_SEND;
2498 break;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302499 case OCRDMA_FR_MR:
2500 ibwc->opcode = IB_WC_FAST_REG_MR;
2501 break;
Parav Panditfe2caef2012-03-21 04:09:06 +05302502 case OCRDMA_LKEY_INV:
2503 ibwc->opcode = IB_WC_LOCAL_INV;
2504 break;
2505 default:
2506 ibwc->status = IB_WC_GENERAL_ERR;
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002507 pr_err("%s() invalid opcode received = 0x%x\n",
2508 __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
Parav Panditfe2caef2012-03-21 04:09:06 +05302509 break;
Joe Perches2b50176d2013-10-08 16:07:22 -07002510 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302511}
2512
2513static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2514 struct ocrdma_cqe *cqe)
2515{
2516 if (is_cqe_for_sq(cqe)) {
2517 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2518 cqe->flags_status_srcqpn) &
2519 ~OCRDMA_CQE_STATUS_MASK);
2520 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2521 cqe->flags_status_srcqpn) |
2522 (OCRDMA_CQE_WR_FLUSH_ERR <<
2523 OCRDMA_CQE_STATUS_SHIFT));
2524 } else {
2525 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2526 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2527 cqe->flags_status_srcqpn) &
2528 ~OCRDMA_CQE_UD_STATUS_MASK);
2529 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2530 cqe->flags_status_srcqpn) |
2531 (OCRDMA_CQE_WR_FLUSH_ERR <<
2532 OCRDMA_CQE_UD_STATUS_SHIFT));
2533 } else {
2534 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2535 cqe->flags_status_srcqpn) &
2536 ~OCRDMA_CQE_STATUS_MASK);
2537 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2538 cqe->flags_status_srcqpn) |
2539 (OCRDMA_CQE_WR_FLUSH_ERR <<
2540 OCRDMA_CQE_STATUS_SHIFT));
2541 }
2542 }
2543}
2544
2545static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2546 struct ocrdma_qp *qp, int status)
2547{
2548 bool expand = false;
2549
2550 ibwc->byte_len = 0;
2551 ibwc->qp = &qp->ibqp;
2552 ibwc->status = ocrdma_to_ibwc_err(status);
2553
2554 ocrdma_flush_qp(qp);
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05302555 ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05302556
2557 /* if wqe/rqe pending for which cqe needs to be returned,
2558 * trigger inflating it.
2559 */
2560 if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2561 expand = true;
2562 ocrdma_set_cqe_status_flushed(qp, cqe);
2563 }
2564 return expand;
2565}
2566
2567static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2568 struct ocrdma_qp *qp, int status)
2569{
2570 ibwc->opcode = IB_WC_RECV;
2571 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2572 ocrdma_hwq_inc_tail(&qp->rq);
2573
2574 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2575}
2576
2577static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2578 struct ocrdma_qp *qp, int status)
2579{
2580 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2581 ocrdma_hwq_inc_tail(&qp->sq);
2582
2583 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2584}
2585
2586
2587static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2588 struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2589 bool *polled, bool *stop)
2590{
2591 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302592 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302593 int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2594 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302595 if (status < OCRDMA_MAX_CQE_ERR)
2596 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302597
2598 /* when hw sq is empty, but rq is not empty, so we continue
2599 * to keep the cqe in order to get the cq event again.
2600 */
2601 if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2602 /* when cq for rq and sq is same, it is safe to return
2603 * flush cqe for RQEs.
2604 */
2605 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2606 *polled = true;
2607 status = OCRDMA_CQE_WR_FLUSH_ERR;
2608 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2609 } else {
2610 /* stop processing further cqe as this cqe is used for
2611 * triggering cq event on buddy cq of RQ.
2612 * When QP is destroyed, this cqe will be removed
2613 * from the cq's hardware q.
2614 */
2615 *polled = false;
2616 *stop = true;
2617 expand = false;
2618 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302619 } else if (is_hw_sq_empty(qp)) {
2620 /* Do nothing */
2621 expand = false;
2622 *polled = false;
2623 *stop = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302624 } else {
2625 *polled = true;
2626 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2627 }
2628 return expand;
2629}
2630
2631static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2632 struct ocrdma_cqe *cqe,
2633 struct ib_wc *ibwc, bool *polled)
2634{
2635 bool expand = false;
2636 int tail = qp->sq.tail;
2637 u32 wqe_idx;
2638
2639 if (!qp->wqe_wr_id_tbl[tail].signaled) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302640 *polled = false; /* WC cannot be consumed yet */
2641 } else {
2642 ibwc->status = IB_WC_SUCCESS;
2643 ibwc->wc_flags = 0;
2644 ibwc->qp = &qp->ibqp;
2645 ocrdma_update_wc(qp, ibwc, tail);
2646 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302647 }
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302648 wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
2649 OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
Parav Panditae3bca92012-08-17 14:45:33 +00002650 if (tail != wqe_idx)
2651 expand = true; /* Coalesced CQE can't be consumed yet */
2652
Parav Panditfe2caef2012-03-21 04:09:06 +05302653 ocrdma_hwq_inc_tail(&qp->sq);
2654 return expand;
2655}
2656
2657static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2658 struct ib_wc *ibwc, bool *polled, bool *stop)
2659{
2660 int status;
2661 bool expand;
2662
2663 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2664 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2665
2666 if (status == OCRDMA_CQE_SUCCESS)
2667 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2668 else
2669 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2670 return expand;
2671}
2672
2673static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
2674{
2675 int status;
2676
2677 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2678 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2679 ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2680 OCRDMA_CQE_SRCQP_MASK;
2681 ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
2682 OCRDMA_CQE_PKEY_MASK;
2683 ibwc->wc_flags = IB_WC_GRH;
2684 ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2685 OCRDMA_CQE_UD_XFER_LEN_SHIFT);
2686 return status;
2687}
2688
2689static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2690 struct ocrdma_cqe *cqe,
2691 struct ocrdma_qp *qp)
2692{
2693 unsigned long flags;
2694 struct ocrdma_srq *srq;
2695 u32 wqe_idx;
2696
2697 srq = get_ocrdma_srq(qp->ibqp.srq);
Naresh Gottumukkala43a6b402013-08-26 15:27:38 +05302698 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302699 OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
2700 if (wqe_idx < 1)
2701 BUG();
2702
Parav Panditfe2caef2012-03-21 04:09:06 +05302703 ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2704 spin_lock_irqsave(&srq->q_lock, flags);
Selvin Xaviercf5788a2014-02-04 11:57:03 +05302705 ocrdma_srq_toggle_bit(srq, wqe_idx - 1);
Parav Panditfe2caef2012-03-21 04:09:06 +05302706 spin_unlock_irqrestore(&srq->q_lock, flags);
2707 ocrdma_hwq_inc_tail(&srq->rq);
2708}
2709
2710static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2711 struct ib_wc *ibwc, bool *polled, bool *stop,
2712 int status)
2713{
2714 bool expand;
Selvin Xavierad56ebb2014-12-18 14:12:59 +05302715 struct ocrdma_dev *dev = get_ocrdma_dev(qp->ibqp.device);
2716
2717 if (status < OCRDMA_MAX_CQE_ERR)
2718 atomic_inc(&dev->cqe_err_stats[status]);
Parav Panditfe2caef2012-03-21 04:09:06 +05302719
2720 /* when hw_rq is empty, but wq is not empty, so continue
2721 * to keep the cqe to get the cq event again.
2722 */
2723 if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2724 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2725 *polled = true;
2726 status = OCRDMA_CQE_WR_FLUSH_ERR;
2727 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2728 } else {
2729 *polled = false;
2730 *stop = true;
2731 expand = false;
2732 }
Selvin Xaviera96ffb12014-06-10 19:32:19 +05302733 } else if (is_hw_rq_empty(qp)) {
2734 /* Do nothing */
2735 expand = false;
2736 *polled = false;
2737 *stop = false;
Parav Pandita3698a92012-06-11 16:39:20 +05302738 } else {
2739 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302740 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
Parav Pandita3698a92012-06-11 16:39:20 +05302741 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302742 return expand;
2743}
2744
2745static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2746 struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2747{
2748 ibwc->opcode = IB_WC_RECV;
2749 ibwc->qp = &qp->ibqp;
2750 ibwc->status = IB_WC_SUCCESS;
2751
2752 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
2753 ocrdma_update_ud_rcqe(ibwc, cqe);
2754 else
2755 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2756
2757 if (is_cqe_imm(cqe)) {
2758 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2759 ibwc->wc_flags |= IB_WC_WITH_IMM;
2760 } else if (is_cqe_wr_imm(cqe)) {
2761 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2762 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2763 ibwc->wc_flags |= IB_WC_WITH_IMM;
2764 } else if (is_cqe_invalidated(cqe)) {
2765 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2766 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2767 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302768 if (qp->ibqp.srq) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302769 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302770 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302771 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2772 ocrdma_hwq_inc_tail(&qp->rq);
2773 }
2774}
2775
2776static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2777 struct ib_wc *ibwc, bool *polled, bool *stop)
2778{
2779 int status;
2780 bool expand = false;
2781
2782 ibwc->wc_flags = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302783 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302784 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2785 OCRDMA_CQE_UD_STATUS_MASK) >>
2786 OCRDMA_CQE_UD_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302787 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302788 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2789 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302790 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302791
2792 if (status == OCRDMA_CQE_SUCCESS) {
2793 *polled = true;
2794 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2795 } else {
2796 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2797 status);
2798 }
2799 return expand;
2800}
2801
2802static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2803 u16 cur_getp)
2804{
2805 if (cq->phase_change) {
2806 if (cur_getp == 0)
2807 cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302808 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302809 /* clear valid bit */
2810 cqe->flags_status_srcqpn = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302811 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302812}
2813
2814static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2815 struct ib_wc *ibwc)
2816{
2817 u16 qpn = 0;
2818 int i = 0;
2819 bool expand = false;
2820 int polled_hw_cqes = 0;
2821 struct ocrdma_qp *qp = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302822 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302823 struct ocrdma_cqe *cqe;
2824 u16 cur_getp; bool polled = false; bool stop = false;
2825
2826 cur_getp = cq->getp;
2827 while (num_entries) {
2828 cqe = cq->va + cur_getp;
2829 /* check whether valid cqe or not */
2830 if (!is_cqe_valid(cq, cqe))
2831 break;
2832 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2833 /* ignore discarded cqe */
2834 if (qpn == 0)
2835 goto skip_cqe;
2836 qp = dev->qp_tbl[qpn];
2837 BUG_ON(qp == NULL);
2838
2839 if (is_cqe_for_sq(cqe)) {
2840 expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2841 &stop);
2842 } else {
2843 expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2844 &stop);
2845 }
2846 if (expand)
2847 goto expand_cqe;
2848 if (stop)
2849 goto stop_cqe;
2850 /* clear qpn to avoid duplicate processing by discard_cqe() */
2851 cqe->cmn.qpn = 0;
2852skip_cqe:
2853 polled_hw_cqes += 1;
2854 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2855 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2856expand_cqe:
2857 if (polled) {
2858 num_entries -= 1;
2859 i += 1;
2860 ibwc = ibwc + 1;
2861 polled = false;
2862 }
2863 }
2864stop_cqe:
2865 cq->getp = cur_getp;
Devesh Sharmaea617622014-02-04 11:56:54 +05302866 if (cq->deferred_arm) {
2867 ocrdma_ring_cq_db(dev, cq->id, true, cq->deferred_sol,
Parav Panditfe2caef2012-03-21 04:09:06 +05302868 polled_hw_cqes);
Devesh Sharmaea617622014-02-04 11:56:54 +05302869 cq->deferred_arm = false;
2870 cq->deferred_sol = false;
2871 } else {
2872 /* We need to pop the CQE. No need to arm */
2873 ocrdma_ring_cq_db(dev, cq->id, false, cq->deferred_sol,
2874 polled_hw_cqes);
2875 cq->deferred_sol = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302876 }
Devesh Sharmaea617622014-02-04 11:56:54 +05302877
Parav Panditfe2caef2012-03-21 04:09:06 +05302878 return i;
2879}
2880
2881/* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2882static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2883 struct ocrdma_qp *qp, struct ib_wc *ibwc)
2884{
2885 int err_cqes = 0;
2886
2887 while (num_entries) {
2888 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2889 break;
2890 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2891 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2892 ocrdma_hwq_inc_tail(&qp->sq);
2893 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2894 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2895 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302896 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302897 return err_cqes;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302898 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302899 ibwc->byte_len = 0;
2900 ibwc->status = IB_WC_WR_FLUSH_ERR;
2901 ibwc = ibwc + 1;
2902 err_cqes += 1;
2903 num_entries -= 1;
2904 }
2905 return err_cqes;
2906}
2907
2908int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2909{
2910 int cqes_to_poll = num_entries;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302911 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2912 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302913 int num_os_cqe = 0, err_cqes = 0;
2914 struct ocrdma_qp *qp;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302915 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05302916
2917 /* poll cqes from adapter CQ */
2918 spin_lock_irqsave(&cq->cq_lock, flags);
2919 num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2920 spin_unlock_irqrestore(&cq->cq_lock, flags);
2921 cqes_to_poll -= num_os_cqe;
2922
2923 if (cqes_to_poll) {
2924 wc = wc + num_os_cqe;
2925 /* adapter returns single error cqe when qp moves to
2926 * error state. So insert error cqes with wc_status as
2927 * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2928 * respectively which uses this CQ.
2929 */
2930 spin_lock_irqsave(&dev->flush_q_lock, flags);
2931 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2932 if (cqes_to_poll == 0)
2933 break;
2934 err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2935 cqes_to_poll -= err_cqes;
2936 num_os_cqe += err_cqes;
2937 wc = wc + err_cqes;
2938 }
2939 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2940 }
2941 return num_os_cqe;
2942}
2943
2944int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2945{
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302946 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2947 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302948 u16 cq_id;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302949 unsigned long flags;
Devesh Sharmaea617622014-02-04 11:56:54 +05302950 bool arm_needed = false, sol_needed = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302951
Parav Panditfe2caef2012-03-21 04:09:06 +05302952 cq_id = cq->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05302953
2954 spin_lock_irqsave(&cq->cq_lock, flags);
2955 if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302956 arm_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302957 if (cq_flags & IB_CQ_SOLICITED)
Devesh Sharmaea617622014-02-04 11:56:54 +05302958 sol_needed = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302959
Devesh Sharmaea617622014-02-04 11:56:54 +05302960 if (cq->first_arm) {
2961 ocrdma_ring_cq_db(dev, cq_id, arm_needed, sol_needed, 0);
2962 cq->first_arm = false;
Parav Panditfe2caef2012-03-21 04:09:06 +05302963 }
Devesh Sharmaea617622014-02-04 11:56:54 +05302964
Devesh Sharmaf93439e2014-06-09 10:52:38 +05302965 cq->deferred_arm = true;
Devesh Sharmaea617622014-02-04 11:56:54 +05302966 cq->deferred_sol = sol_needed;
Parav Panditfe2caef2012-03-21 04:09:06 +05302967 spin_unlock_irqrestore(&cq->cq_lock, flags);
Devesh Sharmaea617622014-02-04 11:56:54 +05302968
Parav Panditfe2caef2012-03-21 04:09:06 +05302969 return 0;
2970}
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05302971
2972struct ib_mr *ocrdma_alloc_frmr(struct ib_pd *ibpd, int max_page_list_len)
2973{
2974 int status;
2975 struct ocrdma_mr *mr;
2976 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
2977 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
2978
2979 if (max_page_list_len > dev->attr.max_pages_per_frmr)
2980 return ERR_PTR(-EINVAL);
2981
2982 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2983 if (!mr)
2984 return ERR_PTR(-ENOMEM);
2985
2986 status = ocrdma_get_pbl_info(dev, mr, max_page_list_len);
2987 if (status)
2988 goto pbl_err;
2989 mr->hwmr.fr_mr = 1;
2990 mr->hwmr.remote_rd = 0;
2991 mr->hwmr.remote_wr = 0;
2992 mr->hwmr.local_rd = 0;
2993 mr->hwmr.local_wr = 0;
2994 mr->hwmr.mw_bind = 0;
2995 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
2996 if (status)
2997 goto pbl_err;
2998 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
2999 if (status)
3000 goto mbx_err;
3001 mr->ibmr.rkey = mr->hwmr.lkey;
3002 mr->ibmr.lkey = mr->hwmr.lkey;
Roland Dreier7a1e89d2014-03-17 23:14:17 -07003003 dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] =
3004 (unsigned long) mr;
Naresh Gottumukkala7c338802013-08-26 15:27:39 +05303005 return &mr->ibmr;
3006mbx_err:
3007 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3008pbl_err:
3009 kfree(mr);
3010 return ERR_PTR(-ENOMEM);
3011}
3012
3013struct ib_fast_reg_page_list *ocrdma_alloc_frmr_page_list(struct ib_device
3014 *ibdev,
3015 int page_list_len)
3016{
3017 struct ib_fast_reg_page_list *frmr_list;
3018 int size;
3019
3020 size = sizeof(*frmr_list) + (page_list_len * sizeof(u64));
3021 frmr_list = kzalloc(size, GFP_KERNEL);
3022 if (!frmr_list)
3023 return ERR_PTR(-ENOMEM);
3024 frmr_list->page_list = (u64 *)(frmr_list + 1);
3025 return frmr_list;
3026}
3027
3028void ocrdma_free_frmr_page_list(struct ib_fast_reg_page_list *page_list)
3029{
3030 kfree(page_list);
3031}
Naresh Gottumukkalacffce992013-08-26 15:27:44 +05303032
3033#define MAX_KERNEL_PBE_SIZE 65536
3034static inline int count_kernel_pbes(struct ib_phys_buf *buf_list,
3035 int buf_cnt, u32 *pbe_size)
3036{
3037 u64 total_size = 0;
3038 u64 buf_size = 0;
3039 int i;
3040 *pbe_size = roundup(buf_list[0].size, PAGE_SIZE);
3041 *pbe_size = roundup_pow_of_two(*pbe_size);
3042
3043 /* find the smallest PBE size that we can have */
3044 for (i = 0; i < buf_cnt; i++) {
3045 /* first addr may not be page aligned, so ignore checking */
3046 if ((i != 0) && ((buf_list[i].addr & ~PAGE_MASK) ||
3047 (buf_list[i].size & ~PAGE_MASK))) {
3048 return 0;
3049 }
3050
3051 /* if configured PBE size is greater then the chosen one,
3052 * reduce the PBE size.
3053 */
3054 buf_size = roundup(buf_list[i].size, PAGE_SIZE);
3055 /* pbe_size has to be even multiple of 4K 1,2,4,8...*/
3056 buf_size = roundup_pow_of_two(buf_size);
3057 if (*pbe_size > buf_size)
3058 *pbe_size = buf_size;
3059
3060 total_size += buf_size;
3061 }
3062 *pbe_size = *pbe_size > MAX_KERNEL_PBE_SIZE ?
3063 (MAX_KERNEL_PBE_SIZE) : (*pbe_size);
3064
3065 /* num_pbes = total_size / (*pbe_size); this is implemented below. */
3066
3067 return total_size >> ilog2(*pbe_size);
3068}
3069
3070static void build_kernel_pbes(struct ib_phys_buf *buf_list, int ib_buf_cnt,
3071 u32 pbe_size, struct ocrdma_pbl *pbl_tbl,
3072 struct ocrdma_hw_mr *hwmr)
3073{
3074 int i;
3075 int idx;
3076 int pbes_per_buf = 0;
3077 u64 buf_addr = 0;
3078 int num_pbes;
3079 struct ocrdma_pbe *pbe;
3080 int total_num_pbes = 0;
3081
3082 if (!hwmr->num_pbes)
3083 return;
3084
3085 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
3086 num_pbes = 0;
3087
3088 /* go through the OS phy regions & fill hw pbe entries into pbls. */
3089 for (i = 0; i < ib_buf_cnt; i++) {
3090 buf_addr = buf_list[i].addr;
3091 pbes_per_buf =
3092 roundup_pow_of_two(roundup(buf_list[i].size, PAGE_SIZE)) /
3093 pbe_size;
3094 hwmr->len += buf_list[i].size;
3095 /* number of pbes can be more for one OS buf, when
3096 * buffers are of different sizes.
3097 * split the ib_buf to one or more pbes.
3098 */
3099 for (idx = 0; idx < pbes_per_buf; idx++) {
3100 /* we program always page aligned addresses,
3101 * first unaligned address is taken care by fbo.
3102 */
3103 if (i == 0) {
3104 /* for non zero fbo, assign the
3105 * start of the page.
3106 */
3107 pbe->pa_lo =
3108 cpu_to_le32((u32) (buf_addr & PAGE_MASK));
3109 pbe->pa_hi =
3110 cpu_to_le32((u32) upper_32_bits(buf_addr));
3111 } else {
3112 pbe->pa_lo =
3113 cpu_to_le32((u32) (buf_addr & 0xffffffff));
3114 pbe->pa_hi =
3115 cpu_to_le32((u32) upper_32_bits(buf_addr));
3116 }
3117 buf_addr += pbe_size;
3118 num_pbes += 1;
3119 total_num_pbes += 1;
3120 pbe++;
3121
3122 if (total_num_pbes == hwmr->num_pbes)
3123 goto mr_tbl_done;
3124 /* if the pbl is full storing the pbes,
3125 * move to next pbl.
3126 */
3127 if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
3128 pbl_tbl++;
3129 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
3130 num_pbes = 0;
3131 }
3132 }
3133 }
3134mr_tbl_done:
3135 return;
3136}
3137
3138struct ib_mr *ocrdma_reg_kernel_mr(struct ib_pd *ibpd,
3139 struct ib_phys_buf *buf_list,
3140 int buf_cnt, int acc, u64 *iova_start)
3141{
3142 int status = -ENOMEM;
3143 struct ocrdma_mr *mr;
3144 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
3145 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
3146 u32 num_pbes;
3147 u32 pbe_size = 0;
3148
3149 if ((acc & IB_ACCESS_REMOTE_WRITE) && !(acc & IB_ACCESS_LOCAL_WRITE))
3150 return ERR_PTR(-EINVAL);
3151
3152 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3153 if (!mr)
3154 return ERR_PTR(status);
3155
3156 num_pbes = count_kernel_pbes(buf_list, buf_cnt, &pbe_size);
3157 if (num_pbes == 0) {
3158 status = -EINVAL;
3159 goto pbl_err;
3160 }
3161 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
3162 if (status)
3163 goto pbl_err;
3164
3165 mr->hwmr.pbe_size = pbe_size;
3166 mr->hwmr.fbo = *iova_start - (buf_list[0].addr & PAGE_MASK);
3167 mr->hwmr.va = *iova_start;
3168 mr->hwmr.local_rd = 1;
3169 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
3170 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
3171 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
3172 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
3173 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
3174
3175 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
3176 if (status)
3177 goto pbl_err;
3178 build_kernel_pbes(buf_list, buf_cnt, pbe_size, mr->hwmr.pbl_table,
3179 &mr->hwmr);
3180 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
3181 if (status)
3182 goto mbx_err;
3183
3184 mr->ibmr.lkey = mr->hwmr.lkey;
3185 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
3186 mr->ibmr.rkey = mr->hwmr.lkey;
3187 return &mr->ibmr;
3188
3189mbx_err:
3190 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3191pbl_err:
3192 kfree(mr);
3193 return ERR_PTR(status);
3194}