qede_main.c 111.3 KB
Newer Older
Y
Yuval Mintz 已提交
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
/* QLogic qede NIC Driver
* Copyright (c) 2015 QLogic Corporation
*
* This software is available under the terms of the GNU General Public License
* (GPL) Version 2, available from the file COPYING in the main directory of
* this source tree.
*/

#include <linux/module.h>
#include <linux/pci.h>
#include <linux/version.h>
#include <linux/device.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/string.h>
#include <linux/dma-mapping.h>
#include <linux/interrupt.h>
#include <asm/byteorder.h>
#include <asm/param.h>
#include <linux/io.h>
#include <linux/netdev_features.h>
#include <linux/udp.h>
#include <linux/tcp.h>
27
#include <net/udp_tunnel.h>
Y
Yuval Mintz 已提交
28 29 30 31 32 33 34 35 36 37 38
#include <linux/ip.h>
#include <net/ipv6.h>
#include <net/tcp.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/pkt_sched.h>
#include <linux/ethtool.h>
#include <linux/in.h>
#include <linux/random.h>
#include <net/ip6_checksum.h>
#include <linux/bitops.h>
R
Ram Amrani 已提交
39
#include <linux/qed/qede_roce.h>
Y
Yuval Mintz 已提交
40 41
#include "qede.h"

42 43
static char version[] =
	"QLogic FastLinQ 4xxxx Ethernet Driver qede " DRV_MODULE_VERSION "\n";
Y
Yuval Mintz 已提交
44

45
MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx Ethernet Driver");
Y
Yuval Mintz 已提交
46 47 48 49 50 51 52 53 54 55
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_MODULE_VERSION);

static uint debug;
module_param(debug, uint, 0);
MODULE_PARM_DESC(debug, " Default debug msglevel");

static const struct qed_eth_ops *qed_ops;

#define CHIP_NUM_57980S_40		0x1634
56
#define CHIP_NUM_57980S_10		0x1666
Y
Yuval Mintz 已提交
57 58 59 60
#define CHIP_NUM_57980S_MF		0x1636
#define CHIP_NUM_57980S_100		0x1644
#define CHIP_NUM_57980S_50		0x1654
#define CHIP_NUM_57980S_25		0x1656
Y
Yuval Mintz 已提交
61
#define CHIP_NUM_57980S_IOV		0x1664
Y
Yuval Mintz 已提交
62 63 64 65 66 67 68 69

#ifndef PCI_DEVICE_ID_NX2_57980E
#define PCI_DEVICE_ID_57980S_40		CHIP_NUM_57980S_40
#define PCI_DEVICE_ID_57980S_10		CHIP_NUM_57980S_10
#define PCI_DEVICE_ID_57980S_MF		CHIP_NUM_57980S_MF
#define PCI_DEVICE_ID_57980S_100	CHIP_NUM_57980S_100
#define PCI_DEVICE_ID_57980S_50		CHIP_NUM_57980S_50
#define PCI_DEVICE_ID_57980S_25		CHIP_NUM_57980S_25
Y
Yuval Mintz 已提交
70
#define PCI_DEVICE_ID_57980S_IOV	CHIP_NUM_57980S_IOV
Y
Yuval Mintz 已提交
71 72
#endif

Y
Yuval Mintz 已提交
73 74 75 76 77
enum qede_pci_private {
	QEDE_PRIVATE_PF,
	QEDE_PRIVATE_VF
};

Y
Yuval Mintz 已提交
78
static const struct pci_device_id qede_pci_tbl[] = {
Y
Yuval Mintz 已提交
79 80 81 82 83 84
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_40), QEDE_PRIVATE_PF},
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_10), QEDE_PRIVATE_PF},
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_MF), QEDE_PRIVATE_PF},
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_100), QEDE_PRIVATE_PF},
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_50), QEDE_PRIVATE_PF},
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_25), QEDE_PRIVATE_PF},
85
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
86
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_IOV), QEDE_PRIVATE_VF},
87
#endif
Y
Yuval Mintz 已提交
88 89 90 91 92 93 94 95 96
	{ 0 }
};

MODULE_DEVICE_TABLE(pci, qede_pci_tbl);

static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id);

#define TX_TIMEOUT		(5 * HZ)

M
Mintz, Yuval 已提交
97 98 99
/* Utilize last protocol index for XDP */
#define XDP_PI	11

Y
Yuval Mintz 已提交
100
static void qede_remove(struct pci_dev *pdev);
M
Mintz, Yuval 已提交
101
static void qede_shutdown(struct pci_dev *pdev);
S
Sudarsana Kalluru 已提交
102
static void qede_link_update(void *dev, struct qed_link_output *link);
Y
Yuval Mintz 已提交
103

M
Mintz, Yuval 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116
/* The qede lock is used to protect driver state change and driver flows that
 * are not reentrant.
 */
void __qede_lock(struct qede_dev *edev)
{
	mutex_lock(&edev->qede_lock);
}

void __qede_unlock(struct qede_dev *edev)
{
	mutex_unlock(&edev->qede_lock);
}

Y
Yuval Mintz 已提交
117
#ifdef CONFIG_QED_SRIOV
118 119
static int qede_set_vf_vlan(struct net_device *ndev, int vf, u16 vlan, u8 qos,
			    __be16 vlan_proto)
Y
Yuval Mintz 已提交
120 121 122 123 124 125 126 127
{
	struct qede_dev *edev = netdev_priv(ndev);

	if (vlan > 4095) {
		DP_NOTICE(edev, "Illegal vlan value %d\n", vlan);
		return -EINVAL;
	}

128 129 130
	if (vlan_proto != htons(ETH_P_8021Q))
		return -EPROTONOSUPPORT;

Y
Yuval Mintz 已提交
131 132 133 134 135 136
	DP_VERBOSE(edev, QED_MSG_IOV, "Setting Vlan 0x%04x to VF [%d]\n",
		   vlan, vf);

	return edev->ops->iov->set_vlan(edev->cdev, vlan, vf);
}

Y
Yuval Mintz 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static int qede_set_vf_mac(struct net_device *ndev, int vfidx, u8 *mac)
{
	struct qede_dev *edev = netdev_priv(ndev);

	DP_VERBOSE(edev, QED_MSG_IOV,
		   "Setting MAC %02x:%02x:%02x:%02x:%02x:%02x to VF [%d]\n",
		   mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], vfidx);

	if (!is_valid_ether_addr(mac)) {
		DP_VERBOSE(edev, QED_MSG_IOV, "MAC address isn't valid\n");
		return -EINVAL;
	}

	return edev->ops->iov->set_mac(edev->cdev, mac, vfidx);
}

Y
Yuval Mintz 已提交
153 154 155
static int qede_sriov_configure(struct pci_dev *pdev, int num_vfs_param)
{
	struct qede_dev *edev = netdev_priv(pci_get_drvdata(pdev));
Y
Yuval Mintz 已提交
156 157
	struct qed_dev_info *qed_info = &edev->dev_info.common;
	int rc;
Y
Yuval Mintz 已提交
158 159 160

	DP_VERBOSE(edev, QED_MSG_IOV, "Requested %d VFs\n", num_vfs_param);

Y
Yuval Mintz 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	rc = edev->ops->iov->configure(edev->cdev, num_vfs_param);

	/* Enable/Disable Tx switching for PF */
	if ((rc == num_vfs_param) && netif_running(edev->ndev) &&
	    qed_info->mf_mode != QED_MF_NPAR && qed_info->tx_switching) {
		struct qed_update_vport_params params;

		memset(&params, 0, sizeof(params));
		params.vport_id = 0;
		params.update_tx_switching_flg = 1;
		params.tx_switching_flg = num_vfs_param ? 1 : 0;
		edev->ops->vport_update(edev->cdev, &params);
	}

	return rc;
Y
Yuval Mintz 已提交
176 177 178
}
#endif

Y
Yuval Mintz 已提交
179 180 181 182 183
static struct pci_driver qede_pci_driver = {
	.name = "qede",
	.id_table = qede_pci_tbl,
	.probe = qede_probe,
	.remove = qede_remove,
M
Mintz, Yuval 已提交
184
	.shutdown = qede_shutdown,
Y
Yuval Mintz 已提交
185 186 187
#ifdef CONFIG_QED_SRIOV
	.sriov_configure = qede_sriov_configure,
#endif
Y
Yuval Mintz 已提交
188 189
};

Y
Yuval Mintz 已提交
190
static void qede_force_mac(void *dev, u8 *mac, bool forced)
Y
Yuval Mintz 已提交
191 192 193
{
	struct qede_dev *edev = dev;

Y
Yuval Mintz 已提交
194 195 196 197
	/* MAC hints take effect only if we haven't set one already */
	if (is_valid_ether_addr(edev->ndev->dev_addr) && !forced)
		return;

Y
Yuval Mintz 已提交
198 199 200 201
	ether_addr_copy(edev->ndev->dev_addr, mac);
	ether_addr_copy(edev->primary_mac, mac);
}

S
Sudarsana Kalluru 已提交
202 203 204 205
static struct qed_eth_cb_ops qede_ll_ops = {
	{
		.link_update = qede_link_update,
	},
Y
Yuval Mintz 已提交
206
	.force_mac = qede_force_mac,
S
Sudarsana Kalluru 已提交
207 208
};

209 210 211 212 213 214 215
static int qede_netdev_event(struct notifier_block *this, unsigned long event,
			     void *ptr)
{
	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
	struct ethtool_drvinfo drvinfo;
	struct qede_dev *edev;

R
Ram Amrani 已提交
216
	if (event != NETDEV_CHANGENAME && event != NETDEV_CHANGEADDR)
217 218 219 220 221 222 223 224 225 226 227 228
		goto done;

	/* Check whether this is a qede device */
	if (!ndev || !ndev->ethtool_ops || !ndev->ethtool_ops->get_drvinfo)
		goto done;

	memset(&drvinfo, 0, sizeof(drvinfo));
	ndev->ethtool_ops->get_drvinfo(ndev, &drvinfo);
	if (strcmp(drvinfo.driver, "qede"))
		goto done;
	edev = netdev_priv(ndev);

R
Ram Amrani 已提交
229 230 231 232 233 234 235 236 237 238 239 240
	switch (event) {
	case NETDEV_CHANGENAME:
		/* Notify qed of the name change */
		if (!edev->ops || !edev->ops->common)
			goto done;
		edev->ops->common->set_id(edev->cdev, edev->ndev->name, "qede");
		break;
	case NETDEV_CHANGEADDR:
		edev = netdev_priv(ndev);
		qede_roce_event_changeaddr(edev);
		break;
	}
241 242 243 244 245 246 247 248 249

done:
	return NOTIFY_DONE;
}

static struct notifier_block qede_netdev_notifier = {
	.notifier_call = qede_netdev_event,
};

Y
Yuval Mintz 已提交
250 251 252 253 254
static
int __init qede_init(void)
{
	int ret;

Y
Yuval Mintz 已提交
255
	pr_info("qede_init: %s\n", version);
Y
Yuval Mintz 已提交
256

R
Rahul Verma 已提交
257
	qed_ops = qed_get_eth_ops();
Y
Yuval Mintz 已提交
258 259 260 261 262
	if (!qed_ops) {
		pr_notice("Failed to get qed ethtool operations\n");
		return -EINVAL;
	}

263 264 265 266 267 268 269 270 271 272
	/* Must register notifier before pci ops, since we might miss
	 * interface rename after pci probe and netdev registeration.
	 */
	ret = register_netdevice_notifier(&qede_netdev_notifier);
	if (ret) {
		pr_notice("Failed to register netdevice_notifier\n");
		qed_put_eth_ops();
		return -EINVAL;
	}

Y
Yuval Mintz 已提交
273 274 275
	ret = pci_register_driver(&qede_pci_driver);
	if (ret) {
		pr_notice("Failed to register driver\n");
276
		unregister_netdevice_notifier(&qede_netdev_notifier);
Y
Yuval Mintz 已提交
277 278 279 280 281 282 283 284 285
		qed_put_eth_ops();
		return -EINVAL;
	}

	return 0;
}

static void __exit qede_cleanup(void)
{
Y
Yuval Mintz 已提交
286 287
	if (debug & QED_LOG_INFO_MASK)
		pr_info("qede_cleanup called\n");
Y
Yuval Mintz 已提交
288

289
	unregister_netdevice_notifier(&qede_netdev_notifier);
Y
Yuval Mintz 已提交
290 291 292 293 294 295 296
	pci_unregister_driver(&qede_pci_driver);
	qed_put_eth_ops();
}

module_init(qede_init);
module_exit(qede_cleanup);

297 298 299 300 301 302 303
/* -------------------------------------------------------------------------
 * START OF FAST-PATH
 * -------------------------------------------------------------------------
 */

/* Unmap the data and free skb */
static int qede_free_tx_pkt(struct qede_dev *edev,
Y
Yuval Mintz 已提交
304
			    struct qede_tx_queue *txq, int *len)
305 306
{
	u16 idx = txq->sw_tx_cons & NUM_TX_BDS_MAX;
M
Mintz, Yuval 已提交
307
	struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
308 309 310 311
	struct eth_tx_1st_bd *first_bd;
	struct eth_tx_bd *tx_data_bd;
	int bds_consumed = 0;
	int nbds;
M
Mintz, Yuval 已提交
312
	bool data_split = txq->sw_tx_ring.skbs[idx].flags & QEDE_TSO_SPLIT_BD;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	int i, split_bd_len = 0;

	if (unlikely(!skb)) {
		DP_ERR(edev,
		       "skb is null for txq idx=%d txq->sw_tx_cons=%d txq->sw_tx_prod=%d\n",
		       idx, txq->sw_tx_cons, txq->sw_tx_prod);
		return -1;
	}

	*len = skb->len;

	first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);

	bds_consumed++;

	nbds = first_bd->data.nbds;

	if (data_split) {
		struct eth_tx_bd *split = (struct eth_tx_bd *)
			qed_chain_consume(&txq->tx_pbl);
		split_bd_len = BD_UNMAP_LEN(split);
		bds_consumed++;
	}
336 337
	dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
			 BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
338 339 340 341 342 343 344 345 346 347 348 349 350 351

	/* Unmap the data of the skb frags */
	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, bds_consumed++) {
		tx_data_bd = (struct eth_tx_bd *)
			qed_chain_consume(&txq->tx_pbl);
		dma_unmap_page(&edev->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
			       BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
	}

	while (bds_consumed++ < nbds)
		qed_chain_consume(&txq->tx_pbl);

	/* Free skb */
	dev_kfree_skb_any(skb);
M
Mintz, Yuval 已提交
352 353
	txq->sw_tx_ring.skbs[idx].skb = NULL;
	txq->sw_tx_ring.skbs[idx].flags = 0;
354 355 356 357 358

	return 0;
}

/* Unmap the data and free skb when mapping failed during start_xmit */
359
static void qede_free_failed_tx_pkt(struct qede_tx_queue *txq,
360
				    struct eth_tx_1st_bd *first_bd,
Y
Yuval Mintz 已提交
361
				    int nbd, bool data_split)
