rtl81x9.c revision 1.65 1 1.65 tsutsui /* $NetBSD: rtl81x9.c,v 1.65 2006/11/05 15:49:41 tsutsui Exp $ */
2 1.1 haya
3 1.1 haya /*
4 1.1 haya * Copyright (c) 1997, 1998
5 1.1 haya * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
6 1.1 haya *
7 1.1 haya * Redistribution and use in source and binary forms, with or without
8 1.1 haya * modification, are permitted provided that the following conditions
9 1.1 haya * are met:
10 1.1 haya * 1. Redistributions of source code must retain the above copyright
11 1.1 haya * notice, this list of conditions and the following disclaimer.
12 1.1 haya * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 haya * notice, this list of conditions and the following disclaimer in the
14 1.1 haya * documentation and/or other materials provided with the distribution.
15 1.1 haya * 3. All advertising materials mentioning features or use of this software
16 1.1 haya * must display the following acknowledgement:
17 1.1 haya * This product includes software developed by Bill Paul.
18 1.1 haya * 4. Neither the name of the author nor the names of any co-contributors
19 1.1 haya * may be used to endorse or promote products derived from this software
20 1.1 haya * without specific prior written permission.
21 1.1 haya *
22 1.1 haya * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 1.1 haya * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 haya * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 haya * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 1.1 haya * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 haya * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 haya * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 haya * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 haya * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 haya * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 haya * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 haya *
34 1.1 haya * FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
35 1.1 haya */
36 1.1 haya
37 1.1 haya /*
38 1.1 haya * RealTek 8129/8139 PCI NIC driver
39 1.1 haya *
40 1.1 haya * Supports several extremely cheap PCI 10/100 adapters based on
41 1.1 haya * the RealTek chipset. Datasheets can be obtained from
42 1.1 haya * www.realtek.com.tw.
43 1.1 haya *
44 1.1 haya * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
45 1.1 haya * Electrical Engineering Department
46 1.1 haya * Columbia University, New York City
47 1.1 haya */
48 1.1 haya
49 1.1 haya /*
50 1.1 haya * The RealTek 8139 PCI NIC redefines the meaning of 'low end.' This is
51 1.1 haya * probably the worst PCI ethernet controller ever made, with the possible
52 1.1 haya * exception of the FEAST chip made by SMC. The 8139 supports bus-master
53 1.1 haya * DMA, but it has a terrible interface that nullifies any performance
54 1.1 haya * gains that bus-master DMA usually offers.
55 1.1 haya *
56 1.1 haya * For transmission, the chip offers a series of four TX descriptor
57 1.1 haya * registers. Each transmit frame must be in a contiguous buffer, aligned
58 1.1 haya * on a longword (32-bit) boundary. This means we almost always have to
59 1.1 haya * do mbuf copies in order to transmit a frame, except in the unlikely
60 1.1 haya * case where a) the packet fits into a single mbuf, and b) the packet
61 1.1 haya * is 32-bit aligned within the mbuf's data area. The presence of only
62 1.1 haya * four descriptor registers means that we can never have more than four
63 1.1 haya * packets queued for transmission at any one time.
64 1.1 haya *
65 1.1 haya * Reception is not much better. The driver has to allocate a single large
66 1.1 haya * buffer area (up to 64K in size) into which the chip will DMA received
67 1.1 haya * frames. Because we don't know where within this region received packets
68 1.1 haya * will begin or end, we have no choice but to copy data from the buffer
69 1.1 haya * area into mbufs in order to pass the packets up to the higher protocol
70 1.1 haya * levels.
71 1.1 haya *
72 1.1 haya * It's impossible given this rotten design to really achieve decent
73 1.45 tsutsui * performance at 100Mbps, unless you happen to have a 400MHz PII or
74 1.1 haya * some equally overmuscled CPU to drive it.
75 1.1 haya *
76 1.1 haya * On the bright side, the 8139 does have a built-in PHY, although
77 1.1 haya * rather than using an MDIO serial interface like most other NICs, the
78 1.1 haya * PHY registers are directly accessible through the 8139's register
79 1.1 haya * space. The 8139 supports autonegotiation, as well as a 64-bit multicast
80 1.1 haya * filter.
81 1.1 haya *
82 1.1 haya * The 8129 chip is an older version of the 8139 that uses an external PHY
83 1.1 haya * chip. The 8129 has a serial MDIO interface for accessing the MII where
84 1.1 haya * the 8139 lets you directly access the on-board PHY registers. We need
85 1.1 haya * to select which interface to use depending on the chip type.
86 1.1 haya */
87 1.40 lukem
88 1.40 lukem #include <sys/cdefs.h>
89 1.65 tsutsui __KERNEL_RCSID(0, "$NetBSD: rtl81x9.c,v 1.65 2006/11/05 15:49:41 tsutsui Exp $");
90 1.1 haya
91 1.1 haya #include "bpfilter.h"
92 1.1 haya #include "rnd.h"
93 1.1 haya
94 1.1 haya #include <sys/param.h>
95 1.1 haya #include <sys/systm.h>
96 1.1 haya #include <sys/callout.h>
97 1.1 haya #include <sys/device.h>
98 1.1 haya #include <sys/sockio.h>
99 1.1 haya #include <sys/mbuf.h>
100 1.1 haya #include <sys/malloc.h>
101 1.1 haya #include <sys/kernel.h>
102 1.1 haya #include <sys/socket.h>
103 1.1 haya
104 1.17 thorpej #include <uvm/uvm_extern.h>
105 1.17 thorpej
106 1.1 haya #include <net/if.h>
107 1.1 haya #include <net/if_arp.h>
108 1.1 haya #include <net/if_ether.h>
109 1.1 haya #include <net/if_dl.h>
110 1.1 haya #include <net/if_media.h>
111 1.1 haya
112 1.1 haya #if NBPFILTER > 0
113 1.1 haya #include <net/bpf.h>
114 1.1 haya #endif
115 1.1 haya #if NRND > 0
116 1.1 haya #include <sys/rnd.h>
117 1.1 haya #endif
118 1.1 haya
119 1.1 haya #include <machine/bus.h>
120 1.3 tsutsui #include <machine/endian.h>
121 1.1 haya
122 1.1 haya #include <dev/mii/mii.h>
123 1.1 haya #include <dev/mii/miivar.h>
124 1.1 haya
125 1.1 haya #include <dev/ic/rtl81x9reg.h>
126 1.4 tsutsui #include <dev/ic/rtl81x9var.h>
127 1.1 haya
128 1.23 tsutsui #if defined(DEBUG)
129 1.1 haya #define STATIC
130 1.1 haya #else
131 1.1 haya #define STATIC static
132 1.1 haya #endif
133 1.1 haya
134 1.61 tsutsui STATIC void rtk_reset(struct rtk_softc *);
135 1.61 tsutsui STATIC void rtk_rxeof(struct rtk_softc *);
136 1.61 tsutsui STATIC void rtk_txeof(struct rtk_softc *);
137 1.61 tsutsui STATIC void rtk_start(struct ifnet *);
138 1.61 tsutsui STATIC int rtk_ioctl(struct ifnet *, u_long, caddr_t);
139 1.61 tsutsui STATIC int rtk_init(struct ifnet *);
140 1.61 tsutsui STATIC void rtk_stop(struct ifnet *, int);
141 1.49 perry
142 1.49 perry STATIC void rtk_watchdog(struct ifnet *);
143 1.49 perry STATIC void rtk_shutdown(void *);
144 1.49 perry STATIC int rtk_ifmedia_upd(struct ifnet *);
145 1.49 perry STATIC void rtk_ifmedia_sts(struct ifnet *, struct ifmediareq *);
146 1.49 perry
147 1.49 perry STATIC void rtk_eeprom_putbyte(struct rtk_softc *, int, int);
148 1.49 perry STATIC void rtk_mii_sync(struct rtk_softc *);
149 1.63 tsutsui STATIC void rtk_mii_send(struct rtk_softc *, uint32_t, int);
150 1.49 perry STATIC int rtk_mii_readreg(struct rtk_softc *, struct rtk_mii_frame *);
151 1.49 perry STATIC int rtk_mii_writereg(struct rtk_softc *, struct rtk_mii_frame *);
152 1.49 perry
153 1.49 perry STATIC int rtk_phy_readreg(struct device *, int, int);
154 1.49 perry STATIC void rtk_phy_writereg(struct device *, int, int, int);
155 1.49 perry STATIC void rtk_phy_statchg(struct device *);
156 1.61 tsutsui STATIC void rtk_tick(void *);
157 1.49 perry
158 1.61 tsutsui STATIC int rtk_enable(struct rtk_softc *);
159 1.61 tsutsui STATIC void rtk_disable(struct rtk_softc *);
160 1.61 tsutsui STATIC void rtk_power(int, void *);
161 1.10 tsutsui
162 1.49 perry STATIC int rtk_list_tx_init(struct rtk_softc *);
163 1.1 haya
164 1.1 haya #define EE_SET(x) \
165 1.10 tsutsui CSR_WRITE_1(sc, RTK_EECMD, \
166 1.10 tsutsui CSR_READ_1(sc, RTK_EECMD) | (x))
167 1.1 haya
168 1.1 haya #define EE_CLR(x) \
169 1.10 tsutsui CSR_WRITE_1(sc, RTK_EECMD, \
170 1.10 tsutsui CSR_READ_1(sc, RTK_EECMD) & ~(x))
171 1.1 haya
172 1.44 bouyer #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN)
173 1.44 bouyer
174 1.1 haya /*
175 1.1 haya * Send a read command and address to the EEPROM, check for ACK.
176 1.1 haya */
177 1.50 jdolecek STATIC void
178 1.62 tsutsui rtk_eeprom_putbyte(struct rtk_softc *sc, int addr, int addr_len)
179 1.1 haya {
180 1.63 tsutsui int d, i;
181 1.1 haya
182 1.10 tsutsui d = (RTK_EECMD_READ << addr_len) | addr;
183 1.1 haya
184 1.1 haya /*
185 1.1 haya * Feed in each bit and stobe the clock.
186 1.1 haya */
187 1.23 tsutsui for (i = RTK_EECMD_LEN + addr_len; i > 0; i--) {
188 1.23 tsutsui if (d & (1 << (i - 1))) {
189 1.10 tsutsui EE_SET(RTK_EE_DATAIN);
190 1.1 haya } else {
191 1.10 tsutsui EE_CLR(RTK_EE_DATAIN);
192 1.1 haya }
193 1.23 tsutsui DELAY(4);
194 1.10 tsutsui EE_SET(RTK_EE_CLK);
195 1.23 tsutsui DELAY(4);
196 1.10 tsutsui EE_CLR(RTK_EE_CLK);
197 1.23 tsutsui DELAY(4);
198 1.1 haya }
199 1.1 haya }
200 1.1 haya
201 1.1 haya /*
202 1.1 haya * Read a word of data stored in the EEPROM at address 'addr.'
203 1.1 haya */
204 1.63 tsutsui uint16_t
205 1.62 tsutsui rtk_read_eeprom(struct rtk_softc *sc, int addr, int addr_len)
206 1.1 haya {
207 1.63 tsutsui uint16_t word;
208 1.63 tsutsui int i;
209 1.1 haya
210 1.1 haya /* Enter EEPROM access mode. */
211 1.10 tsutsui CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM|RTK_EE_SEL);
212 1.1 haya
213 1.1 haya /*
214 1.1 haya * Send address of word we want to read.
215 1.1 haya */
216 1.8 thorpej rtk_eeprom_putbyte(sc, addr, addr_len);
217 1.1 haya
218 1.10 tsutsui CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_PROGRAM|RTK_EE_SEL);
219 1.1 haya
220 1.1 haya /*
221 1.1 haya * Start reading bits from EEPROM.
222 1.1 haya */
223 1.63 tsutsui word = 0;
224 1.23 tsutsui for (i = 16; i > 0; i--) {
225 1.10 tsutsui EE_SET(RTK_EE_CLK);
226 1.23 tsutsui DELAY(4);
227 1.10 tsutsui if (CSR_READ_1(sc, RTK_EECMD) & RTK_EE_DATAOUT)
228 1.23 tsutsui word |= 1 << (i - 1);
229 1.10 tsutsui EE_CLR(RTK_EE_CLK);
230 1.23 tsutsui DELAY(4);
231 1.1 haya }
232 1.1 haya
233 1.1 haya /* Turn off EEPROM access mode. */
234 1.10 tsutsui CSR_WRITE_1(sc, RTK_EECMD, RTK_EEMODE_OFF);
235 1.1 haya
236 1.63 tsutsui return word;
237 1.1 haya }
238 1.1 haya
239 1.1 haya /*
240 1.1 haya * MII access routines are provided for the 8129, which
241 1.1 haya * doesn't have a built-in PHY. For the 8139, we fake things
242 1.8 thorpej * up by diverting rtk_phy_readreg()/rtk_phy_writereg() to the
243 1.1 haya * direct access PHY registers.
244 1.1 haya */
245 1.1 haya #define MII_SET(x) \
246 1.23 tsutsui CSR_WRITE_1(sc, RTK_MII, \
247 1.10 tsutsui CSR_READ_1(sc, RTK_MII) | (x))
248 1.1 haya
249 1.1 haya #define MII_CLR(x) \
250 1.23 tsutsui CSR_WRITE_1(sc, RTK_MII, \
251 1.10 tsutsui CSR_READ_1(sc, RTK_MII) & ~(x))
252 1.1 haya
253 1.1 haya /*
254 1.1 haya * Sync the PHYs by setting data bit and strobing the clock 32 times.
255 1.1 haya */
256 1.50 jdolecek STATIC void
257 1.62 tsutsui rtk_mii_sync(struct rtk_softc *sc)
258 1.1 haya {
259 1.63 tsutsui int i;
260 1.1 haya
261 1.10 tsutsui MII_SET(RTK_MII_DIR|RTK_MII_DATAOUT);
262 1.1 haya
263 1.1 haya for (i = 0; i < 32; i++) {
264 1.10 tsutsui MII_SET(RTK_MII_CLK);
265 1.1 haya DELAY(1);
266 1.10 tsutsui MII_CLR(RTK_MII_CLK);
267 1.1 haya DELAY(1);
268 1.1 haya }
269 1.1 haya }
270 1.1 haya
271 1.1 haya /*
272 1.1 haya * Clock a series of bits through the MII.
273 1.1 haya */
274 1.50 jdolecek STATIC void
275 1.63 tsutsui rtk_mii_send(struct rtk_softc *sc, uint32_t bits, int cnt)
276 1.1 haya {
277 1.63 tsutsui int i;
278 1.1 haya
279 1.10 tsutsui MII_CLR(RTK_MII_CLK);
280 1.1 haya
281 1.23 tsutsui for (i = cnt; i > 0; i--) {
282 1.61 tsutsui if (bits & (1 << (i - 1))) {
283 1.10 tsutsui MII_SET(RTK_MII_DATAOUT);
284 1.61 tsutsui } else {
285 1.10 tsutsui MII_CLR(RTK_MII_DATAOUT);
286 1.61 tsutsui }
287 1.1 haya DELAY(1);
288 1.10 tsutsui MII_CLR(RTK_MII_CLK);
289 1.1 haya DELAY(1);
290 1.10 tsutsui MII_SET(RTK_MII_CLK);
291 1.1 haya }
292 1.1 haya }
293 1.1 haya
294 1.1 haya /*
295 1.1 haya * Read an PHY register through the MII.
296 1.1 haya */
297 1.50 jdolecek STATIC int
298 1.62 tsutsui rtk_mii_readreg(struct rtk_softc *sc, struct rtk_mii_frame *frame)
299 1.1 haya {
300 1.63 tsutsui int i, ack, s;
301 1.1 haya
302 1.9 thorpej s = splnet();
303 1.1 haya
304 1.1 haya /*
305 1.1 haya * Set up frame for RX.
306 1.1 haya */
307 1.10 tsutsui frame->mii_stdelim = RTK_MII_STARTDELIM;
308 1.10 tsutsui frame->mii_opcode = RTK_MII_READOP;
309 1.1 haya frame->mii_turnaround = 0;
310 1.1 haya frame->mii_data = 0;
311 1.23 tsutsui
312 1.10 tsutsui CSR_WRITE_2(sc, RTK_MII, 0);
313 1.1 haya
314 1.1 haya /*
315 1.61 tsutsui * Turn on data xmit.
316 1.1 haya */
317 1.10 tsutsui MII_SET(RTK_MII_DIR);
318 1.1 haya
319 1.8 thorpej rtk_mii_sync(sc);
320 1.1 haya
321 1.1 haya /*
322 1.1 haya * Send command/address info.
323 1.1 haya */
324 1.8 thorpej rtk_mii_send(sc, frame->mii_stdelim, 2);
325 1.8 thorpej rtk_mii_send(sc, frame->mii_opcode, 2);
326 1.8 thorpej rtk_mii_send(sc, frame->mii_phyaddr, 5);
327 1.8 thorpej rtk_mii_send(sc, frame->mii_regaddr, 5);
328 1.1 haya
329 1.1 haya /* Idle bit */
330 1.10 tsutsui MII_CLR((RTK_MII_CLK|RTK_MII_DATAOUT));
331 1.1 haya DELAY(1);
332 1.10 tsutsui MII_SET(RTK_MII_CLK);
333 1.1 haya DELAY(1);
334 1.1 haya
335 1.1 haya /* Turn off xmit. */
336 1.10 tsutsui MII_CLR(RTK_MII_DIR);
337 1.1 haya
338 1.1 haya /* Check for ack */
339 1.10 tsutsui MII_CLR(RTK_MII_CLK);
340 1.1 haya DELAY(1);
341 1.56 tsutsui ack = CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN;
342 1.10 tsutsui MII_SET(RTK_MII_CLK);
343 1.1 haya DELAY(1);
344 1.1 haya
345 1.1 haya /*
346 1.1 haya * Now try reading data bits. If the ack failed, we still
347 1.1 haya * need to clock through 16 cycles to keep the PHY(s) in sync.
348 1.1 haya */
349 1.1 haya if (ack) {
350 1.23 tsutsui for (i = 0; i < 16; i++) {
351 1.10 tsutsui MII_CLR(RTK_MII_CLK);
352 1.1 haya DELAY(1);
353 1.10 tsutsui MII_SET(RTK_MII_CLK);
354 1.1 haya DELAY(1);
355 1.1 haya }
356 1.1 haya goto fail;
357 1.1 haya }
358 1.1 haya
359 1.23 tsutsui for (i = 16; i > 0; i--) {
360 1.10 tsutsui MII_CLR(RTK_MII_CLK);
361 1.1 haya DELAY(1);
362 1.1 haya if (!ack) {
363 1.10 tsutsui if (CSR_READ_2(sc, RTK_MII) & RTK_MII_DATAIN)
364 1.23 tsutsui frame->mii_data |= 1 << (i - 1);
365 1.1 haya DELAY(1);
366 1.1 haya }
367 1.10 tsutsui MII_SET(RTK_MII_CLK);
368 1.1 haya DELAY(1);
369 1.1 haya }
370 1.1 haya
371 1.23 tsutsui fail:
372 1.10 tsutsui MII_CLR(RTK_MII_CLK);
373 1.1 haya DELAY(1);
374 1.10 tsutsui MII_SET(RTK_MII_CLK);
375 1.1 haya DELAY(1);
376 1.1 haya
377 1.1 haya splx(s);
378 1.1 haya
379 1.1 haya if (ack)
380 1.63 tsutsui return 1;
381 1.63 tsutsui return 0;
382 1.1 haya }
383 1.1 haya
384 1.1 haya /*
385 1.1 haya * Write to a PHY register through the MII.
386 1.1 haya */
387 1.50 jdolecek STATIC int
388 1.62 tsutsui rtk_mii_writereg(struct rtk_softc *sc, struct rtk_mii_frame *frame)
389 1.1 haya {
390 1.63 tsutsui int s;
391 1.1 haya
392 1.9 thorpej s = splnet();
393 1.1 haya /*
394 1.1 haya * Set up frame for TX.
395 1.1 haya */
396 1.10 tsutsui frame->mii_stdelim = RTK_MII_STARTDELIM;
397 1.10 tsutsui frame->mii_opcode = RTK_MII_WRITEOP;
398 1.10 tsutsui frame->mii_turnaround = RTK_MII_TURNAROUND;
399 1.51 perry
400 1.1 haya /*
401 1.61 tsutsui * Turn on data output.
402 1.1 haya */
403 1.10 tsutsui MII_SET(RTK_MII_DIR);
404 1.1 haya
405 1.8 thorpej rtk_mii_sync(sc);
406 1.1 haya
407 1.8 thorpej rtk_mii_send(sc, frame->mii_stdelim, 2);
408 1.8 thorpej rtk_mii_send(sc, frame->mii_opcode, 2);
409 1.8 thorpej rtk_mii_send(sc, frame->mii_phyaddr, 5);
410 1.8 thorpej rtk_mii_send(sc, frame->mii_regaddr, 5);
411 1.8 thorpej rtk_mii_send(sc, frame->mii_turnaround, 2);
412 1.8 thorpej rtk_mii_send(sc, frame->mii_data, 16);
413 1.1 haya
414 1.1 haya /* Idle bit. */
415 1.10 tsutsui MII_SET(RTK_MII_CLK);
416 1.1 haya DELAY(1);
417 1.10 tsutsui MII_CLR(RTK_MII_CLK);
418 1.1 haya DELAY(1);
419 1.1 haya
420 1.1 haya /*
421 1.1 haya * Turn off xmit.
422 1.1 haya */
423 1.10 tsutsui MII_CLR(RTK_MII_DIR);
424 1.1 haya
425 1.1 haya splx(s);
426 1.1 haya
427 1.63 tsutsui return 0;
428 1.1 haya }
429 1.1 haya
430 1.50 jdolecek STATIC int
431 1.62 tsutsui rtk_phy_readreg(struct device *self, int phy, int reg)
432 1.1 haya {
433 1.63 tsutsui struct rtk_softc *sc = (void *)self;
434 1.63 tsutsui struct rtk_mii_frame frame;
435 1.63 tsutsui int rval;
436 1.63 tsutsui int rtk8139_reg;
437 1.1 haya
438 1.10 tsutsui if (sc->rtk_type == RTK_8139) {
439 1.1 haya if (phy != 7)
440 1.63 tsutsui return 0;
441 1.1 haya
442 1.63 tsutsui switch (reg) {
443 1.1 haya case MII_BMCR:
444 1.10 tsutsui rtk8139_reg = RTK_BMCR;
445 1.1 haya break;
446 1.1 haya case MII_BMSR:
447 1.10 tsutsui rtk8139_reg = RTK_BMSR;
448 1.1 haya break;
449 1.1 haya case MII_ANAR:
450 1.10 tsutsui rtk8139_reg = RTK_ANAR;
451 1.1 haya break;
452 1.12 drochner case MII_ANER:
453 1.12 drochner rtk8139_reg = RTK_ANER;
454 1.12 drochner break;
455 1.1 haya case MII_ANLPAR:
456 1.10 tsutsui rtk8139_reg = RTK_LPAR;
457 1.1 haya break;
458 1.1 haya default:
459 1.1 haya #if 0
460 1.1 haya printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
461 1.1 haya #endif
462 1.63 tsutsui return 0;
463 1.1 haya }
464 1.10 tsutsui rval = CSR_READ_2(sc, rtk8139_reg);
465 1.63 tsutsui return rval;
466 1.1 haya }
467 1.1 haya
468 1.34 thorpej memset((char *)&frame, 0, sizeof(frame));
469 1.1 haya
470 1.1 haya frame.mii_phyaddr = phy;
471 1.1 haya frame.mii_regaddr = reg;
472 1.8 thorpej rtk_mii_readreg(sc, &frame);
473 1.1 haya
474 1.63 tsutsui return frame.mii_data;
475 1.1 haya }
476 1.1 haya
477 1.62 tsutsui STATIC void rtk_phy_writereg(struct device *self, int phy, int reg, int data)
478 1.1 haya {
479 1.63 tsutsui struct rtk_softc *sc = (void *)self;
480 1.63 tsutsui struct rtk_mii_frame frame;
481 1.63 tsutsui int rtk8139_reg;
482 1.1 haya
483 1.10 tsutsui if (sc->rtk_type == RTK_8139) {
484 1.1 haya if (phy != 7)
485 1.1 haya return;
486 1.1 haya
487 1.63 tsutsui switch (reg) {
488 1.1 haya case MII_BMCR:
489 1.10 tsutsui rtk8139_reg = RTK_BMCR;
490 1.1 haya break;
491 1.1 haya case MII_BMSR:
492 1.10 tsutsui rtk8139_reg = RTK_BMSR;
493 1.1 haya break;
494 1.1 haya case MII_ANAR:
495 1.10 tsutsui rtk8139_reg = RTK_ANAR;
496 1.1 haya break;
497 1.12 drochner case MII_ANER:
498 1.12 drochner rtk8139_reg = RTK_ANER;
499 1.12 drochner break;
500 1.1 haya case MII_ANLPAR:
501 1.10 tsutsui rtk8139_reg = RTK_LPAR;
502 1.1 haya break;
503 1.1 haya default:
504 1.1 haya #if 0
505 1.1 haya printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
506 1.1 haya #endif
507 1.1 haya return;
508 1.1 haya }
509 1.10 tsutsui CSR_WRITE_2(sc, rtk8139_reg, data);
510 1.1 haya return;
511 1.1 haya }
512 1.1 haya
513 1.34 thorpej memset((char *)&frame, 0, sizeof(frame));
514 1.1 haya
515 1.1 haya frame.mii_phyaddr = phy;
516 1.1 haya frame.mii_regaddr = reg;
517 1.1 haya frame.mii_data = data;
518 1.1 haya
519 1.8 thorpej rtk_mii_writereg(sc, &frame);
520 1.1 haya }
521 1.1 haya
522 1.1 haya STATIC void
523 1.64 christos rtk_phy_statchg(struct device *v __unused)
524 1.1 haya {
525 1.1 haya
526 1.1 haya /* Nothing to do. */
527 1.1 haya }
528 1.1 haya
529 1.8 thorpej #define rtk_calchash(addr) \
530 1.7 thorpej (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26)
531 1.1 haya
532 1.1 haya /*
533 1.1 haya * Program the 64-bit multicast hash filter.
534 1.1 haya */
535 1.50 jdolecek void
536 1.62 tsutsui rtk_setmulti(struct rtk_softc *sc)
537 1.1 haya {
538 1.63 tsutsui struct ifnet *ifp;
539 1.63 tsutsui uint32_t hashes[2] = { 0, 0 };
540 1.63 tsutsui uint32_t rxfilt;
541 1.1 haya struct ether_multi *enm;
542 1.1 haya struct ether_multistep step;
543 1.63 tsutsui int h, mcnt;
544 1.1 haya
545 1.1 haya ifp = &sc->ethercom.ec_if;
546 1.1 haya
547 1.10 tsutsui rxfilt = CSR_READ_4(sc, RTK_RXCFG);
548 1.1 haya
549 1.28 enami if (ifp->if_flags & IFF_PROMISC) {
550 1.63 tsutsui allmulti:
551 1.28 enami ifp->if_flags |= IFF_ALLMULTI;
552 1.10 tsutsui rxfilt |= RTK_RXCFG_RX_MULTI;
553 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
554 1.10 tsutsui CSR_WRITE_4(sc, RTK_MAR0, 0xFFFFFFFF);
555 1.10 tsutsui CSR_WRITE_4(sc, RTK_MAR4, 0xFFFFFFFF);
556 1.1 haya return;
557 1.1 haya }
558 1.1 haya
559 1.1 haya /* first, zot all the existing hash bits */
560 1.10 tsutsui CSR_WRITE_4(sc, RTK_MAR0, 0);
561 1.10 tsutsui CSR_WRITE_4(sc, RTK_MAR4, 0);
562 1.1 haya
563 1.1 haya /* now program new ones */
564 1.1 haya ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
565 1.63 tsutsui mcnt = 0;
566 1.1 haya while (enm != NULL) {
567 1.4 tsutsui if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
568 1.4 tsutsui ETHER_ADDR_LEN) != 0)
569 1.28 enami goto allmulti;
570 1.4 tsutsui
571 1.8 thorpej h = rtk_calchash(enm->enm_addrlo);
572 1.1 haya if (h < 32)
573 1.1 haya hashes[0] |= (1 << h);
574 1.1 haya else
575 1.1 haya hashes[1] |= (1 << (h - 32));
576 1.1 haya mcnt++;
577 1.1 haya ETHER_NEXT_MULTI(step, enm);
578 1.1 haya }
579 1.28 enami
580 1.28 enami ifp->if_flags &= ~IFF_ALLMULTI;
581 1.1 haya
582 1.1 haya if (mcnt)
583 1.10 tsutsui rxfilt |= RTK_RXCFG_RX_MULTI;
584 1.1 haya else
585 1.10 tsutsui rxfilt &= ~RTK_RXCFG_RX_MULTI;
586 1.1 haya
587 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, rxfilt);
588 1.10 tsutsui CSR_WRITE_4(sc, RTK_MAR0, hashes[0]);
589 1.10 tsutsui CSR_WRITE_4(sc, RTK_MAR4, hashes[1]);
590 1.1 haya }
591 1.1 haya
592 1.50 jdolecek void
593 1.62 tsutsui rtk_reset(struct rtk_softc *sc)
594 1.1 haya {
595 1.63 tsutsui int i;
596 1.1 haya
597 1.10 tsutsui CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_RESET);
598 1.1 haya
599 1.10 tsutsui for (i = 0; i < RTK_TIMEOUT; i++) {
600 1.1 haya DELAY(10);
601 1.23 tsutsui if ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_RESET) == 0)
602 1.1 haya break;
603 1.1 haya }
604 1.10 tsutsui if (i == RTK_TIMEOUT)
605 1.1 haya printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
606 1.1 haya }
607 1.1 haya
608 1.1 haya /*
609 1.1 haya * Attach the interface. Allocate softc structures, do ifmedia
610 1.1 haya * setup and ethernet/BPF attach.
611 1.1 haya */
612 1.1 haya void
613 1.62 tsutsui rtk_attach(struct rtk_softc *sc)
614 1.1 haya {
615 1.1 haya struct ifnet *ifp;
616 1.31 thorpej struct rtk_tx_desc *txd;
617 1.63 tsutsui uint16_t val;
618 1.63 tsutsui uint8_t eaddr[ETHER_ADDR_LEN];
619 1.10 tsutsui int error;
620 1.23 tsutsui int i, addr_len;
621 1.1 haya
622 1.8 thorpej callout_init(&sc->rtk_tick_ch);
623 1.1 haya
624 1.6 tsutsui /*
625 1.6 tsutsui * Check EEPROM type 9346 or 9356.
626 1.6 tsutsui */
627 1.10 tsutsui if (rtk_read_eeprom(sc, RTK_EE_ID, RTK_EEADDR_LEN1) == 0x8129)
628 1.10 tsutsui addr_len = RTK_EEADDR_LEN1;
629 1.6 tsutsui else
630 1.10 tsutsui addr_len = RTK_EEADDR_LEN0;
631 1.6 tsutsui
632 1.6 tsutsui /*
633 1.6 tsutsui * Get station address.
634 1.6 tsutsui */
635 1.10 tsutsui val = rtk_read_eeprom(sc, RTK_EE_EADDR0, addr_len);
636 1.6 tsutsui eaddr[0] = val & 0xff;
637 1.6 tsutsui eaddr[1] = val >> 8;
638 1.10 tsutsui val = rtk_read_eeprom(sc, RTK_EE_EADDR1, addr_len);
639 1.6 tsutsui eaddr[2] = val & 0xff;
640 1.6 tsutsui eaddr[3] = val >> 8;
641 1.10 tsutsui val = rtk_read_eeprom(sc, RTK_EE_EADDR2, addr_len);
642 1.6 tsutsui eaddr[4] = val & 0xff;
643 1.6 tsutsui eaddr[5] = val >> 8;
644 1.6 tsutsui
645 1.1 haya if ((error = bus_dmamem_alloc(sc->sc_dmat,
646 1.23 tsutsui RTK_RXBUFLEN + 16, PAGE_SIZE, 0, &sc->sc_dmaseg, 1, &sc->sc_dmanseg,
647 1.1 haya BUS_DMA_NOWAIT)) != 0) {
648 1.1 haya printf("%s: can't allocate recv buffer, error = %d\n",
649 1.61 tsutsui sc->sc_dev.dv_xname, error);
650 1.10 tsutsui goto fail_0;
651 1.1 haya }
652 1.1 haya
653 1.10 tsutsui if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg,
654 1.30 thorpej RTK_RXBUFLEN + 16, (caddr_t *)&sc->rtk_rx_buf,
655 1.1 haya BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
656 1.1 haya printf("%s: can't map recv buffer, error = %d\n",
657 1.61 tsutsui sc->sc_dev.dv_xname, error);
658 1.10 tsutsui goto fail_1;
659 1.1 haya }
660 1.1 haya
661 1.1 haya if ((error = bus_dmamap_create(sc->sc_dmat,
662 1.23 tsutsui RTK_RXBUFLEN + 16, 1, RTK_RXBUFLEN + 16, 0, BUS_DMA_NOWAIT,
663 1.1 haya &sc->recv_dmamap)) != 0) {
664 1.1 haya printf("%s: can't create recv buffer DMA map, error = %d\n",
665 1.61 tsutsui sc->sc_dev.dv_xname, error);
666 1.10 tsutsui goto fail_2;
667 1.1 haya }
668 1.1 haya
669 1.1 haya if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
670 1.30 thorpej sc->rtk_rx_buf, RTK_RXBUFLEN + 16,
671 1.35 thorpej NULL, BUS_DMA_READ|BUS_DMA_NOWAIT)) != 0) {
672 1.1 haya printf("%s: can't load recv buffer DMA map, error = %d\n",
673 1.61 tsutsui sc->sc_dev.dv_xname, error);
674 1.10 tsutsui goto fail_3;
675 1.1 haya }
676 1.1 haya
677 1.31 thorpej for (i = 0; i < RTK_TX_LIST_CNT; i++) {
678 1.31 thorpej txd = &sc->rtk_tx_descs[i];
679 1.4 tsutsui if ((error = bus_dmamap_create(sc->sc_dmat,
680 1.6 tsutsui MCLBYTES, 1, MCLBYTES, 0, BUS_DMA_NOWAIT,
681 1.31 thorpej &txd->txd_dmamap)) != 0) {
682 1.4 tsutsui printf("%s: can't create snd buffer DMA map,"
683 1.4 tsutsui " error = %d\n", sc->sc_dev.dv_xname, error);
684 1.10 tsutsui goto fail_4;
685 1.5 tsutsui }
686 1.31 thorpej txd->txd_txaddr = RTK_TXADDR0 + (i * 4);
687 1.31 thorpej txd->txd_txstat = RTK_TXSTAT0 + (i * 4);
688 1.31 thorpej }
689 1.31 thorpej SIMPLEQ_INIT(&sc->rtk_tx_free);
690 1.31 thorpej SIMPLEQ_INIT(&sc->rtk_tx_dirty);
691 1.31 thorpej
692 1.10 tsutsui /*
693 1.10 tsutsui * From this point forward, the attachment cannot fail. A failure
694 1.10 tsutsui * before this releases all resources thar may have been
695 1.10 tsutsui * allocated.
696 1.10 tsutsui */
697 1.10 tsutsui sc->sc_flags |= RTK_ATTACHED;
698 1.1 haya
699 1.6 tsutsui /* Reset the adapter. */
700 1.8 thorpej rtk_reset(sc);
701 1.6 tsutsui
702 1.23 tsutsui printf("%s: Ethernet address %s\n",
703 1.23 tsutsui sc->sc_dev.dv_xname, ether_sprintf(eaddr));
704 1.6 tsutsui
705 1.1 haya ifp = &sc->ethercom.ec_if;
706 1.1 haya ifp->if_softc = sc;
707 1.33 thorpej strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
708 1.1 haya ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
709 1.8 thorpej ifp->if_ioctl = rtk_ioctl;
710 1.8 thorpej ifp->if_start = rtk_start;
711 1.8 thorpej ifp->if_watchdog = rtk_watchdog;
712 1.15 thorpej ifp->if_init = rtk_init;
713 1.15 thorpej ifp->if_stop = rtk_stop;
714 1.25 thorpej IFQ_SET_READY(&ifp->if_snd);
715 1.1 haya
716 1.1 haya /*
717 1.1 haya * Do ifmedia setup.
718 1.1 haya */
719 1.1 haya sc->mii.mii_ifp = ifp;
720 1.8 thorpej sc->mii.mii_readreg = rtk_phy_readreg;
721 1.8 thorpej sc->mii.mii_writereg = rtk_phy_writereg;
722 1.8 thorpej sc->mii.mii_statchg = rtk_phy_statchg;
723 1.61 tsutsui ifmedia_init(&sc->mii.mii_media, IFM_IMASK, rtk_ifmedia_upd,
724 1.61 tsutsui rtk_ifmedia_sts);
725 1.51 perry mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff,
726 1.23 tsutsui MII_PHY_ANY, MII_OFFSET_ANY, 0);
727 1.1 haya
728 1.1 haya /* Choose a default media. */
729 1.1 haya if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
730 1.10 tsutsui ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
731 1.1 haya ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
732 1.1 haya } else {
733 1.1 haya ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
734 1.1 haya }
735 1.1 haya
736 1.1 haya /*
737 1.1 haya * Call MI attach routines.
738 1.1 haya */
739 1.1 haya if_attach(ifp);
740 1.1 haya ether_ifattach(ifp, eaddr);
741 1.1 haya
742 1.10 tsutsui /*
743 1.10 tsutsui * Make sure the interface is shutdown during reboot.
744 1.10 tsutsui */
745 1.10 tsutsui sc->sc_sdhook = shutdownhook_establish(rtk_shutdown, sc);
746 1.10 tsutsui if (sc->sc_sdhook == NULL)
747 1.37 kanaoka printf("%s: WARNING: unable to establish shutdown hook\n",
748 1.23 tsutsui sc->sc_dev.dv_xname);
749 1.10 tsutsui /*
750 1.10 tsutsui * Add a suspend hook to make sure we come back up after a
751 1.10 tsutsui * resume.
752 1.10 tsutsui */
753 1.53 jmcneill sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
754 1.53 jmcneill rtk_power, sc);
755 1.10 tsutsui if (sc->sc_powerhook == NULL)
756 1.10 tsutsui printf("%s: WARNING: unable to establish power hook\n",
757 1.23 tsutsui sc->sc_dev.dv_xname);
758 1.1 haya
759 1.48 dan
760 1.48 dan #if NRND > 0
761 1.48 dan rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
762 1.48 dan RND_TYPE_NET, 0);
763 1.48 dan #endif
764 1.48 dan
765 1.10 tsutsui return;
766 1.23 tsutsui fail_4:
767 1.31 thorpej for (i = 0; i < RTK_TX_LIST_CNT; i++) {
768 1.31 thorpej txd = &sc->rtk_tx_descs[i];
769 1.31 thorpej if (txd->txd_dmamap != NULL)
770 1.31 thorpej bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmamap);
771 1.31 thorpej }
772 1.23 tsutsui fail_3:
773 1.10 tsutsui bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
774 1.23 tsutsui fail_2:
775 1.30 thorpej bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_rx_buf,
776 1.23 tsutsui RTK_RXBUFLEN + 16);
777 1.23 tsutsui fail_1:
778 1.10 tsutsui bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
779 1.23 tsutsui fail_0:
780 1.1 haya return;
781 1.1 haya }
782 1.1 haya
783 1.1 haya /*
784 1.1 haya * Initialize the transmit descriptors.
785 1.1 haya */
786 1.50 jdolecek STATIC int
787 1.62 tsutsui rtk_list_tx_init(struct rtk_softc *sc)
788 1.1 haya {
789 1.31 thorpej struct rtk_tx_desc *txd;
790 1.31 thorpej int i;
791 1.31 thorpej
792 1.31 thorpej while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL)
793 1.41 lukem SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
794 1.31 thorpej while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_free)) != NULL)
795 1.41 lukem SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_free, txd_q);
796 1.1 haya
797 1.10 tsutsui for (i = 0; i < RTK_TX_LIST_CNT; i++) {
798 1.31 thorpej txd = &sc->rtk_tx_descs[i];
799 1.31 thorpej CSR_WRITE_4(sc, txd->txd_txaddr, 0);
800 1.31 thorpej SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_free, txd, txd_q);
801 1.1 haya }
802 1.1 haya
803 1.63 tsutsui return 0;
804 1.1 haya }
805 1.1 haya
806 1.1 haya /*
807 1.10 tsutsui * rtk_activate:
808 1.10 tsutsui * Handle device activation/deactivation requests.
809 1.10 tsutsui */
810 1.10 tsutsui int
811 1.62 tsutsui rtk_activate(struct device *self, enum devact act)
812 1.10 tsutsui {
813 1.63 tsutsui struct rtk_softc *sc = (void *)self;
814 1.63 tsutsui int s, error;
815 1.23 tsutsui
816 1.63 tsutsui error = 0;
817 1.10 tsutsui s = splnet();
818 1.10 tsutsui switch (act) {
819 1.10 tsutsui case DVACT_ACTIVATE:
820 1.10 tsutsui error = EOPNOTSUPP;
821 1.10 tsutsui break;
822 1.10 tsutsui case DVACT_DEACTIVATE:
823 1.10 tsutsui mii_activate(&sc->mii, act, MII_PHY_ANY, MII_OFFSET_ANY);
824 1.10 tsutsui if_deactivate(&sc->ethercom.ec_if);
825 1.10 tsutsui break;
826 1.10 tsutsui }
827 1.10 tsutsui splx(s);
828 1.10 tsutsui
829 1.63 tsutsui return error;
830 1.10 tsutsui }
831 1.10 tsutsui
832 1.10 tsutsui /*
833 1.10 tsutsui * rtk_detach:
834 1.10 tsutsui * Detach a rtk interface.
835 1.10 tsutsui */
836 1.51 perry int
837 1.62 tsutsui rtk_detach(struct rtk_softc *sc)
838 1.10 tsutsui {
839 1.10 tsutsui struct ifnet *ifp = &sc->ethercom.ec_if;
840 1.31 thorpej struct rtk_tx_desc *txd;
841 1.10 tsutsui int i;
842 1.10 tsutsui
843 1.10 tsutsui /*
844 1.39 wiz * Succeed now if there isn't any work to do.
845 1.10 tsutsui */
846 1.10 tsutsui if ((sc->sc_flags & RTK_ATTACHED) == 0)
847 1.63 tsutsui return 0;
848 1.23 tsutsui
849 1.10 tsutsui /* Unhook our tick handler. */
850 1.10 tsutsui callout_stop(&sc->rtk_tick_ch);
851 1.10 tsutsui
852 1.10 tsutsui /* Detach all PHYs. */
853 1.10 tsutsui mii_detach(&sc->mii, MII_PHY_ANY, MII_OFFSET_ANY);
854 1.10 tsutsui
855 1.10 tsutsui /* Delete all remaining media. */
856 1.10 tsutsui ifmedia_delete_instance(&sc->mii.mii_media, IFM_INST_ANY);
857 1.10 tsutsui
858 1.48 dan #if NRND > 0
859 1.48 dan rnd_detach_source(&sc->rnd_source);
860 1.48 dan #endif
861 1.48 dan
862 1.10 tsutsui ether_ifdetach(ifp);
863 1.10 tsutsui if_detach(ifp);
864 1.10 tsutsui
865 1.31 thorpej for (i = 0; i < RTK_TX_LIST_CNT; i++) {
866 1.31 thorpej txd = &sc->rtk_tx_descs[i];
867 1.31 thorpej if (txd->txd_dmamap != NULL)
868 1.31 thorpej bus_dmamap_destroy(sc->sc_dmat, txd->txd_dmamap);
869 1.31 thorpej }
870 1.10 tsutsui bus_dmamap_destroy(sc->sc_dmat, sc->recv_dmamap);
871 1.30 thorpej bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->rtk_rx_buf,
872 1.23 tsutsui RTK_RXBUFLEN + 16);
873 1.24 tsutsui bus_dmamem_free(sc->sc_dmat, &sc->sc_dmaseg, sc->sc_dmanseg);
874 1.10 tsutsui
875 1.10 tsutsui shutdownhook_disestablish(sc->sc_sdhook);
876 1.10 tsutsui powerhook_disestablish(sc->sc_powerhook);
877 1.23 tsutsui
878 1.63 tsutsui return 0;
879 1.10 tsutsui }
880 1.10 tsutsui
881 1.10 tsutsui /*
882 1.10 tsutsui * rtk_enable:
883 1.10 tsutsui * Enable the RTL81X9 chip.
884 1.10 tsutsui */
885 1.51 perry int
886 1.62 tsutsui rtk_enable(struct rtk_softc *sc)
887 1.10 tsutsui {
888 1.23 tsutsui
889 1.10 tsutsui if (RTK_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
890 1.10 tsutsui if ((*sc->sc_enable)(sc) != 0) {
891 1.10 tsutsui printf("%s: device enable failed\n",
892 1.23 tsutsui sc->sc_dev.dv_xname);
893 1.63 tsutsui return EIO;
894 1.10 tsutsui }
895 1.10 tsutsui sc->sc_flags |= RTK_ENABLED;
896 1.10 tsutsui }
897 1.63 tsutsui return 0;
898 1.10 tsutsui }
899 1.10 tsutsui
900 1.10 tsutsui /*
901 1.10 tsutsui * rtk_disable:
902 1.10 tsutsui * Disable the RTL81X9 chip.
903 1.10 tsutsui */
904 1.51 perry void
905 1.62 tsutsui rtk_disable(struct rtk_softc *sc)
906 1.10 tsutsui {
907 1.23 tsutsui
908 1.10 tsutsui if (RTK_IS_ENABLED(sc) && sc->sc_disable != NULL) {
909 1.10 tsutsui (*sc->sc_disable)(sc);
910 1.10 tsutsui sc->sc_flags &= ~RTK_ENABLED;
911 1.10 tsutsui }
912 1.10 tsutsui }
913 1.10 tsutsui
914 1.10 tsutsui /*
915 1.10 tsutsui * rtk_power:
916 1.10 tsutsui * Power management (suspend/resume) hook.
917 1.10 tsutsui */
918 1.51 perry void
919 1.62 tsutsui rtk_power(int why, void *arg)
920 1.10 tsutsui {
921 1.63 tsutsui struct rtk_softc *sc = (void *)arg;
922 1.10 tsutsui struct ifnet *ifp = &sc->ethercom.ec_if;
923 1.10 tsutsui int s;
924 1.10 tsutsui
925 1.10 tsutsui s = splnet();
926 1.19 takemura switch (why) {
927 1.19 takemura case PWR_SUSPEND:
928 1.19 takemura case PWR_STANDBY:
929 1.15 thorpej rtk_stop(ifp, 0);
930 1.10 tsutsui if (sc->sc_power != NULL)
931 1.10 tsutsui (*sc->sc_power)(sc, why);
932 1.19 takemura break;
933 1.19 takemura case PWR_RESUME:
934 1.19 takemura if (ifp->if_flags & IFF_UP) {
935 1.19 takemura if (sc->sc_power != NULL)
936 1.19 takemura (*sc->sc_power)(sc, why);
937 1.19 takemura rtk_init(ifp);
938 1.19 takemura }
939 1.19 takemura break;
940 1.19 takemura case PWR_SOFTSUSPEND:
941 1.19 takemura case PWR_SOFTSTANDBY:
942 1.19 takemura case PWR_SOFTRESUME:
943 1.19 takemura break;
944 1.10 tsutsui }
945 1.10 tsutsui splx(s);
946 1.10 tsutsui }
947 1.10 tsutsui
948 1.10 tsutsui /*
949 1.1 haya * A frame has been uploaded: pass the resulting mbuf chain up to
950 1.1 haya * the higher level protocols.
951 1.1 haya *
952 1.22 tsutsui * You know there's something wrong with a PCI bus-master chip design.
953 1.1 haya *
954 1.1 haya * The receive operation is badly documented in the datasheet, so I'll
955 1.1 haya * attempt to document it here. The driver provides a buffer area and
956 1.1 haya * places its base address in the RX buffer start address register.
957 1.1 haya * The chip then begins copying frames into the RX buffer. Each frame
958 1.39 wiz * is preceded by a 32-bit RX status word which specifies the length
959 1.1 haya * of the frame and certain other status bits. Each frame (starting with
960 1.1 haya * the status word) is also 32-bit aligned. The frame length is in the
961 1.1 haya * first 16 bits of the status word; the lower 15 bits correspond with
962 1.1 haya * the 'rx status register' mentioned in the datasheet.
963 1.1 haya *
964 1.1 haya * Note: to make the Alpha happy, the frame payload needs to be aligned
965 1.22 tsutsui * on a 32-bit boundary. To achieve this, we copy the data to mbuf
966 1.22 tsutsui * shifted forward 2 bytes.
967 1.1 haya */
968 1.50 jdolecek STATIC void
969 1.62 tsutsui rtk_rxeof(struct rtk_softc *sc)
970 1.1 haya {
971 1.63 tsutsui struct mbuf *m;
972 1.63 tsutsui struct ifnet *ifp;
973 1.63 tsutsui caddr_t rxbufpos, dst;
974 1.63 tsutsui u_int total_len, wrap;
975 1.63 tsutsui uint32_t rxstat;
976 1.63 tsutsui uint16_t cur_rx, new_rx;
977 1.63 tsutsui uint16_t limit;
978 1.63 tsutsui uint16_t rx_bytes, max_bytes;
979 1.1 haya
980 1.1 haya ifp = &sc->ethercom.ec_if;
981 1.1 haya
982 1.10 tsutsui cur_rx = (CSR_READ_2(sc, RTK_CURRXADDR) + 16) % RTK_RXBUFLEN;
983 1.1 haya
984 1.1 haya /* Do not try to read past this point. */
985 1.10 tsutsui limit = CSR_READ_2(sc, RTK_CURRXBUF) % RTK_RXBUFLEN;
986 1.1 haya
987 1.1 haya if (limit < cur_rx)
988 1.10 tsutsui max_bytes = (RTK_RXBUFLEN - cur_rx) + limit;
989 1.1 haya else
990 1.1 haya max_bytes = limit - cur_rx;
991 1.63 tsutsui rx_bytes = 0;
992 1.1 haya
993 1.63 tsutsui while ((CSR_READ_1(sc, RTK_COMMAND) & RTK_CMD_EMPTY_RXBUF) == 0) {
994 1.30 thorpej rxbufpos = sc->rtk_rx_buf + cur_rx;
995 1.4 tsutsui bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
996 1.21 tsutsui RTK_RXSTAT_LEN, BUS_DMASYNC_POSTREAD);
997 1.63 tsutsui rxstat = le32toh(*(uint32_t *)rxbufpos);
998 1.4 tsutsui bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, cur_rx,
999 1.21 tsutsui RTK_RXSTAT_LEN, BUS_DMASYNC_PREREAD);
1000 1.1 haya
1001 1.1 haya /*
1002 1.1 haya * Here's a totally undocumented fact for you. When the
1003 1.1 haya * RealTek chip is in the process of copying a packet into
1004 1.1 haya * RAM for you, the length will be 0xfff0. If you spot a
1005 1.1 haya * packet header with this value, you need to stop. The
1006 1.1 haya * datasheet makes absolutely no mention of this and
1007 1.1 haya * RealTek should be shot for this.
1008 1.1 haya */
1009 1.22 tsutsui total_len = rxstat >> 16;
1010 1.22 tsutsui if (total_len == RTK_RXSTAT_UNFINISHED)
1011 1.1 haya break;
1012 1.22 tsutsui
1013 1.27 tsutsui if ((rxstat & RTK_RXSTAT_RXOK) == 0 ||
1014 1.54 tsutsui total_len < ETHER_MIN_LEN ||
1015 1.27 tsutsui total_len > ETHER_MAX_LEN) {
1016 1.1 haya ifp->if_ierrors++;
1017 1.1 haya
1018 1.1 haya /*
1019 1.51 perry * submitted by:[netbsd-pcmcia:00484]
1020 1.1 haya * Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
1021 1.1 haya * obtain from:
1022 1.1 haya * FreeBSD if_rl.c rev 1.24->1.25
1023 1.1 haya *
1024 1.1 haya */
1025 1.1 haya #if 0
1026 1.10 tsutsui if (rxstat & (RTK_RXSTAT_BADSYM|RTK_RXSTAT_RUNT|
1027 1.21 tsutsui RTK_RXSTAT_GIANT|RTK_RXSTAT_CRCERR|
1028 1.21 tsutsui RTK_RXSTAT_ALIGNERR)) {
1029 1.10 tsutsui CSR_WRITE_2(sc, RTK_COMMAND, RTK_CMD_TX_ENB);
1030 1.21 tsutsui CSR_WRITE_2(sc, RTK_COMMAND,
1031 1.21 tsutsui RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1032 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1033 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXADDR,
1034 1.21 tsutsui sc->recv_dmamap->dm_segs[0].ds_addr);
1035 1.1 haya cur_rx = 0;
1036 1.1 haya }
1037 1.1 haya break;
1038 1.1 haya #else
1039 1.15 thorpej rtk_init(ifp);
1040 1.1 haya return;
1041 1.1 haya #endif
1042 1.1 haya }
1043 1.1 haya
1044 1.51 perry /* No errors; receive the packet. */
1045 1.21 tsutsui rx_bytes += total_len + RTK_RXSTAT_LEN;
1046 1.1 haya
1047 1.1 haya /*
1048 1.1 haya * Avoid trying to read more bytes than we know
1049 1.1 haya * the chip has prepared for us.
1050 1.1 haya */
1051 1.1 haya if (rx_bytes > max_bytes)
1052 1.1 haya break;
1053 1.1 haya
1054 1.22 tsutsui /*
1055 1.22 tsutsui * Skip the status word, wrapping around to the beginning
1056 1.22 tsutsui * of the Rx area, if necessary.
1057 1.22 tsutsui */
1058 1.29 thorpej cur_rx = (cur_rx + RTK_RXSTAT_LEN) % RTK_RXBUFLEN;
1059 1.30 thorpej rxbufpos = sc->rtk_rx_buf + cur_rx;
1060 1.4 tsutsui
1061 1.22 tsutsui /*
1062 1.22 tsutsui * Compute the number of bytes at which the packet
1063 1.22 tsutsui * will wrap to the beginning of the ring buffer.
1064 1.22 tsutsui */
1065 1.29 thorpej wrap = RTK_RXBUFLEN - cur_rx;
1066 1.1 haya
1067 1.22 tsutsui /*
1068 1.22 tsutsui * Compute where the next pending packet is.
1069 1.22 tsutsui */
1070 1.22 tsutsui if (total_len > wrap)
1071 1.22 tsutsui new_rx = total_len - wrap;
1072 1.22 tsutsui else
1073 1.22 tsutsui new_rx = cur_rx + total_len;
1074 1.22 tsutsui /* Round up to 32-bit boundary. */
1075 1.57 tsutsui new_rx = ((new_rx + 3) & ~3) % RTK_RXBUFLEN;
1076 1.1 haya
1077 1.22 tsutsui /*
1078 1.54 tsutsui * The RealTek chip includes the CRC with every
1079 1.54 tsutsui * incoming packet; trim it off here.
1080 1.54 tsutsui */
1081 1.54 tsutsui total_len -= ETHER_CRC_LEN;
1082 1.54 tsutsui
1083 1.54 tsutsui /*
1084 1.22 tsutsui * Now allocate an mbuf (and possibly a cluster) to hold
1085 1.22 tsutsui * the packet. Note we offset the packet 2 bytes so that
1086 1.22 tsutsui * data after the Ethernet header will be 4-byte aligned.
1087 1.22 tsutsui */
1088 1.22 tsutsui MGETHDR(m, M_DONTWAIT, MT_DATA);
1089 1.22 tsutsui if (m == NULL) {
1090 1.22 tsutsui printf("%s: unable to allocate Rx mbuf\n",
1091 1.22 tsutsui sc->sc_dev.dv_xname);
1092 1.22 tsutsui ifp->if_ierrors++;
1093 1.22 tsutsui goto next_packet;
1094 1.22 tsutsui }
1095 1.22 tsutsui if (total_len > (MHLEN - RTK_ETHER_ALIGN)) {
1096 1.22 tsutsui MCLGET(m, M_DONTWAIT);
1097 1.22 tsutsui if ((m->m_flags & M_EXT) == 0) {
1098 1.22 tsutsui printf("%s: unable to allocate Rx cluster\n",
1099 1.22 tsutsui sc->sc_dev.dv_xname);
1100 1.22 tsutsui ifp->if_ierrors++;
1101 1.22 tsutsui m_freem(m);
1102 1.22 tsutsui m = NULL;
1103 1.22 tsutsui goto next_packet;
1104 1.22 tsutsui }
1105 1.22 tsutsui }
1106 1.22 tsutsui m->m_data += RTK_ETHER_ALIGN; /* for alignment */
1107 1.22 tsutsui m->m_pkthdr.rcvif = ifp;
1108 1.22 tsutsui m->m_pkthdr.len = m->m_len = total_len;
1109 1.22 tsutsui dst = mtod(m, caddr_t);
1110 1.1 haya
1111 1.22 tsutsui /*
1112 1.22 tsutsui * If the packet wraps, copy up to the wrapping point.
1113 1.22 tsutsui */
1114 1.1 haya if (total_len > wrap) {
1115 1.22 tsutsui bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1116 1.22 tsutsui cur_rx, wrap, BUS_DMASYNC_POSTREAD);
1117 1.22 tsutsui memcpy(dst, rxbufpos, wrap);
1118 1.22 tsutsui bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1119 1.22 tsutsui cur_rx, wrap, BUS_DMASYNC_PREREAD);
1120 1.22 tsutsui cur_rx = 0;
1121 1.30 thorpej rxbufpos = sc->rtk_rx_buf;
1122 1.22 tsutsui total_len -= wrap;
1123 1.22 tsutsui dst += wrap;
1124 1.1 haya }
1125 1.1 haya
1126 1.1 haya /*
1127 1.22 tsutsui * ...and now the rest.
1128 1.1 haya */
1129 1.22 tsutsui bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1130 1.22 tsutsui cur_rx, total_len, BUS_DMASYNC_POSTREAD);
1131 1.22 tsutsui memcpy(dst, rxbufpos, total_len);
1132 1.22 tsutsui bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap,
1133 1.22 tsutsui cur_rx, total_len, BUS_DMASYNC_PREREAD);
1134 1.22 tsutsui
1135 1.23 tsutsui next_packet:
1136 1.57 tsutsui CSR_WRITE_2(sc, RTK_CURRXADDR, (new_rx - 16) % RTK_RXBUFLEN);
1137 1.22 tsutsui cur_rx = new_rx;
1138 1.1 haya
1139 1.1 haya if (m == NULL)
1140 1.1 haya continue;
1141 1.16 thorpej
1142 1.1 haya ifp->if_ipackets++;
1143 1.1 haya
1144 1.1 haya #if NBPFILTER > 0
1145 1.14 thorpej if (ifp->if_bpf)
1146 1.1 haya bpf_mtap(ifp->if_bpf, m);
1147 1.1 haya #endif
1148 1.1 haya /* pass it on. */
1149 1.1 haya (*ifp->if_input)(ifp, m);
1150 1.1 haya }
1151 1.1 haya }
1152 1.1 haya
1153 1.1 haya /*
1154 1.1 haya * A frame was downloaded to the chip. It's safe for us to clean up
1155 1.1 haya * the list buffers.
1156 1.1 haya */
1157 1.50 jdolecek STATIC void
1158 1.62 tsutsui rtk_txeof(struct rtk_softc *sc)
1159 1.1 haya {
1160 1.31 thorpej struct ifnet *ifp;
1161 1.31 thorpej struct rtk_tx_desc *txd;
1162 1.63 tsutsui uint32_t txstat;
1163 1.1 haya
1164 1.1 haya ifp = &sc->ethercom.ec_if;
1165 1.1 haya
1166 1.1 haya /*
1167 1.1 haya * Go through our tx list and free mbufs for those
1168 1.1 haya * frames that have been uploaded.
1169 1.1 haya */
1170 1.31 thorpej while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL) {
1171 1.31 thorpej txstat = CSR_READ_4(sc, txd->txd_txstat);
1172 1.23 tsutsui if ((txstat & (RTK_TXSTAT_TX_OK|
1173 1.23 tsutsui RTK_TXSTAT_TX_UNDERRUN|RTK_TXSTAT_TXABRT)) == 0)
1174 1.1 haya break;
1175 1.1 haya
1176 1.41 lukem SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
1177 1.31 thorpej
1178 1.31 thorpej bus_dmamap_sync(sc->sc_dmat, txd->txd_dmamap, 0,
1179 1.31 thorpej txd->txd_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1180 1.31 thorpej bus_dmamap_unload(sc->sc_dmat, txd->txd_dmamap);
1181 1.31 thorpej m_freem(txd->txd_mbuf);
1182 1.31 thorpej txd->txd_mbuf = NULL;
1183 1.4 tsutsui
1184 1.10 tsutsui ifp->if_collisions += (txstat & RTK_TXSTAT_COLLCNT) >> 24;
1185 1.1 haya
1186 1.10 tsutsui if (txstat & RTK_TXSTAT_TX_OK)
1187 1.1 haya ifp->if_opackets++;
1188 1.1 haya else {
1189 1.1 haya ifp->if_oerrors++;
1190 1.36 kanaoka
1191 1.36 kanaoka /*
1192 1.36 kanaoka * Increase Early TX threshold if underrun occurred.
1193 1.36 kanaoka * Increase step 64 bytes.
1194 1.36 kanaoka */
1195 1.36 kanaoka if (txstat & RTK_TXSTAT_TX_UNDERRUN) {
1196 1.52 xtraeme #ifdef DEBUG
1197 1.36 kanaoka printf("%s: transmit underrun;",
1198 1.36 kanaoka sc->sc_dev.dv_xname);
1199 1.52 xtraeme #endif
1200 1.65 tsutsui if (sc->sc_txthresh < RTK_TXTH_MAX) {
1201 1.36 kanaoka sc->sc_txthresh += 2;
1202 1.52 xtraeme #ifdef DEBUG
1203 1.36 kanaoka printf(" new threshold: %d bytes",
1204 1.36 kanaoka sc->sc_txthresh * 32);
1205 1.52 xtraeme #endif
1206 1.36 kanaoka }
1207 1.36 kanaoka printf("\n");
1208 1.36 kanaoka }
1209 1.23 tsutsui if (txstat & (RTK_TXSTAT_TXABRT|RTK_TXSTAT_OUTOFWIN))
1210 1.10 tsutsui CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1211 1.1 haya }
1212 1.31 thorpej SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_free, txd, txd_q);
1213 1.1 haya ifp->if_flags &= ~IFF_OACTIVE;
1214 1.31 thorpej }
1215 1.55 tsutsui
1216 1.55 tsutsui /* Clear the timeout timer if there is no pending packet. */
1217 1.58 tsutsui if (SIMPLEQ_EMPTY(&sc->rtk_tx_dirty))
1218 1.55 tsutsui ifp->if_timer = 0;
1219 1.55 tsutsui
1220 1.1 haya }
1221 1.1 haya
1222 1.50 jdolecek int
1223 1.62 tsutsui rtk_intr(void *arg)
1224 1.1 haya {
1225 1.63 tsutsui struct rtk_softc *sc;
1226 1.63 tsutsui struct ifnet *ifp;
1227 1.63 tsutsui uint16_t status;
1228 1.63 tsutsui int handled;
1229 1.1 haya
1230 1.1 haya sc = arg;
1231 1.1 haya ifp = &sc->ethercom.ec_if;
1232 1.1 haya
1233 1.1 haya /* Disable interrupts. */
1234 1.10 tsutsui CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1235 1.1 haya
1236 1.63 tsutsui handled = 0;
1237 1.1 haya for (;;) {
1238 1.1 haya
1239 1.10 tsutsui status = CSR_READ_2(sc, RTK_ISR);
1240 1.1 haya if (status)
1241 1.10 tsutsui CSR_WRITE_2(sc, RTK_ISR, status);
1242 1.1 haya
1243 1.10 tsutsui if ((status & RTK_INTRS) == 0)
1244 1.1 haya break;
1245 1.1 haya
1246 1.59 tsutsui handled = 1;
1247 1.59 tsutsui
1248 1.10 tsutsui if (status & RTK_ISR_RX_OK)
1249 1.8 thorpej rtk_rxeof(sc);
1250 1.1 haya
1251 1.10 tsutsui if (status & RTK_ISR_RX_ERR)
1252 1.8 thorpej rtk_rxeof(sc);
1253 1.1 haya
1254 1.23 tsutsui if (status & (RTK_ISR_TX_OK|RTK_ISR_TX_ERR))
1255 1.8 thorpej rtk_txeof(sc);
1256 1.1 haya
1257 1.10 tsutsui if (status & RTK_ISR_SYSTEM_ERR) {
1258 1.8 thorpej rtk_reset(sc);
1259 1.15 thorpej rtk_init(ifp);
1260 1.1 haya }
1261 1.1 haya }
1262 1.1 haya
1263 1.1 haya /* Re-enable interrupts. */
1264 1.10 tsutsui CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1265 1.1 haya
1266 1.25 thorpej if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1267 1.8 thorpej rtk_start(ifp);
1268 1.1 haya
1269 1.48 dan #if NRND > 0
1270 1.48 dan if (RND_ENABLED(&sc->rnd_source))
1271 1.48 dan rnd_add_uint32(&sc->rnd_source, status);
1272 1.48 dan #endif
1273 1.48 dan
1274 1.63 tsutsui return handled;
1275 1.1 haya }
1276 1.1 haya
1277 1.1 haya /*
1278 1.1 haya * Main transmit routine.
1279 1.1 haya */
1280 1.1 haya
1281 1.50 jdolecek STATIC void
1282 1.62 tsutsui rtk_start(struct ifnet *ifp)
1283 1.1 haya {
1284 1.31 thorpej struct rtk_softc *sc;
1285 1.31 thorpej struct rtk_tx_desc *txd;
1286 1.63 tsutsui struct mbuf *m_head, *m_new;
1287 1.31 thorpej int error, len;
1288 1.1 haya
1289 1.1 haya sc = ifp->if_softc;
1290 1.1 haya
1291 1.31 thorpej while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_free)) != NULL) {
1292 1.25 thorpej IFQ_POLL(&ifp->if_snd, m_head);
1293 1.1 haya if (m_head == NULL)
1294 1.1 haya break;
1295 1.26 thorpej m_new = NULL;
1296 1.1 haya
1297 1.4 tsutsui /*
1298 1.4 tsutsui * Load the DMA map. If this fails, the packet didn't
1299 1.4 tsutsui * fit in one DMA segment, and we need to copy. Note,
1300 1.4 tsutsui * the packet must also be aligned.
1301 1.44 bouyer * if the packet is too small, copy it too, so we're sure
1302 1.44 bouyer * so have enouth room for the pad buffer.
1303 1.4 tsutsui */
1304 1.38 mrg if ((mtod(m_head, uintptr_t) & 3) != 0 ||
1305 1.44 bouyer m_head->m_pkthdr.len < ETHER_PAD_LEN ||
1306 1.31 thorpej bus_dmamap_load_mbuf(sc->sc_dmat, txd->txd_dmamap,
1307 1.35 thorpej m_head, BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
1308 1.4 tsutsui MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1309 1.4 tsutsui if (m_new == NULL) {
1310 1.4 tsutsui printf("%s: unable to allocate Tx mbuf\n",
1311 1.4 tsutsui sc->sc_dev.dv_xname);
1312 1.4 tsutsui break;
1313 1.4 tsutsui }
1314 1.4 tsutsui if (m_head->m_pkthdr.len > MHLEN) {
1315 1.4 tsutsui MCLGET(m_new, M_DONTWAIT);
1316 1.4 tsutsui if ((m_new->m_flags & M_EXT) == 0) {
1317 1.4 tsutsui printf("%s: unable to allocate Tx "
1318 1.4 tsutsui "cluster\n", sc->sc_dev.dv_xname);
1319 1.4 tsutsui m_freem(m_new);
1320 1.4 tsutsui break;
1321 1.4 tsutsui }
1322 1.4 tsutsui }
1323 1.4 tsutsui m_copydata(m_head, 0, m_head->m_pkthdr.len,
1324 1.4 tsutsui mtod(m_new, caddr_t));
1325 1.4 tsutsui m_new->m_pkthdr.len = m_new->m_len =
1326 1.4 tsutsui m_head->m_pkthdr.len;
1327 1.44 bouyer if (m_head->m_pkthdr.len < ETHER_PAD_LEN) {
1328 1.44 bouyer memset(
1329 1.44 bouyer mtod(m_new, caddr_t) + m_head->m_pkthdr.len,
1330 1.44 bouyer 0, ETHER_PAD_LEN - m_head->m_pkthdr.len);
1331 1.44 bouyer m_new->m_pkthdr.len = m_new->m_len =
1332 1.44 bouyer ETHER_PAD_LEN;
1333 1.44 bouyer }
1334 1.4 tsutsui error = bus_dmamap_load_mbuf(sc->sc_dmat,
1335 1.35 thorpej txd->txd_dmamap, m_new,
1336 1.35 thorpej BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1337 1.4 tsutsui if (error) {
1338 1.4 tsutsui printf("%s: unable to load Tx buffer, "
1339 1.4 tsutsui "error = %d\n", sc->sc_dev.dv_xname, error);
1340 1.4 tsutsui break;
1341 1.4 tsutsui }
1342 1.4 tsutsui }
1343 1.25 thorpej IFQ_DEQUEUE(&ifp->if_snd, m_head);
1344 1.44 bouyer #if NBPFILTER > 0
1345 1.44 bouyer /*
1346 1.44 bouyer * If there's a BPF listener, bounce a copy of this frame
1347 1.44 bouyer * to him.
1348 1.44 bouyer */
1349 1.44 bouyer if (ifp->if_bpf)
1350 1.44 bouyer bpf_mtap(ifp->if_bpf, m_head);
1351 1.44 bouyer #endif
1352 1.26 thorpej if (m_new != NULL) {
1353 1.26 thorpej m_freem(m_head);
1354 1.26 thorpej m_head = m_new;
1355 1.26 thorpej }
1356 1.31 thorpej txd->txd_mbuf = m_head;
1357 1.4 tsutsui
1358 1.41 lukem SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_free, txd_q);
1359 1.31 thorpej SIMPLEQ_INSERT_TAIL(&sc->rtk_tx_dirty, txd, txd_q);
1360 1.1 haya
1361 1.1 haya /*
1362 1.1 haya * Transmit the frame.
1363 1.61 tsutsui */
1364 1.4 tsutsui bus_dmamap_sync(sc->sc_dmat,
1365 1.31 thorpej txd->txd_dmamap, 0, txd->txd_dmamap->dm_mapsize,
1366 1.4 tsutsui BUS_DMASYNC_PREWRITE);
1367 1.4 tsutsui
1368 1.31 thorpej len = txd->txd_dmamap->dm_segs[0].ds_len;
1369 1.4 tsutsui
1370 1.31 thorpej CSR_WRITE_4(sc, txd->txd_txaddr,
1371 1.31 thorpej txd->txd_dmamap->dm_segs[0].ds_addr);
1372 1.65 tsutsui CSR_WRITE_4(sc, txd->txd_txstat,
1373 1.65 tsutsui RTK_TXSTAT_THRESH(sc->sc_txthresh) | len);
1374 1.60 tsutsui
1375 1.60 tsutsui /*
1376 1.60 tsutsui * Set a timeout in case the chip goes out to lunch.
1377 1.60 tsutsui */
1378 1.60 tsutsui ifp->if_timer = 5;
1379 1.1 haya }
1380 1.1 haya
1381 1.1 haya /*
1382 1.1 haya * We broke out of the loop because all our TX slots are
1383 1.1 haya * full. Mark the NIC as busy until it drains some of the
1384 1.1 haya * packets from the queue.
1385 1.1 haya */
1386 1.41 lukem if (SIMPLEQ_EMPTY(&sc->rtk_tx_free))
1387 1.1 haya ifp->if_flags |= IFF_OACTIVE;
1388 1.1 haya }
1389 1.1 haya
1390 1.50 jdolecek STATIC int
1391 1.62 tsutsui rtk_init(struct ifnet *ifp)
1392 1.1 haya {
1393 1.63 tsutsui struct rtk_softc *sc = ifp->if_softc;
1394 1.63 tsutsui int error, i;
1395 1.63 tsutsui uint32_t rxcfg;
1396 1.1 haya
1397 1.15 thorpej if ((error = rtk_enable(sc)) != 0)
1398 1.15 thorpej goto out;
1399 1.1 haya
1400 1.1 haya /*
1401 1.15 thorpej * Cancel pending I/O.
1402 1.1 haya */
1403 1.15 thorpej rtk_stop(ifp, 0);
1404 1.1 haya
1405 1.1 haya /* Init our MAC address */
1406 1.1 haya for (i = 0; i < ETHER_ADDR_LEN; i++) {
1407 1.10 tsutsui CSR_WRITE_1(sc, RTK_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
1408 1.1 haya }
1409 1.1 haya
1410 1.1 haya /* Init the RX buffer pointer register. */
1411 1.4 tsutsui bus_dmamap_sync(sc->sc_dmat, sc->recv_dmamap, 0,
1412 1.4 tsutsui sc->recv_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
1413 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
1414 1.1 haya
1415 1.1 haya /* Init TX descriptors. */
1416 1.8 thorpej rtk_list_tx_init(sc);
1417 1.1 haya
1418 1.36 kanaoka /* Init Early TX threshold. */
1419 1.65 tsutsui sc->sc_txthresh = RTK_TXTH_256;
1420 1.1 haya /*
1421 1.1 haya * Enable transmit and receive.
1422 1.1 haya */
1423 1.10 tsutsui CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1424 1.1 haya
1425 1.1 haya /*
1426 1.1 haya * Set the initial TX and RX configuration.
1427 1.1 haya */
1428 1.10 tsutsui CSR_WRITE_4(sc, RTK_TXCFG, RTK_TXCFG_CONFIG);
1429 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, RTK_RXCFG_CONFIG);
1430 1.1 haya
1431 1.1 haya /* Set the individual bit to receive frames for this host only. */
1432 1.10 tsutsui rxcfg = CSR_READ_4(sc, RTK_RXCFG);
1433 1.10 tsutsui rxcfg |= RTK_RXCFG_RX_INDIV;
1434 1.1 haya
1435 1.1 haya /* If we want promiscuous mode, set the allframes bit. */
1436 1.1 haya if (ifp->if_flags & IFF_PROMISC) {
1437 1.10 tsutsui rxcfg |= RTK_RXCFG_RX_ALLPHYS;
1438 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1439 1.1 haya } else {
1440 1.10 tsutsui rxcfg &= ~RTK_RXCFG_RX_ALLPHYS;
1441 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1442 1.1 haya }
1443 1.1 haya
1444 1.1 haya /*
1445 1.1 haya * Set capture broadcast bit to capture broadcast frames.
1446 1.1 haya */
1447 1.1 haya if (ifp->if_flags & IFF_BROADCAST) {
1448 1.10 tsutsui rxcfg |= RTK_RXCFG_RX_BROAD;
1449 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1450 1.1 haya } else {
1451 1.10 tsutsui rxcfg &= ~RTK_RXCFG_RX_BROAD;
1452 1.10 tsutsui CSR_WRITE_4(sc, RTK_RXCFG, rxcfg);
1453 1.1 haya }
1454 1.1 haya
1455 1.1 haya /*
1456 1.1 haya * Program the multicast filter, if necessary.
1457 1.1 haya */
1458 1.8 thorpej rtk_setmulti(sc);
1459 1.1 haya
1460 1.1 haya /*
1461 1.1 haya * Enable interrupts.
1462 1.1 haya */
1463 1.10 tsutsui CSR_WRITE_2(sc, RTK_IMR, RTK_INTRS);
1464 1.1 haya
1465 1.1 haya /* Start RX/TX process. */
1466 1.10 tsutsui CSR_WRITE_4(sc, RTK_MISSEDPKT, 0);
1467 1.1 haya
1468 1.1 haya /* Enable receiver and transmitter. */
1469 1.10 tsutsui CSR_WRITE_1(sc, RTK_COMMAND, RTK_CMD_TX_ENB|RTK_CMD_RX_ENB);
1470 1.1 haya
1471 1.10 tsutsui CSR_WRITE_1(sc, RTK_CFG1, RTK_CFG1_DRVLOAD|RTK_CFG1_FULLDUPLEX);
1472 1.1 haya
1473 1.1 haya /*
1474 1.1 haya * Set current media.
1475 1.1 haya */
1476 1.1 haya mii_mediachg(&sc->mii);
1477 1.1 haya
1478 1.1 haya ifp->if_flags |= IFF_RUNNING;
1479 1.1 haya ifp->if_flags &= ~IFF_OACTIVE;
1480 1.1 haya
1481 1.15 thorpej callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
1482 1.1 haya
1483 1.15 thorpej out:
1484 1.15 thorpej if (error) {
1485 1.15 thorpej ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1486 1.15 thorpej ifp->if_timer = 0;
1487 1.15 thorpej printf("%s: interface not running\n", sc->sc_dev.dv_xname);
1488 1.15 thorpej }
1489 1.63 tsutsui return error;
1490 1.1 haya }
1491 1.1 haya
1492 1.1 haya /*
1493 1.1 haya * Set media options.
1494 1.1 haya */
1495 1.50 jdolecek STATIC int
1496 1.62 tsutsui rtk_ifmedia_upd(struct ifnet *ifp)
1497 1.1 haya {
1498 1.63 tsutsui struct rtk_softc *sc;
1499 1.1 haya
1500 1.1 haya sc = ifp->if_softc;
1501 1.1 haya
1502 1.63 tsutsui return mii_mediachg(&sc->mii);
1503 1.1 haya }
1504 1.1 haya
1505 1.1 haya /*
1506 1.1 haya * Report current media status.
1507 1.1 haya */
1508 1.50 jdolecek STATIC void
1509 1.62 tsutsui rtk_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1510 1.1 haya {
1511 1.63 tsutsui struct rtk_softc *sc;
1512 1.1 haya
1513 1.1 haya sc = ifp->if_softc;
1514 1.1 haya
1515 1.1 haya mii_pollstat(&sc->mii);
1516 1.1 haya ifmr->ifm_status = sc->mii.mii_media_status;
1517 1.1 haya ifmr->ifm_active = sc->mii.mii_media_active;
1518 1.1 haya }
1519 1.1 haya
1520 1.50 jdolecek STATIC int
1521 1.62 tsutsui rtk_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1522 1.1 haya {
1523 1.63 tsutsui struct rtk_softc *sc = ifp->if_softc;
1524 1.63 tsutsui struct ifreq *ifr = (struct ifreq *)data;
1525 1.63 tsutsui int s, error;
1526 1.1 haya
1527 1.9 thorpej s = splnet();
1528 1.1 haya
1529 1.12 drochner switch (command) {
1530 1.1 haya case SIOCGIFMEDIA:
1531 1.1 haya case SIOCSIFMEDIA:
1532 1.1 haya error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
1533 1.1 haya break;
1534 1.15 thorpej
1535 1.1 haya default:
1536 1.15 thorpej error = ether_ioctl(ifp, command, data);
1537 1.15 thorpej if (error == ENETRESET) {
1538 1.47 thorpej if (ifp->if_flags & IFF_RUNNING) {
1539 1.15 thorpej /*
1540 1.15 thorpej * Multicast list has changed. Set the
1541 1.15 thorpej * hardware filter accordingly.
1542 1.15 thorpej */
1543 1.15 thorpej rtk_setmulti(sc);
1544 1.15 thorpej }
1545 1.15 thorpej error = 0;
1546 1.15 thorpej }
1547 1.1 haya break;
1548 1.1 haya }
1549 1.1 haya
1550 1.12 drochner splx(s);
1551 1.1 haya
1552 1.63 tsutsui return error;
1553 1.1 haya }
1554 1.1 haya
1555 1.50 jdolecek STATIC void
1556 1.62 tsutsui rtk_watchdog(struct ifnet *ifp)
1557 1.1 haya {
1558 1.63 tsutsui struct rtk_softc *sc;
1559 1.1 haya
1560 1.1 haya sc = ifp->if_softc;
1561 1.1 haya
1562 1.1 haya printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1563 1.1 haya ifp->if_oerrors++;
1564 1.8 thorpej rtk_txeof(sc);
1565 1.8 thorpej rtk_rxeof(sc);
1566 1.15 thorpej rtk_init(ifp);
1567 1.1 haya }
1568 1.1 haya
1569 1.1 haya /*
1570 1.1 haya * Stop the adapter and free any mbufs allocated to the
1571 1.1 haya * RX and TX lists.
1572 1.1 haya */
1573 1.50 jdolecek STATIC void
1574 1.62 tsutsui rtk_stop(struct ifnet *ifp, int disable)
1575 1.1 haya {
1576 1.15 thorpej struct rtk_softc *sc = ifp->if_softc;
1577 1.31 thorpej struct rtk_tx_desc *txd;
1578 1.1 haya
1579 1.8 thorpej callout_stop(&sc->rtk_tick_ch);
1580 1.1 haya
1581 1.1 haya mii_down(&sc->mii);
1582 1.1 haya
1583 1.10 tsutsui CSR_WRITE_1(sc, RTK_COMMAND, 0x00);
1584 1.10 tsutsui CSR_WRITE_2(sc, RTK_IMR, 0x0000);
1585 1.1 haya
1586 1.1 haya /*
1587 1.1 haya * Free the TX list buffers.
1588 1.1 haya */
1589 1.31 thorpej while ((txd = SIMPLEQ_FIRST(&sc->rtk_tx_dirty)) != NULL) {
1590 1.41 lukem SIMPLEQ_REMOVE_HEAD(&sc->rtk_tx_dirty, txd_q);
1591 1.31 thorpej bus_dmamap_unload(sc->sc_dmat, txd->txd_dmamap);
1592 1.31 thorpej m_freem(txd->txd_mbuf);
1593 1.31 thorpej txd->txd_mbuf = NULL;
1594 1.31 thorpej CSR_WRITE_4(sc, txd->txd_txaddr, 0);
1595 1.1 haya }
1596 1.1 haya
1597 1.15 thorpej if (disable)
1598 1.15 thorpej rtk_disable(sc);
1599 1.15 thorpej
1600 1.1 haya ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1601 1.15 thorpej ifp->if_timer = 0;
1602 1.1 haya }
1603 1.1 haya
1604 1.1 haya /*
1605 1.1 haya * Stop all chip I/O so that the kernel's probe routines don't
1606 1.1 haya * get confused by errant DMAs when rebooting.
1607 1.1 haya */
1608 1.50 jdolecek STATIC void
1609 1.63 tsutsui rtk_shutdown(void *arg)
1610 1.1 haya {
1611 1.63 tsutsui struct rtk_softc *sc = (struct rtk_softc *)arg;
1612 1.1 haya
1613 1.15 thorpej rtk_stop(&sc->ethercom.ec_if, 0);
1614 1.1 haya }
1615 1.1 haya
1616 1.1 haya STATIC void
1617 1.62 tsutsui rtk_tick(void *arg)
1618 1.1 haya {
1619 1.8 thorpej struct rtk_softc *sc = arg;
1620 1.63 tsutsui int s;
1621 1.1 haya
1622 1.63 tsutsui s = splnet();
1623 1.1 haya mii_tick(&sc->mii);
1624 1.1 haya splx(s);
1625 1.1 haya
1626 1.8 thorpej callout_reset(&sc->rtk_tick_ch, hz, rtk_tick, sc);
1627 1.1 haya }
1628