1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#include <linux/module.h>
54#include <linux/if_vlan.h>
55#include <linux/crc32.h>
56
57#include "opa_vnic_internal.h"
58
59#define OPA_TX_TIMEOUT_MS 1000
60
61#define OPA_VNIC_SKB_HEADROOM \
62 ALIGN((OPA_VNIC_HDR_LEN + OPA_VNIC_SKB_MDATA_LEN), 8)
63
64
65static void opa_vnic_get_stats64(struct net_device *netdev,
66 struct rtnl_link_stats64 *stats)
67{
68 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
69 struct opa_vnic_stats vstats;
70
71 memset(&vstats, 0, sizeof(vstats));
72 spin_lock(&adapter->stats_lock);
73 adapter->rn_ops->ndo_get_stats64(netdev, &vstats.netstats);
74 spin_unlock(&adapter->stats_lock);
75 memcpy(stats, &vstats.netstats, sizeof(*stats));
76}
77
78
79static netdev_tx_t opa_netdev_start_xmit(struct sk_buff *skb,
80 struct net_device *netdev)
81{
82 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
83
84 v_dbg("xmit: queue %d skb len %d\n", skb->queue_mapping, skb->len);
85
86 if (unlikely(skb->len < ETH_ZLEN)) {
87 if (skb_padto(skb, ETH_ZLEN))
88 return NETDEV_TX_OK;
89
90 skb_put(skb, ETH_ZLEN - skb->len);
91 }
92
93 opa_vnic_encap_skb(adapter, skb);
94 return adapter->rn_ops->ndo_start_xmit(skb, netdev);
95}
96
97static u16 opa_vnic_select_queue(struct net_device *netdev, struct sk_buff *skb,
98 struct net_device *sb_dev)
99{
100 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
101 struct opa_vnic_skb_mdata *mdata;
102 int rc;
103
104
105 mdata = skb_push(skb, sizeof(*mdata));
106 mdata->entropy = opa_vnic_calc_entropy(skb);
107 mdata->vl = opa_vnic_get_vl(adapter, skb);
108 rc = adapter->rn_ops->ndo_select_queue(netdev, skb, sb_dev);
109 skb_pull(skb, sizeof(*mdata));
110 return rc;
111}
112
113static void opa_vnic_update_state(struct opa_vnic_adapter *adapter, bool up)
114{
115 struct __opa_veswport_info *info = &adapter->info;
116
117 mutex_lock(&adapter->lock);
118
119 if ((info->vport.config_state == OPA_VNIC_STATE_FORWARDING) && up) {
120 info->vport.oper_state = OPA_VNIC_STATE_FORWARDING;
121 info->vport.eth_link_status = OPA_VNIC_ETH_LINK_UP;
122 } else {
123 info->vport.oper_state = OPA_VNIC_STATE_DROP_ALL;
124 info->vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN;
125 }
126
127 if (info->vport.config_state == OPA_VNIC_STATE_FORWARDING)
128 netif_dormant_off(adapter->netdev);
129 else
130 netif_dormant_on(adapter->netdev);
131 mutex_unlock(&adapter->lock);
132}
133
134
135void opa_vnic_process_vema_config(struct opa_vnic_adapter *adapter)
136{
137 struct __opa_veswport_info *info = &adapter->info;
138 struct rdma_netdev *rn = netdev_priv(adapter->netdev);
139 u8 port_num[OPA_VESW_MAX_NUM_DEF_PORT] = { 0 };
140 struct net_device *netdev = adapter->netdev;
141 u8 i, port_count = 0;
142 u16 port_mask;
143
144
145 if (memcmp(info->vport.base_mac_addr, adapter->vema_mac_addr,
146 ARRAY_SIZE(info->vport.base_mac_addr))) {
147 struct sockaddr saddr;
148
149 memcpy(saddr.sa_data, info->vport.base_mac_addr,
150 ARRAY_SIZE(info->vport.base_mac_addr));
151 mutex_lock(&adapter->lock);
152 eth_commit_mac_addr_change(netdev, &saddr);
153 memcpy(adapter->vema_mac_addr,
154 info->vport.base_mac_addr, ETH_ALEN);
155 mutex_unlock(&adapter->lock);
156 }
157
158 rn->set_id(netdev, info->vesw.vesw_id);
159
160
161 rtnl_lock();
162 netdev->max_mtu = max_t(unsigned int, info->vesw.eth_mtu,
163 netdev->min_mtu);
164 if (netdev->mtu > netdev->max_mtu)
165 dev_set_mtu(netdev, netdev->max_mtu);
166 rtnl_unlock();
167
168
169 port_mask = info->vesw.def_port_mask;
170 for (i = 0; i < OPA_VESW_MAX_NUM_DEF_PORT; i++) {
171 if (port_mask & 1)
172 port_num[port_count++] = i;
173 port_mask >>= 1;
174 }
175
176
177
178
179
180
181
182 for (i = 0; i < OPA_VNIC_FLOW_TBL_SIZE; i++)
183 adapter->flow_tbl[i] = port_count ? port_num[i % port_count] :
184 OPA_VNIC_INVALID_PORT;
185
186
187 opa_vnic_update_state(adapter, !!(netdev->flags & IFF_UP));
188}
189
190
191
192
193static inline void opa_vnic_set_pod_values(struct opa_vnic_adapter *adapter)
194{
195 adapter->info.vport.max_mac_tbl_ent = OPA_VNIC_MAC_TBL_MAX_ENTRIES;
196 adapter->info.vport.max_smac_ent = OPA_VNIC_MAX_SMAC_LIMIT;
197 adapter->info.vport.config_state = OPA_VNIC_STATE_DROP_ALL;
198 adapter->info.vport.eth_link_status = OPA_VNIC_ETH_LINK_DOWN;
199 adapter->info.vesw.eth_mtu = ETH_DATA_LEN;
200}
201
202
203static int opa_vnic_set_mac_addr(struct net_device *netdev, void *addr)
204{
205 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
206 struct sockaddr *sa = addr;
207 int rc;
208
209 if (!memcmp(netdev->dev_addr, sa->sa_data, ETH_ALEN))
210 return 0;
211
212 mutex_lock(&adapter->lock);
213 rc = eth_mac_addr(netdev, addr);
214 mutex_unlock(&adapter->lock);
215 if (rc)
216 return rc;
217
218 adapter->info.vport.uc_macs_gen_count++;
219 opa_vnic_vema_report_event(adapter,
220 OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE);
221 return 0;
222}
223
224
225
226
227
228
229static void opa_vnic_mac_send_event(struct net_device *netdev, u8 event)
230{
231 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
232 struct netdev_hw_addr *ha;
233 struct netdev_hw_addr_list *hw_list;
234 u32 *ref_crc;
235 u32 l, crc = 0;
236
237 switch (event) {
238 case OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE:
239 hw_list = &netdev->uc;
240 adapter->info.vport.uc_macs_gen_count++;
241 ref_crc = &adapter->umac_hash;
242 break;
243 case OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE:
244 hw_list = &netdev->mc;
245 adapter->info.vport.mc_macs_gen_count++;
246 ref_crc = &adapter->mmac_hash;
247 break;
248 default:
249 return;
250 }
251 netdev_hw_addr_list_for_each(ha, hw_list) {
252 crc = crc32_le(crc, ha->addr, ETH_ALEN);
253 }
254 l = netdev_hw_addr_list_count(hw_list) * ETH_ALEN;
255 crc = ~crc32_le(crc, (void *)&l, sizeof(l));
256
257 if (crc != *ref_crc) {
258 *ref_crc = crc;
259 opa_vnic_vema_report_event(adapter, event);
260 }
261}
262
263
264static void opa_vnic_set_rx_mode(struct net_device *netdev)
265{
266 opa_vnic_mac_send_event(netdev,
267 OPA_VESWPORT_TRAP_IFACE_UCAST_MAC_CHANGE);
268
269 opa_vnic_mac_send_event(netdev,
270 OPA_VESWPORT_TRAP_IFACE_MCAST_MAC_CHANGE);
271}
272
273
274static int opa_netdev_open(struct net_device *netdev)
275{
276 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
277 int rc;
278
279 rc = adapter->rn_ops->ndo_open(adapter->netdev);
280 if (rc) {
281 v_dbg("open failed %d\n", rc);
282 return rc;
283 }
284
285
286 opa_vnic_update_state(adapter, true);
287 opa_vnic_vema_report_event(adapter,
288 OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE);
289 return 0;
290}
291
292
293static int opa_netdev_close(struct net_device *netdev)
294{
295 struct opa_vnic_adapter *adapter = opa_vnic_priv(netdev);
296 int rc;
297
298 rc = adapter->rn_ops->ndo_stop(adapter->netdev);
299 if (rc) {
300 v_dbg("close failed %d\n", rc);
301 return rc;
302 }
303
304
305 opa_vnic_update_state(adapter, false);
306 opa_vnic_vema_report_event(adapter,
307 OPA_VESWPORT_TRAP_ETH_LINK_STATUS_CHANGE);
308 return 0;
309}
310
311
312static const struct net_device_ops opa_netdev_ops = {
313 .ndo_open = opa_netdev_open,
314 .ndo_stop = opa_netdev_close,
315 .ndo_start_xmit = opa_netdev_start_xmit,
316 .ndo_get_stats64 = opa_vnic_get_stats64,
317 .ndo_set_rx_mode = opa_vnic_set_rx_mode,
318 .ndo_select_queue = opa_vnic_select_queue,
319 .ndo_set_mac_address = opa_vnic_set_mac_addr,
320};
321
322
323struct opa_vnic_adapter *opa_vnic_add_netdev(struct ib_device *ibdev,
324 u8 port_num, u8 vport_num)
325{
326 struct opa_vnic_adapter *adapter;
327 struct net_device *netdev;
328 struct rdma_netdev *rn;
329 int rc;
330
331 netdev = ibdev->ops.alloc_rdma_netdev(ibdev, port_num,
332 RDMA_NETDEV_OPA_VNIC,
333 "veth%d", NET_NAME_UNKNOWN,
334 ether_setup);
335 if (!netdev)
336 return ERR_PTR(-ENOMEM);
337 else if (IS_ERR(netdev))
338 return ERR_CAST(netdev);
339
340 rn = netdev_priv(netdev);
341 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
342 if (!adapter) {
343 rc = -ENOMEM;
344 goto adapter_err;
345 }
346
347 rn->clnt_priv = adapter;
348 rn->hca = ibdev;
349 rn->port_num = port_num;
350 adapter->netdev = netdev;
351 adapter->ibdev = ibdev;
352 adapter->port_num = port_num;
353 adapter->vport_num = vport_num;
354 adapter->rn_ops = netdev->netdev_ops;
355
356 netdev->netdev_ops = &opa_netdev_ops;
357 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
358 netdev->hard_header_len += OPA_VNIC_SKB_HEADROOM;
359 mutex_init(&adapter->lock);
360 mutex_init(&adapter->mactbl_lock);
361 spin_lock_init(&adapter->stats_lock);
362
363 SET_NETDEV_DEV(netdev, ibdev->dev.parent);
364
365 opa_vnic_set_ethtool_ops(netdev);
366
367 opa_vnic_set_pod_values(adapter);
368
369 rc = register_netdev(netdev);
370 if (rc)
371 goto netdev_err;
372
373 netif_carrier_off(netdev);
374 netif_dormant_on(netdev);
375 v_info("initialized\n");
376
377 return adapter;
378netdev_err:
379 mutex_destroy(&adapter->lock);
380 mutex_destroy(&adapter->mactbl_lock);
381 kfree(adapter);
382adapter_err:
383 rn->free_rdma_netdev(netdev);
384
385 return ERR_PTR(rc);
386}
387
388
389void opa_vnic_rem_netdev(struct opa_vnic_adapter *adapter)
390{
391 struct net_device *netdev = adapter->netdev;
392 struct rdma_netdev *rn = netdev_priv(netdev);
393
394 v_info("removing\n");
395 unregister_netdev(netdev);
396 opa_vnic_release_mac_tbl(adapter);
397 mutex_destroy(&adapter->lock);
398 mutex_destroy(&adapter->mactbl_lock);
399 kfree(adapter);
400 rn->free_rdma_netdev(netdev);
401}
402