qede_main.c 51.7 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.
 */
Y
Yuval Mintz 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#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>
50
#include <net/udp_tunnel.h>
Y
Yuval Mintz 已提交
51 52 53 54 55 56 57 58 59 60 61
#include <linux/ip.h>
#include <net/ipv6.h>
#include <net/tcp.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/pkt_sched.h>
#include <linux/ethtool.h>
#include <linux/in.h>
#include <linux/random.h>
#include <net/ip6_checksum.h>
#include <linux/bitops.h>
R
Ram Amrani 已提交
62
#include <linux/qed/qede_roce.h>
Y
Yuval Mintz 已提交
63 64
#include "qede.h"

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

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

#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 已提交
93
#define PCI_DEVICE_ID_57980S_IOV	CHIP_NUM_57980S_IOV
Y
Yuval Mintz 已提交
94 95
#endif

Y
Yuval Mintz 已提交
96 97 98 99 100
enum qede_pci_private {
	QEDE_PRIVATE_PF,
	QEDE_PRIVATE_VF
};

Y
Yuval Mintz 已提交
101
static const struct pci_device_id qede_pci_tbl[] = {
Y
Yuval Mintz 已提交
102 103 104 105 106 107
	{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},
108
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
109
	{PCI_VDEVICE(QLOGIC, PCI_DEVICE_ID_57980S_IOV), QEDE_PRIVATE_VF},
110
#endif
Y
Yuval Mintz 已提交
111 112 113 114 115 116 117 118 119
	{ 0 }
};

MODULE_DEVICE_TABLE(pci, qede_pci_tbl);

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

#define TX_TIMEOUT		(5 * HZ)

M
Mintz, Yuval 已提交
120 121 122
/* Utilize last protocol index for XDP */
#define XDP_PI	11

Y
Yuval Mintz 已提交
123
static void qede_remove(struct pci_dev *pdev);
M
Mintz, Yuval 已提交
124
static void qede_shutdown(struct pci_dev *pdev);
S
Sudarsana Kalluru 已提交
125
static void qede_link_update(void *dev, struct qed_link_output *link);
Y
Yuval Mintz 已提交
126

M
Mintz, Yuval 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139
/* The qede lock is used to protect driver state change and driver flows that
 * are not reentrant.
 */
void __qede_lock(struct qede_dev *edev)
{
	mutex_lock(&edev->qede_lock);
}

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

Y
Yuval Mintz 已提交
140
#ifdef CONFIG_QED_SRIOV
141 142
static int qede_set_vf_vlan(struct net_device *ndev, int vf, u16 vlan, u8 qos,
			    __be16 vlan_proto)
Y
Yuval Mintz 已提交
143 144 145 146 147 148 149 150
{
	struct qede_dev *edev = netdev_priv(ndev);

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

151 152 153
	if (vlan_proto != htons(ETH_P_8021Q))
		return -EPROTONOSUPPORT;

Y
Yuval Mintz 已提交
154 155 156 157 158 159
	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 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
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 已提交
176 177 178
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 已提交
179 180
	struct qed_dev_info *qed_info = &edev->dev_info.common;
	int rc;
Y
Yuval Mintz 已提交
181 182 183

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

Y
Yuval Mintz 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	rc = edev->ops->iov->configure(edev->cdev, num_vfs_param);

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

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

	return rc;
Y
Yuval Mintz 已提交
199 200 201
}
#endif

Y
Yuval Mintz 已提交
202 203 204 205 206
static struct pci_driver qede_pci_driver = {
	.name = "qede",
	.id_table = qede_pci_tbl,
	.probe = qede_probe,
	.remove = qede_remove,
M
Mintz, Yuval 已提交
207
	.shutdown = qede_shutdown,
Y
Yuval Mintz 已提交
208 209 210
#ifdef CONFIG_QED_SRIOV
	.sriov_configure = qede_sriov_configure,
#endif
Y
Yuval Mintz 已提交
211 212
};

S
Sudarsana Kalluru 已提交
213 214 215 216
static struct qed_eth_cb_ops qede_ll_ops = {
	{
		.link_update = qede_link_update,
	},
Y
Yuval Mintz 已提交
217
	.force_mac = qede_force_mac,
S
Sudarsana Kalluru 已提交
218 219
};

220 221 222 223 224 225 226
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 已提交
227
	if (event != NETDEV_CHANGENAME && event != NETDEV_CHANGEADDR)
228 229 230 231 232 233 234 235 236 237 238 239
		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 已提交
240 241 242 243 244 245 246 247 248 249 250 251
	switch (event) {
	case NETDEV_CHANGENAME:
		/* Notify qed of the name change */
		if (!edev->ops || !edev->ops->common)
			goto done;
		edev->ops->common->set_id(edev->cdev, edev->ndev->name, "qede");
		break;
	case NETDEV_CHANGEADDR:
		edev = netdev_priv(ndev);
		qede_roce_event_changeaddr(edev);
		break;
	}
252 253 254 255 256 257 258 259 260

done:
	return NOTIFY_DONE;
}

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

Y
Yuval Mintz 已提交
261 262 263 264 265
static
int __init qede_init(void)
{
	int ret;

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

R
Rahul Verma 已提交
268
	qed_ops = qed_get_eth_ops();
Y
Yuval Mintz 已提交
269 270 271 272 273
	if (!qed_ops) {
		pr_notice("Failed to get qed ethtool operations\n");
		return -EINVAL;
	}

274 275 276 277 278 279 280 281 282 283
	/* Must register notifier before pci ops, since we might miss
	 * interface rename after pci probe and netdev registeration.
	 */
	ret = register_netdevice_notifier(&qede_netdev_notifier);
	if (ret) {
		pr_notice("Failed to register netdevice_notifier\n");
		qed_put_eth_ops();
		return -EINVAL;
	}

Y
Yuval Mintz 已提交
284 285 286
	ret = pci_register_driver(&qede_pci_driver);
	if (ret) {
		pr_notice("Failed to register driver\n");
287
		unregister_netdevice_notifier(&qede_netdev_notifier);
Y
Yuval Mintz 已提交
288 289 290 291 292 293 294 295 296
		qed_put_eth_ops();
		return -EINVAL;
	}

	return 0;
}

static void __exit qede_cleanup(void)
{
Y
Yuval Mintz 已提交
297 298
	if (debug & QED_LOG_INFO_MASK)
		pr_info("qede_cleanup called\n");
Y
Yuval Mintz 已提交
299

300
	unregister_netdevice_notifier(&qede_netdev_notifier);
Y
Yuval Mintz 已提交
301 302 303 304 305 306 307
	pci_unregister_driver(&qede_pci_driver);
	qed_put_eth_ops();
}

module_init(qede_init);
module_exit(qede_cleanup);

308 309
static int qede_open(struct net_device *ndev);
static int qede_close(struct net_device *ndev);
310

