3.1.7 branch.
[minix.git] / drivers / e1000 / e1000.c
blob0e1caacd08971d85a76a96697b7dd7ca772567c4
1 /**
2 * @file e1000.c
4 * @brief This file contains a device driver for Intel Pro/1000
5 * Gigabit Ethernet Controllers.
6 */
8 #include <minix/drivers.h>
9 #include <minix/netdriver.h>
10 #include <stdlib.h>
11 #include <net/gen/ether.h>
12 #include <net/gen/eth_io.h>
13 #include <machine/pci.h>
14 #include <minix/ds.h>
15 #include <minix/vm.h>
16 #include <timers.h>
17 #include "assert.h"
18 #include "e1000.h"
19 #include "e1000_hw.h"
20 #include "e1000_reg.h"
21 #include "e1000_pci.h"
23 PRIVATE u16_t pcitab_e1000[] =
25 E1000_DEV_ID_82540EM,
26 E1000_DEV_ID_82541GI_LF,
27 E1000_DEV_ID_ICH10_R_BM_LF,
31 PRIVATE int e1000_instance;
32 PRIVATE e1000_t e1000_state;
34 _PROTOTYPE( PRIVATE void e1000_init, (message *mp) );
35 _PROTOTYPE( PRIVATE void e1000_init_pci, (void) );
36 _PROTOTYPE( PRIVATE int e1000_probe, (e1000_t *e, int skip) );
37 _PROTOTYPE( PRIVATE int e1000_init_hw, (e1000_t *e) );
38 _PROTOTYPE( PRIVATE void e1000_init_addr, (e1000_t *e) );
39 _PROTOTYPE( PRIVATE void e1000_init_buf, (e1000_t *e) );
40 _PROTOTYPE( PRIVATE void e1000_reset_hw, (e1000_t *e) );
41 _PROTOTYPE( PRIVATE void e1000_writev_s, (message *mp, int from_int) );
42 _PROTOTYPE( PRIVATE void e1000_readv_s, (message *mp, int from_int) );
43 _PROTOTYPE( PRIVATE void e1000_getstat_s, (message *mp) );
44 _PROTOTYPE( PRIVATE void e1000_interrupt, (message *mp) );
45 _PROTOTYPE( PRIVATE int e1000_link_changed, (e1000_t *e) );
46 _PROTOTYPE( PRIVATE void e1000_stop, (void) );
47 _PROTOTYPE( PRIVATE uint32_t e1000_reg_read, (e1000_t *e, uint32_t reg) );
48 _PROTOTYPE( PRIVATE void e1000_reg_write, (e1000_t *e, uint32_t reg,
49 uint32_t value) );
50 _PROTOTYPE( PRIVATE void e1000_reg_set, (e1000_t *e, uint32_t reg,
51 uint32_t value) );
52 _PROTOTYPE( PRIVATE void e1000_reg_unset, (e1000_t *e, uint32_t reg,
53 uint32_t value) );
54 _PROTOTYPE( PRIVATE u16_t eeprom_eerd, (void *e, int reg) );
55 _PROTOTYPE( PRIVATE u16_t eeprom_ich, (void *e, int reg) );
56 _PROTOTYPE( PRIVATE int eeprom_ich_init, (e1000_t *e) );
57 _PROTOTYPE( PRIVATE int eeprom_ich_cycle, (const e1000_t *e, u32_t timeout) );
58 _PROTOTYPE( PRIVATE void reply, (e1000_t *e) );
59 _PROTOTYPE( PRIVATE void mess_reply, (message *req, message *reply) );
61 /* SEF functions and variables. */
62 FORWARD _PROTOTYPE( void sef_local_startup, (void) );
63 FORWARD _PROTOTYPE( int sef_cb_init_fresh, (int type, sef_init_info_t *info) );
64 FORWARD _PROTOTYPE( void sef_cb_signal_handler, (int signo) );
65 EXTERN int env_argc;
66 EXTERN char **env_argv;
68 /*===========================================================================*
69 * main *
70 *===========================================================================*/
71 int main(int argc, char *argv[])
73 message m;
74 int ipc_status;
75 int r;
77 /* SEF local startup. */
78 env_setargs(argc, argv);
79 sef_local_startup();
82 * Enter the main driver loop.
84 while (TRUE)
86 if ((r= netdriver_receive(ANY, &m, &ipc_status)) != OK)
88 panic("netdriver_receive failed: %d", r);
91 if (is_ipc_notify(ipc_status))
93 switch (_ENDPOINT_P(m.m_source))
95 case HARDWARE:
96 e1000_interrupt(&m);
97 break;
99 case CLOCK:
100 break;
102 continue;
104 switch (m.m_type)
106 case DL_WRITEV_S: e1000_writev_s(&m, FALSE); break;
107 case DL_READV_S: e1000_readv_s(&m, FALSE); break;
108 case DL_CONF: e1000_init(&m); break;
109 case DL_GETSTAT_S: e1000_getstat_s(&m); break;
110 default:
111 panic("illegal message: %d", m.m_type);
116 /*===========================================================================*
117 * sef_local_startup *
118 *===========================================================================*/
119 PRIVATE void sef_local_startup()
121 /* Register init callbacks. */
122 sef_setcb_init_fresh(sef_cb_init_fresh);
123 sef_setcb_init_lu(sef_cb_init_fresh);
124 sef_setcb_init_restart(sef_cb_init_fresh);
126 /* Register live update callbacks. */
127 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready);
128 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_workfree);
130 /* Register signal callbacks. */
131 sef_setcb_signal_handler(sef_cb_signal_handler);
133 /* Let SEF perform startup. */
134 sef_startup();
137 /*===========================================================================*
138 * sef_cb_init_fresh *
139 *===========================================================================*/
140 PRIVATE int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
142 /* Initialize the e1000 driver. */
143 long v;
144 int r;
146 v = 0;
147 (void) env_parse("instance", "d", 0, &v, 0, 255);
148 e1000_instance = (int) v;
150 /* Clear state. */
151 memset(&e1000_state, 0, sizeof(e1000_state));
153 /* Perform calibration. */
154 if((r = tsc_calibrate()) != OK)
156 panic("tsc_calibrate failed: %d", r);
159 /* Announce we are up! */
160 netdriver_announce();
162 return(OK);
165 /*===========================================================================*
166 * sef_cb_signal_handler *
167 *===========================================================================*/
168 PRIVATE void sef_cb_signal_handler(int signo)
170 E1000_DEBUG(3, ("e1000: got signal\n"));
172 /* Only check for termination signal, ignore anything else. */
173 if (signo != SIGTERM) return;
175 e1000_stop();
178 /*===========================================================================*
179 * e1000_init *
180 *===========================================================================*/
181 PRIVATE void e1000_init(message *mp)
183 static int first_time = 1;
184 message reply_mess;
185 e1000_t *e;
187 E1000_DEBUG(3, ("e1000: init()\n"));
189 /* Configure PCI devices, if needed. */
190 if (first_time)
192 first_time = 0;
193 e1000_init_pci();
195 e = &e1000_state;
197 /* Initialize hardware, if needed. */
198 if (!(e->status & E1000_ENABLED) && !(e1000_init_hw(e)))
200 reply_mess.m_type = DL_CONF_REPLY;
201 reply_mess.DL_STAT = ENXIO;
202 mess_reply(mp, &reply_mess);
203 return;
205 /* Reply back to INET. */
206 reply_mess.m_type = DL_CONF_REPLY;
207 reply_mess.DL_STAT = OK;
208 *(ether_addr_t *) reply_mess.DL_HWADDR = e->address;
209 mess_reply(mp, &reply_mess);
212 /*===========================================================================*
213 * e1000_int_pci *
214 *===========================================================================*/
215 PRIVATE void e1000_init_pci()
217 e1000_t *e;
219 /* Initialize the PCI bus. */
220 pci_init();
222 /* Try to detect e1000's. */
223 e = &e1000_state;
224 strcpy(e->name, "e1000#0");
225 e->name[6] += e1000_instance;
226 e1000_probe(e, e1000_instance);
229 /*===========================================================================*
230 * e1000_probe *
231 *===========================================================================*/
232 PRIVATE int e1000_probe(e1000_t *e, int skip)
234 int i, r, devind;
235 u16_t vid, did;
236 u32_t status[2];
237 u32_t gfpreg, sector_base_addr;
238 char *dname;
240 E1000_DEBUG(3, ("%s: probe()\n", e->name));
243 * Attempt to iterate the PCI bus. Start at the beginning.
245 if ((r = pci_first_dev(&devind, &vid, &did)) == 0)
247 return FALSE;
249 /* Loop devices on the PCI bus. */
250 for(;;)
252 for (i = 0; pcitab_e1000[i] != 0; i++)
254 if (vid != 0x8086)
255 continue;
257 if (did != pcitab_e1000[i])
258 continue;
259 else
260 break;
262 if (pcitab_e1000[i] != 0)
264 if (!skip)
265 break;
266 skip--;
269 if (!(r = pci_next_dev(&devind, &vid, &did)))
271 return FALSE;
275 * Successfully detected an Intel Pro/1000 on the PCI bus.
277 e->status |= E1000_DETECTED;
278 e->eeprom_read = eeprom_eerd;
281 * Set card specific properties.
283 switch (did)
285 case E1000_DEV_ID_ICH10_R_BM_LF:
286 e->eeprom_read = eeprom_ich;
287 break;
289 case E1000_DEV_ID_82541GI_LF:
290 e->eeprom_done_bit = (1 << 1);
291 e->eeprom_addr_off = 2;
292 break;
294 default:
295 e->eeprom_done_bit = (1 << 4);
296 e->eeprom_addr_off = 8;
297 break;
300 /* Inform the user about the new card. */
301 if (!(dname = pci_dev_name(vid, did)))
303 dname = "Intel Pro/1000 Gigabit Ethernet Card";
305 E1000_DEBUG(1, ("%s: %s (%04x/%04x/%02x) at %s\n",
306 e->name, dname, vid, did, e->revision,
307 pci_slot_name(devind)));
309 /* Reserve PCI resources found. */
310 if ((r = pci_reserve_ok(devind)) != OK)
312 panic("failed to reserve PCI device: %d", r);
314 /* Read PCI configuration. */
315 e->irq = pci_attr_r8(devind, PCI_ILR);
316 e->regs = vm_map_phys(SELF, (void *) pci_attr_r32(devind, PCI_BAR),
317 0x20000);
319 /* Verify mapped registers. */
320 if (e->regs == (u8_t *) -1) {
321 panic("failed to map hardware registers from PCI");
323 /* Optionally map flash memory. */
324 if (pci_attr_r32(devind, PCI_BAR_3))
326 e->flash = vm_map_phys(SELF, (void *) pci_attr_r32(devind, PCI_BAR_2),
327 0x10000);
329 gfpreg = E1000_READ_FLASH_REG(e, ICH_FLASH_GFPREG);
331 * sector_base_addr is a "sector"-aligned address (4096 bytes)
333 sector_base_addr = gfpreg & FLASH_GFPREG_BASE_MASK;
335 /* flash_base_addr is byte-aligned */
336 e->flash_base_addr = sector_base_addr << FLASH_SECTOR_ADDR_SHIFT;
339 * Output debug information.
341 status[0] = e1000_reg_read(e, E1000_REG_STATUS);
342 E1000_DEBUG(3, ("%s: MEM at %p, IRQ %d\n",
343 e->name, e->regs, e->irq));
344 E1000_DEBUG(3, ("%s: link %s, %s duplex\n",
345 e->name, status[0] & 3 ? "up" : "down",
346 status[0] & 1 ? "full" : "half"));
347 return TRUE;
350 /*===========================================================================*
351 * e1000_init_hw *
352 *===========================================================================*/
353 PRIVATE int e1000_init_hw(e)
354 e1000_t *e;
356 int r, i;
358 e->status |= E1000_ENABLED;
359 e->irq_hook = e->irq;
362 * Set the interrupt handler and policy. Do not automatically
363 * re-enable interrupts. Return the IRQ line number on interrupts.
365 if ((r = sys_irqsetpolicy(e->irq, 0, &e->irq_hook)) != OK)
367 panic("sys_irqsetpolicy failed: %d", r);
369 if ((r = sys_irqenable(&e->irq_hook)) != OK)
371 panic("sys_irqenable failed: %d", r);
373 /* Reset hardware. */
374 e1000_reset_hw(e);
377 * Initialize appropriately, according to section 14.3 General Configuration
378 * of Intel's Gigabit Ethernet Controllers Software Developer's Manual.
380 e1000_reg_set(e, E1000_REG_CTRL, E1000_REG_CTRL_ASDE | E1000_REG_CTRL_SLU);
381 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_LRST);
382 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_PHY_RST);
383 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_ILOS);
384 e1000_reg_write(e, E1000_REG_FCAL, 0);
385 e1000_reg_write(e, E1000_REG_FCAH, 0);
386 e1000_reg_write(e, E1000_REG_FCT, 0);
387 e1000_reg_write(e, E1000_REG_FCTTV, 0);
388 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_VME);
390 /* Clear Multicast Table Array (MTA). */
391 for (i = 0; i < 128; i++)
393 e1000_reg_write(e, E1000_REG_MTA + i, 0);
395 /* Initialize statistics registers. */
396 for (i = 0; i < 64; i++)
398 e1000_reg_write(e, E1000_REG_CRCERRS + (i * 4), 0);
401 * Aquire MAC address and setup RX/TX buffers.
403 e1000_init_addr(e);
404 e1000_init_buf(e);
406 /* Enable interrupts. */
407 e1000_reg_set(e, E1000_REG_IMS, E1000_REG_IMS_LSC |
408 E1000_REG_IMS_RXO |
409 E1000_REG_IMS_RXT |
410 E1000_REG_IMS_TXQE |
411 E1000_REG_IMS_TXDW);
412 return TRUE;
415 /*===========================================================================*
416 * e1000_init_addr *
417 *===========================================================================*/
418 PRIVATE void e1000_init_addr(e)
419 e1000_t *e;
421 static char eakey[]= E1000_ENVVAR "#_EA";
422 static char eafmt[]= "x:x:x:x:x:x";
423 u16_t word;
424 int i;
425 long v;
428 * Do we have a user defined ethernet address?
430 eakey[sizeof(E1000_ENVVAR)-1] = '0' + e1000_instance;
432 for (i= 0; i < 6; i++)
434 if (env_parse(eakey, eafmt, i, &v, 0x00L, 0xFFL) != EP_SET)
435 break;
436 else
437 e->address.ea_addr[i]= v;
440 * If that fails, read Ethernet Address from EEPROM.
442 if (i != 6)
444 for (i = 0; i < 3; i++)
446 word = e->eeprom_read(e, i);
447 e->address.ea_addr[(i * 2)] = (word & 0xff);
448 e->address.ea_addr[(i * 2) + 1] = (word & 0xff00) >> 8;
452 * Set Receive Address.
454 e1000_reg_write(e, E1000_REG_RAL, *(u32_t *)(&e->address.ea_addr[0]));
455 e1000_reg_write(e, E1000_REG_RAH, *(u16_t *)(&e->address.ea_addr[4]));
456 e1000_reg_set(e, E1000_REG_RAH, E1000_REG_RAH_AV);
457 e1000_reg_set(e, E1000_REG_RCTL, E1000_REG_RCTL_MPE);
459 E1000_DEBUG(3, ("%s: Ethernet Address %x:%x:%x:%x:%x:%x\n", e->name,
460 e->address.ea_addr[0], e->address.ea_addr[1],
461 e->address.ea_addr[2], e->address.ea_addr[3],
462 e->address.ea_addr[4], e->address.ea_addr[5]));
465 /*===========================================================================*
466 * e1000_init_buf *
467 *===========================================================================*/
468 PRIVATE void e1000_init_buf(e)
469 e1000_t *e;
471 phys_bytes rx_desc_p, rx_buff_p;
472 phys_bytes tx_desc_p, tx_buff_p;
473 int i;
475 /* Number of descriptors. */
476 e->rx_desc_count = E1000_RXDESC_NR;
477 e->tx_desc_count = E1000_TXDESC_NR;
480 * First, allocate the receive descriptors.
482 if (!e->rx_desc)
484 if ((e->rx_desc = alloc_contig(sizeof(e1000_rx_desc_t) *
485 e->rx_desc_count, AC_ALIGN4K,
486 &rx_desc_p)) == NULL) {
487 panic("failed to allocate RX descriptors");
489 memset(e->rx_desc, 0, sizeof(e1000_rx_desc_t) * e->rx_desc_count);
492 * Allocate 2048-byte buffers.
494 e->rx_buffer_size = E1000_RXDESC_NR * E1000_IOBUF_SIZE;
496 /* Attempt to allocate. */
497 if ((e->rx_buffer = alloc_contig(e->rx_buffer_size,
498 AC_ALIGN4K, &rx_buff_p)) == NULL)
500 panic("failed to allocate RX buffers");
502 /* Setup receive descriptors. */
503 for (i = 0; i < E1000_RXDESC_NR; i++)
505 e->rx_desc[i].buffer = rx_buff_p + (i * E1000_IOBUF_SIZE);
509 * Then, allocate transmit descriptors.
511 if (!e->tx_desc)
513 if ((e->tx_desc = alloc_contig(sizeof(e1000_tx_desc_t) *
514 e->tx_desc_count, AC_ALIGN4K,
515 &tx_desc_p)) == NULL) {
516 panic("failed to allocate TX descriptors");
518 memset(e->tx_desc, 0, sizeof(e1000_tx_desc_t) * e->tx_desc_count);
521 * Allocate 2048-byte buffers.
523 e->tx_buffer_size = E1000_TXDESC_NR * E1000_IOBUF_SIZE;
525 /* Attempt to allocate. */
526 if ((e->tx_buffer = alloc_contig(e->tx_buffer_size,
527 AC_ALIGN4K, &tx_buff_p)) == NULL)
529 panic("failed to allocate TX buffers");
531 /* Setup transmit descriptors. */
532 for (i = 0; i < E1000_RXDESC_NR; i++)
534 e->tx_desc[i].buffer = tx_buff_p + (i * E1000_IOBUF_SIZE);
538 * Setup the receive ring registers.
540 e1000_reg_write(e, E1000_REG_RDBAL, rx_desc_p);
541 e1000_reg_write(e, E1000_REG_RDBAH, 0);
542 e1000_reg_write(e, E1000_REG_RDLEN, e->rx_desc_count *
543 sizeof(e1000_rx_desc_t));
544 e1000_reg_write(e, E1000_REG_RDH, 0);
545 e1000_reg_write(e, E1000_REG_RDT, e->rx_desc_count - 1);
546 e1000_reg_unset(e, E1000_REG_RCTL, E1000_REG_RCTL_BSIZE);
547 e1000_reg_set(e, E1000_REG_RCTL, E1000_REG_RCTL_EN);
550 * Setup the transmit ring registers.
552 e1000_reg_write(e, E1000_REG_TDBAL, tx_desc_p);
553 e1000_reg_write(e, E1000_REG_TDBAH, 0);
554 e1000_reg_write(e, E1000_REG_TDLEN, e->tx_desc_count *
555 sizeof(e1000_tx_desc_t));
556 e1000_reg_write(e, E1000_REG_TDH, 0);
557 e1000_reg_write(e, E1000_REG_TDT, 0);
558 e1000_reg_set( e, E1000_REG_TCTL, E1000_REG_TCTL_EN | E1000_REG_TCTL_PSP);
561 /*===========================================================================*
562 * e1000_reset_hw *
563 *===========================================================================*/
564 PRIVATE void e1000_reset_hw(e)
565 e1000_t *e;
567 /* Assert a Device Reset signal. */
568 e1000_reg_set(e, E1000_REG_CTRL, E1000_REG_CTRL_RST);
570 /* Wait one microsecond. */
571 tickdelay(1);
574 /*===========================================================================*
575 * e1000_writev_s *
576 *===========================================================================*/
577 PRIVATE void e1000_writev_s(mp, from_int)
578 message *mp;
579 int from_int;
581 e1000_t *e = &e1000_state;
582 e1000_tx_desc_t *desc;
583 iovec_s_t iovec[E1000_IOVEC_NR];
584 int r, head, tail, i, bytes = 0, size;
586 E1000_DEBUG(3, ("e1000: writev_s(%p,%d)\n", mp, from_int));
588 /* Are we called from the interrupt handler? */
589 if (!from_int)
591 /* We cannot write twice simultaneously.
592 assert(!(e->status & E1000_WRITING)); */
594 /* Copy write message. */
595 e->tx_message = *mp;
596 e->client = mp->m_source;
597 e->status |= E1000_WRITING;
599 /* Must be a sane vector count. */
600 assert(e->tx_message.DL_COUNT > 0);
601 assert(e->tx_message.DL_COUNT < E1000_IOVEC_NR);
604 * Copy the I/O vector table.
606 if ((r = sys_safecopyfrom(e->tx_message.DL_ENDPT,
607 e->tx_message.DL_GRANT, 0,
608 (vir_bytes) iovec, e->tx_message.DL_COUNT *
609 sizeof(iovec_s_t), D)) != OK)
611 panic("sys_safecopyfrom() failed: %d", r);
613 /* Find the head, tail and current descriptors. */
614 head = e1000_reg_read(e, E1000_REG_TDH);
615 tail = e1000_reg_read(e, E1000_REG_TDT);
616 desc = &e->tx_desc[tail];
618 E1000_DEBUG(4, ("%s: head=%d, tail=%d\n",
619 e->name, head, tail));
621 /* Loop vector elements. */
622 for (i = 0; i < e->tx_message.DL_COUNT; i++)
624 size = iovec[i].iov_size < (E1000_IOBUF_SIZE - bytes) ?
625 iovec[i].iov_size : (E1000_IOBUF_SIZE - bytes);
627 E1000_DEBUG(4, ("iovec[%d] = %d\n", i, size));
629 /* Copy bytes to TX queue buffers. */
630 if ((r = sys_safecopyfrom(e->tx_message.DL_ENDPT,
631 iovec[i].iov_grant, 0,
632 (vir_bytes) e->tx_buffer +
633 (tail * E1000_IOBUF_SIZE),
634 size, D)) != OK)
636 panic("sys_safecopyfrom() failed: %d", r);
638 /* Mark this descriptor ready. */
639 desc->status = 0;
640 desc->command = 0;
641 desc->length = size;
643 /* Marks End-of-Packet. */
644 if (i == e->tx_message.DL_COUNT - 1)
646 desc->command = E1000_TX_CMD_EOP |
647 E1000_TX_CMD_FCS |
648 E1000_TX_CMD_RS;
650 /* Move to next descriptor. */
651 tail = (tail + 1) % e->tx_desc_count;
652 bytes += size;
653 desc = &e->tx_desc[tail];
655 /* Increment tail. Start transmission. */
656 e1000_reg_write(e, E1000_REG_TDT, tail);
658 E1000_DEBUG(2, ("e1000: wrote %d byte packet\n", bytes));
660 else
662 e->status |= E1000_TRANSMIT;
664 reply(e);
667 /*===========================================================================*
668 * e1000_readv_s *
669 *===========================================================================*/
670 PRIVATE void e1000_readv_s(mp, from_int)
671 message *mp;
672 int from_int;
674 e1000_t *e = &e1000_state;
675 e1000_rx_desc_t *desc;
676 iovec_s_t iovec[E1000_IOVEC_NR];
677 int i, r, head, tail, cur, bytes = 0, size;
679 E1000_DEBUG(3, ("e1000: readv_s(%p,%d)\n", mp, from_int));
681 /* Are we called from the interrupt handler? */
682 if (!from_int)
684 e->rx_message = *mp;
685 e->client = mp->m_source;
686 e->status |= E1000_READING;
687 e->rx_size = 0;
689 assert(e->rx_message.DL_COUNT > 0);
690 assert(e->rx_message.DL_COUNT < E1000_IOVEC_NR);
692 if (e->status & E1000_READING)
695 * Copy the I/O vector table first.
697 if ((r = sys_safecopyfrom(e->rx_message.DL_ENDPT,
698 e->rx_message.DL_GRANT, 0,
699 (vir_bytes) iovec, e->rx_message.DL_COUNT *
700 sizeof(iovec_s_t), D)) != OK)
702 panic("sys_safecopyfrom() failed: %d", r);
704 /* Find the head, tail and current descriptors. */
705 head = e1000_reg_read(e, E1000_REG_RDH);
706 tail = e1000_reg_read(e, E1000_REG_RDT);
707 cur = (tail + 1) % e->rx_desc_count;
708 desc = &e->rx_desc[cur];
711 * Only handle one packet at a time.
713 if (!(desc->status & E1000_RX_STATUS_EOP))
715 reply(e);
716 return;
718 E1000_DEBUG(4, ("%s: head=%x, tail=%d\n",
719 e->name, head, tail));
722 * Copy to vector elements.
724 for (i = 0; i < e->rx_message.DL_COUNT && bytes < desc->length; i++)
726 size = iovec[i].iov_size < (desc->length - bytes) ?
727 iovec[i].iov_size : (desc->length - bytes);
729 E1000_DEBUG(4, ("iovec[%d] = %lu[%d]\n",
730 i, iovec[i].iov_size, size));
732 if ((r = sys_safecopyto(e->rx_message.DL_ENDPT, iovec[i].iov_grant,
733 0, (vir_bytes) e->rx_buffer + bytes +
734 (cur * E1000_IOBUF_SIZE),
735 size, D)) != OK)
737 panic("sys_safecopyto() failed: %d", r);
739 bytes += size;
741 desc->status = 0;
744 * Update state.
746 e->rx_size = bytes;
747 e->status |= E1000_RECEIVED;
748 E1000_DEBUG(2, ("e1000: got %d byte packet\n", e->rx_size));
750 /* Increment tail. */
751 e1000_reg_write(e, E1000_REG_RDT, (tail + 1) % e->rx_desc_count);
753 reply(e);
756 /*===========================================================================*
757 * e1000_getstat_s *
758 *===========================================================================*/
759 PRIVATE void e1000_getstat_s(mp)
760 message *mp;
762 int r;
763 eth_stat_t stats;
764 e1000_t *e = &e1000_state;
766 E1000_DEBUG(3, ("e1000: getstat_s()\n"));
768 stats.ets_recvErr = e1000_reg_read(e, E1000_REG_RXERRC);
769 stats.ets_sendErr = 0;
770 stats.ets_OVW = 0;
771 stats.ets_CRCerr = e1000_reg_read(e, E1000_REG_CRCERRS);
772 stats.ets_frameAll = 0;
773 stats.ets_missedP = e1000_reg_read(e, E1000_REG_MPC);
774 stats.ets_packetR = e1000_reg_read(e, E1000_REG_TPR);
775 stats.ets_packetT = e1000_reg_read(e, E1000_REG_TPT);
776 stats.ets_collision = e1000_reg_read(e, E1000_REG_COLC);
777 stats.ets_transAb = 0;
778 stats.ets_carrSense = 0;
779 stats.ets_fifoUnder = 0;
780 stats.ets_fifoOver = 0;
781 stats.ets_CDheartbeat = 0;
782 stats.ets_OWC = 0;
784 sys_safecopyto(mp->DL_ENDPT, mp->DL_GRANT, 0, (vir_bytes)&stats,
785 sizeof(stats), D);
786 mp->m_type = DL_STAT_REPLY;
787 if((r=send(mp->m_source, mp)) != OK)
788 panic("e1000_getstat: send() failed: %d", r);
791 /*===========================================================================*
792 * e1000_interrupt *
793 *===========================================================================*/
794 PRIVATE void e1000_interrupt(mp)
795 message *mp;
797 e1000_t *e;
798 u32_t cause;
800 E1000_DEBUG(3, ("e1000: interrupt\n"));
803 * Check the card for interrupt reason(s).
805 e = &e1000_state;
807 /* Re-enable interrupts. */
808 if (sys_irqenable(&e->irq_hook) != OK)
810 panic("failed to re-enable IRQ");
813 /* Read the Interrupt Cause Read register. */
814 if ((cause = e1000_reg_read(e, E1000_REG_ICR)))
816 if (cause & E1000_REG_ICR_LSC)
817 e1000_link_changed(e);
819 if (cause & (E1000_REG_ICR_RXO | E1000_REG_ICR_RXT))
820 e1000_readv_s(&e->rx_message, TRUE);
822 if ((cause & E1000_REG_ICR_TXQE) ||
823 (cause & E1000_REG_ICR_TXDW))
824 e1000_writev_s(&e->tx_message, TRUE);
828 /*===========================================================================*
829 * e1000_link_changed *
830 *===========================================================================*/
831 PRIVATE int e1000_link_changed(e)
832 e1000_t *e;
834 E1000_DEBUG(4, ("%s: link_changed()\n", e->name));
835 return FALSE;
838 /*===========================================================================*
839 * e1000_stop *
840 *===========================================================================*/
841 PRIVATE void e1000_stop()
843 E1000_DEBUG(3, ("e1000: stop()\n"));
844 exit(EXIT_SUCCESS);
847 /*===========================================================================*
848 * e1000_reg_read *
849 *===========================================================================*/
850 PRIVATE uint32_t e1000_reg_read(e, reg)
851 e1000_t *e;
852 uint32_t reg;
854 uint32_t value;
856 /* Assume a sane register. */
857 assert(reg < 0x1ffff);
859 /* Read from memory mapped register. */
860 value = *(u32_t *)(e->regs + reg);
862 /* Return the result. */
863 return value;
866 /*===========================================================================*
867 * e1000_reg_write *
868 *===========================================================================*/
869 PRIVATE void e1000_reg_write(e, reg, value)
870 e1000_t *e;
871 uint32_t reg;
872 uint32_t value;
874 /* Assume a sane register. */
875 assert(reg < 0x1ffff);
877 /* Write to memory mapped register. */
878 *(u32_t *)(e->regs + reg) = value;
881 /*===========================================================================*
882 * e1000_reg_set *
883 *===========================================================================*/
884 PRIVATE void e1000_reg_set(e, reg, value)
885 e1000_t *e;
886 uint32_t reg;
887 uint32_t value;
889 uint32_t data;
891 /* First read the current value. */
892 data = e1000_reg_read(e, reg);
894 /* Set value, and write back. */
895 e1000_reg_write(e, reg, data | value);
898 /*===========================================================================*
899 * e1000_reg_unset *
900 *===========================================================================*/
901 PRIVATE void e1000_reg_unset(e, reg, value)
902 e1000_t *e;
903 uint32_t reg;
904 uint32_t value;
906 uint32_t data;
908 /* First read the current value. */
909 data = e1000_reg_read(e, reg);
911 /* Unset value, and write back. */
912 e1000_reg_write(e, reg, data & ~value);
916 /*===========================================================================*
917 * eeprom_eerd *
918 *===========================================================================*/
919 PRIVATE u16_t eeprom_eerd(v, reg)
920 void *v;
921 int reg;
923 e1000_t *e = (e1000_t *) v;
924 u16_t data;
926 /* Request EEPROM read. */
927 e1000_reg_write(e, E1000_REG_EERD,
928 (reg << e->eeprom_addr_off) | (E1000_REG_EERD_START));
930 /* Wait until ready. */
931 while (!(e1000_reg_read(e, E1000_REG_EERD) &
932 e->eeprom_done_bit));
934 /* Fetch data. */
935 data = (e1000_reg_read(e, E1000_REG_EERD) &
936 E1000_REG_EERD_DATA) >> 16;
937 return data;
940 /*===========================================================================*
941 * eeprom_ich_init *
942 *===========================================================================*/
943 PRIVATE int eeprom_ich_init(e)
944 e1000_t *e;
946 union ich8_hws_flash_status hsfsts;
947 int ret_val = -1;
948 int i = 0;
950 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
952 /* Check if the flash descriptor is valid */
953 if (hsfsts.hsf_status.fldesvalid == 0)
955 E1000_DEBUG(3, ("Flash descriptor invalid. "
956 "SW Sequencing must be used."));
957 goto out;
959 /* Clear FCERR and DAEL in hw status by writing 1 */
960 hsfsts.hsf_status.flcerr = 1;
961 hsfsts.hsf_status.dael = 1;
963 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFSTS, hsfsts.regval);
966 * Either we should have a hardware SPI cycle in progress
967 * bit to check against, in order to start a new cycle or
968 * FDONE bit should be changed in the hardware so that it
969 * is 1 after hardware reset, which can then be used as an
970 * indication whether a cycle is in progress or has been
971 * completed.
973 if (hsfsts.hsf_status.flcinprog == 0)
976 * There is no cycle running at present,
977 * so we can start a cycle.
978 * Begin by setting Flash Cycle Done.
980 hsfsts.hsf_status.flcdone = 1;
981 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFSTS, hsfsts.regval);
982 ret_val = 0;
984 else
987 * Otherwise poll for sometime so the current
988 * cycle has a chance to end before giving up.
990 for (i = 0; i < ICH_FLASH_READ_COMMAND_TIMEOUT; i++)
992 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
994 if (hsfsts.hsf_status.flcinprog == 0)
996 ret_val = 0;
997 break;
999 tickdelay(1);
1001 if (ret_val == 0)
1004 * Successful in waiting for previous cycle to timeout,
1005 * now set the Flash Cycle Done.
1007 hsfsts.hsf_status.flcdone = 1;
1008 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFSTS,
1009 hsfsts.regval);
1011 else
1013 E1000_DEBUG(3, ("Flash controller busy, cannot get access"));
1016 out:
1017 return ret_val;
1020 /*===========================================================================*
1021 * eeprom_ich_cycle *
1022 *===========================================================================*/
1023 PRIVATE int eeprom_ich_cycle(const e1000_t *e, u32_t timeout)
1025 union ich8_hws_flash_ctrl hsflctl;
1026 union ich8_hws_flash_status hsfsts;
1027 int ret_val = -1;
1028 u32_t i = 0;
1030 E1000_DEBUG(3, ("e1000_flash_cycle_ich8lan"));
1032 /* Start a cycle by writing 1 in Flash Cycle Go in Hw Flash Control */
1033 hsflctl.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFCTL);
1034 hsflctl.hsf_ctrl.flcgo = 1;
1035 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFCTL, hsflctl.regval);
1037 /* wait till FDONE bit is set to 1 */
1040 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
1041 if (hsfsts.hsf_status.flcdone == 1)
1042 break;
1043 tickdelay(1);
1045 while (i++ < timeout);
1047 if (hsfsts.hsf_status.flcdone == 1 && hsfsts.hsf_status.flcerr == 0)
1048 ret_val = 0;
1050 return ret_val;
1053 /*===========================================================================*
1054 * eeprom_ich *
1055 *===========================================================================*/
1056 PRIVATE u16_t eeprom_ich(v, reg)
1057 void *v;
1058 int reg;
1060 union ich8_hws_flash_status hsfsts;
1061 union ich8_hws_flash_ctrl hsflctl;
1062 u32_t flash_linear_addr;
1063 u32_t flash_data = 0;
1064 int ret_val = -1;
1065 u8_t count = 0;
1066 e1000_t *e = (e1000_t *) v;
1067 u16_t data = 0;
1069 E1000_DEBUG(3, ("e1000_read_flash_data_ich8lan"));
1071 if (reg > ICH_FLASH_LINEAR_ADDR_MASK)
1072 goto out;
1074 reg *= sizeof(u16_t);
1075 flash_linear_addr = (ICH_FLASH_LINEAR_ADDR_MASK & reg) +
1076 e->flash_base_addr;
1078 do {
1079 tickdelay(1);
1081 /* Steps */
1082 ret_val = eeprom_ich_init(e);
1083 if (ret_val != 0)
1084 break;
1086 hsflctl.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFCTL);
1087 /* 0b/1b corresponds to 1 or 2 byte size, respectively. */
1088 hsflctl.hsf_ctrl.fldbcount = 1;
1089 hsflctl.hsf_ctrl.flcycle = ICH_CYCLE_READ;
1090 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFCTL, hsflctl.regval);
1091 E1000_WRITE_FLASH_REG(e, ICH_FLASH_FADDR, flash_linear_addr);
1093 ret_val = eeprom_ich_cycle(v, ICH_FLASH_READ_COMMAND_TIMEOUT);
1096 * Check if FCERR is set to 1, if set to 1, clear it
1097 * and try the whole sequence a few more times, else
1098 * read in (shift in) the Flash Data0, the order is
1099 * least significant byte first msb to lsb
1101 if (ret_val == 0)
1103 flash_data = E1000_READ_FLASH_REG(e, ICH_FLASH_FDATA0);
1104 data = (u16_t)(flash_data & 0x0000FFFF);
1105 break;
1107 else
1110 * If we've gotten here, then things are probably
1111 * completely hosed, but if the error condition is
1112 * detected, it won't hurt to give it another try...
1113 * ICH_FLASH_CYCLE_REPEAT_COUNT times.
1115 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
1117 if (hsfsts.hsf_status.flcerr == 1)
1119 /* Repeat for some time before giving up. */
1120 continue;
1122 else if (hsfsts.hsf_status.flcdone == 0)
1124 E1000_DEBUG(3, ("Timeout error - flash cycle "
1125 "did not complete."));
1126 break;
1129 } while (count++ < ICH_FLASH_CYCLE_REPEAT_COUNT);
1131 out:
1132 return data;
1135 /*===========================================================================*
1136 * reply *
1137 *===========================================================================*/
1138 PRIVATE void reply(e)
1139 e1000_t *e;
1141 message msg;
1142 int r;
1144 /* Only reply to client for read/write request. */
1145 if (!(e->status & E1000_READING ||
1146 e->status & E1000_WRITING))
1148 return;
1150 /* Construct reply message. */
1151 msg.m_type = DL_TASK_REPLY;
1152 msg.DL_FLAGS = DL_NOFLAGS;
1153 msg.DL_COUNT = 0;
1155 /* Did we successfully receive packet(s)? */
1156 if (e->status & E1000_READING &&
1157 e->status & E1000_RECEIVED)
1159 msg.DL_FLAGS |= DL_PACK_RECV;
1160 msg.DL_COUNT = e->rx_size >= ETH_MIN_PACK_SIZE ?
1161 e->rx_size : ETH_MIN_PACK_SIZE;
1163 /* Clear flags. */
1164 e->status &= ~(E1000_READING | E1000_RECEIVED);
1166 /* Did we successfully transmit packet(s)? */
1167 if (e->status & E1000_TRANSMIT &&
1168 e->status & E1000_WRITING)
1170 msg.DL_FLAGS |= DL_PACK_SEND;
1172 /* Clear flags. */
1173 e->status &= ~(E1000_WRITING | E1000_TRANSMIT);
1176 /* Acknowledge to INET. */
1177 if ((r = send(e->client, &msg)) != OK)
1179 panic("send() failed: %d", r);
1183 /*===========================================================================*
1184 * mess_reply *
1185 *===========================================================================*/
1186 PRIVATE void mess_reply(req, reply_mess)
1187 message *req;
1188 message *reply_mess;
1190 if (send(req->m_source, reply_mess) != OK)
1192 panic("unable to send reply message");