362 363
{
	u16 idx = txq->sw_tx_prod & NUM_TX_BDS_MAX;
M
Mintz, Yuval 已提交
364
	struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
365 366 367 368 369
	struct eth_tx_bd *tx_data_bd;
	int i, split_bd_len = 0;

	/* Return prod to its position before this skb was handled */
	qed_chain_set_prod(&txq->tx_pbl,
Y
Yuval Mintz 已提交
370
			   le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
371 372 373 374 375 376 377 378 379 380

	first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);

	if (data_split) {
		struct eth_tx_bd *split = (struct eth_tx_bd *)
					  qed_chain_produce(&txq->tx_pbl);
		split_bd_len = BD_UNMAP_LEN(split);
		nbd--;
	}

381
	dma_unmap_single(txq->dev, BD_UNMAP_ADDR(first_bd),
382
			 BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
383 384 385 386 387 388

	/* Unmap the data of the skb frags */
	for (i = 0; i < nbd; i++) {
		tx_data_bd = (struct eth_tx_bd *)
			qed_chain_produce(&txq->tx_pbl);
		if (tx_data_bd->nbytes)
389
			dma_unmap_page(txq->dev,
390 391 392 393 394 395
				       BD_UNMAP_ADDR(tx_data_bd),
				       BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
	}

	/* Return again prod to its position before this skb was handled */
	qed_chain_set_prod(&txq->tx_pbl,
Y
Yuval Mintz 已提交
396
			   le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
397 398 399

	/* Free skb */
	dev_kfree_skb_any(skb);
M
Mintz, Yuval 已提交
400 401
	txq->sw_tx_ring.skbs[idx].skb = NULL;
	txq->sw_tx_ring.skbs[idx].flags = 0;
402 403
}

404
static u32 qede_xmit_type(struct sk_buff *skb, int *ipv6_ext)
405 406 407 408 409 410 411 412 413 414 415 416
{
	u32 rc = XMIT_L4_CSUM;
	__be16 l3_proto;

	if (skb->ip_summed != CHECKSUM_PARTIAL)
		return XMIT_PLAIN;

	l3_proto = vlan_get_protocol(skb);
	if (l3_proto == htons(ETH_P_IPV6) &&
	    (ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
		*ipv6_ext = 1;

417
	if (skb->encapsulation) {
418
		rc |= XMIT_ENC;
419 420 421 422 423 424 425 426 427 428 429
		if (skb_is_gso(skb)) {
			unsigned short gso_type = skb_shinfo(skb)->gso_type;

			if ((gso_type & SKB_GSO_UDP_TUNNEL_CSUM) ||
			    (gso_type & SKB_GSO_GRE_CSUM))
				rc |= XMIT_ENC_GSO_L4_CSUM;

			rc |= XMIT_LSO;
			return rc;
		}
	}
430

431 432 433 434 435 436 437 438 439 440 441
	if (skb_is_gso(skb))
		rc |= XMIT_LSO;

	return rc;
}

static void qede_set_params_for_ipv6_ext(struct sk_buff *skb,
					 struct eth_tx_2nd_bd *second_bd,
					 struct eth_tx_3rd_bd *third_bd)
{
	u8 l4_proto;
Y
Yuval Mintz 已提交
442
	u16 bd2_bits1 = 0, bd2_bits2 = 0;
443

Y
Yuval Mintz 已提交
444
	bd2_bits1 |= (1 << ETH_TX_DATA_2ND_BD_IPV6_EXT_SHIFT);
445

Y
Yuval Mintz 已提交
446
	bd2_bits2 |= ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) &
447 448 449
		     ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_MASK)
		    << ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_SHIFT;

Y
Yuval Mintz 已提交
450
	bd2_bits1 |= (ETH_L4_PSEUDO_CSUM_CORRECT_LENGTH <<
451 452 453 454 455 456 457 458
		      ETH_TX_DATA_2ND_BD_L4_PSEUDO_CSUM_MODE_SHIFT);

	if (vlan_get_protocol(skb) == htons(ETH_P_IPV6))
		l4_proto = ipv6_hdr(skb)->nexthdr;
	else
		l4_proto = ip_hdr(skb)->protocol;

	if (l4_proto == IPPROTO_UDP)
Y
Yuval Mintz 已提交
459
		bd2_bits1 |= 1 << ETH_TX_DATA_2ND_BD_L4_UDP_SHIFT;
460

Y
Yuval Mintz 已提交
461
	if (third_bd)
462
		third_bd->data.bitfields |=
Y
Yuval Mintz 已提交
463 464 465
			cpu_to_le16(((tcp_hdrlen(skb) / 4) &
				ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_MASK) <<
				ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_SHIFT);
466

Y
Yuval Mintz 已提交
467
	second_bd->data.bitfields1 = cpu_to_le16(bd2_bits1);
468 469 470
	second_bd->data.bitfields2 = cpu_to_le16(bd2_bits2);
}

471
static int map_frag_to_bd(struct qede_tx_queue *txq,
Y
Yuval Mintz 已提交
472
			  skb_frag_t *frag, struct eth_tx_bd *bd)
473 474 475 476
{
	dma_addr_t mapping;

	/* Map skb non-linear frag data for DMA */
477
	mapping = skb_frag_dma_map(txq->dev, frag, 0,
Y
Yuval Mintz 已提交
478
				   skb_frag_size(frag), DMA_TO_DEVICE);
479
	if (unlikely(dma_mapping_error(txq->dev, mapping)))
480 481 482 483 484 485 486 487
		return -ENOMEM;

	/* Setup the data pointer of the frag data */
	BD_SET_UNMAP_ADDR_LEN(bd, mapping, skb_frag_size(frag));

	return 0;
}

488 489 490 491 492 493 494 495 496 497
static u16 qede_get_skb_hlen(struct sk_buff *skb, bool is_encap_pkt)
{
	if (is_encap_pkt)
		return (skb_inner_transport_header(skb) +
			inner_tcp_hdrlen(skb) - skb->data);
	else
		return (skb_transport_header(skb) +
			tcp_hdrlen(skb) - skb->data);
}

Y
Yuval Mintz 已提交
498 499
/* +2 for 1st BD for headers and 2nd BD for headlen (if required) */
#if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
500
static bool qede_pkt_req_lin(struct sk_buff *skb, u8 xmit_type)
Y
Yuval Mintz 已提交
501 502 503 504 505 506
{
	int allowed_frags = ETH_TX_MAX_BDS_PER_NON_LSO_PACKET - 1;

	if (xmit_type & XMIT_LSO) {
		int hlen;

507
		hlen = qede_get_skb_hlen(skb, xmit_type & XMIT_ENC);
Y
Yuval Mintz 已提交
508 509 510 511 512 513 514 515 516 517

		/* linear payload would require its own BD */
		if (skb_headlen(skb) > hlen)
			allowed_frags--;
	}

	return (skb_shinfo(skb)->nr_frags > allowed_frags);
}
#endif

M
Manish Chopra 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
static inline void qede_update_tx_producer(struct qede_tx_queue *txq)
{
	/* wmb makes sure that the BDs data is updated before updating the
	 * producer, otherwise FW may read old data from the BDs.
	 */
	wmb();
	barrier();
	writel(txq->tx_db.raw, txq->doorbell_addr);

	/* mmiowb is needed to synchronize doorbell writes from more than one
	 * processor. It guarantees that the write arrives to the device before
	 * the queue lock is released and another start_xmit is called (possibly
	 * on another CPU). Without this barrier, the next doorbell can bypass
	 * this doorbell. This is applicable to IA64/Altix systems.
	 */
	mmiowb();
}

M
Mintz, Yuval 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
static int qede_xdp_xmit(struct qede_dev *edev, struct qede_fastpath *fp,
			 struct sw_rx_data *metadata, u16 padding, u16 length)
{
	struct qede_tx_queue *txq = fp->xdp_tx;
	u16 idx = txq->sw_tx_prod & NUM_TX_BDS_MAX;
	struct eth_tx_1st_bd *first_bd;

	if (!qed_chain_get_elem_left(&txq->tx_pbl)) {
		txq->stopped_cnt++;
		return -ENOMEM;
	}

	first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);

	memset(first_bd, 0, sizeof(*first_bd));
	first_bd->data.bd_flags.bitfields =
	    BIT(ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT);
	first_bd->data.bitfields |=
	    (length & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
	    ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT;
	first_bd->data.nbds = 1;

	/* We can safely ignore the offset, as it's 0 for XDP */
	BD_SET_UNMAP_ADDR_LEN(first_bd, metadata->mapping + padding, length);

	/* Synchronize the buffer back to device, as program [probably]
	 * has changed it.
	 */
	dma_sync_single_for_device(&edev->pdev->dev,
				   metadata->mapping + padding,
				   length, PCI_DMA_TODEVICE);

	txq->sw_tx_ring.pages[idx] = metadata->data;
	txq->sw_tx_prod++;

	/* Mark the fastpath for future XDP doorbell */
	fp->xdp_xmit = 1;

	return 0;
}

577
/* Main transmit function */
Y
Yuval Mintz 已提交
578 579
static netdev_tx_t qede_start_xmit(struct sk_buff *skb,
				   struct net_device *ndev)
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
{
	struct qede_dev *edev = netdev_priv(ndev);
	struct netdev_queue *netdev_txq;
	struct qede_tx_queue *txq;
	struct eth_tx_1st_bd *first_bd;
	struct eth_tx_2nd_bd *second_bd = NULL;
	struct eth_tx_3rd_bd *third_bd = NULL;
	struct eth_tx_bd *tx_data_bd = NULL;
	u16 txq_index;
	u8 nbd = 0;
	dma_addr_t mapping;
	int rc, frag_idx = 0, ipv6_ext = 0;
	u8 xmit_type;
	u16 idx;
	u16 hlen;
595
	bool data_split = false;
596 597 598

	/* Get tx-queue context and netdev index */
	txq_index = skb_get_queue_mapping(skb);
599
	WARN_ON(txq_index >= QEDE_TSS_COUNT(edev));
M
Mintz, Yuval 已提交
600
	txq = edev->fp_array[edev->fp_num_rx + txq_index].txq;
601 602
	netdev_txq = netdev_get_tx_queue(ndev, txq_index);

Y
Yuval Mintz 已提交
603
	WARN_ON(qed_chain_get_elem_left(&txq->tx_pbl) < (MAX_SKB_FRAGS + 1));
604

605
	xmit_type = qede_xmit_type(skb, &ipv6_ext);
606

Y
Yuval Mintz 已提交
607
#if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
608
	if (qede_pkt_req_lin(skb, xmit_type)) {
Y
Yuval Mintz 已提交
609 610 611 612 613 614 615 616 617
		if (skb_linearize(skb)) {
			DP_NOTICE(edev,
				  "SKB linearization failed - silently dropping this SKB\n");
			dev_kfree_skb_any(skb);
			return NETDEV_TX_OK;
		}
	}
#endif

618 619
	/* Fill the entry in the SW ring and the BDs in the FW ring */
	idx = txq->sw_tx_prod & NUM_TX_BDS_MAX;
M
Mintz, Yuval 已提交
620
	txq->sw_tx_ring.skbs[idx].skb = skb;
621 622 623 624 625 626 627
	first_bd = (struct eth_tx_1st_bd *)
		   qed_chain_produce(&txq->tx_pbl);
	memset(first_bd, 0, sizeof(*first_bd));
	first_bd->data.bd_flags.bitfields =
		1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;

	/* Map skb linear data for DMA and set in the first BD */
628
	mapping = dma_map_single(txq->dev, skb->data,
629
				 skb_headlen(skb), DMA_TO_DEVICE);
630
	if (unlikely(dma_mapping_error(txq->dev, mapping))) {
631
		DP_NOTICE(edev, "SKB mapping failed\n");
632
		qede_free_failed_tx_pkt(txq, first_bd, 0, false);
M
Manish Chopra 已提交
633
		qede_update_tx_producer(txq);
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
		return NETDEV_TX_OK;
	}
	nbd++;
	BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));

	/* In case there is IPv6 with extension headers or LSO we need 2nd and
	 * 3rd BDs.
	 */
	if (unlikely((xmit_type & XMIT_LSO) | ipv6_ext)) {
		second_bd = (struct eth_tx_2nd_bd *)
			qed_chain_produce(&txq->tx_pbl);
		memset(second_bd, 0, sizeof(*second_bd));

		nbd++;
		third_bd = (struct eth_tx_3rd_bd *)
			qed_chain_produce(&txq->tx_pbl);
		memset(third_bd, 0, sizeof(*third_bd));

		nbd++;
		/* We need to fill in additional data in second_bd... */
		tx_data_bd = (struct eth_tx_bd *)second_bd;
	}

	if (skb_vlan_tag_present(skb)) {
		first_bd->data.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
		first_bd->data.bd_flags.bitfields |=
			1 << ETH_TX_1ST_BD_FLAGS_VLAN_INSERTION_SHIFT;
	}

	/* Fill the parsing flags & params according to the requested offload */
	if (xmit_type & XMIT_L4_CSUM) {
		/* We don't re-calculate IP checksum as it is already done by
		 * the upper stack
		 */
		first_bd->data.bd_flags.bitfields |=
			1 << ETH_TX_1ST_BD_FLAGS_L4_CSUM_SHIFT;

671 672 673
		if (xmit_type & XMIT_ENC) {
			first_bd->data.bd_flags.bitfields |=
				1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
Y
Yuval Mintz 已提交
674 675
			first_bd->data.bitfields |=
			    1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT;
676
		}
Y
Yuval Mintz 已提交
677

678 679 680 681 682 683 684 685
		/* Legacy FW had flipped behavior in regard to this bit -
		 * I.e., needed to set to prevent FW from touching encapsulated
		 * packets when it didn't need to.
		 */
		if (unlikely(txq->is_legacy))
			first_bd->data.bitfields ^=
			    1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT;

686 687 688 689 690 691 692 693 694 695 696 697 698 699
		/* If the packet is IPv6 with extension header, indicate that
		 * to FW and pass few params, since the device cracker doesn't
		 * support parsing IPv6 with extension header/s.
		 */
		if (unlikely(ipv6_ext))
			qede_set_params_for_ipv6_ext(skb, second_bd, third_bd);
	}

	if (xmit_type & XMIT_LSO) {
		first_bd->data.bd_flags.bitfields |=
			(1 << ETH_TX_1ST_BD_FLAGS_LSO_SHIFT);
		third_bd->data.lso_mss =
			cpu_to_le16(skb_shinfo(skb)->gso_size);

700 701 702
		if (unlikely(xmit_type & XMIT_ENC)) {
			first_bd->data.bd_flags.bitfields |=
				1 << ETH_TX_1ST_BD_FLAGS_TUNN_IP_CSUM_SHIFT;
703 704 705 706 707 708

			if (xmit_type & XMIT_ENC_GSO_L4_CSUM) {
				u8 tmp = ETH_TX_1ST_BD_FLAGS_TUNN_L4_CSUM_SHIFT;

				first_bd->data.bd_flags.bitfields |= 1 << tmp;
			}
709 710 711 712 713 714
			hlen = qede_get_skb_hlen(skb, true);
		} else {
			first_bd->data.bd_flags.bitfields |=
				1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
			hlen = qede_get_skb_hlen(skb, false);
		}
715 716 717

		/* @@@TBD - if will not be removed need to check */
		third_bd->data.bitfields |=
Y
Yuval Mintz 已提交
718
			cpu_to_le16((1 << ETH_TX_DATA_3RD_BD_HDR_NBD_SHIFT));
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

		/* Make life easier for FW guys who can't deal with header and
		 * data on same BD. If we need to split, use the second bd...
		 */
		if (unlikely(skb_headlen(skb) > hlen)) {
			DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
				   "TSO split header size is %d (%x:%x)\n",
				   first_bd->nbytes, first_bd->addr.hi,
				   first_bd->addr.lo);

			mapping = HILO_U64(le32_to_cpu(first_bd->addr.hi),
					   le32_to_cpu(first_bd->addr.lo)) +
					   hlen;

			BD_SET_UNMAP_ADDR_LEN(tx_data_bd, mapping,
					      le16_to_cpu(first_bd->nbytes) -
					      hlen);

			/* this marks the BD as one that has no
			 * individual mapping
			 */
M
Mintz, Yuval 已提交
740
			txq->sw_tx_ring.skbs[idx].flags |= QEDE_TSO_SPLIT_BD;
741 742 743 744 745 746

			first_bd->nbytes = cpu_to_le16(hlen);

			tx_data_bd = (struct eth_tx_bd *)third_bd;
			data_split = true;
		}
Y
Yuval Mintz 已提交
747 748 749 750
	} else {
		first_bd->data.bitfields |=
		    (skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
		    ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT;
751 752 753 754 755
	}

	/* Handle fragmented skb */
	/* special handle for frags inside 2nd and 3rd bds.. */
	while (tx_data_bd && frag_idx < skb_shinfo(skb)->nr_frags) {
756
		rc = map_frag_to_bd(txq,
757 758 759
				    &skb_shinfo(skb)->frags[frag_idx],
				    tx_data_bd);
		if (rc) {
760
			qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
M
Manish Chopra 已提交
761
			qede_update_tx_producer(txq);
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
			return NETDEV_TX_OK;
		}

		if (tx_data_bd == (struct eth_tx_bd *)second_bd)
			tx_data_bd = (struct eth_tx_bd *)third_bd;
		else
			tx_data_bd = NULL;

		frag_idx++;
	}

	/* map last frags into 4th, 5th .... */
	for (; frag_idx < skb_shinfo(skb)->nr_frags; frag_idx++, nbd++) {
		tx_data_bd = (struct eth_tx_bd *)
			     qed_chain_produce(&txq->tx_pbl);

		memset(tx_data_bd, 0, sizeof(*tx_data_bd));

780
		rc = map_frag_to_bd(txq,
781 782 783
				    &skb_shinfo(skb)->frags[frag_idx],
				    tx_data_bd);
		if (rc) {
784
			qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
M
Manish Chopra 已提交
785
			qede_update_tx_producer(txq);
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
			return NETDEV_TX_OK;
		}
	}

	/* update the first BD with the actual num BDs */
	first_bd->data.nbds = nbd;

	netdev_tx_sent_queue(netdev_txq, skb->len);

	skb_tx_timestamp(skb);

	/* Advance packet producer only before sending the packet since mapping
	 * of pages may fail.
	 */
	txq->sw_tx_prod++;

	/* 'next page' entries are counted in the producer value */
	txq->tx_db.data.bd_prod =
		cpu_to_le16(qed_chain_get_prod_idx(&txq->tx_pbl));

806
	if (!skb->xmit_more || netif_xmit_stopped(netdev_txq))
M
Manish Chopra 已提交
807
		qede_update_tx_producer(txq);
808 809 810

	if (unlikely(qed_chain_get_elem_left(&txq->tx_pbl)
		      < (MAX_SKB_FRAGS + 1))) {
811 812 813
		if (skb->xmit_more)
			qede_update_tx_producer(txq);

814
		netif_tx_stop_queue(netdev_txq);
815
		txq->stopped_cnt++;
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
		DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
			   "Stop queue was called\n");
		/* paired memory barrier is in qede_tx_int(), we have to keep
		 * ordering of set_bit() in netif_tx_stop_queue() and read of
		 * fp->bd_tx_cons
		 */
		smp_mb();

		if (qed_chain_get_elem_left(&txq->tx_pbl)
		     >= (MAX_SKB_FRAGS + 1) &&
		    (edev->state == QEDE_STATE_OPEN)) {
			netif_tx_wake_queue(netdev_txq);
			DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
				   "Wake queue was called\n");
		}
	}

	return NETDEV_TX_OK;
}

836
int qede_txq_has_work(struct qede_tx_queue *txq)
837 838 839 840 841 842 843 844 845 846 847 848
{
	u16 hw_bd_cons;

	/* Tell compiler that consumer and producer can change */
	barrier();
	hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
	if (qed_chain_get_cons_idx(&txq->tx_pbl) == hw_bd_cons + 1)
		return 0;

	return hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl);
}

M
Mintz, Yuval 已提交
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
static void qede_xdp_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
{
	struct eth_tx_1st_bd *bd;
	u16 hw_bd_cons;

	hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
	barrier();

	while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
		bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);

		dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(bd),
				 PAGE_SIZE, DMA_BIDIRECTIONAL);
		__free_page(txq->sw_tx_ring.pages[txq->sw_tx_cons &
						  NUM_TX_BDS_MAX]);

		txq->sw_tx_cons++;
		txq->xmit_pkts++;
	}
}

Y
Yuval Mintz 已提交
870
static int qede_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
{
	struct netdev_queue *netdev_txq;
	u16 hw_bd_cons;
	unsigned int pkts_compl = 0, bytes_compl = 0;
	int rc;

	netdev_txq = netdev_get_tx_queue(edev->ndev, txq->index);

	hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
	barrier();

	while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
		int len = 0;

		rc = qede_free_tx_pkt(edev, txq, &len);
		if (rc) {
			DP_NOTICE(edev, "hw_bd_cons = %d, chain_cons=%d\n",
				  hw_bd_cons,
				  qed_chain_get_cons_idx(&txq->tx_pbl));
			break;
		}

		bytes_compl += len;
		pkts_compl++;
		txq->sw_tx_cons++;
896
		txq->xmit_pkts++;
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	}

	netdev_tx_completed_queue(netdev_txq, pkts_compl, bytes_compl);

	/* Need to make the tx_bd_cons update visible to start_xmit()
	 * before checking for netif_tx_queue_stopped().  Without the
	 * memory barrier, there is a small possibility that
	 * start_xmit() will miss it and cause the queue to be stopped
	 * forever.
	 * On the other hand we need an rmb() here to ensure the proper
	 * ordering of bit testing in the following
	 * netif_tx_queue_stopped(txq) call.
	 */
	smp_mb();

	if (unlikely(netif_tx_queue_stopped(netdev_txq))) {
		/* Taking tx_lock is needed to prevent reenabling the queue
		 * while it's empty. This could have happen if rx_action() gets
		 * suspended in qede_tx_int() after the condition before
		 * netif_tx_wake_queue(), while tx_action (qede_start_xmit()):
		 *
		 * stops the queue->sees fresh tx_bd_cons->releases the queue->
		 * sends some packets consuming the whole queue again->
		 * stops the queue
		 */

		__netif_tx_lock(netdev_txq, smp_processor_id());

		if ((netif_tx_queue_stopped(netdev_txq)) &&
		    (edev->state == QEDE_STATE_OPEN) &&
		    (qed_chain_get_elem_left(&txq->tx_pbl)
		      >= (MAX_SKB_FRAGS + 1))) {
			netif_tx_wake_queue(netdev_txq);
			DP_VERBOSE(edev, NETIF_MSG_TX_DONE,
				   "Wake queue was called\n");
		}

		__netif_tx_unlock(netdev_txq);
	}

	return 0;
}

940
bool qede_has_rx_work(struct qede_rx_queue *rxq)
941 942 943 944 945 946 947 948 949 950 951 952
{
	u16 hw_comp_cons, sw_comp_cons;

	/* Tell compiler that status block fields can change */
	barrier();

	hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
	sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);

	return hw_comp_cons != sw_comp_cons;
}

953 954 955 956 957 958
static inline void qede_rx_bd_ring_consume(struct qede_rx_queue *rxq)
{
	qed_chain_consume(&rxq->rx_bd_ring);
	rxq->sw_rx_cons++;
}

Y
Yuval Mintz 已提交
959 960
/* This function reuses the buffer(from an offset) from
 * consumer index to producer index in the bd ring
961
 */
962
static inline void qede_reuse_page(struct qede_rx_queue *rxq,
Y
Yuval Mintz 已提交
963
				   struct sw_rx_data *curr_cons)
964 965
{
	struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
Y
Yuval Mintz 已提交
966 967
	struct sw_rx_data *curr_prod;
	dma_addr_t new_mapping;
968

Y
Yuval Mintz 已提交
969 970
	curr_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
	*curr_prod = *curr_cons;
971

Y
Yuval Mintz 已提交
972 973 974 975
	new_mapping = curr_prod->mapping + curr_prod->page_offset;

	rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(new_mapping));
	rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(new_mapping));
976 977

	rxq->sw_rx_prod++;
Y
Yuval Mintz 已提交
978 979 980
	curr_cons->data = NULL;
}

981 982 983
/* In case of allocation failures reuse buffers
 * from consumer index to produce buffers for firmware
 */
