Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
0003  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
0004  *
0005  * This software is available to you under a choice of one of two
0006  * licenses.  You may choose to be licensed under the terms of the GNU
0007  * General Public License (GPL) Version 2, available from the file
0008  * COPYING in the main directory of this source tree, or the
0009  * OpenIB.org BSD license below:
0010  *
0011  *     Redistribution and use in source and binary forms, with or
0012  *     without modification, are permitted provided that the following
0013  *     conditions are met:
0014  *
0015  *      - Redistributions of source code must retain the above
0016  *        copyright notice, this list of conditions and the following
0017  *        disclaimer.
0018  *
0019  *      - Redistributions in binary form must reproduce the above
0020  *        copyright notice, this list of conditions and the following
0021  *        disclaimer in the documentation and/or other materials
0022  *        provided with the distribution.
0023  *
0024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0026  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0028  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0029  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0030  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0031  * SOFTWARE.
0032  */
0033 
0034 #include <linux/slab.h>
0035 
0036 #include "ipoib.h"
0037 
0038 int ipoib_mcast_attach(struct net_device *dev, struct ib_device *hca,
0039                union ib_gid *mgid, u16 mlid, int set_qkey, u32 qkey)
0040 {
0041     struct ipoib_dev_priv *priv = ipoib_priv(dev);
0042     struct ib_qp_attr *qp_attr = NULL;
0043     int ret;
0044     u16 pkey_index;
0045 
0046     if (ib_find_pkey(priv->ca, priv->port, priv->pkey, &pkey_index)) {
0047         clear_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
0048         ret = -ENXIO;
0049         goto out;
0050     }
0051     set_bit(IPOIB_PKEY_ASSIGNED, &priv->flags);
0052 
0053     if (set_qkey) {
0054         ret = -ENOMEM;
0055         qp_attr = kmalloc(sizeof(*qp_attr), GFP_KERNEL);
0056         if (!qp_attr)
0057             goto out;
0058 
0059         /* set correct QKey for QP */
0060         qp_attr->qkey = qkey;
0061         ret = ib_modify_qp(priv->qp, qp_attr, IB_QP_QKEY);
0062         if (ret) {
0063             ipoib_warn(priv, "failed to modify QP, ret = %d\n", ret);
0064             goto out;
0065         }
0066     }
0067 
0068     /* attach QP to multicast group */
0069     ret = ib_attach_mcast(priv->qp, mgid, mlid);
0070     if (ret)
0071         ipoib_warn(priv, "failed to attach to multicast group, ret = %d\n", ret);
0072 
0073 out:
0074     kfree(qp_attr);
0075     return ret;
0076 }
0077 
0078 int ipoib_mcast_detach(struct net_device *dev, struct ib_device *hca,
0079                union ib_gid *mgid, u16 mlid)
0080 {
0081     struct ipoib_dev_priv *priv = ipoib_priv(dev);
0082     int ret;
0083 
0084     ret = ib_detach_mcast(priv->qp, mgid, mlid);
0085 
0086     return ret;
0087 }
0088 
0089 int ipoib_init_qp(struct net_device *dev)
0090 {
0091     struct ipoib_dev_priv *priv = ipoib_priv(dev);
0092     int ret;
0093     struct ib_qp_attr qp_attr;
0094     int attr_mask;
0095 
0096     if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
0097         return -1;
0098 
0099     qp_attr.qp_state = IB_QPS_INIT;
0100     qp_attr.qkey = 0;
0101     qp_attr.port_num = priv->port;
0102     qp_attr.pkey_index = priv->pkey_index;
0103     attr_mask =
0104         IB_QP_QKEY |
0105         IB_QP_PORT |
0106         IB_QP_PKEY_INDEX |
0107         IB_QP_STATE;
0108     ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);
0109     if (ret) {
0110         ipoib_warn(priv, "failed to modify QP to init, ret = %d\n", ret);
0111         goto out_fail;
0112     }
0113 
0114     qp_attr.qp_state = IB_QPS_RTR;
0115     /* Can't set this in a INIT->RTR transition */
0116     attr_mask &= ~IB_QP_PORT;
0117     ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);
0118     if (ret) {
0119         ipoib_warn(priv, "failed to modify QP to RTR, ret = %d\n", ret);
0120         goto out_fail;
0121     }
0122 
0123     qp_attr.qp_state = IB_QPS_RTS;
0124     qp_attr.sq_psn = 0;
0125     attr_mask |= IB_QP_SQ_PSN;
0126     attr_mask &= ~IB_QP_PKEY_INDEX;
0127     ret = ib_modify_qp(priv->qp, &qp_attr, attr_mask);
0128     if (ret) {
0129         ipoib_warn(priv, "failed to modify QP to RTS, ret = %d\n", ret);
0130         goto out_fail;
0131     }
0132 
0133     return 0;
0134 
0135 out_fail:
0136     qp_attr.qp_state = IB_QPS_RESET;
0137     if (ib_modify_qp(priv->qp, &qp_attr, IB_QP_STATE))
0138         ipoib_warn(priv, "Failed to modify QP to RESET state\n");
0139 
0140     return ret;
0141 }
0142 
0143 int ipoib_transport_dev_init(struct net_device *dev, struct ib_device *ca)
0144 {
0145     struct ipoib_dev_priv *priv = ipoib_priv(dev);
0146     struct ib_qp_init_attr init_attr = {
0147         .cap = {
0148             .max_send_wr  = ipoib_sendq_size,
0149             .max_recv_wr  = ipoib_recvq_size,
0150             .max_send_sge = min_t(u32, priv->ca->attrs.max_send_sge,
0151                           MAX_SKB_FRAGS + 1),
0152             .max_recv_sge = IPOIB_UD_RX_SG
0153         },
0154         .sq_sig_type = IB_SIGNAL_ALL_WR,
0155         .qp_type     = IB_QPT_UD
0156     };
0157     struct ib_cq_init_attr cq_attr = {};
0158 
0159     int ret, size, req_vec;
0160     int i;
0161     static atomic_t counter;
0162 
0163     size = ipoib_recvq_size + 1;
0164     ret = ipoib_cm_dev_init(dev);
0165     if (!ret) {
0166         size += ipoib_sendq_size;
0167         if (ipoib_cm_has_srq(dev))
0168             size += ipoib_recvq_size + 1; /* 1 extra for rx_drain_qp */
0169         else
0170             size += ipoib_recvq_size * ipoib_max_conn_qp;
0171     } else
0172         if (ret != -EOPNOTSUPP)
0173             return ret;
0174 
0175     req_vec = atomic_inc_return(&counter) * 2;
0176     cq_attr.cqe = size;
0177     cq_attr.comp_vector = req_vec % priv->ca->num_comp_vectors;
0178     priv->recv_cq = ib_create_cq(priv->ca, ipoib_ib_rx_completion, NULL,
0179                      priv, &cq_attr);
0180     if (IS_ERR(priv->recv_cq)) {
0181         pr_warn("%s: failed to create receive CQ\n", ca->name);
0182         goto out_cm_dev_cleanup;
0183     }
0184 
0185     cq_attr.cqe = ipoib_sendq_size;
0186     cq_attr.comp_vector = (req_vec + 1) % priv->ca->num_comp_vectors;
0187     priv->send_cq = ib_create_cq(priv->ca, ipoib_ib_tx_completion, NULL,
0188                      priv, &cq_attr);
0189     if (IS_ERR(priv->send_cq)) {
0190         pr_warn("%s: failed to create send CQ\n", ca->name);
0191         goto out_free_recv_cq;
0192     }
0193 
0194     if (ib_req_notify_cq(priv->recv_cq, IB_CQ_NEXT_COMP))
0195         goto out_free_send_cq;
0196 
0197     init_attr.send_cq = priv->send_cq;
0198     init_attr.recv_cq = priv->recv_cq;
0199 
0200     if (priv->kernel_caps & IBK_UD_TSO)
0201         init_attr.create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
0202 
0203     if (priv->kernel_caps & IBK_BLOCK_MULTICAST_LOOPBACK)
0204         init_attr.create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
0205 
0206     if (priv->hca_caps & IB_DEVICE_MANAGED_FLOW_STEERING)
0207         init_attr.create_flags |= IB_QP_CREATE_NETIF_QP;
0208 
0209     if (priv->kernel_caps & IBK_RDMA_NETDEV_OPA)
0210         init_attr.create_flags |= IB_QP_CREATE_NETDEV_USE;
0211 
0212     priv->qp = ib_create_qp(priv->pd, &init_attr);
0213     if (IS_ERR(priv->qp)) {
0214         pr_warn("%s: failed to create QP\n", ca->name);
0215         goto out_free_send_cq;
0216     }
0217 
0218     if (ib_req_notify_cq(priv->send_cq, IB_CQ_NEXT_COMP))
0219         goto out_free_send_cq;
0220 
0221     for (i = 0; i < MAX_SKB_FRAGS + 1; ++i)
0222         priv->tx_sge[i].lkey = priv->pd->local_dma_lkey;
0223 
0224     priv->tx_wr.wr.opcode       = IB_WR_SEND;
0225     priv->tx_wr.wr.sg_list      = priv->tx_sge;
0226     priv->tx_wr.wr.send_flags   = IB_SEND_SIGNALED;
0227 
0228     priv->rx_sge[0].lkey = priv->pd->local_dma_lkey;
0229 
0230     priv->rx_sge[0].length = IPOIB_UD_BUF_SIZE(priv->max_ib_mtu);
0231     priv->rx_wr.num_sge = 1;
0232 
0233     priv->rx_wr.next = NULL;
0234     priv->rx_wr.sg_list = priv->rx_sge;
0235 
0236     if (init_attr.cap.max_send_sge > 1)
0237         dev->features |= NETIF_F_SG;
0238 
0239     priv->max_send_sge = init_attr.cap.max_send_sge;
0240 
0241     return 0;
0242 
0243 out_free_send_cq:
0244     ib_destroy_cq(priv->send_cq);
0245 
0246 out_free_recv_cq:
0247     ib_destroy_cq(priv->recv_cq);
0248 
0249 out_cm_dev_cleanup:
0250     ipoib_cm_dev_cleanup(dev);
0251 
0252     return -ENODEV;
0253 }
0254 
0255 void ipoib_transport_dev_cleanup(struct net_device *dev)
0256 {
0257     struct ipoib_dev_priv *priv = ipoib_priv(dev);
0258 
0259     if (priv->qp) {
0260         if (ib_destroy_qp(priv->qp))
0261             ipoib_warn(priv, "ib_qp_destroy failed\n");
0262 
0263         priv->qp = NULL;
0264     }
0265 
0266     ib_destroy_cq(priv->send_cq);
0267     ib_destroy_cq(priv->recv_cq);
0268 }
0269 
0270 void ipoib_event(struct ib_event_handler *handler,
0271          struct ib_event *record)
0272 {
0273     struct ipoib_dev_priv *priv =
0274         container_of(handler, struct ipoib_dev_priv, event_handler);
0275 
0276     if (record->element.port_num != priv->port)
0277         return;
0278 
0279     ipoib_dbg(priv, "Event %d on device %s port %d\n", record->event,
0280           dev_name(&record->device->dev), record->element.port_num);
0281 
0282     if (record->event == IB_EVENT_CLIENT_REREGISTER) {
0283         queue_work(ipoib_workqueue, &priv->flush_light);
0284     } else if (record->event == IB_EVENT_PORT_ERR ||
0285            record->event == IB_EVENT_PORT_ACTIVE ||
0286            record->event == IB_EVENT_LID_CHANGE) {
0287         queue_work(ipoib_workqueue, &priv->flush_normal);
0288     } else if (record->event == IB_EVENT_PKEY_CHANGE) {
0289         queue_work(ipoib_workqueue, &priv->flush_heavy);
0290     } else if (record->event == IB_EVENT_GID_CHANGE &&
0291            !test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
0292         queue_work(ipoib_workqueue, &priv->flush_light);
0293     }
0294 }