Path: blob/main/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_verbs.c
39566 views
/*-1* SPDX-License-Identifier: BSD-2-Clause OR GPL-2.02*3* Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.4* Copyright (c) 2005 Mellanox Technologies. All rights reserved.5*6* This software is available to you under a choice of one of two7* licenses. You may choose to be licensed under the terms of the GNU8* General Public License (GPL) Version 2, available from the file9* COPYING in the main directory of this source tree, or the10* OpenIB.org BSD license below:11*12* Redistribution and use in source and binary forms, with or13* without modification, are permitted provided that the following14* conditions are met:15*16* - Redistributions of source code must retain the above17* copyright notice, this list of conditions and the following18* disclaimer.19*20* - Redistributions in binary form must reproduce the above21* copyright notice, this list of conditions and the following22* disclaimer in the documentation and/or other materials23* provided with the distribution.24*25* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,26* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF27* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND28* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS29* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN30* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN31* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE32* SOFTWARE.33*/3435#include <sys/cdefs.h>36#include "ipoib.h"3738int ipoib_mcast_attach(struct ipoib_dev_priv *priv, u16 mlid, union ib_gid *mgid, int set_qkey)39{40struct ib_qp_attr *qp_attr = NULL;41int ret;42u16 pkey_index;4344if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index)) {45clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);46ret = -ENXIO;47goto out;48}49set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);5051if (set_qkey) {52ret = -ENOMEM;53qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);54if (!qp_attr)55goto out;5657/* set correct QKey for QP */58qp_attr->qkey = priv->qkey;59ret = ib_modify_qp(priv->qp, qp_attr, IB_QP_QKEY);60if (ret) {61ipoib_warn(priv, "failed to modify QP, ret = %d\n", ret);62goto out;63}64}6566/* attach QP to multicast group */67ret = ib_attach_mcast(priv->qp, mgid, mlid);68if (ret)69ipoib_warn(priv, "failed to attach to multicast group, ret = %d\n", ret);7071out:72kfree(qp_attr);73return ret;74}7576int ipoib_init_qp(struct ipoib_dev_priv *priv)77{78int ret;79struct ib_qp_attr qp_attr;80int attr_mask;8182if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))83return -1;8485qp_attr.qp_state = IB_QPS_INIT;86qp_attr.qkey = 0;87qp_attr.port_num = priv->port;88qp_attr.pkey_index = priv->pkey_index;89attr_mask =90IB_QP_QKEY |91IB_QP_PORT |92IB_QP_PKEY_INDEX |93IB_QP_STATE;9495ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);96if (ret) {97ipoib_warn(priv, "failed to modify QP to init, ret = %d\n", ret);98goto out_fail;99}100101qp_attr.qp_state = IB_QPS_RTR;102/* Can't set this in a INIT->RTR transition */103attr_mask &= ~IB_QP_PORT;104ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);105if (ret) {106ipoib_warn(priv, "failed to modify QP to RTR, ret = %d\n", ret);107goto out_fail;108}109110qp_attr.qp_state = IB_QPS_RTS;111qp_attr.sq_psn = 0;112attr_mask |= IB_QP_SQ_PSN;113attr_mask &= ~IB_QP_PKEY_INDEX;114ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);115if (ret) {116ipoib_warn(priv, "failed to modify QP to RTS, ret = %d\n", ret);117goto out_fail;118}119120return 0;121122out_fail:123qp_attr.qp_state = IB_QPS_RESET;124if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))125ipoib_warn(priv, "Failed to modify QP to RESET state\n");126127return ret;128}129130int ipoib_transport_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca)131{132struct ib_qp_init_attr init_attr = {133.cap = {134.max_send_wr = ipoib_sendq_size,135.max_recv_wr = ipoib_recvq_size,136.max_send_sge = 1,137.max_recv_sge = IPOIB_UD_RX_SG138},139.sq_sig_type = IB_SIGNAL_ALL_WR,140.qp_type = IB_QPT_UD141};142struct ib_cq_init_attr cq_attr = {};143caddr_t lla;144145int ret, size;146int i;147/* XXX struct ethtool_coalesce *coal; */148149priv->pd = ib_alloc_pd(priv->ca, 0);150if (IS_ERR(priv->pd)) {151printk(KERN_WARNING "%s: failed to allocate PD\n", ca->name);152return -ENODEV;153}154155size = ipoib_recvq_size + 1;156ret = ipoib_cm_dev_init(priv);157if (!ret) {158size += ipoib_sendq_size;159if (ipoib_cm_has_srq(priv))160size += ipoib_recvq_size + 1; /* 1 extra for rx_drain_qp */161else162size += ipoib_recvq_size * ipoib_max_conn_qp;163}164165cq_attr.cqe = size;166priv->recv_cq = ib_create_cq(priv->ca, ipoib_ib_completion, NULL, priv, &cq_attr);167if (IS_ERR(priv->recv_cq)) {168printk(KERN_WARNING "%s: failed to create receive CQ\n", ca->name);169goto out_free_mr;170}171172cq_attr.cqe = ipoib_sendq_size;173priv->send_cq = ib_create_cq(priv->ca, ipoib_send_comp_handler, NULL,174priv, &cq_attr);175if (IS_ERR(priv->send_cq)) {176printk(KERN_WARNING "%s: failed to create send CQ\n", ca->name);177goto out_free_recv_cq;178}179180if (ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP))181goto out_free_send_cq;182183#if 0184/* XXX */185coal = kzalloc(sizeof *coal, GFP_KERNEL);186if (coal) {187coal->rx_coalesce_usecs = 10;188coal->tx_coalesce_usecs = 10;189coal->rx_max_coalesced_frames = 16;190coal->tx_max_coalesced_frames = 16;191dev->ethtool_ops->set_coalesce(dev, coal);192kfree(coal);193}194#endif195196init_attr.send_cq = priv->send_cq;197init_attr.recv_cq = priv->recv_cq;198199if (priv->hca_caps & IB_DEVICE_UD_TSO)200init_attr.create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;201202if (priv->hca_caps & IB_DEVICE_BLOCK_MULTICAST_LOOPBACK)203init_attr.create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;204205init_attr.cap.max_send_sge = IPOIB_UD_TX_SG;206207priv->qp = ib_create_qp(priv->pd, &init_attr);208if (IS_ERR(priv->qp)) {209printk(KERN_WARNING "%s: failed to create QP\n", ca->name);210goto out_free_send_cq;211}212213lla = if_getlladdr(priv->dev);214lla[1] = (priv->qp->qp_num >> 16) & 0xff;215lla[2] = (priv->qp->qp_num >> 8) & 0xff;216lla[3] = (priv->qp->qp_num ) & 0xff;217218for (i = 0; i < IPOIB_MAX_TX_SG; ++i)219priv->tx_sge[i].lkey = priv->pd->local_dma_lkey;220221priv->tx_wr.wr.opcode = IB_WR_SEND;222priv->tx_wr.wr.sg_list = priv->tx_sge;223priv->tx_wr.wr.send_flags = IB_SEND_SIGNALED;224225for (i = 0; i < IPOIB_UD_RX_SG; ++i)226priv->rx_sge[i].lkey = priv->pd->local_dma_lkey;227priv->rx_wr.next = NULL;228priv->rx_wr.sg_list = priv->rx_sge;229230return 0;231232out_free_send_cq:233ib_destroy_cq(priv->send_cq);234235out_free_recv_cq:236ib_destroy_cq(priv->recv_cq);237238out_free_mr:239ipoib_cm_dev_cleanup(priv);240241ib_dealloc_pd(priv->pd);242return -ENODEV;243}244245void ipoib_transport_dev_cleanup(struct ipoib_dev_priv *priv)246{247248if (priv->qp) {249if (ib_destroy_qp(priv->qp))250ipoib_warn(priv, "ib_qp_destroy failed\n");251252priv->qp = NULL;253clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);254}255256ib_destroy_cq(priv->send_cq);257258ib_destroy_cq(priv->recv_cq);259260ipoib_cm_dev_cleanup(priv);261262ib_dealloc_pd(priv->pd);263}264265void ipoib_event(struct ib_event_handler *handler,266struct ib_event *record)267{268struct ipoib_dev_priv *priv =269container_of(handler, struct ipoib_dev_priv, event_handler);270271if (record->element.port_num != priv->port)272return;273274ipoib_dbg(priv, "Event %d on device %s port %d\n", record->event,275record->device->name, record->element.port_num);276277if (record->event == IB_EVENT_SM_CHANGE ||278record->event == IB_EVENT_CLIENT_REREGISTER) {279queue_work(ipoib_workqueue, &priv->flush_light);280} else if (record->event == IB_EVENT_PORT_ERR ||281record->event == IB_EVENT_PORT_ACTIVE ||282record->event == IB_EVENT_LID_CHANGE ||283record->event == IB_EVENT_DEVICE_FATAL) {284queue_work(ipoib_workqueue, &priv->flush_normal);285} else if (record->event == IB_EVENT_PKEY_CHANGE) {286queue_work(ipoib_workqueue, &priv->flush_heavy);287}288}289290291