qede_main.c 72.9 KB
Newer Older
Y
Yuval Mintz 已提交
1
/* QLogic qede NIC Driver
M
Mintz, Yuval 已提交
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 27 28 29 30 31
 * Copyright (c) 2015-2017  QLogic Corporation
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed 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, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and /or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
32
#include <linux/crash_dump.h>
Y
Yuval Mintz 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#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>
51
#include <net/udp_tunnel.h>
Y
Yuval Mintz 已提交
52 53 54 55 56 57 58 59 60 61 62
#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>
63
#include <linux/vmalloc.h>
64
#include <linux/aer.h>
Y
Yuval Mintz 已提交
65
#include "qede.h"
66
#include "qede_ptp.h"
Y
Yuval Mintz 已提交
67

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

71
MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx Ethernet Driver");
Y
Yuval Mintz 已提交
72 73 74 75 76 77 78 79 80 81
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
82
#define CHIP_NUM_57980S_10		0x1666
Y
Yuval Mintz 已提交
83 84 85 86
#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 已提交
87
#define CHIP_NUM_57980S_IOV		0x1664
88 89
#define CHIP_NUM_AH			0x8070
#define CHIP_NUM_AH_IOV			0x8090
Y
Yuval Mintz 已提交
90 91 92 93 94 95 96 97

#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 已提交
98
#define PCI_DEVICE_ID_57980S_IOV	CHIP_NUM_57980S_IOV
99 100 101
#define PCI_DEVICE_ID_AH		CHIP_NUM_AH
#define PCI_DEVICE_ID_AH_IOV		CHIP_NUM_AH_IOV

Y
Yuval Mintz 已提交
102 103
#endif

Y
Yuval Mintz 已提交
104 105 106 107 108
enum qede_pci_private {
	QEDE_PRIVATE_PF,
	QEDE_PRIVATE_VF
};

Y
Yuval Mintz 已提交
109
static const struct pci_device_id qede_pci_tbl[] = {
Y
Yuval Mintz 已提交
110 111 112 113 114 115
	{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},
116
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
117
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_IOV), QEDE_PRIVATE_VF},
118 119 120 121
#endif
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_AH), QEDE_PRIVATE_PF},
#ifdef CONFIG_QED_SRIOV
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_AH_IOV), QEDE_PRIVATE_VF},
122
#endif
Y
Yuval Mintz 已提交
123 124 125 126 127 128
	{ 0 }
};

MODULE_DEVICE_TABLE(pci, qede_pci_tbl);

static int qede_probe(struct pci_dev *pdev, const struct pci_device_id *id);
129 130
static pci_ers_result_t
qede_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state);
Y
Yuval Mintz 已提交
131 132 133

#define TX_TIMEOUT		(5 * HZ)

M
Mintz, Yuval 已提交
134 135 136
/* Utilize last protocol index for XDP */
#define XDP_PI	11

Y
Yuval Mintz 已提交
137
static void qede_remove(struct pci_dev *pdev);
M
Mintz, Yuval 已提交
138
static void qede_shutdown(struct pci_dev *pdev);
S
Sudarsana Kalluru 已提交
139
static void qede_link_update(void *dev, struct qed_link_output *link);
T
Tomer Tayar 已提交
140 141
static void qede_schedule_recovery_handler(void *dev);
static void qede_recovery_handler(struct qede_dev *edev);
142 143
static void qede_schedule_hw_err_handler(void *dev,
					 enum qed_hw_err_type err_type);
144 145 146
static void qede_get_eth_tlv_data(void *edev, void *data);
static void qede_get_generic_tlv_data(void *edev,
				      struct qed_generic_tlvs *data);
147
static void qede_generic_hw_err_handler(struct qede_dev *edev);
Y
Yuval Mintz 已提交
148
#ifdef CONFIG_QED_SRIOV
149 150
static int qede_set_vf_vlan(struct net_device *ndev, int vf, u16 vlan, u8 qos,
			    __be16 vlan_proto)
Y
Yuval Mintz 已提交
151 152 153 154 155 156 157 158
{
	struct qede_dev *edev = netdev_priv(ndev);

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

159 160 161
	if (vlan_proto != htons(ETH_P_8021Q))
		return -EPROTONOSUPPORT;

Y
Yuval Mintz 已提交
162 163 164 165 166 167
	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 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
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 已提交
184 185 186
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 已提交
187
	struct qed_dev_info *qed_info = &edev->dev_info.common;
188
	struct qed_update_vport_params *vport_params;
Y
Yuval Mintz 已提交
189
	int rc;
Y
Yuval Mintz 已提交
190

191 192 193
	vport_params = vzalloc(sizeof(*vport_params));
	if (!vport_params)
		return -ENOMEM;
Y
Yuval Mintz 已提交
194 195
	DP_VERBOSE(edev, QED_MSG_IOV, "Requested %d VFs\n", num_vfs_param);

Y
Yuval Mintz 已提交
196 197 198 199
	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) &&
200
	    !qed_info->b_inter_pf_switch && qed_info->tx_switching) {
201 202 203 204
		vport_params->vport_id = 0;
		vport_params->update_tx_switching_flg = 1;
		vport_params->tx_switching_flg = num_vfs_param ? 1 : 0;
		edev->ops->vport_update(edev->cdev, vport_params);
Y
Yuval Mintz 已提交
205 206
	}

207
	vfree(vport_params);
Y
Yuval Mintz 已提交
208
	return rc;
Y
Yuval Mintz 已提交
209 210 211
}
#endif

212 213 214 215
static const struct pci_error_handlers qede_err_handler = {
	.error_detected = qede_io_error_detected,
};

Y
Yuval Mintz 已提交
216 217 218 219 220
static struct pci_driver qede_pci_driver = {
	.name = "qede",
	.id_table = qede_pci_tbl,
	.probe = qede_probe,
	.remove = qede_remove,
M
Mintz, Yuval 已提交
221
	.shutdown = qede_shutdown,
Y
Yuval Mintz 已提交
222 223 224
#ifdef CONFIG_QED_SRIOV
	.sriov_configure = qede_sriov_configure,
#endif
225
	.err_handler = &qede_err_handler,
Y
Yuval Mintz 已提交
226 227
};

S
Sudarsana Kalluru 已提交
228 229
static struct qed_eth_cb_ops qede_ll_ops = {
	{
C
Chopra, Manish 已提交
230 231 232
#ifdef CONFIG_RFS_ACCEL
		.arfs_filter_op = qede_arfs_filter_op,
#endif
S
Sudarsana Kalluru 已提交
233
		.link_update = qede_link_update,
T
Tomer Tayar 已提交
234
		.schedule_recovery_handler = qede_schedule_recovery_handler,
235
		.schedule_hw_err_handler = qede_schedule_hw_err_handler,
236 237
		.get_generic_tlv_data = qede_get_generic_tlv_data,
		.get_protocol_tlv_data = qede_get_eth_tlv_data,
S
Sudarsana Kalluru 已提交
238
	},
Y
Yuval Mintz 已提交
239
	.force_mac = qede_force_mac,
240
	.ports_update = qede_udp_ports_update,
S
Sudarsana Kalluru 已提交
241 242
};

243 244 245 246 247 248 249
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 已提交
250
	if (event != NETDEV_CHANGENAME && event != NETDEV_CHANGEADDR)
251 252 253 254 255 256 257 258 259 260 261 262
		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 已提交
263 264 265 266 267
	switch (event) {
	case NETDEV_CHANGENAME:
		/* Notify qed of the name change */
		if (!edev->ops || !edev->ops->common)
			goto done;
268
		edev->ops->common->set_name(edev->cdev, edev->ndev->name);
R
Ram Amrani 已提交
269 270 271
		break;
	case NETDEV_CHANGEADDR:
		edev = netdev_priv(ndev);
272
		qede_rdma_event_changeaddr(edev);
R
Ram Amrani 已提交
273 274
		break;
	}
275 276 277 278 279 280 281 282 283

done:
	return NOTIFY_DONE;
}

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

Y
Yuval Mintz 已提交
284 285 286 287 288
static
int __init qede_init(void)
{
	int ret;

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

R
Rahul Verma 已提交
291
	qed_ops = qed_get_eth_ops();
Y
Yuval Mintz 已提交
292 293 294 295 296
	if (!qed_ops) {
		pr_notice("Failed to get qed ethtool operations\n");
		return -EINVAL;
	}

297
	/* Must register notifier before pci ops, since we might miss
298
	 * interface rename after pci probe and netdev registration.
299 300 301 302 303 304 305 306
	 */
	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 已提交
307 308 309
	ret = pci_register_driver(&qede_pci_driver);
	if (ret) {
		pr_notice("Failed to register driver\n");
310
		unregister_netdevice_notifier(&qede_netdev_notifier);
Y
Yuval Mintz 已提交
311 312 313 314 315 316 317 318 319
		qed_put_eth_ops();
		return -EINVAL;
	}

	return 0;
}

static void __exit qede_cleanup(void)
{
Y
Yuval Mintz 已提交
320 321
	if (debug & QED_LOG_INFO_MASK)
		pr_info("qede_cleanup called\n");
Y
Yuval Mintz 已提交
322

323
	unregister_netdevice_notifier(&qede_netdev_notifier);
Y
Yuval Mintz 已提交
324 325 326 327 328 329 330
	pci_unregister_driver(&qede_pci_driver);
	qed_put_eth_ops();
}

module_init(qede_init);
module_exit(qede_cleanup);

331 332
static int qede_open(struct net_device *ndev);
static int qede_close(struct net_device *ndev);
333