311 312 313 314 315 316
void qede_fill_by_demand_stats(struct qede_dev *edev)
{
	struct qed_eth_stats stats;

	edev->ops->get_vport_stats(edev->cdev, &stats);
	edev->stats.no_buff_discards = stats.no_buff_discards;
317 318
	edev->stats.packet_too_big_discard = stats.packet_too_big_discard;
	edev->stats.ttl0_discard = stats.ttl0_discard;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	edev->stats.rx_ucast_bytes = stats.rx_ucast_bytes;
	edev->stats.rx_mcast_bytes = stats.rx_mcast_bytes;
	edev->stats.rx_bcast_bytes = stats.rx_bcast_bytes;
	edev->stats.rx_ucast_pkts = stats.rx_ucast_pkts;
	edev->stats.rx_mcast_pkts = stats.rx_mcast_pkts;
	edev->stats.rx_bcast_pkts = stats.rx_bcast_pkts;
	edev->stats.mftag_filter_discards = stats.mftag_filter_discards;
	edev->stats.mac_filter_discards = stats.mac_filter_discards;

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

	edev->stats.rx_64_byte_packets = stats.rx_64_byte_packets;
Y
Yuval Mintz 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	edev->stats.rx_65_to_127_byte_packets = stats.rx_65_to_127_byte_packets;
	edev->stats.rx_128_to_255_byte_packets =
				stats.rx_128_to_255_byte_packets;
	edev->stats.rx_256_to_511_byte_packets =
				stats.rx_256_to_511_byte_packets;
	edev->stats.rx_512_to_1023_byte_packets =
				stats.rx_512_to_1023_byte_packets;
	edev->stats.rx_1024_to_1518_byte_packets =
				stats.rx_1024_to_1518_byte_packets;
	edev->stats.rx_1519_to_1522_byte_packets =
				stats.rx_1519_to_1522_byte_packets;
	edev->stats.rx_1519_to_2047_byte_packets =
				stats.rx_1519_to_2047_byte_packets;
	edev->stats.rx_2048_to_4095_byte_packets =
				stats.rx_2048_to_4095_byte_packets;
	edev->stats.rx_4096_to_9216_byte_packets =
				stats.rx_4096_to_9216_byte_packets;
	edev->stats.rx_9217_to_16383_byte_packets =
				stats.rx_9217_to_16383_byte_packets;
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
	edev->stats.rx_crc_errors = stats.rx_crc_errors;
	edev->stats.rx_mac_crtl_frames = stats.rx_mac_crtl_frames;
	edev->stats.rx_pause_frames = stats.rx_pause_frames;
	edev->stats.rx_pfc_frames = stats.rx_pfc_frames;
	edev->stats.rx_align_errors = stats.rx_align_errors;
	edev->stats.rx_carrier_errors = stats.rx_carrier_errors;
	edev->stats.rx_oversize_packets = stats.rx_oversize_packets;
	edev->stats.rx_jabbers = stats.rx_jabbers;
	edev->stats.rx_undersize_packets = stats.rx_undersize_packets;
	edev->stats.rx_fragments = stats.rx_fragments;
	edev->stats.tx_64_byte_packets = stats.tx_64_byte_packets;
	edev->stats.tx_65_to_127_byte_packets = stats.tx_65_to_127_byte_packets;
	edev->stats.tx_128_to_255_byte_packets =
				stats.tx_128_to_255_byte_packets;
	edev->stats.tx_256_to_511_byte_packets =
				stats.tx_256_to_511_byte_packets;
	edev->stats.tx_512_to_1023_byte_packets =
				stats.tx_512_to_1023_byte_packets;
	edev->stats.tx_1024_to_1518_byte_packets =
				stats.tx_1024_to_1518_byte_packets;
	edev->stats.tx_1519_to_2047_byte_packets =
				stats.tx_1519_to_2047_byte_packets;
	edev->stats.tx_2048_to_4095_byte_packets =
				stats.tx_2048_to_4095_byte_packets;
	edev->stats.tx_4096_to_9216_byte_packets =
				stats.tx_4096_to_9216_byte_packets;
	edev->stats.tx_9217_to_16383_byte_packets =
				stats.tx_9217_to_16383_byte_packets;
	edev->stats.tx_pause_frames = stats.tx_pause_frames;
	edev->stats.tx_pfc_frames = stats.tx_pfc_frames;
	edev->stats.tx_lpi_entry_count = stats.tx_lpi_entry_count;
	edev->stats.tx_total_collisions = stats.tx_total_collisions;
	edev->stats.brb_truncates = stats.brb_truncates;
	edev->stats.brb_discards = stats.brb_discards;
	edev->stats.tx_mac_ctrl_frames = stats.tx_mac_ctrl_frames;
}

Y
Yuval Mintz 已提交
398 399 400
static
struct rtnl_link_stats64 *qede_get_stats64(struct net_device *dev,
					   struct rtnl_link_stats64 *stats)
401 402 403 404 405 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
{
	struct qede_dev *edev = netdev_priv(dev);

	qede_fill_by_demand_stats(edev);

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

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

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

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

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

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

	return stats;
}

Y
Yuval Mintz 已提交
434
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
435 436 437 438 439 440 441 442 443 444 445
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 已提交
446 447 448 449 450
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 已提交
451
	return edev->ops->iov->set_rate(edev->cdev, vfidx, min_tx_rate,
Y
Yuval Mintz 已提交
452 453 454
					max_tx_rate);
}

