Merge branch 'vmwgfx-fixes-5.1' of git://people.freedesktop.org/~thomash/linux into...
[linux-2.6-microblaze.git] / drivers / infiniband / hw / nes / nes_verbs.c
1 /*
2  * Copyright (c) 2006 - 2011 Intel Corporation.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/random.h>
37 #include <linux/highmem.h>
38 #include <linux/slab.h>
39 #include <asm/byteorder.h>
40
41 #include <rdma/ib_verbs.h>
42 #include <rdma/iw_cm.h>
43 #include <rdma/ib_user_verbs.h>
44 #include <rdma/uverbs_ioctl.h>
45
46 #include "nes.h"
47
48 #include <rdma/ib_umem.h>
49
50 atomic_t mod_qp_timouts;
51 atomic_t qps_created;
52 atomic_t sw_qps_destroyed;
53
54 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
55 static int nes_dereg_mr(struct ib_mr *ib_mr);
56
57 /**
58  * nes_alloc_mw
59  */
60 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd, enum ib_mw_type type,
61                                   struct ib_udata *udata)
62 {
63         struct nes_pd *nespd = to_nespd(ibpd);
64         struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
65         struct nes_device *nesdev = nesvnic->nesdev;
66         struct nes_adapter *nesadapter = nesdev->nesadapter;
67         struct nes_cqp_request *cqp_request;
68         struct nes_mr *nesmr;
69         struct ib_mw *ibmw;
70         struct nes_hw_cqp_wqe *cqp_wqe;
71         int ret;
72         u32 stag;
73         u32 stag_index = 0;
74         u32 next_stag_index = 0;
75         u32 driver_key = 0;
76         u8 stag_key = 0;
77
78         if (type != IB_MW_TYPE_1)
79                 return ERR_PTR(-EINVAL);
80
81         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
82         stag_key = (u8)next_stag_index;
83
84         driver_key = 0;
85
86         next_stag_index >>= 8;
87         next_stag_index %= nesadapter->max_mr;
88
89         ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
90                         nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_MW);
91         if (ret) {
92                 return ERR_PTR(ret);
93         }
94
95         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
96         if (!nesmr) {
97                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
98                 return ERR_PTR(-ENOMEM);
99         }
100
101         stag = stag_index << 8;
102         stag |= driver_key;
103         stag += (u32)stag_key;
104
105         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
106                         stag, stag_index);
107
108         /* Register the region with the adapter */
109         cqp_request = nes_get_cqp_request(nesdev);
110         if (cqp_request == NULL) {
111                 kfree(nesmr);
112                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
113                 return ERR_PTR(-ENOMEM);
114         }
115
116         cqp_request->waiting = 1;
117         cqp_wqe = &cqp_request->cqp_wqe;
118
119         cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
120                         cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
121                         NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
122                         NES_CQP_STAG_REM_ACC_EN);
123
124         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
125         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
126         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
127
128         atomic_set(&cqp_request->refcount, 2);
129         nes_post_cqp_request(nesdev, cqp_request);
130
131         /* Wait for CQP */
132         ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
133                         NES_EVENT_TIMEOUT);
134         nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
135                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
136                         stag, ret, cqp_request->major_code, cqp_request->minor_code);
137         if ((!ret) || (cqp_request->major_code)) {
138                 nes_put_cqp_request(nesdev, cqp_request);
139                 kfree(nesmr);
140                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
141                 if (!ret) {
142                         return ERR_PTR(-ETIME);
143                 } else {
144                         return ERR_PTR(-ENOMEM);
145                 }
146         }
147         nes_put_cqp_request(nesdev, cqp_request);
148
149         nesmr->ibmw.rkey = stag;
150         nesmr->mode = IWNES_MEMREG_TYPE_MW;
151         ibmw = &nesmr->ibmw;
152         nesmr->pbl_4k = 0;
153         nesmr->pbls_used = 0;
154
155         return ibmw;
156 }
157
158
159 /**
160  * nes_dealloc_mw
161  */
162 static int nes_dealloc_mw(struct ib_mw *ibmw)
163 {
164         struct nes_mr *nesmr = to_nesmw(ibmw);
165         struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
166         struct nes_device *nesdev = nesvnic->nesdev;
167         struct nes_adapter *nesadapter = nesdev->nesadapter;
168         struct nes_hw_cqp_wqe *cqp_wqe;
169         struct nes_cqp_request *cqp_request;
170         int err = 0;
171         int ret;
172
173         /* Deallocate the window with the adapter */
174         cqp_request = nes_get_cqp_request(nesdev);
175         if (cqp_request == NULL) {
176                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
177                 return -ENOMEM;
178         }
179         cqp_request->waiting = 1;
180         cqp_wqe = &cqp_request->cqp_wqe;
181         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
182         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
183         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
184
185         atomic_set(&cqp_request->refcount, 2);
186         nes_post_cqp_request(nesdev, cqp_request);
187
188         /* Wait for CQP */
189         nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
190                         ibmw->rkey);
191         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
192                         NES_EVENT_TIMEOUT);
193         nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
194                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
195                         ret, cqp_request->major_code, cqp_request->minor_code);
196         if (!ret)
197                 err = -ETIME;
198         else if (cqp_request->major_code)
199                 err = -EIO;
200
201         nes_put_cqp_request(nesdev, cqp_request);
202
203         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
204                         (ibmw->rkey & 0x0fffff00) >> 8);
205         kfree(nesmr);
206
207         return err;
208 }
209
210
211 /*
212  * nes_alloc_fast_mr
213  */
214 static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
215                              u32 stag, u32 page_count)
216 {
217         struct nes_hw_cqp_wqe *cqp_wqe;
218         struct nes_cqp_request *cqp_request;
219         unsigned long flags;
220         int ret;
221         struct nes_adapter *nesadapter = nesdev->nesadapter;
222         u32 opcode = 0;
223         u16 major_code;
224         u64 region_length = page_count * PAGE_SIZE;
225
226
227         cqp_request = nes_get_cqp_request(nesdev);
228         if (cqp_request == NULL) {
229                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
230                 return -ENOMEM;
231         }
232         nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
233                               "region_length = %llu\n",
234                               page_count, region_length);
235         cqp_request->waiting = 1;
236         cqp_wqe = &cqp_request->cqp_wqe;
237
238         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
239         if (nesadapter->free_4kpbl > 0) {
240                 nesadapter->free_4kpbl--;
241                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
242         } else {
243                 /* No 4kpbl's available: */
244                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
245                 nes_debug(NES_DBG_MR, "Out of Pbls\n");
246                 nes_free_cqp_request(nesdev, cqp_request);
247                 return -ENOMEM;
248         }
249
250         opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
251                  NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
252                  NES_CQP_STAG_REM_ACC_EN;
253         /*
254          * The current OFED API does not support the zero based TO option.
255          * If added then need to changed the NES_CQP_STAG_VA* option.  Also,
256          * the API does not support that ability to have the MR set for local
257          * access only when created and not allow the SQ op to override. Given
258          * this the remote enable must be set here.
259          */
260
261         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
262         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
263         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
264
265         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
266                         cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
267         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
268                         cpu_to_le32(nespd->pd_id & 0x00007fff);
269
270         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
271         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
272         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
273         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
274         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
275         cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
276         barrier();
277
278         atomic_set(&cqp_request->refcount, 2);
279         nes_post_cqp_request(nesdev, cqp_request);
280
281         /* Wait for CQP */
282         ret = wait_event_timeout(cqp_request->waitq,
283                                  (0 != cqp_request->request_done),
284                                  NES_EVENT_TIMEOUT);
285
286         nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
287                   "wait_event_timeout ret = %u, CQP Major:Minor codes = "
288                   "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
289                   cqp_request->minor_code);
290         major_code = cqp_request->major_code;
291         nes_put_cqp_request(nesdev, cqp_request);
292
293         if (!ret || major_code) {
294                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
295                 nesadapter->free_4kpbl++;
296                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
297         }
298
299         if (!ret)
300                 return -ETIME;
301         else if (major_code)
302                 return -EIO;
303         return 0;
304 }
305
306 /*
307  * nes_alloc_mr
308  */
309 static struct ib_mr *nes_alloc_mr(struct ib_pd *ibpd,
310                                   enum ib_mr_type mr_type,
311                                   u32 max_num_sg)
312 {
313         struct nes_pd *nespd = to_nespd(ibpd);
314         struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
315         struct nes_device *nesdev = nesvnic->nesdev;
316         struct nes_adapter *nesadapter = nesdev->nesadapter;
317
318         u32 next_stag_index;
319         u8 stag_key = 0;
320         u32 driver_key = 0;
321         int err = 0;
322         u32 stag_index = 0;
323         struct nes_mr *nesmr;
324         u32 stag;
325         int ret;
326         struct ib_mr *ibmr;
327
328         if (mr_type != IB_MR_TYPE_MEM_REG)
329                 return ERR_PTR(-EINVAL);
330
331         if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
332                 return ERR_PTR(-E2BIG);
333
334 /*
335  * Note:  Set to always use a fixed length single page entry PBL.  This is to allow
336  *       for the fast_reg_mr operation to always know the size of the PBL.
337  */
338         if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
339                 return ERR_PTR(-E2BIG);
340
341         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
342         stag_key = (u8)next_stag_index;
343         next_stag_index >>= 8;
344         next_stag_index %= nesadapter->max_mr;
345
346         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
347                                  nesadapter->max_mr, &stag_index,
348                                  &next_stag_index, NES_RESOURCE_FAST_MR);
349         if (err)
350                 return ERR_PTR(err);
351
352         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
353         if (!nesmr) {
354                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
355                 return ERR_PTR(-ENOMEM);
356         }
357
358         stag = stag_index << 8;
359         stag |= driver_key;
360         stag += (u32)stag_key;
361
362         nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
363                   stag, stag_index);
364
365         ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_num_sg);
366
367         if (ret == 0) {
368                 nesmr->ibmr.rkey = stag;
369                 nesmr->ibmr.lkey = stag;
370                 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
371                 ibmr = &nesmr->ibmr;
372         } else {
373                 kfree(nesmr);
374                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
375                 return ERR_PTR(-ENOMEM);
376         }
377
378         nesmr->pages = pci_alloc_consistent(nesdev->pcidev,
379                                             max_num_sg * sizeof(u64),
380                                             &nesmr->paddr);
381         if (!nesmr->paddr)
382                 goto err;
383
384         nesmr->max_pages = max_num_sg;
385
386         return ibmr;
387
388 err:
389         nes_dereg_mr(ibmr);
390
391         return ERR_PTR(-ENOMEM);
392 }
393
394 static int nes_set_page(struct ib_mr *ibmr, u64 addr)
395 {
396         struct nes_mr *nesmr = to_nesmr(ibmr);
397
398         if (unlikely(nesmr->npages == nesmr->max_pages))
399                 return -ENOMEM;
400
401         nesmr->pages[nesmr->npages++] = cpu_to_le64(addr);
402
403         return 0;
404 }
405
406 static int nes_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
407                          int sg_nents, unsigned int *sg_offset)
408 {
409         struct nes_mr *nesmr = to_nesmr(ibmr);
410
411         nesmr->npages = 0;
412
413         return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, nes_set_page);
414 }
415
416 /**
417  * nes_query_device
418  */
419 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
420                             struct ib_udata *uhw)
421 {
422         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
423         struct nes_device *nesdev = nesvnic->nesdev;
424         struct nes_ib_device *nesibdev = nesvnic->nesibdev;
425
426         if (uhw->inlen || uhw->outlen)
427                 return -EINVAL;
428
429         memset(props, 0, sizeof(*props));
430         memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
431
432         props->fw_ver = nesdev->nesadapter->firmware_version;
433         props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
434         props->vendor_id = nesdev->nesadapter->vendor_id;
435         props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
436         props->hw_ver = nesdev->nesadapter->hw_rev;
437         props->max_mr_size = 0x80000000;
438         props->max_qp = nesibdev->max_qp;
439         props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
440         props->max_send_sge = nesdev->nesadapter->max_sge;
441         props->max_recv_sge = nesdev->nesadapter->max_sge;
442         props->max_cq = nesibdev->max_cq;
443         props->max_cqe = nesdev->nesadapter->max_cqe;
444         props->max_mr = nesibdev->max_mr;
445         props->max_mw = nesibdev->max_mr;
446         props->max_pd = nesibdev->max_pd;
447         props->max_sge_rd = 1;
448         switch (nesdev->nesadapter->max_irrq_wr) {
449                 case 0:
450                         props->max_qp_rd_atom = 2;
451                         break;
452                 case 1:
453                         props->max_qp_rd_atom = 8;
454                         break;
455                 case 2:
456                         props->max_qp_rd_atom = 32;
457                         break;
458                 case 3:
459                         props->max_qp_rd_atom = 64;
460                         break;
461                 default:
462                         props->max_qp_rd_atom = 0;
463         }
464         props->max_qp_init_rd_atom = props->max_qp_rd_atom;
465         props->atomic_cap = IB_ATOMIC_NONE;
466         props->max_map_per_fmr = 1;
467
468         return 0;
469 }
470
471
472 /**
473  * nes_query_port
474  */
475 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
476 {
477         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
478         struct net_device *netdev = nesvnic->netdev;
479
480         /* props being zeroed by the caller, avoid zeroing it here */
481
482         props->max_mtu = IB_MTU_4096;
483         props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
484
485         props->lid = 1;
486         if (netif_queue_stopped(netdev))
487                 props->state = IB_PORT_DOWN;
488         else if (nesvnic->linkup)
489                 props->state = IB_PORT_ACTIVE;
490         else
491                 props->state = IB_PORT_DOWN;
492         props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
493                         IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
494         props->gid_tbl_len = 1;
495         props->pkey_tbl_len = 1;
496         props->active_width = IB_WIDTH_4X;
497         props->active_speed = IB_SPEED_SDR;
498         props->max_msg_sz = 0x80000000;
499
500         return 0;
501 }
502
503 /**
504  * nes_query_pkey
505  */
506 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
507 {
508         *pkey = 0;
509         return 0;
510 }
511
512
513 /**
514  * nes_query_gid
515  */
516 static int nes_query_gid(struct ib_device *ibdev, u8 port,
517                 int index, union ib_gid *gid)
518 {
519         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
520
521         memset(&(gid->raw[0]), 0, sizeof(gid->raw));
522         memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
523
524         return 0;
525 }
526
527
528 /**
529  * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
530  * of all objects associated with a particular user-mode client.
531  */
532 static int nes_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
533 {
534         struct ib_device *ibdev = uctx->device;
535         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
536         struct nes_device *nesdev = nesvnic->nesdev;
537         struct nes_adapter *nesadapter = nesdev->nesadapter;
538         struct nes_alloc_ucontext_req req;
539         struct nes_alloc_ucontext_resp uresp = {};
540         struct nes_ucontext *nes_ucontext = to_nesucontext(uctx);
541         struct nes_ib_device *nesibdev = nesvnic->nesibdev;
542
543
544         if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
545                 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
546                 return -EINVAL;
547         }
548
549         if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
550                 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
551                         req.userspace_ver, NES_ABI_USERSPACE_VER);
552                 return -EINVAL;
553         }
554
555
556         uresp.max_qps = nesibdev->max_qp;
557         uresp.max_pds = nesibdev->max_pd;
558         uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
559         uresp.virtwq = nesadapter->virtwq;
560         uresp.kernel_ver = NES_ABI_KERNEL_VER;
561
562         nes_ucontext->nesdev = nesdev;
563         nes_ucontext->mmap_wq_offset = uresp.max_pds;
564         nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
565                         ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
566                         PAGE_SIZE;
567
568
569         if (ib_copy_to_udata(udata, &uresp, sizeof(uresp)))
570                 return -EFAULT;
571
572         INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
573         INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
574         return 0;
575 }
576
577 /**
578  * nes_dealloc_ucontext
579  */
580 static void nes_dealloc_ucontext(struct ib_ucontext *context)
581 {
582         return;
583 }
584
585 /**
586  * nes_mmap
587  */
588 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
589 {
590         unsigned long index;
591         struct nes_vnic *nesvnic = to_nesvnic(context->device);
592         struct nes_device *nesdev = nesvnic->nesdev;
593         /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
594         struct nes_ucontext *nes_ucontext;
595         struct nes_qp *nesqp;
596
597         nes_ucontext = to_nesucontext(context);
598
599
600         if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
601                 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
602                 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
603                                 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
604                 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
605                         nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
606                         return -EFAULT;
607                 }
608                 nesqp = nes_ucontext->mmap_nesqp[index];
609                 if (nesqp == NULL) {
610                         nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
611                         return -EFAULT;
612                 }
613                 if (remap_pfn_range(vma, vma->vm_start,
614                                 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
615                                 vma->vm_end - vma->vm_start,
616                                 vma->vm_page_prot)) {
617                         nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
618                         return -EAGAIN;
619                 }
620                 vma->vm_private_data = nesqp;
621                 return 0;
622         } else {
623                 index = vma->vm_pgoff;
624                 if (!test_bit(index, nes_ucontext->allocated_doorbells))
625                         return -EFAULT;
626
627                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
628                 if (io_remap_pfn_range(vma, vma->vm_start,
629                                 (nesdev->doorbell_start +
630                                 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
631                                 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
632                         return -EAGAIN;
633                 vma->vm_private_data = nes_ucontext;
634                 return 0;
635         }
636
637         return -ENOSYS;
638 }
639
640
641 /**
642  * nes_alloc_pd
643  */
644 static int nes_alloc_pd(struct ib_pd *pd, struct ib_ucontext *context,
645                         struct ib_udata *udata)
646 {
647         struct ib_device *ibdev = pd->device;
648         struct nes_pd *nespd = to_nespd(pd);
649         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
650         struct nes_device *nesdev = nesvnic->nesdev;
651         struct nes_adapter *nesadapter = nesdev->nesadapter;
652         struct nes_ucontext *nesucontext;
653         struct nes_alloc_pd_resp uresp;
654         u32 pd_num = 0;
655         int err;
656
657         nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
658                         nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
659                         netdev_refcnt_read(nesvnic->netdev));
660
661         err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
662                         nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
663         if (err)
664                 return err;
665
666         nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
667                         nespd, dev_name(&nesvnic->nesibdev->ibdev.dev));
668
669         nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
670
671         if (context) {
672                 nesucontext = to_nesucontext(context);
673                 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
674                                 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
675                 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
676                                 nespd->mmap_db_index, nespd->pd_id);
677                 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
678                         nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
679                         nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
680                         return -ENOMEM;
681                 }
682
683                 uresp.pd_id = nespd->pd_id;
684                 uresp.mmap_db_index = nespd->mmap_db_index;
685                 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
686                         nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
687                         return -EFAULT;
688                 }
689
690                 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
691                 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
692                 nesucontext->first_free_db = nespd->mmap_db_index + 1;
693         }
694
695         nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
696         return 0;
697 }
698
699
700 /**
701  * nes_dealloc_pd
702  */
703 static void nes_dealloc_pd(struct ib_pd *ibpd)
704 {
705         struct nes_ucontext *nesucontext;
706         struct nes_pd *nespd = to_nespd(ibpd);
707         struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
708         struct nes_device *nesdev = nesvnic->nesdev;
709         struct nes_adapter *nesadapter = nesdev->nesadapter;
710
711         if ((ibpd->uobject) && (ibpd->uobject->context)) {
712                 nesucontext = to_nesucontext(ibpd->uobject->context);
713                 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
714                                 nespd->mmap_db_index);
715                 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
716                 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
717                 if (nesucontext->first_free_db > nespd->mmap_db_index) {
718                         nesucontext->first_free_db = nespd->mmap_db_index;
719                 }
720         }
721
722         nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
723                         nespd->pd_id, nespd);
724         nes_free_resource(nesadapter, nesadapter->allocated_pds,
725                         (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
726 }
727
728
729 /**
730  * nes_get_encoded_size
731  */
732 static inline u8 nes_get_encoded_size(int *size)
733 {
734         u8 encoded_size = 0;
735         if (*size <= 32) {
736                 *size = 32;
737                 encoded_size = 1;
738         } else if (*size <= 128) {
739                 *size = 128;
740                 encoded_size = 2;
741         } else if (*size <= 512) {
742                 *size = 512;
743                 encoded_size = 3;
744         }
745         return (encoded_size);
746 }
747
748
749
750 /**
751  * nes_setup_virt_qp
752  */
753 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
754                 struct nes_vnic *nesvnic, int sq_size, int rq_size)
755 {
756         unsigned long flags;
757         void *mem;
758         __le64 *pbl = NULL;
759         __le64 *tpbl;
760         __le64 *pblbuffer;
761         struct nes_device *nesdev = nesvnic->nesdev;
762         struct nes_adapter *nesadapter = nesdev->nesadapter;
763         u32 pbl_entries;
764         u8 rq_pbl_entries;
765         u8 sq_pbl_entries;
766
767         pbl_entries = nespbl->pbl_size >> 3;
768         nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
769                         nespbl->pbl_size, pbl_entries,
770                         (void *)nespbl->pbl_vbase,
771                         (unsigned long) nespbl->pbl_pbase);
772         pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
773         /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
774         /* the first pbl to be fro the rq_vbase... */
775         rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
776         sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
777         nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
778         if (!nespbl->page) {
779                 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
780                 kfree(nespbl);
781                 return -ENOMEM;
782         }
783
784         nesqp->hwqp.sq_vbase = kmap(nespbl->page);
785         nesqp->page = nespbl->page;
786         if (!nesqp->hwqp.sq_vbase) {
787                 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
788                 kfree(nespbl);
789                 return -ENOMEM;
790         }
791
792         /* Now to get to sq.. we need to calculate how many */
793         /* PBL entries were used by the rq.. */
794         pbl += sq_pbl_entries;
795         nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
796         /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
797         /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
798
799         nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
800                   nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
801                   nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
802         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
803         if (!nesadapter->free_256pbl) {
804                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
805                                 nespbl->pbl_pbase);
806                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
807                 kunmap(nesqp->page);
808                 kfree(nespbl);
809                 return -ENOMEM;
810         }
811         nesadapter->free_256pbl--;
812         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
813
814         nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
815         pblbuffer = nesqp->pbl_vbase;
816         if (!nesqp->pbl_vbase) {
817                 /* memory allocated during nes_reg_user_mr() */
818                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
819                                     nespbl->pbl_pbase);
820                 kfree(nespbl);
821                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
822                 nesadapter->free_256pbl++;
823                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
824                 kunmap(nesqp->page);
825                 return -ENOMEM;
826         }
827         memset(nesqp->pbl_vbase, 0, 256);
828         /* fill in the page address in the pbl buffer.. */
829         tpbl = pblbuffer + 16;
830         pbl = (__le64 *)nespbl->pbl_vbase;
831         while (sq_pbl_entries--)
832                 *tpbl++ = *pbl++;
833         tpbl = pblbuffer;
834         while (rq_pbl_entries--)
835                 *tpbl++ = *pbl++;
836
837         /* done with memory allocated during nes_reg_user_mr() */
838         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
839                             nespbl->pbl_pbase);
840         kfree(nespbl);
841
842         nesqp->qp_mem_size =
843                         max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256;     /* this is Q2 */
844         /* Round up to a multiple of a page */
845         nesqp->qp_mem_size += PAGE_SIZE - 1;
846         nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
847
848         mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
849                         &nesqp->hwqp.q2_pbase);
850
851         if (!mem) {
852                 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
853                 nesqp->pbl_vbase = NULL;
854                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
855                 nesadapter->free_256pbl++;
856                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
857                 kunmap(nesqp->page);
858                 return -ENOMEM;
859         }
860         nesqp->sq_kmapped = 1;
861         nesqp->hwqp.q2_vbase = mem;
862         mem += 256;
863         memset(nesqp->hwqp.q2_vbase, 0, 256);
864         nesqp->nesqp_context = mem;
865         memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
866         nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
867
868         return 0;
869 }
870
871
872 /**
873  * nes_setup_mmap_qp
874  */
875 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
876                 int sq_size, int rq_size)
877 {
878         void *mem;
879         struct nes_device *nesdev = nesvnic->nesdev;
880
881         nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
882                         (sizeof(struct nes_hw_qp_wqe) * rq_size) +
883                         max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
884                         256; /* this is Q2 */
885         /* Round up to a multiple of a page */
886         nesqp->qp_mem_size += PAGE_SIZE - 1;
887         nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
888
889         mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
890                         &nesqp->hwqp.sq_pbase);
891         if (!mem)
892                 return -ENOMEM;
893         nes_debug(NES_DBG_QP, "PCI consistent memory for "
894                         "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
895                         mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
896
897         memset(mem, 0, nesqp->qp_mem_size);
898
899         nesqp->hwqp.sq_vbase = mem;
900         mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
901
902         nesqp->hwqp.rq_vbase = mem;
903         nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
904                         sizeof(struct nes_hw_qp_wqe) * sq_size;
905         mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
906
907         nesqp->hwqp.q2_vbase = mem;
908         nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
909                         sizeof(struct nes_hw_qp_wqe) * rq_size;
910         mem += 256;
911         memset(nesqp->hwqp.q2_vbase, 0, 256);
912
913         nesqp->nesqp_context = mem;
914         nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
915         memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
916         return 0;
917 }
918
919
920 /**
921  * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
922  */
923 static void nes_free_qp_mem(struct nes_device *nesdev,
924                 struct nes_qp *nesqp, int virt_wqs)
925 {
926         unsigned long flags;
927         struct nes_adapter *nesadapter = nesdev->nesadapter;
928         if (!virt_wqs) {
929                 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
930                                 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
931         }else {
932                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
933                 nesadapter->free_256pbl++;
934                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
935                 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
936                 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
937                 nesqp->pbl_vbase = NULL;
938                 if (nesqp->sq_kmapped) {
939                         nesqp->sq_kmapped = 0;
940                         kunmap(nesqp->page);
941                 }
942         }
943 }
944
945
946 /**
947  * nes_create_qp
948  */
949 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
950                 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
951 {
952         u64 u64temp= 0;
953         u64 u64nesqp = 0;
954         struct nes_pd *nespd = to_nespd(ibpd);
955         struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
956         struct nes_device *nesdev = nesvnic->nesdev;
957         struct nes_adapter *nesadapter = nesdev->nesadapter;
958         struct nes_qp *nesqp;
959         struct nes_cq *nescq;
960         struct nes_ucontext *nes_ucontext = rdma_udata_to_drv_context(
961                 udata, struct nes_ucontext, ibucontext);
962         struct nes_hw_cqp_wqe *cqp_wqe;
963         struct nes_cqp_request *cqp_request;
964         struct nes_create_qp_req req;
965         struct nes_create_qp_resp uresp;
966         struct nes_pbl  *nespbl = NULL;
967         u32 qp_num = 0;
968         u32 opcode = 0;
969         /* u32 counter = 0; */
970         void *mem;
971         unsigned long flags;
972         int ret;
973         int err;
974         int virt_wqs = 0;
975         int sq_size;
976         int rq_size;
977         u8 sq_encoded_size;
978         u8 rq_encoded_size;
979         /* int counter; */
980
981         if (init_attr->create_flags)
982                 return ERR_PTR(-EINVAL);
983
984         atomic_inc(&qps_created);
985         switch (init_attr->qp_type) {
986                 case IB_QPT_RC:
987                         if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
988                                 init_attr->cap.max_inline_data = 0;
989                         } else {
990                                 init_attr->cap.max_inline_data = 64;
991                         }
992                         sq_size = init_attr->cap.max_send_wr;
993                         rq_size = init_attr->cap.max_recv_wr;
994
995                         /* check if the encoded sizes are OK or not... */
996                         sq_encoded_size = nes_get_encoded_size(&sq_size);
997                         rq_encoded_size = nes_get_encoded_size(&rq_size);
998
999                         if ((!sq_encoded_size) || (!rq_encoded_size)) {
1000                                 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1001                                                 rq_size, sq_size);
1002                                 return ERR_PTR(-EINVAL);
1003                         }
1004
1005                         init_attr->cap.max_send_wr = sq_size -2;
1006                         init_attr->cap.max_recv_wr = rq_size -1;
1007                         nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1008
1009                         ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1010                                         nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
1011                         if (ret) {
1012                                 return ERR_PTR(ret);
1013                         }
1014
1015                         /* Need 512 (actually now 1024) byte alignment on this structure */
1016                         mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1017                         if (!mem) {
1018                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1019                                 return ERR_PTR(-ENOMEM);
1020                         }
1021                         u64nesqp = (unsigned long)mem;
1022                         u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1023                         u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1024                         u64nesqp &= ~u64temp;
1025                         nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1026                         /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p.  Rounded to closest %u\n",
1027                                         nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1028                         nesqp->allocated_buffer = mem;
1029
1030                         if (udata) {
1031                                 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1032                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1033                                         kfree(nesqp->allocated_buffer);
1034                                         nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1035                                         return ERR_PTR(-EFAULT);
1036                                 }
1037                                 if (req.user_wqe_buffers) {
1038                                         virt_wqs = 1;
1039                                 }
1040                                 if (req.user_qp_buffer)
1041                                         nesqp->nesuqp_addr = req.user_qp_buffer;
1042                                 if (udata) {
1043                                         nesqp->user_mode = 1;
1044                                         if (virt_wqs) {
1045                                                 err = 1;
1046                                                 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1047                                                         if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1048                                                                 list_del(&nespbl->list);
1049                                                                 err = 0;
1050                                                                 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1051                                                                           nespbl, nespbl->user_base);
1052                                                                 break;
1053                                                         }
1054                                                 }
1055                                                 if (err) {
1056                                                         nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1057                                                                   (long long unsigned int)req.user_wqe_buffers);
1058                                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1059                                                         kfree(nesqp->allocated_buffer);
1060                                                         return ERR_PTR(-EFAULT);
1061                                                 }
1062                                         }
1063
1064                                         nesqp->mmap_sq_db_index =
1065                                                 find_next_zero_bit(nes_ucontext->allocated_wqs,
1066                                                                    NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1067                                         /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1068                                                         nespd->mmap_db_index); */
1069                                         if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1070                                                 nes_debug(NES_DBG_QP,
1071                                                           "db index > max user regions, failing create QP\n");
1072                                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1073                                                 if (virt_wqs) {
1074                                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1075                                                                             nespbl->pbl_pbase);
1076                                                         kfree(nespbl);
1077                                                 }
1078                                                 kfree(nesqp->allocated_buffer);
1079                                                 return ERR_PTR(-ENOMEM);
1080                                         }
1081                                         set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1082                                         nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1083                                         nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1084                                 } else {
1085                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1086                                         kfree(nesqp->allocated_buffer);
1087                                         return ERR_PTR(-EFAULT);
1088                                 }
1089                         }
1090                         err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1091                                         nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1092                         if (err) {
1093                                 nes_debug(NES_DBG_QP,
1094                                           "error geting qp mem code = %d\n", err);
1095                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1096                                 kfree(nesqp->allocated_buffer);
1097                                 return ERR_PTR(-ENOMEM);
1098                         }
1099
1100                         nesqp->hwqp.sq_size = sq_size;
1101                         nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1102                         nesqp->hwqp.sq_head = 1;
1103                         nesqp->hwqp.rq_size = rq_size;
1104                         nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1105                         /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1106                                         (void *)nesqp->nesqp_context_pbase);
1107                         */
1108                         nesqp->hwqp.qp_id = qp_num;
1109                         nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1110                         nesqp->nespd = nespd;
1111
1112                         nescq = to_nescq(init_attr->send_cq);
1113                         nesqp->nesscq = nescq;
1114                         nescq = to_nescq(init_attr->recv_cq);
1115                         nesqp->nesrcq = nescq;
1116
1117                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1118                                         NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1119                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1120                                         NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1121                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1122                                         NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1123                         if (!udata) {
1124                                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1125                                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1126                         }
1127                         nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1128                                         ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1129                         u64temp = (u64)nesqp->hwqp.sq_pbase;
1130                         nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1131                         nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1132
1133
1134                         if (!virt_wqs) {
1135                                 u64temp = (u64)nesqp->hwqp.sq_pbase;
1136                                 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1137                                 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1138                                 u64temp = (u64)nesqp->hwqp.rq_pbase;
1139                                 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1140                                 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1141                         } else {
1142                                 u64temp = (u64)nesqp->pbl_pbase;
1143                                 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1144                                 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1145                         }
1146
1147                         /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1148                                         nesvnic->next_qp_nic_index,
1149                                         nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1150                         spin_lock_irqsave(&nesdev->cqp.lock, flags);
1151                         nesqp->nesqp_context->misc2 |= cpu_to_le32(
1152                                         (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1153                                         NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1154                         nesvnic->next_qp_nic_index++;
1155                         if ((nesvnic->next_qp_nic_index > 3) ||
1156                                         (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1157                                 nesvnic->next_qp_nic_index = 0;
1158                         }
1159                         spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1160
1161                         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1162                         u64temp = (u64)nesqp->hwqp.q2_pbase;
1163                         nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1164                         nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1165                         nesqp->nesqp_context->aeq_token_low =  cpu_to_le32((u32)((unsigned long)(nesqp)));
1166                         nesqp->nesqp_context->aeq_token_high =  cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1167                         nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1168                                         NES_QPCONTEXT_ORDIRD_AAH |
1169                                         ((((u32)nesadapter->max_irrq_wr) <<
1170                                         NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1171                         if (disable_mpa_crc) {
1172                                 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1173                                 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1174                         }
1175
1176
1177                         /* Create the QP */
1178                         cqp_request = nes_get_cqp_request(nesdev);
1179                         if (cqp_request == NULL) {
1180                                 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1181                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1182                                 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1183                                 kfree(nesqp->allocated_buffer);
1184                                 return ERR_PTR(-ENOMEM);
1185                         }
1186                         cqp_request->waiting = 1;
1187                         cqp_wqe = &cqp_request->cqp_wqe;
1188
1189                         if (!virt_wqs) {
1190                                 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1191                                         NES_CQP_QP_IWARP_STATE_IDLE;
1192                         } else {
1193                                 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1194                                         NES_CQP_QP_IWARP_STATE_IDLE;
1195                         }
1196                         opcode |= NES_CQP_QP_CQS_VALID;
1197                         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1198                         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1199                         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1200
1201                         u64temp = (u64)nesqp->nesqp_context_pbase;
1202                         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1203
1204                         atomic_set(&cqp_request->refcount, 2);
1205                         nes_post_cqp_request(nesdev, cqp_request);
1206
1207                         /* Wait for CQP */
1208                         nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1209                                         nesqp->hwqp.qp_id);
1210                         ret = wait_event_timeout(cqp_request->waitq,
1211                                         (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1212                         nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1213                                         " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1214                                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1215                                         nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1216                                         cqp_request->major_code, cqp_request->minor_code);
1217                         if ((!ret) || (cqp_request->major_code)) {
1218                                 nes_put_cqp_request(nesdev, cqp_request);
1219                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1220                                 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1221                                 kfree(nesqp->allocated_buffer);
1222                                 if (!ret) {
1223                                         return ERR_PTR(-ETIME);
1224                                 } else {
1225                                         return ERR_PTR(-EIO);
1226                                 }
1227                         }
1228
1229                         nes_put_cqp_request(nesdev, cqp_request);
1230
1231                         if (udata) {
1232                                 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1233                                 uresp.mmap_rq_db_index = 0;
1234                                 uresp.actual_sq_size = sq_size;
1235                                 uresp.actual_rq_size = rq_size;
1236                                 uresp.qp_id = nesqp->hwqp.qp_id;
1237                                 uresp.nes_drv_opt = nes_drv_opt;
1238                                 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1239                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1240                                         nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1241                                         kfree(nesqp->allocated_buffer);
1242                                         return ERR_PTR(-EFAULT);
1243                                 }
1244                         }
1245
1246                         nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1247                                         nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1248                         spin_lock_init(&nesqp->lock);
1249                         nes_add_ref(&nesqp->ibqp);
1250                         break;
1251                 default:
1252                         nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1253                         return ERR_PTR(-EINVAL);
1254         }
1255         init_completion(&nesqp->sq_drained);
1256         init_completion(&nesqp->rq_drained);
1257
1258         nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1259         timer_setup(&nesqp->terminate_timer, nes_terminate_timeout, 0);
1260
1261         /* update the QP table */
1262         nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1263         nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1264                         netdev_refcnt_read(nesvnic->netdev));
1265
1266         return &nesqp->ibqp;
1267 }
1268
1269 /**
1270  * nes_clean_cq
1271  */
1272 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1273 {
1274         u32 cq_head;
1275         u32 lo;
1276         u32 hi;
1277         u64 u64temp;
1278         unsigned long flags = 0;
1279
1280         spin_lock_irqsave(&nescq->lock, flags);
1281
1282         cq_head = nescq->hw_cq.cq_head;
1283         while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1284                 rmb();
1285                 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1286                 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1287                 u64temp = (((u64)hi) << 32) | ((u64)lo);
1288                 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1289                 if (u64temp == (u64)(unsigned long)nesqp) {
1290                         /* Zero the context value so cqe will be ignored */
1291                         nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1292                         nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1293                 }
1294
1295                 if (++cq_head >= nescq->hw_cq.cq_size)
1296                         cq_head = 0;
1297         }
1298
1299         spin_unlock_irqrestore(&nescq->lock, flags);
1300 }
1301
1302
1303 /**
1304  * nes_destroy_qp
1305  */
1306 static int nes_destroy_qp(struct ib_qp *ibqp)
1307 {
1308         struct nes_qp *nesqp = to_nesqp(ibqp);
1309         struct nes_ucontext *nes_ucontext;
1310         struct ib_qp_attr attr;
1311         struct iw_cm_id *cm_id;
1312         struct iw_cm_event cm_event;
1313         int ret = 0;
1314
1315         atomic_inc(&sw_qps_destroyed);
1316         nesqp->destroyed = 1;
1317
1318         /* Blow away the connection if it exists. */
1319         if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1320                 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1321                 attr.qp_state = IB_QPS_ERR;
1322                 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1323         }
1324
1325         if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1326                         (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1327                 cm_id = nesqp->cm_id;
1328                 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1329                 cm_event.status = -ETIMEDOUT;
1330                 cm_event.local_addr = cm_id->local_addr;
1331                 cm_event.remote_addr = cm_id->remote_addr;
1332                 cm_event.private_data = NULL;
1333                 cm_event.private_data_len = 0;
1334
1335                 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1336                                 "QP%u. cm_id = %p, refcount = %u. \n",
1337                                 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1338
1339                 cm_id->rem_ref(cm_id);
1340                 ret = cm_id->event_handler(cm_id, &cm_event);
1341                 if (ret)
1342                         nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1343         }
1344
1345         if (nesqp->user_mode) {
1346                 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1347                         nes_ucontext = to_nesucontext(ibqp->uobject->context);
1348                         clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1349                         nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1350                         if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1351                                 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1352                         }
1353                 }
1354                 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1355                         nesqp->sq_kmapped = 0;
1356                         kunmap(nesqp->page);
1357                 }
1358         } else {
1359                 /* Clean any pending completions from the cq(s) */
1360                 if (nesqp->nesscq)
1361                         nes_clean_cq(nesqp, nesqp->nesscq);
1362
1363                 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1364                         nes_clean_cq(nesqp, nesqp->nesrcq);
1365         }
1366         nes_rem_ref(&nesqp->ibqp);
1367         return 0;
1368 }
1369
1370
1371 /**
1372  * nes_create_cq
1373  */
1374 static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1375                                    const struct ib_cq_init_attr *attr,
1376                                    struct ib_ucontext *context,
1377                                    struct ib_udata *udata)
1378 {
1379         int entries = attr->cqe;
1380         u64 u64temp;
1381         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1382         struct nes_device *nesdev = nesvnic->nesdev;
1383         struct nes_adapter *nesadapter = nesdev->nesadapter;
1384         struct nes_cq *nescq;
1385         struct nes_ucontext *nes_ucontext = NULL;
1386         struct nes_cqp_request *cqp_request;
1387         void *mem = NULL;
1388         struct nes_hw_cqp_wqe *cqp_wqe;
1389         struct nes_pbl *nespbl = NULL;
1390         struct nes_create_cq_req req;
1391         struct nes_create_cq_resp resp;
1392         u32 cq_num = 0;
1393         u32 opcode = 0;
1394         u32 pbl_entries = 1;
1395         int err;
1396         unsigned long flags;
1397         int ret;
1398
1399         if (attr->flags)
1400                 return ERR_PTR(-EINVAL);
1401
1402         if (entries > nesadapter->max_cqe)
1403                 return ERR_PTR(-EINVAL);
1404
1405         err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1406                         nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
1407         if (err) {
1408                 return ERR_PTR(err);
1409         }
1410
1411         nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1412         if (!nescq) {
1413                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1414                 return ERR_PTR(-ENOMEM);
1415         }
1416
1417         nescq->hw_cq.cq_size = max(entries + 1, 5);
1418         nescq->hw_cq.cq_number = cq_num;
1419         nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1420
1421
1422         if (context) {
1423                 nes_ucontext = to_nesucontext(context);
1424                 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1425                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1426                         kfree(nescq);
1427                         return ERR_PTR(-EFAULT);
1428                 }
1429                 nesvnic->mcrq_ucontext = nes_ucontext;
1430                 nes_ucontext->mcrqf = req.mcrqf;
1431                 if (nes_ucontext->mcrqf) {
1432                         if (nes_ucontext->mcrqf & 0x80000000)
1433                                 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1434                         else if (nes_ucontext->mcrqf & 0x40000000)
1435                                 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1436                         else
1437                                 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1438                         nescq->mcrqf = nes_ucontext->mcrqf;
1439                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1440                 }
1441                 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1442                                 (unsigned long)req.user_cq_buffer, entries);
1443                 err = 1;
1444                 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1445                         if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1446                                 list_del(&nespbl->list);
1447                                 err = 0;
1448                                 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1449                                                 nespbl);
1450                                 break;
1451                         }
1452                 }
1453                 if (err) {
1454                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1455                         kfree(nescq);
1456                         return ERR_PTR(-EFAULT);
1457                 }
1458
1459                 pbl_entries = nespbl->pbl_size >> 3;
1460                 nescq->cq_mem_size = 0;
1461         } else {
1462                 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1463                 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1464                                 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1465
1466                 /* allocate the physical buffer space */
1467                 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1468                                             &nescq->hw_cq.cq_pbase);
1469                 if (!mem) {
1470                         printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1471                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1472                         kfree(nescq);
1473                         return ERR_PTR(-ENOMEM);
1474                 }
1475
1476                 nescq->hw_cq.cq_vbase = mem;
1477                 nescq->hw_cq.cq_head = 0;
1478                 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1479                                 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1480                                 (u32)nescq->hw_cq.cq_pbase);
1481         }
1482
1483         nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1484         spin_lock_init(&nescq->lock);
1485
1486         /* send CreateCQ request to CQP */
1487         cqp_request = nes_get_cqp_request(nesdev);
1488         if (cqp_request == NULL) {
1489                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1490                 if (!context)
1491                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1492                                         nescq->hw_cq.cq_pbase);
1493                 else {
1494                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1495                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1496                         kfree(nespbl);
1497                 }
1498
1499                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1500                 kfree(nescq);
1501                 return ERR_PTR(-ENOMEM);
1502         }
1503         cqp_request->waiting = 1;
1504         cqp_wqe = &cqp_request->cqp_wqe;
1505
1506         opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1507                         NES_CQP_CQ_CHK_OVERFLOW |
1508                         NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1509
1510         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1511
1512         if (pbl_entries != 1) {
1513                 if (pbl_entries > 32) {
1514                         /* use 4k pbl */
1515                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1516                         if (nesadapter->free_4kpbl == 0) {
1517                                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1518                                 nes_free_cqp_request(nesdev, cqp_request);
1519                                 if (!context)
1520                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1521                                                         nescq->hw_cq.cq_pbase);
1522                                 else {
1523                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1524                                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1525                                         kfree(nespbl);
1526                                 }
1527                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1528                                 kfree(nescq);
1529                                 return ERR_PTR(-ENOMEM);
1530                         } else {
1531                                 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1532                                 nescq->virtual_cq = 2;
1533                                 nesadapter->free_4kpbl--;
1534                         }
1535                 } else {
1536                         /* use 256 byte pbl */
1537                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1538                         if (nesadapter->free_256pbl == 0) {
1539                                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1540                                 nes_free_cqp_request(nesdev, cqp_request);
1541                                 if (!context)
1542                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1543                                                         nescq->hw_cq.cq_pbase);
1544                                 else {
1545                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1546                                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1547                                         kfree(nespbl);
1548                                 }
1549                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1550                                 kfree(nescq);
1551                                 return ERR_PTR(-ENOMEM);
1552                         } else {
1553                                 opcode |= NES_CQP_CQ_VIRT;
1554                                 nescq->virtual_cq = 1;
1555                                 nesadapter->free_256pbl--;
1556                         }
1557                 }
1558         }
1559
1560         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1561
1562         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1563         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1564         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1565                         (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1566
1567         if (context) {
1568                 if (pbl_entries != 1)
1569                         u64temp = (u64)nespbl->pbl_pbase;
1570                 else
1571                         u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1572                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1573                                 nes_ucontext->mmap_db_index[0]);
1574         } else {
1575                 u64temp = (u64)nescq->hw_cq.cq_pbase;
1576                 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1577         }
1578         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1579         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1580         u64temp = (u64)(unsigned long)&nescq->hw_cq;
1581         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1582                         cpu_to_le32((u32)(u64temp >> 1));
1583         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1584                         cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1585
1586         atomic_set(&cqp_request->refcount, 2);
1587         nes_post_cqp_request(nesdev, cqp_request);
1588
1589         /* Wait for CQP */
1590         nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1591                         nescq->hw_cq.cq_number);
1592         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1593                         NES_EVENT_TIMEOUT * 2);
1594         nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1595                         nescq->hw_cq.cq_number, ret);
1596         if ((!ret) || (cqp_request->major_code)) {
1597                 nes_put_cqp_request(nesdev, cqp_request);
1598                 if (!context)
1599                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1600                                         nescq->hw_cq.cq_pbase);
1601                 else {
1602                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1603                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1604                         kfree(nespbl);
1605                 }
1606                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1607                 kfree(nescq);
1608                 return ERR_PTR(-EIO);
1609         }
1610         nes_put_cqp_request(nesdev, cqp_request);
1611
1612         if (context) {
1613                 /* free the nespbl */
1614                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1615                                 nespbl->pbl_pbase);
1616                 kfree(nespbl);
1617                 resp.cq_id = nescq->hw_cq.cq_number;
1618                 resp.cq_size = nescq->hw_cq.cq_size;
1619                 resp.mmap_db_index = 0;
1620                 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
1621                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1622                         kfree(nescq);
1623                         return ERR_PTR(-EFAULT);
1624                 }
1625         }
1626
1627         return &nescq->ibcq;
1628 }
1629
1630
1631 /**
1632  * nes_destroy_cq
1633  */
1634 static int nes_destroy_cq(struct ib_cq *ib_cq)
1635 {
1636         struct nes_cq *nescq;
1637         struct nes_device *nesdev;
1638         struct nes_vnic *nesvnic;
1639         struct nes_adapter *nesadapter;
1640         struct nes_hw_cqp_wqe *cqp_wqe;
1641         struct nes_cqp_request *cqp_request;
1642         unsigned long flags;
1643         u32 opcode = 0;
1644         int ret;
1645
1646         if (ib_cq == NULL)
1647                 return 0;
1648
1649         nescq = to_nescq(ib_cq);
1650         nesvnic = to_nesvnic(ib_cq->device);
1651         nesdev = nesvnic->nesdev;
1652         nesadapter = nesdev->nesadapter;
1653
1654         nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1655
1656         /* Send DestroyCQ request to CQP */
1657         cqp_request = nes_get_cqp_request(nesdev);
1658         if (cqp_request == NULL) {
1659                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1660                 return -ENOMEM;
1661         }
1662         cqp_request->waiting = 1;
1663         cqp_wqe = &cqp_request->cqp_wqe;
1664         opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1665         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1666         if (nescq->virtual_cq == 1) {
1667                 nesadapter->free_256pbl++;
1668                 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1669                         printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1670                                         __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1671                 }
1672         } else if (nescq->virtual_cq == 2) {
1673                 nesadapter->free_4kpbl++;
1674                 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1675                         printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1676                                         __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1677                 }
1678                 opcode |= NES_CQP_CQ_4KB_CHUNK;
1679         }
1680
1681         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1682
1683         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1684         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1685         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1686                 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1687         if (!nescq->mcrqf)
1688                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1689
1690         atomic_set(&cqp_request->refcount, 2);
1691         nes_post_cqp_request(nesdev, cqp_request);
1692
1693         /* Wait for CQP */
1694         nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1695                         nescq->hw_cq.cq_number);
1696         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1697                         NES_EVENT_TIMEOUT);
1698         nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1699                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1700                         nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1701                         cqp_request->minor_code);
1702         if (!ret) {
1703                 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1704                                         nescq->hw_cq.cq_number);
1705                 ret = -ETIME;
1706         } else if (cqp_request->major_code) {
1707                 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1708                                         nescq->hw_cq.cq_number);
1709                 ret = -EIO;
1710         } else {
1711                 ret = 0;
1712         }
1713         nes_put_cqp_request(nesdev, cqp_request);
1714
1715         if (nescq->cq_mem_size)
1716                 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1717                                     nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1718         kfree(nescq);
1719
1720         return ret;
1721 }
1722
1723 /**
1724  * root_256
1725  */
1726 static u32 root_256(struct nes_device *nesdev,
1727                     struct nes_root_vpbl *root_vpbl,
1728                     struct nes_root_vpbl *new_root,
1729                     u16 pbl_count_4k)
1730 {
1731         u64 leaf_pbl;
1732         int i, j, k;
1733
1734         if (pbl_count_4k == 1) {
1735                 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1736                                                 512, &new_root->pbl_pbase);
1737
1738                 if (new_root->pbl_vbase == NULL)
1739                         return 0;
1740
1741                 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1742                 for (i = 0; i < 16; i++) {
1743                         new_root->pbl_vbase[i].pa_low =
1744                                 cpu_to_le32((u32)leaf_pbl);
1745                         new_root->pbl_vbase[i].pa_high =
1746                                 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1747                         leaf_pbl += 256;
1748                 }
1749         } else {
1750                 for (i = 3; i >= 0; i--) {
1751                         j = i * 16;
1752                         root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1753                         leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1754                             (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1755                                 << 32);
1756                         for (k = 1; k < 16; k++) {
1757                                 leaf_pbl += 256;
1758                                 root_vpbl->pbl_vbase[j + k].pa_low =
1759                                                 cpu_to_le32((u32)leaf_pbl);
1760                                 root_vpbl->pbl_vbase[j + k].pa_high =
1761                                     cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1762                         }
1763                 }
1764         }
1765
1766         return 1;
1767 }
1768
1769
1770 /**
1771  * nes_reg_mr
1772  */
1773 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1774                 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1775                 dma_addr_t single_buffer, u16 pbl_count_4k,
1776                 u16 residual_page_count_4k, int acc, u64 *iova_start,
1777                 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1778 {
1779         struct nes_hw_cqp_wqe *cqp_wqe;
1780         struct nes_cqp_request *cqp_request;
1781         unsigned long flags;
1782         int ret;
1783         struct nes_adapter *nesadapter = nesdev->nesadapter;
1784         uint pg_cnt = 0;
1785         u16 pbl_count_256 = 0;
1786         u16 pbl_count = 0;
1787         u8  use_256_pbls = 0;
1788         u8  use_4k_pbls = 0;
1789         u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1790         struct nes_root_vpbl new_root = { 0, NULL, NULL };
1791         u32 opcode = 0;
1792         u16 major_code;
1793
1794         /* Register the region with the adapter */
1795         cqp_request = nes_get_cqp_request(nesdev);
1796         if (cqp_request == NULL) {
1797                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1798                 return -ENOMEM;
1799         }
1800         cqp_request->waiting = 1;
1801         cqp_wqe = &cqp_request->cqp_wqe;
1802
1803         if (pbl_count_4k) {
1804                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1805
1806                 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1807                 pbl_count_256 = (pg_cnt + 31) / 32;
1808                 if (pg_cnt <= 32) {
1809                         if (pbl_count_256 <= nesadapter->free_256pbl)
1810                                 use_256_pbls = 1;
1811                         else if (pbl_count_4k <= nesadapter->free_4kpbl)
1812                                 use_4k_pbls = 1;
1813                 } else if (pg_cnt <= 2048) {
1814                         if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1815                             (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1816                                 use_4k_pbls = 1;
1817                         } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1818                                 use_256_pbls = 1;
1819                                 use_two_level = 1;
1820                         } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1821                                 use_4k_pbls = 1;
1822                         }
1823                 } else {
1824                         if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1825                                 use_4k_pbls = 1;
1826                 }
1827
1828                 if (use_256_pbls) {
1829                         pbl_count = pbl_count_256;
1830                         nesadapter->free_256pbl -= pbl_count + use_two_level;
1831                 } else if (use_4k_pbls) {
1832                         pbl_count =  pbl_count_4k;
1833                         nesadapter->free_4kpbl -= pbl_count + use_two_level;
1834                 } else {
1835                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1836                         nes_debug(NES_DBG_MR, "Out of Pbls\n");
1837                         nes_free_cqp_request(nesdev, cqp_request);
1838                         return -ENOMEM;
1839                 }
1840
1841                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1842         }
1843
1844         if (use_256_pbls && use_two_level) {
1845                 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1846                         if (new_root.pbl_pbase != 0)
1847                                 root_vpbl = &new_root;
1848                 } else {
1849                         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1850                         nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1851                         use_256_pbls = 0;
1852
1853                         if (pbl_count_4k == 1)
1854                                 use_two_level = 0;
1855                         pbl_count = pbl_count_4k;
1856
1857                         if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1858                                 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1859                                 use_4k_pbls = 1;
1860                         }
1861                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1862
1863                         if (use_4k_pbls == 0)
1864                                 return -ENOMEM;
1865                 }
1866         }
1867
1868         opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1869                                         NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1870         if (acc & IB_ACCESS_LOCAL_WRITE)
1871                 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1872         if (acc & IB_ACCESS_REMOTE_WRITE)
1873                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1874         if (acc & IB_ACCESS_REMOTE_READ)
1875                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1876         if (acc & IB_ACCESS_MW_BIND)
1877                 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1878
1879         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1880         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1881         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1882         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1883
1884         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1885                         cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1886         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1887                         cpu_to_le32(nespd->pd_id & 0x00007fff);
1888         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1889
1890         if (pbl_count == 0) {
1891                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1892         } else {
1893                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1894                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
1895                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
1896
1897                 if (use_4k_pbls)
1898                         cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1899         }
1900         barrier();
1901
1902         atomic_set(&cqp_request->refcount, 2);
1903         nes_post_cqp_request(nesdev, cqp_request);
1904
1905         /* Wait for CQP */
1906         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1907                         NES_EVENT_TIMEOUT);
1908         nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1909                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1910                         stag, ret, cqp_request->major_code, cqp_request->minor_code);
1911         major_code = cqp_request->major_code;
1912         nes_put_cqp_request(nesdev, cqp_request);
1913
1914         if ((!ret || major_code) && pbl_count != 0) {
1915                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1916                 if (use_256_pbls)
1917                         nesadapter->free_256pbl += pbl_count + use_two_level;
1918                 else if (use_4k_pbls)
1919                         nesadapter->free_4kpbl += pbl_count + use_two_level;
1920                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1921         }
1922         if (new_root.pbl_pbase)
1923                 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1924                                     new_root.pbl_pbase);
1925
1926         if (!ret)
1927                 return -ETIME;
1928         else if (major_code)
1929                 return -EIO;
1930
1931         *actual_pbl_cnt = pbl_count + use_two_level;
1932         *used_4k_pbls = use_4k_pbls;
1933         return 0;
1934 }
1935
1936
1937 /**
1938  * nes_reg_phys_mr
1939  */
1940 struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
1941                 int acc, u64 *iova_start)
1942 {
1943         u64 region_length;
1944         struct nes_pd *nespd = to_nespd(ib_pd);
1945         struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
1946         struct nes_device *nesdev = nesvnic->nesdev;
1947         struct nes_adapter *nesadapter = nesdev->nesadapter;
1948         struct nes_mr *nesmr;
1949         struct ib_mr *ibmr;
1950         struct nes_vpbl vpbl;
1951         struct nes_root_vpbl root_vpbl;
1952         u32 stag;
1953         unsigned long mask;
1954         u32 stag_index = 0;
1955         u32 next_stag_index = 0;
1956         u32 driver_key = 0;
1957         int err = 0;
1958         int ret = 0;
1959         u16 pbl_count = 0;
1960         u8 single_page = 1;
1961         u8 stag_key = 0;
1962
1963         region_length = 0;
1964         vpbl.pbl_vbase = NULL;
1965         root_vpbl.pbl_vbase = NULL;
1966         root_vpbl.pbl_pbase = 0;
1967
1968         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
1969         stag_key = (u8)next_stag_index;
1970
1971         driver_key = 0;
1972
1973         next_stag_index >>= 8;
1974         next_stag_index %= nesadapter->max_mr;
1975
1976         if ((addr ^ *iova_start) & ~PAGE_MASK)
1977                 return ERR_PTR(-EINVAL);
1978
1979         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
1980                         &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
1981         if (err) {
1982                 return ERR_PTR(err);
1983         }
1984
1985         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
1986         if (!nesmr) {
1987                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1988                 return ERR_PTR(-ENOMEM);
1989         }
1990
1991         /* Allocate a 4K buffer for the PBL */
1992         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
1993                         &vpbl.pbl_pbase);
1994         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
1995                         vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
1996         if (!vpbl.pbl_vbase) {
1997                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1998                 ibmr = ERR_PTR(-ENOMEM);
1999                 kfree(nesmr);
2000                 goto reg_phys_err;
2001         }
2002
2003
2004         mask = !size;
2005
2006         if (mask & ~PAGE_MASK) {
2007                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2008                 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2009                 ibmr = ERR_PTR(-EINVAL);
2010                 kfree(nesmr);
2011                 goto reg_phys_err;
2012         }
2013
2014         region_length += size;
2015         vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2016         vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2017
2018         stag = stag_index << 8;
2019         stag |= driver_key;
2020         stag += (u32)stag_key;
2021
2022         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2023                         " length = 0x%016lX, index = 0x%08X\n",
2024                         stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2025
2026         /* Make the leaf PBL the root if only one PBL */
2027         root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2028
2029         if (single_page) {
2030                 pbl_count = 0;
2031         } else {
2032                 pbl_count = 1;
2033         }
2034         ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2035                         addr, pbl_count, 1, acc, iova_start,
2036                         &nesmr->pbls_used, &nesmr->pbl_4k);
2037
2038         if (ret == 0) {
2039                 nesmr->ibmr.rkey = stag;
2040                 nesmr->ibmr.lkey = stag;
2041                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2042                 ibmr = &nesmr->ibmr;
2043         } else {
2044                 kfree(nesmr);
2045                 ibmr = ERR_PTR(-ENOMEM);
2046         }
2047
2048 reg_phys_err:
2049         /* single PBL case */
2050         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2051         return ibmr;
2052 }
2053
2054
2055 /**
2056  * nes_get_dma_mr
2057  */
2058 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2059 {
2060         u64 kva = 0;
2061
2062         nes_debug(NES_DBG_MR, "\n");
2063
2064         return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
2065 }
2066
2067 /**
2068  * nes_reg_user_mr
2069  */
2070 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2071                 u64 virt, int acc, struct ib_udata *udata)
2072 {
2073         u64 iova_start;
2074         __le64 *pbl;
2075         u64 region_length;
2076         dma_addr_t last_dma_addr = 0;
2077         dma_addr_t first_dma_addr = 0;
2078         struct nes_pd *nespd = to_nespd(pd);
2079         struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2080         struct nes_device *nesdev = nesvnic->nesdev;
2081         struct nes_adapter *nesadapter = nesdev->nesadapter;
2082         struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2083         struct sg_dma_page_iter dma_iter;
2084         struct nes_ucontext *nes_ucontext = rdma_udata_to_drv_context(
2085                 udata, struct nes_ucontext, ibucontext);
2086         struct nes_pbl *nespbl;
2087         struct nes_mr *nesmr;
2088         struct ib_umem *region;
2089         struct nes_mem_reg_req req;
2090         struct nes_vpbl vpbl;
2091         struct nes_root_vpbl root_vpbl;
2092         int page_index;
2093         int page_count = 0;
2094         int err, pbl_depth = 0;
2095         int ret;
2096         u32 stag;
2097         u32 stag_index = 0;
2098         u32 next_stag_index;
2099         u32 driver_key;
2100         u32 root_pbl_index = 0;
2101         u32 cur_pbl_index = 0;
2102         u32 skip_pages;
2103         u16 pbl_count;
2104         u8 single_page = 1;
2105         u8 stag_key;
2106
2107         region = ib_umem_get(udata, start, length, acc, 0);
2108         if (IS_ERR(region)) {
2109                 return (struct ib_mr *)region;
2110         }
2111
2112         nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2113                         " offset = %u, page size = %lu.\n",
2114                         (unsigned long int)start, (unsigned long int)virt, (u32)length,
2115                         ib_umem_offset(region), BIT(region->page_shift));
2116
2117         skip_pages = ((u32)ib_umem_offset(region)) >> 12;
2118
2119         if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2120                 ib_umem_release(region);
2121                 return ERR_PTR(-EFAULT);
2122         }
2123         nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2124
2125         switch (req.reg_type) {
2126                 case IWNES_MEMREG_TYPE_MEM:
2127                         pbl_depth = 0;
2128                         region_length = 0;
2129                         vpbl.pbl_vbase = NULL;
2130                         root_vpbl.pbl_vbase = NULL;
2131                         root_vpbl.pbl_pbase = 0;
2132
2133                         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2134                         stag_key = (u8)next_stag_index;
2135
2136                         driver_key = next_stag_index & 0x70000000;
2137
2138                         next_stag_index >>= 8;
2139                         next_stag_index %= nesadapter->max_mr;
2140
2141                         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2142                                         nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
2143                         if (err) {
2144                                 ib_umem_release(region);
2145                                 return ERR_PTR(err);
2146                         }
2147
2148                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2149                         if (!nesmr) {
2150                                 ib_umem_release(region);
2151                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2152                                 return ERR_PTR(-ENOMEM);
2153                         }
2154                         nesmr->region = region;
2155
2156                         for_each_sg_dma_page (region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2157
2158                                 region_length += PAGE_SIZE;
2159                                 region_length -= skip_pages << 12;
2160                                 skip_pages = 0;
2161                                 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
2162                                         goto enough_pages;
2163                                 if ((page_count & 0x01FF) == 0) {
2164                                         if (page_count >= 1024 * 512) {
2165                                                 ib_umem_release(region);
2166                                                 nes_free_resource(nesadapter,
2167                                                                   nesadapter->allocated_mrs, stag_index);
2168                                                 kfree(nesmr);
2169                                                 ibmr = ERR_PTR(-E2BIG);
2170                                                 goto reg_user_mr_err;
2171                                         }
2172                                         if (root_pbl_index == 1) {
2173                                                 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2174                                                                 8192, &root_vpbl.pbl_pbase);
2175                                                 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2176                                                           root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2177                                                 if (!root_vpbl.pbl_vbase) {
2178                                                         ib_umem_release(region);
2179                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2180                                                                             vpbl.pbl_pbase);
2181                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2182                                                                           stag_index);
2183                                                         kfree(nesmr);
2184                                                         ibmr = ERR_PTR(-ENOMEM);
2185                                                         goto reg_user_mr_err;
2186                                                 }
2187                                                 root_vpbl.leaf_vpbl = kcalloc(1024,
2188                                                                               sizeof(*root_vpbl.leaf_vpbl),
2189                                                                               GFP_KERNEL);
2190                                                 if (!root_vpbl.leaf_vpbl) {
2191                                                         ib_umem_release(region);
2192                                                         pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2193                                                                             root_vpbl.pbl_pbase);
2194                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2195                                                                             vpbl.pbl_pbase);
2196                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2197                                                                           stag_index);
2198                                                         kfree(nesmr);
2199                                                         ibmr = ERR_PTR(-ENOMEM);
2200                                                         goto reg_user_mr_err;
2201                                                 }
2202                                                 root_vpbl.pbl_vbase[0].pa_low =
2203                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2204                                                 root_vpbl.pbl_vbase[0].pa_high =
2205                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2206                                                 root_vpbl.leaf_vpbl[0] = vpbl;
2207                                         }
2208                                         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2209                                                         &vpbl.pbl_pbase);
2210                                         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2211                                                   vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2212                                         if (!vpbl.pbl_vbase) {
2213                                                 ib_umem_release(region);
2214                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2215                                                 ibmr = ERR_PTR(-ENOMEM);
2216                                                 kfree(nesmr);
2217                                                 goto reg_user_mr_err;
2218                                         }
2219                                         if (1 <= root_pbl_index) {
2220                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2221                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2222                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2223                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2224                                                 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2225                                         }
2226                                         root_pbl_index++;
2227                                         cur_pbl_index = 0;
2228                                 }
2229                                 if (single_page) {
2230                                         if (page_count != 0) {
2231                                                 if ((last_dma_addr + 4096) != sg_page_iter_dma_address(&dma_iter))
2232                                                         single_page = 0;
2233                                                 last_dma_addr = sg_page_iter_dma_address(&dma_iter);
2234                                         } else {
2235                                                 first_dma_addr = sg_page_iter_dma_address(&dma_iter);
2236                                                 last_dma_addr = first_dma_addr;
2237                                         }
2238                                 }
2239
2240                                 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2241                                                 cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2242                                 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2243                                                 cpu_to_le32((u32)((u64)(sg_page_iter_dma_address(&dma_iter))));
2244                                 cur_pbl_index++;
2245                                 page_count++;
2246                         }
2247
2248 enough_pages:
2249                         nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2250                                         " stag_key=0x%08x\n",
2251                                         stag_index, driver_key, stag_key);
2252                         stag = stag_index << 8;
2253                         stag |= driver_key;
2254                         stag += (u32)stag_key;
2255
2256                         iova_start = virt;
2257                         /* Make the leaf PBL the root if only one PBL */
2258                         if (root_pbl_index == 1) {
2259                                 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2260                         }
2261
2262                         if (single_page) {
2263                                 pbl_count = 0;
2264                         } else {
2265                                 pbl_count = root_pbl_index;
2266                                 first_dma_addr = 0;
2267                         }
2268                         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2269                                         " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2270                                         stag, (unsigned int)iova_start,
2271                                         (unsigned int)region_length, stag_index,
2272                                         (unsigned long long)region->length, pbl_count);
2273                         ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2274                                          first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2275                                          &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2276
2277                         nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2278
2279                         if (ret == 0) {
2280                                 nesmr->ibmr.rkey = stag;
2281                                 nesmr->ibmr.lkey = stag;
2282                                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2283                                 ibmr = &nesmr->ibmr;
2284                         } else {
2285                                 ib_umem_release(region);
2286                                 kfree(nesmr);
2287                                 ibmr = ERR_PTR(-ENOMEM);
2288                         }
2289
2290 reg_user_mr_err:
2291                         /* free the resources */
2292                         if (root_pbl_index == 1) {
2293                                 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2294                                                 vpbl.pbl_pbase);
2295                         } else {
2296                                 for (page_index=0; page_index<root_pbl_index; page_index++) {
2297                                         pci_free_consistent(nesdev->pcidev, 4096,
2298                                                         root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2299                                                         root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2300                                 }
2301                                 kfree(root_vpbl.leaf_vpbl);
2302                                 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2303                                                 root_vpbl.pbl_pbase);
2304                         }
2305
2306                         nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2307
2308                         return ibmr;
2309                 case IWNES_MEMREG_TYPE_QP:
2310                 case IWNES_MEMREG_TYPE_CQ:
2311                         if (!region->length) {
2312                                 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2313                                 ib_umem_release(region);
2314                                 return ERR_PTR(-EINVAL);
2315                         }
2316                         nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2317                         if (!nespbl) {
2318                                 ib_umem_release(region);
2319                                 return ERR_PTR(-ENOMEM);
2320                         }
2321                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2322                         if (!nesmr) {
2323                                 ib_umem_release(region);
2324                                 kfree(nespbl);
2325                                 return ERR_PTR(-ENOMEM);
2326                         }
2327                         nesmr->region = region;
2328                         pbl_depth = region->length >> 12;
2329                         pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2330                         nespbl->pbl_size = pbl_depth*sizeof(u64);
2331                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2332                                 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2333                         } else {
2334                                 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2335                         }
2336
2337                         nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2338                                         nespbl->pbl_size, pbl_depth);
2339                         pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2340                                         &nespbl->pbl_pbase);
2341                         if (!pbl) {
2342                                 ib_umem_release(region);
2343                                 kfree(nesmr);
2344                                 kfree(nespbl);
2345                                 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2346                                 return ERR_PTR(-ENOMEM);
2347                         }
2348
2349                         nespbl->pbl_vbase = (u64 *)pbl;
2350                         nespbl->user_base = start;
2351                         nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2352                                         " pbl_vbase=%p user_base=0x%lx\n",
2353                                   nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2354                                   (void *) nespbl->pbl_vbase, nespbl->user_base);
2355
2356                         nespbl->page = sg_page(region->sg_head.sgl);
2357                         for_each_sg_dma_page(region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2358                                 ((__le32 *)pbl)[0] = cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2359                                 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)(sg_page_iter_dma_address(&dma_iter)))>>32);
2360                                 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2361                                           (unsigned long long)*pbl,
2362                                           le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2363                                 pbl++;
2364                         }
2365
2366                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2367                                 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2368                         } else {
2369                                 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2370                         }
2371                         nesmr->ibmr.rkey = -1;
2372                         nesmr->ibmr.lkey = -1;
2373                         nesmr->mode = req.reg_type;
2374                         return &nesmr->ibmr;
2375         }
2376
2377         ib_umem_release(region);
2378         return ERR_PTR(-ENOSYS);
2379 }
2380
2381
2382 /**
2383  * nes_dereg_mr
2384  */
2385 static int nes_dereg_mr(struct ib_mr *ib_mr)
2386 {
2387         struct nes_mr *nesmr = to_nesmr(ib_mr);
2388         struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2389         struct nes_device *nesdev = nesvnic->nesdev;
2390         struct nes_adapter *nesadapter = nesdev->nesadapter;
2391         struct nes_hw_cqp_wqe *cqp_wqe;
2392         struct nes_cqp_request *cqp_request;
2393         unsigned long flags;
2394         int ret;
2395         u16 major_code;
2396         u16 minor_code;
2397
2398
2399         if (nesmr->pages)
2400                 pci_free_consistent(nesdev->pcidev,
2401                                     nesmr->max_pages * sizeof(u64),
2402                                     nesmr->pages,
2403                                     nesmr->paddr);
2404
2405         if (nesmr->region) {
2406                 ib_umem_release(nesmr->region);
2407         }
2408         if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2409                 kfree(nesmr);
2410                 return 0;
2411         }
2412
2413         /* Deallocate the region with the adapter */
2414
2415         cqp_request = nes_get_cqp_request(nesdev);
2416         if (cqp_request == NULL) {
2417                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2418                 return -ENOMEM;
2419         }
2420         cqp_request->waiting = 1;
2421         cqp_wqe = &cqp_request->cqp_wqe;
2422
2423         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2424         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2425                         NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2426                         NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2427         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2428
2429         atomic_set(&cqp_request->refcount, 2);
2430         nes_post_cqp_request(nesdev, cqp_request);
2431
2432         /* Wait for CQP */
2433         nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2434         ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2435                         NES_EVENT_TIMEOUT);
2436         nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2437                         " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2438                         ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2439
2440         major_code = cqp_request->major_code;
2441         minor_code = cqp_request->minor_code;
2442
2443         nes_put_cqp_request(nesdev, cqp_request);
2444
2445         if (!ret) {
2446                 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2447                                 " ib_mr=%p, rkey = 0x%08X\n",
2448                                 ib_mr, ib_mr->rkey);
2449                 return -ETIME;
2450         } else if (major_code) {
2451                 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2452                                 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2453                                 major_code, minor_code, ib_mr, ib_mr->rkey);
2454                 return -EIO;
2455         }
2456
2457         if (nesmr->pbls_used != 0) {
2458                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2459                 if (nesmr->pbl_4k) {
2460                         nesadapter->free_4kpbl += nesmr->pbls_used;
2461                         if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2462                                 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2463                                         "exceeded the max(%u)\n",
2464                                         nesadapter->free_4kpbl,
2465                                         nesadapter->max_4kpbl);
2466                 } else {
2467                         nesadapter->free_256pbl += nesmr->pbls_used;
2468                         if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2469                                 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2470                                         "exceeded the max(%u)\n",
2471                                         nesadapter->free_256pbl,
2472                                         nesadapter->max_256pbl);
2473                 }
2474                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2475         }
2476         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2477                         (ib_mr->rkey & 0x0fffff00) >> 8);
2478
2479         kfree(nesmr);
2480
2481         return 0;
2482 }
2483
2484
2485 /**
2486  * show_rev
2487  */
2488 static ssize_t hw_rev_show(struct device *dev,
2489                            struct device_attribute *attr, char *buf)
2490 {
2491         struct nes_ib_device *nesibdev =
2492                 rdma_device_to_drv_device(dev, struct nes_ib_device, ibdev);
2493         struct nes_vnic *nesvnic = nesibdev->nesvnic;
2494
2495         nes_debug(NES_DBG_INIT, "\n");
2496         return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2497 }
2498 static DEVICE_ATTR_RO(hw_rev);
2499
2500 /**
2501  * show_hca
2502  */
2503 static ssize_t hca_type_show(struct device *dev,
2504                              struct device_attribute *attr, char *buf)
2505 {
2506         nes_debug(NES_DBG_INIT, "\n");
2507         return sprintf(buf, "NES020\n");
2508 }
2509 static DEVICE_ATTR_RO(hca_type);
2510
2511 /**
2512  * show_board
2513  */
2514 static ssize_t board_id_show(struct device *dev,
2515                              struct device_attribute *attr, char *buf)
2516 {
2517         nes_debug(NES_DBG_INIT, "\n");
2518         return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2519 }
2520 static DEVICE_ATTR_RO(board_id);
2521
2522 static struct attribute *nes_dev_attributes[] = {
2523         &dev_attr_hw_rev.attr,
2524         &dev_attr_hca_type.attr,
2525         &dev_attr_board_id.attr,
2526         NULL
2527 };
2528
2529 static const struct attribute_group nes_attr_group = {
2530         .attrs = nes_dev_attributes,
2531 };
2532
2533 /**
2534  * nes_query_qp
2535  */
2536 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2537                 int attr_mask, struct ib_qp_init_attr *init_attr)
2538 {
2539         struct nes_qp *nesqp = to_nesqp(ibqp);
2540
2541         nes_debug(NES_DBG_QP, "\n");
2542
2543         attr->qp_access_flags = 0;
2544         attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2545         attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2546         attr->cap.max_recv_sge = 1;
2547         if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2548                 attr->cap.max_inline_data = 0;
2549         else
2550                 attr->cap.max_inline_data = 64;
2551
2552         init_attr->event_handler = nesqp->ibqp.event_handler;
2553         init_attr->qp_context = nesqp->ibqp.qp_context;
2554         init_attr->send_cq = nesqp->ibqp.send_cq;
2555         init_attr->recv_cq = nesqp->ibqp.recv_cq;
2556         init_attr->srq = nesqp->ibqp.srq;
2557         init_attr->cap = attr->cap;
2558
2559         return 0;
2560 }
2561
2562
2563 /**
2564  * nes_hw_modify_qp
2565  */
2566 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2567                 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2568 {
2569         struct nes_hw_cqp_wqe *cqp_wqe;
2570         /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2571         /* struct iw_cm_event cm_event; */
2572         struct nes_cqp_request *cqp_request;
2573         int ret;
2574         u16 major_code;
2575
2576         nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2577                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2578
2579         cqp_request = nes_get_cqp_request(nesdev);
2580         if (cqp_request == NULL) {
2581                 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2582                 return -ENOMEM;
2583         }
2584         if (wait_completion) {
2585                 cqp_request->waiting = 1;
2586         } else {
2587                 cqp_request->waiting = 0;
2588         }
2589         cqp_wqe = &cqp_request->cqp_wqe;
2590
2591         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2592                         NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2593         nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2594                         next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2595         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2596         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2597         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2598
2599         /* If sending a terminate message, fill in the length (in words) */
2600         if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2601             !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2602                 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2603                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2604         }
2605
2606         atomic_set(&cqp_request->refcount, 2);
2607         nes_post_cqp_request(nesdev, cqp_request);
2608
2609         /* Wait for CQP */
2610         if (wait_completion) {
2611                 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2612                                 nesqp->hwqp.qp_id); */
2613                 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2614                                 NES_EVENT_TIMEOUT);
2615                 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2616                                 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2617                                 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2618                 major_code = cqp_request->major_code;
2619                 if (major_code) {
2620                         nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2621                                         "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2622                                         nesqp->hwqp.qp_id, cqp_request->major_code,
2623                                         cqp_request->minor_code, next_iwarp_state);
2624                 }
2625
2626                 nes_put_cqp_request(nesdev, cqp_request);
2627
2628                 if (!ret)
2629                         return -ETIME;
2630                 else if (major_code)
2631                         return -EIO;
2632                 else
2633                         return 0;
2634         } else {
2635                 return 0;
2636         }
2637 }
2638
2639
2640 /**
2641  * nes_modify_qp
2642  */
2643 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2644                 int attr_mask, struct ib_udata *udata)
2645 {
2646         struct nes_qp *nesqp = to_nesqp(ibqp);
2647         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2648         struct nes_device *nesdev = nesvnic->nesdev;
2649         /* u32 cqp_head; */
2650         /* u32 counter; */
2651         u32 next_iwarp_state = 0;
2652         int err;
2653         unsigned long qplockflags;
2654         int ret;
2655         u16 original_last_aeq;
2656         u8 issue_modify_qp = 0;
2657         u8 dont_wait = 0;
2658
2659         nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2660                         " iwarp_state=0x%X, refcount=%d\n",
2661                         nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2662                         nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2663
2664         spin_lock_irqsave(&nesqp->lock, qplockflags);
2665
2666         nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2667                         " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2668                         nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2669                         nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2670
2671         if (attr_mask & IB_QP_STATE) {
2672                 switch (attr->qp_state) {
2673                         case IB_QPS_INIT:
2674                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2675                                                 nesqp->hwqp.qp_id);
2676                                 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2677                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2678                                         return -EINVAL;
2679                                 }
2680                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2681                                 issue_modify_qp = 1;
2682                                 break;
2683                         case IB_QPS_RTR:
2684                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2685                                                 nesqp->hwqp.qp_id);
2686                                 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2687                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2688                                         return -EINVAL;
2689                                 }
2690                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2691                                 issue_modify_qp = 1;
2692                                 break;
2693                         case IB_QPS_RTS:
2694                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2695                                                 nesqp->hwqp.qp_id);
2696                                 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2697                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2698                                         return -EINVAL;
2699                                 }
2700                                 if (nesqp->cm_id == NULL) {
2701                                         nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2702                                                         nesqp->hwqp.qp_id );
2703                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2704                                         return -EINVAL;
2705                                 }
2706                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2707                                 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2708                                         next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2709                                                         NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2710                                 issue_modify_qp = 1;
2711                                 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2712                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2713                                 nesqp->hte_added = 1;
2714                                 break;
2715                         case IB_QPS_SQD:
2716                                 issue_modify_qp = 1;
2717                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2718                                                 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2719                                 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2720                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2721                                         return 0;
2722                                 } else {
2723                                         if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2724                                                 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2725                                                                 " ignored due to current iWARP state\n",
2726                                                                 nesqp->hwqp.qp_id);
2727                                                 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2728                                                 return -EINVAL;
2729                                         }
2730                                         if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2731                                                 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2732                                                                 " already done based on hw state.\n",
2733                                                                 nesqp->hwqp.qp_id);
2734                                                 issue_modify_qp = 0;
2735                                         }
2736                                         switch (nesqp->hw_iwarp_state) {
2737                                                 case NES_AEQE_IWARP_STATE_CLOSING:
2738                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2739                                                         break;
2740                                                 case NES_AEQE_IWARP_STATE_TERMINATE:
2741                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2742                                                         break;
2743                                                 case NES_AEQE_IWARP_STATE_ERROR:
2744                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2745                                                         break;
2746                                                 default:
2747                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2748                                                         nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2749                                                         break;
2750                                         }
2751                                 }
2752                                 break;
2753                         case IB_QPS_SQE:
2754                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2755                                                 nesqp->hwqp.qp_id);
2756                                 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2757                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2758                                         return -EINVAL;
2759                                 }
2760                                 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2761                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2762                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2763                                 issue_modify_qp = 1;
2764                                 break;
2765                         case IB_QPS_ERR:
2766                         case IB_QPS_RESET:
2767                                 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2768                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2769                                         return -EINVAL;
2770                                 }
2771                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2772                                                 nesqp->hwqp.qp_id);
2773                                 if (nesqp->term_flags)
2774                                         del_timer(&nesqp->terminate_timer);
2775
2776                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2777                                 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2778                                 if (nesqp->hte_added) {
2779                                         nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2780                                         next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2781                                         nesqp->hte_added = 0;
2782                                 }
2783                                 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
2784                                                 (nesdev->iw_status) &&
2785                                                 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2786                                         next_iwarp_state |= NES_CQP_QP_RESET;
2787                                 } else {
2788                                         nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2789                                                         nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2790                                         dont_wait = 1;
2791                                 }
2792                                 issue_modify_qp = 1;
2793                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2794                                 break;
2795                         default:
2796                                 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2797                                 return -EINVAL;
2798                                 break;
2799                 }
2800
2801                 nesqp->ibqp_state = attr->qp_state;
2802                 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2803                 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2804                                 nesqp->iwarp_state);
2805         }
2806
2807         if (attr_mask & IB_QP_ACCESS_FLAGS) {
2808                 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2809                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2810                                         NES_QPCONTEXT_MISC_RDMA_READ_EN);
2811                         issue_modify_qp = 1;
2812                 }
2813                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2814                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2815                         issue_modify_qp = 1;
2816                 }
2817                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2818                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2819                         issue_modify_qp = 1;
2820                 }
2821                 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2822                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2823                         issue_modify_qp = 1;
2824                 }
2825
2826                 if (nesqp->user_mode) {
2827                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2828                                         NES_QPCONTEXT_MISC_RDMA_READ_EN);
2829                         issue_modify_qp = 1;
2830                 }
2831         }
2832
2833         original_last_aeq = nesqp->last_aeq;
2834         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2835
2836         nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2837
2838         ret = 0;
2839
2840
2841         if (issue_modify_qp) {
2842                 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
2843                 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
2844                 if (ret)
2845                         nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2846                                         " failed for QP%u.\n",
2847                                         next_iwarp_state, nesqp->hwqp.qp_id);
2848
2849         }
2850
2851         if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2852                 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2853                                 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2854                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2855                                 original_last_aeq, nesqp->last_aeq);
2856                 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
2857                         if (dont_wait) {
2858                                 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2859                                         nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2860                                                         " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2861                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2862                                                         original_last_aeq, nesqp->last_aeq);
2863                                         /* this one is for the cm_disconnect thread */
2864                                         spin_lock_irqsave(&nesqp->lock, qplockflags);
2865                                         nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2866                                         nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2867                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2868                                         nes_cm_disconn(nesqp);
2869                                 } else {
2870                                         nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2871                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2872                                 }
2873                         } else {
2874                                 spin_lock_irqsave(&nesqp->lock, qplockflags);
2875                                 if (nesqp->cm_id) {
2876                                         /* These two are for the timer thread */
2877                                         if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
2878                                                 nesqp->cm_id->add_ref(nesqp->cm_id);
2879                                                 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2880                                                                 " need ae to finish up, original_last_aeq = 0x%04X."
2881                                                                 " last_aeq = 0x%04X, scheduling timer.\n",
2882                                                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2883                                                                 original_last_aeq, nesqp->last_aeq);
2884                                                 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
2885                                         }
2886                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2887                                 } else {
2888                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2889                                         nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2890                                                         " need ae to finish up, original_last_aeq = 0x%04X."
2891                                                         " last_aeq = 0x%04X.\n",
2892                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2893                                                         original_last_aeq, nesqp->last_aeq);
2894                                 }
2895                         }
2896                 } else {
2897                         nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2898                                         " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2899                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2900                                         original_last_aeq, nesqp->last_aeq);
2901                 }
2902         } else {
2903                 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2904                                 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2905                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2906                                 original_last_aeq, nesqp->last_aeq);
2907         }
2908
2909         err = 0;
2910
2911         nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
2912                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2913
2914         return err;
2915 }
2916
2917 static inline void
2918 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, const struct ib_send_wr *ib_wr,
2919                  u32 uselkey)
2920 {
2921         int sge_index;
2922         int total_payload_length = 0;
2923         for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
2924                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
2925                         ib_wr->sg_list[sge_index].addr);
2926                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
2927                         ib_wr->sg_list[sge_index].length);
2928                 if (uselkey)
2929                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
2930                                                 (ib_wr->sg_list[sge_index].lkey));
2931                 else
2932                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
2933
2934                 total_payload_length += ib_wr->sg_list[sge_index].length;
2935         }
2936         nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
2937                         total_payload_length);
2938         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
2939                                 total_payload_length);
2940 }
2941
2942 /**
2943  * nes_post_send
2944  */
2945 static int nes_post_send(struct ib_qp *ibqp, const struct ib_send_wr *ib_wr,
2946                          const struct ib_send_wr **bad_wr)
2947 {
2948         u64 u64temp;
2949         unsigned long flags = 0;
2950         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2951         struct nes_device *nesdev = nesvnic->nesdev;
2952         struct nes_qp *nesqp = to_nesqp(ibqp);
2953         struct nes_hw_qp_wqe *wqe;
2954         int err = 0;
2955         u32 qsize = nesqp->hwqp.sq_size;
2956         u32 head;
2957         u32 wqe_misc = 0;
2958         u32 wqe_count = 0;
2959         u32 counter;
2960
2961         if (nesqp->ibqp_state > IB_QPS_RTS) {
2962                 err = -EINVAL;
2963                 goto out;
2964         }
2965
2966         spin_lock_irqsave(&nesqp->lock, flags);
2967
2968         head = nesqp->hwqp.sq_head;
2969
2970         while (ib_wr) {
2971                 /* Check for QP error */
2972                 if (nesqp->term_flags) {
2973                         err = -EINVAL;
2974                         break;
2975                 }
2976
2977                 /* Check for SQ overflow */
2978                 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
2979                         err = -ENOMEM;
2980                         break;
2981                 }
2982
2983                 wqe = &nesqp->hwqp.sq_vbase[head];
2984                 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
2985                                 nesqp->hwqp.qp_id, wqe, head); */
2986                 nes_fill_init_qp_wqe(wqe, nesqp, head);
2987                 u64temp = (u64)(ib_wr->wr_id);
2988                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
2989                                         u64temp);
2990                 switch (ib_wr->opcode) {
2991                 case IB_WR_SEND:
2992                 case IB_WR_SEND_WITH_INV:
2993                         if (IB_WR_SEND == ib_wr->opcode) {
2994                                 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2995                                         wqe_misc = NES_IWARP_SQ_OP_SENDSE;
2996                                 else
2997                                         wqe_misc = NES_IWARP_SQ_OP_SEND;
2998                         } else {
2999                                 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3000                                         wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3001                                 else
3002                                         wqe_misc = NES_IWARP_SQ_OP_SENDINV;
3003
3004                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3005                                                     ib_wr->ex.invalidate_rkey);
3006                         }
3007
3008                         if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3009                                 err = -EINVAL;
3010                                 break;
3011                         }
3012
3013                         if (ib_wr->send_flags & IB_SEND_FENCE)
3014                                 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3015
3016                         if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3017                             ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3018                              (ib_wr->sg_list[0].length <= 64)) {
3019                                 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3020                                        (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3021                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3022                                                     ib_wr->sg_list[0].length);
3023                                 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3024                         } else {
3025                                 fill_wqe_sg_send(wqe, ib_wr, 1);
3026                         }
3027
3028                         break;
3029                 case IB_WR_RDMA_WRITE:
3030                         wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3031                         if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3032                                 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3033                                           ib_wr->num_sge, nesdev->nesadapter->max_sge);
3034                                 err = -EINVAL;
3035                                 break;
3036                         }
3037
3038                         if (ib_wr->send_flags & IB_SEND_FENCE)
3039                                 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3040
3041                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3042                                             rdma_wr(ib_wr)->rkey);
3043                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3044                                             rdma_wr(ib_wr)->remote_addr);
3045
3046                         if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3047                             ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3048                              (ib_wr->sg_list[0].length <= 64)) {
3049                                 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3050                                        (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3051                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3052                                                     ib_wr->sg_list[0].length);
3053                                 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3054                         } else {
3055                                 fill_wqe_sg_send(wqe, ib_wr, 1);
3056                         }
3057
3058                         wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3059                                 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3060                         break;
3061                 case IB_WR_RDMA_READ:
3062                 case IB_WR_RDMA_READ_WITH_INV:
3063                         /* iWARP only supports 1 sge for RDMA reads */
3064                         if (ib_wr->num_sge > 1) {
3065                                 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3066                                           ib_wr->num_sge);
3067                                 err = -EINVAL;
3068                                 break;
3069                         }
3070                         if (ib_wr->opcode == IB_WR_RDMA_READ) {
3071                                 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3072                         } else {
3073                                 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3074                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3075                                                     ib_wr->ex.invalidate_rkey);
3076                         }
3077
3078                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3079                                             rdma_wr(ib_wr)->remote_addr);
3080                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3081                                             rdma_wr(ib_wr)->rkey);
3082                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3083                                             ib_wr->sg_list->length);
3084                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3085                                             ib_wr->sg_list->addr);
3086                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3087                                             ib_wr->sg_list->lkey);
3088                         break;
3089                 case IB_WR_LOCAL_INV:
3090                         wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3091                         set_wqe_32bit_value(wqe->wqe_words,
3092                                             NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3093                                             ib_wr->ex.invalidate_rkey);
3094                         break;
3095                 case IB_WR_REG_MR:
3096                 {
3097                         struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3098                         int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3099                         int flags = reg_wr(ib_wr)->access;
3100
3101                         if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3102                                 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3103                                 err = -EINVAL;
3104                                 break;
3105                         }
3106                         wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3107                         set_wqe_64bit_value(wqe->wqe_words,
3108                                             NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3109                                             mr->ibmr.iova);
3110                         set_wqe_32bit_value(wqe->wqe_words,
3111                                             NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3112                                             lower_32_bits(mr->ibmr.length));
3113                         set_wqe_32bit_value(wqe->wqe_words,
3114                                             NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3115                         set_wqe_32bit_value(wqe->wqe_words,
3116                                             NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3117                                             reg_wr(ib_wr)->key);
3118
3119                         if (page_shift == 12) {
3120                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3121                         } else if (page_shift == 21) {
3122                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3123                         } else {
3124                                 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3125                                           " ib_wr=%u, max=1\n", ib_wr->num_sge);
3126                                 err = -EINVAL;
3127                                 break;
3128                         }
3129
3130                         /* Set access_flags */
3131                         wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3132                         if (flags & IB_ACCESS_LOCAL_WRITE)
3133                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3134
3135                         if (flags & IB_ACCESS_REMOTE_WRITE)
3136                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3137
3138                         if (flags & IB_ACCESS_REMOTE_READ)
3139                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3140
3141                         if (flags & IB_ACCESS_MW_BIND)
3142                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3143
3144                         /* Fill in PBL info: */
3145                         set_wqe_64bit_value(wqe->wqe_words,
3146                                             NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3147                                             mr->paddr);
3148
3149                         set_wqe_32bit_value(wqe->wqe_words,
3150                                             NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3151                                             mr->npages * 8);
3152
3153                         nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3154                                   "length: %lld, rkey: %0x, pgl_paddr: %llx, "
3155                                   "page_list_len: %u, wqe_misc: %x\n",
3156                                   (unsigned long long) mr->ibmr.iova,
3157                                   mr->ibmr.length,
3158                                   reg_wr(ib_wr)->key,
3159                                   (unsigned long long) mr->paddr,
3160                                   mr->npages,
3161                                   wqe_misc);
3162                         break;
3163                 }
3164                 default:
3165                         /* error */
3166                         err = -EINVAL;
3167                         break;
3168                 }
3169
3170                 if (err)
3171                         break;
3172
3173                 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3174                         wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3175
3176                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3177
3178                 ib_wr = ib_wr->next;
3179                 head++;
3180                 wqe_count++;
3181                 if (head >= qsize)
3182                         head = 0;
3183
3184         }
3185
3186         nesqp->hwqp.sq_head = head;
3187         barrier();
3188         while (wqe_count) {
3189                 counter = min(wqe_count, ((u32)255));
3190                 wqe_count -= counter;
3191                 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3192                                 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3193         }
3194
3195         spin_unlock_irqrestore(&nesqp->lock, flags);
3196
3197 out:
3198         if (err)
3199                 *bad_wr = ib_wr;
3200         return err;
3201 }
3202
3203
3204 /**
3205  * nes_post_recv
3206  */
3207 static int nes_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *ib_wr,
3208                          const struct ib_recv_wr **bad_wr)
3209 {
3210         u64 u64temp;
3211         unsigned long flags = 0;
3212         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3213         struct nes_device *nesdev = nesvnic->nesdev;
3214         struct nes_qp *nesqp = to_nesqp(ibqp);
3215         struct nes_hw_qp_wqe *wqe;
3216         int err = 0;
3217         int sge_index;
3218         u32 qsize = nesqp->hwqp.rq_size;
3219         u32 head;
3220         u32 wqe_count = 0;
3221         u32 counter;
3222         u32 total_payload_length;
3223
3224         if (nesqp->ibqp_state > IB_QPS_RTS) {
3225                 err = -EINVAL;
3226                 goto out;
3227         }
3228
3229         spin_lock_irqsave(&nesqp->lock, flags);
3230
3231         head = nesqp->hwqp.rq_head;
3232
3233         while (ib_wr) {
3234                 /* Check for QP error */
3235                 if (nesqp->term_flags) {
3236                         err = -EINVAL;
3237                         break;
3238                 }
3239
3240                 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3241                         err = -EINVAL;
3242                         break;
3243                 }
3244                 /* Check for RQ overflow */
3245                 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3246                         err = -ENOMEM;
3247                         break;
3248                 }
3249
3250                 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3251                 wqe = &nesqp->hwqp.rq_vbase[head];
3252
3253                 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3254                                 nesqp->hwqp.qp_id, wqe, head); */
3255                 nes_fill_init_qp_wqe(wqe, nesqp, head);
3256                 u64temp = (u64)(ib_wr->wr_id);
3257                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3258                                         u64temp);
3259                 total_payload_length = 0;
3260                 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3261                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3262                                         ib_wr->sg_list[sge_index].addr);
3263                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3264                                         ib_wr->sg_list[sge_index].length);
3265                         set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3266                                         ib_wr->sg_list[sge_index].lkey);
3267
3268                         total_payload_length += ib_wr->sg_list[sge_index].length;
3269                 }
3270                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3271                                         total_payload_length);
3272
3273                 ib_wr = ib_wr->next;
3274                 head++;
3275                 wqe_count++;
3276                 if (head >= qsize)
3277                         head = 0;
3278         }
3279
3280         nesqp->hwqp.rq_head = head;
3281         barrier();
3282         while (wqe_count) {
3283                 counter = min(wqe_count, ((u32)255));
3284                 wqe_count -= counter;
3285                 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3286         }
3287
3288         spin_unlock_irqrestore(&nesqp->lock, flags);
3289
3290 out:
3291         if (err)
3292                 *bad_wr = ib_wr;
3293         return err;
3294 }
3295
3296 /**
3297  * nes_drain_sq - drain sq
3298  * @ibqp: pointer to ibqp
3299  */
3300 static void nes_drain_sq(struct ib_qp *ibqp)
3301 {
3302         struct nes_qp *nesqp = to_nesqp(ibqp);
3303
3304         if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3305                 wait_for_completion(&nesqp->sq_drained);
3306 }
3307
3308 /**
3309  * nes_drain_rq - drain rq
3310  * @ibqp: pointer to ibqp
3311  */
3312 static void nes_drain_rq(struct ib_qp *ibqp)
3313 {
3314         struct nes_qp *nesqp = to_nesqp(ibqp);
3315
3316         if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3317                 wait_for_completion(&nesqp->rq_drained);
3318 }
3319
3320 /**
3321  * nes_poll_cq
3322  */
3323 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3324 {
3325         u64 u64temp;
3326         u64 wrid;
3327         unsigned long flags = 0;
3328         struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3329         struct nes_device *nesdev = nesvnic->nesdev;
3330         struct nes_cq *nescq = to_nescq(ibcq);
3331         struct nes_qp *nesqp;
3332         struct nes_hw_cqe cqe;
3333         u32 head;
3334         u32 wq_tail = 0;
3335         u32 cq_size;
3336         u32 cqe_count = 0;
3337         u32 wqe_index;
3338         u32 u32temp;
3339         u32 move_cq_head = 1;
3340         u32 err_code;
3341
3342         nes_debug(NES_DBG_CQ, "\n");
3343
3344         spin_lock_irqsave(&nescq->lock, flags);
3345
3346         head = nescq->hw_cq.cq_head;
3347         cq_size = nescq->hw_cq.cq_size;
3348
3349         while (cqe_count < num_entries) {
3350                 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3351                                 NES_CQE_VALID) == 0)
3352                         break;
3353
3354                 /*
3355                  * Make sure we read CQ entry contents *after*
3356                  * we've checked the valid bit.
3357                  */
3358                 rmb();
3359
3360                 cqe = nescq->hw_cq.cq_vbase[head];
3361                 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3362                 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3363                 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3364                 /* parse CQE, get completion context from WQE (either rq or sq) */
3365                 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3366                                 ((u64)u32temp);
3367
3368                 if (u64temp) {
3369                         nesqp = (struct nes_qp *)(unsigned long)u64temp;
3370                         memset(entry, 0, sizeof *entry);
3371                         if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3372                                 entry->status = IB_WC_SUCCESS;
3373                         } else {
3374                                 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3375                                 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3376                                         entry->status = err_code & 0x0000ffff;
3377
3378                                         /* The rest of the cqe's will be marked as flushed */
3379                                         nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3380                                                 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3381                                                             NES_IWARP_CQE_MINOR_FLUSH);
3382                                 } else
3383                                         entry->status = IB_WC_WR_FLUSH_ERR;
3384                         }
3385
3386                         entry->qp = &nesqp->ibqp;
3387                         entry->src_qp = nesqp->hwqp.qp_id;
3388
3389                         if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3390                                 if (nesqp->skip_lsmm) {
3391                                         nesqp->skip_lsmm = 0;
3392                                         nesqp->hwqp.sq_tail++;
3393                                 }
3394
3395                                 /* Working on a SQ Completion*/
3396                                 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3397                                                 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3398                                                 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3399                                                 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3400                                 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3401                                                 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3402
3403                                 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3404                                                 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3405                                         case NES_IWARP_SQ_OP_RDMAW:
3406                                                 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3407                                                 entry->opcode = IB_WC_RDMA_WRITE;
3408                                                 break;
3409                                         case NES_IWARP_SQ_OP_RDMAR:
3410                                                 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3411                                                 entry->opcode = IB_WC_RDMA_READ;
3412                                                 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3413                                                                 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3414                                                 break;
3415                                         case NES_IWARP_SQ_OP_SENDINV:
3416                                         case NES_IWARP_SQ_OP_SENDSEINV:
3417                                         case NES_IWARP_SQ_OP_SEND:
3418                                         case NES_IWARP_SQ_OP_SENDSE:
3419                                                 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3420                                                 entry->opcode = IB_WC_SEND;
3421                                                 break;
3422                                         case NES_IWARP_SQ_OP_LOCINV:
3423                                                 entry->opcode = IB_WC_LOCAL_INV;
3424                                                 break;
3425                                         case NES_IWARP_SQ_OP_FAST_REG:
3426                                                 entry->opcode = IB_WC_REG_MR;
3427                                                 break;
3428                                 }
3429
3430                                 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3431                                 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3432                                         move_cq_head = 0;
3433                                         wq_tail = nesqp->hwqp.sq_tail;
3434                                 }
3435                         } else {
3436                                 /* Working on a RQ Completion*/
3437                                 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3438                                 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3439                                         ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3440                                 entry->opcode = IB_WC_RECV;
3441
3442                                 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3443                                 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3444                                         move_cq_head = 0;
3445                                         wq_tail = nesqp->hwqp.rq_tail;
3446                                 }
3447                         }
3448
3449                         if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3450                                 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3451                                         complete(&nesqp->sq_drained);
3452                                 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3453                                         complete(&nesqp->rq_drained);
3454                         }
3455
3456                         entry->wr_id = wrid;
3457                         entry++;
3458                         cqe_count++;
3459                 }
3460
3461                 if (move_cq_head) {
3462                         nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3463                         if (++head >= cq_size)
3464                                 head = 0;
3465                         nescq->polled_completions++;
3466
3467                         if ((nescq->polled_completions > (cq_size / 2)) ||
3468                                         (nescq->polled_completions == 255)) {
3469                                 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3470                                         " are pending %u of %u.\n",
3471                                         nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3472                                 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3473                                         nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3474                                 nescq->polled_completions = 0;
3475                         }
3476                 } else {
3477                         /* Update the wqe index and set status to flush */
3478                         wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3479                         wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3480                         nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3481                                 cpu_to_le32(wqe_index);
3482                         move_cq_head = 1; /* ready for next pass */
3483                 }
3484         }
3485
3486         if (nescq->polled_completions) {
3487                 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3488                                 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3489                 nescq->polled_completions = 0;
3490         }
3491
3492         nescq->hw_cq.cq_head = head;
3493         nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3494                         cqe_count, nescq->hw_cq.cq_number);
3495
3496         spin_unlock_irqrestore(&nescq->lock, flags);
3497
3498         return cqe_count;
3499 }
3500
3501
3502 /**
3503  * nes_req_notify_cq
3504  */
3505 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3506                 {
3507         struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3508         struct nes_device *nesdev = nesvnic->nesdev;
3509         struct nes_cq *nescq = to_nescq(ibcq);
3510         u32 cq_arm;
3511
3512         nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3513                         nescq->hw_cq.cq_number);
3514
3515         cq_arm = nescq->hw_cq.cq_number;
3516         if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3517                 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3518         else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3519                 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3520         else
3521                 return -EINVAL;
3522
3523         nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3524         nes_read32(nesdev->regs+NES_CQE_ALLOC);
3525
3526         return 0;
3527 }
3528
3529 static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3530                               struct ib_port_immutable *immutable)
3531 {
3532         struct ib_port_attr attr;
3533         int err;
3534
3535         immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3536
3537         err = nes_query_port(ibdev, port_num, &attr);
3538         if (err)
3539                 return err;
3540
3541         immutable->pkey_tbl_len = attr.pkey_tbl_len;
3542         immutable->gid_tbl_len = attr.gid_tbl_len;
3543
3544         return 0;
3545 }
3546
3547 static void get_dev_fw_str(struct ib_device *dev, char *str)
3548 {
3549         struct nes_ib_device *nesibdev =
3550                         container_of(dev, struct nes_ib_device, ibdev);
3551         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3552
3553         nes_debug(NES_DBG_INIT, "\n");
3554         snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u",
3555                  (nesvnic->nesdev->nesadapter->firmware_version >> 16),
3556                  (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
3557 }
3558
3559 static const struct ib_device_ops nes_dev_ops = {
3560         .alloc_mr = nes_alloc_mr,
3561         .alloc_mw = nes_alloc_mw,
3562         .alloc_pd = nes_alloc_pd,
3563         .alloc_ucontext = nes_alloc_ucontext,
3564         .create_cq = nes_create_cq,
3565         .create_qp = nes_create_qp,
3566         .dealloc_mw = nes_dealloc_mw,
3567         .dealloc_pd = nes_dealloc_pd,
3568         .dealloc_ucontext = nes_dealloc_ucontext,
3569         .dereg_mr = nes_dereg_mr,
3570         .destroy_cq = nes_destroy_cq,
3571         .destroy_qp = nes_destroy_qp,
3572         .drain_rq = nes_drain_rq,
3573         .drain_sq = nes_drain_sq,
3574         .get_dev_fw_str = get_dev_fw_str,
3575         .get_dma_mr = nes_get_dma_mr,
3576         .get_port_immutable = nes_port_immutable,
3577         .map_mr_sg = nes_map_mr_sg,
3578         .mmap = nes_mmap,
3579         .modify_qp = nes_modify_qp,
3580         .poll_cq = nes_poll_cq,
3581         .post_recv = nes_post_recv,
3582         .post_send = nes_post_send,
3583         .query_device = nes_query_device,
3584         .query_gid = nes_query_gid,
3585         .query_pkey = nes_query_pkey,
3586         .query_port = nes_query_port,
3587         .query_qp = nes_query_qp,
3588         .reg_user_mr = nes_reg_user_mr,
3589         .req_notify_cq = nes_req_notify_cq,
3590         INIT_RDMA_OBJ_SIZE(ib_pd, nes_pd, ibpd),
3591         INIT_RDMA_OBJ_SIZE(ib_ucontext, nes_ucontext, ibucontext),
3592 };
3593
3594 /**
3595  * nes_init_ofa_device
3596  */
3597 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3598 {
3599         struct nes_ib_device *nesibdev;
3600         struct nes_vnic *nesvnic = netdev_priv(netdev);
3601         struct nes_device *nesdev = nesvnic->nesdev;
3602
3603         nesibdev = ib_alloc_device(nes_ib_device, ibdev);
3604         if (nesibdev == NULL) {
3605                 return NULL;
3606         }
3607         nesibdev->ibdev.owner = THIS_MODULE;
3608
3609         nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3610         memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3611         memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3612
3613         nesibdev->ibdev.uverbs_cmd_mask =
3614                         (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3615                         (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3616                         (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3617                         (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3618                         (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3619                         (1ull << IB_USER_VERBS_CMD_REG_MR) |
3620                         (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3621                         (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3622                         (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3623                         (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3624                         (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3625                         (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3626                         (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3627                         (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3628                         (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3629                         (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3630                         (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3631                         (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3632                         (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3633                         (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3634                         (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3635                         (1ull << IB_USER_VERBS_CMD_POST_SEND);
3636
3637         nesibdev->ibdev.phys_port_cnt = 1;
3638         nesibdev->ibdev.num_comp_vectors = 1;
3639         nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3640
3641         nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3642         if (nesibdev->ibdev.iwcm == NULL) {
3643                 ib_dealloc_device(&nesibdev->ibdev);
3644                 return NULL;
3645         }
3646         nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3647         nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3648         nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3649         nesibdev->ibdev.iwcm->connect = nes_connect;
3650         nesibdev->ibdev.iwcm->accept = nes_accept;
3651         nesibdev->ibdev.iwcm->reject = nes_reject;
3652         nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3653         nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3654
3655         ib_set_device_ops(&nesibdev->ibdev, &nes_dev_ops);
3656         memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3657                sizeof(nesibdev->ibdev.iwcm->ifname));
3658
3659         return nesibdev;
3660 }
3661
3662
3663 /**
3664  * nes_handle_delayed_event
3665  */
3666 static void nes_handle_delayed_event(struct timer_list *t)
3667 {
3668         struct nes_vnic *nesvnic = from_timer(nesvnic, t, event_timer);
3669
3670         if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3671                 struct ib_event event;
3672
3673                 event.device = &nesvnic->nesibdev->ibdev;
3674                 if (!event.device)
3675                         goto stop_timer;
3676                 event.event = nesvnic->delayed_event;
3677                 event.element.port_num = nesvnic->logical_port + 1;
3678                 ib_dispatch_event(&event);
3679         }
3680
3681 stop_timer:
3682         nesvnic->event_timer.function = NULL;
3683 }
3684
3685
3686 void  nes_port_ibevent(struct nes_vnic *nesvnic)
3687 {
3688         struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3689         struct nes_device *nesdev = nesvnic->nesdev;
3690         struct ib_event event;
3691         event.device = &nesibdev->ibdev;
3692         event.element.port_num = nesvnic->logical_port + 1;
3693         event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3694
3695         if (!nesvnic->event_timer.function) {
3696                 ib_dispatch_event(&event);
3697                 nesvnic->last_dispatched_event = event.event;
3698                 nesvnic->event_timer.function = nes_handle_delayed_event;
3699                 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3700                 add_timer(&nesvnic->event_timer);
3701         } else {
3702                 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3703         }
3704         nesvnic->delayed_event = event.event;
3705 }
3706
3707
3708 /**
3709  * nes_destroy_ofa_device
3710  */
3711 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3712 {
3713         if (nesibdev == NULL)
3714                 return;
3715
3716         nes_unregister_ofa_device(nesibdev);
3717
3718         kfree(nesibdev->ibdev.iwcm);
3719         ib_dealloc_device(&nesibdev->ibdev);
3720 }
3721
3722
3723 /**
3724  * nes_register_ofa_device
3725  */
3726 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3727 {
3728         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3729         struct nes_device *nesdev = nesvnic->nesdev;
3730         struct nes_adapter *nesadapter = nesdev->nesadapter;
3731         int ret;
3732
3733         rdma_set_device_sysfs_group(&nesvnic->nesibdev->ibdev, &nes_attr_group);
3734         nesvnic->nesibdev->ibdev.driver_id = RDMA_DRIVER_NES;
3735         ret = ib_register_device(&nesvnic->nesibdev->ibdev, "nes%d");
3736         if (ret) {
3737                 return ret;
3738         }
3739
3740         /* Get the resources allocated to this device */
3741         nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3742         nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3743         nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3744         nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3745
3746         nesvnic->of_device_registered = 1;
3747
3748         return 0;
3749 }
3750
3751
3752 /**
3753  * nes_unregister_ofa_device
3754  */
3755 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3756 {
3757         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3758
3759         if (nesvnic->of_device_registered)
3760                 ib_unregister_device(&nesibdev->ibdev);
3761
3762         nesvnic->of_device_registered = 0;
3763 }