1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 1999 - 2006 Intel Corporation. */
3 
4 /* Linux PRO/1000 Ethernet Driver main header file */
5 
6 #ifndef _E1000_H_
7 #define _E1000_H_
8 
9 #include <linux/stddef.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <asm/byteorder.h>
13 #include <linux/mm.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/pci.h>
17 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/skbuff.h>
21 #include <linux/delay.h>
22 #include <linux/timer.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/interrupt.h>
26 #include <linux/string.h>
27 #include <linux/pagemap.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/bitops.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <linux/capability.h>
33 #include <linux/in.h>
34 #include <linux/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/tcp.h>
37 #include <linux/udp.h>
38 #include <net/pkt_sched.h>
39 #include <linux/list.h>
40 #include <linux/reboot.h>
41 #include <net/checksum.h>
42 #include <linux/mii.h>
43 #include <linux/ethtool.h>
44 #include <linux/if_vlan.h>
45 
46 #define BAR_0		0
47 #define BAR_1		1
48 #define BAR_5		5
49 
50 #define INTEL_E1000_ETHERNET_DEVICE(device_id) {\
51 	PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
52 
53 struct e1000_adapter;
54 
55 #include "e1000_hw.h"
56 
57 #define E1000_MAX_INTR			10
58 
59 /*
60  * Count for polling __E1000_RESET condition every 10-20msec.
61  */
62 #define E1000_CHECK_RESET_COUNT	50
63 
64 /* TX/RX descriptor defines */
65 #define E1000_DEFAULT_TXD		256
66 #define E1000_MAX_TXD			256
67 #define E1000_MIN_TXD			48
68 #define E1000_MAX_82544_TXD		4096
69 
70 #define E1000_DEFAULT_RXD		256
71 #define E1000_MAX_RXD			256
72 #define E1000_MIN_RXD			48
73 #define E1000_MAX_82544_RXD		4096
74 
75 #define E1000_MIN_ITR_USECS		10 /* 100000 irq/sec */
76 #define E1000_MAX_ITR_USECS		10000 /* 100    irq/sec */
77 
78 /* this is the size past which hardware will drop packets when setting LPE=0 */
79 #define MAXIMUM_ETHERNET_VLAN_SIZE	1522
80 
81 /* Supported Rx Buffer Sizes */
82 #define E1000_RXBUFFER_128		128    /* Used for packet split */
83 #define E1000_RXBUFFER_256		256    /* Used for packet split */
84 #define E1000_RXBUFFER_512		512
85 #define E1000_RXBUFFER_1024		1024
86 #define E1000_RXBUFFER_2048		2048
87 #define E1000_RXBUFFER_4096		4096
88 #define E1000_RXBUFFER_8192		8192
89 #define E1000_RXBUFFER_16384		16384
90 
91 /* SmartSpeed delimiters */
92 #define E1000_SMARTSPEED_DOWNSHIFT	3
93 #define E1000_SMARTSPEED_MAX		15
94 
95 /* Packet Buffer allocations */
96 #define E1000_PBA_BYTES_SHIFT		0xA
97 #define E1000_TX_HEAD_ADDR_SHIFT	7
98 #define E1000_PBA_TX_MASK		0xFFFF0000
99 
100 /* Flow Control Watermarks */
101 #define E1000_FC_HIGH_DIFF	0x1638 /* High: 5688 bytes below Rx FIFO size */
102 #define E1000_FC_LOW_DIFF	0x1640 /* Low:  5696 bytes below Rx FIFO size */
103 
104 #define E1000_FC_PAUSE_TIME	0xFFFF /* pause for the max or until send xon */
105 
106 /* How many Tx Descriptors do we need to call netif_wake_queue ? */
107 #define E1000_TX_QUEUE_WAKE	16
108 /* How many Rx Buffers do we bundle into one write to the hardware ? */
109 #define E1000_RX_BUFFER_WRITE	16 /* Must be power of 2 */
110 
111 #define AUTO_ALL_MODES		0
112 #define E1000_EEPROM_82544_APM	0x0004
113 #define E1000_EEPROM_APME	0x0400
114 
115 #ifndef E1000_MASTER_SLAVE
116 /* Switch to override PHY master/slave setting */
117 #define E1000_MASTER_SLAVE	e1000_ms_hw_default
118 #endif
119 
120 #define E1000_MNG_VLAN_NONE	(-1)
121 
122 /* wrapper around a pointer to a socket buffer,
123  * so a DMA handle can be stored along with the buffer
124  */
125 struct e1000_tx_buffer {
126 	struct sk_buff *skb;
127 	dma_addr_t dma;
128 	unsigned long time_stamp;
129 	u16 length;
130 	u16 next_to_watch;
131 	bool mapped_as_page;
132 	unsigned short segs;
133 	unsigned int bytecount;
134 };
135 
136 struct e1000_rx_buffer {
137 	union {
138 		struct page *page; /* jumbo: alloc_page */
139 		u8 *data; /* else, netdev_alloc_frag */
140 	} rxbuf;
141 	dma_addr_t dma;
142 };
143 
144 struct e1000_tx_ring {
145 	/* pointer to the descriptor ring memory */
146 	void *desc;
147 	/* physical address of the descriptor ring */
148 	dma_addr_t dma;
149 	/* length of descriptor ring in bytes */
150 	unsigned int size;
151 	/* number of descriptors in the ring */
152 	unsigned int count;
153 	/* next descriptor to associate a buffer with */
154 	unsigned int next_to_use;
155 	/* next descriptor to check for DD status bit */
156 	unsigned int next_to_clean;
157 	/* array of buffer information structs */
158 	struct e1000_tx_buffer *buffer_info;
159 
160 	u16 tdh;
161 	u16 tdt;
162 	bool last_tx_tso;
163 };
164 
165 struct e1000_rx_ring {
166 	/* pointer to the descriptor ring memory */
167 	void *desc;
168 	/* physical address of the descriptor ring */
169 	dma_addr_t dma;
170 	/* length of descriptor ring in bytes */
171 	unsigned int size;
172 	/* number of descriptors in the ring */
173 	unsigned int count;
174 	/* next descriptor to associate a buffer with */
175 	unsigned int next_to_use;
176 	/* next descriptor to check for DD status bit */
177 	unsigned int next_to_clean;
178 	/* array of buffer information structs */
179 	struct e1000_rx_buffer *buffer_info;
180 	struct sk_buff *rx_skb_top;
181 
182 	/* cpu for rx queue */
183 	int cpu;
184 
185 	u16 rdh;
186 	u16 rdt;
187 };
188 
189 #define E1000_DESC_UNUSED(R)						\
190 ({									\
191 	unsigned int clean = smp_load_acquire(&(R)->next_to_clean);	\
192 	unsigned int use = READ_ONCE((R)->next_to_use);			\
193 	(clean > use ? 0 : (R)->count) + clean - use - 1;		\
194 })
195 
196 #define E1000_RX_DESC_EXT(R, i)						\
197 	(&(((union e1000_rx_desc_extended *)((R).desc))[i]))
198 #define E1000_GET_DESC(R, i, type)	(&(((struct type *)((R).desc))[i]))
199 #define E1000_RX_DESC(R, i)		E1000_GET_DESC(R, i, e1000_rx_desc)
200 #define E1000_TX_DESC(R, i)		E1000_GET_DESC(R, i, e1000_tx_desc)
201 #define E1000_CONTEXT_DESC(R, i)	E1000_GET_DESC(R, i, e1000_context_desc)
202 
203 /* board specific private data structure */
204 
205 struct e1000_adapter {
206 	unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
207 	u16 mng_vlan_id;
208 	u32 bd_number;
209 	u32 rx_buffer_len;
210 	u32 wol;
211 	u32 smartspeed;
212 	u32 en_mng_pt;
213 	u16 link_speed;
214 	u16 link_duplex;
215 	spinlock_t stats_lock;
216 	unsigned int total_tx_bytes;
217 	unsigned int total_tx_packets;
218 	unsigned int total_rx_bytes;
219 	unsigned int total_rx_packets;
220 	/* Interrupt Throttle Rate */
221 	u32 itr;
222 	u32 itr_setting;
223 	u16 tx_itr;
224 	u16 rx_itr;
225 
226 	u8 fc_autoneg;
227 
228 	/* TX */
229 	struct e1000_tx_ring *tx_ring;      /* One per active queue */
230 	unsigned int restart_queue;
231 	u32 txd_cmd;
232 	u32 tx_int_delay;
233 	u32 tx_abs_int_delay;
234 	u32 gotcl;
235 	u64 gotcl_old;
236 	u64 tpt_old;
237 	u64 colc_old;
238 	u32 tx_timeout_count;
239 	u32 tx_fifo_head;
240 	u32 tx_head_addr;
241 	u32 tx_fifo_size;
242 	u8  tx_timeout_factor;
243 	atomic_t tx_fifo_stall;
244 	bool pcix_82544;
245 	bool detect_tx_hung;
246 	bool dump_buffers;
247 
248 	/* RX */
249 	bool (*clean_rx)(struct e1000_adapter *adapter,
250 			 struct e1000_rx_ring *rx_ring,
251 			 int *work_done, int work_to_do);
252 	void (*alloc_rx_buf)(struct e1000_adapter *adapter,
253 			     struct e1000_rx_ring *rx_ring,
254 			     int cleaned_count);
255 	struct e1000_rx_ring *rx_ring;      /* One per active queue */
256 	struct napi_struct napi;
257 
258 	int num_tx_queues;
259 	int num_rx_queues;
260 
261 	u64 hw_csum_err;
262 	u64 hw_csum_good;
263 	u32 alloc_rx_buff_failed;
264 	u32 rx_int_delay;
265 	u32 rx_abs_int_delay;
266 	bool rx_csum;
267 	u32 gorcl;
268 	u64 gorcl_old;
269 
270 	/* OS defined structs */
271 	struct net_device *netdev;
272 	struct pci_dev *pdev;
273 
274 	/* structs defined in e1000_hw.h */
275 	struct e1000_hw hw;
276 	struct e1000_hw_stats stats;
277 	struct e1000_phy_info phy_info;
278 	struct e1000_phy_stats phy_stats;
279 
280 	u32 test_icr;
281 	struct e1000_tx_ring test_tx_ring;
282 	struct e1000_rx_ring test_rx_ring;
283 
284 	int msg_enable;
285 
286 	/* to not mess up cache alignment, always add to the bottom */
287 	bool tso_force;
288 	bool smart_power_down;	/* phy smart power down */
289 	bool quad_port_a;
290 	unsigned long flags;
291 	u32 eeprom_wol;
292 
293 	/* for ioport free */
294 	int bars;
295 	int need_ioport;
296 
297 	bool discarding;
298 
299 	struct work_struct reset_task;
300 	struct delayed_work watchdog_task;
301 	struct delayed_work fifo_stall_task;
302 	struct delayed_work phy_info_task;
303 };
304 
305 enum e1000_state_t {
306 	__E1000_TESTING,
307 	__E1000_RESETTING,
308 	__E1000_DOWN,
309 	__E1000_DISABLED
310 };
311 
312 #undef pr_fmt
313 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
314 
315 struct net_device *e1000_get_hw_dev(struct e1000_hw *hw);
316 #define e_dbg(format, arg...) \
317 	netdev_dbg(e1000_get_hw_dev(hw), format, ## arg)
318 #define e_err(msglvl, format, arg...) \
319 	netif_err(adapter, msglvl, adapter->netdev, format, ## arg)
320 #define e_info(msglvl, format, arg...) \
321 	netif_info(adapter, msglvl, adapter->netdev, format, ## arg)
322 #define e_warn(msglvl, format, arg...) \
323 	netif_warn(adapter, msglvl, adapter->netdev, format, ## arg)
324 #define e_notice(msglvl, format, arg...) \
325 	netif_notice(adapter, msglvl, adapter->netdev, format, ## arg)
326 #define e_dev_info(format, arg...) \
327 	dev_info(&adapter->pdev->dev, format, ## arg)
328 #define e_dev_warn(format, arg...) \
329 	dev_warn(&adapter->pdev->dev, format, ## arg)
330 #define e_dev_err(format, arg...) \
331 	dev_err(&adapter->pdev->dev, format, ## arg)
332 
333 extern char e1000_driver_name[];
334 extern const char e1000_driver_version[];
335 
336 int e1000_open(struct net_device *netdev);
337 int e1000_close(struct net_device *netdev);
338 int e1000_up(struct e1000_adapter *adapter);
339 void e1000_down(struct e1000_adapter *adapter);
340 void e1000_reinit_locked(struct e1000_adapter *adapter);
341 void e1000_reset(struct e1000_adapter *adapter);
342 int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx);
343 int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
344 int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
345 void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
346 void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
347 void e1000_update_stats(struct e1000_adapter *adapter);
348 bool e1000_has_link(struct e1000_adapter *adapter);
349 void e1000_power_up_phy(struct e1000_adapter *);
350 void e1000_set_ethtool_ops(struct net_device *netdev);
351 void e1000_check_options(struct e1000_adapter *adapter);
352 char *e1000_get_hw_dev_name(struct e1000_hw *hw);
353 
354 #endif /* _E1000_H_ */
355