Y
Yuval Mintz 已提交
455 456 457 458 459 460 461 462 463 464
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 已提交
465 466 467 468 469 470 471 472 473 474 475 476
static int qede_set_vf_link_state(struct net_device *dev, int vfidx,
				  int link_state)
{
	struct qede_dev *edev = netdev_priv(dev);

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

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

477 478 479 480
static const struct net_device_ops qede_netdev_ops = {
	.ndo_open = qede_open,
	.ndo_stop = qede_close,
	.ndo_start_xmit = qede_start_xmit,
481 482
	.ndo_set_rx_mode = qede_set_rx_mode,
	.ndo_set_mac_address = qede_set_mac_addr,
483
	.ndo_validate_addr = eth_validate_addr,
484
	.ndo_change_mtu = qede_change_mtu,
Y
Yuval Mintz 已提交
485
#ifdef CONFIG_QED_SRIOV
Y
Yuval Mintz 已提交
486
	.ndo_set_vf_mac = qede_set_vf_mac,
Y
Yuval Mintz 已提交
487 488
	.ndo_set_vf_vlan = qede_set_vf_vlan,
#endif
489 490
	.ndo_vlan_rx_add_vid = qede_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid = qede_vlan_rx_kill_vid,
Y
Yuval Mintz 已提交
491
	.ndo_set_features = qede_set_features,
492
	.ndo_get_stats64 = qede_get_stats64,
Y
Yuval Mintz 已提交
493 494
#ifdef CONFIG_QED_SRIOV
	.ndo_set_vf_link_state = qede_set_vf_link_state,
Y
Yuval Mintz 已提交
495
	.ndo_set_vf_spoofchk = qede_set_vf_spoofchk,
Y
Yuval Mintz 已提交
496
	.ndo_get_vf_config = qede_get_vf_config,
Y
Yuval Mintz 已提交
497 498
	.ndo_set_vf_rate = qede_set_vf_rate,
#endif
499 500
	.ndo_udp_tunnel_add = qede_udp_tunnel_add,
	.ndo_udp_tunnel_del = qede_udp_tunnel_del,
501
	.ndo_features_check = qede_features_check,
M
Mintz, Yuval 已提交
502
	.ndo_xdp = qede_xdp,
503 504
};

Y
Yuval Mintz 已提交
505 506 507 508 509 510 511 512
/* -------------------------------------------------------------------------
 * 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 已提交
513
					    u32 dp_module, u8 dp_level)
Y
Yuval Mintz 已提交
514 515 516 517 518
{
	struct net_device *ndev;
	struct qede_dev *edev;

	ndev = alloc_etherdev_mqs(sizeof(*edev),
Y
Yuval Mintz 已提交
519
				  info->num_queues, info->num_queues);
Y
Yuval Mintz 已提交
520 521 522 523 524 525 526 527 528 529 530 531
	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;
532 533
	edev->q_num_rx_buffers = NUM_RX_BDS_DEF;
	edev->q_num_tx_buffers = NUM_TX_BDS_DEF;
Y
Yuval Mintz 已提交
534

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

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

540
	memset(&edev->stats, 0, sizeof(edev->stats));
Y
Yuval Mintz 已提交
541 542
	memcpy(&edev->dev_info, info, sizeof(*info));

543 544
	INIT_LIST_HEAD(&edev->vlan_list);

Y
Yuval Mintz 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
	return edev;
}

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

	pci_set_drvdata(pdev, ndev);

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

	ndev->watchdog_timeo = TX_TIMEOUT;

563 564
	ndev->netdev_ops = &qede_netdev_ops;

565 566
	qede_set_ethtool_ops(ndev);

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

Y
Yuval Mintz 已提交
569 570 571 572 573
	/* user-changeble features */
	hw_features = NETIF_F_GRO | NETIF_F_SG |
		      NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
		      NETIF_F_TSO | NETIF_F_TSO6;

574 575
	/* Encap features*/
	hw_features |= NETIF_F_GSO_GRE | NETIF_F_GSO_UDP_TUNNEL |
576 577
		       NETIF_F_TSO_ECN | NETIF_F_GSO_UDP_TUNNEL_CSUM |
		       NETIF_F_GSO_GRE_CSUM;
578 579 580
	ndev->hw_enc_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
				NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO_ECN |
				NETIF_F_TSO6 | NETIF_F_GSO_GRE |
581 582 583
				NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RXCSUM |
				NETIF_F_GSO_UDP_TUNNEL_CSUM |
				NETIF_F_GSO_GRE_CSUM;
584

Y
Yuval Mintz 已提交
585 586 587 588
	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 |
589
			 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_TX;
Y
Yuval Mintz 已提交
590 591 592

	ndev->hw_features = hw_features;

593 594 595 596
	/* MTU range: 46 - 9600 */
	ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
	ndev->max_mtu = QEDE_MAX_JUMBO_PACKET_SIZE;

Y
Yuval Mintz 已提交
597 598
	/* Set network device HW mac */
	ether_addr_copy(edev->ndev->dev_addr, edev->dev_info.common.hw_mac);
599 600

	ndev->mtu = edev->dev_info.common.mtu;
Y
Yuval Mintz 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613
}

/* 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.
 */
614
void qede_config_debug(uint debug, u32 *p_dp_module, u8 *p_dp_level)
Y
Yuval Mintz 已提交
615 616 617 618 619 620 621 622 623 624 625 626 627 628
{
	*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;
	}
}

629 630 631 632 633 634
static void qede_free_fp_array(struct qede_dev *edev)
{
	if (edev->fp_array) {
		struct qede_fastpath *fp;
		int i;

635
		for_each_queue(i) {
636 637 638 639
			fp = &edev->fp_array[i];

			kfree(fp->sb_info);
			kfree(fp->rxq);
M
Mintz, Yuval 已提交
640
			kfree(fp->xdp_tx);
M
Mintz, Yuval 已提交
641
			kfree(fp->txq);
642 643 644
		}
		kfree(edev->fp_array);
	}
645 646 647 648

	edev->num_queues = 0;
	edev->fp_num_tx = 0;
	edev->fp_num_rx = 0;
649 650 651 652
}

static int qede_alloc_fp_array(struct qede_dev *edev)
{
653
	u8 fp_combined, fp_rx = edev->fp_num_rx;
654 655 656
	struct qede_fastpath *fp;
	int i;

657
	edev->fp_array = kcalloc(QEDE_QUEUE_CNT(edev),
658 659 660 661 662 663
				 sizeof(*edev->fp_array), GFP_KERNEL);
	if (!edev->fp_array) {
		DP_NOTICE(edev, "fp array allocation failed\n");
		goto err;
	}

664 665 666 667 668 669 670 671
	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) {
672 673
		fp = &edev->fp_array[i];

M
Mintz, Yuval 已提交
674
		fp->sb_info = kzalloc(sizeof(*fp->sb_info), GFP_KERNEL);
675 676 677 678 679
		if (!fp->sb_info) {
			DP_NOTICE(edev, "sb info struct allocation failed\n");
			goto err;
		}

680 681 682 683 684 685 686 687
		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;
688 689
		}

690
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
691 692
			fp->txq = kzalloc(sizeof(*fp->txq), GFP_KERNEL);
			if (!fp->txq)
693 694 695 696
				goto err;
		}

		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
697 698
			fp->rxq = kzalloc(sizeof(*fp->rxq), GFP_KERNEL);
			if (!fp->rxq)
699
				goto err;
M
Mintz, Yuval 已提交
700

M
Mintz, Yuval 已提交
701 702 703 704 705
			if (edev->xdp_prog) {
				fp->xdp_tx = kzalloc(sizeof(*fp->xdp_tx),
						     GFP_KERNEL);
				if (!fp->xdp_tx)
					goto err;
M
Mintz, Yuval 已提交
706
				fp->type |= QEDE_FASTPATH_XDP;
M
Mintz, Yuval 已提交
707
			}
708 709 710 711 712 713 714 715 716
		}
	}

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