984
void qede_recycle_rx_bd_ring(struct qede_rx_queue *rxq, u8 count)
985 986 987 988 989
{
	struct sw_rx_data *curr_cons;

	for (; count > 0; count--) {
		curr_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
990
		qede_reuse_page(rxq, curr_cons);
991 992 993 994
		qede_rx_bd_ring_consume(rxq);
	}
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
static int qede_alloc_rx_buffer(struct qede_rx_queue *rxq)
{
	struct sw_rx_data *sw_rx_data;
	struct eth_rx_bd *rx_bd;
	dma_addr_t mapping;
	struct page *data;

	data = alloc_pages(GFP_ATOMIC, 0);
	if (unlikely(!data))
		return -ENOMEM;

	/* Map the entire page as it would be used
	 * for multiple RX buffer segment size mapping.
	 */
	mapping = dma_map_page(rxq->dev, data, 0,
M
Mintz, Yuval 已提交
1010
			       PAGE_SIZE, rxq->data_direction);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	if (unlikely(dma_mapping_error(rxq->dev, mapping))) {
		__free_page(data);
		return -ENOMEM;
	}

	sw_rx_data = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
	sw_rx_data->page_offset = 0;
	sw_rx_data->data = data;
	sw_rx_data->mapping = mapping;

	/* Advance PROD and get BD pointer */
	rx_bd = (struct eth_rx_bd *)qed_chain_produce(&rxq->rx_bd_ring);
	WARN_ON(!rx_bd);
	rx_bd->addr.hi = cpu_to_le32(upper_32_bits(mapping));
	rx_bd->addr.lo = cpu_to_le32(lower_32_bits(mapping));

	rxq->sw_rx_prod++;

	return 0;
}

static inline int qede_realloc_rx_buffer(struct qede_rx_queue *rxq,
Y
Yuval Mintz 已提交
1033 1034 1035 1036 1037 1038
					 struct sw_rx_data *curr_cons)
{
	/* Move to the next segment in the page */
	curr_cons->page_offset += rxq->rx_buf_seg_size;

	if (curr_cons->page_offset == PAGE_SIZE) {
1039
		if (unlikely(qede_alloc_rx_buffer(rxq))) {
1040 1041 1042 1043 1044
			/* Since we failed to allocate new buffer
			 * current buffer can be used again.
			 */
			curr_cons->page_offset -= rxq->rx_buf_seg_size;

Y
Yuval Mintz 已提交
1045
			return -ENOMEM;
1046
		}
Y
Yuval Mintz 已提交
1047

1048
		dma_unmap_page(rxq->dev, curr_cons->mapping,
M
Mintz, Yuval 已提交
1049
			       PAGE_SIZE, rxq->data_direction);
Y
Yuval Mintz 已提交
1050 1051 1052 1053 1054
	} else {
		/* Increment refcount of the page as we don't want
		 * network stack to take the ownership of the page
		 * which can be recycled multiple times by the driver.
		 */
1055
		page_ref_inc(curr_cons->data);
1056
		qede_reuse_page(rxq, curr_cons);
Y
Yuval Mintz 已提交
1057 1058 1059
	}

	return 0;
1060 1061
}

1062
void qede_update_rx_prod(struct qede_dev *edev, struct qede_rx_queue *rxq)
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
{
	u16 bd_prod = qed_chain_get_prod_idx(&rxq->rx_bd_ring);
	u16 cqe_prod = qed_chain_get_prod_idx(&rxq->rx_comp_ring);
	struct eth_rx_prod_data rx_prods = {0};

	/* Update producers */
	rx_prods.bd_prod = cpu_to_le16(bd_prod);
	rx_prods.cqe_prod = cpu_to_le16(cqe_prod);

	/* Make sure that the BD and SGE data is updated before updating the
	 * producers since FW might read the BD/SGE right after the producer
	 * is updated.
	 */
	wmb();

	internal_ram_wr(rxq->hw_rxq_prod_addr, sizeof(rx_prods),
			(u32 *)&rx_prods);

	/* mmiowb is needed to synchronize doorbell writes from more than one
	 * processor. It guarantees that the write arrives to the device before
	 * the napi lock is released and another qede_poll is called (possibly
	 * on another CPU). Without this barrier, the next doorbell can bypass
	 * this doorbell. This is applicable to IA64/Altix systems.
	 */
	mmiowb();
}

1090
static void qede_get_rxhash(struct sk_buff *skb, u8 bitfields, __le32 rss_hash)
1091
{
1092
	enum pkt_hash_types hash_type = PKT_HASH_TYPE_NONE;
1093
	enum rss_hash_type htype;
1094
	u32 hash = 0;
1095 1096

	htype = GET_FIELD(bitfields, ETH_FAST_PATH_RX_REG_CQE_RSS_HASH_TYPE);
1097 1098 1099 1100 1101
	if (htype) {
		hash_type = ((htype == RSS_HASH_TYPE_IPV4) ||
			     (htype == RSS_HASH_TYPE_IPV6)) ?
			    PKT_HASH_TYPE_L3 : PKT_HASH_TYPE_L4;
		hash = le32_to_cpu(rss_hash);
1102
	}
1103
	skb_set_hash(skb, hash, hash_type);
1104 1105 1106 1107 1108 1109 1110 1111
}

static void qede_set_skb_csum(struct sk_buff *skb, u8 csum_flag)
{
	skb_checksum_none_assert(skb);

	if (csum_flag & QEDE_CSUM_UNNECESSARY)
		skb->ip_summed = CHECKSUM_UNNECESSARY;
1112 1113 1114

	if (csum_flag & QEDE_TUNN_CSUM_UNNECESSARY)
		skb->csum_level = 1;
1115 1116 1117 1118
}

static inline void qede_skb_receive(struct qede_dev *edev,
				    struct qede_fastpath *fp,
1119
				    struct qede_rx_queue *rxq,
Y
Yuval Mintz 已提交
1120
				    struct sk_buff *skb, u16 vlan_tag)
1121 1122
{
	if (vlan_tag)
Y
Yuval Mintz 已提交
1123
		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
1124 1125

	napi_gro_receive(&fp->napi, skb);
M
Mintz, Yuval 已提交
1126
	fp->rxq->rcv_pkts++;
1127 1128
}

1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
static void qede_set_gro_params(struct qede_dev *edev,
				struct sk_buff *skb,
				struct eth_fast_path_rx_tpa_start_cqe *cqe)
{
	u16 parsing_flags = le16_to_cpu(cqe->pars_flags.flags);

	if (((parsing_flags >> PARSING_AND_ERR_FLAGS_L3TYPE_SHIFT) &
	    PARSING_AND_ERR_FLAGS_L3TYPE_MASK) == 2)
		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
	else
		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;

	skb_shinfo(skb)->gso_size = __le16_to_cpu(cqe->len_on_first_bd) -
					cqe->header_len;
}

static int qede_fill_frag_skb(struct qede_dev *edev,
			      struct qede_rx_queue *rxq,
Y
Yuval Mintz 已提交
1147
			      u8 tpa_agg_index, u16 len_on_bd)
1148 1149 1150 1151 1152 1153
{
	struct sw_rx_data *current_bd = &rxq->sw_rx_ring[rxq->sw_rx_cons &
							 NUM_RX_BDS_MAX];
	struct qede_agg_info *tpa_info = &rxq->tpa_info[tpa_agg_index];
	struct sk_buff *skb = tpa_info->skb;

1154
	if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
1155 1156 1157 1158 1159 1160 1161
		goto out;

	/* Add one frag and update the appropriate fields in the skb */
	skb_fill_page_desc(skb, tpa_info->frag_id++,
			   current_bd->data, current_bd->page_offset,
			   len_on_bd);

1162
	if (unlikely(qede_realloc_rx_buffer(rxq, current_bd))) {
1163 1164 1165
		/* Incr page ref count to reuse on allocation failure
		 * so that it doesn't get freed while freeing SKB.
		 */
1166
		page_ref_inc(current_bd->data);
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
		goto out;
	}

	qed_chain_consume(&rxq->rx_bd_ring);
	rxq->sw_rx_cons++;

	skb->data_len += len_on_bd;
	skb->truesize += rxq->rx_buf_seg_size;
	skb->len += len_on_bd;

	return 0;

out:
1180
	tpa_info->state = QEDE_AGG_STATE_ERROR;
1181 1182
	qede_recycle_rx_bd_ring(rxq, 1);

1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
	return -ENOMEM;
}

static void qede_tpa_start(struct qede_dev *edev,
			   struct qede_rx_queue *rxq,
			   struct eth_fast_path_rx_tpa_start_cqe *cqe)
{
	struct qede_agg_info *tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
	struct eth_rx_bd *rx_bd_cons = qed_chain_consume(&rxq->rx_bd_ring);
	struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
1193 1194
	struct sw_rx_data *replace_buf = &tpa_info->buffer;
	dma_addr_t mapping = tpa_info->buffer_mapping;
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	struct sw_rx_data *sw_rx_data_cons;
	struct sw_rx_data *sw_rx_data_prod;

	sw_rx_data_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
	sw_rx_data_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];

	/* Use pre-allocated replacement buffer - we can't release the agg.
	 * start until its over and we don't want to risk allocation failing
	 * here, so re-allocate when aggregation will be over.
	 */
M
Manish Chopra 已提交
1205
	sw_rx_data_prod->mapping = replace_buf->mapping;
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

	sw_rx_data_prod->data = replace_buf->data;
	rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(mapping));
	rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(mapping));
	sw_rx_data_prod->page_offset = replace_buf->page_offset;

	rxq->sw_rx_prod++;

	/* move partial skb from cons to pool (don't unmap yet)
	 * save mapping, incase we drop the packet later on.
	 */
1217
	tpa_info->buffer = *sw_rx_data_cons;
1218 1219 1220
	mapping = HILO_U64(le32_to_cpu(rx_bd_cons->addr.hi),
			   le32_to_cpu(rx_bd_cons->addr.lo));

1221
	tpa_info->buffer_mapping = mapping;
1222 1223 1224 1225 1226 1227 1228 1229 1230
	rxq->sw_rx_cons++;

	/* set tpa state to start only if we are able to allocate skb
	 * for this aggregation, otherwise mark as error and aggregation will
	 * be dropped
	 */
	tpa_info->skb = netdev_alloc_skb(edev->ndev,
					 le16_to_cpu(cqe->len_on_first_bd));
	if (unlikely(!tpa_info->skb)) {
1231
		DP_NOTICE(edev, "Failed to allocate SKB for gro\n");
1232
		tpa_info->state = QEDE_AGG_STATE_ERROR;
1233
		goto cons_buf;
1234 1235 1236
	}

	/* Start filling in the aggregation info */
1237
	skb_put(tpa_info->skb, le16_to_cpu(cqe->len_on_first_bd));
1238
	tpa_info->frag_id = 0;
1239
	tpa_info->state = QEDE_AGG_STATE_START;
1240

1241 1242 1243
	/* Store some information from first CQE */
	tpa_info->start_cqe_placement_offset = cqe->placement_offset;
	tpa_info->start_cqe_bd_len = le16_to_cpu(cqe->len_on_first_bd);
1244 1245
	if ((le16_to_cpu(cqe->pars_flags.flags) >>
	     PARSING_AND_ERR_FLAGS_TAG8021QEXIST_SHIFT) &
1246
	    PARSING_AND_ERR_FLAGS_TAG8021QEXIST_MASK)
1247 1248 1249 1250
		tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
	else
		tpa_info->vlan_tag = 0;

1251 1252
	qede_get_rxhash(tpa_info->skb, cqe->bitfields, cqe->rss_hash);

1253 1254 1255
	/* This is needed in order to enable forwarding support */
	qede_set_gro_params(edev, tpa_info->skb, cqe);

1256
cons_buf: /* We still need to handle bd_len_list to consume buffers */
1257 1258 1259 1260 1261 1262 1263
	if (likely(cqe->ext_bd_len_list[0]))
		qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
				   le16_to_cpu(cqe->ext_bd_len_list[0]));

	if (unlikely(cqe->ext_bd_len_list[1])) {
		DP_ERR(edev,
		       "Unlikely - got a TPA aggregation with more than one ext_bd_len_list entry in the TPA start\n");
1264
		tpa_info->state = QEDE_AGG_STATE_ERROR;
1265 1266 1267
	}
}

1268
#ifdef CONFIG_INET
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
static void qede_gro_ip_csum(struct sk_buff *skb)
{
	const struct iphdr *iph = ip_hdr(skb);
	struct tcphdr *th;

	skb_set_transport_header(skb, sizeof(struct iphdr));
	th = tcp_hdr(skb);

	th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
				  iph->saddr, iph->daddr, 0);

	tcp_gro_complete(skb);
}

static void qede_gro_ipv6_csum(struct sk_buff *skb)
{
	struct ipv6hdr *iph = ipv6_hdr(skb);
	struct tcphdr *th;

	skb_set_transport_header(skb, sizeof(struct ipv6hdr));
	th = tcp_hdr(skb);

	th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
				  &iph->saddr, &iph->daddr, 0);
	tcp_gro_complete(skb);
}
1295
#endif
1296 1297 1298 1299 1300 1301

static void qede_gro_receive(struct qede_dev *edev,
			     struct qede_fastpath *fp,
			     struct sk_buff *skb,
			     u16 vlan_tag)
{
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
	/* FW can send a single MTU sized packet from gro flow
	 * due to aggregation timeout/last segment etc. which
	 * is not expected to be a gro packet. If a skb has zero
	 * frags then simply push it in the stack as non gso skb.
	 */
	if (unlikely(!skb->data_len)) {
		skb_shinfo(skb)->gso_type = 0;
		skb_shinfo(skb)->gso_size = 0;
		goto send_skb;
	}

1313
#ifdef CONFIG_INET
1314
	if (skb_shinfo(skb)->gso_size) {
1315
		skb_reset_network_header(skb);
1316

1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
		switch (skb->protocol) {
		case htons(ETH_P_IP):
			qede_gro_ip_csum(skb);
			break;
		case htons(ETH_P_IPV6):
			qede_gro_ipv6_csum(skb);
			break;
		default:
			DP_ERR(edev,
			       "Error: FW GRO supports only IPv4/IPv6, not 0x%04x\n",
			       ntohs(skb->protocol));
		}
	}
1330
#endif
1331 1332

send_skb:
1333
	skb_record_rx_queue(skb, fp->rxq->rxq_id);
1334
	qede_skb_receive(edev, fp, fp->rxq, skb, vlan_tag);
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
}

static inline void qede_tpa_cont(struct qede_dev *edev,
				 struct qede_rx_queue *rxq,
				 struct eth_fast_path_rx_tpa_cont_cqe *cqe)
{
	int i;

	for (i = 0; cqe->len_list[i]; i++)
		qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
				   le16_to_cpu(cqe->len_list[i]));

	if (unlikely(i > 1))
		DP_ERR(edev,
		       "Strange - TPA cont with more than a single len_list entry\n");
}

static void qede_tpa_end(struct qede_dev *edev,
			 struct qede_fastpath *fp,
			 struct eth_fast_path_rx_tpa_end_cqe *cqe)
{
	struct qede_rx_queue *rxq = fp->rxq;
	struct qede_agg_info *tpa_info;
	struct sk_buff *skb;
	int i;

	tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
	skb = tpa_info->skb;

	for (i = 0; cqe->len_list[i]; i++)
		qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
				   le16_to_cpu(cqe->len_list[i]));
	if (unlikely(i > 1))
		DP_ERR(edev,
		       "Strange - TPA emd with more than a single len_list entry\n");

1371
	if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
		goto err;

	/* Sanity */
	if (unlikely(cqe->num_of_bds != tpa_info->frag_id + 1))
		DP_ERR(edev,
		       "Strange - TPA had %02x BDs, but SKB has only %d frags\n",
		       cqe->num_of_bds, tpa_info->frag_id);
	if (unlikely(skb->len != le16_to_cpu(cqe->total_packet_len)))
		DP_ERR(edev,
		       "Strange - total packet len [cqe] is %4x but SKB has len %04x\n",
		       le16_to_cpu(cqe->total_packet_len), skb->len);

	memcpy(skb->data,
1385 1386 1387
	       page_address(tpa_info->buffer.data) +
	       tpa_info->start_cqe_placement_offset +
	       tpa_info->buffer.page_offset, tpa_info->start_cqe_bd_len);
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399

	/* Finalize the SKB */
	skb->protocol = eth_type_trans(skb, edev->ndev);
	skb->ip_summed = CHECKSUM_UNNECESSARY;

	/* tcp_gro_complete() will copy NAPI_GRO_CB(skb)->count
	 * to skb_shinfo(skb)->gso_segs
	 */
	NAPI_GRO_CB(skb)->count = le16_to_cpu(cqe->num_of_coalesced_segs);

	qede_gro_receive(edev, fp, skb, tpa_info->vlan_tag);

1400
	tpa_info->state = QEDE_AGG_STATE_NONE;
1401 1402 1403

	return;
err:
1404
	tpa_info->state = QEDE_AGG_STATE_NONE;
1405 1406 1407 1408
	dev_kfree_skb_any(tpa_info->skb);
	tpa_info->skb = NULL;
}

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
static bool qede_tunn_exist(u16 flag)
{
	return !!(flag & (PARSING_AND_ERR_FLAGS_TUNNELEXIST_MASK <<
			  PARSING_AND_ERR_FLAGS_TUNNELEXIST_SHIFT));
}

static u8 qede_check_tunn_csum(u16 flag)
{
	u16 csum_flag = 0;
	u8 tcsum = 0;

	if (flag & (PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_MASK <<
		    PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_SHIFT))
		csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_MASK <<
			     PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_SHIFT;

	if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
		    PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
		csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
			     PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
		tcsum = QEDE_TUNN_CSUM_UNNECESSARY;
	}

	csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_MASK <<
		     PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_SHIFT |
		     PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
		     PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;

	if (csum_flag & flag)
		return QEDE_CSUM_ERROR;

	return QEDE_CSUM_UNNECESSARY | tcsum;
}

static u8 qede_check_notunn_csum(u16 flag)
1444 1445 1446 1447
{
	u16 csum_flag = 0;
	u8 csum = 0;

1448 1449
	if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
		    PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
		csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
			     PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
		csum = QEDE_CSUM_UNNECESSARY;
	}

	csum_flag |= PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
		     PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;

	if (csum_flag & flag)
		return QEDE_CSUM_ERROR;

	return csum;
}

1464 1465 1466 1467 1468 1469 1470 1471
static u8 qede_check_csum(u16 flag)
{
	if (!qede_tunn_exist(flag))
		return qede_check_notunn_csum(flag);
	else
		return qede_check_tunn_csum(flag);
}

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
static bool qede_pkt_is_ip_fragmented(struct eth_fast_path_rx_reg_cqe *cqe,
				      u16 flag)
{
	u8 tun_pars_flg = cqe->tunnel_pars_flags.flags;

	if ((tun_pars_flg & (ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_MASK <<
			     ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_SHIFT)) ||
	    (flag & (PARSING_AND_ERR_FLAGS_IPV4FRAG_MASK <<
		     PARSING_AND_ERR_FLAGS_IPV4FRAG_SHIFT)))
		return true;

	return false;
}

M
Mintz, Yuval 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
/* Return true iff packet is to be passed to stack */
static bool qede_rx_xdp(struct qede_dev *edev,
			struct qede_fastpath *fp,
			struct qede_rx_queue *rxq,
			struct bpf_prog *prog,
			struct sw_rx_data *bd,
			struct eth_fast_path_rx_reg_cqe *cqe)
{
	u16 len = le16_to_cpu(cqe->len_on_first_bd);
	struct xdp_buff xdp;
	enum xdp_action act;

	xdp.data = page_address(bd->data) + cqe->placement_offset;
	xdp.data_end = xdp.data + len;
1500 1501 1502 1503 1504 1505

	/* Queues always have a full reset currently, so for the time
	 * being until there's atomic program replace just mark read
	 * side for map helpers.
	 */
	rcu_read_lock();
M
Mintz, Yuval 已提交
1506
	act = bpf_prog_run_xdp(prog, &xdp);
1507
	rcu_read_unlock();
M
Mintz, Yuval 已提交
1508 1509 1510 1511 1512 1513 1514 1515

	if (act == XDP_PASS)
		return true;

	/* Count number of packets not to be passed to stack */
	rxq->xdp_no_pass++;

	switch (act) {
M
Mintz, Yuval 已提交
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
	case XDP_TX:
		/* We need the replacement buffer before transmit. */
		if (qede_alloc_rx_buffer(rxq)) {
			qede_recycle_rx_bd_ring(rxq, 1);
			return false;
		}

		/* Now if there's a transmission problem, we'd still have to
		 * throw current buffer, as replacement was already allocated.
		 */
		if (qede_xdp_xmit(edev, fp, bd, cqe->placement_offset, len)) {
			dma_unmap_page(rxq->dev, bd->mapping,
				       PAGE_SIZE, DMA_BIDIRECTIONAL);
			__free_page(bd->data);
		}

		/* Regardless, we've consumed an Rx BD */
		qede_rx_bd_ring_consume(rxq);
		return false;

M
Mintz, Yuval 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	default:
		bpf_warn_invalid_xdp_action(act);
	case XDP_ABORTED:
	case XDP_DROP:
		qede_recycle_rx_bd_ring(rxq, cqe->bd_num);
	}

	return false;
}

M
Mintz, Yuval 已提交
1546 1547 1548 1549
static struct sk_buff *qede_rx_allocate_skb(struct qede_dev *edev,
					    struct qede_rx_queue *rxq,
					    struct sw_rx_data *bd, u16 len,
					    u16 pad)
1550
{
M
Mintz, Yuval 已提交
1551 1552 1553 1554 1555 1556
	unsigned int offset = bd->page_offset;
	struct skb_frag_struct *frag;
	struct page *page = bd->data;
	unsigned int pull_len;
	struct sk_buff *skb;
	unsigned char *va;
1557

M
Mintz, Yuval 已提交
1558 1559 1560 1561
	/* Allocate a new SKB with a sufficient large header len */
	skb = netdev_alloc_skb(edev->ndev, QEDE_RX_HDR_SIZE);
	if (unlikely(!skb))
		return NULL;
1562

M
Mintz, Yuval 已提交
1563 1564
	/* Copy data into SKB - if it's small, we can simply copy it and
	 * re-use the already allcoated & mapped memory.
1565
	 */
M
Mintz, Yuval 已提交
1566 1567 1568
	if (len + pad <= edev->rx_copybreak) {
		memcpy(skb_put(skb, len),
		       page_address(page) + pad + offset, len);
1569
		qede_reuse_page(rxq, bd);
M
Mintz, Yuval 已提交
1570 1571
		goto out;
	}
1572

M
Mintz, Yuval 已提交
1573
	frag = &skb_shinfo(skb)->frags[0];
1574

M
Mintz, Yuval 已提交
1575 1576
	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
			page, pad + offset, len, rxq->rx_buf_seg_size);
1577

M
Mintz, Yuval 已提交
1578 1579
	va = skb_frag_address(frag);
	pull_len = eth_get_headlen(va, QEDE_RX_HDR_SIZE);
1580

M
Mintz, Yuval 已提交
1581 1582
	/* Align the pull_len to optimize memcpy */
	memcpy(skb->data, va, ALIGN(pull_len, sizeof(long)));
1583

M
Mintz, Yuval 已提交
1584 1585 1586 1587 1588 1589
	/* Correct the skb & frag sizes offset after the pull */
	skb_frag_size_sub(frag, pull_len);
	frag->page_offset += pull_len;
	skb->data_len -= pull_len;
	skb->tail += pull_len;

1590
	if (unlikely(qede_realloc_rx_buffer(rxq, bd))) {
M
Mintz, Yuval 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
		/* Incr page ref count to reuse on allocation failure so
		 * that it doesn't get freed while freeing SKB [as its
		 * already mapped there].
		 */
		page_ref_inc(page);
		dev_kfree_skb_any(skb);
		return NULL;
	}

out:
	/* We've consumed the first BD and prepared an SKB */
	qede_rx_bd_ring_consume(rxq);
	return skb;
}

static int qede_rx_build_jumbo(struct qede_dev *edev,
			       struct qede_rx_queue *rxq,
			       struct sk_buff *skb,
			       struct eth_fast_path_rx_reg_cqe *cqe,
			       u16 first_bd_len)
{
	u16 pkt_len = le16_to_cpu(cqe->pkt_len);
	struct sw_rx_data *bd;
	u16 bd_cons_idx;
	u8 num_frags;

	pkt_len -= first_bd_len;

	/* We've already used one BD for the SKB. Now take care of the rest */
	for (num_frags = cqe->bd_num - 1; num_frags > 0; num_frags--) {
		u16 cur_size = pkt_len > rxq->rx_buf_size ? rxq->rx_buf_size :
		    pkt_len;

		if (unlikely(!cur_size)) {
			DP_ERR(edev,
			       "Still got %d BDs for mapping jumbo, but length became 0\n",
			       num_frags);
			goto out;
Y
Yuval Mintz 已提交
1629 1630
		}

M
Mintz, Yuval 已提交
1631
		/* We need a replacement buffer for each BD */
1632
		if (unlikely(qede_alloc_rx_buffer(rxq)))
M
Mintz, Yuval 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641
			goto out;

		/* Now that we've allocated the replacement buffer,
		 * we can safely consume the next BD and map it to the SKB.
		 */
		bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
		bd = &rxq->sw_rx_ring[bd_cons_idx];
		qede_rx_bd_ring_consume(rxq);

1642
		dma_unmap_page(rxq->dev, bd->mapping,
M
Mintz, Yuval 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
			       PAGE_SIZE, DMA_FROM_DEVICE);

		skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
				   bd->data, 0, cur_size);

		skb->truesize += PAGE_SIZE;
		skb->data_len += cur_size;
		skb->len += cur_size;
		pkt_len -= cur_size;
	}

	if (unlikely(pkt_len))
		DP_ERR(edev,
		       "Mapped all BDs of jumbo, but still have %d bytes\n",
		       pkt_len);

