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