if_tl.c revision 1.20 1 1.20 thorpej /* $NetBSD: if_tl.c,v 1.20 1998/08/18 08:00:17 thorpej Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
5 1.1 bouyer *
6 1.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1 bouyer * modification, are permitted provided that the following conditions
8 1.1 bouyer * are met:
9 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1 bouyer * notice, this list of conditions and the following disclaimer.
11 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.1 bouyer * documentation and/or other materials provided with the distribution.
14 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
15 1.1 bouyer * must display the following acknowledgement:
16 1.1 bouyer * This product includes software developed by Manuel Bouyer.
17 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
18 1.1 bouyer * derived from this software without specific prior written permission.
19 1.1 bouyer *
20 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 bouyer */
31 1.1 bouyer
32 1.1 bouyer /*
33 1.2 bouyer * Texas Instruments ThunderLAN ethernet controller
34 1.1 bouyer * ThunderLAN Programmer's Guide (TI Literature Number SPWU013A)
35 1.1 bouyer * available from www.ti.com
36 1.1 bouyer */
37 1.1 bouyer
38 1.1 bouyer #undef TLDEBUG
39 1.1 bouyer #define TL_PRIV_STATS
40 1.1 bouyer #undef TLDEBUG_RX
41 1.1 bouyer #undef TLDEBUG_TX
42 1.1 bouyer #undef TLDEBUG_ADDR
43 1.12 jonathan
44 1.12 jonathan #include "opt_inet.h"
45 1.13 jonathan #include "opt_ns.h"
46 1.1 bouyer
47 1.1 bouyer #include <sys/param.h>
48 1.1 bouyer #include <sys/systm.h>
49 1.1 bouyer #include <sys/mbuf.h>
50 1.1 bouyer #include <sys/protosw.h>
51 1.1 bouyer #include <sys/socket.h>
52 1.1 bouyer #include <sys/ioctl.h>
53 1.1 bouyer #include <sys/errno.h>
54 1.1 bouyer #include <sys/malloc.h>
55 1.1 bouyer #include <sys/kernel.h>
56 1.1 bouyer #include <sys/proc.h> /* only for declaration of wakeup() used by vm.h */
57 1.1 bouyer #include <sys/device.h>
58 1.1 bouyer
59 1.1 bouyer #include <net/if.h>
60 1.1 bouyer #if defined(SIOCSIFMEDIA)
61 1.1 bouyer #include <net/if_media.h>
62 1.1 bouyer #endif
63 1.1 bouyer #include <net/if_types.h>
64 1.1 bouyer #include <net/if_dl.h>
65 1.1 bouyer #include <net/route.h>
66 1.1 bouyer #include <net/netisr.h>
67 1.1 bouyer
68 1.1 bouyer #include "bpfilter.h"
69 1.1 bouyer #if NBPFILTER > 0
70 1.1 bouyer #include <net/bpf.h>
71 1.1 bouyer #include <net/bpfdesc.h>
72 1.1 bouyer #endif
73 1.1 bouyer
74 1.1 bouyer #ifdef INET
75 1.1 bouyer #include <netinet/in.h>
76 1.1 bouyer #include <netinet/in_systm.h>
77 1.1 bouyer #include <netinet/in_var.h>
78 1.1 bouyer #include <netinet/ip.h>
79 1.1 bouyer #endif
80 1.1 bouyer
81 1.1 bouyer #ifdef NS
82 1.1 bouyer #include <netns/ns.h>
83 1.1 bouyer #include <netns/ns_if.h>
84 1.1 bouyer #endif
85 1.1 bouyer
86 1.1 bouyer #include <vm/vm.h>
87 1.1 bouyer #include <vm/vm_param.h>
88 1.1 bouyer #include <vm/vm_kern.h>
89 1.1 bouyer
90 1.1 bouyer #if defined(__NetBSD__)
91 1.1 bouyer #include <net/if_ether.h>
92 1.1 bouyer #if defined(INET)
93 1.1 bouyer #include <netinet/if_inarp.h>
94 1.1 bouyer #endif
95 1.4 thorpej
96 1.1 bouyer #include <machine/bus.h>
97 1.1 bouyer #include <machine/intr.h>
98 1.4 thorpej
99 1.1 bouyer #include <dev/pci/pcireg.h>
100 1.1 bouyer #include <dev/pci/pcivar.h>
101 1.1 bouyer #include <dev/pci/pcidevs.h>
102 1.15 thorpej
103 1.1 bouyer #include <dev/i2c/i2c_bus.h>
104 1.1 bouyer #include <dev/i2c/i2c_eeprom.h>
105 1.15 thorpej
106 1.15 thorpej #include <dev/mii/mii.h>
107 1.15 thorpej #include <dev/mii/miivar.h>
108 1.15 thorpej
109 1.15 thorpej #include <dev/mii/tlphyvar.h>
110 1.15 thorpej
111 1.1 bouyer #include <dev/pci/if_tlregs.h>
112 1.15 thorpej #include <dev/pci/if_tlvar.h>
113 1.1 bouyer #endif /* __NetBSD__ */
114 1.1 bouyer
115 1.15 thorpej #if defined(__NetBSD__) && defined(__alpha__)
116 1.15 thorpej /* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
117 1.15 thorpej #undef vtophys
118 1.20 thorpej #define vtophys(va) alpha_XXX_dmamap((vaddr_t)(va))
119 1.15 thorpej #endif
120 1.15 thorpej
121 1.1 bouyer /* number of transmit/receive buffers */
122 1.1 bouyer #ifndef TL_NBUF
123 1.1 bouyer #define TL_NBUF 10
124 1.1 bouyer #endif
125 1.1 bouyer
126 1.1 bouyer /* number of seconds the link can be idle */
127 1.1 bouyer #ifndef TL_IDLETIME
128 1.1 bouyer #define TL_IDLETIME 10
129 1.1 bouyer #endif
130 1.1 bouyer
131 1.7 drochner static int tl_pci_match __P((struct device *, struct cfdata *, void *));
132 1.1 bouyer static void tl_pci_attach __P((struct device *, struct device *, void *));
133 1.1 bouyer static int tl_intr __P((void *));
134 1.1 bouyer
135 1.1 bouyer static int tl_ifioctl __P((struct ifnet *, ioctl_cmd_t, caddr_t));
136 1.1 bouyer static int tl_mediachange __P((struct ifnet *));
137 1.1 bouyer static void tl_mediastatus __P((struct ifnet *, struct ifmediareq *));
138 1.1 bouyer static void tl_ifwatchdog __P((struct ifnet *));
139 1.1 bouyer static void tl_shutdown __P((void*));
140 1.1 bouyer
141 1.1 bouyer static void tl_ifstart __P((struct ifnet *));
142 1.1 bouyer static void tl_reset __P((tl_softc_t*));
143 1.1 bouyer static int tl_init __P((tl_softc_t*));
144 1.1 bouyer static void tl_restart __P((void *));
145 1.1 bouyer static int tl_add_RxBuff __P((struct Rx_list*, struct mbuf*));
146 1.1 bouyer static void tl_read_stats __P((tl_softc_t*));
147 1.1 bouyer static void tl_ticks __P((void*));
148 1.1 bouyer static int tl_multicast_hash __P((u_int8_t*));
149 1.1 bouyer static void tl_addr_filter __P((tl_softc_t*));
150 1.1 bouyer
151 1.1 bouyer static u_int32_t tl_intreg_read __P((tl_softc_t*, u_int32_t));
152 1.1 bouyer static void tl_intreg_write __P((tl_softc_t*, u_int32_t, u_int32_t));
153 1.1 bouyer static u_int8_t tl_intreg_read_byte __P((tl_softc_t*, u_int32_t));
154 1.1 bouyer static void tl_intreg_write_byte __P((tl_softc_t*, u_int32_t, u_int8_t));
155 1.1 bouyer
156 1.15 thorpej void tl_mii_sync __P((struct tl_softc *));
157 1.15 thorpej void tl_mii_sendbits __P((struct tl_softc *, u_int32_t, int));
158 1.15 thorpej
159 1.1 bouyer
160 1.1 bouyer #if defined(TLDEBUG_RX)
161 1.1 bouyer static void ether_printheader __P((struct ether_header*));
162 1.1 bouyer #endif
163 1.1 bouyer
164 1.15 thorpej int tl_mii_read __P((struct device *, int, int));
165 1.15 thorpej void tl_mii_write __P((struct device *, int, int, int));
166 1.15 thorpej
167 1.15 thorpej void tl_statchg __P((struct device *));
168 1.1 bouyer
169 1.1 bouyer void tl_i2c_set __P((void*, u_int8_t));
170 1.1 bouyer void tl_i2c_clr __P((void*, u_int8_t));
171 1.1 bouyer int tl_i2c_read __P((void*, u_int8_t));
172 1.1 bouyer
173 1.1 bouyer static __inline void netsio_clr __P((tl_softc_t*, u_int8_t));
174 1.1 bouyer static __inline void netsio_set __P((tl_softc_t*, u_int8_t));
175 1.1 bouyer static __inline u_int8_t netsio_read __P((tl_softc_t*, u_int8_t));
176 1.1 bouyer static __inline void netsio_clr(sc, bits)
177 1.1 bouyer tl_softc_t* sc;
178 1.1 bouyer u_int8_t bits;
179 1.1 bouyer {
180 1.1 bouyer tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio,
181 1.17 bouyer tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & (~bits));
182 1.1 bouyer }
183 1.1 bouyer static __inline void netsio_set(sc, bits)
184 1.1 bouyer tl_softc_t* sc;
185 1.1 bouyer u_int8_t bits;
186 1.1 bouyer {
187 1.1 bouyer tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio,
188 1.17 bouyer tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) | bits);
189 1.1 bouyer }
190 1.1 bouyer static __inline u_int8_t netsio_read(sc, bits)
191 1.1 bouyer tl_softc_t* sc;
192 1.1 bouyer u_int8_t bits;
193 1.1 bouyer {
194 1.4 thorpej return (tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & bits);
195 1.1 bouyer }
196 1.1 bouyer
197 1.1 bouyer struct cfattach tl_ca = {
198 1.4 thorpej sizeof(tl_softc_t), tl_pci_match, tl_pci_attach
199 1.1 bouyer };
200 1.1 bouyer
201 1.4 thorpej const struct tl_product_desc tl_compaq_products[] = {
202 1.15 thorpej { PCI_PRODUCT_COMPAQ_N100TX, TLPHY_MEDIA_NO_10_T,
203 1.10 thorpej 0, "Compaq Netelligent 10/100 TX" },
204 1.15 thorpej { PCI_PRODUCT_COMPAQ_N10T, TLPHY_MEDIA_10_5,
205 1.10 thorpej 0, "Compaq Netelligent 10 T" },
206 1.15 thorpej { PCI_PRODUCT_COMPAQ_IntNF3P, TLPHY_MEDIA_10_2,
207 1.10 thorpej 0, "Compaq Integrated NetFlex 3/P" },
208 1.15 thorpej { PCI_PRODUCT_COMPAQ_IntPL100TX, TLPHY_MEDIA_10_2|TLPHY_MEDIA_NO_10_T,
209 1.10 thorpej 0, "Compaq ProLiant Integrated Netelligent 10/100 TX" },
210 1.15 thorpej { PCI_PRODUCT_COMPAQ_DPNet100TX, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T,
211 1.10 thorpej 0, "Compaq Dual Port Netelligent 10/100 TX" },
212 1.15 thorpej { PCI_PRODUCT_COMPAQ_DP4000, TLPHY_MEDIA_10_5,
213 1.10 thorpej 0, "Compaq Deskpro 4000 5233MMX" },
214 1.15 thorpej { PCI_PRODUCT_COMPAQ_NF3P_BNC, TLPHY_MEDIA_10_2,
215 1.10 thorpej 0, "Compaq NetFlex 3/P w/ BNC" },
216 1.15 thorpej { PCI_PRODUCT_COMPAQ_NF3P, TLPHY_MEDIA_10_5,
217 1.10 thorpej 0, "Compaq NetFlex 3/P" },
218 1.4 thorpej { 0, 0, NULL },
219 1.4 thorpej };
220 1.4 thorpej
221 1.4 thorpej const struct tl_product_desc tl_ti_products[] = {
222 1.10 thorpej /*
223 1.10 thorpej * Built-in Ethernet on the TI TravelMate 5000
224 1.10 thorpej * docking station; better product description?
225 1.10 thorpej * XXX Seems to have broken memory-mapped access.
226 1.10 thorpej */
227 1.15 thorpej { PCI_PRODUCT_TI_TLAN, 0,
228 1.10 thorpej TPF_BROKEN_MEM, "Texas Instruments ThunderLAN" },
229 1.4 thorpej { 0, 0, NULL },
230 1.4 thorpej };
231 1.4 thorpej
232 1.4 thorpej struct tl_vendor_desc {
233 1.4 thorpej u_int32_t tv_vendor;
234 1.4 thorpej const struct tl_product_desc *tv_products;
235 1.4 thorpej };
236 1.4 thorpej
237 1.4 thorpej const struct tl_vendor_desc tl_vendors[] = {
238 1.4 thorpej { PCI_VENDOR_COMPAQ, tl_compaq_products },
239 1.4 thorpej { PCI_VENDOR_TI, tl_ti_products },
240 1.4 thorpej { 0, NULL },
241 1.4 thorpej };
242 1.4 thorpej
243 1.4 thorpej const struct tl_product_desc *tl_lookup_product __P((u_int32_t));
244 1.4 thorpej
245 1.4 thorpej const struct tl_product_desc *
246 1.4 thorpej tl_lookup_product(id)
247 1.4 thorpej u_int32_t id;
248 1.4 thorpej {
249 1.4 thorpej const struct tl_product_desc *tp;
250 1.4 thorpej const struct tl_vendor_desc *tv;
251 1.4 thorpej
252 1.4 thorpej for (tv = tl_vendors; tv->tv_products != NULL; tv++)
253 1.4 thorpej if (PCI_VENDOR(id) == tv->tv_vendor)
254 1.4 thorpej break;
255 1.4 thorpej
256 1.4 thorpej if ((tp = tv->tv_products) == NULL)
257 1.4 thorpej return (NULL);
258 1.4 thorpej
259 1.4 thorpej for (; tp->tp_desc != NULL; tp++)
260 1.4 thorpej if (PCI_PRODUCT(id) == tp->tp_product)
261 1.4 thorpej break;
262 1.4 thorpej
263 1.4 thorpej if (tp->tp_desc == NULL)
264 1.4 thorpej return (NULL);
265 1.4 thorpej
266 1.4 thorpej return (tp);
267 1.4 thorpej }
268 1.4 thorpej
269 1.6 bouyer static char *nullbuf = NULL;
270 1.1 bouyer
271 1.1 bouyer static int
272 1.4 thorpej tl_pci_match(parent, match, aux)
273 1.1 bouyer struct device *parent;
274 1.7 drochner struct cfdata *match;
275 1.1 bouyer void *aux;
276 1.1 bouyer {
277 1.1 bouyer struct pci_attach_args *pa = (struct pci_attach_args *) aux;
278 1.1 bouyer
279 1.4 thorpej if (tl_lookup_product(pa->pa_id) != NULL)
280 1.4 thorpej return (1);
281 1.4 thorpej
282 1.4 thorpej return (0);
283 1.1 bouyer }
284 1.1 bouyer
285 1.1 bouyer static void
286 1.1 bouyer tl_pci_attach(parent, self, aux)
287 1.1 bouyer struct device * parent;
288 1.1 bouyer struct device * self;
289 1.1 bouyer void * aux;
290 1.1 bouyer {
291 1.1 bouyer tl_softc_t *sc = (tl_softc_t *)self;
292 1.1 bouyer struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
293 1.4 thorpej const struct tl_product_desc *tp;
294 1.1 bouyer struct ifnet * const ifp = &sc->tl_if;
295 1.1 bouyer bus_space_tag_t iot, memt;
296 1.1 bouyer bus_space_handle_t ioh, memh;
297 1.1 bouyer pci_intr_handle_t intrhandle;
298 1.4 thorpej const char *intrstr;
299 1.4 thorpej int i, tmp, ioh_valid, memh_valid;
300 1.4 thorpej pcireg_t csr;
301 1.4 thorpej
302 1.4 thorpej printf("\n");
303 1.4 thorpej
304 1.10 thorpej tp = tl_lookup_product(pa->pa_id);
305 1.10 thorpej if (tp == NULL)
306 1.10 thorpej panic("tl_pci_attach: impossible");
307 1.15 thorpej sc->tl_product = tp;
308 1.10 thorpej
309 1.4 thorpej /* Map the card space. */
310 1.4 thorpej ioh_valid = (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
311 1.4 thorpej &iot, &ioh, NULL, NULL) == 0);
312 1.4 thorpej memh_valid = (pci_mapreg_map(pa, PCI_CBMA,
313 1.4 thorpej PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
314 1.4 thorpej 0, &memt, &memh, NULL, NULL) == 0);
315 1.4 thorpej
316 1.10 thorpej if (memh_valid && (tp->tp_flags & TPF_BROKEN_MEM) == 0) {
317 1.4 thorpej sc->tl_bustag = memt;
318 1.4 thorpej sc->tl_bushandle = memh;
319 1.4 thorpej } else if (ioh_valid) {
320 1.4 thorpej sc->tl_bustag = iot;
321 1.4 thorpej sc->tl_bushandle = ioh;
322 1.1 bouyer } else {
323 1.4 thorpej printf("%s: unable to map device registers\n",
324 1.4 thorpej sc->sc_dev.dv_xname);
325 1.4 thorpej return;
326 1.1 bouyer }
327 1.1 bouyer
328 1.4 thorpej /* Enable the device. */
329 1.4 thorpej csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
330 1.4 thorpej pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
331 1.4 thorpej csr | PCI_COMMAND_MASTER_ENABLE);
332 1.1 bouyer
333 1.4 thorpej printf("%s: %s\n", sc->sc_dev.dv_xname, tp->tp_desc);
334 1.1 bouyer
335 1.1 bouyer tl_reset(sc);
336 1.1 bouyer
337 1.1 bouyer /* fill in the i2c struct */
338 1.1 bouyer sc->i2cbus.adapter_softc = sc;
339 1.1 bouyer sc->i2cbus.set_bit = tl_i2c_set;
340 1.1 bouyer sc->i2cbus.clr_bit = tl_i2c_clr;
341 1.1 bouyer sc->i2cbus.read_bit = tl_i2c_read;
342 1.1 bouyer
343 1.1 bouyer #ifdef TLDEBUG
344 1.1 bouyer printf("default values of INTreg: 0x%x\n",
345 1.17 bouyer tl_intreg_read(sc, TL_INT_Defaults));
346 1.1 bouyer #endif
347 1.1 bouyer
348 1.1 bouyer /* read mac addr */
349 1.1 bouyer for (i=0; i<ETHER_ADDR_LEN; i++) {
350 1.1 bouyer tmp = i2c_eeprom_read(&sc->i2cbus, 0x83 + i);
351 1.1 bouyer if (tmp < 0) {
352 1.4 thorpej printf("%s: error reading Ethernet adress\n",
353 1.4 thorpej sc->sc_dev.dv_xname);
354 1.1 bouyer return;
355 1.1 bouyer } else {
356 1.1 bouyer sc->tl_enaddr[i] = tmp;
357 1.1 bouyer }
358 1.1 bouyer }
359 1.4 thorpej printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
360 1.17 bouyer ether_sprintf(sc->tl_enaddr));
361 1.1 bouyer
362 1.4 thorpej /* Map and establish interrupts */
363 1.4 thorpej if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
364 1.17 bouyer pa->pa_intrline, &intrhandle)) {
365 1.4 thorpej printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
366 1.4 thorpej return;
367 1.4 thorpej }
368 1.4 thorpej intrstr = pci_intr_string(pa->pa_pc, intrhandle);
369 1.4 thorpej sc->tl_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
370 1.17 bouyer tl_intr, sc);
371 1.4 thorpej if (sc->tl_ih == NULL) {
372 1.4 thorpej printf("%s: couldn't establish interrupt",
373 1.4 thorpej sc->sc_dev.dv_xname);
374 1.4 thorpej if (intrstr != NULL)
375 1.4 thorpej printf(" at %s", intrstr);
376 1.4 thorpej printf("\n");
377 1.4 thorpej return;
378 1.4 thorpej }
379 1.4 thorpej printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
380 1.4 thorpej
381 1.4 thorpej /*
382 1.4 thorpej * Add shutdown hook so that DMA is disabled prior to reboot. Not
383 1.4 thorpej * doing do could allow DMA to corrupt kernel memory during the
384 1.4 thorpej * reboot before the driver initializes.
385 1.4 thorpej */
386 1.4 thorpej (void) shutdownhook_establish(tl_shutdown, sc);
387 1.4 thorpej
388 1.15 thorpej /*
389 1.15 thorpej * Initialize our media structures and probe the MII.
390 1.15 thorpej *
391 1.15 thorpej * Note that we don't care about the media instance. We
392 1.15 thorpej * are expecting to have multiple PHYs on the 10/100 cards,
393 1.15 thorpej * and on those cards we exclude the internal PHY from providing
394 1.15 thorpej * 10baseT. By ignoring the instance, it allows us to not have
395 1.15 thorpej * to specify it on the command line when switching media.
396 1.15 thorpej */
397 1.15 thorpej sc->tl_mii.mii_ifp = ifp;
398 1.15 thorpej sc->tl_mii.mii_readreg = tl_mii_read;
399 1.15 thorpej sc->tl_mii.mii_writereg = tl_mii_write;
400 1.15 thorpej sc->tl_mii.mii_statchg = tl_statchg;
401 1.15 thorpej ifmedia_init(&sc->tl_mii.mii_media, IFM_IMASK, tl_mediachange,
402 1.15 thorpej tl_mediastatus);
403 1.15 thorpej mii_phy_probe(self, &sc->tl_mii, 0xffffffff);
404 1.17 bouyer #ifndef notyet /* XXX XXX XXX */
405 1.15 thorpej if (LIST_FIRST(&sc->tl_mii.mii_phys) == NULL) {
406 1.15 thorpej ifmedia_add(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
407 1.15 thorpej ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE);
408 1.15 thorpej } else
409 1.15 thorpej ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_AUTO);
410 1.15 thorpej #else
411 1.15 thorpej ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE);
412 1.15 thorpej #endif
413 1.1 bouyer
414 1.1 bouyer bcopy(sc->sc_dev.dv_xname, sc->tl_if.if_xname, IFNAMSIZ);
415 1.1 bouyer sc->tl_if.if_softc = sc;
416 1.1 bouyer ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
417 1.1 bouyer ifp->if_ioctl = tl_ifioctl;
418 1.1 bouyer ifp->if_start = tl_ifstart;
419 1.1 bouyer ifp->if_watchdog = tl_ifwatchdog;
420 1.1 bouyer ifp->if_timer = 0;
421 1.1 bouyer if_attach(ifp);
422 1.1 bouyer ether_ifattach(&(sc)->tl_if, (sc)->tl_enaddr);
423 1.1 bouyer #if NBPFILTER > 0
424 1.1 bouyer bpfattach(&sc->tl_bpf, &sc->tl_if, DLT_EN10MB,
425 1.17 bouyer sizeof(struct ether_header));
426 1.1 bouyer #endif
427 1.1 bouyer }
428 1.1 bouyer
429 1.1 bouyer static void
430 1.1 bouyer tl_reset(sc)
431 1.1 bouyer tl_softc_t *sc;
432 1.1 bouyer {
433 1.1 bouyer int i;
434 1.1 bouyer
435 1.1 bouyer /* read stats */
436 1.1 bouyer if (sc->tl_if.if_flags & IFF_RUNNING) {
437 1.1 bouyer untimeout(tl_ticks, sc);
438 1.1 bouyer tl_read_stats(sc);
439 1.1 bouyer }
440 1.1 bouyer /* Reset adapter */
441 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD,
442 1.17 bouyer TL_HR_READ(sc, TL_HOST_CMD) | HOST_CMD_Ad_Rst);
443 1.1 bouyer DELAY(100000);
444 1.1 bouyer /* Disable interrupts */
445 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
446 1.1 bouyer /* setup aregs & hash */
447 1.1 bouyer for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
448 1.1 bouyer tl_intreg_write(sc, i, 0);
449 1.1 bouyer #ifdef TLDEBUG_ADDR
450 1.1 bouyer printf("Areg & hash registers: \n");
451 1.1 bouyer for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
452 1.1 bouyer printf(" reg %x: %x\n", i, tl_intreg_read(sc, i));
453 1.1 bouyer #endif
454 1.1 bouyer /* Setup NetConfig */
455 1.1 bouyer tl_intreg_write(sc, TL_INT_NetConfig,
456 1.17 bouyer TL_NETCONFIG_1F | TL_NETCONFIG_1chn | TL_NETCONFIG_PHY_EN);
457 1.1 bouyer /* Bsize: accept default */
458 1.1 bouyer /* TX commit in Acommit: accept default */
459 1.1 bouyer /* Load Ld_tmr and Ld_thr */
460 1.1 bouyer /* Ld_tmr = 3 */
461 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, 0x3 | HOST_CMD_LdTmr);
462 1.1 bouyer /* Ld_thr = 0 */
463 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, 0x0 | HOST_CMD_LdThr);
464 1.1 bouyer /* Unreset MII */
465 1.1 bouyer netsio_set(sc, TL_NETSIO_NMRST);
466 1.1 bouyer DELAY(100000);
467 1.15 thorpej sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
468 1.1 bouyer sc->tl_flags = 0;
469 1.1 bouyer sc->opkt = 0;
470 1.1 bouyer sc->stats_exesscoll = 0;
471 1.1 bouyer }
472 1.1 bouyer
473 1.1 bouyer static void tl_shutdown(v)
474 1.1 bouyer void *v;
475 1.1 bouyer {
476 1.1 bouyer tl_softc_t *sc = v;
477 1.1 bouyer struct Tx_list *Tx;
478 1.1 bouyer int i;
479 1.1 bouyer
480 1.1 bouyer if ((sc->tl_if.if_flags & IFF_RUNNING) == 0)
481 1.1 bouyer return;
482 1.1 bouyer /* disable interrupts */
483 1.17 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
484 1.1 bouyer /* stop TX and RX channels */
485 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD,
486 1.17 bouyer HOST_CMD_STOP | HOST_CMD_RT | HOST_CMD_Nes);
487 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_STOP);
488 1.1 bouyer DELAY(100000);
489 1.1 bouyer
490 1.1 bouyer /* stop statistics reading loop, read stats */
491 1.1 bouyer untimeout(tl_ticks, sc);
492 1.1 bouyer tl_read_stats(sc);
493 1.1 bouyer
494 1.1 bouyer /* deallocate memory allocations */
495 1.1 bouyer for (i=0; i< TL_NBUF; i++) {
496 1.1 bouyer if (sc->Rx_list[i].m)
497 1.1 bouyer m_freem(sc->Rx_list[i].m);
498 1.17 bouyer sc->Rx_list[i].m = NULL;
499 1.1 bouyer }
500 1.1 bouyer free(sc->Rx_list, M_DEVBUF);
501 1.1 bouyer sc->Rx_list = NULL;
502 1.1 bouyer while ((Tx = sc->active_Tx) != NULL) {
503 1.1 bouyer Tx->hw_list.stat = 0;
504 1.1 bouyer m_freem(Tx->m);
505 1.1 bouyer sc->active_Tx = Tx->next;
506 1.1 bouyer Tx->next = sc->Free_Tx;
507 1.1 bouyer sc->Free_Tx = Tx;
508 1.1 bouyer }
509 1.1 bouyer sc->last_Tx = NULL;
510 1.1 bouyer free(sc->Tx_list, M_DEVBUF);
511 1.1 bouyer sc->Tx_list = NULL;
512 1.1 bouyer sc->tl_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
513 1.15 thorpej sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
514 1.1 bouyer sc->tl_flags = 0;
515 1.1 bouyer }
516 1.1 bouyer
517 1.1 bouyer static void tl_restart(v)
518 1.1 bouyer void *v;
519 1.1 bouyer {
520 1.1 bouyer tl_init(v);
521 1.1 bouyer }
522 1.1 bouyer
523 1.1 bouyer static int tl_init(sc)
524 1.1 bouyer tl_softc_t *sc;
525 1.1 bouyer {
526 1.1 bouyer struct ifnet *ifp = &sc->tl_if;
527 1.1 bouyer int i, s;
528 1.1 bouyer
529 1.14 mycroft s = splnet();
530 1.1 bouyer /* cancel any pending IO */
531 1.1 bouyer tl_shutdown(sc);
532 1.1 bouyer tl_reset(sc);
533 1.1 bouyer if ((sc->tl_if.if_flags & IFF_UP) == 0) {
534 1.1 bouyer splx(s);
535 1.1 bouyer return 0;
536 1.1 bouyer }
537 1.1 bouyer /* Set various register to reasonable value */
538 1.1 bouyer /* setup NetCmd in promisc mode if needed */
539 1.1 bouyer i = (ifp->if_flags & IFF_PROMISC) ? TL_NETCOMMAND_CAF : 0;
540 1.1 bouyer tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd,
541 1.17 bouyer TL_NETCOMMAND_NRESET | TL_NETCOMMAND_NWRAP | i);
542 1.1 bouyer /* Max receive size : MCLBYTES */
543 1.1 bouyer tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxL, MCLBYTES & 0xff);
544 1.1 bouyer tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxH,
545 1.17 bouyer (MCLBYTES >> 8) & 0xff);
546 1.1 bouyer
547 1.1 bouyer /* init MAC addr */
548 1.1 bouyer for (i = 0; i < ETHER_ADDR_LEN; i++)
549 1.1 bouyer tl_intreg_write_byte(sc, TL_INT_Areg0 + i , sc->tl_enaddr[i]);
550 1.1 bouyer /* add multicast filters */
551 1.1 bouyer tl_addr_filter(sc);
552 1.1 bouyer #ifdef TLDEBUG_ADDR
553 1.1 bouyer printf("Wrote Mac addr, Areg & hash registers are now: \n");
554 1.1 bouyer for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
555 1.1 bouyer printf(" reg %x: %x\n", i, tl_intreg_read(sc, i));
556 1.1 bouyer #endif
557 1.1 bouyer
558 1.1 bouyer /* Pre-allocate receivers mbuf, make the lists */
559 1.17 bouyer sc->Rx_list = malloc(sizeof(struct Rx_list) * TL_NBUF, M_DEVBUF,
560 1.17 bouyer M_NOWAIT);
561 1.17 bouyer sc->Tx_list = malloc(sizeof(struct Tx_list) * TL_NBUF, M_DEVBUF,
562 1.17 bouyer M_NOWAIT);
563 1.1 bouyer if (sc->Rx_list == NULL || sc->Tx_list == NULL) {
564 1.1 bouyer printf("%s: out of memory for lists\n", sc->sc_dev.dv_xname);
565 1.1 bouyer sc->tl_if.if_flags &= ~IFF_UP;
566 1.1 bouyer splx(s);
567 1.1 bouyer return ENOMEM;
568 1.1 bouyer }
569 1.1 bouyer for (i=0; i< TL_NBUF; i++) {
570 1.17 bouyer if (tl_add_RxBuff(&sc->Rx_list[i], NULL) == 0) {
571 1.17 bouyer printf("%s: out of mbuf for receive list\n",
572 1.17 bouyer sc->sc_dev.dv_xname);
573 1.1 bouyer sc->tl_if.if_flags &= ~IFF_UP;
574 1.1 bouyer splx(s);
575 1.1 bouyer return ENOMEM;
576 1.1 bouyer }
577 1.1 bouyer if (i > 0) { /* chain the list */
578 1.1 bouyer sc->Rx_list[i-1].next = &sc->Rx_list[i];
579 1.17 bouyer sc->Rx_list[i-1].hw_list.fwd =
580 1.17 bouyer vtophys(&sc->Rx_list[i].hw_list);
581 1.1 bouyer #ifdef DIAGNOSTIC
582 1.1 bouyer if (sc->Rx_list[i-1].hw_list.fwd & 0x7)
583 1.17 bouyer printf("%s: physical addr 0x%x of list not "
584 1.17 bouyer "properly aligned\n",
585 1.17 bouyer sc->sc_dev.dv_xname,
586 1.17 bouyer sc->Rx_list[i-1].hw_list.fwd);
587 1.1 bouyer #endif
588 1.1 bouyer sc->Tx_list[i-1].next = &sc->Tx_list[i];
589 1.1 bouyer }
590 1.1 bouyer }
591 1.1 bouyer sc->Rx_list[TL_NBUF-1].next = NULL;
592 1.1 bouyer sc->Rx_list[TL_NBUF-1].hw_list.fwd = 0;
593 1.1 bouyer sc->Tx_list[TL_NBUF-1].next = NULL;
594 1.1 bouyer
595 1.1 bouyer sc->active_Rx = &sc->Rx_list[0];
596 1.1 bouyer sc->last_Rx = &sc->Rx_list[TL_NBUF-1];
597 1.1 bouyer sc->active_Tx = sc->last_Tx = NULL;
598 1.1 bouyer sc->Free_Tx = &sc->Tx_list[0];
599 1.1 bouyer
600 1.6 bouyer if (nullbuf == NULL)
601 1.6 bouyer nullbuf = malloc(ETHER_MIN_TX, M_DEVBUF, M_NOWAIT);
602 1.1 bouyer if (nullbuf == NULL) {
603 1.1 bouyer printf("%s: can't allocate space for pad buffer\n",
604 1.17 bouyer sc->sc_dev.dv_xname);
605 1.1 bouyer sc->tl_if.if_flags &= ~IFF_UP;
606 1.1 bouyer splx(s);
607 1.1 bouyer return ENOMEM;
608 1.1 bouyer }
609 1.1 bouyer bzero(nullbuf, ETHER_MIN_TX);
610 1.1 bouyer
611 1.15 thorpej /* set media */
612 1.15 thorpej mii_mediachg(&sc->tl_mii);
613 1.1 bouyer
614 1.1 bouyer /* start ticks calls */
615 1.1 bouyer timeout(tl_ticks, sc, hz);
616 1.1 bouyer /* write adress of Rx list and enable interrupts */
617 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CH_PARM, vtophys(&sc->Rx_list[0].hw_list));
618 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD,
619 1.17 bouyer HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | HOST_CMD_IntOn);
620 1.1 bouyer sc->tl_if.if_flags |= IFF_RUNNING;
621 1.1 bouyer sc->tl_if.if_flags &= ~IFF_OACTIVE;
622 1.1 bouyer return 0;
623 1.1 bouyer }
624 1.1 bouyer
625 1.1 bouyer
626 1.1 bouyer static u_int32_t
627 1.1 bouyer tl_intreg_read(sc, reg)
628 1.1 bouyer tl_softc_t *sc;
629 1.1 bouyer u_int32_t reg;
630 1.1 bouyer {
631 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
632 1.1 bouyer return TL_HR_READ(sc, TL_HOST_DIO_DATA);
633 1.1 bouyer }
634 1.1 bouyer
635 1.1 bouyer static u_int8_t
636 1.1 bouyer tl_intreg_read_byte(sc, reg)
637 1.1 bouyer tl_softc_t *sc;
638 1.1 bouyer u_int32_t reg;
639 1.1 bouyer {
640 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
641 1.17 bouyer (reg & (~0x07)) & TL_HOST_DIOADR_MASK);
642 1.1 bouyer return TL_HR_READ_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x07));
643 1.1 bouyer }
644 1.1 bouyer
645 1.1 bouyer static void
646 1.1 bouyer tl_intreg_write(sc, reg, val)
647 1.1 bouyer tl_softc_t *sc;
648 1.1 bouyer u_int32_t reg;
649 1.1 bouyer u_int32_t val;
650 1.1 bouyer {
651 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
652 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_DIO_DATA, val);
653 1.1 bouyer }
654 1.1 bouyer
655 1.1 bouyer static void
656 1.1 bouyer tl_intreg_write_byte(sc, reg, val)
657 1.1 bouyer tl_softc_t *sc;
658 1.1 bouyer u_int32_t reg;
659 1.1 bouyer u_int8_t val;
660 1.1 bouyer {
661 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
662 1.17 bouyer (reg & (~0x03)) & TL_HOST_DIOADR_MASK);
663 1.1 bouyer TL_HR_WRITE_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x03), val);
664 1.1 bouyer }
665 1.1 bouyer
666 1.15 thorpej void
667 1.15 thorpej tl_mii_sync(sc)
668 1.15 thorpej struct tl_softc *sc;
669 1.1 bouyer {
670 1.15 thorpej int i;
671 1.1 bouyer
672 1.15 thorpej netsio_clr(sc, TL_NETSIO_MTXEN);
673 1.15 thorpej for (i = 0; i < 32; i++) {
674 1.15 thorpej netsio_clr(sc, TL_NETSIO_MCLK);
675 1.1 bouyer netsio_set(sc, TL_NETSIO_MCLK);
676 1.1 bouyer }
677 1.1 bouyer }
678 1.1 bouyer
679 1.15 thorpej void
680 1.15 thorpej tl_mii_sendbits(sc, data, nbits)
681 1.15 thorpej struct tl_softc *sc;
682 1.15 thorpej u_int32_t data;
683 1.15 thorpej int nbits;
684 1.1 bouyer {
685 1.15 thorpej int i;
686 1.1 bouyer
687 1.15 thorpej netsio_set(sc, TL_NETSIO_MTXEN);
688 1.15 thorpej for (i = 1 << (nbits - 1); i; i = i >> 1) {
689 1.1 bouyer netsio_clr(sc, TL_NETSIO_MCLK);
690 1.15 thorpej netsio_read(sc, TL_NETSIO_MCLK);
691 1.15 thorpej if (data & i)
692 1.15 thorpej netsio_set(sc, TL_NETSIO_MDATA);
693 1.15 thorpej else
694 1.15 thorpej netsio_clr(sc, TL_NETSIO_MDATA);
695 1.15 thorpej netsio_set(sc, TL_NETSIO_MCLK);
696 1.15 thorpej netsio_read(sc, TL_NETSIO_MCLK);
697 1.1 bouyer }
698 1.1 bouyer }
699 1.1 bouyer
700 1.15 thorpej int
701 1.15 thorpej tl_mii_read(self, phy, reg)
702 1.15 thorpej struct device *self;
703 1.15 thorpej int phy, reg;
704 1.1 bouyer {
705 1.15 thorpej struct tl_softc *sc = (struct tl_softc *)self;
706 1.15 thorpej int val = 0, i, err;
707 1.15 thorpej
708 1.15 thorpej /*
709 1.15 thorpej * Read the PHY register by manually driving the MII control lines.
710 1.15 thorpej */
711 1.1 bouyer
712 1.15 thorpej tl_mii_sync(sc);
713 1.15 thorpej tl_mii_sendbits(sc, MII_COMMAND_START, 2);
714 1.15 thorpej tl_mii_sendbits(sc, MII_COMMAND_READ, 2);
715 1.15 thorpej tl_mii_sendbits(sc, phy, 5);
716 1.15 thorpej tl_mii_sendbits(sc, reg, 5);
717 1.15 thorpej
718 1.15 thorpej netsio_clr(sc, TL_NETSIO_MTXEN);
719 1.15 thorpej netsio_clr(sc, TL_NETSIO_MCLK);
720 1.15 thorpej netsio_set(sc, TL_NETSIO_MCLK);
721 1.15 thorpej netsio_clr(sc, TL_NETSIO_MCLK);
722 1.15 thorpej
723 1.15 thorpej err = netsio_read(sc, TL_NETSIO_MDATA);
724 1.15 thorpej netsio_set(sc, TL_NETSIO_MCLK);
725 1.15 thorpej
726 1.15 thorpej /* Even if an error occurs, must still clock out the cycle. */
727 1.15 thorpej for (i = 0; i < 16; i++) {
728 1.15 thorpej val <<= 1;
729 1.15 thorpej netsio_clr(sc, TL_NETSIO_MCLK);
730 1.15 thorpej if (err == 0 && netsio_read(sc, TL_NETSIO_MDATA))
731 1.15 thorpej val |= 1;
732 1.15 thorpej netsio_set(sc, TL_NETSIO_MCLK);
733 1.1 bouyer }
734 1.15 thorpej netsio_clr(sc, TL_NETSIO_MCLK);
735 1.15 thorpej netsio_set(sc, TL_NETSIO_MCLK);
736 1.15 thorpej
737 1.15 thorpej return (err ? 0 : val);
738 1.15 thorpej }
739 1.15 thorpej
740 1.15 thorpej void
741 1.15 thorpej tl_mii_write(self, phy, reg, val)
742 1.15 thorpej struct device *self;
743 1.15 thorpej int phy, reg, val;
744 1.15 thorpej {
745 1.15 thorpej struct tl_softc *sc = (struct tl_softc *)self;
746 1.15 thorpej
747 1.15 thorpej /*
748 1.15 thorpej * Write the PHY register by manually driving the MII control lines.
749 1.15 thorpej */
750 1.15 thorpej
751 1.15 thorpej tl_mii_sync(sc);
752 1.15 thorpej tl_mii_sendbits(sc, MII_COMMAND_START, 2);
753 1.15 thorpej tl_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
754 1.15 thorpej tl_mii_sendbits(sc, phy, 5);
755 1.15 thorpej tl_mii_sendbits(sc, reg, 5);
756 1.15 thorpej tl_mii_sendbits(sc, MII_COMMAND_ACK, 2);
757 1.15 thorpej tl_mii_sendbits(sc, val, 16);
758 1.15 thorpej
759 1.15 thorpej netsio_clr(sc, TL_NETSIO_MCLK);
760 1.15 thorpej netsio_set(sc, TL_NETSIO_MCLK);
761 1.15 thorpej }
762 1.15 thorpej
763 1.15 thorpej void
764 1.15 thorpej tl_statchg(self)
765 1.15 thorpej struct device *self;
766 1.15 thorpej {
767 1.15 thorpej tl_softc_t *sc = (struct tl_softc *)self;
768 1.15 thorpej u_int32_t reg;
769 1.15 thorpej
770 1.15 thorpej #ifdef TLDEBUG
771 1.15 thorpej printf("tl_statchg, media %x\n", sc->tl_ifmedia.ifm_media);
772 1.15 thorpej #endif
773 1.15 thorpej
774 1.15 thorpej /*
775 1.15 thorpej * We must keep the ThunderLAN and the PHY in sync as
776 1.15 thorpej * to the status of full-duplex!
777 1.15 thorpej */
778 1.15 thorpej reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetCmd);
779 1.15 thorpej if (sc->tl_mii.mii_media_active & IFM_FDX)
780 1.15 thorpej reg |= TL_NETCOMMAND_DUPLEX;
781 1.15 thorpej else
782 1.15 thorpej reg &= ~TL_NETCOMMAND_DUPLEX;
783 1.15 thorpej tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd, reg);
784 1.15 thorpej
785 1.15 thorpej /* XXX Update ifp->if_baudrate */
786 1.1 bouyer }
787 1.1 bouyer
788 1.1 bouyer void tl_i2c_set(v, bit)
789 1.1 bouyer void *v;
790 1.1 bouyer u_int8_t bit;
791 1.1 bouyer {
792 1.1 bouyer tl_softc_t *sc = v;
793 1.1 bouyer
794 1.1 bouyer switch (bit) {
795 1.1 bouyer case I2C_DATA:
796 1.1 bouyer netsio_set(sc, TL_NETSIO_EDATA);
797 1.1 bouyer break;
798 1.1 bouyer case I2C_CLOCK:
799 1.1 bouyer netsio_set(sc, TL_NETSIO_ECLOCK);
800 1.1 bouyer break;
801 1.1 bouyer case I2C_TXEN:
802 1.1 bouyer netsio_set(sc, TL_NETSIO_ETXEN);
803 1.1 bouyer break;
804 1.1 bouyer default:
805 1.1 bouyer printf("tl_i2c_set: unknown bit %d\n", bit);
806 1.1 bouyer }
807 1.1 bouyer return;
808 1.1 bouyer }
809 1.1 bouyer
810 1.1 bouyer void tl_i2c_clr(v, bit)
811 1.1 bouyer void *v;
812 1.1 bouyer u_int8_t bit;
813 1.1 bouyer {
814 1.1 bouyer tl_softc_t *sc = v;
815 1.1 bouyer
816 1.1 bouyer switch (bit) {
817 1.1 bouyer case I2C_DATA:
818 1.1 bouyer netsio_clr(sc, TL_NETSIO_EDATA);
819 1.1 bouyer break;
820 1.1 bouyer case I2C_CLOCK:
821 1.1 bouyer netsio_clr(sc, TL_NETSIO_ECLOCK);
822 1.1 bouyer break;
823 1.1 bouyer case I2C_TXEN:
824 1.1 bouyer netsio_clr(sc, TL_NETSIO_ETXEN);
825 1.1 bouyer break;
826 1.1 bouyer default:
827 1.1 bouyer printf("tl_i2c_clr: unknown bit %d\n", bit);
828 1.1 bouyer }
829 1.1 bouyer return;
830 1.1 bouyer }
831 1.1 bouyer
832 1.1 bouyer int tl_i2c_read(v, bit)
833 1.1 bouyer void *v;
834 1.1 bouyer u_int8_t bit;
835 1.1 bouyer {
836 1.1 bouyer tl_softc_t *sc = v;
837 1.1 bouyer
838 1.1 bouyer switch (bit) {
839 1.1 bouyer case I2C_DATA:
840 1.1 bouyer return netsio_read(sc, TL_NETSIO_EDATA);
841 1.1 bouyer break;
842 1.1 bouyer case I2C_CLOCK:
843 1.1 bouyer return netsio_read(sc, TL_NETSIO_ECLOCK);
844 1.1 bouyer break;
845 1.1 bouyer case I2C_TXEN:
846 1.1 bouyer return netsio_read(sc, TL_NETSIO_ETXEN);
847 1.1 bouyer break;
848 1.1 bouyer default:
849 1.1 bouyer printf("tl_i2c_read: unknown bit %d\n", bit);
850 1.1 bouyer return -1;
851 1.1 bouyer }
852 1.1 bouyer }
853 1.1 bouyer
854 1.1 bouyer static int
855 1.1 bouyer tl_intr(v)
856 1.1 bouyer void *v;
857 1.1 bouyer {
858 1.1 bouyer tl_softc_t *sc = v;
859 1.1 bouyer struct ifnet *ifp = &sc->tl_if;
860 1.1 bouyer struct Rx_list *Rx;
861 1.1 bouyer struct Tx_list *Tx;
862 1.1 bouyer struct mbuf *m;
863 1.1 bouyer u_int32_t int_type, int_reg;
864 1.1 bouyer int ack = 0;
865 1.1 bouyer int size;
866 1.1 bouyer
867 1.1 bouyer int_reg = TL_HR_READ(sc, TL_HOST_INTR_DIOADR);
868 1.1 bouyer int_type = int_reg & TL_INTR_MASK;
869 1.1 bouyer if (int_type == 0)
870 1.1 bouyer return 0;
871 1.1 bouyer #if defined(TLDEBUG_RX) || defined(TLDEBUG_TX)
872 1.1 bouyer printf("%s: interrupt type %x, intr_reg %x\n", sc->sc_dev.dv_xname,
873 1.17 bouyer int_type, int_reg);
874 1.1 bouyer #endif
875 1.1 bouyer /* disable interrupts */
876 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
877 1.1 bouyer switch(int_type & TL_INTR_MASK) {
878 1.1 bouyer case TL_INTR_RxEOF:
879 1.1 bouyer while(sc->active_Rx->hw_list.stat & TL_RX_CSTAT_CPLT) {
880 1.1 bouyer /* dequeue and requeue at end of list */
881 1.1 bouyer ack++;
882 1.1 bouyer Rx = sc->active_Rx;
883 1.1 bouyer sc->active_Rx = Rx->next;
884 1.1 bouyer m = Rx->m;
885 1.1 bouyer size = Rx->hw_list.stat >> 16;
886 1.1 bouyer #ifdef TLDEBUG_RX
887 1.17 bouyer printf("tl_intr: RX list complete, Rx %p, size=%d\n",
888 1.17 bouyer Rx, size);
889 1.1 bouyer #endif
890 1.1 bouyer if (tl_add_RxBuff(Rx, m ) == 0) {
891 1.17 bouyer /*
892 1.17 bouyer * No new mbuf, reuse the same. This means
893 1.17 bouyer * that this packet
894 1.17 bouyer * is lost
895 1.17 bouyer */
896 1.1 bouyer m = NULL;
897 1.1 bouyer #ifdef TL_PRIV_STATS
898 1.1 bouyer sc->ierr_nomem++;
899 1.1 bouyer #endif
900 1.1 bouyer #ifdef TLDEBUG
901 1.1 bouyer printf("%s: out of mbuf, lost input packet\n",
902 1.17 bouyer sc->sc_dev.dv_xname);
903 1.1 bouyer #endif
904 1.1 bouyer }
905 1.1 bouyer Rx->next = NULL;
906 1.1 bouyer Rx->hw_list.fwd = 0;
907 1.1 bouyer sc->last_Rx->hw_list.fwd = vtophys(&Rx->hw_list);
908 1.1 bouyer #ifdef DIAGNOSTIC
909 1.1 bouyer if (sc->last_Rx->hw_list.fwd & 0x7)
910 1.17 bouyer printf("%s: physical addr 0x%x of list not "
911 1.17 bouyer "properly aligned\n",
912 1.17 bouyer sc->sc_dev.dv_xname,
913 1.17 bouyer sc->last_Rx->hw_list.fwd);
914 1.1 bouyer #endif
915 1.1 bouyer sc->last_Rx->next = Rx;
916 1.1 bouyer sc->last_Rx = Rx;
917 1.1 bouyer
918 1.1 bouyer /* deliver packet */
919 1.1 bouyer if (m) {
920 1.1 bouyer struct ether_header *eh;
921 1.1 bouyer if (size < sizeof(struct ether_header)) {
922 1.1 bouyer m_freem(m);
923 1.1 bouyer continue;
924 1.1 bouyer }
925 1.1 bouyer m->m_pkthdr.rcvif = ifp;
926 1.1 bouyer m->m_pkthdr.len = m->m_len =
927 1.1 bouyer size - sizeof(struct ether_header);
928 1.1 bouyer eh = mtod(m, struct ether_header *);
929 1.1 bouyer #ifdef TLDEBUG_RX
930 1.1 bouyer printf("tl_intr: Rx packet:\n");
931 1.1 bouyer ether_printheader(eh);
932 1.1 bouyer #endif
933 1.1 bouyer #if NBPFILTER > 0
934 1.1 bouyer if (ifp->if_bpf) {
935 1.1 bouyer bpf_tap(ifp->if_bpf,
936 1.17 bouyer mtod(m, caddr_t), size);
937 1.1 bouyer /*
938 1.17 bouyer * Only pass this packet up
939 1.17 bouyer * if it is for us.
940 1.17 bouyer */
941 1.1 bouyer if ((ifp->if_flags & IFF_PROMISC) &&
942 1.17 bouyer /* !mcast and !bcast */
943 1.17 bouyer (eh->ether_dhost[0] & 1) == 0 &&
944 1.17 bouyer bcmp(eh->ether_dhost,
945 1.17 bouyer LLADDR(ifp->if_sadl),
946 1.17 bouyer sizeof(eh->ether_dhost)) != 0) {
947 1.1 bouyer m_freem(m);
948 1.1 bouyer continue;
949 1.1 bouyer }
950 1.1 bouyer }
951 1.1 bouyer #endif /* NBPFILTER > 0 */
952 1.1 bouyer m->m_data += sizeof(struct ether_header);
953 1.1 bouyer ether_input(ifp, eh, m);
954 1.1 bouyer }
955 1.1 bouyer }
956 1.1 bouyer #ifdef TLDEBUG_RX
957 1.1 bouyer printf("TL_INTR_RxEOF: ack %d\n", ack);
958 1.1 bouyer #else
959 1.1 bouyer if (ack == 0) {
960 1.1 bouyer printf("%s: EOF intr without anything to read !\n",
961 1.17 bouyer sc->sc_dev.dv_xname);
962 1.1 bouyer tl_reset(sc);
963 1.1 bouyer /* shedule reinit of the board */
964 1.1 bouyer timeout(tl_restart, sc, 1);
965 1.1 bouyer return(1);
966 1.1 bouyer }
967 1.1 bouyer #endif
968 1.1 bouyer break;
969 1.1 bouyer case TL_INTR_RxEOC:
970 1.1 bouyer ack++;
971 1.1 bouyer #ifdef TLDEBUG_RX
972 1.1 bouyer printf("TL_INTR_RxEOC: ack %d\n", ack);
973 1.1 bouyer #endif
974 1.1 bouyer #ifdef DIAGNOSTIC
975 1.1 bouyer if (sc->active_Rx->hw_list.stat & TL_RX_CSTAT_CPLT) {
976 1.17 bouyer printf("%s: Rx EOC interrupt and active Rx list not "
977 1.17 bouyer "cleared\n", sc->sc_dev.dv_xname);
978 1.1 bouyer return 0;
979 1.1 bouyer } else
980 1.1 bouyer #endif
981 1.1 bouyer {
982 1.17 bouyer /*
983 1.17 bouyer * write adress of Rx list and send Rx GO command, ack
984 1.17 bouyer * interrupt and enable interrupts in one command
985 1.17 bouyer */
986 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CH_PARM,
987 1.17 bouyer vtophys(&sc->active_Rx->hw_list));
988 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD,
989 1.17 bouyer HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | ack | int_type |
990 1.17 bouyer HOST_CMD_ACK | HOST_CMD_IntOn);
991 1.1 bouyer return 1;
992 1.1 bouyer }
993 1.1 bouyer case TL_INTR_TxEOF:
994 1.1 bouyer case TL_INTR_TxEOC:
995 1.1 bouyer while ((Tx = sc->active_Tx) != NULL) {
996 1.1 bouyer if((Tx->hw_list.stat & TL_TX_CSTAT_CPLT) == 0)
997 1.1 bouyer break;
998 1.1 bouyer ack++;
999 1.1 bouyer #ifdef TLDEBUG_TX
1000 1.17 bouyer printf("TL_INTR_TxEOC: list 0x%xp done\n",
1001 1.17 bouyer vtophys(&Tx->hw_list));
1002 1.1 bouyer #endif
1003 1.1 bouyer Tx->hw_list.stat = 0;
1004 1.1 bouyer m_freem(Tx->m);
1005 1.1 bouyer Tx->m = NULL;
1006 1.1 bouyer sc->active_Tx = Tx->next;
1007 1.1 bouyer if (sc->active_Tx == NULL)
1008 1.1 bouyer sc->last_Tx = NULL;
1009 1.1 bouyer Tx->next = sc->Free_Tx;
1010 1.1 bouyer sc->Free_Tx = Tx;
1011 1.1 bouyer }
1012 1.1 bouyer /* if this was an EOC, ACK immediatly */
1013 1.1 bouyer if (int_type == TL_INTR_TxEOC) {
1014 1.1 bouyer #ifdef TLDEBUG_TX
1015 1.17 bouyer printf("TL_INTR_TxEOC: ack %d (will be set to 1)\n",
1016 1.17 bouyer ack);
1017 1.1 bouyer #endif
1018 1.17 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, 1 | int_type |
1019 1.17 bouyer HOST_CMD_ACK | HOST_CMD_IntOn);
1020 1.17 bouyer if ( sc->active_Tx != NULL) {
1021 1.17 bouyer /* needs a Tx go command */
1022 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CH_PARM,
1023 1.17 bouyer vtophys(&sc->active_Tx->hw_list));
1024 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1025 1.1 bouyer }
1026 1.1 bouyer sc->tl_if.if_timer = 0;
1027 1.1 bouyer if (sc->tl_if.if_snd.ifq_head != NULL)
1028 1.1 bouyer tl_ifstart(&sc->tl_if);
1029 1.1 bouyer return 1;
1030 1.1 bouyer }
1031 1.1 bouyer #ifdef TLDEBUG
1032 1.1 bouyer else {
1033 1.1 bouyer printf("TL_INTR_TxEOF: ack %d\n", ack);
1034 1.1 bouyer }
1035 1.1 bouyer #endif
1036 1.1 bouyer sc->tl_if.if_timer = 0;
1037 1.1 bouyer if (sc->tl_if.if_snd.ifq_head != NULL)
1038 1.1 bouyer tl_ifstart(&sc->tl_if);
1039 1.1 bouyer break;
1040 1.1 bouyer case TL_INTR_Stat:
1041 1.1 bouyer ack++;
1042 1.1 bouyer #ifdef TLDEBUG
1043 1.1 bouyer printf("TL_INTR_Stat: ack %d\n", ack);
1044 1.1 bouyer #endif
1045 1.1 bouyer tl_read_stats(sc);
1046 1.1 bouyer break;
1047 1.1 bouyer case TL_INTR_Adc:
1048 1.1 bouyer if (int_reg & TL_INTVec_MASK) {
1049 1.1 bouyer /* adapter check conditions */
1050 1.17 bouyer printf("%s: check condition, intvect=0x%x, "
1051 1.17 bouyer "ch_param=0x%x\n", sc->sc_dev.dv_xname,
1052 1.17 bouyer int_reg & TL_INTVec_MASK,
1053 1.17 bouyer TL_HR_READ(sc, TL_HOST_CH_PARM));
1054 1.1 bouyer tl_reset(sc);
1055 1.1 bouyer /* shedule reinit of the board */
1056 1.1 bouyer timeout(tl_restart, sc, 1);
1057 1.1 bouyer return(1);
1058 1.1 bouyer } else {
1059 1.1 bouyer u_int8_t netstat;
1060 1.1 bouyer /* Network status */
1061 1.17 bouyer netstat =
1062 1.17 bouyer tl_intreg_read_byte(sc, TL_INT_NET+TL_INT_NetSts);
1063 1.1 bouyer printf("%s: network status, NetSts=%x\n",
1064 1.17 bouyer sc->sc_dev.dv_xname, netstat);
1065 1.1 bouyer /* Ack interrupts */
1066 1.17 bouyer tl_intreg_write_byte(sc, TL_INT_NET+TL_INT_NetSts,
1067 1.17 bouyer netstat);
1068 1.1 bouyer ack++;
1069 1.1 bouyer }
1070 1.1 bouyer break;
1071 1.1 bouyer default:
1072 1.1 bouyer printf("%s: unhandled interrupt code %x!\n",
1073 1.17 bouyer sc->sc_dev.dv_xname, int_type);
1074 1.1 bouyer ack++;
1075 1.1 bouyer }
1076 1.1 bouyer
1077 1.1 bouyer if (ack) {
1078 1.1 bouyer /* Ack the interrupt and enable interrupts */
1079 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, ack | int_type | HOST_CMD_ACK |
1080 1.17 bouyer HOST_CMD_IntOn);
1081 1.1 bouyer return 1;
1082 1.1 bouyer }
1083 1.1 bouyer /* ack = 0 ; interrupt was perhaps not our. Just enable interrupts */
1084 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOn);
1085 1.1 bouyer return 0;
1086 1.1 bouyer }
1087 1.1 bouyer
1088 1.1 bouyer static int
1089 1.1 bouyer tl_ifioctl(ifp, cmd, data)
1090 1.1 bouyer struct ifnet *ifp;
1091 1.1 bouyer ioctl_cmd_t cmd;
1092 1.1 bouyer caddr_t data;
1093 1.1 bouyer {
1094 1.1 bouyer struct tl_softc *sc = ifp->if_softc;
1095 1.1 bouyer struct ifreq *ifr = (struct ifreq *)data;
1096 1.1 bouyer int s, error;
1097 1.1 bouyer
1098 1.14 mycroft s = splnet();
1099 1.1 bouyer switch(cmd) {
1100 1.1 bouyer case SIOCSIFADDR: {
1101 1.1 bouyer struct ifaddr *ifa = (struct ifaddr *)data;
1102 1.1 bouyer sc->tl_if.if_flags |= IFF_UP;
1103 1.1 bouyer if ((error = tl_init(sc)) != NULL) {
1104 1.1 bouyer sc->tl_if.if_flags &= ~IFF_UP;
1105 1.1 bouyer break;
1106 1.1 bouyer }
1107 1.1 bouyer switch (ifa->ifa_addr->sa_family) {
1108 1.1 bouyer #ifdef INET
1109 1.1 bouyer case AF_INET:
1110 1.1 bouyer arp_ifinit(ifp, ifa);
1111 1.1 bouyer break;
1112 1.1 bouyer #endif
1113 1.1 bouyer #ifdef NS
1114 1.1 bouyer case AF_NS: {
1115 1.1 bouyer struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1116 1.1 bouyer
1117 1.1 bouyer if (ns_nullhost(*ina))
1118 1.17 bouyer ina->x_host =
1119 1.18 bouyer *(union ns_host*) LLADDR(ifp->if_sadl);
1120 1.1 bouyer else
1121 1.1 bouyer bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1122 1.1 bouyer ifp->if_addrlen);
1123 1.1 bouyer break;
1124 1.1 bouyer }
1125 1.1 bouyer #endif
1126 1.1 bouyer default:
1127 1.1 bouyer break;
1128 1.1 bouyer }
1129 1.1 bouyer break;
1130 1.1 bouyer }
1131 1.1 bouyer case SIOCSIFFLAGS:
1132 1.1 bouyer {
1133 1.1 bouyer u_int8_t reg;
1134 1.1 bouyer /*
1135 1.1 bouyer * If interface is marked up and not running, then start it.
1136 1.1 bouyer * If it is marked down and running, stop it.
1137 1.1 bouyer */
1138 1.1 bouyer if (ifp->if_flags & IFF_UP) {
1139 1.1 bouyer if ((ifp->if_flags & IFF_RUNNING) == 0) {
1140 1.1 bouyer error = tl_init(sc);
1141 1.1 bouyer /* all flags have been handled by init */
1142 1.1 bouyer break;
1143 1.1 bouyer }
1144 1.1 bouyer error = 0;
1145 1.17 bouyer reg = tl_intreg_read_byte(sc,
1146 1.17 bouyer TL_INT_NET + TL_INT_NetCmd);
1147 1.1 bouyer if (ifp->if_flags & IFF_PROMISC)
1148 1.1 bouyer reg |= TL_NETCOMMAND_CAF;
1149 1.1 bouyer else
1150 1.1 bouyer reg &= ~TL_NETCOMMAND_CAF;
1151 1.17 bouyer tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd,
1152 1.17 bouyer reg);
1153 1.1 bouyer #ifdef TL_PRIV_STATS
1154 1.1 bouyer if (ifp->if_flags & IFF_LINK0) {
1155 1.1 bouyer ifp->if_flags &= ~IFF_LINK0;
1156 1.17 bouyer printf("%s errors statistics\n",
1157 1.17 bouyer sc->sc_dev.dv_xname);
1158 1.17 bouyer printf(" %4d RX buffer overrun\n",
1159 1.17 bouyer sc->ierr_overr);
1160 1.17 bouyer printf(" %4d RX code error\n",
1161 1.17 bouyer sc->ierr_code);
1162 1.17 bouyer printf(" %4d RX crc error\n",
1163 1.17 bouyer sc->ierr_crc);
1164 1.17 bouyer printf(" %4d RX out of memory\n",
1165 1.17 bouyer sc->ierr_nomem);
1166 1.17 bouyer printf(" %4d TX buffer underrun\n",
1167 1.17 bouyer sc->oerr_underr);
1168 1.17 bouyer printf(" %4d TX deffered frames\n",
1169 1.17 bouyer sc->oerr_deffered);
1170 1.17 bouyer printf(" %4d TX single collisions\n",
1171 1.17 bouyer sc->oerr_coll);
1172 1.17 bouyer printf(" %4d TX multi collisions\n",
1173 1.17 bouyer sc->oerr_multicoll);
1174 1.17 bouyer printf(" %4d TX exessive collisions\n",
1175 1.17 bouyer sc->oerr_exesscoll);
1176 1.17 bouyer printf(" %4d TX late collisions\n",
1177 1.17 bouyer sc->oerr_latecoll);
1178 1.17 bouyer printf(" %4d TX carrier loss\n",
1179 1.17 bouyer sc->oerr_carrloss);
1180 1.17 bouyer printf(" %4d TX mbuf copy\n",
1181 1.17 bouyer sc->oerr_mcopy);
1182 1.1 bouyer }
1183 1.1 bouyer #endif
1184 1.1 bouyer } else {
1185 1.1 bouyer if (ifp->if_flags & IFF_RUNNING)
1186 1.1 bouyer tl_shutdown(sc);
1187 1.1 bouyer error = 0;
1188 1.1 bouyer }
1189 1.1 bouyer break;
1190 1.1 bouyer }
1191 1.1 bouyer case SIOCADDMULTI:
1192 1.1 bouyer case SIOCDELMULTI:
1193 1.1 bouyer /*
1194 1.1 bouyer * Update multicast listeners
1195 1.1 bouyer */
1196 1.1 bouyer if (cmd == SIOCADDMULTI)
1197 1.1 bouyer error = ether_addmulti(ifr, &sc->tl_ec);
1198 1.1 bouyer else
1199 1.1 bouyer error = ether_delmulti(ifr, &sc->tl_ec);
1200 1.1 bouyer if (error == ENETRESET) {
1201 1.1 bouyer tl_addr_filter(sc);
1202 1.1 bouyer error = 0;
1203 1.1 bouyer }
1204 1.1 bouyer break;
1205 1.1 bouyer case SIOCSIFMEDIA:
1206 1.1 bouyer case SIOCGIFMEDIA:
1207 1.15 thorpej error = ifmedia_ioctl(ifp, ifr, &sc->tl_mii.mii_media, cmd);
1208 1.1 bouyer break;
1209 1.1 bouyer default:
1210 1.1 bouyer error = EINVAL;
1211 1.1 bouyer }
1212 1.1 bouyer splx(s);
1213 1.1 bouyer return error;
1214 1.1 bouyer }
1215 1.1 bouyer
1216 1.1 bouyer static void
1217 1.1 bouyer tl_ifstart(ifp)
1218 1.1 bouyer struct ifnet *ifp;
1219 1.1 bouyer {
1220 1.1 bouyer tl_softc_t *sc = ifp->if_softc;
1221 1.1 bouyer struct mbuf *m, *mb_head;
1222 1.1 bouyer struct Tx_list *Tx;
1223 1.1 bouyer int segment, size;
1224 1.1 bouyer
1225 1.1 bouyer txloop:
1226 1.1 bouyer /* If we don't have more space ... */
1227 1.1 bouyer if (sc->Free_Tx == NULL) {
1228 1.1 bouyer #ifdef TLDEBUG
1229 1.1 bouyer printf("tl_ifstart: No free TX list\n");
1230 1.1 bouyer #endif
1231 1.1 bouyer return;
1232 1.1 bouyer }
1233 1.1 bouyer /* Grab a paquet for output */
1234 1.1 bouyer IF_DEQUEUE(&ifp->if_snd, mb_head);
1235 1.1 bouyer if (mb_head == NULL) {
1236 1.1 bouyer #ifdef TLDEBUG_TX
1237 1.1 bouyer printf("tl_ifstart: nothing to send\n");
1238 1.1 bouyer #endif
1239 1.1 bouyer return;
1240 1.1 bouyer }
1241 1.1 bouyer Tx = sc->Free_Tx;
1242 1.1 bouyer sc->Free_Tx = Tx->next;
1243 1.1 bouyer /*
1244 1.1 bouyer * Go through each of the mbufs in the chain and initialize
1245 1.1 bouyer * the transmit list descriptors with the physical address
1246 1.1 bouyer * and size of the mbuf.
1247 1.1 bouyer */
1248 1.1 bouyer tbdinit:
1249 1.1 bouyer bzero(Tx, sizeof(struct Tx_list));
1250 1.1 bouyer Tx->m = mb_head;
1251 1.1 bouyer size = 0;
1252 1.1 bouyer for (m = mb_head, segment = 0; m != NULL ; m = m->m_next) {
1253 1.1 bouyer if (m->m_len != 0) {
1254 1.1 bouyer if (segment == TL_NSEG)
1255 1.1 bouyer break;
1256 1.1 bouyer size += m->m_len;
1257 1.1 bouyer Tx->hw_list.seg[segment].data_addr =
1258 1.16 eeh vtophys(mtod(m, vaddr_t));
1259 1.1 bouyer Tx->hw_list.seg[segment].data_count = m->m_len;
1260 1.1 bouyer segment++;
1261 1.1 bouyer }
1262 1.1 bouyer }
1263 1.1 bouyer if (m != NULL || (size < ETHER_MIN_TX && segment == TL_NSEG)) {
1264 1.1 bouyer /*
1265 1.17 bouyer * We ran out of segments, or we will. We have to recopy this
1266 1.17 bouyer * mbuf chain first.
1267 1.1 bouyer */
1268 1.1 bouyer struct mbuf *mn;
1269 1.1 bouyer #ifdef TLDEBUG_TX
1270 1.1 bouyer printf("tl_ifstart: need to copy mbuf\n");
1271 1.1 bouyer #endif
1272 1.1 bouyer #ifdef TL_PRIV_STATS
1273 1.1 bouyer sc->oerr_mcopy++;
1274 1.1 bouyer #endif
1275 1.1 bouyer MGETHDR(mn, M_DONTWAIT, MT_DATA);
1276 1.1 bouyer if (mn == NULL) {
1277 1.1 bouyer m_freem(mb_head);
1278 1.1 bouyer goto bad;
1279 1.1 bouyer }
1280 1.1 bouyer if (mb_head->m_pkthdr.len > MHLEN) {
1281 1.1 bouyer MCLGET(mn, M_DONTWAIT);
1282 1.1 bouyer if ((mn->m_flags & M_EXT) == 0) {
1283 1.1 bouyer m_freem(mn);
1284 1.1 bouyer m_freem(mb_head);
1285 1.1 bouyer goto bad;
1286 1.1 bouyer }
1287 1.1 bouyer }
1288 1.1 bouyer m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1289 1.17 bouyer mtod(mn, caddr_t));
1290 1.1 bouyer mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
1291 1.1 bouyer m_freem(mb_head);
1292 1.1 bouyer mb_head = mn;
1293 1.1 bouyer goto tbdinit;
1294 1.1 bouyer }
1295 1.1 bouyer /* We are at end of mbuf chain. check the size and
1296 1.1 bouyer * see if it needs to be extended
1297 1.1 bouyer */
1298 1.1 bouyer if (size < ETHER_MIN_TX) {
1299 1.1 bouyer #ifdef DIAGNOSTIC
1300 1.1 bouyer if (segment >= TL_NSEG) {
1301 1.1 bouyer panic("tl_ifstart: to much segmets (%d)\n", segment);
1302 1.1 bouyer }
1303 1.1 bouyer #endif
1304 1.1 bouyer /*
1305 1.1 bouyer * add the nullbuf in the seg
1306 1.1 bouyer */
1307 1.1 bouyer Tx->hw_list.seg[segment].data_count =
1308 1.17 bouyer ETHER_MIN_TX - size;
1309 1.1 bouyer Tx->hw_list.seg[segment].data_addr =
1310 1.17 bouyer vtophys(nullbuf);
1311 1.1 bouyer size = ETHER_MIN_TX;
1312 1.1 bouyer segment++;
1313 1.1 bouyer }
1314 1.1 bouyer /* The list is done, finish the list init */
1315 1.1 bouyer Tx->hw_list.seg[segment-1].data_count |=
1316 1.17 bouyer TL_LAST_SEG;
1317 1.1 bouyer Tx->hw_list.stat = (size << 16) | 0x3000;
1318 1.1 bouyer #ifdef TLDEBUG_TX
1319 1.1 bouyer printf("%s: sending, Tx : stat = 0x%x\n", sc->sc_dev.dv_xname,
1320 1.17 bouyer Tx->hw_list.stat);
1321 1.1 bouyer #if 0
1322 1.1 bouyer for(segment = 0; segment < TL_NSEG; segment++) {
1323 1.1 bouyer printf(" seg %d addr 0x%x len 0x%x\n",
1324 1.17 bouyer segment,
1325 1.17 bouyer Tx->hw_list.seg[segment].data_addr,
1326 1.17 bouyer Tx->hw_list.seg[segment].data_count);
1327 1.1 bouyer }
1328 1.1 bouyer #endif
1329 1.1 bouyer #endif
1330 1.1 bouyer sc->opkt++;
1331 1.1 bouyer if (sc->active_Tx == NULL) {
1332 1.1 bouyer sc->active_Tx = sc->last_Tx = Tx;
1333 1.1 bouyer #ifdef TLDEBUG_TX
1334 1.1 bouyer printf("%s: Tx GO, addr=0x%x\n", sc->sc_dev.dv_xname,
1335 1.17 bouyer vtophys(&Tx->hw_list));
1336 1.1 bouyer #endif
1337 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CH_PARM, vtophys(&Tx->hw_list));
1338 1.1 bouyer TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1339 1.1 bouyer } else {
1340 1.1 bouyer #ifdef TLDEBUG_TX
1341 1.1 bouyer printf("%s: Tx addr=0x%x queued\n", sc->sc_dev.dv_xname,
1342 1.17 bouyer vtophys(&Tx->hw_list));
1343 1.1 bouyer #endif
1344 1.1 bouyer sc->last_Tx->hw_list.fwd = vtophys(&Tx->hw_list);
1345 1.1 bouyer sc->last_Tx->next = Tx;
1346 1.1 bouyer sc->last_Tx = Tx;
1347 1.1 bouyer #ifdef DIAGNOSTIC
1348 1.1 bouyer if (sc->last_Tx->hw_list.fwd & 0x7)
1349 1.17 bouyer printf("%s: physical addr 0x%x of list not properly "
1350 1.17 bouyer "aligned\n",
1351 1.17 bouyer sc->sc_dev.dv_xname, sc->last_Rx->hw_list.fwd);
1352 1.1 bouyer #endif
1353 1.1 bouyer }
1354 1.1 bouyer #if NBPFILTER > 0
1355 1.1 bouyer /* Pass packet to bpf if there is a listener */
1356 1.1 bouyer if (ifp->if_bpf)
1357 1.1 bouyer bpf_mtap(ifp->if_bpf, mb_head);
1358 1.1 bouyer #endif
1359 1.17 bouyer /*
1360 1.17 bouyer * Set a 5 second timer just in case we don't hear from the card again.
1361 1.17 bouyer */
1362 1.1 bouyer ifp->if_timer = 5;
1363 1.1 bouyer goto txloop;
1364 1.1 bouyer bad:
1365 1.1 bouyer #ifdef TLDEBUG
1366 1.1 bouyer printf("tl_ifstart: Out of mbuf, Tx pkt lost\n");
1367 1.1 bouyer #endif
1368 1.1 bouyer Tx->next = sc->Free_Tx;
1369 1.1 bouyer sc->Free_Tx = Tx;
1370 1.1 bouyer return;
1371 1.1 bouyer }
1372 1.1 bouyer
1373 1.1 bouyer static void
1374 1.1 bouyer tl_ifwatchdog(ifp)
1375 1.1 bouyer struct ifnet *ifp;
1376 1.1 bouyer {
1377 1.1 bouyer tl_softc_t *sc = ifp->if_softc;
1378 1.1 bouyer
1379 1.1 bouyer if ((ifp->if_flags & IFF_RUNNING) == 0)
1380 1.1 bouyer return;
1381 1.1 bouyer printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1382 1.1 bouyer ifp->if_oerrors++;
1383 1.1 bouyer tl_init(sc);
1384 1.1 bouyer }
1385 1.1 bouyer
1386 1.1 bouyer static int
1387 1.1 bouyer tl_mediachange(ifp)
1388 1.1 bouyer struct ifnet *ifp;
1389 1.1 bouyer {
1390 1.15 thorpej
1391 1.15 thorpej if (ifp->if_flags & IFF_UP)
1392 1.15 thorpej tl_init(ifp->if_softc);
1393 1.15 thorpej return (0);
1394 1.1 bouyer }
1395 1.1 bouyer
1396 1.1 bouyer static void
1397 1.1 bouyer tl_mediastatus(ifp, ifmr)
1398 1.1 bouyer struct ifnet *ifp;
1399 1.1 bouyer struct ifmediareq *ifmr;
1400 1.1 bouyer {
1401 1.1 bouyer tl_softc_t *sc = ifp->if_softc;
1402 1.15 thorpej
1403 1.15 thorpej mii_pollstat(&sc->tl_mii);
1404 1.15 thorpej ifmr->ifm_active = sc->tl_mii.mii_media_active;
1405 1.15 thorpej ifmr->ifm_status = sc->tl_mii.mii_media_status;
1406 1.1 bouyer }
1407 1.1 bouyer
1408 1.1 bouyer static int tl_add_RxBuff(Rx, oldm)
1409 1.1 bouyer struct Rx_list *Rx;
1410 1.1 bouyer struct mbuf *oldm;
1411 1.1 bouyer {
1412 1.1 bouyer struct mbuf *m;
1413 1.1 bouyer
1414 1.1 bouyer MGETHDR(m, M_DONTWAIT, MT_DATA);
1415 1.1 bouyer if (m != NULL) {
1416 1.1 bouyer MCLGET(m, M_DONTWAIT);
1417 1.1 bouyer if ((m->m_flags & M_EXT) == 0) {
1418 1.1 bouyer m_freem(m);
1419 1.1 bouyer if (oldm == NULL)
1420 1.1 bouyer return 0;
1421 1.1 bouyer m = oldm;
1422 1.1 bouyer m->m_data = m->m_ext.ext_buf;
1423 1.1 bouyer }
1424 1.1 bouyer } else {
1425 1.1 bouyer if (oldm == NULL)
1426 1.1 bouyer return 0;
1427 1.1 bouyer m = oldm;
1428 1.1 bouyer m->m_data = m->m_ext.ext_buf;
1429 1.1 bouyer }
1430 1.1 bouyer /*
1431 1.1 bouyer * Move the data pointer up so that the incoming data packet
1432 1.1 bouyer * will be 32-bit aligned.
1433 1.1 bouyer */
1434 1.1 bouyer m->m_data += 2;
1435 1.1 bouyer
1436 1.1 bouyer /* (re)init the Rx_list struct */
1437 1.1 bouyer
1438 1.1 bouyer Rx->m = m;
1439 1.1 bouyer Rx->hw_list.stat = ((MCLBYTES -2) << 16) | 0x3000;
1440 1.1 bouyer Rx->hw_list.seg.data_count = (MCLBYTES -2);
1441 1.1 bouyer Rx->hw_list.seg.data_addr = vtophys(m->m_data);
1442 1.1 bouyer return (m != oldm);
1443 1.1 bouyer }
1444 1.1 bouyer
1445 1.1 bouyer static void tl_ticks(v)
1446 1.1 bouyer void *v;
1447 1.1 bouyer {
1448 1.1 bouyer tl_softc_t *sc = v;
1449 1.1 bouyer
1450 1.1 bouyer tl_read_stats(sc);
1451 1.19 thorpej
1452 1.19 thorpej /* Tick the MII. */
1453 1.19 thorpej mii_tick(&sc->tl_mii);
1454 1.19 thorpej
1455 1.1 bouyer if (sc->opkt > 0) {
1456 1.17 bouyer if (sc->oerr_exesscoll > sc->opkt / 100) {
1457 1.17 bouyer /* exess collisions */
1458 1.1 bouyer if (sc->tl_flags & TL_IFACT) /* only print once */
1459 1.17 bouyer printf("%s: no carrier\n",
1460 1.17 bouyer sc->sc_dev.dv_xname);
1461 1.17 bouyer sc->tl_flags &= ~TL_IFACT;
1462 1.17 bouyer } else
1463 1.17 bouyer sc->tl_flags |= TL_IFACT;
1464 1.17 bouyer sc->oerr_exesscoll = sc->opkt = 0;
1465 1.17 bouyer sc->tl_lasttx = 0;
1466 1.17 bouyer } else {
1467 1.17 bouyer sc->tl_lasttx++;
1468 1.17 bouyer if (sc->tl_lasttx >= TL_IDLETIME) {
1469 1.17 bouyer /*
1470 1.17 bouyer * No TX activity in the last TL_IDLETIME seconds.
1471 1.17 bouyer * sends a LLC Class1 TEST pkt
1472 1.17 bouyer */
1473 1.17 bouyer struct mbuf *m;
1474 1.17 bouyer int s;
1475 1.17 bouyer MGETHDR(m, M_DONTWAIT, MT_DATA);
1476 1.17 bouyer if (m != NULL) {
1477 1.17 bouyer #ifdef TLDEBUG
1478 1.17 bouyer printf("tl_ticks: sending LLC test pkt\n");
1479 1.17 bouyer #endif
1480 1.17 bouyer bcopy(sc->tl_enaddr,
1481 1.17 bouyer mtod(m, struct ether_header *)->ether_dhost,
1482 1.17 bouyer 6);
1483 1.17 bouyer bcopy(sc->tl_enaddr,
1484 1.17 bouyer mtod(m, struct ether_header *)->ether_shost,
1485 1.17 bouyer 6);
1486 1.17 bouyer mtod(m, struct ether_header *)->ether_type =
1487 1.17 bouyer htons(3);
1488 1.17 bouyer mtod(m, unsigned char *)[14] = 0;
1489 1.17 bouyer mtod(m, unsigned char *)[15] = 0;
1490 1.17 bouyer mtod(m, unsigned char *)[16] = 0xE3;
1491 1.17 bouyer /* LLC Class1 TEST (no poll) */
1492 1.17 bouyer m->m_len = m->m_pkthdr.len =
1493 1.17 bouyer sizeof(struct ether_header) + 3;
1494 1.17 bouyer s = splnet();
1495 1.17 bouyer IF_PREPEND(&sc->tl_if.if_snd, m);
1496 1.17 bouyer tl_ifstart(&sc->tl_if);
1497 1.17 bouyer splx(s);
1498 1.1 bouyer }
1499 1.17 bouyer }
1500 1.17 bouyer }
1501 1.1 bouyer
1502 1.17 bouyer /* read statistics every seconds */
1503 1.17 bouyer timeout(tl_ticks, v, hz);
1504 1.17 bouyer }
1505 1.17 bouyer
1506 1.17 bouyer static void
1507 1.17 bouyer tl_read_stats(sc)
1508 1.17 bouyer tl_softc_t *sc;
1509 1.17 bouyer {
1510 1.17 bouyer u_int32_t reg;
1511 1.17 bouyer int ierr_overr;
1512 1.17 bouyer int ierr_code;
1513 1.17 bouyer int ierr_crc;
1514 1.17 bouyer int oerr_underr;
1515 1.17 bouyer int oerr_deffered;
1516 1.17 bouyer int oerr_coll;
1517 1.17 bouyer int oerr_multicoll;
1518 1.17 bouyer int oerr_exesscoll;
1519 1.17 bouyer int oerr_latecoll;
1520 1.17 bouyer int oerr_carrloss;
1521 1.17 bouyer struct ifnet *ifp = &sc->tl_if;
1522 1.17 bouyer
1523 1.17 bouyer reg = tl_intreg_read(sc, TL_INT_STATS_TX);
1524 1.17 bouyer ifp->if_opackets += reg & 0x00ffffff;
1525 1.17 bouyer oerr_underr = reg >> 24;
1526 1.17 bouyer
1527 1.17 bouyer reg = tl_intreg_read(sc, TL_INT_STATS_RX);
1528 1.17 bouyer ifp->if_ipackets += reg & 0x00ffffff;
1529 1.17 bouyer ierr_overr = reg >> 24;
1530 1.17 bouyer
1531 1.17 bouyer reg = tl_intreg_read(sc, TL_INT_STATS_FERR);
1532 1.17 bouyer ierr_crc = (reg & TL_FERR_CRC) >> 16;
1533 1.17 bouyer ierr_code = (reg & TL_FERR_CODE) >> 24;
1534 1.17 bouyer oerr_deffered = (reg & TL_FERR_DEF);
1535 1.17 bouyer
1536 1.17 bouyer reg = tl_intreg_read(sc, TL_INT_STATS_COLL);
1537 1.17 bouyer oerr_multicoll = (reg & TL_COL_MULTI);
1538 1.17 bouyer oerr_coll = (reg & TL_COL_SINGLE) >> 16;
1539 1.17 bouyer
1540 1.17 bouyer reg = tl_intreg_read(sc, TL_INT_LERR);
1541 1.17 bouyer oerr_exesscoll = (reg & TL_LERR_ECOLL);
1542 1.17 bouyer oerr_latecoll = (reg & TL_LERR_LCOLL) >> 8;
1543 1.17 bouyer oerr_carrloss = (reg & TL_LERR_CL) >> 16;
1544 1.17 bouyer
1545 1.17 bouyer
1546 1.17 bouyer sc->stats_exesscoll += oerr_exesscoll;
1547 1.17 bouyer ifp->if_oerrors += oerr_underr + oerr_exesscoll + oerr_latecoll +
1548 1.17 bouyer oerr_carrloss;
1549 1.17 bouyer ifp->if_collisions += oerr_coll + oerr_multicoll;
1550 1.17 bouyer ifp->if_ierrors += ierr_overr + ierr_code + ierr_crc;
1551 1.17 bouyer
1552 1.17 bouyer if (ierr_overr)
1553 1.17 bouyer printf("%s: receiver ring buffer overrun\n",
1554 1.17 bouyer sc->sc_dev.dv_xname);
1555 1.17 bouyer if (oerr_underr)
1556 1.17 bouyer printf("%s: transmit buffer underrun\n",
1557 1.17 bouyer sc->sc_dev.dv_xname);
1558 1.17 bouyer #ifdef TL_PRIV_STATS
1559 1.17 bouyer sc->ierr_overr += ierr_overr;
1560 1.17 bouyer sc->ierr_code += ierr_code;
1561 1.17 bouyer sc->ierr_crc += ierr_crc;
1562 1.17 bouyer sc->oerr_underr += oerr_underr;
1563 1.17 bouyer sc->oerr_deffered += oerr_deffered;
1564 1.17 bouyer sc->oerr_coll += oerr_coll;
1565 1.17 bouyer sc->oerr_multicoll += oerr_multicoll;
1566 1.17 bouyer sc->oerr_exesscoll += oerr_exesscoll;
1567 1.17 bouyer sc->oerr_latecoll += oerr_latecoll;
1568 1.17 bouyer sc->oerr_carrloss += oerr_carrloss;
1569 1.17 bouyer #endif
1570 1.17 bouyer }
1571 1.1 bouyer
1572 1.17 bouyer static void tl_addr_filter(sc)
1573 1.17 bouyer tl_softc_t *sc;
1574 1.17 bouyer {
1575 1.17 bouyer struct ether_multistep step;
1576 1.17 bouyer struct ether_multi *enm;
1577 1.17 bouyer u_int32_t hash[2] = {0, 0};
1578 1.17 bouyer int i;
1579 1.1 bouyer
1580 1.17 bouyer sc->tl_if.if_flags &= ~IFF_ALLMULTI;
1581 1.17 bouyer ETHER_FIRST_MULTI(step, &sc->tl_ec, enm);
1582 1.17 bouyer while (enm != NULL) {
1583 1.17 bouyer #ifdef TLDEBUG
1584 1.17 bouyer printf("tl_addr_filter: addrs %s %s\n",
1585 1.17 bouyer ether_sprintf(enm->enm_addrlo),
1586 1.17 bouyer ether_sprintf(enm->enm_addrhi));
1587 1.17 bouyer #endif
1588 1.17 bouyer if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
1589 1.17 bouyer i = tl_multicast_hash(enm->enm_addrlo);
1590 1.17 bouyer hash[i/32] |= 1 << (i%32);
1591 1.17 bouyer } else {
1592 1.17 bouyer hash[0] = hash[1] = 0xffffffff;
1593 1.17 bouyer sc->tl_if.if_flags |= IFF_ALLMULTI;
1594 1.17 bouyer break;
1595 1.1 bouyer }
1596 1.17 bouyer ETHER_NEXT_MULTI(step, enm);
1597 1.17 bouyer }
1598 1.17 bouyer #ifdef TLDEBUG
1599 1.17 bouyer printf("tl_addr_filer: hash1 %x has2 %x\n", hash[0], hash[1]);
1600 1.17 bouyer #endif
1601 1.17 bouyer tl_intreg_write(sc, TL_INT_HASH1, hash[0]);
1602 1.17 bouyer tl_intreg_write(sc, TL_INT_HASH2, hash[1]);
1603 1.17 bouyer }
1604 1.1 bouyer
1605 1.17 bouyer static int tl_multicast_hash(a)
1606 1.17 bouyer u_int8_t *a;
1607 1.17 bouyer {
1608 1.17 bouyer int hash;
1609 1.17 bouyer
1610 1.17 bouyer #define DA(addr,bit) (addr[5 - (bit/8)] & (1 << bit%8))
1611 1.17 bouyer #define xor8(a,b,c,d,e,f,g,h) (((a != 0) + (b != 0) + (c != 0) + (d != 0) + (e != 0) + (f != 0) + (g != 0) + (h != 0)) & 1)
1612 1.17 bouyer
1613 1.17 bouyer hash = xor8( DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30),
1614 1.17 bouyer DA(a,36), DA(a,42));
1615 1.17 bouyer hash |= xor8( DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31),
1616 1.17 bouyer DA(a,37), DA(a,43)) << 1;
1617 1.17 bouyer hash |= xor8( DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32),
1618 1.17 bouyer DA(a,38), DA(a,44)) << 2;
1619 1.17 bouyer hash |= xor8( DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33),
1620 1.17 bouyer DA(a,39), DA(a,45)) << 3;
1621 1.17 bouyer hash |= xor8( DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34),
1622 1.17 bouyer DA(a,40), DA(a,46)) << 4;
1623 1.17 bouyer hash |= xor8( DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35),
1624 1.17 bouyer DA(a,41), DA(a,47)) << 5;
1625 1.1 bouyer
1626 1.17 bouyer return hash;
1627 1.17 bouyer }
1628 1.1 bouyer
1629 1.17 bouyer #if defined(TLDEBUG_RX)
1630 1.17 bouyer void
1631 1.17 bouyer ether_printheader(eh)
1632 1.17 bouyer struct ether_header *eh;
1633 1.17 bouyer {
1634 1.17 bouyer u_char *c = (char*)eh;
1635 1.17 bouyer int i;
1636 1.17 bouyer for (i=0; i<sizeof(struct ether_header); i++)
1637 1.17 bouyer printf("%x ", (u_int)c[i]);
1638 1.17 bouyer printf("\n");
1639 1.17 bouyer }
1640 1.1 bouyer #endif
1641