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