1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 * Copyright(c) 2020 Intel Corporation.
8 * This file contains HFI1 support for ipoib functionality
14 static u32 qpn_from_mac(u8 *mac_arr)
16 return (u32)mac_arr[1] << 16 | mac_arr[2] << 8 | mac_arr[3];
19 static int hfi1_ipoib_dev_init(struct net_device *dev)
21 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
24 priv->netstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
26 ret = priv->netdev_ops->ndo_init(dev);
30 ret = hfi1_netdev_add_data(priv->dd,
31 qpn_from_mac(priv->netdev->dev_addr),
34 priv->netdev_ops->ndo_uninit(dev);
41 static void hfi1_ipoib_dev_uninit(struct net_device *dev)
43 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
45 hfi1_netdev_remove_data(priv->dd, qpn_from_mac(priv->netdev->dev_addr));
47 priv->netdev_ops->ndo_uninit(dev);
50 static int hfi1_ipoib_dev_open(struct net_device *dev)
52 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
55 ret = priv->netdev_ops->ndo_open(dev);
57 struct hfi1_ibport *ibp = to_iport(priv->device,
60 u32 qpn = qpn_from_mac(priv->netdev->dev_addr);
63 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
66 priv->netdev_ops->ndo_stop(dev);
73 hfi1_netdev_enable_queues(priv->dd);
74 hfi1_ipoib_napi_tx_enable(dev);
80 static int hfi1_ipoib_dev_stop(struct net_device *dev)
82 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
87 hfi1_ipoib_napi_tx_disable(dev);
88 hfi1_netdev_disable_queues(priv->dd);
93 return priv->netdev_ops->ndo_stop(dev);
96 static void hfi1_ipoib_dev_get_stats64(struct net_device *dev,
97 struct rtnl_link_stats64 *storage)
99 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
101 netdev_stats_to_stats64(storage, &dev->stats);
102 dev_fetch_sw_netstats(storage, priv->netstats);
105 static const struct net_device_ops hfi1_ipoib_netdev_ops = {
106 .ndo_init = hfi1_ipoib_dev_init,
107 .ndo_uninit = hfi1_ipoib_dev_uninit,
108 .ndo_open = hfi1_ipoib_dev_open,
109 .ndo_stop = hfi1_ipoib_dev_stop,
110 .ndo_get_stats64 = hfi1_ipoib_dev_get_stats64,
113 static int hfi1_ipoib_send(struct net_device *dev,
115 struct ib_ah *address,
118 return hfi1_ipoib_send_dma(dev, skb, address, dqpn);
121 static int hfi1_ipoib_mcast_attach(struct net_device *dev,
122 struct ib_device *device,
128 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
129 u32 qpn = (u32)qpn_from_mac(priv->netdev->dev_addr);
130 struct hfi1_ibport *ibp = to_iport(priv->device, priv->port_num);
136 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
143 /* attach QP to multicast group */
144 ret = ib_attach_mcast(&qp->ibqp, mgid, mlid);
153 static int hfi1_ipoib_mcast_detach(struct net_device *dev,
154 struct ib_device *device,
158 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
159 u32 qpn = (u32)qpn_from_mac(priv->netdev->dev_addr);
160 struct hfi1_ibport *ibp = to_iport(priv->device, priv->port_num);
166 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
170 ret = ib_detach_mcast(&qp->ibqp, mgid, mlid);
178 static void hfi1_ipoib_netdev_dtor(struct net_device *dev)
180 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
182 hfi1_ipoib_txreq_deinit(priv);
183 hfi1_ipoib_rxq_deinit(priv->netdev);
185 free_percpu(priv->netstats);
188 static void hfi1_ipoib_set_id(struct net_device *dev, int id)
190 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
192 priv->pkey_index = (u16)id;
193 ib_query_pkey(priv->device,
199 static int hfi1_ipoib_setup_rn(struct ib_device *device,
201 struct net_device *netdev,
204 struct hfi1_devdata *dd = dd_from_ibdev(device);
205 struct rdma_netdev *rn = netdev_priv(netdev);
206 struct hfi1_ipoib_dev_priv *priv;
209 rn->send = hfi1_ipoib_send;
210 rn->attach_mcast = hfi1_ipoib_mcast_attach;
211 rn->detach_mcast = hfi1_ipoib_mcast_detach;
212 rn->set_id = hfi1_ipoib_set_id;
214 rn->port_num = port_num;
215 rn->mtu = netdev->mtu;
217 priv = hfi1_ipoib_priv(netdev);
219 priv->netdev = netdev;
220 priv->device = device;
221 priv->port_num = port_num;
222 priv->netdev_ops = netdev->netdev_ops;
224 ib_query_pkey(device, port_num, priv->pkey_index, &priv->pkey);
226 rc = hfi1_ipoib_txreq_init(priv);
228 dd_dev_err(dd, "IPoIB netdev TX init - failed(%d)\n", rc);
232 rc = hfi1_ipoib_rxq_init(netdev);
234 dd_dev_err(dd, "IPoIB netdev RX init - failed(%d)\n", rc);
235 hfi1_ipoib_txreq_deinit(priv);
239 netdev->netdev_ops = &hfi1_ipoib_netdev_ops;
241 netdev->priv_destructor = hfi1_ipoib_netdev_dtor;
242 netdev->needs_free_netdev = true;
247 int hfi1_ipoib_rn_get_params(struct ib_device *device,
249 enum rdma_netdev_t type,
250 struct rdma_netdev_alloc_params *params)
252 struct hfi1_devdata *dd = dd_from_ibdev(device);
254 if (type != RDMA_NETDEV_IPOIB)
257 if (!HFI1_CAP_IS_KSET(AIP) || !dd->num_netdev_contexts)
260 if (!port_num || port_num > dd->num_pports)
263 params->sizeof_priv = sizeof(struct hfi1_ipoib_rdma_netdev);
264 params->txqs = dd->num_sdma;
265 params->rxqs = dd->num_netdev_contexts;
266 params->param = NULL;
267 params->initialize_rdma_netdev = hfi1_ipoib_setup_rn;