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