if_tl.c revision 1.24 1 /* $NetBSD: if_tl.c,v 1.24 1999/05/18 23:52:58 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_phy_probe(self, &sc->tl_mii, 0xffffffff);
432 if (LIST_FIRST(&sc->tl_mii.mii_phys) == NULL) {
433 ifmedia_add(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
434 ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE);
435 } else
436 ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_AUTO);
437
438 bcopy(sc->sc_dev.dv_xname, sc->tl_if.if_xname, IFNAMSIZ);
439 sc->tl_if.if_softc = sc;
440 ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
441 ifp->if_ioctl = tl_ifioctl;
442 ifp->if_start = tl_ifstart;
443 ifp->if_watchdog = tl_ifwatchdog;
444 ifp->if_timer = 0;
445 if_attach(ifp);
446 ether_ifattach(&(sc)->tl_if, (sc)->tl_enaddr);
447 #if NBPFILTER > 0
448 bpfattach(&sc->tl_bpf, &sc->tl_if, DLT_EN10MB,
449 sizeof(struct ether_header));
450 #endif
451 }
452
453 static void
454 tl_reset(sc)
455 tl_softc_t *sc;
456 {
457 int i;
458
459 /* read stats */
460 if (sc->tl_if.if_flags & IFF_RUNNING) {
461 untimeout(tl_ticks, sc);
462 tl_read_stats(sc);
463 }
464 /* Reset adapter */
465 TL_HR_WRITE(sc, TL_HOST_CMD,
466 TL_HR_READ(sc, TL_HOST_CMD) | HOST_CMD_Ad_Rst);
467 DELAY(100000);
468 /* Disable interrupts */
469 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
470 /* setup aregs & hash */
471 for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
472 tl_intreg_write(sc, i, 0);
473 #ifdef TLDEBUG_ADDR
474 printf("Areg & hash registers: \n");
475 for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
476 printf(" reg %x: %x\n", i, tl_intreg_read(sc, i));
477 #endif
478 /* Setup NetConfig */
479 tl_intreg_write(sc, TL_INT_NetConfig,
480 TL_NETCONFIG_1F | TL_NETCONFIG_1chn | TL_NETCONFIG_PHY_EN);
481 /* Bsize: accept default */
482 /* TX commit in Acommit: accept default */
483 /* Load Ld_tmr and Ld_thr */
484 /* Ld_tmr = 3 */
485 TL_HR_WRITE(sc, TL_HOST_CMD, 0x3 | HOST_CMD_LdTmr);
486 /* Ld_thr = 0 */
487 TL_HR_WRITE(sc, TL_HOST_CMD, 0x0 | HOST_CMD_LdThr);
488 /* Unreset MII */
489 netsio_set(sc, TL_NETSIO_NMRST);
490 DELAY(100000);
491 sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
492 sc->tl_flags = 0;
493 sc->opkt = 0;
494 sc->stats_exesscoll = 0;
495 }
496
497 static void tl_shutdown(v)
498 void *v;
499 {
500 tl_softc_t *sc = v;
501 struct Tx_list *Tx;
502 int i;
503
504 if ((sc->tl_if.if_flags & IFF_RUNNING) == 0)
505 return;
506 /* disable interrupts */
507 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
508 /* stop TX and RX channels */
509 TL_HR_WRITE(sc, TL_HOST_CMD,
510 HOST_CMD_STOP | HOST_CMD_RT | HOST_CMD_Nes);
511 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_STOP);
512 DELAY(100000);
513
514 /* stop statistics reading loop, read stats */
515 untimeout(tl_ticks, sc);
516 tl_read_stats(sc);
517
518 /* deallocate memory allocations */
519 for (i=0; i< TL_NBUF; i++) {
520 if (sc->Rx_list[i].m)
521 m_freem(sc->Rx_list[i].m);
522 sc->Rx_list[i].m = NULL;
523 }
524 free(sc->Rx_list, M_DEVBUF);
525 sc->Rx_list = NULL;
526 while ((Tx = sc->active_Tx) != NULL) {
527 Tx->hw_list.stat = 0;
528 m_freem(Tx->m);
529 sc->active_Tx = Tx->next;
530 Tx->next = sc->Free_Tx;
531 sc->Free_Tx = Tx;
532 }
533 sc->last_Tx = NULL;
534 free(sc->Tx_list, M_DEVBUF);
535 sc->Tx_list = NULL;
536 sc->tl_if.if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
537 sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
538 sc->tl_flags = 0;
539 }
540
541 static void tl_restart(v)
542 void *v;
543 {
544 tl_init(v);
545 }
546
547 static int tl_init(sc)
548 tl_softc_t *sc;
549 {
550 struct ifnet *ifp = &sc->tl_if;
551 int i, s;
552
553 s = splnet();
554 /* cancel any pending IO */
555 tl_shutdown(sc);
556 tl_reset(sc);
557 if ((sc->tl_if.if_flags & IFF_UP) == 0) {
558 splx(s);
559 return 0;
560 }
561 /* Set various register to reasonable value */
562 /* setup NetCmd in promisc mode if needed */
563 i = (ifp->if_flags & IFF_PROMISC) ? TL_NETCOMMAND_CAF : 0;
564 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd,
565 TL_NETCOMMAND_NRESET | TL_NETCOMMAND_NWRAP | i);
566 /* Max receive size : MCLBYTES */
567 tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxL, MCLBYTES & 0xff);
568 tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxH,
569 (MCLBYTES >> 8) & 0xff);
570
571 /* init MAC addr */
572 for (i = 0; i < ETHER_ADDR_LEN; i++)
573 tl_intreg_write_byte(sc, TL_INT_Areg0 + i , sc->tl_enaddr[i]);
574 /* add multicast filters */
575 tl_addr_filter(sc);
576 #ifdef TLDEBUG_ADDR
577 printf("Wrote Mac addr, Areg & hash registers are now: \n");
578 for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
579 printf(" reg %x: %x\n", i, tl_intreg_read(sc, i));
580 #endif
581
582 /* Pre-allocate receivers mbuf, make the lists */
583 sc->Rx_list = malloc(sizeof(struct Rx_list) * TL_NBUF, M_DEVBUF,
584 M_NOWAIT);
585 sc->Tx_list = malloc(sizeof(struct Tx_list) * TL_NBUF, M_DEVBUF,
586 M_NOWAIT);
587 if (sc->Rx_list == NULL || sc->Tx_list == NULL) {
588 printf("%s: out of memory for lists\n", sc->sc_dev.dv_xname);
589 sc->tl_if.if_flags &= ~IFF_UP;
590 splx(s);
591 return ENOMEM;
592 }
593 for (i=0; i< TL_NBUF; i++) {
594 if (tl_add_RxBuff(&sc->Rx_list[i], NULL) == 0) {
595 printf("%s: out of mbuf for receive list\n",
596 sc->sc_dev.dv_xname);
597 sc->tl_if.if_flags &= ~IFF_UP;
598 splx(s);
599 return ENOMEM;
600 }
601 if (i > 0) { /* chain the list */
602 sc->Rx_list[i-1].next = &sc->Rx_list[i];
603 sc->Rx_list[i-1].hw_list.fwd =
604 vtophys(&sc->Rx_list[i].hw_list);
605 #ifdef DIAGNOSTIC
606 if (sc->Rx_list[i-1].hw_list.fwd & 0x7)
607 printf("%s: physical addr 0x%x of list not "
608 "properly aligned\n",
609 sc->sc_dev.dv_xname,
610 sc->Rx_list[i-1].hw_list.fwd);
611 #endif
612 sc->Tx_list[i-1].next = &sc->Tx_list[i];
613 }
614 }
615 sc->Rx_list[TL_NBUF-1].next = NULL;
616 sc->Rx_list[TL_NBUF-1].hw_list.fwd = 0;
617 sc->Tx_list[TL_NBUF-1].next = NULL;
618
619 sc->active_Rx = &sc->Rx_list[0];
620 sc->last_Rx = &sc->Rx_list[TL_NBUF-1];
621 sc->active_Tx = sc->last_Tx = NULL;
622 sc->Free_Tx = &sc->Tx_list[0];
623
624 if (nullbuf == NULL)
625 nullbuf = malloc(ETHER_MIN_TX, M_DEVBUF, M_NOWAIT);
626 if (nullbuf == NULL) {
627 printf("%s: can't allocate space for pad buffer\n",
628 sc->sc_dev.dv_xname);
629 sc->tl_if.if_flags &= ~IFF_UP;
630 splx(s);
631 return ENOMEM;
632 }
633 bzero(nullbuf, ETHER_MIN_TX);
634
635 /* set media */
636 mii_mediachg(&sc->tl_mii);
637
638 /* start ticks calls */
639 timeout(tl_ticks, sc, hz);
640 /* write adress of Rx list and enable interrupts */
641 TL_HR_WRITE(sc, TL_HOST_CH_PARM, vtophys(&sc->Rx_list[0].hw_list));
642 TL_HR_WRITE(sc, TL_HOST_CMD,
643 HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | HOST_CMD_IntOn);
644 sc->tl_if.if_flags |= IFF_RUNNING;
645 sc->tl_if.if_flags &= ~IFF_OACTIVE;
646 return 0;
647 }
648
649
650 static u_int32_t
651 tl_intreg_read(sc, reg)
652 tl_softc_t *sc;
653 u_int32_t reg;
654 {
655 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
656 return TL_HR_READ(sc, TL_HOST_DIO_DATA);
657 }
658
659 static u_int8_t
660 tl_intreg_read_byte(sc, reg)
661 tl_softc_t *sc;
662 u_int32_t reg;
663 {
664 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
665 (reg & (~0x07)) & TL_HOST_DIOADR_MASK);
666 return TL_HR_READ_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x07));
667 }
668
669 static void
670 tl_intreg_write(sc, reg, val)
671 tl_softc_t *sc;
672 u_int32_t reg;
673 u_int32_t val;
674 {
675 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
676 TL_HR_WRITE(sc, TL_HOST_DIO_DATA, val);
677 }
678
679 static void
680 tl_intreg_write_byte(sc, reg, val)
681 tl_softc_t *sc;
682 u_int32_t reg;
683 u_int8_t val;
684 {
685 TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
686 (reg & (~0x03)) & TL_HOST_DIOADR_MASK);
687 TL_HR_WRITE_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x03), val);
688 }
689
690 void
691 tl_mii_sync(sc)
692 struct tl_softc *sc;
693 {
694 int i;
695
696 netsio_clr(sc, TL_NETSIO_MTXEN);
697 for (i = 0; i < 32; i++) {
698 netsio_clr(sc, TL_NETSIO_MCLK);
699 netsio_set(sc, TL_NETSIO_MCLK);
700 }
701 }
702
703 void
704 tl_mii_sendbits(sc, data, nbits)
705 struct tl_softc *sc;
706 u_int32_t data;
707 int nbits;
708 {
709 int i;
710
711 netsio_set(sc, TL_NETSIO_MTXEN);
712 for (i = 1 << (nbits - 1); i; i = i >> 1) {
713 netsio_clr(sc, TL_NETSIO_MCLK);
714 netsio_read(sc, TL_NETSIO_MCLK);
715 if (data & i)
716 netsio_set(sc, TL_NETSIO_MDATA);
717 else
718 netsio_clr(sc, TL_NETSIO_MDATA);
719 netsio_set(sc, TL_NETSIO_MCLK);
720 netsio_read(sc, TL_NETSIO_MCLK);
721 }
722 }
723
724 int
725 tl_mii_read(self, phy, reg)
726 struct device *self;
727 int phy, reg;
728 {
729 struct tl_softc *sc = (struct tl_softc *)self;
730 int val = 0, i, err;
731
732 /*
733 * Read the PHY register by manually driving the MII control lines.
734 */
735
736 tl_mii_sync(sc);
737 tl_mii_sendbits(sc, MII_COMMAND_START, 2);
738 tl_mii_sendbits(sc, MII_COMMAND_READ, 2);
739 tl_mii_sendbits(sc, phy, 5);
740 tl_mii_sendbits(sc, reg, 5);
741
742 netsio_clr(sc, TL_NETSIO_MTXEN);
743 netsio_clr(sc, TL_NETSIO_MCLK);
744 netsio_set(sc, TL_NETSIO_MCLK);
745 netsio_clr(sc, TL_NETSIO_MCLK);
746
747 err = netsio_read(sc, TL_NETSIO_MDATA);
748 netsio_set(sc, TL_NETSIO_MCLK);
749
750 /* Even if an error occurs, must still clock out the cycle. */
751 for (i = 0; i < 16; i++) {
752 val <<= 1;
753 netsio_clr(sc, TL_NETSIO_MCLK);
754 if (err == 0 && netsio_read(sc, TL_NETSIO_MDATA))
755 val |= 1;
756 netsio_set(sc, TL_NETSIO_MCLK);
757 }
758 netsio_clr(sc, TL_NETSIO_MCLK);
759 netsio_set(sc, TL_NETSIO_MCLK);
760
761 return (err ? 0 : val);
762 }
763
764 void
765 tl_mii_write(self, phy, reg, val)
766 struct device *self;
767 int phy, reg, val;
768 {
769 struct tl_softc *sc = (struct tl_softc *)self;
770
771 /*
772 * Write the PHY register by manually driving the MII control lines.
773 */
774
775 tl_mii_sync(sc);
776 tl_mii_sendbits(sc, MII_COMMAND_START, 2);
777 tl_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
778 tl_mii_sendbits(sc, phy, 5);
779 tl_mii_sendbits(sc, reg, 5);
780 tl_mii_sendbits(sc, MII_COMMAND_ACK, 2);
781 tl_mii_sendbits(sc, val, 16);
782
783 netsio_clr(sc, TL_NETSIO_MCLK);
784 netsio_set(sc, TL_NETSIO_MCLK);
785 }
786
787 void
788 tl_statchg(self)
789 struct device *self;
790 {
791 tl_softc_t *sc = (struct tl_softc *)self;
792 u_int32_t reg;
793
794 #ifdef TLDEBUG
795 printf("tl_statchg, media %x\n", sc->tl_ifmedia.ifm_media);
796 #endif
797
798 /*
799 * We must keep the ThunderLAN and the PHY in sync as
800 * to the status of full-duplex!
801 */
802 reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetCmd);
803 if (sc->tl_mii.mii_media_active & IFM_FDX)
804 reg |= TL_NETCOMMAND_DUPLEX;
805 else
806 reg &= ~TL_NETCOMMAND_DUPLEX;
807 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd, reg);
808
809 /* XXX Update ifp->if_baudrate */
810 }
811
812 void tl_i2c_set(v, bit)
813 void *v;
814 u_int8_t bit;
815 {
816 tl_softc_t *sc = v;
817
818 switch (bit) {
819 case I2C_DATA:
820 netsio_set(sc, TL_NETSIO_EDATA);
821 break;
822 case I2C_CLOCK:
823 netsio_set(sc, TL_NETSIO_ECLOCK);
824 break;
825 case I2C_TXEN:
826 netsio_set(sc, TL_NETSIO_ETXEN);
827 break;
828 default:
829 printf("tl_i2c_set: unknown bit %d\n", bit);
830 }
831 return;
832 }
833
834 void tl_i2c_clr(v, bit)
835 void *v;
836 u_int8_t bit;
837 {
838 tl_softc_t *sc = v;
839
840 switch (bit) {
841 case I2C_DATA:
842 netsio_clr(sc, TL_NETSIO_EDATA);
843 break;
844 case I2C_CLOCK:
845 netsio_clr(sc, TL_NETSIO_ECLOCK);
846 break;
847 case I2C_TXEN:
848 netsio_clr(sc, TL_NETSIO_ETXEN);
849 break;
850 default:
851 printf("tl_i2c_clr: unknown bit %d\n", bit);
852 }
853 return;
854 }
855
856 int tl_i2c_read(v, bit)
857 void *v;
858 u_int8_t bit;
859 {
860 tl_softc_t *sc = v;
861
862 switch (bit) {
863 case I2C_DATA:
864 return netsio_read(sc, TL_NETSIO_EDATA);
865 break;
866 case I2C_CLOCK:
867 return netsio_read(sc, TL_NETSIO_ECLOCK);
868 break;
869 case I2C_TXEN:
870 return netsio_read(sc, TL_NETSIO_ETXEN);
871 break;
872 default:
873 printf("tl_i2c_read: unknown bit %d\n", bit);
874 return -1;
875 }
876 }
877
878 static int
879 tl_intr(v)
880 void *v;
881 {
882 tl_softc_t *sc = v;
883 struct ifnet *ifp = &sc->tl_if;
884 struct Rx_list *Rx;
885 struct Tx_list *Tx;
886 struct mbuf *m;
887 u_int32_t int_type, int_reg;
888 int ack = 0;
889 int size;
890
891 int_reg = TL_HR_READ(sc, TL_HOST_INTR_DIOADR);
892 int_type = int_reg & TL_INTR_MASK;
893 if (int_type == 0)
894 return 0;
895 #if defined(TLDEBUG_RX) || defined(TLDEBUG_TX)
896 printf("%s: interrupt type %x, intr_reg %x\n", sc->sc_dev.dv_xname,
897 int_type, int_reg);
898 #endif
899 /* disable interrupts */
900 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
901 switch(int_type & TL_INTR_MASK) {
902 case TL_INTR_RxEOF:
903 while(sc->active_Rx->hw_list.stat & TL_RX_CSTAT_CPLT) {
904 /* dequeue and requeue at end of list */
905 ack++;
906 Rx = sc->active_Rx;
907 sc->active_Rx = Rx->next;
908 m = Rx->m;
909 size = Rx->hw_list.stat >> 16;
910 #ifdef TLDEBUG_RX
911 printf("tl_intr: RX list complete, Rx %p, size=%d\n",
912 Rx, size);
913 #endif
914 if (tl_add_RxBuff(Rx, m ) == 0) {
915 /*
916 * No new mbuf, reuse the same. This means
917 * that this packet
918 * is lost
919 */
920 m = NULL;
921 #ifdef TL_PRIV_STATS
922 sc->ierr_nomem++;
923 #endif
924 #ifdef TLDEBUG
925 printf("%s: out of mbuf, lost input packet\n",
926 sc->sc_dev.dv_xname);
927 #endif
928 }
929 Rx->next = NULL;
930 Rx->hw_list.fwd = 0;
931 sc->last_Rx->hw_list.fwd = vtophys(&Rx->hw_list);
932 #ifdef DIAGNOSTIC
933 if (sc->last_Rx->hw_list.fwd & 0x7)
934 printf("%s: physical addr 0x%x of list not "
935 "properly aligned\n",
936 sc->sc_dev.dv_xname,
937 sc->last_Rx->hw_list.fwd);
938 #endif
939 sc->last_Rx->next = Rx;
940 sc->last_Rx = Rx;
941
942 /* deliver packet */
943 if (m) {
944 struct ether_header *eh;
945 if (size < sizeof(struct ether_header)) {
946 m_freem(m);
947 continue;
948 }
949 m->m_pkthdr.rcvif = ifp;
950 m->m_pkthdr.len = m->m_len = size;
951 eh = mtod(m, struct ether_header *);
952 #ifdef TLDEBUG_RX
953 printf("tl_intr: Rx packet:\n");
954 ether_printheader(eh);
955 #endif
956 #if NBPFILTER > 0
957 if (ifp->if_bpf) {
958 bpf_tap(ifp->if_bpf,
959 mtod(m, caddr_t), size);
960 /*
961 * Only pass this packet up
962 * if it is for us.
963 */
964 if ((ifp->if_flags & IFF_PROMISC) &&
965 /* !mcast and !bcast */
966 (eh->ether_dhost[0] & 1) == 0 &&
967 bcmp(eh->ether_dhost,
968 LLADDR(ifp->if_sadl),
969 sizeof(eh->ether_dhost)) != 0) {
970 m_freem(m);
971 continue;
972 }
973 }
974 #endif /* NBPFILTER > 0 */
975 (*ifp->if_input)(ifp, m);
976 }
977 }
978 #ifdef TLDEBUG_RX
979 printf("TL_INTR_RxEOF: ack %d\n", ack);
980 #else
981 if (ack == 0) {
982 printf("%s: EOF intr without anything to read !\n",
983 sc->sc_dev.dv_xname);
984 tl_reset(sc);
985 /* shedule reinit of the board */
986 timeout(tl_restart, sc, 1);
987 return(1);
988 }
989 #endif
990 break;
991 case TL_INTR_RxEOC:
992 ack++;
993 #ifdef TLDEBUG_RX
994 printf("TL_INTR_RxEOC: ack %d\n", ack);
995 #endif
996 #ifdef DIAGNOSTIC
997 if (sc->active_Rx->hw_list.stat & TL_RX_CSTAT_CPLT) {
998 printf("%s: Rx EOC interrupt and active Rx list not "
999 "cleared\n", sc->sc_dev.dv_xname);
1000 return 0;
1001 } else
1002 #endif
1003 {
1004 /*
1005 * write adress of Rx list and send Rx GO command, ack
1006 * interrupt and enable interrupts in one command
1007 */
1008 TL_HR_WRITE(sc, TL_HOST_CH_PARM,
1009 vtophys(&sc->active_Rx->hw_list));
1010 TL_HR_WRITE(sc, TL_HOST_CMD,
1011 HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | ack | int_type |
1012 HOST_CMD_ACK | HOST_CMD_IntOn);
1013 return 1;
1014 }
1015 case TL_INTR_TxEOF:
1016 case TL_INTR_TxEOC:
1017 while ((Tx = sc->active_Tx) != NULL) {
1018 if((Tx->hw_list.stat & TL_TX_CSTAT_CPLT) == 0)
1019 break;
1020 ack++;
1021 #ifdef TLDEBUG_TX
1022 printf("TL_INTR_TxEOC: list 0x%xp done\n",
1023 vtophys(&Tx->hw_list));
1024 #endif
1025 Tx->hw_list.stat = 0;
1026 m_freem(Tx->m);
1027 Tx->m = NULL;
1028 sc->active_Tx = Tx->next;
1029 if (sc->active_Tx == NULL)
1030 sc->last_Tx = NULL;
1031 Tx->next = sc->Free_Tx;
1032 sc->Free_Tx = Tx;
1033 }
1034 /* if this was an EOC, ACK immediatly */
1035 if (int_type == TL_INTR_TxEOC) {
1036 #ifdef TLDEBUG_TX
1037 printf("TL_INTR_TxEOC: ack %d (will be set to 1)\n",
1038 ack);
1039 #endif
1040 TL_HR_WRITE(sc, TL_HOST_CMD, 1 | int_type |
1041 HOST_CMD_ACK | HOST_CMD_IntOn);
1042 if ( sc->active_Tx != NULL) {
1043 /* needs a Tx go command */
1044 TL_HR_WRITE(sc, TL_HOST_CH_PARM,
1045 vtophys(&sc->active_Tx->hw_list));
1046 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1047 }
1048 sc->tl_if.if_timer = 0;
1049 if (sc->tl_if.if_snd.ifq_head != NULL)
1050 tl_ifstart(&sc->tl_if);
1051 return 1;
1052 }
1053 #ifdef TLDEBUG
1054 else {
1055 printf("TL_INTR_TxEOF: ack %d\n", ack);
1056 }
1057 #endif
1058 sc->tl_if.if_timer = 0;
1059 if (sc->tl_if.if_snd.ifq_head != NULL)
1060 tl_ifstart(&sc->tl_if);
1061 break;
1062 case TL_INTR_Stat:
1063 ack++;
1064 #ifdef TLDEBUG
1065 printf("TL_INTR_Stat: ack %d\n", ack);
1066 #endif
1067 tl_read_stats(sc);
1068 break;
1069 case TL_INTR_Adc:
1070 if (int_reg & TL_INTVec_MASK) {
1071 /* adapter check conditions */
1072 printf("%s: check condition, intvect=0x%x, "
1073 "ch_param=0x%x\n", sc->sc_dev.dv_xname,
1074 int_reg & TL_INTVec_MASK,
1075 TL_HR_READ(sc, TL_HOST_CH_PARM));
1076 tl_reset(sc);
1077 /* shedule reinit of the board */
1078 timeout(tl_restart, sc, 1);
1079 return(1);
1080 } else {
1081 u_int8_t netstat;
1082 /* Network status */
1083 netstat =
1084 tl_intreg_read_byte(sc, TL_INT_NET+TL_INT_NetSts);
1085 printf("%s: network status, NetSts=%x\n",
1086 sc->sc_dev.dv_xname, netstat);
1087 /* Ack interrupts */
1088 tl_intreg_write_byte(sc, TL_INT_NET+TL_INT_NetSts,
1089 netstat);
1090 ack++;
1091 }
1092 break;
1093 default:
1094 printf("%s: unhandled interrupt code %x!\n",
1095 sc->sc_dev.dv_xname, int_type);
1096 ack++;
1097 }
1098
1099 if (ack) {
1100 /* Ack the interrupt and enable interrupts */
1101 TL_HR_WRITE(sc, TL_HOST_CMD, ack | int_type | HOST_CMD_ACK |
1102 HOST_CMD_IntOn);
1103 return 1;
1104 }
1105 /* ack = 0 ; interrupt was perhaps not our. Just enable interrupts */
1106 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOn);
1107 return 0;
1108 }
1109
1110 static int
1111 tl_ifioctl(ifp, cmd, data)
1112 struct ifnet *ifp;
1113 ioctl_cmd_t cmd;
1114 caddr_t data;
1115 {
1116 struct tl_softc *sc = ifp->if_softc;
1117 struct ifreq *ifr = (struct ifreq *)data;
1118 int s, error;
1119
1120 s = splnet();
1121 switch(cmd) {
1122 case SIOCSIFADDR: {
1123 struct ifaddr *ifa = (struct ifaddr *)data;
1124 sc->tl_if.if_flags |= IFF_UP;
1125 if ((error = tl_init(sc)) != NULL) {
1126 sc->tl_if.if_flags &= ~IFF_UP;
1127 break;
1128 }
1129 switch (ifa->ifa_addr->sa_family) {
1130 #ifdef INET
1131 case AF_INET:
1132 arp_ifinit(ifp, ifa);
1133 break;
1134 #endif
1135 #ifdef NS
1136 case AF_NS: {
1137 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1138
1139 if (ns_nullhost(*ina))
1140 ina->x_host =
1141 *(union ns_host*) LLADDR(ifp->if_sadl);
1142 else
1143 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1144 ifp->if_addrlen);
1145 break;
1146 }
1147 #endif
1148 default:
1149 break;
1150 }
1151 break;
1152 }
1153 case SIOCSIFFLAGS:
1154 {
1155 u_int8_t reg;
1156 /*
1157 * If interface is marked up and not running, then start it.
1158 * If it is marked down and running, stop it.
1159 */
1160 if (ifp->if_flags & IFF_UP) {
1161 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1162 error = tl_init(sc);
1163 /* all flags have been handled by init */
1164 break;
1165 }
1166 error = 0;
1167 reg = tl_intreg_read_byte(sc,
1168 TL_INT_NET + TL_INT_NetCmd);
1169 if (ifp->if_flags & IFF_PROMISC)
1170 reg |= TL_NETCOMMAND_CAF;
1171 else
1172 reg &= ~TL_NETCOMMAND_CAF;
1173 tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd,
1174 reg);
1175 #ifdef TL_PRIV_STATS
1176 if (ifp->if_flags & IFF_LINK0) {
1177 ifp->if_flags &= ~IFF_LINK0;
1178 printf("%s errors statistics\n",
1179 sc->sc_dev.dv_xname);
1180 printf(" %4d RX buffer overrun\n",
1181 sc->ierr_overr);
1182 printf(" %4d RX code error\n",
1183 sc->ierr_code);
1184 printf(" %4d RX crc error\n",
1185 sc->ierr_crc);
1186 printf(" %4d RX out of memory\n",
1187 sc->ierr_nomem);
1188 printf(" %4d TX buffer underrun\n",
1189 sc->oerr_underr);
1190 printf(" %4d TX deffered frames\n",
1191 sc->oerr_deffered);
1192 printf(" %4d TX single collisions\n",
1193 sc->oerr_coll);
1194 printf(" %4d TX multi collisions\n",
1195 sc->oerr_multicoll);
1196 printf(" %4d TX exessive collisions\n",
1197 sc->oerr_exesscoll);
1198 printf(" %4d TX late collisions\n",
1199 sc->oerr_latecoll);
1200 printf(" %4d TX carrier loss\n",
1201 sc->oerr_carrloss);
1202 printf(" %4d TX mbuf copy\n",
1203 sc->oerr_mcopy);
1204 }
1205 #endif
1206 } else {
1207 if (ifp->if_flags & IFF_RUNNING)
1208 tl_shutdown(sc);
1209 error = 0;
1210 }
1211 break;
1212 }
1213 case SIOCADDMULTI:
1214 case SIOCDELMULTI:
1215 /*
1216 * Update multicast listeners
1217 */
1218 if (cmd == SIOCADDMULTI)
1219 error = ether_addmulti(ifr, &sc->tl_ec);
1220 else
1221 error = ether_delmulti(ifr, &sc->tl_ec);
1222 if (error == ENETRESET) {
1223 tl_addr_filter(sc);
1224 error = 0;
1225 }
1226 break;
1227 case SIOCSIFMEDIA:
1228 case SIOCGIFMEDIA:
1229 error = ifmedia_ioctl(ifp, ifr, &sc->tl_mii.mii_media, cmd);
1230 break;
1231 default:
1232 error = EINVAL;
1233 }
1234 splx(s);
1235 return error;
1236 }
1237
1238 static void
1239 tl_ifstart(ifp)
1240 struct ifnet *ifp;
1241 {
1242 tl_softc_t *sc = ifp->if_softc;
1243 struct mbuf *m, *mb_head;
1244 struct Tx_list *Tx;
1245 int segment, size;
1246
1247 txloop:
1248 /* If we don't have more space ... */
1249 if (sc->Free_Tx == NULL) {
1250 #ifdef TLDEBUG
1251 printf("tl_ifstart: No free TX list\n");
1252 #endif
1253 return;
1254 }
1255 /* Grab a paquet for output */
1256 IF_DEQUEUE(&ifp->if_snd, mb_head);
1257 if (mb_head == NULL) {
1258 #ifdef TLDEBUG_TX
1259 printf("tl_ifstart: nothing to send\n");
1260 #endif
1261 return;
1262 }
1263 Tx = sc->Free_Tx;
1264 sc->Free_Tx = Tx->next;
1265 /*
1266 * Go through each of the mbufs in the chain and initialize
1267 * the transmit list descriptors with the physical address
1268 * and size of the mbuf.
1269 */
1270 tbdinit:
1271 bzero(Tx, sizeof(struct Tx_list));
1272 Tx->m = mb_head;
1273 size = 0;
1274 for (m = mb_head, segment = 0; m != NULL ; m = m->m_next) {
1275 if (m->m_len != 0) {
1276 if (segment == TL_NSEG)
1277 break;
1278 size += m->m_len;
1279 Tx->hw_list.seg[segment].data_addr =
1280 vtophys(mtod(m, vaddr_t));
1281 Tx->hw_list.seg[segment].data_count = m->m_len;
1282 segment++;
1283 }
1284 }
1285 if (m != NULL || (size < ETHER_MIN_TX && segment == TL_NSEG)) {
1286 /*
1287 * We ran out of segments, or we will. We have to recopy this
1288 * mbuf chain first.
1289 */
1290 struct mbuf *mn;
1291 #ifdef TLDEBUG_TX
1292 printf("tl_ifstart: need to copy mbuf\n");
1293 #endif
1294 #ifdef TL_PRIV_STATS
1295 sc->oerr_mcopy++;
1296 #endif
1297 MGETHDR(mn, M_DONTWAIT, MT_DATA);
1298 if (mn == NULL) {
1299 m_freem(mb_head);
1300 goto bad;
1301 }
1302 if (mb_head->m_pkthdr.len > MHLEN) {
1303 MCLGET(mn, M_DONTWAIT);
1304 if ((mn->m_flags & M_EXT) == 0) {
1305 m_freem(mn);
1306 m_freem(mb_head);
1307 goto bad;
1308 }
1309 }
1310 m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
1311 mtod(mn, caddr_t));
1312 mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
1313 m_freem(mb_head);
1314 mb_head = mn;
1315 goto tbdinit;
1316 }
1317 /* We are at end of mbuf chain. check the size and
1318 * see if it needs to be extended
1319 */
1320 if (size < ETHER_MIN_TX) {
1321 #ifdef DIAGNOSTIC
1322 if (segment >= TL_NSEG) {
1323 panic("tl_ifstart: to much segmets (%d)\n", segment);
1324 }
1325 #endif
1326 /*
1327 * add the nullbuf in the seg
1328 */
1329 Tx->hw_list.seg[segment].data_count =
1330 ETHER_MIN_TX - size;
1331 Tx->hw_list.seg[segment].data_addr =
1332 vtophys(nullbuf);
1333 size = ETHER_MIN_TX;
1334 segment++;
1335 }
1336 /* The list is done, finish the list init */
1337 Tx->hw_list.seg[segment-1].data_count |=
1338 TL_LAST_SEG;
1339 Tx->hw_list.stat = (size << 16) | 0x3000;
1340 #ifdef TLDEBUG_TX
1341 printf("%s: sending, Tx : stat = 0x%x\n", sc->sc_dev.dv_xname,
1342 Tx->hw_list.stat);
1343 #if 0
1344 for(segment = 0; segment < TL_NSEG; segment++) {
1345 printf(" seg %d addr 0x%x len 0x%x\n",
1346 segment,
1347 Tx->hw_list.seg[segment].data_addr,
1348 Tx->hw_list.seg[segment].data_count);
1349 }
1350 #endif
1351 #endif
1352 sc->opkt++;
1353 if (sc->active_Tx == NULL) {
1354 sc->active_Tx = sc->last_Tx = Tx;
1355 #ifdef TLDEBUG_TX
1356 printf("%s: Tx GO, addr=0x%x\n", sc->sc_dev.dv_xname,
1357 vtophys(&Tx->hw_list));
1358 #endif
1359 TL_HR_WRITE(sc, TL_HOST_CH_PARM, vtophys(&Tx->hw_list));
1360 TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
1361 } else {
1362 #ifdef TLDEBUG_TX
1363 printf("%s: Tx addr=0x%x queued\n", sc->sc_dev.dv_xname,
1364 vtophys(&Tx->hw_list));
1365 #endif
1366 sc->last_Tx->hw_list.fwd = vtophys(&Tx->hw_list);
1367 sc->last_Tx->next = Tx;
1368 sc->last_Tx = Tx;
1369 #ifdef DIAGNOSTIC
1370 if (sc->last_Tx->hw_list.fwd & 0x7)
1371 printf("%s: physical addr 0x%x of list not properly "
1372 "aligned\n",
1373 sc->sc_dev.dv_xname, sc->last_Rx->hw_list.fwd);
1374 #endif
1375 }
1376 #if NBPFILTER > 0
1377 /* Pass packet to bpf if there is a listener */
1378 if (ifp->if_bpf)
1379 bpf_mtap(ifp->if_bpf, mb_head);
1380 #endif
1381 /*
1382 * Set a 5 second timer just in case we don't hear from the card again.
1383 */
1384 ifp->if_timer = 5;
1385 goto txloop;
1386 bad:
1387 #ifdef TLDEBUG
1388 printf("tl_ifstart: Out of mbuf, Tx pkt lost\n");
1389 #endif
1390 Tx->next = sc->Free_Tx;
1391 sc->Free_Tx = Tx;
1392 return;
1393 }
1394
1395 static void
1396 tl_ifwatchdog(ifp)
1397 struct ifnet *ifp;
1398 {
1399 tl_softc_t *sc = ifp->if_softc;
1400
1401 if ((ifp->if_flags & IFF_RUNNING) == 0)
1402 return;
1403 printf("%s: device timeout\n", sc->sc_dev.dv_xname);
1404 ifp->if_oerrors++;
1405 tl_init(sc);
1406 }
1407
1408 static int
1409 tl_mediachange(ifp)
1410 struct ifnet *ifp;
1411 {
1412
1413 if (ifp->if_flags & IFF_UP)
1414 tl_init(ifp->if_softc);
1415 return (0);
1416 }
1417
1418 static void
1419 tl_mediastatus(ifp, ifmr)
1420 struct ifnet *ifp;
1421 struct ifmediareq *ifmr;
1422 {
1423 tl_softc_t *sc = ifp->if_softc;
1424
1425 mii_pollstat(&sc->tl_mii);
1426 ifmr->ifm_active = sc->tl_mii.mii_media_active;
1427 ifmr->ifm_status = sc->tl_mii.mii_media_status;
1428 }
1429
1430 static int tl_add_RxBuff(Rx, oldm)
1431 struct Rx_list *Rx;
1432 struct mbuf *oldm;
1433 {
1434 struct mbuf *m;
1435
1436 MGETHDR(m, M_DONTWAIT, MT_DATA);
1437 if (m != NULL) {
1438 MCLGET(m, M_DONTWAIT);
1439 if ((m->m_flags & M_EXT) == 0) {
1440 m_freem(m);
1441 if (oldm == NULL)
1442 return 0;
1443 m = oldm;
1444 m->m_data = m->m_ext.ext_buf;
1445 }
1446 } else {
1447 if (oldm == NULL)
1448 return 0;
1449 m = oldm;
1450 m->m_data = m->m_ext.ext_buf;
1451 }
1452 /*
1453 * Move the data pointer up so that the incoming data packet
1454 * will be 32-bit aligned.
1455 */
1456 m->m_data += 2;
1457
1458 /* (re)init the Rx_list struct */
1459
1460 Rx->m = m;
1461 Rx->hw_list.stat = ((MCLBYTES -2) << 16) | 0x3000;
1462 Rx->hw_list.seg.data_count = (MCLBYTES -2);
1463 Rx->hw_list.seg.data_addr = vtophys(m->m_data);
1464 return (m != oldm);
1465 }
1466
1467 static void tl_ticks(v)
1468 void *v;
1469 {
1470 tl_softc_t *sc = v;
1471
1472 tl_read_stats(sc);
1473
1474 /* Tick the MII. */
1475 mii_tick(&sc->tl_mii);
1476
1477 if (sc->opkt > 0) {
1478 if (sc->oerr_exesscoll > sc->opkt / 100) {
1479 /* exess collisions */
1480 if (sc->tl_flags & TL_IFACT) /* only print once */
1481 printf("%s: no carrier\n",
1482 sc->sc_dev.dv_xname);
1483 sc->tl_flags &= ~TL_IFACT;
1484 } else
1485 sc->tl_flags |= TL_IFACT;
1486 sc->oerr_exesscoll = sc->opkt = 0;
1487 sc->tl_lasttx = 0;
1488 } else {
1489 sc->tl_lasttx++;
1490 if (sc->tl_lasttx >= TL_IDLETIME) {
1491 /*
1492 * No TX activity in the last TL_IDLETIME seconds.
1493 * sends a LLC Class1 TEST pkt
1494 */
1495 struct mbuf *m;
1496 int s;
1497 MGETHDR(m, M_DONTWAIT, MT_DATA);
1498 if (m != NULL) {
1499 #ifdef TLDEBUG
1500 printf("tl_ticks: sending LLC test pkt\n");
1501 #endif
1502 bcopy(sc->tl_enaddr,
1503 mtod(m, struct ether_header *)->ether_dhost,
1504 6);
1505 bcopy(sc->tl_enaddr,
1506 mtod(m, struct ether_header *)->ether_shost,
1507 6);
1508 mtod(m, struct ether_header *)->ether_type =
1509 htons(3);
1510 mtod(m, unsigned char *)[14] = 0;
1511 mtod(m, unsigned char *)[15] = 0;
1512 mtod(m, unsigned char *)[16] = 0xE3;
1513 /* LLC Class1 TEST (no poll) */
1514 m->m_len = m->m_pkthdr.len =
1515 sizeof(struct ether_header) + 3;
1516 s = splnet();
1517 IF_PREPEND(&sc->tl_if.if_snd, m);
1518 tl_ifstart(&sc->tl_if);
1519 splx(s);
1520 }
1521 }
1522 }
1523
1524 /* read statistics every seconds */
1525 timeout(tl_ticks, v, hz);
1526 }
1527
1528 static void
1529 tl_read_stats(sc)
1530 tl_softc_t *sc;
1531 {
1532 u_int32_t reg;
1533 int ierr_overr;
1534 int ierr_code;
1535 int ierr_crc;
1536 int oerr_underr;
1537 int oerr_deffered;
1538 int oerr_coll;
1539 int oerr_multicoll;
1540 int oerr_exesscoll;
1541 int oerr_latecoll;
1542 int oerr_carrloss;
1543 struct ifnet *ifp = &sc->tl_if;
1544
1545 reg = tl_intreg_read(sc, TL_INT_STATS_TX);
1546 ifp->if_opackets += reg & 0x00ffffff;
1547 oerr_underr = reg >> 24;
1548
1549 reg = tl_intreg_read(sc, TL_INT_STATS_RX);
1550 ifp->if_ipackets += reg & 0x00ffffff;
1551 ierr_overr = reg >> 24;
1552
1553 reg = tl_intreg_read(sc, TL_INT_STATS_FERR);
1554 ierr_crc = (reg & TL_FERR_CRC) >> 16;
1555 ierr_code = (reg & TL_FERR_CODE) >> 24;
1556 oerr_deffered = (reg & TL_FERR_DEF);
1557
1558 reg = tl_intreg_read(sc, TL_INT_STATS_COLL);
1559 oerr_multicoll = (reg & TL_COL_MULTI);
1560 oerr_coll = (reg & TL_COL_SINGLE) >> 16;
1561
1562 reg = tl_intreg_read(sc, TL_INT_LERR);
1563 oerr_exesscoll = (reg & TL_LERR_ECOLL);
1564 oerr_latecoll = (reg & TL_LERR_LCOLL) >> 8;
1565 oerr_carrloss = (reg & TL_LERR_CL) >> 16;
1566
1567
1568 sc->stats_exesscoll += oerr_exesscoll;
1569 ifp->if_oerrors += oerr_underr + oerr_exesscoll + oerr_latecoll +
1570 oerr_carrloss;
1571 ifp->if_collisions += oerr_coll + oerr_multicoll;
1572 ifp->if_ierrors += ierr_overr + ierr_code + ierr_crc;
1573
1574 if (ierr_overr)
1575 printf("%s: receiver ring buffer overrun\n",
1576 sc->sc_dev.dv_xname);
1577 if (oerr_underr)
1578 printf("%s: transmit buffer underrun\n",
1579 sc->sc_dev.dv_xname);
1580 #ifdef TL_PRIV_STATS
1581 sc->ierr_overr += ierr_overr;
1582 sc->ierr_code += ierr_code;
1583 sc->ierr_crc += ierr_crc;
1584 sc->oerr_underr += oerr_underr;
1585 sc->oerr_deffered += oerr_deffered;
1586 sc->oerr_coll += oerr_coll;
1587 sc->oerr_multicoll += oerr_multicoll;
1588 sc->oerr_exesscoll += oerr_exesscoll;
1589 sc->oerr_latecoll += oerr_latecoll;
1590 sc->oerr_carrloss += oerr_carrloss;
1591 #endif
1592 }
1593
1594 static void tl_addr_filter(sc)
1595 tl_softc_t *sc;
1596 {
1597 struct ether_multistep step;
1598 struct ether_multi *enm;
1599 u_int32_t hash[2] = {0, 0};
1600 int i;
1601
1602 sc->tl_if.if_flags &= ~IFF_ALLMULTI;
1603 ETHER_FIRST_MULTI(step, &sc->tl_ec, enm);
1604 while (enm != NULL) {
1605 #ifdef TLDEBUG
1606 printf("tl_addr_filter: addrs %s %s\n",
1607 ether_sprintf(enm->enm_addrlo),
1608 ether_sprintf(enm->enm_addrhi));
1609 #endif
1610 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
1611 i = tl_multicast_hash(enm->enm_addrlo);
1612 hash[i/32] |= 1 << (i%32);
1613 } else {
1614 hash[0] = hash[1] = 0xffffffff;
1615 sc->tl_if.if_flags |= IFF_ALLMULTI;
1616 break;
1617 }
1618 ETHER_NEXT_MULTI(step, enm);
1619 }
1620 #ifdef TLDEBUG
1621 printf("tl_addr_filer: hash1 %x has2 %x\n", hash[0], hash[1]);
1622 #endif
1623 tl_intreg_write(sc, TL_INT_HASH1, hash[0]);
1624 tl_intreg_write(sc, TL_INT_HASH2, hash[1]);
1625 }
1626
1627 static int tl_multicast_hash(a)
1628 u_int8_t *a;
1629 {
1630 int hash;
1631
1632 #define DA(addr,bit) (addr[5 - (bit/8)] & (1 << bit%8))
1633 #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)
1634
1635 hash = xor8( DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30),
1636 DA(a,36), DA(a,42));
1637 hash |= xor8( DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31),
1638 DA(a,37), DA(a,43)) << 1;
1639 hash |= xor8( DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32),
1640 DA(a,38), DA(a,44)) << 2;
1641 hash |= xor8( DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33),
1642 DA(a,39), DA(a,45)) << 3;
1643 hash |= xor8( DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34),
1644 DA(a,40), DA(a,46)) << 4;
1645 hash |= xor8( DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35),
1646 DA(a,41), DA(a,47)) << 5;
1647
1648 return hash;
1649 }
1650
1651 #if defined(TLDEBUG_RX)
1652 void
1653 ether_printheader(eh)
1654 struct ether_header *eh;
1655 {
1656 u_char *c = (char*)eh;
1657 int i;
1658 for (i=0; i<sizeof(struct ether_header); i++)
1659 printf("%x ", (u_int)c[i]);
1660 printf("\n");
1661 }
1662 #endif
1663