GNU Linux-libre 4.9.282-gnu1
[releases.git] / drivers / infiniband / hw / i40iw / i40iw_verbs.c
1 /*******************************************************************************
2 *
3 * Copyright (c) 2015-2016 Intel Corporation.  All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenFabrics.org BSD license below:
10 *
11 *   Redistribution and use in source and binary forms, with or
12 *   without modification, are permitted provided that the following
13 *   conditions are met:
14 *
15 *    - Redistributions of source code must retain the above
16 *       copyright notice, this list of conditions and the following
17 *       disclaimer.
18 *
19 *    - Redistributions in binary form must reproduce the above
20 *       copyright notice, this list of conditions and the following
21 *       disclaimer in the documentation and/or other materials
22 *       provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 *******************************************************************************/
34
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/random.h>
38 #include <linux/highmem.h>
39 #include <linux/time.h>
40 #include <asm/byteorder.h>
41 #include <net/ip.h>
42 #include <rdma/ib_verbs.h>
43 #include <rdma/iw_cm.h>
44 #include <rdma/ib_user_verbs.h>
45 #include <rdma/ib_umem.h>
46 #include "i40iw.h"
47
48 /**
49  * i40iw_query_device - get device attributes
50  * @ibdev: device pointer from stack
51  * @props: returning device attributes
52  * @udata: user data
53  */
54 static int i40iw_query_device(struct ib_device *ibdev,
55                               struct ib_device_attr *props,
56                               struct ib_udata *udata)
57 {
58         struct i40iw_device *iwdev = to_iwdev(ibdev);
59
60         if (udata->inlen || udata->outlen)
61                 return -EINVAL;
62         memset(props, 0, sizeof(*props));
63         ether_addr_copy((u8 *)&props->sys_image_guid, iwdev->netdev->dev_addr);
64         props->fw_ver = I40IW_FW_VERSION;
65         props->device_cap_flags = iwdev->device_cap_flags;
66         props->vendor_id = iwdev->ldev->pcidev->vendor;
67         props->vendor_part_id = iwdev->ldev->pcidev->device;
68         props->hw_ver = (u32)iwdev->sc_dev.hw_rev;
69         props->max_mr_size = I40IW_MAX_OUTBOUND_MESSAGE_SIZE;
70         props->max_qp = iwdev->max_qp;
71         props->max_qp_wr = (I40IW_MAX_WQ_ENTRIES >> 2) - 1;
72         props->max_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
73         props->max_cq = iwdev->max_cq;
74         props->max_cqe = iwdev->max_cqe;
75         props->max_mr = iwdev->max_mr;
76         props->max_pd = iwdev->max_pd;
77         props->max_sge_rd = I40IW_MAX_SGE_RD;
78         props->max_qp_rd_atom = I40IW_MAX_IRD_SIZE;
79         props->max_qp_init_rd_atom = props->max_qp_rd_atom;
80         props->atomic_cap = IB_ATOMIC_NONE;
81         props->max_map_per_fmr = 1;
82         props->max_fast_reg_page_list_len = I40IW_MAX_PAGES_PER_FMR;
83         return 0;
84 }
85
86 /**
87  * i40iw_query_port - get port attrubutes
88  * @ibdev: device pointer from stack
89  * @port: port number for query
90  * @props: returning device attributes
91  */
92 static int i40iw_query_port(struct ib_device *ibdev,
93                             u8 port,
94                             struct ib_port_attr *props)
95 {
96         struct i40iw_device *iwdev = to_iwdev(ibdev);
97         struct net_device *netdev = iwdev->netdev;
98
99         memset(props, 0, sizeof(*props));
100
101         props->max_mtu = IB_MTU_4096;
102         if (netdev->mtu >= 4096)
103                 props->active_mtu = IB_MTU_4096;
104         else if (netdev->mtu >= 2048)
105                 props->active_mtu = IB_MTU_2048;
106         else if (netdev->mtu >= 1024)
107                 props->active_mtu = IB_MTU_1024;
108         else if (netdev->mtu >= 512)
109                 props->active_mtu = IB_MTU_512;
110         else
111                 props->active_mtu = IB_MTU_256;
112
113         props->lid = 1;
114         if (netif_carrier_ok(iwdev->netdev))
115                 props->state = IB_PORT_ACTIVE;
116         else
117                 props->state = IB_PORT_DOWN;
118         props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
119                 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
120         props->gid_tbl_len = 1;
121         props->pkey_tbl_len = 1;
122         props->active_width = IB_WIDTH_4X;
123         props->active_speed = 1;
124         props->max_msg_sz = I40IW_MAX_OUTBOUND_MESSAGE_SIZE;
125         return 0;
126 }
127
128 /**
129  * i40iw_alloc_ucontext - Allocate the user context data structure
130  * @ibdev: device pointer from stack
131  * @udata: user data
132  *
133  * This keeps track of all objects associated with a particular
134  * user-mode client.
135  */
136 static struct ib_ucontext *i40iw_alloc_ucontext(struct ib_device *ibdev,
137                                                 struct ib_udata *udata)
138 {
139         struct i40iw_device *iwdev = to_iwdev(ibdev);
140         struct i40iw_alloc_ucontext_req req;
141         struct i40iw_alloc_ucontext_resp uresp;
142         struct i40iw_ucontext *ucontext;
143
144         if (ib_copy_from_udata(&req, udata, sizeof(req)))
145                 return ERR_PTR(-EINVAL);
146
147         if (req.userspace_ver != I40IW_ABI_USERSPACE_VER) {
148                 i40iw_pr_err("Invalid userspace driver version detected. Detected version %d, should be %d\n",
149                              req.userspace_ver, I40IW_ABI_USERSPACE_VER);
150                 return ERR_PTR(-EINVAL);
151         }
152
153         memset(&uresp, 0, sizeof(uresp));
154         uresp.max_qps = iwdev->max_qp;
155         uresp.max_pds = iwdev->max_pd;
156         uresp.wq_size = iwdev->max_qp_wr * 2;
157         uresp.kernel_ver = I40IW_ABI_KERNEL_VER;
158
159         ucontext = kzalloc(sizeof(*ucontext), GFP_KERNEL);
160         if (!ucontext)
161                 return ERR_PTR(-ENOMEM);
162
163         ucontext->iwdev = iwdev;
164
165         if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
166                 kfree(ucontext);
167                 return ERR_PTR(-EFAULT);
168         }
169
170         INIT_LIST_HEAD(&ucontext->cq_reg_mem_list);
171         spin_lock_init(&ucontext->cq_reg_mem_list_lock);
172         INIT_LIST_HEAD(&ucontext->qp_reg_mem_list);
173         spin_lock_init(&ucontext->qp_reg_mem_list_lock);
174
175         return &ucontext->ibucontext;
176 }
177
178 /**
179  * i40iw_dealloc_ucontext - deallocate the user context data structure
180  * @context: user context created during alloc
181  */
182 static int i40iw_dealloc_ucontext(struct ib_ucontext *context)
183 {
184         struct i40iw_ucontext *ucontext = to_ucontext(context);
185         unsigned long flags;
186
187         spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
188         if (!list_empty(&ucontext->cq_reg_mem_list)) {
189                 spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
190                 return -EBUSY;
191         }
192         spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
193         spin_lock_irqsave(&ucontext->qp_reg_mem_list_lock, flags);
194         if (!list_empty(&ucontext->qp_reg_mem_list)) {
195                 spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
196                 return -EBUSY;
197         }
198         spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
199
200         kfree(ucontext);
201         return 0;
202 }
203
204 /**
205  * i40iw_mmap - user memory map
206  * @context: context created during alloc
207  * @vma: kernel info for user memory map
208  */
209 static int i40iw_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
210 {
211         struct i40iw_ucontext *ucontext = to_ucontext(context);
212         u64 dbaddr;
213
214         if (vma->vm_pgoff || vma->vm_end - vma->vm_start != PAGE_SIZE)
215                 return -EINVAL;
216
217         dbaddr = I40IW_DB_ADDR_OFFSET + pci_resource_start(ucontext->iwdev->ldev->pcidev, 0);
218
219         if (io_remap_pfn_range(vma, vma->vm_start, dbaddr >> PAGE_SHIFT, PAGE_SIZE,
220                                pgprot_noncached(vma->vm_page_prot)))
221                 return -EAGAIN;
222
223         return 0;
224 }
225
226 /**
227  * i40iw_alloc_push_page - allocate a push page for qp
228  * @iwdev: iwarp device
229  * @qp: hardware control qp
230  */
231 static void i40iw_alloc_push_page(struct i40iw_device *iwdev, struct i40iw_sc_qp *qp)
232 {
233         struct i40iw_cqp_request *cqp_request;
234         struct cqp_commands_info *cqp_info;
235         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
236         enum i40iw_status_code status;
237
238         if (qp->push_idx != I40IW_INVALID_PUSH_PAGE_INDEX)
239                 return;
240
241         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
242         if (!cqp_request)
243                 return;
244
245         atomic_inc(&cqp_request->refcount);
246
247         cqp_info = &cqp_request->info;
248         cqp_info->cqp_cmd = OP_MANAGE_PUSH_PAGE;
249         cqp_info->post_sq = 1;
250
251         cqp_info->in.u.manage_push_page.info.qs_handle = dev->qs_handle;
252         cqp_info->in.u.manage_push_page.info.free_page = 0;
253         cqp_info->in.u.manage_push_page.cqp = &iwdev->cqp.sc_cqp;
254         cqp_info->in.u.manage_push_page.scratch = (uintptr_t)cqp_request;
255
256         status = i40iw_handle_cqp_op(iwdev, cqp_request);
257         if (!status)
258                 qp->push_idx = cqp_request->compl_info.op_ret_val;
259         else
260                 i40iw_pr_err("CQP-OP Push page fail");
261         i40iw_put_cqp_request(&iwdev->cqp, cqp_request);
262 }
263
264 /**
265  * i40iw_dealloc_push_page - free a push page for qp
266  * @iwdev: iwarp device
267  * @qp: hardware control qp
268  */
269 static void i40iw_dealloc_push_page(struct i40iw_device *iwdev, struct i40iw_sc_qp *qp)
270 {
271         struct i40iw_cqp_request *cqp_request;
272         struct cqp_commands_info *cqp_info;
273         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
274         enum i40iw_status_code status;
275
276         if (qp->push_idx == I40IW_INVALID_PUSH_PAGE_INDEX)
277                 return;
278
279         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, false);
280         if (!cqp_request)
281                 return;
282
283         cqp_info = &cqp_request->info;
284         cqp_info->cqp_cmd = OP_MANAGE_PUSH_PAGE;
285         cqp_info->post_sq = 1;
286
287         cqp_info->in.u.manage_push_page.info.push_idx = qp->push_idx;
288         cqp_info->in.u.manage_push_page.info.qs_handle = dev->qs_handle;
289         cqp_info->in.u.manage_push_page.info.free_page = 1;
290         cqp_info->in.u.manage_push_page.cqp = &iwdev->cqp.sc_cqp;
291         cqp_info->in.u.manage_push_page.scratch = (uintptr_t)cqp_request;
292
293         status = i40iw_handle_cqp_op(iwdev, cqp_request);
294         if (!status)
295                 qp->push_idx = I40IW_INVALID_PUSH_PAGE_INDEX;
296         else
297                 i40iw_pr_err("CQP-OP Push page fail");
298 }
299
300 /**
301  * i40iw_alloc_pd - allocate protection domain
302  * @ibdev: device pointer from stack
303  * @context: user context created during alloc
304  * @udata: user data
305  */
306 static struct ib_pd *i40iw_alloc_pd(struct ib_device *ibdev,
307                                     struct ib_ucontext *context,
308                                     struct ib_udata *udata)
309 {
310         struct i40iw_pd *iwpd;
311         struct i40iw_device *iwdev = to_iwdev(ibdev);
312         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
313         struct i40iw_alloc_pd_resp uresp;
314         struct i40iw_sc_pd *sc_pd;
315         u32 pd_id = 0;
316         int err;
317
318         err = i40iw_alloc_resource(iwdev, iwdev->allocated_pds,
319                                    iwdev->max_pd, &pd_id, &iwdev->next_pd);
320         if (err) {
321                 i40iw_pr_err("alloc resource failed\n");
322                 return ERR_PTR(err);
323         }
324
325         iwpd = kzalloc(sizeof(*iwpd), GFP_KERNEL);
326         if (!iwpd) {
327                 err = -ENOMEM;
328                 goto free_res;
329         }
330
331         sc_pd = &iwpd->sc_pd;
332         dev->iw_pd_ops->pd_init(dev, sc_pd, pd_id);
333
334         if (context) {
335                 memset(&uresp, 0, sizeof(uresp));
336                 uresp.pd_id = pd_id;
337                 if (ib_copy_to_udata(udata, &uresp, sizeof(uresp))) {
338                         err = -EFAULT;
339                         goto error;
340                 }
341         }
342
343         i40iw_add_pdusecount(iwpd);
344         return &iwpd->ibpd;
345 error:
346         kfree(iwpd);
347 free_res:
348         i40iw_free_resource(iwdev, iwdev->allocated_pds, pd_id);
349         return ERR_PTR(err);
350 }
351
352 /**
353  * i40iw_dealloc_pd - deallocate pd
354  * @ibpd: ptr of pd to be deallocated
355  */
356 static int i40iw_dealloc_pd(struct ib_pd *ibpd)
357 {
358         struct i40iw_pd *iwpd = to_iwpd(ibpd);
359         struct i40iw_device *iwdev = to_iwdev(ibpd->device);
360
361         i40iw_rem_pdusecount(iwpd, iwdev);
362         return 0;
363 }
364
365 /**
366  * i40iw_qp_roundup - return round up qp ring size
367  * @wr_ring_size: ring size to round up
368  */
369 static int i40iw_qp_roundup(u32 wr_ring_size)
370 {
371         int scount = 1;
372
373         if (wr_ring_size < I40IWQP_SW_MIN_WQSIZE)
374                 wr_ring_size = I40IWQP_SW_MIN_WQSIZE;
375
376         for (wr_ring_size--; scount <= 16; scount *= 2)
377                 wr_ring_size |= wr_ring_size >> scount;
378         return ++wr_ring_size;
379 }
380
381 /**
382  * i40iw_get_pbl - Retrieve pbl from a list given a virtual
383  * address
384  * @va: user virtual address
385  * @pbl_list: pbl list to search in (QP's or CQ's)
386  */
387 static struct i40iw_pbl *i40iw_get_pbl(unsigned long va,
388                                        struct list_head *pbl_list)
389 {
390         struct i40iw_pbl *iwpbl;
391
392         list_for_each_entry(iwpbl, pbl_list, list) {
393                 if (iwpbl->user_base == va) {
394                         list_del(&iwpbl->list);
395                         return iwpbl;
396                 }
397         }
398         return NULL;
399 }
400
401 /**
402  * i40iw_free_qp_resources - free up memory resources for qp
403  * @iwdev: iwarp device
404  * @iwqp: qp ptr (user or kernel)
405  * @qp_num: qp number assigned
406  */
407 void i40iw_free_qp_resources(struct i40iw_device *iwdev,
408                              struct i40iw_qp *iwqp,
409                              u32 qp_num)
410 {
411         i40iw_dealloc_push_page(iwdev, &iwqp->sc_qp);
412         if (qp_num)
413                 i40iw_free_resource(iwdev, iwdev->allocated_qps, qp_num);
414         i40iw_free_dma_mem(iwdev->sc_dev.hw, &iwqp->q2_ctx_mem);
415         i40iw_free_dma_mem(iwdev->sc_dev.hw, &iwqp->kqp.dma_mem);
416         kfree(iwqp->kqp.wrid_mem);
417         iwqp->kqp.wrid_mem = NULL;
418         kfree(iwqp->allocated_buffer);
419 }
420
421 /**
422  * i40iw_clean_cqes - clean cq entries for qp
423  * @iwqp: qp ptr (user or kernel)
424  * @iwcq: cq ptr
425  */
426 static void i40iw_clean_cqes(struct i40iw_qp *iwqp, struct i40iw_cq *iwcq)
427 {
428         struct i40iw_cq_uk *ukcq = &iwcq->sc_cq.cq_uk;
429
430         ukcq->ops.iw_cq_clean(&iwqp->sc_qp.qp_uk, ukcq);
431 }
432
433 /**
434  * i40iw_destroy_qp - destroy qp
435  * @ibqp: qp's ib pointer also to get to device's qp address
436  */
437 static int i40iw_destroy_qp(struct ib_qp *ibqp)
438 {
439         struct i40iw_qp *iwqp = to_iwqp(ibqp);
440
441         iwqp->destroyed = 1;
442
443         if (iwqp->ibqp_state >= IB_QPS_INIT && iwqp->ibqp_state < IB_QPS_RTS)
444                 i40iw_next_iw_state(iwqp, I40IW_QP_STATE_ERROR, 0, 0, 0);
445
446         if (!iwqp->user_mode) {
447                 if (iwqp->iwscq) {
448                         i40iw_clean_cqes(iwqp, iwqp->iwscq);
449                         if (iwqp->iwrcq != iwqp->iwscq)
450                                 i40iw_clean_cqes(iwqp, iwqp->iwrcq);
451                 }
452         }
453
454         i40iw_rem_ref(&iwqp->ibqp);
455         return 0;
456 }
457
458 /**
459  * i40iw_setup_virt_qp - setup for allocation of virtual qp
460  * @dev: iwarp device
461  * @qp: qp ptr
462  * @init_info: initialize info to return
463  */
464 static int i40iw_setup_virt_qp(struct i40iw_device *iwdev,
465                                struct i40iw_qp *iwqp,
466                                struct i40iw_qp_init_info *init_info)
467 {
468         struct i40iw_pbl *iwpbl = iwqp->iwpbl;
469         struct i40iw_qp_mr *qpmr = &iwpbl->qp_mr;
470
471         iwqp->page = qpmr->sq_page;
472         init_info->shadow_area_pa = cpu_to_le64(qpmr->shadow);
473         if (iwpbl->pbl_allocated) {
474                 init_info->virtual_map = true;
475                 init_info->sq_pa = qpmr->sq_pbl.idx;
476                 init_info->rq_pa = qpmr->rq_pbl.idx;
477         } else {
478                 init_info->sq_pa = qpmr->sq_pbl.addr;
479                 init_info->rq_pa = qpmr->rq_pbl.addr;
480         }
481         return 0;
482 }
483
484 /**
485  * i40iw_setup_kmode_qp - setup initialization for kernel mode qp
486  * @iwdev: iwarp device
487  * @iwqp: qp ptr (user or kernel)
488  * @info: initialize info to return
489  */
490 static int i40iw_setup_kmode_qp(struct i40iw_device *iwdev,
491                                 struct i40iw_qp *iwqp,
492                                 struct i40iw_qp_init_info *info)
493 {
494         struct i40iw_dma_mem *mem = &iwqp->kqp.dma_mem;
495         u32 sqdepth, rqdepth;
496         u32 sq_size, rq_size;
497         u8 sqshift, rqshift;
498         u32 size;
499         enum i40iw_status_code status;
500         struct i40iw_qp_uk_init_info *ukinfo = &info->qp_uk_init_info;
501
502         sq_size = i40iw_qp_roundup(ukinfo->sq_size + 1);
503         rq_size = i40iw_qp_roundup(ukinfo->rq_size + 1);
504
505         status = i40iw_get_wqe_shift(sq_size, ukinfo->max_sq_frag_cnt, ukinfo->max_inline_data, &sqshift);
506         if (!status)
507                 status = i40iw_get_wqe_shift(rq_size, ukinfo->max_rq_frag_cnt, 0, &rqshift);
508
509         if (status)
510                 return -ENOMEM;
511
512         sqdepth = sq_size << sqshift;
513         rqdepth = rq_size << rqshift;
514
515         size = sqdepth * sizeof(struct i40iw_sq_uk_wr_trk_info) + (rqdepth << 3);
516         iwqp->kqp.wrid_mem = kzalloc(size, GFP_KERNEL);
517
518         ukinfo->sq_wrtrk_array = (struct i40iw_sq_uk_wr_trk_info *)iwqp->kqp.wrid_mem;
519         if (!ukinfo->sq_wrtrk_array)
520                 return -ENOMEM;
521
522         ukinfo->rq_wrid_array = (u64 *)&ukinfo->sq_wrtrk_array[sqdepth];
523
524         size = (sqdepth + rqdepth) * I40IW_QP_WQE_MIN_SIZE;
525         size += (I40IW_SHADOW_AREA_SIZE << 3);
526
527         status = i40iw_allocate_dma_mem(iwdev->sc_dev.hw, mem, size, 256);
528         if (status) {
529                 kfree(ukinfo->sq_wrtrk_array);
530                 ukinfo->sq_wrtrk_array = NULL;
531                 return -ENOMEM;
532         }
533
534         ukinfo->sq = mem->va;
535         info->sq_pa = mem->pa;
536
537         ukinfo->rq = &ukinfo->sq[sqdepth];
538         info->rq_pa = info->sq_pa + (sqdepth * I40IW_QP_WQE_MIN_SIZE);
539
540         ukinfo->shadow_area = ukinfo->rq[rqdepth].elem;
541         info->shadow_area_pa = info->rq_pa + (rqdepth * I40IW_QP_WQE_MIN_SIZE);
542
543         ukinfo->sq_size = sq_size;
544         ukinfo->rq_size = rq_size;
545         ukinfo->qp_id = iwqp->ibqp.qp_num;
546         return 0;
547 }
548
549 /**
550  * i40iw_create_qp - create qp
551  * @ibpd: ptr of pd
552  * @init_attr: attributes for qp
553  * @udata: user data for create qp
554  */
555 static struct ib_qp *i40iw_create_qp(struct ib_pd *ibpd,
556                                      struct ib_qp_init_attr *init_attr,
557                                      struct ib_udata *udata)
558 {
559         struct i40iw_pd *iwpd = to_iwpd(ibpd);
560         struct i40iw_device *iwdev = to_iwdev(ibpd->device);
561         struct i40iw_cqp *iwcqp = &iwdev->cqp;
562         struct i40iw_qp *iwqp;
563         struct i40iw_ucontext *ucontext;
564         struct i40iw_create_qp_req req;
565         struct i40iw_create_qp_resp uresp;
566         u32 qp_num = 0;
567         void *mem;
568         enum i40iw_status_code ret;
569         int err_code;
570         int sq_size;
571         int rq_size;
572         struct i40iw_sc_qp *qp;
573         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
574         struct i40iw_qp_init_info init_info;
575         struct i40iw_create_qp_info *qp_info;
576         struct i40iw_cqp_request *cqp_request;
577         struct cqp_commands_info *cqp_info;
578
579         struct i40iw_qp_host_ctx_info *ctx_info;
580         struct i40iwarp_offload_info *iwarp_info;
581         unsigned long flags;
582
583         if (init_attr->create_flags)
584                 return ERR_PTR(-EINVAL);
585         if (init_attr->cap.max_inline_data > I40IW_MAX_INLINE_DATA_SIZE)
586                 init_attr->cap.max_inline_data = I40IW_MAX_INLINE_DATA_SIZE;
587
588         if (init_attr->cap.max_send_sge > I40IW_MAX_WQ_FRAGMENT_COUNT)
589                 init_attr->cap.max_send_sge = I40IW_MAX_WQ_FRAGMENT_COUNT;
590
591         memset(&init_info, 0, sizeof(init_info));
592
593         sq_size = init_attr->cap.max_send_wr;
594         rq_size = init_attr->cap.max_recv_wr;
595
596         init_info.qp_uk_init_info.sq_size = sq_size;
597         init_info.qp_uk_init_info.rq_size = rq_size;
598         init_info.qp_uk_init_info.max_sq_frag_cnt = init_attr->cap.max_send_sge;
599         init_info.qp_uk_init_info.max_rq_frag_cnt = init_attr->cap.max_recv_sge;
600         init_info.qp_uk_init_info.max_inline_data = init_attr->cap.max_inline_data;
601
602         mem = kzalloc(sizeof(*iwqp), GFP_KERNEL);
603         if (!mem)
604                 return ERR_PTR(-ENOMEM);
605
606         iwqp = (struct i40iw_qp *)mem;
607         qp = &iwqp->sc_qp;
608         qp->back_qp = (void *)iwqp;
609         qp->push_idx = I40IW_INVALID_PUSH_PAGE_INDEX;
610
611         iwqp->ctx_info.iwarp_info = &iwqp->iwarp_info;
612
613         if (i40iw_allocate_dma_mem(dev->hw,
614                                    &iwqp->q2_ctx_mem,
615                                    I40IW_Q2_BUFFER_SIZE + I40IW_QP_CTX_SIZE,
616                                    256)) {
617                 i40iw_pr_err("dma_mem failed\n");
618                 err_code = -ENOMEM;
619                 goto error;
620         }
621
622         init_info.q2 = iwqp->q2_ctx_mem.va;
623         init_info.q2_pa = iwqp->q2_ctx_mem.pa;
624
625         init_info.host_ctx = (void *)init_info.q2 + I40IW_Q2_BUFFER_SIZE;
626         init_info.host_ctx_pa = init_info.q2_pa + I40IW_Q2_BUFFER_SIZE;
627
628         err_code = i40iw_alloc_resource(iwdev, iwdev->allocated_qps, iwdev->max_qp,
629                                         &qp_num, &iwdev->next_qp);
630         if (err_code) {
631                 i40iw_pr_err("qp resource\n");
632                 goto error;
633         }
634
635         iwqp->allocated_buffer = mem;
636         iwqp->iwdev = iwdev;
637         iwqp->iwpd = iwpd;
638         iwqp->ibqp.qp_num = qp_num;
639         qp = &iwqp->sc_qp;
640         iwqp->iwscq = to_iwcq(init_attr->send_cq);
641         iwqp->iwrcq = to_iwcq(init_attr->recv_cq);
642
643         iwqp->host_ctx.va = init_info.host_ctx;
644         iwqp->host_ctx.pa = init_info.host_ctx_pa;
645         iwqp->host_ctx.size = I40IW_QP_CTX_SIZE;
646
647         init_info.pd = &iwpd->sc_pd;
648         init_info.qp_uk_init_info.qp_id = iwqp->ibqp.qp_num;
649         iwqp->ctx_info.qp_compl_ctx = (uintptr_t)qp;
650
651         if (init_attr->qp_type != IB_QPT_RC) {
652                 err_code = -EINVAL;
653                 goto error;
654         }
655         if (iwdev->push_mode)
656                 i40iw_alloc_push_page(iwdev, qp);
657         if (udata) {
658                 err_code = ib_copy_from_udata(&req, udata, sizeof(req));
659                 if (err_code) {
660                         i40iw_pr_err("ib_copy_from_data\n");
661                         goto error;
662                 }
663                 iwqp->ctx_info.qp_compl_ctx = req.user_compl_ctx;
664                 if (ibpd->uobject && ibpd->uobject->context) {
665                         iwqp->user_mode = 1;
666                         ucontext = to_ucontext(ibpd->uobject->context);
667
668                         if (req.user_wqe_buffers) {
669                                 spin_lock_irqsave(
670                                     &ucontext->qp_reg_mem_list_lock, flags);
671                                 iwqp->iwpbl = i40iw_get_pbl(
672                                     (unsigned long)req.user_wqe_buffers,
673                                     &ucontext->qp_reg_mem_list);
674                                 spin_unlock_irqrestore(
675                                     &ucontext->qp_reg_mem_list_lock, flags);
676
677                                 if (!iwqp->iwpbl) {
678                                         err_code = -ENODATA;
679                                         i40iw_pr_err("no pbl info\n");
680                                         goto error;
681                                 }
682                         }
683                 }
684                 err_code = i40iw_setup_virt_qp(iwdev, iwqp, &init_info);
685         } else {
686                 err_code = i40iw_setup_kmode_qp(iwdev, iwqp, &init_info);
687         }
688
689         if (err_code) {
690                 i40iw_pr_err("setup qp failed\n");
691                 goto error;
692         }
693
694         init_info.type = I40IW_QP_TYPE_IWARP;
695         ret = dev->iw_priv_qp_ops->qp_init(qp, &init_info);
696         if (ret) {
697                 err_code = -EPROTO;
698                 i40iw_pr_err("qp_init fail\n");
699                 goto error;
700         }
701         ctx_info = &iwqp->ctx_info;
702         iwarp_info = &iwqp->iwarp_info;
703         iwarp_info->rd_enable = true;
704         iwarp_info->wr_rdresp_en = true;
705         if (!iwqp->user_mode) {
706                 iwarp_info->fast_reg_en = true;
707                 iwarp_info->priv_mode_en = true;
708         }
709         iwarp_info->ddp_ver = 1;
710         iwarp_info->rdmap_ver = 1;
711
712         ctx_info->iwarp_info_valid = true;
713         ctx_info->send_cq_num = iwqp->iwscq->sc_cq.cq_uk.cq_id;
714         ctx_info->rcv_cq_num = iwqp->iwrcq->sc_cq.cq_uk.cq_id;
715         if (qp->push_idx == I40IW_INVALID_PUSH_PAGE_INDEX) {
716                 ctx_info->push_mode_en = false;
717         } else {
718                 ctx_info->push_mode_en = true;
719                 ctx_info->push_idx = qp->push_idx;
720         }
721
722         ret = dev->iw_priv_qp_ops->qp_setctx(&iwqp->sc_qp,
723                                              (u64 *)iwqp->host_ctx.va,
724                                              ctx_info);
725         ctx_info->iwarp_info_valid = false;
726         cqp_request = i40iw_get_cqp_request(iwcqp, true);
727         if (!cqp_request) {
728                 err_code = -ENOMEM;
729                 goto error;
730         }
731         cqp_info = &cqp_request->info;
732         qp_info = &cqp_request->info.in.u.qp_create.info;
733
734         memset(qp_info, 0, sizeof(*qp_info));
735
736         qp_info->cq_num_valid = true;
737         qp_info->next_iwarp_state = I40IW_QP_STATE_IDLE;
738
739         cqp_info->cqp_cmd = OP_QP_CREATE;
740         cqp_info->post_sq = 1;
741         cqp_info->in.u.qp_create.qp = qp;
742         cqp_info->in.u.qp_create.scratch = (uintptr_t)cqp_request;
743         ret = i40iw_handle_cqp_op(iwdev, cqp_request);
744         if (ret) {
745                 i40iw_pr_err("CQP-OP QP create fail");
746                 err_code = -EACCES;
747                 goto error;
748         }
749
750         i40iw_add_ref(&iwqp->ibqp);
751         spin_lock_init(&iwqp->lock);
752         iwqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR) ? 1 : 0;
753         iwdev->qp_table[qp_num] = iwqp;
754         i40iw_add_pdusecount(iwqp->iwpd);
755         if (ibpd->uobject && udata) {
756                 memset(&uresp, 0, sizeof(uresp));
757                 uresp.actual_sq_size = sq_size;
758                 uresp.actual_rq_size = rq_size;
759                 uresp.qp_id = qp_num;
760                 uresp.push_idx = qp->push_idx;
761                 err_code = ib_copy_to_udata(udata, &uresp, sizeof(uresp));
762                 if (err_code) {
763                         i40iw_pr_err("copy_to_udata failed\n");
764                         i40iw_destroy_qp(&iwqp->ibqp);
765                            /* let the completion of the qp destroy free the qp */
766                         return ERR_PTR(err_code);
767                 }
768         }
769         init_completion(&iwqp->sq_drained);
770         init_completion(&iwqp->rq_drained);
771
772         return &iwqp->ibqp;
773 error:
774         i40iw_free_qp_resources(iwdev, iwqp, qp_num);
775         return ERR_PTR(err_code);
776 }
777
778 /**
779  * i40iw_query - query qp attributes
780  * @ibqp: qp pointer
781  * @attr: attributes pointer
782  * @attr_mask: Not used
783  * @init_attr: qp attributes to return
784  */
785 static int i40iw_query_qp(struct ib_qp *ibqp,
786                           struct ib_qp_attr *attr,
787                           int attr_mask,
788                           struct ib_qp_init_attr *init_attr)
789 {
790         struct i40iw_qp *iwqp = to_iwqp(ibqp);
791         struct i40iw_sc_qp *qp = &iwqp->sc_qp;
792
793         attr->qp_state = iwqp->ibqp_state;
794         attr->cur_qp_state = attr->qp_state;
795         attr->qp_access_flags = 0;
796         attr->cap.max_send_wr = qp->qp_uk.sq_size;
797         attr->cap.max_recv_wr = qp->qp_uk.rq_size;
798         attr->cap.max_recv_sge = 1;
799         attr->cap.max_inline_data = I40IW_MAX_INLINE_DATA_SIZE;
800         init_attr->event_handler = iwqp->ibqp.event_handler;
801         init_attr->qp_context = iwqp->ibqp.qp_context;
802         init_attr->send_cq = iwqp->ibqp.send_cq;
803         init_attr->recv_cq = iwqp->ibqp.recv_cq;
804         init_attr->srq = iwqp->ibqp.srq;
805         init_attr->cap = attr->cap;
806         return 0;
807 }
808
809 /**
810  * i40iw_hw_modify_qp - setup cqp for modify qp
811  * @iwdev: iwarp device
812  * @iwqp: qp ptr (user or kernel)
813  * @info: info for modify qp
814  * @wait: flag to wait or not for modify qp completion
815  */
816 void i40iw_hw_modify_qp(struct i40iw_device *iwdev, struct i40iw_qp *iwqp,
817                         struct i40iw_modify_qp_info *info, bool wait)
818 {
819         enum i40iw_status_code status;
820         struct i40iw_cqp_request *cqp_request;
821         struct cqp_commands_info *cqp_info;
822         struct i40iw_modify_qp_info *m_info;
823
824         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, wait);
825         if (!cqp_request)
826                 return;
827
828         cqp_info = &cqp_request->info;
829         m_info = &cqp_info->in.u.qp_modify.info;
830         memcpy(m_info, info, sizeof(*m_info));
831         cqp_info->cqp_cmd = OP_QP_MODIFY;
832         cqp_info->post_sq = 1;
833         cqp_info->in.u.qp_modify.qp = &iwqp->sc_qp;
834         cqp_info->in.u.qp_modify.scratch = (uintptr_t)cqp_request;
835         status = i40iw_handle_cqp_op(iwdev, cqp_request);
836         if (status)
837                 i40iw_pr_err("CQP-OP Modify QP fail");
838 }
839
840 /**
841  * i40iw_modify_qp - modify qp request
842  * @ibqp: qp's pointer for modify
843  * @attr: access attributes
844  * @attr_mask: state mask
845  * @udata: user data
846  */
847 int i40iw_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
848                     int attr_mask, struct ib_udata *udata)
849 {
850         struct i40iw_qp *iwqp = to_iwqp(ibqp);
851         struct i40iw_device *iwdev = iwqp->iwdev;
852         struct i40iw_qp_host_ctx_info *ctx_info;
853         struct i40iwarp_offload_info *iwarp_info;
854         struct i40iw_modify_qp_info info;
855         u8 issue_modify_qp = 0;
856         u8 dont_wait = 0;
857         u32 err;
858         unsigned long flags;
859
860         memset(&info, 0, sizeof(info));
861         ctx_info = &iwqp->ctx_info;
862         iwarp_info = &iwqp->iwarp_info;
863
864         spin_lock_irqsave(&iwqp->lock, flags);
865
866         if (attr_mask & IB_QP_STATE) {
867                 switch (attr->qp_state) {
868                 case IB_QPS_INIT:
869                 case IB_QPS_RTR:
870                         if (iwqp->iwarp_state > (u32)I40IW_QP_STATE_IDLE) {
871                                 err = -EINVAL;
872                                 goto exit;
873                         }
874                         if (iwqp->iwarp_state == I40IW_QP_STATE_INVALID) {
875                                 info.next_iwarp_state = I40IW_QP_STATE_IDLE;
876                                 issue_modify_qp = 1;
877                         }
878                         break;
879                 case IB_QPS_RTS:
880                         if ((iwqp->iwarp_state > (u32)I40IW_QP_STATE_RTS) ||
881                             (!iwqp->cm_id)) {
882                                 err = -EINVAL;
883                                 goto exit;
884                         }
885
886                         issue_modify_qp = 1;
887                         iwqp->hw_tcp_state = I40IW_TCP_STATE_ESTABLISHED;
888                         iwqp->hte_added = 1;
889                         info.next_iwarp_state = I40IW_QP_STATE_RTS;
890                         info.tcp_ctx_valid = true;
891                         info.ord_valid = true;
892                         info.arp_cache_idx_valid = true;
893                         info.cq_num_valid = true;
894                         break;
895                 case IB_QPS_SQD:
896                         if (iwqp->hw_iwarp_state > (u32)I40IW_QP_STATE_RTS) {
897                                 err = 0;
898                                 goto exit;
899                         }
900                         if ((iwqp->iwarp_state == (u32)I40IW_QP_STATE_CLOSING) ||
901                             (iwqp->iwarp_state < (u32)I40IW_QP_STATE_RTS)) {
902                                 err = 0;
903                                 goto exit;
904                         }
905                         if (iwqp->iwarp_state > (u32)I40IW_QP_STATE_CLOSING) {
906                                 err = -EINVAL;
907                                 goto exit;
908                         }
909                         info.next_iwarp_state = I40IW_QP_STATE_CLOSING;
910                         issue_modify_qp = 1;
911                         break;
912                 case IB_QPS_SQE:
913                         if (iwqp->iwarp_state >= (u32)I40IW_QP_STATE_TERMINATE) {
914                                 err = -EINVAL;
915                                 goto exit;
916                         }
917                         info.next_iwarp_state = I40IW_QP_STATE_TERMINATE;
918                         issue_modify_qp = 1;
919                         break;
920                 case IB_QPS_ERR:
921                 case IB_QPS_RESET:
922                         if (iwqp->iwarp_state == (u32)I40IW_QP_STATE_ERROR) {
923                                 err = -EINVAL;
924                                 goto exit;
925                         }
926                         if (iwqp->sc_qp.term_flags)
927                                 del_timer(&iwqp->terminate_timer);
928                         info.next_iwarp_state = I40IW_QP_STATE_ERROR;
929                         if ((iwqp->hw_tcp_state > I40IW_TCP_STATE_CLOSED) &&
930                             iwdev->iw_status &&
931                             (iwqp->hw_tcp_state != I40IW_TCP_STATE_TIME_WAIT))
932                                 info.reset_tcp_conn = true;
933                         else
934                                 dont_wait = 1;
935                         issue_modify_qp = 1;
936                         info.next_iwarp_state = I40IW_QP_STATE_ERROR;
937                         break;
938                 default:
939                         err = -EINVAL;
940                         goto exit;
941                 }
942
943                 iwqp->ibqp_state = attr->qp_state;
944
945                 if (issue_modify_qp)
946                         iwqp->iwarp_state = info.next_iwarp_state;
947                 else
948                         info.next_iwarp_state = iwqp->iwarp_state;
949         }
950         if (attr_mask & IB_QP_ACCESS_FLAGS) {
951                 ctx_info->iwarp_info_valid = true;
952                 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE)
953                         iwarp_info->wr_rdresp_en = true;
954                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
955                         iwarp_info->wr_rdresp_en = true;
956                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
957                         iwarp_info->rd_enable = true;
958                 if (attr->qp_access_flags & IB_ACCESS_MW_BIND)
959                         iwarp_info->bind_en = true;
960
961                 if (iwqp->user_mode) {
962                         iwarp_info->rd_enable = true;
963                         iwarp_info->wr_rdresp_en = true;
964                         iwarp_info->priv_mode_en = false;
965                 }
966         }
967
968         if (ctx_info->iwarp_info_valid) {
969                 struct i40iw_sc_dev *dev = &iwdev->sc_dev;
970                 int ret;
971
972                 ctx_info->send_cq_num = iwqp->iwscq->sc_cq.cq_uk.cq_id;
973                 ctx_info->rcv_cq_num = iwqp->iwrcq->sc_cq.cq_uk.cq_id;
974                 ret = dev->iw_priv_qp_ops->qp_setctx(&iwqp->sc_qp,
975                                                      (u64 *)iwqp->host_ctx.va,
976                                                      ctx_info);
977                 if (ret) {
978                         i40iw_pr_err("setting QP context\n");
979                         err = -EINVAL;
980                         goto exit;
981                 }
982         }
983
984         spin_unlock_irqrestore(&iwqp->lock, flags);
985
986         if (issue_modify_qp)
987                 i40iw_hw_modify_qp(iwdev, iwqp, &info, true);
988
989         if (issue_modify_qp && (iwqp->ibqp_state > IB_QPS_RTS)) {
990                 if (dont_wait) {
991                         if (iwqp->cm_id && iwqp->hw_tcp_state) {
992                                 spin_lock_irqsave(&iwqp->lock, flags);
993                                 iwqp->hw_tcp_state = I40IW_TCP_STATE_CLOSED;
994                                 iwqp->last_aeq = I40IW_AE_RESET_SENT;
995                                 spin_unlock_irqrestore(&iwqp->lock, flags);
996                         }
997                 }
998         }
999         return 0;
1000 exit:
1001         spin_unlock_irqrestore(&iwqp->lock, flags);
1002         return err;
1003 }
1004
1005 /**
1006  * cq_free_resources - free up recources for cq
1007  * @iwdev: iwarp device
1008  * @iwcq: cq ptr
1009  */
1010 static void cq_free_resources(struct i40iw_device *iwdev, struct i40iw_cq *iwcq)
1011 {
1012         struct i40iw_sc_cq *cq = &iwcq->sc_cq;
1013
1014         if (!iwcq->user_mode)
1015                 i40iw_free_dma_mem(iwdev->sc_dev.hw, &iwcq->kmem);
1016         i40iw_free_resource(iwdev, iwdev->allocated_cqs, cq->cq_uk.cq_id);
1017 }
1018
1019 /**
1020  * cq_wq_destroy - send cq destroy cqp
1021  * @iwdev: iwarp device
1022  * @cq: hardware control cq
1023  */
1024 static void cq_wq_destroy(struct i40iw_device *iwdev, struct i40iw_sc_cq *cq)
1025 {
1026         enum i40iw_status_code status;
1027         struct i40iw_cqp_request *cqp_request;
1028         struct cqp_commands_info *cqp_info;
1029
1030         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1031         if (!cqp_request)
1032                 return;
1033
1034         cqp_info = &cqp_request->info;
1035
1036         cqp_info->cqp_cmd = OP_CQ_DESTROY;
1037         cqp_info->post_sq = 1;
1038         cqp_info->in.u.cq_destroy.cq = cq;
1039         cqp_info->in.u.cq_destroy.scratch = (uintptr_t)cqp_request;
1040         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1041         if (status)
1042                 i40iw_pr_err("CQP-OP Destroy QP fail");
1043 }
1044
1045 /**
1046  * i40iw_destroy_cq - destroy cq
1047  * @ib_cq: cq pointer
1048  */
1049 static int i40iw_destroy_cq(struct ib_cq *ib_cq)
1050 {
1051         struct i40iw_cq *iwcq;
1052         struct i40iw_device *iwdev;
1053         struct i40iw_sc_cq *cq;
1054
1055         if (!ib_cq) {
1056                 i40iw_pr_err("ib_cq == NULL\n");
1057                 return 0;
1058         }
1059
1060         iwcq = to_iwcq(ib_cq);
1061         iwdev = to_iwdev(ib_cq->device);
1062         cq = &iwcq->sc_cq;
1063         cq_wq_destroy(iwdev, cq);
1064         cq_free_resources(iwdev, iwcq);
1065         kfree(iwcq);
1066         return 0;
1067 }
1068
1069 /**
1070  * i40iw_create_cq - create cq
1071  * @ibdev: device pointer from stack
1072  * @attr: attributes for cq
1073  * @context: user context created during alloc
1074  * @udata: user data
1075  */
1076 static struct ib_cq *i40iw_create_cq(struct ib_device *ibdev,
1077                                      const struct ib_cq_init_attr *attr,
1078                                      struct ib_ucontext *context,
1079                                      struct ib_udata *udata)
1080 {
1081         struct i40iw_device *iwdev = to_iwdev(ibdev);
1082         struct i40iw_cq *iwcq;
1083         struct i40iw_pbl *iwpbl;
1084         u32 cq_num = 0;
1085         struct i40iw_sc_cq *cq;
1086         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
1087         struct i40iw_cq_init_info info;
1088         enum i40iw_status_code status;
1089         struct i40iw_cqp_request *cqp_request;
1090         struct cqp_commands_info *cqp_info;
1091         struct i40iw_cq_uk_init_info *ukinfo = &info.cq_uk_init_info;
1092         unsigned long flags;
1093         int err_code;
1094         int entries = attr->cqe;
1095
1096         if (entries > iwdev->max_cqe)
1097                 return ERR_PTR(-EINVAL);
1098
1099         iwcq = kzalloc(sizeof(*iwcq), GFP_KERNEL);
1100         if (!iwcq)
1101                 return ERR_PTR(-ENOMEM);
1102
1103         memset(&info, 0, sizeof(info));
1104
1105         err_code = i40iw_alloc_resource(iwdev, iwdev->allocated_cqs,
1106                                         iwdev->max_cq, &cq_num,
1107                                         &iwdev->next_cq);
1108         if (err_code)
1109                 goto error;
1110
1111         cq = &iwcq->sc_cq;
1112         cq->back_cq = (void *)iwcq;
1113         spin_lock_init(&iwcq->lock);
1114
1115         info.dev = dev;
1116         ukinfo->cq_size = max(entries, 4);
1117         ukinfo->cq_id = cq_num;
1118         iwcq->ibcq.cqe = info.cq_uk_init_info.cq_size;
1119         info.ceqe_mask = 0;
1120         info.ceq_id = 0;
1121         info.ceq_id_valid = true;
1122         info.ceqe_mask = 1;
1123         info.type = I40IW_CQ_TYPE_IWARP;
1124         if (context) {
1125                 struct i40iw_ucontext *ucontext;
1126                 struct i40iw_create_cq_req req;
1127                 struct i40iw_cq_mr *cqmr;
1128
1129                 memset(&req, 0, sizeof(req));
1130                 iwcq->user_mode = true;
1131                 ucontext = to_ucontext(context);
1132                 if (ib_copy_from_udata(&req, udata, sizeof(struct i40iw_create_cq_req)))
1133                         goto cq_free_resources;
1134
1135                 spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
1136                 iwpbl = i40iw_get_pbl((unsigned long)req.user_cq_buffer,
1137                                       &ucontext->cq_reg_mem_list);
1138                 spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
1139                 if (!iwpbl) {
1140                         err_code = -EPROTO;
1141                         goto cq_free_resources;
1142                 }
1143
1144                 iwcq->iwpbl = iwpbl;
1145                 iwcq->cq_mem_size = 0;
1146                 cqmr = &iwpbl->cq_mr;
1147                 info.shadow_area_pa = cpu_to_le64(cqmr->shadow);
1148                 if (iwpbl->pbl_allocated) {
1149                         info.virtual_map = true;
1150                         info.pbl_chunk_size = 1;
1151                         info.first_pm_pbl_idx = cqmr->cq_pbl.idx;
1152                 } else {
1153                         info.cq_base_pa = cqmr->cq_pbl.addr;
1154                 }
1155         } else {
1156                 /* Kmode allocations */
1157                 int rsize;
1158                 int shadow;
1159
1160                 rsize = info.cq_uk_init_info.cq_size * sizeof(struct i40iw_cqe);
1161                 rsize = round_up(rsize, 256);
1162                 shadow = I40IW_SHADOW_AREA_SIZE << 3;
1163                 status = i40iw_allocate_dma_mem(dev->hw, &iwcq->kmem,
1164                                                 rsize + shadow, 256);
1165                 if (status) {
1166                         err_code = -ENOMEM;
1167                         goto cq_free_resources;
1168                 }
1169                 ukinfo->cq_base = iwcq->kmem.va;
1170                 info.cq_base_pa = iwcq->kmem.pa;
1171                 info.shadow_area_pa = info.cq_base_pa + rsize;
1172                 ukinfo->shadow_area = iwcq->kmem.va + rsize;
1173         }
1174
1175         if (dev->iw_priv_cq_ops->cq_init(cq, &info)) {
1176                 i40iw_pr_err("init cq fail\n");
1177                 err_code = -EPROTO;
1178                 goto cq_free_resources;
1179         }
1180
1181         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1182         if (!cqp_request) {
1183                 err_code = -ENOMEM;
1184                 goto cq_free_resources;
1185         }
1186
1187         cqp_info = &cqp_request->info;
1188         cqp_info->cqp_cmd = OP_CQ_CREATE;
1189         cqp_info->post_sq = 1;
1190         cqp_info->in.u.cq_create.cq = cq;
1191         cqp_info->in.u.cq_create.scratch = (uintptr_t)cqp_request;
1192         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1193         if (status) {
1194                 i40iw_pr_err("CQP-OP Create QP fail");
1195                 err_code = -EPROTO;
1196                 goto cq_free_resources;
1197         }
1198
1199         if (context) {
1200                 struct i40iw_create_cq_resp resp;
1201
1202                 memset(&resp, 0, sizeof(resp));
1203                 resp.cq_id = info.cq_uk_init_info.cq_id;
1204                 resp.cq_size = info.cq_uk_init_info.cq_size;
1205                 if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
1206                         i40iw_pr_err("copy to user data\n");
1207                         err_code = -EPROTO;
1208                         goto cq_destroy;
1209                 }
1210         }
1211
1212         return (struct ib_cq *)iwcq;
1213
1214 cq_destroy:
1215         cq_wq_destroy(iwdev, cq);
1216 cq_free_resources:
1217         cq_free_resources(iwdev, iwcq);
1218 error:
1219         kfree(iwcq);
1220         return ERR_PTR(err_code);
1221 }
1222
1223 /**
1224  * i40iw_get_user_access - get hw access from IB access
1225  * @acc: IB access to return hw access
1226  */
1227 static inline u16 i40iw_get_user_access(int acc)
1228 {
1229         u16 access = 0;
1230
1231         access |= (acc & IB_ACCESS_LOCAL_WRITE) ? I40IW_ACCESS_FLAGS_LOCALWRITE : 0;
1232         access |= (acc & IB_ACCESS_REMOTE_WRITE) ? I40IW_ACCESS_FLAGS_REMOTEWRITE : 0;
1233         access |= (acc & IB_ACCESS_REMOTE_READ) ? I40IW_ACCESS_FLAGS_REMOTEREAD : 0;
1234         access |= (acc & IB_ACCESS_MW_BIND) ? I40IW_ACCESS_FLAGS_BIND_WINDOW : 0;
1235         return access;
1236 }
1237
1238 /**
1239  * i40iw_free_stag - free stag resource
1240  * @iwdev: iwarp device
1241  * @stag: stag to free
1242  */
1243 static void i40iw_free_stag(struct i40iw_device *iwdev, u32 stag)
1244 {
1245         u32 stag_idx;
1246
1247         stag_idx = (stag & iwdev->mr_stagmask) >> I40IW_CQPSQ_STAG_IDX_SHIFT;
1248         i40iw_free_resource(iwdev, iwdev->allocated_mrs, stag_idx);
1249 }
1250
1251 /**
1252  * i40iw_create_stag - create random stag
1253  * @iwdev: iwarp device
1254  */
1255 static u32 i40iw_create_stag(struct i40iw_device *iwdev)
1256 {
1257         u32 stag = 0;
1258         u32 stag_index = 0;
1259         u32 next_stag_index;
1260         u32 driver_key;
1261         u32 random;
1262         u8 consumer_key;
1263         int ret;
1264
1265         get_random_bytes(&random, sizeof(random));
1266         consumer_key = (u8)random;
1267
1268         driver_key = random & ~iwdev->mr_stagmask;
1269         next_stag_index = (random & iwdev->mr_stagmask) >> 8;
1270         next_stag_index %= iwdev->max_mr;
1271
1272         ret = i40iw_alloc_resource(iwdev,
1273                                    iwdev->allocated_mrs, iwdev->max_mr,
1274                                    &stag_index, &next_stag_index);
1275         if (!ret) {
1276                 stag = stag_index << I40IW_CQPSQ_STAG_IDX_SHIFT;
1277                 stag |= driver_key;
1278                 stag += (u32)consumer_key;
1279         }
1280         return stag;
1281 }
1282
1283 /**
1284  * i40iw_next_pbl_addr - Get next pbl address
1285  * @palloc: Poiner to allocated pbles
1286  * @pbl: pointer to a pble
1287  * @pinfo: info pointer
1288  * @idx: index
1289  */
1290 static inline u64 *i40iw_next_pbl_addr(struct i40iw_pble_alloc *palloc,
1291                                        u64 *pbl,
1292                                        struct i40iw_pble_info **pinfo,
1293                                        u32 *idx)
1294 {
1295         *idx += 1;
1296         if ((!(*pinfo)) || (*idx != (*pinfo)->cnt))
1297                 return ++pbl;
1298         *idx = 0;
1299         (*pinfo)++;
1300         return (u64 *)(*pinfo)->addr;
1301 }
1302
1303 /**
1304  * i40iw_copy_user_pgaddrs - copy user page address to pble's os locally
1305  * @iwmr: iwmr for IB's user page addresses
1306  * @pbl: ple pointer to save 1 level or 0 level pble
1307  * @level: indicated level 0, 1 or 2
1308  */
1309 static void i40iw_copy_user_pgaddrs(struct i40iw_mr *iwmr,
1310                                     u64 *pbl,
1311                                     enum i40iw_pble_level level)
1312 {
1313         struct ib_umem *region = iwmr->region;
1314         struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
1315         int chunk_pages, entry, pg_shift, i;
1316         struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
1317         struct i40iw_pble_info *pinfo;
1318         struct scatterlist *sg;
1319         u32 idx = 0;
1320
1321         pinfo = (level == I40IW_LEVEL_1) ? NULL : palloc->level2.leaf;
1322         pg_shift = ffs(region->page_size) - 1;
1323         for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
1324                 chunk_pages = sg_dma_len(sg) >> pg_shift;
1325                 if ((iwmr->type == IW_MEMREG_TYPE_QP) &&
1326                     !iwpbl->qp_mr.sq_page)
1327                         iwpbl->qp_mr.sq_page = sg_page(sg);
1328                 for (i = 0; i < chunk_pages; i++) {
1329                         *pbl = cpu_to_le64(sg_dma_address(sg) + region->page_size * i);
1330                         pbl = i40iw_next_pbl_addr(palloc, pbl, &pinfo, &idx);
1331                 }
1332         }
1333 }
1334
1335 /**
1336  * i40iw_setup_pbles - copy user pg address to pble's
1337  * @iwdev: iwarp device
1338  * @iwmr: mr pointer for this memory registration
1339  * @use_pbles: flag if to use pble's or memory (level 0)
1340  */
1341 static int i40iw_setup_pbles(struct i40iw_device *iwdev,
1342                              struct i40iw_mr *iwmr,
1343                              bool use_pbles)
1344 {
1345         struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
1346         struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
1347         struct i40iw_pble_info *pinfo;
1348         u64 *pbl;
1349         enum i40iw_status_code status;
1350         enum i40iw_pble_level level = I40IW_LEVEL_1;
1351
1352         if (!use_pbles && (iwmr->page_cnt > MAX_SAVE_PAGE_ADDRS))
1353                 return -ENOMEM;
1354
1355         if (use_pbles) {
1356                 mutex_lock(&iwdev->pbl_mutex);
1357                 status = i40iw_get_pble(&iwdev->sc_dev, iwdev->pble_rsrc, palloc, iwmr->page_cnt);
1358                 mutex_unlock(&iwdev->pbl_mutex);
1359                 if (status)
1360                         return -ENOMEM;
1361
1362                 iwpbl->pbl_allocated = true;
1363                 level = palloc->level;
1364                 pinfo = (level == I40IW_LEVEL_1) ? &palloc->level1 : palloc->level2.leaf;
1365                 pbl = (u64 *)pinfo->addr;
1366         } else {
1367                 pbl = iwmr->pgaddrmem;
1368         }
1369
1370         i40iw_copy_user_pgaddrs(iwmr, pbl, level);
1371         return 0;
1372 }
1373
1374 /**
1375  * i40iw_handle_q_mem - handle memory for qp and cq
1376  * @iwdev: iwarp device
1377  * @req: information for q memory management
1378  * @iwpbl: pble struct
1379  * @use_pbles: flag to use pble
1380  */
1381 static int i40iw_handle_q_mem(struct i40iw_device *iwdev,
1382                               struct i40iw_mem_reg_req *req,
1383                               struct i40iw_pbl *iwpbl,
1384                               bool use_pbles)
1385 {
1386         struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
1387         struct i40iw_mr *iwmr = iwpbl->iwmr;
1388         struct i40iw_qp_mr *qpmr = &iwpbl->qp_mr;
1389         struct i40iw_cq_mr *cqmr = &iwpbl->cq_mr;
1390         struct i40iw_hmc_pble *hmc_p;
1391         u64 *arr = iwmr->pgaddrmem;
1392         int err;
1393         int total;
1394
1395         total = req->sq_pages + req->rq_pages + req->cq_pages;
1396
1397         err = i40iw_setup_pbles(iwdev, iwmr, use_pbles);
1398         if (err)
1399                 return err;
1400         if (use_pbles && (palloc->level != I40IW_LEVEL_1)) {
1401                 i40iw_free_pble(iwdev->pble_rsrc, palloc);
1402                 iwpbl->pbl_allocated = false;
1403                 return -ENOMEM;
1404         }
1405
1406         if (use_pbles)
1407                 arr = (u64 *)palloc->level1.addr;
1408         if (req->reg_type == IW_MEMREG_TYPE_QP) {
1409                 hmc_p = &qpmr->sq_pbl;
1410                 qpmr->shadow = (dma_addr_t)arr[total];
1411                 if (use_pbles) {
1412                         hmc_p->idx = palloc->level1.idx;
1413                         hmc_p = &qpmr->rq_pbl;
1414                         hmc_p->idx = palloc->level1.idx + req->sq_pages;
1415                 } else {
1416                         hmc_p->addr = arr[0];
1417                         hmc_p = &qpmr->rq_pbl;
1418                         hmc_p->addr = arr[1];
1419                 }
1420         } else {                /* CQ */
1421                 hmc_p = &cqmr->cq_pbl;
1422                 cqmr->shadow = (dma_addr_t)arr[total];
1423                 if (use_pbles)
1424                         hmc_p->idx = palloc->level1.idx;
1425                 else
1426                         hmc_p->addr = arr[0];
1427         }
1428         return err;
1429 }
1430
1431 /**
1432  * i40iw_hw_alloc_stag - cqp command to allocate stag
1433  * @iwdev: iwarp device
1434  * @iwmr: iwarp mr pointer
1435  */
1436 static int i40iw_hw_alloc_stag(struct i40iw_device *iwdev, struct i40iw_mr *iwmr)
1437 {
1438         struct i40iw_allocate_stag_info *info;
1439         struct i40iw_pd *iwpd = to_iwpd(iwmr->ibmr.pd);
1440         enum i40iw_status_code status;
1441         int err = 0;
1442         struct i40iw_cqp_request *cqp_request;
1443         struct cqp_commands_info *cqp_info;
1444
1445         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1446         if (!cqp_request)
1447                 return -ENOMEM;
1448
1449         cqp_info = &cqp_request->info;
1450         info = &cqp_info->in.u.alloc_stag.info;
1451         memset(info, 0, sizeof(*info));
1452         info->page_size = PAGE_SIZE;
1453         info->stag_idx = iwmr->stag >> I40IW_CQPSQ_STAG_IDX_SHIFT;
1454         info->pd_id = iwpd->sc_pd.pd_id;
1455         info->total_len = iwmr->length;
1456         info->remote_access = true;
1457         cqp_info->cqp_cmd = OP_ALLOC_STAG;
1458         cqp_info->post_sq = 1;
1459         cqp_info->in.u.alloc_stag.dev = &iwdev->sc_dev;
1460         cqp_info->in.u.alloc_stag.scratch = (uintptr_t)cqp_request;
1461
1462         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1463         if (status) {
1464                 err = -ENOMEM;
1465                 i40iw_pr_err("CQP-OP MR Reg fail");
1466         }
1467         return err;
1468 }
1469
1470 /**
1471  * i40iw_alloc_mr - register stag for fast memory registration
1472  * @pd: ibpd pointer
1473  * @mr_type: memory for stag registrion
1474  * @max_num_sg: man number of pages
1475  */
1476 static struct ib_mr *i40iw_alloc_mr(struct ib_pd *pd,
1477                                     enum ib_mr_type mr_type,
1478                                     u32 max_num_sg)
1479 {
1480         struct i40iw_pd *iwpd = to_iwpd(pd);
1481         struct i40iw_device *iwdev = to_iwdev(pd->device);
1482         struct i40iw_pble_alloc *palloc;
1483         struct i40iw_pbl *iwpbl;
1484         struct i40iw_mr *iwmr;
1485         enum i40iw_status_code status;
1486         u32 stag;
1487         int err_code = -ENOMEM;
1488
1489         iwmr = kzalloc(sizeof(*iwmr), GFP_KERNEL);
1490         if (!iwmr)
1491                 return ERR_PTR(-ENOMEM);
1492
1493         stag = i40iw_create_stag(iwdev);
1494         if (!stag) {
1495                 err_code = -EOVERFLOW;
1496                 goto err;
1497         }
1498         stag &= ~I40IW_CQPSQ_STAG_KEY_MASK;
1499         iwmr->stag = stag;
1500         iwmr->ibmr.rkey = stag;
1501         iwmr->ibmr.lkey = stag;
1502         iwmr->ibmr.pd = pd;
1503         iwmr->ibmr.device = pd->device;
1504         iwpbl = &iwmr->iwpbl;
1505         iwpbl->iwmr = iwmr;
1506         iwmr->type = IW_MEMREG_TYPE_MEM;
1507         palloc = &iwpbl->pble_alloc;
1508         iwmr->page_cnt = max_num_sg;
1509         mutex_lock(&iwdev->pbl_mutex);
1510         status = i40iw_get_pble(&iwdev->sc_dev, iwdev->pble_rsrc, palloc, iwmr->page_cnt);
1511         mutex_unlock(&iwdev->pbl_mutex);
1512         if (status)
1513                 goto err1;
1514
1515         if (palloc->level != I40IW_LEVEL_1)
1516                 goto err2;
1517         err_code = i40iw_hw_alloc_stag(iwdev, iwmr);
1518         if (err_code)
1519                 goto err2;
1520         iwpbl->pbl_allocated = true;
1521         i40iw_add_pdusecount(iwpd);
1522         return &iwmr->ibmr;
1523 err2:
1524         i40iw_free_pble(iwdev->pble_rsrc, palloc);
1525 err1:
1526         i40iw_free_stag(iwdev, stag);
1527 err:
1528         kfree(iwmr);
1529         return ERR_PTR(err_code);
1530 }
1531
1532 /**
1533  * i40iw_set_page - populate pbl list for fmr
1534  * @ibmr: ib mem to access iwarp mr pointer
1535  * @addr: page dma address fro pbl list
1536  */
1537 static int i40iw_set_page(struct ib_mr *ibmr, u64 addr)
1538 {
1539         struct i40iw_mr *iwmr = to_iwmr(ibmr);
1540         struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
1541         struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
1542         u64 *pbl;
1543
1544         if (unlikely(iwmr->npages == iwmr->page_cnt))
1545                 return -ENOMEM;
1546
1547         pbl = (u64 *)palloc->level1.addr;
1548         pbl[iwmr->npages++] = cpu_to_le64(addr);
1549         return 0;
1550 }
1551
1552 /**
1553  * i40iw_map_mr_sg - map of sg list for fmr
1554  * @ibmr: ib mem to access iwarp mr pointer
1555  * @sg: scatter gather list for fmr
1556  * @sg_nents: number of sg pages
1557  */
1558 static int i40iw_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
1559                            int sg_nents, unsigned int *sg_offset)
1560 {
1561         struct i40iw_mr *iwmr = to_iwmr(ibmr);
1562
1563         iwmr->npages = 0;
1564         return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, i40iw_set_page);
1565 }
1566
1567 /**
1568  * i40iw_drain_sq - drain the send queue
1569  * @ibqp: ib qp pointer
1570  */
1571 static void i40iw_drain_sq(struct ib_qp *ibqp)
1572 {
1573         struct i40iw_qp *iwqp = to_iwqp(ibqp);
1574         struct i40iw_sc_qp *qp = &iwqp->sc_qp;
1575
1576         if (I40IW_RING_MORE_WORK(qp->qp_uk.sq_ring))
1577                 wait_for_completion(&iwqp->sq_drained);
1578 }
1579
1580 /**
1581  * i40iw_drain_rq - drain the receive queue
1582  * @ibqp: ib qp pointer
1583  */
1584 static void i40iw_drain_rq(struct ib_qp *ibqp)
1585 {
1586         struct i40iw_qp *iwqp = to_iwqp(ibqp);
1587         struct i40iw_sc_qp *qp = &iwqp->sc_qp;
1588
1589         if (I40IW_RING_MORE_WORK(qp->qp_uk.rq_ring))
1590                 wait_for_completion(&iwqp->rq_drained);
1591 }
1592
1593 /**
1594  * i40iw_hwreg_mr - send cqp command for memory registration
1595  * @iwdev: iwarp device
1596  * @iwmr: iwarp mr pointer
1597  * @access: access for MR
1598  */
1599 static int i40iw_hwreg_mr(struct i40iw_device *iwdev,
1600                           struct i40iw_mr *iwmr,
1601                           u16 access)
1602 {
1603         struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
1604         struct i40iw_reg_ns_stag_info *stag_info;
1605         struct i40iw_pd *iwpd = to_iwpd(iwmr->ibmr.pd);
1606         struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
1607         enum i40iw_status_code status;
1608         int err = 0;
1609         struct i40iw_cqp_request *cqp_request;
1610         struct cqp_commands_info *cqp_info;
1611
1612         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1613         if (!cqp_request)
1614                 return -ENOMEM;
1615
1616         cqp_info = &cqp_request->info;
1617         stag_info = &cqp_info->in.u.mr_reg_non_shared.info;
1618         memset(stag_info, 0, sizeof(*stag_info));
1619         stag_info->va = (void *)(unsigned long)iwpbl->user_base;
1620         stag_info->stag_idx = iwmr->stag >> I40IW_CQPSQ_STAG_IDX_SHIFT;
1621         stag_info->stag_key = (u8)iwmr->stag;
1622         stag_info->total_len = iwmr->length;
1623         stag_info->access_rights = access;
1624         stag_info->pd_id = iwpd->sc_pd.pd_id;
1625         stag_info->addr_type = I40IW_ADDR_TYPE_VA_BASED;
1626
1627         if (iwmr->page_cnt > 1) {
1628                 if (palloc->level == I40IW_LEVEL_1) {
1629                         stag_info->first_pm_pbl_index = palloc->level1.idx;
1630                         stag_info->chunk_size = 1;
1631                 } else {
1632                         stag_info->first_pm_pbl_index = palloc->level2.root.idx;
1633                         stag_info->chunk_size = 3;
1634                 }
1635         } else {
1636                 stag_info->reg_addr_pa = iwmr->pgaddrmem[0];
1637         }
1638
1639         cqp_info->cqp_cmd = OP_MR_REG_NON_SHARED;
1640         cqp_info->post_sq = 1;
1641         cqp_info->in.u.mr_reg_non_shared.dev = &iwdev->sc_dev;
1642         cqp_info->in.u.mr_reg_non_shared.scratch = (uintptr_t)cqp_request;
1643
1644         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1645         if (status) {
1646                 err = -ENOMEM;
1647                 i40iw_pr_err("CQP-OP MR Reg fail");
1648         }
1649         return err;
1650 }
1651
1652 /**
1653  * i40iw_reg_user_mr - Register a user memory region
1654  * @pd: ptr of pd
1655  * @start: virtual start address
1656  * @length: length of mr
1657  * @virt: virtual address
1658  * @acc: access of mr
1659  * @udata: user data
1660  */
1661 static struct ib_mr *i40iw_reg_user_mr(struct ib_pd *pd,
1662                                        u64 start,
1663                                        u64 length,
1664                                        u64 virt,
1665                                        int acc,
1666                                        struct ib_udata *udata)
1667 {
1668         struct i40iw_pd *iwpd = to_iwpd(pd);
1669         struct i40iw_device *iwdev = to_iwdev(pd->device);
1670         struct i40iw_ucontext *ucontext;
1671         struct i40iw_pble_alloc *palloc;
1672         struct i40iw_pbl *iwpbl;
1673         struct i40iw_mr *iwmr;
1674         struct ib_umem *region;
1675         struct i40iw_mem_reg_req req;
1676         u64 pbl_depth = 0;
1677         u32 stag = 0;
1678         u16 access;
1679         u64 region_length;
1680         bool use_pbles = false;
1681         unsigned long flags;
1682         int err = -ENOSYS;
1683
1684         if (length > I40IW_MAX_MR_SIZE)
1685                 return ERR_PTR(-EINVAL);
1686         region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
1687         if (IS_ERR(region))
1688                 return (struct ib_mr *)region;
1689
1690         if (ib_copy_from_udata(&req, udata, sizeof(req))) {
1691                 ib_umem_release(region);
1692                 return ERR_PTR(-EFAULT);
1693         }
1694
1695         iwmr = kzalloc(sizeof(*iwmr), GFP_KERNEL);
1696         if (!iwmr) {
1697                 ib_umem_release(region);
1698                 return ERR_PTR(-ENOMEM);
1699         }
1700
1701         iwpbl = &iwmr->iwpbl;
1702         iwpbl->iwmr = iwmr;
1703         iwmr->region = region;
1704         iwmr->ibmr.pd = pd;
1705         iwmr->ibmr.device = pd->device;
1706         ucontext = to_ucontext(pd->uobject->context);
1707         region_length = region->length + (start & 0xfff);
1708         pbl_depth = region_length >> 12;
1709         pbl_depth += (region_length & (4096 - 1)) ? 1 : 0;
1710         iwmr->length = region->length;
1711
1712         iwpbl->user_base = virt;
1713         palloc = &iwpbl->pble_alloc;
1714
1715         iwmr->type = req.reg_type;
1716         iwmr->page_cnt = (u32)pbl_depth;
1717
1718         switch (req.reg_type) {
1719         case IW_MEMREG_TYPE_QP:
1720                 use_pbles = ((req.sq_pages + req.rq_pages) > 2);
1721                 err = i40iw_handle_q_mem(iwdev, &req, iwpbl, use_pbles);
1722                 if (err)
1723                         goto error;
1724                 spin_lock_irqsave(&ucontext->qp_reg_mem_list_lock, flags);
1725                 list_add_tail(&iwpbl->list, &ucontext->qp_reg_mem_list);
1726                 spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
1727                 break;
1728         case IW_MEMREG_TYPE_CQ:
1729                 use_pbles = (req.cq_pages > 1);
1730                 err = i40iw_handle_q_mem(iwdev, &req, iwpbl, use_pbles);
1731                 if (err)
1732                         goto error;
1733
1734                 spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
1735                 list_add_tail(&iwpbl->list, &ucontext->cq_reg_mem_list);
1736                 spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
1737                 break;
1738         case IW_MEMREG_TYPE_MEM:
1739                 access = I40IW_ACCESS_FLAGS_LOCALREAD;
1740
1741                 use_pbles = (iwmr->page_cnt != 1);
1742                 err = i40iw_setup_pbles(iwdev, iwmr, use_pbles);
1743                 if (err)
1744                         goto error;
1745
1746                 access |= i40iw_get_user_access(acc);
1747                 stag = i40iw_create_stag(iwdev);
1748                 if (!stag) {
1749                         err = -ENOMEM;
1750                         goto error;
1751                 }
1752
1753                 iwmr->stag = stag;
1754                 iwmr->ibmr.rkey = stag;
1755                 iwmr->ibmr.lkey = stag;
1756
1757                 err = i40iw_hwreg_mr(iwdev, iwmr, access);
1758                 if (err) {
1759                         i40iw_free_stag(iwdev, stag);
1760                         goto error;
1761                 }
1762                 break;
1763         default:
1764                 goto error;
1765         }
1766
1767         iwmr->type = req.reg_type;
1768         if (req.reg_type == IW_MEMREG_TYPE_MEM)
1769                 i40iw_add_pdusecount(iwpd);
1770         return &iwmr->ibmr;
1771
1772 error:
1773         if (palloc->level != I40IW_LEVEL_0)
1774                 i40iw_free_pble(iwdev->pble_rsrc, palloc);
1775         ib_umem_release(region);
1776         kfree(iwmr);
1777         return ERR_PTR(err);
1778 }
1779
1780 /**
1781  * i40iw_reg_phys_mr - register kernel physical memory
1782  * @pd: ibpd pointer
1783  * @addr: physical address of memory to register
1784  * @size: size of memory to register
1785  * @acc: Access rights
1786  * @iova_start: start of virtual address for physical buffers
1787  */
1788 struct ib_mr *i40iw_reg_phys_mr(struct ib_pd *pd,
1789                                 u64 addr,
1790                                 u64 size,
1791                                 int acc,
1792                                 u64 *iova_start)
1793 {
1794         struct i40iw_pd *iwpd = to_iwpd(pd);
1795         struct i40iw_device *iwdev = to_iwdev(pd->device);
1796         struct i40iw_pbl *iwpbl;
1797         struct i40iw_mr *iwmr;
1798         enum i40iw_status_code status;
1799         u32 stag;
1800         u16 access = I40IW_ACCESS_FLAGS_LOCALREAD;
1801         int ret;
1802
1803         iwmr = kzalloc(sizeof(*iwmr), GFP_KERNEL);
1804         if (!iwmr)
1805                 return ERR_PTR(-ENOMEM);
1806         iwmr->ibmr.pd = pd;
1807         iwmr->ibmr.device = pd->device;
1808         iwpbl = &iwmr->iwpbl;
1809         iwpbl->iwmr = iwmr;
1810         iwmr->type = IW_MEMREG_TYPE_MEM;
1811         iwpbl->user_base = *iova_start;
1812         stag = i40iw_create_stag(iwdev);
1813         if (!stag) {
1814                 ret = -EOVERFLOW;
1815                 goto err;
1816         }
1817         access |= i40iw_get_user_access(acc);
1818         iwmr->stag = stag;
1819         iwmr->ibmr.rkey = stag;
1820         iwmr->ibmr.lkey = stag;
1821         iwmr->page_cnt = 1;
1822         iwmr->pgaddrmem[0]  = addr;
1823         iwmr->length = size;
1824         status = i40iw_hwreg_mr(iwdev, iwmr, access);
1825         if (status) {
1826                 i40iw_free_stag(iwdev, stag);
1827                 ret = -ENOMEM;
1828                 goto err;
1829         }
1830
1831         i40iw_add_pdusecount(iwpd);
1832         return &iwmr->ibmr;
1833  err:
1834         kfree(iwmr);
1835         return ERR_PTR(ret);
1836 }
1837
1838 /**
1839  * i40iw_get_dma_mr - register physical mem
1840  * @pd: ptr of pd
1841  * @acc: access for memory
1842  */
1843 static struct ib_mr *i40iw_get_dma_mr(struct ib_pd *pd, int acc)
1844 {
1845         u64 kva = 0;
1846
1847         return i40iw_reg_phys_mr(pd, 0, 0, acc, &kva);
1848 }
1849
1850 /**
1851  * i40iw_del_mem_list - Deleting pbl list entries for CQ/QP
1852  * @iwmr: iwmr for IB's user page addresses
1853  * @ucontext: ptr to user context
1854  */
1855 static void i40iw_del_memlist(struct i40iw_mr *iwmr,
1856                               struct i40iw_ucontext *ucontext)
1857 {
1858         struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
1859         unsigned long flags;
1860
1861         switch (iwmr->type) {
1862         case IW_MEMREG_TYPE_CQ:
1863                 spin_lock_irqsave(&ucontext->cq_reg_mem_list_lock, flags);
1864                 if (!list_empty(&ucontext->cq_reg_mem_list))
1865                         list_del(&iwpbl->list);
1866                 spin_unlock_irqrestore(&ucontext->cq_reg_mem_list_lock, flags);
1867                 break;
1868         case IW_MEMREG_TYPE_QP:
1869                 spin_lock_irqsave(&ucontext->qp_reg_mem_list_lock, flags);
1870                 if (!list_empty(&ucontext->qp_reg_mem_list))
1871                         list_del(&iwpbl->list);
1872                 spin_unlock_irqrestore(&ucontext->qp_reg_mem_list_lock, flags);
1873                 break;
1874         default:
1875                 break;
1876         }
1877 }
1878
1879 /**
1880  * i40iw_dereg_mr - deregister mr
1881  * @ib_mr: mr ptr for dereg
1882  */
1883 static int i40iw_dereg_mr(struct ib_mr *ib_mr)
1884 {
1885         struct ib_pd *ibpd = ib_mr->pd;
1886         struct i40iw_pd *iwpd = to_iwpd(ibpd);
1887         struct i40iw_mr *iwmr = to_iwmr(ib_mr);
1888         struct i40iw_device *iwdev = to_iwdev(ib_mr->device);
1889         enum i40iw_status_code status;
1890         struct i40iw_dealloc_stag_info *info;
1891         struct i40iw_pbl *iwpbl = &iwmr->iwpbl;
1892         struct i40iw_pble_alloc *palloc = &iwpbl->pble_alloc;
1893         struct i40iw_cqp_request *cqp_request;
1894         struct cqp_commands_info *cqp_info;
1895         u32 stag_idx;
1896
1897         if (iwmr->region)
1898                 ib_umem_release(iwmr->region);
1899
1900         if (iwmr->type != IW_MEMREG_TYPE_MEM) {
1901                 if (ibpd->uobject) {
1902                         struct i40iw_ucontext *ucontext;
1903
1904                         ucontext = to_ucontext(ibpd->uobject->context);
1905                         i40iw_del_memlist(iwmr, ucontext);
1906                 }
1907                 if (iwpbl->pbl_allocated)
1908                         i40iw_free_pble(iwdev->pble_rsrc, palloc);
1909                 kfree(iwmr);
1910                 return 0;
1911         }
1912
1913         cqp_request = i40iw_get_cqp_request(&iwdev->cqp, true);
1914         if (!cqp_request)
1915                 return -ENOMEM;
1916
1917         cqp_info = &cqp_request->info;
1918         info = &cqp_info->in.u.dealloc_stag.info;
1919         memset(info, 0, sizeof(*info));
1920
1921         info->pd_id = cpu_to_le32(iwpd->sc_pd.pd_id & 0x00007fff);
1922         info->stag_idx = RS_64_1(ib_mr->rkey, I40IW_CQPSQ_STAG_IDX_SHIFT);
1923         stag_idx = info->stag_idx;
1924         info->mr = true;
1925         if (iwpbl->pbl_allocated)
1926                 info->dealloc_pbl = true;
1927
1928         cqp_info->cqp_cmd = OP_DEALLOC_STAG;
1929         cqp_info->post_sq = 1;
1930         cqp_info->in.u.dealloc_stag.dev = &iwdev->sc_dev;
1931         cqp_info->in.u.dealloc_stag.scratch = (uintptr_t)cqp_request;
1932         status = i40iw_handle_cqp_op(iwdev, cqp_request);
1933         if (status)
1934                 i40iw_pr_err("CQP-OP dealloc failed for stag_idx = 0x%x\n", stag_idx);
1935         i40iw_rem_pdusecount(iwpd, iwdev);
1936         i40iw_free_stag(iwdev, iwmr->stag);
1937         if (iwpbl->pbl_allocated)
1938                 i40iw_free_pble(iwdev->pble_rsrc, palloc);
1939         kfree(iwmr);
1940         return 0;
1941 }
1942
1943 /**
1944  * i40iw_show_rev
1945  */
1946 static ssize_t i40iw_show_rev(struct device *dev,
1947                               struct device_attribute *attr, char *buf)
1948 {
1949         struct i40iw_ib_device *iwibdev = container_of(dev,
1950                                                        struct i40iw_ib_device,
1951                                                        ibdev.dev);
1952         u32 hw_rev = iwibdev->iwdev->sc_dev.hw_rev;
1953
1954         return sprintf(buf, "%x\n", hw_rev);
1955 }
1956
1957 /**
1958  * i40iw_show_hca
1959  */
1960 static ssize_t i40iw_show_hca(struct device *dev,
1961                               struct device_attribute *attr, char *buf)
1962 {
1963         return sprintf(buf, "I40IW\n");
1964 }
1965
1966 /**
1967  * i40iw_show_board
1968  */
1969 static ssize_t i40iw_show_board(struct device *dev,
1970                                 struct device_attribute *attr,
1971                                 char *buf)
1972 {
1973         return sprintf(buf, "%.*s\n", 32, "I40IW Board ID");
1974 }
1975
1976 static DEVICE_ATTR(hw_rev, S_IRUGO, i40iw_show_rev, NULL);
1977 static DEVICE_ATTR(hca_type, S_IRUGO, i40iw_show_hca, NULL);
1978 static DEVICE_ATTR(board_id, S_IRUGO, i40iw_show_board, NULL);
1979
1980 static struct device_attribute *i40iw_dev_attributes[] = {
1981         &dev_attr_hw_rev,
1982         &dev_attr_hca_type,
1983         &dev_attr_board_id
1984 };
1985
1986 /**
1987  * i40iw_copy_sg_list - copy sg list for qp
1988  * @sg_list: copied into sg_list
1989  * @sgl: copy from sgl
1990  * @num_sges: count of sg entries
1991  */
1992 static void i40iw_copy_sg_list(struct i40iw_sge *sg_list, struct ib_sge *sgl, int num_sges)
1993 {
1994         unsigned int i;
1995
1996         for (i = 0; (i < num_sges) && (i < I40IW_MAX_WQ_FRAGMENT_COUNT); i++) {
1997                 sg_list[i].tag_off = sgl[i].addr;
1998                 sg_list[i].len = sgl[i].length;
1999                 sg_list[i].stag = sgl[i].lkey;
2000         }
2001 }
2002
2003 /**
2004  * i40iw_post_send -  kernel application wr
2005  * @ibqp: qp ptr for wr
2006  * @ib_wr: work request ptr
2007  * @bad_wr: return of bad wr if err
2008  */
2009 static int i40iw_post_send(struct ib_qp *ibqp,
2010                            struct ib_send_wr *ib_wr,
2011                            struct ib_send_wr **bad_wr)
2012 {
2013         struct i40iw_qp *iwqp;
2014         struct i40iw_qp_uk *ukqp;
2015         struct i40iw_post_sq_info info;
2016         enum i40iw_status_code ret;
2017         int err = 0;
2018         unsigned long flags;
2019         bool inv_stag;
2020
2021         iwqp = (struct i40iw_qp *)ibqp;
2022         ukqp = &iwqp->sc_qp.qp_uk;
2023
2024         spin_lock_irqsave(&iwqp->lock, flags);
2025         while (ib_wr) {
2026                 inv_stag = false;
2027                 memset(&info, 0, sizeof(info));
2028                 info.wr_id = (u64)(ib_wr->wr_id);
2029                 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || iwqp->sig_all)
2030                         info.signaled = true;
2031                 if (ib_wr->send_flags & IB_SEND_FENCE)
2032                         info.read_fence = true;
2033
2034                 switch (ib_wr->opcode) {
2035                 case IB_WR_SEND:
2036                         /* fall-through */
2037                 case IB_WR_SEND_WITH_INV:
2038                         if (ib_wr->opcode == IB_WR_SEND) {
2039                                 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2040                                         info.op_type = I40IW_OP_TYPE_SEND_SOL;
2041                                 else
2042                                         info.op_type = I40IW_OP_TYPE_SEND;
2043                         } else {
2044                                 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2045                                         info.op_type = I40IW_OP_TYPE_SEND_SOL_INV;
2046                                 else
2047                                         info.op_type = I40IW_OP_TYPE_SEND_INV;
2048                         }
2049
2050                         if (ib_wr->send_flags & IB_SEND_INLINE) {
2051                                 info.op.inline_send.data = (void *)(unsigned long)ib_wr->sg_list[0].addr;
2052                                 info.op.inline_send.len = ib_wr->sg_list[0].length;
2053                                 ret = ukqp->ops.iw_inline_send(ukqp, &info, ib_wr->ex.invalidate_rkey, false);
2054                         } else {
2055                                 info.op.send.num_sges = ib_wr->num_sge;
2056                                 info.op.send.sg_list = (struct i40iw_sge *)ib_wr->sg_list;
2057                                 ret = ukqp->ops.iw_send(ukqp, &info, ib_wr->ex.invalidate_rkey, false);
2058                         }
2059
2060                         if (ret) {
2061                                 if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
2062                                         err = -ENOMEM;
2063                                 else
2064                                         err = -EINVAL;
2065                         }
2066                         break;
2067                 case IB_WR_RDMA_WRITE:
2068                         info.op_type = I40IW_OP_TYPE_RDMA_WRITE;
2069
2070                         if (ib_wr->send_flags & IB_SEND_INLINE) {
2071                                 info.op.inline_rdma_write.data = (void *)(unsigned long)ib_wr->sg_list[0].addr;
2072                                 info.op.inline_rdma_write.len = ib_wr->sg_list[0].length;
2073                                 info.op.inline_rdma_write.rem_addr.tag_off = rdma_wr(ib_wr)->remote_addr;
2074                                 info.op.inline_rdma_write.rem_addr.stag = rdma_wr(ib_wr)->rkey;
2075                                 info.op.inline_rdma_write.rem_addr.len = ib_wr->sg_list->length;
2076                                 ret = ukqp->ops.iw_inline_rdma_write(ukqp, &info, false);
2077                         } else {
2078                                 info.op.rdma_write.lo_sg_list = (void *)ib_wr->sg_list;
2079                                 info.op.rdma_write.num_lo_sges = ib_wr->num_sge;
2080                                 info.op.rdma_write.rem_addr.tag_off = rdma_wr(ib_wr)->remote_addr;
2081                                 info.op.rdma_write.rem_addr.stag = rdma_wr(ib_wr)->rkey;
2082                                 info.op.rdma_write.rem_addr.len = ib_wr->sg_list->length;
2083                                 ret = ukqp->ops.iw_rdma_write(ukqp, &info, false);
2084                         }
2085
2086                         if (ret) {
2087                                 if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
2088                                         err = -ENOMEM;
2089                                 else
2090                                         err = -EINVAL;
2091                         }
2092                         break;
2093                 case IB_WR_RDMA_READ_WITH_INV:
2094                         inv_stag = true;
2095                         /* fall-through*/
2096                 case IB_WR_RDMA_READ:
2097                         if (ib_wr->num_sge > I40IW_MAX_SGE_RD) {
2098                                 err = -EINVAL;
2099                                 break;
2100                         }
2101                         info.op_type = I40IW_OP_TYPE_RDMA_READ;
2102                         info.op.rdma_read.rem_addr.tag_off = rdma_wr(ib_wr)->remote_addr;
2103                         info.op.rdma_read.rem_addr.stag = rdma_wr(ib_wr)->rkey;
2104                         info.op.rdma_read.rem_addr.len = ib_wr->sg_list->length;
2105                         info.op.rdma_read.lo_addr.tag_off = ib_wr->sg_list->addr;
2106                         info.op.rdma_read.lo_addr.stag = ib_wr->sg_list->lkey;
2107                         info.op.rdma_read.lo_addr.len = ib_wr->sg_list->length;
2108                         ret = ukqp->ops.iw_rdma_read(ukqp, &info, inv_stag, false);
2109                         if (ret) {
2110                                 if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
2111                                         err = -ENOMEM;
2112                                 else
2113                                         err = -EINVAL;
2114                         }
2115                         break;
2116                 case IB_WR_LOCAL_INV:
2117                         info.op_type = I40IW_OP_TYPE_INV_STAG;
2118                         info.op.inv_local_stag.target_stag = ib_wr->ex.invalidate_rkey;
2119                         ret = ukqp->ops.iw_stag_local_invalidate(ukqp, &info, true);
2120                         if (ret)
2121                                 err = -ENOMEM;
2122                         break;
2123                 case IB_WR_REG_MR:
2124                 {
2125                         struct i40iw_mr *iwmr = to_iwmr(reg_wr(ib_wr)->mr);
2126                         int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
2127                         int flags = reg_wr(ib_wr)->access;
2128                         struct i40iw_pble_alloc *palloc = &iwmr->iwpbl.pble_alloc;
2129                         struct i40iw_sc_dev *dev = &iwqp->iwdev->sc_dev;
2130                         struct i40iw_fast_reg_stag_info info;
2131
2132                         memset(&info, 0, sizeof(info));
2133                         info.access_rights = I40IW_ACCESS_FLAGS_LOCALREAD;
2134                         info.access_rights |= i40iw_get_user_access(flags);
2135                         info.stag_key = reg_wr(ib_wr)->key & 0xff;
2136                         info.stag_idx = reg_wr(ib_wr)->key >> 8;
2137                         info.wr_id = ib_wr->wr_id;
2138
2139                         info.addr_type = I40IW_ADDR_TYPE_VA_BASED;
2140                         info.va = (void *)(uintptr_t)iwmr->ibmr.iova;
2141                         info.total_len = iwmr->ibmr.length;
2142                         info.reg_addr_pa = *(u64 *)palloc->level1.addr;
2143                         info.first_pm_pbl_index = palloc->level1.idx;
2144                         info.local_fence = ib_wr->send_flags & IB_SEND_FENCE;
2145                         info.signaled = ib_wr->send_flags & IB_SEND_SIGNALED;
2146
2147                         if (iwmr->npages > I40IW_MIN_PAGES_PER_FMR)
2148                                 info.chunk_size = 1;
2149
2150                         if (page_shift == 21)
2151                                 info.page_size = 1; /* 2M page */
2152
2153                         ret = dev->iw_priv_qp_ops->iw_mr_fast_register(&iwqp->sc_qp, &info, true);
2154                         if (ret)
2155                                 err = -ENOMEM;
2156                         break;
2157                 }
2158                 default:
2159                         err = -EINVAL;
2160                         i40iw_pr_err(" upost_send bad opcode = 0x%x\n",
2161                                      ib_wr->opcode);
2162                         break;
2163                 }
2164
2165                 if (err)
2166                         break;
2167                 ib_wr = ib_wr->next;
2168         }
2169
2170         if (err)
2171                 *bad_wr = ib_wr;
2172         else
2173                 ukqp->ops.iw_qp_post_wr(ukqp);
2174         spin_unlock_irqrestore(&iwqp->lock, flags);
2175
2176         return err;
2177 }
2178
2179 /**
2180  * i40iw_post_recv - post receive wr for kernel application
2181  * @ibqp: ib qp pointer
2182  * @ib_wr: work request for receive
2183  * @bad_wr: bad wr caused an error
2184  */
2185 static int i40iw_post_recv(struct ib_qp *ibqp,
2186                            struct ib_recv_wr *ib_wr,
2187                            struct ib_recv_wr **bad_wr)
2188 {
2189         struct i40iw_qp *iwqp;
2190         struct i40iw_qp_uk *ukqp;
2191         struct i40iw_post_rq_info post_recv;
2192         struct i40iw_sge sg_list[I40IW_MAX_WQ_FRAGMENT_COUNT];
2193         enum i40iw_status_code ret = 0;
2194         unsigned long flags;
2195         int err = 0;
2196
2197         iwqp = (struct i40iw_qp *)ibqp;
2198         ukqp = &iwqp->sc_qp.qp_uk;
2199
2200         memset(&post_recv, 0, sizeof(post_recv));
2201         spin_lock_irqsave(&iwqp->lock, flags);
2202         while (ib_wr) {
2203                 post_recv.num_sges = ib_wr->num_sge;
2204                 post_recv.wr_id = ib_wr->wr_id;
2205                 i40iw_copy_sg_list(sg_list, ib_wr->sg_list, ib_wr->num_sge);
2206                 post_recv.sg_list = sg_list;
2207                 ret = ukqp->ops.iw_post_receive(ukqp, &post_recv);
2208                 if (ret) {
2209                         i40iw_pr_err(" post_recv err %d\n", ret);
2210                         if (ret == I40IW_ERR_QP_TOOMANY_WRS_POSTED)
2211                                 err = -ENOMEM;
2212                         else
2213                                 err = -EINVAL;
2214                         *bad_wr = ib_wr;
2215                         goto out;
2216                 }
2217                 ib_wr = ib_wr->next;
2218         }
2219  out:
2220         spin_unlock_irqrestore(&iwqp->lock, flags);
2221         return err;
2222 }
2223
2224 /**
2225  * i40iw_poll_cq - poll cq for completion (kernel apps)
2226  * @ibcq: cq to poll
2227  * @num_entries: number of entries to poll
2228  * @entry: wr of entry completed
2229  */
2230 static int i40iw_poll_cq(struct ib_cq *ibcq,
2231                          int num_entries,
2232                          struct ib_wc *entry)
2233 {
2234         struct i40iw_cq *iwcq;
2235         int cqe_count = 0;
2236         struct i40iw_cq_poll_info cq_poll_info;
2237         enum i40iw_status_code ret;
2238         struct i40iw_cq_uk *ukcq;
2239         struct i40iw_sc_qp *qp;
2240         struct i40iw_qp *iwqp;
2241         unsigned long flags;
2242
2243         iwcq = (struct i40iw_cq *)ibcq;
2244         ukcq = &iwcq->sc_cq.cq_uk;
2245
2246         spin_lock_irqsave(&iwcq->lock, flags);
2247         while (cqe_count < num_entries) {
2248                 ret = ukcq->ops.iw_cq_poll_completion(ukcq, &cq_poll_info);
2249                 if (ret == I40IW_ERR_QUEUE_EMPTY) {
2250                         break;
2251                 } else if (ret == I40IW_ERR_QUEUE_DESTROYED) {
2252                         continue;
2253                 } else if (ret) {
2254                         if (!cqe_count)
2255                                 cqe_count = -1;
2256                         break;
2257                 }
2258                 entry->wc_flags = 0;
2259                 entry->wr_id = cq_poll_info.wr_id;
2260                 if (cq_poll_info.error) {
2261                         entry->status = IB_WC_WR_FLUSH_ERR;
2262                         entry->vendor_err = cq_poll_info.major_err << 16 | cq_poll_info.minor_err;
2263                 } else {
2264                         entry->status = IB_WC_SUCCESS;
2265                 }
2266
2267                 switch (cq_poll_info.op_type) {
2268                 case I40IW_OP_TYPE_RDMA_WRITE:
2269                         entry->opcode = IB_WC_RDMA_WRITE;
2270                         break;
2271                 case I40IW_OP_TYPE_RDMA_READ_INV_STAG:
2272                 case I40IW_OP_TYPE_RDMA_READ:
2273                         entry->opcode = IB_WC_RDMA_READ;
2274                         break;
2275                 case I40IW_OP_TYPE_SEND_SOL:
2276                 case I40IW_OP_TYPE_SEND_SOL_INV:
2277                 case I40IW_OP_TYPE_SEND_INV:
2278                 case I40IW_OP_TYPE_SEND:
2279                         entry->opcode = IB_WC_SEND;
2280                         break;
2281                 case I40IW_OP_TYPE_REC:
2282                         entry->opcode = IB_WC_RECV;
2283                         break;
2284                 default:
2285                         entry->opcode = IB_WC_RECV;
2286                         break;
2287                 }
2288
2289                 entry->ex.imm_data = 0;
2290                 qp = (struct i40iw_sc_qp *)cq_poll_info.qp_handle;
2291                 entry->qp = (struct ib_qp *)qp->back_qp;
2292                 entry->src_qp = cq_poll_info.qp_id;
2293                 iwqp = (struct i40iw_qp *)qp->back_qp;
2294                 if (iwqp->iwarp_state > I40IW_QP_STATE_RTS) {
2295                         if (!I40IW_RING_MORE_WORK(qp->qp_uk.sq_ring))
2296                                 complete(&iwqp->sq_drained);
2297                         if (!I40IW_RING_MORE_WORK(qp->qp_uk.rq_ring))
2298                                 complete(&iwqp->rq_drained);
2299                 }
2300                 entry->byte_len = cq_poll_info.bytes_xfered;
2301                 entry++;
2302                 cqe_count++;
2303         }
2304         spin_unlock_irqrestore(&iwcq->lock, flags);
2305         return cqe_count;
2306 }
2307
2308 /**
2309  * i40iw_req_notify_cq - arm cq kernel application
2310  * @ibcq: cq to arm
2311  * @notify_flags: notofication flags
2312  */
2313 static int i40iw_req_notify_cq(struct ib_cq *ibcq,
2314                                enum ib_cq_notify_flags notify_flags)
2315 {
2316         struct i40iw_cq *iwcq;
2317         struct i40iw_cq_uk *ukcq;
2318         unsigned long flags;
2319         enum i40iw_completion_notify cq_notify = IW_CQ_COMPL_EVENT;
2320
2321         iwcq = (struct i40iw_cq *)ibcq;
2322         ukcq = &iwcq->sc_cq.cq_uk;
2323         if (notify_flags == IB_CQ_SOLICITED)
2324                 cq_notify = IW_CQ_COMPL_SOLICITED;
2325         spin_lock_irqsave(&iwcq->lock, flags);
2326         ukcq->ops.iw_cq_request_notification(ukcq, cq_notify);
2327         spin_unlock_irqrestore(&iwcq->lock, flags);
2328         return 0;
2329 }
2330
2331 /**
2332  * i40iw_port_immutable - return port's immutable data
2333  * @ibdev: ib dev struct
2334  * @port_num: port number
2335  * @immutable: immutable data for the port return
2336  */
2337 static int i40iw_port_immutable(struct ib_device *ibdev, u8 port_num,
2338                                 struct ib_port_immutable *immutable)
2339 {
2340         struct ib_port_attr attr;
2341         int err;
2342
2343         err = i40iw_query_port(ibdev, port_num, &attr);
2344
2345         if (err)
2346                 return err;
2347
2348         immutable->pkey_tbl_len = attr.pkey_tbl_len;
2349         immutable->gid_tbl_len = attr.gid_tbl_len;
2350         immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
2351
2352         return 0;
2353 }
2354
2355 static const char * const i40iw_hw_stat_names[] = {
2356         // 32bit names
2357         [I40IW_HW_STAT_INDEX_IP4RXDISCARD] = "ip4InDiscards",
2358         [I40IW_HW_STAT_INDEX_IP4RXTRUNC] = "ip4InTruncatedPkts",
2359         [I40IW_HW_STAT_INDEX_IP4TXNOROUTE] = "ip4OutNoRoutes",
2360         [I40IW_HW_STAT_INDEX_IP6RXDISCARD] = "ip6InDiscards",
2361         [I40IW_HW_STAT_INDEX_IP6RXTRUNC] = "ip6InTruncatedPkts",
2362         [I40IW_HW_STAT_INDEX_IP6TXNOROUTE] = "ip6OutNoRoutes",
2363         [I40IW_HW_STAT_INDEX_TCPRTXSEG] = "tcpRetransSegs",
2364         [I40IW_HW_STAT_INDEX_TCPRXOPTERR] = "tcpInOptErrors",
2365         [I40IW_HW_STAT_INDEX_TCPRXPROTOERR] = "tcpInProtoErrors",
2366         // 64bit names
2367         [I40IW_HW_STAT_INDEX_IP4RXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
2368                 "ip4InOctets",
2369         [I40IW_HW_STAT_INDEX_IP4RXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2370                 "ip4InPkts",
2371         [I40IW_HW_STAT_INDEX_IP4RXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
2372                 "ip4InReasmRqd",
2373         [I40IW_HW_STAT_INDEX_IP4RXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2374                 "ip4InMcastPkts",
2375         [I40IW_HW_STAT_INDEX_IP4TXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
2376                 "ip4OutOctets",
2377         [I40IW_HW_STAT_INDEX_IP4TXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2378                 "ip4OutPkts",
2379         [I40IW_HW_STAT_INDEX_IP4TXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
2380                 "ip4OutSegRqd",
2381         [I40IW_HW_STAT_INDEX_IP4TXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2382                 "ip4OutMcastPkts",
2383         [I40IW_HW_STAT_INDEX_IP6RXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
2384                 "ip6InOctets",
2385         [I40IW_HW_STAT_INDEX_IP6RXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2386                 "ip6InPkts",
2387         [I40IW_HW_STAT_INDEX_IP6RXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
2388                 "ip6InReasmRqd",
2389         [I40IW_HW_STAT_INDEX_IP6RXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2390                 "ip6InMcastPkts",
2391         [I40IW_HW_STAT_INDEX_IP6TXOCTS + I40IW_HW_STAT_INDEX_MAX_32] =
2392                 "ip6OutOctets",
2393         [I40IW_HW_STAT_INDEX_IP6TXPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2394                 "ip6OutPkts",
2395         [I40IW_HW_STAT_INDEX_IP6TXFRAGS + I40IW_HW_STAT_INDEX_MAX_32] =
2396                 "ip6OutSegRqd",
2397         [I40IW_HW_STAT_INDEX_IP6TXMCPKTS + I40IW_HW_STAT_INDEX_MAX_32] =
2398                 "ip6OutMcastPkts",
2399         [I40IW_HW_STAT_INDEX_TCPRXSEGS + I40IW_HW_STAT_INDEX_MAX_32] =
2400                 "tcpInSegs",
2401         [I40IW_HW_STAT_INDEX_TCPTXSEG + I40IW_HW_STAT_INDEX_MAX_32] =
2402                 "tcpOutSegs",
2403         [I40IW_HW_STAT_INDEX_RDMARXRDS + I40IW_HW_STAT_INDEX_MAX_32] =
2404                 "iwInRdmaReads",
2405         [I40IW_HW_STAT_INDEX_RDMARXSNDS + I40IW_HW_STAT_INDEX_MAX_32] =
2406                 "iwInRdmaSends",
2407         [I40IW_HW_STAT_INDEX_RDMARXWRS + I40IW_HW_STAT_INDEX_MAX_32] =
2408                 "iwInRdmaWrites",
2409         [I40IW_HW_STAT_INDEX_RDMATXRDS + I40IW_HW_STAT_INDEX_MAX_32] =
2410                 "iwOutRdmaReads",
2411         [I40IW_HW_STAT_INDEX_RDMATXSNDS + I40IW_HW_STAT_INDEX_MAX_32] =
2412                 "iwOutRdmaSends",
2413         [I40IW_HW_STAT_INDEX_RDMATXWRS + I40IW_HW_STAT_INDEX_MAX_32] =
2414                 "iwOutRdmaWrites",
2415         [I40IW_HW_STAT_INDEX_RDMAVBND + I40IW_HW_STAT_INDEX_MAX_32] =
2416                 "iwRdmaBnd",
2417         [I40IW_HW_STAT_INDEX_RDMAVINV + I40IW_HW_STAT_INDEX_MAX_32] =
2418                 "iwRdmaInv"
2419 };
2420
2421 static void i40iw_get_dev_fw_str(struct ib_device *dev, char *str,
2422                                  size_t str_len)
2423 {
2424         u32 firmware_version = I40IW_FW_VERSION;
2425
2426         snprintf(str, str_len, "%u.%u", firmware_version,
2427                        (firmware_version & 0x000000ff));
2428 }
2429
2430 /**
2431  * i40iw_alloc_hw_stats - Allocate a hw stats structure
2432  * @ibdev: device pointer from stack
2433  * @port_num: port number
2434  */
2435 static struct rdma_hw_stats *i40iw_alloc_hw_stats(struct ib_device *ibdev,
2436                                                   u8 port_num)
2437 {
2438         struct i40iw_device *iwdev = to_iwdev(ibdev);
2439         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
2440         int num_counters = I40IW_HW_STAT_INDEX_MAX_32 +
2441                 I40IW_HW_STAT_INDEX_MAX_64;
2442         unsigned long lifespan = RDMA_HW_STATS_DEFAULT_LIFESPAN;
2443
2444         BUILD_BUG_ON(ARRAY_SIZE(i40iw_hw_stat_names) !=
2445                      (I40IW_HW_STAT_INDEX_MAX_32 +
2446                       I40IW_HW_STAT_INDEX_MAX_64));
2447
2448         /*
2449          * PFs get the default update lifespan, but VFs only update once
2450          * per second
2451          */
2452         if (!dev->is_pf)
2453                 lifespan = 1000;
2454         return rdma_alloc_hw_stats_struct(i40iw_hw_stat_names, num_counters,
2455                                           lifespan);
2456 }
2457
2458 /**
2459  * i40iw_get_hw_stats - Populates the rdma_hw_stats structure
2460  * @ibdev: device pointer from stack
2461  * @stats: stats pointer from stack
2462  * @port_num: port number
2463  * @index: which hw counter the stack is requesting we update
2464  */
2465 static int i40iw_get_hw_stats(struct ib_device *ibdev,
2466                               struct rdma_hw_stats *stats,
2467                               u8 port_num, int index)
2468 {
2469         struct i40iw_device *iwdev = to_iwdev(ibdev);
2470         struct i40iw_sc_dev *dev = &iwdev->sc_dev;
2471         struct i40iw_dev_pestat *devstat = &dev->dev_pestat;
2472         struct i40iw_dev_hw_stats *hw_stats = &devstat->hw_stats;
2473         unsigned long flags;
2474
2475         if (dev->is_pf) {
2476                 spin_lock_irqsave(&devstat->stats_lock, flags);
2477                 devstat->ops.iw_hw_stat_read_all(devstat,
2478                         &devstat->hw_stats);
2479                 spin_unlock_irqrestore(&devstat->stats_lock, flags);
2480         } else {
2481                 if (i40iw_vchnl_vf_get_pe_stats(dev, &devstat->hw_stats))
2482                         return -ENOSYS;
2483         }
2484
2485         memcpy(&stats->value[0], hw_stats, sizeof(*hw_stats));
2486
2487         return stats->num_counters;
2488 }
2489
2490 /**
2491  * i40iw_query_gid - Query port GID
2492  * @ibdev: device pointer from stack
2493  * @port: port number
2494  * @index: Entry index
2495  * @gid: Global ID
2496  */
2497 static int i40iw_query_gid(struct ib_device *ibdev,
2498                            u8 port,
2499                            int index,
2500                            union ib_gid *gid)
2501 {
2502         struct i40iw_device *iwdev = to_iwdev(ibdev);
2503
2504         memset(gid->raw, 0, sizeof(gid->raw));
2505         ether_addr_copy(gid->raw, iwdev->netdev->dev_addr);
2506         return 0;
2507 }
2508
2509 /**
2510  * i40iw_modify_port  Modify port properties
2511  * @ibdev: device pointer from stack
2512  * @port: port number
2513  * @port_modify_mask: mask for port modifications
2514  * @props: port properties
2515  */
2516 static int i40iw_modify_port(struct ib_device *ibdev,
2517                              u8 port,
2518                              int port_modify_mask,
2519                              struct ib_port_modify *props)
2520 {
2521         return -ENOSYS;
2522 }
2523
2524 /**
2525  * i40iw_query_pkey - Query partition key
2526  * @ibdev: device pointer from stack
2527  * @port: port number
2528  * @index: index of pkey
2529  * @pkey: pointer to store the pkey
2530  */
2531 static int i40iw_query_pkey(struct ib_device *ibdev,
2532                             u8 port,
2533                             u16 index,
2534                             u16 *pkey)
2535 {
2536         *pkey = 0;
2537         return 0;
2538 }
2539
2540 /**
2541  * i40iw_create_ah - create address handle
2542  * @ibpd: ptr of pd
2543  * @ah_attr: address handle attributes
2544  */
2545 static struct ib_ah *i40iw_create_ah(struct ib_pd *ibpd,
2546                                      struct ib_ah_attr *attr)
2547 {
2548         return ERR_PTR(-ENOSYS);
2549 }
2550
2551 /**
2552  * i40iw_destroy_ah - Destroy address handle
2553  * @ah: pointer to address handle
2554  */
2555 static int i40iw_destroy_ah(struct ib_ah *ah)
2556 {
2557         return -ENOSYS;
2558 }
2559
2560 /**
2561  * i40iw_init_rdma_device - initialization of iwarp device
2562  * @iwdev: iwarp device
2563  */
2564 static struct i40iw_ib_device *i40iw_init_rdma_device(struct i40iw_device *iwdev)
2565 {
2566         struct i40iw_ib_device *iwibdev;
2567         struct net_device *netdev = iwdev->netdev;
2568         struct pci_dev *pcidev = (struct pci_dev *)iwdev->hw.dev_context;
2569
2570         iwibdev = (struct i40iw_ib_device *)ib_alloc_device(sizeof(*iwibdev));
2571         if (!iwibdev) {
2572                 i40iw_pr_err("iwdev == NULL\n");
2573                 return NULL;
2574         }
2575         strlcpy(iwibdev->ibdev.name, "i40iw%d", IB_DEVICE_NAME_MAX);
2576         iwibdev->ibdev.owner = THIS_MODULE;
2577         iwdev->iwibdev = iwibdev;
2578         iwibdev->iwdev = iwdev;
2579
2580         iwibdev->ibdev.node_type = RDMA_NODE_RNIC;
2581         ether_addr_copy((u8 *)&iwibdev->ibdev.node_guid, netdev->dev_addr);
2582
2583         iwibdev->ibdev.uverbs_cmd_mask =
2584             (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
2585             (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
2586             (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
2587             (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
2588             (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
2589             (1ull << IB_USER_VERBS_CMD_REG_MR) |
2590             (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
2591             (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
2592             (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
2593             (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
2594             (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
2595             (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
2596             (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
2597             (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
2598             (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
2599             (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
2600             (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
2601             (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
2602             (1ull << IB_USER_VERBS_CMD_POST_RECV) |
2603             (1ull << IB_USER_VERBS_CMD_POST_SEND);
2604         iwibdev->ibdev.phys_port_cnt = 1;
2605         iwibdev->ibdev.num_comp_vectors = 1;
2606         iwibdev->ibdev.dma_device = &pcidev->dev;
2607         iwibdev->ibdev.dev.parent = &pcidev->dev;
2608         iwibdev->ibdev.query_port = i40iw_query_port;
2609         iwibdev->ibdev.modify_port = i40iw_modify_port;
2610         iwibdev->ibdev.query_pkey = i40iw_query_pkey;
2611         iwibdev->ibdev.query_gid = i40iw_query_gid;
2612         iwibdev->ibdev.alloc_ucontext = i40iw_alloc_ucontext;
2613         iwibdev->ibdev.dealloc_ucontext = i40iw_dealloc_ucontext;
2614         iwibdev->ibdev.mmap = i40iw_mmap;
2615         iwibdev->ibdev.alloc_pd = i40iw_alloc_pd;
2616         iwibdev->ibdev.dealloc_pd = i40iw_dealloc_pd;
2617         iwibdev->ibdev.create_qp = i40iw_create_qp;
2618         iwibdev->ibdev.modify_qp = i40iw_modify_qp;
2619         iwibdev->ibdev.query_qp = i40iw_query_qp;
2620         iwibdev->ibdev.destroy_qp = i40iw_destroy_qp;
2621         iwibdev->ibdev.create_cq = i40iw_create_cq;
2622         iwibdev->ibdev.destroy_cq = i40iw_destroy_cq;
2623         iwibdev->ibdev.get_dma_mr = i40iw_get_dma_mr;
2624         iwibdev->ibdev.reg_user_mr = i40iw_reg_user_mr;
2625         iwibdev->ibdev.dereg_mr = i40iw_dereg_mr;
2626         iwibdev->ibdev.alloc_hw_stats = i40iw_alloc_hw_stats;
2627         iwibdev->ibdev.get_hw_stats = i40iw_get_hw_stats;
2628         iwibdev->ibdev.query_device = i40iw_query_device;
2629         iwibdev->ibdev.create_ah = i40iw_create_ah;
2630         iwibdev->ibdev.destroy_ah = i40iw_destroy_ah;
2631         iwibdev->ibdev.drain_sq = i40iw_drain_sq;
2632         iwibdev->ibdev.drain_rq = i40iw_drain_rq;
2633         iwibdev->ibdev.alloc_mr = i40iw_alloc_mr;
2634         iwibdev->ibdev.map_mr_sg = i40iw_map_mr_sg;
2635         iwibdev->ibdev.iwcm = kzalloc(sizeof(*iwibdev->ibdev.iwcm), GFP_KERNEL);
2636         if (!iwibdev->ibdev.iwcm) {
2637                 ib_dealloc_device(&iwibdev->ibdev);
2638                 i40iw_pr_err("iwcm == NULL\n");
2639                 return NULL;
2640         }
2641
2642         iwibdev->ibdev.iwcm->add_ref = i40iw_add_ref;
2643         iwibdev->ibdev.iwcm->rem_ref = i40iw_rem_ref;
2644         iwibdev->ibdev.iwcm->get_qp = i40iw_get_qp;
2645         iwibdev->ibdev.iwcm->connect = i40iw_connect;
2646         iwibdev->ibdev.iwcm->accept = i40iw_accept;
2647         iwibdev->ibdev.iwcm->reject = i40iw_reject;
2648         iwibdev->ibdev.iwcm->create_listen = i40iw_create_listen;
2649         iwibdev->ibdev.iwcm->destroy_listen = i40iw_destroy_listen;
2650         memcpy(iwibdev->ibdev.iwcm->ifname, netdev->name,
2651                sizeof(iwibdev->ibdev.iwcm->ifname));
2652         iwibdev->ibdev.get_port_immutable   = i40iw_port_immutable;
2653         iwibdev->ibdev.get_dev_fw_str       = i40iw_get_dev_fw_str;
2654         iwibdev->ibdev.poll_cq = i40iw_poll_cq;
2655         iwibdev->ibdev.req_notify_cq = i40iw_req_notify_cq;
2656         iwibdev->ibdev.post_send = i40iw_post_send;
2657         iwibdev->ibdev.post_recv = i40iw_post_recv;
2658
2659         return iwibdev;
2660 }
2661
2662 /**
2663  * i40iw_port_ibevent - indicate port event
2664  * @iwdev: iwarp device
2665  */
2666 void i40iw_port_ibevent(struct i40iw_device *iwdev)
2667 {
2668         struct i40iw_ib_device *iwibdev = iwdev->iwibdev;
2669         struct ib_event event;
2670
2671         event.device = &iwibdev->ibdev;
2672         event.element.port_num = 1;
2673         event.event = iwdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2674         ib_dispatch_event(&event);
2675 }
2676
2677 /**
2678  * i40iw_unregister_rdma_device - unregister of iwarp from IB
2679  * @iwibdev: rdma device ptr
2680  */
2681 static void i40iw_unregister_rdma_device(struct i40iw_ib_device *iwibdev)
2682 {
2683         int i;
2684
2685         for (i = 0; i < ARRAY_SIZE(i40iw_dev_attributes); ++i)
2686                 device_remove_file(&iwibdev->ibdev.dev,
2687                                    i40iw_dev_attributes[i]);
2688         ib_unregister_device(&iwibdev->ibdev);
2689 }
2690
2691 /**
2692  * i40iw_destroy_rdma_device - destroy rdma device and free resources
2693  * @iwibdev: IB device ptr
2694  */
2695 void i40iw_destroy_rdma_device(struct i40iw_ib_device *iwibdev)
2696 {
2697         if (!iwibdev)
2698                 return;
2699
2700         i40iw_unregister_rdma_device(iwibdev);
2701         kfree(iwibdev->ibdev.iwcm);
2702         iwibdev->ibdev.iwcm = NULL;
2703         ib_dealloc_device(&iwibdev->ibdev);
2704 }
2705
2706 /**
2707  * i40iw_register_rdma_device - register iwarp device to IB
2708  * @iwdev: iwarp device
2709  */
2710 int i40iw_register_rdma_device(struct i40iw_device *iwdev)
2711 {
2712         int i, ret;
2713         struct i40iw_ib_device *iwibdev;
2714
2715         iwdev->iwibdev = i40iw_init_rdma_device(iwdev);
2716         if (!iwdev->iwibdev)
2717                 return -ENOMEM;
2718         iwibdev = iwdev->iwibdev;
2719
2720         ret = ib_register_device(&iwibdev->ibdev, NULL);
2721         if (ret)
2722                 goto error;
2723
2724         for (i = 0; i < ARRAY_SIZE(i40iw_dev_attributes); ++i) {
2725                 ret =
2726                     device_create_file(&iwibdev->ibdev.dev,
2727                                        i40iw_dev_attributes[i]);
2728                 if (ret) {
2729                         while (i > 0) {
2730                                 i--;
2731                                 device_remove_file(&iwibdev->ibdev.dev, i40iw_dev_attributes[i]);
2732                         }
2733                         ib_unregister_device(&iwibdev->ibdev);
2734                         goto error;
2735                 }
2736         }
2737         return 0;
2738 error:
2739         kfree(iwdev->iwibdev->ibdev.iwcm);
2740         iwdev->iwibdev->ibdev.iwcm = NULL;
2741         ib_dealloc_device(&iwdev->iwibdev->ibdev);
2742         return ret;
2743 }