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
);
528 if ((r
= sys_irqsetpolicy(state
.irq
, 0, &state
.hook_id
)) != OK
)
529 panic("unable to register IRQ: %d", r
);
532 panic("unable to reset hardware");
534 if ((r
= sys_irqenable(&state
.hook_id
)) != OK
)
535 panic("unable to enable IRQ: %d", r
);
542 /*===========================================================================*
544 *===========================================================================*/
545 static void atl2_tx_stat(u32_t stat
)
547 /* Update statistics for packet transmission.
550 if (stat
& ATL2_TXS_SUCCESS
)
551 state
.stat
.ets_packetT
++;
553 state
.stat
.ets_recvErr
++;
555 if (stat
& ATL2_TXS_DEFER
)
556 state
.stat
.ets_transDef
++;
557 if (stat
& (ATL2_TXS_EXCDEFER
| ATL2_TXS_ABORTCOL
))
558 state
.stat
.ets_transAb
++;
559 if (stat
& ATL2_TXS_SINGLECOL
)
560 state
.stat
.ets_collision
++;
561 if (stat
& ATL2_TXS_MULTICOL
)
562 state
.stat
.ets_collision
++;
563 if (stat
& ATL2_TXS_LATECOL
)
564 state
.stat
.ets_OWC
++;
565 if (stat
& ATL2_TXS_UNDERRUN
)
566 state
.stat
.ets_fifoUnder
++;
569 /*===========================================================================*
571 *===========================================================================*/
572 static void atl2_rx_stat(u32_t stat
)
574 /* Update statistics for packet receipt.
577 if (stat
& ATL2_RXD_SUCCESS
)
578 state
.stat
.ets_packetR
++;
580 state
.stat
.ets_recvErr
++;
582 if (stat
& ATL2_RXD_CRCERR
)
583 state
.stat
.ets_CRCerr
++;
584 if (stat
& ATL2_RXD_FRAG
)
585 state
.stat
.ets_collision
++;
586 if (stat
& ATL2_RXD_TRUNC
)
587 state
.stat
.ets_fifoOver
++;
588 if (stat
& ATL2_RXD_ALIGN
)
589 state
.stat
.ets_frameAll
++;
592 /*===========================================================================*
594 *===========================================================================*/
595 static int atl2_tx_advance(void)
597 /* Advance the TxD/TxS tails by as many sent packets as found.
599 u32_t stat
, size
, dsize
;
604 while (state
.txs_num
> 0) {
605 /* Has the tail packet been processed by the driver? */
606 stat
= state
.txs_base
[state
.txs_tail
];
608 if (!(stat
& ATL2_TXS_UPDATE
))
611 /* The packet size from the status must match the packet size
612 * we put in. If they don't, there's not much we can do..
614 size
= stat
& ATL2_TXS_SIZE_MASK
;
616 assert((u32_t
) state
.txd_tail
<=
617 ATL2_TXD_BUFSIZE
- sizeof(u32_t
));
618 dsize
= * (u32_t
*) (state
.txd_base
+ state
.txd_tail
);
620 printf("ATL2: TxD/TxS size mismatch (%x vs %x)\n",
623 /* Advance tails accordingly. */
624 size
= sizeof(u32_t
) + ATL2_ALIGN_32(dsize
);
625 assert((u32_t
) state
.txd_num
>= size
);
626 state
.txd_tail
= (state
.txd_tail
+ size
) % ATL2_TXD_BUFSIZE
;
627 state
.txd_num
-= size
;
629 state
.txs_tail
= (state
.txs_tail
+ 1) % ATL2_TXS_COUNT
;
632 if (stat
& ATL2_TXS_SUCCESS
) {
633 ATL2_DEBUG(("ATL2: successfully sent packet\n"));
635 ATL2_DEBUG(("ATL2: failed to send packet\n"));
638 /* Update statistics. */
647 /*===========================================================================*
649 *===========================================================================*/
650 static void atl2_rx_advance(int next
)
652 /* Advance the RxD tail by as many failed receipts as possible, and
653 * see if there is an actual packet left to receive. If 'next' is set,
654 * the packet at the current tail has been processed.
663 state
.rxd_tail
= (state
.rxd_tail
+ 1) % ATL2_RXD_COUNT
;
666 ATL2_DEBUG(("ATL2: successfully received packet\n"));
668 state
.flags
&= ~ATL2_FLAG_RX_AVAIL
;
671 assert(!(state
.flags
& ATL2_FLAG_RX_AVAIL
));
674 /* Check the RxD tail for updates. */
675 rxd
= &state
.rxd_base
[state
.rxd_tail
];
679 if (!(hdr
& ATL2_RXD_UPDATE
))
682 rxd
->hdr
= hdr
& ~(ATL2_RXD_UPDATE
);
684 /* Update statistics. */
687 /* Stop at the first successful receipt. The packet will be
688 * picked up by Inet later.
690 size
= hdr
& ATL2_RXD_SIZE_MASK
;
692 if ((hdr
& ATL2_RXD_SUCCESS
) && size
>= ETH_MIN_PACK_SIZE
) {
693 ATL2_DEBUG(("ATL2: packet available, size %ld\n",
696 state
.flags
|= ATL2_FLAG_RX_AVAIL
;
700 ATL2_DEBUG(("ATL2: packet receipt failed\n"));
703 state
.rxd_tail
= (state
.rxd_tail
+ 1) % ATL2_RXD_COUNT
;
707 /* If new RxD descriptors are now up for reuse, tell the device. */
711 ATL2_WRITE_U32(ATL2_RXD_IDX_REG
, state
.rxd_tail
);
715 /*===========================================================================*
717 *===========================================================================*/
718 static void atl2_reply(void)
720 /* Send a task reply to Inet.
726 if (state
.flags
& ATL2_FLAG_PACK_SENT
)
727 flags
|= DL_PACK_SEND
;
728 if (state
.flags
& ATL2_FLAG_PACK_RCVD
)
729 flags
|= DL_PACK_RECV
;
731 m
.m_type
= DL_TASK_REPLY
;
733 m
.DL_COUNT
= state
.recv_count
;
735 ATL2_DEBUG(("ATL2: sending reply, flags %x count %d\n", flags
,
738 if ((r
= send(state
.task_endpt
, &m
)) != OK
)
739 panic("unable to reply: %d", r
);
741 state
.flags
&= ~(ATL2_FLAG_PACK_SENT
| ATL2_FLAG_PACK_RCVD
);
742 state
.recv_count
= 0;
745 /*===========================================================================*
747 *===========================================================================*/
748 static void atl2_readv(const message
*m
, int from_int
)
754 size_t count
, off
, left
, size
;
758 /* We can deal with only one read request from Inet at a time. */
759 assert(from_int
|| !(state
.flags
& ATL2_FLAG_READ_PEND
));
761 state
.task_endpt
= m
->m_source
;
763 /* Are there any packets available at all? */
764 if (!(state
.flags
& ATL2_FLAG_RX_AVAIL
))
767 /* Get the first available packet's size. Cut off the CRC. */
768 rxd
= &state
.rxd_base
[state
.rxd_tail
];
770 count
= rxd
->hdr
& ATL2_RXD_SIZE_MASK
;
771 count
-= ETH_CRC_SIZE
;
773 ATL2_DEBUG(("ATL2: readv: found packet with length %d\n", count
));
775 /* Copy out the packet. */
780 for (i
= 0; i
< m
->DL_COUNT
&& left
> 0; i
+= batch
) {
781 /* Copy in the next batch. */
782 batch
= MIN(m
->DL_COUNT
- i
, NR_IOREQS
);
784 r
= sys_safecopyfrom(m
->m_source
, m
->DL_GRANT
, off
,
785 (vir_bytes
) iovec
, batch
* sizeof(iovec
[0]));
787 panic("vector copy failed: %d", r
);
789 /* Copy out each element in the batch, until we run out. */
790 for (j
= 0, iovp
= iovec
; j
< batch
&& left
> 0; j
++, iovp
++) {
791 size
= MIN(iovp
->iov_size
, left
);
793 r
= sys_safecopyto(m
->m_source
, iovp
->iov_grant
, 0,
794 (vir_bytes
) pos
, size
);
796 panic("safe copy failed: %d", r
);
802 off
+= batch
* sizeof(iovec
[0]);
805 /* Not sure what to do here. Inet shouldn't mess this up anyway. */
807 printf("ATL2: truncated packet of %d bytes by %d bytes\n",
812 /* We are done with this packet. Move on to the next. */
813 atl2_rx_advance(TRUE
/*next*/);
815 /* We have now successfully received a packet. */
816 state
.flags
&= ~ATL2_FLAG_READ_PEND
;
817 state
.flags
|= ATL2_FLAG_PACK_RCVD
;
818 state
.recv_count
= count
;
820 /* If called from the interrupt handler, the caller will reply. */
827 /* No packets are available at this time. If we were not already
828 * trying to resume receipt, save the read request for later, and tell
829 * Inet that the request has been suspended.
834 state
.flags
|= ATL2_FLAG_READ_PEND
;
840 /*===========================================================================*
842 *===========================================================================*/
843 static void atl2_writev(const message
*m
, int from_int
)
845 /* Write packet data.
848 size_t off
, count
, left
, pos
, skip
;
851 int i
, j
, r
, batch
, maxnum
;
853 /* We can deal with only one write request from Inet at a time. */
854 assert(from_int
|| !(state
.flags
& ATL2_FLAG_WRITE_PEND
));
856 state
.task_endpt
= m
->m_source
;
858 /* If we are already certain that the packet won't fit, bail out.
859 * Keep at least some space between TxD head and tail, as it is not
860 * clear whether the device deals well with the case that they collide.
862 if (state
.txs_num
>= ATL2_TXS_COUNT
)
864 maxnum
= ATL2_TXD_BUFSIZE
- ETH_MIN_PACK_SIZE
- sizeof(u32_t
);
865 if (state
.txd_num
>= maxnum
)
868 /* Optimistically try to copy in the data; suspend if it turns out
869 * that it does not fit.
873 left
= state
.txd_num
- sizeof(u32_t
);
874 pos
= (state
.txd_tail
+ state
.txd_num
+
875 sizeof(u32_t
)) % ATL2_TXD_BUFSIZE
;
877 for (i
= 0; i
< m
->DL_COUNT
; i
+= batch
) {
878 /* Copy in the next batch. */
879 batch
= MIN(m
->DL_COUNT
- i
, NR_IOREQS
);
881 r
= sys_safecopyfrom(m
->m_source
, m
->DL_GRANT
, off
,
882 (vir_bytes
) iovec
, batch
* sizeof(iovec
[0]));
884 panic("vector copy failed: %d", r
);
886 /* Copy in each element in the batch. */
887 for (j
= 0, iovp
= iovec
; j
< batch
; j
++, iovp
++) {
888 size
= iovp
->iov_size
;
893 if (size
> ATL2_TXD_BUFSIZE
- pos
) {
894 skip
= ATL2_TXD_BUFSIZE
- pos
;
895 r
= sys_safecopyfrom(m
->m_source
,
897 (vir_bytes
) (state
.txd_base
+ pos
),
900 panic("safe copy failed: %d", r
);
904 r
= sys_safecopyfrom(m
->m_source
, iovp
->iov_grant
,
905 skip
, (vir_bytes
) (state
.txd_base
+ pos
),
908 panic("safe copy failed: %d", r
);
910 pos
= (pos
+ size
- skip
) % ATL2_TXD_BUFSIZE
;
915 off
+= batch
* sizeof(iovec
[0]);
918 assert(count
<= ETH_MAX_PACK_SIZE_TAGGED
);
920 /* Write the length to the DWORD right before the packet. */
921 sizep
= state
.txd_base
+
922 (state
.txd_tail
+ state
.txd_num
) % ATL2_TXD_BUFSIZE
;
923 * (u32_t
*) sizep
= count
;
925 /* Update the TxD head. */
926 state
.txd_num
+= sizeof(u32_t
) + ATL2_ALIGN_32(count
);
927 pos
= ATL2_ALIGN_32(pos
) % ATL2_TXD_BUFSIZE
;
929 (state
.txd_tail
+ state
.txd_num
) % ATL2_TXD_BUFSIZE
);
931 /* Initialize and update the TxS head. */
932 state
.txs_base
[(state
.txs_tail
+ state
.txs_num
) % ATL2_TXS_COUNT
] = 0;
935 /* Tell the device about our new position. */
938 ATL2_WRITE_U32(ATL2_TXD_IDX_REG
, pos
/ sizeof(u32_t
));
940 /* We have now successfully set up the transmission of a packet. */
941 state
.flags
&= ~ATL2_FLAG_WRITE_PEND
;
942 state
.flags
|= ATL2_FLAG_PACK_SENT
;
944 /* If called from the interrupt handler, the caller will reply. */
951 /* We cannot transmit the packet at this time. If we were not already
952 * trying to resume transmission, save the write request for later,
953 * and tell Inet that the request has been suspended.
958 state
.flags
|= ATL2_FLAG_WRITE_PEND
;
959 state
.write_msg
= *m
;
964 /*===========================================================================*
966 *===========================================================================*/
967 static void atl2_intr(const message
*UNUSED(m
))
969 /* Interrupt received.
972 int r
, try_write
, try_read
;
974 /* Clear and disable interrupts. */
975 val
= ATL2_READ_U32(ATL2_ISR_REG
);
977 ATL2_WRITE_U32(ATL2_ISR_REG
, val
| ATL2_ISR_DISABLE
);
979 ATL2_DEBUG(("ATL2: interrupt (0x%08lx)\n", val
));
981 /* If an error occurred, reset the card. */
982 if (val
& (ATL2_ISR_DMAR_TIMEOUT
| ATL2_ISR_DMAW_TIMEOUT
|
983 ATL2_ISR_PHY_LINKDOWN
)) {
987 try_write
= try_read
= FALSE
;
989 /* Process sent data, and possibly send pending data. */
990 if (val
& ATL2_ISR_TX_EVENT
) {
991 if (atl2_tx_advance())
992 try_write
= (state
.flags
& ATL2_FLAG_WRITE_PEND
);
995 /* Receive new data, and possible satisfy a pending receive request. */
996 if (val
& ATL2_ISR_RX_EVENT
) {
997 if (!(state
.flags
& ATL2_FLAG_RX_AVAIL
)) {
998 atl2_rx_advance(FALSE
/*next*/);
1000 try_read
= (state
.flags
& ATL2_FLAG_READ_PEND
);
1004 /* Reenable interrupts. */
1005 ATL2_WRITE_U32(ATL2_ISR_REG
, 0);
1007 if ((r
= sys_irqenable(&state
.hook_id
)) != OK
)
1008 panic("unable to enable IRQ: %d", r
);
1010 /* Attempt to satisfy pending write and read requests. */
1012 atl2_writev(&state
.write_msg
, TRUE
/*from_int*/);
1014 atl2_readv(&state
.read_msg
, TRUE
/*from_int*/);
1015 if (state
.flags
& (ATL2_FLAG_PACK_SENT
| ATL2_FLAG_PACK_RCVD
))
1019 /*===========================================================================*
1021 *===========================================================================*/
1022 static void atl2_conf(message
*m
)
1024 /* Configure the mode of the card.
1029 state
.mode
= m
->DL_MODE
;
1033 addr
.ea_addr
[0] = state
.hwaddr
[1] >> 8;
1034 addr
.ea_addr
[1] = state
.hwaddr
[1] & 0xff;
1035 addr
.ea_addr
[2] = state
.hwaddr
[0] >> 24;
1036 addr
.ea_addr
[3] = (state
.hwaddr
[0] >> 16) & 0xff;
1037 addr
.ea_addr
[4] = (state
.hwaddr
[0] >> 8) & 0xff;
1038 addr
.ea_addr
[5] = state
.hwaddr
[0] & 0xff;
1040 memcpy(m
->DL_HWADDR
, &addr
, sizeof(addr
));
1042 m
->m_type
= DL_CONF_REPLY
;
1045 if ((r
= send(m
->m_source
, m
)) != OK
)
1046 printf("ATL2: unable to send reply (%d)\n", r
);
1049 /*===========================================================================*
1051 *===========================================================================*/
1052 static void atl2_getstat(message
*m
)
1054 /* Copy out statistics.
1058 sys_safecopyto(m
->m_source
, m
->DL_GRANT
, 0,
1059 (vir_bytes
) &state
.stat
, sizeof(state
.stat
));
1061 m
->m_type
= DL_STAT_REPLY
;
1063 if ((r
= send(m
->m_source
, m
)) != OK
)
1064 printf("ATL2: unable to send reply (%d)\n", r
);
1067 /*===========================================================================*
1069 *===========================================================================*/
1070 static void atl2_dump_link(void)
1072 /* Dump link status.
1077 /* The link status bit is latched. Read the status register twice. */
1078 atl2_read_mdio(ATL2_MII_BMSR
, &val
);
1079 if (!atl2_read_mdio(ATL2_MII_BMSR
, &val
)) return;
1081 link_up
= val
& ATL2_MII_BMSR_LSTATUS
;
1082 printf("link status: %4s\t", link_up
? "up" : "down");
1084 if (!link_up
) return;
1086 if (!atl2_read_mdio(ATL2_MII_PSSR
, &val
)) return;
1088 if (!(val
& ATL2_MII_PSSR_RESOLVED
)) {
1089 printf("(not resolved)\n");
1094 switch (val
& ATL2_MII_PSSR_SPEED
) {
1095 case ATL2_MII_PSSR_10
: printf("(10Mbps "); break;
1096 case ATL2_MII_PSSR_100
: printf("(100Mbps "); break;
1097 case ATL2_MII_PSSR_1000
: printf("(1000Mbps "); break;
1098 default: printf("(unknown, ");
1101 printf("%s duplex)", (val
& ATL2_MII_PSSR_DUPLEX
) ? "full" : "half");
1104 /*===========================================================================*
1106 *===========================================================================*/
1107 static void atl2_dump(void)
1113 printf("Attansic L2 statistics:\n");
1115 printf("recvErr: %8ld\t", state
.stat
.ets_recvErr
);
1116 printf("sendErr: %8ld\t", state
.stat
.ets_sendErr
);
1117 printf("OVW: %8ld\n", state
.stat
.ets_OVW
);
1119 printf("CRCerr: %8ld\t", state
.stat
.ets_CRCerr
);
1120 printf("frameAll: %8ld\t", state
.stat
.ets_frameAll
);
1121 printf("missedP: %8ld\n", state
.stat
.ets_missedP
);
1123 printf("packetR: %8ld\t", state
.stat
.ets_packetR
);
1124 printf("packetT: %8ld\t", state
.stat
.ets_packetT
);
1125 printf("transDef: %8ld\n", state
.stat
.ets_transDef
);
1127 printf("collision: %8ld\t", state
.stat
.ets_collision
);
1128 printf("transAb: %8ld\t", state
.stat
.ets_transAb
);
1129 printf("carrSense: %8ld\n", state
.stat
.ets_carrSense
);
1131 printf("fifoUnder: %8ld\t", state
.stat
.ets_fifoUnder
);
1132 printf("fifoOver: %8ld\t", state
.stat
.ets_fifoOver
);
1133 printf("CDheartbeat: %8ld\n", state
.stat
.ets_CDheartbeat
);
1135 printf("OWC: %8ld\t", state
.stat
.ets_OWC
);
1136 printf("TxD tail: %8d\t", state
.txd_tail
);
1137 printf("TxD count: %8d\n", state
.txd_num
);
1139 printf("RxD tail: %8d\t", state
.rxd_tail
);
1140 printf("TxS tail: %8d\t", state
.txs_tail
);
1141 printf("TxS count: %8d\n", state
.txs_num
);
1143 printf("flags: 0x%04x\t", state
.flags
);
1148 /*===========================================================================*
1149 * sef_cb_init_fresh *
1150 *===========================================================================*/
1151 static int sef_cb_init_fresh(int UNUSED(type
), sef_init_info_t
*UNUSED(info
))
1153 /* Initialize the atl2 driver.
1161 /* How many matching devices should we skip? */
1163 (void) env_parse("instance", "d", 0, &v
, 0, 255);
1166 /* Try to find a recognized device. */
1167 devind
= atl2_probe(instance
);
1170 panic("no matching device found");
1172 /* Initialize the device. */
1175 /* Announce we are up! */
1176 netdriver_announce();
1179 /* Register debug dump function key. */
1181 bit_set(sfkeys
, 11);
1182 if ((r
= fkey_map(&fkeys
, &sfkeys
)) != OK
)
1183 printf("ATL2: warning, could not map Shift+F11 key (%d)\n", r
);
1189 /*===========================================================================*
1190 * sef_cb_signal_handler *
1191 *===========================================================================*/
1192 static void sef_cb_signal_handler(int signo
)
1194 /* In case of a termination signal, shut down this driver.
1195 * Stop the device, and deallocate resources as proof of concept.
1199 /* Only check for termination signal, ignore anything else. */
1200 if (signo
!= SIGTERM
) return;
1204 if ((r
= sys_irqrmpolicy(&state
.hook_id
)) != OK
)
1205 panic("unable to deregister IRQ: %d", r
);
1207 free_contig(state
.txd_base
, ATL2_TXD_BUFSIZE
);
1208 free_contig(state
.txs_base
, ATL2_TXS_COUNT
* sizeof(u32_t
));
1209 free_contig(state
.rxd_base_u
,
1210 state
.rxd_align
+ ATL2_RXD_COUNT
* ATL2_RXD_SIZE
);
1212 vm_unmap_phys(SELF
, (void *) state
.base
, state
.size
);
1214 /* We cannot free the PCI device at this time. */
1219 /*===========================================================================*
1220 * sef_local_startup *
1221 *===========================================================================*/
1222 static void sef_local_startup(void)
1224 /* Register init callbacks. */
1225 sef_setcb_init_fresh(sef_cb_init_fresh
);
1226 sef_setcb_init_lu(sef_cb_init_fresh
);
1227 sef_setcb_init_restart(sef_cb_init_fresh
);
1229 /* Register live update callbacks. */
1230 sef_setcb_lu_prepare(sef_cb_lu_prepare_always_ready
);
1231 sef_setcb_lu_state_isvalid(sef_cb_lu_state_isvalid_workfree
);
1233 /* Register signal callbacks. */
1234 sef_setcb_signal_handler(sef_cb_signal_handler
);
1236 /* Let SEF perform startup. */
1240 /*===========================================================================*
1242 *===========================================================================*/
1243 int main(int argc
, char **argv
)
1251 /* Initialize SEF. */
1252 env_setargs(argc
, argv
);
1253 sef_local_startup();
1256 if ((r
= netdriver_receive(ANY
, &m
, &ipc_status
)) != OK
)
1257 panic("netdriver_receive failed: %d", r
);
1259 if (is_ipc_notify(ipc_status
)) {
1260 switch (m
.m_source
) {
1261 case HARDWARE
: /* interrupt */
1266 case TTY_PROC_NR
: /* function key */
1272 printf("ATL2: illegal notify from %d\n",
1279 /* Process requests from Inet. */
1281 case DL_CONF
: atl2_conf(&m
); break;
1282 case DL_GETSTAT_S
: atl2_getstat(&m
); break;
1283 case DL_WRITEV_S
: atl2_writev(&m
, FALSE
); break;
1284 case DL_READV_S
: atl2_readv(&m
, FALSE
); break;
1286 printf("ATL2: illegal message %d from %d\n",
1287 m
.m_type
, m
.m_source
);