2 * Copyright (c) 2006 - 2008 NetEffect, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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>
40 #include <rdma/ib_verbs.h>
41 #include <rdma/iw_cm.h>
42 #include <rdma/ib_user_verbs.h>
46 #include <rdma/ib_umem.h>
48 atomic_t mod_qp_timouts;
50 atomic_t sw_qps_destroyed;
52 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
57 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) {
59 struct nes_pd *nespd = to_nespd(ibpd);
60 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
61 struct nes_device *nesdev = nesvnic->nesdev;
62 struct nes_adapter *nesadapter = nesdev->nesadapter;
63 struct nes_cqp_request *cqp_request;
66 struct nes_hw_cqp_wqe *cqp_wqe;
70 u32 next_stag_index = 0;
74 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
75 stag_key = (u8)next_stag_index;
79 next_stag_index >>= 8;
80 next_stag_index %= nesadapter->max_mr;
82 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
83 nesadapter->max_mr, &stag_index, &next_stag_index);
88 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
90 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
91 return ERR_PTR(-ENOMEM);
94 stag = stag_index << 8;
96 stag += (u32)stag_key;
98 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
101 /* Register the region with the adapter */
102 cqp_request = nes_get_cqp_request(nesdev);
103 if (cqp_request == NULL) {
105 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
106 return ERR_PTR(-ENOMEM);
109 cqp_request->waiting = 1;
110 cqp_wqe = &cqp_request->cqp_wqe;
112 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
113 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
114 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
115 NES_CQP_STAG_REM_ACC_EN);
117 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
118 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
119 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
121 atomic_set(&cqp_request->refcount, 2);
122 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
125 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
127 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
128 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
129 stag, ret, cqp_request->major_code, cqp_request->minor_code);
130 if ((!ret) || (cqp_request->major_code)) {
131 if (atomic_dec_and_test(&cqp_request->refcount)) {
132 if (cqp_request->dynamic) {
135 spin_lock_irqsave(&nesdev->cqp.lock, flags);
136 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
137 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
141 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
143 return ERR_PTR(-ETIME);
145 return ERR_PTR(-ENOMEM);
148 if (atomic_dec_and_test(&cqp_request->refcount)) {
149 if (cqp_request->dynamic) {
152 spin_lock_irqsave(&nesdev->cqp.lock, flags);
153 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
154 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
159 nesmr->ibmw.rkey = stag;
160 nesmr->mode = IWNES_MEMREG_TYPE_MW;
163 nesmr->pbls_used = 0;
172 static int nes_dealloc_mw(struct ib_mw *ibmw)
174 struct nes_mr *nesmr = to_nesmw(ibmw);
175 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
176 struct nes_device *nesdev = nesvnic->nesdev;
177 struct nes_adapter *nesadapter = nesdev->nesadapter;
178 struct nes_hw_cqp_wqe *cqp_wqe;
179 struct nes_cqp_request *cqp_request;
184 /* Deallocate the window with the adapter */
185 cqp_request = nes_get_cqp_request(nesdev);
186 if (cqp_request == NULL) {
187 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
190 cqp_request->waiting = 1;
191 cqp_wqe = &cqp_request->cqp_wqe;
192 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
193 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
194 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
196 atomic_set(&cqp_request->refcount, 2);
197 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
200 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
202 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
204 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
205 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
206 ret, cqp_request->major_code, cqp_request->minor_code);
207 if ((!ret) || (cqp_request->major_code)) {
208 if (atomic_dec_and_test(&cqp_request->refcount)) {
209 if (cqp_request->dynamic) {
212 spin_lock_irqsave(&nesdev->cqp.lock, flags);
213 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
214 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
223 if (atomic_dec_and_test(&cqp_request->refcount)) {
224 if (cqp_request->dynamic) {
227 spin_lock_irqsave(&nesdev->cqp.lock, flags);
228 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
229 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
234 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
235 (ibmw->rkey & 0x0fffff00) >> 8);
245 static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
246 struct ib_mw_bind *ibmw_bind)
249 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
250 struct nes_device *nesdev = nesvnic->nesdev;
251 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
252 struct nes_qp *nesqp = to_nesqp(ibqp);
253 struct nes_hw_qp_wqe *wqe;
254 unsigned long flags = 0;
259 if (nesqp->ibqp_state > IB_QPS_RTS)
262 spin_lock_irqsave(&nesqp->lock, flags);
264 head = nesqp->hwqp.sq_head;
265 qsize = nesqp->hwqp.sq_tail;
267 /* Check for SQ overflow */
268 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
269 spin_unlock_irqrestore(&nesqp->lock, flags);
273 wqe = &nesqp->hwqp.sq_vbase[head];
274 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
275 nes_fill_init_qp_wqe(wqe, nesqp, head);
276 u64temp = ibmw_bind->wr_id;
277 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
278 wqe_misc = NES_IWARP_SQ_OP_BIND;
280 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
282 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
283 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
285 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_WRITE) {
286 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
288 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_READ) {
289 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
292 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
293 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX, ibmw_bind->mr->lkey);
294 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
295 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
297 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
298 u64temp = (u64)ibmw_bind->addr;
299 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
305 nesqp->hwqp.sq_head = head;
308 nes_write32(nesdev->regs+NES_WQE_ALLOC,
309 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
311 spin_unlock_irqrestore(&nesqp->lock, flags);
320 static struct ib_fmr *nes_alloc_fmr(struct ib_pd *ibpd,
321 int ibmr_access_flags,
322 struct ib_fmr_attr *ibfmr_attr)
325 struct nes_pd *nespd = to_nespd(ibpd);
326 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
327 struct nes_device *nesdev = nesvnic->nesdev;
328 struct nes_adapter *nesadapter = nesdev->nesadapter;
329 struct nes_fmr *nesfmr;
330 struct nes_cqp_request *cqp_request;
331 struct nes_hw_cqp_wqe *cqp_wqe;
335 u32 next_stag_index = 0;
340 struct nes_vpbl vpbl;
342 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
343 stag_key = (u8)next_stag_index;
347 next_stag_index >>= 8;
348 next_stag_index %= nesadapter->max_mr;
350 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
351 nesadapter->max_mr, &stag_index, &next_stag_index);
353 goto failed_resource_alloc;
356 nesfmr = kzalloc(sizeof(*nesfmr), GFP_KERNEL);
359 goto failed_fmr_alloc;
362 nesfmr->nesmr.mode = IWNES_MEMREG_TYPE_FMR;
363 if (ibfmr_attr->max_pages == 1) {
364 /* use zero length PBL */
365 nesfmr->nesmr.pbl_4k = 0;
366 nesfmr->nesmr.pbls_used = 0;
367 } else if (ibfmr_attr->max_pages <= 32) {
369 nesfmr->nesmr.pbl_4k = 0;
370 nesfmr->nesmr.pbls_used = 1;
371 } else if (ibfmr_attr->max_pages <= 512) {
373 nesfmr->nesmr.pbl_4k = 1;
374 nesfmr->nesmr.pbls_used = 1;
376 /* use two level 4K PBLs */
377 /* add support for two level 256B PBLs */
378 nesfmr->nesmr.pbl_4k = 1;
379 nesfmr->nesmr.pbls_used = 1 + (ibfmr_attr->max_pages >> 9) +
380 ((ibfmr_attr->max_pages & 511) ? 1 : 0);
382 /* Register the region with the adapter */
383 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
385 /* track PBL resources */
386 if (nesfmr->nesmr.pbls_used != 0) {
387 if (nesfmr->nesmr.pbl_4k) {
388 if (nesfmr->nesmr.pbls_used > nesadapter->free_4kpbl) {
389 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
391 goto failed_vpbl_alloc;
393 nesadapter->free_4kpbl -= nesfmr->nesmr.pbls_used;
396 if (nesfmr->nesmr.pbls_used > nesadapter->free_256pbl) {
397 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
399 goto failed_vpbl_alloc;
401 nesadapter->free_256pbl -= nesfmr->nesmr.pbls_used;
407 if (nesfmr->nesmr.pbls_used == 0) {
408 nesfmr->root_vpbl.pbl_vbase = NULL;
409 nes_debug(NES_DBG_MR, "zero level pbl \n");
410 } else if (nesfmr->nesmr.pbls_used == 1) {
411 /* can change it to kmalloc & dma_map_single */
412 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
413 &nesfmr->root_vpbl.pbl_pbase);
414 if (!nesfmr->root_vpbl.pbl_vbase) {
415 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
417 goto failed_vpbl_alloc;
419 nesfmr->leaf_pbl_cnt = 0;
420 nes_debug(NES_DBG_MR, "one level pbl, root_vpbl.pbl_vbase=%p \n",
421 nesfmr->root_vpbl.pbl_vbase);
425 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
426 &nesfmr->root_vpbl.pbl_pbase);
427 if (!nesfmr->root_vpbl.pbl_vbase) {
428 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
430 goto failed_vpbl_alloc;
433 nesfmr->root_vpbl.leaf_vpbl = kzalloc(sizeof(*nesfmr->root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
434 if (!nesfmr->root_vpbl.leaf_vpbl) {
435 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
437 goto failed_leaf_vpbl_alloc;
440 nesfmr->leaf_pbl_cnt = nesfmr->nesmr.pbls_used-1;
441 nes_debug(NES_DBG_MR, "two level pbl, root_vpbl.pbl_vbase=%p"
442 " leaf_pbl_cnt=%d root_vpbl.leaf_vpbl=%p\n",
443 nesfmr->root_vpbl.pbl_vbase, nesfmr->leaf_pbl_cnt, nesfmr->root_vpbl.leaf_vpbl);
445 for (i=0; i<nesfmr->leaf_pbl_cnt; i++)
446 nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase = NULL;
448 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
449 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
452 if (!vpbl.pbl_vbase) {
454 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
455 goto failed_leaf_vpbl_pages_alloc;
458 nesfmr->root_vpbl.pbl_vbase[i].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
459 nesfmr->root_vpbl.pbl_vbase[i].pa_high = cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
460 nesfmr->root_vpbl.leaf_vpbl[i] = vpbl;
462 nes_debug(NES_DBG_MR, "pbase_low=0x%x, pbase_high=0x%x, vpbl=%p\n",
463 nesfmr->root_vpbl.pbl_vbase[i].pa_low,
464 nesfmr->root_vpbl.pbl_vbase[i].pa_high,
465 &nesfmr->root_vpbl.leaf_vpbl[i]);
468 nesfmr->ib_qp = NULL;
469 nesfmr->access_rights =0;
471 stag = stag_index << 8;
473 stag += (u32)stag_key;
475 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
476 cqp_request = nes_get_cqp_request(nesdev);
477 if (cqp_request == NULL) {
478 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
480 goto failed_leaf_vpbl_pages_alloc;
482 cqp_request->waiting = 1;
483 cqp_wqe = &cqp_request->cqp_wqe;
485 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
488 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
490 if (nesfmr->nesmr.pbl_4k == 1)
491 opcode |= NES_CQP_STAG_PBL_BLK_SIZE;
493 if (ibmr_access_flags & IB_ACCESS_REMOTE_WRITE) {
494 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE |
495 NES_CQP_STAG_RIGHTS_LOCAL_WRITE | NES_CQP_STAG_REM_ACC_EN;
496 nesfmr->access_rights |=
497 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_RIGHTS_LOCAL_WRITE |
498 NES_CQP_STAG_REM_ACC_EN;
501 if (ibmr_access_flags & IB_ACCESS_REMOTE_READ) {
502 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ |
503 NES_CQP_STAG_RIGHTS_LOCAL_READ | NES_CQP_STAG_REM_ACC_EN;
504 nesfmr->access_rights |=
505 NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_RIGHTS_LOCAL_READ |
506 NES_CQP_STAG_REM_ACC_EN;
509 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
510 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
511 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
512 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
514 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX] =
515 cpu_to_le32((nesfmr->nesmr.pbls_used>1) ?
516 (nesfmr->nesmr.pbls_used-1) : nesfmr->nesmr.pbls_used);
518 atomic_set(&cqp_request->refcount, 2);
519 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
522 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
524 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
525 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
526 stag, ret, cqp_request->major_code, cqp_request->minor_code);
528 if ((!ret) || (cqp_request->major_code)) {
529 if (atomic_dec_and_test(&cqp_request->refcount)) {
530 if (cqp_request->dynamic) {
533 spin_lock_irqsave(&nesdev->cqp.lock, flags);
534 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
535 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
538 ret = (!ret) ? -ETIME : -EIO;
539 goto failed_leaf_vpbl_pages_alloc;
541 if (atomic_dec_and_test(&cqp_request->refcount)) {
542 if (cqp_request->dynamic) {
545 spin_lock_irqsave(&nesdev->cqp.lock, flags);
546 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
547 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
552 nesfmr->nesmr.ibfmr.lkey = stag;
553 nesfmr->nesmr.ibfmr.rkey = stag;
554 nesfmr->attr = *ibfmr_attr;
556 return &nesfmr->nesmr.ibfmr;
558 failed_leaf_vpbl_pages_alloc:
559 /* unroll all allocated pages */
560 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
561 if (nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase) {
562 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
563 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
566 if (nesfmr->root_vpbl.leaf_vpbl)
567 kfree(nesfmr->root_vpbl.leaf_vpbl);
569 failed_leaf_vpbl_alloc:
570 if (nesfmr->leaf_pbl_cnt == 0) {
571 if (nesfmr->root_vpbl.pbl_vbase)
572 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
573 nesfmr->root_vpbl.pbl_pbase);
575 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
576 nesfmr->root_vpbl.pbl_pbase);
582 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
584 failed_resource_alloc:
592 static int nes_dealloc_fmr(struct ib_fmr *ibfmr)
594 struct nes_mr *nesmr = to_nesmr_from_ibfmr(ibfmr);
595 struct nes_fmr *nesfmr = to_nesfmr(nesmr);
596 struct nes_vnic *nesvnic = to_nesvnic(ibfmr->device);
597 struct nes_device *nesdev = nesvnic->nesdev;
598 struct nes_mr temp_nesmr = *nesmr;
601 temp_nesmr.ibmw.device = ibfmr->device;
602 temp_nesmr.ibmw.pd = ibfmr->pd;
603 temp_nesmr.ibmw.rkey = ibfmr->rkey;
604 temp_nesmr.ibmw.uobject = NULL;
606 /* free the resources */
607 if (nesfmr->leaf_pbl_cnt == 0) {
608 /* single PBL case */
609 if (nesfmr->root_vpbl.pbl_vbase)
610 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
611 nesfmr->root_vpbl.pbl_pbase);
613 for (i = 0; i < nesfmr->leaf_pbl_cnt; i++) {
614 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
615 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
617 kfree(nesfmr->root_vpbl.leaf_vpbl);
618 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
619 nesfmr->root_vpbl.pbl_pbase);
622 return nes_dealloc_mw(&temp_nesmr.ibmw);
629 static int nes_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
630 int list_len, u64 iova)
639 static int nes_unmap_fmr(struct list_head *ibfmr_list)
649 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
651 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
652 struct nes_device *nesdev = nesvnic->nesdev;
653 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
655 memset(props, 0, sizeof(*props));
656 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
658 props->fw_ver = nesdev->nesadapter->fw_ver;
659 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
660 props->vendor_id = nesdev->nesadapter->vendor_id;
661 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
662 props->hw_ver = nesdev->nesadapter->hw_rev;
663 props->max_mr_size = 0x80000000;
664 props->max_qp = nesibdev->max_qp;
665 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
666 props->max_sge = nesdev->nesadapter->max_sge;
667 props->max_cq = nesibdev->max_cq;
668 props->max_cqe = nesdev->nesadapter->max_cqe - 1;
669 props->max_mr = nesibdev->max_mr;
670 props->max_mw = nesibdev->max_mr;
671 props->max_pd = nesibdev->max_pd;
672 props->max_sge_rd = 1;
673 switch (nesdev->nesadapter->max_irrq_wr) {
675 props->max_qp_rd_atom = 1;
678 props->max_qp_rd_atom = 4;
681 props->max_qp_rd_atom = 16;
684 props->max_qp_rd_atom = 32;
687 props->max_qp_rd_atom = 0;
689 props->max_qp_init_rd_atom = props->max_qp_wr;
690 props->atomic_cap = IB_ATOMIC_NONE;
691 props->max_map_per_fmr = 1;
700 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
702 memset(props, 0, sizeof(*props));
704 props->max_mtu = IB_MTU_2048;
705 props->active_mtu = IB_MTU_2048;
710 props->state = IB_PORT_ACTIVE;
711 props->phys_state = 0;
712 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
713 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
714 props->gid_tbl_len = 1;
715 props->pkey_tbl_len = 1;
716 props->qkey_viol_cntr = 0;
717 props->active_width = IB_WIDTH_4X;
718 props->active_speed = 1;
719 props->max_msg_sz = 0x80000000;
728 static int nes_modify_port(struct ib_device *ibdev, u8 port,
729 int port_modify_mask, struct ib_port_modify *props)
738 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
748 static int nes_query_gid(struct ib_device *ibdev, u8 port,
749 int index, union ib_gid *gid)
751 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
753 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
754 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
761 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
762 * of all objects associated with a particular user-mode client.
764 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
765 struct ib_udata *udata)
767 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
768 struct nes_device *nesdev = nesvnic->nesdev;
769 struct nes_adapter *nesadapter = nesdev->nesadapter;
770 struct nes_alloc_ucontext_req req;
771 struct nes_alloc_ucontext_resp uresp;
772 struct nes_ucontext *nes_ucontext;
773 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
776 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
777 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
778 return ERR_PTR(-EINVAL);
781 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
782 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
783 req.userspace_ver, NES_ABI_USERSPACE_VER);
784 return ERR_PTR(-EINVAL);
788 memset(&uresp, 0, sizeof uresp);
790 uresp.max_qps = nesibdev->max_qp;
791 uresp.max_pds = nesibdev->max_pd;
792 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
793 uresp.virtwq = nesadapter->virtwq;
794 uresp.kernel_ver = NES_ABI_KERNEL_VER;
796 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
798 return ERR_PTR(-ENOMEM);
800 nes_ucontext->nesdev = nesdev;
801 nes_ucontext->mmap_wq_offset = uresp.max_pds;
802 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
803 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
807 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
809 return ERR_PTR(-EFAULT);
812 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
813 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
814 atomic_set(&nes_ucontext->usecnt, 1);
815 return &nes_ucontext->ibucontext;
820 * nes_dealloc_ucontext
822 static int nes_dealloc_ucontext(struct ib_ucontext *context)
824 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
825 /* struct nes_device *nesdev = nesvnic->nesdev; */
826 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
828 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
838 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
841 struct nes_vnic *nesvnic = to_nesvnic(context->device);
842 struct nes_device *nesdev = nesvnic->nesdev;
843 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
844 struct nes_ucontext *nes_ucontext;
845 struct nes_qp *nesqp;
847 nes_ucontext = to_nesucontext(context);
850 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
851 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
852 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
853 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
854 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
855 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
858 nesqp = nes_ucontext->mmap_nesqp[index];
860 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
863 if (remap_pfn_range(vma, vma->vm_start,
864 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
865 vma->vm_end - vma->vm_start,
866 vma->vm_page_prot)) {
867 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
870 vma->vm_private_data = nesqp;
873 index = vma->vm_pgoff;
874 if (!test_bit(index, nes_ucontext->allocated_doorbells))
877 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
878 if (io_remap_pfn_range(vma, vma->vm_start,
879 (nesdev->doorbell_start +
880 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
881 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
883 vma->vm_private_data = nes_ucontext;
894 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
895 struct ib_ucontext *context, struct ib_udata *udata)
897 struct nes_pd *nespd;
898 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
899 struct nes_device *nesdev = nesvnic->nesdev;
900 struct nes_adapter *nesadapter = nesdev->nesadapter;
901 struct nes_ucontext *nesucontext;
902 struct nes_alloc_pd_resp uresp;
906 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
907 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
908 atomic_read(&nesvnic->netdev->refcnt));
910 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
911 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
916 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
918 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
919 return ERR_PTR(-ENOMEM);
922 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
923 nespd, nesvnic->nesibdev->ibdev.name);
925 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
928 nesucontext = to_nesucontext(context);
929 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
930 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
931 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
932 nespd->mmap_db_index, nespd->pd_id);
933 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
934 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
935 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
937 return ERR_PTR(-ENOMEM);
940 uresp.pd_id = nespd->pd_id;
941 uresp.mmap_db_index = nespd->mmap_db_index;
942 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
943 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
945 return ERR_PTR(-EFAULT);
948 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
949 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
950 nesucontext->first_free_db = nespd->mmap_db_index + 1;
953 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
961 static int nes_dealloc_pd(struct ib_pd *ibpd)
963 struct nes_ucontext *nesucontext;
964 struct nes_pd *nespd = to_nespd(ibpd);
965 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
966 struct nes_device *nesdev = nesvnic->nesdev;
967 struct nes_adapter *nesadapter = nesdev->nesadapter;
969 if ((ibpd->uobject) && (ibpd->uobject->context)) {
970 nesucontext = to_nesucontext(ibpd->uobject->context);
971 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
972 nespd->mmap_db_index);
973 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
974 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
975 if (nesucontext->first_free_db > nespd->mmap_db_index) {
976 nesucontext->first_free_db = nespd->mmap_db_index;
980 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
981 nespd->pd_id, nespd);
982 nes_free_resource(nesadapter, nesadapter->allocated_pds,
983 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
993 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
995 return ERR_PTR(-ENOSYS);
1002 static int nes_destroy_ah(struct ib_ah *ah)
1009 * nes_get_encoded_size
1011 static inline u8 nes_get_encoded_size(int *size)
1013 u8 encoded_size = 0;
1017 } else if (*size <= 128) {
1020 } else if (*size <= 512) {
1024 return (encoded_size);
1032 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
1033 struct nes_vnic *nesvnic, int sq_size, int rq_size)
1035 unsigned long flags;
1040 struct nes_device *nesdev = nesvnic->nesdev;
1041 struct nes_adapter *nesadapter = nesdev->nesadapter;
1046 pbl_entries = nespbl->pbl_size >> 3;
1047 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
1048 nespbl->pbl_size, pbl_entries,
1049 (void *)nespbl->pbl_vbase,
1050 (unsigned long) nespbl->pbl_pbase);
1051 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
1052 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
1053 /* the first pbl to be fro the rq_vbase... */
1054 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1055 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1056 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1057 if (!nespbl->page) {
1058 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
1063 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
1064 nesqp->page = nespbl->page;
1065 if (!nesqp->hwqp.sq_vbase) {
1066 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
1071 /* Now to get to sq.. we need to calculate how many */
1072 /* PBL entries were used by the rq.. */
1073 pbl += sq_pbl_entries;
1074 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1075 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
1076 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
1078 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
1079 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
1080 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
1081 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1082 if (!nesadapter->free_256pbl) {
1083 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1085 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1086 kunmap(nesqp->page);
1090 nesadapter->free_256pbl--;
1091 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1093 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
1094 pblbuffer = nesqp->pbl_vbase;
1095 if (!nesqp->pbl_vbase) {
1096 /* memory allocated during nes_reg_user_mr() */
1097 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1100 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1101 nesadapter->free_256pbl++;
1102 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1103 kunmap(nesqp->page);
1106 memset(nesqp->pbl_vbase, 0, 256);
1107 /* fill in the page address in the pbl buffer.. */
1108 tpbl = pblbuffer + 16;
1109 pbl = (__le64 *)nespbl->pbl_vbase;
1110 while (sq_pbl_entries--)
1113 while (rq_pbl_entries--)
1116 /* done with memory allocated during nes_reg_user_mr() */
1117 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1121 nesqp->qp_mem_size =
1122 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1123 /* Round up to a multiple of a page */
1124 nesqp->qp_mem_size += PAGE_SIZE - 1;
1125 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1127 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1128 &nesqp->hwqp.q2_pbase);
1131 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1132 nesqp->pbl_vbase = NULL;
1133 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1134 nesadapter->free_256pbl++;
1135 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1136 kunmap(nesqp->page);
1139 nesqp->hwqp.q2_vbase = mem;
1141 memset(nesqp->hwqp.q2_vbase, 0, 256);
1142 nesqp->nesqp_context = mem;
1143 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1144 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1153 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1154 int sq_size, int rq_size)
1157 struct nes_device *nesdev = nesvnic->nesdev;
1159 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1160 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1161 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1162 256; /* this is Q2 */
1163 /* Round up to a multiple of a page */
1164 nesqp->qp_mem_size += PAGE_SIZE - 1;
1165 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1167 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1168 &nesqp->hwqp.sq_pbase);
1171 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1172 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1173 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1175 memset(mem, 0, nesqp->qp_mem_size);
1177 nesqp->hwqp.sq_vbase = mem;
1178 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1180 nesqp->hwqp.rq_vbase = mem;
1181 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1182 sizeof(struct nes_hw_qp_wqe) * sq_size;
1183 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1185 nesqp->hwqp.q2_vbase = mem;
1186 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1187 sizeof(struct nes_hw_qp_wqe) * rq_size;
1189 memset(nesqp->hwqp.q2_vbase, 0, 256);
1191 nesqp->nesqp_context = mem;
1192 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1193 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1199 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1201 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1202 struct nes_qp *nesqp, int virt_wqs)
1204 unsigned long flags;
1205 struct nes_adapter *nesadapter = nesdev->nesadapter;
1207 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1208 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1210 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1211 nesadapter->free_256pbl++;
1212 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1213 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1214 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1215 nesqp->pbl_vbase = NULL;
1216 kunmap(nesqp->page);
1224 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1225 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1229 struct nes_pd *nespd = to_nespd(ibpd);
1230 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1231 struct nes_device *nesdev = nesvnic->nesdev;
1232 struct nes_adapter *nesadapter = nesdev->nesadapter;
1233 struct nes_qp *nesqp;
1234 struct nes_cq *nescq;
1235 struct nes_ucontext *nes_ucontext;
1236 struct nes_hw_cqp_wqe *cqp_wqe;
1237 struct nes_cqp_request *cqp_request;
1238 struct nes_create_qp_req req;
1239 struct nes_create_qp_resp uresp;
1240 struct nes_pbl *nespbl = NULL;
1243 /* u32 counter = 0; */
1245 unsigned long flags;
1255 if (init_attr->create_flags)
1256 return ERR_PTR(-EINVAL);
1258 atomic_inc(&qps_created);
1259 switch (init_attr->qp_type) {
1261 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1262 init_attr->cap.max_inline_data = 0;
1264 init_attr->cap.max_inline_data = 64;
1266 sq_size = init_attr->cap.max_send_wr;
1267 rq_size = init_attr->cap.max_recv_wr;
1269 // check if the encoded sizes are OK or not...
1270 sq_encoded_size = nes_get_encoded_size(&sq_size);
1271 rq_encoded_size = nes_get_encoded_size(&rq_size);
1273 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1274 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1276 return ERR_PTR(-EINVAL);
1279 init_attr->cap.max_send_wr = sq_size -2;
1280 init_attr->cap.max_recv_wr = rq_size -1;
1281 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1283 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1284 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1286 return ERR_PTR(ret);
1289 /* Need 512 (actually now 1024) byte alignment on this structure */
1290 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1292 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1293 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1294 return ERR_PTR(-ENOMEM);
1296 u64nesqp = (unsigned long)mem;
1297 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1298 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1299 u64nesqp &= ~u64temp;
1300 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1301 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1302 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1303 nesqp->allocated_buffer = mem;
1306 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1307 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1308 kfree(nesqp->allocated_buffer);
1309 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1312 if (req.user_wqe_buffers) {
1315 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1316 nesqp->user_mode = 1;
1317 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1320 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1321 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1322 list_del(&nespbl->list);
1324 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1325 nespbl, nespbl->user_base);
1330 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1331 (long long unsigned int)req.user_wqe_buffers);
1332 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1333 kfree(nesqp->allocated_buffer);
1334 return ERR_PTR(-EFAULT);
1338 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1339 nesqp->mmap_sq_db_index =
1340 find_next_zero_bit(nes_ucontext->allocated_wqs,
1341 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1342 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1343 nespd->mmap_db_index); */
1344 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1345 nes_debug(NES_DBG_QP,
1346 "db index > max user regions, failing create QP\n");
1347 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1349 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1353 kfree(nesqp->allocated_buffer);
1354 return ERR_PTR(-ENOMEM);
1356 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1357 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1358 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1360 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1361 kfree(nesqp->allocated_buffer);
1362 return ERR_PTR(-EFAULT);
1365 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1366 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1368 nes_debug(NES_DBG_QP,
1369 "error geting qp mem code = %d\n", err);
1370 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1371 kfree(nesqp->allocated_buffer);
1372 return ERR_PTR(-ENOMEM);
1375 nesqp->hwqp.sq_size = sq_size;
1376 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1377 nesqp->hwqp.sq_head = 1;
1378 nesqp->hwqp.rq_size = rq_size;
1379 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1380 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1381 (void *)nesqp->nesqp_context_pbase);
1383 nesqp->hwqp.qp_id = qp_num;
1384 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1385 nesqp->nespd = nespd;
1387 nescq = to_nescq(init_attr->send_cq);
1388 nesqp->nesscq = nescq;
1389 nescq = to_nescq(init_attr->recv_cq);
1390 nesqp->nesrcq = nescq;
1392 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1393 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1394 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1395 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1396 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1397 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1398 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1399 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1400 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1401 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1402 u64temp = (u64)nesqp->hwqp.sq_pbase;
1403 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1404 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1408 u64temp = (u64)nesqp->hwqp.sq_pbase;
1409 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1410 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1411 u64temp = (u64)nesqp->hwqp.rq_pbase;
1412 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1413 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1415 u64temp = (u64)nesqp->pbl_pbase;
1416 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1417 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1420 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1421 nesvnic->next_qp_nic_index,
1422 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1423 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1424 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1425 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1426 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1427 nesvnic->next_qp_nic_index++;
1428 if ((nesvnic->next_qp_nic_index > 3) ||
1429 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1430 nesvnic->next_qp_nic_index = 0;
1432 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1434 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1435 u64temp = (u64)nesqp->hwqp.q2_pbase;
1436 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1437 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1438 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1439 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1440 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1441 ((((u32)nesadapter->max_irrq_wr) <<
1442 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1443 if (disable_mpa_crc) {
1444 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1445 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1450 cqp_request = nes_get_cqp_request(nesdev);
1451 if (cqp_request == NULL) {
1452 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
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(-ENOMEM);
1458 cqp_request->waiting = 1;
1459 cqp_wqe = &cqp_request->cqp_wqe;
1462 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1463 NES_CQP_QP_IWARP_STATE_IDLE;
1465 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1466 NES_CQP_QP_IWARP_STATE_IDLE;
1468 opcode |= NES_CQP_QP_CQS_VALID;
1469 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1470 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1471 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1473 u64temp = (u64)nesqp->nesqp_context_pbase;
1474 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1476 atomic_set(&cqp_request->refcount, 2);
1477 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1480 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1482 ret = wait_event_timeout(cqp_request->waitq,
1483 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1484 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1485 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1486 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1487 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1488 cqp_request->major_code, cqp_request->minor_code);
1489 if ((!ret) || (cqp_request->major_code)) {
1490 if (atomic_dec_and_test(&cqp_request->refcount)) {
1491 if (cqp_request->dynamic) {
1494 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1495 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1496 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1499 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1500 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1501 kfree(nesqp->allocated_buffer);
1503 return ERR_PTR(-ETIME);
1505 return ERR_PTR(-EIO);
1508 if (atomic_dec_and_test(&cqp_request->refcount)) {
1509 if (cqp_request->dynamic) {
1512 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1513 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1514 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1519 if (ibpd->uobject) {
1520 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1521 uresp.actual_sq_size = sq_size;
1522 uresp.actual_rq_size = rq_size;
1523 uresp.qp_id = nesqp->hwqp.qp_id;
1524 uresp.nes_drv_opt = nes_drv_opt;
1525 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1526 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1527 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1528 kfree(nesqp->allocated_buffer);
1529 return ERR_PTR(-EFAULT);
1533 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1534 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1535 spin_lock_init(&nesqp->lock);
1536 init_waitqueue_head(&nesqp->state_waitq);
1537 init_waitqueue_head(&nesqp->kick_waitq);
1538 nes_add_ref(&nesqp->ibqp);
1541 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1542 return ERR_PTR(-EINVAL);
1546 /* update the QP table */
1547 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1548 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1549 atomic_read(&nesvnic->netdev->refcnt));
1551 return &nesqp->ibqp;
1558 static int nes_destroy_qp(struct ib_qp *ibqp)
1560 struct nes_qp *nesqp = to_nesqp(ibqp);
1561 /* struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); */
1562 struct nes_ucontext *nes_ucontext;
1563 struct ib_qp_attr attr;
1564 struct iw_cm_id *cm_id;
1565 struct iw_cm_event cm_event;
1568 atomic_inc(&sw_qps_destroyed);
1569 nesqp->destroyed = 1;
1571 /* Blow away the connection if it exists. */
1572 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1573 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1574 attr.qp_state = IB_QPS_ERR;
1575 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1578 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1579 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1580 cm_id = nesqp->cm_id;
1581 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1582 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1583 cm_event.local_addr = cm_id->local_addr;
1584 cm_event.remote_addr = cm_id->remote_addr;
1585 cm_event.private_data = NULL;
1586 cm_event.private_data_len = 0;
1588 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1589 "QP%u. cm_id = %p, refcount = %u. \n",
1590 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1592 cm_id->rem_ref(cm_id);
1593 ret = cm_id->event_handler(cm_id, &cm_event);
1595 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1599 if (nesqp->user_mode) {
1600 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1601 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1602 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1603 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1604 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1605 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1608 if (nesqp->pbl_pbase)
1609 kunmap(nesqp->page);
1612 nes_rem_ref(&nesqp->ibqp);
1620 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1622 struct ib_ucontext *context, struct ib_udata *udata)
1625 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1626 struct nes_device *nesdev = nesvnic->nesdev;
1627 struct nes_adapter *nesadapter = nesdev->nesadapter;
1628 struct nes_cq *nescq;
1629 struct nes_ucontext *nes_ucontext = NULL;
1630 struct nes_cqp_request *cqp_request;
1632 struct nes_hw_cqp_wqe *cqp_wqe;
1633 struct nes_pbl *nespbl = NULL;
1634 struct nes_create_cq_req req;
1635 struct nes_create_cq_resp resp;
1638 u32 pbl_entries = 1;
1640 unsigned long flags;
1643 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1644 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1646 return ERR_PTR(err);
1649 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1651 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1652 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1653 return ERR_PTR(-ENOMEM);
1656 nescq->hw_cq.cq_size = max(entries + 1, 5);
1657 nescq->hw_cq.cq_number = cq_num;
1658 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1662 nes_ucontext = to_nesucontext(context);
1663 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1664 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1666 return ERR_PTR(-EFAULT);
1668 nesvnic->mcrq_ucontext = nes_ucontext;
1669 nes_ucontext->mcrqf = req.mcrqf;
1670 if (nes_ucontext->mcrqf) {
1671 if (nes_ucontext->mcrqf & 0x80000000)
1672 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 12 + (nes_ucontext->mcrqf & 0xf) - 1;
1673 else if (nes_ucontext->mcrqf & 0x40000000)
1674 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1676 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1677 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1679 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1680 (unsigned long)req.user_cq_buffer, entries);
1682 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1683 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1684 list_del(&nespbl->list);
1686 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1692 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1694 return ERR_PTR(-EFAULT);
1697 pbl_entries = nespbl->pbl_size >> 3;
1698 nescq->cq_mem_size = 0;
1700 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1701 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1702 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1704 /* allocate the physical buffer space */
1705 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1706 &nescq->hw_cq.cq_pbase);
1708 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1709 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1711 return ERR_PTR(-ENOMEM);
1714 memset(mem, 0, nescq->cq_mem_size);
1715 nescq->hw_cq.cq_vbase = mem;
1716 nescq->hw_cq.cq_head = 0;
1717 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1718 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1719 (u32)nescq->hw_cq.cq_pbase);
1722 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1723 spin_lock_init(&nescq->lock);
1725 /* send CreateCQ request to CQP */
1726 cqp_request = nes_get_cqp_request(nesdev);
1727 if (cqp_request == NULL) {
1728 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1730 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1731 nescq->hw_cq.cq_pbase);
1732 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1734 return ERR_PTR(-ENOMEM);
1736 cqp_request->waiting = 1;
1737 cqp_wqe = &cqp_request->cqp_wqe;
1739 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1740 NES_CQP_CQ_CHK_OVERFLOW |
1741 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1743 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1745 if (pbl_entries != 1) {
1746 if (pbl_entries > 32) {
1748 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1749 if (nesadapter->free_4kpbl == 0) {
1750 if (cqp_request->dynamic) {
1751 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1754 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1755 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1758 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1759 nescq->hw_cq.cq_pbase);
1760 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1762 return ERR_PTR(-ENOMEM);
1764 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1765 nescq->virtual_cq = 2;
1766 nesadapter->free_4kpbl--;
1769 /* use 256 byte pbl */
1770 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1771 if (nesadapter->free_256pbl == 0) {
1772 if (cqp_request->dynamic) {
1773 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1776 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1777 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1780 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1781 nescq->hw_cq.cq_pbase);
1782 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1784 return ERR_PTR(-ENOMEM);
1786 opcode |= NES_CQP_CQ_VIRT;
1787 nescq->virtual_cq = 1;
1788 nesadapter->free_256pbl--;
1793 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1795 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1796 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1797 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1798 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1801 if (pbl_entries != 1)
1802 u64temp = (u64)nespbl->pbl_pbase;
1804 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1805 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1806 nes_ucontext->mmap_db_index[0]);
1808 u64temp = (u64)nescq->hw_cq.cq_pbase;
1809 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1811 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1812 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1813 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1814 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1815 cpu_to_le32((u32)(u64temp >> 1));
1816 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1817 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1819 atomic_set(&cqp_request->refcount, 2);
1820 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1823 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1824 nescq->hw_cq.cq_number);
1825 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1826 NES_EVENT_TIMEOUT * 2);
1827 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1828 nescq->hw_cq.cq_number, ret);
1829 if ((!ret) || (cqp_request->major_code)) {
1830 if (atomic_dec_and_test(&cqp_request->refcount)) {
1831 if (cqp_request->dynamic) {
1834 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1835 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1836 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1840 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1841 nescq->hw_cq.cq_pbase);
1842 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1844 return ERR_PTR(-EIO);
1846 if (atomic_dec_and_test(&cqp_request->refcount)) {
1847 if (cqp_request->dynamic) {
1850 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1851 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1852 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1858 /* free the nespbl */
1859 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1862 resp.cq_id = nescq->hw_cq.cq_number;
1863 resp.cq_size = nescq->hw_cq.cq_size;
1864 resp.mmap_db_index = 0;
1865 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1866 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1868 return ERR_PTR(-EFAULT);
1872 return &nescq->ibcq;
1879 static int nes_destroy_cq(struct ib_cq *ib_cq)
1881 struct nes_cq *nescq;
1882 struct nes_device *nesdev;
1883 struct nes_vnic *nesvnic;
1884 struct nes_adapter *nesadapter;
1885 struct nes_hw_cqp_wqe *cqp_wqe;
1886 struct nes_cqp_request *cqp_request;
1887 unsigned long flags;
1894 nescq = to_nescq(ib_cq);
1895 nesvnic = to_nesvnic(ib_cq->device);
1896 nesdev = nesvnic->nesdev;
1897 nesadapter = nesdev->nesadapter;
1899 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1901 /* Send DestroyCQ request to CQP */
1902 cqp_request = nes_get_cqp_request(nesdev);
1903 if (cqp_request == NULL) {
1904 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1907 cqp_request->waiting = 1;
1908 cqp_wqe = &cqp_request->cqp_wqe;
1909 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1910 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1911 if (nescq->virtual_cq == 1) {
1912 nesadapter->free_256pbl++;
1913 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1914 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1915 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1917 } else if (nescq->virtual_cq == 2) {
1918 nesadapter->free_4kpbl++;
1919 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1920 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1921 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1923 opcode |= NES_CQP_CQ_4KB_CHUNK;
1926 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1928 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1929 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1930 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1931 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1932 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1933 atomic_set(&cqp_request->refcount, 2);
1934 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1937 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1938 nescq->hw_cq.cq_number);
1939 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1941 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1942 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1943 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1944 cqp_request->minor_code);
1945 if ((!ret) || (cqp_request->major_code)) {
1946 if (atomic_dec_and_test(&cqp_request->refcount)) {
1947 if (cqp_request->dynamic) {
1950 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1951 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1952 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1956 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1957 nescq->hw_cq.cq_number);
1960 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1961 nescq->hw_cq.cq_number);
1966 if (atomic_dec_and_test(&cqp_request->refcount)) {
1967 if (cqp_request->dynamic) {
1970 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1971 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1972 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1977 if (nescq->cq_mem_size)
1978 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1979 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1989 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1990 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1991 dma_addr_t single_buffer, u16 pbl_count, u16 residual_page_count,
1992 int acc, u64 *iova_start)
1994 struct nes_hw_cqp_wqe *cqp_wqe;
1995 struct nes_cqp_request *cqp_request;
1996 unsigned long flags;
1998 struct nes_adapter *nesadapter = nesdev->nesadapter;
2003 /* Register the region with the adapter */
2004 cqp_request = nes_get_cqp_request(nesdev);
2005 if (cqp_request == NULL) {
2006 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2009 cqp_request->waiting = 1;
2010 cqp_wqe = &cqp_request->cqp_wqe;
2012 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2013 /* track PBL resources */
2014 if (pbl_count != 0) {
2015 if (pbl_count > 1) {
2017 if ((pbl_count+1) > nesadapter->free_4kpbl) {
2018 nes_debug(NES_DBG_MR, "Out of 4KB Pbls for two level request.\n");
2019 if (cqp_request->dynamic) {
2020 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2023 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2024 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2028 nesadapter->free_4kpbl -= pbl_count+1;
2030 } else if (residual_page_count > 32) {
2031 if (pbl_count > nesadapter->free_4kpbl) {
2032 nes_debug(NES_DBG_MR, "Out of 4KB Pbls.\n");
2033 if (cqp_request->dynamic) {
2034 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2037 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2038 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2042 nesadapter->free_4kpbl -= pbl_count;
2045 if (pbl_count > nesadapter->free_256pbl) {
2046 nes_debug(NES_DBG_MR, "Out of 256B Pbls.\n");
2047 if (cqp_request->dynamic) {
2048 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2051 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2052 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2056 nesadapter->free_256pbl -= pbl_count;
2061 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2063 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2064 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2065 if (acc & IB_ACCESS_LOCAL_WRITE)
2066 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2067 if (acc & IB_ACCESS_REMOTE_WRITE)
2068 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2069 if (acc & IB_ACCESS_REMOTE_READ)
2070 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2071 if (acc & IB_ACCESS_MW_BIND)
2072 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2074 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2075 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2076 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2077 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2079 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2080 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2081 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2082 cpu_to_le32(nespd->pd_id & 0x00007fff);
2083 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2085 if (pbl_count == 0) {
2086 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2088 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2089 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2090 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX,
2091 (((pbl_count - 1) * 4096) + (residual_page_count*8)));
2093 if ((pbl_count > 1) || (residual_page_count > 32))
2094 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2098 atomic_set(&cqp_request->refcount, 2);
2099 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2102 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2104 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2105 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2106 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2107 major_code = cqp_request->major_code;
2108 if (atomic_dec_and_test(&cqp_request->refcount)) {
2109 if (cqp_request->dynamic) {
2112 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2113 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2114 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2119 else if (major_code)
2131 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2132 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2136 struct nes_pd *nespd = to_nespd(ib_pd);
2137 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2138 struct nes_device *nesdev = nesvnic->nesdev;
2139 struct nes_adapter *nesadapter = nesdev->nesadapter;
2140 struct nes_mr *nesmr;
2142 struct nes_vpbl vpbl;
2143 struct nes_root_vpbl root_vpbl;
2147 u32 next_stag_index = 0;
2149 u32 root_pbl_index = 0;
2150 u32 cur_pbl_index = 0;
2151 int err = 0, pbl_depth = 0;
2159 vpbl.pbl_vbase = NULL;
2160 root_vpbl.pbl_vbase = NULL;
2161 root_vpbl.pbl_pbase = 0;
2163 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2164 stag_key = (u8)next_stag_index;
2168 next_stag_index >>= 8;
2169 next_stag_index %= nesadapter->max_mr;
2170 if (num_phys_buf > (1024*512)) {
2171 return ERR_PTR(-E2BIG);
2174 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2175 &stag_index, &next_stag_index);
2177 return ERR_PTR(err);
2180 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2182 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2183 return ERR_PTR(-ENOMEM);
2186 for (i = 0; i < num_phys_buf; i++) {
2188 if ((i & 0x01FF) == 0) {
2189 if (root_pbl_index == 1) {
2190 /* Allocate the root PBL */
2191 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2192 &root_vpbl.pbl_pbase);
2193 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2194 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2195 if (!root_vpbl.pbl_vbase) {
2196 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2198 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2200 return ERR_PTR(-ENOMEM);
2202 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2203 if (!root_vpbl.leaf_vpbl) {
2204 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2205 root_vpbl.pbl_pbase);
2206 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2208 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2210 return ERR_PTR(-ENOMEM);
2212 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2213 root_vpbl.pbl_vbase[0].pa_high =
2214 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2215 root_vpbl.leaf_vpbl[0] = vpbl;
2217 /* Allocate a 4K buffer for the PBL */
2218 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2220 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2221 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2222 if (!vpbl.pbl_vbase) {
2223 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2224 ibmr = ERR_PTR(-ENOMEM);
2228 /* Fill in the root table */
2229 if (1 <= root_pbl_index) {
2230 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2231 cpu_to_le32((u32)vpbl.pbl_pbase);
2232 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2233 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2234 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2239 if (buffer_list[i].addr & ~PAGE_MASK) {
2240 /* TODO: Unwind allocated buffers */
2241 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2242 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2243 (unsigned int) buffer_list[i].addr);
2244 ibmr = ERR_PTR(-EINVAL);
2249 if (!buffer_list[i].size) {
2250 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2251 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2252 ibmr = ERR_PTR(-EINVAL);
2257 region_length += buffer_list[i].size;
2258 if ((i != 0) && (single_page)) {
2259 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2262 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr);
2263 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2264 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2267 stag = stag_index << 8;
2269 stag += (u32)stag_key;
2271 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2272 " length = 0x%016lX, index = 0x%08X\n",
2273 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2275 region_length -= (*iova_start)&PAGE_MASK;
2277 /* Make the leaf PBL the root if only one PBL */
2278 if (root_pbl_index == 1) {
2279 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2285 pbl_count = root_pbl_index;
2287 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2288 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start);
2291 nesmr->ibmr.rkey = stag;
2292 nesmr->ibmr.lkey = stag;
2293 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2294 ibmr = &nesmr->ibmr;
2295 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2296 nesmr->pbls_used = pbl_count;
2297 if (pbl_count > 1) {
2302 ibmr = ERR_PTR(-ENOMEM);
2306 /* free the resources */
2307 if (root_pbl_index == 1) {
2308 /* single PBL case */
2309 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2311 for (i=0; i<root_pbl_index; i++) {
2312 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2313 root_vpbl.leaf_vpbl[i].pbl_pbase);
2315 kfree(root_vpbl.leaf_vpbl);
2316 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2317 root_vpbl.pbl_pbase);
2327 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2329 struct ib_phys_buf bl;
2332 nes_debug(NES_DBG_MR, "\n");
2334 bl.size = (u64)0xffffffffffULL;
2336 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2343 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2344 u64 virt, int acc, struct ib_udata *udata)
2349 dma_addr_t last_dma_addr = 0;
2350 dma_addr_t first_dma_addr = 0;
2351 struct nes_pd *nespd = to_nespd(pd);
2352 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2353 struct nes_device *nesdev = nesvnic->nesdev;
2354 struct nes_adapter *nesadapter = nesdev->nesadapter;
2355 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2356 struct ib_umem_chunk *chunk;
2357 struct nes_ucontext *nes_ucontext;
2358 struct nes_pbl *nespbl;
2359 struct nes_mr *nesmr;
2360 struct ib_umem *region;
2361 struct nes_mem_reg_req req;
2362 struct nes_vpbl vpbl;
2363 struct nes_root_vpbl root_vpbl;
2364 int nmap_index, page_index;
2366 int err, pbl_depth = 0;
2371 u32 next_stag_index;
2373 u32 root_pbl_index = 0;
2374 u32 cur_pbl_index = 0;
2380 region = ib_umem_get(pd->uobject->context, start, length, acc);
2381 if (IS_ERR(region)) {
2382 return (struct ib_mr *)region;
2385 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2386 " offset = %u, page size = %u.\n",
2387 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2388 region->offset, region->page_size);
2390 skip_pages = ((u32)region->offset) >> 12;
2392 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2393 return ERR_PTR(-EFAULT);
2394 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2396 switch (req.reg_type) {
2397 case IWNES_MEMREG_TYPE_MEM:
2400 vpbl.pbl_vbase = NULL;
2401 root_vpbl.pbl_vbase = NULL;
2402 root_vpbl.pbl_pbase = 0;
2404 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2405 stag_key = (u8)next_stag_index;
2407 driver_key = next_stag_index & 0x70000000;
2409 next_stag_index >>= 8;
2410 next_stag_index %= nesadapter->max_mr;
2412 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2413 nesadapter->max_mr, &stag_index, &next_stag_index);
2415 ib_umem_release(region);
2416 return ERR_PTR(err);
2419 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2421 ib_umem_release(region);
2422 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2423 return ERR_PTR(-ENOMEM);
2425 nesmr->region = region;
2427 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2428 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2429 chunk->nents, chunk->nmap);
2430 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2431 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2432 ib_umem_release(region);
2433 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2434 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2435 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2436 ibmr = ERR_PTR(-EINVAL);
2438 goto reg_user_mr_err;
2441 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2442 ib_umem_release(region);
2443 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2445 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2446 ibmr = ERR_PTR(-EINVAL);
2448 goto reg_user_mr_err;
2451 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2452 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2453 region_length -= skip_pages << 12;
2454 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2456 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2458 if ((page_count&0x01FF) == 0) {
2459 if (page_count>(1024*512)) {
2460 ib_umem_release(region);
2461 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2463 nes_free_resource(nesadapter,
2464 nesadapter->allocated_mrs, stag_index);
2466 ibmr = ERR_PTR(-E2BIG);
2467 goto reg_user_mr_err;
2469 if (root_pbl_index == 1) {
2470 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2471 8192, &root_vpbl.pbl_pbase);
2472 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2473 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2474 if (!root_vpbl.pbl_vbase) {
2475 ib_umem_release(region);
2476 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2478 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2481 ibmr = ERR_PTR(-ENOMEM);
2482 goto reg_user_mr_err;
2484 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2486 if (!root_vpbl.leaf_vpbl) {
2487 ib_umem_release(region);
2488 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2489 root_vpbl.pbl_pbase);
2490 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2492 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2495 ibmr = ERR_PTR(-ENOMEM);
2496 goto reg_user_mr_err;
2498 root_vpbl.pbl_vbase[0].pa_low =
2499 cpu_to_le32((u32)vpbl.pbl_pbase);
2500 root_vpbl.pbl_vbase[0].pa_high =
2501 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2502 root_vpbl.leaf_vpbl[0] = vpbl;
2504 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2506 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2507 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2508 if (!vpbl.pbl_vbase) {
2509 ib_umem_release(region);
2510 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2511 ibmr = ERR_PTR(-ENOMEM);
2513 goto reg_user_mr_err;
2515 if (1 <= root_pbl_index) {
2516 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2517 cpu_to_le32((u32)vpbl.pbl_pbase);
2518 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2519 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2520 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2526 if (page_count != 0) {
2527 if ((last_dma_addr+4096) !=
2528 (sg_dma_address(&chunk->page_list[nmap_index])+
2531 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2534 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2536 last_dma_addr = first_dma_addr;
2540 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2541 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2542 (page_index*4096)));
2543 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2544 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2545 (page_index*4096))) >> 32)));
2552 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2553 " stag_key=0x%08x\n",
2554 stag_index, driver_key, stag_key);
2555 stag = stag_index << 8;
2557 stag += (u32)stag_key;
2563 /* Make the leaf PBL the root if only one PBL */
2564 if (root_pbl_index == 1) {
2565 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2571 pbl_count = root_pbl_index;
2574 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2575 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2576 stag, (unsigned int)iova_start,
2577 (unsigned int)region_length, stag_index,
2578 (unsigned long long)region->length, pbl_count);
2579 ret = nes_reg_mr( nesdev, nespd, stag, region->length, &root_vpbl,
2580 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc, &iova_start);
2582 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2585 nesmr->ibmr.rkey = stag;
2586 nesmr->ibmr.lkey = stag;
2587 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2588 ibmr = &nesmr->ibmr;
2589 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2590 nesmr->pbls_used = pbl_count;
2591 if (pbl_count > 1) {
2595 ib_umem_release(region);
2597 ibmr = ERR_PTR(-ENOMEM);
2601 /* free the resources */
2602 if (root_pbl_index == 1) {
2603 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2606 for (page_index=0; page_index<root_pbl_index; page_index++) {
2607 pci_free_consistent(nesdev->pcidev, 4096,
2608 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2609 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2611 kfree(root_vpbl.leaf_vpbl);
2612 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2613 root_vpbl.pbl_pbase);
2616 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2620 case IWNES_MEMREG_TYPE_QP:
2621 case IWNES_MEMREG_TYPE_CQ:
2622 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2624 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2625 ib_umem_release(region);
2626 return ERR_PTR(-ENOMEM);
2628 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2630 ib_umem_release(region);
2632 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2633 return ERR_PTR(-ENOMEM);
2635 nesmr->region = region;
2636 nes_ucontext = to_nesucontext(pd->uobject->context);
2637 pbl_depth = region->length >> 12;
2638 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2639 nespbl->pbl_size = pbl_depth*sizeof(u64);
2640 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2641 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2643 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2646 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2647 nespbl->pbl_size, pbl_depth);
2648 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2649 &nespbl->pbl_pbase);
2651 ib_umem_release(region);
2654 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2655 return ERR_PTR(-ENOMEM);
2658 nespbl->pbl_vbase = (u64 *)pbl;
2659 nespbl->user_base = start;
2660 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2661 " pbl_vbase=%p user_base=0x%lx\n",
2662 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2663 (void *) nespbl->pbl_vbase, nespbl->user_base);
2665 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2666 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2667 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2668 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2669 nespbl->page = sg_page(&chunk->page_list[0]);
2670 for (page_index=0; page_index<chunk_pages; page_index++) {
2671 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2672 (sg_dma_address(&chunk->page_list[nmap_index])+
2673 (page_index*4096)));
2674 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2675 (sg_dma_address(&chunk->page_list[nmap_index])+
2676 (page_index*4096)))>>32);
2677 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2678 (unsigned long long)*pbl,
2679 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2684 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2685 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2687 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2689 nesmr->ibmr.rkey = -1;
2690 nesmr->ibmr.lkey = -1;
2691 nesmr->mode = req.reg_type;
2692 return &nesmr->ibmr;
2696 return ERR_PTR(-ENOSYS);
2703 static int nes_dereg_mr(struct ib_mr *ib_mr)
2705 struct nes_mr *nesmr = to_nesmr(ib_mr);
2706 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2707 struct nes_device *nesdev = nesvnic->nesdev;
2708 struct nes_adapter *nesadapter = nesdev->nesadapter;
2709 struct nes_hw_cqp_wqe *cqp_wqe;
2710 struct nes_cqp_request *cqp_request;
2711 unsigned long flags;
2716 if (nesmr->region) {
2717 ib_umem_release(nesmr->region);
2719 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2724 /* Deallocate the region with the adapter */
2726 cqp_request = nes_get_cqp_request(nesdev);
2727 if (cqp_request == NULL) {
2728 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2731 cqp_request->waiting = 1;
2732 cqp_wqe = &cqp_request->cqp_wqe;
2734 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2735 if (nesmr->pbls_used != 0) {
2736 if (nesmr->pbl_4k) {
2737 nesadapter->free_4kpbl += nesmr->pbls_used;
2738 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
2739 printk(KERN_ERR PFX "free 4KB PBLs(%u) has exceeded the max(%u)\n",
2740 nesadapter->free_4kpbl, nesadapter->max_4kpbl);
2743 nesadapter->free_256pbl += nesmr->pbls_used;
2744 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
2745 printk(KERN_ERR PFX "free 256B PBLs(%u) has exceeded the max(%u)\n",
2746 nesadapter->free_256pbl, nesadapter->max_256pbl);
2751 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2752 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2753 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2754 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2755 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2756 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2758 atomic_set(&cqp_request->refcount, 2);
2759 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2762 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2763 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2765 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2766 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2767 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2769 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2770 (ib_mr->rkey & 0x0fffff00) >> 8);
2774 major_code = cqp_request->major_code;
2775 minor_code = cqp_request->minor_code;
2776 if (atomic_dec_and_test(&cqp_request->refcount)) {
2777 if (cqp_request->dynamic) {
2780 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2781 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2782 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2786 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2787 " ib_mr=%p, rkey = 0x%08X\n",
2788 ib_mr, ib_mr->rkey);
2790 } else if (major_code) {
2791 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2792 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2793 major_code, minor_code, ib_mr, ib_mr->rkey);
2803 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2806 struct nes_ib_device *nesibdev =
2807 container_of(dev, struct nes_ib_device, ibdev.dev);
2808 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2810 nes_debug(NES_DBG_INIT, "\n");
2811 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2818 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2821 struct nes_ib_device *nesibdev =
2822 container_of(dev, struct nes_ib_device, ibdev.dev);
2823 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2825 nes_debug(NES_DBG_INIT, "\n");
2826 return sprintf(buf, "%x.%x.%x\n",
2827 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 32),
2828 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 16) & 0xffff,
2829 (int)(nesvnic->nesdev->nesadapter->fw_ver & 0xffff));
2836 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2839 nes_debug(NES_DBG_INIT, "\n");
2840 return sprintf(buf, "NES020\n");
2847 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2850 nes_debug(NES_DBG_INIT, "\n");
2851 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2855 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2856 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2857 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2858 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2860 static struct device_attribute *nes_dev_attributes[] = {
2871 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2872 int attr_mask, struct ib_qp_init_attr *init_attr)
2874 struct nes_qp *nesqp = to_nesqp(ibqp);
2876 nes_debug(NES_DBG_QP, "\n");
2878 attr->qp_access_flags = 0;
2879 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2880 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2881 attr->cap.max_recv_sge = 1;
2882 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2883 init_attr->cap.max_inline_data = 0;
2885 init_attr->cap.max_inline_data = 64;
2888 init_attr->event_handler = nesqp->ibqp.event_handler;
2889 init_attr->qp_context = nesqp->ibqp.qp_context;
2890 init_attr->send_cq = nesqp->ibqp.send_cq;
2891 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2892 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2893 init_attr->cap = attr->cap;
2902 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2903 u32 next_iwarp_state, u32 wait_completion)
2905 struct nes_hw_cqp_wqe *cqp_wqe;
2906 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2907 /* struct iw_cm_event cm_event; */
2908 struct nes_cqp_request *cqp_request;
2909 unsigned long flags;
2913 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2914 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2916 cqp_request = nes_get_cqp_request(nesdev);
2917 if (cqp_request == NULL) {
2918 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2921 if (wait_completion) {
2922 cqp_request->waiting = 1;
2924 cqp_request->waiting = 0;
2926 cqp_wqe = &cqp_request->cqp_wqe;
2928 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2929 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2930 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2931 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2932 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2933 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2934 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2936 atomic_set(&cqp_request->refcount, 2);
2937 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2940 if (wait_completion) {
2941 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2942 nesqp->hwqp.qp_id); */
2943 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2945 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2946 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2947 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2948 major_code = cqp_request->major_code;
2950 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2951 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2952 nesqp->hwqp.qp_id, cqp_request->major_code,
2953 cqp_request->minor_code, next_iwarp_state);
2955 if (atomic_dec_and_test(&cqp_request->refcount)) {
2956 if (cqp_request->dynamic) {
2959 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2960 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2961 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2966 else if (major_code)
2979 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2980 int attr_mask, struct ib_udata *udata)
2982 struct nes_qp *nesqp = to_nesqp(ibqp);
2983 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2984 struct nes_device *nesdev = nesvnic->nesdev;
2987 u32 next_iwarp_state = 0;
2989 unsigned long qplockflags;
2991 u16 original_last_aeq;
2992 u8 issue_modify_qp = 0;
2993 u8 issue_disconnect = 0;
2996 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2997 " iwarp_state=0x%X, refcount=%d\n",
2998 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2999 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
3001 nes_add_ref(&nesqp->ibqp);
3002 spin_lock_irqsave(&nesqp->lock, qplockflags);
3004 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
3005 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
3006 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
3007 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
3009 if (attr_mask & IB_QP_STATE) {
3010 switch (attr->qp_state) {
3012 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
3014 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3015 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3016 nes_rem_ref(&nesqp->ibqp);
3019 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3020 issue_modify_qp = 1;
3023 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
3025 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3026 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3027 nes_rem_ref(&nesqp->ibqp);
3030 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3031 issue_modify_qp = 1;
3034 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
3036 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
3037 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3038 nes_rem_ref(&nesqp->ibqp);
3041 if (nesqp->cm_id == NULL) {
3042 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
3043 nesqp->hwqp.qp_id );
3044 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3045 nes_rem_ref(&nesqp->ibqp);
3048 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
3049 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
3050 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
3051 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
3052 issue_modify_qp = 1;
3053 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
3054 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
3055 nesqp->hte_added = 1;
3058 issue_modify_qp = 1;
3059 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3060 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3061 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3062 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3063 nes_rem_ref(&nesqp->ibqp);
3066 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3067 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3068 " ignored due to current iWARP state\n",
3070 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3071 nes_rem_ref(&nesqp->ibqp);
3074 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3075 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3076 " already done based on hw state.\n",
3078 issue_modify_qp = 0;
3079 nesqp->in_disconnect = 0;
3081 switch (nesqp->hw_iwarp_state) {
3082 case NES_AEQE_IWARP_STATE_CLOSING:
3083 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3084 case NES_AEQE_IWARP_STATE_TERMINATE:
3085 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3087 case NES_AEQE_IWARP_STATE_ERROR:
3088 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3091 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3092 nesqp->in_disconnect = 1;
3093 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3099 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3101 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3102 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3103 nes_rem_ref(&nesqp->ibqp);
3106 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3107 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3108 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3109 issue_modify_qp = 1;
3110 nesqp->in_disconnect = 1;
3114 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3115 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3116 nes_rem_ref(&nesqp->ibqp);
3119 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3121 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3122 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3123 if (nesqp->hte_added) {
3124 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3125 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3126 nesqp->hte_added = 0;
3128 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3129 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3130 next_iwarp_state |= NES_CQP_QP_RESET;
3131 nesqp->in_disconnect = 1;
3133 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3134 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3137 issue_modify_qp = 1;
3138 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3141 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3142 nes_rem_ref(&nesqp->ibqp);
3147 nesqp->ibqp_state = attr->qp_state;
3148 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3149 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3150 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3151 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3152 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3153 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3154 nesqp->iwarp_state);
3155 issue_disconnect = 1;
3157 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3158 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3159 nesqp->iwarp_state);
3163 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3164 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3165 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3166 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3167 issue_modify_qp = 1;
3169 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3170 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3171 issue_modify_qp = 1;
3173 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3174 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3175 issue_modify_qp = 1;
3177 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3178 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3179 issue_modify_qp = 1;
3182 if (nesqp->user_mode) {
3183 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3184 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3185 issue_modify_qp = 1;
3189 original_last_aeq = nesqp->last_aeq;
3190 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3192 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3197 if (issue_modify_qp) {
3198 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3199 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 1);
3201 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3202 " failed for QP%u.\n",
3203 next_iwarp_state, nesqp->hwqp.qp_id);
3207 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3208 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3209 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3210 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3211 original_last_aeq, nesqp->last_aeq);
3213 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3216 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3217 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3218 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3219 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3220 original_last_aeq, nesqp->last_aeq);
3221 /* this one is for the cm_disconnect thread */
3222 nes_add_ref(&nesqp->ibqp);
3223 spin_lock_irqsave(&nesqp->lock, qplockflags);
3224 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3225 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3226 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3227 nes_cm_disconn(nesqp);
3229 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3230 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3231 nes_rem_ref(&nesqp->ibqp);
3234 spin_lock_irqsave(&nesqp->lock, qplockflags);
3236 /* These two are for the timer thread */
3237 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3238 nes_add_ref(&nesqp->ibqp);
3239 nesqp->cm_id->add_ref(nesqp->cm_id);
3240 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3241 " need ae to finish up, original_last_aeq = 0x%04X."
3242 " last_aeq = 0x%04X, scheduling timer.\n",
3243 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3244 original_last_aeq, nesqp->last_aeq);
3245 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3247 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3249 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3250 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3251 " need ae to finish up, original_last_aeq = 0x%04X."
3252 " last_aeq = 0x%04X.\n",
3253 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3254 original_last_aeq, nesqp->last_aeq);
3258 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3259 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3260 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3261 original_last_aeq, nesqp->last_aeq);
3262 nes_rem_ref(&nesqp->ibqp);
3265 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3266 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3267 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3268 original_last_aeq, nesqp->last_aeq);
3269 nes_rem_ref(&nesqp->ibqp);
3274 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3275 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3282 * nes_muticast_attach
3284 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3286 nes_debug(NES_DBG_INIT, "\n");
3292 * nes_multicast_detach
3294 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3296 nes_debug(NES_DBG_INIT, "\n");
3304 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3305 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3306 struct ib_mad *in_mad, struct ib_mad *out_mad)
3308 nes_debug(NES_DBG_INIT, "\n");
3313 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3316 int total_payload_length = 0;
3317 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3318 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3319 ib_wr->sg_list[sge_index].addr);
3320 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3321 ib_wr->sg_list[sge_index].length);
3323 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3324 (ib_wr->sg_list[sge_index].lkey));
3326 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3328 total_payload_length += ib_wr->sg_list[sge_index].length;
3330 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3331 total_payload_length);
3332 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3333 total_payload_length);
3339 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3340 struct ib_send_wr **bad_wr)
3343 unsigned long flags = 0;
3344 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3345 struct nes_device *nesdev = nesvnic->nesdev;
3346 struct nes_qp *nesqp = to_nesqp(ibqp);
3347 struct nes_hw_qp_wqe *wqe;
3349 u32 qsize = nesqp->hwqp.sq_size;
3354 u32 total_payload_length;
3359 total_payload_length = 0;
3361 if (nesqp->ibqp_state > IB_QPS_RTS)
3364 spin_lock_irqsave(&nesqp->lock, flags);
3366 head = nesqp->hwqp.sq_head;
3369 /* Check for SQ overflow */
3370 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3375 wqe = &nesqp->hwqp.sq_vbase[head];
3376 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3377 nesqp->hwqp.qp_id, wqe, head); */
3378 nes_fill_init_qp_wqe(wqe, nesqp, head);
3379 u64temp = (u64)(ib_wr->wr_id);
3380 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3382 switch (ib_wr->opcode) {
3384 if (ib_wr->send_flags & IB_SEND_SOLICITED) {
3385 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3387 wqe_misc = NES_IWARP_SQ_OP_SEND;
3389 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3393 if (ib_wr->send_flags & IB_SEND_FENCE) {
3394 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3396 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3397 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3398 (ib_wr->sg_list[0].length <= 64)) {
3399 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3400 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3401 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3402 ib_wr->sg_list[0].length);
3403 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3405 fill_wqe_sg_send(wqe, ib_wr, 1);
3409 case IB_WR_RDMA_WRITE:
3410 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3411 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3412 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3414 nesdev->nesadapter->max_sge);
3418 if (ib_wr->send_flags & IB_SEND_FENCE) {
3419 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3422 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3423 ib_wr->wr.rdma.rkey);
3424 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3425 ib_wr->wr.rdma.remote_addr);
3427 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3428 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3429 (ib_wr->sg_list[0].length <= 64)) {
3430 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3431 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3432 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3433 ib_wr->sg_list[0].length);
3434 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3436 fill_wqe_sg_send(wqe, ib_wr, 1);
3438 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3439 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3441 case IB_WR_RDMA_READ:
3442 /* iWARP only supports 1 sge for RDMA reads */
3443 if (ib_wr->num_sge > 1) {
3444 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3449 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3450 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3451 ib_wr->wr.rdma.remote_addr);
3452 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3453 ib_wr->wr.rdma.rkey);
3454 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3455 ib_wr->sg_list->length);
3456 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3457 ib_wr->sg_list->addr);
3458 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3459 ib_wr->sg_list->lkey);
3467 if (ib_wr->send_flags & IB_SEND_SIGNALED) {
3468 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3470 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3472 ib_wr = ib_wr->next;
3480 nesqp->hwqp.sq_head = head;
3483 counter = min(wqe_count, ((u32)255));
3484 wqe_count -= counter;
3485 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3486 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3489 spin_unlock_irqrestore(&nesqp->lock, flags);
3500 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3501 struct ib_recv_wr **bad_wr)
3504 unsigned long flags = 0;
3505 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3506 struct nes_device *nesdev = nesvnic->nesdev;
3507 struct nes_qp *nesqp = to_nesqp(ibqp);
3508 struct nes_hw_qp_wqe *wqe;
3511 u32 qsize = nesqp->hwqp.rq_size;
3515 u32 total_payload_length;
3517 if (nesqp->ibqp_state > IB_QPS_RTS)
3520 spin_lock_irqsave(&nesqp->lock, flags);
3522 head = nesqp->hwqp.rq_head;
3525 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3529 /* Check for RQ overflow */
3530 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3535 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3536 wqe = &nesqp->hwqp.rq_vbase[head];
3538 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3539 nesqp->hwqp.qp_id, wqe, head); */
3540 nes_fill_init_qp_wqe(wqe, nesqp, head);
3541 u64temp = (u64)(ib_wr->wr_id);
3542 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3544 total_payload_length = 0;
3545 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3546 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3547 ib_wr->sg_list[sge_index].addr);
3548 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3549 ib_wr->sg_list[sge_index].length);
3550 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3551 ib_wr->sg_list[sge_index].lkey);
3553 total_payload_length += ib_wr->sg_list[sge_index].length;
3555 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3556 total_payload_length);
3558 ib_wr = ib_wr->next;
3565 nesqp->hwqp.rq_head = head;
3568 counter = min(wqe_count, ((u32)255));
3569 wqe_count -= counter;
3570 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3573 spin_unlock_irqrestore(&nesqp->lock, flags);
3584 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3589 unsigned long flags = 0;
3590 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3591 struct nes_device *nesdev = nesvnic->nesdev;
3592 struct nes_cq *nescq = to_nescq(ibcq);
3593 struct nes_qp *nesqp;
3594 struct nes_hw_cqe cqe;
3603 nes_debug(NES_DBG_CQ, "\n");
3605 spin_lock_irqsave(&nescq->lock, flags);
3607 head = nescq->hw_cq.cq_head;
3608 cq_size = nescq->hw_cq.cq_size;
3610 while (cqe_count < num_entries) {
3611 if (le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3614 * Make sure we read CQ entry contents *after*
3615 * we've checked the valid bit.
3619 cqe = nescq->hw_cq.cq_vbase[head];
3620 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3621 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3622 wqe_index = u32temp &
3623 (nesdev->nesadapter->max_qp_wr - 1);
3624 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3625 /* parse CQE, get completion context from WQE (either rq or sq */
3626 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3628 nesqp = *((struct nes_qp **)&u64temp);
3629 memset(entry, 0, sizeof *entry);
3630 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3631 entry->status = IB_WC_SUCCESS;
3633 entry->status = IB_WC_WR_FLUSH_ERR;
3636 entry->qp = &nesqp->ibqp;
3637 entry->src_qp = nesqp->hwqp.qp_id;
3639 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3640 if (nesqp->skip_lsmm) {
3641 nesqp->skip_lsmm = 0;
3642 wq_tail = nesqp->hwqp.sq_tail++;
3645 /* Working on a SQ Completion*/
3646 wq_tail = wqe_index;
3647 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3648 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3649 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3650 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3651 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3652 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3653 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3655 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3656 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3657 case NES_IWARP_SQ_OP_RDMAW:
3658 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3659 entry->opcode = IB_WC_RDMA_WRITE;
3661 case NES_IWARP_SQ_OP_RDMAR:
3662 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3663 entry->opcode = IB_WC_RDMA_READ;
3664 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3665 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3667 case NES_IWARP_SQ_OP_SENDINV:
3668 case NES_IWARP_SQ_OP_SENDSEINV:
3669 case NES_IWARP_SQ_OP_SEND:
3670 case NES_IWARP_SQ_OP_SENDSE:
3671 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3672 entry->opcode = IB_WC_SEND;
3676 /* Working on a RQ Completion*/
3677 wq_tail = wqe_index;
3678 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3679 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3680 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3681 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3682 entry->opcode = IB_WC_RECV;
3684 entry->wr_id = wrid;
3686 if (++head >= cq_size)
3689 nescq->polled_completions++;
3690 if ((nescq->polled_completions > (cq_size / 2)) ||
3691 (nescq->polled_completions == 255)) {
3692 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3693 " are pending %u of %u.\n",
3694 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3695 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3696 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3697 nescq->polled_completions = 0;
3704 if (nescq->polled_completions) {
3705 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3706 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3707 nescq->polled_completions = 0;
3710 nescq->hw_cq.cq_head = head;
3711 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3712 cqe_count, nescq->hw_cq.cq_number);
3714 spin_unlock_irqrestore(&nescq->lock, flags);
3723 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3725 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3726 struct nes_device *nesdev = nesvnic->nesdev;
3727 struct nes_cq *nescq = to_nescq(ibcq);
3730 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3731 nescq->hw_cq.cq_number);
3733 cq_arm = nescq->hw_cq.cq_number;
3734 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3735 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3736 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3737 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3741 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3742 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3749 * nes_init_ofa_device
3751 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3753 struct nes_ib_device *nesibdev;
3754 struct nes_vnic *nesvnic = netdev_priv(netdev);
3755 struct nes_device *nesdev = nesvnic->nesdev;
3757 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3758 if (nesibdev == NULL) {
3761 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3762 nesibdev->ibdev.owner = THIS_MODULE;
3764 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3765 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3766 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3768 nesibdev->ibdev.uverbs_cmd_mask =
3769 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3770 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3771 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3772 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3773 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3774 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3775 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3776 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3777 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3778 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3779 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3780 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3781 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3782 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3783 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3784 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3785 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3786 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3787 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3788 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3789 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3790 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3792 nesibdev->ibdev.phys_port_cnt = 1;
3793 nesibdev->ibdev.num_comp_vectors = 1;
3794 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3795 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3796 nesibdev->ibdev.query_device = nes_query_device;
3797 nesibdev->ibdev.query_port = nes_query_port;
3798 nesibdev->ibdev.modify_port = nes_modify_port;
3799 nesibdev->ibdev.query_pkey = nes_query_pkey;
3800 nesibdev->ibdev.query_gid = nes_query_gid;
3801 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3802 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3803 nesibdev->ibdev.mmap = nes_mmap;
3804 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3805 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3806 nesibdev->ibdev.create_ah = nes_create_ah;
3807 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3808 nesibdev->ibdev.create_qp = nes_create_qp;
3809 nesibdev->ibdev.modify_qp = nes_modify_qp;
3810 nesibdev->ibdev.query_qp = nes_query_qp;
3811 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3812 nesibdev->ibdev.create_cq = nes_create_cq;
3813 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3814 nesibdev->ibdev.poll_cq = nes_poll_cq;
3815 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3816 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3817 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3818 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3819 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3820 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3821 nesibdev->ibdev.bind_mw = nes_bind_mw;
3823 nesibdev->ibdev.alloc_fmr = nes_alloc_fmr;
3824 nesibdev->ibdev.unmap_fmr = nes_unmap_fmr;
3825 nesibdev->ibdev.dealloc_fmr = nes_dealloc_fmr;
3826 nesibdev->ibdev.map_phys_fmr = nes_map_phys_fmr;
3828 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3829 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3830 nesibdev->ibdev.process_mad = nes_process_mad;
3832 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3833 nesibdev->ibdev.post_send = nes_post_send;
3834 nesibdev->ibdev.post_recv = nes_post_recv;
3836 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3837 if (nesibdev->ibdev.iwcm == NULL) {
3838 ib_dealloc_device(&nesibdev->ibdev);
3841 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3842 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3843 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3844 nesibdev->ibdev.iwcm->connect = nes_connect;
3845 nesibdev->ibdev.iwcm->accept = nes_accept;
3846 nesibdev->ibdev.iwcm->reject = nes_reject;
3847 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3848 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3855 * nes_destroy_ofa_device
3857 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3859 if (nesibdev == NULL)
3862 nes_unregister_ofa_device(nesibdev);
3864 kfree(nesibdev->ibdev.iwcm);
3865 ib_dealloc_device(&nesibdev->ibdev);
3870 * nes_register_ofa_device
3872 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3874 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3875 struct nes_device *nesdev = nesvnic->nesdev;
3876 struct nes_adapter *nesadapter = nesdev->nesadapter;
3879 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3884 /* Get the resources allocated to this device */
3885 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3886 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3887 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3888 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3890 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3891 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3895 device_remove_file(&nesibdev->ibdev.dev,
3896 nes_dev_attributes[i]);
3898 ib_unregister_device(&nesibdev->ibdev);
3903 nesvnic->of_device_registered = 1;
3910 * nes_unregister_ofa_device
3912 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3914 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3917 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3918 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3921 if (nesvnic->of_device_registered) {
3922 ib_unregister_device(&nesibdev->ibdev);
3925 nesvnic->of_device_registered = 0;