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); |
Dan Carpenter | 55a8d62 | 2012-05-02 09:13:57 +0300 | [diff] [blame] | 489 | if (IS_ERR(mr)) |
| 490 | return ERR_CAST(mr); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 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 | } |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 943 | |
| 944 | if (qp->dpp_enabled) { |
| 945 | uresp.dpp_credit = dpp_credit_lmt; |
| 946 | uresp.dpp_offset = dpp_offset; |
| 947 | } |
| 948 | status = ib_copy_to_udata(udata, &uresp, sizeof(uresp)); |
| 949 | if (status) { |
| 950 | ocrdma_err("%s(%d) user copy error.\n", __func__, dev->id); |
| 951 | goto err; |
| 952 | } |
| 953 | status = ocrdma_add_mmap(pd->uctx, uresp.sq_page_addr[0], |
| 954 | uresp.sq_page_size); |
| 955 | if (status) |
| 956 | goto err; |
| 957 | |
| 958 | if (!srq) { |
| 959 | status = ocrdma_add_mmap(pd->uctx, uresp.rq_page_addr[0], |
| 960 | uresp.rq_page_size); |
| 961 | if (status) |
| 962 | goto rq_map_err; |
| 963 | } |
| 964 | return status; |
| 965 | rq_map_err: |
| 966 | ocrdma_del_mmap(pd->uctx, uresp.sq_page_addr[0], uresp.sq_page_size); |
| 967 | err: |
| 968 | return status; |
| 969 | } |
| 970 | |
| 971 | static void ocrdma_set_qp_db(struct ocrdma_dev *dev, struct ocrdma_qp *qp, |
| 972 | struct ocrdma_pd *pd) |
| 973 | { |
| 974 | if (dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) { |
| 975 | qp->sq_db = dev->nic_info.db + |
| 976 | (pd->id * dev->nic_info.db_page_size) + |
| 977 | OCRDMA_DB_GEN2_SQ_OFFSET; |
| 978 | qp->rq_db = dev->nic_info.db + |
| 979 | (pd->id * dev->nic_info.db_page_size) + |
| 980 | ((qp->id < 128) ? |
| 981 | OCRDMA_DB_GEN2_RQ1_OFFSET : OCRDMA_DB_GEN2_RQ2_OFFSET); |
| 982 | } else { |
| 983 | qp->sq_db = dev->nic_info.db + |
| 984 | (pd->id * dev->nic_info.db_page_size) + |
| 985 | OCRDMA_DB_SQ_OFFSET; |
| 986 | qp->rq_db = dev->nic_info.db + |
| 987 | (pd->id * dev->nic_info.db_page_size) + |
| 988 | OCRDMA_DB_RQ_OFFSET; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | static int ocrdma_alloc_wr_id_tbl(struct ocrdma_qp *qp) |
| 993 | { |
| 994 | qp->wqe_wr_id_tbl = |
| 995 | kzalloc(sizeof(*(qp->wqe_wr_id_tbl)) * qp->sq.max_cnt, |
| 996 | GFP_KERNEL); |
| 997 | if (qp->wqe_wr_id_tbl == NULL) |
| 998 | return -ENOMEM; |
| 999 | qp->rqe_wr_id_tbl = |
| 1000 | kzalloc(sizeof(u64) * qp->rq.max_cnt, GFP_KERNEL); |
| 1001 | if (qp->rqe_wr_id_tbl == NULL) |
| 1002 | return -ENOMEM; |
| 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | static void ocrdma_set_qp_init_params(struct ocrdma_qp *qp, |
| 1008 | struct ocrdma_pd *pd, |
| 1009 | struct ib_qp_init_attr *attrs) |
| 1010 | { |
| 1011 | qp->pd = pd; |
| 1012 | spin_lock_init(&qp->q_lock); |
| 1013 | INIT_LIST_HEAD(&qp->sq_entry); |
| 1014 | INIT_LIST_HEAD(&qp->rq_entry); |
| 1015 | |
| 1016 | qp->qp_type = attrs->qp_type; |
| 1017 | qp->cap_flags = OCRDMA_QP_INB_RD | OCRDMA_QP_INB_WR; |
| 1018 | qp->max_inline_data = attrs->cap.max_inline_data; |
| 1019 | qp->sq.max_sges = attrs->cap.max_send_sge; |
| 1020 | qp->rq.max_sges = attrs->cap.max_recv_sge; |
| 1021 | qp->state = OCRDMA_QPS_RST; |
| 1022 | } |
| 1023 | |
| 1024 | static void ocrdma_set_qp_use_cnt(struct ocrdma_qp *qp, struct ocrdma_pd *pd) |
| 1025 | { |
| 1026 | atomic_inc(&pd->use_cnt); |
| 1027 | atomic_inc(&qp->sq_cq->use_cnt); |
| 1028 | atomic_inc(&qp->rq_cq->use_cnt); |
| 1029 | if (qp->srq) |
| 1030 | atomic_inc(&qp->srq->use_cnt); |
| 1031 | qp->ibqp.qp_num = qp->id; |
| 1032 | } |
| 1033 | |
| 1034 | static void ocrdma_store_gsi_qp_cq(struct ocrdma_dev *dev, |
| 1035 | struct ib_qp_init_attr *attrs) |
| 1036 | { |
| 1037 | if (attrs->qp_type == IB_QPT_GSI) { |
| 1038 | dev->gsi_qp_created = 1; |
| 1039 | dev->gsi_sqcq = get_ocrdma_cq(attrs->send_cq); |
| 1040 | dev->gsi_rqcq = get_ocrdma_cq(attrs->recv_cq); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | struct ib_qp *ocrdma_create_qp(struct ib_pd *ibpd, |
| 1045 | struct ib_qp_init_attr *attrs, |
| 1046 | struct ib_udata *udata) |
| 1047 | { |
| 1048 | int status; |
| 1049 | struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); |
| 1050 | struct ocrdma_qp *qp; |
| 1051 | struct ocrdma_dev *dev = pd->dev; |
| 1052 | struct ocrdma_create_qp_ureq ureq; |
| 1053 | u16 dpp_credit_lmt, dpp_offset; |
| 1054 | |
| 1055 | status = ocrdma_check_qp_params(ibpd, dev, attrs); |
| 1056 | if (status) |
| 1057 | goto gen_err; |
| 1058 | |
| 1059 | memset(&ureq, 0, sizeof(ureq)); |
| 1060 | if (udata) { |
| 1061 | if (ib_copy_from_udata(&ureq, udata, sizeof(ureq))) |
| 1062 | return ERR_PTR(-EFAULT); |
| 1063 | } |
| 1064 | qp = kzalloc(sizeof(*qp), GFP_KERNEL); |
| 1065 | if (!qp) { |
| 1066 | status = -ENOMEM; |
| 1067 | goto gen_err; |
| 1068 | } |
| 1069 | qp->dev = dev; |
| 1070 | ocrdma_set_qp_init_params(qp, pd, attrs); |
| 1071 | |
| 1072 | mutex_lock(&dev->dev_lock); |
| 1073 | status = ocrdma_mbx_create_qp(qp, attrs, ureq.enable_dpp_cq, |
| 1074 | ureq.dpp_cq_id, |
| 1075 | &dpp_offset, &dpp_credit_lmt); |
| 1076 | if (status) |
| 1077 | goto mbx_err; |
| 1078 | |
| 1079 | /* user space QP's wr_id table are managed in library */ |
| 1080 | if (udata == NULL) { |
| 1081 | qp->cap_flags |= (OCRDMA_QP_MW_BIND | OCRDMA_QP_LKEY0 | |
| 1082 | OCRDMA_QP_FAST_REG); |
| 1083 | status = ocrdma_alloc_wr_id_tbl(qp); |
| 1084 | if (status) |
| 1085 | goto map_err; |
| 1086 | } |
| 1087 | |
| 1088 | status = ocrdma_add_qpn_map(dev, qp); |
| 1089 | if (status) |
| 1090 | goto map_err; |
| 1091 | ocrdma_set_qp_db(dev, qp, pd); |
| 1092 | if (udata) { |
| 1093 | status = ocrdma_copy_qp_uresp(qp, udata, dpp_offset, |
| 1094 | dpp_credit_lmt, |
| 1095 | (attrs->srq != NULL)); |
| 1096 | if (status) |
| 1097 | goto cpy_err; |
| 1098 | } |
| 1099 | ocrdma_store_gsi_qp_cq(dev, attrs); |
| 1100 | ocrdma_set_qp_use_cnt(qp, pd); |
| 1101 | mutex_unlock(&dev->dev_lock); |
| 1102 | return &qp->ibqp; |
| 1103 | |
| 1104 | cpy_err: |
| 1105 | ocrdma_del_qpn_map(dev, qp); |
| 1106 | map_err: |
| 1107 | ocrdma_mbx_destroy_qp(dev, qp); |
| 1108 | mbx_err: |
| 1109 | mutex_unlock(&dev->dev_lock); |
| 1110 | kfree(qp->wqe_wr_id_tbl); |
| 1111 | kfree(qp->rqe_wr_id_tbl); |
| 1112 | kfree(qp); |
| 1113 | ocrdma_err("%s(%d) error=%d\n", __func__, dev->id, status); |
| 1114 | gen_err: |
| 1115 | return ERR_PTR(status); |
| 1116 | } |
| 1117 | |
| 1118 | int _ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 1119 | int attr_mask) |
| 1120 | { |
| 1121 | int status = 0; |
| 1122 | struct ocrdma_qp *qp; |
| 1123 | struct ocrdma_dev *dev; |
| 1124 | enum ib_qp_state old_qps; |
| 1125 | |
| 1126 | qp = get_ocrdma_qp(ibqp); |
| 1127 | dev = qp->dev; |
| 1128 | if (attr_mask & IB_QP_STATE) |
| 1129 | status = ocrdma_qp_state_machine(qp, attr->qp_state, &old_qps); |
| 1130 | /* if new and previous states are same hw doesn't need to |
| 1131 | * know about it. |
| 1132 | */ |
| 1133 | if (status < 0) |
| 1134 | return status; |
| 1135 | status = ocrdma_mbx_modify_qp(dev, qp, attr, attr_mask, old_qps); |
| 1136 | return status; |
| 1137 | } |
| 1138 | |
| 1139 | int ocrdma_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, |
| 1140 | int attr_mask, struct ib_udata *udata) |
| 1141 | { |
| 1142 | unsigned long flags; |
| 1143 | int status = -EINVAL; |
| 1144 | struct ocrdma_qp *qp; |
| 1145 | struct ocrdma_dev *dev; |
| 1146 | enum ib_qp_state old_qps, new_qps; |
| 1147 | |
| 1148 | qp = get_ocrdma_qp(ibqp); |
| 1149 | dev = qp->dev; |
| 1150 | |
| 1151 | /* syncronize with multiple context trying to change, retrive qps */ |
| 1152 | mutex_lock(&dev->dev_lock); |
| 1153 | /* syncronize with wqe, rqe posting and cqe processing contexts */ |
| 1154 | spin_lock_irqsave(&qp->q_lock, flags); |
| 1155 | old_qps = get_ibqp_state(qp->state); |
| 1156 | if (attr_mask & IB_QP_STATE) |
| 1157 | new_qps = attr->qp_state; |
| 1158 | else |
| 1159 | new_qps = old_qps; |
| 1160 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1161 | |
| 1162 | if (!ib_modify_qp_is_ok(old_qps, new_qps, ibqp->qp_type, attr_mask)) { |
| 1163 | ocrdma_err("%s(%d) invalid attribute mask=0x%x specified for " |
| 1164 | "qpn=0x%x of type=0x%x old_qps=0x%x, new_qps=0x%x\n", |
| 1165 | __func__, dev->id, attr_mask, qp->id, ibqp->qp_type, |
| 1166 | old_qps, new_qps); |
| 1167 | goto param_err; |
| 1168 | } |
| 1169 | |
| 1170 | status = _ocrdma_modify_qp(ibqp, attr, attr_mask); |
| 1171 | if (status > 0) |
| 1172 | status = 0; |
| 1173 | param_err: |
| 1174 | mutex_unlock(&dev->dev_lock); |
| 1175 | return status; |
| 1176 | } |
| 1177 | |
| 1178 | static enum ib_mtu ocrdma_mtu_int_to_enum(u16 mtu) |
| 1179 | { |
| 1180 | switch (mtu) { |
| 1181 | case 256: |
| 1182 | return IB_MTU_256; |
| 1183 | case 512: |
| 1184 | return IB_MTU_512; |
| 1185 | case 1024: |
| 1186 | return IB_MTU_1024; |
| 1187 | case 2048: |
| 1188 | return IB_MTU_2048; |
| 1189 | case 4096: |
| 1190 | return IB_MTU_4096; |
| 1191 | default: |
| 1192 | return IB_MTU_1024; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | static int ocrdma_to_ib_qp_acc_flags(int qp_cap_flags) |
| 1197 | { |
| 1198 | int ib_qp_acc_flags = 0; |
| 1199 | |
| 1200 | if (qp_cap_flags & OCRDMA_QP_INB_WR) |
| 1201 | ib_qp_acc_flags |= IB_ACCESS_REMOTE_WRITE; |
| 1202 | if (qp_cap_flags & OCRDMA_QP_INB_RD) |
| 1203 | ib_qp_acc_flags |= IB_ACCESS_LOCAL_WRITE; |
| 1204 | return ib_qp_acc_flags; |
| 1205 | } |
| 1206 | |
| 1207 | int ocrdma_query_qp(struct ib_qp *ibqp, |
| 1208 | struct ib_qp_attr *qp_attr, |
| 1209 | int attr_mask, struct ib_qp_init_attr *qp_init_attr) |
| 1210 | { |
| 1211 | int status; |
| 1212 | u32 qp_state; |
| 1213 | struct ocrdma_qp_params params; |
| 1214 | struct ocrdma_qp *qp = get_ocrdma_qp(ibqp); |
| 1215 | struct ocrdma_dev *dev = qp->dev; |
| 1216 | |
| 1217 | memset(¶ms, 0, sizeof(params)); |
| 1218 | mutex_lock(&dev->dev_lock); |
| 1219 | status = ocrdma_mbx_query_qp(dev, qp, ¶ms); |
| 1220 | mutex_unlock(&dev->dev_lock); |
| 1221 | if (status) |
| 1222 | goto mbx_err; |
| 1223 | qp_attr->qp_state = get_ibqp_state(IB_QPS_INIT); |
| 1224 | qp_attr->cur_qp_state = get_ibqp_state(IB_QPS_INIT); |
| 1225 | qp_attr->path_mtu = |
| 1226 | ocrdma_mtu_int_to_enum(params.path_mtu_pkey_indx & |
| 1227 | OCRDMA_QP_PARAMS_PATH_MTU_MASK) >> |
| 1228 | OCRDMA_QP_PARAMS_PATH_MTU_SHIFT; |
| 1229 | qp_attr->path_mig_state = IB_MIG_MIGRATED; |
| 1230 | qp_attr->rq_psn = params.hop_lmt_rq_psn & OCRDMA_QP_PARAMS_RQ_PSN_MASK; |
| 1231 | qp_attr->sq_psn = params.tclass_sq_psn & OCRDMA_QP_PARAMS_SQ_PSN_MASK; |
| 1232 | qp_attr->dest_qp_num = |
| 1233 | params.ack_to_rnr_rtc_dest_qpn & OCRDMA_QP_PARAMS_DEST_QPN_MASK; |
| 1234 | |
| 1235 | qp_attr->qp_access_flags = ocrdma_to_ib_qp_acc_flags(qp->cap_flags); |
| 1236 | qp_attr->cap.max_send_wr = qp->sq.max_cnt - 1; |
| 1237 | qp_attr->cap.max_recv_wr = qp->rq.max_cnt - 1; |
| 1238 | qp_attr->cap.max_send_sge = qp->sq.max_sges; |
| 1239 | qp_attr->cap.max_recv_sge = qp->rq.max_sges; |
| 1240 | qp_attr->cap.max_inline_data = dev->attr.max_inline_data; |
| 1241 | qp_init_attr->cap = qp_attr->cap; |
| 1242 | memcpy(&qp_attr->ah_attr.grh.dgid, ¶ms.dgid[0], |
| 1243 | sizeof(params.dgid)); |
| 1244 | qp_attr->ah_attr.grh.flow_label = params.rnt_rc_sl_fl & |
| 1245 | OCRDMA_QP_PARAMS_FLOW_LABEL_MASK; |
| 1246 | qp_attr->ah_attr.grh.sgid_index = qp->sgid_idx; |
| 1247 | qp_attr->ah_attr.grh.hop_limit = (params.hop_lmt_rq_psn & |
| 1248 | OCRDMA_QP_PARAMS_HOP_LMT_MASK) >> |
| 1249 | OCRDMA_QP_PARAMS_HOP_LMT_SHIFT; |
| 1250 | qp_attr->ah_attr.grh.traffic_class = (params.tclass_sq_psn & |
| 1251 | OCRDMA_QP_PARAMS_SQ_PSN_MASK) >> |
| 1252 | OCRDMA_QP_PARAMS_TCLASS_SHIFT; |
| 1253 | |
| 1254 | qp_attr->ah_attr.ah_flags = IB_AH_GRH; |
| 1255 | qp_attr->ah_attr.port_num = 1; |
| 1256 | qp_attr->ah_attr.sl = (params.rnt_rc_sl_fl & |
| 1257 | OCRDMA_QP_PARAMS_SL_MASK) >> |
| 1258 | OCRDMA_QP_PARAMS_SL_SHIFT; |
| 1259 | qp_attr->timeout = (params.ack_to_rnr_rtc_dest_qpn & |
| 1260 | OCRDMA_QP_PARAMS_ACK_TIMEOUT_MASK) >> |
| 1261 | OCRDMA_QP_PARAMS_ACK_TIMEOUT_SHIFT; |
| 1262 | qp_attr->rnr_retry = (params.ack_to_rnr_rtc_dest_qpn & |
| 1263 | OCRDMA_QP_PARAMS_RNR_RETRY_CNT_MASK) >> |
| 1264 | OCRDMA_QP_PARAMS_RNR_RETRY_CNT_SHIFT; |
| 1265 | qp_attr->retry_cnt = |
| 1266 | (params.rnt_rc_sl_fl & OCRDMA_QP_PARAMS_RETRY_CNT_MASK) >> |
| 1267 | OCRDMA_QP_PARAMS_RETRY_CNT_SHIFT; |
| 1268 | qp_attr->min_rnr_timer = 0; |
| 1269 | qp_attr->pkey_index = 0; |
| 1270 | qp_attr->port_num = 1; |
| 1271 | qp_attr->ah_attr.src_path_bits = 0; |
| 1272 | qp_attr->ah_attr.static_rate = 0; |
| 1273 | qp_attr->alt_pkey_index = 0; |
| 1274 | qp_attr->alt_port_num = 0; |
| 1275 | qp_attr->alt_timeout = 0; |
| 1276 | memset(&qp_attr->alt_ah_attr, 0, sizeof(qp_attr->alt_ah_attr)); |
| 1277 | qp_state = (params.max_sge_recv_flags & OCRDMA_QP_PARAMS_STATE_MASK) >> |
| 1278 | OCRDMA_QP_PARAMS_STATE_SHIFT; |
| 1279 | qp_attr->sq_draining = (qp_state == OCRDMA_QPS_SQ_DRAINING) ? 1 : 0; |
| 1280 | qp_attr->max_dest_rd_atomic = |
| 1281 | params.max_ord_ird >> OCRDMA_QP_PARAMS_MAX_ORD_SHIFT; |
| 1282 | qp_attr->max_rd_atomic = |
| 1283 | params.max_ord_ird & OCRDMA_QP_PARAMS_MAX_IRD_MASK; |
| 1284 | qp_attr->en_sqd_async_notify = (params.max_sge_recv_flags & |
| 1285 | OCRDMA_QP_PARAMS_FLAGS_SQD_ASYNC) ? 1 : 0; |
| 1286 | mbx_err: |
| 1287 | return status; |
| 1288 | } |
| 1289 | |
| 1290 | static void ocrdma_srq_toggle_bit(struct ocrdma_srq *srq, int idx) |
| 1291 | { |
| 1292 | int i = idx / 32; |
| 1293 | unsigned int mask = (1 << (idx % 32)); |
| 1294 | |
| 1295 | if (srq->idx_bit_fields[i] & mask) |
| 1296 | srq->idx_bit_fields[i] &= ~mask; |
| 1297 | else |
| 1298 | srq->idx_bit_fields[i] |= mask; |
| 1299 | } |
| 1300 | |
| 1301 | static int ocrdma_hwq_free_cnt(struct ocrdma_qp_hwq_info *q) |
| 1302 | { |
| 1303 | int free_cnt; |
| 1304 | if (q->head >= q->tail) |
| 1305 | free_cnt = (q->max_cnt - q->head) + q->tail; |
| 1306 | else |
| 1307 | free_cnt = q->tail - q->head; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1308 | return free_cnt; |
| 1309 | } |
| 1310 | |
| 1311 | static int is_hw_sq_empty(struct ocrdma_qp *qp) |
| 1312 | { |
| 1313 | return (qp->sq.tail == qp->sq.head && |
| 1314 | ocrdma_hwq_free_cnt(&qp->sq) ? 1 : 0); |
| 1315 | } |
| 1316 | |
| 1317 | static int is_hw_rq_empty(struct ocrdma_qp *qp) |
| 1318 | { |
| 1319 | return (qp->rq.tail == qp->rq.head) ? 1 : 0; |
| 1320 | } |
| 1321 | |
| 1322 | static void *ocrdma_hwq_head(struct ocrdma_qp_hwq_info *q) |
| 1323 | { |
| 1324 | return q->va + (q->head * q->entry_size); |
| 1325 | } |
| 1326 | |
| 1327 | static void *ocrdma_hwq_head_from_idx(struct ocrdma_qp_hwq_info *q, |
| 1328 | u32 idx) |
| 1329 | { |
| 1330 | return q->va + (idx * q->entry_size); |
| 1331 | } |
| 1332 | |
| 1333 | static void ocrdma_hwq_inc_head(struct ocrdma_qp_hwq_info *q) |
| 1334 | { |
| 1335 | q->head = (q->head + 1) & q->max_wqe_idx; |
| 1336 | } |
| 1337 | |
| 1338 | static void ocrdma_hwq_inc_tail(struct ocrdma_qp_hwq_info *q) |
| 1339 | { |
| 1340 | q->tail = (q->tail + 1) & q->max_wqe_idx; |
| 1341 | } |
| 1342 | |
| 1343 | /* discard the cqe for a given QP */ |
| 1344 | static void ocrdma_discard_cqes(struct ocrdma_qp *qp, struct ocrdma_cq *cq) |
| 1345 | { |
| 1346 | unsigned long cq_flags; |
| 1347 | unsigned long flags; |
| 1348 | int discard_cnt = 0; |
| 1349 | u32 cur_getp, stop_getp; |
| 1350 | struct ocrdma_cqe *cqe; |
| 1351 | u32 qpn = 0; |
| 1352 | |
| 1353 | spin_lock_irqsave(&cq->cq_lock, cq_flags); |
| 1354 | |
| 1355 | /* traverse through the CQEs in the hw CQ, |
| 1356 | * find the matching CQE for a given qp, |
| 1357 | * mark the matching one discarded by clearing qpn. |
| 1358 | * ring the doorbell in the poll_cq() as |
| 1359 | * we don't complete out of order cqe. |
| 1360 | */ |
| 1361 | |
| 1362 | cur_getp = cq->getp; |
| 1363 | /* find upto when do we reap the cq. */ |
| 1364 | stop_getp = cur_getp; |
| 1365 | do { |
| 1366 | if (is_hw_sq_empty(qp) && (!qp->srq && is_hw_rq_empty(qp))) |
| 1367 | break; |
| 1368 | |
| 1369 | cqe = cq->va + cur_getp; |
| 1370 | /* if (a) done reaping whole hw cq, or |
| 1371 | * (b) qp_xq becomes empty. |
| 1372 | * then exit |
| 1373 | */ |
| 1374 | qpn = cqe->cmn.qpn & OCRDMA_CQE_QPN_MASK; |
| 1375 | /* if previously discarded cqe found, skip that too. */ |
| 1376 | /* check for matching qp */ |
| 1377 | if (qpn == 0 || qpn != qp->id) |
| 1378 | goto skip_cqe; |
| 1379 | |
| 1380 | /* mark cqe discarded so that it is not picked up later |
| 1381 | * in the poll_cq(). |
| 1382 | */ |
| 1383 | discard_cnt += 1; |
| 1384 | cqe->cmn.qpn = 0; |
| 1385 | if (is_cqe_for_sq(cqe)) |
| 1386 | ocrdma_hwq_inc_tail(&qp->sq); |
| 1387 | else { |
| 1388 | if (qp->srq) { |
| 1389 | spin_lock_irqsave(&qp->srq->q_lock, flags); |
| 1390 | ocrdma_hwq_inc_tail(&qp->srq->rq); |
| 1391 | ocrdma_srq_toggle_bit(qp->srq, cur_getp); |
| 1392 | spin_unlock_irqrestore(&qp->srq->q_lock, flags); |
| 1393 | |
| 1394 | } else |
| 1395 | ocrdma_hwq_inc_tail(&qp->rq); |
| 1396 | } |
| 1397 | skip_cqe: |
| 1398 | cur_getp = (cur_getp + 1) % cq->max_hw_cqe; |
| 1399 | } while (cur_getp != stop_getp); |
| 1400 | spin_unlock_irqrestore(&cq->cq_lock, cq_flags); |
| 1401 | } |
| 1402 | |
| 1403 | static void ocrdma_del_flush_qp(struct ocrdma_qp *qp) |
| 1404 | { |
| 1405 | int found = false; |
| 1406 | unsigned long flags; |
| 1407 | struct ocrdma_dev *dev = qp->dev; |
| 1408 | /* sync with any active CQ poll */ |
| 1409 | |
| 1410 | spin_lock_irqsave(&dev->flush_q_lock, flags); |
| 1411 | found = ocrdma_is_qp_in_sq_flushlist(qp->sq_cq, qp); |
| 1412 | if (found) |
| 1413 | list_del(&qp->sq_entry); |
| 1414 | if (!qp->srq) { |
| 1415 | found = ocrdma_is_qp_in_rq_flushlist(qp->rq_cq, qp); |
| 1416 | if (found) |
| 1417 | list_del(&qp->rq_entry); |
| 1418 | } |
| 1419 | spin_unlock_irqrestore(&dev->flush_q_lock, flags); |
| 1420 | } |
| 1421 | |
| 1422 | int ocrdma_destroy_qp(struct ib_qp *ibqp) |
| 1423 | { |
| 1424 | int status; |
| 1425 | struct ocrdma_pd *pd; |
| 1426 | struct ocrdma_qp *qp; |
| 1427 | struct ocrdma_dev *dev; |
| 1428 | struct ib_qp_attr attrs; |
| 1429 | int attr_mask = IB_QP_STATE; |
Dan Carpenter | d19081e | 2012-05-02 09:14:47 +0300 | [diff] [blame] | 1430 | unsigned long flags; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1431 | |
| 1432 | qp = get_ocrdma_qp(ibqp); |
| 1433 | dev = qp->dev; |
| 1434 | |
| 1435 | attrs.qp_state = IB_QPS_ERR; |
| 1436 | pd = qp->pd; |
| 1437 | |
| 1438 | /* change the QP state to ERROR */ |
| 1439 | _ocrdma_modify_qp(ibqp, &attrs, attr_mask); |
| 1440 | |
| 1441 | /* ensure that CQEs for newly created QP (whose id may be same with |
| 1442 | * one which just getting destroyed are same), dont get |
| 1443 | * discarded until the old CQEs are discarded. |
| 1444 | */ |
| 1445 | mutex_lock(&dev->dev_lock); |
| 1446 | status = ocrdma_mbx_destroy_qp(dev, qp); |
| 1447 | |
| 1448 | /* |
| 1449 | * acquire CQ lock while destroy is in progress, in order to |
| 1450 | * protect against proessing in-flight CQEs for this QP. |
| 1451 | */ |
Dan Carpenter | d19081e | 2012-05-02 09:14:47 +0300 | [diff] [blame] | 1452 | spin_lock_irqsave(&qp->sq_cq->cq_lock, flags); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1453 | if (qp->rq_cq && (qp->rq_cq != qp->sq_cq)) |
Dan Carpenter | d19081e | 2012-05-02 09:14:47 +0300 | [diff] [blame] | 1454 | spin_lock(&qp->rq_cq->cq_lock); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1455 | |
| 1456 | ocrdma_del_qpn_map(dev, qp); |
| 1457 | |
| 1458 | if (qp->rq_cq && (qp->rq_cq != qp->sq_cq)) |
Dan Carpenter | d19081e | 2012-05-02 09:14:47 +0300 | [diff] [blame] | 1459 | spin_unlock(&qp->rq_cq->cq_lock); |
| 1460 | spin_unlock_irqrestore(&qp->sq_cq->cq_lock, flags); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1461 | |
| 1462 | if (!pd->uctx) { |
| 1463 | ocrdma_discard_cqes(qp, qp->sq_cq); |
| 1464 | ocrdma_discard_cqes(qp, qp->rq_cq); |
| 1465 | } |
| 1466 | mutex_unlock(&dev->dev_lock); |
| 1467 | |
| 1468 | if (pd->uctx) { |
| 1469 | ocrdma_del_mmap(pd->uctx, (u64) qp->sq.pa, qp->sq.len); |
| 1470 | if (!qp->srq) |
| 1471 | ocrdma_del_mmap(pd->uctx, (u64) qp->rq.pa, qp->rq.len); |
| 1472 | } |
| 1473 | |
| 1474 | ocrdma_del_flush_qp(qp); |
| 1475 | |
| 1476 | atomic_dec(&qp->pd->use_cnt); |
| 1477 | atomic_dec(&qp->sq_cq->use_cnt); |
| 1478 | atomic_dec(&qp->rq_cq->use_cnt); |
| 1479 | if (qp->srq) |
| 1480 | atomic_dec(&qp->srq->use_cnt); |
| 1481 | kfree(qp->wqe_wr_id_tbl); |
| 1482 | kfree(qp->rqe_wr_id_tbl); |
| 1483 | kfree(qp); |
| 1484 | return status; |
| 1485 | } |
| 1486 | |
| 1487 | static int ocrdma_copy_srq_uresp(struct ocrdma_srq *srq, struct ib_udata *udata) |
| 1488 | { |
| 1489 | int status; |
| 1490 | struct ocrdma_create_srq_uresp uresp; |
| 1491 | |
| 1492 | uresp.rq_dbid = srq->rq.dbid; |
| 1493 | uresp.num_rq_pages = 1; |
| 1494 | uresp.rq_page_addr[0] = srq->rq.pa; |
| 1495 | uresp.rq_page_size = srq->rq.len; |
| 1496 | uresp.db_page_addr = srq->dev->nic_info.unmapped_db + |
| 1497 | (srq->pd->id * srq->dev->nic_info.db_page_size); |
| 1498 | uresp.db_page_size = srq->dev->nic_info.db_page_size; |
| 1499 | uresp.num_rqe_allocated = srq->rq.max_cnt; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1500 | if (srq->dev->nic_info.dev_family == OCRDMA_GEN2_FAMILY) { |
| 1501 | uresp.db_rq_offset = OCRDMA_DB_GEN2_RQ1_OFFSET; |
| 1502 | uresp.db_shift = 24; |
| 1503 | } else { |
| 1504 | uresp.db_rq_offset = OCRDMA_DB_RQ_OFFSET; |
| 1505 | uresp.db_shift = 16; |
| 1506 | } |
| 1507 | |
| 1508 | status = ib_copy_to_udata(udata, &uresp, sizeof(uresp)); |
| 1509 | if (status) |
| 1510 | return status; |
| 1511 | status = ocrdma_add_mmap(srq->pd->uctx, uresp.rq_page_addr[0], |
| 1512 | uresp.rq_page_size); |
| 1513 | if (status) |
| 1514 | return status; |
| 1515 | return status; |
| 1516 | } |
| 1517 | |
| 1518 | struct ib_srq *ocrdma_create_srq(struct ib_pd *ibpd, |
| 1519 | struct ib_srq_init_attr *init_attr, |
| 1520 | struct ib_udata *udata) |
| 1521 | { |
| 1522 | int status = -ENOMEM; |
| 1523 | struct ocrdma_pd *pd = get_ocrdma_pd(ibpd); |
| 1524 | struct ocrdma_dev *dev = pd->dev; |
| 1525 | struct ocrdma_srq *srq; |
| 1526 | |
| 1527 | if (init_attr->attr.max_sge > dev->attr.max_recv_sge) |
| 1528 | return ERR_PTR(-EINVAL); |
| 1529 | if (init_attr->attr.max_wr > dev->attr.max_rqe) |
| 1530 | return ERR_PTR(-EINVAL); |
| 1531 | |
| 1532 | srq = kzalloc(sizeof(*srq), GFP_KERNEL); |
| 1533 | if (!srq) |
| 1534 | return ERR_PTR(status); |
| 1535 | |
| 1536 | spin_lock_init(&srq->q_lock); |
| 1537 | srq->dev = dev; |
| 1538 | srq->pd = pd; |
| 1539 | srq->db = dev->nic_info.db + (pd->id * dev->nic_info.db_page_size); |
| 1540 | status = ocrdma_mbx_create_srq(srq, init_attr, pd); |
| 1541 | if (status) |
| 1542 | goto err; |
| 1543 | |
| 1544 | if (udata == NULL) { |
| 1545 | srq->rqe_wr_id_tbl = kzalloc(sizeof(u64) * srq->rq.max_cnt, |
| 1546 | GFP_KERNEL); |
| 1547 | if (srq->rqe_wr_id_tbl == NULL) |
| 1548 | goto arm_err; |
| 1549 | |
| 1550 | srq->bit_fields_len = (srq->rq.max_cnt / 32) + |
| 1551 | (srq->rq.max_cnt % 32 ? 1 : 0); |
| 1552 | srq->idx_bit_fields = |
| 1553 | kmalloc(srq->bit_fields_len * sizeof(u32), GFP_KERNEL); |
| 1554 | if (srq->idx_bit_fields == NULL) |
| 1555 | goto arm_err; |
| 1556 | memset(srq->idx_bit_fields, 0xff, |
| 1557 | srq->bit_fields_len * sizeof(u32)); |
| 1558 | } |
| 1559 | |
| 1560 | if (init_attr->attr.srq_limit) { |
| 1561 | status = ocrdma_mbx_modify_srq(srq, &init_attr->attr); |
| 1562 | if (status) |
| 1563 | goto arm_err; |
| 1564 | } |
| 1565 | |
| 1566 | atomic_set(&srq->use_cnt, 0); |
| 1567 | if (udata) { |
| 1568 | status = ocrdma_copy_srq_uresp(srq, udata); |
| 1569 | if (status) |
| 1570 | goto arm_err; |
| 1571 | } |
| 1572 | |
| 1573 | atomic_inc(&pd->use_cnt); |
| 1574 | return &srq->ibsrq; |
| 1575 | |
| 1576 | arm_err: |
| 1577 | ocrdma_mbx_destroy_srq(dev, srq); |
| 1578 | err: |
| 1579 | kfree(srq->rqe_wr_id_tbl); |
| 1580 | kfree(srq->idx_bit_fields); |
| 1581 | kfree(srq); |
| 1582 | return ERR_PTR(status); |
| 1583 | } |
| 1584 | |
| 1585 | int ocrdma_modify_srq(struct ib_srq *ibsrq, |
| 1586 | struct ib_srq_attr *srq_attr, |
| 1587 | enum ib_srq_attr_mask srq_attr_mask, |
| 1588 | struct ib_udata *udata) |
| 1589 | { |
| 1590 | int status = 0; |
| 1591 | struct ocrdma_srq *srq; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1592 | |
| 1593 | srq = get_ocrdma_srq(ibsrq); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1594 | if (srq_attr_mask & IB_SRQ_MAX_WR) |
| 1595 | status = -EINVAL; |
| 1596 | else |
| 1597 | status = ocrdma_mbx_modify_srq(srq, srq_attr); |
| 1598 | return status; |
| 1599 | } |
| 1600 | |
| 1601 | int ocrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr) |
| 1602 | { |
| 1603 | int status; |
| 1604 | struct ocrdma_srq *srq; |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1605 | |
| 1606 | srq = get_ocrdma_srq(ibsrq); |
Parav Pandit | fe2caef | 2012-03-21 04:09:06 +0530 | [diff] [blame] | 1607 | status = ocrdma_mbx_query_srq(srq, srq_attr); |
| 1608 | return status; |
| 1609 | } |
| 1610 | |
| 1611 | int ocrdma_destroy_srq(struct ib_srq *ibsrq) |
| 1612 | { |
| 1613 | int status; |
| 1614 | struct ocrdma_srq *srq; |
| 1615 | struct ocrdma_dev *dev; |
| 1616 | |
| 1617 | srq = get_ocrdma_srq(ibsrq); |
| 1618 | dev = srq->dev; |
| 1619 | if (atomic_read(&srq->use_cnt)) { |
| 1620 | ocrdma_err("%s(%d) err, srq=0x%x in use\n", |
| 1621 | __func__, dev->id, srq->id); |
| 1622 | return -EAGAIN; |
| 1623 | } |
| 1624 | |
| 1625 | status = ocrdma_mbx_destroy_srq(dev, srq); |
| 1626 | |
| 1627 | if (srq->pd->uctx) |
| 1628 | ocrdma_del_mmap(srq->pd->uctx, (u64) srq->rq.pa, srq->rq.len); |
| 1629 | |
| 1630 | atomic_dec(&srq->pd->use_cnt); |
| 1631 | kfree(srq->idx_bit_fields); |
| 1632 | kfree(srq->rqe_wr_id_tbl); |
| 1633 | kfree(srq); |
| 1634 | return status; |
| 1635 | } |
| 1636 | |
| 1637 | /* unprivileged verbs and their support functions. */ |
| 1638 | static void ocrdma_build_ud_hdr(struct ocrdma_qp *qp, |
| 1639 | struct ocrdma_hdr_wqe *hdr, |
| 1640 | struct ib_send_wr *wr) |
| 1641 | { |
| 1642 | struct ocrdma_ewqe_ud_hdr *ud_hdr = |
| 1643 | (struct ocrdma_ewqe_ud_hdr *)(hdr + 1); |
| 1644 | struct ocrdma_ah *ah = get_ocrdma_ah(wr->wr.ud.ah); |
| 1645 | |
| 1646 | ud_hdr->rsvd_dest_qpn = wr->wr.ud.remote_qpn; |
| 1647 | if (qp->qp_type == IB_QPT_GSI) |
| 1648 | ud_hdr->qkey = qp->qkey; |
| 1649 | else |
| 1650 | ud_hdr->qkey = wr->wr.ud.remote_qkey; |
| 1651 | ud_hdr->rsvd_ahid = ah->id; |
| 1652 | } |
| 1653 | |
| 1654 | static void ocrdma_build_sges(struct ocrdma_hdr_wqe *hdr, |
| 1655 | struct ocrdma_sge *sge, int num_sge, |
| 1656 | struct ib_sge *sg_list) |
| 1657 | { |
| 1658 | int i; |
| 1659 | |
| 1660 | for (i = 0; i < num_sge; i++) { |
| 1661 | sge[i].lrkey = sg_list[i].lkey; |
| 1662 | sge[i].addr_lo = sg_list[i].addr; |
| 1663 | sge[i].addr_hi = upper_32_bits(sg_list[i].addr); |
| 1664 | sge[i].len = sg_list[i].length; |
| 1665 | hdr->total_len += sg_list[i].length; |
| 1666 | } |
| 1667 | if (num_sge == 0) |
| 1668 | memset(sge, 0, sizeof(*sge)); |
| 1669 | } |
| 1670 | |
| 1671 | static int ocrdma_build_inline_sges(struct ocrdma_qp *qp, |
| 1672 | struct ocrdma_hdr_wqe *hdr, |
| 1673 | struct ocrdma_sge *sge, |
| 1674 | struct ib_send_wr *wr, u32 wqe_size) |
| 1675 | { |
| 1676 | if (wr->send_flags & IB_SEND_INLINE) { |
| 1677 | if (wr->sg_list[0].length > qp->max_inline_data) { |
| 1678 | ocrdma_err("%s() supported_len=0x%x," |
| 1679 | " unspported len req=0x%x\n", __func__, |
| 1680 | qp->max_inline_data, wr->sg_list[0].length); |
| 1681 | return -EINVAL; |
| 1682 | } |
| 1683 | memcpy(sge, |
| 1684 | (void *)(unsigned long)wr->sg_list[0].addr, |
| 1685 | wr->sg_list[0].length); |
| 1686 | hdr->total_len = wr->sg_list[0].length; |
| 1687 | wqe_size += roundup(hdr->total_len, OCRDMA_WQE_ALIGN_BYTES); |
| 1688 | hdr->cw |= (OCRDMA_TYPE_INLINE << OCRDMA_WQE_TYPE_SHIFT); |
| 1689 | } else { |
| 1690 | ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list); |
| 1691 | if (wr->num_sge) |
| 1692 | wqe_size += (wr->num_sge * sizeof(struct ocrdma_sge)); |
| 1693 | else |
| 1694 | wqe_size += sizeof(struct ocrdma_sge); |
| 1695 | hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT); |
| 1696 | } |
| 1697 | hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT); |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | static int ocrdma_build_send(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr, |
| 1702 | struct ib_send_wr *wr) |
| 1703 | { |
| 1704 | int status; |
| 1705 | struct ocrdma_sge *sge; |
| 1706 | u32 wqe_size = sizeof(*hdr); |
| 1707 | |
| 1708 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) { |
| 1709 | ocrdma_build_ud_hdr(qp, hdr, wr); |
| 1710 | sge = (struct ocrdma_sge *)(hdr + 2); |
| 1711 | wqe_size += sizeof(struct ocrdma_ewqe_ud_hdr); |
| 1712 | } else |
| 1713 | sge = (struct ocrdma_sge *)(hdr + 1); |
| 1714 | |
| 1715 | status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size); |
| 1716 | return status; |
| 1717 | } |
| 1718 | |
| 1719 | static int ocrdma_build_write(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr, |
| 1720 | struct ib_send_wr *wr) |
| 1721 | { |
| 1722 | int status; |
| 1723 | struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1); |
| 1724 | struct ocrdma_sge *sge = ext_rw + 1; |
| 1725 | u32 wqe_size = sizeof(*hdr) + sizeof(*ext_rw); |
| 1726 | |
| 1727 | status = ocrdma_build_inline_sges(qp, hdr, sge, wr, wqe_size); |
| 1728 | if (status) |
| 1729 | return status; |
| 1730 | ext_rw->addr_lo = wr->wr.rdma.remote_addr; |
| 1731 | ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr); |
| 1732 | ext_rw->lrkey = wr->wr.rdma.rkey; |
| 1733 | ext_rw->len = hdr->total_len; |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | static void ocrdma_build_read(struct ocrdma_qp *qp, struct ocrdma_hdr_wqe *hdr, |
| 1738 | struct ib_send_wr *wr) |
| 1739 | { |
| 1740 | struct ocrdma_sge *ext_rw = (struct ocrdma_sge *)(hdr + 1); |
| 1741 | struct ocrdma_sge *sge = ext_rw + 1; |
| 1742 | u32 wqe_size = ((wr->num_sge + 1) * sizeof(struct ocrdma_sge)) + |
| 1743 | sizeof(struct ocrdma_hdr_wqe); |
| 1744 | |
| 1745 | ocrdma_build_sges(hdr, sge, wr->num_sge, wr->sg_list); |
| 1746 | hdr->cw |= ((wqe_size / OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT); |
| 1747 | hdr->cw |= (OCRDMA_READ << OCRDMA_WQE_OPCODE_SHIFT); |
| 1748 | hdr->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT); |
| 1749 | |
| 1750 | ext_rw->addr_lo = wr->wr.rdma.remote_addr; |
| 1751 | ext_rw->addr_hi = upper_32_bits(wr->wr.rdma.remote_addr); |
| 1752 | ext_rw->lrkey = wr->wr.rdma.rkey; |
| 1753 | ext_rw->len = hdr->total_len; |
| 1754 | } |
| 1755 | |
| 1756 | static void ocrdma_ring_sq_db(struct ocrdma_qp *qp) |
| 1757 | { |
| 1758 | u32 val = qp->sq.dbid | (1 << 16); |
| 1759 | |
| 1760 | iowrite32(val, qp->sq_db); |
| 1761 | } |
| 1762 | |
| 1763 | int ocrdma_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr, |
| 1764 | struct ib_send_wr **bad_wr) |
| 1765 | { |
| 1766 | int status = 0; |
| 1767 | struct ocrdma_qp *qp = get_ocrdma_qp(ibqp); |
| 1768 | struct ocrdma_hdr_wqe *hdr; |
| 1769 | unsigned long flags; |
| 1770 | |
| 1771 | spin_lock_irqsave(&qp->q_lock, flags); |
| 1772 | if (qp->state != OCRDMA_QPS_RTS && qp->state != OCRDMA_QPS_SQD) { |
| 1773 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1774 | return -EINVAL; |
| 1775 | } |
| 1776 | |
| 1777 | while (wr) { |
| 1778 | if (ocrdma_hwq_free_cnt(&qp->sq) == 0 || |
| 1779 | wr->num_sge > qp->sq.max_sges) { |
| 1780 | status = -ENOMEM; |
| 1781 | break; |
| 1782 | } |
| 1783 | hdr = ocrdma_hwq_head(&qp->sq); |
| 1784 | hdr->cw = 0; |
| 1785 | if (wr->send_flags & IB_SEND_SIGNALED) |
| 1786 | hdr->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT); |
| 1787 | if (wr->send_flags & IB_SEND_FENCE) |
| 1788 | hdr->cw |= |
| 1789 | (OCRDMA_FLAG_FENCE_L << OCRDMA_WQE_FLAGS_SHIFT); |
| 1790 | if (wr->send_flags & IB_SEND_SOLICITED) |
| 1791 | hdr->cw |= |
| 1792 | (OCRDMA_FLAG_SOLICIT << OCRDMA_WQE_FLAGS_SHIFT); |
| 1793 | hdr->total_len = 0; |
| 1794 | switch (wr->opcode) { |
| 1795 | case IB_WR_SEND_WITH_IMM: |
| 1796 | hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT); |
| 1797 | hdr->immdt = ntohl(wr->ex.imm_data); |
| 1798 | case IB_WR_SEND: |
| 1799 | hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT); |
| 1800 | ocrdma_build_send(qp, hdr, wr); |
| 1801 | break; |
| 1802 | case IB_WR_SEND_WITH_INV: |
| 1803 | hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT); |
| 1804 | hdr->cw |= (OCRDMA_SEND << OCRDMA_WQE_OPCODE_SHIFT); |
| 1805 | hdr->lkey = wr->ex.invalidate_rkey; |
| 1806 | status = ocrdma_build_send(qp, hdr, wr); |
| 1807 | break; |
| 1808 | case IB_WR_RDMA_WRITE_WITH_IMM: |
| 1809 | hdr->cw |= (OCRDMA_FLAG_IMM << OCRDMA_WQE_FLAGS_SHIFT); |
| 1810 | hdr->immdt = ntohl(wr->ex.imm_data); |
| 1811 | case IB_WR_RDMA_WRITE: |
| 1812 | hdr->cw |= (OCRDMA_WRITE << OCRDMA_WQE_OPCODE_SHIFT); |
| 1813 | status = ocrdma_build_write(qp, hdr, wr); |
| 1814 | break; |
| 1815 | case IB_WR_RDMA_READ_WITH_INV: |
| 1816 | hdr->cw |= (OCRDMA_FLAG_INV << OCRDMA_WQE_FLAGS_SHIFT); |
| 1817 | case IB_WR_RDMA_READ: |
| 1818 | ocrdma_build_read(qp, hdr, wr); |
| 1819 | break; |
| 1820 | case IB_WR_LOCAL_INV: |
| 1821 | hdr->cw |= |
| 1822 | (OCRDMA_LKEY_INV << OCRDMA_WQE_OPCODE_SHIFT); |
| 1823 | hdr->cw |= (sizeof(struct ocrdma_hdr_wqe) / |
| 1824 | OCRDMA_WQE_STRIDE) << OCRDMA_WQE_SIZE_SHIFT; |
| 1825 | hdr->lkey = wr->ex.invalidate_rkey; |
| 1826 | break; |
| 1827 | default: |
| 1828 | status = -EINVAL; |
| 1829 | break; |
| 1830 | } |
| 1831 | if (status) { |
| 1832 | *bad_wr = wr; |
| 1833 | break; |
| 1834 | } |
| 1835 | if (wr->send_flags & IB_SEND_SIGNALED) |
| 1836 | qp->wqe_wr_id_tbl[qp->sq.head].signaled = 1; |
| 1837 | else |
| 1838 | qp->wqe_wr_id_tbl[qp->sq.head].signaled = 0; |
| 1839 | qp->wqe_wr_id_tbl[qp->sq.head].wrid = wr->wr_id; |
| 1840 | ocrdma_cpu_to_le32(hdr, ((hdr->cw >> OCRDMA_WQE_SIZE_SHIFT) & |
| 1841 | OCRDMA_WQE_SIZE_MASK) * OCRDMA_WQE_STRIDE); |
| 1842 | /* make sure wqe is written before adapter can access it */ |
| 1843 | wmb(); |
| 1844 | /* inform hw to start processing it */ |
| 1845 | ocrdma_ring_sq_db(qp); |
| 1846 | |
| 1847 | /* update pointer, counter for next wr */ |
| 1848 | ocrdma_hwq_inc_head(&qp->sq); |
| 1849 | wr = wr->next; |
| 1850 | } |
| 1851 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1852 | return status; |
| 1853 | } |
| 1854 | |
| 1855 | static void ocrdma_ring_rq_db(struct ocrdma_qp *qp) |
| 1856 | { |
| 1857 | u32 val = qp->rq.dbid | (1 << OCRDMA_GET_NUM_POSTED_SHIFT_VAL(qp)); |
| 1858 | |
| 1859 | iowrite32(val, qp->rq_db); |
| 1860 | } |
| 1861 | |
| 1862 | static void ocrdma_build_rqe(struct ocrdma_hdr_wqe *rqe, struct ib_recv_wr *wr, |
| 1863 | u16 tag) |
| 1864 | { |
| 1865 | u32 wqe_size = 0; |
| 1866 | struct ocrdma_sge *sge; |
| 1867 | if (wr->num_sge) |
| 1868 | wqe_size = (wr->num_sge * sizeof(*sge)) + sizeof(*rqe); |
| 1869 | else |
| 1870 | wqe_size = sizeof(*sge) + sizeof(*rqe); |
| 1871 | |
| 1872 | rqe->cw = ((wqe_size / OCRDMA_WQE_STRIDE) << |
| 1873 | OCRDMA_WQE_SIZE_SHIFT); |
| 1874 | rqe->cw |= (OCRDMA_FLAG_SIG << OCRDMA_WQE_FLAGS_SHIFT); |
| 1875 | rqe->cw |= (OCRDMA_TYPE_LKEY << OCRDMA_WQE_TYPE_SHIFT); |
| 1876 | rqe->total_len = 0; |
| 1877 | rqe->rsvd_tag = tag; |
| 1878 | sge = (struct ocrdma_sge *)(rqe + 1); |
| 1879 | ocrdma_build_sges(rqe, sge, wr->num_sge, wr->sg_list); |
| 1880 | ocrdma_cpu_to_le32(rqe, wqe_size); |
| 1881 | } |
| 1882 | |
| 1883 | int ocrdma_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr, |
| 1884 | struct ib_recv_wr **bad_wr) |
| 1885 | { |
| 1886 | int status = 0; |
| 1887 | unsigned long flags; |
| 1888 | struct ocrdma_qp *qp = get_ocrdma_qp(ibqp); |
| 1889 | struct ocrdma_hdr_wqe *rqe; |
| 1890 | |
| 1891 | spin_lock_irqsave(&qp->q_lock, flags); |
| 1892 | if (qp->state == OCRDMA_QPS_RST || qp->state == OCRDMA_QPS_ERR) { |
| 1893 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1894 | *bad_wr = wr; |
| 1895 | return -EINVAL; |
| 1896 | } |
| 1897 | while (wr) { |
| 1898 | if (ocrdma_hwq_free_cnt(&qp->rq) == 0 || |
| 1899 | wr->num_sge > qp->rq.max_sges) { |
| 1900 | *bad_wr = wr; |
| 1901 | status = -ENOMEM; |
| 1902 | break; |
| 1903 | } |
| 1904 | rqe = ocrdma_hwq_head(&qp->rq); |
| 1905 | ocrdma_build_rqe(rqe, wr, 0); |
| 1906 | |
| 1907 | qp->rqe_wr_id_tbl[qp->rq.head] = wr->wr_id; |
| 1908 | /* make sure rqe is written before adapter can access it */ |
| 1909 | wmb(); |
| 1910 | |
| 1911 | /* inform hw to start processing it */ |
| 1912 | ocrdma_ring_rq_db(qp); |
| 1913 | |
| 1914 | /* update pointer, counter for next wr */ |
| 1915 | ocrdma_hwq_inc_head(&qp->rq); |
| 1916 | wr = wr->next; |
| 1917 | } |
| 1918 | spin_unlock_irqrestore(&qp->q_lock, flags); |
| 1919 | return status; |
| 1920 | } |
| 1921 | |
| 1922 | /* cqe for srq's rqe can potentially arrive out of order. |
| 1923 | * index gives the entry in the shadow table where to store |
| 1924 | * the wr_id. tag/index is returned in cqe to reference back |
| 1925 | * for a given rqe. |
| 1926 | */ |
| 1927 | static int ocrdma_srq_get_idx(struct ocrdma_srq *srq) |
| 1928 | { |
| 1929 | int row = 0; |
| 1930 | int indx = 0; |
| 1931 | |
| 1932 | for (row = 0; row < srq->bit_fields_len; row++) { |
| 1933 | if (srq->idx_bit_fields[row]) { |
| 1934 | indx = ffs(srq->idx_bit_fields[row]); |
| 1935 | indx = (row * 32) + (indx - 1); |
| 1936 | if (indx >= srq->rq.max_cnt) |
| 1937 | BUG(); |
| 1938 | ocrdma_srq_toggle_bit(srq, indx); |
| 1939 | break; |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | if (row == srq->bit_fields_len) |
| 1944 | BUG(); |
| 1945 | return indx; |
| 1946 | } |
| 1947 | |
| 1948 | static void ocrdma_ring_srq_db(struct ocrdma_srq *srq) |
| 1949 | { |
| 1950 | u32 val = srq->rq.dbid | (1 << 16); |
| 1951 | |
| 1952 | iowrite32(val, srq->db + OCRDMA_DB_GEN2_SRQ_OFFSET); |
| 1953 | } |
| 1954 | |
| 1955 | int ocrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr, |
| 1956 | struct ib_recv_wr **bad_wr) |
| 1957 | { |
| 1958 | int status = 0; |
| 1959 | unsigned long flags; |
| 1960 | struct ocrdma_srq *srq; |
| 1961 | struct ocrdma_hdr_wqe *rqe; |
| 1962 | u16 tag; |
| 1963 | |
| 1964 | srq = get_ocrdma_srq(ibsrq); |
| 1965 | |
| 1966 | spin_lock_irqsave(&srq->q_lock, flags); |
| 1967 | while (wr) { |
| 1968 | if (ocrdma_hwq_free_cnt(&srq->rq) == 0 || |
| 1969 | wr->num_sge > srq->rq.max_sges) { |
| 1970 | status = -ENOMEM; |
| 1971 | *bad_wr = wr; |
| 1972 | break; |
| 1973 | } |
| 1974 | tag = ocrdma_srq_get_idx(srq); |
| 1975 | rqe = ocrdma_hwq_head(&srq->rq); |
| 1976 | ocrdma_build_rqe(rqe, wr, tag); |
| 1977 | |
| 1978 | srq->rqe_wr_id_tbl[tag] = wr->wr_id; |
| 1979 | /* make sure rqe is written before adapter can perform DMA */ |
| 1980 | wmb(); |
| 1981 | /* inform hw to start processing it */ |
| 1982 | ocrdma_ring_srq_db(srq); |
| 1983 | /* update pointer, counter for next wr */ |
| 1984 | ocrdma_hwq_inc_head(&srq->rq); |
| 1985 | wr = wr->next; |
| 1986 | } |
| 1987 | spin_unlock_irqrestore(&srq->q_lock, flags); |
| 1988 | return status; |
| 1989 | } |
| 1990 | |
| 1991 | static enum ib_wc_status ocrdma_to_ibwc_err(u16 status) |
| 1992 | { |
| 1993 | enum ib_wc_status ibwc_status = IB_WC_GENERAL_ERR; |
| 1994 | |
| 1995 | switch (status) { |
| 1996 | case OCRDMA_CQE_GENERAL_ERR: |
| 1997 | ibwc_status = IB_WC_GENERAL_ERR; |
| 1998 | break; |
| 1999 | case OCRDMA_CQE_LOC_LEN_ERR: |
| 2000 | ibwc_status = IB_WC_LOC_LEN_ERR; |
| 2001 | break; |
| 2002 | case OCRDMA_CQE_LOC_QP_OP_ERR: |
| 2003 | ibwc_status = IB_WC_LOC_QP_OP_ERR; |
| 2004 | break; |
| 2005 | case OCRDMA_CQE_LOC_EEC_OP_ERR: |
| 2006 | ibwc_status = IB_WC_LOC_EEC_OP_ERR; |
| 2007 | break; |
| 2008 | case OCRDMA_CQE_LOC_PROT_ERR: |
| 2009 | ibwc_status = IB_WC_LOC_PROT_ERR; |
| 2010 | break; |
| 2011 | case OCRDMA_CQE_WR_FLUSH_ERR: |
| 2012 | ibwc_status = IB_WC_WR_FLUSH_ERR; |
| 2013 | break; |
| 2014 | case OCRDMA_CQE_MW_BIND_ERR: |
| 2015 | ibwc_status = IB_WC_MW_BIND_ERR; |
| 2016 | break; |
| 2017 | case OCRDMA_CQE_BAD_RESP_ERR: |
| 2018 | ibwc_status = IB_WC_BAD_RESP_ERR; |
| 2019 | break; |
| 2020 | case OCRDMA_CQE_LOC_ACCESS_ERR: |
| 2021 | ibwc_status = IB_WC_LOC_ACCESS_ERR; |
| 2022 | break; |
| 2023 | case OCRDMA_CQE_REM_INV_REQ_ERR: |
| 2024 | ibwc_status = IB_WC_REM_INV_REQ_ERR; |
| 2025 | break; |
| 2026 | case OCRDMA_CQE_REM_ACCESS_ERR: |
| 2027 | ibwc_status = IB_WC_REM_ACCESS_ERR; |
| 2028 | break; |
| 2029 | case OCRDMA_CQE_REM_OP_ERR: |
| 2030 | ibwc_status = IB_WC_REM_OP_ERR; |
| 2031 | break; |
| 2032 | case OCRDMA_CQE_RETRY_EXC_ERR: |
| 2033 | ibwc_status = IB_WC_RETRY_EXC_ERR; |
| 2034 | break; |
| 2035 | case OCRDMA_CQE_RNR_RETRY_EXC_ERR: |
| 2036 | ibwc_status = IB_WC_RNR_RETRY_EXC_ERR; |
| 2037 | break; |
| 2038 | case OCRDMA_CQE_LOC_RDD_VIOL_ERR: |
| 2039 | ibwc_status = IB_WC_LOC_RDD_VIOL_ERR; |
| 2040 | break; |
| 2041 | case OCRDMA_CQE_REM_INV_RD_REQ_ERR: |
| 2042 | ibwc_status = IB_WC_REM_INV_RD_REQ_ERR; |
| 2043 | break; |
| 2044 | case OCRDMA_CQE_REM_ABORT_ERR: |
| 2045 | ibwc_status = IB_WC_REM_ABORT_ERR; |
| 2046 | break; |
| 2047 | case OCRDMA_CQE_INV_EECN_ERR: |
| 2048 | ibwc_status = IB_WC_INV_EECN_ERR; |
| 2049 | break; |
| 2050 | case OCRDMA_CQE_INV_EEC_STATE_ERR: |
| 2051 | ibwc_status = IB_WC_INV_EEC_STATE_ERR; |
| 2052 | break; |
| 2053 | case OCRDMA_CQE_FATAL_ERR: |
| 2054 | ibwc_status = IB_WC_FATAL_ERR; |
| 2055 | break; |
| 2056 | case OCRDMA_CQE_RESP_TIMEOUT_ERR: |
| 2057 | ibwc_status = IB_WC_RESP_TIMEOUT_ERR; |
| 2058 | break; |
| 2059 | default: |
| 2060 | ibwc_status = IB_WC_GENERAL_ERR; |
| 2061 | break; |
| 2062 | }; |
| 2063 | return ibwc_status; |
| 2064 | } |
| 2065 | |
| 2066 | static void ocrdma_update_wc(struct ocrdma_qp *qp, struct ib_wc *ibwc, |
| 2067 | u32 wqe_idx) |
| 2068 | { |
| 2069 | struct ocrdma_hdr_wqe *hdr; |
| 2070 | struct ocrdma_sge *rw; |
| 2071 | int opcode; |
| 2072 | |
| 2073 | hdr = ocrdma_hwq_head_from_idx(&qp->sq, wqe_idx); |
| 2074 | |
| 2075 | ibwc->wr_id = qp->wqe_wr_id_tbl[wqe_idx].wrid; |
| 2076 | /* Undo the hdr->cw swap */ |
| 2077 | opcode = le32_to_cpu(hdr->cw) & OCRDMA_WQE_OPCODE_MASK; |
| 2078 | switch (opcode) { |
| 2079 | case OCRDMA_WRITE: |
| 2080 | ibwc->opcode = IB_WC_RDMA_WRITE; |
| 2081 | break; |
| 2082 | case OCRDMA_READ: |
| 2083 | rw = (struct ocrdma_sge *)(hdr + 1); |
| 2084 | ibwc->opcode = IB_WC_RDMA_READ; |
| 2085 | ibwc->byte_len = rw->len; |
| 2086 | break; |
| 2087 | case OCRDMA_SEND: |
| 2088 | ibwc->opcode = IB_WC_SEND; |
| 2089 | break; |
| 2090 | case OCRDMA_LKEY_INV: |
| 2091 | ibwc->opcode = IB_WC_LOCAL_INV; |
| 2092 | break; |
| 2093 | default: |
| 2094 | ibwc->status = IB_WC_GENERAL_ERR; |
| 2095 | ocrdma_err("%s() invalid opcode received = 0x%x\n", |
| 2096 | __func__, hdr->cw & OCRDMA_WQE_OPCODE_MASK); |
| 2097 | break; |
| 2098 | }; |
| 2099 | } |
| 2100 | |
| 2101 | static void ocrdma_set_cqe_status_flushed(struct ocrdma_qp *qp, |
| 2102 | struct ocrdma_cqe *cqe) |
| 2103 | { |
| 2104 | if (is_cqe_for_sq(cqe)) { |
| 2105 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2106 | cqe->flags_status_srcqpn) & |
| 2107 | ~OCRDMA_CQE_STATUS_MASK); |
| 2108 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2109 | cqe->flags_status_srcqpn) | |
| 2110 | (OCRDMA_CQE_WR_FLUSH_ERR << |
| 2111 | OCRDMA_CQE_STATUS_SHIFT)); |
| 2112 | } else { |
| 2113 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) { |
| 2114 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2115 | cqe->flags_status_srcqpn) & |
| 2116 | ~OCRDMA_CQE_UD_STATUS_MASK); |
| 2117 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2118 | cqe->flags_status_srcqpn) | |
| 2119 | (OCRDMA_CQE_WR_FLUSH_ERR << |
| 2120 | OCRDMA_CQE_UD_STATUS_SHIFT)); |
| 2121 | } else { |
| 2122 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2123 | cqe->flags_status_srcqpn) & |
| 2124 | ~OCRDMA_CQE_STATUS_MASK); |
| 2125 | cqe->flags_status_srcqpn = cpu_to_le32(le32_to_cpu( |
| 2126 | cqe->flags_status_srcqpn) | |
| 2127 | (OCRDMA_CQE_WR_FLUSH_ERR << |
| 2128 | OCRDMA_CQE_STATUS_SHIFT)); |
| 2129 | } |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | static bool ocrdma_update_err_cqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe, |
| 2134 | struct ocrdma_qp *qp, int status) |
| 2135 | { |
| 2136 | bool expand = false; |
| 2137 | |
| 2138 | ibwc->byte_len = 0; |
| 2139 | ibwc->qp = &qp->ibqp; |
| 2140 | ibwc->status = ocrdma_to_ibwc_err(status); |
| 2141 | |
| 2142 | ocrdma_flush_qp(qp); |
| 2143 | ocrdma_qp_state_machine(qp, IB_QPS_ERR, NULL); |
| 2144 | |
| 2145 | /* if wqe/rqe pending for which cqe needs to be returned, |
| 2146 | * trigger inflating it. |
| 2147 | */ |
| 2148 | if (!is_hw_rq_empty(qp) || !is_hw_sq_empty(qp)) { |
| 2149 | expand = true; |
| 2150 | ocrdma_set_cqe_status_flushed(qp, cqe); |
| 2151 | } |
| 2152 | return expand; |
| 2153 | } |
| 2154 | |
| 2155 | static int ocrdma_update_err_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe, |
| 2156 | struct ocrdma_qp *qp, int status) |
| 2157 | { |
| 2158 | ibwc->opcode = IB_WC_RECV; |
| 2159 | ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail]; |
| 2160 | ocrdma_hwq_inc_tail(&qp->rq); |
| 2161 | |
| 2162 | return ocrdma_update_err_cqe(ibwc, cqe, qp, status); |
| 2163 | } |
| 2164 | |
| 2165 | static int ocrdma_update_err_scqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe, |
| 2166 | struct ocrdma_qp *qp, int status) |
| 2167 | { |
| 2168 | ocrdma_update_wc(qp, ibwc, qp->sq.tail); |
| 2169 | ocrdma_hwq_inc_tail(&qp->sq); |
| 2170 | |
| 2171 | return ocrdma_update_err_cqe(ibwc, cqe, qp, status); |
| 2172 | } |
| 2173 | |
| 2174 | |
| 2175 | static bool ocrdma_poll_err_scqe(struct ocrdma_qp *qp, |
| 2176 | struct ocrdma_cqe *cqe, struct ib_wc *ibwc, |
| 2177 | bool *polled, bool *stop) |
| 2178 | { |
| 2179 | bool expand; |
| 2180 | int status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2181 | OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT; |
| 2182 | |
| 2183 | /* when hw sq is empty, but rq is not empty, so we continue |
| 2184 | * to keep the cqe in order to get the cq event again. |
| 2185 | */ |
| 2186 | if (is_hw_sq_empty(qp) && !is_hw_rq_empty(qp)) { |
| 2187 | /* when cq for rq and sq is same, it is safe to return |
| 2188 | * flush cqe for RQEs. |
| 2189 | */ |
| 2190 | if (!qp->srq && (qp->sq_cq == qp->rq_cq)) { |
| 2191 | *polled = true; |
| 2192 | status = OCRDMA_CQE_WR_FLUSH_ERR; |
| 2193 | expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status); |
| 2194 | } else { |
| 2195 | /* stop processing further cqe as this cqe is used for |
| 2196 | * triggering cq event on buddy cq of RQ. |
| 2197 | * When QP is destroyed, this cqe will be removed |
| 2198 | * from the cq's hardware q. |
| 2199 | */ |
| 2200 | *polled = false; |
| 2201 | *stop = true; |
| 2202 | expand = false; |
| 2203 | } |
| 2204 | } else { |
| 2205 | *polled = true; |
| 2206 | expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status); |
| 2207 | } |
| 2208 | return expand; |
| 2209 | } |
| 2210 | |
| 2211 | static bool ocrdma_poll_success_scqe(struct ocrdma_qp *qp, |
| 2212 | struct ocrdma_cqe *cqe, |
| 2213 | struct ib_wc *ibwc, bool *polled) |
| 2214 | { |
| 2215 | bool expand = false; |
| 2216 | int tail = qp->sq.tail; |
| 2217 | u32 wqe_idx; |
| 2218 | |
| 2219 | if (!qp->wqe_wr_id_tbl[tail].signaled) { |
| 2220 | expand = true; /* CQE cannot be consumed yet */ |
| 2221 | *polled = false; /* WC cannot be consumed yet */ |
| 2222 | } else { |
| 2223 | ibwc->status = IB_WC_SUCCESS; |
| 2224 | ibwc->wc_flags = 0; |
| 2225 | ibwc->qp = &qp->ibqp; |
| 2226 | ocrdma_update_wc(qp, ibwc, tail); |
| 2227 | *polled = true; |
| 2228 | wqe_idx = le32_to_cpu(cqe->wq.wqeidx) & OCRDMA_CQE_WQEIDX_MASK; |
| 2229 | if (tail != wqe_idx) |
| 2230 | expand = true; /* Coalesced CQE can't be consumed yet */ |
| 2231 | } |
| 2232 | ocrdma_hwq_inc_tail(&qp->sq); |
| 2233 | return expand; |
| 2234 | } |
| 2235 | |
| 2236 | static bool ocrdma_poll_scqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe, |
| 2237 | struct ib_wc *ibwc, bool *polled, bool *stop) |
| 2238 | { |
| 2239 | int status; |
| 2240 | bool expand; |
| 2241 | |
| 2242 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2243 | OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT; |
| 2244 | |
| 2245 | if (status == OCRDMA_CQE_SUCCESS) |
| 2246 | expand = ocrdma_poll_success_scqe(qp, cqe, ibwc, polled); |
| 2247 | else |
| 2248 | expand = ocrdma_poll_err_scqe(qp, cqe, ibwc, polled, stop); |
| 2249 | return expand; |
| 2250 | } |
| 2251 | |
| 2252 | static int ocrdma_update_ud_rcqe(struct ib_wc *ibwc, struct ocrdma_cqe *cqe) |
| 2253 | { |
| 2254 | int status; |
| 2255 | |
| 2256 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2257 | OCRDMA_CQE_UD_STATUS_MASK) >> OCRDMA_CQE_UD_STATUS_SHIFT; |
| 2258 | ibwc->src_qp = le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2259 | OCRDMA_CQE_SRCQP_MASK; |
| 2260 | ibwc->pkey_index = le32_to_cpu(cqe->ud.rxlen_pkey) & |
| 2261 | OCRDMA_CQE_PKEY_MASK; |
| 2262 | ibwc->wc_flags = IB_WC_GRH; |
| 2263 | ibwc->byte_len = (le32_to_cpu(cqe->ud.rxlen_pkey) >> |
| 2264 | OCRDMA_CQE_UD_XFER_LEN_SHIFT); |
| 2265 | return status; |
| 2266 | } |
| 2267 | |
| 2268 | static void ocrdma_update_free_srq_cqe(struct ib_wc *ibwc, |
| 2269 | struct ocrdma_cqe *cqe, |
| 2270 | struct ocrdma_qp *qp) |
| 2271 | { |
| 2272 | unsigned long flags; |
| 2273 | struct ocrdma_srq *srq; |
| 2274 | u32 wqe_idx; |
| 2275 | |
| 2276 | srq = get_ocrdma_srq(qp->ibqp.srq); |
| 2277 | wqe_idx = le32_to_cpu(cqe->rq.buftag_qpn) >> OCRDMA_CQE_BUFTAG_SHIFT; |
| 2278 | ibwc->wr_id = srq->rqe_wr_id_tbl[wqe_idx]; |
| 2279 | spin_lock_irqsave(&srq->q_lock, flags); |
| 2280 | ocrdma_srq_toggle_bit(srq, wqe_idx); |
| 2281 | spin_unlock_irqrestore(&srq->q_lock, flags); |
| 2282 | ocrdma_hwq_inc_tail(&srq->rq); |
| 2283 | } |
| 2284 | |
| 2285 | static bool ocrdma_poll_err_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe, |
| 2286 | struct ib_wc *ibwc, bool *polled, bool *stop, |
| 2287 | int status) |
| 2288 | { |
| 2289 | bool expand; |
| 2290 | |
| 2291 | /* when hw_rq is empty, but wq is not empty, so continue |
| 2292 | * to keep the cqe to get the cq event again. |
| 2293 | */ |
| 2294 | if (is_hw_rq_empty(qp) && !is_hw_sq_empty(qp)) { |
| 2295 | if (!qp->srq && (qp->sq_cq == qp->rq_cq)) { |
| 2296 | *polled = true; |
| 2297 | status = OCRDMA_CQE_WR_FLUSH_ERR; |
| 2298 | expand = ocrdma_update_err_scqe(ibwc, cqe, qp, status); |
| 2299 | } else { |
| 2300 | *polled = false; |
| 2301 | *stop = true; |
| 2302 | expand = false; |
| 2303 | } |
| 2304 | } else |
| 2305 | expand = ocrdma_update_err_rcqe(ibwc, cqe, qp, status); |
| 2306 | return expand; |
| 2307 | } |
| 2308 | |
| 2309 | static void ocrdma_poll_success_rcqe(struct ocrdma_qp *qp, |
| 2310 | struct ocrdma_cqe *cqe, struct ib_wc *ibwc) |
| 2311 | { |
| 2312 | ibwc->opcode = IB_WC_RECV; |
| 2313 | ibwc->qp = &qp->ibqp; |
| 2314 | ibwc->status = IB_WC_SUCCESS; |
| 2315 | |
| 2316 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) |
| 2317 | ocrdma_update_ud_rcqe(ibwc, cqe); |
| 2318 | else |
| 2319 | ibwc->byte_len = le32_to_cpu(cqe->rq.rxlen); |
| 2320 | |
| 2321 | if (is_cqe_imm(cqe)) { |
| 2322 | ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt)); |
| 2323 | ibwc->wc_flags |= IB_WC_WITH_IMM; |
| 2324 | } else if (is_cqe_wr_imm(cqe)) { |
| 2325 | ibwc->opcode = IB_WC_RECV_RDMA_WITH_IMM; |
| 2326 | ibwc->ex.imm_data = htonl(le32_to_cpu(cqe->rq.lkey_immdt)); |
| 2327 | ibwc->wc_flags |= IB_WC_WITH_IMM; |
| 2328 | } else if (is_cqe_invalidated(cqe)) { |
| 2329 | ibwc->ex.invalidate_rkey = le32_to_cpu(cqe->rq.lkey_immdt); |
| 2330 | ibwc->wc_flags |= IB_WC_WITH_INVALIDATE; |
| 2331 | } |
| 2332 | if (qp->ibqp.srq) |
| 2333 | ocrdma_update_free_srq_cqe(ibwc, cqe, qp); |
| 2334 | else { |
| 2335 | ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail]; |
| 2336 | ocrdma_hwq_inc_tail(&qp->rq); |
| 2337 | } |
| 2338 | } |
| 2339 | |
| 2340 | static bool ocrdma_poll_rcqe(struct ocrdma_qp *qp, struct ocrdma_cqe *cqe, |
| 2341 | struct ib_wc *ibwc, bool *polled, bool *stop) |
| 2342 | { |
| 2343 | int status; |
| 2344 | bool expand = false; |
| 2345 | |
| 2346 | ibwc->wc_flags = 0; |
| 2347 | if (qp->qp_type == IB_QPT_UD || qp->qp_type == IB_QPT_GSI) |
| 2348 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2349 | OCRDMA_CQE_UD_STATUS_MASK) >> |
| 2350 | OCRDMA_CQE_UD_STATUS_SHIFT; |
| 2351 | else |
| 2352 | status = (le32_to_cpu(cqe->flags_status_srcqpn) & |
| 2353 | OCRDMA_CQE_STATUS_MASK) >> OCRDMA_CQE_STATUS_SHIFT; |
| 2354 | |
| 2355 | if (status == OCRDMA_CQE_SUCCESS) { |
| 2356 | *polled = true; |
| 2357 | ocrdma_poll_success_rcqe(qp, cqe, ibwc); |
| 2358 | } else { |
| 2359 | expand = ocrdma_poll_err_rcqe(qp, cqe, ibwc, polled, stop, |
| 2360 | status); |
| 2361 | } |
| 2362 | return expand; |
| 2363 | } |
| 2364 | |
| 2365 | static void ocrdma_change_cq_phase(struct ocrdma_cq *cq, struct ocrdma_cqe *cqe, |
| 2366 | u16 cur_getp) |
| 2367 | { |
| 2368 | if (cq->phase_change) { |
| 2369 | if (cur_getp == 0) |
| 2370 | cq->phase = (~cq->phase & OCRDMA_CQE_VALID); |
| 2371 | } else |
| 2372 | /* clear valid bit */ |
| 2373 | cqe->flags_status_srcqpn = 0; |
| 2374 | } |
| 2375 | |
| 2376 | static int ocrdma_poll_hwcq(struct ocrdma_cq *cq, int num_entries, |
| 2377 | struct ib_wc *ibwc) |
| 2378 | { |
| 2379 | u16 qpn = 0; |
| 2380 | int i = 0; |
| 2381 | bool expand = false; |
| 2382 | int polled_hw_cqes = 0; |
| 2383 | struct ocrdma_qp *qp = NULL; |
| 2384 | struct ocrdma_dev *dev = cq->dev; |
| 2385 | struct ocrdma_cqe *cqe; |
| 2386 | u16 cur_getp; bool polled = false; bool stop = false; |
| 2387 | |
| 2388 | cur_getp = cq->getp; |
| 2389 | while (num_entries) { |
| 2390 | cqe = cq->va + cur_getp; |
| 2391 | /* check whether valid cqe or not */ |
| 2392 | if (!is_cqe_valid(cq, cqe)) |
| 2393 | break; |
| 2394 | qpn = (le32_to_cpu(cqe->cmn.qpn) & OCRDMA_CQE_QPN_MASK); |
| 2395 | /* ignore discarded cqe */ |
| 2396 | if (qpn == 0) |
| 2397 | goto skip_cqe; |
| 2398 | qp = dev->qp_tbl[qpn]; |
| 2399 | BUG_ON(qp == NULL); |
| 2400 | |
| 2401 | if (is_cqe_for_sq(cqe)) { |
| 2402 | expand = ocrdma_poll_scqe(qp, cqe, ibwc, &polled, |
| 2403 | &stop); |
| 2404 | } else { |
| 2405 | expand = ocrdma_poll_rcqe(qp, cqe, ibwc, &polled, |
| 2406 | &stop); |
| 2407 | } |
| 2408 | if (expand) |
| 2409 | goto expand_cqe; |
| 2410 | if (stop) |
| 2411 | goto stop_cqe; |
| 2412 | /* clear qpn to avoid duplicate processing by discard_cqe() */ |
| 2413 | cqe->cmn.qpn = 0; |
| 2414 | skip_cqe: |
| 2415 | polled_hw_cqes += 1; |
| 2416 | cur_getp = (cur_getp + 1) % cq->max_hw_cqe; |
| 2417 | ocrdma_change_cq_phase(cq, cqe, cur_getp); |
| 2418 | expand_cqe: |
| 2419 | if (polled) { |
| 2420 | num_entries -= 1; |
| 2421 | i += 1; |
| 2422 | ibwc = ibwc + 1; |
| 2423 | polled = false; |
| 2424 | } |
| 2425 | } |
| 2426 | stop_cqe: |
| 2427 | cq->getp = cur_getp; |
| 2428 | if (polled_hw_cqes || expand || stop) { |
| 2429 | ocrdma_ring_cq_db(dev, cq->id, cq->armed, cq->solicited, |
| 2430 | polled_hw_cqes); |
| 2431 | } |
| 2432 | return i; |
| 2433 | } |
| 2434 | |
| 2435 | /* insert error cqe if the QP's SQ or RQ's CQ matches the CQ under poll. */ |
| 2436 | static int ocrdma_add_err_cqe(struct ocrdma_cq *cq, int num_entries, |
| 2437 | struct ocrdma_qp *qp, struct ib_wc *ibwc) |
| 2438 | { |
| 2439 | int err_cqes = 0; |
| 2440 | |
| 2441 | while (num_entries) { |
| 2442 | if (is_hw_sq_empty(qp) && is_hw_rq_empty(qp)) |
| 2443 | break; |
| 2444 | if (!is_hw_sq_empty(qp) && qp->sq_cq == cq) { |
| 2445 | ocrdma_update_wc(qp, ibwc, qp->sq.tail); |
| 2446 | ocrdma_hwq_inc_tail(&qp->sq); |
| 2447 | } else if (!is_hw_rq_empty(qp) && qp->rq_cq == cq) { |
| 2448 | ibwc->wr_id = qp->rqe_wr_id_tbl[qp->rq.tail]; |
| 2449 | ocrdma_hwq_inc_tail(&qp->rq); |
| 2450 | } else |
| 2451 | return err_cqes; |
| 2452 | ibwc->byte_len = 0; |
| 2453 | ibwc->status = IB_WC_WR_FLUSH_ERR; |
| 2454 | ibwc = ibwc + 1; |
| 2455 | err_cqes += 1; |
| 2456 | num_entries -= 1; |
| 2457 | } |
| 2458 | return err_cqes; |
| 2459 | } |
| 2460 | |
| 2461 | int ocrdma_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc) |
| 2462 | { |
| 2463 | int cqes_to_poll = num_entries; |
| 2464 | struct ocrdma_cq *cq = NULL; |
| 2465 | unsigned long flags; |
| 2466 | struct ocrdma_dev *dev; |
| 2467 | int num_os_cqe = 0, err_cqes = 0; |
| 2468 | struct ocrdma_qp *qp; |
| 2469 | |
| 2470 | cq = get_ocrdma_cq(ibcq); |
| 2471 | dev = cq->dev; |
| 2472 | |
| 2473 | /* poll cqes from adapter CQ */ |
| 2474 | spin_lock_irqsave(&cq->cq_lock, flags); |
| 2475 | num_os_cqe = ocrdma_poll_hwcq(cq, cqes_to_poll, wc); |
| 2476 | spin_unlock_irqrestore(&cq->cq_lock, flags); |
| 2477 | cqes_to_poll -= num_os_cqe; |
| 2478 | |
| 2479 | if (cqes_to_poll) { |
| 2480 | wc = wc + num_os_cqe; |
| 2481 | /* adapter returns single error cqe when qp moves to |
| 2482 | * error state. So insert error cqes with wc_status as |
| 2483 | * FLUSHED for pending WQEs and RQEs of QP's SQ and RQ |
| 2484 | * respectively which uses this CQ. |
| 2485 | */ |
| 2486 | spin_lock_irqsave(&dev->flush_q_lock, flags); |
| 2487 | list_for_each_entry(qp, &cq->sq_head, sq_entry) { |
| 2488 | if (cqes_to_poll == 0) |
| 2489 | break; |
| 2490 | err_cqes = ocrdma_add_err_cqe(cq, cqes_to_poll, qp, wc); |
| 2491 | cqes_to_poll -= err_cqes; |
| 2492 | num_os_cqe += err_cqes; |
| 2493 | wc = wc + err_cqes; |
| 2494 | } |
| 2495 | spin_unlock_irqrestore(&dev->flush_q_lock, flags); |
| 2496 | } |
| 2497 | return num_os_cqe; |
| 2498 | } |
| 2499 | |
| 2500 | int ocrdma_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags cq_flags) |
| 2501 | { |
| 2502 | struct ocrdma_cq *cq; |
| 2503 | unsigned long flags; |
| 2504 | struct ocrdma_dev *dev; |
| 2505 | u16 cq_id; |
| 2506 | u16 cur_getp; |
| 2507 | struct ocrdma_cqe *cqe; |
| 2508 | |
| 2509 | cq = get_ocrdma_cq(ibcq); |
| 2510 | cq_id = cq->id; |
| 2511 | dev = cq->dev; |
| 2512 | |
| 2513 | spin_lock_irqsave(&cq->cq_lock, flags); |
| 2514 | if (cq_flags & IB_CQ_NEXT_COMP || cq_flags & IB_CQ_SOLICITED) |
| 2515 | cq->armed = true; |
| 2516 | if (cq_flags & IB_CQ_SOLICITED) |
| 2517 | cq->solicited = true; |
| 2518 | |
| 2519 | cur_getp = cq->getp; |
| 2520 | cqe = cq->va + cur_getp; |
| 2521 | |
| 2522 | /* check whether any valid cqe exist or not, if not then safe to |
| 2523 | * arm. If cqe is not yet consumed, then let it get consumed and then |
| 2524 | * we arm it to avoid false interrupts. |
| 2525 | */ |
| 2526 | if (!is_cqe_valid(cq, cqe) || cq->arm_needed) { |
| 2527 | cq->arm_needed = false; |
| 2528 | ocrdma_ring_cq_db(dev, cq_id, cq->armed, cq->solicited, 0); |
| 2529 | } |
| 2530 | spin_unlock_irqrestore(&cq->cq_lock, flags); |
| 2531 | return 0; |
| 2532 | } |