if_tl.c revision 1.29 1 /* $NetBSD: if_tl.c,v 1.29 2000/02/02 08:05:29 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 "Compaq Netelligent 10/100 TX" },
204 { PCI_PRODUCT_COMPAQ_N10T, TLPHY_MEDIA_10_5,
205 "Compaq Netelligent 10 T" },
206 { PCI_PRODUCT_COMPAQ_IntNF3P, TLPHY_MEDIA_10_2,
207 "Compaq Integrated NetFlex 3/P" },
208 { PCI_PRODUCT_COMPAQ_IntPL100TX, TLPHY_MEDIA_10_2|TLPHY_MEDIA_NO_10_T,
209 "Compaq ProLiant Integrated Netelligent 10/100 TX" },
210 { PCI_PRODUCT_COMPAQ_DPNet100TX, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T,
211 "Compaq Dual Port Netelligent 10/100 TX" },
212 { PCI_PRODUCT_COMPAQ_DP4000, TLPHY_MEDIA_10_5,
213 "Compaq Deskpro 4000 5233MMX" },
214 { PCI_PRODUCT_COMPAQ_NF3P_BNC, TLPHY_MEDIA_10_2,
215 "Compaq NetFlex 3/P w/ BNC" },
216 { PCI_PRODUCT_COMPAQ_NF3P, TLPHY_MEDIA_10_5,
217 "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 */
226 { PCI_PRODUCT_TI_TLAN, 0,
227 "Texas Instruments ThunderLAN" },
228 { 0, 0, NULL },
229 };
230
231 struct tl_vendor_desc {
232 u_int32_t tv_vendor;
233 const struct tl_product_desc *tv_products;
234 };
235
236 const struct tl_vendor_desc tl_vendors[] = {
237 { PCI_VENDOR_COMPAQ, tl_compaq_products },
238 { PCI_VENDOR_TI, tl_ti_products },
239 { 0, NULL },
240 };
241
242 const struct tl_product_desc *tl_lookup_product __P((u_int32_t));
243
244 const struct tl_product_desc *
245 tl_lookup_product(id)
246 u_int32_t id;
247 {
248 const struct tl_product_desc *tp;
249 const struct tl_vendor_desc *tv;
250
251 for (tv = tl_vendors; tv->tv_products != NULL; tv++)
252 if (PCI_VENDOR(id) == tv->tv_vendor)
253 break;
254
255 if ((tp = tv->tv_products) == NULL)
256 return (NULL);
257
258 for (; tp->tp_desc != NULL; tp++)
259 if (PCI_PRODUCT(id) == tp->tp_product)
260 break;
261
262 if (tp->tp_desc == NULL)
263 return (NULL);
264
265 return (tp);
266 }
267
268 static char *nullbuf = NULL;
269
270 static int
271 tl_pci_match(parent, match, aux)
272 struct device *parent;
273 struct cfdata *match;
274 void *aux;
275 {
276 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
277
278 if (tl_lookup_product(pa->pa_id) != NULL)
279 return (1);
280
281 return (0);
282 }
283
284 static void
285 tl_pci_attach(parent, self, aux)
286 struct device * parent;
287 struct device * self;
288 void * aux;
289 {
290 tl_softc_t *sc = (tl_softc_t *)self;
291 struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
292 const struct tl_product_desc *tp;
293 struct ifnet * const ifp = &sc->tl_if;
294 bus_space_tag_t iot, memt;
295 bus_space_handle_t ioh, memh;
296 pci_intr_handle_t intrhandle;
297 const char *intrstr;
298 int i, tmp, ioh_valid, memh_valid;
299 int reg_io, reg_mem;
300 pcireg_t reg10, reg14;
301 pcireg_t csr;
302
303 printf("\n");
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);
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 untimeout(tl_ticks, sc);
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 untimeout(tl_ticks, sc);
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(&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 timeout(tl_ticks, sc, hz);
644 /* write adress of Rx list and enable interrupts */
645 TL_HR_WRITE(sc, TL_HOST_CH_PARM, vtophys(&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 /* XXX Update ifp->if_baudrate */
814 }
815
816 void tl_i2c_set(v, bit)
817 void *v;
818 u_int8_t bit;
819 {
820 tl_softc_t *sc = v;
821
822 switch (bit) {
823 case I2C_DATA:
824 netsio_set(sc, TL_NETSIO_EDATA);
825 break;
826 case I2C_CLOCK:
827 netsio_set(sc, TL_NETSIO_ECLOCK);
828 break;
829 case I2C_TXEN:
830 netsio_set(sc, TL_NETSIO_ETXEN);
831 break;
832 default:
833 printf("tl_i2c_set: unknown bit %d\n", bit);
834 }
835 return;
836 }
837
838 void tl_i2c_clr(v, bit)
839 void *v;
840 u_int8_t bit;
841 {
842 tl_softc_t *sc = v;
843
844 switch (bit) {
845 case I2C_DATA:
846 netsio_clr(sc, TL_NETSIO_EDATA);
847 break;
848 case I2C_CLOCK:
849 netsio_clr(sc, TL_NETSIO_ECLOCK);
850 break;
851 case I2C_TXEN:
852 netsio_clr(sc, TL_NETSIO_ETXEN);
853 break;
854 default:
855 printf("tl_i2c_clr: unknown bit %d\n", bit);
856 }
857 return;
858 }
859
860 int tl_i2c_read(v, bit)
861 void *v;
862 u_int8_t bit;
863 {
864 tl_softc_t *sc = v;
865
866 switch (bit) {
867 case I2C_DATA:
868 return netsio_read(sc, TL_NETSIO_EDATA);
869 break;
870 case I2C_CLOCK:
871 return netsio_read(sc, TL_NETSIO_ECLOCK);
872 break;
873 case I2C_TXEN:
874 return netsio_read(sc, TL_NETSIO_ETXEN);
875 break;
876 default:
877 printf("tl_i2c_read: unknown bit %d\n", bit);
878 return -1;
879 }
880 }
881
882 static int
883 tl_intr(v)
884 void *v;
885 {
886 tl_softc_t *sc = v;
887 struct ifnet *ifp = &sc->tl_if;
888 struct Rx_list *Rx;
889 struct Tx_list *Tx;
890 struct mbuf *m;
891 u_int32_t int_type, int_reg;
892 int ack = 0;
893 int size;
894
895 int_reg = TL_HR_READ(sc, TL_HOST_INTR_DIOADR);
896 int_type = int_reg & TL_INTR_MASK;
897 if (int_type == 0)
898 return 0;
899 #if defined(TLDEBUG_RX) || defined(TLDEBUG_TX)
900 printf("%s: interrupt type %x, intr_reg %x\n", sc->sc_dev.dv_xname,
901 int_type, int_reg);
902 #endif
903 /* disable interrupts */
904 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
905 switch(int_type & TL_INTR_MASK) {
906 case TL_INTR_RxEOF:
907 while(sc->active_Rx->hw_list.stat & TL_RX_CSTAT_CPLT) {
908 /* dequeue and requeue at end of list */
909 ack++;
910 Rx = sc->active_Rx;
911 sc->active_Rx = Rx->next;
912 m = Rx->m;
913 size = Rx->hw_list.stat >> 16;
914 #ifdef TLDEBUG_RX
915 printf("tl_intr: RX list complete, Rx %p, size=%d\n",
916 Rx, size);
917 #endif
918 if (tl_add_RxBuff(Rx, m ) == 0) {
919 /*
920 * No new mbuf, reuse the same. This means
921 * that this packet
922 * is lost
923 */
924 m = NULL;
925 #ifdef TL_PRIV_STATS
926 sc->ierr_nomem++;
927 #endif
928 #ifdef TLDEBUG
929 printf("%s: out of mbuf, lost input packet\n",
930 sc->sc_dev.dv_xname);
931 #endif
932 }
933 Rx->next = NULL;
934 Rx->hw_list.fwd = 0;
935 sc->last_Rx->hw_list.fwd = vtophys(&Rx->hw_list);
936 #ifdef DIAGNOSTIC
937 if (sc->last_Rx->hw_list.fwd & 0x7)
938 printf("%s: physical addr 0x%x of list not "
939 "properly aligned\n",
940 sc->sc_dev.dv_xname,
941 sc->last_Rx->hw_list.fwd);
942 #endif
943 sc->last_Rx->next = Rx;
944 sc->last_Rx = Rx;
945
946 /* deliver packet */
947 if (m) {
948 struct ether_header *eh;
949 if (size < sizeof(struct ether_header)) {
950 m_freem(m);
951 continue;
952 }
953 m->m_pkthdr.rcvif = ifp;
954 m->m_pkthdr.len = m->m_len = size;
955 eh = mtod(m, struct ether_header *);
956 #ifdef TLDEBUG_RX
957 printf("tl_intr: Rx packet:\n");
958 ether_printheader(eh);
959 #endif
960 #if NBPFILTER > 0
961 if (ifp->if_bpf) {
962 bpf_tap(ifp->if_bpf,
963 mtod(m, caddr_t), size);
964 /*
965 * Only pass this packet up
966 * if it is for us.
967 */
968 if ((ifp->if_flags & IFF_PROMISC) &&
969 /* !mcast and !bcast */
970 (eh->ether_dhost[0] & 1) == 0 &&
971 bcmp(eh->ether_dhost,
972 LLADDR(ifp->if_sadl),
973 sizeof(eh->ether_dhost)) != 0) {
974 m_freem(m);
975 continue;
976 }
977 }
978 #endif /* NBPFILTER > 0 */
979 (*ifp->if_input)(ifp, m);
980 }
981 }
982 #ifdef TLDEBUG_RX
983 printf("TL_INTR_RxEOF: ack %d\n", ack);
984 #else
985 if (ack == 0) {
986 printf("%s: EOF intr without anything to read !\n",
987 sc->sc_dev.dv_xname);
988 tl_reset(sc);
989 /* shedule reinit of the board */
990 timeout(tl_restart, sc, 1);
991 return(1);
992 }
993 #endif
994 break;
995 case TL_INTR_RxEOC:
996 ack++;
997 #ifdef TLDEBUG_RX
998 printf("TL_INTR_RxEOC: ack %d\n", ack);
999 #endif
1000 #ifdef DIAGNOSTIC
1001 if (sc->active_Rx->hw_list.stat & TL_RX_CSTAT_CPLT) {
1002 printf("%s: Rx EOC interrupt and active Rx list not "
1003 "cleared\n", sc->sc_dev.dv_xname);
1004 return 0;
1005 } else
1006 #endif
1007 {
1008 /*
1009 * write adress of Rx list and send Rx GO command, ack
1010 * interrupt and enable interrupts in one command
1011 */
1012 TL_HR_WRITE(sc, TL_HOST_CH_PARM,
1013 vtophys(&sc->active_Rx->hw_list));
1014 TL_HR_WRITE(sc, TL_HOST_CMD,
1015 HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | ack | int_type |
1016 HOST_CMD_ACK | HOST_CMD_IntOn);
1017 return 1;
1018 }
1019 case TL_INTR_TxEOF:
1020 case TL_INTR_TxEOC:
1021 while ((Tx = sc->active_Tx) != NULL) {
1022 if((Tx->hw_list.stat & TL_TX_CSTAT_CPLT) == 0)
1023 break;
1024 ack++;
1025 #ifdef TLDEBUG_TX
1026 printf("TL_INTR_TxEOC: list 0x%xp done\n",
1027 vtophys(&Tx->hw_list));
1028 #endif
1029 Tx->hw_list.stat = 0;
1030 m_freem(Tx->m);
1031 Tx->m = NULL;
1032 sc->active_Tx = Tx->next;
1033 if (sc->active_Tx == NULL)
1034 sc->last_Tx = NULL;
1035 Tx->next = sc->Free_Tx;
1036 sc->Free_Tx = Tx;
1037 }
1038 /* if this was an EOC, ACK immediatly */
1039 if (int_type == TL_INTR_TxEOC) {
1040 #ifdef TLDEBUG_TX
1041 printf("TL_INTR_TxEOC: ack %d (will be set to 1)\n",
1042 ack);
1043 #endif
1044 TL_HR_WRITE(sc, TL_HOST_CMD, 1 | int_type |
1045 HOST_CMD_ACK | HOST_CMD_IntOn);
1046 if ( sc->active_Tx != NULL) {
1047 /* needs a Tx go command */
1048 TL_HR_WRITE(sc, TL_HOST_CH_PARM,
1049 vtophys(&sc->active_Tx->hw_list));
1050 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1051 }
1052 sc->tl_if.if_timer = 0;
1053 if (sc->tl_if.if_snd.ifq_head != NULL)
1054 tl_ifstart(&sc->tl_if);
1055 return 1;
1056 }
1057 #ifdef TLDEBUG
1058 else {
1059 printf("TL_INTR_TxEOF: ack %d\n", ack);
1060 }
1061 #endif
1062 sc->tl_if.if_timer = 0;
1063 if (sc->tl_if.if_snd.ifq_head != NULL)
1064 tl_ifstart(&sc->tl_if);
1065 break;
1066 case TL_INTR_Stat:
1067 ack++;
1068 #ifdef TLDEBUG
1069 printf("TL_INTR_Stat: ack %d\n", ack);
1070 #endif
1071 tl_read_stats(sc);
1072 break;
1073 case TL_INTR_Adc:
1074 if (int_reg & TL_INTVec_MASK) {
1075 /* adapter check conditions */
1076 printf("%s: check condition, intvect=0x%x, "
1077 "ch_param=0x%x\n", sc->sc_dev.dv_xname,
1078 int_reg & TL_INTVec_MASK,
1079 TL_HR_READ(sc, TL_HOST_CH_PARM));
1080 tl_reset(sc);
1081 /* shedule reinit of the board */
1082 timeout(tl_restart, sc, 1);
1083 return(1);
1084 } else {
1085 u_int8_t netstat;
1086 /* Network status */
1087 netstat =
1088 tl_intreg_read_byte(sc, TL_INT_NET+TL_INT_NetSts);
1089 printf("%s: network status, NetSts=%x\n",
1090 sc->sc_dev.dv_xname, netstat);
1091 /* Ack interrupts */
1092 tl_intreg_write_byte(sc, TL_INT_NET+TL_INT_NetSts,
1093 netstat);
1094 ack++;
1095 }
1096 break;
1097 default:
1098 printf("%s: unhandled interrupt code %x!\n",
1099 sc->sc_dev.dv_xname, int_type);
1100 ack++;
1101 }
1102
1103 if (ack) {
1104 /* Ack the interrupt and enable interrupts */
1105 TL_HR_WRITE(sc, TL_HOST_CMD, ack | int_type | HOST_CMD_ACK |
1106 HOST_CMD_IntOn);
1107 return 1;
1108 }
1109 /* ack = 0 ; interrupt was perhaps not our. Just enable interrupts */
1110 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOn);
1111 return 0;
1112 }
1113
1114 static int
1115 tl_ifioctl(ifp, cmd, data)
1116 struct ifnet *ifp;
1117 ioctl_cmd_t cmd;
1118 caddr_t data;
1119 {
1120 struct tl_softc *sc = ifp->if_softc;
1121 struct ifreq *ifr = (struct ifreq *)data;
1122 int s, error;
1123
1124 s = splnet();
1125 switch(cmd) {
1126 case SIOCSIFADDR: {
1127 struct ifaddr *ifa = (struct ifaddr *)data;
1128 sc->tl_if.if_flags |= IFF_UP;
1129 if ((error = tl_init(sc)) != NULL) {
1130 sc->tl_if.if_flags &= ~IFF_UP;
1131 break;
1132 }
1133 switch (ifa->ifa_addr->sa_family) {
1134 #ifdef INET
1135 case AF_INET:
1136 arp_ifinit(ifp, ifa);
1137 break;
1138 #endif
1139 #ifdef NS
1140 case AF_NS: {
1141 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1142
1143 if (ns_nullhost(*ina))
1144 ina->x_host =
1145 *(union ns_host*) LLADDR(ifp->if_sadl);
1146 else
1147 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1148 ifp->if_addrlen);
1149 break;
1150 }
1151 #endif
1152 default:
1153 break;
1154 }
1155 break;
1156 }
1157 case SIOCSIFFLAGS:
1158 {
1159 u_int8_t reg;
1160 /*
1161 * If interface is marked up and not running, then start it.
1162 * If it is marked down and running, stop it.
1163 */
1164 if (ifp->if_flags & IFF_UP) {
1165 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1166 error = tl_init(sc);
1167 /* all flags have been handled by init */
1168 break;
1169 }
1170 error = 0;
1171 reg = tl_intreg_read_byte(sc,
1172 TL_INT_NET + TL_INT_NetCmd);
1173 if (ifp->if_flags & IFF_PROMISC)
1174 reg |= TL_NETCOMMAND_CAF;
1175 else
1176 reg &= ~TL_NETCOMMAND_CAF;
1177 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd,
1178 reg);
1179 #ifdef TL_PRIV_STATS
1180 if (ifp->if_flags & IFF_LINK0) {
1181 ifp->if_flags &= ~IFF_LINK0;
1182 printf("%s errors statistics\n",
1183 sc->sc_dev.dv_xname);
1184 printf(" %4d RX buffer overrun\n",
1185 sc->ierr_overr);
1186 printf(" %4d RX code error\n",
1187 sc->ierr_code);
1188 printf(" %4d RX crc error\n",
1189 sc->ierr_crc);
1190 printf(" %4d RX out of memory\n",
1191 sc->ierr_nomem);
1192 printf(" %4d TX buffer underrun\n",
1193 sc->oerr_underr);
1194 printf(" %4d TX deffered frames\n",
1195 sc->oerr_deffered);
1196 printf(" %4d TX single collisions\n",
1197 sc->oerr_coll);
1198 printf(" %4d TX multi collisions\n",
1199 sc->oerr_multicoll);
1200 printf(" %4d TX exessive collisions\n",
1201 sc->oerr_exesscoll);
1202 printf(" %4d TX late collisions\n",
1203 sc->oerr_latecoll);
1204 printf(" %4d TX carrier loss\n",
1205 sc->oerr_carrloss);
1206 printf(" %4d TX mbuf copy\n",
1207 sc->oerr_mcopy);
1208 }
1209 #endif
1210 } else {
1211 if (ifp->if_flags & IFF_RUNNING)
1212 tl_shutdown(sc);
1213 error = 0;
1214 }
1215 break;
1216 }
1217 case SIOCADDMULTI:
1218 case SIOCDELMULTI:
1219 /*
1220 * Update multicast listeners
1221 */
1222 if (cmd == SIOCADDMULTI)
1223 error = ether_addmulti(ifr, &sc->tl_ec);
1224 else
1225 error = ether_delmulti(ifr, &sc->tl_ec);
1226 if (error == ENETRESET) {
1227 tl_addr_filter(sc);
1228 error = 0;
1229 }
1230 break;
1231 case SIOCSIFMEDIA:
1232 case SIOCGIFMEDIA:
1233 error = ifmedia_ioctl(ifp, ifr, &sc->tl_mii.mii_media, cmd);
1234 break;
1235 default:
1236 error = EINVAL;
1237 }
1238 splx(s);
1239 return error;
1240 }
1241
1242 static void
1243 tl_ifstart(ifp)
1244 struct ifnet *ifp;
1245 {
1246 tl_softc_t *sc = ifp->if_softc;
1247 struct mbuf *m, *mb_head;
1248 struct Tx_list *Tx;
1249 int segment, size;
1250
1251 txloop:
1252 /* If we don't have more space ... */
1253 if (sc->Free_Tx == NULL) {
1254 #ifdef TLDEBUG
1255 printf("tl_ifstart: No free TX list\n");
1256 #endif
1257 return;
1258 }
1259 /* Grab a paquet for output */
1260 IF_DEQUEUE(&ifp->if_snd, mb_head);
1261 if (mb_head == NULL) {
1262 #ifdef TLDEBUG_TX
1263 printf("tl_ifstart: nothing to send\n");
1264 #endif
1265 return;
1266 }
1267 Tx = sc->Free_Tx;
1268 sc->Free_Tx = Tx->next;
1269 /*
1270 * Go through each of the mbufs in the chain and initialize
1271 * the transmit list descriptors with the physical address
1272 * and size of the mbuf.
1273 */
1274 tbdinit:
1275 bzero(Tx, sizeof(struct Tx_list));
1276 Tx->m = mb_head;
1277 size = 0;
1278 for (m = mb_head, segment = 0; m != NULL ; m = m->m_next) {
1279 if (m->m_len != 0) {
1280 if (segment == TL_NSEG)
1281 break;
1282 size += m->m_len;
1283 Tx->hw_list.seg[segment].data_addr =
1284 vtophys(mtod(m, vaddr_t));
1285 Tx->hw_list.seg[segment].data_count = m->m_len;
1286 segment++;
1287 }
1288 }
1289 if (m != NULL || (size < ETHER_MIN_TX && segment == TL_NSEG)) {
1290 /*
1291 * We ran out of segments, or we will. We have to recopy this
1292 * mbuf chain first.
1293 */
1294 struct mbuf *mn;
1295 #ifdef TLDEBUG_TX
1296 printf("tl_ifstart: need to copy mbuf\n");
1297 #endif
1298 #ifdef TL_PRIV_STATS
1299 sc->oerr_mcopy++;
1300 #endif
1301 MGETHDR(mn, M_DONTWAIT, MT_DATA);
1302 if (mn == NULL) {
1303 m_freem(mb_head);
1304 goto bad;
1305 }
1306 if (mb_head->m_pkthdr.len > MHLEN) {
1307 MCLGET(mn, M_DONTWAIT);
1308 if ((mn->m_flags & M_EXT) == 0) {
1309 m_freem(mn);
1310 m_freem(mb_head);
1311 goto bad;
1312 }
1313 }
1314 m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1315 mtod(mn, caddr_t));
1316 mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
1317 m_freem(mb_head);
1318 mb_head = mn;
1319 goto tbdinit;
1320 }
1321 /* We are at end of mbuf chain. check the size and
1322 * see if it needs to be extended
1323 */
1324 if (size < ETHER_MIN_TX) {
1325 #ifdef DIAGNOSTIC
1326 if (segment >= TL_NSEG) {
1327 panic("tl_ifstart: to much segmets (%d)\n", segment);
1328 }
1329 #endif
1330 /*
1331 * add the nullbuf in the seg
1332 */
1333 Tx->hw_list.seg[segment].data_count =
1334 ETHER_MIN_TX - size;
1335 Tx->hw_list.seg[segment].data_addr =
1336 vtophys(nullbuf);
1337 size = ETHER_MIN_TX;
1338 segment++;
1339 }
1340 /* The list is done, finish the list init */
1341 Tx->hw_list.seg[segment-1].data_count |=
1342 TL_LAST_SEG;
1343 Tx->hw_list.stat = (size << 16) | 0x3000;
1344 #ifdef TLDEBUG_TX
1345 printf("%s: sending, Tx : stat = 0x%x\n", sc->sc_dev.dv_xname,
1346 Tx->hw_list.stat);
1347 #if 0
1348 for(segment = 0; segment < TL_NSEG; segment++) {
1349 printf(" seg %d addr 0x%x len 0x%x\n",
1350 segment,
1351 Tx->hw_list.seg[segment].data_addr,
1352 Tx->hw_list.seg[segment].data_count);
1353 }
1354 #endif
1355 #endif
1356 sc->opkt++;
1357 if (sc->active_Tx == NULL) {
1358 sc->active_Tx = sc->last_Tx = Tx;
1359 #ifdef TLDEBUG_TX
1360 printf("%s: Tx GO, addr=0x%x\n", sc->sc_dev.dv_xname,
1361 vtophys(&Tx->hw_list));
1362 #endif
1363 TL_HR_WRITE(sc, TL_HOST_CH_PARM, vtophys(&Tx->hw_list));
1364 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1365 } else {
1366 #ifdef TLDEBUG_TX
1367 printf("%s: Tx addr=0x%x queued\n", sc->sc_dev.dv_xname,
1368 vtophys(&Tx->hw_list));
1369 #endif
1370 sc->last_Tx->hw_list.fwd = vtophys(&Tx->hw_list);
1371 sc->last_Tx->next = Tx;
1372 sc->last_Tx = Tx;
1373 #ifdef DIAGNOSTIC
1374 if (sc->last_Tx->hw_list.fwd & 0x7)
1375 printf("%s: physical addr 0x%x of list not properly "
1376 "aligned\n",
1377 sc->sc_dev.dv_xname, sc->last_Rx->hw_list.fwd);
1378 #endif
1379 }
1380 #if NBPFILTER > 0
1381 /* Pass packet to bpf if there is a listener */
1382 if (ifp->if_bpf)
1383 bpf_mtap(ifp->if_bpf, mb_head);
1384 #endif
1385 /*
1386 * Set a 5 second timer just in case we don't hear from the card again.
1387 */
1388 ifp->if_timer = 5;
1389 goto txloop;
1390 bad:
1391 #ifdef TLDEBUG
1392 printf("tl_ifstart: Out of mbuf, Tx pkt lost\n");
1393 #endif
1394 Tx->next = sc->Free_Tx;
1395 sc->Free_Tx = Tx;
1396 return;
1397 }
1398
1399 static void
1400 tl_ifwatchdog(ifp)
1401 struct ifnet *ifp;
1402 {
1403 tl_softc_t *sc = ifp->if_softc;
1404
1405 if ((ifp->if_flags & IFF_RUNNING) == 0)
1406 return;
1407 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1408 ifp->if_oerrors++;
1409 tl_init(sc);
1410 }
1411
1412 static int
1413 tl_mediachange(ifp)
1414 struct ifnet *ifp;
1415 {
1416
1417 if (ifp->if_flags & IFF_UP)
1418 tl_init(ifp->if_softc);
1419 return (0);
1420 }
1421
1422 static void
1423 tl_mediastatus(ifp, ifmr)
1424 struct ifnet *ifp;
1425 struct ifmediareq *ifmr;
1426 {
1427 tl_softc_t *sc = ifp->if_softc;
1428
1429 mii_pollstat(&sc->tl_mii);
1430 ifmr->ifm_active = sc->tl_mii.mii_media_active;
1431 ifmr->ifm_status = sc->tl_mii.mii_media_status;
1432 }
1433
1434 static int tl_add_RxBuff(Rx, oldm)
1435 struct Rx_list *Rx;
1436 struct mbuf *oldm;
1437 {
1438 struct mbuf *m;
1439
1440 MGETHDR(m, M_DONTWAIT, MT_DATA);
1441 if (m != NULL) {
1442 MCLGET(m, M_DONTWAIT);
1443 if ((m->m_flags & M_EXT) == 0) {
1444 m_freem(m);
1445 if (oldm == NULL)
1446 return 0;
1447 m = oldm;
1448 m->m_data = m->m_ext.ext_buf;
1449 }
1450 } else {
1451 if (oldm == NULL)
1452 return 0;
1453 m = oldm;
1454 m->m_data = m->m_ext.ext_buf;
1455 }
1456 /*
1457 * Move the data pointer up so that the incoming data packet
1458 * will be 32-bit aligned.
1459 */
1460 m->m_data += 2;
1461
1462 /* (re)init the Rx_list struct */
1463
1464 Rx->m = m;
1465 Rx->hw_list.stat = ((MCLBYTES -2) << 16) | 0x3000;
1466 Rx->hw_list.seg.data_count = (MCLBYTES -2);
1467 Rx->hw_list.seg.data_addr = vtophys(m->m_data);
1468 return (m != oldm);
1469 }
1470
1471 static void tl_ticks(v)
1472 void *v;
1473 {
1474 tl_softc_t *sc = v;
1475
1476 tl_read_stats(sc);
1477
1478 /* Tick the MII. */
1479 mii_tick(&sc->tl_mii);
1480
1481 if (sc->opkt > 0) {
1482 if (sc->oerr_exesscoll > sc->opkt / 100) {
1483 /* exess collisions */
1484 if (sc->tl_flags & TL_IFACT) /* only print once */
1485 printf("%s: no carrier\n",
1486 sc->sc_dev.dv_xname);
1487 sc->tl_flags &= ~TL_IFACT;
1488 } else
1489 sc->tl_flags |= TL_IFACT;
1490 sc->oerr_exesscoll = sc->opkt = 0;
1491 sc->tl_lasttx = 0;
1492 } else {
1493 sc->tl_lasttx++;
1494 if (sc->tl_lasttx >= TL_IDLETIME) {
1495 /*
1496 * No TX activity in the last TL_IDLETIME seconds.
1497 * sends a LLC Class1 TEST pkt
1498 */
1499 struct mbuf *m;
1500 int s;
1501 MGETHDR(m, M_DONTWAIT, MT_DATA);
1502 if (m != NULL) {
1503 #ifdef TLDEBUG
1504 printf("tl_ticks: sending LLC test pkt\n");
1505 #endif
1506 bcopy(sc->tl_enaddr,
1507 mtod(m, struct ether_header *)->ether_dhost,
1508 6);
1509 bcopy(sc->tl_enaddr,
1510 mtod(m, struct ether_header *)->ether_shost,
1511 6);
1512 mtod(m, struct ether_header *)->ether_type =
1513 htons(3);
1514 mtod(m, unsigned char *)[14] = 0;
1515 mtod(m, unsigned char *)[15] = 0;
1516 mtod(m, unsigned char *)[16] = 0xE3;
1517 /* LLC Class1 TEST (no poll) */
1518 m->m_len = m->m_pkthdr.len =
1519 sizeof(struct ether_header) + 3;
1520 s = splnet();
1521 IF_PREPEND(&sc->tl_if.if_snd, m);
1522 tl_ifstart(&sc->tl_if);
1523 splx(s);
1524 }
1525 }
1526 }
1527
1528 /* read statistics every seconds */
1529 timeout(tl_ticks, v, hz);
1530 }
1531
1532 static void
1533 tl_read_stats(sc)
1534 tl_softc_t *sc;
1535 {
1536 u_int32_t reg;
1537 int ierr_overr;
1538 int ierr_code;
1539 int ierr_crc;
1540 int oerr_underr;
1541 int oerr_deffered;
1542 int oerr_coll;
1543 int oerr_multicoll;
1544 int oerr_exesscoll;
1545 int oerr_latecoll;
1546 int oerr_carrloss;
1547 struct ifnet *ifp = &sc->tl_if;
1548
1549 reg = tl_intreg_read(sc, TL_INT_STATS_TX);
1550 ifp->if_opackets += reg & 0x00ffffff;
1551 oerr_underr = reg >> 24;
1552
1553 reg = tl_intreg_read(sc, TL_INT_STATS_RX);
1554 ifp->if_ipackets += reg & 0x00ffffff;
1555 ierr_overr = reg >> 24;
1556
1557 reg = tl_intreg_read(sc, TL_INT_STATS_FERR);
1558 ierr_crc = (reg & TL_FERR_CRC) >> 16;
1559 ierr_code = (reg & TL_FERR_CODE) >> 24;
1560 oerr_deffered = (reg & TL_FERR_DEF);
1561
1562 reg = tl_intreg_read(sc, TL_INT_STATS_COLL);
1563 oerr_multicoll = (reg & TL_COL_MULTI);
1564 oerr_coll = (reg & TL_COL_SINGLE) >> 16;
1565
1566 reg = tl_intreg_read(sc, TL_INT_LERR);
1567 oerr_exesscoll = (reg & TL_LERR_ECOLL);
1568 oerr_latecoll = (reg & TL_LERR_LCOLL) >> 8;
1569 oerr_carrloss = (reg & TL_LERR_CL) >> 16;
1570
1571
1572 sc->stats_exesscoll += oerr_exesscoll;
1573 ifp->if_oerrors += oerr_underr + oerr_exesscoll + oerr_latecoll +
1574 oerr_carrloss;
1575 ifp->if_collisions += oerr_coll + oerr_multicoll;
1576 ifp->if_ierrors += ierr_overr + ierr_code + ierr_crc;
1577
1578 if (ierr_overr)
1579 printf("%s: receiver ring buffer overrun\n",
1580 sc->sc_dev.dv_xname);
1581 if (oerr_underr)
1582 printf("%s: transmit buffer underrun\n",
1583 sc->sc_dev.dv_xname);
1584 #ifdef TL_PRIV_STATS
1585 sc->ierr_overr += ierr_overr;
1586 sc->ierr_code += ierr_code;
1587 sc->ierr_crc += ierr_crc;
1588 sc->oerr_underr += oerr_underr;
1589 sc->oerr_deffered += oerr_deffered;
1590 sc->oerr_coll += oerr_coll;
1591 sc->oerr_multicoll += oerr_multicoll;
1592 sc->oerr_exesscoll += oerr_exesscoll;
1593 sc->oerr_latecoll += oerr_latecoll;
1594 sc->oerr_carrloss += oerr_carrloss;
1595 #endif
1596 }
1597
1598 static void tl_addr_filter(sc)
1599 tl_softc_t *sc;
1600 {
1601 struct ether_multistep step;
1602 struct ether_multi *enm;
1603 u_int32_t hash[2] = {0, 0};
1604 int i;
1605
1606 sc->tl_if.if_flags &= ~IFF_ALLMULTI;
1607 ETHER_FIRST_MULTI(step, &sc->tl_ec, enm);
1608 while (enm != NULL) {
1609 #ifdef TLDEBUG
1610 printf("tl_addr_filter: addrs %s %s\n",
1611 ether_sprintf(enm->enm_addrlo),
1612 ether_sprintf(enm->enm_addrhi));
1613 #endif
1614 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
1615 i = tl_multicast_hash(enm->enm_addrlo);
1616 hash[i/32] |= 1 << (i%32);
1617 } else {
1618 hash[0] = hash[1] = 0xffffffff;
1619 sc->tl_if.if_flags |= IFF_ALLMULTI;
1620 break;
1621 }
1622 ETHER_NEXT_MULTI(step, enm);
1623 }
1624 #ifdef TLDEBUG
1625 printf("tl_addr_filer: hash1 %x has2 %x\n", hash[0], hash[1]);
1626 #endif
1627 tl_intreg_write(sc, TL_INT_HASH1, hash[0]);
1628 tl_intreg_write(sc, TL_INT_HASH2, hash[1]);
1629 }
1630
1631 static int tl_multicast_hash(a)
1632 u_int8_t *a;
1633 {
1634 int hash;
1635
1636 #define DA(addr,bit) (addr[5 - (bit/8)] & (1 << bit%8))
1637 #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)
1638
1639 hash = xor8( DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30),
1640 DA(a,36), DA(a,42));
1641 hash |= xor8( DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31),
1642 DA(a,37), DA(a,43)) << 1;
1643 hash |= xor8( DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32),
1644 DA(a,38), DA(a,44)) << 2;
1645 hash |= xor8( DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33),
1646 DA(a,39), DA(a,45)) << 3;
1647 hash |= xor8( DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34),
1648 DA(a,40), DA(a,46)) << 4;
1649 hash |= xor8( DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35),
1650 DA(a,41), DA(a,47)) << 5;
1651
1652 return hash;
1653 }
1654
1655 #if defined(TLDEBUG_RX)
1656 void
1657 ether_printheader(eh)
1658 struct ether_header *eh;
1659 {
1660 u_char *c = (char*)eh;
1661 int i;
1662 for (i=0; i<sizeof(struct ether_header); i++)
1663 printf("%x ", (u_int)c[i]);
1664 printf("\n");
1665 }
1666 #endif
1667