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