1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2016-2017 Broadcom Limited
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  */
9 #include <linux/pci.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/rtnetlink.h>
13 #include <linux/jhash.h>
14 #include <net/pkt_cls.h>
15 
16 #include "bnxt_hsi.h"
17 #include "bnxt.h"
18 #include "bnxt_hwrm.h"
19 #include "bnxt_vfr.h"
20 #include "bnxt_devlink.h"
21 #include "bnxt_tc.h"
22 
23 #ifdef CONFIG_BNXT_SRIOV
24 
25 #define CFA_HANDLE_INVALID		0xffff
26 #define VF_IDX_INVALID			0xffff
27 
hwrm_cfa_vfr_alloc(struct bnxt * bp,u16 vf_idx,u16 * tx_cfa_action,u16 * rx_cfa_code)28 static int hwrm_cfa_vfr_alloc(struct bnxt *bp, u16 vf_idx,
29 			      u16 *tx_cfa_action, u16 *rx_cfa_code)
30 {
31 	struct hwrm_cfa_vfr_alloc_output *resp;
32 	struct hwrm_cfa_vfr_alloc_input *req;
33 	int rc;
34 
35 	rc = hwrm_req_init(bp, req, HWRM_CFA_VFR_ALLOC);
36 	if (!rc) {
37 		req->vf_id = cpu_to_le16(vf_idx);
38 		sprintf(req->vfr_name, "vfr%d", vf_idx);
39 
40 		resp = hwrm_req_hold(bp, req);
41 		rc = hwrm_req_send(bp, req);
42 		if (!rc) {
43 			*tx_cfa_action = le16_to_cpu(resp->tx_cfa_action);
44 			*rx_cfa_code = le16_to_cpu(resp->rx_cfa_code);
45 			netdev_dbg(bp->dev, "tx_cfa_action=0x%x, rx_cfa_code=0x%x",
46 				   *tx_cfa_action, *rx_cfa_code);
47 		}
48 		hwrm_req_drop(bp, req);
49 	}
50 	if (rc)
51 		netdev_info(bp->dev, "%s error rc=%d\n", __func__, rc);
52 	return rc;
53 }
54 
hwrm_cfa_vfr_free(struct bnxt * bp,u16 vf_idx)55 static int hwrm_cfa_vfr_free(struct bnxt *bp, u16 vf_idx)
56 {
57 	struct hwrm_cfa_vfr_free_input *req;
58 	int rc;
59 
60 	rc = hwrm_req_init(bp, req, HWRM_CFA_VFR_FREE);
61 	if (!rc) {
62 		sprintf(req->vfr_name, "vfr%d", vf_idx);
63 		rc = hwrm_req_send(bp, req);
64 	}
65 	if (rc)
66 		netdev_info(bp->dev, "%s error rc=%d\n", __func__, rc);
67 	return rc;
68 }
69 
bnxt_hwrm_vfr_qcfg(struct bnxt * bp,struct bnxt_vf_rep * vf_rep,u16 * max_mtu)70 static int bnxt_hwrm_vfr_qcfg(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
71 			      u16 *max_mtu)
72 {
73 	struct hwrm_func_qcfg_output *resp;
74 	struct hwrm_func_qcfg_input *req;
75 	u16 mtu;
76 	int rc;
77 
78 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
79 	if (rc)
80 		return rc;
81 
82 	req->fid = cpu_to_le16(bp->pf.vf[vf_rep->vf_idx].fw_fid);
83 	resp = hwrm_req_hold(bp, req);
84 	rc = hwrm_req_send(bp, req);
85 	if (!rc) {
86 		mtu = le16_to_cpu(resp->max_mtu_configured);
87 		if (!mtu)
88 			*max_mtu = BNXT_MAX_MTU;
89 		else
90 			*max_mtu = mtu;
91 	}
92 	hwrm_req_drop(bp, req);
93 	return rc;
94 }
95 
bnxt_vf_rep_open(struct net_device * dev)96 static int bnxt_vf_rep_open(struct net_device *dev)
97 {
98 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
99 	struct bnxt *bp = vf_rep->bp;
100 
101 	/* Enable link and TX only if the parent PF is open. */
102 	if (netif_running(bp->dev)) {
103 		netif_carrier_on(dev);
104 		netif_tx_start_all_queues(dev);
105 	}
106 	return 0;
107 }
108 
bnxt_vf_rep_close(struct net_device * dev)109 static int bnxt_vf_rep_close(struct net_device *dev)
110 {
111 	netif_carrier_off(dev);
112 	netif_tx_disable(dev);
113 
114 	return 0;
115 }
116 
bnxt_vf_rep_xmit(struct sk_buff * skb,struct net_device * dev)117 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
118 				    struct net_device *dev)
119 {
120 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
121 	int rc, len = skb->len;
122 
123 	skb_dst_drop(skb);
124 	dst_hold((struct dst_entry *)vf_rep->dst);
125 	skb_dst_set(skb, (struct dst_entry *)vf_rep->dst);
126 	skb->dev = vf_rep->dst->u.port_info.lower_dev;
127 
128 	rc = dev_queue_xmit(skb);
129 	if (!rc) {
130 		vf_rep->tx_stats.packets++;
131 		vf_rep->tx_stats.bytes += len;
132 	}
133 	return rc;
134 }
135 
136 static void
bnxt_vf_rep_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)137 bnxt_vf_rep_get_stats64(struct net_device *dev,
138 			struct rtnl_link_stats64 *stats)
139 {
140 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
141 
142 	stats->rx_packets = vf_rep->rx_stats.packets;
143 	stats->rx_bytes = vf_rep->rx_stats.bytes;
144 	stats->tx_packets = vf_rep->tx_stats.packets;
145 	stats->tx_bytes = vf_rep->tx_stats.bytes;
146 }
147 
bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)148 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,
149 					 void *type_data,
150 					 void *cb_priv)
151 {
152 	struct bnxt_vf_rep *vf_rep = cb_priv;
153 	struct bnxt *bp = vf_rep->bp;
154 	int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid;
155 
156 	if (!bnxt_tc_flower_enabled(vf_rep->bp) ||
157 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
158 		return -EOPNOTSUPP;
159 
160 	switch (type) {
161 	case TC_SETUP_CLSFLOWER:
162 		return bnxt_tc_setup_flower(bp, vf_fid, type_data);
163 	default:
164 		return -EOPNOTSUPP;
165 	}
166 }
167 
168 static LIST_HEAD(bnxt_vf_block_cb_list);
169 
bnxt_vf_rep_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)170 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
171 				void *type_data)
172 {
173 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
174 
175 	switch (type) {
176 	case TC_SETUP_BLOCK:
177 		return flow_block_cb_setup_simple(type_data,
178 						  &bnxt_vf_block_cb_list,
179 						  bnxt_vf_rep_setup_tc_block_cb,
180 						  vf_rep, vf_rep, true);
181 	default:
182 		return -EOPNOTSUPP;
183 	}
184 }
185 
bnxt_get_vf_rep(struct bnxt * bp,u16 cfa_code)186 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
187 {
188 	u16 vf_idx;
189 
190 	if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) {
191 		vf_idx = bp->cfa_code_map[cfa_code];
192 		if (vf_idx != VF_IDX_INVALID)
193 			return bp->vf_reps[vf_idx]->dev;
194 	}
195 	return NULL;
196 }
197 
bnxt_vf_rep_rx(struct bnxt * bp,struct sk_buff * skb)198 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
199 {
200 	struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
201 
202 	vf_rep->rx_stats.bytes += skb->len;
203 	vf_rep->rx_stats.packets++;
204 
205 	netif_receive_skb(skb);
206 }
207 
bnxt_vf_rep_get_phys_port_name(struct net_device * dev,char * buf,size_t len)208 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
209 					  size_t len)
210 {
211 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
212 	struct pci_dev *pf_pdev = vf_rep->bp->pdev;
213 	int rc;
214 
215 	rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
216 		      vf_rep->vf_idx);
217 	if (rc >= len)
218 		return -EOPNOTSUPP;
219 	return 0;
220 }
221 
bnxt_vf_rep_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)222 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
223 				    struct ethtool_drvinfo *info)
224 {
225 	strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
226 }
227 
bnxt_vf_rep_get_port_parent_id(struct net_device * dev,struct netdev_phys_item_id * ppid)228 static int bnxt_vf_rep_get_port_parent_id(struct net_device *dev,
229 					  struct netdev_phys_item_id *ppid)
230 {
231 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
232 
233 	/* as only PORT_PARENT_ID is supported currently use common code
234 	 * between PF and VF-rep for now.
235 	 */
236 	return bnxt_get_port_parent_id(vf_rep->bp->dev, ppid);
237 }
238 
239 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
240 	.get_drvinfo		= bnxt_vf_rep_get_drvinfo
241 };
242 
243 static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
244 	.ndo_open		= bnxt_vf_rep_open,
245 	.ndo_stop		= bnxt_vf_rep_close,
246 	.ndo_start_xmit		= bnxt_vf_rep_xmit,
247 	.ndo_get_stats64	= bnxt_vf_rep_get_stats64,
248 	.ndo_setup_tc		= bnxt_vf_rep_setup_tc,
249 	.ndo_get_port_parent_id	= bnxt_vf_rep_get_port_parent_id,
250 	.ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
251 };
252 
bnxt_dev_is_vf_rep(struct net_device * dev)253 bool bnxt_dev_is_vf_rep(struct net_device *dev)
254 {
255 	return dev->netdev_ops == &bnxt_vf_rep_netdev_ops;
256 }
257 
258 /* Called when the parent PF interface is closed:
259  * As the mode transition from SWITCHDEV to LEGACY
260  * happens under the rtnl_lock() this routine is safe
261  * under the rtnl_lock()
262  */
bnxt_vf_reps_close(struct bnxt * bp)263 void bnxt_vf_reps_close(struct bnxt *bp)
264 {
265 	struct bnxt_vf_rep *vf_rep;
266 	u16 num_vfs, i;
267 
268 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
269 		return;
270 
271 	num_vfs = pci_num_vf(bp->pdev);
272 	for (i = 0; i < num_vfs; i++) {
273 		vf_rep = bp->vf_reps[i];
274 		if (netif_running(vf_rep->dev))
275 			bnxt_vf_rep_close(vf_rep->dev);
276 	}
277 }
278 
279 /* Called when the parent PF interface is opened (re-opened):
280  * As the mode transition from SWITCHDEV to LEGACY
281  * happen under the rtnl_lock() this routine is safe
282  * under the rtnl_lock()
283  */
bnxt_vf_reps_open(struct bnxt * bp)284 void bnxt_vf_reps_open(struct bnxt *bp)
285 {
286 	int i;
287 
288 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
289 		return;
290 
291 	for (i = 0; i < pci_num_vf(bp->pdev); i++) {
292 		/* Open the VF-Rep only if it is allocated in the FW */
293 		if (bp->vf_reps[i]->tx_cfa_action != CFA_HANDLE_INVALID)
294 			bnxt_vf_rep_open(bp->vf_reps[i]->dev);
295 	}
296 }
297 
__bnxt_free_one_vf_rep(struct bnxt * bp,struct bnxt_vf_rep * vf_rep)298 static void __bnxt_free_one_vf_rep(struct bnxt *bp, struct bnxt_vf_rep *vf_rep)
299 {
300 	if (!vf_rep)
301 		return;
302 
303 	if (vf_rep->dst) {
304 		dst_release((struct dst_entry *)vf_rep->dst);
305 		vf_rep->dst = NULL;
306 	}
307 	if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID) {
308 		hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
309 		vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
310 	}
311 }
312 
__bnxt_vf_reps_destroy(struct bnxt * bp)313 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
314 {
315 	u16 num_vfs = pci_num_vf(bp->pdev);
316 	struct bnxt_vf_rep *vf_rep;
317 	int i;
318 
319 	for (i = 0; i < num_vfs; i++) {
320 		vf_rep = bp->vf_reps[i];
321 		if (vf_rep) {
322 			__bnxt_free_one_vf_rep(bp, vf_rep);
323 			if (vf_rep->dev) {
324 				/* if register_netdev failed, then netdev_ops
325 				 * would have been set to NULL
326 				 */
327 				if (vf_rep->dev->netdev_ops)
328 					unregister_netdev(vf_rep->dev);
329 				free_netdev(vf_rep->dev);
330 			}
331 		}
332 	}
333 
334 	kfree(bp->vf_reps);
335 	bp->vf_reps = NULL;
336 }
337 
bnxt_vf_reps_destroy(struct bnxt * bp)338 void bnxt_vf_reps_destroy(struct bnxt *bp)
339 {
340 	bool closed = false;
341 
342 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
343 		return;
344 
345 	if (!bp->vf_reps)
346 		return;
347 
348 	/* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
349 	 * before proceeding with VF-rep cleanup.
350 	 */
351 	rtnl_lock();
352 	if (netif_running(bp->dev)) {
353 		bnxt_close_nic(bp, false, false);
354 		closed = true;
355 	}
356 	/* un-publish cfa_code_map so that RX path can't see it anymore */
357 	kfree(bp->cfa_code_map);
358 	bp->cfa_code_map = NULL;
359 	bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
360 
361 	if (closed)
362 		bnxt_open_nic(bp, false, false);
363 	rtnl_unlock();
364 
365 	/* Need to call vf_reps_destroy() outside of rntl_lock
366 	 * as unregister_netdev takes rtnl_lock
367 	 */
368 	__bnxt_vf_reps_destroy(bp);
369 }
370 
371 /* Free the VF-Reps in firmware, during firmware hot-reset processing.
372  * Note that the VF-Rep netdevs are still active (not unregistered) during
373  * this process. As the mode transition from SWITCHDEV to LEGACY happens
374  * under the rtnl_lock() this routine is safe under the rtnl_lock().
375  */
bnxt_vf_reps_free(struct bnxt * bp)376 void bnxt_vf_reps_free(struct bnxt *bp)
377 {
378 	u16 num_vfs = pci_num_vf(bp->pdev);
379 	int i;
380 
381 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
382 		return;
383 
384 	for (i = 0; i < num_vfs; i++)
385 		__bnxt_free_one_vf_rep(bp, bp->vf_reps[i]);
386 }
387 
bnxt_alloc_vf_rep(struct bnxt * bp,struct bnxt_vf_rep * vf_rep,u16 * cfa_code_map)388 static int bnxt_alloc_vf_rep(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
389 			     u16 *cfa_code_map)
390 {
391 	/* get cfa handles from FW */
392 	if (hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx, &vf_rep->tx_cfa_action,
393 			       &vf_rep->rx_cfa_code))
394 		return -ENOLINK;
395 
396 	cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
397 	vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX, GFP_KERNEL);
398 	if (!vf_rep->dst)
399 		return -ENOMEM;
400 
401 	/* only cfa_action is needed to mux a packet while TXing */
402 	vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
403 	vf_rep->dst->u.port_info.lower_dev = bp->dev;
404 
405 	return 0;
406 }
407 
408 /* Allocate the VF-Reps in firmware, during firmware hot-reset processing.
409  * Note that the VF-Rep netdevs are still active (not unregistered) during
410  * this process. As the mode transition from SWITCHDEV to LEGACY happens
411  * under the rtnl_lock() this routine is safe under the rtnl_lock().
412  */
bnxt_vf_reps_alloc(struct bnxt * bp)413 int bnxt_vf_reps_alloc(struct bnxt *bp)
414 {
415 	u16 *cfa_code_map = bp->cfa_code_map, num_vfs = pci_num_vf(bp->pdev);
416 	struct bnxt_vf_rep *vf_rep;
417 	int rc, i;
418 
419 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
420 		return 0;
421 
422 	if (!cfa_code_map)
423 		return -EINVAL;
424 
425 	for (i = 0; i < MAX_CFA_CODE; i++)
426 		cfa_code_map[i] = VF_IDX_INVALID;
427 
428 	for (i = 0; i < num_vfs; i++) {
429 		vf_rep = bp->vf_reps[i];
430 		vf_rep->vf_idx = i;
431 
432 		rc = bnxt_alloc_vf_rep(bp, vf_rep, cfa_code_map);
433 		if (rc)
434 			goto err;
435 	}
436 
437 	return 0;
438 
439 err:
440 	netdev_info(bp->dev, "%s error=%d\n", __func__, rc);
441 	bnxt_vf_reps_free(bp);
442 	return rc;
443 }
444 
445 /* Use the OUI of the PF's perm addr and report the same mac addr
446  * for the same VF-rep each time
447  */
bnxt_vf_rep_eth_addr_gen(u8 * src_mac,u16 vf_idx,u8 * mac)448 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
449 {
450 	u32 addr;
451 
452 	ether_addr_copy(mac, src_mac);
453 
454 	addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
455 	mac[3] = (u8)(addr & 0xFF);
456 	mac[4] = (u8)((addr >> 8) & 0xFF);
457 	mac[5] = (u8)((addr >> 16) & 0xFF);
458 }
459 
bnxt_vf_rep_netdev_init(struct bnxt * bp,struct bnxt_vf_rep * vf_rep,struct net_device * dev)460 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
461 				    struct net_device *dev)
462 {
463 	struct net_device *pf_dev = bp->dev;
464 	u16 max_mtu;
465 
466 	dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
467 	dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
468 	/* Just inherit all the featues of the parent PF as the VF-R
469 	 * uses the RX/TX rings of the parent PF
470 	 */
471 	dev->hw_features = pf_dev->hw_features;
472 	dev->gso_partial_features = pf_dev->gso_partial_features;
473 	dev->vlan_features = pf_dev->vlan_features;
474 	dev->hw_enc_features = pf_dev->hw_enc_features;
475 	dev->features |= pf_dev->features;
476 	bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
477 				 dev->perm_addr);
478 	eth_hw_addr_set(dev, dev->perm_addr);
479 	/* Set VF-Rep's max-mtu to the corresponding VF's max-mtu */
480 	if (!bnxt_hwrm_vfr_qcfg(bp, vf_rep, &max_mtu))
481 		dev->max_mtu = max_mtu;
482 	dev->min_mtu = ETH_ZLEN;
483 }
484 
bnxt_vf_reps_create(struct bnxt * bp)485 static int bnxt_vf_reps_create(struct bnxt *bp)
486 {
487 	u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
488 	struct bnxt_vf_rep *vf_rep;
489 	struct net_device *dev;
490 	int rc, i;
491 
492 	if (!(bp->flags & BNXT_FLAG_DSN_VALID))
493 		return -ENODEV;
494 
495 	bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
496 	if (!bp->vf_reps)
497 		return -ENOMEM;
498 
499 	/* storage for cfa_code to vf-idx mapping */
500 	cfa_code_map = kmalloc_array(MAX_CFA_CODE, sizeof(*bp->cfa_code_map),
501 				     GFP_KERNEL);
502 	if (!cfa_code_map) {
503 		rc = -ENOMEM;
504 		goto err;
505 	}
506 	for (i = 0; i < MAX_CFA_CODE; i++)
507 		cfa_code_map[i] = VF_IDX_INVALID;
508 
509 	for (i = 0; i < num_vfs; i++) {
510 		dev = alloc_etherdev(sizeof(*vf_rep));
511 		if (!dev) {
512 			rc = -ENOMEM;
513 			goto err;
514 		}
515 
516 		vf_rep = netdev_priv(dev);
517 		bp->vf_reps[i] = vf_rep;
518 		vf_rep->dev = dev;
519 		vf_rep->bp = bp;
520 		vf_rep->vf_idx = i;
521 		vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
522 
523 		rc = bnxt_alloc_vf_rep(bp, vf_rep, cfa_code_map);
524 		if (rc)
525 			goto err;
526 
527 		bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
528 		rc = register_netdev(dev);
529 		if (rc) {
530 			/* no need for unregister_netdev in cleanup */
531 			dev->netdev_ops = NULL;
532 			goto err;
533 		}
534 	}
535 
536 	/* publish cfa_code_map only after all VF-reps have been initialized */
537 	bp->cfa_code_map = cfa_code_map;
538 	bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
539 	netif_keep_dst(bp->dev);
540 	return 0;
541 
542 err:
543 	netdev_info(bp->dev, "%s error=%d\n", __func__, rc);
544 	kfree(cfa_code_map);
545 	__bnxt_vf_reps_destroy(bp);
546 	return rc;
547 }
548 
549 /* Devlink related routines */
bnxt_dl_eswitch_mode_get(struct devlink * devlink,u16 * mode)550 int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
551 {
552 	struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
553 
554 	*mode = bp->eswitch_mode;
555 	return 0;
556 }
557 
bnxt_dl_eswitch_mode_set(struct devlink * devlink,u16 mode,struct netlink_ext_ack * extack)558 int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode,
559 			     struct netlink_ext_ack *extack)
560 {
561 	struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
562 
563 	if (bp->eswitch_mode == mode) {
564 		netdev_info(bp->dev, "already in %s eswitch mode\n",
565 			    mode == DEVLINK_ESWITCH_MODE_LEGACY ?
566 			    "legacy" : "switchdev");
567 		return -EINVAL;
568 	}
569 
570 	switch (mode) {
571 	case DEVLINK_ESWITCH_MODE_LEGACY:
572 		bnxt_vf_reps_destroy(bp);
573 		return 0;
574 
575 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
576 		if (bp->hwrm_spec_code < 0x10803) {
577 			netdev_warn(bp->dev, "FW does not support SRIOV E-Switch SWITCHDEV mode\n");
578 			return -ENOTSUPP;
579 		}
580 
581 		if (pci_num_vf(bp->pdev) == 0) {
582 			netdev_info(bp->dev, "Enable VFs before setting switchdev mode\n");
583 			return -EPERM;
584 		}
585 		return bnxt_vf_reps_create(bp);
586 
587 	default:
588 		return -EINVAL;
589 	}
590 }
591 
592 #endif
593