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