717 718 719 720
static void qede_sp_task(struct work_struct *work)
{
	struct qede_dev *edev = container_of(work, struct qede_dev,
					     sp_task.work);
721 722
	struct qed_dev *cdev = edev->cdev;

M
Mintz, Yuval 已提交
723
	__qede_lock(edev);
724

M
Mintz, Yuval 已提交
725 726
	if (test_and_clear_bit(QEDE_SP_RX_MODE, &edev->sp_flags))
		if (edev->state == QEDE_STATE_OPEN)
727 728
			qede_config_rx_mode(edev->ndev);

729 730 731 732 733 734 735 736 737
	if (test_and_clear_bit(QEDE_SP_VXLAN_PORT_CONFIG, &edev->sp_flags)) {
		struct qed_tunn_params tunn_params;

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

738 739 740 741 742 743 744 745 746
	if (test_and_clear_bit(QEDE_SP_GENEVE_PORT_CONFIG, &edev->sp_flags)) {
		struct qed_tunn_params tunn_params;

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

M
Mintz, Yuval 已提交
747
	__qede_unlock(edev);
748 749
}

Y
Yuval Mintz 已提交
750 751 752 753
static void qede_update_pf_params(struct qed_dev *cdev)
{
	struct qed_pf_params pf_params;

M
Mintz, Yuval 已提交
754
	/* 64 rx + 64 tx + 64 XDP */
Y
Yuval Mintz 已提交
755
	memset(&pf_params, 0, sizeof(struct qed_pf_params));
M
Mintz, Yuval 已提交
756
	pf_params.eth_pf_params.num_cons = 192;
Y
Yuval Mintz 已提交
757 758 759 760 761 762 763 764
	qed_ops->common->update_pf_params(cdev, &pf_params);
}

enum qede_probe_mode {
	QEDE_PROBE_NORMAL,
};

static int __qede_probe(struct pci_dev *pdev, u32 dp_module, u8 dp_level,
Y
Yuval Mintz 已提交
765
			bool is_vf, enum qede_probe_mode mode)
Y
Yuval Mintz 已提交
766
{
Y
Yuval Mintz 已提交
767
	struct qed_probe_params probe_params;
Y
Yuval Mintz 已提交
768
	struct qed_slowpath_params sp_params;
Y
Yuval Mintz 已提交
769 770 771 772 773 774 775 776
	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 已提交
777 778 779 780 781 782
	memset(&probe_params, 0, sizeof(probe_params));
	probe_params.protocol = QED_PROTOCOL_ETH;
	probe_params.dp_module = dp_module;
	probe_params.dp_level = dp_level;
	probe_params.is_vf = is_vf;
	cdev = qed_ops->common->probe(pdev, &probe_params);
Y
Yuval Mintz 已提交
783 784 785 786 787 788 789 790
	if (!cdev) {
		rc = -ENODEV;
		goto err0;
	}

	qede_update_pf_params(cdev);

	/* Start the Slowpath-process */
Y
Yuval Mintz 已提交
791 792 793 794 795 796 797 798
	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 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
	if (rc) {
		pr_notice("Cannot start slowpath\n");
		goto err1;
	}

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

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

Y
Yuval Mintz 已提交
816 817 818
	if (is_vf)
		edev->flags |= QEDE_FLAG_IS_VF;

Y
Yuval Mintz 已提交
819 820
	qede_init_ndev(edev);

R
Ram Amrani 已提交
821 822 823 824
	rc = qede_roce_dev_add(edev);
	if (rc)
		goto err3;

825 826 827
	rc = register_netdev(edev->ndev);
	if (rc) {
		DP_NOTICE(edev, "Cannot register net-device\n");
R
Ram Amrani 已提交
828
		goto err4;
829 830
	}

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

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

835
#ifdef CONFIG_DCB
836 837
	if (!IS_VF(edev))
		qede_set_dcbnl_ops(edev->ndev);
838 839
#endif

840 841
	INIT_DELAYED_WORK(&edev->sp_task, qede_sp_task);
	mutex_init(&edev->qede_lock);
842
	edev->rx_copybreak = QEDE_RX_HDR_SIZE;
843

Y
Yuval Mintz 已提交
844 845 846 847
	DP_INFO(edev, "Ending successfully qede probe\n");

	return 0;

R
Ram Amrani 已提交
848 849
err4:
	qede_roce_dev_remove(edev);
850 851
err3:
	free_netdev(edev->ndev);
Y
Yuval Mintz 已提交
852 853 854 855 856 857 858 859 860 861
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 已提交
862
	bool is_vf = false;
Y
Yuval Mintz 已提交
863 864 865
	u32 dp_module = 0;
	u8 dp_level = 0;

Y
Yuval Mintz 已提交
866 867 868 869 870 871 872 873 874 875 876
	switch ((enum qede_pci_private)id->driver_data) {
	case QEDE_PRIVATE_VF:
		if (debug & QED_LOG_VERBOSE_MASK)
			dev_err(&pdev->dev, "Probing a VF\n");
		is_vf = true;
		break;
	default:
		if (debug & QED_LOG_VERBOSE_MASK)
			dev_err(&pdev->dev, "Probing a PF\n");
	}

Y
Yuval Mintz 已提交
877 878
	qede_config_debug(debug, &dp_module, &dp_level);

Y
Yuval Mintz 已提交
879
	return __qede_probe(pdev, dp_module, dp_level, is_vf,
Y
Yuval Mintz 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
			    QEDE_PROBE_NORMAL);
}

enum qede_remove_mode {
	QEDE_REMOVE_NORMAL,
};

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

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

895
	cancel_delayed_work_sync(&edev->sp_task);
R
Ram Amrani 已提交
896

897 898
	unregister_netdev(ndev);

R
Ram Amrani 已提交
899 900
	qede_roce_dev_remove(edev);

Y
Yuval Mintz 已提交
901 902 903 904
	edev->ops->common->set_power_state(cdev, PCI_D0);

	pci_set_drvdata(pdev, NULL);

M
Mintz, Yuval 已提交
905 906 907 908
	/* Release edev's reference to XDP's bpf if such exist */
	if (edev->xdp_prog)
		bpf_prog_put(edev->xdp_prog);

Y
Yuval Mintz 已提交
909 910 911 912
	free_netdev(ndev);

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

Y
Yuval Mintz 已提交
917
	dev_info(&pdev->dev, "Ending qede_remove successfully\n");
Y
Yuval Mintz 已提交
918 919 920 921 922 923
}

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

M
Mintz, Yuval 已提交
925 926 927 928 929
static void qede_shutdown(struct pci_dev *pdev)
{
	__qede_remove(pdev, QEDE_REMOVE_NORMAL);
}

930 931 932 933 934 935 936 937 938 939 940
/* -------------------------------------------------------------------------
 * 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*/
941 942
	if (edev->req_queues)
		rss_num = edev->req_queues;
943 944 945
	else
		rss_num = netif_get_num_default_rss_queues() *
			  edev->dev_info.common.num_hwfns;
946 947 948 949 950 951

	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 */
952
		edev->num_queues = rc;
953
		DP_INFO(edev, "Managed %d [of %d] RSS queues\n",
954
			QEDE_QUEUE_CNT(edev), rss_num);
955 956
		rc = 0;
	}
957 958 959 960

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

961 962 963 964 965 966 967 968 969 970 971 972 973
	return rc;
}

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

/* This function allocates fast-path status block memory */
static int qede_alloc_mem_sb(struct qede_dev *edev,
Y
Yuval Mintz 已提交
974
			     struct qed_sb_info *sb_info, u16 sb_id)
