blob: 5f68dff0d6caec3ad8f59cf5f5fe1b6844abcbe7 [file] [log] [blame]
Parav Panditfe2caef2012-03-21 04:09:06 +05301/*******************************************************************
2 * This file is part of the Emulex RoCE Device Driver for *
3 * RoCE (RDMA over Converged Ethernet) adapters. *
4 * Copyright (C) 2008-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * *
8 * This program is free software; you can redistribute it and/or *
9 * modify it under the terms of version 2 of the GNU General *
10 * Public License as published by the Free Software Foundation. *
11 * This program is distributed in the hope that it will be useful. *
12 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
13 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
14 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
15 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16 * TO BE LEGALLY INVALID. See the GNU General Public License for *
17 * more details, a copy of which can be found in the file COPYING *
18 * included with this package. *
19 *
20 * Contact Information:
21 * linux-drivers@emulex.com
22 *
23 * Emulex
24 * 3333 Susan Street
25 * Costa Mesa, CA 92626
26 *******************************************************************/
27
28#include <linux/dma-mapping.h>
29#include <rdma/ib_verbs.h>
30#include <rdma/ib_user_verbs.h>
31#include <rdma/iw_cm.h>
32#include <rdma/ib_umem.h>
33#include <rdma/ib_addr.h>
34
35#include "ocrdma.h"
36#include "ocrdma_hw.h"
37#include "ocrdma_verbs.h"
38#include "ocrdma_abi.h"
39
40int ocrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
41{
42 if (index > 1)
43 return -EINVAL;
44
45 *pkey = 0xffff;
46 return 0;
47}
48
49int ocrdma_query_gid(struct ib_device *ibdev, u8 port,
50 int index, union ib_gid *sgid)
51{
52 struct ocrdma_dev *dev;
53
54 dev = get_ocrdma_dev(ibdev);
55 memset(sgid, 0, sizeof(*sgid));
Dan Carpenter7b33dc22012-06-14 21:36:09 +030056 if (index >= OCRDMA_MAX_SGID)
Parav Panditfe2caef2012-03-21 04:09:06 +053057 return -EINVAL;
58
59 memcpy(sgid, &dev->sgid_tbl[index], sizeof(*sgid));
60
61 return 0;
62}
63
64int ocrdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr)
65{
66 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
67
68 memset(attr, 0, sizeof *attr);
69 memcpy(&attr->fw_ver, &dev->attr.fw_ver[0],
70 min(sizeof(dev->attr.fw_ver), sizeof(attr->fw_ver)));
71 ocrdma_get_guid(dev, (u8 *)&attr->sys_image_guid);
72 attr->max_mr_size = ~0ull;
73 attr->page_size_cap = 0xffff000;
74 attr->vendor_id = dev->nic_info.pdev->vendor;
75 attr->vendor_part_id = dev->nic_info.pdev->device;
76 attr->hw_ver = 0;
77 attr->max_qp = dev->attr.max_qp;
78 attr->max_ah = dev->attr.max_qp;
79 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 |
85 IB_DEVICE_LOCAL_DMA_LKEY;
Mahesh Vardhamanaiah634c5792012-06-08 21:26:11 +053086 attr->max_sge = min(dev->attr.max_send_sge, dev->attr.max_srq_sge);
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +053087 attr->max_sge_rd = dev->attr.max_rdma_sge;
Parav Panditfe2caef2012-03-21 04:09:06 +053088 attr->max_cq = dev->attr.max_cq;
89 attr->max_cqe = dev->attr.max_cqe;
90 attr->max_mr = dev->attr.max_mr;
91 attr->max_mw = 0;
92 attr->max_pd = dev->attr.max_pd;
93 attr->atomic_cap = 0;
94 attr->max_fmr = 0;
95 attr->max_map_per_fmr = 0;
96 attr->max_qp_rd_atom =
97 min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp);
98 attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp;
99 attr->max_srq = (dev->attr.max_qp - 1);
Roland Dreierd1e09eb2012-07-07 15:13:47 -0700100 attr->max_srq_sge = dev->attr.max_srq_sge;
Parav Panditfe2caef2012-03-21 04:09:06 +0530101 attr->max_srq_wr = dev->attr.max_rqe;
102 attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay;
103 attr->max_fast_reg_page_list_len = 0;
104 attr->max_pkeys = 1;
105 return 0;
106}
107
108int ocrdma_query_port(struct ib_device *ibdev,
109 u8 port, struct ib_port_attr *props)
110{
111 enum ib_port_state port_state;
112 struct ocrdma_dev *dev;
113 struct net_device *netdev;
114
115 dev = get_ocrdma_dev(ibdev);
116 if (port > 1) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000117 pr_err("%s(%d) invalid_port=0x%x\n", __func__,
118 dev->id, port);
Parav Panditfe2caef2012-03-21 04:09:06 +0530119 return -EINVAL;
120 }
121 netdev = dev->nic_info.netdev;
122 if (netif_running(netdev) && netif_oper_up(netdev)) {
123 port_state = IB_PORT_ACTIVE;
124 props->phys_state = 5;
125 } else {
126 port_state = IB_PORT_DOWN;
127 props->phys_state = 3;
128 }
129 props->max_mtu = IB_MTU_4096;
130 props->active_mtu = iboe_get_mtu(netdev->mtu);
131 props->lid = 0;
132 props->lmc = 0;
133 props->sm_lid = 0;
134 props->sm_sl = 0;
135 props->state = port_state;
136 props->port_cap_flags =
137 IB_PORT_CM_SUP |
138 IB_PORT_REINIT_SUP |
139 IB_PORT_DEVICE_MGMT_SUP | IB_PORT_VENDOR_CLASS_SUP;
140 props->gid_tbl_len = OCRDMA_MAX_SGID;
141 props->pkey_tbl_len = 1;
142 props->bad_pkey_cntr = 0;
143 props->qkey_viol_cntr = 0;
144 props->active_width = IB_WIDTH_1X;
145 props->active_speed = 4;
146 props->max_msg_sz = 0x80000000;
147 props->max_vl_num = 4;
148 return 0;
149}
150
151int ocrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
152 struct ib_port_modify *props)
153{
154 struct ocrdma_dev *dev;
155
156 dev = get_ocrdma_dev(ibdev);
157 if (port > 1) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000158 pr_err("%s(%d) invalid_port=0x%x\n", __func__, dev->id, port);
Parav Panditfe2caef2012-03-21 04:09:06 +0530159 return -EINVAL;
160 }
161 return 0;
162}
163
164static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
165 unsigned long len)
166{
167 struct ocrdma_mm *mm;
168
169 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
170 if (mm == NULL)
171 return -ENOMEM;
172 mm->key.phy_addr = phy_addr;
173 mm->key.len = len;
174 INIT_LIST_HEAD(&mm->entry);
175
176 mutex_lock(&uctx->mm_list_lock);
177 list_add_tail(&mm->entry, &uctx->mm_head);
178 mutex_unlock(&uctx->mm_list_lock);
179 return 0;
180}
181
182static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
183 unsigned long len)
184{
185 struct ocrdma_mm *mm, *tmp;
186
187 mutex_lock(&uctx->mm_list_lock);
188 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
189 if (len != mm->key.len || phy_addr != mm->key.phy_addr)
190 continue;
191
192 list_del(&mm->entry);
193 kfree(mm);
194 break;
195 }
196 mutex_unlock(&uctx->mm_list_lock);
197}
198
199static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
200 unsigned long len)
201{
202 bool found = false;
203 struct ocrdma_mm *mm;
204
205 mutex_lock(&uctx->mm_list_lock);
206 list_for_each_entry(mm, &uctx->mm_head, entry) {
207 if (len != mm->key.len || phy_addr != mm->key.phy_addr)
208 continue;
209
210 found = true;
211 break;
212 }
213 mutex_unlock(&uctx->mm_list_lock);
214 return found;
215}
216
217struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
218 struct ib_udata *udata)
219{
220 int status;
221 struct ocrdma_ucontext *ctx;
222 struct ocrdma_alloc_ucontext_resp resp;
223 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
224 struct pci_dev *pdev = dev->nic_info.pdev;
225 u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
226
227 if (!udata)
228 return ERR_PTR(-EFAULT);
229 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
230 if (!ctx)
231 return ERR_PTR(-ENOMEM);
Parav Panditfe2caef2012-03-21 04:09:06 +0530232 INIT_LIST_HEAD(&ctx->mm_head);
233 mutex_init(&ctx->mm_list_lock);
234
235 ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
236 &ctx->ah_tbl.pa, GFP_KERNEL);
237 if (!ctx->ah_tbl.va) {
238 kfree(ctx);
239 return ERR_PTR(-ENOMEM);
240 }
241 memset(ctx->ah_tbl.va, 0, map_len);
242 ctx->ah_tbl.len = map_len;
243
Dan Carpenter63ea3742013-07-29 22:34:29 +0300244 memset(&resp, 0, sizeof(resp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530245 resp.ah_tbl_len = ctx->ah_tbl.len;
246 resp.ah_tbl_page = ctx->ah_tbl.pa;
247
248 status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
249 if (status)
250 goto map_err;
251 resp.dev_id = dev->id;
252 resp.max_inline_data = dev->attr.max_inline_data;
253 resp.wqe_size = dev->attr.wqe_size;
254 resp.rqe_size = dev->attr.rqe_size;
255 resp.dpp_wqe_size = dev->attr.wqe_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530256
257 memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
258 status = ib_copy_to_udata(udata, &resp, sizeof(resp));
259 if (status)
260 goto cpy_err;
261 return &ctx->ibucontext;
262
263cpy_err:
264 ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
265map_err:
266 dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
267 ctx->ah_tbl.pa);
268 kfree(ctx);
269 return ERR_PTR(status);
270}
271
272int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
273{
274 struct ocrdma_mm *mm, *tmp;
275 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530276 struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
277 struct pci_dev *pdev = dev->nic_info.pdev;
Parav Panditfe2caef2012-03-21 04:09:06 +0530278
279 ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
280 dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
281 uctx->ah_tbl.pa);
282
283 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
284 list_del(&mm->entry);
285 kfree(mm);
286 }
287 kfree(uctx);
288 return 0;
289}
290
291int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
292{
293 struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530294 struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530295 unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
296 u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
297 unsigned long len = (vma->vm_end - vma->vm_start);
298 int status = 0;
299 bool found;
300
301 if (vma->vm_start & (PAGE_SIZE - 1))
302 return -EINVAL;
303 found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
304 if (!found)
305 return -EINVAL;
306
307 if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
308 dev->nic_info.db_total_size)) &&
309 (len <= dev->nic_info.db_page_size)) {
310 /* doorbell mapping */
311 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
312 len, vma->vm_page_prot);
313 } else if (dev->nic_info.dpp_unmapped_len &&
314 (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
315 (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
316 dev->nic_info.dpp_unmapped_len)) &&
317 (len <= dev->nic_info.dpp_unmapped_len)) {
318 /* dpp area mapping */
319 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
320 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
321 len, vma->vm_page_prot);
322 } else {
323 /* queue memory mapping */
324 status = remap_pfn_range(vma, vma->vm_start,
325 vma->vm_pgoff, len, vma->vm_page_prot);
326 }
327 return status;
328}
329
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530330static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
Parav Panditfe2caef2012-03-21 04:09:06 +0530331 struct ib_ucontext *ib_ctx,
332 struct ib_udata *udata)
333{
334 int status;
335 u64 db_page_addr;
Roland Dreierda496432012-04-16 11:32:17 -0700336 u64 dpp_page_addr = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530337 u32 db_page_size;
338 struct ocrdma_alloc_pd_uresp rsp;
339 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
340
Dan Carpenter63ea3742013-07-29 22:34:29 +0300341 memset(&rsp, 0, sizeof(rsp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530342 rsp.id = pd->id;
343 rsp.dpp_enabled = pd->dpp_enabled;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530344 db_page_addr = dev->nic_info.unmapped_db +
345 (pd->id * dev->nic_info.db_page_size);
346 db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530347
348 status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
349 if (status)
350 return status;
351
352 if (pd->dpp_enabled) {
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530353 dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
Parav Panditfe2caef2012-03-21 04:09:06 +0530354 (pd->id * OCRDMA_DPP_PAGE_SIZE);
355 status = ocrdma_add_mmap(uctx, dpp_page_addr,
356 OCRDMA_DPP_PAGE_SIZE);
357 if (status)
358 goto dpp_map_err;
359 rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
360 rsp.dpp_page_addr_lo = dpp_page_addr;
361 }
362
363 status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
364 if (status)
365 goto ucopy_err;
366
367 pd->uctx = uctx;
368 return 0;
369
370ucopy_err:
Roland Dreierda496432012-04-16 11:32:17 -0700371 if (pd->dpp_enabled)
372 ocrdma_del_mmap(pd->uctx, dpp_page_addr, OCRDMA_DPP_PAGE_SIZE);
Parav Panditfe2caef2012-03-21 04:09:06 +0530373dpp_map_err:
374 ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
375 return status;
376}
377
378struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev,
379 struct ib_ucontext *context,
380 struct ib_udata *udata)
381{
382 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
383 struct ocrdma_pd *pd;
384 int status;
385
386 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
387 if (!pd)
388 return ERR_PTR(-ENOMEM);
Parav Panditfe2caef2012-03-21 04:09:06 +0530389 if (udata && context) {
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530390 pd->dpp_enabled =
391 (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY);
Parav Panditfe2caef2012-03-21 04:09:06 +0530392 pd->num_dpp_qp =
393 pd->dpp_enabled ? OCRDMA_PD_MAX_DPP_ENABLED_QP : 0;
394 }
395 status = ocrdma_mbx_alloc_pd(dev, pd);
396 if (status) {
397 kfree(pd);
398 return ERR_PTR(status);
399 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530400
401 if (udata && context) {
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530402 status = ocrdma_copy_pd_uresp(dev, pd, context, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +0530403 if (status)
404 goto err;
405 }
406 return &pd->ibpd;
407
408err:
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +0530409 status = ocrdma_mbx_dealloc_pd(dev, pd);
410 kfree(pd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530411 return ERR_PTR(status);
412}
413
414int ocrdma_dealloc_pd(struct ib_pd *ibpd)
415{
416 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530417 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530418 int status;
419 u64 usr_db;
420
Parav Panditfe2caef2012-03-21 04:09:06 +0530421 status = ocrdma_mbx_dealloc_pd(dev, pd);
422 if (pd->uctx) {
423 u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
424 (pd->id * OCRDMA_DPP_PAGE_SIZE);
425 if (pd->dpp_enabled)
426 ocrdma_del_mmap(pd->uctx, dpp_db, OCRDMA_DPP_PAGE_SIZE);
427 usr_db = dev->nic_info.unmapped_db +
428 (pd->id * dev->nic_info.db_page_size);
429 ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
430 }
431 kfree(pd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530432 return status;
433}
434
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530435static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
436 u32 pdid, int acc, u32 num_pbls, u32 addr_check)
Parav Panditfe2caef2012-03-21 04:09:06 +0530437{
438 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530439
Parav Panditfe2caef2012-03-21 04:09:06 +0530440 mr->hwmr.fr_mr = 0;
441 mr->hwmr.local_rd = 1;
442 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
443 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
444 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
445 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
446 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
447 mr->hwmr.num_pbls = num_pbls;
448
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530449 status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
450 if (status)
451 return status;
452
Parav Panditfe2caef2012-03-21 04:09:06 +0530453 mr->ibmr.lkey = mr->hwmr.lkey;
454 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
455 mr->ibmr.rkey = mr->hwmr.lkey;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530456 return 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530457}
458
459struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
460{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530461 int status;
Parav Panditfe2caef2012-03-21 04:09:06 +0530462 struct ocrdma_mr *mr;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530463 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
464 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530465
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530466 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
467 pr_err("%s err, invalid access rights\n", __func__);
468 return ERR_PTR(-EINVAL);
469 }
470
471 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
472 if (!mr)
473 return ERR_PTR(-ENOMEM);
474
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530475 status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530476 OCRDMA_ADDR_CHECK_DISABLE);
477 if (status) {
478 kfree(mr);
479 return ERR_PTR(status);
480 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530481
482 return &mr->ibmr;
483}
484
485static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
486 struct ocrdma_hw_mr *mr)
487{
488 struct pci_dev *pdev = dev->nic_info.pdev;
489 int i = 0;
490
491 if (mr->pbl_table) {
492 for (i = 0; i < mr->num_pbls; i++) {
493 if (!mr->pbl_table[i].va)
494 continue;
495 dma_free_coherent(&pdev->dev, mr->pbl_size,
496 mr->pbl_table[i].va,
497 mr->pbl_table[i].pa);
498 }
499 kfree(mr->pbl_table);
500 mr->pbl_table = NULL;
501 }
502}
503
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530504static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
505 u32 num_pbes)
Parav Panditfe2caef2012-03-21 04:09:06 +0530506{
507 u32 num_pbls = 0;
508 u32 idx = 0;
509 int status = 0;
510 u32 pbl_size;
511
512 do {
513 pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
514 if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
515 status = -EFAULT;
516 break;
517 }
518 num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
519 num_pbls = num_pbls / (pbl_size / sizeof(u64));
520 idx++;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530521 } while (num_pbls >= dev->attr.max_num_mr_pbl);
Parav Panditfe2caef2012-03-21 04:09:06 +0530522
523 mr->hwmr.num_pbes = num_pbes;
524 mr->hwmr.num_pbls = num_pbls;
525 mr->hwmr.pbl_size = pbl_size;
526 return status;
527}
528
529static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
530{
531 int status = 0;
532 int i;
533 u32 dma_len = mr->pbl_size;
534 struct pci_dev *pdev = dev->nic_info.pdev;
535 void *va;
536 dma_addr_t pa;
537
538 mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) *
539 mr->num_pbls, GFP_KERNEL);
540
541 if (!mr->pbl_table)
542 return -ENOMEM;
543
544 for (i = 0; i < mr->num_pbls; i++) {
545 va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
546 if (!va) {
547 ocrdma_free_mr_pbl_tbl(dev, mr);
548 status = -ENOMEM;
549 break;
550 }
551 memset(va, 0, dma_len);
552 mr->pbl_table[i].va = va;
553 mr->pbl_table[i].pa = pa;
554 }
555 return status;
556}
557
558static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
559 u32 num_pbes)
560{
561 struct ocrdma_pbe *pbe;
562 struct ib_umem_chunk *chunk;
563 struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
564 struct ib_umem *umem = mr->umem;
565 int i, shift, pg_cnt, pages, pbe_cnt, total_num_pbes = 0;
566
567 if (!mr->hwmr.num_pbes)
568 return;
569
570 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
571 pbe_cnt = 0;
572
573 shift = ilog2(umem->page_size);
574
575 list_for_each_entry(chunk, &umem->chunk_list, list) {
576 /* get all the dma regions from the chunk. */
577 for (i = 0; i < chunk->nmap; i++) {
578 pages = sg_dma_len(&chunk->page_list[i]) >> shift;
579 for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) {
580 /* store the page address in pbe */
581 pbe->pa_lo =
582 cpu_to_le32(sg_dma_address
583 (&chunk->page_list[i]) +
584 (umem->page_size * pg_cnt));
585 pbe->pa_hi =
586 cpu_to_le32(upper_32_bits
587 ((sg_dma_address
588 (&chunk->page_list[i]) +
589 umem->page_size * pg_cnt)));
590 pbe_cnt += 1;
591 total_num_pbes += 1;
592 pbe++;
593
594 /* if done building pbes, issue the mbx cmd. */
595 if (total_num_pbes == num_pbes)
596 return;
597
598 /* if the given pbl is full storing the pbes,
599 * move to next pbl.
600 */
601 if (pbe_cnt ==
602 (mr->hwmr.pbl_size / sizeof(u64))) {
603 pbl_tbl++;
604 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
605 pbe_cnt = 0;
606 }
607 }
608 }
609 }
610}
611
612struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
613 u64 usr_addr, int acc, struct ib_udata *udata)
614{
615 int status = -ENOMEM;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +0530616 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530617 struct ocrdma_mr *mr;
618 struct ocrdma_pd *pd;
Parav Panditfe2caef2012-03-21 04:09:06 +0530619 u32 num_pbes;
620
621 pd = get_ocrdma_pd(ibpd);
Parav Panditfe2caef2012-03-21 04:09:06 +0530622
623 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
624 return ERR_PTR(-EINVAL);
625
626 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
627 if (!mr)
628 return ERR_PTR(status);
Parav Panditfe2caef2012-03-21 04:09:06 +0530629 mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
630 if (IS_ERR(mr->umem)) {
631 status = -EFAULT;
632 goto umem_err;
633 }
634 num_pbes = ib_umem_page_count(mr->umem);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530635 status = ocrdma_get_pbl_info(dev, mr, num_pbes);
Parav Panditfe2caef2012-03-21 04:09:06 +0530636 if (status)
637 goto umem_err;
638
639 mr->hwmr.pbe_size = mr->umem->page_size;
640 mr->hwmr.fbo = mr->umem->offset;
641 mr->hwmr.va = usr_addr;
642 mr->hwmr.len = len;
643 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
644 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
645 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
646 mr->hwmr.local_rd = 1;
647 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
648 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
649 if (status)
650 goto umem_err;
651 build_user_pbes(dev, mr, num_pbes);
652 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
653 if (status)
654 goto mbx_err;
Parav Panditfe2caef2012-03-21 04:09:06 +0530655 mr->ibmr.lkey = mr->hwmr.lkey;
656 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
657 mr->ibmr.rkey = mr->hwmr.lkey;
658
659 return &mr->ibmr;
660
661mbx_err:
662 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
663umem_err:
664 kfree(mr);
665 return ERR_PTR(status);
666}
667
668int ocrdma_dereg_mr(struct ib_mr *ib_mr)
669{
670 struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530671 struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530672 int status;
673
674 status = ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
675
676 if (mr->hwmr.fr_mr == 0)
677 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
678
Parav Panditfe2caef2012-03-21 04:09:06 +0530679 /* it could be user registered memory. */
680 if (mr->umem)
681 ib_umem_release(mr->umem);
682 kfree(mr);
683 return status;
684}
685
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530686static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
687 struct ib_udata *udata,
Parav Panditfe2caef2012-03-21 04:09:06 +0530688 struct ib_ucontext *ib_ctx)
689{
690 int status;
691 struct ocrdma_ucontext *uctx;
692 struct ocrdma_create_cq_uresp uresp;
693
Dan Carpenter63ea3742013-07-29 22:34:29 +0300694 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +0530695 uresp.cq_id = cq->id;
696 uresp.page_size = cq->len;
697 uresp.num_pages = 1;
698 uresp.max_hw_cqe = cq->max_hw_cqe;
699 uresp.page_addr[0] = cq->pa;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530700 uresp.db_page_addr = dev->nic_info.unmapped_db;
701 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +0530702 uresp.phase_change = cq->phase_change ? 1 : 0;
703 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
704 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000705 pr_err("%s(%d) copy error cqid=0x%x.\n",
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530706 __func__, dev->id, cq->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530707 goto err;
708 }
709 uctx = get_ocrdma_ucontext(ib_ctx);
710 status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
711 if (status)
712 goto err;
713 status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
714 if (status) {
715 ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
716 goto err;
717 }
718 cq->ucontext = uctx;
719err:
720 return status;
721}
722
723struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector,
724 struct ib_ucontext *ib_ctx,
725 struct ib_udata *udata)
726{
727 struct ocrdma_cq *cq;
728 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
729 int status;
730 struct ocrdma_create_cq_ureq ureq;
731
732 if (udata) {
733 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
734 return ERR_PTR(-EFAULT);
735 } else
736 ureq.dpp_cq = 0;
737 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
738 if (!cq)
739 return ERR_PTR(-ENOMEM);
740
741 spin_lock_init(&cq->cq_lock);
742 spin_lock_init(&cq->comp_handler_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +0530743 INIT_LIST_HEAD(&cq->sq_head);
744 INIT_LIST_HEAD(&cq->rq_head);
Parav Panditfe2caef2012-03-21 04:09:06 +0530745
746 status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq);
747 if (status) {
748 kfree(cq);
749 return ERR_PTR(status);
750 }
751 if (ib_ctx) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530752 status = ocrdma_copy_cq_uresp(dev, cq, udata, ib_ctx);
Parav Panditfe2caef2012-03-21 04:09:06 +0530753 if (status)
754 goto ctx_err;
755 }
756 cq->phase = OCRDMA_CQE_VALID;
757 cq->arm_needed = true;
758 dev->cq_tbl[cq->id] = cq;
759
760 return &cq->ibcq;
761
762ctx_err:
763 ocrdma_mbx_destroy_cq(dev, cq);
764 kfree(cq);
765 return ERR_PTR(status);
766}
767
768int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
769 struct ib_udata *udata)
770{
771 int status = 0;
772 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
773
774 if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
775 status = -EINVAL;
776 return status;
777 }
778 ibcq->cqe = new_cnt;
779 return status;
780}
781
782int ocrdma_destroy_cq(struct ib_cq *ibcq)
783{
784 int status;
785 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +0530786 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +0530787
Parav Panditfe2caef2012-03-21 04:09:06 +0530788 status = ocrdma_mbx_destroy_cq(dev, cq);
789
790 if (cq->ucontext) {
791 ocrdma_del_mmap(cq->ucontext, (u64) cq->pa, cq->len);
792 ocrdma_del_mmap(cq->ucontext, dev->nic_info.unmapped_db,
793 dev->nic_info.db_page_size);
794 }
795 dev->cq_tbl[cq->id] = NULL;
796
797 kfree(cq);
798 return status;
799}
800
801static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
802{
803 int status = -EINVAL;
804
805 if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
806 dev->qp_tbl[qp->id] = qp;
807 status = 0;
808 }
809 return status;
810}
811
812static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
813{
814 dev->qp_tbl[qp->id] = NULL;
815}
816
817static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
818 struct ib_qp_init_attr *attrs)
819{
820 if (attrs->qp_type != IB_QPT_GSI &&
821 attrs->qp_type != IB_QPT_RC &&
822 attrs->qp_type != IB_QPT_UD) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000823 pr_err("%s(%d) unsupported qp type=0x%x requested\n",
824 __func__, dev->id, attrs->qp_type);
Parav Panditfe2caef2012-03-21 04:09:06 +0530825 return -EINVAL;
826 }
827 if (attrs->cap.max_send_wr > dev->attr.max_wqe) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000828 pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
829 __func__, dev->id, attrs->cap.max_send_wr);
830 pr_err("%s(%d) supported send_wr=0x%x\n",
831 __func__, dev->id, dev->attr.max_wqe);
Parav Panditfe2caef2012-03-21 04:09:06 +0530832 return -EINVAL;
833 }
834 if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000835 pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
836 __func__, dev->id, attrs->cap.max_recv_wr);
837 pr_err("%s(%d) supported recv_wr=0x%x\n",
838 __func__, dev->id, dev->attr.max_rqe);
Parav Panditfe2caef2012-03-21 04:09:06 +0530839 return -EINVAL;
840 }
841 if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000842 pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
843 __func__, dev->id, attrs->cap.max_inline_data);
844 pr_err("%s(%d) supported inline data size=0x%x\n",
845 __func__, dev->id, dev->attr.max_inline_data);
Parav Panditfe2caef2012-03-21 04:09:06 +0530846 return -EINVAL;
847 }
848 if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000849 pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
850 __func__, dev->id, attrs->cap.max_send_sge);
851 pr_err("%s(%d) supported send_sge=0x%x\n",
852 __func__, dev->id, dev->attr.max_send_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +0530853 return -EINVAL;
854 }
855 if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000856 pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
857 __func__, dev->id, attrs->cap.max_recv_sge);
858 pr_err("%s(%d) supported recv_sge=0x%x\n",
859 __func__, dev->id, dev->attr.max_recv_sge);
Parav Panditfe2caef2012-03-21 04:09:06 +0530860 return -EINVAL;
861 }
862 /* unprivileged user space cannot create special QP */
863 if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000864 pr_err
Parav Panditfe2caef2012-03-21 04:09:06 +0530865 ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
866 __func__, dev->id, attrs->qp_type);
867 return -EINVAL;
868 }
869 /* allow creating only one GSI type of QP */
870 if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000871 pr_err("%s(%d) GSI special QPs already created.\n",
872 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530873 return -EINVAL;
874 }
875 /* verify consumer QPs are not trying to use GSI QP's CQ */
876 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
877 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
Roland Dreier9e8fa042012-07-27 13:14:44 -0700878 (dev->gsi_sqcq == get_ocrdma_cq(attrs->recv_cq)) ||
879 (dev->gsi_rqcq == get_ocrdma_cq(attrs->send_cq)) ||
880 (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000881 pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
882 __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530883 return -EINVAL;
884 }
885 }
886 return 0;
887}
888
889static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
890 struct ib_udata *udata, int dpp_offset,
891 int dpp_credit_lmt, int srq)
892{
893 int status = 0;
894 u64 usr_db;
895 struct ocrdma_create_qp_uresp uresp;
896 struct ocrdma_dev *dev = qp->dev;
897 struct ocrdma_pd *pd = qp->pd;
898
899 memset(&uresp, 0, sizeof(uresp));
900 usr_db = dev->nic_info.unmapped_db +
901 (pd->id * dev->nic_info.db_page_size);
902 uresp.qp_id = qp->id;
903 uresp.sq_dbid = qp->sq.dbid;
904 uresp.num_sq_pages = 1;
905 uresp.sq_page_size = qp->sq.len;
906 uresp.sq_page_addr[0] = qp->sq.pa;
907 uresp.num_wqe_allocated = qp->sq.max_cnt;
908 if (!srq) {
909 uresp.rq_dbid = qp->rq.dbid;
910 uresp.num_rq_pages = 1;
911 uresp.rq_page_size = qp->rq.len;
912 uresp.rq_page_addr[0] = qp->rq.pa;
913 uresp.num_rqe_allocated = qp->rq.max_cnt;
914 }
915 uresp.db_page_addr = usr_db;
916 uresp.db_page_size = dev->nic_info.db_page_size;
917 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
918 uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
919 uresp.db_rq_offset = ((qp->id & 0xFFFF) < 128) ?
920 OCRDMA_DB_GEN2_RQ1_OFFSET : OCRDMA_DB_GEN2_RQ2_OFFSET;
921 uresp.db_shift = (qp->id < 128) ? 24 : 16;
922 } else {
923 uresp.db_sq_offset = OCRDMA_DB_SQ_OFFSET;
924 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
925 uresp.db_shift = 16;
926 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530927
928 if (qp->dpp_enabled) {
929 uresp.dpp_credit = dpp_credit_lmt;
930 uresp.dpp_offset = dpp_offset;
931 }
932 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
933 if (status) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +0000934 pr_err("%s(%d) user copy error.\n", __func__, dev->id);
Parav Panditfe2caef2012-03-21 04:09:06 +0530935 goto err;
936 }
937 status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
938 uresp.sq_page_size);
939 if (status)
940 goto err;
941
942 if (!srq) {
943 status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
944 uresp.rq_page_size);
945 if (status)
946 goto rq_map_err;
947 }
948 return status;
949rq_map_err:
950 ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
951err:
952 return status;
953}
954
955static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
956 struct ocrdma_pd *pd)
957{
958 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
959 qp->sq_db = dev->nic_info.db +
960 (pd->id * dev->nic_info.db_page_size) +
961 OCRDMA_DB_GEN2_SQ_OFFSET;
962 qp->rq_db = dev->nic_info.db +
963 (pd->id * dev->nic_info.db_page_size) +
964 ((qp->id < 128) ?
965 OCRDMA_DB_GEN2_RQ1_OFFSET : OCRDMA_DB_GEN2_RQ2_OFFSET);
966 } else {
967 qp->sq_db = dev->nic_info.db +
968 (pd->id * dev->nic_info.db_page_size) +
969 OCRDMA_DB_SQ_OFFSET;
970 qp->rq_db = dev->nic_info.db +
971 (pd->id * dev->nic_info.db_page_size) +
972 OCRDMA_DB_RQ_OFFSET;
973 }
974}
975
976static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
977{
978 qp->wqe_wr_id_tbl =
979 kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
980 GFP_KERNEL);
981 if (qp->wqe_wr_id_tbl == NULL)
982 return -ENOMEM;
983 qp->rqe_wr_id_tbl =
984 kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
985 if (qp->rqe_wr_id_tbl == NULL)
986 return -ENOMEM;
987
988 return 0;
989}
990
991static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
992 struct ocrdma_pd *pd,
993 struct ib_qp_init_attr *attrs)
994{
995 qp->pd = pd;
996 spin_lock_init(&qp->q_lock);
997 INIT_LIST_HEAD(&qp->sq_entry);
998 INIT_LIST_HEAD(&qp->rq_entry);
999
1000 qp->qp_type = attrs->qp_type;
1001 qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
1002 qp->max_inline_data = attrs->cap.max_inline_data;
1003 qp->sq.max_sges = attrs->cap.max_send_sge;
1004 qp->rq.max_sges = attrs->cap.max_recv_sge;
1005 qp->state = OCRDMA_QPS_RST;
1006}
1007
Parav Panditfe2caef2012-03-21 04:09:06 +05301008
1009static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
1010 struct ib_qp_init_attr *attrs)
1011{
1012 if (attrs->qp_type == IB_QPT_GSI) {
1013 dev->gsi_qp_created = 1;
1014 dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
1015 dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
1016 }
1017}
1018
1019struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
1020 struct ib_qp_init_attr *attrs,
1021 struct ib_udata *udata)
1022{
1023 int status;
1024 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1025 struct ocrdma_qp *qp;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301026 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301027 struct ocrdma_create_qp_ureq ureq;
1028 u16 dpp_credit_lmt, dpp_offset;
1029
1030 status = ocrdma_check_qp_params(ibpd, dev, attrs);
1031 if (status)
1032 goto gen_err;
1033
1034 memset(&ureq, 0, sizeof(ureq));
1035 if (udata) {
1036 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1037 return ERR_PTR(-EFAULT);
1038 }
1039 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1040 if (!qp) {
1041 status = -ENOMEM;
1042 goto gen_err;
1043 }
1044 qp->dev = dev;
1045 ocrdma_set_qp_init_params(qp, pd, attrs);
1046
1047 mutex_lock(&dev->dev_lock);
1048 status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
1049 ureq.dpp_cq_id,
1050 &dpp_offset, &dpp_credit_lmt);
1051 if (status)
1052 goto mbx_err;
1053
1054 /* user space QP's wr_id table are managed in library */
1055 if (udata == NULL) {
1056 qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
1057 OCRDMA_QP_FAST_REG);
1058 status = ocrdma_alloc_wr_id_tbl(qp);
1059 if (status)
1060 goto map_err;
1061 }
1062
1063 status = ocrdma_add_qpn_map(dev, qp);
1064 if (status)
1065 goto map_err;
1066 ocrdma_set_qp_db(dev, qp, pd);
1067 if (udata) {
1068 status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
1069 dpp_credit_lmt,
1070 (attrs->srq != NULL));
1071 if (status)
1072 goto cpy_err;
1073 }
1074 ocrdma_store_gsi_qp_cq(dev, attrs);
Gottumukkala, Naresh27159f52013-06-05 08:50:46 +00001075 qp->ibqp.qp_num = qp->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301076 mutex_unlock(&dev->dev_lock);
1077 return &qp->ibqp;
1078
1079cpy_err:
1080 ocrdma_del_qpn_map(dev, qp);
1081map_err:
1082 ocrdma_mbx_destroy_qp(dev, qp);
1083mbx_err:
1084 mutex_unlock(&dev->dev_lock);
1085 kfree(qp->wqe_wr_id_tbl);
1086 kfree(qp->rqe_wr_id_tbl);
1087 kfree(qp);
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001088 pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
Parav Panditfe2caef2012-03-21 04:09:06 +05301089gen_err:
1090 return ERR_PTR(status);
1091}
1092
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +05301093
1094static void ocrdma_flush_rq_db(struct ocrdma_qp *qp)
1095{
1096 if (qp->db_cache) {
1097 u32 val = qp->rq.dbid | (qp->db_cache <<
1098 ocrdma_get_num_posted_shift(qp));
1099 iowrite32(val, qp->rq_db);
1100 qp->db_cache = 0;
1101 }
1102}
1103
Parav Panditfe2caef2012-03-21 04:09:06 +05301104int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1105 int attr_mask)
1106{
1107 int status = 0;
1108 struct ocrdma_qp *qp;
1109 struct ocrdma_dev *dev;
1110 enum ib_qp_state old_qps;
1111
1112 qp = get_ocrdma_qp(ibqp);
1113 dev = qp->dev;
1114 if (attr_mask & IB_QP_STATE)
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05301115 status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301116 /* if new and previous states are same hw doesn't need to
1117 * know about it.
1118 */
1119 if (status < 0)
1120 return status;
1121 status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask, old_qps);
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +05301122 if (!status && attr_mask & IB_QP_STATE && attr->qp_state == IB_QPS_RTR)
1123 ocrdma_flush_rq_db(qp);
1124
Parav Panditfe2caef2012-03-21 04:09:06 +05301125 return status;
1126}
1127
1128int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1129 int attr_mask, struct ib_udata *udata)
1130{
1131 unsigned long flags;
1132 int status = -EINVAL;
1133 struct ocrdma_qp *qp;
1134 struct ocrdma_dev *dev;
1135 enum ib_qp_state old_qps, new_qps;
1136
1137 qp = get_ocrdma_qp(ibqp);
1138 dev = qp->dev;
1139
1140 /* syncronize with multiple context trying to change, retrive qps */
1141 mutex_lock(&dev->dev_lock);
1142 /* syncronize with wqe, rqe posting and cqe processing contexts */
1143 spin_lock_irqsave(&qp->q_lock, flags);
1144 old_qps = get_ibqp_state(qp->state);
1145 if (attr_mask & IB_QP_STATE)
1146 new_qps = attr->qp_state;
1147 else
1148 new_qps = old_qps;
1149 spin_unlock_irqrestore(&qp->q_lock, flags);
1150
1151 if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask)) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001152 pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
1153 "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
1154 __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
1155 old_qps, new_qps);
Parav Panditfe2caef2012-03-21 04:09:06 +05301156 goto param_err;
1157 }
1158
1159 status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
1160 if (status > 0)
1161 status = 0;
1162param_err:
1163 mutex_unlock(&dev->dev_lock);
1164 return status;
1165}
1166
1167static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
1168{
1169 switch (mtu) {
1170 case 256:
1171 return IB_MTU_256;
1172 case 512:
1173 return IB_MTU_512;
1174 case 1024:
1175 return IB_MTU_1024;
1176 case 2048:
1177 return IB_MTU_2048;
1178 case 4096:
1179 return IB_MTU_4096;
1180 default:
1181 return IB_MTU_1024;
1182 }
1183}
1184
1185static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
1186{
1187 int ib_qp_acc_flags = 0;
1188
1189 if (qp_cap_flags & OCRDMA_QP_INB_WR)
1190 ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
1191 if (qp_cap_flags & OCRDMA_QP_INB_RD)
1192 ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
1193 return ib_qp_acc_flags;
1194}
1195
1196int ocrdma_query_qp(struct ib_qp *ibqp,
1197 struct ib_qp_attr *qp_attr,
1198 int attr_mask, struct ib_qp_init_attr *qp_init_attr)
1199{
1200 int status;
1201 u32 qp_state;
1202 struct ocrdma_qp_params params;
1203 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
1204 struct ocrdma_dev *dev = qp->dev;
1205
1206 memset(&params, 0, sizeof(params));
1207 mutex_lock(&dev->dev_lock);
1208 status = ocrdma_mbx_query_qp(dev, qp, &params);
1209 mutex_unlock(&dev->dev_lock);
1210 if (status)
1211 goto mbx_err;
1212 qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT);
1213 qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT);
1214 qp_attr->path_mtu =
1215 ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
1216 OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
1217 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
1218 qp_attr->path_mig_state = IB_MIG_MIGRATED;
1219 qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
1220 qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
1221 qp_attr->dest_qp_num =
1222 params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
1223
1224 qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
1225 qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
1226 qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
1227 qp_attr->cap.max_send_sge = qp->sq.max_sges;
1228 qp_attr->cap.max_recv_sge = qp->rq.max_sges;
1229 qp_attr->cap.max_inline_data = dev->attr.max_inline_data;
1230 qp_init_attr->cap = qp_attr->cap;
1231 memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
1232 sizeof(params.dgid));
1233 qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
1234 OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
1235 qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
1236 qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
1237 OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
1238 OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
1239 qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
1240 OCRDMA_QP_PARAMS_SQ_PSN_MASK) >>
1241 OCRDMA_QP_PARAMS_TCLASS_SHIFT;
1242
1243 qp_attr->ah_attr.ah_flags = IB_AH_GRH;
1244 qp_attr->ah_attr.port_num = 1;
1245 qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
1246 OCRDMA_QP_PARAMS_SL_MASK) >>
1247 OCRDMA_QP_PARAMS_SL_SHIFT;
1248 qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
1249 OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
1250 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
1251 qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
1252 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
1253 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
1254 qp_attr->retry_cnt =
1255 (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
1256 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
1257 qp_attr->min_rnr_timer = 0;
1258 qp_attr->pkey_index = 0;
1259 qp_attr->port_num = 1;
1260 qp_attr->ah_attr.src_path_bits = 0;
1261 qp_attr->ah_attr.static_rate = 0;
1262 qp_attr->alt_pkey_index = 0;
1263 qp_attr->alt_port_num = 0;
1264 qp_attr->alt_timeout = 0;
1265 memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
1266 qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
1267 OCRDMA_QP_PARAMS_STATE_SHIFT;
1268 qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
1269 qp_attr->max_dest_rd_atomic =
1270 params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
1271 qp_attr->max_rd_atomic =
1272 params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
1273 qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
1274 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
1275mbx_err:
1276 return status;
1277}
1278
1279static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx)
1280{
1281 int i = idx / 32;
1282 unsigned int mask = (1 << (idx % 32));
1283
1284 if (srq->idx_bit_fields[i] & mask)
1285 srq->idx_bit_fields[i] &= ~mask;
1286 else
1287 srq->idx_bit_fields[i] |= mask;
1288}
1289
1290static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
1291{
1292 int free_cnt;
1293 if (q->head >= q->tail)
1294 free_cnt = (q->max_cnt - q->head) + q->tail;
1295 else
1296 free_cnt = q->tail - q->head;
Parav Panditfe2caef2012-03-21 04:09:06 +05301297 return free_cnt;
1298}
1299
1300static int is_hw_sq_empty(struct ocrdma_qp *qp)
1301{
1302 return (qp->sq.tail == qp->sq.head &&
1303 ocrdma_hwq_free_cnt(&qp->sq) ? 1 : 0);
1304}
1305
1306static int is_hw_rq_empty(struct ocrdma_qp *qp)
1307{
1308 return (qp->rq.tail == qp->rq.head) ? 1 : 0;
1309}
1310
1311static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
1312{
1313 return q->va + (q->head * q->entry_size);
1314}
1315
1316static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
1317 u32 idx)
1318{
1319 return q->va + (idx * q->entry_size);
1320}
1321
1322static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
1323{
1324 q->head = (q->head + 1) & q->max_wqe_idx;
1325}
1326
1327static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
1328{
1329 q->tail = (q->tail + 1) & q->max_wqe_idx;
1330}
1331
1332/* discard the cqe for a given QP */
1333static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
1334{
1335 unsigned long cq_flags;
1336 unsigned long flags;
1337 int discard_cnt = 0;
1338 u32 cur_getp, stop_getp;
1339 struct ocrdma_cqe *cqe;
1340 u32 qpn = 0;
1341
1342 spin_lock_irqsave(&cq->cq_lock, cq_flags);
1343
1344 /* traverse through the CQEs in the hw CQ,
1345 * find the matching CQE for a given qp,
1346 * mark the matching one discarded by clearing qpn.
1347 * ring the doorbell in the poll_cq() as
1348 * we don't complete out of order cqe.
1349 */
1350
1351 cur_getp = cq->getp;
1352 /* find upto when do we reap the cq. */
1353 stop_getp = cur_getp;
1354 do {
1355 if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
1356 break;
1357
1358 cqe = cq->va + cur_getp;
1359 /* if (a) done reaping whole hw cq, or
1360 * (b) qp_xq becomes empty.
1361 * then exit
1362 */
1363 qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
1364 /* if previously discarded cqe found, skip that too. */
1365 /* check for matching qp */
1366 if (qpn == 0 || qpn != qp->id)
1367 goto skip_cqe;
1368
1369 /* mark cqe discarded so that it is not picked up later
1370 * in the poll_cq().
1371 */
1372 discard_cnt += 1;
1373 cqe->cmn.qpn = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301374 if (is_cqe_for_sq(cqe)) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301375 ocrdma_hwq_inc_tail(&qp->sq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301376 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301377 if (qp->srq) {
1378 spin_lock_irqsave(&qp->srq->q_lock, flags);
1379 ocrdma_hwq_inc_tail(&qp->srq->rq);
1380 ocrdma_srq_toggle_bit(qp->srq, cur_getp);
1381 spin_unlock_irqrestore(&qp->srq->q_lock, flags);
1382
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301383 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301384 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301385 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301386 }
1387skip_cqe:
1388 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
1389 } while (cur_getp != stop_getp);
1390 spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
1391}
1392
1393static void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
1394{
1395 int found = false;
1396 unsigned long flags;
1397 struct ocrdma_dev *dev = qp->dev;
1398 /* sync with any active CQ poll */
1399
1400 spin_lock_irqsave(&dev->flush_q_lock, flags);
1401 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1402 if (found)
1403 list_del(&qp->sq_entry);
1404 if (!qp->srq) {
1405 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1406 if (found)
1407 list_del(&qp->rq_entry);
1408 }
1409 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
1410}
1411
1412int ocrdma_destroy_qp(struct ib_qp *ibqp)
1413{
1414 int status;
1415 struct ocrdma_pd *pd;
1416 struct ocrdma_qp *qp;
1417 struct ocrdma_dev *dev;
1418 struct ib_qp_attr attrs;
1419 int attr_mask = IB_QP_STATE;
Dan Carpenterd19081e2012-05-02 09:14:47 +03001420 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05301421
1422 qp = get_ocrdma_qp(ibqp);
1423 dev = qp->dev;
1424
1425 attrs.qp_state = IB_QPS_ERR;
1426 pd = qp->pd;
1427
1428 /* change the QP state to ERROR */
1429 _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
1430
1431 /* ensure that CQEs for newly created QP (whose id may be same with
1432 * one which just getting destroyed are same), dont get
1433 * discarded until the old CQEs are discarded.
1434 */
1435 mutex_lock(&dev->dev_lock);
1436 status = ocrdma_mbx_destroy_qp(dev, qp);
1437
1438 /*
1439 * acquire CQ lock while destroy is in progress, in order to
1440 * protect against proessing in-flight CQEs for this QP.
1441 */
Dan Carpenterd19081e2012-05-02 09:14:47 +03001442 spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301443 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001444 spin_lock(&qp->rq_cq->cq_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301445
1446 ocrdma_del_qpn_map(dev, qp);
1447
1448 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001449 spin_unlock(&qp->rq_cq->cq_lock);
1450 spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301451
1452 if (!pd->uctx) {
1453 ocrdma_discard_cqes(qp, qp->sq_cq);
1454 ocrdma_discard_cqes(qp, qp->rq_cq);
1455 }
1456 mutex_unlock(&dev->dev_lock);
1457
1458 if (pd->uctx) {
1459 ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa, qp->sq.len);
1460 if (!qp->srq)
1461 ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa, qp->rq.len);
1462 }
1463
1464 ocrdma_del_flush_qp(qp);
1465
Parav Panditfe2caef2012-03-21 04:09:06 +05301466 kfree(qp->wqe_wr_id_tbl);
1467 kfree(qp->rqe_wr_id_tbl);
1468 kfree(qp);
1469 return status;
1470}
1471
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301472static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
1473 struct ib_udata *udata)
Parav Panditfe2caef2012-03-21 04:09:06 +05301474{
1475 int status;
1476 struct ocrdma_create_srq_uresp uresp;
1477
Dan Carpenter63ea3742013-07-29 22:34:29 +03001478 memset(&uresp, 0, sizeof(uresp));
Parav Panditfe2caef2012-03-21 04:09:06 +05301479 uresp.rq_dbid = srq->rq.dbid;
1480 uresp.num_rq_pages = 1;
1481 uresp.rq_page_addr[0] = srq->rq.pa;
1482 uresp.rq_page_size = srq->rq.len;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301483 uresp.db_page_addr = dev->nic_info.unmapped_db +
1484 (srq->pd->id * dev->nic_info.db_page_size);
1485 uresp.db_page_size = dev->nic_info.db_page_size;
Parav Panditfe2caef2012-03-21 04:09:06 +05301486 uresp.num_rqe_allocated = srq->rq.max_cnt;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301487 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
Parav Panditfe2caef2012-03-21 04:09:06 +05301488 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ1_OFFSET;
1489 uresp.db_shift = 24;
1490 } else {
1491 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
1492 uresp.db_shift = 16;
1493 }
1494
1495 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1496 if (status)
1497 return status;
1498 status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
1499 uresp.rq_page_size);
1500 if (status)
1501 return status;
1502 return status;
1503}
1504
1505struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
1506 struct ib_srq_init_attr *init_attr,
1507 struct ib_udata *udata)
1508{
1509 int status = -ENOMEM;
1510 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301511 struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301512 struct ocrdma_srq *srq;
1513
1514 if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
1515 return ERR_PTR(-EINVAL);
1516 if (init_attr->attr.max_wr > dev->attr.max_rqe)
1517 return ERR_PTR(-EINVAL);
1518
1519 srq = kzalloc(sizeof(*srq), GFP_KERNEL);
1520 if (!srq)
1521 return ERR_PTR(status);
1522
1523 spin_lock_init(&srq->q_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301524 srq->pd = pd;
1525 srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301526 status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
Parav Panditfe2caef2012-03-21 04:09:06 +05301527 if (status)
1528 goto err;
1529
1530 if (udata == NULL) {
1531 srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
1532 GFP_KERNEL);
1533 if (srq->rqe_wr_id_tbl == NULL)
1534 goto arm_err;
1535
1536 srq->bit_fields_len = (srq->rq.max_cnt / 32) +
1537 (srq->rq.max_cnt % 32 ? 1 : 0);
1538 srq->idx_bit_fields =
1539 kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
1540 if (srq->idx_bit_fields == NULL)
1541 goto arm_err;
1542 memset(srq->idx_bit_fields, 0xff,
1543 srq->bit_fields_len * sizeof(u32));
1544 }
1545
1546 if (init_attr->attr.srq_limit) {
1547 status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
1548 if (status)
1549 goto arm_err;
1550 }
1551
Parav Panditfe2caef2012-03-21 04:09:06 +05301552 if (udata) {
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301553 status = ocrdma_copy_srq_uresp(dev, srq, udata);
Parav Panditfe2caef2012-03-21 04:09:06 +05301554 if (status)
1555 goto arm_err;
1556 }
1557
Parav Panditfe2caef2012-03-21 04:09:06 +05301558 return &srq->ibsrq;
1559
1560arm_err:
1561 ocrdma_mbx_destroy_srq(dev, srq);
1562err:
1563 kfree(srq->rqe_wr_id_tbl);
1564 kfree(srq->idx_bit_fields);
1565 kfree(srq);
1566 return ERR_PTR(status);
1567}
1568
1569int ocrdma_modify_srq(struct ib_srq *ibsrq,
1570 struct ib_srq_attr *srq_attr,
1571 enum ib_srq_attr_mask srq_attr_mask,
1572 struct ib_udata *udata)
1573{
1574 int status = 0;
1575 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301576
1577 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301578 if (srq_attr_mask & IB_SRQ_MAX_WR)
1579 status = -EINVAL;
1580 else
1581 status = ocrdma_mbx_modify_srq(srq, srq_attr);
1582 return status;
1583}
1584
1585int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
1586{
1587 int status;
1588 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301589
1590 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301591 status = ocrdma_mbx_query_srq(srq, srq_attr);
1592 return status;
1593}
1594
1595int ocrdma_destroy_srq(struct ib_srq *ibsrq)
1596{
1597 int status;
1598 struct ocrdma_srq *srq;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05301599 struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05301600
1601 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301602
1603 status = ocrdma_mbx_destroy_srq(dev, srq);
1604
1605 if (srq->pd->uctx)
1606 ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa, srq->rq.len);
1607
Parav Panditfe2caef2012-03-21 04:09:06 +05301608 kfree(srq->idx_bit_fields);
1609 kfree(srq->rqe_wr_id_tbl);
1610 kfree(srq);
1611 return status;
1612}
1613
1614/* unprivileged verbs and their support functions. */
1615static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
1616 struct ocrdma_hdr_wqe *hdr,
1617 struct ib_send_wr *wr)
1618{
1619 struct ocrdma_ewqe_ud_hdr *ud_hdr =
1620 (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
1621 struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
1622
1623 ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
1624 if (qp->qp_type == IB_QPT_GSI)
1625 ud_hdr->qkey = qp->qkey;
1626 else
1627 ud_hdr->qkey = wr->wr.ud.remote_qkey;
1628 ud_hdr->rsvd_ahid = ah->id;
1629}
1630
1631static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
1632 struct ocrdma_sge *sge, int num_sge,
1633 struct ib_sge *sg_list)
1634{
1635 int i;
1636
1637 for (i = 0; i < num_sge; i++) {
1638 sge[i].lrkey = sg_list[i].lkey;
1639 sge[i].addr_lo = sg_list[i].addr;
1640 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
1641 sge[i].len = sg_list[i].length;
1642 hdr->total_len += sg_list[i].length;
1643 }
1644 if (num_sge == 0)
1645 memset(sge, 0, sizeof(*sge));
1646}
1647
1648static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
1649 struct ocrdma_hdr_wqe *hdr,
1650 struct ocrdma_sge *sge,
1651 struct ib_send_wr *wr, u32 wqe_size)
1652{
1653 if (wr->send_flags & IB_SEND_INLINE) {
1654 if (wr->sg_list[0].length > qp->max_inline_data) {
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00001655 pr_err("%s() supported_len=0x%x,\n"
1656 " unspported len req=0x%x\n", __func__,
1657 qp->max_inline_data, wr->sg_list[0].length);
Parav Panditfe2caef2012-03-21 04:09:06 +05301658 return -EINVAL;
1659 }
1660 memcpy(sge,
1661 (void *)(unsigned long)wr->sg_list[0].addr,
1662 wr->sg_list[0].length);
1663 hdr->total_len = wr->sg_list[0].length;
1664 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
1665 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
1666 } else {
1667 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1668 if (wr->num_sge)
1669 wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
1670 else
1671 wqe_size += sizeof(struct ocrdma_sge);
1672 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1673 }
1674 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
1675 return 0;
1676}
1677
1678static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1679 struct ib_send_wr *wr)
1680{
1681 int status;
1682 struct ocrdma_sge *sge;
1683 u32 wqe_size = sizeof(*hdr);
1684
1685 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
1686 ocrdma_build_ud_hdr(qp, hdr, wr);
1687 sge = (struct ocrdma_sge *)(hdr + 2);
1688 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301689 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05301690 sge = (struct ocrdma_sge *)(hdr + 1);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301691 }
Parav Panditfe2caef2012-03-21 04:09:06 +05301692
1693 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1694 return status;
1695}
1696
1697static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1698 struct ib_send_wr *wr)
1699{
1700 int status;
1701 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
1702 struct ocrdma_sge *sge = ext_rw + 1;
1703 u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
1704
1705 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1706 if (status)
1707 return status;
1708 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
1709 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
1710 ext_rw->lrkey = wr->wr.rdma.rkey;
1711 ext_rw->len = hdr->total_len;
1712 return 0;
1713}
1714
1715static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1716 struct ib_send_wr *wr)
1717{
1718 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
1719 struct ocrdma_sge *sge = ext_rw + 1;
1720 u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
1721 sizeof(struct ocrdma_hdr_wqe);
1722
1723 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1724 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
1725 hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
1726 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1727
1728 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
1729 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
1730 ext_rw->lrkey = wr->wr.rdma.rkey;
1731 ext_rw->len = hdr->total_len;
1732}
1733
1734static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
1735{
1736 u32 val = qp->sq.dbid | (1 << 16);
1737
1738 iowrite32(val, qp->sq_db);
1739}
1740
1741int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1742 struct ib_send_wr **bad_wr)
1743{
1744 int status = 0;
1745 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
1746 struct ocrdma_hdr_wqe *hdr;
1747 unsigned long flags;
1748
1749 spin_lock_irqsave(&qp->q_lock, flags);
1750 if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
1751 spin_unlock_irqrestore(&qp->q_lock, flags);
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00001752 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05301753 return -EINVAL;
1754 }
1755
1756 while (wr) {
1757 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
1758 wr->num_sge > qp->sq.max_sges) {
Naresh Gottumukkalaf6ddcf72013-06-10 04:42:40 +00001759 *bad_wr = wr;
Parav Panditfe2caef2012-03-21 04:09:06 +05301760 status = -ENOMEM;
1761 break;
1762 }
1763 hdr = ocrdma_hwq_head(&qp->sq);
1764 hdr->cw = 0;
1765 if (wr->send_flags & IB_SEND_SIGNALED)
1766 hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
1767 if (wr->send_flags & IB_SEND_FENCE)
1768 hdr->cw |=
1769 (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
1770 if (wr->send_flags & IB_SEND_SOLICITED)
1771 hdr->cw |=
1772 (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
1773 hdr->total_len = 0;
1774 switch (wr->opcode) {
1775 case IB_WR_SEND_WITH_IMM:
1776 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
1777 hdr->immdt = ntohl(wr->ex.imm_data);
1778 case IB_WR_SEND:
1779 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
1780 ocrdma_build_send(qp, hdr, wr);
1781 break;
1782 case IB_WR_SEND_WITH_INV:
1783 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
1784 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
1785 hdr->lkey = wr->ex.invalidate_rkey;
1786 status = ocrdma_build_send(qp, hdr, wr);
1787 break;
1788 case IB_WR_RDMA_WRITE_WITH_IMM:
1789 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
1790 hdr->immdt = ntohl(wr->ex.imm_data);
1791 case IB_WR_RDMA_WRITE:
1792 hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
1793 status = ocrdma_build_write(qp, hdr, wr);
1794 break;
1795 case IB_WR_RDMA_READ_WITH_INV:
1796 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
1797 case IB_WR_RDMA_READ:
1798 ocrdma_build_read(qp, hdr, wr);
1799 break;
1800 case IB_WR_LOCAL_INV:
1801 hdr->cw |=
1802 (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
1803 hdr->cw |= (sizeof(struct ocrdma_hdr_wqe) /
1804 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
1805 hdr->lkey = wr->ex.invalidate_rkey;
1806 break;
1807 default:
1808 status = -EINVAL;
1809 break;
1810 }
1811 if (status) {
1812 *bad_wr = wr;
1813 break;
1814 }
1815 if (wr->send_flags & IB_SEND_SIGNALED)
1816 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
1817 else
1818 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
1819 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
1820 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
1821 OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
1822 /* make sure wqe is written before adapter can access it */
1823 wmb();
1824 /* inform hw to start processing it */
1825 ocrdma_ring_sq_db(qp);
1826
1827 /* update pointer, counter for next wr */
1828 ocrdma_hwq_inc_head(&qp->sq);
1829 wr = wr->next;
1830 }
1831 spin_unlock_irqrestore(&qp->q_lock, flags);
1832 return status;
1833}
1834
1835static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
1836{
Naresh Gottumukkaladf176ea2013-06-10 04:42:41 +00001837 u32 val = qp->rq.dbid | (1 << ocrdma_get_num_posted_shift(qp));
Parav Panditfe2caef2012-03-21 04:09:06 +05301838
Naresh Gottumukkala45e86b32013-08-07 12:52:37 +05301839 if (qp->state != OCRDMA_QPS_INIT)
1840 iowrite32(val, qp->rq_db);
1841 else
1842 qp->db_cache++;
Parav Panditfe2caef2012-03-21 04:09:06 +05301843}
1844
1845static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
1846 u16 tag)
1847{
1848 u32 wqe_size = 0;
1849 struct ocrdma_sge *sge;
1850 if (wr->num_sge)
1851 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
1852 else
1853 wqe_size = sizeof(*sge) + sizeof(*rqe);
1854
1855 rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
1856 OCRDMA_WQE_SIZE_SHIFT);
1857 rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
1858 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1859 rqe->total_len = 0;
1860 rqe->rsvd_tag = tag;
1861 sge = (struct ocrdma_sge *)(rqe + 1);
1862 ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
1863 ocrdma_cpu_to_le32(rqe, wqe_size);
1864}
1865
1866int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1867 struct ib_recv_wr **bad_wr)
1868{
1869 int status = 0;
1870 unsigned long flags;
1871 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
1872 struct ocrdma_hdr_wqe *rqe;
1873
1874 spin_lock_irqsave(&qp->q_lock, flags);
1875 if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
1876 spin_unlock_irqrestore(&qp->q_lock, flags);
1877 *bad_wr = wr;
1878 return -EINVAL;
1879 }
1880 while (wr) {
1881 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
1882 wr->num_sge > qp->rq.max_sges) {
1883 *bad_wr = wr;
1884 status = -ENOMEM;
1885 break;
1886 }
1887 rqe = ocrdma_hwq_head(&qp->rq);
1888 ocrdma_build_rqe(rqe, wr, 0);
1889
1890 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
1891 /* make sure rqe is written before adapter can access it */
1892 wmb();
1893
1894 /* inform hw to start processing it */
1895 ocrdma_ring_rq_db(qp);
1896
1897 /* update pointer, counter for next wr */
1898 ocrdma_hwq_inc_head(&qp->rq);
1899 wr = wr->next;
1900 }
1901 spin_unlock_irqrestore(&qp->q_lock, flags);
1902 return status;
1903}
1904
1905/* cqe for srq's rqe can potentially arrive out of order.
1906 * index gives the entry in the shadow table where to store
1907 * the wr_id. tag/index is returned in cqe to reference back
1908 * for a given rqe.
1909 */
1910static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
1911{
1912 int row = 0;
1913 int indx = 0;
1914
1915 for (row = 0; row < srq->bit_fields_len; row++) {
1916 if (srq->idx_bit_fields[row]) {
1917 indx = ffs(srq->idx_bit_fields[row]);
1918 indx = (row * 32) + (indx - 1);
1919 if (indx >= srq->rq.max_cnt)
1920 BUG();
1921 ocrdma_srq_toggle_bit(srq, indx);
1922 break;
1923 }
1924 }
1925
1926 if (row == srq->bit_fields_len)
1927 BUG();
1928 return indx;
1929}
1930
1931static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
1932{
1933 u32 val = srq->rq.dbid | (1 << 16);
1934
1935 iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
1936}
1937
1938int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1939 struct ib_recv_wr **bad_wr)
1940{
1941 int status = 0;
1942 unsigned long flags;
1943 struct ocrdma_srq *srq;
1944 struct ocrdma_hdr_wqe *rqe;
1945 u16 tag;
1946
1947 srq = get_ocrdma_srq(ibsrq);
1948
1949 spin_lock_irqsave(&srq->q_lock, flags);
1950 while (wr) {
1951 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
1952 wr->num_sge > srq->rq.max_sges) {
1953 status = -ENOMEM;
1954 *bad_wr = wr;
1955 break;
1956 }
1957 tag = ocrdma_srq_get_idx(srq);
1958 rqe = ocrdma_hwq_head(&srq->rq);
1959 ocrdma_build_rqe(rqe, wr, tag);
1960
1961 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
1962 /* make sure rqe is written before adapter can perform DMA */
1963 wmb();
1964 /* inform hw to start processing it */
1965 ocrdma_ring_srq_db(srq);
1966 /* update pointer, counter for next wr */
1967 ocrdma_hwq_inc_head(&srq->rq);
1968 wr = wr->next;
1969 }
1970 spin_unlock_irqrestore(&srq->q_lock, flags);
1971 return status;
1972}
1973
1974static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
1975{
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05301976 enum ib_wc_status ibwc_status;
Parav Panditfe2caef2012-03-21 04:09:06 +05301977
1978 switch (status) {
1979 case OCRDMA_CQE_GENERAL_ERR:
1980 ibwc_status = IB_WC_GENERAL_ERR;
1981 break;
1982 case OCRDMA_CQE_LOC_LEN_ERR:
1983 ibwc_status = IB_WC_LOC_LEN_ERR;
1984 break;
1985 case OCRDMA_CQE_LOC_QP_OP_ERR:
1986 ibwc_status = IB_WC_LOC_QP_OP_ERR;
1987 break;
1988 case OCRDMA_CQE_LOC_EEC_OP_ERR:
1989 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
1990 break;
1991 case OCRDMA_CQE_LOC_PROT_ERR:
1992 ibwc_status = IB_WC_LOC_PROT_ERR;
1993 break;
1994 case OCRDMA_CQE_WR_FLUSH_ERR:
1995 ibwc_status = IB_WC_WR_FLUSH_ERR;
1996 break;
1997 case OCRDMA_CQE_MW_BIND_ERR:
1998 ibwc_status = IB_WC_MW_BIND_ERR;
1999 break;
2000 case OCRDMA_CQE_BAD_RESP_ERR:
2001 ibwc_status = IB_WC_BAD_RESP_ERR;
2002 break;
2003 case OCRDMA_CQE_LOC_ACCESS_ERR:
2004 ibwc_status = IB_WC_LOC_ACCESS_ERR;
2005 break;
2006 case OCRDMA_CQE_REM_INV_REQ_ERR:
2007 ibwc_status = IB_WC_REM_INV_REQ_ERR;
2008 break;
2009 case OCRDMA_CQE_REM_ACCESS_ERR:
2010 ibwc_status = IB_WC_REM_ACCESS_ERR;
2011 break;
2012 case OCRDMA_CQE_REM_OP_ERR:
2013 ibwc_status = IB_WC_REM_OP_ERR;
2014 break;
2015 case OCRDMA_CQE_RETRY_EXC_ERR:
2016 ibwc_status = IB_WC_RETRY_EXC_ERR;
2017 break;
2018 case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2019 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2020 break;
2021 case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2022 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2023 break;
2024 case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2025 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2026 break;
2027 case OCRDMA_CQE_REM_ABORT_ERR:
2028 ibwc_status = IB_WC_REM_ABORT_ERR;
2029 break;
2030 case OCRDMA_CQE_INV_EECN_ERR:
2031 ibwc_status = IB_WC_INV_EECN_ERR;
2032 break;
2033 case OCRDMA_CQE_INV_EEC_STATE_ERR:
2034 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2035 break;
2036 case OCRDMA_CQE_FATAL_ERR:
2037 ibwc_status = IB_WC_FATAL_ERR;
2038 break;
2039 case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2040 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2041 break;
2042 default:
2043 ibwc_status = IB_WC_GENERAL_ERR;
2044 break;
2045 };
2046 return ibwc_status;
2047}
2048
2049static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2050 u32 wqe_idx)
2051{
2052 struct ocrdma_hdr_wqe *hdr;
2053 struct ocrdma_sge *rw;
2054 int opcode;
2055
2056 hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2057
2058 ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2059 /* Undo the hdr->cw swap */
2060 opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2061 switch (opcode) {
2062 case OCRDMA_WRITE:
2063 ibwc->opcode = IB_WC_RDMA_WRITE;
2064 break;
2065 case OCRDMA_READ:
2066 rw = (struct ocrdma_sge *)(hdr + 1);
2067 ibwc->opcode = IB_WC_RDMA_READ;
2068 ibwc->byte_len = rw->len;
2069 break;
2070 case OCRDMA_SEND:
2071 ibwc->opcode = IB_WC_SEND;
2072 break;
2073 case OCRDMA_LKEY_INV:
2074 ibwc->opcode = IB_WC_LOCAL_INV;
2075 break;
2076 default:
2077 ibwc->status = IB_WC_GENERAL_ERR;
Naresh Gottumukkalaef99c4c2013-06-10 04:42:39 +00002078 pr_err("%s() invalid opcode received = 0x%x\n",
2079 __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
Parav Panditfe2caef2012-03-21 04:09:06 +05302080 break;
2081 };
2082}
2083
2084static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2085 struct ocrdma_cqe *cqe)
2086{
2087 if (is_cqe_for_sq(cqe)) {
2088 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2089 cqe->flags_status_srcqpn) &
2090 ~OCRDMA_CQE_STATUS_MASK);
2091 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2092 cqe->flags_status_srcqpn) |
2093 (OCRDMA_CQE_WR_FLUSH_ERR <<
2094 OCRDMA_CQE_STATUS_SHIFT));
2095 } else {
2096 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2097 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2098 cqe->flags_status_srcqpn) &
2099 ~OCRDMA_CQE_UD_STATUS_MASK);
2100 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2101 cqe->flags_status_srcqpn) |
2102 (OCRDMA_CQE_WR_FLUSH_ERR <<
2103 OCRDMA_CQE_UD_STATUS_SHIFT));
2104 } else {
2105 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2106 cqe->flags_status_srcqpn) &
2107 ~OCRDMA_CQE_STATUS_MASK);
2108 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2109 cqe->flags_status_srcqpn) |
2110 (OCRDMA_CQE_WR_FLUSH_ERR <<
2111 OCRDMA_CQE_STATUS_SHIFT));
2112 }
2113 }
2114}
2115
2116static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2117 struct ocrdma_qp *qp, int status)
2118{
2119 bool expand = false;
2120
2121 ibwc->byte_len = 0;
2122 ibwc->qp = &qp->ibqp;
2123 ibwc->status = ocrdma_to_ibwc_err(status);
2124
2125 ocrdma_flush_qp(qp);
Naresh Gottumukkala057729c2013-08-07 12:52:35 +05302126 ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
Parav Panditfe2caef2012-03-21 04:09:06 +05302127
2128 /* if wqe/rqe pending for which cqe needs to be returned,
2129 * trigger inflating it.
2130 */
2131 if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2132 expand = true;
2133 ocrdma_set_cqe_status_flushed(qp, cqe);
2134 }
2135 return expand;
2136}
2137
2138static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2139 struct ocrdma_qp *qp, int status)
2140{
2141 ibwc->opcode = IB_WC_RECV;
2142 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2143 ocrdma_hwq_inc_tail(&qp->rq);
2144
2145 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2146}
2147
2148static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2149 struct ocrdma_qp *qp, int status)
2150{
2151 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2152 ocrdma_hwq_inc_tail(&qp->sq);
2153
2154 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2155}
2156
2157
2158static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2159 struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2160 bool *polled, bool *stop)
2161{
2162 bool expand;
2163 int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2164 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2165
2166 /* when hw sq is empty, but rq is not empty, so we continue
2167 * to keep the cqe in order to get the cq event again.
2168 */
2169 if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2170 /* when cq for rq and sq is same, it is safe to return
2171 * flush cqe for RQEs.
2172 */
2173 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2174 *polled = true;
2175 status = OCRDMA_CQE_WR_FLUSH_ERR;
2176 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2177 } else {
2178 /* stop processing further cqe as this cqe is used for
2179 * triggering cq event on buddy cq of RQ.
2180 * When QP is destroyed, this cqe will be removed
2181 * from the cq's hardware q.
2182 */
2183 *polled = false;
2184 *stop = true;
2185 expand = false;
2186 }
2187 } else {
2188 *polled = true;
2189 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2190 }
2191 return expand;
2192}
2193
2194static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2195 struct ocrdma_cqe *cqe,
2196 struct ib_wc *ibwc, bool *polled)
2197{
2198 bool expand = false;
2199 int tail = qp->sq.tail;
2200 u32 wqe_idx;
2201
2202 if (!qp->wqe_wr_id_tbl[tail].signaled) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302203 *polled = false; /* WC cannot be consumed yet */
2204 } else {
2205 ibwc->status = IB_WC_SUCCESS;
2206 ibwc->wc_flags = 0;
2207 ibwc->qp = &qp->ibqp;
2208 ocrdma_update_wc(qp, ibwc, tail);
2209 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302210 }
Parav Panditae3bca92012-08-17 14:45:33 +00002211 wqe_idx = le32_to_cpu(cqe->wq.wqeidx) & OCRDMA_CQE_WQEIDX_MASK;
2212 if (tail != wqe_idx)
2213 expand = true; /* Coalesced CQE can't be consumed yet */
2214
Parav Panditfe2caef2012-03-21 04:09:06 +05302215 ocrdma_hwq_inc_tail(&qp->sq);
2216 return expand;
2217}
2218
2219static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2220 struct ib_wc *ibwc, bool *polled, bool *stop)
2221{
2222 int status;
2223 bool expand;
2224
2225 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2226 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2227
2228 if (status == OCRDMA_CQE_SUCCESS)
2229 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2230 else
2231 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2232 return expand;
2233}
2234
2235static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
2236{
2237 int status;
2238
2239 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2240 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2241 ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2242 OCRDMA_CQE_SRCQP_MASK;
2243 ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
2244 OCRDMA_CQE_PKEY_MASK;
2245 ibwc->wc_flags = IB_WC_GRH;
2246 ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2247 OCRDMA_CQE_UD_XFER_LEN_SHIFT);
2248 return status;
2249}
2250
2251static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2252 struct ocrdma_cqe *cqe,
2253 struct ocrdma_qp *qp)
2254{
2255 unsigned long flags;
2256 struct ocrdma_srq *srq;
2257 u32 wqe_idx;
2258
2259 srq = get_ocrdma_srq(qp->ibqp.srq);
2260 wqe_idx = le32_to_cpu(cqe->rq.buftag_qpn) >> OCRDMA_CQE_BUFTAG_SHIFT;
2261 ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2262 spin_lock_irqsave(&srq->q_lock, flags);
2263 ocrdma_srq_toggle_bit(srq, wqe_idx);
2264 spin_unlock_irqrestore(&srq->q_lock, flags);
2265 ocrdma_hwq_inc_tail(&srq->rq);
2266}
2267
2268static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2269 struct ib_wc *ibwc, bool *polled, bool *stop,
2270 int status)
2271{
2272 bool expand;
2273
2274 /* when hw_rq is empty, but wq is not empty, so continue
2275 * to keep the cqe to get the cq event again.
2276 */
2277 if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2278 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2279 *polled = true;
2280 status = OCRDMA_CQE_WR_FLUSH_ERR;
2281 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2282 } else {
2283 *polled = false;
2284 *stop = true;
2285 expand = false;
2286 }
Parav Pandita3698a92012-06-11 16:39:20 +05302287 } else {
2288 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302289 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
Parav Pandita3698a92012-06-11 16:39:20 +05302290 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302291 return expand;
2292}
2293
2294static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2295 struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2296{
2297 ibwc->opcode = IB_WC_RECV;
2298 ibwc->qp = &qp->ibqp;
2299 ibwc->status = IB_WC_SUCCESS;
2300
2301 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
2302 ocrdma_update_ud_rcqe(ibwc, cqe);
2303 else
2304 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2305
2306 if (is_cqe_imm(cqe)) {
2307 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2308 ibwc->wc_flags |= IB_WC_WITH_IMM;
2309 } else if (is_cqe_wr_imm(cqe)) {
2310 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2311 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2312 ibwc->wc_flags |= IB_WC_WITH_IMM;
2313 } else if (is_cqe_invalidated(cqe)) {
2314 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2315 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2316 }
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302317 if (qp->ibqp.srq) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302318 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302319 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302320 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2321 ocrdma_hwq_inc_tail(&qp->rq);
2322 }
2323}
2324
2325static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2326 struct ib_wc *ibwc, bool *polled, bool *stop)
2327{
2328 int status;
2329 bool expand = false;
2330
2331 ibwc->wc_flags = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302332 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302333 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2334 OCRDMA_CQE_UD_STATUS_MASK) >>
2335 OCRDMA_CQE_UD_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302336 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302337 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2338 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302339 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302340
2341 if (status == OCRDMA_CQE_SUCCESS) {
2342 *polled = true;
2343 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2344 } else {
2345 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2346 status);
2347 }
2348 return expand;
2349}
2350
2351static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2352 u16 cur_getp)
2353{
2354 if (cq->phase_change) {
2355 if (cur_getp == 0)
2356 cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302357 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302358 /* clear valid bit */
2359 cqe->flags_status_srcqpn = 0;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302360 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302361}
2362
2363static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2364 struct ib_wc *ibwc)
2365{
2366 u16 qpn = 0;
2367 int i = 0;
2368 bool expand = false;
2369 int polled_hw_cqes = 0;
2370 struct ocrdma_qp *qp = NULL;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302371 struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302372 struct ocrdma_cqe *cqe;
2373 u16 cur_getp; bool polled = false; bool stop = false;
2374
2375 cur_getp = cq->getp;
2376 while (num_entries) {
2377 cqe = cq->va + cur_getp;
2378 /* check whether valid cqe or not */
2379 if (!is_cqe_valid(cq, cqe))
2380 break;
2381 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2382 /* ignore discarded cqe */
2383 if (qpn == 0)
2384 goto skip_cqe;
2385 qp = dev->qp_tbl[qpn];
2386 BUG_ON(qp == NULL);
2387
2388 if (is_cqe_for_sq(cqe)) {
2389 expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2390 &stop);
2391 } else {
2392 expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2393 &stop);
2394 }
2395 if (expand)
2396 goto expand_cqe;
2397 if (stop)
2398 goto stop_cqe;
2399 /* clear qpn to avoid duplicate processing by discard_cqe() */
2400 cqe->cmn.qpn = 0;
2401skip_cqe:
2402 polled_hw_cqes += 1;
2403 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2404 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2405expand_cqe:
2406 if (polled) {
2407 num_entries -= 1;
2408 i += 1;
2409 ibwc = ibwc + 1;
2410 polled = false;
2411 }
2412 }
2413stop_cqe:
2414 cq->getp = cur_getp;
2415 if (polled_hw_cqes || expand || stop) {
2416 ocrdma_ring_cq_db(dev, cq->id, cq->armed, cq->solicited,
2417 polled_hw_cqes);
2418 }
2419 return i;
2420}
2421
2422/* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2423static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2424 struct ocrdma_qp *qp, struct ib_wc *ibwc)
2425{
2426 int err_cqes = 0;
2427
2428 while (num_entries) {
2429 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2430 break;
2431 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2432 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2433 ocrdma_hwq_inc_tail(&qp->sq);
2434 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2435 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2436 ocrdma_hwq_inc_tail(&qp->rq);
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302437 } else {
Parav Panditfe2caef2012-03-21 04:09:06 +05302438 return err_cqes;
Naresh Gottumukkalaf99b1642013-08-07 12:52:32 +05302439 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302440 ibwc->byte_len = 0;
2441 ibwc->status = IB_WC_WR_FLUSH_ERR;
2442 ibwc = ibwc + 1;
2443 err_cqes += 1;
2444 num_entries -= 1;
2445 }
2446 return err_cqes;
2447}
2448
2449int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2450{
2451 int cqes_to_poll = num_entries;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302452 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2453 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302454 int num_os_cqe = 0, err_cqes = 0;
2455 struct ocrdma_qp *qp;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302456 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05302457
2458 /* poll cqes from adapter CQ */
2459 spin_lock_irqsave(&cq->cq_lock, flags);
2460 num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2461 spin_unlock_irqrestore(&cq->cq_lock, flags);
2462 cqes_to_poll -= num_os_cqe;
2463
2464 if (cqes_to_poll) {
2465 wc = wc + num_os_cqe;
2466 /* adapter returns single error cqe when qp moves to
2467 * error state. So insert error cqes with wc_status as
2468 * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2469 * respectively which uses this CQ.
2470 */
2471 spin_lock_irqsave(&dev->flush_q_lock, flags);
2472 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2473 if (cqes_to_poll == 0)
2474 break;
2475 err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2476 cqes_to_poll -= err_cqes;
2477 num_os_cqe += err_cqes;
2478 wc = wc + err_cqes;
2479 }
2480 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2481 }
2482 return num_os_cqe;
2483}
2484
2485int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2486{
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302487 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2488 struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
Parav Panditfe2caef2012-03-21 04:09:06 +05302489 u16 cq_id;
2490 u16 cur_getp;
2491 struct ocrdma_cqe *cqe;
Naresh Gottumukkala1afc0452013-08-07 12:52:33 +05302492 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05302493
Parav Panditfe2caef2012-03-21 04:09:06 +05302494 cq_id = cq->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05302495
2496 spin_lock_irqsave(&cq->cq_lock, flags);
2497 if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
2498 cq->armed = true;
2499 if (cq_flags & IB_CQ_SOLICITED)
2500 cq->solicited = true;
2501
2502 cur_getp = cq->getp;
2503 cqe = cq->va + cur_getp;
2504
2505 /* check whether any valid cqe exist or not, if not then safe to
2506 * arm. If cqe is not yet consumed, then let it get consumed and then
2507 * we arm it to avoid false interrupts.
2508 */
2509 if (!is_cqe_valid(cq, cqe) || cq->arm_needed) {
2510 cq->arm_needed = false;
2511 ocrdma_ring_cq_db(dev, cq_id, cq->armed, cq->solicited, 0);
2512 }
2513 spin_unlock_irqrestore(&cq->cq_lock, flags);
2514 return 0;
2515}