if_fwe.c revision 150789
1139749Simp/*-
2113584Ssimokawa * Copyright (c) 2002-2003
3103285Sikob * 	Hidetoshi Shimokawa. All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that the following conditions
7103285Sikob * are met:
8103285Sikob * 1. Redistributions of source code must retain the above copyright
9103285Sikob *    notice, this list of conditions and the following disclaimer.
10103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
11103285Sikob *    notice, this list of conditions and the following disclaimer in the
12103285Sikob *    documentation and/or other materials provided with the distribution.
13103285Sikob * 3. All advertising materials mentioning features or use of this software
14103285Sikob *    must display the following acknowledgement:
15103285Sikob *
16103285Sikob *	This product includes software developed by Hidetoshi Shimokawa.
17103285Sikob *
18103285Sikob * 4. Neither the name of the author nor the names of its contributors
19103285Sikob *    may be used to endorse or promote products derived from this software
20103285Sikob *    without specific prior written permission.
21103285Sikob *
22103285Sikob * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23103285Sikob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24103285Sikob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25103285Sikob * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26103285Sikob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27103285Sikob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28103285Sikob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29103285Sikob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30103285Sikob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31103285Sikob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32103285Sikob * SUCH DAMAGE.
33103285Sikob *
34103285Sikob * $FreeBSD: head/sys/dev/firewire/if_fwe.c 150789 2005-10-01 18:56:19Z glebius $
35103285Sikob */
36103285Sikob
37103285Sikob#include "opt_inet.h"
38103285Sikob
39103285Sikob#include <sys/param.h>
40103285Sikob#include <sys/kernel.h>
41103285Sikob#include <sys/malloc.h>
42103285Sikob#include <sys/mbuf.h>
43103285Sikob#include <sys/socket.h>
44103285Sikob#include <sys/sockio.h>
45103285Sikob#include <sys/sysctl.h>
46103285Sikob#include <sys/systm.h>
47103285Sikob#include <sys/module.h>
48103285Sikob#include <sys/bus.h>
49113584Ssimokawa#include <machine/bus.h>
50103285Sikob
51103285Sikob#include <net/bpf.h>
52103285Sikob#include <net/ethernet.h>
53103285Sikob#include <net/if.h>
54103285Sikob#include <net/if_arp.h>
55147256Sbrooks#include <net/if_types.h>
56127468Ssimokawa#ifdef __DragonFly__
57127468Ssimokawa#include <net/vlan/if_vlan_var.h>
58127468Ssimokawa#include <bus/firewire/firewire.h>
59127468Ssimokawa#include <bus/firewire/firewirereg.h>
60127468Ssimokawa#include "if_fwevar.h"
61127468Ssimokawa#else
62103285Sikob#include <net/if_vlan_var.h>
63103285Sikob
64103285Sikob#include <dev/firewire/firewire.h>
65103285Sikob#include <dev/firewire/firewirereg.h>
66103285Sikob#include <dev/firewire/if_fwevar.h>
67127468Ssimokawa#endif
68103285Sikob
69122161Ssimokawa#define FWEDEBUG	if (fwedebug) if_printf
70111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
71103285Sikob
72103285Sikob/* network interface */
73124169Ssimokawastatic void fwe_start (struct ifnet *);
74124169Ssimokawastatic int fwe_ioctl (struct ifnet *, u_long, caddr_t);
75124169Ssimokawastatic void fwe_init (void *);
76103285Sikob
77124169Ssimokawastatic void fwe_output_callback (struct fw_xfer *);
78124169Ssimokawastatic void fwe_as_output (struct fwe_softc *, struct ifnet *);
79124169Ssimokawastatic void fwe_as_input (struct fw_xferq *);
80103285Sikob
81103285Sikobstatic int fwedebug = 0;
82103285Sikobstatic int stream_ch = 1;
83116139Ssimokawastatic int tx_speed = 2;
84122603Ssimokawastatic int rx_queue_len = FWMAXQUEUE;
85103285Sikob
86108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
87103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
88103285SikobSYSCTL_DECL(_hw_firewire);
89103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
90122603Ssimokawa	"Ethernet emulation subsystem");
91103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
92103285Sikob	"Stream channel to use");
93116139SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
94122603Ssimokawa	"Transmission speed");
95122603SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
96122603Ssimokawa	0, "Length of the receive queue");
97103285Sikob
98122603SsimokawaTUNABLE_INT("hw.firewire.fwe.stream_ch", &stream_ch);
99122603SsimokawaTUNABLE_INT("hw.firewire.fwe.tx_speed", &tx_speed);
100122603SsimokawaTUNABLE_INT("hw.firewire.fwe.rx_queue_len", &rx_queue_len);
101122603Ssimokawa
102103285Sikob#ifdef DEVICE_POLLING
103103285Sikobstatic poll_handler_t fwe_poll;
104103285Sikob
105103285Sikobstatic void
106103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
107103285Sikob{
108103285Sikob	struct fwe_softc *fwe;
109103285Sikob	struct firewire_comm *fc;
110103285Sikob
111150789Sglebius	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
112150789Sglebius		return;
113150789Sglebius
114103285Sikob	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
115103285Sikob	fc = fwe->fd.fc;
116103285Sikob	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
117103285Sikob}
118150789Sglebius#endif /* DEVICE_POLLING */
119150789Sglebius
120103285Sikobstatic void
121103285Sikobfwe_identify(driver_t *driver, device_t parent)
122103285Sikob{
123121953Ssimokawa	BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
124103285Sikob}
125103285Sikob
126103285Sikobstatic int
127103285Sikobfwe_probe(device_t dev)
128103285Sikob{
129103285Sikob	device_t pa;
130103285Sikob
131103285Sikob	pa = device_get_parent(dev);
132103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
133103285Sikob		return(ENXIO);
134103285Sikob	}
135103285Sikob
136108281Ssimokawa	device_set_desc(dev, "Ethernet over FireWire");
137103285Sikob	return (0);
138103285Sikob}
139103285Sikob
140103285Sikobstatic int
141103285Sikobfwe_attach(device_t dev)
142103285Sikob{
143103285Sikob	struct fwe_softc *fwe;
144103285Sikob	struct ifnet *ifp;
145103285Sikob	int unit, s;
146147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
147103285Sikob	u_char *eaddr;
148147256Sbrooks#else
149147256Sbrooks	u_char eaddr[6];
150147256Sbrooks#endif
151109814Ssimokawa	struct fw_eui64 *eui;
152103285Sikob
153103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
154103285Sikob	unit = device_get_unit(dev);
155103285Sikob
156103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
157103285Sikob	/* XXX */
158103285Sikob	fwe->stream_ch = stream_ch;
159103285Sikob	fwe->dma_ch = -1;
160103285Sikob
161103285Sikob	fwe->fd.fc = device_get_ivars(dev);
162124251Ssimokawa	if (tx_speed < 0)
163124251Ssimokawa		tx_speed = fwe->fd.fc->speed;
164124251Ssimokawa
165103285Sikob	fwe->fd.dev = dev;
166103285Sikob	fwe->fd.post_explore = NULL;
167103285Sikob	fwe->eth_softc.fwe = fwe;
168103285Sikob
169103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
170103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
171103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
172103285Sikob
173103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
174103285Sikob#define LOCAL (0x02)
175103285Sikob#define GROUP (0x01)
176147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
177147256Sbrooks	eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
178147256Sbrooks#endif
179109814Ssimokawa
180147256Sbrooks
181109814Ssimokawa	eui = &fwe->fd.fc->eui;
182109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
183109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
184109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
185109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
186109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
187109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
188107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
189107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
190103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
191103285Sikob
192103285Sikob	/* fill the rest and attach interface */
193147256Sbrooks	ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
194147256Sbrooks	if (ifp == NULL) {
195147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
196147256Sbrooks		return (ENOSPC);
197147256Sbrooks	}
198103285Sikob	ifp->if_softc = &fwe->eth_softc;
199103285Sikob
200127468Ssimokawa#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
201121953Ssimokawa	if_initname(ifp, device_get_name(dev), unit);
202122212Ssimokawa#else
203122212Ssimokawa	ifp->if_unit = unit;
204122212Ssimokawa	ifp->if_name = "fwe";
205122212Ssimokawa#endif
206103285Sikob	ifp->if_init = fwe_init;
207132430Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
208132430Ssimokawa	ifp->if_output = ether_output;
209132430Ssimokawa#endif
210103285Sikob	ifp->if_start = fwe_start;
211103285Sikob	ifp->if_ioctl = fwe_ioctl;
212103285Sikob	ifp->if_mtu = ETHERMTU;
213133538Srwatson	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST|
214133538Srwatson	    IFF_NEEDSGIANT);
215111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
216103285Sikob
217103285Sikob	s = splimp();
218127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
219127468Ssimokawa	ether_ifattach(ifp, 1);
220127468Ssimokawa#else
221106937Ssam	ether_ifattach(ifp, eaddr);
222108712Ssimokawa#endif
223103285Sikob	splx(s);
224103285Sikob
225103285Sikob        /* Tell the upper layer(s) we support long frames. */
226103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
227127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
228150789Sglebius	ifp->if_capabilities |= IFCAP_VLAN_MTU & IFCAP_POLLING;
229129552Syar	ifp->if_capenable |= IFCAP_VLAN_MTU;
230108712Ssimokawa#endif
231103285Sikob
232103285Sikob
233122161Ssimokawa	FWEDEBUG(ifp, "interface created\n");
234103285Sikob	return 0;
235103285Sikob}
236103285Sikob
237103285Sikobstatic void
238103285Sikobfwe_stop(struct fwe_softc *fwe)
239103285Sikob{
240103285Sikob	struct firewire_comm *fc;
241103285Sikob	struct fw_xferq *xferq;
242147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
243111942Ssimokawa	struct fw_xfer *xfer, *next;
244111942Ssimokawa	int i;
245103285Sikob
246103285Sikob	fc = fwe->fd.fc;
247103285Sikob
248103285Sikob	if (fwe->dma_ch >= 0) {
249103285Sikob		xferq = fc->ir[fwe->dma_ch];
250103285Sikob
251103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
252103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
253103285Sikob		xferq->flag &=
254113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
255113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
256111942Ssimokawa		xferq->hand =  NULL;
257111942Ssimokawa
258111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
259111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
260111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
261111942Ssimokawa
262111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
263111942Ssimokawa					xfer = next) {
264111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
265111942Ssimokawa			fw_xfer_free(xfer);
266111942Ssimokawa		}
267111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
268111942Ssimokawa
269111942Ssimokawa		xferq->bulkxfer =  NULL;
270103285Sikob		fwe->dma_ch = -1;
271103285Sikob	}
272103285Sikob
273148887Srwatson#if defined(__FreeBSD__)
274148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
275148887Srwatson#else
276103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
277148887Srwatson#endif
278103285Sikob}
279103285Sikob
280103285Sikobstatic int
281103285Sikobfwe_detach(device_t dev)
282103285Sikob{
283103285Sikob	struct fwe_softc *fwe;
284147256Sbrooks	struct ifnet *ifp;
285103285Sikob	int s;
286103285Sikob
287147256Sbrooks	fwe = device_get_softc(dev);
288147256Sbrooks	ifp = fwe->eth_softc.ifp;
289150789Sglebius
290150789Sglebius#ifdef DEVICE_POLLING
291150789Sglebius	if (ifp->if_capenable & IFCAP_POLLING)
292150789Sglebius		ether_poll_deregister(ifp);
293150789Sglebius#endif
294103285Sikob	s = splimp();
295103285Sikob
296103285Sikob	fwe_stop(fwe);
297127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
298147256Sbrooks	ether_ifdetach(ifp, 1);
299127468Ssimokawa#else
300147256Sbrooks	ether_ifdetach(ifp);
301147256Sbrooks	if_free(ifp);
302108712Ssimokawa#endif
303103285Sikob
304103285Sikob	splx(s);
305103285Sikob	return 0;
306103285Sikob}
307103285Sikob
308103285Sikobstatic void
309103285Sikobfwe_init(void *arg)
310103285Sikob{
311103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
312103285Sikob	struct firewire_comm *fc;
313147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
314103285Sikob	struct fw_xferq *xferq;
315111942Ssimokawa	struct fw_xfer *xfer;
316113584Ssimokawa	struct mbuf *m;
317103285Sikob	int i;
318103285Sikob
319122161Ssimokawa	FWEDEBUG(ifp, "initializing\n");
320103285Sikob
321103285Sikob	/* XXX keep promiscoud mode */
322103285Sikob	ifp->if_flags |= IFF_PROMISC;
323103285Sikob
324103285Sikob	fc = fwe->fd.fc;
325103285Sikob#define START 0
326103285Sikob	if (fwe->dma_ch < 0) {
327103285Sikob		for (i = START; i < fc->nisodma; i ++) {
328103285Sikob			xferq = fc->ir[i];
329103285Sikob			if ((xferq->flag & FWXFERQ_OPEN) == 0)
330118312Ssimokawa				goto found;
331103285Sikob		}
332118312Ssimokawa		printf("no free dma channel\n");
333118312Ssimokawa		return;
334118312Ssimokawafound:
335103285Sikob		fwe->dma_ch = i;
336103285Sikob		fwe->stream_ch = stream_ch;
337103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
338103285Sikob		/* allocate DMA channel and init packet mode */
339113584Ssimokawa		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
340113584Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
341112400Ssimokawa		xferq->flag &= ~0xff;
342103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
343103285Sikob		/* register fwe_input handler */
344103285Sikob		xferq->sc = (caddr_t) fwe;
345103285Sikob		xferq->hand = fwe_as_input;
346122603Ssimokawa		xferq->bnchunk = rx_queue_len;
347111942Ssimokawa		xferq->bnpacket = 1;
348111942Ssimokawa		xferq->psize = MCLBYTES;
349111942Ssimokawa		xferq->queued = 0;
350113584Ssimokawa		xferq->buf = NULL;
351111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
352113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
353113584Ssimokawa							M_FWE, M_WAITOK);
354111942Ssimokawa		if (xferq->bulkxfer == NULL) {
355111942Ssimokawa			printf("if_fwe: malloc failed\n");
356111942Ssimokawa			return;
357111942Ssimokawa		}
358111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
359111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
360111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
361111942Ssimokawa		xferq->stproc = NULL;
362111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
363113584Ssimokawa			m =
364127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
365127468Ssimokawa				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
366127468Ssimokawa#else
367111942Ssimokawa				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
368111942Ssimokawa#endif
369113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
370113584Ssimokawa			if (m != NULL) {
371113584Ssimokawa				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
372113584Ssimokawa				STAILQ_INSERT_TAIL(&xferq->stfree,
373113584Ssimokawa						&xferq->bulkxfer[i], link);
374113584Ssimokawa			} else
375113584Ssimokawa				printf("fwe_as_input: m_getcl failed\n");
376111942Ssimokawa		}
377111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
378111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
379111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
380111942Ssimokawa			if (xfer == NULL)
381111942Ssimokawa				break;
382120660Ssimokawa			xfer->send.spd = tx_speed;
383111942Ssimokawa			xfer->fc = fwe->fd.fc;
384111942Ssimokawa			xfer->retry_req = fw_asybusy;
385111942Ssimokawa			xfer->sc = (caddr_t)fwe;
386111942Ssimokawa			xfer->act.hand = fwe_output_callback;
387111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
388111942Ssimokawa		}
389103285Sikob	} else
390103285Sikob		xferq = fc->ir[fwe->dma_ch];
391103285Sikob
392103285Sikob
393103285Sikob	/* start dma */
394103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
395103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
396103285Sikob
397148887Srwatson#if defined(__FreeBSD__)
398148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
399148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
400148887Srwatson#else
401103285Sikob	ifp->if_flags |= IFF_RUNNING;
402103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
403148887Srwatson#endif
404103285Sikob
405103285Sikob#if 0
406103285Sikob	/* attempt to start output */
407103285Sikob	fwe_start(ifp);
408103285Sikob#endif
409103285Sikob}
410103285Sikob
411103285Sikob
412103285Sikobstatic int
413103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
414103285Sikob{
415103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
416103285Sikob	struct ifstat *ifs = NULL;
417103285Sikob	int s, error, len;
418103285Sikob
419103285Sikob	switch (cmd) {
420103285Sikob		case SIOCSIFFLAGS:
421103285Sikob			s = splimp();
422103285Sikob			if (ifp->if_flags & IFF_UP) {
423148887Srwatson#if defined(__FreeBSD__)
424148887Srwatson				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
425148887Srwatson#else
426103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
427148887Srwatson#endif
428103285Sikob					fwe_init(&fwe->eth_softc);
429103285Sikob			} else {
430148887Srwatson#if defined(__FreeBSD__)
431148887Srwatson				if (ifp->if_drv_flags & IFF_DRV_RUNNING)
432148887Srwatson#else
433103285Sikob				if (ifp->if_flags & IFF_RUNNING)
434148887Srwatson#endif
435103285Sikob					fwe_stop(fwe);
436103285Sikob			}
437103285Sikob			/* XXX keep promiscoud mode */
438103285Sikob			ifp->if_flags |= IFF_PROMISC;
439103285Sikob			splx(s);
440103285Sikob			break;
441103285Sikob		case SIOCADDMULTI:
442103285Sikob		case SIOCDELMULTI:
443108712Ssimokawa			break;
444103285Sikob
445103285Sikob		case SIOCGIFSTATUS:
446103285Sikob			s = splimp();
447103285Sikob			ifs = (struct ifstat *)data;
448103285Sikob			len = strlen(ifs->ascii);
449103285Sikob			if (len < sizeof(ifs->ascii))
450103285Sikob				snprintf(ifs->ascii + len,
451103285Sikob					sizeof(ifs->ascii) - len,
452103285Sikob					"\tch %d dma %d\n",
453103285Sikob						fwe->stream_ch, fwe->dma_ch);
454103285Sikob			splx(s);
455108712Ssimokawa			break;
456150789Sglebius		case SIOCSIFCAP:
457150789Sglebius#ifdef DEVICE_POLLING
458150789Sglebius		    {
459150789Sglebius			struct ifreq *ifr = (struct ifreq *) data;
460150789Sglebius			struct firewire_comm *fc = fc = fwe->fd.fc;
461150789Sglebius
462150789Sglebius			if (ifr->ifr_reqcap & IFCAP_POLLING &&
463150789Sglebius			    !(ifp->if_capenable & IFCAP_POLLING)) {
464150789Sglebius				error = ether_poll_register(fwe_poll, ifp);
465150789Sglebius				if (error)
466150789Sglebius					return(error);
467150789Sglebius				/* Disable interrupts */
468150789Sglebius				fc->set_intr(fc, 0);
469150789Sglebius				ifp->if_capenable |= IFCAP_POLLING;
470150789Sglebius				return (error);
471150789Sglebius
472150789Sglebius			}
473150789Sglebius			if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
474150789Sglebius			    ifp->if_capenable & IFCAP_POLLING) {
475150789Sglebius				error = ether_poll_deregister(ifp);
476150789Sglebius				/* Enable interrupts. */
477150789Sglebius				fc->set_intr(fc, 1);
478150789Sglebius				ifp->if_capenable &= ~IFCAP_POLLING;
479150789Sglebius				return (error);
480150789Sglebius			}
481150789Sglebius		    }
482150789Sglebius#endif /* DEVICE_POLLING */
483150789Sglebius			break;
484127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
485103285Sikob		default:
486108712Ssimokawa#else
487108712Ssimokawa		case SIOCSIFADDR:
488108712Ssimokawa		case SIOCGIFADDR:
489108712Ssimokawa		case SIOCSIFMTU:
490108712Ssimokawa#endif
491106937Ssam			s = splimp();
492106937Ssam			error = ether_ioctl(ifp, cmd, data);
493106937Ssam			splx(s);
494106937Ssam			return (error);
495127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
496108712Ssimokawa		default:
497108712Ssimokawa			return (EINVAL);
498108712Ssimokawa#endif
499103285Sikob	}
500103285Sikob
501103285Sikob	return (0);
502103285Sikob}
503103285Sikob
504103285Sikobstatic void
505111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
506111942Ssimokawa{
507111942Ssimokawa	struct fwe_softc *fwe;
508111942Ssimokawa	struct ifnet *ifp;
509111942Ssimokawa	int s;
510111942Ssimokawa
511111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
512147256Sbrooks	ifp = fwe->eth_softc.ifp;
513111942Ssimokawa	/* XXX error check */
514122161Ssimokawa	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
515111942Ssimokawa	if (xfer->resp != 0)
516111942Ssimokawa		ifp->if_oerrors ++;
517111942Ssimokawa
518111942Ssimokawa	m_freem(xfer->mbuf);
519111942Ssimokawa	fw_xfer_unload(xfer);
520113584Ssimokawa
521111942Ssimokawa	s = splimp();
522111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
523111942Ssimokawa	splx(s);
524113584Ssimokawa
525113584Ssimokawa	/* for queue full */
526111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
527111942Ssimokawa		fwe_start(ifp);
528111942Ssimokawa}
529111942Ssimokawa
530111942Ssimokawastatic void
531103285Sikobfwe_start(struct ifnet *ifp)
532103285Sikob{
533103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
534103285Sikob	int s;
535103285Sikob
536133930Srwatson	GIANT_REQUIRED;
537133930Srwatson
538122161Ssimokawa	FWEDEBUG(ifp, "starting\n");
539103285Sikob
540103285Sikob	if (fwe->dma_ch < 0) {
541103285Sikob		struct mbuf	*m = NULL;
542103285Sikob
543122161Ssimokawa		FWEDEBUG(ifp, "not ready\n");
544103285Sikob
545103285Sikob		s = splimp();
546103285Sikob		do {
547103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
548103285Sikob			if (m != NULL)
549103285Sikob				m_freem(m);
550103285Sikob			ifp->if_oerrors ++;
551103285Sikob		} while (m != NULL);
552103285Sikob		splx(s);
553103285Sikob
554103285Sikob		return;
555103285Sikob	}
556103285Sikob
557103285Sikob	s = splimp();
558148887Srwatson#if defined(__FreeBSD__)
559148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
560148887Srwatson#else
561103285Sikob	ifp->if_flags |= IFF_OACTIVE;
562148887Srwatson#endif
563103285Sikob
564103285Sikob	if (ifp->if_snd.ifq_len != 0)
565103285Sikob		fwe_as_output(fwe, ifp);
566103285Sikob
567148887Srwatson#if defined(__FreeBSD__)
568148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
569148887Srwatson#else
570103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
571148887Srwatson#endif
572103285Sikob	splx(s);
573103285Sikob}
574103285Sikob
575111942Ssimokawa#define HDR_LEN 4
576111942Ssimokawa#ifndef ETHER_ALIGN
577111942Ssimokawa#define ETHER_ALIGN 2
578103285Sikob#endif
579103285Sikob/* Async. stream output */
580103285Sikobstatic void
581103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
582103285Sikob{
583103285Sikob	struct mbuf *m;
584103285Sikob	struct fw_xfer *xfer;
585103285Sikob	struct fw_xferq *xferq;
586103285Sikob	struct fw_pkt *fp;
587103285Sikob	int i = 0;
588103285Sikob
589103285Sikob	xfer = NULL;
590103285Sikob	xferq = fwe->fd.fc->atq;
591111942Ssimokawa	while (xferq->queued < xferq->maxq - 1) {
592111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
593111942Ssimokawa		if (xfer == NULL) {
594111942Ssimokawa			printf("if_fwe: lack of xfer\n");
595111942Ssimokawa			return;
596111942Ssimokawa		}
597103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
598103285Sikob		if (m == NULL)
599103285Sikob			break;
600111942Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
601127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
602108712Ssimokawa		if (ifp->if_bpf != NULL)
603108712Ssimokawa			bpf_mtap(ifp, m);
604127468Ssimokawa#else
605127468Ssimokawa		BPF_MTAP(ifp, m);
606108712Ssimokawa#endif
607103285Sikob
608103285Sikob		/* keep ip packet alignment for alpha */
609111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
610120660Ssimokawa		fp = &xfer->send.hdr;
611129585Sdfr		*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
612113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
613103285Sikob		xfer->mbuf = m;
614120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
615103285Sikob
616111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
617103285Sikob			/* error */
618103285Sikob			ifp->if_oerrors ++;
619103285Sikob			/* XXX set error code */
620103285Sikob			fwe_output_callback(xfer);
621103285Sikob		} else {
622103285Sikob			ifp->if_opackets ++;
623111942Ssimokawa			i++;
624103285Sikob		}
625103285Sikob	}
626103285Sikob#if 0
627103285Sikob	if (i > 1)
628103285Sikob		printf("%d queued\n", i);
629103285Sikob#endif
630111942Ssimokawa	if (i > 0)
631111942Ssimokawa		xferq->start(fwe->fd.fc);
632103285Sikob}
633103285Sikob
634103285Sikob/* Async. stream output */
635103285Sikobstatic void
636103285Sikobfwe_as_input(struct fw_xferq *xferq)
637103285Sikob{
638113584Ssimokawa	struct mbuf *m, *m0;
639103285Sikob	struct ifnet *ifp;
640103285Sikob	struct fwe_softc *fwe;
641111942Ssimokawa	struct fw_bulkxfer *sxfer;
642111942Ssimokawa	struct fw_pkt *fp;
643103285Sikob	u_char *c;
644127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
645111942Ssimokawa	struct ether_header *eh;
646111942Ssimokawa#endif
647103285Sikob
648103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
649147256Sbrooks	ifp = fwe->eth_softc.ifp;
650150789Sglebius
651111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
652111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
653113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
654111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
655111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
656111942Ssimokawa		m = sxfer->mbuf;
657111942Ssimokawa
658119119Ssimokawa		/* insert new rbuf */
659113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
660113584Ssimokawa		if (m0 != NULL) {
661113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
662113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
663113584Ssimokawa		} else
664113584Ssimokawa			printf("fwe_as_input: m_getcl failed\n");
665111942Ssimokawa
666119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
667119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
668119119Ssimokawa			m_freem(m);
669119119Ssimokawa			ifp->if_ierrors ++;
670119119Ssimokawa			continue;
671119119Ssimokawa		}
672119119Ssimokawa
673111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
674111942Ssimokawa		c = mtod(m, char *);
675127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
676111942Ssimokawa		eh = (struct ether_header *)c;
677111942Ssimokawa		m->m_data += sizeof(struct ether_header);
678132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
679132429Ssimokawa		    - sizeof(struct ether_header);
680132429Ssimokawa#else
681132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
682108712Ssimokawa#endif
683103285Sikob		m->m_pkthdr.rcvif = ifp;
684103285Sikob#if 0
685122161Ssimokawa		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
686103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
687103285Sikob			 "%02x %02x %02x %02x\n"
688103285Sikob			 "%02x %02x %02x %02x\n"
689103285Sikob			 "%02x %02x %02x %02x\n"
690103285Sikob			 "%02x %02x %02x %02x\n",
691103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
692103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
693103285Sikob			 c[12], c[13], c[14], c[15],
694103285Sikob			 c[16], c[17], c[18], c[19],
695103285Sikob			 c[20], c[21], c[22], c[23],
696103285Sikob			 c[20], c[21], c[22], c[23]
697103285Sikob		 );
698103285Sikob#endif
699127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
700127468Ssimokawa		ether_input(ifp, eh, m);
701127468Ssimokawa#else
702106937Ssam		(*ifp->if_input)(ifp, m);
703108712Ssimokawa#endif
704103285Sikob		ifp->if_ipackets ++;
705103285Sikob	}
706111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
707111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
708103285Sikob}
709103285Sikob
710103285Sikob
711103285Sikobstatic devclass_t fwe_devclass;
712103285Sikob
713103285Sikobstatic device_method_t fwe_methods[] = {
714103285Sikob	/* device interface */
715103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
716103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
717103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
718103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
719103285Sikob	{ 0, 0 }
720103285Sikob};
721103285Sikob
722103285Sikobstatic driver_t fwe_driver = {
723121953Ssimokawa        "fwe",
724103285Sikob	fwe_methods,
725103285Sikob	sizeof(struct fwe_softc),
726103285Sikob};
727103285Sikob
728103285Sikob
729127468Ssimokawa#ifdef __DragonFly__
730127468SsimokawaDECLARE_DUMMY_MODULE(fwe);
731127468Ssimokawa#endif
732113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
733113506SmdoddMODULE_VERSION(fwe, 1);
734113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
735