ena_netdev.h 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright 2015 Amazon.com, Inc. or its affiliates.
 *
 * 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
 * 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.
 */

#ifndef ENA_H
#define ENA_H

#include <linux/bitops.h>
37
#include <linux/dim.h>
38
#include <linux/etherdevice.h>
39
#include <linux/if_vlan.h>
40 41 42 43 44 45 46 47
#include <linux/inetdevice.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/skbuff.h>

#include "ena_com.h"
#include "ena_eth_com.h"

48 49 50 51
#define DRV_MODULE_GEN_MAJOR	2
#define DRV_MODULE_GEN_MINOR	1
#define DRV_MODULE_GEN_SUBMINOR 0

52
#define DRV_MODULE_NAME		"ena"
53

54 55 56
#define DEVICE_NAME	"Elastic Network Adapter (ENA)"

/* 1 for AENQ + ADMIN */
57 58 59
#define ENA_ADMIN_MSIX_VEC		1
#define ENA_MAX_MSIX_VEC(io_queues)	(ENA_ADMIN_MSIX_VEC + (io_queues))

60 61 62 63 64 65
/* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the
 * driver passes 0.
 * Since the max packet size the ENA handles is ~9kB limit the buffer length to
 * 16kB.
 */
#if PAGE_SIZE > SZ_16K
66
#define ENA_PAGE_SIZE (_AC(SZ_16K, UL))
67 68 69 70
#else
#define ENA_PAGE_SIZE PAGE_SIZE
#endif

71
#define ENA_MIN_MSIX_VEC		2
72 73 74 75 76 77

#define ENA_REG_BAR			0
#define ENA_MEM_BAR			2
#define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR))

#define ENA_DEFAULT_RING_SIZE	(1024)
78 79
#define ENA_MIN_RING_SIZE	(256)

80 81
#define ENA_MIN_NUM_IO_QUEUES	(1)

82
#define ENA_TX_WAKEUP_THRESH		(MAX_SKB_FRAGS + 2)
83
#define ENA_DEFAULT_RX_COPYBREAK	(256 - NET_IP_ALIGN)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

/* limit the buffer size to 600 bytes to handle MTU changes from very
 * small to very large, in which case the number of buffers per packet
 * could exceed ENA_PKT_MAX_BUFS
 */
#define ENA_DEFAULT_MIN_RX_BUFF_ALLOC_SIZE 600

#define ENA_MIN_MTU		128

#define ENA_NAME_MAX_LEN	20
#define ENA_IRQNAME_SIZE	40

#define ENA_PKT_MAX_BUFS	19

#define ENA_RX_RSS_TABLE_LOG_SIZE  7
#define ENA_RX_RSS_TABLE_SIZE	(1 << ENA_RX_RSS_TABLE_LOG_SIZE)

/* The number of tx packet completions that will be handled each NAPI poll
 * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER.
 */
#define ENA_TX_POLL_BUDGET_DIVIDER	4

106 107
/* Refill Rx queue when number of required descriptors is above
 * QUEUE_SIZE / ENA_RX_REFILL_THRESH_DIVIDER or ENA_RX_REFILL_THRESH_PACKET
108 109
 */
#define ENA_RX_REFILL_THRESH_DIVIDER	8
110
#define ENA_RX_REFILL_THRESH_PACKET	256
111 112 113 114

/* Number of queues to check for missing queues per timer service */
#define ENA_MONITORED_TX_QUEUES	4
/* Max timeout packets before device reset */
115
#define MAX_NUM_OF_TIMEOUTED_PACKETS 128
116 117 118 119 120 121 122 123 124

#define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))

#define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1))
#define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \
	(((idx) + (n)) & ((ring_size) - 1))

#define ENA_IO_TXQ_IDX(q)	(2 * (q))
#define ENA_IO_RXQ_IDX(q)	(2 * (q) + 1)
125 126
#define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q)	((q) / 2)
#define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q)	(((q) - 1) / 2)
127 128 129 130 131