out:
	return num_frags;
}

static int qede_rx_process_tpa_cqe(struct qede_dev *edev,
				   struct qede_fastpath *fp,
				   struct qede_rx_queue *rxq,
				   union eth_rx_cqe *cqe,
				   enum eth_rx_cqe_type type)
{
	switch (type) {
	case ETH_RX_CQE_TYPE_TPA_START:
		qede_tpa_start(edev, rxq, &cqe->fast_path_tpa_start);
		return 0;
	case ETH_RX_CQE_TYPE_TPA_CONT:
		qede_tpa_cont(edev, rxq, &cqe->fast_path_tpa_cont);
		return 0;
	case ETH_RX_CQE_TYPE_TPA_END:
		qede_tpa_end(edev, fp, &cqe->fast_path_tpa_end);
		return 1;
	default:
		return 0;
	}
}

static int qede_rx_process_cqe(struct qede_dev *edev,
			       struct qede_fastpath *fp,
			       struct qede_rx_queue *rxq)
{
M
Mintz, Yuval 已提交
1688
	struct bpf_prog *xdp_prog = READ_ONCE(rxq->xdp_prog);
M
Mintz, Yuval 已提交
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
	struct eth_fast_path_rx_reg_cqe *fp_cqe;
	u16 len, pad, bd_cons_idx, parse_flag;
	enum eth_rx_cqe_type cqe_type;
	union eth_rx_cqe *cqe;
	struct sw_rx_data *bd;
	struct sk_buff *skb;
	__le16 flags;
	u8 csum_flag;

	/* Get the CQE from the completion ring */
	cqe = (union eth_rx_cqe *)qed_chain_consume(&rxq->rx_comp_ring);
	cqe_type = cqe->fast_path_regular.type;

	/* Process an unlikely slowpath event */
	if (unlikely(cqe_type == ETH_RX_CQE_TYPE_SLOW_PATH)) {
		struct eth_slow_path_rx_cqe *sp_cqe;

		sp_cqe = (struct eth_slow_path_rx_cqe *)cqe;
		edev->ops->eth_cqe_completion(edev->cdev, fp->id, sp_cqe);
		return 0;
	}

	/* Handle TPA cqes */
	if (cqe_type != ETH_RX_CQE_TYPE_REGULAR)
		return qede_rx_process_tpa_cqe(edev, fp, rxq, cqe, cqe_type);

	/* Get the data from the SW ring; Consume it only after it's evident
	 * we wouldn't recycle it.
	 */
	bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
	bd = &rxq->sw_rx_ring[bd_cons_idx];

	fp_cqe = &cqe->fast_path_regular;
	len = le16_to_cpu(fp_cqe->len_on_first_bd);
	pad = fp_cqe->placement_offset;

M
Mintz, Yuval 已提交
1725 1726 1727 1728 1729
	/* Run eBPF program if one is attached */
	if (xdp_prog)
		if (!qede_rx_xdp(edev, fp, rxq, xdp_prog, bd, fp_cqe))
			return 1;

M
Mintz, Yuval 已提交
1730 1731 1732 1733 1734 1735 1736 1737
	/* If this is an error packet then drop it */
	flags = cqe->fast_path_regular.pars_flags.flags;
	parse_flag = le16_to_cpu(flags);

	csum_flag = qede_check_csum(parse_flag);
	if (unlikely(csum_flag == QEDE_CSUM_ERROR)) {
		if (qede_pkt_is_ip_fragmented(fp_cqe, parse_flag)) {
			rxq->rx_ip_frags++;
Y
Yuval Mintz 已提交
1738
		} else {
M
Mintz, Yuval 已提交
1739 1740 1741 1742
			DP_NOTICE(edev,
				  "CQE has error, flags = %x, dropping incoming packet\n",
				  parse_flag);
			rxq->rx_hw_errors++;
1743
			qede_recycle_rx_bd_ring(rxq, fp_cqe->bd_num);
M
Mintz, Yuval 已提交
1744
			return 0;
1745
		}
M
Mintz, Yuval 已提交
1746
	}
1747

M
Mintz, Yuval 已提交
1748 1749 1750 1751 1752 1753
	/* Basic validation passed; Need to prepare an SKB. This would also
	 * guarantee to finally consume the first BD upon success.
	 */
	skb = qede_rx_allocate_skb(edev, rxq, bd, len, pad);
	if (!skb) {
		rxq->rx_alloc_errors++;
1754
		qede_recycle_rx_bd_ring(rxq, fp_cqe->bd_num);
M
Mintz, Yuval 已提交
1755 1756
		return 0;
	}
1757

M
Mintz, Yuval 已提交
1758 1759 1760 1761 1762 1763
	/* In case of Jumbo packet, several PAGE_SIZEd buffers will be pointed
	 * by a single cqe.
	 */
	if (fp_cqe->bd_num > 1) {
		u16 unmapped_frags = qede_rx_build_jumbo(edev, rxq, skb,
							 fp_cqe, len);
1764

M
Mintz, Yuval 已提交
1765
		if (unlikely(unmapped_frags > 0)) {
1766
			qede_recycle_rx_bd_ring(rxq, unmapped_frags);
M
Mintz, Yuval 已提交
1767 1768
			dev_kfree_skb_any(skb);
			return 0;
Y
Yuval Mintz 已提交
1769
		}
M
Mintz, Yuval 已提交
1770
	}
1771

M
Mintz, Yuval 已提交
1772 1773
	/* The SKB contains all the data. Now prepare meta-magic */
	skb->protocol = eth_type_trans(skb, edev->ndev);
1774
	qede_get_rxhash(skb, fp_cqe->bitfields, fp_cqe->rss_hash);
M
Mintz, Yuval 已提交
1775 1776
	qede_set_skb_csum(skb, csum_flag);
	skb_record_rx_queue(skb, rxq->rxq_id);
1777

M
Mintz, Yuval 已提交
1778
	/* SKB is prepared - pass it to stack */
1779
	qede_skb_receive(edev, fp, rxq, skb, le16_to_cpu(fp_cqe->vlan_tag));
1780

M
Mintz, Yuval 已提交
1781 1782
	return 1;
}
1783

M
Mintz, Yuval 已提交
1784 1785 1786 1787 1788 1789
static int qede_rx_int(struct qede_fastpath *fp, int budget)
{
	struct qede_rx_queue *rxq = fp->rxq;
	struct qede_dev *edev = fp->edev;
	u16 hw_comp_cons, sw_comp_cons;
	int work_done = 0;
1790

M
Mintz, Yuval 已提交
1791 1792
	hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
	sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1793

M
Mintz, Yuval 已提交
1794 1795 1796 1797 1798 1799
	/* Memory barrier to prevent the CPU from doing speculative reads of CQE
	 * / BD in the while-loop before reading hw_comp_cons. If the CQE is
	 * read before it is written by FW, then FW writes CQE and SB, and then
	 * the CPU reads the hw_comp_cons, it will use an old CQE.
	 */
	rmb();
1800

M
Mintz, Yuval 已提交
1801 1802 1803
	/* Loop to complete all indicated BDs */
	while ((sw_comp_cons != hw_comp_cons) && (work_done < budget)) {
		qede_rx_process_cqe(edev, fp, rxq);
1804 1805
		qed_chain_recycle_consumed(&rxq->rx_comp_ring);
		sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
M
Mintz, Yuval 已提交
1806 1807
		work_done++;
	}
1808 1809 1810 1811

	/* Update producers */
	qede_update_rx_prod(edev, rxq);

M
Mintz, Yuval 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
	return work_done;
}

static bool qede_poll_is_more_work(struct qede_fastpath *fp)
{
	qed_sb_update_sb_idx(fp->sb_info);

	/* *_has_*_work() reads the status block, thus we need to ensure that
	 * status block indices have been actually read (qed_sb_update_sb_idx)
	 * prior to this check (*_has_*_work) so that we won't write the
	 * "newer" value of the status block to HW (if there was a DMA right
	 * after qede_has_rx_work and if there is no rmb, the memory reading
	 * (qed_sb_update_sb_idx) may be postponed to right before *_ack_sb).
	 * In this case there will never be another interrupt until there is
	 * another update of the status block, while there is still unhandled
	 * work.
	 */
	rmb();

	if (likely(fp->type & QEDE_FASTPATH_RX))
		if (qede_has_rx_work(fp->rxq))
			return true;

M
Mintz, Yuval 已提交
1835 1836 1837 1838
	if (fp->type & QEDE_FASTPATH_XDP)
		if (qede_txq_has_work(fp->xdp_tx))
			return true;

M
Mintz, Yuval 已提交
1839 1840 1841
	if (likely(fp->type & QEDE_FASTPATH_TX))
		if (qede_txq_has_work(fp->txq))
			return true;
1842

M
Mintz, Yuval 已提交
1843
	return false;
1844 1845 1846 1847 1848
}

static int qede_poll(struct napi_struct *napi, int budget)
{
	struct qede_fastpath *fp = container_of(napi, struct qede_fastpath,
M
Manish Chopra 已提交
1849
						napi);
1850
	struct qede_dev *edev = fp->edev;
M
Manish Chopra 已提交
1851
	int rx_work_done = 0;
1852

M
Mintz, Yuval 已提交
1853 1854
	if (likely(fp->type & QEDE_FASTPATH_TX) && qede_txq_has_work(fp->txq))
		qede_tx_int(edev, fp->txq);
M
Manish Chopra 已提交
1855

M
Mintz, Yuval 已提交
1856 1857 1858
	if ((fp->type & QEDE_FASTPATH_XDP) && qede_txq_has_work(fp->xdp_tx))
		qede_xdp_tx_int(edev, fp->xdp_tx);

1859 1860
	rx_work_done = (likely(fp->type & QEDE_FASTPATH_RX) &&
			qede_has_rx_work(fp->rxq)) ?
M
Manish Chopra 已提交
1861 1862
			qede_rx_int(fp, budget) : 0;
	if (rx_work_done < budget) {
M
Mintz, Yuval 已提交
1863
		if (!qede_poll_is_more_work(fp)) {
M
Manish Chopra 已提交
1864 1865 1866
			napi_complete(napi);

			/* Update and reenable interrupts */
M
Mintz, Yuval 已提交
1867
			qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
M
Manish Chopra 已提交
1868 1869
		} else {
			rx_work_done = budget;
1870 1871 1872
		}
	}

M
Mintz, Yuval 已提交
1873 1874 1875 1876 1877 1878 1879 1880
	if (fp->xdp_xmit) {
		u16 xdp_prod = qed_chain_get_prod_idx(&fp->xdp_tx->tx_pbl);

		fp->xdp_xmit = 0;
		fp->xdp_tx->tx_db.data.bd_prod = cpu_to_le16(xdp_prod);
		qede_update_tx_producer(fp->xdp_tx);
	}

M
Manish Chopra 已提交
1881
	return rx_work_done;
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
}

static irqreturn_t qede_msix_fp_int(int irq, void *fp_cookie)
{
	struct qede_fastpath *fp = fp_cookie;

	qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);

	napi_schedule_irqoff(&fp->napi);
	return IRQ_HANDLED;
}

/* -------------------------------------------------------------------------
 * END OF FAST-PATH
 * -------------------------------------------------------------------------
 */

static int qede_open(struct net_device *ndev);
static int qede_close(struct net_device *ndev);
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
static int qede_set_mac_addr(struct net_device *ndev, void *p);
static void qede_set_rx_mode(struct net_device *ndev);
static void qede_config_rx_mode(struct net_device *ndev);

static int qede_set_ucast_rx_mac(struct qede_dev *edev,
				 enum qed_filter_xcast_params_type opcode,
				 unsigned char mac[ETH_ALEN])
{
	struct qed_filter_params filter_cmd;

	memset(&filter_cmd, 0, sizeof(filter_cmd));
	filter_cmd.type = QED_FILTER_TYPE_UCAST;
	filter_cmd.filter.ucast.type = opcode;
	filter_cmd.filter.ucast.mac_valid = 1;
	ether_addr_copy(filter_cmd.filter.ucast.mac, mac);

	return edev->ops->filter_config(edev->cdev, &filter_cmd);
}

1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
static int qede_set_ucast_rx_vlan(struct qede_dev *edev,
				  enum qed_filter_xcast_params_type opcode,
				  u16 vid)
{
	struct qed_filter_params filter_cmd;

	memset(&filter_cmd, 0, sizeof(filter_cmd));
	filter_cmd.type = QED_FILTER_TYPE_UCAST;
	filter_cmd.filter.ucast.type = opcode;
	filter_cmd.filter.ucast.vlan_valid = 1;
	filter_cmd.filter.ucast.vlan = vid;

	return edev->ops->filter_config(edev->cdev, &filter_cmd);
}

1935 1936 1937 1938 1939 1940
void qede_fill_by_demand_stats(struct qede_dev *edev)
{
	struct qed_eth_stats stats;

	edev->ops->get_vport_stats(edev->cdev, &stats);
	edev->stats.no_buff_discards = stats.no_buff_discards;
1941 1942
	edev->stats.packet_too_big_discard = stats.packet_too_big_discard;
	edev->stats.ttl0_discard = stats.ttl0_discard;
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
	edev->stats.rx_ucast_bytes = stats.rx_ucast_bytes;
	edev->stats.rx_mcast_bytes = stats.rx_mcast_bytes;
	edev->stats.rx_bcast_bytes = stats.rx_bcast_bytes;
	edev->stats.rx_ucast_pkts = stats.rx_ucast_pkts;
	edev->stats.rx_mcast_pkts = stats.rx_mcast_pkts;
	edev->stats.rx_bcast_pkts = stats.rx_bcast_pkts;
	edev->stats.mftag_filter_discards = stats.mftag_filter_discards;
	edev->stats.mac_filter_discards = stats.mac_filter_discards;

	edev->stats.tx_ucast_bytes = stats.tx_ucast_bytes;
	edev->stats.tx_mcast_bytes = stats.tx_mcast_bytes;
	edev->stats.tx_bcast_bytes = stats.tx_bcast_bytes;
	edev->stats.tx_ucast_pkts = stats.tx_ucast_pkts;
	edev->stats.tx_mcast_pkts = stats.tx_mcast_pkts;
	edev->stats.tx_bcast_pkts = stats.tx_bcast_pkts;
	edev->stats.tx_err_drop_pkts = stats.tx_err_drop_pkts;
	edev->stats.coalesced_pkts = stats.tpa_coalesced_pkts;
	edev->stats.coalesced_events = stats.tpa_coalesced_events;
	edev->stats.coalesced_aborts_num = stats.tpa_aborts_num;
	edev->stats.non_coalesced_pkts = stats.tpa_not_coalesced_pkts;
	edev->stats.coalesced_bytes = stats.tpa_coalesced_bytes;

	edev->stats.rx_64_byte_packets = stats.rx_64_byte_packets;
Y
Yuval Mintz 已提交
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
	edev->stats.rx_65_to_127_byte_packets = stats.rx_65_to_127_byte_packets;
	edev->stats.rx_128_to_255_byte_packets =
				stats.rx_128_to_255_byte_packets;
	edev->stats.rx_256_to_511_byte_packets =
				stats.rx_256_to_511_byte_packets;
	edev->stats.rx_512_to_1023_byte_packets =
				stats.rx_512_to_1023_byte_packets;
	edev->stats.rx_1024_to_1518_byte_packets =
				stats.rx_1024_to_1518_byte_packets;
	edev->stats.rx_1519_to_1522_byte_packets =
				stats.rx_1519_to_1522_byte_packets;
	edev->stats.rx_1519_to_2047_byte_packets =
				stats.rx_1519_to_2047_byte_packets;
	edev->stats.rx_2048_to_4095_byte_packets =
				stats.rx_2048_to_4095_byte_packets;
	edev->stats.rx_4096_to_9216_byte_packets =
				stats.rx_4096_to_9216_byte_packets;
	edev->stats.rx_9217_to_16383_byte_packets =
				stats.rx_9217_to_16383_byte_packets;
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
	edev->stats.rx_crc_errors = stats.rx_crc_errors;
	edev->stats.rx_mac_crtl_frames = stats.rx_mac_crtl_frames;
	edev->stats.rx_pause_frames = stats.rx_pause_frames;
	edev->stats.rx_pfc_frames = stats.rx_pfc_frames;
	edev->stats.rx_align_errors = stats.rx_align_errors;
	edev->stats.rx_carrier_errors = stats.rx_carrier_errors;
	edev->stats.rx_oversize_packets = stats.rx_oversize_packets;
	edev->stats.rx_jabbers = stats.rx_jabbers;
	edev->stats.rx_undersize_packets = stats.rx_undersize_packets;
	edev->stats.rx_fragments = stats.rx_fragments;
	edev->stats.tx_64_byte_packets = stats.tx_64_byte_packets;
	edev->stats.tx_65_to_127_byte_packets = stats.tx_65_to_127_byte_packets;
	edev->stats.tx_128_to_255_byte_packets =
				stats.tx_128_to_255_byte_packets;
	edev->stats.tx_256_to_511_byte_packets =
				stats.tx_256_to_511_byte_packets;
	edev->stats.tx_512_to_1023_byte_packets =
				stats.tx_512_to_1023_byte_packets;
	edev->stats.tx_1024_to_1518_byte_packets =
				stats.tx_1024_to_1518_byte_packets;
	edev->stats.tx_1519_to_2047_byte_packets =
				stats.tx_1519_to_2047_byte_packets;
	edev->stats.tx_2048_to_4095_byte_packets =
				stats.tx_2048_to_4095_byte_packets;
	edev->stats.tx_4096_to_9216_byte_packets =
				stats.tx_4096_to_9216_byte_packets;
	edev->stats.tx_9217_to_16383_byte_packets =
				stats.tx_9217_to_16383_byte_packets;
	edev->stats.tx_pause_frames = stats.tx_pause_frames;
	edev->stats.tx_pfc_frames = stats.tx_pfc_frames;
	edev->stats.tx_lpi_entry_count = stats.tx_lpi_entry_count;
	edev->stats.tx_total_collisions = stats.tx_total_collisions;
	edev->stats.brb_truncates = stats.brb_truncates;
	edev->stats.brb_discards = stats.brb_discards;
	edev->stats.tx_mac_ctrl_frames = stats.tx_mac_ctrl_frames;
}

Y
Yuval Mintz 已提交
2022 2023 2024
static
struct rtnl_link_stats64 *qede_get_stats64(struct net_device *dev,
					   struct rtnl_link_stats64 *stats)
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057
{
	struct qede_dev *edev = netdev_priv(dev);

	qede_fill_by_demand_stats(edev);

	stats->rx_packets = edev->stats.rx_ucast_pkts +
			    edev->stats.rx_mcast_pkts +
			    edev->stats.rx_bcast_pkts;
	stats->tx_packets = edev->stats.tx_ucast_pkts +
			    edev->stats.tx_mcast_pkts +
			    edev->stats.tx_bcast_pkts;

	stats->rx_bytes = edev->stats.rx_ucast_bytes +
			  edev->stats.rx_mcast_bytes +
			  edev->stats.rx_bcast_bytes;

	stats->tx_bytes = edev->stats.tx_ucast_bytes +
			  edev->stats.tx_mcast_bytes +
			  edev->stats.tx_bcast_bytes;

	stats->tx_errors = edev->stats.tx_err_drop_pkts;
	stats->multicast = edev->stats.rx_mcast_pkts +
			   edev->stats.rx_bcast_pkts;

	stats->rx_fifo_errors = edev->stats.no_buff_discards;

	stats->collisions = edev->stats.tx_total_collisions;
	stats->rx_crc_errors = edev->stats.rx_crc_errors;
	stats->rx_frame_errors = edev->stats.rx_align_errors;

	return stats;
}

Y
Yuval Mintz 已提交
2058
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
static int qede_get_vf_config(struct net_device *dev, int vfidx,
			      struct ifla_vf_info *ivi)
{
	struct qede_dev *edev = netdev_priv(dev);

	if (!edev->ops)
		return -EINVAL;

	return edev->ops->iov->get_config(edev->cdev, vfidx, ivi);
}

Y
Yuval Mintz 已提交
2070 2071 2072 2073 2074
static int qede_set_vf_rate(struct net_device *dev, int vfidx,
			    int min_tx_rate, int max_tx_rate)
{
	struct qede_dev *edev = netdev_priv(dev);

Y
Yuval Mintz 已提交
2075
	return edev->ops->iov->set_rate(edev->cdev, vfidx, min_tx_rate,
Y
Yuval Mintz 已提交
2076 2077 2078
					max_tx_rate);
}

Y
Yuval Mintz 已提交
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
static int qede_set_vf_spoofchk(struct net_device *dev, int vfidx, bool val)
{
	struct qede_dev *edev = netdev_priv(dev);

	if (!edev->ops)
		return -EINVAL;

	return edev->ops->iov->set_spoof(edev->cdev, vfidx, val);
}

