4 * This file contains a ethernet device driver for Realtek rtl8169 based
9 #include <minix/drivers.h>
10 #include <minix/netdriver.h>
15 #include <minix/com.h>
17 #include <minix/syslib.h>
18 #include <minix/type.h>
19 #include <minix/sysutil.h>
20 #include <minix/endpoint.h>
23 #include <net/gen/ether.h>
24 #include <net/gen/eth_io.h>
25 #include <machine/pci.h>
27 #include <sys/types.h>
30 #include "kernel/const.h"
31 #include "kernel/config.h"
32 #include "kernel/type.h"
34 #define VERBOSE 0 /* display message during init */
38 #define IOVEC_NR 16 /* I/O vectors are handled IOVEC_NR entries at a time. */
40 #define RE_DTCC_VALUE 600 /* DTCC Update after every 10 minutes */
42 #define RX_CONFIG_MASK 0xff7e1880 /* Clears the bits supported by chip */
44 #define RE_INTR_MASK (RL_IMR_TDU | RL_IMR_FOVW | RL_IMR_PUN | RL_IMR_RDU | RL_IMR_TER | RL_IMR_TOK | RL_IMR_RER | RL_IMR_ROK)
46 #define RL_ENVVAR "RTLETH" /* Configuration */
48 typedef struct re_desc
50 u32_t status
; /* command/status */
51 u32_t vlan
; /* VLAN */
52 u32_t addr_low
; /* low 32-bits of physical buffer address */
53 u32_t addr_high
; /* high 32-bits of physical buffer address */
56 typedef struct re_dtcc
58 u32_t TxOk_low
; /* low 32-bits of Tx Ok packets */
59 u32_t TxOk_high
; /* high 32-bits of Tx Ok packets */
60 u32_t RxOk_low
; /* low 32-bits of Rx Ok packets */
61 u32_t RxOk_high
; /* high 32-bits of Rx Ok packets */
62 u32_t TxEr_low
; /* low 32-bits of Tx errors */
63 u32_t TxEr_high
; /* high 32-bits of Tx errors */
64 u32_t RxEr
; /* Rx errors */
65 u16_t MissPkt
; /* Missed packets */
66 u16_t FAE
; /* Frame Aignment Error packets (MII mode only) */
67 u32_t Tx1Col
; /* Tx Ok packets with only 1 collision happened before Tx Ok */
68 u32_t TxMCol
; /* Tx Ok packets with > 1 and < 16 collisions happened before Tx Ok */
69 u32_t RxOkPhy_low
; /* low 32-bits of Rx Ok packets with physical addr destination ID */
70 u32_t RxOkPhy_high
; /* high 32-bits of Rx Ok packets with physical addr destination ID */
71 u32_t RxOkBrd_low
; /* low 32-bits of Rx Ok packets with broadcast destination ID */
72 u32_t RxOkBrd_high
; /* high 32-bits of Rx Ok packets with broadcast destination ID */
73 u32_t RxOkMul
; /* Rx Ok Packets with multicast destination ID */
74 u16_t TxAbt
; /* Tx abort packets */
75 u16_t TxUndrn
; /* Tx underrun packets */
103 re_desc
*re_rx_desc
; /* Rx descriptor buffer */
104 phys_bytes p_rx_desc
; /* Rx descriptor buffer physical */
113 re_desc
*re_tx_desc
; /* Tx descriptor buffer */
114 phys_bytes p_tx_desc
; /* Tx descriptor buffer physical */
117 int re_seen
; /* TRUE iff device available */
120 int re_hook_id
; /* IRQ hook id at kernel */
122 phys_bytes dtcc_buf
; /* Dump Tally Counter buffer physical */
123 re_dtcc
*v_dtcc_buf
; /* Dump Tally Counter buffer */
124 u32_t dtcc_counter
; /* DTCC update counter */
125 ether_addr_t re_address
;
128 char re_name
[sizeof("rtl8169#n")];
129 iovec_t re_iovec
[IOVEC_NR
];
130 iovec_s_t re_iovec_s
[IOVEC_NR
];
135 #define REM_DISABLED 0x0
136 #define REM_ENABLED 0x1
138 #define REF_PACK_SENT 0x001
139 #define REF_PACK_RECV 0x002
140 #define REF_SEND_AVAIL 0x004
141 #define REF_READING 0x010
142 #define REF_EMPTY 0x000
143 #define REF_PROMISC 0x040
144 #define REF_MULTI 0x080
145 #define REF_BROAD 0x100
146 #define REF_ENABLED 0x200
148 static re_t re_state
;
150 static int re_instance
;
152 static unsigned my_inb(u16_t port
)
156 if ((s
= sys_inb(port
, &value
)) != OK
)
157 printf("RTL8169: warning, sys_inb failed: %d\n", s
);
160 static unsigned my_inw(u16_t port
)
164 if ((s
= sys_inw(port
, &value
)) != OK
)
165 printf("RTL8169: warning, sys_inw failed: %d\n", s
);
168 static unsigned my_inl(u16_t port
)
172 if ((s
= sys_inl(port
, &value
)) != OK
)
173 printf("RTL8169: warning, sys_inl failed: %d\n", s
);
176 #define rl_inb(port, offset) (my_inb((port) + (offset)))
177 #define rl_inw(port, offset) (my_inw((port) + (offset)))
178 #define rl_inl(port, offset) (my_inl((port) + (offset)))
180 static void my_outb(u16_t port
, u8_t value
)
184 if ((s
= sys_outb(port
, value
)) != OK
)
185 printf("RTL8169: warning, sys_outb failed: %d\n", s
);
187 static void my_outw(u16_t port
, u16_t value
)
191 if ((s
= sys_outw(port
, value
)) != OK
)
192 printf("RTL8169: warning, sys_outw failed: %d\n", s
);
194 static void my_outl(u16_t port
, u32_t value
)
198 if ((s
= sys_outl(port
, value
)) != OK
)
199 printf("RTL8169: warning, sys_outl failed: %d\n", s
);
201 #define rl_outb(port, offset, value) (my_outb((port) + (offset), (value)))
202 #define rl_outw(port, offset, value) (my_outw((port) + (offset), (value)))
203 #define rl_outl(port, offset, value) (my_outl((port) + (offset), (value)))
205 static void rl_init(message
*mp
);
206 static void rl_pci_conf(void);
207 static int rl_probe(re_t
*rep
, int skip
);
208 static void rl_conf_hw(re_t
*rep
);
209 static void rl_init_buf(re_t
*rep
);
210 static void rl_init_hw(re_t
*rep
);
211 static void rl_reset_hw(re_t
*rep
);
212 static void rl_confaddr(re_t
*rep
);
213 static void rl_rec_mode(re_t
*rep
);
214 static void rl_readv_s(const message
*mp
, int from_int
);
215 static void rl_writev_s(const message
*mp
, int from_int
);
216 static void rl_check_ints(re_t
*rep
);
217 static void rl_report_link(re_t
*rep
);
218 static void rl_do_reset(re_t
*rep
);
219 static void rl_getstat_s(message
*mp
);
220 static void reply(re_t
*rep
);
221 static void mess_reply(message
*req
, message
*reply
);
222 static void check_int_events(void);
223 static void do_hard_int(void);
224 static void dump_phy(const re_t
*rep
);
225 static void rl_handler(re_t
*rep
);
226 static void rl_watchdog_f(timer_t
*tp
);
229 * The message used in the main loop is made global, so that rl_watchdog_f()
230 * can change its message type to fake an interrupt message.
233 static int int_event_check
; /* set to TRUE if events arrived */
237 /* SEF functions and variables. */
238 static void sef_local_startup(void);
239 static int sef_cb_init_fresh(int type
, sef_init_info_t
*info
);
240 static void sef_cb_signal_handler(int signo
);
242 /*===========================================================================*
244 *===========================================================================*/
245 int main(int argc
, char *argv
[])
250 /* SEF local startup. */
251 env_setargs(argc
, argv
);
255 if ((r
= netdriver_receive(ANY
, &m
, &ipc_status
)) != OK
)
256 panic("netdriver_receive failed: %d", r
);
258 if (is_ipc_notify(ipc_status
)) {
259 switch (_ENDPOINT_P(m
.m_source
)) {
262 * Under MINIX, synchronous alarms are used
263 * instead of watchdog functions.
264 * The approach is very different: MINIX VMD
265 * timeouts are handled within the kernel
266 * (the watchdog is executed by CLOCK), and
267 * notify() the driver in some cases. MINIX
268 * timeouts result in a SYN_ALARM message to
269 * the driver and thus are handled where they
270 * should be handled. Locally, watchdog
271 * functions are used again.
277 if (int_event_check
) {
282 panic("illegal notify from: %d", m
.m_type
);
285 /* done, get nwe message */
290 case DL_WRITEV_S
: rl_writev_s(&m
, FALSE
); break;
291 case DL_READV_S
: rl_readv_s(&m
, FALSE
); break;
292 case DL_CONF
: rl_init(&m
); break;
293 case DL_GETSTAT_S
: rl_getstat_s(&m
); break;
295 panic("illegal message: %d", m
.m_type
);
300 /*===========================================================================*
301 * sef_local_startup *
302 *===========================================================================*/
303 static void sef_local_startup()
305 /* Register init callbacks. */
306 sef_setcb_init_fresh(sef_cb_init_fresh
);
307 sef_setcb_init_lu(sef_cb_init_fresh
);
308 sef_setcb_init_restart(sef_cb_init_fresh
);
310 /* Register live update callbacks. */
311 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready
);
312 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_workfree
);
314 /* Register signal callbacks. */
315 sef_setcb_signal_handler(sef_cb_signal_handler
);
317 /* Let SEF perform startup. */
321 /*===========================================================================*
322 * sef_cb_init_fresh *
323 *===========================================================================*/
324 static int sef_cb_init_fresh(int type
, sef_init_info_t
*UNUSED(info
))
326 /* Initialize the rtl8169 driver. */
329 system_hz
= sys_hz();
332 (void) env_parse("instance", "d", 0, &v
, 0, 255);
333 re_instance
= (int) v
;
335 /* Claim buffer memory now. */
336 rl_init_buf(&re_state
);
338 /* Announce we are up! */
339 netdriver_announce();
344 /*===========================================================================*
345 * sef_cb_signal_handler *
346 *===========================================================================*/
347 static void sef_cb_signal_handler(int signo
)
351 /* Only check for termination signal, ignore anything else. */
352 if (signo
!= SIGTERM
) return;
355 if (rep
->re_mode
== REM_ENABLED
)
356 rl_outb(rep
->re_base_port
, RL_CR
, RL_CR_RST
);
361 static void mdio_write(u16_t port
, int regaddr
, int value
)
365 rl_outl(port
, RL_PHYAR
, 0x80000000 | (regaddr
& 0x1F) << 16 | (value
& 0xFFFF));
367 for (i
= 20; i
> 0; i
--) {
369 * Check if the RTL8169 has completed writing to the specified
372 if (!(rl_inl(port
, RL_PHYAR
) & 0x80000000))
379 static int mdio_read(u16_t port
, int regaddr
)
383 rl_outl(port
, RL_PHYAR
, (regaddr
& 0x1F) << 16);
385 for (i
= 20; i
> 0; i
--) {
387 * Check if the RTL8169 has completed retrieving data from
388 * the specified MII register
390 if (rl_inl(port
, RL_PHYAR
) & 0x80000000) {
391 value
= (int)(rl_inl(port
, RL_PHYAR
) & 0xFFFF);
399 /*===========================================================================*
401 *===========================================================================*/
402 static void check_int_events(void)
408 if (rep
->re_mode
!= REM_ENABLED
)
410 if (!rep
->re_got_int
)
413 assert(rep
->re_flags
& REF_ENABLED
);
417 static void rtl8169_update_stat(re_t
*rep
)
422 port
= rep
->re_base_port
;
424 /* Fetch Missed Packets */
425 rep
->re_stat
.ets_missedP
+= rl_inw(port
, RL_MPC
);
426 rl_outw(port
, RL_MPC
, 0x00);
428 /* Dump Tally Counter Command */
429 rl_outl(port
, RL_DTCCR_HI
, 0); /* 64 bits */
430 rl_outl(port
, RL_DTCCR_LO
, rep
->dtcc_buf
| RL_DTCCR_CMD
);
431 for (i
= 0; i
< 1000; i
++) {
432 if (!(rl_inl(port
, RL_DTCCR_LO
) & RL_DTCCR_CMD
))
437 /* Update counters */
438 rep
->re_stat
.ets_frameAll
= rep
->v_dtcc_buf
->FAE
;
439 rep
->re_stat
.ets_transDef
= rep
->v_dtcc_buf
->TxUndrn
;
440 rep
->re_stat
.ets_transAb
= rep
->v_dtcc_buf
->TxAbt
;
441 rep
->re_stat
.ets_collision
=
442 rep
->v_dtcc_buf
->Tx1Col
+ rep
->v_dtcc_buf
->TxMCol
;
446 /*===========================================================================*
448 *===========================================================================*/
449 static void rtl8169_dump(void)
457 if (rep
->re_mode
== REM_DISABLED
)
458 printf("Realtek RTL 8169 instance %d is disabled\n",
461 if (rep
->re_mode
!= REM_ENABLED
)
464 rtl8169_update_stat(rep
);
466 printf("Realtek RTL 8169 statistics of instance %d:\n", re_instance
);
468 printf("recvErr :%8ld\t", rep
->re_stat
.ets_recvErr
);
469 printf("sendErr :%8ld\t", rep
->re_stat
.ets_sendErr
);
470 printf("OVW :%8ld\n", rep
->re_stat
.ets_OVW
);
472 printf("CRCerr :%8ld\t", rep
->re_stat
.ets_CRCerr
);
473 printf("frameAll :%8ld\t", rep
->re_stat
.ets_frameAll
);
474 printf("missedP :%8ld\n", rep
->re_stat
.ets_missedP
);
476 printf("packetR :%8ld\t", rep
->re_stat
.ets_packetR
);
477 printf("packetT :%8ld\t", rep
->re_stat
.ets_packetT
);
478 printf("transDef :%8ld\n", rep
->re_stat
.ets_transDef
);
480 printf("collision :%8ld\t", rep
->re_stat
.ets_collision
);
481 printf("transAb :%8ld\t", rep
->re_stat
.ets_transAb
);
482 printf("carrSense :%8ld\n", rep
->re_stat
.ets_carrSense
);
484 printf("fifoUnder :%8ld\t", rep
->re_stat
.ets_fifoUnder
);
485 printf("fifoOver :%8ld\t", rep
->re_stat
.ets_fifoOver
);
486 printf("OWC :%8ld\n", rep
->re_stat
.ets_OWC
);
487 printf("interrupts :%8lu\n", rep
->interrupts
);
489 printf("\nRealtek RTL 8169 Tally Counters:\n");
491 dtcc
= rep
->v_dtcc_buf
;
494 printf("TxOk :%8ld%08ld\t", dtcc
->TxOk_high
, dtcc
->TxOk_low
);
496 printf("TxOk :%16lu\t", dtcc
->TxOk_low
);
499 printf("RxOk :%8ld%08ld\n", dtcc
->RxOk_high
, dtcc
->RxOk_low
);
501 printf("RxOk :%16lu\n", dtcc
->RxOk_low
);
504 printf("TxEr :%8ld%08ld\t", dtcc
->TxEr_high
, dtcc
->TxEr_low
);
506 printf("TxEr :%16ld\t", dtcc
->TxEr_low
);
508 printf("RxEr :%16ld\n", dtcc
->RxEr
);
510 printf("Tx1Col :%16ld\t", dtcc
->Tx1Col
);
511 printf("TxMCol :%16ld\n", dtcc
->TxMCol
);
513 if (dtcc
->RxOkPhy_high
)
514 printf("RxOkPhy :%8ld%08ld\t", dtcc
->RxOkPhy_high
, dtcc
->RxOkPhy_low
);
516 printf("RxOkPhy :%16ld\t", dtcc
->RxOkPhy_low
);
518 if (dtcc
->RxOkBrd_high
)
519 printf("RxOkBrd :%8ld%08ld\n", dtcc
->RxOkBrd_high
, dtcc
->RxOkBrd_low
);
521 printf("RxOkBrd :%16ld\n", dtcc
->RxOkBrd_low
);
523 printf("RxOkMul :%16ld\t", dtcc
->RxOkMul
);
524 printf("MissPkt :%16d\n", dtcc
->MissPkt
);
526 printf("\nRealtek RTL 8169 Miscellaneous Info:\n");
528 printf("re_flags : 0x%08x\n", rep
->re_flags
);
529 printf("tx_head :%8d busy %d\t",
530 rep
->re_tx_head
, rep
->re_tx
[rep
->re_tx_head
].ret_busy
);
534 /*===========================================================================*
536 *===========================================================================*/
537 static void rl_init(mp
)
540 static int first_time
= 1;
547 rl_pci_conf(); /* Configure PCI devices. */
549 /* Use a synchronous alarm instead of a watchdog timer. */
550 sys_setalarm(system_hz
, 0);
554 if (rep
->re_mode
== REM_DISABLED
) {
555 /* This is the default, try to (re)locate the device. */
557 if (rep
->re_mode
== REM_DISABLED
) {
558 /* Probe failed, or the device is configured off. */
559 reply_mess
.m_type
= DL_CONF_REPLY
;
560 reply_mess
.DL_STAT
= ENXIO
;
561 mess_reply(mp
, &reply_mess
);
564 if (rep
->re_mode
== REM_ENABLED
)
568 assert(rep
->re_mode
== REM_ENABLED
);
569 assert(rep
->re_flags
& REF_ENABLED
);
571 rep
->re_flags
&= ~(REF_PROMISC
| REF_MULTI
| REF_BROAD
);
573 if (mp
->DL_MODE
& DL_PROMISC_REQ
)
574 rep
->re_flags
|= REF_PROMISC
;
575 if (mp
->DL_MODE
& DL_MULTI_REQ
)
576 rep
->re_flags
|= REF_MULTI
;
577 if (mp
->DL_MODE
& DL_BROAD_REQ
)
578 rep
->re_flags
|= REF_BROAD
;
582 reply_mess
.m_type
= DL_CONF_REPLY
;
583 reply_mess
.DL_STAT
= OK
;
584 *(ether_addr_t
*) reply_mess
.DL_HWADDR
= rep
->re_address
;
586 mess_reply(mp
, &reply_mess
);
589 /*===========================================================================*
591 *===========================================================================*/
592 static void rl_pci_conf()
598 strlcpy(rep
->re_name
, "rtl8169#0", sizeof(rep
->re_name
));
599 rep
->re_name
[8] += re_instance
;
600 rep
->re_seen
= FALSE
;
604 if (rl_probe(rep
, re_instance
))
608 /*===========================================================================*
610 *===========================================================================*/
611 static int rl_probe(rep
, skip
)
623 r
= pci_first_dev(&devind
, &vid
, &did
);
628 r
= pci_next_dev(&devind
, &vid
, &did
);
634 dname
= pci_dev_name(vid
, did
);
636 dname
= "unknown device";
637 printf("%s: ", rep
->re_name
);
638 printf("%s (%x/%x) at %s\n", dname
, vid
, did
, pci_slot_name(devind
));
642 bar
= pci_attr_r32(devind
, PCI_BAR
) & 0xffffffe0;
644 panic("base address is not properly configured");
646 rep
->re_base_port
= bar
;
648 ilr
= pci_attr_r8(devind
, PCI_ILR
);
651 printf("%s: using I/O address 0x%lx, IRQ %d\n",
652 rep
->re_name
, (unsigned long)bar
, ilr
);
658 /*===========================================================================*
660 *===========================================================================*/
661 static void rl_conf_hw(rep
)
664 static eth_stat_t empty_stat
= {0, 0, 0, 0, 0, 0 /* ,... */ };
666 rep
->re_mode
= REM_DISABLED
; /* Superfluous */
669 rep
->re_mode
= REM_ENABLED
; /* PCI device is present */
670 if (rep
->re_mode
!= REM_ENABLED
)
673 rep
->re_flags
= REF_EMPTY
;
676 rep
->re_send_int
= 0;
677 rep
->re_report_link
= 0;
678 rep
->re_need_reset
= 0;
679 rep
->re_tx_alive
= 0;
683 rep
->re_stat
= empty_stat
;
684 rep
->dtcc_counter
= 0;
687 /*===========================================================================*
689 *===========================================================================*/
690 static void rl_init_buf(rep
)
693 size_t rx_bufsize
, tx_bufsize
, rx_descsize
, tx_descsize
, tot_bufsize
;
694 struct re_desc
*desc
;
701 /* Allocate receive and transmit descriptors */
702 rx_descsize
= (N_RX_DESC
* sizeof(struct re_desc
));
703 tx_descsize
= (N_TX_DESC
* sizeof(struct re_desc
));
705 /* Allocate receive and transmit buffers */
706 tx_bufsize
= ETH_MAX_PACK_SIZE_TAGGED
;
708 tx_bufsize
+= 4-(tx_bufsize
% 4); /* Align */
709 rx_bufsize
= RX_BUFSIZE
;
710 tot_bufsize
= rx_descsize
+ tx_descsize
;
711 tot_bufsize
+= (N_TX_DESC
* tx_bufsize
) + (N_RX_DESC
* rx_bufsize
);
712 tot_bufsize
+= sizeof(struct re_dtcc
);
714 if (tot_bufsize
% 4096)
715 tot_bufsize
+= 4096 - (tot_bufsize
% 4096);
717 if (!(mallocbuf
= alloc_contig(tot_bufsize
, AC_ALIGN64K
, &buf
)))
718 panic("Couldn't allocate kernel buffer");
721 rep
->re_rx_desc
= (re_desc
*)mallocbuf
;
722 rep
->p_rx_desc
= buf
;
723 memset(mallocbuf
, 0x00, rx_descsize
);
725 mallocbuf
+= rx_descsize
;
728 rep
->re_tx_desc
= (re_desc
*)mallocbuf
;
729 rep
->p_tx_desc
= buf
;
730 memset(mallocbuf
, 0x00, tx_descsize
);
732 mallocbuf
+= tx_descsize
;
734 desc
= rep
->re_rx_desc
;
735 for (d
= 0; d
< N_RX_DESC
; d
++) {
736 /* Setting Rx buffer */
737 rep
->re_rx
[d
].ret_buf
= buf
;
738 rep
->re_rx
[d
].v_ret_buf
= mallocbuf
;
740 mallocbuf
+= rx_bufsize
;
742 /* Setting Rx descriptor */
743 if (d
== (N_RX_DESC
- 1)) /* Last descriptor? if so, set the EOR bit */
744 desc
->status
= DESC_EOR
| DESC_OWN
| (RX_BUFSIZE
& DESC_RX_LENMASK
);
746 desc
->status
= DESC_OWN
| (RX_BUFSIZE
& DESC_RX_LENMASK
);
748 desc
->addr_low
= rep
->re_rx
[d
].ret_buf
;
751 desc
= rep
->re_tx_desc
;
752 for (d
= 0; d
< N_TX_DESC
; d
++) {
753 rep
->re_tx
[d
].ret_busy
= FALSE
;
754 rep
->re_tx
[d
].ret_buf
= buf
;
755 rep
->re_tx
[d
].v_ret_buf
= mallocbuf
;
757 mallocbuf
+= tx_bufsize
;
759 /* Setting Tx descriptor */
760 desc
->addr_low
= rep
->re_tx
[d
].ret_buf
;
764 /* Dump Tally Counter buffer */
766 rep
->v_dtcc_buf
= (re_dtcc
*)mallocbuf
;
771 /*===========================================================================*
773 *===========================================================================*/
774 static void rl_init_hw(rep
)
782 rep
->re_flags
= REF_EMPTY
;
783 rep
->re_flags
|= REF_ENABLED
;
786 * Set the interrupt handler. The policy is to only send HARD_INT
787 * notifications. Don't reenable interrupts automatically. The id
788 * that is passed back is the interrupt line number.
790 rep
->re_hook_id
= rep
->re_irq
;
791 if ((s
= sys_irqsetpolicy(rep
->re_irq
, 0, &rep
->re_hook_id
)) != OK
)
792 printf("RTL8169: error, couldn't set IRQ policy: %d\n", s
);
796 if ((s
= sys_irqenable(&rep
->re_hook_id
)) != OK
)
797 printf("RTL8169: error, couldn't enable interrupts: %d\n", s
);
800 printf("%s: model: %s mac: 0x%08x\n",
801 rep
->re_name
, rep
->re_model
, rep
->re_mac
);
806 printf("%s: Ethernet address ", rep
->re_name
);
807 for (i
= 0; i
< 6; i
++) {
808 printf("%x%c", rep
->re_address
.ea_addr
[i
],
814 static void rtl8169s_phy_config(port_t port
)
816 mdio_write(port
, 0x1f, 0x0001);
817 mdio_write(port
, 0x06, 0x006e);
818 mdio_write(port
, 0x08, 0x0708);
819 mdio_write(port
, 0x15, 0x4000);
820 mdio_write(port
, 0x18, 0x65c7);
822 mdio_write(port
, 0x1f, 0x0001);
823 mdio_write(port
, 0x03, 0x00a1);
824 mdio_write(port
, 0x02, 0x0008);
825 mdio_write(port
, 0x01, 0x0120);
826 mdio_write(port
, 0x00, 0x1000);
827 mdio_write(port
, 0x04, 0x0800);
828 mdio_write(port
, 0x04, 0x0000);
830 mdio_write(port
, 0x03, 0xff41);
831 mdio_write(port
, 0x02, 0xdf60);
832 mdio_write(port
, 0x01, 0x0140);
833 mdio_write(port
, 0x00, 0x0077);
834 mdio_write(port
, 0x04, 0x7800);
835 mdio_write(port
, 0x04, 0x7000);
837 mdio_write(port
, 0x03, 0x802f);
838 mdio_write(port
, 0x02, 0x4f02);
839 mdio_write(port
, 0x01, 0x0409);
840 mdio_write(port
, 0x00, 0xf0f9);
841 mdio_write(port
, 0x04, 0x9800);
842 mdio_write(port
, 0x04, 0x9000);
844 mdio_write(port
, 0x03, 0xdf01);
845 mdio_write(port
, 0x02, 0xdf20);
846 mdio_write(port
, 0x01, 0xff95);
847 mdio_write(port
, 0x00, 0xba00);
848 mdio_write(port
, 0x04, 0xa800);
849 mdio_write(port
, 0x04, 0xa000);
851 mdio_write(port
, 0x03, 0xff41);
852 mdio_write(port
, 0x02, 0xdf20);
853 mdio_write(port
, 0x01, 0x0140);
854 mdio_write(port
, 0x00, 0x00bb);
855 mdio_write(port
, 0x04, 0xb800);
856 mdio_write(port
, 0x04, 0xb000);
858 mdio_write(port
, 0x03, 0xdf41);
859 mdio_write(port
, 0x02, 0xdc60);
860 mdio_write(port
, 0x01, 0x6340);
861 mdio_write(port
, 0x00, 0x007d);
862 mdio_write(port
, 0x04, 0xd800);
863 mdio_write(port
, 0x04, 0xd000);
865 mdio_write(port
, 0x03, 0xdf01);
866 mdio_write(port
, 0x02, 0xdf20);
867 mdio_write(port
, 0x01, 0x100a);
868 mdio_write(port
, 0x00, 0xa0ff);
869 mdio_write(port
, 0x04, 0xf800);
870 mdio_write(port
, 0x04, 0xf000);
872 mdio_write(port
, 0x1f, 0x0000);
873 mdio_write(port
, 0x0b, 0x0000);
874 mdio_write(port
, 0x00, 0x9200);
877 static void rtl8169scd_phy_config(port_t port
)
879 mdio_write(port
, 0x1f, 0x0001);
880 mdio_write(port
, 0x04, 0x0000);
881 mdio_write(port
, 0x03, 0x00a1);
882 mdio_write(port
, 0x02, 0x0008);
883 mdio_write(port
, 0x01, 0x0120);
884 mdio_write(port
, 0x00, 0x1000);
885 mdio_write(port
, 0x04, 0x0800);
886 mdio_write(port
, 0x04, 0x9000);
887 mdio_write(port
, 0x03, 0x802f);
888 mdio_write(port
, 0x02, 0x4f02);
889 mdio_write(port
, 0x01, 0x0409);
890 mdio_write(port
, 0x00, 0xf099);
891 mdio_write(port
, 0x04, 0x9800);
892 mdio_write(port
, 0x04, 0xa000);
893 mdio_write(port
, 0x03, 0xdf01);
894 mdio_write(port
, 0x02, 0xdf20);
895 mdio_write(port
, 0x01, 0xff95);
896 mdio_write(port
, 0x00, 0xba00);
897 mdio_write(port
, 0x04, 0xa800);
898 mdio_write(port
, 0x04, 0xf000);
899 mdio_write(port
, 0x03, 0xdf01);
900 mdio_write(port
, 0x02, 0xdf20);
901 mdio_write(port
, 0x01, 0x101a);
902 mdio_write(port
, 0x00, 0xa0ff);
903 mdio_write(port
, 0x04, 0xf800);
904 mdio_write(port
, 0x04, 0x0000);
905 mdio_write(port
, 0x1f, 0x0000);
907 mdio_write(port
, 0x1f, 0x0001);
908 mdio_write(port
, 0x10, 0xf41b);
909 mdio_write(port
, 0x14, 0xfb54);
910 mdio_write(port
, 0x18, 0xf5c7);
911 mdio_write(port
, 0x1f, 0x0000);
913 mdio_write(port
, 0x1f, 0x0001);
914 mdio_write(port
, 0x17, 0x0cc0);
915 mdio_write(port
, 0x1f, 0x0000);
918 /*===========================================================================*
920 *===========================================================================*/
921 static void rl_reset_hw(rep
)
928 port
= rep
->re_base_port
;
930 rl_outw(port
, RL_IMR
, 0x0000);
932 /* Reset the device */
933 rl_outb(port
, RL_CR
, RL_CR_RST
);
934 SPIN_UNTIL(!(rl_inb(port
, RL_CR
) & RL_CR_RST
), 1000000);
935 if (rl_inb(port
, RL_CR
) & RL_CR_RST
)
936 printf("rtl8169: reset failed to complete");
937 rl_outw(port
, RL_ISR
, 0xFFFF);
939 /* Get Model and MAC info */
940 t
= rl_inl(port
, RL_TCR
);
941 rep
->re_mac
= (t
& (RL_TCR_HWVER_AM
| RL_TCR_HWVER_BM
));
942 switch (rep
->re_mac
) {
943 case RL_TCR_HWVER_RTL8169
:
944 rep
->re_model
= "RTL8169";
946 rl_outw(port
, RL_CCR_UNDOC
, 0x01);
948 case RL_TCR_HWVER_RTL8169S
:
949 rep
->re_model
= "RTL8169S";
951 rtl8169s_phy_config(port
);
953 rl_outw(port
, RL_CCR_UNDOC
, 0x01);
954 mdio_write(port
, 0x0b, 0x0000); /* w 0x0b 15 0 0 */
956 case RL_TCR_HWVER_RTL8110S
:
957 rep
->re_model
= "RTL8110S";
959 rtl8169s_phy_config(port
);
961 rl_outw(port
, RL_CCR_UNDOC
, 0x01);
963 case RL_TCR_HWVER_RTL8169SB
:
964 rep
->re_model
= "RTL8169SB";
966 mdio_write(port
, 0x1f, 0x02);
967 mdio_write(port
, 0x01, 0x90d0);
968 mdio_write(port
, 0x1f, 0x00);
970 rl_outw(port
, RL_CCR_UNDOC
, 0x01);
972 case RL_TCR_HWVER_RTL8110SCd
:
973 rep
->re_model
= "RTL8110SCd";
975 rtl8169scd_phy_config(port
);
977 rl_outw(port
, RL_CCR_UNDOC
, 0x01);
979 case RL_TCR_HWVER_RTL8105E
:
980 rep
->re_model
= "RTL8105E";
983 rep
->re_model
= "Unknown";
988 mdio_write(port
, MII_CTRL
, MII_CTRL_RST
);
989 for (i
= 0; i
< 1000; i
++) {
990 t
= mdio_read(port
, MII_CTRL
);
991 if (!(t
& MII_CTRL_RST
))
997 t
= mdio_read(port
, MII_CTRL
) | MII_CTRL_ANE
| MII_CTRL_DM
| MII_CTRL_SP_1000
;
998 mdio_write(port
, MII_CTRL
, t
);
1000 t
= mdio_read(port
, MII_ANA
);
1001 t
|= MII_ANA_10THD
| MII_ANA_10TFD
| MII_ANA_100TXHD
| MII_ANA_100TXFD
;
1002 t
|= MII_ANA_PAUSE_SYM
| MII_ANA_PAUSE_ASYM
;
1003 mdio_write(port
, MII_ANA
, t
);
1005 t
= mdio_read(port
, MII_1000_CTRL
) | 0x300;
1006 mdio_write(port
, MII_1000_CTRL
, t
);
1008 /* Restart Auto-Negotiation Process */
1009 t
= mdio_read(port
, MII_CTRL
) | MII_CTRL_ANE
| MII_CTRL_RAN
;
1010 mdio_write(port
, MII_CTRL
, t
);
1012 rl_outw(port
, RL_9346CR
, RL_9346CR_EEM_CONFIG
); /* Unlock */
1014 switch (rep
->re_mac
) {
1015 case RL_TCR_HWVER_RTL8169S
:
1016 case RL_TCR_HWVER_RTL8110S
:
1017 /* Bit-3 and bit-14 of the C+CR register MUST be 1. */
1018 t
= rl_inw(port
, RL_CPLUSCMD
);
1019 rl_outw(port
, RL_CPLUSCMD
, t
| RL_CPLUS_MULRW
| (1 << 14));
1021 case RL_TCR_HWVER_RTL8169
:
1022 case RL_TCR_HWVER_RTL8169SB
:
1023 case RL_TCR_HWVER_RTL8110SCd
:
1024 t
= rl_inw(port
, RL_CPLUSCMD
);
1025 rl_outw(port
, RL_CPLUSCMD
, t
| RL_CPLUS_MULRW
);
1029 rl_outw(port
, RL_INTRMITIGATE
, 0x00);
1031 t
= rl_inb(port
, RL_CR
);
1032 rl_outb(port
, RL_CR
, t
| RL_CR_RE
| RL_CR_TE
);
1035 rl_outw(port
, RL_RMS
, RX_BUFSIZE
); /* Maximum rx packet size */
1036 t
= rl_inl(port
, RL_RCR
) & RX_CONFIG_MASK
;
1037 rl_outl(port
, RL_RCR
, RL_RCR_RXFTH_UNLIM
| RL_RCR_MXDMA_1024
| t
);
1038 rl_outl(port
, RL_RDSAR_LO
, rep
->p_rx_desc
);
1039 rl_outl(port
, RL_RDSAR_HI
, 0x00); /* For 64 bit */
1042 rl_outw(port
, RL_ETTHR
, 0x3f); /* No early transmit */
1043 rl_outl(port
, RL_TCR
, RL_TCR_MXDMA_2048
| RL_TCR_IFG_STD
);
1044 rl_outl(port
, RL_TNPDS_LO
, rep
->p_tx_desc
);
1045 rl_outl(port
, RL_TNPDS_HI
, 0x00); /* For 64 bit */
1047 rl_outw(port
, RL_9346CR
, RL_9346CR_EEM_NORMAL
); /* Lock */
1049 rl_outw(port
, RL_MPC
, 0x00);
1050 rl_outw(port
, RL_MULINT
, rl_inw(port
, RL_MULINT
) & 0xF000);
1051 rl_outw(port
, RL_IMR
, RE_INTR_MASK
);
1054 /*===========================================================================*
1056 *===========================================================================*/
1057 static void rl_confaddr(rep
)
1060 static char eakey
[] = RL_ENVVAR
"#_EA";
1061 static char eafmt
[] = "x:x:x:x:x:x";
1068 /* User defined ethernet address? */
1069 eakey
[sizeof(RL_ENVVAR
)-1] = '0' + re_instance
;
1071 port
= rep
->re_base_port
;
1073 for (i
= 0; i
< 6; i
++) {
1074 if (env_parse(eakey
, eafmt
, i
, &v
, 0x00L
, 0xFFL
) != EP_SET
)
1076 rep
->re_address
.ea_addr
[i
] = v
;
1079 if (i
!= 0 && i
!= 6)
1080 env_panic(eakey
); /* It's all or nothing */
1082 /* Should update ethernet address in hardware */
1084 port
= rep
->re_base_port
;
1085 rl_outb(port
, RL_9346CR
, RL_9346CR_EEM_CONFIG
);
1087 for (i
= 0; i
< 4; i
++)
1088 w
|= (rep
->re_address
.ea_addr
[i
] << (i
* 8));
1089 rl_outl(port
, RL_IDR
, w
);
1091 for (i
= 4; i
< 6; i
++)
1092 w
|= (rep
->re_address
.ea_addr
[i
] << ((i
-4) * 8));
1093 rl_outl(port
, RL_IDR
+ 4, w
);
1094 rl_outb(port
, RL_9346CR
, RL_9346CR_EEM_NORMAL
);
1097 /* Get ethernet address */
1098 for (i
= 0; i
< 6; i
++)
1099 rep
->re_address
.ea_addr
[i
] = rl_inb(port
, RL_IDR
+i
);
1102 /*===========================================================================*
1104 *===========================================================================*/
1105 static void rl_rec_mode(rep
)
1110 u32_t mc_filter
[2]; /* Multicast hash filter */
1112 port
= rep
->re_base_port
;
1114 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
1115 rl_outl(port
, RL_MAR
+ 0, mc_filter
[0]);
1116 rl_outl(port
, RL_MAR
+ 4, mc_filter
[1]);
1118 rcr
= rl_inl(port
, RL_RCR
);
1119 rcr
&= ~(RL_RCR_AB
| RL_RCR_AM
| RL_RCR_APM
| RL_RCR_AAP
);
1120 if (rep
->re_flags
& REF_PROMISC
)
1121 rcr
|= RL_RCR_AB
| RL_RCR_AM
| RL_RCR_AAP
;
1122 if (rep
->re_flags
& REF_BROAD
)
1124 if (rep
->re_flags
& REF_MULTI
)
1127 rl_outl(port
, RL_RCR
, RL_RCR_RXFTH_UNLIM
| RL_RCR_MXDMA_1024
| rcr
);
1130 void transmittest(re_t
*rep
)
1135 tx_head
= rep
->re_tx_head
;
1137 if(rep
->re_tx
[tx_head
].ret_busy
) {
1141 if ((r
= netdriver_receive(ANY
, &m
, &ipc_status
)) != OK
)
1142 panic("netdriver_receive failed: %d", r
);
1143 } while(m
.m_source
!= HARDWARE
);
1144 assert(!(rep
->re_flags
& REF_SEND_AVAIL
));
1145 rep
->re_flags
|= REF_SEND_AVAIL
;
1151 /*===========================================================================*
1153 *===========================================================================*/
1154 static void rl_readv_s(const message
*mp
, int from_int
)
1156 int i
, j
, n
, s
, count
, size
, index
;
1158 unsigned totlen
, packlen
;
1160 u32_t rxstat
= 0x12345678;
1168 rep
->re_client
= mp
->m_source
;
1169 count
= mp
->DL_COUNT
;
1171 assert(rep
->re_mode
== REM_ENABLED
);
1172 assert(rep
->re_flags
& REF_ENABLED
);
1174 port
= rep
->re_base_port
;
1177 * Assume that the RL_CR_BUFE check was been done by rl_checks_ints
1179 if (!from_int
&& (rl_inb(port
, RL_CR
) & RL_CR_BUFE
))
1180 goto suspend
; /* Receive buffer is empty, suspend */
1182 index
= rep
->re_rx_head
;
1183 desc
= rep
->re_rx_desc
;
1186 rxstat
= desc
->status
;
1188 if (rxstat
& DESC_OWN
)
1191 if (rxstat
& DESC_RX_CRC
)
1192 rep
->re_stat
.ets_CRCerr
++;
1194 if ((rxstat
& (DESC_FS
| DESC_LS
)) != (DESC_FS
| DESC_LS
)) {
1196 printf("rl_readv_s: packet is fragmented\n");
1198 /* Fix the fragmented packet */
1199 if (index
== N_RX_DESC
- 1) {
1200 desc
->status
= DESC_EOR
| DESC_OWN
| (RX_BUFSIZE
& DESC_RX_LENMASK
);
1202 desc
= rep
->re_rx_desc
;
1204 desc
->status
= DESC_OWN
| (RX_BUFSIZE
& DESC_RX_LENMASK
);
1208 goto readvs_loop
; /* Loop until we get correct packet */
1211 totlen
= rxstat
& DESC_RX_LENMASK
;
1212 if (totlen
< 8 || totlen
> 2 * ETH_MAX_PACK_SIZE
) {
1213 /* Someting went wrong */
1214 printf("rl_readv_s: bad length (%u) in status 0x%08x\n",
1219 /* Should subtract the CRC */
1220 packlen
= totlen
- ETH_CRC_SIZE
;
1223 for (i
= 0; i
< count
; i
+= IOVEC_NR
,
1224 iov_offset
+= IOVEC_NR
* sizeof(rep
->re_iovec_s
[0]))
1229 cps
= sys_safecopyfrom(mp
->m_source
, mp
->DL_GRANT
, iov_offset
,
1230 (vir_bytes
) rep
->re_iovec_s
,
1231 n
* sizeof(rep
->re_iovec_s
[0]));
1233 panic("rl_readv_s: sys_safecopyfrom failed: %d", cps
);
1236 for (j
= 0, iovp
= rep
->re_iovec_s
; j
< n
; j
++, iovp
++) {
1238 if (size
+ s
> packlen
) {
1239 assert(packlen
> size
);
1243 cps
= sys_safecopyto(mp
->m_source
, iovp
->iov_grant
, 0,
1244 (vir_bytes
) rep
->re_rx
[index
].v_ret_buf
+ size
, s
);
1246 panic("rl_readv_s: sys_safecopyto failed: %d", cps
);
1249 if (size
== packlen
)
1252 if (size
== packlen
)
1258 rep
->re_stat
.ets_packetR
++;
1259 rep
->re_read_s
= packlen
;
1260 if (index
== N_RX_DESC
- 1) {
1261 desc
->status
= DESC_EOR
| DESC_OWN
| (RX_BUFSIZE
& DESC_RX_LENMASK
);
1264 desc
->status
= DESC_OWN
| (RX_BUFSIZE
& DESC_RX_LENMASK
);
1267 rep
->re_rx_head
= index
;
1268 assert(rep
->re_rx_head
< N_RX_DESC
);
1269 rep
->re_flags
= (rep
->re_flags
& ~REF_READING
) | REF_PACK_RECV
;
1278 assert(rep
->re_flags
& REF_READING
);
1280 /* No need to store any state */
1284 rep
->re_rx_mess
= *mp
;
1285 assert(!(rep
->re_flags
& REF_READING
));
1286 rep
->re_flags
|= REF_READING
;
1291 /*===========================================================================*
1293 *===========================================================================*/
1294 static void rl_writev_s(const message
*mp
, int from_int
)
1296 int i
, j
, n
, s
, count
, size
;
1307 rep
->re_client
= mp
->m_source
;
1308 count
= mp
->DL_COUNT
;
1311 assert(rep
->re_mode
== REM_ENABLED
);
1312 assert(rep
->re_flags
& REF_ENABLED
);
1315 assert(rep
->re_flags
& REF_SEND_AVAIL
);
1316 rep
->re_flags
&= ~REF_SEND_AVAIL
;
1317 rep
->re_send_int
= FALSE
;
1318 rep
->re_tx_alive
= TRUE
;
1321 tx_head
= rep
->re_tx_head
;
1323 desc
= rep
->re_tx_desc
;
1326 if(!desc
|| !rep
->re_tx_desc
) {
1327 printf("desc %p, re_tx_desc %p, tx_head %d, setup %d\n",
1328 desc
, rep
->re_tx_desc
, tx_head
, rep
->setup
);
1331 assert(rep
->re_tx_desc
);
1332 assert(rep
->re_tx_head
>= 0 && rep
->re_tx_head
< N_TX_DESC
);
1336 if (rep
->re_tx
[tx_head
].ret_busy
) {
1337 assert(!(rep
->re_flags
& REF_SEND_AVAIL
));
1338 rep
->re_flags
|= REF_SEND_AVAIL
;
1339 if (rep
->re_tx
[tx_head
].ret_busy
)
1343 * Race condition, the interrupt handler may clear re_busy
1344 * before we got a chance to set REF_SEND_AVAIL. Checking
1345 * ret_busy twice should be sufficient.
1348 printf("rl_writev_s: race detected\n");
1350 rep
->re_flags
&= ~REF_SEND_AVAIL
;
1351 rep
->re_send_int
= FALSE
;
1354 assert(!(rep
->re_flags
& REF_SEND_AVAIL
));
1355 assert(!(rep
->re_flags
& REF_PACK_SENT
));
1358 ret
= rep
->re_tx
[tx_head
].v_ret_buf
;
1359 for (i
= 0; i
< count
; i
+= IOVEC_NR
,
1360 iov_offset
+= IOVEC_NR
* sizeof(rep
->re_iovec_s
[0]))
1365 cps
= sys_safecopyfrom(mp
->m_source
, mp
->DL_GRANT
, iov_offset
,
1366 (vir_bytes
) rep
->re_iovec_s
,
1367 n
* sizeof(rep
->re_iovec_s
[0]));
1369 panic("rl_writev_s: sys_safecopyfrom failed: %d", cps
);
1372 for (j
= 0, iovp
= rep
->re_iovec_s
; j
< n
; j
++, iovp
++) {
1374 if (size
+ s
> ETH_MAX_PACK_SIZE_TAGGED
)
1375 panic("invalid packet size");
1377 cps
= sys_safecopyfrom(mp
->m_source
, iovp
->iov_grant
,
1378 0, (vir_bytes
) ret
, s
);
1380 panic("rl_writev_s: sys_safecopyfrom failed: %d", cps
);
1387 if (size
< ETH_MIN_PACK_SIZE
)
1388 panic("invalid packet size: %d", size
);
1390 rep
->re_tx
[tx_head
].ret_busy
= TRUE
;
1392 if (tx_head
== N_TX_DESC
- 1) {
1393 desc
->status
= DESC_EOR
| DESC_OWN
| DESC_FS
| DESC_LS
| size
;
1396 desc
->status
= DESC_OWN
| DESC_FS
| DESC_LS
| size
;
1400 assert(tx_head
< N_TX_DESC
);
1401 rep
->re_tx_head
= tx_head
;
1403 rl_outl(rep
->re_base_port
, RL_TPPOLL
, RL_TPPOLL_NPQ
);
1404 rep
->re_flags
|= REF_PACK_SENT
;
1407 * If the interrupt handler called, don't send a reply. The reply
1408 * will be sent after all interrupts are handled.
1417 panic("should not be sending");
1419 rep
->re_tx_mess
= *mp
;
1423 /*===========================================================================*
1425 *===========================================================================*/
1426 static void rl_check_ints(rep
)
1431 re_flags
= rep
->re_flags
;
1433 if ((re_flags
& REF_READING
) &&
1434 !(rl_inb(rep
->re_base_port
, RL_CR
) & RL_CR_BUFE
))
1436 assert(rep
->re_rx_mess
.m_type
== DL_READV_S
);
1437 rl_readv_s(&rep
->re_rx_mess
, TRUE
/* from int */);
1440 if (rep
->re_need_reset
)
1443 if (rep
->re_send_int
) {
1444 assert(rep
->re_tx_mess
.m_type
== DL_WRITEV_S
);
1445 rl_writev_s(&rep
->re_tx_mess
, TRUE
/* from int */);
1448 if (rep
->re_report_link
) {
1449 rep
->re_report_link
= FALSE
;
1451 rl_report_link(rep
);
1454 if (rep
->re_flags
& (REF_PACK_SENT
| REF_PACK_RECV
))
1458 /*===========================================================================*
1460 *===========================================================================*/
1461 static void rl_report_link(rep
)
1468 port
= rep
->re_base_port
;
1470 mii_status
= rl_inb(port
, RL_PHYSTAT
);
1472 if (mii_status
& RL_STAT_LINK
) {
1473 rep
->re_link_up
= 1;
1474 printf("%s: link up at ", rep
->re_name
);
1476 rep
->re_link_up
= 0;
1477 printf("%s: link down\n", rep
->re_name
);
1481 if (mii_status
& RL_STAT_1000
)
1482 printf("1000 Mbps");
1483 else if (mii_status
& RL_STAT_100
)
1485 else if (mii_status
& RL_STAT_10
)
1488 if (mii_status
& RL_STAT_FULLDUP
)
1489 printf(", full duplex");
1491 printf(", half duplex");
1498 /*===========================================================================*
1500 *===========================================================================*/
1501 static void rl_do_reset(rep
)
1504 rep
->re_need_reset
= FALSE
;
1508 rep
->re_tx_head
= 0;
1509 if (rep
->re_flags
& REF_SEND_AVAIL
) {
1510 rep
->re_tx
[rep
->re_tx_head
].ret_busy
= FALSE
;
1511 rep
->re_send_int
= TRUE
;
1515 /*===========================================================================*
1517 *===========================================================================*/
1518 static void rl_getstat_s(mp
)
1527 assert(rep
->re_mode
== REM_ENABLED
);
1528 assert(rep
->re_flags
& REF_ENABLED
);
1530 stats
= rep
->re_stat
;
1532 r
= sys_safecopyto(mp
->m_source
, mp
->DL_GRANT
, 0,
1533 (vir_bytes
) &stats
, sizeof(stats
));
1535 panic("rl_getstat_s: sys_safecopyto failed: %d", r
);
1537 mp
->m_type
= DL_STAT_REPLY
;
1538 r
= send(mp
->m_source
, mp
);
1540 panic("rl_getstat_s: send failed: %d", r
);
1543 /*===========================================================================*
1545 *===========================================================================*/
1546 static void reply(rep
)
1554 if (rep
->re_flags
& REF_PACK_SENT
)
1555 flags
|= DL_PACK_SEND
;
1556 if (rep
->re_flags
& REF_PACK_RECV
)
1557 flags
|= DL_PACK_RECV
;
1559 reply
.m_type
= DL_TASK_REPLY
;
1560 reply
.DL_FLAGS
= flags
;
1561 reply
.DL_COUNT
= rep
->re_read_s
;
1563 r
= send(rep
->re_client
, &reply
);
1566 printf("RTL8169 tried sending to %d, type %d\n",
1567 rep
->re_client
, reply
.m_type
);
1568 panic("send failed: %d", r
);
1572 rep
->re_flags
&= ~(REF_PACK_SENT
| REF_PACK_RECV
);
1575 /*===========================================================================*
1577 *===========================================================================*/
1578 static void mess_reply(req
, reply_mess
)
1580 message
*reply_mess
;
1582 if (send(req
->m_source
, reply_mess
) != OK
)
1583 panic("unable to mess_reply");
1586 static void dump_phy(const re_t
*rep
)
1592 port
= rep
->re_base_port
;
1594 t
= rl_inb(port
, RL_CONFIG0
);
1595 printf("CONFIG0\t\t:");
1596 t
= t
& RL_CFG0_ROM
;
1597 if (t
== RL_CFG0_ROM128K
)
1598 printf(" 128K Boot ROM");
1599 else if (t
== RL_CFG0_ROM64K
)
1600 printf(" 64K Boot ROM");
1601 else if (t
== RL_CFG0_ROM32K
)
1602 printf(" 32K Boot ROM");
1603 else if (t
== RL_CFG0_ROM16K
)
1604 printf(" 16K Boot ROM");
1605 else if (t
== RL_CFG0_ROM8K
)
1606 printf(" 8K Boot ROM");
1607 else if (t
== RL_CFG0_ROMNO
)
1608 printf(" No Boot ROM");
1611 t
= rl_inb(port
, RL_CONFIG1
);
1612 printf("CONFIG1\t\t:");
1613 if (t
& RL_CFG1_LEDS1
)
1615 if (t
& RL_CFG1_LEDS0
)
1617 if (t
& RL_CFG1_DVRLOAD
)
1619 if (t
& RL_CFG1_LWACT
)
1621 if (t
& RL_CFG1_IOMAP
)
1623 if (t
& RL_CFG1_MEMMAP
)
1625 if (t
& RL_CFG1_VPD
)
1627 if (t
& RL_CFG1_PME
)
1631 t
= rl_inb(port
, RL_CONFIG2
);
1632 printf("CONFIG2\t\t:");
1633 if (t
& RL_CFG2_AUX
)
1635 if (t
& RL_CFG2_PCIBW
)
1636 printf(" PCI-64-Bit");
1638 printf(" PCI-32-Bit");
1639 t
= t
& RL_CFG2_PCICLK
;
1640 if (t
== RL_CFG2_66MHZ
)
1642 else if (t
== RL_CFG2_33MHZ
)
1646 t
= mdio_read(port
, MII_CTRL
);
1647 printf("MII_CTRL\t:");
1648 if (t
& MII_CTRL_RST
)
1650 if (t
& MII_CTRL_LB
)
1651 printf(" Loopback");
1652 if (t
& MII_CTRL_ANE
)
1654 if (t
& MII_CTRL_PD
)
1655 printf(" Power-down");
1656 if (t
& MII_CTRL_ISO
)
1658 if (t
& MII_CTRL_RAN
)
1660 if (t
& MII_CTRL_DM
)
1661 printf(" Full-duplex");
1662 if (t
& MII_CTRL_CT
)
1663 printf(" COL-signal");
1664 t
= t
& (MII_CTRL_SP_LSB
| MII_CTRL_SP_MSB
);
1665 if (t
== MII_CTRL_SP_10
)
1667 else if (t
== MII_CTRL_SP_100
)
1668 printf(" 100 Mb/s");
1669 else if (t
== MII_CTRL_SP_1000
)
1670 printf(" 1000 Mb/s");
1673 t
= mdio_read(port
, MII_STATUS
);
1674 printf("MII_STATUS\t:");
1675 if (t
& MII_STATUS_100T4
)
1676 printf(" 100Base-T4");
1677 if (t
& MII_STATUS_100XFD
)
1678 printf(" 100BaseX-FD");
1679 if (t
& MII_STATUS_100XHD
)
1680 printf(" 100BaseX-HD");
1681 if (t
& MII_STATUS_10FD
)
1682 printf(" 10Mbps-FD");
1683 if (t
& MII_STATUS_10HD
)
1684 printf(" 10Mbps-HD");
1685 if (t
& MII_STATUS_100T2FD
)
1686 printf(" 100Base-T2-FD");
1687 if (t
& MII_STATUS_100T2HD
)
1688 printf(" 100Base-T2-HD");
1689 if (t
& MII_STATUS_EXT_STAT
)
1690 printf(" Ext-stat");
1691 if (t
& MII_STATUS_RES
)
1692 printf(" res-0x%x", t
& MII_STATUS_RES
);
1693 if (t
& MII_STATUS_MFPS
)
1695 if (t
& MII_STATUS_ANC
)
1697 if (t
& MII_STATUS_RF
)
1698 printf(" remote-fault");
1699 if (t
& MII_STATUS_ANA
)
1701 if (t
& MII_STATUS_LS
)
1703 if (t
& MII_STATUS_JD
)
1705 if (t
& MII_STATUS_EC
)
1706 printf(" Extended-capability");
1709 t
= mdio_read(port
, MII_ANA
);
1710 printf("MII_ANA\t\t: 0x%04x\n", t
);
1712 t
= mdio_read(port
, MII_ANLPA
);
1713 printf("MII_ANLPA\t: 0x%04x\n", t
);
1715 t
= mdio_read(port
, MII_ANE
);
1716 printf("MII_ANE\t\t:");
1717 if (t
& MII_ANE_RES
)
1718 printf(" res-0x%x", t
& MII_ANE_RES
);
1719 if (t
& MII_ANE_PDF
)
1720 printf(" Par-Detect-Fault");
1721 if (t
& MII_ANE_LPNPA
)
1722 printf(" LP-Next-Page-Able");
1723 if (t
& MII_ANE_NPA
)
1724 printf(" Loc-Next-Page-Able");
1726 printf(" Page-Received");
1727 if (t
& MII_ANE_LPANA
)
1728 printf(" LP-Auto-Neg-Able");
1731 t
= mdio_read(port
, MII_1000_CTRL
);
1732 printf("MII_1000_CTRL\t:");
1733 if (t
& MII_1000C_FULL
)
1734 printf(" 1000BaseT-FD");
1735 if (t
& MII_1000C_HALF
)
1736 printf(" 1000BaseT-HD");
1739 t
= mdio_read(port
, MII_1000_STATUS
);
1741 printf("MII_1000_STATUS\t:");
1742 if (t
& MII_1000S_LRXOK
)
1743 printf(" Local-Receiver");
1744 if (t
& MII_1000S_RRXOK
)
1745 printf(" Remote-Receiver");
1746 if (t
& MII_1000S_HALF
)
1747 printf(" 1000BaseT-HD");
1748 if (t
& MII_1000S_FULL
)
1749 printf(" 1000BaseT-FD");
1752 t
= mdio_read(port
, MII_EXT_STATUS
);
1753 printf("MII_EXT_STATUS\t:");
1754 if (t
& MII_ESTAT_1000XFD
)
1755 printf(" 1000BaseX-FD");
1756 if (t
& MII_ESTAT_1000XHD
)
1757 printf(" 1000BaseX-HD");
1758 if (t
& MII_ESTAT_1000TFD
)
1759 printf(" 1000BaseT-FD");
1760 if (t
& MII_ESTAT_1000THD
)
1761 printf(" 1000BaseT-HD");
1767 static void do_hard_int(void)
1771 /* Run interrupt handler at driver level. */
1772 rl_handler(&re_state
);
1774 /* Reenable interrupts for this hook. */
1775 if ((s
= sys_irqenable(&re_state
.re_hook_id
)) != OK
)
1776 printf("RTL8169: error, couldn't enable interrupts: %d\n", s
);
1779 /*===========================================================================*
1781 *===========================================================================*/
1782 static void rl_handler(re_t
*rep
)
1784 int i
, port
, tx_head
, tx_tail
, link_up
;
1787 int_event_check
= FALSE
; /* disable check by default */
1789 port
= rep
->re_base_port
;
1792 isr
= rl_inw(port
, RL_ISR
);
1795 rl_outw(port
, RL_ISR
, isr
);
1798 if (isr
& RL_IMR_FOVW
) {
1799 isr
&= ~RL_IMR_FOVW
;
1800 /* Should do anything? */
1802 rep
->re_stat
.ets_fifoOver
++;
1804 if (isr
& RL_IMR_PUN
) {
1808 * Either the link status changed or there was a TX fifo
1811 link_up
= !(!(rl_inb(port
, RL_PHYSTAT
) & RL_STAT_LINK
));
1812 if (link_up
!= rep
->re_link_up
) {
1813 rep
->re_report_link
= TRUE
;
1814 rep
->re_got_int
= TRUE
;
1815 int_event_check
= TRUE
;
1819 if (isr
& (RL_ISR_RDU
| RL_ISR_RER
| RL_ISR_ROK
)) {
1820 if (isr
& RL_ISR_RER
)
1821 rep
->re_stat
.ets_recvErr
++;
1822 isr
&= ~(RL_ISR_RDU
| RL_ISR_RER
| RL_ISR_ROK
);
1824 if (!rep
->re_got_int
&& (rep
->re_flags
& REF_READING
)) {
1825 rep
->re_got_int
= TRUE
;
1826 int_event_check
= TRUE
;
1830 if ((isr
& (RL_ISR_TDU
| RL_ISR_TER
| RL_ISR_TOK
)) || 1) {
1831 if (isr
& RL_ISR_TER
)
1832 rep
->re_stat
.ets_sendErr
++;
1833 isr
&= ~(RL_ISR_TDU
| RL_ISR_TER
| RL_ISR_TOK
);
1835 /* Transmit completed */
1836 tx_head
= rep
->re_tx_head
;
1837 tx_tail
= tx_head
+1;
1838 if (tx_tail
>= N_TX_DESC
)
1840 for (i
= 0; i
< 2 * N_TX_DESC
; i
++) {
1841 if (!rep
->re_tx
[tx_tail
].ret_busy
) {
1842 /* Strange, this buffer is not in-use.
1843 * Increment tx_tail until tx_head is
1844 * reached (or until we find a buffer that
1847 if (tx_tail
== tx_head
)
1849 if (++tx_tail
>= N_TX_DESC
)
1851 assert(tx_tail
< N_TX_DESC
);
1854 desc
= rep
->re_tx_desc
;
1856 if (desc
->status
& DESC_OWN
) {
1857 /* Buffer is not yet ready */
1861 rep
->re_stat
.ets_packetT
++;
1862 rep
->re_tx
[tx_tail
].ret_busy
= FALSE
;
1864 if (++tx_tail
>= N_TX_DESC
)
1866 assert(tx_tail
< N_TX_DESC
);
1868 if (rep
->re_flags
& REF_SEND_AVAIL
) {
1869 rep
->re_send_int
= TRUE
;
1870 if (!rep
->re_got_int
) {
1871 rep
->re_got_int
= TRUE
;
1872 int_event_check
= TRUE
;
1876 assert(i
< 2 * N_TX_DESC
);
1879 /* Ignore Reserved Interrupt */
1880 if (isr
& RL_ISR_RES
)
1884 printf("rl_handler: unhandled interrupt isr = 0x%04x\n", isr
);
1887 /*===========================================================================*
1889 *===========================================================================*/
1890 static void rl_watchdog_f(tp
)
1894 /* Use a synchronous alarm instead of a watchdog timer. */
1895 sys_setalarm(system_hz
, 0);
1899 if (rep
->re_mode
!= REM_ENABLED
)
1902 /* Should collect statistics */
1903 if (!(++rep
->dtcc_counter
% RE_DTCC_VALUE
))
1904 rtl8169_update_stat(rep
);
1906 if (!(rep
->re_flags
& REF_SEND_AVAIL
)) {
1907 /* Assume that an idle system is alive */
1908 rep
->re_tx_alive
= TRUE
;
1911 if (rep
->re_tx_alive
) {
1912 rep
->re_tx_alive
= FALSE
;
1915 printf("rl_watchdog_f: resetting instance %d mode 0x%x flags 0x%x\n",
1916 re_instance
, rep
->re_mode
, rep
->re_flags
);
1917 printf("tx_head :%8d busy %d\t",
1918 rep
->re_tx_head
, rep
->re_tx
[rep
->re_tx_head
].ret_busy
);
1919 rep
->re_need_reset
= TRUE
;
1920 rep
->re_got_int
= TRUE
;