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