Path: blob/master/drivers/infiniband/ulp/ipoib/ipoib_verbs.c
15112 views
/*1* Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.2* Copyright (c) 2005 Mellanox Technologies. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* 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 OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/slab.h>3435#include "ipoib.h"3637int ipoib_mcast_attach(struct net_device *dev, u16 mlid, union ib_gid *mgid, int set_qkey)38{39struct ipoib_dev_priv *priv = netdev_priv(dev);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 net_device *dev)77{78struct ipoib_dev_priv *priv = netdev_priv(dev);79int ret;80struct ib_qp_attr qp_attr;81int attr_mask;8283if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))84return -1;8586qp_attr.qp_state = IB_QPS_INIT;87qp_attr.qkey = 0;88qp_attr.port_num = priv->port;89qp_attr.pkey_index = priv->pkey_index;90attr_mask =91IB_QP_QKEY |92IB_QP_PORT |93IB_QP_PKEY_INDEX |94IB_QP_STATE;95ret = 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 net_device *dev, struct ib_device *ca)131{132struct ipoib_dev_priv *priv = netdev_priv(dev);133struct ib_qp_init_attr init_attr = {134.cap = {135.max_send_wr = ipoib_sendq_size,136.max_recv_wr = ipoib_recvq_size,137.max_send_sge = 1,138.max_recv_sge = IPOIB_UD_RX_SG139},140.sq_sig_type = IB_SIGNAL_ALL_WR,141.qp_type = IB_QPT_UD142};143144int ret, size;145int i;146147priv->pd = ib_alloc_pd(priv->ca);148if (IS_ERR(priv->pd)) {149printk(KERN_WARNING "%s: failed to allocate PD\n", ca->name);150return -ENODEV;151}152153priv->mr = ib_get_dma_mr(priv->pd, IB_ACCESS_LOCAL_WRITE);154if (IS_ERR(priv->mr)) {155printk(KERN_WARNING "%s: ib_get_dma_mr failed\n", ca->name);156goto out_free_pd;157}158159size = ipoib_recvq_size + 1;160ret = ipoib_cm_dev_init(dev);161if (!ret) {162size += ipoib_sendq_size;163if (ipoib_cm_has_srq(dev))164size += ipoib_recvq_size + 1; /* 1 extra for rx_drain_qp */165else166size += ipoib_recvq_size * ipoib_max_conn_qp;167}168169priv->recv_cq = ib_create_cq(priv->ca, ipoib_ib_completion, NULL, dev, size, 0);170if (IS_ERR(priv->recv_cq)) {171printk(KERN_WARNING "%s: failed to create receive CQ\n", ca->name);172goto out_free_mr;173}174175priv->send_cq = ib_create_cq(priv->ca, ipoib_send_comp_handler, NULL,176dev, ipoib_sendq_size, 0);177if (IS_ERR(priv->send_cq)) {178printk(KERN_WARNING "%s: failed to create send CQ\n", ca->name);179goto out_free_recv_cq;180}181182if (ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP))183goto out_free_send_cq;184185init_attr.send_cq = priv->send_cq;186init_attr.recv_cq = priv->recv_cq;187188if (priv->hca_caps & IB_DEVICE_UD_TSO)189init_attr.create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;190191if (priv->hca_caps & IB_DEVICE_BLOCK_MULTICAST_LOOPBACK)192init_attr.create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;193194if (dev->features & NETIF_F_SG)195init_attr.cap.max_send_sge = MAX_SKB_FRAGS + 1;196197priv->qp = ib_create_qp(priv->pd, &init_attr);198if (IS_ERR(priv->qp)) {199printk(KERN_WARNING "%s: failed to create QP\n", ca->name);200goto out_free_send_cq;201}202203priv->dev->dev_addr[1] = (priv->qp->qp_num >> 16) & 0xff;204priv->dev->dev_addr[2] = (priv->qp->qp_num >> 8) & 0xff;205priv->dev->dev_addr[3] = (priv->qp->qp_num ) & 0xff;206207for (i = 0; i < MAX_SKB_FRAGS + 1; ++i)208priv->tx_sge[i].lkey = priv->mr->lkey;209210priv->tx_wr.opcode = IB_WR_SEND;211priv->tx_wr.sg_list = priv->tx_sge;212priv->tx_wr.send_flags = IB_SEND_SIGNALED;213214priv->rx_sge[0].lkey = priv->mr->lkey;215if (ipoib_ud_need_sg(priv->max_ib_mtu)) {216priv->rx_sge[0].length = IPOIB_UD_HEAD_SIZE;217priv->rx_sge[1].length = PAGE_SIZE;218priv->rx_sge[1].lkey = priv->mr->lkey;219priv->rx_wr.num_sge = IPOIB_UD_RX_SG;220} else {221priv->rx_sge[0].length = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);222priv->rx_wr.num_sge = 1;223}224priv->rx_wr.next = NULL;225priv->rx_wr.sg_list = priv->rx_sge;226227return 0;228229out_free_send_cq:230ib_destroy_cq(priv->send_cq);231232out_free_recv_cq:233ib_destroy_cq(priv->recv_cq);234235out_free_mr:236ib_dereg_mr(priv->mr);237ipoib_cm_dev_cleanup(dev);238239out_free_pd:240ib_dealloc_pd(priv->pd);241return -ENODEV;242}243244void ipoib_transport_dev_cleanup(struct net_device *dev)245{246struct ipoib_dev_priv *priv = netdev_priv(dev);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}255256if (ib_destroy_cq(priv->send_cq))257ipoib_warn(priv, "ib_cq_destroy (send) failed\n");258259if (ib_destroy_cq(priv->recv_cq))260ipoib_warn(priv, "ib_cq_destroy (recv) failed\n");261262ipoib_cm_dev_cleanup(dev);263264if (ib_dereg_mr(priv->mr))265ipoib_warn(priv, "ib_dereg_mr failed\n");266267if (ib_dealloc_pd(priv->pd))268ipoib_warn(priv, "ib_dealloc_pd failed\n");269}270271void ipoib_event(struct ib_event_handler *handler,272struct ib_event *record)273{274struct ipoib_dev_priv *priv =275container_of(handler, struct ipoib_dev_priv, event_handler);276277if (record->element.port_num != priv->port)278return;279280ipoib_dbg(priv, "Event %d on device %s port %d\n", record->event,281record->device->name, record->element.port_num);282283if (record->event == IB_EVENT_SM_CHANGE ||284record->event == IB_EVENT_CLIENT_REREGISTER) {285queue_work(ipoib_workqueue, &priv->flush_light);286} else if (record->event == IB_EVENT_PORT_ERR ||287record->event == IB_EVENT_PORT_ACTIVE ||288record->event == IB_EVENT_LID_CHANGE) {289queue_work(ipoib_workqueue, &priv->flush_normal);290} else if (record->event == IB_EVENT_PKEY_CHANGE) {291queue_work(ipoib_workqueue, &priv->flush_heavy);292}293}294295296