#define ENA_MGMNT_IRQ_IDX		0
#define ENA_IO_IRQ_FIRST_IDX		1
#define ENA_IO_IRQ_IDX(q)		(ENA_IO_IRQ_FIRST_IDX + (q))

132 133
#define ENA_ADMIN_POLL_DELAY_US 100

134
/* ENA device should send keep alive msg every 1 sec.
135
 * We wait for 6 sec just to be on the safe side.
136
 */
137
#define ENA_DEVICE_KALIVE_TIMEOUT	(6 * HZ)
138
#define ENA_MAX_NO_INTERRUPT_ITERATIONS 3
139 140 141

#define ENA_MMIO_DISABLE_REG_READ	BIT(0)

142 143 144 145 146 147
/* The max MTU size is configured to be the ethernet frame size without
 * the overhead of the ethernet header, which can have a VLAN header, and
 * a frame check sequence (FCS).
 * The buffer size we share with the device is defined to be ENA_PAGE_SIZE
 */

148 149 150
#define ENA_XDP_MAX_MTU (ENA_PAGE_SIZE - ETH_HLEN - ETH_FCS_LEN -	\
			 VLAN_HLEN - XDP_PACKET_HEADROOM -		\
			 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
151

S
Sameeh Jubran 已提交
152 153 154
#define ENA_IS_XDP_INDEX(adapter, index) (((index) >= (adapter)->xdp_first_ring) && \
	((index) < (adapter)->xdp_first_ring + (adapter)->xdp_num_queues))

155 156 157 158 159 160 161 162 163 164 165 166 167
struct ena_irq {
	irq_handler_t handler;
	void *data;
	int cpu;
	u32 vector;
	cpumask_t affinity_hint_mask;
	char name[ENA_IRQNAME_SIZE];
};

struct ena_napi {
	struct napi_struct napi ____cacheline_aligned;
	struct ena_ring *tx_ring;
	struct ena_ring *rx_ring;
S
Sameeh Jubran 已提交
168
	struct ena_ring *xdp_ring;
169
	bool first_interrupt;
170
	bool interrupts_masked;
171
	u32 qid;
172
	struct dim dim;
173 174
};

175 176 177 178
struct ena_calc_queue_size_ctx {
	struct ena_com_dev_get_features_ctx *get_feat_ctx;
	struct ena_com_dev *ena_dev;
	struct pci_dev *pdev;
179 180 181 182
	u32 tx_queue_size;
	u32 rx_queue_size;
	u32 max_tx_queue_size;
	u32 max_rx_queue_size;
183 184 185 186
	u16 max_tx_sgl_size;
	u16 max_rx_sgl_size;
};

187 188 189 190 191 192 193 194
struct ena_tx_buffer {
	struct sk_buff *skb;
	/* num of ena desc for this specific skb
	 * (includes data desc and metadata desc)
	 */
	u32 tx_descs;
	/* num of buffers used by this skb */
	u32 num_of_bufs;
195

S
Sameeh Jubran 已提交
196 197 198 199 200 201 202 203 204 205 206
	/* XDP buffer structure which is used for sending packets in
	 * the xdp queues
	 */
	struct xdp_frame *xdpf;
	/* The rx page for the rx buffer that was received in rx and
	 * re transmitted on xdp tx queues as a result of XDP_TX action.
	 * We need to free the page once we finished cleaning the buffer in
	 * clean_xdp_irq()
	 */
	struct page *xdp_rx_page;

207 208 209
	/* Indicate if bufs[0] map the linear data of the skb. */
	u8 map_linear_data;

210 211 212 213 214 215 216 217 218 219 220
	/* Used for detect missing tx packets to limit the number of prints */
	u32 print_once;
	/* Save the last jiffies to detect missing tx packets
	 *
	 * sets to non zero value on ena_start_xmit and set to zero on
	 * napi and timer_Service_routine.
	 *
	 * while this value is not protected by lock,
	 * a given packet is not expected to be handled by ena_start_xmit
	 * and by napi/timer_service at the same time.
	 */
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	unsigned long last_jiffies;
	struct ena_com_buf bufs[ENA_PKT_MAX_BUFS];
} ____cacheline_aligned;

