0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
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
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
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
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;
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 }