1 /* 2 * Copyright 2015 Amazon.com, Inc. or its affiliates. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 #ifndef ENA_H 34 #define ENA_H 35 36 #include <linux/bitops.h> 37 #include <linux/dim.h> 38 #include <linux/etherdevice.h> 39 #include <linux/if_vlan.h> 40 #include <linux/inetdevice.h> 41 #include <linux/interrupt.h> 42 #include <linux/netdevice.h> 43 #include <linux/skbuff.h> 44 45 #include "ena_com.h" 46 #include "ena_eth_com.h" 47 48 #define DRV_MODULE_GEN_MAJOR 2 49 #define DRV_MODULE_GEN_MINOR 1 50 #define DRV_MODULE_GEN_SUBMINOR 0 51 52 #define DRV_MODULE_NAME "ena" 53 54 #define DEVICE_NAME "Elastic Network Adapter (ENA)" 55 56 /* 1 for AENQ + ADMIN */ 57 #define ENA_ADMIN_MSIX_VEC 1 58 #define ENA_MAX_MSIX_VEC(io_queues) (ENA_ADMIN_MSIX_VEC + (io_queues)) 59 60 /* The ENA buffer length fields is 16 bit long. So when PAGE_SIZE == 64kB the 61 * driver passes 0. 62 * Since the max packet size the ENA handles is ~9kB limit the buffer length to 63 * 16kB. 64 */ 65 #if PAGE_SIZE > SZ_16K 66 #define ENA_PAGE_SIZE (_AC(SZ_16K, UL)) 67 #else 68 #define ENA_PAGE_SIZE PAGE_SIZE 69 #endif 70 71 #define ENA_MIN_MSIX_VEC 2 72 73 #define ENA_REG_BAR 0 74 #define ENA_MEM_BAR 2 75 #define ENA_BAR_MASK (BIT(ENA_REG_BAR) | BIT(ENA_MEM_BAR)) 76 77 #define ENA_DEFAULT_RING_SIZE (1024) 78 #define ENA_MIN_RING_SIZE (256) 79 80 #define ENA_MIN_NUM_IO_QUEUES (1) 81 82 #define ENA_TX_WAKEUP_THRESH (MAX_SKB_FRAGS + 2) 83 #define ENA_DEFAULT_RX_COPYBREAK (256 - NET_IP_ALIGN) 84 85 /* limit the buffer size to 600 bytes to handle MTU changes from very 86 * small to very large, in which case the number of buffers per packet 87 * could exceed ENA_PKT_MAX_BUFS 88 */ 89 #define ENA_DEFAULT_MIN_RX_BUFF_ALLOC_SIZE 600 90 91 #define ENA_MIN_MTU 128 92 93 #define ENA_NAME_MAX_LEN 20 94 #define ENA_IRQNAME_SIZE 40 95 96 #define ENA_PKT_MAX_BUFS 19 97 98 #define ENA_RX_RSS_TABLE_LOG_SIZE 7 99 #define ENA_RX_RSS_TABLE_SIZE (1 << ENA_RX_RSS_TABLE_LOG_SIZE) 100 101 /* The number of tx packet completions that will be handled each NAPI poll 102 * cycle is ring_size / ENA_TX_POLL_BUDGET_DIVIDER. 103 */ 104 #define ENA_TX_POLL_BUDGET_DIVIDER 4 105 106 /* Refill Rx queue when number of required descriptors is above 107 * 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 /* Number of queues to check for missing queues per timer service */ 113 #define ENA_MONITORED_TX_QUEUES 4 114 /* Max timeout packets before device reset */ 115 #define MAX_NUM_OF_TIMEOUTED_PACKETS 128 116 117 #define ENA_TX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1)) 118 119 #define ENA_RX_RING_IDX_NEXT(idx, ring_size) (((idx) + 1) & ((ring_size) - 1)) 120 #define ENA_RX_RING_IDX_ADD(idx, n, ring_size) \ 121 (((idx) + (n)) & ((ring_size) - 1)) 122 123 #define ENA_IO_TXQ_IDX(q) (2 * (q)) 124 #define ENA_IO_RXQ_IDX(q) (2 * (q) + 1) 125 #define ENA_IO_TXQ_IDX_TO_COMBINED_IDX(q) ((q) / 2) 126 #define ENA_IO_RXQ_IDX_TO_COMBINED_IDX(q) (((q) - 1) / 2) 127 128 #define ENA_MGMNT_IRQ_IDX 0 129 #define ENA_IO_IRQ_FIRST_IDX 1 130 #define ENA_IO_IRQ_IDX(q) (ENA_IO_IRQ_FIRST_IDX + (q)) 131 132 #define ENA_ADMIN_POLL_DELAY_US 100 133 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 #define ENA_MMIO_DISABLE_REG_READ BIT(0) 141 142 /* The max MTU size is configured to be the ethernet frame size without 143 * the overhead of the ethernet header, which can have a VLAN header, and 144 * a frame check sequence (FCS). 145 * The buffer size we share with the device is defined to be ENA_PAGE_SIZE 146 */ 147 148 #define ENA_XDP_MAX_MTU (ENA_PAGE_SIZE - ETH_HLEN - ETH_FCS_LEN - \ 149 VLAN_HLEN - XDP_PACKET_HEADROOM - \ 150 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 151 152 #define ENA_IS_XDP_INDEX(adapter, index) (((index) >= (adapter)->xdp_first_ring) && \ 153 ((index) < (adapter)->xdp_first_ring + (adapter)->xdp_num_queues)) 154 155 struct ena_irq { 156 irq_handler_t handler; 157 void *data; 158 int cpu; 159 u32 vector; 160 cpumask_t affinity_hint_mask; 161 char name[ENA_IRQNAME_SIZE]; 162 }; 163 164 struct ena_napi { 165 struct napi_struct napi ____cacheline_aligned; 166 struct ena_ring *tx_ring; 167 struct ena_ring *rx_ring; 168 struct ena_ring *xdp_ring; 169 bool first_interrupt; 170 u32 qid; 171 struct dim dim; 172 }; 173 174 struct ena_calc_queue_size_ctx { 175 struct ena_com_dev_get_features_ctx *get_feat_ctx; 176 struct ena_com_dev *ena_dev; 177 struct pci_dev *pdev; 178 u32 tx_queue_size; 179 u32 rx_queue_size; 180 u32 max_tx_queue_size; 181 u32 max_rx_queue_size; 182 u16 max_tx_sgl_size; 183 u16 max_rx_sgl_size; 184 }; 185 186 struct ena_tx_buffer { 187 struct sk_buff *skb; 188 /* num of ena desc for this specific skb 189 * (includes data desc and metadata desc) 190 */ 191 u32 tx_descs; 192 /* num of buffers used by this skb */ 193 u32 num_of_bufs; 194 195 /* XDP buffer structure which is used for sending packets in 196 * the xdp queues 197 */ 198 struct xdp_frame *xdpf; 199 /* The rx page for the rx buffer that was received in rx and 200 * re transmitted on xdp tx queues as a result of XDP_TX action. 201 * We need to free the page once we finished cleaning the buffer in 202 * clean_xdp_irq() 203 */ 204 struct page *xdp_rx_page; 205 206 /* Indicate if bufs[0] map the linear data of the skb. */ 207 u8 map_linear_data; 208 209 /* Used for detect missing tx packets to limit the number of prints */ 210 u32 print_once; 211 /* Save the last jiffies to detect missing tx packets 212 * 213 * sets to non zero value on ena_start_xmit and set to zero on 214 * napi and timer_Service_routine. 215 * 216 * while this value is not protected by lock, 217 * a given packet is not expected to be handled by ena_start_xmit 218 * and by napi/timer_service at the same time. 219 */ 220 unsigned long last_jiffies; 221 struct ena_com_buf bufs[ENA_PKT_MAX_BUFS]; 222 } ____cacheline_aligned; 223 224 struct ena_rx_buffer { 225 struct sk_buff *skb; 226 struct page *page; 227 u32 page_offset; 228 struct ena_com_buf ena_buf; 229 } ____cacheline_aligned; 230 231 struct ena_stats_tx { 232 u64 cnt; 233 u64 bytes; 234 u64 queue_stop; 235 u64 prepare_ctx_err; 236 u64 queue_wakeup; 237 u64 dma_mapping_err; 238 u64 linearize; 239 u64 linearize_failed; 240 u64 napi_comp; 241 u64 tx_poll; 242 u64 doorbells; 243 u64 bad_req_id; 244 u64 llq_buffer_copy; 245 u64 missed_tx; 246 u64 unmask_interrupt; 247 }; 248 249 struct ena_stats_rx { 250 u64 cnt; 251 u64 bytes; 252 u64 rx_copybreak_pkt; 253 u64 csum_good; 254 u64 refil_partial; 255 u64 bad_csum; 256 u64 page_alloc_fail; 257 u64 skb_alloc_fail; 258 u64 dma_mapping_err; 259 u64 bad_desc_num; 260 u64 bad_req_id; 261 u64 empty_rx_ring; 262 u64 csum_unchecked; 263 }; 264 265 struct ena_ring { 266 /* Holds the empty requests for TX/RX 267 * out of order completions 268 */ 269 u16 *free_ids; 270 271 union { 272 struct ena_tx_buffer *tx_buffer_info; 273 struct ena_rx_buffer *rx_buffer_info; 274 }; 275 276 /* cache ptr to avoid using the adapter */ 277 struct device *dev; 278 struct pci_dev *pdev; 279 struct napi_struct *napi; 280 struct net_device *netdev; 281 struct ena_com_dev *ena_dev; 282 struct ena_adapter *adapter; 283 struct ena_com_io_cq *ena_com_io_cq; 284 struct ena_com_io_sq *ena_com_io_sq; 285 struct bpf_prog *xdp_bpf_prog; 286 struct xdp_rxq_info xdp_rxq; 287 288 u16 next_to_use; 289 u16 next_to_clean; 290 u16 rx_copybreak; 291 u16 rx_headroom; 292 u16 qid; 293 u16 mtu; 294 u16 sgl_size; 295 296 /* The maximum header length the device can handle */ 297 u8 tx_max_header_size; 298 299 bool first_interrupt; 300 u16 no_interrupt_event_cnt; 301 302 /* cpu for TPH */ 303 int cpu; 304 /* number of tx/rx_buffer_info's entries */ 305 int ring_size; 306 307 enum ena_admin_placement_policy_type tx_mem_queue_type; 308 309 struct ena_com_rx_buf_info ena_bufs[ENA_PKT_MAX_BUFS]; 310 u32 smoothed_interval; 311 u32 per_napi_packets; 312 u16 non_empty_napi_events; 313 struct u64_stats_sync syncp; 314 union { 315 struct ena_stats_tx tx_stats; 316 struct ena_stats_rx rx_stats; 317 }; 318 319 u8 *push_buf_intermediate_buf; 320 int empty_rx_queue; 321 } ____cacheline_aligned; 322 323 struct ena_stats_dev { 324 u64 tx_timeout; 325 u64 suspend; 326 u64 resume; 327 u64 wd_expired; 328 u64 interface_up; 329 u64 interface_down; 330 u64 admin_q_pause; 331 u64 rx_drops; 332 u64 tx_drops; 333 }; 334 335 enum ena_flags_t { 336 ENA_FLAG_DEVICE_RUNNING, 337 ENA_FLAG_DEV_UP, 338 ENA_FLAG_LINK_UP, 339 ENA_FLAG_MSIX_ENABLED, 340 ENA_FLAG_TRIGGER_RESET, 341 ENA_FLAG_ONGOING_RESET 342 }; 343 344 /* adapter specific private data structure */ 345 struct ena_adapter { 346 struct ena_com_dev *ena_dev; 347 /* OS defined structs */ 348 struct net_device *netdev; 349 struct pci_dev *pdev; 350 351 /* rx packets that shorter that this len will be copied to the skb 352 * header 353 */ 354 u32 rx_copybreak; 355 u32 max_mtu; 356 357 u32 num_io_queues; 358 u32 max_num_io_queues; 359 360 int msix_vecs; 361 362 u32 missing_tx_completion_threshold; 363 364 u32 requested_tx_ring_size; 365 u32 requested_rx_ring_size; 366 367 u32 max_tx_ring_size; 368 u32 max_rx_ring_size; 369 370 u32 msg_enable; 371 372 u16 max_tx_sgl_size; 373 u16 max_rx_sgl_size; 374 375 u8 mac_addr[ETH_ALEN]; 376 377 unsigned long keep_alive_timeout; 378 unsigned long missing_tx_completion_to; 379 380 char name[ENA_NAME_MAX_LEN]; 381 382 unsigned long flags; 383 /* TX */ 384 struct ena_ring tx_ring[ENA_MAX_NUM_IO_QUEUES] 385 ____cacheline_aligned_in_smp; 386 387 /* RX */ 388 struct ena_ring rx_ring[ENA_MAX_NUM_IO_QUEUES] 389 ____cacheline_aligned_in_smp; 390 391 struct ena_napi ena_napi[ENA_MAX_NUM_IO_QUEUES]; 392 393 struct ena_irq irq_tbl[ENA_MAX_MSIX_VEC(ENA_MAX_NUM_IO_QUEUES)]; 394 395 /* timer service */ 396 struct work_struct reset_task; 397 struct timer_list timer_service; 398 399 bool wd_state; 400 bool dev_up_before_reset; 401 unsigned long last_keep_alive_jiffies; 402 403 struct u64_stats_sync syncp; 404 struct ena_stats_dev dev_stats; 405 406 /* last queue index that was checked for uncompleted tx packets */ 407 u32 last_monitored_tx_qid; 408 409 enum ena_regs_reset_reason_types reset_reason; 410 411 struct bpf_prog *xdp_bpf_prog; 412 u32 xdp_first_ring; 413 u32 xdp_num_queues; 414 }; 415 416 void ena_set_ethtool_ops(struct net_device *netdev); 417 418 void ena_dump_stats_to_dmesg(struct ena_adapter *adapter); 419 420 void ena_dump_stats_to_buf(struct ena_adapter *adapter, u8 *buf); 421 422 int ena_update_queue_sizes(struct ena_adapter *adapter, 423 u32 new_tx_size, 424 u32 new_rx_size); 425 426 int ena_update_queue_count(struct ena_adapter *adapter, u32 new_channel_count); 427 428 int ena_get_sset_count(struct net_device *netdev, int sset); 429 430 enum ena_xdp_errors_t { 431 ENA_XDP_ALLOWED = 0, 432 ENA_XDP_CURRENT_MTU_TOO_LARGE, 433 ENA_XDP_NO_ENOUGH_QUEUES, 434 }; 435 436 static inline bool ena_xdp_queues_present(struct ena_adapter *adapter) 437 { 438 return adapter->xdp_first_ring != 0; 439 } 440 441 static inline bool ena_xdp_present(struct ena_adapter *adapter) 442 { 443 return !!adapter->xdp_bpf_prog; 444 } 445 446 static inline bool ena_xdp_present_ring(struct ena_ring *ring) 447 { 448 return !!ring->xdp_bpf_prog; 449 } 450 451 static inline int ena_xdp_legal_queue_count(struct ena_adapter *adapter, 452 u32 queues) 453 { 454 return 2 * queues <= adapter->max_num_io_queues; 455 } 456 457 static inline enum ena_xdp_errors_t ena_xdp_allowed(struct ena_adapter *adapter) 458 { 459 enum ena_xdp_errors_t rc = ENA_XDP_ALLOWED; 460 461 if (adapter->netdev->mtu > ENA_XDP_MAX_MTU) 462 rc = ENA_XDP_CURRENT_MTU_TOO_LARGE; 463 else if (!ena_xdp_legal_queue_count(adapter, adapter->num_io_queues)) 464 rc = ENA_XDP_NO_ENOUGH_QUEUES; 465 466 return rc; 467 } 468 469 #endif /* !(ENA_H) */ 470