1 /* Attansic/Atheros L2 FastEthernet driver, by D.C. van Moolenbroek */
3 /* No documentation is available for this card. The FreeBSD driver is based
4 * heavily on the official Linux driver; this driver is based heavily on both.
7 #include <minix/drivers.h>
8 #include <minix/netdriver.h>
13 #include <machine/pci.h>
14 #include <net/gen/ether.h>
15 #include <net/gen/eth_io.h>
20 #define VERBOSE 0 /* Verbose debugging output */
21 #define ATL2_FKEY 1 /* Register Shift+F11 for dumping statistics */
24 #define ATL2_DEBUG(x) printf x
32 u8_t data
[ATL2_RXD_SIZE
- sizeof(u32_t
) * 2];
36 int devind
; /* PCI device index */
37 int irq
; /* IRQ number */
38 int hook_id
; /* IRQ hook ID */
39 int mode
; /* datalink mode */
40 volatile u8_t
*base
; /* base address of memory-mapped registers */
41 u32_t size
; /* size of memory-mapped area */
42 u32_t hwaddr
[2]; /* MAC address, in register representation */
44 u8_t
*txd_base
; /* local address of TxD ring buffer base */
45 u32_t
*txs_base
; /* local address of TxS ring buffer base */
46 u8_t
*rxd_base_u
; /* unaligned base address of RxD ring buffer */
47 rxd_t
*rxd_base
; /* local address of RxD ring buffer base */
49 int rxd_align
; /* alignment offset of RxD ring buffer */
51 vir_bytes txd_phys
; /* physical address of TxD ring buffer */
52 vir_bytes txs_phys
; /* physical address of TxS ring buffer */
53 vir_bytes rxd_phys
; /* physical address of RxD ring buffer */
55 int txd_tail
; /* tail index into TxD, in bytes */
56 int txd_num
; /* head-tail offset into TxD, in bytes */
57 int txs_tail
; /* tail index into TxS, in elements */
58 int txs_num
; /* head-tail offset into TxS, in elements */
59 int rxd_tail
; /* tail index into RxD, in elements */
61 int flags
; /* state flags (ATL2_FLAG_) */
62 message read_msg
; /* suspended read request (READ_PEND) */
63 message write_msg
; /* suspended write request (WRITE_PEND) */
64 endpoint_t task_endpt
; /* requester endpoint (PACK_RCVD|PACK_SENT) */
65 size_t recv_count
; /* packet size (PACK_RCVD) */
67 eth_stat_t stat
; /* statistics */
70 #define ATL2_FLAG_RX_AVAIL 0x01 /* packet available for receipt */
71 #define ATL2_FLAG_READ_PEND 0x02 /* read request pending */
72 #define ATL2_FLAG_WRITE_PEND 0x04 /* write request pending */
73 #define ATL2_FLAG_PACK_RCVD 0x08 /* packet received */
74 #define ATL2_FLAG_PACK_SENT 0x10 /* packet transmitted */
76 #define ATL2_READ_U8(off) (* (u8_t *) (state.base + (off)))
77 #define ATL2_READ_U16(off) (* (u16_t *) (state.base + (off)))
78 #define ATL2_READ_U32(off) (* (u32_t *) (state.base + (off)))
79 #define ATL2_WRITE_U8(off, val) * (u8_t *) (state.base + (off)) = (val);
80 #define ATL2_WRITE_U16(off, val) * (u16_t *) (state.base + (off)) = (val);
81 #define ATL2_WRITE_U32(off, val) * (u32_t *) (state.base + (off)) = (val);
83 #define ATL2_ALIGN_32(n) (((n) + 3) & ~3)
85 static iovec_s_t iovec
[NR_IOREQS
];
89 /*===========================================================================*
91 *===========================================================================*/
92 static int atl2_read_vpd(int index
, u32_t
*res
)
94 /* Read a value from the VPD register area.
99 ATL2_WRITE_U32(ATL2_VPD_DATA_REG
, 0);
101 off
= ATL2_VPD_REGBASE
+ index
* sizeof(u32_t
);
103 ATL2_WRITE_U32(ATL2_VPD_CAP_REG
,
104 (off
<< ATL2_VPD_CAP_ADDR_SHIFT
) & ATL2_VPD_CAP_ADDR_MASK
);
106 for (i
= 0; i
< ATL2_VPD_NTRIES
; i
++) {
107 micro_delay(ATL2_VPD_DELAY
);
109 val
= ATL2_READ_U32(ATL2_VPD_CAP_REG
);
110 if (val
& ATL2_VPD_CAP_DONE
)
114 if (i
== ATL2_VPD_NTRIES
) {
115 printf("ATL2: timeout reading EEPROM register %d\n", index
);
119 *res
= ATL2_READ_U32(ATL2_VPD_DATA_REG
);
123 /*===========================================================================*
124 * atl2_get_vpd_hwaddr *
125 *===========================================================================*/
126 static int atl2_get_vpd_hwaddr(void)
128 /* Read the MAC address from the EEPROM, using the Vital Product Data
129 * register interface.
134 /* No idea, copied from FreeBSD which copied it from Linux. */
135 val
= ATL2_READ_U32(ATL2_SPICTL_REG
);
136 if (val
& ATL2_SPICTL_VPD_EN
) {
137 val
&= ~ATL2_SPICTL_VPD_EN
;
138 ATL2_WRITE_U32(ATL2_SPICTL_REG
, val
);
141 /* Is VPD supported? */
142 #ifdef PCI_CAP_VPD /* FIXME: just a guess at the future name */
143 if (!pci_find_cap(state
.devind
, PCI_CAP_VPD
, &n
))
147 /* Read out the set of key/value pairs. Look for the two parts that
148 * make up the MAC address.
150 found
[0] = found
[1] = FALSE
;
151 for (i
= 0; i
< ATL2_VPD_NREGS
; i
+= 2) {
152 if (!atl2_read_vpd(i
, &key
))
155 if ((key
& ATL2_VPD_SIG_MASK
) != ATL2_VPD_SIG
)
158 key
>>= ATL2_VPD_REG_SHIFT
;
160 if (key
!= ATL2_HWADDR0_REG
&& key
!= ATL2_HWADDR1_REG
)
163 if (!atl2_read_vpd(i
+ 1, &val
))
166 n
= (key
== ATL2_HWADDR1_REG
);
167 state
.hwaddr
[n
] = val
;
170 if (found
[1 - n
]) break;
173 return found
[0] && found
[1];
176 /*===========================================================================*
178 *===========================================================================*/
179 static void atl2_get_hwaddr(void)
181 /* Get the MAC address of the card. First try the EEPROM; if that
182 * fails, just use whatever the card was already set to.
185 if (!atl2_get_vpd_hwaddr()) {
186 printf("ATL2: unable to read from VPD\n");
188 state
.hwaddr
[0] = ATL2_READ_U32(ATL2_HWADDR0_REG
);
189 state
.hwaddr
[1] = ATL2_READ_U32(ATL2_HWADDR1_REG
) & 0xffff;
192 ATL2_DEBUG(("ATL2: MAC address %04lx%08lx\n",
193 state
.hwaddr
[1], state
.hwaddr
[0]));
196 /*===========================================================================*
198 *===========================================================================*/
199 static int atl2_read_mdio(int addr
, u16_t
*res
)
201 /* Read a MII PHY register using MDIO.
206 rval
= ((addr
<< ATL2_MDIO_ADDR_SHIFT
) & ATL2_MDIO_ADDR_MASK
) |
207 ATL2_MDIO_START
| ATL2_MDIO_READ
| ATL2_MDIO_SUP_PREAMBLE
|
210 ATL2_WRITE_U32(ATL2_MDIO_REG
, rval
);
212 for (i
= 0; i
< ATL2_MDIO_NTRIES
; i
++) {
213 micro_delay(ATL2_MDIO_DELAY
);
215 rval
= ATL2_READ_U32(ATL2_MDIO_REG
);
217 if (!(rval
& (ATL2_MDIO_START
| ATL2_MDIO_BUSY
)))
221 if (i
== ATL2_MDIO_NTRIES
) return FALSE
;
223 *res
= (u16_t
) (rval
& ATL2_MDIO_DATA_MASK
);
227 /*===========================================================================*
229 *===========================================================================*/
230 static int atl2_alloc_dma(void)
232 /* Allocate DMA ring buffers.
235 state
.txd_base
= alloc_contig(ATL2_TXD_BUFSIZE
,
236 AC_ALIGN4K
, &state
.txd_phys
);
237 state
.txs_base
= alloc_contig(ATL2_TXS_COUNT
* sizeof(u32_t
),
238 AC_ALIGN4K
, &state
.txs_phys
);
240 /* The data buffer in each RxD descriptor must be 128-byte aligned.
241 * The two Tx buffers merely require a 4-byte start alignment.
243 state
.rxd_align
= 128 - offsetof(rxd_t
, data
);
245 alloc_contig(state
.rxd_align
+ ATL2_RXD_COUNT
* ATL2_RXD_SIZE
,
246 AC_ALIGN4K
, &state
.rxd_phys
);
248 /* Unlike mmap, alloc_contig returns NULL on failure. */
249 if (!state
.txd_base
|| !state
.txs_base
|| !state
.rxd_base_u
)
252 state
.rxd_base
= (rxd_t
*) (state
.rxd_base_u
+ state
.rxd_align
);
253 state
.rxd_phys
+= state
.rxd_align
;
255 /* Zero out just in case. */
256 memset(state
.txd_base
, 0, ATL2_TXD_BUFSIZE
);
257 memset(state
.txs_base
, 0, ATL2_TXS_COUNT
* sizeof(u32_t
));
258 memset(state
.rxd_base
, 0, ATL2_RXD_COUNT
* ATL2_RXD_SIZE
);
263 /*===========================================================================*
265 *===========================================================================*/
266 static int atl2_stop(void)
273 /* Clear and disable interrupts. */
274 ATL2_WRITE_U32(ATL2_IMR_REG
, 0);
275 ATL2_WRITE_U32(ATL2_ISR_REG
, 0xffffffff);
277 /* Stop Rx/Tx MACs. */
278 val
= ATL2_READ_U32(ATL2_MAC_REG
);
279 if (val
& (ATL2_MAC_RX_EN
| ATL2_MAC_TX_EN
)) {
280 val
&= ~(ATL2_MAC_RX_EN
| ATL2_MAC_TX_EN
);
281 ATL2_WRITE_U32(ATL2_MAC_REG
, val
);
284 ATL2_WRITE_U8(ATL2_DMAWRITE_REG
, 0);
285 ATL2_WRITE_U8(ATL2_DMAREAD_REG
, 0);
287 /* Wait until everything is idle. */
288 for (i
= 0; i
< ATL2_IDLE_NTRIES
; i
++) {
289 if (ATL2_READ_U32(ATL2_IDLE_REG
) == 0)
292 micro_delay(ATL2_IDLE_DELAY
);
295 /* The caller will generally ignore this return value. */
296 return (i
< ATL2_IDLE_NTRIES
);
299 /*===========================================================================*
301 *===========================================================================*/
302 static int atl2_reset(void)
304 /* Reset the device to a known good state.
309 /* Issue a soft reset, and wait for the device to respond. */
310 ATL2_WRITE_U32(ATL2_MASTER_REG
, ATL2_MASTER_SOFT_RESET
);
312 for (i
= 0; i
< ATL2_RESET_NTRIES
; i
++) {
313 val
= ATL2_READ_U32(ATL2_MASTER_REG
);
314 if (!(val
& ATL2_MASTER_SOFT_RESET
))
317 micro_delay(ATL2_RESET_DELAY
);
320 if (i
== ATL2_RESET_NTRIES
)
323 /* Wait until everything is idle. */
324 for (i
= 0; i
< ATL2_IDLE_NTRIES
; i
++) {
325 if (ATL2_READ_U32(ATL2_IDLE_REG
) == 0)
328 micro_delay(ATL2_IDLE_DELAY
);
331 return (i
< ATL2_IDLE_NTRIES
);
334 /*===========================================================================*
336 *===========================================================================*/
337 static void atl2_set_mode(void)
339 /* Reconfigure the device's promiscuity, multicast, and broadcast mode
344 val
= ATL2_READ_U32(ATL2_MAC_REG
);
345 val
&= ~(ATL2_MAC_PROMISC_EN
| ATL2_MAC_MCAST_EN
| ATL2_MAC_BCAST_EN
);
347 if (state
.mode
& DL_PROMISC_REQ
)
348 val
|= ATL2_MAC_PROMISC_EN
;
349 if (state
.mode
& DL_MULTI_REQ
)
350 val
|= ATL2_MAC_MCAST_EN
;
351 if (state
.mode
& DL_BROAD_REQ
)
352 val
|= ATL2_MAC_BCAST_EN
;
354 ATL2_WRITE_U32(ATL2_MAC_REG
, val
);
357 /*===========================================================================*
359 *===========================================================================*/
360 static int atl2_setup(void)
362 /* Set up the device for normal operation.
371 /* Initialize PCIE module. Magic. */
372 ATL2_WRITE_U32(ATL2_LTSSM_TESTMODE_REG
, ATL2_LTSSM_TESTMODE_DEFAULT
);
373 ATL2_WRITE_U32(ATL2_DLL_TX_CTRL_REG
, ATL2_DLL_TX_CTRL_DEFAULT
);
376 ATL2_WRITE_U32(ATL2_PHY_ENABLE_REG
, ATL2_PHY_ENABLE
);
379 /* Clear and disable interrupts. */
380 ATL2_WRITE_U32(ATL2_ISR_REG
, 0xffffffff);
382 /* Set the MAC address. */
383 ATL2_WRITE_U32(ATL2_HWADDR0_REG
, state
.hwaddr
[0]);
384 ATL2_WRITE_U32(ATL2_HWADDR1_REG
, state
.hwaddr
[1]);
386 /* Initialize ring buffer addresses and sizes. */
387 ATL2_WRITE_U32(ATL2_DESC_ADDR_HI_REG
, 0); /* no 64 bit */
388 ATL2_WRITE_U32(ATL2_TXD_ADDR_LO_REG
, state
.txd_phys
);
389 ATL2_WRITE_U32(ATL2_TXS_ADDR_LO_REG
, state
.txs_phys
);
390 ATL2_WRITE_U32(ATL2_RXD_ADDR_LO_REG
, state
.rxd_phys
);
392 ATL2_WRITE_U16(ATL2_RXD_COUNT_REG
, ATL2_RXD_COUNT
);
393 ATL2_WRITE_U16(ATL2_TXD_BUFSIZE_REG
, ATL2_TXD_BUFSIZE
/ sizeof(u32_t
));
394 ATL2_WRITE_U16(ATL2_TXS_COUNT_REG
, ATL2_TXS_COUNT
);
396 /* A whole lot of other initialization copied from Linux/FreeBSD. */
397 ATL2_WRITE_U32(ATL2_IFG_REG
, ATL2_IFG_DEFAULT
);
399 ATL2_WRITE_U32(ATL2_HDPX_REG
, ATL2_HDPX_DEFAULT
);
401 ATL2_WRITE_U16(ATL2_IMT_REG
, ATL2_IMT_DEFAULT
);
402 val
= ATL2_READ_U32(ATL2_MASTER_REG
);
403 ATL2_WRITE_U32(ATL2_MASTER_REG
, val
| ATL2_MASTER_IMT_EN
);
405 ATL2_WRITE_U16(ATL2_ICT_REG
, ATL2_ICT_DEFAULT
);
407 ATL2_WRITE_U32(ATL2_CUT_THRESH_REG
, ATL2_CUT_THRESH_DEFAULT
);
409 ATL2_WRITE_U16(ATL2_FLOW_THRESH_HI_REG
, (ATL2_RXD_COUNT
/ 8) * 7);
410 ATL2_WRITE_U16(ATL2_FLOW_THRESH_LO_REG
, ATL2_RXD_COUNT
/ 12);
413 ATL2_WRITE_U16(ATL2_MTU_REG
, ATL2_MTU_DEFAULT
);
415 /* Reset descriptors, and enable DMA. */
416 state
.txd_tail
= state
.txs_tail
= state
.rxd_tail
= 0;
417 state
.txd_num
= state
.txs_num
= 0;
418 state
.flags
&= ~ATL2_FLAG_RX_AVAIL
;
419 ATL2_WRITE_U16(ATL2_TXD_IDX_REG
, 0);
420 ATL2_WRITE_U16(ATL2_RXD_IDX_REG
, 0);
422 ATL2_WRITE_U8(ATL2_DMAREAD_REG
, ATL2_DMAREAD_EN
);
423 ATL2_WRITE_U8(ATL2_DMAWRITE_REG
, ATL2_DMAWRITE_EN
);
425 /* Did everything go alright? */
426 val
= ATL2_READ_U32(ATL2_ISR_REG
);
427 if (val
& ATL2_ISR_PHY_LINKDOWN
) {
428 printf("ATL2: initialization failed\n");
432 /* Clear interrupt status. */
433 ATL2_WRITE_U32(ATL2_ISR_REG
, 0x3fffffff);
434 ATL2_WRITE_U32(ATL2_ISR_REG
, 0);
436 /* Enable interrupts. */
437 ATL2_WRITE_U32(ATL2_IMR_REG
, ATL2_IMR_DEFAULT
);
440 ATL2_WRITE_U32(ATL2_MAC_REG
, ATL2_MAC_DEFAULT
);
442 /* Inet does not tell us about the multicast addresses that it is
443 * interested in, so we have to simply accept all multicast packets.
445 ATL2_WRITE_U32(ATL2_MHT0_REG
, 0xffffffff);
446 ATL2_WRITE_U32(ATL2_MHT1_REG
, 0xffffffff);
451 val
= ATL2_READ_U32(ATL2_MAC_REG
);
452 ATL2_WRITE_U32(ATL2_MAC_REG
, val
| ATL2_MAC_TX_EN
| ATL2_MAC_RX_EN
);
457 /*===========================================================================*
459 *===========================================================================*/
460 static int atl2_probe(int skip
)
462 /* Find a matching PCI device.
472 r
= pci_first_dev(&devind
, &vid
, &did
);
477 r
= pci_next_dev(&devind
, &vid
, &did
);
483 dname
= pci_dev_name(vid
, did
);
484 ATL2_DEBUG(("ATL2: found %s (%x/%x) at %s\n",
485 dname
? dname
: "<unknown>", vid
, did
,
486 pci_slot_name(devind
)));
494 /*===========================================================================*
496 *===========================================================================*/
497 static void atl2_init(int devind
)
499 /* Initialize the device.
504 /* Initialize global state. */
505 state
.devind
= devind
;
506 state
.mode
= DL_NOMODE
;
508 state
.recv_count
= 0;
510 memset(&state
.stat
, 0, sizeof(state
.stat
));
512 if ((r
= pci_get_bar(devind
, PCI_BAR
, &bar
, &state
.size
, &flag
)) != OK
)
513 panic("unable to retrieve bar: %d", r
);
515 if (state
.size
< ATL2_MIN_MMAP_SIZE
|| flag
)
516 panic("invalid register bar");
518 state
.base
= vm_map_phys(SELF
, (void *) bar
, state
.size
);
519 if (state
.base
== MAP_FAILED
)
520 panic("unable to map in registers");
522 if ((r
= atl2_alloc_dma()) != OK
)
523 panic("unable to allocate DMA buffers: %d", r
);
525 state
.irq
= pci_attr_r8(devind
, PCI_ILR
);
527 if ((r
= sys_irqsetpolicy(state
.irq
, 0, &state
.hook_id
)) != OK
)
528 panic("unable to register IRQ: %d", r
);
531 panic("unable to reset hardware");
533 if ((r
= sys_irqenable(&state
.hook_id
)) != OK
)
534 panic("unable to enable IRQ: %d", r
);
541 /*===========================================================================*
543 *===========================================================================*/
544 static void atl2_tx_stat(u32_t stat
)
546 /* Update statistics for packet transmission.
549 if (stat
& ATL2_TXS_SUCCESS
)
550 state
.stat
.ets_packetT
++;
552 state
.stat
.ets_recvErr
++;
554 if (stat
& ATL2_TXS_DEFER
)
555 state
.stat
.ets_transDef
++;
556 if (stat
& (ATL2_TXS_EXCDEFER
| ATL2_TXS_ABORTCOL
))
557 state
.stat
.ets_transAb
++;
558 if (stat
& ATL2_TXS_SINGLECOL
)
559 state
.stat
.ets_collision
++;
560 if (stat
& ATL2_TXS_MULTICOL
)
561 state
.stat
.ets_collision
++;
562 if (stat
& ATL2_TXS_LATECOL
)
563 state
.stat
.ets_OWC
++;
564 if (stat
& ATL2_TXS_UNDERRUN
)
565 state
.stat
.ets_fifoUnder
++;
568 /*===========================================================================*
570 *===========================================================================*/
571 static void atl2_rx_stat(u32_t stat
)
573 /* Update statistics for packet receipt.
576 if (stat
& ATL2_RXD_SUCCESS
)
577 state
.stat
.ets_packetR
++;
579 state
.stat
.ets_recvErr
++;
581 if (stat
& ATL2_RXD_CRCERR
)
582 state
.stat
.ets_CRCerr
++;
583 if (stat
& ATL2_RXD_FRAG
)
584 state
.stat
.ets_collision
++;
585 if (stat
& ATL2_RXD_TRUNC
)
586 state
.stat
.ets_fifoOver
++;
587 if (stat
& ATL2_RXD_ALIGN
)
588 state
.stat
.ets_frameAll
++;
591 /*===========================================================================*
593 *===========================================================================*/
594 static int atl2_tx_advance(void)
596 /* Advance the TxD/TxS tails by as many sent packets as found.
598 u32_t stat
, size
, dsize
;
603 while (state
.txs_num
> 0) {
604 /* Has the tail packet been processed by the driver? */
605 stat
= state
.txs_base
[state
.txs_tail
];
607 if (!(stat
& ATL2_TXS_UPDATE
))
610 /* The packet size from the status must match the packet size
611 * we put in. If they don't, there's not much we can do..
613 size
= stat
& ATL2_TXS_SIZE_MASK
;
615 assert((u32_t
) state
.txd_tail
<=
616 ATL2_TXD_BUFSIZE
- sizeof(u32_t
));
617 dsize
= * (u32_t
*) (state
.txd_base
+ state
.txd_tail
);
619 printf("ATL2: TxD/TxS size mismatch (%x vs %x)\n",
622 /* Advance tails accordingly. */
623 size
= sizeof(u32_t
) + ATL2_ALIGN_32(dsize
);
624 assert((u32_t
) state
.txd_num
>= size
);
625 state
.txd_tail
= (state
.txd_tail
+ size
) % ATL2_TXD_BUFSIZE
;
626 state
.txd_num
-= size
;
628 state
.txs_tail
= (state
.txs_tail
+ 1) % ATL2_TXS_COUNT
;
631 if (stat
& ATL2_TXS_SUCCESS
) {
632 ATL2_DEBUG(("ATL2: successfully sent packet\n"));
634 ATL2_DEBUG(("ATL2: failed to send packet\n"));
637 /* Update statistics. */
646 /*===========================================================================*
648 *===========================================================================*/
649 static void atl2_rx_advance(int next
)
651 /* Advance the RxD tail by as many failed receipts as possible, and
652 * see if there is an actual packet left to receive. If 'next' is set,
653 * the packet at the current tail has been processed.
662 state
.rxd_tail
= (state
.rxd_tail
+ 1) % ATL2_RXD_COUNT
;
665 ATL2_DEBUG(("ATL2: successfully received packet\n"));
667 state
.flags
&= ~ATL2_FLAG_RX_AVAIL
;
670 assert(!(state
.flags
& ATL2_FLAG_RX_AVAIL
));
673 /* Check the RxD tail for updates. */
674 rxd
= &state
.rxd_base
[state
.rxd_tail
];
678 if (!(hdr
& ATL2_RXD_UPDATE
))
681 rxd
->hdr
= hdr
& ~(ATL2_RXD_UPDATE
);
683 /* Update statistics. */
686 /* Stop at the first successful receipt. The packet will be
687 * picked up by Inet later.
689 size
= hdr
& ATL2_RXD_SIZE_MASK
;
691 if ((hdr
& ATL2_RXD_SUCCESS
) && size
>= ETH_MIN_PACK_SIZE
) {
692 ATL2_DEBUG(("ATL2: packet available, size %ld\n",
695 state
.flags
|= ATL2_FLAG_RX_AVAIL
;
699 ATL2_DEBUG(("ATL2: packet receipt failed\n"));
702 state
.rxd_tail
= (state
.rxd_tail
+ 1) % ATL2_RXD_COUNT
;
706 /* If new RxD descriptors are now up for reuse, tell the device. */
710 ATL2_WRITE_U32(ATL2_RXD_IDX_REG
, state
.rxd_tail
);
714 /*===========================================================================*
716 *===========================================================================*/
717 static void atl2_reply(void)
719 /* Send a task reply to Inet.
725 if (state
.flags
& ATL2_FLAG_PACK_SENT
)
726 flags
|= DL_PACK_SEND
;
727 if (state
.flags
& ATL2_FLAG_PACK_RCVD
)
728 flags
|= DL_PACK_RECV
;
730 m
.m_type
= DL_TASK_REPLY
;
732 m
.DL_COUNT
= state
.recv_count
;
734 ATL2_DEBUG(("ATL2: sending reply, flags %x count %d\n", flags
,
737 if ((r
= send(state
.task_endpt
, &m
)) != OK
)
738 panic("unable to reply: %d", r
);
740 state
.flags
&= ~(ATL2_FLAG_PACK_SENT
| ATL2_FLAG_PACK_RCVD
);
741 state
.recv_count
= 0;
744 /*===========================================================================*
746 *===========================================================================*/
747 static void atl2_readv(const message
*m
, int from_int
)
753 size_t count
, off
, left
, size
;
757 /* We can deal with only one read request from Inet at a time. */
758 assert(from_int
|| !(state
.flags
& ATL2_FLAG_READ_PEND
));
760 state
.task_endpt
= m
->m_source
;
762 /* Are there any packets available at all? */
763 if (!(state
.flags
& ATL2_FLAG_RX_AVAIL
))
766 /* Get the first available packet's size. Cut off the CRC. */
767 rxd
= &state
.rxd_base
[state
.rxd_tail
];
769 count
= rxd
->hdr
& ATL2_RXD_SIZE_MASK
;
770 count
-= ETH_CRC_SIZE
;
772 ATL2_DEBUG(("ATL2: readv: found packet with length %d\n", count
));
774 /* Copy out the packet. */
779 for (i
= 0; i
< m
->DL_COUNT
&& left
> 0; i
+= batch
) {
780 /* Copy in the next batch. */
781 batch
= MIN(m
->DL_COUNT
- i
, NR_IOREQS
);
783 r
= sys_safecopyfrom(m
->m_source
, m
->DL_GRANT
, off
,
784 (vir_bytes
) iovec
, batch
* sizeof(iovec
[0]));
786 panic("vector copy failed: %d", r
);
788 /* Copy out each element in the batch, until we run out. */
789 for (j
= 0, iovp
= iovec
; j
< batch
&& left
> 0; j
++, iovp
++) {
790 size
= MIN(iovp
->iov_size
, left
);
792 r
= sys_safecopyto(m
->m_source
, iovp
->iov_grant
, 0,
793 (vir_bytes
) pos
, size
);
795 panic("safe copy failed: %d", r
);
801 off
+= batch
* sizeof(iovec
[0]);
804 /* Not sure what to do here. Inet shouldn't mess this up anyway. */
806 printf("ATL2: truncated packet of %d bytes by %d bytes\n",
811 /* We are done with this packet. Move on to the next. */
812 atl2_rx_advance(TRUE
/*next*/);
814 /* We have now successfully received a packet. */
815 state
.flags
&= ~ATL2_FLAG_READ_PEND
;
816 state
.flags
|= ATL2_FLAG_PACK_RCVD
;
817 state
.recv_count
= count
;
819 /* If called from the interrupt handler, the caller will reply. */
826 /* No packets are available at this time. If we were not already
827 * trying to resume receipt, save the read request for later, and tell
828 * Inet that the request has been suspended.
833 state
.flags
|= ATL2_FLAG_READ_PEND
;
839 /*===========================================================================*
841 *===========================================================================*/
842 static void atl2_writev(const message
*m
, int from_int
)
844 /* Write packet data.
847 size_t off
, count
, left
, pos
, skip
;
850 int i
, j
, r
, batch
, maxnum
;
852 /* We can deal with only one write request from Inet at a time. */
853 assert(from_int
|| !(state
.flags
& ATL2_FLAG_WRITE_PEND
));
855 state
.task_endpt
= m
->m_source
;
857 /* If we are already certain that the packet won't fit, bail out.
858 * Keep at least some space between TxD head and tail, as it is not
859 * clear whether the device deals well with the case that they collide.
861 if (state
.txs_num
>= ATL2_TXS_COUNT
)
863 maxnum
= ATL2_TXD_BUFSIZE
- ETH_MIN_PACK_SIZE
- sizeof(u32_t
);
864 if (state
.txd_num
>= maxnum
)
867 /* Optimistically try to copy in the data; suspend if it turns out
868 * that it does not fit.
872 left
= state
.txd_num
- sizeof(u32_t
);
873 pos
= (state
.txd_tail
+ state
.txd_num
+
874 sizeof(u32_t
)) % ATL2_TXD_BUFSIZE
;
876 for (i
= 0; i
< m
->DL_COUNT
; i
+= batch
) {
877 /* Copy in the next batch. */
878 batch
= MIN(m
->DL_COUNT
- i
, NR_IOREQS
);
880 r
= sys_safecopyfrom(m
->m_source
, m
->DL_GRANT
, off
,
881 (vir_bytes
) iovec
, batch
* sizeof(iovec
[0]));
883 panic("vector copy failed: %d", r
);
885 /* Copy in each element in the batch. */
886 for (j
= 0, iovp
= iovec
; j
< batch
; j
++, iovp
++) {
887 size
= iovp
->iov_size
;
892 if (size
> ATL2_TXD_BUFSIZE
- pos
) {
893 skip
= ATL2_TXD_BUFSIZE
- pos
;
894 r
= sys_safecopyfrom(m
->m_source
,
896 (vir_bytes
) (state
.txd_base
+ pos
),
899 panic("safe copy failed: %d", r
);
903 r
= sys_safecopyfrom(m
->m_source
, iovp
->iov_grant
,
904 skip
, (vir_bytes
) (state
.txd_base
+ pos
),
907 panic("safe copy failed: %d", r
);
909 pos
= (pos
+ size
- skip
) % ATL2_TXD_BUFSIZE
;
914 off
+= batch
* sizeof(iovec
[0]);
917 assert(count
<= ETH_MAX_PACK_SIZE_TAGGED
);
919 /* Write the length to the DWORD right before the packet. */
920 sizep
= state
.txd_base
+
921 (state
.txd_tail
+ state
.txd_num
) % ATL2_TXD_BUFSIZE
;
922 * (u32_t
*) sizep
= count
;
924 /* Update the TxD head. */
925 state
.txd_num
+= sizeof(u32_t
) + ATL2_ALIGN_32(count
);
926 pos
= ATL2_ALIGN_32(pos
) % ATL2_TXD_BUFSIZE
;
928 (state
.txd_tail
+ state
.txd_num
) % ATL2_TXD_BUFSIZE
);
930 /* Initialize and update the TxS head. */
931 state
.txs_base
[(state
.txs_tail
+ state
.txs_num
) % ATL2_TXS_COUNT
] = 0;
934 /* Tell the device about our new position. */
937 ATL2_WRITE_U32(ATL2_TXD_IDX_REG
, pos
/ sizeof(u32_t
));
939 /* We have now successfully set up the transmission of a packet. */
940 state
.flags
&= ~ATL2_FLAG_WRITE_PEND
;
941 state
.flags
|= ATL2_FLAG_PACK_SENT
;
943 /* If called from the interrupt handler, the caller will reply. */
950 /* We cannot transmit the packet at this time. If we were not already
951 * trying to resume transmission, save the write request for later,
952 * and tell Inet that the request has been suspended.
957 state
.flags
|= ATL2_FLAG_WRITE_PEND
;
958 state
.write_msg
= *m
;
963 /*===========================================================================*
965 *===========================================================================*/
966 static void atl2_intr(const message
*UNUSED(m
))
968 /* Interrupt received.
971 int r
, try_write
, try_read
;
973 /* Clear and disable interrupts. */
974 val
= ATL2_READ_U32(ATL2_ISR_REG
);
976 ATL2_WRITE_U32(ATL2_ISR_REG
, val
| ATL2_ISR_DISABLE
);
978 ATL2_DEBUG(("ATL2: interrupt (0x%08lx)\n", val
));
980 /* If an error occurred, reset the card. */
981 if (val
& (ATL2_ISR_DMAR_TIMEOUT
| ATL2_ISR_DMAW_TIMEOUT
|
982 ATL2_ISR_PHY_LINKDOWN
)) {
986 try_write
= try_read
= FALSE
;
988 /* Process sent data, and possibly send pending data. */
989 if (val
& ATL2_ISR_TX_EVENT
) {
990 if (atl2_tx_advance())
991 try_write
= (state
.flags
& ATL2_FLAG_WRITE_PEND
);
994 /* Receive new data, and possible satisfy a pending receive request. */
995 if (val
& ATL2_ISR_RX_EVENT
) {
996 if (!(state
.flags
& ATL2_FLAG_RX_AVAIL
)) {
997 atl2_rx_advance(FALSE
/*next*/);
999 try_read
= (state
.flags
& ATL2_FLAG_READ_PEND
);
1003 /* Reenable interrupts. */
1004 ATL2_WRITE_U32(ATL2_ISR_REG
, 0);
1006 if ((r
= sys_irqenable(&state
.hook_id
)) != OK
)
1007 panic("unable to enable IRQ: %d", r
);
1009 /* Attempt to satisfy pending write and read requests. */
1011 atl2_writev(&state
.write_msg
, TRUE
/*from_int*/);
1013 atl2_readv(&state
.read_msg
, TRUE
/*from_int*/);
1014 if (state
.flags
& (ATL2_FLAG_PACK_SENT
| ATL2_FLAG_PACK_RCVD
))
1018 /*===========================================================================*
1020 *===========================================================================*/
1021 static void atl2_conf(message
*m
)
1023 /* Configure the mode of the card.
1028 state
.mode
= m
->DL_MODE
;
1032 addr
.ea_addr
[0] = state
.hwaddr
[1] >> 8;
1033 addr
.ea_addr
[1] = state
.hwaddr
[1] & 0xff;
1034 addr
.ea_addr
[2] = state
.hwaddr
[0] >> 24;
1035 addr
.ea_addr
[3] = (state
.hwaddr
[0] >> 16) & 0xff;
1036 addr
.ea_addr
[4] = (state
.hwaddr
[0] >> 8) & 0xff;
1037 addr
.ea_addr
[5] = state
.hwaddr
[0] & 0xff;
1039 memcpy(m
->DL_HWADDR
, &addr
, sizeof(addr
));
1041 m
->m_type
= DL_CONF_REPLY
;
1044 if ((r
= send(m
->m_source
, m
)) != OK
)
1045 printf("ATL2: unable to send reply (%d)\n", r
);
1048 /*===========================================================================*
1050 *===========================================================================*/
1051 static void atl2_getstat(message
*m
)
1053 /* Copy out statistics.
1057 sys_safecopyto(m
->m_source
, m
->DL_GRANT
, 0,
1058 (vir_bytes
) &state
.stat
, sizeof(state
.stat
));
1060 m
->m_type
= DL_STAT_REPLY
;
1062 if ((r
= send(m
->m_source
, m
)) != OK
)
1063 printf("ATL2: unable to send reply (%d)\n", r
);
1066 /*===========================================================================*
1068 *===========================================================================*/
1069 static void atl2_dump_link(void)
1071 /* Dump link status.
1076 /* The link status bit is latched. Read the status register twice. */
1077 atl2_read_mdio(ATL2_MII_BMSR
, &val
);
1078 if (!atl2_read_mdio(ATL2_MII_BMSR
, &val
)) return;
1080 link_up
= val
& ATL2_MII_BMSR_LSTATUS
;
1081 printf("link status: %4s\t", link_up
? "up" : "down");
1083 if (!link_up
) return;
1085 if (!atl2_read_mdio(ATL2_MII_PSSR
, &val
)) return;
1087 if (!(val
& ATL2_MII_PSSR_RESOLVED
)) {
1088 printf("(not resolved)\n");
1093 switch (val
& ATL2_MII_PSSR_SPEED
) {
1094 case ATL2_MII_PSSR_10
: printf("(10Mbps "); break;
1095 case ATL2_MII_PSSR_100
: printf("(100Mbps "); break;
1096 case ATL2_MII_PSSR_1000
: printf("(1000Mbps "); break;
1097 default: printf("(unknown, ");
1100 printf("%s duplex)", (val
& ATL2_MII_PSSR_DUPLEX
) ? "full" : "half");
1103 /*===========================================================================*
1105 *===========================================================================*/
1106 static void atl2_dump(void)
1112 printf("Attansic L2 statistics:\n");
1114 printf("recvErr: %8ld\t", state
.stat
.ets_recvErr
);
1115 printf("sendErr: %8ld\t", state
.stat
.ets_sendErr
);
1116 printf("OVW: %8ld\n", state
.stat
.ets_OVW
);
1118 printf("CRCerr: %8ld\t", state
.stat
.ets_CRCerr
);
1119 printf("frameAll: %8ld\t", state
.stat
.ets_frameAll
);
1120 printf("missedP: %8ld\n", state
.stat
.ets_missedP
);
1122 printf("packetR: %8ld\t", state
.stat
.ets_packetR
);
1123 printf("packetT: %8ld\t", state
.stat
.ets_packetT
);
1124 printf("transDef: %8ld\n", state
.stat
.ets_transDef
);
1126 printf("collision: %8ld\t", state
.stat
.ets_collision
);
1127 printf("transAb: %8ld\t", state
.stat
.ets_transAb
);
1128 printf("carrSense: %8ld\n", state
.stat
.ets_carrSense
);
1130 printf("fifoUnder: %8ld\t", state
.stat
.ets_fifoUnder
);
1131 printf("fifoOver: %8ld\t", state
.stat
.ets_fifoOver
);
1132 printf("CDheartbeat: %8ld\n", state
.stat
.ets_CDheartbeat
);
1134 printf("OWC: %8ld\t", state
.stat
.ets_OWC
);
1135 printf("TxD tail: %8d\t", state
.txd_tail
);
1136 printf("TxD count: %8d\n", state
.txd_num
);
1138 printf("RxD tail: %8d\t", state
.rxd_tail
);
1139 printf("TxS tail: %8d\t", state
.txs_tail
);
1140 printf("TxS count: %8d\n", state
.txs_num
);
1142 printf("flags: 0x%04x\t", state
.flags
);
1147 /*===========================================================================*
1148 * sef_cb_init_fresh *
1149 *===========================================================================*/
1150 static int sef_cb_init_fresh(int UNUSED(type
), sef_init_info_t
*UNUSED(info
))
1152 /* Initialize the atl2 driver.
1160 /* How many matching devices should we skip? */
1162 (void) env_parse("instance", "d", 0, &v
, 0, 255);
1165 /* Try to find a recognized device. */
1166 devind
= atl2_probe(instance
);
1169 panic("no matching device found");
1171 /* Initialize the device. */
1174 /* Announce we are up! */
1175 netdriver_announce();
1178 /* Register debug dump function key. */
1180 bit_set(sfkeys
, 11);
1181 if ((r
= fkey_map(&fkeys
, &sfkeys
)) != OK
)
1182 printf("ATL2: warning, could not map Shift+F11 key (%d)\n", r
);
1188 /*===========================================================================*
1189 * sef_cb_signal_handler *
1190 *===========================================================================*/
1191 static void sef_cb_signal_handler(int signo
)
1193 /* In case of a termination signal, shut down this driver.
1194 * Stop the device, and deallocate resources as proof of concept.
1198 /* Only check for termination signal, ignore anything else. */
1199 if (signo
!= SIGTERM
) return;
1203 if ((r
= sys_irqrmpolicy(&state
.hook_id
)) != OK
)
1204 panic("unable to deregister IRQ: %d", r
);
1206 free_contig(state
.txd_base
, ATL2_TXD_BUFSIZE
);
1207 free_contig(state
.txs_base
, ATL2_TXS_COUNT
* sizeof(u32_t
));
1208 free_contig(state
.rxd_base_u
,
1209 state
.rxd_align
+ ATL2_RXD_COUNT
* ATL2_RXD_SIZE
);
1211 vm_unmap_phys(SELF
, (void *) state
.base
, state
.size
);
1213 /* We cannot free the PCI device at this time. */
1218 /*===========================================================================*
1219 * sef_local_startup *
1220 *===========================================================================*/
1221 static void sef_local_startup(void)
1223 /* Register init callbacks. */
1224 sef_setcb_init_fresh(sef_cb_init_fresh
);
1225 sef_setcb_init_lu(sef_cb_init_fresh
);
1226 sef_setcb_init_restart(sef_cb_init_fresh
);
1228 /* Register live update callbacks. */
1229 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready
);
1230 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_workfree
);
1232 /* Register signal callbacks. */
1233 sef_setcb_signal_handler(sef_cb_signal_handler
);
1235 /* Let SEF perform startup. */
1239 /*===========================================================================*
1241 *===========================================================================*/
1242 int main(int argc
, char **argv
)
1250 /* Initialize SEF. */
1251 env_setargs(argc
, argv
);
1252 sef_local_startup();
1255 if ((r
= netdriver_receive(ANY
, &m
, &ipc_status
)) != OK
)
1256 panic("netdriver_receive failed: %d", r
);
1258 if (is_ipc_notify(ipc_status
)) {
1259 switch (m
.m_source
) {
1260 case HARDWARE
: /* interrupt */
1265 case TTY_PROC_NR
: /* function key */
1271 printf("ATL2: illegal notify from %d\n",
1278 /* Process requests from Inet. */
1280 case DL_CONF
: atl2_conf(&m
); break;
1281 case DL_GETSTAT_S
: atl2_getstat(&m
); break;
1282 case DL_WRITEV_S
: atl2_writev(&m
, FALSE
); break;
1283 case DL_READV_S
: atl2_readv(&m
, FALSE
); break;
1285 printf("ATL2: illegal message %d from %d\n",
1286 m
.m_type
, m
.m_source
);