975 976 977 978 979 980
{
	struct status_block *sb_virt;
	dma_addr_t sb_phys;
	int rc;

	sb_virt = dma_alloc_coherent(&edev->pdev->dev,
Y
Yuval Mintz 已提交
981
				     sizeof(*sb_virt), &sb_phys, GFP_KERNEL);
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	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 已提交
1007
		struct page *data;
1008 1009 1010 1011

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

Y
Yuval Mintz 已提交
1012
		dma_unmap_page(&edev->pdev->dev,
M
Mintz, Yuval 已提交
1013
			       rx_buf->mapping, PAGE_SIZE, rxq->data_direction);
1014 1015

		rx_buf->data = NULL;
Y
Yuval Mintz 已提交
1016
		__free_page(data);
1017 1018 1019
	}
}

Y
Yuval Mintz 已提交
1020 1021
static void qede_free_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
{
1022 1023 1024 1025 1026 1027 1028
	int i;

	if (edev->gro_disable)
		return;

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

1031
		if (replace_buf->data) {
1032
			dma_unmap_page(&edev->pdev->dev,
M
Manish Chopra 已提交
1033
				       replace_buf->mapping,
1034 1035 1036 1037 1038 1039
				       PAGE_SIZE, DMA_FROM_DEVICE);
			__free_page(replace_buf->data);
		}
	}
}

