2 * Copyright (c) 2006 - 2008 NetEffect, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/random.h>
37 #include <linux/highmem.h>
38 #include <asm/byteorder.h>
40 #include <rdma/ib_verbs.h>
41 #include <rdma/iw_cm.h>
42 #include <rdma/ib_user_verbs.h>
46 #include <rdma/ib_umem.h>
48 atomic_t mod_qp_timouts;
50 atomic_t sw_qps_destroyed;
52 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
57 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) {
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_alloc;
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_alloc;
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->root_vpbl.leaf_vpbl = kzalloc(sizeof(*nesfmr->root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
395 if (!nesfmr->root_vpbl.leaf_vpbl) {
396 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
398 goto failed_leaf_vpbl_alloc;
401 nesfmr->leaf_pbl_cnt = nesfmr->nesmr.pbls_used-1;
402 nes_debug(NES_DBG_MR, "two level pbl, root_vpbl.pbl_vbase=%p"
403 " leaf_pbl_cnt=%d root_vpbl.leaf_vpbl=%p\n",
404 nesfmr->root_vpbl.pbl_vbase, nesfmr->leaf_pbl_cnt, nesfmr->root_vpbl.leaf_vpbl);
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);
525 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
527 failed_resource_alloc:
535 static int nes_dealloc_fmr(struct ib_fmr *ibfmr)
537 struct nes_mr *nesmr = to_nesmr_from_ibfmr(ibfmr);
538 struct nes_fmr *nesfmr = to_nesfmr(nesmr);
539 struct nes_vnic *nesvnic = to_nesvnic(ibfmr->device);
540 struct nes_device *nesdev = nesvnic->nesdev;
541 struct nes_mr temp_nesmr = *nesmr;
544 temp_nesmr.ibmw.device = ibfmr->device;
545 temp_nesmr.ibmw.pd = ibfmr->pd;
546 temp_nesmr.ibmw.rkey = ibfmr->rkey;
547 temp_nesmr.ibmw.uobject = NULL;
549 /* free the resources */
550 if (nesfmr->leaf_pbl_cnt == 0) {
551 /* single PBL case */
552 if (nesfmr->root_vpbl.pbl_vbase)
553 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.pbl_vbase,
554 nesfmr->root_vpbl.pbl_pbase);
556 for (i = 0; i < nesfmr->leaf_pbl_cnt; i++) {
557 pci_free_consistent(nesdev->pcidev, 4096, nesfmr->root_vpbl.leaf_vpbl[i].pbl_vbase,
558 nesfmr->root_vpbl.leaf_vpbl[i].pbl_pbase);
560 kfree(nesfmr->root_vpbl.leaf_vpbl);
561 pci_free_consistent(nesdev->pcidev, 8192, nesfmr->root_vpbl.pbl_vbase,
562 nesfmr->root_vpbl.pbl_pbase);
565 return nes_dealloc_mw(&temp_nesmr.ibmw);
572 static int nes_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
573 int list_len, u64 iova)
582 static int nes_unmap_fmr(struct list_head *ibfmr_list)
592 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
594 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
595 struct nes_device *nesdev = nesvnic->nesdev;
596 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
598 memset(props, 0, sizeof(*props));
599 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
601 props->fw_ver = nesdev->nesadapter->fw_ver;
602 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
603 props->vendor_id = nesdev->nesadapter->vendor_id;
604 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
605 props->hw_ver = nesdev->nesadapter->hw_rev;
606 props->max_mr_size = 0x80000000;
607 props->max_qp = nesibdev->max_qp;
608 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
609 props->max_sge = nesdev->nesadapter->max_sge;
610 props->max_cq = nesibdev->max_cq;
611 props->max_cqe = nesdev->nesadapter->max_cqe - 1;
612 props->max_mr = nesibdev->max_mr;
613 props->max_mw = nesibdev->max_mr;
614 props->max_pd = nesibdev->max_pd;
615 props->max_sge_rd = 1;
616 switch (nesdev->nesadapter->max_irrq_wr) {
618 props->max_qp_rd_atom = 1;
621 props->max_qp_rd_atom = 4;
624 props->max_qp_rd_atom = 16;
627 props->max_qp_rd_atom = 32;
630 props->max_qp_rd_atom = 0;
632 props->max_qp_init_rd_atom = props->max_qp_wr;
633 props->atomic_cap = IB_ATOMIC_NONE;
634 props->max_map_per_fmr = 1;
643 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
645 memset(props, 0, sizeof(*props));
647 props->max_mtu = IB_MTU_2048;
648 props->active_mtu = IB_MTU_2048;
653 props->state = IB_PORT_ACTIVE;
654 props->phys_state = 0;
655 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
656 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
657 props->gid_tbl_len = 1;
658 props->pkey_tbl_len = 1;
659 props->qkey_viol_cntr = 0;
660 props->active_width = IB_WIDTH_4X;
661 props->active_speed = 1;
662 props->max_msg_sz = 0x80000000;
671 static int nes_modify_port(struct ib_device *ibdev, u8 port,
672 int port_modify_mask, struct ib_port_modify *props)
681 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
691 static int nes_query_gid(struct ib_device *ibdev, u8 port,
692 int index, union ib_gid *gid)
694 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
696 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
697 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
704 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
705 * of all objects associated with a particular user-mode client.
707 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
708 struct ib_udata *udata)
710 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
711 struct nes_device *nesdev = nesvnic->nesdev;
712 struct nes_adapter *nesadapter = nesdev->nesadapter;
713 struct nes_alloc_ucontext_req req;
714 struct nes_alloc_ucontext_resp uresp;
715 struct nes_ucontext *nes_ucontext;
716 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
719 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
720 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
721 return ERR_PTR(-EINVAL);
724 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
725 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
726 req.userspace_ver, NES_ABI_USERSPACE_VER);
727 return ERR_PTR(-EINVAL);
731 memset(&uresp, 0, sizeof uresp);
733 uresp.max_qps = nesibdev->max_qp;
734 uresp.max_pds = nesibdev->max_pd;
735 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
736 uresp.virtwq = nesadapter->virtwq;
737 uresp.kernel_ver = NES_ABI_KERNEL_VER;
739 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
741 return ERR_PTR(-ENOMEM);
743 nes_ucontext->nesdev = nesdev;
744 nes_ucontext->mmap_wq_offset = uresp.max_pds;
745 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
746 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
750 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
752 return ERR_PTR(-EFAULT);
755 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
756 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
757 atomic_set(&nes_ucontext->usecnt, 1);
758 return &nes_ucontext->ibucontext;
763 * nes_dealloc_ucontext
765 static int nes_dealloc_ucontext(struct ib_ucontext *context)
767 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
768 /* struct nes_device *nesdev = nesvnic->nesdev; */
769 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
771 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
781 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
784 struct nes_vnic *nesvnic = to_nesvnic(context->device);
785 struct nes_device *nesdev = nesvnic->nesdev;
786 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
787 struct nes_ucontext *nes_ucontext;
788 struct nes_qp *nesqp;
790 nes_ucontext = to_nesucontext(context);
793 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
794 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
795 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
796 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
797 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
798 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
801 nesqp = nes_ucontext->mmap_nesqp[index];
803 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
806 if (remap_pfn_range(vma, vma->vm_start,
807 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
808 vma->vm_end - vma->vm_start,
809 vma->vm_page_prot)) {
810 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
813 vma->vm_private_data = nesqp;
816 index = vma->vm_pgoff;
817 if (!test_bit(index, nes_ucontext->allocated_doorbells))
820 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
821 if (io_remap_pfn_range(vma, vma->vm_start,
822 (nesdev->doorbell_start +
823 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
824 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
826 vma->vm_private_data = nes_ucontext;
837 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
838 struct ib_ucontext *context, struct ib_udata *udata)
840 struct nes_pd *nespd;
841 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
842 struct nes_device *nesdev = nesvnic->nesdev;
843 struct nes_adapter *nesadapter = nesdev->nesadapter;
844 struct nes_ucontext *nesucontext;
845 struct nes_alloc_pd_resp uresp;
849 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
850 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
851 atomic_read(&nesvnic->netdev->refcnt));
853 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
854 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
859 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
861 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
862 return ERR_PTR(-ENOMEM);
865 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
866 nespd, nesvnic->nesibdev->ibdev.name);
868 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
871 nesucontext = to_nesucontext(context);
872 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
873 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
874 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
875 nespd->mmap_db_index, nespd->pd_id);
876 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
877 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
878 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
880 return ERR_PTR(-ENOMEM);
883 uresp.pd_id = nespd->pd_id;
884 uresp.mmap_db_index = nespd->mmap_db_index;
885 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
886 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
888 return ERR_PTR(-EFAULT);
891 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
892 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
893 nesucontext->first_free_db = nespd->mmap_db_index + 1;
896 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
904 static int nes_dealloc_pd(struct ib_pd *ibpd)
906 struct nes_ucontext *nesucontext;
907 struct nes_pd *nespd = to_nespd(ibpd);
908 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
909 struct nes_device *nesdev = nesvnic->nesdev;
910 struct nes_adapter *nesadapter = nesdev->nesadapter;
912 if ((ibpd->uobject) && (ibpd->uobject->context)) {
913 nesucontext = to_nesucontext(ibpd->uobject->context);
914 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
915 nespd->mmap_db_index);
916 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
917 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
918 if (nesucontext->first_free_db > nespd->mmap_db_index) {
919 nesucontext->first_free_db = nespd->mmap_db_index;
923 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
924 nespd->pd_id, nespd);
925 nes_free_resource(nesadapter, nesadapter->allocated_pds,
926 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
936 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
938 return ERR_PTR(-ENOSYS);
945 static int nes_destroy_ah(struct ib_ah *ah)
952 * nes_get_encoded_size
954 static inline u8 nes_get_encoded_size(int *size)
960 } else if (*size <= 128) {
963 } else if (*size <= 512) {
967 return (encoded_size);
975 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
976 struct nes_vnic *nesvnic, int sq_size, int rq_size)
983 struct nes_device *nesdev = nesvnic->nesdev;
984 struct nes_adapter *nesadapter = nesdev->nesadapter;
989 pbl_entries = nespbl->pbl_size >> 3;
990 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
991 nespbl->pbl_size, pbl_entries,
992 (void *)nespbl->pbl_vbase,
993 (unsigned long) nespbl->pbl_pbase);
994 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
995 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
996 /* the first pbl to be fro the rq_vbase... */
997 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
998 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
999 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1000 if (!nespbl->page) {
1001 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
1006 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
1007 nesqp->page = nespbl->page;
1008 if (!nesqp->hwqp.sq_vbase) {
1009 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
1014 /* Now to get to sq.. we need to calculate how many */
1015 /* PBL entries were used by the rq.. */
1016 pbl += sq_pbl_entries;
1017 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
1018 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
1019 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
1021 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
1022 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
1023 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
1024 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1025 if (!nesadapter->free_256pbl) {
1026 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1028 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1029 kunmap(nesqp->page);
1033 nesadapter->free_256pbl--;
1034 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1036 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
1037 pblbuffer = nesqp->pbl_vbase;
1038 if (!nesqp->pbl_vbase) {
1039 /* memory allocated during nes_reg_user_mr() */
1040 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1043 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1044 nesadapter->free_256pbl++;
1045 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1046 kunmap(nesqp->page);
1049 memset(nesqp->pbl_vbase, 0, 256);
1050 /* fill in the page address in the pbl buffer.. */
1051 tpbl = pblbuffer + 16;
1052 pbl = (__le64 *)nespbl->pbl_vbase;
1053 while (sq_pbl_entries--)
1056 while (rq_pbl_entries--)
1059 /* done with memory allocated during nes_reg_user_mr() */
1060 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1064 nesqp->qp_mem_size =
1065 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1066 /* Round up to a multiple of a page */
1067 nesqp->qp_mem_size += PAGE_SIZE - 1;
1068 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1070 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1071 &nesqp->hwqp.q2_pbase);
1074 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1075 nesqp->pbl_vbase = NULL;
1076 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1077 nesadapter->free_256pbl++;
1078 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1079 kunmap(nesqp->page);
1082 nesqp->hwqp.q2_vbase = mem;
1084 memset(nesqp->hwqp.q2_vbase, 0, 256);
1085 nesqp->nesqp_context = mem;
1086 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1087 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1096 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1097 int sq_size, int rq_size)
1100 struct nes_device *nesdev = nesvnic->nesdev;
1102 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1103 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1104 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1105 256; /* this is Q2 */
1106 /* Round up to a multiple of a page */
1107 nesqp->qp_mem_size += PAGE_SIZE - 1;
1108 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1110 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1111 &nesqp->hwqp.sq_pbase);
1114 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1115 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1116 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1118 memset(mem, 0, nesqp->qp_mem_size);
1120 nesqp->hwqp.sq_vbase = mem;
1121 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1123 nesqp->hwqp.rq_vbase = mem;
1124 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1125 sizeof(struct nes_hw_qp_wqe) * sq_size;
1126 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1128 nesqp->hwqp.q2_vbase = mem;
1129 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1130 sizeof(struct nes_hw_qp_wqe) * rq_size;
1132 memset(nesqp->hwqp.q2_vbase, 0, 256);
1134 nesqp->nesqp_context = mem;
1135 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1136 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1142 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1144 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1145 struct nes_qp *nesqp, int virt_wqs)
1147 unsigned long flags;
1148 struct nes_adapter *nesadapter = nesdev->nesadapter;
1150 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1151 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1153 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1154 nesadapter->free_256pbl++;
1155 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1156 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1157 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1158 nesqp->pbl_vbase = NULL;
1159 kunmap(nesqp->page);
1167 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1168 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1172 struct nes_pd *nespd = to_nespd(ibpd);
1173 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1174 struct nes_device *nesdev = nesvnic->nesdev;
1175 struct nes_adapter *nesadapter = nesdev->nesadapter;
1176 struct nes_qp *nesqp;
1177 struct nes_cq *nescq;
1178 struct nes_ucontext *nes_ucontext;
1179 struct nes_hw_cqp_wqe *cqp_wqe;
1180 struct nes_cqp_request *cqp_request;
1181 struct nes_create_qp_req req;
1182 struct nes_create_qp_resp uresp;
1183 struct nes_pbl *nespbl = NULL;
1186 /* u32 counter = 0; */
1188 unsigned long flags;
1198 if (init_attr->create_flags)
1199 return ERR_PTR(-EINVAL);
1201 atomic_inc(&qps_created);
1202 switch (init_attr->qp_type) {
1204 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1205 init_attr->cap.max_inline_data = 0;
1207 init_attr->cap.max_inline_data = 64;
1209 sq_size = init_attr->cap.max_send_wr;
1210 rq_size = init_attr->cap.max_recv_wr;
1212 /* check if the encoded sizes are OK or not... */
1213 sq_encoded_size = nes_get_encoded_size(&sq_size);
1214 rq_encoded_size = nes_get_encoded_size(&rq_size);
1216 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1217 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1219 return ERR_PTR(-EINVAL);
1222 init_attr->cap.max_send_wr = sq_size -2;
1223 init_attr->cap.max_recv_wr = rq_size -1;
1224 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1226 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1227 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1229 return ERR_PTR(ret);
1232 /* Need 512 (actually now 1024) byte alignment on this structure */
1233 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1235 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1236 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1237 return ERR_PTR(-ENOMEM);
1239 u64nesqp = (unsigned long)mem;
1240 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1241 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1242 u64nesqp &= ~u64temp;
1243 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1244 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1245 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1246 nesqp->allocated_buffer = mem;
1249 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1250 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1251 kfree(nesqp->allocated_buffer);
1252 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1255 if (req.user_wqe_buffers) {
1258 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1259 nesqp->user_mode = 1;
1260 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1263 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1264 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1265 list_del(&nespbl->list);
1267 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1268 nespbl, nespbl->user_base);
1273 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1274 (long long unsigned int)req.user_wqe_buffers);
1275 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1276 kfree(nesqp->allocated_buffer);
1277 return ERR_PTR(-EFAULT);
1281 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1282 nesqp->mmap_sq_db_index =
1283 find_next_zero_bit(nes_ucontext->allocated_wqs,
1284 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1285 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1286 nespd->mmap_db_index); */
1287 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1288 nes_debug(NES_DBG_QP,
1289 "db index > max user regions, failing create QP\n");
1290 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1292 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1296 kfree(nesqp->allocated_buffer);
1297 return ERR_PTR(-ENOMEM);
1299 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1300 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1301 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1303 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1304 kfree(nesqp->allocated_buffer);
1305 return ERR_PTR(-EFAULT);
1308 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1309 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1311 nes_debug(NES_DBG_QP,
1312 "error geting qp mem code = %d\n", err);
1313 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1314 kfree(nesqp->allocated_buffer);
1315 return ERR_PTR(-ENOMEM);
1318 nesqp->hwqp.sq_size = sq_size;
1319 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1320 nesqp->hwqp.sq_head = 1;
1321 nesqp->hwqp.rq_size = rq_size;
1322 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1323 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1324 (void *)nesqp->nesqp_context_pbase);
1326 nesqp->hwqp.qp_id = qp_num;
1327 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1328 nesqp->nespd = nespd;
1330 nescq = to_nescq(init_attr->send_cq);
1331 nesqp->nesscq = nescq;
1332 nescq = to_nescq(init_attr->recv_cq);
1333 nesqp->nesrcq = nescq;
1335 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1336 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1337 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1338 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1339 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1340 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1341 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1342 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1343 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1344 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1345 u64temp = (u64)nesqp->hwqp.sq_pbase;
1346 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1347 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1351 u64temp = (u64)nesqp->hwqp.sq_pbase;
1352 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1353 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1354 u64temp = (u64)nesqp->hwqp.rq_pbase;
1355 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1356 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1358 u64temp = (u64)nesqp->pbl_pbase;
1359 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1360 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1363 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1364 nesvnic->next_qp_nic_index,
1365 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1366 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1367 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1368 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1369 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1370 nesvnic->next_qp_nic_index++;
1371 if ((nesvnic->next_qp_nic_index > 3) ||
1372 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1373 nesvnic->next_qp_nic_index = 0;
1375 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1377 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1378 u64temp = (u64)nesqp->hwqp.q2_pbase;
1379 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1380 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1381 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1382 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1383 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1384 ((((u32)nesadapter->max_irrq_wr) <<
1385 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1386 if (disable_mpa_crc) {
1387 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1388 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1393 cqp_request = nes_get_cqp_request(nesdev);
1394 if (cqp_request == NULL) {
1395 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1396 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1397 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1398 kfree(nesqp->allocated_buffer);
1399 return ERR_PTR(-ENOMEM);
1401 cqp_request->waiting = 1;
1402 cqp_wqe = &cqp_request->cqp_wqe;
1405 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1406 NES_CQP_QP_IWARP_STATE_IDLE;
1408 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1409 NES_CQP_QP_IWARP_STATE_IDLE;
1411 opcode |= NES_CQP_QP_CQS_VALID;
1412 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1413 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1414 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1416 u64temp = (u64)nesqp->nesqp_context_pbase;
1417 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1419 atomic_set(&cqp_request->refcount, 2);
1420 nes_post_cqp_request(nesdev, cqp_request);
1423 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1425 ret = wait_event_timeout(cqp_request->waitq,
1426 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1427 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1428 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1429 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1430 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1431 cqp_request->major_code, cqp_request->minor_code);
1432 if ((!ret) || (cqp_request->major_code)) {
1433 nes_put_cqp_request(nesdev, cqp_request);
1434 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1435 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1436 kfree(nesqp->allocated_buffer);
1438 return ERR_PTR(-ETIME);
1440 return ERR_PTR(-EIO);
1444 nes_put_cqp_request(nesdev, cqp_request);
1446 if (ibpd->uobject) {
1447 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1448 uresp.actual_sq_size = sq_size;
1449 uresp.actual_rq_size = rq_size;
1450 uresp.qp_id = nesqp->hwqp.qp_id;
1451 uresp.nes_drv_opt = nes_drv_opt;
1452 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1453 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1454 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1455 kfree(nesqp->allocated_buffer);
1456 return ERR_PTR(-EFAULT);
1460 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1461 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1462 spin_lock_init(&nesqp->lock);
1463 init_waitqueue_head(&nesqp->state_waitq);
1464 init_waitqueue_head(&nesqp->kick_waitq);
1465 nes_add_ref(&nesqp->ibqp);
1468 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1469 return ERR_PTR(-EINVAL);
1472 /* update the QP table */
1473 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1474 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1475 atomic_read(&nesvnic->netdev->refcnt));
1477 return &nesqp->ibqp;
1484 static int nes_destroy_qp(struct ib_qp *ibqp)
1486 struct nes_qp *nesqp = to_nesqp(ibqp);
1487 /* struct nes_vnic *nesvnic = to_nesvnic(ibqp->device); */
1488 struct nes_ucontext *nes_ucontext;
1489 struct ib_qp_attr attr;
1490 struct iw_cm_id *cm_id;
1491 struct iw_cm_event cm_event;
1494 atomic_inc(&sw_qps_destroyed);
1495 nesqp->destroyed = 1;
1497 /* Blow away the connection if it exists. */
1498 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1499 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1500 attr.qp_state = IB_QPS_ERR;
1501 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1504 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1505 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1506 cm_id = nesqp->cm_id;
1507 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1508 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1509 cm_event.local_addr = cm_id->local_addr;
1510 cm_event.remote_addr = cm_id->remote_addr;
1511 cm_event.private_data = NULL;
1512 cm_event.private_data_len = 0;
1514 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1515 "QP%u. cm_id = %p, refcount = %u. \n",
1516 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1518 cm_id->rem_ref(cm_id);
1519 ret = cm_id->event_handler(cm_id, &cm_event);
1521 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1525 if (nesqp->user_mode) {
1526 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1527 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1528 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1529 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1530 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1531 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1534 if (nesqp->pbl_pbase)
1535 kunmap(nesqp->page);
1538 nes_rem_ref(&nesqp->ibqp);
1546 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1548 struct ib_ucontext *context, struct ib_udata *udata)
1551 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1552 struct nes_device *nesdev = nesvnic->nesdev;
1553 struct nes_adapter *nesadapter = nesdev->nesadapter;
1554 struct nes_cq *nescq;
1555 struct nes_ucontext *nes_ucontext = NULL;
1556 struct nes_cqp_request *cqp_request;
1558 struct nes_hw_cqp_wqe *cqp_wqe;
1559 struct nes_pbl *nespbl = NULL;
1560 struct nes_create_cq_req req;
1561 struct nes_create_cq_resp resp;
1564 u32 pbl_entries = 1;
1566 unsigned long flags;
1569 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1570 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1572 return ERR_PTR(err);
1575 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1577 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1578 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1579 return ERR_PTR(-ENOMEM);
1582 nescq->hw_cq.cq_size = max(entries + 1, 5);
1583 nescq->hw_cq.cq_number = cq_num;
1584 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1588 nes_ucontext = to_nesucontext(context);
1589 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1590 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1592 return ERR_PTR(-EFAULT);
1594 nesvnic->mcrq_ucontext = nes_ucontext;
1595 nes_ucontext->mcrqf = req.mcrqf;
1596 if (nes_ucontext->mcrqf) {
1597 if (nes_ucontext->mcrqf & 0x80000000)
1598 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 12 + (nes_ucontext->mcrqf & 0xf) - 1;
1599 else if (nes_ucontext->mcrqf & 0x40000000)
1600 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1602 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1603 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1605 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1606 (unsigned long)req.user_cq_buffer, entries);
1608 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1609 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1610 list_del(&nespbl->list);
1612 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1618 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1620 return ERR_PTR(-EFAULT);
1623 pbl_entries = nespbl->pbl_size >> 3;
1624 nescq->cq_mem_size = 0;
1626 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1627 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1628 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1630 /* allocate the physical buffer space */
1631 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1632 &nescq->hw_cq.cq_pbase);
1634 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1635 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1637 return ERR_PTR(-ENOMEM);
1640 memset(mem, 0, nescq->cq_mem_size);
1641 nescq->hw_cq.cq_vbase = mem;
1642 nescq->hw_cq.cq_head = 0;
1643 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1644 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1645 (u32)nescq->hw_cq.cq_pbase);
1648 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1649 spin_lock_init(&nescq->lock);
1651 /* send CreateCQ request to CQP */
1652 cqp_request = nes_get_cqp_request(nesdev);
1653 if (cqp_request == NULL) {
1654 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1656 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1657 nescq->hw_cq.cq_pbase);
1658 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1660 return ERR_PTR(-ENOMEM);
1662 cqp_request->waiting = 1;
1663 cqp_wqe = &cqp_request->cqp_wqe;
1665 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1666 NES_CQP_CQ_CHK_OVERFLOW |
1667 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1669 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1671 if (pbl_entries != 1) {
1672 if (pbl_entries > 32) {
1674 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1675 if (nesadapter->free_4kpbl == 0) {
1676 if (cqp_request->dynamic) {
1677 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1680 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1681 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1684 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1685 nescq->hw_cq.cq_pbase);
1686 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1688 return ERR_PTR(-ENOMEM);
1690 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1691 nescq->virtual_cq = 2;
1692 nesadapter->free_4kpbl--;
1695 /* use 256 byte pbl */
1696 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1697 if (nesadapter->free_256pbl == 0) {
1698 if (cqp_request->dynamic) {
1699 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1702 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1703 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1706 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1707 nescq->hw_cq.cq_pbase);
1708 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1710 return ERR_PTR(-ENOMEM);
1712 opcode |= NES_CQP_CQ_VIRT;
1713 nescq->virtual_cq = 1;
1714 nesadapter->free_256pbl--;
1719 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1721 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1722 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1723 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1724 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1727 if (pbl_entries != 1)
1728 u64temp = (u64)nespbl->pbl_pbase;
1730 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1731 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1732 nes_ucontext->mmap_db_index[0]);
1734 u64temp = (u64)nescq->hw_cq.cq_pbase;
1735 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1737 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1738 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1739 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1740 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1741 cpu_to_le32((u32)(u64temp >> 1));
1742 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1743 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1745 atomic_set(&cqp_request->refcount, 2);
1746 nes_post_cqp_request(nesdev, cqp_request);
1749 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1750 nescq->hw_cq.cq_number);
1751 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1752 NES_EVENT_TIMEOUT * 2);
1753 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1754 nescq->hw_cq.cq_number, ret);
1755 if ((!ret) || (cqp_request->major_code)) {
1756 nes_put_cqp_request(nesdev, cqp_request);
1758 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1759 nescq->hw_cq.cq_pbase);
1760 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1762 return ERR_PTR(-EIO);
1764 nes_put_cqp_request(nesdev, cqp_request);
1767 /* free the nespbl */
1768 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1771 resp.cq_id = nescq->hw_cq.cq_number;
1772 resp.cq_size = nescq->hw_cq.cq_size;
1773 resp.mmap_db_index = 0;
1774 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1775 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1777 return ERR_PTR(-EFAULT);
1781 return &nescq->ibcq;
1788 static int nes_destroy_cq(struct ib_cq *ib_cq)
1790 struct nes_cq *nescq;
1791 struct nes_device *nesdev;
1792 struct nes_vnic *nesvnic;
1793 struct nes_adapter *nesadapter;
1794 struct nes_hw_cqp_wqe *cqp_wqe;
1795 struct nes_cqp_request *cqp_request;
1796 unsigned long flags;
1803 nescq = to_nescq(ib_cq);
1804 nesvnic = to_nesvnic(ib_cq->device);
1805 nesdev = nesvnic->nesdev;
1806 nesadapter = nesdev->nesadapter;
1808 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1810 /* Send DestroyCQ request to CQP */
1811 cqp_request = nes_get_cqp_request(nesdev);
1812 if (cqp_request == NULL) {
1813 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1816 cqp_request->waiting = 1;
1817 cqp_wqe = &cqp_request->cqp_wqe;
1818 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1819 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1820 if (nescq->virtual_cq == 1) {
1821 nesadapter->free_256pbl++;
1822 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1823 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1824 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1826 } else if (nescq->virtual_cq == 2) {
1827 nesadapter->free_4kpbl++;
1828 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1829 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1830 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1832 opcode |= NES_CQP_CQ_4KB_CHUNK;
1835 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1837 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1838 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1839 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1840 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1841 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1842 atomic_set(&cqp_request->refcount, 2);
1843 nes_post_cqp_request(nesdev, cqp_request);
1846 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1847 nescq->hw_cq.cq_number);
1848 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1850 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1851 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1852 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1853 cqp_request->minor_code);
1855 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1856 nescq->hw_cq.cq_number);
1858 } else if (cqp_request->major_code) {
1859 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1860 nescq->hw_cq.cq_number);
1865 nes_put_cqp_request(nesdev, cqp_request);
1867 if (nescq->cq_mem_size)
1868 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1869 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1879 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1880 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1881 dma_addr_t single_buffer, u16 pbl_count, u16 residual_page_count,
1882 int acc, u64 *iova_start)
1884 struct nes_hw_cqp_wqe *cqp_wqe;
1885 struct nes_cqp_request *cqp_request;
1886 unsigned long flags;
1888 struct nes_adapter *nesadapter = nesdev->nesadapter;
1893 /* Register the region with the adapter */
1894 cqp_request = nes_get_cqp_request(nesdev);
1895 if (cqp_request == NULL) {
1896 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1899 cqp_request->waiting = 1;
1900 cqp_wqe = &cqp_request->cqp_wqe;
1902 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1903 /* track PBL resources */
1904 if (pbl_count != 0) {
1905 if (pbl_count > 1) {
1907 if ((pbl_count+1) > nesadapter->free_4kpbl) {
1908 nes_debug(NES_DBG_MR, "Out of 4KB Pbls for two level request.\n");
1909 if (cqp_request->dynamic) {
1910 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1913 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1914 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1918 nesadapter->free_4kpbl -= pbl_count+1;
1920 } else if (residual_page_count > 32) {
1921 if (pbl_count > nesadapter->free_4kpbl) {
1922 nes_debug(NES_DBG_MR, "Out of 4KB Pbls.\n");
1923 if (cqp_request->dynamic) {
1924 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1927 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1928 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1932 nesadapter->free_4kpbl -= pbl_count;
1935 if (pbl_count > nesadapter->free_256pbl) {
1936 nes_debug(NES_DBG_MR, "Out of 256B Pbls.\n");
1937 if (cqp_request->dynamic) {
1938 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1941 list_add_tail(&cqp_request->list, &nesdev->cqp_avail_reqs);
1942 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1946 nesadapter->free_256pbl -= pbl_count;
1951 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1953 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1954 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1955 if (acc & IB_ACCESS_LOCAL_WRITE)
1956 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1957 if (acc & IB_ACCESS_REMOTE_WRITE)
1958 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1959 if (acc & IB_ACCESS_REMOTE_READ)
1960 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1961 if (acc & IB_ACCESS_MW_BIND)
1962 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1964 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1965 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1966 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1967 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1969 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1970 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1971 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1972 cpu_to_le32(nespd->pd_id & 0x00007fff);
1973 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1975 if (pbl_count == 0) {
1976 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1978 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1979 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
1980 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX,
1981 (((pbl_count - 1) * 4096) + (residual_page_count*8)));
1983 if ((pbl_count > 1) || (residual_page_count > 32))
1984 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1988 atomic_set(&cqp_request->refcount, 2);
1989 nes_post_cqp_request(nesdev, cqp_request);
1992 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1994 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1995 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1996 stag, ret, cqp_request->major_code, cqp_request->minor_code);
1997 major_code = cqp_request->major_code;
1998 nes_put_cqp_request(nesdev, cqp_request);
2002 else if (major_code)
2014 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2015 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2019 struct nes_pd *nespd = to_nespd(ib_pd);
2020 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2021 struct nes_device *nesdev = nesvnic->nesdev;
2022 struct nes_adapter *nesadapter = nesdev->nesadapter;
2023 struct nes_mr *nesmr;
2025 struct nes_vpbl vpbl;
2026 struct nes_root_vpbl root_vpbl;
2030 u32 next_stag_index = 0;
2032 u32 root_pbl_index = 0;
2033 u32 cur_pbl_index = 0;
2034 int err = 0, pbl_depth = 0;
2042 vpbl.pbl_vbase = NULL;
2043 root_vpbl.pbl_vbase = NULL;
2044 root_vpbl.pbl_pbase = 0;
2046 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2047 stag_key = (u8)next_stag_index;
2051 next_stag_index >>= 8;
2052 next_stag_index %= nesadapter->max_mr;
2053 if (num_phys_buf > (1024*512)) {
2054 return ERR_PTR(-E2BIG);
2057 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2058 &stag_index, &next_stag_index);
2060 return ERR_PTR(err);
2063 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2065 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2066 return ERR_PTR(-ENOMEM);
2069 for (i = 0; i < num_phys_buf; i++) {
2071 if ((i & 0x01FF) == 0) {
2072 if (root_pbl_index == 1) {
2073 /* Allocate the root PBL */
2074 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2075 &root_vpbl.pbl_pbase);
2076 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2077 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2078 if (!root_vpbl.pbl_vbase) {
2079 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2081 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2083 return ERR_PTR(-ENOMEM);
2085 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2086 if (!root_vpbl.leaf_vpbl) {
2087 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2088 root_vpbl.pbl_pbase);
2089 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2091 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2093 return ERR_PTR(-ENOMEM);
2095 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2096 root_vpbl.pbl_vbase[0].pa_high =
2097 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2098 root_vpbl.leaf_vpbl[0] = vpbl;
2100 /* Allocate a 4K buffer for the PBL */
2101 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2103 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2104 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2105 if (!vpbl.pbl_vbase) {
2106 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2107 ibmr = ERR_PTR(-ENOMEM);
2111 /* Fill in the root table */
2112 if (1 <= root_pbl_index) {
2113 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2114 cpu_to_le32((u32)vpbl.pbl_pbase);
2115 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2116 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2117 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2122 if (buffer_list[i].addr & ~PAGE_MASK) {
2123 /* TODO: Unwind allocated buffers */
2124 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2125 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2126 (unsigned int) buffer_list[i].addr);
2127 ibmr = ERR_PTR(-EINVAL);
2132 if (!buffer_list[i].size) {
2133 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2134 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2135 ibmr = ERR_PTR(-EINVAL);
2140 region_length += buffer_list[i].size;
2141 if ((i != 0) && (single_page)) {
2142 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2145 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr);
2146 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2147 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2150 stag = stag_index << 8;
2152 stag += (u32)stag_key;
2154 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2155 " length = 0x%016lX, index = 0x%08X\n",
2156 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2158 region_length -= (*iova_start)&PAGE_MASK;
2160 /* Make the leaf PBL the root if only one PBL */
2161 if (root_pbl_index == 1) {
2162 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2168 pbl_count = root_pbl_index;
2170 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2171 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start);
2174 nesmr->ibmr.rkey = stag;
2175 nesmr->ibmr.lkey = stag;
2176 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2177 ibmr = &nesmr->ibmr;
2178 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2179 nesmr->pbls_used = pbl_count;
2180 if (pbl_count > 1) {
2185 ibmr = ERR_PTR(-ENOMEM);
2189 /* free the resources */
2190 if (root_pbl_index == 1) {
2191 /* single PBL case */
2192 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2194 for (i=0; i<root_pbl_index; i++) {
2195 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2196 root_vpbl.leaf_vpbl[i].pbl_pbase);
2198 kfree(root_vpbl.leaf_vpbl);
2199 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2200 root_vpbl.pbl_pbase);
2210 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2212 struct ib_phys_buf bl;
2215 nes_debug(NES_DBG_MR, "\n");
2217 bl.size = (u64)0xffffffffffULL;
2219 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2226 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2227 u64 virt, int acc, struct ib_udata *udata)
2232 dma_addr_t last_dma_addr = 0;
2233 dma_addr_t first_dma_addr = 0;
2234 struct nes_pd *nespd = to_nespd(pd);
2235 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2236 struct nes_device *nesdev = nesvnic->nesdev;
2237 struct nes_adapter *nesadapter = nesdev->nesadapter;
2238 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2239 struct ib_umem_chunk *chunk;
2240 struct nes_ucontext *nes_ucontext;
2241 struct nes_pbl *nespbl;
2242 struct nes_mr *nesmr;
2243 struct ib_umem *region;
2244 struct nes_mem_reg_req req;
2245 struct nes_vpbl vpbl;
2246 struct nes_root_vpbl root_vpbl;
2247 int nmap_index, page_index;
2249 int err, pbl_depth = 0;
2254 u32 next_stag_index;
2256 u32 root_pbl_index = 0;
2257 u32 cur_pbl_index = 0;
2263 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
2264 if (IS_ERR(region)) {
2265 return (struct ib_mr *)region;
2268 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2269 " offset = %u, page size = %u.\n",
2270 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2271 region->offset, region->page_size);
2273 skip_pages = ((u32)region->offset) >> 12;
2275 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2276 return ERR_PTR(-EFAULT);
2277 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2279 switch (req.reg_type) {
2280 case IWNES_MEMREG_TYPE_MEM:
2283 vpbl.pbl_vbase = NULL;
2284 root_vpbl.pbl_vbase = NULL;
2285 root_vpbl.pbl_pbase = 0;
2287 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2288 stag_key = (u8)next_stag_index;
2290 driver_key = next_stag_index & 0x70000000;
2292 next_stag_index >>= 8;
2293 next_stag_index %= nesadapter->max_mr;
2295 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2296 nesadapter->max_mr, &stag_index, &next_stag_index);
2298 ib_umem_release(region);
2299 return ERR_PTR(err);
2302 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2304 ib_umem_release(region);
2305 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2306 return ERR_PTR(-ENOMEM);
2308 nesmr->region = region;
2310 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2311 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2312 chunk->nents, chunk->nmap);
2313 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2314 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2315 ib_umem_release(region);
2316 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2317 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2318 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2319 ibmr = ERR_PTR(-EINVAL);
2321 goto reg_user_mr_err;
2324 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2325 ib_umem_release(region);
2326 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2328 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2329 ibmr = ERR_PTR(-EINVAL);
2331 goto reg_user_mr_err;
2334 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2335 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2336 region_length -= skip_pages << 12;
2337 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2339 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2341 if ((page_count&0x01FF) == 0) {
2342 if (page_count >= 1024 * 512) {
2343 ib_umem_release(region);
2344 nes_free_resource(nesadapter,
2345 nesadapter->allocated_mrs, stag_index);
2347 ibmr = ERR_PTR(-E2BIG);
2348 goto reg_user_mr_err;
2350 if (root_pbl_index == 1) {
2351 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2352 8192, &root_vpbl.pbl_pbase);
2353 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2354 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2355 if (!root_vpbl.pbl_vbase) {
2356 ib_umem_release(region);
2357 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2359 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2362 ibmr = ERR_PTR(-ENOMEM);
2363 goto reg_user_mr_err;
2365 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2367 if (!root_vpbl.leaf_vpbl) {
2368 ib_umem_release(region);
2369 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2370 root_vpbl.pbl_pbase);
2371 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2373 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2376 ibmr = ERR_PTR(-ENOMEM);
2377 goto reg_user_mr_err;
2379 root_vpbl.pbl_vbase[0].pa_low =
2380 cpu_to_le32((u32)vpbl.pbl_pbase);
2381 root_vpbl.pbl_vbase[0].pa_high =
2382 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2383 root_vpbl.leaf_vpbl[0] = vpbl;
2385 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2387 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2388 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2389 if (!vpbl.pbl_vbase) {
2390 ib_umem_release(region);
2391 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2392 ibmr = ERR_PTR(-ENOMEM);
2394 goto reg_user_mr_err;
2396 if (1 <= root_pbl_index) {
2397 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2398 cpu_to_le32((u32)vpbl.pbl_pbase);
2399 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2400 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2401 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2407 if (page_count != 0) {
2408 if ((last_dma_addr+4096) !=
2409 (sg_dma_address(&chunk->page_list[nmap_index])+
2412 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2415 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2417 last_dma_addr = first_dma_addr;
2421 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2422 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2423 (page_index*4096)));
2424 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2425 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2426 (page_index*4096))) >> 32)));
2433 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2434 " stag_key=0x%08x\n",
2435 stag_index, driver_key, stag_key);
2436 stag = stag_index << 8;
2438 stag += (u32)stag_key;
2444 /* Make the leaf PBL the root if only one PBL */
2445 if (root_pbl_index == 1) {
2446 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2452 pbl_count = root_pbl_index;
2455 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2456 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2457 stag, (unsigned int)iova_start,
2458 (unsigned int)region_length, stag_index,
2459 (unsigned long long)region->length, pbl_count);
2460 ret = nes_reg_mr( nesdev, nespd, stag, region->length, &root_vpbl,
2461 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc, &iova_start);
2463 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2466 nesmr->ibmr.rkey = stag;
2467 nesmr->ibmr.lkey = stag;
2468 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2469 ibmr = &nesmr->ibmr;
2470 nesmr->pbl_4k = ((pbl_count > 1) || (cur_pbl_index > 32)) ? 1 : 0;
2471 nesmr->pbls_used = pbl_count;
2472 if (pbl_count > 1) {
2476 ib_umem_release(region);
2478 ibmr = ERR_PTR(-ENOMEM);
2482 /* free the resources */
2483 if (root_pbl_index == 1) {
2484 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2487 for (page_index=0; page_index<root_pbl_index; page_index++) {
2488 pci_free_consistent(nesdev->pcidev, 4096,
2489 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2490 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2492 kfree(root_vpbl.leaf_vpbl);
2493 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2494 root_vpbl.pbl_pbase);
2497 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2500 case IWNES_MEMREG_TYPE_QP:
2501 case IWNES_MEMREG_TYPE_CQ:
2502 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2504 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2505 ib_umem_release(region);
2506 return ERR_PTR(-ENOMEM);
2508 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2510 ib_umem_release(region);
2512 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2513 return ERR_PTR(-ENOMEM);
2515 nesmr->region = region;
2516 nes_ucontext = to_nesucontext(pd->uobject->context);
2517 pbl_depth = region->length >> 12;
2518 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2519 nespbl->pbl_size = pbl_depth*sizeof(u64);
2520 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2521 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2523 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2526 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2527 nespbl->pbl_size, pbl_depth);
2528 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2529 &nespbl->pbl_pbase);
2531 ib_umem_release(region);
2534 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2535 return ERR_PTR(-ENOMEM);
2538 nespbl->pbl_vbase = (u64 *)pbl;
2539 nespbl->user_base = start;
2540 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2541 " pbl_vbase=%p user_base=0x%lx\n",
2542 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2543 (void *) nespbl->pbl_vbase, nespbl->user_base);
2545 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2546 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2547 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2548 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2549 nespbl->page = sg_page(&chunk->page_list[0]);
2550 for (page_index=0; page_index<chunk_pages; page_index++) {
2551 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2552 (sg_dma_address(&chunk->page_list[nmap_index])+
2553 (page_index*4096)));
2554 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2555 (sg_dma_address(&chunk->page_list[nmap_index])+
2556 (page_index*4096)))>>32);
2557 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2558 (unsigned long long)*pbl,
2559 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2564 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2565 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2567 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2569 nesmr->ibmr.rkey = -1;
2570 nesmr->ibmr.lkey = -1;
2571 nesmr->mode = req.reg_type;
2572 return &nesmr->ibmr;
2575 return ERR_PTR(-ENOSYS);
2582 static int nes_dereg_mr(struct ib_mr *ib_mr)
2584 struct nes_mr *nesmr = to_nesmr(ib_mr);
2585 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2586 struct nes_device *nesdev = nesvnic->nesdev;
2587 struct nes_adapter *nesadapter = nesdev->nesadapter;
2588 struct nes_hw_cqp_wqe *cqp_wqe;
2589 struct nes_cqp_request *cqp_request;
2590 unsigned long flags;
2595 if (nesmr->region) {
2596 ib_umem_release(nesmr->region);
2598 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2603 /* Deallocate the region with the adapter */
2605 cqp_request = nes_get_cqp_request(nesdev);
2606 if (cqp_request == NULL) {
2607 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2610 cqp_request->waiting = 1;
2611 cqp_wqe = &cqp_request->cqp_wqe;
2613 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2614 if (nesmr->pbls_used != 0) {
2615 if (nesmr->pbl_4k) {
2616 nesadapter->free_4kpbl += nesmr->pbls_used;
2617 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
2618 printk(KERN_ERR PFX "free 4KB PBLs(%u) has exceeded the max(%u)\n",
2619 nesadapter->free_4kpbl, nesadapter->max_4kpbl);
2622 nesadapter->free_256pbl += nesmr->pbls_used;
2623 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
2624 printk(KERN_ERR PFX "free 256B PBLs(%u) has exceeded the max(%u)\n",
2625 nesadapter->free_256pbl, nesadapter->max_256pbl);
2630 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2631 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2632 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2633 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2634 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2635 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2637 atomic_set(&cqp_request->refcount, 2);
2638 nes_post_cqp_request(nesdev, cqp_request);
2641 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2642 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2644 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2645 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2646 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2648 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2649 (ib_mr->rkey & 0x0fffff00) >> 8);
2653 major_code = cqp_request->major_code;
2654 minor_code = cqp_request->minor_code;
2656 nes_put_cqp_request(nesdev, cqp_request);
2659 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2660 " ib_mr=%p, rkey = 0x%08X\n",
2661 ib_mr, ib_mr->rkey);
2663 } else if (major_code) {
2664 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2665 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2666 major_code, minor_code, ib_mr, ib_mr->rkey);
2676 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2679 struct nes_ib_device *nesibdev =
2680 container_of(dev, struct nes_ib_device, ibdev.dev);
2681 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2683 nes_debug(NES_DBG_INIT, "\n");
2684 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2691 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2694 struct nes_ib_device *nesibdev =
2695 container_of(dev, struct nes_ib_device, ibdev.dev);
2696 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2698 nes_debug(NES_DBG_INIT, "\n");
2699 return sprintf(buf, "%x.%x.%x\n",
2700 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 32),
2701 (int)(nesvnic->nesdev->nesadapter->fw_ver >> 16) & 0xffff,
2702 (int)(nesvnic->nesdev->nesadapter->fw_ver & 0xffff));
2709 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2712 nes_debug(NES_DBG_INIT, "\n");
2713 return sprintf(buf, "NES020\n");
2720 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2723 nes_debug(NES_DBG_INIT, "\n");
2724 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2728 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2729 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2730 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2731 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2733 static struct device_attribute *nes_dev_attributes[] = {
2744 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2745 int attr_mask, struct ib_qp_init_attr *init_attr)
2747 struct nes_qp *nesqp = to_nesqp(ibqp);
2749 nes_debug(NES_DBG_QP, "\n");
2751 attr->qp_access_flags = 0;
2752 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2753 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2754 attr->cap.max_recv_sge = 1;
2755 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2756 init_attr->cap.max_inline_data = 0;
2758 init_attr->cap.max_inline_data = 64;
2761 init_attr->event_handler = nesqp->ibqp.event_handler;
2762 init_attr->qp_context = nesqp->ibqp.qp_context;
2763 init_attr->send_cq = nesqp->ibqp.send_cq;
2764 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2765 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2766 init_attr->cap = attr->cap;
2775 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2776 u32 next_iwarp_state, u32 wait_completion)
2778 struct nes_hw_cqp_wqe *cqp_wqe;
2779 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2780 /* struct iw_cm_event cm_event; */
2781 struct nes_cqp_request *cqp_request;
2785 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2786 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2788 cqp_request = nes_get_cqp_request(nesdev);
2789 if (cqp_request == NULL) {
2790 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2793 if (wait_completion) {
2794 cqp_request->waiting = 1;
2796 cqp_request->waiting = 0;
2798 cqp_wqe = &cqp_request->cqp_wqe;
2800 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2801 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2802 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2803 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2804 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2805 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2806 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2808 atomic_set(&cqp_request->refcount, 2);
2809 nes_post_cqp_request(nesdev, cqp_request);
2812 if (wait_completion) {
2813 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2814 nesqp->hwqp.qp_id); */
2815 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2817 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2818 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2819 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2820 major_code = cqp_request->major_code;
2822 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2823 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2824 nesqp->hwqp.qp_id, cqp_request->major_code,
2825 cqp_request->minor_code, next_iwarp_state);
2828 nes_put_cqp_request(nesdev, cqp_request);
2832 else if (major_code)
2845 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2846 int attr_mask, struct ib_udata *udata)
2848 struct nes_qp *nesqp = to_nesqp(ibqp);
2849 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2850 struct nes_device *nesdev = nesvnic->nesdev;
2853 u32 next_iwarp_state = 0;
2855 unsigned long qplockflags;
2857 u16 original_last_aeq;
2858 u8 issue_modify_qp = 0;
2859 u8 issue_disconnect = 0;
2862 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2863 " iwarp_state=0x%X, refcount=%d\n",
2864 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2865 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2867 spin_lock_irqsave(&nesqp->lock, qplockflags);
2869 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2870 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2871 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2872 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2874 if (attr_mask & IB_QP_STATE) {
2875 switch (attr->qp_state) {
2877 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2879 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2880 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2883 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2884 issue_modify_qp = 1;
2887 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2889 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2890 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2893 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2894 issue_modify_qp = 1;
2897 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2899 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2900 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2903 if (nesqp->cm_id == NULL) {
2904 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2905 nesqp->hwqp.qp_id );
2906 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2909 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2910 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2911 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2912 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2913 issue_modify_qp = 1;
2914 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2915 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2916 nesqp->hte_added = 1;
2919 issue_modify_qp = 1;
2920 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2921 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2922 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2923 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2926 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2927 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2928 " ignored due to current iWARP state\n",
2930 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2933 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2934 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2935 " already done based on hw state.\n",
2937 issue_modify_qp = 0;
2938 nesqp->in_disconnect = 0;
2940 switch (nesqp->hw_iwarp_state) {
2941 case NES_AEQE_IWARP_STATE_CLOSING:
2942 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2943 case NES_AEQE_IWARP_STATE_TERMINATE:
2944 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2946 case NES_AEQE_IWARP_STATE_ERROR:
2947 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2950 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2951 nesqp->in_disconnect = 1;
2952 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2958 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2960 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2961 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2964 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2965 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2966 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2967 issue_modify_qp = 1;
2968 nesqp->in_disconnect = 1;
2972 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2973 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2976 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2978 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2979 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2980 if (nesqp->hte_added) {
2981 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2982 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2983 nesqp->hte_added = 0;
2985 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
2986 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2987 next_iwarp_state |= NES_CQP_QP_RESET;
2988 nesqp->in_disconnect = 1;
2990 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2991 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2994 issue_modify_qp = 1;
2995 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2998 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3003 nesqp->ibqp_state = attr->qp_state;
3004 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3005 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3006 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3007 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3008 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3009 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3010 nesqp->iwarp_state);
3011 issue_disconnect = 1;
3013 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3014 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3015 nesqp->iwarp_state);
3019 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3020 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3021 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3022 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3023 issue_modify_qp = 1;
3025 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3026 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3027 issue_modify_qp = 1;
3029 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3030 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3031 issue_modify_qp = 1;
3033 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3034 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3035 issue_modify_qp = 1;
3038 if (nesqp->user_mode) {
3039 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3040 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3041 issue_modify_qp = 1;
3045 original_last_aeq = nesqp->last_aeq;
3046 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3048 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3053 if (issue_modify_qp) {
3054 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3055 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 1);
3057 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3058 " failed for QP%u.\n",
3059 next_iwarp_state, nesqp->hwqp.qp_id);
3063 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3064 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3065 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3066 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3067 original_last_aeq, nesqp->last_aeq);
3069 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3072 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3073 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3074 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3075 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3076 original_last_aeq, nesqp->last_aeq);
3077 /* this one is for the cm_disconnect thread */
3078 spin_lock_irqsave(&nesqp->lock, qplockflags);
3079 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3080 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3081 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3082 nes_cm_disconn(nesqp);
3084 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3085 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3088 spin_lock_irqsave(&nesqp->lock, qplockflags);
3090 /* These two are for the timer thread */
3091 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3092 nesqp->cm_id->add_ref(nesqp->cm_id);
3093 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3094 " need ae to finish up, original_last_aeq = 0x%04X."
3095 " last_aeq = 0x%04X, scheduling timer.\n",
3096 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3097 original_last_aeq, nesqp->last_aeq);
3098 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3100 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3102 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3103 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3104 " need ae to finish up, original_last_aeq = 0x%04X."
3105 " last_aeq = 0x%04X.\n",
3106 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3107 original_last_aeq, nesqp->last_aeq);
3111 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3112 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3113 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3114 original_last_aeq, nesqp->last_aeq);
3117 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3118 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3119 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3120 original_last_aeq, nesqp->last_aeq);
3125 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3126 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3133 * nes_muticast_attach
3135 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3137 nes_debug(NES_DBG_INIT, "\n");
3143 * nes_multicast_detach
3145 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3147 nes_debug(NES_DBG_INIT, "\n");
3155 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3156 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3157 struct ib_mad *in_mad, struct ib_mad *out_mad)
3159 nes_debug(NES_DBG_INIT, "\n");
3164 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3167 int total_payload_length = 0;
3168 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3169 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3170 ib_wr->sg_list[sge_index].addr);
3171 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3172 ib_wr->sg_list[sge_index].length);
3174 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3175 (ib_wr->sg_list[sge_index].lkey));
3177 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3179 total_payload_length += ib_wr->sg_list[sge_index].length;
3181 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3182 total_payload_length);
3183 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3184 total_payload_length);
3190 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3191 struct ib_send_wr **bad_wr)
3194 unsigned long flags = 0;
3195 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3196 struct nes_device *nesdev = nesvnic->nesdev;
3197 struct nes_qp *nesqp = to_nesqp(ibqp);
3198 struct nes_hw_qp_wqe *wqe;
3200 u32 qsize = nesqp->hwqp.sq_size;
3205 u32 total_payload_length;
3210 total_payload_length = 0;
3212 if (nesqp->ibqp_state > IB_QPS_RTS)
3215 spin_lock_irqsave(&nesqp->lock, flags);
3217 head = nesqp->hwqp.sq_head;
3220 /* Check for SQ overflow */
3221 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3226 wqe = &nesqp->hwqp.sq_vbase[head];
3227 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3228 nesqp->hwqp.qp_id, wqe, head); */
3229 nes_fill_init_qp_wqe(wqe, nesqp, head);
3230 u64temp = (u64)(ib_wr->wr_id);
3231 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3233 switch (ib_wr->opcode) {
3235 if (ib_wr->send_flags & IB_SEND_SOLICITED) {
3236 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3238 wqe_misc = NES_IWARP_SQ_OP_SEND;
3240 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3244 if (ib_wr->send_flags & IB_SEND_FENCE) {
3245 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3247 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3248 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3249 (ib_wr->sg_list[0].length <= 64)) {
3250 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3251 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3252 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3253 ib_wr->sg_list[0].length);
3254 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3256 fill_wqe_sg_send(wqe, ib_wr, 1);
3260 case IB_WR_RDMA_WRITE:
3261 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3262 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3263 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3265 nesdev->nesadapter->max_sge);
3269 if (ib_wr->send_flags & IB_SEND_FENCE) {
3270 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3273 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3274 ib_wr->wr.rdma.rkey);
3275 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3276 ib_wr->wr.rdma.remote_addr);
3278 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3279 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3280 (ib_wr->sg_list[0].length <= 64)) {
3281 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3282 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3283 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3284 ib_wr->sg_list[0].length);
3285 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3287 fill_wqe_sg_send(wqe, ib_wr, 1);
3289 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3290 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3292 case IB_WR_RDMA_READ:
3293 /* iWARP only supports 1 sge for RDMA reads */
3294 if (ib_wr->num_sge > 1) {
3295 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3300 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3301 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3302 ib_wr->wr.rdma.remote_addr);
3303 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3304 ib_wr->wr.rdma.rkey);
3305 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3306 ib_wr->sg_list->length);
3307 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3308 ib_wr->sg_list->addr);
3309 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3310 ib_wr->sg_list->lkey);
3318 if (ib_wr->send_flags & IB_SEND_SIGNALED) {
3319 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3321 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3323 ib_wr = ib_wr->next;
3331 nesqp->hwqp.sq_head = head;
3334 counter = min(wqe_count, ((u32)255));
3335 wqe_count -= counter;
3336 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3337 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3340 spin_unlock_irqrestore(&nesqp->lock, flags);
3351 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3352 struct ib_recv_wr **bad_wr)
3355 unsigned long flags = 0;
3356 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3357 struct nes_device *nesdev = nesvnic->nesdev;
3358 struct nes_qp *nesqp = to_nesqp(ibqp);
3359 struct nes_hw_qp_wqe *wqe;
3362 u32 qsize = nesqp->hwqp.rq_size;
3366 u32 total_payload_length;
3368 if (nesqp->ibqp_state > IB_QPS_RTS)
3371 spin_lock_irqsave(&nesqp->lock, flags);
3373 head = nesqp->hwqp.rq_head;
3376 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3380 /* Check for RQ overflow */
3381 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3386 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3387 wqe = &nesqp->hwqp.rq_vbase[head];
3389 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3390 nesqp->hwqp.qp_id, wqe, head); */
3391 nes_fill_init_qp_wqe(wqe, nesqp, head);
3392 u64temp = (u64)(ib_wr->wr_id);
3393 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3395 total_payload_length = 0;
3396 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3397 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3398 ib_wr->sg_list[sge_index].addr);
3399 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3400 ib_wr->sg_list[sge_index].length);
3401 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3402 ib_wr->sg_list[sge_index].lkey);
3404 total_payload_length += ib_wr->sg_list[sge_index].length;
3406 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3407 total_payload_length);
3409 ib_wr = ib_wr->next;
3416 nesqp->hwqp.rq_head = head;
3419 counter = min(wqe_count, ((u32)255));
3420 wqe_count -= counter;
3421 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3424 spin_unlock_irqrestore(&nesqp->lock, flags);
3435 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3440 unsigned long flags = 0;
3441 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3442 struct nes_device *nesdev = nesvnic->nesdev;
3443 struct nes_cq *nescq = to_nescq(ibcq);
3444 struct nes_qp *nesqp;
3445 struct nes_hw_cqe cqe;
3454 nes_debug(NES_DBG_CQ, "\n");
3456 spin_lock_irqsave(&nescq->lock, flags);
3458 head = nescq->hw_cq.cq_head;
3459 cq_size = nescq->hw_cq.cq_size;
3461 while (cqe_count < num_entries) {
3462 if (le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3465 * Make sure we read CQ entry contents *after*
3466 * we've checked the valid bit.
3470 cqe = nescq->hw_cq.cq_vbase[head];
3471 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3472 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3473 wqe_index = u32temp &
3474 (nesdev->nesadapter->max_qp_wr - 1);
3475 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3476 /* parse CQE, get completion context from WQE (either rq or sq */
3477 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3479 nesqp = *((struct nes_qp **)&u64temp);
3480 memset(entry, 0, sizeof *entry);
3481 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3482 entry->status = IB_WC_SUCCESS;
3484 entry->status = IB_WC_WR_FLUSH_ERR;
3487 entry->qp = &nesqp->ibqp;
3488 entry->src_qp = nesqp->hwqp.qp_id;
3490 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3491 if (nesqp->skip_lsmm) {
3492 nesqp->skip_lsmm = 0;
3493 wq_tail = nesqp->hwqp.sq_tail++;
3496 /* Working on a SQ Completion*/
3497 wq_tail = wqe_index;
3498 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3499 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3500 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3501 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wq_tail].
3502 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3503 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3504 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3506 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3507 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3508 case NES_IWARP_SQ_OP_RDMAW:
3509 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3510 entry->opcode = IB_WC_RDMA_WRITE;
3512 case NES_IWARP_SQ_OP_RDMAR:
3513 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3514 entry->opcode = IB_WC_RDMA_READ;
3515 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wq_tail].
3516 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3518 case NES_IWARP_SQ_OP_SENDINV:
3519 case NES_IWARP_SQ_OP_SENDSEINV:
3520 case NES_IWARP_SQ_OP_SEND:
3521 case NES_IWARP_SQ_OP_SENDSE:
3522 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3523 entry->opcode = IB_WC_SEND;
3527 /* Working on a RQ Completion*/
3528 wq_tail = wqe_index;
3529 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3530 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3531 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3532 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wq_tail].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3533 entry->opcode = IB_WC_RECV;
3535 entry->wr_id = wrid;
3537 if (++head >= cq_size)
3540 nescq->polled_completions++;
3541 if ((nescq->polled_completions > (cq_size / 2)) ||
3542 (nescq->polled_completions == 255)) {
3543 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3544 " are pending %u of %u.\n",
3545 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3546 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3547 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3548 nescq->polled_completions = 0;
3555 if (nescq->polled_completions) {
3556 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3557 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3558 nescq->polled_completions = 0;
3561 nescq->hw_cq.cq_head = head;
3562 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3563 cqe_count, nescq->hw_cq.cq_number);
3565 spin_unlock_irqrestore(&nescq->lock, flags);
3574 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3576 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3577 struct nes_device *nesdev = nesvnic->nesdev;
3578 struct nes_cq *nescq = to_nescq(ibcq);
3581 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3582 nescq->hw_cq.cq_number);
3584 cq_arm = nescq->hw_cq.cq_number;
3585 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3586 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3587 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3588 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3592 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3593 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3600 * nes_init_ofa_device
3602 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3604 struct nes_ib_device *nesibdev;
3605 struct nes_vnic *nesvnic = netdev_priv(netdev);
3606 struct nes_device *nesdev = nesvnic->nesdev;
3608 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3609 if (nesibdev == NULL) {
3612 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3613 nesibdev->ibdev.owner = THIS_MODULE;
3615 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3616 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3617 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3619 nesibdev->ibdev.uverbs_cmd_mask =
3620 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3621 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3622 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3623 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3624 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3625 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3626 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3627 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3628 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3629 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3630 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3631 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3632 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3633 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3634 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3635 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3636 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3637 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3638 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3639 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3640 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3641 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3643 nesibdev->ibdev.phys_port_cnt = 1;
3644 nesibdev->ibdev.num_comp_vectors = 1;
3645 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3646 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3647 nesibdev->ibdev.query_device = nes_query_device;
3648 nesibdev->ibdev.query_port = nes_query_port;
3649 nesibdev->ibdev.modify_port = nes_modify_port;
3650 nesibdev->ibdev.query_pkey = nes_query_pkey;
3651 nesibdev->ibdev.query_gid = nes_query_gid;
3652 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3653 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3654 nesibdev->ibdev.mmap = nes_mmap;
3655 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3656 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3657 nesibdev->ibdev.create_ah = nes_create_ah;
3658 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3659 nesibdev->ibdev.create_qp = nes_create_qp;
3660 nesibdev->ibdev.modify_qp = nes_modify_qp;
3661 nesibdev->ibdev.query_qp = nes_query_qp;
3662 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3663 nesibdev->ibdev.create_cq = nes_create_cq;
3664 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3665 nesibdev->ibdev.poll_cq = nes_poll_cq;
3666 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3667 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3668 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3669 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3670 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3671 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3672 nesibdev->ibdev.bind_mw = nes_bind_mw;
3674 nesibdev->ibdev.alloc_fmr = nes_alloc_fmr;
3675 nesibdev->ibdev.unmap_fmr = nes_unmap_fmr;
3676 nesibdev->ibdev.dealloc_fmr = nes_dealloc_fmr;
3677 nesibdev->ibdev.map_phys_fmr = nes_map_phys_fmr;
3679 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3680 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3681 nesibdev->ibdev.process_mad = nes_process_mad;
3683 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3684 nesibdev->ibdev.post_send = nes_post_send;
3685 nesibdev->ibdev.post_recv = nes_post_recv;
3687 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3688 if (nesibdev->ibdev.iwcm == NULL) {
3689 ib_dealloc_device(&nesibdev->ibdev);
3692 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3693 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3694 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3695 nesibdev->ibdev.iwcm->connect = nes_connect;
3696 nesibdev->ibdev.iwcm->accept = nes_accept;
3697 nesibdev->ibdev.iwcm->reject = nes_reject;
3698 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3699 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3706 * nes_destroy_ofa_device
3708 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3710 if (nesibdev == NULL)
3713 nes_unregister_ofa_device(nesibdev);
3715 kfree(nesibdev->ibdev.iwcm);
3716 ib_dealloc_device(&nesibdev->ibdev);
3721 * nes_register_ofa_device
3723 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3725 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3726 struct nes_device *nesdev = nesvnic->nesdev;
3727 struct nes_adapter *nesadapter = nesdev->nesadapter;
3730 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3735 /* Get the resources allocated to this device */
3736 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3737 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3738 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3739 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3741 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3742 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3746 device_remove_file(&nesibdev->ibdev.dev,
3747 nes_dev_attributes[i]);
3749 ib_unregister_device(&nesibdev->ibdev);
3754 nesvnic->of_device_registered = 1;
3761 * nes_unregister_ofa_device
3763 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3765 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3768 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3769 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3772 if (nesvnic->of_device_registered) {
3773 ib_unregister_device(&nesibdev->ibdev);
3776 nesvnic->of_device_registered = 0;