334 335
void qede_fill_by_demand_stats(struct qede_dev *edev)
{
336
	struct qede_stats_common *p_common = &edev->stats.common;
337 338 339
	struct qed_eth_stats stats;

	edev->ops->get_vport_stats(edev->cdev, &stats);
340 341 342 343 344 345 346 347 348 349 350 351

	p_common->no_buff_discards = stats.common.no_buff_discards;
	p_common->packet_too_big_discard = stats.common.packet_too_big_discard;
	p_common->ttl0_discard = stats.common.ttl0_discard;
	p_common->rx_ucast_bytes = stats.common.rx_ucast_bytes;
	p_common->rx_mcast_bytes = stats.common.rx_mcast_bytes;
	p_common->rx_bcast_bytes = stats.common.rx_bcast_bytes;
	p_common->rx_ucast_pkts = stats.common.rx_ucast_pkts;
	p_common->rx_mcast_pkts = stats.common.rx_mcast_pkts;
	p_common->rx_bcast_pkts = stats.common.rx_bcast_pkts;
	p_common->mftag_filter_discards = stats.common.mftag_filter_discards;
	p_common->mac_filter_discards = stats.common.mac_filter_discards;
352
	p_common->gft_filter_drop = stats.common.gft_filter_drop;
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

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

	p_common->rx_64_byte_packets = stats.common.rx_64_byte_packets;
	p_common->rx_65_to_127_byte_packets =
	    stats.common.rx_65_to_127_byte_packets;
	p_common->rx_128_to_255_byte_packets =
	    stats.common.rx_128_to_255_byte_packets;
	p_common->rx_256_to_511_byte_packets =
	    stats.common.rx_256_to_511_byte_packets;
	p_common->rx_512_to_1023_byte_packets =
	    stats.common.rx_512_to_1023_byte_packets;
	p_common->rx_1024_to_1518_byte_packets =
	    stats.common.rx_1024_to_1518_byte_packets;
	p_common->rx_crc_errors = stats.common.rx_crc_errors;
	p_common->rx_mac_crtl_frames = stats.common.rx_mac_crtl_frames;
	p_common->rx_pause_frames = stats.common.rx_pause_frames;
	p_common->rx_pfc_frames = stats.common.rx_pfc_frames;
	p_common->rx_align_errors = stats.common.rx_align_errors;
	p_common->rx_carrier_errors = stats.common.rx_carrier_errors;
	p_common->rx_oversize_packets = stats.common.rx_oversize_packets;
	p_common->rx_jabbers = stats.common.rx_jabbers;
	p_common->rx_undersize_packets = stats.common.rx_undersize_packets;
	p_common->rx_fragments = stats.common.rx_fragments;
	p_common->tx_64_byte_packets = stats.common.tx_64_byte_packets;
	p_common->tx_65_to_127_byte_packets =
	    stats.common.tx_65_to_127_byte_packets;
	p_common->tx_128_to_255_byte_packets =
	    stats.common.tx_128_to_255_byte_packets;
	p_common->tx_256_to_511_byte_packets =
	    stats.common.tx_256_to_511_byte_packets;
	p_common->tx_512_to_1023_byte_packets =
	    stats.common.tx_512_to_1023_byte_packets;
	p_common->tx_1024_to_1518_byte_packets =
	    stats.common.tx_1024_to_1518_byte_packets;
	p_common->tx_pause_frames = stats.common.tx_pause_frames;
	p_common->tx_pfc_frames = stats.common.tx_pfc_frames;
	p_common->brb_truncates = stats.common.brb_truncates;
	p_common->brb_discards = stats.common.brb_discards;
	p_common->tx_mac_ctrl_frames = stats.common.tx_mac_ctrl_frames;
404
	p_common->link_change_count = stats.common.link_change_count;
405
	p_common->ptp_skip_txts = edev->ptp_skip_txts;
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

	if (QEDE_IS_BB(edev)) {
		struct qede_stats_bb *p_bb = &edev->stats.bb;

		p_bb->rx_1519_to_1522_byte_packets =
		    stats.bb.rx_1519_to_1522_byte_packets;
		p_bb->rx_1519_to_2047_byte_packets =
		    stats.bb.rx_1519_to_2047_byte_packets;
		p_bb->rx_2048_to_4095_byte_packets =
		    stats.bb.rx_2048_to_4095_byte_packets;
		p_bb->rx_4096_to_9216_byte_packets =
		    stats.bb.rx_4096_to_9216_byte_packets;
		p_bb->rx_9217_to_16383_byte_packets =
		    stats.bb.rx_9217_to_16383_byte_packets;
		p_bb->tx_1519_to_2047_byte_packets =
		    stats.bb.tx_1519_to_2047_byte_packets;
		p_bb->tx_2048_to_4095_byte_packets =
		    stats.bb.tx_2048_to_4095_byte_packets;
		p_bb->tx_4096_to_9216_byte_packets =
		    stats.bb.tx_4096_to_9216_byte_packets;
		p_bb->tx_9217_to_16383_byte_packets =
		    stats.bb.tx_9217_to_16383_byte_packets;
		p_bb->tx_lpi_entry_count = stats.bb.tx_lpi_entry_count;
		p_bb->tx_total_collisions = stats.bb.tx_total_collisions;
	} else {
		struct qede_stats_ah *p_ah = &edev->stats.ah;

		p_ah->rx_1519_to_max_byte_packets =
		    stats.ah.rx_1519_to_max_byte_packets;
		p_ah->tx_1519_to_max_byte_packets =
		    stats.ah.tx_1519_to_max_byte_packets;
	}
438 439
}

440 441
static void qede_get_stats64(struct net_device *dev,
			     struct rtnl_link_stats64 *stats)
442 443
{
	struct qede_dev *edev = netdev_priv(dev);
444
	struct qede_stats_common *p_common;
445 446

	qede_fill_by_demand_stats(edev);
447
	p_common = &edev->stats.common;
448

449 450 451 452
	stats->rx_packets = p_common->rx_ucast_pkts + p_common->rx_mcast_pkts +
			    p_common->rx_bcast_pkts;
	stats->tx_packets = p_common->tx_ucast_pkts + p_common->tx_mcast_pkts +
			    p_common->tx_bcast_pkts;
453

454 455 456 457
	stats->rx_bytes = p_common->rx_ucast_bytes + p_common->rx_mcast_bytes +
			  p_common->rx_bcast_bytes;
	stats->tx_bytes = p_common->tx_ucast_bytes + p_common->tx_mcast_bytes +
			  p_common->tx_bcast_bytes;
458

459 460
	stats->tx_errors = p_common->tx_err_drop_pkts;
	stats->multicast = p_common->rx_mcast_pkts + p_common->rx_bcast_pkts;
461

462
	stats->rx_fifo_errors = p_common->no_buff_discards;
463

464 465 466 467
	if (QEDE_IS_BB(edev))
		stats->collisions = edev->stats.bb.tx_total_collisions;
	stats->rx_crc_errors = p_common->rx_crc_errors;
	stats->rx_frame_errors = p_common->rx_align_errors;
468 469
}

Y
Yuval Mintz 已提交
470
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
471 472 473 474 475 476 477 478 479 480 481
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 已提交
482 483 484 485 486
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 已提交
487
	return edev->ops->iov->set_rate(edev->cdev, vfidx, min_tx_rate,
Y
Yuval Mintz 已提交
488 489 490
					max_tx_rate);
}

Y
Yuval Mintz 已提交
491 492 493 494 495 496 497 498 499 500
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 已提交
501 502 503 504 505 506 507 508 509 510
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);
}
511 512 513 514 515 516 517 518 519 520

static int qede_set_vf_trust(struct net_device *dev, int vfidx, bool setting)
{
	struct qede_dev *edev = netdev_priv(dev);

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

	return edev->ops->iov->set_trust(edev->cdev, vfidx, setting);
}
Y
Yuval Mintz 已提交
521 522
#endif

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
static int qede_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
	struct qede_dev *edev = netdev_priv(dev);

	if (!netif_running(dev))
		return -EAGAIN;

	switch (cmd) {
	case SIOCSHWTSTAMP:
		return qede_ptp_hw_ts(edev, ifr);
	default:
		DP_VERBOSE(edev, QED_MSG_DEBUG,
			   "default IOCTL cmd 0x%x\n", cmd);
		return -EOPNOTSUPP;
	}

	return 0;
}

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 577 578 579 580 581 582 583 584 585 586
static void qede_tx_log_print(struct qede_dev *edev, struct qede_tx_queue *txq)
{
	DP_NOTICE(edev,
		  "Txq[%d]: FW cons [host] %04x, SW cons %04x, SW prod %04x [Jiffies %lu]\n",
		  txq->index, le16_to_cpu(*txq->hw_cons_ptr),
		  qed_chain_get_cons_idx(&txq->tx_pbl),
		  qed_chain_get_prod_idx(&txq->tx_pbl),
		  jiffies);
}

static void qede_tx_timeout(struct net_device *dev, unsigned int txqueue)
{
	struct qede_dev *edev = netdev_priv(dev);
	struct qede_tx_queue *txq;
	int cos;

	netif_carrier_off(dev);
	DP_NOTICE(edev, "TX timeout on queue %u!\n", txqueue);

	if (!(edev->fp_array[txqueue].type & QEDE_FASTPATH_TX))
		return;

	for_each_cos_in_txq(edev, cos) {
		txq = &edev->fp_array[txqueue].txq[cos];

		if (qed_chain_get_cons_idx(&txq->tx_pbl) !=
		    qed_chain_get_prod_idx(&txq->tx_pbl))
			qede_tx_log_print(edev, txq);
	}

	if (IS_VF(edev))
		return;

	if (test_and_set_bit(QEDE_ERR_IS_HANDLED, &edev->err_flags) ||
	    edev->state == QEDE_STATE_RECOVERY) {
		DP_INFO(edev,
			"Avoid handling a Tx timeout while another HW error is being handled\n");
		return;
	}

	set_bit(QEDE_ERR_GET_DBG_INFO, &edev->err_flags);
	set_bit(QEDE_SP_HW_ERR, &edev->sp_flags);
	schedule_delayed_work(&edev->sp_task, 0);
}

587
static int qede_setup_tc(struct net_device *ndev, u8 num_tc)
M
Manish Chopra 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
{
	struct qede_dev *edev = netdev_priv(ndev);
	int cos, count, offset;

	if (num_tc > edev->dev_info.num_tc)
		return -EINVAL;

	netdev_reset_tc(ndev);
	netdev_set_num_tc(ndev, num_tc);

	for_each_cos_in_txq(edev, cos) {
		count = QEDE_TSS_COUNT(edev);
		offset = cos * QEDE_TSS_COUNT(edev);
		netdev_set_tc_queue(ndev, cos, count, offset);
	}

	return 0;
}

607
static int
608
qede_set_flower(struct qede_dev *edev, struct flow_cls_offload *f,
609 610 611
		__be16 proto)
{
	switch (f->command) {
612
	case FLOW_CLS_REPLACE:
613
		return qede_add_tc_flower_fltr(edev, proto, f);
614
	case FLOW_CLS_DESTROY:
615 616 617 618 619 620 621 622 623
		return qede_delete_flow_filter(edev, f->cookie);
	default:
		return -EOPNOTSUPP;
	}
}

static int qede_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
				  void *cb_priv)
{
624
	struct flow_cls_offload *f;
625 626 627 628 629 630 631 632 633 634 635 636 637 638
	struct qede_dev *edev = cb_priv;

	if (!tc_cls_can_offload_and_chain0(edev->ndev, type_data))
		return -EOPNOTSUPP;

	switch (type) {
	case TC_SETUP_CLSFLOWER:
		f = type_data;
		return qede_set_flower(edev, f, f->common.protocol);
	default:
		return -EOPNOTSUPP;
	}
}

639 640
static LIST_HEAD(qede_block_cb_list);

M
Manish Chopra 已提交
641 642 643 644
static int
qede_setup_tc_offload(struct net_device *dev, enum tc_setup_type type,
		      void *type_data)
{
645
	struct qede_dev *edev = netdev_priv(dev);
M
Manish Chopra 已提交
646 647 648
	struct tc_mqprio_qopt *mqprio;

	switch (type) {
649
	case TC_SETUP_BLOCK:
650 651
		return flow_block_cb_setup_simple(type_data,
						  &qede_block_cb_list,
652 653
						  qede_setup_tc_block_cb,
						  edev, edev, true);
M
Manish Chopra 已提交
654 655 656 657 658 659 660 661 662 663
	case TC_SETUP_QDISC_MQPRIO:
		mqprio = type_data;

		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
		return qede_setup_tc(dev, mqprio->num_tc);
	default:
		return -EOPNOTSUPP;
	}
}

664 665 666 667
static const struct net_device_ops qede_netdev_ops = {
	.ndo_open = qede_open,
	.ndo_stop = qede_close,
	.ndo_start_xmit = qede_start_xmit,
668
	.ndo_select_queue = qede_select_queue,
669 670
	.ndo_set_rx_mode = qede_set_rx_mode,
	.ndo_set_mac_address = qede_set_mac_addr,
671
	.ndo_validate_addr = eth_validate_addr,
672
	.ndo_change_mtu = qede_change_mtu,
673
	.ndo_do_ioctl = qede_ioctl,
674
	.ndo_tx_timeout = qede_tx_timeout,
Y
Yuval Mintz 已提交
675
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
676
	.ndo_set_vf_mac = qede_set_vf_mac,
Y
Yuval Mintz 已提交
677
	.ndo_set_vf_vlan = qede_set_vf_vlan,
678
	.ndo_set_vf_trust = qede_set_vf_trust,
Y
Yuval Mintz 已提交
679
#endif
680 681
	.ndo_vlan_rx_add_vid = qede_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid = qede_vlan_rx_kill_vid,
M
Michael Chan 已提交
682
	.ndo_fix_features = qede_fix_features,
Y
Yuval Mintz 已提交
683
	.ndo_set_features = qede_set_features,
684
	.ndo_get_stats64 = qede_get_stats64,
Y
Yuval Mintz 已提交
685 686
#ifdef CONFIG_QED_SRIOV
	.ndo_set_vf_link_state = qede_set_vf_link_state,
Y
Yuval Mintz 已提交
687
	.ndo_set_vf_spoofchk = qede_set_vf_spoofchk,
Y
Yuval Mintz 已提交
688
	.ndo_get_vf_config = qede_get_vf_config,
Y
Yuval Mintz 已提交
689 690
	.ndo_set_vf_rate = qede_set_vf_rate,
#endif
691 692
	.ndo_udp_tunnel_add = qede_udp_tunnel_add,
	.ndo_udp_tunnel_del = qede_udp_tunnel_del,
693
	.ndo_features_check = qede_features_check,
694
	.ndo_bpf = qede_xdp,
C
Chopra, Manish 已提交
695 696 697
#ifdef CONFIG_RFS_ACCEL
	.ndo_rx_flow_steer = qede_rx_flow_steer,
#endif
M
Manish Chopra 已提交
698
	.ndo_setup_tc = qede_setup_tc_offload,
699 700
};

