2 * Copyright (c) 2006 - 2009 Intel-NE, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/random.h>
37 #include <linux/highmem.h>
38 #include <asm/byteorder.h>
40 #include <rdma/ib_verbs.h>
41 #include <rdma/iw_cm.h>
42 #include <rdma/ib_user_verbs.h>
46 #include <rdma/ib_umem.h>
48 atomic_t mod_qp_timouts;
50 atomic_t sw_qps_destroyed;
52 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
57 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd) {
58 struct nes_pd *nespd = to_nespd(ibpd);
59 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
60 struct nes_device *nesdev = nesvnic->nesdev;
61 struct nes_adapter *nesadapter = nesdev->nesadapter;
62 struct nes_cqp_request *cqp_request;
65 struct nes_hw_cqp_wqe *cqp_wqe;
69 u32 next_stag_index = 0;
73 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
74 stag_key = (u8)next_stag_index;
78 next_stag_index >>= 8;
79 next_stag_index %= nesadapter->max_mr;
81 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
82 nesadapter->max_mr, &stag_index, &next_stag_index);
87 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
89 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
90 return ERR_PTR(-ENOMEM);
93 stag = stag_index << 8;
95 stag += (u32)stag_key;
97 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
100 /* Register the region with the adapter */
101 cqp_request = nes_get_cqp_request(nesdev);
102 if (cqp_request == NULL) {
104 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
105 return ERR_PTR(-ENOMEM);
108 cqp_request->waiting = 1;
109 cqp_wqe = &cqp_request->cqp_wqe;
111 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
112 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
113 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
114 NES_CQP_STAG_REM_ACC_EN);
116 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
117 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
118 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
120 atomic_set(&cqp_request->refcount, 2);
121 nes_post_cqp_request(nesdev, cqp_request);
124 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
126 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
127 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
128 stag, ret, cqp_request->major_code, cqp_request->minor_code);
129 if ((!ret) || (cqp_request->major_code)) {
130 nes_put_cqp_request(nesdev, cqp_request);
132 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
134 return ERR_PTR(-ETIME);
136 return ERR_PTR(-ENOMEM);
139 nes_put_cqp_request(nesdev, cqp_request);
141 nesmr->ibmw.rkey = stag;
142 nesmr->mode = IWNES_MEMREG_TYPE_MW;
145 nesmr->pbls_used = 0;
154 static int nes_dealloc_mw(struct ib_mw *ibmw)
156 struct nes_mr *nesmr = to_nesmw(ibmw);
157 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
158 struct nes_device *nesdev = nesvnic->nesdev;
159 struct nes_adapter *nesadapter = nesdev->nesadapter;
160 struct nes_hw_cqp_wqe *cqp_wqe;
161 struct nes_cqp_request *cqp_request;
165 /* Deallocate the window with the adapter */
166 cqp_request = nes_get_cqp_request(nesdev);
167 if (cqp_request == NULL) {
168 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
171 cqp_request->waiting = 1;
172 cqp_wqe = &cqp_request->cqp_wqe;
173 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
174 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
175 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
177 atomic_set(&cqp_request->refcount, 2);
178 nes_post_cqp_request(nesdev, cqp_request);
181 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
183 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
185 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
186 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
187 ret, cqp_request->major_code, cqp_request->minor_code);
190 else if (cqp_request->major_code)
193 nes_put_cqp_request(nesdev, cqp_request);
195 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
196 (ibmw->rkey & 0x0fffff00) >> 8);
206 static int nes_bind_mw(struct ib_qp *ibqp, struct ib_mw *ibmw,
207 struct ib_mw_bind *ibmw_bind)
210 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
211 struct nes_device *nesdev = nesvnic->nesdev;
212 /* struct nes_mr *nesmr = to_nesmw(ibmw); */
213 struct nes_qp *nesqp = to_nesqp(ibqp);
214 struct nes_hw_qp_wqe *wqe;
215 unsigned long flags = 0;
220 if (nesqp->ibqp_state > IB_QPS_RTS)
223 spin_lock_irqsave(&nesqp->lock, flags);
225 head = nesqp->hwqp.sq_head;
226 qsize = nesqp->hwqp.sq_tail;
228 /* Check for SQ overflow */
229 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
230 spin_unlock_irqrestore(&nesqp->lock, flags);
234 wqe = &nesqp->hwqp.sq_vbase[head];
235 /* nes_debug(NES_DBG_MR, "processing sq wqe at %p, head = %u.\n", wqe, head); */
236 nes_fill_init_qp_wqe(wqe, nesqp, head);
237 u64temp = ibmw_bind->wr_id;
238 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX, u64temp);
239 wqe_misc = NES_IWARP_SQ_OP_BIND;
241 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
243 if (ibmw_bind->send_flags & IB_SEND_SIGNALED)
244 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
246 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_WRITE) {
247 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE;
249 if (ibmw_bind->mw_access_flags & IB_ACCESS_REMOTE_READ) {
250 wqe_misc |= NES_CQP_STAG_RIGHTS_REMOTE_READ;
253 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_MISC_IDX, wqe_misc);
254 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MR_IDX, ibmw_bind->mr->lkey);
255 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_MW_IDX, ibmw->rkey);
256 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_LENGTH_LOW_IDX,
258 wqe->wqe_words[NES_IWARP_SQ_BIND_WQE_LENGTH_HIGH_IDX] = 0;
259 u64temp = (u64)ibmw_bind->addr;
260 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_BIND_WQE_VA_FBO_LOW_IDX, u64temp);
266 nesqp->hwqp.sq_head = head;
269 nes_write32(nesdev->regs+NES_WQE_ALLOC,
270 (1 << 24) | 0x00800000 | nesqp->hwqp.qp_id);
272 spin_unlock_irqrestore(&nesqp->lock, flags);
281 static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
282 u32 stag, u32 page_count)
284 struct nes_hw_cqp_wqe *cqp_wqe;
285 struct nes_cqp_request *cqp_request;
288 struct nes_adapter *nesadapter = nesdev->nesadapter;
291 u64 region_length = page_count * PAGE_SIZE;
294 cqp_request = nes_get_cqp_request(nesdev);
295 if (cqp_request == NULL) {
296 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
299 nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
300 "region_length = %llu\n",
301 page_count, region_length);
302 cqp_request->waiting = 1;
303 cqp_wqe = &cqp_request->cqp_wqe;
305 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
306 if (nesadapter->free_4kpbl > 0) {
307 nesadapter->free_4kpbl--;
308 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
310 /* No 4kpbl's available: */
311 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
312 nes_debug(NES_DBG_MR, "Out of Pbls\n");
313 nes_free_cqp_request(nesdev, cqp_request);
317 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
318 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
319 NES_CQP_STAG_REM_ACC_EN;
321 * The current OFED API does not support the zero based TO option.
322 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
323 * the API does not support that ability to have the MR set for local
324 * access only when created and not allow the SQ op to override. Given
325 * this the remote enable must be set here.
328 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
329 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
330 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
332 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
333 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
334 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
335 cpu_to_le32(nespd->pd_id & 0x00007fff);
337 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
338 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
339 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
340 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
341 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
342 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
345 atomic_set(&cqp_request->refcount, 2);
346 nes_post_cqp_request(nesdev, cqp_request);
349 ret = wait_event_timeout(cqp_request->waitq,
350 (0 != cqp_request->request_done),
353 nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
354 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
355 "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
356 cqp_request->minor_code);
357 major_code = cqp_request->major_code;
358 nes_put_cqp_request(nesdev, cqp_request);
360 if (!ret || major_code) {
361 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
362 nesadapter->free_4kpbl++;
363 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
374 * nes_alloc_fast_reg_mr
376 struct ib_mr *nes_alloc_fast_reg_mr(struct ib_pd *ibpd, int max_page_list_len)
378 struct nes_pd *nespd = to_nespd(ibpd);
379 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
380 struct nes_device *nesdev = nesvnic->nesdev;
381 struct nes_adapter *nesadapter = nesdev->nesadapter;
388 struct nes_mr *nesmr;
393 * Note: Set to always use a fixed length single page entry PBL. This is to allow
394 * for the fast_reg_mr operation to always know the size of the PBL.
396 if (max_page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
397 return ERR_PTR(-E2BIG);
399 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
400 stag_key = (u8)next_stag_index;
401 next_stag_index >>= 8;
402 next_stag_index %= nesadapter->max_mr;
404 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
405 nesadapter->max_mr, &stag_index,
410 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
412 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
413 return ERR_PTR(-ENOMEM);
416 stag = stag_index << 8;
418 stag += (u32)stag_key;
420 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
423 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_page_list_len);
426 nesmr->ibmr.rkey = stag;
427 nesmr->ibmr.lkey = stag;
428 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
432 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
433 ibmr = ERR_PTR(-ENOMEM);
439 * nes_alloc_fast_reg_page_list
441 static struct ib_fast_reg_page_list *nes_alloc_fast_reg_page_list(
442 struct ib_device *ibdev,
445 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
446 struct nes_device *nesdev = nesvnic->nesdev;
447 struct ib_fast_reg_page_list *pifrpl;
448 struct nes_ib_fast_reg_page_list *pnesfrpl;
450 if (page_list_len > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
451 return ERR_PTR(-E2BIG);
453 * Allocate the ib_fast_reg_page_list structure, the
454 * nes_fast_bpl structure, and the PLB table.
456 pnesfrpl = kmalloc(sizeof(struct nes_ib_fast_reg_page_list) +
457 page_list_len * sizeof(u64), GFP_KERNEL);
460 return ERR_PTR(-ENOMEM);
462 pifrpl = &pnesfrpl->ibfrpl;
463 pifrpl->page_list = &pnesfrpl->pbl;
464 pifrpl->max_page_list_len = page_list_len;
466 * Allocate the WQE PBL
468 pnesfrpl->nes_wqe_pbl.kva = pci_alloc_consistent(nesdev->pcidev,
469 page_list_len * sizeof(u64),
470 &pnesfrpl->nes_wqe_pbl.paddr);
472 if (!pnesfrpl->nes_wqe_pbl.kva) {
474 return ERR_PTR(-ENOMEM);
476 nes_debug(NES_DBG_MR, "nes_alloc_fast_reg_pbl: nes_frpl = %p, "
477 "ibfrpl = %p, ibfrpl.page_list = %p, pbl.kva = %p, "
478 "pbl.paddr= %p\n", pnesfrpl, &pnesfrpl->ibfrpl,
479 pnesfrpl->ibfrpl.page_list, pnesfrpl->nes_wqe_pbl.kva,
480 (void *)pnesfrpl->nes_wqe_pbl.paddr);
486 * nes_free_fast_reg_page_list
488 static void nes_free_fast_reg_page_list(struct ib_fast_reg_page_list *pifrpl)
490 struct nes_vnic *nesvnic = to_nesvnic(pifrpl->device);
491 struct nes_device *nesdev = nesvnic->nesdev;
492 struct nes_ib_fast_reg_page_list *pnesfrpl;
494 pnesfrpl = container_of(pifrpl, struct nes_ib_fast_reg_page_list, ibfrpl);
498 pci_free_consistent(nesdev->pcidev,
499 pifrpl->max_page_list_len * sizeof(u64),
500 pnesfrpl->nes_wqe_pbl.kva,
501 pnesfrpl->nes_wqe_pbl.paddr);
503 * Free the PBL structure
511 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props)
513 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
514 struct nes_device *nesdev = nesvnic->nesdev;
515 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
517 memset(props, 0, sizeof(*props));
518 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
520 props->fw_ver = nesdev->nesadapter->fw_ver;
521 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
522 props->vendor_id = nesdev->nesadapter->vendor_id;
523 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
524 props->hw_ver = nesdev->nesadapter->hw_rev;
525 props->max_mr_size = 0x80000000;
526 props->max_qp = nesibdev->max_qp;
527 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
528 props->max_sge = nesdev->nesadapter->max_sge;
529 props->max_cq = nesibdev->max_cq;
530 props->max_cqe = nesdev->nesadapter->max_cqe - 1;
531 props->max_mr = nesibdev->max_mr;
532 props->max_mw = nesibdev->max_mr;
533 props->max_pd = nesibdev->max_pd;
534 props->max_sge_rd = 1;
535 switch (nesdev->nesadapter->max_irrq_wr) {
537 props->max_qp_rd_atom = 1;
540 props->max_qp_rd_atom = 4;
543 props->max_qp_rd_atom = 16;
546 props->max_qp_rd_atom = 32;
549 props->max_qp_rd_atom = 0;
551 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
552 props->atomic_cap = IB_ATOMIC_NONE;
553 props->max_map_per_fmr = 1;
562 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
564 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
565 struct net_device *netdev = nesvnic->netdev;
567 memset(props, 0, sizeof(*props));
569 props->max_mtu = IB_MTU_4096;
571 if (netdev->mtu >= 4096)
572 props->active_mtu = IB_MTU_4096;
573 else if (netdev->mtu >= 2048)
574 props->active_mtu = IB_MTU_2048;
575 else if (netdev->mtu >= 1024)
576 props->active_mtu = IB_MTU_1024;
577 else if (netdev->mtu >= 512)
578 props->active_mtu = IB_MTU_512;
580 props->active_mtu = IB_MTU_256;
587 props->state = IB_PORT_ACTIVE;
589 props->state = IB_PORT_DOWN;
590 props->phys_state = 0;
591 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
592 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
593 props->gid_tbl_len = 1;
594 props->pkey_tbl_len = 1;
595 props->qkey_viol_cntr = 0;
596 props->active_width = IB_WIDTH_4X;
597 props->active_speed = 1;
598 props->max_msg_sz = 0x80000000;
607 static int nes_modify_port(struct ib_device *ibdev, u8 port,
608 int port_modify_mask, struct ib_port_modify *props)
617 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
627 static int nes_query_gid(struct ib_device *ibdev, u8 port,
628 int index, union ib_gid *gid)
630 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
632 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
633 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
640 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
641 * of all objects associated with a particular user-mode client.
643 static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
644 struct ib_udata *udata)
646 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
647 struct nes_device *nesdev = nesvnic->nesdev;
648 struct nes_adapter *nesadapter = nesdev->nesadapter;
649 struct nes_alloc_ucontext_req req;
650 struct nes_alloc_ucontext_resp uresp;
651 struct nes_ucontext *nes_ucontext;
652 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
655 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
656 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
657 return ERR_PTR(-EINVAL);
660 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
661 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
662 req.userspace_ver, NES_ABI_USERSPACE_VER);
663 return ERR_PTR(-EINVAL);
667 memset(&uresp, 0, sizeof uresp);
669 uresp.max_qps = nesibdev->max_qp;
670 uresp.max_pds = nesibdev->max_pd;
671 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
672 uresp.virtwq = nesadapter->virtwq;
673 uresp.kernel_ver = NES_ABI_KERNEL_VER;
675 nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
677 return ERR_PTR(-ENOMEM);
679 nes_ucontext->nesdev = nesdev;
680 nes_ucontext->mmap_wq_offset = uresp.max_pds;
681 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
682 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
686 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
688 return ERR_PTR(-EFAULT);
691 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
692 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
693 atomic_set(&nes_ucontext->usecnt, 1);
694 return &nes_ucontext->ibucontext;
699 * nes_dealloc_ucontext
701 static int nes_dealloc_ucontext(struct ib_ucontext *context)
703 /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
704 /* struct nes_device *nesdev = nesvnic->nesdev; */
705 struct nes_ucontext *nes_ucontext = to_nesucontext(context);
707 if (!atomic_dec_and_test(&nes_ucontext->usecnt))
717 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
720 struct nes_vnic *nesvnic = to_nesvnic(context->device);
721 struct nes_device *nesdev = nesvnic->nesdev;
722 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
723 struct nes_ucontext *nes_ucontext;
724 struct nes_qp *nesqp;
726 nes_ucontext = to_nesucontext(context);
729 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
730 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
731 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
732 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
733 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
734 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
737 nesqp = nes_ucontext->mmap_nesqp[index];
739 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
742 if (remap_pfn_range(vma, vma->vm_start,
743 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
744 vma->vm_end - vma->vm_start,
745 vma->vm_page_prot)) {
746 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
749 vma->vm_private_data = nesqp;
752 index = vma->vm_pgoff;
753 if (!test_bit(index, nes_ucontext->allocated_doorbells))
756 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
757 if (io_remap_pfn_range(vma, vma->vm_start,
758 (nesdev->doorbell_start +
759 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
760 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
762 vma->vm_private_data = nes_ucontext;
773 static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
774 struct ib_ucontext *context, struct ib_udata *udata)
776 struct nes_pd *nespd;
777 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
778 struct nes_device *nesdev = nesvnic->nesdev;
779 struct nes_adapter *nesadapter = nesdev->nesadapter;
780 struct nes_ucontext *nesucontext;
781 struct nes_alloc_pd_resp uresp;
785 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
786 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
787 atomic_read(&nesvnic->netdev->refcnt));
789 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
790 nesadapter->max_pd, &pd_num, &nesadapter->next_pd);
795 nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
797 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
798 return ERR_PTR(-ENOMEM);
801 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
802 nespd, nesvnic->nesibdev->ibdev.name);
804 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
807 nesucontext = to_nesucontext(context);
808 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
809 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
810 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
811 nespd->mmap_db_index, nespd->pd_id);
812 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
813 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
814 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
816 return ERR_PTR(-ENOMEM);
819 uresp.pd_id = nespd->pd_id;
820 uresp.mmap_db_index = nespd->mmap_db_index;
821 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
822 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
824 return ERR_PTR(-EFAULT);
827 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
828 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
829 nesucontext->first_free_db = nespd->mmap_db_index + 1;
832 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
840 static int nes_dealloc_pd(struct ib_pd *ibpd)
842 struct nes_ucontext *nesucontext;
843 struct nes_pd *nespd = to_nespd(ibpd);
844 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
845 struct nes_device *nesdev = nesvnic->nesdev;
846 struct nes_adapter *nesadapter = nesdev->nesadapter;
848 if ((ibpd->uobject) && (ibpd->uobject->context)) {
849 nesucontext = to_nesucontext(ibpd->uobject->context);
850 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
851 nespd->mmap_db_index);
852 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
853 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
854 if (nesucontext->first_free_db > nespd->mmap_db_index) {
855 nesucontext->first_free_db = nespd->mmap_db_index;
859 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
860 nespd->pd_id, nespd);
861 nes_free_resource(nesadapter, nesadapter->allocated_pds,
862 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
872 static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
874 return ERR_PTR(-ENOSYS);
881 static int nes_destroy_ah(struct ib_ah *ah)
888 * nes_get_encoded_size
890 static inline u8 nes_get_encoded_size(int *size)
896 } else if (*size <= 128) {
899 } else if (*size <= 512) {
903 return (encoded_size);
911 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
912 struct nes_vnic *nesvnic, int sq_size, int rq_size)
919 struct nes_device *nesdev = nesvnic->nesdev;
920 struct nes_adapter *nesadapter = nesdev->nesadapter;
925 pbl_entries = nespbl->pbl_size >> 3;
926 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
927 nespbl->pbl_size, pbl_entries,
928 (void *)nespbl->pbl_vbase,
929 (unsigned long) nespbl->pbl_pbase);
930 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
931 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
932 /* the first pbl to be fro the rq_vbase... */
933 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
934 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
935 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
937 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
942 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
943 nesqp->page = nespbl->page;
944 if (!nesqp->hwqp.sq_vbase) {
945 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
950 /* Now to get to sq.. we need to calculate how many */
951 /* PBL entries were used by the rq.. */
952 pbl += sq_pbl_entries;
953 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
954 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
955 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
957 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
958 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
959 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
960 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
961 if (!nesadapter->free_256pbl) {
962 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
964 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
969 nesadapter->free_256pbl--;
970 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
972 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
973 pblbuffer = nesqp->pbl_vbase;
974 if (!nesqp->pbl_vbase) {
975 /* memory allocated during nes_reg_user_mr() */
976 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
979 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
980 nesadapter->free_256pbl++;
981 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
985 memset(nesqp->pbl_vbase, 0, 256);
986 /* fill in the page address in the pbl buffer.. */
987 tpbl = pblbuffer + 16;
988 pbl = (__le64 *)nespbl->pbl_vbase;
989 while (sq_pbl_entries--)
992 while (rq_pbl_entries--)
995 /* done with memory allocated during nes_reg_user_mr() */
996 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1000 nesqp->qp_mem_size =
1001 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
1002 /* Round up to a multiple of a page */
1003 nesqp->qp_mem_size += PAGE_SIZE - 1;
1004 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1006 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1007 &nesqp->hwqp.q2_pbase);
1010 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
1011 nesqp->pbl_vbase = NULL;
1012 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1013 nesadapter->free_256pbl++;
1014 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1015 kunmap(nesqp->page);
1018 nesqp->hwqp.q2_vbase = mem;
1020 memset(nesqp->hwqp.q2_vbase, 0, 256);
1021 nesqp->nesqp_context = mem;
1022 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1023 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1032 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
1033 int sq_size, int rq_size)
1036 struct nes_device *nesdev = nesvnic->nesdev;
1038 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
1039 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
1040 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
1041 256; /* this is Q2 */
1042 /* Round up to a multiple of a page */
1043 nesqp->qp_mem_size += PAGE_SIZE - 1;
1044 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
1046 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1047 &nesqp->hwqp.sq_pbase);
1050 nes_debug(NES_DBG_QP, "PCI consistent memory for "
1051 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
1052 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
1054 memset(mem, 0, nesqp->qp_mem_size);
1056 nesqp->hwqp.sq_vbase = mem;
1057 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
1059 nesqp->hwqp.rq_vbase = mem;
1060 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
1061 sizeof(struct nes_hw_qp_wqe) * sq_size;
1062 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
1064 nesqp->hwqp.q2_vbase = mem;
1065 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
1066 sizeof(struct nes_hw_qp_wqe) * rq_size;
1068 memset(nesqp->hwqp.q2_vbase, 0, 256);
1070 nesqp->nesqp_context = mem;
1071 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
1072 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
1078 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
1080 static inline void nes_free_qp_mem(struct nes_device *nesdev,
1081 struct nes_qp *nesqp, int virt_wqs)
1083 unsigned long flags;
1084 struct nes_adapter *nesadapter = nesdev->nesadapter;
1086 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
1087 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
1089 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1090 nesadapter->free_256pbl++;
1091 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1092 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
1093 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
1094 nesqp->pbl_vbase = NULL;
1095 kunmap(nesqp->page);
1103 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1104 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1108 struct nes_pd *nespd = to_nespd(ibpd);
1109 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1110 struct nes_device *nesdev = nesvnic->nesdev;
1111 struct nes_adapter *nesadapter = nesdev->nesadapter;
1112 struct nes_qp *nesqp;
1113 struct nes_cq *nescq;
1114 struct nes_ucontext *nes_ucontext;
1115 struct nes_hw_cqp_wqe *cqp_wqe;
1116 struct nes_cqp_request *cqp_request;
1117 struct nes_create_qp_req req;
1118 struct nes_create_qp_resp uresp;
1119 struct nes_pbl *nespbl = NULL;
1122 /* u32 counter = 0; */
1124 unsigned long flags;
1134 if (init_attr->create_flags)
1135 return ERR_PTR(-EINVAL);
1137 atomic_inc(&qps_created);
1138 switch (init_attr->qp_type) {
1140 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1141 init_attr->cap.max_inline_data = 0;
1143 init_attr->cap.max_inline_data = 64;
1145 sq_size = init_attr->cap.max_send_wr;
1146 rq_size = init_attr->cap.max_recv_wr;
1148 /* check if the encoded sizes are OK or not... */
1149 sq_encoded_size = nes_get_encoded_size(&sq_size);
1150 rq_encoded_size = nes_get_encoded_size(&rq_size);
1152 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1153 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1155 return ERR_PTR(-EINVAL);
1158 init_attr->cap.max_send_wr = sq_size -2;
1159 init_attr->cap.max_recv_wr = rq_size -1;
1160 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1162 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1163 nesadapter->max_qp, &qp_num, &nesadapter->next_qp);
1165 return ERR_PTR(ret);
1168 /* Need 512 (actually now 1024) byte alignment on this structure */
1169 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1171 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1172 nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1173 return ERR_PTR(-ENOMEM);
1175 u64nesqp = (unsigned long)mem;
1176 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1177 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1178 u64nesqp &= ~u64temp;
1179 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1180 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1181 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1182 nesqp->allocated_buffer = mem;
1185 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1186 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1187 kfree(nesqp->allocated_buffer);
1188 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1191 if (req.user_wqe_buffers) {
1194 if ((ibpd->uobject) && (ibpd->uobject->context)) {
1195 nesqp->user_mode = 1;
1196 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1199 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1200 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1201 list_del(&nespbl->list);
1203 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1204 nespbl, nespbl->user_base);
1209 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1210 (long long unsigned int)req.user_wqe_buffers);
1211 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1212 kfree(nesqp->allocated_buffer);
1213 return ERR_PTR(-EFAULT);
1217 nes_ucontext = to_nesucontext(ibpd->uobject->context);
1218 nesqp->mmap_sq_db_index =
1219 find_next_zero_bit(nes_ucontext->allocated_wqs,
1220 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1221 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1222 nespd->mmap_db_index); */
1223 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1224 nes_debug(NES_DBG_QP,
1225 "db index > max user regions, failing create QP\n");
1226 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1228 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1232 kfree(nesqp->allocated_buffer);
1233 return ERR_PTR(-ENOMEM);
1235 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1236 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1237 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1239 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1240 kfree(nesqp->allocated_buffer);
1241 return ERR_PTR(-EFAULT);
1244 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1245 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1247 nes_debug(NES_DBG_QP,
1248 "error geting qp mem code = %d\n", err);
1249 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1250 kfree(nesqp->allocated_buffer);
1251 return ERR_PTR(-ENOMEM);
1254 nesqp->hwqp.sq_size = sq_size;
1255 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1256 nesqp->hwqp.sq_head = 1;
1257 nesqp->hwqp.rq_size = rq_size;
1258 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1259 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1260 (void *)nesqp->nesqp_context_pbase);
1262 nesqp->hwqp.qp_id = qp_num;
1263 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1264 nesqp->nespd = nespd;
1266 nescq = to_nescq(init_attr->send_cq);
1267 nesqp->nesscq = nescq;
1268 nescq = to_nescq(init_attr->recv_cq);
1269 nesqp->nesrcq = nescq;
1271 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1272 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1273 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1274 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1275 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1276 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1278 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1279 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1281 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1282 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1283 u64temp = (u64)nesqp->hwqp.sq_pbase;
1284 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1285 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1289 u64temp = (u64)nesqp->hwqp.sq_pbase;
1290 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1291 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1292 u64temp = (u64)nesqp->hwqp.rq_pbase;
1293 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1294 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1296 u64temp = (u64)nesqp->pbl_pbase;
1297 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1298 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1301 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1302 nesvnic->next_qp_nic_index,
1303 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1304 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1305 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1306 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1307 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1308 nesvnic->next_qp_nic_index++;
1309 if ((nesvnic->next_qp_nic_index > 3) ||
1310 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1311 nesvnic->next_qp_nic_index = 0;
1313 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1315 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1316 u64temp = (u64)nesqp->hwqp.q2_pbase;
1317 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1318 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1319 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1320 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1321 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1322 ((((u32)nesadapter->max_irrq_wr) <<
1323 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1324 if (disable_mpa_crc) {
1325 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1326 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1331 cqp_request = nes_get_cqp_request(nesdev);
1332 if (cqp_request == NULL) {
1333 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1334 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1335 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1336 kfree(nesqp->allocated_buffer);
1337 return ERR_PTR(-ENOMEM);
1339 cqp_request->waiting = 1;
1340 cqp_wqe = &cqp_request->cqp_wqe;
1343 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1344 NES_CQP_QP_IWARP_STATE_IDLE;
1346 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1347 NES_CQP_QP_IWARP_STATE_IDLE;
1349 opcode |= NES_CQP_QP_CQS_VALID;
1350 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1351 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1352 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1354 u64temp = (u64)nesqp->nesqp_context_pbase;
1355 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1357 atomic_set(&cqp_request->refcount, 2);
1358 nes_post_cqp_request(nesdev, cqp_request);
1361 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1363 ret = wait_event_timeout(cqp_request->waitq,
1364 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1365 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1366 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1367 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1368 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1369 cqp_request->major_code, cqp_request->minor_code);
1370 if ((!ret) || (cqp_request->major_code)) {
1371 nes_put_cqp_request(nesdev, cqp_request);
1372 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1373 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1374 kfree(nesqp->allocated_buffer);
1376 return ERR_PTR(-ETIME);
1378 return ERR_PTR(-EIO);
1382 nes_put_cqp_request(nesdev, cqp_request);
1384 if (ibpd->uobject) {
1385 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1386 uresp.actual_sq_size = sq_size;
1387 uresp.actual_rq_size = rq_size;
1388 uresp.qp_id = nesqp->hwqp.qp_id;
1389 uresp.nes_drv_opt = nes_drv_opt;
1390 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1391 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1392 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1393 kfree(nesqp->allocated_buffer);
1394 return ERR_PTR(-EFAULT);
1398 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1399 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1400 spin_lock_init(&nesqp->lock);
1401 nes_add_ref(&nesqp->ibqp);
1404 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1405 return ERR_PTR(-EINVAL);
1408 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1410 /* update the QP table */
1411 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1412 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1413 atomic_read(&nesvnic->netdev->refcnt));
1415 return &nesqp->ibqp;
1422 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1428 unsigned long flags = 0;
1430 spin_lock_irqsave(&nescq->lock, flags);
1432 cq_head = nescq->hw_cq.cq_head;
1433 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1435 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1436 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1437 u64temp = (((u64)hi) << 32) | ((u64)lo);
1438 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1439 if (u64temp == (u64)(unsigned long)nesqp) {
1440 /* Zero the context value so cqe will be ignored */
1441 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1442 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1445 if (++cq_head >= nescq->hw_cq.cq_size)
1449 spin_unlock_irqrestore(&nescq->lock, flags);
1456 static int nes_destroy_qp(struct ib_qp *ibqp)
1458 struct nes_qp *nesqp = to_nesqp(ibqp);
1459 struct nes_ucontext *nes_ucontext;
1460 struct ib_qp_attr attr;
1461 struct iw_cm_id *cm_id;
1462 struct iw_cm_event cm_event;
1465 atomic_inc(&sw_qps_destroyed);
1466 nesqp->destroyed = 1;
1468 /* Blow away the connection if it exists. */
1469 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1470 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1471 attr.qp_state = IB_QPS_ERR;
1472 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1475 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1476 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1477 cm_id = nesqp->cm_id;
1478 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1479 cm_event.status = IW_CM_EVENT_STATUS_TIMEOUT;
1480 cm_event.local_addr = cm_id->local_addr;
1481 cm_event.remote_addr = cm_id->remote_addr;
1482 cm_event.private_data = NULL;
1483 cm_event.private_data_len = 0;
1485 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1486 "QP%u. cm_id = %p, refcount = %u. \n",
1487 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1489 cm_id->rem_ref(cm_id);
1490 ret = cm_id->event_handler(cm_id, &cm_event);
1492 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1495 if (nesqp->user_mode) {
1496 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1497 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1498 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1499 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1500 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1501 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1504 if (nesqp->pbl_pbase)
1505 kunmap(nesqp->page);
1507 /* Clean any pending completions from the cq(s) */
1509 nes_clean_cq(nesqp, nesqp->nesscq);
1511 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1512 nes_clean_cq(nesqp, nesqp->nesrcq);
1515 nes_rem_ref(&nesqp->ibqp);
1523 static struct ib_cq *nes_create_cq(struct ib_device *ibdev, int entries,
1525 struct ib_ucontext *context, struct ib_udata *udata)
1528 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1529 struct nes_device *nesdev = nesvnic->nesdev;
1530 struct nes_adapter *nesadapter = nesdev->nesadapter;
1531 struct nes_cq *nescq;
1532 struct nes_ucontext *nes_ucontext = NULL;
1533 struct nes_cqp_request *cqp_request;
1535 struct nes_hw_cqp_wqe *cqp_wqe;
1536 struct nes_pbl *nespbl = NULL;
1537 struct nes_create_cq_req req;
1538 struct nes_create_cq_resp resp;
1541 u32 pbl_entries = 1;
1543 unsigned long flags;
1546 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1547 nesadapter->max_cq, &cq_num, &nesadapter->next_cq);
1549 return ERR_PTR(err);
1552 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1554 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1555 nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1556 return ERR_PTR(-ENOMEM);
1559 nescq->hw_cq.cq_size = max(entries + 1, 5);
1560 nescq->hw_cq.cq_number = cq_num;
1561 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1565 nes_ucontext = to_nesucontext(context);
1566 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1567 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1569 return ERR_PTR(-EFAULT);
1571 nesvnic->mcrq_ucontext = nes_ucontext;
1572 nes_ucontext->mcrqf = req.mcrqf;
1573 if (nes_ucontext->mcrqf) {
1574 if (nes_ucontext->mcrqf & 0x80000000)
1575 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1576 else if (nes_ucontext->mcrqf & 0x40000000)
1577 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1579 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1580 nescq->mcrqf = nes_ucontext->mcrqf;
1581 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1583 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1584 (unsigned long)req.user_cq_buffer, entries);
1586 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1587 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1588 list_del(&nespbl->list);
1590 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1596 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1598 return ERR_PTR(-EFAULT);
1601 pbl_entries = nespbl->pbl_size >> 3;
1602 nescq->cq_mem_size = 0;
1604 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1605 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1606 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1608 /* allocate the physical buffer space */
1609 mem = pci_alloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1610 &nescq->hw_cq.cq_pbase);
1612 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1613 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1615 return ERR_PTR(-ENOMEM);
1618 memset(mem, 0, nescq->cq_mem_size);
1619 nescq->hw_cq.cq_vbase = mem;
1620 nescq->hw_cq.cq_head = 0;
1621 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1622 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1623 (u32)nescq->hw_cq.cq_pbase);
1626 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1627 spin_lock_init(&nescq->lock);
1629 /* send CreateCQ request to CQP */
1630 cqp_request = nes_get_cqp_request(nesdev);
1631 if (cqp_request == NULL) {
1632 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1634 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1635 nescq->hw_cq.cq_pbase);
1637 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1638 nespbl->pbl_vbase, nespbl->pbl_pbase);
1642 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1644 return ERR_PTR(-ENOMEM);
1646 cqp_request->waiting = 1;
1647 cqp_wqe = &cqp_request->cqp_wqe;
1649 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1650 NES_CQP_CQ_CHK_OVERFLOW |
1651 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1653 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1655 if (pbl_entries != 1) {
1656 if (pbl_entries > 32) {
1658 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1659 if (nesadapter->free_4kpbl == 0) {
1660 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1661 nes_free_cqp_request(nesdev, cqp_request);
1663 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1664 nescq->hw_cq.cq_pbase);
1666 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1667 nespbl->pbl_vbase, nespbl->pbl_pbase);
1670 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1672 return ERR_PTR(-ENOMEM);
1674 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1675 nescq->virtual_cq = 2;
1676 nesadapter->free_4kpbl--;
1679 /* use 256 byte pbl */
1680 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1681 if (nesadapter->free_256pbl == 0) {
1682 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1683 nes_free_cqp_request(nesdev, cqp_request);
1685 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1686 nescq->hw_cq.cq_pbase);
1688 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1689 nespbl->pbl_vbase, nespbl->pbl_pbase);
1692 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1694 return ERR_PTR(-ENOMEM);
1696 opcode |= NES_CQP_CQ_VIRT;
1697 nescq->virtual_cq = 1;
1698 nesadapter->free_256pbl--;
1703 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1705 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1706 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1707 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1708 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1711 if (pbl_entries != 1)
1712 u64temp = (u64)nespbl->pbl_pbase;
1714 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1715 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1716 nes_ucontext->mmap_db_index[0]);
1718 u64temp = (u64)nescq->hw_cq.cq_pbase;
1719 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1721 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1722 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1723 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1724 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1725 cpu_to_le32((u32)(u64temp >> 1));
1726 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1727 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1729 atomic_set(&cqp_request->refcount, 2);
1730 nes_post_cqp_request(nesdev, cqp_request);
1733 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1734 nescq->hw_cq.cq_number);
1735 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1736 NES_EVENT_TIMEOUT * 2);
1737 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1738 nescq->hw_cq.cq_number, ret);
1739 if ((!ret) || (cqp_request->major_code)) {
1740 nes_put_cqp_request(nesdev, cqp_request);
1742 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1743 nescq->hw_cq.cq_pbase);
1745 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1746 nespbl->pbl_vbase, nespbl->pbl_pbase);
1749 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1751 return ERR_PTR(-EIO);
1753 nes_put_cqp_request(nesdev, cqp_request);
1756 /* free the nespbl */
1757 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1760 resp.cq_id = nescq->hw_cq.cq_number;
1761 resp.cq_size = nescq->hw_cq.cq_size;
1762 resp.mmap_db_index = 0;
1763 if (ib_copy_to_udata(udata, &resp, sizeof resp)) {
1764 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1766 return ERR_PTR(-EFAULT);
1770 return &nescq->ibcq;
1777 static int nes_destroy_cq(struct ib_cq *ib_cq)
1779 struct nes_cq *nescq;
1780 struct nes_device *nesdev;
1781 struct nes_vnic *nesvnic;
1782 struct nes_adapter *nesadapter;
1783 struct nes_hw_cqp_wqe *cqp_wqe;
1784 struct nes_cqp_request *cqp_request;
1785 unsigned long flags;
1792 nescq = to_nescq(ib_cq);
1793 nesvnic = to_nesvnic(ib_cq->device);
1794 nesdev = nesvnic->nesdev;
1795 nesadapter = nesdev->nesadapter;
1797 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1799 /* Send DestroyCQ request to CQP */
1800 cqp_request = nes_get_cqp_request(nesdev);
1801 if (cqp_request == NULL) {
1802 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1805 cqp_request->waiting = 1;
1806 cqp_wqe = &cqp_request->cqp_wqe;
1807 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1808 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1809 if (nescq->virtual_cq == 1) {
1810 nesadapter->free_256pbl++;
1811 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1812 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1813 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1815 } else if (nescq->virtual_cq == 2) {
1816 nesadapter->free_4kpbl++;
1817 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1818 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1819 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1821 opcode |= NES_CQP_CQ_4KB_CHUNK;
1824 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1826 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1827 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1828 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1829 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1831 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1833 atomic_set(&cqp_request->refcount, 2);
1834 nes_post_cqp_request(nesdev, cqp_request);
1837 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1838 nescq->hw_cq.cq_number);
1839 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1841 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1842 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1843 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1844 cqp_request->minor_code);
1846 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1847 nescq->hw_cq.cq_number);
1849 } else if (cqp_request->major_code) {
1850 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1851 nescq->hw_cq.cq_number);
1856 nes_put_cqp_request(nesdev, cqp_request);
1858 if (nescq->cq_mem_size)
1859 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1860 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1869 static u32 root_256(struct nes_device *nesdev,
1870 struct nes_root_vpbl *root_vpbl,
1871 struct nes_root_vpbl *new_root,
1877 if (pbl_count_4k == 1) {
1878 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1879 512, &new_root->pbl_pbase);
1881 if (new_root->pbl_vbase == NULL)
1884 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1885 for (i = 0; i < 16; i++) {
1886 new_root->pbl_vbase[i].pa_low =
1887 cpu_to_le32((u32)leaf_pbl);
1888 new_root->pbl_vbase[i].pa_high =
1889 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1893 for (i = 3; i >= 0; i--) {
1895 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1896 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1897 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1899 for (k = 1; k < 16; k++) {
1901 root_vpbl->pbl_vbase[j + k].pa_low =
1902 cpu_to_le32((u32)leaf_pbl);
1903 root_vpbl->pbl_vbase[j + k].pa_high =
1904 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1916 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1917 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1918 dma_addr_t single_buffer, u16 pbl_count_4k,
1919 u16 residual_page_count_4k, int acc, u64 *iova_start,
1920 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1922 struct nes_hw_cqp_wqe *cqp_wqe;
1923 struct nes_cqp_request *cqp_request;
1924 unsigned long flags;
1926 struct nes_adapter *nesadapter = nesdev->nesadapter;
1928 u16 pbl_count_256 = 0;
1930 u8 use_256_pbls = 0;
1932 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1933 struct nes_root_vpbl new_root = {0, 0, 0};
1937 /* Register the region with the adapter */
1938 cqp_request = nes_get_cqp_request(nesdev);
1939 if (cqp_request == NULL) {
1940 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1943 cqp_request->waiting = 1;
1944 cqp_wqe = &cqp_request->cqp_wqe;
1947 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1949 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1950 pbl_count_256 = (pg_cnt + 31) / 32;
1952 if (pbl_count_256 <= nesadapter->free_256pbl)
1954 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1956 } else if (pg_cnt <= 2048) {
1957 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1958 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1960 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1963 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1967 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1972 pbl_count = pbl_count_256;
1973 nesadapter->free_256pbl -= pbl_count + use_two_level;
1974 } else if (use_4k_pbls) {
1975 pbl_count = pbl_count_4k;
1976 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1978 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1979 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1980 nes_free_cqp_request(nesdev, cqp_request);
1984 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1987 if (use_256_pbls && use_two_level) {
1988 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1989 if (new_root.pbl_pbase != 0)
1990 root_vpbl = &new_root;
1992 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1993 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1996 if (pbl_count_4k == 1)
1998 pbl_count = pbl_count_4k;
2000 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
2001 nesadapter->free_4kpbl -= pbl_count + use_two_level;
2004 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2006 if (use_4k_pbls == 0)
2011 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
2012 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
2013 if (acc & IB_ACCESS_LOCAL_WRITE)
2014 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
2015 if (acc & IB_ACCESS_REMOTE_WRITE)
2016 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
2017 if (acc & IB_ACCESS_REMOTE_READ)
2018 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
2019 if (acc & IB_ACCESS_MW_BIND)
2020 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
2022 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2023 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
2024 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
2025 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
2027 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
2028 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
2029 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
2030 cpu_to_le32(nespd->pd_id & 0x00007fff);
2031 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
2033 if (pbl_count == 0) {
2034 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
2036 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
2037 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
2038 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
2041 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
2045 atomic_set(&cqp_request->refcount, 2);
2046 nes_post_cqp_request(nesdev, cqp_request);
2049 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
2051 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
2052 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2053 stag, ret, cqp_request->major_code, cqp_request->minor_code);
2054 major_code = cqp_request->major_code;
2055 nes_put_cqp_request(nesdev, cqp_request);
2057 if ((!ret || major_code) && pbl_count != 0) {
2058 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2060 nesadapter->free_256pbl += pbl_count + use_two_level;
2061 else if (use_4k_pbls)
2062 nesadapter->free_4kpbl += pbl_count + use_two_level;
2063 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2065 if (new_root.pbl_pbase)
2066 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
2067 new_root.pbl_pbase);
2071 else if (major_code)
2074 *actual_pbl_cnt = pbl_count + use_two_level;
2075 *used_4k_pbls = use_4k_pbls;
2083 static struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd,
2084 struct ib_phys_buf *buffer_list, int num_phys_buf, int acc,
2088 struct nes_pd *nespd = to_nespd(ib_pd);
2089 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2090 struct nes_device *nesdev = nesvnic->nesdev;
2091 struct nes_adapter *nesadapter = nesdev->nesadapter;
2092 struct nes_mr *nesmr;
2094 struct nes_vpbl vpbl;
2095 struct nes_root_vpbl root_vpbl;
2100 u32 next_stag_index = 0;
2102 u32 root_pbl_index = 0;
2103 u32 cur_pbl_index = 0;
2104 int err = 0, pbl_depth = 0;
2112 vpbl.pbl_vbase = NULL;
2113 root_vpbl.pbl_vbase = NULL;
2114 root_vpbl.pbl_pbase = 0;
2116 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2117 stag_key = (u8)next_stag_index;
2121 next_stag_index >>= 8;
2122 next_stag_index %= nesadapter->max_mr;
2123 if (num_phys_buf > (1024*512)) {
2124 return ERR_PTR(-E2BIG);
2127 if ((buffer_list[0].addr ^ *iova_start) & ~PAGE_MASK)
2128 return ERR_PTR(-EINVAL);
2130 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2131 &stag_index, &next_stag_index);
2133 return ERR_PTR(err);
2136 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2138 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2139 return ERR_PTR(-ENOMEM);
2142 for (i = 0; i < num_phys_buf; i++) {
2144 if ((i & 0x01FF) == 0) {
2145 if (root_pbl_index == 1) {
2146 /* Allocate the root PBL */
2147 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 8192,
2148 &root_vpbl.pbl_pbase);
2149 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2150 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2151 if (!root_vpbl.pbl_vbase) {
2152 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2154 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2156 return ERR_PTR(-ENOMEM);
2158 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024, GFP_KERNEL);
2159 if (!root_vpbl.leaf_vpbl) {
2160 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2161 root_vpbl.pbl_pbase);
2162 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2164 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2166 return ERR_PTR(-ENOMEM);
2168 root_vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)vpbl.pbl_pbase);
2169 root_vpbl.pbl_vbase[0].pa_high =
2170 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2171 root_vpbl.leaf_vpbl[0] = vpbl;
2173 /* Allocate a 4K buffer for the PBL */
2174 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2176 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2177 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2178 if (!vpbl.pbl_vbase) {
2179 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2180 ibmr = ERR_PTR(-ENOMEM);
2184 /* Fill in the root table */
2185 if (1 <= root_pbl_index) {
2186 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2187 cpu_to_le32((u32)vpbl.pbl_pbase);
2188 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2189 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2190 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2196 mask = !buffer_list[i].size;
2198 mask |= buffer_list[i].addr;
2199 if (i != num_phys_buf - 1)
2200 mask |= buffer_list[i].addr + buffer_list[i].size;
2202 if (mask & ~PAGE_MASK) {
2203 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2204 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2205 ibmr = ERR_PTR(-EINVAL);
2210 region_length += buffer_list[i].size;
2211 if ((i != 0) && (single_page)) {
2212 if ((buffer_list[i-1].addr+PAGE_SIZE) != buffer_list[i].addr)
2215 vpbl.pbl_vbase[cur_pbl_index].pa_low = cpu_to_le32((u32)buffer_list[i].addr & PAGE_MASK);
2216 vpbl.pbl_vbase[cur_pbl_index++].pa_high =
2217 cpu_to_le32((u32)((((u64)buffer_list[i].addr) >> 32)));
2220 stag = stag_index << 8;
2222 stag += (u32)stag_key;
2224 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2225 " length = 0x%016lX, index = 0x%08X\n",
2226 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2228 /* Make the leaf PBL the root if only one PBL */
2229 if (root_pbl_index == 1) {
2230 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2236 pbl_count = root_pbl_index;
2238 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2239 buffer_list[0].addr, pbl_count, (u16)cur_pbl_index, acc, iova_start,
2240 &nesmr->pbls_used, &nesmr->pbl_4k);
2243 nesmr->ibmr.rkey = stag;
2244 nesmr->ibmr.lkey = stag;
2245 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2246 ibmr = &nesmr->ibmr;
2249 ibmr = ERR_PTR(-ENOMEM);
2253 /* free the resources */
2254 if (root_pbl_index == 1) {
2255 /* single PBL case */
2256 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2258 for (i=0; i<root_pbl_index; i++) {
2259 pci_free_consistent(nesdev->pcidev, 4096, root_vpbl.leaf_vpbl[i].pbl_vbase,
2260 root_vpbl.leaf_vpbl[i].pbl_pbase);
2262 kfree(root_vpbl.leaf_vpbl);
2263 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2264 root_vpbl.pbl_pbase);
2274 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2276 struct ib_phys_buf bl;
2279 nes_debug(NES_DBG_MR, "\n");
2281 bl.size = (u64)0xffffffffffULL;
2283 return nes_reg_phys_mr(pd, &bl, 1, acc, &kva);
2290 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2291 u64 virt, int acc, struct ib_udata *udata)
2296 dma_addr_t last_dma_addr = 0;
2297 dma_addr_t first_dma_addr = 0;
2298 struct nes_pd *nespd = to_nespd(pd);
2299 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2300 struct nes_device *nesdev = nesvnic->nesdev;
2301 struct nes_adapter *nesadapter = nesdev->nesadapter;
2302 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2303 struct ib_umem_chunk *chunk;
2304 struct nes_ucontext *nes_ucontext;
2305 struct nes_pbl *nespbl;
2306 struct nes_mr *nesmr;
2307 struct ib_umem *region;
2308 struct nes_mem_reg_req req;
2309 struct nes_vpbl vpbl;
2310 struct nes_root_vpbl root_vpbl;
2311 int nmap_index, page_index;
2313 int err, pbl_depth = 0;
2318 u32 next_stag_index;
2320 u32 root_pbl_index = 0;
2321 u32 cur_pbl_index = 0;
2327 region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
2328 if (IS_ERR(region)) {
2329 return (struct ib_mr *)region;
2332 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2333 " offset = %u, page size = %u.\n",
2334 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2335 region->offset, region->page_size);
2337 skip_pages = ((u32)region->offset) >> 12;
2339 if (ib_copy_from_udata(&req, udata, sizeof(req)))
2340 return ERR_PTR(-EFAULT);
2341 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2343 switch (req.reg_type) {
2344 case IWNES_MEMREG_TYPE_MEM:
2347 vpbl.pbl_vbase = NULL;
2348 root_vpbl.pbl_vbase = NULL;
2349 root_vpbl.pbl_pbase = 0;
2351 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2352 stag_key = (u8)next_stag_index;
2354 driver_key = next_stag_index & 0x70000000;
2356 next_stag_index >>= 8;
2357 next_stag_index %= nesadapter->max_mr;
2359 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2360 nesadapter->max_mr, &stag_index, &next_stag_index);
2362 ib_umem_release(region);
2363 return ERR_PTR(err);
2366 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2368 ib_umem_release(region);
2369 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2370 return ERR_PTR(-ENOMEM);
2372 nesmr->region = region;
2374 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2375 nes_debug(NES_DBG_MR, "Chunk: nents = %u, nmap = %u .\n",
2376 chunk->nents, chunk->nmap);
2377 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2378 if (sg_dma_address(&chunk->page_list[nmap_index]) & ~PAGE_MASK) {
2379 ib_umem_release(region);
2380 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2381 nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2382 (unsigned int) sg_dma_address(&chunk->page_list[nmap_index]));
2383 ibmr = ERR_PTR(-EINVAL);
2385 goto reg_user_mr_err;
2388 if (!sg_dma_len(&chunk->page_list[nmap_index])) {
2389 ib_umem_release(region);
2390 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2392 nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2393 ibmr = ERR_PTR(-EINVAL);
2395 goto reg_user_mr_err;
2398 region_length += sg_dma_len(&chunk->page_list[nmap_index]);
2399 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2400 region_length -= skip_pages << 12;
2401 for (page_index=skip_pages; page_index < chunk_pages; page_index++) {
2403 if ((page_count!=0)&&(page_count<<12)-(region->offset&(4096-1))>=region->length)
2405 if ((page_count&0x01FF) == 0) {
2406 if (page_count >= 1024 * 512) {
2407 ib_umem_release(region);
2408 nes_free_resource(nesadapter,
2409 nesadapter->allocated_mrs, stag_index);
2411 ibmr = ERR_PTR(-E2BIG);
2412 goto reg_user_mr_err;
2414 if (root_pbl_index == 1) {
2415 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2416 8192, &root_vpbl.pbl_pbase);
2417 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2418 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2419 if (!root_vpbl.pbl_vbase) {
2420 ib_umem_release(region);
2421 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2423 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2426 ibmr = ERR_PTR(-ENOMEM);
2427 goto reg_user_mr_err;
2429 root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2431 if (!root_vpbl.leaf_vpbl) {
2432 ib_umem_release(region);
2433 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2434 root_vpbl.pbl_pbase);
2435 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2437 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2440 ibmr = ERR_PTR(-ENOMEM);
2441 goto reg_user_mr_err;
2443 root_vpbl.pbl_vbase[0].pa_low =
2444 cpu_to_le32((u32)vpbl.pbl_pbase);
2445 root_vpbl.pbl_vbase[0].pa_high =
2446 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2447 root_vpbl.leaf_vpbl[0] = vpbl;
2449 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2451 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2452 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2453 if (!vpbl.pbl_vbase) {
2454 ib_umem_release(region);
2455 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2456 ibmr = ERR_PTR(-ENOMEM);
2458 goto reg_user_mr_err;
2460 if (1 <= root_pbl_index) {
2461 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2462 cpu_to_le32((u32)vpbl.pbl_pbase);
2463 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2464 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2465 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2471 if (page_count != 0) {
2472 if ((last_dma_addr+4096) !=
2473 (sg_dma_address(&chunk->page_list[nmap_index])+
2476 last_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2479 first_dma_addr = sg_dma_address(&chunk->page_list[nmap_index])+
2481 last_dma_addr = first_dma_addr;
2485 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2486 cpu_to_le32((u32)(sg_dma_address(&chunk->page_list[nmap_index])+
2487 (page_index*4096)));
2488 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2489 cpu_to_le32((u32)((((u64)(sg_dma_address(&chunk->page_list[nmap_index])+
2490 (page_index*4096))) >> 32)));
2497 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2498 " stag_key=0x%08x\n",
2499 stag_index, driver_key, stag_key);
2500 stag = stag_index << 8;
2502 stag += (u32)stag_key;
2508 /* Make the leaf PBL the root if only one PBL */
2509 if (root_pbl_index == 1) {
2510 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2516 pbl_count = root_pbl_index;
2519 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2520 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2521 stag, (unsigned int)iova_start,
2522 (unsigned int)region_length, stag_index,
2523 (unsigned long long)region->length, pbl_count);
2524 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2525 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2526 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2528 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2531 nesmr->ibmr.rkey = stag;
2532 nesmr->ibmr.lkey = stag;
2533 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2534 ibmr = &nesmr->ibmr;
2536 ib_umem_release(region);
2538 ibmr = ERR_PTR(-ENOMEM);
2542 /* free the resources */
2543 if (root_pbl_index == 1) {
2544 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2547 for (page_index=0; page_index<root_pbl_index; page_index++) {
2548 pci_free_consistent(nesdev->pcidev, 4096,
2549 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2550 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2552 kfree(root_vpbl.leaf_vpbl);
2553 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2554 root_vpbl.pbl_pbase);
2557 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2560 case IWNES_MEMREG_TYPE_QP:
2561 case IWNES_MEMREG_TYPE_CQ:
2562 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2564 nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2565 ib_umem_release(region);
2566 return ERR_PTR(-ENOMEM);
2568 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2570 ib_umem_release(region);
2572 nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2573 return ERR_PTR(-ENOMEM);
2575 nesmr->region = region;
2576 nes_ucontext = to_nesucontext(pd->uobject->context);
2577 pbl_depth = region->length >> 12;
2578 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2579 nespbl->pbl_size = pbl_depth*sizeof(u64);
2580 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2581 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2583 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2586 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2587 nespbl->pbl_size, pbl_depth);
2588 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2589 &nespbl->pbl_pbase);
2591 ib_umem_release(region);
2594 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2595 return ERR_PTR(-ENOMEM);
2598 nespbl->pbl_vbase = (u64 *)pbl;
2599 nespbl->user_base = start;
2600 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2601 " pbl_vbase=%p user_base=0x%lx\n",
2602 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2603 (void *) nespbl->pbl_vbase, nespbl->user_base);
2605 list_for_each_entry(chunk, ®ion->chunk_list, list) {
2606 for (nmap_index = 0; nmap_index < chunk->nmap; ++nmap_index) {
2607 chunk_pages = sg_dma_len(&chunk->page_list[nmap_index]) >> 12;
2608 chunk_pages += (sg_dma_len(&chunk->page_list[nmap_index]) & (4096-1)) ? 1 : 0;
2609 nespbl->page = sg_page(&chunk->page_list[0]);
2610 for (page_index=0; page_index<chunk_pages; page_index++) {
2611 ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2612 (sg_dma_address(&chunk->page_list[nmap_index])+
2613 (page_index*4096)));
2614 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2615 (sg_dma_address(&chunk->page_list[nmap_index])+
2616 (page_index*4096)))>>32);
2617 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2618 (unsigned long long)*pbl,
2619 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2624 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2625 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2627 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2629 nesmr->ibmr.rkey = -1;
2630 nesmr->ibmr.lkey = -1;
2631 nesmr->mode = req.reg_type;
2632 return &nesmr->ibmr;
2635 return ERR_PTR(-ENOSYS);
2642 static int nes_dereg_mr(struct ib_mr *ib_mr)
2644 struct nes_mr *nesmr = to_nesmr(ib_mr);
2645 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2646 struct nes_device *nesdev = nesvnic->nesdev;
2647 struct nes_adapter *nesadapter = nesdev->nesadapter;
2648 struct nes_hw_cqp_wqe *cqp_wqe;
2649 struct nes_cqp_request *cqp_request;
2650 unsigned long flags;
2655 if (nesmr->region) {
2656 ib_umem_release(nesmr->region);
2658 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2663 /* Deallocate the region with the adapter */
2665 cqp_request = nes_get_cqp_request(nesdev);
2666 if (cqp_request == NULL) {
2667 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2670 cqp_request->waiting = 1;
2671 cqp_wqe = &cqp_request->cqp_wqe;
2673 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2674 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2675 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2676 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2677 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2679 atomic_set(&cqp_request->refcount, 2);
2680 nes_post_cqp_request(nesdev, cqp_request);
2683 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2684 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2686 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2687 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2688 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2690 major_code = cqp_request->major_code;
2691 minor_code = cqp_request->minor_code;
2693 nes_put_cqp_request(nesdev, cqp_request);
2696 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2697 " ib_mr=%p, rkey = 0x%08X\n",
2698 ib_mr, ib_mr->rkey);
2700 } else if (major_code) {
2701 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2702 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2703 major_code, minor_code, ib_mr, ib_mr->rkey);
2707 if (nesmr->pbls_used != 0) {
2708 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2709 if (nesmr->pbl_4k) {
2710 nesadapter->free_4kpbl += nesmr->pbls_used;
2711 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2712 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2713 "exceeded the max(%u)\n",
2714 nesadapter->free_4kpbl,
2715 nesadapter->max_4kpbl);
2717 nesadapter->free_256pbl += nesmr->pbls_used;
2718 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2719 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2720 "exceeded the max(%u)\n",
2721 nesadapter->free_256pbl,
2722 nesadapter->max_256pbl);
2724 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2726 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2727 (ib_mr->rkey & 0x0fffff00) >> 8);
2738 static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2741 struct nes_ib_device *nesibdev =
2742 container_of(dev, struct nes_ib_device, ibdev.dev);
2743 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2745 nes_debug(NES_DBG_INIT, "\n");
2746 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2753 static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2756 struct nes_ib_device *nesibdev =
2757 container_of(dev, struct nes_ib_device, ibdev.dev);
2758 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2760 nes_debug(NES_DBG_INIT, "\n");
2761 return sprintf(buf, "%u.%u\n",
2762 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2763 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
2770 static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2773 nes_debug(NES_DBG_INIT, "\n");
2774 return sprintf(buf, "NES020\n");
2781 static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2784 nes_debug(NES_DBG_INIT, "\n");
2785 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2789 static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2790 static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2791 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2792 static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2794 static struct device_attribute *nes_dev_attributes[] = {
2805 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2806 int attr_mask, struct ib_qp_init_attr *init_attr)
2808 struct nes_qp *nesqp = to_nesqp(ibqp);
2810 nes_debug(NES_DBG_QP, "\n");
2812 attr->qp_access_flags = 0;
2813 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2814 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2815 attr->cap.max_recv_sge = 1;
2816 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
2817 init_attr->cap.max_inline_data = 0;
2819 init_attr->cap.max_inline_data = 64;
2822 init_attr->event_handler = nesqp->ibqp.event_handler;
2823 init_attr->qp_context = nesqp->ibqp.qp_context;
2824 init_attr->send_cq = nesqp->ibqp.send_cq;
2825 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2826 init_attr->srq = nesqp->ibqp.srq = nesqp->ibqp.srq;
2827 init_attr->cap = attr->cap;
2836 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2837 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2839 struct nes_hw_cqp_wqe *cqp_wqe;
2840 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2841 /* struct iw_cm_event cm_event; */
2842 struct nes_cqp_request *cqp_request;
2846 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2847 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2849 cqp_request = nes_get_cqp_request(nesdev);
2850 if (cqp_request == NULL) {
2851 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2854 if (wait_completion) {
2855 cqp_request->waiting = 1;
2857 cqp_request->waiting = 0;
2859 cqp_wqe = &cqp_request->cqp_wqe;
2861 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2862 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2863 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2864 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2865 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2866 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2867 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2869 /* If sending a terminate message, fill in the length (in words) */
2870 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2871 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2872 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2873 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2876 atomic_set(&cqp_request->refcount, 2);
2877 nes_post_cqp_request(nesdev, cqp_request);
2880 if (wait_completion) {
2881 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2882 nesqp->hwqp.qp_id); */
2883 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2885 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2886 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2887 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2888 major_code = cqp_request->major_code;
2890 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2891 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2892 nesqp->hwqp.qp_id, cqp_request->major_code,
2893 cqp_request->minor_code, next_iwarp_state);
2896 nes_put_cqp_request(nesdev, cqp_request);
2900 else if (major_code)
2913 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2914 int attr_mask, struct ib_udata *udata)
2916 struct nes_qp *nesqp = to_nesqp(ibqp);
2917 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2918 struct nes_device *nesdev = nesvnic->nesdev;
2921 u32 next_iwarp_state = 0;
2923 unsigned long qplockflags;
2925 u16 original_last_aeq;
2926 u8 issue_modify_qp = 0;
2927 u8 issue_disconnect = 0;
2930 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2931 " iwarp_state=0x%X, refcount=%d\n",
2932 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2933 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2935 spin_lock_irqsave(&nesqp->lock, qplockflags);
2937 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2938 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2939 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2940 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2942 if (attr_mask & IB_QP_STATE) {
2943 switch (attr->qp_state) {
2945 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2947 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2948 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2951 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2952 issue_modify_qp = 1;
2955 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2957 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2958 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2961 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2962 issue_modify_qp = 1;
2965 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2967 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2968 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2971 if (nesqp->cm_id == NULL) {
2972 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2973 nesqp->hwqp.qp_id );
2974 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2977 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2978 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2979 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2980 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2981 issue_modify_qp = 1;
2982 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2983 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2984 nesqp->hte_added = 1;
2987 issue_modify_qp = 1;
2988 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2989 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2990 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2991 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2994 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2995 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2996 " ignored due to current iWARP state\n",
2998 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3001 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
3002 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
3003 " already done based on hw state.\n",
3005 issue_modify_qp = 0;
3007 switch (nesqp->hw_iwarp_state) {
3008 case NES_AEQE_IWARP_STATE_CLOSING:
3009 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3010 case NES_AEQE_IWARP_STATE_TERMINATE:
3011 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3013 case NES_AEQE_IWARP_STATE_ERROR:
3014 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3017 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
3018 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
3024 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
3026 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
3027 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3030 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3031 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
3032 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
3033 issue_modify_qp = 1;
3037 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
3038 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3041 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
3043 if (nesqp->term_flags)
3044 del_timer(&nesqp->terminate_timer);
3046 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
3047 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
3048 if (nesqp->hte_added) {
3049 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
3050 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
3051 nesqp->hte_added = 0;
3053 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
3054 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
3055 next_iwarp_state |= NES_CQP_QP_RESET;
3057 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
3058 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
3061 issue_modify_qp = 1;
3062 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
3065 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3070 nesqp->ibqp_state = attr->qp_state;
3071 if (((nesqp->iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) ==
3072 (u32)NES_CQP_QP_IWARP_STATE_RTS) &&
3073 ((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) >
3074 (u32)NES_CQP_QP_IWARP_STATE_RTS)) {
3075 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3076 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3077 nesqp->iwarp_state);
3078 issue_disconnect = 1;
3080 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
3081 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
3082 nesqp->iwarp_state);
3086 if (attr_mask & IB_QP_ACCESS_FLAGS) {
3087 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
3088 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3089 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3090 issue_modify_qp = 1;
3092 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
3093 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
3094 issue_modify_qp = 1;
3096 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
3097 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
3098 issue_modify_qp = 1;
3100 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
3101 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
3102 issue_modify_qp = 1;
3105 if (nesqp->user_mode) {
3106 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
3107 NES_QPCONTEXT_MISC_RDMA_READ_EN);
3108 issue_modify_qp = 1;
3112 original_last_aeq = nesqp->last_aeq;
3113 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3115 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
3120 if (issue_modify_qp) {
3121 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
3122 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
3124 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
3125 " failed for QP%u.\n",
3126 next_iwarp_state, nesqp->hwqp.qp_id);
3130 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
3131 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
3132 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3133 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3134 original_last_aeq, nesqp->last_aeq);
3136 ((original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) &&
3139 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
3140 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
3141 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3142 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3143 original_last_aeq, nesqp->last_aeq);
3144 /* this one is for the cm_disconnect thread */
3145 spin_lock_irqsave(&nesqp->lock, qplockflags);
3146 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
3147 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
3148 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3149 nes_cm_disconn(nesqp);
3151 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
3152 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3155 spin_lock_irqsave(&nesqp->lock, qplockflags);
3157 /* These two are for the timer thread */
3158 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3159 nesqp->cm_id->add_ref(nesqp->cm_id);
3160 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3161 " need ae to finish up, original_last_aeq = 0x%04X."
3162 " last_aeq = 0x%04X, scheduling timer.\n",
3163 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3164 original_last_aeq, nesqp->last_aeq);
3165 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3167 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3169 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3170 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3171 " need ae to finish up, original_last_aeq = 0x%04X."
3172 " last_aeq = 0x%04X.\n",
3173 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3174 original_last_aeq, nesqp->last_aeq);
3178 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3179 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3180 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3181 original_last_aeq, nesqp->last_aeq);
3184 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3185 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3186 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3187 original_last_aeq, nesqp->last_aeq);
3192 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3193 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3200 * nes_muticast_attach
3202 static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3204 nes_debug(NES_DBG_INIT, "\n");
3210 * nes_multicast_detach
3212 static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3214 nes_debug(NES_DBG_INIT, "\n");
3222 static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3223 u8 port_num, struct ib_wc *in_wc, struct ib_grh *in_grh,
3224 struct ib_mad *in_mad, struct ib_mad *out_mad)
3226 nes_debug(NES_DBG_INIT, "\n");
3231 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3234 int total_payload_length = 0;
3235 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3236 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3237 ib_wr->sg_list[sge_index].addr);
3238 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3239 ib_wr->sg_list[sge_index].length);
3241 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3242 (ib_wr->sg_list[sge_index].lkey));
3244 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3246 total_payload_length += ib_wr->sg_list[sge_index].length;
3248 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3249 total_payload_length);
3250 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3251 total_payload_length);
3257 static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3258 struct ib_send_wr **bad_wr)
3261 unsigned long flags = 0;
3262 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3263 struct nes_device *nesdev = nesvnic->nesdev;
3264 struct nes_qp *nesqp = to_nesqp(ibqp);
3265 struct nes_hw_qp_wqe *wqe;
3267 u32 qsize = nesqp->hwqp.sq_size;
3273 if (nesqp->ibqp_state > IB_QPS_RTS) {
3278 spin_lock_irqsave(&nesqp->lock, flags);
3280 head = nesqp->hwqp.sq_head;
3283 /* Check for QP error */
3284 if (nesqp->term_flags) {
3289 /* Check for SQ overflow */
3290 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3295 wqe = &nesqp->hwqp.sq_vbase[head];
3296 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3297 nesqp->hwqp.qp_id, wqe, head); */
3298 nes_fill_init_qp_wqe(wqe, nesqp, head);
3299 u64temp = (u64)(ib_wr->wr_id);
3300 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3302 switch (ib_wr->opcode) {
3304 case IB_WR_SEND_WITH_INV:
3305 if (IB_WR_SEND == ib_wr->opcode) {
3306 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3307 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3309 wqe_misc = NES_IWARP_SQ_OP_SEND;
3311 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3312 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3314 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
3316 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3317 ib_wr->ex.invalidate_rkey);
3320 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3325 if (ib_wr->send_flags & IB_SEND_FENCE)
3326 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3328 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3329 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3330 (ib_wr->sg_list[0].length <= 64)) {
3331 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3332 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3333 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3334 ib_wr->sg_list[0].length);
3335 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3337 fill_wqe_sg_send(wqe, ib_wr, 1);
3341 case IB_WR_RDMA_WRITE:
3342 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3343 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3344 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3345 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3350 if (ib_wr->send_flags & IB_SEND_FENCE)
3351 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3353 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3354 ib_wr->wr.rdma.rkey);
3355 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3356 ib_wr->wr.rdma.remote_addr);
3358 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3359 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3360 (ib_wr->sg_list[0].length <= 64)) {
3361 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3362 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3363 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3364 ib_wr->sg_list[0].length);
3365 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3367 fill_wqe_sg_send(wqe, ib_wr, 1);
3370 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3371 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3373 case IB_WR_RDMA_READ:
3374 case IB_WR_RDMA_READ_WITH_INV:
3375 /* iWARP only supports 1 sge for RDMA reads */
3376 if (ib_wr->num_sge > 1) {
3377 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3382 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3383 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3385 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3386 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3387 ib_wr->ex.invalidate_rkey);
3390 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3391 ib_wr->wr.rdma.remote_addr);
3392 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3393 ib_wr->wr.rdma.rkey);
3394 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3395 ib_wr->sg_list->length);
3396 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3397 ib_wr->sg_list->addr);
3398 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3399 ib_wr->sg_list->lkey);
3401 case IB_WR_LOCAL_INV:
3402 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3403 set_wqe_32bit_value(wqe->wqe_words,
3404 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3405 ib_wr->ex.invalidate_rkey);
3407 case IB_WR_FAST_REG_MR:
3410 int flags = ib_wr->wr.fast_reg.access_flags;
3411 struct nes_ib_fast_reg_page_list *pnesfrpl =
3412 container_of(ib_wr->wr.fast_reg.page_list,
3413 struct nes_ib_fast_reg_page_list,
3415 u64 *src_page_list = pnesfrpl->ibfrpl.page_list;
3416 u64 *dst_page_list = pnesfrpl->nes_wqe_pbl.kva;
3418 if (ib_wr->wr.fast_reg.page_list_len >
3419 (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3420 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3424 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3425 set_wqe_64bit_value(wqe->wqe_words,
3426 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3427 ib_wr->wr.fast_reg.iova_start);
3428 set_wqe_32bit_value(wqe->wqe_words,
3429 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3430 ib_wr->wr.fast_reg.length);
3431 set_wqe_32bit_value(wqe->wqe_words,
3432 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3433 ib_wr->wr.fast_reg.rkey);
3434 /* Set page size: */
3435 if (ib_wr->wr.fast_reg.page_shift == 12) {
3436 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3437 } else if (ib_wr->wr.fast_reg.page_shift == 21) {
3438 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3440 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3441 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3445 /* Set access_flags */
3446 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3447 if (flags & IB_ACCESS_LOCAL_WRITE)
3448 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3450 if (flags & IB_ACCESS_REMOTE_WRITE)
3451 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3453 if (flags & IB_ACCESS_REMOTE_READ)
3454 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3456 if (flags & IB_ACCESS_MW_BIND)
3457 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3459 /* Fill in PBL info: */
3460 if (ib_wr->wr.fast_reg.page_list_len >
3461 pnesfrpl->ibfrpl.max_page_list_len) {
3462 nes_debug(NES_DBG_IW_TX, "Invalid page list length,"
3463 " ib_wr=%p, value=%u, max=%u\n",
3464 ib_wr, ib_wr->wr.fast_reg.page_list_len,
3465 pnesfrpl->ibfrpl.max_page_list_len);
3470 set_wqe_64bit_value(wqe->wqe_words,
3471 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3472 pnesfrpl->nes_wqe_pbl.paddr);
3474 set_wqe_32bit_value(wqe->wqe_words,
3475 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3476 ib_wr->wr.fast_reg.page_list_len * 8);
3478 for (i = 0; i < ib_wr->wr.fast_reg.page_list_len; i++)
3479 dst_page_list[i] = cpu_to_le64(src_page_list[i]);
3481 nes_debug(NES_DBG_IW_TX, "SQ_FMR: iova_start: %p, "
3482 "length: %d, rkey: %0x, pgl_paddr: %p, "
3483 "page_list_len: %u, wqe_misc: %x\n",
3484 (void *)ib_wr->wr.fast_reg.iova_start,
3485 ib_wr->wr.fast_reg.length,
3486 ib_wr->wr.fast_reg.rkey,
3487 (void *)pnesfrpl->nes_wqe_pbl.paddr,
3488 ib_wr->wr.fast_reg.page_list_len,
3501 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3502 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3504 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3506 ib_wr = ib_wr->next;
3514 nesqp->hwqp.sq_head = head;
3517 counter = min(wqe_count, ((u32)255));
3518 wqe_count -= counter;
3519 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3520 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3523 spin_unlock_irqrestore(&nesqp->lock, flags);
3535 static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3536 struct ib_recv_wr **bad_wr)
3539 unsigned long flags = 0;
3540 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3541 struct nes_device *nesdev = nesvnic->nesdev;
3542 struct nes_qp *nesqp = to_nesqp(ibqp);
3543 struct nes_hw_qp_wqe *wqe;
3546 u32 qsize = nesqp->hwqp.rq_size;
3550 u32 total_payload_length;
3552 if (nesqp->ibqp_state > IB_QPS_RTS) {
3557 spin_lock_irqsave(&nesqp->lock, flags);
3559 head = nesqp->hwqp.rq_head;
3562 /* Check for QP error */
3563 if (nesqp->term_flags) {
3568 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3572 /* Check for RQ overflow */
3573 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3578 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3579 wqe = &nesqp->hwqp.rq_vbase[head];
3581 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3582 nesqp->hwqp.qp_id, wqe, head); */
3583 nes_fill_init_qp_wqe(wqe, nesqp, head);
3584 u64temp = (u64)(ib_wr->wr_id);
3585 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3587 total_payload_length = 0;
3588 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3589 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3590 ib_wr->sg_list[sge_index].addr);
3591 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3592 ib_wr->sg_list[sge_index].length);
3593 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3594 ib_wr->sg_list[sge_index].lkey);
3596 total_payload_length += ib_wr->sg_list[sge_index].length;
3598 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3599 total_payload_length);
3601 ib_wr = ib_wr->next;
3608 nesqp->hwqp.rq_head = head;
3611 counter = min(wqe_count, ((u32)255));
3612 wqe_count -= counter;
3613 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3616 spin_unlock_irqrestore(&nesqp->lock, flags);
3628 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3632 unsigned long flags = 0;
3633 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3634 struct nes_device *nesdev = nesvnic->nesdev;
3635 struct nes_cq *nescq = to_nescq(ibcq);
3636 struct nes_qp *nesqp;
3637 struct nes_hw_cqe cqe;
3644 u32 move_cq_head = 1;
3647 nes_debug(NES_DBG_CQ, "\n");
3649 spin_lock_irqsave(&nescq->lock, flags);
3651 head = nescq->hw_cq.cq_head;
3652 cq_size = nescq->hw_cq.cq_size;
3654 while (cqe_count < num_entries) {
3655 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3656 NES_CQE_VALID) == 0)
3660 * Make sure we read CQ entry contents *after*
3661 * we've checked the valid bit.
3665 cqe = nescq->hw_cq.cq_vbase[head];
3666 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3667 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3668 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3669 /* parse CQE, get completion context from WQE (either rq or sq) */
3670 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3674 nesqp = (struct nes_qp *)(unsigned long)u64temp;
3675 memset(entry, 0, sizeof *entry);
3676 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3677 entry->status = IB_WC_SUCCESS;
3679 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3680 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3681 entry->status = err_code & 0x0000ffff;
3683 /* The rest of the cqe's will be marked as flushed */
3684 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3685 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3686 NES_IWARP_CQE_MINOR_FLUSH);
3688 entry->status = IB_WC_WR_FLUSH_ERR;
3691 entry->qp = &nesqp->ibqp;
3692 entry->src_qp = nesqp->hwqp.qp_id;
3694 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3695 if (nesqp->skip_lsmm) {
3696 nesqp->skip_lsmm = 0;
3697 nesqp->hwqp.sq_tail++;
3700 /* Working on a SQ Completion*/
3701 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3702 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3703 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3704 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3705 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3706 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3708 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3709 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3710 case NES_IWARP_SQ_OP_RDMAW:
3711 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3712 entry->opcode = IB_WC_RDMA_WRITE;
3714 case NES_IWARP_SQ_OP_RDMAR:
3715 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3716 entry->opcode = IB_WC_RDMA_READ;
3717 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3718 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3720 case NES_IWARP_SQ_OP_SENDINV:
3721 case NES_IWARP_SQ_OP_SENDSEINV:
3722 case NES_IWARP_SQ_OP_SEND:
3723 case NES_IWARP_SQ_OP_SENDSE:
3724 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3725 entry->opcode = IB_WC_SEND;
3727 case NES_IWARP_SQ_OP_LOCINV:
3728 entry->opcode = IB_WR_LOCAL_INV;
3730 case NES_IWARP_SQ_OP_FAST_REG:
3731 entry->opcode = IB_WC_FAST_REG_MR;
3735 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3736 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3738 wq_tail = nesqp->hwqp.sq_tail;
3741 /* Working on a RQ Completion*/
3742 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3743 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3744 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3745 entry->opcode = IB_WC_RECV;
3747 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3748 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3750 wq_tail = nesqp->hwqp.rq_tail;
3754 entry->wr_id = wrid;
3760 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3761 if (++head >= cq_size)
3763 nescq->polled_completions++;
3765 if ((nescq->polled_completions > (cq_size / 2)) ||
3766 (nescq->polled_completions == 255)) {
3767 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3768 " are pending %u of %u.\n",
3769 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3770 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3771 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3772 nescq->polled_completions = 0;
3775 /* Update the wqe index and set status to flush */
3776 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3777 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3778 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3779 cpu_to_le32(wqe_index);
3780 move_cq_head = 1; /* ready for next pass */
3784 if (nescq->polled_completions) {
3785 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3786 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3787 nescq->polled_completions = 0;
3790 nescq->hw_cq.cq_head = head;
3791 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3792 cqe_count, nescq->hw_cq.cq_number);
3794 spin_unlock_irqrestore(&nescq->lock, flags);
3803 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3805 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3806 struct nes_device *nesdev = nesvnic->nesdev;
3807 struct nes_cq *nescq = to_nescq(ibcq);
3810 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3811 nescq->hw_cq.cq_number);
3813 cq_arm = nescq->hw_cq.cq_number;
3814 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3815 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3816 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3817 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3821 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3822 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3829 * nes_init_ofa_device
3831 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3833 struct nes_ib_device *nesibdev;
3834 struct nes_vnic *nesvnic = netdev_priv(netdev);
3835 struct nes_device *nesdev = nesvnic->nesdev;
3837 nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3838 if (nesibdev == NULL) {
3841 strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3842 nesibdev->ibdev.owner = THIS_MODULE;
3844 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3845 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3846 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3848 nesibdev->ibdev.uverbs_cmd_mask =
3849 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3850 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3851 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3852 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3853 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3854 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3855 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3856 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3857 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3858 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3859 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3860 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3861 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3862 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3863 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3864 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3865 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3866 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3867 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3868 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3869 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3870 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3872 nesibdev->ibdev.phys_port_cnt = 1;
3873 nesibdev->ibdev.num_comp_vectors = 1;
3874 nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3875 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3876 nesibdev->ibdev.query_device = nes_query_device;
3877 nesibdev->ibdev.query_port = nes_query_port;
3878 nesibdev->ibdev.modify_port = nes_modify_port;
3879 nesibdev->ibdev.query_pkey = nes_query_pkey;
3880 nesibdev->ibdev.query_gid = nes_query_gid;
3881 nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3882 nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3883 nesibdev->ibdev.mmap = nes_mmap;
3884 nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3885 nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3886 nesibdev->ibdev.create_ah = nes_create_ah;
3887 nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3888 nesibdev->ibdev.create_qp = nes_create_qp;
3889 nesibdev->ibdev.modify_qp = nes_modify_qp;
3890 nesibdev->ibdev.query_qp = nes_query_qp;
3891 nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3892 nesibdev->ibdev.create_cq = nes_create_cq;
3893 nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3894 nesibdev->ibdev.poll_cq = nes_poll_cq;
3895 nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3896 nesibdev->ibdev.reg_phys_mr = nes_reg_phys_mr;
3897 nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3898 nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3899 nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3900 nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3901 nesibdev->ibdev.bind_mw = nes_bind_mw;
3903 nesibdev->ibdev.alloc_fast_reg_mr = nes_alloc_fast_reg_mr;
3904 nesibdev->ibdev.alloc_fast_reg_page_list = nes_alloc_fast_reg_page_list;
3905 nesibdev->ibdev.free_fast_reg_page_list = nes_free_fast_reg_page_list;
3907 nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3908 nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3909 nesibdev->ibdev.process_mad = nes_process_mad;
3911 nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3912 nesibdev->ibdev.post_send = nes_post_send;
3913 nesibdev->ibdev.post_recv = nes_post_recv;
3915 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3916 if (nesibdev->ibdev.iwcm == NULL) {
3917 ib_dealloc_device(&nesibdev->ibdev);
3920 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3921 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3922 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3923 nesibdev->ibdev.iwcm->connect = nes_connect;
3924 nesibdev->ibdev.iwcm->accept = nes_accept;
3925 nesibdev->ibdev.iwcm->reject = nes_reject;
3926 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3927 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3934 * nes_destroy_ofa_device
3936 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3938 if (nesibdev == NULL)
3941 nes_unregister_ofa_device(nesibdev);
3943 kfree(nesibdev->ibdev.iwcm);
3944 ib_dealloc_device(&nesibdev->ibdev);
3949 * nes_register_ofa_device
3951 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3953 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3954 struct nes_device *nesdev = nesvnic->nesdev;
3955 struct nes_adapter *nesadapter = nesdev->nesadapter;
3958 ret = ib_register_device(&nesvnic->nesibdev->ibdev);
3963 /* Get the resources allocated to this device */
3964 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3965 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3966 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3967 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3969 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3970 ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3974 device_remove_file(&nesibdev->ibdev.dev,
3975 nes_dev_attributes[i]);
3977 ib_unregister_device(&nesibdev->ibdev);
3982 nesvnic->of_device_registered = 1;
3989 * nes_unregister_ofa_device
3991 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3993 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3996 for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3997 device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
4000 if (nesvnic->of_device_registered) {
4001 ib_unregister_device(&nesibdev->ibdev);
4004 nesvnic->of_device_registered = 0;