Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1 | /******************************************************************* |
| 2 | * This file is part of the Emulex RoCE Device Driver for * |
| 3 | * RoCE (RDMA over Converged Ethernet) adapters. * |
| 4 | * Copyright (C) 2008-2012 Emulex. All rights reserved. * |
| 5 | * EMULEX and SLI are trademarks of Emulex. * |
| 6 | * www.emulex.com * |
| 7 | * * |
| 8 | * This program is free software; you can redistribute it and/or * |
| 9 | * modify it under the terms of version 2 of the GNU General * |
| 10 | * Public License as published by the Free Software Foundation. * |
| 11 | * This program is distributed in the hope that it will be useful. * |
| 12 | * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * |
| 13 | * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE * |
| 15 | * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD * |
| 16 | * TO BE LEGALLY INVALID. See the GNU General Public License for * |
| 17 | * more details, a copy of which can be found in the file COPYING * |
| 18 | * included with this package. * |
| 19 | * |
| 20 | * Contact Information: |
| 21 | * linux-drivers@emulex.com |
| 22 | * |
| 23 | * Emulex |
| 24 | * 3333 Susan Street |
| 25 | * Costa Mesa, CA 92626 |
| 26 | *******************************************************************/ |
| 27 | |
| 28 | #include <linux/dma-mapping.h> |
| 29 | #include <rdma/ib_verbs.h> |
| 30 | #include <rdma/ib_user_verbs.h> |
| 31 | #include <rdma/iw_cm.h> |
| 32 | #include <rdma/ib_umem.h> |
| 33 | #include <rdma/ib_addr.h> |
| 34 | |
| 35 | #include "ocrdma.h" |
| 36 | #include "ocrdma_hw.h" |
| 37 | #include "ocrdma_verbs.h" |
| 38 | #include "ocrdma_abi.h" |
| 39 | |
| 40 | int ocrdma_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey) |
| 41 | { |
| 42 | if (index > 1) |
| 43 | return -EINVAL; |
| 44 | |
| 45 | *pkey = 0xffff; |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | int ocrdma_query_gid(struct ib_device *ibdev, u8 port, |
| 50 | int index, union ib_gid *sgid) |
| 51 | { |
| 52 | struct ocrdma_dev *dev; |
| 53 | |
| 54 | dev = get_ocrdma_dev(ibdev); |
| 55 | memset(sgid, 0, sizeof(*sgid)); |
| 56 | if (index > OCRDMA_MAX_SGID) |
| 57 | return -EINVAL; |
| 58 | |
| 59 | memcpy(sgid, &dev->sgid_tbl[index], sizeof(*sgid)); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int ocrdma_query_device(struct ib_device *ibdev, struct ib_device_attr *attr) |
| 65 | { |
| 66 | struct ocrdma_dev *dev = get_ocrdma_dev(ibdev); |
| 67 | |
| 68 | memset(attr, 0, sizeof *attr); |
| 69 | memcpy(&attr->fw_ver, &dev->attr.fw_ver[0], |
| 70 | min(sizeof(dev->attr.fw_ver), sizeof(attr->fw_ver))); |
| 71 | ocrdma_get_guid(dev, (u8 *)&attr->sys_image_guid); |
| 72 | attr->max_mr_size = ~0ull; |
| 73 | attr->page_size_cap = 0xffff000; |
| 74 | attr->vendor_id = dev->nic_info.pdev->vendor; |
| 75 | attr->vendor_part_id = dev->nic_info.pdev->device; |
| 76 | attr->hw_ver = 0; |
| 77 | attr->max_qp = dev->attr.max_qp; |
| 78 | attr->max_ah = dev->attr.max_qp; |
| 79 | attr->max_qp_wr = dev->attr.max_wqe; |
| 80 | |
| 81 | attr->device_cap_flags = IB_DEVICE_CURR_QP_STATE_MOD | |
| 82 | IB_DEVICE_RC_RNR_NAK_GEN | |
| 83 | IB_DEVICE_SHUTDOWN_PORT | |
| 84 | IB_DEVICE_SYS_IMAGE_GUID | |
| 85 | IB_DEVICE_LOCAL_DMA_LKEY; |
| 86 | attr->max_sge = dev->attr.max_send_sge; |
| 87 | attr->max_sge_rd = dev->attr.max_send_sge; |
| 88 | attr->max_cq = dev->attr.max_cq; |
| 89 | attr->max_cqe = dev->attr.max_cqe; |
| 90 | attr->max_mr = dev->attr.max_mr; |
| 91 | attr->max_mw = 0; |
| 92 | attr->max_pd = dev->attr.max_pd; |
| 93 | attr->atomic_cap = 0; |
| 94 | attr->max_fmr = 0; |
| 95 | attr->max_map_per_fmr = 0; |
| 96 | attr->max_qp_rd_atom = |
| 97 | min(dev->attr.max_ord_per_qp, dev->attr.max_ird_per_qp); |
| 98 | attr->max_qp_init_rd_atom = dev->attr.max_ord_per_qp; |
| 99 | attr->max_srq = (dev->attr.max_qp - 1); |
| 100 | attr->max_srq_sge = attr->max_sge; |
| 101 | attr->max_srq_wr = dev->attr.max_rqe; |
| 102 | attr->local_ca_ack_delay = dev->attr.local_ca_ack_delay; |
| 103 | attr->max_fast_reg_page_list_len = 0; |
| 104 | attr->max_pkeys = 1; |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int ocrdma_query_port(struct ib_device *ibdev, |
| 109 | u8 port, struct ib_port_attr *props) |
| 110 | { |
| 111 | enum ib_port_state port_state; |
| 112 | struct ocrdma_dev *dev; |
| 113 | struct net_device *netdev; |
| 114 | |
| 115 | dev = get_ocrdma_dev(ibdev); |
| 116 | if (port > 1) { |
| 117 | ocrdma_err("%s(%d) invalid_port=0x%x\n", __func__, |
| 118 | dev->id, port); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | netdev = dev->nic_info.netdev; |
| 122 | if (netif_running(netdev) && netif_oper_up(netdev)) { |
| 123 | port_state = IB_PORT_ACTIVE; |
| 124 | props->phys_state = 5; |
| 125 | } else { |
| 126 | port_state = IB_PORT_DOWN; |
| 127 | props->phys_state = 3; |
| 128 | } |
| 129 | props->max_mtu = IB_MTU_4096; |
| 130 | props->active_mtu = iboe_get_mtu(netdev->mtu); |
| 131 | props->lid = 0; |
| 132 | props->lmc = 0; |
| 133 | props->sm_lid = 0; |
| 134 | props->sm_sl = 0; |
| 135 | props->state = port_state; |
| 136 | props->port_cap_flags = |
| 137 | IB_PORT_CM_SUP | |
| 138 | IB_PORT_REINIT_SUP | |
| 139 | IB_PORT_DEVICE_MGMT_SUP | IB_PORT_VENDOR_CLASS_SUP; |
| 140 | props->gid_tbl_len = OCRDMA_MAX_SGID; |
| 141 | props->pkey_tbl_len = 1; |
| 142 | props->bad_pkey_cntr = 0; |
| 143 | props->qkey_viol_cntr = 0; |
| 144 | props->active_width = IB_WIDTH_1X; |
| 145 | props->active_speed = 4; |
| 146 | props->max_msg_sz = 0x80000000; |
| 147 | props->max_vl_num = 4; |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int ocrdma_modify_port(struct ib_device *ibdev, u8 port, int mask, |
| 152 | struct ib_port_modify *props) |
| 153 | { |
| 154 | struct ocrdma_dev *dev; |
| 155 | |
| 156 | dev = get_ocrdma_dev(ibdev); |
| 157 | if (port > 1) { |
| 158 | ocrdma_err("%s(%d) invalid_port=0x%x\n", __func__, |
| 159 | dev->id, port); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int ocrdma_add_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr, |
| 166 | unsigned long len) |
| 167 | { |
| 168 | struct ocrdma_mm *mm; |
| 169 | |
| 170 | mm = kzalloc(sizeof(*mm), GFP_KERNEL); |
| 171 | if (mm == NULL) |
| 172 | return -ENOMEM; |
| 173 | mm->key.phy_addr = phy_addr; |
| 174 | mm->key.len = len; |
| 175 | INIT_LIST_HEAD(&mm->entry); |
| 176 | |
| 177 | mutex_lock(&uctx->mm_list_lock); |
| 178 | list_add_tail(&mm->entry, &uctx->mm_head); |
| 179 | mutex_unlock(&uctx->mm_list_lock); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static void ocrdma_del_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr, |
| 184 | unsigned long len) |
| 185 | { |
| 186 | struct ocrdma_mm *mm, *tmp; |
| 187 | |
| 188 | mutex_lock(&uctx->mm_list_lock); |
| 189 | list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) { |
| 190 | if (len != mm->key.len || phy_addr != mm->key.phy_addr) |
| 191 | continue; |
| 192 | |
| 193 | list_del(&mm->entry); |
| 194 | kfree(mm); |
| 195 | break; |
| 196 | } |
| 197 | mutex_unlock(&uctx->mm_list_lock); |
| 198 | } |
| 199 | |
| 200 | static bool ocrdma_search_mmap(struct ocrdma_ucontext *uctx, u64 phy_addr, |
| 201 | unsigned long len) |
| 202 | { |
| 203 | bool found = false; |
| 204 | struct ocrdma_mm *mm; |
| 205 | |
| 206 | mutex_lock(&uctx->mm_list_lock); |
| 207 | list_for_each_entry(mm, &uctx->mm_head, entry) { |
| 208 | if (len != mm->key.len || phy_addr != mm->key.phy_addr) |
| 209 | continue; |
| 210 | |
| 211 | found = true; |
| 212 | break; |
| 213 | } |
| 214 | mutex_unlock(&uctx->mm_list_lock); |
| 215 | return found; |
| 216 | } |
| 217 | |
| 218 | struct ib_ucontext *ocrdma_alloc_ucontext(struct ib_device *ibdev, |
| 219 | struct ib_udata *udata) |
| 220 | { |
| 221 | int status; |
| 222 | struct ocrdma_ucontext *ctx; |
| 223 | struct ocrdma_alloc_ucontext_resp resp; |
| 224 | struct ocrdma_dev *dev = get_ocrdma_dev(ibdev); |
| 225 | struct pci_dev *pdev = dev->nic_info.pdev; |
| 226 | u32 map_len = roundup(sizeof(u32) * 2048, PAGE_SIZE); |
| 227 | |
| 228 | if (!udata) |
| 229 | return ERR_PTR(-EFAULT); |
| 230 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 231 | if (!ctx) |
| 232 | return ERR_PTR(-ENOMEM); |
| 233 | ctx->dev = dev; |
| 234 | INIT_LIST_HEAD(&ctx->mm_head); |
| 235 | mutex_init(&ctx->mm_list_lock); |
| 236 | |
| 237 | ctx->ah_tbl.va = dma_alloc_coherent(&pdev->dev, map_len, |
| 238 | &ctx->ah_tbl.pa, GFP_KERNEL); |
| 239 | if (!ctx->ah_tbl.va) { |
| 240 | kfree(ctx); |
| 241 | return ERR_PTR(-ENOMEM); |
| 242 | } |
| 243 | memset(ctx->ah_tbl.va, 0, map_len); |
| 244 | ctx->ah_tbl.len = map_len; |
| 245 | |
| 246 | resp.ah_tbl_len = ctx->ah_tbl.len; |
| 247 | resp.ah_tbl_page = ctx->ah_tbl.pa; |
| 248 | |
| 249 | status = ocrdma_add_mmap(ctx, resp.ah_tbl_page, resp.ah_tbl_len); |
| 250 | if (status) |
| 251 | goto map_err; |
| 252 | resp.dev_id = dev->id; |
| 253 | resp.max_inline_data = dev->attr.max_inline_data; |
| 254 | resp.wqe_size = dev->attr.wqe_size; |
| 255 | resp.rqe_size = dev->attr.rqe_size; |
| 256 | resp.dpp_wqe_size = dev->attr.wqe_size; |
| 257 | resp.rsvd = 0; |
| 258 | |
| 259 | memcpy(resp.fw_ver, dev->attr.fw_ver, sizeof(resp.fw_ver)); |
| 260 | status = ib_copy_to_udata(udata, &resp, sizeof(resp)); |
| 261 | if (status) |
| 262 | goto cpy_err; |
| 263 | return &ctx->ibucontext; |
| 264 | |
| 265 | cpy_err: |
| 266 | ocrdma_del_mmap(ctx, ctx->ah_tbl.pa, ctx->ah_tbl.len); |
| 267 | map_err: |
| 268 | dma_free_coherent(&pdev->dev, ctx->ah_tbl.len, ctx->ah_tbl.va, |
| 269 | ctx->ah_tbl.pa); |
| 270 | kfree(ctx); |
| 271 | return ERR_PTR(status); |
| 272 | } |
| 273 | |
| 274 | int ocrdma_dealloc_ucontext(struct ib_ucontext *ibctx) |
| 275 | { |
| 276 | struct ocrdma_mm *mm, *tmp; |
| 277 | struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ibctx); |
| 278 | struct pci_dev *pdev = uctx->dev->nic_info.pdev; |
| 279 | |
| 280 | ocrdma_del_mmap(uctx, uctx->ah_tbl.pa, uctx->ah_tbl.len); |
| 281 | dma_free_coherent(&pdev->dev, uctx->ah_tbl.len, uctx->ah_tbl.va, |
| 282 | uctx->ah_tbl.pa); |
| 283 | |
| 284 | list_for_each_entry_safe(mm, tmp, &uctx->mm_head, entry) { |
| 285 | list_del(&mm->entry); |
| 286 | kfree(mm); |
| 287 | } |
| 288 | kfree(uctx); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | int ocrdma_mmap(struct ib_ucontext *context, struct vm_area_struct *vma) |
| 293 | { |
| 294 | struct ocrdma_ucontext *ucontext = get_ocrdma_ucontext(context); |
| 295 | struct ocrdma_dev *dev = ucontext->dev; |
| 296 | unsigned long vm_page = vma->vm_pgoff << PAGE_SHIFT; |
| 297 | u64 unmapped_db = (u64) dev->nic_info.unmapped_db; |
| 298 | unsigned long len = (vma->vm_end - vma->vm_start); |
| 299 | int status = 0; |
| 300 | bool found; |
| 301 | |
| 302 | if (vma->vm_start & (PAGE_SIZE - 1)) |
| 303 | return -EINVAL; |
| 304 | found = ocrdma_search_mmap(ucontext, vma->vm_pgoff << PAGE_SHIFT, len); |
| 305 | if (!found) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | if ((vm_page >= unmapped_db) && (vm_page <= (unmapped_db + |
| 309 | dev->nic_info.db_total_size)) && |
| 310 | (len <= dev->nic_info.db_page_size)) { |
| 311 | /* doorbell mapping */ |
| 312 | status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
| 313 | len, vma->vm_page_prot); |
| 314 | } else if (dev->nic_info.dpp_unmapped_len && |
| 315 | (vm_page >= (u64) dev->nic_info.dpp_unmapped_addr) && |
| 316 | (vm_page <= (u64) (dev->nic_info.dpp_unmapped_addr + |
| 317 | dev->nic_info.dpp_unmapped_len)) && |
| 318 | (len <= dev->nic_info.dpp_unmapped_len)) { |
| 319 | /* dpp area mapping */ |
| 320 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 321 | status = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
| 322 | len, vma->vm_page_prot); |
| 323 | } else { |
| 324 | /* queue memory mapping */ |
| 325 | status = remap_pfn_range(vma, vma->vm_start, |
| 326 | vma->vm_pgoff, len, vma->vm_page_prot); |
| 327 | } |
| 328 | return status; |
| 329 | } |
| 330 | |
| 331 | static int ocrdma_copy_pd_uresp(struct ocrdma_pd *pd, |
| 332 | struct ib_ucontext *ib_ctx, |
| 333 | struct ib_udata *udata) |
| 334 | { |
| 335 | int status; |
| 336 | u64 db_page_addr; |
Roland Dreier | da49643 | 2012-04-16 11:32:17 -0700 | [diff] [blame] | 337 | u64 dpp_page_addr = 0; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 338 | u32 db_page_size; |
| 339 | struct ocrdma_alloc_pd_uresp rsp; |
| 340 | struct ocrdma_ucontext *uctx = get_ocrdma_ucontext(ib_ctx); |
| 341 | |
| 342 | rsp.id = pd->id; |
| 343 | rsp.dpp_enabled = pd->dpp_enabled; |
| 344 | db_page_addr = pd->dev->nic_info.unmapped_db + |
| 345 | (pd->id * pd->dev->nic_info.db_page_size); |
| 346 | db_page_size = pd->dev->nic_info.db_page_size; |
| 347 | |
| 348 | status = ocrdma_add_mmap(uctx, db_page_addr, db_page_size); |
| 349 | if (status) |
| 350 | return status; |
| 351 | |
| 352 | if (pd->dpp_enabled) { |
| 353 | dpp_page_addr = pd->dev->nic_info.dpp_unmapped_addr + |
| 354 | (pd->id * OCRDMA_DPP_PAGE_SIZE); |
| 355 | status = ocrdma_add_mmap(uctx, dpp_page_addr, |
| 356 | OCRDMA_DPP_PAGE_SIZE); |
| 357 | if (status) |
| 358 | goto dpp_map_err; |
| 359 | rsp.dpp_page_addr_hi = upper_32_bits(dpp_page_addr); |
| 360 | rsp.dpp_page_addr_lo = dpp_page_addr; |
| 361 | } |
| 362 | |
| 363 | status = ib_copy_to_udata(udata, &rsp, sizeof(rsp)); |
| 364 | if (status) |
| 365 | goto ucopy_err; |
| 366 | |
| 367 | pd->uctx = uctx; |
| 368 | return 0; |
| 369 | |
| 370 | ucopy_err: |
Roland Dreier | da49643 | 2012-04-16 11:32:17 -0700 | [diff] [blame] | 371 | if (pd->dpp_enabled) |
| 372 | ocrdma_del_mmap(pd->uctx, dpp_page_addr, OCRDMA_DPP_PAGE_SIZE); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 373 | dpp_map_err: |
| 374 | ocrdma_del_mmap(pd->uctx, db_page_addr, db_page_size); |
| 375 | return status; |
| 376 | } |
| 377 | |
| 378 | struct ib_pd *ocrdma_alloc_pd(struct ib_device *ibdev, |
| 379 | struct ib_ucontext *context, |
| 380 | struct ib_udata *udata) |
| 381 | { |
| 382 | struct ocrdma_dev *dev = get_ocrdma_dev(ibdev); |
| 383 | struct ocrdma_pd *pd; |
| 384 | int status; |
| 385 | |
| 386 | pd = kzalloc(sizeof(*pd), GFP_KERNEL); |
| 387 | if (!pd) |
| 388 | return ERR_PTR(-ENOMEM); |
| 389 | pd->dev = dev; |
| 390 | if (udata && context) { |
| 391 | pd->dpp_enabled = (dev->nic_info.dev_family == |
| 392 | OCRDMA_GEN2_FAMILY) ? true : false; |
| 393 | pd->num_dpp_qp = |
| 394 | pd->dpp_enabled ? OCRDMA_PD_MAX_DPP_ENABLED_QP : 0; |
| 395 | } |
| 396 | status = ocrdma_mbx_alloc_pd(dev, pd); |
| 397 | if (status) { |
| 398 | kfree(pd); |
| 399 | return ERR_PTR(status); |
| 400 | } |
| 401 | atomic_set(&pd->use_cnt, 0); |
| 402 | |
| 403 | if (udata && context) { |
| 404 | status = ocrdma_copy_pd_uresp(pd, context, udata); |
| 405 | if (status) |
| 406 | goto err; |
| 407 | } |
| 408 | return &pd->ibpd; |
| 409 | |
| 410 | err: |
| 411 | ocrdma_dealloc_pd(&pd->ibpd); |
| 412 | return ERR_PTR(status); |
| 413 | } |
| 414 | |
| 415 | int ocrdma_dealloc_pd(struct ib_pd *ibpd) |
| 416 | { |
| 417 | struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); |
| 418 | struct ocrdma_dev *dev = pd->dev; |
| 419 | int status; |
| 420 | u64 usr_db; |
| 421 | |
| 422 | if (atomic_read(&pd->use_cnt)) { |
| 423 | ocrdma_err("%s(%d) pd=0x%x is in use.\n", |
| 424 | __func__, dev->id, pd->id); |
| 425 | status = -EFAULT; |
| 426 | goto dealloc_err; |
| 427 | } |
| 428 | status = ocrdma_mbx_dealloc_pd(dev, pd); |
| 429 | if (pd->uctx) { |
| 430 | u64 dpp_db = dev->nic_info.dpp_unmapped_addr + |
| 431 | (pd->id * OCRDMA_DPP_PAGE_SIZE); |
| 432 | if (pd->dpp_enabled) |
| 433 | ocrdma_del_mmap(pd->uctx, dpp_db, OCRDMA_DPP_PAGE_SIZE); |
| 434 | usr_db = dev->nic_info.unmapped_db + |
| 435 | (pd->id * dev->nic_info.db_page_size); |
| 436 | ocrdma_del_mmap(pd->uctx, usr_db, dev->nic_info.db_page_size); |
| 437 | } |
| 438 | kfree(pd); |
| 439 | dealloc_err: |
| 440 | return status; |
| 441 | } |
| 442 | |
| 443 | static struct ocrdma_mr *ocrdma_alloc_lkey(struct ib_pd *ibpd, |
| 444 | int acc, u32 num_pbls, |
| 445 | u32 addr_check) |
| 446 | { |
| 447 | int status; |
| 448 | struct ocrdma_mr *mr; |
| 449 | struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); |
| 450 | struct ocrdma_dev *dev = pd->dev; |
| 451 | |
| 452 | if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) { |
| 453 | ocrdma_err("%s(%d) leaving err, invalid access rights\n", |
| 454 | __func__, dev->id); |
| 455 | return ERR_PTR(-EINVAL); |
| 456 | } |
| 457 | |
| 458 | mr = kzalloc(sizeof(*mr), GFP_KERNEL); |
| 459 | if (!mr) |
| 460 | return ERR_PTR(-ENOMEM); |
| 461 | mr->hwmr.dev = dev; |
| 462 | mr->hwmr.fr_mr = 0; |
| 463 | mr->hwmr.local_rd = 1; |
| 464 | mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0; |
| 465 | mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0; |
| 466 | mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0; |
| 467 | mr->hwmr.mw_bind = (acc & IB_ACCESS_MW_BIND) ? 1 : 0; |
| 468 | mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0; |
| 469 | mr->hwmr.num_pbls = num_pbls; |
| 470 | |
| 471 | status = ocrdma_mbx_alloc_lkey(dev, &mr->hwmr, pd->id, addr_check); |
| 472 | if (status) { |
| 473 | kfree(mr); |
| 474 | return ERR_PTR(-ENOMEM); |
| 475 | } |
| 476 | mr->pd = pd; |
| 477 | atomic_inc(&pd->use_cnt); |
| 478 | mr->ibmr.lkey = mr->hwmr.lkey; |
| 479 | if (mr->hwmr.remote_wr || mr->hwmr.remote_rd) |
| 480 | mr->ibmr.rkey = mr->hwmr.lkey; |
| 481 | return mr; |
| 482 | } |
| 483 | |
| 484 | struct ib_mr *ocrdma_get_dma_mr(struct ib_pd *ibpd, int acc) |
| 485 | { |
| 486 | struct ocrdma_mr *mr; |
| 487 | |
| 488 | mr = ocrdma_alloc_lkey(ibpd, acc, 0, OCRDMA_ADDR_CHECK_DISABLE); |
| 489 | if (!mr) |
| 490 | return ERR_PTR(-ENOMEM); |
| 491 | |
| 492 | return &mr->ibmr; |
| 493 | } |
| 494 | |
| 495 | static void ocrdma_free_mr_pbl_tbl(struct ocrdma_dev *dev, |
| 496 | struct ocrdma_hw_mr *mr) |
| 497 | { |
| 498 | struct pci_dev *pdev = dev->nic_info.pdev; |
| 499 | int i = 0; |
| 500 | |
| 501 | if (mr->pbl_table) { |
| 502 | for (i = 0; i < mr->num_pbls; i++) { |
| 503 | if (!mr->pbl_table[i].va) |
| 504 | continue; |
| 505 | dma_free_coherent(&pdev->dev, mr->pbl_size, |
| 506 | mr->pbl_table[i].va, |
| 507 | mr->pbl_table[i].pa); |
| 508 | } |
| 509 | kfree(mr->pbl_table); |
| 510 | mr->pbl_table = NULL; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static int ocrdma_get_pbl_info(struct ocrdma_mr *mr, u32 num_pbes) |
| 515 | { |
| 516 | u32 num_pbls = 0; |
| 517 | u32 idx = 0; |
| 518 | int status = 0; |
| 519 | u32 pbl_size; |
| 520 | |
| 521 | do { |
| 522 | pbl_size = OCRDMA_MIN_HPAGE_SIZE * (1 << idx); |
| 523 | if (pbl_size > MAX_OCRDMA_PBL_SIZE) { |
| 524 | status = -EFAULT; |
| 525 | break; |
| 526 | } |
| 527 | num_pbls = roundup(num_pbes, (pbl_size / sizeof(u64))); |
| 528 | num_pbls = num_pbls / (pbl_size / sizeof(u64)); |
| 529 | idx++; |
| 530 | } while (num_pbls >= mr->hwmr.dev->attr.max_num_mr_pbl); |
| 531 | |
| 532 | mr->hwmr.num_pbes = num_pbes; |
| 533 | mr->hwmr.num_pbls = num_pbls; |
| 534 | mr->hwmr.pbl_size = pbl_size; |
| 535 | return status; |
| 536 | } |
| 537 | |
| 538 | static int ocrdma_build_pbl_tbl(struct ocrdma_dev *dev, struct ocrdma_hw_mr *mr) |
| 539 | { |
| 540 | int status = 0; |
| 541 | int i; |
| 542 | u32 dma_len = mr->pbl_size; |
| 543 | struct pci_dev *pdev = dev->nic_info.pdev; |
| 544 | void *va; |
| 545 | dma_addr_t pa; |
| 546 | |
| 547 | mr->pbl_table = kzalloc(sizeof(struct ocrdma_pbl) * |
| 548 | mr->num_pbls, GFP_KERNEL); |
| 549 | |
| 550 | if (!mr->pbl_table) |
| 551 | return -ENOMEM; |
| 552 | |
| 553 | for (i = 0; i < mr->num_pbls; i++) { |
| 554 | va = dma_alloc_coherent(&pdev->dev, dma_len, &pa, GFP_KERNEL); |
| 555 | if (!va) { |
| 556 | ocrdma_free_mr_pbl_tbl(dev, mr); |
| 557 | status = -ENOMEM; |
| 558 | break; |
| 559 | } |
| 560 | memset(va, 0, dma_len); |
| 561 | mr->pbl_table[i].va = va; |
| 562 | mr->pbl_table[i].pa = pa; |
| 563 | } |
| 564 | return status; |
| 565 | } |
| 566 | |
| 567 | static void build_user_pbes(struct ocrdma_dev *dev, struct ocrdma_mr *mr, |
| 568 | u32 num_pbes) |
| 569 | { |
| 570 | struct ocrdma_pbe *pbe; |
| 571 | struct ib_umem_chunk *chunk; |
| 572 | struct ocrdma_pbl *pbl_tbl = mr->hwmr.pbl_table; |
| 573 | struct ib_umem *umem = mr->umem; |
| 574 | int i, shift, pg_cnt, pages, pbe_cnt, total_num_pbes = 0; |
| 575 | |
| 576 | if (!mr->hwmr.num_pbes) |
| 577 | return; |
| 578 | |
| 579 | pbe = (struct ocrdma_pbe *)pbl_tbl->va; |
| 580 | pbe_cnt = 0; |
| 581 | |
| 582 | shift = ilog2(umem->page_size); |
| 583 | |
| 584 | list_for_each_entry(chunk, &umem->chunk_list, list) { |
| 585 | /* get all the dma regions from the chunk. */ |
| 586 | for (i = 0; i < chunk->nmap; i++) { |
| 587 | pages = sg_dma_len(&chunk->page_list[i]) >> shift; |
| 588 | for (pg_cnt = 0; pg_cnt < pages; pg_cnt++) { |
| 589 | /* store the page address in pbe */ |
| 590 | pbe->pa_lo = |
| 591 | cpu_to_le32(sg_dma_address |
| 592 | (&chunk->page_list[i]) + |
| 593 | (umem->page_size * pg_cnt)); |
| 594 | pbe->pa_hi = |
| 595 | cpu_to_le32(upper_32_bits |
| 596 | ((sg_dma_address |
| 597 | (&chunk->page_list[i]) + |
| 598 | umem->page_size * pg_cnt))); |
| 599 | pbe_cnt += 1; |
| 600 | total_num_pbes += 1; |
| 601 | pbe++; |
| 602 | |
| 603 | /* if done building pbes, issue the mbx cmd. */ |
| 604 | if (total_num_pbes == num_pbes) |
| 605 | return; |
| 606 | |
| 607 | /* if the given pbl is full storing the pbes, |
| 608 | * move to next pbl. |
| 609 | */ |
| 610 | if (pbe_cnt == |
| 611 | (mr->hwmr.pbl_size / sizeof(u64))) { |
| 612 | pbl_tbl++; |
| 613 | pbe = (struct ocrdma_pbe *)pbl_tbl->va; |
| 614 | pbe_cnt = 0; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | struct ib_mr *ocrdma_reg_user_mr(struct ib_pd *ibpd, u64 start, u64 len, |
| 622 | u64 usr_addr, int acc, struct ib_udata *udata) |
| 623 | { |
| 624 | int status = -ENOMEM; |
| 625 | struct ocrdma_dev *dev; |
| 626 | struct ocrdma_mr *mr; |
| 627 | struct ocrdma_pd *pd; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 628 | u32 num_pbes; |
| 629 | |
| 630 | pd = get_ocrdma_pd(ibpd); |
| 631 | dev = pd->dev; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 632 | |
| 633 | if (acc & IB_ACCESS_REMOTE_WRITE && !(acc & IB_ACCESS_LOCAL_WRITE)) |
| 634 | return ERR_PTR(-EINVAL); |
| 635 | |
| 636 | mr = kzalloc(sizeof(*mr), GFP_KERNEL); |
| 637 | if (!mr) |
| 638 | return ERR_PTR(status); |
| 639 | mr->hwmr.dev = dev; |
| 640 | mr->umem = ib_umem_get(ibpd->uobject->context, start, len, acc, 0); |
| 641 | if (IS_ERR(mr->umem)) { |
| 642 | status = -EFAULT; |
| 643 | goto umem_err; |
| 644 | } |
| 645 | num_pbes = ib_umem_page_count(mr->umem); |
| 646 | status = ocrdma_get_pbl_info(mr, num_pbes); |
| 647 | if (status) |
| 648 | goto umem_err; |
| 649 | |
| 650 | mr->hwmr.pbe_size = mr->umem->page_size; |
| 651 | mr->hwmr.fbo = mr->umem->offset; |
| 652 | mr->hwmr.va = usr_addr; |
| 653 | mr->hwmr.len = len; |
| 654 | mr->hwmr.remote_wr = (acc & IB_ACCESS_REMOTE_WRITE) ? 1 : 0; |
| 655 | mr->hwmr.remote_rd = (acc & IB_ACCESS_REMOTE_READ) ? 1 : 0; |
| 656 | mr->hwmr.local_wr = (acc & IB_ACCESS_LOCAL_WRITE) ? 1 : 0; |
| 657 | mr->hwmr.local_rd = 1; |
| 658 | mr->hwmr.remote_atomic = (acc & IB_ACCESS_REMOTE_ATOMIC) ? 1 : 0; |
| 659 | status = ocrdma_build_pbl_tbl(dev, &mr->hwmr); |
| 660 | if (status) |
| 661 | goto umem_err; |
| 662 | build_user_pbes(dev, mr, num_pbes); |
| 663 | status = ocrdma_reg_mr(dev, &mr->hwmr, pd->id, acc); |
| 664 | if (status) |
| 665 | goto mbx_err; |
| 666 | mr->pd = pd; |
| 667 | atomic_inc(&pd->use_cnt); |
| 668 | mr->ibmr.lkey = mr->hwmr.lkey; |
| 669 | if (mr->hwmr.remote_wr || mr->hwmr.remote_rd) |
| 670 | mr->ibmr.rkey = mr->hwmr.lkey; |
| 671 | |
| 672 | return &mr->ibmr; |
| 673 | |
| 674 | mbx_err: |
| 675 | ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr); |
| 676 | umem_err: |
| 677 | kfree(mr); |
| 678 | return ERR_PTR(status); |
| 679 | } |
| 680 | |
| 681 | int ocrdma_dereg_mr(struct ib_mr *ib_mr) |
| 682 | { |
| 683 | struct ocrdma_mr *mr = get_ocrdma_mr(ib_mr); |
| 684 | struct ocrdma_dev *dev = mr->hwmr.dev; |
| 685 | int status; |
| 686 | |
| 687 | status = ocrdma_mbx_dealloc_lkey(dev, mr->hwmr.fr_mr, mr->hwmr.lkey); |
| 688 | |
| 689 | if (mr->hwmr.fr_mr == 0) |
| 690 | ocrdma_free_mr_pbl_tbl(dev, &mr->hwmr); |
| 691 | |
| 692 | atomic_dec(&mr->pd->use_cnt); |
| 693 | /* it could be user registered memory. */ |
| 694 | if (mr->umem) |
| 695 | ib_umem_release(mr->umem); |
| 696 | kfree(mr); |
| 697 | return status; |
| 698 | } |
| 699 | |
| 700 | static int ocrdma_copy_cq_uresp(struct ocrdma_cq *cq, struct ib_udata *udata, |
| 701 | struct ib_ucontext *ib_ctx) |
| 702 | { |
| 703 | int status; |
| 704 | struct ocrdma_ucontext *uctx; |
| 705 | struct ocrdma_create_cq_uresp uresp; |
| 706 | |
| 707 | uresp.cq_id = cq->id; |
| 708 | uresp.page_size = cq->len; |
| 709 | uresp.num_pages = 1; |
| 710 | uresp.max_hw_cqe = cq->max_hw_cqe; |
| 711 | uresp.page_addr[0] = cq->pa; |
| 712 | uresp.db_page_addr = cq->dev->nic_info.unmapped_db; |
| 713 | uresp.db_page_size = cq->dev->nic_info.db_page_size; |
| 714 | uresp.phase_change = cq->phase_change ? 1 : 0; |
| 715 | status = ib_copy_to_udata(udata, &uresp, sizeof(uresp)); |
| 716 | if (status) { |
| 717 | ocrdma_err("%s(%d) copy error cqid=0x%x.\n", |
| 718 | __func__, cq->dev->id, cq->id); |
| 719 | goto err; |
| 720 | } |
| 721 | uctx = get_ocrdma_ucontext(ib_ctx); |
| 722 | status = ocrdma_add_mmap(uctx, uresp.db_page_addr, uresp.db_page_size); |
| 723 | if (status) |
| 724 | goto err; |
| 725 | status = ocrdma_add_mmap(uctx, uresp.page_addr[0], uresp.page_size); |
| 726 | if (status) { |
| 727 | ocrdma_del_mmap(uctx, uresp.db_page_addr, uresp.db_page_size); |
| 728 | goto err; |
| 729 | } |
| 730 | cq->ucontext = uctx; |
| 731 | err: |
| 732 | return status; |
| 733 | } |
| 734 | |
| 735 | struct ib_cq *ocrdma_create_cq(struct ib_device *ibdev, int entries, int vector, |
| 736 | struct ib_ucontext *ib_ctx, |
| 737 | struct ib_udata *udata) |
| 738 | { |
| 739 | struct ocrdma_cq *cq; |
| 740 | struct ocrdma_dev *dev = get_ocrdma_dev(ibdev); |
| 741 | int status; |
| 742 | struct ocrdma_create_cq_ureq ureq; |
| 743 | |
| 744 | if (udata) { |
| 745 | if (ib_copy_from_udata(&ureq, udata, sizeof(ureq))) |
| 746 | return ERR_PTR(-EFAULT); |
| 747 | } else |
| 748 | ureq.dpp_cq = 0; |
| 749 | cq = kzalloc(sizeof(*cq), GFP_KERNEL); |
| 750 | if (!cq) |
| 751 | return ERR_PTR(-ENOMEM); |
| 752 | |
| 753 | spin_lock_init(&cq->cq_lock); |
| 754 | spin_lock_init(&cq->comp_handler_lock); |
| 755 | atomic_set(&cq->use_cnt, 0); |
| 756 | INIT_LIST_HEAD(&cq->sq_head); |
| 757 | INIT_LIST_HEAD(&cq->rq_head); |
| 758 | cq->dev = dev; |
| 759 | |
| 760 | status = ocrdma_mbx_create_cq(dev, cq, entries, ureq.dpp_cq); |
| 761 | if (status) { |
| 762 | kfree(cq); |
| 763 | return ERR_PTR(status); |
| 764 | } |
| 765 | if (ib_ctx) { |
| 766 | status = ocrdma_copy_cq_uresp(cq, udata, ib_ctx); |
| 767 | if (status) |
| 768 | goto ctx_err; |
| 769 | } |
| 770 | cq->phase = OCRDMA_CQE_VALID; |
| 771 | cq->arm_needed = true; |
| 772 | dev->cq_tbl[cq->id] = cq; |
| 773 | |
| 774 | return &cq->ibcq; |
| 775 | |
| 776 | ctx_err: |
| 777 | ocrdma_mbx_destroy_cq(dev, cq); |
| 778 | kfree(cq); |
| 779 | return ERR_PTR(status); |
| 780 | } |
| 781 | |
| 782 | int ocrdma_resize_cq(struct ib_cq *ibcq, int new_cnt, |
| 783 | struct ib_udata *udata) |
| 784 | { |
| 785 | int status = 0; |
| 786 | struct ocrdma_cq *cq = get_ocrdma_cq(ibcq); |
| 787 | |
| 788 | if (new_cnt < 1 || new_cnt > cq->max_hw_cqe) { |
| 789 | status = -EINVAL; |
| 790 | return status; |
| 791 | } |
| 792 | ibcq->cqe = new_cnt; |
| 793 | return status; |
| 794 | } |
| 795 | |
| 796 | int ocrdma_destroy_cq(struct ib_cq *ibcq) |
| 797 | { |
| 798 | int status; |
| 799 | struct ocrdma_cq *cq = get_ocrdma_cq(ibcq); |
| 800 | struct ocrdma_dev *dev = cq->dev; |
| 801 | |
| 802 | if (atomic_read(&cq->use_cnt)) |
| 803 | return -EINVAL; |
| 804 | |
| 805 | status = ocrdma_mbx_destroy_cq(dev, cq); |
| 806 | |
| 807 | if (cq->ucontext) { |
| 808 | ocrdma_del_mmap(cq->ucontext, (u64) cq->pa, cq->len); |
| 809 | ocrdma_del_mmap(cq->ucontext, dev->nic_info.unmapped_db, |
| 810 | dev->nic_info.db_page_size); |
| 811 | } |
| 812 | dev->cq_tbl[cq->id] = NULL; |
| 813 | |
| 814 | kfree(cq); |
| 815 | return status; |
| 816 | } |
| 817 | |
| 818 | static int ocrdma_add_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp) |
| 819 | { |
| 820 | int status = -EINVAL; |
| 821 | |
| 822 | if (qp->id < OCRDMA_MAX_QP && dev->qp_tbl[qp->id] == NULL) { |
| 823 | dev->qp_tbl[qp->id] = qp; |
| 824 | status = 0; |
| 825 | } |
| 826 | return status; |
| 827 | } |
| 828 | |
| 829 | static void ocrdma_del_qpn_map(struct ocrdma_dev *dev, struct ocrdma_qp *qp) |
| 830 | { |
| 831 | dev->qp_tbl[qp->id] = NULL; |
| 832 | } |
| 833 | |
| 834 | static int ocrdma_check_qp_params(struct ib_pd *ibpd, struct ocrdma_dev *dev, |
| 835 | struct ib_qp_init_attr *attrs) |
| 836 | { |
| 837 | if (attrs->qp_type != IB_QPT_GSI && |
| 838 | attrs->qp_type != IB_QPT_RC && |
| 839 | attrs->qp_type != IB_QPT_UD) { |
| 840 | ocrdma_err("%s(%d) unsupported qp type=0x%x requested\n", |
| 841 | __func__, dev->id, attrs->qp_type); |
| 842 | return -EINVAL; |
| 843 | } |
| 844 | if (attrs->cap.max_send_wr > dev->attr.max_wqe) { |
| 845 | ocrdma_err("%s(%d) unsupported send_wr=0x%x requested\n", |
| 846 | __func__, dev->id, attrs->cap.max_send_wr); |
| 847 | ocrdma_err("%s(%d) supported send_wr=0x%x\n", |
| 848 | __func__, dev->id, dev->attr.max_wqe); |
| 849 | return -EINVAL; |
| 850 | } |
| 851 | if (!attrs->srq && (attrs->cap.max_recv_wr > dev->attr.max_rqe)) { |
| 852 | ocrdma_err("%s(%d) unsupported recv_wr=0x%x requested\n", |
| 853 | __func__, dev->id, attrs->cap.max_recv_wr); |
| 854 | ocrdma_err("%s(%d) supported recv_wr=0x%x\n", |
| 855 | __func__, dev->id, dev->attr.max_rqe); |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | if (attrs->cap.max_inline_data > dev->attr.max_inline_data) { |
| 859 | ocrdma_err("%s(%d) unsupported inline data size=0x%x" |
| 860 | " requested\n", __func__, dev->id, |
| 861 | attrs->cap.max_inline_data); |
| 862 | ocrdma_err("%s(%d) supported inline data size=0x%x\n", |
| 863 | __func__, dev->id, dev->attr.max_inline_data); |
| 864 | return -EINVAL; |
| 865 | } |
| 866 | if (attrs->cap.max_send_sge > dev->attr.max_send_sge) { |
| 867 | ocrdma_err("%s(%d) unsupported send_sge=0x%x requested\n", |
| 868 | __func__, dev->id, attrs->cap.max_send_sge); |
| 869 | ocrdma_err("%s(%d) supported send_sge=0x%x\n", |
| 870 | __func__, dev->id, dev->attr.max_send_sge); |
| 871 | return -EINVAL; |
| 872 | } |
| 873 | if (attrs->cap.max_recv_sge > dev->attr.max_recv_sge) { |
| 874 | ocrdma_err("%s(%d) unsupported recv_sge=0x%x requested\n", |
| 875 | __func__, dev->id, attrs->cap.max_recv_sge); |
| 876 | ocrdma_err("%s(%d) supported recv_sge=0x%x\n", |
| 877 | __func__, dev->id, dev->attr.max_recv_sge); |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | /* unprivileged user space cannot create special QP */ |
| 881 | if (ibpd->uobject && attrs->qp_type == IB_QPT_GSI) { |
| 882 | ocrdma_err |
| 883 | ("%s(%d) Userspace can't create special QPs of type=0x%x\n", |
| 884 | __func__, dev->id, attrs->qp_type); |
| 885 | return -EINVAL; |
| 886 | } |
| 887 | /* allow creating only one GSI type of QP */ |
| 888 | if (attrs->qp_type == IB_QPT_GSI && dev->gsi_qp_created) { |
| 889 | ocrdma_err("%s(%d) GSI special QPs already created.\n", |
| 890 | __func__, dev->id); |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | /* verify consumer QPs are not trying to use GSI QP's CQ */ |
| 894 | if ((attrs->qp_type != IB_QPT_GSI) && (dev->gsi_qp_created)) { |
| 895 | if ((dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq)) || |
| 896 | (dev->gsi_sqcq == get_ocrdma_cq(attrs->send_cq))) { |
| 897 | ocrdma_err("%s(%d) Consumer QP cannot use GSI CQs.\n", |
| 898 | __func__, dev->id); |
| 899 | return -EINVAL; |
| 900 | } |
| 901 | } |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static int ocrdma_copy_qp_uresp(struct ocrdma_qp *qp, |
| 906 | struct ib_udata *udata, int dpp_offset, |
| 907 | int dpp_credit_lmt, int srq) |
| 908 | { |
| 909 | int status = 0; |
| 910 | u64 usr_db; |
| 911 | struct ocrdma_create_qp_uresp uresp; |
| 912 | struct ocrdma_dev *dev = qp->dev; |
| 913 | struct ocrdma_pd *pd = qp->pd; |
| 914 | |
| 915 | memset(&uresp, 0, sizeof(uresp)); |
| 916 | usr_db = dev->nic_info.unmapped_db + |
| 917 | (pd->id * dev->nic_info.db_page_size); |
| 918 | uresp.qp_id = qp->id; |
| 919 | uresp.sq_dbid = qp->sq.dbid; |
| 920 | uresp.num_sq_pages = 1; |
| 921 | uresp.sq_page_size = qp->sq.len; |
| 922 | uresp.sq_page_addr[0] = qp->sq.pa; |
| 923 | uresp.num_wqe_allocated = qp->sq.max_cnt; |
| 924 | if (!srq) { |
| 925 | uresp.rq_dbid = qp->rq.dbid; |
| 926 | uresp.num_rq_pages = 1; |
| 927 | uresp.rq_page_size = qp->rq.len; |
| 928 | uresp.rq_page_addr[0] = qp->rq.pa; |
| 929 | uresp.num_rqe_allocated = qp->rq.max_cnt; |
| 930 | } |
| 931 | uresp.db_page_addr = usr_db; |
| 932 | uresp.db_page_size = dev->nic_info.db_page_size; |
| 933 | if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) { |
| 934 | uresp.db_sq_offset = OCRDMA_DB_GEN2_SQ_OFFSET; |
| 935 | uresp.db_rq_offset = ((qp->id & 0xFFFF) < 128) ? |
| 936 | OCRDMA_DB_GEN2_RQ1_OFFSET : OCRDMA_DB_GEN2_RQ2_OFFSET; |
| 937 | uresp.db_shift = (qp->id < 128) ? 24 : 16; |
| 938 | } else { |
| 939 | uresp.db_sq_offset = OCRDMA_DB_SQ_OFFSET; |
| 940 | uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET; |
| 941 | uresp.db_shift = 16; |
| 942 | } |
| 943 | uresp.free_wqe_delta = qp->sq.free_delta; |
| 944 | uresp.free_rqe_delta = qp->rq.free_delta; |
| 945 | |
| 946 | if (qp->dpp_enabled) { |
| 947 | uresp.dpp_credit = dpp_credit_lmt; |
| 948 | uresp.dpp_offset = dpp_offset; |
| 949 | } |
| 950 | status = ib_copy_to_udata(udata, &uresp, sizeof(uresp)); |
| 951 | if (status) { |
| 952 | ocrdma_err("%s(%d) user copy error.\n", __func__, dev->id); |
| 953 | goto err; |
| 954 | } |
| 955 | status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0], |
| 956 | uresp.sq_page_size); |
| 957 | if (status) |
| 958 | goto err; |
| 959 | |
| 960 | if (!srq) { |
| 961 | status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0], |
| 962 | uresp.rq_page_size); |
| 963 | if (status) |
| 964 | goto rq_map_err; |
| 965 | } |
| 966 | return status; |
| 967 | rq_map_err: |
| 968 | ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size); |
| 969 | err: |
| 970 | return status; |
| 971 | } |
| 972 | |
| 973 | static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp, |
| 974 | struct ocrdma_pd *pd) |
| 975 | { |
| 976 | if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) { |
| 977 | qp->sq_db = dev->nic_info.db + |
| 978 | (pd->id * dev->nic_info.db_page_size) + |
| 979 | OCRDMA_DB_GEN2_SQ_OFFSET; |
| 980 | qp->rq_db = dev->nic_info.db + |
| 981 | (pd->id * dev->nic_info.db_page_size) + |
| 982 | ((qp->id < 128) ? |
| 983 | OCRDMA_DB_GEN2_RQ1_OFFSET : OCRDMA_DB_GEN2_RQ2_OFFSET); |
| 984 | } else { |
| 985 | qp->sq_db = dev->nic_info.db + |
| 986 | (pd->id * dev->nic_info.db_page_size) + |
| 987 | OCRDMA_DB_SQ_OFFSET; |
| 988 | qp->rq_db = dev->nic_info.db + |
| 989 | (pd->id * dev->nic_info.db_page_size) + |
| 990 | OCRDMA_DB_RQ_OFFSET; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp) |
| 995 | { |
| 996 | qp->wqe_wr_id_tbl = |
| 997 | kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt, |
| 998 | GFP_KERNEL); |
| 999 | if (qp->wqe_wr_id_tbl == NULL) |
| 1000 | return -ENOMEM; |
| 1001 | qp->rqe_wr_id_tbl = |
| 1002 | kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL); |
| 1003 | if (qp->rqe_wr_id_tbl == NULL) |
| 1004 | return -ENOMEM; |
| 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp, |
| 1010 | struct ocrdma_pd *pd, |
| 1011 | struct ib_qp_init_attr *attrs) |
| 1012 | { |
| 1013 | qp->pd = pd; |
| 1014 | spin_lock_init(&qp->q_lock); |
| 1015 | INIT_LIST_HEAD(&qp->sq_entry); |
| 1016 | INIT_LIST_HEAD(&qp->rq_entry); |
| 1017 | |
| 1018 | qp->qp_type = attrs->qp_type; |
| 1019 | qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR; |
| 1020 | qp->max_inline_data = attrs->cap.max_inline_data; |
| 1021 | qp->sq.max_sges = attrs->cap.max_send_sge; |
| 1022 | qp->rq.max_sges = attrs->cap.max_recv_sge; |
| 1023 | qp->state = OCRDMA_QPS_RST; |
| 1024 | } |
| 1025 | |
| 1026 | static void ocrdma_set_qp_use_cnt(struct ocrdma_qp *qp, struct ocrdma_pd *pd) |
| 1027 | { |
| 1028 | atomic_inc(&pd->use_cnt); |
| 1029 | atomic_inc(&qp->sq_cq->use_cnt); |
| 1030 | atomic_inc(&qp->rq_cq->use_cnt); |
| 1031 | if (qp->srq) |
| 1032 | atomic_inc(&qp->srq->use_cnt); |
| 1033 | qp->ibqp.qp_num = qp->id; |
| 1034 | } |
| 1035 | |
| 1036 | static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev, |
| 1037 | struct ib_qp_init_attr *attrs) |
| 1038 | { |
| 1039 | if (attrs->qp_type == IB_QPT_GSI) { |
| 1040 | dev->gsi_qp_created = 1; |
| 1041 | dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq); |
| 1042 | dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd, |
| 1047 | struct ib_qp_init_attr *attrs, |
| 1048 | struct ib_udata *udata) |
| 1049 | { |
| 1050 | int status; |
| 1051 | struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); |
| 1052 | struct ocrdma_qp *qp; |
| 1053 | struct ocrdma_dev *dev = pd->dev; |
| 1054 | struct ocrdma_create_qp_ureq ureq; |
| 1055 | u16 dpp_credit_lmt, dpp_offset; |
| 1056 | |
| 1057 | status = ocrdma_check_qp_params(ibpd, dev, attrs); |
| 1058 | if (status) |
| 1059 | goto gen_err; |
| 1060 | |
| 1061 | memset(&ureq, 0, sizeof(ureq)); |
| 1062 | if (udata) { |
| 1063 | if (ib_copy_from_udata(&ureq, udata, sizeof(ureq))) |
| 1064 | return ERR_PTR(-EFAULT); |
| 1065 | } |
| 1066 | qp = kzalloc(sizeof(*qp), GFP_KERNEL); |
| 1067 | if (!qp) { |
| 1068 | status = -ENOMEM; |
| 1069 | goto gen_err; |
| 1070 | } |
| 1071 | qp->dev = dev; |
| 1072 | ocrdma_set_qp_init_params(qp, pd, attrs); |
| 1073 | |
| 1074 | mutex_lock(&dev->dev_lock); |
| 1075 | status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq, |
| 1076 | ureq.dpp_cq_id, |
| 1077 | &dpp_offset, &dpp_credit_lmt); |
| 1078 | if (status) |
| 1079 | goto mbx_err; |
| 1080 | |
| 1081 | /* user space QP's wr_id table are managed in library */ |
| 1082 | if (udata == NULL) { |
| 1083 | qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 | |
| 1084 | OCRDMA_QP_FAST_REG); |
| 1085 | status = ocrdma_alloc_wr_id_tbl(qp); |
| 1086 | if (status) |
| 1087 | goto map_err; |
| 1088 | } |
| 1089 | |
| 1090 | status = ocrdma_add_qpn_map(dev, qp); |
| 1091 | if (status) |
| 1092 | goto map_err; |
| 1093 | ocrdma_set_qp_db(dev, qp, pd); |
| 1094 | if (udata) { |
| 1095 | status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset, |
| 1096 | dpp_credit_lmt, |
| 1097 | (attrs->srq != NULL)); |
| 1098 | if (status) |
| 1099 | goto cpy_err; |
| 1100 | } |
| 1101 | ocrdma_store_gsi_qp_cq(dev, attrs); |
| 1102 | ocrdma_set_qp_use_cnt(qp, pd); |
| 1103 | mutex_unlock(&dev->dev_lock); |
| 1104 | return &qp->ibqp; |
| 1105 | |
| 1106 | cpy_err: |
| 1107 | ocrdma_del_qpn_map(dev, qp); |
| 1108 | map_err: |
| 1109 | ocrdma_mbx_destroy_qp(dev, qp); |
| 1110 | mbx_err: |
| 1111 | mutex_unlock(&dev->dev_lock); |
| 1112 | kfree(qp->wqe_wr_id_tbl); |
| 1113 | kfree(qp->rqe_wr_id_tbl); |
| 1114 | kfree(qp); |
| 1115 | ocrdma_err("%s(%d) error=%d\n", __func__, dev->id, status); |
| 1116 | gen_err: |
| 1117 | return ERR_PTR(status); |
| 1118 | } |
| 1119 | |
| 1120 | int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 1121 | int attr_mask) |
| 1122 | { |
| 1123 | int status = 0; |
| 1124 | struct ocrdma_qp *qp; |
| 1125 | struct ocrdma_dev *dev; |
| 1126 | enum ib_qp_state old_qps; |
| 1127 | |
| 1128 | qp = get_ocrdma_qp(ibqp); |
| 1129 | dev = qp->dev; |
| 1130 | if (attr_mask & IB_QP_STATE) |
| 1131 | status = ocrdma_qp_state_machine(qp, attr->qp_state, &old_qps); |
| 1132 | /* if new and previous states are same hw doesn't need to |
| 1133 | * know about it. |
| 1134 | */ |
| 1135 | if (status < 0) |
| 1136 | return status; |
| 1137 | status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask, old_qps); |
| 1138 | return status; |
| 1139 | } |
| 1140 | |
| 1141 | int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 1142 | int attr_mask, struct ib_udata *udata) |
| 1143 | { |
| 1144 | unsigned long flags; |
| 1145 | int status = -EINVAL; |
| 1146 | struct ocrdma_qp *qp; |
| 1147 | struct ocrdma_dev *dev; |
| 1148 | enum ib_qp_state old_qps, new_qps; |
| 1149 | |
| 1150 | qp = get_ocrdma_qp(ibqp); |
| 1151 | dev = qp->dev; |
| 1152 | |
| 1153 | /* syncronize with multiple context trying to change, retrive qps */ |
| 1154 | mutex_lock(&dev->dev_lock); |
| 1155 | /* syncronize with wqe, rqe posting and cqe processing contexts */ |
| 1156 | spin_lock_irqsave(&qp->q_lock, flags); |
| 1157 | old_qps = get_ibqp_state(qp->state); |
| 1158 | if (attr_mask & IB_QP_STATE) |
| 1159 | new_qps = attr->qp_state; |
| 1160 | else |
| 1161 | new_qps = old_qps; |
| 1162 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1163 | |
| 1164 | if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask)) { |
| 1165 | ocrdma_err("%s(%d) invalid attribute mask=0x%x specified for " |
| 1166 | "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n", |
| 1167 | __func__, dev->id, attr_mask, qp->id, ibqp->qp_type, |
| 1168 | old_qps, new_qps); |
| 1169 | goto param_err; |
| 1170 | } |
| 1171 | |
| 1172 | status = _ocrdma_modify_qp(ibqp, attr, attr_mask); |
| 1173 | if (status > 0) |
| 1174 | status = 0; |
| 1175 | param_err: |
| 1176 | mutex_unlock(&dev->dev_lock); |
| 1177 | return status; |
| 1178 | } |
| 1179 | |
| 1180 | static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu) |
| 1181 | { |
| 1182 | switch (mtu) { |
| 1183 | case 256: |
| 1184 | return IB_MTU_256; |
| 1185 | case 512: |
| 1186 | return IB_MTU_512; |
| 1187 | case 1024: |
| 1188 | return IB_MTU_1024; |
| 1189 | case 2048: |
| 1190 | return IB_MTU_2048; |
| 1191 | case 4096: |
| 1192 | return IB_MTU_4096; |
| 1193 | default: |
| 1194 | return IB_MTU_1024; |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags) |
| 1199 | { |
| 1200 | int ib_qp_acc_flags = 0; |
| 1201 | |
| 1202 | if (qp_cap_flags & OCRDMA_QP_INB_WR) |
| 1203 | ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE; |
| 1204 | if (qp_cap_flags & OCRDMA_QP_INB_RD) |
| 1205 | ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE; |
| 1206 | return ib_qp_acc_flags; |
| 1207 | } |
| 1208 | |
| 1209 | int ocrdma_query_qp(struct ib_qp *ibqp, |
| 1210 | struct ib_qp_attr *qp_attr, |
| 1211 | int attr_mask, struct ib_qp_init_attr *qp_init_attr) |
| 1212 | { |
| 1213 | int status; |
| 1214 | u32 qp_state; |
| 1215 | struct ocrdma_qp_params params; |
| 1216 | struct ocrdma_qp *qp = get_ocrdma_qp(ibqp); |
| 1217 | struct ocrdma_dev *dev = qp->dev; |
| 1218 | |
| 1219 | memset(¶ms, 0, sizeof(params)); |
| 1220 | mutex_lock(&dev->dev_lock); |
| 1221 | status = ocrdma_mbx_query_qp(dev, qp, ¶ms); |
| 1222 | mutex_unlock(&dev->dev_lock); |
| 1223 | if (status) |
| 1224 | goto mbx_err; |
| 1225 | qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT); |
| 1226 | qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT); |
| 1227 | qp_attr->path_mtu = |
| 1228 | ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx & |
| 1229 | OCRDMA_QP_PARAMS_PATH_MTU_MASK) >> |
| 1230 | OCRDMA_QP_PARAMS_PATH_MTU_SHIFT; |
| 1231 | qp_attr->path_mig_state = IB_MIG_MIGRATED; |
| 1232 | qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK; |
| 1233 | qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK; |
| 1234 | qp_attr->dest_qp_num = |
| 1235 | params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK; |
| 1236 | |
| 1237 | qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags); |
| 1238 | qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1; |
| 1239 | qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1; |
| 1240 | qp_attr->cap.max_send_sge = qp->sq.max_sges; |
| 1241 | qp_attr->cap.max_recv_sge = qp->rq.max_sges; |
| 1242 | qp_attr->cap.max_inline_data = dev->attr.max_inline_data; |
| 1243 | qp_init_attr->cap = qp_attr->cap; |
| 1244 | memcpy(&qp_attr->ah_attr.grh.dgid, ¶ms.dgid[0], |
| 1245 | sizeof(params.dgid)); |
| 1246 | qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl & |
| 1247 | OCRDMA_QP_PARAMS_FLOW_LABEL_MASK; |
| 1248 | qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx; |
| 1249 | qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn & |
| 1250 | OCRDMA_QP_PARAMS_HOP_LMT_MASK) >> |
| 1251 | OCRDMA_QP_PARAMS_HOP_LMT_SHIFT; |
| 1252 | qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn & |
| 1253 | OCRDMA_QP_PARAMS_SQ_PSN_MASK) >> |
| 1254 | OCRDMA_QP_PARAMS_TCLASS_SHIFT; |
| 1255 | |
| 1256 | qp_attr->ah_attr.ah_flags = IB_AH_GRH; |
| 1257 | qp_attr->ah_attr.port_num = 1; |
| 1258 | qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl & |
| 1259 | OCRDMA_QP_PARAMS_SL_MASK) >> |
| 1260 | OCRDMA_QP_PARAMS_SL_SHIFT; |
| 1261 | qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn & |
| 1262 | OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >> |
| 1263 | OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT; |
| 1264 | qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn & |
| 1265 | OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >> |
| 1266 | OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT; |
| 1267 | qp_attr->retry_cnt = |
| 1268 | (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >> |
| 1269 | OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT; |
| 1270 | qp_attr->min_rnr_timer = 0; |
| 1271 | qp_attr->pkey_index = 0; |
| 1272 | qp_attr->port_num = 1; |
| 1273 | qp_attr->ah_attr.src_path_bits = 0; |
| 1274 | qp_attr->ah_attr.static_rate = 0; |
| 1275 | qp_attr->alt_pkey_index = 0; |
| 1276 | qp_attr->alt_port_num = 0; |
| 1277 | qp_attr->alt_timeout = 0; |
| 1278 | memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr)); |
| 1279 | qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >> |
| 1280 | OCRDMA_QP_PARAMS_STATE_SHIFT; |
| 1281 | qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0; |
| 1282 | qp_attr->max_dest_rd_atomic = |
| 1283 | params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT; |
| 1284 | qp_attr->max_rd_atomic = |
| 1285 | params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK; |
| 1286 | qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags & |
| 1287 | OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0; |
| 1288 | mbx_err: |
| 1289 | return status; |
| 1290 | } |
| 1291 | |
| 1292 | static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx) |
| 1293 | { |
| 1294 | int i = idx / 32; |
| 1295 | unsigned int mask = (1 << (idx % 32)); |
| 1296 | |
| 1297 | if (srq->idx_bit_fields[i] & mask) |
| 1298 | srq->idx_bit_fields[i] &= ~mask; |
| 1299 | else |
| 1300 | srq->idx_bit_fields[i] |= mask; |
| 1301 | } |
| 1302 | |
| 1303 | static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q) |
| 1304 | { |
| 1305 | int free_cnt; |
| 1306 | if (q->head >= q->tail) |
| 1307 | free_cnt = (q->max_cnt - q->head) + q->tail; |
| 1308 | else |
| 1309 | free_cnt = q->tail - q->head; |
| 1310 | if (q->free_delta) |
| 1311 | free_cnt -= q->free_delta; |
| 1312 | return free_cnt; |
| 1313 | } |
| 1314 | |
| 1315 | static int is_hw_sq_empty(struct ocrdma_qp *qp) |
| 1316 | { |
| 1317 | return (qp->sq.tail == qp->sq.head && |
| 1318 | ocrdma_hwq_free_cnt(&qp->sq) ? 1 : 0); |
| 1319 | } |
| 1320 | |
| 1321 | static int is_hw_rq_empty(struct ocrdma_qp *qp) |
| 1322 | { |
| 1323 | return (qp->rq.tail == qp->rq.head) ? 1 : 0; |
| 1324 | } |
| 1325 | |
| 1326 | static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q) |
| 1327 | { |
| 1328 | return q->va + (q->head * q->entry_size); |
| 1329 | } |
| 1330 | |
| 1331 | static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q, |
| 1332 | u32 idx) |
| 1333 | { |
| 1334 | return q->va + (idx * q->entry_size); |
| 1335 | } |
| 1336 | |
| 1337 | static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q) |
| 1338 | { |
| 1339 | q->head = (q->head + 1) & q->max_wqe_idx; |
| 1340 | } |
| 1341 | |
| 1342 | static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q) |
| 1343 | { |
| 1344 | q->tail = (q->tail + 1) & q->max_wqe_idx; |
| 1345 | } |
| 1346 | |
| 1347 | /* discard the cqe for a given QP */ |
| 1348 | static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq) |
| 1349 | { |
| 1350 | unsigned long cq_flags; |
| 1351 | unsigned long flags; |
| 1352 | int discard_cnt = 0; |
| 1353 | u32 cur_getp, stop_getp; |
| 1354 | struct ocrdma_cqe *cqe; |
| 1355 | u32 qpn = 0; |
| 1356 | |
| 1357 | spin_lock_irqsave(&cq->cq_lock, cq_flags); |
| 1358 | |
| 1359 | /* traverse through the CQEs in the hw CQ, |
| 1360 | * find the matching CQE for a given qp, |
| 1361 | * mark the matching one discarded by clearing qpn. |
| 1362 | * ring the doorbell in the poll_cq() as |
| 1363 | * we don't complete out of order cqe. |
| 1364 | */ |
| 1365 | |
| 1366 | cur_getp = cq->getp; |
| 1367 | /* find upto when do we reap the cq. */ |
| 1368 | stop_getp = cur_getp; |
| 1369 | do { |
| 1370 | if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp))) |
| 1371 | break; |
| 1372 | |
| 1373 | cqe = cq->va + cur_getp; |
| 1374 | /* if (a) done reaping whole hw cq, or |
| 1375 | * (b) qp_xq becomes empty. |
| 1376 | * then exit |
| 1377 | */ |
| 1378 | qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK; |
| 1379 | /* if previously discarded cqe found, skip that too. */ |
| 1380 | /* check for matching qp */ |
| 1381 | if (qpn == 0 || qpn != qp->id) |
| 1382 | goto skip_cqe; |
| 1383 | |
| 1384 | /* mark cqe discarded so that it is not picked up later |
| 1385 | * in the poll_cq(). |
| 1386 | */ |
| 1387 | discard_cnt += 1; |
| 1388 | cqe->cmn.qpn = 0; |
| 1389 | if (is_cqe_for_sq(cqe)) |
| 1390 | ocrdma_hwq_inc_tail(&qp->sq); |
| 1391 | else { |
| 1392 | if (qp->srq) { |
| 1393 | spin_lock_irqsave(&qp->srq->q_lock, flags); |
| 1394 | ocrdma_hwq_inc_tail(&qp->srq->rq); |
| 1395 | ocrdma_srq_toggle_bit(qp->srq, cur_getp); |
| 1396 | spin_unlock_irqrestore(&qp->srq->q_lock, flags); |
| 1397 | |
| 1398 | } else |
| 1399 | ocrdma_hwq_inc_tail(&qp->rq); |
| 1400 | } |
| 1401 | skip_cqe: |
| 1402 | cur_getp = (cur_getp + 1) % cq->max_hw_cqe; |
| 1403 | } while (cur_getp != stop_getp); |
| 1404 | spin_unlock_irqrestore(&cq->cq_lock, cq_flags); |
| 1405 | } |
| 1406 | |
| 1407 | static void ocrdma_del_flush_qp(struct ocrdma_qp *qp) |
| 1408 | { |
| 1409 | int found = false; |
| 1410 | unsigned long flags; |
| 1411 | struct ocrdma_dev *dev = qp->dev; |
| 1412 | /* sync with any active CQ poll */ |
| 1413 | |
| 1414 | spin_lock_irqsave(&dev->flush_q_lock, flags); |
| 1415 | found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp); |
| 1416 | if (found) |
| 1417 | list_del(&qp->sq_entry); |
| 1418 | if (!qp->srq) { |
| 1419 | found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp); |
| 1420 | if (found) |
| 1421 | list_del(&qp->rq_entry); |
| 1422 | } |
| 1423 | spin_unlock_irqrestore(&dev->flush_q_lock, flags); |
| 1424 | } |
| 1425 | |
| 1426 | int ocrdma_destroy_qp(struct ib_qp *ibqp) |
| 1427 | { |
| 1428 | int status; |
| 1429 | struct ocrdma_pd *pd; |
| 1430 | struct ocrdma_qp *qp; |
| 1431 | struct ocrdma_dev *dev; |
| 1432 | struct ib_qp_attr attrs; |
| 1433 | int attr_mask = IB_QP_STATE; |
| 1434 | unsigned long wq_flags = 0, rq_flags = 0; |
| 1435 | |
| 1436 | qp = get_ocrdma_qp(ibqp); |
| 1437 | dev = qp->dev; |
| 1438 | |
| 1439 | attrs.qp_state = IB_QPS_ERR; |
| 1440 | pd = qp->pd; |
| 1441 | |
| 1442 | /* change the QP state to ERROR */ |
| 1443 | _ocrdma_modify_qp(ibqp, &attrs, attr_mask); |
| 1444 | |
| 1445 | /* ensure that CQEs for newly created QP (whose id may be same with |
| 1446 | * one which just getting destroyed are same), dont get |
| 1447 | * discarded until the old CQEs are discarded. |
| 1448 | */ |
| 1449 | mutex_lock(&dev->dev_lock); |
| 1450 | status = ocrdma_mbx_destroy_qp(dev, qp); |
| 1451 | |
| 1452 | /* |
| 1453 | * acquire CQ lock while destroy is in progress, in order to |
| 1454 | * protect against proessing in-flight CQEs for this QP. |
| 1455 | */ |
| 1456 | spin_lock_irqsave(&qp->sq_cq->cq_lock, wq_flags); |
| 1457 | if (qp->rq_cq && (qp->rq_cq != qp->sq_cq)) |
| 1458 | spin_lock_irqsave(&qp->rq_cq->cq_lock, rq_flags); |
| 1459 | |
| 1460 | ocrdma_del_qpn_map(dev, qp); |
| 1461 | |
| 1462 | if (qp->rq_cq && (qp->rq_cq != qp->sq_cq)) |
| 1463 | spin_unlock_irqrestore(&qp->rq_cq->cq_lock, rq_flags); |
| 1464 | spin_unlock_irqrestore(&qp->sq_cq->cq_lock, wq_flags); |
| 1465 | |
| 1466 | if (!pd->uctx) { |
| 1467 | ocrdma_discard_cqes(qp, qp->sq_cq); |
| 1468 | ocrdma_discard_cqes(qp, qp->rq_cq); |
| 1469 | } |
| 1470 | mutex_unlock(&dev->dev_lock); |
| 1471 | |
| 1472 | if (pd->uctx) { |
| 1473 | ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa, qp->sq.len); |
| 1474 | if (!qp->srq) |
| 1475 | ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa, qp->rq.len); |
| 1476 | } |
| 1477 | |
| 1478 | ocrdma_del_flush_qp(qp); |
| 1479 | |
| 1480 | atomic_dec(&qp->pd->use_cnt); |
| 1481 | atomic_dec(&qp->sq_cq->use_cnt); |
| 1482 | atomic_dec(&qp->rq_cq->use_cnt); |
| 1483 | if (qp->srq) |
| 1484 | atomic_dec(&qp->srq->use_cnt); |
| 1485 | kfree(qp->wqe_wr_id_tbl); |
| 1486 | kfree(qp->rqe_wr_id_tbl); |
| 1487 | kfree(qp); |
| 1488 | return status; |
| 1489 | } |
| 1490 | |
| 1491 | static int ocrdma_copy_srq_uresp(struct ocrdma_srq *srq, struct ib_udata *udata) |
| 1492 | { |
| 1493 | int status; |
| 1494 | struct ocrdma_create_srq_uresp uresp; |
| 1495 | |
| 1496 | uresp.rq_dbid = srq->rq.dbid; |
| 1497 | uresp.num_rq_pages = 1; |
| 1498 | uresp.rq_page_addr[0] = srq->rq.pa; |
| 1499 | uresp.rq_page_size = srq->rq.len; |
| 1500 | uresp.db_page_addr = srq->dev->nic_info.unmapped_db + |
| 1501 | (srq->pd->id * srq->dev->nic_info.db_page_size); |
| 1502 | uresp.db_page_size = srq->dev->nic_info.db_page_size; |
| 1503 | uresp.num_rqe_allocated = srq->rq.max_cnt; |
| 1504 | uresp.free_rqe_delta = 1; |
| 1505 | if (srq->dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) { |
| 1506 | uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ1_OFFSET; |
| 1507 | uresp.db_shift = 24; |
| 1508 | } else { |
| 1509 | uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET; |
| 1510 | uresp.db_shift = 16; |
| 1511 | } |
| 1512 | |
| 1513 | status = ib_copy_to_udata(udata, &uresp, sizeof(uresp)); |
| 1514 | if (status) |
| 1515 | return status; |
| 1516 | status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0], |
| 1517 | uresp.rq_page_size); |
| 1518 | if (status) |
| 1519 | return status; |
| 1520 | return status; |
| 1521 | } |
| 1522 | |
| 1523 | struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd, |
| 1524 | struct ib_srq_init_attr *init_attr, |
| 1525 | struct ib_udata *udata) |
| 1526 | { |
| 1527 | int status = -ENOMEM; |
| 1528 | struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); |
| 1529 | struct ocrdma_dev *dev = pd->dev; |
| 1530 | struct ocrdma_srq *srq; |
| 1531 | |
| 1532 | if (init_attr->attr.max_sge > dev->attr.max_recv_sge) |
| 1533 | return ERR_PTR(-EINVAL); |
| 1534 | if (init_attr->attr.max_wr > dev->attr.max_rqe) |
| 1535 | return ERR_PTR(-EINVAL); |
| 1536 | |
| 1537 | srq = kzalloc(sizeof(*srq), GFP_KERNEL); |
| 1538 | if (!srq) |
| 1539 | return ERR_PTR(status); |
| 1540 | |
| 1541 | spin_lock_init(&srq->q_lock); |
| 1542 | srq->dev = dev; |
| 1543 | srq->pd = pd; |
| 1544 | srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size); |
| 1545 | status = ocrdma_mbx_create_srq(srq, init_attr, pd); |
| 1546 | if (status) |
| 1547 | goto err; |
| 1548 | |
| 1549 | if (udata == NULL) { |
| 1550 | srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt, |
| 1551 | GFP_KERNEL); |
| 1552 | if (srq->rqe_wr_id_tbl == NULL) |
| 1553 | goto arm_err; |
| 1554 | |
| 1555 | srq->bit_fields_len = (srq->rq.max_cnt / 32) + |
| 1556 | (srq->rq.max_cnt % 32 ? 1 : 0); |
| 1557 | srq->idx_bit_fields = |
| 1558 | kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL); |
| 1559 | if (srq->idx_bit_fields == NULL) |
| 1560 | goto arm_err; |
| 1561 | memset(srq->idx_bit_fields, 0xff, |
| 1562 | srq->bit_fields_len * sizeof(u32)); |
| 1563 | } |
| 1564 | |
| 1565 | if (init_attr->attr.srq_limit) { |
| 1566 | status = ocrdma_mbx_modify_srq(srq, &init_attr->attr); |
| 1567 | if (status) |
| 1568 | goto arm_err; |
| 1569 | } |
| 1570 | |
| 1571 | atomic_set(&srq->use_cnt, 0); |
| 1572 | if (udata) { |
| 1573 | status = ocrdma_copy_srq_uresp(srq, udata); |
| 1574 | if (status) |
| 1575 | goto arm_err; |
| 1576 | } |
| 1577 | |
| 1578 | atomic_inc(&pd->use_cnt); |
| 1579 | return &srq->ibsrq; |
| 1580 | |
| 1581 | arm_err: |
| 1582 | ocrdma_mbx_destroy_srq(dev, srq); |
| 1583 | err: |
| 1584 | kfree(srq->rqe_wr_id_tbl); |
| 1585 | kfree(srq->idx_bit_fields); |
| 1586 | kfree(srq); |
| 1587 | return ERR_PTR(status); |
| 1588 | } |
| 1589 | |
| 1590 | int ocrdma_modify_srq(struct ib_srq *ibsrq, |
| 1591 | struct ib_srq_attr *srq_attr, |
| 1592 | enum ib_srq_attr_mask srq_attr_mask, |
| 1593 | struct ib_udata *udata) |
| 1594 | { |
| 1595 | int status = 0; |
| 1596 | struct ocrdma_srq *srq; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1597 | |
| 1598 | srq = get_ocrdma_srq(ibsrq); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1599 | if (srq_attr_mask & IB_SRQ_MAX_WR) |
| 1600 | status = -EINVAL; |
| 1601 | else |
| 1602 | status = ocrdma_mbx_modify_srq(srq, srq_attr); |
| 1603 | return status; |
| 1604 | } |
| 1605 | |
| 1606 | int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) |
| 1607 | { |
| 1608 | int status; |
| 1609 | struct ocrdma_srq *srq; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1610 | |
| 1611 | srq = get_ocrdma_srq(ibsrq); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1612 | status = ocrdma_mbx_query_srq(srq, srq_attr); |
| 1613 | return status; |
| 1614 | } |
| 1615 | |
| 1616 | int ocrdma_destroy_srq(struct ib_srq *ibsrq) |
| 1617 | { |
| 1618 | int status; |
| 1619 | struct ocrdma_srq *srq; |
| 1620 | struct ocrdma_dev *dev; |
| 1621 | |
| 1622 | srq = get_ocrdma_srq(ibsrq); |
| 1623 | dev = srq->dev; |
| 1624 | if (atomic_read(&srq->use_cnt)) { |
| 1625 | ocrdma_err("%s(%d) err, srq=0x%x in use\n", |
| 1626 | __func__, dev->id, srq->id); |
| 1627 | return -EAGAIN; |
| 1628 | } |
| 1629 | |
| 1630 | status = ocrdma_mbx_destroy_srq(dev, srq); |
| 1631 | |
| 1632 | if (srq->pd->uctx) |
| 1633 | ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa, srq->rq.len); |
| 1634 | |
| 1635 | atomic_dec(&srq->pd->use_cnt); |
| 1636 | kfree(srq->idx_bit_fields); |
| 1637 | kfree(srq->rqe_wr_id_tbl); |
| 1638 | kfree(srq); |
| 1639 | return status; |
| 1640 | } |
| 1641 | |
| 1642 | /* unprivileged verbs and their support functions. */ |
| 1643 | static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp, |
| 1644 | struct ocrdma_hdr_wqe *hdr, |
| 1645 | struct ib_send_wr *wr) |
| 1646 | { |
| 1647 | struct ocrdma_ewqe_ud_hdr *ud_hdr = |
| 1648 | (struct ocrdma_ewqe_ud_hdr *)(hdr + 1); |
| 1649 | struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah); |
| 1650 | |
| 1651 | ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn; |
| 1652 | if (qp->qp_type == IB_QPT_GSI) |
| 1653 | ud_hdr->qkey = qp->qkey; |
| 1654 | else |
| 1655 | ud_hdr->qkey = wr->wr.ud.remote_qkey; |
| 1656 | ud_hdr->rsvd_ahid = ah->id; |
| 1657 | } |
| 1658 | |
| 1659 | static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr, |
| 1660 | struct ocrdma_sge *sge, int num_sge, |
| 1661 | struct ib_sge *sg_list) |
| 1662 | { |
| 1663 | int i; |
| 1664 | |
| 1665 | for (i = 0; i < num_sge; i++) { |
| 1666 | sge[i].lrkey = sg_list[i].lkey; |
| 1667 | sge[i].addr_lo = sg_list[i].addr; |
| 1668 | sge[i].addr_hi = upper_32_bits(sg_list[i].addr); |
| 1669 | sge[i].len = sg_list[i].length; |
| 1670 | hdr->total_len += sg_list[i].length; |
| 1671 | } |
| 1672 | if (num_sge == 0) |
| 1673 | memset(sge, 0, sizeof(*sge)); |
| 1674 | } |
| 1675 | |
| 1676 | static int ocrdma_build_inline_sges(struct ocrdma_qp *qp, |
| 1677 | struct ocrdma_hdr_wqe *hdr, |
| 1678 | struct ocrdma_sge *sge, |
| 1679 | struct ib_send_wr *wr, u32 wqe_size) |
| 1680 | { |
| 1681 | if (wr->send_flags & IB_SEND_INLINE) { |
| 1682 | if (wr->sg_list[0].length > qp->max_inline_data) { |
| 1683 | ocrdma_err("%s() supported_len=0x%x," |
| 1684 | " unspported len req=0x%x\n", __func__, |
| 1685 | qp->max_inline_data, wr->sg_list[0].length); |
| 1686 | return -EINVAL; |
| 1687 | } |
| 1688 | memcpy(sge, |
| 1689 | (void *)(unsigned long)wr->sg_list[0].addr, |
| 1690 | wr->sg_list[0].length); |
| 1691 | hdr->total_len = wr->sg_list[0].length; |
| 1692 | wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES); |
| 1693 | hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT); |
| 1694 | } else { |
| 1695 | ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list); |
| 1696 | if (wr->num_sge) |
| 1697 | wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge)); |
| 1698 | else |
| 1699 | wqe_size += sizeof(struct ocrdma_sge); |
| 1700 | hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT); |
| 1701 | } |
| 1702 | hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT); |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr, |
| 1707 | struct ib_send_wr *wr) |
| 1708 | { |
| 1709 | int status; |
| 1710 | struct ocrdma_sge *sge; |
| 1711 | u32 wqe_size = sizeof(*hdr); |
| 1712 | |
| 1713 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) { |
| 1714 | ocrdma_build_ud_hdr(qp, hdr, wr); |
| 1715 | sge = (struct ocrdma_sge *)(hdr + 2); |
| 1716 | wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr); |
| 1717 | } else |
| 1718 | sge = (struct ocrdma_sge *)(hdr + 1); |
| 1719 | |
| 1720 | status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size); |
| 1721 | return status; |
| 1722 | } |
| 1723 | |
| 1724 | static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr, |
| 1725 | struct ib_send_wr *wr) |
| 1726 | { |
| 1727 | int status; |
| 1728 | struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1); |
| 1729 | struct ocrdma_sge *sge = ext_rw + 1; |
| 1730 | u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw); |
| 1731 | |
| 1732 | status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size); |
| 1733 | if (status) |
| 1734 | return status; |
| 1735 | ext_rw->addr_lo = wr->wr.rdma.remote_addr; |
| 1736 | ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr); |
| 1737 | ext_rw->lrkey = wr->wr.rdma.rkey; |
| 1738 | ext_rw->len = hdr->total_len; |
| 1739 | return 0; |
| 1740 | } |
| 1741 | |
| 1742 | static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr, |
| 1743 | struct ib_send_wr *wr) |
| 1744 | { |
| 1745 | struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1); |
| 1746 | struct ocrdma_sge *sge = ext_rw + 1; |
| 1747 | u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) + |
| 1748 | sizeof(struct ocrdma_hdr_wqe); |
| 1749 | |
| 1750 | ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list); |
| 1751 | hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT); |
| 1752 | hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT); |
| 1753 | hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT); |
| 1754 | |
| 1755 | ext_rw->addr_lo = wr->wr.rdma.remote_addr; |
| 1756 | ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr); |
| 1757 | ext_rw->lrkey = wr->wr.rdma.rkey; |
| 1758 | ext_rw->len = hdr->total_len; |
| 1759 | } |
| 1760 | |
| 1761 | static void ocrdma_ring_sq_db(struct ocrdma_qp *qp) |
| 1762 | { |
| 1763 | u32 val = qp->sq.dbid | (1 << 16); |
| 1764 | |
| 1765 | iowrite32(val, qp->sq_db); |
| 1766 | } |
| 1767 | |
| 1768 | int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr, |
| 1769 | struct ib_send_wr **bad_wr) |
| 1770 | { |
| 1771 | int status = 0; |
| 1772 | struct ocrdma_qp *qp = get_ocrdma_qp(ibqp); |
| 1773 | struct ocrdma_hdr_wqe *hdr; |
| 1774 | unsigned long flags; |
| 1775 | |
| 1776 | spin_lock_irqsave(&qp->q_lock, flags); |
| 1777 | if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) { |
| 1778 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1779 | return -EINVAL; |
| 1780 | } |
| 1781 | |
| 1782 | while (wr) { |
| 1783 | if (ocrdma_hwq_free_cnt(&qp->sq) == 0 || |
| 1784 | wr->num_sge > qp->sq.max_sges) { |
| 1785 | status = -ENOMEM; |
| 1786 | break; |
| 1787 | } |
| 1788 | hdr = ocrdma_hwq_head(&qp->sq); |
| 1789 | hdr->cw = 0; |
| 1790 | if (wr->send_flags & IB_SEND_SIGNALED) |
| 1791 | hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT); |
| 1792 | if (wr->send_flags & IB_SEND_FENCE) |
| 1793 | hdr->cw |= |
| 1794 | (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT); |
| 1795 | if (wr->send_flags & IB_SEND_SOLICITED) |
| 1796 | hdr->cw |= |
| 1797 | (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT); |
| 1798 | hdr->total_len = 0; |
| 1799 | switch (wr->opcode) { |
| 1800 | case IB_WR_SEND_WITH_IMM: |
| 1801 | hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT); |
| 1802 | hdr->immdt = ntohl(wr->ex.imm_data); |
| 1803 | case IB_WR_SEND: |
| 1804 | hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT); |
| 1805 | ocrdma_build_send(qp, hdr, wr); |
| 1806 | break; |
| 1807 | case IB_WR_SEND_WITH_INV: |
| 1808 | hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT); |
| 1809 | hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT); |
| 1810 | hdr->lkey = wr->ex.invalidate_rkey; |
| 1811 | status = ocrdma_build_send(qp, hdr, wr); |
| 1812 | break; |
| 1813 | case IB_WR_RDMA_WRITE_WITH_IMM: |
| 1814 | hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT); |
| 1815 | hdr->immdt = ntohl(wr->ex.imm_data); |
| 1816 | case IB_WR_RDMA_WRITE: |
| 1817 | hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT); |
| 1818 | status = ocrdma_build_write(qp, hdr, wr); |
| 1819 | break; |
| 1820 | case IB_WR_RDMA_READ_WITH_INV: |
| 1821 | hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT); |
| 1822 | case IB_WR_RDMA_READ: |
| 1823 | ocrdma_build_read(qp, hdr, wr); |
| 1824 | break; |
| 1825 | case IB_WR_LOCAL_INV: |
| 1826 | hdr->cw |= |
| 1827 | (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT); |
| 1828 | hdr->cw |= (sizeof(struct ocrdma_hdr_wqe) / |
| 1829 | OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT; |
| 1830 | hdr->lkey = wr->ex.invalidate_rkey; |
| 1831 | break; |
| 1832 | default: |
| 1833 | status = -EINVAL; |
| 1834 | break; |
| 1835 | } |
| 1836 | if (status) { |
| 1837 | *bad_wr = wr; |
| 1838 | break; |
| 1839 | } |
| 1840 | if (wr->send_flags & IB_SEND_SIGNALED) |
| 1841 | qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1; |
| 1842 | else |
| 1843 | qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0; |
| 1844 | qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id; |
| 1845 | ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) & |
| 1846 | OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE); |
| 1847 | /* make sure wqe is written before adapter can access it */ |
| 1848 | wmb(); |
| 1849 | /* inform hw to start processing it */ |
| 1850 | ocrdma_ring_sq_db(qp); |
| 1851 | |
| 1852 | /* update pointer, counter for next wr */ |
| 1853 | ocrdma_hwq_inc_head(&qp->sq); |
| 1854 | wr = wr->next; |
| 1855 | } |
| 1856 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1857 | return status; |
| 1858 | } |
| 1859 | |
| 1860 | static void ocrdma_ring_rq_db(struct ocrdma_qp *qp) |
| 1861 | { |
| 1862 | u32 val = qp->rq.dbid | (1 << OCRDMA_GET_NUM_POSTED_SHIFT_VAL(qp)); |
| 1863 | |
| 1864 | iowrite32(val, qp->rq_db); |
| 1865 | } |
| 1866 | |
| 1867 | static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr, |
| 1868 | u16 tag) |
| 1869 | { |
| 1870 | u32 wqe_size = 0; |
| 1871 | struct ocrdma_sge *sge; |
| 1872 | if (wr->num_sge) |
| 1873 | wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe); |
| 1874 | else |
| 1875 | wqe_size = sizeof(*sge) + sizeof(*rqe); |
| 1876 | |
| 1877 | rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) << |
| 1878 | OCRDMA_WQE_SIZE_SHIFT); |
| 1879 | rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT); |
| 1880 | rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT); |
| 1881 | rqe->total_len = 0; |
| 1882 | rqe->rsvd_tag = tag; |
| 1883 | sge = (struct ocrdma_sge *)(rqe + 1); |
| 1884 | ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list); |
| 1885 | ocrdma_cpu_to_le32(rqe, wqe_size); |
| 1886 | } |
| 1887 | |
| 1888 | int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr, |
| 1889 | struct ib_recv_wr **bad_wr) |
| 1890 | { |
| 1891 | int status = 0; |
| 1892 | unsigned long flags; |
| 1893 | struct ocrdma_qp *qp = get_ocrdma_qp(ibqp); |
| 1894 | struct ocrdma_hdr_wqe *rqe; |
| 1895 | |
| 1896 | spin_lock_irqsave(&qp->q_lock, flags); |
| 1897 | if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) { |
| 1898 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1899 | *bad_wr = wr; |
| 1900 | return -EINVAL; |
| 1901 | } |
| 1902 | while (wr) { |
| 1903 | if (ocrdma_hwq_free_cnt(&qp->rq) == 0 || |
| 1904 | wr->num_sge > qp->rq.max_sges) { |
| 1905 | *bad_wr = wr; |
| 1906 | status = -ENOMEM; |
| 1907 | break; |
| 1908 | } |
| 1909 | rqe = ocrdma_hwq_head(&qp->rq); |
| 1910 | ocrdma_build_rqe(rqe, wr, 0); |
| 1911 | |
| 1912 | qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id; |
| 1913 | /* make sure rqe is written before adapter can access it */ |
| 1914 | wmb(); |
| 1915 | |
| 1916 | /* inform hw to start processing it */ |
| 1917 | ocrdma_ring_rq_db(qp); |
| 1918 | |
| 1919 | /* update pointer, counter for next wr */ |
| 1920 | ocrdma_hwq_inc_head(&qp->rq); |
| 1921 | wr = wr->next; |
| 1922 | } |
| 1923 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1924 | return status; |
| 1925 | } |
| 1926 | |
| 1927 | /* cqe for srq's rqe can potentially arrive out of order. |
| 1928 | * index gives the entry in the shadow table where to store |
| 1929 | * the wr_id. tag/index is returned in cqe to reference back |
| 1930 | * for a given rqe. |
| 1931 | */ |
| 1932 | static int ocrdma_srq_get_idx(struct ocrdma_srq *srq) |
| 1933 | { |
| 1934 | int row = 0; |
| 1935 | int indx = 0; |
| 1936 | |
| 1937 | for (row = 0; row < srq->bit_fields_len; row++) { |
| 1938 | if (srq->idx_bit_fields[row]) { |
| 1939 | indx = ffs(srq->idx_bit_fields[row]); |
| 1940 | indx = (row * 32) + (indx - 1); |
| 1941 | if (indx >= srq->rq.max_cnt) |
| 1942 | BUG(); |
| 1943 | ocrdma_srq_toggle_bit(srq, indx); |
| 1944 | break; |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | if (row == srq->bit_fields_len) |
| 1949 | BUG(); |
| 1950 | return indx; |
| 1951 | } |
| 1952 | |
| 1953 | static void ocrdma_ring_srq_db(struct ocrdma_srq *srq) |
| 1954 | { |
| 1955 | u32 val = srq->rq.dbid | (1 << 16); |
| 1956 | |
| 1957 | iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET); |
| 1958 | } |
| 1959 | |
| 1960 | int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, |
| 1961 | struct ib_recv_wr **bad_wr) |
| 1962 | { |
| 1963 | int status = 0; |
| 1964 | unsigned long flags; |
| 1965 | struct ocrdma_srq *srq; |
| 1966 | struct ocrdma_hdr_wqe *rqe; |
| 1967 | u16 tag; |
| 1968 | |
| 1969 | srq = get_ocrdma_srq(ibsrq); |
| 1970 | |
| 1971 | spin_lock_irqsave(&srq->q_lock, flags); |
| 1972 | while (wr) { |
| 1973 | if (ocrdma_hwq_free_cnt(&srq->rq) == 0 || |
| 1974 | wr->num_sge > srq->rq.max_sges) { |
| 1975 | status = -ENOMEM; |
| 1976 | *bad_wr = wr; |
| 1977 | break; |
| 1978 | } |
| 1979 | tag = ocrdma_srq_get_idx(srq); |
| 1980 | rqe = ocrdma_hwq_head(&srq->rq); |
| 1981 | ocrdma_build_rqe(rqe, wr, tag); |
| 1982 | |
| 1983 | srq->rqe_wr_id_tbl[tag] = wr->wr_id; |
| 1984 | /* make sure rqe is written before adapter can perform DMA */ |
| 1985 | wmb(); |
| 1986 | /* inform hw to start processing it */ |
| 1987 | ocrdma_ring_srq_db(srq); |
| 1988 | /* update pointer, counter for next wr */ |
| 1989 | ocrdma_hwq_inc_head(&srq->rq); |
| 1990 | wr = wr->next; |
| 1991 | } |
| 1992 | spin_unlock_irqrestore(&srq->q_lock, flags); |
| 1993 | return status; |
| 1994 | } |
| 1995 | |
| 1996 | static enum ib_wc_status ocrdma_to_ibwc_err(u16 status) |
| 1997 | { |
| 1998 | enum ib_wc_status ibwc_status = IB_WC_GENERAL_ERR; |
| 1999 | |
| 2000 | switch (status) { |
| 2001 | case OCRDMA_CQE_GENERAL_ERR: |
| 2002 | ibwc_status = IB_WC_GENERAL_ERR; |
| 2003 | break; |
| 2004 | case OCRDMA_CQE_LOC_LEN_ERR: |
| 2005 | ibwc_status = IB_WC_LOC_LEN_ERR; |
| 2006 | break; |
| 2007 | case OCRDMA_CQE_LOC_QP_OP_ERR: |
| 2008 | ibwc_status = IB_WC_LOC_QP_OP_ERR; |
| 2009 | break; |
| 2010 | case OCRDMA_CQE_LOC_EEC_OP_ERR: |
| 2011 | ibwc_status = IB_WC_LOC_EEC_OP_ERR; |
| 2012 | break; |
| 2013 | case OCRDMA_CQE_LOC_PROT_ERR: |
| 2014 | ibwc_status = IB_WC_LOC_PROT_ERR; |
| 2015 | break; |
| 2016 | case OCRDMA_CQE_WR_FLUSH_ERR: |
| 2017 | ibwc_status = IB_WC_WR_FLUSH_ERR; |
| 2018 | break; |
| 2019 | case OCRDMA_CQE_MW_BIND_ERR: |
| 2020 | ibwc_status = IB_WC_MW_BIND_ERR; |
| 2021 | break; |
| 2022 | case OCRDMA_CQE_BAD_RESP_ERR: |
| 2023 | ibwc_status = IB_WC_BAD_RESP_ERR; |
| 2024 | break; |
| 2025 | case OCRDMA_CQE_LOC_ACCESS_ERR: |
| 2026 | ibwc_status = IB_WC_LOC_ACCESS_ERR; |
| 2027 | break; |
| 2028 | case OCRDMA_CQE_REM_INV_REQ_ERR: |
| 2029 | ibwc_status = IB_WC_REM_INV_REQ_ERR; |
| 2030 | break; |
| 2031 | case OCRDMA_CQE_REM_ACCESS_ERR: |
| 2032 | ibwc_status = IB_WC_REM_ACCESS_ERR; |
| 2033 | break; |
| 2034 | case OCRDMA_CQE_REM_OP_ERR: |
| 2035 | ibwc_status = IB_WC_REM_OP_ERR; |
| 2036 | break; |
| 2037 | case OCRDMA_CQE_RETRY_EXC_ERR: |
| 2038 | ibwc_status = IB_WC_RETRY_EXC_ERR; |
| 2039 | break; |
| 2040 | case OCRDMA_CQE_RNR_RETRY_EXC_ERR: |
| 2041 | ibwc_status = IB_WC_RNR_RETRY_EXC_ERR; |
| 2042 | break; |
| 2043 | case OCRDMA_CQE_LOC_RDD_VIOL_ERR: |
| 2044 | ibwc_status = IB_WC_LOC_RDD_VIOL_ERR; |
| 2045 | break; |
| 2046 | case OCRDMA_CQE_REM_INV_RD_REQ_ERR: |
| 2047 | ibwc_status = IB_WC_REM_INV_RD_REQ_ERR; |
| 2048 | break; |
| 2049 | case OCRDMA_CQE_REM_ABORT_ERR: |
| 2050 | ibwc_status = IB_WC_REM_ABORT_ERR; |
| 2051 | break; |
| 2052 | case OCRDMA_CQE_INV_EECN_ERR: |
| 2053 | ibwc_status = IB_WC_INV_EECN_ERR; |
| 2054 | break; |
| 2055 | case OCRDMA_CQE_INV_EEC_STATE_ERR: |
| 2056 | ibwc_status = IB_WC_INV_EEC_STATE_ERR; |
| 2057 | break; |
| 2058 | case OCRDMA_CQE_FATAL_ERR: |
| 2059 | ibwc_status = IB_WC_FATAL_ERR; |
| 2060 | break; |
| 2061 | case OCRDMA_CQE_RESP_TIMEOUT_ERR: |
| 2062 | ibwc_status = IB_WC_RESP_TIMEOUT_ERR; |
| 2063 | break; |
| 2064 | default: |
| 2065 | ibwc_status = IB_WC_GENERAL_ERR; |
| 2066 | break; |
| 2067 | }; |
| 2068 | return ibwc_status; |
| 2069 | } |
| 2070 | |
| 2071 | static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc, |
| 2072 | u32 wqe_idx) |
| 2073 | { |
| 2074 | struct ocrdma_hdr_wqe *hdr; |
| 2075 | struct ocrdma_sge *rw; |
| 2076 | int opcode; |
| 2077 | |
| 2078 | hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx); |
| 2079 | |
| 2080 | ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid; |
| 2081 | /* Undo the hdr->cw swap */ |
| 2082 | opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK; |
| 2083 | switch (opcode) { |
| 2084 | case OCRDMA_WRITE: |
| 2085 | ibwc->opcode = IB_WC_RDMA_WRITE; |
| 2086 | break; |
| 2087 | case OCRDMA_READ: |
| 2088 | rw = (struct ocrdma_sge *)(hdr + 1); |
| 2089 | ibwc->opcode = IB_WC_RDMA_READ; |
| 2090 | ibwc->byte_len = rw->len; |
| 2091 | break; |
| 2092 | case OCRDMA_SEND: |
| 2093 | ibwc->opcode = IB_WC_SEND; |
| 2094 | break; |
| 2095 | case OCRDMA_LKEY_INV: |
| 2096 | ibwc->opcode = IB_WC_LOCAL_INV; |
| 2097 | break; |
| 2098 | default: |
| 2099 | ibwc->status = IB_WC_GENERAL_ERR; |
| 2100 | ocrdma_err("%s() invalid opcode received = 0x%x\n", |
| 2101 | __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK); |
| 2102 | break; |
| 2103 | }; |
| 2104 | } |
| 2105 | |
| 2106 | static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp, |
| 2107 | struct ocrdma_cqe *cqe) |
| 2108 | { |
| 2109 | if (is_cqe_for_sq(cqe)) { |
| 2110 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2111 | cqe->flags_status_srcqpn) & |
| 2112 | ~OCRDMA_CQE_STATUS_MASK); |
| 2113 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2114 | cqe->flags_status_srcqpn) | |
| 2115 | (OCRDMA_CQE_WR_FLUSH_ERR << |
| 2116 | OCRDMA_CQE_STATUS_SHIFT)); |
| 2117 | } else { |
| 2118 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) { |
| 2119 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2120 | cqe->flags_status_srcqpn) & |
| 2121 | ~OCRDMA_CQE_UD_STATUS_MASK); |
| 2122 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2123 | cqe->flags_status_srcqpn) | |
| 2124 | (OCRDMA_CQE_WR_FLUSH_ERR << |
| 2125 | OCRDMA_CQE_UD_STATUS_SHIFT)); |
| 2126 | } else { |
| 2127 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2128 | cqe->flags_status_srcqpn) & |
| 2129 | ~OCRDMA_CQE_STATUS_MASK); |
| 2130 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2131 | cqe->flags_status_srcqpn) | |
| 2132 | (OCRDMA_CQE_WR_FLUSH_ERR << |
| 2133 | OCRDMA_CQE_STATUS_SHIFT)); |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe, |
| 2139 | struct ocrdma_qp *qp, int status) |
| 2140 | { |
| 2141 | bool expand = false; |
| 2142 | |
| 2143 | ibwc->byte_len = 0; |
| 2144 | ibwc->qp = &qp->ibqp; |
| 2145 | ibwc->status = ocrdma_to_ibwc_err(status); |
| 2146 | |
| 2147 | ocrdma_flush_qp(qp); |
| 2148 | ocrdma_qp_state_machine(qp, IB_QPS_ERR, NULL); |
| 2149 | |
| 2150 | /* if wqe/rqe pending for which cqe needs to be returned, |
| 2151 | * trigger inflating it. |
| 2152 | */ |
| 2153 | if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) { |
| 2154 | expand = true; |
| 2155 | ocrdma_set_cqe_status_flushed(qp, cqe); |
| 2156 | } |
| 2157 | return expand; |
| 2158 | } |
| 2159 | |
| 2160 | static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe, |
| 2161 | struct ocrdma_qp *qp, int status) |
| 2162 | { |
| 2163 | ibwc->opcode = IB_WC_RECV; |
| 2164 | ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail]; |
| 2165 | ocrdma_hwq_inc_tail(&qp->rq); |
| 2166 | |
| 2167 | return ocrdma_update_err_cqe(ibwc, cqe, qp, status); |
| 2168 | } |
| 2169 | |
| 2170 | static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe, |
| 2171 | struct ocrdma_qp *qp, int status) |
| 2172 | { |
| 2173 | ocrdma_update_wc(qp, ibwc, qp->sq.tail); |
| 2174 | ocrdma_hwq_inc_tail(&qp->sq); |
| 2175 | |
| 2176 | return ocrdma_update_err_cqe(ibwc, cqe, qp, status); |
| 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp, |
| 2181 | struct ocrdma_cqe *cqe, struct ib_wc *ibwc, |
| 2182 | bool *polled, bool *stop) |
| 2183 | { |
| 2184 | bool expand; |
| 2185 | int status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2186 | OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT; |
| 2187 | |
| 2188 | /* when hw sq is empty, but rq is not empty, so we continue |
| 2189 | * to keep the cqe in order to get the cq event again. |
| 2190 | */ |
| 2191 | if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) { |
| 2192 | /* when cq for rq and sq is same, it is safe to return |
| 2193 | * flush cqe for RQEs. |
| 2194 | */ |
| 2195 | if (!qp->srq && (qp->sq_cq == qp->rq_cq)) { |
| 2196 | *polled = true; |
| 2197 | status = OCRDMA_CQE_WR_FLUSH_ERR; |
| 2198 | expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status); |
| 2199 | } else { |
| 2200 | /* stop processing further cqe as this cqe is used for |
| 2201 | * triggering cq event on buddy cq of RQ. |
| 2202 | * When QP is destroyed, this cqe will be removed |
| 2203 | * from the cq's hardware q. |
| 2204 | */ |
| 2205 | *polled = false; |
| 2206 | *stop = true; |
| 2207 | expand = false; |
| 2208 | } |
| 2209 | } else { |
| 2210 | *polled = true; |
| 2211 | expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status); |
| 2212 | } |
| 2213 | return expand; |
| 2214 | } |
| 2215 | |
| 2216 | static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp, |
| 2217 | struct ocrdma_cqe *cqe, |
| 2218 | struct ib_wc *ibwc, bool *polled) |
| 2219 | { |
| 2220 | bool expand = false; |
| 2221 | int tail = qp->sq.tail; |
| 2222 | u32 wqe_idx; |
| 2223 | |
| 2224 | if (!qp->wqe_wr_id_tbl[tail].signaled) { |
| 2225 | expand = true; /* CQE cannot be consumed yet */ |
| 2226 | *polled = false; /* WC cannot be consumed yet */ |
| 2227 | } else { |
| 2228 | ibwc->status = IB_WC_SUCCESS; |
| 2229 | ibwc->wc_flags = 0; |
| 2230 | ibwc->qp = &qp->ibqp; |
| 2231 | ocrdma_update_wc(qp, ibwc, tail); |
| 2232 | *polled = true; |
| 2233 | wqe_idx = le32_to_cpu(cqe->wq.wqeidx) & OCRDMA_CQE_WQEIDX_MASK; |
| 2234 | if (tail != wqe_idx) |
| 2235 | expand = true; /* Coalesced CQE can't be consumed yet */ |
| 2236 | } |
| 2237 | ocrdma_hwq_inc_tail(&qp->sq); |
| 2238 | return expand; |
| 2239 | } |
| 2240 | |
| 2241 | static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe, |
| 2242 | struct ib_wc *ibwc, bool *polled, bool *stop) |
| 2243 | { |
| 2244 | int status; |
| 2245 | bool expand; |
| 2246 | |
| 2247 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2248 | OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT; |
| 2249 | |
| 2250 | if (status == OCRDMA_CQE_SUCCESS) |
| 2251 | expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled); |
| 2252 | else |
| 2253 | expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop); |
| 2254 | return expand; |
| 2255 | } |
| 2256 | |
| 2257 | static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe) |
| 2258 | { |
| 2259 | int status; |
| 2260 | |
| 2261 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2262 | OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT; |
| 2263 | ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2264 | OCRDMA_CQE_SRCQP_MASK; |
| 2265 | ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) & |
| 2266 | OCRDMA_CQE_PKEY_MASK; |
| 2267 | ibwc->wc_flags = IB_WC_GRH; |
| 2268 | ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >> |
| 2269 | OCRDMA_CQE_UD_XFER_LEN_SHIFT); |
| 2270 | return status; |
| 2271 | } |
| 2272 | |
| 2273 | static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc, |
| 2274 | struct ocrdma_cqe *cqe, |
| 2275 | struct ocrdma_qp *qp) |
| 2276 | { |
| 2277 | unsigned long flags; |
| 2278 | struct ocrdma_srq *srq; |
| 2279 | u32 wqe_idx; |
| 2280 | |
| 2281 | srq = get_ocrdma_srq(qp->ibqp.srq); |
| 2282 | wqe_idx = le32_to_cpu(cqe->rq.buftag_qpn) >> OCRDMA_CQE_BUFTAG_SHIFT; |
| 2283 | ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx]; |
| 2284 | spin_lock_irqsave(&srq->q_lock, flags); |
| 2285 | ocrdma_srq_toggle_bit(srq, wqe_idx); |
| 2286 | spin_unlock_irqrestore(&srq->q_lock, flags); |
| 2287 | ocrdma_hwq_inc_tail(&srq->rq); |
| 2288 | } |
| 2289 | |
| 2290 | static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe, |
| 2291 | struct ib_wc *ibwc, bool *polled, bool *stop, |
| 2292 | int status) |
| 2293 | { |
| 2294 | bool expand; |
| 2295 | |
| 2296 | /* when hw_rq is empty, but wq is not empty, so continue |
| 2297 | * to keep the cqe to get the cq event again. |
| 2298 | */ |
| 2299 | if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) { |
| 2300 | if (!qp->srq && (qp->sq_cq == qp->rq_cq)) { |
| 2301 | *polled = true; |
| 2302 | status = OCRDMA_CQE_WR_FLUSH_ERR; |
| 2303 | expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status); |
| 2304 | } else { |
| 2305 | *polled = false; |
| 2306 | *stop = true; |
| 2307 | expand = false; |
| 2308 | } |
| 2309 | } else |
| 2310 | expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status); |
| 2311 | return expand; |
| 2312 | } |
| 2313 | |
| 2314 | static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp, |
| 2315 | struct ocrdma_cqe *cqe, struct ib_wc *ibwc) |
| 2316 | { |
| 2317 | ibwc->opcode = IB_WC_RECV; |
| 2318 | ibwc->qp = &qp->ibqp; |
| 2319 | ibwc->status = IB_WC_SUCCESS; |
| 2320 | |
| 2321 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) |
| 2322 | ocrdma_update_ud_rcqe(ibwc, cqe); |
| 2323 | else |
| 2324 | ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen); |
| 2325 | |
| 2326 | if (is_cqe_imm(cqe)) { |
| 2327 | ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt)); |
| 2328 | ibwc->wc_flags |= IB_WC_WITH_IMM; |
| 2329 | } else if (is_cqe_wr_imm(cqe)) { |
| 2330 | ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM; |
| 2331 | ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt)); |
| 2332 | ibwc->wc_flags |= IB_WC_WITH_IMM; |
| 2333 | } else if (is_cqe_invalidated(cqe)) { |
| 2334 | ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt); |
| 2335 | ibwc->wc_flags |= IB_WC_WITH_INVALIDATE; |
| 2336 | } |
| 2337 | if (qp->ibqp.srq) |
| 2338 | ocrdma_update_free_srq_cqe(ibwc, cqe, qp); |
| 2339 | else { |
| 2340 | ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail]; |
| 2341 | ocrdma_hwq_inc_tail(&qp->rq); |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe, |
| 2346 | struct ib_wc *ibwc, bool *polled, bool *stop) |
| 2347 | { |
| 2348 | int status; |
| 2349 | bool expand = false; |
| 2350 | |
| 2351 | ibwc->wc_flags = 0; |
| 2352 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) |
| 2353 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2354 | OCRDMA_CQE_UD_STATUS_MASK) >> |
| 2355 | OCRDMA_CQE_UD_STATUS_SHIFT; |
| 2356 | else |
| 2357 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2358 | OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT; |
| 2359 | |
| 2360 | if (status == OCRDMA_CQE_SUCCESS) { |
| 2361 | *polled = true; |
| 2362 | ocrdma_poll_success_rcqe(qp, cqe, ibwc); |
| 2363 | } else { |
| 2364 | expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop, |
| 2365 | status); |
| 2366 | } |
| 2367 | return expand; |
| 2368 | } |
| 2369 | |
| 2370 | static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe, |
| 2371 | u16 cur_getp) |
| 2372 | { |
| 2373 | if (cq->phase_change) { |
| 2374 | if (cur_getp == 0) |
| 2375 | cq->phase = (~cq->phase & OCRDMA_CQE_VALID); |
| 2376 | } else |
| 2377 | /* clear valid bit */ |
| 2378 | cqe->flags_status_srcqpn = 0; |
| 2379 | } |
| 2380 | |
| 2381 | static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries, |
| 2382 | struct ib_wc *ibwc) |
| 2383 | { |
| 2384 | u16 qpn = 0; |
| 2385 | int i = 0; |
| 2386 | bool expand = false; |
| 2387 | int polled_hw_cqes = 0; |
| 2388 | struct ocrdma_qp *qp = NULL; |
| 2389 | struct ocrdma_dev *dev = cq->dev; |
| 2390 | struct ocrdma_cqe *cqe; |
| 2391 | u16 cur_getp; bool polled = false; bool stop = false; |
| 2392 | |
| 2393 | cur_getp = cq->getp; |
| 2394 | while (num_entries) { |
| 2395 | cqe = cq->va + cur_getp; |
| 2396 | /* check whether valid cqe or not */ |
| 2397 | if (!is_cqe_valid(cq, cqe)) |
| 2398 | break; |
| 2399 | qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK); |
| 2400 | /* ignore discarded cqe */ |
| 2401 | if (qpn == 0) |
| 2402 | goto skip_cqe; |
| 2403 | qp = dev->qp_tbl[qpn]; |
| 2404 | BUG_ON(qp == NULL); |
| 2405 | |
| 2406 | if (is_cqe_for_sq(cqe)) { |
| 2407 | expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled, |
| 2408 | &stop); |
| 2409 | } else { |
| 2410 | expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled, |
| 2411 | &stop); |
| 2412 | } |
| 2413 | if (expand) |
| 2414 | goto expand_cqe; |
| 2415 | if (stop) |
| 2416 | goto stop_cqe; |
| 2417 | /* clear qpn to avoid duplicate processing by discard_cqe() */ |
| 2418 | cqe->cmn.qpn = 0; |
| 2419 | skip_cqe: |
| 2420 | polled_hw_cqes += 1; |
| 2421 | cur_getp = (cur_getp + 1) % cq->max_hw_cqe; |
| 2422 | ocrdma_change_cq_phase(cq, cqe, cur_getp); |
| 2423 | expand_cqe: |
| 2424 | if (polled) { |
| 2425 | num_entries -= 1; |
| 2426 | i += 1; |
| 2427 | ibwc = ibwc + 1; |
| 2428 | polled = false; |
| 2429 | } |
| 2430 | } |
| 2431 | stop_cqe: |
| 2432 | cq->getp = cur_getp; |
| 2433 | if (polled_hw_cqes || expand || stop) { |
| 2434 | ocrdma_ring_cq_db(dev, cq->id, cq->armed, cq->solicited, |
| 2435 | polled_hw_cqes); |
| 2436 | } |
| 2437 | return i; |
| 2438 | } |
| 2439 | |
| 2440 | /* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */ |
| 2441 | static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries, |
| 2442 | struct ocrdma_qp *qp, struct ib_wc *ibwc) |
| 2443 | { |
| 2444 | int err_cqes = 0; |
| 2445 | |
| 2446 | while (num_entries) { |
| 2447 | if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp)) |
| 2448 | break; |
| 2449 | if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) { |
| 2450 | ocrdma_update_wc(qp, ibwc, qp->sq.tail); |
| 2451 | ocrdma_hwq_inc_tail(&qp->sq); |
| 2452 | } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) { |
| 2453 | ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail]; |
| 2454 | ocrdma_hwq_inc_tail(&qp->rq); |
| 2455 | } else |
| 2456 | return err_cqes; |
| 2457 | ibwc->byte_len = 0; |
| 2458 | ibwc->status = IB_WC_WR_FLUSH_ERR; |
| 2459 | ibwc = ibwc + 1; |
| 2460 | err_cqes += 1; |
| 2461 | num_entries -= 1; |
| 2462 | } |
| 2463 | return err_cqes; |
| 2464 | } |
| 2465 | |
| 2466 | int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) |
| 2467 | { |
| 2468 | int cqes_to_poll = num_entries; |
| 2469 | struct ocrdma_cq *cq = NULL; |
| 2470 | unsigned long flags; |
| 2471 | struct ocrdma_dev *dev; |
| 2472 | int num_os_cqe = 0, err_cqes = 0; |
| 2473 | struct ocrdma_qp *qp; |
| 2474 | |
| 2475 | cq = get_ocrdma_cq(ibcq); |
| 2476 | dev = cq->dev; |
| 2477 | |
| 2478 | /* poll cqes from adapter CQ */ |
| 2479 | spin_lock_irqsave(&cq->cq_lock, flags); |
| 2480 | num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc); |
| 2481 | spin_unlock_irqrestore(&cq->cq_lock, flags); |
| 2482 | cqes_to_poll -= num_os_cqe; |
| 2483 | |
| 2484 | if (cqes_to_poll) { |
| 2485 | wc = wc + num_os_cqe; |
| 2486 | /* adapter returns single error cqe when qp moves to |
| 2487 | * error state. So insert error cqes with wc_status as |
| 2488 | * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ |
| 2489 | * respectively which uses this CQ. |
| 2490 | */ |
| 2491 | spin_lock_irqsave(&dev->flush_q_lock, flags); |
| 2492 | list_for_each_entry(qp, &cq->sq_head, sq_entry) { |
| 2493 | if (cqes_to_poll == 0) |
| 2494 | break; |
| 2495 | err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc); |
| 2496 | cqes_to_poll -= err_cqes; |
| 2497 | num_os_cqe += err_cqes; |
| 2498 | wc = wc + err_cqes; |
| 2499 | } |
| 2500 | spin_unlock_irqrestore(&dev->flush_q_lock, flags); |
| 2501 | } |
| 2502 | return num_os_cqe; |
| 2503 | } |
| 2504 | |
| 2505 | int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags) |
| 2506 | { |
| 2507 | struct ocrdma_cq *cq; |
| 2508 | unsigned long flags; |
| 2509 | struct ocrdma_dev *dev; |
| 2510 | u16 cq_id; |
| 2511 | u16 cur_getp; |
| 2512 | struct ocrdma_cqe *cqe; |
| 2513 | |
| 2514 | cq = get_ocrdma_cq(ibcq); |
| 2515 | cq_id = cq->id; |
| 2516 | dev = cq->dev; |
| 2517 | |
| 2518 | spin_lock_irqsave(&cq->cq_lock, flags); |
| 2519 | if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED) |
| 2520 | cq->armed = true; |
| 2521 | if (cq_flags & IB_CQ_SOLICITED) |
| 2522 | cq->solicited = true; |
| 2523 | |
| 2524 | cur_getp = cq->getp; |
| 2525 | cqe = cq->va + cur_getp; |
| 2526 | |
| 2527 | /* check whether any valid cqe exist or not, if not then safe to |
| 2528 | * arm. If cqe is not yet consumed, then let it get consumed and then |
| 2529 | * we arm it to avoid false interrupts. |
| 2530 | */ |
| 2531 | if (!is_cqe_valid(cq, cqe) || cq->arm_needed) { |
| 2532 | cq->arm_needed = false; |
| 2533 | ocrdma_ring_cq_db(dev, cq_id, cq->armed, cq->solicited, 0); |
| 2534 | } |
| 2535 | spin_unlock_irqrestore(&cq->cq_lock, flags); |
| 2536 | return 0; |
| 2537 | } |