Path: blob/master/drivers/infiniband/hw/ehca/ehca_qp.c
15112 views
/*1* IBM eServer eHCA Infiniband device driver for Linux on POWER2*3* QP functions4*5* Authors: Joachim Fenkes <[email protected]>6* Stefan Roscher <[email protected]>7* Waleri Fomin <[email protected]>8* Hoang-Nam Nguyen <[email protected]>9* Reinhard Ernst <[email protected]>10* Heiko J Schick <[email protected]>11*12* Copyright (c) 2005 IBM Corporation13*14* All rights reserved.15*16* This source code is distributed under a dual license of GPL v2.0 and OpenIB17* BSD.18*19* OpenIB BSD License20*21* Redistribution and use in source and binary forms, with or without22* modification, are permitted provided that the following conditions are met:23*24* Redistributions of source code must retain the above copyright notice, this25* list of conditions and the following disclaimer.26*27* Redistributions in binary form must reproduce the above copyright notice,28* this list of conditions and the following disclaimer in the documentation29* and/or other materials30* provided with the distribution.31*32* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"33* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE34* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE35* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE36* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR37* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF38* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR39* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER40* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)41* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE42* POSSIBILITY OF SUCH DAMAGE.43*/4445#include <linux/slab.h>4647#include "ehca_classes.h"48#include "ehca_tools.h"49#include "ehca_qes.h"50#include "ehca_iverbs.h"51#include "hcp_if.h"52#include "hipz_fns.h"5354static struct kmem_cache *qp_cache;5556/*57* attributes not supported by query qp58*/59#define QP_ATTR_QUERY_NOT_SUPPORTED (IB_QP_ACCESS_FLAGS | \60IB_QP_EN_SQD_ASYNC_NOTIFY)6162/*63* ehca (internal) qp state values64*/65enum ehca_qp_state {66EHCA_QPS_RESET = 1,67EHCA_QPS_INIT = 2,68EHCA_QPS_RTR = 3,69EHCA_QPS_RTS = 5,70EHCA_QPS_SQD = 6,71EHCA_QPS_SQE = 8,72EHCA_QPS_ERR = 12873};7475/*76* qp state transitions as defined by IB Arch Rel 1.1 page 43177*/78enum ib_qp_statetrans {79IB_QPST_ANY2RESET,80IB_QPST_ANY2ERR,81IB_QPST_RESET2INIT,82IB_QPST_INIT2RTR,83IB_QPST_INIT2INIT,84IB_QPST_RTR2RTS,85IB_QPST_RTS2SQD,86IB_QPST_RTS2RTS,87IB_QPST_SQD2RTS,88IB_QPST_SQE2RTS,89IB_QPST_SQD2SQD,90IB_QPST_MAX /* nr of transitions, this must be last!!! */91};9293/*94* ib2ehca_qp_state maps IB to ehca qp_state95* returns ehca qp state corresponding to given ib qp state96*/97static inline enum ehca_qp_state ib2ehca_qp_state(enum ib_qp_state ib_qp_state)98{99switch (ib_qp_state) {100case IB_QPS_RESET:101return EHCA_QPS_RESET;102case IB_QPS_INIT:103return EHCA_QPS_INIT;104case IB_QPS_RTR:105return EHCA_QPS_RTR;106case IB_QPS_RTS:107return EHCA_QPS_RTS;108case IB_QPS_SQD:109return EHCA_QPS_SQD;110case IB_QPS_SQE:111return EHCA_QPS_SQE;112case IB_QPS_ERR:113return EHCA_QPS_ERR;114default:115ehca_gen_err("invalid ib_qp_state=%x", ib_qp_state);116return -EINVAL;117}118}119120/*121* ehca2ib_qp_state maps ehca to IB qp_state122* returns ib qp state corresponding to given ehca qp state123*/124static inline enum ib_qp_state ehca2ib_qp_state(enum ehca_qp_state125ehca_qp_state)126{127switch (ehca_qp_state) {128case EHCA_QPS_RESET:129return IB_QPS_RESET;130case EHCA_QPS_INIT:131return IB_QPS_INIT;132case EHCA_QPS_RTR:133return IB_QPS_RTR;134case EHCA_QPS_RTS:135return IB_QPS_RTS;136case EHCA_QPS_SQD:137return IB_QPS_SQD;138case EHCA_QPS_SQE:139return IB_QPS_SQE;140case EHCA_QPS_ERR:141return IB_QPS_ERR;142default:143ehca_gen_err("invalid ehca_qp_state=%x", ehca_qp_state);144return -EINVAL;145}146}147148/*149* ehca_qp_type used as index for req_attr and opt_attr of150* struct ehca_modqp_statetrans151*/152enum ehca_qp_type {153QPT_RC = 0,154QPT_UC = 1,155QPT_UD = 2,156QPT_SQP = 3,157QPT_MAX158};159160/*161* ib2ehcaqptype maps Ib to ehca qp_type162* returns ehca qp type corresponding to ib qp type163*/164static inline enum ehca_qp_type ib2ehcaqptype(enum ib_qp_type ibqptype)165{166switch (ibqptype) {167case IB_QPT_SMI:168case IB_QPT_GSI:169return QPT_SQP;170case IB_QPT_RC:171return QPT_RC;172case IB_QPT_UC:173return QPT_UC;174case IB_QPT_UD:175return QPT_UD;176default:177ehca_gen_err("Invalid ibqptype=%x", ibqptype);178return -EINVAL;179}180}181182static inline enum ib_qp_statetrans get_modqp_statetrans(int ib_fromstate,183int ib_tostate)184{185int index = -EINVAL;186switch (ib_tostate) {187case IB_QPS_RESET:188index = IB_QPST_ANY2RESET;189break;190case IB_QPS_INIT:191switch (ib_fromstate) {192case IB_QPS_RESET:193index = IB_QPST_RESET2INIT;194break;195case IB_QPS_INIT:196index = IB_QPST_INIT2INIT;197break;198}199break;200case IB_QPS_RTR:201if (ib_fromstate == IB_QPS_INIT)202index = IB_QPST_INIT2RTR;203break;204case IB_QPS_RTS:205switch (ib_fromstate) {206case IB_QPS_RTR:207index = IB_QPST_RTR2RTS;208break;209case IB_QPS_RTS:210index = IB_QPST_RTS2RTS;211break;212case IB_QPS_SQD:213index = IB_QPST_SQD2RTS;214break;215case IB_QPS_SQE:216index = IB_QPST_SQE2RTS;217break;218}219break;220case IB_QPS_SQD:221if (ib_fromstate == IB_QPS_RTS)222index = IB_QPST_RTS2SQD;223break;224case IB_QPS_SQE:225break;226case IB_QPS_ERR:227index = IB_QPST_ANY2ERR;228break;229default:230break;231}232return index;233}234235/*236* ibqptype2servicetype returns hcp service type corresponding to given237* ib qp type used by create_qp()238*/239static inline int ibqptype2servicetype(enum ib_qp_type ibqptype)240{241switch (ibqptype) {242case IB_QPT_SMI:243case IB_QPT_GSI:244return ST_UD;245case IB_QPT_RC:246return ST_RC;247case IB_QPT_UC:248return ST_UC;249case IB_QPT_UD:250return ST_UD;251case IB_QPT_RAW_IPV6:252return -EINVAL;253case IB_QPT_RAW_ETHERTYPE:254return -EINVAL;255default:256ehca_gen_err("Invalid ibqptype=%x", ibqptype);257return -EINVAL;258}259}260261/*262* init userspace queue info from ipz_queue data263*/264static inline void queue2resp(struct ipzu_queue_resp *resp,265struct ipz_queue *queue)266{267resp->qe_size = queue->qe_size;268resp->act_nr_of_sg = queue->act_nr_of_sg;269resp->queue_length = queue->queue_length;270resp->pagesize = queue->pagesize;271resp->toggle_state = queue->toggle_state;272resp->offset = queue->offset;273}274275/*276* init_qp_queue initializes/constructs r/squeue and registers queue pages.277*/278static inline int init_qp_queue(struct ehca_shca *shca,279struct ehca_pd *pd,280struct ehca_qp *my_qp,281struct ipz_queue *queue,282int q_type,283u64 expected_hret,284struct ehca_alloc_queue_parms *parms,285int wqe_size)286{287int ret, cnt, ipz_rc, nr_q_pages;288void *vpage;289u64 rpage, h_ret;290struct ib_device *ib_dev = &shca->ib_device;291struct ipz_adapter_handle ipz_hca_handle = shca->ipz_hca_handle;292293if (!parms->queue_size)294return 0;295296if (parms->is_small) {297nr_q_pages = 1;298ipz_rc = ipz_queue_ctor(pd, queue, nr_q_pages,299128 << parms->page_size,300wqe_size, parms->act_nr_sges, 1);301} else {302nr_q_pages = parms->queue_size;303ipz_rc = ipz_queue_ctor(pd, queue, nr_q_pages,304EHCA_PAGESIZE, wqe_size,305parms->act_nr_sges, 0);306}307308if (!ipz_rc) {309ehca_err(ib_dev, "Cannot allocate page for queue. ipz_rc=%i",310ipz_rc);311return -EBUSY;312}313314/* register queue pages */315for (cnt = 0; cnt < nr_q_pages; cnt++) {316vpage = ipz_qpageit_get_inc(queue);317if (!vpage) {318ehca_err(ib_dev, "ipz_qpageit_get_inc() "319"failed p_vpage= %p", vpage);320ret = -EINVAL;321goto init_qp_queue1;322}323rpage = virt_to_abs(vpage);324325h_ret = hipz_h_register_rpage_qp(ipz_hca_handle,326my_qp->ipz_qp_handle,327NULL, 0, q_type,328rpage, parms->is_small ? 0 : 1,329my_qp->galpas.kernel);330if (cnt == (nr_q_pages - 1)) { /* last page! */331if (h_ret != expected_hret) {332ehca_err(ib_dev, "hipz_qp_register_rpage() "333"h_ret=%lli", h_ret);334ret = ehca2ib_return_code(h_ret);335goto init_qp_queue1;336}337vpage = ipz_qpageit_get_inc(&my_qp->ipz_rqueue);338if (vpage) {339ehca_err(ib_dev, "ipz_qpageit_get_inc() "340"should not succeed vpage=%p", vpage);341ret = -EINVAL;342goto init_qp_queue1;343}344} else {345if (h_ret != H_PAGE_REGISTERED) {346ehca_err(ib_dev, "hipz_qp_register_rpage() "347"h_ret=%lli", h_ret);348ret = ehca2ib_return_code(h_ret);349goto init_qp_queue1;350}351}352}353354ipz_qeit_reset(queue);355356return 0;357358init_qp_queue1:359ipz_queue_dtor(pd, queue);360return ret;361}362363static inline int ehca_calc_wqe_size(int act_nr_sge, int is_llqp)364{365if (is_llqp)366return 128 << act_nr_sge;367else368return offsetof(struct ehca_wqe,369u.nud.sg_list[act_nr_sge]);370}371372static void ehca_determine_small_queue(struct ehca_alloc_queue_parms *queue,373int req_nr_sge, int is_llqp)374{375u32 wqe_size, q_size;376int act_nr_sge = req_nr_sge;377378if (!is_llqp)379/* round up #SGEs so WQE size is a power of 2 */380for (act_nr_sge = 4; act_nr_sge <= 252;381act_nr_sge = 4 + 2 * act_nr_sge)382if (act_nr_sge >= req_nr_sge)383break;384385wqe_size = ehca_calc_wqe_size(act_nr_sge, is_llqp);386q_size = wqe_size * (queue->max_wr + 1);387388if (q_size <= 512)389queue->page_size = 2;390else if (q_size <= 1024)391queue->page_size = 3;392else393queue->page_size = 0;394395queue->is_small = (queue->page_size != 0);396}397398/* needs to be called with cq->spinlock held */399void ehca_add_to_err_list(struct ehca_qp *qp, int on_sq)400{401struct list_head *list, *node;402403/* TODO: support low latency QPs */404if (qp->ext_type == EQPT_LLQP)405return;406407if (on_sq) {408list = &qp->send_cq->sqp_err_list;409node = &qp->sq_err_node;410} else {411list = &qp->recv_cq->rqp_err_list;412node = &qp->rq_err_node;413}414415if (list_empty(node))416list_add_tail(node, list);417418return;419}420421static void del_from_err_list(struct ehca_cq *cq, struct list_head *node)422{423unsigned long flags;424425spin_lock_irqsave(&cq->spinlock, flags);426427if (!list_empty(node))428list_del_init(node);429430spin_unlock_irqrestore(&cq->spinlock, flags);431}432433static void reset_queue_map(struct ehca_queue_map *qmap)434{435int i;436437qmap->tail = qmap->entries - 1;438qmap->left_to_poll = 0;439qmap->next_wqe_idx = 0;440for (i = 0; i < qmap->entries; i++) {441qmap->map[i].reported = 1;442qmap->map[i].cqe_req = 0;443}444}445446/*447* Create an ib_qp struct that is either a QP or an SRQ, depending on448* the value of the is_srq parameter. If init_attr and srq_init_attr share449* fields, the field out of init_attr is used.450*/451static struct ehca_qp *internal_create_qp(452struct ib_pd *pd,453struct ib_qp_init_attr *init_attr,454struct ib_srq_init_attr *srq_init_attr,455struct ib_udata *udata, int is_srq)456{457struct ehca_qp *my_qp, *my_srq = NULL;458struct ehca_pd *my_pd = container_of(pd, struct ehca_pd, ib_pd);459struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,460ib_device);461struct ib_ucontext *context = NULL;462u64 h_ret;463int is_llqp = 0, has_srq = 0, is_user = 0;464int qp_type, max_send_sge, max_recv_sge, ret;465466/* h_call's out parameters */467struct ehca_alloc_qp_parms parms;468u32 swqe_size = 0, rwqe_size = 0, ib_qp_num;469unsigned long flags;470471if (!atomic_add_unless(&shca->num_qps, 1, shca->max_num_qps)) {472ehca_err(pd->device, "Unable to create QP, max number of %i "473"QPs reached.", shca->max_num_qps);474ehca_err(pd->device, "To increase the maximum number of QPs "475"use the number_of_qps module parameter.\n");476return ERR_PTR(-ENOSPC);477}478479if (init_attr->create_flags) {480atomic_dec(&shca->num_qps);481return ERR_PTR(-EINVAL);482}483484memset(&parms, 0, sizeof(parms));485qp_type = init_attr->qp_type;486487if (init_attr->sq_sig_type != IB_SIGNAL_REQ_WR &&488init_attr->sq_sig_type != IB_SIGNAL_ALL_WR) {489ehca_err(pd->device, "init_attr->sg_sig_type=%x not allowed",490init_attr->sq_sig_type);491atomic_dec(&shca->num_qps);492return ERR_PTR(-EINVAL);493}494495/* save LLQP info */496if (qp_type & 0x80) {497is_llqp = 1;498parms.ext_type = EQPT_LLQP;499parms.ll_comp_flags = qp_type & LLQP_COMP_MASK;500}501qp_type &= 0x1F;502init_attr->qp_type &= 0x1F;503504/* handle SRQ base QPs */505if (init_attr->srq) {506my_srq = container_of(init_attr->srq, struct ehca_qp, ib_srq);507508if (qp_type == IB_QPT_UC) {509ehca_err(pd->device, "UC with SRQ not supported");510atomic_dec(&shca->num_qps);511return ERR_PTR(-EINVAL);512}513514has_srq = 1;515parms.ext_type = EQPT_SRQBASE;516parms.srq_qpn = my_srq->real_qp_num;517}518519if (is_llqp && has_srq) {520ehca_err(pd->device, "LLQPs can't have an SRQ");521atomic_dec(&shca->num_qps);522return ERR_PTR(-EINVAL);523}524525/* handle SRQs */526if (is_srq) {527parms.ext_type = EQPT_SRQ;528parms.srq_limit = srq_init_attr->attr.srq_limit;529if (init_attr->cap.max_recv_sge > 3) {530ehca_err(pd->device, "no more than three SGEs "531"supported for SRQ pd=%p max_sge=%x",532pd, init_attr->cap.max_recv_sge);533atomic_dec(&shca->num_qps);534return ERR_PTR(-EINVAL);535}536}537538/* check QP type */539if (qp_type != IB_QPT_UD &&540qp_type != IB_QPT_UC &&541qp_type != IB_QPT_RC &&542qp_type != IB_QPT_SMI &&543qp_type != IB_QPT_GSI) {544ehca_err(pd->device, "wrong QP Type=%x", qp_type);545atomic_dec(&shca->num_qps);546return ERR_PTR(-EINVAL);547}548549if (is_llqp) {550switch (qp_type) {551case IB_QPT_RC:552if ((init_attr->cap.max_send_wr > 255) ||553(init_attr->cap.max_recv_wr > 255)) {554ehca_err(pd->device,555"Invalid Number of max_sq_wr=%x "556"or max_rq_wr=%x for RC LLQP",557init_attr->cap.max_send_wr,558init_attr->cap.max_recv_wr);559atomic_dec(&shca->num_qps);560return ERR_PTR(-EINVAL);561}562break;563case IB_QPT_UD:564if (!EHCA_BMASK_GET(HCA_CAP_UD_LL_QP, shca->hca_cap)) {565ehca_err(pd->device, "UD LLQP not supported "566"by this adapter");567atomic_dec(&shca->num_qps);568return ERR_PTR(-ENOSYS);569}570if (!(init_attr->cap.max_send_sge <= 5571&& init_attr->cap.max_send_sge >= 1572&& init_attr->cap.max_recv_sge <= 5573&& init_attr->cap.max_recv_sge >= 1)) {574ehca_err(pd->device,575"Invalid Number of max_send_sge=%x "576"or max_recv_sge=%x for UD LLQP",577init_attr->cap.max_send_sge,578init_attr->cap.max_recv_sge);579atomic_dec(&shca->num_qps);580return ERR_PTR(-EINVAL);581} else if (init_attr->cap.max_send_wr > 255) {582ehca_err(pd->device,583"Invalid Number of "584"max_send_wr=%x for UD QP_TYPE=%x",585init_attr->cap.max_send_wr, qp_type);586atomic_dec(&shca->num_qps);587return ERR_PTR(-EINVAL);588}589break;590default:591ehca_err(pd->device, "unsupported LL QP Type=%x",592qp_type);593atomic_dec(&shca->num_qps);594return ERR_PTR(-EINVAL);595}596} else {597int max_sge = (qp_type == IB_QPT_UD || qp_type == IB_QPT_SMI598|| qp_type == IB_QPT_GSI) ? 250 : 252;599600if (init_attr->cap.max_send_sge > max_sge601|| init_attr->cap.max_recv_sge > max_sge) {602ehca_err(pd->device, "Invalid number of SGEs requested "603"send_sge=%x recv_sge=%x max_sge=%x",604init_attr->cap.max_send_sge,605init_attr->cap.max_recv_sge, max_sge);606atomic_dec(&shca->num_qps);607return ERR_PTR(-EINVAL);608}609}610611my_qp = kmem_cache_zalloc(qp_cache, GFP_KERNEL);612if (!my_qp) {613ehca_err(pd->device, "pd=%p not enough memory to alloc qp", pd);614atomic_dec(&shca->num_qps);615return ERR_PTR(-ENOMEM);616}617618if (pd->uobject && udata) {619is_user = 1;620context = pd->uobject->context;621}622623atomic_set(&my_qp->nr_events, 0);624init_waitqueue_head(&my_qp->wait_completion);625spin_lock_init(&my_qp->spinlock_s);626spin_lock_init(&my_qp->spinlock_r);627my_qp->qp_type = qp_type;628my_qp->ext_type = parms.ext_type;629my_qp->state = IB_QPS_RESET;630631if (init_attr->recv_cq)632my_qp->recv_cq =633container_of(init_attr->recv_cq, struct ehca_cq, ib_cq);634if (init_attr->send_cq)635my_qp->send_cq =636container_of(init_attr->send_cq, struct ehca_cq, ib_cq);637638do {639if (!idr_pre_get(&ehca_qp_idr, GFP_KERNEL)) {640ret = -ENOMEM;641ehca_err(pd->device, "Can't reserve idr resources.");642goto create_qp_exit0;643}644645write_lock_irqsave(&ehca_qp_idr_lock, flags);646ret = idr_get_new(&ehca_qp_idr, my_qp, &my_qp->token);647write_unlock_irqrestore(&ehca_qp_idr_lock, flags);648} while (ret == -EAGAIN);649650if (ret) {651ret = -ENOMEM;652ehca_err(pd->device, "Can't allocate new idr entry.");653goto create_qp_exit0;654}655656if (my_qp->token > 0x1FFFFFF) {657ret = -EINVAL;658ehca_err(pd->device, "Invalid number of qp");659goto create_qp_exit1;660}661662if (has_srq)663parms.srq_token = my_qp->token;664665parms.servicetype = ibqptype2servicetype(qp_type);666if (parms.servicetype < 0) {667ret = -EINVAL;668ehca_err(pd->device, "Invalid qp_type=%x", qp_type);669goto create_qp_exit1;670}671672/* Always signal by WQE so we can hide circ. WQEs */673parms.sigtype = HCALL_SIGT_BY_WQE;674675/* UD_AV CIRCUMVENTION */676max_send_sge = init_attr->cap.max_send_sge;677max_recv_sge = init_attr->cap.max_recv_sge;678if (parms.servicetype == ST_UD && !is_llqp) {679max_send_sge += 2;680max_recv_sge += 2;681}682683parms.token = my_qp->token;684parms.eq_handle = shca->eq.ipz_eq_handle;685parms.pd = my_pd->fw_pd;686if (my_qp->send_cq)687parms.send_cq_handle = my_qp->send_cq->ipz_cq_handle;688if (my_qp->recv_cq)689parms.recv_cq_handle = my_qp->recv_cq->ipz_cq_handle;690691parms.squeue.max_wr = init_attr->cap.max_send_wr;692parms.rqueue.max_wr = init_attr->cap.max_recv_wr;693parms.squeue.max_sge = max_send_sge;694parms.rqueue.max_sge = max_recv_sge;695696/* RC QPs need one more SWQE for unsolicited ack circumvention */697if (qp_type == IB_QPT_RC)698parms.squeue.max_wr++;699700if (EHCA_BMASK_GET(HCA_CAP_MINI_QP, shca->hca_cap)) {701if (HAS_SQ(my_qp))702ehca_determine_small_queue(703&parms.squeue, max_send_sge, is_llqp);704if (HAS_RQ(my_qp))705ehca_determine_small_queue(706&parms.rqueue, max_recv_sge, is_llqp);707parms.qp_storage =708(parms.squeue.is_small || parms.rqueue.is_small);709}710711h_ret = hipz_h_alloc_resource_qp(shca->ipz_hca_handle, &parms, is_user);712if (h_ret != H_SUCCESS) {713ehca_err(pd->device, "h_alloc_resource_qp() failed h_ret=%lli",714h_ret);715ret = ehca2ib_return_code(h_ret);716goto create_qp_exit1;717}718719ib_qp_num = my_qp->real_qp_num = parms.real_qp_num;720my_qp->ipz_qp_handle = parms.qp_handle;721my_qp->galpas = parms.galpas;722723swqe_size = ehca_calc_wqe_size(parms.squeue.act_nr_sges, is_llqp);724rwqe_size = ehca_calc_wqe_size(parms.rqueue.act_nr_sges, is_llqp);725726switch (qp_type) {727case IB_QPT_RC:728if (is_llqp) {729parms.squeue.act_nr_sges = 1;730parms.rqueue.act_nr_sges = 1;731}732/* hide the extra WQE */733parms.squeue.act_nr_wqes--;734break;735case IB_QPT_UD:736case IB_QPT_GSI:737case IB_QPT_SMI:738/* UD circumvention */739if (is_llqp) {740parms.squeue.act_nr_sges = 1;741parms.rqueue.act_nr_sges = 1;742} else {743parms.squeue.act_nr_sges -= 2;744parms.rqueue.act_nr_sges -= 2;745}746747if (IB_QPT_GSI == qp_type || IB_QPT_SMI == qp_type) {748parms.squeue.act_nr_wqes = init_attr->cap.max_send_wr;749parms.rqueue.act_nr_wqes = init_attr->cap.max_recv_wr;750parms.squeue.act_nr_sges = init_attr->cap.max_send_sge;751parms.rqueue.act_nr_sges = init_attr->cap.max_recv_sge;752ib_qp_num = (qp_type == IB_QPT_SMI) ? 0 : 1;753}754755break;756757default:758break;759}760761/* initialize r/squeue and register queue pages */762if (HAS_SQ(my_qp)) {763ret = init_qp_queue(764shca, my_pd, my_qp, &my_qp->ipz_squeue, 0,765HAS_RQ(my_qp) ? H_PAGE_REGISTERED : H_SUCCESS,766&parms.squeue, swqe_size);767if (ret) {768ehca_err(pd->device, "Couldn't initialize squeue "769"and pages ret=%i", ret);770goto create_qp_exit2;771}772773if (!is_user) {774my_qp->sq_map.entries = my_qp->ipz_squeue.queue_length /775my_qp->ipz_squeue.qe_size;776my_qp->sq_map.map = vmalloc(my_qp->sq_map.entries *777sizeof(struct ehca_qmap_entry));778if (!my_qp->sq_map.map) {779ehca_err(pd->device, "Couldn't allocate squeue "780"map ret=%i", ret);781goto create_qp_exit3;782}783INIT_LIST_HEAD(&my_qp->sq_err_node);784/* to avoid the generation of bogus flush CQEs */785reset_queue_map(&my_qp->sq_map);786}787}788789if (HAS_RQ(my_qp)) {790ret = init_qp_queue(791shca, my_pd, my_qp, &my_qp->ipz_rqueue, 1,792H_SUCCESS, &parms.rqueue, rwqe_size);793if (ret) {794ehca_err(pd->device, "Couldn't initialize rqueue "795"and pages ret=%i", ret);796goto create_qp_exit4;797}798if (!is_user) {799my_qp->rq_map.entries = my_qp->ipz_rqueue.queue_length /800my_qp->ipz_rqueue.qe_size;801my_qp->rq_map.map = vmalloc(my_qp->rq_map.entries *802sizeof(struct ehca_qmap_entry));803if (!my_qp->rq_map.map) {804ehca_err(pd->device, "Couldn't allocate squeue "805"map ret=%i", ret);806goto create_qp_exit5;807}808INIT_LIST_HEAD(&my_qp->rq_err_node);809/* to avoid the generation of bogus flush CQEs */810reset_queue_map(&my_qp->rq_map);811}812} else if (init_attr->srq && !is_user) {813/* this is a base QP, use the queue map of the SRQ */814my_qp->rq_map = my_srq->rq_map;815INIT_LIST_HEAD(&my_qp->rq_err_node);816817my_qp->ipz_rqueue = my_srq->ipz_rqueue;818}819820if (is_srq) {821my_qp->ib_srq.pd = &my_pd->ib_pd;822my_qp->ib_srq.device = my_pd->ib_pd.device;823824my_qp->ib_srq.srq_context = init_attr->qp_context;825my_qp->ib_srq.event_handler = init_attr->event_handler;826} else {827my_qp->ib_qp.qp_num = ib_qp_num;828my_qp->ib_qp.pd = &my_pd->ib_pd;829my_qp->ib_qp.device = my_pd->ib_pd.device;830831my_qp->ib_qp.recv_cq = init_attr->recv_cq;832my_qp->ib_qp.send_cq = init_attr->send_cq;833834my_qp->ib_qp.qp_type = qp_type;835my_qp->ib_qp.srq = init_attr->srq;836837my_qp->ib_qp.qp_context = init_attr->qp_context;838my_qp->ib_qp.event_handler = init_attr->event_handler;839}840841init_attr->cap.max_inline_data = 0; /* not supported yet */842init_attr->cap.max_recv_sge = parms.rqueue.act_nr_sges;843init_attr->cap.max_recv_wr = parms.rqueue.act_nr_wqes;844init_attr->cap.max_send_sge = parms.squeue.act_nr_sges;845init_attr->cap.max_send_wr = parms.squeue.act_nr_wqes;846my_qp->init_attr = *init_attr;847848if (qp_type == IB_QPT_SMI || qp_type == IB_QPT_GSI) {849shca->sport[init_attr->port_num - 1].ibqp_sqp[qp_type] =850&my_qp->ib_qp;851if (ehca_nr_ports < 0) {852/* alloc array to cache subsequent modify qp parms853* for autodetect mode854*/855my_qp->mod_qp_parm =856kzalloc(EHCA_MOD_QP_PARM_MAX *857sizeof(*my_qp->mod_qp_parm),858GFP_KERNEL);859if (!my_qp->mod_qp_parm) {860ehca_err(pd->device,861"Could not alloc mod_qp_parm");862goto create_qp_exit5;863}864}865}866867/* NOTE: define_apq0() not supported yet */868if (qp_type == IB_QPT_GSI) {869h_ret = ehca_define_sqp(shca, my_qp, init_attr);870if (h_ret != H_SUCCESS) {871kfree(my_qp->mod_qp_parm);872my_qp->mod_qp_parm = NULL;873/* the QP pointer is no longer valid */874shca->sport[init_attr->port_num - 1].ibqp_sqp[qp_type] =875NULL;876ret = ehca2ib_return_code(h_ret);877goto create_qp_exit6;878}879}880881if (my_qp->send_cq) {882ret = ehca_cq_assign_qp(my_qp->send_cq, my_qp);883if (ret) {884ehca_err(pd->device,885"Couldn't assign qp to send_cq ret=%i", ret);886goto create_qp_exit7;887}888}889890/* copy queues, galpa data to user space */891if (context && udata) {892struct ehca_create_qp_resp resp;893memset(&resp, 0, sizeof(resp));894895resp.qp_num = my_qp->real_qp_num;896resp.token = my_qp->token;897resp.qp_type = my_qp->qp_type;898resp.ext_type = my_qp->ext_type;899resp.qkey = my_qp->qkey;900resp.real_qp_num = my_qp->real_qp_num;901902if (HAS_SQ(my_qp))903queue2resp(&resp.ipz_squeue, &my_qp->ipz_squeue);904if (HAS_RQ(my_qp))905queue2resp(&resp.ipz_rqueue, &my_qp->ipz_rqueue);906resp.fw_handle_ofs = (u32)907(my_qp->galpas.user.fw_handle & (PAGE_SIZE - 1));908909if (ib_copy_to_udata(udata, &resp, sizeof resp)) {910ehca_err(pd->device, "Copy to udata failed");911ret = -EINVAL;912goto create_qp_exit8;913}914}915916return my_qp;917918create_qp_exit8:919ehca_cq_unassign_qp(my_qp->send_cq, my_qp->real_qp_num);920921create_qp_exit7:922kfree(my_qp->mod_qp_parm);923924create_qp_exit6:925if (HAS_RQ(my_qp) && !is_user)926vfree(my_qp->rq_map.map);927928create_qp_exit5:929if (HAS_RQ(my_qp))930ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue);931932create_qp_exit4:933if (HAS_SQ(my_qp) && !is_user)934vfree(my_qp->sq_map.map);935936create_qp_exit3:937if (HAS_SQ(my_qp))938ipz_queue_dtor(my_pd, &my_qp->ipz_squeue);939940create_qp_exit2:941hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);942943create_qp_exit1:944write_lock_irqsave(&ehca_qp_idr_lock, flags);945idr_remove(&ehca_qp_idr, my_qp->token);946write_unlock_irqrestore(&ehca_qp_idr_lock, flags);947948create_qp_exit0:949kmem_cache_free(qp_cache, my_qp);950atomic_dec(&shca->num_qps);951return ERR_PTR(ret);952}953954struct ib_qp *ehca_create_qp(struct ib_pd *pd,955struct ib_qp_init_attr *qp_init_attr,956struct ib_udata *udata)957{958struct ehca_qp *ret;959960ret = internal_create_qp(pd, qp_init_attr, NULL, udata, 0);961return IS_ERR(ret) ? (struct ib_qp *)ret : &ret->ib_qp;962}963964static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,965struct ib_uobject *uobject);966967struct ib_srq *ehca_create_srq(struct ib_pd *pd,968struct ib_srq_init_attr *srq_init_attr,969struct ib_udata *udata)970{971struct ib_qp_init_attr qp_init_attr;972struct ehca_qp *my_qp;973struct ib_srq *ret;974struct ehca_shca *shca = container_of(pd->device, struct ehca_shca,975ib_device);976struct hcp_modify_qp_control_block *mqpcb;977u64 hret, update_mask;978979/* For common attributes, internal_create_qp() takes its info980* out of qp_init_attr, so copy all common attrs there.981*/982memset(&qp_init_attr, 0, sizeof(qp_init_attr));983qp_init_attr.event_handler = srq_init_attr->event_handler;984qp_init_attr.qp_context = srq_init_attr->srq_context;985qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;986qp_init_attr.qp_type = IB_QPT_RC;987qp_init_attr.cap.max_recv_wr = srq_init_attr->attr.max_wr;988qp_init_attr.cap.max_recv_sge = srq_init_attr->attr.max_sge;989990my_qp = internal_create_qp(pd, &qp_init_attr, srq_init_attr, udata, 1);991if (IS_ERR(my_qp))992return (struct ib_srq *)my_qp;993994/* copy back return values */995srq_init_attr->attr.max_wr = qp_init_attr.cap.max_recv_wr;996srq_init_attr->attr.max_sge = 3;997998/* drive SRQ into RTR state */999mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);1000if (!mqpcb) {1001ehca_err(pd->device, "Could not get zeroed page for mqpcb "1002"ehca_qp=%p qp_num=%x ", my_qp, my_qp->real_qp_num);1003ret = ERR_PTR(-ENOMEM);1004goto create_srq1;1005}10061007mqpcb->qp_state = EHCA_QPS_INIT;1008mqpcb->prim_phys_port = 1;1009update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);1010hret = hipz_h_modify_qp(shca->ipz_hca_handle,1011my_qp->ipz_qp_handle,1012&my_qp->pf,1013update_mask,1014mqpcb, my_qp->galpas.kernel);1015if (hret != H_SUCCESS) {1016ehca_err(pd->device, "Could not modify SRQ to INIT "1017"ehca_qp=%p qp_num=%x h_ret=%lli",1018my_qp, my_qp->real_qp_num, hret);1019goto create_srq2;1020}10211022mqpcb->qp_enable = 1;1023update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);1024hret = hipz_h_modify_qp(shca->ipz_hca_handle,1025my_qp->ipz_qp_handle,1026&my_qp->pf,1027update_mask,1028mqpcb, my_qp->galpas.kernel);1029if (hret != H_SUCCESS) {1030ehca_err(pd->device, "Could not enable SRQ "1031"ehca_qp=%p qp_num=%x h_ret=%lli",1032my_qp, my_qp->real_qp_num, hret);1033goto create_srq2;1034}10351036mqpcb->qp_state = EHCA_QPS_RTR;1037update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);1038hret = hipz_h_modify_qp(shca->ipz_hca_handle,1039my_qp->ipz_qp_handle,1040&my_qp->pf,1041update_mask,1042mqpcb, my_qp->galpas.kernel);1043if (hret != H_SUCCESS) {1044ehca_err(pd->device, "Could not modify SRQ to RTR "1045"ehca_qp=%p qp_num=%x h_ret=%lli",1046my_qp, my_qp->real_qp_num, hret);1047goto create_srq2;1048}10491050ehca_free_fw_ctrlblock(mqpcb);10511052return &my_qp->ib_srq;10531054create_srq2:1055ret = ERR_PTR(ehca2ib_return_code(hret));1056ehca_free_fw_ctrlblock(mqpcb);10571058create_srq1:1059internal_destroy_qp(pd->device, my_qp, my_qp->ib_srq.uobject);10601061return ret;1062}10631064/*1065* prepare_sqe_rts called by internal_modify_qp() at trans sqe -> rts1066* set purge bit of bad wqe and subsequent wqes to avoid reentering sqe1067* returns total number of bad wqes in bad_wqe_cnt1068*/1069static int prepare_sqe_rts(struct ehca_qp *my_qp, struct ehca_shca *shca,1070int *bad_wqe_cnt)1071{1072u64 h_ret;1073struct ipz_queue *squeue;1074void *bad_send_wqe_p, *bad_send_wqe_v;1075u64 q_ofs;1076struct ehca_wqe *wqe;1077int qp_num = my_qp->ib_qp.qp_num;10781079/* get send wqe pointer */1080h_ret = hipz_h_disable_and_get_wqe(shca->ipz_hca_handle,1081my_qp->ipz_qp_handle, &my_qp->pf,1082&bad_send_wqe_p, NULL, 2);1083if (h_ret != H_SUCCESS) {1084ehca_err(&shca->ib_device, "hipz_h_disable_and_get_wqe() failed"1085" ehca_qp=%p qp_num=%x h_ret=%lli",1086my_qp, qp_num, h_ret);1087return ehca2ib_return_code(h_ret);1088}1089bad_send_wqe_p = (void *)((u64)bad_send_wqe_p & (~(1L << 63)));1090ehca_dbg(&shca->ib_device, "qp_num=%x bad_send_wqe_p=%p",1091qp_num, bad_send_wqe_p);1092/* convert wqe pointer to vadr */1093bad_send_wqe_v = abs_to_virt((u64)bad_send_wqe_p);1094if (ehca_debug_level >= 2)1095ehca_dmp(bad_send_wqe_v, 32, "qp_num=%x bad_wqe", qp_num);1096squeue = &my_qp->ipz_squeue;1097if (ipz_queue_abs_to_offset(squeue, (u64)bad_send_wqe_p, &q_ofs)) {1098ehca_err(&shca->ib_device, "failed to get wqe offset qp_num=%x"1099" bad_send_wqe_p=%p", qp_num, bad_send_wqe_p);1100return -EFAULT;1101}11021103/* loop sets wqe's purge bit */1104wqe = (struct ehca_wqe *)ipz_qeit_calc(squeue, q_ofs);1105*bad_wqe_cnt = 0;1106while (wqe->optype != 0xff && wqe->wqef != 0xff) {1107if (ehca_debug_level >= 2)1108ehca_dmp(wqe, 32, "qp_num=%x wqe", qp_num);1109wqe->nr_of_data_seg = 0; /* suppress data access */1110wqe->wqef = WQEF_PURGE; /* WQE to be purged */1111q_ofs = ipz_queue_advance_offset(squeue, q_ofs);1112wqe = (struct ehca_wqe *)ipz_qeit_calc(squeue, q_ofs);1113*bad_wqe_cnt = (*bad_wqe_cnt)+1;1114}1115/*1116* bad wqe will be reprocessed and ignored when pol_cq() is called,1117* i.e. nr of wqes with flush error status is one less1118*/1119ehca_dbg(&shca->ib_device, "qp_num=%x flusherr_wqe_cnt=%x",1120qp_num, (*bad_wqe_cnt)-1);1121wqe->wqef = 0;11221123return 0;1124}11251126static int calc_left_cqes(u64 wqe_p, struct ipz_queue *ipz_queue,1127struct ehca_queue_map *qmap)1128{1129void *wqe_v;1130u64 q_ofs;1131u32 wqe_idx;1132unsigned int tail_idx;11331134/* convert real to abs address */1135wqe_p = wqe_p & (~(1UL << 63));11361137wqe_v = abs_to_virt(wqe_p);11381139if (ipz_queue_abs_to_offset(ipz_queue, wqe_p, &q_ofs)) {1140ehca_gen_err("Invalid offset for calculating left cqes "1141"wqe_p=%#llx wqe_v=%p\n", wqe_p, wqe_v);1142return -EFAULT;1143}11441145tail_idx = next_index(qmap->tail, qmap->entries);1146wqe_idx = q_ofs / ipz_queue->qe_size;11471148/* check all processed wqes, whether a cqe is requested or not */1149while (tail_idx != wqe_idx) {1150if (qmap->map[tail_idx].cqe_req)1151qmap->left_to_poll++;1152tail_idx = next_index(tail_idx, qmap->entries);1153}1154/* save index in queue, where we have to start flushing */1155qmap->next_wqe_idx = wqe_idx;1156return 0;1157}11581159static int check_for_left_cqes(struct ehca_qp *my_qp, struct ehca_shca *shca)1160{1161u64 h_ret;1162void *send_wqe_p, *recv_wqe_p;1163int ret;1164unsigned long flags;1165int qp_num = my_qp->ib_qp.qp_num;11661167/* this hcall is not supported on base QPs */1168if (my_qp->ext_type != EQPT_SRQBASE) {1169/* get send and receive wqe pointer */1170h_ret = hipz_h_disable_and_get_wqe(shca->ipz_hca_handle,1171my_qp->ipz_qp_handle, &my_qp->pf,1172&send_wqe_p, &recv_wqe_p, 4);1173if (h_ret != H_SUCCESS) {1174ehca_err(&shca->ib_device, "disable_and_get_wqe() "1175"failed ehca_qp=%p qp_num=%x h_ret=%lli",1176my_qp, qp_num, h_ret);1177return ehca2ib_return_code(h_ret);1178}11791180/*1181* acquire lock to ensure that nobody is polling the cq which1182* could mean that the qmap->tail pointer is in an1183* inconsistent state.1184*/1185spin_lock_irqsave(&my_qp->send_cq->spinlock, flags);1186ret = calc_left_cqes((u64)send_wqe_p, &my_qp->ipz_squeue,1187&my_qp->sq_map);1188spin_unlock_irqrestore(&my_qp->send_cq->spinlock, flags);1189if (ret)1190return ret;119111921193spin_lock_irqsave(&my_qp->recv_cq->spinlock, flags);1194ret = calc_left_cqes((u64)recv_wqe_p, &my_qp->ipz_rqueue,1195&my_qp->rq_map);1196spin_unlock_irqrestore(&my_qp->recv_cq->spinlock, flags);1197if (ret)1198return ret;1199} else {1200spin_lock_irqsave(&my_qp->send_cq->spinlock, flags);1201my_qp->sq_map.left_to_poll = 0;1202my_qp->sq_map.next_wqe_idx = next_index(my_qp->sq_map.tail,1203my_qp->sq_map.entries);1204spin_unlock_irqrestore(&my_qp->send_cq->spinlock, flags);12051206spin_lock_irqsave(&my_qp->recv_cq->spinlock, flags);1207my_qp->rq_map.left_to_poll = 0;1208my_qp->rq_map.next_wqe_idx = next_index(my_qp->rq_map.tail,1209my_qp->rq_map.entries);1210spin_unlock_irqrestore(&my_qp->recv_cq->spinlock, flags);1211}12121213/* this assures flush cqes being generated only for pending wqes */1214if ((my_qp->sq_map.left_to_poll == 0) &&1215(my_qp->rq_map.left_to_poll == 0)) {1216spin_lock_irqsave(&my_qp->send_cq->spinlock, flags);1217ehca_add_to_err_list(my_qp, 1);1218spin_unlock_irqrestore(&my_qp->send_cq->spinlock, flags);12191220if (HAS_RQ(my_qp)) {1221spin_lock_irqsave(&my_qp->recv_cq->spinlock, flags);1222ehca_add_to_err_list(my_qp, 0);1223spin_unlock_irqrestore(&my_qp->recv_cq->spinlock,1224flags);1225}1226}12271228return 0;1229}12301231/*1232* internal_modify_qp with circumvention to handle aqp0 properly1233* smi_reset2init indicates if this is an internal reset-to-init-call for1234* smi. This flag must always be zero if called from ehca_modify_qp()!1235* This internal func was intorduced to avoid recursion of ehca_modify_qp()!1236*/1237static int internal_modify_qp(struct ib_qp *ibqp,1238struct ib_qp_attr *attr,1239int attr_mask, int smi_reset2init)1240{1241enum ib_qp_state qp_cur_state, qp_new_state;1242int cnt, qp_attr_idx, ret = 0;1243enum ib_qp_statetrans statetrans;1244struct hcp_modify_qp_control_block *mqpcb;1245struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);1246struct ehca_shca *shca =1247container_of(ibqp->pd->device, struct ehca_shca, ib_device);1248u64 update_mask;1249u64 h_ret;1250int bad_wqe_cnt = 0;1251int is_user = 0;1252int squeue_locked = 0;1253unsigned long flags = 0;12541255/* do query_qp to obtain current attr values */1256mqpcb = ehca_alloc_fw_ctrlblock(GFP_ATOMIC);1257if (!mqpcb) {1258ehca_err(ibqp->device, "Could not get zeroed page for mqpcb "1259"ehca_qp=%p qp_num=%x ", my_qp, ibqp->qp_num);1260return -ENOMEM;1261}12621263h_ret = hipz_h_query_qp(shca->ipz_hca_handle,1264my_qp->ipz_qp_handle,1265&my_qp->pf,1266mqpcb, my_qp->galpas.kernel);1267if (h_ret != H_SUCCESS) {1268ehca_err(ibqp->device, "hipz_h_query_qp() failed "1269"ehca_qp=%p qp_num=%x h_ret=%lli",1270my_qp, ibqp->qp_num, h_ret);1271ret = ehca2ib_return_code(h_ret);1272goto modify_qp_exit1;1273}1274if (ibqp->uobject)1275is_user = 1;12761277qp_cur_state = ehca2ib_qp_state(mqpcb->qp_state);12781279if (qp_cur_state == -EINVAL) { /* invalid qp state */1280ret = -EINVAL;1281ehca_err(ibqp->device, "Invalid current ehca_qp_state=%x "1282"ehca_qp=%p qp_num=%x",1283mqpcb->qp_state, my_qp, ibqp->qp_num);1284goto modify_qp_exit1;1285}1286/*1287* circumvention to set aqp0 initial state to init1288* as expected by IB spec1289*/1290if (smi_reset2init == 0 &&1291ibqp->qp_type == IB_QPT_SMI &&1292qp_cur_state == IB_QPS_RESET &&1293(attr_mask & IB_QP_STATE) &&1294attr->qp_state == IB_QPS_INIT) { /* RESET -> INIT */1295struct ib_qp_attr smiqp_attr = {1296.qp_state = IB_QPS_INIT,1297.port_num = my_qp->init_attr.port_num,1298.pkey_index = 0,1299.qkey = 01300};1301int smiqp_attr_mask = IB_QP_STATE | IB_QP_PORT |1302IB_QP_PKEY_INDEX | IB_QP_QKEY;1303int smirc = internal_modify_qp(1304ibqp, &smiqp_attr, smiqp_attr_mask, 1);1305if (smirc) {1306ehca_err(ibqp->device, "SMI RESET -> INIT failed. "1307"ehca_modify_qp() rc=%i", smirc);1308ret = H_PARAMETER;1309goto modify_qp_exit1;1310}1311qp_cur_state = IB_QPS_INIT;1312ehca_dbg(ibqp->device, "SMI RESET -> INIT succeeded");1313}1314/* is transmitted current state equal to "real" current state */1315if ((attr_mask & IB_QP_CUR_STATE) &&1316qp_cur_state != attr->cur_qp_state) {1317ret = -EINVAL;1318ehca_err(ibqp->device,1319"Invalid IB_QP_CUR_STATE attr->curr_qp_state=%x <>"1320" actual cur_qp_state=%x. ehca_qp=%p qp_num=%x",1321attr->cur_qp_state, qp_cur_state, my_qp, ibqp->qp_num);1322goto modify_qp_exit1;1323}13241325ehca_dbg(ibqp->device, "ehca_qp=%p qp_num=%x current qp_state=%x "1326"new qp_state=%x attribute_mask=%x",1327my_qp, ibqp->qp_num, qp_cur_state, attr->qp_state, attr_mask);13281329qp_new_state = attr_mask & IB_QP_STATE ? attr->qp_state : qp_cur_state;1330if (!smi_reset2init &&1331!ib_modify_qp_is_ok(qp_cur_state, qp_new_state, ibqp->qp_type,1332attr_mask)) {1333ret = -EINVAL;1334ehca_err(ibqp->device,1335"Invalid qp transition new_state=%x cur_state=%x "1336"ehca_qp=%p qp_num=%x attr_mask=%x", qp_new_state,1337qp_cur_state, my_qp, ibqp->qp_num, attr_mask);1338goto modify_qp_exit1;1339}13401341mqpcb->qp_state = ib2ehca_qp_state(qp_new_state);1342if (mqpcb->qp_state)1343update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_STATE, 1);1344else {1345ret = -EINVAL;1346ehca_err(ibqp->device, "Invalid new qp state=%x "1347"ehca_qp=%p qp_num=%x",1348qp_new_state, my_qp, ibqp->qp_num);1349goto modify_qp_exit1;1350}13511352/* retrieve state transition struct to get req and opt attrs */1353statetrans = get_modqp_statetrans(qp_cur_state, qp_new_state);1354if (statetrans < 0) {1355ret = -EINVAL;1356ehca_err(ibqp->device, "<INVALID STATE CHANGE> qp_cur_state=%x "1357"new_qp_state=%x State_xsition=%x ehca_qp=%p "1358"qp_num=%x", qp_cur_state, qp_new_state,1359statetrans, my_qp, ibqp->qp_num);1360goto modify_qp_exit1;1361}13621363qp_attr_idx = ib2ehcaqptype(ibqp->qp_type);13641365if (qp_attr_idx < 0) {1366ret = qp_attr_idx;1367ehca_err(ibqp->device,1368"Invalid QP type=%x ehca_qp=%p qp_num=%x",1369ibqp->qp_type, my_qp, ibqp->qp_num);1370goto modify_qp_exit1;1371}13721373ehca_dbg(ibqp->device,1374"ehca_qp=%p qp_num=%x <VALID STATE CHANGE> qp_state_xsit=%x",1375my_qp, ibqp->qp_num, statetrans);13761377/* eHCA2 rev2 and higher require the SEND_GRH_FLAG to be set1378* in non-LL UD QPs.1379*/1380if ((my_qp->qp_type == IB_QPT_UD) &&1381(my_qp->ext_type != EQPT_LLQP) &&1382(statetrans == IB_QPST_INIT2RTR) &&1383(shca->hw_level >= 0x22)) {1384update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);1385mqpcb->send_grh_flag = 1;1386}13871388/* sqe -> rts: set purge bit of bad wqe before actual trans */1389if ((my_qp->qp_type == IB_QPT_UD ||1390my_qp->qp_type == IB_QPT_GSI ||1391my_qp->qp_type == IB_QPT_SMI) &&1392statetrans == IB_QPST_SQE2RTS) {1393/* mark next free wqe if kernel */1394if (!ibqp->uobject) {1395struct ehca_wqe *wqe;1396/* lock send queue */1397spin_lock_irqsave(&my_qp->spinlock_s, flags);1398squeue_locked = 1;1399/* mark next free wqe */1400wqe = (struct ehca_wqe *)1401ipz_qeit_get(&my_qp->ipz_squeue);1402wqe->optype = wqe->wqef = 0xff;1403ehca_dbg(ibqp->device, "qp_num=%x next_free_wqe=%p",1404ibqp->qp_num, wqe);1405}1406ret = prepare_sqe_rts(my_qp, shca, &bad_wqe_cnt);1407if (ret) {1408ehca_err(ibqp->device, "prepare_sqe_rts() failed "1409"ehca_qp=%p qp_num=%x ret=%i",1410my_qp, ibqp->qp_num, ret);1411goto modify_qp_exit2;1412}1413}14141415/*1416* enable RDMA_Atomic_Control if reset->init und reliable con1417* this is necessary since gen2 does not provide that flag,1418* but pHyp requires it1419*/1420if (statetrans == IB_QPST_RESET2INIT &&1421(ibqp->qp_type == IB_QPT_RC || ibqp->qp_type == IB_QPT_UC)) {1422mqpcb->rdma_atomic_ctrl = 3;1423update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RDMA_ATOMIC_CTRL, 1);1424}1425/* circ. pHyp requires #RDMA/Atomic Resp Res for UC INIT -> RTR */1426if (statetrans == IB_QPST_INIT2RTR &&1427(ibqp->qp_type == IB_QPT_UC) &&1428!(attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)) {1429mqpcb->rdma_nr_atomic_resp_res = 1; /* default to 1 */1430update_mask |=1431EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);1432}14331434if (attr_mask & IB_QP_PKEY_INDEX) {1435if (attr->pkey_index >= 16) {1436ret = -EINVAL;1437ehca_err(ibqp->device, "Invalid pkey_index=%x. "1438"ehca_qp=%p qp_num=%x max_pkey_index=f",1439attr->pkey_index, my_qp, ibqp->qp_num);1440goto modify_qp_exit2;1441}1442mqpcb->prim_p_key_idx = attr->pkey_index;1443update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_P_KEY_IDX, 1);1444}1445if (attr_mask & IB_QP_PORT) {1446struct ehca_sport *sport;1447struct ehca_qp *aqp1;1448if (attr->port_num < 1 || attr->port_num > shca->num_ports) {1449ret = -EINVAL;1450ehca_err(ibqp->device, "Invalid port=%x. "1451"ehca_qp=%p qp_num=%x num_ports=%x",1452attr->port_num, my_qp, ibqp->qp_num,1453shca->num_ports);1454goto modify_qp_exit2;1455}1456sport = &shca->sport[attr->port_num - 1];1457if (!sport->ibqp_sqp[IB_QPT_GSI]) {1458/* should not occur */1459ret = -EFAULT;1460ehca_err(ibqp->device, "AQP1 was not created for "1461"port=%x", attr->port_num);1462goto modify_qp_exit2;1463}1464aqp1 = container_of(sport->ibqp_sqp[IB_QPT_GSI],1465struct ehca_qp, ib_qp);1466if (ibqp->qp_type != IB_QPT_GSI &&1467ibqp->qp_type != IB_QPT_SMI &&1468aqp1->mod_qp_parm) {1469/*1470* firmware will reject this modify_qp() because1471* port is not activated/initialized fully1472*/1473ret = -EFAULT;1474ehca_warn(ibqp->device, "Couldn't modify qp port=%x: "1475"either port is being activated (try again) "1476"or cabling issue", attr->port_num);1477goto modify_qp_exit2;1478}1479mqpcb->prim_phys_port = attr->port_num;1480update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PRIM_PHYS_PORT, 1);1481}1482if (attr_mask & IB_QP_QKEY) {1483mqpcb->qkey = attr->qkey;1484update_mask |= EHCA_BMASK_SET(MQPCB_MASK_QKEY, 1);1485}1486if (attr_mask & IB_QP_AV) {1487mqpcb->dlid = attr->ah_attr.dlid;1488update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DLID, 1);1489mqpcb->source_path_bits = attr->ah_attr.src_path_bits;1490update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS, 1);1491mqpcb->service_level = attr->ah_attr.sl;1492update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL, 1);14931494if (ehca_calc_ipd(shca, mqpcb->prim_phys_port,1495attr->ah_attr.static_rate,1496&mqpcb->max_static_rate)) {1497ret = -EINVAL;1498goto modify_qp_exit2;1499}1500update_mask |= EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE, 1);15011502/*1503* Always supply the GRH flag, even if it's zero, to give the1504* hypervisor a clear "yes" or "no" instead of a "perhaps"1505*/1506update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG, 1);15071508/*1509* only if GRH is TRUE we might consider SOURCE_GID_IDX1510* and DEST_GID otherwise phype will return H_ATTR_PARM!!!1511*/1512if (attr->ah_attr.ah_flags == IB_AH_GRH) {1513mqpcb->send_grh_flag = 1;15141515mqpcb->source_gid_idx = attr->ah_attr.grh.sgid_index;1516update_mask |=1517EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX, 1);15181519for (cnt = 0; cnt < 16; cnt++)1520mqpcb->dest_gid.byte[cnt] =1521attr->ah_attr.grh.dgid.raw[cnt];15221523update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_GID, 1);1524mqpcb->flow_label = attr->ah_attr.grh.flow_label;1525update_mask |= EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL, 1);1526mqpcb->hop_limit = attr->ah_attr.grh.hop_limit;1527update_mask |= EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT, 1);1528mqpcb->traffic_class = attr->ah_attr.grh.traffic_class;1529update_mask |=1530EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS, 1);1531}1532}15331534if (attr_mask & IB_QP_PATH_MTU) {1535/* store ld(MTU) */1536my_qp->mtu_shift = attr->path_mtu + 7;1537mqpcb->path_mtu = attr->path_mtu;1538update_mask |= EHCA_BMASK_SET(MQPCB_MASK_PATH_MTU, 1);1539}1540if (attr_mask & IB_QP_TIMEOUT) {1541mqpcb->timeout = attr->timeout;1542update_mask |= EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT, 1);1543}1544if (attr_mask & IB_QP_RETRY_CNT) {1545mqpcb->retry_count = attr->retry_cnt;1546update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT, 1);1547}1548if (attr_mask & IB_QP_RNR_RETRY) {1549mqpcb->rnr_retry_count = attr->rnr_retry;1550update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT, 1);1551}1552if (attr_mask & IB_QP_RQ_PSN) {1553mqpcb->receive_psn = attr->rq_psn;1554update_mask |= EHCA_BMASK_SET(MQPCB_MASK_RECEIVE_PSN, 1);1555}1556if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {1557mqpcb->rdma_nr_atomic_resp_res = attr->max_dest_rd_atomic < 3 ?1558attr->max_dest_rd_atomic : 2;1559update_mask |=1560EHCA_BMASK_SET(MQPCB_MASK_RDMA_NR_ATOMIC_RESP_RES, 1);1561}1562if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {1563mqpcb->rdma_atomic_outst_dest_qp = attr->max_rd_atomic < 3 ?1564attr->max_rd_atomic : 2;1565update_mask |=1566EHCA_BMASK_SET1567(MQPCB_MASK_RDMA_ATOMIC_OUTST_DEST_QP, 1);1568}1569if (attr_mask & IB_QP_ALT_PATH) {1570if (attr->alt_port_num < 11571|| attr->alt_port_num > shca->num_ports) {1572ret = -EINVAL;1573ehca_err(ibqp->device, "Invalid alt_port=%x. "1574"ehca_qp=%p qp_num=%x num_ports=%x",1575attr->alt_port_num, my_qp, ibqp->qp_num,1576shca->num_ports);1577goto modify_qp_exit2;1578}1579mqpcb->alt_phys_port = attr->alt_port_num;15801581if (attr->alt_pkey_index >= 16) {1582ret = -EINVAL;1583ehca_err(ibqp->device, "Invalid alt_pkey_index=%x. "1584"ehca_qp=%p qp_num=%x max_pkey_index=f",1585attr->pkey_index, my_qp, ibqp->qp_num);1586goto modify_qp_exit2;1587}1588mqpcb->alt_p_key_idx = attr->alt_pkey_index;15891590mqpcb->timeout_al = attr->alt_timeout;1591mqpcb->dlid_al = attr->alt_ah_attr.dlid;1592mqpcb->source_path_bits_al = attr->alt_ah_attr.src_path_bits;1593mqpcb->service_level_al = attr->alt_ah_attr.sl;15941595if (ehca_calc_ipd(shca, mqpcb->alt_phys_port,1596attr->alt_ah_attr.static_rate,1597&mqpcb->max_static_rate_al)) {1598ret = -EINVAL;1599goto modify_qp_exit2;1600}16011602/* OpenIB doesn't support alternate retry counts - copy them */1603mqpcb->retry_count_al = mqpcb->retry_count;1604mqpcb->rnr_retry_count_al = mqpcb->rnr_retry_count;16051606update_mask |= EHCA_BMASK_SET(MQPCB_MASK_ALT_PHYS_PORT, 1)1607| EHCA_BMASK_SET(MQPCB_MASK_ALT_P_KEY_IDX, 1)1608| EHCA_BMASK_SET(MQPCB_MASK_TIMEOUT_AL, 1)1609| EHCA_BMASK_SET(MQPCB_MASK_DLID_AL, 1)1610| EHCA_BMASK_SET(MQPCB_MASK_SOURCE_PATH_BITS_AL, 1)1611| EHCA_BMASK_SET(MQPCB_MASK_SERVICE_LEVEL_AL, 1)1612| EHCA_BMASK_SET(MQPCB_MASK_MAX_STATIC_RATE_AL, 1)1613| EHCA_BMASK_SET(MQPCB_MASK_RETRY_COUNT_AL, 1)1614| EHCA_BMASK_SET(MQPCB_MASK_RNR_RETRY_COUNT_AL, 1);16151616/*1617* Always supply the GRH flag, even if it's zero, to give the1618* hypervisor a clear "yes" or "no" instead of a "perhaps"1619*/1620update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_GRH_FLAG_AL, 1);16211622/*1623* only if GRH is TRUE we might consider SOURCE_GID_IDX1624* and DEST_GID otherwise phype will return H_ATTR_PARM!!!1625*/1626if (attr->alt_ah_attr.ah_flags == IB_AH_GRH) {1627mqpcb->send_grh_flag_al = 1;16281629for (cnt = 0; cnt < 16; cnt++)1630mqpcb->dest_gid_al.byte[cnt] =1631attr->alt_ah_attr.grh.dgid.raw[cnt];1632mqpcb->source_gid_idx_al =1633attr->alt_ah_attr.grh.sgid_index;1634mqpcb->flow_label_al = attr->alt_ah_attr.grh.flow_label;1635mqpcb->hop_limit_al = attr->alt_ah_attr.grh.hop_limit;1636mqpcb->traffic_class_al =1637attr->alt_ah_attr.grh.traffic_class;16381639update_mask |=1640EHCA_BMASK_SET(MQPCB_MASK_SOURCE_GID_IDX_AL, 1)1641| EHCA_BMASK_SET(MQPCB_MASK_DEST_GID_AL, 1)1642| EHCA_BMASK_SET(MQPCB_MASK_FLOW_LABEL_AL, 1)1643| EHCA_BMASK_SET(MQPCB_MASK_HOP_LIMIT_AL, 1) |1644EHCA_BMASK_SET(MQPCB_MASK_TRAFFIC_CLASS_AL, 1);1645}1646}16471648if (attr_mask & IB_QP_MIN_RNR_TIMER) {1649mqpcb->min_rnr_nak_timer_field = attr->min_rnr_timer;1650update_mask |=1651EHCA_BMASK_SET(MQPCB_MASK_MIN_RNR_NAK_TIMER_FIELD, 1);1652}16531654if (attr_mask & IB_QP_SQ_PSN) {1655mqpcb->send_psn = attr->sq_psn;1656update_mask |= EHCA_BMASK_SET(MQPCB_MASK_SEND_PSN, 1);1657}16581659if (attr_mask & IB_QP_DEST_QPN) {1660mqpcb->dest_qp_nr = attr->dest_qp_num;1661update_mask |= EHCA_BMASK_SET(MQPCB_MASK_DEST_QP_NR, 1);1662}16631664if (attr_mask & IB_QP_PATH_MIG_STATE) {1665if (attr->path_mig_state != IB_MIG_REARM1666&& attr->path_mig_state != IB_MIG_MIGRATED) {1667ret = -EINVAL;1668ehca_err(ibqp->device, "Invalid mig_state=%x",1669attr->path_mig_state);1670goto modify_qp_exit2;1671}1672mqpcb->path_migration_state = attr->path_mig_state + 1;1673if (attr->path_mig_state == IB_MIG_REARM)1674my_qp->mig_armed = 1;1675update_mask |=1676EHCA_BMASK_SET(MQPCB_MASK_PATH_MIGRATION_STATE, 1);1677}16781679if (attr_mask & IB_QP_CAP) {1680mqpcb->max_nr_outst_send_wr = attr->cap.max_send_wr+1;1681update_mask |=1682EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_SEND_WR, 1);1683mqpcb->max_nr_outst_recv_wr = attr->cap.max_recv_wr+1;1684update_mask |=1685EHCA_BMASK_SET(MQPCB_MASK_MAX_NR_OUTST_RECV_WR, 1);1686/* no support for max_send/recv_sge yet */1687}16881689if (ehca_debug_level >= 2)1690ehca_dmp(mqpcb, 4*70, "qp_num=%x", ibqp->qp_num);16911692h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,1693my_qp->ipz_qp_handle,1694&my_qp->pf,1695update_mask,1696mqpcb, my_qp->galpas.kernel);16971698if (h_ret != H_SUCCESS) {1699ret = ehca2ib_return_code(h_ret);1700ehca_err(ibqp->device, "hipz_h_modify_qp() failed h_ret=%lli "1701"ehca_qp=%p qp_num=%x", h_ret, my_qp, ibqp->qp_num);1702goto modify_qp_exit2;1703}17041705if ((my_qp->qp_type == IB_QPT_UD ||1706my_qp->qp_type == IB_QPT_GSI ||1707my_qp->qp_type == IB_QPT_SMI) &&1708statetrans == IB_QPST_SQE2RTS) {1709/* doorbell to reprocessing wqes */1710iosync(); /* serialize GAL register access */1711hipz_update_sqa(my_qp, bad_wqe_cnt-1);1712ehca_gen_dbg("doorbell for %x wqes", bad_wqe_cnt);1713}17141715if (statetrans == IB_QPST_RESET2INIT ||1716statetrans == IB_QPST_INIT2INIT) {1717mqpcb->qp_enable = 1;1718mqpcb->qp_state = EHCA_QPS_INIT;1719update_mask = 0;1720update_mask = EHCA_BMASK_SET(MQPCB_MASK_QP_ENABLE, 1);17211722h_ret = hipz_h_modify_qp(shca->ipz_hca_handle,1723my_qp->ipz_qp_handle,1724&my_qp->pf,1725update_mask,1726mqpcb,1727my_qp->galpas.kernel);17281729if (h_ret != H_SUCCESS) {1730ret = ehca2ib_return_code(h_ret);1731ehca_err(ibqp->device, "ENABLE in context of "1732"RESET_2_INIT failed! Maybe you didn't get "1733"a LID h_ret=%lli ehca_qp=%p qp_num=%x",1734h_ret, my_qp, ibqp->qp_num);1735goto modify_qp_exit2;1736}1737}1738if ((qp_new_state == IB_QPS_ERR) && (qp_cur_state != IB_QPS_ERR)1739&& !is_user) {1740ret = check_for_left_cqes(my_qp, shca);1741if (ret)1742goto modify_qp_exit2;1743}17441745if (statetrans == IB_QPST_ANY2RESET) {1746ipz_qeit_reset(&my_qp->ipz_rqueue);1747ipz_qeit_reset(&my_qp->ipz_squeue);17481749if (qp_cur_state == IB_QPS_ERR && !is_user) {1750del_from_err_list(my_qp->send_cq, &my_qp->sq_err_node);17511752if (HAS_RQ(my_qp))1753del_from_err_list(my_qp->recv_cq,1754&my_qp->rq_err_node);1755}1756if (!is_user)1757reset_queue_map(&my_qp->sq_map);17581759if (HAS_RQ(my_qp) && !is_user)1760reset_queue_map(&my_qp->rq_map);1761}17621763if (attr_mask & IB_QP_QKEY)1764my_qp->qkey = attr->qkey;17651766modify_qp_exit2:1767if (squeue_locked) { /* this means: sqe -> rts */1768spin_unlock_irqrestore(&my_qp->spinlock_s, flags);1769my_qp->sqerr_purgeflag = 1;1770}17711772modify_qp_exit1:1773ehca_free_fw_ctrlblock(mqpcb);17741775return ret;1776}17771778int ehca_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr, int attr_mask,1779struct ib_udata *udata)1780{1781int ret = 0;17821783struct ehca_shca *shca = container_of(ibqp->device, struct ehca_shca,1784ib_device);1785struct ehca_qp *my_qp = container_of(ibqp, struct ehca_qp, ib_qp);17861787/* The if-block below caches qp_attr to be modified for GSI and SMI1788* qps during the initialization by ib_mad. When the respective port1789* is activated, ie we got an event PORT_ACTIVE, we'll replay the1790* cached modify calls sequence, see ehca_recover_sqs() below.1791* Why that is required:1792* 1) If one port is connected, older code requires that port one1793* to be connected and module option nr_ports=1 to be given by1794* user, which is very inconvenient for end user.1795* 2) Firmware accepts modify_qp() only if respective port has become1796* active. Older code had a wait loop of 30sec create_qp()/1797* define_aqp1(), which is not appropriate in practice. This1798* code now removes that wait loop, see define_aqp1(), and always1799* reports all ports to ib_mad resp. users. Only activated ports1800* will then usable for the users.1801*/1802if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI) {1803int port = my_qp->init_attr.port_num;1804struct ehca_sport *sport = &shca->sport[port - 1];1805unsigned long flags;1806spin_lock_irqsave(&sport->mod_sqp_lock, flags);1807/* cache qp_attr only during init */1808if (my_qp->mod_qp_parm) {1809struct ehca_mod_qp_parm *p;1810if (my_qp->mod_qp_parm_idx >= EHCA_MOD_QP_PARM_MAX) {1811ehca_err(&shca->ib_device,1812"mod_qp_parm overflow state=%x port=%x"1813" type=%x", attr->qp_state,1814my_qp->init_attr.port_num,1815ibqp->qp_type);1816spin_unlock_irqrestore(&sport->mod_sqp_lock,1817flags);1818return -EINVAL;1819}1820p = &my_qp->mod_qp_parm[my_qp->mod_qp_parm_idx];1821p->mask = attr_mask;1822p->attr = *attr;1823my_qp->mod_qp_parm_idx++;1824ehca_dbg(&shca->ib_device,1825"Saved qp_attr for state=%x port=%x type=%x",1826attr->qp_state, my_qp->init_attr.port_num,1827ibqp->qp_type);1828spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);1829goto out;1830}1831spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);1832}18331834ret = internal_modify_qp(ibqp, attr, attr_mask, 0);18351836out:1837if ((ret == 0) && (attr_mask & IB_QP_STATE))1838my_qp->state = attr->qp_state;18391840return ret;1841}18421843void ehca_recover_sqp(struct ib_qp *sqp)1844{1845struct ehca_qp *my_sqp = container_of(sqp, struct ehca_qp, ib_qp);1846int port = my_sqp->init_attr.port_num;1847struct ib_qp_attr attr;1848struct ehca_mod_qp_parm *qp_parm;1849int i, qp_parm_idx, ret;1850unsigned long flags, wr_cnt;18511852if (!my_sqp->mod_qp_parm)1853return;1854ehca_dbg(sqp->device, "SQP port=%x qp_num=%x", port, sqp->qp_num);18551856qp_parm = my_sqp->mod_qp_parm;1857qp_parm_idx = my_sqp->mod_qp_parm_idx;1858for (i = 0; i < qp_parm_idx; i++) {1859attr = qp_parm[i].attr;1860ret = internal_modify_qp(sqp, &attr, qp_parm[i].mask, 0);1861if (ret) {1862ehca_err(sqp->device, "Could not modify SQP port=%x "1863"qp_num=%x ret=%x", port, sqp->qp_num, ret);1864goto free_qp_parm;1865}1866ehca_dbg(sqp->device, "SQP port=%x qp_num=%x in state=%x",1867port, sqp->qp_num, attr.qp_state);1868}18691870/* re-trigger posted recv wrs */1871wr_cnt = my_sqp->ipz_rqueue.current_q_offset /1872my_sqp->ipz_rqueue.qe_size;1873if (wr_cnt) {1874spin_lock_irqsave(&my_sqp->spinlock_r, flags);1875hipz_update_rqa(my_sqp, wr_cnt);1876spin_unlock_irqrestore(&my_sqp->spinlock_r, flags);1877ehca_dbg(sqp->device, "doorbell port=%x qp_num=%x wr_cnt=%lx",1878port, sqp->qp_num, wr_cnt);1879}18801881free_qp_parm:1882kfree(qp_parm);1883/* this prevents subsequent calls to modify_qp() to cache qp_attr */1884my_sqp->mod_qp_parm = NULL;1885}18861887int ehca_query_qp(struct ib_qp *qp,1888struct ib_qp_attr *qp_attr,1889int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)1890{1891struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);1892struct ehca_shca *shca = container_of(qp->device, struct ehca_shca,1893ib_device);1894struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;1895struct hcp_modify_qp_control_block *qpcb;1896int cnt, ret = 0;1897u64 h_ret;18981899if (qp_attr_mask & QP_ATTR_QUERY_NOT_SUPPORTED) {1900ehca_err(qp->device, "Invalid attribute mask "1901"ehca_qp=%p qp_num=%x qp_attr_mask=%x ",1902my_qp, qp->qp_num, qp_attr_mask);1903return -EINVAL;1904}19051906qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);1907if (!qpcb) {1908ehca_err(qp->device, "Out of memory for qpcb "1909"ehca_qp=%p qp_num=%x", my_qp, qp->qp_num);1910return -ENOMEM;1911}19121913h_ret = hipz_h_query_qp(adapter_handle,1914my_qp->ipz_qp_handle,1915&my_qp->pf,1916qpcb, my_qp->galpas.kernel);19171918if (h_ret != H_SUCCESS) {1919ret = ehca2ib_return_code(h_ret);1920ehca_err(qp->device, "hipz_h_query_qp() failed "1921"ehca_qp=%p qp_num=%x h_ret=%lli",1922my_qp, qp->qp_num, h_ret);1923goto query_qp_exit1;1924}19251926qp_attr->cur_qp_state = ehca2ib_qp_state(qpcb->qp_state);1927qp_attr->qp_state = qp_attr->cur_qp_state;19281929if (qp_attr->cur_qp_state == -EINVAL) {1930ret = -EINVAL;1931ehca_err(qp->device, "Got invalid ehca_qp_state=%x "1932"ehca_qp=%p qp_num=%x",1933qpcb->qp_state, my_qp, qp->qp_num);1934goto query_qp_exit1;1935}19361937if (qp_attr->qp_state == IB_QPS_SQD)1938qp_attr->sq_draining = 1;19391940qp_attr->qkey = qpcb->qkey;1941qp_attr->path_mtu = qpcb->path_mtu;1942qp_attr->path_mig_state = qpcb->path_migration_state - 1;1943qp_attr->rq_psn = qpcb->receive_psn;1944qp_attr->sq_psn = qpcb->send_psn;1945qp_attr->min_rnr_timer = qpcb->min_rnr_nak_timer_field;1946qp_attr->cap.max_send_wr = qpcb->max_nr_outst_send_wr-1;1947qp_attr->cap.max_recv_wr = qpcb->max_nr_outst_recv_wr-1;1948/* UD_AV CIRCUMVENTION */1949if (my_qp->qp_type == IB_QPT_UD) {1950qp_attr->cap.max_send_sge =1951qpcb->actual_nr_sges_in_sq_wqe - 2;1952qp_attr->cap.max_recv_sge =1953qpcb->actual_nr_sges_in_rq_wqe - 2;1954} else {1955qp_attr->cap.max_send_sge =1956qpcb->actual_nr_sges_in_sq_wqe;1957qp_attr->cap.max_recv_sge =1958qpcb->actual_nr_sges_in_rq_wqe;1959}19601961qp_attr->cap.max_inline_data = my_qp->sq_max_inline_data_size;1962qp_attr->dest_qp_num = qpcb->dest_qp_nr;19631964qp_attr->pkey_index = qpcb->prim_p_key_idx;1965qp_attr->port_num = qpcb->prim_phys_port;1966qp_attr->timeout = qpcb->timeout;1967qp_attr->retry_cnt = qpcb->retry_count;1968qp_attr->rnr_retry = qpcb->rnr_retry_count;19691970qp_attr->alt_pkey_index = qpcb->alt_p_key_idx;1971qp_attr->alt_port_num = qpcb->alt_phys_port;1972qp_attr->alt_timeout = qpcb->timeout_al;19731974qp_attr->max_dest_rd_atomic = qpcb->rdma_nr_atomic_resp_res;1975qp_attr->max_rd_atomic = qpcb->rdma_atomic_outst_dest_qp;19761977/* primary av */1978qp_attr->ah_attr.sl = qpcb->service_level;19791980if (qpcb->send_grh_flag) {1981qp_attr->ah_attr.ah_flags = IB_AH_GRH;1982}19831984qp_attr->ah_attr.static_rate = qpcb->max_static_rate;1985qp_attr->ah_attr.dlid = qpcb->dlid;1986qp_attr->ah_attr.src_path_bits = qpcb->source_path_bits;1987qp_attr->ah_attr.port_num = qp_attr->port_num;19881989/* primary GRH */1990qp_attr->ah_attr.grh.traffic_class = qpcb->traffic_class;1991qp_attr->ah_attr.grh.hop_limit = qpcb->hop_limit;1992qp_attr->ah_attr.grh.sgid_index = qpcb->source_gid_idx;1993qp_attr->ah_attr.grh.flow_label = qpcb->flow_label;19941995for (cnt = 0; cnt < 16; cnt++)1996qp_attr->ah_attr.grh.dgid.raw[cnt] =1997qpcb->dest_gid.byte[cnt];19981999/* alternate AV */2000qp_attr->alt_ah_attr.sl = qpcb->service_level_al;2001if (qpcb->send_grh_flag_al) {2002qp_attr->alt_ah_attr.ah_flags = IB_AH_GRH;2003}20042005qp_attr->alt_ah_attr.static_rate = qpcb->max_static_rate_al;2006qp_attr->alt_ah_attr.dlid = qpcb->dlid_al;2007qp_attr->alt_ah_attr.src_path_bits = qpcb->source_path_bits_al;20082009/* alternate GRH */2010qp_attr->alt_ah_attr.grh.traffic_class = qpcb->traffic_class_al;2011qp_attr->alt_ah_attr.grh.hop_limit = qpcb->hop_limit_al;2012qp_attr->alt_ah_attr.grh.sgid_index = qpcb->source_gid_idx_al;2013qp_attr->alt_ah_attr.grh.flow_label = qpcb->flow_label_al;20142015for (cnt = 0; cnt < 16; cnt++)2016qp_attr->alt_ah_attr.grh.dgid.raw[cnt] =2017qpcb->dest_gid_al.byte[cnt];20182019/* return init attributes given in ehca_create_qp */2020if (qp_init_attr)2021*qp_init_attr = my_qp->init_attr;20222023if (ehca_debug_level >= 2)2024ehca_dmp(qpcb, 4*70, "qp_num=%x", qp->qp_num);20252026query_qp_exit1:2027ehca_free_fw_ctrlblock(qpcb);20282029return ret;2030}20312032int ehca_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,2033enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)2034{2035struct ehca_qp *my_qp =2036container_of(ibsrq, struct ehca_qp, ib_srq);2037struct ehca_shca *shca =2038container_of(ibsrq->pd->device, struct ehca_shca, ib_device);2039struct hcp_modify_qp_control_block *mqpcb;2040u64 update_mask;2041u64 h_ret;2042int ret = 0;20432044mqpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);2045if (!mqpcb) {2046ehca_err(ibsrq->device, "Could not get zeroed page for mqpcb "2047"ehca_qp=%p qp_num=%x ", my_qp, my_qp->real_qp_num);2048return -ENOMEM;2049}20502051update_mask = 0;2052if (attr_mask & IB_SRQ_LIMIT) {2053attr_mask &= ~IB_SRQ_LIMIT;2054update_mask |=2055EHCA_BMASK_SET(MQPCB_MASK_CURR_SRQ_LIMIT, 1)2056| EHCA_BMASK_SET(MQPCB_MASK_QP_AFF_ASYN_EV_LOG_REG, 1);2057mqpcb->curr_srq_limit = attr->srq_limit;2058mqpcb->qp_aff_asyn_ev_log_reg =2059EHCA_BMASK_SET(QPX_AAELOG_RESET_SRQ_LIMIT, 1);2060}20612062/* by now, all bits in attr_mask should have been cleared */2063if (attr_mask) {2064ehca_err(ibsrq->device, "invalid attribute mask bits set "2065"attr_mask=%x", attr_mask);2066ret = -EINVAL;2067goto modify_srq_exit0;2068}20692070if (ehca_debug_level >= 2)2071ehca_dmp(mqpcb, 4*70, "qp_num=%x", my_qp->real_qp_num);20722073h_ret = hipz_h_modify_qp(shca->ipz_hca_handle, my_qp->ipz_qp_handle,2074NULL, update_mask, mqpcb,2075my_qp->galpas.kernel);20762077if (h_ret != H_SUCCESS) {2078ret = ehca2ib_return_code(h_ret);2079ehca_err(ibsrq->device, "hipz_h_modify_qp() failed h_ret=%lli "2080"ehca_qp=%p qp_num=%x",2081h_ret, my_qp, my_qp->real_qp_num);2082}20832084modify_srq_exit0:2085ehca_free_fw_ctrlblock(mqpcb);20862087return ret;2088}20892090int ehca_query_srq(struct ib_srq *srq, struct ib_srq_attr *srq_attr)2091{2092struct ehca_qp *my_qp = container_of(srq, struct ehca_qp, ib_srq);2093struct ehca_shca *shca = container_of(srq->device, struct ehca_shca,2094ib_device);2095struct ipz_adapter_handle adapter_handle = shca->ipz_hca_handle;2096struct hcp_modify_qp_control_block *qpcb;2097int ret = 0;2098u64 h_ret;20992100qpcb = ehca_alloc_fw_ctrlblock(GFP_KERNEL);2101if (!qpcb) {2102ehca_err(srq->device, "Out of memory for qpcb "2103"ehca_qp=%p qp_num=%x", my_qp, my_qp->real_qp_num);2104return -ENOMEM;2105}21062107h_ret = hipz_h_query_qp(adapter_handle, my_qp->ipz_qp_handle,2108NULL, qpcb, my_qp->galpas.kernel);21092110if (h_ret != H_SUCCESS) {2111ret = ehca2ib_return_code(h_ret);2112ehca_err(srq->device, "hipz_h_query_qp() failed "2113"ehca_qp=%p qp_num=%x h_ret=%lli",2114my_qp, my_qp->real_qp_num, h_ret);2115goto query_srq_exit1;2116}21172118srq_attr->max_wr = qpcb->max_nr_outst_recv_wr - 1;2119srq_attr->max_sge = 3;2120srq_attr->srq_limit = qpcb->curr_srq_limit;21212122if (ehca_debug_level >= 2)2123ehca_dmp(qpcb, 4*70, "qp_num=%x", my_qp->real_qp_num);21242125query_srq_exit1:2126ehca_free_fw_ctrlblock(qpcb);21272128return ret;2129}21302131static int internal_destroy_qp(struct ib_device *dev, struct ehca_qp *my_qp,2132struct ib_uobject *uobject)2133{2134struct ehca_shca *shca = container_of(dev, struct ehca_shca, ib_device);2135struct ehca_pd *my_pd = container_of(my_qp->ib_qp.pd, struct ehca_pd,2136ib_pd);2137struct ehca_sport *sport = &shca->sport[my_qp->init_attr.port_num - 1];2138u32 qp_num = my_qp->real_qp_num;2139int ret;2140u64 h_ret;2141u8 port_num;2142int is_user = 0;2143enum ib_qp_type qp_type;2144unsigned long flags;21452146if (uobject) {2147is_user = 1;2148if (my_qp->mm_count_galpa ||2149my_qp->mm_count_rqueue || my_qp->mm_count_squeue) {2150ehca_err(dev, "Resources still referenced in "2151"user space qp_num=%x", qp_num);2152return -EINVAL;2153}2154}21552156if (my_qp->send_cq) {2157ret = ehca_cq_unassign_qp(my_qp->send_cq, qp_num);2158if (ret) {2159ehca_err(dev, "Couldn't unassign qp from "2160"send_cq ret=%i qp_num=%x cq_num=%x", ret,2161qp_num, my_qp->send_cq->cq_number);2162return ret;2163}2164}21652166write_lock_irqsave(&ehca_qp_idr_lock, flags);2167idr_remove(&ehca_qp_idr, my_qp->token);2168write_unlock_irqrestore(&ehca_qp_idr_lock, flags);21692170/*2171* SRQs will never get into an error list and do not have a recv_cq,2172* so we need to skip them here.2173*/2174if (HAS_RQ(my_qp) && !IS_SRQ(my_qp) && !is_user)2175del_from_err_list(my_qp->recv_cq, &my_qp->rq_err_node);21762177if (HAS_SQ(my_qp) && !is_user)2178del_from_err_list(my_qp->send_cq, &my_qp->sq_err_node);21792180/* now wait until all pending events have completed */2181wait_event(my_qp->wait_completion, !atomic_read(&my_qp->nr_events));21822183h_ret = hipz_h_destroy_qp(shca->ipz_hca_handle, my_qp);2184if (h_ret != H_SUCCESS) {2185ehca_err(dev, "hipz_h_destroy_qp() failed h_ret=%lli "2186"ehca_qp=%p qp_num=%x", h_ret, my_qp, qp_num);2187return ehca2ib_return_code(h_ret);2188}21892190port_num = my_qp->init_attr.port_num;2191qp_type = my_qp->init_attr.qp_type;21922193if (qp_type == IB_QPT_SMI || qp_type == IB_QPT_GSI) {2194spin_lock_irqsave(&sport->mod_sqp_lock, flags);2195kfree(my_qp->mod_qp_parm);2196my_qp->mod_qp_parm = NULL;2197shca->sport[port_num - 1].ibqp_sqp[qp_type] = NULL;2198spin_unlock_irqrestore(&sport->mod_sqp_lock, flags);2199}22002201/* no support for IB_QPT_SMI yet */2202if (qp_type == IB_QPT_GSI) {2203struct ib_event event;2204ehca_info(dev, "device %s: port %x is inactive.",2205shca->ib_device.name, port_num);2206event.device = &shca->ib_device;2207event.event = IB_EVENT_PORT_ERR;2208event.element.port_num = port_num;2209shca->sport[port_num - 1].port_state = IB_PORT_DOWN;2210ib_dispatch_event(&event);2211}22122213if (HAS_RQ(my_qp)) {2214ipz_queue_dtor(my_pd, &my_qp->ipz_rqueue);2215if (!is_user)2216vfree(my_qp->rq_map.map);2217}2218if (HAS_SQ(my_qp)) {2219ipz_queue_dtor(my_pd, &my_qp->ipz_squeue);2220if (!is_user)2221vfree(my_qp->sq_map.map);2222}2223kmem_cache_free(qp_cache, my_qp);2224atomic_dec(&shca->num_qps);2225return 0;2226}22272228int ehca_destroy_qp(struct ib_qp *qp)2229{2230return internal_destroy_qp(qp->device,2231container_of(qp, struct ehca_qp, ib_qp),2232qp->uobject);2233}22342235int ehca_destroy_srq(struct ib_srq *srq)2236{2237return internal_destroy_qp(srq->device,2238container_of(srq, struct ehca_qp, ib_srq),2239srq->uobject);2240}22412242int ehca_init_qp_cache(void)2243{2244qp_cache = kmem_cache_create("ehca_cache_qp",2245sizeof(struct ehca_qp), 0,2246SLAB_HWCACHE_ALIGN,2247NULL);2248if (!qp_cache)2249return -ENOMEM;2250return 0;2251}22522253void ehca_cleanup_qp_cache(void)2254{2255if (qp_cache)2256kmem_cache_destroy(qp_cache);2257}225822592260