1/*
2 * Copyright (c) 2008, ETH Zurich. All rights reserved.
3 *
4 * This file is distributed under the terms in the attached LICENSE file.
5 * If you do not find this file, copies can be found by writing to:
6 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
7 */
8
9#ifndef E1000_H__
10#define E1000_H__
11
12#include <barrelfish/barrelfish.h>
13#include <pci/pci_driver_client.h>
14
15#include <dev/e1000_dev.h>      /* auto generated by Mackerel */
16#include "e1000n_desc.h"
17#include "e1000n_debug.h"
18
19#define DRIVER_STRING "e1000: "
20
21/**
22 * Default message print format.
23 */
24#define E1000_PRINT(fmt, ...)        printf(DRIVER_STRING fmt, ##__VA_ARGS__)
25#define E1000_PRINT_ERROR(fmt, ...)  fprintf(stderr, DRIVER_STRING fmt, ##__VA_ARGS__)
26
27#define E1000_USE_LEGACY_DESC 1
28/*
29 * Global constants
30 */
31#define MAC_ADDRESS_LEN     6
32
33
34/*
35 * e1000 (e1000) device family id's.
36 */
37#define E1000_DEVICE_82542               0x1000
38#define E1000_DEVICE_82543GC_FIBER       0x1001
39#define E1000_DEVICE_82543GC_COPPER      0x1004
40#define E1000_DEVICE_82544EI_COPPER      0x1008
41#define E1000_DEVICE_82544EI_FIBER       0x1009
42#define E1000_DEVICE_82544GC_COPPER      0x100C
43#define E1000_DEVICE_82544GC_LOM         0x100D
44#define E1000_DEVICE_82540EM             0x100E
45#define E1000_DEVICE_82540EM_LOM         0x1015
46#define E1000_DEVICE_82540EP_LOM         0x1016
47#define E1000_DEVICE_82540EP             0x1017
48#define E1000_DEVICE_82540EP_LP          0x101E
49#define E1000_DEVICE_82545EM_COPPER      0x100F
50#define E1000_DEVICE_82545EM_FIBER       0x1011
51#define E1000_DEVICE_82545GM_COPPER      0x1026
52#define E1000_DEVICE_82545GM_FIBER       0x1027
53#define E1000_DEVICE_82545GM_SERDES      0x1028
54#define E1000_DEVICE_82546EB_COPPER      0x1010
55#define E1000_DEVICE_82546EB_FIBER       0x1012
56#define E1000_DEVICE_82546EB_QUAD_COPPER 0x101D
57#define E1000_DEVICE_82541EI             0x1013
58#define E1000_DEVICE_82541EI_MOBILE      0x1018
59#define E1000_DEVICE_82541ER_LOM         0x1014
60#define E1000_DEVICE_82541ER             0x1078
61#define E1000_DEVICE_82547GI             0x1075
62#define E1000_DEVICE_82541GI             0x1076
63#define E1000_DEVICE_82541GI_MOBILE      0x1077
64#define E1000_DEVICE_82541GI_LF          0x107C
65#define E1000_DEVICE_82546GB_COPPER      0x1079
66#define E1000_DEVICE_82546GB_FIBER       0x107A
67#define E1000_DEVICE_82546GB_SERDES      0x107B
68#define E1000_DEVICE_82546GB_PCIE        0x108A
69#define E1000_DEVICE_82546GB_QUAD_COPPER 0x1099
70#define E1000_DEVICE_82563EB             0x1096
71#define E1000_DEVICE_82547EI             0x1019
72#define E1000_DEVICE_82547EI_MOBILE      0x101A
73#define E1000_DEVICE_82571EB_COPPER      0x105E
74#define E1000_DEVICE_82571EB_FIBER       0x105F
75#define E1000_DEVICE_82571EB_SERDES      0x1060
76#define E1000_DEVICE_82571EB_QUAD_COPPER 0x10A4
77#define E1000_DEVICE_82571EB_QUAD_FIBER  0x10A5
78#define E1000_DEVICE_82571EB_QUAD_COPPER_LOWPROFILE  0x10BC
79#define E1000_DEVICE_82571EB_SERDES_DUAL 0x10D9
80#define E1000_DEVICE_82571EB_SERDES_QUAD 0x10DA
81#define E1000_DEVICE_82572EI_COPPER      0x107D
82#define E1000_DEVICE_82572EI_FIBER       0x107E
83#define E1000_DEVICE_82572EI_SERDES      0x107F
84#define E1000_DEVICE_82572EI             0x10B9
85#define E1000_DEVICE_82573E              0x108B
86#define E1000_DEVICE_82573E_IAMT         0x108C
87#define E1000_DEVICE_82573L              0x109A
88#define E1000_DEVICE_82574L              0x10D3
89#define E1000_DEVICE_82575EB             0x10A7 // TODO(gz): This cards needs more work
90#define E1000_DEVICE_82576EG             0x10C9 // TODO(gz): This cards needs more work
91#define E1000_DEVICE_I210                0x1533
92#define E1000_DEVICE_I219                0x15b8
93#define E1000_DEVICE_I350_EEPROM_LESS    0x151F
94#define E1000_DEVICE_I350_COPPER         0x1521
95#define E1000_DEVICE_I350_FIBER          0x1522
96#define E1000_DEVICE_I350_BACKPANE       0x1523
97#define E1000_DEVICE_I350_SGMII          0x1524
98#define E1000_DEVICE_I350_DUMMY          0x10A6
99#define E1000_DEVICE_82546GB_QUAD_COPPER_KSP3 0x10B5
100
101/**
102 * Fixed buffer sizes
103 */
104/* Transmit and receive buffers must be multiples of 8 */
105#define DRIVER_RECEIVE_BUFFERS      (1024 * 16)
106#define DRIVER_TRANSMIT_BUFFERS     (1024 * 16)
107
108/**
109 * Initial default values
110 */
111
112#define E1000_DEFAULT_INT_THROTTLE_RATE 130
113#define E1000_INT_THROTTLE_RATE_DISABLED 0
114
115/**
116 * Group definitions for cards that share specification and quirks.
117 *
118 * e1000_82542 should be split into:
119 *   e1000_82542_rev_2_1 and e1000_82542_rev_2_2.
120 * This can be figured out reading the PCI bus.
121 */
122typedef enum {
123    e1000_undefined = 0,
124    e1000_82542,        /* revision 2.1 and 2.2 merged */
125    e1000_82543,
126    e1000_82544,
127    e1000_82540,
128    e1000_82545,
129    e1000_82545_rev_3,
130    e1000_82546,
131    e1000_82546_rev_3,
132    e1000_82541,
133    e1000_82541_rev_2,
134    e1000_82547,
135    e1000_82547_rev_2,
136    e1000_82563,
137    e1000_82571,
138    e1000_82572,
139    e1000_82573,
140    e1000_82574,
141    e1000_82575,
142    e1000_82576,
143    e1000_I210,
144    e1000_I219,
145    e1000_I350,
146    e1000_num_macs
147} e1000_mac_type_t;
148
149
150/**
151 * Hardware supported buffer sizes.
152 */
153typedef enum {
154    bsize_256 = 256,
155    bsize_512 = 512,
156    bsize_1024 = 1024,
157    bsize_2048 = 2048,
158    bsize_4096 = 4096,
159    bsize_8192 = 8192,
160    bsize_16384 = 16384
161} e1000_rx_bsize_t;
162
163/**
164 * Media types.
165 */
166typedef enum {
167    e1000_media_type_undefined,
168    e1000_media_type_fiber,
169    e1000_media_type_copper,
170    e1000_media_type_serdes,
171    e1000_num_media_types
172} e1000_media_type_t;
173
174
175/**
176 * Data-structure to map sent buffer slots back to application slots
177 */
178struct pbuf_desc {
179    void *opaque;
180};
181
182/**
183 * Per device state
184 */
185struct e1000_driver_state {
186    /* Kaluga args */
187    char **args;
188    int args_len;
189    char *inst_name;
190
191    struct pcid pdc;
192
193    /* Internal device info */
194    e1000_media_type_t media_type;
195    e1000_mac_type_t mac_type;
196    bool tbi_combaility;
197    uint32_t level;
198
199    bool user_mac_address; /* True if the user specified the MAC address */
200    bool use_interrupt; /* don't use card polling mode */
201    bool use_force; /* don't attempt to find card force load */
202    bool initialized;
203    bool queue_init_done;
204
205    bool msix;
206    bool extended_interrupts;
207    unsigned advanced_descriptors; // 0 - none, 1 - 82572/4, 3 - 82576/i210/i350
208
209    struct capref regs;
210
211    /* e1000 states */
212    e1000_t device_inst;
213    e1000_t *device; //pointer to device_inst
214    uint8_t mac_address[MAC_ADDRESS_LEN]; /* buffers the card's MAC address upon card reset */
215
216    /* For use with the net_queue_manager */
217    char *service_name;
218    uint64_t assumed_queue_id;   /* what net queue to bind to */
219    uint32_t ether_transmit_index;
220    uint32_t ether_transmit_bufptr;
221
222    /* RAM affinity */
223    uint64_t minbase;
224    uint64_t maxbase;
225
226    /* transmit */
227    volatile struct tx_desc *transmit_ring; //set by _hwinit
228    struct pbuf_desc pbuf_list_tx[DRIVER_TRANSMIT_BUFFERS];
229    size_t transmit_buffers;
230
231    /* receive */
232    e1000_rx_bsize_t rx_bsize;
233    volatile union rx_desc *receive_ring; //set by _hwinit
234    uint32_t receive_bufptr;
235    uint32_t receive_index;
236    uint32_t receive_free;
237    void **receive_opaque;
238    size_t receive_buffers;
239
240    /* irqtest statistics */
241#ifdef UNDER_TEST
242    int int_trigger_counter;
243    int test_initialized;
244    int lsc_interrupt_counter;
245    uint64_t last_int_trigger_ticks;
246#endif
247};
248
249void e1000_driver_state_init(struct e1000_driver_state * eds);
250
251
252bool e1000_supports_msix(e1000_mac_type_t mt);
253e1000_mac_type_t e1000_get_mac_type(uint32_t vendor, uint32_t device_id);
254char * e1000_mac_type_to_str(e1000_mac_type_t mt);
255bool e1000_supported_device(uint32_t vendor, uint32_t device_id);
256bool e1000_link_up_led_status(struct e1000_driver_state *eds);
257bool e1000_check_link_up(e1000_t *device);
258bool e1000_auto_negotiate_link(e1000_t *device, e1000_mac_type_t mac);
259void e1000_set_interrupt_throttle(struct e1000_driver_state *eds, uint16_t usec);
260
261void e1000_hwinit(struct e1000_driver_state *eds);
262
263void e1000_init_queues(struct e1000_driver_state* eds, struct capref rx,
264                       size_t rx_bufs, struct capref tx, size_t tx_bufs);
265
266/*****************************************************************
267 * On the i82541xx GPI_SP2 and GPI_SP3 are merged into one register
268 * value of bits.
269 *
270 ****************************************************************/
271static inline uint8_t i82541xx_get_icr_gpi_sdp(struct e1000_driver_state *eds)
272{
273    e1000_intreg_t intreg = e1000_icr_rawrd(eds->device);
274    uint8_t sdp2 = e1000_intreg_gpi_sdp2_extract(intreg);
275    uint8_t sdp3 = e1000_intreg_gpi_sdp3_extract(intreg);
276    uint8_t sdp = sdp2 | (sdp3 << 1);
277    return sdp;
278}
279
280
281/*****************************************************************
282 * Barrelfish has no delay. We do it like this instead.
283 ****************************************************************/
284
285#include <barrelfish/sys_debug.h>
286
287#if 0
288/* apparently this does not work... getting usertrap #13 */
289extern cycles_t tscperms;
290
291static inline void usec_delay(unsigned int ms)
292{
293    if (tscperms == 0) {
294        errval_t err = sys_debug_get_tsc_per_ms(&tscperms);
295        assert(err_is_ok(err));
296    }
297    cycles_t end = (cycles_t)ms * tscperms + rdtsc();
298    while(rdtsc() < end) {
299        thread_yield();
300    }
301
302}
303#else
304static inline void usec_delay(unsigned int count)
305{
306    while(count--) {
307        __asm__ __volatile__("inb $0x80, %b0" :: "a"(0));
308    }
309}
310#endif
311
312#endif
313