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