Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / net / intel.h
1 #ifndef _INTEL_H
2 #define _INTEL_H
3
4 /** @file
5  *
6  * Intel 10/100/1000 network card driver
7  *
8  */
9
10 FILE_LICENCE ( GPL2_OR_LATER );
11
12 #include <stdint.h>
13 #include <ipxe/if_ether.h>
14 #include <ipxe/nvs.h>
15
16 /** Intel BAR size */
17 #define INTEL_BAR_SIZE ( 128 * 1024 )
18
19 /** A packet descriptor */
20 struct intel_descriptor {
21         /** Buffer address */
22         uint64_t address;
23         /** Length */
24         uint16_t length;
25         /** Reserved */
26         uint8_t reserved_a;
27         /** Command */
28         uint8_t command;
29         /** Status */
30         uint8_t status;
31         /** Errors */
32         uint8_t errors;
33         /** Reserved */
34         uint16_t reserved_b;
35 } __attribute__ (( packed ));
36
37 /** Packet descriptor command bits */
38 enum intel_descriptor_command {
39         /** Report status */
40         INTEL_DESC_CMD_RS = 0x08,
41         /** Insert frame checksum (CRC) */
42         INTEL_DESC_CMD_IFCS = 0x02,
43         /** End of packet */
44         INTEL_DESC_CMD_EOP = 0x01,
45 };
46
47 /** Packet descriptor status bits */
48 enum intel_descriptor_status {
49         /** Descriptor done */
50         INTEL_DESC_STATUS_DD = 0x01,
51 };
52
53 /** Device Control Register */
54 #define INTEL_CTRL 0x00000UL
55 #define INTEL_CTRL_LRST         0x00000008UL    /**< Link reset */
56 #define INTEL_CTRL_ASDE         0x00000020UL    /**< Auto-speed detection */
57 #define INTEL_CTRL_SLU          0x00000040UL    /**< Set link up */
58 #define INTEL_CTRL_FRCSPD       0x00000800UL    /**< Force speed */
59 #define INTEL_CTRL_FRCDPLX      0x00001000UL    /**< Force duplex */
60 #define INTEL_CTRL_RST          0x04000000UL    /**< Device reset */
61 #define INTEL_CTRL_PHY_RST      0x80000000UL    /**< PHY reset */
62
63 /** Time to delay for device reset, in milliseconds */
64 #define INTEL_RESET_DELAY_MS 20
65
66 /** Device Status Register */
67 #define INTEL_STATUS 0x00008UL
68 #define INTEL_STATUS_LU         0x00000002UL    /**< Link up */
69
70 /** EEPROM Read Register */
71 #define INTEL_EERD 0x00014UL
72 #define INTEL_EERD_START        0x00000001UL    /**< Start read */
73 #define INTEL_EERD_DONE_SMALL   0x00000010UL    /**< Read done (small EERD) */
74 #define INTEL_EERD_DONE_LARGE   0x00000002UL    /**< Read done (large EERD) */
75 #define INTEL_EERD_ADDR_SHIFT_SMALL 8           /**< Address shift (small) */
76 #define INTEL_EERD_ADDR_SHIFT_LARGE 2           /**< Address shift (large) */
77 #define INTEL_EERD_DATA(value)  ( (value) >> 16 ) /**< Read data */
78
79 /** Maximum time to wait for EEPROM read, in milliseconds */
80 #define INTEL_EEPROM_MAX_WAIT_MS 100
81
82 /** EEPROM word length */
83 #define INTEL_EEPROM_WORD_LEN_LOG2 1
84
85 /** Minimum EEPROM size, in words */
86 #define INTEL_EEPROM_MIN_SIZE_WORDS 64
87
88 /** Offset of MAC address within EEPROM */
89 #define INTEL_EEPROM_MAC 0x00
90
91 /** Interrupt Cause Read Register */
92 #define INTEL_ICR 0x000c0UL
93 #define INTEL_IRQ_TXDW          0x00000001UL    /**< Transmit descriptor done */
94 #define INTEL_IRQ_LSC           0x00000004UL    /**< Link status change */
95 #define INTEL_IRQ_RXT0          0x00000080UL    /**< Receive timer */
96 #define INTEL_IRQ_RXO           0x00000400UL    /**< Receive overrun */
97
98 /** Interrupt Mask Set/Read Register */
99 #define INTEL_IMS 0x000d0UL
100
101 /** Interrupt Mask Clear Register */
102 #define INTEL_IMC 0x000d8UL
103
104 /** Receive Control Register */
105 #define INTEL_RCTL 0x00100UL
106 #define INTEL_RCTL_EN           0x00000002UL    /**< Receive enable */
107 #define INTEL_RCTL_UPE          0x00000008UL    /**< Unicast promiscuous mode */
108 #define INTEL_RCTL_MPE          0x00000010UL    /**< Multicast promiscuous */
109 #define INTEL_RCTL_BAM          0x00008000UL    /**< Broadcast accept mode */
110 #define INTEL_RCTL_BSIZE_BSEX(bsex,bsize) \
111         ( ( (bsize) << 16 ) | ( (bsex) << 25 ) ) /**< Buffer size */
112 #define INTEL_RCTL_BSIZE_2048   INTEL_RCTL_BSIZE_BSEX ( 0, 0 )
113 #define INTEL_RCTL_BSIZE_BSEX_MASK INTEL_RCTL_BSIZE_BSEX ( 1, 3 )
114 #define INTEL_RCTL_SECRC        0x04000000UL    /**< Strip CRC */
115
116 /** Transmit Control Register */
117 #define INTEL_TCTL 0x00400UL
118 #define INTEL_TCTL_EN           0x00000002UL    /**< Transmit enable */
119 #define INTEL_TCTL_PSP          0x00000008UL    /**< Pad short packets */
120 #define INTEL_TCTL_CT(x)        ( (x) << 4 )    /**< Collision threshold */
121 #define INTEL_TCTL_CT_DEFAULT   INTEL_TCTL_CT ( 0x0f )
122 #define INTEL_TCTL_CT_MASK      INTEL_TCTL_CT ( 0xff )
123 #define INTEL_TCTL_COLD(x)      ( (x) << 12 )   /**< Collision distance */
124 #define INTEL_TCTL_COLD_DEFAULT INTEL_TCTL_COLD ( 0x040 )
125 #define INTEL_TCTL_COLD_MASK    INTEL_TCTL_COLD ( 0x3ff )
126
127 /** Packet Buffer Allocation */
128 #define INTEL_PBA 0x01000UL
129
130 /** Packet Buffer Size */
131 #define INTEL_PBS 0x01008UL
132
133 /** Receive Descriptor register block */
134 #define INTEL_RD 0x02800UL
135
136 /** Number of receive descriptors
137  *
138  * Minimum value is 8, since the descriptor ring length must be a
139  * multiple of 128.
140  */
141 #define INTEL_NUM_RX_DESC 16
142
143 /** Receive descriptor ring fill level */
144 #define INTEL_RX_FILL 8
145
146 /** Receive buffer length */
147 #define INTEL_RX_MAX_LEN 2048
148
149 /** Transmit Descriptor register block */
150 #define INTEL_TD 0x03800UL
151
152 /** Number of transmit descriptors
153  *
154  * Descriptor ring length must be a multiple of 16.  ICH8/9/10
155  * requires a minimum of 16 TX descriptors.
156  */
157 #define INTEL_NUM_TX_DESC 16
158
159 /** Transmit descriptor ring maximum fill level */
160 #define INTEL_TX_FILL ( INTEL_NUM_TX_DESC - 1 )
161
162 /** Receive/Transmit Descriptor Base Address Low (offset) */
163 #define INTEL_xDBAL 0x00
164
165 /** Receive/Transmit Descriptor Base Address High (offset) */
166 #define INTEL_xDBAH 0x04
167
168 /** Receive/Transmit Descriptor Length (offset) */
169 #define INTEL_xDLEN 0x08
170
171 /** Receive/Transmit Descriptor Head (offset) */
172 #define INTEL_xDH 0x10
173
174 /** Receive/Transmit Descriptor Tail (offset) */
175 #define INTEL_xDT 0x18
176
177 /** Receive/Transmit Descriptor Control (offset) */
178 #define INTEL_xDCTL 0x28
179 #define INTEL_xDCTL_ENABLE      0x02000000UL    /**< Queue enable */
180
181 /** Receive Address Low */
182 #define INTEL_RAL0 0x05400UL
183
184 /** Receive Address High */
185 #define INTEL_RAH0 0x05404UL
186 #define INTEL_RAH0_AV           0x80000000UL    /**< Address valid */
187
188 /** Receive address */
189 union intel_receive_address {
190         struct {
191                 uint32_t low;
192                 uint32_t high;
193         } __attribute__ (( packed )) reg;
194         uint8_t raw[ETH_ALEN];
195 };
196
197 /** An Intel descriptor ring */
198 struct intel_ring {
199         /** Descriptors */
200         struct intel_descriptor *desc;
201         /** Producer index */
202         unsigned int prod;
203         /** Consumer index */
204         unsigned int cons;
205
206         /** Register block */
207         unsigned int reg;
208         /** Length (in bytes) */
209         size_t len;
210 };
211
212 /**
213  * Initialise descriptor ring
214  *
215  * @v ring              Descriptor ring
216  * @v count             Number of descriptors
217  * @v reg               Descriptor register block
218  */
219 static inline __attribute__ (( always_inline)) void
220 intel_init_ring ( struct intel_ring *ring, unsigned int count,
221                   unsigned int reg ) {
222         ring->len = ( count * sizeof ( ring->desc[0] ) );
223         ring->reg = reg;
224 }
225
226 /** An Intel network card */
227 struct intel_nic {
228         /** Registers */
229         void *regs;
230         /** Port number (for multi-port devices) */
231         unsigned int port;
232         /** Flags */
233         unsigned int flags;
234
235         /** EEPROM */
236         struct nvs_device eeprom;
237         /** EEPROM done flag */
238         uint32_t eerd_done;
239         /** EEPROM address shift */
240         unsigned int eerd_addr_shift;
241
242         /** Transmit descriptor ring */
243         struct intel_ring tx;
244         /** Receive descriptor ring */
245         struct intel_ring rx;
246         /** Receive I/O buffers */
247         struct io_buffer *rx_iobuf[INTEL_NUM_RX_DESC];
248 };
249
250 /** Driver flags */
251 enum intel_flags {
252         /** PBS/PBA errata workaround required */
253         INTEL_PBS_ERRATA = 0x0001,
254 };
255
256 extern int intel_create_ring ( struct intel_nic *intel,
257                                struct intel_ring *ring );
258 extern void intel_destroy_ring ( struct intel_nic *intel,
259                                  struct intel_ring *ring );
260 extern void intel_refill_rx ( struct intel_nic *intel );
261 extern void intel_empty_rx ( struct intel_nic *intel );
262 extern int intel_transmit ( struct net_device *netdev,
263                             struct io_buffer *iobuf );
264 extern void intel_poll_tx ( struct net_device *netdev );
265 extern void intel_poll_rx ( struct net_device *netdev );
266
267 #endif /* _INTEL_H */