Y
Yuval Mintz 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100
static int qede_set_vf_link_state(struct net_device *dev, int vfidx,
				  int link_state)
{
	struct qede_dev *edev = netdev_priv(dev);

	if (!edev->ops)
		return -EINVAL;

	return edev->ops->iov->set_link_state(edev->cdev, vfidx, link_state);
}
#endif

2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
static void qede_config_accept_any_vlan(struct qede_dev *edev, bool action)
{
	struct qed_update_vport_params params;
	int rc;

	/* Proceed only if action actually needs to be performed */
	if (edev->accept_any_vlan == action)
		return;

	memset(&params, 0, sizeof(params));

	params.vport_id = 0;
	params.accept_any_vlan = action;
	params.update_accept_any_vlan_flg = 1;

	rc = edev->ops->vport_update(edev->cdev, &params);
	if (rc) {
		DP_ERR(edev, "Failed to %s accept-any-vlan\n",
		       action ? "enable" : "disable");
	} else {
		DP_INFO(edev, "%s accept-any-vlan\n",
			action ? "enabled" : "disabled");
		edev->accept_any_vlan = action;
	}
}

static int qede_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
{
	struct qede_dev *edev = netdev_priv(dev);
	struct qede_vlan *vlan, *tmp;
M
Mintz, Yuval 已提交
2131
	int rc = 0;
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154

	DP_VERBOSE(edev, NETIF_MSG_IFUP, "Adding vlan 0x%04x\n", vid);

	vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
	if (!vlan) {
		DP_INFO(edev, "Failed to allocate struct for vlan\n");
		return -ENOMEM;
	}
	INIT_LIST_HEAD(&vlan->list);
	vlan->vid = vid;
	vlan->configured = false;

	/* Verify vlan isn't already configured */
	list_for_each_entry(tmp, &edev->vlan_list, list) {
		if (tmp->vid == vlan->vid) {
			DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
				   "vlan already configured\n");
			kfree(vlan);
			return -EEXIST;
		}
	}

	/* If interface is down, cache this VLAN ID and return */
M
Mintz, Yuval 已提交
2155
	__qede_lock(edev);
2156 2157 2158 2159 2160 2161 2162
	if (edev->state != QEDE_STATE_OPEN) {
		DP_VERBOSE(edev, NETIF_MSG_IFDOWN,
			   "Interface is down, VLAN %d will be configured when interface is up\n",
			   vid);
		if (vid != 0)
			edev->non_configured_vlans++;
		list_add(&vlan->list, &edev->vlan_list);
M
Mintz, Yuval 已提交
2163
		goto out;
2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
	}

	/* Check for the filter limit.
	 * Note - vlan0 has a reserved filter and can be added without
	 * worrying about quota
	 */
	if ((edev->configured_vlans < edev->dev_info.num_vlan_filters) ||
	    (vlan->vid == 0)) {
		rc = qede_set_ucast_rx_vlan(edev,
					    QED_FILTER_XCAST_TYPE_ADD,
					    vlan->vid);
		if (rc) {
			DP_ERR(edev, "Failed to configure VLAN %d\n",
			       vlan->vid);
			kfree(vlan);
M
Mintz, Yuval 已提交
2179
			goto out;
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
		}
		vlan->configured = true;

		/* vlan0 filter isn't consuming out of our quota */
		if (vlan->vid != 0)
			edev->configured_vlans++;
	} else {
		/* Out of quota; Activate accept-any-VLAN mode */
		if (!edev->non_configured_vlans)
			qede_config_accept_any_vlan(edev, true);

		edev->non_configured_vlans++;
	}

	list_add(&vlan->list, &edev->vlan_list);

M
Mintz, Yuval 已提交
2196 2197 2198
out:
	__qede_unlock(edev);
	return rc;
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274
}

static void qede_del_vlan_from_list(struct qede_dev *edev,
				    struct qede_vlan *vlan)
{
	/* vlan0 filter isn't consuming out of our quota */
	if (vlan->vid != 0) {
		if (vlan->configured)
			edev->configured_vlans--;
		else
			edev->non_configured_vlans--;
	}

	list_del(&vlan->list);
	kfree(vlan);
}

static int qede_configure_vlan_filters(struct qede_dev *edev)
{
	int rc = 0, real_rc = 0, accept_any_vlan = 0;
	struct qed_dev_eth_info *dev_info;
	struct qede_vlan *vlan = NULL;

	if (list_empty(&edev->vlan_list))
		return 0;

	dev_info = &edev->dev_info;

	/* Configure non-configured vlans */
	list_for_each_entry(vlan, &edev->vlan_list, list) {
		if (vlan->configured)
			continue;

		/* We have used all our credits, now enable accept_any_vlan */
		if ((vlan->vid != 0) &&
		    (edev->configured_vlans == dev_info->num_vlan_filters)) {
			accept_any_vlan = 1;
			continue;
		}

		DP_VERBOSE(edev, NETIF_MSG_IFUP, "Adding vlan %d\n", vlan->vid);

		rc = qede_set_ucast_rx_vlan(edev, QED_FILTER_XCAST_TYPE_ADD,
					    vlan->vid);
		if (rc) {
			DP_ERR(edev, "Failed to configure VLAN %u\n",
			       vlan->vid);
			real_rc = rc;
			continue;
		}

		vlan->configured = true;
		/* vlan0 filter doesn't consume our VLAN filter's quota */
		if (vlan->vid != 0) {
			edev->non_configured_vlans--;
			edev->configured_vlans++;
		}
	}

	/* enable accept_any_vlan mode if we have more VLANs than credits,
	 * or remove accept_any_vlan mode if we've actually removed
	 * a non-configured vlan, and all remaining vlans are truly configured.
	 */

	if (accept_any_vlan)
		qede_config_accept_any_vlan(edev, true);
	else if (!edev->non_configured_vlans)
		qede_config_accept_any_vlan(edev, false);

	return real_rc;
}

static int qede_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid)
{
	struct qede_dev *edev = netdev_priv(dev);
	struct qede_vlan *vlan = NULL;
M
Mintz, Yuval 已提交
2275
	int rc = 0;
2276 2277 2278 2279

	DP_VERBOSE(edev, NETIF_MSG_IFDOWN, "Removing vlan 0x%04x\n", vid);

	/* Find whether entry exists */
M
Mintz, Yuval 已提交
2280
	__qede_lock(edev);
2281 2282 2283 2284 2285 2286 2287
	list_for_each_entry(vlan, &edev->vlan_list, list)
		if (vlan->vid == vid)
			break;

	if (!vlan || (vlan->vid != vid)) {
		DP_VERBOSE(edev, (NETIF_MSG_IFUP | NETIF_MSG_IFDOWN),
			   "Vlan isn't configured\n");
M
Mintz, Yuval 已提交
2288
		goto out;
2289 2290 2291 2292 2293 2294 2295 2296 2297
	}

	if (edev->state != QEDE_STATE_OPEN) {
		/* As interface is already down, we don't have a VPORT
		 * instance to remove vlan filter. So just update vlan list
		 */
		DP_VERBOSE(edev, NETIF_MSG_IFDOWN,
			   "Interface is down, removing VLAN from list only\n");
		qede_del_vlan_from_list(edev, vlan);
M
Mintz, Yuval 已提交
2298
		goto out;
2299 2300 2301
	}

	/* Remove vlan */
2302 2303 2304 2305 2306
	if (vlan->configured) {
		rc = qede_set_ucast_rx_vlan(edev, QED_FILTER_XCAST_TYPE_DEL,
					    vid);
		if (rc) {
			DP_ERR(edev, "Failed to remove VLAN %d\n", vid);
M
Mintz, Yuval 已提交
2307
			goto out;
2308
		}
2309 2310 2311 2312 2313 2314 2315 2316 2317
	}

	qede_del_vlan_from_list(edev, vlan);

	/* We have removed a VLAN - try to see if we can
	 * configure non-configured VLAN from the list.
	 */
	rc = qede_configure_vlan_filters(edev);

M
Mintz, Yuval 已提交
2318 2319
out:
	__qede_unlock(edev);
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342
	return rc;
}

static void qede_vlan_mark_nonconfigured(struct qede_dev *edev)
{
	struct qede_vlan *vlan = NULL;

	if (list_empty(&edev->vlan_list))
		return;

	list_for_each_entry(vlan, &edev->vlan_list, list) {
		if (!vlan->configured)
			continue;

		vlan->configured = false;

		/* vlan0 filter isn't consuming out of our quota */
		if (vlan->vid != 0) {
			edev->non_configured_vlans++;
			edev->configured_vlans--;
		}

		DP_VERBOSE(edev, NETIF_MSG_IFDOWN,
Y
Yuval Mintz 已提交
2343
			   "marked vlan %d as non-configured\n", vlan->vid);
2344 2345 2346 2347 2348
	}

	edev->accept_any_vlan = false;
}

M
Mintz, Yuval 已提交
2349 2350 2351 2352 2353 2354 2355
static void qede_set_features_reload(struct qede_dev *edev,
				     struct qede_reload_args *args)
{
	edev->ndev->features = args->u.features;
}

int qede_set_features(struct net_device *dev, netdev_features_t features)
Y
Yuval Mintz 已提交
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
{
	struct qede_dev *edev = netdev_priv(dev);
	netdev_features_t changes = features ^ dev->features;
	bool need_reload = false;

	/* No action needed if hardware GRO is disabled during driver load */
	if (changes & NETIF_F_GRO) {
		if (dev->features & NETIF_F_GRO)
			need_reload = !edev->gro_disable;
		else
			need_reload = edev->gro_disable;
	}

M
Mintz, Yuval 已提交
2369 2370 2371 2372 2373 2374
	if (need_reload) {
		struct qede_reload_args args;

		args.u.features = features;
		args.func = &qede_set_features_reload;

M
Mintz, Yuval 已提交
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384
		/* Make sure that we definitely need to reload.
		 * In case of an eBPF attached program, there will be no FW
		 * aggregations, so no need to actually reload.
		 */
		__qede_lock(edev);
		if (edev->xdp_prog)
			args.func(edev, &args);
		else
			qede_reload(edev, &args, true);
		__qede_unlock(edev);
M
Mintz, Yuval 已提交
2385

Y
Yuval Mintz 已提交
2386 2387 2388 2389 2390 2391
		return 1;
	}

	return 0;
}

2392 2393
static void qede_udp_tunnel_add(struct net_device *dev,
				struct udp_tunnel_info *ti)
2394 2395
{
	struct qede_dev *edev = netdev_priv(dev);
2396
	u16 t_port = ntohs(ti->port);
2397

2398 2399 2400 2401
	switch (ti->type) {
	case UDP_TUNNEL_TYPE_VXLAN:
		if (edev->vxlan_dst_port)
			return;
2402

2403
		edev->vxlan_dst_port = t_port;
2404

Y
Yuval Mintz 已提交
2405
		DP_VERBOSE(edev, QED_MSG_DEBUG, "Added vxlan port=%d\n",
2406
			   t_port);
2407

2408 2409 2410 2411 2412
		set_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags);
		break;
	case UDP_TUNNEL_TYPE_GENEVE:
		if (edev->geneve_dst_port)
			return;
2413

2414
		edev->geneve_dst_port = t_port;
2415

Y
Yuval Mintz 已提交
2416
		DP_VERBOSE(edev, QED_MSG_DEBUG, "Added geneve port=%d\n",
2417 2418 2419 2420
			   t_port);
		set_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags);
		break;
	default:
2421
		return;
2422
	}
2423 2424 2425 2426

	schedule_delayed_work(&edev->sp_task, 0);
}

2427 2428
static void qede_udp_tunnel_del(struct net_device *dev,
				struct udp_tunnel_info *ti)
2429 2430
{
	struct qede_dev *edev = netdev_priv(dev);
2431
	u16 t_port = ntohs(ti->port);
2432

2433 2434 2435 2436
	switch (ti->type) {
	case UDP_TUNNEL_TYPE_VXLAN:
		if (t_port != edev->vxlan_dst_port)
			return;
2437

2438
		edev->vxlan_dst_port = 0;
2439

Y
Yuval Mintz 已提交
2440
		DP_VERBOSE(edev, QED_MSG_DEBUG, "Deleted vxlan port=%d\n",
2441
			   t_port);
2442

2443 2444 2445 2446 2447
		set_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags);
		break;
	case UDP_TUNNEL_TYPE_GENEVE:
		if (t_port != edev->geneve_dst_port)
			return;
2448

2449
		edev->geneve_dst_port = 0;
2450

Y
Yuval Mintz 已提交
2451
		DP_VERBOSE(edev, QED_MSG_DEBUG, "Deleted geneve port=%d\n",
2452 2453 2454 2455 2456 2457
			   t_port);
		set_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags);
		break;
	default:
		return;
	}
2458 2459 2460 2461

	schedule_delayed_work(&edev->sp_task, 0);
}

2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
/* 8B udp header + 8B base tunnel header + 32B option length */
#define QEDE_MAX_TUN_HDR_LEN 48

static netdev_features_t qede_features_check(struct sk_buff *skb,
					     struct net_device *dev,
					     netdev_features_t features)
{
	if (skb->encapsulation) {
		u8 l4_proto = 0;

		switch (vlan_get_protocol(skb)) {
		case htons(ETH_P_IP):
			l4_proto = ip_hdr(skb)->protocol;
			break;
		case htons(ETH_P_IPV6):
			l4_proto = ipv6_hdr(skb)->nexthdr;
			break;
		default:
			return features;
		}

		/* Disable offloads for geneve tunnels, as HW can't parse
		 * the geneve header which has option length greater than 32B.
		 */
		if ((l4_proto == IPPROTO_UDP) &&
		    ((skb_inner_mac_header(skb) -
		      skb_transport_header(skb)) > QEDE_MAX_TUN_HDR_LEN))
			return features & ~(NETIF_F_CSUM_MASK |
					    NETIF_F_GSO_MASK);
	}

	return features;
}

M
Mintz, Yuval 已提交
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
static void qede_xdp_reload_func(struct qede_dev *edev,
				 struct qede_reload_args *args)
{
	struct bpf_prog *old;

	old = xchg(&edev->xdp_prog, args->u.new_prog);
	if (old)
		bpf_prog_put(old);
}

static int qede_xdp_set(struct qede_dev *edev, struct bpf_prog *prog)
{
	struct qede_reload_args args;

2510 2511 2512 2513 2514
	if (prog && prog->xdp_adjust_head) {
		DP_ERR(edev, "Does not support bpf_xdp_adjust_head()\n");
		return -EOPNOTSUPP;
	}

M
Mintz, Yuval 已提交
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
	/* If we're called, there was already a bpf reference increment */
	args.func = &qede_xdp_reload_func;
	args.u.new_prog = prog;
	qede_reload(edev, &args, false);

	return 0;
}

static int qede_xdp(struct net_device *dev, struct netdev_xdp *xdp)
{
	struct qede_dev *edev = netdev_priv(dev);

	switch (xdp->command) {
	case XDP_SETUP_PROG:
		return qede_xdp_set(edev, xdp->prog);
	case XDP_QUERY_PROG:
		xdp->prog_attached = !!edev->xdp_prog;
		return 0;
	default:
		return -EINVAL;
	}
}

2538 2539 2540 2541
static const struct net_device_ops qede_netdev_ops = {
	.ndo_open = qede_open,
	.ndo_stop = qede_close,
	.ndo_start_xmit = qede_start_xmit,
2542 2543
	.ndo_set_rx_mode = qede_set_rx_mode,
	.ndo_set_mac_address = qede_set_mac_addr,
2544
	.ndo_validate_addr = eth_validate_addr,
2545
	.ndo_change_mtu = qede_change_mtu,
Y
Yuval Mintz 已提交
2546
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
2547
	.ndo_set_vf_mac = qede_set_vf_mac,
Y
Yuval Mintz 已提交
2548 2549
	.ndo_set_vf_vlan = qede_set_vf_vlan,
#endif
2550 2551
	.ndo_vlan_rx_add_vid = qede_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid = qede_vlan_rx_kill_vid,
Y
Yuval Mintz 已提交
2552
	.ndo_set_features = qede_set_features,
2553
	.ndo_get_stats64 = qede_get_stats64,
Y
Yuval Mintz 已提交
2554 2555
#ifdef CONFIG_QED_SRIOV
	.ndo_set_vf_link_state = qede_set_vf_link_state,
Y
Yuval Mintz 已提交
2556
	.ndo_set_vf_spoofchk = qede_set_vf_spoofchk,
Y
Yuval Mintz 已提交
2557
	.ndo_get_vf_config = qede_get_vf_config,
Y
Yuval Mintz 已提交
2558 2559
	.ndo_set_vf_rate = qede_set_vf_rate,
#endif
2560 2561
	.ndo_udp_tunnel_add = qede_udp_tunnel_add,
	.ndo_udp_tunnel_del = qede_udp_tunnel_del,
2562
	.ndo_features_check = qede_features_check,
M
Mintz, Yuval 已提交
2563
	.ndo_xdp = qede_xdp,
2564 2565
};

Y
Yuval Mintz 已提交
2566 2567 2568 2569 2570 2571 2572 2573
/* -------------------------------------------------------------------------
 * START OF PROBE / REMOVE
 * -------------------------------------------------------------------------
 */

static struct qede_dev *qede_alloc_etherdev(struct qed_dev *cdev,
					    struct pci_dev *pdev,
					    struct qed_dev_eth_info *info,
Y
Yuval Mintz 已提交
2574
					    u32 dp_module, u8 dp_level)
Y
Yuval Mintz 已提交
2575 2576 2577 2578 2579
{
	struct net_device *ndev;
	struct qede_dev *edev;

	ndev = alloc_etherdev_mqs(sizeof(*edev),
Y
Yuval Mintz 已提交
2580
				  info->num_queues, info->num_queues);
Y
Yuval Mintz 已提交
2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
	if (!ndev) {
		pr_err("etherdev allocation failed\n");
		return NULL;
	}

	edev = netdev_priv(ndev);
	edev->ndev = ndev;
	edev->cdev = cdev;
	edev->pdev = pdev;
	edev->dp_module = dp_module;
	edev->dp_level = dp_level;
	edev->ops = qed_ops;
2593 2594
	edev->q_num_rx_buffers = NUM_RX_BDS_DEF;
	edev->q_num_tx_buffers = NUM_TX_BDS_DEF;
Y
Yuval Mintz 已提交
2595

Y
Yuval Mintz 已提交
2596 2597 2598
	DP_INFO(edev, "Allocated netdev with %d tx queues and %d rx queues\n",
		info->num_queues, info->num_queues);

Y
Yuval Mintz 已提交
2599 2600
	SET_NETDEV_DEV(ndev, &pdev->dev);

2601
	memset(&edev->stats, 0, sizeof(edev->stats));
Y
Yuval Mintz 已提交
2602 2603
	memcpy(&edev->dev_info, info, sizeof(*info));

2604 2605
	INIT_LIST_HEAD(&edev->vlan_list);

Y
Yuval Mintz 已提交
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
	return edev;
}

static void qede_init_ndev(struct qede_dev *edev)
{
	struct net_device *ndev = edev->ndev;
	struct pci_dev *pdev = edev->pdev;
	u32 hw_features;

	pci_set_drvdata(pdev, ndev);

	ndev->mem_start = edev->dev_info.common.pci_mem_start;
	ndev->base_addr = ndev->mem_start;
	ndev->mem_end = edev->dev_info.common.pci_mem_end;
	ndev->irq = edev->dev_info.common.pci_irq;

	ndev->watchdog_timeo = TX_TIMEOUT;

2624 2625
	ndev->netdev_ops = &qede_netdev_ops;

2626 2627
	qede_set_ethtool_ops(ndev);

M
Mintz, Yuval 已提交
2628
	ndev->priv_flags |= IFF_UNICAST_FLT;
Y
Yuval Mintz 已提交
2629

Y
Yuval Mintz 已提交
2630 2631 2632 2633 2634
	/* user-changeble features */
	hw_features = NETIF_F_GRO | NETIF_F_SG |
		      NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
		      NETIF_F_TSO | NETIF_F_TSO6;

2635 2636
	/* Encap features*/
	hw_features |= NETIF_F_GSO_GRE | NETIF_F_GSO_UDP_TUNNEL |
2637 2638
		       NETIF_F_TSO_ECN | NETIF_F_GSO_UDP_TUNNEL_CSUM |
		       NETIF_F_GSO_GRE_CSUM;
2639 2640 2641
	ndev->hw_enc_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
				NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO_ECN |
				NETIF_F_TSO6 | NETIF_F_GSO_GRE |
2642 2643 2644
				NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RXCSUM |
				NETIF_F_GSO_UDP_TUNNEL_CSUM |
				NETIF_F_GSO_GRE_CSUM;
2645

Y
Yuval Mintz 已提交
2646 2647 2648 2649
	ndev->vlan_features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
			      NETIF_F_HIGHDMA;
	ndev->features = hw_features | NETIF_F_RXHASH | NETIF_F_RXCSUM |
			 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HIGHDMA |
2650
			 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_TX;
Y
Yuval Mintz 已提交
2651 2652 2653

	ndev->hw_features = hw_features;

2654 2655 2656 2657
	/* MTU range: 46 - 9600 */
	ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
	ndev->max_mtu = QEDE_MAX_JUMBO_PACKET_SIZE;

Y
Yuval Mintz 已提交
2658 2659
	/* Set network device HW mac */
	ether_addr_copy(edev->ndev->dev_addr, edev->dev_info.common.hw_mac);
2660 2661

	ndev->mtu = edev->dev_info.common.mtu;
Y
Yuval Mintz 已提交
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
}

