1 /* $OpenBSD: rtl81x9.c,v 1.42 2005/07/02 23:20:05 brad Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * RealTek 8129/8139 PCI NIC driver
37 *
38 * Supports several extremely cheap PCI 10/100 adapters based on
39 * the RealTek chipset. Datasheets can be obtained from
40 * www.realtek.com.tw.
41 *
42 * Written by Bill Paul <wpaul@ctr.columbia.edu>
43 * Electrical Engineering Department
44 * Columbia University, New York City
45 */
46
47 /*
48 * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
49 * probably the worst PCI ethernet controller ever made, with the possible
50 * exception of the FEAST chip made by SMC. The 8139 supports bus-master
51 * DMA, but it has a terrible interface that nullifies any performance
52 * gains that bus-master DMA usually offers.
53 *
54 * For transmission, the chip offers a series of four TX descriptor
55 * registers. Each transmit frame must be in a contiguous buffer, aligned
56 * on a longword (32-bit) boundary. This means we almost always have to
57 * do mbuf copies in order to transmit a frame, except in the unlikely
58 * case where a) the packet fits into a single mbuf, and b) the packet
59 * is 32-bit aligned within the mbuf's data area. The presence of only
60 * four descriptor registers means that we can never have more than four
61 * packets queued for transmission at any one time.
62 *
63 * Reception is not much better. The driver has to allocate a single large
64 * buffer area (up to 64K in size) into which the chip will DMA received
65 * frames. Because we don't know where within this region received packets
66 * will begin or end, we have no choice but to copy data from the buffer
67 * area into mbufs in order to pass the packets up to the higher protocol
68 * levels.
69 *
70 * It's impossible given this rotten design to really achieve decent
71 * performance at 100Mbps, unless you happen to have a 400MHz PII or
72 * some equally overmuscled CPU to drive it.
73 *
74 * On the bright side, the 8139 does have a built-in PHY, although
75 * rather than using an MDIO serial interface like most other NICs, the
76 * PHY registers are directly accessible through the 8139's register
77 * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
78 * filter.
79 *
80 * The 8129 chip is an older version of the 8139 that uses an external PHY
81 * chip. The 8129 has a serial MDIO interface for accessing the MII where
82 * the 8139 lets you directly access the on-board PHY registers. We need
83 * to select which interface to use depending on the chip type.
84 */
85
86 #include "bpfilter.h"
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/sockio.h>
91 #include <sys/mbuf.h>
92 #include <sys/malloc.h>
93 #include <sys/kernel.h>
94 #include <sys/socket.h>
95 #include <sys/device.h>
96 #include <sys/timeout.h>
97
98 #include <net/if.h>
99 #include <net/if_dl.h>
100 #include <net/if_types.h>
101
102 #ifdef INET
103 #include <netinet/in.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip.h>
107 #include <netinet/if_ether.h>
108 #endif
109
110 #include <net/if_media.h>
111
112 #if NBPFILTER > 0
113 #include <net/bpf.h>
114 #endif
115
116 #include <machine/bus.h>
117
118 #include <dev/mii/mii.h>
119 #include <dev/mii/miivar.h>
120 #include <dev/pci/pcireg.h>
121 #include <dev/pci/pcivar.h>
122 #include <dev/pci/pcidevs.h>
123
124 #include <dev/ic/rtl81x9reg.h>
125
126 /*
127 * Various supported PHY vendors/types and their names. Note that
128 * this driver will work with pretty much any MII-compliant PHY,
129 * so failure to positively identify the chip is not a fatal error.
130 */
131
132 void rl_tick(void *);
133 void rl_shutdown(void *);
134 void rl_powerhook(int, void *);
135
136 int rl_encap(struct rl_softc *, struct mbuf * );
137
138 void rl_rxeof(struct rl_softc *);
139 void rl_txeof(struct rl_softc *);
140 void rl_start(struct ifnet *);
141 int rl_ioctl(struct ifnet *, u_long, caddr_t);
142 void rl_init(void *);
143 void rl_stop(struct rl_softc *);
144 void rl_watchdog(struct ifnet *);
145 int rl_ifmedia_upd(struct ifnet *);
146 void rl_ifmedia_sts(struct ifnet *, struct ifmediareq *);
147
148 void rl_eeprom_getword(struct rl_softc *, int, int, u_int16_t *);
149 void rl_eeprom_putbyte(struct rl_softc *, int, int);
150 void rl_read_eeprom(struct rl_softc *, caddr_t, int, int, int, int);
151
152 void rl_mii_sync(struct rl_softc *);
153 void rl_mii_send(struct rl_softc *, u_int32_t, int);
154 int rl_mii_readreg(struct rl_softc *, struct rl_mii_frame *);
155 int rl_mii_writereg(struct rl_softc *, struct rl_mii_frame *);
156
157 int rl_miibus_readreg(struct device *, int, int);
158 void rl_miibus_writereg(struct device *, int, int, int);
159 void rl_miibus_statchg(struct device *);
160
161 void rl_setmulti(struct rl_softc *);
162 void rl_reset(struct rl_softc *);
163 int rl_list_tx_init(struct rl_softc *);
164
165 #define EE_SET(x) \
166 CSR_WRITE_1(sc, RL_EECMD, \
167 CSR_READ_1(sc, RL_EECMD) | x)
168
169 #define EE_CLR(x) \
170 CSR_WRITE_1(sc, RL_EECMD, \
171 CSR_READ_1(sc, RL_EECMD) & ~x)
172
173 /*
174 * Send a read command and address to the EEPROM, check for ACK.
175 */
rl_eeprom_putbyte(sc,addr,addr_len)176 void rl_eeprom_putbyte(sc, addr, addr_len)
177 struct rl_softc *sc;
178 int addr, addr_len;
179 {
180 register int d, i;
181
182 d = (RL_EECMD_READ << addr_len) | addr;
183
184 /*
185 * Feed in each bit and strobe the clock.
186 */
187 for (i = RL_EECMD_LEN + addr_len; i; i--) {
188 if (d & (1 << (i - 1)))
189 EE_SET(RL_EE_DATAIN);
190 else
191 EE_CLR(RL_EE_DATAIN);
192
193 DELAY(100);
194 EE_SET(RL_EE_CLK);
195 DELAY(150);
196 EE_CLR(RL_EE_CLK);
197 DELAY(100);
198 }
199 }
200
201 /*
202 * Read a word of data stored in the EEPROM at address 'addr.'
203 */
rl_eeprom_getword(sc,addr,addr_len,dest)204 void rl_eeprom_getword(sc, addr, addr_len, dest)
205 struct rl_softc *sc;
206 int addr, addr_len;
207 u_int16_t *dest;
208 {
209 register int i;
210 u_int16_t word = 0;
211
212 /* Enter EEPROM access mode. */
213 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
214
215 /*
216 * Send address of word we want to read.
217 */
218 rl_eeprom_putbyte(sc, addr, addr_len);
219
220 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
221
222 /*
223 * Start reading bits from EEPROM.
224 */
225 for (i = 16; i > 0; i--) {
226 EE_SET(RL_EE_CLK);
227 DELAY(100);
228 if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
229 word |= 1 << (i - 1);
230 EE_CLR(RL_EE_CLK);
231 DELAY(100);
232 }
233
234 /* Turn off EEPROM access mode. */
235 CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
236
237 *dest = word;
238 }
239
240 /*
241 * Read a sequence of words from the EEPROM.
242 */
rl_read_eeprom(sc,dest,off,addr_len,cnt,swap)243 void rl_read_eeprom(sc, dest, off, addr_len, cnt, swap)
244 struct rl_softc *sc;
245 caddr_t dest;
246 int off;
247 int addr_len;
248 int cnt;
249 int swap;
250 {
251 int i;
252 u_int16_t word = 0, *ptr;
253
254 for (i = 0; i < cnt; i++) {
255 rl_eeprom_getword(sc, off + i, addr_len, &word);
256 ptr = (u_int16_t *)(dest + (i * 2));
257 if (swap)
258 *ptr = letoh16(word);
259 else
260 *ptr = word;
261 }
262 }
263
264 /*
265 * MII access routines are provided for the 8129, which
266 * doesn't have a built-in PHY. For the 8139, we fake things
267 * up by diverting rl_phy_readreg()/rl_phy_writereg() to the
268 * direct access PHY registers.
269 */
270 #define MII_SET(x) \
271 CSR_WRITE_1(sc, RL_MII, \
272 CSR_READ_1(sc, RL_MII) | x)
273
274 #define MII_CLR(x) \
275 CSR_WRITE_1(sc, RL_MII, \
276 CSR_READ_1(sc, RL_MII) & ~x)
277
278 /*
279 * Sync the PHYs by setting data bit and strobing the clock 32 times.
280 */
rl_mii_sync(sc)281 void rl_mii_sync(sc)
282 struct rl_softc *sc;
283 {
284 register int i;
285
286 MII_SET(RL_MII_DIR|RL_MII_DATAOUT);
287
288 for (i = 0; i < 32; i++) {
289 MII_SET(RL_MII_CLK);
290 DELAY(1);
291 MII_CLR(RL_MII_CLK);
292 DELAY(1);
293 }
294 }
295
296 /*
297 * Clock a series of bits through the MII.
298 */
rl_mii_send(sc,bits,cnt)299 void rl_mii_send(sc, bits, cnt)
300 struct rl_softc *sc;
301 u_int32_t bits;
302 int cnt;
303 {
304 int i;
305
306 MII_CLR(RL_MII_CLK);
307
308 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
309 if (bits & i)
310 MII_SET(RL_MII_DATAOUT);
311 else
312 MII_CLR(RL_MII_DATAOUT);
313 DELAY(1);
314 MII_CLR(RL_MII_CLK);
315 DELAY(1);
316 MII_SET(RL_MII_CLK);
317 }
318 }
319
320 /*
321 * Read an PHY register through the MII.
322 */
rl_mii_readreg(sc,frame)323 int rl_mii_readreg(sc, frame)
324 struct rl_softc *sc;
325 struct rl_mii_frame *frame;
326 {
327 int i, ack, s;
328
329 s = splimp();
330
331 /*
332 * Set up frame for RX.
333 */
334 frame->mii_stdelim = RL_MII_STARTDELIM;
335 frame->mii_opcode = RL_MII_READOP;
336 frame->mii_turnaround = 0;
337 frame->mii_data = 0;
338
339 CSR_WRITE_2(sc, RL_MII, 0);
340
341 /*
342 * Turn on data xmit.
343 */
344 MII_SET(RL_MII_DIR);
345
346 rl_mii_sync(sc);
347
348 /*
349 * Send command/address info.
350 */
351 rl_mii_send(sc, frame->mii_stdelim, 2);
352 rl_mii_send(sc, frame->mii_opcode, 2);
353 rl_mii_send(sc, frame->mii_phyaddr, 5);
354 rl_mii_send(sc, frame->mii_regaddr, 5);
355
356 /* Idle bit */
357 MII_CLR((RL_MII_CLK|RL_MII_DATAOUT));
358 DELAY(1);
359 MII_SET(RL_MII_CLK);
360 DELAY(1);
361
362 /* Turn off xmit. */
363 MII_CLR(RL_MII_DIR);
364
365 /* Check for ack */
366 MII_CLR(RL_MII_CLK);
367 DELAY(1);
368 ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN;
369 MII_SET(RL_MII_CLK);
370 DELAY(1);
371
372 /*
373 * Now try reading data bits. If the ack failed, we still
374 * need to clock through 16 cycles to keep the PHY(s) in sync.
375 */
376 if (ack) {
377 for(i = 0; i < 16; i++) {
378 MII_CLR(RL_MII_CLK);
379 DELAY(1);
380 MII_SET(RL_MII_CLK);
381 DELAY(1);
382 }
383 goto fail;
384 }
385
386 for (i = 0x8000; i; i >>= 1) {
387 MII_CLR(RL_MII_CLK);
388 DELAY(1);
389 if (!ack) {
390 if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN)
391 frame->mii_data |= i;
392 DELAY(1);
393 }
394 MII_SET(RL_MII_CLK);
395 DELAY(1);
396 }
397
398 fail:
399
400 MII_CLR(RL_MII_CLK);
401 DELAY(1);
402 MII_SET(RL_MII_CLK);
403 DELAY(1);
404
405 splx(s);
406
407 if (ack)
408 return(1);
409 return(0);
410 }
411
412 /*
413 * Write to a PHY register through the MII.
414 */
rl_mii_writereg(sc,frame)415 int rl_mii_writereg(sc, frame)
416 struct rl_softc *sc;
417 struct rl_mii_frame *frame;
418 {
419 int s;
420
421 s = splimp();
422 /*
423 * Set up frame for TX.
424 */
425
426 frame->mii_stdelim = RL_MII_STARTDELIM;
427 frame->mii_opcode = RL_MII_WRITEOP;
428 frame->mii_turnaround = RL_MII_TURNAROUND;
429
430 /*
431 * Turn on data output.
432 */
433 MII_SET(RL_MII_DIR);
434
435 rl_mii_sync(sc);
436
437 rl_mii_send(sc, frame->mii_stdelim, 2);
438 rl_mii_send(sc, frame->mii_opcode, 2);
439 rl_mii_send(sc, frame->mii_phyaddr, 5);
440 rl_mii_send(sc, frame->mii_regaddr, 5);
441 rl_mii_send(sc, frame->mii_turnaround, 2);
442 rl_mii_send(sc, frame->mii_data, 16);
443
444 /* Idle bit. */
445 MII_SET(RL_MII_CLK);
446 DELAY(1);
447 MII_CLR(RL_MII_CLK);
448 DELAY(1);
449
450 /*
451 * Turn off xmit.
452 */
453 MII_CLR(RL_MII_DIR);
454
455 splx(s);
456
457 return(0);
458 }
459
460 /*
461 * Program the 64-bit multicast hash filter.
462 */
rl_setmulti(sc)463 void rl_setmulti(sc)
464 struct rl_softc *sc;
465 {
466 struct ifnet *ifp;
467 int h = 0;
468 u_int32_t hashes[2] = { 0, 0 };
469 struct arpcom *ac = &sc->sc_arpcom;
470 struct ether_multi *enm;
471 struct ether_multistep step;
472 u_int32_t rxfilt;
473 int mcnt = 0;
474
475 ifp = &sc->sc_arpcom.ac_if;
476
477 rxfilt = CSR_READ_4(sc, RL_RXCFG);
478
479 allmulti:
480 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
481 rxfilt |= RL_RXCFG_RX_MULTI;
482 CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
483 CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
484 CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
485 return;
486 }
487
488 /* first, zot all the existing hash bits */
489 CSR_WRITE_4(sc, RL_MAR0, 0);
490 CSR_WRITE_4(sc, RL_MAR4, 0);
491
492 /* now program new ones */
493 ETHER_FIRST_MULTI(step, ac, enm);
494 while (enm != NULL) {
495 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
496 ifp->if_flags |= IFF_ALLMULTI;
497 goto allmulti;
498 }
499 mcnt++;
500 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
501 if (h < 32)
502 hashes[0] |= (1 << h);
503 else
504 hashes[1] |= (1 << (h - 32));
505 mcnt++;
506 ETHER_NEXT_MULTI(step, enm);
507 }
508
509 if (mcnt)
510 rxfilt |= RL_RXCFG_RX_MULTI;
511 else
512 rxfilt &= ~RL_RXCFG_RX_MULTI;
513
514 CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
515 CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
516 CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
517 }
518
519 void
rl_reset(sc)520 rl_reset(sc)
521 struct rl_softc *sc;
522 {
523 register int i;
524
525 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
526
527 for (i = 0; i < RL_TIMEOUT; i++) {
528 DELAY(10);
529 if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
530 break;
531 }
532 if (i == RL_TIMEOUT)
533 printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
534
535 }
536
537 /*
538 * Initialize the transmit descriptors.
539 */
540 int
rl_list_tx_init(sc)541 rl_list_tx_init(sc)
542 struct rl_softc *sc;
543 {
544 struct rl_chain_data *cd;
545 int i;
546
547 cd = &sc->rl_cdata;
548 for (i = 0; i < RL_TX_LIST_CNT; i++) {
549 cd->rl_tx_chain[i] = NULL;
550 CSR_WRITE_4(sc,
551 RL_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
552 }
553
554 sc->rl_cdata.cur_tx = 0;
555 sc->rl_cdata.last_tx = 0;
556
557 return(0);
558 }
559
560 /*
561 * A frame has been uploaded: pass the resulting mbuf chain up to
562 * the higher level protocols.
563 *
564 * You know there's something wrong with a PCI bus-master chip design
565 * when you have to use m_devget().
566 *
567 * The receive operation is badly documented in the datasheet, so I'll
568 * attempt to document it here. The driver provides a buffer area and
569 * places its base address in the RX buffer start address register.
570 * The chip then begins copying frames into the RX buffer. Each frame
571 * is preceded by a 32-bit RX status word which specifies the length
572 * of the frame and certain other status bits. Each frame (starting with
573 * the status word) is also 32-bit aligned. The frame length is in the
574 * first 16 bits of the status word; the lower 15 bits correspond with
575 * the 'rx status register' mentioned in the datasheet.
576 *
577 * Note: to make the Alpha happy, the frame payload needs to be aligned
578 * on a 32-bit boundary. To achieve this, we cheat a bit by copying from
579 * the ring buffer starting at an address two bytes before the actual
580 * data location. We can then shave off the first two bytes using m_adj().
581 * The reason we do this is because m_devget() doesn't let us specify an
582 * offset into the mbuf storage space, so we have to artificially create
583 * one. The ring is allocated in such a way that there are a few unused
584 * bytes of space preceding it so that it will be safe for us to do the
585 * 2-byte backstep even if reading from the ring at offset 0.
586 */
587 void
rl_rxeof(sc)588 rl_rxeof(sc)
589 struct rl_softc *sc;
590 {
591 struct mbuf *m;
592 struct ifnet *ifp;
593 int total_len;
594 u_int32_t rxstat;
595 caddr_t rxbufpos;
596 int wrap = 0;
597 u_int16_t cur_rx;
598 u_int16_t limit;
599 u_int16_t rx_bytes = 0, max_bytes;
600
601 ifp = &sc->sc_arpcom.ac_if;
602
603 cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN;
604
605 /* Do not try to read past this point. */
606 limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN;
607
608 if (limit < cur_rx)
609 max_bytes = (RL_RXBUFLEN - cur_rx) + limit;
610 else
611 max_bytes = limit - cur_rx;
612
613 while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) {
614 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dmamap,
615 0, sc->sc_rx_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
616 rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx;
617 rxstat = *(u_int32_t *)rxbufpos;
618
619 /*
620 * Here's a totally undocumented fact for you. When the
621 * RealTek chip is in the process of copying a packet into
622 * RAM for you, the length will be 0xfff0. If you spot a
623 * packet header with this value, you need to stop. The
624 * datasheet makes absolutely no mention of this and
625 * RealTek should be shot for this.
626 */
627 rxstat = htole32(rxstat);
628 total_len = rxstat >> 16;
629 if (total_len == RL_RXSTAT_UNFINISHED) {
630 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dmamap,
631 0, sc->sc_rx_dmamap->dm_mapsize,
632 BUS_DMASYNC_PREREAD);
633 break;
634 }
635
636 if (!(rxstat & RL_RXSTAT_RXOK)) {
637 ifp->if_ierrors++;
638 rl_init(sc);
639 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dmamap,
640 0, sc->sc_rx_dmamap->dm_mapsize,
641 BUS_DMASYNC_PREREAD);
642 return;
643 }
644
645 /* No errors; receive the packet. */
646 rx_bytes += total_len + 4;
647
648 /*
649 * XXX The RealTek chip includes the CRC with every
650 * received frame, and there's no way to turn this
651 * behavior off (at least, I can't find anything in
652 * the manual that explains how to do it) so we have
653 * to trim off the CRC manually.
654 */
655 total_len -= ETHER_CRC_LEN;
656
657 /*
658 * Avoid trying to read more bytes than we know
659 * the chip has prepared for us.
660 */
661 if (rx_bytes > max_bytes) {
662 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dmamap,
663 0, sc->sc_rx_dmamap->dm_mapsize,
664 BUS_DMASYNC_PREREAD);
665 break;
666 }
667
668 rxbufpos = sc->rl_cdata.rl_rx_buf +
669 ((cur_rx + sizeof(u_int32_t)) % RL_RXBUFLEN);
670
671 if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN))
672 rxbufpos = sc->rl_cdata.rl_rx_buf;
673
674 wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos;
675
676 if (total_len > wrap) {
677 m = m_devget(rxbufpos - ETHER_ALIGN,
678 wrap + ETHER_ALIGN, 0, ifp, NULL);
679 if (m == NULL)
680 ifp->if_ierrors++;
681 else {
682 m_copyback(m, wrap + ETHER_ALIGN,
683 total_len - wrap, sc->rl_cdata.rl_rx_buf);
684 m = m_pullup(m, sizeof(struct ip) +ETHER_ALIGN);
685 if (m == NULL)
686 ifp->if_ierrors++;
687 else
688 m_adj(m, ETHER_ALIGN);
689 }
690 cur_rx = (total_len - wrap + ETHER_CRC_LEN);
691 } else {
692 m = m_devget(rxbufpos - ETHER_ALIGN,
693 total_len + ETHER_ALIGN, 0, ifp, NULL);
694 if (m == NULL)
695 ifp->if_ierrors++;
696 else
697 m_adj(m, ETHER_ALIGN);
698 cur_rx += total_len + 4 + ETHER_CRC_LEN;
699 }
700
701 /*
702 * Round up to 32-bit boundary.
703 */
704 cur_rx = (cur_rx + 3) & ~3;
705 CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
706
707 if (m == NULL) {
708 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dmamap,
709 0, sc->sc_rx_dmamap->dm_mapsize,
710 BUS_DMASYNC_PREREAD);
711 continue;
712 }
713
714 ifp->if_ipackets++;
715
716 #if NBPFILTER > 0
717 /*
718 * Handle BPF listeners. Let the BPF user see the packet.
719 */
720 if (ifp->if_bpf)
721 bpf_mtap(ifp->if_bpf, m);
722 #endif
723 ether_input_mbuf(ifp, m);
724
725 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dmamap,
726 0, sc->sc_rx_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
727 }
728 }
729
730 /*
731 * A frame was downloaded to the chip. It's safe for us to clean up
732 * the list buffers.
733 */
rl_txeof(sc)734 void rl_txeof(sc)
735 struct rl_softc *sc;
736 {
737 struct ifnet *ifp;
738 u_int32_t txstat;
739
740 ifp = &sc->sc_arpcom.ac_if;
741
742 /*
743 * Go through our tx list and free mbufs for those
744 * frames that have been uploaded.
745 */
746 do {
747 if (RL_LAST_TXMBUF(sc) == NULL)
748 break;
749 txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc));
750 if (!(txstat & (RL_TXSTAT_TX_OK|
751 RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT)))
752 break;
753
754 ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24;
755
756 bus_dmamap_sync(sc->sc_dmat, RL_LAST_TXMAP(sc),
757 0, RL_LAST_TXMAP(sc)->dm_mapsize,
758 BUS_DMASYNC_POSTWRITE);
759 bus_dmamap_unload(sc->sc_dmat, RL_LAST_TXMAP(sc));
760 m_freem(RL_LAST_TXMBUF(sc));
761 RL_LAST_TXMBUF(sc) = NULL;
762 /*
763 * If there was a transmit underrun, bump the TX threshold.
764 * Make sure not to overflow the 63 * 32byte we can address
765 * with the 6 available bit.
766 */
767 if ((txstat & RL_TXSTAT_TX_UNDERRUN) &&
768 (sc->rl_txthresh < 2016))
769 sc->rl_txthresh += 32;
770 if (txstat & RL_TXSTAT_TX_OK)
771 ifp->if_opackets++;
772 else {
773 int oldthresh;
774
775 ifp->if_oerrors++;
776 if ((txstat & RL_TXSTAT_TXABRT) ||
777 (txstat & RL_TXSTAT_OUTOFWIN))
778 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
779 oldthresh = sc->rl_txthresh;
780 /* error recovery */
781 rl_reset(sc);
782 rl_init(sc);
783 /* restore original threshold */
784 sc->rl_txthresh = oldthresh;
785 return;
786 }
787 RL_INC(sc->rl_cdata.last_tx);
788 ifp->if_flags &= ~IFF_OACTIVE;
789 } while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx);
790
791 if (RL_LAST_TXMBUF(sc) == NULL)
792 ifp->if_timer = 0;
793 else if (ifp->if_timer == 0)
794 ifp->if_timer = 5;
795 }
796
rl_intr(arg)797 int rl_intr(arg)
798 void *arg;
799 {
800 struct rl_softc *sc;
801 struct ifnet *ifp;
802 int claimed = 0;
803 u_int16_t status;
804
805 sc = arg;
806 ifp = &sc->sc_arpcom.ac_if;
807
808 /* Disable interrupts. */
809 CSR_WRITE_2(sc, RL_IMR, 0x0000);
810
811 for (;;) {
812 status = CSR_READ_2(sc, RL_ISR);
813 /* If the card has gone away, the read returns 0xffff. */
814 if (status == 0xffff)
815 break;
816 if (status != 0)
817 CSR_WRITE_2(sc, RL_ISR, status);
818 if ((status & RL_INTRS) == 0)
819 break;
820 if (status & RL_ISR_RX_OK)
821 rl_rxeof(sc);
822 if (status & RL_ISR_RX_ERR)
823 rl_rxeof(sc);
824 if ((status & RL_ISR_TX_OK) || (status & RL_ISR_TX_ERR))
825 rl_txeof(sc);
826 if (status & RL_ISR_SYSTEM_ERR) {
827 rl_reset(sc);
828 rl_init(sc);
829 }
830 claimed = 1;
831 }
832
833 /* Re-enable interrupts. */
834 CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
835
836 if (!IFQ_IS_EMPTY(&ifp->if_snd))
837 rl_start(ifp);
838
839 return (claimed);
840 }
841
842 /*
843 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
844 * pointers to the fragment pointers.
845 */
rl_encap(sc,m_head)846 int rl_encap(sc, m_head)
847 struct rl_softc *sc;
848 struct mbuf *m_head;
849 {
850 struct mbuf *m_new;
851
852 /*
853 * The RealTek is brain damaged and wants longword-aligned
854 * TX buffers, plus we can only have one fragment buffer
855 * per packet. We have to copy pretty much all the time.
856 */
857
858 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
859 if (m_new == NULL)
860 return(1);
861 if (m_head->m_pkthdr.len > MHLEN) {
862 MCLGET(m_new, M_DONTWAIT);
863
864 if (!(m_new->m_flags & M_EXT)) {
865 m_freem(m_new);
866 return(1);
867 }
868 }
869 m_copydata(m_head, 0, m_head->m_pkthdr.len, mtod(m_new, caddr_t));
870 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
871
872 /* Pad frames to at least 60 bytes. */
873 if (m_new->m_pkthdr.len < RL_MIN_FRAMELEN) {
874 /*
875 * Make security-conscious people happy: zero out the
876 * bytes in the pad area, since we don't know what
877 * this mbuf cluster buffer's previous user might
878 * have left in it.
879 */
880 bzero(mtod(m_new, char *) + m_new->m_pkthdr.len,
881 RL_MIN_FRAMELEN - m_new->m_pkthdr.len);
882 m_new->m_pkthdr.len +=
883 (RL_MIN_FRAMELEN - m_new->m_pkthdr.len);
884 m_new->m_len = m_new->m_pkthdr.len;
885 }
886
887 if (bus_dmamap_load_mbuf(sc->sc_dmat, RL_CUR_TXMAP(sc),
888 m_new, BUS_DMA_NOWAIT) != 0) {
889 m_freem(m_new);
890 return (1);
891 }
892 m_freem(m_head);
893
894 RL_CUR_TXMBUF(sc) = m_new;
895 bus_dmamap_sync(sc->sc_dmat, RL_CUR_TXMAP(sc), 0,
896 RL_CUR_TXMAP(sc)->dm_mapsize, BUS_DMASYNC_PREWRITE);
897 return(0);
898 }
899
900 /*
901 * Main transmit routine.
902 */
903
rl_start(ifp)904 void rl_start(ifp)
905 struct ifnet *ifp;
906 {
907 struct rl_softc *sc;
908 struct mbuf *m_head = NULL;
909 int pkts = 0;
910
911 sc = ifp->if_softc;
912
913 while(RL_CUR_TXMBUF(sc) == NULL) {
914 IFQ_DEQUEUE(&ifp->if_snd, m_head);
915 if (m_head == NULL)
916 break;
917
918 /* Pack the data into the descriptor. */
919 if (rl_encap(sc, m_head))
920 break;
921 pkts++;
922
923 #if NBPFILTER > 0
924 /*
925 * If there's a BPF listener, bounce a copy of this frame
926 * to him.
927 */
928 if (ifp->if_bpf)
929 bpf_mtap(ifp->if_bpf, RL_CUR_TXMBUF(sc));
930 #endif
931 /*
932 * Transmit the frame.
933 */
934 CSR_WRITE_4(sc, RL_CUR_TXADDR(sc),
935 RL_CUR_TXMAP(sc)->dm_segs[0].ds_addr);
936 CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc),
937 RL_TXTHRESH(sc->rl_txthresh) |
938 RL_CUR_TXMAP(sc)->dm_segs[0].ds_len);
939
940 RL_INC(sc->rl_cdata.cur_tx);
941
942 /*
943 * Set a timeout in case the chip goes out to lunch.
944 */
945 ifp->if_timer = 5;
946 }
947 if (pkts == 0)
948 return;
949
950 /*
951 * We broke out of the loop because all our TX slots are
952 * full. Mark the NIC as busy until it drains some of the
953 * packets from the queue.
954 */
955 if (RL_CUR_TXMBUF(sc) != NULL)
956 ifp->if_flags |= IFF_OACTIVE;
957 }
958
rl_init(xsc)959 void rl_init(xsc)
960 void *xsc;
961 {
962 struct rl_softc *sc = xsc;
963 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
964 int s, i;
965 u_int32_t rxcfg = 0;
966
967 s = splimp();
968
969 /*
970 * Cancel pending I/O and free all RX/TX buffers.
971 */
972 rl_stop(sc);
973
974 /* Init our MAC address */
975 for (i = 0; i < ETHER_ADDR_LEN; i++) {
976 CSR_WRITE_1(sc, RL_IDR0 + i, sc->sc_arpcom.ac_enaddr[i]);
977 }
978
979 /* Init the RX buffer pointer register. */
980 CSR_WRITE_4(sc, RL_RXADDR, sc->rl_cdata.rl_rx_buf_pa);
981
982 /* Init TX descriptors. */
983 rl_list_tx_init(sc);
984
985 /*
986 * Enable transmit and receive.
987 */
988 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
989
990 /*
991 * Set the initial TX and RX configuration.
992 */
993 CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
994 CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
995
996 /* Set the individual bit to receive frames for this host only. */
997 rxcfg = CSR_READ_4(sc, RL_RXCFG);
998 rxcfg |= RL_RXCFG_RX_INDIV;
999
1000 /* If we want promiscuous mode, set the allframes bit. */
1001 if (ifp->if_flags & IFF_PROMISC)
1002 rxcfg |= RL_RXCFG_RX_ALLPHYS;
1003 else
1004 rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
1005 CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1006
1007 /*
1008 * Set capture broadcast bit to capture broadcast frames.
1009 */
1010 if (ifp->if_flags & IFF_BROADCAST)
1011 rxcfg |= RL_RXCFG_RX_BROAD;
1012 else
1013 rxcfg &= ~RL_RXCFG_RX_BROAD;
1014 CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1015
1016 /*
1017 * Program the multicast filter, if necessary.
1018 */
1019 rl_setmulti(sc);
1020
1021 /*
1022 * Enable interrupts.
1023 */
1024 CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1025
1026 /* Set initial TX threshold */
1027 sc->rl_txthresh = RL_TX_THRESH_INIT;
1028
1029 /* Start RX/TX process. */
1030 CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
1031
1032 /* Enable receiver and transmitter. */
1033 CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
1034
1035 mii_mediachg(&sc->sc_mii);
1036
1037 CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
1038
1039 ifp->if_flags |= IFF_RUNNING;
1040 ifp->if_flags &= ~IFF_OACTIVE;
1041
1042 splx(s);
1043
1044 timeout_set(&sc->sc_tick_tmo, rl_tick, sc);
1045 timeout_add(&sc->sc_tick_tmo, hz);
1046 }
1047
1048 /*
1049 * Set media options.
1050 */
rl_ifmedia_upd(ifp)1051 int rl_ifmedia_upd(ifp)
1052 struct ifnet *ifp;
1053 {
1054 struct rl_softc *sc = (struct rl_softc *)ifp->if_softc;
1055
1056 mii_mediachg(&sc->sc_mii);
1057 return (0);
1058 }
1059
1060 /*
1061 * Report current media status.
1062 */
rl_ifmedia_sts(ifp,ifmr)1063 void rl_ifmedia_sts(ifp, ifmr)
1064 struct ifnet *ifp;
1065 struct ifmediareq *ifmr;
1066 {
1067 struct rl_softc *sc = ifp->if_softc;
1068
1069 mii_pollstat(&sc->sc_mii);
1070 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1071 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1072 }
1073
rl_ioctl(ifp,command,data)1074 int rl_ioctl(ifp, command, data)
1075 struct ifnet *ifp;
1076 u_long command;
1077 caddr_t data;
1078 {
1079 struct rl_softc *sc = ifp->if_softc;
1080 struct ifreq *ifr = (struct ifreq *) data;
1081 struct ifaddr *ifa = (struct ifaddr *)data;
1082 int s, error = 0;
1083
1084 s = splimp();
1085
1086 if ((error = ether_ioctl(ifp, &sc->sc_arpcom, command, data)) > 0) {
1087 splx(s);
1088 return error;
1089 }
1090
1091 switch(command) {
1092 case SIOCSIFADDR:
1093 ifp->if_flags |= IFF_UP;
1094 switch (ifa->ifa_addr->sa_family) {
1095 #ifdef INET
1096 case AF_INET:
1097 rl_init(sc);
1098 arp_ifinit(&sc->sc_arpcom, ifa);
1099 break;
1100 #endif /* INET */
1101 default:
1102 rl_init(sc);
1103 break;
1104 }
1105 break;
1106 case SIOCSIFMTU:
1107 if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN) {
1108 error = EINVAL;
1109 } else if (ifp->if_mtu != ifr->ifr_mtu) {
1110 ifp->if_mtu = ifr->ifr_mtu;
1111 }
1112 break;
1113 case SIOCSIFFLAGS:
1114 if (ifp->if_flags & IFF_UP) {
1115 rl_init(sc);
1116 } else {
1117 if (ifp->if_flags & IFF_RUNNING)
1118 rl_stop(sc);
1119 }
1120 error = 0;
1121 break;
1122 case SIOCADDMULTI:
1123 case SIOCDELMULTI:
1124 error = (command == SIOCADDMULTI) ?
1125 ether_addmulti(ifr, &sc->sc_arpcom) :
1126 ether_delmulti(ifr, &sc->sc_arpcom);
1127
1128 if (error == ENETRESET) {
1129 /*
1130 * Multicast list has changed; set the hardware
1131 * filter accordingly.
1132 */
1133 if (ifp->if_flags & IFF_RUNNING)
1134 rl_setmulti(sc);
1135 error = 0;
1136 }
1137 break;
1138 case SIOCGIFMEDIA:
1139 case SIOCSIFMEDIA:
1140 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
1141 break;
1142 default:
1143 error = EINVAL;
1144 break;
1145 }
1146
1147 splx(s);
1148
1149 return(error);
1150 }
1151
rl_watchdog(ifp)1152 void rl_watchdog(ifp)
1153 struct ifnet *ifp;
1154 {
1155 struct rl_softc *sc;
1156
1157 sc = ifp->if_softc;
1158
1159 printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1160 ifp->if_oerrors++;
1161 rl_txeof(sc);
1162 rl_rxeof(sc);
1163 rl_init(sc);
1164 }
1165
1166 /*
1167 * Stop the adapter and free any mbufs allocated to the
1168 * RX and TX lists.
1169 */
rl_stop(sc)1170 void rl_stop(sc)
1171 struct rl_softc *sc;
1172 {
1173 register int i;
1174 struct ifnet *ifp;
1175
1176 ifp = &sc->sc_arpcom.ac_if;
1177 ifp->if_timer = 0;
1178
1179 timeout_del(&sc->sc_tick_tmo);
1180
1181 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1182
1183 CSR_WRITE_1(sc, RL_COMMAND, 0x00);
1184 CSR_WRITE_2(sc, RL_IMR, 0x0000);
1185
1186 /*
1187 * Free the TX list buffers.
1188 */
1189 for (i = 0; i < RL_TX_LIST_CNT; i++) {
1190 if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
1191 bus_dmamap_sync(sc->sc_dmat,
1192 sc->rl_cdata.rl_tx_dmamap[i], 0,
1193 sc->rl_cdata.rl_tx_dmamap[i]->dm_mapsize,
1194 BUS_DMASYNC_POSTWRITE);
1195 bus_dmamap_unload(sc->sc_dmat,
1196 sc->rl_cdata.rl_tx_dmamap[i]);
1197 m_freem(sc->rl_cdata.rl_tx_chain[i]);
1198 sc->rl_cdata.rl_tx_chain[i] = NULL;
1199 CSR_WRITE_4(sc, RL_TXADDR0 + (i * sizeof(u_int32_t)),
1200 0x00000000);
1201 }
1202 }
1203 }
1204
1205 int
rl_attach(sc)1206 rl_attach(sc)
1207 struct rl_softc *sc;
1208 {
1209 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1210 int rseg, i;
1211 u_int16_t rl_id, rl_did;
1212 caddr_t kva;
1213 int addr_len;
1214
1215 rl_reset(sc);
1216
1217 /*
1218 * Check EEPROM type 9346 or 9356.
1219 */
1220 rl_read_eeprom(sc, (caddr_t)&rl_id, RL_EE_ID, RL_EEADDR_LEN1, 1, 0);
1221 if (rl_id == 0x8129)
1222 addr_len = RL_EEADDR_LEN1;
1223 else
1224 addr_len = RL_EEADDR_LEN0;
1225
1226 /*
1227 * Get station address.
1228 */
1229 rl_read_eeprom(sc, (caddr_t)sc->sc_arpcom.ac_enaddr, RL_EE_EADDR,
1230 addr_len, 3, 1);
1231
1232 printf(" address %s\n", ether_sprintf(sc->sc_arpcom.ac_enaddr));
1233
1234 rl_read_eeprom(sc, (caddr_t)&rl_did, RL_EE_PCI_DID, addr_len, 1, 0);
1235
1236 if (rl_did == RT_DEVICEID_8139 || rl_did == ACCTON_DEVICEID_5030 ||
1237 rl_did == DELTA_DEVICEID_8139 || rl_did == ADDTRON_DEVICEID_8139 ||
1238 rl_did == DLINK_DEVICEID_8139 || rl_did == DLINK_DEVICEID_8139_2 ||
1239 rl_did == ABOCOM_DEVICEID_8139)
1240 sc->rl_type = RL_8139;
1241 else if (rl_did == RT_DEVICEID_8129)
1242 sc->rl_type = RL_8129;
1243 else
1244 sc->rl_type = RL_UNKNOWN; /* could be 8138 or other */
1245
1246 if (bus_dmamem_alloc(sc->sc_dmat, RL_RXBUFLEN + 32, PAGE_SIZE, 0,
1247 &sc->sc_rx_seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1248 printf("\n%s: can't alloc rx buffers\n", sc->sc_dev.dv_xname);
1249 return (1);
1250 }
1251 if (bus_dmamem_map(sc->sc_dmat, &sc->sc_rx_seg, rseg,
1252 RL_RXBUFLEN + 32, &kva, BUS_DMA_NOWAIT)) {
1253 printf("%s: can't map dma buffers (%d bytes)\n",
1254 sc->sc_dev.dv_xname, RL_RXBUFLEN + 32);
1255 bus_dmamem_free(sc->sc_dmat, &sc->sc_rx_seg, rseg);
1256 return (1);
1257 }
1258 if (bus_dmamap_create(sc->sc_dmat, RL_RXBUFLEN + 32, 1,
1259 RL_RXBUFLEN + 32, 0, BUS_DMA_NOWAIT, &sc->sc_rx_dmamap)) {
1260 printf("%s: can't create dma map\n", sc->sc_dev.dv_xname);
1261 bus_dmamem_unmap(sc->sc_dmat, kva, RL_RXBUFLEN + 32);
1262 bus_dmamem_free(sc->sc_dmat, &sc->sc_rx_seg, rseg);
1263 return (1);
1264 }
1265 if (bus_dmamap_load(sc->sc_dmat, sc->sc_rx_dmamap, kva,
1266 RL_RXBUFLEN + 32, NULL, BUS_DMA_NOWAIT)) {
1267 printf("%s: can't load dma map\n", sc->sc_dev.dv_xname);
1268 bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_dmamap);
1269 bus_dmamem_unmap(sc->sc_dmat, kva, RL_RXBUFLEN + 32);
1270 bus_dmamem_free(sc->sc_dmat, &sc->sc_rx_seg, rseg);
1271 return (1);
1272 }
1273 sc->rl_cdata.rl_rx_buf = kva;
1274 sc->rl_cdata.rl_rx_buf_pa = sc->sc_rx_dmamap->dm_segs[0].ds_addr;
1275
1276 bzero(sc->rl_cdata.rl_rx_buf, RL_RXBUFLEN + 32);
1277
1278 bus_dmamap_sync(sc->sc_dmat, sc->sc_rx_dmamap,
1279 0, sc->sc_rx_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1280
1281 for (i = 0; i < RL_TX_LIST_CNT; i++) {
1282 if (bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0,
1283 BUS_DMA_NOWAIT, &sc->rl_cdata.rl_tx_dmamap[i]) != 0) {
1284 printf("%s: can't create tx maps\n", sc->sc_dev.dv_xname);
1285 /* XXX free any allocated... */
1286 return (1);
1287 }
1288 }
1289
1290 /* Leave a few bytes before the start of the RX ring buffer. */
1291 sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf;
1292 sc->rl_cdata.rl_rx_buf += sizeof(u_int64_t);
1293 sc->rl_cdata.rl_rx_buf_pa += sizeof(u_int64_t);
1294
1295 ifp->if_softc = sc;
1296 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1297 ifp->if_ioctl = rl_ioctl;
1298 ifp->if_start = rl_start;
1299 ifp->if_watchdog = rl_watchdog;
1300 ifp->if_baudrate = 10000000;
1301 IFQ_SET_READY(&ifp->if_snd);
1302
1303 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
1304
1305 ifp->if_capabilities = IFCAP_VLAN_MTU;
1306
1307 /*
1308 * Initialize our media structures and probe the MII.
1309 */
1310 sc->sc_mii.mii_ifp = ifp;
1311 sc->sc_mii.mii_readreg = rl_miibus_readreg;
1312 sc->sc_mii.mii_writereg = rl_miibus_writereg;
1313 sc->sc_mii.mii_statchg = rl_miibus_statchg;
1314 ifmedia_init(&sc->sc_mii.mii_media, 0, rl_ifmedia_upd, rl_ifmedia_sts);
1315 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
1316 MII_OFFSET_ANY, 0);
1317 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
1318 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
1319 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
1320 } else
1321 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
1322
1323 /*
1324 * Attach us everywhere
1325 */
1326 if_attach(ifp);
1327 ether_ifattach(ifp);
1328
1329 sc->sc_sdhook = shutdownhook_establish(rl_shutdown, sc);
1330 sc->sc_pwrhook = powerhook_establish(rl_powerhook, sc);
1331
1332 return (0);
1333 }
1334
1335 int
rl_detach(sc)1336 rl_detach(sc)
1337 struct rl_softc *sc;
1338 {
1339 struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1340
1341 /* Unhook our tick handler. */
1342 timeout_del(&sc->sc_tick_tmo);
1343
1344 /* Detach any PHYs we might have. */
1345 if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL)
1346 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
1347
1348 /* Delete any remaining media. */
1349 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
1350
1351 ether_ifdetach(ifp);
1352 if_detach(ifp);
1353
1354 shutdownhook_disestablish(sc->sc_sdhook);
1355 powerhook_disestablish(sc->sc_pwrhook);
1356
1357 return (0);
1358 }
1359
1360 void
rl_shutdown(arg)1361 rl_shutdown(arg)
1362 void *arg;
1363 {
1364 struct rl_softc *sc = (struct rl_softc *)arg;
1365
1366 rl_stop(sc);
1367 }
1368
1369 void
rl_powerhook(why,arg)1370 rl_powerhook(why, arg)
1371 int why;
1372 void *arg;
1373 {
1374 if (why == PWR_RESUME)
1375 rl_init(arg);
1376 }
1377
1378 int
rl_miibus_readreg(self,phy,reg)1379 rl_miibus_readreg(self, phy, reg)
1380 struct device *self;
1381 int phy, reg;
1382 {
1383 struct rl_softc *sc = (struct rl_softc *)self;
1384 struct rl_mii_frame frame;
1385 u_int16_t rl8139_reg;
1386
1387 if (sc->rl_type == RL_8139) {
1388 /*
1389 * The RTL8139 PHY is mapped into PCI registers, unfortunately
1390 * it has no phyid, or phyaddr, so assume it is phyaddr 0.
1391 */
1392 if (phy != 0)
1393 return(0);
1394
1395 switch (reg) {
1396 case MII_BMCR:
1397 rl8139_reg = RL_BMCR;
1398 break;
1399 case MII_BMSR:
1400 rl8139_reg = RL_BMSR;
1401 break;
1402 case MII_ANAR:
1403 rl8139_reg = RL_ANAR;
1404 break;
1405 case MII_ANER:
1406 rl8139_reg = RL_ANER;
1407 break;
1408 case MII_ANLPAR:
1409 rl8139_reg = RL_LPAR;
1410 break;
1411 case RL_MEDIASTAT:
1412 return (CSR_READ_1(sc, RL_MEDIASTAT));
1413 case MII_PHYIDR1:
1414 case MII_PHYIDR2:
1415 default:
1416 return (0);
1417 }
1418 return (CSR_READ_2(sc, rl8139_reg));
1419 }
1420
1421 bzero((char *)&frame, sizeof(frame));
1422
1423 frame.mii_phyaddr = phy;
1424 frame.mii_regaddr = reg;
1425 rl_mii_readreg(sc, &frame);
1426
1427 return(frame.mii_data);
1428 }
1429
1430 void
rl_miibus_writereg(self,phy,reg,val)1431 rl_miibus_writereg(self, phy, reg, val)
1432 struct device *self;
1433 int phy, reg, val;
1434 {
1435 struct rl_softc *sc = (struct rl_softc *)self;
1436 struct rl_mii_frame frame;
1437 u_int16_t rl8139_reg = 0;
1438
1439 if (sc->rl_type == RL_8139) {
1440 if (phy)
1441 return;
1442
1443 switch (reg) {
1444 case MII_BMCR:
1445 rl8139_reg = RL_BMCR;
1446 break;
1447 case MII_BMSR:
1448 rl8139_reg = RL_BMSR;
1449 break;
1450 case MII_ANAR:
1451 rl8139_reg = RL_ANAR;
1452 break;
1453 case MII_ANER:
1454 rl8139_reg = RL_ANER;
1455 break;
1456 case MII_ANLPAR:
1457 rl8139_reg = RL_LPAR;
1458 break;
1459 case MII_PHYIDR1:
1460 case MII_PHYIDR2:
1461 return;
1462 }
1463 CSR_WRITE_2(sc, rl8139_reg, val);
1464 return;
1465 }
1466
1467 bzero((char *)&frame, sizeof(frame));
1468 frame.mii_phyaddr = phy;
1469 frame.mii_regaddr = reg;
1470 frame.mii_data = val;
1471 rl_mii_writereg(sc, &frame);
1472 }
1473
1474 void
rl_miibus_statchg(self)1475 rl_miibus_statchg(self)
1476 struct device *self;
1477 {
1478 }
1479
1480 void
rl_tick(v)1481 rl_tick(v)
1482 void *v;
1483 {
1484 struct rl_softc *sc = v;
1485
1486 mii_tick(&sc->sc_mii);
1487 timeout_add(&sc->sc_tick_tmo, hz);
1488 }
1489
1490 struct cfdriver rl_cd = {
1491 0, "rl", DV_IFNET
1492 };
1493