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