M
Mintz, Yuval 已提交
701 702 703 704
static const struct net_device_ops qede_netdev_vf_ops = {
	.ndo_open = qede_open,
	.ndo_stop = qede_close,
	.ndo_start_xmit = qede_start_xmit,
705
	.ndo_select_queue = qede_select_queue,
M
Mintz, Yuval 已提交
706 707 708 709 710 711
	.ndo_set_rx_mode = qede_set_rx_mode,
	.ndo_set_mac_address = qede_set_mac_addr,
	.ndo_validate_addr = eth_validate_addr,
	.ndo_change_mtu = qede_change_mtu,
	.ndo_vlan_rx_add_vid = qede_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid = qede_vlan_rx_kill_vid,
M
Michael Chan 已提交
712
	.ndo_fix_features = qede_fix_features,
M
Mintz, Yuval 已提交
713 714 715 716 717 718 719
	.ndo_set_features = qede_set_features,
	.ndo_get_stats64 = qede_get_stats64,
	.ndo_udp_tunnel_add = qede_udp_tunnel_add,
	.ndo_udp_tunnel_del = qede_udp_tunnel_del,
	.ndo_features_check = qede_features_check,
};

M
Mintz, Yuval 已提交
720 721 722 723
static const struct net_device_ops qede_netdev_vf_xdp_ops = {
	.ndo_open = qede_open,
	.ndo_stop = qede_close,
	.ndo_start_xmit = qede_start_xmit,
724
	.ndo_select_queue = qede_select_queue,
M
Mintz, Yuval 已提交
725 726 727 728 729 730
	.ndo_set_rx_mode = qede_set_rx_mode,
	.ndo_set_mac_address = qede_set_mac_addr,
	.ndo_validate_addr = eth_validate_addr,
	.ndo_change_mtu = qede_change_mtu,
	.ndo_vlan_rx_add_vid = qede_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid = qede_vlan_rx_kill_vid,
M
Michael Chan 已提交
731
	.ndo_fix_features = qede_fix_features,
M
Mintz, Yuval 已提交
732 733 734 735 736
	.ndo_set_features = qede_set_features,
	.ndo_get_stats64 = qede_get_stats64,
	.ndo_udp_tunnel_add = qede_udp_tunnel_add,
	.ndo_udp_tunnel_del = qede_udp_tunnel_del,
	.ndo_features_check = qede_features_check,
737
	.ndo_bpf = qede_xdp,
M
Mintz, Yuval 已提交
738 739
};

Y
Yuval Mintz 已提交
740 741 742 743 744 745 746 747
/* -------------------------------------------------------------------------
 * 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 已提交
748
					    u32 dp_module, u8 dp_level)
Y
Yuval Mintz 已提交
749 750 751 752 753
{
	struct net_device *ndev;
	struct qede_dev *edev;

	ndev = alloc_etherdev_mqs(sizeof(*edev),
M
Manish Chopra 已提交
754 755
				  info->num_queues * info->num_tc,
				  info->num_queues);
Y
Yuval Mintz 已提交
756 757 758 759 760 761 762 763 764 765 766 767
	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;
768 769 770 771 772 773 774 775

	if (is_kdump_kernel()) {
		edev->q_num_rx_buffers = NUM_RX_BDS_KDUMP_MIN;
		edev->q_num_tx_buffers = NUM_TX_BDS_KDUMP_MIN;
	} else {
		edev->q_num_rx_buffers = NUM_RX_BDS_DEF;
		edev->q_num_tx_buffers = NUM_TX_BDS_DEF;
	}
Y
Yuval Mintz 已提交
776

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

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

782
	memset(&edev->stats, 0, sizeof(edev->stats));
Y
Yuval Mintz 已提交
783 784
	memcpy(&edev->dev_info, info, sizeof(*info));

785 786 787 788 789 790
	/* As ethtool doesn't have the ability to show WoL behavior as
	 * 'default', if device supports it declare it's enabled.
	 */
	if (edev->dev_info.common.wol_support)
		edev->wol_enabled = true;

791 792
	INIT_LIST_HEAD(&edev->vlan_list);

Y
Yuval Mintz 已提交
793 794 795 796 797 798 799
	return edev;
}

static void qede_init_ndev(struct qede_dev *edev)
{
	struct net_device *ndev = edev->ndev;
	struct pci_dev *pdev = edev->pdev;
800
	bool udp_tunnel_enable = false;
C
Chopra, Manish 已提交
801
	netdev_features_t hw_features;
Y
Yuval Mintz 已提交
802 803 804 805 806 807 808 809 810 811

	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;

M
Mintz, Yuval 已提交
812 813 814 815 816 817
	if (IS_VF(edev)) {
		if (edev->dev_info.xdp_supported)
			ndev->netdev_ops = &qede_netdev_vf_xdp_ops;
		else
			ndev->netdev_ops = &qede_netdev_vf_ops;
	} else {
M
Mintz, Yuval 已提交
818
		ndev->netdev_ops = &qede_netdev_ops;
M
Mintz, Yuval 已提交
819
	}
820

821 822
	qede_set_ethtool_ops(ndev);

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

Y
Yuval Mintz 已提交
825
	/* user-changeble features */
M
Michael Chan 已提交
826
	hw_features = NETIF_F_GRO | NETIF_F_GRO_HW | NETIF_F_SG |
Y
Yuval Mintz 已提交
827
		      NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
828
		      NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_TC;
Y
Yuval Mintz 已提交
829

C
Chopra, Manish 已提交
830 831 832
	if (!IS_VF(edev) && edev->dev_info.common.num_hwfns == 1)
		hw_features |= NETIF_F_NTUPLE;

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
	if (edev->dev_info.common.vxlan_enable ||
	    edev->dev_info.common.geneve_enable)
		udp_tunnel_enable = true;

	if (udp_tunnel_enable || edev->dev_info.common.gre_enable) {
		hw_features |= NETIF_F_TSO_ECN;
		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_RXCSUM;
	}

	if (udp_tunnel_enable) {
		hw_features |= (NETIF_F_GSO_UDP_TUNNEL |
				NETIF_F_GSO_UDP_TUNNEL_CSUM);
		ndev->hw_enc_features |= (NETIF_F_GSO_UDP_TUNNEL |
					  NETIF_F_GSO_UDP_TUNNEL_CSUM);
	}

	if (edev->dev_info.common.gre_enable) {
		hw_features |= (NETIF_F_GSO_GRE | NETIF_F_GSO_GRE_CSUM);
		ndev->hw_enc_features |= (NETIF_F_GSO_GRE |
					  NETIF_F_GSO_GRE_CSUM);
	}
857

Y
Yuval Mintz 已提交
858 859 860 861
	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 |
862
			 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_TX;
Y
Yuval Mintz 已提交
863 864 865

	ndev->hw_features = hw_features;

866 867 868 869
	/* MTU range: 46 - 9600 */
	ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
	ndev->max_mtu = QEDE_MAX_JUMBO_PACKET_SIZE;

Y
Yuval Mintz 已提交
870 871
	/* Set network device HW mac */
	ether_addr_copy(edev->ndev->dev_addr, edev->dev_info.common.hw_mac);
872 873

	ndev->mtu = edev->dev_info.common.mtu;
Y
Yuval Mintz 已提交
874 875 876 877 878 879 880 881 882 883 884 885 886
}

/* 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.
 */
887
void qede_config_debug(uint debug, u32 *p_dp_module, u8 *p_dp_level)
Y
Yuval Mintz 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901
{
	*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;
	}
}

902 903 904 905 906 907
static void qede_free_fp_array(struct qede_dev *edev)
{
	if (edev->fp_array) {
		struct qede_fastpath *fp;
		int i;

908
		for_each_queue(i) {
909 910 911
			fp = &edev->fp_array[i];

			kfree(fp->sb_info);
912 913 914 915 916 917
			/* Handle mem alloc failure case where qede_init_fp
			 * didn't register xdp_rxq_info yet.
			 * Implicit only (fp->type & QEDE_FASTPATH_RX)
			 */
			if (fp->rxq && xdp_rxq_info_is_reg(&fp->rxq->xdp_rxq))
				xdp_rxq_info_unreg(&fp->rxq->xdp_rxq);
918
			kfree(fp->rxq);
M
Mintz, Yuval 已提交
919
			kfree(fp->xdp_tx);
M
Mintz, Yuval 已提交
920
			kfree(fp->txq);
921 922 923
		}
		kfree(edev->fp_array);
	}
924 925 926 927

	edev->num_queues = 0;
	edev->fp_num_tx = 0;
	edev->fp_num_rx = 0;
928 929 930 931
}

static int qede_alloc_fp_array(struct qede_dev *edev)
{
932
	u8 fp_combined, fp_rx = edev->fp_num_rx;
933 934 935
	struct qede_fastpath *fp;
	int i;

936
	edev->fp_array = kcalloc(QEDE_QUEUE_CNT(edev),
937 938 939 940 941 942
				 sizeof(*edev->fp_array), GFP_KERNEL);
	if (!edev->fp_array) {
		DP_NOTICE(edev, "fp array allocation failed\n");
		goto err;
	}

943 944 945 946 947 948 949 950
	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) {
951 952
		fp = &edev->fp_array[i];

M
Mintz, Yuval 已提交
953
		fp->sb_info = kzalloc(sizeof(*fp->sb_info), GFP_KERNEL);
954 955 956 957 958
		if (!fp->sb_info) {
			DP_NOTICE(edev, "sb info struct allocation failed\n");
			goto err;
		}

959 960 961 962 963 964 965 966
		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;
967 968
		}

969
		if (fp->type & QEDE_FASTPATH_TX) {
M
Manish Chopra 已提交
970 971
			fp->txq = kcalloc(edev->dev_info.num_tc,
					  sizeof(*fp->txq), GFP_KERNEL);
M
Mintz, Yuval 已提交
972
			if (!fp->txq)
973 974 975 976
				goto err;
		}

		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
977 978
			fp->rxq = kzalloc(sizeof(*fp->rxq), GFP_KERNEL);
			if (!fp->rxq)
979
				goto err;
M
Mintz, Yuval 已提交
980

M
Mintz, Yuval 已提交
981 982 983 984 985
			if (edev->xdp_prog) {
				fp->xdp_tx = kzalloc(sizeof(*fp->xdp_tx),
						     GFP_KERNEL);
				if (!fp->xdp_tx)
					goto err;
M
Mintz, Yuval 已提交
986
				fp->type |= QEDE_FASTPATH_XDP;
M
Mintz, Yuval 已提交
987
			}
988 989 990 991 992 993 994 995 996
		}
	}

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

T
Tomer Tayar 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
/* 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);
}

/* This version of the lock should be used when acquiring the RTNL lock is also
 * needed in addition to the internal qede lock.
 */
Y
YueHaibing 已提交
1013
static void qede_lock(struct qede_dev *edev)
T
Tomer Tayar 已提交
1014 1015 1016 1017 1018
{
	rtnl_lock();
	__qede_lock(edev);
}

