RDMA/ocrdma: Use non-zero tag in SRQ posting
[cascardo/linux.git] / drivers / infiniband / hw / ocrdma / ocrdma_verbs.c
1 /*******************************************************************
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
40 int 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
49 int 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));
56         if (index >= OCRDMA_MAX_SGID)
57                 return -EINVAL;
58
59         memcpy(sgid, &dev->sgid_tbl[index], sizeof(*sgid));
60
61         return 0;
62 }
63
64 int 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 = OCRDMA_MAX_AH;
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 |
86                                         IB_DEVICE_MEM_MGT_EXTENSIONS;
87         attr->max_sge = min(dev->attr.max_send_sge, dev->attr.max_srq_sge);
88         attr->max_sge_rd = 0;
89         attr->max_cq = dev->attr.max_cq;
90         attr->max_cqe = dev->attr.max_cqe;
91         attr->max_mr = dev->attr.max_mr;
92         attr->max_mw = 0;
93         attr->max_pd = dev->attr.max_pd;
94         attr->atomic_cap = 0;
95         attr->max_fmr = 0;
96         attr->max_map_per_fmr = 0;
97         attr->max_qp_rd_atom =
98             min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp);
99         attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp;
100         attr->max_srq = dev->attr.max_srq;
101         attr->max_srq_sge = dev->attr.max_srq_sge;
102         attr->max_srq_wr = dev->attr.max_rqe;
103         attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay;
104         attr->max_fast_reg_page_list_len = 0;
105         attr->max_pkeys = 1;
106         return 0;
107 }
108
109 static inline void get_link_speed_and_width(struct ocrdma_dev *dev,
110                                             u8 *ib_speed, u8 *ib_width)
111 {
112         int status;
113         u8 speed;
114
115         status = ocrdma_mbx_get_link_speed(dev, &speed);
116         if (status)
117                 speed = OCRDMA_PHYS_LINK_SPEED_ZERO;
118
119         switch (speed) {
120         case OCRDMA_PHYS_LINK_SPEED_1GBPS:
121                 *ib_speed = IB_SPEED_SDR;
122                 *ib_width = IB_WIDTH_1X;
123                 break;
124
125         case OCRDMA_PHYS_LINK_SPEED_10GBPS:
126                 *ib_speed = IB_SPEED_QDR;
127                 *ib_width = IB_WIDTH_1X;
128                 break;
129
130         case OCRDMA_PHYS_LINK_SPEED_20GBPS:
131                 *ib_speed = IB_SPEED_DDR;
132                 *ib_width = IB_WIDTH_4X;
133                 break;
134
135         case OCRDMA_PHYS_LINK_SPEED_40GBPS:
136                 *ib_speed = IB_SPEED_QDR;
137                 *ib_width = IB_WIDTH_4X;
138                 break;
139
140         default:
141                 /* Unsupported */
142                 *ib_speed = IB_SPEED_SDR;
143                 *ib_width = IB_WIDTH_1X;
144         }
145 }
146
147
148 int ocrdma_query_port(struct ib_device *ibdev,
149                       u8 port, struct ib_port_attr *props)
150 {
151         enum ib_port_state port_state;
152         struct ocrdma_dev *dev;
153         struct net_device *netdev;
154
155         dev = get_ocrdma_dev(ibdev);
156         if (port > 1) {
157                 pr_err("%s(%d) invalid_port=0x%x\n", __func__,
158                        dev->id, port);
159                 return -EINVAL;
160         }
161         netdev = dev->nic_info.netdev;
162         if (netif_running(netdev) && netif_oper_up(netdev)) {
163                 port_state = IB_PORT_ACTIVE;
164                 props->phys_state = 5;
165         } else {
166                 port_state = IB_PORT_DOWN;
167                 props->phys_state = 3;
168         }
169         props->max_mtu = IB_MTU_4096;
170         props->active_mtu = iboe_get_mtu(netdev->mtu);
171         props->lid = 0;
172         props->lmc = 0;
173         props->sm_lid = 0;
174         props->sm_sl = 0;
175         props->state = port_state;
176         props->port_cap_flags =
177             IB_PORT_CM_SUP |
178             IB_PORT_REINIT_SUP |
179             IB_PORT_DEVICE_MGMT_SUP | IB_PORT_VENDOR_CLASS_SUP | IB_PORT_IP_BASED_GIDS;
180         props->gid_tbl_len = OCRDMA_MAX_SGID;
181         props->pkey_tbl_len = 1;
182         props->bad_pkey_cntr = 0;
183         props->qkey_viol_cntr = 0;
184         get_link_speed_and_width(dev, &props->active_speed,
185                                  &props->active_width);
186         props->max_msg_sz = 0x80000000;
187         props->max_vl_num = 4;
188         return 0;
189 }
190
191 int ocrdma_modify_port(struct ib_device *ibdev, u8 port, int mask,
192                        struct ib_port_modify *props)
193 {
194         struct ocrdma_dev *dev;
195
196         dev = get_ocrdma_dev(ibdev);
197         if (port > 1) {
198                 pr_err("%s(%d) invalid_port=0x%x\n", __func__, dev->id, port);
199                 return -EINVAL;
200         }
201         return 0;
202 }
203
204 static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
205                            unsigned long len)
206 {
207         struct ocrdma_mm *mm;
208
209         mm = kzalloc(sizeof(*mm), GFP_KERNEL);
210         if (mm == NULL)
211                 return -ENOMEM;
212         mm->key.phy_addr = phy_addr;
213         mm->key.len = len;
214         INIT_LIST_HEAD(&mm->entry);
215
216         mutex_lock(&uctx->mm_list_lock);
217         list_add_tail(&mm->entry, &uctx->mm_head);
218         mutex_unlock(&uctx->mm_list_lock);
219         return 0;
220 }
221
222 static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
223                             unsigned long len)
224 {
225         struct ocrdma_mm *mm, *tmp;
226
227         mutex_lock(&uctx->mm_list_lock);
228         list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
229                 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
230                         continue;
231
232                 list_del(&mm->entry);
233                 kfree(mm);
234                 break;
235         }
236         mutex_unlock(&uctx->mm_list_lock);
237 }
238
239 static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr,
240                               unsigned long len)
241 {
242         bool found = false;
243         struct ocrdma_mm *mm;
244
245         mutex_lock(&uctx->mm_list_lock);
246         list_for_each_entry(mm, &uctx->mm_head, entry) {
247                 if (len != mm->key.len && phy_addr != mm->key.phy_addr)
248                         continue;
249
250                 found = true;
251                 break;
252         }
253         mutex_unlock(&uctx->mm_list_lock);
254         return found;
255 }
256
257 static struct ocrdma_pd *_ocrdma_alloc_pd(struct ocrdma_dev *dev,
258                                           struct ocrdma_ucontext *uctx,
259                                           struct ib_udata *udata)
260 {
261         struct ocrdma_pd *pd = NULL;
262         int status = 0;
263
264         pd = kzalloc(sizeof(*pd), GFP_KERNEL);
265         if (!pd)
266                 return ERR_PTR(-ENOMEM);
267
268         if (udata && uctx) {
269                 pd->dpp_enabled =
270                         ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R;
271                 pd->num_dpp_qp =
272                         pd->dpp_enabled ? OCRDMA_PD_MAX_DPP_ENABLED_QP : 0;
273         }
274
275 retry:
276         status = ocrdma_mbx_alloc_pd(dev, pd);
277         if (status) {
278                 if (pd->dpp_enabled) {
279                         pd->dpp_enabled = false;
280                         pd->num_dpp_qp = 0;
281                         goto retry;
282                 } else {
283                         kfree(pd);
284                         return ERR_PTR(status);
285                 }
286         }
287
288         return pd;
289 }
290
291 static inline int is_ucontext_pd(struct ocrdma_ucontext *uctx,
292                                  struct ocrdma_pd *pd)
293 {
294         return (uctx->cntxt_pd == pd ? true : false);
295 }
296
297 static int _ocrdma_dealloc_pd(struct ocrdma_dev *dev,
298                               struct ocrdma_pd *pd)
299 {
300         int status = 0;
301
302         status = ocrdma_mbx_dealloc_pd(dev, pd);
303         kfree(pd);
304         return status;
305 }
306
307 static int ocrdma_alloc_ucontext_pd(struct ocrdma_dev *dev,
308                                     struct ocrdma_ucontext *uctx,
309                                     struct ib_udata *udata)
310 {
311         int status = 0;
312
313         uctx->cntxt_pd = _ocrdma_alloc_pd(dev, uctx, udata);
314         if (IS_ERR(uctx->cntxt_pd)) {
315                 status = PTR_ERR(uctx->cntxt_pd);
316                 uctx->cntxt_pd = NULL;
317                 goto err;
318         }
319
320         uctx->cntxt_pd->uctx = uctx;
321         uctx->cntxt_pd->ibpd.device = &dev->ibdev;
322 err:
323         return status;
324 }
325
326 static int ocrdma_dealloc_ucontext_pd(struct ocrdma_ucontext *uctx)
327 {
328         int status = 0;
329         struct ocrdma_pd *pd = uctx->cntxt_pd;
330         struct ocrdma_dev *dev = get_ocrdma_dev(pd->ibpd.device);
331
332         BUG_ON(uctx->pd_in_use);
333         uctx->cntxt_pd = NULL;
334         status = _ocrdma_dealloc_pd(dev, pd);
335         return status;
336 }
337
338 static struct ocrdma_pd *ocrdma_get_ucontext_pd(struct ocrdma_ucontext *uctx)
339 {
340         struct ocrdma_pd *pd = NULL;
341
342         mutex_lock(&uctx->mm_list_lock);
343         if (!uctx->pd_in_use) {
344                 uctx->pd_in_use = true;
345                 pd = uctx->cntxt_pd;
346         }
347         mutex_unlock(&uctx->mm_list_lock);
348
349         return pd;
350 }
351
352 static void ocrdma_release_ucontext_pd(struct ocrdma_ucontext *uctx)
353 {
354         mutex_lock(&uctx->mm_list_lock);
355         uctx->pd_in_use = false;
356         mutex_unlock(&uctx->mm_list_lock);
357 }
358
359 struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev,
360                                           struct ib_udata *udata)
361 {
362         int status;
363         struct ocrdma_ucontext *ctx;
364         struct ocrdma_alloc_ucontext_resp resp;
365         struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
366         struct pci_dev *pdev = dev->nic_info.pdev;
367         u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE);
368
369         if (!udata)
370                 return ERR_PTR(-EFAULT);
371         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
372         if (!ctx)
373                 return ERR_PTR(-ENOMEM);
374         INIT_LIST_HEAD(&ctx->mm_head);
375         mutex_init(&ctx->mm_list_lock);
376
377         ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len,
378                                             &ctx->ah_tbl.pa, GFP_KERNEL);
379         if (!ctx->ah_tbl.va) {
380                 kfree(ctx);
381                 return ERR_PTR(-ENOMEM);
382         }
383         memset(ctx->ah_tbl.va, 0, map_len);
384         ctx->ah_tbl.len = map_len;
385
386         memset(&resp, 0, sizeof(resp));
387         resp.ah_tbl_len = ctx->ah_tbl.len;
388         resp.ah_tbl_page = ctx->ah_tbl.pa;
389
390         status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len);
391         if (status)
392                 goto map_err;
393
394         status = ocrdma_alloc_ucontext_pd(dev, ctx, udata);
395         if (status)
396                 goto pd_err;
397
398         resp.dev_id = dev->id;
399         resp.max_inline_data = dev->attr.max_inline_data;
400         resp.wqe_size = dev->attr.wqe_size;
401         resp.rqe_size = dev->attr.rqe_size;
402         resp.dpp_wqe_size = dev->attr.wqe_size;
403
404         memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver));
405         status = ib_copy_to_udata(udata, &resp, sizeof(resp));
406         if (status)
407                 goto cpy_err;
408         return &ctx->ibucontext;
409
410 cpy_err:
411 pd_err:
412         ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len);
413 map_err:
414         dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va,
415                           ctx->ah_tbl.pa);
416         kfree(ctx);
417         return ERR_PTR(status);
418 }
419
420 int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx)
421 {
422         int status = 0;
423         struct ocrdma_mm *mm, *tmp;
424         struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx);
425         struct ocrdma_dev *dev = get_ocrdma_dev(ibctx->device);
426         struct pci_dev *pdev = dev->nic_info.pdev;
427
428         status = ocrdma_dealloc_ucontext_pd(uctx);
429
430         ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len);
431         dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va,
432                           uctx->ah_tbl.pa);
433
434         list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) {
435                 list_del(&mm->entry);
436                 kfree(mm);
437         }
438         kfree(uctx);
439         return status;
440 }
441
442 int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
443 {
444         struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context);
445         struct ocrdma_dev *dev = get_ocrdma_dev(context->device);
446         unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT;
447         u64 unmapped_db = (u64) dev->nic_info.unmapped_db;
448         unsigned long len = (vma->vm_end - vma->vm_start);
449         int status = 0;
450         bool found;
451
452         if (vma->vm_start & (PAGE_SIZE - 1))
453                 return -EINVAL;
454         found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len);
455         if (!found)
456                 return -EINVAL;
457
458         if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db +
459                 dev->nic_info.db_total_size)) &&
460                 (len <= dev->nic_info.db_page_size)) {
461                 if (vma->vm_flags & VM_READ)
462                         return -EPERM;
463
464                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
465                 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
466                                             len, vma->vm_page_prot);
467         } else if (dev->nic_info.dpp_unmapped_len &&
468                 (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) &&
469                 (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr +
470                         dev->nic_info.dpp_unmapped_len)) &&
471                 (len <= dev->nic_info.dpp_unmapped_len)) {
472                 if (vma->vm_flags & VM_READ)
473                         return -EPERM;
474
475                 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
476                 status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
477                                             len, vma->vm_page_prot);
478         } else {
479                 status = remap_pfn_range(vma, vma->vm_start,
480                                          vma->vm_pgoff, len, vma->vm_page_prot);
481         }
482         return status;
483 }
484
485 static int ocrdma_copy_pd_uresp(struct ocrdma_dev *dev, struct ocrdma_pd *pd,
486                                 struct ib_ucontext *ib_ctx,
487                                 struct ib_udata *udata)
488 {
489         int status;
490         u64 db_page_addr;
491         u64 dpp_page_addr = 0;
492         u32 db_page_size;
493         struct ocrdma_alloc_pd_uresp rsp;
494         struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
495
496         memset(&rsp, 0, sizeof(rsp));
497         rsp.id = pd->id;
498         rsp.dpp_enabled = pd->dpp_enabled;
499         db_page_addr = ocrdma_get_db_addr(dev, pd->id);
500         db_page_size = dev->nic_info.db_page_size;
501
502         status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size);
503         if (status)
504                 return status;
505
506         if (pd->dpp_enabled) {
507                 dpp_page_addr = dev->nic_info.dpp_unmapped_addr +
508                                 (pd->id * PAGE_SIZE);
509                 status = ocrdma_add_mmap(uctx, dpp_page_addr,
510                                  PAGE_SIZE);
511                 if (status)
512                         goto dpp_map_err;
513                 rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr);
514                 rsp.dpp_page_addr_lo = dpp_page_addr;
515         }
516
517         status = ib_copy_to_udata(udata, &rsp, sizeof(rsp));
518         if (status)
519                 goto ucopy_err;
520
521         pd->uctx = uctx;
522         return 0;
523
524 ucopy_err:
525         if (pd->dpp_enabled)
526                 ocrdma_del_mmap(pd->uctx, dpp_page_addr, PAGE_SIZE);
527 dpp_map_err:
528         ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size);
529         return status;
530 }
531
532 struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev,
533                               struct ib_ucontext *context,
534                               struct ib_udata *udata)
535 {
536         struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
537         struct ocrdma_pd *pd;
538         struct ocrdma_ucontext *uctx = NULL;
539         int status;
540         u8 is_uctx_pd = false;
541
542         if (udata && context) {
543                 uctx = get_ocrdma_ucontext(context);
544                 pd = ocrdma_get_ucontext_pd(uctx);
545                 if (pd) {
546                         is_uctx_pd = true;
547                         goto pd_mapping;
548                 }
549         }
550
551         pd = _ocrdma_alloc_pd(dev, uctx, udata);
552         if (IS_ERR(pd)) {
553                 status = PTR_ERR(pd);
554                 goto exit;
555         }
556
557 pd_mapping:
558         if (udata && context) {
559                 status = ocrdma_copy_pd_uresp(dev, pd, context, udata);
560                 if (status)
561                         goto err;
562         }
563         return &pd->ibpd;
564
565 err:
566         if (is_uctx_pd) {
567                 ocrdma_release_ucontext_pd(uctx);
568         } else {
569                 status = ocrdma_mbx_dealloc_pd(dev, pd);
570                 kfree(pd);
571         }
572 exit:
573         return ERR_PTR(status);
574 }
575
576 int ocrdma_dealloc_pd(struct ib_pd *ibpd)
577 {
578         struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
579         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
580         struct ocrdma_ucontext *uctx = NULL;
581         int status = 0;
582         u64 usr_db;
583
584         uctx = pd->uctx;
585         if (uctx) {
586                 u64 dpp_db = dev->nic_info.dpp_unmapped_addr +
587                         (pd->id * PAGE_SIZE);
588                 if (pd->dpp_enabled)
589                         ocrdma_del_mmap(pd->uctx, dpp_db, PAGE_SIZE);
590                 usr_db = ocrdma_get_db_addr(dev, pd->id);
591                 ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size);
592
593                 if (is_ucontext_pd(uctx, pd)) {
594                         ocrdma_release_ucontext_pd(uctx);
595                         return status;
596                 }
597         }
598         status = _ocrdma_dealloc_pd(dev, pd);
599         return status;
600 }
601
602 static int ocrdma_alloc_lkey(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
603                             u32 pdid, int acc, u32 num_pbls, u32 addr_check)
604 {
605         int status;
606
607         mr->hwmr.fr_mr = 0;
608         mr->hwmr.local_rd = 1;
609         mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
610         mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
611         mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
612         mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
613         mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
614         mr->hwmr.num_pbls = num_pbls;
615
616         status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pdid, addr_check);
617         if (status)
618                 return status;
619
620         mr->ibmr.lkey = mr->hwmr.lkey;
621         if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
622                 mr->ibmr.rkey = mr->hwmr.lkey;
623         return 0;
624 }
625
626 struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc)
627 {
628         int status;
629         struct ocrdma_mr *mr;
630         struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
631         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
632
633         if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) {
634                 pr_err("%s err, invalid access rights\n", __func__);
635                 return ERR_PTR(-EINVAL);
636         }
637
638         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
639         if (!mr)
640                 return ERR_PTR(-ENOMEM);
641
642         status = ocrdma_alloc_lkey(dev, mr, pd->id, acc, 0,
643                                    OCRDMA_ADDR_CHECK_DISABLE);
644         if (status) {
645                 kfree(mr);
646                 return ERR_PTR(status);
647         }
648
649         return &mr->ibmr;
650 }
651
652 static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev,
653                                    struct ocrdma_hw_mr *mr)
654 {
655         struct pci_dev *pdev = dev->nic_info.pdev;
656         int i = 0;
657
658         if (mr->pbl_table) {
659                 for (i = 0; i < mr->num_pbls; i++) {
660                         if (!mr->pbl_table[i].va)
661                                 continue;
662                         dma_free_coherent(&pdev->dev, mr->pbl_size,
663                                           mr->pbl_table[i].va,
664                                           mr->pbl_table[i].pa);
665                 }
666                 kfree(mr->pbl_table);
667                 mr->pbl_table = NULL;
668         }
669 }
670
671 static int ocrdma_get_pbl_info(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
672                               u32 num_pbes)
673 {
674         u32 num_pbls = 0;
675         u32 idx = 0;
676         int status = 0;
677         u32 pbl_size;
678
679         do {
680                 pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx);
681                 if (pbl_size > MAX_OCRDMA_PBL_SIZE) {
682                         status = -EFAULT;
683                         break;
684                 }
685                 num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64)));
686                 num_pbls = num_pbls / (pbl_size / sizeof(u64));
687                 idx++;
688         } while (num_pbls >= dev->attr.max_num_mr_pbl);
689
690         mr->hwmr.num_pbes = num_pbes;
691         mr->hwmr.num_pbls = num_pbls;
692         mr->hwmr.pbl_size = pbl_size;
693         return status;
694 }
695
696 static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr)
697 {
698         int status = 0;
699         int i;
700         u32 dma_len = mr->pbl_size;
701         struct pci_dev *pdev = dev->nic_info.pdev;
702         void *va;
703         dma_addr_t pa;
704
705         mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) *
706                                 mr->num_pbls, GFP_KERNEL);
707
708         if (!mr->pbl_table)
709                 return -ENOMEM;
710
711         for (i = 0; i < mr->num_pbls; i++) {
712                 va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL);
713                 if (!va) {
714                         ocrdma_free_mr_pbl_tbl(dev, mr);
715                         status = -ENOMEM;
716                         break;
717                 }
718                 memset(va, 0, dma_len);
719                 mr->pbl_table[i].va = va;
720                 mr->pbl_table[i].pa = pa;
721         }
722         return status;
723 }
724
725 static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr,
726                             u32 num_pbes)
727 {
728         struct ocrdma_pbe *pbe;
729         struct ib_umem_chunk *chunk;
730         struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table;
731         struct ib_umem *umem = mr->umem;
732         int i, shift, pg_cnt, pages, pbe_cnt, total_num_pbes = 0;
733
734         if (!mr->hwmr.num_pbes)
735                 return;
736
737         pbe = (struct ocrdma_pbe *)pbl_tbl->va;
738         pbe_cnt = 0;
739
740         shift = ilog2(umem->page_size);
741
742         list_for_each_entry(chunk, &umem->chunk_list, list) {
743                 /* get all the dma regions from the chunk. */
744                 for (i = 0; i < chunk->nmap; i++) {
745                         pages = sg_dma_len(&chunk->page_list[i]) >> shift;
746                         for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) {
747                                 /* store the page address in pbe */
748                                 pbe->pa_lo =
749                                     cpu_to_le32(sg_dma_address
750                                                 (&chunk->page_list[i]) +
751                                                 (umem->page_size * pg_cnt));
752                                 pbe->pa_hi =
753                                     cpu_to_le32(upper_32_bits
754                                                 ((sg_dma_address
755                                                   (&chunk->page_list[i]) +
756                                                   umem->page_size * pg_cnt)));
757                                 pbe_cnt += 1;
758                                 total_num_pbes += 1;
759                                 pbe++;
760
761                                 /* if done building pbes, issue the mbx cmd. */
762                                 if (total_num_pbes == num_pbes)
763                                         return;
764
765                                 /* if the given pbl is full storing the pbes,
766                                  * move to next pbl.
767                                  */
768                                 if (pbe_cnt ==
769                                         (mr->hwmr.pbl_size / sizeof(u64))) {
770                                         pbl_tbl++;
771                                         pbe = (struct ocrdma_pbe *)pbl_tbl->va;
772                                         pbe_cnt = 0;
773                                 }
774                         }
775                 }
776         }
777 }
778
779 struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len,
780                                  u64 usr_addr, int acc, struct ib_udata *udata)
781 {
782         int status = -ENOMEM;
783         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
784         struct ocrdma_mr *mr;
785         struct ocrdma_pd *pd;
786         u32 num_pbes;
787
788         pd = get_ocrdma_pd(ibpd);
789
790         if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE))
791                 return ERR_PTR(-EINVAL);
792
793         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
794         if (!mr)
795                 return ERR_PTR(status);
796         mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0);
797         if (IS_ERR(mr->umem)) {
798                 status = -EFAULT;
799                 goto umem_err;
800         }
801         num_pbes = ib_umem_page_count(mr->umem);
802         status = ocrdma_get_pbl_info(dev, mr, num_pbes);
803         if (status)
804                 goto umem_err;
805
806         mr->hwmr.pbe_size = mr->umem->page_size;
807         mr->hwmr.fbo = mr->umem->offset;
808         mr->hwmr.va = usr_addr;
809         mr->hwmr.len = len;
810         mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
811         mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
812         mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
813         mr->hwmr.local_rd = 1;
814         mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
815         status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
816         if (status)
817                 goto umem_err;
818         build_user_pbes(dev, mr, num_pbes);
819         status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
820         if (status)
821                 goto mbx_err;
822         mr->ibmr.lkey = mr->hwmr.lkey;
823         if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
824                 mr->ibmr.rkey = mr->hwmr.lkey;
825
826         return &mr->ibmr;
827
828 mbx_err:
829         ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
830 umem_err:
831         kfree(mr);
832         return ERR_PTR(status);
833 }
834
835 int ocrdma_dereg_mr(struct ib_mr *ib_mr)
836 {
837         struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr);
838         struct ocrdma_dev *dev = get_ocrdma_dev(ib_mr->device);
839         int status;
840
841         status = ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey);
842
843         ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
844
845         /* it could be user registered memory. */
846         if (mr->umem)
847                 ib_umem_release(mr->umem);
848         kfree(mr);
849         return status;
850 }
851
852 static int ocrdma_copy_cq_uresp(struct ocrdma_dev *dev, struct ocrdma_cq *cq,
853                                 struct ib_udata *udata,
854                                 struct ib_ucontext *ib_ctx)
855 {
856         int status;
857         struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx);
858         struct ocrdma_create_cq_uresp uresp;
859
860         memset(&uresp, 0, sizeof(uresp));
861         uresp.cq_id = cq->id;
862         uresp.page_size = PAGE_ALIGN(cq->len);
863         uresp.num_pages = 1;
864         uresp.max_hw_cqe = cq->max_hw_cqe;
865         uresp.page_addr[0] = cq->pa;
866         uresp.db_page_addr =  ocrdma_get_db_addr(dev, uctx->cntxt_pd->id);
867         uresp.db_page_size = dev->nic_info.db_page_size;
868         uresp.phase_change = cq->phase_change ? 1 : 0;
869         status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
870         if (status) {
871                 pr_err("%s(%d) copy error cqid=0x%x.\n",
872                        __func__, dev->id, cq->id);
873                 goto err;
874         }
875         status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
876         if (status)
877                 goto err;
878         status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size);
879         if (status) {
880                 ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size);
881                 goto err;
882         }
883         cq->ucontext = uctx;
884 err:
885         return status;
886 }
887
888 struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector,
889                                struct ib_ucontext *ib_ctx,
890                                struct ib_udata *udata)
891 {
892         struct ocrdma_cq *cq;
893         struct ocrdma_dev *dev = get_ocrdma_dev(ibdev);
894         struct ocrdma_ucontext *uctx = NULL;
895         u16 pd_id = 0;
896         int status;
897         struct ocrdma_create_cq_ureq ureq;
898
899         if (udata) {
900                 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
901                         return ERR_PTR(-EFAULT);
902         } else
903                 ureq.dpp_cq = 0;
904         cq = kzalloc(sizeof(*cq), GFP_KERNEL);
905         if (!cq)
906                 return ERR_PTR(-ENOMEM);
907
908         spin_lock_init(&cq->cq_lock);
909         spin_lock_init(&cq->comp_handler_lock);
910         INIT_LIST_HEAD(&cq->sq_head);
911         INIT_LIST_HEAD(&cq->rq_head);
912         cq->first_arm = true;
913
914         if (ib_ctx) {
915                 uctx = get_ocrdma_ucontext(ib_ctx);
916                 pd_id = uctx->cntxt_pd->id;
917         }
918
919         status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq, pd_id);
920         if (status) {
921                 kfree(cq);
922                 return ERR_PTR(status);
923         }
924         if (ib_ctx) {
925                 status = ocrdma_copy_cq_uresp(dev, cq, udata, ib_ctx);
926                 if (status)
927                         goto ctx_err;
928         }
929         cq->phase = OCRDMA_CQE_VALID;
930         dev->cq_tbl[cq->id] = cq;
931         return &cq->ibcq;
932
933 ctx_err:
934         ocrdma_mbx_destroy_cq(dev, cq);
935         kfree(cq);
936         return ERR_PTR(status);
937 }
938
939 int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt,
940                      struct ib_udata *udata)
941 {
942         int status = 0;
943         struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
944
945         if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) {
946                 status = -EINVAL;
947                 return status;
948         }
949         ibcq->cqe = new_cnt;
950         return status;
951 }
952
953 static void ocrdma_flush_cq(struct ocrdma_cq *cq)
954 {
955         int cqe_cnt;
956         int valid_count = 0;
957         unsigned long flags;
958
959         struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
960         struct ocrdma_cqe *cqe = NULL;
961
962         cqe = cq->va;
963         cqe_cnt = cq->cqe_cnt;
964
965         /* Last irq might have scheduled a polling thread
966          * sync-up with it before hard flushing.
967          */
968         spin_lock_irqsave(&cq->cq_lock, flags);
969         while (cqe_cnt) {
970                 if (is_cqe_valid(cq, cqe))
971                         valid_count++;
972                 cqe++;
973                 cqe_cnt--;
974         }
975         ocrdma_ring_cq_db(dev, cq->id, false, false, valid_count);
976         spin_unlock_irqrestore(&cq->cq_lock, flags);
977 }
978
979 int ocrdma_destroy_cq(struct ib_cq *ibcq)
980 {
981         int status;
982         struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
983         struct ocrdma_eq *eq = NULL;
984         struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
985         int pdid = 0;
986         u32 irq, indx;
987
988         dev->cq_tbl[cq->id] = NULL;
989         indx = ocrdma_get_eq_table_index(dev, cq->eqn);
990         if (indx == -EINVAL)
991                 BUG();
992
993         eq = &dev->eq_tbl[indx];
994         irq = ocrdma_get_irq(dev, eq);
995         synchronize_irq(irq);
996         ocrdma_flush_cq(cq);
997
998         status = ocrdma_mbx_destroy_cq(dev, cq);
999         if (cq->ucontext) {
1000                 pdid = cq->ucontext->cntxt_pd->id;
1001                 ocrdma_del_mmap(cq->ucontext, (u64) cq->pa,
1002                                 PAGE_ALIGN(cq->len));
1003                 ocrdma_del_mmap(cq->ucontext,
1004                                 ocrdma_get_db_addr(dev, pdid),
1005                                 dev->nic_info.db_page_size);
1006         }
1007
1008         kfree(cq);
1009         return status;
1010 }
1011
1012 static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1013 {
1014         int status = -EINVAL;
1015
1016         if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) {
1017                 dev->qp_tbl[qp->id] = qp;
1018                 status = 0;
1019         }
1020         return status;
1021 }
1022
1023 static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp)
1024 {
1025         dev->qp_tbl[qp->id] = NULL;
1026 }
1027
1028 static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev,
1029                                   struct ib_qp_init_attr *attrs)
1030 {
1031         if ((attrs->qp_type != IB_QPT_GSI) &&
1032             (attrs->qp_type != IB_QPT_RC) &&
1033             (attrs->qp_type != IB_QPT_UC) &&
1034             (attrs->qp_type != IB_QPT_UD)) {
1035                 pr_err("%s(%d) unsupported qp type=0x%x requested\n",
1036                        __func__, dev->id, attrs->qp_type);
1037                 return -EINVAL;
1038         }
1039         /* Skip the check for QP1 to support CM size of 128 */
1040         if ((attrs->qp_type != IB_QPT_GSI) &&
1041             (attrs->cap.max_send_wr > dev->attr.max_wqe)) {
1042                 pr_err("%s(%d) unsupported send_wr=0x%x requested\n",
1043                        __func__, dev->id, attrs->cap.max_send_wr);
1044                 pr_err("%s(%d) supported send_wr=0x%x\n",
1045                        __func__, dev->id, dev->attr.max_wqe);
1046                 return -EINVAL;
1047         }
1048         if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) {
1049                 pr_err("%s(%d) unsupported recv_wr=0x%x requested\n",
1050                        __func__, dev->id, attrs->cap.max_recv_wr);
1051                 pr_err("%s(%d) supported recv_wr=0x%x\n",
1052                        __func__, dev->id, dev->attr.max_rqe);
1053                 return -EINVAL;
1054         }
1055         if (attrs->cap.max_inline_data > dev->attr.max_inline_data) {
1056                 pr_err("%s(%d) unsupported inline data size=0x%x requested\n",
1057                        __func__, dev->id, attrs->cap.max_inline_data);
1058                 pr_err("%s(%d) supported inline data size=0x%x\n",
1059                        __func__, dev->id, dev->attr.max_inline_data);
1060                 return -EINVAL;
1061         }
1062         if (attrs->cap.max_send_sge > dev->attr.max_send_sge) {
1063                 pr_err("%s(%d) unsupported send_sge=0x%x requested\n",
1064                        __func__, dev->id, attrs->cap.max_send_sge);
1065                 pr_err("%s(%d) supported send_sge=0x%x\n",
1066                        __func__, dev->id, dev->attr.max_send_sge);
1067                 return -EINVAL;
1068         }
1069         if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) {
1070                 pr_err("%s(%d) unsupported recv_sge=0x%x requested\n",
1071                        __func__, dev->id, attrs->cap.max_recv_sge);
1072                 pr_err("%s(%d) supported recv_sge=0x%x\n",
1073                        __func__, dev->id, dev->attr.max_recv_sge);
1074                 return -EINVAL;
1075         }
1076         /* unprivileged user space cannot create special QP */
1077         if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) {
1078                 pr_err
1079                     ("%s(%d) Userspace can't create special QPs of type=0x%x\n",
1080                      __func__, dev->id, attrs->qp_type);
1081                 return -EINVAL;
1082         }
1083         /* allow creating only one GSI type of QP */
1084         if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) {
1085                 pr_err("%s(%d) GSI special QPs already created.\n",
1086                        __func__, dev->id);
1087                 return -EINVAL;
1088         }
1089         /* verify consumer QPs are not trying to use GSI QP's CQ */
1090         if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) {
1091                 if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) ||
1092                         (dev->gsi_rqcq == get_ocrdma_cq(attrs->recv_cq))) {
1093                         pr_err("%s(%d) Consumer QP cannot use GSI CQs.\n",
1094                                 __func__, dev->id);
1095                         return -EINVAL;
1096                 }
1097         }
1098         return 0;
1099 }
1100
1101 static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp,
1102                                 struct ib_udata *udata, int dpp_offset,
1103                                 int dpp_credit_lmt, int srq)
1104 {
1105         int status = 0;
1106         u64 usr_db;
1107         struct ocrdma_create_qp_uresp uresp;
1108         struct ocrdma_dev *dev = qp->dev;
1109         struct ocrdma_pd *pd = qp->pd;
1110
1111         memset(&uresp, 0, sizeof(uresp));
1112         usr_db = dev->nic_info.unmapped_db +
1113                         (pd->id * dev->nic_info.db_page_size);
1114         uresp.qp_id = qp->id;
1115         uresp.sq_dbid = qp->sq.dbid;
1116         uresp.num_sq_pages = 1;
1117         uresp.sq_page_size = PAGE_ALIGN(qp->sq.len);
1118         uresp.sq_page_addr[0] = qp->sq.pa;
1119         uresp.num_wqe_allocated = qp->sq.max_cnt;
1120         if (!srq) {
1121                 uresp.rq_dbid = qp->rq.dbid;
1122                 uresp.num_rq_pages = 1;
1123                 uresp.rq_page_size = PAGE_ALIGN(qp->rq.len);
1124                 uresp.rq_page_addr[0] = qp->rq.pa;
1125                 uresp.num_rqe_allocated = qp->rq.max_cnt;
1126         }
1127         uresp.db_page_addr = usr_db;
1128         uresp.db_page_size = dev->nic_info.db_page_size;
1129         uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET;
1130         uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
1131         uresp.db_shift = OCRDMA_DB_RQ_SHIFT;
1132
1133         if (qp->dpp_enabled) {
1134                 uresp.dpp_credit = dpp_credit_lmt;
1135                 uresp.dpp_offset = dpp_offset;
1136         }
1137         status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1138         if (status) {
1139                 pr_err("%s(%d) user copy error.\n", __func__, dev->id);
1140                 goto err;
1141         }
1142         status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0],
1143                                  uresp.sq_page_size);
1144         if (status)
1145                 goto err;
1146
1147         if (!srq) {
1148                 status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0],
1149                                          uresp.rq_page_size);
1150                 if (status)
1151                         goto rq_map_err;
1152         }
1153         return status;
1154 rq_map_err:
1155         ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size);
1156 err:
1157         return status;
1158 }
1159
1160 static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp,
1161                              struct ocrdma_pd *pd)
1162 {
1163         if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
1164                 qp->sq_db = dev->nic_info.db +
1165                         (pd->id * dev->nic_info.db_page_size) +
1166                         OCRDMA_DB_GEN2_SQ_OFFSET;
1167                 qp->rq_db = dev->nic_info.db +
1168                         (pd->id * dev->nic_info.db_page_size) +
1169                         OCRDMA_DB_GEN2_RQ_OFFSET;
1170         } else {
1171                 qp->sq_db = dev->nic_info.db +
1172                         (pd->id * dev->nic_info.db_page_size) +
1173                         OCRDMA_DB_SQ_OFFSET;
1174                 qp->rq_db = dev->nic_info.db +
1175                         (pd->id * dev->nic_info.db_page_size) +
1176                         OCRDMA_DB_RQ_OFFSET;
1177         }
1178 }
1179
1180 static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp)
1181 {
1182         qp->wqe_wr_id_tbl =
1183             kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt,
1184                     GFP_KERNEL);
1185         if (qp->wqe_wr_id_tbl == NULL)
1186                 return -ENOMEM;
1187         qp->rqe_wr_id_tbl =
1188             kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL);
1189         if (qp->rqe_wr_id_tbl == NULL)
1190                 return -ENOMEM;
1191
1192         return 0;
1193 }
1194
1195 static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp,
1196                                       struct ocrdma_pd *pd,
1197                                       struct ib_qp_init_attr *attrs)
1198 {
1199         qp->pd = pd;
1200         spin_lock_init(&qp->q_lock);
1201         INIT_LIST_HEAD(&qp->sq_entry);
1202         INIT_LIST_HEAD(&qp->rq_entry);
1203
1204         qp->qp_type = attrs->qp_type;
1205         qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR;
1206         qp->max_inline_data = attrs->cap.max_inline_data;
1207         qp->sq.max_sges = attrs->cap.max_send_sge;
1208         qp->rq.max_sges = attrs->cap.max_recv_sge;
1209         qp->state = OCRDMA_QPS_RST;
1210         qp->signaled = (attrs->sq_sig_type == IB_SIGNAL_ALL_WR) ? true : false;
1211 }
1212
1213
1214 static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev,
1215                                    struct ib_qp_init_attr *attrs)
1216 {
1217         if (attrs->qp_type == IB_QPT_GSI) {
1218                 dev->gsi_qp_created = 1;
1219                 dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq);
1220                 dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq);
1221         }
1222 }
1223
1224 struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd,
1225                                struct ib_qp_init_attr *attrs,
1226                                struct ib_udata *udata)
1227 {
1228         int status;
1229         struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1230         struct ocrdma_qp *qp;
1231         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
1232         struct ocrdma_create_qp_ureq ureq;
1233         u16 dpp_credit_lmt, dpp_offset;
1234
1235         status = ocrdma_check_qp_params(ibpd, dev, attrs);
1236         if (status)
1237                 goto gen_err;
1238
1239         memset(&ureq, 0, sizeof(ureq));
1240         if (udata) {
1241                 if (ib_copy_from_udata(&ureq, udata, sizeof(ureq)))
1242                         return ERR_PTR(-EFAULT);
1243         }
1244         qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1245         if (!qp) {
1246                 status = -ENOMEM;
1247                 goto gen_err;
1248         }
1249         qp->dev = dev;
1250         ocrdma_set_qp_init_params(qp, pd, attrs);
1251         if (udata == NULL)
1252                 qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 |
1253                                         OCRDMA_QP_FAST_REG);
1254
1255         mutex_lock(&dev->dev_lock);
1256         status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq,
1257                                         ureq.dpp_cq_id,
1258                                         &dpp_offset, &dpp_credit_lmt);
1259         if (status)
1260                 goto mbx_err;
1261
1262         /* user space QP's wr_id table are managed in library */
1263         if (udata == NULL) {
1264                 status = ocrdma_alloc_wr_id_tbl(qp);
1265                 if (status)
1266                         goto map_err;
1267         }
1268
1269         status = ocrdma_add_qpn_map(dev, qp);
1270         if (status)
1271                 goto map_err;
1272         ocrdma_set_qp_db(dev, qp, pd);
1273         if (udata) {
1274                 status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset,
1275                                               dpp_credit_lmt,
1276                                               (attrs->srq != NULL));
1277                 if (status)
1278                         goto cpy_err;
1279         }
1280         ocrdma_store_gsi_qp_cq(dev, attrs);
1281         qp->ibqp.qp_num = qp->id;
1282         mutex_unlock(&dev->dev_lock);
1283         return &qp->ibqp;
1284
1285 cpy_err:
1286         ocrdma_del_qpn_map(dev, qp);
1287 map_err:
1288         ocrdma_mbx_destroy_qp(dev, qp);
1289 mbx_err:
1290         mutex_unlock(&dev->dev_lock);
1291         kfree(qp->wqe_wr_id_tbl);
1292         kfree(qp->rqe_wr_id_tbl);
1293         kfree(qp);
1294         pr_err("%s(%d) error=%d\n", __func__, dev->id, status);
1295 gen_err:
1296         return ERR_PTR(status);
1297 }
1298
1299
1300 static void ocrdma_flush_rq_db(struct ocrdma_qp *qp)
1301 {
1302         if (qp->db_cache) {
1303                 u32 val = qp->rq.dbid | (qp->db_cache <<
1304                                 OCRDMA_DB_RQ_SHIFT);
1305                 iowrite32(val, qp->rq_db);
1306                 qp->db_cache = 0;
1307         }
1308 }
1309
1310 int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1311                       int attr_mask)
1312 {
1313         int status = 0;
1314         struct ocrdma_qp *qp;
1315         struct ocrdma_dev *dev;
1316         enum ib_qp_state old_qps;
1317
1318         qp = get_ocrdma_qp(ibqp);
1319         dev = qp->dev;
1320         if (attr_mask & IB_QP_STATE)
1321                 status = ocrdma_qp_state_change(qp, attr->qp_state, &old_qps);
1322         /* if new and previous states are same hw doesn't need to
1323          * know about it.
1324          */
1325         if (status < 0)
1326                 return status;
1327         status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask);
1328         if (!status && attr_mask & IB_QP_STATE && attr->qp_state == IB_QPS_RTR)
1329                 ocrdma_flush_rq_db(qp);
1330
1331         return status;
1332 }
1333
1334 int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
1335                      int attr_mask, struct ib_udata *udata)
1336 {
1337         unsigned long flags;
1338         int status = -EINVAL;
1339         struct ocrdma_qp *qp;
1340         struct ocrdma_dev *dev;
1341         enum ib_qp_state old_qps, new_qps;
1342
1343         qp = get_ocrdma_qp(ibqp);
1344         dev = qp->dev;
1345
1346         /* syncronize with multiple context trying to change, retrive qps */
1347         mutex_lock(&dev->dev_lock);
1348         /* syncronize with wqe, rqe posting and cqe processing contexts */
1349         spin_lock_irqsave(&qp->q_lock, flags);
1350         old_qps = get_ibqp_state(qp->state);
1351         if (attr_mask & IB_QP_STATE)
1352                 new_qps = attr->qp_state;
1353         else
1354                 new_qps = old_qps;
1355         spin_unlock_irqrestore(&qp->q_lock, flags);
1356
1357         if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask,
1358                                 IB_LINK_LAYER_ETHERNET)) {
1359                 pr_err("%s(%d) invalid attribute mask=0x%x specified for\n"
1360                        "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n",
1361                        __func__, dev->id, attr_mask, qp->id, ibqp->qp_type,
1362                        old_qps, new_qps);
1363                 goto param_err;
1364         }
1365
1366         status = _ocrdma_modify_qp(ibqp, attr, attr_mask);
1367         if (status > 0)
1368                 status = 0;
1369 param_err:
1370         mutex_unlock(&dev->dev_lock);
1371         return status;
1372 }
1373
1374 static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu)
1375 {
1376         switch (mtu) {
1377         case 256:
1378                 return IB_MTU_256;
1379         case 512:
1380                 return IB_MTU_512;
1381         case 1024:
1382                 return IB_MTU_1024;
1383         case 2048:
1384                 return IB_MTU_2048;
1385         case 4096:
1386                 return IB_MTU_4096;
1387         default:
1388                 return IB_MTU_1024;
1389         }
1390 }
1391
1392 static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags)
1393 {
1394         int ib_qp_acc_flags = 0;
1395
1396         if (qp_cap_flags & OCRDMA_QP_INB_WR)
1397                 ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE;
1398         if (qp_cap_flags & OCRDMA_QP_INB_RD)
1399                 ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE;
1400         return ib_qp_acc_flags;
1401 }
1402
1403 int ocrdma_query_qp(struct ib_qp *ibqp,
1404                     struct ib_qp_attr *qp_attr,
1405                     int attr_mask, struct ib_qp_init_attr *qp_init_attr)
1406 {
1407         int status;
1408         u32 qp_state;
1409         struct ocrdma_qp_params params;
1410         struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
1411         struct ocrdma_dev *dev = qp->dev;
1412
1413         memset(&params, 0, sizeof(params));
1414         mutex_lock(&dev->dev_lock);
1415         status = ocrdma_mbx_query_qp(dev, qp, &params);
1416         mutex_unlock(&dev->dev_lock);
1417         if (status)
1418                 goto mbx_err;
1419         qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT);
1420         qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT);
1421         qp_attr->path_mtu =
1422                 ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx &
1423                                 OCRDMA_QP_PARAMS_PATH_MTU_MASK) >>
1424                                 OCRDMA_QP_PARAMS_PATH_MTU_SHIFT;
1425         qp_attr->path_mig_state = IB_MIG_MIGRATED;
1426         qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK;
1427         qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK;
1428         qp_attr->dest_qp_num =
1429             params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK;
1430
1431         qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags);
1432         qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1;
1433         qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1;
1434         qp_attr->cap.max_send_sge = qp->sq.max_sges;
1435         qp_attr->cap.max_recv_sge = qp->rq.max_sges;
1436         qp_attr->cap.max_inline_data = qp->max_inline_data;
1437         qp_init_attr->cap = qp_attr->cap;
1438         memcpy(&qp_attr->ah_attr.grh.dgid, &params.dgid[0],
1439                sizeof(params.dgid));
1440         qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl &
1441             OCRDMA_QP_PARAMS_FLOW_LABEL_MASK;
1442         qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx;
1443         qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn &
1444                                           OCRDMA_QP_PARAMS_HOP_LMT_MASK) >>
1445                                                 OCRDMA_QP_PARAMS_HOP_LMT_SHIFT;
1446         qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn &
1447                                               OCRDMA_QP_PARAMS_TCLASS_MASK) >>
1448                                                 OCRDMA_QP_PARAMS_TCLASS_SHIFT;
1449
1450         qp_attr->ah_attr.ah_flags = IB_AH_GRH;
1451         qp_attr->ah_attr.port_num = 1;
1452         qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl &
1453                                OCRDMA_QP_PARAMS_SL_MASK) >>
1454                                 OCRDMA_QP_PARAMS_SL_SHIFT;
1455         qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn &
1456                             OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >>
1457                                 OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT;
1458         qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn &
1459                               OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >>
1460                                 OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT;
1461         qp_attr->retry_cnt =
1462             (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >>
1463                 OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT;
1464         qp_attr->min_rnr_timer = 0;
1465         qp_attr->pkey_index = 0;
1466         qp_attr->port_num = 1;
1467         qp_attr->ah_attr.src_path_bits = 0;
1468         qp_attr->ah_attr.static_rate = 0;
1469         qp_attr->alt_pkey_index = 0;
1470         qp_attr->alt_port_num = 0;
1471         qp_attr->alt_timeout = 0;
1472         memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr));
1473         qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >>
1474                     OCRDMA_QP_PARAMS_STATE_SHIFT;
1475         qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0;
1476         qp_attr->max_dest_rd_atomic =
1477             params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT;
1478         qp_attr->max_rd_atomic =
1479             params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK;
1480         qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags &
1481                                 OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0;
1482 mbx_err:
1483         return status;
1484 }
1485
1486 static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx)
1487 {
1488         int i = idx / 32;
1489         unsigned int mask = (1 << (idx % 32));
1490
1491         if (srq->idx_bit_fields[i] & mask)
1492                 srq->idx_bit_fields[i] &= ~mask;
1493         else
1494                 srq->idx_bit_fields[i] |= mask;
1495 }
1496
1497 static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q)
1498 {
1499         return ((q->max_wqe_idx - q->head) + q->tail) % q->max_cnt;
1500 }
1501
1502 static int is_hw_sq_empty(struct ocrdma_qp *qp)
1503 {
1504         return (qp->sq.tail == qp->sq.head);
1505 }
1506
1507 static int is_hw_rq_empty(struct ocrdma_qp *qp)
1508 {
1509         return (qp->rq.tail == qp->rq.head);
1510 }
1511
1512 static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q)
1513 {
1514         return q->va + (q->head * q->entry_size);
1515 }
1516
1517 static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q,
1518                                       u32 idx)
1519 {
1520         return q->va + (idx * q->entry_size);
1521 }
1522
1523 static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q)
1524 {
1525         q->head = (q->head + 1) & q->max_wqe_idx;
1526 }
1527
1528 static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q)
1529 {
1530         q->tail = (q->tail + 1) & q->max_wqe_idx;
1531 }
1532
1533 /* discard the cqe for a given QP */
1534 static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq)
1535 {
1536         unsigned long cq_flags;
1537         unsigned long flags;
1538         int discard_cnt = 0;
1539         u32 cur_getp, stop_getp;
1540         struct ocrdma_cqe *cqe;
1541         u32 qpn = 0, wqe_idx = 0;
1542
1543         spin_lock_irqsave(&cq->cq_lock, cq_flags);
1544
1545         /* traverse through the CQEs in the hw CQ,
1546          * find the matching CQE for a given qp,
1547          * mark the matching one discarded by clearing qpn.
1548          * ring the doorbell in the poll_cq() as
1549          * we don't complete out of order cqe.
1550          */
1551
1552         cur_getp = cq->getp;
1553         /* find upto when do we reap the cq. */
1554         stop_getp = cur_getp;
1555         do {
1556                 if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp)))
1557                         break;
1558
1559                 cqe = cq->va + cur_getp;
1560                 /* if (a) done reaping whole hw cq, or
1561                  *    (b) qp_xq becomes empty.
1562                  * then exit
1563                  */
1564                 qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK;
1565                 /* if previously discarded cqe found, skip that too. */
1566                 /* check for matching qp */
1567                 if (qpn == 0 || qpn != qp->id)
1568                         goto skip_cqe;
1569
1570                 if (is_cqe_for_sq(cqe)) {
1571                         ocrdma_hwq_inc_tail(&qp->sq);
1572                 } else {
1573                         if (qp->srq) {
1574                                 wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
1575                                         OCRDMA_CQE_BUFTAG_SHIFT) &
1576                                         qp->srq->rq.max_wqe_idx;
1577                                 if (wqe_idx < 1)
1578                                         BUG();
1579                                 spin_lock_irqsave(&qp->srq->q_lock, flags);
1580                                 ocrdma_hwq_inc_tail(&qp->srq->rq);
1581                                 ocrdma_srq_toggle_bit(qp->srq, wqe_idx - 1);
1582                                 spin_unlock_irqrestore(&qp->srq->q_lock, flags);
1583
1584                         } else {
1585                                 ocrdma_hwq_inc_tail(&qp->rq);
1586                         }
1587                 }
1588                 /* mark cqe discarded so that it is not picked up later
1589                  * in the poll_cq().
1590                  */
1591                 discard_cnt += 1;
1592                 cqe->cmn.qpn = 0;
1593 skip_cqe:
1594                 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
1595         } while (cur_getp != stop_getp);
1596         spin_unlock_irqrestore(&cq->cq_lock, cq_flags);
1597 }
1598
1599 void ocrdma_del_flush_qp(struct ocrdma_qp *qp)
1600 {
1601         int found = false;
1602         unsigned long flags;
1603         struct ocrdma_dev *dev = qp->dev;
1604         /* sync with any active CQ poll */
1605
1606         spin_lock_irqsave(&dev->flush_q_lock, flags);
1607         found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp);
1608         if (found)
1609                 list_del(&qp->sq_entry);
1610         if (!qp->srq) {
1611                 found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp);
1612                 if (found)
1613                         list_del(&qp->rq_entry);
1614         }
1615         spin_unlock_irqrestore(&dev->flush_q_lock, flags);
1616 }
1617
1618 int ocrdma_destroy_qp(struct ib_qp *ibqp)
1619 {
1620         int status;
1621         struct ocrdma_pd *pd;
1622         struct ocrdma_qp *qp;
1623         struct ocrdma_dev *dev;
1624         struct ib_qp_attr attrs;
1625         int attr_mask = IB_QP_STATE;
1626         unsigned long flags;
1627
1628         qp = get_ocrdma_qp(ibqp);
1629         dev = qp->dev;
1630
1631         attrs.qp_state = IB_QPS_ERR;
1632         pd = qp->pd;
1633
1634         /* change the QP state to ERROR */
1635         _ocrdma_modify_qp(ibqp, &attrs, attr_mask);
1636
1637         /* ensure that CQEs for newly created QP (whose id may be same with
1638          * one which just getting destroyed are same), dont get
1639          * discarded until the old CQEs are discarded.
1640          */
1641         mutex_lock(&dev->dev_lock);
1642         status = ocrdma_mbx_destroy_qp(dev, qp);
1643
1644         /*
1645          * acquire CQ lock while destroy is in progress, in order to
1646          * protect against proessing in-flight CQEs for this QP.
1647          */
1648         spin_lock_irqsave(&qp->sq_cq->cq_lock, flags);
1649         if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
1650                 spin_lock(&qp->rq_cq->cq_lock);
1651
1652         ocrdma_del_qpn_map(dev, qp);
1653
1654         if (qp->rq_cq && (qp->rq_cq != qp->sq_cq))
1655                 spin_unlock(&qp->rq_cq->cq_lock);
1656         spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags);
1657
1658         if (!pd->uctx) {
1659                 ocrdma_discard_cqes(qp, qp->sq_cq);
1660                 ocrdma_discard_cqes(qp, qp->rq_cq);
1661         }
1662         mutex_unlock(&dev->dev_lock);
1663
1664         if (pd->uctx) {
1665                 ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa,
1666                                 PAGE_ALIGN(qp->sq.len));
1667                 if (!qp->srq)
1668                         ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa,
1669                                         PAGE_ALIGN(qp->rq.len));
1670         }
1671
1672         ocrdma_del_flush_qp(qp);
1673
1674         kfree(qp->wqe_wr_id_tbl);
1675         kfree(qp->rqe_wr_id_tbl);
1676         kfree(qp);
1677         return status;
1678 }
1679
1680 static int ocrdma_copy_srq_uresp(struct ocrdma_dev *dev, struct ocrdma_srq *srq,
1681                                 struct ib_udata *udata)
1682 {
1683         int status;
1684         struct ocrdma_create_srq_uresp uresp;
1685
1686         memset(&uresp, 0, sizeof(uresp));
1687         uresp.rq_dbid = srq->rq.dbid;
1688         uresp.num_rq_pages = 1;
1689         uresp.rq_page_addr[0] = srq->rq.pa;
1690         uresp.rq_page_size = srq->rq.len;
1691         uresp.db_page_addr = dev->nic_info.unmapped_db +
1692             (srq->pd->id * dev->nic_info.db_page_size);
1693         uresp.db_page_size = dev->nic_info.db_page_size;
1694         uresp.num_rqe_allocated = srq->rq.max_cnt;
1695         if (ocrdma_get_asic_type(dev) == OCRDMA_ASIC_GEN_SKH_R) {
1696                 uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ_OFFSET;
1697                 uresp.db_shift = 24;
1698         } else {
1699                 uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET;
1700                 uresp.db_shift = 16;
1701         }
1702
1703         status = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
1704         if (status)
1705                 return status;
1706         status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0],
1707                                  uresp.rq_page_size);
1708         if (status)
1709                 return status;
1710         return status;
1711 }
1712
1713 struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd,
1714                                  struct ib_srq_init_attr *init_attr,
1715                                  struct ib_udata *udata)
1716 {
1717         int status = -ENOMEM;
1718         struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
1719         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
1720         struct ocrdma_srq *srq;
1721
1722         if (init_attr->attr.max_sge > dev->attr.max_recv_sge)
1723                 return ERR_PTR(-EINVAL);
1724         if (init_attr->attr.max_wr > dev->attr.max_rqe)
1725                 return ERR_PTR(-EINVAL);
1726
1727         srq = kzalloc(sizeof(*srq), GFP_KERNEL);
1728         if (!srq)
1729                 return ERR_PTR(status);
1730
1731         spin_lock_init(&srq->q_lock);
1732         srq->pd = pd;
1733         srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size);
1734         status = ocrdma_mbx_create_srq(dev, srq, init_attr, pd);
1735         if (status)
1736                 goto err;
1737
1738         if (udata == NULL) {
1739                 srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt,
1740                             GFP_KERNEL);
1741                 if (srq->rqe_wr_id_tbl == NULL)
1742                         goto arm_err;
1743
1744                 srq->bit_fields_len = (srq->rq.max_cnt / 32) +
1745                     (srq->rq.max_cnt % 32 ? 1 : 0);
1746                 srq->idx_bit_fields =
1747                     kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL);
1748                 if (srq->idx_bit_fields == NULL)
1749                         goto arm_err;
1750                 memset(srq->idx_bit_fields, 0xff,
1751                        srq->bit_fields_len * sizeof(u32));
1752         }
1753
1754         if (init_attr->attr.srq_limit) {
1755                 status = ocrdma_mbx_modify_srq(srq, &init_attr->attr);
1756                 if (status)
1757                         goto arm_err;
1758         }
1759
1760         if (udata) {
1761                 status = ocrdma_copy_srq_uresp(dev, srq, udata);
1762                 if (status)
1763                         goto arm_err;
1764         }
1765
1766         return &srq->ibsrq;
1767
1768 arm_err:
1769         ocrdma_mbx_destroy_srq(dev, srq);
1770 err:
1771         kfree(srq->rqe_wr_id_tbl);
1772         kfree(srq->idx_bit_fields);
1773         kfree(srq);
1774         return ERR_PTR(status);
1775 }
1776
1777 int ocrdma_modify_srq(struct ib_srq *ibsrq,
1778                       struct ib_srq_attr *srq_attr,
1779                       enum ib_srq_attr_mask srq_attr_mask,
1780                       struct ib_udata *udata)
1781 {
1782         int status = 0;
1783         struct ocrdma_srq *srq;
1784
1785         srq = get_ocrdma_srq(ibsrq);
1786         if (srq_attr_mask & IB_SRQ_MAX_WR)
1787                 status = -EINVAL;
1788         else
1789                 status = ocrdma_mbx_modify_srq(srq, srq_attr);
1790         return status;
1791 }
1792
1793 int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
1794 {
1795         int status;
1796         struct ocrdma_srq *srq;
1797
1798         srq = get_ocrdma_srq(ibsrq);
1799         status = ocrdma_mbx_query_srq(srq, srq_attr);
1800         return status;
1801 }
1802
1803 int ocrdma_destroy_srq(struct ib_srq *ibsrq)
1804 {
1805         int status;
1806         struct ocrdma_srq *srq;
1807         struct ocrdma_dev *dev = get_ocrdma_dev(ibsrq->device);
1808
1809         srq = get_ocrdma_srq(ibsrq);
1810
1811         status = ocrdma_mbx_destroy_srq(dev, srq);
1812
1813         if (srq->pd->uctx)
1814                 ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa,
1815                                 PAGE_ALIGN(srq->rq.len));
1816
1817         kfree(srq->idx_bit_fields);
1818         kfree(srq->rqe_wr_id_tbl);
1819         kfree(srq);
1820         return status;
1821 }
1822
1823 /* unprivileged verbs and their support functions. */
1824 static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp,
1825                                 struct ocrdma_hdr_wqe *hdr,
1826                                 struct ib_send_wr *wr)
1827 {
1828         struct ocrdma_ewqe_ud_hdr *ud_hdr =
1829                 (struct ocrdma_ewqe_ud_hdr *)(hdr + 1);
1830         struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah);
1831
1832         ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn;
1833         if (qp->qp_type == IB_QPT_GSI)
1834                 ud_hdr->qkey = qp->qkey;
1835         else
1836                 ud_hdr->qkey = wr->wr.ud.remote_qkey;
1837         ud_hdr->rsvd_ahid = ah->id;
1838 }
1839
1840 static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr,
1841                               struct ocrdma_sge *sge, int num_sge,
1842                               struct ib_sge *sg_list)
1843 {
1844         int i;
1845
1846         for (i = 0; i < num_sge; i++) {
1847                 sge[i].lrkey = sg_list[i].lkey;
1848                 sge[i].addr_lo = sg_list[i].addr;
1849                 sge[i].addr_hi = upper_32_bits(sg_list[i].addr);
1850                 sge[i].len = sg_list[i].length;
1851                 hdr->total_len += sg_list[i].length;
1852         }
1853         if (num_sge == 0)
1854                 memset(sge, 0, sizeof(*sge));
1855 }
1856
1857 static inline uint32_t ocrdma_sglist_len(struct ib_sge *sg_list, int num_sge)
1858 {
1859         uint32_t total_len = 0, i;
1860
1861         for (i = 0; i < num_sge; i++)
1862                 total_len += sg_list[i].length;
1863         return total_len;
1864 }
1865
1866
1867 static int ocrdma_build_inline_sges(struct ocrdma_qp *qp,
1868                                     struct ocrdma_hdr_wqe *hdr,
1869                                     struct ocrdma_sge *sge,
1870                                     struct ib_send_wr *wr, u32 wqe_size)
1871 {
1872         int i;
1873         char *dpp_addr;
1874
1875         if (wr->send_flags & IB_SEND_INLINE && qp->qp_type != IB_QPT_UD) {
1876                 hdr->total_len = ocrdma_sglist_len(wr->sg_list, wr->num_sge);
1877                 if (unlikely(hdr->total_len > qp->max_inline_data)) {
1878                         pr_err("%s() supported_len=0x%x,\n"
1879                                " unspported len req=0x%x\n", __func__,
1880                                 qp->max_inline_data, hdr->total_len);
1881                         return -EINVAL;
1882                 }
1883                 dpp_addr = (char *)sge;
1884                 for (i = 0; i < wr->num_sge; i++) {
1885                         memcpy(dpp_addr,
1886                                (void *)(unsigned long)wr->sg_list[i].addr,
1887                                wr->sg_list[i].length);
1888                         dpp_addr += wr->sg_list[i].length;
1889                 }
1890
1891                 wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES);
1892                 if (0 == hdr->total_len)
1893                         wqe_size += sizeof(struct ocrdma_sge);
1894                 hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT);
1895         } else {
1896                 ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1897                 if (wr->num_sge)
1898                         wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge));
1899                 else
1900                         wqe_size += sizeof(struct ocrdma_sge);
1901                 hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1902         }
1903         hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
1904         return 0;
1905 }
1906
1907 static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1908                              struct ib_send_wr *wr)
1909 {
1910         int status;
1911         struct ocrdma_sge *sge;
1912         u32 wqe_size = sizeof(*hdr);
1913
1914         if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
1915                 ocrdma_build_ud_hdr(qp, hdr, wr);
1916                 sge = (struct ocrdma_sge *)(hdr + 2);
1917                 wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr);
1918         } else {
1919                 sge = (struct ocrdma_sge *)(hdr + 1);
1920         }
1921
1922         status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1923         return status;
1924 }
1925
1926 static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1927                               struct ib_send_wr *wr)
1928 {
1929         int status;
1930         struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
1931         struct ocrdma_sge *sge = ext_rw + 1;
1932         u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw);
1933
1934         status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size);
1935         if (status)
1936                 return status;
1937         ext_rw->addr_lo = wr->wr.rdma.remote_addr;
1938         ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
1939         ext_rw->lrkey = wr->wr.rdma.rkey;
1940         ext_rw->len = hdr->total_len;
1941         return 0;
1942 }
1943
1944 static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
1945                               struct ib_send_wr *wr)
1946 {
1947         struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1);
1948         struct ocrdma_sge *sge = ext_rw + 1;
1949         u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) +
1950             sizeof(struct ocrdma_hdr_wqe);
1951
1952         ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list);
1953         hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
1954         hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT);
1955         hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
1956
1957         ext_rw->addr_lo = wr->wr.rdma.remote_addr;
1958         ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr);
1959         ext_rw->lrkey = wr->wr.rdma.rkey;
1960         ext_rw->len = hdr->total_len;
1961 }
1962
1963 static void build_frmr_pbes(struct ib_send_wr *wr, struct ocrdma_pbl *pbl_tbl,
1964                             struct ocrdma_hw_mr *hwmr)
1965 {
1966         int i;
1967         u64 buf_addr = 0;
1968         int num_pbes;
1969         struct ocrdma_pbe *pbe;
1970
1971         pbe = (struct ocrdma_pbe *)pbl_tbl->va;
1972         num_pbes = 0;
1973
1974         /* go through the OS phy regions & fill hw pbe entries into pbls. */
1975         for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
1976                 /* number of pbes can be more for one OS buf, when
1977                  * buffers are of different sizes.
1978                  * split the ib_buf to one or more pbes.
1979                  */
1980                 buf_addr = wr->wr.fast_reg.page_list->page_list[i];
1981                 pbe->pa_lo = cpu_to_le32((u32) (buf_addr & PAGE_MASK));
1982                 pbe->pa_hi = cpu_to_le32((u32) upper_32_bits(buf_addr));
1983                 num_pbes += 1;
1984                 pbe++;
1985
1986                 /* if the pbl is full storing the pbes,
1987                  * move to next pbl.
1988                 */
1989                 if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
1990                         pbl_tbl++;
1991                         pbe = (struct ocrdma_pbe *)pbl_tbl->va;
1992                 }
1993         }
1994         return;
1995 }
1996
1997 static int get_encoded_page_size(int pg_sz)
1998 {
1999         /* Max size is 256M 4096 << 16 */
2000         int i = 0;
2001         for (; i < 17; i++)
2002                 if (pg_sz == (4096 << i))
2003                         break;
2004         return i;
2005 }
2006
2007
2008 static int ocrdma_build_fr(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr,
2009                            struct ib_send_wr *wr)
2010 {
2011         u64 fbo;
2012         struct ocrdma_ewqe_fr *fast_reg = (struct ocrdma_ewqe_fr *)(hdr + 1);
2013         struct ocrdma_mr *mr;
2014         u32 wqe_size = sizeof(*fast_reg) + sizeof(*hdr);
2015
2016         wqe_size = roundup(wqe_size, OCRDMA_WQE_ALIGN_BYTES);
2017
2018         if (wr->wr.fast_reg.page_list_len > qp->dev->attr.max_pages_per_frmr)
2019                 return -EINVAL;
2020
2021         hdr->cw |= (OCRDMA_FR_MR << OCRDMA_WQE_OPCODE_SHIFT);
2022         hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT);
2023
2024         if (wr->wr.fast_reg.page_list_len == 0)
2025                 BUG();
2026         if (wr->wr.fast_reg.access_flags & IB_ACCESS_LOCAL_WRITE)
2027                 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_LOCAL_WR;
2028         if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_WRITE)
2029                 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_WR;
2030         if (wr->wr.fast_reg.access_flags & IB_ACCESS_REMOTE_READ)
2031                 hdr->rsvd_lkey_flags |= OCRDMA_LKEY_FLAG_REMOTE_RD;
2032         hdr->lkey = wr->wr.fast_reg.rkey;
2033         hdr->total_len = wr->wr.fast_reg.length;
2034
2035         fbo = wr->wr.fast_reg.iova_start -
2036             (wr->wr.fast_reg.page_list->page_list[0] & PAGE_MASK);
2037
2038         fast_reg->va_hi = upper_32_bits(wr->wr.fast_reg.iova_start);
2039         fast_reg->va_lo = (u32) (wr->wr.fast_reg.iova_start & 0xffffffff);
2040         fast_reg->fbo_hi = upper_32_bits(fbo);
2041         fast_reg->fbo_lo = (u32) fbo & 0xffffffff;
2042         fast_reg->num_sges = wr->wr.fast_reg.page_list_len;
2043         fast_reg->size_sge =
2044                 get_encoded_page_size(1 << wr->wr.fast_reg.page_shift);
2045         mr = (struct ocrdma_mr *) (unsigned long) qp->dev->stag_arr[(hdr->lkey >> 8) &
2046                 (OCRDMA_MAX_STAG - 1)];
2047         build_frmr_pbes(wr, mr->hwmr.pbl_table, &mr->hwmr);
2048         return 0;
2049 }
2050
2051 static void ocrdma_ring_sq_db(struct ocrdma_qp *qp)
2052 {
2053         u32 val = qp->sq.dbid | (1 << OCRDMA_DB_SQ_SHIFT);
2054
2055         iowrite32(val, qp->sq_db);
2056 }
2057
2058 int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
2059                      struct ib_send_wr **bad_wr)
2060 {
2061         int status = 0;
2062         struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2063         struct ocrdma_hdr_wqe *hdr;
2064         unsigned long flags;
2065
2066         spin_lock_irqsave(&qp->q_lock, flags);
2067         if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) {
2068                 spin_unlock_irqrestore(&qp->q_lock, flags);
2069                 *bad_wr = wr;
2070                 return -EINVAL;
2071         }
2072
2073         while (wr) {
2074                 if (ocrdma_hwq_free_cnt(&qp->sq) == 0 ||
2075                     wr->num_sge > qp->sq.max_sges) {
2076                         *bad_wr = wr;
2077                         status = -ENOMEM;
2078                         break;
2079                 }
2080                 hdr = ocrdma_hwq_head(&qp->sq);
2081                 hdr->cw = 0;
2082                 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
2083                         hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2084                 if (wr->send_flags & IB_SEND_FENCE)
2085                         hdr->cw |=
2086                             (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT);
2087                 if (wr->send_flags & IB_SEND_SOLICITED)
2088                         hdr->cw |=
2089                             (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT);
2090                 hdr->total_len = 0;
2091                 switch (wr->opcode) {
2092                 case IB_WR_SEND_WITH_IMM:
2093                         hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2094                         hdr->immdt = ntohl(wr->ex.imm_data);
2095                 case IB_WR_SEND:
2096                         hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2097                         ocrdma_build_send(qp, hdr, wr);
2098                         break;
2099                 case IB_WR_SEND_WITH_INV:
2100                         hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
2101                         hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT);
2102                         hdr->lkey = wr->ex.invalidate_rkey;
2103                         status = ocrdma_build_send(qp, hdr, wr);
2104                         break;
2105                 case IB_WR_RDMA_WRITE_WITH_IMM:
2106                         hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT);
2107                         hdr->immdt = ntohl(wr->ex.imm_data);
2108                 case IB_WR_RDMA_WRITE:
2109                         hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT);
2110                         status = ocrdma_build_write(qp, hdr, wr);
2111                         break;
2112                 case IB_WR_RDMA_READ_WITH_INV:
2113                         hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT);
2114                 case IB_WR_RDMA_READ:
2115                         ocrdma_build_read(qp, hdr, wr);
2116                         break;
2117                 case IB_WR_LOCAL_INV:
2118                         hdr->cw |=
2119                             (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT);
2120                         hdr->cw |= ((sizeof(struct ocrdma_hdr_wqe) +
2121                                         sizeof(struct ocrdma_sge)) /
2122                                 OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT;
2123                         hdr->lkey = wr->ex.invalidate_rkey;
2124                         break;
2125                 case IB_WR_FAST_REG_MR:
2126                         status = ocrdma_build_fr(qp, hdr, wr);
2127                         break;
2128                 default:
2129                         status = -EINVAL;
2130                         break;
2131                 }
2132                 if (status) {
2133                         *bad_wr = wr;
2134                         break;
2135                 }
2136                 if (wr->send_flags & IB_SEND_SIGNALED || qp->signaled)
2137                         qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1;
2138                 else
2139                         qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0;
2140                 qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id;
2141                 ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) &
2142                                    OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE);
2143                 /* make sure wqe is written before adapter can access it */
2144                 wmb();
2145                 /* inform hw to start processing it */
2146                 ocrdma_ring_sq_db(qp);
2147
2148                 /* update pointer, counter for next wr */
2149                 ocrdma_hwq_inc_head(&qp->sq);
2150                 wr = wr->next;
2151         }
2152         spin_unlock_irqrestore(&qp->q_lock, flags);
2153         return status;
2154 }
2155
2156 static void ocrdma_ring_rq_db(struct ocrdma_qp *qp)
2157 {
2158         u32 val = qp->rq.dbid | (1 << OCRDMA_DB_RQ_SHIFT);
2159
2160         iowrite32(val, qp->rq_db);
2161 }
2162
2163 static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr,
2164                              u16 tag)
2165 {
2166         u32 wqe_size = 0;
2167         struct ocrdma_sge *sge;
2168         if (wr->num_sge)
2169                 wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe);
2170         else
2171                 wqe_size = sizeof(*sge) + sizeof(*rqe);
2172
2173         rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) <<
2174                                 OCRDMA_WQE_SIZE_SHIFT);
2175         rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT);
2176         rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT);
2177         rqe->total_len = 0;
2178         rqe->rsvd_tag = tag;
2179         sge = (struct ocrdma_sge *)(rqe + 1);
2180         ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list);
2181         ocrdma_cpu_to_le32(rqe, wqe_size);
2182 }
2183
2184 int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
2185                      struct ib_recv_wr **bad_wr)
2186 {
2187         int status = 0;
2188         unsigned long flags;
2189         struct ocrdma_qp *qp = get_ocrdma_qp(ibqp);
2190         struct ocrdma_hdr_wqe *rqe;
2191
2192         spin_lock_irqsave(&qp->q_lock, flags);
2193         if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) {
2194                 spin_unlock_irqrestore(&qp->q_lock, flags);
2195                 *bad_wr = wr;
2196                 return -EINVAL;
2197         }
2198         while (wr) {
2199                 if (ocrdma_hwq_free_cnt(&qp->rq) == 0 ||
2200                     wr->num_sge > qp->rq.max_sges) {
2201                         *bad_wr = wr;
2202                         status = -ENOMEM;
2203                         break;
2204                 }
2205                 rqe = ocrdma_hwq_head(&qp->rq);
2206                 ocrdma_build_rqe(rqe, wr, 0);
2207
2208                 qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id;
2209                 /* make sure rqe is written before adapter can access it */
2210                 wmb();
2211
2212                 /* inform hw to start processing it */
2213                 ocrdma_ring_rq_db(qp);
2214
2215                 /* update pointer, counter for next wr */
2216                 ocrdma_hwq_inc_head(&qp->rq);
2217                 wr = wr->next;
2218         }
2219         spin_unlock_irqrestore(&qp->q_lock, flags);
2220         return status;
2221 }
2222
2223 /* cqe for srq's rqe can potentially arrive out of order.
2224  * index gives the entry in the shadow table where to store
2225  * the wr_id. tag/index is returned in cqe to reference back
2226  * for a given rqe.
2227  */
2228 static int ocrdma_srq_get_idx(struct ocrdma_srq *srq)
2229 {
2230         int row = 0;
2231         int indx = 0;
2232
2233         for (row = 0; row < srq->bit_fields_len; row++) {
2234                 if (srq->idx_bit_fields[row]) {
2235                         indx = ffs(srq->idx_bit_fields[row]);
2236                         indx = (row * 32) + (indx - 1);
2237                         if (indx >= srq->rq.max_cnt)
2238                                 BUG();
2239                         ocrdma_srq_toggle_bit(srq, indx);
2240                         break;
2241                 }
2242         }
2243
2244         if (row == srq->bit_fields_len)
2245                 BUG();
2246         return indx + 1; /* Use from index 1 */
2247 }
2248
2249 static void ocrdma_ring_srq_db(struct ocrdma_srq *srq)
2250 {
2251         u32 val = srq->rq.dbid | (1 << 16);
2252
2253         iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET);
2254 }
2255
2256 int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
2257                          struct ib_recv_wr **bad_wr)
2258 {
2259         int status = 0;
2260         unsigned long flags;
2261         struct ocrdma_srq *srq;
2262         struct ocrdma_hdr_wqe *rqe;
2263         u16 tag;
2264
2265         srq = get_ocrdma_srq(ibsrq);
2266
2267         spin_lock_irqsave(&srq->q_lock, flags);
2268         while (wr) {
2269                 if (ocrdma_hwq_free_cnt(&srq->rq) == 0 ||
2270                     wr->num_sge > srq->rq.max_sges) {
2271                         status = -ENOMEM;
2272                         *bad_wr = wr;
2273                         break;
2274                 }
2275                 tag = ocrdma_srq_get_idx(srq);
2276                 rqe = ocrdma_hwq_head(&srq->rq);
2277                 ocrdma_build_rqe(rqe, wr, tag);
2278
2279                 srq->rqe_wr_id_tbl[tag] = wr->wr_id;
2280                 /* make sure rqe is written before adapter can perform DMA */
2281                 wmb();
2282                 /* inform hw to start processing it */
2283                 ocrdma_ring_srq_db(srq);
2284                 /* update pointer, counter for next wr */
2285                 ocrdma_hwq_inc_head(&srq->rq);
2286                 wr = wr->next;
2287         }
2288         spin_unlock_irqrestore(&srq->q_lock, flags);
2289         return status;
2290 }
2291
2292 static enum ib_wc_status ocrdma_to_ibwc_err(u16 status)
2293 {
2294         enum ib_wc_status ibwc_status;
2295
2296         switch (status) {
2297         case OCRDMA_CQE_GENERAL_ERR:
2298                 ibwc_status = IB_WC_GENERAL_ERR;
2299                 break;
2300         case OCRDMA_CQE_LOC_LEN_ERR:
2301                 ibwc_status = IB_WC_LOC_LEN_ERR;
2302                 break;
2303         case OCRDMA_CQE_LOC_QP_OP_ERR:
2304                 ibwc_status = IB_WC_LOC_QP_OP_ERR;
2305                 break;
2306         case OCRDMA_CQE_LOC_EEC_OP_ERR:
2307                 ibwc_status = IB_WC_LOC_EEC_OP_ERR;
2308                 break;
2309         case OCRDMA_CQE_LOC_PROT_ERR:
2310                 ibwc_status = IB_WC_LOC_PROT_ERR;
2311                 break;
2312         case OCRDMA_CQE_WR_FLUSH_ERR:
2313                 ibwc_status = IB_WC_WR_FLUSH_ERR;
2314                 break;
2315         case OCRDMA_CQE_MW_BIND_ERR:
2316                 ibwc_status = IB_WC_MW_BIND_ERR;
2317                 break;
2318         case OCRDMA_CQE_BAD_RESP_ERR:
2319                 ibwc_status = IB_WC_BAD_RESP_ERR;
2320                 break;
2321         case OCRDMA_CQE_LOC_ACCESS_ERR:
2322                 ibwc_status = IB_WC_LOC_ACCESS_ERR;
2323                 break;
2324         case OCRDMA_CQE_REM_INV_REQ_ERR:
2325                 ibwc_status = IB_WC_REM_INV_REQ_ERR;
2326                 break;
2327         case OCRDMA_CQE_REM_ACCESS_ERR:
2328                 ibwc_status = IB_WC_REM_ACCESS_ERR;
2329                 break;
2330         case OCRDMA_CQE_REM_OP_ERR:
2331                 ibwc_status = IB_WC_REM_OP_ERR;
2332                 break;
2333         case OCRDMA_CQE_RETRY_EXC_ERR:
2334                 ibwc_status = IB_WC_RETRY_EXC_ERR;
2335                 break;
2336         case OCRDMA_CQE_RNR_RETRY_EXC_ERR:
2337                 ibwc_status = IB_WC_RNR_RETRY_EXC_ERR;
2338                 break;
2339         case OCRDMA_CQE_LOC_RDD_VIOL_ERR:
2340                 ibwc_status = IB_WC_LOC_RDD_VIOL_ERR;
2341                 break;
2342         case OCRDMA_CQE_REM_INV_RD_REQ_ERR:
2343                 ibwc_status = IB_WC_REM_INV_RD_REQ_ERR;
2344                 break;
2345         case OCRDMA_CQE_REM_ABORT_ERR:
2346                 ibwc_status = IB_WC_REM_ABORT_ERR;
2347                 break;
2348         case OCRDMA_CQE_INV_EECN_ERR:
2349                 ibwc_status = IB_WC_INV_EECN_ERR;
2350                 break;
2351         case OCRDMA_CQE_INV_EEC_STATE_ERR:
2352                 ibwc_status = IB_WC_INV_EEC_STATE_ERR;
2353                 break;
2354         case OCRDMA_CQE_FATAL_ERR:
2355                 ibwc_status = IB_WC_FATAL_ERR;
2356                 break;
2357         case OCRDMA_CQE_RESP_TIMEOUT_ERR:
2358                 ibwc_status = IB_WC_RESP_TIMEOUT_ERR;
2359                 break;
2360         default:
2361                 ibwc_status = IB_WC_GENERAL_ERR;
2362                 break;
2363         }
2364         return ibwc_status;
2365 }
2366
2367 static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc,
2368                       u32 wqe_idx)
2369 {
2370         struct ocrdma_hdr_wqe *hdr;
2371         struct ocrdma_sge *rw;
2372         int opcode;
2373
2374         hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx);
2375
2376         ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid;
2377         /* Undo the hdr->cw swap */
2378         opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK;
2379         switch (opcode) {
2380         case OCRDMA_WRITE:
2381                 ibwc->opcode = IB_WC_RDMA_WRITE;
2382                 break;
2383         case OCRDMA_READ:
2384                 rw = (struct ocrdma_sge *)(hdr + 1);
2385                 ibwc->opcode = IB_WC_RDMA_READ;
2386                 ibwc->byte_len = rw->len;
2387                 break;
2388         case OCRDMA_SEND:
2389                 ibwc->opcode = IB_WC_SEND;
2390                 break;
2391         case OCRDMA_FR_MR:
2392                 ibwc->opcode = IB_WC_FAST_REG_MR;
2393                 break;
2394         case OCRDMA_LKEY_INV:
2395                 ibwc->opcode = IB_WC_LOCAL_INV;
2396                 break;
2397         default:
2398                 ibwc->status = IB_WC_GENERAL_ERR;
2399                 pr_err("%s() invalid opcode received = 0x%x\n",
2400                        __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK);
2401                 break;
2402         }
2403 }
2404
2405 static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp,
2406                                                 struct ocrdma_cqe *cqe)
2407 {
2408         if (is_cqe_for_sq(cqe)) {
2409                 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2410                                 cqe->flags_status_srcqpn) &
2411                                         ~OCRDMA_CQE_STATUS_MASK);
2412                 cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2413                                 cqe->flags_status_srcqpn) |
2414                                 (OCRDMA_CQE_WR_FLUSH_ERR <<
2415                                         OCRDMA_CQE_STATUS_SHIFT));
2416         } else {
2417                 if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2418                         cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2419                                         cqe->flags_status_srcqpn) &
2420                                                 ~OCRDMA_CQE_UD_STATUS_MASK);
2421                         cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2422                                         cqe->flags_status_srcqpn) |
2423                                         (OCRDMA_CQE_WR_FLUSH_ERR <<
2424                                                 OCRDMA_CQE_UD_STATUS_SHIFT));
2425                 } else {
2426                         cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2427                                         cqe->flags_status_srcqpn) &
2428                                                 ~OCRDMA_CQE_STATUS_MASK);
2429                         cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu(
2430                                         cqe->flags_status_srcqpn) |
2431                                         (OCRDMA_CQE_WR_FLUSH_ERR <<
2432                                                 OCRDMA_CQE_STATUS_SHIFT));
2433                 }
2434         }
2435 }
2436
2437 static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2438                                   struct ocrdma_qp *qp, int status)
2439 {
2440         bool expand = false;
2441
2442         ibwc->byte_len = 0;
2443         ibwc->qp = &qp->ibqp;
2444         ibwc->status = ocrdma_to_ibwc_err(status);
2445
2446         ocrdma_flush_qp(qp);
2447         ocrdma_qp_state_change(qp, IB_QPS_ERR, NULL);
2448
2449         /* if wqe/rqe pending for which cqe needs to be returned,
2450          * trigger inflating it.
2451          */
2452         if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) {
2453                 expand = true;
2454                 ocrdma_set_cqe_status_flushed(qp, cqe);
2455         }
2456         return expand;
2457 }
2458
2459 static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2460                                   struct ocrdma_qp *qp, int status)
2461 {
2462         ibwc->opcode = IB_WC_RECV;
2463         ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2464         ocrdma_hwq_inc_tail(&qp->rq);
2465
2466         return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2467 }
2468
2469 static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe,
2470                                   struct ocrdma_qp *qp, int status)
2471 {
2472         ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2473         ocrdma_hwq_inc_tail(&qp->sq);
2474
2475         return ocrdma_update_err_cqe(ibwc, cqe, qp, status);
2476 }
2477
2478
2479 static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp,
2480                                  struct ocrdma_cqe *cqe, struct ib_wc *ibwc,
2481                                  bool *polled, bool *stop)
2482 {
2483         bool expand;
2484         int status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2485                 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2486
2487         /* when hw sq is empty, but rq is not empty, so we continue
2488          * to keep the cqe in order to get the cq event again.
2489          */
2490         if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) {
2491                 /* when cq for rq and sq is same, it is safe to return
2492                  * flush cqe for RQEs.
2493                  */
2494                 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2495                         *polled = true;
2496                         status = OCRDMA_CQE_WR_FLUSH_ERR;
2497                         expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2498                 } else {
2499                         /* stop processing further cqe as this cqe is used for
2500                          * triggering cq event on buddy cq of RQ.
2501                          * When QP is destroyed, this cqe will be removed
2502                          * from the cq's hardware q.
2503                          */
2504                         *polled = false;
2505                         *stop = true;
2506                         expand = false;
2507                 }
2508         } else {
2509                 *polled = true;
2510                 expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2511         }
2512         return expand;
2513 }
2514
2515 static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp,
2516                                      struct ocrdma_cqe *cqe,
2517                                      struct ib_wc *ibwc, bool *polled)
2518 {
2519         bool expand = false;
2520         int tail = qp->sq.tail;
2521         u32 wqe_idx;
2522
2523         if (!qp->wqe_wr_id_tbl[tail].signaled) {
2524                 *polled = false;    /* WC cannot be consumed yet */
2525         } else {
2526                 ibwc->status = IB_WC_SUCCESS;
2527                 ibwc->wc_flags = 0;
2528                 ibwc->qp = &qp->ibqp;
2529                 ocrdma_update_wc(qp, ibwc, tail);
2530                 *polled = true;
2531         }
2532         wqe_idx = (le32_to_cpu(cqe->wq.wqeidx) &
2533                         OCRDMA_CQE_WQEIDX_MASK) & qp->sq.max_wqe_idx;
2534         if (tail != wqe_idx)
2535                 expand = true; /* Coalesced CQE can't be consumed yet */
2536
2537         ocrdma_hwq_inc_tail(&qp->sq);
2538         return expand;
2539 }
2540
2541 static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2542                              struct ib_wc *ibwc, bool *polled, bool *stop)
2543 {
2544         int status;
2545         bool expand;
2546
2547         status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2548                 OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2549
2550         if (status == OCRDMA_CQE_SUCCESS)
2551                 expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled);
2552         else
2553                 expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop);
2554         return expand;
2555 }
2556
2557 static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe)
2558 {
2559         int status;
2560
2561         status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2562                 OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT;
2563         ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) &
2564                                                 OCRDMA_CQE_SRCQP_MASK;
2565         ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) &
2566                                                 OCRDMA_CQE_PKEY_MASK;
2567         ibwc->wc_flags = IB_WC_GRH;
2568         ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >>
2569                                         OCRDMA_CQE_UD_XFER_LEN_SHIFT);
2570         return status;
2571 }
2572
2573 static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc,
2574                                        struct ocrdma_cqe *cqe,
2575                                        struct ocrdma_qp *qp)
2576 {
2577         unsigned long flags;
2578         struct ocrdma_srq *srq;
2579         u32 wqe_idx;
2580
2581         srq = get_ocrdma_srq(qp->ibqp.srq);
2582         wqe_idx = (le32_to_cpu(cqe->rq.buftag_qpn) >>
2583                 OCRDMA_CQE_BUFTAG_SHIFT) & srq->rq.max_wqe_idx;
2584         if (wqe_idx < 1)
2585                 BUG();
2586
2587         ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx];
2588         spin_lock_irqsave(&srq->q_lock, flags);
2589         ocrdma_srq_toggle_bit(srq, wqe_idx - 1);
2590         spin_unlock_irqrestore(&srq->q_lock, flags);
2591         ocrdma_hwq_inc_tail(&srq->rq);
2592 }
2593
2594 static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2595                                 struct ib_wc *ibwc, bool *polled, bool *stop,
2596                                 int status)
2597 {
2598         bool expand;
2599
2600         /* when hw_rq is empty, but wq is not empty, so continue
2601          * to keep the cqe to get the cq event again.
2602          */
2603         if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) {
2604                 if (!qp->srq && (qp->sq_cq == qp->rq_cq)) {
2605                         *polled = true;
2606                         status = OCRDMA_CQE_WR_FLUSH_ERR;
2607                         expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status);
2608                 } else {
2609                         *polled = false;
2610                         *stop = true;
2611                         expand = false;
2612                 }
2613         } else {
2614                 *polled = true;
2615                 expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status);
2616         }
2617         return expand;
2618 }
2619
2620 static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp,
2621                                      struct ocrdma_cqe *cqe, struct ib_wc *ibwc)
2622 {
2623         ibwc->opcode = IB_WC_RECV;
2624         ibwc->qp = &qp->ibqp;
2625         ibwc->status = IB_WC_SUCCESS;
2626
2627         if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI)
2628                 ocrdma_update_ud_rcqe(ibwc, cqe);
2629         else
2630                 ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen);
2631
2632         if (is_cqe_imm(cqe)) {
2633                 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2634                 ibwc->wc_flags |= IB_WC_WITH_IMM;
2635         } else if (is_cqe_wr_imm(cqe)) {
2636                 ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM;
2637                 ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt));
2638                 ibwc->wc_flags |= IB_WC_WITH_IMM;
2639         } else if (is_cqe_invalidated(cqe)) {
2640                 ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt);
2641                 ibwc->wc_flags |= IB_WC_WITH_INVALIDATE;
2642         }
2643         if (qp->ibqp.srq) {
2644                 ocrdma_update_free_srq_cqe(ibwc, cqe, qp);
2645         } else {
2646                 ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2647                 ocrdma_hwq_inc_tail(&qp->rq);
2648         }
2649 }
2650
2651 static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe,
2652                              struct ib_wc *ibwc, bool *polled, bool *stop)
2653 {
2654         int status;
2655         bool expand = false;
2656
2657         ibwc->wc_flags = 0;
2658         if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) {
2659                 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2660                                         OCRDMA_CQE_UD_STATUS_MASK) >>
2661                                         OCRDMA_CQE_UD_STATUS_SHIFT;
2662         } else {
2663                 status = (le32_to_cpu(cqe->flags_status_srcqpn) &
2664                              OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT;
2665         }
2666
2667         if (status == OCRDMA_CQE_SUCCESS) {
2668                 *polled = true;
2669                 ocrdma_poll_success_rcqe(qp, cqe, ibwc);
2670         } else {
2671                 expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop,
2672                                               status);
2673         }
2674         return expand;
2675 }
2676
2677 static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe,
2678                                    u16 cur_getp)
2679 {
2680         if (cq->phase_change) {
2681                 if (cur_getp == 0)
2682                         cq->phase = (~cq->phase & OCRDMA_CQE_VALID);
2683         } else {
2684                 /* clear valid bit */
2685                 cqe->flags_status_srcqpn = 0;
2686         }
2687 }
2688
2689 static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries,
2690                             struct ib_wc *ibwc)
2691 {
2692         u16 qpn = 0;
2693         int i = 0;
2694         bool expand = false;
2695         int polled_hw_cqes = 0;
2696         struct ocrdma_qp *qp = NULL;
2697         struct ocrdma_dev *dev = get_ocrdma_dev(cq->ibcq.device);
2698         struct ocrdma_cqe *cqe;
2699         u16 cur_getp; bool polled = false; bool stop = false;
2700
2701         cur_getp = cq->getp;
2702         while (num_entries) {
2703                 cqe = cq->va + cur_getp;
2704                 /* check whether valid cqe or not */
2705                 if (!is_cqe_valid(cq, cqe))
2706                         break;
2707                 qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK);
2708                 /* ignore discarded cqe */
2709                 if (qpn == 0)
2710                         goto skip_cqe;
2711                 qp = dev->qp_tbl[qpn];
2712                 BUG_ON(qp == NULL);
2713
2714                 if (is_cqe_for_sq(cqe)) {
2715                         expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled,
2716                                                   &stop);
2717                 } else {
2718                         expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled,
2719                                                   &stop);
2720                 }
2721                 if (expand)
2722                         goto expand_cqe;
2723                 if (stop)
2724                         goto stop_cqe;
2725                 /* clear qpn to avoid duplicate processing by discard_cqe() */
2726                 cqe->cmn.qpn = 0;
2727 skip_cqe:
2728                 polled_hw_cqes += 1;
2729                 cur_getp = (cur_getp + 1) % cq->max_hw_cqe;
2730                 ocrdma_change_cq_phase(cq, cqe, cur_getp);
2731 expand_cqe:
2732                 if (polled) {
2733                         num_entries -= 1;
2734                         i += 1;
2735                         ibwc = ibwc + 1;
2736                         polled = false;
2737                 }
2738         }
2739 stop_cqe:
2740         cq->getp = cur_getp;
2741         if (cq->deferred_arm) {
2742                 ocrdma_ring_cq_db(dev, cq->id, true, cq->deferred_sol,
2743                                   polled_hw_cqes);
2744                 cq->deferred_arm = false;
2745                 cq->deferred_sol = false;
2746         } else {
2747                 /* We need to pop the CQE. No need to arm */
2748                 ocrdma_ring_cq_db(dev, cq->id, false, cq->deferred_sol,
2749                                   polled_hw_cqes);
2750                 cq->deferred_sol = false;
2751         }
2752
2753         return i;
2754 }
2755
2756 /* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */
2757 static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries,
2758                               struct ocrdma_qp *qp, struct ib_wc *ibwc)
2759 {
2760         int err_cqes = 0;
2761
2762         while (num_entries) {
2763                 if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp))
2764                         break;
2765                 if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) {
2766                         ocrdma_update_wc(qp, ibwc, qp->sq.tail);
2767                         ocrdma_hwq_inc_tail(&qp->sq);
2768                 } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) {
2769                         ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail];
2770                         ocrdma_hwq_inc_tail(&qp->rq);
2771                 } else {
2772                         return err_cqes;
2773                 }
2774                 ibwc->byte_len = 0;
2775                 ibwc->status = IB_WC_WR_FLUSH_ERR;
2776                 ibwc = ibwc + 1;
2777                 err_cqes += 1;
2778                 num_entries -= 1;
2779         }
2780         return err_cqes;
2781 }
2782
2783 int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
2784 {
2785         int cqes_to_poll = num_entries;
2786         struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2787         struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
2788         int num_os_cqe = 0, err_cqes = 0;
2789         struct ocrdma_qp *qp;
2790         unsigned long flags;
2791
2792         /* poll cqes from adapter CQ */
2793         spin_lock_irqsave(&cq->cq_lock, flags);
2794         num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc);
2795         spin_unlock_irqrestore(&cq->cq_lock, flags);
2796         cqes_to_poll -= num_os_cqe;
2797
2798         if (cqes_to_poll) {
2799                 wc = wc + num_os_cqe;
2800                 /* adapter returns single error cqe when qp moves to
2801                  * error state. So insert error cqes with wc_status as
2802                  * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ
2803                  * respectively which uses this CQ.
2804                  */
2805                 spin_lock_irqsave(&dev->flush_q_lock, flags);
2806                 list_for_each_entry(qp, &cq->sq_head, sq_entry) {
2807                         if (cqes_to_poll == 0)
2808                                 break;
2809                         err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc);
2810                         cqes_to_poll -= err_cqes;
2811                         num_os_cqe += err_cqes;
2812                         wc = wc + err_cqes;
2813                 }
2814                 spin_unlock_irqrestore(&dev->flush_q_lock, flags);
2815         }
2816         return num_os_cqe;
2817 }
2818
2819 int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags)
2820 {
2821         struct ocrdma_cq *cq = get_ocrdma_cq(ibcq);
2822         struct ocrdma_dev *dev = get_ocrdma_dev(ibcq->device);
2823         u16 cq_id;
2824         unsigned long flags;
2825         bool arm_needed = false, sol_needed = false;
2826
2827         cq_id = cq->id;
2828
2829         spin_lock_irqsave(&cq->cq_lock, flags);
2830         if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED)
2831                 arm_needed = true;
2832         if (cq_flags & IB_CQ_SOLICITED)
2833                 sol_needed = true;
2834
2835         if (cq->first_arm) {
2836                 ocrdma_ring_cq_db(dev, cq_id, arm_needed, sol_needed, 0);
2837                 cq->first_arm = false;
2838                 goto skip_defer;
2839         }
2840         cq->deferred_arm = true;
2841
2842 skip_defer:
2843         cq->deferred_sol = sol_needed;
2844         spin_unlock_irqrestore(&cq->cq_lock, flags);
2845
2846         return 0;
2847 }
2848
2849 struct ib_mr *ocrdma_alloc_frmr(struct ib_pd *ibpd, int max_page_list_len)
2850 {
2851         int status;
2852         struct ocrdma_mr *mr;
2853         struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
2854         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
2855
2856         if (max_page_list_len > dev->attr.max_pages_per_frmr)
2857                 return ERR_PTR(-EINVAL);
2858
2859         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
2860         if (!mr)
2861                 return ERR_PTR(-ENOMEM);
2862
2863         status = ocrdma_get_pbl_info(dev, mr, max_page_list_len);
2864         if (status)
2865                 goto pbl_err;
2866         mr->hwmr.fr_mr = 1;
2867         mr->hwmr.remote_rd = 0;
2868         mr->hwmr.remote_wr = 0;
2869         mr->hwmr.local_rd = 0;
2870         mr->hwmr.local_wr = 0;
2871         mr->hwmr.mw_bind = 0;
2872         status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
2873         if (status)
2874                 goto pbl_err;
2875         status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, 0);
2876         if (status)
2877                 goto mbx_err;
2878         mr->ibmr.rkey = mr->hwmr.lkey;
2879         mr->ibmr.lkey = mr->hwmr.lkey;
2880         dev->stag_arr[(mr->hwmr.lkey >> 8) & (OCRDMA_MAX_STAG - 1)] = mr;
2881         return &mr->ibmr;
2882 mbx_err:
2883         ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
2884 pbl_err:
2885         kfree(mr);
2886         return ERR_PTR(-ENOMEM);
2887 }
2888
2889 struct ib_fast_reg_page_list *ocrdma_alloc_frmr_page_list(struct ib_device
2890                                                           *ibdev,
2891                                                           int page_list_len)
2892 {
2893         struct ib_fast_reg_page_list *frmr_list;
2894         int size;
2895
2896         size = sizeof(*frmr_list) + (page_list_len * sizeof(u64));
2897         frmr_list = kzalloc(size, GFP_KERNEL);
2898         if (!frmr_list)
2899                 return ERR_PTR(-ENOMEM);
2900         frmr_list->page_list = (u64 *)(frmr_list + 1);
2901         return frmr_list;
2902 }
2903
2904 void ocrdma_free_frmr_page_list(struct ib_fast_reg_page_list *page_list)
2905 {
2906         kfree(page_list);
2907 }
2908
2909 #define MAX_KERNEL_PBE_SIZE 65536
2910 static inline int count_kernel_pbes(struct ib_phys_buf *buf_list,
2911                                     int buf_cnt, u32 *pbe_size)
2912 {
2913         u64 total_size = 0;
2914         u64 buf_size = 0;
2915         int i;
2916         *pbe_size = roundup(buf_list[0].size, PAGE_SIZE);
2917         *pbe_size = roundup_pow_of_two(*pbe_size);
2918
2919         /* find the smallest PBE size that we can have */
2920         for (i = 0; i < buf_cnt; i++) {
2921                 /* first addr may not be page aligned, so ignore checking */
2922                 if ((i != 0) && ((buf_list[i].addr & ~PAGE_MASK) ||
2923                                  (buf_list[i].size & ~PAGE_MASK))) {
2924                         return 0;
2925                 }
2926
2927                 /* if configured PBE size is greater then the chosen one,
2928                  * reduce the PBE size.
2929                  */
2930                 buf_size = roundup(buf_list[i].size, PAGE_SIZE);
2931                 /* pbe_size has to be even multiple of 4K 1,2,4,8...*/
2932                 buf_size = roundup_pow_of_two(buf_size);
2933                 if (*pbe_size > buf_size)
2934                         *pbe_size = buf_size;
2935
2936                 total_size += buf_size;
2937         }
2938         *pbe_size = *pbe_size > MAX_KERNEL_PBE_SIZE ?
2939             (MAX_KERNEL_PBE_SIZE) : (*pbe_size);
2940
2941         /* num_pbes = total_size / (*pbe_size);  this is implemented below. */
2942
2943         return total_size >> ilog2(*pbe_size);
2944 }
2945
2946 static void build_kernel_pbes(struct ib_phys_buf *buf_list, int ib_buf_cnt,
2947                               u32 pbe_size, struct ocrdma_pbl *pbl_tbl,
2948                               struct ocrdma_hw_mr *hwmr)
2949 {
2950         int i;
2951         int idx;
2952         int pbes_per_buf = 0;
2953         u64 buf_addr = 0;
2954         int num_pbes;
2955         struct ocrdma_pbe *pbe;
2956         int total_num_pbes = 0;
2957
2958         if (!hwmr->num_pbes)
2959                 return;
2960
2961         pbe = (struct ocrdma_pbe *)pbl_tbl->va;
2962         num_pbes = 0;
2963
2964         /* go through the OS phy regions & fill hw pbe entries into pbls. */
2965         for (i = 0; i < ib_buf_cnt; i++) {
2966                 buf_addr = buf_list[i].addr;
2967                 pbes_per_buf =
2968                     roundup_pow_of_two(roundup(buf_list[i].size, PAGE_SIZE)) /
2969                     pbe_size;
2970                 hwmr->len += buf_list[i].size;
2971                 /* number of pbes can be more for one OS buf, when
2972                  * buffers are of different sizes.
2973                  * split the ib_buf to one or more pbes.
2974                  */
2975                 for (idx = 0; idx < pbes_per_buf; idx++) {
2976                         /* we program always page aligned addresses,
2977                          * first unaligned address is taken care by fbo.
2978                          */
2979                         if (i == 0) {
2980                                 /* for non zero fbo, assign the
2981                                  * start of the page.
2982                                  */
2983                                 pbe->pa_lo =
2984                                     cpu_to_le32((u32) (buf_addr & PAGE_MASK));
2985                                 pbe->pa_hi =
2986                                     cpu_to_le32((u32) upper_32_bits(buf_addr));
2987                         } else {
2988                                 pbe->pa_lo =
2989                                     cpu_to_le32((u32) (buf_addr & 0xffffffff));
2990                                 pbe->pa_hi =
2991                                     cpu_to_le32((u32) upper_32_bits(buf_addr));
2992                         }
2993                         buf_addr += pbe_size;
2994                         num_pbes += 1;
2995                         total_num_pbes += 1;
2996                         pbe++;
2997
2998                         if (total_num_pbes == hwmr->num_pbes)
2999                                 goto mr_tbl_done;
3000                         /* if the pbl is full storing the pbes,
3001                          * move to next pbl.
3002                          */
3003                         if (num_pbes == (hwmr->pbl_size/sizeof(u64))) {
3004                                 pbl_tbl++;
3005                                 pbe = (struct ocrdma_pbe *)pbl_tbl->va;
3006                                 num_pbes = 0;
3007                         }
3008                 }
3009         }
3010 mr_tbl_done:
3011         return;
3012 }
3013
3014 struct ib_mr *ocrdma_reg_kernel_mr(struct ib_pd *ibpd,
3015                                    struct ib_phys_buf *buf_list,
3016                                    int buf_cnt, int acc, u64 *iova_start)
3017 {
3018         int status = -ENOMEM;
3019         struct ocrdma_mr *mr;
3020         struct ocrdma_pd *pd = get_ocrdma_pd(ibpd);
3021         struct ocrdma_dev *dev = get_ocrdma_dev(ibpd->device);
3022         u32 num_pbes;
3023         u32 pbe_size = 0;
3024
3025         if ((acc & IB_ACCESS_REMOTE_WRITE) && !(acc & IB_ACCESS_LOCAL_WRITE))
3026                 return ERR_PTR(-EINVAL);
3027
3028         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
3029         if (!mr)
3030                 return ERR_PTR(status);
3031
3032         num_pbes = count_kernel_pbes(buf_list, buf_cnt, &pbe_size);
3033         if (num_pbes == 0) {
3034                 status = -EINVAL;
3035                 goto pbl_err;
3036         }
3037         status = ocrdma_get_pbl_info(dev, mr, num_pbes);
3038         if (status)
3039                 goto pbl_err;
3040
3041         mr->hwmr.pbe_size = pbe_size;
3042         mr->hwmr.fbo = *iova_start - (buf_list[0].addr & PAGE_MASK);
3043         mr->hwmr.va = *iova_start;
3044         mr->hwmr.local_rd = 1;
3045         mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0;
3046         mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0;
3047         mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0;
3048         mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0;
3049         mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0;
3050
3051         status = ocrdma_build_pbl_tbl(dev, &mr->hwmr);
3052         if (status)
3053                 goto pbl_err;
3054         build_kernel_pbes(buf_list, buf_cnt, pbe_size, mr->hwmr.pbl_table,
3055                           &mr->hwmr);
3056         status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc);
3057         if (status)
3058                 goto mbx_err;
3059
3060         mr->ibmr.lkey = mr->hwmr.lkey;
3061         if (mr->hwmr.remote_wr || mr->hwmr.remote_rd)
3062                 mr->ibmr.rkey = mr->hwmr.lkey;
3063         return &mr->ibmr;
3064
3065 mbx_err:
3066         ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr);
3067 pbl_err:
3068         kfree(mr);
3069         return ERR_PTR(status);
3070 }