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;
56 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) {
58 struct nes_pd *nespd = to_nespd(ibpd);
59 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
60 struct nes_device *nesdev = nesvnic->nesdev;
61 struct nes_adapter *nesadapter = nesdev->nesadapter;
62 struct nes_cqp_request *cqp_request;
65 struct nes_hw_cqp_wqe *cqp_wqe;
69 u32 next_stag_index = 0;
73 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
74 stag_key = (u8)next_stag_index;
78 next_stag_index >>= 8;
79 next_stag_index %= nesadapter->max_mr;
81 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
82 nesadapter->max_mr, &stag_index, &next_stag_index);
87 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
89 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
90 return ERR_PTR(-ENOMEM);
93 stag = stag_index << 8;
95 stag += (u32)stag_key;
97 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
100 /* Register the region with the adapter */
101 cqp_request = nes_get_cqp_request(nesdev);
102 if (cqp_request == NULL) {
104 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
105 return ERR_PTR(-ENOMEM);
108 cqp_request->waiting = 1;
109 cqp_wqe = &cqp_request->cqp_wqe;
111 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
112 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
113 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
114 NES_CQP_STAG_REM_ACC_EN);
116 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
117 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
118 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
120 atomic_set(&cqp_request->refcount, 2);
121 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
124 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
126 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
127 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
128 stag, ret, cqp_request->major_code, cqp_request->minor_code);
129 if ((!ret) || (cqp_request->major_code)) {
130 if (atomic_dec_and_test(&cqp_request->refcount)) {
131 if (cqp_request->dynamic) {
134 spin_lock_irqsave(&nesdev->cqp.lock, flags);
135 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
136 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
140 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
142 return ERR_PTR(-ETIME);
144 return ERR_PTR(-ENOMEM);
147 if (atomic_dec_and_test(&cqp_request->refcount)) {
148 if (cqp_request->dynamic) {
151 spin_lock_irqsave(&nesdev->cqp.lock, flags);
152 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
153 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
158 nesmr->ibmw.rkey = stag;
159 nesmr->mode = IWNES_MEMREG_TYPE_MW;
162 nesmr->pbls_used = 0;
171 static int nes_dealloc_mw(struct ib_mw *ibmw)
173 struct nes_mr *nesmr = to_nesmw(ibmw);
174 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
175 struct nes_device *nesdev = nesvnic->nesdev;
176 struct nes_adapter *nesadapter = nesdev->nesadapter;
177 struct nes_hw_cqp_wqe *cqp_wqe;
178 struct nes_cqp_request *cqp_request;
183 /* Deallocate the window with the adapter */
184 cqp_request = nes_get_cqp_request(nesdev);
185 if (cqp_request == NULL) {
186 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
189 cqp_request->waiting = 1;
190 cqp_wqe = &cqp_request->cqp_wqe;
191 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
192 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
193 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
195 atomic_set(&cqp_request->refcount, 2);
196 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
199 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
201 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
203 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
204 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
205 ret, cqp_request->major_code, cqp_request->minor_code);
206 if ((!ret) || (cqp_request->major_code)) {
207 if (atomic_dec_and_test(&cqp_request->refcount)) {
208 if (cqp_request->dynamic) {
211 spin_lock_irqsave(&nesdev->cqp.lock, flags);
212 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
213 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
222 if (atomic_dec_and_test(&cqp_request->refcount)) {
223 if (cqp_request->dynamic) {
226 spin_lock_irqsave(&nesdev->cqp.lock, flags);
227 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
228 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
233 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
234 (ibmw->rkey & 0x0fffff00) >> 8);
244 static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
245 struct ib_mw_bind *ibmw_bind)
248 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
249 struct nes_device *nesdev = nesvnic->nesdev;
250 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
251 struct nes_qp *nesqp = to_nesqp(ibqp);
252 struct nes_hw_qp_wqe *wqe;
253 unsigned long flags = 0;
258 if (nesqp->ibqp_state > IB_QPS_RTS)
261 spin_lock_irqsave(&nesqp->lock, flags);
263 head = nesqp->hwqp.sq_head;
264 qsize = nesqp->hwqp.sq_tail;
266 /* Check for SQ overflow */
267 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
268 spin_unlock_irqrestore(&nesqp->lock, flags);
272 wqe = &nesqp->hwqp.sq_vbase[head];
273 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
274 nes_fill_init_qp_wqe(wqe, nesqp, head);
275 u64temp = ibmw_bind->wr_id;
276 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
277 wqe_misc = NES_IWARP_SQ_OP_BIND;
279 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
281 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
282 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
284 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_WRITE) {
285 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
287 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_READ) {
288 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
291 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
292 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX, ibmw_bind->mr->lkey);
293 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
294 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
296 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
297 u64temp = (u64)ibmw_bind->addr;
298 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
304 nesqp->hwqp.sq_head = head;
307 nes_write32(nesdev->regs+NES_WQE_ALLOC,
308 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
310 spin_unlock_irqrestore(&nesqp->lock, flags);
319 static struct ib_fmr *nes_alloc_fmr(struct ib_pd *ibpd,
320 int ibmr_access_flags,
321 struct ib_fmr_attr *ibfmr_attr)
324 struct nes_pd *nespd = to_nespd(ibpd);
325 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
326 struct nes_device *nesdev = nesvnic->nesdev;
327 struct nes_adapter *nesadapter = nesdev->nesadapter;
328 struct nes_fmr *nesfmr;
329 struct nes_cqp_request *cqp_request;
330 struct nes_hw_cqp_wqe *cqp_wqe;
334 u32 next_stag_index = 0;
339 struct nes_vpbl vpbl;
341 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
342 stag_key = (u8)next_stag_index;
346 next_stag_index >>= 8;
347 next_stag_index %= nesadapter->max_mr;
349 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
350 nesadapter->max_mr, &stag_index, &next_stag_index);
352 goto failed_resource_alloc;
355 nesfmr = kzalloc(sizeof(*nesfmr), GFP_KERNEL);
358 goto failed_fmr_alloc;
361 nesfmr->nesmr.mode = IWNES_MEMREG_TYPE_FMR;
362 if (ibfmr_attr->max_pages == 1) {
363 /* use zero length PBL */
364 nesfmr->nesmr.pbl_4k = 0;
365 nesfmr->nesmr.pbls_used = 0;
366 } else if (ibfmr_attr->max_pages <= 32) {
368 nesfmr->nesmr.pbl_4k = 0;
369 nesfmr->nesmr.pbls_used = 1;
370 } else if (ibfmr_attr->max_pages <= 512) {
372 nesfmr->nesmr.pbl_4k = 1;
373 nesfmr->nesmr.pbls_used = 1;
375 /* use two level 4K PBLs */
376 /* add support for two level 256B PBLs */
377 nesfmr->nesmr.pbl_4k = 1;
378 nesfmr->nesmr.pbls_used = 1 + (ibfmr_attr->max_pages >> 9) +
379 ((ibfmr_attr->max_pages & 511) ? 1 : 0);
381 /* Register the region with the adapter */
382 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
384 /* track PBL resources */
385 if (nesfmr->nesmr.pbls_used != 0) {
386 if (nesfmr->nesmr.pbl_4k) {
387 if (nesfmr->nesmr.pbls_used > nesadapter->free_4kpbl) {
388 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
390 goto failed_vpbl_alloc;
392 nesadapter->free_4kpbl -= nesfmr->nesmr.pbls_used;
395 if (nesfmr->nesmr.pbls_used > nesadapter->free_256pbl) {
396 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
398 goto failed_vpbl_alloc;
400 nesadapter->free_256pbl -= nesfmr->nesmr.pbls_used;
406 if (nesfmr->nesmr.pbls_used == 0) {
407 nesfmr->root_vpbl.pbl_vbase = NULL;
408 nes_debug(NES_DBG_MR, "zero level pbl \n");
409 } else if (nesfmr->nesmr.pbls_used == 1) {
410 /* can change it to kmalloc & dma_map_single */
411 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
412 &nesfmr->root_vpbl.pbl_pbase);
413 if (!nesfmr->root_vpbl.pbl_vbase) {
414 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
416 goto failed_vpbl_alloc;
418 nesfmr->leaf_pbl_cnt = 0;
419 nes_debug(NES_DBG_MR, "one level pbl, root_vpbl.pbl_vbase=%p \n",
420 nesfmr->root_vpbl.pbl_vbase);
424 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
425 &nesfmr->root_vpbl.pbl_pbase);
426 if (!nesfmr->root_vpbl.pbl_vbase) {
427 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
429 goto failed_vpbl_alloc;
432 nesfmr->root_vpbl.leaf_vpbl = kzalloc(sizeof(*nesfmr->root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
433 if (!nesfmr->root_vpbl.leaf_vpbl) {
434 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
436 goto failed_leaf_vpbl_alloc;
439 nesfmr->leaf_pbl_cnt = nesfmr->nesmr.pbls_used-1;
440 nes_debug(NES_DBG_MR, "two level pbl, root_vpbl.pbl_vbase=%p"
441 " leaf_pbl_cnt=%d root_vpbl.leaf_vpbl=%p\n",
442 nesfmr->root_vpbl.pbl_vbase, nesfmr->leaf_pbl_cnt, nesfmr->root_vpbl.leaf_vpbl);
444 for (i=0; i<nesfmr->leaf_pbl_cnt; i++)
445 nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase = NULL;
447 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
448 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
451 if (!vpbl.pbl_vbase) {
453 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
454 goto failed_leaf_vpbl_pages_alloc;
457 nesfmr->root_vpbl.pbl_vbase[i].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
458 nesfmr->root_vpbl.pbl_vbase[i].pa_high = cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
459 nesfmr->root_vpbl.leaf_vpbl[i] = vpbl;
461 nes_debug(NES_DBG_MR, "pbase_low=0x%x, pbase_high=0x%x, vpbl=%p\n",
462 nesfmr->root_vpbl.pbl_vbase[i].pa_low,
463 nesfmr->root_vpbl.pbl_vbase[i].pa_high,
464 &nesfmr->root_vpbl.leaf_vpbl[i]);
467 nesfmr->ib_qp = NULL;
468 nesfmr->access_rights =0;
470 stag = stag_index << 8;
472 stag += (u32)stag_key;
474 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
475 cqp_request = nes_get_cqp_request(nesdev);
476 if (cqp_request == NULL) {
477 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
479 goto failed_leaf_vpbl_pages_alloc;
481 cqp_request->waiting = 1;
482 cqp_wqe = &cqp_request->cqp_wqe;
484 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
487 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
489 if (nesfmr->nesmr.pbl_4k == 1)
490 opcode |= NES_CQP_STAG_PBL_BLK_SIZE;
492 if (ibmr_access_flags & IB_ACCESS_REMOTE_WRITE) {
493 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE |
494 NES_CQP_STAG_RIGHTS_LOCAL_WRITE | NES_CQP_STAG_REM_ACC_EN;
495 nesfmr->access_rights |=
496 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_RIGHTS_LOCAL_WRITE |
497 NES_CQP_STAG_REM_ACC_EN;
500 if (ibmr_access_flags & IB_ACCESS_REMOTE_READ) {
501 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ |
502 NES_CQP_STAG_RIGHTS_LOCAL_READ | NES_CQP_STAG_REM_ACC_EN;
503 nesfmr->access_rights |=
504 NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_RIGHTS_LOCAL_READ |
505 NES_CQP_STAG_REM_ACC_EN;
508 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
509 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
510 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
511 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
513 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX] =
514 cpu_to_le32((nesfmr->nesmr.pbls_used>1) ?
515 (nesfmr->nesmr.pbls_used-1) : nesfmr->nesmr.pbls_used);
517 atomic_set(&cqp_request->refcount, 2);
518 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
521 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
523 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
524 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
525 stag, ret, cqp_request->major_code, cqp_request->minor_code);
527 if ((!ret) || (cqp_request->major_code)) {
528 if (atomic_dec_and_test(&cqp_request->refcount)) {
529 if (cqp_request->dynamic) {
532 spin_lock_irqsave(&nesdev->cqp.lock, flags);
533 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
534 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
537 ret = (!ret) ? -ETIME : -EIO;
538 goto failed_leaf_vpbl_pages_alloc;
540 if (atomic_dec_and_test(&cqp_request->refcount)) {
541 if (cqp_request->dynamic) {
544 spin_lock_irqsave(&nesdev->cqp.lock, flags);
545 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
546 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
551 nesfmr->nesmr.ibfmr.lkey = stag;
552 nesfmr->nesmr.ibfmr.rkey = stag;
553 nesfmr->attr = *ibfmr_attr;
555 return &nesfmr->nesmr.ibfmr;
557 failed_leaf_vpbl_pages_alloc:
558 /* unroll all allocated pages */
559 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
560 if (nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase) {
561 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
562 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
565 if (nesfmr->root_vpbl.leaf_vpbl)
566 kfree(nesfmr->root_vpbl.leaf_vpbl);
568 failed_leaf_vpbl_alloc:
569 if (nesfmr->leaf_pbl_cnt == 0) {
570 if (nesfmr->root_vpbl.pbl_vbase)
571 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
572 nesfmr->root_vpbl.pbl_pbase);
574 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
575 nesfmr->root_vpbl.pbl_pbase);
581 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
583 failed_resource_alloc:
591 static int nes_dealloc_fmr(struct ib_fmr *ibfmr)
593 struct nes_mr *nesmr = to_nesmr_from_ibfmr(ibfmr);
594 struct nes_fmr *nesfmr = to_nesfmr(nesmr);
595 struct nes_vnic *nesvnic = to_nesvnic(ibfmr->device);
596 struct nes_device *nesdev = nesvnic->nesdev;
597 struct nes_mr temp_nesmr = *nesmr;
600 temp_nesmr.ibmw.device = ibfmr->device;
601 temp_nesmr.ibmw.pd = ibfmr->pd;
602 temp_nesmr.ibmw.rkey = ibfmr->rkey;
603 temp_nesmr.ibmw.uobject = NULL;
605 /* free the resources */
606 if (nesfmr->leaf_pbl_cnt == 0) {
607 /* single PBL case */
608 if (nesfmr->root_vpbl.pbl_vbase)
609 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
610 nesfmr->root_vpbl.pbl_pbase);
612 for (i = 0; i < nesfmr->leaf_pbl_cnt; i++) {
613 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
614 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
616 kfree(nesfmr->root_vpbl.leaf_vpbl);
617 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
618 nesfmr->root_vpbl.pbl_pbase);
621 return nes_dealloc_mw(&temp_nesmr.ibmw);
628 static int nes_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
629 int list_len, u64 iova)
638 static int nes_unmap_fmr(struct list_head *ibfmr_list)
648 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
650 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
651 struct nes_device *nesdev = nesvnic->nesdev;
652 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
654 memset(props, 0, sizeof(*props));
655 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
657 props->fw_ver = nesdev->nesadapter->fw_ver;
658 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
659 props->vendor_id = nesdev->nesadapter->vendor_id;
660 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
661 props->hw_ver = nesdev->nesadapter->hw_rev;
662 props->max_mr_size = 0x80000000;
663 props->max_qp = nesibdev->max_qp;
664 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
665 props->max_sge = nesdev->nesadapter->max_sge;
666 props->max_cq = nesibdev->max_cq;
667 props->max_cqe = nesdev->nesadapter->max_cqe - 1;
668 props->max_mr = nesibdev->max_mr;
669 props->max_mw = nesibdev->max_mr;
670 props->max_pd = nesibdev->max_pd;
671 props->max_sge_rd = 1;
672 switch (nesdev->nesadapter->max_irrq_wr) {
674 props->max_qp_rd_atom = 1;
677 props->max_qp_rd_atom = 4;
680 props->max_qp_rd_atom = 16;
683 props->max_qp_rd_atom = 32;
686 props->max_qp_rd_atom = 0;
688 props->max_qp_init_rd_atom = props->max_qp_wr;
689 props->atomic_cap = IB_ATOMIC_NONE;
690 props->max_map_per_fmr = 1;
699 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
701 memset(props, 0, sizeof(*props));
703 props->max_mtu = IB_MTU_2048;
704 props->active_mtu = IB_MTU_2048;
709 props->state = IB_PORT_ACTIVE;
710 props->phys_state = 0;
711 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
712 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
713 props->gid_tbl_len = 1;
714 props->pkey_tbl_len = 1;
715 props->qkey_viol_cntr = 0;
716 props->active_width = IB_WIDTH_4X;
717 props->active_speed = 1;
718 props->max_msg_sz = 0x80000000;
727 static int nes_modify_port(struct ib_device *ibdev, u8 port,
728 int port_modify_mask, struct ib_port_modify *props)
737 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
747 static int nes_query_gid(struct ib_device *ibdev, u8 port,
748 int index, union ib_gid *gid)
750 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
752 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
753 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
760 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
761 * of all objects associated with a particular user-mode client.
763 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
764 struct ib_udata *udata)
766 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
767 struct nes_device *nesdev = nesvnic->nesdev;
768 struct nes_adapter *nesadapter = nesdev->nesadapter;
769 struct nes_alloc_ucontext_req req;
770 struct nes_alloc_ucontext_resp uresp;
771 struct nes_ucontext *nes_ucontext;
772 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
775 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
776 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
777 return ERR_PTR(-EINVAL);
780 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
781 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
782 req.userspace_ver, NES_ABI_USERSPACE_VER);
783 return ERR_PTR(-EINVAL);
787 memset(&uresp, 0, sizeof uresp);
789 uresp.max_qps = nesibdev->max_qp;
790 uresp.max_pds = nesibdev->max_pd;
791 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
792 uresp.virtwq = nesadapter->virtwq;
793 uresp.kernel_ver = NES_ABI_KERNEL_VER;
795 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
797 return ERR_PTR(-ENOMEM);
799 nes_ucontext->nesdev = nesdev;
800 nes_ucontext->mmap_wq_offset = uresp.max_pds;
801 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
802 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
806 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
808 return ERR_PTR(-EFAULT);
811 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
812 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
813 atomic_set(&nes_ucontext->usecnt, 1);
814 return &nes_ucontext->ibucontext;
819 * nes_dealloc_ucontext
821 static int nes_dealloc_ucontext(struct ib_ucontext *context)
823 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
824 /* struct nes_device *nesdev = nesvnic->nesdev; */
825 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
827 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
837 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
840 struct nes_vnic *nesvnic = to_nesvnic(context->device);
841 struct nes_device *nesdev = nesvnic->nesdev;
842 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
843 struct nes_ucontext *nes_ucontext;
844 struct nes_qp *nesqp;
846 nes_ucontext = to_nesucontext(context);
849 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
850 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
851 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
852 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
853 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
854 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
857 nesqp = nes_ucontext->mmap_nesqp[index];
859 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
862 if (remap_pfn_range(vma, vma->vm_start,
863 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
864 vma->vm_end - vma->vm_start,
865 vma->vm_page_prot)) {
866 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
869 vma->vm_private_data = nesqp;
872 index = vma->vm_pgoff;
873 if (!test_bit(index, nes_ucontext->allocated_doorbells))
876 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
877 if (io_remap_pfn_range(vma, vma->vm_start,
878 (nesdev->doorbell_start +
879 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
880 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
882 vma->vm_private_data = nes_ucontext;
893 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
894 struct ib_ucontext *context, struct ib_udata *udata)
896 struct nes_pd *nespd;
897 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
898 struct nes_device *nesdev = nesvnic->nesdev;
899 struct nes_adapter *nesadapter = nesdev->nesadapter;
900 struct nes_ucontext *nesucontext;
901 struct nes_alloc_pd_resp uresp;
905 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
906 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
907 atomic_read(&nesvnic->netdev->refcnt));
909 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
910 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
915 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
917 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
918 return ERR_PTR(-ENOMEM);
921 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
922 nespd, nesvnic->nesibdev->ibdev.name);
924 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
927 nesucontext = to_nesucontext(context);
928 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
929 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
930 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
931 nespd->mmap_db_index, nespd->pd_id);
932 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
933 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
934 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
936 return ERR_PTR(-ENOMEM);
939 uresp.pd_id = nespd->pd_id;
940 uresp.mmap_db_index = nespd->mmap_db_index;
941 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
942 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
944 return ERR_PTR(-EFAULT);
947 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
948 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
949 nesucontext->first_free_db = nespd->mmap_db_index + 1;
952 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
960 static int nes_dealloc_pd(struct ib_pd *ibpd)
962 struct nes_ucontext *nesucontext;
963 struct nes_pd *nespd = to_nespd(ibpd);
964 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
965 struct nes_device *nesdev = nesvnic->nesdev;
966 struct nes_adapter *nesadapter = nesdev->nesadapter;
968 if ((ibpd->uobject) && (ibpd->uobject->context)) {
969 nesucontext = to_nesucontext(ibpd->uobject->context);
970 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
971 nespd->mmap_db_index);
972 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
973 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
974 if (nesucontext->first_free_db > nespd->mmap_db_index) {
975 nesucontext->first_free_db = nespd->mmap_db_index;
979 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
980 nespd->pd_id, nespd);
981 nes_free_resource(nesadapter, nesadapter->allocated_pds,
982 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
992 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
994 return ERR_PTR(-ENOSYS);
1001 static int nes_destroy_ah(struct ib_ah *ah)
1008 * nes_get_encoded_size
1010 static inline u8 nes_get_encoded_size(int *size)
1012 u8 encoded_size = 0;
1016 } else if (*size <= 128) {
1019 } else if (*size <= 512) {
1023 return (encoded_size);
1031 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
1032 struct nes_vnic *nesvnic, int sq_size, int rq_size)
1034 unsigned long flags;
1039 struct nes_device *nesdev = nesvnic->nesdev;
1040 struct nes_adapter *nesadapter = nesdev->nesadapter;
1045 pbl_entries = nespbl->pbl_size >> 3;
1046 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%p\n",
1047 nespbl->pbl_size, pbl_entries,
1048 (void *)nespbl->pbl_vbase,
1049 (void *)nespbl->pbl_pbase);
1050 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
1051 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
1052 /* the first pbl to be fro the rq_vbase... */
1053 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1054 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1055 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1056 if (!nespbl->page) {
1057 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
1062 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
1063 nesqp->page = nespbl->page;
1064 if (!nesqp->hwqp.sq_vbase) {
1065 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
1070 /* Now to get to sq.. we need to calculate how many */
1071 /* PBL entries were used by the rq.. */
1072 pbl += sq_pbl_entries;
1073 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1074 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
1075 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
1077 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%p rq_vbase=%p rq_pbase=%p\n",
1078 nesqp->hwqp.sq_vbase, (void *)nesqp->hwqp.sq_pbase,
1079 nesqp->hwqp.rq_vbase, (void *)nesqp->hwqp.rq_pbase);
1080 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1081 if (!nesadapter->free_256pbl) {
1082 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1084 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1085 kunmap(nesqp->page);
1089 nesadapter->free_256pbl--;
1090 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1092 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
1093 pblbuffer = nesqp->pbl_vbase;
1094 if (!nesqp->pbl_vbase) {
1095 /* memory allocated during nes_reg_user_mr() */
1096 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1099 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1100 nesadapter->free_256pbl++;
1101 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1102 kunmap(nesqp->page);
1105 memset(nesqp->pbl_vbase, 0, 256);
1106 /* fill in the page address in the pbl buffer.. */
1107 tpbl = pblbuffer + 16;
1108 pbl = (__le64 *)nespbl->pbl_vbase;
1109 while (sq_pbl_entries--)
1112 while (rq_pbl_entries--)
1115 /* done with memory allocated during nes_reg_user_mr() */
1116 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1120 nesqp->qp_mem_size =
1121 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1122 /* Round up to a multiple of a page */
1123 nesqp->qp_mem_size += PAGE_SIZE - 1;
1124 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1126 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1127 &nesqp->hwqp.q2_pbase);
1130 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1131 nesqp->pbl_vbase = NULL;
1132 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1133 nesadapter->free_256pbl++;
1134 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1135 kunmap(nesqp->page);
1138 nesqp->hwqp.q2_vbase = mem;
1140 memset(nesqp->hwqp.q2_vbase, 0, 256);
1141 nesqp->nesqp_context = mem;
1142 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1143 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1152 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1153 int sq_size, int rq_size)
1156 struct nes_device *nesdev = nesvnic->nesdev;
1158 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1159 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1160 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1161 256; /* this is Q2 */
1162 /* Round up to a multiple of a page */
1163 nesqp->qp_mem_size += PAGE_SIZE - 1;
1164 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1166 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1167 &nesqp->hwqp.sq_pbase);
1170 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1171 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1172 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1174 memset(mem, 0, nesqp->qp_mem_size);
1176 nesqp->hwqp.sq_vbase = mem;
1177 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1179 nesqp->hwqp.rq_vbase = mem;
1180 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1181 sizeof(struct nes_hw_qp_wqe) * sq_size;
1182 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1184 nesqp->hwqp.q2_vbase = mem;
1185 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1186 sizeof(struct nes_hw_qp_wqe) * rq_size;
1188 memset(nesqp->hwqp.q2_vbase, 0, 256);
1190 nesqp->nesqp_context = mem;
1191 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1192 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1198 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1200 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1201 struct nes_qp *nesqp, int virt_wqs)
1203 unsigned long flags;
1204 struct nes_adapter *nesadapter = nesdev->nesadapter;
1206 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1207 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1209 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1210 nesadapter->free_256pbl++;
1211 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1212 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1213 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1214 nesqp->pbl_vbase = NULL;
1215 kunmap(nesqp->page);
1223 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1224 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1228 struct nes_pd *nespd = to_nespd(ibpd);
1229 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1230 struct nes_device *nesdev = nesvnic->nesdev;
1231 struct nes_adapter *nesadapter = nesdev->nesadapter;
1232 struct nes_qp *nesqp;
1233 struct nes_cq *nescq;
1234 struct nes_ucontext *nes_ucontext;
1235 struct nes_hw_cqp_wqe *cqp_wqe;
1236 struct nes_cqp_request *cqp_request;
1237 struct nes_create_qp_req req;
1238 struct nes_create_qp_resp uresp;
1239 struct nes_pbl *nespbl = NULL;
1242 /* u32 counter = 0; */
1244 unsigned long flags;
1254 atomic_inc(&qps_created);
1255 switch (init_attr->qp_type) {
1257 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1258 init_attr->cap.max_inline_data = 0;
1260 init_attr->cap.max_inline_data = 64;
1262 sq_size = init_attr->cap.max_send_wr;
1263 rq_size = init_attr->cap.max_recv_wr;
1265 // check if the encoded sizes are OK or not...
1266 sq_encoded_size = nes_get_encoded_size(&sq_size);
1267 rq_encoded_size = nes_get_encoded_size(&rq_size);
1269 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1270 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1272 return ERR_PTR(-EINVAL);
1275 init_attr->cap.max_send_wr = sq_size -2;
1276 init_attr->cap.max_recv_wr = rq_size -1;
1277 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1279 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1280 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1282 return ERR_PTR(ret);
1285 /* Need 512 (actually now 1024) byte alignment on this structure */
1286 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1288 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1289 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1290 return ERR_PTR(-ENOMEM);
1292 u64nesqp = (unsigned long)mem;
1293 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1294 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1295 u64nesqp &= ~u64temp;
1296 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1297 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1298 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1299 nesqp->allocated_buffer = mem;
1302 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1303 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1304 kfree(nesqp->allocated_buffer);
1305 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1308 if (req.user_wqe_buffers) {
1311 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1312 nesqp->user_mode = 1;
1313 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1316 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1317 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1318 list_del(&nespbl->list);
1320 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1321 nespbl, nespbl->user_base);
1326 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1327 (long long unsigned int)req.user_wqe_buffers);
1328 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1329 kfree(nesqp->allocated_buffer);
1330 return ERR_PTR(-EFAULT);
1334 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1335 nesqp->mmap_sq_db_index =
1336 find_next_zero_bit(nes_ucontext->allocated_wqs,
1337 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1338 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1339 nespd->mmap_db_index); */
1340 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1341 nes_debug(NES_DBG_QP,
1342 "db index > max user regions, failing create QP\n");
1343 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1345 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1349 kfree(nesqp->allocated_buffer);
1350 return ERR_PTR(-ENOMEM);
1352 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1353 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1354 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1356 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1357 kfree(nesqp->allocated_buffer);
1358 return ERR_PTR(-EFAULT);
1361 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1362 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1364 nes_debug(NES_DBG_QP,
1365 "error geting qp mem code = %d\n", err);
1366 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1367 kfree(nesqp->allocated_buffer);
1368 return ERR_PTR(-ENOMEM);
1371 nesqp->hwqp.sq_size = sq_size;
1372 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1373 nesqp->hwqp.sq_head = 1;
1374 nesqp->hwqp.rq_size = rq_size;
1375 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1376 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1377 (void *)nesqp->nesqp_context_pbase);
1379 nesqp->hwqp.qp_id = qp_num;
1380 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1381 nesqp->nespd = nespd;
1383 nescq = to_nescq(init_attr->send_cq);
1384 nesqp->nesscq = nescq;
1385 nescq = to_nescq(init_attr->recv_cq);
1386 nesqp->nesrcq = nescq;
1388 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1389 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1390 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1391 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1392 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1393 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1394 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1395 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1396 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1397 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1398 u64temp = (u64)nesqp->hwqp.sq_pbase;
1399 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1400 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1404 u64temp = (u64)nesqp->hwqp.sq_pbase;
1405 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1406 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1407 u64temp = (u64)nesqp->hwqp.rq_pbase;
1408 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1409 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1411 u64temp = (u64)nesqp->pbl_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));
1416 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1417 nesvnic->next_qp_nic_index,
1418 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1419 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1420 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1421 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1422 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1423 nesvnic->next_qp_nic_index++;
1424 if ((nesvnic->next_qp_nic_index > 3) ||
1425 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1426 nesvnic->next_qp_nic_index = 0;
1428 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1430 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1431 u64temp = (u64)nesqp->hwqp.q2_pbase;
1432 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1433 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1434 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1435 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1436 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1437 ((((u32)nesadapter->max_irrq_wr) <<
1438 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1439 if (disable_mpa_crc) {
1440 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1441 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1446 cqp_request = nes_get_cqp_request(nesdev);
1447 if (cqp_request == NULL) {
1448 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1449 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1450 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1451 kfree(nesqp->allocated_buffer);
1452 return ERR_PTR(-ENOMEM);
1454 cqp_request->waiting = 1;
1455 cqp_wqe = &cqp_request->cqp_wqe;
1458 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1459 NES_CQP_QP_IWARP_STATE_IDLE;
1461 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1462 NES_CQP_QP_IWARP_STATE_IDLE;
1464 opcode |= NES_CQP_QP_CQS_VALID;
1465 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1466 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1467 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1469 u64temp = (u64)nesqp->nesqp_context_pbase;
1470 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1472 atomic_set(&cqp_request->refcount, 2);
1473 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1476 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1478 ret = wait_event_timeout(cqp_request->waitq,
1479 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1480 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1481 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1482 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1483 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1484 cqp_request->major_code, cqp_request->minor_code);
1485 if ((!ret) || (cqp_request->major_code)) {
1486 if (atomic_dec_and_test(&cqp_request->refcount)) {
1487 if (cqp_request->dynamic) {
1490 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1491 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1492 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1495 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1496 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1497 kfree(nesqp->allocated_buffer);
1499 return ERR_PTR(-ETIME);
1501 return ERR_PTR(-EIO);
1504 if (atomic_dec_and_test(&cqp_request->refcount)) {
1505 if (cqp_request->dynamic) {
1508 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1509 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1510 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1515 if (ibpd->uobject) {
1516 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1517 uresp.actual_sq_size = sq_size;
1518 uresp.actual_rq_size = rq_size;
1519 uresp.qp_id = nesqp->hwqp.qp_id;
1520 uresp.nes_drv_opt = nes_drv_opt;
1521 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1522 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1523 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1524 kfree(nesqp->allocated_buffer);
1525 return ERR_PTR(-EFAULT);
1529 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1530 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1531 spin_lock_init(&nesqp->lock);
1532 init_waitqueue_head(&nesqp->state_waitq);
1533 init_waitqueue_head(&nesqp->kick_waitq);
1534 nes_add_ref(&nesqp->ibqp);
1537 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1538 return ERR_PTR(-EINVAL);
1542 /* update the QP table */
1543 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1544 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1545 atomic_read(&nesvnic->netdev->refcnt));
1547 return &nesqp->ibqp;
1554 static int nes_destroy_qp(struct ib_qp *ibqp)
1556 struct nes_qp *nesqp = to_nesqp(ibqp);
1557 /* struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); */
1558 struct nes_ucontext *nes_ucontext;
1559 struct ib_qp_attr attr;
1560 struct iw_cm_id *cm_id;
1561 struct iw_cm_event cm_event;
1564 atomic_inc(&sw_qps_destroyed);
1565 nesqp->destroyed = 1;
1567 /* Blow away the connection if it exists. */
1568 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1569 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1570 attr.qp_state = IB_QPS_ERR;
1571 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1574 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1575 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1576 cm_id = nesqp->cm_id;
1577 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1578 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1579 cm_event.local_addr = cm_id->local_addr;
1580 cm_event.remote_addr = cm_id->remote_addr;
1581 cm_event.private_data = NULL;
1582 cm_event.private_data_len = 0;
1584 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1585 "QP%u. cm_id = %p, refcount = %u. \n",
1586 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1588 cm_id->rem_ref(cm_id);
1589 ret = cm_id->event_handler(cm_id, &cm_event);
1591 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1595 if (nesqp->user_mode) {
1596 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1597 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1598 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1599 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1600 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1601 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1604 if (nesqp->pbl_pbase)
1605 kunmap(nesqp->page);
1608 nes_rem_ref(&nesqp->ibqp);
1616 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1618 struct ib_ucontext *context, struct ib_udata *udata)
1621 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1622 struct nes_device *nesdev = nesvnic->nesdev;
1623 struct nes_adapter *nesadapter = nesdev->nesadapter;
1624 struct nes_cq *nescq;
1625 struct nes_ucontext *nes_ucontext = NULL;
1626 struct nes_cqp_request *cqp_request;
1628 struct nes_hw_cqp_wqe *cqp_wqe;
1629 struct nes_pbl *nespbl = NULL;
1630 struct nes_create_cq_req req;
1631 struct nes_create_cq_resp resp;
1634 u32 pbl_entries = 1;
1636 unsigned long flags;
1639 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1640 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1642 return ERR_PTR(err);
1645 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1647 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1648 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1649 return ERR_PTR(-ENOMEM);
1652 nescq->hw_cq.cq_size = max(entries + 1, 5);
1653 nescq->hw_cq.cq_number = cq_num;
1654 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1658 nes_ucontext = to_nesucontext(context);
1659 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1660 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1662 return ERR_PTR(-EFAULT);
1664 nesvnic->mcrq_ucontext = nes_ucontext;
1665 nes_ucontext->mcrqf = req.mcrqf;
1666 if (nes_ucontext->mcrqf) {
1667 if (nes_ucontext->mcrqf & 0x80000000)
1668 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 12 + (nes_ucontext->mcrqf & 0xf) - 1;
1669 else if (nes_ucontext->mcrqf & 0x40000000)
1670 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1672 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1673 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1675 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1676 (unsigned long)req.user_cq_buffer, entries);
1678 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1679 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1680 list_del(&nespbl->list);
1682 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1688 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1690 return ERR_PTR(-EFAULT);
1693 pbl_entries = nespbl->pbl_size >> 3;
1694 nescq->cq_mem_size = 0;
1696 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1697 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1698 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1700 /* allocate the physical buffer space */
1701 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1702 &nescq->hw_cq.cq_pbase);
1704 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1705 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1707 return ERR_PTR(-ENOMEM);
1710 memset(mem, 0, nescq->cq_mem_size);
1711 nescq->hw_cq.cq_vbase = mem;
1712 nescq->hw_cq.cq_head = 0;
1713 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1714 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1715 (u32)nescq->hw_cq.cq_pbase);
1718 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1719 spin_lock_init(&nescq->lock);
1721 /* send CreateCQ request to CQP */
1722 cqp_request = nes_get_cqp_request(nesdev);
1723 if (cqp_request == NULL) {
1724 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1726 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1727 nescq->hw_cq.cq_pbase);
1728 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1730 return ERR_PTR(-ENOMEM);
1732 cqp_request->waiting = 1;
1733 cqp_wqe = &cqp_request->cqp_wqe;
1735 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1736 NES_CQP_CQ_CHK_OVERFLOW |
1737 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1739 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1741 if (pbl_entries != 1) {
1742 if (pbl_entries > 32) {
1744 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1745 if (nesadapter->free_4kpbl == 0) {
1746 if (cqp_request->dynamic) {
1747 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1750 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1751 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1754 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1755 nescq->hw_cq.cq_pbase);
1756 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1758 return ERR_PTR(-ENOMEM);
1760 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1761 nescq->virtual_cq = 2;
1762 nesadapter->free_4kpbl--;
1765 /* use 256 byte pbl */
1766 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1767 if (nesadapter->free_256pbl == 0) {
1768 if (cqp_request->dynamic) {
1769 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1772 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1773 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1776 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1777 nescq->hw_cq.cq_pbase);
1778 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1780 return ERR_PTR(-ENOMEM);
1782 opcode |= NES_CQP_CQ_VIRT;
1783 nescq->virtual_cq = 1;
1784 nesadapter->free_256pbl--;
1789 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1791 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1792 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1793 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1794 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1797 if (pbl_entries != 1)
1798 u64temp = (u64)nespbl->pbl_pbase;
1800 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1801 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1802 nes_ucontext->mmap_db_index[0]);
1804 u64temp = (u64)nescq->hw_cq.cq_pbase;
1805 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1807 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1808 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1809 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1810 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1811 cpu_to_le32((u32)(u64temp >> 1));
1812 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1813 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1815 atomic_set(&cqp_request->refcount, 2);
1816 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1819 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1820 nescq->hw_cq.cq_number);
1821 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1822 NES_EVENT_TIMEOUT * 2);
1823 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1824 nescq->hw_cq.cq_number, ret);
1825 if ((!ret) || (cqp_request->major_code)) {
1826 if (atomic_dec_and_test(&cqp_request->refcount)) {
1827 if (cqp_request->dynamic) {
1830 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1831 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1832 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1836 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1837 nescq->hw_cq.cq_pbase);
1838 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1840 return ERR_PTR(-EIO);
1842 if (atomic_dec_and_test(&cqp_request->refcount)) {
1843 if (cqp_request->dynamic) {
1846 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1847 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1848 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1854 /* free the nespbl */
1855 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1858 resp.cq_id = nescq->hw_cq.cq_number;
1859 resp.cq_size = nescq->hw_cq.cq_size;
1860 resp.mmap_db_index = 0;
1861 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1862 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1864 return ERR_PTR(-EFAULT);
1868 return &nescq->ibcq;
1875 static int nes_destroy_cq(struct ib_cq *ib_cq)
1877 struct nes_cq *nescq;
1878 struct nes_device *nesdev;
1879 struct nes_vnic *nesvnic;
1880 struct nes_adapter *nesadapter;
1881 struct nes_hw_cqp_wqe *cqp_wqe;
1882 struct nes_cqp_request *cqp_request;
1883 unsigned long flags;
1890 nescq = to_nescq(ib_cq);
1891 nesvnic = to_nesvnic(ib_cq->device);
1892 nesdev = nesvnic->nesdev;
1893 nesadapter = nesdev->nesadapter;
1895 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1897 /* Send DestroyCQ request to CQP */
1898 cqp_request = nes_get_cqp_request(nesdev);
1899 if (cqp_request == NULL) {
1900 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1903 cqp_request->waiting = 1;
1904 cqp_wqe = &cqp_request->cqp_wqe;
1905 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1906 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1907 if (nescq->virtual_cq == 1) {
1908 nesadapter->free_256pbl++;
1909 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1910 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1911 __FUNCTION__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1913 } else if (nescq->virtual_cq == 2) {
1914 nesadapter->free_4kpbl++;
1915 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1916 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1917 __FUNCTION__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1919 opcode |= NES_CQP_CQ_4KB_CHUNK;
1922 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1924 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1925 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1926 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1927 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1928 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1929 atomic_set(&cqp_request->refcount, 2);
1930 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
1933 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1934 nescq->hw_cq.cq_number);
1935 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1937 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1938 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1939 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1940 cqp_request->minor_code);
1941 if ((!ret) || (cqp_request->major_code)) {
1942 if (atomic_dec_and_test(&cqp_request->refcount)) {
1943 if (cqp_request->dynamic) {
1946 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1947 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1948 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1952 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1953 nescq->hw_cq.cq_number);
1956 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1957 nescq->hw_cq.cq_number);
1962 if (atomic_dec_and_test(&cqp_request->refcount)) {
1963 if (cqp_request->dynamic) {
1966 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1967 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1968 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1973 if (nescq->cq_mem_size)
1974 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1975 (void *)nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1985 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1986 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1987 dma_addr_t single_buffer, u16 pbl_count, u16 residual_page_count,
1988 int acc, u64 *iova_start)
1990 struct nes_hw_cqp_wqe *cqp_wqe;
1991 struct nes_cqp_request *cqp_request;
1992 unsigned long flags;
1994 struct nes_adapter *nesadapter = nesdev->nesadapter;
1999 /* Register the region with the adapter */
2000 cqp_request = nes_get_cqp_request(nesdev);
2001 if (cqp_request == NULL) {
2002 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2005 cqp_request->waiting = 1;
2006 cqp_wqe = &cqp_request->cqp_wqe;
2008 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2009 /* track PBL resources */
2010 if (pbl_count != 0) {
2011 if (pbl_count > 1) {
2013 if ((pbl_count+1) > nesadapter->free_4kpbl) {
2014 nes_debug(NES_DBG_MR, "Out of 4KB Pbls for two level request.\n");
2015 if (cqp_request->dynamic) {
2016 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2019 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2020 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2024 nesadapter->free_4kpbl -= pbl_count+1;
2026 } else if (residual_page_count > 32) {
2027 if (pbl_count > nesadapter->free_4kpbl) {
2028 nes_debug(NES_DBG_MR, "Out of 4KB Pbls.\n");
2029 if (cqp_request->dynamic) {
2030 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2033 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2034 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2038 nesadapter->free_4kpbl -= pbl_count;
2041 if (pbl_count > nesadapter->free_256pbl) {
2042 nes_debug(NES_DBG_MR, "Out of 256B Pbls.\n");
2043 if (cqp_request->dynamic) {
2044 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2047 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2048 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2052 nesadapter->free_256pbl -= pbl_count;
2057 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2059 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2060 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2061 if (acc & IB_ACCESS_LOCAL_WRITE)
2062 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2063 if (acc & IB_ACCESS_REMOTE_WRITE)
2064 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2065 if (acc & IB_ACCESS_REMOTE_READ)
2066 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2067 if (acc & IB_ACCESS_MW_BIND)
2068 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2070 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2071 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2072 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2073 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2075 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2076 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2077 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2078 cpu_to_le32(nespd->pd_id & 0x00007fff);
2079 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2081 if (pbl_count == 0) {
2082 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2084 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2085 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2086 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX,
2087 (((pbl_count - 1) * 4096) + (residual_page_count*8)));
2089 if ((pbl_count > 1) || (residual_page_count > 32))
2090 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2094 atomic_set(&cqp_request->refcount, 2);
2095 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2098 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2100 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2101 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2102 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2103 major_code = cqp_request->major_code;
2104 if (atomic_dec_and_test(&cqp_request->refcount)) {
2105 if (cqp_request->dynamic) {
2108 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2109 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2110 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2115 else if (major_code)
2127 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2128 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2132 struct nes_pd *nespd = to_nespd(ib_pd);
2133 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2134 struct nes_device *nesdev = nesvnic->nesdev;
2135 struct nes_adapter *nesadapter = nesdev->nesadapter;
2136 struct nes_mr *nesmr;
2138 struct nes_vpbl vpbl;
2139 struct nes_root_vpbl root_vpbl;
2143 u32 next_stag_index = 0;
2145 u32 root_pbl_index = 0;
2146 u32 cur_pbl_index = 0;
2147 int err = 0, pbl_depth = 0;
2155 vpbl.pbl_vbase = NULL;
2156 root_vpbl.pbl_vbase = NULL;
2157 root_vpbl.pbl_pbase = 0;
2159 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2160 stag_key = (u8)next_stag_index;
2164 next_stag_index >>= 8;
2165 next_stag_index %= nesadapter->max_mr;
2166 if (num_phys_buf > (1024*512)) {
2167 return ERR_PTR(-E2BIG);
2170 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2171 &stag_index, &next_stag_index);
2173 return ERR_PTR(err);
2176 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2178 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2179 return ERR_PTR(-ENOMEM);
2182 for (i = 0; i < num_phys_buf; i++) {
2184 if ((i & 0x01FF) == 0) {
2185 if (root_pbl_index == 1) {
2186 /* Allocate the root PBL */
2187 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2188 &root_vpbl.pbl_pbase);
2189 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2190 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2191 if (!root_vpbl.pbl_vbase) {
2192 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2194 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2196 return ERR_PTR(-ENOMEM);
2198 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2199 if (!root_vpbl.leaf_vpbl) {
2200 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2201 root_vpbl.pbl_pbase);
2202 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2204 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2206 return ERR_PTR(-ENOMEM);
2208 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2209 root_vpbl.pbl_vbase[0].pa_high =
2210 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2211 root_vpbl.leaf_vpbl[0] = vpbl;
2213 /* Allocate a 4K buffer for the PBL */
2214 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2216 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2217 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2218 if (!vpbl.pbl_vbase) {
2219 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2220 ibmr = ERR_PTR(-ENOMEM);
2224 /* Fill in the root table */
2225 if (1 <= root_pbl_index) {
2226 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2227 cpu_to_le32((u32)vpbl.pbl_pbase);
2228 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2229 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2230 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2235 if (buffer_list[i].addr & ~PAGE_MASK) {
2236 /* TODO: Unwind allocated buffers */
2237 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2238 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2239 (unsigned int) buffer_list[i].addr);
2240 ibmr = ERR_PTR(-EINVAL);
2245 if (!buffer_list[i].size) {
2246 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2247 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2248 ibmr = ERR_PTR(-EINVAL);
2253 region_length += buffer_list[i].size;
2254 if ((i != 0) && (single_page)) {
2255 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2258 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr);
2259 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2260 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2263 stag = stag_index << 8;
2265 stag += (u32)stag_key;
2267 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2268 " length = 0x%016lX, index = 0x%08X\n",
2269 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2271 region_length -= (*iova_start)&PAGE_MASK;
2273 /* Make the leaf PBL the root if only one PBL */
2274 if (root_pbl_index == 1) {
2275 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2281 pbl_count = root_pbl_index;
2283 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2284 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start);
2287 nesmr->ibmr.rkey = stag;
2288 nesmr->ibmr.lkey = stag;
2289 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2290 ibmr = &nesmr->ibmr;
2291 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2292 nesmr->pbls_used = pbl_count;
2293 if (pbl_count > 1) {
2298 ibmr = ERR_PTR(-ENOMEM);
2302 /* free the resources */
2303 if (root_pbl_index == 1) {
2304 /* single PBL case */
2305 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2307 for (i=0; i<root_pbl_index; i++) {
2308 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2309 root_vpbl.leaf_vpbl[i].pbl_pbase);
2311 kfree(root_vpbl.leaf_vpbl);
2312 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2313 root_vpbl.pbl_pbase);
2323 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2325 struct ib_phys_buf bl;
2328 nes_debug(NES_DBG_MR, "\n");
2330 bl.size = (u64)0xffffffffffULL;
2332 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2339 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2340 u64 virt, int acc, struct ib_udata *udata)
2345 dma_addr_t last_dma_addr = 0;
2346 dma_addr_t first_dma_addr = 0;
2347 struct nes_pd *nespd = to_nespd(pd);
2348 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2349 struct nes_device *nesdev = nesvnic->nesdev;
2350 struct nes_adapter *nesadapter = nesdev->nesadapter;
2351 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2352 struct ib_umem_chunk *chunk;
2353 struct nes_ucontext *nes_ucontext;
2354 struct nes_pbl *nespbl;
2355 struct nes_mr *nesmr;
2356 struct ib_umem *region;
2357 struct nes_mem_reg_req req;
2358 struct nes_vpbl vpbl;
2359 struct nes_root_vpbl root_vpbl;
2360 int nmap_index, page_index;
2362 int err, pbl_depth = 0;
2367 u32 next_stag_index;
2369 u32 root_pbl_index = 0;
2370 u32 cur_pbl_index = 0;
2376 region = ib_umem_get(pd->uobject->context, start, length, acc);
2377 if (IS_ERR(region)) {
2378 return (struct ib_mr *)region;
2381 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2382 " offset = %u, page size = %u.\n",
2383 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2384 region->offset, region->page_size);
2386 skip_pages = ((u32)region->offset) >> 12;
2388 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2389 return ERR_PTR(-EFAULT);
2390 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2392 switch (req.reg_type) {
2393 case IWNES_MEMREG_TYPE_MEM:
2396 vpbl.pbl_vbase = NULL;
2397 root_vpbl.pbl_vbase = NULL;
2398 root_vpbl.pbl_pbase = 0;
2400 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2401 stag_key = (u8)next_stag_index;
2403 driver_key = next_stag_index & 0x70000000;
2405 next_stag_index >>= 8;
2406 next_stag_index %= nesadapter->max_mr;
2408 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2409 nesadapter->max_mr, &stag_index, &next_stag_index);
2411 ib_umem_release(region);
2412 return ERR_PTR(err);
2415 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2417 ib_umem_release(region);
2418 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2419 return ERR_PTR(-ENOMEM);
2421 nesmr->region = region;
2423 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2424 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2425 chunk->nents, chunk->nmap);
2426 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2427 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2428 ib_umem_release(region);
2429 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2430 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2431 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2432 ibmr = ERR_PTR(-EINVAL);
2434 goto reg_user_mr_err;
2437 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2438 ib_umem_release(region);
2439 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2441 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2442 ibmr = ERR_PTR(-EINVAL);
2444 goto reg_user_mr_err;
2447 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2448 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2449 region_length -= skip_pages << 12;
2450 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2452 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2454 if ((page_count&0x01FF) == 0) {
2455 if (page_count>(1024*512)) {
2456 ib_umem_release(region);
2457 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2459 nes_free_resource(nesadapter,
2460 nesadapter->allocated_mrs, stag_index);
2462 ibmr = ERR_PTR(-E2BIG);
2463 goto reg_user_mr_err;
2465 if (root_pbl_index == 1) {
2466 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2467 8192, &root_vpbl.pbl_pbase);
2468 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2469 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2470 if (!root_vpbl.pbl_vbase) {
2471 ib_umem_release(region);
2472 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2474 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2477 ibmr = ERR_PTR(-ENOMEM);
2478 goto reg_user_mr_err;
2480 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2482 if (!root_vpbl.leaf_vpbl) {
2483 ib_umem_release(region);
2484 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2485 root_vpbl.pbl_pbase);
2486 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2488 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2491 ibmr = ERR_PTR(-ENOMEM);
2492 goto reg_user_mr_err;
2494 root_vpbl.pbl_vbase[0].pa_low =
2495 cpu_to_le32((u32)vpbl.pbl_pbase);
2496 root_vpbl.pbl_vbase[0].pa_high =
2497 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2498 root_vpbl.leaf_vpbl[0] = vpbl;
2500 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2502 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2503 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2504 if (!vpbl.pbl_vbase) {
2505 ib_umem_release(region);
2506 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2507 ibmr = ERR_PTR(-ENOMEM);
2509 goto reg_user_mr_err;
2511 if (1 <= root_pbl_index) {
2512 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2513 cpu_to_le32((u32)vpbl.pbl_pbase);
2514 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2515 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2516 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2522 if (page_count != 0) {
2523 if ((last_dma_addr+4096) !=
2524 (sg_dma_address(&chunk->page_list[nmap_index])+
2527 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2530 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2532 last_dma_addr = first_dma_addr;
2536 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2537 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2538 (page_index*4096)));
2539 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2540 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2541 (page_index*4096))) >> 32)));
2548 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2549 " stag_key=0x%08x\n",
2550 stag_index, driver_key, stag_key);
2551 stag = stag_index << 8;
2553 stag += (u32)stag_key;
2559 /* Make the leaf PBL the root if only one PBL */
2560 if (root_pbl_index == 1) {
2561 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2567 pbl_count = root_pbl_index;
2570 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2571 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2572 stag, (unsigned int)iova_start,
2573 (unsigned int)region_length, stag_index,
2574 (unsigned long long)region->length, pbl_count);
2575 ret = nes_reg_mr( nesdev, nespd, stag, region->length, &root_vpbl,
2576 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc, &iova_start);
2578 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2581 nesmr->ibmr.rkey = stag;
2582 nesmr->ibmr.lkey = stag;
2583 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2584 ibmr = &nesmr->ibmr;
2585 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2586 nesmr->pbls_used = pbl_count;
2587 if (pbl_count > 1) {
2591 ib_umem_release(region);
2593 ibmr = ERR_PTR(-ENOMEM);
2597 /* free the resources */
2598 if (root_pbl_index == 1) {
2599 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2602 for (page_index=0; page_index<root_pbl_index; page_index++) {
2603 pci_free_consistent(nesdev->pcidev, 4096,
2604 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2605 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2607 kfree(root_vpbl.leaf_vpbl);
2608 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2609 root_vpbl.pbl_pbase);
2612 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2616 case IWNES_MEMREG_TYPE_QP:
2617 case IWNES_MEMREG_TYPE_CQ:
2618 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2620 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2621 ib_umem_release(region);
2622 return ERR_PTR(-ENOMEM);
2624 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2626 ib_umem_release(region);
2628 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2629 return ERR_PTR(-ENOMEM);
2631 nesmr->region = region;
2632 nes_ucontext = to_nesucontext(pd->uobject->context);
2633 pbl_depth = region->length >> 12;
2634 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2635 nespbl->pbl_size = pbl_depth*sizeof(u64);
2636 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2637 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2639 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2642 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2643 nespbl->pbl_size, pbl_depth);
2644 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2645 &nespbl->pbl_pbase);
2647 ib_umem_release(region);
2650 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2651 return ERR_PTR(-ENOMEM);
2654 nespbl->pbl_vbase = (u64 *)pbl;
2655 nespbl->user_base = start;
2656 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%p,"
2657 " pbl_vbase=%p user_base=0x%lx\n",
2658 nespbl->pbl_size, (void *)nespbl->pbl_pbase,
2659 (void*)nespbl->pbl_vbase, nespbl->user_base);
2661 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2662 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2663 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2664 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2665 nespbl->page = sg_page(&chunk->page_list[0]);
2666 for (page_index=0; page_index<chunk_pages; page_index++) {
2667 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2668 (sg_dma_address(&chunk->page_list[nmap_index])+
2669 (page_index*4096)));
2670 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2671 (sg_dma_address(&chunk->page_list[nmap_index])+
2672 (page_index*4096)))>>32);
2673 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2674 (unsigned long long)*pbl,
2675 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2680 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2681 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2683 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2685 nesmr->ibmr.rkey = -1;
2686 nesmr->ibmr.lkey = -1;
2687 nesmr->mode = req.reg_type;
2688 return &nesmr->ibmr;
2692 return ERR_PTR(-ENOSYS);
2699 static int nes_dereg_mr(struct ib_mr *ib_mr)
2701 struct nes_mr *nesmr = to_nesmr(ib_mr);
2702 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2703 struct nes_device *nesdev = nesvnic->nesdev;
2704 struct nes_adapter *nesadapter = nesdev->nesadapter;
2705 struct nes_hw_cqp_wqe *cqp_wqe;
2706 struct nes_cqp_request *cqp_request;
2707 unsigned long flags;
2712 if (nesmr->region) {
2713 ib_umem_release(nesmr->region);
2715 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2720 /* Deallocate the region with the adapter */
2722 cqp_request = nes_get_cqp_request(nesdev);
2723 if (cqp_request == NULL) {
2724 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2727 cqp_request->waiting = 1;
2728 cqp_wqe = &cqp_request->cqp_wqe;
2730 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2731 if (nesmr->pbls_used != 0) {
2732 if (nesmr->pbl_4k) {
2733 nesadapter->free_4kpbl += nesmr->pbls_used;
2734 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
2735 printk(KERN_ERR PFX "free 4KB PBLs(%u) has exceeded the max(%u)\n",
2736 nesadapter->free_4kpbl, nesadapter->max_4kpbl);
2739 nesadapter->free_256pbl += nesmr->pbls_used;
2740 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
2741 printk(KERN_ERR PFX "free 256B PBLs(%u) has exceeded the max(%u)\n",
2742 nesadapter->free_256pbl, nesadapter->max_256pbl);
2747 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2748 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2749 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2750 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2751 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2752 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2754 atomic_set(&cqp_request->refcount, 2);
2755 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2758 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2759 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2761 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2762 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2763 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2765 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2766 (ib_mr->rkey & 0x0fffff00) >> 8);
2770 major_code = cqp_request->major_code;
2771 minor_code = cqp_request->minor_code;
2772 if (atomic_dec_and_test(&cqp_request->refcount)) {
2773 if (cqp_request->dynamic) {
2776 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2777 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2778 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2782 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2783 " ib_mr=%p, rkey = 0x%08X\n",
2784 ib_mr, ib_mr->rkey);
2786 } else if (major_code) {
2787 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2788 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2789 major_code, minor_code, ib_mr, ib_mr->rkey);
2799 static ssize_t show_rev(struct class_device *cdev, char *buf)
2801 struct nes_ib_device *nesibdev =
2802 container_of(cdev, struct nes_ib_device, ibdev.class_dev);
2803 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2805 nes_debug(NES_DBG_INIT, "\n");
2806 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2813 static ssize_t show_fw_ver(struct class_device *cdev, char *buf)
2815 struct nes_ib_device *nesibdev =
2816 container_of(cdev, struct nes_ib_device, ibdev.class_dev);
2817 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2819 nes_debug(NES_DBG_INIT, "\n");
2820 return sprintf(buf, "%x.%x.%x\n",
2821 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 32),
2822 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 16) & 0xffff,
2823 (int)(nesvnic->nesdev->nesadapter->fw_ver & 0xffff));
2830 static ssize_t show_hca(struct class_device *cdev, char *buf)
2832 nes_debug(NES_DBG_INIT, "\n");
2833 return sprintf(buf, "NES020\n");
2840 static ssize_t show_board(struct class_device *cdev, char *buf)
2842 nes_debug(NES_DBG_INIT, "\n");
2843 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2847 static CLASS_DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2848 static CLASS_DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2849 static CLASS_DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2850 static CLASS_DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2852 static struct class_device_attribute *nes_class_attributes[] = {
2853 &class_device_attr_hw_rev,
2854 &class_device_attr_fw_ver,
2855 &class_device_attr_hca_type,
2856 &class_device_attr_board_id
2863 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2864 int attr_mask, struct ib_qp_init_attr *init_attr)
2866 struct nes_qp *nesqp = to_nesqp(ibqp);
2868 nes_debug(NES_DBG_QP, "\n");
2870 attr->qp_access_flags = 0;
2871 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2872 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2873 attr->cap.max_recv_sge = 1;
2874 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2875 init_attr->cap.max_inline_data = 0;
2877 init_attr->cap.max_inline_data = 64;
2880 init_attr->event_handler = nesqp->ibqp.event_handler;
2881 init_attr->qp_context = nesqp->ibqp.qp_context;
2882 init_attr->send_cq = nesqp->ibqp.send_cq;
2883 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2884 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2885 init_attr->cap = attr->cap;
2894 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2895 u32 next_iwarp_state, u32 wait_completion)
2897 struct nes_hw_cqp_wqe *cqp_wqe;
2898 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2899 /* struct iw_cm_event cm_event; */
2900 struct nes_cqp_request *cqp_request;
2901 unsigned long flags;
2905 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2906 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2908 cqp_request = nes_get_cqp_request(nesdev);
2909 if (cqp_request == NULL) {
2910 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2913 if (wait_completion) {
2914 cqp_request->waiting = 1;
2916 cqp_request->waiting = 0;
2918 cqp_wqe = &cqp_request->cqp_wqe;
2920 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2921 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2922 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2923 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2924 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2925 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2926 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2928 atomic_set(&cqp_request->refcount, 2);
2929 nes_post_cqp_request(nesdev, cqp_request, NES_CQP_REQUEST_RING_DOORBELL);
2932 if (wait_completion) {
2933 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2934 nesqp->hwqp.qp_id); */
2935 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2937 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2938 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2939 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2940 major_code = cqp_request->major_code;
2942 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2943 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2944 nesqp->hwqp.qp_id, cqp_request->major_code,
2945 cqp_request->minor_code, next_iwarp_state);
2947 if (atomic_dec_and_test(&cqp_request->refcount)) {
2948 if (cqp_request->dynamic) {
2951 spin_lock_irqsave(&nesdev->cqp.lock, flags);
2952 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
2953 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
2958 else if (major_code)
2971 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2972 int attr_mask, struct ib_udata *udata)
2974 struct nes_qp *nesqp = to_nesqp(ibqp);
2975 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2976 struct nes_device *nesdev = nesvnic->nesdev;
2979 u32 next_iwarp_state = 0;
2981 unsigned long qplockflags;
2983 u16 original_last_aeq;
2984 u8 issue_modify_qp = 0;
2985 u8 issue_disconnect = 0;
2988 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2989 " iwarp_state=0x%X, refcount=%d\n",
2990 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2991 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2993 nes_add_ref(&nesqp->ibqp);
2994 spin_lock_irqsave(&nesqp->lock, qplockflags);
2996 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2997 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2998 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2999 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
3001 if (attr_mask & IB_QP_STATE) {
3002 switch (attr->qp_state) {
3004 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
3006 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3007 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3008 nes_rem_ref(&nesqp->ibqp);
3011 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3012 issue_modify_qp = 1;
3015 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
3017 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3018 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3019 nes_rem_ref(&nesqp->ibqp);
3022 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3023 issue_modify_qp = 1;
3026 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
3028 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
3029 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3030 nes_rem_ref(&nesqp->ibqp);
3033 if (nesqp->cm_id == NULL) {
3034 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
3035 nesqp->hwqp.qp_id );
3036 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3037 nes_rem_ref(&nesqp->ibqp);
3040 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
3041 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
3042 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
3043 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
3044 issue_modify_qp = 1;
3045 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
3046 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
3047 nesqp->hte_added = 1;
3050 issue_modify_qp = 1;
3051 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3052 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3053 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3054 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3055 nes_rem_ref(&nesqp->ibqp);
3058 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3059 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3060 " ignored due to current iWARP state\n",
3062 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3063 nes_rem_ref(&nesqp->ibqp);
3066 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3067 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3068 " already done based on hw state.\n",
3070 issue_modify_qp = 0;
3071 nesqp->in_disconnect = 0;
3073 switch (nesqp->hw_iwarp_state) {
3074 case NES_AEQE_IWARP_STATE_CLOSING:
3075 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3076 case NES_AEQE_IWARP_STATE_TERMINATE:
3077 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3079 case NES_AEQE_IWARP_STATE_ERROR:
3080 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3083 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3084 nesqp->in_disconnect = 1;
3085 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3091 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3093 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3094 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3095 nes_rem_ref(&nesqp->ibqp);
3098 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3099 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3100 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3101 issue_modify_qp = 1;
3102 nesqp->in_disconnect = 1;
3106 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3107 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3108 nes_rem_ref(&nesqp->ibqp);
3111 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3113 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3114 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3115 if (nesqp->hte_added) {
3116 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3117 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3118 nesqp->hte_added = 0;
3120 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3121 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3122 next_iwarp_state |= NES_CQP_QP_RESET;
3123 nesqp->in_disconnect = 1;
3125 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3126 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3129 issue_modify_qp = 1;
3130 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3133 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3134 nes_rem_ref(&nesqp->ibqp);
3139 nesqp->ibqp_state = attr->qp_state;
3140 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3141 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3142 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3143 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3144 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3145 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3146 nesqp->iwarp_state);
3147 issue_disconnect = 1;
3149 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3150 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3151 nesqp->iwarp_state);
3155 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3156 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3157 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3158 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3159 issue_modify_qp = 1;
3161 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3162 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3163 issue_modify_qp = 1;
3165 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3166 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3167 issue_modify_qp = 1;
3169 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3170 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3171 issue_modify_qp = 1;
3174 if (nesqp->user_mode) {
3175 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3176 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3177 issue_modify_qp = 1;
3181 original_last_aeq = nesqp->last_aeq;
3182 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3184 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3189 if (issue_modify_qp) {
3190 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3191 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 1);
3193 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3194 " failed for QP%u.\n",
3195 next_iwarp_state, nesqp->hwqp.qp_id);
3199 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3200 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3201 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3202 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3203 original_last_aeq, nesqp->last_aeq);
3205 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3208 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3209 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3210 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3211 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3212 original_last_aeq, nesqp->last_aeq);
3213 /* this one is for the cm_disconnect thread */
3214 nes_add_ref(&nesqp->ibqp);
3215 spin_lock_irqsave(&nesqp->lock, qplockflags);
3216 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3217 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3218 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3219 nes_cm_disconn(nesqp);
3221 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3222 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3223 nes_rem_ref(&nesqp->ibqp);
3226 spin_lock_irqsave(&nesqp->lock, qplockflags);
3228 /* These two are for the timer thread */
3229 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3230 nes_add_ref(&nesqp->ibqp);
3231 nesqp->cm_id->add_ref(nesqp->cm_id);
3232 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3233 " need ae to finish up, original_last_aeq = 0x%04X."
3234 " last_aeq = 0x%04X, scheduling timer.\n",
3235 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3236 original_last_aeq, nesqp->last_aeq);
3237 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3239 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3241 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3242 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3243 " need ae to finish up, original_last_aeq = 0x%04X."
3244 " last_aeq = 0x%04X.\n",
3245 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3246 original_last_aeq, nesqp->last_aeq);
3250 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3251 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3252 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3253 original_last_aeq, nesqp->last_aeq);
3254 nes_rem_ref(&nesqp->ibqp);
3257 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3258 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3259 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3260 original_last_aeq, nesqp->last_aeq);
3261 nes_rem_ref(&nesqp->ibqp);
3266 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3267 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3274 * nes_muticast_attach
3276 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3278 nes_debug(NES_DBG_INIT, "\n");
3284 * nes_multicast_detach
3286 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3288 nes_debug(NES_DBG_INIT, "\n");
3296 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3297 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3298 struct ib_mad *in_mad, struct ib_mad *out_mad)
3300 nes_debug(NES_DBG_INIT, "\n");
3305 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3308 int total_payload_length = 0;
3309 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3310 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3311 ib_wr->sg_list[sge_index].addr);
3312 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3313 ib_wr->sg_list[sge_index].length);
3315 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3316 (ib_wr->sg_list[sge_index].lkey));
3318 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3320 total_payload_length += ib_wr->sg_list[sge_index].length;
3322 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3323 total_payload_length);
3324 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3325 total_payload_length);
3331 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3332 struct ib_send_wr **bad_wr)
3335 unsigned long flags = 0;
3336 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3337 struct nes_device *nesdev = nesvnic->nesdev;
3338 struct nes_qp *nesqp = to_nesqp(ibqp);
3339 struct nes_hw_qp_wqe *wqe;
3341 u32 qsize = nesqp->hwqp.sq_size;
3346 u32 total_payload_length;
3351 total_payload_length = 0;
3353 if (nesqp->ibqp_state > IB_QPS_RTS)
3356 spin_lock_irqsave(&nesqp->lock, flags);
3358 head = nesqp->hwqp.sq_head;
3361 /* Check for SQ overflow */
3362 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3367 wqe = &nesqp->hwqp.sq_vbase[head];
3368 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3369 nesqp->hwqp.qp_id, wqe, head); */
3370 nes_fill_init_qp_wqe(wqe, nesqp, head);
3371 u64temp = (u64)(ib_wr->wr_id);
3372 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3374 switch (ib_wr->opcode) {
3376 if (ib_wr->send_flags & IB_SEND_SOLICITED) {
3377 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3379 wqe_misc = NES_IWARP_SQ_OP_SEND;
3381 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3385 if (ib_wr->send_flags & IB_SEND_FENCE) {
3386 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3388 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3389 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3390 (ib_wr->sg_list[0].length <= 64)) {
3391 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3392 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3393 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3394 ib_wr->sg_list[0].length);
3395 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3397 fill_wqe_sg_send(wqe, ib_wr, 1);
3401 case IB_WR_RDMA_WRITE:
3402 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3403 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3404 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3406 nesdev->nesadapter->max_sge);
3410 if (ib_wr->send_flags & IB_SEND_FENCE) {
3411 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3414 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3415 ib_wr->wr.rdma.rkey);
3416 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3417 ib_wr->wr.rdma.remote_addr);
3419 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3420 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3421 (ib_wr->sg_list[0].length <= 64)) {
3422 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3423 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3424 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3425 ib_wr->sg_list[0].length);
3426 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3428 fill_wqe_sg_send(wqe, ib_wr, 1);
3430 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3431 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3433 case IB_WR_RDMA_READ:
3434 /* iWARP only supports 1 sge for RDMA reads */
3435 if (ib_wr->num_sge > 1) {
3436 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3441 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3442 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3443 ib_wr->wr.rdma.remote_addr);
3444 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3445 ib_wr->wr.rdma.rkey);
3446 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3447 ib_wr->sg_list->length);
3448 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3449 ib_wr->sg_list->addr);
3450 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3451 ib_wr->sg_list->lkey);
3459 if (ib_wr->send_flags & IB_SEND_SIGNALED) {
3460 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3462 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3464 ib_wr = ib_wr->next;
3472 nesqp->hwqp.sq_head = head;
3475 counter = min(wqe_count, ((u32)255));
3476 wqe_count -= counter;
3477 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3478 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3481 spin_unlock_irqrestore(&nesqp->lock, flags);
3492 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3493 struct ib_recv_wr **bad_wr)
3496 unsigned long flags = 0;
3497 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3498 struct nes_device *nesdev = nesvnic->nesdev;
3499 struct nes_qp *nesqp = to_nesqp(ibqp);
3500 struct nes_hw_qp_wqe *wqe;
3503 u32 qsize = nesqp->hwqp.rq_size;
3507 u32 total_payload_length;
3509 if (nesqp->ibqp_state > IB_QPS_RTS)
3512 spin_lock_irqsave(&nesqp->lock, flags);
3514 head = nesqp->hwqp.rq_head;
3517 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3521 /* Check for RQ overflow */
3522 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3527 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3528 wqe = &nesqp->hwqp.rq_vbase[head];
3530 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3531 nesqp->hwqp.qp_id, wqe, head); */
3532 nes_fill_init_qp_wqe(wqe, nesqp, head);
3533 u64temp = (u64)(ib_wr->wr_id);
3534 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3536 total_payload_length = 0;
3537 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3538 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3539 ib_wr->sg_list[sge_index].addr);
3540 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3541 ib_wr->sg_list[sge_index].length);
3542 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3543 ib_wr->sg_list[sge_index].lkey);
3545 total_payload_length += ib_wr->sg_list[sge_index].length;
3547 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3548 total_payload_length);
3550 ib_wr = ib_wr->next;
3557 nesqp->hwqp.rq_head = head;
3560 counter = min(wqe_count, ((u32)255));
3561 wqe_count -= counter;
3562 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3565 spin_unlock_irqrestore(&nesqp->lock, flags);
3576 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3581 unsigned long flags = 0;
3582 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3583 struct nes_device *nesdev = nesvnic->nesdev;
3584 struct nes_cq *nescq = to_nescq(ibcq);
3585 struct nes_qp *nesqp;
3586 struct nes_hw_cqe cqe;
3595 nes_debug(NES_DBG_CQ, "\n");
3597 spin_lock_irqsave(&nescq->lock, flags);
3599 head = nescq->hw_cq.cq_head;
3600 cq_size = nescq->hw_cq.cq_size;
3602 while (cqe_count < num_entries) {
3603 if (le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3605 cqe = nescq->hw_cq.cq_vbase[head];
3606 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3607 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3608 wqe_index = u32temp &
3609 (nesdev->nesadapter->max_qp_wr - 1);
3610 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3611 /* parse CQE, get completion context from WQE (either rq or sq */
3612 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3614 nesqp = *((struct nes_qp **)&u64temp);
3615 memset(entry, 0, sizeof *entry);
3616 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3617 entry->status = IB_WC_SUCCESS;
3619 entry->status = IB_WC_WR_FLUSH_ERR;
3622 entry->qp = &nesqp->ibqp;
3623 entry->src_qp = nesqp->hwqp.qp_id;
3625 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3626 if (nesqp->skip_lsmm) {
3627 nesqp->skip_lsmm = 0;
3628 wq_tail = nesqp->hwqp.sq_tail++;
3631 /* Working on a SQ Completion*/
3632 wq_tail = wqe_index;
3633 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3634 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3635 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3636 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3637 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3638 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3639 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3641 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3642 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3643 case NES_IWARP_SQ_OP_RDMAW:
3644 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3645 entry->opcode = IB_WC_RDMA_WRITE;
3647 case NES_IWARP_SQ_OP_RDMAR:
3648 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3649 entry->opcode = IB_WC_RDMA_READ;
3650 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3651 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3653 case NES_IWARP_SQ_OP_SENDINV:
3654 case NES_IWARP_SQ_OP_SENDSEINV:
3655 case NES_IWARP_SQ_OP_SEND:
3656 case NES_IWARP_SQ_OP_SENDSE:
3657 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3658 entry->opcode = IB_WC_SEND;
3662 /* Working on a RQ Completion*/
3663 wq_tail = wqe_index;
3664 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3665 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3666 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3667 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3668 entry->opcode = IB_WC_RECV;
3670 entry->wr_id = wrid;
3672 if (++head >= cq_size)
3675 nescq->polled_completions++;
3676 if ((nescq->polled_completions > (cq_size / 2)) ||
3677 (nescq->polled_completions == 255)) {
3678 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3679 " are pending %u of %u.\n",
3680 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3681 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3682 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3683 nescq->polled_completions = 0;
3690 if (nescq->polled_completions) {
3691 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3692 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3693 nescq->polled_completions = 0;
3696 nescq->hw_cq.cq_head = head;
3697 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3698 cqe_count, nescq->hw_cq.cq_number);
3700 spin_unlock_irqrestore(&nescq->lock, flags);
3709 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3711 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3712 struct nes_device *nesdev = nesvnic->nesdev;
3713 struct nes_cq *nescq = to_nescq(ibcq);
3716 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3717 nescq->hw_cq.cq_number);
3719 cq_arm = nescq->hw_cq.cq_number;
3720 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3721 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3722 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3723 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3727 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3728 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3735 * nes_init_ofa_device
3737 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3739 struct nes_ib_device *nesibdev;
3740 struct nes_vnic *nesvnic = netdev_priv(netdev);
3741 struct nes_device *nesdev = nesvnic->nesdev;
3743 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3744 if (nesibdev == NULL) {
3747 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3748 nesibdev->ibdev.owner = THIS_MODULE;
3750 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3751 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3752 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3754 nesibdev->ibdev.uverbs_cmd_mask =
3755 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3756 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3757 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3758 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3759 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3760 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3761 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3762 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3763 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3764 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3765 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3766 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3767 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3768 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3769 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3770 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3771 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3772 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3773 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3774 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3775 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3776 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3778 nesibdev->ibdev.phys_port_cnt = 1;
3779 nesibdev->ibdev.num_comp_vectors = 1;
3780 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3781 nesibdev->ibdev.class_dev.dev = &nesdev->pcidev->dev;
3782 nesibdev->ibdev.query_device = nes_query_device;
3783 nesibdev->ibdev.query_port = nes_query_port;
3784 nesibdev->ibdev.modify_port = nes_modify_port;
3785 nesibdev->ibdev.query_pkey = nes_query_pkey;
3786 nesibdev->ibdev.query_gid = nes_query_gid;
3787 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3788 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3789 nesibdev->ibdev.mmap = nes_mmap;
3790 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3791 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3792 nesibdev->ibdev.create_ah = nes_create_ah;
3793 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3794 nesibdev->ibdev.create_qp = nes_create_qp;
3795 nesibdev->ibdev.modify_qp = nes_modify_qp;
3796 nesibdev->ibdev.query_qp = nes_query_qp;
3797 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3798 nesibdev->ibdev.create_cq = nes_create_cq;
3799 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3800 nesibdev->ibdev.poll_cq = nes_poll_cq;
3801 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3802 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3803 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3804 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3805 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3806 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3807 nesibdev->ibdev.bind_mw = nes_bind_mw;
3809 nesibdev->ibdev.alloc_fmr = nes_alloc_fmr;
3810 nesibdev->ibdev.unmap_fmr = nes_unmap_fmr;
3811 nesibdev->ibdev.dealloc_fmr = nes_dealloc_fmr;
3812 nesibdev->ibdev.map_phys_fmr = nes_map_phys_fmr;
3814 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3815 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3816 nesibdev->ibdev.process_mad = nes_process_mad;
3818 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3819 nesibdev->ibdev.post_send = nes_post_send;
3820 nesibdev->ibdev.post_recv = nes_post_recv;
3822 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3823 if (nesibdev->ibdev.iwcm == NULL) {
3824 ib_dealloc_device(&nesibdev->ibdev);
3827 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3828 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3829 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3830 nesibdev->ibdev.iwcm->connect = nes_connect;
3831 nesibdev->ibdev.iwcm->accept = nes_accept;
3832 nesibdev->ibdev.iwcm->reject = nes_reject;
3833 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3834 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3841 * nes_destroy_ofa_device
3843 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3845 if (nesibdev == NULL)
3848 nes_unregister_ofa_device(nesibdev);
3850 kfree(nesibdev->ibdev.iwcm);
3851 ib_dealloc_device(&nesibdev->ibdev);
3856 * nes_register_ofa_device
3858 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3860 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3861 struct nes_device *nesdev = nesvnic->nesdev;
3862 struct nes_adapter *nesadapter = nesdev->nesadapter;
3865 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3870 /* Get the resources allocated to this device */
3871 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3872 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3873 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3874 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3876 for (i = 0; i < ARRAY_SIZE(nes_class_attributes); ++i) {
3877 ret = class_device_create_file(&nesibdev->ibdev.class_dev, nes_class_attributes[i]);
3881 class_device_remove_file(&nesibdev->ibdev.class_dev,
3882 nes_class_attributes[i]);
3884 ib_unregister_device(&nesibdev->ibdev);
3889 nesvnic->of_device_registered = 1;
3896 * nes_unregister_ofa_device
3898 void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3900 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3903 if (nesibdev == NULL)
3906 for (i = 0; i < ARRAY_SIZE(nes_class_attributes); ++i) {
3907 class_device_remove_file(&nesibdev->ibdev.class_dev, nes_class_attributes[i]);
3910 if (nesvnic->of_device_registered) {
3911 ib_unregister_device(&nesibdev->ibdev);
3914 nesvnic->of_device_registered = 0;