2 * Copyright (c) 2006 - 2009 Intel-NE, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/random.h>
37 #include <linux/highmem.h>
38 #include <asm/byteorder.h>
40 #include <rdma/ib_verbs.h>
41 #include <rdma/iw_cm.h>
42 #include <rdma/ib_user_verbs.h>
46 #include <rdma/ib_umem.h>
48 atomic_t mod_qp_timouts;
50 atomic_t sw_qps_destroyed;
52 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
57 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) {
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);
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 nes_put_cqp_request(nesdev, cqp_request);
132 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
134 return ERR_PTR(-ETIME);
136 return ERR_PTR(-ENOMEM);
139 nes_put_cqp_request(nesdev, cqp_request);
141 nesmr->ibmw.rkey = stag;
142 nesmr->mode = IWNES_MEMREG_TYPE_MW;
145 nesmr->pbls_used = 0;
154 static int nes_dealloc_mw(struct ib_mw *ibmw)
156 struct nes_mr *nesmr = to_nesmw(ibmw);
157 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
158 struct nes_device *nesdev = nesvnic->nesdev;
159 struct nes_adapter *nesadapter = nesdev->nesadapter;
160 struct nes_hw_cqp_wqe *cqp_wqe;
161 struct nes_cqp_request *cqp_request;
165 /* Deallocate the window with the adapter */
166 cqp_request = nes_get_cqp_request(nesdev);
167 if (cqp_request == NULL) {
168 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
171 cqp_request->waiting = 1;
172 cqp_wqe = &cqp_request->cqp_wqe;
173 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
174 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
175 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
177 atomic_set(&cqp_request->refcount, 2);
178 nes_post_cqp_request(nesdev, cqp_request);
181 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
183 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
185 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
186 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
187 ret, cqp_request->major_code, cqp_request->minor_code);
190 else if (cqp_request->major_code)
193 nes_put_cqp_request(nesdev, cqp_request);
195 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
196 (ibmw->rkey & 0x0fffff00) >> 8);
206 static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
207 struct ib_mw_bind *ibmw_bind)
210 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
211 struct nes_device *nesdev = nesvnic->nesdev;
212 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
213 struct nes_qp *nesqp = to_nesqp(ibqp);
214 struct nes_hw_qp_wqe *wqe;
215 unsigned long flags = 0;
220 if (nesqp->ibqp_state > IB_QPS_RTS)
223 spin_lock_irqsave(&nesqp->lock, flags);
225 head = nesqp->hwqp.sq_head;
226 qsize = nesqp->hwqp.sq_tail;
228 /* Check for SQ overflow */
229 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
230 spin_unlock_irqrestore(&nesqp->lock, flags);
234 wqe = &nesqp->hwqp.sq_vbase[head];
235 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
236 nes_fill_init_qp_wqe(wqe, nesqp, head);
237 u64temp = ibmw_bind->wr_id;
238 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
239 wqe_misc = NES_IWARP_SQ_OP_BIND;
241 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
243 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
244 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
246 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_WRITE) {
247 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
249 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_READ) {
250 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
253 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
254 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX, ibmw_bind->mr->lkey);
255 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
256 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
258 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
259 u64temp = (u64)ibmw_bind->addr;
260 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
266 nesqp->hwqp.sq_head = head;
269 nes_write32(nesdev->regs+NES_WQE_ALLOC,
270 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
272 spin_unlock_irqrestore(&nesqp->lock, flags);
281 static struct ib_fmr *nes_alloc_fmr(struct ib_pd *ibpd,
282 int ibmr_access_flags,
283 struct ib_fmr_attr *ibfmr_attr)
286 struct nes_pd *nespd = to_nespd(ibpd);
287 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
288 struct nes_device *nesdev = nesvnic->nesdev;
289 struct nes_adapter *nesadapter = nesdev->nesadapter;
290 struct nes_fmr *nesfmr;
291 struct nes_cqp_request *cqp_request;
292 struct nes_hw_cqp_wqe *cqp_wqe;
296 u32 next_stag_index = 0;
301 struct nes_vpbl vpbl;
303 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
304 stag_key = (u8)next_stag_index;
308 next_stag_index >>= 8;
309 next_stag_index %= nesadapter->max_mr;
311 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
312 nesadapter->max_mr, &stag_index, &next_stag_index);
314 goto failed_resource_alloc;
317 nesfmr = kzalloc(sizeof(*nesfmr), GFP_KERNEL);
320 goto failed_fmr_alloc;
323 nesfmr->nesmr.mode = IWNES_MEMREG_TYPE_FMR;
324 if (ibfmr_attr->max_pages == 1) {
325 /* use zero length PBL */
326 nesfmr->nesmr.pbl_4k = 0;
327 nesfmr->nesmr.pbls_used = 0;
328 } else if (ibfmr_attr->max_pages <= 32) {
330 nesfmr->nesmr.pbl_4k = 0;
331 nesfmr->nesmr.pbls_used = 1;
332 } else if (ibfmr_attr->max_pages <= 512) {
334 nesfmr->nesmr.pbl_4k = 1;
335 nesfmr->nesmr.pbls_used = 1;
337 /* use two level 4K PBLs */
338 /* add support for two level 256B PBLs */
339 nesfmr->nesmr.pbl_4k = 1;
340 nesfmr->nesmr.pbls_used = 1 + (ibfmr_attr->max_pages >> 9) +
341 ((ibfmr_attr->max_pages & 511) ? 1 : 0);
343 /* Register the region with the adapter */
344 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
346 /* track PBL resources */
347 if (nesfmr->nesmr.pbls_used != 0) {
348 if (nesfmr->nesmr.pbl_4k) {
349 if (nesfmr->nesmr.pbls_used > nesadapter->free_4kpbl) {
350 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
352 goto failed_vpbl_avail;
354 nesadapter->free_4kpbl -= nesfmr->nesmr.pbls_used;
357 if (nesfmr->nesmr.pbls_used > nesadapter->free_256pbl) {
358 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
360 goto failed_vpbl_avail;
362 nesadapter->free_256pbl -= nesfmr->nesmr.pbls_used;
368 if (nesfmr->nesmr.pbls_used == 0) {
369 nesfmr->root_vpbl.pbl_vbase = NULL;
370 nes_debug(NES_DBG_MR, "zero level pbl \n");
371 } else if (nesfmr->nesmr.pbls_used == 1) {
372 /* can change it to kmalloc & dma_map_single */
373 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
374 &nesfmr->root_vpbl.pbl_pbase);
375 if (!nesfmr->root_vpbl.pbl_vbase) {
376 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
378 goto failed_vpbl_alloc;
380 nesfmr->leaf_pbl_cnt = 0;
381 nes_debug(NES_DBG_MR, "one level pbl, root_vpbl.pbl_vbase=%p \n",
382 nesfmr->root_vpbl.pbl_vbase);
386 nesfmr->root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
387 &nesfmr->root_vpbl.pbl_pbase);
388 if (!nesfmr->root_vpbl.pbl_vbase) {
389 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
391 goto failed_vpbl_alloc;
394 nesfmr->leaf_pbl_cnt = nesfmr->nesmr.pbls_used-1;
395 nesfmr->root_vpbl.leaf_vpbl = kzalloc(sizeof(*nesfmr->root_vpbl.leaf_vpbl)*1024, GFP_ATOMIC);
396 if (!nesfmr->root_vpbl.leaf_vpbl) {
397 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
399 goto failed_leaf_vpbl_alloc;
402 nes_debug(NES_DBG_MR, "two level pbl, root_vpbl.pbl_vbase=%p"
403 " leaf_pbl_cnt=%d root_vpbl.leaf_vpbl=%p\n",
404 nesfmr->root_vpbl.pbl_vbase, nesfmr->leaf_pbl_cnt, nesfmr->root_vpbl.leaf_vpbl);
406 for (i=0; i<nesfmr->leaf_pbl_cnt; i++)
407 nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase = NULL;
409 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
410 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
413 if (!vpbl.pbl_vbase) {
415 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
416 goto failed_leaf_vpbl_pages_alloc;
419 nesfmr->root_vpbl.pbl_vbase[i].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
420 nesfmr->root_vpbl.pbl_vbase[i].pa_high = cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
421 nesfmr->root_vpbl.leaf_vpbl[i] = vpbl;
423 nes_debug(NES_DBG_MR, "pbase_low=0x%x, pbase_high=0x%x, vpbl=%p\n",
424 nesfmr->root_vpbl.pbl_vbase[i].pa_low,
425 nesfmr->root_vpbl.pbl_vbase[i].pa_high,
426 &nesfmr->root_vpbl.leaf_vpbl[i]);
429 nesfmr->ib_qp = NULL;
430 nesfmr->access_rights =0;
432 stag = stag_index << 8;
434 stag += (u32)stag_key;
436 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
437 cqp_request = nes_get_cqp_request(nesdev);
438 if (cqp_request == NULL) {
439 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
441 goto failed_leaf_vpbl_pages_alloc;
443 cqp_request->waiting = 1;
444 cqp_wqe = &cqp_request->cqp_wqe;
446 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
449 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
451 if (nesfmr->nesmr.pbl_4k == 1)
452 opcode |= NES_CQP_STAG_PBL_BLK_SIZE;
454 if (ibmr_access_flags & IB_ACCESS_REMOTE_WRITE) {
455 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE |
456 NES_CQP_STAG_RIGHTS_LOCAL_WRITE | NES_CQP_STAG_REM_ACC_EN;
457 nesfmr->access_rights |=
458 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_RIGHTS_LOCAL_WRITE |
459 NES_CQP_STAG_REM_ACC_EN;
462 if (ibmr_access_flags & IB_ACCESS_REMOTE_READ) {
463 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ |
464 NES_CQP_STAG_RIGHTS_LOCAL_READ | NES_CQP_STAG_REM_ACC_EN;
465 nesfmr->access_rights |=
466 NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_RIGHTS_LOCAL_READ |
467 NES_CQP_STAG_REM_ACC_EN;
470 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
471 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
472 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
473 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
475 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX] =
476 cpu_to_le32((nesfmr->nesmr.pbls_used>1) ?
477 (nesfmr->nesmr.pbls_used-1) : nesfmr->nesmr.pbls_used);
479 atomic_set(&cqp_request->refcount, 2);
480 nes_post_cqp_request(nesdev, cqp_request);
483 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
485 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
486 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
487 stag, ret, cqp_request->major_code, cqp_request->minor_code);
489 if ((!ret) || (cqp_request->major_code)) {
490 nes_put_cqp_request(nesdev, cqp_request);
491 ret = (!ret) ? -ETIME : -EIO;
492 goto failed_leaf_vpbl_pages_alloc;
494 nes_put_cqp_request(nesdev, cqp_request);
495 nesfmr->nesmr.ibfmr.lkey = stag;
496 nesfmr->nesmr.ibfmr.rkey = stag;
497 nesfmr->attr = *ibfmr_attr;
499 return &nesfmr->nesmr.ibfmr;
501 failed_leaf_vpbl_pages_alloc:
502 /* unroll all allocated pages */
503 for (i=0; i<nesfmr->leaf_pbl_cnt; i++) {
504 if (nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase) {
505 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
506 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
509 if (nesfmr->root_vpbl.leaf_vpbl)
510 kfree(nesfmr->root_vpbl.leaf_vpbl);
512 failed_leaf_vpbl_alloc:
513 if (nesfmr->leaf_pbl_cnt == 0) {
514 if (nesfmr->root_vpbl.pbl_vbase)
515 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
516 nesfmr->root_vpbl.pbl_pbase);
518 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
519 nesfmr->root_vpbl.pbl_pbase);
522 if (nesfmr->nesmr.pbls_used != 0) {
523 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
524 if (nesfmr->nesmr.pbl_4k)
525 nesadapter->free_4kpbl += nesfmr->nesmr.pbls_used;
527 nesadapter->free_256pbl += nesfmr->nesmr.pbls_used;
528 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
535 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
537 failed_resource_alloc:
545 static int nes_dealloc_fmr(struct ib_fmr *ibfmr)
548 struct nes_mr *nesmr = to_nesmr_from_ibfmr(ibfmr);
549 struct nes_fmr *nesfmr = to_nesfmr(nesmr);
550 struct nes_vnic *nesvnic = to_nesvnic(ibfmr->device);
551 struct nes_device *nesdev = nesvnic->nesdev;
552 struct nes_adapter *nesadapter = nesdev->nesadapter;
556 /* free the resources */
557 if (nesfmr->leaf_pbl_cnt == 0) {
558 /* single PBL case */
559 if (nesfmr->root_vpbl.pbl_vbase)
560 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
561 nesfmr->root_vpbl.pbl_pbase);
563 for (i = 0; i < nesfmr->leaf_pbl_cnt; i++) {
564 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
565 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
567 kfree(nesfmr->root_vpbl.leaf_vpbl);
568 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
569 nesfmr->root_vpbl.pbl_pbase);
571 nesmr->ibmw.device = ibfmr->device;
572 nesmr->ibmw.pd = ibfmr->pd;
573 nesmr->ibmw.rkey = ibfmr->rkey;
574 nesmr->ibmw.uobject = NULL;
576 rc = nes_dealloc_mw(&nesmr->ibmw);
578 if ((rc == 0) && (nesfmr->nesmr.pbls_used != 0)) {
579 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
580 if (nesfmr->nesmr.pbl_4k) {
581 nesadapter->free_4kpbl += nesfmr->nesmr.pbls_used;
582 WARN_ON(nesadapter->free_4kpbl > nesadapter->max_4kpbl);
584 nesadapter->free_256pbl += nesfmr->nesmr.pbls_used;
585 WARN_ON(nesadapter->free_256pbl > nesadapter->max_256pbl);
587 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
597 static int nes_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
598 int list_len, u64 iova)
607 static int nes_unmap_fmr(struct list_head *ibfmr_list)
617 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
619 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
620 struct nes_device *nesdev = nesvnic->nesdev;
621 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
623 memset(props, 0, sizeof(*props));
624 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
626 props->fw_ver = nesdev->nesadapter->fw_ver;
627 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
628 props->vendor_id = nesdev->nesadapter->vendor_id;
629 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
630 props->hw_ver = nesdev->nesadapter->hw_rev;
631 props->max_mr_size = 0x80000000;
632 props->max_qp = nesibdev->max_qp;
633 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
634 props->max_sge = nesdev->nesadapter->max_sge;
635 props->max_cq = nesibdev->max_cq;
636 props->max_cqe = nesdev->nesadapter->max_cqe - 1;
637 props->max_mr = nesibdev->max_mr;
638 props->max_mw = nesibdev->max_mr;
639 props->max_pd = nesibdev->max_pd;
640 props->max_sge_rd = 1;
641 switch (nesdev->nesadapter->max_irrq_wr) {
643 props->max_qp_rd_atom = 1;
646 props->max_qp_rd_atom = 4;
649 props->max_qp_rd_atom = 16;
652 props->max_qp_rd_atom = 32;
655 props->max_qp_rd_atom = 0;
657 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
658 props->atomic_cap = IB_ATOMIC_NONE;
659 props->max_map_per_fmr = 1;
668 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
670 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
671 struct net_device *netdev = nesvnic->netdev;
673 memset(props, 0, sizeof(*props));
675 props->max_mtu = IB_MTU_4096;
677 if (netdev->mtu >= 4096)
678 props->active_mtu = IB_MTU_4096;
679 else if (netdev->mtu >= 2048)
680 props->active_mtu = IB_MTU_2048;
681 else if (netdev->mtu >= 1024)
682 props->active_mtu = IB_MTU_1024;
683 else if (netdev->mtu >= 512)
684 props->active_mtu = IB_MTU_512;
686 props->active_mtu = IB_MTU_256;
693 props->state = IB_PORT_ACTIVE;
695 props->state = IB_PORT_DOWN;
696 props->phys_state = 0;
697 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
698 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
699 props->gid_tbl_len = 1;
700 props->pkey_tbl_len = 1;
701 props->qkey_viol_cntr = 0;
702 props->active_width = IB_WIDTH_4X;
703 props->active_speed = 1;
704 props->max_msg_sz = 0x80000000;
713 static int nes_modify_port(struct ib_device *ibdev, u8 port,
714 int port_modify_mask, struct ib_port_modify *props)
723 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
733 static int nes_query_gid(struct ib_device *ibdev, u8 port,
734 int index, union ib_gid *gid)
736 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
738 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
739 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
746 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
747 * of all objects associated with a particular user-mode client.
749 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
750 struct ib_udata *udata)
752 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
753 struct nes_device *nesdev = nesvnic->nesdev;
754 struct nes_adapter *nesadapter = nesdev->nesadapter;
755 struct nes_alloc_ucontext_req req;
756 struct nes_alloc_ucontext_resp uresp;
757 struct nes_ucontext *nes_ucontext;
758 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
761 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
762 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
763 return ERR_PTR(-EINVAL);
766 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
767 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
768 req.userspace_ver, NES_ABI_USERSPACE_VER);
769 return ERR_PTR(-EINVAL);
773 memset(&uresp, 0, sizeof uresp);
775 uresp.max_qps = nesibdev->max_qp;
776 uresp.max_pds = nesibdev->max_pd;
777 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
778 uresp.virtwq = nesadapter->virtwq;
779 uresp.kernel_ver = NES_ABI_KERNEL_VER;
781 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
783 return ERR_PTR(-ENOMEM);
785 nes_ucontext->nesdev = nesdev;
786 nes_ucontext->mmap_wq_offset = uresp.max_pds;
787 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
788 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
792 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
794 return ERR_PTR(-EFAULT);
797 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
798 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
799 atomic_set(&nes_ucontext->usecnt, 1);
800 return &nes_ucontext->ibucontext;
805 * nes_dealloc_ucontext
807 static int nes_dealloc_ucontext(struct ib_ucontext *context)
809 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
810 /* struct nes_device *nesdev = nesvnic->nesdev; */
811 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
813 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
823 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
826 struct nes_vnic *nesvnic = to_nesvnic(context->device);
827 struct nes_device *nesdev = nesvnic->nesdev;
828 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
829 struct nes_ucontext *nes_ucontext;
830 struct nes_qp *nesqp;
832 nes_ucontext = to_nesucontext(context);
835 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
836 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
837 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
838 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
839 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
840 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
843 nesqp = nes_ucontext->mmap_nesqp[index];
845 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
848 if (remap_pfn_range(vma, vma->vm_start,
849 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
850 vma->vm_end - vma->vm_start,
851 vma->vm_page_prot)) {
852 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
855 vma->vm_private_data = nesqp;
858 index = vma->vm_pgoff;
859 if (!test_bit(index, nes_ucontext->allocated_doorbells))
862 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
863 if (io_remap_pfn_range(vma, vma->vm_start,
864 (nesdev->doorbell_start +
865 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
866 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
868 vma->vm_private_data = nes_ucontext;
879 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
880 struct ib_ucontext *context, struct ib_udata *udata)
882 struct nes_pd *nespd;
883 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
884 struct nes_device *nesdev = nesvnic->nesdev;
885 struct nes_adapter *nesadapter = nesdev->nesadapter;
886 struct nes_ucontext *nesucontext;
887 struct nes_alloc_pd_resp uresp;
891 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
892 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
893 atomic_read(&nesvnic->netdev->refcnt));
895 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
896 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
901 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
903 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
904 return ERR_PTR(-ENOMEM);
907 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
908 nespd, nesvnic->nesibdev->ibdev.name);
910 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
913 nesucontext = to_nesucontext(context);
914 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
915 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
916 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
917 nespd->mmap_db_index, nespd->pd_id);
918 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
919 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
920 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
922 return ERR_PTR(-ENOMEM);
925 uresp.pd_id = nespd->pd_id;
926 uresp.mmap_db_index = nespd->mmap_db_index;
927 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
928 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
930 return ERR_PTR(-EFAULT);
933 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
934 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
935 nesucontext->first_free_db = nespd->mmap_db_index + 1;
938 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
946 static int nes_dealloc_pd(struct ib_pd *ibpd)
948 struct nes_ucontext *nesucontext;
949 struct nes_pd *nespd = to_nespd(ibpd);
950 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
951 struct nes_device *nesdev = nesvnic->nesdev;
952 struct nes_adapter *nesadapter = nesdev->nesadapter;
954 if ((ibpd->uobject) && (ibpd->uobject->context)) {
955 nesucontext = to_nesucontext(ibpd->uobject->context);
956 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
957 nespd->mmap_db_index);
958 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
959 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
960 if (nesucontext->first_free_db > nespd->mmap_db_index) {
961 nesucontext->first_free_db = nespd->mmap_db_index;
965 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
966 nespd->pd_id, nespd);
967 nes_free_resource(nesadapter, nesadapter->allocated_pds,
968 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
978 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
980 return ERR_PTR(-ENOSYS);
987 static int nes_destroy_ah(struct ib_ah *ah)
994 * nes_get_encoded_size
996 static inline u8 nes_get_encoded_size(int *size)
1002 } else if (*size <= 128) {
1005 } else if (*size <= 512) {
1009 return (encoded_size);
1017 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
1018 struct nes_vnic *nesvnic, int sq_size, int rq_size)
1020 unsigned long flags;
1025 struct nes_device *nesdev = nesvnic->nesdev;
1026 struct nes_adapter *nesadapter = nesdev->nesadapter;
1031 pbl_entries = nespbl->pbl_size >> 3;
1032 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
1033 nespbl->pbl_size, pbl_entries,
1034 (void *)nespbl->pbl_vbase,
1035 (unsigned long) nespbl->pbl_pbase);
1036 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
1037 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
1038 /* the first pbl to be fro the rq_vbase... */
1039 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1040 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
1041 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1042 if (!nespbl->page) {
1043 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
1048 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
1049 nesqp->page = nespbl->page;
1050 if (!nesqp->hwqp.sq_vbase) {
1051 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
1056 /* Now to get to sq.. we need to calculate how many */
1057 /* PBL entries were used by the rq.. */
1058 pbl += sq_pbl_entries;
1059 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1060 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
1061 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
1063 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
1064 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
1065 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
1066 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1067 if (!nesadapter->free_256pbl) {
1068 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1070 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1071 kunmap(nesqp->page);
1075 nesadapter->free_256pbl--;
1076 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1078 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
1079 pblbuffer = nesqp->pbl_vbase;
1080 if (!nesqp->pbl_vbase) {
1081 /* memory allocated during nes_reg_user_mr() */
1082 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1085 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1086 nesadapter->free_256pbl++;
1087 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1088 kunmap(nesqp->page);
1091 memset(nesqp->pbl_vbase, 0, 256);
1092 /* fill in the page address in the pbl buffer.. */
1093 tpbl = pblbuffer + 16;
1094 pbl = (__le64 *)nespbl->pbl_vbase;
1095 while (sq_pbl_entries--)
1098 while (rq_pbl_entries--)
1101 /* done with memory allocated during nes_reg_user_mr() */
1102 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1106 nesqp->qp_mem_size =
1107 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1108 /* Round up to a multiple of a page */
1109 nesqp->qp_mem_size += PAGE_SIZE - 1;
1110 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1112 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1113 &nesqp->hwqp.q2_pbase);
1116 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1117 nesqp->pbl_vbase = NULL;
1118 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1119 nesadapter->free_256pbl++;
1120 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1121 kunmap(nesqp->page);
1124 nesqp->hwqp.q2_vbase = mem;
1126 memset(nesqp->hwqp.q2_vbase, 0, 256);
1127 nesqp->nesqp_context = mem;
1128 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1129 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1138 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1139 int sq_size, int rq_size)
1142 struct nes_device *nesdev = nesvnic->nesdev;
1144 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1145 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1146 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1147 256; /* this is Q2 */
1148 /* Round up to a multiple of a page */
1149 nesqp->qp_mem_size += PAGE_SIZE - 1;
1150 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1152 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1153 &nesqp->hwqp.sq_pbase);
1156 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1157 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1158 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1160 memset(mem, 0, nesqp->qp_mem_size);
1162 nesqp->hwqp.sq_vbase = mem;
1163 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1165 nesqp->hwqp.rq_vbase = mem;
1166 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1167 sizeof(struct nes_hw_qp_wqe) * sq_size;
1168 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1170 nesqp->hwqp.q2_vbase = mem;
1171 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1172 sizeof(struct nes_hw_qp_wqe) * rq_size;
1174 memset(nesqp->hwqp.q2_vbase, 0, 256);
1176 nesqp->nesqp_context = mem;
1177 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1178 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1184 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1186 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1187 struct nes_qp *nesqp, int virt_wqs)
1189 unsigned long flags;
1190 struct nes_adapter *nesadapter = nesdev->nesadapter;
1192 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1193 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1195 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1196 nesadapter->free_256pbl++;
1197 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1198 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1199 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1200 nesqp->pbl_vbase = NULL;
1201 kunmap(nesqp->page);
1209 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1210 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1214 struct nes_pd *nespd = to_nespd(ibpd);
1215 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1216 struct nes_device *nesdev = nesvnic->nesdev;
1217 struct nes_adapter *nesadapter = nesdev->nesadapter;
1218 struct nes_qp *nesqp;
1219 struct nes_cq *nescq;
1220 struct nes_ucontext *nes_ucontext;
1221 struct nes_hw_cqp_wqe *cqp_wqe;
1222 struct nes_cqp_request *cqp_request;
1223 struct nes_create_qp_req req;
1224 struct nes_create_qp_resp uresp;
1225 struct nes_pbl *nespbl = NULL;
1228 /* u32 counter = 0; */
1230 unsigned long flags;
1240 if (init_attr->create_flags)
1241 return ERR_PTR(-EINVAL);
1243 atomic_inc(&qps_created);
1244 switch (init_attr->qp_type) {
1246 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1247 init_attr->cap.max_inline_data = 0;
1249 init_attr->cap.max_inline_data = 64;
1251 sq_size = init_attr->cap.max_send_wr;
1252 rq_size = init_attr->cap.max_recv_wr;
1254 /* check if the encoded sizes are OK or not... */
1255 sq_encoded_size = nes_get_encoded_size(&sq_size);
1256 rq_encoded_size = nes_get_encoded_size(&rq_size);
1258 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1259 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1261 return ERR_PTR(-EINVAL);
1264 init_attr->cap.max_send_wr = sq_size -2;
1265 init_attr->cap.max_recv_wr = rq_size -1;
1266 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1268 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1269 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1271 return ERR_PTR(ret);
1274 /* Need 512 (actually now 1024) byte alignment on this structure */
1275 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1277 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1278 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1279 return ERR_PTR(-ENOMEM);
1281 u64nesqp = (unsigned long)mem;
1282 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1283 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1284 u64nesqp &= ~u64temp;
1285 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1286 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1287 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1288 nesqp->allocated_buffer = mem;
1291 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1292 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1293 kfree(nesqp->allocated_buffer);
1294 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1297 if (req.user_wqe_buffers) {
1300 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1301 nesqp->user_mode = 1;
1302 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1305 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1306 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1307 list_del(&nespbl->list);
1309 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1310 nespbl, nespbl->user_base);
1315 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1316 (long long unsigned int)req.user_wqe_buffers);
1317 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1318 kfree(nesqp->allocated_buffer);
1319 return ERR_PTR(-EFAULT);
1323 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1324 nesqp->mmap_sq_db_index =
1325 find_next_zero_bit(nes_ucontext->allocated_wqs,
1326 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1327 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1328 nespd->mmap_db_index); */
1329 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1330 nes_debug(NES_DBG_QP,
1331 "db index > max user regions, failing create QP\n");
1332 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1334 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1338 kfree(nesqp->allocated_buffer);
1339 return ERR_PTR(-ENOMEM);
1341 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1342 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1343 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1345 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1346 kfree(nesqp->allocated_buffer);
1347 return ERR_PTR(-EFAULT);
1350 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1351 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1353 nes_debug(NES_DBG_QP,
1354 "error geting qp mem code = %d\n", err);
1355 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1356 kfree(nesqp->allocated_buffer);
1357 return ERR_PTR(-ENOMEM);
1360 nesqp->hwqp.sq_size = sq_size;
1361 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1362 nesqp->hwqp.sq_head = 1;
1363 nesqp->hwqp.rq_size = rq_size;
1364 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1365 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1366 (void *)nesqp->nesqp_context_pbase);
1368 nesqp->hwqp.qp_id = qp_num;
1369 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1370 nesqp->nespd = nespd;
1372 nescq = to_nescq(init_attr->send_cq);
1373 nesqp->nesscq = nescq;
1374 nescq = to_nescq(init_attr->recv_cq);
1375 nesqp->nesrcq = nescq;
1377 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1378 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1379 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1380 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1381 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1382 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1384 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1385 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1387 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1388 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1389 u64temp = (u64)nesqp->hwqp.sq_pbase;
1390 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1391 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1395 u64temp = (u64)nesqp->hwqp.sq_pbase;
1396 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1397 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1398 u64temp = (u64)nesqp->hwqp.rq_pbase;
1399 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1400 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1402 u64temp = (u64)nesqp->pbl_pbase;
1403 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1404 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1407 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1408 nesvnic->next_qp_nic_index,
1409 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1410 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1411 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1412 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1413 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1414 nesvnic->next_qp_nic_index++;
1415 if ((nesvnic->next_qp_nic_index > 3) ||
1416 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1417 nesvnic->next_qp_nic_index = 0;
1419 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1421 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1422 u64temp = (u64)nesqp->hwqp.q2_pbase;
1423 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1424 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1425 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1426 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1427 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1428 ((((u32)nesadapter->max_irrq_wr) <<
1429 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1430 if (disable_mpa_crc) {
1431 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1432 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1437 cqp_request = nes_get_cqp_request(nesdev);
1438 if (cqp_request == NULL) {
1439 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1440 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1441 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1442 kfree(nesqp->allocated_buffer);
1443 return ERR_PTR(-ENOMEM);
1445 cqp_request->waiting = 1;
1446 cqp_wqe = &cqp_request->cqp_wqe;
1449 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1450 NES_CQP_QP_IWARP_STATE_IDLE;
1452 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1453 NES_CQP_QP_IWARP_STATE_IDLE;
1455 opcode |= NES_CQP_QP_CQS_VALID;
1456 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1457 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1458 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1460 u64temp = (u64)nesqp->nesqp_context_pbase;
1461 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1463 atomic_set(&cqp_request->refcount, 2);
1464 nes_post_cqp_request(nesdev, cqp_request);
1467 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1469 ret = wait_event_timeout(cqp_request->waitq,
1470 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1471 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1472 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1473 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1474 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1475 cqp_request->major_code, cqp_request->minor_code);
1476 if ((!ret) || (cqp_request->major_code)) {
1477 nes_put_cqp_request(nesdev, cqp_request);
1478 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1479 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1480 kfree(nesqp->allocated_buffer);
1482 return ERR_PTR(-ETIME);
1484 return ERR_PTR(-EIO);
1488 nes_put_cqp_request(nesdev, cqp_request);
1490 if (ibpd->uobject) {
1491 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1492 uresp.actual_sq_size = sq_size;
1493 uresp.actual_rq_size = rq_size;
1494 uresp.qp_id = nesqp->hwqp.qp_id;
1495 uresp.nes_drv_opt = nes_drv_opt;
1496 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1497 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1498 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1499 kfree(nesqp->allocated_buffer);
1500 return ERR_PTR(-EFAULT);
1504 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1505 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1506 spin_lock_init(&nesqp->lock);
1507 init_waitqueue_head(&nesqp->state_waitq);
1508 init_waitqueue_head(&nesqp->kick_waitq);
1509 nes_add_ref(&nesqp->ibqp);
1512 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1513 return ERR_PTR(-EINVAL);
1516 /* update the QP table */
1517 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1518 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1519 atomic_read(&nesvnic->netdev->refcnt));
1521 return &nesqp->ibqp;
1528 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1534 unsigned long flags = 0;
1536 spin_lock_irqsave(&nescq->lock, flags);
1538 cq_head = nescq->hw_cq.cq_head;
1539 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1541 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1542 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1543 u64temp = (((u64)hi) << 32) | ((u64)lo);
1544 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1545 if (u64temp == (u64)(unsigned long)nesqp) {
1546 /* Zero the context value so cqe will be ignored */
1547 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1548 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1551 if (++cq_head >= nescq->hw_cq.cq_size)
1555 spin_unlock_irqrestore(&nescq->lock, flags);
1562 static int nes_destroy_qp(struct ib_qp *ibqp)
1564 struct nes_qp *nesqp = to_nesqp(ibqp);
1565 struct nes_ucontext *nes_ucontext;
1566 struct ib_qp_attr attr;
1567 struct iw_cm_id *cm_id;
1568 struct iw_cm_event cm_event;
1571 atomic_inc(&sw_qps_destroyed);
1572 nesqp->destroyed = 1;
1574 /* Blow away the connection if it exists. */
1575 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1576 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1577 attr.qp_state = IB_QPS_ERR;
1578 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1581 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1582 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1583 cm_id = nesqp->cm_id;
1584 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1585 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1586 cm_event.local_addr = cm_id->local_addr;
1587 cm_event.remote_addr = cm_id->remote_addr;
1588 cm_event.private_data = NULL;
1589 cm_event.private_data_len = 0;
1591 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1592 "QP%u. cm_id = %p, refcount = %u. \n",
1593 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1595 cm_id->rem_ref(cm_id);
1596 ret = cm_id->event_handler(cm_id, &cm_event);
1598 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1601 if (nesqp->user_mode) {
1602 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1603 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1604 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1605 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1606 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1607 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1610 if (nesqp->pbl_pbase)
1611 kunmap(nesqp->page);
1613 /* Clean any pending completions from the cq(s) */
1615 nes_clean_cq(nesqp, nesqp->nesscq);
1617 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1618 nes_clean_cq(nesqp, nesqp->nesrcq);
1621 nes_rem_ref(&nesqp->ibqp);
1629 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1631 struct ib_ucontext *context, struct ib_udata *udata)
1634 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1635 struct nes_device *nesdev = nesvnic->nesdev;
1636 struct nes_adapter *nesadapter = nesdev->nesadapter;
1637 struct nes_cq *nescq;
1638 struct nes_ucontext *nes_ucontext = NULL;
1639 struct nes_cqp_request *cqp_request;
1641 struct nes_hw_cqp_wqe *cqp_wqe;
1642 struct nes_pbl *nespbl = NULL;
1643 struct nes_create_cq_req req;
1644 struct nes_create_cq_resp resp;
1647 u32 pbl_entries = 1;
1649 unsigned long flags;
1652 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1653 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1655 return ERR_PTR(err);
1658 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1660 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1661 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1662 return ERR_PTR(-ENOMEM);
1665 nescq->hw_cq.cq_size = max(entries + 1, 5);
1666 nescq->hw_cq.cq_number = cq_num;
1667 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1671 nes_ucontext = to_nesucontext(context);
1672 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1673 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1675 return ERR_PTR(-EFAULT);
1677 nesvnic->mcrq_ucontext = nes_ucontext;
1678 nes_ucontext->mcrqf = req.mcrqf;
1679 if (nes_ucontext->mcrqf) {
1680 if (nes_ucontext->mcrqf & 0x80000000)
1681 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1682 else if (nes_ucontext->mcrqf & 0x40000000)
1683 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1685 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1686 nescq->mcrqf = nes_ucontext->mcrqf;
1687 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1689 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1690 (unsigned long)req.user_cq_buffer, entries);
1692 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1693 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1694 list_del(&nespbl->list);
1696 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1702 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1704 return ERR_PTR(-EFAULT);
1707 pbl_entries = nespbl->pbl_size >> 3;
1708 nescq->cq_mem_size = 0;
1710 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1711 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1712 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1714 /* allocate the physical buffer space */
1715 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1716 &nescq->hw_cq.cq_pbase);
1718 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1719 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1721 return ERR_PTR(-ENOMEM);
1724 memset(mem, 0, nescq->cq_mem_size);
1725 nescq->hw_cq.cq_vbase = mem;
1726 nescq->hw_cq.cq_head = 0;
1727 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1728 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1729 (u32)nescq->hw_cq.cq_pbase);
1732 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1733 spin_lock_init(&nescq->lock);
1735 /* send CreateCQ request to CQP */
1736 cqp_request = nes_get_cqp_request(nesdev);
1737 if (cqp_request == NULL) {
1738 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1740 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1741 nescq->hw_cq.cq_pbase);
1743 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1744 nespbl->pbl_vbase, nespbl->pbl_pbase);
1748 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1750 return ERR_PTR(-ENOMEM);
1752 cqp_request->waiting = 1;
1753 cqp_wqe = &cqp_request->cqp_wqe;
1755 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1756 NES_CQP_CQ_CHK_OVERFLOW |
1757 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1759 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1761 if (pbl_entries != 1) {
1762 if (pbl_entries > 32) {
1764 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1765 if (nesadapter->free_4kpbl == 0) {
1766 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1767 nes_free_cqp_request(nesdev, cqp_request);
1769 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1770 nescq->hw_cq.cq_pbase);
1772 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1773 nespbl->pbl_vbase, nespbl->pbl_pbase);
1776 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1778 return ERR_PTR(-ENOMEM);
1780 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1781 nescq->virtual_cq = 2;
1782 nesadapter->free_4kpbl--;
1785 /* use 256 byte pbl */
1786 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1787 if (nesadapter->free_256pbl == 0) {
1788 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1789 nes_free_cqp_request(nesdev, cqp_request);
1791 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1792 nescq->hw_cq.cq_pbase);
1794 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1795 nespbl->pbl_vbase, nespbl->pbl_pbase);
1798 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1800 return ERR_PTR(-ENOMEM);
1802 opcode |= NES_CQP_CQ_VIRT;
1803 nescq->virtual_cq = 1;
1804 nesadapter->free_256pbl--;
1809 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1811 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1812 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1813 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1814 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1817 if (pbl_entries != 1)
1818 u64temp = (u64)nespbl->pbl_pbase;
1820 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1821 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1822 nes_ucontext->mmap_db_index[0]);
1824 u64temp = (u64)nescq->hw_cq.cq_pbase;
1825 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1827 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1828 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1829 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1830 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1831 cpu_to_le32((u32)(u64temp >> 1));
1832 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1833 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1835 atomic_set(&cqp_request->refcount, 2);
1836 nes_post_cqp_request(nesdev, cqp_request);
1839 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1840 nescq->hw_cq.cq_number);
1841 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1842 NES_EVENT_TIMEOUT * 2);
1843 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1844 nescq->hw_cq.cq_number, ret);
1845 if ((!ret) || (cqp_request->major_code)) {
1846 nes_put_cqp_request(nesdev, cqp_request);
1848 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1849 nescq->hw_cq.cq_pbase);
1851 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1852 nespbl->pbl_vbase, nespbl->pbl_pbase);
1855 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1857 return ERR_PTR(-EIO);
1859 nes_put_cqp_request(nesdev, cqp_request);
1862 /* free the nespbl */
1863 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1866 resp.cq_id = nescq->hw_cq.cq_number;
1867 resp.cq_size = nescq->hw_cq.cq_size;
1868 resp.mmap_db_index = 0;
1869 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1870 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1872 return ERR_PTR(-EFAULT);
1876 return &nescq->ibcq;
1883 static int nes_destroy_cq(struct ib_cq *ib_cq)
1885 struct nes_cq *nescq;
1886 struct nes_device *nesdev;
1887 struct nes_vnic *nesvnic;
1888 struct nes_adapter *nesadapter;
1889 struct nes_hw_cqp_wqe *cqp_wqe;
1890 struct nes_cqp_request *cqp_request;
1891 unsigned long flags;
1898 nescq = to_nescq(ib_cq);
1899 nesvnic = to_nesvnic(ib_cq->device);
1900 nesdev = nesvnic->nesdev;
1901 nesadapter = nesdev->nesadapter;
1903 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1905 /* Send DestroyCQ request to CQP */
1906 cqp_request = nes_get_cqp_request(nesdev);
1907 if (cqp_request == NULL) {
1908 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1911 cqp_request->waiting = 1;
1912 cqp_wqe = &cqp_request->cqp_wqe;
1913 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1914 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1915 if (nescq->virtual_cq == 1) {
1916 nesadapter->free_256pbl++;
1917 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1918 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1919 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1921 } else if (nescq->virtual_cq == 2) {
1922 nesadapter->free_4kpbl++;
1923 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1924 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1925 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1927 opcode |= NES_CQP_CQ_4KB_CHUNK;
1930 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1932 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1933 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1934 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1935 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1937 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1939 atomic_set(&cqp_request->refcount, 2);
1940 nes_post_cqp_request(nesdev, cqp_request);
1943 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1944 nescq->hw_cq.cq_number);
1945 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1947 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1948 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1949 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1950 cqp_request->minor_code);
1952 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1953 nescq->hw_cq.cq_number);
1955 } else if (cqp_request->major_code) {
1956 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1957 nescq->hw_cq.cq_number);
1962 nes_put_cqp_request(nesdev, cqp_request);
1964 if (nescq->cq_mem_size)
1965 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1966 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1975 static u32 root_256(struct nes_device *nesdev,
1976 struct nes_root_vpbl *root_vpbl,
1977 struct nes_root_vpbl *new_root,
1983 if (pbl_count_4k == 1) {
1984 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1985 512, &new_root->pbl_pbase);
1987 if (new_root->pbl_vbase == NULL)
1990 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1991 for (i = 0; i < 16; i++) {
1992 new_root->pbl_vbase[i].pa_low =
1993 cpu_to_le32((u32)leaf_pbl);
1994 new_root->pbl_vbase[i].pa_high =
1995 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1999 for (i = 3; i >= 0; i--) {
2001 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
2002 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
2003 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
2005 for (k = 1; k < 16; k++) {
2007 root_vpbl->pbl_vbase[j + k].pa_low =
2008 cpu_to_le32((u32)leaf_pbl);
2009 root_vpbl->pbl_vbase[j + k].pa_high =
2010 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
2022 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
2023 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
2024 dma_addr_t single_buffer, u16 pbl_count_4k,
2025 u16 residual_page_count_4k, int acc, u64 *iova_start,
2026 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
2028 struct nes_hw_cqp_wqe *cqp_wqe;
2029 struct nes_cqp_request *cqp_request;
2030 unsigned long flags;
2032 struct nes_adapter *nesadapter = nesdev->nesadapter;
2034 u16 pbl_count_256 = 0;
2036 u8 use_256_pbls = 0;
2038 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
2039 struct nes_root_vpbl new_root = {0, 0, 0};
2043 /* Register the region with the adapter */
2044 cqp_request = nes_get_cqp_request(nesdev);
2045 if (cqp_request == NULL) {
2046 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2049 cqp_request->waiting = 1;
2050 cqp_wqe = &cqp_request->cqp_wqe;
2053 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2055 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
2056 pbl_count_256 = (pg_cnt + 31) / 32;
2058 if (pbl_count_256 <= nesadapter->free_256pbl)
2060 else if (pbl_count_4k <= nesadapter->free_4kpbl)
2062 } else if (pg_cnt <= 2048) {
2063 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
2064 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
2066 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
2069 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2073 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
2078 pbl_count = pbl_count_256;
2079 nesadapter->free_256pbl -= pbl_count + use_two_level;
2080 } else if (use_4k_pbls) {
2081 pbl_count = pbl_count_4k;
2082 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2084 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2085 nes_debug(NES_DBG_MR, "Out of Pbls\n");
2086 nes_free_cqp_request(nesdev, cqp_request);
2090 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2093 if (use_256_pbls && use_two_level) {
2094 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
2095 if (new_root.pbl_pbase != 0)
2096 root_vpbl = &new_root;
2098 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2099 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
2102 if (pbl_count_4k == 1)
2104 pbl_count = pbl_count_4k;
2106 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2107 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2110 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2112 if (use_4k_pbls == 0)
2117 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2118 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2119 if (acc & IB_ACCESS_LOCAL_WRITE)
2120 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2121 if (acc & IB_ACCESS_REMOTE_WRITE)
2122 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2123 if (acc & IB_ACCESS_REMOTE_READ)
2124 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2125 if (acc & IB_ACCESS_MW_BIND)
2126 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2128 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2129 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2130 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2131 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2133 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2134 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2135 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2136 cpu_to_le32(nespd->pd_id & 0x00007fff);
2137 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2139 if (pbl_count == 0) {
2140 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2142 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2143 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2144 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
2147 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2151 atomic_set(&cqp_request->refcount, 2);
2152 nes_post_cqp_request(nesdev, cqp_request);
2155 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2157 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2158 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2159 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2160 major_code = cqp_request->major_code;
2161 nes_put_cqp_request(nesdev, cqp_request);
2163 if ((!ret || major_code) && pbl_count != 0) {
2164 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2166 nesadapter->free_256pbl += pbl_count + use_two_level;
2167 else if (use_4k_pbls)
2168 nesadapter->free_4kpbl += pbl_count + use_two_level;
2169 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2171 if (new_root.pbl_pbase)
2172 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2173 new_root.pbl_pbase);
2177 else if (major_code)
2180 *actual_pbl_cnt = pbl_count + use_two_level;
2181 *used_4k_pbls = use_4k_pbls;
2189 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2190 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2194 struct nes_pd *nespd = to_nespd(ib_pd);
2195 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2196 struct nes_device *nesdev = nesvnic->nesdev;
2197 struct nes_adapter *nesadapter = nesdev->nesadapter;
2198 struct nes_mr *nesmr;
2200 struct nes_vpbl vpbl;
2201 struct nes_root_vpbl root_vpbl;
2206 u32 next_stag_index = 0;
2208 u32 root_pbl_index = 0;
2209 u32 cur_pbl_index = 0;
2210 int err = 0, pbl_depth = 0;
2218 vpbl.pbl_vbase = NULL;
2219 root_vpbl.pbl_vbase = NULL;
2220 root_vpbl.pbl_pbase = 0;
2222 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2223 stag_key = (u8)next_stag_index;
2227 next_stag_index >>= 8;
2228 next_stag_index %= nesadapter->max_mr;
2229 if (num_phys_buf > (1024*512)) {
2230 return ERR_PTR(-E2BIG);
2233 if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2234 return ERR_PTR(-EINVAL);
2236 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2237 &stag_index, &next_stag_index);
2239 return ERR_PTR(err);
2242 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2244 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2245 return ERR_PTR(-ENOMEM);
2248 for (i = 0; i < num_phys_buf; i++) {
2250 if ((i & 0x01FF) == 0) {
2251 if (root_pbl_index == 1) {
2252 /* Allocate the root PBL */
2253 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2254 &root_vpbl.pbl_pbase);
2255 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2256 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2257 if (!root_vpbl.pbl_vbase) {
2258 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2260 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2262 return ERR_PTR(-ENOMEM);
2264 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2265 if (!root_vpbl.leaf_vpbl) {
2266 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2267 root_vpbl.pbl_pbase);
2268 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2270 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2272 return ERR_PTR(-ENOMEM);
2274 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2275 root_vpbl.pbl_vbase[0].pa_high =
2276 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2277 root_vpbl.leaf_vpbl[0] = vpbl;
2279 /* Allocate a 4K buffer for the PBL */
2280 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2282 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2283 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2284 if (!vpbl.pbl_vbase) {
2285 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2286 ibmr = ERR_PTR(-ENOMEM);
2290 /* Fill in the root table */
2291 if (1 <= root_pbl_index) {
2292 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2293 cpu_to_le32((u32)vpbl.pbl_pbase);
2294 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2295 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2296 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2302 mask = !buffer_list[i].size;
2304 mask |= buffer_list[i].addr;
2305 if (i != num_phys_buf - 1)
2306 mask |= buffer_list[i].addr + buffer_list[i].size;
2308 if (mask & ~PAGE_MASK) {
2309 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2310 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2311 ibmr = ERR_PTR(-EINVAL);
2316 region_length += buffer_list[i].size;
2317 if ((i != 0) && (single_page)) {
2318 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2321 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr & PAGE_MASK);
2322 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2323 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2326 stag = stag_index << 8;
2328 stag += (u32)stag_key;
2330 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2331 " length = 0x%016lX, index = 0x%08X\n",
2332 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2334 /* Make the leaf PBL the root if only one PBL */
2335 if (root_pbl_index == 1) {
2336 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2342 pbl_count = root_pbl_index;
2344 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2345 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2346 &nesmr->pbls_used, &nesmr->pbl_4k);
2349 nesmr->ibmr.rkey = stag;
2350 nesmr->ibmr.lkey = stag;
2351 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2352 ibmr = &nesmr->ibmr;
2355 ibmr = ERR_PTR(-ENOMEM);
2359 /* free the resources */
2360 if (root_pbl_index == 1) {
2361 /* single PBL case */
2362 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2364 for (i=0; i<root_pbl_index; i++) {
2365 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2366 root_vpbl.leaf_vpbl[i].pbl_pbase);
2368 kfree(root_vpbl.leaf_vpbl);
2369 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2370 root_vpbl.pbl_pbase);
2380 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2382 struct ib_phys_buf bl;
2385 nes_debug(NES_DBG_MR, "\n");
2387 bl.size = (u64)0xffffffffffULL;
2389 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2396 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2397 u64 virt, int acc, struct ib_udata *udata)
2402 dma_addr_t last_dma_addr = 0;
2403 dma_addr_t first_dma_addr = 0;
2404 struct nes_pd *nespd = to_nespd(pd);
2405 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2406 struct nes_device *nesdev = nesvnic->nesdev;
2407 struct nes_adapter *nesadapter = nesdev->nesadapter;
2408 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2409 struct ib_umem_chunk *chunk;
2410 struct nes_ucontext *nes_ucontext;
2411 struct nes_pbl *nespbl;
2412 struct nes_mr *nesmr;
2413 struct ib_umem *region;
2414 struct nes_mem_reg_req req;
2415 struct nes_vpbl vpbl;
2416 struct nes_root_vpbl root_vpbl;
2417 int nmap_index, page_index;
2419 int err, pbl_depth = 0;
2424 u32 next_stag_index;
2426 u32 root_pbl_index = 0;
2427 u32 cur_pbl_index = 0;
2433 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
2434 if (IS_ERR(region)) {
2435 return (struct ib_mr *)region;
2438 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2439 " offset = %u, page size = %u.\n",
2440 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2441 region->offset, region->page_size);
2443 skip_pages = ((u32)region->offset) >> 12;
2445 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2446 return ERR_PTR(-EFAULT);
2447 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2449 switch (req.reg_type) {
2450 case IWNES_MEMREG_TYPE_MEM:
2453 vpbl.pbl_vbase = NULL;
2454 root_vpbl.pbl_vbase = NULL;
2455 root_vpbl.pbl_pbase = 0;
2457 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2458 stag_key = (u8)next_stag_index;
2460 driver_key = next_stag_index & 0x70000000;
2462 next_stag_index >>= 8;
2463 next_stag_index %= nesadapter->max_mr;
2465 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2466 nesadapter->max_mr, &stag_index, &next_stag_index);
2468 ib_umem_release(region);
2469 return ERR_PTR(err);
2472 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2474 ib_umem_release(region);
2475 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2476 return ERR_PTR(-ENOMEM);
2478 nesmr->region = region;
2480 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2481 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2482 chunk->nents, chunk->nmap);
2483 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2484 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2485 ib_umem_release(region);
2486 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2487 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2488 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2489 ibmr = ERR_PTR(-EINVAL);
2491 goto reg_user_mr_err;
2494 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2495 ib_umem_release(region);
2496 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2498 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2499 ibmr = ERR_PTR(-EINVAL);
2501 goto reg_user_mr_err;
2504 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2505 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2506 region_length -= skip_pages << 12;
2507 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2509 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2511 if ((page_count&0x01FF) == 0) {
2512 if (page_count >= 1024 * 512) {
2513 ib_umem_release(region);
2514 nes_free_resource(nesadapter,
2515 nesadapter->allocated_mrs, stag_index);
2517 ibmr = ERR_PTR(-E2BIG);
2518 goto reg_user_mr_err;
2520 if (root_pbl_index == 1) {
2521 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2522 8192, &root_vpbl.pbl_pbase);
2523 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2524 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2525 if (!root_vpbl.pbl_vbase) {
2526 ib_umem_release(region);
2527 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2529 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2532 ibmr = ERR_PTR(-ENOMEM);
2533 goto reg_user_mr_err;
2535 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2537 if (!root_vpbl.leaf_vpbl) {
2538 ib_umem_release(region);
2539 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2540 root_vpbl.pbl_pbase);
2541 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2543 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2546 ibmr = ERR_PTR(-ENOMEM);
2547 goto reg_user_mr_err;
2549 root_vpbl.pbl_vbase[0].pa_low =
2550 cpu_to_le32((u32)vpbl.pbl_pbase);
2551 root_vpbl.pbl_vbase[0].pa_high =
2552 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2553 root_vpbl.leaf_vpbl[0] = vpbl;
2555 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2557 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2558 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2559 if (!vpbl.pbl_vbase) {
2560 ib_umem_release(region);
2561 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2562 ibmr = ERR_PTR(-ENOMEM);
2564 goto reg_user_mr_err;
2566 if (1 <= root_pbl_index) {
2567 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2568 cpu_to_le32((u32)vpbl.pbl_pbase);
2569 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2570 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2571 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2577 if (page_count != 0) {
2578 if ((last_dma_addr+4096) !=
2579 (sg_dma_address(&chunk->page_list[nmap_index])+
2582 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2585 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2587 last_dma_addr = first_dma_addr;
2591 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2592 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2593 (page_index*4096)));
2594 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2595 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2596 (page_index*4096))) >> 32)));
2603 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2604 " stag_key=0x%08x\n",
2605 stag_index, driver_key, stag_key);
2606 stag = stag_index << 8;
2608 stag += (u32)stag_key;
2614 /* Make the leaf PBL the root if only one PBL */
2615 if (root_pbl_index == 1) {
2616 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2622 pbl_count = root_pbl_index;
2625 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2626 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2627 stag, (unsigned int)iova_start,
2628 (unsigned int)region_length, stag_index,
2629 (unsigned long long)region->length, pbl_count);
2630 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2631 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2632 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2634 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2637 nesmr->ibmr.rkey = stag;
2638 nesmr->ibmr.lkey = stag;
2639 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2640 ibmr = &nesmr->ibmr;
2642 ib_umem_release(region);
2644 ibmr = ERR_PTR(-ENOMEM);
2648 /* free the resources */
2649 if (root_pbl_index == 1) {
2650 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2653 for (page_index=0; page_index<root_pbl_index; page_index++) {
2654 pci_free_consistent(nesdev->pcidev, 4096,
2655 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2656 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2658 kfree(root_vpbl.leaf_vpbl);
2659 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2660 root_vpbl.pbl_pbase);
2663 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2666 case IWNES_MEMREG_TYPE_QP:
2667 case IWNES_MEMREG_TYPE_CQ:
2668 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2670 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2671 ib_umem_release(region);
2672 return ERR_PTR(-ENOMEM);
2674 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2676 ib_umem_release(region);
2678 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2679 return ERR_PTR(-ENOMEM);
2681 nesmr->region = region;
2682 nes_ucontext = to_nesucontext(pd->uobject->context);
2683 pbl_depth = region->length >> 12;
2684 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2685 nespbl->pbl_size = pbl_depth*sizeof(u64);
2686 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2687 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2689 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2692 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2693 nespbl->pbl_size, pbl_depth);
2694 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2695 &nespbl->pbl_pbase);
2697 ib_umem_release(region);
2700 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2701 return ERR_PTR(-ENOMEM);
2704 nespbl->pbl_vbase = (u64 *)pbl;
2705 nespbl->user_base = start;
2706 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2707 " pbl_vbase=%p user_base=0x%lx\n",
2708 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2709 (void *) nespbl->pbl_vbase, nespbl->user_base);
2711 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2712 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2713 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2714 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2715 nespbl->page = sg_page(&chunk->page_list[0]);
2716 for (page_index=0; page_index<chunk_pages; page_index++) {
2717 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2718 (sg_dma_address(&chunk->page_list[nmap_index])+
2719 (page_index*4096)));
2720 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2721 (sg_dma_address(&chunk->page_list[nmap_index])+
2722 (page_index*4096)))>>32);
2723 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2724 (unsigned long long)*pbl,
2725 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2730 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2731 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2733 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2735 nesmr->ibmr.rkey = -1;
2736 nesmr->ibmr.lkey = -1;
2737 nesmr->mode = req.reg_type;
2738 return &nesmr->ibmr;
2741 return ERR_PTR(-ENOSYS);
2748 static int nes_dereg_mr(struct ib_mr *ib_mr)
2750 struct nes_mr *nesmr = to_nesmr(ib_mr);
2751 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2752 struct nes_device *nesdev = nesvnic->nesdev;
2753 struct nes_adapter *nesadapter = nesdev->nesadapter;
2754 struct nes_hw_cqp_wqe *cqp_wqe;
2755 struct nes_cqp_request *cqp_request;
2756 unsigned long flags;
2761 if (nesmr->region) {
2762 ib_umem_release(nesmr->region);
2764 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2769 /* Deallocate the region with the adapter */
2771 cqp_request = nes_get_cqp_request(nesdev);
2772 if (cqp_request == NULL) {
2773 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2776 cqp_request->waiting = 1;
2777 cqp_wqe = &cqp_request->cqp_wqe;
2779 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2780 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2781 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2782 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2783 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2785 atomic_set(&cqp_request->refcount, 2);
2786 nes_post_cqp_request(nesdev, cqp_request);
2789 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2790 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2792 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2793 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2794 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2796 major_code = cqp_request->major_code;
2797 minor_code = cqp_request->minor_code;
2799 nes_put_cqp_request(nesdev, cqp_request);
2802 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2803 " ib_mr=%p, rkey = 0x%08X\n",
2804 ib_mr, ib_mr->rkey);
2806 } else if (major_code) {
2807 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2808 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2809 major_code, minor_code, ib_mr, ib_mr->rkey);
2813 if (nesmr->pbls_used != 0) {
2814 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2815 if (nesmr->pbl_4k) {
2816 nesadapter->free_4kpbl += nesmr->pbls_used;
2817 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2818 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2819 "exceeded the max(%u)\n",
2820 nesadapter->free_4kpbl,
2821 nesadapter->max_4kpbl);
2823 nesadapter->free_256pbl += nesmr->pbls_used;
2824 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2825 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2826 "exceeded the max(%u)\n",
2827 nesadapter->free_256pbl,
2828 nesadapter->max_256pbl);
2830 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2832 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2833 (ib_mr->rkey & 0x0fffff00) >> 8);
2844 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2847 struct nes_ib_device *nesibdev =
2848 container_of(dev, struct nes_ib_device, ibdev.dev);
2849 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2851 nes_debug(NES_DBG_INIT, "\n");
2852 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2859 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2862 struct nes_ib_device *nesibdev =
2863 container_of(dev, struct nes_ib_device, ibdev.dev);
2864 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2866 nes_debug(NES_DBG_INIT, "\n");
2867 return sprintf(buf, "%u.%u\n",
2868 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2869 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
2876 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2879 nes_debug(NES_DBG_INIT, "\n");
2880 return sprintf(buf, "NES020\n");
2887 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2890 nes_debug(NES_DBG_INIT, "\n");
2891 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2895 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2896 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2897 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2898 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2900 static struct device_attribute *nes_dev_attributes[] = {
2911 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2912 int attr_mask, struct ib_qp_init_attr *init_attr)
2914 struct nes_qp *nesqp = to_nesqp(ibqp);
2916 nes_debug(NES_DBG_QP, "\n");
2918 attr->qp_access_flags = 0;
2919 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2920 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2921 attr->cap.max_recv_sge = 1;
2922 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2923 init_attr->cap.max_inline_data = 0;
2925 init_attr->cap.max_inline_data = 64;
2928 init_attr->event_handler = nesqp->ibqp.event_handler;
2929 init_attr->qp_context = nesqp->ibqp.qp_context;
2930 init_attr->send_cq = nesqp->ibqp.send_cq;
2931 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2932 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2933 init_attr->cap = attr->cap;
2942 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2943 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2945 struct nes_hw_cqp_wqe *cqp_wqe;
2946 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2947 /* struct iw_cm_event cm_event; */
2948 struct nes_cqp_request *cqp_request;
2952 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2953 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2955 cqp_request = nes_get_cqp_request(nesdev);
2956 if (cqp_request == NULL) {
2957 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2960 if (wait_completion) {
2961 cqp_request->waiting = 1;
2963 cqp_request->waiting = 0;
2965 cqp_wqe = &cqp_request->cqp_wqe;
2967 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2968 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2969 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2970 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2971 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2972 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2973 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2975 /* If sending a terminate message, fill in the length (in words) */
2976 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2977 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2978 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2979 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2982 atomic_set(&cqp_request->refcount, 2);
2983 nes_post_cqp_request(nesdev, cqp_request);
2986 if (wait_completion) {
2987 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2988 nesqp->hwqp.qp_id); */
2989 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2991 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2992 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2993 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2994 major_code = cqp_request->major_code;
2996 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2997 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2998 nesqp->hwqp.qp_id, cqp_request->major_code,
2999 cqp_request->minor_code, next_iwarp_state);
3002 nes_put_cqp_request(nesdev, cqp_request);
3006 else if (major_code)
3019 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
3020 int attr_mask, struct ib_udata *udata)
3022 struct nes_qp *nesqp = to_nesqp(ibqp);
3023 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3024 struct nes_device *nesdev = nesvnic->nesdev;
3027 u32 next_iwarp_state = 0;
3029 unsigned long qplockflags;
3031 u16 original_last_aeq;
3032 u8 issue_modify_qp = 0;
3033 u8 issue_disconnect = 0;
3036 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
3037 " iwarp_state=0x%X, refcount=%d\n",
3038 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
3039 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
3041 spin_lock_irqsave(&nesqp->lock, qplockflags);
3043 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
3044 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
3045 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
3046 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
3048 if (attr_mask & IB_QP_STATE) {
3049 switch (attr->qp_state) {
3051 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
3053 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3054 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3057 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3058 issue_modify_qp = 1;
3061 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
3063 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
3064 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3067 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
3068 issue_modify_qp = 1;
3071 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
3073 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
3074 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3077 if (nesqp->cm_id == NULL) {
3078 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
3079 nesqp->hwqp.qp_id );
3080 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3083 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
3084 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
3085 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
3086 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
3087 issue_modify_qp = 1;
3088 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
3089 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
3090 nesqp->hte_added = 1;
3093 issue_modify_qp = 1;
3094 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
3095 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
3096 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3097 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3100 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
3101 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3102 " ignored due to current iWARP state\n",
3104 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3107 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3108 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3109 " already done based on hw state.\n",
3111 issue_modify_qp = 0;
3112 nesqp->in_disconnect = 0;
3114 switch (nesqp->hw_iwarp_state) {
3115 case NES_AEQE_IWARP_STATE_CLOSING:
3116 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3117 case NES_AEQE_IWARP_STATE_TERMINATE:
3118 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3120 case NES_AEQE_IWARP_STATE_ERROR:
3121 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3124 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3125 nesqp->in_disconnect = 1;
3126 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3132 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3134 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3135 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3138 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3139 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3140 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3141 issue_modify_qp = 1;
3142 nesqp->in_disconnect = 1;
3146 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3147 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3150 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3152 if (nesqp->term_flags)
3153 del_timer(&nesqp->terminate_timer);
3155 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3156 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3157 if (nesqp->hte_added) {
3158 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3159 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3160 nesqp->hte_added = 0;
3162 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3163 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3164 next_iwarp_state |= NES_CQP_QP_RESET;
3165 nesqp->in_disconnect = 1;
3167 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3168 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3171 issue_modify_qp = 1;
3172 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3175 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3180 nesqp->ibqp_state = attr->qp_state;
3181 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3182 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3183 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3184 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3185 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3186 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3187 nesqp->iwarp_state);
3188 issue_disconnect = 1;
3190 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3191 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3192 nesqp->iwarp_state);
3196 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3197 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3198 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3199 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3200 issue_modify_qp = 1;
3202 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3203 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3204 issue_modify_qp = 1;
3206 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3207 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3208 issue_modify_qp = 1;
3210 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3211 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3212 issue_modify_qp = 1;
3215 if (nesqp->user_mode) {
3216 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3217 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3218 issue_modify_qp = 1;
3222 original_last_aeq = nesqp->last_aeq;
3223 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3225 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3230 if (issue_modify_qp) {
3231 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3232 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
3234 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3235 " failed for QP%u.\n",
3236 next_iwarp_state, nesqp->hwqp.qp_id);
3240 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3241 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3242 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3243 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3244 original_last_aeq, nesqp->last_aeq);
3246 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3249 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3250 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
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 /* this one is for the cm_disconnect thread */
3255 spin_lock_irqsave(&nesqp->lock, qplockflags);
3256 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3257 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3258 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3259 nes_cm_disconn(nesqp);
3261 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3262 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3265 spin_lock_irqsave(&nesqp->lock, qplockflags);
3267 /* These two are for the timer thread */
3268 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3269 nesqp->cm_id->add_ref(nesqp->cm_id);
3270 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3271 " need ae to finish up, original_last_aeq = 0x%04X."
3272 " last_aeq = 0x%04X, scheduling timer.\n",
3273 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3274 original_last_aeq, nesqp->last_aeq);
3275 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3277 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3279 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3280 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3281 " need ae to finish up, original_last_aeq = 0x%04X."
3282 " last_aeq = 0x%04X.\n",
3283 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3284 original_last_aeq, nesqp->last_aeq);
3288 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3289 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3290 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3291 original_last_aeq, nesqp->last_aeq);
3294 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3295 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3296 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3297 original_last_aeq, nesqp->last_aeq);
3302 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3303 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3310 * nes_muticast_attach
3312 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3314 nes_debug(NES_DBG_INIT, "\n");
3320 * nes_multicast_detach
3322 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3324 nes_debug(NES_DBG_INIT, "\n");
3332 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3333 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3334 struct ib_mad *in_mad, struct ib_mad *out_mad)
3336 nes_debug(NES_DBG_INIT, "\n");
3341 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3344 int total_payload_length = 0;
3345 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3346 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3347 ib_wr->sg_list[sge_index].addr);
3348 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3349 ib_wr->sg_list[sge_index].length);
3351 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3352 (ib_wr->sg_list[sge_index].lkey));
3354 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3356 total_payload_length += ib_wr->sg_list[sge_index].length;
3358 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3359 total_payload_length);
3360 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3361 total_payload_length);
3367 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3368 struct ib_send_wr **bad_wr)
3371 unsigned long flags = 0;
3372 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3373 struct nes_device *nesdev = nesvnic->nesdev;
3374 struct nes_qp *nesqp = to_nesqp(ibqp);
3375 struct nes_hw_qp_wqe *wqe;
3377 u32 qsize = nesqp->hwqp.sq_size;
3382 u32 total_payload_length;
3387 total_payload_length = 0;
3389 if (nesqp->ibqp_state > IB_QPS_RTS) {
3394 spin_lock_irqsave(&nesqp->lock, flags);
3396 head = nesqp->hwqp.sq_head;
3399 /* Check for QP error */
3400 if (nesqp->term_flags) {
3405 /* Check for SQ overflow */
3406 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3411 wqe = &nesqp->hwqp.sq_vbase[head];
3412 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3413 nesqp->hwqp.qp_id, wqe, head); */
3414 nes_fill_init_qp_wqe(wqe, nesqp, head);
3415 u64temp = (u64)(ib_wr->wr_id);
3416 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3418 switch (ib_wr->opcode) {
3420 if (ib_wr->send_flags & IB_SEND_SOLICITED) {
3421 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3423 wqe_misc = NES_IWARP_SQ_OP_SEND;
3425 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3429 if (ib_wr->send_flags & IB_SEND_FENCE) {
3430 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3432 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3433 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3434 (ib_wr->sg_list[0].length <= 64)) {
3435 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3436 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3437 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3438 ib_wr->sg_list[0].length);
3439 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3441 fill_wqe_sg_send(wqe, ib_wr, 1);
3445 case IB_WR_RDMA_WRITE:
3446 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3447 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3448 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3450 nesdev->nesadapter->max_sge);
3454 if (ib_wr->send_flags & IB_SEND_FENCE) {
3455 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3458 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3459 ib_wr->wr.rdma.rkey);
3460 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3461 ib_wr->wr.rdma.remote_addr);
3463 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3464 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3465 (ib_wr->sg_list[0].length <= 64)) {
3466 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3467 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3468 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3469 ib_wr->sg_list[0].length);
3470 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3472 fill_wqe_sg_send(wqe, ib_wr, 1);
3474 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3475 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3477 case IB_WR_RDMA_READ:
3478 /* iWARP only supports 1 sge for RDMA reads */
3479 if (ib_wr->num_sge > 1) {
3480 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3485 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3486 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3487 ib_wr->wr.rdma.remote_addr);
3488 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3489 ib_wr->wr.rdma.rkey);
3490 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3491 ib_wr->sg_list->length);
3492 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3493 ib_wr->sg_list->addr);
3494 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3495 ib_wr->sg_list->lkey);
3506 if (ib_wr->send_flags & IB_SEND_SIGNALED) {
3507 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3509 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3511 ib_wr = ib_wr->next;
3519 nesqp->hwqp.sq_head = head;
3522 counter = min(wqe_count, ((u32)255));
3523 wqe_count -= counter;
3524 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3525 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3528 spin_unlock_irqrestore(&nesqp->lock, flags);
3540 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3541 struct ib_recv_wr **bad_wr)
3544 unsigned long flags = 0;
3545 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3546 struct nes_device *nesdev = nesvnic->nesdev;
3547 struct nes_qp *nesqp = to_nesqp(ibqp);
3548 struct nes_hw_qp_wqe *wqe;
3551 u32 qsize = nesqp->hwqp.rq_size;
3555 u32 total_payload_length;
3557 if (nesqp->ibqp_state > IB_QPS_RTS)
3560 spin_lock_irqsave(&nesqp->lock, flags);
3562 head = nesqp->hwqp.rq_head;
3565 /* Check for QP error */
3566 if (nesqp->term_flags) {
3571 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3575 /* Check for RQ overflow */
3576 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3581 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3582 wqe = &nesqp->hwqp.rq_vbase[head];
3584 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3585 nesqp->hwqp.qp_id, wqe, head); */
3586 nes_fill_init_qp_wqe(wqe, nesqp, head);
3587 u64temp = (u64)(ib_wr->wr_id);
3588 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3590 total_payload_length = 0;
3591 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3592 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3593 ib_wr->sg_list[sge_index].addr);
3594 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3595 ib_wr->sg_list[sge_index].length);
3596 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3597 ib_wr->sg_list[sge_index].lkey);
3599 total_payload_length += ib_wr->sg_list[sge_index].length;
3601 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3602 total_payload_length);
3604 ib_wr = ib_wr->next;
3611 nesqp->hwqp.rq_head = head;
3614 counter = min(wqe_count, ((u32)255));
3615 wqe_count -= counter;
3616 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3619 spin_unlock_irqrestore(&nesqp->lock, flags);
3630 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3634 unsigned long flags = 0;
3635 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3636 struct nes_device *nesdev = nesvnic->nesdev;
3637 struct nes_cq *nescq = to_nescq(ibcq);
3638 struct nes_qp *nesqp;
3639 struct nes_hw_cqe cqe;
3646 u32 move_cq_head = 1;
3649 nes_debug(NES_DBG_CQ, "\n");
3651 spin_lock_irqsave(&nescq->lock, flags);
3653 head = nescq->hw_cq.cq_head;
3654 cq_size = nescq->hw_cq.cq_size;
3656 while (cqe_count < num_entries) {
3657 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3658 NES_CQE_VALID) == 0)
3662 * Make sure we read CQ entry contents *after*
3663 * we've checked the valid bit.
3667 cqe = nescq->hw_cq.cq_vbase[head];
3668 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3669 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3670 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3671 /* parse CQE, get completion context from WQE (either rq or sq) */
3672 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3676 nesqp = (struct nes_qp *)(unsigned long)u64temp;
3677 memset(entry, 0, sizeof *entry);
3678 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3679 entry->status = IB_WC_SUCCESS;
3681 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3682 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3683 entry->status = err_code & 0x0000ffff;
3685 /* The rest of the cqe's will be marked as flushed */
3686 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3687 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3688 NES_IWARP_CQE_MINOR_FLUSH);
3690 entry->status = IB_WC_WR_FLUSH_ERR;
3693 entry->qp = &nesqp->ibqp;
3694 entry->src_qp = nesqp->hwqp.qp_id;
3696 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3697 if (nesqp->skip_lsmm) {
3698 nesqp->skip_lsmm = 0;
3699 nesqp->hwqp.sq_tail++;
3702 /* Working on a SQ Completion*/
3703 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3704 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3705 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3706 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3707 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3708 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3710 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3711 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3712 case NES_IWARP_SQ_OP_RDMAW:
3713 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3714 entry->opcode = IB_WC_RDMA_WRITE;
3716 case NES_IWARP_SQ_OP_RDMAR:
3717 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3718 entry->opcode = IB_WC_RDMA_READ;
3719 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3720 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3722 case NES_IWARP_SQ_OP_SENDINV:
3723 case NES_IWARP_SQ_OP_SENDSEINV:
3724 case NES_IWARP_SQ_OP_SEND:
3725 case NES_IWARP_SQ_OP_SENDSE:
3726 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3727 entry->opcode = IB_WC_SEND;
3731 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3732 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3734 wq_tail = nesqp->hwqp.sq_tail;
3737 /* Working on a RQ Completion*/
3738 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3739 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3740 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3741 entry->opcode = IB_WC_RECV;
3743 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3744 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3746 wq_tail = nesqp->hwqp.rq_tail;
3750 entry->wr_id = wrid;
3756 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3757 if (++head >= cq_size)
3759 nescq->polled_completions++;
3761 if ((nescq->polled_completions > (cq_size / 2)) ||
3762 (nescq->polled_completions == 255)) {
3763 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3764 " are pending %u of %u.\n",
3765 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3766 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3767 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3768 nescq->polled_completions = 0;
3771 /* Update the wqe index and set status to flush */
3772 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3773 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3774 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3775 cpu_to_le32(wqe_index);
3776 move_cq_head = 1; /* ready for next pass */
3780 if (nescq->polled_completions) {
3781 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3782 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3783 nescq->polled_completions = 0;
3786 nescq->hw_cq.cq_head = head;
3787 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3788 cqe_count, nescq->hw_cq.cq_number);
3790 spin_unlock_irqrestore(&nescq->lock, flags);
3799 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3801 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3802 struct nes_device *nesdev = nesvnic->nesdev;
3803 struct nes_cq *nescq = to_nescq(ibcq);
3806 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3807 nescq->hw_cq.cq_number);
3809 cq_arm = nescq->hw_cq.cq_number;
3810 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3811 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3812 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3813 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3817 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3818 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3825 * nes_init_ofa_device
3827 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3829 struct nes_ib_device *nesibdev;
3830 struct nes_vnic *nesvnic = netdev_priv(netdev);
3831 struct nes_device *nesdev = nesvnic->nesdev;
3833 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3834 if (nesibdev == NULL) {
3837 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3838 nesibdev->ibdev.owner = THIS_MODULE;
3840 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3841 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3842 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3844 nesibdev->ibdev.uverbs_cmd_mask =
3845 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3846 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3847 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3848 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3849 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3850 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3851 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3852 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3853 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3854 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3855 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3856 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3857 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3858 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3859 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3860 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3861 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3862 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3863 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3864 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3865 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3866 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3868 nesibdev->ibdev.phys_port_cnt = 1;
3869 nesibdev->ibdev.num_comp_vectors = 1;
3870 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3871 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3872 nesibdev->ibdev.query_device = nes_query_device;
3873 nesibdev->ibdev.query_port = nes_query_port;
3874 nesibdev->ibdev.modify_port = nes_modify_port;
3875 nesibdev->ibdev.query_pkey = nes_query_pkey;
3876 nesibdev->ibdev.query_gid = nes_query_gid;
3877 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3878 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3879 nesibdev->ibdev.mmap = nes_mmap;
3880 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3881 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3882 nesibdev->ibdev.create_ah = nes_create_ah;
3883 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3884 nesibdev->ibdev.create_qp = nes_create_qp;
3885 nesibdev->ibdev.modify_qp = nes_modify_qp;
3886 nesibdev->ibdev.query_qp = nes_query_qp;
3887 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3888 nesibdev->ibdev.create_cq = nes_create_cq;
3889 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3890 nesibdev->ibdev.poll_cq = nes_poll_cq;
3891 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3892 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3893 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3894 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3895 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3896 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3897 nesibdev->ibdev.bind_mw = nes_bind_mw;
3899 nesibdev->ibdev.alloc_fmr = nes_alloc_fmr;
3900 nesibdev->ibdev.unmap_fmr = nes_unmap_fmr;
3901 nesibdev->ibdev.dealloc_fmr = nes_dealloc_fmr;
3902 nesibdev->ibdev.map_phys_fmr = nes_map_phys_fmr;
3904 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3905 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3906 nesibdev->ibdev.process_mad = nes_process_mad;
3908 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3909 nesibdev->ibdev.post_send = nes_post_send;
3910 nesibdev->ibdev.post_recv = nes_post_recv;
3912 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3913 if (nesibdev->ibdev.iwcm == NULL) {
3914 ib_dealloc_device(&nesibdev->ibdev);
3917 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3918 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3919 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3920 nesibdev->ibdev.iwcm->connect = nes_connect;
3921 nesibdev->ibdev.iwcm->accept = nes_accept;
3922 nesibdev->ibdev.iwcm->reject = nes_reject;
3923 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3924 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3931 * nes_destroy_ofa_device
3933 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3935 if (nesibdev == NULL)
3938 nes_unregister_ofa_device(nesibdev);
3940 kfree(nesibdev->ibdev.iwcm);
3941 ib_dealloc_device(&nesibdev->ibdev);
3946 * nes_register_ofa_device
3948 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3950 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3951 struct nes_device *nesdev = nesvnic->nesdev;
3952 struct nes_adapter *nesadapter = nesdev->nesadapter;
3955 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3960 /* Get the resources allocated to this device */
3961 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3962 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3963 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3964 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3966 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3967 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3971 device_remove_file(&nesibdev->ibdev.dev,
3972 nes_dev_attributes[i]);
3974 ib_unregister_device(&nesibdev->ibdev);
3979 nesvnic->of_device_registered = 1;
3986 * nes_unregister_ofa_device
3988 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3990 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3993 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3994 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3997 if (nesvnic->of_device_registered) {
3998 ib_unregister_device(&nesibdev->ibdev);
4001 nesvnic->of_device_registered = 0;