Y
Yuval Mintz 已提交
1040
static void qede_free_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
1041
{
1042 1043
	qede_free_sge_mem(edev, rxq);

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	/* Free rx buffers */
	qede_free_rx_buffers(edev, rxq);

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

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

Y
Yuval Mintz 已提交
1055
static int qede_alloc_sge_mem(struct qede_dev *edev, struct qede_rx_queue *rxq)
1056 1057 1058 1059
{
	dma_addr_t mapping;
	int i;

M
Mintz, Yuval 已提交
1060 1061 1062 1063
	/* Don't perform FW aggregations in case of XDP */
	if (edev->xdp_prog)
		edev->gro_disable = 1;

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
	if (edev->gro_disable)
		return 0;

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

	for (i = 0; i < ETH_TPA_MAX_AGGS_NUM; i++) {
		struct qede_agg_info *tpa_info = &rxq->tpa_info[i];
1074
		struct sw_rx_data *replace_buf = &tpa_info->buffer;
1075 1076 1077 1078 1079 1080 1081 1082 1083

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

		mapping = dma_map_page(&edev->pdev->dev, replace_buf->data, 0,
1084
				       PAGE_SIZE, DMA_FROM_DEVICE);
1085 1086 1087 1088 1089 1090
		if (unlikely(dma_mapping_error(&edev->pdev->dev, mapping))) {
			DP_NOTICE(edev,
				  "Failed to map TPA replacement buffer\n");
			goto err;
		}

M
Manish Chopra 已提交
1091
		replace_buf->mapping = mapping;
1092 1093 1094
		tpa_info->buffer.page_offset = 0;
		tpa_info->buffer_mapping = mapping;
		tpa_info->state = QEDE_AGG_STATE_NONE;
1095 1096 1097 1098 1099 1100 1101 1102 1103
	}

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

1104
/* This function allocates all memory needed per Rx queue */
Y
Yuval Mintz 已提交
1105
static int qede_alloc_mem_rxq(struct qede_dev *edev, struct qede_rx_queue *rxq)
1106
{
1107
	int i, rc, size;
1108 1109 1110

	rxq->num_rx_buffers = edev->q_num_rx_buffers;

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

Y
Yuval Mintz 已提交
1113 1114 1115
	if (rxq->rx_buf_size > PAGE_SIZE)
		rxq->rx_buf_size = PAGE_SIZE;

M
Mintz, Yuval 已提交
1116 1117 1118 1119 1120 1121 1122
	/* Segment size to spilt a page in multiple equal parts,
	 * unless XDP is used in which case we'd use the entire page.
	 */
	if (!edev->xdp_prog)
		rxq->rx_buf_seg_size = roundup_pow_of_two(rxq->rx_buf_size);
	else
		rxq->rx_buf_seg_size = PAGE_SIZE;
1123 1124

	/* Allocate the parallel driver ring for Rx buffers */
Y
Yuval Mintz 已提交
1125
	size = sizeof(*rxq->sw_rx_ring) * RX_RING_SIZE;
1126 1127 1128
	rxq->sw_rx_ring = kzalloc(size, GFP_KERNEL);
	if (!rxq->sw_rx_ring) {
		DP_ERR(edev, "Rx buffers ring allocation failed\n");
1129
		rc = -ENOMEM;
1130 1131 1132 1133 1134 1135 1136
		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 已提交
1137
					    QED_CHAIN_CNT_TYPE_U16,
Y
Yuval Mintz 已提交
1138
					    RX_RING_SIZE,
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
					    sizeof(struct eth_rx_bd),
					    &rxq->rx_bd_ring);

	if (rc)
		goto err;

	/* Allocate FW completion ring */
	rc = edev->ops->common->chain_alloc(edev->cdev,
					    QED_CHAIN_USE_TO_CONSUME,
					    QED_CHAIN_MODE_PBL,
Y
Yuval Mintz 已提交
1149
					    QED_CHAIN_CNT_TYPE_U16,
Y
Yuval Mintz 已提交
1150
					    RX_RING_SIZE,
1151 1152 1153 1154 1155 1156 1157
					    sizeof(union eth_rx_cqe),
					    &rxq->rx_comp_ring);
	if (rc)
		goto err;

	/* Allocate buffers for the Rx ring */
	for (i = 0; i < rxq->num_rx_buffers; i++) {
1158
		rc = qede_alloc_rx_buffer(rxq);
1159 1160 1161 1162 1163
		if (rc) {
			DP_ERR(edev,
			       "Rx buffers allocation failed at index %d\n", i);
			goto err;
		}
1164 1165
	}

1166
	rc = qede_alloc_sge_mem(edev, rxq);
1167
err:
1168
	return rc;
1169 1170
}

Y
Yuval Mintz 已提交
1171
static void qede_free_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
1172 1173
{
	/* Free the parallel SW ring */
M
Mintz, Yuval 已提交
1174 1175 1176 1177
	if (txq->is_xdp)
		kfree(txq->sw_tx_ring.pages);
	else
		kfree(txq->sw_tx_ring.skbs);
1178 1179 1180 1181 1182 1183

	/* 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 已提交
1184
static int qede_alloc_mem_txq(struct qede_dev *edev, struct qede_tx_queue *txq)
1185 1186
{
	union eth_tx_bd_types *p_virt;
M
Mintz, Yuval 已提交
1187
	int size, rc;
1188 1189 1190 1191

	txq->num_tx_buffers = edev->q_num_tx_buffers;

	/* Allocate the parallel driver ring for Tx buffers */
M
Mintz, Yuval 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	if (txq->is_xdp) {
		size = sizeof(*txq->sw_tx_ring.pages) * TX_RING_SIZE;
		txq->sw_tx_ring.pages = kzalloc(size, GFP_KERNEL);
		if (!txq->sw_tx_ring.pages)
			goto err;
	} else {
		size = sizeof(*txq->sw_tx_ring.skbs) * TX_RING_SIZE;
		txq->sw_tx_ring.skbs = kzalloc(size, GFP_KERNEL);
		if (!txq->sw_tx_ring.skbs)
			goto err;
1202 1203 1204 1205 1206
	}

	rc = edev->ops->common->chain_alloc(edev->cdev,
					    QED_CHAIN_USE_TO_CONSUME_PRODUCE,
					    QED_CHAIN_MODE_PBL,
Y
Yuval Mintz 已提交
1207
					    QED_CHAIN_CNT_TYPE_U16,
1208
					    TX_RING_SIZE,
Y
Yuval Mintz 已提交
1209
					    sizeof(*p_virt), &txq->tx_pbl);
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
	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 已提交
1221
static void qede_free_mem_fp(struct qede_dev *edev, struct qede_fastpath *fp)
1222 1223 1224
{
	qede_free_mem_sb(edev, fp->sb_info);

1225 1226
	if (fp->type & QEDE_FASTPATH_RX)
		qede_free_mem_rxq(edev, fp->rxq);
1227

1228
	if (fp->type & QEDE_FASTPATH_TX)
M
Mintz, Yuval 已提交
1229
		qede_free_mem_txq(edev, fp->txq);
1230 1231 1232
}

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

1239
	rc = qede_alloc_mem_sb(edev, fp->sb_info, fp->id);
1240
	if (rc)
M
Mintz, Yuval 已提交
1241
		goto out;
1242

1243 1244
	if (fp->type & QEDE_FASTPATH_RX) {
		rc = qede_alloc_mem_rxq(edev, fp->rxq);
1245
		if (rc)
M
Mintz, Yuval 已提交
1246 1247 1248 1249 1250 1251 1252
			goto out;
	}

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

1255
	if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
1256 1257
		rc = qede_alloc_mem_txq(edev, fp->txq);
		if (rc)
M
Mintz, Yuval 已提交
1258
			goto out;
1259 1260
	}

M
Mintz, Yuval 已提交
1261
out:
1262
	return rc;
1263 1264 1265 1266 1267 1268
}

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

1269
	for_each_queue(i) {
1270 1271 1272 1273 1274 1275 1276 1277 1278
		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)
{
1279
	int rc = 0, queue_id;
1280

1281 1282
	for (queue_id = 0; queue_id < QEDE_QUEUE_CNT(edev); queue_id++) {
		struct qede_fastpath *fp = &edev->fp_array[queue_id];
1283 1284

		rc = qede_alloc_mem_fp(edev, fp);
1285
		if (rc) {
1286
			DP_ERR(edev,
1287
			       "Failed to allocate memory for fastpath - rss id = %d\n",
1288
			       queue_id);
1289 1290
			qede_free_mem_load(edev);
			return rc;
1291 1292 1293 1294 1295 1296 1297 1298 1299
		}
	}

	return 0;
}

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

1303 1304
	for_each_queue(queue_id) {
		fp = &edev->fp_array[queue_id];
1305 1306

		fp->edev = edev;
1307
		fp->id = queue_id;
1308

M
Mintz, Yuval 已提交
1309 1310 1311 1312 1313
		if (fp->type & QEDE_FASTPATH_XDP) {
			fp->xdp_tx->index = QEDE_TXQ_IDX_TO_XDP(edev,
								rxq_index);
			fp->xdp_tx->is_xdp = 1;
		}
1314

1315 1316
		if (fp->type & QEDE_FASTPATH_RX) {
			fp->rxq->rxq_id = rxq_index++;
M
Mintz, Yuval 已提交
1317 1318 1319 1320 1321 1322

			/* 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;
1323
			fp->rxq->dev = &edev->pdev->dev;
1324
		}
1325

1326
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
1327 1328 1329
			fp->txq->index = txq_index++;
			if (edev->dev_info.is_legacy)
				fp->txq->is_legacy = 1;
1330
			fp->txq->dev = &edev->pdev->dev;
1331 1332 1333
		}

		snprintf(fp->name, sizeof(fp->name), "%s-fp-%d",
1334
			 edev->ndev->name, queue_id);
1335
	}
1336 1337

	edev->gro_disable = !(edev->ndev->features & NETIF_F_GRO);
1338 1339 1340 1341 1342 1343
}

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

1344
	rc = netif_set_real_num_tx_queues(edev->ndev, QEDE_TSS_COUNT(edev));
1345 1346 1347 1348
	if (rc) {
		DP_NOTICE(edev, "Failed to set real number of Tx queues\n");
		return rc;
	}
1349 1350

	rc = netif_set_real_num_rx_queues(edev->ndev, QEDE_RSS_COUNT(edev));
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
	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;

1363
	for_each_queue(i) {
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
		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 */
1375
	for_each_queue(i) {
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
		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 */
1404
	if (QEDE_QUEUE_CNT(edev) > edev->int_info.msix_cnt) {
1405 1406
		DP_ERR(edev,
		       "Interrupt mismatch: %d RSS queues > %d MSI-x vectors\n",
1407
		       QEDE_QUEUE_CNT(edev), edev->int_info.msix_cnt);
1408 1409 1410
		return -EINVAL;
	}

1411
	for (i = 0; i < QEDE_QUEUE_CNT(edev); i++) {
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
		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;
1456
		for (i = 0; i < QEDE_QUEUE_CNT(edev); i++)
1457 1458 1459
			ops->simd_handler_config(edev->cdev,
						 &edev->fp_array[i], i,
						 qede_simd_fp_handler);
1460
		edev->int_info.used_cnt = QEDE_QUEUE_CNT(edev);
1461 1462 1463 1464 1465
	}
	return 0;
}

static int qede_drain_txq(struct qede_dev *edev,
Y
Yuval Mintz 已提交
1466
			  struct qede_tx_queue *txq, bool allow_drain)
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
{
	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 已提交
1498 1499 1500 1501 1502 1503
static int qede_stop_txq(struct qede_dev *edev,
			 struct qede_tx_queue *txq, int rss_id)
{
	return edev->ops->q_tx_stop(edev->cdev, rss_id, txq->handle);
}

1504 1505 1506 1507
static int qede_stop_queues(struct qede_dev *edev)
{
	struct qed_update_vport_params vport_update_params;
	struct qed_dev *cdev = edev->cdev;
M
Mintz, Yuval 已提交
1508 1509
	struct qede_fastpath *fp;
	int rc, i;
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524

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

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

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

1528
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
1529 1530 1531
			rc = qede_drain_txq(edev, fp->txq, true);
			if (rc)
				return rc;
1532
		}
M
Mintz, Yuval 已提交
1533 1534 1535 1536 1537 1538

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

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

1545
		/* Stop the Tx Queue(s) */
M
Mintz, Yuval 已提交
1546
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
1547 1548 1549
			rc = qede_stop_txq(edev, fp->txq, i);
			if (rc)
				return rc;
1550 1551
		}

1552
		/* Stop the Rx Queue */
M
Mintz, Yuval 已提交
1553
		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
1554
			rc = edev->ops->q_rx_stop(cdev, i, fp->rxq->handle);
1555 1556 1557 1558
			if (rc) {
				DP_ERR(edev, "Failed to stop RXQ #%d\n", i);
				return rc;
			}
1559
		}
M
Mintz, Yuval 已提交
1560

M
Mintz, Yuval 已提交
1561 1562 1563 1564 1565 1566
		/* 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 已提交
1567
			bpf_prog_put(fp->rxq->xdp_prog);
M
Mintz, Yuval 已提交
1568
		}
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
	}

	/* 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 已提交
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
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 已提交
1592 1593 1594 1595 1596 1597 1598 1599
	/* Let the XDP queue share the queue-zone with one of the regular txq.
	 * We don't really care about its coalescing.
	 */
	if (txq->is_xdp)
		params.queue_id = QEDE_TXQ_XDP_TO_IDX(edev, txq);
	else
		params.queue_id = txq->index;

M
Mintz, Yuval 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	params.sb = fp->sb_info->igu_sb_id;
	params.sb_idx = sb_idx;

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

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

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

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

	return rc;
}

1626
static int qede_start_queues(struct qede_dev *edev, bool clear_stats)
1627
{
1628
	int vlan_removal_en = 1;
1629 1630 1631
	struct qed_dev *cdev = edev->cdev;
	struct qed_update_vport_params vport_update_params;
	struct qed_queue_start_common_params q_params;
Y
Yuval Mintz 已提交
1632
	struct qed_dev_info *qed_info = &edev->dev_info.common;
1633
	struct qed_start_vport_params start = {0};
1634
	bool reset_rss_indir = false;
M
Mintz, Yuval 已提交
1635
	int rc, i;
1636

1637
	if (!edev->num_queues) {
1638 1639 1640 1641 1642
		DP_ERR(edev,
		       "Cannot update V-VPORT as active as there are no Rx queues\n");
		return -EINVAL;
	}

1643
	start.gro_enable = !edev->gro_disable;
1644 1645 1646 1647
	start.mtu = edev->ndev->mtu;
	start.vport_id = 0;
	start.drop_ttl0 = true;
	start.remove_inner_vlan = vlan_removal_en;
1648
	start.clear_stats = clear_stats;
1649 1650

	rc = edev->ops->vport_start(cdev, &start);
1651 1652 1653 1654 1655 1656 1657 1658

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

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

1661
	for_each_queue(i) {
1662
		struct qede_fastpath *fp = &edev->fp_array[i];
1663 1664
		dma_addr_t p_phys_table;
		u32 page_cnt;
1665

1666
		if (fp->type & QEDE_FASTPATH_RX) {
M
Mintz, Yuval 已提交
1667
			struct qed_rxq_start_ret_params ret_params;
1668 1669
			struct qede_rx_queue *rxq = fp->rxq;
			__le16 *val;
1670

M
Mintz, Yuval 已提交
1671
			memset(&ret_params, 0, sizeof(ret_params));
1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
			memset(&q_params, 0, sizeof(q_params));
			q_params.queue_id = rxq->rxq_id;
			q_params.vport_id = 0;
			q_params.sb = fp->sb_info->igu_sb_id;
			q_params.sb_idx = RX_PI;

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

M
Mintz, Yuval 已提交
1682
			rc = edev->ops->q_rx_start(cdev, i, &q_params,
1683 1684 1685
						   rxq->rx_buf_size,
						   rxq->rx_bd_ring.p_phys_addr,
						   p_phys_table,
M
Mintz, Yuval 已提交
1686
						   page_cnt, &ret_params);
1687 1688 1689 1690 1691 1692
			if (rc) {
				DP_ERR(edev, "Start RXQ #%d failed %d\n", i,
				       rc);
				return rc;
			}

M
Mintz, Yuval 已提交
1693 1694 1695 1696
			/* Use the return parameters */
			rxq->hw_rxq_prod_addr = ret_params.p_prod;
			rxq->handle = ret_params.p_handle;

1697 1698 1699 1700 1701 1702
			val = &fp->sb_info->sb_virt->pi_array[RX_PI];
			rxq->hw_cons_ptr = val;

			qede_update_rx_prod(edev, rxq);
		}

M
Mintz, Yuval 已提交
1703
		if (fp->type & QEDE_FASTPATH_XDP) {
M
Mintz, Yuval 已提交
1704 1705 1706 1707
			rc = qede_start_txq(edev, fp, fp->xdp_tx, i, XDP_PI);
			if (rc)
				return rc;

M
Mintz, Yuval 已提交
1708 1709 1710 1711 1712 1713 1714 1715
			fp->rxq->xdp_prog = bpf_prog_add(edev->xdp_prog, 1);
			if (IS_ERR(fp->rxq->xdp_prog)) {
				rc = PTR_ERR(fp->rxq->xdp_prog);
				fp->rxq->xdp_prog = NULL;
				return rc;
			}
		}

M
Mintz, Yuval 已提交
1716
		if (fp->type & QEDE_FASTPATH_TX) {
M
Mintz, Yuval 已提交
1717 1718
			rc = qede_start_txq(edev, fp, fp->txq, i, TX_PI(0));
			if (rc)
1719 1720 1721 1722 1723 1724
				return rc;
		}
	}

	/* Prepare and send the vport enable */
	memset(&vport_update_params, 0, sizeof(vport_update_params));
1725
	vport_update_params.vport_id = start.vport_id;
1726 1727 1728
	vport_update_params.update_vport_active_flg = 1;
	vport_update_params.vport_active_flg = 1;

Y
Yuval Mintz 已提交
1729 1730 1731 1732 1733 1734
	if ((qed_info->mf_mode == QED_MF_NPAR || pci_num_vf(edev->pdev)) &&
	    qed_info->tx_switching) {
		vport_update_params.update_tx_switching_flg = 1;
		vport_update_params.tx_switching_flg = 1;
	}

1735
	/* Fill struct with RSS params */
1736
	if (QEDE_RSS_COUNT(edev) > 1) {
1737
		vport_update_params.update_rss_flg = 1;
1738 1739 1740 1741

		/* Need to validate current RSS config uses valid entries */
		for (i = 0; i < QED_RSS_IND_TABLE_SIZE; i++) {
			if (edev->rss_params.rss_ind_table[i] >=
1742
			    QEDE_RSS_COUNT(edev)) {
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
				reset_rss_indir = true;
				break;
			}
		}

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

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

1755
				val = QEDE_RSS_COUNT(edev);
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
				indir_val = ethtool_rxfh_indir_default(i, val);
				edev->rss_params.rss_ind_table[i] = indir_val;
			}
			edev->rss_params_inited |= QEDE_RSS_INDIR_INITED;
		}

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

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

		memcpy(&vport_update_params.rss_params, &edev->rss_params,
		       sizeof(vport_update_params.rss_params));
1778
	} else {
1779 1780
		memset(&vport_update_params.rss_params, 0,
		       sizeof(vport_update_params.rss_params));
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
	}

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

	return 0;
}