/* This function converts from 32b param to two params of level and module
 * Input 32b decoding:
 * b31 - enable all NOTICE prints. NOTICE prints are for deviation from the
 * 'happy' flow, e.g. memory allocation failed.
 * b30 - enable all INFO prints. INFO prints are for major steps in the flow
 * and provide important parameters.
 * b29-b0 - per-module bitmap, where each bit enables VERBOSE prints of that
 * module. VERBOSE prints are for tracking the specific flow in low level.
 *
 * Notice that the level should be that of the lowest required logs.
 */
2675
void qede_config_debug(uint debug, u32 *p_dp_module, u8 *p_dp_level)
Y
Yuval Mintz 已提交
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
{
	*p_dp_level = QED_LEVEL_NOTICE;
	*p_dp_module = 0;

	if (debug & QED_LOG_VERBOSE_MASK) {
		*p_dp_level = QED_LEVEL_VERBOSE;
		*p_dp_module = (debug & 0x3FFFFFFF);
	} else if (debug & QED_LOG_INFO_MASK) {
		*p_dp_level = QED_LEVEL_INFO;
	} else if (debug & QED_LOG_NOTICE_MASK) {
		*p_dp_level = QED_LEVEL_NOTICE;
	}
}

2690 2691 2692 2693 2694 2695
static void qede_free_fp_array(struct qede_dev *edev)
{
	if (edev->fp_array) {
		struct qede_fastpath *fp;
		int i;

2696
		for_each_queue(i) {
2697 2698 2699 2700
			fp = &edev->fp_array[i];

			kfree(fp->sb_info);
			kfree(fp->rxq);
M
Mintz, Yuval 已提交
2701
			kfree(fp->xdp_tx);
M
Mintz, Yuval 已提交
2702
			kfree(fp->txq);
2703 2704 2705
		}
		kfree(edev->fp_array);
	}
2706 2707 2708 2709

	edev->num_queues = 0;
	edev->fp_num_tx = 0;
	edev->fp_num_rx = 0;
2710 2711 2712 2713
}

static int qede_alloc_fp_array(struct qede_dev *edev)
{
2714
	u8 fp_combined, fp_rx = edev->fp_num_rx;
2715 2716 2717
	struct qede_fastpath *fp;
	int i;

2718
	edev->fp_array = kcalloc(QEDE_QUEUE_CNT(edev),
2719 2720 2721 2722 2723 2724
				 sizeof(*edev->fp_array), GFP_KERNEL);
	if (!edev->fp_array) {
		DP_NOTICE(edev, "fp array allocation failed\n");
		goto err;
	}

2725 2726 2727 2728 2729 2730 2731 2732
	fp_combined = QEDE_QUEUE_CNT(edev) - fp_rx - edev->fp_num_tx;

	/* Allocate the FP elements for Rx queues followed by combined and then
	 * the Tx. This ordering should be maintained so that the respective
	 * queues (Rx or Tx) will be together in the fastpath array and the
	 * associated ids will be sequential.
	 */
	for_each_queue(i) {
2733 2734
		fp = &edev->fp_array[i];

M
Mintz, Yuval 已提交
2735
		fp->sb_info = kzalloc(sizeof(*fp->sb_info), GFP_KERNEL);
2736 2737 2738 2739 2740
		if (!fp->sb_info) {
			DP_NOTICE(edev, "sb info struct allocation failed\n");
			goto err;
		}

2741 2742 2743 2744 2745 2746 2747 2748
		if (fp_rx) {
			fp->type = QEDE_FASTPATH_RX;
			fp_rx--;
		} else if (fp_combined) {
			fp->type = QEDE_FASTPATH_COMBINED;
			fp_combined--;
		} else {
			fp->type = QEDE_FASTPATH_TX;
2749 2750
		}

2751
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
2752 2753
			fp->txq = kzalloc(sizeof(*fp->txq), GFP_KERNEL);
			if (!fp->txq)
2754 2755 2756 2757
				goto err;
		}

		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
2758 2759
			fp->rxq = kzalloc(sizeof(*fp->rxq), GFP_KERNEL);
			if (!fp->rxq)
2760
				goto err;
M
Mintz, Yuval 已提交
2761

M
Mintz, Yuval 已提交
2762 2763 2764 2765 2766
			if (edev->xdp_prog) {
				fp->xdp_tx = kzalloc(sizeof(*fp->xdp_tx),
						     GFP_KERNEL);
				if (!fp->xdp_tx)
					goto err;
M
Mintz, Yuval 已提交
2767
				fp->type |= QEDE_FASTPATH_XDP;
M
Mintz, Yuval 已提交
2768
			}
2769 2770 2771 2772 2773 2774 2775 2776 2777
		}
	}

	return 0;
err:
	qede_free_fp_array(edev);
	return -ENOMEM;
}

2778 2779 2780 2781
static void qede_sp_task(struct work_struct *work)
{
	struct qede_dev *edev = container_of(work, struct qede_dev,
					     sp_task.work);
2782 2783
	struct qed_dev *cdev = edev->cdev;

M
Mintz, Yuval 已提交
2784
	__qede_lock(edev);
2785

M
Mintz, Yuval 已提交
2786 2787
	if (test_and_clear_bit(QEDE_SP_RX_MODE, &edev->sp_flags))
		if (edev->state == QEDE_STATE_OPEN)
2788 2789
			qede_config_rx_mode(edev->ndev);

2790 2791 2792 2793 2794 2795 2796 2797 2798
	if (test_and_clear_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags)) {
		struct qed_tunn_params tunn_params;

		memset(&tunn_params, 0, sizeof(tunn_params));
		tunn_params.update_vxlan_port = 1;
		tunn_params.vxlan_port = edev->vxlan_dst_port;
		qed_ops->tunn_config(cdev, &tunn_params);
	}

2799 2800 2801 2802 2803 2804 2805 2806 2807
	if (test_and_clear_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags)) {
		struct qed_tunn_params tunn_params;

		memset(&tunn_params, 0, sizeof(tunn_params));
		tunn_params.update_geneve_port = 1;
		tunn_params.geneve_port = edev->geneve_dst_port;
		qed_ops->tunn_config(cdev, &tunn_params);
	}

M
Mintz, Yuval 已提交
2808
	__qede_unlock(edev);
2809 2810
}

Y
Yuval Mintz 已提交
2811 2812 2813 2814
static void qede_update_pf_params(struct qed_dev *cdev)
{
	struct qed_pf_params pf_params;

M
Mintz, Yuval 已提交
2815
	/* 64 rx + 64 tx + 64 XDP */
Y
Yuval Mintz 已提交
2816
	memset(&pf_params, 0, sizeof(struct qed_pf_params));
M
Mintz, Yuval 已提交
2817
	pf_params.eth_pf_params.num_cons = 192;
Y
Yuval Mintz 已提交
2818 2819 2820 2821 2822 2823 2824 2825
	qed_ops->common->update_pf_params(cdev, &pf_params);
}

enum qede_probe_mode {
	QEDE_PROBE_NORMAL,
};

static int __qede_probe(struct pci_dev *pdev, u32 dp_module, u8 dp_level,
Y
Yuval Mintz 已提交
2826
			bool is_vf, enum qede_probe_mode mode)
Y
Yuval Mintz 已提交
2827
{
Y
Yuval Mintz 已提交
2828
	struct qed_probe_params probe_params;
Y
Yuval Mintz 已提交
2829
	struct qed_slowpath_params sp_params;
Y
Yuval Mintz 已提交
2830 2831 2832 2833 2834 2835 2836 2837
	struct qed_dev_eth_info dev_info;
	struct qede_dev *edev;
	struct qed_dev *cdev;
	int rc;

	if (unlikely(dp_level & QED_LEVEL_INFO))
		pr_notice("Starting qede probe\n");

Y
Yuval Mintz 已提交
2838 2839 2840 2841 2842 2843
	memset(&probe_params, 0, sizeof(probe_params));
	probe_params.protocol = QED_PROTOCOL_ETH;
	probe_params.dp_module = dp_module;
	probe_params.dp_level = dp_level;
	probe_params.is_vf = is_vf;
	cdev = qed_ops->common->probe(pdev, &probe_params);
Y
Yuval Mintz 已提交
2844 2845 2846 2847 2848 2849 2850 2851
	if (!cdev) {
		rc = -ENODEV;
		goto err0;
	}

	qede_update_pf_params(cdev);

	/* Start the Slowpath-process */
Y
Yuval Mintz 已提交
2852 2853 2854 2855 2856 2857 2858 2859
	memset(&sp_params, 0, sizeof(sp_params));
	sp_params.int_mode = QED_INT_MODE_MSIX;
	sp_params.drv_major = QEDE_MAJOR_VERSION;
	sp_params.drv_minor = QEDE_MINOR_VERSION;
	sp_params.drv_rev = QEDE_REVISION_VERSION;
	sp_params.drv_eng = QEDE_ENGINEERING_VERSION;
	strlcpy(sp_params.name, "qede LAN", QED_DRV_VER_STR_SIZE);
	rc = qed_ops->common->slowpath_start(cdev, &sp_params);
Y
Yuval Mintz 已提交
2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876
	if (rc) {
		pr_notice("Cannot start slowpath\n");
		goto err1;
	}

	/* Learn information crucial for qede to progress */
	rc = qed_ops->fill_dev_info(cdev, &dev_info);
	if (rc)
		goto err2;

	edev = qede_alloc_etherdev(cdev, pdev, &dev_info, dp_module,
				   dp_level);
	if (!edev) {
		rc = -ENOMEM;
		goto err2;
	}

Y
Yuval Mintz 已提交
2877 2878 2879
	if (is_vf)
		edev->flags |= QEDE_FLAG_IS_VF;

Y
Yuval Mintz 已提交
2880 2881
	qede_init_ndev(edev);

R
Ram Amrani 已提交
2882 2883 2884 2885
	rc = qede_roce_dev_add(edev);
	if (rc)
		goto err3;

2886 2887 2888
	rc = register_netdev(edev->ndev);
	if (rc) {
		DP_NOTICE(edev, "Cannot register net-device\n");
R
Ram Amrani 已提交
2889
		goto err4;
2890 2891
	}

Y
Yuval Mintz 已提交
2892 2893
	edev->ops->common->set_id(cdev, edev->ndev->name, DRV_MODULE_VERSION);

S
Sudarsana Kalluru 已提交
2894 2895
	edev->ops->register_ops(cdev, &qede_ll_ops, edev);

2896
#ifdef CONFIG_DCB
2897 2898
	if (!IS_VF(edev))
		qede_set_dcbnl_ops(edev->ndev);
2899 2900
#endif

2901 2902
	INIT_DELAYED_WORK(&edev->sp_task, qede_sp_task);
	mutex_init(&edev->qede_lock);
2903
	edev->rx_copybreak = QEDE_RX_HDR_SIZE;
2904

Y
Yuval Mintz 已提交
2905 2906 2907 2908
	DP_INFO(edev, "Ending successfully qede probe\n");

	return 0;

R
Ram Amrani 已提交
2909 2910
err4:
	qede_roce_dev_remove(edev);
2911 2912
err3:
	free_netdev(edev->ndev);
Y
Yuval Mintz 已提交
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922
err2:
	qed_ops->common->slowpath_stop(cdev);
err1:
	qed_ops->common->remove(cdev);
err0:
	return rc;
}

static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id)
{
Y
Yuval Mintz 已提交
2923
	bool is_vf = false;
Y
Yuval Mintz 已提交
2924 2925 2926
	u32 dp_module = 0;
	u8 dp_level = 0;

Y
Yuval Mintz 已提交
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
	switch ((enum qede_pci_private)id->driver_data) {
	case QEDE_PRIVATE_VF:
		if (debug & QED_LOG_VERBOSE_MASK)
			dev_err(&pdev->dev, "Probing a VF\n");
		is_vf = true;
		break;
	default:
		if (debug & QED_LOG_VERBOSE_MASK)
			dev_err(&pdev->dev, "Probing a PF\n");
	}

Y
Yuval Mintz 已提交
2938 2939
	qede_config_debug(debug, &dp_module, &dp_level);

Y
Yuval Mintz 已提交
2940
	return __qede_probe(pdev, dp_module, dp_level, is_vf,
Y
Yuval Mintz 已提交
2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955
			    QEDE_PROBE_NORMAL);
}

enum qede_remove_mode {
	QEDE_REMOVE_NORMAL,
};

static void __qede_remove(struct pci_dev *pdev, enum qede_remove_mode mode)
{
	struct net_device *ndev = pci_get_drvdata(pdev);
	struct qede_dev *edev = netdev_priv(ndev);
	struct qed_dev *cdev = edev->cdev;

	DP_INFO(edev, "Starting qede_remove\n");

2956
	cancel_delayed_work_sync(&edev->sp_task);
R
Ram Amrani 已提交
2957

2958 2959
	unregister_netdev(ndev);

R
Ram Amrani 已提交
2960 2961
	qede_roce_dev_remove(edev);

Y
Yuval Mintz 已提交
2962 2963 2964 2965
	edev->ops->common->set_power_state(cdev, PCI_D0);

	pci_set_drvdata(pdev, NULL);

M
Mintz, Yuval 已提交
2966 2967 2968 2969
	/* Release edev's reference to XDP's bpf if such exist */
	if (edev->xdp_prog)
		bpf_prog_put(edev->xdp_prog);

Y
Yuval Mintz 已提交
2970 2971 2972 2973
	free_netdev(ndev);

	/* Use global ops since we've freed edev */
	qed_ops->common->slowpath_stop(cdev);
M
Mintz, Yuval 已提交
2974 2975
	if (system_state == SYSTEM_POWER_OFF)
		return;
Y
Yuval Mintz 已提交
2976 2977
	qed_ops->common->remove(cdev);

Y
Yuval Mintz 已提交
2978
	dev_info(&pdev->dev, "Ending qede_remove successfully\n");
Y
Yuval Mintz 已提交
2979 2980 2981 2982 2983 2984
}

static void qede_remove(struct pci_dev *pdev)
{
	__qede_remove(pdev, QEDE_REMOVE_NORMAL);
}
2985

M
Mintz, Yuval 已提交
2986 2987 2988 2989 2990
static void qede_shutdown(struct pci_dev *pdev)
{
	__qede_remove(pdev, QEDE_REMOVE_NORMAL);
}

2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
/* -------------------------------------------------------------------------
 * START OF LOAD / UNLOAD
 * -------------------------------------------------------------------------
 */

static int qede_set_num_queues(struct qede_dev *edev)
{
	int rc;
	u16 rss_num;

	/* Setup queues according to possible resources*/
3002 3003
	if (edev->req_queues)
		rss_num = edev->req_queues;
3004 3005 3006
	else
		rss_num = netif_get_num_default_rss_queues() *
			  edev->dev_info.common.num_hwfns;
3007 3008 3009 3010 3011 3012

	rss_num = min_t(u16, QEDE_MAX_RSS_CNT(edev), rss_num);

	rc = edev->ops->common->set_fp_int(edev->cdev, rss_num);
	if (rc > 0) {
		/* Managed to request interrupts for our queues */
3013
		edev->num_queues = rc;
3014
		DP_INFO(edev, "Managed %d [of %d] RSS queues\n",
3015
			QEDE_QUEUE_CNT(edev), rss_num);
3016 3017
		rc = 0;
	}
3018 3019 3020 3021

	edev->fp_num_tx = edev->req_num_tx;
	edev->fp_num_rx = edev->req_num_rx;

3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
	return rc;
}

static void qede_free_mem_sb(struct qede_dev *edev,
			     struct qed_sb_info *sb_info)
{
	if (sb_info->sb_virt)
		dma_free_coherent(&edev->pdev->dev, sizeof(*sb_info->sb_virt),
				  (void *)sb_info->sb_virt, sb_info->sb_phys);
}

/* This function allocates fast-path status block memory */
static int qede_alloc_mem_sb(struct qede_dev *edev,
Y
Yuval Mintz 已提交
3035
			     struct qed_sb_info *sb_info, u16 sb_id)
3036 3037 3038 3039 3040 3041
{
	struct status_block *sb_virt;
	dma_addr_t sb_phys;
	int rc;

	sb_virt = dma_alloc_coherent(&edev->pdev->dev,
Y
Yuval Mintz 已提交
3042
				     sizeof(*sb_virt), &sb_phys, GFP_KERNEL);
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
	if (!sb_virt) {
		DP_ERR(edev, "Status block allocation failed\n");
		return -ENOMEM;
	}

	rc = edev->ops->common->sb_init(edev->cdev, sb_info,
					sb_virt, sb_phys, sb_id,
					QED_SB_TYPE_L2_QUEUE);
	if (rc) {
		DP_ERR(edev, "Status block initialization failed\n");
		dma_free_coherent(&edev->pdev->dev, sizeof(*sb_virt),
				  sb_virt, sb_phys);
		return rc;
	}

	return 0;
}

static void qede_free_rx_buffers(struct qede_dev *edev,
				 struct qede_rx_queue *rxq)
{
	u16 i;

	for (i = rxq->sw_rx_cons; i != rxq->sw_rx_prod; i++) {
		struct sw_rx_data *rx_buf;
Y
Yuval Mintz 已提交
3068
		struct page *data;
3069 3070 3071 3072

		rx_buf = &rxq->sw_rx_ring[i & NUM_RX_BDS_MAX];
		data = rx_buf->data;

Y
Yuval Mintz 已提交
3073
		dma_unmap_page(&edev->pdev->dev,
M
Mintz, Yuval 已提交
3074
			       rx_buf->mapping, PAGE_SIZE, rxq->data_direction);
3075 3076

		rx_buf->data = NULL;
Y
Yuval Mintz 已提交
3077
		__free_page(data);
3078 3079 3080
	}
}

Y
Yuval Mintz 已提交
3081 3082
static void qede_free_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
{
3083 3084 3085 3086 3087 3088 3089
	int i;

	if (edev->gro_disable)
		return;

	for (i = 0; i < ETH_TPA_MAX_AGGS_NUM; i++) {
		struct qede_agg_info *tpa_info = &rxq->tpa_info[i];
3090
		struct sw_rx_data *replace_buf = &tpa_info->buffer;
3091

3092
		if (replace_buf->data) {
3093
			dma_unmap_page(&edev->pdev->dev,
M
Manish Chopra 已提交
3094
				       replace_buf->mapping,
3095 3096 3097 3098 3099 3100
				       PAGE_SIZE, DMA_FROM_DEVICE);
			__free_page(replace_buf->data);
		}
	}
}

Y
Yuval Mintz 已提交
3101
static void qede_free_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
3102
{
3103 3104
	qede_free_sge_mem(edev, rxq);

3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115
	/* Free rx buffers */
	qede_free_rx_buffers(edev, rxq);

	/* Free the parallel SW ring */
	kfree(rxq->sw_rx_ring);

	/* Free the real RQ ring used by FW */
	edev->ops->common->chain_free(edev->cdev, &rxq->rx_bd_ring);
	edev->ops->common->chain_free(edev->cdev, &rxq->rx_comp_ring);
}

Y
Yuval Mintz 已提交
3116
static int qede_alloc_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
3117 3118 3119 3120
{
	dma_addr_t mapping;
	int i;

M
Mintz, Yuval 已提交
3121 3122 3123 3124
	/* Don't perform FW aggregations in case of XDP */
	if (edev->xdp_prog)
		edev->gro_disable = 1;

3125 3126 3127 3128 3129 3130 3131 3132 3133 3134
	if (edev->gro_disable)
		return 0;

	if (edev->ndev->mtu > PAGE_SIZE) {
		edev->gro_disable = 1;
		return 0;
	}

	for (i = 0; i < ETH_TPA_MAX_AGGS_NUM; i++) {
		struct qede_agg_info *tpa_info = &rxq->tpa_info[i];
3135
		struct sw_rx_data *replace_buf = &tpa_info->buffer;
3136 3137 3138 3139 3140 3141 3142 3143 3144

		replace_buf->data = alloc_pages(GFP_ATOMIC, 0);
		if (unlikely(!replace_buf->data)) {
			DP_NOTICE(edev,
				  "Failed to allocate TPA skb pool [replacement buffer]\n");
			goto err;
		}

		mapping = dma_map_page(&edev->pdev->dev, replace_buf->data, 0,
3145
				       PAGE_SIZE, DMA_FROM_DEVICE);
3146 3147 3148 3149 3150 3151
		if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
			DP_NOTICE(edev,
				  "Failed to map TPA replacement buffer\n");
			goto err;
		}

M
Manish Chopra 已提交
3152
		replace_buf->mapping = mapping;
3153 3154 3155
		tpa_info->buffer.page_offset = 0;
		tpa_info->buffer_mapping = mapping;
		tpa_info->state = QEDE_AGG_STATE_NONE;
3156 3157 3158 3159 3160 3161 3162 3163 3164
	}

	return 0;
err:
	qede_free_sge_mem(edev, rxq);
	edev->gro_disable = 1;
	return -ENOMEM;
}

