rtl81x9.c revision 1.3 1 1.3 tsutsui /* $NetBSD: rtl81x9.c,v 1.3 2000/04/25 14:16:46 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.1 haya * 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.1 haya
88 1.1 haya #include "opt_inet.h"
89 1.1 haya #include "opt_ns.h"
90 1.1 haya #include "bpfilter.h"
91 1.1 haya #include "rnd.h"
92 1.1 haya
93 1.1 haya #include <sys/param.h>
94 1.1 haya #include <sys/systm.h>
95 1.1 haya #include <sys/callout.h>
96 1.1 haya #include <sys/device.h>
97 1.1 haya #include <sys/sockio.h>
98 1.1 haya #include <sys/mbuf.h>
99 1.1 haya #include <sys/malloc.h>
100 1.1 haya #include <sys/kernel.h>
101 1.1 haya #include <sys/socket.h>
102 1.1 haya
103 1.1 haya #include <net/if.h>
104 1.1 haya #include <net/if_arp.h>
105 1.1 haya #include <net/if_ether.h>
106 1.1 haya #include <net/if_dl.h>
107 1.1 haya #include <net/if_media.h>
108 1.1 haya #ifdef INET
109 1.1 haya #include <netinet/in.h>
110 1.1 haya #include <netinet/if_inarp.h>
111 1.1 haya #endif
112 1.1 haya #ifdef NS
113 1.1 haya #include <netns/ns.h>
114 1.1 haya #include <netns/ns_if.h>
115 1.1 haya #endif
116 1.1 haya
117 1.1 haya #if NBPFILTER > 0
118 1.1 haya #include <net/bpf.h>
119 1.1 haya #endif
120 1.1 haya #if NRND > 0
121 1.1 haya #include <sys/rnd.h>
122 1.1 haya #endif
123 1.1 haya
124 1.1 haya #include <machine/bus.h>
125 1.3 tsutsui #include <machine/endian.h>
126 1.1 haya
127 1.1 haya #include <dev/mii/mii.h>
128 1.1 haya #include <dev/mii/miivar.h>
129 1.1 haya
130 1.1 haya /*
131 1.1 haya * Default to using PIO access for this driver. On SMP systems,
132 1.1 haya * there appear to be problems with memory mapped mode: it looks like
133 1.1 haya * doing too many memory mapped access back to back in rapid succession
134 1.1 haya * can hang the bus. I'm inclined to blame this on crummy design/construction
135 1.1 haya * on the part of RealTek. Memory mapped mode does appear to work on
136 1.1 haya * uniprocessor systems though.
137 1.1 haya */
138 1.1 haya
139 1.1 haya #include <dev/ic/rtl81x9reg.h>
140 1.1 haya
141 1.1 haya #if defined DEBUG
142 1.1 haya #define STATIC
143 1.1 haya #else
144 1.1 haya #define STATIC static
145 1.1 haya #endif
146 1.1 haya
147 1.1 haya STATIC int rl_encap __P((struct rl_softc *, struct mbuf * ));
148 1.1 haya
149 1.1 haya STATIC void rl_rxeof __P((struct rl_softc *));
150 1.1 haya STATIC void rl_txeof __P((struct rl_softc *));
151 1.1 haya STATIC void rl_start __P((struct ifnet *));
152 1.1 haya STATIC int rl_ioctl __P((struct ifnet *, u_long, caddr_t));
153 1.1 haya STATIC void rl_init __P((void *));
154 1.1 haya STATIC void rl_stop __P((struct rl_softc *));
155 1.1 haya STATIC void rl_watchdog __P((struct ifnet *));
156 1.1 haya STATIC void rl_shutdown __P((void *));
157 1.1 haya STATIC int rl_ifmedia_upd __P((struct ifnet *));
158 1.1 haya STATIC void rl_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
159 1.1 haya
160 1.1 haya STATIC void rl_eeprom_putbyte __P((struct rl_softc *, int));
161 1.1 haya STATIC void rl_eeprom_getword __P((struct rl_softc *, int, u_int16_t *));
162 1.1 haya STATIC void rl_mii_sync __P((struct rl_softc *));
163 1.1 haya STATIC void rl_mii_send __P((struct rl_softc *, u_int32_t, int));
164 1.1 haya STATIC int rl_mii_readreg __P((struct rl_softc *, struct rl_mii_frame *));
165 1.1 haya STATIC int rl_mii_writereg __P((struct rl_softc *, struct rl_mii_frame *));
166 1.1 haya
167 1.1 haya STATIC int rl_phy_readreg __P((struct device *, int, int));
168 1.1 haya STATIC void rl_phy_writereg __P((struct device *, int, int, int));
169 1.1 haya STATIC void rl_phy_statchg __P((struct device *));
170 1.1 haya STATIC void rl_tick __P((void *));
171 1.1 haya
172 1.1 haya STATIC u_int8_t rl_calchash __P((caddr_t));
173 1.1 haya STATIC void rl_setmulti __P((struct rl_softc *));
174 1.1 haya STATIC int rl_list_tx_init __P((struct rl_softc *));
175 1.1 haya
176 1.1 haya STATIC int rl_ether_ioctl __P((struct ifnet *, u_long, caddr_t));
177 1.1 haya STATIC int rl_allocsndbuf __P((struct rl_softc *, int));
178 1.1 haya
179 1.1 haya
180 1.1 haya #define EE_SET(x) \
181 1.1 haya CSR_WRITE_1(sc, RL_EECMD, \
182 1.1 haya CSR_READ_1(sc, RL_EECMD) | x)
183 1.1 haya
184 1.1 haya #define EE_CLR(x) \
185 1.1 haya CSR_WRITE_1(sc, RL_EECMD, \
186 1.1 haya CSR_READ_1(sc, RL_EECMD) & ~x)
187 1.1 haya
188 1.1 haya /*
189 1.1 haya * Send a read command and address to the EEPROM, check for ACK.
190 1.1 haya */
191 1.1 haya STATIC void rl_eeprom_putbyte(sc, addr)
192 1.1 haya struct rl_softc *sc;
193 1.1 haya int addr;
194 1.1 haya {
195 1.2 tsutsui int d, i;
196 1.1 haya
197 1.1 haya d = addr | RL_EECMD_READ;
198 1.1 haya
199 1.1 haya /*
200 1.1 haya * Feed in each bit and stobe the clock.
201 1.1 haya */
202 1.1 haya for (i = 0x400; i; i >>= 1) {
203 1.1 haya if (d & i) {
204 1.1 haya EE_SET(RL_EE_DATAIN);
205 1.1 haya } else {
206 1.1 haya EE_CLR(RL_EE_DATAIN);
207 1.1 haya }
208 1.1 haya DELAY(100);
209 1.1 haya EE_SET(RL_EE_CLK);
210 1.1 haya DELAY(150);
211 1.1 haya EE_CLR(RL_EE_CLK);
212 1.1 haya DELAY(100);
213 1.1 haya }
214 1.1 haya
215 1.1 haya return;
216 1.1 haya }
217 1.1 haya
218 1.1 haya /*
219 1.1 haya * Read a word of data stored in the EEPROM at address 'addr.'
220 1.1 haya */
221 1.1 haya STATIC void rl_eeprom_getword(sc, addr, dest)
222 1.1 haya struct rl_softc *sc;
223 1.1 haya int addr;
224 1.1 haya u_int16_t *dest;
225 1.1 haya {
226 1.2 tsutsui int i;
227 1.1 haya u_int16_t word = 0;
228 1.1 haya
229 1.1 haya /* Enter EEPROM access mode. */
230 1.1 haya CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
231 1.1 haya
232 1.1 haya /*
233 1.1 haya * Send address of word we want to read.
234 1.1 haya */
235 1.1 haya rl_eeprom_putbyte(sc, addr);
236 1.1 haya
237 1.1 haya CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
238 1.1 haya
239 1.1 haya /*
240 1.1 haya * Start reading bits from EEPROM.
241 1.1 haya */
242 1.1 haya for (i = 0x8000; i; i >>= 1) {
243 1.1 haya EE_SET(RL_EE_CLK);
244 1.1 haya DELAY(100);
245 1.1 haya if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
246 1.1 haya word |= i;
247 1.1 haya EE_CLR(RL_EE_CLK);
248 1.1 haya DELAY(100);
249 1.1 haya }
250 1.1 haya
251 1.1 haya /* Turn off EEPROM access mode. */
252 1.1 haya CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
253 1.1 haya
254 1.1 haya *dest = word;
255 1.1 haya
256 1.1 haya return;
257 1.1 haya }
258 1.1 haya
259 1.1 haya /*
260 1.1 haya * Read a sequence of words from the EEPROM.
261 1.1 haya */
262 1.1 haya void rl_read_eeprom(sc, dest, off, cnt, swap)
263 1.1 haya struct rl_softc *sc;
264 1.1 haya caddr_t dest;
265 1.1 haya int off;
266 1.1 haya int cnt;
267 1.1 haya int swap;
268 1.1 haya {
269 1.1 haya int i;
270 1.1 haya u_int16_t word = 0, *ptr;
271 1.1 haya
272 1.1 haya for (i = 0; i < cnt; i++) {
273 1.1 haya rl_eeprom_getword(sc, off + i, &word);
274 1.1 haya ptr = (u_int16_t *)(dest + (i * 2));
275 1.1 haya if (swap)
276 1.3 tsutsui /*
277 1.3 tsutsui * EEPROM stores data in little endian and
278 1.3 tsutsui * rl_eeprom_getword() returns them in host's endian.
279 1.3 tsutsui * If caller requires byte-stream data, we should
280 1.3 tsutsui * revert them little endian.
281 1.3 tsutsui */
282 1.3 tsutsui *ptr = htole16(word);
283 1.1 haya else
284 1.1 haya *ptr = word;
285 1.1 haya }
286 1.1 haya
287 1.1 haya return;
288 1.1 haya }
289 1.1 haya
290 1.1 haya
291 1.1 haya /*
292 1.1 haya * MII access routines are provided for the 8129, which
293 1.1 haya * doesn't have a built-in PHY. For the 8139, we fake things
294 1.1 haya * up by diverting rl_phy_readreg()/rl_phy_writereg() to the
295 1.1 haya * direct access PHY registers.
296 1.1 haya */
297 1.1 haya #define MII_SET(x) \
298 1.1 haya CSR_WRITE_1(sc, RL_MII, \
299 1.1 haya CSR_READ_1(sc, RL_MII) | x)
300 1.1 haya
301 1.1 haya #define MII_CLR(x) \
302 1.1 haya CSR_WRITE_1(sc, RL_MII, \
303 1.1 haya CSR_READ_1(sc, RL_MII) & ~x)
304 1.1 haya
305 1.1 haya /*
306 1.1 haya * Sync the PHYs by setting data bit and strobing the clock 32 times.
307 1.1 haya */
308 1.1 haya STATIC void rl_mii_sync(sc)
309 1.1 haya struct rl_softc *sc;
310 1.1 haya {
311 1.2 tsutsui int i;
312 1.1 haya
313 1.1 haya MII_SET(RL_MII_DIR|RL_MII_DATAOUT);
314 1.1 haya
315 1.1 haya for (i = 0; i < 32; i++) {
316 1.1 haya MII_SET(RL_MII_CLK);
317 1.1 haya DELAY(1);
318 1.1 haya MII_CLR(RL_MII_CLK);
319 1.1 haya DELAY(1);
320 1.1 haya }
321 1.1 haya
322 1.1 haya return;
323 1.1 haya }
324 1.1 haya
325 1.1 haya /*
326 1.1 haya * Clock a series of bits through the MII.
327 1.1 haya */
328 1.1 haya STATIC void rl_mii_send(sc, bits, cnt)
329 1.1 haya struct rl_softc *sc;
330 1.1 haya u_int32_t bits;
331 1.1 haya int cnt;
332 1.1 haya {
333 1.1 haya int i;
334 1.1 haya
335 1.1 haya MII_CLR(RL_MII_CLK);
336 1.1 haya
337 1.1 haya for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
338 1.1 haya if (bits & i) {
339 1.1 haya MII_SET(RL_MII_DATAOUT);
340 1.1 haya } else {
341 1.1 haya MII_CLR(RL_MII_DATAOUT);
342 1.1 haya }
343 1.1 haya DELAY(1);
344 1.1 haya MII_CLR(RL_MII_CLK);
345 1.1 haya DELAY(1);
346 1.1 haya MII_SET(RL_MII_CLK);
347 1.1 haya }
348 1.1 haya }
349 1.1 haya
350 1.1 haya /*
351 1.1 haya * Read an PHY register through the MII.
352 1.1 haya */
353 1.1 haya STATIC int rl_mii_readreg(sc, frame)
354 1.1 haya struct rl_softc *sc;
355 1.1 haya struct rl_mii_frame *frame;
356 1.1 haya
357 1.1 haya {
358 1.1 haya int i, ack, s;
359 1.1 haya
360 1.1 haya s = splimp();
361 1.1 haya
362 1.1 haya /*
363 1.1 haya * Set up frame for RX.
364 1.1 haya */
365 1.1 haya frame->mii_stdelim = RL_MII_STARTDELIM;
366 1.1 haya frame->mii_opcode = RL_MII_READOP;
367 1.1 haya frame->mii_turnaround = 0;
368 1.1 haya frame->mii_data = 0;
369 1.1 haya
370 1.1 haya CSR_WRITE_2(sc, RL_MII, 0);
371 1.1 haya
372 1.1 haya /*
373 1.1 haya * Turn on data xmit.
374 1.1 haya */
375 1.1 haya MII_SET(RL_MII_DIR);
376 1.1 haya
377 1.1 haya rl_mii_sync(sc);
378 1.1 haya
379 1.1 haya /*
380 1.1 haya * Send command/address info.
381 1.1 haya */
382 1.1 haya rl_mii_send(sc, frame->mii_stdelim, 2);
383 1.1 haya rl_mii_send(sc, frame->mii_opcode, 2);
384 1.1 haya rl_mii_send(sc, frame->mii_phyaddr, 5);
385 1.1 haya rl_mii_send(sc, frame->mii_regaddr, 5);
386 1.1 haya
387 1.1 haya /* Idle bit */
388 1.1 haya MII_CLR((RL_MII_CLK|RL_MII_DATAOUT));
389 1.1 haya DELAY(1);
390 1.1 haya MII_SET(RL_MII_CLK);
391 1.1 haya DELAY(1);
392 1.1 haya
393 1.1 haya /* Turn off xmit. */
394 1.1 haya MII_CLR(RL_MII_DIR);
395 1.1 haya
396 1.1 haya /* Check for ack */
397 1.1 haya MII_CLR(RL_MII_CLK);
398 1.1 haya DELAY(1);
399 1.1 haya MII_SET(RL_MII_CLK);
400 1.1 haya DELAY(1);
401 1.1 haya ack = CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN;
402 1.1 haya
403 1.1 haya /*
404 1.1 haya * Now try reading data bits. If the ack failed, we still
405 1.1 haya * need to clock through 16 cycles to keep the PHY(s) in sync.
406 1.1 haya */
407 1.1 haya if (ack) {
408 1.1 haya for(i = 0; i < 16; i++) {
409 1.1 haya MII_CLR(RL_MII_CLK);
410 1.1 haya DELAY(1);
411 1.1 haya MII_SET(RL_MII_CLK);
412 1.1 haya DELAY(1);
413 1.1 haya }
414 1.1 haya goto fail;
415 1.1 haya }
416 1.1 haya
417 1.1 haya for (i = 0x8000; i; i >>= 1) {
418 1.1 haya MII_CLR(RL_MII_CLK);
419 1.1 haya DELAY(1);
420 1.1 haya if (!ack) {
421 1.1 haya if (CSR_READ_2(sc, RL_MII) & RL_MII_DATAIN)
422 1.1 haya frame->mii_data |= i;
423 1.1 haya DELAY(1);
424 1.1 haya }
425 1.1 haya MII_SET(RL_MII_CLK);
426 1.1 haya DELAY(1);
427 1.1 haya }
428 1.1 haya
429 1.1 haya fail:
430 1.1 haya
431 1.1 haya MII_CLR(RL_MII_CLK);
432 1.1 haya DELAY(1);
433 1.1 haya MII_SET(RL_MII_CLK);
434 1.1 haya DELAY(1);
435 1.1 haya
436 1.1 haya splx(s);
437 1.1 haya
438 1.1 haya if (ack)
439 1.1 haya return(1);
440 1.1 haya return(0);
441 1.1 haya }
442 1.1 haya
443 1.1 haya /*
444 1.1 haya * Write to a PHY register through the MII.
445 1.1 haya */
446 1.1 haya STATIC int rl_mii_writereg(sc, frame)
447 1.1 haya struct rl_softc *sc;
448 1.1 haya struct rl_mii_frame *frame;
449 1.1 haya
450 1.1 haya {
451 1.1 haya int s;
452 1.1 haya
453 1.1 haya s = splimp();
454 1.1 haya /*
455 1.1 haya * Set up frame for TX.
456 1.1 haya */
457 1.1 haya
458 1.1 haya frame->mii_stdelim = RL_MII_STARTDELIM;
459 1.1 haya frame->mii_opcode = RL_MII_WRITEOP;
460 1.1 haya frame->mii_turnaround = RL_MII_TURNAROUND;
461 1.1 haya
462 1.1 haya /*
463 1.1 haya * Turn on data output.
464 1.1 haya */
465 1.1 haya MII_SET(RL_MII_DIR);
466 1.1 haya
467 1.1 haya rl_mii_sync(sc);
468 1.1 haya
469 1.1 haya rl_mii_send(sc, frame->mii_stdelim, 2);
470 1.1 haya rl_mii_send(sc, frame->mii_opcode, 2);
471 1.1 haya rl_mii_send(sc, frame->mii_phyaddr, 5);
472 1.1 haya rl_mii_send(sc, frame->mii_regaddr, 5);
473 1.1 haya rl_mii_send(sc, frame->mii_turnaround, 2);
474 1.1 haya rl_mii_send(sc, frame->mii_data, 16);
475 1.1 haya
476 1.1 haya /* Idle bit. */
477 1.1 haya MII_SET(RL_MII_CLK);
478 1.1 haya DELAY(1);
479 1.1 haya MII_CLR(RL_MII_CLK);
480 1.1 haya DELAY(1);
481 1.1 haya
482 1.1 haya /*
483 1.1 haya * Turn off xmit.
484 1.1 haya */
485 1.1 haya MII_CLR(RL_MII_DIR);
486 1.1 haya
487 1.1 haya splx(s);
488 1.1 haya
489 1.1 haya return(0);
490 1.1 haya }
491 1.1 haya
492 1.1 haya STATIC int rl_phy_readreg(self, phy, reg)
493 1.1 haya struct device *self;
494 1.1 haya int phy, reg;
495 1.1 haya {
496 1.1 haya struct rl_softc *sc = (void *)self;
497 1.1 haya struct rl_mii_frame frame;
498 1.1 haya u_int16_t rval = 0;
499 1.1 haya u_int16_t rl8139_reg = 0;
500 1.1 haya
501 1.1 haya if (sc->rl_type == RL_8139) {
502 1.1 haya if (phy != 7)
503 1.1 haya return (0);
504 1.1 haya
505 1.1 haya switch(reg) {
506 1.1 haya case MII_BMCR:
507 1.1 haya rl8139_reg = RL_BMCR;
508 1.1 haya break;
509 1.1 haya case MII_BMSR:
510 1.1 haya rl8139_reg = RL_BMSR;
511 1.1 haya break;
512 1.1 haya case MII_ANAR:
513 1.1 haya rl8139_reg = RL_ANAR;
514 1.1 haya break;
515 1.1 haya case MII_ANLPAR:
516 1.1 haya rl8139_reg = RL_LPAR;
517 1.1 haya break;
518 1.1 haya default:
519 1.1 haya #if 0
520 1.1 haya printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
521 1.1 haya #endif
522 1.1 haya return(0);
523 1.1 haya }
524 1.1 haya rval = CSR_READ_2(sc, rl8139_reg);
525 1.1 haya return(rval);
526 1.1 haya }
527 1.1 haya
528 1.1 haya bzero((char *)&frame, sizeof(frame));
529 1.1 haya
530 1.1 haya frame.mii_phyaddr = phy;
531 1.1 haya frame.mii_regaddr = reg;
532 1.1 haya rl_mii_readreg(sc, &frame);
533 1.1 haya
534 1.1 haya return(frame.mii_data);
535 1.1 haya }
536 1.1 haya
537 1.1 haya STATIC void rl_phy_writereg(self, phy, reg, data)
538 1.1 haya struct device *self;
539 1.1 haya int phy, reg;
540 1.1 haya int data;
541 1.1 haya {
542 1.1 haya struct rl_softc *sc = (void *)self;
543 1.1 haya struct rl_mii_frame frame;
544 1.1 haya u_int16_t rl8139_reg = 0;
545 1.1 haya
546 1.1 haya if (sc->rl_type == RL_8139) {
547 1.1 haya if (phy != 7)
548 1.1 haya return;
549 1.1 haya
550 1.1 haya switch(reg) {
551 1.1 haya case MII_BMCR:
552 1.1 haya rl8139_reg = RL_BMCR;
553 1.1 haya break;
554 1.1 haya case MII_BMSR:
555 1.1 haya rl8139_reg = RL_BMSR;
556 1.1 haya break;
557 1.1 haya case MII_ANAR:
558 1.1 haya rl8139_reg = RL_ANAR;
559 1.1 haya break;
560 1.1 haya case MII_ANLPAR:
561 1.1 haya rl8139_reg = RL_LPAR;
562 1.1 haya break;
563 1.1 haya default:
564 1.1 haya #if 0
565 1.1 haya printf("%s: bad phy register\n", sc->sc_dev.dv_xname);
566 1.1 haya #endif
567 1.1 haya return;
568 1.1 haya }
569 1.1 haya CSR_WRITE_2(sc, rl8139_reg, data);
570 1.1 haya return;
571 1.1 haya }
572 1.1 haya
573 1.1 haya bzero((char *)&frame, sizeof(frame));
574 1.1 haya
575 1.1 haya frame.mii_phyaddr = phy;
576 1.1 haya frame.mii_regaddr = reg;
577 1.1 haya frame.mii_data = data;
578 1.1 haya
579 1.1 haya rl_mii_writereg(sc, &frame);
580 1.1 haya
581 1.1 haya return;
582 1.1 haya }
583 1.1 haya
584 1.1 haya STATIC void
585 1.1 haya rl_phy_statchg(v)
586 1.1 haya struct device *v;
587 1.1 haya {
588 1.1 haya
589 1.1 haya /* Nothing to do. */
590 1.1 haya }
591 1.1 haya
592 1.1 haya /*
593 1.1 haya * Calculate CRC of a multicast group address, return the upper 6 bits.
594 1.1 haya */
595 1.1 haya STATIC u_int8_t rl_calchash(addr)
596 1.1 haya caddr_t addr;
597 1.1 haya {
598 1.1 haya u_int32_t crc, carry;
599 1.1 haya int i, j;
600 1.1 haya u_int8_t c;
601 1.1 haya
602 1.1 haya /* Compute CRC for the address value. */
603 1.1 haya crc = 0xFFFFFFFF; /* initial value */
604 1.1 haya
605 1.1 haya for (i = 0; i < 6; i++) {
606 1.1 haya c = *(addr + i);
607 1.1 haya for (j = 0; j < 8; j++) {
608 1.1 haya carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
609 1.1 haya crc <<= 1;
610 1.1 haya c >>= 1;
611 1.1 haya if (carry)
612 1.1 haya crc = (crc ^ 0x04c11db6) | carry;
613 1.1 haya }
614 1.1 haya }
615 1.1 haya
616 1.1 haya /* return the filter bit position */
617 1.1 haya return(crc >> 26);
618 1.1 haya }
619 1.1 haya
620 1.1 haya /*
621 1.1 haya * Program the 64-bit multicast hash filter.
622 1.1 haya */
623 1.1 haya STATIC void rl_setmulti(sc)
624 1.1 haya struct rl_softc *sc;
625 1.1 haya {
626 1.1 haya struct ifnet *ifp;
627 1.1 haya int h = 0;
628 1.1 haya u_int32_t hashes[2] = { 0, 0 };
629 1.1 haya u_int32_t rxfilt;
630 1.1 haya int mcnt = 0;
631 1.1 haya struct ether_multi *enm;
632 1.1 haya struct ether_multistep step;
633 1.1 haya
634 1.1 haya ifp = &sc->ethercom.ec_if;
635 1.1 haya
636 1.1 haya rxfilt = CSR_READ_4(sc, RL_RXCFG);
637 1.1 haya
638 1.1 haya if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
639 1.1 haya rxfilt |= RL_RXCFG_RX_MULTI;
640 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
641 1.1 haya CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
642 1.1 haya CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
643 1.1 haya return;
644 1.1 haya }
645 1.1 haya
646 1.1 haya /* first, zot all the existing hash bits */
647 1.1 haya CSR_WRITE_4(sc, RL_MAR0, 0);
648 1.1 haya CSR_WRITE_4(sc, RL_MAR4, 0);
649 1.1 haya
650 1.1 haya /* now program new ones */
651 1.1 haya ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
652 1.1 haya while (enm != NULL) {
653 1.1 haya h = rl_calchash(enm->enm_addrlo);
654 1.1 haya if (h < 32)
655 1.1 haya hashes[0] |= (1 << h);
656 1.1 haya else
657 1.1 haya hashes[1] |= (1 << (h - 32));
658 1.1 haya mcnt++;
659 1.1 haya ETHER_NEXT_MULTI(step, enm);
660 1.1 haya }
661 1.1 haya
662 1.1 haya if (mcnt)
663 1.1 haya rxfilt |= RL_RXCFG_RX_MULTI;
664 1.1 haya else
665 1.1 haya rxfilt &= ~RL_RXCFG_RX_MULTI;
666 1.1 haya
667 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
668 1.1 haya CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
669 1.1 haya CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
670 1.1 haya
671 1.1 haya return;
672 1.1 haya }
673 1.1 haya
674 1.1 haya void rl_reset(sc)
675 1.1 haya struct rl_softc *sc;
676 1.1 haya {
677 1.2 tsutsui int i;
678 1.1 haya
679 1.1 haya CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
680 1.1 haya
681 1.1 haya for (i = 0; i < RL_TIMEOUT; i++) {
682 1.1 haya DELAY(10);
683 1.1 haya if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
684 1.1 haya break;
685 1.1 haya }
686 1.1 haya if (i == RL_TIMEOUT)
687 1.1 haya printf("%s: reset never completed!\n", sc->sc_dev.dv_xname);
688 1.1 haya
689 1.1 haya return;
690 1.1 haya }
691 1.1 haya
692 1.1 haya /*
693 1.1 haya * Attach the interface. Allocate softc structures, do ifmedia
694 1.1 haya * setup and ethernet/BPF attach.
695 1.1 haya */
696 1.1 haya void
697 1.1 haya rl_attach(sc, eaddr)
698 1.1 haya struct rl_softc *sc;
699 1.1 haya const u_int8_t *eaddr;
700 1.1 haya {
701 1.1 haya
702 1.1 haya struct ifnet *ifp;
703 1.1 haya bus_dma_segment_t dmaseg;
704 1.1 haya int error,dmanseg;
705 1.1 haya int i;
706 1.1 haya
707 1.1 haya callout_init(&sc->rl_tick_ch);
708 1.1 haya
709 1.1 haya if ((error = bus_dmamem_alloc(sc->sc_dmat,
710 1.1 haya RL_RXBUFLEN + 32, NBPG, 0, &dmaseg, 1, &dmanseg,
711 1.1 haya BUS_DMA_NOWAIT)) != 0) {
712 1.1 haya printf("%s: can't allocate recv buffer, error = %d\n",
713 1.1 haya sc->sc_dev.dv_xname, error);
714 1.1 haya goto fail;
715 1.1 haya }
716 1.1 haya
717 1.1 haya if ((error = bus_dmamem_map(sc->sc_dmat, &dmaseg, dmanseg,
718 1.1 haya RL_RXBUFLEN + 32, (caddr_t *)&sc->rl_cdata.rl_rx_buf,
719 1.1 haya BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
720 1.1 haya printf("%s: can't map recv buffer, error = %d\n",
721 1.1 haya sc->sc_dev.dv_xname, error);
722 1.1 haya goto fail;
723 1.1 haya }
724 1.1 haya
725 1.1 haya /* Leave a few bytes before the start of the RX ring buffer. */
726 1.1 haya sc->rl_cdata.rl_rx_buf_ptr = sc->rl_cdata.rl_rx_buf;
727 1.1 haya sc->rl_cdata.rl_rx_buf += sizeof(u_int64_t);
728 1.1 haya
729 1.1 haya if ((error = bus_dmamap_create(sc->sc_dmat,
730 1.1 haya RL_RXBUFLEN + 32 - sizeof(u_int64_t), 1,
731 1.1 haya RL_RXBUFLEN + 32 - sizeof(u_int64_t), 0, BUS_DMA_NOWAIT,
732 1.1 haya &sc->recv_dmamap)) != 0) {
733 1.1 haya printf("%s: can't create recv buffer DMA map, error = %d\n",
734 1.1 haya sc->sc_dev.dv_xname, error);
735 1.1 haya goto fail;
736 1.1 haya }
737 1.1 haya
738 1.1 haya if ((error = bus_dmamap_load(sc->sc_dmat, sc->recv_dmamap,
739 1.1 haya sc->rl_cdata.rl_rx_buf, RL_RXBUFLEN + 32 - sizeof(u_int64_t), NULL,
740 1.1 haya BUS_DMA_NOWAIT)) != 0) {
741 1.1 haya printf("%s: can't load recv buffer DMA map, error = %d\n",
742 1.1 haya sc->sc_dev.dv_xname, error);
743 1.1 haya goto fail;
744 1.1 haya }
745 1.1 haya
746 1.1 haya for (i = 0; i < RL_TX_LIST_CNT; i++)
747 1.1 haya if (rl_allocsndbuf(sc, i))
748 1.1 haya goto fail;
749 1.1 haya
750 1.1 haya ifp = &sc->ethercom.ec_if;
751 1.1 haya ifp->if_softc = sc;
752 1.1 haya bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
753 1.1 haya ifp->if_mtu = ETHERMTU;
754 1.1 haya ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
755 1.1 haya ifp->if_ioctl = rl_ioctl;
756 1.1 haya #if 0
757 1.1 haya ifp->if_output = ether_output;
758 1.1 haya #endif
759 1.1 haya ifp->if_start = rl_start;
760 1.1 haya ifp->if_watchdog = rl_watchdog;
761 1.1 haya ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
762 1.1 haya
763 1.1 haya /*
764 1.1 haya * Do ifmedia setup.
765 1.1 haya */
766 1.1 haya sc->mii.mii_ifp = ifp;
767 1.1 haya sc->mii.mii_readreg = rl_phy_readreg;
768 1.1 haya sc->mii.mii_writereg = rl_phy_writereg;
769 1.1 haya sc->mii.mii_statchg = rl_phy_statchg;
770 1.1 haya ifmedia_init(&sc->mii.mii_media, 0, rl_ifmedia_upd, rl_ifmedia_sts);
771 1.1 haya mii_attach(&sc->sc_dev, &sc->mii, 0xffffffff,
772 1.1 haya MII_PHY_ANY, MII_OFFSET_ANY, 0);
773 1.1 haya
774 1.1 haya /* Choose a default media. */
775 1.1 haya if (LIST_FIRST(&sc->mii.mii_phys) == NULL) {
776 1.1 haya ifmedia_add(&sc->mii.mii_media, IFM_ETHER|IFM_NONE,
777 1.1 haya 0, NULL);
778 1.1 haya ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_NONE);
779 1.1 haya } else {
780 1.1 haya ifmedia_set(&sc->mii.mii_media, IFM_ETHER|IFM_AUTO);
781 1.1 haya }
782 1.1 haya
783 1.1 haya /*
784 1.1 haya * Call MI attach routines.
785 1.1 haya */
786 1.1 haya if_attach(ifp);
787 1.1 haya ether_ifattach(ifp, eaddr);
788 1.1 haya
789 1.1 haya #if NBPFILTER > 0
790 1.1 haya bpfattach(&sc->ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
791 1.1 haya sizeof(struct ether_header));
792 1.1 haya #endif
793 1.1 haya shutdownhook_establish(rl_shutdown, sc);
794 1.1 haya
795 1.1 haya fail:
796 1.1 haya return;
797 1.1 haya }
798 1.1 haya
799 1.1 haya /*
800 1.1 haya * Initialize the transmit descriptors.
801 1.1 haya */
802 1.1 haya STATIC int rl_list_tx_init(sc)
803 1.1 haya struct rl_softc *sc;
804 1.1 haya {
805 1.1 haya struct rl_chain_data *cd;
806 1.1 haya int i;
807 1.1 haya
808 1.1 haya cd = &sc->rl_cdata;
809 1.1 haya for (i = 0; i < RL_TX_LIST_CNT; i++) {
810 1.1 haya cd->rl_tx_chain[i] = NULL;
811 1.1 haya CSR_WRITE_4(sc,
812 1.1 haya RL_TXADDR0 + (i * sizeof(u_int32_t)), 0x0000000);
813 1.1 haya }
814 1.1 haya
815 1.1 haya sc->rl_cdata.cur_tx = 0;
816 1.1 haya sc->rl_cdata.last_tx = 0;
817 1.1 haya
818 1.1 haya return(0);
819 1.1 haya }
820 1.1 haya
821 1.1 haya /*
822 1.1 haya * A frame has been uploaded: pass the resulting mbuf chain up to
823 1.1 haya * the higher level protocols.
824 1.1 haya *
825 1.1 haya * You know there's something wrong with a PCI bus-master chip design
826 1.1 haya * when you have to use m_devget().
827 1.1 haya *
828 1.1 haya * The receive operation is badly documented in the datasheet, so I'll
829 1.1 haya * attempt to document it here. The driver provides a buffer area and
830 1.1 haya * places its base address in the RX buffer start address register.
831 1.1 haya * The chip then begins copying frames into the RX buffer. Each frame
832 1.1 haya * is preceeded by a 32-bit RX status word which specifies the length
833 1.1 haya * of the frame and certain other status bits. Each frame (starting with
834 1.1 haya * the status word) is also 32-bit aligned. The frame length is in the
835 1.1 haya * first 16 bits of the status word; the lower 15 bits correspond with
836 1.1 haya * the 'rx status register' mentioned in the datasheet.
837 1.1 haya *
838 1.1 haya * Note: to make the Alpha happy, the frame payload needs to be aligned
839 1.1 haya * on a 32-bit boundary. To achieve this, we cheat a bit by copying from
840 1.1 haya * the ring buffer starting at an address two bytes before the actual
841 1.1 haya * data location. We can then shave off the first two bytes using m_adj().
842 1.1 haya * The reason we do this is because m_devget() doesn't let us specify an
843 1.1 haya * offset into the mbuf storage space, so we have to artificially create
844 1.1 haya * one. The ring is allocated in such a way that there are a few unused
845 1.1 haya * bytes of space preceecing it so that it will be safe for us to do the
846 1.1 haya * 2-byte backstep even if reading from the ring at offset 0.
847 1.1 haya */
848 1.1 haya STATIC void rl_rxeof(sc)
849 1.1 haya struct rl_softc *sc;
850 1.1 haya {
851 1.1 haya struct ether_header *eh;
852 1.1 haya struct mbuf *m;
853 1.1 haya struct ifnet *ifp;
854 1.1 haya int total_len = 0;
855 1.1 haya u_int32_t rxstat;
856 1.1 haya caddr_t rxbufpos;
857 1.1 haya int wrap = 0;
858 1.1 haya u_int16_t cur_rx;
859 1.1 haya u_int16_t limit;
860 1.1 haya u_int16_t rx_bytes = 0, max_bytes;
861 1.1 haya
862 1.1 haya ifp = &sc->ethercom.ec_if;
863 1.1 haya
864 1.1 haya cur_rx = (CSR_READ_2(sc, RL_CURRXADDR) + 16) % RL_RXBUFLEN;
865 1.1 haya
866 1.1 haya /* Do not try to read past this point. */
867 1.1 haya limit = CSR_READ_2(sc, RL_CURRXBUF) % RL_RXBUFLEN;
868 1.1 haya
869 1.1 haya if (limit < cur_rx)
870 1.1 haya max_bytes = (RL_RXBUFLEN - cur_rx) + limit;
871 1.1 haya else
872 1.1 haya max_bytes = limit - cur_rx;
873 1.1 haya
874 1.1 haya while((CSR_READ_1(sc, RL_COMMAND) & RL_CMD_EMPTY_RXBUF) == 0) {
875 1.1 haya rxbufpos = sc->rl_cdata.rl_rx_buf + cur_rx;
876 1.3 tsutsui rxstat = le32toh(*(u_int32_t *)rxbufpos);
877 1.1 haya
878 1.1 haya /*
879 1.1 haya * Here's a totally undocumented fact for you. When the
880 1.1 haya * RealTek chip is in the process of copying a packet into
881 1.1 haya * RAM for you, the length will be 0xfff0. If you spot a
882 1.1 haya * packet header with this value, you need to stop. The
883 1.1 haya * datasheet makes absolutely no mention of this and
884 1.1 haya * RealTek should be shot for this.
885 1.1 haya */
886 1.1 haya if ((u_int16_t)(rxstat >> 16) == RL_RXSTAT_UNFINISHED)
887 1.1 haya break;
888 1.1 haya
889 1.1 haya if (!(rxstat & RL_RXSTAT_RXOK)) {
890 1.1 haya ifp->if_ierrors++;
891 1.1 haya
892 1.1 haya /*
893 1.1 haya * submitted by:[netbsd-pcmcia:00484]
894 1.1 haya * Takahiro Kambe <taca (at) sky.yamashina.kyoto.jp>
895 1.1 haya * obtain from:
896 1.1 haya * FreeBSD if_rl.c rev 1.24->1.25
897 1.1 haya *
898 1.1 haya */
899 1.1 haya #if 0
900 1.1 haya if (rxstat & (RL_RXSTAT_BADSYM|RL_RXSTAT_RUNT|
901 1.1 haya RL_RXSTAT_GIANT|RL_RXSTAT_CRCERR|
902 1.1 haya RL_RXSTAT_ALIGNERR)) {
903 1.1 haya if (rxstat & (RL_RXSTAT_BADSYM|RL_RXSTAT_RUNT|
904 1.1 haya RL_RXSTAT_GIANT|RL_RXSTAT_CRCERR|
905 1.1 haya RL_RXSTAT_ALIGNERR)) {
906 1.1 haya CSR_WRITE_2(sc, RL_COMMAND, RL_CMD_TX_ENB);
907 1.1 haya CSR_WRITE_2(sc, RL_COMMAND, RL_CMD_TX_ENB|
908 1.1 haya RL_CMD_RX_ENB);
909 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
910 1.1 haya CSR_WRITE_4(sc, RL_RXADDR,
911 1.1 haya sc->recv_dmamap->dm_segs[0].ds_addr);
912 1.1 haya CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
913 1.1 haya cur_rx = 0;
914 1.1 haya }
915 1.1 haya break;
916 1.1 haya #else
917 1.1 haya rl_init(sc);
918 1.1 haya return;
919 1.1 haya #endif
920 1.1 haya }
921 1.1 haya
922 1.1 haya /* No errors; receive the packet. */
923 1.1 haya total_len = rxstat >> 16;
924 1.1 haya rx_bytes += total_len + 4;
925 1.1 haya
926 1.1 haya /*
927 1.1 haya * XXX The RealTek chip includes the CRC with every
928 1.1 haya * received frame, and there's no way to turn this
929 1.1 haya * behavior off (at least, I can't find anything in
930 1.1 haya * the manual that explains how to do it) so we have
931 1.1 haya * to trim off the CRC manually.
932 1.1 haya */
933 1.1 haya total_len -= ETHER_CRC_LEN;
934 1.1 haya
935 1.1 haya /*
936 1.1 haya * Avoid trying to read more bytes than we know
937 1.1 haya * the chip has prepared for us.
938 1.1 haya */
939 1.1 haya if (rx_bytes > max_bytes)
940 1.1 haya break;
941 1.1 haya
942 1.1 haya rxbufpos = sc->rl_cdata.rl_rx_buf +
943 1.1 haya ((cur_rx + sizeof(u_int32_t)) % RL_RXBUFLEN);
944 1.1 haya
945 1.1 haya if (rxbufpos == (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN))
946 1.1 haya rxbufpos = sc->rl_cdata.rl_rx_buf;
947 1.1 haya
948 1.1 haya wrap = (sc->rl_cdata.rl_rx_buf + RL_RXBUFLEN) - rxbufpos;
949 1.1 haya
950 1.1 haya if (total_len > wrap) {
951 1.1 haya m = m_devget(rxbufpos - RL_ETHER_ALIGN,
952 1.1 haya wrap + RL_ETHER_ALIGN, 0, ifp, NULL);
953 1.1 haya if (m == NULL) {
954 1.1 haya ifp->if_ierrors++;
955 1.1 haya printf("%s: out of mbufs, tried to "
956 1.1 haya "copy %d bytes\n", sc->sc_dev.dv_xname, wrap);
957 1.1 haya }
958 1.1 haya else {
959 1.1 haya m_adj(m, RL_ETHER_ALIGN);
960 1.1 haya m_copyback(m, wrap, total_len - wrap,
961 1.1 haya sc->rl_cdata.rl_rx_buf);
962 1.1 haya }
963 1.1 haya cur_rx = (total_len - wrap + ETHER_CRC_LEN);
964 1.1 haya } else {
965 1.1 haya m = m_devget(rxbufpos - RL_ETHER_ALIGN,
966 1.1 haya total_len + RL_ETHER_ALIGN, 0, ifp, NULL);
967 1.1 haya if (m == NULL) {
968 1.1 haya ifp->if_ierrors++;
969 1.1 haya printf("%s: out of mbufs, tried to "
970 1.1 haya "copy %d bytes\n", sc->sc_dev.dv_xname, total_len);
971 1.1 haya } else
972 1.1 haya m_adj(m, RL_ETHER_ALIGN);
973 1.1 haya cur_rx += total_len + 4 + ETHER_CRC_LEN;
974 1.1 haya }
975 1.1 haya
976 1.1 haya /*
977 1.1 haya * Round up to 32-bit boundary.
978 1.1 haya */
979 1.1 haya cur_rx = (cur_rx + 3) & ~3;
980 1.1 haya CSR_WRITE_2(sc, RL_CURRXADDR, cur_rx - 16);
981 1.1 haya
982 1.1 haya if (m == NULL)
983 1.1 haya continue;
984 1.1 haya
985 1.1 haya eh = mtod(m, struct ether_header *);
986 1.1 haya ifp->if_ipackets++;
987 1.1 haya
988 1.1 haya #if NBPFILTER > 0
989 1.1 haya /*
990 1.1 haya * Handle BPF listeners. Let the BPF user see the packet, but
991 1.1 haya * don't pass it up to the ether_input() layer unless it's
992 1.1 haya * a broadcast packet, multicast packet, matches our ethernet
993 1.1 haya * address or the interface is in promiscuous mode.
994 1.1 haya */
995 1.1 haya if (ifp->if_bpf) {
996 1.1 haya bpf_mtap(ifp->if_bpf, m);
997 1.1 haya if (ifp->if_flags & IFF_PROMISC &&
998 1.1 haya (bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
999 1.1 haya ETHER_ADDR_LEN) &&
1000 1.1 haya (eh->ether_dhost[0] & 1) == 0)) {
1001 1.1 haya m_freem(m);
1002 1.1 haya continue;
1003 1.1 haya }
1004 1.1 haya }
1005 1.1 haya #endif
1006 1.1 haya /* pass it on. */
1007 1.1 haya (*ifp->if_input)(ifp, m);
1008 1.1 haya }
1009 1.1 haya
1010 1.1 haya return;
1011 1.1 haya }
1012 1.1 haya
1013 1.1 haya /*
1014 1.1 haya * A frame was downloaded to the chip. It's safe for us to clean up
1015 1.1 haya * the list buffers.
1016 1.1 haya */
1017 1.1 haya STATIC void rl_txeof(sc)
1018 1.1 haya struct rl_softc *sc;
1019 1.1 haya {
1020 1.1 haya struct ifnet *ifp;
1021 1.1 haya u_int32_t txstat;
1022 1.1 haya
1023 1.1 haya ifp = &sc->ethercom.ec_if;
1024 1.1 haya
1025 1.1 haya /* Clear the timeout timer. */
1026 1.1 haya ifp->if_timer = 0;
1027 1.1 haya
1028 1.1 haya /*
1029 1.1 haya * Go through our tx list and free mbufs for those
1030 1.1 haya * frames that have been uploaded.
1031 1.1 haya */
1032 1.1 haya do {
1033 1.1 haya txstat = CSR_READ_4(sc, RL_LAST_TXSTAT(sc));
1034 1.1 haya if (!(txstat & (RL_TXSTAT_TX_OK|
1035 1.1 haya RL_TXSTAT_TX_UNDERRUN|RL_TXSTAT_TXABRT)))
1036 1.1 haya break;
1037 1.1 haya
1038 1.1 haya ifp->if_collisions += (txstat & RL_TXSTAT_COLLCNT) >> 24;
1039 1.1 haya
1040 1.1 haya if (RL_LAST_TXMBUF(sc) != NULL) {
1041 1.1 haya RL_LAST_TXMBUF(sc) = NULL;
1042 1.1 haya }
1043 1.1 haya if (txstat & RL_TXSTAT_TX_OK)
1044 1.1 haya ifp->if_opackets++;
1045 1.1 haya else {
1046 1.1 haya ifp->if_oerrors++;
1047 1.1 haya if ((txstat & RL_TXSTAT_TXABRT) ||
1048 1.1 haya (txstat & RL_TXSTAT_OUTOFWIN))
1049 1.1 haya CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
1050 1.1 haya }
1051 1.1 haya RL_INC(sc->rl_cdata.last_tx);
1052 1.1 haya ifp->if_flags &= ~IFF_OACTIVE;
1053 1.1 haya } while (sc->rl_cdata.last_tx != sc->rl_cdata.cur_tx);
1054 1.1 haya
1055 1.1 haya return;
1056 1.1 haya }
1057 1.1 haya
1058 1.1 haya int rl_intr(arg)
1059 1.1 haya void *arg;
1060 1.1 haya {
1061 1.1 haya struct rl_softc *sc;
1062 1.1 haya struct ifnet *ifp;
1063 1.1 haya u_int16_t status;
1064 1.1 haya int handled = 0;
1065 1.1 haya
1066 1.1 haya sc = arg;
1067 1.1 haya ifp = &sc->ethercom.ec_if;
1068 1.1 haya
1069 1.1 haya /* Disable interrupts. */
1070 1.1 haya CSR_WRITE_2(sc, RL_IMR, 0x0000);
1071 1.1 haya
1072 1.1 haya for (;;) {
1073 1.1 haya
1074 1.1 haya status = CSR_READ_2(sc, RL_ISR);
1075 1.1 haya if (status)
1076 1.1 haya CSR_WRITE_2(sc, RL_ISR, status);
1077 1.1 haya
1078 1.1 haya handled = 1;
1079 1.1 haya
1080 1.1 haya if ((status & RL_INTRS) == 0)
1081 1.1 haya break;
1082 1.1 haya
1083 1.1 haya if (status & RL_ISR_RX_OK)
1084 1.1 haya rl_rxeof(sc);
1085 1.1 haya
1086 1.1 haya if (status & RL_ISR_RX_ERR)
1087 1.1 haya rl_rxeof(sc);
1088 1.1 haya
1089 1.1 haya if ((status & RL_ISR_TX_OK) || (status & RL_ISR_TX_ERR))
1090 1.1 haya rl_txeof(sc);
1091 1.1 haya
1092 1.1 haya if (status & RL_ISR_SYSTEM_ERR) {
1093 1.1 haya rl_reset(sc);
1094 1.1 haya rl_init(sc);
1095 1.1 haya }
1096 1.1 haya
1097 1.1 haya }
1098 1.1 haya
1099 1.1 haya /* Re-enable interrupts. */
1100 1.1 haya CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1101 1.1 haya
1102 1.1 haya if (ifp->if_snd.ifq_head != NULL) {
1103 1.1 haya rl_start(ifp);
1104 1.1 haya }
1105 1.1 haya
1106 1.1 haya return (handled);
1107 1.1 haya }
1108 1.1 haya
1109 1.1 haya STATIC int
1110 1.1 haya rl_allocsndbuf(sc, idx)
1111 1.1 haya struct rl_softc *sc;
1112 1.1 haya int idx;
1113 1.1 haya {
1114 1.1 haya struct mbuf *m_new = NULL;
1115 1.1 haya int error;
1116 1.1 haya
1117 1.1 haya MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1118 1.1 haya if (m_new == NULL) {
1119 1.1 haya printf("%s: no memory for tx list", sc->sc_dev.dv_xname);
1120 1.1 haya return(1);
1121 1.1 haya }
1122 1.1 haya
1123 1.1 haya MCLGET(m_new, M_DONTWAIT);
1124 1.1 haya if (!(m_new->m_flags & M_EXT)) {
1125 1.1 haya m_freem(m_new);
1126 1.1 haya printf("%s: no memory for tx list", sc->sc_dev.dv_xname);
1127 1.1 haya return(1);
1128 1.1 haya }
1129 1.1 haya
1130 1.1 haya if ((error = bus_dmamap_create(sc->sc_dmat,
1131 1.1 haya MCLBYTES, 1,
1132 1.1 haya MCLBYTES, 0, BUS_DMA_NOWAIT,
1133 1.1 haya &sc->snd_dmamap[idx])) != 0) {
1134 1.1 haya printf("%s: can't create snd buffer DMA map, error = %d\n",
1135 1.1 haya sc->sc_dev.dv_xname, error);
1136 1.1 haya return (1);
1137 1.1 haya }
1138 1.1 haya
1139 1.1 haya if ((error = bus_dmamap_load(sc->sc_dmat, sc->snd_dmamap[idx],
1140 1.1 haya mtod(m_new, caddr_t), MCLBYTES, NULL,
1141 1.1 haya BUS_DMA_NOWAIT)) != 0) {
1142 1.1 haya printf("%s: can't load snd buffer DMA map, error = %d\n",
1143 1.1 haya sc->sc_dev.dv_xname, error);
1144 1.1 haya return (1);
1145 1.1 haya }
1146 1.1 haya
1147 1.1 haya sc->sndbuf[idx] = m_new;
1148 1.1 haya return (0);
1149 1.1 haya }
1150 1.1 haya
1151 1.1 haya /*
1152 1.1 haya * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1153 1.1 haya * pointers to the fragment pointers.
1154 1.1 haya */
1155 1.1 haya STATIC int rl_encap(sc, m_head)
1156 1.1 haya struct rl_softc *sc;
1157 1.1 haya struct mbuf *m_head;
1158 1.1 haya {
1159 1.1 haya struct mbuf *m_new;
1160 1.1 haya
1161 1.1 haya /*
1162 1.1 haya * The RealTek is brain damaged and wants longword-aligned
1163 1.1 haya * TX buffers, plus we can only have one fragment buffer
1164 1.1 haya * per packet. We have to copy pretty much all the time.
1165 1.1 haya */
1166 1.1 haya
1167 1.1 haya m_new = sc->sndbuf[sc->rl_cdata.cur_tx];
1168 1.1 haya
1169 1.1 haya m_copydata(m_head, 0, m_head->m_pkthdr.len,
1170 1.1 haya mtod(m_new, caddr_t));
1171 1.1 haya m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1172 1.1 haya m_freem(m_head);
1173 1.1 haya m_head = m_new;
1174 1.1 haya
1175 1.1 haya /* Pad frames to at least 60 bytes. */
1176 1.1 haya if (m_head->m_pkthdr.len < RL_MIN_FRAMELEN) {
1177 1.1 haya m_head->m_pkthdr.len +=
1178 1.1 haya (RL_MIN_FRAMELEN - m_head->m_pkthdr.len);
1179 1.1 haya m_head->m_len = m_head->m_pkthdr.len;
1180 1.1 haya }
1181 1.1 haya
1182 1.1 haya RL_CUR_TXMBUF(sc) = m_head;
1183 1.1 haya
1184 1.1 haya return(0);
1185 1.1 haya }
1186 1.1 haya
1187 1.1 haya /*
1188 1.1 haya * Main transmit routine.
1189 1.1 haya */
1190 1.1 haya
1191 1.1 haya STATIC void rl_start(ifp)
1192 1.1 haya struct ifnet *ifp;
1193 1.1 haya {
1194 1.1 haya struct rl_softc *sc;
1195 1.1 haya struct mbuf *m_head = NULL;
1196 1.1 haya
1197 1.1 haya sc = ifp->if_softc;
1198 1.1 haya
1199 1.1 haya while(RL_CUR_TXMBUF(sc) == NULL) {
1200 1.1 haya IF_DEQUEUE(&ifp->if_snd, m_head);
1201 1.1 haya if (m_head == NULL)
1202 1.1 haya break;
1203 1.1 haya
1204 1.1 haya rl_encap(sc, m_head);
1205 1.1 haya
1206 1.1 haya #if NBPFILTER > 0
1207 1.1 haya /*
1208 1.1 haya * If there's a BPF listener, bounce a copy of this frame
1209 1.1 haya * to him.
1210 1.1 haya */
1211 1.1 haya if (ifp->if_bpf)
1212 1.1 haya bpf_mtap(ifp->if_bpf, RL_CUR_TXMBUF(sc));
1213 1.1 haya #endif
1214 1.1 haya /*
1215 1.1 haya * Transmit the frame.
1216 1.1 haya */
1217 1.1 haya CSR_WRITE_4(sc, RL_CUR_TXADDR(sc),
1218 1.1 haya sc->snd_dmamap[sc->rl_cdata.cur_tx]->dm_segs[0].ds_addr);
1219 1.1 haya CSR_WRITE_4(sc, RL_CUR_TXSTAT(sc),
1220 1.1 haya RL_TX_EARLYTHRESH | RL_CUR_TXMBUF(sc)->m_pkthdr.len);
1221 1.1 haya
1222 1.1 haya RL_INC(sc->rl_cdata.cur_tx);
1223 1.1 haya }
1224 1.1 haya
1225 1.1 haya /*
1226 1.1 haya * We broke out of the loop because all our TX slots are
1227 1.1 haya * full. Mark the NIC as busy until it drains some of the
1228 1.1 haya * packets from the queue.
1229 1.1 haya */
1230 1.1 haya if (RL_CUR_TXMBUF(sc) != NULL)
1231 1.1 haya ifp->if_flags |= IFF_OACTIVE;
1232 1.1 haya
1233 1.1 haya /*
1234 1.1 haya * Set a timeout in case the chip goes out to lunch.
1235 1.1 haya */
1236 1.1 haya ifp->if_timer = 5;
1237 1.1 haya
1238 1.1 haya return;
1239 1.1 haya }
1240 1.1 haya
1241 1.1 haya STATIC void rl_init(xsc)
1242 1.1 haya void *xsc;
1243 1.1 haya {
1244 1.1 haya struct rl_softc *sc = xsc;
1245 1.1 haya struct ifnet *ifp = &sc->ethercom.ec_if;
1246 1.1 haya int s, i;
1247 1.1 haya u_int32_t rxcfg = 0;
1248 1.1 haya u_int16_t phy_bmcr = 0;
1249 1.1 haya
1250 1.1 haya s = splimp();
1251 1.1 haya
1252 1.1 haya /*
1253 1.1 haya * XXX Hack for the 8139: the built-in autoneg logic's state
1254 1.1 haya * gets reset by rl_init() when we don't want it to. Try
1255 1.1 haya * to preserve it.
1256 1.1 haya */
1257 1.1 haya if (sc->rl_type == RL_8139)
1258 1.1 haya phy_bmcr = rl_phy_readreg((struct device *)sc, 7, MII_BMCR);
1259 1.1 haya
1260 1.1 haya /*
1261 1.1 haya * Cancel pending I/O and free all RX/TX buffers.
1262 1.1 haya */
1263 1.1 haya rl_stop(sc);
1264 1.1 haya
1265 1.1 haya /* Init our MAC address */
1266 1.1 haya for (i = 0; i < ETHER_ADDR_LEN; i++) {
1267 1.1 haya CSR_WRITE_1(sc, RL_IDR0 + i, LLADDR(ifp->if_sadl)[i]);
1268 1.1 haya }
1269 1.1 haya
1270 1.1 haya /* Init the RX buffer pointer register. */
1271 1.1 haya CSR_WRITE_4(sc, RL_RXADDR, sc->recv_dmamap->dm_segs[0].ds_addr);
1272 1.1 haya
1273 1.1 haya /* Init TX descriptors. */
1274 1.1 haya rl_list_tx_init(sc);
1275 1.1 haya
1276 1.1 haya /*
1277 1.1 haya * Enable transmit and receive.
1278 1.1 haya */
1279 1.1 haya CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
1280 1.1 haya
1281 1.1 haya /*
1282 1.1 haya * Set the initial TX and RX configuration.
1283 1.1 haya */
1284 1.1 haya CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
1285 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
1286 1.1 haya
1287 1.1 haya /* Set the individual bit to receive frames for this host only. */
1288 1.1 haya rxcfg = CSR_READ_4(sc, RL_RXCFG);
1289 1.1 haya rxcfg |= RL_RXCFG_RX_INDIV;
1290 1.1 haya
1291 1.1 haya /* If we want promiscuous mode, set the allframes bit. */
1292 1.1 haya if (ifp->if_flags & IFF_PROMISC) {
1293 1.1 haya rxcfg |= RL_RXCFG_RX_ALLPHYS;
1294 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1295 1.1 haya } else {
1296 1.1 haya rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
1297 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1298 1.1 haya }
1299 1.1 haya
1300 1.1 haya /*
1301 1.1 haya * Set capture broadcast bit to capture broadcast frames.
1302 1.1 haya */
1303 1.1 haya if (ifp->if_flags & IFF_BROADCAST) {
1304 1.1 haya rxcfg |= RL_RXCFG_RX_BROAD;
1305 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1306 1.1 haya } else {
1307 1.1 haya rxcfg &= ~RL_RXCFG_RX_BROAD;
1308 1.1 haya CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
1309 1.1 haya }
1310 1.1 haya
1311 1.1 haya /*
1312 1.1 haya * Program the multicast filter, if necessary.
1313 1.1 haya */
1314 1.1 haya rl_setmulti(sc);
1315 1.1 haya
1316 1.1 haya /*
1317 1.1 haya * Enable interrupts.
1318 1.1 haya */
1319 1.1 haya CSR_WRITE_2(sc, RL_IMR, RL_INTRS);
1320 1.1 haya
1321 1.1 haya /* Start RX/TX process. */
1322 1.1 haya CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
1323 1.1 haya
1324 1.1 haya /* Enable receiver and transmitter. */
1325 1.1 haya CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
1326 1.1 haya
1327 1.1 haya /* Restore state of BMCR */
1328 1.1 haya if (sc->rl_type == RL_8139)
1329 1.1 haya rl_phy_writereg((struct device *)sc, 7, MII_BMCR, phy_bmcr);
1330 1.1 haya
1331 1.1 haya CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
1332 1.1 haya
1333 1.1 haya /*
1334 1.1 haya * Set current media.
1335 1.1 haya */
1336 1.1 haya mii_mediachg(&sc->mii);
1337 1.1 haya
1338 1.1 haya ifp->if_flags |= IFF_RUNNING;
1339 1.1 haya ifp->if_flags &= ~IFF_OACTIVE;
1340 1.1 haya
1341 1.1 haya (void)splx(s);
1342 1.1 haya
1343 1.1 haya callout_reset(&sc->rl_tick_ch, hz, rl_tick, sc);
1344 1.1 haya }
1345 1.1 haya
1346 1.1 haya /*
1347 1.1 haya * Set media options.
1348 1.1 haya */
1349 1.1 haya STATIC int rl_ifmedia_upd(ifp)
1350 1.1 haya struct ifnet *ifp;
1351 1.1 haya {
1352 1.1 haya struct rl_softc *sc;
1353 1.1 haya struct ifmedia *ifm;
1354 1.1 haya
1355 1.1 haya sc = ifp->if_softc;
1356 1.1 haya ifm = &sc->mii.mii_media;
1357 1.1 haya
1358 1.1 haya if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1359 1.1 haya return(EINVAL);
1360 1.1 haya
1361 1.1 haya return (mii_mediachg(&sc->mii));
1362 1.1 haya }
1363 1.1 haya
1364 1.1 haya /*
1365 1.1 haya * Report current media status.
1366 1.1 haya */
1367 1.1 haya STATIC void rl_ifmedia_sts(ifp, ifmr)
1368 1.1 haya struct ifnet *ifp;
1369 1.1 haya struct ifmediareq *ifmr;
1370 1.1 haya {
1371 1.1 haya struct rl_softc *sc;
1372 1.1 haya
1373 1.1 haya sc = ifp->if_softc;
1374 1.1 haya
1375 1.1 haya mii_pollstat(&sc->mii);
1376 1.1 haya ifmr->ifm_status = sc->mii.mii_media_status;
1377 1.1 haya ifmr->ifm_active = sc->mii.mii_media_active;
1378 1.1 haya }
1379 1.1 haya
1380 1.1 haya STATIC int
1381 1.1 haya rl_ether_ioctl(ifp, cmd, data)
1382 1.1 haya struct ifnet *ifp;
1383 1.1 haya u_long cmd;
1384 1.1 haya caddr_t data;
1385 1.1 haya {
1386 1.1 haya struct ifaddr *ifa = (struct ifaddr *) data;
1387 1.1 haya struct rl_softc *sc = ifp->if_softc;
1388 1.1 haya
1389 1.1 haya switch (cmd) {
1390 1.1 haya case SIOCSIFADDR:
1391 1.1 haya ifp->if_flags |= IFF_UP;
1392 1.1 haya
1393 1.1 haya switch (ifa->ifa_addr->sa_family) {
1394 1.1 haya #ifdef INET
1395 1.1 haya case AF_INET:
1396 1.1 haya rl_init(sc);
1397 1.1 haya arp_ifinit(ifp, ifa);
1398 1.1 haya break;
1399 1.1 haya #endif
1400 1.1 haya #ifdef NS
1401 1.1 haya case AF_NS:
1402 1.1 haya {
1403 1.2 tsutsui struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1404 1.1 haya
1405 1.1 haya if (ns_nullhost(*ina))
1406 1.1 haya ina->x_host = *(union ns_host *)
1407 1.1 haya LLADDR(ifp->if_sadl);
1408 1.1 haya else
1409 1.1 haya bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1410 1.1 haya ifp->if_addrlen);
1411 1.1 haya /* Set new address. */
1412 1.1 haya rl_init(sc);
1413 1.1 haya break;
1414 1.1 haya }
1415 1.1 haya #endif
1416 1.1 haya default:
1417 1.1 haya rl_init(sc);
1418 1.1 haya break;
1419 1.1 haya }
1420 1.1 haya break;
1421 1.1 haya
1422 1.1 haya default:
1423 1.1 haya return (EINVAL);
1424 1.1 haya }
1425 1.1 haya
1426 1.1 haya return (0);
1427 1.1 haya }
1428 1.1 haya
1429 1.1 haya STATIC int rl_ioctl(ifp, command, data)
1430 1.1 haya struct ifnet *ifp;
1431 1.1 haya u_long command;
1432 1.1 haya caddr_t data;
1433 1.1 haya {
1434 1.1 haya struct rl_softc *sc = ifp->if_softc;
1435 1.1 haya struct ifreq *ifr = (struct ifreq *) data;
1436 1.1 haya int s, error = 0;
1437 1.1 haya
1438 1.1 haya s = splimp();
1439 1.1 haya
1440 1.1 haya switch(command) {
1441 1.1 haya case SIOCSIFADDR:
1442 1.1 haya case SIOCGIFADDR:
1443 1.1 haya case SIOCSIFMTU:
1444 1.1 haya error = rl_ether_ioctl(ifp, command, data);
1445 1.1 haya break;
1446 1.1 haya case SIOCSIFFLAGS:
1447 1.1 haya if (ifp->if_flags & IFF_UP) {
1448 1.1 haya rl_init(sc);
1449 1.1 haya } else {
1450 1.1 haya if (ifp->if_flags & IFF_RUNNING)
1451 1.1 haya rl_stop(sc);
1452 1.1 haya }
1453 1.1 haya error = 0;
1454 1.1 haya break;
1455 1.1 haya case SIOCADDMULTI:
1456 1.1 haya case SIOCDELMULTI:
1457 1.1 haya rl_setmulti(sc);
1458 1.1 haya error = 0;
1459 1.1 haya break;
1460 1.1 haya case SIOCGIFMEDIA:
1461 1.1 haya case SIOCSIFMEDIA:
1462 1.1 haya error = ifmedia_ioctl(ifp, ifr, &sc->mii.mii_media, command);
1463 1.1 haya break;
1464 1.1 haya default:
1465 1.1 haya error = EINVAL;
1466 1.1 haya break;
1467 1.1 haya }
1468 1.1 haya
1469 1.1 haya (void)splx(s);
1470 1.1 haya
1471 1.1 haya return(error);
1472 1.1 haya }
1473 1.1 haya
1474 1.1 haya STATIC void rl_watchdog(ifp)
1475 1.1 haya struct ifnet *ifp;
1476 1.1 haya {
1477 1.1 haya struct rl_softc *sc;
1478 1.1 haya
1479 1.1 haya sc = ifp->if_softc;
1480 1.1 haya
1481 1.1 haya printf("%s: watchdog timeout\n", sc->sc_dev.dv_xname);
1482 1.1 haya ifp->if_oerrors++;
1483 1.1 haya rl_txeof(sc);
1484 1.1 haya rl_rxeof(sc);
1485 1.1 haya rl_init(sc);
1486 1.1 haya
1487 1.1 haya return;
1488 1.1 haya }
1489 1.1 haya
1490 1.1 haya /*
1491 1.1 haya * Stop the adapter and free any mbufs allocated to the
1492 1.1 haya * RX and TX lists.
1493 1.1 haya */
1494 1.1 haya STATIC void rl_stop(sc)
1495 1.1 haya struct rl_softc *sc;
1496 1.1 haya {
1497 1.2 tsutsui int i;
1498 1.1 haya struct ifnet *ifp;
1499 1.1 haya
1500 1.1 haya ifp = &sc->ethercom.ec_if;
1501 1.1 haya ifp->if_timer = 0;
1502 1.1 haya
1503 1.1 haya callout_stop(&sc->rl_tick_ch);
1504 1.1 haya
1505 1.1 haya mii_down(&sc->mii);
1506 1.1 haya
1507 1.1 haya CSR_WRITE_1(sc, RL_COMMAND, 0x00);
1508 1.1 haya CSR_WRITE_2(sc, RL_IMR, 0x0000);
1509 1.1 haya
1510 1.1 haya /*
1511 1.1 haya * Free the TX list buffers.
1512 1.1 haya */
1513 1.1 haya for (i = 0; i < RL_TX_LIST_CNT; i++) {
1514 1.1 haya if (sc->rl_cdata.rl_tx_chain[i] != NULL) {
1515 1.1 haya m_freem(sc->rl_cdata.rl_tx_chain[i]);
1516 1.1 haya sc->rl_cdata.rl_tx_chain[i] = NULL;
1517 1.1 haya CSR_WRITE_4(sc, RL_TXADDR0 + i, 0x0000000);
1518 1.1 haya }
1519 1.1 haya }
1520 1.1 haya
1521 1.1 haya ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1522 1.1 haya
1523 1.1 haya return;
1524 1.1 haya }
1525 1.1 haya
1526 1.1 haya /*
1527 1.1 haya * Stop all chip I/O so that the kernel's probe routines don't
1528 1.1 haya * get confused by errant DMAs when rebooting.
1529 1.1 haya */
1530 1.1 haya STATIC void rl_shutdown(vsc)
1531 1.1 haya void *vsc;
1532 1.1 haya {
1533 1.1 haya struct rl_softc *sc = (struct rl_softc *)vsc;
1534 1.1 haya
1535 1.1 haya rl_stop(sc);
1536 1.1 haya
1537 1.1 haya return;
1538 1.1 haya }
1539 1.1 haya
1540 1.1 haya STATIC void
1541 1.1 haya rl_tick(arg)
1542 1.1 haya void *arg;
1543 1.1 haya {
1544 1.1 haya struct rl_softc *sc = arg;
1545 1.1 haya int s = splnet();
1546 1.1 haya
1547 1.1 haya mii_tick(&sc->mii);
1548 1.1 haya splx(s);
1549 1.1 haya
1550 1.1 haya callout_reset(&sc->rl_tick_ch, hz, rl_tick, sc);
1551 1.1 haya }
1552