0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "ipoib.h"
0012 #include "hfi.h"
0013
0014 static u32 qpn_from_mac(const u8 *mac_arr)
0015 {
0016 return (u32)mac_arr[1] << 16 | mac_arr[2] << 8 | mac_arr[3];
0017 }
0018
0019 static int hfi1_ipoib_dev_init(struct net_device *dev)
0020 {
0021 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0022 int ret;
0023
0024 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
0025 if (!dev->tstats)
0026 return -ENOMEM;
0027
0028 ret = priv->netdev_ops->ndo_init(dev);
0029 if (ret)
0030 goto out_ret;
0031
0032 ret = hfi1_netdev_add_data(priv->dd,
0033 qpn_from_mac(priv->netdev->dev_addr),
0034 dev);
0035 if (ret < 0) {
0036 priv->netdev_ops->ndo_uninit(dev);
0037 goto out_ret;
0038 }
0039
0040 return 0;
0041 out_ret:
0042 free_percpu(dev->tstats);
0043 dev->tstats = NULL;
0044 return ret;
0045 }
0046
0047 static void hfi1_ipoib_dev_uninit(struct net_device *dev)
0048 {
0049 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0050
0051 free_percpu(dev->tstats);
0052 dev->tstats = NULL;
0053
0054 hfi1_netdev_remove_data(priv->dd, qpn_from_mac(priv->netdev->dev_addr));
0055
0056 priv->netdev_ops->ndo_uninit(dev);
0057 }
0058
0059 static int hfi1_ipoib_dev_open(struct net_device *dev)
0060 {
0061 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0062 int ret;
0063
0064 ret = priv->netdev_ops->ndo_open(dev);
0065 if (!ret) {
0066 struct hfi1_ibport *ibp = to_iport(priv->device,
0067 priv->port_num);
0068 struct rvt_qp *qp;
0069 u32 qpn = qpn_from_mac(priv->netdev->dev_addr);
0070
0071 rcu_read_lock();
0072 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
0073 if (!qp) {
0074 rcu_read_unlock();
0075 priv->netdev_ops->ndo_stop(dev);
0076 return -EINVAL;
0077 }
0078 rvt_get_qp(qp);
0079 priv->qp = qp;
0080 rcu_read_unlock();
0081
0082 hfi1_netdev_enable_queues(priv->dd);
0083 hfi1_ipoib_napi_tx_enable(dev);
0084 }
0085
0086 return ret;
0087 }
0088
0089 static int hfi1_ipoib_dev_stop(struct net_device *dev)
0090 {
0091 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0092
0093 if (!priv->qp)
0094 return 0;
0095
0096 hfi1_ipoib_napi_tx_disable(dev);
0097 hfi1_netdev_disable_queues(priv->dd);
0098
0099 rvt_put_qp(priv->qp);
0100 priv->qp = NULL;
0101
0102 return priv->netdev_ops->ndo_stop(dev);
0103 }
0104
0105 static const struct net_device_ops hfi1_ipoib_netdev_ops = {
0106 .ndo_init = hfi1_ipoib_dev_init,
0107 .ndo_uninit = hfi1_ipoib_dev_uninit,
0108 .ndo_open = hfi1_ipoib_dev_open,
0109 .ndo_stop = hfi1_ipoib_dev_stop,
0110 .ndo_get_stats64 = dev_get_tstats64,
0111 };
0112
0113 static int hfi1_ipoib_mcast_attach(struct net_device *dev,
0114 struct ib_device *device,
0115 union ib_gid *mgid,
0116 u16 mlid,
0117 int set_qkey,
0118 u32 qkey)
0119 {
0120 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0121 u32 qpn = (u32)qpn_from_mac(priv->netdev->dev_addr);
0122 struct hfi1_ibport *ibp = to_iport(priv->device, priv->port_num);
0123 struct rvt_qp *qp;
0124 int ret = -EINVAL;
0125
0126 rcu_read_lock();
0127
0128 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
0129 if (qp) {
0130 rvt_get_qp(qp);
0131 rcu_read_unlock();
0132 if (set_qkey)
0133 priv->qkey = qkey;
0134
0135
0136 ret = ib_attach_mcast(&qp->ibqp, mgid, mlid);
0137 rvt_put_qp(qp);
0138 } else {
0139 rcu_read_unlock();
0140 }
0141
0142 return ret;
0143 }
0144
0145 static int hfi1_ipoib_mcast_detach(struct net_device *dev,
0146 struct ib_device *device,
0147 union ib_gid *mgid,
0148 u16 mlid)
0149 {
0150 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0151 u32 qpn = (u32)qpn_from_mac(priv->netdev->dev_addr);
0152 struct hfi1_ibport *ibp = to_iport(priv->device, priv->port_num);
0153 struct rvt_qp *qp;
0154 int ret = -EINVAL;
0155
0156 rcu_read_lock();
0157
0158 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
0159 if (qp) {
0160 rvt_get_qp(qp);
0161 rcu_read_unlock();
0162 ret = ib_detach_mcast(&qp->ibqp, mgid, mlid);
0163 rvt_put_qp(qp);
0164 } else {
0165 rcu_read_unlock();
0166 }
0167 return ret;
0168 }
0169
0170 static void hfi1_ipoib_netdev_dtor(struct net_device *dev)
0171 {
0172 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0173
0174 hfi1_ipoib_txreq_deinit(priv);
0175 hfi1_ipoib_rxq_deinit(priv->netdev);
0176
0177 free_percpu(dev->tstats);
0178 dev->tstats = NULL;
0179 }
0180
0181 static void hfi1_ipoib_set_id(struct net_device *dev, int id)
0182 {
0183 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
0184
0185 priv->pkey_index = (u16)id;
0186 ib_query_pkey(priv->device,
0187 priv->port_num,
0188 priv->pkey_index,
0189 &priv->pkey);
0190 }
0191
0192 static int hfi1_ipoib_setup_rn(struct ib_device *device,
0193 u32 port_num,
0194 struct net_device *netdev,
0195 void *param)
0196 {
0197 struct hfi1_devdata *dd = dd_from_ibdev(device);
0198 struct rdma_netdev *rn = netdev_priv(netdev);
0199 struct hfi1_ipoib_dev_priv *priv;
0200 int rc;
0201
0202 rn->send = hfi1_ipoib_send;
0203 rn->tx_timeout = hfi1_ipoib_tx_timeout;
0204 rn->attach_mcast = hfi1_ipoib_mcast_attach;
0205 rn->detach_mcast = hfi1_ipoib_mcast_detach;
0206 rn->set_id = hfi1_ipoib_set_id;
0207 rn->hca = device;
0208 rn->port_num = port_num;
0209 rn->mtu = netdev->mtu;
0210
0211 priv = hfi1_ipoib_priv(netdev);
0212 priv->dd = dd;
0213 priv->netdev = netdev;
0214 priv->device = device;
0215 priv->port_num = port_num;
0216 priv->netdev_ops = netdev->netdev_ops;
0217
0218 ib_query_pkey(device, port_num, priv->pkey_index, &priv->pkey);
0219
0220 rc = hfi1_ipoib_txreq_init(priv);
0221 if (rc) {
0222 dd_dev_err(dd, "IPoIB netdev TX init - failed(%d)\n", rc);
0223 return rc;
0224 }
0225
0226 rc = hfi1_ipoib_rxq_init(netdev);
0227 if (rc) {
0228 dd_dev_err(dd, "IPoIB netdev RX init - failed(%d)\n", rc);
0229 hfi1_ipoib_txreq_deinit(priv);
0230 return rc;
0231 }
0232
0233 netdev->netdev_ops = &hfi1_ipoib_netdev_ops;
0234
0235 netdev->priv_destructor = hfi1_ipoib_netdev_dtor;
0236 netdev->needs_free_netdev = true;
0237
0238 return 0;
0239 }
0240
0241 int hfi1_ipoib_rn_get_params(struct ib_device *device,
0242 u32 port_num,
0243 enum rdma_netdev_t type,
0244 struct rdma_netdev_alloc_params *params)
0245 {
0246 struct hfi1_devdata *dd = dd_from_ibdev(device);
0247
0248 if (type != RDMA_NETDEV_IPOIB)
0249 return -EOPNOTSUPP;
0250
0251 if (!HFI1_CAP_IS_KSET(AIP) || !dd->num_netdev_contexts)
0252 return -EOPNOTSUPP;
0253
0254 if (!port_num || port_num > dd->num_pports)
0255 return -EINVAL;
0256
0257 params->sizeof_priv = sizeof(struct hfi1_ipoib_rdma_netdev);
0258 params->txqs = dd->num_sdma;
0259 params->rxqs = dd->num_netdev_contexts;
0260 params->param = NULL;
0261 params->initialize_rdma_netdev = hfi1_ipoib_setup_rn;
0262
0263 return 0;
0264 }