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 err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1086 nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1088 nes_debug(NES_DBG_QP,
1089 "error geting qp mem code = %d\n", err);
1090 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1091 kfree(nesqp->allocated_buffer);
1092 return ERR_PTR(-ENOMEM);
1095 nesqp->hwqp.sq_size = sq_size;
1096 nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1097 nesqp->hwqp.sq_head = 1;
1098 nesqp->hwqp.rq_size = rq_size;
1099 nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1100 /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1101 (void *)nesqp->nesqp_context_pbase);
1103 nesqp->hwqp.qp_id = qp_num;
1104 nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1105 nesqp->nespd = nespd;
1107 nescq = to_nescq(init_attr->send_cq);
1108 nesqp->nesscq = nescq;
1109 nescq = to_nescq(init_attr->recv_cq);
1110 nesqp->nesrcq = nescq;
1112 nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1113 NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1114 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1115 NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1116 nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1117 NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1119 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1120 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1122 nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1123 ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1124 u64temp = (u64)nesqp->hwqp.sq_pbase;
1125 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1126 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1130 u64temp = (u64)nesqp->hwqp.sq_pbase;
1131 nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1132 nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1133 u64temp = (u64)nesqp->hwqp.rq_pbase;
1134 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1135 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1137 u64temp = (u64)nesqp->pbl_pbase;
1138 nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1139 nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1142 /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1143 nesvnic->next_qp_nic_index,
1144 nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1145 spin_lock_irqsave(&nesdev->cqp.lock, flags);
1146 nesqp->nesqp_context->misc2 |= cpu_to_le32(
1147 (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1148 NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1149 nesvnic->next_qp_nic_index++;
1150 if ((nesvnic->next_qp_nic_index > 3) ||
1151 (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1152 nesvnic->next_qp_nic_index = 0;
1154 spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1156 nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1157 u64temp = (u64)nesqp->hwqp.q2_pbase;
1158 nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1159 nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1160 nesqp->nesqp_context->aeq_token_low = cpu_to_le32((u32)((unsigned long)(nesqp)));
1161 nesqp->nesqp_context->aeq_token_high = cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1162 nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1163 NES_QPCONTEXT_ORDIRD_AAH |
1164 ((((u32)nesadapter->max_irrq_wr) <<
1165 NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1166 if (disable_mpa_crc) {
1167 nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1168 nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1173 cqp_request = nes_get_cqp_request(nesdev);
1174 if (cqp_request == NULL) {
1175 nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1176 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1177 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1178 kfree(nesqp->allocated_buffer);
1179 return ERR_PTR(-ENOMEM);
1181 cqp_request->waiting = 1;
1182 cqp_wqe = &cqp_request->cqp_wqe;
1185 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1186 NES_CQP_QP_IWARP_STATE_IDLE;
1188 opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1189 NES_CQP_QP_IWARP_STATE_IDLE;
1191 opcode |= NES_CQP_QP_CQS_VALID;
1192 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1193 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1194 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1196 u64temp = (u64)nesqp->nesqp_context_pbase;
1197 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1199 atomic_set(&cqp_request->refcount, 2);
1200 nes_post_cqp_request(nesdev, cqp_request);
1203 nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1205 ret = wait_event_timeout(cqp_request->waitq,
1206 (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1207 nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1208 " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1209 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1210 nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1211 cqp_request->major_code, cqp_request->minor_code);
1212 if ((!ret) || (cqp_request->major_code)) {
1213 nes_put_cqp_request(nesdev, cqp_request);
1214 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1215 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1216 kfree(nesqp->allocated_buffer);
1218 return ERR_PTR(-ETIME);
1220 return ERR_PTR(-EIO);
1224 nes_put_cqp_request(nesdev, cqp_request);
1227 uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1228 uresp.mmap_rq_db_index = 0;
1229 uresp.actual_sq_size = sq_size;
1230 uresp.actual_rq_size = rq_size;
1231 uresp.qp_id = nesqp->hwqp.qp_id;
1232 uresp.nes_drv_opt = nes_drv_opt;
1233 if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1234 nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1235 nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1236 kfree(nesqp->allocated_buffer);
1237 return ERR_PTR(-EFAULT);
1241 nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1242 nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1243 spin_lock_init(&nesqp->lock);
1244 nes_add_ref(&nesqp->ibqp);
1247 nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1248 return ERR_PTR(-EINVAL);
1250 init_completion(&nesqp->sq_drained);
1251 init_completion(&nesqp->rq_drained);
1253 nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1254 timer_setup(&nesqp->terminate_timer, nes_terminate_timeout, 0);
1256 /* update the QP table */
1257 nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1258 nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1259 netdev_refcnt_read(nesvnic->netdev));
1261 return &nesqp->ibqp;
1267 static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1273 unsigned long flags = 0;
1275 spin_lock_irqsave(&nescq->lock, flags);
1277 cq_head = nescq->hw_cq.cq_head;
1278 while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1280 lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1281 hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1282 u64temp = (((u64)hi) << 32) | ((u64)lo);
1283 u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1284 if (u64temp == (u64)(unsigned long)nesqp) {
1285 /* Zero the context value so cqe will be ignored */
1286 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1287 nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1290 if (++cq_head >= nescq->hw_cq.cq_size)
1294 spin_unlock_irqrestore(&nescq->lock, flags);
1301 static int nes_destroy_qp(struct ib_qp *ibqp)
1303 struct nes_qp *nesqp = to_nesqp(ibqp);
1304 struct nes_ucontext *nes_ucontext;
1305 struct ib_qp_attr attr;
1306 struct iw_cm_id *cm_id;
1307 struct iw_cm_event cm_event;
1310 atomic_inc(&sw_qps_destroyed);
1311 nesqp->destroyed = 1;
1313 /* Blow away the connection if it exists. */
1314 if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1315 /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1316 attr.qp_state = IB_QPS_ERR;
1317 nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1320 if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1321 (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1322 cm_id = nesqp->cm_id;
1323 cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1324 cm_event.status = -ETIMEDOUT;
1325 cm_event.local_addr = cm_id->local_addr;
1326 cm_event.remote_addr = cm_id->remote_addr;
1327 cm_event.private_data = NULL;
1328 cm_event.private_data_len = 0;
1330 nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1331 "QP%u. cm_id = %p, refcount = %u. \n",
1332 nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1334 cm_id->rem_ref(cm_id);
1335 ret = cm_id->event_handler(cm_id, &cm_event);
1337 nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1340 if (nesqp->user_mode) {
1341 if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1342 nes_ucontext = to_nesucontext(ibqp->uobject->context);
1343 clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1344 nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1345 if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1346 nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1349 if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1350 nesqp->sq_kmapped = 0;
1351 kunmap(nesqp->page);
1354 /* Clean any pending completions from the cq(s) */
1356 nes_clean_cq(nesqp, nesqp->nesscq);
1358 if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1359 nes_clean_cq(nesqp, nesqp->nesrcq);
1361 nes_rem_ref(&nesqp->ibqp);
1369 static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1370 const struct ib_cq_init_attr *attr,
1371 struct ib_ucontext *context,
1372 struct ib_udata *udata)
1374 int entries = attr->cqe;
1376 struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1377 struct nes_device *nesdev = nesvnic->nesdev;
1378 struct nes_adapter *nesadapter = nesdev->nesadapter;
1379 struct nes_cq *nescq;
1380 struct nes_ucontext *nes_ucontext = NULL;
1381 struct nes_cqp_request *cqp_request;
1383 struct nes_hw_cqp_wqe *cqp_wqe;
1384 struct nes_pbl *nespbl = NULL;
1385 struct nes_create_cq_req req;
1386 struct nes_create_cq_resp resp;
1389 u32 pbl_entries = 1;
1391 unsigned long flags;
1395 return ERR_PTR(-EINVAL);
1397 if (entries > nesadapter->max_cqe)
1398 return ERR_PTR(-EINVAL);
1400 err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1401 nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
1403 return ERR_PTR(err);
1406 nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1408 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1409 return ERR_PTR(-ENOMEM);
1412 nescq->hw_cq.cq_size = max(entries + 1, 5);
1413 nescq->hw_cq.cq_number = cq_num;
1414 nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1418 nes_ucontext = to_nesucontext(context);
1419 if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1420 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1422 return ERR_PTR(-EFAULT);
1424 nesvnic->mcrq_ucontext = nes_ucontext;
1425 nes_ucontext->mcrqf = req.mcrqf;
1426 if (nes_ucontext->mcrqf) {
1427 if (nes_ucontext->mcrqf & 0x80000000)
1428 nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1429 else if (nes_ucontext->mcrqf & 0x40000000)
1430 nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1432 nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1433 nescq->mcrqf = nes_ucontext->mcrqf;
1434 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1436 nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1437 (unsigned long)req.user_cq_buffer, entries);
1439 list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1440 if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1441 list_del(&nespbl->list);
1443 nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1449 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1451 return ERR_PTR(-EFAULT);
1454 pbl_entries = nespbl->pbl_size >> 3;
1455 nescq->cq_mem_size = 0;
1457 nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1458 nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1459 entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1461 /* allocate the physical buffer space */
1462 mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1463 &nescq->hw_cq.cq_pbase);
1465 printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1466 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1468 return ERR_PTR(-ENOMEM);
1471 nescq->hw_cq.cq_vbase = mem;
1472 nescq->hw_cq.cq_head = 0;
1473 nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1474 nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1475 (u32)nescq->hw_cq.cq_pbase);
1478 nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1479 spin_lock_init(&nescq->lock);
1481 /* send CreateCQ request to CQP */
1482 cqp_request = nes_get_cqp_request(nesdev);
1483 if (cqp_request == NULL) {
1484 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1486 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1487 nescq->hw_cq.cq_pbase);
1489 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1490 nespbl->pbl_vbase, nespbl->pbl_pbase);
1494 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1496 return ERR_PTR(-ENOMEM);
1498 cqp_request->waiting = 1;
1499 cqp_wqe = &cqp_request->cqp_wqe;
1501 opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1502 NES_CQP_CQ_CHK_OVERFLOW |
1503 NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1505 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1507 if (pbl_entries != 1) {
1508 if (pbl_entries > 32) {
1510 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1511 if (nesadapter->free_4kpbl == 0) {
1512 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1513 nes_free_cqp_request(nesdev, cqp_request);
1515 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1516 nescq->hw_cq.cq_pbase);
1518 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1519 nespbl->pbl_vbase, nespbl->pbl_pbase);
1522 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1524 return ERR_PTR(-ENOMEM);
1526 opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1527 nescq->virtual_cq = 2;
1528 nesadapter->free_4kpbl--;
1531 /* use 256 byte pbl */
1532 nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1533 if (nesadapter->free_256pbl == 0) {
1534 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1535 nes_free_cqp_request(nesdev, cqp_request);
1537 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1538 nescq->hw_cq.cq_pbase);
1540 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1541 nespbl->pbl_vbase, nespbl->pbl_pbase);
1544 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1546 return ERR_PTR(-ENOMEM);
1548 opcode |= NES_CQP_CQ_VIRT;
1549 nescq->virtual_cq = 1;
1550 nesadapter->free_256pbl--;
1555 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1557 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1558 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1559 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1560 (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1563 if (pbl_entries != 1)
1564 u64temp = (u64)nespbl->pbl_pbase;
1566 u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1567 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1568 nes_ucontext->mmap_db_index[0]);
1570 u64temp = (u64)nescq->hw_cq.cq_pbase;
1571 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1573 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1574 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1575 u64temp = (u64)(unsigned long)&nescq->hw_cq;
1576 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1577 cpu_to_le32((u32)(u64temp >> 1));
1578 cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1579 cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1581 atomic_set(&cqp_request->refcount, 2);
1582 nes_post_cqp_request(nesdev, cqp_request);
1585 nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1586 nescq->hw_cq.cq_number);
1587 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1588 NES_EVENT_TIMEOUT * 2);
1589 nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1590 nescq->hw_cq.cq_number, ret);
1591 if ((!ret) || (cqp_request->major_code)) {
1592 nes_put_cqp_request(nesdev, cqp_request);
1594 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1595 nescq->hw_cq.cq_pbase);
1597 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1598 nespbl->pbl_vbase, nespbl->pbl_pbase);
1601 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1603 return ERR_PTR(-EIO);
1605 nes_put_cqp_request(nesdev, cqp_request);
1608 /* free the nespbl */
1609 pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1612 resp.cq_id = nescq->hw_cq.cq_number;
1613 resp.cq_size = nescq->hw_cq.cq_size;
1614 resp.mmap_db_index = 0;
1615 if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
1616 nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1618 return ERR_PTR(-EFAULT);
1622 return &nescq->ibcq;
1629 static int nes_destroy_cq(struct ib_cq *ib_cq)
1631 struct nes_cq *nescq;
1632 struct nes_device *nesdev;
1633 struct nes_vnic *nesvnic;
1634 struct nes_adapter *nesadapter;
1635 struct nes_hw_cqp_wqe *cqp_wqe;
1636 struct nes_cqp_request *cqp_request;
1637 unsigned long flags;
1644 nescq = to_nescq(ib_cq);
1645 nesvnic = to_nesvnic(ib_cq->device);
1646 nesdev = nesvnic->nesdev;
1647 nesadapter = nesdev->nesadapter;
1649 nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1651 /* Send DestroyCQ request to CQP */
1652 cqp_request = nes_get_cqp_request(nesdev);
1653 if (cqp_request == NULL) {
1654 nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1657 cqp_request->waiting = 1;
1658 cqp_wqe = &cqp_request->cqp_wqe;
1659 opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1660 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1661 if (nescq->virtual_cq == 1) {
1662 nesadapter->free_256pbl++;
1663 if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1664 printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1665 __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1667 } else if (nescq->virtual_cq == 2) {
1668 nesadapter->free_4kpbl++;
1669 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1670 printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1671 __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1673 opcode |= NES_CQP_CQ_4KB_CHUNK;
1676 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1678 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1679 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1680 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1681 (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1683 nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1685 atomic_set(&cqp_request->refcount, 2);
1686 nes_post_cqp_request(nesdev, cqp_request);
1689 nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1690 nescq->hw_cq.cq_number);
1691 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1693 nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1694 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1695 nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1696 cqp_request->minor_code);
1698 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1699 nescq->hw_cq.cq_number);
1701 } else if (cqp_request->major_code) {
1702 nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1703 nescq->hw_cq.cq_number);
1708 nes_put_cqp_request(nesdev, cqp_request);
1710 if (nescq->cq_mem_size)
1711 pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1712 nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1721 static u32 root_256(struct nes_device *nesdev,
1722 struct nes_root_vpbl *root_vpbl,
1723 struct nes_root_vpbl *new_root,
1729 if (pbl_count_4k == 1) {
1730 new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1731 512, &new_root->pbl_pbase);
1733 if (new_root->pbl_vbase == NULL)
1736 leaf_pbl = (u64)root_vpbl->pbl_pbase;
1737 for (i = 0; i < 16; i++) {
1738 new_root->pbl_vbase[i].pa_low =
1739 cpu_to_le32((u32)leaf_pbl);
1740 new_root->pbl_vbase[i].pa_high =
1741 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1745 for (i = 3; i >= 0; i--) {
1747 root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1748 leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1749 (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1751 for (k = 1; k < 16; k++) {
1753 root_vpbl->pbl_vbase[j + k].pa_low =
1754 cpu_to_le32((u32)leaf_pbl);
1755 root_vpbl->pbl_vbase[j + k].pa_high =
1756 cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1768 static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1769 u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1770 dma_addr_t single_buffer, u16 pbl_count_4k,
1771 u16 residual_page_count_4k, int acc, u64 *iova_start,
1772 u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1774 struct nes_hw_cqp_wqe *cqp_wqe;
1775 struct nes_cqp_request *cqp_request;
1776 unsigned long flags;
1778 struct nes_adapter *nesadapter = nesdev->nesadapter;
1780 u16 pbl_count_256 = 0;
1782 u8 use_256_pbls = 0;
1784 u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1785 struct nes_root_vpbl new_root = { 0, NULL, NULL };
1789 /* Register the region with the adapter */
1790 cqp_request = nes_get_cqp_request(nesdev);
1791 if (cqp_request == NULL) {
1792 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1795 cqp_request->waiting = 1;
1796 cqp_wqe = &cqp_request->cqp_wqe;
1799 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1801 pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1802 pbl_count_256 = (pg_cnt + 31) / 32;
1804 if (pbl_count_256 <= nesadapter->free_256pbl)
1806 else if (pbl_count_4k <= nesadapter->free_4kpbl)
1808 } else if (pg_cnt <= 2048) {
1809 if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1810 (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1812 } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1815 } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1819 if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1824 pbl_count = pbl_count_256;
1825 nesadapter->free_256pbl -= pbl_count + use_two_level;
1826 } else if (use_4k_pbls) {
1827 pbl_count = pbl_count_4k;
1828 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1830 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1831 nes_debug(NES_DBG_MR, "Out of Pbls\n");
1832 nes_free_cqp_request(nesdev, cqp_request);
1836 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1839 if (use_256_pbls && use_two_level) {
1840 if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1841 if (new_root.pbl_pbase != 0)
1842 root_vpbl = &new_root;
1844 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1845 nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1848 if (pbl_count_4k == 1)
1850 pbl_count = pbl_count_4k;
1852 if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1853 nesadapter->free_4kpbl -= pbl_count + use_two_level;
1856 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1858 if (use_4k_pbls == 0)
1863 opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1864 NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1865 if (acc & IB_ACCESS_LOCAL_WRITE)
1866 opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1867 if (acc & IB_ACCESS_REMOTE_WRITE)
1868 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1869 if (acc & IB_ACCESS_REMOTE_READ)
1870 opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1871 if (acc & IB_ACCESS_MW_BIND)
1872 opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1874 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1875 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1876 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1877 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1879 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1880 cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1881 cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1882 cpu_to_le32(nespd->pd_id & 0x00007fff);
1883 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1885 if (pbl_count == 0) {
1886 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1888 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1889 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
1890 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
1893 cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1897 atomic_set(&cqp_request->refcount, 2);
1898 nes_post_cqp_request(nesdev, cqp_request);
1901 ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1903 nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1904 " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1905 stag, ret, cqp_request->major_code, cqp_request->minor_code);
1906 major_code = cqp_request->major_code;
1907 nes_put_cqp_request(nesdev, cqp_request);
1909 if ((!ret || major_code) && pbl_count != 0) {
1910 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1912 nesadapter->free_256pbl += pbl_count + use_two_level;
1913 else if (use_4k_pbls)
1914 nesadapter->free_4kpbl += pbl_count + use_two_level;
1915 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1917 if (new_root.pbl_pbase)
1918 pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1919 new_root.pbl_pbase);
1923 else if (major_code)
1926 *actual_pbl_cnt = pbl_count + use_two_level;
1927 *used_4k_pbls = use_4k_pbls;
1935 struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
1936 int acc, u64 *iova_start)
1939 struct nes_pd *nespd = to_nespd(ib_pd);
1940 struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
1941 struct nes_device *nesdev = nesvnic->nesdev;
1942 struct nes_adapter *nesadapter = nesdev->nesadapter;
1943 struct nes_mr *nesmr;
1945 struct nes_vpbl vpbl;
1946 struct nes_root_vpbl root_vpbl;
1950 u32 next_stag_index = 0;
1959 vpbl.pbl_vbase = NULL;
1960 root_vpbl.pbl_vbase = NULL;
1961 root_vpbl.pbl_pbase = 0;
1963 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
1964 stag_key = (u8)next_stag_index;
1968 next_stag_index >>= 8;
1969 next_stag_index %= nesadapter->max_mr;
1971 if ((addr ^ *iova_start) & ~PAGE_MASK)
1972 return ERR_PTR(-EINVAL);
1974 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
1975 &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
1977 return ERR_PTR(err);
1980 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
1982 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1983 return ERR_PTR(-ENOMEM);
1986 /* Allocate a 4K buffer for the PBL */
1987 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
1989 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
1990 vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
1991 if (!vpbl.pbl_vbase) {
1992 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
1993 ibmr = ERR_PTR(-ENOMEM);
2001 if (mask & ~PAGE_MASK) {
2002 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2003 nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2004 ibmr = ERR_PTR(-EINVAL);
2009 region_length += size;
2010 vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2011 vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2013 stag = stag_index << 8;
2015 stag += (u32)stag_key;
2017 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2018 " length = 0x%016lX, index = 0x%08X\n",
2019 stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2021 /* Make the leaf PBL the root if only one PBL */
2022 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2029 ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2030 addr, pbl_count, 1, acc, iova_start,
2031 &nesmr->pbls_used, &nesmr->pbl_4k);
2034 nesmr->ibmr.rkey = stag;
2035 nesmr->ibmr.lkey = stag;
2036 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2037 ibmr = &nesmr->ibmr;
2040 ibmr = ERR_PTR(-ENOMEM);
2044 /* single PBL case */
2045 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2053 static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2057 nes_debug(NES_DBG_MR, "\n");
2059 return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
2065 static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2066 u64 virt, int acc, struct ib_udata *udata)
2071 dma_addr_t last_dma_addr = 0;
2072 dma_addr_t first_dma_addr = 0;
2073 struct nes_pd *nespd = to_nespd(pd);
2074 struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2075 struct nes_device *nesdev = nesvnic->nesdev;
2076 struct nes_adapter *nesadapter = nesdev->nesadapter;
2077 struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2078 struct sg_dma_page_iter dma_iter;
2079 struct nes_ucontext *nes_ucontext = rdma_udata_to_drv_context(
2080 udata, struct nes_ucontext, ibucontext);
2081 struct nes_pbl *nespbl;
2082 struct nes_mr *nesmr;
2083 struct ib_umem *region;
2084 struct nes_mem_reg_req req;
2085 struct nes_vpbl vpbl;
2086 struct nes_root_vpbl root_vpbl;
2089 int err, pbl_depth = 0;
2093 u32 next_stag_index;
2095 u32 root_pbl_index = 0;
2096 u32 cur_pbl_index = 0;
2102 region = ib_umem_get(udata, start, length, acc, 0);
2103 if (IS_ERR(region)) {
2104 return (struct ib_mr *)region;
2107 nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2108 " offset = %u, page size = %lu.\n",
2109 (unsigned long int)start, (unsigned long int)virt, (u32)length,
2110 ib_umem_offset(region), BIT(region->page_shift));
2112 skip_pages = ((u32)ib_umem_offset(region)) >> 12;
2114 if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2115 ib_umem_release(region);
2116 return ERR_PTR(-EFAULT);
2118 nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2120 switch (req.reg_type) {
2121 case IWNES_MEMREG_TYPE_MEM:
2124 vpbl.pbl_vbase = NULL;
2125 root_vpbl.pbl_vbase = NULL;
2126 root_vpbl.pbl_pbase = 0;
2128 get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2129 stag_key = (u8)next_stag_index;
2131 driver_key = next_stag_index & 0x70000000;
2133 next_stag_index >>= 8;
2134 next_stag_index %= nesadapter->max_mr;
2136 err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2137 nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
2139 ib_umem_release(region);
2140 return ERR_PTR(err);
2143 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2145 ib_umem_release(region);
2146 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2147 return ERR_PTR(-ENOMEM);
2149 nesmr->region = region;
2151 for_each_sg_dma_page (region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2153 region_length += PAGE_SIZE;
2154 region_length -= skip_pages << 12;
2156 if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
2158 if ((page_count & 0x01FF) == 0) {
2159 if (page_count >= 1024 * 512) {
2160 ib_umem_release(region);
2161 nes_free_resource(nesadapter,
2162 nesadapter->allocated_mrs, stag_index);
2164 ibmr = ERR_PTR(-E2BIG);
2165 goto reg_user_mr_err;
2167 if (root_pbl_index == 1) {
2168 root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2169 8192, &root_vpbl.pbl_pbase);
2170 nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2171 root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2172 if (!root_vpbl.pbl_vbase) {
2173 ib_umem_release(region);
2174 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2176 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2179 ibmr = ERR_PTR(-ENOMEM);
2180 goto reg_user_mr_err;
2182 root_vpbl.leaf_vpbl = kcalloc(1024,
2183 sizeof(*root_vpbl.leaf_vpbl),
2185 if (!root_vpbl.leaf_vpbl) {
2186 ib_umem_release(region);
2187 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2188 root_vpbl.pbl_pbase);
2189 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2191 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2194 ibmr = ERR_PTR(-ENOMEM);
2195 goto reg_user_mr_err;
2197 root_vpbl.pbl_vbase[0].pa_low =
2198 cpu_to_le32((u32)vpbl.pbl_pbase);
2199 root_vpbl.pbl_vbase[0].pa_high =
2200 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2201 root_vpbl.leaf_vpbl[0] = vpbl;
2203 vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2205 nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2206 vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2207 if (!vpbl.pbl_vbase) {
2208 ib_umem_release(region);
2209 nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2210 ibmr = ERR_PTR(-ENOMEM);
2212 goto reg_user_mr_err;
2214 if (1 <= root_pbl_index) {
2215 root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2216 cpu_to_le32((u32)vpbl.pbl_pbase);
2217 root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2218 cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2219 root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2225 if (page_count != 0) {
2226 if ((last_dma_addr + 4096) != sg_page_iter_dma_address(&dma_iter))
2228 last_dma_addr = sg_page_iter_dma_address(&dma_iter);
2230 first_dma_addr = sg_page_iter_dma_address(&dma_iter);
2231 last_dma_addr = first_dma_addr;
2235 vpbl.pbl_vbase[cur_pbl_index].pa_low =
2236 cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2237 vpbl.pbl_vbase[cur_pbl_index].pa_high =
2238 cpu_to_le32((u32)((u64)(sg_page_iter_dma_address(&dma_iter))));
2244 nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2245 " stag_key=0x%08x\n",
2246 stag_index, driver_key, stag_key);
2247 stag = stag_index << 8;
2249 stag += (u32)stag_key;
2252 /* Make the leaf PBL the root if only one PBL */
2253 if (root_pbl_index == 1) {
2254 root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2260 pbl_count = root_pbl_index;
2263 nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2264 " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2265 stag, (unsigned int)iova_start,
2266 (unsigned int)region_length, stag_index,
2267 (unsigned long long)region->length, pbl_count);
2268 ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2269 first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2270 &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2272 nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2275 nesmr->ibmr.rkey = stag;
2276 nesmr->ibmr.lkey = stag;
2277 nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2278 ibmr = &nesmr->ibmr;
2280 ib_umem_release(region);
2282 ibmr = ERR_PTR(-ENOMEM);
2286 /* free the resources */
2287 if (root_pbl_index == 1) {
2288 pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2291 for (page_index=0; page_index<root_pbl_index; page_index++) {
2292 pci_free_consistent(nesdev->pcidev, 4096,
2293 root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2294 root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2296 kfree(root_vpbl.leaf_vpbl);
2297 pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2298 root_vpbl.pbl_pbase);
2301 nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2304 case IWNES_MEMREG_TYPE_QP:
2305 case IWNES_MEMREG_TYPE_CQ:
2306 if (!region->length) {
2307 nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2308 ib_umem_release(region);
2309 return ERR_PTR(-EINVAL);
2311 nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2313 ib_umem_release(region);
2314 return ERR_PTR(-ENOMEM);
2316 nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2318 ib_umem_release(region);
2320 return ERR_PTR(-ENOMEM);
2322 nesmr->region = region;
2323 pbl_depth = region->length >> 12;
2324 pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2325 nespbl->pbl_size = pbl_depth*sizeof(u64);
2326 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2327 nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2329 nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2332 nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2333 nespbl->pbl_size, pbl_depth);
2334 pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2335 &nespbl->pbl_pbase);
2337 ib_umem_release(region);
2340 nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2341 return ERR_PTR(-ENOMEM);
2344 nespbl->pbl_vbase = (u64 *)pbl;
2345 nespbl->user_base = start;
2346 nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2347 " pbl_vbase=%p user_base=0x%lx\n",
2348 nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2349 (void *) nespbl->pbl_vbase, nespbl->user_base);
2351 nespbl->page = sg_page(region->sg_head.sgl);
2352 for_each_sg_dma_page(region->sg_head.sgl, &dma_iter, region->nmap, 0) {
2353 ((__le32 *)pbl)[0] = cpu_to_le32((u32)(sg_page_iter_dma_address(&dma_iter)));
2354 ((__le32 *)pbl)[1] = cpu_to_le32(((u64)(sg_page_iter_dma_address(&dma_iter)))>>32);
2355 nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2356 (unsigned long long)*pbl,
2357 le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2361 if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2362 list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2364 list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2366 nesmr->ibmr.rkey = -1;
2367 nesmr->ibmr.lkey = -1;
2368 nesmr->mode = req.reg_type;
2369 return &nesmr->ibmr;
2372 ib_umem_release(region);
2373 return ERR_PTR(-ENOSYS);
2380 static int nes_dereg_mr(struct ib_mr *ib_mr)
2382 struct nes_mr *nesmr = to_nesmr(ib_mr);
2383 struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2384 struct nes_device *nesdev = nesvnic->nesdev;
2385 struct nes_adapter *nesadapter = nesdev->nesadapter;
2386 struct nes_hw_cqp_wqe *cqp_wqe;
2387 struct nes_cqp_request *cqp_request;
2388 unsigned long flags;
2395 pci_free_consistent(nesdev->pcidev,
2396 nesmr->max_pages * sizeof(u64),
2400 if (nesmr->region) {
2401 ib_umem_release(nesmr->region);
2403 if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2408 /* Deallocate the region with the adapter */
2410 cqp_request = nes_get_cqp_request(nesdev);
2411 if (cqp_request == NULL) {
2412 nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2415 cqp_request->waiting = 1;
2416 cqp_wqe = &cqp_request->cqp_wqe;
2418 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2419 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2420 NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2421 NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2422 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2424 atomic_set(&cqp_request->refcount, 2);
2425 nes_post_cqp_request(nesdev, cqp_request);
2428 nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2429 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2431 nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2432 " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2433 ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2435 major_code = cqp_request->major_code;
2436 minor_code = cqp_request->minor_code;
2438 nes_put_cqp_request(nesdev, cqp_request);
2441 nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2442 " ib_mr=%p, rkey = 0x%08X\n",
2443 ib_mr, ib_mr->rkey);
2445 } else if (major_code) {
2446 nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2447 " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2448 major_code, minor_code, ib_mr, ib_mr->rkey);
2452 if (nesmr->pbls_used != 0) {
2453 spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2454 if (nesmr->pbl_4k) {
2455 nesadapter->free_4kpbl += nesmr->pbls_used;
2456 if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2457 printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2458 "exceeded the max(%u)\n",
2459 nesadapter->free_4kpbl,
2460 nesadapter->max_4kpbl);
2462 nesadapter->free_256pbl += nesmr->pbls_used;
2463 if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2464 printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2465 "exceeded the max(%u)\n",
2466 nesadapter->free_256pbl,
2467 nesadapter->max_256pbl);
2469 spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2471 nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2472 (ib_mr->rkey & 0x0fffff00) >> 8);
2483 static ssize_t hw_rev_show(struct device *dev,
2484 struct device_attribute *attr, char *buf)
2486 struct nes_ib_device *nesibdev =
2487 rdma_device_to_drv_device(dev, struct nes_ib_device, ibdev);
2488 struct nes_vnic *nesvnic = nesibdev->nesvnic;
2490 nes_debug(NES_DBG_INIT, "\n");
2491 return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2493 static DEVICE_ATTR_RO(hw_rev);
2498 static ssize_t hca_type_show(struct device *dev,
2499 struct device_attribute *attr, char *buf)
2501 nes_debug(NES_DBG_INIT, "\n");
2502 return sprintf(buf, "NES020\n");
2504 static DEVICE_ATTR_RO(hca_type);
2509 static ssize_t board_id_show(struct device *dev,
2510 struct device_attribute *attr, char *buf)
2512 nes_debug(NES_DBG_INIT, "\n");
2513 return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2515 static DEVICE_ATTR_RO(board_id);
2517 static struct attribute *nes_dev_attributes[] = {
2518 &dev_attr_hw_rev.attr,
2519 &dev_attr_hca_type.attr,
2520 &dev_attr_board_id.attr,
2524 static const struct attribute_group nes_attr_group = {
2525 .attrs = nes_dev_attributes,
2531 static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2532 int attr_mask, struct ib_qp_init_attr *init_attr)
2534 struct nes_qp *nesqp = to_nesqp(ibqp);
2536 nes_debug(NES_DBG_QP, "\n");
2538 attr->qp_access_flags = 0;
2539 attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2540 attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2541 attr->cap.max_recv_sge = 1;
2542 if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2543 attr->cap.max_inline_data = 0;
2545 attr->cap.max_inline_data = 64;
2547 init_attr->event_handler = nesqp->ibqp.event_handler;
2548 init_attr->qp_context = nesqp->ibqp.qp_context;
2549 init_attr->send_cq = nesqp->ibqp.send_cq;
2550 init_attr->recv_cq = nesqp->ibqp.recv_cq;
2551 init_attr->srq = nesqp->ibqp.srq;
2552 init_attr->cap = attr->cap;
2561 int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2562 u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2564 struct nes_hw_cqp_wqe *cqp_wqe;
2565 /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2566 /* struct iw_cm_event cm_event; */
2567 struct nes_cqp_request *cqp_request;
2571 nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2572 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2574 cqp_request = nes_get_cqp_request(nesdev);
2575 if (cqp_request == NULL) {
2576 nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2579 if (wait_completion) {
2580 cqp_request->waiting = 1;
2582 cqp_request->waiting = 0;
2584 cqp_wqe = &cqp_request->cqp_wqe;
2586 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2587 NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2588 nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2589 next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2590 nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2591 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2592 set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2594 /* If sending a terminate message, fill in the length (in words) */
2595 if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2596 !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2597 termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2598 set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2601 atomic_set(&cqp_request->refcount, 2);
2602 nes_post_cqp_request(nesdev, cqp_request);
2605 if (wait_completion) {
2606 /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2607 nesqp->hwqp.qp_id); */
2608 ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2610 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2611 "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2612 nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2613 major_code = cqp_request->major_code;
2615 nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2616 "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2617 nesqp->hwqp.qp_id, cqp_request->major_code,
2618 cqp_request->minor_code, next_iwarp_state);
2621 nes_put_cqp_request(nesdev, cqp_request);
2625 else if (major_code)
2638 int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2639 int attr_mask, struct ib_udata *udata)
2641 struct nes_qp *nesqp = to_nesqp(ibqp);
2642 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2643 struct nes_device *nesdev = nesvnic->nesdev;
2646 u32 next_iwarp_state = 0;
2648 unsigned long qplockflags;
2650 u16 original_last_aeq;
2651 u8 issue_modify_qp = 0;
2654 nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2655 " iwarp_state=0x%X, refcount=%d\n",
2656 nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2657 nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2659 spin_lock_irqsave(&nesqp->lock, qplockflags);
2661 nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2662 " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2663 nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2664 nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2666 if (attr_mask & IB_QP_STATE) {
2667 switch (attr->qp_state) {
2669 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2671 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2672 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2675 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2676 issue_modify_qp = 1;
2679 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2681 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2682 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2685 next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2686 issue_modify_qp = 1;
2689 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2691 if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2692 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2695 if (nesqp->cm_id == NULL) {
2696 nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2697 nesqp->hwqp.qp_id );
2698 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2701 next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2702 if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2703 next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2704 NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2705 issue_modify_qp = 1;
2706 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2707 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2708 nesqp->hte_added = 1;
2711 issue_modify_qp = 1;
2712 nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2713 nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2714 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2715 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2718 if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2719 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2720 " ignored due to current iWARP state\n",
2722 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2725 if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2726 nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2727 " already done based on hw state.\n",
2729 issue_modify_qp = 0;
2731 switch (nesqp->hw_iwarp_state) {
2732 case NES_AEQE_IWARP_STATE_CLOSING:
2733 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2735 case NES_AEQE_IWARP_STATE_TERMINATE:
2736 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2738 case NES_AEQE_IWARP_STATE_ERROR:
2739 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2742 next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2743 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2749 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2751 if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2752 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2755 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2756 next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2757 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2758 issue_modify_qp = 1;
2762 if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2763 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2766 nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2768 if (nesqp->term_flags)
2769 del_timer(&nesqp->terminate_timer);
2771 next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2772 /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2773 if (nesqp->hte_added) {
2774 nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2775 next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2776 nesqp->hte_added = 0;
2778 if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
2779 (nesdev->iw_status) &&
2780 (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2781 next_iwarp_state |= NES_CQP_QP_RESET;
2783 nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2784 nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2787 issue_modify_qp = 1;
2788 nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2791 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2796 nesqp->ibqp_state = attr->qp_state;
2797 nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2798 nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2799 nesqp->iwarp_state);
2802 if (attr_mask & IB_QP_ACCESS_FLAGS) {
2803 if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2804 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2805 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2806 issue_modify_qp = 1;
2808 if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2809 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2810 issue_modify_qp = 1;
2812 if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2813 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2814 issue_modify_qp = 1;
2816 if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2817 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2818 issue_modify_qp = 1;
2821 if (nesqp->user_mode) {
2822 nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2823 NES_QPCONTEXT_MISC_RDMA_READ_EN);
2824 issue_modify_qp = 1;
2828 original_last_aeq = nesqp->last_aeq;
2829 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2831 nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2836 if (issue_modify_qp) {
2837 nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
2838 ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
2840 nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2841 " failed for QP%u.\n",
2842 next_iwarp_state, nesqp->hwqp.qp_id);
2846 if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2847 nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2848 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2849 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2850 original_last_aeq, nesqp->last_aeq);
2851 if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
2853 if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2854 nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2855 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2856 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2857 original_last_aeq, nesqp->last_aeq);
2858 /* this one is for the cm_disconnect thread */
2859 spin_lock_irqsave(&nesqp->lock, qplockflags);
2860 nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2861 nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2862 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2863 nes_cm_disconn(nesqp);
2865 nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2866 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2869 spin_lock_irqsave(&nesqp->lock, qplockflags);
2871 /* These two are for the timer thread */
2872 if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
2873 nesqp->cm_id->add_ref(nesqp->cm_id);
2874 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2875 " need ae to finish up, original_last_aeq = 0x%04X."
2876 " last_aeq = 0x%04X, scheduling timer.\n",
2877 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2878 original_last_aeq, nesqp->last_aeq);
2879 schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
2881 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2883 spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2884 nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
2885 " need ae to finish up, original_last_aeq = 0x%04X."
2886 " last_aeq = 0x%04X.\n",
2887 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2888 original_last_aeq, nesqp->last_aeq);
2892 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2893 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2894 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2895 original_last_aeq, nesqp->last_aeq);
2898 nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
2899 " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2900 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2901 original_last_aeq, nesqp->last_aeq);
2906 nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
2907 nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2913 fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, const struct ib_send_wr *ib_wr,
2917 int total_payload_length = 0;
2918 for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
2919 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
2920 ib_wr->sg_list[sge_index].addr);
2921 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
2922 ib_wr->sg_list[sge_index].length);
2924 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
2925 (ib_wr->sg_list[sge_index].lkey));
2927 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
2929 total_payload_length += ib_wr->sg_list[sge_index].length;
2931 nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
2932 total_payload_length);
2933 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
2934 total_payload_length);
2940 static int nes_post_send(struct ib_qp *ibqp, const struct ib_send_wr *ib_wr,
2941 const struct ib_send_wr **bad_wr)
2944 unsigned long flags = 0;
2945 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2946 struct nes_device *nesdev = nesvnic->nesdev;
2947 struct nes_qp *nesqp = to_nesqp(ibqp);
2948 struct nes_hw_qp_wqe *wqe;
2950 u32 qsize = nesqp->hwqp.sq_size;
2956 if (nesqp->ibqp_state > IB_QPS_RTS) {
2961 spin_lock_irqsave(&nesqp->lock, flags);
2963 head = nesqp->hwqp.sq_head;
2966 /* Check for QP error */
2967 if (nesqp->term_flags) {
2972 /* Check for SQ overflow */
2973 if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
2978 wqe = &nesqp->hwqp.sq_vbase[head];
2979 /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
2980 nesqp->hwqp.qp_id, wqe, head); */
2981 nes_fill_init_qp_wqe(wqe, nesqp, head);
2982 u64temp = (u64)(ib_wr->wr_id);
2983 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
2985 switch (ib_wr->opcode) {
2987 case IB_WR_SEND_WITH_INV:
2988 if (IB_WR_SEND == ib_wr->opcode) {
2989 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2990 wqe_misc = NES_IWARP_SQ_OP_SENDSE;
2992 wqe_misc = NES_IWARP_SQ_OP_SEND;
2994 if (ib_wr->send_flags & IB_SEND_SOLICITED)
2995 wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
2997 wqe_misc = NES_IWARP_SQ_OP_SENDINV;
2999 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3000 ib_wr->ex.invalidate_rkey);
3003 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3008 if (ib_wr->send_flags & IB_SEND_FENCE)
3009 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3011 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3012 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3013 (ib_wr->sg_list[0].length <= 64)) {
3014 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3015 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3016 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3017 ib_wr->sg_list[0].length);
3018 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3020 fill_wqe_sg_send(wqe, ib_wr, 1);
3024 case IB_WR_RDMA_WRITE:
3025 wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3026 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3027 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3028 ib_wr->num_sge, nesdev->nesadapter->max_sge);
3033 if (ib_wr->send_flags & IB_SEND_FENCE)
3034 wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3036 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3037 rdma_wr(ib_wr)->rkey);
3038 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3039 rdma_wr(ib_wr)->remote_addr);
3041 if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3042 ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3043 (ib_wr->sg_list[0].length <= 64)) {
3044 memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3045 (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3046 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3047 ib_wr->sg_list[0].length);
3048 wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3050 fill_wqe_sg_send(wqe, ib_wr, 1);
3053 wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3054 wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3056 case IB_WR_RDMA_READ:
3057 case IB_WR_RDMA_READ_WITH_INV:
3058 /* iWARP only supports 1 sge for RDMA reads */
3059 if (ib_wr->num_sge > 1) {
3060 nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3065 if (ib_wr->opcode == IB_WR_RDMA_READ) {
3066 wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3068 wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3069 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3070 ib_wr->ex.invalidate_rkey);
3073 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3074 rdma_wr(ib_wr)->remote_addr);
3075 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3076 rdma_wr(ib_wr)->rkey);
3077 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3078 ib_wr->sg_list->length);
3079 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3080 ib_wr->sg_list->addr);
3081 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3082 ib_wr->sg_list->lkey);
3084 case IB_WR_LOCAL_INV:
3085 wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3086 set_wqe_32bit_value(wqe->wqe_words,
3087 NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3088 ib_wr->ex.invalidate_rkey);
3092 struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3093 int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3094 int flags = reg_wr(ib_wr)->access;
3096 if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3097 nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3101 wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3102 set_wqe_64bit_value(wqe->wqe_words,
3103 NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3105 set_wqe_32bit_value(wqe->wqe_words,
3106 NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3107 lower_32_bits(mr->ibmr.length));
3108 set_wqe_32bit_value(wqe->wqe_words,
3109 NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3110 set_wqe_32bit_value(wqe->wqe_words,
3111 NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3112 reg_wr(ib_wr)->key);
3114 if (page_shift == 12) {
3115 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3116 } else if (page_shift == 21) {
3117 wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3119 nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3120 " ib_wr=%u, max=1\n", ib_wr->num_sge);
3125 /* Set access_flags */
3126 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3127 if (flags & IB_ACCESS_LOCAL_WRITE)
3128 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3130 if (flags & IB_ACCESS_REMOTE_WRITE)
3131 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3133 if (flags & IB_ACCESS_REMOTE_READ)
3134 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3136 if (flags & IB_ACCESS_MW_BIND)
3137 wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3139 /* Fill in PBL info: */
3140 set_wqe_64bit_value(wqe->wqe_words,
3141 NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3144 set_wqe_32bit_value(wqe->wqe_words,
3145 NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3148 nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3149 "length: %lld, rkey: %0x, pgl_paddr: %llx, "
3150 "page_list_len: %u, wqe_misc: %x\n",
3151 (unsigned long long) mr->ibmr.iova,
3154 (unsigned long long) mr->paddr,
3168 if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3169 wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3171 wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3173 ib_wr = ib_wr->next;
3181 nesqp->hwqp.sq_head = head;
3184 counter = min(wqe_count, ((u32)255));
3185 wqe_count -= counter;
3186 nes_write32(nesdev->regs + NES_WQE_ALLOC,
3187 (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3190 spin_unlock_irqrestore(&nesqp->lock, flags);
3202 static int nes_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *ib_wr,
3203 const struct ib_recv_wr **bad_wr)
3206 unsigned long flags = 0;
3207 struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3208 struct nes_device *nesdev = nesvnic->nesdev;
3209 struct nes_qp *nesqp = to_nesqp(ibqp);
3210 struct nes_hw_qp_wqe *wqe;
3213 u32 qsize = nesqp->hwqp.rq_size;
3217 u32 total_payload_length;
3219 if (nesqp->ibqp_state > IB_QPS_RTS) {
3224 spin_lock_irqsave(&nesqp->lock, flags);
3226 head = nesqp->hwqp.rq_head;
3229 /* Check for QP error */
3230 if (nesqp->term_flags) {
3235 if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3239 /* Check for RQ overflow */
3240 if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3245 nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3246 wqe = &nesqp->hwqp.rq_vbase[head];
3248 /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3249 nesqp->hwqp.qp_id, wqe, head); */
3250 nes_fill_init_qp_wqe(wqe, nesqp, head);
3251 u64temp = (u64)(ib_wr->wr_id);
3252 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3254 total_payload_length = 0;
3255 for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3256 set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3257 ib_wr->sg_list[sge_index].addr);
3258 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3259 ib_wr->sg_list[sge_index].length);
3260 set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3261 ib_wr->sg_list[sge_index].lkey);
3263 total_payload_length += ib_wr->sg_list[sge_index].length;
3265 set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3266 total_payload_length);
3268 ib_wr = ib_wr->next;
3275 nesqp->hwqp.rq_head = head;
3278 counter = min(wqe_count, ((u32)255));
3279 wqe_count -= counter;
3280 nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3283 spin_unlock_irqrestore(&nesqp->lock, flags);
3292 * nes_drain_sq - drain sq
3293 * @ibqp: pointer to ibqp
3295 static void nes_drain_sq(struct ib_qp *ibqp)
3297 struct nes_qp *nesqp = to_nesqp(ibqp);
3299 if (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)
3300 wait_for_completion(&nesqp->sq_drained);
3304 * nes_drain_rq - drain rq
3305 * @ibqp: pointer to ibqp
3307 static void nes_drain_rq(struct ib_qp *ibqp)
3309 struct nes_qp *nesqp = to_nesqp(ibqp);
3311 if (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)
3312 wait_for_completion(&nesqp->rq_drained);
3318 static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3322 unsigned long flags = 0;
3323 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3324 struct nes_device *nesdev = nesvnic->nesdev;
3325 struct nes_cq *nescq = to_nescq(ibcq);
3326 struct nes_qp *nesqp;
3327 struct nes_hw_cqe cqe;
3334 u32 move_cq_head = 1;
3337 nes_debug(NES_DBG_CQ, "\n");
3339 spin_lock_irqsave(&nescq->lock, flags);
3341 head = nescq->hw_cq.cq_head;
3342 cq_size = nescq->hw_cq.cq_size;
3344 while (cqe_count < num_entries) {
3345 if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3346 NES_CQE_VALID) == 0)
3350 * Make sure we read CQ entry contents *after*
3351 * we've checked the valid bit.
3355 cqe = nescq->hw_cq.cq_vbase[head];
3356 u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3357 wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3358 u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3359 /* parse CQE, get completion context from WQE (either rq or sq) */
3360 u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3364 nesqp = (struct nes_qp *)(unsigned long)u64temp;
3365 memset(entry, 0, sizeof *entry);
3366 if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3367 entry->status = IB_WC_SUCCESS;
3369 err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3370 if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3371 entry->status = err_code & 0x0000ffff;
3373 /* The rest of the cqe's will be marked as flushed */
3374 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3375 cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3376 NES_IWARP_CQE_MINOR_FLUSH);
3378 entry->status = IB_WC_WR_FLUSH_ERR;
3381 entry->qp = &nesqp->ibqp;
3382 entry->src_qp = nesqp->hwqp.qp_id;
3384 if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3385 if (nesqp->skip_lsmm) {
3386 nesqp->skip_lsmm = 0;
3387 nesqp->hwqp.sq_tail++;
3390 /* Working on a SQ Completion*/
3391 wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3392 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3393 ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3394 wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3395 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3396 wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3398 switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3399 wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3400 case NES_IWARP_SQ_OP_RDMAW:
3401 nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3402 entry->opcode = IB_WC_RDMA_WRITE;
3404 case NES_IWARP_SQ_OP_RDMAR:
3405 nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3406 entry->opcode = IB_WC_RDMA_READ;
3407 entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3408 wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3410 case NES_IWARP_SQ_OP_SENDINV:
3411 case NES_IWARP_SQ_OP_SENDSEINV:
3412 case NES_IWARP_SQ_OP_SEND:
3413 case NES_IWARP_SQ_OP_SENDSE:
3414 nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3415 entry->opcode = IB_WC_SEND;
3417 case NES_IWARP_SQ_OP_LOCINV:
3418 entry->opcode = IB_WC_LOCAL_INV;
3420 case NES_IWARP_SQ_OP_FAST_REG:
3421 entry->opcode = IB_WC_REG_MR;
3425 nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3426 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3428 wq_tail = nesqp->hwqp.sq_tail;
3431 /* Working on a RQ Completion*/
3432 entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3433 wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3434 ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3435 entry->opcode = IB_WC_RECV;
3437 nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3438 if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3440 wq_tail = nesqp->hwqp.rq_tail;
3444 if (nesqp->iwarp_state > NES_CQP_QP_IWARP_STATE_RTS) {
3445 if (nesqp->hwqp.sq_tail == nesqp->hwqp.sq_head)
3446 complete(&nesqp->sq_drained);
3447 if (nesqp->hwqp.rq_tail == nesqp->hwqp.rq_head)
3448 complete(&nesqp->rq_drained);
3451 entry->wr_id = wrid;
3457 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3458 if (++head >= cq_size)
3460 nescq->polled_completions++;
3462 if ((nescq->polled_completions > (cq_size / 2)) ||
3463 (nescq->polled_completions == 255)) {
3464 nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3465 " are pending %u of %u.\n",
3466 nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3467 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3468 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3469 nescq->polled_completions = 0;
3472 /* Update the wqe index and set status to flush */
3473 wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3474 wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3475 nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3476 cpu_to_le32(wqe_index);
3477 move_cq_head = 1; /* ready for next pass */
3481 if (nescq->polled_completions) {
3482 nes_write32(nesdev->regs+NES_CQE_ALLOC,
3483 nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3484 nescq->polled_completions = 0;
3487 nescq->hw_cq.cq_head = head;
3488 nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3489 cqe_count, nescq->hw_cq.cq_number);
3491 spin_unlock_irqrestore(&nescq->lock, flags);
3500 static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3502 struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3503 struct nes_device *nesdev = nesvnic->nesdev;
3504 struct nes_cq *nescq = to_nescq(ibcq);
3507 nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3508 nescq->hw_cq.cq_number);
3510 cq_arm = nescq->hw_cq.cq_number;
3511 if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3512 cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3513 else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3514 cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3518 nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3519 nes_read32(nesdev->regs+NES_CQE_ALLOC);
3524 static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3525 struct ib_port_immutable *immutable)
3527 struct ib_port_attr attr;
3530 immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3532 err = nes_query_port(ibdev, port_num, &attr);
3536 immutable->pkey_tbl_len = attr.pkey_tbl_len;
3537 immutable->gid_tbl_len = attr.gid_tbl_len;
3542 static void get_dev_fw_str(struct ib_device *dev, char *str)
3544 struct nes_ib_device *nesibdev =
3545 container_of(dev, struct nes_ib_device, ibdev);
3546 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3548 nes_debug(NES_DBG_INIT, "\n");
3549 snprintf(str, IB_FW_VERSION_NAME_MAX, "%u.%u",
3550 (nesvnic->nesdev->nesadapter->firmware_version >> 16),
3551 (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
3554 static const struct ib_device_ops nes_dev_ops = {
3555 .alloc_mr = nes_alloc_mr,
3556 .alloc_mw = nes_alloc_mw,
3557 .alloc_pd = nes_alloc_pd,
3558 .alloc_ucontext = nes_alloc_ucontext,
3559 .create_cq = nes_create_cq,
3560 .create_qp = nes_create_qp,
3561 .dealloc_mw = nes_dealloc_mw,
3562 .dealloc_pd = nes_dealloc_pd,
3563 .dealloc_ucontext = nes_dealloc_ucontext,
3564 .dereg_mr = nes_dereg_mr,
3565 .destroy_cq = nes_destroy_cq,
3566 .destroy_qp = nes_destroy_qp,
3567 .drain_rq = nes_drain_rq,
3568 .drain_sq = nes_drain_sq,
3569 .get_dev_fw_str = get_dev_fw_str,
3570 .get_dma_mr = nes_get_dma_mr,
3571 .get_port_immutable = nes_port_immutable,
3572 .map_mr_sg = nes_map_mr_sg,
3574 .modify_qp = nes_modify_qp,
3575 .poll_cq = nes_poll_cq,
3576 .post_recv = nes_post_recv,
3577 .post_send = nes_post_send,
3578 .query_device = nes_query_device,
3579 .query_gid = nes_query_gid,
3580 .query_pkey = nes_query_pkey,
3581 .query_port = nes_query_port,
3582 .query_qp = nes_query_qp,
3583 .reg_user_mr = nes_reg_user_mr,
3584 .req_notify_cq = nes_req_notify_cq,
3585 INIT_RDMA_OBJ_SIZE(ib_pd, nes_pd, ibpd),
3586 INIT_RDMA_OBJ_SIZE(ib_ucontext, nes_ucontext, ibucontext),
3590 * nes_init_ofa_device
3592 struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3594 struct nes_ib_device *nesibdev;
3595 struct nes_vnic *nesvnic = netdev_priv(netdev);
3596 struct nes_device *nesdev = nesvnic->nesdev;
3598 nesibdev = ib_alloc_device(nes_ib_device, ibdev);
3599 if (nesibdev == NULL) {
3602 nesibdev->ibdev.owner = THIS_MODULE;
3604 nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3605 memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3606 memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3608 nesibdev->ibdev.uverbs_cmd_mask =
3609 (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3610 (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3611 (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3612 (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3613 (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3614 (1ull << IB_USER_VERBS_CMD_REG_MR) |
3615 (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3616 (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3617 (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3618 (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3619 (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3620 (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3621 (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3622 (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3623 (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3624 (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3625 (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3626 (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3627 (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3628 (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3629 (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3630 (1ull << IB_USER_VERBS_CMD_POST_SEND);
3632 nesibdev->ibdev.phys_port_cnt = 1;
3633 nesibdev->ibdev.num_comp_vectors = 1;
3634 nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3636 nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3637 if (nesibdev->ibdev.iwcm == NULL) {
3638 ib_dealloc_device(&nesibdev->ibdev);
3641 nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3642 nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3643 nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3644 nesibdev->ibdev.iwcm->connect = nes_connect;
3645 nesibdev->ibdev.iwcm->accept = nes_accept;
3646 nesibdev->ibdev.iwcm->reject = nes_reject;
3647 nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3648 nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3650 ib_set_device_ops(&nesibdev->ibdev, &nes_dev_ops);
3651 memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3652 sizeof(nesibdev->ibdev.iwcm->ifname));
3659 * nes_handle_delayed_event
3661 static void nes_handle_delayed_event(struct timer_list *t)
3663 struct nes_vnic *nesvnic = from_timer(nesvnic, t, event_timer);
3665 if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3666 struct ib_event event;
3668 event.device = &nesvnic->nesibdev->ibdev;
3671 event.event = nesvnic->delayed_event;
3672 event.element.port_num = nesvnic->logical_port + 1;
3673 ib_dispatch_event(&event);
3677 nesvnic->event_timer.function = NULL;
3681 void nes_port_ibevent(struct nes_vnic *nesvnic)
3683 struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3684 struct nes_device *nesdev = nesvnic->nesdev;
3685 struct ib_event event;
3686 event.device = &nesibdev->ibdev;
3687 event.element.port_num = nesvnic->logical_port + 1;
3688 event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3690 if (!nesvnic->event_timer.function) {
3691 ib_dispatch_event(&event);
3692 nesvnic->last_dispatched_event = event.event;
3693 nesvnic->event_timer.function = nes_handle_delayed_event;
3694 nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3695 add_timer(&nesvnic->event_timer);
3697 mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3699 nesvnic->delayed_event = event.event;
3704 * nes_destroy_ofa_device
3706 void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3708 if (nesibdev == NULL)
3711 nes_unregister_ofa_device(nesibdev);
3713 kfree(nesibdev->ibdev.iwcm);
3714 ib_dealloc_device(&nesibdev->ibdev);
3719 * nes_register_ofa_device
3721 int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3723 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3724 struct nes_device *nesdev = nesvnic->nesdev;
3725 struct nes_adapter *nesadapter = nesdev->nesadapter;
3728 rdma_set_device_sysfs_group(&nesvnic->nesibdev->ibdev, &nes_attr_group);
3729 nesvnic->nesibdev->ibdev.driver_id = RDMA_DRIVER_NES;
3730 ret = ib_register_device(&nesvnic->nesibdev->ibdev, "nes%d");
3735 /* Get the resources allocated to this device */
3736 nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3737 nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3738 nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3739 nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3741 nesvnic->of_device_registered = 1;
3748 * nes_unregister_ofa_device
3750 static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3752 struct nes_vnic *nesvnic = nesibdev->nesvnic;
3754 if (nesvnic->of_device_registered)
3755 ib_unregister_device(&nesibdev->ibdev);
3757 nesvnic->of_device_registered = 0;