RDMA/nes: remove redundant check on udata
[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
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                         }
1085                         err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1086                                         nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1087                         if (err) {
1088                                 nes_debug(NES_DBG_QP,
1089                                           "error geting qp mem code = %d\n", err);
1090                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1091                                 kfree(nesqp->allocated_buffer);
1092                                 return ERR_PTR(-ENOMEM);
1093                         }
1094
1095                         nesqp->hwqp.sq_size = sq_size;
1096                         nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1097                         nesqp->hwqp.sq_head = 1;
1098                         nesqp->hwqp.rq_size = rq_size;
1099                         nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1100                         /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1101                                         (void *)nesqp->nesqp_context_pbase);
1102                         */
1103                         nesqp->hwqp.qp_id = qp_num;
1104                         nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1105                         nesqp->nespd = nespd;
1106
1107                         nescq = to_nescq(init_attr->send_cq);
1108                         nesqp->nesscq = nescq;
1109                         nescq = to_nescq(init_attr->recv_cq);
1110                         nesqp->nesrcq = nescq;
1111
1112                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1113                                         NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1114                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1115                                         NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1116                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1117                                         NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1118                         if (!udata) {
1119                                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1120                                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1121                         }
1122                         nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1123                                         ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1124                         u64temp = (u64)nesqp->hwqp.sq_pbase;
1125                         nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1126                         nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1127
1128
1129                         if (!virt_wqs) {
1130                                 u64temp = (u64)nesqp->hwqp.sq_pbase;
1131                                 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1132                                 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1133                                 u64temp = (u64)nesqp->hwqp.rq_pbase;
1134                                 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1135                                 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1136                         } else {
1137                                 u64temp = (u64)nesqp->pbl_pbase;
1138                                 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1139                                 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1140                         }
1141
1142                         /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1143                                         nesvnic->next_qp_nic_index,
1144                                         nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1145                         spin_lock_irqsave(&nesdev->cqp.lock, flags);
1146                         nesqp->nesqp_context->misc2 |= cpu_to_le32(
1147                                         (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1148                                         NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1149                         nesvnic->next_qp_nic_index++;
1150                         if ((nesvnic->next_qp_nic_index > 3) ||
1151                                         (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1152                                 nesvnic->next_qp_nic_index = 0;
1153                         }
1154                         spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1155
1156                         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1157                         u64temp = (u64)nesqp->hwqp.q2_pbase;
1158                         nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1159                         nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1160                         nesqp->nesqp_context->aeq_token_low =  cpu_to_le32((u32)((unsigned long)(nesqp)));
1161                         nesqp->nesqp_context->aeq_token_high =  cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1162                         nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1163                                         NES_QPCONTEXT_ORDIRD_AAH |
1164                                         ((((u32)nesadapter->max_irrq_wr) <<
1165                                         NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1166                         if (disable_mpa_crc) {
1167                                 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1168                                 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1169                         }
1170
1171
1172                         /* Create the QP */
1173                         cqp_request = nes_get_cqp_request(nesdev);
1174                         if (cqp_request == NULL) {
1175                                 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1176                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1177                                 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1178                                 kfree(nesqp->allocated_buffer);
1179                                 return ERR_PTR(-ENOMEM);
1180                         }
1181                         cqp_request->waiting = 1;
1182                         cqp_wqe = &cqp_request->cqp_wqe;
1183
1184                         if (!virt_wqs) {
1185                                 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1186                                         NES_CQP_QP_IWARP_STATE_IDLE;
1187                         } else {
1188                                 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1189                                         NES_CQP_QP_IWARP_STATE_IDLE;
1190                         }
1191                         opcode |= NES_CQP_QP_CQS_VALID;
1192                         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1193                         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1194                         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1195
1196                         u64temp = (u64)nesqp->nesqp_context_pbase;
1197                         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1198
1199                         atomic_set(&cqp_request->refcount, 2);
1200                         nes_post_cqp_request(nesdev, cqp_request);
1201
1202                         /* Wait for CQP */
1203                         nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1204                                         nesqp->hwqp.qp_id);
1205                         ret = wait_event_timeout(cqp_request->waitq,
1206                                         (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1207                         nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1208                                         " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1209                                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1210                                         nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1211                                         cqp_request->major_code, cqp_request->minor_code);
1212                         if ((!ret) || (cqp_request->major_code)) {
1213                                 nes_put_cqp_request(nesdev, cqp_request);
1214                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1215                                 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1216                                 kfree(nesqp->allocated_buffer);
1217                                 if (!ret) {
1218                                         return ERR_PTR(-ETIME);
1219                                 } else {
1220                                         return ERR_PTR(-EIO);
1221                                 }
1222                         }
1223
1224                         nes_put_cqp_request(nesdev, cqp_request);
1225
1226                         if (udata) {
1227                                 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1228                                 uresp.mmap_rq_db_index = 0;
1229                                 uresp.actual_sq_size = sq_size;
1230                                 uresp.actual_rq_size = rq_size;
1231                                 uresp.qp_id = nesqp->hwqp.qp_id;
1232                                 uresp.nes_drv_opt = nes_drv_opt;
1233                                 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1234                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1235                                         nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1236                                         kfree(nesqp->allocated_buffer);
1237                                         return ERR_PTR(-EFAULT);
1238                                 }
1239                         }
1240
1241                         nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1242                                         nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1243                         spin_lock_init(&nesqp->lock);
1244                         nes_add_ref(&nesqp->ibqp);
1245                         break;
1246                 default:
1247                         nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1248                         return ERR_PTR(-EINVAL);
1249         }
1250         init_completion(&nesqp->sq_drained);
1251         init_completion(&nesqp->rq_drained);
1252
1253         nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1254         timer_setup(&nesqp->terminate_timer, nes_terminate_timeout, 0);
1255
1256         /* update the QP table */
1257         nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1258         nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1259                         netdev_refcnt_read(nesvnic->netdev));
1260
1261         return &nesqp->ibqp;
1262 }
1263
1264 /**
1265  * nes_clean_cq
1266  */
1267 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1268 {
1269         u32 cq_head;
1270         u32 lo;
1271         u32 hi;
1272         u64 u64temp;
1273         unsigned long flags = 0;
1274
1275         spin_lock_irqsave(&nescq->lock, flags);
1276
1277         cq_head = nescq->hw_cq.cq_head;
1278         while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1279                 rmb();
1280                 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1281                 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1282                 u64temp = (((u64)hi) << 32) | ((u64)lo);
1283                 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1284                 if (u64temp == (u64)(unsigned long)nesqp) {
1285                         /* Zero the context value so cqe will be ignored */
1286                         nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1287                         nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1288                 }
1289
1290                 if (++cq_head >= nescq->hw_cq.cq_size)
1291                         cq_head = 0;
1292         }
1293
1294         spin_unlock_irqrestore(&nescq->lock, flags);
1295 }
1296
1297
1298 /**
1299  * nes_destroy_qp
1300  */
1301 static int nes_destroy_qp(struct ib_qp *ibqp)
1302 {
1303         struct nes_qp *nesqp = to_nesqp(ibqp);
1304         struct nes_ucontext *nes_ucontext;
1305         struct ib_qp_attr attr;
1306         struct iw_cm_id *cm_id;
1307         struct iw_cm_event cm_event;
1308         int ret = 0;
1309
1310         atomic_inc(&sw_qps_destroyed);
1311         nesqp->destroyed = 1;
1312
1313         /* Blow away the connection if it exists. */
1314         if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1315                 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1316                 attr.qp_state = IB_QPS_ERR;
1317                 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1318         }
1319
1320         if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1321                         (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1322                 cm_id = nesqp->cm_id;
1323                 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1324                 cm_event.status = -ETIMEDOUT;
1325                 cm_event.local_addr = cm_id->local_addr;
1326                 cm_event.remote_addr = cm_id->remote_addr;
1327                 cm_event.private_data = NULL;
1328                 cm_event.private_data_len = 0;
1329
1330                 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1331                                 "QP%u. cm_id = %p, refcount = %u. \n",
1332                                 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1333
1334                 cm_id->rem_ref(cm_id);
1335                 ret = cm_id->event_handler(cm_id, &cm_event);
1336                 if (ret)
1337                         nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1338         }
1339
1340         if (nesqp->user_mode) {
1341                 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1342                         nes_ucontext = to_nesucontext(ibqp->uobject->context);
1343                         clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1344                         nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1345                         if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1346                                 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1347                         }
1348                 }
1349                 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1350                         nesqp->sq_kmapped = 0;
1351                         kunmap(nesqp->page);
1352                 }
1353         } else {
1354                 /* Clean any pending completions from the cq(s) */
1355                 if (nesqp->nesscq)
1356                         nes_clean_cq(nesqp, nesqp->nesscq);
1357
1358                 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1359                         nes_clean_cq(nesqp, nesqp->nesrcq);
1360         }
1361         nes_rem_ref(&nesqp->ibqp);
1362         return 0;
1363 }
1364
1365
1366 /**
1367  * nes_create_cq
1368  */
1369 static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1370                                    const struct ib_cq_init_attr *attr,
1371                                    struct ib_ucontext *context,
1372                                    struct ib_udata *udata)
1373 {
1374         int entries = attr->cqe;
1375         u64 u64temp;
1376         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1377         struct nes_device *nesdev = nesvnic->nesdev;
1378         struct nes_adapter *nesadapter = nesdev->nesadapter;
1379         struct nes_cq *nescq;
1380         struct nes_ucontext *nes_ucontext = NULL;
1381         struct nes_cqp_request *cqp_request;
1382         void *mem = NULL;
1383         struct nes_hw_cqp_wqe *cqp_wqe;
1384         struct nes_pbl *nespbl = NULL;
1385         struct nes_create_cq_req req;
1386         struct nes_create_cq_resp resp;
1387         u32 cq_num = 0;
1388         u32 opcode = 0;
1389         u32 pbl_entries = 1;
1390         int err;
1391         unsigned long flags;
1392         int ret;
1393
1394         if (attr->flags)
1395                 return ERR_PTR(-EINVAL);
1396
1397         if (entries > nesadapter->max_cqe)
1398                 return ERR_PTR(-EINVAL);
1399
1400         err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1401                         nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
1402         if (err) {
1403                 return ERR_PTR(err);
1404         }
1405
1406         nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1407         if (!nescq) {
1408                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1409                 return ERR_PTR(-ENOMEM);
1410         }
1411
1412         nescq->hw_cq.cq_size = max(entries + 1, 5);
1413         nescq->hw_cq.cq_number = cq_num;
1414         nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1415
1416
1417         if (context) {
1418                 nes_ucontext = to_nesucontext(context);
1419                 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1420                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1421                         kfree(nescq);
1422                         return ERR_PTR(-EFAULT);
1423                 }
1424                 nesvnic->mcrq_ucontext = nes_ucontext;
1425                 nes_ucontext->mcrqf = req.mcrqf;
1426                 if (nes_ucontext->mcrqf) {
1427                         if (nes_ucontext->mcrqf & 0x80000000)
1428                                 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1429                         else if (nes_ucontext->mcrqf & 0x40000000)
1430                                 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1431                         else
1432                                 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1433                         nescq->mcrqf = nes_ucontext->mcrqf;
1434                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1435                 }
1436                 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1437                                 (unsigned long)req.user_cq_buffer, entries);
1438                 err = 1;
1439                 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1440                         if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1441                                 list_del(&nespbl->list);
1442                                 err = 0;
1443                                 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1444                                                 nespbl);
1445                                 break;
1446                         }
1447                 }
1448                 if (err) {
1449                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1450                         kfree(nescq);
1451                         return ERR_PTR(-EFAULT);
1452                 }
1453
1454                 pbl_entries = nespbl->pbl_size >> 3;
1455                 nescq->cq_mem_size = 0;
1456         } else {
1457                 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1458                 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1459                                 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1460
1461                 /* allocate the physical buffer space */
1462                 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1463                                             &nescq->hw_cq.cq_pbase);
1464                 if (!mem) {
1465                         printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1466                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1467                         kfree(nescq);
1468                         return ERR_PTR(-ENOMEM);
1469                 }
1470
1471                 nescq->hw_cq.cq_vbase = mem;
1472                 nescq->hw_cq.cq_head = 0;
1473                 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1474                                 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1475                                 (u32)nescq->hw_cq.cq_pbase);
1476         }
1477
1478         nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1479         spin_lock_init(&nescq->lock);
1480
1481         /* send CreateCQ request to CQP */
1482         cqp_request = nes_get_cqp_request(nesdev);
1483         if (cqp_request == NULL) {
1484                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1485                 if (!context)
1486                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1487                                         nescq->hw_cq.cq_pbase);
1488                 else {
1489                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1490                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1491                         kfree(nespbl);
1492                 }
1493
1494                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1495                 kfree(nescq);
1496                 return ERR_PTR(-ENOMEM);
1497         }
1498         cqp_request->waiting = 1;
1499         cqp_wqe = &cqp_request->cqp_wqe;
1500
1501         opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1502                         NES_CQP_CQ_CHK_OVERFLOW |
1503                         NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1504
1505         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1506
1507         if (pbl_entries != 1) {
1508                 if (pbl_entries > 32) {
1509                         /* use 4k pbl */
1510                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1511                         if (nesadapter->free_4kpbl == 0) {
1512                                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1513                                 nes_free_cqp_request(nesdev, cqp_request);
1514                                 if (!context)
1515                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1516                                                         nescq->hw_cq.cq_pbase);
1517                                 else {
1518                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1519                                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1520                                         kfree(nespbl);
1521                                 }
1522                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1523                                 kfree(nescq);
1524                                 return ERR_PTR(-ENOMEM);
1525                         } else {
1526                                 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1527                                 nescq->virtual_cq = 2;
1528                                 nesadapter->free_4kpbl--;
1529                         }
1530                 } else {
1531                         /* use 256 byte pbl */
1532                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1533                         if (nesadapter->free_256pbl == 0) {
1534                                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1535                                 nes_free_cqp_request(nesdev, cqp_request);
1536                                 if (!context)
1537                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1538                                                         nescq->hw_cq.cq_pbase);
1539                                 else {
1540                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1541                                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1542                                         kfree(nespbl);
1543                                 }
1544                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1545                                 kfree(nescq);
1546                                 return ERR_PTR(-ENOMEM);
1547                         } else {
1548                                 opcode |= NES_CQP_CQ_VIRT;
1549                                 nescq->virtual_cq = 1;
1550                                 nesadapter->free_256pbl--;
1551                         }
1552                 }
1553         }
1554
1555         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1556
1557         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1558         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1559         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1560                         (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1561
1562         if (context) {
1563                 if (pbl_entries != 1)
1564                         u64temp = (u64)nespbl->pbl_pbase;
1565                 else
1566                         u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1567                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1568                                 nes_ucontext->mmap_db_index[0]);
1569         } else {
1570                 u64temp = (u64)nescq->hw_cq.cq_pbase;
1571                 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1572         }
1573         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1574         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1575         u64temp = (u64)(unsigned long)&nescq->hw_cq;
1576         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1577                         cpu_to_le32((u32)(u64temp >> 1));
1578         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1579                         cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1580
1581         atomic_set(&cqp_request->refcount, 2);
1582         nes_post_cqp_request(nesdev, cqp_request);
1583
1584         /* Wait for CQP */
1585         nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1586                         nescq->hw_cq.cq_number);
1587         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1588                         NES_EVENT_TIMEOUT * 2);
1589         nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1590                         nescq->hw_cq.cq_number, ret);
1591         if ((!ret) || (cqp_request->major_code)) {
1592                 nes_put_cqp_request(nesdev, cqp_request);
1593                 if (!context)
1594                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1595                                         nescq->hw_cq.cq_pbase);
1596                 else {
1597                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1598                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1599                         kfree(nespbl);
1600                 }
1601                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1602                 kfree(nescq);
1603                 return ERR_PTR(-EIO);
1604         }
1605         nes_put_cqp_request(nesdev, cqp_request);
1606
1607         if (context) {
1608                 /* free the nespbl */
1609                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1610                                 nespbl->pbl_pbase);
1611                 kfree(nespbl);
1612                 resp.cq_id = nescq->hw_cq.cq_number;
1613                 resp.cq_size = nescq->hw_cq.cq_size;
1614                 resp.mmap_db_index = 0;
1615                 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
1616                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1617                         kfree(nescq);
1618                         return ERR_PTR(-EFAULT);
1619                 }
1620         }
1621
1622         return &nescq->ibcq;
1623 }
1624
1625
1626 /**
1627  * nes_destroy_cq
1628  */
1629 static int nes_destroy_cq(struct ib_cq *ib_cq)
1630 {
1631         struct nes_cq *nescq;
1632         struct nes_device *nesdev;
1633         struct nes_vnic *nesvnic;
1634         struct nes_adapter *nesadapter;
1635         struct nes_hw_cqp_wqe *cqp_wqe;
1636         struct nes_cqp_request *cqp_request;
1637         unsigned long flags;
1638         u32 opcode = 0;
1639         int ret;
1640
1641         if (ib_cq == NULL)
1642                 return 0;
1643
1644         nescq = to_nescq(ib_cq);
1645         nesvnic = to_nesvnic(ib_cq->device);
1646         nesdev = nesvnic->nesdev;
1647         nesadapter = nesdev->nesadapter;
1648
1649         nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1650
1651         /* Send DestroyCQ request to CQP */
1652         cqp_request = nes_get_cqp_request(nesdev);
1653         if (cqp_request == NULL) {
1654                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1655                 return -ENOMEM;
1656         }
1657         cqp_request->waiting = 1;
1658         cqp_wqe = &cqp_request->cqp_wqe;
1659         opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1660         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1661         if (nescq->virtual_cq == 1) {
1662                 nesadapter->free_256pbl++;
1663                 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1664                         printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1665                                         __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1666                 }
1667         } else if (nescq->virtual_cq == 2) {
1668                 nesadapter->free_4kpbl++;
1669                 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1670                         printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1671                                         __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1672                 }
1673                 opcode |= NES_CQP_CQ_4KB_CHUNK;
1674         }
1675
1676         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1677
1678         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1679         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1680         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1681                 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1682         if (!nescq->mcrqf)
1683                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1684
1685         atomic_set(&cqp_request->refcount, 2);
1686         nes_post_cqp_request(nesdev, cqp_request);
1687
1688         /* Wait for CQP */
1689         nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1690                         nescq->hw_cq.cq_number);
1691         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1692                         NES_EVENT_TIMEOUT);
1693         nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1694                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1695                         nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1696                         cqp_request->minor_code);
1697         if (!ret) {
1698                 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1699                                         nescq->hw_cq.cq_number);
1700                 ret = -ETIME;
1701         } else if (cqp_request->major_code) {
1702                 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1703                                         nescq->hw_cq.cq_number);
1704                 ret = -EIO;
1705         } else {
1706                 ret = 0;
1707         }
1708         nes_put_cqp_request(nesdev, cqp_request);
1709
1710         if (nescq->cq_mem_size)
1711                 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1712                                     nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1713         kfree(nescq);
1714
1715         return ret;
1716 }
1717
1718 /**
1719  * root_256
1720  */
1721 static u32 root_256(struct nes_device *nesdev,
1722                     struct nes_root_vpbl *root_vpbl,
1723                     struct nes_root_vpbl *new_root,
1724                     u16 pbl_count_4k)
1725 {
1726         u64 leaf_pbl;
1727         int i, j, k;
1728
1729         if (pbl_count_4k == 1) {
1730                 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1731                                                 512, &new_root->pbl_pbase);
1732
1733                 if (new_root->pbl_vbase == NULL)
1734                         return 0;
1735
1736                 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1737                 for (i = 0; i < 16; i++) {
1738                         new_root->pbl_vbase[i].pa_low =
1739                                 cpu_to_le32((u32)leaf_pbl);
1740                         new_root->pbl_vbase[i].pa_high =
1741                                 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1742                         leaf_pbl += 256;
1743                 }
1744         } else {
1745                 for (i = 3; i >= 0; i--) {
1746                         j = i * 16;
1747                         root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1748                         leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1749                             (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1750                                 << 32);
1751                         for (k = 1; k < 16; k++) {
1752                                 leaf_pbl += 256;
1753                                 root_vpbl->pbl_vbase[j + k].pa_low =
1754                                                 cpu_to_le32((u32)leaf_pbl);
1755                                 root_vpbl->pbl_vbase[j + k].pa_high =
1756                                     cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1757                         }
1758                 }
1759         }
1760
1761         return 1;
1762 }
1763
1764
1765 /**
1766  * nes_reg_mr
1767  */
1768 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1769                 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1770                 dma_addr_t single_buffer, u16 pbl_count_4k,
1771                 u16 residual_page_count_4k, int acc, u64 *iova_start,
1772                 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1773 {
1774         struct nes_hw_cqp_wqe *cqp_wqe;
1775         struct nes_cqp_request *cqp_request;
1776         unsigned long flags;
1777         int ret;
1778         struct nes_adapter *nesadapter = nesdev->nesadapter;
1779         uint pg_cnt = 0;
1780         u16 pbl_count_256 = 0;
1781         u16 pbl_count = 0;
1782         u8  use_256_pbls = 0;
1783         u8  use_4k_pbls = 0;
1784         u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1785         struct nes_root_vpbl new_root = { 0, NULL, NULL };
1786         u32 opcode = 0;
1787         u16 major_code;
1788
1789         /* Register the region with the adapter */
1790         cqp_request = nes_get_cqp_request(nesdev);
1791         if (cqp_request == NULL) {
1792                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1793                 return -ENOMEM;
1794         }
1795         cqp_request->waiting = 1;
1796         cqp_wqe = &cqp_request->cqp_wqe;
1797
1798         if (pbl_count_4k) {
1799                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1800
1801                 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1802                 pbl_count_256 = (pg_cnt + 31) / 32;
1803                 if (pg_cnt <= 32) {
1804                         if (pbl_count_256 <= nesadapter->free_256pbl)
1805                                 use_256_pbls = 1;
1806                         else if (pbl_count_4k <= nesadapter->free_4kpbl)
1807                                 use_4k_pbls = 1;
1808                 } else if (pg_cnt <= 2048) {
1809                         if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1810                             (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1811                                 use_4k_pbls = 1;
1812                         } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1813                                 use_256_pbls = 1;
1814                                 use_two_level = 1;
1815                         } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1816                                 use_4k_pbls = 1;
1817                         }
1818                 } else {
1819                         if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1820                                 use_4k_pbls = 1;
1821                 }
1822
1823                 if (use_256_pbls) {
1824                         pbl_count = pbl_count_256;
1825                         nesadapter->free_256pbl -= pbl_count + use_two_level;
1826                 } else if (use_4k_pbls) {
1827                         pbl_count =  pbl_count_4k;
1828                         nesadapter->free_4kpbl -= pbl_count + use_two_level;
1829                 } else {
1830                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1831                         nes_debug(NES_DBG_MR, "Out of Pbls\n");
1832                         nes_free_cqp_request(nesdev, cqp_request);
1833                         return -ENOMEM;
1834                 }
1835
1836                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1837         }
1838
1839         if (use_256_pbls && use_two_level) {
1840                 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1841                         if (new_root.pbl_pbase != 0)
1842                                 root_vpbl = &new_root;
1843                 } else {
1844                         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1845                         nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1846                         use_256_pbls = 0;
1847
1848                         if (pbl_count_4k == 1)
1849                                 use_two_level = 0;
1850                         pbl_count = pbl_count_4k;
1851
1852                         if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1853                                 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1854                                 use_4k_pbls = 1;
1855                         }
1856                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1857
1858                         if (use_4k_pbls == 0)
1859                                 return -ENOMEM;
1860                 }
1861         }
1862
1863         opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1864                                         NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1865         if (acc & IB_ACCESS_LOCAL_WRITE)
1866                 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1867         if (acc & IB_ACCESS_REMOTE_WRITE)
1868                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1869         if (acc & IB_ACCESS_REMOTE_READ)
1870                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1871         if (acc & IB_ACCESS_MW_BIND)
1872                 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1873
1874         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1875         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1876         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1877         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1878
1879         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1880                         cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1881         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1882                         cpu_to_le32(nespd->pd_id & 0x00007fff);
1883         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1884
1885         if (pbl_count == 0) {
1886                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1887         } else {
1888                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1889                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
1890                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
1891
1892                 if (use_4k_pbls)
1893                         cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1894         }
1895         barrier();
1896
1897         atomic_set(&cqp_request->refcount, 2);
1898         nes_post_cqp_request(nesdev, cqp_request);
1899
1900         /* Wait for CQP */
1901         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1902                         NES_EVENT_TIMEOUT);
1903         nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1904                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1905                         stag, ret, cqp_request->major_code, cqp_request->minor_code);
1906         major_code = cqp_request->major_code;
1907         nes_put_cqp_request(nesdev, cqp_request);
1908
1909         if ((!ret || major_code) && pbl_count != 0) {
1910                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1911                 if (use_256_pbls)
1912                         nesadapter->free_256pbl += pbl_count + use_two_level;
1913                 else if (use_4k_pbls)
1914                         nesadapter->free_4kpbl += pbl_count + use_two_level;
1915                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1916         }
1917         if (new_root.pbl_pbase)
1918                 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1919                                     new_root.pbl_pbase);
1920
1921         if (!ret)
1922                 return -ETIME;
1923         else if (major_code)
1924                 return -EIO;
1925
1926         *actual_pbl_cnt = pbl_count + use_two_level;
1927         *used_4k_pbls = use_4k_pbls;
1928         return 0;
1929 }
1930
1931
1932 /**
1933  * nes_reg_phys_mr
1934  */
1935 struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
1936                 int acc, u64 *iova_start)
1937 {
1938         u64 region_length;
1939         struct nes_pd *nespd = to_nespd(ib_pd);
1940         struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
1941         struct nes_device *nesdev = nesvnic->nesdev;
1942         struct nes_adapter *nesadapter = nesdev->nesadapter;
1943         struct nes_mr *nesmr;
1944         struct ib_mr *ibmr;
1945         struct nes_vpbl vpbl;
1946         struct nes_root_vpbl root_vpbl;
1947         u32 stag;
1948         unsigned long mask;
1949         u32 stag_index = 0;
1950         u32 next_stag_index = 0;
1951         u32 driver_key = 0;
1952         int err = 0;
1953         int ret = 0;
1954         u16 pbl_count = 0;
1955         u8 single_page = 1;
1956         u8 stag_key = 0;
1957
1958         region_length = 0;
1959         vpbl.pbl_vbase = NULL;
1960         root_vpbl.pbl_vbase = NULL;
1961         root_vpbl.pbl_pbase = 0;
1962
1963         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
1964         stag_key = (u8)next_stag_index;
1965
1966         driver_key = 0;
1967
1968         next_stag_index >>= 8;
1969         next_stag_index %= nesadapter->max_mr;
1970
1971         if ((addr ^ *iova_start) & ~PAGE_MASK)
1972                 return ERR_PTR(-EINVAL);
1973
1974         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
1975                         &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
1976         if (err) {
1977                 return ERR_PTR(err);
1978         }
1979
1980         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
1981         if (!nesmr) {
1982                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1983                 return ERR_PTR(-ENOMEM);
1984         }
1985
1986         /* Allocate a 4K buffer for the PBL */
1987         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
1988                         &vpbl.pbl_pbase);
1989         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
1990                         vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
1991         if (!vpbl.pbl_vbase) {
1992                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1993                 ibmr = ERR_PTR(-ENOMEM);
1994                 kfree(nesmr);
1995                 goto reg_phys_err;
1996         }
1997
1998
1999         mask = !size;
2000
2001         if (mask & ~PAGE_MASK) {
2002                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2003                 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2004                 ibmr = ERR_PTR(-EINVAL);
2005                 kfree(nesmr);
2006                 goto reg_phys_err;
2007         }
2008
2009         region_length += size;
2010         vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2011         vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2012
2013         stag = stag_index << 8;
2014         stag |= driver_key;
2015         stag += (u32)stag_key;
2016
2017         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2018                         " length = 0x%016lX, index = 0x%08X\n",
2019                         stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2020
2021         /* Make the leaf PBL the root if only one PBL */
2022         root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2023
2024         if (single_page) {
2025                 pbl_count = 0;
2026         } else {
2027                 pbl_count = 1;
2028         }
2029         ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2030                         addr, pbl_count, 1, acc, iova_start,
2031                         &nesmr->pbls_used, &nesmr->pbl_4k);
2032
2033         if (ret == 0) {
2034                 nesmr->ibmr.rkey = stag;
2035                 nesmr->ibmr.lkey = stag;
2036                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2037                 ibmr = &nesmr->ibmr;
2038         } else {
2039                 kfree(nesmr);
2040                 ibmr = ERR_PTR(-ENOMEM);
2041         }
2042
2043 reg_phys_err:
2044         /* single PBL case */
2045         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2046         return ibmr;
2047 }
2048
2049
2050 /**
2051  * nes_get_dma_mr
2052  */
2053 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2054 {
2055         u64 kva = 0;
2056
2057         nes_debug(NES_DBG_MR, "\n");
2058
2059         return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
2060 }
2061
2062 /**
2063  * nes_reg_user_mr
2064  */
2065 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2066                 u64 virt, int acc, struct ib_udata *udata)
2067 {
2068         u64 iova_start;
2069         __le64 *pbl;
2070         u64 region_length;
2071         dma_addr_t last_dma_addr = 0;
2072         dma_addr_t first_dma_addr = 0;
2073         struct nes_pd *nespd = to_nespd(pd);
2074         struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2075         struct nes_device *nesdev = nesvnic->nesdev;
2076         struct nes_adapter *nesadapter = nesdev->nesadapter;
2077         struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2078         struct sg_dma_page_iter dma_iter;
2079         struct nes_ucontext *nes_ucontext = rdma_udata_to_drv_context(
2080                 udata, struct nes_ucontext, ibucontext);
2081         struct nes_pbl *nespbl;
2082         struct nes_mr *nesmr;
2083         struct ib_umem *region;
2084         struct nes_mem_reg_req req;
2085         struct nes_vpbl vpbl;
2086         struct nes_root_vpbl root_vpbl;
2087         int page_index;
2088         int page_count = 0;
2089         int err, pbl_depth = 0;
2090         int ret;
2091         u32 stag;
2092         u32 stag_index = 0;
2093         u32 next_stag_index;
2094         u32 driver_key;
2095         u32 root_pbl_index = 0;
2096         u32 cur_pbl_index = 0;
2097         u32 skip_pages;
2098         u16 pbl_count;
2099         u8 single_page = 1;
2100         u8 stag_key;
2101
2102         region = ib_umem_get(udata, start, length, acc, 0);
2103         if (IS_ERR(region)) {
2104                 return (struct ib_mr *)region;
2105         }
2106
2107         nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2108                         " offset = %u, page size = %lu.\n",
2109                         (unsigned long int)start, (unsigned long int)virt, (u32)length,
2110                         ib_umem_offset(region), BIT(region->page_shift));
2111
2112         skip_pages = ((u32)ib_umem_offset(region)) >> 12;
2113
2114         if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2115                 ib_umem_release(region);
2116                 return ERR_PTR(-EFAULT);
2117         }
2118         nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2119
2120         switch (req.reg_type) {
2121                 case IWNES_MEMREG_TYPE_MEM:
2122                         pbl_depth = 0;
2123                         region_length = 0;
2124                         vpbl.pbl_vbase = NULL;
2125                         root_vpbl.pbl_vbase = NULL;
2126                         root_vpbl.pbl_pbase = 0;
2127
2128                         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2129                         stag_key = (u8)next_stag_index;
2130
2131                         driver_key = next_stag_index & 0x70000000;
2132
2133                         next_stag_index >>= 8;
2134                         next_stag_index %= nesadapter->max_mr;
2135
2136                         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2137                                         nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
2138                         if (err) {
2139                                 ib_umem_release(region);
2140                                 return ERR_PTR(err);
2141                         }
2142
2143                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2144                         if (!nesmr) {
2145                                 ib_umem_release(region);
2146                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2147                                 return ERR_PTR(-ENOMEM);
2148                         }
2149                         nesmr->region = region;
2150
2151                         for_each_sg_dma_page (region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2152
2153                                 region_length += PAGE_SIZE;
2154                                 region_length -= skip_pages << 12;
2155                                 skip_pages = 0;
2156                                 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
2157                                         goto enough_pages;
2158                                 if ((page_count & 0x01FF) == 0) {
2159                                         if (page_count >= 1024 * 512) {
2160                                                 ib_umem_release(region);
2161                                                 nes_free_resource(nesadapter,
2162                                                                   nesadapter->allocated_mrs, stag_index);
2163                                                 kfree(nesmr);
2164                                                 ibmr = ERR_PTR(-E2BIG);
2165                                                 goto reg_user_mr_err;
2166                                         }
2167                                         if (root_pbl_index == 1) {
2168                                                 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2169                                                                 8192, &root_vpbl.pbl_pbase);
2170                                                 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2171                                                           root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2172                                                 if (!root_vpbl.pbl_vbase) {
2173                                                         ib_umem_release(region);
2174                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2175                                                                             vpbl.pbl_pbase);
2176                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2177                                                                           stag_index);
2178                                                         kfree(nesmr);
2179                                                         ibmr = ERR_PTR(-ENOMEM);
2180                                                         goto reg_user_mr_err;
2181                                                 }
2182                                                 root_vpbl.leaf_vpbl = kcalloc(1024,
2183                                                                               sizeof(*root_vpbl.leaf_vpbl),
2184                                                                               GFP_KERNEL);
2185                                                 if (!root_vpbl.leaf_vpbl) {
2186                                                         ib_umem_release(region);
2187                                                         pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2188                                                                             root_vpbl.pbl_pbase);
2189                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2190                                                                             vpbl.pbl_pbase);
2191                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2192                                                                           stag_index);
2193                                                         kfree(nesmr);
2194                                                         ibmr = ERR_PTR(-ENOMEM);
2195                                                         goto reg_user_mr_err;
2196                                                 }
2197                                                 root_vpbl.pbl_vbase[0].pa_low =
2198                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2199                                                 root_vpbl.pbl_vbase[0].pa_high =
2200                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2201                                                 root_vpbl.leaf_vpbl[0] = vpbl;
2202                                         }
2203                                         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2204                                                         &vpbl.pbl_pbase);
2205                                         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2206                                                   vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2207                                         if (!vpbl.pbl_vbase) {
2208                                                 ib_umem_release(region);
2209                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2210                                                 ibmr = ERR_PTR(-ENOMEM);
2211                                                 kfree(nesmr);
2212                                                 goto reg_user_mr_err;
2213                                         }
2214                                         if (1 <= root_pbl_index) {
2215                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2216                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2217                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2218                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2219                                                 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2220                                         }
2221                                         root_pbl_index++;
2222                                         cur_pbl_index = 0;
2223                                 }
2224                                 if (single_page) {
2225                                         if (page_count != 0) {
2226                                                 if ((last_dma_addr + 4096) != sg_page_iter_dma_address(&dma_iter))
2227                                                         single_page = 0;
2228                                                 last_dma_addr = sg_page_iter_dma_address(&dma_iter);
2229                                         } else {
2230                                                 first_dma_addr = sg_page_iter_dma_address(&dma_iter);
2231                                                 last_dma_addr = first_dma_addr;
2232                                         }
2233                                 }
2234
2235                                 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2236                                                 cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2237                                 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2238                                                 cpu_to_le32((u32)((u64)(sg_page_iter_dma_address(&dma_iter))));
2239                                 cur_pbl_index++;
2240                                 page_count++;
2241                         }
2242
2243 enough_pages:
2244                         nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2245                                         " stag_key=0x%08x\n",
2246                                         stag_index, driver_key, stag_key);
2247                         stag = stag_index << 8;
2248                         stag |= driver_key;
2249                         stag += (u32)stag_key;
2250
2251                         iova_start = virt;
2252                         /* Make the leaf PBL the root if only one PBL */
2253                         if (root_pbl_index == 1) {
2254                                 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2255                         }
2256
2257                         if (single_page) {
2258                                 pbl_count = 0;
2259                         } else {
2260                                 pbl_count = root_pbl_index;
2261                                 first_dma_addr = 0;
2262                         }
2263                         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2264                                         " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2265                                         stag, (unsigned int)iova_start,
2266                                         (unsigned int)region_length, stag_index,
2267                                         (unsigned long long)region->length, pbl_count);
2268                         ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2269                                          first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2270                                          &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2271
2272                         nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2273
2274                         if (ret == 0) {
2275                                 nesmr->ibmr.rkey = stag;
2276                                 nesmr->ibmr.lkey = stag;
2277                                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2278                                 ibmr = &nesmr->ibmr;
2279                         } else {
2280                                 ib_umem_release(region);
2281                                 kfree(nesmr);
2282                                 ibmr = ERR_PTR(-ENOMEM);
2283                         }
2284
2285 reg_user_mr_err:
2286                         /* free the resources */
2287                         if (root_pbl_index == 1) {
2288                                 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2289                                                 vpbl.pbl_pbase);
2290                         } else {
2291                                 for (page_index=0; page_index<root_pbl_index; page_index++) {
2292                                         pci_free_consistent(nesdev->pcidev, 4096,
2293                                                         root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2294                                                         root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2295                                 }
2296                                 kfree(root_vpbl.leaf_vpbl);
2297                                 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2298                                                 root_vpbl.pbl_pbase);
2299                         }
2300
2301                         nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2302
2303                         return ibmr;
2304                 case IWNES_MEMREG_TYPE_QP:
2305                 case IWNES_MEMREG_TYPE_CQ:
2306                         if (!region->length) {
2307                                 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2308                                 ib_umem_release(region);
2309                                 return ERR_PTR(-EINVAL);
2310                         }
2311                         nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2312                         if (!nespbl) {
2313                                 ib_umem_release(region);
2314                                 return ERR_PTR(-ENOMEM);
2315                         }
2316                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2317                         if (!nesmr) {
2318                                 ib_umem_release(region);
2319                                 kfree(nespbl);
2320                                 return ERR_PTR(-ENOMEM);
2321                         }
2322                         nesmr->region = region;
2323                         pbl_depth = region->length >> 12;
2324                         pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2325                         nespbl->pbl_size = pbl_depth*sizeof(u64);
2326                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2327                                 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2328                         } else {
2329                                 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2330                         }
2331
2332                         nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2333                                         nespbl->pbl_size, pbl_depth);
2334                         pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2335                                         &nespbl->pbl_pbase);
2336                         if (!pbl) {
2337                                 ib_umem_release(region);
2338                                 kfree(nesmr);
2339                                 kfree(nespbl);
2340                                 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2341                                 return ERR_PTR(-ENOMEM);
2342                         }
2343
2344                         nespbl->pbl_vbase = (u64 *)pbl;
2345                         nespbl->user_base = start;
2346                         nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2347                                         " pbl_vbase=%p user_base=0x%lx\n",
2348                                   nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2349                                   (void *) nespbl->pbl_vbase, nespbl->user_base);
2350
2351                         nespbl->page = sg_page(region->sg_head.sgl);
2352                         for_each_sg_dma_page(region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2353                                 ((__le32 *)pbl)[0] = cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2354                                 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)(sg_page_iter_dma_address(&dma_iter)))>>32);
2355                                 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2356                                           (unsigned long long)*pbl,
2357                                           le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2358                                 pbl++;
2359                         }
2360
2361                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2362                                 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2363                         } else {
2364                                 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2365                         }
2366                         nesmr->ibmr.rkey = -1;
2367                         nesmr->ibmr.lkey = -1;
2368                         nesmr->mode = req.reg_type;
2369                         return &nesmr->ibmr;
2370         }
2371
2372         ib_umem_release(region);
2373         return ERR_PTR(-ENOSYS);
2374 }
2375
2376
2377 /**
2378  * nes_dereg_mr
2379  */
2380 static int nes_dereg_mr(struct ib_mr *ib_mr)
2381 {
2382         struct nes_mr *nesmr = to_nesmr(ib_mr);
2383         struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2384         struct nes_device *nesdev = nesvnic->nesdev;
2385         struct nes_adapter *nesadapter = nesdev->nesadapter;
2386         struct nes_hw_cqp_wqe *cqp_wqe;
2387         struct nes_cqp_request *cqp_request;
2388         unsigned long flags;
2389         int ret;
2390         u16 major_code;
2391         u16 minor_code;
2392
2393
2394         if (nesmr->pages)
2395                 pci_free_consistent(nesdev->pcidev,
2396                                     nesmr->max_pages * sizeof(u64),
2397                                     nesmr->pages,
2398                                     nesmr->paddr);
2399
2400         if (nesmr->region) {
2401                 ib_umem_release(nesmr->region);
2402         }
2403         if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2404                 kfree(nesmr);
2405                 return 0;
2406         }
2407
2408         /* Deallocate the region with the adapter */
2409
2410         cqp_request = nes_get_cqp_request(nesdev);
2411         if (cqp_request == NULL) {
2412                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2413                 return -ENOMEM;
2414         }
2415         cqp_request->waiting = 1;
2416         cqp_wqe = &cqp_request->cqp_wqe;
2417
2418         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2419         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2420                         NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2421                         NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2422         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2423
2424         atomic_set(&cqp_request->refcount, 2);
2425         nes_post_cqp_request(nesdev, cqp_request);
2426
2427         /* Wait for CQP */
2428         nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2429         ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2430                         NES_EVENT_TIMEOUT);
2431         nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2432                         " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2433                         ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2434
2435         major_code = cqp_request->major_code;
2436         minor_code = cqp_request->minor_code;
2437
2438         nes_put_cqp_request(nesdev, cqp_request);
2439
2440         if (!ret) {
2441                 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2442                                 " ib_mr=%p, rkey = 0x%08X\n",
2443                                 ib_mr, ib_mr->rkey);
2444                 return -ETIME;
2445         } else if (major_code) {
2446                 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2447                                 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2448                                 major_code, minor_code, ib_mr, ib_mr->rkey);
2449                 return -EIO;
2450         }
2451
2452         if (nesmr->pbls_used != 0) {
2453                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2454                 if (nesmr->pbl_4k) {
2455                         nesadapter->free_4kpbl += nesmr->pbls_used;
2456                         if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2457                                 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2458                                         "exceeded the max(%u)\n",
2459                                         nesadapter->free_4kpbl,
2460                                         nesadapter->max_4kpbl);
2461                 } else {
2462                         nesadapter->free_256pbl += nesmr->pbls_used;
2463                         if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2464                                 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2465                                         "exceeded the max(%u)\n",
2466                                         nesadapter->free_256pbl,
2467                                         nesadapter->max_256pbl);
2468                 }
2469                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2470         }
2471         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2472                         (ib_mr->rkey & 0x0fffff00) >> 8);
2473
2474         kfree(nesmr);
2475
2476         return 0;
2477 }
2478
2479
2480 /**
2481  * show_rev
2482  */
2483 static ssize_t hw_rev_show(struct device *dev,
2484                            struct device_attribute *attr, char *buf)
2485 {
2486         struct nes_ib_device *nesibdev =
2487                 rdma_device_to_drv_device(dev, struct nes_ib_device, ibdev);
2488         struct nes_vnic *nesvnic = nesibdev->nesvnic;
2489
2490         nes_debug(NES_DBG_INIT, "\n");
2491         return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2492 }
2493 static DEVICE_ATTR_RO(hw_rev);
2494
2495 /**
2496  * show_hca
2497  */
2498 static ssize_t hca_type_show(struct device *dev,
2499                              struct device_attribute *attr, char *buf)
2500 {
2501         nes_debug(NES_DBG_INIT, "\n");
2502         return sprintf(buf, "NES020\n");
2503 }
2504 static DEVICE_ATTR_RO(hca_type);
2505
2506 /**
2507  * show_board
2508  */
2509 static ssize_t board_id_show(struct device *dev,
2510                              struct device_attribute *attr, char *buf)
2511 {
2512         nes_debug(NES_DBG_INIT, "\n");
2513         return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2514 }
2515 static DEVICE_ATTR_RO(board_id);
2516
2517 static struct attribute *nes_dev_attributes[] = {
2518         &dev_attr_hw_rev.attr,
2519         &dev_attr_hca_type.attr,
2520         &dev_attr_board_id.attr,
2521         NULL
2522 };
2523
2524 static const struct attribute_group nes_attr_group = {
2525         .attrs = nes_dev_attributes,
2526 };
2527
2528 /**
2529  * nes_query_qp
2530  */
2531 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2532                 int attr_mask, struct ib_qp_init_attr *init_attr)
2533 {
2534         struct nes_qp *nesqp = to_nesqp(ibqp);
2535
2536         nes_debug(NES_DBG_QP, "\n");
2537
2538         attr->qp_access_flags = 0;
2539         attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2540         attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2541         attr->cap.max_recv_sge = 1;
2542         if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2543                 attr->cap.max_inline_data = 0;
2544         else
2545                 attr->cap.max_inline_data = 64;
2546
2547         init_attr->event_handler = nesqp->ibqp.event_handler;
2548         init_attr->qp_context = nesqp->ibqp.qp_context;
2549         init_attr->send_cq = nesqp->ibqp.send_cq;
2550         init_attr->recv_cq = nesqp->ibqp.recv_cq;
2551         init_attr->srq = nesqp->ibqp.srq;
2552         init_attr->cap = attr->cap;
2553
2554         return 0;
2555 }
2556
2557
2558 /**
2559  * nes_hw_modify_qp
2560  */
2561 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2562                 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2563 {
2564         struct nes_hw_cqp_wqe *cqp_wqe;
2565         /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2566         /* struct iw_cm_event cm_event; */
2567         struct nes_cqp_request *cqp_request;
2568         int ret;
2569         u16 major_code;
2570
2571         nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2572                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2573
2574         cqp_request = nes_get_cqp_request(nesdev);
2575         if (cqp_request == NULL) {
2576                 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2577                 return -ENOMEM;
2578         }
2579         if (wait_completion) {
2580                 cqp_request->waiting = 1;
2581         } else {
2582                 cqp_request->waiting = 0;
2583         }
2584         cqp_wqe = &cqp_request->cqp_wqe;
2585
2586         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2587                         NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2588         nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2589                         next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2590         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2591         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2592         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2593
2594         /* If sending a terminate message, fill in the length (in words) */
2595         if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2596             !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2597                 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2598                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2599         }
2600
2601         atomic_set(&cqp_request->refcount, 2);
2602         nes_post_cqp_request(nesdev, cqp_request);
2603
2604         /* Wait for CQP */
2605         if (wait_completion) {
2606                 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2607                                 nesqp->hwqp.qp_id); */
2608                 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2609                                 NES_EVENT_TIMEOUT);
2610                 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2611                                 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2612                                 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2613                 major_code = cqp_request->major_code;
2614                 if (major_code) {
2615                         nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2616                                         "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2617                                         nesqp->hwqp.qp_id, cqp_request->major_code,
2618                                         cqp_request->minor_code, next_iwarp_state);
2619                 }
2620
2621                 nes_put_cqp_request(nesdev, cqp_request);
2622
2623                 if (!ret)
2624                         return -ETIME;
2625                 else if (major_code)
2626                         return -EIO;
2627                 else
2628                         return 0;
2629         } else {
2630                 return 0;
2631         }
2632 }
2633
2634
2635 /**
2636  * nes_modify_qp
2637  */
2638 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2639                 int attr_mask, struct ib_udata *udata)
2640 {
2641         struct nes_qp *nesqp = to_nesqp(ibqp);
2642         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2643         struct nes_device *nesdev = nesvnic->nesdev;
2644         /* u32 cqp_head; */
2645         /* u32 counter; */
2646         u32 next_iwarp_state = 0;
2647         int err;
2648         unsigned long qplockflags;
2649         int ret;
2650         u16 original_last_aeq;
2651         u8 issue_modify_qp = 0;
2652         u8 dont_wait = 0;
2653
2654         nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2655                         " iwarp_state=0x%X, refcount=%d\n",
2656                         nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2657                         nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2658
2659         spin_lock_irqsave(&nesqp->lock, qplockflags);
2660
2661         nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2662                         " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2663                         nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2664                         nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2665
2666         if (attr_mask & IB_QP_STATE) {
2667                 switch (attr->qp_state) {
2668                         case IB_QPS_INIT:
2669                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2670                                                 nesqp->hwqp.qp_id);
2671                                 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2672                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2673                                         return -EINVAL;
2674                                 }
2675                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2676                                 issue_modify_qp = 1;
2677                                 break;
2678                         case IB_QPS_RTR:
2679                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2680                                                 nesqp->hwqp.qp_id);
2681                                 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2682                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2683                                         return -EINVAL;
2684                                 }
2685                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2686                                 issue_modify_qp = 1;
2687                                 break;
2688                         case IB_QPS_RTS:
2689                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2690                                                 nesqp->hwqp.qp_id);
2691                                 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2692                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2693                                         return -EINVAL;
2694                                 }
2695                                 if (nesqp->cm_id == NULL) {
2696                                         nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2697                                                         nesqp->hwqp.qp_id );
2698                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2699                                         return -EINVAL;
2700                                 }
2701                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2702                                 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2703                                         next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2704                                                         NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2705                                 issue_modify_qp = 1;
2706                                 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2707                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2708                                 nesqp->hte_added = 1;
2709                                 break;
2710                         case IB_QPS_SQD:
2711                                 issue_modify_qp = 1;
2712                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2713                                                 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2714                                 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2715                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2716                                         return 0;
2717                                 } else {
2718                                         if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2719                                                 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2720                                                                 " ignored due to current iWARP state\n",
2721                                                                 nesqp->hwqp.qp_id);
2722                                                 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2723                                                 return -EINVAL;
2724                                         }
2725                                         if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2726                                                 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2727                                                                 " already done based on hw state.\n",
2728                                                                 nesqp->hwqp.qp_id);
2729                                                 issue_modify_qp = 0;
2730                                         }
2731                                         switch (nesqp->hw_iwarp_state) {
2732                                                 case NES_AEQE_IWARP_STATE_CLOSING:
2733                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2734                                                         break;
2735                                                 case NES_AEQE_IWARP_STATE_TERMINATE:
2736                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2737                                                         break;
2738                                                 case NES_AEQE_IWARP_STATE_ERROR:
2739                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2740                                                         break;
2741                                                 default:
2742                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2743                                                         nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2744                                                         break;
2745                                         }
2746                                 }
2747                                 break;
2748                         case IB_QPS_SQE:
2749                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2750                                                 nesqp->hwqp.qp_id);
2751                                 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2752                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2753                                         return -EINVAL;
2754                                 }
2755                                 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2756                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2757                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2758                                 issue_modify_qp = 1;
2759                                 break;
2760                         case IB_QPS_ERR:
2761                         case IB_QPS_RESET:
2762                                 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2763                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2764                                         return -EINVAL;
2765                                 }
2766                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2767                                                 nesqp->hwqp.qp_id);
2768                                 if (nesqp->term_flags)
2769                                         del_timer(&nesqp->terminate_timer);
2770
2771                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2772                                 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2773                                 if (nesqp->hte_added) {
2774                                         nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2775                                         next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2776                                         nesqp->hte_added = 0;
2777                                 }
2778                                 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
2779                                                 (nesdev->iw_status) &&
2780                                                 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2781                                         next_iwarp_state |= NES_CQP_QP_RESET;
2782                                 } else {
2783                                         nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2784                                                         nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2785                                         dont_wait = 1;
2786                                 }
2787                                 issue_modify_qp = 1;
2788                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2789                                 break;
2790                         default:
2791                                 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2792                                 return -EINVAL;
2793                                 break;
2794                 }
2795
2796                 nesqp->ibqp_state = attr->qp_state;
2797                 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2798                 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2799                                 nesqp->iwarp_state);
2800         }
2801
2802         if (attr_mask & IB_QP_ACCESS_FLAGS) {
2803                 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2804                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2805                                         NES_QPCONTEXT_MISC_RDMA_READ_EN);
2806                         issue_modify_qp = 1;
2807                 }
2808                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2809                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2810                         issue_modify_qp = 1;
2811                 }
2812                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2813                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2814                         issue_modify_qp = 1;
2815                 }
2816                 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2817                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2818                         issue_modify_qp = 1;
2819                 }
2820
2821                 if (nesqp->user_mode) {
2822                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2823                                         NES_QPCONTEXT_MISC_RDMA_READ_EN);
2824                         issue_modify_qp = 1;
2825                 }
2826         }
2827
2828         original_last_aeq = nesqp->last_aeq;
2829         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2830
2831         nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2832
2833         ret = 0;
2834
2835
2836         if (issue_modify_qp) {
2837                 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
2838                 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
2839                 if (ret)
2840                         nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2841                                         " failed for QP%u.\n",
2842                                         next_iwarp_state, nesqp->hwqp.qp_id);
2843
2844         }
2845
2846         if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2847                 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2848                                 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2849                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2850                                 original_last_aeq, nesqp->last_aeq);
2851                 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
2852                         if (dont_wait) {
2853                                 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2854                                         nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2855                                                         " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2856                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2857                                                         original_last_aeq, nesqp->last_aeq);
2858                                         /* this one is for the cm_disconnect thread */
2859                                         spin_lock_irqsave(&nesqp->lock, qplockflags);
2860                                         nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2861                                         nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2862                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2863                                         nes_cm_disconn(nesqp);
2864                                 } else {
2865                                         nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2866                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2867                                 }
2868                         } else {
2869                                 spin_lock_irqsave(&nesqp->lock, qplockflags);
2870                                 if (nesqp->cm_id) {
2871                                         /* These two are for the timer thread */
2872                                         if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
2873                                                 nesqp->cm_id->add_ref(nesqp->cm_id);
2874                                                 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2875                                                                 " need ae to finish up, original_last_aeq = 0x%04X."
2876                                                                 " last_aeq = 0x%04X, scheduling timer.\n",
2877                                                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2878                                                                 original_last_aeq, nesqp->last_aeq);
2879                                                 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
2880                                         }
2881                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2882                                 } else {
2883                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2884                                         nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2885                                                         " need ae to finish up, original_last_aeq = 0x%04X."
2886                                                         " last_aeq = 0x%04X.\n",
2887                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2888                                                         original_last_aeq, nesqp->last_aeq);
2889                                 }
2890                         }
2891                 } else {
2892                         nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2893                                         " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2894                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2895                                         original_last_aeq, nesqp->last_aeq);
2896                 }
2897         } else {
2898                 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2899                                 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2900                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2901                                 original_last_aeq, nesqp->last_aeq);
2902         }
2903
2904         err = 0;
2905
2906         nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
2907                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2908
2909         return err;
2910 }
2911
2912 static inline void
2913 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, const struct ib_send_wr *ib_wr,
2914                  u32 uselkey)
2915 {
2916         int sge_index;
2917         int total_payload_length = 0;
2918         for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
2919                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
2920                         ib_wr->sg_list[sge_index].addr);
2921                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
2922                         ib_wr->sg_list[sge_index].length);
2923                 if (uselkey)
2924                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
2925                                                 (ib_wr->sg_list[sge_index].lkey));
2926                 else
2927                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
2928
2929                 total_payload_length += ib_wr->sg_list[sge_index].length;
2930         }
2931         nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
2932                         total_payload_length);
2933         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
2934                                 total_payload_length);
2935 }
2936
2937 /**
2938  * nes_post_send
2939  */
2940 static int nes_post_send(struct ib_qp *ibqp, const struct ib_send_wr *ib_wr,
2941                          const struct ib_send_wr **bad_wr)
2942 {
2943         u64 u64temp;
2944         unsigned long flags = 0;
2945         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2946         struct nes_device *nesdev = nesvnic->nesdev;
2947         struct nes_qp *nesqp = to_nesqp(ibqp);
2948         struct nes_hw_qp_wqe *wqe;
2949         int err = 0;
2950         u32 qsize = nesqp->hwqp.sq_size;
2951         u32 head;
2952         u32 wqe_misc = 0;
2953         u32 wqe_count = 0;
2954         u32 counter;
2955
2956         if (nesqp->ibqp_state > IB_QPS_RTS) {
2957                 err = -EINVAL;
2958                 goto out;
2959         }
2960
2961         spin_lock_irqsave(&nesqp->lock, flags);
2962
2963         head = nesqp->hwqp.sq_head;
2964
2965         while (ib_wr) {
2966                 /* Check for QP error */
2967                 if (nesqp->term_flags) {
2968                         err = -EINVAL;
2969                         break;
2970                 }
2971
2972                 /* Check for SQ overflow */
2973                 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
2974                         err = -ENOMEM;
2975                         break;
2976                 }
2977
2978                 wqe = &nesqp->hwqp.sq_vbase[head];
2979                 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
2980                                 nesqp->hwqp.qp_id, wqe, head); */
2981                 nes_fill_init_qp_wqe(wqe, nesqp, head);
2982                 u64temp = (u64)(ib_wr->wr_id);
2983                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
2984                                         u64temp);
2985                 switch (ib_wr->opcode) {
2986                 case IB_WR_SEND:
2987                 case IB_WR_SEND_WITH_INV:
2988                         if (IB_WR_SEND == ib_wr->opcode) {
2989                                 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2990                                         wqe_misc = NES_IWARP_SQ_OP_SENDSE;
2991                                 else
2992                                         wqe_misc = NES_IWARP_SQ_OP_SEND;
2993                         } else {
2994                                 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2995                                         wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
2996                                 else
2997                                         wqe_misc = NES_IWARP_SQ_OP_SENDINV;
2998
2999                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3000                                                     ib_wr->ex.invalidate_rkey);
3001                         }
3002
3003                         if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3004                                 err = -EINVAL;
3005                                 break;
3006                         }
3007
3008                         if (ib_wr->send_flags & IB_SEND_FENCE)
3009                                 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3010
3011                         if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3012                             ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3013                              (ib_wr->sg_list[0].length <= 64)) {
3014                                 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3015                                        (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3016                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3017                                                     ib_wr->sg_list[0].length);
3018                                 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3019                         } else {
3020                                 fill_wqe_sg_send(wqe, ib_wr, 1);
3021                         }
3022
3023                         break;
3024                 case IB_WR_RDMA_WRITE:
3025                         wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3026                         if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3027                                 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3028                                           ib_wr->num_sge, nesdev->nesadapter->max_sge);
3029                                 err = -EINVAL;
3030                                 break;
3031                         }
3032
3033                         if (ib_wr->send_flags & IB_SEND_FENCE)
3034                                 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3035
3036                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3037                                             rdma_wr(ib_wr)->rkey);
3038                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3039                                             rdma_wr(ib_wr)->remote_addr);
3040
3041                         if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3042                             ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3043                              (ib_wr->sg_list[0].length <= 64)) {
3044                                 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3045                                        (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3046                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3047                                                     ib_wr->sg_list[0].length);
3048                                 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3049                         } else {
3050                                 fill_wqe_sg_send(wqe, ib_wr, 1);
3051                         }
3052
3053                         wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3054                                 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3055                         break;
3056                 case IB_WR_RDMA_READ:
3057                 case IB_WR_RDMA_READ_WITH_INV:
3058                         /* iWARP only supports 1 sge for RDMA reads */
3059                         if (ib_wr->num_sge > 1) {
3060                                 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3061                                           ib_wr->num_sge);
3062                                 err = -EINVAL;
3063                                 break;
3064                         }
3065                         if (ib_wr->opcode == IB_WR_RDMA_READ) {
3066                                 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3067                         } else {
3068                                 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3069                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3070                                                     ib_wr->ex.invalidate_rkey);
3071                         }
3072
3073                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3074                                             rdma_wr(ib_wr)->remote_addr);
3075                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3076                                             rdma_wr(ib_wr)->rkey);
3077                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3078                                             ib_wr->sg_list->length);
3079                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3080                                             ib_wr->sg_list->addr);
3081                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3082                                             ib_wr->sg_list->lkey);
3083                         break;
3084                 case IB_WR_LOCAL_INV:
3085                         wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3086                         set_wqe_32bit_value(wqe->wqe_words,
3087                                             NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3088                                             ib_wr->ex.invalidate_rkey);
3089                         break;
3090                 case IB_WR_REG_MR:
3091                 {
3092                         struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3093                         int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3094                         int flags = reg_wr(ib_wr)->access;
3095
3096                         if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3097                                 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3098                                 err = -EINVAL;
3099                                 break;
3100                         }
3101                         wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3102                         set_wqe_64bit_value(wqe->wqe_words,
3103                                             NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3104                                             mr->ibmr.iova);
3105                         set_wqe_32bit_value(wqe->wqe_words,
3106                                             NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3107                                             lower_32_bits(mr->ibmr.length));
3108                         set_wqe_32bit_value(wqe->wqe_words,
3109                                             NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3110                         set_wqe_32bit_value(wqe->wqe_words,
3111                                             NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3112                                             reg_wr(ib_wr)->key);
3113
3114                         if (page_shift == 12) {
3115                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3116                         } else if (page_shift == 21) {
3117                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3118                         } else {
3119                                 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3120                                           " ib_wr=%u, max=1\n", ib_wr->num_sge);
3121                                 err = -EINVAL;
3122                                 break;
3123                         }
3124
3125                         /* Set access_flags */
3126                         wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3127                         if (flags & IB_ACCESS_LOCAL_WRITE)
3128                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3129
3130                         if (flags & IB_ACCESS_REMOTE_WRITE)
3131                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3132
3133                         if (flags & IB_ACCESS_REMOTE_READ)
3134                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3135
3136                         if (flags & IB_ACCESS_MW_BIND)
3137                                 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3138
3139                         /* Fill in PBL info: */
3140                         set_wqe_64bit_value(wqe->wqe_words,
3141                                             NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3142                                             mr->paddr);
3143
3144                         set_wqe_32bit_value(wqe->wqe_words,
3145                                             NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3146                                             mr->npages * 8);
3147
3148                         nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3149                                   "length: %lld, rkey: %0x, pgl_paddr: %llx, "
3150                                   "page_list_len: %u, wqe_misc: %x\n",
3151                                   (unsigned long long) mr->ibmr.iova,
3152                                   mr->ibmr.length,
3153                                   reg_wr(ib_wr)->key,
3154                                   (unsigned long long) mr->paddr,
3155                                   mr->npages,
3156                                   wqe_misc);
3157                         break;
3158                 }
3159                 default:
3160                         /* error */
3161                         err = -EINVAL;
3162                         break;
3163                 }
3164
3165                 if (err)
3166                         break;
3167
3168                 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3169                         wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3170
3171                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3172
3173                 ib_wr = ib_wr->next;
3174                 head++;
3175                 wqe_count++;
3176                 if (head >= qsize)
3177                         head = 0;
3178
3179         }
3180
3181         nesqp->hwqp.sq_head = head;
3182         barrier();
3183         while (wqe_count) {
3184                 counter = min(wqe_count, ((u32)255));
3185                 wqe_count -= counter;
3186                 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3187                                 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3188         }
3189
3190         spin_unlock_irqrestore(&nesqp->lock, flags);
3191
3192 out:
3193         if (err)
3194                 *bad_wr = ib_wr;
3195         return err;
3196 }
3197
3198
3199 /**
3200  * nes_post_recv
3201  */
3202 static int nes_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *ib_wr,
3203                          const struct ib_recv_wr **bad_wr)
3204 {
3205         u64 u64temp;
3206         unsigned long flags = 0;
3207         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3208         struct nes_device *nesdev = nesvnic->nesdev;
3209         struct nes_qp *nesqp = to_nesqp(ibqp);
3210         struct nes_hw_qp_wqe *wqe;
3211         int err = 0;
3212         int sge_index;
3213         u32 qsize = nesqp->hwqp.rq_size;
3214         u32 head;
3215         u32 wqe_count = 0;
3216         u32 counter;
3217         u32 total_payload_length;
3218
3219         if (nesqp->ibqp_state > IB_QPS_RTS) {
3220                 err = -EINVAL;
3221                 goto out;
3222         }
3223
3224         spin_lock_irqsave(&nesqp->lock, flags);
3225
3226         head = nesqp->hwqp.rq_head;
3227
3228         while (ib_wr) {
3229                 /* Check for QP error */
3230                 if (nesqp->term_flags) {
3231                         err = -EINVAL;
3232                         break;
3233                 }
3234
3235                 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3236                         err = -EINVAL;
3237                         break;
3238                 }
3239                 /* Check for RQ overflow */
3240                 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3241                         err = -ENOMEM;
3242                         break;
3243                 }
3244
3245                 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3246                 wqe = &nesqp->hwqp.rq_vbase[head];
3247
3248                 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3249                                 nesqp->hwqp.qp_id, wqe, head); */
3250                 nes_fill_init_qp_wqe(wqe, nesqp, head);
3251                 u64temp = (u64)(ib_wr->wr_id);
3252                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3253                                         u64temp);
3254                 total_payload_length = 0;
3255                 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3256                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3257                                         ib_wr->sg_list[sge_index].addr);
3258                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3259                                         ib_wr->sg_list[sge_index].length);
3260                         set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3261                                         ib_wr->sg_list[sge_index].lkey);
3262
3263                         total_payload_length += ib_wr->sg_list[sge_index].length;
3264                 }
3265                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3266                                         total_payload_length);
3267
3268                 ib_wr = ib_wr->next;
3269                 head++;
3270                 wqe_count++;
3271                 if (head >= qsize)
3272                         head = 0;
3273         }
3274
3275         nesqp->hwqp.rq_head = head;
3276         barrier();
3277         while (wqe_count) {
3278                 counter = min(wqe_count, ((u32)255));
3279                 wqe_count -= counter;
3280                 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3281         }
3282
3283         spin_unlock_irqrestore(&nesqp->lock, flags);
3284
3285 out:
3286         if (err)
3287                 *bad_wr = ib_wr;
3288         return err;
3289 }
3290
3291 /**
3292  * nes_drain_sq - drain sq
3293  * @ibqp: pointer to ibqp
3294  */
3295 static void nes_drain_sq(struct ib_qp *ibqp)
3296 {
3297         struct nes_qp *nesqp = to_nesqp(ibqp);
3298
3299         if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3300                 wait_for_completion(&nesqp->sq_drained);
3301 }
3302
3303 /**
3304  * nes_drain_rq - drain rq
3305  * @ibqp: pointer to ibqp
3306  */
3307 static void nes_drain_rq(struct ib_qp *ibqp)
3308 {
3309         struct nes_qp *nesqp = to_nesqp(ibqp);
3310
3311         if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3312                 wait_for_completion(&nesqp->rq_drained);
3313 }
3314
3315 /**
3316  * nes_poll_cq
3317  */
3318 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3319 {
3320         u64 u64temp;
3321         u64 wrid;
3322         unsigned long flags = 0;
3323         struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3324         struct nes_device *nesdev = nesvnic->nesdev;
3325         struct nes_cq *nescq = to_nescq(ibcq);
3326         struct nes_qp *nesqp;
3327         struct nes_hw_cqe cqe;
3328         u32 head;
3329         u32 wq_tail = 0;
3330         u32 cq_size;
3331         u32 cqe_count = 0;
3332         u32 wqe_index;
3333         u32 u32temp;
3334         u32 move_cq_head = 1;
3335         u32 err_code;
3336
3337         nes_debug(NES_DBG_CQ, "\n");
3338
3339         spin_lock_irqsave(&nescq->lock, flags);
3340
3341         head = nescq->hw_cq.cq_head;
3342         cq_size = nescq->hw_cq.cq_size;
3343
3344         while (cqe_count < num_entries) {
3345                 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3346                                 NES_CQE_VALID) == 0)
3347                         break;
3348
3349                 /*
3350                  * Make sure we read CQ entry contents *after*
3351                  * we've checked the valid bit.
3352                  */
3353                 rmb();
3354
3355                 cqe = nescq->hw_cq.cq_vbase[head];
3356                 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3357                 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3358                 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3359                 /* parse CQE, get completion context from WQE (either rq or sq) */
3360                 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3361                                 ((u64)u32temp);
3362
3363                 if (u64temp) {
3364                         nesqp = (struct nes_qp *)(unsigned long)u64temp;
3365                         memset(entry, 0, sizeof *entry);
3366                         if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3367                                 entry->status = IB_WC_SUCCESS;
3368                         } else {
3369                                 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3370                                 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3371                                         entry->status = err_code & 0x0000ffff;
3372
3373                                         /* The rest of the cqe's will be marked as flushed */
3374                                         nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3375                                                 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3376                                                             NES_IWARP_CQE_MINOR_FLUSH);
3377                                 } else
3378                                         entry->status = IB_WC_WR_FLUSH_ERR;
3379                         }
3380
3381                         entry->qp = &nesqp->ibqp;
3382                         entry->src_qp = nesqp->hwqp.qp_id;
3383
3384                         if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3385                                 if (nesqp->skip_lsmm) {
3386                                         nesqp->skip_lsmm = 0;
3387                                         nesqp->hwqp.sq_tail++;
3388                                 }
3389
3390                                 /* Working on a SQ Completion*/
3391                                 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3392                                                 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3393                                                 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3394                                                 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3395                                 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3396                                                 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3397
3398                                 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3399                                                 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3400                                         case NES_IWARP_SQ_OP_RDMAW:
3401                                                 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3402                                                 entry->opcode = IB_WC_RDMA_WRITE;
3403                                                 break;
3404                                         case NES_IWARP_SQ_OP_RDMAR:
3405                                                 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3406                                                 entry->opcode = IB_WC_RDMA_READ;
3407                                                 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3408                                                                 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3409                                                 break;
3410                                         case NES_IWARP_SQ_OP_SENDINV:
3411                                         case NES_IWARP_SQ_OP_SENDSEINV:
3412                                         case NES_IWARP_SQ_OP_SEND:
3413                                         case NES_IWARP_SQ_OP_SENDSE:
3414                                                 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3415                                                 entry->opcode = IB_WC_SEND;
3416                                                 break;
3417                                         case NES_IWARP_SQ_OP_LOCINV:
3418                                                 entry->opcode = IB_WC_LOCAL_INV;
3419                                                 break;
3420                                         case NES_IWARP_SQ_OP_FAST_REG:
3421                                                 entry->opcode = IB_WC_REG_MR;
3422                                                 break;
3423                                 }
3424
3425                                 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3426                                 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3427                                         move_cq_head = 0;
3428                                         wq_tail = nesqp->hwqp.sq_tail;
3429                                 }
3430                         } else {
3431                                 /* Working on a RQ Completion*/
3432                                 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3433                                 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3434                                         ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3435                                 entry->opcode = IB_WC_RECV;
3436
3437                                 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3438                                 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3439                                         move_cq_head = 0;
3440                                         wq_tail = nesqp->hwqp.rq_tail;
3441                                 }
3442                         }
3443
3444                         if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3445                                 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3446                                         complete(&nesqp->sq_drained);
3447                                 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3448                                         complete(&nesqp->rq_drained);
3449                         }
3450
3451                         entry->wr_id = wrid;
3452                         entry++;
3453                         cqe_count++;
3454                 }
3455
3456                 if (move_cq_head) {
3457                         nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3458                         if (++head >= cq_size)
3459                                 head = 0;
3460                         nescq->polled_completions++;
3461
3462                         if ((nescq->polled_completions > (cq_size / 2)) ||
3463                                         (nescq->polled_completions == 255)) {
3464                                 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3465                                         " are pending %u of %u.\n",
3466                                         nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3467                                 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3468                                         nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3469                                 nescq->polled_completions = 0;
3470                         }
3471                 } else {
3472                         /* Update the wqe index and set status to flush */
3473                         wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3474                         wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3475                         nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3476                                 cpu_to_le32(wqe_index);
3477                         move_cq_head = 1; /* ready for next pass */
3478                 }
3479         }
3480
3481         if (nescq->polled_completions) {
3482                 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3483                                 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3484                 nescq->polled_completions = 0;
3485         }
3486
3487         nescq->hw_cq.cq_head = head;
3488         nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3489                         cqe_count, nescq->hw_cq.cq_number);
3490
3491         spin_unlock_irqrestore(&nescq->lock, flags);
3492
3493         return cqe_count;
3494 }
3495
3496
3497 /**
3498  * nes_req_notify_cq
3499  */
3500 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3501                 {
3502         struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3503         struct nes_device *nesdev = nesvnic->nesdev;
3504         struct nes_cq *nescq = to_nescq(ibcq);
3505         u32 cq_arm;
3506
3507         nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3508                         nescq->hw_cq.cq_number);
3509
3510         cq_arm = nescq->hw_cq.cq_number;
3511         if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3512                 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3513         else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3514                 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3515         else
3516                 return -EINVAL;
3517
3518         nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3519         nes_read32(nesdev->regs+NES_CQE_ALLOC);
3520
3521         return 0;
3522 }
3523
3524 static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3525                               struct ib_port_immutable *immutable)
3526 {
3527         struct ib_port_attr attr;
3528         int err;
3529
3530         immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3531
3532         err = nes_query_port(ibdev, port_num, &attr);
3533         if (err)
3534                 return err;
3535
3536         immutable->pkey_tbl_len = attr.pkey_tbl_len;
3537         immutable->gid_tbl_len = attr.gid_tbl_len;
3538
3539         return 0;
3540 }
3541
3542 static void get_dev_fw_str(struct ib_device *dev, char *str)
3543 {
3544         struct nes_ib_device *nesibdev =
3545                         container_of(dev, struct nes_ib_device, ibdev);
3546         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3547
3548         nes_debug(NES_DBG_INIT, "\n");
3549         snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u",
3550                  (nesvnic->nesdev->nesadapter->firmware_version >> 16),
3551                  (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
3552 }
3553
3554 static const struct ib_device_ops nes_dev_ops = {
3555         .alloc_mr = nes_alloc_mr,
3556         .alloc_mw = nes_alloc_mw,
3557         .alloc_pd = nes_alloc_pd,
3558         .alloc_ucontext = nes_alloc_ucontext,
3559         .create_cq = nes_create_cq,
3560         .create_qp = nes_create_qp,
3561         .dealloc_mw = nes_dealloc_mw,
3562         .dealloc_pd = nes_dealloc_pd,
3563         .dealloc_ucontext = nes_dealloc_ucontext,
3564         .dereg_mr = nes_dereg_mr,
3565         .destroy_cq = nes_destroy_cq,
3566         .destroy_qp = nes_destroy_qp,
3567         .drain_rq = nes_drain_rq,
3568         .drain_sq = nes_drain_sq,
3569         .get_dev_fw_str = get_dev_fw_str,
3570         .get_dma_mr = nes_get_dma_mr,
3571         .get_port_immutable = nes_port_immutable,
3572         .map_mr_sg = nes_map_mr_sg,
3573         .mmap = nes_mmap,
3574         .modify_qp = nes_modify_qp,
3575         .poll_cq = nes_poll_cq,
3576         .post_recv = nes_post_recv,
3577         .post_send = nes_post_send,
3578         .query_device = nes_query_device,
3579         .query_gid = nes_query_gid,
3580         .query_pkey = nes_query_pkey,
3581         .query_port = nes_query_port,
3582         .query_qp = nes_query_qp,
3583         .reg_user_mr = nes_reg_user_mr,
3584         .req_notify_cq = nes_req_notify_cq,
3585         INIT_RDMA_OBJ_SIZE(ib_pd, nes_pd, ibpd),
3586         INIT_RDMA_OBJ_SIZE(ib_ucontext, nes_ucontext, ibucontext),
3587 };
3588
3589 /**
3590  * nes_init_ofa_device
3591  */
3592 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3593 {
3594         struct nes_ib_device *nesibdev;
3595         struct nes_vnic *nesvnic = netdev_priv(netdev);
3596         struct nes_device *nesdev = nesvnic->nesdev;
3597
3598         nesibdev = ib_alloc_device(nes_ib_device, ibdev);
3599         if (nesibdev == NULL) {
3600                 return NULL;
3601         }
3602         nesibdev->ibdev.owner = THIS_MODULE;
3603
3604         nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3605         memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3606         memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3607
3608         nesibdev->ibdev.uverbs_cmd_mask =
3609                         (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3610                         (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3611                         (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3612                         (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3613                         (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3614                         (1ull << IB_USER_VERBS_CMD_REG_MR) |
3615                         (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3616                         (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3617                         (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3618                         (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3619                         (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3620                         (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3621                         (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3622                         (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3623                         (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3624                         (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3625                         (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3626                         (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3627                         (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3628                         (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3629                         (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3630                         (1ull << IB_USER_VERBS_CMD_POST_SEND);
3631
3632         nesibdev->ibdev.phys_port_cnt = 1;
3633         nesibdev->ibdev.num_comp_vectors = 1;
3634         nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3635
3636         nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3637         if (nesibdev->ibdev.iwcm == NULL) {
3638                 ib_dealloc_device(&nesibdev->ibdev);
3639                 return NULL;
3640         }
3641         nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3642         nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3643         nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3644         nesibdev->ibdev.iwcm->connect = nes_connect;
3645         nesibdev->ibdev.iwcm->accept = nes_accept;
3646         nesibdev->ibdev.iwcm->reject = nes_reject;
3647         nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3648         nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3649
3650         ib_set_device_ops(&nesibdev->ibdev, &nes_dev_ops);
3651         memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3652                sizeof(nesibdev->ibdev.iwcm->ifname));
3653
3654         return nesibdev;
3655 }
3656
3657
3658 /**
3659  * nes_handle_delayed_event
3660  */
3661 static void nes_handle_delayed_event(struct timer_list *t)
3662 {
3663         struct nes_vnic *nesvnic = from_timer(nesvnic, t, event_timer);
3664
3665         if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3666                 struct ib_event event;
3667
3668                 event.device = &nesvnic->nesibdev->ibdev;
3669                 if (!event.device)
3670                         goto stop_timer;
3671                 event.event = nesvnic->delayed_event;
3672                 event.element.port_num = nesvnic->logical_port + 1;
3673                 ib_dispatch_event(&event);
3674         }
3675
3676 stop_timer:
3677         nesvnic->event_timer.function = NULL;
3678 }
3679
3680
3681 void  nes_port_ibevent(struct nes_vnic *nesvnic)
3682 {
3683         struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3684         struct nes_device *nesdev = nesvnic->nesdev;
3685         struct ib_event event;
3686         event.device = &nesibdev->ibdev;
3687         event.element.port_num = nesvnic->logical_port + 1;
3688         event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3689
3690         if (!nesvnic->event_timer.function) {
3691                 ib_dispatch_event(&event);
3692                 nesvnic->last_dispatched_event = event.event;
3693                 nesvnic->event_timer.function = nes_handle_delayed_event;
3694                 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3695                 add_timer(&nesvnic->event_timer);
3696         } else {
3697                 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3698         }
3699         nesvnic->delayed_event = event.event;
3700 }
3701
3702
3703 /**
3704  * nes_destroy_ofa_device
3705  */
3706 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3707 {
3708         if (nesibdev == NULL)
3709                 return;
3710
3711         nes_unregister_ofa_device(nesibdev);
3712
3713         kfree(nesibdev->ibdev.iwcm);
3714         ib_dealloc_device(&nesibdev->ibdev);
3715 }
3716
3717
3718 /**
3719  * nes_register_ofa_device
3720  */
3721 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3722 {
3723         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3724         struct nes_device *nesdev = nesvnic->nesdev;
3725         struct nes_adapter *nesadapter = nesdev->nesadapter;
3726         int ret;
3727
3728         rdma_set_device_sysfs_group(&nesvnic->nesibdev->ibdev, &nes_attr_group);
3729         nesvnic->nesibdev->ibdev.driver_id = RDMA_DRIVER_NES;
3730         ret = ib_register_device(&nesvnic->nesibdev->ibdev, "nes%d");
3731         if (ret) {
3732                 return ret;
3733         }
3734
3735         /* Get the resources allocated to this device */
3736         nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3737         nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3738         nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3739         nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3740
3741         nesvnic->of_device_registered = 1;
3742
3743         return 0;
3744 }
3745
3746
3747 /**
3748  * nes_unregister_ofa_device
3749  */
3750 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3751 {
3752         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3753
3754         if (nesvnic->of_device_registered)
3755                 ib_unregister_device(&nesibdev->ibdev);
3756
3757         nesvnic->of_device_registered = 0;
3758 }