1792

1793 1794 1795 1796
enum qede_unload_mode {
	QEDE_UNLOAD_NORMAL,
};

M
Mintz, Yuval 已提交
1797 1798
static void qede_unload(struct qede_dev *edev, enum qede_unload_mode mode,
			bool is_locked)
1799
{
S
Sudarsana Kalluru 已提交
1800
	struct qed_link_params link_params;
1801 1802 1803 1804
	int rc;

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

M
Mintz, Yuval 已提交
1805 1806 1807
	if (!is_locked)
		__qede_lock(edev);

R
Ram Amrani 已提交
1808
	qede_roce_dev_event_close(edev);
1809 1810
	edev->state = QEDE_STATE_CLOSED;

1811 1812 1813 1814
	/* Close OS Tx */
	netif_tx_disable(edev->ndev);
	netif_carrier_off(edev->ndev);

S
Sudarsana Kalluru 已提交
1815 1816 1817 1818
	/* Reset the link */
	memset(&link_params, 0, sizeof(link_params));
	link_params.link_up = false;
	edev->ops->common->set_link(edev->cdev, &link_params);
1819 1820 1821 1822 1823 1824 1825 1826
	rc = qede_stop_queues(edev);
	if (rc) {
		qede_sync_free_irqs(edev);
		goto out;
	}

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

1827
	qede_vlan_mark_nonconfigured(edev);
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
	edev->ops->fastpath_stop(edev->cdev);

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

	qede_napi_disable_remove(edev);

	qede_free_mem_load(edev);
	qede_free_fp_array(edev);

out:
M
Mintz, Yuval 已提交
1840 1841
	if (!is_locked)
		__qede_unlock(edev);
1842 1843 1844 1845 1846
	DP_INFO(edev, "Ending qede unload\n");
}

