Announce MAC address in ether_ifattach, not in each NIC indepently.
[dragonfly.git] / sys / dev / netif / txp / if_txp.c
1 /*      $OpenBSD: if_txp.c,v 1.48 2001/06/27 06:34:50 kjc Exp $ */
2 /*      $FreeBSD: src/sys/dev/txp/if_txp.c,v 1.4.2.4 2001/12/14 19:50:43 jlemon Exp $ */
3 /*      $DragonFly: src/sys/dev/netif/txp/if_txp.c,v 1.12 2004/07/02 17:42:20 joerg Exp $ */
4
5 /*
6  * Copyright (c) 2001
7  *      Jason L. Wright <jason@thought.net>, Theo de Raadt, and
8  *      Aaron Campbell <aaron@monkey.org>.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Jason L. Wright,
21  *      Theo de Raadt and Aaron Campbell.
22  * 4. Neither the name of the author nor the names of any co-contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36  * THE POSSIBILITY OF SUCH DAMAGE.
37  *
38  * $FreeBSD: src/sys/dev/txp/if_txp.c,v 1.4.2.4 2001/12/14 19:50:43 jlemon Exp $
39  */
40
41 /*
42  * Driver for 3c990 (Typhoon) Ethernet ASIC
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sockio.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/socket.h>
52
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/ethernet.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/vlan/if_vlan_var.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/if_ether.h>
65 #include <sys/in_cksum.h>
66
67 #include <net/if_media.h>
68
69 #include <net/bpf.h>
70
71 #include <vm/vm.h>              /* for vtophys */
72 #include <vm/pmap.h>            /* for vtophys */
73 #include <machine/clock.h>      /* for DELAY */
74 #include <machine/bus_pio.h>
75 #include <machine/bus_memio.h>
76 #include <machine/bus.h>
77 #include <machine/resource.h>
78 #include <sys/bus.h>
79 #include <sys/rman.h>
80
81 #include "../mii_layer/mii.h"
82 #include "../mii_layer/miivar.h"
83 #include <bus/pci/pcireg.h>
84 #include <bus/pci/pcivar.h>
85
86 #define TXP_USEIOSPACE
87 #define __STRICT_ALIGNMENT
88
89 #include "if_txpreg.h"
90 #include "3c990img.h"
91
92 /*
93  * Various supported device vendors/types and their names.
94  */
95 static struct txp_type txp_devs[] = {
96         { TXP_VENDORID_3COM, TXP_DEVICEID_3CR990_TX_95,
97             "3Com 3cR990-TX-95 Etherlink with 3XP Processor" },
98         { TXP_VENDORID_3COM, TXP_DEVICEID_3CR990_TX_97,
99             "3Com 3cR990-TX-97 Etherlink with 3XP Processor" },
100         { TXP_VENDORID_3COM, TXP_DEVICEID_3CR990B_TXM,
101             "3Com 3cR990B-TXM Etherlink with 3XP Processor" },
102         { TXP_VENDORID_3COM, TXP_DEVICEID_3CR990_SRV_95,
103             "3Com 3cR990-SRV-95 Etherlink Server with 3XP Processor" },
104         { TXP_VENDORID_3COM, TXP_DEVICEID_3CR990_SRV_97,
105             "3Com 3cR990-SRV-97 Etherlink Server with 3XP Processor" },
106         { TXP_VENDORID_3COM, TXP_DEVICEID_3CR990B_SRV,
107             "3Com 3cR990B-SRV Etherlink Server with 3XP Processor" },
108         { 0, 0, NULL }
109 };
110
111 static int txp_probe    (device_t);
112 static int txp_attach   (device_t);
113 static int txp_detach   (device_t);
114 static void txp_intr    (void *);
115 static void txp_tick    (void *);
116 static int txp_shutdown (device_t);
117 static int txp_ioctl    (struct ifnet *, u_long, caddr_t, struct ucred *);
118 static void txp_start   (struct ifnet *);
119 static void txp_stop    (struct txp_softc *);
120 static void txp_init    (void *);
121 static void txp_watchdog        (struct ifnet *);
122
123 static void txp_release_resources (struct txp_softc *);
124 static int txp_chip_init (struct txp_softc *);
125 static int txp_reset_adapter (struct txp_softc *);
126 static int txp_download_fw (struct txp_softc *);
127 static int txp_download_fw_wait (struct txp_softc *);
128 static int txp_download_fw_section (struct txp_softc *,
129     struct txp_fw_section_header *, int);
130 static int txp_alloc_rings (struct txp_softc *);
131 static int txp_rxring_fill (struct txp_softc *);
132 static void txp_rxring_empty (struct txp_softc *);
133 static void txp_set_filter (struct txp_softc *);
134
135 static int txp_cmd_desc_numfree (struct txp_softc *);
136 static int txp_command (struct txp_softc *, u_int16_t, u_int16_t, u_int32_t,
137     u_int32_t, u_int16_t *, u_int32_t *, u_int32_t *, int);
138 static int txp_command2 (struct txp_softc *, u_int16_t, u_int16_t,
139     u_int32_t, u_int32_t, struct txp_ext_desc *, u_int8_t,
140     struct txp_rsp_desc **, int);
141 static int txp_response (struct txp_softc *, u_int32_t, u_int16_t, u_int16_t,
142     struct txp_rsp_desc **);
143 static void txp_rsp_fixup (struct txp_softc *, struct txp_rsp_desc *,
144     struct txp_rsp_desc *);
145 static void txp_capabilities (struct txp_softc *);
146
147 static void txp_ifmedia_sts (struct ifnet *, struct ifmediareq *);
148 static int txp_ifmedia_upd (struct ifnet *);
149 #ifdef TXP_DEBUG
150 static void txp_show_descriptor (void *);
151 #endif
152 static void txp_tx_reclaim (struct txp_softc *, struct txp_tx_ring *);
153 static void txp_rxbuf_reclaim (struct txp_softc *);
154 static void txp_rx_reclaim (struct txp_softc *, struct txp_rx_ring *);
155
156 #ifdef TXP_USEIOSPACE
157 #define TXP_RES                 SYS_RES_IOPORT
158 #define TXP_RID                 TXP_PCI_LOIO
159 #else
160 #define TXP_RES                 SYS_RES_MEMORY
161 #define TXP_RID                 TXP_PCI_LOMEM
162 #endif
163
164 static device_method_t txp_methods[] = {
165         /* Device interface */
166         DEVMETHOD(device_probe,         txp_probe),
167         DEVMETHOD(device_attach,        txp_attach),
168         DEVMETHOD(device_detach,        txp_detach),
169         DEVMETHOD(device_shutdown,      txp_shutdown),
170         { 0, 0 }
171 };
172
173 static driver_t txp_driver = {
174         "txp",
175         txp_methods,
176         sizeof(struct txp_softc)
177 };
178
179 static devclass_t txp_devclass;
180
181 DECLARE_DUMMY_MODULE(if_txp);
182 DRIVER_MODULE(if_txp, pci, txp_driver, txp_devclass, 0, 0);
183
184 static int
185 txp_probe(dev)
186         device_t dev;
187 {
188         struct txp_type *t;
189
190         t = txp_devs;
191
192         while(t->txp_name != NULL) {
193                 if ((pci_get_vendor(dev) == t->txp_vid) &&
194                     (pci_get_device(dev) == t->txp_did)) {
195                         device_set_desc(dev, t->txp_name);
196                         return(0);
197                 }
198                 t++;
199         }
200
201         return(ENXIO);
202 }
203
204 static int
205 txp_attach(dev)
206         device_t dev;
207 {
208         struct txp_softc *sc;
209         struct ifnet *ifp;
210         u_int32_t command;
211         u_int16_t p1;
212         u_int32_t p2;
213         int unit, error = 0, rid;
214
215         sc = device_get_softc(dev);
216         unit = device_get_unit(dev);
217         sc->sc_dev = dev;
218         sc->sc_cold = 1;
219
220         /*
221          * Map control/status registers.
222          */
223         command = pci_read_config(dev, PCIR_COMMAND, 4);
224         command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
225         pci_write_config(dev, PCIR_COMMAND, command, 4);
226         command = pci_read_config(dev, PCIR_COMMAND, 4);
227
228 #ifdef TXP_USEIOSPACE
229         if (!(command & PCIM_CMD_PORTEN)) {
230                 device_printf(dev, "failed to enable I/O ports!\n");
231                 error = ENXIO;;
232                 goto fail;
233         }
234 #else
235         if (!(command & PCIM_CMD_MEMEN)) {
236                 device_printf(dev, "failed to enable memory mapping!\n");
237                 error = ENXIO;;
238                 goto fail;
239         }
240 #endif
241
242         rid = TXP_RID;
243         sc->sc_res = bus_alloc_resource(dev, TXP_RES, &rid,
244             0, ~0, 1, RF_ACTIVE);
245
246         if (sc->sc_res == NULL) {
247                 device_printf(dev, "couldn't map ports/memory\n");
248                 error = ENXIO;
249                 goto fail;
250         }
251
252         sc->sc_bt = rman_get_bustag(sc->sc_res);
253         sc->sc_bh = rman_get_bushandle(sc->sc_res);
254
255         /* Allocate interrupt */
256         rid = 0;
257         sc->sc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
258             RF_SHAREABLE | RF_ACTIVE);
259
260         if (sc->sc_irq == NULL) {
261                 device_printf(dev, "couldn't map interrupt\n");
262                 txp_release_resources(sc);
263                 error = ENXIO;
264                 goto fail;
265         }
266
267         error = bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_NET,
268             txp_intr, sc, &sc->sc_intrhand);
269
270         if (error) {
271                 txp_release_resources(sc);
272                 device_printf(dev, "couldn't set up irq\n");
273                 goto fail;
274         }
275
276         if (txp_chip_init(sc)) {
277                 txp_release_resources(sc);
278                 goto fail;
279         }
280
281         sc->sc_fwbuf = contigmalloc(32768, M_DEVBUF,
282             M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
283         error = txp_download_fw(sc);
284         contigfree(sc->sc_fwbuf, 32768, M_DEVBUF);
285         sc->sc_fwbuf = NULL;
286
287         if (error) {
288                 txp_release_resources(sc);
289                 goto fail;
290         }
291
292         sc->sc_ldata = contigmalloc(sizeof(struct txp_ldata), M_DEVBUF,
293             M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
294         bzero(sc->sc_ldata, sizeof(struct txp_ldata));
295
296         if (txp_alloc_rings(sc)) {
297                 txp_release_resources(sc);
298                 goto fail;
299         }
300
301         if (txp_command(sc, TXP_CMD_MAX_PKT_SIZE_WRITE, TXP_MAX_PKTLEN, 0, 0,
302             NULL, NULL, NULL, 1)) {
303                 txp_release_resources(sc);
304                 goto fail;
305         }
306
307         if (txp_command(sc, TXP_CMD_STATION_ADDRESS_READ, 0, 0, 0,
308             &p1, &p2, NULL, 1)) {
309                 txp_release_resources(sc);
310                 goto fail;
311         }
312
313         txp_set_filter(sc);
314
315         sc->sc_arpcom.ac_enaddr[0] = ((uint8_t *)&p1)[1];
316         sc->sc_arpcom.ac_enaddr[1] = ((uint8_t *)&p1)[0];
317         sc->sc_arpcom.ac_enaddr[2] = ((uint8_t *)&p2)[3];
318         sc->sc_arpcom.ac_enaddr[3] = ((uint8_t *)&p2)[2];
319         sc->sc_arpcom.ac_enaddr[4] = ((uint8_t *)&p2)[1];
320         sc->sc_arpcom.ac_enaddr[5] = ((uint8_t *)&p2)[0];
321
322         sc->sc_cold = 0;
323
324         ifmedia_init(&sc->sc_ifmedia, 0, txp_ifmedia_upd, txp_ifmedia_sts);
325         ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
326         ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
327         ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
328         ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
329         ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX|IFM_HDX, 0, NULL);
330         ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
331         ifmedia_add(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
332
333         sc->sc_xcvr = TXP_XCVR_AUTO;
334         txp_command(sc, TXP_CMD_XCVR_SELECT, TXP_XCVR_AUTO, 0, 0,
335             NULL, NULL, NULL, 0);
336         ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO);
337
338         ifp = &sc->sc_arpcom.ac_if;
339         ifp->if_softc = sc;
340         if_initname(ifp, "txp", unit);
341         ifp->if_mtu = ETHERMTU;
342         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
343         ifp->if_ioctl = txp_ioctl;
344         ifp->if_output = ether_output;
345         ifp->if_start = txp_start;
346         ifp->if_watchdog = txp_watchdog;
347         ifp->if_init = txp_init;
348         ifp->if_baudrate = 100000000;
349         ifp->if_snd.ifq_maxlen = TX_ENTRIES;
350         ifp->if_hwassist = 0;
351         txp_capabilities(sc);
352
353         /*
354          * Attach us everywhere
355          */
356         ether_ifattach(ifp, sc->sc_arpcom.ac_enaddr);
357         callout_handle_init(&sc->sc_tick);
358         return(0);
359
360 fail:
361         txp_release_resources(sc);
362         return(error);
363 }
364
365 static int
366 txp_detach(dev)
367         device_t dev;
368 {
369         struct txp_softc *sc;
370         struct ifnet *ifp;
371         int i;
372
373         sc = device_get_softc(dev);
374         ifp = &sc->sc_arpcom.ac_if;
375
376         txp_stop(sc);
377         txp_shutdown(dev);
378
379         ifmedia_removeall(&sc->sc_ifmedia);
380         ether_ifdetach(ifp);
381
382         for (i = 0; i < RXBUF_ENTRIES; i++)
383                 free(sc->sc_rxbufs[i].rb_sd, M_DEVBUF);
384
385         txp_release_resources(sc);
386
387         return(0);
388 }
389
390 static void
391 txp_release_resources(sc)
392         struct txp_softc *sc;
393 {
394         device_t dev;
395
396         dev = sc->sc_dev;
397
398         if (sc->sc_intrhand != NULL)
399                 bus_teardown_intr(dev, sc->sc_irq, sc->sc_intrhand);
400
401         if (sc->sc_irq != NULL)
402                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq);
403
404         if (sc->sc_res != NULL)
405                 bus_release_resource(dev, TXP_RES, TXP_RID, sc->sc_res);
406
407         if (sc->sc_ldata != NULL)
408                 contigfree(sc->sc_ldata, sizeof(struct txp_ldata), M_DEVBUF);
409
410         return;
411 }
412
413 static int
414 txp_chip_init(sc)
415         struct txp_softc *sc;
416 {
417         /* disable interrupts */
418         WRITE_REG(sc, TXP_IER, 0);
419         WRITE_REG(sc, TXP_IMR,
420             TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
421             TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
422             TXP_INT_LATCH);
423
424         /* ack all interrupts */
425         WRITE_REG(sc, TXP_ISR, TXP_INT_RESERVED | TXP_INT_LATCH |
426             TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
427             TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
428             TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
429             TXP_INT_A2H_3 | TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0);
430
431         if (txp_reset_adapter(sc))
432                 return (-1);
433
434         /* disable interrupts */
435         WRITE_REG(sc, TXP_IER, 0);
436         WRITE_REG(sc, TXP_IMR,
437             TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
438             TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
439             TXP_INT_LATCH);
440
441         /* ack all interrupts */
442         WRITE_REG(sc, TXP_ISR, TXP_INT_RESERVED | TXP_INT_LATCH |
443             TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
444             TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
445             TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
446             TXP_INT_A2H_3 | TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0);
447
448         return (0);
449 }
450
451 static int
452 txp_reset_adapter(sc)
453         struct txp_softc *sc;
454 {
455         u_int32_t r;
456         int i;
457
458         WRITE_REG(sc, TXP_SRR, TXP_SRR_ALL);
459         DELAY(1000);
460         WRITE_REG(sc, TXP_SRR, 0);
461
462         /* Should wait max 6 seconds */
463         for (i = 0; i < 6000; i++) {
464                 r = READ_REG(sc, TXP_A2H_0);
465                 if (r == STAT_WAITING_FOR_HOST_REQUEST)
466                         break;
467                 DELAY(1000);
468         }
469
470         if (r != STAT_WAITING_FOR_HOST_REQUEST) {
471                 device_printf(sc->sc_dev, "reset hung\n");
472                 return (-1);
473         }
474
475         return (0);
476 }
477
478 static int
479 txp_download_fw(sc)
480         struct txp_softc *sc;
481 {
482         struct txp_fw_file_header *fileheader;
483         struct txp_fw_section_header *secthead;
484         int sect;
485         u_int32_t r, i, ier, imr;
486
487         ier = READ_REG(sc, TXP_IER);
488         WRITE_REG(sc, TXP_IER, ier | TXP_INT_A2H_0);
489
490         imr = READ_REG(sc, TXP_IMR);
491         WRITE_REG(sc, TXP_IMR, imr | TXP_INT_A2H_0);
492
493         for (i = 0; i < 10000; i++) {
494                 r = READ_REG(sc, TXP_A2H_0);
495                 if (r == STAT_WAITING_FOR_HOST_REQUEST)
496                         break;
497                 DELAY(50);
498         }
499         if (r != STAT_WAITING_FOR_HOST_REQUEST) {
500                 device_printf(sc->sc_dev, "not waiting for host request\n");
501                 return (-1);
502         }
503
504         /* Ack the status */
505         WRITE_REG(sc, TXP_ISR, TXP_INT_A2H_0);
506
507         fileheader = (struct txp_fw_file_header *)tc990image;
508         if (bcmp("TYPHOON", fileheader->magicid, sizeof(fileheader->magicid))) {
509                 device_printf(sc->sc_dev, "fw invalid magic\n");
510                 return (-1);
511         }
512
513         /* Tell boot firmware to get ready for image */
514         WRITE_REG(sc, TXP_H2A_1, fileheader->addr);
515         WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_RUNTIME_IMAGE);
516
517         if (txp_download_fw_wait(sc)) {
518                 device_printf(sc->sc_dev, "fw wait failed, initial\n");
519                 return (-1);
520         }
521
522         secthead = (struct txp_fw_section_header *)(((u_int8_t *)tc990image) +
523             sizeof(struct txp_fw_file_header));
524
525         for (sect = 0; sect < fileheader->nsections; sect++) {
526                 if (txp_download_fw_section(sc, secthead, sect))
527                         return (-1);
528                 secthead = (struct txp_fw_section_header *)
529                     (((u_int8_t *)secthead) + secthead->nbytes +
530                     sizeof(*secthead));
531         }
532
533         WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_DOWNLOAD_COMPLETE);
534
535         for (i = 0; i < 10000; i++) {
536                 r = READ_REG(sc, TXP_A2H_0);
537                 if (r == STAT_WAITING_FOR_BOOT)
538                         break;
539                 DELAY(50);
540         }
541         if (r != STAT_WAITING_FOR_BOOT) {
542                 device_printf(sc->sc_dev, "not waiting for boot\n");
543                 return (-1);
544         }
545
546         WRITE_REG(sc, TXP_IER, ier);
547         WRITE_REG(sc, TXP_IMR, imr);
548
549         return (0);
550 }
551
552 static int
553 txp_download_fw_wait(sc)
554         struct txp_softc *sc;
555 {
556         u_int32_t i, r;
557
558         for (i = 0; i < 10000; i++) {
559                 r = READ_REG(sc, TXP_ISR);
560                 if (r & TXP_INT_A2H_0)
561                         break;
562                 DELAY(50);
563         }
564
565         if (!(r & TXP_INT_A2H_0)) {
566                 device_printf(sc->sc_dev, "fw wait failed comm0\n");
567                 return (-1);
568         }
569
570         WRITE_REG(sc, TXP_ISR, TXP_INT_A2H_0);
571
572         r = READ_REG(sc, TXP_A2H_0);
573         if (r != STAT_WAITING_FOR_SEGMENT) {
574                 device_printf(sc->sc_dev, "fw not waiting for segment\n");
575                 return (-1);
576         }
577         return (0);
578 }
579
580 static int
581 txp_download_fw_section(sc, sect, sectnum)
582         struct txp_softc *sc;
583         struct txp_fw_section_header *sect;
584         int sectnum;
585 {
586         vm_offset_t dma;
587         int rseg, err = 0;
588         struct mbuf m;
589         u_int16_t csum;
590
591         /* Skip zero length sections */
592         if (sect->nbytes == 0)
593                 return (0);
594
595         /* Make sure we aren't past the end of the image */
596         rseg = ((u_int8_t *)sect) - ((u_int8_t *)tc990image);
597         if (rseg >= sizeof(tc990image)) {
598                 device_printf(sc->sc_dev, "fw invalid section address, "
599                     "section %d\n", sectnum);
600                 return (-1);
601         }
602
603         /* Make sure this section doesn't go past the end */
604         rseg += sect->nbytes;
605         if (rseg >= sizeof(tc990image)) {
606                 device_printf(sc->sc_dev, "fw truncated section %d\n",
607                     sectnum);
608                 return (-1);
609         }
610
611         bcopy(((u_int8_t *)sect) + sizeof(*sect), sc->sc_fwbuf, sect->nbytes);
612         dma = vtophys(sc->sc_fwbuf);
613
614         /*
615          * dummy up mbuf and verify section checksum
616          */
617         m.m_type = MT_DATA;
618         m.m_next = m.m_nextpkt = NULL;
619         m.m_len = sect->nbytes;
620         m.m_data = sc->sc_fwbuf;
621         m.m_flags = 0;
622         csum = in_cksum(&m, sect->nbytes);
623         if (csum != sect->cksum) {
624                 device_printf(sc->sc_dev, "fw section %d, bad "
625                     "cksum (expected 0x%x got 0x%x)\n",
626                     sectnum, sect->cksum, csum);
627                 err = -1;
628                 goto bail;
629         }
630
631         WRITE_REG(sc, TXP_H2A_1, sect->nbytes);
632         WRITE_REG(sc, TXP_H2A_2, sect->cksum);
633         WRITE_REG(sc, TXP_H2A_3, sect->addr);
634         WRITE_REG(sc, TXP_H2A_4, 0);
635         WRITE_REG(sc, TXP_H2A_5, dma & 0xffffffff);
636         WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_SEGMENT_AVAILABLE);
637
638         if (txp_download_fw_wait(sc)) {
639                 device_printf(sc->sc_dev, "fw wait failed, "
640                     "section %d\n", sectnum);
641                 err = -1;
642         }
643
644 bail:
645         return (err);
646 }
647
648 static void 
649 txp_intr(vsc)
650         void *vsc;
651 {
652         struct txp_softc *sc = vsc;
653         struct txp_hostvar *hv = sc->sc_hostvar;
654         u_int32_t isr;
655
656         /* mask all interrupts */
657         WRITE_REG(sc, TXP_IMR, TXP_INT_RESERVED | TXP_INT_SELF |
658             TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
659             TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0 |
660             TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
661             TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |  TXP_INT_LATCH);
662
663         isr = READ_REG(sc, TXP_ISR);
664         while (isr) {
665                 WRITE_REG(sc, TXP_ISR, isr);
666
667                 if ((*sc->sc_rxhir.r_roff) != (*sc->sc_rxhir.r_woff))
668                         txp_rx_reclaim(sc, &sc->sc_rxhir);
669                 if ((*sc->sc_rxlor.r_roff) != (*sc->sc_rxlor.r_woff))
670                         txp_rx_reclaim(sc, &sc->sc_rxlor);
671
672                 if (hv->hv_rx_buf_write_idx == hv->hv_rx_buf_read_idx)
673                         txp_rxbuf_reclaim(sc);
674
675                 if (sc->sc_txhir.r_cnt && (sc->sc_txhir.r_cons !=
676                     TXP_OFFSET2IDX(*(sc->sc_txhir.r_off))))
677                         txp_tx_reclaim(sc, &sc->sc_txhir);
678
679                 if (sc->sc_txlor.r_cnt && (sc->sc_txlor.r_cons !=
680                     TXP_OFFSET2IDX(*(sc->sc_txlor.r_off))))
681                         txp_tx_reclaim(sc, &sc->sc_txlor);
682
683                 isr = READ_REG(sc, TXP_ISR);
684         }
685
686         /* unmask all interrupts */
687         WRITE_REG(sc, TXP_IMR, TXP_INT_A2H_3);
688
689         txp_start(&sc->sc_arpcom.ac_if);
690
691         return;
692 }
693
694 static void
695 txp_rx_reclaim(sc, r)
696         struct txp_softc *sc;
697         struct txp_rx_ring *r;
698 {
699         struct ifnet *ifp = &sc->sc_arpcom.ac_if;
700         struct txp_rx_desc *rxd;
701         struct mbuf *m;
702         struct txp_swdesc *sd = NULL;
703         u_int32_t roff, woff;
704         struct ether_header *eh = NULL;
705
706         roff = *r->r_roff;
707         woff = *r->r_woff;
708         rxd = r->r_desc + (roff / sizeof(struct txp_rx_desc));
709
710         while (roff != woff) {
711
712                 if (rxd->rx_flags & RX_FLAGS_ERROR) {
713                         device_printf(sc->sc_dev, "error 0x%x\n",
714                             rxd->rx_stat);
715                         ifp->if_ierrors++;
716                         goto next;
717                 }
718
719                 /* retrieve stashed pointer */
720                 sd = rxd->rx_sd;
721
722                 m = sd->sd_mbuf;
723                 sd->sd_mbuf = NULL;
724
725                 m->m_pkthdr.len = m->m_len = rxd->rx_len;
726
727 #ifdef __STRICT_ALIGNMENT
728                 {
729                         /*
730                          * XXX Nice chip, except it won't accept "off by 2"
731                          * buffers, so we're force to copy.  Supposedly
732                          * this will be fixed in a newer firmware rev
733                          * and this will be temporary.
734                          */
735                         struct mbuf *mnew;
736
737                         MGETHDR(mnew, MB_DONTWAIT, MT_DATA);
738                         if (mnew == NULL) {
739                                 m_freem(m);
740                                 goto next;
741                         }
742                         if (m->m_len > (MHLEN - 2)) {
743                                 MCLGET(mnew, MB_DONTWAIT);
744                                 if (!(mnew->m_flags & M_EXT)) {
745                                         m_freem(mnew);
746                                         m_freem(m);
747                                         goto next;
748                                 }
749                         }
750                         mnew->m_pkthdr.rcvif = ifp;
751                         m_adj(mnew, 2);
752                         mnew->m_pkthdr.len = mnew->m_len = m->m_len;
753                         m_copydata(m, 0, m->m_pkthdr.len, mtod(mnew, caddr_t));
754                         m_freem(m);
755                         m = mnew;
756                 }
757 #endif
758
759                 if (rxd->rx_stat & RX_STAT_IPCKSUMBAD)
760                         m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
761                 else if (rxd->rx_stat & RX_STAT_IPCKSUMGOOD)
762                         m->m_pkthdr.csum_flags |=
763                             CSUM_IP_CHECKED|CSUM_IP_VALID;
764
765                 if ((rxd->rx_stat & RX_STAT_TCPCKSUMGOOD) ||
766                     (rxd->rx_stat & RX_STAT_UDPCKSUMGOOD)) {
767                         m->m_pkthdr.csum_flags |=
768                             CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
769                         m->m_pkthdr.csum_data = 0xffff;
770                 }
771
772                 eh = mtod(m, struct ether_header *);
773                 /* Remove header from mbuf and pass it on. */
774                 m_adj(m, sizeof(struct ether_header));
775
776                 if (rxd->rx_stat & RX_STAT_VLAN) {
777                         VLAN_INPUT_TAG(eh, m, htons(rxd->rx_vlan >> 16));
778                         goto next;
779                 }
780
781                 ether_input(ifp, eh, m);
782
783 next:
784
785                 roff += sizeof(struct txp_rx_desc);
786                 if (roff == (RX_ENTRIES * sizeof(struct txp_rx_desc))) {
787                         roff = 0;
788                         rxd = r->r_desc;
789                 } else
790                         rxd++;
791                 woff = *r->r_woff;
792         }
793
794         *r->r_roff = woff;
795
796         return;
797 }
798
799 static void
800 txp_rxbuf_reclaim(sc)
801         struct txp_softc *sc;
802 {
803         struct ifnet *ifp = &sc->sc_arpcom.ac_if;
804         struct txp_hostvar *hv = sc->sc_hostvar;
805         struct txp_rxbuf_desc *rbd;
806         struct txp_swdesc *sd;
807         u_int32_t i;
808
809         if (!(ifp->if_flags & IFF_RUNNING))
810                 return;
811
812         i = sc->sc_rxbufprod;
813         rbd = sc->sc_rxbufs + i;
814
815         while (1) {
816                 sd = rbd->rb_sd;
817                 if (sd->sd_mbuf != NULL)
818                         break;
819
820                 MGETHDR(sd->sd_mbuf, MB_DONTWAIT, MT_DATA);
821                 if (sd->sd_mbuf == NULL)
822                         goto err_sd;
823
824                 MCLGET(sd->sd_mbuf, MB_DONTWAIT);
825                 if ((sd->sd_mbuf->m_flags & M_EXT) == 0)
826                         goto err_mbuf;
827                 sd->sd_mbuf->m_pkthdr.rcvif = ifp;
828                 sd->sd_mbuf->m_pkthdr.len = sd->sd_mbuf->m_len = MCLBYTES;
829
830                 rbd->rb_paddrlo = vtophys(mtod(sd->sd_mbuf, vm_offset_t))
831                     & 0xffffffff;
832                 rbd->rb_paddrhi = 0;
833
834                 hv->hv_rx_buf_write_idx = TXP_IDX2OFFSET(i);
835
836                 if (++i == RXBUF_ENTRIES) {
837                         i = 0;
838                         rbd = sc->sc_rxbufs;
839                 } else
840                         rbd++;
841         }
842
843         sc->sc_rxbufprod = i;
844
845         return;
846
847 err_mbuf:
848         m_freem(sd->sd_mbuf);
849 err_sd:
850         free(sd, M_DEVBUF);
851 }
852
853 /*
854  * Reclaim mbufs and entries from a transmit ring.
855  */
856 static void
857 txp_tx_reclaim(sc, r)
858         struct txp_softc *sc;
859         struct txp_tx_ring *r;
860 {
861         struct ifnet *ifp = &sc->sc_arpcom.ac_if;
862         u_int32_t idx = TXP_OFFSET2IDX(*(r->r_off));
863         u_int32_t cons = r->r_cons, cnt = r->r_cnt;
864         struct txp_tx_desc *txd = r->r_desc + cons;
865         struct txp_swdesc *sd = sc->sc_txd + cons;
866         struct mbuf *m;
867
868         while (cons != idx) {
869                 if (cnt == 0)
870                         break;
871
872                 if ((txd->tx_flags & TX_FLAGS_TYPE_M) ==
873                     TX_FLAGS_TYPE_DATA) {
874                         m = sd->sd_mbuf;
875                         if (m != NULL) {
876                                 m_freem(m);
877                                 txd->tx_addrlo = 0;
878                                 txd->tx_addrhi = 0;
879                                 ifp->if_opackets++;
880                         }
881                 }
882                 ifp->if_flags &= ~IFF_OACTIVE;
883
884                 if (++cons == TX_ENTRIES) {
885                         txd = r->r_desc;
886                         cons = 0;
887                         sd = sc->sc_txd;
888                 } else {
889                         txd++;
890                         sd++;
891                 }
892
893                 cnt--;
894         }
895
896         r->r_cons = cons;
897         r->r_cnt = cnt;
898         if (cnt == 0)
899                 ifp->if_timer = 0;
900 }
901
902 static int
903 txp_shutdown(dev)
904         device_t dev;
905 {
906         struct txp_softc *sc;
907
908         sc = device_get_softc(dev);
909
910         /* mask all interrupts */
911         WRITE_REG(sc, TXP_IMR,
912             TXP_INT_SELF | TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |
913             TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
914             TXP_INT_LATCH);
915
916         txp_command(sc, TXP_CMD_TX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 0);
917         txp_command(sc, TXP_CMD_RX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 0);
918         txp_command(sc, TXP_CMD_HALT, 0, 0, 0, NULL, NULL, NULL, 0);
919
920         return(0);
921 }
922
923 static int
924 txp_alloc_rings(sc)
925         struct txp_softc *sc;
926 {
927         struct txp_boot_record *boot;
928         struct txp_ldata *ld;
929         u_int32_t r;
930         int i;
931
932         ld = sc->sc_ldata;
933         boot = &ld->txp_boot;
934
935         /* boot record */
936         sc->sc_boot = boot;
937
938         /* host variables */
939         bzero(&ld->txp_hostvar, sizeof(struct txp_hostvar));
940         boot->br_hostvar_lo = vtophys(&ld->txp_hostvar);
941         boot->br_hostvar_hi = 0;
942         sc->sc_hostvar = (struct txp_hostvar *)&ld->txp_hostvar;
943
944         /* hi priority tx ring */
945         boot->br_txhipri_lo = vtophys(&ld->txp_txhiring);;
946         boot->br_txhipri_hi = 0;
947         boot->br_txhipri_siz = TX_ENTRIES * sizeof(struct txp_tx_desc);
948         sc->sc_txhir.r_reg = TXP_H2A_1;
949         sc->sc_txhir.r_desc = (struct txp_tx_desc *)&ld->txp_txhiring;
950         sc->sc_txhir.r_cons = sc->sc_txhir.r_prod = sc->sc_txhir.r_cnt = 0;
951         sc->sc_txhir.r_off = &sc->sc_hostvar->hv_tx_hi_desc_read_idx;
952
953         /* lo priority tx ring */
954         boot->br_txlopri_lo = vtophys(&ld->txp_txloring);
955         boot->br_txlopri_hi = 0;
956         boot->br_txlopri_siz = TX_ENTRIES * sizeof(struct txp_tx_desc);
957         sc->sc_txlor.r_reg = TXP_H2A_3;
958         sc->sc_txlor.r_desc = (struct txp_tx_desc *)&ld->txp_txloring;
959         sc->sc_txlor.r_cons = sc->sc_txlor.r_prod = sc->sc_txlor.r_cnt = 0;
960         sc->sc_txlor.r_off = &sc->sc_hostvar->hv_tx_lo_desc_read_idx;
961
962         /* high priority rx ring */
963         boot->br_rxhipri_lo = vtophys(&ld->txp_rxhiring);
964         boot->br_rxhipri_hi = 0;
965         boot->br_rxhipri_siz = RX_ENTRIES * sizeof(struct txp_rx_desc);
966         sc->sc_rxhir.r_desc = (struct txp_rx_desc *)&ld->txp_rxhiring;
967         sc->sc_rxhir.r_roff = &sc->sc_hostvar->hv_rx_hi_read_idx;
968         sc->sc_rxhir.r_woff = &sc->sc_hostvar->hv_rx_hi_write_idx;
969
970         /* low priority rx ring */
971         boot->br_rxlopri_lo = vtophys(&ld->txp_rxloring);
972         boot->br_rxlopri_hi = 0;
973         boot->br_rxlopri_siz = RX_ENTRIES * sizeof(struct txp_rx_desc);
974         sc->sc_rxlor.r_desc = (struct txp_rx_desc *)&ld->txp_rxloring;
975         sc->sc_rxlor.r_roff = &sc->sc_hostvar->hv_rx_lo_read_idx;
976         sc->sc_rxlor.r_woff = &sc->sc_hostvar->hv_rx_lo_write_idx;
977
978         /* command ring */
979         bzero(&ld->txp_cmdring, sizeof(struct txp_cmd_desc) * CMD_ENTRIES);
980         boot->br_cmd_lo = vtophys(&ld->txp_cmdring);
981         boot->br_cmd_hi = 0;
982         boot->br_cmd_siz = CMD_ENTRIES * sizeof(struct txp_cmd_desc);
983         sc->sc_cmdring.base = (struct txp_cmd_desc *)&ld->txp_cmdring;
984         sc->sc_cmdring.size = CMD_ENTRIES * sizeof(struct txp_cmd_desc);
985         sc->sc_cmdring.lastwrite = 0;
986
987         /* response ring */
988         bzero(&ld->txp_rspring, sizeof(struct txp_rsp_desc) * RSP_ENTRIES);
989         boot->br_resp_lo = vtophys(&ld->txp_rspring);
990         boot->br_resp_hi = 0;
991         boot->br_resp_siz = CMD_ENTRIES * sizeof(struct txp_rsp_desc);
992         sc->sc_rspring.base = (struct txp_rsp_desc *)&ld->txp_rspring;
993         sc->sc_rspring.size = RSP_ENTRIES * sizeof(struct txp_rsp_desc);
994         sc->sc_rspring.lastwrite = 0;
995
996         /* receive buffer ring */
997         boot->br_rxbuf_lo = vtophys(&ld->txp_rxbufs);
998         boot->br_rxbuf_hi = 0;
999         boot->br_rxbuf_siz = RXBUF_ENTRIES * sizeof(struct txp_rxbuf_desc);
1000         sc->sc_rxbufs = (struct txp_rxbuf_desc *)&ld->txp_rxbufs;
1001
1002         for (i = 0; i < RXBUF_ENTRIES; i++) {
1003                 struct txp_swdesc *sd;
1004                 if (sc->sc_rxbufs[i].rb_sd != NULL)
1005                         continue;
1006                 sc->sc_rxbufs[i].rb_sd = malloc(sizeof(struct txp_swdesc),
1007                     M_DEVBUF, M_WAITOK);
1008                 if (sc->sc_rxbufs[i].rb_sd == NULL)
1009                         return(ENOBUFS);
1010                 sd = sc->sc_rxbufs[i].rb_sd;
1011                 sd->sd_mbuf = NULL;
1012         }
1013         sc->sc_rxbufprod = 0;
1014
1015         /* zero dma */
1016         bzero(&ld->txp_zero, sizeof(u_int32_t));
1017         boot->br_zero_lo = vtophys(&ld->txp_zero);
1018         boot->br_zero_hi = 0;
1019
1020         /* See if it's waiting for boot, and try to boot it */
1021         for (i = 0; i < 10000; i++) {
1022                 r = READ_REG(sc, TXP_A2H_0);
1023                 if (r == STAT_WAITING_FOR_BOOT)
1024                         break;
1025                 DELAY(50);
1026         }
1027
1028         if (r != STAT_WAITING_FOR_BOOT) {
1029                 device_printf(sc->sc_dev, "not waiting for boot\n");
1030                 return(ENXIO);
1031         }
1032
1033         WRITE_REG(sc, TXP_H2A_2, 0);
1034         WRITE_REG(sc, TXP_H2A_1, vtophys(sc->sc_boot));
1035         WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_REGISTER_BOOT_RECORD);
1036
1037         /* See if it booted */
1038         for (i = 0; i < 10000; i++) {
1039                 r = READ_REG(sc, TXP_A2H_0);
1040                 if (r == STAT_RUNNING)
1041                         break;
1042                 DELAY(50);
1043         }
1044         if (r != STAT_RUNNING) {
1045                 device_printf(sc->sc_dev, "fw not running\n");
1046                 return(ENXIO);
1047         }
1048
1049         /* Clear TX and CMD ring write registers */
1050         WRITE_REG(sc, TXP_H2A_1, TXP_BOOTCMD_NULL);
1051         WRITE_REG(sc, TXP_H2A_2, TXP_BOOTCMD_NULL);
1052         WRITE_REG(sc, TXP_H2A_3, TXP_BOOTCMD_NULL);
1053         WRITE_REG(sc, TXP_H2A_0, TXP_BOOTCMD_NULL);
1054
1055         return (0);
1056 }
1057
1058 static int
1059 txp_ioctl(ifp, command, data, cr)
1060         struct ifnet *ifp;
1061         u_long command;
1062         caddr_t data;
1063         struct ucred *cr;
1064 {
1065         struct txp_softc *sc = ifp->if_softc;
1066         struct ifreq *ifr = (struct ifreq *)data;
1067         int s, error = 0;
1068
1069         s = splnet();
1070
1071         if ((error = ether_ioctl(ifp, command, data)) > 0) {
1072                 splx(s);
1073                 return error;
1074         }
1075
1076         switch(command) {
1077         case SIOCSIFADDR:
1078         case SIOCGIFADDR:
1079         case SIOCSIFMTU:
1080                 error = ether_ioctl(ifp, command, data);
1081                 break;
1082         case SIOCSIFFLAGS:
1083                 if (ifp->if_flags & IFF_UP) {
1084                         txp_init(sc);
1085                 } else {
1086                         if (ifp->if_flags & IFF_RUNNING)
1087                                 txp_stop(sc);
1088                 }
1089                 break;
1090         case SIOCADDMULTI:
1091         case SIOCDELMULTI:
1092                 /*
1093                  * Multicast list has changed; set the hardware
1094                  * filter accordingly.
1095                  */
1096                 txp_set_filter(sc);
1097                 error = 0;
1098                 break;
1099         case SIOCGIFMEDIA:
1100         case SIOCSIFMEDIA:
1101                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, command);
1102                 break;
1103         default:
1104                 error = EINVAL;
1105                 break;
1106         }
1107
1108         (void)splx(s);
1109
1110         return(error);
1111 }
1112
1113 static int
1114 txp_rxring_fill(sc)
1115         struct txp_softc *sc;
1116 {
1117         int i;
1118         struct ifnet *ifp;
1119         struct txp_swdesc *sd;
1120
1121         ifp = &sc->sc_arpcom.ac_if;
1122
1123         for (i = 0; i < RXBUF_ENTRIES; i++) {
1124                 sd = sc->sc_rxbufs[i].rb_sd;
1125                 MGETHDR(sd->sd_mbuf, MB_DONTWAIT, MT_DATA);
1126                 if (sd->sd_mbuf == NULL)
1127                         return(ENOBUFS);
1128
1129                 MCLGET(sd->sd_mbuf, MB_DONTWAIT);
1130                 if ((sd->sd_mbuf->m_flags & M_EXT) == 0) {
1131                         m_freem(sd->sd_mbuf);
1132                         return(ENOBUFS);
1133                 }
1134                 sd->sd_mbuf->m_pkthdr.len = sd->sd_mbuf->m_len = MCLBYTES;
1135                 sd->sd_mbuf->m_pkthdr.rcvif = ifp;
1136
1137                 sc->sc_rxbufs[i].rb_paddrlo =
1138                     vtophys(mtod(sd->sd_mbuf, vm_offset_t));
1139                 sc->sc_rxbufs[i].rb_paddrhi = 0;
1140         }
1141
1142         sc->sc_hostvar->hv_rx_buf_write_idx = (RXBUF_ENTRIES - 1) *
1143             sizeof(struct txp_rxbuf_desc);
1144
1145         return(0);
1146 }
1147
1148 static void
1149 txp_rxring_empty(sc)
1150         struct txp_softc *sc;
1151 {
1152         int i;
1153         struct txp_swdesc *sd;
1154
1155         if (sc->sc_rxbufs == NULL)
1156                 return;
1157
1158         for (i = 0; i < RXBUF_ENTRIES; i++) {
1159                 if (&sc->sc_rxbufs[i] == NULL)
1160                         continue;
1161                 sd = sc->sc_rxbufs[i].rb_sd;
1162                 if (sd == NULL)
1163                         continue;
1164                 if (sd->sd_mbuf != NULL) {
1165                         m_freem(sd->sd_mbuf);
1166                         sd->sd_mbuf = NULL;
1167                 }
1168         }
1169
1170         return;
1171 }
1172
1173 static void
1174 txp_init(xsc)
1175         void *xsc;
1176 {
1177         struct txp_softc *sc;
1178         struct ifnet *ifp;
1179         u_int16_t p1;
1180         u_int32_t p2;
1181         int s;
1182
1183         sc = xsc;
1184         ifp = &sc->sc_arpcom.ac_if;
1185
1186         if (ifp->if_flags & IFF_RUNNING)
1187                 return;
1188
1189         txp_stop(sc);
1190
1191         s = splnet();
1192
1193         txp_command(sc, TXP_CMD_MAX_PKT_SIZE_WRITE, TXP_MAX_PKTLEN, 0, 0,
1194             NULL, NULL, NULL, 1);
1195
1196         /* Set station address. */
1197         ((u_int8_t *)&p1)[1] = sc->sc_arpcom.ac_enaddr[0];
1198         ((u_int8_t *)&p1)[0] = sc->sc_arpcom.ac_enaddr[1];
1199         ((u_int8_t *)&p2)[3] = sc->sc_arpcom.ac_enaddr[2];
1200         ((u_int8_t *)&p2)[2] = sc->sc_arpcom.ac_enaddr[3];
1201         ((u_int8_t *)&p2)[1] = sc->sc_arpcom.ac_enaddr[4];
1202         ((u_int8_t *)&p2)[0] = sc->sc_arpcom.ac_enaddr[5];
1203         txp_command(sc, TXP_CMD_STATION_ADDRESS_WRITE, p1, p2, 0,
1204             NULL, NULL, NULL, 1);
1205
1206         txp_set_filter(sc);
1207
1208         txp_rxring_fill(sc);
1209
1210         txp_command(sc, TXP_CMD_TX_ENABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1211         txp_command(sc, TXP_CMD_RX_ENABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1212
1213         WRITE_REG(sc, TXP_IER, TXP_INT_RESERVED | TXP_INT_SELF |
1214             TXP_INT_A2H_7 | TXP_INT_A2H_6 | TXP_INT_A2H_5 | TXP_INT_A2H_4 |
1215             TXP_INT_A2H_2 | TXP_INT_A2H_1 | TXP_INT_A2H_0 |
1216             TXP_INT_DMA3 | TXP_INT_DMA2 | TXP_INT_DMA1 | TXP_INT_DMA0 |
1217             TXP_INT_PCI_TABORT | TXP_INT_PCI_MABORT |  TXP_INT_LATCH);
1218         WRITE_REG(sc, TXP_IMR, TXP_INT_A2H_3);
1219
1220         ifp->if_flags |= IFF_RUNNING;
1221         ifp->if_flags &= ~IFF_OACTIVE;
1222         ifp->if_timer = 0;
1223
1224         sc->sc_tick = timeout(txp_tick, sc, hz);
1225
1226         splx(s);
1227 }
1228
1229 static void
1230 txp_tick(vsc)
1231         void *vsc;
1232 {
1233         struct txp_softc *sc = vsc;
1234         struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1235         struct txp_rsp_desc *rsp = NULL;
1236         struct txp_ext_desc *ext;
1237         int s;
1238
1239         s = splnet();
1240         txp_rxbuf_reclaim(sc);
1241
1242         if (txp_command2(sc, TXP_CMD_READ_STATISTICS, 0, 0, 0, NULL, 0,
1243             &rsp, 1))
1244                 goto out;
1245         if (rsp->rsp_numdesc != 6)
1246                 goto out;
1247         if (txp_command(sc, TXP_CMD_CLEAR_STATISTICS, 0, 0, 0,
1248             NULL, NULL, NULL, 1))
1249                 goto out;
1250         ext = (struct txp_ext_desc *)(rsp + 1);
1251
1252         ifp->if_ierrors += ext[3].ext_2 + ext[3].ext_3 + ext[3].ext_4 +
1253             ext[4].ext_1 + ext[4].ext_4;
1254         ifp->if_oerrors += ext[0].ext_1 + ext[1].ext_1 + ext[1].ext_4 +
1255             ext[2].ext_1;
1256         ifp->if_collisions += ext[0].ext_2 + ext[0].ext_3 + ext[1].ext_2 +
1257             ext[1].ext_3;
1258         ifp->if_opackets += rsp->rsp_par2;
1259         ifp->if_ipackets += ext[2].ext_3;
1260
1261 out:
1262         if (rsp != NULL)
1263                 free(rsp, M_DEVBUF);
1264
1265         splx(s);
1266         sc->sc_tick = timeout(txp_tick, sc, hz);
1267
1268         return;
1269 }
1270
1271 static void
1272 txp_start(ifp)
1273         struct ifnet *ifp;
1274 {
1275         struct txp_softc *sc = ifp->if_softc;
1276         struct txp_tx_ring *r = &sc->sc_txhir;
1277         struct txp_tx_desc *txd;
1278         struct txp_frag_desc *fxd;
1279         struct mbuf *m, *m0;
1280         struct txp_swdesc *sd;
1281         u_int32_t firstprod, firstcnt, prod, cnt;
1282         struct ifvlan           *ifv;
1283
1284         if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1285                 return;
1286
1287         prod = r->r_prod;
1288         cnt = r->r_cnt;
1289
1290         while (1) {
1291                 IF_DEQUEUE(&ifp->if_snd, m);
1292                 if (m == NULL)
1293                         break;
1294
1295                 firstprod = prod;
1296                 firstcnt = cnt;
1297
1298                 sd = sc->sc_txd + prod;
1299                 sd->sd_mbuf = m;
1300
1301                 if ((TX_ENTRIES - cnt) < 4)
1302                         goto oactive;
1303
1304                 txd = r->r_desc + prod;
1305
1306                 txd->tx_flags = TX_FLAGS_TYPE_DATA;
1307                 txd->tx_numdesc = 0;
1308                 txd->tx_addrlo = 0;
1309                 txd->tx_addrhi = 0;
1310                 txd->tx_totlen = 0;
1311                 txd->tx_pflags = 0;
1312
1313                 if (++prod == TX_ENTRIES)
1314                         prod = 0;
1315
1316                 if (++cnt >= (TX_ENTRIES - 4))
1317                         goto oactive;
1318
1319                 if ((m->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
1320                     m->m_pkthdr.rcvif != NULL) {
1321                         ifv = m->m_pkthdr.rcvif->if_softc;
1322                         txd->tx_pflags = TX_PFLAGS_VLAN |
1323                             (htons(ifv->ifv_tag) << TX_PFLAGS_VLANTAG_S);
1324                 }
1325
1326                 if (m->m_pkthdr.csum_flags & CSUM_IP)
1327                         txd->tx_pflags |= TX_PFLAGS_IPCKSUM;
1328
1329 #if 0
1330                 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1331                         txd->tx_pflags |= TX_PFLAGS_TCPCKSUM;
1332                 if (m->m_pkthdr.csum_flags & CSUM_UDP)
1333                         txd->tx_pflags |= TX_PFLAGS_UDPCKSUM;
1334 #endif
1335
1336                 fxd = (struct txp_frag_desc *)(r->r_desc + prod);
1337                 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
1338                         if (m0->m_len == 0)
1339                                 continue;
1340                         if (++cnt >= (TX_ENTRIES - 4))
1341                                 goto oactive;
1342
1343                         txd->tx_numdesc++;
1344
1345                         fxd->frag_flags = FRAG_FLAGS_TYPE_FRAG;
1346                         fxd->frag_rsvd1 = 0;
1347                         fxd->frag_len = m0->m_len;
1348                         fxd->frag_addrlo = vtophys(mtod(m0, vm_offset_t));
1349                         fxd->frag_addrhi = 0;
1350                         fxd->frag_rsvd2 = 0;
1351
1352                         if (++prod == TX_ENTRIES) {
1353                                 fxd = (struct txp_frag_desc *)r->r_desc;
1354                                 prod = 0;
1355                         } else
1356                                 fxd++;
1357
1358                 }
1359
1360                 ifp->if_timer = 5;
1361
1362                 if (ifp->if_bpf)
1363                         bpf_mtap(ifp, m);
1364                 WRITE_REG(sc, r->r_reg, TXP_IDX2OFFSET(prod));
1365         }
1366
1367         r->r_prod = prod;
1368         r->r_cnt = cnt;
1369         return;
1370
1371 oactive:
1372         ifp->if_flags |= IFF_OACTIVE;
1373         r->r_prod = firstprod;
1374         r->r_cnt = firstcnt;
1375         IF_PREPEND(&ifp->if_snd, m);
1376         return;
1377 }
1378
1379 /*
1380  * Handle simple commands sent to the typhoon
1381  */
1382 static int
1383 txp_command(sc, id, in1, in2, in3, out1, out2, out3, wait)
1384         struct txp_softc *sc;
1385         u_int16_t id, in1, *out1;
1386         u_int32_t in2, in3, *out2, *out3;
1387         int wait;
1388 {
1389         struct txp_rsp_desc *rsp = NULL;
1390
1391         if (txp_command2(sc, id, in1, in2, in3, NULL, 0, &rsp, wait))
1392                 return (-1);
1393
1394         if (!wait)
1395                 return (0);
1396
1397         if (out1 != NULL)
1398                 *out1 = rsp->rsp_par1;
1399         if (out2 != NULL)
1400                 *out2 = rsp->rsp_par2;
1401         if (out3 != NULL)
1402                 *out3 = rsp->rsp_par3;
1403         free(rsp, M_DEVBUF);
1404         return (0);
1405 }
1406
1407 static int
1408 txp_command2(sc, id, in1, in2, in3, in_extp, in_extn, rspp, wait)
1409         struct txp_softc *sc;
1410         u_int16_t id, in1;
1411         u_int32_t in2, in3;
1412         struct txp_ext_desc *in_extp;
1413         u_int8_t in_extn;
1414         struct txp_rsp_desc **rspp;
1415         int wait;
1416 {
1417         struct txp_hostvar *hv = sc->sc_hostvar;
1418         struct txp_cmd_desc *cmd;
1419         struct txp_ext_desc *ext;
1420         u_int32_t idx, i;
1421         u_int16_t seq;
1422
1423         if (txp_cmd_desc_numfree(sc) < (in_extn + 1)) {
1424                 device_printf(sc->sc_dev, "no free cmd descriptors\n");
1425                 return (-1);
1426         }
1427
1428         idx = sc->sc_cmdring.lastwrite;
1429         cmd = (struct txp_cmd_desc *)(((u_int8_t *)sc->sc_cmdring.base) + idx);
1430         bzero(cmd, sizeof(*cmd));
1431
1432         cmd->cmd_numdesc = in_extn;
1433         cmd->cmd_seq = seq = sc->sc_seq++;
1434         cmd->cmd_id = id;
1435         cmd->cmd_par1 = in1;
1436         cmd->cmd_par2 = in2;
1437         cmd->cmd_par3 = in3;
1438         cmd->cmd_flags = CMD_FLAGS_TYPE_CMD |
1439             (wait ? CMD_FLAGS_RESP : 0) | CMD_FLAGS_VALID;
1440
1441         idx += sizeof(struct txp_cmd_desc);
1442         if (idx == sc->sc_cmdring.size)
1443                 idx = 0;
1444
1445         for (i = 0; i < in_extn; i++) {
1446                 ext = (struct txp_ext_desc *)(((u_int8_t *)sc->sc_cmdring.base) + idx);
1447                 bcopy(in_extp, ext, sizeof(struct txp_ext_desc));
1448                 in_extp++;
1449                 idx += sizeof(struct txp_cmd_desc);
1450                 if (idx == sc->sc_cmdring.size)
1451                         idx = 0;
1452         }
1453
1454         sc->sc_cmdring.lastwrite = idx;
1455
1456         WRITE_REG(sc, TXP_H2A_2, sc->sc_cmdring.lastwrite);
1457
1458         if (!wait)
1459                 return (0);
1460
1461         for (i = 0; i < 10000; i++) {
1462                 idx = hv->hv_resp_read_idx;
1463                 if (idx != hv->hv_resp_write_idx) {
1464                         *rspp = NULL;
1465                         if (txp_response(sc, idx, id, seq, rspp))
1466                                 return (-1);
1467                         if (*rspp != NULL)
1468                                 break;
1469                 }
1470                 DELAY(50);
1471         }
1472         if (i == 1000 || (*rspp) == NULL) {
1473                 device_printf(sc->sc_dev, "0x%x command failed\n", id);
1474                 return (-1);
1475         }
1476
1477         return (0);
1478 }
1479
1480 static int
1481 txp_response(sc, ridx, id, seq, rspp)
1482         struct txp_softc *sc;
1483         u_int32_t ridx;
1484         u_int16_t id;
1485         u_int16_t seq;
1486         struct txp_rsp_desc **rspp;
1487 {
1488         struct txp_hostvar *hv = sc->sc_hostvar;
1489         struct txp_rsp_desc *rsp;
1490
1491         while (ridx != hv->hv_resp_write_idx) {
1492                 rsp = (struct txp_rsp_desc *)(((u_int8_t *)sc->sc_rspring.base) + ridx);
1493
1494                 if (id == rsp->rsp_id && rsp->rsp_seq == seq) {
1495                         *rspp = (struct txp_rsp_desc *)malloc(
1496                             sizeof(struct txp_rsp_desc) * (rsp->rsp_numdesc + 1),
1497                             M_DEVBUF, M_INTWAIT);
1498                         if ((*rspp) == NULL)
1499                                 return (-1);
1500                         txp_rsp_fixup(sc, rsp, *rspp);
1501                         return (0);
1502                 }
1503
1504                 if (rsp->rsp_flags & RSP_FLAGS_ERROR) {
1505                         device_printf(sc->sc_dev, "response error!\n");
1506                         txp_rsp_fixup(sc, rsp, NULL);
1507                         ridx = hv->hv_resp_read_idx;
1508                         continue;
1509                 }
1510
1511                 switch (rsp->rsp_id) {
1512                 case TXP_CMD_CYCLE_STATISTICS:
1513                 case TXP_CMD_MEDIA_STATUS_READ:
1514                         break;
1515                 case TXP_CMD_HELLO_RESPONSE:
1516                         device_printf(sc->sc_dev, "hello\n");
1517                         break;
1518                 default:
1519                         device_printf(sc->sc_dev, "unknown id(0x%x)\n",
1520                             rsp->rsp_id);
1521                 }
1522
1523                 txp_rsp_fixup(sc, rsp, NULL);
1524                 ridx = hv->hv_resp_read_idx;
1525                 hv->hv_resp_read_idx = ridx;
1526         }
1527
1528         return (0);
1529 }
1530
1531 static void
1532 txp_rsp_fixup(sc, rsp, dst)
1533         struct txp_softc *sc;
1534         struct txp_rsp_desc *rsp, *dst;
1535 {
1536         struct txp_rsp_desc *src = rsp;
1537         struct txp_hostvar *hv = sc->sc_hostvar;
1538         u_int32_t i, ridx;
1539
1540         ridx = hv->hv_resp_read_idx;
1541
1542         for (i = 0; i < rsp->rsp_numdesc + 1; i++) {
1543                 if (dst != NULL)
1544                         bcopy(src, dst++, sizeof(struct txp_rsp_desc));
1545                 ridx += sizeof(struct txp_rsp_desc);
1546                 if (ridx == sc->sc_rspring.size) {
1547                         src = sc->sc_rspring.base;
1548                         ridx = 0;
1549                 } else
1550                         src++;
1551                 sc->sc_rspring.lastwrite = hv->hv_resp_read_idx = ridx;
1552         }
1553         
1554         hv->hv_resp_read_idx = ridx;
1555 }
1556
1557 static int
1558 txp_cmd_desc_numfree(sc)
1559         struct txp_softc *sc;
1560 {
1561         struct txp_hostvar *hv = sc->sc_hostvar;
1562         struct txp_boot_record *br = sc->sc_boot;
1563         u_int32_t widx, ridx, nfree;
1564
1565         widx = sc->sc_cmdring.lastwrite;
1566         ridx = hv->hv_cmd_read_idx;
1567
1568         if (widx == ridx) {
1569                 /* Ring is completely free */
1570                 nfree = br->br_cmd_siz - sizeof(struct txp_cmd_desc);
1571         } else {
1572                 if (widx > ridx)
1573                         nfree = br->br_cmd_siz -
1574                             (widx - ridx + sizeof(struct txp_cmd_desc));
1575                 else
1576                         nfree = ridx - widx - sizeof(struct txp_cmd_desc);
1577         }
1578
1579         return (nfree / sizeof(struct txp_cmd_desc));
1580 }
1581
1582 static void
1583 txp_stop(sc)
1584         struct txp_softc *sc;
1585 {
1586         struct ifnet *ifp;
1587
1588         ifp = &sc->sc_arpcom.ac_if;
1589
1590         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1591
1592         untimeout(txp_tick, sc, sc->sc_tick);
1593
1594         txp_command(sc, TXP_CMD_TX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1595         txp_command(sc, TXP_CMD_RX_DISABLE, 0, 0, 0, NULL, NULL, NULL, 1);
1596
1597         txp_rxring_empty(sc);
1598
1599         return;
1600 }
1601
1602 static void
1603 txp_watchdog(ifp)
1604         struct ifnet *ifp;
1605 {
1606         return;
1607 }
1608
1609 static int
1610 txp_ifmedia_upd(ifp)
1611         struct ifnet *ifp;
1612 {
1613         struct txp_softc *sc = ifp->if_softc;
1614         struct ifmedia *ifm = &sc->sc_ifmedia;
1615         u_int16_t new_xcvr;
1616
1617         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1618                 return (EINVAL);
1619
1620         if (IFM_SUBTYPE(ifm->ifm_media) == IFM_10_T) {
1621                 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1622                         new_xcvr = TXP_XCVR_10_FDX;
1623                 else
1624                         new_xcvr = TXP_XCVR_10_HDX;
1625         } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
1626                 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX)
1627                         new_xcvr = TXP_XCVR_100_FDX;
1628                 else
1629                         new_xcvr = TXP_XCVR_100_HDX;
1630         } else if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) {
1631                 new_xcvr = TXP_XCVR_AUTO;
1632         } else
1633                 return (EINVAL);
1634
1635         /* nothing to do */
1636         if (sc->sc_xcvr == new_xcvr)
1637                 return (0);
1638
1639         txp_command(sc, TXP_CMD_XCVR_SELECT, new_xcvr, 0, 0,
1640             NULL, NULL, NULL, 0);
1641         sc->sc_xcvr = new_xcvr;
1642
1643         return (0);
1644 }
1645
1646 static void
1647 txp_ifmedia_sts(ifp, ifmr)
1648         struct ifnet *ifp;
1649         struct ifmediareq *ifmr;
1650 {
1651         struct txp_softc *sc = ifp->if_softc;
1652         struct ifmedia *ifm = &sc->sc_ifmedia;
1653         u_int16_t bmsr, bmcr, anlpar;
1654
1655         ifmr->ifm_status = IFM_AVALID;
1656         ifmr->ifm_active = IFM_ETHER;
1657
1658         if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMSR, 0,
1659             &bmsr, NULL, NULL, 1))
1660                 goto bail;
1661         if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMSR, 0,
1662             &bmsr, NULL, NULL, 1))
1663                 goto bail;
1664
1665         if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_BMCR, 0,
1666             &bmcr, NULL, NULL, 1))
1667                 goto bail;
1668
1669         if (txp_command(sc, TXP_CMD_PHY_MGMT_READ, 0, MII_ANLPAR, 0,
1670             &anlpar, NULL, NULL, 1))
1671                 goto bail;
1672
1673         if (bmsr & BMSR_LINK)
1674                 ifmr->ifm_status |= IFM_ACTIVE;
1675
1676         if (bmcr & BMCR_ISO) {
1677                 ifmr->ifm_active |= IFM_NONE;
1678                 ifmr->ifm_status = 0;
1679                 return;
1680         }
1681
1682         if (bmcr & BMCR_LOOP)
1683                 ifmr->ifm_active |= IFM_LOOP;
1684
1685         if (bmcr & BMCR_AUTOEN) {
1686                 if ((bmsr & BMSR_ACOMP) == 0) {
1687                         ifmr->ifm_active |= IFM_NONE;
1688                         return;
1689                 }
1690
1691                 if (anlpar & ANLPAR_T4)
1692                         ifmr->ifm_active |= IFM_100_T4;
1693                 else if (anlpar & ANLPAR_TX_FD)
1694                         ifmr->ifm_active |= IFM_100_TX|IFM_FDX;
1695                 else if (anlpar & ANLPAR_TX)
1696                         ifmr->ifm_active |= IFM_100_TX;
1697                 else if (anlpar & ANLPAR_10_FD)
1698                         ifmr->ifm_active |= IFM_10_T|IFM_FDX;
1699                 else if (anlpar & ANLPAR_10)
1700                         ifmr->ifm_active |= IFM_10_T;
1701                 else
1702                         ifmr->ifm_active |= IFM_NONE;
1703         } else
1704                 ifmr->ifm_active = ifm->ifm_cur->ifm_media;
1705         return;
1706
1707 bail:
1708         ifmr->ifm_active |= IFM_NONE;
1709         ifmr->ifm_status &= ~IFM_AVALID;
1710 }
1711
1712 #ifdef TXP_DEBUG
1713 static void
1714 txp_show_descriptor(d)
1715         void *d;
1716 {
1717         struct txp_cmd_desc *cmd = d;
1718         struct txp_rsp_desc *rsp = d;
1719         struct txp_tx_desc *txd = d;
1720         struct txp_frag_desc *frgd = d;
1721
1722         switch (cmd->cmd_flags & CMD_FLAGS_TYPE_M) {
1723         case CMD_FLAGS_TYPE_CMD:
1724                 /* command descriptor */
1725                 printf("[cmd flags 0x%x num %d id %d seq %d par1 0x%x par2 0x%x par3 0x%x]\n",
1726                     cmd->cmd_flags, cmd->cmd_numdesc, cmd->cmd_id, cmd->cmd_seq,
1727                     cmd->cmd_par1, cmd->cmd_par2, cmd->cmd_par3);
1728                 break;
1729         case CMD_FLAGS_TYPE_RESP:
1730                 /* response descriptor */
1731                 printf("[rsp flags 0x%x num %d id %d seq %d par1 0x%x par2 0x%x par3 0x%x]\n",
1732                     rsp->rsp_flags, rsp->rsp_numdesc, rsp->rsp_id, rsp->rsp_seq,
1733                     rsp->rsp_par1, rsp->rsp_par2, rsp->rsp_par3);
1734                 break;
1735         case CMD_FLAGS_TYPE_DATA:
1736                 /* data header (assuming tx for now) */
1737                 printf("[data flags 0x%x num %d totlen %d addr 0x%x/0x%x pflags 0x%x]",
1738                     txd->tx_flags, txd->tx_numdesc, txd->tx_totlen,
1739                     txd->tx_addrlo, txd->tx_addrhi, txd->tx_pflags);
1740                 break;
1741         case CMD_FLAGS_TYPE_FRAG:
1742                 /* fragment descriptor */
1743                 printf("[frag flags 0x%x rsvd1 0x%x len %d addr 0x%x/0x%x rsvd2 0x%x]",
1744                     frgd->frag_flags, frgd->frag_rsvd1, frgd->frag_len,
1745                     frgd->frag_addrlo, frgd->frag_addrhi, frgd->frag_rsvd2);
1746                 break;
1747         default:
1748                 printf("[unknown(%x) flags 0x%x num %d id %d seq %d par1 0x%x par2 0x%x par3 0x%x]\n",
1749                     cmd->cmd_flags & CMD_FLAGS_TYPE_M,
1750                     cmd->cmd_flags, cmd->cmd_numdesc, cmd->cmd_id, cmd->cmd_seq,
1751                     cmd->cmd_par1, cmd->cmd_par2, cmd->cmd_par3);
1752                 break;
1753         }
1754 }
1755 #endif
1756
1757 static void
1758 txp_set_filter(sc)
1759         struct txp_softc *sc;
1760 {
1761         struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1762         u_int32_t crc, carry, hashbit, hash[2];
1763         u_int16_t filter;
1764         u_int8_t octet;
1765         int i, j, mcnt = 0;
1766         struct ifmultiaddr *ifma;
1767         char *enm;
1768
1769         if (ifp->if_flags & IFF_PROMISC) {
1770                 filter = TXP_RXFILT_PROMISC;
1771                 goto setit;
1772         }
1773
1774         filter = TXP_RXFILT_DIRECT;
1775
1776         if (ifp->if_flags & IFF_BROADCAST)
1777                 filter |= TXP_RXFILT_BROADCAST;
1778
1779         if (ifp->if_flags & IFF_ALLMULTI)
1780                 filter |= TXP_RXFILT_ALLMULTI;
1781         else {
1782                 hash[0] = hash[1] = 0;
1783
1784                 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
1785                     ifma = ifma->ifma_link.le_next) {
1786                         if (ifma->ifma_addr->sa_family != AF_LINK)
1787                                 continue;
1788
1789                         enm = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
1790                         mcnt++;
1791                         crc = 0xffffffff;
1792
1793                         for (i = 0; i < ETHER_ADDR_LEN; i++) {
1794                                 octet = enm[i];
1795                                 for (j = 0; j < 8; j++) {
1796                                         carry = ((crc & 0x80000000) ? 1 : 0) ^
1797                                             (octet & 1);
1798                                         crc <<= 1;
1799                                         octet >>= 1;
1800                                         if (carry)
1801                                                 crc = (crc ^ TXP_POLYNOMIAL) |
1802                                                     carry;
1803                                 }
1804                         }
1805                         hashbit = (u_int16_t)(crc & (64 - 1));
1806                         hash[hashbit / 32] |= (1 << hashbit % 32);
1807                 }
1808
1809                 if (mcnt > 0) {
1810                         filter |= TXP_RXFILT_HASHMULTI;
1811                         txp_command(sc, TXP_CMD_MCAST_HASH_MASK_WRITE,
1812                             2, hash[0], hash[1], NULL, NULL, NULL, 0);
1813                 }
1814         }
1815
1816 setit:
1817
1818         txp_command(sc, TXP_CMD_RX_FILTER_WRITE, filter, 0, 0,
1819             NULL, NULL, NULL, 1);
1820
1821         return;
1822 }
1823
1824 static void
1825 txp_capabilities(sc)
1826         struct txp_softc *sc;
1827 {
1828         struct ifnet *ifp = &sc->sc_arpcom.ac_if;
1829         struct txp_rsp_desc *rsp = NULL;
1830         struct txp_ext_desc *ext;
1831
1832         if (txp_command2(sc, TXP_CMD_OFFLOAD_READ, 0, 0, 0, NULL, 0, &rsp, 1))
1833                 goto out;
1834
1835         if (rsp->rsp_numdesc != 1)
1836                 goto out;
1837         ext = (struct txp_ext_desc *)(rsp + 1);
1838
1839         sc->sc_tx_capability = ext->ext_1 & OFFLOAD_MASK;
1840         sc->sc_rx_capability = ext->ext_2 & OFFLOAD_MASK;
1841         ifp->if_capabilities = 0;
1842
1843         if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_VLAN) {
1844                 sc->sc_tx_capability |= OFFLOAD_VLAN;
1845                 sc->sc_rx_capability |= OFFLOAD_VLAN;
1846         }
1847
1848 #if 0
1849         /* not ready yet */
1850         if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_IPSEC) {
1851                 sc->sc_tx_capability |= OFFLOAD_IPSEC;
1852                 sc->sc_rx_capability |= OFFLOAD_IPSEC;
1853                 ifp->if_capabilities |= IFCAP_IPSEC;
1854         }
1855 #endif
1856
1857         if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_IPCKSUM) {
1858                 sc->sc_tx_capability |= OFFLOAD_IPCKSUM;
1859                 sc->sc_rx_capability |= OFFLOAD_IPCKSUM;
1860                 ifp->if_capabilities |= IFCAP_HWCSUM;
1861                 ifp->if_hwassist |= CSUM_IP;
1862         }
1863
1864         if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_TCPCKSUM) {
1865 #if 0
1866                 sc->sc_tx_capability |= OFFLOAD_TCPCKSUM;
1867 #endif
1868                 sc->sc_rx_capability |= OFFLOAD_TCPCKSUM;
1869                 ifp->if_capabilities |= IFCAP_HWCSUM;
1870         }
1871
1872         if (rsp->rsp_par2 & rsp->rsp_par3 & OFFLOAD_UDPCKSUM) {
1873 #if 0
1874                 sc->sc_tx_capability |= OFFLOAD_UDPCKSUM;
1875 #endif
1876                 sc->sc_rx_capability |= OFFLOAD_UDPCKSUM;
1877                 ifp->if_capabilities |= IFCAP_HWCSUM;
1878         }
1879         ifp->if_capenable = ifp->if_capabilities;
1880
1881         if (txp_command(sc, TXP_CMD_OFFLOAD_WRITE, 0,
1882             sc->sc_tx_capability, sc->sc_rx_capability, NULL, NULL, NULL, 1))
1883                 goto out;
1884
1885 out:
1886         if (rsp != NULL)
1887                 free(rsp, M_DEVBUF);
1888
1889         return;
1890 }