Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6 into for-linus
[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(-ENOMEM);
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                 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1678                         if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1679                                 list_del(&nespbl->list);
1680                                 err = 0;
1681                                 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1682                                                 nespbl);
1683                                 break;
1684                         }
1685                 }
1686                 if (err) {
1687                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1688                         kfree(nescq);
1689                         return ERR_PTR(err);
1690                 }
1691
1692                 pbl_entries = nespbl->pbl_size >> 3;
1693                 nescq->cq_mem_size = 0;
1694         } else {
1695                 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1696                 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1697                                 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1698
1699                 /* allocate the physical buffer space */
1700                 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1701                                 &nescq->hw_cq.cq_pbase);
1702                 if (!mem) {
1703                         printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1704                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1705                         kfree(nescq);
1706                         return ERR_PTR(-ENOMEM);
1707                 }
1708
1709                 memset(mem, 0, nescq->cq_mem_size);
1710                 nescq->hw_cq.cq_vbase = mem;
1711                 nescq->hw_cq.cq_head = 0;
1712                 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1713                                 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1714                                 (u32)nescq->hw_cq.cq_pbase);
1715         }
1716
1717         nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1718         spin_lock_init(&nescq->lock);
1719
1720         /* send CreateCQ request to CQP */
1721         cqp_request = nes_get_cqp_request(nesdev);
1722         if (cqp_request == NULL) {
1723                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1724                 if (!context)
1725                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1726                                         nescq->hw_cq.cq_pbase);
1727                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1728                 kfree(nescq);
1729                 return ERR_PTR(-ENOMEM);
1730         }
1731         cqp_request->waiting = 1;
1732         cqp_wqe = &cqp_request->cqp_wqe;
1733
1734         opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1735                         NES_CQP_CQ_CHK_OVERFLOW |
1736                         NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1737
1738         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1739
1740         if (pbl_entries != 1) {
1741                 if (pbl_entries > 32) {
1742                         /* use 4k pbl */
1743                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1744                         if (nesadapter->free_4kpbl == 0) {
1745                                 if (cqp_request->dynamic) {
1746                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1747                                         kfree(cqp_request);
1748                                 } else {
1749                                         list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1750                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1751                                 }
1752                                 if (!context)
1753                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1754                                                         nescq->hw_cq.cq_pbase);
1755                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1756                                 kfree(nescq);
1757                                 return ERR_PTR(-ENOMEM);
1758                         } else {
1759                                 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1760                                 nescq->virtual_cq = 2;
1761                                 nesadapter->free_4kpbl--;
1762                         }
1763                 } else {
1764                         /* use 256 byte pbl */
1765                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1766                         if (nesadapter->free_256pbl == 0) {
1767                                 if (cqp_request->dynamic) {
1768                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1769                                         kfree(cqp_request);
1770                                 } else {
1771                                         list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1772                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1773                                 }
1774                                 if (!context)
1775                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1776                                                         nescq->hw_cq.cq_pbase);
1777                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1778                                 kfree(nescq);
1779                                 return ERR_PTR(-ENOMEM);
1780                         } else {
1781                                 opcode |= NES_CQP_CQ_VIRT;
1782                                 nescq->virtual_cq = 1;
1783                                 nesadapter->free_256pbl--;
1784                         }
1785                 }
1786         }
1787
1788         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1789
1790         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1791         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1792         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1793                         (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1794
1795         if (context) {
1796                 if (pbl_entries != 1)
1797                         u64temp = (u64)nespbl->pbl_pbase;
1798                 else
1799                         u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1800                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1801                                 nes_ucontext->mmap_db_index[0]);
1802         } else {
1803                 u64temp = (u64)nescq->hw_cq.cq_pbase;
1804                 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1805         }
1806         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1807         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1808         u64temp = (u64)(unsigned long)&nescq->hw_cq;
1809         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1810                         cpu_to_le32((u32)(u64temp >> 1));
1811         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1812                         cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1813
1814         atomic_set(&cqp_request->refcount, 2);
1815         nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1816
1817         /* Wait for CQP */
1818         nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1819                         nescq->hw_cq.cq_number);
1820         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1821                         NES_EVENT_TIMEOUT * 2);
1822         nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1823                         nescq->hw_cq.cq_number, ret);
1824         if ((!ret) || (cqp_request->major_code)) {
1825                 if (atomic_dec_and_test(&cqp_request->refcount)) {
1826                         if (cqp_request->dynamic) {
1827                                 kfree(cqp_request);
1828                         } else {
1829                                 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1830                                 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1831                                 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1832                         }
1833                 }
1834                 nes_debug(NES_DBG_CQ, "iWARP CQ%u create timeout expired, major code = 0x%04X,"
1835                                 " minor code = 0x%04X\n",
1836                                 nescq->hw_cq.cq_number, cqp_request->major_code, cqp_request->minor_code);
1837                 if (!context)
1838                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1839                                         nescq->hw_cq.cq_pbase);
1840                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1841                 kfree(nescq);
1842                 return ERR_PTR(-EIO);
1843         } else {
1844                 if (atomic_dec_and_test(&cqp_request->refcount)) {
1845                         if (cqp_request->dynamic) {
1846                                 kfree(cqp_request);
1847                         } else {
1848                                 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1849                                 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1850                                 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1851                         }
1852                 }
1853         }
1854
1855         if (context) {
1856                 /* free the nespbl */
1857                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1858                                 nespbl->pbl_pbase);
1859                 kfree(nespbl);
1860                 resp.cq_id = nescq->hw_cq.cq_number;
1861                 resp.cq_size = nescq->hw_cq.cq_size;
1862                 resp.mmap_db_index = 0;
1863                 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1864                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1865                         kfree(nescq);
1866                         return ERR_PTR(-EFAULT);
1867                 }
1868         }
1869
1870         return &nescq->ibcq;
1871 }
1872
1873
1874 /**
1875  * nes_destroy_cq
1876  */
1877 static int nes_destroy_cq(struct ib_cq *ib_cq)
1878 {
1879         struct nes_cq *nescq;
1880         struct nes_device *nesdev;
1881         struct nes_vnic *nesvnic;
1882         struct nes_adapter *nesadapter;
1883         struct nes_hw_cqp_wqe *cqp_wqe;
1884         struct nes_cqp_request *cqp_request;
1885         unsigned long flags;
1886         u32 opcode = 0;
1887         int ret;
1888
1889         if (ib_cq == NULL)
1890                 return 0;
1891
1892         nescq = to_nescq(ib_cq);
1893         nesvnic = to_nesvnic(ib_cq->device);
1894         nesdev = nesvnic->nesdev;
1895         nesadapter = nesdev->nesadapter;
1896
1897         nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1898
1899         /* Send DestroyCQ request to CQP */
1900         cqp_request = nes_get_cqp_request(nesdev);
1901         if (cqp_request == NULL) {
1902                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1903                 return -ENOMEM;
1904         }
1905         cqp_request->waiting = 1;
1906         cqp_wqe = &cqp_request->cqp_wqe;
1907         opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1908         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1909         if (nescq->virtual_cq == 1) {
1910                 nesadapter->free_256pbl++;
1911                 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1912                         printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1913                                         __FUNCTION__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1914                 }
1915         } else if (nescq->virtual_cq == 2) {
1916                 nesadapter->free_4kpbl++;
1917                 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1918                         printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1919                                         __FUNCTION__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1920                 }
1921                 opcode |= NES_CQP_CQ_4KB_CHUNK;
1922         }
1923
1924         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1925
1926         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1927         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1928         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1929                 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1930         nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1931         atomic_set(&cqp_request->refcount, 2);
1932         nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1933
1934         /* Wait for CQP */
1935         nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1936                         nescq->hw_cq.cq_number);
1937         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1938                         NES_EVENT_TIMEOUT);
1939         nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1940                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1941                         nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1942                         cqp_request->minor_code);
1943         if ((!ret) || (cqp_request->major_code)) {
1944                 if (atomic_dec_and_test(&cqp_request->refcount)) {
1945                         if (cqp_request->dynamic) {
1946                                 kfree(cqp_request);
1947                         } else {
1948                                 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1949                                 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1950                                 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1951                         }
1952                 }
1953                 if (!ret) {
1954                         nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1955                                         nescq->hw_cq.cq_number);
1956                         ret = -ETIME;
1957                 } else {
1958                         nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1959                                         nescq->hw_cq.cq_number);
1960                         ret = -EIO;
1961                 }
1962         } else {
1963                 ret = 0;
1964                 if (atomic_dec_and_test(&cqp_request->refcount)) {
1965                         if (cqp_request->dynamic) {
1966                                 kfree(cqp_request);
1967                         } else {
1968                                 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1969                                 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1970                                 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1971                         }
1972                 }
1973         }
1974
1975         if (nescq->cq_mem_size)
1976                 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1977                                 (void *)nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1978         kfree(nescq);
1979
1980         return ret;
1981 }
1982
1983
1984 /**
1985  * nes_reg_mr
1986  */
1987 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1988                 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1989                 dma_addr_t single_buffer, u16 pbl_count, u16 residual_page_count,
1990                 int acc, u64 *iova_start)
1991 {
1992         struct nes_hw_cqp_wqe *cqp_wqe;
1993         struct nes_cqp_request *cqp_request;
1994         unsigned long flags;
1995         int ret;
1996         struct nes_adapter *nesadapter = nesdev->nesadapter;
1997         /* int count; */
1998         u32 opcode = 0;
1999         u16 major_code;
2000
2001         /* Register the region with the adapter */
2002         cqp_request = nes_get_cqp_request(nesdev);
2003         if (cqp_request == NULL) {
2004                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2005                 return -ENOMEM;
2006         }
2007         cqp_request->waiting = 1;
2008         cqp_wqe = &cqp_request->cqp_wqe;
2009
2010         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2011         /* track PBL resources */
2012         if (pbl_count != 0) {
2013                 if (pbl_count > 1) {
2014                         /* Two level PBL */
2015                         if ((pbl_count+1) > nesadapter->free_4kpbl) {
2016                                 nes_debug(NES_DBG_MR, "Out of 4KB Pbls for two level request.\n");
2017                                 if (cqp_request->dynamic) {
2018                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2019                                         kfree(cqp_request);
2020                                 } else {
2021                                         list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2022                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2023                                 }
2024                                 return -ENOMEM;
2025                         } else {
2026                                 nesadapter->free_4kpbl -= pbl_count+1;
2027                         }
2028                 } else if (residual_page_count > 32) {
2029                         if (pbl_count > nesadapter->free_4kpbl) {
2030                                 nes_debug(NES_DBG_MR, "Out of 4KB Pbls.\n");
2031                                 if (cqp_request->dynamic) {
2032                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2033                                         kfree(cqp_request);
2034                                 } else {
2035                                         list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2036                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2037                                 }
2038                                 return -ENOMEM;
2039                         } else {
2040                                 nesadapter->free_4kpbl -= pbl_count;
2041                         }
2042                 } else {
2043                         if (pbl_count > nesadapter->free_256pbl) {
2044                                 nes_debug(NES_DBG_MR, "Out of 256B Pbls.\n");
2045                                 if (cqp_request->dynamic) {
2046                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2047                                         kfree(cqp_request);
2048                                 } else {
2049                                         list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2050                                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2051                                 }
2052                                 return -ENOMEM;
2053                         } else {
2054                                 nesadapter->free_256pbl -= pbl_count;
2055                         }
2056                 }
2057         }
2058
2059         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2060
2061         opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2062                                         NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2063         if (acc & IB_ACCESS_LOCAL_WRITE)
2064                 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2065         if (acc & IB_ACCESS_REMOTE_WRITE)
2066                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2067         if (acc & IB_ACCESS_REMOTE_READ)
2068                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2069         if (acc & IB_ACCESS_MW_BIND)
2070                 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2071
2072         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2073         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2074         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2075         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2076
2077         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2078                         cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2079         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2080                         cpu_to_le32(nespd->pd_id & 0x00007fff);
2081         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2082
2083         if (pbl_count == 0) {
2084                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2085         } else {
2086                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2087                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2088                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX,
2089                                 (((pbl_count - 1) * 4096) + (residual_page_count*8)));
2090
2091                 if ((pbl_count > 1) || (residual_page_count > 32))
2092                         cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2093         }
2094         barrier();
2095
2096         atomic_set(&cqp_request->refcount, 2);
2097         nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2098
2099         /* Wait for CQP */
2100         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2101                         NES_EVENT_TIMEOUT);
2102         nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2103                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2104                         stag, ret, cqp_request->major_code, cqp_request->minor_code);
2105         major_code = cqp_request->major_code;
2106         if (atomic_dec_and_test(&cqp_request->refcount)) {
2107                 if (cqp_request->dynamic) {
2108                         kfree(cqp_request);
2109                 } else {
2110                         spin_lock_irqsave(&nesdev->cqp.lock, flags);
2111                         list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2112                         spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2113                 }
2114         }
2115         if (!ret)
2116                 return -ETIME;
2117         else if (major_code)
2118                 return -EIO;
2119         else
2120                 return 0;
2121
2122         return 0;
2123 }
2124
2125
2126 /**
2127  * nes_reg_phys_mr
2128  */
2129 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2130                 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2131                 u64 * iova_start)
2132 {
2133         u64 region_length;
2134         struct nes_pd *nespd = to_nespd(ib_pd);
2135         struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2136         struct nes_device *nesdev = nesvnic->nesdev;
2137         struct nes_adapter *nesadapter = nesdev->nesadapter;
2138         struct nes_mr *nesmr;
2139         struct ib_mr *ibmr;
2140         struct nes_vpbl vpbl;
2141         struct nes_root_vpbl root_vpbl;
2142         u32 stag;
2143         u32 i;
2144         u32 stag_index = 0;
2145         u32 next_stag_index = 0;
2146         u32 driver_key = 0;
2147         u32 root_pbl_index = 0;
2148         u32 cur_pbl_index = 0;
2149         int err = 0, pbl_depth = 0;
2150         int ret = 0;
2151         u16 pbl_count = 0;
2152         u8 single_page = 1;
2153         u8 stag_key = 0;
2154
2155         pbl_depth = 0;
2156         region_length = 0;
2157         vpbl.pbl_vbase = NULL;
2158         root_vpbl.pbl_vbase = NULL;
2159         root_vpbl.pbl_pbase = 0;
2160
2161         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2162         stag_key = (u8)next_stag_index;
2163
2164         driver_key = 0;
2165
2166         next_stag_index >>= 8;
2167         next_stag_index %= nesadapter->max_mr;
2168         if (num_phys_buf > (1024*512)) {
2169                 return ERR_PTR(-E2BIG);
2170         }
2171
2172         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2173                         &stag_index, &next_stag_index);
2174         if (err) {
2175                 return ERR_PTR(err);
2176         }
2177
2178         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2179         if (!nesmr) {
2180                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2181                 return ERR_PTR(-ENOMEM);
2182         }
2183
2184         for (i = 0; i < num_phys_buf; i++) {
2185
2186                 if ((i & 0x01FF) == 0) {
2187                         if (root_pbl_index == 1) {
2188                                 /* Allocate the root PBL */
2189                                 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2190                                                 &root_vpbl.pbl_pbase);
2191                                 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2192                                                 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2193                                 if (!root_vpbl.pbl_vbase) {
2194                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2195                                                         vpbl.pbl_pbase);
2196                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2197                                         kfree(nesmr);
2198                                         return ERR_PTR(-ENOMEM);
2199                                 }
2200                                 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2201                                 if (!root_vpbl.leaf_vpbl) {
2202                                         pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2203                                                         root_vpbl.pbl_pbase);
2204                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2205                                                         vpbl.pbl_pbase);
2206                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2207                                         kfree(nesmr);
2208                                         return ERR_PTR(-ENOMEM);
2209                                 }
2210                                 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2211                                 root_vpbl.pbl_vbase[0].pa_high =
2212                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2213                                 root_vpbl.leaf_vpbl[0] = vpbl;
2214                         }
2215                         /* Allocate a 4K buffer for the PBL */
2216                         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2217                                         &vpbl.pbl_pbase);
2218                         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2219                                         vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2220                         if (!vpbl.pbl_vbase) {
2221                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2222                                 ibmr = ERR_PTR(-ENOMEM);
2223                                 kfree(nesmr);
2224                                 goto reg_phys_err;
2225                         }
2226                         /* Fill in the root table */
2227                         if (1 <= root_pbl_index) {
2228                                 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2229                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2230                                 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2231                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2232                                 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2233                         }
2234                         root_pbl_index++;
2235                         cur_pbl_index = 0;
2236                 }
2237                 if (buffer_list[i].addr & ~PAGE_MASK) {
2238                         /* TODO: Unwind allocated buffers */
2239                         nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2240                         nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2241                                         (unsigned int) buffer_list[i].addr);
2242                         ibmr = ERR_PTR(-EINVAL);
2243                         kfree(nesmr);
2244                         goto reg_phys_err;
2245                 }
2246
2247                 if (!buffer_list[i].size) {
2248                         nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2249                         nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2250                         ibmr = ERR_PTR(-EINVAL);
2251                         kfree(nesmr);
2252                         goto reg_phys_err;
2253                 }
2254
2255                 region_length += buffer_list[i].size;
2256                 if ((i != 0) && (single_page)) {
2257                         if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2258                                 single_page = 0;
2259                 }
2260                 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr);
2261                 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2262                                 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2263         }
2264
2265         stag = stag_index << 8;
2266         stag |= driver_key;
2267         stag += (u32)stag_key;
2268
2269         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2270                         " length = 0x%016lX, index = 0x%08X\n",
2271                         stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2272
2273         region_length -= (*iova_start)&PAGE_MASK;
2274
2275         /* Make the leaf PBL the root if only one PBL */
2276         if (root_pbl_index == 1) {
2277                 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2278         }
2279
2280         if (single_page) {
2281                 pbl_count = 0;
2282         } else {
2283                 pbl_count = root_pbl_index;
2284         }
2285         ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2286                         buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start);
2287
2288         if (ret == 0) {
2289                 nesmr->ibmr.rkey = stag;
2290                 nesmr->ibmr.lkey = stag;
2291                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2292                 ibmr = &nesmr->ibmr;
2293                 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2294                 nesmr->pbls_used = pbl_count;
2295                 if (pbl_count > 1) {
2296                         nesmr->pbls_used++;
2297                 }
2298         } else {
2299                 kfree(nesmr);
2300                 ibmr = ERR_PTR(-ENOMEM);
2301         }
2302
2303         reg_phys_err:
2304         /* free the resources */
2305         if (root_pbl_index == 1) {
2306                 /* single PBL case */
2307                 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2308         } else {
2309                 for (i=0; i<root_pbl_index; i++) {
2310                         pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2311                                         root_vpbl.leaf_vpbl[i].pbl_pbase);
2312                 }
2313                 kfree(root_vpbl.leaf_vpbl);
2314                 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2315                                 root_vpbl.pbl_pbase);
2316         }
2317
2318         return ibmr;
2319 }
2320
2321
2322 /**
2323  * nes_get_dma_mr
2324  */
2325 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2326 {
2327         struct ib_phys_buf bl;
2328         u64 kva = 0;
2329
2330         nes_debug(NES_DBG_MR, "\n");
2331
2332         bl.size = (u64)0xffffffffffULL;
2333         bl.addr = 0;
2334         return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2335 }
2336
2337
2338 /**
2339  * nes_reg_user_mr
2340  */
2341 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2342                 u64 virt, int acc, struct ib_udata *udata)
2343 {
2344         u64 iova_start;
2345         __le64 *pbl;
2346         u64 region_length;
2347         dma_addr_t last_dma_addr = 0;
2348         dma_addr_t first_dma_addr = 0;
2349         struct nes_pd *nespd = to_nespd(pd);
2350         struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2351         struct nes_device *nesdev = nesvnic->nesdev;
2352         struct nes_adapter *nesadapter = nesdev->nesadapter;
2353         struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2354         struct ib_umem_chunk *chunk;
2355         struct nes_ucontext *nes_ucontext;
2356         struct nes_pbl *nespbl;
2357         struct nes_mr *nesmr;
2358         struct ib_umem *region;
2359         struct nes_mem_reg_req req;
2360         struct nes_vpbl vpbl;
2361         struct nes_root_vpbl root_vpbl;
2362         int nmap_index, page_index;
2363         int page_count = 0;
2364         int err, pbl_depth = 0;
2365         int chunk_pages;
2366         int ret;
2367         u32 stag;
2368         u32 stag_index = 0;
2369         u32 next_stag_index;
2370         u32 driver_key;
2371         u32 root_pbl_index = 0;
2372         u32 cur_pbl_index = 0;
2373         u32 skip_pages;
2374         u16 pbl_count;
2375         u8 single_page = 1;
2376         u8 stag_key;
2377
2378         region = ib_umem_get(pd->uobject->context, start, length, acc);
2379         if (IS_ERR(region)) {
2380                 return (struct ib_mr *)region;
2381         }
2382
2383         nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2384                         " offset = %u, page size = %u.\n",
2385                         (unsigned long int)start, (unsigned long int)virt, (u32)length,
2386                         region->offset, region->page_size);
2387
2388         skip_pages = ((u32)region->offset) >> 12;
2389
2390         if (ib_copy_from_udata(&req, udata, sizeof(req)))
2391                 return ERR_PTR(-EFAULT);
2392         nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2393
2394         switch (req.reg_type) {
2395                 case IWNES_MEMREG_TYPE_MEM:
2396                         pbl_depth = 0;
2397                         region_length = 0;
2398                         vpbl.pbl_vbase = NULL;
2399                         root_vpbl.pbl_vbase = NULL;
2400                         root_vpbl.pbl_pbase = 0;
2401
2402                         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2403                         stag_key = (u8)next_stag_index;
2404
2405                         driver_key = next_stag_index & 0x70000000;
2406
2407                         next_stag_index >>= 8;
2408                         next_stag_index %= nesadapter->max_mr;
2409
2410                         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2411                                         nesadapter->max_mr, &stag_index, &next_stag_index);
2412                         if (err) {
2413                                 ib_umem_release(region);
2414                                 return ERR_PTR(err);
2415                         }
2416
2417                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2418                         if (!nesmr) {
2419                                 ib_umem_release(region);
2420                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2421                                 return ERR_PTR(-ENOMEM);
2422                         }
2423                         nesmr->region = region;
2424
2425                         list_for_each_entry(chunk, &region->chunk_list, list) {
2426                                 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2427                                                 chunk->nents, chunk->nmap);
2428                                 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2429                                         if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2430                                                 ib_umem_release(region);
2431                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2432                                                 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2433                                                                 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2434                                                 ibmr = ERR_PTR(-EINVAL);
2435                                                 kfree(nesmr);
2436                                                 goto reg_user_mr_err;
2437                                         }
2438
2439                                         if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2440                                                 ib_umem_release(region);
2441                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2442                                                                 stag_index);
2443                                                 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2444                                                 ibmr = ERR_PTR(-EINVAL);
2445                                                 kfree(nesmr);
2446                                                 goto reg_user_mr_err;
2447                                         }
2448
2449                                         region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2450                                         chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2451                                         region_length -= skip_pages << 12;
2452                                         for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2453                                                 skip_pages = 0;
2454                                                 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2455                                                         goto enough_pages;
2456                                                 if ((page_count&0x01FF) == 0) {
2457                                                         if (page_count>(1024*512)) {
2458                                                                 ib_umem_release(region);
2459                                                                 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2460                                                                                 vpbl.pbl_pbase);
2461                                                                 nes_free_resource(nesadapter,
2462                                                                                 nesadapter->allocated_mrs, stag_index);
2463                                                                 kfree(nesmr);
2464                                                                 ibmr = ERR_PTR(-E2BIG);
2465                                                                 goto reg_user_mr_err;
2466                                                         }
2467                                                         if (root_pbl_index == 1) {
2468                                                                 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2469                                                                                 8192, &root_vpbl.pbl_pbase);
2470                                                                 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2471                                                                                 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2472                                                                 if (!root_vpbl.pbl_vbase) {
2473                                                                         ib_umem_release(region);
2474                                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2475                                                                                         vpbl.pbl_pbase);
2476                                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2477                                                                                         stag_index);
2478                                                                         kfree(nesmr);
2479                                                                         ibmr = ERR_PTR(-ENOMEM);
2480                                                                         goto reg_user_mr_err;
2481                                                                 }
2482                                                                 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2483                                                                                 GFP_KERNEL);
2484                                                                 if (!root_vpbl.leaf_vpbl) {
2485                                                                         ib_umem_release(region);
2486                                                                         pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2487                                                                                         root_vpbl.pbl_pbase);
2488                                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2489                                                                                         vpbl.pbl_pbase);
2490                                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2491                                                                                         stag_index);
2492                                                                         kfree(nesmr);
2493                                                                         ibmr = ERR_PTR(-ENOMEM);
2494                                                                         goto reg_user_mr_err;
2495                                                                 }
2496                                                                 root_vpbl.pbl_vbase[0].pa_low =
2497                                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2498                                                                 root_vpbl.pbl_vbase[0].pa_high =
2499                                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2500                                                                 root_vpbl.leaf_vpbl[0] = vpbl;
2501                                                         }
2502                                                         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2503                                                                         &vpbl.pbl_pbase);
2504                                                         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2505                                                                         vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2506                                                         if (!vpbl.pbl_vbase) {
2507                                                                 ib_umem_release(region);
2508                                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2509                                                                 ibmr = ERR_PTR(-ENOMEM);
2510                                                                 kfree(nesmr);
2511                                                                 goto reg_user_mr_err;
2512                                                         }
2513                                                         if (1 <= root_pbl_index) {
2514                                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2515                                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2516                                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2517                                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2518                                                                 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2519                                                         }
2520                                                         root_pbl_index++;
2521                                                         cur_pbl_index = 0;
2522                                                 }
2523                                                 if (single_page) {
2524                                                         if (page_count != 0) {
2525                                                                 if ((last_dma_addr+4096) !=
2526                                                                                 (sg_dma_address(&chunk->page_list[nmap_index])+
2527                                                                                 (page_index*4096)))
2528                                                                         single_page = 0;
2529                                                                 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2530                                                                                 (page_index*4096);
2531                                                         } else {
2532                                                                 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2533                                                                                 (page_index*4096);
2534                                                                 last_dma_addr = first_dma_addr;
2535                                                         }
2536                                                 }
2537
2538                                                 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2539                                                                 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2540                                                                 (page_index*4096)));
2541                                                 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2542                                                                 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2543                                                                 (page_index*4096))) >> 32)));
2544                                                 cur_pbl_index++;
2545                                                 page_count++;
2546                                         }
2547                                 }
2548                         }
2549                         enough_pages:
2550                         nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2551                                         " stag_key=0x%08x\n",
2552                                         stag_index, driver_key, stag_key);
2553                         stag = stag_index << 8;
2554                         stag |= driver_key;
2555                         stag += (u32)stag_key;
2556                         if (stag == 0) {
2557                                 stag = 1;
2558                         }
2559
2560                         iova_start = virt;
2561                         /* Make the leaf PBL the root if only one PBL */
2562                         if (root_pbl_index == 1) {
2563                                 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2564                         }
2565
2566                         if (single_page) {
2567                                 pbl_count = 0;
2568                         } else {
2569                                 pbl_count = root_pbl_index;
2570                                 first_dma_addr = 0;
2571                         }
2572                         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2573                                         " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2574                                         stag, (unsigned int)iova_start,
2575                                         (unsigned int)region_length, stag_index,
2576                                         (unsigned long long)region->length, pbl_count);
2577                         ret = nes_reg_mr( nesdev, nespd, stag, region->length, &root_vpbl,
2578                                         first_dma_addr, pbl_count, (u16)cur_pbl_index, acc, &iova_start);
2579
2580                         nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2581
2582                         if (ret == 0) {
2583                                 nesmr->ibmr.rkey = stag;
2584                                 nesmr->ibmr.lkey = stag;
2585                                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2586                                 ibmr = &nesmr->ibmr;
2587                                 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2588                                 nesmr->pbls_used = pbl_count;
2589                                 if (pbl_count > 1) {
2590                                         nesmr->pbls_used++;
2591                                 }
2592                         } else {
2593                                 ib_umem_release(region);
2594                                 kfree(nesmr);
2595                                 ibmr = ERR_PTR(-ENOMEM);
2596                         }
2597
2598                         reg_user_mr_err:
2599                         /* free the resources */
2600                         if (root_pbl_index == 1) {
2601                                 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2602                                                 vpbl.pbl_pbase);
2603                         } else {
2604                                 for (page_index=0; page_index<root_pbl_index; page_index++) {
2605                                         pci_free_consistent(nesdev->pcidev, 4096,
2606                                                         root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2607                                                         root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2608                                 }
2609                                 kfree(root_vpbl.leaf_vpbl);
2610                                 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2611                                                 root_vpbl.pbl_pbase);
2612                         }
2613
2614                         nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2615
2616                         return ibmr;
2617                         break;
2618                 case IWNES_MEMREG_TYPE_QP:
2619                 case IWNES_MEMREG_TYPE_CQ:
2620                         nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2621                         if (!nespbl) {
2622                                 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2623                                 ib_umem_release(region);
2624                                 return ERR_PTR(-ENOMEM);
2625                         }
2626                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2627                         if (!nesmr) {
2628                                 ib_umem_release(region);
2629                                 kfree(nespbl);
2630                                 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2631                                 return ERR_PTR(-ENOMEM);
2632                         }
2633                         nesmr->region = region;
2634                         nes_ucontext = to_nesucontext(pd->uobject->context);
2635                         pbl_depth = region->length >> 12;
2636                         pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2637                         nespbl->pbl_size = pbl_depth*sizeof(u64);
2638                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2639                                 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2640                         } else {
2641                                 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2642                         }
2643
2644                         nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2645                                         nespbl->pbl_size, pbl_depth);
2646                         pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2647                                         &nespbl->pbl_pbase);
2648                         if (!pbl) {
2649                                 ib_umem_release(region);
2650                                 kfree(nesmr);
2651                                 kfree(nespbl);
2652                                 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2653                                 return ERR_PTR(-ENOMEM);
2654                         }
2655
2656                         nespbl->pbl_vbase = (u64 *)pbl;
2657                         nespbl->user_base = start;
2658                         nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%p,"
2659                                         " pbl_vbase=%p user_base=0x%lx\n",
2660                                         nespbl->pbl_size, (void *)nespbl->pbl_pbase,
2661                                         (void*)nespbl->pbl_vbase, nespbl->user_base);
2662
2663                         list_for_each_entry(chunk, &region->chunk_list, list) {
2664                                 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2665                                         chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2666                                         chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2667                                         nespbl->page = sg_page(&chunk->page_list[0]);
2668                                         for (page_index=0; page_index<chunk_pages; page_index++) {
2669                                                 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2670                                                                 (sg_dma_address(&chunk->page_list[nmap_index])+
2671                                                                 (page_index*4096)));
2672                                                 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2673                                                                 (sg_dma_address(&chunk->page_list[nmap_index])+
2674                                                                 (page_index*4096)))>>32);
2675                                                 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2676                                                                 (unsigned long long)*pbl,
2677                                                                 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2678                                                 pbl++;
2679                                         }
2680                                 }
2681                         }
2682                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2683                                 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2684                         } else {
2685                                 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2686                         }
2687                         nesmr->ibmr.rkey = -1;
2688                         nesmr->ibmr.lkey = -1;
2689                         nesmr->mode = req.reg_type;
2690                         return &nesmr->ibmr;
2691                         break;
2692         }
2693
2694         return ERR_PTR(-ENOSYS);
2695 }
2696
2697
2698 /**
2699  * nes_dereg_mr
2700  */
2701 static int nes_dereg_mr(struct ib_mr *ib_mr)
2702 {
2703         struct nes_mr *nesmr = to_nesmr(ib_mr);
2704         struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2705         struct nes_device *nesdev = nesvnic->nesdev;
2706         struct nes_adapter *nesadapter = nesdev->nesadapter;
2707         struct nes_hw_cqp_wqe *cqp_wqe;
2708         struct nes_cqp_request *cqp_request;
2709         unsigned long flags;
2710         int ret;
2711         u16 major_code;
2712         u16 minor_code;
2713
2714         if (nesmr->region) {
2715                 ib_umem_release(nesmr->region);
2716         }
2717         if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2718                 kfree(nesmr);
2719                 return 0;
2720         }
2721
2722         /* Deallocate the region with the adapter */
2723
2724         cqp_request = nes_get_cqp_request(nesdev);
2725         if (cqp_request == NULL) {
2726                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2727                 return -ENOMEM;
2728         }
2729         cqp_request->waiting = 1;
2730         cqp_wqe = &cqp_request->cqp_wqe;
2731
2732         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2733         if (nesmr->pbls_used != 0) {
2734                 if (nesmr->pbl_4k) {
2735                         nesadapter->free_4kpbl += nesmr->pbls_used;
2736                         if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
2737                                 printk(KERN_ERR PFX "free 4KB PBLs(%u) has exceeded the max(%u)\n",
2738                                                 nesadapter->free_4kpbl, nesadapter->max_4kpbl);
2739                         }
2740                 } else {
2741                         nesadapter->free_256pbl += nesmr->pbls_used;
2742                         if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
2743                                 printk(KERN_ERR PFX "free 256B PBLs(%u) has exceeded the max(%u)\n",
2744                                                 nesadapter->free_256pbl, nesadapter->max_256pbl);
2745                         }
2746                 }
2747         }
2748
2749         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2750         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2751         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2752                         NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2753                         NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2754         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2755
2756         atomic_set(&cqp_request->refcount, 2);
2757         nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2758
2759         /* Wait for CQP */
2760         nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2761         ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2762                         NES_EVENT_TIMEOUT);
2763         nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2764                         " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2765                         ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2766
2767         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2768                         (ib_mr->rkey & 0x0fffff00) >> 8);
2769
2770         kfree(nesmr);
2771
2772         major_code = cqp_request->major_code;
2773         minor_code = cqp_request->minor_code;
2774         if (atomic_dec_and_test(&cqp_request->refcount)) {
2775                 if (cqp_request->dynamic) {
2776                         kfree(cqp_request);
2777                 } else {
2778                         spin_lock_irqsave(&nesdev->cqp.lock, flags);
2779                         list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2780                         spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2781                 }
2782         }
2783         if (!ret) {
2784                 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2785                                 " ib_mr=%p, rkey = 0x%08X\n",
2786                                 ib_mr, ib_mr->rkey);
2787                 return -ETIME;
2788         } else if (major_code) {
2789                 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2790                                 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2791                                 major_code, minor_code, ib_mr, ib_mr->rkey);
2792                 return -EIO;
2793         } else
2794                 return 0;
2795 }
2796
2797
2798 /**
2799  * show_rev
2800  */
2801 static ssize_t show_rev(struct class_device *cdev, char *buf)
2802 {
2803         struct nes_ib_device *nesibdev =
2804                         container_of(cdev, struct nes_ib_device, ibdev.class_dev);
2805         struct nes_vnic *nesvnic = nesibdev->nesvnic;
2806
2807         nes_debug(NES_DBG_INIT, "\n");
2808         return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2809 }
2810
2811
2812 /**
2813  * show_fw_ver
2814  */
2815 static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
2816 {
2817         struct nes_ib_device *nesibdev =
2818                         container_of(cdev, struct nes_ib_device, ibdev.class_dev);
2819         struct nes_vnic *nesvnic = nesibdev->nesvnic;
2820
2821         nes_debug(NES_DBG_INIT, "\n");
2822         return sprintf(buf, "%x.%x.%x\n",
2823                         (int)(nesvnic->nesdev->nesadapter->fw_ver >> 32),
2824                         (int)(nesvnic->nesdev->nesadapter->fw_ver >> 16) & 0xffff,
2825                         (int)(nesvnic->nesdev->nesadapter->fw_ver & 0xffff));
2826 }
2827
2828
2829 /**
2830  * show_hca
2831  */
2832 static ssize_t show_hca(struct class_device *cdev, char *buf)
2833 {
2834         nes_debug(NES_DBG_INIT, "\n");
2835         return sprintf(buf, "NES020\n");
2836 }
2837
2838
2839 /**
2840  * show_board
2841  */
2842 static ssize_t show_board(struct class_device *cdev, char *buf)
2843 {
2844         nes_debug(NES_DBG_INIT, "\n");
2845         return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2846 }
2847
2848
2849 static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2850 static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2851 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2852 static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2853
2854 static struct class_device_attribute *nes_class_attributes[] = {
2855         &class_device_attr_hw_rev,
2856         &class_device_attr_fw_ver,
2857         &class_device_attr_hca_type,
2858         &class_device_attr_board_id
2859 };
2860
2861
2862 /**
2863  * nes_query_qp
2864  */
2865 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2866                 int attr_mask, struct ib_qp_init_attr *init_attr)
2867 {
2868         struct nes_qp *nesqp = to_nesqp(ibqp);
2869
2870         nes_debug(NES_DBG_QP, "\n");
2871
2872         attr->qp_access_flags = 0;
2873         attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2874         attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2875         attr->cap.max_recv_sge = 1;
2876         if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2877                 init_attr->cap.max_inline_data = 0;
2878         } else {
2879                 init_attr->cap.max_inline_data = 64;
2880         }
2881
2882         init_attr->event_handler = nesqp->ibqp.event_handler;
2883         init_attr->qp_context = nesqp->ibqp.qp_context;
2884         init_attr->send_cq = nesqp->ibqp.send_cq;
2885         init_attr->recv_cq = nesqp->ibqp.recv_cq;
2886         init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2887         init_attr->cap = attr->cap;
2888
2889         return 0;
2890 }
2891
2892
2893 /**
2894  * nes_hw_modify_qp
2895  */
2896 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2897                 u32 next_iwarp_state, u32 wait_completion)
2898 {
2899         struct nes_hw_cqp_wqe *cqp_wqe;
2900         /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2901         /* struct iw_cm_event cm_event; */
2902         struct nes_cqp_request *cqp_request;
2903         unsigned long flags;
2904         int ret;
2905         u16 major_code;
2906
2907         nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2908                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2909
2910         cqp_request = nes_get_cqp_request(nesdev);
2911         if (cqp_request == NULL) {
2912                 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2913                 return -ENOMEM;
2914         }
2915         if (wait_completion) {
2916                 cqp_request->waiting = 1;
2917         } else {
2918                 cqp_request->waiting = 0;
2919         }
2920         cqp_wqe = &cqp_request->cqp_wqe;
2921
2922         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2923                         NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2924         nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2925                         next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2926         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2927         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2928         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2929
2930         atomic_set(&cqp_request->refcount, 2);
2931         nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2932
2933         /* Wait for CQP */
2934         if (wait_completion) {
2935                 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2936                                 nesqp->hwqp.qp_id); */
2937                 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2938                                 NES_EVENT_TIMEOUT);
2939                 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2940                                 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2941                                 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2942                 major_code = cqp_request->major_code;
2943                 if (major_code) {
2944                         nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2945                                         "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2946                                         nesqp->hwqp.qp_id, cqp_request->major_code,
2947                                         cqp_request->minor_code, next_iwarp_state);
2948                 }
2949                 if (atomic_dec_and_test(&cqp_request->refcount)) {
2950                         if (cqp_request->dynamic) {
2951                                 kfree(cqp_request);
2952                         } else {
2953                                 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2954                                 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2955                                 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2956                         }
2957                 }
2958                 if (!ret)
2959                         return -ETIME;
2960                 else if (major_code)
2961                         return -EIO;
2962                 else
2963                         return 0;
2964         } else {
2965                 return 0;
2966         }
2967 }
2968
2969
2970 /**
2971  * nes_modify_qp
2972  */
2973 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2974                 int attr_mask, struct ib_udata *udata)
2975 {
2976         struct nes_qp *nesqp = to_nesqp(ibqp);
2977         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2978         struct nes_device *nesdev = nesvnic->nesdev;
2979         /* u32 cqp_head; */
2980         /* u32 counter; */
2981         u32 next_iwarp_state = 0;
2982         int err;
2983         unsigned long qplockflags;
2984         int ret;
2985         u16 original_last_aeq;
2986         u8 issue_modify_qp = 0;
2987         u8 issue_disconnect = 0;
2988         u8 dont_wait = 0;
2989
2990         nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2991                         " iwarp_state=0x%X, refcount=%d\n",
2992                         nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2993                         nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2994
2995         nes_add_ref(&nesqp->ibqp);
2996         spin_lock_irqsave(&nesqp->lock, qplockflags);
2997
2998         nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2999                         " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
3000                         nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
3001                         nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
3002
3003         if (attr_mask & IB_QP_STATE) {
3004                 switch (attr->qp_state) {
3005                         case IB_QPS_INIT:
3006                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
3007                                                 nesqp->hwqp.qp_id);
3008                                 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3009                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3010                                         nes_rem_ref(&nesqp->ibqp);
3011                                         return -EINVAL;
3012                                 }
3013                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3014                                 issue_modify_qp = 1;
3015                                 break;
3016                         case IB_QPS_RTR:
3017                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
3018                                                 nesqp->hwqp.qp_id);
3019                                 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3020                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3021                                         nes_rem_ref(&nesqp->ibqp);
3022                                         return -EINVAL;
3023                                 }
3024                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3025                                 issue_modify_qp = 1;
3026                                 break;
3027                         case IB_QPS_RTS:
3028                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
3029                                                 nesqp->hwqp.qp_id);
3030                                 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
3031                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3032                                         nes_rem_ref(&nesqp->ibqp);
3033                                         return -EINVAL;
3034                                 }
3035                                 if (nesqp->cm_id == NULL) {
3036                                         nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
3037                                                         nesqp->hwqp.qp_id );
3038                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3039                                         nes_rem_ref(&nesqp->ibqp);
3040                                         return -EINVAL;
3041                                 }
3042                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
3043                                 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
3044                                         next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
3045                                                         NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
3046                                 issue_modify_qp = 1;
3047                                 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
3048                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
3049                                 nesqp->hte_added = 1;
3050                                 break;
3051                         case IB_QPS_SQD:
3052                                 issue_modify_qp = 1;
3053                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3054                                                 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3055                                 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3056                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3057                                         nes_rem_ref(&nesqp->ibqp);
3058                                         return 0;
3059                                 } else {
3060                                         if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3061                                                 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3062                                                                 " ignored due to current iWARP state\n",
3063                                                                 nesqp->hwqp.qp_id);
3064                                                 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3065                                                 nes_rem_ref(&nesqp->ibqp);
3066                                                 return -EINVAL;
3067                                         }
3068                                         if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3069                                                 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3070                                                                 " already done based on hw state.\n",
3071                                                                 nesqp->hwqp.qp_id);
3072                                                 issue_modify_qp = 0;
3073                                                 nesqp->in_disconnect = 0;
3074                                         }
3075                                         switch (nesqp->hw_iwarp_state) {
3076                                                 case NES_AEQE_IWARP_STATE_CLOSING:
3077                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3078                                                 case NES_AEQE_IWARP_STATE_TERMINATE:
3079                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3080                                                         break;
3081                                                 case NES_AEQE_IWARP_STATE_ERROR:
3082                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3083                                                         break;
3084                                                 default:
3085                                                         next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3086                                                         nesqp->in_disconnect = 1;
3087                                                         nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3088                                                         break;
3089                                         }
3090                                 }
3091                                 break;
3092                         case IB_QPS_SQE:
3093                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3094                                                 nesqp->hwqp.qp_id);
3095                                 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3096                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3097                                         nes_rem_ref(&nesqp->ibqp);
3098                                         return -EINVAL;
3099                                 }
3100                                 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3101                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3102                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3103                                 issue_modify_qp = 1;
3104                                 nesqp->in_disconnect = 1;
3105                                 break;
3106                         case IB_QPS_ERR:
3107                         case IB_QPS_RESET:
3108                                 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3109                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3110                                         nes_rem_ref(&nesqp->ibqp);
3111                                         return -EINVAL;
3112                                 }
3113                                 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3114                                                 nesqp->hwqp.qp_id);
3115                                 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3116                                 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3117                                         if (nesqp->hte_added) {
3118                                                 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3119                                                 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3120                                                 nesqp->hte_added = 0;
3121                                         }
3122                                 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3123                                                 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3124                                         next_iwarp_state |= NES_CQP_QP_RESET;
3125                                         nesqp->in_disconnect = 1;
3126                                 } else {
3127                                         nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3128                                                         nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3129                                         dont_wait = 1;
3130                                 }
3131                                 issue_modify_qp = 1;
3132                                 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3133                                 break;
3134                         default:
3135                                 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3136                                 nes_rem_ref(&nesqp->ibqp);
3137                                 return -EINVAL;
3138                                 break;
3139                 }
3140
3141                 nesqp->ibqp_state = attr->qp_state;
3142                 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3143                                 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3144                                 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3145                                 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3146                         nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3147                         nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3148                                         nesqp->iwarp_state);
3149                         issue_disconnect = 1;
3150                 } else {
3151                         nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3152                         nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3153                                         nesqp->iwarp_state);
3154                 }
3155         }
3156
3157         if (attr_mask & IB_QP_ACCESS_FLAGS) {
3158                 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3159                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3160                                         NES_QPCONTEXT_MISC_RDMA_READ_EN);
3161                         issue_modify_qp = 1;
3162                 }
3163                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3164                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3165                         issue_modify_qp = 1;
3166                 }
3167                 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3168                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3169                         issue_modify_qp = 1;
3170                 }
3171                 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3172                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3173                         issue_modify_qp = 1;
3174                 }
3175
3176                 if (nesqp->user_mode) {
3177                         nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3178                                         NES_QPCONTEXT_MISC_RDMA_READ_EN);
3179                         issue_modify_qp = 1;
3180                 }
3181         }
3182
3183         original_last_aeq = nesqp->last_aeq;
3184         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3185
3186         nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3187
3188         ret = 0;
3189
3190
3191         if (issue_modify_qp) {
3192                 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3193                 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 1);
3194                 if (ret)
3195                         nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3196                                         " failed for QP%u.\n",
3197                                         next_iwarp_state, nesqp->hwqp.qp_id);
3198
3199         }
3200
3201         if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3202                 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3203                                 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3204                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3205                                 original_last_aeq, nesqp->last_aeq);
3206                 if ((!ret) ||
3207                                 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3208                                 (ret))) {
3209                         if (dont_wait) {
3210                                 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3211                                         nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3212                                                         " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3213                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3214                                                         original_last_aeq, nesqp->last_aeq);
3215                                         /* this one is for the cm_disconnect thread */
3216                                         nes_add_ref(&nesqp->ibqp);
3217                                         spin_lock_irqsave(&nesqp->lock, qplockflags);
3218                                         nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3219                                         nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3220                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3221                                         nes_cm_disconn(nesqp);
3222                                 } else {
3223                                         nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3224                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3225                                         nes_rem_ref(&nesqp->ibqp);
3226                                 }
3227                         } else {
3228                                 spin_lock_irqsave(&nesqp->lock, qplockflags);
3229                                 if (nesqp->cm_id) {
3230                                         /* These two are for the timer thread */
3231                                         if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3232                                                 nes_add_ref(&nesqp->ibqp);
3233                                                 nesqp->cm_id->add_ref(nesqp->cm_id);
3234                                                 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3235                                                                 " need ae to finish up, original_last_aeq = 0x%04X."
3236                                                                 " last_aeq = 0x%04X, scheduling timer.\n",
3237                                                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3238                                                                 original_last_aeq, nesqp->last_aeq);
3239                                                 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3240                                         }
3241                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3242                                 } else {
3243                                         spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3244                                         nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3245                                                         " need ae to finish up, original_last_aeq = 0x%04X."
3246                                                         " last_aeq = 0x%04X.\n",
3247                                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3248                                                         original_last_aeq, nesqp->last_aeq);
3249                                 }
3250                         }
3251                 } else {
3252                         nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3253                                         " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3254                                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3255                                         original_last_aeq, nesqp->last_aeq);
3256                         nes_rem_ref(&nesqp->ibqp);
3257                 }
3258         } else {
3259                 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3260                                 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3261                                 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3262                                 original_last_aeq, nesqp->last_aeq);
3263                 nes_rem_ref(&nesqp->ibqp);
3264         }
3265
3266         err = 0;
3267
3268         nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3269                         nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3270
3271         return err;
3272 }
3273
3274
3275 /**
3276  * nes_muticast_attach
3277  */
3278 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3279 {
3280         nes_debug(NES_DBG_INIT, "\n");
3281         return -ENOSYS;
3282 }
3283
3284
3285 /**
3286  * nes_multicast_detach
3287  */
3288 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3289 {
3290         nes_debug(NES_DBG_INIT, "\n");
3291         return -ENOSYS;
3292 }
3293
3294
3295 /**
3296  * nes_process_mad
3297  */
3298 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3299                 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3300                 struct ib_mad *in_mad, struct ib_mad *out_mad)
3301 {
3302         nes_debug(NES_DBG_INIT, "\n");
3303         return -ENOSYS;
3304 }
3305
3306 static inline void
3307 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3308 {
3309         int sge_index;
3310         int total_payload_length = 0;
3311         for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3312                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3313                         ib_wr->sg_list[sge_index].addr);
3314                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3315                         ib_wr->sg_list[sge_index].length);
3316                 if (uselkey)
3317                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3318                                                 (ib_wr->sg_list[sge_index].lkey));
3319                 else
3320                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3321
3322                 total_payload_length += ib_wr->sg_list[sge_index].length;
3323         }
3324         nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3325                         total_payload_length);
3326         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3327                                 total_payload_length);
3328 }
3329
3330 /**
3331  * nes_post_send
3332  */
3333 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3334                 struct ib_send_wr **bad_wr)
3335 {
3336         u64 u64temp;
3337         unsigned long flags = 0;
3338         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3339         struct nes_device *nesdev = nesvnic->nesdev;
3340         struct nes_qp *nesqp = to_nesqp(ibqp);
3341         struct nes_hw_qp_wqe *wqe;
3342         int err;
3343         u32 qsize = nesqp->hwqp.sq_size;
3344         u32 head;
3345         u32 wqe_misc;
3346         u32 wqe_count;
3347         u32 counter;
3348         u32 total_payload_length;
3349
3350         err = 0;
3351         wqe_misc = 0;
3352         wqe_count = 0;
3353         total_payload_length = 0;
3354
3355         if (nesqp->ibqp_state > IB_QPS_RTS)
3356                 return -EINVAL;
3357
3358                 spin_lock_irqsave(&nesqp->lock, flags);
3359
3360         head = nesqp->hwqp.sq_head;
3361
3362         while (ib_wr) {
3363                 /* Check for SQ overflow */
3364                 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3365                         err = -EINVAL;
3366                         break;
3367                 }
3368
3369                 wqe = &nesqp->hwqp.sq_vbase[head];
3370                 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3371                                 nesqp->hwqp.qp_id, wqe, head); */
3372                 nes_fill_init_qp_wqe(wqe, nesqp, head);
3373                 u64temp = (u64)(ib_wr->wr_id);
3374                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3375                                         u64temp);
3376                         switch (ib_wr->opcode) {
3377                                 case IB_WR_SEND:
3378                                         if (ib_wr->send_flags & IB_SEND_SOLICITED) {
3379                                                 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3380                                         } else {
3381                                                 wqe_misc = NES_IWARP_SQ_OP_SEND;
3382                                         }
3383                                         if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3384                                                 err = -EINVAL;
3385                                                 break;
3386                                         }
3387                                         if (ib_wr->send_flags & IB_SEND_FENCE) {
3388                                                 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3389                                         }
3390                                         if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3391                                                         ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3392                                                         (ib_wr->sg_list[0].length <= 64)) {
3393                                                 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3394                                                                (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3395                                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3396                                                                 ib_wr->sg_list[0].length);
3397                                                 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3398                                         } else {
3399                                                 fill_wqe_sg_send(wqe, ib_wr, 1);
3400                                         }
3401
3402                                         break;
3403                                 case IB_WR_RDMA_WRITE:
3404                                         wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3405                                         if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3406                                                 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3407                                                                 ib_wr->num_sge,
3408                                                                 nesdev->nesadapter->max_sge);
3409                                                 err = -EINVAL;
3410                                                 break;
3411                                         }
3412                                         if (ib_wr->send_flags & IB_SEND_FENCE) {
3413                                                 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3414                                         }
3415
3416                                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3417                                                         ib_wr->wr.rdma.rkey);
3418                                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3419                                                         ib_wr->wr.rdma.remote_addr);
3420
3421                                         if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3422                                                         ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3423                                                         (ib_wr->sg_list[0].length <= 64)) {
3424                                                 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3425                                                                (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3426                                                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3427                                                                 ib_wr->sg_list[0].length);
3428                                                 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3429                                         } else {
3430                                                 fill_wqe_sg_send(wqe, ib_wr, 1);
3431                                         }
3432                                         wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3433                                                         wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3434                                         break;
3435                                 case IB_WR_RDMA_READ:
3436                                         /* iWARP only supports 1 sge for RDMA reads */
3437                                         if (ib_wr->num_sge > 1) {
3438                                                 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3439                                                                 ib_wr->num_sge);
3440                                                 err = -EINVAL;
3441                                                 break;
3442                                         }
3443                                         wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3444                                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3445                                                         ib_wr->wr.rdma.remote_addr);
3446                                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3447                                                         ib_wr->wr.rdma.rkey);
3448                                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3449                                                         ib_wr->sg_list->length);
3450                                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3451                                                         ib_wr->sg_list->addr);
3452                                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3453                                                         ib_wr->sg_list->lkey);
3454                                         break;
3455                                 default:
3456                                         /* error */
3457                                         err = -EINVAL;
3458                                         break;
3459                         }
3460
3461                 if (ib_wr->send_flags & IB_SEND_SIGNALED) {
3462                         wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3463                 }
3464                 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3465
3466                 ib_wr = ib_wr->next;
3467                 head++;
3468                 wqe_count++;
3469                 if (head >= qsize)
3470                         head = 0;
3471
3472         }
3473
3474         nesqp->hwqp.sq_head = head;
3475         barrier();
3476         while (wqe_count) {
3477                 counter = min(wqe_count, ((u32)255));
3478                 wqe_count -= counter;
3479                 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3480                                 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3481         }
3482
3483                 spin_unlock_irqrestore(&nesqp->lock, flags);
3484
3485         if (err)
3486                 *bad_wr = ib_wr;
3487         return err;
3488 }
3489
3490
3491 /**
3492  * nes_post_recv
3493  */
3494 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3495                 struct ib_recv_wr **bad_wr)
3496 {
3497         u64 u64temp;
3498         unsigned long flags = 0;
3499         struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3500         struct nes_device *nesdev = nesvnic->nesdev;
3501         struct nes_qp *nesqp = to_nesqp(ibqp);
3502         struct nes_hw_qp_wqe *wqe;
3503         int err = 0;
3504         int sge_index;
3505         u32 qsize = nesqp->hwqp.rq_size;
3506         u32 head;
3507         u32 wqe_count = 0;
3508         u32 counter;
3509         u32 total_payload_length;
3510
3511         if (nesqp->ibqp_state > IB_QPS_RTS)
3512                 return -EINVAL;
3513
3514                 spin_lock_irqsave(&nesqp->lock, flags);
3515
3516         head = nesqp->hwqp.rq_head;
3517
3518         while (ib_wr) {
3519                 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3520                         err = -EINVAL;
3521                         break;
3522                 }
3523                 /* Check for RQ overflow */
3524                 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3525                         err = -EINVAL;
3526                         break;
3527                 }
3528
3529                 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3530                 wqe = &nesqp->hwqp.rq_vbase[head];
3531
3532                 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3533                                 nesqp->hwqp.qp_id, wqe, head); */
3534                 nes_fill_init_qp_wqe(wqe, nesqp, head);
3535                 u64temp = (u64)(ib_wr->wr_id);
3536                 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3537                                         u64temp);
3538                 total_payload_length = 0;
3539                 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3540                         set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3541                                         ib_wr->sg_list[sge_index].addr);
3542                         set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3543                                         ib_wr->sg_list[sge_index].length);
3544                         set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3545                                         ib_wr->sg_list[sge_index].lkey);
3546
3547                         total_payload_length += ib_wr->sg_list[sge_index].length;
3548                 }
3549                 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3550                                         total_payload_length);
3551
3552                 ib_wr = ib_wr->next;
3553                 head++;
3554                 wqe_count++;
3555                 if (head >= qsize)
3556                         head = 0;
3557         }
3558
3559         nesqp->hwqp.rq_head = head;
3560         barrier();
3561         while (wqe_count) {
3562                 counter = min(wqe_count, ((u32)255));
3563                 wqe_count -= counter;
3564                 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3565         }
3566
3567                 spin_unlock_irqrestore(&nesqp->lock, flags);
3568
3569         if (err)
3570                 *bad_wr = ib_wr;
3571         return err;
3572 }
3573
3574
3575 /**
3576  * nes_poll_cq
3577  */
3578 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3579 {
3580         u64 u64temp;
3581         u64 wrid;
3582         /* u64 u64temp; */
3583         unsigned long flags = 0;
3584         struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3585         struct nes_device *nesdev = nesvnic->nesdev;
3586         struct nes_cq *nescq = to_nescq(ibcq);
3587         struct nes_qp *nesqp;
3588         struct nes_hw_cqe cqe;
3589         u32 head;
3590         u32 wq_tail;
3591         u32 cq_size;
3592         u32 cqe_count = 0;
3593         u32 wqe_index;
3594         u32 u32temp;
3595         /* u32 counter; */
3596
3597         nes_debug(NES_DBG_CQ, "\n");
3598
3599                 spin_lock_irqsave(&nescq->lock, flags);
3600
3601         head = nescq->hw_cq.cq_head;
3602         cq_size = nescq->hw_cq.cq_size;
3603
3604         while (cqe_count < num_entries) {
3605                 if (le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3606                                 NES_CQE_VALID) {
3607                         cqe = nescq->hw_cq.cq_vbase[head];
3608                         nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3609                         u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3610                         wqe_index = u32temp &
3611                                         (nesdev->nesadapter->max_qp_wr - 1);
3612                         u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3613                         /* parse CQE, get completion context from WQE (either rq or sq */
3614                         u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3615                                         ((u64)u32temp);
3616                         nesqp = *((struct nes_qp **)&u64temp);
3617                         memset(entry, 0, sizeof *entry);
3618                         if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3619                                 entry->status = IB_WC_SUCCESS;
3620                         } else {
3621                                 entry->status = IB_WC_WR_FLUSH_ERR;
3622                         }
3623
3624                         entry->qp = &nesqp->ibqp;
3625                         entry->src_qp = nesqp->hwqp.qp_id;
3626
3627                         if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3628                                 if (nesqp->skip_lsmm) {
3629                                         nesqp->skip_lsmm = 0;
3630                                         wq_tail = nesqp->hwqp.sq_tail++;
3631                                 }
3632
3633                                 /* Working on a SQ Completion*/
3634                                 wq_tail = wqe_index;
3635                                 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3636                                 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3637                                                 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3638                                                 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3639                                                 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3640                                 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3641                                                 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3642
3643                                 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3644                                                 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3645                                         case NES_IWARP_SQ_OP_RDMAW:
3646                                                 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3647                                                 entry->opcode = IB_WC_RDMA_WRITE;
3648                                                 break;
3649                                         case NES_IWARP_SQ_OP_RDMAR:
3650                                                 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3651                                                 entry->opcode = IB_WC_RDMA_READ;
3652                                                 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3653                                                                 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3654                                                 break;
3655                                         case NES_IWARP_SQ_OP_SENDINV:
3656                                         case NES_IWARP_SQ_OP_SENDSEINV:
3657                                         case NES_IWARP_SQ_OP_SEND:
3658                                         case NES_IWARP_SQ_OP_SENDSE:
3659                                                 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3660                                                 entry->opcode = IB_WC_SEND;
3661                                                 break;
3662                                 }
3663                         } else {
3664                                 /* Working on a RQ Completion*/
3665                                 wq_tail = wqe_index;
3666                                         nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3667                                 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3668                                 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3669                                         ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3670                                         entry->opcode = IB_WC_RECV;
3671                         }
3672                         entry->wr_id = wrid;
3673
3674                         if (++head >= cq_size)
3675                                 head = 0;
3676                         cqe_count++;
3677                         nescq->polled_completions++;
3678                         if ((nescq->polled_completions > (cq_size / 2)) ||
3679                                         (nescq->polled_completions == 255)) {
3680                                 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3681                                                 " are pending %u of %u.\n",
3682                                                 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3683                                 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3684                                                 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3685                                 nescq->polled_completions = 0;
3686                         }
3687                         entry++;
3688                 } else
3689                         break;
3690         }
3691
3692         if (nescq->polled_completions) {
3693                 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3694                                 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3695                 nescq->polled_completions = 0;
3696         }
3697
3698         nescq->hw_cq.cq_head = head;
3699         nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3700                         cqe_count, nescq->hw_cq.cq_number);
3701
3702                 spin_unlock_irqrestore(&nescq->lock, flags);
3703
3704         return cqe_count;
3705 }
3706
3707
3708 /**
3709  * nes_req_notify_cq
3710  */
3711 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3712                 {
3713         struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3714         struct nes_device *nesdev = nesvnic->nesdev;
3715         struct nes_cq *nescq = to_nescq(ibcq);
3716         u32 cq_arm;
3717
3718         nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3719                         nescq->hw_cq.cq_number);
3720
3721         cq_arm = nescq->hw_cq.cq_number;
3722         if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3723                 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3724         else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3725                 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3726         else
3727                 return -EINVAL;
3728
3729         nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3730         nes_read32(nesdev->regs+NES_CQE_ALLOC);
3731
3732         return 0;
3733 }
3734
3735
3736 /**
3737  * nes_init_ofa_device
3738  */
3739 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3740 {
3741         struct nes_ib_device *nesibdev;
3742         struct nes_vnic *nesvnic = netdev_priv(netdev);
3743         struct nes_device *nesdev = nesvnic->nesdev;
3744
3745         nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3746         if (nesibdev == NULL) {
3747                 return NULL;
3748         }
3749         strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3750         nesibdev->ibdev.owner = THIS_MODULE;
3751
3752         nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3753         memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3754         memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3755
3756         nesibdev->ibdev.uverbs_cmd_mask =
3757                         (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3758                         (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3759                         (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3760                         (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3761                         (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3762                         (1ull << IB_USER_VERBS_CMD_REG_MR) |
3763                         (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3764                         (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3765                         (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3766                         (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3767                         (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3768                         (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3769                         (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3770                         (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3771                         (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3772                         (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3773                         (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3774                         (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3775                         (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3776                         (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3777                         (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3778                         (1ull << IB_USER_VERBS_CMD_POST_SEND);
3779
3780         nesibdev->ibdev.phys_port_cnt = 1;
3781         nesibdev->ibdev.num_comp_vectors = 1;
3782         nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3783         nesibdev->ibdev.class_dev.dev = &nesdev->pcidev->dev;
3784         nesibdev->ibdev.query_device = nes_query_device;
3785         nesibdev->ibdev.query_port = nes_query_port;
3786         nesibdev->ibdev.modify_port = nes_modify_port;
3787         nesibdev->ibdev.query_pkey = nes_query_pkey;
3788         nesibdev->ibdev.query_gid = nes_query_gid;
3789         nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3790         nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3791         nesibdev->ibdev.mmap = nes_mmap;
3792         nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3793         nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3794         nesibdev->ibdev.create_ah = nes_create_ah;
3795         nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3796         nesibdev->ibdev.create_qp = nes_create_qp;
3797         nesibdev->ibdev.modify_qp = nes_modify_qp;
3798         nesibdev->ibdev.query_qp = nes_query_qp;
3799         nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3800         nesibdev->ibdev.create_cq = nes_create_cq;
3801         nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3802         nesibdev->ibdev.poll_cq = nes_poll_cq;
3803         nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3804         nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3805         nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3806         nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3807         nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3808         nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3809         nesibdev->ibdev.bind_mw = nes_bind_mw;
3810
3811         nesibdev->ibdev.alloc_fmr = nes_alloc_fmr;
3812         nesibdev->ibdev.unmap_fmr = nes_unmap_fmr;
3813         nesibdev->ibdev.dealloc_fmr = nes_dealloc_fmr;
3814         nesibdev->ibdev.map_phys_fmr = nes_map_phys_fmr;
3815
3816         nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3817         nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3818         nesibdev->ibdev.process_mad = nes_process_mad;
3819
3820         nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3821         nesibdev->ibdev.post_send = nes_post_send;
3822         nesibdev->ibdev.post_recv = nes_post_recv;
3823
3824         nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3825         if (nesibdev->ibdev.iwcm == NULL) {
3826                 ib_dealloc_device(&nesibdev->ibdev);
3827                 return NULL;
3828         }
3829         nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3830         nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3831         nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3832         nesibdev->ibdev.iwcm->connect = nes_connect;
3833         nesibdev->ibdev.iwcm->accept = nes_accept;
3834         nesibdev->ibdev.iwcm->reject = nes_reject;
3835         nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3836         nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3837
3838         return nesibdev;
3839 }
3840
3841
3842 /**
3843  * nes_destroy_ofa_device
3844  */
3845 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3846 {
3847         if (nesibdev == NULL)
3848                 return;
3849
3850         nes_unregister_ofa_device(nesibdev);
3851
3852         kfree(nesibdev->ibdev.iwcm);
3853         ib_dealloc_device(&nesibdev->ibdev);
3854 }
3855
3856
3857 /**
3858  * nes_register_ofa_device
3859  */
3860 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3861 {
3862         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3863         struct nes_device *nesdev = nesvnic->nesdev;
3864         struct nes_adapter *nesadapter = nesdev->nesadapter;
3865         int i, ret;
3866
3867         ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3868         if (ret) {
3869                 return ret;
3870         }
3871
3872         /* Get the resources allocated to this device */
3873         nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3874         nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3875         nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3876         nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3877
3878         for (i = 0; i < ARRAY_SIZE(nes_class_attributes); ++i) {
3879                 ret = class_device_create_file(&nesibdev->ibdev.class_dev, nes_class_attributes[i]);
3880                 if (ret) {
3881                         while (i > 0) {
3882                                 i--;
3883                                 class_device_remove_file(&nesibdev->ibdev.class_dev,
3884                                                 nes_class_attributes[i]);
3885                         }
3886                         ib_unregister_device(&nesibdev->ibdev);
3887                         return ret;
3888                 }
3889         }
3890
3891         nesvnic->of_device_registered = 1;
3892
3893         return 0;
3894 }
3895
3896
3897 /**
3898  * nes_unregister_ofa_device
3899  */
3900 void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3901 {
3902         struct nes_vnic *nesvnic = nesibdev->nesvnic;
3903         int i;
3904
3905         if (nesibdev == NULL)
3906                 return;
3907
3908         for (i = 0; i < ARRAY_SIZE(nes_class_attributes); ++i) {
3909                 class_device_remove_file(&nesibdev->ibdev.class_dev, nes_class_attributes[i]);
3910         }
3911
3912         if (nesvnic->of_device_registered) {
3913                 ib_unregister_device(&nesibdev->ibdev);
3914         }
3915
3916         nesvnic->of_device_registered = 0;
3917 }