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