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", __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", __func__, rc);
64 	return rc;
65 }
66 
67 static int bnxt_vf_rep_open(struct net_device *dev)
68 {
69 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
70 	struct bnxt *bp = vf_rep->bp;
71 
72 	/* Enable link and TX only if the parent PF is open. */
73 	if (netif_running(bp->dev)) {
74 		netif_carrier_on(dev);
75 		netif_tx_start_all_queues(dev);
76 	}
77 	return 0;
78 }
79 
80 static int bnxt_vf_rep_close(struct net_device *dev)
81 {
82 	netif_carrier_off(dev);
83 	netif_tx_disable(dev);
84 
85 	return 0;
86 }
87 
88 static netdev_tx_t bnxt_vf_rep_xmit(struct sk_buff *skb,
89 				    struct net_device *dev)
90 {
91 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
92 	int rc, len = skb->len;
93 
94 	skb_dst_drop(skb);
95 	dst_hold((struct dst_entry *)vf_rep->dst);
96 	skb_dst_set(skb, (struct dst_entry *)vf_rep->dst);
97 	skb->dev = vf_rep->dst->u.port_info.lower_dev;
98 
99 	rc = dev_queue_xmit(skb);
100 	if (!rc) {
101 		vf_rep->tx_stats.packets++;
102 		vf_rep->tx_stats.bytes += len;
103 	}
104 	return rc;
105 }
106 
107 static void
108 bnxt_vf_rep_get_stats64(struct net_device *dev,
109 			struct rtnl_link_stats64 *stats)
110 {
111 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
112 
113 	stats->rx_packets = vf_rep->rx_stats.packets;
114 	stats->rx_bytes = vf_rep->rx_stats.bytes;
115 	stats->tx_packets = vf_rep->tx_stats.packets;
116 	stats->tx_bytes = vf_rep->tx_stats.bytes;
117 }
118 
119 static int bnxt_vf_rep_setup_tc_block_cb(enum tc_setup_type type,
120 					 void *type_data,
121 					 void *cb_priv)
122 {
123 	struct bnxt_vf_rep *vf_rep = cb_priv;
124 	struct bnxt *bp = vf_rep->bp;
125 	int vf_fid = bp->pf.vf[vf_rep->vf_idx].fw_fid;
126 
127 	if (!bnxt_tc_flower_enabled(vf_rep->bp) || !tc_can_offload(bp->dev))
128 		return -EOPNOTSUPP;
129 
130 	switch (type) {
131 	case TC_SETUP_CLSFLOWER:
132 		return bnxt_tc_setup_flower(bp, vf_fid, type_data);
133 	default:
134 		return -EOPNOTSUPP;
135 	}
136 }
137 
138 static int bnxt_vf_rep_setup_tc_block(struct net_device *dev,
139 				      struct tc_block_offload *f)
140 {
141 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
142 
143 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
144 		return -EOPNOTSUPP;
145 
146 	switch (f->command) {
147 	case TC_BLOCK_BIND:
148 		return tcf_block_cb_register(f->block,
149 					     bnxt_vf_rep_setup_tc_block_cb,
150 					     vf_rep, vf_rep);
151 	case TC_BLOCK_UNBIND:
152 		tcf_block_cb_unregister(f->block,
153 					bnxt_vf_rep_setup_tc_block_cb, vf_rep);
154 		return 0;
155 	default:
156 		return -EOPNOTSUPP;
157 	}
158 }
159 
160 static int bnxt_vf_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
161 				void *type_data)
162 {
163 	switch (type) {
164 	case TC_SETUP_BLOCK:
165 		return bnxt_vf_rep_setup_tc_block(dev, type_data);
166 	default:
167 		return -EOPNOTSUPP;
168 	}
169 }
170 
171 struct net_device *bnxt_get_vf_rep(struct bnxt *bp, u16 cfa_code)
172 {
173 	u16 vf_idx;
174 
175 	if (cfa_code && bp->cfa_code_map && BNXT_PF(bp)) {
176 		vf_idx = bp->cfa_code_map[cfa_code];
177 		if (vf_idx != VF_IDX_INVALID)
178 			return bp->vf_reps[vf_idx]->dev;
179 	}
180 	return NULL;
181 }
182 
183 void bnxt_vf_rep_rx(struct bnxt *bp, struct sk_buff *skb)
184 {
185 	struct bnxt_vf_rep *vf_rep = netdev_priv(skb->dev);
186 	struct bnxt_vf_rep_stats *rx_stats;
187 
188 	rx_stats = &vf_rep->rx_stats;
189 	vf_rep->rx_stats.bytes += skb->len;
190 	vf_rep->rx_stats.packets++;
191 
192 	netif_receive_skb(skb);
193 }
194 
195 static int bnxt_vf_rep_get_phys_port_name(struct net_device *dev, char *buf,
196 					  size_t len)
197 {
198 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
199 	struct pci_dev *pf_pdev = vf_rep->bp->pdev;
200 	int rc;
201 
202 	rc = snprintf(buf, len, "pf%dvf%d", PCI_FUNC(pf_pdev->devfn),
203 		      vf_rep->vf_idx);
204 	if (rc >= len)
205 		return -EOPNOTSUPP;
206 	return 0;
207 }
208 
209 static void bnxt_vf_rep_get_drvinfo(struct net_device *dev,
210 				    struct ethtool_drvinfo *info)
211 {
212 	strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
213 	strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
214 }
215 
216 static int bnxt_vf_rep_port_attr_get(struct net_device *dev,
217 				     struct switchdev_attr *attr)
218 {
219 	struct bnxt_vf_rep *vf_rep = netdev_priv(dev);
220 
221 	/* as only PORT_PARENT_ID is supported currently use common code
222 	 * between PF and VF-rep for now.
223 	 */
224 	return bnxt_port_attr_get(vf_rep->bp, attr);
225 }
226 
227 static const struct switchdev_ops bnxt_vf_rep_switchdev_ops = {
228 	.switchdev_port_attr_get	= bnxt_vf_rep_port_attr_get
229 };
230 
231 static const struct ethtool_ops bnxt_vf_rep_ethtool_ops = {
232 	.get_drvinfo		= bnxt_vf_rep_get_drvinfo
233 };
234 
235 static const struct net_device_ops bnxt_vf_rep_netdev_ops = {
236 	.ndo_open		= bnxt_vf_rep_open,
237 	.ndo_stop		= bnxt_vf_rep_close,
238 	.ndo_start_xmit		= bnxt_vf_rep_xmit,
239 	.ndo_get_stats64	= bnxt_vf_rep_get_stats64,
240 	.ndo_setup_tc		= bnxt_vf_rep_setup_tc,
241 	.ndo_get_phys_port_name = bnxt_vf_rep_get_phys_port_name
242 };
243 
244 /* Called when the parent PF interface is closed:
245  * As the mode transition from SWITCHDEV to LEGACY
246  * happens under the rtnl_lock() this routine is safe
247  * under the rtnl_lock()
248  */
249 void bnxt_vf_reps_close(struct bnxt *bp)
250 {
251 	struct bnxt_vf_rep *vf_rep;
252 	u16 num_vfs, i;
253 
254 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
255 		return;
256 
257 	num_vfs = pci_num_vf(bp->pdev);
258 	for (i = 0; i < num_vfs; i++) {
259 		vf_rep = bp->vf_reps[i];
260 		if (netif_running(vf_rep->dev))
261 			bnxt_vf_rep_close(vf_rep->dev);
262 	}
263 }
264 
265 /* Called when the parent PF interface is opened (re-opened):
266  * As the mode transition from SWITCHDEV to LEGACY
267  * happen under the rtnl_lock() this routine is safe
268  * under the rtnl_lock()
269  */
270 void bnxt_vf_reps_open(struct bnxt *bp)
271 {
272 	int i;
273 
274 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
275 		return;
276 
277 	for (i = 0; i < pci_num_vf(bp->pdev); i++)
278 		bnxt_vf_rep_open(bp->vf_reps[i]->dev);
279 }
280 
281 static void __bnxt_vf_reps_destroy(struct bnxt *bp)
282 {
283 	u16 num_vfs = pci_num_vf(bp->pdev);
284 	struct bnxt_vf_rep *vf_rep;
285 	int i;
286 
287 	for (i = 0; i < num_vfs; i++) {
288 		vf_rep = bp->vf_reps[i];
289 		if (vf_rep) {
290 			dst_release((struct dst_entry *)vf_rep->dst);
291 
292 			if (vf_rep->tx_cfa_action != CFA_HANDLE_INVALID)
293 				hwrm_cfa_vfr_free(bp, vf_rep->vf_idx);
294 
295 			if (vf_rep->dev) {
296 				/* if register_netdev failed, then netdev_ops
297 				 * would have been set to NULL
298 				 */
299 				if (vf_rep->dev->netdev_ops)
300 					unregister_netdev(vf_rep->dev);
301 				free_netdev(vf_rep->dev);
302 			}
303 		}
304 	}
305 
306 	kfree(bp->vf_reps);
307 	bp->vf_reps = NULL;
308 }
309 
310 void bnxt_vf_reps_destroy(struct bnxt *bp)
311 {
312 	bool closed = false;
313 
314 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
315 		return;
316 
317 	if (!bp->vf_reps)
318 		return;
319 
320 	/* Ensure that parent PF's and VF-reps' RX/TX has been quiesced
321 	 * before proceeding with VF-rep cleanup.
322 	 */
323 	rtnl_lock();
324 	if (netif_running(bp->dev)) {
325 		bnxt_close_nic(bp, false, false);
326 		closed = true;
327 	}
328 	/* un-publish cfa_code_map so that RX path can't see it anymore */
329 	kfree(bp->cfa_code_map);
330 	bp->cfa_code_map = NULL;
331 	bp->eswitch_mode = DEVLINK_ESWITCH_MODE_LEGACY;
332 
333 	if (closed)
334 		bnxt_open_nic(bp, false, false);
335 	rtnl_unlock();
336 
337 	/* Need to call vf_reps_destroy() outside of rntl_lock
338 	 * as unregister_netdev takes rtnl_lock
339 	 */
340 	__bnxt_vf_reps_destroy(bp);
341 }
342 
343 /* Use the OUI of the PF's perm addr and report the same mac addr
344  * for the same VF-rep each time
345  */
346 static void bnxt_vf_rep_eth_addr_gen(u8 *src_mac, u16 vf_idx, u8 *mac)
347 {
348 	u32 addr;
349 
350 	ether_addr_copy(mac, src_mac);
351 
352 	addr = jhash(src_mac, ETH_ALEN, 0) + vf_idx;
353 	mac[3] = (u8)(addr & 0xFF);
354 	mac[4] = (u8)((addr >> 8) & 0xFF);
355 	mac[5] = (u8)((addr >> 16) & 0xFF);
356 }
357 
358 static void bnxt_vf_rep_netdev_init(struct bnxt *bp, struct bnxt_vf_rep *vf_rep,
359 				    struct net_device *dev)
360 {
361 	struct net_device *pf_dev = bp->dev;
362 
363 	dev->netdev_ops = &bnxt_vf_rep_netdev_ops;
364 	dev->ethtool_ops = &bnxt_vf_rep_ethtool_ops;
365 	SWITCHDEV_SET_OPS(dev, &bnxt_vf_rep_switchdev_ops);
366 	/* Just inherit all the featues of the parent PF as the VF-R
367 	 * uses the RX/TX rings of the parent PF
368 	 */
369 	dev->hw_features = pf_dev->hw_features;
370 	dev->gso_partial_features = pf_dev->gso_partial_features;
371 	dev->vlan_features = pf_dev->vlan_features;
372 	dev->hw_enc_features = pf_dev->hw_enc_features;
373 	dev->features |= pf_dev->features;
374 	bnxt_vf_rep_eth_addr_gen(bp->pf.mac_addr, vf_rep->vf_idx,
375 				 dev->perm_addr);
376 	ether_addr_copy(dev->dev_addr, dev->perm_addr);
377 }
378 
379 static int bnxt_vf_reps_create(struct bnxt *bp)
380 {
381 	u16 *cfa_code_map = NULL, num_vfs = pci_num_vf(bp->pdev);
382 	struct bnxt_vf_rep *vf_rep;
383 	struct net_device *dev;
384 	int rc, i;
385 
386 	bp->vf_reps = kcalloc(num_vfs, sizeof(vf_rep), GFP_KERNEL);
387 	if (!bp->vf_reps)
388 		return -ENOMEM;
389 
390 	/* storage for cfa_code to vf-idx mapping */
391 	cfa_code_map = kmalloc(sizeof(*bp->cfa_code_map) * MAX_CFA_CODE,
392 			       GFP_KERNEL);
393 	if (!cfa_code_map) {
394 		rc = -ENOMEM;
395 		goto err;
396 	}
397 	for (i = 0; i < MAX_CFA_CODE; i++)
398 		cfa_code_map[i] = VF_IDX_INVALID;
399 
400 	for (i = 0; i < num_vfs; i++) {
401 		dev = alloc_etherdev(sizeof(*vf_rep));
402 		if (!dev) {
403 			rc = -ENOMEM;
404 			goto err;
405 		}
406 
407 		vf_rep = netdev_priv(dev);
408 		bp->vf_reps[i] = vf_rep;
409 		vf_rep->dev = dev;
410 		vf_rep->bp = bp;
411 		vf_rep->vf_idx = i;
412 		vf_rep->tx_cfa_action = CFA_HANDLE_INVALID;
413 
414 		/* get cfa handles from FW */
415 		rc = hwrm_cfa_vfr_alloc(bp, vf_rep->vf_idx,
416 					&vf_rep->tx_cfa_action,
417 					&vf_rep->rx_cfa_code);
418 		if (rc) {
419 			rc = -ENOLINK;
420 			goto err;
421 		}
422 		cfa_code_map[vf_rep->rx_cfa_code] = vf_rep->vf_idx;
423 
424 		vf_rep->dst = metadata_dst_alloc(0, METADATA_HW_PORT_MUX,
425 						 GFP_KERNEL);
426 		if (!vf_rep->dst) {
427 			rc = -ENOMEM;
428 			goto err;
429 		}
430 		/* only cfa_action is needed to mux a packet while TXing */
431 		vf_rep->dst->u.port_info.port_id = vf_rep->tx_cfa_action;
432 		vf_rep->dst->u.port_info.lower_dev = bp->dev;
433 
434 		bnxt_vf_rep_netdev_init(bp, vf_rep, dev);
435 		rc = register_netdev(dev);
436 		if (rc) {
437 			/* no need for unregister_netdev in cleanup */
438 			dev->netdev_ops = NULL;
439 			goto err;
440 		}
441 	}
442 
443 	/* publish cfa_code_map only after all VF-reps have been initialized */
444 	bp->cfa_code_map = cfa_code_map;
445 	bp->eswitch_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
446 	netif_keep_dst(bp->dev);
447 	return 0;
448 
449 err:
450 	netdev_info(bp->dev, "%s error=%d", __func__, rc);
451 	kfree(cfa_code_map);
452 	__bnxt_vf_reps_destroy(bp);
453 	return rc;
454 }
455 
456 /* Devlink related routines */
457 int bnxt_dl_eswitch_mode_get(struct devlink *devlink, u16 *mode)
458 {
459 	struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
460 
461 	*mode = bp->eswitch_mode;
462 	return 0;
463 }
464 
465 int bnxt_dl_eswitch_mode_set(struct devlink *devlink, u16 mode)
466 {
467 	struct bnxt *bp = bnxt_get_bp_from_dl(devlink);
468 	int rc = 0;
469 
470 	mutex_lock(&bp->sriov_lock);
471 	if (bp->eswitch_mode == mode) {
472 		netdev_info(bp->dev, "already in %s eswitch mode",
473 			    mode == DEVLINK_ESWITCH_MODE_LEGACY ?
474 			    "legacy" : "switchdev");
475 		rc = -EINVAL;
476 		goto done;
477 	}
478 
479 	switch (mode) {
480 	case DEVLINK_ESWITCH_MODE_LEGACY:
481 		bnxt_vf_reps_destroy(bp);
482 		break;
483 
484 	case DEVLINK_ESWITCH_MODE_SWITCHDEV:
485 		if (pci_num_vf(bp->pdev) == 0) {
486 			netdev_info(bp->dev,
487 				    "Enable VFs before setting switchdev mode");
488 			rc = -EPERM;
489 			goto done;
490 		}
491 		rc = bnxt_vf_reps_create(bp);
492 		break;
493 
494 	default:
495 		rc = -EINVAL;
496 		goto done;
497 	}
498 done:
499 	mutex_unlock(&bp->sriov_lock);
500 	return rc;
501 }
502 
503 #endif
504