Y
YueHaibing 已提交
1019
static void qede_unlock(struct qede_dev *edev)
T
Tomer Tayar 已提交
1020 1021 1022 1023 1024
{
	__qede_unlock(edev);
	rtnl_unlock();
}

1025 1026 1027 1028
static void qede_sp_task(struct work_struct *work)
{
	struct qede_dev *edev = container_of(work, struct qede_dev,
					     sp_task.work);
1029

T
Tomer Tayar 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	/* The locking scheme depends on the specific flag:
	 * In case of QEDE_SP_RECOVERY, acquiring the RTNL lock is required to
	 * ensure that ongoing flows are ended and new ones are not started.
	 * In other cases - only the internal qede lock should be acquired.
	 */

	if (test_and_clear_bit(QEDE_SP_RECOVERY, &edev->sp_flags)) {
#ifdef CONFIG_QED_SRIOV
		/* SRIOV must be disabled outside the lock to avoid a deadlock.
		 * The recovery of the active VFs is currently not supported.
		 */
1041 1042
		if (pci_num_vf(edev->pdev))
			qede_sriov_configure(edev->pdev, 0);
T
Tomer Tayar 已提交
1043 1044 1045 1046 1047 1048
#endif
		qede_lock(edev);
		qede_recovery_handler(edev);
		qede_unlock(edev);
	}

M
Mintz, Yuval 已提交
1049
	__qede_lock(edev);
1050

M
Mintz, Yuval 已提交
1051 1052
	if (test_and_clear_bit(QEDE_SP_RX_MODE, &edev->sp_flags))
		if (edev->state == QEDE_STATE_OPEN)
1053 1054
			qede_config_rx_mode(edev->ndev);

C
Chopra, Manish 已提交
1055 1056 1057 1058 1059 1060
#ifdef CONFIG_RFS_ACCEL
	if (test_and_clear_bit(QEDE_SP_ARFS_CONFIG, &edev->sp_flags)) {
		if (edev->state == QEDE_STATE_OPEN)
			qede_process_arfs_filters(edev, false);
	}
#endif
1061 1062
	if (test_and_clear_bit(QEDE_SP_HW_ERR, &edev->sp_flags))
		qede_generic_hw_err_handler(edev);
M
Mintz, Yuval 已提交
1063
	__qede_unlock(edev);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

	if (test_and_clear_bit(QEDE_SP_AER, &edev->sp_flags)) {
#ifdef CONFIG_QED_SRIOV
		/* SRIOV must be disabled outside the lock to avoid a deadlock.
		 * The recovery of the active VFs is currently not supported.
		 */
		if (pci_num_vf(edev->pdev))
			qede_sriov_configure(edev->pdev, 0);
#endif
		edev->ops->common->recovery_process(edev->cdev);
	}
1075 1076
}

Y
Yuval Mintz 已提交
1077 1078 1079
static void qede_update_pf_params(struct qed_dev *cdev)
{
	struct qed_pf_params pf_params;
M
Manish Chopra 已提交
1080
	u16 num_cons;
Y
Yuval Mintz 已提交
1081

M
Mintz, Yuval 已提交
1082
	/* 64 rx + 64 tx + 64 XDP */
Y
Yuval Mintz 已提交
1083
	memset(&pf_params, 0, sizeof(struct qed_pf_params));
M
Manish Chopra 已提交
1084 1085 1086 1087 1088

	/* 1 rx + 1 xdp + max tx cos */
	num_cons = QED_MIN_L2_CONS;

	pf_params.eth_pf_params.num_cons = (MAX_SB_PER_PF_MIMD - 1) * num_cons;
M
Mintz, Yuval 已提交
1089 1090 1091 1092 1093 1094

	/* Same for VFs - make sure they'll have sufficient connections
	 * to support XDP Tx queues.
	 */
	pf_params.eth_pf_params.num_vf_cons = 48;

C
Chopra, Manish 已提交
1095
	pf_params.eth_pf_params.num_arfs_filters = QEDE_RFS_MAX_FLTR;
Y
Yuval Mintz 已提交
1096 1097 1098
	qed_ops->common->update_pf_params(cdev, &pf_params);
}

M
Mintz, Yuval 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
#define QEDE_FW_VER_STR_SIZE	80

static void qede_log_probe(struct qede_dev *edev)
{
	struct qed_dev_info *p_dev_info = &edev->dev_info.common;
	u8 buf[QEDE_FW_VER_STR_SIZE];
	size_t left_size;

	snprintf(buf, QEDE_FW_VER_STR_SIZE,
		 "Storm FW %d.%d.%d.%d, Management FW %d.%d.%d.%d",
		 p_dev_info->fw_major, p_dev_info->fw_minor, p_dev_info->fw_rev,
		 p_dev_info->fw_eng,
		 (p_dev_info->mfw_rev & QED_MFW_VERSION_3_MASK) >>
		 QED_MFW_VERSION_3_OFFSET,
		 (p_dev_info->mfw_rev & QED_MFW_VERSION_2_MASK) >>
		 QED_MFW_VERSION_2_OFFSET,
		 (p_dev_info->mfw_rev & QED_MFW_VERSION_1_MASK) >>
		 QED_MFW_VERSION_1_OFFSET,
		 (p_dev_info->mfw_rev & QED_MFW_VERSION_0_MASK) >>
		 QED_MFW_VERSION_0_OFFSET);

	left_size = QEDE_FW_VER_STR_SIZE - strlen(buf);
	if (p_dev_info->mbi_version && left_size)
		snprintf(buf + strlen(buf), left_size,
			 " [MBI %d.%d.%d]",
			 (p_dev_info->mbi_version & QED_MBI_VERSION_2_MASK) >>
			 QED_MBI_VERSION_2_OFFSET,
			 (p_dev_info->mbi_version & QED_MBI_VERSION_1_MASK) >>
			 QED_MBI_VERSION_1_OFFSET,
			 (p_dev_info->mbi_version & QED_MBI_VERSION_0_MASK) >>
			 QED_MBI_VERSION_0_OFFSET);

	pr_info("qede %02x:%02x.%02x: %s [%s]\n", edev->pdev->bus->number,
		PCI_SLOT(edev->pdev->devfn), PCI_FUNC(edev->pdev->devfn),
		buf, edev->ndev->name);
}

Y
Yuval Mintz 已提交
1136 1137
enum qede_probe_mode {
	QEDE_PROBE_NORMAL,
T
Tomer Tayar 已提交
1138
	QEDE_PROBE_RECOVERY,
Y
Yuval Mintz 已提交
1139 1140 1141
};

static int __qede_probe(struct pci_dev *pdev, u32 dp_module, u8 dp_level,
Y
Yuval Mintz 已提交
1142
			bool is_vf, enum qede_probe_mode mode)
Y
Yuval Mintz 已提交
1143
{
Y
Yuval Mintz 已提交
1144
	struct qed_probe_params probe_params;
Y
Yuval Mintz 已提交
1145
	struct qed_slowpath_params sp_params;
Y
Yuval Mintz 已提交
1146 1147 1148 1149 1150 1151 1152 1153
	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 已提交
1154 1155 1156 1157 1158
	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;
T
Tomer Tayar 已提交
1159
	probe_params.recov_in_prog = (mode == QEDE_PROBE_RECOVERY);
Y
Yuval Mintz 已提交
1160
	cdev = qed_ops->common->probe(pdev, &probe_params);
Y
Yuval Mintz 已提交
1161 1162 1163 1164 1165 1166 1167 1168
	if (!cdev) {
		rc = -ENODEV;
		goto err0;
	}

	qede_update_pf_params(cdev);

	/* Start the Slowpath-process */
Y
Yuval Mintz 已提交
1169 1170 1171 1172 1173 1174 1175 1176
	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 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	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;

T
Tomer Tayar 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
	if (mode != QEDE_PROBE_RECOVERY) {
		edev = qede_alloc_etherdev(cdev, pdev, &dev_info, dp_module,
					   dp_level);
		if (!edev) {
			rc = -ENOMEM;
			goto err2;
		}
	} else {
		struct net_device *ndev = pci_get_drvdata(pdev);

		edev = netdev_priv(ndev);
		edev->cdev = cdev;
		memset(&edev->stats, 0, sizeof(edev->stats));
		memcpy(&edev->dev_info, &dev_info, sizeof(dev_info));
Y
Yuval Mintz 已提交
1201 1202
	}

Y
Yuval Mintz 已提交
1203
	if (is_vf)
1204
		set_bit(QEDE_FLAGS_IS_VF, &edev->flags);
Y
Yuval Mintz 已提交
1205

Y
Yuval Mintz 已提交
1206 1207
	qede_init_ndev(edev);

T
Tomer Tayar 已提交
1208
	rc = qede_rdma_dev_add(edev, (mode == QEDE_PROBE_RECOVERY));
R
Ram Amrani 已提交
1209 1210 1211
	if (rc)
		goto err3;

T
Tomer Tayar 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
	if (mode != QEDE_PROBE_RECOVERY) {
		/* Prepare the lock prior to the registration of the netdev,
		 * as once it's registered we might reach flows requiring it
		 * [it's even possible to reach a flow needing it directly
		 * from there, although it's unlikely].
		 */
		INIT_DELAYED_WORK(&edev->sp_task, qede_sp_task);
		mutex_init(&edev->qede_lock);

		rc = register_netdev(edev->ndev);
		if (rc) {
			DP_NOTICE(edev, "Cannot register net-device\n");
			goto err4;
		}
1226 1227
	}

1228
	edev->ops->common->set_name(cdev, edev->ndev->name);
Y
Yuval Mintz 已提交
1229

1230
	/* PTP not supported on VFs */
1231
	if (!is_vf)
T
Tomer Tayar 已提交
1232
		qede_ptp_enable(edev, (mode == QEDE_PROBE_NORMAL));
1233

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

1236
#ifdef CONFIG_DCB
1237 1238
	if (!IS_VF(edev))
		qede_set_dcbnl_ops(edev->ndev);
1239 1240
#endif

1241
	edev->rx_copybreak = QEDE_RX_HDR_SIZE;
1242

M
Mintz, Yuval 已提交
1243
	qede_log_probe(edev);
Y
Yuval Mintz 已提交
1244 1245
	return 0;

R
Ram Amrani 已提交
1246
err4:
T
Tomer Tayar 已提交
1247
	qede_rdma_dev_remove(edev, (mode == QEDE_PROBE_RECOVERY));
1248 1249
err3:
	free_netdev(edev->ndev);
Y
Yuval Mintz 已提交
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
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 已提交
1260
	bool is_vf = false;
Y
Yuval Mintz 已提交
1261 1262 1263
	u32 dp_module = 0;
	u8 dp_level = 0;

Y
Yuval Mintz 已提交
1264 1265 1266 1267
	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");
1268
		is_vf = is_kdump_kernel() ? false : true;
Y
Yuval Mintz 已提交
1269 1270 1271 1272 1273 1274
		break;
	default:
		if (debug & QED_LOG_VERBOSE_MASK)
			dev_err(&pdev->dev, "Probing a PF\n");
	}

Y
Yuval Mintz 已提交
1275 1276
	qede_config_debug(debug, &dp_module, &dp_level);

Y
Yuval Mintz 已提交
1277
	return __qede_probe(pdev, dp_module, dp_level, is_vf,
Y
Yuval Mintz 已提交
1278 1279 1280 1281 1282
			    QEDE_PROBE_NORMAL);
}

enum qede_remove_mode {
	QEDE_REMOVE_NORMAL,
T
Tomer Tayar 已提交
1283
	QEDE_REMOVE_RECOVERY,
Y
Yuval Mintz 已提交
1284 1285 1286 1287 1288
};

