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