blob: 38c145b28f5c87cb21bf9f1494111b7e80993ab7 [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);
87 attr->max_sge_rd = 0;
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) {
117 ocrdma_err("%s(%d) invalid_port=0x%x\n", __func__,
118 dev->id, port);
119 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) {
158 ocrdma_err("%s(%d) invalid_port=0x%x\n", __func__,
159 dev->id, port);
160 return -EINVAL;
161 }
162 return 0;
163}
164
165static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
166 unsigned long len)
167{
168 struct ocrdma_mm *mm;
169
170 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
171 if (mm == NULL)
172 return -ENOMEM;
173 mm->key.phy_addr = phy_addr;
174 mm->key.len = len;
175 INIT_LIST_HEAD(&mm->entry);
176
177 mutex_lock(&uctx->mm_list_lock);
178 list_add_tail(&mm->entry, &uctx->mm_head);
179 mutex_unlock(&uctx->mm_list_lock);
180 return 0;
181}
182
183static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
184 unsigned long len)
185{
186 struct ocrdma_mm *mm, *tmp;
187
188 mutex_lock(&uctx->mm_list_lock);
189 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
190 if (len != mm->key.len || phy_addr != mm->key.phy_addr)
191 continue;
192
193 list_del(&mm->entry);
194 kfree(mm);
195 break;
196 }
197 mutex_unlock(&uctx->mm_list_lock);
198}
199
200static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
201 unsigned long len)
202{
203 bool found = false;
204 struct ocrdma_mm *mm;
205
206 mutex_lock(&uctx->mm_list_lock);
207 list_for_each_entry(mm, &uctx->mm_head, entry) {
208 if (len != mm->key.len || phy_addr != mm->key.phy_addr)
209 continue;
210
211 found = true;
212 break;
213 }
214 mutex_unlock(&uctx->mm_list_lock);
215 return found;
216}
217
218struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
219 struct ib_udata *udata)
220{
221 int status;
222 struct ocrdma_ucontext *ctx;
223 struct ocrdma_alloc_ucontext_resp resp;
224 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
225 struct pci_dev *pdev = dev->nic_info.pdev;
226 u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
227
228 if (!udata)
229 return ERR_PTR(-EFAULT);
230 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
231 if (!ctx)
232 return ERR_PTR(-ENOMEM);
233 ctx->dev = dev;
234 INIT_LIST_HEAD(&ctx->mm_head);
235 mutex_init(&ctx->mm_list_lock);
236
237 ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
238 &ctx->ah_tbl.pa, GFP_KERNEL);
239 if (!ctx->ah_tbl.va) {
240 kfree(ctx);
241 return ERR_PTR(-ENOMEM);
242 }
243 memset(ctx->ah_tbl.va, 0, map_len);
244 ctx->ah_tbl.len = map_len;
245
246 resp.ah_tbl_len = ctx->ah_tbl.len;
247 resp.ah_tbl_page = ctx->ah_tbl.pa;
248
249 status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
250 if (status)
251 goto map_err;
252 resp.dev_id = dev->id;
253 resp.max_inline_data = dev->attr.max_inline_data;
254 resp.wqe_size = dev->attr.wqe_size;
255 resp.rqe_size = dev->attr.rqe_size;
256 resp.dpp_wqe_size = dev->attr.wqe_size;
257 resp.rsvd = 0;
258
259 memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
260 status = ib_copy_to_udata(udata, &resp, sizeof(resp));
261 if (status)
262 goto cpy_err;
263 return &ctx->ibucontext;
264
265cpy_err:
266 ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
267map_err:
268 dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
269 ctx->ah_tbl.pa);
270 kfree(ctx);
271 return ERR_PTR(status);
272}
273
274int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
275{
276 struct ocrdma_mm *mm, *tmp;
277 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
278 struct pci_dev *pdev = uctx->dev->nic_info.pdev;
279
280 ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
281 dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
282 uctx->ah_tbl.pa);
283
284 list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
285 list_del(&mm->entry);
286 kfree(mm);
287 }
288 kfree(uctx);
289 return 0;
290}
291
292int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
293{
294 struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
295 struct ocrdma_dev *dev = ucontext->dev;
296 unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
297 u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
298 unsigned long len = (vma->vm_end - vma->vm_start);
299 int status = 0;
300 bool found;
301
302 if (vma->vm_start & (PAGE_SIZE - 1))
303 return -EINVAL;
304 found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
305 if (!found)
306 return -EINVAL;
307
308 if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
309 dev->nic_info.db_total_size)) &&
310 (len <= dev->nic_info.db_page_size)) {
311 /* doorbell mapping */
312 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
313 len, vma->vm_page_prot);
314 } else if (dev->nic_info.dpp_unmapped_len &&
315 (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
316 (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
317 dev->nic_info.dpp_unmapped_len)) &&
318 (len <= dev->nic_info.dpp_unmapped_len)) {
319 /* dpp area mapping */
320 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
321 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
322 len, vma->vm_page_prot);
323 } else {
324 /* queue memory mapping */
325 status = remap_pfn_range(vma, vma->vm_start,
326 vma->vm_pgoff, len, vma->vm_page_prot);
327 }
328 return status;
329}
330
331static int ocrdma_copy_pd_uresp(struct ocrdma_pd *pd,
332 struct ib_ucontext *ib_ctx,
333 struct ib_udata *udata)
334{
335 int status;
336 u64 db_page_addr;
Roland Dreierda496432012-04-16 11:32:17 -0700337 u64 dpp_page_addr = 0;
Parav Panditfe2caef2012-03-21 04:09:06 +0530338 u32 db_page_size;
339 struct ocrdma_alloc_pd_uresp rsp;
340 struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
341
342 rsp.id = pd->id;
343 rsp.dpp_enabled = pd->dpp_enabled;
344 db_page_addr = pd->dev->nic_info.unmapped_db +
345 (pd->id * pd->dev->nic_info.db_page_size);
346 db_page_size = pd->dev->nic_info.db_page_size;
347
348 status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
349 if (status)
350 return status;
351
352 if (pd->dpp_enabled) {
353 dpp_page_addr = pd->dev->nic_info.dpp_unmapped_addr +
354 (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);
389 pd->dev = dev;
390 if (udata && context) {
391 pd->dpp_enabled = (dev->nic_info.dev_family ==
392 OCRDMA_GEN2_FAMILY) ? true : false;
393 pd->num_dpp_qp =
394 pd->dpp_enabled ? OCRDMA_PD_MAX_DPP_ENABLED_QP : 0;
395 }
396 status = ocrdma_mbx_alloc_pd(dev, pd);
397 if (status) {
398 kfree(pd);
399 return ERR_PTR(status);
400 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530401
402 if (udata && context) {
403 status = ocrdma_copy_pd_uresp(pd, context, udata);
404 if (status)
405 goto err;
406 }
407 return &pd->ibpd;
408
409err:
410 ocrdma_dealloc_pd(&pd->ibpd);
411 return ERR_PTR(status);
412}
413
414int ocrdma_dealloc_pd(struct ib_pd *ibpd)
415{
416 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
417 struct ocrdma_dev *dev = pd->dev;
418 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
435static struct ocrdma_mr *ocrdma_alloc_lkey(struct ib_pd *ibpd,
436 int acc, u32 num_pbls,
437 u32 addr_check)
438{
439 int status;
440 struct ocrdma_mr *mr;
441 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
442 struct ocrdma_dev *dev = pd->dev;
443
444 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
445 ocrdma_err("%s(%d) leaving err, invalid access rights\n",
446 __func__, dev->id);
447 return ERR_PTR(-EINVAL);
448 }
449
450 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
451 if (!mr)
452 return ERR_PTR(-ENOMEM);
453 mr->hwmr.dev = dev;
454 mr->hwmr.fr_mr = 0;
455 mr->hwmr.local_rd = 1;
456 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
457 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
458 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
459 mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
460 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
461 mr->hwmr.num_pbls = num_pbls;
462
463 status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pd->id, addr_check);
464 if (status) {
465 kfree(mr);
466 return ERR_PTR(-ENOMEM);
467 }
468 mr->pd = pd;
Parav Panditfe2caef2012-03-21 04:09:06 +0530469 mr->ibmr.lkey = mr->hwmr.lkey;
470 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
471 mr->ibmr.rkey = mr->hwmr.lkey;
472 return mr;
473}
474
475struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
476{
477 struct ocrdma_mr *mr;
478
479 mr = ocrdma_alloc_lkey(ibpd, acc, 0, OCRDMA_ADDR_CHECK_DISABLE);
Dan Carpenter55a8d622012-05-02 09:13:57 +0300480 if (IS_ERR(mr))
481 return ERR_CAST(mr);
Parav Panditfe2caef2012-03-21 04:09:06 +0530482
483 return &mr->ibmr;
484}
485
486static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
487 struct ocrdma_hw_mr *mr)
488{
489 struct pci_dev *pdev = dev->nic_info.pdev;
490 int i = 0;
491
492 if (mr->pbl_table) {
493 for (i = 0; i < mr->num_pbls; i++) {
494 if (!mr->pbl_table[i].va)
495 continue;
496 dma_free_coherent(&pdev->dev, mr->pbl_size,
497 mr->pbl_table[i].va,
498 mr->pbl_table[i].pa);
499 }
500 kfree(mr->pbl_table);
501 mr->pbl_table = NULL;
502 }
503}
504
505static int ocrdma_get_pbl_info(struct ocrdma_mr *mr, u32 num_pbes)
506{
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++;
521 } while (num_pbls >= mr->hwmr.dev->attr.max_num_mr_pbl);
522
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;
616 struct ocrdma_dev *dev;
617 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);
622 dev = pd->dev;
Parav Panditfe2caef2012-03-21 04:09:06 +0530623
624 if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
625 return ERR_PTR(-EINVAL);
626
627 mr = kzalloc(sizeof(*mr), GFP_KERNEL);
628 if (!mr)
629 return ERR_PTR(status);
630 mr->hwmr.dev = dev;
631 mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
632 if (IS_ERR(mr->umem)) {
633 status = -EFAULT;
634 goto umem_err;
635 }
636 num_pbes = ib_umem_page_count(mr->umem);
637 status = ocrdma_get_pbl_info(mr, num_pbes);
638 if (status)
639 goto umem_err;
640
641 mr->hwmr.pbe_size = mr->umem->page_size;
642 mr->hwmr.fbo = mr->umem->offset;
643 mr->hwmr.va = usr_addr;
644 mr->hwmr.len = len;
645 mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
646 mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
647 mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
648 mr->hwmr.local_rd = 1;
649 mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
650 status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
651 if (status)
652 goto umem_err;
653 build_user_pbes(dev, mr, num_pbes);
654 status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
655 if (status)
656 goto mbx_err;
657 mr->pd = pd;
Parav Panditfe2caef2012-03-21 04:09:06 +0530658 mr->ibmr.lkey = mr->hwmr.lkey;
659 if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
660 mr->ibmr.rkey = mr->hwmr.lkey;
661
662 return &mr->ibmr;
663
664mbx_err:
665 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
666umem_err:
667 kfree(mr);
668 return ERR_PTR(status);
669}
670
671int ocrdma_dereg_mr(struct ib_mr *ib_mr)
672{
673 struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
674 struct ocrdma_dev *dev = mr->hwmr.dev;
675 int status;
676
677 status = ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
678
679 if (mr->hwmr.fr_mr == 0)
680 ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
681
Parav Panditfe2caef2012-03-21 04:09:06 +0530682 /* it could be user registered memory. */
683 if (mr->umem)
684 ib_umem_release(mr->umem);
685 kfree(mr);
686 return status;
687}
688
689static int ocrdma_copy_cq_uresp(struct ocrdma_cq *cq, struct ib_udata *udata,
690 struct ib_ucontext *ib_ctx)
691{
692 int status;
693 struct ocrdma_ucontext *uctx;
694 struct ocrdma_create_cq_uresp uresp;
695
696 uresp.cq_id = cq->id;
697 uresp.page_size = cq->len;
698 uresp.num_pages = 1;
699 uresp.max_hw_cqe = cq->max_hw_cqe;
700 uresp.page_addr[0] = cq->pa;
701 uresp.db_page_addr = cq->dev->nic_info.unmapped_db;
702 uresp.db_page_size = cq->dev->nic_info.db_page_size;
703 uresp.phase_change = cq->phase_change ? 1 : 0;
704 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
705 if (status) {
706 ocrdma_err("%s(%d) copy error cqid=0x%x.\n",
707 __func__, cq->dev->id, cq->id);
708 goto err;
709 }
710 uctx = get_ocrdma_ucontext(ib_ctx);
711 status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
712 if (status)
713 goto err;
714 status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
715 if (status) {
716 ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
717 goto err;
718 }
719 cq->ucontext = uctx;
720err:
721 return status;
722}
723
724struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector,
725 struct ib_ucontext *ib_ctx,
726 struct ib_udata *udata)
727{
728 struct ocrdma_cq *cq;
729 struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
730 int status;
731 struct ocrdma_create_cq_ureq ureq;
732
733 if (udata) {
734 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
735 return ERR_PTR(-EFAULT);
736 } else
737 ureq.dpp_cq = 0;
738 cq = kzalloc(sizeof(*cq), GFP_KERNEL);
739 if (!cq)
740 return ERR_PTR(-ENOMEM);
741
742 spin_lock_init(&cq->cq_lock);
743 spin_lock_init(&cq->comp_handler_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +0530744 INIT_LIST_HEAD(&cq->sq_head);
745 INIT_LIST_HEAD(&cq->rq_head);
746 cq->dev = dev;
747
748 status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq);
749 if (status) {
750 kfree(cq);
751 return ERR_PTR(status);
752 }
753 if (ib_ctx) {
754 status = ocrdma_copy_cq_uresp(cq, udata, ib_ctx);
755 if (status)
756 goto ctx_err;
757 }
758 cq->phase = OCRDMA_CQE_VALID;
759 cq->arm_needed = true;
760 dev->cq_tbl[cq->id] = cq;
761
762 return &cq->ibcq;
763
764ctx_err:
765 ocrdma_mbx_destroy_cq(dev, cq);
766 kfree(cq);
767 return ERR_PTR(status);
768}
769
770int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
771 struct ib_udata *udata)
772{
773 int status = 0;
774 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
775
776 if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
777 status = -EINVAL;
778 return status;
779 }
780 ibcq->cqe = new_cnt;
781 return status;
782}
783
784int ocrdma_destroy_cq(struct ib_cq *ibcq)
785{
786 int status;
787 struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
788 struct ocrdma_dev *dev = cq->dev;
789
Parav Panditfe2caef2012-03-21 04:09:06 +0530790 status = ocrdma_mbx_destroy_cq(dev, cq);
791
792 if (cq->ucontext) {
793 ocrdma_del_mmap(cq->ucontext, (u64) cq->pa, cq->len);
794 ocrdma_del_mmap(cq->ucontext, dev->nic_info.unmapped_db,
795 dev->nic_info.db_page_size);
796 }
797 dev->cq_tbl[cq->id] = NULL;
798
799 kfree(cq);
800 return status;
801}
802
803static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
804{
805 int status = -EINVAL;
806
807 if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
808 dev->qp_tbl[qp->id] = qp;
809 status = 0;
810 }
811 return status;
812}
813
814static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
815{
816 dev->qp_tbl[qp->id] = NULL;
817}
818
819static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
820 struct ib_qp_init_attr *attrs)
821{
822 if (attrs->qp_type != IB_QPT_GSI &&
823 attrs->qp_type != IB_QPT_RC &&
824 attrs->qp_type != IB_QPT_UD) {
825 ocrdma_err("%s(%d) unsupported qp type=0x%x requested\n",
826 __func__, dev->id, attrs->qp_type);
827 return -EINVAL;
828 }
829 if (attrs->cap.max_send_wr > dev->attr.max_wqe) {
830 ocrdma_err("%s(%d) unsupported send_wr=0x%x requested\n",
831 __func__, dev->id, attrs->cap.max_send_wr);
832 ocrdma_err("%s(%d) supported send_wr=0x%x\n",
833 __func__, dev->id, dev->attr.max_wqe);
834 return -EINVAL;
835 }
836 if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
837 ocrdma_err("%s(%d) unsupported recv_wr=0x%x requested\n",
838 __func__, dev->id, attrs->cap.max_recv_wr);
839 ocrdma_err("%s(%d) supported recv_wr=0x%x\n",
840 __func__, dev->id, dev->attr.max_rqe);
841 return -EINVAL;
842 }
843 if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
844 ocrdma_err("%s(%d) unsupported inline data size=0x%x"
845 " requested\n", __func__, dev->id,
846 attrs->cap.max_inline_data);
847 ocrdma_err("%s(%d) supported inline data size=0x%x\n",
848 __func__, dev->id, dev->attr.max_inline_data);
849 return -EINVAL;
850 }
851 if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
852 ocrdma_err("%s(%d) unsupported send_sge=0x%x requested\n",
853 __func__, dev->id, attrs->cap.max_send_sge);
854 ocrdma_err("%s(%d) supported send_sge=0x%x\n",
855 __func__, dev->id, dev->attr.max_send_sge);
856 return -EINVAL;
857 }
858 if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
859 ocrdma_err("%s(%d) unsupported recv_sge=0x%x requested\n",
860 __func__, dev->id, attrs->cap.max_recv_sge);
861 ocrdma_err("%s(%d) supported recv_sge=0x%x\n",
862 __func__, dev->id, dev->attr.max_recv_sge);
863 return -EINVAL;
864 }
865 /* unprivileged user space cannot create special QP */
866 if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
867 ocrdma_err
868 ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
869 __func__, dev->id, attrs->qp_type);
870 return -EINVAL;
871 }
872 /* allow creating only one GSI type of QP */
873 if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
874 ocrdma_err("%s(%d) GSI special QPs already created.\n",
875 __func__, dev->id);
876 return -EINVAL;
877 }
878 /* verify consumer QPs are not trying to use GSI QP's CQ */
879 if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
880 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
Roland Dreier9e8fa042012-07-27 13:14:44 -0700881 (dev->gsi_sqcq == get_ocrdma_cq(attrs->recv_cq)) ||
882 (dev->gsi_rqcq == get_ocrdma_cq(attrs->send_cq)) ||
883 (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
Parav Panditfe2caef2012-03-21 04:09:06 +0530884 ocrdma_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
885 __func__, dev->id);
886 return -EINVAL;
887 }
888 }
889 return 0;
890}
891
892static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
893 struct ib_udata *udata, int dpp_offset,
894 int dpp_credit_lmt, int srq)
895{
896 int status = 0;
897 u64 usr_db;
898 struct ocrdma_create_qp_uresp uresp;
899 struct ocrdma_dev *dev = qp->dev;
900 struct ocrdma_pd *pd = qp->pd;
901
902 memset(&uresp, 0, sizeof(uresp));
903 usr_db = dev->nic_info.unmapped_db +
904 (pd->id * dev->nic_info.db_page_size);
905 uresp.qp_id = qp->id;
906 uresp.sq_dbid = qp->sq.dbid;
907 uresp.num_sq_pages = 1;
908 uresp.sq_page_size = qp->sq.len;
909 uresp.sq_page_addr[0] = qp->sq.pa;
910 uresp.num_wqe_allocated = qp->sq.max_cnt;
911 if (!srq) {
912 uresp.rq_dbid = qp->rq.dbid;
913 uresp.num_rq_pages = 1;
914 uresp.rq_page_size = qp->rq.len;
915 uresp.rq_page_addr[0] = qp->rq.pa;
916 uresp.num_rqe_allocated = qp->rq.max_cnt;
917 }
918 uresp.db_page_addr = usr_db;
919 uresp.db_page_size = dev->nic_info.db_page_size;
920 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
921 uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
922 uresp.db_rq_offset = ((qp->id & 0xFFFF) < 128) ?
923 OCRDMA_DB_GEN2_RQ1_OFFSET : OCRDMA_DB_GEN2_RQ2_OFFSET;
924 uresp.db_shift = (qp->id < 128) ? 24 : 16;
925 } else {
926 uresp.db_sq_offset = OCRDMA_DB_SQ_OFFSET;
927 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
928 uresp.db_shift = 16;
929 }
Parav Panditfe2caef2012-03-21 04:09:06 +0530930
931 if (qp->dpp_enabled) {
932 uresp.dpp_credit = dpp_credit_lmt;
933 uresp.dpp_offset = dpp_offset;
934 }
935 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
936 if (status) {
937 ocrdma_err("%s(%d) user copy error.\n", __func__, dev->id);
938 goto err;
939 }
940 status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
941 uresp.sq_page_size);
942 if (status)
943 goto err;
944
945 if (!srq) {
946 status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
947 uresp.rq_page_size);
948 if (status)
949 goto rq_map_err;
950 }
951 return status;
952rq_map_err:
953 ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
954err:
955 return status;
956}
957
958static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
959 struct ocrdma_pd *pd)
960{
961 if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
962 qp->sq_db = dev->nic_info.db +
963 (pd->id * dev->nic_info.db_page_size) +
964 OCRDMA_DB_GEN2_SQ_OFFSET;
965 qp->rq_db = dev->nic_info.db +
966 (pd->id * dev->nic_info.db_page_size) +
967 ((qp->id < 128) ?
968 OCRDMA_DB_GEN2_RQ1_OFFSET : OCRDMA_DB_GEN2_RQ2_OFFSET);
969 } else {
970 qp->sq_db = dev->nic_info.db +
971 (pd->id * dev->nic_info.db_page_size) +
972 OCRDMA_DB_SQ_OFFSET;
973 qp->rq_db = dev->nic_info.db +
974 (pd->id * dev->nic_info.db_page_size) +
975 OCRDMA_DB_RQ_OFFSET;
976 }
977}
978
979static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
980{
981 qp->wqe_wr_id_tbl =
982 kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
983 GFP_KERNEL);
984 if (qp->wqe_wr_id_tbl == NULL)
985 return -ENOMEM;
986 qp->rqe_wr_id_tbl =
987 kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
988 if (qp->rqe_wr_id_tbl == NULL)
989 return -ENOMEM;
990
991 return 0;
992}
993
994static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
995 struct ocrdma_pd *pd,
996 struct ib_qp_init_attr *attrs)
997{
998 qp->pd = pd;
999 spin_lock_init(&qp->q_lock);
1000 INIT_LIST_HEAD(&qp->sq_entry);
1001 INIT_LIST_HEAD(&qp->rq_entry);
1002
1003 qp->qp_type = attrs->qp_type;
1004 qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
1005 qp->max_inline_data = attrs->cap.max_inline_data;
1006 qp->sq.max_sges = attrs->cap.max_send_sge;
1007 qp->rq.max_sges = attrs->cap.max_recv_sge;
1008 qp->state = OCRDMA_QPS_RST;
1009}
1010
Parav Panditfe2caef2012-03-21 04:09:06 +05301011
1012static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
1013 struct ib_qp_init_attr *attrs)
1014{
1015 if (attrs->qp_type == IB_QPT_GSI) {
1016 dev->gsi_qp_created = 1;
1017 dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
1018 dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
1019 }
1020}
1021
1022struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
1023 struct ib_qp_init_attr *attrs,
1024 struct ib_udata *udata)
1025{
1026 int status;
1027 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1028 struct ocrdma_qp *qp;
1029 struct ocrdma_dev *dev = pd->dev;
1030 struct ocrdma_create_qp_ureq ureq;
1031 u16 dpp_credit_lmt, dpp_offset;
1032
1033 status = ocrdma_check_qp_params(ibpd, dev, attrs);
1034 if (status)
1035 goto gen_err;
1036
1037 memset(&ureq, 0, sizeof(ureq));
1038 if (udata) {
1039 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1040 return ERR_PTR(-EFAULT);
1041 }
1042 qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1043 if (!qp) {
1044 status = -ENOMEM;
1045 goto gen_err;
1046 }
1047 qp->dev = dev;
1048 ocrdma_set_qp_init_params(qp, pd, attrs);
1049
1050 mutex_lock(&dev->dev_lock);
1051 status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
1052 ureq.dpp_cq_id,
1053 &dpp_offset, &dpp_credit_lmt);
1054 if (status)
1055 goto mbx_err;
1056
1057 /* user space QP's wr_id table are managed in library */
1058 if (udata == NULL) {
1059 qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
1060 OCRDMA_QP_FAST_REG);
1061 status = ocrdma_alloc_wr_id_tbl(qp);
1062 if (status)
1063 goto map_err;
1064 }
1065
1066 status = ocrdma_add_qpn_map(dev, qp);
1067 if (status)
1068 goto map_err;
1069 ocrdma_set_qp_db(dev, qp, pd);
1070 if (udata) {
1071 status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
1072 dpp_credit_lmt,
1073 (attrs->srq != NULL));
1074 if (status)
1075 goto cpy_err;
1076 }
1077 ocrdma_store_gsi_qp_cq(dev, attrs);
Gottumukkala, Naresh27159f52013-06-05 08:50:46 +00001078 qp->ibqp.qp_num = qp->id;
Parav Panditfe2caef2012-03-21 04:09:06 +05301079 mutex_unlock(&dev->dev_lock);
1080 return &qp->ibqp;
1081
1082cpy_err:
1083 ocrdma_del_qpn_map(dev, qp);
1084map_err:
1085 ocrdma_mbx_destroy_qp(dev, qp);
1086mbx_err:
1087 mutex_unlock(&dev->dev_lock);
1088 kfree(qp->wqe_wr_id_tbl);
1089 kfree(qp->rqe_wr_id_tbl);
1090 kfree(qp);
1091 ocrdma_err("%s(%d) error=%d\n", __func__, dev->id, status);
1092gen_err:
1093 return ERR_PTR(status);
1094}
1095
1096int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1097 int attr_mask)
1098{
1099 int status = 0;
1100 struct ocrdma_qp *qp;
1101 struct ocrdma_dev *dev;
1102 enum ib_qp_state old_qps;
1103
1104 qp = get_ocrdma_qp(ibqp);
1105 dev = qp->dev;
1106 if (attr_mask & IB_QP_STATE)
1107 status = ocrdma_qp_state_machine(qp, attr->qp_state, &old_qps);
1108 /* if new and previous states are same hw doesn't need to
1109 * know about it.
1110 */
1111 if (status < 0)
1112 return status;
1113 status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask, old_qps);
1114 return status;
1115}
1116
1117int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1118 int attr_mask, struct ib_udata *udata)
1119{
1120 unsigned long flags;
1121 int status = -EINVAL;
1122 struct ocrdma_qp *qp;
1123 struct ocrdma_dev *dev;
1124 enum ib_qp_state old_qps, new_qps;
1125
1126 qp = get_ocrdma_qp(ibqp);
1127 dev = qp->dev;
1128
1129 /* syncronize with multiple context trying to change, retrive qps */
1130 mutex_lock(&dev->dev_lock);
1131 /* syncronize with wqe, rqe posting and cqe processing contexts */
1132 spin_lock_irqsave(&qp->q_lock, flags);
1133 old_qps = get_ibqp_state(qp->state);
1134 if (attr_mask & IB_QP_STATE)
1135 new_qps = attr->qp_state;
1136 else
1137 new_qps = old_qps;
1138 spin_unlock_irqrestore(&qp->q_lock, flags);
1139
1140 if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask)) {
1141 ocrdma_err("%s(%d) invalid attribute mask=0x%x specified for "
1142 "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
1143 __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
1144 old_qps, new_qps);
1145 goto param_err;
1146 }
1147
1148 status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
1149 if (status > 0)
1150 status = 0;
1151param_err:
1152 mutex_unlock(&dev->dev_lock);
1153 return status;
1154}
1155
1156static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
1157{
1158 switch (mtu) {
1159 case 256:
1160 return IB_MTU_256;
1161 case 512:
1162 return IB_MTU_512;
1163 case 1024:
1164 return IB_MTU_1024;
1165 case 2048:
1166 return IB_MTU_2048;
1167 case 4096:
1168 return IB_MTU_4096;
1169 default:
1170 return IB_MTU_1024;
1171 }
1172}
1173
1174static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
1175{
1176 int ib_qp_acc_flags = 0;
1177
1178 if (qp_cap_flags & OCRDMA_QP_INB_WR)
1179 ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
1180 if (qp_cap_flags & OCRDMA_QP_INB_RD)
1181 ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
1182 return ib_qp_acc_flags;
1183}
1184
1185int ocrdma_query_qp(struct ib_qp *ibqp,
1186 struct ib_qp_attr *qp_attr,
1187 int attr_mask, struct ib_qp_init_attr *qp_init_attr)
1188{
1189 int status;
1190 u32 qp_state;
1191 struct ocrdma_qp_params params;
1192 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
1193 struct ocrdma_dev *dev = qp->dev;
1194
1195 memset(&params, 0, sizeof(params));
1196 mutex_lock(&dev->dev_lock);
1197 status = ocrdma_mbx_query_qp(dev, qp, &params);
1198 mutex_unlock(&dev->dev_lock);
1199 if (status)
1200 goto mbx_err;
1201 qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT);
1202 qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT);
1203 qp_attr->path_mtu =
1204 ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
1205 OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
1206 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
1207 qp_attr->path_mig_state = IB_MIG_MIGRATED;
1208 qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
1209 qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
1210 qp_attr->dest_qp_num =
1211 params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
1212
1213 qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
1214 qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
1215 qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
1216 qp_attr->cap.max_send_sge = qp->sq.max_sges;
1217 qp_attr->cap.max_recv_sge = qp->rq.max_sges;
1218 qp_attr->cap.max_inline_data = dev->attr.max_inline_data;
1219 qp_init_attr->cap = qp_attr->cap;
1220 memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
1221 sizeof(params.dgid));
1222 qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
1223 OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
1224 qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
1225 qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
1226 OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
1227 OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
1228 qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
1229 OCRDMA_QP_PARAMS_SQ_PSN_MASK) >>
1230 OCRDMA_QP_PARAMS_TCLASS_SHIFT;
1231
1232 qp_attr->ah_attr.ah_flags = IB_AH_GRH;
1233 qp_attr->ah_attr.port_num = 1;
1234 qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
1235 OCRDMA_QP_PARAMS_SL_MASK) >>
1236 OCRDMA_QP_PARAMS_SL_SHIFT;
1237 qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
1238 OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
1239 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
1240 qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
1241 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
1242 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
1243 qp_attr->retry_cnt =
1244 (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
1245 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
1246 qp_attr->min_rnr_timer = 0;
1247 qp_attr->pkey_index = 0;
1248 qp_attr->port_num = 1;
1249 qp_attr->ah_attr.src_path_bits = 0;
1250 qp_attr->ah_attr.static_rate = 0;
1251 qp_attr->alt_pkey_index = 0;
1252 qp_attr->alt_port_num = 0;
1253 qp_attr->alt_timeout = 0;
1254 memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
1255 qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
1256 OCRDMA_QP_PARAMS_STATE_SHIFT;
1257 qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
1258 qp_attr->max_dest_rd_atomic =
1259 params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
1260 qp_attr->max_rd_atomic =
1261 params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
1262 qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
1263 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
1264mbx_err:
1265 return status;
1266}
1267
1268static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx)
1269{
1270 int i = idx / 32;
1271 unsigned int mask = (1 << (idx % 32));
1272
1273 if (srq->idx_bit_fields[i] & mask)
1274 srq->idx_bit_fields[i] &= ~mask;
1275 else
1276 srq->idx_bit_fields[i] |= mask;
1277}
1278
1279static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
1280{
1281 int free_cnt;
1282 if (q->head >= q->tail)
1283 free_cnt = (q->max_cnt - q->head) + q->tail;
1284 else
1285 free_cnt = q->tail - q->head;
Parav Panditfe2caef2012-03-21 04:09:06 +05301286 return free_cnt;
1287}
1288
1289static int is_hw_sq_empty(struct ocrdma_qp *qp)
1290{
1291 return (qp->sq.tail == qp->sq.head &&
1292 ocrdma_hwq_free_cnt(&qp->sq) ? 1 : 0);
1293}
1294
1295static int is_hw_rq_empty(struct ocrdma_qp *qp)
1296{
1297 return (qp->rq.tail == qp->rq.head) ? 1 : 0;
1298}
1299
1300static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
1301{
1302 return q->va + (q->head * q->entry_size);
1303}
1304
1305static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
1306 u32 idx)
1307{
1308 return q->va + (idx * q->entry_size);
1309}
1310
1311static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
1312{
1313 q->head = (q->head + 1) & q->max_wqe_idx;
1314}
1315
1316static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
1317{
1318 q->tail = (q->tail + 1) & q->max_wqe_idx;
1319}
1320
1321/* discard the cqe for a given QP */
1322static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
1323{
1324 unsigned long cq_flags;
1325 unsigned long flags;
1326 int discard_cnt = 0;
1327 u32 cur_getp, stop_getp;
1328 struct ocrdma_cqe *cqe;
1329 u32 qpn = 0;
1330
1331 spin_lock_irqsave(&cq->cq_lock, cq_flags);
1332
1333 /* traverse through the CQEs in the hw CQ,
1334 * find the matching CQE for a given qp,
1335 * mark the matching one discarded by clearing qpn.
1336 * ring the doorbell in the poll_cq() as
1337 * we don't complete out of order cqe.
1338 */
1339
1340 cur_getp = cq->getp;
1341 /* find upto when do we reap the cq. */
1342 stop_getp = cur_getp;
1343 do {
1344 if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
1345 break;
1346
1347 cqe = cq->va + cur_getp;
1348 /* if (a) done reaping whole hw cq, or
1349 * (b) qp_xq becomes empty.
1350 * then exit
1351 */
1352 qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
1353 /* if previously discarded cqe found, skip that too. */
1354 /* check for matching qp */
1355 if (qpn == 0 || qpn != qp->id)
1356 goto skip_cqe;
1357
1358 /* mark cqe discarded so that it is not picked up later
1359 * in the poll_cq().
1360 */
1361 discard_cnt += 1;
1362 cqe->cmn.qpn = 0;
1363 if (is_cqe_for_sq(cqe))
1364 ocrdma_hwq_inc_tail(&qp->sq);
1365 else {
1366 if (qp->srq) {
1367 spin_lock_irqsave(&qp->srq->q_lock, flags);
1368 ocrdma_hwq_inc_tail(&qp->srq->rq);
1369 ocrdma_srq_toggle_bit(qp->srq, cur_getp);
1370 spin_unlock_irqrestore(&qp->srq->q_lock, flags);
1371
1372 } else
1373 ocrdma_hwq_inc_tail(&qp->rq);
1374 }
1375skip_cqe:
1376 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
1377 } while (cur_getp != stop_getp);
1378 spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
1379}
1380
1381static void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
1382{
1383 int found = false;
1384 unsigned long flags;
1385 struct ocrdma_dev *dev = qp->dev;
1386 /* sync with any active CQ poll */
1387
1388 spin_lock_irqsave(&dev->flush_q_lock, flags);
1389 found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1390 if (found)
1391 list_del(&qp->sq_entry);
1392 if (!qp->srq) {
1393 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1394 if (found)
1395 list_del(&qp->rq_entry);
1396 }
1397 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
1398}
1399
1400int ocrdma_destroy_qp(struct ib_qp *ibqp)
1401{
1402 int status;
1403 struct ocrdma_pd *pd;
1404 struct ocrdma_qp *qp;
1405 struct ocrdma_dev *dev;
1406 struct ib_qp_attr attrs;
1407 int attr_mask = IB_QP_STATE;
Dan Carpenterd19081e2012-05-02 09:14:47 +03001408 unsigned long flags;
Parav Panditfe2caef2012-03-21 04:09:06 +05301409
1410 qp = get_ocrdma_qp(ibqp);
1411 dev = qp->dev;
1412
1413 attrs.qp_state = IB_QPS_ERR;
1414 pd = qp->pd;
1415
1416 /* change the QP state to ERROR */
1417 _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
1418
1419 /* ensure that CQEs for newly created QP (whose id may be same with
1420 * one which just getting destroyed are same), dont get
1421 * discarded until the old CQEs are discarded.
1422 */
1423 mutex_lock(&dev->dev_lock);
1424 status = ocrdma_mbx_destroy_qp(dev, qp);
1425
1426 /*
1427 * acquire CQ lock while destroy is in progress, in order to
1428 * protect against proessing in-flight CQEs for this QP.
1429 */
Dan Carpenterd19081e2012-05-02 09:14:47 +03001430 spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301431 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001432 spin_lock(&qp->rq_cq->cq_lock);
Parav Panditfe2caef2012-03-21 04:09:06 +05301433
1434 ocrdma_del_qpn_map(dev, qp);
1435
1436 if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
Dan Carpenterd19081e2012-05-02 09:14:47 +03001437 spin_unlock(&qp->rq_cq->cq_lock);
1438 spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
Parav Panditfe2caef2012-03-21 04:09:06 +05301439
1440 if (!pd->uctx) {
1441 ocrdma_discard_cqes(qp, qp->sq_cq);
1442 ocrdma_discard_cqes(qp, qp->rq_cq);
1443 }
1444 mutex_unlock(&dev->dev_lock);
1445
1446 if (pd->uctx) {
1447 ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa, qp->sq.len);
1448 if (!qp->srq)
1449 ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa, qp->rq.len);
1450 }
1451
1452 ocrdma_del_flush_qp(qp);
1453
Parav Panditfe2caef2012-03-21 04:09:06 +05301454 kfree(qp->wqe_wr_id_tbl);
1455 kfree(qp->rqe_wr_id_tbl);
1456 kfree(qp);
1457 return status;
1458}
1459
1460static int ocrdma_copy_srq_uresp(struct ocrdma_srq *srq, struct ib_udata *udata)
1461{
1462 int status;
1463 struct ocrdma_create_srq_uresp uresp;
1464
1465 uresp.rq_dbid = srq->rq.dbid;
1466 uresp.num_rq_pages = 1;
1467 uresp.rq_page_addr[0] = srq->rq.pa;
1468 uresp.rq_page_size = srq->rq.len;
1469 uresp.db_page_addr = srq->dev->nic_info.unmapped_db +
1470 (srq->pd->id * srq->dev->nic_info.db_page_size);
1471 uresp.db_page_size = srq->dev->nic_info.db_page_size;
1472 uresp.num_rqe_allocated = srq->rq.max_cnt;
Parav Panditfe2caef2012-03-21 04:09:06 +05301473 if (srq->dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) {
1474 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ1_OFFSET;
1475 uresp.db_shift = 24;
1476 } else {
1477 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
1478 uresp.db_shift = 16;
1479 }
1480
1481 status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1482 if (status)
1483 return status;
1484 status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
1485 uresp.rq_page_size);
1486 if (status)
1487 return status;
1488 return status;
1489}
1490
1491struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
1492 struct ib_srq_init_attr *init_attr,
1493 struct ib_udata *udata)
1494{
1495 int status = -ENOMEM;
1496 struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1497 struct ocrdma_dev *dev = pd->dev;
1498 struct ocrdma_srq *srq;
1499
1500 if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
1501 return ERR_PTR(-EINVAL);
1502 if (init_attr->attr.max_wr > dev->attr.max_rqe)
1503 return ERR_PTR(-EINVAL);
1504
1505 srq = kzalloc(sizeof(*srq), GFP_KERNEL);
1506 if (!srq)
1507 return ERR_PTR(status);
1508
1509 spin_lock_init(&srq->q_lock);
1510 srq->dev = dev;
1511 srq->pd = pd;
1512 srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
1513 status = ocrdma_mbx_create_srq(srq, init_attr, pd);
1514 if (status)
1515 goto err;
1516
1517 if (udata == NULL) {
1518 srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
1519 GFP_KERNEL);
1520 if (srq->rqe_wr_id_tbl == NULL)
1521 goto arm_err;
1522
1523 srq->bit_fields_len = (srq->rq.max_cnt / 32) +
1524 (srq->rq.max_cnt % 32 ? 1 : 0);
1525 srq->idx_bit_fields =
1526 kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
1527 if (srq->idx_bit_fields == NULL)
1528 goto arm_err;
1529 memset(srq->idx_bit_fields, 0xff,
1530 srq->bit_fields_len * sizeof(u32));
1531 }
1532
1533 if (init_attr->attr.srq_limit) {
1534 status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
1535 if (status)
1536 goto arm_err;
1537 }
1538
Parav Panditfe2caef2012-03-21 04:09:06 +05301539 if (udata) {
1540 status = ocrdma_copy_srq_uresp(srq, udata);
1541 if (status)
1542 goto arm_err;
1543 }
1544
Parav Panditfe2caef2012-03-21 04:09:06 +05301545 return &srq->ibsrq;
1546
1547arm_err:
1548 ocrdma_mbx_destroy_srq(dev, srq);
1549err:
1550 kfree(srq->rqe_wr_id_tbl);
1551 kfree(srq->idx_bit_fields);
1552 kfree(srq);
1553 return ERR_PTR(status);
1554}
1555
1556int ocrdma_modify_srq(struct ib_srq *ibsrq,
1557 struct ib_srq_attr *srq_attr,
1558 enum ib_srq_attr_mask srq_attr_mask,
1559 struct ib_udata *udata)
1560{
1561 int status = 0;
1562 struct ocrdma_srq *srq;
Parav Panditfe2caef2012-03-21 04:09:06 +05301563
1564 srq = get_ocrdma_srq(ibsrq);
Parav Panditfe2caef2012-03-21 04:09:06 +05301565 if (srq_attr_mask & IB_SRQ_MAX_WR)
1566 status = -EINVAL;
1567 else
1568 status = ocrdma_mbx_modify_srq(srq, srq_attr);
1569 return status;
1570}
1571
1572int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
1573{
1574 int status;
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 status = ocrdma_mbx_query_srq(srq, srq_attr);
1579 return status;
1580}
1581
1582int ocrdma_destroy_srq(struct ib_srq *ibsrq)
1583{
1584 int status;
1585 struct ocrdma_srq *srq;
1586 struct ocrdma_dev *dev;
1587
1588 srq = get_ocrdma_srq(ibsrq);
1589 dev = srq->dev;
Parav Panditfe2caef2012-03-21 04:09:06 +05301590
1591 status = ocrdma_mbx_destroy_srq(dev, srq);
1592
1593 if (srq->pd->uctx)
1594 ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa, srq->rq.len);
1595
Parav Panditfe2caef2012-03-21 04:09:06 +05301596 kfree(srq->idx_bit_fields);
1597 kfree(srq->rqe_wr_id_tbl);
1598 kfree(srq);
1599 return status;
1600}
1601
1602/* unprivileged verbs and their support functions. */
1603static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
1604 struct ocrdma_hdr_wqe *hdr,
1605 struct ib_send_wr *wr)
1606{
1607 struct ocrdma_ewqe_ud_hdr *ud_hdr =
1608 (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
1609 struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
1610
1611 ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
1612 if (qp->qp_type == IB_QPT_GSI)
1613 ud_hdr->qkey = qp->qkey;
1614 else
1615 ud_hdr->qkey = wr->wr.ud.remote_qkey;
1616 ud_hdr->rsvd_ahid = ah->id;
1617}
1618
1619static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
1620 struct ocrdma_sge *sge, int num_sge,
1621 struct ib_sge *sg_list)
1622{
1623 int i;
1624
1625 for (i = 0; i < num_sge; i++) {
1626 sge[i].lrkey = sg_list[i].lkey;
1627 sge[i].addr_lo = sg_list[i].addr;
1628 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
1629 sge[i].len = sg_list[i].length;
1630 hdr->total_len += sg_list[i].length;
1631 }
1632 if (num_sge == 0)
1633 memset(sge, 0, sizeof(*sge));
1634}
1635
1636static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
1637 struct ocrdma_hdr_wqe *hdr,
1638 struct ocrdma_sge *sge,
1639 struct ib_send_wr *wr, u32 wqe_size)
1640{
1641 if (wr->send_flags & IB_SEND_INLINE) {
1642 if (wr->sg_list[0].length > qp->max_inline_data) {
1643 ocrdma_err("%s() supported_len=0x%x,"
1644 " unspported len req=0x%x\n", __func__,
1645 qp->max_inline_data, wr->sg_list[0].length);
1646 return -EINVAL;
1647 }
1648 memcpy(sge,
1649 (void *)(unsigned long)wr->sg_list[0].addr,
1650 wr->sg_list[0].length);
1651 hdr->total_len = wr->sg_list[0].length;
1652 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
1653 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
1654 } else {
1655 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1656 if (wr->num_sge)
1657 wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
1658 else
1659 wqe_size += sizeof(struct ocrdma_sge);
1660 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1661 }
1662 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
1663 return 0;
1664}
1665
1666static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1667 struct ib_send_wr *wr)
1668{
1669 int status;
1670 struct ocrdma_sge *sge;
1671 u32 wqe_size = sizeof(*hdr);
1672
1673 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
1674 ocrdma_build_ud_hdr(qp, hdr, wr);
1675 sge = (struct ocrdma_sge *)(hdr + 2);
1676 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
1677 } else
1678 sge = (struct ocrdma_sge *)(hdr + 1);
1679
1680 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1681 return status;
1682}
1683
1684static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1685 struct ib_send_wr *wr)
1686{
1687 int status;
1688 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
1689 struct ocrdma_sge *sge = ext_rw + 1;
1690 u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
1691
1692 status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1693 if (status)
1694 return status;
1695 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
1696 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
1697 ext_rw->lrkey = wr->wr.rdma.rkey;
1698 ext_rw->len = hdr->total_len;
1699 return 0;
1700}
1701
1702static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1703 struct ib_send_wr *wr)
1704{
1705 struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
1706 struct ocrdma_sge *sge = ext_rw + 1;
1707 u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
1708 sizeof(struct ocrdma_hdr_wqe);
1709
1710 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1711 hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
1712 hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
1713 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1714
1715 ext_rw->addr_lo = wr->wr.rdma.remote_addr;
1716 ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
1717 ext_rw->lrkey = wr->wr.rdma.rkey;
1718 ext_rw->len = hdr->total_len;
1719}
1720
1721static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
1722{
1723 u32 val = qp->sq.dbid | (1 << 16);
1724
1725 iowrite32(val, qp->sq_db);
1726}
1727
1728int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
1729 struct ib_send_wr **bad_wr)
1730{
1731 int status = 0;
1732 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
1733 struct ocrdma_hdr_wqe *hdr;
1734 unsigned long flags;
1735
1736 spin_lock_irqsave(&qp->q_lock, flags);
1737 if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
1738 spin_unlock_irqrestore(&qp->q_lock, flags);
1739 return -EINVAL;
1740 }
1741
1742 while (wr) {
1743 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
1744 wr->num_sge > qp->sq.max_sges) {
1745 status = -ENOMEM;
1746 break;
1747 }
1748 hdr = ocrdma_hwq_head(&qp->sq);
1749 hdr->cw = 0;
1750 if (wr->send_flags & IB_SEND_SIGNALED)
1751 hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
1752 if (wr->send_flags & IB_SEND_FENCE)
1753 hdr->cw |=
1754 (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
1755 if (wr->send_flags & IB_SEND_SOLICITED)
1756 hdr->cw |=
1757 (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
1758 hdr->total_len = 0;
1759 switch (wr->opcode) {
1760 case IB_WR_SEND_WITH_IMM:
1761 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
1762 hdr->immdt = ntohl(wr->ex.imm_data);
1763 case IB_WR_SEND:
1764 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
1765 ocrdma_build_send(qp, hdr, wr);
1766 break;
1767 case IB_WR_SEND_WITH_INV:
1768 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
1769 hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
1770 hdr->lkey = wr->ex.invalidate_rkey;
1771 status = ocrdma_build_send(qp, hdr, wr);
1772 break;
1773 case IB_WR_RDMA_WRITE_WITH_IMM:
1774 hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
1775 hdr->immdt = ntohl(wr->ex.imm_data);
1776 case IB_WR_RDMA_WRITE:
1777 hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
1778 status = ocrdma_build_write(qp, hdr, wr);
1779 break;
1780 case IB_WR_RDMA_READ_WITH_INV:
1781 hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
1782 case IB_WR_RDMA_READ:
1783 ocrdma_build_read(qp, hdr, wr);
1784 break;
1785 case IB_WR_LOCAL_INV:
1786 hdr->cw |=
1787 (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
1788 hdr->cw |= (sizeof(struct ocrdma_hdr_wqe) /
1789 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
1790 hdr->lkey = wr->ex.invalidate_rkey;
1791 break;
1792 default:
1793 status = -EINVAL;
1794 break;
1795 }
1796 if (status) {
1797 *bad_wr = wr;
1798 break;
1799 }
1800 if (wr->send_flags & IB_SEND_SIGNALED)
1801 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
1802 else
1803 qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
1804 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
1805 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
1806 OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
1807 /* make sure wqe is written before adapter can access it */
1808 wmb();
1809 /* inform hw to start processing it */
1810 ocrdma_ring_sq_db(qp);
1811
1812 /* update pointer, counter for next wr */
1813 ocrdma_hwq_inc_head(&qp->sq);
1814 wr = wr->next;
1815 }
1816 spin_unlock_irqrestore(&qp->q_lock, flags);
1817 return status;
1818}
1819
1820static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
1821{
1822 u32 val = qp->rq.dbid | (1 << OCRDMA_GET_NUM_POSTED_SHIFT_VAL(qp));
1823
1824 iowrite32(val, qp->rq_db);
1825}
1826
1827static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
1828 u16 tag)
1829{
1830 u32 wqe_size = 0;
1831 struct ocrdma_sge *sge;
1832 if (wr->num_sge)
1833 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
1834 else
1835 wqe_size = sizeof(*sge) + sizeof(*rqe);
1836
1837 rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
1838 OCRDMA_WQE_SIZE_SHIFT);
1839 rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
1840 rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1841 rqe->total_len = 0;
1842 rqe->rsvd_tag = tag;
1843 sge = (struct ocrdma_sge *)(rqe + 1);
1844 ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
1845 ocrdma_cpu_to_le32(rqe, wqe_size);
1846}
1847
1848int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
1849 struct ib_recv_wr **bad_wr)
1850{
1851 int status = 0;
1852 unsigned long flags;
1853 struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
1854 struct ocrdma_hdr_wqe *rqe;
1855
1856 spin_lock_irqsave(&qp->q_lock, flags);
1857 if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
1858 spin_unlock_irqrestore(&qp->q_lock, flags);
1859 *bad_wr = wr;
1860 return -EINVAL;
1861 }
1862 while (wr) {
1863 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
1864 wr->num_sge > qp->rq.max_sges) {
1865 *bad_wr = wr;
1866 status = -ENOMEM;
1867 break;
1868 }
1869 rqe = ocrdma_hwq_head(&qp->rq);
1870 ocrdma_build_rqe(rqe, wr, 0);
1871
1872 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
1873 /* make sure rqe is written before adapter can access it */
1874 wmb();
1875
1876 /* inform hw to start processing it */
1877 ocrdma_ring_rq_db(qp);
1878
1879 /* update pointer, counter for next wr */
1880 ocrdma_hwq_inc_head(&qp->rq);
1881 wr = wr->next;
1882 }
1883 spin_unlock_irqrestore(&qp->q_lock, flags);
1884 return status;
1885}
1886
1887/* cqe for srq's rqe can potentially arrive out of order.
1888 * index gives the entry in the shadow table where to store
1889 * the wr_id. tag/index is returned in cqe to reference back
1890 * for a given rqe.
1891 */
1892static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
1893{
1894 int row = 0;
1895 int indx = 0;
1896
1897 for (row = 0; row < srq->bit_fields_len; row++) {
1898 if (srq->idx_bit_fields[row]) {
1899 indx = ffs(srq->idx_bit_fields[row]);
1900 indx = (row * 32) + (indx - 1);
1901 if (indx >= srq->rq.max_cnt)
1902 BUG();
1903 ocrdma_srq_toggle_bit(srq, indx);
1904 break;
1905 }
1906 }
1907
1908 if (row == srq->bit_fields_len)
1909 BUG();
1910 return indx;
1911}
1912
1913static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
1914{
1915 u32 val = srq->rq.dbid | (1 << 16);
1916
1917 iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
1918}
1919
1920int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
1921 struct ib_recv_wr **bad_wr)
1922{
1923 int status = 0;
1924 unsigned long flags;
1925 struct ocrdma_srq *srq;
1926 struct ocrdma_hdr_wqe *rqe;
1927 u16 tag;
1928
1929 srq = get_ocrdma_srq(ibsrq);
1930
1931 spin_lock_irqsave(&srq->q_lock, flags);
1932 while (wr) {
1933 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
1934 wr->num_sge > srq->rq.max_sges) {
1935 status = -ENOMEM;
1936 *bad_wr = wr;
1937 break;
1938 }
1939 tag = ocrdma_srq_get_idx(srq);
1940 rqe = ocrdma_hwq_head(&srq->rq);
1941 ocrdma_build_rqe(rqe, wr, tag);
1942
1943 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
1944 /* make sure rqe is written before adapter can perform DMA */
1945 wmb();
1946 /* inform hw to start processing it */
1947 ocrdma_ring_srq_db(srq);
1948 /* update pointer, counter for next wr */
1949 ocrdma_hwq_inc_head(&srq->rq);
1950 wr = wr->next;
1951 }
1952 spin_unlock_irqrestore(&srq->q_lock, flags);
1953 return status;
1954}
1955
1956static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
1957{
1958 enum ib_wc_status ibwc_status = IB_WC_GENERAL_ERR;
1959
1960 switch (status) {
1961 case OCRDMA_CQE_GENERAL_ERR:
1962 ibwc_status = IB_WC_GENERAL_ERR;
1963 break;
1964 case OCRDMA_CQE_LOC_LEN_ERR:
1965 ibwc_status = IB_WC_LOC_LEN_ERR;
1966 break;
1967 case OCRDMA_CQE_LOC_QP_OP_ERR:
1968 ibwc_status = IB_WC_LOC_QP_OP_ERR;
1969 break;
1970 case OCRDMA_CQE_LOC_EEC_OP_ERR:
1971 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
1972 break;
1973 case OCRDMA_CQE_LOC_PROT_ERR:
1974 ibwc_status = IB_WC_LOC_PROT_ERR;
1975 break;
1976 case OCRDMA_CQE_WR_FLUSH_ERR:
1977 ibwc_status = IB_WC_WR_FLUSH_ERR;
1978 break;
1979 case OCRDMA_CQE_MW_BIND_ERR:
1980 ibwc_status = IB_WC_MW_BIND_ERR;
1981 break;
1982 case OCRDMA_CQE_BAD_RESP_ERR:
1983 ibwc_status = IB_WC_BAD_RESP_ERR;
1984 break;
1985 case OCRDMA_CQE_LOC_ACCESS_ERR:
1986 ibwc_status = IB_WC_LOC_ACCESS_ERR;
1987 break;
1988 case OCRDMA_CQE_REM_INV_REQ_ERR:
1989 ibwc_status = IB_WC_REM_INV_REQ_ERR;
1990 break;
1991 case OCRDMA_CQE_REM_ACCESS_ERR:
1992 ibwc_status = IB_WC_REM_ACCESS_ERR;
1993 break;
1994 case OCRDMA_CQE_REM_OP_ERR:
1995 ibwc_status = IB_WC_REM_OP_ERR;
1996 break;
1997 case OCRDMA_CQE_RETRY_EXC_ERR:
1998 ibwc_status = IB_WC_RETRY_EXC_ERR;
1999 break;
2000 case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2001 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2002 break;
2003 case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2004 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2005 break;
2006 case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2007 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2008 break;
2009 case OCRDMA_CQE_REM_ABORT_ERR:
2010 ibwc_status = IB_WC_REM_ABORT_ERR;
2011 break;
2012 case OCRDMA_CQE_INV_EECN_ERR:
2013 ibwc_status = IB_WC_INV_EECN_ERR;
2014 break;
2015 case OCRDMA_CQE_INV_EEC_STATE_ERR:
2016 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2017 break;
2018 case OCRDMA_CQE_FATAL_ERR:
2019 ibwc_status = IB_WC_FATAL_ERR;
2020 break;
2021 case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2022 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2023 break;
2024 default:
2025 ibwc_status = IB_WC_GENERAL_ERR;
2026 break;
2027 };
2028 return ibwc_status;
2029}
2030
2031static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2032 u32 wqe_idx)
2033{
2034 struct ocrdma_hdr_wqe *hdr;
2035 struct ocrdma_sge *rw;
2036 int opcode;
2037
2038 hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2039
2040 ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2041 /* Undo the hdr->cw swap */
2042 opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2043 switch (opcode) {
2044 case OCRDMA_WRITE:
2045 ibwc->opcode = IB_WC_RDMA_WRITE;
2046 break;
2047 case OCRDMA_READ:
2048 rw = (struct ocrdma_sge *)(hdr + 1);
2049 ibwc->opcode = IB_WC_RDMA_READ;
2050 ibwc->byte_len = rw->len;
2051 break;
2052 case OCRDMA_SEND:
2053 ibwc->opcode = IB_WC_SEND;
2054 break;
2055 case OCRDMA_LKEY_INV:
2056 ibwc->opcode = IB_WC_LOCAL_INV;
2057 break;
2058 default:
2059 ibwc->status = IB_WC_GENERAL_ERR;
2060 ocrdma_err("%s() invalid opcode received = 0x%x\n",
2061 __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
2062 break;
2063 };
2064}
2065
2066static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2067 struct ocrdma_cqe *cqe)
2068{
2069 if (is_cqe_for_sq(cqe)) {
2070 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2071 cqe->flags_status_srcqpn) &
2072 ~OCRDMA_CQE_STATUS_MASK);
2073 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2074 cqe->flags_status_srcqpn) |
2075 (OCRDMA_CQE_WR_FLUSH_ERR <<
2076 OCRDMA_CQE_STATUS_SHIFT));
2077 } else {
2078 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2079 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2080 cqe->flags_status_srcqpn) &
2081 ~OCRDMA_CQE_UD_STATUS_MASK);
2082 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2083 cqe->flags_status_srcqpn) |
2084 (OCRDMA_CQE_WR_FLUSH_ERR <<
2085 OCRDMA_CQE_UD_STATUS_SHIFT));
2086 } else {
2087 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2088 cqe->flags_status_srcqpn) &
2089 ~OCRDMA_CQE_STATUS_MASK);
2090 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2091 cqe->flags_status_srcqpn) |
2092 (OCRDMA_CQE_WR_FLUSH_ERR <<
2093 OCRDMA_CQE_STATUS_SHIFT));
2094 }
2095 }
2096}
2097
2098static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2099 struct ocrdma_qp *qp, int status)
2100{
2101 bool expand = false;
2102
2103 ibwc->byte_len = 0;
2104 ibwc->qp = &qp->ibqp;
2105 ibwc->status = ocrdma_to_ibwc_err(status);
2106
2107 ocrdma_flush_qp(qp);
2108 ocrdma_qp_state_machine(qp, IB_QPS_ERR, NULL);
2109
2110 /* if wqe/rqe pending for which cqe needs to be returned,
2111 * trigger inflating it.
2112 */
2113 if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2114 expand = true;
2115 ocrdma_set_cqe_status_flushed(qp, cqe);
2116 }
2117 return expand;
2118}
2119
2120static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2121 struct ocrdma_qp *qp, int status)
2122{
2123 ibwc->opcode = IB_WC_RECV;
2124 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2125 ocrdma_hwq_inc_tail(&qp->rq);
2126
2127 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2128}
2129
2130static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2131 struct ocrdma_qp *qp, int status)
2132{
2133 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2134 ocrdma_hwq_inc_tail(&qp->sq);
2135
2136 return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2137}
2138
2139
2140static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2141 struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2142 bool *polled, bool *stop)
2143{
2144 bool expand;
2145 int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2146 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2147
2148 /* when hw sq is empty, but rq is not empty, so we continue
2149 * to keep the cqe in order to get the cq event again.
2150 */
2151 if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2152 /* when cq for rq and sq is same, it is safe to return
2153 * flush cqe for RQEs.
2154 */
2155 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2156 *polled = true;
2157 status = OCRDMA_CQE_WR_FLUSH_ERR;
2158 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2159 } else {
2160 /* stop processing further cqe as this cqe is used for
2161 * triggering cq event on buddy cq of RQ.
2162 * When QP is destroyed, this cqe will be removed
2163 * from the cq's hardware q.
2164 */
2165 *polled = false;
2166 *stop = true;
2167 expand = false;
2168 }
2169 } else {
2170 *polled = true;
2171 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2172 }
2173 return expand;
2174}
2175
2176static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2177 struct ocrdma_cqe *cqe,
2178 struct ib_wc *ibwc, bool *polled)
2179{
2180 bool expand = false;
2181 int tail = qp->sq.tail;
2182 u32 wqe_idx;
2183
2184 if (!qp->wqe_wr_id_tbl[tail].signaled) {
Parav Panditfe2caef2012-03-21 04:09:06 +05302185 *polled = false; /* WC cannot be consumed yet */
2186 } else {
2187 ibwc->status = IB_WC_SUCCESS;
2188 ibwc->wc_flags = 0;
2189 ibwc->qp = &qp->ibqp;
2190 ocrdma_update_wc(qp, ibwc, tail);
2191 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302192 }
Parav Panditae3bca92012-08-17 14:45:33 +00002193 wqe_idx = le32_to_cpu(cqe->wq.wqeidx) & OCRDMA_CQE_WQEIDX_MASK;
2194 if (tail != wqe_idx)
2195 expand = true; /* Coalesced CQE can't be consumed yet */
2196
Parav Panditfe2caef2012-03-21 04:09:06 +05302197 ocrdma_hwq_inc_tail(&qp->sq);
2198 return expand;
2199}
2200
2201static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2202 struct ib_wc *ibwc, bool *polled, bool *stop)
2203{
2204 int status;
2205 bool expand;
2206
2207 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2208 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2209
2210 if (status == OCRDMA_CQE_SUCCESS)
2211 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2212 else
2213 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2214 return expand;
2215}
2216
2217static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
2218{
2219 int status;
2220
2221 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2222 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2223 ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2224 OCRDMA_CQE_SRCQP_MASK;
2225 ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
2226 OCRDMA_CQE_PKEY_MASK;
2227 ibwc->wc_flags = IB_WC_GRH;
2228 ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2229 OCRDMA_CQE_UD_XFER_LEN_SHIFT);
2230 return status;
2231}
2232
2233static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2234 struct ocrdma_cqe *cqe,
2235 struct ocrdma_qp *qp)
2236{
2237 unsigned long flags;
2238 struct ocrdma_srq *srq;
2239 u32 wqe_idx;
2240
2241 srq = get_ocrdma_srq(qp->ibqp.srq);
2242 wqe_idx = le32_to_cpu(cqe->rq.buftag_qpn) >> OCRDMA_CQE_BUFTAG_SHIFT;
2243 ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2244 spin_lock_irqsave(&srq->q_lock, flags);
2245 ocrdma_srq_toggle_bit(srq, wqe_idx);
2246 spin_unlock_irqrestore(&srq->q_lock, flags);
2247 ocrdma_hwq_inc_tail(&srq->rq);
2248}
2249
2250static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2251 struct ib_wc *ibwc, bool *polled, bool *stop,
2252 int status)
2253{
2254 bool expand;
2255
2256 /* when hw_rq is empty, but wq is not empty, so continue
2257 * to keep the cqe to get the cq event again.
2258 */
2259 if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2260 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2261 *polled = true;
2262 status = OCRDMA_CQE_WR_FLUSH_ERR;
2263 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2264 } else {
2265 *polled = false;
2266 *stop = true;
2267 expand = false;
2268 }
Parav Pandita3698a92012-06-11 16:39:20 +05302269 } else {
2270 *polled = true;
Parav Panditfe2caef2012-03-21 04:09:06 +05302271 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
Parav Pandita3698a92012-06-11 16:39:20 +05302272 }
Parav Panditfe2caef2012-03-21 04:09:06 +05302273 return expand;
2274}
2275
2276static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2277 struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2278{
2279 ibwc->opcode = IB_WC_RECV;
2280 ibwc->qp = &qp->ibqp;
2281 ibwc->status = IB_WC_SUCCESS;
2282
2283 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
2284 ocrdma_update_ud_rcqe(ibwc, cqe);
2285 else
2286 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2287
2288 if (is_cqe_imm(cqe)) {
2289 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2290 ibwc->wc_flags |= IB_WC_WITH_IMM;
2291 } else if (is_cqe_wr_imm(cqe)) {
2292 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2293 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2294 ibwc->wc_flags |= IB_WC_WITH_IMM;
2295 } else if (is_cqe_invalidated(cqe)) {
2296 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2297 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2298 }
2299 if (qp->ibqp.srq)
2300 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
2301 else {
2302 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2303 ocrdma_hwq_inc_tail(&qp->rq);
2304 }
2305}
2306
2307static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2308 struct ib_wc *ibwc, bool *polled, bool *stop)
2309{
2310 int status;
2311 bool expand = false;
2312
2313 ibwc->wc_flags = 0;
2314 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
2315 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2316 OCRDMA_CQE_UD_STATUS_MASK) >>
2317 OCRDMA_CQE_UD_STATUS_SHIFT;
2318 else
2319 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2320 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2321
2322 if (status == OCRDMA_CQE_SUCCESS) {
2323 *polled = true;
2324 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2325 } else {
2326 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2327 status);
2328 }
2329 return expand;
2330}
2331
2332static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2333 u16 cur_getp)
2334{
2335 if (cq->phase_change) {
2336 if (cur_getp == 0)
2337 cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
2338 } else
2339 /* clear valid bit */
2340 cqe->flags_status_srcqpn = 0;
2341}
2342
2343static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2344 struct ib_wc *ibwc)
2345{
2346 u16 qpn = 0;
2347 int i = 0;
2348 bool expand = false;
2349 int polled_hw_cqes = 0;
2350 struct ocrdma_qp *qp = NULL;
2351 struct ocrdma_dev *dev = cq->dev;
2352 struct ocrdma_cqe *cqe;
2353 u16 cur_getp; bool polled = false; bool stop = false;
2354
2355 cur_getp = cq->getp;
2356 while (num_entries) {
2357 cqe = cq->va + cur_getp;
2358 /* check whether valid cqe or not */
2359 if (!is_cqe_valid(cq, cqe))
2360 break;
2361 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2362 /* ignore discarded cqe */
2363 if (qpn == 0)
2364 goto skip_cqe;
2365 qp = dev->qp_tbl[qpn];
2366 BUG_ON(qp == NULL);
2367
2368 if (is_cqe_for_sq(cqe)) {
2369 expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2370 &stop);
2371 } else {
2372 expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2373 &stop);
2374 }
2375 if (expand)
2376 goto expand_cqe;
2377 if (stop)
2378 goto stop_cqe;
2379 /* clear qpn to avoid duplicate processing by discard_cqe() */
2380 cqe->cmn.qpn = 0;
2381skip_cqe:
2382 polled_hw_cqes += 1;
2383 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2384 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2385expand_cqe:
2386 if (polled) {
2387 num_entries -= 1;
2388 i += 1;
2389 ibwc = ibwc + 1;
2390 polled = false;
2391 }
2392 }
2393stop_cqe:
2394 cq->getp = cur_getp;
2395 if (polled_hw_cqes || expand || stop) {
2396 ocrdma_ring_cq_db(dev, cq->id, cq->armed, cq->solicited,
2397 polled_hw_cqes);
2398 }
2399 return i;
2400}
2401
2402/* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2403static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2404 struct ocrdma_qp *qp, struct ib_wc *ibwc)
2405{
2406 int err_cqes = 0;
2407
2408 while (num_entries) {
2409 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2410 break;
2411 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2412 ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2413 ocrdma_hwq_inc_tail(&qp->sq);
2414 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2415 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2416 ocrdma_hwq_inc_tail(&qp->rq);
2417 } else
2418 return err_cqes;
2419 ibwc->byte_len = 0;
2420 ibwc->status = IB_WC_WR_FLUSH_ERR;
2421 ibwc = ibwc + 1;
2422 err_cqes += 1;
2423 num_entries -= 1;
2424 }
2425 return err_cqes;
2426}
2427
2428int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2429{
2430 int cqes_to_poll = num_entries;
2431 struct ocrdma_cq *cq = NULL;
2432 unsigned long flags;
2433 struct ocrdma_dev *dev;
2434 int num_os_cqe = 0, err_cqes = 0;
2435 struct ocrdma_qp *qp;
2436
2437 cq = get_ocrdma_cq(ibcq);
2438 dev = cq->dev;
2439
2440 /* poll cqes from adapter CQ */
2441 spin_lock_irqsave(&cq->cq_lock, flags);
2442 num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2443 spin_unlock_irqrestore(&cq->cq_lock, flags);
2444 cqes_to_poll -= num_os_cqe;
2445
2446 if (cqes_to_poll) {
2447 wc = wc + num_os_cqe;
2448 /* adapter returns single error cqe when qp moves to
2449 * error state. So insert error cqes with wc_status as
2450 * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2451 * respectively which uses this CQ.
2452 */
2453 spin_lock_irqsave(&dev->flush_q_lock, flags);
2454 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2455 if (cqes_to_poll == 0)
2456 break;
2457 err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2458 cqes_to_poll -= err_cqes;
2459 num_os_cqe += err_cqes;
2460 wc = wc + err_cqes;
2461 }
2462 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2463 }
2464 return num_os_cqe;
2465}
2466
2467int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2468{
2469 struct ocrdma_cq *cq;
2470 unsigned long flags;
2471 struct ocrdma_dev *dev;
2472 u16 cq_id;
2473 u16 cur_getp;
2474 struct ocrdma_cqe *cqe;
2475
2476 cq = get_ocrdma_cq(ibcq);
2477 cq_id = cq->id;
2478 dev = cq->dev;
2479
2480 spin_lock_irqsave(&cq->cq_lock, flags);
2481 if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
2482 cq->armed = true;
2483 if (cq_flags & IB_CQ_SOLICITED)
2484 cq->solicited = true;
2485
2486 cur_getp = cq->getp;
2487 cqe = cq->va + cur_getp;
2488
2489 /* check whether any valid cqe exist or not, if not then safe to
2490 * arm. If cqe is not yet consumed, then let it get consumed and then
2491 * we arm it to avoid false interrupts.
2492 */
2493 if (!is_cqe_valid(cq, cqe) || cq->arm_needed) {
2494 cq->arm_needed = false;
2495 ocrdma_ring_cq_db(dev, cq_id, cq->armed, cq->solicited, 0);
2496 }
2497 spin_unlock_irqrestore(&cq->cq_lock, flags);
2498 return 0;
2499}