3165
/* This function allocates all memory needed per Rx queue */
Y
Yuval Mintz 已提交
3166
static int qede_alloc_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
3167
{
3168
	int i, rc, size;
3169 3170 3171

	rxq->num_rx_buffers = edev->q_num_rx_buffers;

Y
Yuval Mintz 已提交
3172 3173
	rxq->rx_buf_size = NET_IP_ALIGN + ETH_OVERHEAD + edev->ndev->mtu;

Y
Yuval Mintz 已提交
3174 3175 3176
	if (rxq->rx_buf_size > PAGE_SIZE)
		rxq->rx_buf_size = PAGE_SIZE;

M
Mintz, Yuval 已提交
3177 3178 3179 3180 3181 3182 3183
	/* Segment size to spilt a page in multiple equal parts,
	 * unless XDP is used in which case we'd use the entire page.
	 */
	if (!edev->xdp_prog)
		rxq->rx_buf_seg_size = roundup_pow_of_two(rxq->rx_buf_size);
	else
		rxq->rx_buf_seg_size = PAGE_SIZE;
3184 3185

	/* Allocate the parallel driver ring for Rx buffers */
Y
Yuval Mintz 已提交
3186
	size = sizeof(*rxq->sw_rx_ring) * RX_RING_SIZE;
3187 3188 3189
	rxq->sw_rx_ring = kzalloc(size, GFP_KERNEL);
	if (!rxq->sw_rx_ring) {
		DP_ERR(edev, "Rx buffers ring allocation failed\n");
3190
		rc = -ENOMEM;
3191 3192 3193 3194 3195 3196 3197
		goto err;
	}

	/* Allocate FW Rx ring  */
	rc = edev->ops->common->chain_alloc(edev->cdev,
					    QED_CHAIN_USE_TO_CONSUME_PRODUCE,
					    QED_CHAIN_MODE_NEXT_PTR,
Y
Yuval Mintz 已提交
3198
					    QED_CHAIN_CNT_TYPE_U16,
Y
Yuval Mintz 已提交
3199
					    RX_RING_SIZE,
3200 3201 3202 3203 3204 3205 3206 3207 3208 3209
					    sizeof(struct eth_rx_bd),
					    &rxq->rx_bd_ring);

	if (rc)
		goto err;

	/* Allocate FW completion ring */
	rc = edev->ops->common->chain_alloc(edev->cdev,
					    QED_CHAIN_USE_TO_CONSUME,
					    QED_CHAIN_MODE_PBL,
Y
Yuval Mintz 已提交
3210
					    QED_CHAIN_CNT_TYPE_U16,
Y
Yuval Mintz 已提交
3211
					    RX_RING_SIZE,
3212 3213 3214 3215 3216 3217 3218
					    sizeof(union eth_rx_cqe),
					    &rxq->rx_comp_ring);
	if (rc)
		goto err;

	/* Allocate buffers for the Rx ring */
	for (i = 0; i < rxq->num_rx_buffers; i++) {
3219
		rc = qede_alloc_rx_buffer(rxq);
3220 3221 3222 3223 3224
		if (rc) {
			DP_ERR(edev,
			       "Rx buffers allocation failed at index %d\n", i);
			goto err;
		}
3225 3226
	}

3227
	rc = qede_alloc_sge_mem(edev, rxq);
3228
err:
3229
	return rc;
3230 3231
}

Y
Yuval Mintz 已提交
3232
static void qede_free_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
3233 3234
{
	/* Free the parallel SW ring */
M
Mintz, Yuval 已提交
3235 3236 3237 3238
	if (txq->is_xdp)
		kfree(txq->sw_tx_ring.pages);
	else
		kfree(txq->sw_tx_ring.skbs);
3239 3240 3241 3242 3243 3244

	/* Free the real RQ ring used by FW */
	edev->ops->common->chain_free(edev->cdev, &txq->tx_pbl);
}

/* This function allocates all memory needed per Tx queue */
Y
Yuval Mintz 已提交
3245
static int qede_alloc_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
3246 3247
{
	union eth_tx_bd_types *p_virt;
M
Mintz, Yuval 已提交
3248
	int size, rc;
3249 3250 3251 3252

	txq->num_tx_buffers = edev->q_num_tx_buffers;

	/* Allocate the parallel driver ring for Tx buffers */
M
Mintz, Yuval 已提交
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262
	if (txq->is_xdp) {
		size = sizeof(*txq->sw_tx_ring.pages) * TX_RING_SIZE;
		txq->sw_tx_ring.pages = kzalloc(size, GFP_KERNEL);
		if (!txq->sw_tx_ring.pages)
			goto err;
	} else {
		size = sizeof(*txq->sw_tx_ring.skbs) * TX_RING_SIZE;
		txq->sw_tx_ring.skbs = kzalloc(size, GFP_KERNEL);
		if (!txq->sw_tx_ring.skbs)
			goto err;
3263 3264 3265 3266 3267
	}

	rc = edev->ops->common->chain_alloc(edev->cdev,
					    QED_CHAIN_USE_TO_CONSUME_PRODUCE,
					    QED_CHAIN_MODE_PBL,
Y
Yuval Mintz 已提交
3268
					    QED_CHAIN_CNT_TYPE_U16,
3269
					    TX_RING_SIZE,
Y
Yuval Mintz 已提交
3270
					    sizeof(*p_virt), &txq->tx_pbl);
3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281
	if (rc)
		goto err;

	return 0;

err:
	qede_free_mem_txq(edev, txq);
	return -ENOMEM;
}

/* This function frees all memory of a single fp */
Y
Yuval Mintz 已提交
3282
static void qede_free_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
3283 3284 3285
{
	qede_free_mem_sb(edev, fp->sb_info);

3286 3287
	if (fp->type & QEDE_FASTPATH_RX)
		qede_free_mem_rxq(edev, fp->rxq);
3288

3289
	if (fp->type & QEDE_FASTPATH_TX)
M
Mintz, Yuval 已提交
3290
		qede_free_mem_txq(edev, fp->txq);
3291 3292 3293
}

/* This function allocates all memory needed for a single fp (i.e. an entity
3294
 * which contains status block, one rx queue and/or multiple per-TC tx queues.
3295
 */
Y
Yuval Mintz 已提交
3296
static int qede_alloc_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
3297
{
M
Mintz, Yuval 已提交
3298
	int rc = 0;
3299

3300
	rc = qede_alloc_mem_sb(edev, fp->sb_info, fp->id);
3301
	if (rc)
M
Mintz, Yuval 已提交
3302
		goto out;
3303

3304 3305
	if (fp->type & QEDE_FASTPATH_RX) {
		rc = qede_alloc_mem_rxq(edev, fp->rxq);
3306
		if (rc)
M
Mintz, Yuval 已提交
3307 3308 3309 3310 3311 3312 3313
			goto out;
	}

	if (fp->type & QEDE_FASTPATH_XDP) {
		rc = qede_alloc_mem_txq(edev, fp->xdp_tx);
		if (rc)
			goto out;
3314 3315
	}

3316
	if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
3317 3318
		rc = qede_alloc_mem_txq(edev, fp->txq);
		if (rc)
M
Mintz, Yuval 已提交
3319
			goto out;
3320 3321
	}

M
Mintz, Yuval 已提交
3322
out:
3323
	return rc;
3324 3325 3326 3327 3328 3329
}

static void qede_free_mem_load(struct qede_dev *edev)
{
	int i;

3330
	for_each_queue(i) {
3331 3332 3333 3334 3335 3336 3337 3338 3339
		struct qede_fastpath *fp = &edev->fp_array[i];

		qede_free_mem_fp(edev, fp);
	}
}

/* This function allocates all qede memory at NIC load. */
static int qede_alloc_mem_load(struct qede_dev *edev)
{
3340
	int rc = 0, queue_id;
3341

3342 3343
	for (queue_id = 0; queue_id < QEDE_QUEUE_CNT(edev); queue_id++) {
		struct qede_fastpath *fp = &edev->fp_array[queue_id];
3344 3345

		rc = qede_alloc_mem_fp(edev, fp);
3346
		if (rc) {
3347
			DP_ERR(edev,
3348
			       "Failed to allocate memory for fastpath - rss id = %d\n",
3349
			       queue_id);
3350 3351
			qede_free_mem_load(edev);
			return rc;
3352 3353 3354 3355 3356 3357 3358 3359 3360
		}
	}

	return 0;
}

/* This function inits fp content and resets the SB, RXQ and TXQ structures */
static void qede_init_fp(struct qede_dev *edev)
{
M
Mintz, Yuval 已提交
3361
	int queue_id, rxq_index = 0, txq_index = 0;
3362 3363
	struct qede_fastpath *fp;

3364 3365
	for_each_queue(queue_id) {
		fp = &edev->fp_array[queue_id];
3366 3367

		fp->edev = edev;
3368
		fp->id = queue_id;
3369

M
Mintz, Yuval 已提交
3370 3371 3372 3373 3374
		if (fp->type & QEDE_FASTPATH_XDP) {
			fp->xdp_tx->index = QEDE_TXQ_IDX_TO_XDP(edev,
								rxq_index);
			fp->xdp_tx->is_xdp = 1;
		}
3375

3376 3377
		if (fp->type & QEDE_FASTPATH_RX) {
			fp->rxq->rxq_id = rxq_index++;
M
Mintz, Yuval 已提交
3378 3379 3380 3381 3382 3383

			/* Determine how to map buffers for this queue */
			if (fp->type & QEDE_FASTPATH_XDP)
				fp->rxq->data_direction = DMA_BIDIRECTIONAL;
			else
				fp->rxq->data_direction = DMA_FROM_DEVICE;
3384
			fp->rxq->dev = &edev->pdev->dev;
3385
		}
3386

3387
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
3388 3389 3390
			fp->txq->index = txq_index++;
			if (edev->dev_info.is_legacy)
				fp->txq->is_legacy = 1;
3391
			fp->txq->dev = &edev->pdev->dev;
3392 3393 3394
		}

		snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
3395
			 edev->ndev->name, queue_id);
3396
	}
3397 3398

	edev->gro_disable = !(edev->ndev->features & NETIF_F_GRO);
3399 3400 3401 3402 3403 3404
}

static int qede_set_real_num_queues(struct qede_dev *edev)
{
	int rc = 0;

3405
	rc = netif_set_real_num_tx_queues(edev->ndev, QEDE_TSS_COUNT(edev));
3406 3407 3408 3409
	if (rc) {
		DP_NOTICE(edev, "Failed to set real number of Tx queues\n");
		return rc;
	}
3410 3411

	rc = netif_set_real_num_rx_queues(edev->ndev, QEDE_RSS_COUNT(edev));
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423
	if (rc) {
		DP_NOTICE(edev, "Failed to set real number of Rx queues\n");
		return rc;
	}

	return 0;
}

static void qede_napi_disable_remove(struct qede_dev *edev)
{
	int i;

3424
	for_each_queue(i) {
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
		napi_disable(&edev->fp_array[i].napi);

		netif_napi_del(&edev->fp_array[i].napi);
	}
}

static void qede_napi_add_enable(struct qede_dev *edev)
{
	int i;

	/* Add NAPI objects */
3436
	for_each_queue(i) {
3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
		netif_napi_add(edev->ndev, &edev->fp_array[i].napi,
			       qede_poll, NAPI_POLL_WEIGHT);
		napi_enable(&edev->fp_array[i].napi);
	}
}

static void qede_sync_free_irqs(struct qede_dev *edev)
{
	int i;

	for (i = 0; i < edev->int_info.used_cnt; i++) {
		if (edev->int_info.msix_cnt) {
			synchronize_irq(edev->int_info.msix[i].vector);
			free_irq(edev->int_info.msix[i].vector,
				 &edev->fp_array[i]);
		} else {
			edev->ops->common->simd_handler_clean(edev->cdev, i);
		}
	}

	edev->int_info.used_cnt = 0;
}

static int qede_req_msix_irqs(struct qede_dev *edev)
{
	int i, rc;

	/* Sanitize number of interrupts == number of prepared RSS queues */
3465
	if (QEDE_QUEUE_CNT(edev) > edev->int_info.msix_cnt) {
3466 3467
		DP_ERR(edev,
		       "Interrupt mismatch: %d RSS queues > %d MSI-x vectors\n",
3468
		       QEDE_QUEUE_CNT(edev), edev->int_info.msix_cnt);
3469 3470 3471
		return -EINVAL;
	}

3472
	for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
		rc = request_irq(edev->int_info.msix[i].vector,
				 qede_msix_fp_int, 0, edev->fp_array[i].name,
				 &edev->fp_array[i]);
		if (rc) {
			DP_ERR(edev, "Request fp %d irq failed\n", i);
			qede_sync_free_irqs(edev);
			return rc;
		}
		DP_VERBOSE(edev, NETIF_MSG_INTR,
			   "Requested fp irq for %s [entry %d]. Cookie is at %p\n",
			   edev->fp_array[i].name, i,
			   &edev->fp_array[i]);
		edev->int_info.used_cnt++;
	}

	return 0;
}

static void qede_simd_fp_handler(void *cookie)
{
	struct qede_fastpath *fp = (struct qede_fastpath *)cookie;

	napi_schedule_irqoff(&fp->napi);
}

static int qede_setup_irqs(struct qede_dev *edev)
{
	int i, rc = 0;

	/* Learn Interrupt configuration */
	rc = edev->ops->common->get_fp_int(edev->cdev, &edev->int_info);
	if (rc)
		return rc;

	if (edev->int_info.msix_cnt) {
		rc = qede_req_msix_irqs(edev);
		if (rc)
			return rc;
		edev->ndev->irq = edev->int_info.msix[0].vector;
	} else {
		const struct qed_common_ops *ops;

		/* qed should learn receive the RSS ids and callbacks */
		ops = edev->ops->common;
3517
		for (i = 0; i < QEDE_QUEUE_CNT(edev); i++)
3518 3519 3520
			ops->simd_handler_config(edev->cdev,
						 &edev->fp_array[i], i,
						 qede_simd_fp_handler);
3521
		edev->int_info.used_cnt = QEDE_QUEUE_CNT(edev);
3522 3523 3524 3525 3526
	}
	return 0;
}

static int qede_drain_txq(struct qede_dev *edev,
Y
Yuval Mintz 已提交
3527
			  struct qede_tx_queue *txq, bool allow_drain)
3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558
{
	int rc, cnt = 1000;

	while (txq->sw_tx_cons != txq->sw_tx_prod) {
		if (!cnt) {
			if (allow_drain) {
				DP_NOTICE(edev,
					  "Tx queue[%d] is stuck, requesting MCP to drain\n",
					  txq->index);
				rc = edev->ops->common->drain(edev->cdev);
				if (rc)
					return rc;
				return qede_drain_txq(edev, txq, false);
			}
			DP_NOTICE(edev,
				  "Timeout waiting for tx queue[%d]: PROD=%d, CONS=%d\n",
				  txq->index, txq->sw_tx_prod,
				  txq->sw_tx_cons);
			return -ENODEV;
		}
		cnt--;
		usleep_range(1000, 2000);
		barrier();
	}

	/* FW finished processing, wait for HW to transmit all tx packets */
	usleep_range(1000, 2000);

	return 0;
}

M
Mintz, Yuval 已提交
3559 3560 3561 3562 3563 3564
static int qede_stop_txq(struct qede_dev *edev,
			 struct qede_tx_queue *txq, int rss_id)
{
	return edev->ops->q_tx_stop(edev->cdev, rss_id, txq->handle);
}

3565 3566 3567 3568
static int qede_stop_queues(struct qede_dev *edev)
{
	struct qed_update_vport_params vport_update_params;
	struct qed_dev *cdev = edev->cdev;
M
Mintz, Yuval 已提交
3569 3570
	struct qede_fastpath *fp;
	int rc, i;
3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585

	/* Disable the vport */
	memset(&vport_update_params, 0, sizeof(vport_update_params));
	vport_update_params.vport_id = 0;
	vport_update_params.update_vport_active_flg = 1;
	vport_update_params.vport_active_flg = 0;
	vport_update_params.update_rss_flg = 0;

	rc = edev->ops->vport_update(cdev, &vport_update_params);
	if (rc) {
		DP_ERR(edev, "Failed to update vport\n");
		return rc;
	}

	/* Flush Tx queues. If needed, request drain from MCP */
3586
	for_each_queue(i) {
M
Mintz, Yuval 已提交
3587
		fp = &edev->fp_array[i];
3588

3589
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
3590 3591 3592
			rc = qede_drain_txq(edev, fp->txq, true);
			if (rc)
				return rc;
3593
		}
M
Mintz, Yuval 已提交
3594 3595 3596 3597 3598 3599

		if (fp->type & QEDE_FASTPATH_XDP) {
			rc = qede_drain_txq(edev, fp->xdp_tx, true);
			if (rc)
				return rc;
		}
3600 3601
	}

3602 3603
	/* Stop all Queues in reverse order */
	for (i = QEDE_QUEUE_CNT(edev) - 1; i >= 0; i--) {
M
Mintz, Yuval 已提交
3604 3605
		fp = &edev->fp_array[i];

3606
		/* Stop the Tx Queue(s) */
M
Mintz, Yuval 已提交
3607
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
3608 3609 3610
			rc = qede_stop_txq(edev, fp->txq, i);
			if (rc)
				return rc;
3611 3612
		}

3613
		/* Stop the Rx Queue */
M
Mintz, Yuval 已提交
3614
		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
3615
			rc = edev->ops->q_rx_stop(cdev, i, fp->rxq->handle);
3616 3617 3618 3619
			if (rc) {
				DP_ERR(edev, "Failed to stop RXQ #%d\n", i);
				return rc;
			}
3620
		}
M
Mintz, Yuval 已提交
3621

M
Mintz, Yuval 已提交
3622 3623 3624 3625 3626 3627
		/* Stop the XDP forwarding queue */
		if (fp->type & QEDE_FASTPATH_XDP) {
			rc = qede_stop_txq(edev, fp->xdp_tx, i);
			if (rc)
				return rc;

M
Mintz, Yuval 已提交
3628
			bpf_prog_put(fp->rxq->xdp_prog);
M
Mintz, Yuval 已提交
3629
		}
3630 3631 3632 3633 3634 3635 3636 3637 3638 3639
	}

	/* Stop the vport */
	rc = edev->ops->vport_stop(cdev, 0);
	if (rc)
		DP_ERR(edev, "Failed to stop VPORT\n");

	return rc;
}

M
Mintz, Yuval 已提交
3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652
static int qede_start_txq(struct qede_dev *edev,
			  struct qede_fastpath *fp,
			  struct qede_tx_queue *txq, u8 rss_id, u16 sb_idx)
{
	dma_addr_t phys_table = qed_chain_get_pbl_phys(&txq->tx_pbl);
	u32 page_cnt = qed_chain_get_page_cnt(&txq->tx_pbl);
	struct qed_queue_start_common_params params;
	struct qed_txq_start_ret_params ret_params;
	int rc;

	memset(&params, 0, sizeof(params));
	memset(&ret_params, 0, sizeof(ret_params));

M
Mintz, Yuval 已提交
3653 3654 3655 3656 3657 3658 3659 3660
	/* Let the XDP queue share the queue-zone with one of the regular txq.
	 * We don't really care about its coalescing.
	 */
	if (txq->is_xdp)
		params.queue_id = QEDE_TXQ_XDP_TO_IDX(edev, txq);
	else
		params.queue_id = txq->index;

M
Mintz, Yuval 已提交
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
	params.sb = fp->sb_info->igu_sb_id;
	params.sb_idx = sb_idx;

	rc = edev->ops->q_tx_start(edev->cdev, rss_id, &params, phys_table,
				   page_cnt, &ret_params);
	if (rc) {
		DP_ERR(edev, "Start TXQ #%d failed %d\n", txq->index, rc);
		return rc;
	}

	txq->doorbell_addr = ret_params.p_doorbell;
	txq->handle = ret_params.p_handle;

	/* Determine the FW consumer address associated */
	txq->hw_cons_ptr = &fp->sb_info->sb_virt->pi_array[sb_idx];

	/* Prepare the doorbell parameters */
	SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_DEST, DB_DEST_XCM);
	SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
	SET_FIELD(txq->tx_db.data.params, ETH_DB_DATA_AGG_VAL_SEL,
		  DQ_XCM_ETH_TX_BD_PROD_CMD);
	txq->tx_db.data.agg_flags = DQ_XCM_ETH_DQ_CF_CMD;

	return rc;
}

