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