enum qede_load_mode {
	QEDE_LOAD_NORMAL,
1847
	QEDE_LOAD_RELOAD,
1848 1849
};

M
Mintz, Yuval 已提交
1850 1851
static int qede_load(struct qede_dev *edev, enum qede_load_mode mode,
		     bool is_locked)
1852
{
S
Sudarsana Kalluru 已提交
1853 1854
	struct qed_link_params link_params;
	struct qed_link_output link_output;
1855 1856 1857 1858
	int rc;

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

M
Mintz, Yuval 已提交
1859 1860 1861
	if (!is_locked)
		__qede_lock(edev);

1862 1863
	rc = qede_set_num_queues(edev);
	if (rc)
M
Mintz, Yuval 已提交
1864
		goto out;
1865 1866 1867

	rc = qede_alloc_fp_array(edev);
	if (rc)
M
Mintz, Yuval 已提交
1868
		goto out;
1869 1870 1871 1872 1873 1874

	qede_init_fp(edev);

	rc = qede_alloc_mem_load(edev);
	if (rc)
		goto err1;
M
Mintz, Yuval 已提交
1875 1876
	DP_INFO(edev, "Allocated %d Rx, %d Tx queues\n",
		QEDE_RSS_COUNT(edev), QEDE_TSS_COUNT(edev));
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889

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

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

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

1890
	rc = qede_start_queues(edev, mode != QEDE_LOAD_RELOAD);
1891 1892 1893 1894 1895 1896 1897
	if (rc)
		goto err4;
	DP_INFO(edev, "Start VPORT, RXQ and TXQ succeeded\n");

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

1898 1899 1900
	/* Program un-configured VLANs */
	qede_configure_vlan_filters(edev);

S
Sudarsana Kalluru 已提交
1901 1902 1903 1904 1905 1906 1907 1908
	/* Ask for link-up using current configuration */
	memset(&link_params, 0, sizeof(link_params));
	link_params.link_up = true;
	edev->ops->common->set_link(edev->cdev, &link_params);

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

M
Mintz, Yuval 已提交
1912 1913
	edev->state = QEDE_STATE_OPEN;

1914 1915 1916
	DP_INFO(edev, "Ending successfully qede load\n");


M
Mintz, Yuval 已提交
1917
	goto out;
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
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);
1928 1929 1930
	edev->num_queues = 0;
	edev->fp_num_tx = 0;
	edev->fp_num_rx = 0;
M
Mintz, Yuval 已提交
1931 1932 1933 1934
out:
	if (!is_locked)
		__qede_unlock(edev);

1935 1936 1937
	return rc;
}

M
Mintz, Yuval 已提交
1938 1939 1940
/* 'func' should be able to run between unload and reload assuming interface
 * is actually running, or afterwards in case it's currently DOWN.
 */
1941
void qede_reload(struct qede_dev *edev,
M
Mintz, Yuval 已提交
1942
		 struct qede_reload_args *args, bool is_locked)
1943
{
M
Mintz, Yuval 已提交
1944 1945 1946 1947 1948 1949
	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.
1950
	 */
M
Mintz, Yuval 已提交
1951 1952 1953 1954 1955
	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);
1956

M
Mintz, Yuval 已提交
1957 1958 1959 1960 1961
		/* 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);
	}
1962

M
Mintz, Yuval 已提交
1963 1964
	if (!is_locked)
		__qede_unlock(edev);
1965 1966
}

1967 1968 1969 1970
/* called with rtnl_lock */
static int qede_open(struct net_device *ndev)
{
	struct qede_dev *edev = netdev_priv(ndev);
1971
	int rc;
1972 1973 1974 1975 1976

	netif_carrier_off(ndev);

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

M
Mintz, Yuval 已提交
1977
	rc = qede_load(edev, QEDE_LOAD_NORMAL, false);
1978 1979 1980
	if (rc)
		return rc;

1981 1982
	udp_tunnel_get_rx_info(ndev);

1983 1984
	edev->ops->common->update_drv_state(edev->cdev, true);

1985
	return 0;
1986 1987 1988 1989 1990 1991
}

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

M
Mintz, Yuval 已提交
1992
	qede_unload(edev, QEDE_UNLOAD_NORMAL, false);
1993

1994 1995
	edev->ops->common->update_drv_state(edev->cdev, false);

1996 1997
	return 0;
}
1998

S
Sudarsana Kalluru 已提交
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008
static void qede_link_update(void *dev, struct qed_link_output *link)
{
	struct qede_dev *edev = dev;

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

	if (link->link_up) {
2009 2010 2011 2012 2013
		if (!netif_carrier_ok(edev->ndev)) {
			DP_NOTICE(edev, "Link is up\n");
			netif_tx_start_all_queues(edev->ndev);
			netif_carrier_on(edev->ndev);
		}
S
Sudarsana Kalluru 已提交
2014
	} else {
2015 2016 2017 2018 2019
		if (netif_carrier_ok(edev->ndev)) {
			DP_NOTICE(edev, "Link is down\n");
			netif_tx_disable(edev->ndev);
			netif_carrier_off(edev->ndev);
		}
S
Sudarsana Kalluru 已提交
2020 2021
	}
}