struct ena_rx_buffer {
	struct sk_buff *skb;
	struct page *page;
	u32 page_offset;
	struct ena_com_buf ena_buf;
} ____cacheline_aligned;

struct ena_stats_tx {
	u64 cnt;
	u64 bytes;
	u64 queue_stop;
	u64 prepare_ctx_err;
	u64 queue_wakeup;
	u64 dma_mapping_err;
	u64 linearize;
	u64 linearize_failed;
	u64 napi_comp;
	u64 tx_poll;
	u64 doorbells;
	u64 bad_req_id;
245
	u64 llq_buffer_copy;
246
	u64 missed_tx;
247
	u64 unmask_interrupt;
248 249 250 251 252
};

struct ena_stats_rx {
	u64 cnt;
	u64 bytes;
253 254
	u64 rx_copybreak_pkt;
	u64 csum_good;
255 256 257 258 259 260
	u64 refil_partial;
	u64 bad_csum;
	u64 page_alloc_fail;
	u64 skb_alloc_fail;
	u64 dma_mapping_err;
	u64 bad_desc_num;
261
	u64 bad_req_id;
262
	u64 empty_rx_ring;
263
	u64 csum_unchecked;
264 265 266
};

struct ena_ring {
267 268 269 270
	/* Holds the empty requests for TX/RX
	 * out of order completions
	 */
	u16 *free_ids;
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285
	union {
		struct ena_tx_buffer *tx_buffer_info;
		struct ena_rx_buffer *rx_buffer_info;
	};

	/* cache ptr to avoid using the adapter */
	struct device *dev;
	struct pci_dev *pdev;
	struct napi_struct *napi;
	struct net_device *netdev;
	struct ena_com_dev *ena_dev;
	struct ena_adapter *adapter;
	struct ena_com_io_cq *ena_com_io_cq;
	struct ena_com_io_sq *ena_com_io_sq;
286 287
	struct bpf_prog *xdp_bpf_prog;
	struct xdp_rxq_info xdp_rxq;
288 289 290 291

	u16 next_to_use;
	u16 next_to_clean;
	u16 rx_copybreak;
292
	u16 rx_headroom;
293 294 295 296 297 298 299
	u16 qid;
	u16 mtu;
	u16 sgl_size;

	/* The maximum header length the device can handle */
	u8 tx_max_header_size;

300
	bool first_interrupt;
301
	bool disable_meta_caching;
302 303
	u16 no_interrupt_event_cnt;

304 305 306 307 308 309 310 311 312 313
	/* cpu for TPH */
	int cpu;
	 /* number of tx/rx_buffer_info's entries */
	int ring_size;

	enum ena_admin_placement_policy_type tx_mem_queue_type;

	struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS];
	u32  smoothed_interval;
	u32  per_napi_packets;
314
	u16 non_empty_napi_events;
315 316 317 318 319
	struct u64_stats_sync syncp;
	union {
		struct ena_stats_tx tx_stats;
		struct ena_stats_rx rx_stats;
	};
320 321

	u8 *push_buf_intermediate_buf;
322
	int empty_rx_queue;
323 324 325 326
} ____cacheline_aligned;

struct ena_stats_dev {
	u64 tx_timeout;
327 328
	u64 suspend;
	u64 resume;
329 330 331 332
	u64 wd_expired;
	u64 interface_up;
	u64 interface_down;
	u64 admin_q_pause;
333
	u64 rx_drops;
334
	u64 tx_drops;
335 336 337 338 339 340
};

enum ena_flags_t {
	ENA_FLAG_DEVICE_RUNNING,
	ENA_FLAG_DEV_UP,
	ENA_FLAG_LINK_UP,
341
	ENA_FLAG_MSIX_ENABLED,
342 343
	ENA_FLAG_TRIGGER_RESET,
	ENA_FLAG_ONGOING_RESET
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
};

/* adapter specific private data structure */
struct ena_adapter {
	struct ena_com_dev *ena_dev;
	/* OS defined structs */
	struct net_device *netdev;
	struct pci_dev *pdev;

