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(const 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 ret
= priv
->netdev_ops
->ndo_init(dev
);
28 ret
= hfi1_netdev_add_data(priv
->dd
,
29 qpn_from_mac(priv
->netdev
->dev_addr
),
32 priv
->netdev_ops
->ndo_uninit(dev
);
39 static void hfi1_ipoib_dev_uninit(struct net_device
*dev
)
41 struct hfi1_ipoib_dev_priv
*priv
= hfi1_ipoib_priv(dev
);
43 hfi1_netdev_remove_data(priv
->dd
, qpn_from_mac(priv
->netdev
->dev_addr
));
45 priv
->netdev_ops
->ndo_uninit(dev
);
48 static int hfi1_ipoib_dev_open(struct net_device
*dev
)
50 struct hfi1_ipoib_dev_priv
*priv
= hfi1_ipoib_priv(dev
);
53 ret
= priv
->netdev_ops
->ndo_open(dev
);
55 struct hfi1_ibport
*ibp
= to_iport(priv
->device
,
58 u32 qpn
= qpn_from_mac(priv
->netdev
->dev_addr
);
61 qp
= rvt_lookup_qpn(ib_to_rvt(priv
->device
), &ibp
->rvp
, qpn
);
64 priv
->netdev_ops
->ndo_stop(dev
);
71 hfi1_netdev_enable_queues(priv
->dd
);
72 hfi1_ipoib_napi_tx_enable(dev
);
78 static int hfi1_ipoib_dev_stop(struct net_device
*dev
)
80 struct hfi1_ipoib_dev_priv
*priv
= hfi1_ipoib_priv(dev
);
85 hfi1_ipoib_napi_tx_disable(dev
);
86 hfi1_netdev_disable_queues(priv
->dd
);
91 return priv
->netdev_ops
->ndo_stop(dev
);
94 static const struct net_device_ops hfi1_ipoib_netdev_ops
= {
95 .ndo_init
= hfi1_ipoib_dev_init
,
96 .ndo_uninit
= hfi1_ipoib_dev_uninit
,
97 .ndo_open
= hfi1_ipoib_dev_open
,
98 .ndo_stop
= hfi1_ipoib_dev_stop
,
101 static int hfi1_ipoib_mcast_attach(struct net_device
*dev
,
102 struct ib_device
*device
,
108 struct hfi1_ipoib_dev_priv
*priv
= hfi1_ipoib_priv(dev
);
109 u32 qpn
= (u32
)qpn_from_mac(priv
->netdev
->dev_addr
);
110 struct hfi1_ibport
*ibp
= to_iport(priv
->device
, priv
->port_num
);
116 qp
= rvt_lookup_qpn(ib_to_rvt(priv
->device
), &ibp
->rvp
, qpn
);
123 /* attach QP to multicast group */
124 ret
= ib_attach_mcast(&qp
->ibqp
, mgid
, mlid
);
133 static int hfi1_ipoib_mcast_detach(struct net_device
*dev
,
134 struct ib_device
*device
,
138 struct hfi1_ipoib_dev_priv
*priv
= hfi1_ipoib_priv(dev
);
139 u32 qpn
= (u32
)qpn_from_mac(priv
->netdev
->dev_addr
);
140 struct hfi1_ibport
*ibp
= to_iport(priv
->device
, priv
->port_num
);
146 qp
= rvt_lookup_qpn(ib_to_rvt(priv
->device
), &ibp
->rvp
, qpn
);
150 ret
= ib_detach_mcast(&qp
->ibqp
, mgid
, mlid
);
158 static void hfi1_ipoib_netdev_dtor(struct net_device
*dev
)
160 struct hfi1_ipoib_dev_priv
*priv
= hfi1_ipoib_priv(dev
);
162 hfi1_ipoib_txreq_deinit(priv
);
163 hfi1_ipoib_rxq_deinit(priv
->netdev
);
166 static void hfi1_ipoib_set_id(struct net_device
*dev
, int id
)
168 struct hfi1_ipoib_dev_priv
*priv
= hfi1_ipoib_priv(dev
);
170 priv
->pkey_index
= (u16
)id
;
171 ib_query_pkey(priv
->device
,
177 static int hfi1_ipoib_setup_rn(struct ib_device
*device
,
179 struct net_device
*netdev
,
182 struct hfi1_devdata
*dd
= dd_from_ibdev(device
);
183 struct rdma_netdev
*rn
= netdev_priv(netdev
);
184 struct hfi1_ipoib_dev_priv
*priv
;
187 rn
->send
= hfi1_ipoib_send
;
188 rn
->tx_timeout
= hfi1_ipoib_tx_timeout
;
189 rn
->attach_mcast
= hfi1_ipoib_mcast_attach
;
190 rn
->detach_mcast
= hfi1_ipoib_mcast_detach
;
191 rn
->set_id
= hfi1_ipoib_set_id
;
193 rn
->port_num
= port_num
;
194 rn
->mtu
= netdev
->mtu
;
196 priv
= hfi1_ipoib_priv(netdev
);
198 priv
->netdev
= netdev
;
199 priv
->device
= device
;
200 priv
->port_num
= port_num
;
201 priv
->netdev_ops
= netdev
->netdev_ops
;
203 ib_query_pkey(device
, port_num
, priv
->pkey_index
, &priv
->pkey
);
205 rc
= hfi1_ipoib_txreq_init(priv
);
207 dd_dev_err(dd
, "IPoIB netdev TX init - failed(%d)\n", rc
);
211 rc
= hfi1_ipoib_rxq_init(netdev
);
213 dd_dev_err(dd
, "IPoIB netdev RX init - failed(%d)\n", rc
);
214 hfi1_ipoib_txreq_deinit(priv
);
218 netdev
->netdev_ops
= &hfi1_ipoib_netdev_ops
;
220 netdev
->priv_destructor
= hfi1_ipoib_netdev_dtor
;
221 netdev
->needs_free_netdev
= true;
222 netdev
->pcpu_stat_type
= NETDEV_PCPU_STAT_TSTATS
;
227 int hfi1_ipoib_rn_get_params(struct ib_device
*device
,
229 enum rdma_netdev_t type
,
230 struct rdma_netdev_alloc_params
*params
)
232 struct hfi1_devdata
*dd
= dd_from_ibdev(device
);
234 if (type
!= RDMA_NETDEV_IPOIB
)
237 if (!HFI1_CAP_IS_KSET(AIP
) || !dd
->num_netdev_contexts
)
240 if (!port_num
|| port_num
> dd
->num_pports
)
243 params
->sizeof_priv
= sizeof(struct hfi1_ipoib_rdma_netdev
);
244 params
->txqs
= dd
->num_sdma
;
245 params
->rxqs
= dd
->num_netdev_contexts
;
246 params
->param
= NULL
;
247 params
->initialize_rdma_netdev
= hfi1_ipoib_setup_rn
;