IB/mlx4: Check correct variable for allocation failure
[sfrench/cifs-2.6.git] / drivers / infiniband / hw / nes / nes_verbs.c
1 /*
2  * Copyright (c) 2006 - 2009 Intel Corporation.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/random.h>
37 #include <linux/highmem.h>
38 #include <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 -ENOMEM;
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_fast_mr
280  */
281 static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
282                              u32 stag, u32 page_count)
283 {
284         struct nes_hw_cqp_wqe *cqp_wqe;
285         struct nes_cqp_request *cqp_request;
286         unsigned long flags;
287         int ret;
288         struct nes_adapter *nesadapter = nesdev->nesadapter;
289         u32 opcode = 0;
290         u16 major_code;
291         u64 region_length = page_count * PAGE_SIZE;
292
293
294         cqp_request = nes_get_cqp_request(nesdev);
295         if (cqp_request == NULL) {
296                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
297                 return -ENOMEM;
298         }
299         nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
300                               "region_length = %llu\n",
301                               page_count, region_length);
302         cqp_request->waiting = 1;
303         cqp_wqe = &cqp_request->cqp_wqe;
304
305         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
306         if (nesadapter->free_4kpbl > 0) {
307                 nesadapter->free_4kpbl--;
308                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
309         } else {
310                 /* No 4kpbl's available: */
311                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
312                 nes_debug(NES_DBG_MR, "Out of Pbls\n");
313                 nes_free_cqp_request(nesdev, cqp_request);
314                 return -ENOMEM;
315         }
316
317         opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
318                  NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
319                  NES_CQP_STAG_REM_ACC_EN;
320         /*
321          * The current OFED API does not support the zero based TO option.
322          * If added then need to changed the NES_CQP_STAG_VA* option.  Also,
323          * the API does not support that ability to have the MR set for local
324          * access only when created and not allow the SQ op to override. Given
325          * this the remote enable must be set here.
326          */
327
328         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
329         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
330         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
331
332         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
333                         cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
334         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
335                         cpu_to_le32(nespd->pd_id & 0x00007fff);
336
337         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
338         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
339         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
340         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
341         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
342         cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
343         barrier();
344
345         atomic_set(&cqp_request->refcount, 2);
346         nes_post_cqp_request(nesdev, cqp_request);
347
348         /* Wait for CQP */
349         ret = wait_event_timeout(cqp_request->waitq,
350                                  (0 != cqp_request->request_done),
351                                  NES_EVENT_TIMEOUT);
352
353         nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
354                   "wait_event_timeout ret = %u, CQP Major:Minor codes = "
355                   "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
356                   cqp_request->minor_code);
357         major_code = cqp_request->major_code;
358         nes_put_cqp_request(nesdev, cqp_request);
359
360         if (!ret || major_code) {
361                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
362                 nesadapter->free_4kpbl++;
363                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
364         }
365
366         if (!ret)
367                 return -ETIME;
368         else if (major_code)
369                 return -EIO;
370         return 0;
371 }
372
373 /*
374  * nes_alloc_fast_reg_mr
375  */
376 struct ib_mr *nes_alloc_fast_reg_mr(struct ib_pd *ibpd, int max_page_list_len)
377 {
378         struct nes_pd *nespd = to_nespd(ibpd);
379         struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
380         struct nes_device *nesdev = nesvnic->nesdev;
381         struct nes_adapter *nesadapter = nesdev->nesadapter;
382
383         u32 next_stag_index;
384         u8 stag_key = 0;
385         u32 driver_key = 0;
386         int err = 0;
387         u32 stag_index = 0;
388         struct nes_mr *nesmr;
389         u32 stag;
390         int ret;
391         struct ib_mr *ibmr;
392 /*
393  * Note:  Set to always use a fixed length single page entry PBL.  This is to allow
394  *       for the fast_reg_mr operation to always know the size of the PBL.
395  */
396         if (max_page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
397                 return ERR_PTR(-E2BIG);
398
399         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
400         stag_key = (u8)next_stag_index;
401         next_stag_index >>= 8;
402         next_stag_index %= nesadapter->max_mr;
403
404         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
405                                  nesadapter->max_mr, &stag_index,
406                                  &next_stag_index);
407         if (err)
408                 return ERR_PTR(err);
409
410         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
411         if (!nesmr) {
412                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
413                 return ERR_PTR(-ENOMEM);
414         }
415
416         stag = stag_index << 8;
417         stag |= driver_key;
418         stag += (u32)stag_key;
419
420         nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
421                   stag, stag_index);
422
423         ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_page_list_len);
424
425         if (ret == 0) {
426                 nesmr->ibmr.rkey = stag;
427                 nesmr->ibmr.lkey = stag;
428                 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
429                 ibmr = &nesmr->ibmr;
430         } else {
431                 kfree(nesmr);
432                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
433                 ibmr = ERR_PTR(-ENOMEM);
434         }
435         return ibmr;
436 }
437
438 /*
439  * nes_alloc_fast_reg_page_list
440  */
441 static struct ib_fast_reg_page_list *nes_alloc_fast_reg_page_list(
442                                                         struct ib_device *ibdev,
443                                                         int page_list_len)
444 {
445         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
446         struct nes_device *nesdev = nesvnic->nesdev;
447         struct ib_fast_reg_page_list *pifrpl;
448         struct nes_ib_fast_reg_page_list *pnesfrpl;
449
450         if (page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
451                 return ERR_PTR(-E2BIG);
452         /*
453          * Allocate the ib_fast_reg_page_list structure, the
454          * nes_fast_bpl structure, and the PLB table.
455          */
456         pnesfrpl = kmalloc(sizeof(struct nes_ib_fast_reg_page_list) +
457                            page_list_len * sizeof(u64), GFP_KERNEL);
458
459         if (!pnesfrpl)
460                 return ERR_PTR(-ENOMEM);
461
462         pifrpl = &pnesfrpl->ibfrpl;
463         pifrpl->page_list = &pnesfrpl->pbl;
464         pifrpl->max_page_list_len = page_list_len;
465         /*
466          * Allocate the WQE PBL
467          */
468         pnesfrpl->nes_wqe_pbl.kva = pci_alloc_consistent(nesdev->pcidev,
469                                                          page_list_len * sizeof(u64),
470                                                          &pnesfrpl->nes_wqe_pbl.paddr);
471
472         if (!pnesfrpl->nes_wqe_pbl.kva) {
473                 kfree(pnesfrpl);
474                 return ERR_PTR(-ENOMEM);
475         }
476         nes_debug(NES_DBG_MR, "nes_alloc_fast_reg_pbl: nes_frpl = %p, "
477                   "ibfrpl = %p, ibfrpl.page_list = %p, pbl.kva = %p, "
478                   "pbl.paddr= %p\n", pnesfrpl, &pnesfrpl->ibfrpl,
479                   pnesfrpl->ibfrpl.page_list, pnesfrpl->nes_wqe_pbl.kva,
480                   (void *)pnesfrpl->nes_wqe_pbl.paddr);
481
482         return pifrpl;
483 }
484
485 /*
486  * nes_free_fast_reg_page_list
487  */
488 static void nes_free_fast_reg_page_list(struct ib_fast_reg_page_list *pifrpl)
489 {
490         struct nes_vnic *nesvnic = to_nesvnic(pifrpl->device);
491         struct nes_device *nesdev = nesvnic->nesdev;
492         struct nes_ib_fast_reg_page_list *pnesfrpl;
493
494         pnesfrpl = container_of(pifrpl, struct nes_ib_fast_reg_page_list, ibfrpl);
495         /*
496          * Free the WQE PBL.
497          */
498         pci_free_consistent(nesdev->pcidev,
499                             pifrpl->max_page_list_len * sizeof(u64),
500                             pnesfrpl->nes_wqe_pbl.kva,
501                             pnesfrpl->nes_wqe_pbl.paddr);
502         /*
503          * Free the PBL structure
504          */
505         kfree(pnesfrpl);
506 }
507
508 /**
509  * nes_query_device
510  */
511 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
512 {
513         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
514         struct nes_device *nesdev = nesvnic->nesdev;
515         struct nes_ib_device *nesibdev = nesvnic->nesibdev;
516
517         memset(props, 0, sizeof(*props));
518         memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
519
520         props->fw_ver = nesdev->nesadapter->fw_ver;
521         props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
522         props->vendor_id = nesdev->nesadapter->vendor_id;
523         props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
524         props->hw_ver = nesdev->nesadapter->hw_rev;
525         props->max_mr_size = 0x80000000;
526         props->max_qp = nesibdev->max_qp;
527         props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
528         props->max_sge = nesdev->nesadapter->max_sge;
529         props->max_cq = nesibdev->max_cq;
530         props->max_cqe = nesdev->nesadapter->max_cqe;
531         props->max_mr = nesibdev->max_mr;
532         props->max_mw = nesibdev->max_mr;
533         props->max_pd = nesibdev->max_pd;
534         props->max_sge_rd = 1;
535         switch (nesdev->nesadapter->max_irrq_wr) {
536                 case 0:
537                         props->max_qp_rd_atom = 2;
538                         break;
539                 case 1:
540                         props->max_qp_rd_atom = 8;
541                         break;
542                 case 2:
543                         props->max_qp_rd_atom = 32;
544                         break;
545                 case 3:
546                         props->max_qp_rd_atom = 64;
547                         break;
548                 default:
549                         props->max_qp_rd_atom = 0;
550         }
551         props->max_qp_init_rd_atom = props->max_qp_rd_atom;
552         props->atomic_cap = IB_ATOMIC_NONE;
553         props->max_map_per_fmr = 1;
554
555         return 0;
556 }
557
558
559 /**
560  * nes_query_port
561  */
562 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
563 {
564         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
565         struct net_device *netdev = nesvnic->netdev;
566
567         memset(props, 0, sizeof(*props));
568
569         props->max_mtu = IB_MTU_4096;
570
571         if (netdev->mtu  >= 4096)
572                 props->active_mtu = IB_MTU_4096;
573         else if (netdev->mtu  >= 2048)
574                 props->active_mtu = IB_MTU_2048;
575         else if (netdev->mtu  >= 1024)
576                 props->active_mtu = IB_MTU_1024;
577         else if (netdev->mtu  >= 512)
578                 props->active_mtu = IB_MTU_512;
579         else
580                 props->active_mtu = IB_MTU_256;
581
582         props->lid = 1;
583         props->lmc = 0;
584         props->sm_lid = 0;
585         props->sm_sl = 0;
586         if (nesvnic->linkup)
587                 props->state = IB_PORT_ACTIVE;
588         else
589                 props->state = IB_PORT_DOWN;
590         props->phys_state = 0;
591         props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
592                         IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
593         props->gid_tbl_len = 1;
594         props->pkey_tbl_len = 1;
595         props->qkey_viol_cntr = 0;
596         props->active_width = IB_WIDTH_4X;
597         props->active_speed = 1;
598         props->max_msg_sz = 0x80000000;
599
600         return 0;
601 }
602
603
604 /**
605  * nes_modify_port
606  */
607 static int nes_modify_port(struct ib_device *ibdev, u8 port,
608                 int port_modify_mask, struct ib_port_modify *props)
609 {
610         return 0;
611 }
612
613
614 /**
615  * nes_query_pkey
616  */
617 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
618 {
619         *pkey = 0;
620         return 0;
621 }
622
623
624 /**
625  * nes_query_gid
626  */
627 static int nes_query_gid(struct ib_device *ibdev, u8 port,
628                 int index, union ib_gid *gid)
629 {
630         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
631
632         memset(&(gid->raw[0]), 0, sizeof(gid->raw));
633         memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
634
635         return 0;
636 }
637
638
639 /**
640  * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
641  * of all objects associated with a particular user-mode client.
642  */
643 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
644                 struct ib_udata *udata)
645 {
646         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
647         struct nes_device *nesdev = nesvnic->nesdev;
648         struct nes_adapter *nesadapter = nesdev->nesadapter;
649         struct nes_alloc_ucontext_req req;
650         struct nes_alloc_ucontext_resp uresp;
651         struct nes_ucontext *nes_ucontext;
652         struct nes_ib_device *nesibdev = nesvnic->nesibdev;
653
654
655         if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
656                 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
657                 return ERR_PTR(-EINVAL);
658         }
659
660         if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
661                 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
662                         req.userspace_ver, NES_ABI_USERSPACE_VER);
663                 return ERR_PTR(-EINVAL);
664         }
665
666
667         memset(&uresp, 0, sizeof uresp);
668
669         uresp.max_qps = nesibdev->max_qp;
670         uresp.max_pds = nesibdev->max_pd;
671         uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
672         uresp.virtwq = nesadapter->virtwq;
673         uresp.kernel_ver = NES_ABI_KERNEL_VER;
674
675         nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
676         if (!nes_ucontext)
677                 return ERR_PTR(-ENOMEM);
678
679         nes_ucontext->nesdev = nesdev;
680         nes_ucontext->mmap_wq_offset = uresp.max_pds;
681         nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
682                         ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
683                         PAGE_SIZE;
684
685
686         if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
687                 kfree(nes_ucontext);
688                 return ERR_PTR(-EFAULT);
689         }
690
691         INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
692         INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
693         atomic_set(&nes_ucontext->usecnt, 1);
694         return &nes_ucontext->ibucontext;
695 }
696
697
698 /**
699  * nes_dealloc_ucontext
700  */
701 static int nes_dealloc_ucontext(struct ib_ucontext *context)
702 {
703         /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
704         /* struct nes_device *nesdev = nesvnic->nesdev; */
705         struct nes_ucontext *nes_ucontext = to_nesucontext(context);
706
707         if (!atomic_dec_and_test(&nes_ucontext->usecnt))
708           return 0;
709         kfree(nes_ucontext);
710         return 0;
711 }
712
713
714 /**
715  * nes_mmap
716  */
717 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
718 {
719         unsigned long index;
720         struct nes_vnic *nesvnic = to_nesvnic(context->device);
721         struct nes_device *nesdev = nesvnic->nesdev;
722         /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
723         struct nes_ucontext *nes_ucontext;
724         struct nes_qp *nesqp;
725
726         nes_ucontext = to_nesucontext(context);
727
728
729         if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
730                 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
731                 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
732                                 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
733                 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
734                         nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
735                         return -EFAULT;
736                 }
737                 nesqp = nes_ucontext->mmap_nesqp[index];
738                 if (nesqp == NULL) {
739                         nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
740                         return -EFAULT;
741                 }
742                 if (remap_pfn_range(vma, vma->vm_start,
743                                 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
744                                 vma->vm_end - vma->vm_start,
745                                 vma->vm_page_prot)) {
746                         nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
747                         return -EAGAIN;
748                 }
749                 vma->vm_private_data = nesqp;
750                 return 0;
751         } else {
752                 index = vma->vm_pgoff;
753                 if (!test_bit(index, nes_ucontext->allocated_doorbells))
754                         return -EFAULT;
755
756                 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
757                 if (io_remap_pfn_range(vma, vma->vm_start,
758                                 (nesdev->doorbell_start +
759                                 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
760                                 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
761                         return -EAGAIN;
762                 vma->vm_private_data = nes_ucontext;
763                 return 0;
764         }
765
766         return -ENOSYS;
767 }
768
769
770 /**
771  * nes_alloc_pd
772  */
773 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
774                 struct ib_ucontext *context, struct ib_udata *udata)
775 {
776         struct nes_pd *nespd;
777         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
778         struct nes_device *nesdev = nesvnic->nesdev;
779         struct nes_adapter *nesadapter = nesdev->nesadapter;
780         struct nes_ucontext *nesucontext;
781         struct nes_alloc_pd_resp uresp;
782         u32 pd_num = 0;
783         int err;
784
785         nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
786                         nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
787                         atomic_read(&nesvnic->netdev->refcnt));
788
789         err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
790                         nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
791         if (err) {
792                 return ERR_PTR(err);
793         }
794
795         nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
796         if (!nespd) {
797                 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
798                 return ERR_PTR(-ENOMEM);
799         }
800
801         nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
802                         nespd, nesvnic->nesibdev->ibdev.name);
803
804         nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
805
806         if (context) {
807                 nesucontext = to_nesucontext(context);
808                 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
809                                 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
810                 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
811                                 nespd->mmap_db_index, nespd->pd_id);
812                 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
813                         nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
814                         nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
815                         kfree(nespd);
816                         return ERR_PTR(-ENOMEM);
817                 }
818
819                 uresp.pd_id = nespd->pd_id;
820                 uresp.mmap_db_index = nespd->mmap_db_index;
821                 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
822                         nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
823                         kfree(nespd);
824                         return ERR_PTR(-EFAULT);
825                 }
826
827                 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
828                 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
829                 nesucontext->first_free_db = nespd->mmap_db_index + 1;
830         }
831
832         nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
833         return &nespd->ibpd;
834 }
835
836
837 /**
838  * nes_dealloc_pd
839  */
840 static int nes_dealloc_pd(struct ib_pd *ibpd)
841 {
842         struct nes_ucontext *nesucontext;
843         struct nes_pd *nespd = to_nespd(ibpd);
844         struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
845         struct nes_device *nesdev = nesvnic->nesdev;
846         struct nes_adapter *nesadapter = nesdev->nesadapter;
847
848         if ((ibpd->uobject) && (ibpd->uobject->context)) {
849                 nesucontext = to_nesucontext(ibpd->uobject->context);
850                 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
851                                 nespd->mmap_db_index);
852                 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
853                 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
854                 if (nesucontext->first_free_db > nespd->mmap_db_index) {
855                         nesucontext->first_free_db = nespd->mmap_db_index;
856                 }
857         }
858
859         nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
860                         nespd->pd_id, nespd);
861         nes_free_resource(nesadapter, nesadapter->allocated_pds,
862                         (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
863         kfree(nespd);
864
865         return 0;
866 }
867
868
869 /**
870  * nes_create_ah
871  */
872 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
873 {
874         return ERR_PTR(-ENOSYS);
875 }
876
877
878 /**
879  * nes_destroy_ah
880  */
881 static int nes_destroy_ah(struct ib_ah *ah)
882 {
883         return -ENOSYS;
884 }
885
886
887 /**
888  * nes_get_encoded_size
889  */
890 static inline u8 nes_get_encoded_size(int *size)
891 {
892         u8 encoded_size = 0;
893         if (*size <= 32) {
894                 *size = 32;
895                 encoded_size = 1;
896         } else if (*size <= 128) {
897                 *size = 128;
898                 encoded_size = 2;
899         } else if (*size <= 512) {
900                 *size = 512;
901                 encoded_size = 3;
902         }
903         return (encoded_size);
904 }
905
906
907
908 /**
909  * nes_setup_virt_qp
910  */
911 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
912                 struct nes_vnic *nesvnic, int sq_size, int rq_size)
913 {
914         unsigned long flags;
915         void *mem;
916         __le64 *pbl = NULL;
917         __le64 *tpbl;
918         __le64 *pblbuffer;
919         struct nes_device *nesdev = nesvnic->nesdev;
920         struct nes_adapter *nesadapter = nesdev->nesadapter;
921         u32 pbl_entries;
922         u8 rq_pbl_entries;
923         u8 sq_pbl_entries;
924
925         pbl_entries = nespbl->pbl_size >> 3;
926         nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
927                         nespbl->pbl_size, pbl_entries,
928                         (void *)nespbl->pbl_vbase,
929                         (unsigned long) nespbl->pbl_pbase);
930         pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
931         /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
932         /* the first pbl to be fro the rq_vbase... */
933         rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
934         sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
935         nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
936         if (!nespbl->page) {
937                 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
938                 kfree(nespbl);
939                 return -ENOMEM;
940         }
941
942         nesqp->hwqp.sq_vbase = kmap(nespbl->page);
943         nesqp->page = nespbl->page;
944         if (!nesqp->hwqp.sq_vbase) {
945                 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
946                 kfree(nespbl);
947                 return -ENOMEM;
948         }
949
950         /* Now to get to sq.. we need to calculate how many */
951         /* PBL entries were used by the rq.. */
952         pbl += sq_pbl_entries;
953         nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
954         /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
955         /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
956
957         nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
958                   nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
959                   nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
960         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
961         if (!nesadapter->free_256pbl) {
962                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
963                                 nespbl->pbl_pbase);
964                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
965                 kunmap(nesqp->page);
966                 kfree(nespbl);
967                 return -ENOMEM;
968         }
969         nesadapter->free_256pbl--;
970         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
971
972         nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
973         pblbuffer = nesqp->pbl_vbase;
974         if (!nesqp->pbl_vbase) {
975                 /* memory allocated during nes_reg_user_mr() */
976                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
977                                     nespbl->pbl_pbase);
978                 kfree(nespbl);
979                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
980                 nesadapter->free_256pbl++;
981                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
982                 kunmap(nesqp->page);
983                 return -ENOMEM;
984         }
985         memset(nesqp->pbl_vbase, 0, 256);
986         /* fill in the page address in the pbl buffer.. */
987         tpbl = pblbuffer + 16;
988         pbl = (__le64 *)nespbl->pbl_vbase;
989         while (sq_pbl_entries--)
990                 *tpbl++ = *pbl++;
991         tpbl = pblbuffer;
992         while (rq_pbl_entries--)
993                 *tpbl++ = *pbl++;
994
995         /* done with memory allocated during nes_reg_user_mr() */
996         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
997                             nespbl->pbl_pbase);
998         kfree(nespbl);
999
1000         nesqp->qp_mem_size =
1001                         max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256;     /* this is Q2 */
1002         /* Round up to a multiple of a page */
1003         nesqp->qp_mem_size += PAGE_SIZE - 1;
1004         nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1005
1006         mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1007                         &nesqp->hwqp.q2_pbase);
1008
1009         if (!mem) {
1010                 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1011                 nesqp->pbl_vbase = NULL;
1012                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1013                 nesadapter->free_256pbl++;
1014                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1015                 kunmap(nesqp->page);
1016                 return -ENOMEM;
1017         }
1018         nesqp->sq_kmapped = 1;
1019         nesqp->hwqp.q2_vbase = mem;
1020         mem += 256;
1021         memset(nesqp->hwqp.q2_vbase, 0, 256);
1022         nesqp->nesqp_context = mem;
1023         memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1024         nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1025
1026         return 0;
1027 }
1028
1029
1030 /**
1031  * nes_setup_mmap_qp
1032  */
1033 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1034                 int sq_size, int rq_size)
1035 {
1036         void *mem;
1037         struct nes_device *nesdev = nesvnic->nesdev;
1038
1039         nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1040                         (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1041                         max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1042                         256; /* this is Q2 */
1043         /* Round up to a multiple of a page */
1044         nesqp->qp_mem_size += PAGE_SIZE - 1;
1045         nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1046
1047         mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1048                         &nesqp->hwqp.sq_pbase);
1049         if (!mem)
1050                 return -ENOMEM;
1051         nes_debug(NES_DBG_QP, "PCI consistent memory for "
1052                         "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1053                         mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1054
1055         memset(mem, 0, nesqp->qp_mem_size);
1056
1057         nesqp->hwqp.sq_vbase = mem;
1058         mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1059
1060         nesqp->hwqp.rq_vbase = mem;
1061         nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1062                         sizeof(struct nes_hw_qp_wqe) * sq_size;
1063         mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1064
1065         nesqp->hwqp.q2_vbase = mem;
1066         nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1067                         sizeof(struct nes_hw_qp_wqe) * rq_size;
1068         mem += 256;
1069         memset(nesqp->hwqp.q2_vbase, 0, 256);
1070
1071         nesqp->nesqp_context = mem;
1072         nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1073         memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1074         return 0;
1075 }
1076
1077
1078 /**
1079  * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1080  */
1081 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1082                 struct nes_qp *nesqp, int virt_wqs)
1083 {
1084         unsigned long flags;
1085         struct nes_adapter *nesadapter = nesdev->nesadapter;
1086         if (!virt_wqs) {
1087                 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1088                                 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1089         }else {
1090                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1091                 nesadapter->free_256pbl++;
1092                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1093                 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1094                 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1095                 nesqp->pbl_vbase = NULL;
1096                 if (nesqp->sq_kmapped) {
1097                         nesqp->sq_kmapped = 0;
1098                         kunmap(nesqp->page);
1099                 }
1100         }
1101 }
1102
1103
1104 /**
1105  * nes_create_qp
1106  */
1107 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1108                 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1109 {
1110         u64 u64temp= 0;
1111         u64 u64nesqp = 0;
1112         struct nes_pd *nespd = to_nespd(ibpd);
1113         struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1114         struct nes_device *nesdev = nesvnic->nesdev;
1115         struct nes_adapter *nesadapter = nesdev->nesadapter;
1116         struct nes_qp *nesqp;
1117         struct nes_cq *nescq;
1118         struct nes_ucontext *nes_ucontext;
1119         struct nes_hw_cqp_wqe *cqp_wqe;
1120         struct nes_cqp_request *cqp_request;
1121         struct nes_create_qp_req req;
1122         struct nes_create_qp_resp uresp;
1123         struct nes_pbl  *nespbl = NULL;
1124         u32 qp_num = 0;
1125         u32 opcode = 0;
1126         /* u32 counter = 0; */
1127         void *mem;
1128         unsigned long flags;
1129         int ret;
1130         int err;
1131         int virt_wqs = 0;
1132         int sq_size;
1133         int rq_size;
1134         u8 sq_encoded_size;
1135         u8 rq_encoded_size;
1136         /* int counter; */
1137
1138         if (init_attr->create_flags)
1139                 return ERR_PTR(-EINVAL);
1140
1141         atomic_inc(&qps_created);
1142         switch (init_attr->qp_type) {
1143                 case IB_QPT_RC:
1144                         if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1145                                 init_attr->cap.max_inline_data = 0;
1146                         } else {
1147                                 init_attr->cap.max_inline_data = 64;
1148                         }
1149                         sq_size = init_attr->cap.max_send_wr;
1150                         rq_size = init_attr->cap.max_recv_wr;
1151
1152                         /* check if the encoded sizes are OK or not... */
1153                         sq_encoded_size = nes_get_encoded_size(&sq_size);
1154                         rq_encoded_size = nes_get_encoded_size(&rq_size);
1155
1156                         if ((!sq_encoded_size) || (!rq_encoded_size)) {
1157                                 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1158                                                 rq_size, sq_size);
1159                                 return ERR_PTR(-EINVAL);
1160                         }
1161
1162                         init_attr->cap.max_send_wr = sq_size -2;
1163                         init_attr->cap.max_recv_wr = rq_size -1;
1164                         nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1165
1166                         ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1167                                         nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1168                         if (ret) {
1169                                 return ERR_PTR(ret);
1170                         }
1171
1172                         /* Need 512 (actually now 1024) byte alignment on this structure */
1173                         mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1174                         if (!mem) {
1175                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1176                                 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1177                                 return ERR_PTR(-ENOMEM);
1178                         }
1179                         u64nesqp = (unsigned long)mem;
1180                         u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1181                         u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1182                         u64nesqp &= ~u64temp;
1183                         nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1184                         /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p.  Rounded to closest %u\n",
1185                                         nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1186                         nesqp->allocated_buffer = mem;
1187
1188                         if (udata) {
1189                                 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1190                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1191                                         kfree(nesqp->allocated_buffer);
1192                                         nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1193                                         return NULL;
1194                                 }
1195                                 if (req.user_wqe_buffers) {
1196                                         virt_wqs = 1;
1197                                 }
1198                                 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1199                                         nesqp->user_mode = 1;
1200                                         nes_ucontext = to_nesucontext(ibpd->uobject->context);
1201                                         if (virt_wqs) {
1202                                                 err = 1;
1203                                                 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1204                                                         if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1205                                                                 list_del(&nespbl->list);
1206                                                                 err = 0;
1207                                                                 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1208                                                                           nespbl, nespbl->user_base);
1209                                                                 break;
1210                                                         }
1211                                                 }
1212                                                 if (err) {
1213                                                         nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1214                                                                   (long long unsigned int)req.user_wqe_buffers);
1215                                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1216                                                         kfree(nesqp->allocated_buffer);
1217                                                         return ERR_PTR(-EFAULT);
1218                                                 }
1219                                         }
1220
1221                                         nes_ucontext = to_nesucontext(ibpd->uobject->context);
1222                                         nesqp->mmap_sq_db_index =
1223                                                 find_next_zero_bit(nes_ucontext->allocated_wqs,
1224                                                                    NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1225                                         /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1226                                                         nespd->mmap_db_index); */
1227                                         if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1228                                                 nes_debug(NES_DBG_QP,
1229                                                           "db index > max user regions, failing create QP\n");
1230                                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1231                                                 if (virt_wqs) {
1232                                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1233                                                                             nespbl->pbl_pbase);
1234                                                         kfree(nespbl);
1235                                                 }
1236                                                 kfree(nesqp->allocated_buffer);
1237                                                 return ERR_PTR(-ENOMEM);
1238                                         }
1239                                         set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1240                                         nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1241                                         nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1242                                 } else {
1243                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1244                                         kfree(nesqp->allocated_buffer);
1245                                         return ERR_PTR(-EFAULT);
1246                                 }
1247                         }
1248                         err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1249                                         nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1250                         if (err) {
1251                                 nes_debug(NES_DBG_QP,
1252                                           "error geting qp mem code = %d\n", err);
1253                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1254                                 kfree(nesqp->allocated_buffer);
1255                                 return ERR_PTR(-ENOMEM);
1256                         }
1257
1258                         nesqp->hwqp.sq_size = sq_size;
1259                         nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1260                         nesqp->hwqp.sq_head = 1;
1261                         nesqp->hwqp.rq_size = rq_size;
1262                         nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1263                         /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1264                                         (void *)nesqp->nesqp_context_pbase);
1265                         */
1266                         nesqp->hwqp.qp_id = qp_num;
1267                         nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1268                         nesqp->nespd = nespd;
1269
1270                         nescq = to_nescq(init_attr->send_cq);
1271                         nesqp->nesscq = nescq;
1272                         nescq = to_nescq(init_attr->recv_cq);
1273                         nesqp->nesrcq = nescq;
1274
1275                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1276                                         NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1277                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1278                                         NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1279                         nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1280                                         NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1281                         if (!udata) {
1282                                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1283                                 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1284                         }
1285                         nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1286                                         ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1287                         u64temp = (u64)nesqp->hwqp.sq_pbase;
1288                         nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1289                         nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1290
1291
1292                         if (!virt_wqs) {
1293                                 u64temp = (u64)nesqp->hwqp.sq_pbase;
1294                                 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1295                                 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1296                                 u64temp = (u64)nesqp->hwqp.rq_pbase;
1297                                 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1298                                 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1299                         } else {
1300                                 u64temp = (u64)nesqp->pbl_pbase;
1301                                 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1302                                 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1303                         }
1304
1305                         /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1306                                         nesvnic->next_qp_nic_index,
1307                                         nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1308                         spin_lock_irqsave(&nesdev->cqp.lock, flags);
1309                         nesqp->nesqp_context->misc2 |= cpu_to_le32(
1310                                         (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1311                                         NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1312                         nesvnic->next_qp_nic_index++;
1313                         if ((nesvnic->next_qp_nic_index > 3) ||
1314                                         (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1315                                 nesvnic->next_qp_nic_index = 0;
1316                         }
1317                         spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1318
1319                         nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1320                         u64temp = (u64)nesqp->hwqp.q2_pbase;
1321                         nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1322                         nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1323                         nesqp->nesqp_context->aeq_token_low =  cpu_to_le32((u32)((unsigned long)(nesqp)));
1324                         nesqp->nesqp_context->aeq_token_high =  cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1325                         nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1326                                         NES_QPCONTEXT_ORDIRD_AAH |
1327                                         ((((u32)nesadapter->max_irrq_wr) <<
1328                                         NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1329                         if (disable_mpa_crc) {
1330                                 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1331                                 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1332                         }
1333
1334
1335                         /* Create the QP */
1336                         cqp_request = nes_get_cqp_request(nesdev);
1337                         if (cqp_request == NULL) {
1338                                 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1339                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1340                                 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1341                                 kfree(nesqp->allocated_buffer);
1342                                 return ERR_PTR(-ENOMEM);
1343                         }
1344                         cqp_request->waiting = 1;
1345                         cqp_wqe = &cqp_request->cqp_wqe;
1346
1347                         if (!virt_wqs) {
1348                                 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1349                                         NES_CQP_QP_IWARP_STATE_IDLE;
1350                         } else {
1351                                 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1352                                         NES_CQP_QP_IWARP_STATE_IDLE;
1353                         }
1354                         opcode |= NES_CQP_QP_CQS_VALID;
1355                         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1356                         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1357                         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1358
1359                         u64temp = (u64)nesqp->nesqp_context_pbase;
1360                         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1361
1362                         atomic_set(&cqp_request->refcount, 2);
1363                         nes_post_cqp_request(nesdev, cqp_request);
1364
1365                         /* Wait for CQP */
1366                         nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1367                                         nesqp->hwqp.qp_id);
1368                         ret = wait_event_timeout(cqp_request->waitq,
1369                                         (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1370                         nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1371                                         " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1372                                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1373                                         nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1374                                         cqp_request->major_code, cqp_request->minor_code);
1375                         if ((!ret) || (cqp_request->major_code)) {
1376                                 nes_put_cqp_request(nesdev, cqp_request);
1377                                 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1378                                 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1379                                 kfree(nesqp->allocated_buffer);
1380                                 if (!ret) {
1381                                         return ERR_PTR(-ETIME);
1382                                 } else {
1383                                         return ERR_PTR(-EIO);
1384                                 }
1385                         }
1386
1387                         nes_put_cqp_request(nesdev, cqp_request);
1388
1389                         if (ibpd->uobject) {
1390                                 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1391                                 uresp.actual_sq_size = sq_size;
1392                                 uresp.actual_rq_size = rq_size;
1393                                 uresp.qp_id = nesqp->hwqp.qp_id;
1394                                 uresp.nes_drv_opt = nes_drv_opt;
1395                                 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1396                                         nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1397                                         nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1398                                         kfree(nesqp->allocated_buffer);
1399                                         return ERR_PTR(-EFAULT);
1400                                 }
1401                         }
1402
1403                         nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1404                                         nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1405                         spin_lock_init(&nesqp->lock);
1406                         nes_add_ref(&nesqp->ibqp);
1407                         break;
1408                 default:
1409                         nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1410                         return ERR_PTR(-EINVAL);
1411         }
1412
1413         nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1414
1415         /* update the QP table */
1416         nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1417         nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1418                         atomic_read(&nesvnic->netdev->refcnt));
1419
1420         return &nesqp->ibqp;
1421 }
1422
1423
1424 /**
1425  * nes_clean_cq
1426  */
1427 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1428 {
1429         u32 cq_head;
1430         u32 lo;
1431         u32 hi;
1432         u64 u64temp;
1433         unsigned long flags = 0;
1434
1435         spin_lock_irqsave(&nescq->lock, flags);
1436
1437         cq_head = nescq->hw_cq.cq_head;
1438         while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1439                 rmb();
1440                 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1441                 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1442                 u64temp = (((u64)hi) << 32) | ((u64)lo);
1443                 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1444                 if (u64temp == (u64)(unsigned long)nesqp) {
1445                         /* Zero the context value so cqe will be ignored */
1446                         nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1447                         nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1448                 }
1449
1450                 if (++cq_head >= nescq->hw_cq.cq_size)
1451                         cq_head = 0;
1452         }
1453
1454         spin_unlock_irqrestore(&nescq->lock, flags);
1455 }
1456
1457
1458 /**
1459  * nes_destroy_qp
1460  */
1461 static int nes_destroy_qp(struct ib_qp *ibqp)
1462 {
1463         struct nes_qp *nesqp = to_nesqp(ibqp);
1464         struct nes_ucontext *nes_ucontext;
1465         struct ib_qp_attr attr;
1466         struct iw_cm_id *cm_id;
1467         struct iw_cm_event cm_event;
1468         int ret;
1469
1470         atomic_inc(&sw_qps_destroyed);
1471         nesqp->destroyed = 1;
1472
1473         /* Blow away the connection if it exists. */
1474         if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1475                 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1476                 attr.qp_state = IB_QPS_ERR;
1477                 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1478         }
1479
1480         if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1481                         (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1482                 cm_id = nesqp->cm_id;
1483                 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1484                 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1485                 cm_event.local_addr = cm_id->local_addr;
1486                 cm_event.remote_addr = cm_id->remote_addr;
1487                 cm_event.private_data = NULL;
1488                 cm_event.private_data_len = 0;
1489
1490                 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1491                                 "QP%u. cm_id = %p, refcount = %u. \n",
1492                                 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1493
1494                 cm_id->rem_ref(cm_id);
1495                 ret = cm_id->event_handler(cm_id, &cm_event);
1496                 if (ret)
1497                         nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1498         }
1499
1500         if (nesqp->user_mode) {
1501                 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1502                         nes_ucontext = to_nesucontext(ibqp->uobject->context);
1503                         clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1504                         nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1505                         if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1506                                 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1507                         }
1508                 }
1509                 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1510                         nesqp->sq_kmapped = 0;
1511                         kunmap(nesqp->page);
1512                 }
1513         } else {
1514                 /* Clean any pending completions from the cq(s) */
1515                 if (nesqp->nesscq)
1516                         nes_clean_cq(nesqp, nesqp->nesscq);
1517
1518                 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1519                         nes_clean_cq(nesqp, nesqp->nesrcq);
1520         }
1521
1522         nes_rem_ref(&nesqp->ibqp);
1523         return 0;
1524 }
1525
1526
1527 /**
1528  * nes_create_cq
1529  */
1530 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1531                 int comp_vector,
1532                 struct ib_ucontext *context, struct ib_udata *udata)
1533 {
1534         u64 u64temp;
1535         struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1536         struct nes_device *nesdev = nesvnic->nesdev;
1537         struct nes_adapter *nesadapter = nesdev->nesadapter;
1538         struct nes_cq *nescq;
1539         struct nes_ucontext *nes_ucontext = NULL;
1540         struct nes_cqp_request *cqp_request;
1541         void *mem = NULL;
1542         struct nes_hw_cqp_wqe *cqp_wqe;
1543         struct nes_pbl *nespbl = NULL;
1544         struct nes_create_cq_req req;
1545         struct nes_create_cq_resp resp;
1546         u32 cq_num = 0;
1547         u32 opcode = 0;
1548         u32 pbl_entries = 1;
1549         int err;
1550         unsigned long flags;
1551         int ret;
1552
1553         if (entries > nesadapter->max_cqe)
1554                 return ERR_PTR(-EINVAL);
1555
1556         err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1557                         nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1558         if (err) {
1559                 return ERR_PTR(err);
1560         }
1561
1562         nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1563         if (!nescq) {
1564                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1565                 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1566                 return ERR_PTR(-ENOMEM);
1567         }
1568
1569         nescq->hw_cq.cq_size = max(entries + 1, 5);
1570         nescq->hw_cq.cq_number = cq_num;
1571         nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1572
1573
1574         if (context) {
1575                 nes_ucontext = to_nesucontext(context);
1576                 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1577                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1578                         kfree(nescq);
1579                         return ERR_PTR(-EFAULT);
1580                 }
1581                 nesvnic->mcrq_ucontext = nes_ucontext;
1582                 nes_ucontext->mcrqf = req.mcrqf;
1583                 if (nes_ucontext->mcrqf) {
1584                         if (nes_ucontext->mcrqf & 0x80000000)
1585                                 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1586                         else if (nes_ucontext->mcrqf & 0x40000000)
1587                                 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1588                         else
1589                                 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1590                         nescq->mcrqf = nes_ucontext->mcrqf;
1591                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1592                 }
1593                 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1594                                 (unsigned long)req.user_cq_buffer, entries);
1595                 err = 1;
1596                 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1597                         if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1598                                 list_del(&nespbl->list);
1599                                 err = 0;
1600                                 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1601                                                 nespbl);
1602                                 break;
1603                         }
1604                 }
1605                 if (err) {
1606                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1607                         kfree(nescq);
1608                         return ERR_PTR(-EFAULT);
1609                 }
1610
1611                 pbl_entries = nespbl->pbl_size >> 3;
1612                 nescq->cq_mem_size = 0;
1613         } else {
1614                 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1615                 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1616                                 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1617
1618                 /* allocate the physical buffer space */
1619                 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1620                                 &nescq->hw_cq.cq_pbase);
1621                 if (!mem) {
1622                         printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1623                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1624                         kfree(nescq);
1625                         return ERR_PTR(-ENOMEM);
1626                 }
1627
1628                 memset(mem, 0, nescq->cq_mem_size);
1629                 nescq->hw_cq.cq_vbase = mem;
1630                 nescq->hw_cq.cq_head = 0;
1631                 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1632                                 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1633                                 (u32)nescq->hw_cq.cq_pbase);
1634         }
1635
1636         nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1637         spin_lock_init(&nescq->lock);
1638
1639         /* send CreateCQ request to CQP */
1640         cqp_request = nes_get_cqp_request(nesdev);
1641         if (cqp_request == NULL) {
1642                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1643                 if (!context)
1644                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1645                                         nescq->hw_cq.cq_pbase);
1646                 else {
1647                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1648                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1649                         kfree(nespbl);
1650                 }
1651
1652                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1653                 kfree(nescq);
1654                 return ERR_PTR(-ENOMEM);
1655         }
1656         cqp_request->waiting = 1;
1657         cqp_wqe = &cqp_request->cqp_wqe;
1658
1659         opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1660                         NES_CQP_CQ_CHK_OVERFLOW |
1661                         NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1662
1663         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1664
1665         if (pbl_entries != 1) {
1666                 if (pbl_entries > 32) {
1667                         /* use 4k pbl */
1668                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1669                         if (nesadapter->free_4kpbl == 0) {
1670                                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1671                                 nes_free_cqp_request(nesdev, cqp_request);
1672                                 if (!context)
1673                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1674                                                         nescq->hw_cq.cq_pbase);
1675                                 else {
1676                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1677                                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1678                                         kfree(nespbl);
1679                                 }
1680                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1681                                 kfree(nescq);
1682                                 return ERR_PTR(-ENOMEM);
1683                         } else {
1684                                 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1685                                 nescq->virtual_cq = 2;
1686                                 nesadapter->free_4kpbl--;
1687                         }
1688                 } else {
1689                         /* use 256 byte pbl */
1690                         nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1691                         if (nesadapter->free_256pbl == 0) {
1692                                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1693                                 nes_free_cqp_request(nesdev, cqp_request);
1694                                 if (!context)
1695                                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1696                                                         nescq->hw_cq.cq_pbase);
1697                                 else {
1698                                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1699                                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1700                                         kfree(nespbl);
1701                                 }
1702                                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1703                                 kfree(nescq);
1704                                 return ERR_PTR(-ENOMEM);
1705                         } else {
1706                                 opcode |= NES_CQP_CQ_VIRT;
1707                                 nescq->virtual_cq = 1;
1708                                 nesadapter->free_256pbl--;
1709                         }
1710                 }
1711         }
1712
1713         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1714
1715         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1716         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1717         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1718                         (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1719
1720         if (context) {
1721                 if (pbl_entries != 1)
1722                         u64temp = (u64)nespbl->pbl_pbase;
1723                 else
1724                         u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1725                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1726                                 nes_ucontext->mmap_db_index[0]);
1727         } else {
1728                 u64temp = (u64)nescq->hw_cq.cq_pbase;
1729                 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1730         }
1731         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1732         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1733         u64temp = (u64)(unsigned long)&nescq->hw_cq;
1734         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1735                         cpu_to_le32((u32)(u64temp >> 1));
1736         cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1737                         cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1738
1739         atomic_set(&cqp_request->refcount, 2);
1740         nes_post_cqp_request(nesdev, cqp_request);
1741
1742         /* Wait for CQP */
1743         nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1744                         nescq->hw_cq.cq_number);
1745         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1746                         NES_EVENT_TIMEOUT * 2);
1747         nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1748                         nescq->hw_cq.cq_number, ret);
1749         if ((!ret) || (cqp_request->major_code)) {
1750                 nes_put_cqp_request(nesdev, cqp_request);
1751                 if (!context)
1752                         pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1753                                         nescq->hw_cq.cq_pbase);
1754                 else {
1755                         pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1756                                             nespbl->pbl_vbase, nespbl->pbl_pbase);
1757                         kfree(nespbl);
1758                 }
1759                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1760                 kfree(nescq);
1761                 return ERR_PTR(-EIO);
1762         }
1763         nes_put_cqp_request(nesdev, cqp_request);
1764
1765         if (context) {
1766                 /* free the nespbl */
1767                 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1768                                 nespbl->pbl_pbase);
1769                 kfree(nespbl);
1770                 resp.cq_id = nescq->hw_cq.cq_number;
1771                 resp.cq_size = nescq->hw_cq.cq_size;
1772                 resp.mmap_db_index = 0;
1773                 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1774                         nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1775                         kfree(nescq);
1776                         return ERR_PTR(-EFAULT);
1777                 }
1778         }
1779
1780         return &nescq->ibcq;
1781 }
1782
1783
1784 /**
1785  * nes_destroy_cq
1786  */
1787 static int nes_destroy_cq(struct ib_cq *ib_cq)
1788 {
1789         struct nes_cq *nescq;
1790         struct nes_device *nesdev;
1791         struct nes_vnic *nesvnic;
1792         struct nes_adapter *nesadapter;
1793         struct nes_hw_cqp_wqe *cqp_wqe;
1794         struct nes_cqp_request *cqp_request;
1795         unsigned long flags;
1796         u32 opcode = 0;
1797         int ret;
1798
1799         if (ib_cq == NULL)
1800                 return 0;
1801
1802         nescq = to_nescq(ib_cq);
1803         nesvnic = to_nesvnic(ib_cq->device);
1804         nesdev = nesvnic->nesdev;
1805         nesadapter = nesdev->nesadapter;
1806
1807         nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1808
1809         /* Send DestroyCQ request to CQP */
1810         cqp_request = nes_get_cqp_request(nesdev);
1811         if (cqp_request == NULL) {
1812                 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1813                 return -ENOMEM;
1814         }
1815         cqp_request->waiting = 1;
1816         cqp_wqe = &cqp_request->cqp_wqe;
1817         opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1818         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1819         if (nescq->virtual_cq == 1) {
1820                 nesadapter->free_256pbl++;
1821                 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1822                         printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1823                                         __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1824                 }
1825         } else if (nescq->virtual_cq == 2) {
1826                 nesadapter->free_4kpbl++;
1827                 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1828                         printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1829                                         __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1830                 }
1831                 opcode |= NES_CQP_CQ_4KB_CHUNK;
1832         }
1833
1834         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1835
1836         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1837         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1838         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1839                 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1840         if (!nescq->mcrqf)
1841                 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1842
1843         atomic_set(&cqp_request->refcount, 2);
1844         nes_post_cqp_request(nesdev, cqp_request);
1845
1846         /* Wait for CQP */
1847         nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1848                         nescq->hw_cq.cq_number);
1849         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1850                         NES_EVENT_TIMEOUT);
1851         nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1852                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1853                         nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1854                         cqp_request->minor_code);
1855         if (!ret) {
1856                 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1857                                         nescq->hw_cq.cq_number);
1858                 ret = -ETIME;
1859         } else if (cqp_request->major_code) {
1860                 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1861                                         nescq->hw_cq.cq_number);
1862                 ret = -EIO;
1863         } else {
1864                 ret = 0;
1865         }
1866         nes_put_cqp_request(nesdev, cqp_request);
1867
1868         if (nescq->cq_mem_size)
1869                 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1870                                     nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1871         kfree(nescq);
1872
1873         return ret;
1874 }
1875
1876 /**
1877  * root_256
1878  */
1879 static u32 root_256(struct nes_device *nesdev,
1880                     struct nes_root_vpbl *root_vpbl,
1881                     struct nes_root_vpbl *new_root,
1882                     u16 pbl_count_4k)
1883 {
1884         u64 leaf_pbl;
1885         int i, j, k;
1886
1887         if (pbl_count_4k == 1) {
1888                 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1889                                                 512, &new_root->pbl_pbase);
1890
1891                 if (new_root->pbl_vbase == NULL)
1892                         return 0;
1893
1894                 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1895                 for (i = 0; i < 16; i++) {
1896                         new_root->pbl_vbase[i].pa_low =
1897                                 cpu_to_le32((u32)leaf_pbl);
1898                         new_root->pbl_vbase[i].pa_high =
1899                                 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1900                         leaf_pbl += 256;
1901                 }
1902         } else {
1903                 for (i = 3; i >= 0; i--) {
1904                         j = i * 16;
1905                         root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1906                         leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1907                             (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1908                                 << 32);
1909                         for (k = 1; k < 16; k++) {
1910                                 leaf_pbl += 256;
1911                                 root_vpbl->pbl_vbase[j + k].pa_low =
1912                                                 cpu_to_le32((u32)leaf_pbl);
1913                                 root_vpbl->pbl_vbase[j + k].pa_high =
1914                                     cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1915                         }
1916                 }
1917         }
1918
1919         return 1;
1920 }
1921
1922
1923 /**
1924  * nes_reg_mr
1925  */
1926 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1927                 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1928                 dma_addr_t single_buffer, u16 pbl_count_4k,
1929                 u16 residual_page_count_4k, int acc, u64 *iova_start,
1930                 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1931 {
1932         struct nes_hw_cqp_wqe *cqp_wqe;
1933         struct nes_cqp_request *cqp_request;
1934         unsigned long flags;
1935         int ret;
1936         struct nes_adapter *nesadapter = nesdev->nesadapter;
1937         uint pg_cnt = 0;
1938         u16 pbl_count_256 = 0;
1939         u16 pbl_count = 0;
1940         u8  use_256_pbls = 0;
1941         u8  use_4k_pbls = 0;
1942         u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1943         struct nes_root_vpbl new_root = {0, 0, 0};
1944         u32 opcode = 0;
1945         u16 major_code;
1946
1947         /* Register the region with the adapter */
1948         cqp_request = nes_get_cqp_request(nesdev);
1949         if (cqp_request == NULL) {
1950                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1951                 return -ENOMEM;
1952         }
1953         cqp_request->waiting = 1;
1954         cqp_wqe = &cqp_request->cqp_wqe;
1955
1956         if (pbl_count_4k) {
1957                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1958
1959                 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1960                 pbl_count_256 = (pg_cnt + 31) / 32;
1961                 if (pg_cnt <= 32) {
1962                         if (pbl_count_256 <= nesadapter->free_256pbl)
1963                                 use_256_pbls = 1;
1964                         else if (pbl_count_4k <= nesadapter->free_4kpbl)
1965                                 use_4k_pbls = 1;
1966                 } else if (pg_cnt <= 2048) {
1967                         if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1968                             (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1969                                 use_4k_pbls = 1;
1970                         } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1971                                 use_256_pbls = 1;
1972                                 use_two_level = 1;
1973                         } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1974                                 use_4k_pbls = 1;
1975                         }
1976                 } else {
1977                         if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1978                                 use_4k_pbls = 1;
1979                 }
1980
1981                 if (use_256_pbls) {
1982                         pbl_count = pbl_count_256;
1983                         nesadapter->free_256pbl -= pbl_count + use_two_level;
1984                 } else if (use_4k_pbls) {
1985                         pbl_count =  pbl_count_4k;
1986                         nesadapter->free_4kpbl -= pbl_count + use_two_level;
1987                 } else {
1988                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1989                         nes_debug(NES_DBG_MR, "Out of Pbls\n");
1990                         nes_free_cqp_request(nesdev, cqp_request);
1991                         return -ENOMEM;
1992                 }
1993
1994                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1995         }
1996
1997         if (use_256_pbls && use_two_level) {
1998                 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1999                         if (new_root.pbl_pbase != 0)
2000                                 root_vpbl = &new_root;
2001                 } else {
2002                         spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2003                         nesadapter->free_256pbl += pbl_count_256 + use_two_level;
2004                         use_256_pbls = 0;
2005
2006                         if (pbl_count_4k == 1)
2007                                 use_two_level = 0;
2008                         pbl_count = pbl_count_4k;
2009
2010                         if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2011                                 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2012                                 use_4k_pbls = 1;
2013                         }
2014                         spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2015
2016                         if (use_4k_pbls == 0)
2017                                 return -ENOMEM;
2018                 }
2019         }
2020
2021         opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2022                                         NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2023         if (acc & IB_ACCESS_LOCAL_WRITE)
2024                 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2025         if (acc & IB_ACCESS_REMOTE_WRITE)
2026                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2027         if (acc & IB_ACCESS_REMOTE_READ)
2028                 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2029         if (acc & IB_ACCESS_MW_BIND)
2030                 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2031
2032         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2033         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2034         set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2035         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2036
2037         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2038                         cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2039         cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2040                         cpu_to_le32(nespd->pd_id & 0x00007fff);
2041         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2042
2043         if (pbl_count == 0) {
2044                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2045         } else {
2046                 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2047                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2048                 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
2049
2050                 if (use_4k_pbls)
2051                         cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2052         }
2053         barrier();
2054
2055         atomic_set(&cqp_request->refcount, 2);
2056         nes_post_cqp_request(nesdev, cqp_request);
2057
2058         /* Wait for CQP */
2059         ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2060                         NES_EVENT_TIMEOUT);
2061         nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2062                         " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2063                         stag, ret, cqp_request->major_code, cqp_request->minor_code);
2064         major_code = cqp_request->major_code;
2065         nes_put_cqp_request(nesdev, cqp_request);
2066
2067         if ((!ret || major_code) && pbl_count != 0) {
2068                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2069                 if (use_256_pbls)
2070                         nesadapter->free_256pbl += pbl_count + use_two_level;
2071                 else if (use_4k_pbls)
2072                         nesadapter->free_4kpbl += pbl_count + use_two_level;
2073                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2074         }
2075         if (new_root.pbl_pbase)
2076                 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2077                                     new_root.pbl_pbase);
2078
2079         if (!ret)
2080                 return -ETIME;
2081         else if (major_code)
2082                 return -EIO;
2083
2084         *actual_pbl_cnt = pbl_count + use_two_level;
2085         *used_4k_pbls = use_4k_pbls;
2086         return 0;
2087 }
2088
2089
2090 /**
2091  * nes_reg_phys_mr
2092  */
2093 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2094                 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2095                 u64 * iova_start)
2096 {
2097         u64 region_length;
2098         struct nes_pd *nespd = to_nespd(ib_pd);
2099         struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2100         struct nes_device *nesdev = nesvnic->nesdev;
2101         struct nes_adapter *nesadapter = nesdev->nesadapter;
2102         struct nes_mr *nesmr;
2103         struct ib_mr *ibmr;
2104         struct nes_vpbl vpbl;
2105         struct nes_root_vpbl root_vpbl;
2106         u32 stag;
2107         u32 i;
2108         unsigned long mask;
2109         u32 stag_index = 0;
2110         u32 next_stag_index = 0;
2111         u32 driver_key = 0;
2112         u32 root_pbl_index = 0;
2113         u32 cur_pbl_index = 0;
2114         int err = 0, pbl_depth = 0;
2115         int ret = 0;
2116         u16 pbl_count = 0;
2117         u8 single_page = 1;
2118         u8 stag_key = 0;
2119
2120         pbl_depth = 0;
2121         region_length = 0;
2122         vpbl.pbl_vbase = NULL;
2123         root_vpbl.pbl_vbase = NULL;
2124         root_vpbl.pbl_pbase = 0;
2125
2126         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2127         stag_key = (u8)next_stag_index;
2128
2129         driver_key = 0;
2130
2131         next_stag_index >>= 8;
2132         next_stag_index %= nesadapter->max_mr;
2133         if (num_phys_buf > (1024*512)) {
2134                 return ERR_PTR(-E2BIG);
2135         }
2136
2137         if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2138                 return ERR_PTR(-EINVAL);
2139
2140         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2141                         &stag_index, &next_stag_index);
2142         if (err) {
2143                 return ERR_PTR(err);
2144         }
2145
2146         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2147         if (!nesmr) {
2148                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2149                 return ERR_PTR(-ENOMEM);
2150         }
2151
2152         for (i = 0; i < num_phys_buf; i++) {
2153
2154                 if ((i & 0x01FF) == 0) {
2155                         if (root_pbl_index == 1) {
2156                                 /* Allocate the root PBL */
2157                                 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2158                                                 &root_vpbl.pbl_pbase);
2159                                 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2160                                                 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2161                                 if (!root_vpbl.pbl_vbase) {
2162                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2163                                                         vpbl.pbl_pbase);
2164                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2165                                         kfree(nesmr);
2166                                         return ERR_PTR(-ENOMEM);
2167                                 }
2168                                 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2169                                 if (!root_vpbl.leaf_vpbl) {
2170                                         pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2171                                                         root_vpbl.pbl_pbase);
2172                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2173                                                         vpbl.pbl_pbase);
2174                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2175                                         kfree(nesmr);
2176                                         return ERR_PTR(-ENOMEM);
2177                                 }
2178                                 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2179                                 root_vpbl.pbl_vbase[0].pa_high =
2180                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2181                                 root_vpbl.leaf_vpbl[0] = vpbl;
2182                         }
2183                         /* Allocate a 4K buffer for the PBL */
2184                         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2185                                         &vpbl.pbl_pbase);
2186                         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2187                                         vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2188                         if (!vpbl.pbl_vbase) {
2189                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2190                                 ibmr = ERR_PTR(-ENOMEM);
2191                                 kfree(nesmr);
2192                                 goto reg_phys_err;
2193                         }
2194                         /* Fill in the root table */
2195                         if (1 <= root_pbl_index) {
2196                                 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2197                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2198                                 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2199                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2200                                 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2201                         }
2202                         root_pbl_index++;
2203                         cur_pbl_index = 0;
2204                 }
2205
2206                 mask = !buffer_list[i].size;
2207                 if (i != 0)
2208                         mask |= buffer_list[i].addr;
2209                 if (i != num_phys_buf - 1)
2210                         mask |= buffer_list[i].addr + buffer_list[i].size;
2211
2212                 if (mask & ~PAGE_MASK) {
2213                         nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2214                         nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2215                         ibmr = ERR_PTR(-EINVAL);
2216                         kfree(nesmr);
2217                         goto reg_phys_err;
2218                 }
2219
2220                 region_length += buffer_list[i].size;
2221                 if ((i != 0) && (single_page)) {
2222                         if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2223                                 single_page = 0;
2224                 }
2225                 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr & PAGE_MASK);
2226                 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2227                                 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2228         }
2229
2230         stag = stag_index << 8;
2231         stag |= driver_key;
2232         stag += (u32)stag_key;
2233
2234         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2235                         " length = 0x%016lX, index = 0x%08X\n",
2236                         stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2237
2238         /* Make the leaf PBL the root if only one PBL */
2239         if (root_pbl_index == 1) {
2240                 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2241         }
2242
2243         if (single_page) {
2244                 pbl_count = 0;
2245         } else {
2246                 pbl_count = root_pbl_index;
2247         }
2248         ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2249                         buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2250                         &nesmr->pbls_used, &nesmr->pbl_4k);
2251
2252         if (ret == 0) {
2253                 nesmr->ibmr.rkey = stag;
2254                 nesmr->ibmr.lkey = stag;
2255                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2256                 ibmr = &nesmr->ibmr;
2257         } else {
2258                 kfree(nesmr);
2259                 ibmr = ERR_PTR(-ENOMEM);
2260         }
2261
2262         reg_phys_err:
2263         /* free the resources */
2264         if (root_pbl_index == 1) {
2265                 /* single PBL case */
2266                 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2267         } else {
2268                 for (i=0; i<root_pbl_index; i++) {
2269                         pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2270                                         root_vpbl.leaf_vpbl[i].pbl_pbase);
2271                 }
2272                 kfree(root_vpbl.leaf_vpbl);
2273                 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2274                                 root_vpbl.pbl_pbase);
2275         }
2276
2277         return ibmr;
2278 }
2279
2280
2281 /**
2282  * nes_get_dma_mr
2283  */
2284 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2285 {
2286         struct ib_phys_buf bl;
2287         u64 kva = 0;
2288
2289         nes_debug(NES_DBG_MR, "\n");
2290
2291         bl.size = (u64)0xffffffffffULL;
2292         bl.addr = 0;
2293         return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2294 }
2295
2296
2297 /**
2298  * nes_reg_user_mr
2299  */
2300 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2301                 u64 virt, int acc, struct ib_udata *udata)
2302 {
2303         u64 iova_start;
2304         __le64 *pbl;
2305         u64 region_length;
2306         dma_addr_t last_dma_addr = 0;
2307         dma_addr_t first_dma_addr = 0;
2308         struct nes_pd *nespd = to_nespd(pd);
2309         struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2310         struct nes_device *nesdev = nesvnic->nesdev;
2311         struct nes_adapter *nesadapter = nesdev->nesadapter;
2312         struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2313         struct ib_umem_chunk *chunk;
2314         struct nes_ucontext *nes_ucontext;
2315         struct nes_pbl *nespbl;
2316         struct nes_mr *nesmr;
2317         struct ib_umem *region;
2318         struct nes_mem_reg_req req;
2319         struct nes_vpbl vpbl;
2320         struct nes_root_vpbl root_vpbl;
2321         int nmap_index, page_index;
2322         int page_count = 0;
2323         int err, pbl_depth = 0;
2324         int chunk_pages;
2325         int ret;
2326         u32 stag;
2327         u32 stag_index = 0;
2328         u32 next_stag_index;
2329         u32 driver_key;
2330         u32 root_pbl_index = 0;
2331         u32 cur_pbl_index = 0;
2332         u32 skip_pages;
2333         u16 pbl_count;
2334         u8 single_page = 1;
2335         u8 stag_key;
2336
2337         region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
2338         if (IS_ERR(region)) {
2339                 return (struct ib_mr *)region;
2340         }
2341
2342         nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2343                         " offset = %u, page size = %u.\n",
2344                         (unsigned long int)start, (unsigned long int)virt, (u32)length,
2345                         region->offset, region->page_size);
2346
2347         skip_pages = ((u32)region->offset) >> 12;
2348
2349         if (ib_copy_from_udata(&req, udata, sizeof(req)))
2350                 return ERR_PTR(-EFAULT);
2351         nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2352
2353         switch (req.reg_type) {
2354                 case IWNES_MEMREG_TYPE_MEM:
2355                         pbl_depth = 0;
2356                         region_length = 0;
2357                         vpbl.pbl_vbase = NULL;
2358                         root_vpbl.pbl_vbase = NULL;
2359                         root_vpbl.pbl_pbase = 0;
2360
2361                         get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2362                         stag_key = (u8)next_stag_index;
2363
2364                         driver_key = next_stag_index & 0x70000000;
2365
2366                         next_stag_index >>= 8;
2367                         next_stag_index %= nesadapter->max_mr;
2368
2369                         err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2370                                         nesadapter->max_mr, &stag_index, &next_stag_index);
2371                         if (err) {
2372                                 ib_umem_release(region);
2373                                 return ERR_PTR(err);
2374                         }
2375
2376                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2377                         if (!nesmr) {
2378                                 ib_umem_release(region);
2379                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2380                                 return ERR_PTR(-ENOMEM);
2381                         }
2382                         nesmr->region = region;
2383
2384                         list_for_each_entry(chunk, &region->chunk_list, list) {
2385                                 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2386                                                 chunk->nents, chunk->nmap);
2387                                 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2388                                         if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2389                                                 ib_umem_release(region);
2390                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2391                                                 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2392                                                                 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2393                                                 ibmr = ERR_PTR(-EINVAL);
2394                                                 kfree(nesmr);
2395                                                 goto reg_user_mr_err;
2396                                         }
2397
2398                                         if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2399                                                 ib_umem_release(region);
2400                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2401                                                                 stag_index);
2402                                                 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2403                                                 ibmr = ERR_PTR(-EINVAL);
2404                                                 kfree(nesmr);
2405                                                 goto reg_user_mr_err;
2406                                         }
2407
2408                                         region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2409                                         chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2410                                         region_length -= skip_pages << 12;
2411                                         for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2412                                                 skip_pages = 0;
2413                                                 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2414                                                         goto enough_pages;
2415                                                 if ((page_count&0x01FF) == 0) {
2416                                                         if (page_count >= 1024 * 512) {
2417                                                                 ib_umem_release(region);
2418                                                                 nes_free_resource(nesadapter,
2419                                                                                 nesadapter->allocated_mrs, stag_index);
2420                                                                 kfree(nesmr);
2421                                                                 ibmr = ERR_PTR(-E2BIG);
2422                                                                 goto reg_user_mr_err;
2423                                                         }
2424                                                         if (root_pbl_index == 1) {
2425                                                                 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2426                                                                                 8192, &root_vpbl.pbl_pbase);
2427                                                                 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2428                                                                                 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2429                                                                 if (!root_vpbl.pbl_vbase) {
2430                                                                         ib_umem_release(region);
2431                                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2432                                                                                         vpbl.pbl_pbase);
2433                                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2434                                                                                         stag_index);
2435                                                                         kfree(nesmr);
2436                                                                         ibmr = ERR_PTR(-ENOMEM);
2437                                                                         goto reg_user_mr_err;
2438                                                                 }
2439                                                                 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2440                                                                                 GFP_KERNEL);
2441                                                                 if (!root_vpbl.leaf_vpbl) {
2442                                                                         ib_umem_release(region);
2443                                                                         pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2444                                                                                         root_vpbl.pbl_pbase);
2445                                                                         pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2446                                                                                         vpbl.pbl_pbase);
2447                                                                         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2448                                                                                         stag_index);
2449                                                                         kfree(nesmr);
2450                                                                         ibmr = ERR_PTR(-ENOMEM);
2451                                                                         goto reg_user_mr_err;
2452                                                                 }
2453                                                                 root_vpbl.pbl_vbase[0].pa_low =
2454                                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2455                                                                 root_vpbl.pbl_vbase[0].pa_high =
2456                                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2457                                                                 root_vpbl.leaf_vpbl[0] = vpbl;
2458                                                         }
2459                                                         vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2460                                                                         &vpbl.pbl_pbase);
2461                                                         nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2462                                                                         vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2463                                                         if (!vpbl.pbl_vbase) {
2464                                                                 ib_umem_release(region);
2465                                                                 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2466                                                                 ibmr = ERR_PTR(-ENOMEM);
2467                                                                 kfree(nesmr);
2468                                                                 goto reg_user_mr_err;
2469                                                         }
2470                                                         if (1 <= root_pbl_index) {
2471                                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2472                                                                                 cpu_to_le32((u32)vpbl.pbl_pbase);
2473                                                                 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2474                                                                                 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2475                                                                 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2476                                                         }
2477                                                         root_pbl_index++;
2478                                                         cur_pbl_index = 0;
2479                                                 }
2480                                                 if (single_page) {
2481                                                         if (page_count != 0) {
2482                                                                 if ((last_dma_addr+4096) !=
2483                                                                                 (sg_dma_address(&chunk->page_list[nmap_index])+
2484                                                                                 (page_index*4096)))
2485                                                                         single_page = 0;
2486                                                                 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2487                                                                                 (page_index*4096);
2488                                                         } else {
2489                                                                 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2490                                                                                 (page_index*4096);
2491                                                                 last_dma_addr = first_dma_addr;
2492                                                         }
2493                                                 }
2494
2495                                                 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2496                                                                 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2497                                                                 (page_index*4096)));
2498                                                 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2499                                                                 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2500                                                                 (page_index*4096))) >> 32)));
2501                                                 cur_pbl_index++;
2502                                                 page_count++;
2503                                         }
2504                                 }
2505                         }
2506                         enough_pages:
2507                         nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2508                                         " stag_key=0x%08x\n",
2509                                         stag_index, driver_key, stag_key);
2510                         stag = stag_index << 8;
2511                         stag |= driver_key;
2512                         stag += (u32)stag_key;
2513
2514                         iova_start = virt;
2515                         /* Make the leaf PBL the root if only one PBL */
2516                         if (root_pbl_index == 1) {
2517                                 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2518                         }
2519
2520                         if (single_page) {
2521                                 pbl_count = 0;
2522                         } else {
2523                                 pbl_count = root_pbl_index;
2524                                 first_dma_addr = 0;
2525                         }
2526                         nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2527                                         " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2528                                         stag, (unsigned int)iova_start,
2529                                         (unsigned int)region_length, stag_index,
2530                                         (unsigned long long)region->length, pbl_count);
2531                         ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2532                                          first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2533                                          &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2534
2535                         nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2536
2537                         if (ret == 0) {
2538                                 nesmr->ibmr.rkey = stag;
2539                                 nesmr->ibmr.lkey = stag;
2540                                 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2541                                 ibmr = &nesmr->ibmr;
2542                         } else {
2543                                 ib_umem_release(region);
2544                                 kfree(nesmr);
2545                                 ibmr = ERR_PTR(-ENOMEM);
2546                         }
2547
2548                         reg_user_mr_err:
2549                         /* free the resources */
2550                         if (root_pbl_index == 1) {
2551                                 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2552                                                 vpbl.pbl_pbase);
2553                         } else {
2554                                 for (page_index=0; page_index<root_pbl_index; page_index++) {
2555                                         pci_free_consistent(nesdev->pcidev, 4096,
2556                                                         root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2557                                                         root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2558                                 }
2559                                 kfree(root_vpbl.leaf_vpbl);
2560                                 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2561                                                 root_vpbl.pbl_pbase);
2562                         }
2563
2564                         nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2565
2566                         return ibmr;
2567                 case IWNES_MEMREG_TYPE_QP:
2568                 case IWNES_MEMREG_TYPE_CQ:
2569                         nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2570                         if (!nespbl) {
2571                                 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2572                                 ib_umem_release(region);
2573                                 return ERR_PTR(-ENOMEM);
2574                         }
2575                         nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2576                         if (!nesmr) {
2577                                 ib_umem_release(region);
2578                                 kfree(nespbl);
2579                                 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2580                                 return ERR_PTR(-ENOMEM);
2581                         }
2582                         nesmr->region = region;
2583                         nes_ucontext = to_nesucontext(pd->uobject->context);
2584                         pbl_depth = region->length >> 12;
2585                         pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2586                         nespbl->pbl_size = pbl_depth*sizeof(u64);
2587                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2588                                 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2589                         } else {
2590                                 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2591                         }
2592
2593                         nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2594                                         nespbl->pbl_size, pbl_depth);
2595                         pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2596                                         &nespbl->pbl_pbase);
2597                         if (!pbl) {
2598                                 ib_umem_release(region);
2599                                 kfree(nesmr);
2600                                 kfree(nespbl);
2601                                 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2602                                 return ERR_PTR(-ENOMEM);
2603                         }
2604
2605                         nespbl->pbl_vbase = (u64 *)pbl;
2606                         nespbl->user_base = start;
2607                         nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2608                                         " pbl_vbase=%p user_base=0x%lx\n",
2609                                   nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2610                                   (void *) nespbl->pbl_vbase, nespbl->user_base);
2611
2612                         list_for_each_entry(chunk, &region->chunk_list, list) {
2613                                 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2614                                         chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2615                                         chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2616                                         nespbl->page = sg_page(&chunk->page_list[0]);
2617                                         for (page_index=0; page_index<chunk_pages; page_index++) {
2618                                                 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2619                                                                 (sg_dma_address(&chunk->page_list[nmap_index])+
2620                                                                 (page_index*4096)));
2621                                                 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2622                                                                 (sg_dma_address(&chunk->page_list[nmap_index])+
2623                                                                 (page_index*4096)))>>32);
2624                                                 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2625                                                                 (unsigned long long)*pbl,
2626                                                                 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2627                                                 pbl++;
2628                                         }
2629                                 }
2630                         }
2631                         if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2632                                 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2633                         } else {
2634                                 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2635                         }
2636                         nesmr->ibmr.rkey = -1;
2637                         nesmr->ibmr.lkey = -1;
2638                         nesmr->mode = req.reg_type;
2639                         return &nesmr->ibmr;
2640         }
2641
2642         return ERR_PTR(-ENOSYS);
2643 }
2644
2645
2646 /**
2647  * nes_dereg_mr
2648  */
2649 static int nes_dereg_mr(struct ib_mr *ib_mr)
2650 {
2651         struct nes_mr *nesmr = to_nesmr(ib_mr);
2652         struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2653         struct nes_device *nesdev = nesvnic->nesdev;
2654         struct nes_adapter *nesadapter = nesdev->nesadapter;
2655         struct nes_hw_cqp_wqe *cqp_wqe;
2656         struct nes_cqp_request *cqp_request;
2657         unsigned long flags;
2658         int ret;
2659         u16 major_code;
2660         u16 minor_code;
2661
2662         if (nesmr->region) {
2663                 ib_umem_release(nesmr->region);
2664         }
2665         if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2666                 kfree(nesmr);
2667                 return 0;
2668         }
2669
2670         /* Deallocate the region with the adapter */
2671
2672         cqp_request = nes_get_cqp_request(nesdev);
2673         if (cqp_request == NULL) {
2674                 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2675                 return -ENOMEM;
2676         }
2677         cqp_request->waiting = 1;
2678         cqp_wqe = &cqp_request->cqp_wqe;
2679
2680         nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2681         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2682                         NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2683                         NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2684         set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2685
2686         atomic_set(&cqp_request->refcount, 2);
2687         nes_post_cqp_request(nesdev, cqp_request);
2688
2689         /* Wait for CQP */
2690         nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2691         ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2692                         NES_EVENT_TIMEOUT);
2693         nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2694                         " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2695                         ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2696
2697         major_code = cqp_request->major_code;
2698         minor_code = cqp_request->minor_code;
2699
2700         nes_put_cqp_request(nesdev, cqp_request);
2701
2702         if (!ret) {
2703                 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2704                                 " ib_mr=%p, rkey = 0x%08X\n",
2705                                 ib_mr, ib_mr->rkey);
2706                 return -ETIME;
2707         } else if (major_code) {
2708                 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2709                                 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2710                                 major_code, minor_code, ib_mr, ib_mr->rkey);
2711                 return -EIO;
2712         }
2713
2714         if (nesmr->pbls_used != 0) {
2715                 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2716                 if (nesmr->pbl_4k) {
2717                         nesadapter->free_4kpbl += nesmr->pbls_used;
2718                         if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2719                                 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2720                                         "exceeded the max(%u)\n",
2721                                         nesadapter->free_4kpbl,
2722                                         nesadapter->max_4kpbl);
2723                 } else {
2724                         nesadapter->free_256pbl += nesmr->pbls_used;
2725                         if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2726                                 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2727                                         "exceeded the max(%u)\n",
2728                                         nesadapter->free_256pbl,
2729                                         nesadapter->max_256pbl);
2730                 }
2731                 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2732         }
2733         nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2734                         (ib_mr->rkey & 0x0fffff00) >> 8);
2735
2736         kfree(nesmr);
2737
2738         return 0;
2739 }
2740
2741
2742 /**