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);
100 u64 rx_packets = 0ull;
102 u64 tx_packets = 0ull;
106 netdev_stats_to_stats64(storage, &dev->stats);
108 for_each_possible_cpu(i) {
109 const struct pcpu_sw_netstats *stats;
116 stats = per_cpu_ptr(priv->netstats, i);
118 start = u64_stats_fetch_begin_irq(&stats->syncp);
119 trx_packets = stats->rx_packets;
120 trx_bytes = stats->rx_bytes;
121 ttx_packets = stats->tx_packets;
122 ttx_bytes = stats->tx_bytes;
123 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
125 rx_packets += trx_packets;
126 rx_bytes += trx_bytes;
127 tx_packets += ttx_packets;
128 tx_bytes += ttx_bytes;
131 storage->rx_packets += rx_packets;
132 storage->rx_bytes += rx_bytes;
133 storage->tx_packets += tx_packets;
134 storage->tx_bytes += tx_bytes;
137 static const struct net_device_ops hfi1_ipoib_netdev_ops = {
138 .ndo_init = hfi1_ipoib_dev_init,
139 .ndo_uninit = hfi1_ipoib_dev_uninit,
140 .ndo_open = hfi1_ipoib_dev_open,
141 .ndo_stop = hfi1_ipoib_dev_stop,
142 .ndo_get_stats64 = hfi1_ipoib_dev_get_stats64,
145 static int hfi1_ipoib_send(struct net_device *dev,
147 struct ib_ah *address,
150 return hfi1_ipoib_send_dma(dev, skb, address, dqpn);
153 static int hfi1_ipoib_mcast_attach(struct net_device *dev,
154 struct ib_device *device,
160 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
161 u32 qpn = (u32)qpn_from_mac(priv->netdev->dev_addr);
162 struct hfi1_ibport *ibp = to_iport(priv->device, priv->port_num);
168 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
175 /* attach QP to multicast group */
176 ret = ib_attach_mcast(&qp->ibqp, mgid, mlid);
185 static int hfi1_ipoib_mcast_detach(struct net_device *dev,
186 struct ib_device *device,
190 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
191 u32 qpn = (u32)qpn_from_mac(priv->netdev->dev_addr);
192 struct hfi1_ibport *ibp = to_iport(priv->device, priv->port_num);
198 qp = rvt_lookup_qpn(ib_to_rvt(priv->device), &ibp->rvp, qpn);
202 ret = ib_detach_mcast(&qp->ibqp, mgid, mlid);
210 static void hfi1_ipoib_netdev_dtor(struct net_device *dev)
212 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
214 hfi1_ipoib_txreq_deinit(priv);
215 hfi1_ipoib_rxq_deinit(priv->netdev);
217 free_percpu(priv->netstats);
220 static void hfi1_ipoib_free_rdma_netdev(struct net_device *dev)
222 hfi1_ipoib_netdev_dtor(dev);
226 static void hfi1_ipoib_set_id(struct net_device *dev, int id)
228 struct hfi1_ipoib_dev_priv *priv = hfi1_ipoib_priv(dev);
230 priv->pkey_index = (u16)id;
231 ib_query_pkey(priv->device,
237 static int hfi1_ipoib_setup_rn(struct ib_device *device,
239 struct net_device *netdev,
242 struct hfi1_devdata *dd = dd_from_ibdev(device);
243 struct rdma_netdev *rn = netdev_priv(netdev);
244 struct hfi1_ipoib_dev_priv *priv;
247 rn->send = hfi1_ipoib_send;
248 rn->attach_mcast = hfi1_ipoib_mcast_attach;
249 rn->detach_mcast = hfi1_ipoib_mcast_detach;
250 rn->set_id = hfi1_ipoib_set_id;
252 rn->port_num = port_num;
253 rn->mtu = netdev->mtu;
255 priv = hfi1_ipoib_priv(netdev);
257 priv->netdev = netdev;
258 priv->device = device;
259 priv->port_num = port_num;
260 priv->netdev_ops = netdev->netdev_ops;
262 netdev->netdev_ops = &hfi1_ipoib_netdev_ops;
264 ib_query_pkey(device, port_num, priv->pkey_index, &priv->pkey);
266 rc = hfi1_ipoib_txreq_init(priv);
268 dd_dev_err(dd, "IPoIB netdev TX init - failed(%d)\n", rc);
269 hfi1_ipoib_free_rdma_netdev(netdev);
273 rc = hfi1_ipoib_rxq_init(netdev);
275 dd_dev_err(dd, "IPoIB netdev RX init - failed(%d)\n", rc);
276 hfi1_ipoib_free_rdma_netdev(netdev);
280 netdev->priv_destructor = hfi1_ipoib_netdev_dtor;
281 netdev->needs_free_netdev = true;
286 int hfi1_ipoib_rn_get_params(struct ib_device *device,
288 enum rdma_netdev_t type,
289 struct rdma_netdev_alloc_params *params)
291 struct hfi1_devdata *dd = dd_from_ibdev(device);
293 if (type != RDMA_NETDEV_IPOIB)
296 if (!HFI1_CAP_IS_KSET(AIP) || !dd->num_netdev_contexts)
299 if (!port_num || port_num > dd->num_pports)
302 params->sizeof_priv = sizeof(struct hfi1_ipoib_rdma_netdev);
303 params->txqs = dd->num_sdma;
304 params->rxqs = dd->num_netdev_contexts;
305 params->param = NULL;
306 params->initialize_rdma_netdev = hfi1_ipoib_setup_rn;