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