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