static void __qede_remove(struct pci_dev *pdev, enum qede_remove_mode mode)
{
	struct net_device *ndev = pci_get_drvdata(pdev);
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	struct qede_dev *edev;
	struct qed_dev *cdev;

	if (!ndev) {
		dev_info(&pdev->dev, "Device has already been removed\n");
		return;
	}

	edev = netdev_priv(ndev);
	cdev = edev->cdev;
Y
Yuval Mintz 已提交
1299 1300 1301

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

T
Tomer Tayar 已提交
1302
	qede_rdma_dev_remove(edev, (mode == QEDE_REMOVE_RECOVERY));
1303

T
Tomer Tayar 已提交
1304 1305
	if (mode != QEDE_REMOVE_RECOVERY) {
		unregister_netdev(ndev);
1306

T
Tomer Tayar 已提交
1307
		cancel_delayed_work_sync(&edev->sp_task);
Y
Yuval Mintz 已提交
1308

T
Tomer Tayar 已提交
1309 1310 1311 1312 1313 1314
		edev->ops->common->set_power_state(cdev, PCI_D0);

		pci_set_drvdata(pdev, NULL);
	}

	qede_ptp_disable(edev);
Y
Yuval Mintz 已提交
1315 1316 1317

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

1322 1323 1324 1325 1326 1327
	/* Since this can happen out-of-sync with other flows,
	 * don't release the netdevice until after slowpath stop
	 * has been called to guarantee various other contexts
	 * [e.g., QED register callbacks] won't break anything when
	 * accessing the netdevice.
	 */
T
Tomer Tayar 已提交
1328 1329
	if (mode != QEDE_REMOVE_RECOVERY)
		free_netdev(ndev);
1330

Y
Yuval Mintz 已提交
1331
	dev_info(&pdev->dev, "Ending qede_remove successfully\n");
Y
Yuval Mintz 已提交
1332 1333 1334 1335 1336 1337
}

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

M
Mintz, Yuval 已提交
1339 1340 1341 1342 1343
static void qede_shutdown(struct pci_dev *pdev)
{
	__qede_remove(pdev, QEDE_REMOVE_NORMAL);
}

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
/* -------------------------------------------------------------------------
 * 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*/
1355 1356
	if (edev->req_queues)
		rss_num = edev->req_queues;
1357 1358 1359
	else
		rss_num = netif_get_num_default_rss_queues() *
			  edev->dev_info.common.num_hwfns;
1360 1361 1362 1363 1364 1365

	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 */
1366
		edev->num_queues = rc;
1367
		DP_INFO(edev, "Managed %d [of %d] RSS queues\n",
1368
			QEDE_QUEUE_CNT(edev), rss_num);
1369 1370
		rc = 0;
	}
1371 1372 1373 1374

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

1375 1376 1377
	return rc;
}

1378 1379
static void qede_free_mem_sb(struct qede_dev *edev, struct qed_sb_info *sb_info,
			     u16 sb_id)
1380
{
1381
	if (sb_info->sb_virt) {
1382 1383
		edev->ops->common->sb_release(edev->cdev, sb_info, sb_id,
					      QED_SB_TYPE_L2_QUEUE);
1384 1385
		dma_free_coherent(&edev->pdev->dev, sizeof(*sb_info->sb_virt),
				  (void *)sb_info->sb_virt, sb_info->sb_phys);
1386 1387
		memset(sb_info, 0, sizeof(*sb_info));
	}
1388 1389 1390 1391
}

/* This function allocates fast-path status block memory */
static int qede_alloc_mem_sb(struct qede_dev *edev,
Y
Yuval Mintz 已提交
1392
			     struct qed_sb_info *sb_info, u16 sb_id)
1393
{
1394
	struct status_block_e4 *sb_virt;
1395 1396 1397 1398
	dma_addr_t sb_phys;
	int rc;

	sb_virt = dma_alloc_coherent(&edev->pdev->dev,
Y
Yuval Mintz 已提交
1399
				     sizeof(*sb_virt), &sb_phys, GFP_KERNEL);
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
	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 已提交
1425
		struct page *data;
1426 1427 1428 1429

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

Y
Yuval Mintz 已提交
1430
		dma_unmap_page(&edev->pdev->dev,
M
Mintz, Yuval 已提交
1431
			       rx_buf->mapping, PAGE_SIZE, rxq->data_direction);
1432 1433

		rx_buf->data = NULL;
Y
Yuval Mintz 已提交
1434
		__free_page(data);
1435 1436 1437
	}
}

Y
Yuval Mintz 已提交
1438
static void qede_free_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
{
	/* 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);
}

M
Manish Chopra 已提交
1451
static void qede_set_tpa_param(struct qede_rx_queue *rxq)
1452 1453 1454 1455 1456 1457
{
	int i;

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

1458
		tpa_info->state = QEDE_AGG_STATE_NONE;
1459 1460 1461
	}
}

1462
/* This function allocates all memory needed per Rx queue */
Y
Yuval Mintz 已提交
1463
static int qede_alloc_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
1464
{
1465
	int i, rc, size;
1466 1467 1468

	rxq->num_rx_buffers = edev->q_num_rx_buffers;

Y
Yuval Mintz 已提交
1469
	rxq->rx_buf_size = NET_IP_ALIGN + ETH_OVERHEAD + edev->ndev->mtu;
M
Manish Chopra 已提交
1470 1471 1472 1473

	rxq->rx_headroom = edev->xdp_prog ? XDP_PACKET_HEADROOM : NET_SKB_PAD;
	size = rxq->rx_headroom +
	       SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Y
Yuval Mintz 已提交
1474

1475
	/* Make sure that the headroom and  payload fit in a single page */
M
Manish Chopra 已提交
1476 1477
	if (rxq->rx_buf_size + size > PAGE_SIZE)
		rxq->rx_buf_size = PAGE_SIZE - size;
Y
Yuval Mintz 已提交
1478

M
Manish Chopra 已提交
1479
	/* Segment size to spilt a page in multiple equal parts ,
M
Mintz, Yuval 已提交
1480 1481
	 * unless XDP is used in which case we'd use the entire page.
	 */
M
Manish Chopra 已提交
1482 1483 1484 1485
	if (!edev->xdp_prog) {
		size = size + rxq->rx_buf_size;
		rxq->rx_buf_seg_size = roundup_pow_of_two(size);
	} else {
M
Mintz, Yuval 已提交
1486
		rxq->rx_buf_seg_size = PAGE_SIZE;
1487
		edev->ndev->features &= ~NETIF_F_GRO_HW;
M
Manish Chopra 已提交
1488
	}
1489 1490

	/* Allocate the parallel driver ring for Rx buffers */
Y
Yuval Mintz 已提交
1491
	size = sizeof(*rxq->sw_rx_ring) * RX_RING_SIZE;
1492 1493 1494
	rxq->sw_rx_ring = kzalloc(size, GFP_KERNEL);
	if (!rxq->sw_rx_ring) {
		DP_ERR(edev, "Rx buffers ring allocation failed\n");
1495
		rc = -ENOMEM;
1496 1497 1498 1499 1500 1501 1502
		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 已提交
1503
					    QED_CHAIN_CNT_TYPE_U16,
Y
Yuval Mintz 已提交
1504
					    RX_RING_SIZE,
1505
					    sizeof(struct eth_rx_bd),
1506
					    &rxq->rx_bd_ring, NULL);
1507 1508 1509 1510 1511 1512 1513
	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 已提交
1514
					    QED_CHAIN_CNT_TYPE_U16,
Y
Yuval Mintz 已提交
1515
					    RX_RING_SIZE,
1516
					    sizeof(union eth_rx_cqe),
1517
					    &rxq->rx_comp_ring, NULL);
1518 1519 1520 1521
	if (rc)
		goto err;

	/* Allocate buffers for the Rx ring */
1522
	rxq->filled_buffers = 0;
1523
	for (i = 0; i < rxq->num_rx_buffers; i++) {
1524
		rc = qede_alloc_rx_buffer(rxq, false);
1525 1526 1527 1528 1529
		if (rc) {
			DP_ERR(edev,
			       "Rx buffers allocation failed at index %d\n", i);
			goto err;
		}
1530 1531
	}

1532
	edev->gro_disable = !(edev->ndev->features & NETIF_F_GRO_HW);
M
Manish Chopra 已提交
1533 1534
	if (!edev->gro_disable)
		qede_set_tpa_param(rxq);
1535
err:
1536
	return rc;
1537 1538
}

Y
Yuval Mintz 已提交
1539
static void qede_free_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
1540 1541
{
	/* Free the parallel SW ring */
M
Mintz, Yuval 已提交
1542
	if (txq->is_xdp)
1543
		kfree(txq->sw_tx_ring.xdp);
M
Mintz, Yuval 已提交
1544 1545
	else
		kfree(txq->sw_tx_ring.skbs);
1546 1547 1548 1549 1550 1551

	/* 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 已提交
1552
static int qede_alloc_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
1553 1554
{
	union eth_tx_bd_types *p_virt;
M
Mintz, Yuval 已提交
1555
	int size, rc;
1556 1557 1558 1559

	txq->num_tx_buffers = edev->q_num_tx_buffers;

	/* Allocate the parallel driver ring for Tx buffers */
M
Mintz, Yuval 已提交
1560
	if (txq->is_xdp) {
1561
		size = sizeof(*txq->sw_tx_ring.xdp) * txq->num_tx_buffers;
1562 1563
		txq->sw_tx_ring.xdp = kzalloc(size, GFP_KERNEL);
		if (!txq->sw_tx_ring.xdp)
M
Mintz, Yuval 已提交
1564 1565
			goto err;
	} else {
1566
		size = sizeof(*txq->sw_tx_ring.skbs) * txq->num_tx_buffers;
M
Mintz, Yuval 已提交
1567 1568 1569
		txq->sw_tx_ring.skbs = kzalloc(size, GFP_KERNEL);
		if (!txq->sw_tx_ring.skbs)
			goto err;
1570 1571 1572 1573 1574
	}

	rc = edev->ops->common->chain_alloc(edev->cdev,
					    QED_CHAIN_USE_TO_CONSUME_PRODUCE,
					    QED_CHAIN_MODE_PBL,
Y
Yuval Mintz 已提交
1575
					    QED_CHAIN_CNT_TYPE_U16,
1576
					    txq->num_tx_buffers,
1577 1578
					    sizeof(*p_virt),
					    &txq->tx_pbl, NULL);
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
	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 已提交
1590
static void qede_free_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
1591
{
1592
	qede_free_mem_sb(edev, fp->sb_info, fp->id);
1593

1594 1595
	if (fp->type & QEDE_FASTPATH_RX)
		qede_free_mem_rxq(edev, fp->rxq);
1596

1597 1598 1599
	if (fp->type & QEDE_FASTPATH_XDP)
		qede_free_mem_txq(edev, fp->xdp_tx);

M
Manish Chopra 已提交
1600 1601 1602 1603 1604 1605
	if (fp->type & QEDE_FASTPATH_TX) {
		int cos;

		for_each_cos_in_txq(edev, cos)
			qede_free_mem_txq(edev, &fp->txq[cos]);
	}
1606 1607 1608
}

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

1615
	rc = qede_alloc_mem_sb(edev, fp->sb_info, fp->id);
1616
	if (rc)
M
Mintz, Yuval 已提交
1617
		goto out;
1618

1619 1620
	if (fp->type & QEDE_FASTPATH_RX) {
		rc = qede_alloc_mem_rxq(edev, fp->rxq);
1621
		if (rc)
M
Mintz, Yuval 已提交
1622 1623 1624 1625 1626 1627 1628
			goto out;
	}

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

1631
	if (fp->type & QEDE_FASTPATH_TX) {
M
Manish Chopra 已提交
1632 1633 1634 1635 1636 1637 1638
		int cos;

		for_each_cos_in_txq(edev, cos) {
			rc = qede_alloc_mem_txq(edev, &fp->txq[cos]);
			if (rc)
				goto out;
		}
1639 1640
	}

M
Mintz, Yuval 已提交
1641
out:
1642
	return rc;
1643 1644 1645 1646 1647 1648
}

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

