iso9660fs: initialize buffer cache
[minix.git] / drivers / e1000 / e1000.c
blob47f21f026c7b95160860f85d73a45435746fed6d
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 <sys/mman.h>
18 #include "assert.h"
19 #include "e1000.h"
20 #include "e1000_hw.h"
21 #include "e1000_reg.h"
22 #include "e1000_pci.h"
24 static int e1000_instance;
25 static e1000_t e1000_state;
27 static void e1000_init(message *mp);
28 static void e1000_init_pci(void);
29 static int e1000_probe(e1000_t *e, int skip);
30 static int e1000_init_hw(e1000_t *e);
31 static void e1000_init_addr(e1000_t *e);
32 static void e1000_init_buf(e1000_t *e);
33 static void e1000_reset_hw(e1000_t *e);
34 static void e1000_writev_s(message *mp, int from_int);
35 static void e1000_readv_s(message *mp, int from_int);
36 static void e1000_getstat_s(message *mp);
37 static void e1000_interrupt(message *mp);
38 static int e1000_link_changed(e1000_t *e);
39 static void e1000_stop(e1000_t *e);
40 static uint32_t e1000_reg_read(e1000_t *e, uint32_t reg);
41 static void e1000_reg_write(e1000_t *e, uint32_t reg, uint32_t value);
42 static void e1000_reg_set(e1000_t *e, uint32_t reg, uint32_t value);
43 static void e1000_reg_unset(e1000_t *e, uint32_t reg, uint32_t value);
44 static u16_t eeprom_eerd(void *e, int reg);
45 static u16_t eeprom_ich(void *e, int reg);
46 static int eeprom_ich_init(e1000_t *e);
47 static int eeprom_ich_cycle(const e1000_t *e, u32_t timeout);
48 static void reply(e1000_t *e);
49 static void mess_reply(message *req, message *reply);
51 /* SEF functions and variables. */
52 static void sef_local_startup(void);
53 static int sef_cb_init_fresh(int type, sef_init_info_t *info);
54 static void sef_cb_signal_handler(int signo);
56 /*===========================================================================*
57 * main *
58 *===========================================================================*/
59 int main(int argc, char *argv[])
61 message m;
62 int ipc_status;
63 int r;
65 /* SEF local startup. */
66 env_setargs(argc, argv);
67 sef_local_startup();
70 * Enter the main driver loop.
72 while (TRUE)
74 if ((r= netdriver_receive(ANY, &m, &ipc_status)) != OK)
76 panic("netdriver_receive failed: %d", r);
79 if (is_ipc_notify(ipc_status))
81 switch (_ENDPOINT_P(m.m_source))
83 case HARDWARE:
84 e1000_interrupt(&m);
85 break;
87 case CLOCK:
88 break;
90 continue;
92 switch (m.m_type)
94 case DL_WRITEV_S: e1000_writev_s(&m, FALSE); break;
95 case DL_READV_S: e1000_readv_s(&m, FALSE); break;
96 case DL_CONF: e1000_init(&m); break;
97 case DL_GETSTAT_S: e1000_getstat_s(&m); break;
98 default:
99 panic("illegal message: %d", m.m_type);
104 /*===========================================================================*
105 * sef_local_startup *
106 *===========================================================================*/
107 static void sef_local_startup()
109 /* Register init callbacks. */
110 sef_setcb_init_fresh(sef_cb_init_fresh);
111 sef_setcb_init_lu(sef_cb_init_fresh);
112 sef_setcb_init_restart(sef_cb_init_fresh);
114 /* Register live update callbacks. */
115 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready);
116 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_workfree);
118 /* Register signal callbacks. */
119 sef_setcb_signal_handler(sef_cb_signal_handler);
121 /* Let SEF perform startup. */
122 sef_startup();
125 /*===========================================================================*
126 * sef_cb_init_fresh *
127 *===========================================================================*/
128 static int sef_cb_init_fresh(int UNUSED(type), sef_init_info_t *UNUSED(info))
130 /* Initialize the e1000 driver. */
131 long v;
132 int r;
134 v = 0;
135 (void) env_parse("instance", "d", 0, &v, 0, 255);
136 e1000_instance = (int) v;
138 /* Clear state. */
139 memset(&e1000_state, 0, sizeof(e1000_state));
141 /* Perform calibration. */
142 if((r = tsc_calibrate()) != OK)
144 panic("tsc_calibrate failed: %d", r);
147 /* Announce we are up! */
148 netdriver_announce();
150 return(OK);
153 /*===========================================================================*
154 * sef_cb_signal_handler *
155 *===========================================================================*/
156 static void sef_cb_signal_handler(int signo)
158 e1000_t *e;
159 e = &e1000_state;
161 E1000_DEBUG(3, ("%s: got signal\n", e->name));
163 /* Only check for termination signal, ignore anything else. */
164 if (signo != SIGTERM) return;
166 e1000_stop(e);
169 /*===========================================================================*
170 * e1000_init *
171 *===========================================================================*/
172 static void e1000_init(message *mp)
174 static int first_time = 1;
175 message reply_mess;
176 e1000_t *e;
178 E1000_DEBUG(3, ("e1000: init()\n"));
180 /* Configure PCI devices, if needed. */
181 if (first_time)
183 first_time = 0;
184 e1000_init_pci();
186 e = &e1000_state;
188 /* Initialize hardware, if needed. */
189 if (!(e->status & E1000_ENABLED) && !(e1000_init_hw(e)))
191 reply_mess.m_type = DL_CONF_REPLY;
192 reply_mess.DL_STAT = ENXIO;
193 mess_reply(mp, &reply_mess);
194 return;
196 /* Reply back to INET. */
197 reply_mess.m_type = DL_CONF_REPLY;
198 reply_mess.DL_STAT = OK;
199 *(ether_addr_t *) reply_mess.DL_HWADDR = e->address;
200 mess_reply(mp, &reply_mess);
203 /*===========================================================================*
204 * e1000_int_pci *
205 *===========================================================================*/
206 static void e1000_init_pci()
208 e1000_t *e;
210 /* Initialize the PCI bus. */
211 pci_init();
213 /* Try to detect e1000's. */
214 e = &e1000_state;
215 strlcpy(e->name, "e1000#0", sizeof(e->name));
216 e->name[6] += e1000_instance;
217 e1000_probe(e, e1000_instance);
220 /*===========================================================================*
221 * e1000_probe *
222 *===========================================================================*/
223 static int e1000_probe(e1000_t *e, int skip)
225 int r, devind, ioflag;
226 u16_t vid, did, cr;
227 u32_t status[2];
228 u32_t base, size;
229 u32_t gfpreg, sector_base_addr;
230 char *dname;
232 E1000_DEBUG(3, ("%s: probe()\n", e->name));
235 * Attempt to iterate the PCI bus. Start at the beginning.
237 if ((r = pci_first_dev(&devind, &vid, &did)) == 0)
239 return FALSE;
241 /* Loop devices on the PCI bus. */
242 while (skip--)
244 E1000_DEBUG(3, ("%s: probe() devind %d vid 0x%x did 0x%x\n",
245 e->name, devind, vid, did));
247 if (!(r = pci_next_dev(&devind, &vid, &did)))
249 return FALSE;
253 * Successfully detected an Intel Pro/1000 on the PCI bus.
255 e->status |= E1000_DETECTED;
256 e->eeprom_read = eeprom_eerd;
259 * Set card specific properties.
261 switch (did)
263 case E1000_DEV_ID_ICH10_D_BM_LM:
264 case E1000_DEV_ID_ICH10_R_BM_LF:
265 e->eeprom_read = eeprom_ich;
266 break;
268 case E1000_DEV_ID_82540EM:
269 case E1000_DEV_ID_82545EM:
270 e->eeprom_done_bit = (1 << 4);
271 e->eeprom_addr_off = 8;
272 break;
274 default:
275 e->eeprom_done_bit = (1 << 1);
276 e->eeprom_addr_off = 2;
277 break;
280 /* Inform the user about the new card. */
281 if (!(dname = pci_dev_name(vid, did)))
283 dname = "Intel Pro/1000 Gigabit Ethernet Card";
285 E1000_DEBUG(1, ("%s: %s (%04x/%04x/%02x) at %s\n",
286 e->name, dname, vid, did, e->revision,
287 pci_slot_name(devind)));
289 /* Reserve PCI resources found. */
290 if ((r = pci_reserve_ok(devind)) != OK)
292 panic("failed to reserve PCI device: %d", r);
294 /* Read PCI configuration. */
295 e->irq = pci_attr_r8(devind, PCI_ILR);
297 if ((r = pci_get_bar(devind, PCI_BAR, &base, &size, &ioflag)) != OK)
298 panic("failed to get PCI BAR (%d)", r);
299 if (ioflag) panic("PCI BAR is not for memory");
301 e->regs = vm_map_phys(SELF, (void *) base, size);
302 if (e->regs == (u8_t *) -1) {
303 panic("failed to map hardware registers from PCI");
306 /* FIXME: enable DMA bus mastering if necessary. This is disabled by
307 * default on VMware. Eventually, the PCI driver should deal with this.
309 cr = pci_attr_r16(devind, PCI_CR);
310 if (!(cr & PCI_CR_MAST_EN))
311 pci_attr_w16(devind, PCI_CR, cr | PCI_CR_MAST_EN);
313 /* Optionally map flash memory. */
314 if (did != E1000_DEV_ID_82540EM &&
315 did != E1000_DEV_ID_82545EM &&
316 did != E1000_DEV_ID_82540EP &&
317 pci_attr_r32(devind, PCI_BAR_2))
319 size_t flash_size;
321 /* 82566/82567/82562V series support mapping 4kB of flash memory */
322 switch(did)
324 case E1000_DEV_ID_ICH10_D_BM_LM:
325 case E1000_DEV_ID_ICH10_R_BM_LF:
326 flash_size = 0x1000;
327 break;
328 default:
329 flash_size = 0x10000;
332 if ((e->flash = vm_map_phys(SELF,
333 (void *) pci_attr_r32(devind, PCI_BAR_2),
334 flash_size)) == MAP_FAILED) {
335 panic("e1000: couldn't map in flash.");
338 gfpreg = E1000_READ_FLASH_REG(e, ICH_FLASH_GFPREG);
340 * sector_base_addr is a "sector"-aligned address (4096 bytes)
342 sector_base_addr = gfpreg & FLASH_GFPREG_BASE_MASK;
344 /* flash_base_addr is byte-aligned */
345 e->flash_base_addr = sector_base_addr << FLASH_SECTOR_ADDR_SHIFT;
348 * Output debug information.
350 status[0] = e1000_reg_read(e, E1000_REG_STATUS);
351 E1000_DEBUG(3, ("%s: MEM at %p, IRQ %d\n",
352 e->name, e->regs, e->irq));
353 E1000_DEBUG(3, ("%s: link %s, %s duplex\n",
354 e->name, status[0] & 3 ? "up" : "down",
355 status[0] & 1 ? "full" : "half"));
356 return TRUE;
359 /*===========================================================================*
360 * e1000_init_hw *
361 *===========================================================================*/
362 static int e1000_init_hw(e)
363 e1000_t *e;
365 int r, i;
367 e->status |= E1000_ENABLED;
368 e->irq_hook = e->irq;
371 * Set the interrupt handler and policy. Do not automatically
372 * re-enable interrupts. Return the IRQ line number on interrupts.
374 if ((r = sys_irqsetpolicy(e->irq, 0, &e->irq_hook)) != OK)
376 panic("sys_irqsetpolicy failed: %d", r);
378 if ((r = sys_irqenable(&e->irq_hook)) != OK)
380 panic("sys_irqenable failed: %d", r);
382 /* Reset hardware. */
383 e1000_reset_hw(e);
386 * Initialize appropriately, according to section 14.3 General Configuration
387 * of Intel's Gigabit Ethernet Controllers Software Developer's Manual.
389 e1000_reg_set(e, E1000_REG_CTRL, E1000_REG_CTRL_ASDE | E1000_REG_CTRL_SLU);
390 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_LRST);
391 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_PHY_RST);
392 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_ILOS);
393 e1000_reg_write(e, E1000_REG_FCAL, 0);
394 e1000_reg_write(e, E1000_REG_FCAH, 0);
395 e1000_reg_write(e, E1000_REG_FCT, 0);
396 e1000_reg_write(e, E1000_REG_FCTTV, 0);
397 e1000_reg_unset(e, E1000_REG_CTRL, E1000_REG_CTRL_VME);
399 /* Clear Multicast Table Array (MTA). */
400 for (i = 0; i < 128; i++)
402 e1000_reg_write(e, E1000_REG_MTA + i, 0);
404 /* Initialize statistics registers. */
405 for (i = 0; i < 64; i++)
407 e1000_reg_write(e, E1000_REG_CRCERRS + (i * 4), 0);
410 * Aquire MAC address and setup RX/TX buffers.
412 e1000_init_addr(e);
413 e1000_init_buf(e);
415 /* Enable interrupts. */
416 e1000_reg_set(e, E1000_REG_IMS, E1000_REG_IMS_LSC |
417 E1000_REG_IMS_RXO |
418 E1000_REG_IMS_RXT |
419 E1000_REG_IMS_TXQE |
420 E1000_REG_IMS_TXDW);
421 return TRUE;
424 /*===========================================================================*
425 * e1000_init_addr *
426 *===========================================================================*/
427 static void e1000_init_addr(e)
428 e1000_t *e;
430 static char eakey[]= E1000_ENVVAR "#_EA";
431 static char eafmt[]= "x:x:x:x:x:x";
432 u16_t word;
433 int i;
434 long v;
437 * Do we have a user defined ethernet address?
439 eakey[sizeof(E1000_ENVVAR)-1] = '0' + e1000_instance;
441 for (i= 0; i < 6; i++)
443 if (env_parse(eakey, eafmt, i, &v, 0x00L, 0xFFL) != EP_SET)
444 break;
445 else
446 e->address.ea_addr[i]= v;
449 * If that fails, read Ethernet Address from EEPROM.
451 if (i != 6)
453 for (i = 0; i < 3; i++)
455 word = e->eeprom_read(e, i);
456 e->address.ea_addr[(i * 2)] = (word & 0xff);
457 e->address.ea_addr[(i * 2) + 1] = (word & 0xff00) >> 8;
461 * Set Receive Address.
463 e1000_reg_write(e, E1000_REG_RAL, *(u32_t *)(&e->address.ea_addr[0]));
464 e1000_reg_write(e, E1000_REG_RAH, *(u16_t *)(&e->address.ea_addr[4]));
465 e1000_reg_set(e, E1000_REG_RAH, E1000_REG_RAH_AV);
466 e1000_reg_set(e, E1000_REG_RCTL, E1000_REG_RCTL_MPE);
468 E1000_DEBUG(3, ("%s: Ethernet Address %x:%x:%x:%x:%x:%x\n", e->name,
469 e->address.ea_addr[0], e->address.ea_addr[1],
470 e->address.ea_addr[2], e->address.ea_addr[3],
471 e->address.ea_addr[4], e->address.ea_addr[5]));
474 /*===========================================================================*
475 * e1000_init_buf *
476 *===========================================================================*/
477 static void e1000_init_buf(e)
478 e1000_t *e;
480 phys_bytes rx_buff_p;
481 phys_bytes tx_buff_p;
482 int i;
484 /* Number of descriptors. */
485 e->rx_desc_count = E1000_RXDESC_NR;
486 e->tx_desc_count = E1000_TXDESC_NR;
489 * First, allocate the receive descriptors.
491 if (!e->rx_desc)
493 if ((e->rx_desc = alloc_contig(sizeof(e1000_rx_desc_t) *
494 e->rx_desc_count, AC_ALIGN4K,
495 &e->rx_desc_p)) == NULL) {
496 panic("failed to allocate RX descriptors");
498 memset(e->rx_desc, 0, sizeof(e1000_rx_desc_t) * e->rx_desc_count);
501 * Allocate 2048-byte buffers.
503 e->rx_buffer_size = E1000_RXDESC_NR * E1000_IOBUF_SIZE;
505 /* Attempt to allocate. */
506 if ((e->rx_buffer = alloc_contig(e->rx_buffer_size,
507 AC_ALIGN4K, &rx_buff_p)) == NULL)
509 panic("failed to allocate RX buffers");
511 /* Setup receive descriptors. */
512 for (i = 0; i < E1000_RXDESC_NR; i++)
514 e->rx_desc[i].buffer = rx_buff_p + (i * E1000_IOBUF_SIZE);
518 * Then, allocate transmit descriptors.
520 if (!e->tx_desc)
522 if ((e->tx_desc = alloc_contig(sizeof(e1000_tx_desc_t) *
523 e->tx_desc_count, AC_ALIGN4K,
524 &e->tx_desc_p)) == NULL) {
525 panic("failed to allocate TX descriptors");
527 memset(e->tx_desc, 0, sizeof(e1000_tx_desc_t) * e->tx_desc_count);
530 * Allocate 2048-byte buffers.
532 e->tx_buffer_size = E1000_TXDESC_NR * E1000_IOBUF_SIZE;
534 /* Attempt to allocate. */
535 if ((e->tx_buffer = alloc_contig(e->tx_buffer_size,
536 AC_ALIGN4K, &tx_buff_p)) == NULL)
538 panic("failed to allocate TX buffers");
540 /* Setup transmit descriptors. */
541 for (i = 0; i < E1000_TXDESC_NR; i++)
543 e->tx_desc[i].buffer = tx_buff_p + (i * E1000_IOBUF_SIZE);
547 * Setup the receive ring registers.
549 e1000_reg_write(e, E1000_REG_RDBAL, e->rx_desc_p);
550 e1000_reg_write(e, E1000_REG_RDBAH, 0);
551 e1000_reg_write(e, E1000_REG_RDLEN, e->rx_desc_count *
552 sizeof(e1000_rx_desc_t));
553 e1000_reg_write(e, E1000_REG_RDH, 0);
554 e1000_reg_write(e, E1000_REG_RDT, e->rx_desc_count - 1);
555 e1000_reg_unset(e, E1000_REG_RCTL, E1000_REG_RCTL_BSIZE);
556 e1000_reg_set(e, E1000_REG_RCTL, E1000_REG_RCTL_EN);
559 * Setup the transmit ring registers.
561 e1000_reg_write(e, E1000_REG_TDBAL, e->tx_desc_p);
562 e1000_reg_write(e, E1000_REG_TDBAH, 0);
563 e1000_reg_write(e, E1000_REG_TDLEN, e->tx_desc_count *
564 sizeof(e1000_tx_desc_t));
565 e1000_reg_write(e, E1000_REG_TDH, 0);
566 e1000_reg_write(e, E1000_REG_TDT, 0);
567 e1000_reg_set( e, E1000_REG_TCTL, E1000_REG_TCTL_EN | E1000_REG_TCTL_PSP);
570 /*===========================================================================*
571 * e1000_reset_hw *
572 *===========================================================================*/
573 static void e1000_reset_hw(e)
574 e1000_t *e;
576 /* Assert a Device Reset signal. */
577 e1000_reg_set(e, E1000_REG_CTRL, E1000_REG_CTRL_RST);
579 /* Wait one microsecond. */
580 tickdelay(1);
583 /*===========================================================================*
584 * e1000_writev_s *
585 *===========================================================================*/
586 static void e1000_writev_s(mp, from_int)
587 message *mp;
588 int from_int;
590 e1000_t *e = &e1000_state;
591 e1000_tx_desc_t *desc;
592 iovec_s_t iovec[E1000_IOVEC_NR];
593 int r, head, tail, i, bytes = 0, size;
595 E1000_DEBUG(3, ("e1000: writev_s(%p,%d)\n", mp, from_int));
597 /* Are we called from the interrupt handler? */
598 if (!from_int)
600 /* We cannot write twice simultaneously.
601 assert(!(e->status & E1000_WRITING)); */
603 /* Copy write message. */
604 e->tx_message = *mp;
605 e->client = mp->m_source;
606 e->status |= E1000_WRITING;
608 /* Must be a sane vector count. */
609 assert(e->tx_message.DL_COUNT > 0);
610 assert(e->tx_message.DL_COUNT < E1000_IOVEC_NR);
613 * Copy the I/O vector table.
615 if ((r = sys_safecopyfrom(e->tx_message.m_source,
616 e->tx_message.DL_GRANT, 0,
617 (vir_bytes) iovec, e->tx_message.DL_COUNT *
618 sizeof(iovec_s_t))) != OK)
620 panic("sys_safecopyfrom() failed: %d", r);
622 /* Find the head, tail and current descriptors. */
623 head = e1000_reg_read(e, E1000_REG_TDH);
624 tail = e1000_reg_read(e, E1000_REG_TDT);
625 desc = &e->tx_desc[tail];
627 E1000_DEBUG(4, ("%s: head=%d, tail=%d\n",
628 e->name, head, tail));
630 /* Loop vector elements. */
631 for (i = 0; i < e->tx_message.DL_COUNT; i++)
633 size = iovec[i].iov_size < (E1000_IOBUF_SIZE - bytes) ?
634 iovec[i].iov_size : (E1000_IOBUF_SIZE - bytes);
636 E1000_DEBUG(4, ("iovec[%d] = %d\n", i, size));
638 /* Copy bytes to TX queue buffers. */
639 if ((r = sys_safecopyfrom(e->tx_message.m_source,
640 iovec[i].iov_grant, 0,
641 (vir_bytes) e->tx_buffer +
642 (tail * E1000_IOBUF_SIZE),
643 size)) != OK)
645 panic("sys_safecopyfrom() failed: %d", r);
647 /* Mark this descriptor ready. */
648 desc->status = 0;
649 desc->command = 0;
650 desc->length = size;
652 /* Marks End-of-Packet. */
653 if (i == e->tx_message.DL_COUNT - 1)
655 desc->command = E1000_TX_CMD_EOP |
656 E1000_TX_CMD_FCS |
657 E1000_TX_CMD_RS;
659 /* Move to next descriptor. */
660 tail = (tail + 1) % e->tx_desc_count;
661 bytes += size;
662 desc = &e->tx_desc[tail];
664 /* Increment tail. Start transmission. */
665 e1000_reg_write(e, E1000_REG_TDT, tail);
667 E1000_DEBUG(2, ("e1000: wrote %d byte packet\n", bytes));
669 else
671 e->status |= E1000_TRANSMIT;
673 reply(e);
676 /*===========================================================================*
677 * e1000_readv_s *
678 *===========================================================================*/
679 static void e1000_readv_s(mp, from_int)
680 message *mp;
681 int from_int;
683 e1000_t *e = &e1000_state;
684 e1000_rx_desc_t *desc;
685 iovec_s_t iovec[E1000_IOVEC_NR];
686 int i, r, head, tail, cur, bytes = 0, size;
688 E1000_DEBUG(3, ("e1000: readv_s(%p,%d)\n", mp, from_int));
690 /* Are we called from the interrupt handler? */
691 if (!from_int)
693 e->rx_message = *mp;
694 e->client = mp->m_source;
695 e->status |= E1000_READING;
696 e->rx_size = 0;
698 assert(e->rx_message.DL_COUNT > 0);
699 assert(e->rx_message.DL_COUNT < E1000_IOVEC_NR);
701 if (e->status & E1000_READING)
704 * Copy the I/O vector table first.
706 if ((r = sys_safecopyfrom(e->rx_message.m_source,
707 e->rx_message.DL_GRANT, 0,
708 (vir_bytes) iovec, e->rx_message.DL_COUNT *
709 sizeof(iovec_s_t))) != OK)
711 panic("sys_safecopyfrom() failed: %d", r);
713 /* Find the head, tail and current descriptors. */
714 head = e1000_reg_read(e, E1000_REG_RDH);
715 tail = e1000_reg_read(e, E1000_REG_RDT);
716 cur = (tail + 1) % e->rx_desc_count;
717 desc = &e->rx_desc[cur];
720 * Only handle one packet at a time.
722 if (!(desc->status & E1000_RX_STATUS_EOP))
724 reply(e);
725 return;
727 E1000_DEBUG(4, ("%s: head=%x, tail=%d\n",
728 e->name, head, tail));
731 * Copy to vector elements.
733 for (i = 0; i < e->rx_message.DL_COUNT && bytes < desc->length; i++)
735 size = iovec[i].iov_size < (desc->length - bytes) ?
736 iovec[i].iov_size : (desc->length - bytes);
738 E1000_DEBUG(4, ("iovec[%d] = %lu[%d]\n",
739 i, iovec[i].iov_size, size));
741 if ((r = sys_safecopyto(e->rx_message.m_source, iovec[i].iov_grant,
742 0, (vir_bytes) e->rx_buffer + bytes +
743 (cur * E1000_IOBUF_SIZE),
744 size)) != OK)
746 panic("sys_safecopyto() failed: %d", r);
748 bytes += size;
750 desc->status = 0;
753 * Update state.
755 e->rx_size = bytes;
756 e->status |= E1000_RECEIVED;
757 E1000_DEBUG(2, ("e1000: got %d byte packet\n", e->rx_size));
759 /* Increment tail. */
760 e1000_reg_write(e, E1000_REG_RDT, (tail + 1) % e->rx_desc_count);
762 reply(e);
765 /*===========================================================================*
766 * e1000_getstat_s *
767 *===========================================================================*/
768 static void e1000_getstat_s(mp)
769 message *mp;
771 int r;
772 eth_stat_t stats;
773 e1000_t *e = &e1000_state;
775 E1000_DEBUG(3, ("e1000: getstat_s()\n"));
777 stats.ets_recvErr = e1000_reg_read(e, E1000_REG_RXERRC);
778 stats.ets_sendErr = 0;
779 stats.ets_OVW = 0;
780 stats.ets_CRCerr = e1000_reg_read(e, E1000_REG_CRCERRS);
781 stats.ets_frameAll = 0;
782 stats.ets_missedP = e1000_reg_read(e, E1000_REG_MPC);
783 stats.ets_packetR = e1000_reg_read(e, E1000_REG_TPR);
784 stats.ets_packetT = e1000_reg_read(e, E1000_REG_TPT);
785 stats.ets_collision = e1000_reg_read(e, E1000_REG_COLC);
786 stats.ets_transAb = 0;
787 stats.ets_carrSense = 0;
788 stats.ets_fifoUnder = 0;
789 stats.ets_fifoOver = 0;
790 stats.ets_CDheartbeat = 0;
791 stats.ets_OWC = 0;
793 sys_safecopyto(mp->m_source, mp->DL_GRANT, 0, (vir_bytes)&stats,
794 sizeof(stats));
795 mp->m_type = DL_STAT_REPLY;
796 if((r=send(mp->m_source, mp)) != OK)
797 panic("e1000_getstat: send() failed: %d", r);
800 /*===========================================================================*
801 * e1000_interrupt *
802 *===========================================================================*/
803 static void e1000_interrupt(mp)
804 message *mp;
806 e1000_t *e;
807 u32_t cause;
809 E1000_DEBUG(3, ("e1000: interrupt\n"));
812 * Check the card for interrupt reason(s).
814 e = &e1000_state;
816 /* Re-enable interrupts. */
817 if (sys_irqenable(&e->irq_hook) != OK)
819 panic("failed to re-enable IRQ");
822 /* Read the Interrupt Cause Read register. */
823 if ((cause = e1000_reg_read(e, E1000_REG_ICR)))
825 if (cause & E1000_REG_ICR_LSC)
826 e1000_link_changed(e);
828 if (cause & (E1000_REG_ICR_RXO | E1000_REG_ICR_RXT))
829 e1000_readv_s(&e->rx_message, TRUE);
831 if ((cause & E1000_REG_ICR_TXQE) ||
832 (cause & E1000_REG_ICR_TXDW))
833 e1000_writev_s(&e->tx_message, TRUE);
837 /*===========================================================================*
838 * e1000_link_changed *
839 *===========================================================================*/
840 static int e1000_link_changed(e)
841 e1000_t *e;
843 E1000_DEBUG(4, ("%s: link_changed()\n", e->name));
844 return FALSE;
847 /*===========================================================================*
848 * e1000_stop *
849 *===========================================================================*/
850 static void e1000_stop(e)
851 e1000_t *e;
853 E1000_DEBUG(3, ("%s: stop()\n", e->name));
855 e1000_reset_hw(e);
857 exit(EXIT_SUCCESS);
860 /*===========================================================================*
861 * e1000_reg_read *
862 *===========================================================================*/
863 static uint32_t e1000_reg_read(e, reg)
864 e1000_t *e;
865 uint32_t reg;
867 uint32_t value;
869 /* Assume a sane register. */
870 assert(reg < 0x1ffff);
872 /* Read from memory mapped register. */
873 value = *(volatile u32_t *)(e->regs + reg);
875 /* Return the result. */
876 return value;
879 /*===========================================================================*
880 * e1000_reg_write *
881 *===========================================================================*/
882 static void e1000_reg_write(e, reg, value)
883 e1000_t *e;
884 uint32_t reg;
885 uint32_t value;
887 /* Assume a sane register. */
888 assert(reg < 0x1ffff);
890 /* Write to memory mapped register. */
891 *(volatile u32_t *)(e->regs + reg) = value;
894 /*===========================================================================*
895 * e1000_reg_set *
896 *===========================================================================*/
897 static void e1000_reg_set(e, reg, value)
898 e1000_t *e;
899 uint32_t reg;
900 uint32_t value;
902 uint32_t data;
904 /* First read the current value. */
905 data = e1000_reg_read(e, reg);
907 /* Set value, and write back. */
908 e1000_reg_write(e, reg, data | value);
911 /*===========================================================================*
912 * e1000_reg_unset *
913 *===========================================================================*/
914 static void e1000_reg_unset(e, reg, value)
915 e1000_t *e;
916 uint32_t reg;
917 uint32_t value;
919 uint32_t data;
921 /* First read the current value. */
922 data = e1000_reg_read(e, reg);
924 /* Unset value, and write back. */
925 e1000_reg_write(e, reg, data & ~value);
929 /*===========================================================================*
930 * eeprom_eerd *
931 *===========================================================================*/
932 static u16_t eeprom_eerd(v, reg)
933 void *v;
934 int reg;
936 e1000_t *e = (e1000_t *) v;
937 u32_t data;
939 /* Request EEPROM read. */
940 e1000_reg_write(e, E1000_REG_EERD,
941 (reg << e->eeprom_addr_off) | (E1000_REG_EERD_START));
943 /* Wait until ready. */
944 while (!((data = (e1000_reg_read(e, E1000_REG_EERD))) & e->eeprom_done_bit));
946 return data >> 16;
949 /*===========================================================================*
950 * eeprom_ich_init *
951 *===========================================================================*/
952 static int eeprom_ich_init(e)
953 e1000_t *e;
955 union ich8_hws_flash_status hsfsts;
956 int ret_val = -1;
957 int i = 0;
959 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
961 /* Check if the flash descriptor is valid */
962 if (hsfsts.hsf_status.fldesvalid == 0)
964 E1000_DEBUG(3, ("Flash descriptor invalid. "
965 "SW Sequencing must be used."));
966 goto out;
968 /* Clear FCERR and DAEL in hw status by writing 1 */
969 hsfsts.hsf_status.flcerr = 1;
970 hsfsts.hsf_status.dael = 1;
972 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFSTS, hsfsts.regval);
975 * Either we should have a hardware SPI cycle in progress
976 * bit to check against, in order to start a new cycle or
977 * FDONE bit should be changed in the hardware so that it
978 * is 1 after hardware reset, which can then be used as an
979 * indication whether a cycle is in progress or has been
980 * completed.
982 if (hsfsts.hsf_status.flcinprog == 0)
985 * There is no cycle running at present,
986 * so we can start a cycle.
987 * Begin by setting Flash Cycle Done.
989 hsfsts.hsf_status.flcdone = 1;
990 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFSTS, hsfsts.regval);
991 ret_val = 0;
993 else
996 * Otherwise poll for sometime so the current
997 * cycle has a chance to end before giving up.
999 for (i = 0; i < ICH_FLASH_READ_COMMAND_TIMEOUT; i++)
1001 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
1003 if (hsfsts.hsf_status.flcinprog == 0)
1005 ret_val = 0;
1006 break;
1008 tickdelay(1);
1010 if (ret_val == 0)
1013 * Successful in waiting for previous cycle to timeout,
1014 * now set the Flash Cycle Done.
1016 hsfsts.hsf_status.flcdone = 1;
1017 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFSTS,
1018 hsfsts.regval);
1020 else
1022 E1000_DEBUG(3, ("Flash controller busy, cannot get access"));
1025 out:
1026 return ret_val;
1029 /*===========================================================================*
1030 * eeprom_ich_cycle *
1031 *===========================================================================*/
1032 static int eeprom_ich_cycle(const e1000_t *e, u32_t timeout)
1034 union ich8_hws_flash_ctrl hsflctl;
1035 union ich8_hws_flash_status hsfsts;
1036 int ret_val = -1;
1037 u32_t i = 0;
1039 E1000_DEBUG(3, ("e1000_flash_cycle_ich8lan"));
1041 /* Start a cycle by writing 1 in Flash Cycle Go in Hw Flash Control */
1042 hsflctl.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFCTL);
1043 hsflctl.hsf_ctrl.flcgo = 1;
1044 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFCTL, hsflctl.regval);
1046 /* wait till FDONE bit is set to 1 */
1049 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
1050 if (hsfsts.hsf_status.flcdone == 1)
1051 break;
1052 tickdelay(1);
1054 while (i++ < timeout);
1056 if (hsfsts.hsf_status.flcdone == 1 && hsfsts.hsf_status.flcerr == 0)
1057 ret_val = 0;
1059 return ret_val;
1062 /*===========================================================================*
1063 * eeprom_ich *
1064 *===========================================================================*/
1065 static u16_t eeprom_ich(v, reg)
1066 void *v;
1067 int reg;
1069 union ich8_hws_flash_status hsfsts;
1070 union ich8_hws_flash_ctrl hsflctl;
1071 u32_t flash_linear_addr;
1072 u32_t flash_data = 0;
1073 int ret_val = -1;
1074 u8_t count = 0;
1075 e1000_t *e = (e1000_t *) v;
1076 u16_t data = 0;
1078 E1000_DEBUG(3, ("e1000_read_flash_data_ich8lan"));
1080 if (reg > ICH_FLASH_LINEAR_ADDR_MASK)
1081 goto out;
1083 reg *= sizeof(u16_t);
1084 flash_linear_addr = (ICH_FLASH_LINEAR_ADDR_MASK & reg) +
1085 e->flash_base_addr;
1087 do {
1088 tickdelay(1);
1090 /* Steps */
1091 ret_val = eeprom_ich_init(e);
1092 if (ret_val != 0)
1093 break;
1095 hsflctl.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFCTL);
1096 /* 0b/1b corresponds to 1 or 2 byte size, respectively. */
1097 hsflctl.hsf_ctrl.fldbcount = 1;
1098 hsflctl.hsf_ctrl.flcycle = ICH_CYCLE_READ;
1099 E1000_WRITE_FLASH_REG16(e, ICH_FLASH_HSFCTL, hsflctl.regval);
1100 E1000_WRITE_FLASH_REG(e, ICH_FLASH_FADDR, flash_linear_addr);
1102 ret_val = eeprom_ich_cycle(v, ICH_FLASH_READ_COMMAND_TIMEOUT);
1105 * Check if FCERR is set to 1, if set to 1, clear it
1106 * and try the whole sequence a few more times, else
1107 * read in (shift in) the Flash Data0, the order is
1108 * least significant byte first msb to lsb
1110 if (ret_val == 0)
1112 flash_data = E1000_READ_FLASH_REG(e, ICH_FLASH_FDATA0);
1113 data = (u16_t)(flash_data & 0x0000FFFF);
1114 break;
1116 else
1119 * If we've gotten here, then things are probably
1120 * completely hosed, but if the error condition is
1121 * detected, it won't hurt to give it another try...
1122 * ICH_FLASH_CYCLE_REPEAT_COUNT times.
1124 hsfsts.regval = E1000_READ_FLASH_REG16(e, ICH_FLASH_HSFSTS);
1126 if (hsfsts.hsf_status.flcerr == 1)
1128 /* Repeat for some time before giving up. */
1129 continue;
1131 else if (hsfsts.hsf_status.flcdone == 0)
1133 E1000_DEBUG(3, ("Timeout error - flash cycle "
1134 "did not complete."));
1135 break;
1138 } while (count++ < ICH_FLASH_CYCLE_REPEAT_COUNT);
1140 out:
1141 return data;
1144 /*===========================================================================*
1145 * reply *
1146 *===========================================================================*/
1147 static void reply(e)
1148 e1000_t *e;
1150 message msg;
1151 int r;
1153 /* Only reply to client for read/write request. */
1154 if (!(e->status & E1000_READING ||
1155 e->status & E1000_WRITING))
1157 return;
1159 /* Construct reply message. */
1160 msg.m_type = DL_TASK_REPLY;
1161 msg.DL_FLAGS = DL_NOFLAGS;
1162 msg.DL_COUNT = 0;
1164 /* Did we successfully receive packet(s)? */
1165 if (e->status & E1000_READING &&
1166 e->status & E1000_RECEIVED)
1168 msg.DL_FLAGS |= DL_PACK_RECV;
1169 msg.DL_COUNT = e->rx_size >= ETH_MIN_PACK_SIZE ?
1170 e->rx_size : ETH_MIN_PACK_SIZE;
1172 /* Clear flags. */
1173 e->status &= ~(E1000_READING | E1000_RECEIVED);
1175 /* Did we successfully transmit packet(s)? */
1176 if (e->status & E1000_TRANSMIT &&
1177 e->status & E1000_WRITING)
1179 msg.DL_FLAGS |= DL_PACK_SEND;
1181 /* Clear flags. */
1182 e->status &= ~(E1000_WRITING | E1000_TRANSMIT);
1185 /* Acknowledge to INET. */
1186 if ((r = send(e->client, &msg)) != OK)
1188 panic("send() failed: %d", r);
1192 /*===========================================================================*
1193 * mess_reply *
1194 *===========================================================================*/
1195 static void mess_reply(req, reply_mess)
1196 message *req;
1197 message *reply_mess;
1199 if (send(req->m_source, reply_mess) != OK)
1201 panic("unable to send reply message");