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