1649
	for_each_queue(i) {
1650 1651 1652 1653 1654 1655 1656 1657 1658
		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)
{
1659
	int rc = 0, queue_id;
1660

1661 1662
	for (queue_id = 0; queue_id < QEDE_QUEUE_CNT(edev); queue_id++) {
		struct qede_fastpath *fp = &edev->fp_array[queue_id];
1663 1664

		rc = qede_alloc_mem_fp(edev, fp);
1665
		if (rc) {
1666
			DP_ERR(edev,
1667
			       "Failed to allocate memory for fastpath - rss id = %d\n",
1668
			       queue_id);
1669 1670
			qede_free_mem_load(edev);
			return rc;
1671 1672 1673 1674 1675 1676
		}
	}

	return 0;
}

T
Tomer Tayar 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 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 1725 1726 1727 1728
static void qede_empty_tx_queue(struct qede_dev *edev,
				struct qede_tx_queue *txq)
{
	unsigned int pkts_compl = 0, bytes_compl = 0;
	struct netdev_queue *netdev_txq;
	int rc, len = 0;

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

	while (qed_chain_get_cons_idx(&txq->tx_pbl) !=
	       qed_chain_get_prod_idx(&txq->tx_pbl)) {
		DP_VERBOSE(edev, NETIF_MSG_IFDOWN,
			   "Freeing a packet on tx queue[%d]: chain_cons 0x%x, chain_prod 0x%x\n",
			   txq->index, qed_chain_get_cons_idx(&txq->tx_pbl),
			   qed_chain_get_prod_idx(&txq->tx_pbl));

		rc = qede_free_tx_pkt(edev, txq, &len);
		if (rc) {
			DP_NOTICE(edev,
				  "Failed to free a packet on tx queue[%d]: chain_cons 0x%x, chain_prod 0x%x\n",
				  txq->index,
				  qed_chain_get_cons_idx(&txq->tx_pbl),
				  qed_chain_get_prod_idx(&txq->tx_pbl));
			break;
		}

		bytes_compl += len;
		pkts_compl++;
		txq->sw_tx_cons++;
	}

	netdev_tx_completed_queue(netdev_txq, pkts_compl, bytes_compl);
}

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

	for_each_queue(i)
		if (edev->fp_array[i].type & QEDE_FASTPATH_TX) {
			int cos;

			for_each_cos_in_txq(edev, cos) {
				struct qede_fastpath *fp;

				fp = &edev->fp_array[i];
				qede_empty_tx_queue(edev,
						    &fp->txq[cos]);
			}
		}
}

1729 1730 1731
/* 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 已提交
1732
	int queue_id, rxq_index = 0, txq_index = 0;
1733 1734
	struct qede_fastpath *fp;

1735 1736
	for_each_queue(queue_id) {
		fp = &edev->fp_array[queue_id];
1737 1738

		fp->edev = edev;
1739
		fp->id = queue_id;
1740

M
Mintz, Yuval 已提交
1741 1742 1743 1744 1745
		if (fp->type & QEDE_FASTPATH_XDP) {
			fp->xdp_tx->index = QEDE_TXQ_IDX_TO_XDP(edev,
								rxq_index);
			fp->xdp_tx->is_xdp = 1;
		}
1746

1747 1748
		if (fp->type & QEDE_FASTPATH_RX) {
			fp->rxq->rxq_id = rxq_index++;
M
Mintz, Yuval 已提交
1749 1750 1751 1752 1753 1754

			/* 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;
1755
			fp->rxq->dev = &edev->pdev->dev;
1756 1757 1758 1759

			/* Driver have no error path from here */
			WARN_ON(xdp_rxq_info_reg(&fp->rxq->xdp_rxq, edev->ndev,
						 fp->rxq->rxq_id) < 0);
1760
		}
1761

1762
		if (fp->type & QEDE_FASTPATH_TX) {
M
Manish Chopra 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
			int cos;

			for_each_cos_in_txq(edev, cos) {
				struct qede_tx_queue *txq = &fp->txq[cos];
				u16 ndev_tx_id;

				txq->cos = cos;
				txq->index = txq_index;
				ndev_tx_id = QEDE_TXQ_TO_NDEV_TXQ_ID(edev, txq);
				txq->ndev_txq_id = ndev_tx_id;

				if (edev->dev_info.is_legacy)
1775
					txq->is_legacy = true;
M
Manish Chopra 已提交
1776 1777 1778 1779
				txq->dev = &edev->pdev->dev;
			}

			txq_index++;
1780 1781 1782
		}

		snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
1783
			 edev->ndev->name, queue_id);
1784 1785 1786 1787 1788 1789 1790
	}
}

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

M
Manish Chopra 已提交
1791 1792 1793
	rc = netif_set_real_num_tx_queues(edev->ndev,
					  QEDE_TSS_COUNT(edev) *
					  edev->dev_info.num_tc);
1794 1795 1796 1797
	if (rc) {
		DP_NOTICE(edev, "Failed to set real number of Tx queues\n");
		return rc;
	}
1798 1799

	rc = netif_set_real_num_rx_queues(edev->ndev, QEDE_RSS_COUNT(edev));
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
	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;

1812
	for_each_queue(i) {
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
		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 */
1824
	for_each_queue(i) {
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
		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 */
1853
	if (QEDE_QUEUE_CNT(edev) > edev->int_info.msix_cnt) {
1854 1855
		DP_ERR(edev,
		       "Interrupt mismatch: %d RSS queues > %d MSI-x vectors\n",
1856
		       QEDE_QUEUE_CNT(edev), edev->int_info.msix_cnt);
1857 1858 1859
		return -EINVAL;
	}

1860
	for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
C
Chopra, Manish 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
#ifdef CONFIG_RFS_ACCEL
		struct qede_fastpath *fp = &edev->fp_array[i];

		if (edev->ndev->rx_cpu_rmap && (fp->type & QEDE_FASTPATH_RX)) {
			rc = irq_cpu_rmap_add(edev->ndev->rx_cpu_rmap,
					      edev->int_info.msix[i].vector);
			if (rc) {
				DP_ERR(edev, "Failed to add CPU rmap\n");
				qede_free_arfs(edev);
			}
		}
#endif
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
		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;
1917
		for (i = 0; i < QEDE_QUEUE_CNT(edev); i++)
1918 1919 1920
			ops->simd_handler_config(edev->cdev,
						 &edev->fp_array[i], i,
						 qede_simd_fp_handler);
1921
		edev->int_info.used_cnt = QEDE_QUEUE_CNT(edev);
1922 1923 1924 1925 1926
	}
	return 0;
}

static int qede_drain_txq(struct qede_dev *edev,
Y
Yuval Mintz 已提交
1927
			  struct qede_tx_queue *txq, bool allow_drain)
1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
{
	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 已提交
1959 1960 1961
static int qede_stop_txq(struct qede_dev *edev,
			 struct qede_tx_queue *txq, int rss_id)
{
1962 1963 1964 1965
	/* delete doorbell from doorbell recovery mechanism */
	edev->ops->common->db_recovery_del(edev->cdev, txq->doorbell_addr,
					   &txq->tx_db);

M
Mintz, Yuval 已提交
1966 1967 1968
	return edev->ops->q_tx_stop(edev->cdev, rss_id, txq->handle);
}

1969 1970
static int qede_stop_queues(struct qede_dev *edev)
{
1971
	struct qed_update_vport_params *vport_update_params;
1972
	struct qed_dev *cdev = edev->cdev;
M
Mintz, Yuval 已提交
1973 1974
	struct qede_fastpath *fp;
	int rc, i;
1975 1976

	/* Disable the vport */
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
	vport_update_params = vzalloc(sizeof(*vport_update_params));
	if (!vport_update_params)
		return -ENOMEM;

	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);
	vfree(vport_update_params);
1988 1989 1990 1991 1992 1993 1994

	if (rc) {
		DP_ERR(edev, "Failed to update vport\n");
		return rc;
	}

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

1998
		if (fp->type & QEDE_FASTPATH_TX) {
M
Manish Chopra 已提交
1999 2000 2001 2002 2003 2004 2005
			int cos;

			for_each_cos_in_txq(edev, cos) {
				rc = qede_drain_txq(edev, &fp->txq[cos], true);
				if (rc)
					return rc;
			}
2006
		}
M
Mintz, Yuval 已提交
2007 2008 2009 2010 2011 2012

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

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

2019
		/* Stop the Tx Queue(s) */
M
Mintz, Yuval 已提交
2020
		if (fp->type & QEDE_FASTPATH_TX) {
M
Manish Chopra 已提交
2021 2022 2023 2024 2025 2026 2027
			int cos;

			for_each_cos_in_txq(edev, cos) {
				rc = qede_stop_txq(edev, &fp->txq[cos], i);
				if (rc)
					return rc;
			}
2028 2029
		}

2030
		/* Stop the Rx Queue */
M
Mintz, Yuval 已提交
2031
		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
2032
			rc = edev->ops->q_rx_stop(cdev, i, fp->rxq->handle);
2033 2034 2035 2036
			if (rc) {
				DP_ERR(edev, "Failed to stop RXQ #%d\n", i);
				return rc;
			}
2037
		}
M
Mintz, Yuval 已提交
2038

M
Mintz, Yuval 已提交
2039 2040 2041 2042 2043 2044
		/* 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 已提交
2045
			bpf_prog_put(fp->rxq->xdp_prog);
M
Mintz, Yuval 已提交
2046
		}
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
	}

	/* 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 已提交
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
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 已提交
2070 2071 2072 2073 2074 2075 2076 2077
	/* 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;

2078
	params.p_sb = fp->sb_info;
M
Mintz, Yuval 已提交
2079
	params.sb_idx = sb_idx;
M
Manish Chopra 已提交
2080
	params.tc = txq->cos;
M
Mintz, Yuval 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101

	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;

2102 2103 2104 2105 2106
	/* register doorbell with doorbell recovery mechanism */
	rc = edev->ops->common->db_recovery_add(edev->cdev, txq->doorbell_addr,
						&txq->tx_db, DB_REC_WIDTH_32B,
						DB_REC_KERNEL);

M
Mintz, Yuval 已提交
2107 2108 2109
	return rc;
}

