4 * @brief This file contains a device driver for Intel Pro/1000
5 * Gigabit Ethernet Controllers.
8 #include <minix/drivers.h>
9 #include <minix/netdriver.h>
11 #include <net/gen/ether.h>
12 #include <net/gen/eth_io.h>
13 #include <machine/pci.h>
20 #include "e1000_reg.h"
21 #include "e1000_pci.h"
23 PRIVATE u16_t pcitab_e1000
[] =
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
,
50 _PROTOTYPE( PRIVATE
void e1000_reg_set
, (e1000_t
*e
, uint32_t reg
,
52 _PROTOTYPE( PRIVATE
void e1000_reg_unset
, (e1000_t
*e
, uint32_t reg
,
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
) );
66 EXTERN
char **env_argv
;
68 /*===========================================================================*
70 *===========================================================================*/
71 int main(int argc
, char *argv
[])
77 /* SEF local startup. */
78 env_setargs(argc
, argv
);
82 * Enter the main driver loop.
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
))
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;
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. */
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. */
147 (void) env_parse("instance", "d", 0, &v
, 0, 255);
148 e1000_instance
= (int) v
;
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();
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;
178 /*===========================================================================*
180 *===========================================================================*/
181 PRIVATE
void e1000_init(message
*mp
)
183 static int first_time
= 1;
187 E1000_DEBUG(3, ("e1000: init()\n"));
189 /* Configure PCI devices, if needed. */
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
);
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 /*===========================================================================*
214 *===========================================================================*/
215 PRIVATE
void e1000_init_pci()
219 /* Initialize the PCI bus. */
222 /* Try to detect e1000's. */
224 strcpy(e
->name
, "e1000#0");
225 e
->name
[6] += e1000_instance
;
226 e1000_probe(e
, e1000_instance
);
229 /*===========================================================================*
231 *===========================================================================*/
232 PRIVATE
int e1000_probe(e1000_t
*e
, int skip
)
237 u32_t gfpreg
, sector_base_addr
;
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)
249 /* Loop devices on the PCI bus. */
252 for (i
= 0; pcitab_e1000
[i
] != 0; i
++)
257 if (did
!= pcitab_e1000
[i
])
262 if (pcitab_e1000
[i
] != 0)
269 if (!(r
= pci_next_dev(&devind
, &vid
, &did
)))
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.
285 case E1000_DEV_ID_ICH10_R_BM_LF
:
286 e
->eeprom_read
= eeprom_ich
;
289 case E1000_DEV_ID_82541GI_LF
:
290 e
->eeprom_done_bit
= (1 << 1);
291 e
->eeprom_addr_off
= 2;
295 e
->eeprom_done_bit
= (1 << 4);
296 e
->eeprom_addr_off
= 8;
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
),
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
),
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"));
350 /*===========================================================================*
352 *===========================================================================*/
353 PRIVATE
int e1000_init_hw(e
)
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. */
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.
406 /* Enable interrupts. */
407 e1000_reg_set(e
, E1000_REG_IMS
, E1000_REG_IMS_LSC
|
415 /*===========================================================================*
417 *===========================================================================*/
418 PRIVATE
void e1000_init_addr(e
)
421 static char eakey
[]= E1000_ENVVAR
"#_EA";
422 static char eafmt
[]= "x:x:x:x:x:x";
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
)
437 e
->address
.ea_addr
[i
]= v
;
440 * If that fails, read Ethernet Address from EEPROM.
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 /*===========================================================================*
467 *===========================================================================*/
468 PRIVATE
void e1000_init_buf(e
)
471 phys_bytes rx_desc_p
, rx_buff_p
;
472 phys_bytes tx_desc_p
, tx_buff_p
;
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.
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.
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 /*===========================================================================*
563 *===========================================================================*/
564 PRIVATE
void e1000_reset_hw(e
)
567 /* Assert a Device Reset signal. */
568 e1000_reg_set(e
, E1000_REG_CTRL
, E1000_REG_CTRL_RST
);
570 /* Wait one microsecond. */
574 /*===========================================================================*
576 *===========================================================================*/
577 PRIVATE
void e1000_writev_s(mp
, 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? */
591 /* We cannot write twice simultaneously.
592 assert(!(e->status & E1000_WRITING)); */
594 /* Copy write message. */
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
),
636 panic("sys_safecopyfrom() failed: %d", r
);
638 /* Mark this descriptor ready. */
643 /* Marks End-of-Packet. */
644 if (i
== e
->tx_message
.DL_COUNT
- 1)
646 desc
->command
= E1000_TX_CMD_EOP
|
650 /* Move to next descriptor. */
651 tail
= (tail
+ 1) % e
->tx_desc_count
;
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
));
662 e
->status
|= E1000_TRANSMIT
;
667 /*===========================================================================*
669 *===========================================================================*/
670 PRIVATE
void e1000_readv_s(mp
, 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? */
685 e
->client
= mp
->m_source
;
686 e
->status
|= E1000_READING
;
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
))
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
),
737 panic("sys_safecopyto() failed: %d", r
);
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
);
756 /*===========================================================================*
758 *===========================================================================*/
759 PRIVATE
void e1000_getstat_s(mp
)
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;
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;
784 sys_safecopyto(mp
->DL_ENDPT
, mp
->DL_GRANT
, 0, (vir_bytes
)&stats
,
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 /*===========================================================================*
793 *===========================================================================*/
794 PRIVATE
void e1000_interrupt(mp
)
800 E1000_DEBUG(3, ("e1000: interrupt\n"));
803 * Check the card for interrupt reason(s).
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
)
834 E1000_DEBUG(4, ("%s: link_changed()\n", e
->name
));
838 /*===========================================================================*
840 *===========================================================================*/
841 PRIVATE
void e1000_stop()
843 E1000_DEBUG(3, ("e1000: stop()\n"));
847 /*===========================================================================*
849 *===========================================================================*/
850 PRIVATE
uint32_t e1000_reg_read(e
, reg
)
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. */
866 /*===========================================================================*
868 *===========================================================================*/
869 PRIVATE
void e1000_reg_write(e
, reg
, value
)
874 /* Assume a sane register. */
875 assert(reg
< 0x1ffff);
877 /* Write to memory mapped register. */
878 *(u32_t
*)(e
->regs
+ reg
) = value
;
881 /*===========================================================================*
883 *===========================================================================*/
884 PRIVATE
void e1000_reg_set(e
, reg
, value
)
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 /*===========================================================================*
900 *===========================================================================*/
901 PRIVATE
void e1000_reg_unset(e
, reg
, value
)
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 /*===========================================================================*
918 *===========================================================================*/
919 PRIVATE u16_t
eeprom_eerd(v
, reg
)
923 e1000_t
*e
= (e1000_t
*) v
;
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
));
935 data
= (e1000_reg_read(e
, E1000_REG_EERD
) &
936 E1000_REG_EERD_DATA
) >> 16;
940 /*===========================================================================*
942 *===========================================================================*/
943 PRIVATE
int eeprom_ich_init(e
)
946 union ich8_hws_flash_status hsfsts
;
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."));
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
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
);
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)
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
,
1013 E1000_DEBUG(3, ("Flash controller busy, cannot get access"));
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
;
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)
1045 while (i
++ < timeout
);
1047 if (hsfsts
.hsf_status
.flcdone
== 1 && hsfsts
.hsf_status
.flcerr
== 0)
1053 /*===========================================================================*
1055 *===========================================================================*/
1056 PRIVATE u16_t
eeprom_ich(v
, 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;
1066 e1000_t
*e
= (e1000_t
*) v
;
1069 E1000_DEBUG(3, ("e1000_read_flash_data_ich8lan"));
1071 if (reg
> ICH_FLASH_LINEAR_ADDR_MASK
)
1074 reg
*= sizeof(u16_t
);
1075 flash_linear_addr
= (ICH_FLASH_LINEAR_ADDR_MASK
& reg
) +
1082 ret_val
= eeprom_ich_init(e
);
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
1103 flash_data
= E1000_READ_FLASH_REG(e
, ICH_FLASH_FDATA0
);
1104 data
= (u16_t
)(flash_data
& 0x0000FFFF);
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. */
1122 else if (hsfsts
.hsf_status
.flcdone
== 0)
1124 E1000_DEBUG(3, ("Timeout error - flash cycle "
1125 "did not complete."));
1129 } while (count
++ < ICH_FLASH_CYCLE_REPEAT_COUNT
);
1135 /*===========================================================================*
1137 *===========================================================================*/
1138 PRIVATE
void reply(e
)
1144 /* Only reply to client for read/write request. */
1145 if (!(e
->status
& E1000_READING
||
1146 e
->status
& E1000_WRITING
))
1150 /* Construct reply message. */
1151 msg
.m_type
= DL_TASK_REPLY
;
1152 msg
.DL_FLAGS
= DL_NOFLAGS
;
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
;
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
;
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 /*===========================================================================*
1185 *===========================================================================*/
1186 PRIVATE
void mess_reply(req
, reply_mess
)
1188 message
*reply_mess
;
1190 if (send(req
->m_source
, reply_mess
) != OK
)
1192 panic("unable to send reply message");