2 * Copyright (c) 2016 Matt Macy <mmacy@nextbsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "opt_inet6.h"
33 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include "opt_device_polling.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/types.h>
43 #if __FreeBSD_version >= 800000
44 #include <sys/buf_ring.h>
47 #include <sys/endian.h>
48 #include <sys/kernel.h>
49 #include <sys/kthread.h>
50 #include <sys/malloc.h>
52 #include <sys/module.h>
55 #include <sys/socket.h>
56 #include <sys/sockio.h>
57 #include <sys/sysctl.h>
58 #include <sys/taskqueue.h>
59 #include <sys/eventhandler.h>
60 #include <machine/bus.h>
61 #include <machine/resource.h>
64 #include <net/ethernet.h>
66 #include <net/if_var.h>
67 #include <net/if_arp.h>
68 #include <net/if_dl.h>
69 #include <net/if_media.h>
70 #include <net/iflib.h>
72 #include <net/if_types.h>
73 #include <net/if_vlan_var.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/ip.h>
79 #include <netinet/ip6.h>
80 #include <netinet/tcp.h>
81 #include <netinet/udp.h>
83 #include <machine/in_cksum.h>
84 #include <dev/led/led.h>
85 #include <dev/pci/pcivar.h>
86 #include <dev/pci/pcireg.h>
88 #include "e1000_api.h"
89 #include "e1000_82571.h"
93 #ifndef _EM_H_DEFINED_
94 #define _EM_H_DEFINED_
100 * EM_TXD: Maximum number of Transmit Descriptors
101 * Valid Range: 80-256 for 82542 and 82543-based adapters
104 * This value is the number of transmit descriptors allocated by the driver.
105 * Increasing this value allows the driver to queue more transmits. Each
106 * descriptor is 16 bytes.
107 * Since TDLEN should be multiple of 128bytes, the number of transmit
108 * desscriptors should meet the following condition.
109 * (num_tx_desc * sizeof(struct e1000_tx_desc)) % 128 == 0
111 #define EM_MIN_TXD 128
112 #define EM_MAX_TXD 4096
113 #define EM_DEFAULT_TXD 1024
114 #define EM_DEFAULT_MULTI_TXD 4096
117 * EM_RXD - Maximum number of receive Descriptors
118 * Valid Range: 80-256 for 82542 and 82543-based adapters
121 * This value is the number of receive descriptors allocated by the driver.
122 * Increasing this value allows the driver to buffer more incoming packets.
123 * Each descriptor is 16 bytes. A receive buffer is also allocated for each
124 * descriptor. The maximum MTU size is 16110.
125 * Since TDLEN should be multiple of 128bytes, the number of transmit
126 * desscriptors should meet the following condition.
127 * (num_tx_desc * sizeof(struct e1000_tx_desc)) % 128 == 0
129 #define EM_MIN_RXD 128
130 #define EM_MAX_RXD 4096
131 #define EM_DEFAULT_RXD 1024
132 #define EM_DEFAULT_MULTI_RXD 4096
135 * EM_TIDV - Transmit Interrupt Delay Value
136 * Valid Range: 0-65535 (0=off)
138 * This value delays the generation of transmit interrupts in units of
139 * 1.024 microseconds. Transmit interrupt reduction can improve CPU
140 * efficiency if properly tuned for specific network traffic. If the
141 * system is reporting dropped transmits, this value may be set too high
142 * causing the driver to run out of available transmit descriptors.
147 * EM_TADV - Transmit Absolute Interrupt Delay Value
148 * (Not valid for 82542/82543/82544)
149 * Valid Range: 0-65535 (0=off)
151 * This value, in units of 1.024 microseconds, limits the delay in which a
152 * transmit interrupt is generated. Useful only if EM_TIDV is non-zero,
153 * this value ensures that an interrupt is generated after the initial
154 * packet is sent on the wire within the set amount of time. Proper tuning,
155 * along with EM_TIDV, may improve traffic throughput in specific
156 * network conditions.
161 * EM_RDTR - Receive Interrupt Delay Timer (Packet Timer)
162 * Valid Range: 0-65535 (0=off)
164 * This value delays the generation of receive interrupts in units of 1.024
165 * microseconds. Receive interrupt reduction can improve CPU efficiency if
166 * properly tuned for specific network traffic. Increasing this value adds
167 * extra latency to frame reception and can end up decreasing the throughput
168 * of TCP traffic. If the system is reporting dropped receives, this value
169 * may be set too high, causing the driver to run out of available receive
172 * CAUTION: When setting EM_RDTR to a value other than 0, adapters
173 * may hang (stop transmitting) under certain network conditions.
174 * If this occurs a WATCHDOG message is logged in the system
175 * event log. In addition, the controller is automatically reset,
176 * restoring the network connection. To eliminate the potential
177 * for the hang ensure that EM_RDTR is set to 0.
186 * Receive Interrupt Absolute Delay Timer (Not valid for 82542/82543/82544)
187 * Valid Range: 0-65535 (0=off)
189 * This value, in units of 1.024 microseconds, limits the delay in which a
190 * receive interrupt is generated. Useful only if EM_RDTR is non-zero,
191 * this value ensures that an interrupt is generated after the initial
192 * packet is received within the set amount of time. Proper tuning,
193 * along with EM_RDTR, may improve traffic throughput in specific network
203 * This parameter controls whether or not autonegotation is enabled.
204 * 0 - Disable autonegotiation
205 * 1 - Enable autonegotiation
207 #define DO_AUTO_NEG 1
210 * This parameter control whether or not the driver will wait for
211 * autonegotiation to complete.
212 * 1 - Wait for autonegotiation to complete
213 * 0 - Don't wait for autonegotiation to complete
215 #define WAIT_FOR_AUTO_NEG_DEFAULT 0
217 /* Tunables -- End */
219 #define AUTONEG_ADV_DEFAULT (ADVERTISE_10_HALF | ADVERTISE_10_FULL | \
220 ADVERTISE_100_HALF | ADVERTISE_100_FULL | \
223 #define AUTO_ALL_MODES 0
225 /* PHY master/slave setting */
226 #define EM_MASTER_SLAVE e1000_ms_hw_default
229 * Micellaneous constants
231 #define EM_VENDOR_ID 0x8086
232 #define EM_FLASH 0x0014
234 #define EM_JUMBO_PBA 0x00000028
235 #define EM_DEFAULT_PBA 0x00000030
236 #define EM_SMARTSPEED_DOWNSHIFT 3
237 #define EM_SMARTSPEED_MAX 15
238 #define EM_MAX_LOOP 10
240 #define MAX_NUM_MULTICAST_ADDRESSES 128
241 #define PCI_ANY_ID (~0U)
242 #define ETHER_ALIGN 2
243 #define EM_FC_PAUSE_TIME 0x0680
244 #define EM_EEPROM_APME 0x400;
245 #define EM_82544_APME 0x0004;
248 * Driver state logic for the detection of a hung state
249 * in hardware. Set TX_HUNG whenever a TX packet is used
250 * (data is sent) and clear it when txeof() is invoked if
251 * any descriptors from the ring are cleaned/reclaimed.
252 * Increment internal counter if no descriptors are cleaned
253 * and compare to TX_MAXTRIES. When counter > TX_MAXTRIES,
256 #define EM_TX_IDLE 0x00000000
257 #define EM_TX_BUSY 0x00000001
258 #define EM_TX_HUNG 0x80000000
259 #define EM_TX_MAXTRIES 10
261 #define PCICFG_DESC_RING_STATUS 0xe4
262 #define FLUSH_DESC_REQUIRED 0x100
265 #define IGB_RX_PTHRESH ((hw->mac.type == e1000_i354) ? 12 : \
266 ((hw->mac.type <= e1000_82576) ? 16 : 8))
267 #define IGB_RX_HTHRESH 8
268 #define IGB_RX_WTHRESH ((hw->mac.type == e1000_82576 && \
269 (adapter->intr_type == IFLIB_INTR_MSIX)) ? 1 : 4)
271 #define IGB_TX_PTHRESH ((hw->mac.type == e1000_i354) ? 20 : 8)
272 #define IGB_TX_HTHRESH 1
273 #define IGB_TX_WTHRESH ((hw->mac.type != e1000_82575 && \
274 (adapter->intr_type == IFLIB_INTR_MSIX) ? 1 : 16)
277 * TDBA/RDBA should be aligned on 16 byte boundary. But TDLEN/RDLEN should be
278 * multiple of 128 bytes. So we align TDBA/RDBA on 128 byte boundary. This will
279 * also optimize cache line size effect. H/W supports up to cache line size 128.
281 #define EM_DBA_ALIGN 128
284 * See Intel 82574 Driver Programming Interface Manual, Section 10.2.6.9
286 #define TARC_COMPENSATION_MODE (1 << 7) /* Compensation Mode */
287 #define TARC_SPEED_MODE_BIT (1 << 21) /* On PCI-E MACs only */
288 #define TARC_MQ_FIX (1 << 23) | \
290 (1 << 25) /* Handle errata in MQ mode */
291 #define TARC_ERRATA_BIT (1 << 26) /* Note from errata on 82574 */
293 /* PCI Config defines */
294 #define EM_BAR_TYPE(v) ((v) & EM_BAR_TYPE_MASK)
295 #define EM_BAR_TYPE_MASK 0x00000001
296 #define EM_BAR_TYPE_MMEM 0x00000000
297 #define EM_BAR_TYPE_IO 0x00000001
298 #define EM_BAR_TYPE_FLASH 0x0014
299 #define EM_BAR_MEM_TYPE(v) ((v) & EM_BAR_MEM_TYPE_MASK)
300 #define EM_BAR_MEM_TYPE_MASK 0x00000006
301 #define EM_BAR_MEM_TYPE_32BIT 0x00000000
302 #define EM_BAR_MEM_TYPE_64BIT 0x00000004
303 #define EM_MSIX_BAR 3 /* On 82575 */
305 /* More backward compatibility */
306 #if __FreeBSD_version < 900000
307 #define SYSCTL_ADD_UQUAD SYSCTL_ADD_QUAD
310 /* Defines for printing debug information */
312 #define DEBUG_IOCTL 0
315 #define INIT_DEBUGOUT(S) if (DEBUG_INIT) printf(S "\n")
316 #define INIT_DEBUGOUT1(S, A) if (DEBUG_INIT) printf(S "\n", A)
317 #define INIT_DEBUGOUT2(S, A, B) if (DEBUG_INIT) printf(S "\n", A, B)
318 #define IOCTL_DEBUGOUT(S) if (DEBUG_IOCTL) printf(S "\n")
319 #define IOCTL_DEBUGOUT1(S, A) if (DEBUG_IOCTL) printf(S "\n", A)
320 #define IOCTL_DEBUGOUT2(S, A, B) if (DEBUG_IOCTL) printf(S "\n", A, B)
321 #define HW_DEBUGOUT(S) if (DEBUG_HW) printf(S "\n")
322 #define HW_DEBUGOUT1(S, A) if (DEBUG_HW) printf(S "\n", A)
323 #define HW_DEBUGOUT2(S, A, B) if (DEBUG_HW) printf(S "\n", A, B)
325 #define EM_MAX_SCATTER 40
326 #define EM_VFTA_SIZE 128
327 #define EM_TSO_SIZE (65535 + sizeof(struct ether_vlan_header))
328 #define EM_TSO_SEG_SIZE 4096 /* Max dma segment size */
329 #define EM_MSIX_MASK 0x01F00000 /* For 82574 use */
330 #define EM_MSIX_LINK 0x01000000 /* For 82574 use */
332 #define ETH_ADDR_LEN 6
333 #define EM_CSUM_OFFLOAD 7 /* Offload bits in mbuf flag */
334 #define IGB_CSUM_OFFLOAD 0x0E0F /* Offload bits in mbuf flag */
336 #define IGB_PKTTYPE_MASK 0x0000FFF0
337 #define IGB_DMCTLX_DCFLUSH_DIS 0x80000000 /* Disable DMA Coalesce Flush */
340 * 82574 has a nonstandard address for EIAC
341 * and since its only used in MSIX, and in
342 * the em driver only 82574 uses MSIX we can
343 * solve it just using this define.
345 #define EM_EIAC 0x000DC
347 * 82574 only reports 3 MSI-X vectors by default;
348 * defines assisting with making it report 5 are
351 #define EM_NVM_PCIE_CTRL 0x1B
352 #define EM_NVM_MSIX_N_MASK (0x7 << EM_NVM_MSIX_N_SHIFT)
353 #define EM_NVM_MSIX_N_SHIFT 7
357 struct em_int_delay_info {
358 struct adapter *adapter; /* Back-pointer to the adapter struct */
359 int offset; /* Register offset to read/write */
360 int value; /* Current value in usecs */
364 * The transmit ring, one per tx queue
367 struct adapter *adapter;
368 struct em_tx_queue *que;
371 struct e1000_tx_desc *tx_base;
373 struct em_txbuffer *tx_buffers;
374 u32 tx_tso; /* last tx was tso */
376 /* Interrupt resources */
378 struct resource *res;
379 unsigned long tx_irq;
380 unsigned long no_desc_avail;
382 /* Saved csum offloading context information */
391 uint32_t csum_txd_upper;
392 uint32_t csum_txd_lower; /* last field */
396 * The Receive ring, one per rx queue
399 struct adapter *adapter;
400 struct em_rx_queue *que;
403 union e1000_rx_desc_extended *rx_base;
406 /* Interrupt resources */
408 struct resource *res;
412 unsigned long rx_irq;
413 unsigned long rx_discarded;
414 unsigned long rx_packets;
415 unsigned long rx_bytes;
419 struct adapter *adapter;
421 u32 eims; /* This queue's EIMS bit */
427 struct adapter *adapter;
433 struct if_irq que_irq;
436 /* Our adapter structure */
441 if_softc_ctx_t shared;
443 #define tx_num_queues shared->isc_ntxqsets
444 #define rx_num_queues shared->isc_nrxqsets
445 #define intr_type shared->isc_intr
446 /* FreeBSD operating-system-specific structures. */
447 struct e1000_osdep osdep;
449 struct cdev *led_dev;
451 struct em_tx_queue *tx_queues;
452 struct em_rx_queue *rx_queues;
455 struct resource *memory;
456 struct resource *flash;
457 struct resource *ioport;
460 struct resource *res;
465 struct ifmedia *media;
469 int em_insert_vlan_header;
473 /* Task for FAST handling */
474 struct grouptask link_task;
479 u32 tx_process_limit;
480 u32 rx_process_limit;
483 /* Management and WOL features */
488 /* Multicast array memory */
492 ** Shadow VFTA table, this is needed because
493 ** the real vlan filter table gets cleared during
494 ** a soft reset and the driver needs to be able
497 u32 shadow_vfta[EM_VFTA_SIZE];
499 /* Info about the interface */
511 struct em_int_delay_info tx_int_delay;
512 struct em_int_delay_info tx_abs_int_delay;
513 struct em_int_delay_info rx_int_delay;
514 struct em_int_delay_info rx_abs_int_delay;
515 struct em_int_delay_info tx_itr;
517 /* Misc stats maintained by the driver */
518 unsigned long dropped_pkts;
519 unsigned long link_irq;
520 unsigned long mbuf_defrag_failed;
521 unsigned long no_tx_dma_setup;
522 unsigned long no_tx_map_avail;
523 unsigned long rx_overruns;
524 unsigned long watchdog_events;
526 struct e1000_hw_stats stats;
529 /********************************************************************************
532 * This array contains the list of Subvendor/Subdevice IDs on which the driver
535 ********************************************************************************/
536 typedef struct _em_vendor_info_t {
537 unsigned int vendor_id;
538 unsigned int device_id;
539 unsigned int subvendor_id;
540 unsigned int subdevice_id;
549 #define EM_CORE_LOCK_INIT(_sc, _name) \
550 mtx_init(&(_sc)->core_mtx, _name, "EM Core Lock", MTX_DEF)
551 #define EM_TX_LOCK_INIT(_sc, _name) \
552 mtx_init(&(_sc)->tx_mtx, _name, "EM TX Lock", MTX_DEF)
553 #define EM_RX_LOCK_INIT(_sc, _name) \
554 mtx_init(&(_sc)->rx_mtx, _name, "EM RX Lock", MTX_DEF)
555 #define EM_CORE_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->core_mtx)
556 #define EM_TX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->tx_mtx)
557 #define EM_RX_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->rx_mtx)
558 #define EM_CORE_LOCK(_sc) mtx_lock(&(_sc)->core_mtx)
559 #define EM_TX_LOCK(_sc) mtx_lock(&(_sc)->tx_mtx)
560 #define EM_TX_TRYLOCK(_sc) mtx_trylock(&(_sc)->tx_mtx)
561 #define EM_RX_LOCK(_sc) mtx_lock(&(_sc)->rx_mtx)
562 #define EM_CORE_UNLOCK(_sc) mtx_unlock(&(_sc)->core_mtx)
563 #define EM_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->tx_mtx)
564 #define EM_RX_UNLOCK(_sc) mtx_unlock(&(_sc)->rx_mtx)
565 #define EM_CORE_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->core_mtx, MA_OWNED)
566 #define EM_TX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->tx_mtx, MA_OWNED)
567 #define EM_RX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->rx_mtx, MA_OWNED)
569 #define EM_RSSRK_SIZE 4
570 #define EM_RSSRK_VAL(key, i) (key[(i) * EM_RSSRK_SIZE] | \
571 key[(i) * EM_RSSRK_SIZE + 1] << 8 | \
572 key[(i) * EM_RSSRK_SIZE + 2] << 16 | \
573 key[(i) * EM_RSSRK_SIZE + 3] << 24)
574 #endif /* _EM_H_DEFINED_ */