2110
static int qede_start_queues(struct qede_dev *edev, bool clear_stats)
2111
{
2112
	int vlan_removal_en = 1;
2113
	struct qed_dev *cdev = edev->cdev;
Y
Yuval Mintz 已提交
2114
	struct qed_dev_info *qed_info = &edev->dev_info.common;
2115 2116
	struct qed_update_vport_params *vport_update_params;
	struct qed_queue_start_common_params q_params;
2117
	struct qed_start_vport_params start = {0};
M
Mintz, Yuval 已提交
2118
	int rc, i;
2119

2120
	if (!edev->num_queues) {
2121 2122 2123 2124 2125
		DP_ERR(edev,
		       "Cannot update V-VPORT as active as there are no Rx queues\n");
		return -EINVAL;
	}

2126 2127 2128 2129
	vport_update_params = vzalloc(sizeof(*vport_update_params));
	if (!vport_update_params)
		return -ENOMEM;

2130
	start.handle_ptp_pkts = !!(edev->ptp);
2131
	start.gro_enable = !edev->gro_disable;
2132 2133 2134 2135
	start.mtu = edev->ndev->mtu;
	start.vport_id = 0;
	start.drop_ttl0 = true;
	start.remove_inner_vlan = vlan_removal_en;
2136
	start.clear_stats = clear_stats;
2137 2138

	rc = edev->ops->vport_start(cdev, &start);
2139 2140 2141

	if (rc) {
		DP_ERR(edev, "Start V-PORT failed %d\n", rc);
2142
		goto out;
2143 2144 2145 2146
	}

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

2149
	for_each_queue(i) {
2150
		struct qede_fastpath *fp = &edev->fp_array[i];
2151 2152
		dma_addr_t p_phys_table;
		u32 page_cnt;
2153

2154
		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
2155
			struct qed_rxq_start_ret_params ret_params;
2156 2157
			struct qede_rx_queue *rxq = fp->rxq;
			__le16 *val;
2158

M
Mintz, Yuval 已提交
2159
			memset(&ret_params, 0, sizeof(ret_params));
2160 2161 2162
			memset(&q_params, 0, sizeof(q_params));
			q_params.queue_id = rxq->rxq_id;
			q_params.vport_id = 0;
2163
			q_params.p_sb = fp->sb_info;
2164 2165 2166 2167 2168 2169
			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 已提交
2170
			rc = edev->ops->q_rx_start(cdev, i, &q_params,
2171 2172 2173
						   rxq->rx_buf_size,
						   rxq->rx_bd_ring.p_phys_addr,
						   p_phys_table,
M
Mintz, Yuval 已提交
2174
						   page_cnt, &ret_params);
2175 2176 2177
			if (rc) {
				DP_ERR(edev, "Start RXQ #%d failed %d\n", i,
				       rc);
2178
				goto out;
2179 2180
			}

M
Mintz, Yuval 已提交
2181 2182 2183 2184
			/* Use the return parameters */
			rxq->hw_rxq_prod_addr = ret_params.p_prod;
			rxq->handle = ret_params.p_handle;

2185 2186 2187 2188 2189 2190
			val = &fp->sb_info->sb_virt->pi_array[RX_PI];
			rxq->hw_cons_ptr = val;

			qede_update_rx_prod(edev, rxq);
		}

M
Mintz, Yuval 已提交
2191
		if (fp->type & QEDE_FASTPATH_XDP) {
M
Mintz, Yuval 已提交
2192 2193
			rc = qede_start_txq(edev, fp, fp->xdp_tx, i, XDP_PI);
			if (rc)
2194
				goto out;
M
Mintz, Yuval 已提交
2195

2196 2197
			bpf_prog_add(edev->xdp_prog, 1);
			fp->rxq->xdp_prog = edev->xdp_prog;
M
Mintz, Yuval 已提交
2198 2199
		}

M
Mintz, Yuval 已提交
2200
		if (fp->type & QEDE_FASTPATH_TX) {
M
Manish Chopra 已提交
2201 2202 2203 2204 2205 2206 2207 2208
			int cos;

			for_each_cos_in_txq(edev, cos) {
				rc = qede_start_txq(edev, fp, &fp->txq[cos], i,
						    TX_PI(cos));
				if (rc)
					goto out;
			}
2209 2210 2211 2212
		}
	}

	/* Prepare and send the vport enable */
2213 2214 2215
	vport_update_params->vport_id = start.vport_id;
	vport_update_params->update_vport_active_flg = 1;
	vport_update_params->vport_active_flg = 1;
2216

2217
	if ((qed_info->b_inter_pf_switch || pci_num_vf(edev->pdev)) &&
Y
Yuval Mintz 已提交
2218
	    qed_info->tx_switching) {
2219 2220
		vport_update_params->update_tx_switching_flg = 1;
		vport_update_params->tx_switching_flg = 1;
Y
Yuval Mintz 已提交
2221 2222
	}

2223 2224
	qede_fill_rss_params(edev, &vport_update_params->rss_params,
			     &vport_update_params->update_rss_flg);
2225

2226 2227
	rc = edev->ops->vport_update(cdev, vport_update_params);
	if (rc)
2228 2229
		DP_ERR(edev, "Update V-PORT failed %d\n", rc);

2230 2231 2232
out:
	vfree(vport_update_params);
	return rc;
2233 2234 2235 2236
}

enum qede_unload_mode {
	QEDE_UNLOAD_NORMAL,
T
Tomer Tayar 已提交
2237
	QEDE_UNLOAD_RECOVERY,
2238 2239
};

M
Mintz, Yuval 已提交
2240 2241
static void qede_unload(struct qede_dev *edev, enum qede_unload_mode mode,
			bool is_locked)
2242
{
S
Sudarsana Kalluru 已提交
2243
	struct qed_link_params link_params;
2244 2245 2246 2247
	int rc;

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

M
Mintz, Yuval 已提交
2248 2249 2250
	if (!is_locked)
		__qede_lock(edev);

2251 2252
	clear_bit(QEDE_FLAGS_LINK_REQUESTED, &edev->flags);

T
Tomer Tayar 已提交
2253 2254
	if (mode != QEDE_UNLOAD_RECOVERY)
		edev->state = QEDE_STATE_CLOSED;
2255

2256
	qede_rdma_dev_event_close(edev);
2257

2258 2259 2260 2261
	/* Close OS Tx */
	netif_tx_disable(edev->ndev);
	netif_carrier_off(edev->ndev);

T
Tomer Tayar 已提交
2262 2263 2264 2265 2266
	if (mode != QEDE_UNLOAD_RECOVERY) {
		/* Reset the link */
		memset(&link_params, 0, sizeof(link_params));
		link_params.link_up = false;
		edev->ops->common->set_link(edev->cdev, &link_params);
2267

T
Tomer Tayar 已提交
2268 2269 2270 2271 2272 2273 2274 2275
		rc = qede_stop_queues(edev);
		if (rc) {
			qede_sync_free_irqs(edev);
			goto out;
		}

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

2277
	qede_vlan_mark_nonconfigured(edev);
2278
	edev->ops->fastpath_stop(edev->cdev);
2279

C
Chopra, Manish 已提交
2280 2281 2282 2283
	if (!IS_VF(edev) && edev->dev_info.common.num_hwfns == 1) {
		qede_poll_for_freeing_arfs_filters(edev);
		qede_free_arfs(edev);
	}
2284

2285 2286 2287 2288 2289 2290
	/* Release the interrupts */
	qede_sync_free_irqs(edev);
	edev->ops->common->set_fp_int(edev->cdev, 0);

	qede_napi_disable_remove(edev);

T
Tomer Tayar 已提交
2291 2292 2293
	if (mode == QEDE_UNLOAD_RECOVERY)
		qede_empty_tx_queues(edev);

2294 2295 2296 2297
	qede_free_mem_load(edev);
	qede_free_fp_array(edev);

out:
M
Mintz, Yuval 已提交
2298 2299
	if (!is_locked)
		__qede_unlock(edev);
T
Tomer Tayar 已提交
2300 2301 2302 2303

	if (mode != QEDE_UNLOAD_RECOVERY)
		DP_NOTICE(edev, "Link is down\n");

2304 2305
	edev->ptp_skip_txts = 0;

2306 2307 2308 2309 2310
	DP_INFO(edev, "Ending qede unload\n");
}

enum qede_load_mode {
	QEDE_LOAD_NORMAL,
2311
	QEDE_LOAD_RELOAD,
T
Tomer Tayar 已提交
2312
	QEDE_LOAD_RECOVERY,
2313 2314
};

M
Mintz, Yuval 已提交
2315 2316
static int qede_load(struct qede_dev *edev, enum qede_load_mode mode,
		     bool is_locked)
2317
{
S
Sudarsana Kalluru 已提交
2318
	struct qed_link_params link_params;
M
Manish Chopra 已提交
2319
	u8 num_tc;
2320 2321 2322 2323
	int rc;

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

M
Mintz, Yuval 已提交
2324 2325 2326
	if (!is_locked)
		__qede_lock(edev);

2327 2328
	rc = qede_set_num_queues(edev);
	if (rc)
M
Mintz, Yuval 已提交
2329
		goto out;
2330 2331 2332

	rc = qede_alloc_fp_array(edev);
	if (rc)
M
Mintz, Yuval 已提交
2333
		goto out;
2334 2335 2336 2337 2338 2339

	qede_init_fp(edev);

	rc = qede_alloc_mem_load(edev);
	if (rc)
		goto err1;
M
Mintz, Yuval 已提交
2340 2341
	DP_INFO(edev, "Allocated %d Rx, %d Tx queues\n",
		QEDE_RSS_COUNT(edev), QEDE_TSS_COUNT(edev));
2342 2343 2344 2345 2346

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

C
Chopra, Manish 已提交
2347 2348 2349 2350 2351
	if (!IS_VF(edev) && edev->dev_info.common.num_hwfns == 1) {
		rc = qede_alloc_arfs(edev);
		if (rc)
			DP_NOTICE(edev, "aRFS memory allocation failed\n");
	}
2352

2353 2354 2355 2356 2357 2358 2359 2360
	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");

2361
	rc = qede_start_queues(edev, mode != QEDE_LOAD_RELOAD);
2362 2363 2364 2365
	if (rc)
		goto err4;
	DP_INFO(edev, "Start VPORT, RXQ and TXQ succeeded\n");

M
Manish Chopra 已提交
2366 2367 2368 2369
	num_tc = netdev_get_num_tc(edev->ndev);
	num_tc = num_tc ? num_tc : edev->dev_info.num_tc;
	qede_setup_tc(edev->ndev, num_tc);

2370 2371 2372
	/* Program un-configured VLANs */
	qede_configure_vlan_filters(edev);

2373 2374
	set_bit(QEDE_FLAGS_LINK_REQUESTED, &edev->flags);

S
Sudarsana Kalluru 已提交
2375 2376 2377 2378 2379
	/* 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);

M
Mintz, Yuval 已提交
2380 2381
	edev->state = QEDE_STATE_OPEN;

2382 2383
	DP_INFO(edev, "Ending successfully qede load\n");

M
Mintz, Yuval 已提交
2384
	goto out;
2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
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);
2395 2396 2397
	edev->num_queues = 0;
	edev->fp_num_tx = 0;
	edev->fp_num_rx = 0;
M
Mintz, Yuval 已提交
2398 2399 2400 2401
out:
	if (!is_locked)
		__qede_unlock(edev);

2402 2403 2404
	return rc;
}

M
Mintz, Yuval 已提交
2405 2406 2407
/* 'func' should be able to run between unload and reload assuming interface
 * is actually running, or afterwards in case it's currently DOWN.
 */
2408
void qede_reload(struct qede_dev *edev,
M
Mintz, Yuval 已提交
2409
		 struct qede_reload_args *args, bool is_locked)
2410
{
M
Mintz, Yuval 已提交
2411 2412 2413 2414 2415 2416
	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.
2417
	 */
M
Mintz, Yuval 已提交
2418 2419 2420 2421 2422
	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);
2423

M
Mintz, Yuval 已提交
2424 2425 2426 2427 2428
		/* 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);
	}
2429

M
Mintz, Yuval 已提交
2430 2431
	if (!is_locked)
		__qede_unlock(edev);
2432 2433
}

2434 2435 2436 2437
/* called with rtnl_lock */
static int qede_open(struct net_device *ndev)
{
	struct qede_dev *edev = netdev_priv(ndev);
2438
	int rc;
2439 2440 2441 2442 2443

	netif_carrier_off(ndev);

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

M
Mintz, Yuval 已提交
2444
	rc = qede_load(edev, QEDE_LOAD_NORMAL, false);
2445 2446 2447
	if (rc)
		return rc;

2448 2449
	udp_tunnel_get_rx_info(ndev);

2450 2451
	edev->ops->common->update_drv_state(edev->cdev, true);

2452
	return 0;
2453 2454 2455 2456 2457 2458
}

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

M
Mintz, Yuval 已提交
2459
	qede_unload(edev, QEDE_UNLOAD_NORMAL, false);
2460

2461 2462
	edev->ops->common->update_drv_state(edev->cdev, false);

2463 2464
	return 0;
}
2465

S
Sudarsana Kalluru 已提交
2466 2467 2468 2469
static void qede_link_update(void *dev, struct qed_link_output *link)
{
	struct qede_dev *edev = dev;

2470 2471
	if (!test_bit(QEDE_FLAGS_LINK_REQUESTED, &edev->flags)) {
		DP_VERBOSE(edev, NETIF_MSG_LINK, "Interface is not ready\n");
S
Sudarsana Kalluru 已提交
2472 2473 2474 2475
		return;
	}

	if (link->link_up) {
2476 2477 2478 2479
		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);
M
Michal Kalderon 已提交
2480
			qede_rdma_dev_event_open(edev);
2481
		}
S
Sudarsana Kalluru 已提交
2482
	} else {
2483 2484 2485 2486
		if (netif_carrier_ok(edev->ndev)) {
			DP_NOTICE(edev, "Link is down\n");
			netif_tx_disable(edev->ndev);
			netif_carrier_off(edev->ndev);
M
Michal Kalderon 已提交
2487
			qede_rdma_dev_event_close(edev);
2488
		}
S
Sudarsana Kalluru 已提交
2489 2490
	}
}
2491

