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