2 * Copyright (c) 2006 - 2011 Intel Corporation. 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 <linux/slab.h>
39 #include <asm/byteorder.h>
41 #include <rdma/ib_verbs.h>
42 #include <rdma/iw_cm.h>
43 #include <rdma/ib_user_verbs.h>
44 #include <rdma/uverbs_ioctl.h>
48 #include <rdma/ib_umem.h>
50 atomic_t mod_qp_timouts;
52 atomic_t sw_qps_destroyed;
54 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
55 static int nes_dereg_mr(struct ib_mr *ib_mr);
60 static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd, enum ib_mw_type type,
61 struct ib_udata *udata)
63 struct nes_pd *nespd = to_nespd(ibpd);
64 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
65 struct nes_device *nesdev = nesvnic->nesdev;
66 struct nes_adapter *nesadapter = nesdev->nesadapter;
67 struct nes_cqp_request *cqp_request;
70 struct nes_hw_cqp_wqe *cqp_wqe;
74 u32 next_stag_index = 0;
78 if (type != IB_MW_TYPE_1)
79 return ERR_PTR(-EINVAL);
81 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
82 stag_key = (u8)next_stag_index;
86 next_stag_index >>= 8;
87 next_stag_index %= nesadapter->max_mr;
89 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
90 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_MW);
95 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
97 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
98 return ERR_PTR(-ENOMEM);
101 stag = stag_index << 8;
103 stag += (u32)stag_key;
105 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
108 /* Register the region with the adapter */
109 cqp_request = nes_get_cqp_request(nesdev);
110 if (cqp_request == NULL) {
112 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
113 return ERR_PTR(-ENOMEM);
116 cqp_request->waiting = 1;
117 cqp_wqe = &cqp_request->cqp_wqe;
119 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
120 cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
121 NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
122 NES_CQP_STAG_REM_ACC_EN);
124 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
125 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
126 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
128 atomic_set(&cqp_request->refcount, 2);
129 nes_post_cqp_request(nesdev, cqp_request);
132 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
134 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
135 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
136 stag, ret, cqp_request->major_code, cqp_request->minor_code);
137 if ((!ret) || (cqp_request->major_code)) {
138 nes_put_cqp_request(nesdev, cqp_request);
140 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
142 return ERR_PTR(-ETIME);
144 return ERR_PTR(-ENOMEM);
147 nes_put_cqp_request(nesdev, cqp_request);
149 nesmr->ibmw.rkey = stag;
150 nesmr->mode = IWNES_MEMREG_TYPE_MW;
153 nesmr->pbls_used = 0;
162 static int nes_dealloc_mw(struct ib_mw *ibmw)
164 struct nes_mr *nesmr = to_nesmw(ibmw);
165 struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
166 struct nes_device *nesdev = nesvnic->nesdev;
167 struct nes_adapter *nesadapter = nesdev->nesadapter;
168 struct nes_hw_cqp_wqe *cqp_wqe;
169 struct nes_cqp_request *cqp_request;
173 /* Deallocate the window with the adapter */
174 cqp_request = nes_get_cqp_request(nesdev);
175 if (cqp_request == NULL) {
176 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
179 cqp_request->waiting = 1;
180 cqp_wqe = &cqp_request->cqp_wqe;
181 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
182 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
183 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
185 atomic_set(&cqp_request->refcount, 2);
186 nes_post_cqp_request(nesdev, cqp_request);
189 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
191 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
193 nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
194 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
195 ret, cqp_request->major_code, cqp_request->minor_code);
198 else if (cqp_request->major_code)
201 nes_put_cqp_request(nesdev, cqp_request);
203 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
204 (ibmw->rkey & 0x0fffff00) >> 8);
214 static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
215 u32 stag, u32 page_count)
217 struct nes_hw_cqp_wqe *cqp_wqe;
218 struct nes_cqp_request *cqp_request;
221 struct nes_adapter *nesadapter = nesdev->nesadapter;
224 u64 region_length = page_count * PAGE_SIZE;
227 cqp_request = nes_get_cqp_request(nesdev);
228 if (cqp_request == NULL) {
229 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
232 nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
233 "region_length = %llu\n",
234 page_count, region_length);
235 cqp_request->waiting = 1;
236 cqp_wqe = &cqp_request->cqp_wqe;
238 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
239 if (nesadapter->free_4kpbl > 0) {
240 nesadapter->free_4kpbl--;
241 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
243 /* No 4kpbl's available: */
244 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
245 nes_debug(NES_DBG_MR, "Out of Pbls\n");
246 nes_free_cqp_request(nesdev, cqp_request);
250 opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
251 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
252 NES_CQP_STAG_REM_ACC_EN;
254 * The current OFED API does not support the zero based TO option.
255 * If added then need to changed the NES_CQP_STAG_VA* option. Also,
256 * the API does not support that ability to have the MR set for local
257 * access only when created and not allow the SQ op to override. Given
258 * this the remote enable must be set here.
261 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
262 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
263 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
265 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
266 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
267 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
268 cpu_to_le32(nespd->pd_id & 0x00007fff);
270 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
271 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
272 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
273 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
274 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
275 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
278 atomic_set(&cqp_request->refcount, 2);
279 nes_post_cqp_request(nesdev, cqp_request);
282 ret = wait_event_timeout(cqp_request->waitq,
283 (0 != cqp_request->request_done),
286 nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
287 "wait_event_timeout ret = %u, CQP Major:Minor codes = "
288 "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
289 cqp_request->minor_code);
290 major_code = cqp_request->major_code;
291 nes_put_cqp_request(nesdev, cqp_request);
293 if (!ret || major_code) {
294 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
295 nesadapter->free_4kpbl++;
296 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
309 static struct ib_mr *nes_alloc_mr(struct ib_pd *ibpd,
310 enum ib_mr_type mr_type,
313 struct nes_pd *nespd = to_nespd(ibpd);
314 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
315 struct nes_device *nesdev = nesvnic->nesdev;
316 struct nes_adapter *nesadapter = nesdev->nesadapter;
323 struct nes_mr *nesmr;
328 if (mr_type != IB_MR_TYPE_MEM_REG)
329 return ERR_PTR(-EINVAL);
331 if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
332 return ERR_PTR(-E2BIG);
335 * Note: Set to always use a fixed length single page entry PBL. This is to allow
336 * for the fast_reg_mr operation to always know the size of the PBL.
338 if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
339 return ERR_PTR(-E2BIG);
341 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
342 stag_key = (u8)next_stag_index;
343 next_stag_index >>= 8;
344 next_stag_index %= nesadapter->max_mr;
346 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
347 nesadapter->max_mr, &stag_index,
348 &next_stag_index, NES_RESOURCE_FAST_MR);
352 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
354 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
355 return ERR_PTR(-ENOMEM);
358 stag = stag_index << 8;
360 stag += (u32)stag_key;
362 nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
365 ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_num_sg);
368 nesmr->ibmr.rkey = stag;
369 nesmr->ibmr.lkey = stag;
370 nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
374 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
375 return ERR_PTR(-ENOMEM);
378 nesmr->pages = pci_alloc_consistent(nesdev->pcidev,
379 max_num_sg * sizeof(u64),
384 nesmr->max_pages = max_num_sg;
391 return ERR_PTR(-ENOMEM);
394 static int nes_set_page(struct ib_mr *ibmr, u64 addr)
396 struct nes_mr *nesmr = to_nesmr(ibmr);
398 if (unlikely(nesmr->npages == nesmr->max_pages))
401 nesmr->pages[nesmr->npages++] = cpu_to_le64(addr);
406 static int nes_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
407 int sg_nents, unsigned int *sg_offset)
409 struct nes_mr *nesmr = to_nesmr(ibmr);
413 return ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, nes_set_page);
419 static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
420 struct ib_udata *uhw)
422 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
423 struct nes_device *nesdev = nesvnic->nesdev;
424 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
426 if (uhw->inlen || uhw->outlen)
429 memset(props, 0, sizeof(*props));
430 memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
432 props->fw_ver = nesdev->nesadapter->firmware_version;
433 props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
434 props->vendor_id = nesdev->nesadapter->vendor_id;
435 props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
436 props->hw_ver = nesdev->nesadapter->hw_rev;
437 props->max_mr_size = 0x80000000;
438 props->max_qp = nesibdev->max_qp;
439 props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
440 props->max_send_sge = nesdev->nesadapter->max_sge;
441 props->max_recv_sge = nesdev->nesadapter->max_sge;
442 props->max_cq = nesibdev->max_cq;
443 props->max_cqe = nesdev->nesadapter->max_cqe;
444 props->max_mr = nesibdev->max_mr;
445 props->max_mw = nesibdev->max_mr;
446 props->max_pd = nesibdev->max_pd;
447 props->max_sge_rd = 1;
448 switch (nesdev->nesadapter->max_irrq_wr) {
450 props->max_qp_rd_atom = 2;
453 props->max_qp_rd_atom = 8;
456 props->max_qp_rd_atom = 32;
459 props->max_qp_rd_atom = 64;
462 props->max_qp_rd_atom = 0;
464 props->max_qp_init_rd_atom = props->max_qp_rd_atom;
465 props->atomic_cap = IB_ATOMIC_NONE;
466 props->max_map_per_fmr = 1;
475 static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
477 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
478 struct net_device *netdev = nesvnic->netdev;
480 /* props being zeroed by the caller, avoid zeroing it here */
482 props->max_mtu = IB_MTU_4096;
483 props->active_mtu = ib_mtu_int_to_enum(netdev->mtu);
486 if (netif_queue_stopped(netdev))
487 props->state = IB_PORT_DOWN;
488 else if (nesvnic->linkup)
489 props->state = IB_PORT_ACTIVE;
491 props->state = IB_PORT_DOWN;
492 props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
493 IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
494 props->gid_tbl_len = 1;
495 props->pkey_tbl_len = 1;
496 props->active_width = IB_WIDTH_4X;
497 props->active_speed = IB_SPEED_SDR;
498 props->max_msg_sz = 0x80000000;
506 static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
516 static int nes_query_gid(struct ib_device *ibdev, u8 port,
517 int index, union ib_gid *gid)
519 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
521 memset(&(gid->raw[0]), 0, sizeof(gid->raw));
522 memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
529 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
530 * of all objects associated with a particular user-mode client.
532 static int nes_alloc_ucontext(struct ib_ucontext *uctx, struct ib_udata *udata)
534 struct ib_device *ibdev = uctx->device;
535 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
536 struct nes_device *nesdev = nesvnic->nesdev;
537 struct nes_adapter *nesadapter = nesdev->nesadapter;
538 struct nes_alloc_ucontext_req req;
539 struct nes_alloc_ucontext_resp uresp = {};
540 struct nes_ucontext *nes_ucontext = to_nesucontext(uctx);
541 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
544 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
545 printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
549 if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
550 printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
551 req.userspace_ver, NES_ABI_USERSPACE_VER);
556 uresp.max_qps = nesibdev->max_qp;
557 uresp.max_pds = nesibdev->max_pd;
558 uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
559 uresp.virtwq = nesadapter->virtwq;
560 uresp.kernel_ver = NES_ABI_KERNEL_VER;
562 nes_ucontext->nesdev = nesdev;
563 nes_ucontext->mmap_wq_offset = uresp.max_pds;
564 nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
565 ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
569 if (ib_copy_to_udata(udata, &uresp, sizeof(uresp)))
572 INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
573 INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
578 * nes_dealloc_ucontext
580 static void nes_dealloc_ucontext(struct ib_ucontext *context)
588 static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
591 struct nes_vnic *nesvnic = to_nesvnic(context->device);
592 struct nes_device *nesdev = nesvnic->nesdev;
593 /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
594 struct nes_ucontext *nes_ucontext;
595 struct nes_qp *nesqp;
597 nes_ucontext = to_nesucontext(context);
600 if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
601 index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
602 index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
603 PAGE_SIZE-1) & (~(PAGE_SIZE-1));
604 if (!test_bit(index, nes_ucontext->allocated_wqs)) {
605 nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
608 nesqp = nes_ucontext->mmap_nesqp[index];
610 nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
613 if (remap_pfn_range(vma, vma->vm_start,
614 virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
615 vma->vm_end - vma->vm_start,
616 vma->vm_page_prot)) {
617 nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
620 vma->vm_private_data = nesqp;
623 index = vma->vm_pgoff;
624 if (!test_bit(index, nes_ucontext->allocated_doorbells))
627 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
628 if (io_remap_pfn_range(vma, vma->vm_start,
629 (nesdev->doorbell_start +
630 ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
631 >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
633 vma->vm_private_data = nes_ucontext;
644 static int nes_alloc_pd(struct ib_pd *pd, struct ib_ucontext *context,
645 struct ib_udata *udata)
647 struct ib_device *ibdev = pd->device;
648 struct nes_pd *nespd = to_nespd(pd);
649 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
650 struct nes_device *nesdev = nesvnic->nesdev;
651 struct nes_adapter *nesadapter = nesdev->nesadapter;
652 struct nes_ucontext *nesucontext;
653 struct nes_alloc_pd_resp uresp;
657 nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
658 nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
659 netdev_refcnt_read(nesvnic->netdev));
661 err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
662 nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
666 nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
667 nespd, dev_name(&nesvnic->nesibdev->ibdev.dev));
669 nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
672 nesucontext = to_nesucontext(context);
673 nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
674 NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
675 nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
676 nespd->mmap_db_index, nespd->pd_id);
677 if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
678 nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
679 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
683 uresp.pd_id = nespd->pd_id;
684 uresp.mmap_db_index = nespd->mmap_db_index;
685 if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
686 nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
690 set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
691 nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
692 nesucontext->first_free_db = nespd->mmap_db_index + 1;
695 nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
703 static void nes_dealloc_pd(struct ib_pd *ibpd)
705 struct nes_ucontext *nesucontext;
706 struct nes_pd *nespd = to_nespd(ibpd);
707 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
708 struct nes_device *nesdev = nesvnic->nesdev;
709 struct nes_adapter *nesadapter = nesdev->nesadapter;
711 if ((ibpd->uobject) && (ibpd->uobject->context)) {
712 nesucontext = to_nesucontext(ibpd->uobject->context);
713 nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
714 nespd->mmap_db_index);
715 clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
716 nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
717 if (nesucontext->first_free_db > nespd->mmap_db_index) {
718 nesucontext->first_free_db = nespd->mmap_db_index;
722 nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
723 nespd->pd_id, nespd);
724 nes_free_resource(nesadapter, nesadapter->allocated_pds,
725 (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
730 * nes_get_encoded_size
732 static inline u8 nes_get_encoded_size(int *size)
738 } else if (*size <= 128) {
741 } else if (*size <= 512) {
745 return (encoded_size);
753 static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
754 struct nes_vnic *nesvnic, int sq_size, int rq_size)
761 struct nes_device *nesdev = nesvnic->nesdev;
762 struct nes_adapter *nesadapter = nesdev->nesadapter;
767 pbl_entries = nespbl->pbl_size >> 3;
768 nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
769 nespbl->pbl_size, pbl_entries,
770 (void *)nespbl->pbl_vbase,
771 (unsigned long) nespbl->pbl_pbase);
772 pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
773 /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
774 /* the first pbl to be fro the rq_vbase... */
775 rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
776 sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
777 nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
779 nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
784 nesqp->hwqp.sq_vbase = kmap(nespbl->page);
785 nesqp->page = nespbl->page;
786 if (!nesqp->hwqp.sq_vbase) {
787 nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
792 /* Now to get to sq.. we need to calculate how many */
793 /* PBL entries were used by the rq.. */
794 pbl += sq_pbl_entries;
795 nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
796 /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
797 /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
799 nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
800 nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
801 nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
802 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
803 if (!nesadapter->free_256pbl) {
804 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
806 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
811 nesadapter->free_256pbl--;
812 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
814 nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
815 pblbuffer = nesqp->pbl_vbase;
816 if (!nesqp->pbl_vbase) {
817 /* memory allocated during nes_reg_user_mr() */
818 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
821 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
822 nesadapter->free_256pbl++;
823 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
827 memset(nesqp->pbl_vbase, 0, 256);
828 /* fill in the page address in the pbl buffer.. */
829 tpbl = pblbuffer + 16;
830 pbl = (__le64 *)nespbl->pbl_vbase;
831 while (sq_pbl_entries--)
834 while (rq_pbl_entries--)
837 /* done with memory allocated during nes_reg_user_mr() */
838 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
843 max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256; /* this is Q2 */
844 /* Round up to a multiple of a page */
845 nesqp->qp_mem_size += PAGE_SIZE - 1;
846 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
848 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
849 &nesqp->hwqp.q2_pbase);
852 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
853 nesqp->pbl_vbase = NULL;
854 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
855 nesadapter->free_256pbl++;
856 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
860 nesqp->sq_kmapped = 1;
861 nesqp->hwqp.q2_vbase = mem;
863 memset(nesqp->hwqp.q2_vbase, 0, 256);
864 nesqp->nesqp_context = mem;
865 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
866 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
875 static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
876 int sq_size, int rq_size)
879 struct nes_device *nesdev = nesvnic->nesdev;
881 nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
882 (sizeof(struct nes_hw_qp_wqe) * rq_size) +
883 max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
884 256; /* this is Q2 */
885 /* Round up to a multiple of a page */
886 nesqp->qp_mem_size += PAGE_SIZE - 1;
887 nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
889 mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
890 &nesqp->hwqp.sq_pbase);
893 nes_debug(NES_DBG_QP, "PCI consistent memory for "
894 "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
895 mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
897 memset(mem, 0, nesqp->qp_mem_size);
899 nesqp->hwqp.sq_vbase = mem;
900 mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
902 nesqp->hwqp.rq_vbase = mem;
903 nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
904 sizeof(struct nes_hw_qp_wqe) * sq_size;
905 mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
907 nesqp->hwqp.q2_vbase = mem;
908 nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
909 sizeof(struct nes_hw_qp_wqe) * rq_size;
911 memset(nesqp->hwqp.q2_vbase, 0, 256);
913 nesqp->nesqp_context = mem;
914 nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
915 memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
921 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
923 static void nes_free_qp_mem(struct nes_device *nesdev,
924 struct nes_qp *nesqp, int virt_wqs)
927 struct nes_adapter *nesadapter = nesdev->nesadapter;
929 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
930 nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
932 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
933 nesadapter->free_256pbl++;
934 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
935 pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
936 pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
937 nesqp->pbl_vbase = NULL;
938 if (nesqp->sq_kmapped) {
939 nesqp->sq_kmapped = 0;
949 static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
950 struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
954 struct nes_pd *nespd = to_nespd(ibpd);
955 struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
956 struct nes_device *nesdev = nesvnic->nesdev;
957 struct nes_adapter *nesadapter = nesdev->nesadapter;
958 struct nes_qp *nesqp;
959 struct nes_cq *nescq;
960 struct nes_ucontext *nes_ucontext = rdma_udata_to_drv_context(
961 udata, struct nes_ucontext, ibucontext);
962 struct nes_hw_cqp_wqe *cqp_wqe;
963 struct nes_cqp_request *cqp_request;
964 struct nes_create_qp_req req;
965 struct nes_create_qp_resp uresp;
966 struct nes_pbl *nespbl = NULL;
969 /* u32 counter = 0; */
981 if (init_attr->create_flags)
982 return ERR_PTR(-EINVAL);
984 atomic_inc(&qps_created);
985 switch (init_attr->qp_type) {
987 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
988 init_attr->cap.max_inline_data = 0;
990 init_attr->cap.max_inline_data = 64;
992 sq_size = init_attr->cap.max_send_wr;
993 rq_size = init_attr->cap.max_recv_wr;
995 /* check if the encoded sizes are OK or not... */
996 sq_encoded_size = nes_get_encoded_size(&sq_size);
997 rq_encoded_size = nes_get_encoded_size(&rq_size);
999 if ((!sq_encoded_size) || (!rq_encoded_size)) {
1000 nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1002 return ERR_PTR(-EINVAL);
1005 init_attr->cap.max_send_wr = sq_size -2;
1006 init_attr->cap.max_recv_wr = rq_size -1;
1007 nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1009 ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1010 nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
1012 return ERR_PTR(ret);
1015 /* Need 512 (actually now 1024) byte alignment on this structure */
1016 mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1018 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1019 return ERR_PTR(-ENOMEM);
1021 u64nesqp = (unsigned long)mem;
1022 u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1023 u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1024 u64nesqp &= ~u64temp;
1025 nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1026 /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p. Rounded to closest %u\n",
1027 nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1028 nesqp->allocated_buffer = mem;
1031 if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1032 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1033 kfree(nesqp->allocated_buffer);
1034 nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1035 return ERR_PTR(-EFAULT);
1037 if (req.user_wqe_buffers) {
1040 if (req.user_qp_buffer)
1041 nesqp->nesuqp_addr = req.user_qp_buffer;
1043 nesqp->user_mode = 1;
1046 list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1047 if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1048 list_del(&nespbl->list);
1050 nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1051 nespbl, nespbl->user_base);
1056 nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1057 (long long unsigned int)req.user_wqe_buffers);
1058 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1059 kfree(nesqp->allocated_buffer);
1060 return ERR_PTR(-EFAULT);
1064 nesqp->mmap_sq_db_index =
1065 find_next_zero_bit(nes_ucontext->allocated_wqs,
1066 NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1067 /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1068 nespd->mmap_db_index); */
1069 if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1070 nes_debug(NES_DBG_QP,
1071 "db index > max user regions, failing create QP\n");
1072 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1074 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1078 kfree(nesqp->allocated_buffer);
1079 return ERR_PTR(-ENOMEM);
1081 set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1082 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1083 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1085 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1086 kfree(nesqp->allocated_buffer);
1087 return ERR_PTR(-EFAULT);
1090 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1091 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1093 nes_debug(NES_DBG_QP,
1094 "error geting qp mem code = %d\n", err);
1095 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1096 kfree(nesqp->allocated_buffer);
1097 return ERR_PTR(-ENOMEM);
1100 nesqp->hwqp.sq_size = sq_size;
1101 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1102 nesqp->hwqp.sq_head = 1;
1103 nesqp->hwqp.rq_size = rq_size;
1104 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1105 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1106 (void *)nesqp->nesqp_context_pbase);
1108 nesqp->hwqp.qp_id = qp_num;
1109 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1110 nesqp->nespd = nespd;
1112 nescq = to_nescq(init_attr->send_cq);
1113 nesqp->nesscq = nescq;
1114 nescq = to_nescq(init_attr->recv_cq);
1115 nesqp->nesrcq = nescq;
1117 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1118 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1119 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1120 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1121 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1122 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1124 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1125 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1127 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1128 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1129 u64temp = (u64)nesqp->hwqp.sq_pbase;
1130 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1131 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1135 u64temp = (u64)nesqp->hwqp.sq_pbase;
1136 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1137 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1138 u64temp = (u64)nesqp->hwqp.rq_pbase;
1139 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1140 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1142 u64temp = (u64)nesqp->pbl_pbase;
1143 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1144 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1147 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1148 nesvnic->next_qp_nic_index,
1149 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1150 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1151 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1152 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1153 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1154 nesvnic->next_qp_nic_index++;
1155 if ((nesvnic->next_qp_nic_index > 3) ||
1156 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1157 nesvnic->next_qp_nic_index = 0;
1159 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1161 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1162 u64temp = (u64)nesqp->hwqp.q2_pbase;
1163 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1164 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1165 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1166 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1167 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1168 NES_QPCONTEXT_ORDIRD_AAH |
1169 ((((u32)nesadapter->max_irrq_wr) <<
1170 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1171 if (disable_mpa_crc) {
1172 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1173 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1178 cqp_request = nes_get_cqp_request(nesdev);
1179 if (cqp_request == NULL) {
1180 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1181 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1182 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1183 kfree(nesqp->allocated_buffer);
1184 return ERR_PTR(-ENOMEM);
1186 cqp_request->waiting = 1;
1187 cqp_wqe = &cqp_request->cqp_wqe;
1190 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1191 NES_CQP_QP_IWARP_STATE_IDLE;
1193 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1194 NES_CQP_QP_IWARP_STATE_IDLE;
1196 opcode |= NES_CQP_QP_CQS_VALID;
1197 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1198 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1199 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1201 u64temp = (u64)nesqp->nesqp_context_pbase;
1202 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1204 atomic_set(&cqp_request->refcount, 2);
1205 nes_post_cqp_request(nesdev, cqp_request);
1208 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1210 ret = wait_event_timeout(cqp_request->waitq,
1211 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1212 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1213 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1214 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1215 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1216 cqp_request->major_code, cqp_request->minor_code);
1217 if ((!ret) || (cqp_request->major_code)) {
1218 nes_put_cqp_request(nesdev, cqp_request);
1219 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1220 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1221 kfree(nesqp->allocated_buffer);
1223 return ERR_PTR(-ETIME);
1225 return ERR_PTR(-EIO);
1229 nes_put_cqp_request(nesdev, cqp_request);
1232 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1233 uresp.mmap_rq_db_index = 0;
1234 uresp.actual_sq_size = sq_size;
1235 uresp.actual_rq_size = rq_size;
1236 uresp.qp_id = nesqp->hwqp.qp_id;
1237 uresp.nes_drv_opt = nes_drv_opt;
1238 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1239 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1240 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1241 kfree(nesqp->allocated_buffer);
1242 return ERR_PTR(-EFAULT);
1246 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1247 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1248 spin_lock_init(&nesqp->lock);
1249 nes_add_ref(&nesqp->ibqp);
1252 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1253 return ERR_PTR(-EINVAL);
1255 init_completion(&nesqp->sq_drained);
1256 init_completion(&nesqp->rq_drained);
1258 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1259 timer_setup(&nesqp->terminate_timer, nes_terminate_timeout, 0);
1261 /* update the QP table */
1262 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1263 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1264 netdev_refcnt_read(nesvnic->netdev));
1266 return &nesqp->ibqp;
1272 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1278 unsigned long flags = 0;
1280 spin_lock_irqsave(&nescq->lock, flags);
1282 cq_head = nescq->hw_cq.cq_head;
1283 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1285 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1286 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1287 u64temp = (((u64)hi) << 32) | ((u64)lo);
1288 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1289 if (u64temp == (u64)(unsigned long)nesqp) {
1290 /* Zero the context value so cqe will be ignored */
1291 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1292 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1295 if (++cq_head >= nescq->hw_cq.cq_size)
1299 spin_unlock_irqrestore(&nescq->lock, flags);
1306 static int nes_destroy_qp(struct ib_qp *ibqp)
1308 struct nes_qp *nesqp = to_nesqp(ibqp);
1309 struct nes_ucontext *nes_ucontext;
1310 struct ib_qp_attr attr;
1311 struct iw_cm_id *cm_id;
1312 struct iw_cm_event cm_event;
1315 atomic_inc(&sw_qps_destroyed);
1316 nesqp->destroyed = 1;
1318 /* Blow away the connection if it exists. */
1319 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1320 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1321 attr.qp_state = IB_QPS_ERR;
1322 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1325 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1326 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1327 cm_id = nesqp->cm_id;
1328 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1329 cm_event.status = -ETIMEDOUT;
1330 cm_event.local_addr = cm_id->local_addr;
1331 cm_event.remote_addr = cm_id->remote_addr;
1332 cm_event.private_data = NULL;
1333 cm_event.private_data_len = 0;
1335 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1336 "QP%u. cm_id = %p, refcount = %u. \n",
1337 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1339 cm_id->rem_ref(cm_id);
1340 ret = cm_id->event_handler(cm_id, &cm_event);
1342 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1345 if (nesqp->user_mode) {
1346 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1347 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1348 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1349 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1350 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1351 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1354 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1355 nesqp->sq_kmapped = 0;
1356 kunmap(nesqp->page);
1359 /* Clean any pending completions from the cq(s) */
1361 nes_clean_cq(nesqp, nesqp->nesscq);
1363 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1364 nes_clean_cq(nesqp, nesqp->nesrcq);
1366 nes_rem_ref(&nesqp->ibqp);
1374 static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1375 const struct ib_cq_init_attr *attr,
1376 struct ib_ucontext *context,
1377 struct ib_udata *udata)
1379 int entries = attr->cqe;
1381 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1382 struct nes_device *nesdev = nesvnic->nesdev;
1383 struct nes_adapter *nesadapter = nesdev->nesadapter;
1384 struct nes_cq *nescq;
1385 struct nes_ucontext *nes_ucontext = NULL;
1386 struct nes_cqp_request *cqp_request;
1388 struct nes_hw_cqp_wqe *cqp_wqe;
1389 struct nes_pbl *nespbl = NULL;
1390 struct nes_create_cq_req req;
1391 struct nes_create_cq_resp resp;
1394 u32 pbl_entries = 1;
1396 unsigned long flags;
1400 return ERR_PTR(-EINVAL);
1402 if (entries > nesadapter->max_cqe)
1403 return ERR_PTR(-EINVAL);
1405 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1406 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
1408 return ERR_PTR(err);
1411 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1413 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1414 return ERR_PTR(-ENOMEM);
1417 nescq->hw_cq.cq_size = max(entries + 1, 5);
1418 nescq->hw_cq.cq_number = cq_num;
1419 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1423 nes_ucontext = to_nesucontext(context);
1424 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1425 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1427 return ERR_PTR(-EFAULT);
1429 nesvnic->mcrq_ucontext = nes_ucontext;
1430 nes_ucontext->mcrqf = req.mcrqf;
1431 if (nes_ucontext->mcrqf) {
1432 if (nes_ucontext->mcrqf & 0x80000000)
1433 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1434 else if (nes_ucontext->mcrqf & 0x40000000)
1435 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1437 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1438 nescq->mcrqf = nes_ucontext->mcrqf;
1439 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1441 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1442 (unsigned long)req.user_cq_buffer, entries);
1444 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1445 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1446 list_del(&nespbl->list);
1448 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1454 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1456 return ERR_PTR(-EFAULT);
1459 pbl_entries = nespbl->pbl_size >> 3;
1460 nescq->cq_mem_size = 0;
1462 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1463 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1464 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1466 /* allocate the physical buffer space */
1467 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1468 &nescq->hw_cq.cq_pbase);
1470 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1471 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1473 return ERR_PTR(-ENOMEM);
1476 nescq->hw_cq.cq_vbase = mem;
1477 nescq->hw_cq.cq_head = 0;
1478 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1479 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1480 (u32)nescq->hw_cq.cq_pbase);
1483 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1484 spin_lock_init(&nescq->lock);
1486 /* send CreateCQ request to CQP */
1487 cqp_request = nes_get_cqp_request(nesdev);
1488 if (cqp_request == NULL) {
1489 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1491 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1492 nescq->hw_cq.cq_pbase);
1494 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1495 nespbl->pbl_vbase, nespbl->pbl_pbase);
1499 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1501 return ERR_PTR(-ENOMEM);
1503 cqp_request->waiting = 1;
1504 cqp_wqe = &cqp_request->cqp_wqe;
1506 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1507 NES_CQP_CQ_CHK_OVERFLOW |
1508 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1510 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1512 if (pbl_entries != 1) {
1513 if (pbl_entries > 32) {
1515 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1516 if (nesadapter->free_4kpbl == 0) {
1517 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1518 nes_free_cqp_request(nesdev, cqp_request);
1520 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1521 nescq->hw_cq.cq_pbase);
1523 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1524 nespbl->pbl_vbase, nespbl->pbl_pbase);
1527 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1529 return ERR_PTR(-ENOMEM);
1531 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1532 nescq->virtual_cq = 2;
1533 nesadapter->free_4kpbl--;
1536 /* use 256 byte pbl */
1537 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1538 if (nesadapter->free_256pbl == 0) {
1539 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1540 nes_free_cqp_request(nesdev, cqp_request);
1542 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1543 nescq->hw_cq.cq_pbase);
1545 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1546 nespbl->pbl_vbase, nespbl->pbl_pbase);
1549 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1551 return ERR_PTR(-ENOMEM);
1553 opcode |= NES_CQP_CQ_VIRT;
1554 nescq->virtual_cq = 1;
1555 nesadapter->free_256pbl--;
1560 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1562 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1563 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1564 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1565 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1568 if (pbl_entries != 1)
1569 u64temp = (u64)nespbl->pbl_pbase;
1571 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1572 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1573 nes_ucontext->mmap_db_index[0]);
1575 u64temp = (u64)nescq->hw_cq.cq_pbase;
1576 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1578 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1579 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1580 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1581 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1582 cpu_to_le32((u32)(u64temp >> 1));
1583 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1584 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1586 atomic_set(&cqp_request->refcount, 2);
1587 nes_post_cqp_request(nesdev, cqp_request);
1590 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1591 nescq->hw_cq.cq_number);
1592 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1593 NES_EVENT_TIMEOUT * 2);
1594 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1595 nescq->hw_cq.cq_number, ret);
1596 if ((!ret) || (cqp_request->major_code)) {
1597 nes_put_cqp_request(nesdev, cqp_request);
1599 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1600 nescq->hw_cq.cq_pbase);
1602 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1603 nespbl->pbl_vbase, nespbl->pbl_pbase);
1606 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1608 return ERR_PTR(-EIO);
1610 nes_put_cqp_request(nesdev, cqp_request);
1613 /* free the nespbl */
1614 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1617 resp.cq_id = nescq->hw_cq.cq_number;
1618 resp.cq_size = nescq->hw_cq.cq_size;
1619 resp.mmap_db_index = 0;
1620 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
1621 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1623 return ERR_PTR(-EFAULT);
1627 return &nescq->ibcq;
1634 static int nes_destroy_cq(struct ib_cq *ib_cq)
1636 struct nes_cq *nescq;
1637 struct nes_device *nesdev;
1638 struct nes_vnic *nesvnic;
1639 struct nes_adapter *nesadapter;
1640 struct nes_hw_cqp_wqe *cqp_wqe;
1641 struct nes_cqp_request *cqp_request;
1642 unsigned long flags;
1649 nescq = to_nescq(ib_cq);
1650 nesvnic = to_nesvnic(ib_cq->device);
1651 nesdev = nesvnic->nesdev;
1652 nesadapter = nesdev->nesadapter;
1654 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1656 /* Send DestroyCQ request to CQP */
1657 cqp_request = nes_get_cqp_request(nesdev);
1658 if (cqp_request == NULL) {
1659 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1662 cqp_request->waiting = 1;
1663 cqp_wqe = &cqp_request->cqp_wqe;
1664 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1665 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1666 if (nescq->virtual_cq == 1) {
1667 nesadapter->free_256pbl++;
1668 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1669 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1670 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1672 } else if (nescq->virtual_cq == 2) {
1673 nesadapter->free_4kpbl++;
1674 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1675 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1676 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1678 opcode |= NES_CQP_CQ_4KB_CHUNK;
1681 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1683 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1684 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1685 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1686 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1688 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1690 atomic_set(&cqp_request->refcount, 2);
1691 nes_post_cqp_request(nesdev, cqp_request);
1694 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1695 nescq->hw_cq.cq_number);
1696 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1698 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1699 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1700 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1701 cqp_request->minor_code);
1703 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1704 nescq->hw_cq.cq_number);
1706 } else if (cqp_request->major_code) {
1707 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1708 nescq->hw_cq.cq_number);
1713 nes_put_cqp_request(nesdev, cqp_request);
1715 if (nescq->cq_mem_size)
1716 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1717 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1726 static u32 root_256(struct nes_device *nesdev,
1727 struct nes_root_vpbl *root_vpbl,
1728 struct nes_root_vpbl *new_root,
1734 if (pbl_count_4k == 1) {
1735 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1736 512, &new_root->pbl_pbase);
1738 if (new_root->pbl_vbase == NULL)
1741 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1742 for (i = 0; i < 16; i++) {
1743 new_root->pbl_vbase[i].pa_low =
1744 cpu_to_le32((u32)leaf_pbl);
1745 new_root->pbl_vbase[i].pa_high =
1746 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1750 for (i = 3; i >= 0; i--) {
1752 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1753 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1754 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1756 for (k = 1; k < 16; k++) {
1758 root_vpbl->pbl_vbase[j + k].pa_low =
1759 cpu_to_le32((u32)leaf_pbl);
1760 root_vpbl->pbl_vbase[j + k].pa_high =
1761 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1773 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1774 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1775 dma_addr_t single_buffer, u16 pbl_count_4k,
1776 u16 residual_page_count_4k, int acc, u64 *iova_start,
1777 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1779 struct nes_hw_cqp_wqe *cqp_wqe;
1780 struct nes_cqp_request *cqp_request;
1781 unsigned long flags;
1783 struct nes_adapter *nesadapter = nesdev->nesadapter;
1785 u16 pbl_count_256 = 0;
1787 u8 use_256_pbls = 0;
1789 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1790 struct nes_root_vpbl new_root = { 0, NULL, NULL };
1794 /* Register the region with the adapter */
1795 cqp_request = nes_get_cqp_request(nesdev);
1796 if (cqp_request == NULL) {
1797 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1800 cqp_request->waiting = 1;
1801 cqp_wqe = &cqp_request->cqp_wqe;
1804 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1806 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1807 pbl_count_256 = (pg_cnt + 31) / 32;
1809 if (pbl_count_256 <= nesadapter->free_256pbl)
1811 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1813 } else if (pg_cnt <= 2048) {
1814 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1815 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1817 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1820 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1824 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1829 pbl_count = pbl_count_256;
1830 nesadapter->free_256pbl -= pbl_count + use_two_level;
1831 } else if (use_4k_pbls) {
1832 pbl_count = pbl_count_4k;
1833 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1835 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1836 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1837 nes_free_cqp_request(nesdev, cqp_request);
1841 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1844 if (use_256_pbls && use_two_level) {
1845 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1846 if (new_root.pbl_pbase != 0)
1847 root_vpbl = &new_root;
1849 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1850 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1853 if (pbl_count_4k == 1)
1855 pbl_count = pbl_count_4k;
1857 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1858 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1861 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1863 if (use_4k_pbls == 0)
1868 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1869 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1870 if (acc & IB_ACCESS_LOCAL_WRITE)
1871 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1872 if (acc & IB_ACCESS_REMOTE_WRITE)
1873 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1874 if (acc & IB_ACCESS_REMOTE_READ)
1875 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1876 if (acc & IB_ACCESS_MW_BIND)
1877 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1879 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1880 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1881 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1882 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1884 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1885 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1886 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1887 cpu_to_le32(nespd->pd_id & 0x00007fff);
1888 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1890 if (pbl_count == 0) {
1891 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1893 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1894 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
1895 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
1898 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1902 atomic_set(&cqp_request->refcount, 2);
1903 nes_post_cqp_request(nesdev, cqp_request);
1906 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1908 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1909 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1910 stag, ret, cqp_request->major_code, cqp_request->minor_code);
1911 major_code = cqp_request->major_code;
1912 nes_put_cqp_request(nesdev, cqp_request);
1914 if ((!ret || major_code) && pbl_count != 0) {
1915 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1917 nesadapter->free_256pbl += pbl_count + use_two_level;
1918 else if (use_4k_pbls)
1919 nesadapter->free_4kpbl += pbl_count + use_two_level;
1920 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1922 if (new_root.pbl_pbase)
1923 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1924 new_root.pbl_pbase);
1928 else if (major_code)
1931 *actual_pbl_cnt = pbl_count + use_two_level;
1932 *used_4k_pbls = use_4k_pbls;
1940 struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
1941 int acc, u64 *iova_start)
1944 struct nes_pd *nespd = to_nespd(ib_pd);
1945 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
1946 struct nes_device *nesdev = nesvnic->nesdev;
1947 struct nes_adapter *nesadapter = nesdev->nesadapter;
1948 struct nes_mr *nesmr;
1950 struct nes_vpbl vpbl;
1951 struct nes_root_vpbl root_vpbl;
1955 u32 next_stag_index = 0;
1964 vpbl.pbl_vbase = NULL;
1965 root_vpbl.pbl_vbase = NULL;
1966 root_vpbl.pbl_pbase = 0;
1968 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
1969 stag_key = (u8)next_stag_index;
1973 next_stag_index >>= 8;
1974 next_stag_index %= nesadapter->max_mr;
1976 if ((addr ^ *iova_start) & ~PAGE_MASK)
1977 return ERR_PTR(-EINVAL);
1979 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
1980 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
1982 return ERR_PTR(err);
1985 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
1987 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1988 return ERR_PTR(-ENOMEM);
1991 /* Allocate a 4K buffer for the PBL */
1992 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
1994 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
1995 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
1996 if (!vpbl.pbl_vbase) {
1997 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1998 ibmr = ERR_PTR(-ENOMEM);
2006 if (mask & ~PAGE_MASK) {
2007 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2008 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2009 ibmr = ERR_PTR(-EINVAL);
2014 region_length += size;
2015 vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2016 vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2018 stag = stag_index << 8;
2020 stag += (u32)stag_key;
2022 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2023 " length = 0x%016lX, index = 0x%08X\n",
2024 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2026 /* Make the leaf PBL the root if only one PBL */
2027 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2034 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2035 addr, pbl_count, 1, acc, iova_start,
2036 &nesmr->pbls_used, &nesmr->pbl_4k);
2039 nesmr->ibmr.rkey = stag;
2040 nesmr->ibmr.lkey = stag;
2041 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2042 ibmr = &nesmr->ibmr;
2045 ibmr = ERR_PTR(-ENOMEM);
2049 /* single PBL case */
2050 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2058 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2062 nes_debug(NES_DBG_MR, "\n");
2064 return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
2070 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2071 u64 virt, int acc, struct ib_udata *udata)
2076 dma_addr_t last_dma_addr = 0;
2077 dma_addr_t first_dma_addr = 0;
2078 struct nes_pd *nespd = to_nespd(pd);
2079 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2080 struct nes_device *nesdev = nesvnic->nesdev;
2081 struct nes_adapter *nesadapter = nesdev->nesadapter;
2082 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2083 struct sg_dma_page_iter dma_iter;
2084 struct nes_ucontext *nes_ucontext = rdma_udata_to_drv_context(
2085 udata, struct nes_ucontext, ibucontext);
2086 struct nes_pbl *nespbl;
2087 struct nes_mr *nesmr;
2088 struct ib_umem *region;
2089 struct nes_mem_reg_req req;
2090 struct nes_vpbl vpbl;
2091 struct nes_root_vpbl root_vpbl;
2094 int err, pbl_depth = 0;
2098 u32 next_stag_index;
2100 u32 root_pbl_index = 0;
2101 u32 cur_pbl_index = 0;
2107 region = ib_umem_get(udata, start, length, acc, 0);
2108 if (IS_ERR(region)) {
2109 return (struct ib_mr *)region;
2112 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2113 " offset = %u, page size = %lu.\n",
2114 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2115 ib_umem_offset(region), BIT(region->page_shift));
2117 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
2119 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2120 ib_umem_release(region);
2121 return ERR_PTR(-EFAULT);
2123 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2125 switch (req.reg_type) {
2126 case IWNES_MEMREG_TYPE_MEM:
2129 vpbl.pbl_vbase = NULL;
2130 root_vpbl.pbl_vbase = NULL;
2131 root_vpbl.pbl_pbase = 0;
2133 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2134 stag_key = (u8)next_stag_index;
2136 driver_key = next_stag_index & 0x70000000;
2138 next_stag_index >>= 8;
2139 next_stag_index %= nesadapter->max_mr;
2141 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2142 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
2144 ib_umem_release(region);
2145 return ERR_PTR(err);
2148 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2150 ib_umem_release(region);
2151 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2152 return ERR_PTR(-ENOMEM);
2154 nesmr->region = region;
2156 for_each_sg_dma_page (region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2158 region_length += PAGE_SIZE;
2159 region_length -= skip_pages << 12;
2161 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
2163 if ((page_count & 0x01FF) == 0) {
2164 if (page_count >= 1024 * 512) {
2165 ib_umem_release(region);
2166 nes_free_resource(nesadapter,
2167 nesadapter->allocated_mrs, stag_index);
2169 ibmr = ERR_PTR(-E2BIG);
2170 goto reg_user_mr_err;
2172 if (root_pbl_index == 1) {
2173 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2174 8192, &root_vpbl.pbl_pbase);
2175 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2176 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2177 if (!root_vpbl.pbl_vbase) {
2178 ib_umem_release(region);
2179 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2181 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2184 ibmr = ERR_PTR(-ENOMEM);
2185 goto reg_user_mr_err;
2187 root_vpbl.leaf_vpbl = kcalloc(1024,
2188 sizeof(*root_vpbl.leaf_vpbl),
2190 if (!root_vpbl.leaf_vpbl) {
2191 ib_umem_release(region);
2192 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2193 root_vpbl.pbl_pbase);
2194 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2196 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2199 ibmr = ERR_PTR(-ENOMEM);
2200 goto reg_user_mr_err;
2202 root_vpbl.pbl_vbase[0].pa_low =
2203 cpu_to_le32((u32)vpbl.pbl_pbase);
2204 root_vpbl.pbl_vbase[0].pa_high =
2205 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2206 root_vpbl.leaf_vpbl[0] = vpbl;
2208 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2210 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2211 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2212 if (!vpbl.pbl_vbase) {
2213 ib_umem_release(region);
2214 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2215 ibmr = ERR_PTR(-ENOMEM);
2217 goto reg_user_mr_err;
2219 if (1 <= root_pbl_index) {
2220 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2221 cpu_to_le32((u32)vpbl.pbl_pbase);
2222 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2223 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2224 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2230 if (page_count != 0) {
2231 if ((last_dma_addr + 4096) != sg_page_iter_dma_address(&dma_iter))
2233 last_dma_addr = sg_page_iter_dma_address(&dma_iter);
2235 first_dma_addr = sg_page_iter_dma_address(&dma_iter);
2236 last_dma_addr = first_dma_addr;
2240 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2241 cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2242 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2243 cpu_to_le32((u32)((u64)(sg_page_iter_dma_address(&dma_iter))));
2249 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2250 " stag_key=0x%08x\n",
2251 stag_index, driver_key, stag_key);
2252 stag = stag_index << 8;
2254 stag += (u32)stag_key;
2257 /* Make the leaf PBL the root if only one PBL */
2258 if (root_pbl_index == 1) {
2259 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2265 pbl_count = root_pbl_index;
2268 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2269 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2270 stag, (unsigned int)iova_start,
2271 (unsigned int)region_length, stag_index,
2272 (unsigned long long)region->length, pbl_count);
2273 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2274 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2275 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2277 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2280 nesmr->ibmr.rkey = stag;
2281 nesmr->ibmr.lkey = stag;
2282 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2283 ibmr = &nesmr->ibmr;
2285 ib_umem_release(region);
2287 ibmr = ERR_PTR(-ENOMEM);
2291 /* free the resources */
2292 if (root_pbl_index == 1) {
2293 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2296 for (page_index=0; page_index<root_pbl_index; page_index++) {
2297 pci_free_consistent(nesdev->pcidev, 4096,
2298 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2299 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2301 kfree(root_vpbl.leaf_vpbl);
2302 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2303 root_vpbl.pbl_pbase);
2306 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2309 case IWNES_MEMREG_TYPE_QP:
2310 case IWNES_MEMREG_TYPE_CQ:
2311 if (!region->length) {
2312 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2313 ib_umem_release(region);
2314 return ERR_PTR(-EINVAL);
2316 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2318 ib_umem_release(region);
2319 return ERR_PTR(-ENOMEM);
2321 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2323 ib_umem_release(region);
2325 return ERR_PTR(-ENOMEM);
2327 nesmr->region = region;
2328 pbl_depth = region->length >> 12;
2329 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2330 nespbl->pbl_size = pbl_depth*sizeof(u64);
2331 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2332 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2334 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2337 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2338 nespbl->pbl_size, pbl_depth);
2339 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2340 &nespbl->pbl_pbase);
2342 ib_umem_release(region);
2345 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2346 return ERR_PTR(-ENOMEM);
2349 nespbl->pbl_vbase = (u64 *)pbl;
2350 nespbl->user_base = start;
2351 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2352 " pbl_vbase=%p user_base=0x%lx\n",
2353 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2354 (void *) nespbl->pbl_vbase, nespbl->user_base);
2356 nespbl->page = sg_page(region->sg_head.sgl);
2357 for_each_sg_dma_page(region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2358 ((__le32 *)pbl)[0] = cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2359 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)(sg_page_iter_dma_address(&dma_iter)))>>32);
2360 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2361 (unsigned long long)*pbl,
2362 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2366 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2367 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2369 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2371 nesmr->ibmr.rkey = -1;
2372 nesmr->ibmr.lkey = -1;
2373 nesmr->mode = req.reg_type;
2374 return &nesmr->ibmr;
2377 ib_umem_release(region);
2378 return ERR_PTR(-ENOSYS);
2385 static int nes_dereg_mr(struct ib_mr *ib_mr)
2387 struct nes_mr *nesmr = to_nesmr(ib_mr);
2388 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2389 struct nes_device *nesdev = nesvnic->nesdev;
2390 struct nes_adapter *nesadapter = nesdev->nesadapter;
2391 struct nes_hw_cqp_wqe *cqp_wqe;
2392 struct nes_cqp_request *cqp_request;
2393 unsigned long flags;
2400 pci_free_consistent(nesdev->pcidev,
2401 nesmr->max_pages * sizeof(u64),
2405 if (nesmr->region) {
2406 ib_umem_release(nesmr->region);
2408 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2413 /* Deallocate the region with the adapter */
2415 cqp_request = nes_get_cqp_request(nesdev);
2416 if (cqp_request == NULL) {
2417 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2420 cqp_request->waiting = 1;
2421 cqp_wqe = &cqp_request->cqp_wqe;
2423 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2424 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2425 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2426 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2427 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2429 atomic_set(&cqp_request->refcount, 2);
2430 nes_post_cqp_request(nesdev, cqp_request);
2433 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2434 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2436 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2437 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2438 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2440 major_code = cqp_request->major_code;
2441 minor_code = cqp_request->minor_code;
2443 nes_put_cqp_request(nesdev, cqp_request);
2446 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2447 " ib_mr=%p, rkey = 0x%08X\n",
2448 ib_mr, ib_mr->rkey);
2450 } else if (major_code) {
2451 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2452 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2453 major_code, minor_code, ib_mr, ib_mr->rkey);
2457 if (nesmr->pbls_used != 0) {
2458 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2459 if (nesmr->pbl_4k) {
2460 nesadapter->free_4kpbl += nesmr->pbls_used;
2461 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2462 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2463 "exceeded the max(%u)\n",
2464 nesadapter->free_4kpbl,
2465 nesadapter->max_4kpbl);
2467 nesadapter->free_256pbl += nesmr->pbls_used;
2468 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2469 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2470 "exceeded the max(%u)\n",
2471 nesadapter->free_256pbl,
2472 nesadapter->max_256pbl);
2474 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2476 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2477 (ib_mr->rkey & 0x0fffff00) >> 8);
2488 static ssize_t hw_rev_show(struct device *dev,
2489 struct device_attribute *attr, char *buf)
2491 struct nes_ib_device *nesibdev =
2492 rdma_device_to_drv_device(dev, struct nes_ib_device, ibdev);
2493 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2495 nes_debug(NES_DBG_INIT, "\n");
2496 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2498 static DEVICE_ATTR_RO(hw_rev);
2503 static ssize_t hca_type_show(struct device *dev,
2504 struct device_attribute *attr, char *buf)
2506 nes_debug(NES_DBG_INIT, "\n");
2507 return sprintf(buf, "NES020\n");
2509 static DEVICE_ATTR_RO(hca_type);
2514 static ssize_t board_id_show(struct device *dev,
2515 struct device_attribute *attr, char *buf)
2517 nes_debug(NES_DBG_INIT, "\n");
2518 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2520 static DEVICE_ATTR_RO(board_id);
2522 static struct attribute *nes_dev_attributes[] = {
2523 &dev_attr_hw_rev.attr,
2524 &dev_attr_hca_type.attr,
2525 &dev_attr_board_id.attr,
2529 static const struct attribute_group nes_attr_group = {
2530 .attrs = nes_dev_attributes,
2536 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2537 int attr_mask, struct ib_qp_init_attr *init_attr)
2539 struct nes_qp *nesqp = to_nesqp(ibqp);
2541 nes_debug(NES_DBG_QP, "\n");
2543 attr->qp_access_flags = 0;
2544 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2545 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2546 attr->cap.max_recv_sge = 1;
2547 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2548 attr->cap.max_inline_data = 0;
2550 attr->cap.max_inline_data = 64;
2552 init_attr->event_handler = nesqp->ibqp.event_handler;
2553 init_attr->qp_context = nesqp->ibqp.qp_context;
2554 init_attr->send_cq = nesqp->ibqp.send_cq;
2555 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2556 init_attr->srq = nesqp->ibqp.srq;
2557 init_attr->cap = attr->cap;
2566 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2567 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2569 struct nes_hw_cqp_wqe *cqp_wqe;
2570 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2571 /* struct iw_cm_event cm_event; */
2572 struct nes_cqp_request *cqp_request;
2576 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2577 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2579 cqp_request = nes_get_cqp_request(nesdev);
2580 if (cqp_request == NULL) {
2581 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2584 if (wait_completion) {
2585 cqp_request->waiting = 1;
2587 cqp_request->waiting = 0;
2589 cqp_wqe = &cqp_request->cqp_wqe;
2591 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2592 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2593 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2594 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2595 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2596 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2597 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2599 /* If sending a terminate message, fill in the length (in words) */
2600 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2601 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2602 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2603 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2606 atomic_set(&cqp_request->refcount, 2);
2607 nes_post_cqp_request(nesdev, cqp_request);
2610 if (wait_completion) {
2611 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2612 nesqp->hwqp.qp_id); */
2613 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2615 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2616 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2617 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2618 major_code = cqp_request->major_code;
2620 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2621 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2622 nesqp->hwqp.qp_id, cqp_request->major_code,
2623 cqp_request->minor_code, next_iwarp_state);
2626 nes_put_cqp_request(nesdev, cqp_request);
2630 else if (major_code)
2643 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2644 int attr_mask, struct ib_udata *udata)
2646 struct nes_qp *nesqp = to_nesqp(ibqp);
2647 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2648 struct nes_device *nesdev = nesvnic->nesdev;
2651 u32 next_iwarp_state = 0;
2653 unsigned long qplockflags;
2655 u16 original_last_aeq;
2656 u8 issue_modify_qp = 0;
2659 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2660 " iwarp_state=0x%X, refcount=%d\n",
2661 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2662 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2664 spin_lock_irqsave(&nesqp->lock, qplockflags);
2666 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2667 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2668 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2669 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2671 if (attr_mask & IB_QP_STATE) {
2672 switch (attr->qp_state) {
2674 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2676 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2677 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2680 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2681 issue_modify_qp = 1;
2684 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2686 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2687 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2690 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2691 issue_modify_qp = 1;
2694 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2696 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2697 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2700 if (nesqp->cm_id == NULL) {
2701 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2702 nesqp->hwqp.qp_id );
2703 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2706 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2707 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2708 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2709 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2710 issue_modify_qp = 1;
2711 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2712 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2713 nesqp->hte_added = 1;
2716 issue_modify_qp = 1;
2717 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2718 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2719 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2720 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2723 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2724 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2725 " ignored due to current iWARP state\n",
2727 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2730 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2731 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2732 " already done based on hw state.\n",
2734 issue_modify_qp = 0;
2736 switch (nesqp->hw_iwarp_state) {
2737 case NES_AEQE_IWARP_STATE_CLOSING:
2738 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2740 case NES_AEQE_IWARP_STATE_TERMINATE:
2741 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2743 case NES_AEQE_IWARP_STATE_ERROR:
2744 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2747 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2748 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2754 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2756 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2757 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2760 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2761 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2762 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2763 issue_modify_qp = 1;
2767 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2768 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2771 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2773 if (nesqp->term_flags)
2774 del_timer(&nesqp->terminate_timer);
2776 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2777 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2778 if (nesqp->hte_added) {
2779 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2780 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2781 nesqp->hte_added = 0;
2783 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
2784 (nesdev->iw_status) &&
2785 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2786 next_iwarp_state |= NES_CQP_QP_RESET;
2788 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2789 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2792 issue_modify_qp = 1;
2793 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2796 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2801 nesqp->ibqp_state = attr->qp_state;
2802 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2803 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2804 nesqp->iwarp_state);
2807 if (attr_mask & IB_QP_ACCESS_FLAGS) {
2808 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2809 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2810 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2811 issue_modify_qp = 1;
2813 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2814 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2815 issue_modify_qp = 1;
2817 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2818 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2819 issue_modify_qp = 1;
2821 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2822 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2823 issue_modify_qp = 1;
2826 if (nesqp->user_mode) {
2827 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2828 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2829 issue_modify_qp = 1;
2833 original_last_aeq = nesqp->last_aeq;
2834 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2836 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2841 if (issue_modify_qp) {
2842 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
2843 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
2845 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2846 " failed for QP%u.\n",
2847 next_iwarp_state, nesqp->hwqp.qp_id);
2851 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2852 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2853 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2854 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2855 original_last_aeq, nesqp->last_aeq);
2856 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
2858 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2859 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2860 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2861 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2862 original_last_aeq, nesqp->last_aeq);
2863 /* this one is for the cm_disconnect thread */
2864 spin_lock_irqsave(&nesqp->lock, qplockflags);
2865 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2866 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2867 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2868 nes_cm_disconn(nesqp);
2870 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2871 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2874 spin_lock_irqsave(&nesqp->lock, qplockflags);
2876 /* These two are for the timer thread */
2877 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
2878 nesqp->cm_id->add_ref(nesqp->cm_id);
2879 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2880 " need ae to finish up, original_last_aeq = 0x%04X."
2881 " last_aeq = 0x%04X, scheduling timer.\n",
2882 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2883 original_last_aeq, nesqp->last_aeq);
2884 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
2886 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2888 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2889 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2890 " need ae to finish up, original_last_aeq = 0x%04X."
2891 " last_aeq = 0x%04X.\n",
2892 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2893 original_last_aeq, nesqp->last_aeq);
2897 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2898 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2899 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2900 original_last_aeq, nesqp->last_aeq);
2903 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2904 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2905 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2906 original_last_aeq, nesqp->last_aeq);
2911 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
2912 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2918 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, const struct ib_send_wr *ib_wr,
2922 int total_payload_length = 0;
2923 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
2924 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
2925 ib_wr->sg_list[sge_index].addr);
2926 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
2927 ib_wr->sg_list[sge_index].length);
2929 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
2930 (ib_wr->sg_list[sge_index].lkey));
2932 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
2934 total_payload_length += ib_wr->sg_list[sge_index].length;
2936 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
2937 total_payload_length);
2938 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
2939 total_payload_length);
2945 static int nes_post_send(struct ib_qp *ibqp, const struct ib_send_wr *ib_wr,
2946 const struct ib_send_wr **bad_wr)
2949 unsigned long flags = 0;
2950 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2951 struct nes_device *nesdev = nesvnic->nesdev;
2952 struct nes_qp *nesqp = to_nesqp(ibqp);
2953 struct nes_hw_qp_wqe *wqe;
2955 u32 qsize = nesqp->hwqp.sq_size;
2961 if (nesqp->ibqp_state > IB_QPS_RTS) {
2966 spin_lock_irqsave(&nesqp->lock, flags);
2968 head = nesqp->hwqp.sq_head;
2971 /* Check for QP error */
2972 if (nesqp->term_flags) {
2977 /* Check for SQ overflow */
2978 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
2983 wqe = &nesqp->hwqp.sq_vbase[head];
2984 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
2985 nesqp->hwqp.qp_id, wqe, head); */
2986 nes_fill_init_qp_wqe(wqe, nesqp, head);
2987 u64temp = (u64)(ib_wr->wr_id);
2988 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
2990 switch (ib_wr->opcode) {
2992 case IB_WR_SEND_WITH_INV:
2993 if (IB_WR_SEND == ib_wr->opcode) {
2994 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2995 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
2997 wqe_misc = NES_IWARP_SQ_OP_SEND;
2999 if (ib_wr->send_flags & IB_SEND_SOLICITED)
3000 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3002 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
3004 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3005 ib_wr->ex.invalidate_rkey);
3008 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3013 if (ib_wr->send_flags & IB_SEND_FENCE)
3014 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3016 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3017 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3018 (ib_wr->sg_list[0].length <= 64)) {
3019 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3020 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3021 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3022 ib_wr->sg_list[0].length);
3023 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3025 fill_wqe_sg_send(wqe, ib_wr, 1);
3029 case IB_WR_RDMA_WRITE:
3030 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3031 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3032 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3033 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3038 if (ib_wr->send_flags & IB_SEND_FENCE)
3039 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3041 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3042 rdma_wr(ib_wr)->rkey);
3043 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3044 rdma_wr(ib_wr)->remote_addr);
3046 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3047 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3048 (ib_wr->sg_list[0].length <= 64)) {
3049 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3050 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3051 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3052 ib_wr->sg_list[0].length);
3053 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3055 fill_wqe_sg_send(wqe, ib_wr, 1);
3058 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3059 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3061 case IB_WR_RDMA_READ:
3062 case IB_WR_RDMA_READ_WITH_INV:
3063 /* iWARP only supports 1 sge for RDMA reads */
3064 if (ib_wr->num_sge > 1) {
3065 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3070 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3071 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3073 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3074 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3075 ib_wr->ex.invalidate_rkey);
3078 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3079 rdma_wr(ib_wr)->remote_addr);
3080 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3081 rdma_wr(ib_wr)->rkey);
3082 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3083 ib_wr->sg_list->length);
3084 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3085 ib_wr->sg_list->addr);
3086 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3087 ib_wr->sg_list->lkey);
3089 case IB_WR_LOCAL_INV:
3090 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3091 set_wqe_32bit_value(wqe->wqe_words,
3092 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3093 ib_wr->ex.invalidate_rkey);
3097 struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3098 int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3099 int flags = reg_wr(ib_wr)->access;
3101 if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3102 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3106 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3107 set_wqe_64bit_value(wqe->wqe_words,
3108 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3110 set_wqe_32bit_value(wqe->wqe_words,
3111 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3112 lower_32_bits(mr->ibmr.length));
3113 set_wqe_32bit_value(wqe->wqe_words,
3114 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3115 set_wqe_32bit_value(wqe->wqe_words,
3116 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3117 reg_wr(ib_wr)->key);
3119 if (page_shift == 12) {
3120 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3121 } else if (page_shift == 21) {
3122 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3124 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3125 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3130 /* Set access_flags */
3131 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3132 if (flags & IB_ACCESS_LOCAL_WRITE)
3133 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3135 if (flags & IB_ACCESS_REMOTE_WRITE)
3136 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3138 if (flags & IB_ACCESS_REMOTE_READ)
3139 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3141 if (flags & IB_ACCESS_MW_BIND)
3142 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3144 /* Fill in PBL info: */
3145 set_wqe_64bit_value(wqe->wqe_words,
3146 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3149 set_wqe_32bit_value(wqe->wqe_words,
3150 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3153 nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3154 "length: %lld, rkey: %0x, pgl_paddr: %llx, "
3155 "page_list_len: %u, wqe_misc: %x\n",
3156 (unsigned long long) mr->ibmr.iova,
3159 (unsigned long long) mr->paddr,
3173 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3174 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3176 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3178 ib_wr = ib_wr->next;
3186 nesqp->hwqp.sq_head = head;
3189 counter = min(wqe_count, ((u32)255));
3190 wqe_count -= counter;
3191 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3192 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3195 spin_unlock_irqrestore(&nesqp->lock, flags);
3207 static int nes_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *ib_wr,
3208 const struct ib_recv_wr **bad_wr)
3211 unsigned long flags = 0;
3212 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3213 struct nes_device *nesdev = nesvnic->nesdev;
3214 struct nes_qp *nesqp = to_nesqp(ibqp);
3215 struct nes_hw_qp_wqe *wqe;
3218 u32 qsize = nesqp->hwqp.rq_size;
3222 u32 total_payload_length;
3224 if (nesqp->ibqp_state > IB_QPS_RTS) {
3229 spin_lock_irqsave(&nesqp->lock, flags);
3231 head = nesqp->hwqp.rq_head;
3234 /* Check for QP error */
3235 if (nesqp->term_flags) {
3240 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3244 /* Check for RQ overflow */
3245 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3250 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3251 wqe = &nesqp->hwqp.rq_vbase[head];
3253 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3254 nesqp->hwqp.qp_id, wqe, head); */
3255 nes_fill_init_qp_wqe(wqe, nesqp, head);
3256 u64temp = (u64)(ib_wr->wr_id);
3257 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3259 total_payload_length = 0;
3260 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3261 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3262 ib_wr->sg_list[sge_index].addr);
3263 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3264 ib_wr->sg_list[sge_index].length);
3265 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3266 ib_wr->sg_list[sge_index].lkey);
3268 total_payload_length += ib_wr->sg_list[sge_index].length;
3270 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3271 total_payload_length);
3273 ib_wr = ib_wr->next;
3280 nesqp->hwqp.rq_head = head;
3283 counter = min(wqe_count, ((u32)255));
3284 wqe_count -= counter;
3285 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3288 spin_unlock_irqrestore(&nesqp->lock, flags);
3297 * nes_drain_sq - drain sq
3298 * @ibqp: pointer to ibqp
3300 static void nes_drain_sq(struct ib_qp *ibqp)
3302 struct nes_qp *nesqp = to_nesqp(ibqp);
3304 if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3305 wait_for_completion(&nesqp->sq_drained);
3309 * nes_drain_rq - drain rq
3310 * @ibqp: pointer to ibqp
3312 static void nes_drain_rq(struct ib_qp *ibqp)
3314 struct nes_qp *nesqp = to_nesqp(ibqp);
3316 if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3317 wait_for_completion(&nesqp->rq_drained);
3323 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3327 unsigned long flags = 0;
3328 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3329 struct nes_device *nesdev = nesvnic->nesdev;
3330 struct nes_cq *nescq = to_nescq(ibcq);
3331 struct nes_qp *nesqp;
3332 struct nes_hw_cqe cqe;
3339 u32 move_cq_head = 1;
3342 nes_debug(NES_DBG_CQ, "\n");
3344 spin_lock_irqsave(&nescq->lock, flags);
3346 head = nescq->hw_cq.cq_head;
3347 cq_size = nescq->hw_cq.cq_size;
3349 while (cqe_count < num_entries) {
3350 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3351 NES_CQE_VALID) == 0)
3355 * Make sure we read CQ entry contents *after*
3356 * we've checked the valid bit.
3360 cqe = nescq->hw_cq.cq_vbase[head];
3361 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3362 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3363 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3364 /* parse CQE, get completion context from WQE (either rq or sq) */
3365 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3369 nesqp = (struct nes_qp *)(unsigned long)u64temp;
3370 memset(entry, 0, sizeof *entry);
3371 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3372 entry->status = IB_WC_SUCCESS;
3374 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3375 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3376 entry->status = err_code & 0x0000ffff;
3378 /* The rest of the cqe's will be marked as flushed */
3379 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3380 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3381 NES_IWARP_CQE_MINOR_FLUSH);
3383 entry->status = IB_WC_WR_FLUSH_ERR;
3386 entry->qp = &nesqp->ibqp;
3387 entry->src_qp = nesqp->hwqp.qp_id;
3389 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3390 if (nesqp->skip_lsmm) {
3391 nesqp->skip_lsmm = 0;
3392 nesqp->hwqp.sq_tail++;
3395 /* Working on a SQ Completion*/
3396 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3397 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3398 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3399 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3400 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3401 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3403 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3404 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3405 case NES_IWARP_SQ_OP_RDMAW:
3406 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3407 entry->opcode = IB_WC_RDMA_WRITE;
3409 case NES_IWARP_SQ_OP_RDMAR:
3410 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3411 entry->opcode = IB_WC_RDMA_READ;
3412 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3413 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3415 case NES_IWARP_SQ_OP_SENDINV:
3416 case NES_IWARP_SQ_OP_SENDSEINV:
3417 case NES_IWARP_SQ_OP_SEND:
3418 case NES_IWARP_SQ_OP_SENDSE:
3419 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3420 entry->opcode = IB_WC_SEND;
3422 case NES_IWARP_SQ_OP_LOCINV:
3423 entry->opcode = IB_WC_LOCAL_INV;
3425 case NES_IWARP_SQ_OP_FAST_REG:
3426 entry->opcode = IB_WC_REG_MR;
3430 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3431 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3433 wq_tail = nesqp->hwqp.sq_tail;
3436 /* Working on a RQ Completion*/
3437 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3438 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3439 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3440 entry->opcode = IB_WC_RECV;
3442 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3443 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3445 wq_tail = nesqp->hwqp.rq_tail;
3449 if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3450 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3451 complete(&nesqp->sq_drained);
3452 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3453 complete(&nesqp->rq_drained);
3456 entry->wr_id = wrid;
3462 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3463 if (++head >= cq_size)
3465 nescq->polled_completions++;
3467 if ((nescq->polled_completions > (cq_size / 2)) ||
3468 (nescq->polled_completions == 255)) {
3469 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3470 " are pending %u of %u.\n",
3471 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3472 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3473 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3474 nescq->polled_completions = 0;
3477 /* Update the wqe index and set status to flush */
3478 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3479 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3480 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3481 cpu_to_le32(wqe_index);
3482 move_cq_head = 1; /* ready for next pass */
3486 if (nescq->polled_completions) {
3487 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3488 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3489 nescq->polled_completions = 0;
3492 nescq->hw_cq.cq_head = head;
3493 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3494 cqe_count, nescq->hw_cq.cq_number);
3496 spin_unlock_irqrestore(&nescq->lock, flags);
3505 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3507 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3508 struct nes_device *nesdev = nesvnic->nesdev;
3509 struct nes_cq *nescq = to_nescq(ibcq);
3512 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3513 nescq->hw_cq.cq_number);
3515 cq_arm = nescq->hw_cq.cq_number;
3516 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3517 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3518 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3519 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3523 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3524 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3529 static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3530 struct ib_port_immutable *immutable)
3532 struct ib_port_attr attr;
3535 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3537 err = nes_query_port(ibdev, port_num, &attr);
3541 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3542 immutable->gid_tbl_len = attr.gid_tbl_len;
3547 static void get_dev_fw_str(struct ib_device *dev, char *str)
3549 struct nes_ib_device *nesibdev =
3550 container_of(dev, struct nes_ib_device, ibdev);
3551 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3553 nes_debug(NES_DBG_INIT, "\n");
3554 snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u",
3555 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
3556 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
3559 static const struct ib_device_ops nes_dev_ops = {
3560 .alloc_mr = nes_alloc_mr,
3561 .alloc_mw = nes_alloc_mw,
3562 .alloc_pd = nes_alloc_pd,
3563 .alloc_ucontext = nes_alloc_ucontext,
3564 .create_cq = nes_create_cq,
3565 .create_qp = nes_create_qp,
3566 .dealloc_mw = nes_dealloc_mw,
3567 .dealloc_pd = nes_dealloc_pd,
3568 .dealloc_ucontext = nes_dealloc_ucontext,
3569 .dereg_mr = nes_dereg_mr,
3570 .destroy_cq = nes_destroy_cq,
3571 .destroy_qp = nes_destroy_qp,
3572 .drain_rq = nes_drain_rq,
3573 .drain_sq = nes_drain_sq,
3574 .get_dev_fw_str = get_dev_fw_str,
3575 .get_dma_mr = nes_get_dma_mr,
3576 .get_port_immutable = nes_port_immutable,
3577 .map_mr_sg = nes_map_mr_sg,
3579 .modify_qp = nes_modify_qp,
3580 .poll_cq = nes_poll_cq,
3581 .post_recv = nes_post_recv,
3582 .post_send = nes_post_send,
3583 .query_device = nes_query_device,
3584 .query_gid = nes_query_gid,
3585 .query_pkey = nes_query_pkey,
3586 .query_port = nes_query_port,
3587 .query_qp = nes_query_qp,
3588 .reg_user_mr = nes_reg_user_mr,
3589 .req_notify_cq = nes_req_notify_cq,
3590 INIT_RDMA_OBJ_SIZE(ib_pd, nes_pd, ibpd),
3591 INIT_RDMA_OBJ_SIZE(ib_ucontext, nes_ucontext, ibucontext),
3595 * nes_init_ofa_device
3597 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3599 struct nes_ib_device *nesibdev;
3600 struct nes_vnic *nesvnic = netdev_priv(netdev);
3601 struct nes_device *nesdev = nesvnic->nesdev;
3603 nesibdev = ib_alloc_device(nes_ib_device, ibdev);
3604 if (nesibdev == NULL) {
3607 nesibdev->ibdev.owner = THIS_MODULE;
3609 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3610 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3611 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3613 nesibdev->ibdev.uverbs_cmd_mask =
3614 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3615 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3616 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3617 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3618 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3619 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3620 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3621 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3622 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3623 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3624 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3625 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3626 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3627 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3628 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3629 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3630 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3631 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3632 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3633 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3634 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3635 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3637 nesibdev->ibdev.phys_port_cnt = 1;
3638 nesibdev->ibdev.num_comp_vectors = 1;
3639 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3641 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3642 if (nesibdev->ibdev.iwcm == NULL) {
3643 ib_dealloc_device(&nesibdev->ibdev);
3646 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3647 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3648 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3649 nesibdev->ibdev.iwcm->connect = nes_connect;
3650 nesibdev->ibdev.iwcm->accept = nes_accept;
3651 nesibdev->ibdev.iwcm->reject = nes_reject;
3652 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3653 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3655 ib_set_device_ops(&nesibdev->ibdev, &nes_dev_ops);
3656 memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3657 sizeof(nesibdev->ibdev.iwcm->ifname));
3664 * nes_handle_delayed_event
3666 static void nes_handle_delayed_event(struct timer_list *t)
3668 struct nes_vnic *nesvnic = from_timer(nesvnic, t, event_timer);
3670 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3671 struct ib_event event;
3673 event.device = &nesvnic->nesibdev->ibdev;
3676 event.event = nesvnic->delayed_event;
3677 event.element.port_num = nesvnic->logical_port + 1;
3678 ib_dispatch_event(&event);
3682 nesvnic->event_timer.function = NULL;
3686 void nes_port_ibevent(struct nes_vnic *nesvnic)
3688 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3689 struct nes_device *nesdev = nesvnic->nesdev;
3690 struct ib_event event;
3691 event.device = &nesibdev->ibdev;
3692 event.element.port_num = nesvnic->logical_port + 1;
3693 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3695 if (!nesvnic->event_timer.function) {
3696 ib_dispatch_event(&event);
3697 nesvnic->last_dispatched_event = event.event;
3698 nesvnic->event_timer.function = nes_handle_delayed_event;
3699 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3700 add_timer(&nesvnic->event_timer);
3702 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3704 nesvnic->delayed_event = event.event;
3709 * nes_destroy_ofa_device
3711 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3713 if (nesibdev == NULL)
3716 nes_unregister_ofa_device(nesibdev);
3718 kfree(nesibdev->ibdev.iwcm);
3719 ib_dealloc_device(&nesibdev->ibdev);
3724 * nes_register_ofa_device
3726 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3728 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3729 struct nes_device *nesdev = nesvnic->nesdev;
3730 struct nes_adapter *nesadapter = nesdev->nesadapter;
3733 rdma_set_device_sysfs_group(&nesvnic->nesibdev->ibdev, &nes_attr_group);
3734 nesvnic->nesibdev->ibdev.driver_id = RDMA_DRIVER_NES;
3735 ret = ib_register_device(&nesvnic->nesibdev->ibdev, "nes%d");
3740 /* Get the resources allocated to this device */
3741 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3742 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3743 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3744 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3746 nesvnic->of_device_registered = 1;
3753 * nes_unregister_ofa_device
3755 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3757 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3759 if (nesvnic->of_device_registered)
3760 ib_unregister_device(&nesibdev->ibdev);
3762 nesvnic->of_device_registered = 0;