T
Tomer Tayar 已提交
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562
static void qede_schedule_recovery_handler(void *dev)
{
	struct qede_dev *edev = dev;

	if (edev->state == QEDE_STATE_RECOVERY) {
		DP_NOTICE(edev,
			  "Avoid scheduling a recovery handling since already in recovery state\n");
		return;
	}

	set_bit(QEDE_SP_RECOVERY, &edev->sp_flags);
	schedule_delayed_work(&edev->sp_task, 0);

	DP_INFO(edev, "Scheduled a recovery handler\n");
}

static void qede_recovery_failed(struct qede_dev *edev)
{
	netdev_err(edev->ndev, "Recovery handling has failed. Power cycle is needed.\n");

	netif_device_detach(edev->ndev);

	if (edev->cdev)
		edev->ops->common->set_power_state(edev->cdev, PCI_D3hot);
}

static void qede_recovery_handler(struct qede_dev *edev)
{
	u32 curr_state = edev->state;
	int rc;

	DP_NOTICE(edev, "Starting a recovery process\n");

	/* No need to acquire first the qede_lock since is done by qede_sp_task
	 * before calling this function.
	 */
	edev->state = QEDE_STATE_RECOVERY;

	edev->ops->common->recovery_prolog(edev->cdev);

	if (curr_state == QEDE_STATE_OPEN)
		qede_unload(edev, QEDE_UNLOAD_RECOVERY, true);

	__qede_remove(edev->pdev, QEDE_REMOVE_RECOVERY);

	rc = __qede_probe(edev->pdev, edev->dp_module, edev->dp_level,
			  IS_VF(edev), QEDE_PROBE_RECOVERY);
	if (rc) {
		edev->cdev = NULL;
		goto err;
	}

	if (curr_state == QEDE_STATE_OPEN) {
		rc = qede_load(edev, QEDE_LOAD_RECOVERY, true);
		if (rc)
			goto err;

		qede_config_rx_mode(edev->ndev);
		udp_tunnel_get_rx_info(edev->ndev);
	}

	edev->state = curr_state;

	DP_NOTICE(edev, "Recovery handling is done\n");

	return;

err:
	qede_recovery_failed(edev);
}

2563 2564
static void qede_atomic_hw_err_handler(struct qede_dev *edev)
{
2565 2566
	struct qed_dev *cdev = edev->cdev;

2567 2568 2569 2570 2571 2572 2573
	DP_NOTICE(edev,
		  "Generic non-sleepable HW error handling started - err_flags 0x%lx\n",
		  edev->err_flags);

	/* Get a call trace of the flow that led to the error */
	WARN_ON(test_bit(QEDE_ERR_WARN, &edev->err_flags));

2574 2575 2576 2577
	/* Prevent HW attentions from being reasserted */
	if (test_bit(QEDE_ERR_ATTN_CLR_EN, &edev->err_flags))
		edev->ops->common->attn_clr_enable(cdev, true);

2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
	DP_NOTICE(edev, "Generic non-sleepable HW error handling is done\n");
}

static void qede_generic_hw_err_handler(struct qede_dev *edev)
{
	struct qed_dev *cdev = edev->cdev;

	DP_NOTICE(edev,
		  "Generic sleepable HW error handling started - err_flags 0x%lx\n",
		  edev->err_flags);

	/* Trigger a recovery process.
	 * This is placed in the sleep requiring section just to make
	 * sure it is the last one, and that all the other operations
	 * were completed.
	 */
	if (test_bit(QEDE_ERR_IS_RECOVERABLE, &edev->err_flags))
		edev->ops->common->recovery_process(cdev);

	clear_bit(QEDE_ERR_IS_HANDLED, &edev->err_flags);

	DP_NOTICE(edev, "Generic sleepable HW error handling is done\n");
}

static void qede_set_hw_err_flags(struct qede_dev *edev,
				  enum qed_hw_err_type err_type)
{
	unsigned long err_flags = 0;

	switch (err_type) {
	case QED_HW_ERR_DMAE_FAIL:
		set_bit(QEDE_ERR_WARN, &err_flags);
		fallthrough;
	case QED_HW_ERR_MFW_RESP_FAIL:
	case QED_HW_ERR_HW_ATTN:
	case QED_HW_ERR_RAMROD_FAIL:
	case QED_HW_ERR_FW_ASSERT:
		set_bit(QEDE_ERR_ATTN_CLR_EN, &err_flags);
		set_bit(QEDE_ERR_GET_DBG_INFO, &err_flags);
		break;

	default:
		DP_NOTICE(edev, "Unexpected HW error [%d]\n", err_type);
		break;
	}

	edev->err_flags |= err_flags;
}

static void qede_schedule_hw_err_handler(void *dev,
					 enum qed_hw_err_type err_type)
{
	struct qede_dev *edev = dev;

	/* Fan failure cannot be masked by handling of another HW error or by a
	 * concurrent recovery process.
	 */
	if ((test_and_set_bit(QEDE_ERR_IS_HANDLED, &edev->err_flags) ||
	     edev->state == QEDE_STATE_RECOVERY) &&
	     err_type != QED_HW_ERR_FAN_FAIL) {
		DP_INFO(edev,
			"Avoid scheduling an error handling while another HW error is being handled\n");
		return;
	}

	if (err_type >= QED_HW_ERR_LAST) {
		DP_NOTICE(edev, "Unknown HW error [%d]\n", err_type);
		clear_bit(QEDE_ERR_IS_HANDLED, &edev->err_flags);
		return;
	}

	qede_set_hw_err_flags(edev, err_type);
	qede_atomic_hw_err_handler(edev);
	set_bit(QEDE_SP_HW_ERR, &edev->sp_flags);
	schedule_delayed_work(&edev->sp_task, 0);

	DP_INFO(edev, "Scheduled a error handler [err_type %d]\n", err_type);
}

2657 2658 2659 2660
static bool qede_is_txq_full(struct qede_dev *edev, struct qede_tx_queue *txq)
{
	struct netdev_queue *netdev_txq;

M
Manish Chopra 已提交
2661
	netdev_txq = netdev_get_tx_queue(edev->ndev, txq->ndev_txq_id);
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725
	if (netif_xmit_stopped(netdev_txq))
		return true;

	return false;
}

static void qede_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
{
	struct qede_dev *edev = dev;
	struct netdev_hw_addr *ha;
	int i;

	if (edev->ndev->features & NETIF_F_IP_CSUM)
		data->feat_flags |= QED_TLV_IP_CSUM;
	if (edev->ndev->features & NETIF_F_TSO)
		data->feat_flags |= QED_TLV_LSO;

	ether_addr_copy(data->mac[0], edev->ndev->dev_addr);
	memset(data->mac[1], 0, ETH_ALEN);
	memset(data->mac[2], 0, ETH_ALEN);
	/* Copy the first two UC macs */
	netif_addr_lock_bh(edev->ndev);
	i = 1;
	netdev_for_each_uc_addr(ha, edev->ndev) {
		ether_addr_copy(data->mac[i++], ha->addr);
		if (i == QED_TLV_MAC_COUNT)
			break;
	}

	netif_addr_unlock_bh(edev->ndev);
}

static void qede_get_eth_tlv_data(void *dev, void *data)
{
	struct qed_mfw_tlv_eth *etlv = data;
	struct qede_dev *edev = dev;
	struct qede_fastpath *fp;
	int i;

	etlv->lso_maxoff_size = 0XFFFF;
	etlv->lso_maxoff_size_set = true;
	etlv->lso_minseg_size = (u16)ETH_TX_LSO_WINDOW_MIN_LEN;
	etlv->lso_minseg_size_set = true;
	etlv->prom_mode = !!(edev->ndev->flags & IFF_PROMISC);
	etlv->prom_mode_set = true;
	etlv->tx_descr_size = QEDE_TSS_COUNT(edev);
	etlv->tx_descr_size_set = true;
	etlv->rx_descr_size = QEDE_RSS_COUNT(edev);
	etlv->rx_descr_size_set = true;
	etlv->iov_offload = QED_MFW_TLV_IOV_OFFLOAD_VEB;
	etlv->iov_offload_set = true;

	/* Fill information regarding queues; Should be done under the qede
	 * lock to guarantee those don't change beneath our feet.
	 */
	etlv->txqs_empty = true;
	etlv->rxqs_empty = true;
	etlv->num_txqs_full = 0;
	etlv->num_rxqs_full = 0;

	__qede_lock(edev);
	for_each_queue(i) {
		fp = &edev->fp_array[i];
		if (fp->type & QEDE_FASTPATH_TX) {
M
Manish Chopra 已提交
2726 2727 2728
			struct qede_tx_queue *txq = QEDE_FP_TC0_TXQ(fp);

			if (txq->sw_tx_cons != txq->sw_tx_prod)
2729
				etlv->txqs_empty = false;
M
Manish Chopra 已提交
2730
			if (qede_is_txq_full(edev, txq))
2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753
				etlv->num_txqs_full++;
		}
		if (fp->type & QEDE_FASTPATH_RX) {
			if (qede_has_rx_work(fp->rxq))
				etlv->rxqs_empty = false;

			/* This one is a bit tricky; Firmware might stop
			 * placing packets if ring is not yet full.
			 * Give an approximation.
			 */
			if (le16_to_cpu(*fp->rxq->hw_cons_ptr) -
			    qed_chain_get_cons_idx(&fp->rxq->rx_comp_ring) >
			    RX_RING_SIZE - 100)
				etlv->num_rxqs_full++;
		}
	}
	__qede_unlock(edev);

	etlv->txqs_empty_set = true;
	etlv->rxqs_empty_set = true;
	etlv->num_txqs_full_set = true;
	etlv->num_rxqs_full_set = true;
}
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799

/**
 * qede_io_error_detected - called when PCI error is detected
 * @pdev: Pointer to PCI device
 * @state: The current pci connection state
 *
 * This function is called after a PCI bus error affecting
 * this device has been detected.
 */
static pci_ers_result_t
qede_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
{
	struct net_device *dev = pci_get_drvdata(pdev);
	struct qede_dev *edev = netdev_priv(dev);

	if (!edev)
		return PCI_ERS_RESULT_NONE;

	DP_NOTICE(edev, "IO error detected [%d]\n", state);

	__qede_lock(edev);
	if (edev->state == QEDE_STATE_RECOVERY) {
		DP_NOTICE(edev, "Device already in the recovery state\n");
		__qede_unlock(edev);
		return PCI_ERS_RESULT_NONE;
	}

	/* PF handles the recovery of its VFs */
	if (IS_VF(edev)) {
		DP_VERBOSE(edev, QED_MSG_IOV,
			   "VF recovery is handled by its PF\n");
		__qede_unlock(edev);
		return PCI_ERS_RESULT_RECOVERED;
	}

	/* Close OS Tx */
	netif_tx_disable(edev->ndev);
	netif_carrier_off(edev->ndev);

	set_bit(QEDE_SP_AER, &edev->sp_flags);
	schedule_delayed_work(&edev->sp_task, 0);

	__qede_unlock(edev);

	return PCI_ERS_RESULT_CAN_RECOVER;
}