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