3687
static int qede_start_queues(struct qede_dev *edev, bool clear_stats)
3688
{
3689
	int vlan_removal_en = 1;
3690 3691 3692
	struct qed_dev *cdev = edev->cdev;
	struct qed_update_vport_params vport_update_params;
	struct qed_queue_start_common_params q_params;
Y
Yuval Mintz 已提交
3693
	struct qed_dev_info *qed_info = &edev->dev_info.common;
3694
	struct qed_start_vport_params start = {0};
3695
	bool reset_rss_indir = false;
M
Mintz, Yuval 已提交
3696
	int rc, i;
3697

3698
	if (!edev->num_queues) {
3699 3700 3701 3702 3703
		DP_ERR(edev,
		       "Cannot update V-VPORT as active as there are no Rx queues\n");
		return -EINVAL;
	}

3704
	start.gro_enable = !edev->gro_disable;
3705 3706 3707 3708
	start.mtu = edev->ndev->mtu;
	start.vport_id = 0;
	start.drop_ttl0 = true;
	start.remove_inner_vlan = vlan_removal_en;
3709
	start.clear_stats = clear_stats;
3710 3711

	rc = edev->ops->vport_start(cdev, &start);
3712 3713 3714 3715 3716 3717 3718 3719

	if (rc) {
		DP_ERR(edev, "Start V-PORT failed %d\n", rc);
		return rc;
	}

	DP_VERBOSE(edev, NETIF_MSG_IFUP,
		   "Start vport ramrod passed, vport_id = %d, MTU = %d, vlan_removal_en = %d\n",
3720
		   start.vport_id, edev->ndev->mtu + 0xe, vlan_removal_en);
3721

3722
	for_each_queue(i) {
3723
		struct qede_fastpath *fp = &edev->fp_array[i];
3724 3725
		dma_addr_t p_phys_table;
		u32 page_cnt;
3726

3727
		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
3728
			struct qed_rxq_start_ret_params ret_params;
3729 3730
			struct qede_rx_queue *rxq = fp->rxq;
			__le16 *val;
3731

M
Mintz, Yuval 已提交
3732
			memset(&ret_params, 0, sizeof(ret_params));
3733 3734 3735 3736 3737 3738 3739 3740 3741 3742
			memset(&q_params, 0, sizeof(q_params));
			q_params.queue_id = rxq->rxq_id;
			q_params.vport_id = 0;
			q_params.sb = fp->sb_info->igu_sb_id;
			q_params.sb_idx = RX_PI;

			p_phys_table =
			    qed_chain_get_pbl_phys(&rxq->rx_comp_ring);
			page_cnt = qed_chain_get_page_cnt(&rxq->rx_comp_ring);

M
Mintz, Yuval 已提交
3743
			rc = edev->ops->q_rx_start(cdev, i, &q_params,
3744 3745 3746
						   rxq->rx_buf_size,
						   rxq->rx_bd_ring.p_phys_addr,
						   p_phys_table,
M
Mintz, Yuval 已提交
3747
						   page_cnt, &ret_params);
3748 3749 3750 3751 3752 3753
			if (rc) {
				DP_ERR(edev, "Start RXQ #%d failed %d\n", i,
				       rc);
				return rc;
			}

M
Mintz, Yuval 已提交
3754 3755 3756 3757
			/* Use the return parameters */
			rxq->hw_rxq_prod_addr = ret_params.p_prod;
			rxq->handle = ret_params.p_handle;

3758 3759 3760 3761 3762 3763
			val = &fp->sb_info->sb_virt->pi_array[RX_PI];
			rxq->hw_cons_ptr = val;

			qede_update_rx_prod(edev, rxq);
		}

M
Mintz, Yuval 已提交
3764
		if (fp->type & QEDE_FASTPATH_XDP) {
M
Mintz, Yuval 已提交
3765 3766 3767 3768
			rc = qede_start_txq(edev, fp, fp->xdp_tx, i, XDP_PI);
			if (rc)
				return rc;

M
Mintz, Yuval 已提交
3769 3770 3771 3772 3773 3774 3775 3776
			fp->rxq->xdp_prog = bpf_prog_add(edev->xdp_prog, 1);
			if (IS_ERR(fp->rxq->xdp_prog)) {
				rc = PTR_ERR(fp->rxq->xdp_prog);
				fp->rxq->xdp_prog = NULL;
				return rc;
			}
		}

M
Mintz, Yuval 已提交
3777
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
3778 3779
			rc = qede_start_txq(edev, fp, fp->txq, i, TX_PI(0));
			if (rc)
3780 3781 3782 3783 3784 3785
				return rc;
		}
	}

	/* Prepare and send the vport enable */
	memset(&vport_update_params, 0, sizeof(vport_update_params));
3786
	vport_update_params.vport_id = start.vport_id;
3787 3788 3789
	vport_update_params.update_vport_active_flg = 1;
	vport_update_params.vport_active_flg = 1;

Y
Yuval Mintz 已提交
3790 3791 3792 3793 3794 3795
	if ((qed_info->mf_mode == QED_MF_NPAR || pci_num_vf(edev->pdev)) &&
	    qed_info->tx_switching) {
		vport_update_params.update_tx_switching_flg = 1;
		vport_update_params.tx_switching_flg = 1;
	}

3796
	/* Fill struct with RSS params */
3797
	if (QEDE_RSS_COUNT(edev) > 1) {
3798
		vport_update_params.update_rss_flg = 1;
3799 3800 3801 3802

		/* Need to validate current RSS config uses valid entries */
		for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++) {
			if (edev->rss_params.rss_ind_table[i] >=
3803
			    QEDE_RSS_COUNT(edev)) {
3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
				reset_rss_indir = true;
				break;
			}
		}

		if (!(edev->rss_params_inited & QEDE_RSS_INDIR_INITED) ||
		    reset_rss_indir) {
			u16 val;

			for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++) {
				u16 indir_val;

3816
				val = QEDE_RSS_COUNT(edev);
3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838
				indir_val = ethtool_rxfh_indir_default(i, val);
				edev->rss_params.rss_ind_table[i] = indir_val;
			}
			edev->rss_params_inited |= QEDE_RSS_INDIR_INITED;
		}

		if (!(edev->rss_params_inited & QEDE_RSS_KEY_INITED)) {
			netdev_rss_key_fill(edev->rss_params.rss_key,
					    sizeof(edev->rss_params.rss_key));
			edev->rss_params_inited |= QEDE_RSS_KEY_INITED;
		}

		if (!(edev->rss_params_inited & QEDE_RSS_CAPS_INITED)) {
			edev->rss_params.rss_caps = QED_RSS_IPV4 |
						    QED_RSS_IPV6 |
						    QED_RSS_IPV4_TCP |
						    QED_RSS_IPV6_TCP;
			edev->rss_params_inited |= QEDE_RSS_CAPS_INITED;
		}

		memcpy(&vport_update_params.rss_params, &edev->rss_params,
		       sizeof(vport_update_params.rss_params));
3839
	} else {
3840 3841
		memset(&vport_update_params.rss_params, 0,
		       sizeof(vport_update_params.rss_params));
3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852
	}

	rc = edev->ops->vport_update(cdev, &vport_update_params);
	if (rc) {
		DP_ERR(edev, "Update V-PORT failed %d\n", rc);
		return rc;
	}

	return 0;
}

3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870
static int qede_set_mcast_rx_mac(struct qede_dev *edev,
				 enum qed_filter_xcast_params_type opcode,
				 unsigned char *mac, int num_macs)
{
	struct qed_filter_params filter_cmd;
	int i;

	memset(&filter_cmd, 0, sizeof(filter_cmd));
	filter_cmd.type = QED_FILTER_TYPE_MCAST;
	filter_cmd.filter.mcast.type = opcode;
	filter_cmd.filter.mcast.num = num_macs;

	for (i = 0; i < num_macs; i++, mac += ETH_ALEN)
		ether_addr_copy(filter_cmd.filter.mcast.mac[i], mac);

	return edev->ops->filter_config(edev->cdev, &filter_cmd);
}

3871 3872 3873 3874
enum qede_unload_mode {
	QEDE_UNLOAD_NORMAL,
};

M
Mintz, Yuval 已提交
3875 3876
static void qede_unload(struct qede_dev *edev, enum qede_unload_mode mode,
			bool is_locked)
3877
{
S
Sudarsana Kalluru 已提交
3878
	struct qed_link_params link_params;
3879 3880 3881 3882
	int rc;

	DP_INFO(edev, "Starting qede unload\n");

M
Mintz, Yuval 已提交
3883 3884 3885
	if (!is_locked)
		__qede_lock(edev);

R
Ram Amrani 已提交
3886
	qede_roce_dev_event_close(edev);
3887 3888
	edev->state = QEDE_STATE_CLOSED;

3889 3890 3891 3892
	/* Close OS Tx */
	netif_tx_disable(edev->ndev);
	netif_carrier_off(edev->ndev);

S
Sudarsana Kalluru 已提交
3893 3894 3895 3896
	/* Reset the link */
	memset(&link_params, 0, sizeof(link_params));
	link_params.link_up = false;
	edev->ops->common->set_link(edev->cdev, &link_params);
3897 3898 3899 3900 3901 3902 3903 3904
	rc = qede_stop_queues(edev);
	if (rc) {
		qede_sync_free_irqs(edev);
		goto out;
	}

	DP_INFO(edev, "Stopped Queues\n");

3905
	qede_vlan_mark_nonconfigured(edev);
3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917
	edev->ops->fastpath_stop(edev->cdev);

	/* Release the interrupts */
	qede_sync_free_irqs(edev);
	edev->ops->common->set_fp_int(edev->cdev, 0);

	qede_napi_disable_remove(edev);

	qede_free_mem_load(edev);
	qede_free_fp_array(edev);

out:
M
Mintz, Yuval 已提交
3918 3919
	if (!is_locked)
		__qede_unlock(edev);
3920 3921 3922 3923 3924
	DP_INFO(edev, "Ending qede unload\n");
}

enum qede_load_mode {
	QEDE_LOAD_NORMAL,
3925
	QEDE_LOAD_RELOAD,
3926 3927
};

M
Mintz, Yuval 已提交
3928 3929
static int qede_load(struct qede_dev *edev, enum qede_load_mode mode,
		     bool is_locked)
3930
{
S
Sudarsana Kalluru 已提交
3931 3932
	struct qed_link_params link_params;
	struct qed_link_output link_output;
3933 3934 3935 3936
	int rc;

	DP_INFO(edev, "Starting qede load\n");

M
Mintz, Yuval 已提交
3937 3938 3939
	if (!is_locked)
		__qede_lock(edev);

3940 3941
	rc = qede_set_num_queues(edev);
	if (rc)
M
Mintz, Yuval 已提交
3942
		goto out;
3943 3944 3945

	rc = qede_alloc_fp_array(edev);
	if (rc)
M
Mintz, Yuval 已提交
3946
		goto out;
3947 3948 3949 3950 3951 3952

	qede_init_fp(edev);

	rc = qede_alloc_mem_load(edev);
	if (rc)
		goto err1;
M
Mintz, Yuval 已提交
3953 3954
	DP_INFO(edev, "Allocated %d Rx, %d Tx queues\n",
		QEDE_RSS_COUNT(edev), QEDE_TSS_COUNT(edev));
3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967

	rc = qede_set_real_num_queues(edev);
	if (rc)
		goto err2;

	qede_napi_add_enable(edev);
	DP_INFO(edev, "Napi added and enabled\n");

	rc = qede_setup_irqs(edev);
	if (rc)
		goto err3;
	DP_INFO(edev, "Setup IRQs succeeded\n");

3968
	rc = qede_start_queues(edev, mode != QEDE_LOAD_RELOAD);
3969 3970 3971 3972 3973 3974 3975
	if (rc)
		goto err4;
	DP_INFO(edev, "Start VPORT, RXQ and TXQ succeeded\n");

	/* Add primary mac and set Rx filters */
	ether_addr_copy(edev->primary_mac, edev->ndev->dev_addr);

3976 3977 3978
	/* Program un-configured VLANs */
	qede_configure_vlan_filters(edev);

S
Sudarsana Kalluru 已提交
3979 3980 3981 3982 3983 3984 3985 3986
	/* Ask for link-up using current configuration */
	memset(&link_params, 0, sizeof(link_params));
	link_params.link_up = true;
	edev->ops->common->set_link(edev->cdev, &link_params);

	/* Query whether link is already-up */
	memset(&link_output, 0, sizeof(link_output));
	edev->ops->common->get_link(edev->cdev, &link_output);
R
Ram Amrani 已提交
3987
	qede_roce_dev_event_open(edev);
S
Sudarsana Kalluru 已提交
3988 3989
	qede_link_update(edev, &link_output);

M
Mintz, Yuval 已提交
3990 3991
	edev->state = QEDE_STATE_OPEN;

3992 3993 3994
	DP_INFO(edev, "Ending successfully qede load\n");


M
Mintz, Yuval 已提交
3995
	goto out;
3996 3997 3998 3999 4000 4001 4002 4003 4004 4005
err4:
	qede_sync_free_irqs(edev);
	memset(&edev->int_info.msix_cnt, 0, sizeof(struct qed_int_info));
err3:
	qede_napi_disable_remove(edev);
err2:
	qede_free_mem_load(edev);
err1:
	edev->ops->common->set_fp_int(edev->cdev, 0);
	qede_free_fp_array(edev);
4006 4007 4008
	edev->num_queues = 0;
	edev->fp_num_tx = 0;
	edev->fp_num_rx = 0;
M
Mintz, Yuval 已提交
4009 4010 4011 4012
out:
	if (!is_locked)
		__qede_unlock(edev);

4013 4014 4015
	return rc;
}

M
Mintz, Yuval 已提交
4016 4017 4018
/* 'func' should be able to run between unload and reload assuming interface
 * is actually running, or afterwards in case it's currently DOWN.
 */
4019
void qede_reload(struct qede_dev *edev,
M
Mintz, Yuval 已提交
4020
		 struct qede_reload_args *args, bool is_locked)
4021
{
M
Mintz, Yuval 已提交
4022 4023 4024 4025 4026 4027
	if (!is_locked)
		__qede_lock(edev);

	/* Since qede_lock is held, internal state wouldn't change even
	 * if netdev state would start transitioning. Check whether current
	 * internal configuration indicates device is up, then reload.
4028
	 */
M
Mintz, Yuval 已提交
4029 4030 4031 4032 4033
	if (edev->state == QEDE_STATE_OPEN) {
		qede_unload(edev, QEDE_UNLOAD_NORMAL, true);
		if (args)
			args->func(edev, args);
		qede_load(edev, QEDE_LOAD_RELOAD, true);
4034

M
Mintz, Yuval 已提交
4035 4036 4037 4038 4039
		/* Since no one is going to do it for us, re-configure */
		qede_config_rx_mode(edev->ndev);
	} else if (args) {
		args->func(edev, args);
	}
4040

M
Mintz, Yuval 已提交
4041 4042
	if (!is_locked)
		__qede_unlock(edev);
4043 4044
}

4045 4046 4047 4048
/* called with rtnl_lock */
static int qede_open(struct net_device *ndev)
{
	struct qede_dev *edev = netdev_priv(ndev);
4049
	int rc;
4050 4051 4052 4053 4054

	netif_carrier_off(ndev);

	edev->ops->common->set_power_state(edev->cdev, PCI_D0);

M
Mintz, Yuval 已提交
4055
	rc = qede_load(edev, QEDE_LOAD_NORMAL, false);
4056 4057 4058
	if (rc)
		return rc;

4059 4060
	udp_tunnel_get_rx_info(ndev);

4061 4062
	edev->ops->common->update_drv_state(edev->cdev, true);

4063
	return 0;
4064 4065 4066 4067 4068 4069
}

static int qede_close(struct net_device *ndev)
{
	struct qede_dev *edev = netdev_priv(ndev);

M
Mintz, Yuval 已提交
4070
	qede_unload(edev, QEDE_UNLOAD_NORMAL, false);
4071

4072 4073
	edev->ops->common->update_drv_state(edev->cdev, false);

4074 4075
	return 0;
}
4076

S
Sudarsana Kalluru 已提交
4077 4078 4079 4080 4081 4082 4083 4084 4085 4086
static void qede_link_update(void *dev, struct qed_link_output *link)
{
	struct qede_dev *edev = dev;

	if (!netif_running(edev->ndev)) {
		DP_VERBOSE(edev, NETIF_MSG_LINK, "Interface is not running\n");
		return;
	}

	if (link->link_up) {
4087 4088 4089 4090 4091
		if (!netif_carrier_ok(edev->ndev)) {
			DP_NOTICE(edev, "Link is up\n");
			netif_tx_start_all_queues(edev->ndev);
			netif_carrier_on(edev->ndev);
		}
S
Sudarsana Kalluru 已提交
4092
	} else {
4093 4094 4095 4096 4097
		if (netif_carrier_ok(edev->ndev)) {
			DP_NOTICE(edev, "Link is down\n");
			netif_tx_disable(edev->ndev);
			netif_carrier_off(edev->ndev);
		}
S
Sudarsana Kalluru 已提交
4098 4099 4100
	}
}

4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115
static int qede_set_mac_addr(struct net_device *ndev, void *p)
{
	struct qede_dev *edev = netdev_priv(ndev);
	struct sockaddr *addr = p;
	int rc;

	ASSERT_RTNL(); /* @@@TBD To be removed */

	DP_INFO(edev, "Set_mac_addr called\n");

	if (!is_valid_ether_addr(addr->sa_data)) {
		DP_NOTICE(edev, "The MAC address is not valid\n");
		return -EFAULT;
	}

Y
Yuval Mintz 已提交
4116 4117 4118 4119 4120
	if (!edev->ops->check_mac(edev->cdev, addr->sa_data)) {
		DP_NOTICE(edev, "qed prevents setting MAC\n");
		return -EINVAL;
	}

4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133
	ether_addr_copy(ndev->dev_addr, addr->sa_data);

	if (!netif_running(ndev))  {
		DP_NOTICE(edev, "The device is currently down\n");
		return 0;
	}

	/* Remove the previous primary mac */
	rc = qede_set_ucast_rx_mac(edev, QED_FILTER_XCAST_TYPE_DEL,
				   edev->primary_mac);
	if (rc)
		return rc;

4134 4135
	edev->ops->common->update_mac(edev->cdev, addr->sa_data);

4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201
	/* Add MAC filter according to the new unicast HW MAC address */
	ether_addr_copy(edev->primary_mac, ndev->dev_addr);
	return qede_set_ucast_rx_mac(edev, QED_FILTER_XCAST_TYPE_ADD,
				      edev->primary_mac);
}

static int
qede_configure_mcast_filtering(struct net_device *ndev,
			       enum qed_filter_rx_mode_type *accept_flags)
{
	struct qede_dev *edev = netdev_priv(ndev);
	unsigned char *mc_macs, *temp;
	struct netdev_hw_addr *ha;
	int rc = 0, mc_count;
	size_t size;

	size = 64 * ETH_ALEN;

	mc_macs = kzalloc(size, GFP_KERNEL);
	if (!mc_macs) {
		DP_NOTICE(edev,
			  "Failed to allocate memory for multicast MACs\n");
		rc = -ENOMEM;
		goto exit;
	}

	temp = mc_macs;

	/* Remove all previously configured MAC filters */
	rc = qede_set_mcast_rx_mac(edev, QED_FILTER_XCAST_TYPE_DEL,
				   mc_macs, 1);
	if (rc)
		goto exit;

	netif_addr_lock_bh(ndev);

	mc_count = netdev_mc_count(ndev);
	if (mc_count < 64) {
		netdev_for_each_mc_addr(ha, ndev) {
			ether_addr_copy(temp, ha->addr);
			temp += ETH_ALEN;
		}
	}

	netif_addr_unlock_bh(ndev);

	/* Check for all multicast @@@TBD resource allocation */
	if ((ndev->flags & IFF_ALLMULTI) ||
	    (mc_count > 64)) {
		if (*accept_flags == QED_FILTER_RX_MODE_TYPE_REGULAR)
			*accept_flags = QED_FILTER_RX_MODE_TYPE_MULTI_PROMISC;
	} else {
		/* Add all multicast MAC filters */
		rc = qede_set_mcast_rx_mac(edev, QED_FILTER_XCAST_TYPE_ADD,
					   mc_macs, mc_count);
	}

exit:
	kfree(mc_macs);
	return rc;
}

static void qede_set_rx_mode(struct net_device *ndev)
{
	struct qede_dev *edev = netdev_priv(ndev);

M
Mintz, Yuval 已提交
4202 4203
	set_bit(QEDE_SP_RX_MODE, &edev->sp_flags);
	schedule_delayed_work(&edev->sp_task, 0);
4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250
}

/* Must be called with qede_lock held */
static void qede_config_rx_mode(struct net_device *ndev)
{
	enum qed_filter_rx_mode_type accept_flags = QED_FILTER_TYPE_UCAST;
	struct qede_dev *edev = netdev_priv(ndev);
	struct qed_filter_params rx_mode;
	unsigned char *uc_macs, *temp;
	struct netdev_hw_addr *ha;
	int rc, uc_count;
	size_t size;

	netif_addr_lock_bh(ndev);

	uc_count = netdev_uc_count(ndev);
	size = uc_count * ETH_ALEN;

	uc_macs = kzalloc(size, GFP_ATOMIC);
	if (!uc_macs) {
		DP_NOTICE(edev, "Failed to allocate memory for unicast MACs\n");
		netif_addr_unlock_bh(ndev);
		return;
	}

	temp = uc_macs;
	netdev_for_each_uc_addr(ha, ndev) {
		ether_addr_copy(temp, ha->addr);
		temp += ETH_ALEN;
	}

	netif_addr_unlock_bh(ndev);

	/* Configure the struct for the Rx mode */
	memset(&rx_mode, 0, sizeof(struct qed_filter_params));
	rx_mode.type = QED_FILTER_TYPE_RX_MODE;

	/* Remove all previous unicast secondary macs and multicast macs
	 * (configrue / leave the primary mac)
	 */
	rc = qede_set_ucast_rx_mac(edev, QED_FILTER_XCAST_TYPE_REPLACE,
				   edev->primary_mac);
	if (rc)
		goto out;

	/* Check for promiscuous */
	if ((ndev->flags & IFF_PROMISC) ||
Y
Yuval Mintz 已提交
4251
	    (uc_count > edev->dev_info.num_mac_filters - 1)) {
4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
		accept_flags = QED_FILTER_RX_MODE_TYPE_PROMISC;
	} else {
		/* Add MAC filters according to the unicast secondary macs */
		int i;

		temp = uc_macs;
		for (i = 0; i < uc_count; i++) {
			rc = qede_set_ucast_rx_mac(edev,
						   QED_FILTER_XCAST_TYPE_ADD,
						   temp);
			if (rc)
				goto out;

			temp += ETH_ALEN;
		}

		rc = qede_configure_mcast_filtering(ndev, &accept_flags);
		if (rc)
			goto out;
	}

4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283
	/* take care of VLAN mode */
	if (ndev->flags & IFF_PROMISC) {
		qede_config_accept_any_vlan(edev, true);
	} else if (!edev->non_configured_vlans) {
		/* It's possible that accept_any_vlan mode is set due to a
		 * previous setting of IFF_PROMISC. If vlan credits are
		 * sufficient, disable accept_any_vlan.
		 */
		qede_config_accept_any_vlan(edev, false);
	}

4284 4285 4286 4287 4288
	rx_mode.filter.accept_flags = accept_flags;
	edev->ops->filter_config(edev->cdev, &rx_mode);
out:
	kfree(uc_macs);
}