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