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