linux/drivers/infiniband/hw/nes/nes_verbs.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2006 - 2011 Intel Corporation.  All rights reserved.
   3 *
   4 * This software is available to you under a choice of one of two
   5 * licenses.  You may choose to be licensed under the terms of the GNU
   6 * General Public License (GPL) Version 2, available from the file
   7 * COPYING in the main directory of this source tree, or the
   8 * OpenIB.org BSD license below:
   9 *
  10 *     Redistribution and use in source and binary forms, with or
  11 *     without modification, are permitted provided that the following
  12 *     conditions are met:
  13 *
  14 *      - Redistributions of source code must retain the above
  15 *        copyright notice, this list of conditions and the following
  16 *        disclaimer.
  17 *
  18 *      - Redistributions in binary form must reproduce the above
  19 *        copyright notice, this list of conditions and the following
  20 *        disclaimer in the documentation and/or other materials
  21 *        provided with the distribution.
  22 *
  23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30 * SOFTWARE.
  31 *
  32 */
  33
  34#include <linux/module.h>
  35#include <linux/moduleparam.h>
  36#include <linux/random.h>
  37#include <linux/highmem.h>
  38#include <linux/slab.h>
  39#include <asm/byteorder.h>
  40
  41#include <rdma/ib_verbs.h>
  42#include <rdma/iw_cm.h>
  43#include <rdma/ib_user_verbs.h>
  44
  45#include "nes.h"
  46
  47#include <rdma/ib_umem.h>
  48
  49atomic_t mod_qp_timouts;
  50atomic_t qps_created;
  51atomic_t sw_qps_destroyed;
  52
  53static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev);
  54static int nes_dereg_mr(struct ib_mr *ib_mr);
  55
  56/**
  57 * nes_alloc_mw
  58 */
  59static struct ib_mw *nes_alloc_mw(struct ib_pd *ibpd, enum ib_mw_type type,
  60                                  struct ib_udata *udata)
  61{
  62        struct nes_pd *nespd = to_nespd(ibpd);
  63        struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
  64        struct nes_device *nesdev = nesvnic->nesdev;
  65        struct nes_adapter *nesadapter = nesdev->nesadapter;
  66        struct nes_cqp_request *cqp_request;
  67        struct nes_mr *nesmr;
  68        struct ib_mw *ibmw;
  69        struct nes_hw_cqp_wqe *cqp_wqe;
  70        int ret;
  71        u32 stag;
  72        u32 stag_index = 0;
  73        u32 next_stag_index = 0;
  74        u32 driver_key = 0;
  75        u8 stag_key = 0;
  76
  77        if (type != IB_MW_TYPE_1)
  78                return ERR_PTR(-EINVAL);
  79
  80        get_random_bytes(&next_stag_index, sizeof(next_stag_index));
  81        stag_key = (u8)next_stag_index;
  82
  83        driver_key = 0;
  84
  85        next_stag_index >>= 8;
  86        next_stag_index %= nesadapter->max_mr;
  87
  88        ret = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
  89                        nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_MW);
  90        if (ret) {
  91                return ERR_PTR(ret);
  92        }
  93
  94        nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
  95        if (!nesmr) {
  96                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
  97                return ERR_PTR(-ENOMEM);
  98        }
  99
 100        stag = stag_index << 8;
 101        stag |= driver_key;
 102        stag += (u32)stag_key;
 103
 104        nes_debug(NES_DBG_MR, "Registering STag 0x%08X, index = 0x%08X\n",
 105                        stag, stag_index);
 106
 107        /* Register the region with the adapter */
 108        cqp_request = nes_get_cqp_request(nesdev);
 109        if (cqp_request == NULL) {
 110                kfree(nesmr);
 111                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
 112                return ERR_PTR(-ENOMEM);
 113        }
 114
 115        cqp_request->waiting = 1;
 116        cqp_wqe = &cqp_request->cqp_wqe;
 117
 118        cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] =
 119                        cpu_to_le32( NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_RIGHTS_REMOTE_READ |
 120                        NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_VA_TO |
 121                        NES_CQP_STAG_REM_ACC_EN);
 122
 123        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
 124        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX, (nespd->pd_id & 0x00007fff));
 125        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
 126
 127        atomic_set(&cqp_request->refcount, 2);
 128        nes_post_cqp_request(nesdev, cqp_request);
 129
 130        /* Wait for CQP */
 131        ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
 132                        NES_EVENT_TIMEOUT);
 133        nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
 134                        " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
 135                        stag, ret, cqp_request->major_code, cqp_request->minor_code);
 136        if ((!ret) || (cqp_request->major_code)) {
 137                nes_put_cqp_request(nesdev, cqp_request);
 138                kfree(nesmr);
 139                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
 140                if (!ret) {
 141                        return ERR_PTR(-ETIME);
 142                } else {
 143                        return ERR_PTR(-ENOMEM);
 144                }
 145        }
 146        nes_put_cqp_request(nesdev, cqp_request);
 147
 148        nesmr->ibmw.rkey = stag;
 149        nesmr->mode = IWNES_MEMREG_TYPE_MW;
 150        ibmw = &nesmr->ibmw;
 151        nesmr->pbl_4k = 0;
 152        nesmr->pbls_used = 0;
 153
 154        return ibmw;
 155}
 156
 157
 158/**
 159 * nes_dealloc_mw
 160 */
 161static int nes_dealloc_mw(struct ib_mw *ibmw)
 162{
 163        struct nes_mr *nesmr = to_nesmw(ibmw);
 164        struct nes_vnic *nesvnic = to_nesvnic(ibmw->device);
 165        struct nes_device *nesdev = nesvnic->nesdev;
 166        struct nes_adapter *nesadapter = nesdev->nesadapter;
 167        struct nes_hw_cqp_wqe *cqp_wqe;
 168        struct nes_cqp_request *cqp_request;
 169        int err = 0;
 170        int ret;
 171
 172        /* Deallocate the window with the adapter */
 173        cqp_request = nes_get_cqp_request(nesdev);
 174        if (cqp_request == NULL) {
 175                nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
 176                return -ENOMEM;
 177        }
 178        cqp_request->waiting = 1;
 179        cqp_wqe = &cqp_request->cqp_wqe;
 180        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
 181        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, NES_CQP_DEALLOCATE_STAG);
 182        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ibmw->rkey);
 183
 184        atomic_set(&cqp_request->refcount, 2);
 185        nes_post_cqp_request(nesdev, cqp_request);
 186
 187        /* Wait for CQP */
 188        nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X to complete.\n",
 189                        ibmw->rkey);
 190        ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
 191                        NES_EVENT_TIMEOUT);
 192        nes_debug(NES_DBG_MR, "Deallocate STag completed, wait_event_timeout ret = %u,"
 193                        " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
 194                        ret, cqp_request->major_code, cqp_request->minor_code);
 195        if (!ret)
 196                err = -ETIME;
 197        else if (cqp_request->major_code)
 198                err = -EIO;
 199
 200        nes_put_cqp_request(nesdev, cqp_request);
 201
 202        nes_free_resource(nesadapter, nesadapter->allocated_mrs,
 203                        (ibmw->rkey & 0x0fffff00) >> 8);
 204        kfree(nesmr);
 205
 206        return err;
 207}
 208
 209
 210/*
 211 * nes_alloc_fast_mr
 212 */
 213static int alloc_fast_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
 214                             u32 stag, u32 page_count)
 215{
 216        struct nes_hw_cqp_wqe *cqp_wqe;
 217        struct nes_cqp_request *cqp_request;
 218        unsigned long flags;
 219        int ret;
 220        struct nes_adapter *nesadapter = nesdev->nesadapter;
 221        u32 opcode = 0;
 222        u16 major_code;
 223        u64 region_length = page_count * PAGE_SIZE;
 224
 225
 226        cqp_request = nes_get_cqp_request(nesdev);
 227        if (cqp_request == NULL) {
 228                nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
 229                return -ENOMEM;
 230        }
 231        nes_debug(NES_DBG_MR, "alloc_fast_reg_mr: page_count = %d, "
 232                              "region_length = %llu\n",
 233                              page_count, region_length);
 234        cqp_request->waiting = 1;
 235        cqp_wqe = &cqp_request->cqp_wqe;
 236
 237        spin_lock_irqsave(&nesadapter->pbl_lock, flags);
 238        if (nesadapter->free_4kpbl > 0) {
 239                nesadapter->free_4kpbl--;
 240                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 241        } else {
 242                /* No 4kpbl's available: */
 243                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 244                nes_debug(NES_DBG_MR, "Out of Pbls\n");
 245                nes_free_cqp_request(nesdev, cqp_request);
 246                return -ENOMEM;
 247        }
 248
 249        opcode = NES_CQP_ALLOCATE_STAG | NES_CQP_STAG_MR |
 250                 NES_CQP_STAG_PBL_BLK_SIZE | NES_CQP_STAG_VA_TO |
 251                 NES_CQP_STAG_REM_ACC_EN;
 252        /*
 253         * The current OFED API does not support the zero based TO option.
 254         * If added then need to changed the NES_CQP_STAG_VA* option.  Also,
 255         * the API does not support that ability to have the MR set for local
 256         * access only when created and not allow the SQ op to override. Given
 257         * this the remote enable must be set here.
 258         */
 259
 260        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
 261        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
 262        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, 1);
 263
 264        cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
 265                        cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
 266        cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
 267                        cpu_to_le32(nespd->pd_id & 0x00007fff);
 268
 269        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
 270        set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, 0);
 271        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, 0);
 272        set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, 0);
 273        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (page_count * 8));
 274        cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
 275        barrier();
 276
 277        atomic_set(&cqp_request->refcount, 2);
 278        nes_post_cqp_request(nesdev, cqp_request);
 279
 280        /* Wait for CQP */
 281        ret = wait_event_timeout(cqp_request->waitq,
 282                                 (0 != cqp_request->request_done),
 283                                 NES_EVENT_TIMEOUT);
 284
 285        nes_debug(NES_DBG_MR, "Allocate STag 0x%08X completed, "
 286                  "wait_event_timeout ret = %u, CQP Major:Minor codes = "
 287                  "0x%04X:0x%04X.\n", stag, ret, cqp_request->major_code,
 288                  cqp_request->minor_code);
 289        major_code = cqp_request->major_code;
 290        nes_put_cqp_request(nesdev, cqp_request);
 291
 292        if (!ret || major_code) {
 293                spin_lock_irqsave(&nesadapter->pbl_lock, flags);
 294                nesadapter->free_4kpbl++;
 295                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 296        }
 297
 298        if (!ret)
 299                return -ETIME;
 300        else if (major_code)
 301                return -EIO;
 302        return 0;
 303}
 304
 305/*
 306 * nes_alloc_mr
 307 */
 308static struct ib_mr *nes_alloc_mr(struct ib_pd *ibpd,
 309                                  enum ib_mr_type mr_type,
 310                                  u32 max_num_sg)
 311{
 312        struct nes_pd *nespd = to_nespd(ibpd);
 313        struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
 314        struct nes_device *nesdev = nesvnic->nesdev;
 315        struct nes_adapter *nesadapter = nesdev->nesadapter;
 316
 317        u32 next_stag_index;
 318        u8 stag_key = 0;
 319        u32 driver_key = 0;
 320        int err = 0;
 321        u32 stag_index = 0;
 322        struct nes_mr *nesmr;
 323        u32 stag;
 324        int ret;
 325        struct ib_mr *ibmr;
 326
 327        if (mr_type != IB_MR_TYPE_MEM_REG)
 328                return ERR_PTR(-EINVAL);
 329
 330        if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
 331                return ERR_PTR(-E2BIG);
 332
 333/*
 334 * Note:  Set to always use a fixed length single page entry PBL.  This is to allow
 335 *       for the fast_reg_mr operation to always know the size of the PBL.
 336 */
 337        if (max_num_sg > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64)))
 338                return ERR_PTR(-E2BIG);
 339
 340        get_random_bytes(&next_stag_index, sizeof(next_stag_index));
 341        stag_key = (u8)next_stag_index;
 342        next_stag_index >>= 8;
 343        next_stag_index %= nesadapter->max_mr;
 344
 345        err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
 346                                 nesadapter->max_mr, &stag_index,
 347                                 &next_stag_index, NES_RESOURCE_FAST_MR);
 348        if (err)
 349                return ERR_PTR(err);
 350
 351        nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
 352        if (!nesmr) {
 353                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
 354                return ERR_PTR(-ENOMEM);
 355        }
 356
 357        stag = stag_index << 8;
 358        stag |= driver_key;
 359        stag += (u32)stag_key;
 360
 361        nes_debug(NES_DBG_MR, "Allocating STag 0x%08X index = 0x%08X\n",
 362                  stag, stag_index);
 363
 364        ret = alloc_fast_reg_mr(nesdev, nespd, stag, max_num_sg);
 365
 366        if (ret == 0) {
 367                nesmr->ibmr.rkey = stag;
 368                nesmr->ibmr.lkey = stag;
 369                nesmr->mode = IWNES_MEMREG_TYPE_FMEM;
 370                ibmr = &nesmr->ibmr;
 371        } else {
 372                kfree(nesmr);
 373                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
 374                return ERR_PTR(-ENOMEM);
 375        }
 376
 377        nesmr->pages = pci_alloc_consistent(nesdev->pcidev,
 378                                            max_num_sg * sizeof(u64),
 379                                            &nesmr->paddr);
 380        if (!nesmr->paddr)
 381                goto err;
 382
 383        nesmr->max_pages = max_num_sg;
 384
 385        return ibmr;
 386
 387err:
 388        nes_dereg_mr(ibmr);
 389
 390        return ERR_PTR(-ENOMEM);
 391}
 392
 393static int nes_set_page(struct ib_mr *ibmr, u64 addr)
 394{
 395        struct nes_mr *nesmr = to_nesmr(ibmr);
 396
 397        if (unlikely(nesmr->npages == nesmr->max_pages))
 398                return -ENOMEM;
 399
 400        nesmr->pages[nesmr->npages++] = cpu_to_le64(addr);
 401
 402        return 0;
 403}
 404
 405static int nes_map_mr_sg(struct ib_mr *ibmr,
 406                         struct scatterlist *sg,
 407                         int sg_nents)
 408{
 409        struct nes_mr *nesmr = to_nesmr(ibmr);
 410
 411        nesmr->npages = 0;
 412
 413        return ib_sg_to_pages(ibmr, sg, sg_nents, nes_set_page);
 414}
 415
 416/**
 417 * nes_query_device
 418 */
 419static int nes_query_device(struct ib_device *ibdev, struct ib_device_attr *props,
 420                            struct ib_udata *uhw)
 421{
 422        struct nes_vnic *nesvnic = to_nesvnic(ibdev);
 423        struct nes_device *nesdev = nesvnic->nesdev;
 424        struct nes_ib_device *nesibdev = nesvnic->nesibdev;
 425
 426        if (uhw->inlen || uhw->outlen)
 427                return -EINVAL;
 428
 429        memset(props, 0, sizeof(*props));
 430        memcpy(&props->sys_image_guid, nesvnic->netdev->dev_addr, 6);
 431
 432        props->fw_ver = nesdev->nesadapter->firmware_version;
 433        props->device_cap_flags = nesdev->nesadapter->device_cap_flags;
 434        props->vendor_id = nesdev->nesadapter->vendor_id;
 435        props->vendor_part_id = nesdev->nesadapter->vendor_part_id;
 436        props->hw_ver = nesdev->nesadapter->hw_rev;
 437        props->max_mr_size = 0x80000000;
 438        props->max_qp = nesibdev->max_qp;
 439        props->max_qp_wr = nesdev->nesadapter->max_qp_wr - 2;
 440        props->max_sge = nesdev->nesadapter->max_sge;
 441        props->max_cq = nesibdev->max_cq;
 442        props->max_cqe = nesdev->nesadapter->max_cqe;
 443        props->max_mr = nesibdev->max_mr;
 444        props->max_mw = nesibdev->max_mr;
 445        props->max_pd = nesibdev->max_pd;
 446        props->max_sge_rd = 1;
 447        switch (nesdev->nesadapter->max_irrq_wr) {
 448                case 0:
 449                        props->max_qp_rd_atom = 2;
 450                        break;
 451                case 1:
 452                        props->max_qp_rd_atom = 8;
 453                        break;
 454                case 2:
 455                        props->max_qp_rd_atom = 32;
 456                        break;
 457                case 3:
 458                        props->max_qp_rd_atom = 64;
 459                        break;
 460                default:
 461                        props->max_qp_rd_atom = 0;
 462        }
 463        props->max_qp_init_rd_atom = props->max_qp_rd_atom;
 464        props->atomic_cap = IB_ATOMIC_NONE;
 465        props->max_map_per_fmr = 1;
 466
 467        return 0;
 468}
 469
 470
 471/**
 472 * nes_query_port
 473 */
 474static int nes_query_port(struct ib_device *ibdev, u8 port, struct ib_port_attr *props)
 475{
 476        struct nes_vnic *nesvnic = to_nesvnic(ibdev);
 477        struct net_device *netdev = nesvnic->netdev;
 478
 479        memset(props, 0, sizeof(*props));
 480
 481        props->max_mtu = IB_MTU_4096;
 482
 483        if (netdev->mtu  >= 4096)
 484                props->active_mtu = IB_MTU_4096;
 485        else if (netdev->mtu  >= 2048)
 486                props->active_mtu = IB_MTU_2048;
 487        else if (netdev->mtu  >= 1024)
 488                props->active_mtu = IB_MTU_1024;
 489        else if (netdev->mtu  >= 512)
 490                props->active_mtu = IB_MTU_512;
 491        else
 492                props->active_mtu = IB_MTU_256;
 493
 494        props->lid = 1;
 495        props->lmc = 0;
 496        props->sm_lid = 0;
 497        props->sm_sl = 0;
 498        if (netif_queue_stopped(netdev))
 499                props->state = IB_PORT_DOWN;
 500        else if (nesvnic->linkup)
 501                props->state = IB_PORT_ACTIVE;
 502        else
 503                props->state = IB_PORT_DOWN;
 504        props->phys_state = 0;
 505        props->port_cap_flags = IB_PORT_CM_SUP | IB_PORT_REINIT_SUP |
 506                        IB_PORT_VENDOR_CLASS_SUP | IB_PORT_BOOT_MGMT_SUP;
 507        props->gid_tbl_len = 1;
 508        props->pkey_tbl_len = 1;
 509        props->qkey_viol_cntr = 0;
 510        props->active_width = IB_WIDTH_4X;
 511        props->active_speed = IB_SPEED_SDR;
 512        props->max_msg_sz = 0x80000000;
 513
 514        return 0;
 515}
 516
 517/**
 518 * nes_query_pkey
 519 */
 520static int nes_query_pkey(struct ib_device *ibdev, u8 port, u16 index, u16 *pkey)
 521{
 522        *pkey = 0;
 523        return 0;
 524}
 525
 526
 527/**
 528 * nes_query_gid
 529 */
 530static int nes_query_gid(struct ib_device *ibdev, u8 port,
 531                int index, union ib_gid *gid)
 532{
 533        struct nes_vnic *nesvnic = to_nesvnic(ibdev);
 534
 535        memset(&(gid->raw[0]), 0, sizeof(gid->raw));
 536        memcpy(&(gid->raw[0]), nesvnic->netdev->dev_addr, 6);
 537
 538        return 0;
 539}
 540
 541
 542/**
 543 * nes_alloc_ucontext - Allocate the user context data structure. This keeps track
 544 * of all objects associated with a particular user-mode client.
 545 */
 546static struct ib_ucontext *nes_alloc_ucontext(struct ib_device *ibdev,
 547                struct ib_udata *udata)
 548{
 549        struct nes_vnic *nesvnic = to_nesvnic(ibdev);
 550        struct nes_device *nesdev = nesvnic->nesdev;
 551        struct nes_adapter *nesadapter = nesdev->nesadapter;
 552        struct nes_alloc_ucontext_req req;
 553        struct nes_alloc_ucontext_resp uresp;
 554        struct nes_ucontext *nes_ucontext;
 555        struct nes_ib_device *nesibdev = nesvnic->nesibdev;
 556
 557
 558        if (ib_copy_from_udata(&req, udata, sizeof(struct nes_alloc_ucontext_req))) {
 559                printk(KERN_ERR PFX "Invalid structure size on allocate user context.\n");
 560                return ERR_PTR(-EINVAL);
 561        }
 562
 563        if (req.userspace_ver != NES_ABI_USERSPACE_VER) {
 564                printk(KERN_ERR PFX "Invalid userspace driver version detected. Detected version %d, should be %d\n",
 565                        req.userspace_ver, NES_ABI_USERSPACE_VER);
 566                return ERR_PTR(-EINVAL);
 567        }
 568
 569
 570        memset(&uresp, 0, sizeof uresp);
 571
 572        uresp.max_qps = nesibdev->max_qp;
 573        uresp.max_pds = nesibdev->max_pd;
 574        uresp.wq_size = nesdev->nesadapter->max_qp_wr * 2;
 575        uresp.virtwq = nesadapter->virtwq;
 576        uresp.kernel_ver = NES_ABI_KERNEL_VER;
 577
 578        nes_ucontext = kzalloc(sizeof *nes_ucontext, GFP_KERNEL);
 579        if (!nes_ucontext)
 580                return ERR_PTR(-ENOMEM);
 581
 582        nes_ucontext->nesdev = nesdev;
 583        nes_ucontext->mmap_wq_offset = uresp.max_pds;
 584        nes_ucontext->mmap_cq_offset = nes_ucontext->mmap_wq_offset +
 585                        ((sizeof(struct nes_hw_qp_wqe) * uresp.max_qps * 2) + PAGE_SIZE-1) /
 586                        PAGE_SIZE;
 587
 588
 589        if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
 590                kfree(nes_ucontext);
 591                return ERR_PTR(-EFAULT);
 592        }
 593
 594        INIT_LIST_HEAD(&nes_ucontext->cq_reg_mem_list);
 595        INIT_LIST_HEAD(&nes_ucontext->qp_reg_mem_list);
 596        atomic_set(&nes_ucontext->usecnt, 1);
 597        return &nes_ucontext->ibucontext;
 598}
 599
 600
 601/**
 602 * nes_dealloc_ucontext
 603 */
 604static int nes_dealloc_ucontext(struct ib_ucontext *context)
 605{
 606        /* struct nes_vnic *nesvnic = to_nesvnic(context->device); */
 607        /* struct nes_device *nesdev = nesvnic->nesdev; */
 608        struct nes_ucontext *nes_ucontext = to_nesucontext(context);
 609
 610        if (!atomic_dec_and_test(&nes_ucontext->usecnt))
 611          return 0;
 612        kfree(nes_ucontext);
 613        return 0;
 614}
 615
 616
 617/**
 618 * nes_mmap
 619 */
 620static int nes_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
 621{
 622        unsigned long index;
 623        struct nes_vnic *nesvnic = to_nesvnic(context->device);
 624        struct nes_device *nesdev = nesvnic->nesdev;
 625        /* struct nes_adapter *nesadapter = nesdev->nesadapter; */
 626        struct nes_ucontext *nes_ucontext;
 627        struct nes_qp *nesqp;
 628
 629        nes_ucontext = to_nesucontext(context);
 630
 631
 632        if (vma->vm_pgoff >= nes_ucontext->mmap_wq_offset) {
 633                index = (vma->vm_pgoff - nes_ucontext->mmap_wq_offset) * PAGE_SIZE;
 634                index /= ((sizeof(struct nes_hw_qp_wqe) * nesdev->nesadapter->max_qp_wr * 2) +
 635                                PAGE_SIZE-1) & (~(PAGE_SIZE-1));
 636                if (!test_bit(index, nes_ucontext->allocated_wqs)) {
 637                        nes_debug(NES_DBG_MMAP, "wq %lu not allocated\n", index);
 638                        return -EFAULT;
 639                }
 640                nesqp = nes_ucontext->mmap_nesqp[index];
 641                if (nesqp == NULL) {
 642                        nes_debug(NES_DBG_MMAP, "wq %lu has a NULL QP base.\n", index);
 643                        return -EFAULT;
 644                }
 645                if (remap_pfn_range(vma, vma->vm_start,
 646                                virt_to_phys(nesqp->hwqp.sq_vbase) >> PAGE_SHIFT,
 647                                vma->vm_end - vma->vm_start,
 648                                vma->vm_page_prot)) {
 649                        nes_debug(NES_DBG_MMAP, "remap_pfn_range failed.\n");
 650                        return -EAGAIN;
 651                }
 652                vma->vm_private_data = nesqp;
 653                return 0;
 654        } else {
 655                index = vma->vm_pgoff;
 656                if (!test_bit(index, nes_ucontext->allocated_doorbells))
 657                        return -EFAULT;
 658
 659                vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
 660                if (io_remap_pfn_range(vma, vma->vm_start,
 661                                (nesdev->doorbell_start +
 662                                ((nes_ucontext->mmap_db_index[index] - nesdev->base_doorbell_index) * 4096))
 663                                >> PAGE_SHIFT, PAGE_SIZE, vma->vm_page_prot))
 664                        return -EAGAIN;
 665                vma->vm_private_data = nes_ucontext;
 666                return 0;
 667        }
 668
 669        return -ENOSYS;
 670}
 671
 672
 673/**
 674 * nes_alloc_pd
 675 */
 676static struct ib_pd *nes_alloc_pd(struct ib_device *ibdev,
 677                struct ib_ucontext *context, struct ib_udata *udata)
 678{
 679        struct nes_pd *nespd;
 680        struct nes_vnic *nesvnic = to_nesvnic(ibdev);
 681        struct nes_device *nesdev = nesvnic->nesdev;
 682        struct nes_adapter *nesadapter = nesdev->nesadapter;
 683        struct nes_ucontext *nesucontext;
 684        struct nes_alloc_pd_resp uresp;
 685        u32 pd_num = 0;
 686        int err;
 687
 688        nes_debug(NES_DBG_PD, "nesvnic=%p, netdev=%p %s, ibdev=%p, context=%p, netdev refcnt=%u\n",
 689                        nesvnic, nesdev->netdev[0], nesdev->netdev[0]->name, ibdev, context,
 690                        netdev_refcnt_read(nesvnic->netdev));
 691
 692        err = nes_alloc_resource(nesadapter, nesadapter->allocated_pds,
 693                        nesadapter->max_pd, &pd_num, &nesadapter->next_pd, NES_RESOURCE_PD);
 694        if (err) {
 695                return ERR_PTR(err);
 696        }
 697
 698        nespd = kzalloc(sizeof (struct nes_pd), GFP_KERNEL);
 699        if (!nespd) {
 700                nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
 701                return ERR_PTR(-ENOMEM);
 702        }
 703
 704        nes_debug(NES_DBG_PD, "Allocating PD (%p) for ib device %s\n",
 705                        nespd, nesvnic->nesibdev->ibdev.name);
 706
 707        nespd->pd_id = (pd_num << (PAGE_SHIFT-12)) + nesadapter->base_pd;
 708
 709        if (context) {
 710                nesucontext = to_nesucontext(context);
 711                nespd->mmap_db_index = find_next_zero_bit(nesucontext->allocated_doorbells,
 712                                NES_MAX_USER_DB_REGIONS, nesucontext->first_free_db);
 713                nes_debug(NES_DBG_PD, "find_first_zero_biton doorbells returned %u, mapping pd_id %u.\n",
 714                                nespd->mmap_db_index, nespd->pd_id);
 715                if (nespd->mmap_db_index >= NES_MAX_USER_DB_REGIONS) {
 716                        nes_debug(NES_DBG_PD, "mmap_db_index > MAX\n");
 717                        nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
 718                        kfree(nespd);
 719                        return ERR_PTR(-ENOMEM);
 720                }
 721
 722                uresp.pd_id = nespd->pd_id;
 723                uresp.mmap_db_index = nespd->mmap_db_index;
 724                if (ib_copy_to_udata(udata, &uresp, sizeof (struct nes_alloc_pd_resp))) {
 725                        nes_free_resource(nesadapter, nesadapter->allocated_pds, pd_num);
 726                        kfree(nespd);
 727                        return ERR_PTR(-EFAULT);
 728                }
 729
 730                set_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
 731                nesucontext->mmap_db_index[nespd->mmap_db_index] = nespd->pd_id;
 732                nesucontext->first_free_db = nespd->mmap_db_index + 1;
 733        }
 734
 735        nes_debug(NES_DBG_PD, "PD%u structure located @%p.\n", nespd->pd_id, nespd);
 736        return &nespd->ibpd;
 737}
 738
 739
 740/**
 741 * nes_dealloc_pd
 742 */
 743static int nes_dealloc_pd(struct ib_pd *ibpd)
 744{
 745        struct nes_ucontext *nesucontext;
 746        struct nes_pd *nespd = to_nespd(ibpd);
 747        struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
 748        struct nes_device *nesdev = nesvnic->nesdev;
 749        struct nes_adapter *nesadapter = nesdev->nesadapter;
 750
 751        if ((ibpd->uobject) && (ibpd->uobject->context)) {
 752                nesucontext = to_nesucontext(ibpd->uobject->context);
 753                nes_debug(NES_DBG_PD, "Clearing bit %u from allocated doorbells\n",
 754                                nespd->mmap_db_index);
 755                clear_bit(nespd->mmap_db_index, nesucontext->allocated_doorbells);
 756                nesucontext->mmap_db_index[nespd->mmap_db_index] = 0;
 757                if (nesucontext->first_free_db > nespd->mmap_db_index) {
 758                        nesucontext->first_free_db = nespd->mmap_db_index;
 759                }
 760        }
 761
 762        nes_debug(NES_DBG_PD, "Deallocating PD%u structure located @%p.\n",
 763                        nespd->pd_id, nespd);
 764        nes_free_resource(nesadapter, nesadapter->allocated_pds,
 765                        (nespd->pd_id-nesadapter->base_pd)>>(PAGE_SHIFT-12));
 766        kfree(nespd);
 767
 768        return 0;
 769}
 770
 771
 772/**
 773 * nes_create_ah
 774 */
 775static struct ib_ah *nes_create_ah(struct ib_pd *pd, struct ib_ah_attr *ah_attr)
 776{
 777        return ERR_PTR(-ENOSYS);
 778}
 779
 780
 781/**
 782 * nes_destroy_ah
 783 */
 784static int nes_destroy_ah(struct ib_ah *ah)
 785{
 786        return -ENOSYS;
 787}
 788
 789
 790/**
 791 * nes_get_encoded_size
 792 */
 793static inline u8 nes_get_encoded_size(int *size)
 794{
 795        u8 encoded_size = 0;
 796        if (*size <= 32) {
 797                *size = 32;
 798                encoded_size = 1;
 799        } else if (*size <= 128) {
 800                *size = 128;
 801                encoded_size = 2;
 802        } else if (*size <= 512) {
 803                *size = 512;
 804                encoded_size = 3;
 805        }
 806        return (encoded_size);
 807}
 808
 809
 810
 811/**
 812 * nes_setup_virt_qp
 813 */
 814static int nes_setup_virt_qp(struct nes_qp *nesqp, struct nes_pbl *nespbl,
 815                struct nes_vnic *nesvnic, int sq_size, int rq_size)
 816{
 817        unsigned long flags;
 818        void *mem;
 819        __le64 *pbl = NULL;
 820        __le64 *tpbl;
 821        __le64 *pblbuffer;
 822        struct nes_device *nesdev = nesvnic->nesdev;
 823        struct nes_adapter *nesadapter = nesdev->nesadapter;
 824        u32 pbl_entries;
 825        u8 rq_pbl_entries;
 826        u8 sq_pbl_entries;
 827
 828        pbl_entries = nespbl->pbl_size >> 3;
 829        nes_debug(NES_DBG_QP, "Userspace PBL, pbl_size=%u, pbl_entries = %d pbl_vbase=%p, pbl_pbase=%lx\n",
 830                        nespbl->pbl_size, pbl_entries,
 831                        (void *)nespbl->pbl_vbase,
 832                        (unsigned long) nespbl->pbl_pbase);
 833        pbl = (__le64 *) nespbl->pbl_vbase; /* points to first pbl entry */
 834        /* now lets set the sq_vbase as well as rq_vbase addrs we will assign */
 835        /* the first pbl to be fro the rq_vbase... */
 836        rq_pbl_entries = (rq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
 837        sq_pbl_entries = (sq_size * sizeof(struct nes_hw_qp_wqe)) >> 12;
 838        nesqp->hwqp.sq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
 839        if (!nespbl->page) {
 840                nes_debug(NES_DBG_QP, "QP nespbl->page is NULL \n");
 841                kfree(nespbl);
 842                return -ENOMEM;
 843        }
 844
 845        nesqp->hwqp.sq_vbase = kmap(nespbl->page);
 846        nesqp->page = nespbl->page;
 847        if (!nesqp->hwqp.sq_vbase) {
 848                nes_debug(NES_DBG_QP, "QP sq_vbase kmap failed\n");
 849                kfree(nespbl);
 850                return -ENOMEM;
 851        }
 852
 853        /* Now to get to sq.. we need to calculate how many */
 854        /* PBL entries were used by the rq.. */
 855        pbl += sq_pbl_entries;
 856        nesqp->hwqp.rq_pbase = (le32_to_cpu(((__le32 *)pbl)[0])) | ((u64)((le32_to_cpu(((__le32 *)pbl)[1]))) << 32);
 857        /* nesqp->hwqp.rq_vbase = bus_to_virt(*pbl); */
 858        /*nesqp->hwqp.rq_vbase = phys_to_virt(*pbl); */
 859
 860        nes_debug(NES_DBG_QP, "QP sq_vbase= %p sq_pbase=%lx rq_vbase=%p rq_pbase=%lx\n",
 861                  nesqp->hwqp.sq_vbase, (unsigned long) nesqp->hwqp.sq_pbase,
 862                  nesqp->hwqp.rq_vbase, (unsigned long) nesqp->hwqp.rq_pbase);
 863        spin_lock_irqsave(&nesadapter->pbl_lock, flags);
 864        if (!nesadapter->free_256pbl) {
 865                pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
 866                                nespbl->pbl_pbase);
 867                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 868                kunmap(nesqp->page);
 869                kfree(nespbl);
 870                return -ENOMEM;
 871        }
 872        nesadapter->free_256pbl--;
 873        spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 874
 875        nesqp->pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 256, &nesqp->pbl_pbase);
 876        pblbuffer = nesqp->pbl_vbase;
 877        if (!nesqp->pbl_vbase) {
 878                /* memory allocated during nes_reg_user_mr() */
 879                pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
 880                                    nespbl->pbl_pbase);
 881                kfree(nespbl);
 882                spin_lock_irqsave(&nesadapter->pbl_lock, flags);
 883                nesadapter->free_256pbl++;
 884                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 885                kunmap(nesqp->page);
 886                return -ENOMEM;
 887        }
 888        memset(nesqp->pbl_vbase, 0, 256);
 889        /* fill in the page address in the pbl buffer.. */
 890        tpbl = pblbuffer + 16;
 891        pbl = (__le64 *)nespbl->pbl_vbase;
 892        while (sq_pbl_entries--)
 893                *tpbl++ = *pbl++;
 894        tpbl = pblbuffer;
 895        while (rq_pbl_entries--)
 896                *tpbl++ = *pbl++;
 897
 898        /* done with memory allocated during nes_reg_user_mr() */
 899        pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
 900                            nespbl->pbl_pbase);
 901        kfree(nespbl);
 902
 903        nesqp->qp_mem_size =
 904                        max((u32)sizeof(struct nes_qp_context), ((u32)256)) + 256;     /* this is Q2 */
 905        /* Round up to a multiple of a page */
 906        nesqp->qp_mem_size += PAGE_SIZE - 1;
 907        nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
 908
 909        mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
 910                        &nesqp->hwqp.q2_pbase);
 911
 912        if (!mem) {
 913                pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase);
 914                nesqp->pbl_vbase = NULL;
 915                spin_lock_irqsave(&nesadapter->pbl_lock, flags);
 916                nesadapter->free_256pbl++;
 917                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 918                kunmap(nesqp->page);
 919                return -ENOMEM;
 920        }
 921        nesqp->sq_kmapped = 1;
 922        nesqp->hwqp.q2_vbase = mem;
 923        mem += 256;
 924        memset(nesqp->hwqp.q2_vbase, 0, 256);
 925        nesqp->nesqp_context = mem;
 926        memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
 927        nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
 928
 929        return 0;
 930}
 931
 932
 933/**
 934 * nes_setup_mmap_qp
 935 */
 936static int nes_setup_mmap_qp(struct nes_qp *nesqp, struct nes_vnic *nesvnic,
 937                int sq_size, int rq_size)
 938{
 939        void *mem;
 940        struct nes_device *nesdev = nesvnic->nesdev;
 941
 942        nesqp->qp_mem_size = (sizeof(struct nes_hw_qp_wqe) * sq_size) +
 943                        (sizeof(struct nes_hw_qp_wqe) * rq_size) +
 944                        max((u32)sizeof(struct nes_qp_context), ((u32)256)) +
 945                        256; /* this is Q2 */
 946        /* Round up to a multiple of a page */
 947        nesqp->qp_mem_size += PAGE_SIZE - 1;
 948        nesqp->qp_mem_size &= ~(PAGE_SIZE - 1);
 949
 950        mem = pci_alloc_consistent(nesdev->pcidev, nesqp->qp_mem_size,
 951                        &nesqp->hwqp.sq_pbase);
 952        if (!mem)
 953                return -ENOMEM;
 954        nes_debug(NES_DBG_QP, "PCI consistent memory for "
 955                        "host descriptor rings located @ %p (pa = 0x%08lX.) size = %u.\n",
 956                        mem, (unsigned long)nesqp->hwqp.sq_pbase, nesqp->qp_mem_size);
 957
 958        memset(mem, 0, nesqp->qp_mem_size);
 959
 960        nesqp->hwqp.sq_vbase = mem;
 961        mem += sizeof(struct nes_hw_qp_wqe) * sq_size;
 962
 963        nesqp->hwqp.rq_vbase = mem;
 964        nesqp->hwqp.rq_pbase = nesqp->hwqp.sq_pbase +
 965                        sizeof(struct nes_hw_qp_wqe) * sq_size;
 966        mem += sizeof(struct nes_hw_qp_wqe) * rq_size;
 967
 968        nesqp->hwqp.q2_vbase = mem;
 969        nesqp->hwqp.q2_pbase = nesqp->hwqp.rq_pbase +
 970                        sizeof(struct nes_hw_qp_wqe) * rq_size;
 971        mem += 256;
 972        memset(nesqp->hwqp.q2_vbase, 0, 256);
 973
 974        nesqp->nesqp_context = mem;
 975        nesqp->nesqp_context_pbase = nesqp->hwqp.q2_pbase + 256;
 976        memset(nesqp->nesqp_context, 0, sizeof(*nesqp->nesqp_context));
 977        return 0;
 978}
 979
 980
 981/**
 982 * nes_free_qp_mem() is to free up the qp's pci_alloc_consistent() memory.
 983 */
 984static inline void nes_free_qp_mem(struct nes_device *nesdev,
 985                struct nes_qp *nesqp, int virt_wqs)
 986{
 987        unsigned long flags;
 988        struct nes_adapter *nesadapter = nesdev->nesadapter;
 989        if (!virt_wqs) {
 990                pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size,
 991                                nesqp->hwqp.sq_vbase, nesqp->hwqp.sq_pbase);
 992        }else {
 993                spin_lock_irqsave(&nesadapter->pbl_lock, flags);
 994                nesadapter->free_256pbl++;
 995                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
 996                pci_free_consistent(nesdev->pcidev, nesqp->qp_mem_size, nesqp->hwqp.q2_vbase, nesqp->hwqp.q2_pbase);
 997                pci_free_consistent(nesdev->pcidev, 256, nesqp->pbl_vbase, nesqp->pbl_pbase );
 998                nesqp->pbl_vbase = NULL;
 999                if (nesqp->sq_kmapped) {
1000                        nesqp->sq_kmapped = 0;
1001                        kunmap(nesqp->page);
1002                }
1003        }
1004}
1005
1006
1007/**
1008 * nes_create_qp
1009 */
1010static struct ib_qp *nes_create_qp(struct ib_pd *ibpd,
1011                struct ib_qp_init_attr *init_attr, struct ib_udata *udata)
1012{
1013        u64 u64temp= 0;
1014        u64 u64nesqp = 0;
1015        struct nes_pd *nespd = to_nespd(ibpd);
1016        struct nes_vnic *nesvnic = to_nesvnic(ibpd->device);
1017        struct nes_device *nesdev = nesvnic->nesdev;
1018        struct nes_adapter *nesadapter = nesdev->nesadapter;
1019        struct nes_qp *nesqp;
1020        struct nes_cq *nescq;
1021        struct nes_ucontext *nes_ucontext;
1022        struct nes_hw_cqp_wqe *cqp_wqe;
1023        struct nes_cqp_request *cqp_request;
1024        struct nes_create_qp_req req;
1025        struct nes_create_qp_resp uresp;
1026        struct nes_pbl  *nespbl = NULL;
1027        u32 qp_num = 0;
1028        u32 opcode = 0;
1029        /* u32 counter = 0; */
1030        void *mem;
1031        unsigned long flags;
1032        int ret;
1033        int err;
1034        int virt_wqs = 0;
1035        int sq_size;
1036        int rq_size;
1037        u8 sq_encoded_size;
1038        u8 rq_encoded_size;
1039        /* int counter; */
1040
1041        if (init_attr->create_flags)
1042                return ERR_PTR(-EINVAL);
1043
1044        atomic_inc(&qps_created);
1045        switch (init_attr->qp_type) {
1046                case IB_QPT_RC:
1047                        if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) {
1048                                init_attr->cap.max_inline_data = 0;
1049                        } else {
1050                                init_attr->cap.max_inline_data = 64;
1051                        }
1052                        sq_size = init_attr->cap.max_send_wr;
1053                        rq_size = init_attr->cap.max_recv_wr;
1054
1055                        /* check if the encoded sizes are OK or not... */
1056                        sq_encoded_size = nes_get_encoded_size(&sq_size);
1057                        rq_encoded_size = nes_get_encoded_size(&rq_size);
1058
1059                        if ((!sq_encoded_size) || (!rq_encoded_size)) {
1060                                nes_debug(NES_DBG_QP, "ERROR bad rq (%u) or sq (%u) size\n",
1061                                                rq_size, sq_size);
1062                                return ERR_PTR(-EINVAL);
1063                        }
1064
1065                        init_attr->cap.max_send_wr = sq_size -2;
1066                        init_attr->cap.max_recv_wr = rq_size -1;
1067                        nes_debug(NES_DBG_QP, "RQ size=%u, SQ Size=%u\n", rq_size, sq_size);
1068
1069                        ret = nes_alloc_resource(nesadapter, nesadapter->allocated_qps,
1070                                        nesadapter->max_qp, &qp_num, &nesadapter->next_qp, NES_RESOURCE_QP);
1071                        if (ret) {
1072                                return ERR_PTR(ret);
1073                        }
1074
1075                        /* Need 512 (actually now 1024) byte alignment on this structure */
1076                        mem = kzalloc(sizeof(*nesqp)+NES_SW_CONTEXT_ALIGN-1, GFP_KERNEL);
1077                        if (!mem) {
1078                                nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1079                                nes_debug(NES_DBG_QP, "Unable to allocate QP\n");
1080                                return ERR_PTR(-ENOMEM);
1081                        }
1082                        u64nesqp = (unsigned long)mem;
1083                        u64nesqp += ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1084                        u64temp = ((u64)NES_SW_CONTEXT_ALIGN) - 1;
1085                        u64nesqp &= ~u64temp;
1086                        nesqp = (struct nes_qp *)(unsigned long)u64nesqp;
1087                        /* nes_debug(NES_DBG_QP, "nesqp=%p, allocated buffer=%p.  Rounded to closest %u\n",
1088                                        nesqp, mem, NES_SW_CONTEXT_ALIGN); */
1089                        nesqp->allocated_buffer = mem;
1090
1091                        if (udata) {
1092                                if (ib_copy_from_udata(&req, udata, sizeof(struct nes_create_qp_req))) {
1093                                        nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1094                                        kfree(nesqp->allocated_buffer);
1095                                        nes_debug(NES_DBG_QP, "ib_copy_from_udata() Failed \n");
1096                                        return ERR_PTR(-EFAULT);
1097                                }
1098                                if (req.user_wqe_buffers) {
1099                                        virt_wqs = 1;
1100                                }
1101                                if (req.user_qp_buffer)
1102                                        nesqp->nesuqp_addr = req.user_qp_buffer;
1103                                if ((ibpd->uobject) && (ibpd->uobject->context)) {
1104                                        nesqp->user_mode = 1;
1105                                        nes_ucontext = to_nesucontext(ibpd->uobject->context);
1106                                        if (virt_wqs) {
1107                                                err = 1;
1108                                                list_for_each_entry(nespbl, &nes_ucontext->qp_reg_mem_list, list) {
1109                                                        if (nespbl->user_base == (unsigned long )req.user_wqe_buffers) {
1110                                                                list_del(&nespbl->list);
1111                                                                err = 0;
1112                                                                nes_debug(NES_DBG_QP, "Found PBL for virtual QP. nespbl=%p. user_base=0x%lx\n",
1113                                                                          nespbl, nespbl->user_base);
1114                                                                break;
1115                                                        }
1116                                                }
1117                                                if (err) {
1118                                                        nes_debug(NES_DBG_QP, "Didn't Find PBL for virtual QP. address = %llx.\n",
1119                                                                  (long long unsigned int)req.user_wqe_buffers);
1120                                                        nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1121                                                        kfree(nesqp->allocated_buffer);
1122                                                        return ERR_PTR(-EFAULT);
1123                                                }
1124                                        }
1125
1126                                        nes_ucontext = to_nesucontext(ibpd->uobject->context);
1127                                        nesqp->mmap_sq_db_index =
1128                                                find_next_zero_bit(nes_ucontext->allocated_wqs,
1129                                                                   NES_MAX_USER_WQ_REGIONS, nes_ucontext->first_free_wq);
1130                                        /* nes_debug(NES_DBG_QP, "find_first_zero_biton wqs returned %u\n",
1131                                                        nespd->mmap_db_index); */
1132                                        if (nesqp->mmap_sq_db_index >= NES_MAX_USER_WQ_REGIONS) {
1133                                                nes_debug(NES_DBG_QP,
1134                                                          "db index > max user regions, failing create QP\n");
1135                                                nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1136                                                if (virt_wqs) {
1137                                                        pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1138                                                                            nespbl->pbl_pbase);
1139                                                        kfree(nespbl);
1140                                                }
1141                                                kfree(nesqp->allocated_buffer);
1142                                                return ERR_PTR(-ENOMEM);
1143                                        }
1144                                        set_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1145                                        nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = nesqp;
1146                                        nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index + 1;
1147                                } else {
1148                                        nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1149                                        kfree(nesqp->allocated_buffer);
1150                                        return ERR_PTR(-EFAULT);
1151                                }
1152                        }
1153                        err = (!virt_wqs) ? nes_setup_mmap_qp(nesqp, nesvnic, sq_size, rq_size) :
1154                                        nes_setup_virt_qp(nesqp, nespbl, nesvnic, sq_size, rq_size);
1155                        if (err) {
1156                                nes_debug(NES_DBG_QP,
1157                                          "error geting qp mem code = %d\n", err);
1158                                nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1159                                kfree(nesqp->allocated_buffer);
1160                                return ERR_PTR(-ENOMEM);
1161                        }
1162
1163                        nesqp->hwqp.sq_size = sq_size;
1164                        nesqp->hwqp.sq_encoded_size = sq_encoded_size;
1165                        nesqp->hwqp.sq_head = 1;
1166                        nesqp->hwqp.rq_size = rq_size;
1167                        nesqp->hwqp.rq_encoded_size = rq_encoded_size;
1168                        /* nes_debug(NES_DBG_QP, "nesqp->nesqp_context_pbase = %p\n",
1169                                        (void *)nesqp->nesqp_context_pbase);
1170                        */
1171                        nesqp->hwqp.qp_id = qp_num;
1172                        nesqp->ibqp.qp_num = nesqp->hwqp.qp_id;
1173                        nesqp->nespd = nespd;
1174
1175                        nescq = to_nescq(init_attr->send_cq);
1176                        nesqp->nesscq = nescq;
1177                        nescq = to_nescq(init_attr->recv_cq);
1178                        nesqp->nesrcq = nescq;
1179
1180                        nesqp->nesqp_context->misc |= cpu_to_le32((u32)PCI_FUNC(nesdev->pcidev->devfn) <<
1181                                        NES_QPCONTEXT_MISC_PCI_FCN_SHIFT);
1182                        nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.rq_encoded_size <<
1183                                        NES_QPCONTEXT_MISC_RQ_SIZE_SHIFT);
1184                        nesqp->nesqp_context->misc |= cpu_to_le32((u32)nesqp->hwqp.sq_encoded_size <<
1185                                        NES_QPCONTEXT_MISC_SQ_SIZE_SHIFT);
1186                        if (!udata) {
1187                                nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_PRIV_EN);
1188                                nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_FAST_REGISTER_EN);
1189                        }
1190                        nesqp->nesqp_context->cqs = cpu_to_le32(nesqp->nesscq->hw_cq.cq_number +
1191                                        ((u32)nesqp->nesrcq->hw_cq.cq_number << 16));
1192                        u64temp = (u64)nesqp->hwqp.sq_pbase;
1193                        nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1194                        nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1195
1196
1197                        if (!virt_wqs) {
1198                                u64temp = (u64)nesqp->hwqp.sq_pbase;
1199                                nesqp->nesqp_context->sq_addr_low = cpu_to_le32((u32)u64temp);
1200                                nesqp->nesqp_context->sq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1201                                u64temp = (u64)nesqp->hwqp.rq_pbase;
1202                                nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1203                                nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1204                        } else {
1205                                u64temp = (u64)nesqp->pbl_pbase;
1206                                nesqp->nesqp_context->rq_addr_low = cpu_to_le32((u32)u64temp);
1207                                nesqp->nesqp_context->rq_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1208                        }
1209
1210                        /* nes_debug(NES_DBG_QP, "next_qp_nic_index=%u, using nic_index=%d\n",
1211                                        nesvnic->next_qp_nic_index,
1212                                        nesvnic->qp_nic_index[nesvnic->next_qp_nic_index]); */
1213                        spin_lock_irqsave(&nesdev->cqp.lock, flags);
1214                        nesqp->nesqp_context->misc2 |= cpu_to_le32(
1215                                        (u32)nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] <<
1216                                        NES_QPCONTEXT_MISC2_NIC_INDEX_SHIFT);
1217                        nesvnic->next_qp_nic_index++;
1218                        if ((nesvnic->next_qp_nic_index > 3) ||
1219                                        (nesvnic->qp_nic_index[nesvnic->next_qp_nic_index] == 0xf)) {
1220                                nesvnic->next_qp_nic_index = 0;
1221                        }
1222                        spin_unlock_irqrestore(&nesdev->cqp.lock, flags);
1223
1224                        nesqp->nesqp_context->pd_index_wscale |= cpu_to_le32((u32)nesqp->nespd->pd_id << 16);
1225                        u64temp = (u64)nesqp->hwqp.q2_pbase;
1226                        nesqp->nesqp_context->q2_addr_low = cpu_to_le32((u32)u64temp);
1227                        nesqp->nesqp_context->q2_addr_high = cpu_to_le32((u32)(u64temp >> 32));
1228                        nesqp->nesqp_context->aeq_token_low =  cpu_to_le32((u32)((unsigned long)(nesqp)));
1229                        nesqp->nesqp_context->aeq_token_high =  cpu_to_le32((u32)(upper_32_bits((unsigned long)(nesqp))));
1230                        nesqp->nesqp_context->ird_ord_sizes = cpu_to_le32(NES_QPCONTEXT_ORDIRD_ALSMM |
1231                                        NES_QPCONTEXT_ORDIRD_AAH |
1232                                        ((((u32)nesadapter->max_irrq_wr) <<
1233                                        NES_QPCONTEXT_ORDIRD_IRDSIZE_SHIFT) & NES_QPCONTEXT_ORDIRD_IRDSIZE_MASK));
1234                        if (disable_mpa_crc) {
1235                                nes_debug(NES_DBG_QP, "Disabling MPA crc checking due to module option.\n");
1236                                nesqp->nesqp_context->ird_ord_sizes |= cpu_to_le32(NES_QPCONTEXT_ORDIRD_RNMC);
1237                        }
1238
1239
1240                        /* Create the QP */
1241                        cqp_request = nes_get_cqp_request(nesdev);
1242                        if (cqp_request == NULL) {
1243                                nes_debug(NES_DBG_QP, "Failed to get a cqp_request\n");
1244                                nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1245                                nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1246                                kfree(nesqp->allocated_buffer);
1247                                return ERR_PTR(-ENOMEM);
1248                        }
1249                        cqp_request->waiting = 1;
1250                        cqp_wqe = &cqp_request->cqp_wqe;
1251
1252                        if (!virt_wqs) {
1253                                opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP |
1254                                        NES_CQP_QP_IWARP_STATE_IDLE;
1255                        } else {
1256                                opcode = NES_CQP_CREATE_QP | NES_CQP_QP_TYPE_IWARP | NES_CQP_QP_VIRT_WQS |
1257                                        NES_CQP_QP_IWARP_STATE_IDLE;
1258                        }
1259                        opcode |= NES_CQP_QP_CQS_VALID;
1260                        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1261                        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1262                        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
1263
1264                        u64temp = (u64)nesqp->nesqp_context_pbase;
1265                        set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, u64temp);
1266
1267                        atomic_set(&cqp_request->refcount, 2);
1268                        nes_post_cqp_request(nesdev, cqp_request);
1269
1270                        /* Wait for CQP */
1271                        nes_debug(NES_DBG_QP, "Waiting for create iWARP QP%u to complete.\n",
1272                                        nesqp->hwqp.qp_id);
1273                        ret = wait_event_timeout(cqp_request->waitq,
1274                                        (cqp_request->request_done != 0), NES_EVENT_TIMEOUT);
1275                        nes_debug(NES_DBG_QP, "Create iwarp QP%u completed, wait_event_timeout ret=%u,"
1276                                        " nesdev->cqp_head = %u, nesdev->cqp.sq_tail = %u,"
1277                                        " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1278                                        nesqp->hwqp.qp_id, ret, nesdev->cqp.sq_head, nesdev->cqp.sq_tail,
1279                                        cqp_request->major_code, cqp_request->minor_code);
1280                        if ((!ret) || (cqp_request->major_code)) {
1281                                nes_put_cqp_request(nesdev, cqp_request);
1282                                nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1283                                nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1284                                kfree(nesqp->allocated_buffer);
1285                                if (!ret) {
1286                                        return ERR_PTR(-ETIME);
1287                                } else {
1288                                        return ERR_PTR(-EIO);
1289                                }
1290                        }
1291
1292                        nes_put_cqp_request(nesdev, cqp_request);
1293
1294                        if (ibpd->uobject) {
1295                                uresp.mmap_sq_db_index = nesqp->mmap_sq_db_index;
1296                                uresp.mmap_rq_db_index = 0;
1297                                uresp.actual_sq_size = sq_size;
1298                                uresp.actual_rq_size = rq_size;
1299                                uresp.qp_id = nesqp->hwqp.qp_id;
1300                                uresp.nes_drv_opt = nes_drv_opt;
1301                                if (ib_copy_to_udata(udata, &uresp, sizeof uresp)) {
1302                                        nes_free_resource(nesadapter, nesadapter->allocated_qps, qp_num);
1303                                        nes_free_qp_mem(nesdev, nesqp,virt_wqs);
1304                                        kfree(nesqp->allocated_buffer);
1305                                        return ERR_PTR(-EFAULT);
1306                                }
1307                        }
1308
1309                        nes_debug(NES_DBG_QP, "QP%u structure located @%p.Size = %u.\n",
1310                                        nesqp->hwqp.qp_id, nesqp, (u32)sizeof(*nesqp));
1311                        spin_lock_init(&nesqp->lock);
1312                        nes_add_ref(&nesqp->ibqp);
1313                        break;
1314                default:
1315                        nes_debug(NES_DBG_QP, "Invalid QP type: %d\n", init_attr->qp_type);
1316                        return ERR_PTR(-EINVAL);
1317        }
1318
1319        nesqp->sig_all = (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR);
1320        init_timer(&nesqp->terminate_timer);
1321        nesqp->terminate_timer.function = nes_terminate_timeout;
1322        nesqp->terminate_timer.data = (unsigned long)nesqp;
1323
1324        /* update the QP table */
1325        nesdev->nesadapter->qp_table[nesqp->hwqp.qp_id-NES_FIRST_QPN] = nesqp;
1326        nes_debug(NES_DBG_QP, "netdev refcnt=%u\n",
1327                        netdev_refcnt_read(nesvnic->netdev));
1328
1329        return &nesqp->ibqp;
1330}
1331
1332/**
1333 * nes_clean_cq
1334 */
1335static void nes_clean_cq(struct nes_qp *nesqp, struct nes_cq *nescq)
1336{
1337        u32 cq_head;
1338        u32 lo;
1339        u32 hi;
1340        u64 u64temp;
1341        unsigned long flags = 0;
1342
1343        spin_lock_irqsave(&nescq->lock, flags);
1344
1345        cq_head = nescq->hw_cq.cq_head;
1346        while (le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_VALID) {
1347                rmb();
1348                lo = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
1349                hi = le32_to_cpu(nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX]);
1350                u64temp = (((u64)hi) << 32) | ((u64)lo);
1351                u64temp &= ~(NES_SW_CONTEXT_ALIGN-1);
1352                if (u64temp == (u64)(unsigned long)nesqp) {
1353                        /* Zero the context value so cqe will be ignored */
1354                        nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] = 0;
1355                        nescq->hw_cq.cq_vbase[cq_head].cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX] = 0;
1356                }
1357
1358                if (++cq_head >= nescq->hw_cq.cq_size)
1359                        cq_head = 0;
1360        }
1361
1362        spin_unlock_irqrestore(&nescq->lock, flags);
1363}
1364
1365
1366/**
1367 * nes_destroy_qp
1368 */
1369static int nes_destroy_qp(struct ib_qp *ibqp)
1370{
1371        struct nes_qp *nesqp = to_nesqp(ibqp);
1372        struct nes_ucontext *nes_ucontext;
1373        struct ib_qp_attr attr;
1374        struct iw_cm_id *cm_id;
1375        struct iw_cm_event cm_event;
1376        int ret = 0;
1377
1378        atomic_inc(&sw_qps_destroyed);
1379        nesqp->destroyed = 1;
1380
1381        /* Blow away the connection if it exists. */
1382        if (nesqp->ibqp_state >= IB_QPS_INIT && nesqp->ibqp_state <= IB_QPS_RTS) {
1383                /* if (nesqp->ibqp_state == IB_QPS_RTS) { */
1384                attr.qp_state = IB_QPS_ERR;
1385                nes_modify_qp(&nesqp->ibqp, &attr, IB_QP_STATE, NULL);
1386        }
1387
1388        if (((nesqp->ibqp_state == IB_QPS_INIT) ||
1389                        (nesqp->ibqp_state == IB_QPS_RTR)) && (nesqp->cm_id)) {
1390                cm_id = nesqp->cm_id;
1391                cm_event.event = IW_CM_EVENT_CONNECT_REPLY;
1392                cm_event.status = -ETIMEDOUT;
1393                cm_event.local_addr = cm_id->local_addr;
1394                cm_event.remote_addr = cm_id->remote_addr;
1395                cm_event.private_data = NULL;
1396                cm_event.private_data_len = 0;
1397
1398                nes_debug(NES_DBG_QP, "Generating a CM Timeout Event for "
1399                                "QP%u. cm_id = %p, refcount = %u. \n",
1400                                nesqp->hwqp.qp_id, cm_id, atomic_read(&nesqp->refcount));
1401
1402                cm_id->rem_ref(cm_id);
1403                ret = cm_id->event_handler(cm_id, &cm_event);
1404                if (ret)
1405                        nes_debug(NES_DBG_QP, "OFA CM event_handler returned, ret=%d\n", ret);
1406        }
1407
1408        if (nesqp->user_mode) {
1409                if ((ibqp->uobject)&&(ibqp->uobject->context)) {
1410                        nes_ucontext = to_nesucontext(ibqp->uobject->context);
1411                        clear_bit(nesqp->mmap_sq_db_index, nes_ucontext->allocated_wqs);
1412                        nes_ucontext->mmap_nesqp[nesqp->mmap_sq_db_index] = NULL;
1413                        if (nes_ucontext->first_free_wq > nesqp->mmap_sq_db_index) {
1414                                nes_ucontext->first_free_wq = nesqp->mmap_sq_db_index;
1415                        }
1416                }
1417                if (nesqp->pbl_pbase && nesqp->sq_kmapped) {
1418                        nesqp->sq_kmapped = 0;
1419                        kunmap(nesqp->page);
1420                }
1421        } else {
1422                /* Clean any pending completions from the cq(s) */
1423                if (nesqp->nesscq)
1424                        nes_clean_cq(nesqp, nesqp->nesscq);
1425
1426                if ((nesqp->nesrcq) && (nesqp->nesrcq != nesqp->nesscq))
1427                        nes_clean_cq(nesqp, nesqp->nesrcq);
1428        }
1429        nes_rem_ref(&nesqp->ibqp);
1430        return 0;
1431}
1432
1433
1434/**
1435 * nes_create_cq
1436 */
1437static struct ib_cq *nes_create_cq(struct ib_device *ibdev,
1438                                   const struct ib_cq_init_attr *attr,
1439                                   struct ib_ucontext *context,
1440                                   struct ib_udata *udata)
1441{
1442        int entries = attr->cqe;
1443        u64 u64temp;
1444        struct nes_vnic *nesvnic = to_nesvnic(ibdev);
1445        struct nes_device *nesdev = nesvnic->nesdev;
1446        struct nes_adapter *nesadapter = nesdev->nesadapter;
1447        struct nes_cq *nescq;
1448        struct nes_ucontext *nes_ucontext = NULL;
1449        struct nes_cqp_request *cqp_request;
1450        void *mem = NULL;
1451        struct nes_hw_cqp_wqe *cqp_wqe;
1452        struct nes_pbl *nespbl = NULL;
1453        struct nes_create_cq_req req;
1454        struct nes_create_cq_resp resp;
1455        u32 cq_num = 0;
1456        u32 opcode = 0;
1457        u32 pbl_entries = 1;
1458        int err;
1459        unsigned long flags;
1460        int ret;
1461
1462        if (attr->flags)
1463                return ERR_PTR(-EINVAL);
1464
1465        if (entries > nesadapter->max_cqe)
1466                return ERR_PTR(-EINVAL);
1467
1468        err = nes_alloc_resource(nesadapter, nesadapter->allocated_cqs,
1469                        nesadapter->max_cq, &cq_num, &nesadapter->next_cq, NES_RESOURCE_CQ);
1470        if (err) {
1471                return ERR_PTR(err);
1472        }
1473
1474        nescq = kzalloc(sizeof(struct nes_cq), GFP_KERNEL);
1475        if (!nescq) {
1476                nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1477                nes_debug(NES_DBG_CQ, "Unable to allocate nes_cq struct\n");
1478                return ERR_PTR(-ENOMEM);
1479        }
1480
1481        nescq->hw_cq.cq_size = max(entries + 1, 5);
1482        nescq->hw_cq.cq_number = cq_num;
1483        nescq->ibcq.cqe = nescq->hw_cq.cq_size - 1;
1484
1485
1486        if (context) {
1487                nes_ucontext = to_nesucontext(context);
1488                if (ib_copy_from_udata(&req, udata, sizeof (struct nes_create_cq_req))) {
1489                        nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1490                        kfree(nescq);
1491                        return ERR_PTR(-EFAULT);
1492                }
1493                nesvnic->mcrq_ucontext = nes_ucontext;
1494                nes_ucontext->mcrqf = req.mcrqf;
1495                if (nes_ucontext->mcrqf) {
1496                        if (nes_ucontext->mcrqf & 0x80000000)
1497                                nescq->hw_cq.cq_number = nesvnic->nic.qp_id + 28 + 2 * ((nes_ucontext->mcrqf & 0xf) - 1);
1498                        else if (nes_ucontext->mcrqf & 0x40000000)
1499                                nescq->hw_cq.cq_number = nes_ucontext->mcrqf & 0xffff;
1500                        else
1501                                nescq->hw_cq.cq_number = nesvnic->mcrq_qp_id + nes_ucontext->mcrqf-1;
1502                        nescq->mcrqf = nes_ucontext->mcrqf;
1503                        nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1504                }
1505                nes_debug(NES_DBG_CQ, "CQ Virtual Address = %08lX, size = %u.\n",
1506                                (unsigned long)req.user_cq_buffer, entries);
1507                err = 1;
1508                list_for_each_entry(nespbl, &nes_ucontext->cq_reg_mem_list, list) {
1509                        if (nespbl->user_base == (unsigned long )req.user_cq_buffer) {
1510                                list_del(&nespbl->list);
1511                                err = 0;
1512                                nes_debug(NES_DBG_CQ, "Found PBL for virtual CQ. nespbl=%p.\n",
1513                                                nespbl);
1514                                break;
1515                        }
1516                }
1517                if (err) {
1518                        nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1519                        kfree(nescq);
1520                        return ERR_PTR(-EFAULT);
1521                }
1522
1523                pbl_entries = nespbl->pbl_size >> 3;
1524                nescq->cq_mem_size = 0;
1525        } else {
1526                nescq->cq_mem_size = nescq->hw_cq.cq_size * sizeof(struct nes_hw_cqe);
1527                nes_debug(NES_DBG_CQ, "Attempting to allocate pci memory (%u entries, %u bytes) for CQ%u.\n",
1528                                entries, nescq->cq_mem_size, nescq->hw_cq.cq_number);
1529
1530                /* allocate the physical buffer space */
1531                mem = pci_zalloc_consistent(nesdev->pcidev, nescq->cq_mem_size,
1532                                            &nescq->hw_cq.cq_pbase);
1533                if (!mem) {
1534                        printk(KERN_ERR PFX "Unable to allocate pci memory for cq\n");
1535                        nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1536                        kfree(nescq);
1537                        return ERR_PTR(-ENOMEM);
1538                }
1539
1540                nescq->hw_cq.cq_vbase = mem;
1541                nescq->hw_cq.cq_head = 0;
1542                nes_debug(NES_DBG_CQ, "CQ%u virtual address @ %p, phys = 0x%08X\n",
1543                                nescq->hw_cq.cq_number, nescq->hw_cq.cq_vbase,
1544                                (u32)nescq->hw_cq.cq_pbase);
1545        }
1546
1547        nescq->hw_cq.ce_handler = nes_iwarp_ce_handler;
1548        spin_lock_init(&nescq->lock);
1549
1550        /* send CreateCQ request to CQP */
1551        cqp_request = nes_get_cqp_request(nesdev);
1552        if (cqp_request == NULL) {
1553                nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1554                if (!context)
1555                        pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1556                                        nescq->hw_cq.cq_pbase);
1557                else {
1558                        pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1559                                            nespbl->pbl_vbase, nespbl->pbl_pbase);
1560                        kfree(nespbl);
1561                }
1562
1563                nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1564                kfree(nescq);
1565                return ERR_PTR(-ENOMEM);
1566        }
1567        cqp_request->waiting = 1;
1568        cqp_wqe = &cqp_request->cqp_wqe;
1569
1570        opcode = NES_CQP_CREATE_CQ | NES_CQP_CQ_CEQ_VALID |
1571                        NES_CQP_CQ_CHK_OVERFLOW |
1572                        NES_CQP_CQ_CEQE_MASK | ((u32)nescq->hw_cq.cq_size << 16);
1573
1574        spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1575
1576        if (pbl_entries != 1) {
1577                if (pbl_entries > 32) {
1578                        /* use 4k pbl */
1579                        nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 4k PBL\n", pbl_entries);
1580                        if (nesadapter->free_4kpbl == 0) {
1581                                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1582                                nes_free_cqp_request(nesdev, cqp_request);
1583                                if (!context)
1584                                        pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1585                                                        nescq->hw_cq.cq_pbase);
1586                                else {
1587                                        pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1588                                                            nespbl->pbl_vbase, nespbl->pbl_pbase);
1589                                        kfree(nespbl);
1590                                }
1591                                nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1592                                kfree(nescq);
1593                                return ERR_PTR(-ENOMEM);
1594                        } else {
1595                                opcode |= (NES_CQP_CQ_VIRT | NES_CQP_CQ_4KB_CHUNK);
1596                                nescq->virtual_cq = 2;
1597                                nesadapter->free_4kpbl--;
1598                        }
1599                } else {
1600                        /* use 256 byte pbl */
1601                        nes_debug(NES_DBG_CQ, "pbl_entries=%u, use a 256 byte PBL\n", pbl_entries);
1602                        if (nesadapter->free_256pbl == 0) {
1603                                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1604                                nes_free_cqp_request(nesdev, cqp_request);
1605                                if (!context)
1606                                        pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1607                                                        nescq->hw_cq.cq_pbase);
1608                                else {
1609                                        pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1610                                                            nespbl->pbl_vbase, nespbl->pbl_pbase);
1611                                        kfree(nespbl);
1612                                }
1613                                nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1614                                kfree(nescq);
1615                                return ERR_PTR(-ENOMEM);
1616                        } else {
1617                                opcode |= NES_CQP_CQ_VIRT;
1618                                nescq->virtual_cq = 1;
1619                                nesadapter->free_256pbl--;
1620                        }
1621                }
1622        }
1623
1624        spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1625
1626        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1627        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1628        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1629                        (nescq->hw_cq.cq_number | ((u32)nesdev->ceq_index << 16)));
1630
1631        if (context) {
1632                if (pbl_entries != 1)
1633                        u64temp = (u64)nespbl->pbl_pbase;
1634                else
1635                        u64temp = le64_to_cpu(nespbl->pbl_vbase[0]);
1636                set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX,
1637                                nes_ucontext->mmap_db_index[0]);
1638        } else {
1639                u64temp = (u64)nescq->hw_cq.cq_pbase;
1640                cqp_wqe->wqe_words[NES_CQP_CQ_WQE_DOORBELL_INDEX_HIGH_IDX] = 0;
1641        }
1642        set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_CQ_WQE_PBL_LOW_IDX, u64temp);
1643        cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] = 0;
1644        u64temp = (u64)(unsigned long)&nescq->hw_cq;
1645        cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_LOW_IDX] =
1646                        cpu_to_le32((u32)(u64temp >> 1));
1647        cqp_wqe->wqe_words[NES_CQP_CQ_WQE_CQ_CONTEXT_HIGH_IDX] =
1648                        cpu_to_le32(((u32)((u64temp) >> 33)) & 0x7FFFFFFF);
1649
1650        atomic_set(&cqp_request->refcount, 2);
1651        nes_post_cqp_request(nesdev, cqp_request);
1652
1653        /* Wait for CQP */
1654        nes_debug(NES_DBG_CQ, "Waiting for create iWARP CQ%u to complete.\n",
1655                        nescq->hw_cq.cq_number);
1656        ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1657                        NES_EVENT_TIMEOUT * 2);
1658        nes_debug(NES_DBG_CQ, "Create iWARP CQ%u completed, wait_event_timeout ret = %d.\n",
1659                        nescq->hw_cq.cq_number, ret);
1660        if ((!ret) || (cqp_request->major_code)) {
1661                nes_put_cqp_request(nesdev, cqp_request);
1662                if (!context)
1663                        pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size, mem,
1664                                        nescq->hw_cq.cq_pbase);
1665                else {
1666                        pci_free_consistent(nesdev->pcidev, nespbl->pbl_size,
1667                                            nespbl->pbl_vbase, nespbl->pbl_pbase);
1668                        kfree(nespbl);
1669                }
1670                nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1671                kfree(nescq);
1672                return ERR_PTR(-EIO);
1673        }
1674        nes_put_cqp_request(nesdev, cqp_request);
1675
1676        if (context) {
1677                /* free the nespbl */
1678                pci_free_consistent(nesdev->pcidev, nespbl->pbl_size, nespbl->pbl_vbase,
1679                                nespbl->pbl_pbase);
1680                kfree(nespbl);
1681                resp.cq_id = nescq->hw_cq.cq_number;
1682                resp.cq_size = nescq->hw_cq.cq_size;
1683                resp.mmap_db_index = 0;
1684                if (ib_copy_to_udata(udata, &resp, sizeof resp - sizeof resp.reserved)) {
1685                        nes_free_resource(nesadapter, nesadapter->allocated_cqs, cq_num);
1686                        kfree(nescq);
1687                        return ERR_PTR(-EFAULT);
1688                }
1689        }
1690
1691        return &nescq->ibcq;
1692}
1693
1694
1695/**
1696 * nes_destroy_cq
1697 */
1698static int nes_destroy_cq(struct ib_cq *ib_cq)
1699{
1700        struct nes_cq *nescq;
1701        struct nes_device *nesdev;
1702        struct nes_vnic *nesvnic;
1703        struct nes_adapter *nesadapter;
1704        struct nes_hw_cqp_wqe *cqp_wqe;
1705        struct nes_cqp_request *cqp_request;
1706        unsigned long flags;
1707        u32 opcode = 0;
1708        int ret;
1709
1710        if (ib_cq == NULL)
1711                return 0;
1712
1713        nescq = to_nescq(ib_cq);
1714        nesvnic = to_nesvnic(ib_cq->device);
1715        nesdev = nesvnic->nesdev;
1716        nesadapter = nesdev->nesadapter;
1717
1718        nes_debug(NES_DBG_CQ, "Destroy CQ%u\n", nescq->hw_cq.cq_number);
1719
1720        /* Send DestroyCQ request to CQP */
1721        cqp_request = nes_get_cqp_request(nesdev);
1722        if (cqp_request == NULL) {
1723                nes_debug(NES_DBG_CQ, "Failed to get a cqp_request.\n");
1724                return -ENOMEM;
1725        }
1726        cqp_request->waiting = 1;
1727        cqp_wqe = &cqp_request->cqp_wqe;
1728        opcode = NES_CQP_DESTROY_CQ | (nescq->hw_cq.cq_size << 16);
1729        spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1730        if (nescq->virtual_cq == 1) {
1731                nesadapter->free_256pbl++;
1732                if (nesadapter->free_256pbl > nesadapter->max_256pbl) {
1733                        printk(KERN_ERR PFX "%s: free 256B PBLs(%u) has exceeded the max(%u)\n",
1734                                        __func__, nesadapter->free_256pbl, nesadapter->max_256pbl);
1735                }
1736        } else if (nescq->virtual_cq == 2) {
1737                nesadapter->free_4kpbl++;
1738                if (nesadapter->free_4kpbl > nesadapter->max_4kpbl) {
1739                        printk(KERN_ERR PFX "%s: free 4K PBLs(%u) has exceeded the max(%u)\n",
1740                                        __func__, nesadapter->free_4kpbl, nesadapter->max_4kpbl);
1741                }
1742                opcode |= NES_CQP_CQ_4KB_CHUNK;
1743        }
1744
1745        spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1746
1747        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1748        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1749        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX,
1750                (nescq->hw_cq.cq_number | ((u32)PCI_FUNC(nesdev->pcidev->devfn) << 16)));
1751        if (!nescq->mcrqf)
1752                nes_free_resource(nesadapter, nesadapter->allocated_cqs, nescq->hw_cq.cq_number);
1753
1754        atomic_set(&cqp_request->refcount, 2);
1755        nes_post_cqp_request(nesdev, cqp_request);
1756
1757        /* Wait for CQP */
1758        nes_debug(NES_DBG_CQ, "Waiting for destroy iWARP CQ%u to complete.\n",
1759                        nescq->hw_cq.cq_number);
1760        ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1761                        NES_EVENT_TIMEOUT);
1762        nes_debug(NES_DBG_CQ, "Destroy iWARP CQ%u completed, wait_event_timeout ret = %u,"
1763                        " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1764                        nescq->hw_cq.cq_number, ret, cqp_request->major_code,
1765                        cqp_request->minor_code);
1766        if (!ret) {
1767                nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy timeout expired\n",
1768                                        nescq->hw_cq.cq_number);
1769                ret = -ETIME;
1770        } else if (cqp_request->major_code) {
1771                nes_debug(NES_DBG_CQ, "iWARP CQ%u destroy failed\n",
1772                                        nescq->hw_cq.cq_number);
1773                ret = -EIO;
1774        } else {
1775                ret = 0;
1776        }
1777        nes_put_cqp_request(nesdev, cqp_request);
1778
1779        if (nescq->cq_mem_size)
1780                pci_free_consistent(nesdev->pcidev, nescq->cq_mem_size,
1781                                    nescq->hw_cq.cq_vbase, nescq->hw_cq.cq_pbase);
1782        kfree(nescq);
1783
1784        return ret;
1785}
1786
1787/**
1788 * root_256
1789 */
1790static u32 root_256(struct nes_device *nesdev,
1791                    struct nes_root_vpbl *root_vpbl,
1792                    struct nes_root_vpbl *new_root,
1793                    u16 pbl_count_4k)
1794{
1795        u64 leaf_pbl;
1796        int i, j, k;
1797
1798        if (pbl_count_4k == 1) {
1799                new_root->pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
1800                                                512, &new_root->pbl_pbase);
1801
1802                if (new_root->pbl_vbase == NULL)
1803                        return 0;
1804
1805                leaf_pbl = (u64)root_vpbl->pbl_pbase;
1806                for (i = 0; i < 16; i++) {
1807                        new_root->pbl_vbase[i].pa_low =
1808                                cpu_to_le32((u32)leaf_pbl);
1809                        new_root->pbl_vbase[i].pa_high =
1810                                cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1811                        leaf_pbl += 256;
1812                }
1813        } else {
1814                for (i = 3; i >= 0; i--) {
1815                        j = i * 16;
1816                        root_vpbl->pbl_vbase[j] = root_vpbl->pbl_vbase[i];
1817                        leaf_pbl = le32_to_cpu(root_vpbl->pbl_vbase[j].pa_low) +
1818                            (((u64)le32_to_cpu(root_vpbl->pbl_vbase[j].pa_high))
1819                                << 32);
1820                        for (k = 1; k < 16; k++) {
1821                                leaf_pbl += 256;
1822                                root_vpbl->pbl_vbase[j + k].pa_low =
1823                                                cpu_to_le32((u32)leaf_pbl);
1824                                root_vpbl->pbl_vbase[j + k].pa_high =
1825                                    cpu_to_le32((u32)((((u64)leaf_pbl) >> 32)));
1826                        }
1827                }
1828        }
1829
1830        return 1;
1831}
1832
1833
1834/**
1835 * nes_reg_mr
1836 */
1837static int nes_reg_mr(struct nes_device *nesdev, struct nes_pd *nespd,
1838                u32 stag, u64 region_length, struct nes_root_vpbl *root_vpbl,
1839                dma_addr_t single_buffer, u16 pbl_count_4k,
1840                u16 residual_page_count_4k, int acc, u64 *iova_start,
1841                u16 *actual_pbl_cnt, u8 *used_4k_pbls)
1842{
1843        struct nes_hw_cqp_wqe *cqp_wqe;
1844        struct nes_cqp_request *cqp_request;
1845        unsigned long flags;
1846        int ret;
1847        struct nes_adapter *nesadapter = nesdev->nesadapter;
1848        uint pg_cnt = 0;
1849        u16 pbl_count_256 = 0;
1850        u16 pbl_count = 0;
1851        u8  use_256_pbls = 0;
1852        u8  use_4k_pbls = 0;
1853        u16 use_two_level = (pbl_count_4k > 1) ? 1 : 0;
1854        struct nes_root_vpbl new_root = { 0, NULL, NULL };
1855        u32 opcode = 0;
1856        u16 major_code;
1857
1858        /* Register the region with the adapter */
1859        cqp_request = nes_get_cqp_request(nesdev);
1860        if (cqp_request == NULL) {
1861                nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
1862                return -ENOMEM;
1863        }
1864        cqp_request->waiting = 1;
1865        cqp_wqe = &cqp_request->cqp_wqe;
1866
1867        if (pbl_count_4k) {
1868                spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1869
1870                pg_cnt = ((pbl_count_4k - 1) * 512) + residual_page_count_4k;
1871                pbl_count_256 = (pg_cnt + 31) / 32;
1872                if (pg_cnt <= 32) {
1873                        if (pbl_count_256 <= nesadapter->free_256pbl)
1874                                use_256_pbls = 1;
1875                        else if (pbl_count_4k <= nesadapter->free_4kpbl)
1876                                use_4k_pbls = 1;
1877                } else if (pg_cnt <= 2048) {
1878                        if (((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) &&
1879                            (nesadapter->free_4kpbl > (nesadapter->max_4kpbl >> 1))) {
1880                                use_4k_pbls = 1;
1881                        } else if ((pbl_count_256 + 1) <= nesadapter->free_256pbl) {
1882                                use_256_pbls = 1;
1883                                use_two_level = 1;
1884                        } else if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1885                                use_4k_pbls = 1;
1886                        }
1887                } else {
1888                        if ((pbl_count_4k + 1) <= nesadapter->free_4kpbl)
1889                                use_4k_pbls = 1;
1890                }
1891
1892                if (use_256_pbls) {
1893                        pbl_count = pbl_count_256;
1894                        nesadapter->free_256pbl -= pbl_count + use_two_level;
1895                } else if (use_4k_pbls) {
1896                        pbl_count =  pbl_count_4k;
1897                        nesadapter->free_4kpbl -= pbl_count + use_two_level;
1898                } else {
1899                        spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1900                        nes_debug(NES_DBG_MR, "Out of Pbls\n");
1901                        nes_free_cqp_request(nesdev, cqp_request);
1902                        return -ENOMEM;
1903                }
1904
1905                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1906        }
1907
1908        if (use_256_pbls && use_two_level) {
1909                if (root_256(nesdev, root_vpbl, &new_root, pbl_count_4k) == 1) {
1910                        if (new_root.pbl_pbase != 0)
1911                                root_vpbl = &new_root;
1912                } else {
1913                        spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1914                        nesadapter->free_256pbl += pbl_count_256 + use_two_level;
1915                        use_256_pbls = 0;
1916
1917                        if (pbl_count_4k == 1)
1918                                use_two_level = 0;
1919                        pbl_count = pbl_count_4k;
1920
1921                        if ((pbl_count_4k + use_two_level) <= nesadapter->free_4kpbl) {
1922                                nesadapter->free_4kpbl -= pbl_count + use_two_level;
1923                                use_4k_pbls = 1;
1924                        }
1925                        spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1926
1927                        if (use_4k_pbls == 0)
1928                                return -ENOMEM;
1929                }
1930        }
1931
1932        opcode = NES_CQP_REGISTER_STAG | NES_CQP_STAG_RIGHTS_LOCAL_READ |
1933                                        NES_CQP_STAG_VA_TO | NES_CQP_STAG_MR;
1934        if (acc & IB_ACCESS_LOCAL_WRITE)
1935                opcode |= NES_CQP_STAG_RIGHTS_LOCAL_WRITE;
1936        if (acc & IB_ACCESS_REMOTE_WRITE)
1937                opcode |= NES_CQP_STAG_RIGHTS_REMOTE_WRITE | NES_CQP_STAG_REM_ACC_EN;
1938        if (acc & IB_ACCESS_REMOTE_READ)
1939                opcode |= NES_CQP_STAG_RIGHTS_REMOTE_READ | NES_CQP_STAG_REM_ACC_EN;
1940        if (acc & IB_ACCESS_MW_BIND)
1941                opcode |= NES_CQP_STAG_RIGHTS_WINDOW_BIND | NES_CQP_STAG_REM_ACC_EN;
1942
1943        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
1944        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX, opcode);
1945        set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_VA_LOW_IDX, *iova_start);
1946        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_LEN_LOW_IDX, region_length);
1947
1948        cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] =
1949                        cpu_to_le32((u32)(region_length >> 8) & 0xff000000);
1950        cqp_wqe->wqe_words[NES_CQP_STAG_WQE_LEN_HIGH_PD_IDX] |=
1951                        cpu_to_le32(nespd->pd_id & 0x00007fff);
1952        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, stag);
1953
1954        if (pbl_count == 0) {
1955                set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, single_buffer);
1956        } else {
1957                set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PA_LOW_IDX, root_vpbl->pbl_pbase);
1958                set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_BLK_COUNT_IDX, pbl_count);
1959                set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_PBL_LEN_IDX, (pg_cnt * 8));
1960
1961                if (use_4k_pbls)
1962                        cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX] |= cpu_to_le32(NES_CQP_STAG_PBL_BLK_SIZE);
1963        }
1964        barrier();
1965
1966        atomic_set(&cqp_request->refcount, 2);
1967        nes_post_cqp_request(nesdev, cqp_request);
1968
1969        /* Wait for CQP */
1970        ret = wait_event_timeout(cqp_request->waitq, (0 != cqp_request->request_done),
1971                        NES_EVENT_TIMEOUT);
1972        nes_debug(NES_DBG_MR, "Register STag 0x%08X completed, wait_event_timeout ret = %u,"
1973                        " CQP Major:Minor codes = 0x%04X:0x%04X.\n",
1974                        stag, ret, cqp_request->major_code, cqp_request->minor_code);
1975        major_code = cqp_request->major_code;
1976        nes_put_cqp_request(nesdev, cqp_request);
1977
1978        if ((!ret || major_code) && pbl_count != 0) {
1979                spin_lock_irqsave(&nesadapter->pbl_lock, flags);
1980                if (use_256_pbls)
1981                        nesadapter->free_256pbl += pbl_count + use_two_level;
1982                else if (use_4k_pbls)
1983                        nesadapter->free_4kpbl += pbl_count + use_two_level;
1984                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
1985        }
1986        if (new_root.pbl_pbase)
1987                pci_free_consistent(nesdev->pcidev, 512, new_root.pbl_vbase,
1988                                    new_root.pbl_pbase);
1989
1990        if (!ret)
1991                return -ETIME;
1992        else if (major_code)
1993                return -EIO;
1994
1995        *actual_pbl_cnt = pbl_count + use_two_level;
1996        *used_4k_pbls = use_4k_pbls;
1997        return 0;
1998}
1999
2000
2001/**
2002 * nes_reg_phys_mr
2003 */
2004struct ib_mr *nes_reg_phys_mr(struct ib_pd *ib_pd, u64 addr, u64 size,
2005                int acc, u64 *iova_start)
2006{
2007        u64 region_length;
2008        struct nes_pd *nespd = to_nespd(ib_pd);
2009        struct nes_vnic *nesvnic = to_nesvnic(ib_pd->device);
2010        struct nes_device *nesdev = nesvnic->nesdev;
2011        struct nes_adapter *nesadapter = nesdev->nesadapter;
2012        struct nes_mr *nesmr;
2013        struct ib_mr *ibmr;
2014        struct nes_vpbl vpbl;
2015        struct nes_root_vpbl root_vpbl;
2016        u32 stag;
2017        unsigned long mask;
2018        u32 stag_index = 0;
2019        u32 next_stag_index = 0;
2020        u32 driver_key = 0;
2021        int err = 0;
2022        int ret = 0;
2023        u16 pbl_count = 0;
2024        u8 single_page = 1;
2025        u8 stag_key = 0;
2026
2027        region_length = 0;
2028        vpbl.pbl_vbase = NULL;
2029        root_vpbl.pbl_vbase = NULL;
2030        root_vpbl.pbl_pbase = 0;
2031
2032        get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2033        stag_key = (u8)next_stag_index;
2034
2035        driver_key = 0;
2036
2037        next_stag_index >>= 8;
2038        next_stag_index %= nesadapter->max_mr;
2039
2040        if ((addr ^ *iova_start) & ~PAGE_MASK)
2041                return ERR_PTR(-EINVAL);
2042
2043        err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs, nesadapter->max_mr,
2044                        &stag_index, &next_stag_index, NES_RESOURCE_PHYS_MR);
2045        if (err) {
2046                return ERR_PTR(err);
2047        }
2048
2049        nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2050        if (!nesmr) {
2051                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2052                return ERR_PTR(-ENOMEM);
2053        }
2054
2055        /* Allocate a 4K buffer for the PBL */
2056        vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2057                        &vpbl.pbl_pbase);
2058        nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%016lX\n",
2059                        vpbl.pbl_vbase, (unsigned long)vpbl.pbl_pbase);
2060        if (!vpbl.pbl_vbase) {
2061                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2062                ibmr = ERR_PTR(-ENOMEM);
2063                kfree(nesmr);
2064                goto reg_phys_err;
2065        }
2066
2067
2068        mask = !size;
2069
2070        if (mask & ~PAGE_MASK) {
2071                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2072                nes_debug(NES_DBG_MR, "Invalid buffer addr or size\n");
2073                ibmr = ERR_PTR(-EINVAL);
2074                kfree(nesmr);
2075                goto reg_phys_err;
2076        }
2077
2078        region_length += size;
2079        vpbl.pbl_vbase[0].pa_low = cpu_to_le32((u32)addr & PAGE_MASK);
2080        vpbl.pbl_vbase[0].pa_high = cpu_to_le32((u32)((((u64)addr) >> 32)));
2081
2082        stag = stag_index << 8;
2083        stag |= driver_key;
2084        stag += (u32)stag_key;
2085
2086        nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%016lX,"
2087                        " length = 0x%016lX, index = 0x%08X\n",
2088                        stag, (unsigned long)*iova_start, (unsigned long)region_length, stag_index);
2089
2090        /* Make the leaf PBL the root if only one PBL */
2091        root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2092
2093        if (single_page) {
2094                pbl_count = 0;
2095        } else {
2096                pbl_count = 1;
2097        }
2098        ret = nes_reg_mr(nesdev, nespd, stag, region_length, &root_vpbl,
2099                        addr, pbl_count, 1, acc, iova_start,
2100                        &nesmr->pbls_used, &nesmr->pbl_4k);
2101
2102        if (ret == 0) {
2103                nesmr->ibmr.rkey = stag;
2104                nesmr->ibmr.lkey = stag;
2105                nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2106                ibmr = &nesmr->ibmr;
2107        } else {
2108                kfree(nesmr);
2109                ibmr = ERR_PTR(-ENOMEM);
2110        }
2111
2112reg_phys_err:
2113        /* single PBL case */
2114        pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase, vpbl.pbl_pbase);
2115        return ibmr;
2116}
2117
2118
2119/**
2120 * nes_get_dma_mr
2121 */
2122static struct ib_mr *nes_get_dma_mr(struct ib_pd *pd, int acc)
2123{
2124        u64 kva = 0;
2125
2126        nes_debug(NES_DBG_MR, "\n");
2127
2128        return nes_reg_phys_mr(pd, 0, 0xffffffffffULL, acc, &kva);
2129}
2130
2131/**
2132 * nes_reg_user_mr
2133 */
2134static struct ib_mr *nes_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
2135                u64 virt, int acc, struct ib_udata *udata)
2136{
2137        u64 iova_start;
2138        __le64 *pbl;
2139        u64 region_length;
2140        dma_addr_t last_dma_addr = 0;
2141        dma_addr_t first_dma_addr = 0;
2142        struct nes_pd *nespd = to_nespd(pd);
2143        struct nes_vnic *nesvnic = to_nesvnic(pd->device);
2144        struct nes_device *nesdev = nesvnic->nesdev;
2145        struct nes_adapter *nesadapter = nesdev->nesadapter;
2146        struct ib_mr *ibmr = ERR_PTR(-EINVAL);
2147        struct scatterlist *sg;
2148        struct nes_ucontext *nes_ucontext;
2149        struct nes_pbl *nespbl;
2150        struct nes_mr *nesmr;
2151        struct ib_umem *region;
2152        struct nes_mem_reg_req req;
2153        struct nes_vpbl vpbl;
2154        struct nes_root_vpbl root_vpbl;
2155        int entry, page_index;
2156        int page_count = 0;
2157        int err, pbl_depth = 0;
2158        int chunk_pages;
2159        int ret;
2160        u32 stag;
2161        u32 stag_index = 0;
2162        u32 next_stag_index;
2163        u32 driver_key;
2164        u32 root_pbl_index = 0;
2165        u32 cur_pbl_index = 0;
2166        u32 skip_pages;
2167        u16 pbl_count;
2168        u8 single_page = 1;
2169        u8 stag_key;
2170        int first_page = 1;
2171
2172        region = ib_umem_get(pd->uobject->context, start, length, acc, 0);
2173        if (IS_ERR(region)) {
2174                return (struct ib_mr *)region;
2175        }
2176
2177        nes_debug(NES_DBG_MR, "User base = 0x%lX, Virt base = 0x%lX, length = %u,"
2178                        " offset = %u, page size = %u.\n",
2179                        (unsigned long int)start, (unsigned long int)virt, (u32)length,
2180                        ib_umem_offset(region), region->page_size);
2181
2182        skip_pages = ((u32)ib_umem_offset(region)) >> 12;
2183
2184        if (ib_copy_from_udata(&req, udata, sizeof(req))) {
2185                ib_umem_release(region);
2186                return ERR_PTR(-EFAULT);
2187        }
2188        nes_debug(NES_DBG_MR, "Memory Registration type = %08X.\n", req.reg_type);
2189
2190        switch (req.reg_type) {
2191                case IWNES_MEMREG_TYPE_MEM:
2192                        pbl_depth = 0;
2193                        region_length = 0;
2194                        vpbl.pbl_vbase = NULL;
2195                        root_vpbl.pbl_vbase = NULL;
2196                        root_vpbl.pbl_pbase = 0;
2197
2198                        get_random_bytes(&next_stag_index, sizeof(next_stag_index));
2199                        stag_key = (u8)next_stag_index;
2200
2201                        driver_key = next_stag_index & 0x70000000;
2202
2203                        next_stag_index >>= 8;
2204                        next_stag_index %= nesadapter->max_mr;
2205
2206                        err = nes_alloc_resource(nesadapter, nesadapter->allocated_mrs,
2207                                        nesadapter->max_mr, &stag_index, &next_stag_index, NES_RESOURCE_USER_MR);
2208                        if (err) {
2209                                ib_umem_release(region);
2210                                return ERR_PTR(err);
2211                        }
2212
2213                        nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2214                        if (!nesmr) {
2215                                ib_umem_release(region);
2216                                nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2217                                return ERR_PTR(-ENOMEM);
2218                        }
2219                        nesmr->region = region;
2220
2221                        for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2222                                if (sg_dma_address(sg) & ~PAGE_MASK) {
2223                                        ib_umem_release(region);
2224                                        nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2225                                        nes_debug(NES_DBG_MR, "Unaligned Memory Buffer: 0x%x\n",
2226                                                  (unsigned int) sg_dma_address(sg));
2227                                        ibmr = ERR_PTR(-EINVAL);
2228                                        kfree(nesmr);
2229                                        goto reg_user_mr_err;
2230                                }
2231
2232                                if (!sg_dma_len(sg)) {
2233                                        ib_umem_release(region);
2234                                        nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2235                                                          stag_index);
2236                                        nes_debug(NES_DBG_MR, "Invalid Buffer Size\n");
2237                                        ibmr = ERR_PTR(-EINVAL);
2238                                        kfree(nesmr);
2239                                        goto reg_user_mr_err;
2240                                }
2241
2242                                region_length += sg_dma_len(sg);
2243                                chunk_pages = sg_dma_len(sg) >> 12;
2244                                region_length -= skip_pages << 12;
2245                                for (page_index = skip_pages; page_index < chunk_pages; page_index++) {
2246                                        skip_pages = 0;
2247                                        if ((page_count != 0) && (page_count << 12) - (ib_umem_offset(region) & (4096 - 1)) >= region->length)
2248                                                goto enough_pages;
2249                                        if ((page_count&0x01FF) == 0) {
2250                                                if (page_count >= 1024 * 512) {
2251                                                        ib_umem_release(region);
2252                                                        nes_free_resource(nesadapter,
2253                                                                          nesadapter->allocated_mrs, stag_index);
2254                                                        kfree(nesmr);
2255                                                        ibmr = ERR_PTR(-E2BIG);
2256                                                        goto reg_user_mr_err;
2257                                                }
2258                                                if (root_pbl_index == 1) {
2259                                                        root_vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev,
2260                                                                        8192, &root_vpbl.pbl_pbase);
2261                                                        nes_debug(NES_DBG_MR, "Allocating root PBL, va = %p, pa = 0x%08X\n",
2262                                                                  root_vpbl.pbl_vbase, (unsigned int)root_vpbl.pbl_pbase);
2263                                                        if (!root_vpbl.pbl_vbase) {
2264                                                                ib_umem_release(region);
2265                                                                pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2266                                                                                    vpbl.pbl_pbase);
2267                                                                nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2268                                                                                  stag_index);
2269                                                                kfree(nesmr);
2270                                                                ibmr = ERR_PTR(-ENOMEM);
2271                                                                goto reg_user_mr_err;
2272                                                        }
2273                                                        root_vpbl.leaf_vpbl = kzalloc(sizeof(*root_vpbl.leaf_vpbl)*1024,
2274                                                                        GFP_KERNEL);
2275                                                        if (!root_vpbl.leaf_vpbl) {
2276                                                                ib_umem_release(region);
2277                                                                pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2278                                                                                    root_vpbl.pbl_pbase);
2279                                                                pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2280                                                                                    vpbl.pbl_pbase);
2281                                                                nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2282                                                                                  stag_index);
2283                                                                kfree(nesmr);
2284                                                                ibmr = ERR_PTR(-ENOMEM);
2285                                                                goto reg_user_mr_err;
2286                                                        }
2287                                                        root_vpbl.pbl_vbase[0].pa_low =
2288                                                                        cpu_to_le32((u32)vpbl.pbl_pbase);
2289                                                        root_vpbl.pbl_vbase[0].pa_high =
2290                                                                        cpu_to_le32((u32)((((u64)vpbl.pbl_pbase) >> 32)));
2291                                                        root_vpbl.leaf_vpbl[0] = vpbl;
2292                                                }
2293                                                vpbl.pbl_vbase = pci_alloc_consistent(nesdev->pcidev, 4096,
2294                                                                &vpbl.pbl_pbase);
2295                                                nes_debug(NES_DBG_MR, "Allocating leaf PBL, va = %p, pa = 0x%08X\n",
2296                                                          vpbl.pbl_vbase, (unsigned int)vpbl.pbl_pbase);
2297                                                if (!vpbl.pbl_vbase) {
2298                                                        ib_umem_release(region);
2299                                                        nes_free_resource(nesadapter, nesadapter->allocated_mrs, stag_index);
2300                                                        ibmr = ERR_PTR(-ENOMEM);
2301                                                        kfree(nesmr);
2302                                                        goto reg_user_mr_err;
2303                                                }
2304                                                if (1 <= root_pbl_index) {
2305                                                        root_vpbl.pbl_vbase[root_pbl_index].pa_low =
2306                                                                        cpu_to_le32((u32)vpbl.pbl_pbase);
2307                                                        root_vpbl.pbl_vbase[root_pbl_index].pa_high =
2308                                                                        cpu_to_le32((u32)((((u64)vpbl.pbl_pbase)>>32)));
2309                                                        root_vpbl.leaf_vpbl[root_pbl_index] = vpbl;
2310                                                }
2311                                                root_pbl_index++;
2312                                                cur_pbl_index = 0;
2313                                        }
2314                                        if (single_page) {
2315                                                if (page_count != 0) {
2316                                                        if ((last_dma_addr+4096) !=
2317                                                                        (sg_dma_address(sg)+
2318                                                                        (page_index*4096)))
2319                                                                single_page = 0;
2320                                                        last_dma_addr = sg_dma_address(sg)+
2321                                                                        (page_index*4096);
2322                                                } else {
2323                                                        first_dma_addr = sg_dma_address(sg)+
2324                                                                        (page_index*4096);
2325                                                        last_dma_addr = first_dma_addr;
2326                                                }
2327                                        }
2328
2329                                        vpbl.pbl_vbase[cur_pbl_index].pa_low =
2330                                                        cpu_to_le32((u32)(sg_dma_address(sg)+
2331                                                        (page_index*4096)));
2332                                        vpbl.pbl_vbase[cur_pbl_index].pa_high =
2333                                                        cpu_to_le32((u32)((((u64)(sg_dma_address(sg)+
2334                                                        (page_index*4096))) >> 32)));
2335                                        cur_pbl_index++;
2336                                        page_count++;
2337                                }
2338                        }
2339
2340                        enough_pages:
2341                        nes_debug(NES_DBG_MR, "calculating stag, stag_index=0x%08x, driver_key=0x%08x,"
2342                                        " stag_key=0x%08x\n",
2343                                        stag_index, driver_key, stag_key);
2344                        stag = stag_index << 8;
2345                        stag |= driver_key;
2346                        stag += (u32)stag_key;
2347
2348                        iova_start = virt;
2349                        /* Make the leaf PBL the root if only one PBL */
2350                        if (root_pbl_index == 1) {
2351                                root_vpbl.pbl_pbase = vpbl.pbl_pbase;
2352                        }
2353
2354                        if (single_page) {
2355                                pbl_count = 0;
2356                        } else {
2357                                pbl_count = root_pbl_index;
2358                                first_dma_addr = 0;
2359                        }
2360                        nes_debug(NES_DBG_MR, "Registering STag 0x%08X, VA = 0x%08X, length = 0x%08X,"
2361                                        " index = 0x%08X, region->length=0x%08llx, pbl_count = %u\n",
2362                                        stag, (unsigned int)iova_start,
2363                                        (unsigned int)region_length, stag_index,
2364                                        (unsigned long long)region->length, pbl_count);
2365                        ret = nes_reg_mr(nesdev, nespd, stag, region->length, &root_vpbl,
2366                                         first_dma_addr, pbl_count, (u16)cur_pbl_index, acc,
2367                                         &iova_start, &nesmr->pbls_used, &nesmr->pbl_4k);
2368
2369                        nes_debug(NES_DBG_MR, "ret=%d\n", ret);
2370
2371                        if (ret == 0) {
2372                                nesmr->ibmr.rkey = stag;
2373                                nesmr->ibmr.lkey = stag;
2374                                nesmr->mode = IWNES_MEMREG_TYPE_MEM;
2375                                ibmr = &nesmr->ibmr;
2376                        } else {
2377                                ib_umem_release(region);
2378                                kfree(nesmr);
2379                                ibmr = ERR_PTR(-ENOMEM);
2380                        }
2381
2382                        reg_user_mr_err:
2383                        /* free the resources */
2384                        if (root_pbl_index == 1) {
2385                                pci_free_consistent(nesdev->pcidev, 4096, vpbl.pbl_vbase,
2386                                                vpbl.pbl_pbase);
2387                        } else {
2388                                for (page_index=0; page_index<root_pbl_index; page_index++) {
2389                                        pci_free_consistent(nesdev->pcidev, 4096,
2390                                                        root_vpbl.leaf_vpbl[page_index].pbl_vbase,
2391                                                        root_vpbl.leaf_vpbl[page_index].pbl_pbase);
2392                                }
2393                                kfree(root_vpbl.leaf_vpbl);
2394                                pci_free_consistent(nesdev->pcidev, 8192, root_vpbl.pbl_vbase,
2395                                                root_vpbl.pbl_pbase);
2396                        }
2397
2398                        nes_debug(NES_DBG_MR, "Leaving, ibmr=%p", ibmr);
2399
2400                        return ibmr;
2401                case IWNES_MEMREG_TYPE_QP:
2402                case IWNES_MEMREG_TYPE_CQ:
2403                        if (!region->length) {
2404                                nes_debug(NES_DBG_MR, "Unable to register zero length region for CQ\n");
2405                                ib_umem_release(region);
2406                                return ERR_PTR(-EINVAL);
2407                        }
2408                        nespbl = kzalloc(sizeof(*nespbl), GFP_KERNEL);
2409                        if (!nespbl) {
2410                                nes_debug(NES_DBG_MR, "Unable to allocate PBL\n");
2411                                ib_umem_release(region);
2412                                return ERR_PTR(-ENOMEM);
2413                        }
2414                        nesmr = kzalloc(sizeof(*nesmr), GFP_KERNEL);
2415                        if (!nesmr) {
2416                                ib_umem_release(region);
2417                                kfree(nespbl);
2418                                nes_debug(NES_DBG_MR, "Unable to allocate nesmr\n");
2419                                return ERR_PTR(-ENOMEM);
2420                        }
2421                        nesmr->region = region;
2422                        nes_ucontext = to_nesucontext(pd->uobject->context);
2423                        pbl_depth = region->length >> 12;
2424                        pbl_depth += (region->length & (4096-1)) ? 1 : 0;
2425                        nespbl->pbl_size = pbl_depth*sizeof(u64);
2426                        if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2427                                nes_debug(NES_DBG_MR, "Attempting to allocate QP PBL memory");
2428                        } else {
2429                                nes_debug(NES_DBG_MR, "Attempting to allocate CP PBL memory");
2430                        }
2431
2432                        nes_debug(NES_DBG_MR, " %u bytes, %u entries.\n",
2433                                        nespbl->pbl_size, pbl_depth);
2434                        pbl = pci_alloc_consistent(nesdev->pcidev, nespbl->pbl_size,
2435                                        &nespbl->pbl_pbase);
2436                        if (!pbl) {
2437                                ib_umem_release(region);
2438                                kfree(nesmr);
2439                                kfree(nespbl);
2440                                nes_debug(NES_DBG_MR, "Unable to allocate PBL memory\n");
2441                                return ERR_PTR(-ENOMEM);
2442                        }
2443
2444                        nespbl->pbl_vbase = (u64 *)pbl;
2445                        nespbl->user_base = start;
2446                        nes_debug(NES_DBG_MR, "Allocated PBL memory, %u bytes, pbl_pbase=%lx,"
2447                                        " pbl_vbase=%p user_base=0x%lx\n",
2448                                  nespbl->pbl_size, (unsigned long) nespbl->pbl_pbase,
2449                                  (void *) nespbl->pbl_vbase, nespbl->user_base);
2450
2451                        for_each_sg(region->sg_head.sgl, sg, region->nmap, entry) {
2452                                chunk_pages = sg_dma_len(sg) >> 12;
2453                                chunk_pages += (sg_dma_len(sg) & (4096-1)) ? 1 : 0;
2454                                if (first_page) {
2455                                        nespbl->page = sg_page(sg);
2456                                        first_page = 0;
2457                                }
2458
2459                                for (page_index = 0; page_index < chunk_pages; page_index++) {
2460                                        ((__le32 *)pbl)[0] = cpu_to_le32((u32)
2461                                                        (sg_dma_address(sg)+
2462                                                        (page_index*4096)));
2463                                        ((__le32 *)pbl)[1] = cpu_to_le32(((u64)
2464                                                        (sg_dma_address(sg)+
2465                                                        (page_index*4096)))>>32);
2466                                        nes_debug(NES_DBG_MR, "pbl=%p, *pbl=0x%016llx, 0x%08x%08x\n", pbl,
2467                                                  (unsigned long long)*pbl,
2468                                                  le32_to_cpu(((__le32 *)pbl)[1]), le32_to_cpu(((__le32 *)pbl)[0]));
2469                                        pbl++;
2470                                }
2471                        }
2472
2473                        if (req.reg_type == IWNES_MEMREG_TYPE_QP) {
2474                                list_add_tail(&nespbl->list, &nes_ucontext->qp_reg_mem_list);
2475                        } else {
2476                                list_add_tail(&nespbl->list, &nes_ucontext->cq_reg_mem_list);
2477                        }
2478                        nesmr->ibmr.rkey = -1;
2479                        nesmr->ibmr.lkey = -1;
2480                        nesmr->mode = req.reg_type;
2481                        return &nesmr->ibmr;
2482        }
2483
2484        ib_umem_release(region);
2485        return ERR_PTR(-ENOSYS);
2486}
2487
2488
2489/**
2490 * nes_dereg_mr
2491 */
2492static int nes_dereg_mr(struct ib_mr *ib_mr)
2493{
2494        struct nes_mr *nesmr = to_nesmr(ib_mr);
2495        struct nes_vnic *nesvnic = to_nesvnic(ib_mr->device);
2496        struct nes_device *nesdev = nesvnic->nesdev;
2497        struct nes_adapter *nesadapter = nesdev->nesadapter;
2498        struct nes_hw_cqp_wqe *cqp_wqe;
2499        struct nes_cqp_request *cqp_request;
2500        unsigned long flags;
2501        int ret;
2502        u16 major_code;
2503        u16 minor_code;
2504
2505
2506        if (nesmr->pages)
2507                pci_free_consistent(nesdev->pcidev,
2508                                    nesmr->max_pages * sizeof(u64),
2509                                    nesmr->pages,
2510                                    nesmr->paddr);
2511
2512        if (nesmr->region) {
2513                ib_umem_release(nesmr->region);
2514        }
2515        if (nesmr->mode != IWNES_MEMREG_TYPE_MEM) {
2516                kfree(nesmr);
2517                return 0;
2518        }
2519
2520        /* Deallocate the region with the adapter */
2521
2522        cqp_request = nes_get_cqp_request(nesdev);
2523        if (cqp_request == NULL) {
2524                nes_debug(NES_DBG_MR, "Failed to get a cqp_request.\n");
2525                return -ENOMEM;
2526        }
2527        cqp_request->waiting = 1;
2528        cqp_wqe = &cqp_request->cqp_wqe;
2529
2530        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2531        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2532                        NES_CQP_DEALLOCATE_STAG | NES_CQP_STAG_VA_TO |
2533                        NES_CQP_STAG_DEALLOC_PBLS | NES_CQP_STAG_MR);
2534        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_STAG_WQE_STAG_IDX, ib_mr->rkey);
2535
2536        atomic_set(&cqp_request->refcount, 2);
2537        nes_post_cqp_request(nesdev, cqp_request);
2538
2539        /* Wait for CQP */
2540        nes_debug(NES_DBG_MR, "Waiting for deallocate STag 0x%08X completed\n", ib_mr->rkey);
2541        ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2542                        NES_EVENT_TIMEOUT);
2543        nes_debug(NES_DBG_MR, "Deallocate STag 0x%08X completed, wait_event_timeout ret = %u,"
2544                        " CQP Major:Minor codes = 0x%04X:0x%04X\n",
2545                        ib_mr->rkey, ret, cqp_request->major_code, cqp_request->minor_code);
2546
2547        major_code = cqp_request->major_code;
2548        minor_code = cqp_request->minor_code;
2549
2550        nes_put_cqp_request(nesdev, cqp_request);
2551
2552        if (!ret) {
2553                nes_debug(NES_DBG_MR, "Timeout waiting to destroy STag,"
2554                                " ib_mr=%p, rkey = 0x%08X\n",
2555                                ib_mr, ib_mr->rkey);
2556                return -ETIME;
2557        } else if (major_code) {
2558                nes_debug(NES_DBG_MR, "Error (0x%04X:0x%04X) while attempting"
2559                                " to destroy STag, ib_mr=%p, rkey = 0x%08X\n",
2560                                major_code, minor_code, ib_mr, ib_mr->rkey);
2561                return -EIO;
2562        }
2563
2564        if (nesmr->pbls_used != 0) {
2565                spin_lock_irqsave(&nesadapter->pbl_lock, flags);
2566                if (nesmr->pbl_4k) {
2567                        nesadapter->free_4kpbl += nesmr->pbls_used;
2568                        if (nesadapter->free_4kpbl > nesadapter->max_4kpbl)
2569                                printk(KERN_ERR PFX "free 4KB PBLs(%u) has "
2570                                        "exceeded the max(%u)\n",
2571                                        nesadapter->free_4kpbl,
2572                                        nesadapter->max_4kpbl);
2573                } else {
2574                        nesadapter->free_256pbl += nesmr->pbls_used;
2575                        if (nesadapter->free_256pbl > nesadapter->max_256pbl)
2576                                printk(KERN_ERR PFX "free 256B PBLs(%u) has "
2577                                        "exceeded the max(%u)\n",
2578                                        nesadapter->free_256pbl,
2579                                        nesadapter->max_256pbl);
2580                }
2581                spin_unlock_irqrestore(&nesadapter->pbl_lock, flags);
2582        }
2583        nes_free_resource(nesadapter, nesadapter->allocated_mrs,
2584                        (ib_mr->rkey & 0x0fffff00) >> 8);
2585
2586        kfree(nesmr);
2587
2588        return 0;
2589}
2590
2591
2592/**
2593 * show_rev
2594 */
2595static ssize_t show_rev(struct device *dev, struct device_attribute *attr,
2596                        char *buf)
2597{
2598        struct nes_ib_device *nesibdev =
2599                        container_of(dev, struct nes_ib_device, ibdev.dev);
2600        struct nes_vnic *nesvnic = nesibdev->nesvnic;
2601
2602        nes_debug(NES_DBG_INIT, "\n");
2603        return sprintf(buf, "%x\n", nesvnic->nesdev->nesadapter->hw_rev);
2604}
2605
2606
2607/**
2608 * show_fw_ver
2609 */
2610static ssize_t show_fw_ver(struct device *dev, struct device_attribute *attr,
2611                           char *buf)
2612{
2613        struct nes_ib_device *nesibdev =
2614                        container_of(dev, struct nes_ib_device, ibdev.dev);
2615        struct nes_vnic *nesvnic = nesibdev->nesvnic;
2616
2617        nes_debug(NES_DBG_INIT, "\n");
2618        return sprintf(buf, "%u.%u\n",
2619                (nesvnic->nesdev->nesadapter->firmware_version >> 16),
2620                (nesvnic->nesdev->nesadapter->firmware_version & 0x000000ff));
2621}
2622
2623
2624/**
2625 * show_hca
2626 */
2627static ssize_t show_hca(struct device *dev, struct device_attribute *attr,
2628                        char *buf)
2629{
2630        nes_debug(NES_DBG_INIT, "\n");
2631        return sprintf(buf, "NES020\n");
2632}
2633
2634
2635/**
2636 * show_board
2637 */
2638static ssize_t show_board(struct device *dev, struct device_attribute *attr,
2639                          char *buf)
2640{
2641        nes_debug(NES_DBG_INIT, "\n");
2642        return sprintf(buf, "%.*s\n", 32, "NES020 Board ID");
2643}
2644
2645
2646static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
2647static DEVICE_ATTR(fw_ver, S_IRUGO, show_fw_ver, NULL);
2648static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
2649static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
2650
2651static struct device_attribute *nes_dev_attributes[] = {
2652        &dev_attr_hw_rev,
2653        &dev_attr_fw_ver,
2654        &dev_attr_hca_type,
2655        &dev_attr_board_id
2656};
2657
2658
2659/**
2660 * nes_query_qp
2661 */
2662static int nes_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2663                int attr_mask, struct ib_qp_init_attr *init_attr)
2664{
2665        struct nes_qp *nesqp = to_nesqp(ibqp);
2666
2667        nes_debug(NES_DBG_QP, "\n");
2668
2669        attr->qp_access_flags = 0;
2670        attr->cap.max_send_wr = nesqp->hwqp.sq_size;
2671        attr->cap.max_recv_wr = nesqp->hwqp.rq_size;
2672        attr->cap.max_recv_sge = 1;
2673        if (nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA)
2674                attr->cap.max_inline_data = 0;
2675        else
2676                attr->cap.max_inline_data = 64;
2677
2678        init_attr->event_handler = nesqp->ibqp.event_handler;
2679        init_attr->qp_context = nesqp->ibqp.qp_context;
2680        init_attr->send_cq = nesqp->ibqp.send_cq;
2681        init_attr->recv_cq = nesqp->ibqp.recv_cq;
2682        init_attr->srq = nesqp->ibqp.srq;
2683        init_attr->cap = attr->cap;
2684
2685        return 0;
2686}
2687
2688
2689/**
2690 * nes_hw_modify_qp
2691 */
2692int nes_hw_modify_qp(struct nes_device *nesdev, struct nes_qp *nesqp,
2693                u32 next_iwarp_state, u32 termlen, u32 wait_completion)
2694{
2695        struct nes_hw_cqp_wqe *cqp_wqe;
2696        /* struct iw_cm_id *cm_id = nesqp->cm_id; */
2697        /* struct iw_cm_event cm_event; */
2698        struct nes_cqp_request *cqp_request;
2699        int ret;
2700        u16 major_code;
2701
2702        nes_debug(NES_DBG_MOD_QP, "QP%u, refcount=%d\n",
2703                        nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2704
2705        cqp_request = nes_get_cqp_request(nesdev);
2706        if (cqp_request == NULL) {
2707                nes_debug(NES_DBG_MOD_QP, "Failed to get a cqp_request.\n");
2708                return -ENOMEM;
2709        }
2710        if (wait_completion) {
2711                cqp_request->waiting = 1;
2712        } else {
2713                cqp_request->waiting = 0;
2714        }
2715        cqp_wqe = &cqp_request->cqp_wqe;
2716
2717        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_OPCODE_IDX,
2718                        NES_CQP_MODIFY_QP | NES_CQP_QP_TYPE_IWARP | next_iwarp_state);
2719        nes_debug(NES_DBG_MOD_QP, "using next_iwarp_state=%08x, wqe_words=%08x\n",
2720                        next_iwarp_state, le32_to_cpu(cqp_wqe->wqe_words[NES_CQP_WQE_OPCODE_IDX]));
2721        nes_fill_init_cqp_wqe(cqp_wqe, nesdev);
2722        set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_WQE_ID_IDX, nesqp->hwqp.qp_id);
2723        set_wqe_64bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_CONTEXT_LOW_IDX, (u64)nesqp->nesqp_context_pbase);
2724
2725        /* If sending a terminate message, fill in the length (in words) */
2726        if (((next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK) == NES_CQP_QP_IWARP_STATE_TERMINATE) &&
2727            !(next_iwarp_state & NES_CQP_QP_TERM_DONT_SEND_TERM_MSG)) {
2728                termlen = ((termlen + 3) >> 2) << NES_CQP_OP_TERMLEN_SHIFT;
2729                set_wqe_32bit_value(cqp_wqe->wqe_words, NES_CQP_QP_WQE_NEW_MSS_IDX, termlen);
2730        }
2731
2732        atomic_set(&cqp_request->refcount, 2);
2733        nes_post_cqp_request(nesdev, cqp_request);
2734
2735        /* Wait for CQP */
2736        if (wait_completion) {
2737                /* nes_debug(NES_DBG_MOD_QP, "Waiting for modify iWARP QP%u to complete.\n",
2738                                nesqp->hwqp.qp_id); */
2739                ret = wait_event_timeout(cqp_request->waitq, (cqp_request->request_done != 0),
2740                                NES_EVENT_TIMEOUT);
2741                nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u completed, wait_event_timeout ret=%u, "
2742                                "CQP Major:Minor codes = 0x%04X:0x%04X.\n",
2743                                nesqp->hwqp.qp_id, ret, cqp_request->major_code, cqp_request->minor_code);
2744                major_code = cqp_request->major_code;
2745                if (major_code) {
2746                        nes_debug(NES_DBG_MOD_QP, "Modify iwarp QP%u failed"
2747                                        "CQP Major:Minor codes = 0x%04X:0x%04X, intended next state = 0x%08X.\n",
2748                                        nesqp->hwqp.qp_id, cqp_request->major_code,
2749                                        cqp_request->minor_code, next_iwarp_state);
2750                }
2751
2752                nes_put_cqp_request(nesdev, cqp_request);
2753
2754                if (!ret)
2755                        return -ETIME;
2756                else if (major_code)
2757                        return -EIO;
2758                else
2759                        return 0;
2760        } else {
2761                return 0;
2762        }
2763}
2764
2765
2766/**
2767 * nes_modify_qp
2768 */
2769int nes_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2770                int attr_mask, struct ib_udata *udata)
2771{
2772        struct nes_qp *nesqp = to_nesqp(ibqp);
2773        struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
2774        struct nes_device *nesdev = nesvnic->nesdev;
2775        /* u32 cqp_head; */
2776        /* u32 counter; */
2777        u32 next_iwarp_state = 0;
2778        int err;
2779        unsigned long qplockflags;
2780        int ret;
2781        u16 original_last_aeq;
2782        u8 issue_modify_qp = 0;
2783        u8 dont_wait = 0;
2784
2785        nes_debug(NES_DBG_MOD_QP, "QP%u: QP State=%u, cur QP State=%u,"
2786                        " iwarp_state=0x%X, refcount=%d\n",
2787                        nesqp->hwqp.qp_id, attr->qp_state, nesqp->ibqp_state,
2788                        nesqp->iwarp_state, atomic_read(&nesqp->refcount));
2789
2790        spin_lock_irqsave(&nesqp->lock, qplockflags);
2791
2792        nes_debug(NES_DBG_MOD_QP, "QP%u: hw_iwarp_state=0x%X, hw_tcp_state=0x%X,"
2793                        " QP Access Flags=0x%X, attr_mask = 0x%0x\n",
2794                        nesqp->hwqp.qp_id, nesqp->hw_iwarp_state,
2795                        nesqp->hw_tcp_state, attr->qp_access_flags, attr_mask);
2796
2797        if (attr_mask & IB_QP_STATE) {
2798                switch (attr->qp_state) {
2799                        case IB_QPS_INIT:
2800                                nes_debug(NES_DBG_MOD_QP, "QP%u: new state = init\n",
2801                                                nesqp->hwqp.qp_id);
2802                                if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2803                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2804                                        return -EINVAL;
2805                                }
2806                                next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2807                                issue_modify_qp = 1;
2808                                break;
2809                        case IB_QPS_RTR:
2810                                nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rtr\n",
2811                                                nesqp->hwqp.qp_id);
2812                                if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_IDLE) {
2813                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2814                                        return -EINVAL;
2815                                }
2816                                next_iwarp_state = NES_CQP_QP_IWARP_STATE_IDLE;
2817                                issue_modify_qp = 1;
2818                                break;
2819                        case IB_QPS_RTS:
2820                                nes_debug(NES_DBG_MOD_QP, "QP%u: new state = rts\n",
2821                                                nesqp->hwqp.qp_id);
2822                                if (nesqp->iwarp_state>(u32)NES_CQP_QP_IWARP_STATE_RTS) {
2823                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2824                                        return -EINVAL;
2825                                }
2826                                if (nesqp->cm_id == NULL) {
2827                                        nes_debug(NES_DBG_MOD_QP, "QP%u: Failing attempt to move QP to RTS without a CM_ID. \n",
2828                                                        nesqp->hwqp.qp_id );
2829                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2830                                        return -EINVAL;
2831                                }
2832                                next_iwarp_state = NES_CQP_QP_IWARP_STATE_RTS;
2833                                if (nesqp->iwarp_state != NES_CQP_QP_IWARP_STATE_RTS)
2834                                        next_iwarp_state |= NES_CQP_QP_CONTEXT_VALID |
2835                                                        NES_CQP_QP_ARP_VALID | NES_CQP_QP_ORD_VALID;
2836                                issue_modify_qp = 1;
2837                                nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_ESTABLISHED;
2838                                nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_RTS;
2839                                nesqp->hte_added = 1;
2840                                break;
2841                        case IB_QPS_SQD:
2842                                issue_modify_qp = 1;
2843                                nes_debug(NES_DBG_MOD_QP, "QP%u: new state=closing. SQ head=%u, SQ tail=%u\n",
2844                                                nesqp->hwqp.qp_id, nesqp->hwqp.sq_head, nesqp->hwqp.sq_tail);
2845                                if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2846                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2847                                        return 0;
2848                                } else {
2849                                        if (nesqp->iwarp_state > (u32)NES_CQP_QP_IWARP_STATE_CLOSING) {
2850                                                nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2851                                                                " ignored due to current iWARP state\n",
2852                                                                nesqp->hwqp.qp_id);
2853                                                spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2854                                                return -EINVAL;
2855                                        }
2856                                        if (nesqp->hw_iwarp_state != NES_AEQE_IWARP_STATE_RTS) {
2857                                                nes_debug(NES_DBG_MOD_QP, "QP%u: State change to closing"
2858                                                                " already done based on hw state.\n",
2859                                                                nesqp->hwqp.qp_id);
2860                                                issue_modify_qp = 0;
2861                                        }
2862                                        switch (nesqp->hw_iwarp_state) {
2863                                                case NES_AEQE_IWARP_STATE_CLOSING:
2864                                                        next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2865                                                        break;
2866                                                case NES_AEQE_IWARP_STATE_TERMINATE:
2867                                                        next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2868                                                        break;
2869                                                case NES_AEQE_IWARP_STATE_ERROR:
2870                                                        next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2871                                                        break;
2872                                                default:
2873                                                        next_iwarp_state = NES_CQP_QP_IWARP_STATE_CLOSING;
2874                                                        nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_CLOSING;
2875                                                        break;
2876                                        }
2877                                }
2878                                break;
2879                        case IB_QPS_SQE:
2880                                nes_debug(NES_DBG_MOD_QP, "QP%u: new state = terminate\n",
2881                                                nesqp->hwqp.qp_id);
2882                                if (nesqp->iwarp_state>=(u32)NES_CQP_QP_IWARP_STATE_TERMINATE) {
2883                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2884                                        return -EINVAL;
2885                                }
2886                                /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2887                                next_iwarp_state = NES_CQP_QP_IWARP_STATE_TERMINATE;
2888                                nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_TERMINATE;
2889                                issue_modify_qp = 1;
2890                                break;
2891                        case IB_QPS_ERR:
2892                        case IB_QPS_RESET:
2893                                if (nesqp->iwarp_state == (u32)NES_CQP_QP_IWARP_STATE_ERROR) {
2894                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2895                                        return -EINVAL;
2896                                }
2897                                nes_debug(NES_DBG_MOD_QP, "QP%u: new state = error\n",
2898                                                nesqp->hwqp.qp_id);
2899                                if (nesqp->term_flags)
2900                                        del_timer(&nesqp->terminate_timer);
2901
2902                                next_iwarp_state = NES_CQP_QP_IWARP_STATE_ERROR;
2903                                /* next_iwarp_state = (NES_CQP_QP_IWARP_STATE_TERMINATE | 0x02000000); */
2904                                        if (nesqp->hte_added) {
2905                                                nes_debug(NES_DBG_MOD_QP, "set CQP_QP_DEL_HTE\n");
2906                                                next_iwarp_state |= NES_CQP_QP_DEL_HTE;
2907                                                nesqp->hte_added = 0;
2908                                        }
2909                                if ((nesqp->hw_tcp_state > NES_AEQE_TCP_STATE_CLOSED) &&
2910                                                (nesdev->iw_status) &&
2911                                                (nesqp->hw_tcp_state != NES_AEQE_TCP_STATE_TIME_WAIT)) {
2912                                        next_iwarp_state |= NES_CQP_QP_RESET;
2913                                } else {
2914                                        nes_debug(NES_DBG_MOD_QP, "QP%u NOT setting NES_CQP_QP_RESET since TCP state = %u\n",
2915                                                        nesqp->hwqp.qp_id, nesqp->hw_tcp_state);
2916                                        dont_wait = 1;
2917                                }
2918                                issue_modify_qp = 1;
2919                                nesqp->hw_iwarp_state = NES_AEQE_IWARP_STATE_ERROR;
2920                                break;
2921                        default:
2922                                spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2923                                return -EINVAL;
2924                                break;
2925                }
2926
2927                nesqp->ibqp_state = attr->qp_state;
2928                nesqp->iwarp_state = next_iwarp_state & NES_CQP_QP_IWARP_STATE_MASK;
2929                nes_debug(NES_DBG_MOD_QP, "Change nesqp->iwarp_state=%08x\n",
2930                                nesqp->iwarp_state);
2931        }
2932
2933        if (attr_mask & IB_QP_ACCESS_FLAGS) {
2934                if (attr->qp_access_flags & IB_ACCESS_LOCAL_WRITE) {
2935                        nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2936                                        NES_QPCONTEXT_MISC_RDMA_READ_EN);
2937                        issue_modify_qp = 1;
2938                }
2939                if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE) {
2940                        nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN);
2941                        issue_modify_qp = 1;
2942                }
2943                if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ) {
2944                        nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_READ_EN);
2945                        issue_modify_qp = 1;
2946                }
2947                if (attr->qp_access_flags & IB_ACCESS_MW_BIND) {
2948                        nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_WBIND_EN);
2949                        issue_modify_qp = 1;
2950                }
2951
2952                if (nesqp->user_mode) {
2953                        nesqp->nesqp_context->misc |= cpu_to_le32(NES_QPCONTEXT_MISC_RDMA_WRITE_EN |
2954                                        NES_QPCONTEXT_MISC_RDMA_READ_EN);
2955                        issue_modify_qp = 1;
2956                }
2957        }
2958
2959        original_last_aeq = nesqp->last_aeq;
2960        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2961
2962        nes_debug(NES_DBG_MOD_QP, "issue_modify_qp=%u\n", issue_modify_qp);
2963
2964        ret = 0;
2965
2966
2967        if (issue_modify_qp) {
2968                nes_debug(NES_DBG_MOD_QP, "call nes_hw_modify_qp\n");
2969                ret = nes_hw_modify_qp(nesdev, nesqp, next_iwarp_state, 0, 1);
2970                if (ret)
2971                        nes_debug(NES_DBG_MOD_QP, "nes_hw_modify_qp (next_iwarp_state = 0x%08X)"
2972                                        " failed for QP%u.\n",
2973                                        next_iwarp_state, nesqp->hwqp.qp_id);
2974
2975        }
2976
2977        if ((issue_modify_qp) && (nesqp->ibqp_state > IB_QPS_RTS)) {
2978                nes_debug(NES_DBG_MOD_QP, "QP%u Issued ModifyQP refcount (%d),"
2979                                " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2980                                nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2981                                original_last_aeq, nesqp->last_aeq);
2982                if (!ret || original_last_aeq != NES_AEQE_AEID_RDMAP_ROE_BAD_LLP_CLOSE) {
2983                        if (dont_wait) {
2984                                if (nesqp->cm_id && nesqp->hw_tcp_state != 0) {
2985                                        nes_debug(NES_DBG_MOD_QP, "QP%u Queuing fake disconnect for QP refcount (%d),"
2986                                                        " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
2987                                                        nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
2988                                                        original_last_aeq, nesqp->last_aeq);
2989                                        /* this one is for the cm_disconnect thread */
2990                                        spin_lock_irqsave(&nesqp->lock, qplockflags);
2991                                        nesqp->hw_tcp_state = NES_AEQE_TCP_STATE_CLOSED;
2992                                        nesqp->last_aeq = NES_AEQE_AEID_RESET_SENT;
2993                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
2994                                        nes_cm_disconn(nesqp);
2995                                } else {
2996                                        nes_debug(NES_DBG_MOD_QP, "QP%u No fake disconnect, QP refcount=%d\n",
2997                                                        nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
2998                                }
2999                        } else {
3000                                spin_lock_irqsave(&nesqp->lock, qplockflags);
3001                                if (nesqp->cm_id) {
3002                                        /* These two are for the timer thread */
3003                                        if (atomic_inc_return(&nesqp->close_timer_started) == 1) {
3004                                                nesqp->cm_id->add_ref(nesqp->cm_id);
3005                                                nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3006                                                                " need ae to finish up, original_last_aeq = 0x%04X."
3007                                                                " last_aeq = 0x%04X, scheduling timer.\n",
3008                                                                nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3009                                                                original_last_aeq, nesqp->last_aeq);
3010                                                schedule_nes_timer(nesqp->cm_node, (struct sk_buff *) nesqp, NES_TIMER_TYPE_CLOSE, 1, 0);
3011                                        }
3012                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3013                                } else {
3014                                        spin_unlock_irqrestore(&nesqp->lock, qplockflags);
3015                                        nes_debug(NES_DBG_MOD_QP, "QP%u Not decrementing QP refcount (%d),"
3016                                                        " need ae to finish up, original_last_aeq = 0x%04X."
3017                                                        " last_aeq = 0x%04X.\n",
3018                                                        nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3019                                                        original_last_aeq, nesqp->last_aeq);
3020                                }
3021                        }
3022                } else {
3023                        nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3024                                        " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3025                                        nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3026                                        original_last_aeq, nesqp->last_aeq);
3027                }
3028        } else {
3029                nes_debug(NES_DBG_MOD_QP, "QP%u Decrementing QP refcount (%d), No ae to finish up,"
3030                                " original_last_aeq = 0x%04X. last_aeq = 0x%04X.\n",
3031                                nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount),
3032                                original_last_aeq, nesqp->last_aeq);
3033        }
3034
3035        err = 0;
3036
3037        nes_debug(NES_DBG_MOD_QP, "QP%u Leaving, refcount=%d\n",
3038                        nesqp->hwqp.qp_id, atomic_read(&nesqp->refcount));
3039
3040        return err;
3041}
3042
3043
3044/**
3045 * nes_muticast_attach
3046 */
3047static int nes_multicast_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3048{
3049        nes_debug(NES_DBG_INIT, "\n");
3050        return -ENOSYS;
3051}
3052
3053
3054/**
3055 * nes_multicast_detach
3056 */
3057static int nes_multicast_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
3058{
3059        nes_debug(NES_DBG_INIT, "\n");
3060        return -ENOSYS;
3061}
3062
3063
3064/**
3065 * nes_process_mad
3066 */
3067static int nes_process_mad(struct ib_device *ibdev, int mad_flags,
3068                u8 port_num, const struct ib_wc *in_wc, const struct ib_grh *in_grh,
3069                const struct ib_mad_hdr *in, size_t in_mad_size,
3070                struct ib_mad_hdr *out, size_t *out_mad_size,
3071                u16 *out_mad_pkey_index)
3072{
3073        nes_debug(NES_DBG_INIT, "\n");
3074        return -ENOSYS;
3075}
3076
3077static inline void
3078fill_wqe_sg_send(struct nes_hw_qp_wqe *wqe, struct ib_send_wr *ib_wr, u32 uselkey)
3079{
3080        int sge_index;
3081        int total_payload_length = 0;
3082        for (sge_index = 0; sge_index < ib_wr->num_sge; sge_index++) {
3083                set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3084                        ib_wr->sg_list[sge_index].addr);
3085                set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_LENGTH0_IDX + (sge_index*4),
3086                        ib_wr->sg_list[sge_index].length);
3087                if (uselkey)
3088                        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4),
3089                                                (ib_wr->sg_list[sge_index].lkey));
3090                else
3091                        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX + (sge_index*4), 0);
3092
3093                total_payload_length += ib_wr->sg_list[sge_index].length;
3094        }
3095        nes_debug(NES_DBG_IW_TX, "UC UC UC, sending total_payload_length=%u \n",
3096                        total_payload_length);
3097        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3098                                total_payload_length);
3099}
3100
3101/**
3102 * nes_post_send
3103 */
3104static int nes_post_send(struct ib_qp *ibqp, struct ib_send_wr *ib_wr,
3105                struct ib_send_wr **bad_wr)
3106{
3107        u64 u64temp;
3108        unsigned long flags = 0;
3109        struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3110        struct nes_device *nesdev = nesvnic->nesdev;
3111        struct nes_qp *nesqp = to_nesqp(ibqp);
3112        struct nes_hw_qp_wqe *wqe;
3113        int err = 0;
3114        u32 qsize = nesqp->hwqp.sq_size;
3115        u32 head;
3116        u32 wqe_misc = 0;
3117        u32 wqe_count = 0;
3118        u32 counter;
3119
3120        if (nesqp->ibqp_state > IB_QPS_RTS) {
3121                err = -EINVAL;
3122                goto out;
3123        }
3124
3125        spin_lock_irqsave(&nesqp->lock, flags);
3126
3127        head = nesqp->hwqp.sq_head;
3128
3129        while (ib_wr) {
3130                /* Check for QP error */
3131                if (nesqp->term_flags) {
3132                        err = -EINVAL;
3133                        break;
3134                }
3135
3136                /* Check for SQ overflow */
3137                if (((head + (2 * qsize) - nesqp->hwqp.sq_tail) % qsize) == (qsize - 1)) {
3138                        err = -ENOMEM;
3139                        break;
3140                }
3141
3142                wqe = &nesqp->hwqp.sq_vbase[head];
3143                /* nes_debug(NES_DBG_IW_TX, "processing sq wqe for QP%u at %p, head = %u.\n",
3144                                nesqp->hwqp.qp_id, wqe, head); */
3145                nes_fill_init_qp_wqe(wqe, nesqp, head);
3146                u64temp = (u64)(ib_wr->wr_id);
3147                set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3148                                        u64temp);
3149                switch (ib_wr->opcode) {
3150                case IB_WR_SEND:
3151                case IB_WR_SEND_WITH_INV:
3152                        if (IB_WR_SEND == ib_wr->opcode) {
3153                                if (ib_wr->send_flags & IB_SEND_SOLICITED)
3154                                        wqe_misc = NES_IWARP_SQ_OP_SENDSE;
3155                                else
3156                                        wqe_misc = NES_IWARP_SQ_OP_SEND;
3157                        } else {
3158                                if (ib_wr->send_flags & IB_SEND_SOLICITED)
3159                                        wqe_misc = NES_IWARP_SQ_OP_SENDSEINV;
3160                                else
3161                                        wqe_misc = NES_IWARP_SQ_OP_SENDINV;
3162
3163                                set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3164                                                    ib_wr->ex.invalidate_rkey);
3165                        }
3166
3167                        if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3168                                err = -EINVAL;
3169                                break;
3170                        }
3171
3172                        if (ib_wr->send_flags & IB_SEND_FENCE)
3173                                wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3174
3175                        if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3176                            ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3177                             (ib_wr->sg_list[0].length <= 64)) {
3178                                memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3179                                       (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3180                                set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3181                                                    ib_wr->sg_list[0].length);
3182                                wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3183                        } else {
3184                                fill_wqe_sg_send(wqe, ib_wr, 1);
3185                        }
3186
3187                        break;
3188                case IB_WR_RDMA_WRITE:
3189                        wqe_misc = NES_IWARP_SQ_OP_RDMAW;
3190                        if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3191                                nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=%u\n",
3192                                          ib_wr->num_sge, nesdev->nesadapter->max_sge);
3193                                err = -EINVAL;
3194                                break;
3195                        }
3196
3197                        if (ib_wr->send_flags & IB_SEND_FENCE)
3198                                wqe_misc |= NES_IWARP_SQ_WQE_LOCAL_FENCE;
3199
3200                        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3201                                            rdma_wr(ib_wr)->rkey);
3202                        set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3203                                            rdma_wr(ib_wr)->remote_addr);
3204
3205                        if ((ib_wr->send_flags & IB_SEND_INLINE) &&
3206                            ((nes_drv_opt & NES_DRV_OPT_NO_INLINE_DATA) == 0) &&
3207                             (ib_wr->sg_list[0].length <= 64)) {
3208                                memcpy(&wqe->wqe_words[NES_IWARP_SQ_WQE_IMM_DATA_START_IDX],
3209                                       (void *)(unsigned long)ib_wr->sg_list[0].addr, ib_wr->sg_list[0].length);
3210                                set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX,
3211                                                    ib_wr->sg_list[0].length);
3212                                wqe_misc |= NES_IWARP_SQ_WQE_IMM_DATA;
3213                        } else {
3214                                fill_wqe_sg_send(wqe, ib_wr, 1);
3215                        }
3216
3217                        wqe->wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX] =
3218                                wqe->wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX];
3219                        break;
3220                case IB_WR_RDMA_READ:
3221                case IB_WR_RDMA_READ_WITH_INV:
3222                        /* iWARP only supports 1 sge for RDMA reads */
3223                        if (ib_wr->num_sge > 1) {
3224                                nes_debug(NES_DBG_IW_TX, "Exceeded max sge, ib_wr=%u, max=1\n",
3225                                          ib_wr->num_sge);
3226                                err = -EINVAL;
3227                                break;
3228                        }
3229                        if (ib_wr->opcode == IB_WR_RDMA_READ) {
3230                                wqe_misc = NES_IWARP_SQ_OP_RDMAR;
3231                        } else {
3232                                wqe_misc = NES_IWARP_SQ_OP_RDMAR_LOCINV;
3233                                set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_INV_STAG_LOW_IDX,
3234                                                    ib_wr->ex.invalidate_rkey);
3235                        }
3236
3237                        set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_TO_LOW_IDX,
3238                                            rdma_wr(ib_wr)->remote_addr);
3239                        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_STAG_IDX,
3240                                            rdma_wr(ib_wr)->rkey);
3241                        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX,
3242                                            ib_wr->sg_list->length);
3243                        set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_FRAG0_LOW_IDX,
3244                                            ib_wr->sg_list->addr);
3245                        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_STAG0_IDX,
3246                                            ib_wr->sg_list->lkey);
3247                        break;
3248                case IB_WR_LOCAL_INV:
3249                        wqe_misc = NES_IWARP_SQ_OP_LOCINV;
3250                        set_wqe_32bit_value(wqe->wqe_words,
3251                                            NES_IWARP_SQ_LOCINV_WQE_INV_STAG_IDX,
3252                                            ib_wr->ex.invalidate_rkey);
3253                        break;
3254                case IB_WR_REG_MR:
3255                {
3256                        struct nes_mr *mr = to_nesmr(reg_wr(ib_wr)->mr);
3257                        int page_shift = ilog2(reg_wr(ib_wr)->mr->page_size);
3258                        int flags = reg_wr(ib_wr)->access;
3259
3260                        if (mr->npages > (NES_4K_PBL_CHUNK_SIZE / sizeof(u64))) {
3261                                nes_debug(NES_DBG_IW_TX, "SQ_FMR: bad page_list_len\n");
3262                                err = -EINVAL;
3263                                break;
3264                        }
3265                        wqe_misc = NES_IWARP_SQ_OP_FAST_REG;
3266                        set_wqe_64bit_value(wqe->wqe_words,
3267                                            NES_IWARP_SQ_FMR_WQE_VA_FBO_LOW_IDX,
3268                                            mr->ibmr.iova);
3269                        set_wqe_32bit_value(wqe->wqe_words,
3270                                            NES_IWARP_SQ_FMR_WQE_LENGTH_LOW_IDX,
3271                                            mr->ibmr.length);
3272                        set_wqe_32bit_value(wqe->wqe_words,
3273                                            NES_IWARP_SQ_FMR_WQE_LENGTH_HIGH_IDX, 0);
3274                        set_wqe_32bit_value(wqe->wqe_words,
3275                                            NES_IWARP_SQ_FMR_WQE_MR_STAG_IDX,
3276                                            reg_wr(ib_wr)->key);
3277
3278                        if (page_shift == 12) {
3279                                wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_4K;
3280                        } else if (page_shift == 21) {
3281                                wqe_misc |= NES_IWARP_SQ_FMR_WQE_PAGE_SIZE_2M;
3282                        } else {
3283                                nes_debug(NES_DBG_IW_TX, "Invalid page shift,"
3284                                          " ib_wr=%u, max=1\n", ib_wr->num_sge);
3285                                err = -EINVAL;
3286                                break;
3287                        }
3288
3289                        /* Set access_flags */
3290                        wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_READ;
3291                        if (flags & IB_ACCESS_LOCAL_WRITE)
3292                                wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_LOCAL_WRITE;
3293
3294                        if (flags & IB_ACCESS_REMOTE_WRITE)
3295                                wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_WRITE;
3296
3297                        if (flags & IB_ACCESS_REMOTE_READ)
3298                                wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_REMOTE_READ;
3299
3300                        if (flags & IB_ACCESS_MW_BIND)
3301                                wqe_misc |= NES_IWARP_SQ_FMR_WQE_RIGHTS_ENABLE_WINDOW_BIND;
3302
3303                        /* Fill in PBL info: */
3304                        set_wqe_64bit_value(wqe->wqe_words,
3305                                            NES_IWARP_SQ_FMR_WQE_PBL_ADDR_LOW_IDX,
3306                                            mr->paddr);
3307
3308                        set_wqe_32bit_value(wqe->wqe_words,
3309                                            NES_IWARP_SQ_FMR_WQE_PBL_LENGTH_IDX,
3310                                            mr->npages * 8);
3311
3312                        nes_debug(NES_DBG_IW_TX, "SQ_REG_MR: iova_start: %llx, "
3313                                  "length: %d, rkey: %0x, pgl_paddr: %llx, "
3314                                  "page_list_len: %u, wqe_misc: %x\n",
3315                                  (unsigned long long) mr->ibmr.iova,
3316                                  mr->ibmr.length,
3317                                  reg_wr(ib_wr)->key,
3318                                  (unsigned long long) mr->paddr,
3319                                  mr->npages,
3320                                  wqe_misc);
3321                        break;
3322                }
3323                default:
3324                        /* error */
3325                        err = -EINVAL;
3326                        break;
3327                }
3328
3329                if (err)
3330                        break;
3331
3332                if ((ib_wr->send_flags & IB_SEND_SIGNALED) || nesqp->sig_all)
3333                        wqe_misc |= NES_IWARP_SQ_WQE_SIGNALED_COMPL;
3334
3335                wqe->wqe_words[NES_IWARP_SQ_WQE_MISC_IDX] = cpu_to_le32(wqe_misc);
3336
3337                ib_wr = ib_wr->next;
3338                head++;
3339                wqe_count++;
3340                if (head >= qsize)
3341                        head = 0;
3342
3343        }
3344
3345        nesqp->hwqp.sq_head = head;
3346        barrier();
3347        while (wqe_count) {
3348                counter = min(wqe_count, ((u32)255));
3349                wqe_count -= counter;
3350                nes_write32(nesdev->regs + NES_WQE_ALLOC,
3351                                (counter << 24) | 0x00800000 | nesqp->hwqp.qp_id);
3352        }
3353
3354        spin_unlock_irqrestore(&nesqp->lock, flags);
3355
3356out:
3357        if (err)
3358                *bad_wr = ib_wr;
3359        return err;
3360}
3361
3362
3363/**
3364 * nes_post_recv
3365 */
3366static int nes_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *ib_wr,
3367                struct ib_recv_wr **bad_wr)
3368{
3369        u64 u64temp;
3370        unsigned long flags = 0;
3371        struct nes_vnic *nesvnic = to_nesvnic(ibqp->device);
3372        struct nes_device *nesdev = nesvnic->nesdev;
3373        struct nes_qp *nesqp = to_nesqp(ibqp);
3374        struct nes_hw_qp_wqe *wqe;
3375        int err = 0;
3376        int sge_index;
3377        u32 qsize = nesqp->hwqp.rq_size;
3378        u32 head;
3379        u32 wqe_count = 0;
3380        u32 counter;
3381        u32 total_payload_length;
3382
3383        if (nesqp->ibqp_state > IB_QPS_RTS) {
3384                err = -EINVAL;
3385                goto out;
3386        }
3387
3388        spin_lock_irqsave(&nesqp->lock, flags);
3389
3390        head = nesqp->hwqp.rq_head;
3391
3392        while (ib_wr) {
3393                /* Check for QP error */
3394                if (nesqp->term_flags) {
3395                        err = -EINVAL;
3396                        break;
3397                }
3398
3399                if (ib_wr->num_sge > nesdev->nesadapter->max_sge) {
3400                        err = -EINVAL;
3401                        break;
3402                }
3403                /* Check for RQ overflow */
3404                if (((head + (2 * qsize) - nesqp->hwqp.rq_tail) % qsize) == (qsize - 1)) {
3405                        err = -ENOMEM;
3406                        break;
3407                }
3408
3409                nes_debug(NES_DBG_IW_RX, "ibwr sge count = %u.\n", ib_wr->num_sge);
3410                wqe = &nesqp->hwqp.rq_vbase[head];
3411
3412                /* nes_debug(NES_DBG_IW_RX, "QP%u:processing rq wqe at %p, head = %u.\n",
3413                                nesqp->hwqp.qp_id, wqe, head); */
3414                nes_fill_init_qp_wqe(wqe, nesqp, head);
3415                u64temp = (u64)(ib_wr->wr_id);
3416                set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX,
3417                                        u64temp);
3418                total_payload_length = 0;
3419                for (sge_index=0; sge_index < ib_wr->num_sge; sge_index++) {
3420                        set_wqe_64bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_FRAG0_LOW_IDX+(sge_index*4),
3421                                        ib_wr->sg_list[sge_index].addr);
3422                        set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_LENGTH0_IDX+(sge_index*4),
3423                                        ib_wr->sg_list[sge_index].length);
3424                        set_wqe_32bit_value(wqe->wqe_words,NES_IWARP_RQ_WQE_STAG0_IDX+(sge_index*4),
3425                                        ib_wr->sg_list[sge_index].lkey);
3426
3427                        total_payload_length += ib_wr->sg_list[sge_index].length;
3428                }
3429                set_wqe_32bit_value(wqe->wqe_words, NES_IWARP_RQ_WQE_TOTAL_PAYLOAD_IDX,
3430                                        total_payload_length);
3431
3432                ib_wr = ib_wr->next;
3433                head++;
3434                wqe_count++;
3435                if (head >= qsize)
3436                        head = 0;
3437        }
3438
3439        nesqp->hwqp.rq_head = head;
3440        barrier();
3441        while (wqe_count) {
3442                counter = min(wqe_count, ((u32)255));
3443                wqe_count -= counter;
3444                nes_write32(nesdev->regs+NES_WQE_ALLOC, (counter<<24) | nesqp->hwqp.qp_id);
3445        }
3446
3447        spin_unlock_irqrestore(&nesqp->lock, flags);
3448
3449out:
3450        if (err)
3451                *bad_wr = ib_wr;
3452        return err;
3453}
3454
3455
3456/**
3457 * nes_poll_cq
3458 */
3459static int nes_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *entry)
3460{
3461        u64 u64temp;
3462        u64 wrid;
3463        unsigned long flags = 0;
3464        struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3465        struct nes_device *nesdev = nesvnic->nesdev;
3466        struct nes_cq *nescq = to_nescq(ibcq);
3467        struct nes_qp *nesqp;
3468        struct nes_hw_cqe cqe;
3469        u32 head;
3470        u32 wq_tail = 0;
3471        u32 cq_size;
3472        u32 cqe_count = 0;
3473        u32 wqe_index;
3474        u32 u32temp;
3475        u32 move_cq_head = 1;
3476        u32 err_code;
3477
3478        nes_debug(NES_DBG_CQ, "\n");
3479
3480        spin_lock_irqsave(&nescq->lock, flags);
3481
3482        head = nescq->hw_cq.cq_head;
3483        cq_size = nescq->hw_cq.cq_size;
3484
3485        while (cqe_count < num_entries) {
3486                if ((le32_to_cpu(nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX]) &
3487                                NES_CQE_VALID) == 0)
3488                        break;
3489
3490                /*
3491                 * Make sure we read CQ entry contents *after*
3492                 * we've checked the valid bit.
3493                 */
3494                rmb();
3495
3496                cqe = nescq->hw_cq.cq_vbase[head];
3497                u32temp = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3498                wqe_index = u32temp & (nesdev->nesadapter->max_qp_wr - 1);
3499                u32temp &= ~(NES_SW_CONTEXT_ALIGN-1);
3500                /* parse CQE, get completion context from WQE (either rq or sq) */
3501                u64temp = (((u64)(le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_HIGH_IDX])))<<32) |
3502                                ((u64)u32temp);
3503
3504                if (u64temp) {
3505                        nesqp = (struct nes_qp *)(unsigned long)u64temp;
3506                        memset(entry, 0, sizeof *entry);
3507                        if (cqe.cqe_words[NES_CQE_ERROR_CODE_IDX] == 0) {
3508                                entry->status = IB_WC_SUCCESS;
3509                        } else {
3510                                err_code = le32_to_cpu(cqe.cqe_words[NES_CQE_ERROR_CODE_IDX]);
3511                                if (NES_IWARP_CQE_MAJOR_DRV == (err_code >> 16)) {
3512                                        entry->status = err_code & 0x0000ffff;
3513
3514                                        /* The rest of the cqe's will be marked as flushed */
3515                                        nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_ERROR_CODE_IDX] =
3516                                                cpu_to_le32((NES_IWARP_CQE_MAJOR_FLUSH << 16) |
3517                                                            NES_IWARP_CQE_MINOR_FLUSH);
3518                                } else
3519                                        entry->status = IB_WC_WR_FLUSH_ERR;
3520                        }
3521
3522                        entry->qp = &nesqp->ibqp;
3523                        entry->src_qp = nesqp->hwqp.qp_id;
3524
3525                        if (le32_to_cpu(cqe.cqe_words[NES_CQE_OPCODE_IDX]) & NES_CQE_SQ) {
3526                                if (nesqp->skip_lsmm) {
3527                                        nesqp->skip_lsmm = 0;
3528                                        nesqp->hwqp.sq_tail++;
3529                                }
3530
3531                                /* Working on a SQ Completion*/
3532                                wrid = (((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3533                                                wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_HIGH_IDX]))) << 32) |
3534                                                ((u64)(cpu_to_le32((u32)nesqp->hwqp.sq_vbase[wqe_index].
3535                                                wqe_words[NES_IWARP_SQ_WQE_COMP_SCRATCH_LOW_IDX])));
3536                                entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3537                                                wqe_words[NES_IWARP_SQ_WQE_TOTAL_PAYLOAD_IDX]);
3538
3539                                switch (le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3540                                                wqe_words[NES_IWARP_SQ_WQE_MISC_IDX]) & 0x3f) {
3541                                        case NES_IWARP_SQ_OP_RDMAW:
3542                                                nes_debug(NES_DBG_CQ, "Operation = RDMA WRITE.\n");
3543                                                entry->opcode = IB_WC_RDMA_WRITE;
3544                                                break;
3545                                        case NES_IWARP_SQ_OP_RDMAR:
3546                                                nes_debug(NES_DBG_CQ, "Operation = RDMA READ.\n");
3547                                                entry->opcode = IB_WC_RDMA_READ;
3548                                                entry->byte_len = le32_to_cpu(nesqp->hwqp.sq_vbase[wqe_index].
3549                                                                wqe_words[NES_IWARP_SQ_WQE_RDMA_LENGTH_IDX]);
3550                                                break;
3551                                        case NES_IWARP_SQ_OP_SENDINV:
3552                                        case NES_IWARP_SQ_OP_SENDSEINV:
3553                                        case NES_IWARP_SQ_OP_SEND:
3554                                        case NES_IWARP_SQ_OP_SENDSE:
3555                                                nes_debug(NES_DBG_CQ, "Operation = Send.\n");
3556                                                entry->opcode = IB_WC_SEND;
3557                                                break;
3558                                        case NES_IWARP_SQ_OP_LOCINV:
3559                                                entry->opcode = IB_WC_LOCAL_INV;
3560                                                break;
3561                                        case NES_IWARP_SQ_OP_FAST_REG:
3562                                                entry->opcode = IB_WC_REG_MR;
3563                                                break;
3564                                }
3565
3566                                nesqp->hwqp.sq_tail = (wqe_index+1)&(nesqp->hwqp.sq_size - 1);
3567                                if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.sq_tail != nesqp->hwqp.sq_head)) {
3568                                        move_cq_head = 0;
3569                                        wq_tail = nesqp->hwqp.sq_tail;
3570                                }
3571                        } else {
3572                                /* Working on a RQ Completion*/
3573                                entry->byte_len = le32_to_cpu(cqe.cqe_words[NES_CQE_PAYLOAD_LENGTH_IDX]);
3574                                wrid = ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_LOW_IDX]))) |
3575                                        ((u64)(le32_to_cpu(nesqp->hwqp.rq_vbase[wqe_index].wqe_words[NES_IWARP_RQ_WQE_COMP_SCRATCH_HIGH_IDX]))<<32);
3576                                        entry->opcode = IB_WC_RECV;
3577
3578                                nesqp->hwqp.rq_tail = (wqe_index+1)&(nesqp->hwqp.rq_size - 1);
3579                                if ((entry->status != IB_WC_SUCCESS) && (nesqp->hwqp.rq_tail != nesqp->hwqp.rq_head)) {
3580                                        move_cq_head = 0;
3581                                        wq_tail = nesqp->hwqp.rq_tail;
3582                                }
3583                        }
3584
3585                        entry->wr_id = wrid;
3586                        entry++;
3587                        cqe_count++;
3588                }
3589
3590                if (move_cq_head) {
3591                        nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_OPCODE_IDX] = 0;
3592                        if (++head >= cq_size)
3593                                head = 0;
3594                        nescq->polled_completions++;
3595
3596                        if ((nescq->polled_completions > (cq_size / 2)) ||
3597                                        (nescq->polled_completions == 255)) {
3598                                nes_debug(NES_DBG_CQ, "CQ%u Issuing CQE Allocate since more than half of cqes"
3599                                        " are pending %u of %u.\n",
3600                                        nescq->hw_cq.cq_number, nescq->polled_completions, cq_size);
3601                                nes_write32(nesdev->regs+NES_CQE_ALLOC,
3602                                        nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3603                                nescq->polled_completions = 0;
3604                        }
3605                } else {
3606                        /* Update the wqe index and set status to flush */
3607                        wqe_index = le32_to_cpu(cqe.cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX]);
3608                        wqe_index = (wqe_index & (~(nesdev->nesadapter->max_qp_wr - 1))) | wq_tail;
3609                        nescq->hw_cq.cq_vbase[head].cqe_words[NES_CQE_COMP_COMP_CTX_LOW_IDX] =
3610                                cpu_to_le32(wqe_index);
3611                        move_cq_head = 1; /* ready for next pass */
3612                }
3613        }
3614
3615        if (nescq->polled_completions) {
3616                nes_write32(nesdev->regs+NES_CQE_ALLOC,
3617                                nescq->hw_cq.cq_number | (nescq->polled_completions << 16));
3618                nescq->polled_completions = 0;
3619        }
3620
3621        nescq->hw_cq.cq_head = head;
3622        nes_debug(NES_DBG_CQ, "Reporting %u completions for CQ%u.\n",
3623                        cqe_count, nescq->hw_cq.cq_number);
3624
3625        spin_unlock_irqrestore(&nescq->lock, flags);
3626
3627        return cqe_count;
3628}
3629
3630
3631/**
3632 * nes_req_notify_cq
3633 */
3634static int nes_req_notify_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags notify_flags)
3635                {
3636        struct nes_vnic *nesvnic = to_nesvnic(ibcq->device);
3637        struct nes_device *nesdev = nesvnic->nesdev;
3638        struct nes_cq *nescq = to_nescq(ibcq);
3639        u32 cq_arm;
3640
3641        nes_debug(NES_DBG_CQ, "Requesting notification for CQ%u.\n",
3642                        nescq->hw_cq.cq_number);
3643
3644        cq_arm = nescq->hw_cq.cq_number;
3645        if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_NEXT_COMP)
3646                cq_arm |= NES_CQE_ALLOC_NOTIFY_NEXT;
3647        else if ((notify_flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED)
3648                cq_arm |= NES_CQE_ALLOC_NOTIFY_SE;
3649        else
3650                return -EINVAL;
3651
3652        nes_write32(nesdev->regs+NES_CQE_ALLOC, cq_arm);
3653        nes_read32(nesdev->regs+NES_CQE_ALLOC);
3654
3655        return 0;
3656}
3657
3658static int nes_port_immutable(struct ib_device *ibdev, u8 port_num,
3659                              struct ib_port_immutable *immutable)
3660{
3661        struct ib_port_attr attr;
3662        int err;
3663
3664        err = nes_query_port(ibdev, port_num, &attr);
3665        if (err)
3666                return err;
3667
3668        immutable->pkey_tbl_len = attr.pkey_tbl_len;
3669        immutable->gid_tbl_len = attr.gid_tbl_len;
3670        immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
3671
3672        return 0;
3673}
3674
3675/**
3676 * nes_init_ofa_device
3677 */
3678struct nes_ib_device *nes_init_ofa_device(struct net_device *netdev)
3679{
3680        struct nes_ib_device *nesibdev;
3681        struct nes_vnic *nesvnic = netdev_priv(netdev);
3682        struct nes_device *nesdev = nesvnic->nesdev;
3683
3684        nesibdev = (struct nes_ib_device *)ib_alloc_device(sizeof(struct nes_ib_device));
3685        if (nesibdev == NULL) {
3686                return NULL;
3687        }
3688        strlcpy(nesibdev->ibdev.name, "nes%d", IB_DEVICE_NAME_MAX);
3689        nesibdev->ibdev.owner = THIS_MODULE;
3690
3691        nesibdev->ibdev.node_type = RDMA_NODE_RNIC;
3692        memset(&nesibdev->ibdev.node_guid, 0, sizeof(nesibdev->ibdev.node_guid));
3693        memcpy(&nesibdev->ibdev.node_guid, netdev->dev_addr, 6);
3694
3695        nesibdev->ibdev.uverbs_cmd_mask =
3696                        (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
3697                        (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
3698                        (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
3699                        (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
3700                        (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
3701                        (1ull << IB_USER_VERBS_CMD_REG_MR) |
3702                        (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
3703                        (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
3704                        (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
3705                        (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
3706                        (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
3707                        (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
3708                        (1ull << IB_USER_VERBS_CMD_REQ_NOTIFY_CQ) |
3709                        (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
3710                        (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
3711                        (1ull << IB_USER_VERBS_CMD_POLL_CQ) |
3712                        (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
3713                        (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
3714                        (1ull << IB_USER_VERBS_CMD_BIND_MW) |
3715                        (1ull << IB_USER_VERBS_CMD_DEALLOC_MW) |
3716                        (1ull << IB_USER_VERBS_CMD_POST_RECV) |
3717                        (1ull << IB_USER_VERBS_CMD_POST_SEND);
3718
3719        nesibdev->ibdev.phys_port_cnt = 1;
3720        nesibdev->ibdev.num_comp_vectors = 1;
3721        nesibdev->ibdev.dma_device = &nesdev->pcidev->dev;
3722        nesibdev->ibdev.dev.parent = &nesdev->pcidev->dev;
3723        nesibdev->ibdev.query_device = nes_query_device;
3724        nesibdev->ibdev.query_port = nes_query_port;
3725        nesibdev->ibdev.query_pkey = nes_query_pkey;
3726        nesibdev->ibdev.query_gid = nes_query_gid;
3727        nesibdev->ibdev.alloc_ucontext = nes_alloc_ucontext;
3728        nesibdev->ibdev.dealloc_ucontext = nes_dealloc_ucontext;
3729        nesibdev->ibdev.mmap = nes_mmap;
3730        nesibdev->ibdev.alloc_pd = nes_alloc_pd;
3731        nesibdev->ibdev.dealloc_pd = nes_dealloc_pd;
3732        nesibdev->ibdev.create_ah = nes_create_ah;
3733        nesibdev->ibdev.destroy_ah = nes_destroy_ah;
3734        nesibdev->ibdev.create_qp = nes_create_qp;
3735        nesibdev->ibdev.modify_qp = nes_modify_qp;
3736        nesibdev->ibdev.query_qp = nes_query_qp;
3737        nesibdev->ibdev.destroy_qp = nes_destroy_qp;
3738        nesibdev->ibdev.create_cq = nes_create_cq;
3739        nesibdev->ibdev.destroy_cq = nes_destroy_cq;
3740        nesibdev->ibdev.poll_cq = nes_poll_cq;
3741        nesibdev->ibdev.get_dma_mr = nes_get_dma_mr;
3742        nesibdev->ibdev.reg_user_mr = nes_reg_user_mr;
3743        nesibdev->ibdev.dereg_mr = nes_dereg_mr;
3744        nesibdev->ibdev.alloc_mw = nes_alloc_mw;
3745        nesibdev->ibdev.dealloc_mw = nes_dealloc_mw;
3746
3747        nesibdev->ibdev.alloc_mr = nes_alloc_mr;
3748        nesibdev->ibdev.map_mr_sg = nes_map_mr_sg;
3749
3750        nesibdev->ibdev.attach_mcast = nes_multicast_attach;
3751        nesibdev->ibdev.detach_mcast = nes_multicast_detach;
3752        nesibdev->ibdev.process_mad = nes_process_mad;
3753
3754        nesibdev->ibdev.req_notify_cq = nes_req_notify_cq;
3755        nesibdev->ibdev.post_send = nes_post_send;
3756        nesibdev->ibdev.post_recv = nes_post_recv;
3757
3758        nesibdev->ibdev.iwcm = kzalloc(sizeof(*nesibdev->ibdev.iwcm), GFP_KERNEL);
3759        if (nesibdev->ibdev.iwcm == NULL) {
3760                ib_dealloc_device(&nesibdev->ibdev);
3761                return NULL;
3762        }
3763        nesibdev->ibdev.iwcm->add_ref = nes_add_ref;
3764        nesibdev->ibdev.iwcm->rem_ref = nes_rem_ref;
3765        nesibdev->ibdev.iwcm->get_qp = nes_get_qp;
3766        nesibdev->ibdev.iwcm->connect = nes_connect;
3767        nesibdev->ibdev.iwcm->accept = nes_accept;
3768        nesibdev->ibdev.iwcm->reject = nes_reject;
3769        nesibdev->ibdev.iwcm->create_listen = nes_create_listen;
3770        nesibdev->ibdev.iwcm->destroy_listen = nes_destroy_listen;
3771        nesibdev->ibdev.get_port_immutable   = nes_port_immutable;
3772        memcpy(nesibdev->ibdev.iwcm->ifname, netdev->name,
3773               sizeof(nesibdev->ibdev.iwcm->ifname));
3774
3775        return nesibdev;
3776}
3777
3778
3779/**
3780 * nes_handle_delayed_event
3781 */
3782static void nes_handle_delayed_event(unsigned long data)
3783{
3784        struct nes_vnic *nesvnic = (void *) data;
3785
3786        if (nesvnic->delayed_event != nesvnic->last_dispatched_event) {
3787                struct ib_event event;
3788
3789                event.device = &nesvnic->nesibdev->ibdev;
3790                if (!event.device)
3791                        goto stop_timer;
3792                event.event = nesvnic->delayed_event;
3793                event.element.port_num = nesvnic->logical_port + 1;
3794                ib_dispatch_event(&event);
3795        }
3796
3797stop_timer:
3798        nesvnic->event_timer.function = NULL;
3799}
3800
3801
3802void  nes_port_ibevent(struct nes_vnic *nesvnic)
3803{
3804        struct nes_ib_device *nesibdev = nesvnic->nesibdev;
3805        struct nes_device *nesdev = nesvnic->nesdev;
3806        struct ib_event event;
3807        event.device = &nesibdev->ibdev;
3808        event.element.port_num = nesvnic->logical_port + 1;
3809        event.event = nesdev->iw_status ? IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
3810
3811        if (!nesvnic->event_timer.function) {
3812                ib_dispatch_event(&event);
3813                nesvnic->last_dispatched_event = event.event;
3814                nesvnic->event_timer.function = nes_handle_delayed_event;
3815                nesvnic->event_timer.data = (unsigned long) nesvnic;
3816                nesvnic->event_timer.expires = jiffies + NES_EVENT_DELAY;
3817                add_timer(&nesvnic->event_timer);
3818        } else {
3819                mod_timer(&nesvnic->event_timer, jiffies + NES_EVENT_DELAY);
3820        }
3821        nesvnic->delayed_event = event.event;
3822}
3823
3824
3825/**
3826 * nes_destroy_ofa_device
3827 */
3828void nes_destroy_ofa_device(struct nes_ib_device *nesibdev)
3829{
3830        if (nesibdev == NULL)
3831                return;
3832
3833        nes_unregister_ofa_device(nesibdev);
3834
3835        kfree(nesibdev->ibdev.iwcm);
3836        ib_dealloc_device(&nesibdev->ibdev);
3837}
3838
3839
3840/**
3841 * nes_register_ofa_device
3842 */
3843int nes_register_ofa_device(struct nes_ib_device *nesibdev)
3844{
3845        struct nes_vnic *nesvnic = nesibdev->nesvnic;
3846        struct nes_device *nesdev = nesvnic->nesdev;
3847        struct nes_adapter *nesadapter = nesdev->nesadapter;
3848        int i, ret;
3849
3850        ret = ib_register_device(&nesvnic->nesibdev->ibdev, NULL);
3851        if (ret) {
3852                return ret;
3853        }
3854
3855        /* Get the resources allocated to this device */
3856        nesibdev->max_cq = (nesadapter->max_cq-NES_FIRST_QPN) / nesadapter->port_count;
3857        nesibdev->max_mr = nesadapter->max_mr / nesadapter->port_count;
3858        nesibdev->max_qp = (nesadapter->max_qp-NES_FIRST_QPN) / nesadapter->port_count;
3859        nesibdev->max_pd = nesadapter->max_pd / nesadapter->port_count;
3860
3861        for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3862                ret = device_create_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3863                if (ret) {
3864                        while (i > 0) {
3865                                i--;
3866                                device_remove_file(&nesibdev->ibdev.dev,
3867                                                   nes_dev_attributes[i]);
3868                        }
3869                        ib_unregister_device(&nesibdev->ibdev);
3870                        return ret;
3871                }
3872        }
3873
3874        nesvnic->of_device_registered = 1;
3875
3876        return 0;
3877}
3878
3879
3880/**
3881 * nes_unregister_ofa_device
3882 */
3883static void nes_unregister_ofa_device(struct nes_ib_device *nesibdev)
3884{
3885        struct nes_vnic *nesvnic = nesibdev->nesvnic;
3886        int i;
3887
3888        for (i = 0; i < ARRAY_SIZE(nes_dev_attributes); ++i) {
3889                device_remove_file(&nesibdev->ibdev.dev, nes_dev_attributes[i]);
3890        }
3891
3892        if (nesvnic->of_device_registered) {
3893                ib_unregister_device(&nesibdev->ibdev);
3894        }
3895
3896        nesvnic->of_device_registered = 0;
3897}
3898