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