	/* rx packets that shorter that this len will be copied to the skb
	 * header
	 */
	u32 rx_copybreak;
	u32 max_mtu;

359 360
	u32 num_io_queues;
	u32 max_num_io_queues;
361 362 363

	int msix_vecs;

364 365
	u32 missing_tx_completion_threshold;

366 367
	u32 requested_tx_ring_size;
	u32 requested_rx_ring_size;
368

369 370 371
	u32 max_tx_ring_size;
	u32 max_rx_ring_size;

372 373 374 375 376 377 378
	u32 msg_enable;

	u16 max_tx_sgl_size;
	u16 max_rx_sgl_size;

	u8 mac_addr[ETH_ALEN];

379 380 381
	unsigned long keep_alive_timeout;
	unsigned long missing_tx_completion_to;

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	char name[ENA_NAME_MAX_LEN];

	unsigned long flags;
	/* TX */
	struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES]
		____cacheline_aligned_in_smp;

	/* RX */
	struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES]
		____cacheline_aligned_in_smp;

	struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES];

	struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)];

	/* timer service */
	struct work_struct reset_task;
	struct timer_list timer_service;

	bool wd_state;
402
	bool dev_up_before_reset;
403
	bool disable_meta_caching;
404 405 406 407
	unsigned long last_keep_alive_jiffies;

	struct u64_stats_sync syncp;
	struct ena_stats_dev dev_stats;
408 409
	struct ena_admin_eni_stats eni_stats;
	bool eni_stats_supported;
410 411 412

	/* last queue index that was checked for uncompleted tx packets */
	u32 last_monitored_tx_qid;
413 414

	enum ena_regs_reset_reason_types reset_reason;
415 416

	struct bpf_prog *xdp_bpf_prog;
S
Sameeh Jubran 已提交
417 418
	u32 xdp_first_ring;
	u32 xdp_num_queues;
419 420 421 422 423 424 425 426
};

void ena_set_ethtool_ops(struct net_device *netdev);

void ena_dump_stats_to_dmesg(struct ena_adapter *adapter);

void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf);

427 428
int ena_update_hw_stats(struct ena_adapter *adapter);

429 430 431
int ena_update_queue_sizes(struct ena_adapter *adapter,
			   u32 new_tx_size,
			   u32 new_rx_size);
432

433
int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count);
434

435 436
int ena_get_sset_count(struct net_device *netdev, int sset);

S
Sameeh Jubran 已提交
437 438 439 440 441 442 443 444 445 446 447
enum ena_xdp_errors_t {
	ENA_XDP_ALLOWED = 0,
	ENA_XDP_CURRENT_MTU_TOO_LARGE,
	ENA_XDP_NO_ENOUGH_QUEUES,
};

static inline bool ena_xdp_queues_present(struct ena_adapter *adapter)
{
	return adapter->xdp_first_ring != 0;
}

448 449 450 451 452 453 454 455 456 457
static inline bool ena_xdp_present(struct ena_adapter *adapter)
{
	return !!adapter->xdp_bpf_prog;
}

static inline bool ena_xdp_present_ring(struct ena_ring *ring)
{
	return !!ring->xdp_bpf_prog;
}

S
Sameeh Jubran 已提交
458 459
static inline int ena_xdp_legal_queue_count(struct ena_adapter *adapter,
					    u32 queues)
460
{
S
Sameeh Jubran 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473
	return 2 * queues <= adapter->max_num_io_queues;
}

static inline enum ena_xdp_errors_t ena_xdp_allowed(struct ena_adapter *adapter)
{
	enum ena_xdp_errors_t rc = ENA_XDP_ALLOWED;

	if (adapter->netdev->mtu > ENA_XDP_MAX_MTU)
		rc = ENA_XDP_CURRENT_MTU_TOO_LARGE;
	else if (!ena_xdp_legal_queue_count(adapter, adapter->num_io_queues))
		rc = ENA_XDP_NO_ENOUGH_QUEUES;

	return rc;
474 475
}

476
#endif /* !(ENA_H) */