if_fwe.c revision 177599
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 177599 2008-03-25 09:39:02Z ru $
35103285Sikob */
36103285Sikob
37150968Sglebius#ifdef HAVE_KERNEL_OPTION_HEADERS
38150968Sglebius#include "opt_device_polling.h"
39103285Sikob#include "opt_inet.h"
40150968Sglebius#endif
41103285Sikob
42103285Sikob#include <sys/param.h>
43103285Sikob#include <sys/kernel.h>
44103285Sikob#include <sys/malloc.h>
45103285Sikob#include <sys/mbuf.h>
46103285Sikob#include <sys/socket.h>
47103285Sikob#include <sys/sockio.h>
48103285Sikob#include <sys/sysctl.h>
49103285Sikob#include <sys/systm.h>
50103285Sikob#include <sys/module.h>
51103285Sikob#include <sys/bus.h>
52113584Ssimokawa#include <machine/bus.h>
53103285Sikob
54103285Sikob#include <net/bpf.h>
55103285Sikob#include <net/ethernet.h>
56103285Sikob#include <net/if.h>
57103285Sikob#include <net/if_arp.h>
58147256Sbrooks#include <net/if_types.h>
59127468Ssimokawa#ifdef __DragonFly__
60127468Ssimokawa#include <net/vlan/if_vlan_var.h>
61127468Ssimokawa#include <bus/firewire/firewire.h>
62127468Ssimokawa#include <bus/firewire/firewirereg.h>
63127468Ssimokawa#include "if_fwevar.h"
64127468Ssimokawa#else
65103285Sikob#include <net/if_vlan_var.h>
66103285Sikob
67103285Sikob#include <dev/firewire/firewire.h>
68103285Sikob#include <dev/firewire/firewirereg.h>
69103285Sikob#include <dev/firewire/if_fwevar.h>
70127468Ssimokawa#endif
71103285Sikob
72122161Ssimokawa#define FWEDEBUG	if (fwedebug) if_printf
73111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
74103285Sikob
75103285Sikob/* network interface */
76124169Ssimokawastatic void fwe_start (struct ifnet *);
77124169Ssimokawastatic int fwe_ioctl (struct ifnet *, u_long, caddr_t);
78124169Ssimokawastatic void fwe_init (void *);
79103285Sikob
80124169Ssimokawastatic void fwe_output_callback (struct fw_xfer *);
81124169Ssimokawastatic void fwe_as_output (struct fwe_softc *, struct ifnet *);
82124169Ssimokawastatic void fwe_as_input (struct fw_xferq *);
83103285Sikob
84103285Sikobstatic int fwedebug = 0;
85103285Sikobstatic int stream_ch = 1;
86116139Ssimokawastatic int tx_speed = 2;
87122603Ssimokawastatic int rx_queue_len = FWMAXQUEUE;
88103285Sikob
89108281SsimokawaMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
90103285SikobSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
91103285SikobSYSCTL_DECL(_hw_firewire);
92103285SikobSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
93122603Ssimokawa	"Ethernet emulation subsystem");
94103285SikobSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
95103285Sikob	"Stream channel to use");
96116139SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
97122603Ssimokawa	"Transmission speed");
98122603SsimokawaSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
99122603Ssimokawa	0, "Length of the receive queue");
100103285Sikob
101122603SsimokawaTUNABLE_INT("hw.firewire.fwe.stream_ch", &stream_ch);
102122603SsimokawaTUNABLE_INT("hw.firewire.fwe.tx_speed", &tx_speed);
103122603SsimokawaTUNABLE_INT("hw.firewire.fwe.rx_queue_len", &rx_queue_len);
104122603Ssimokawa
105103285Sikob#ifdef DEVICE_POLLING
106103285Sikobstatic poll_handler_t fwe_poll;
107103285Sikob
108103285Sikobstatic void
109103285Sikobfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
110103285Sikob{
111103285Sikob	struct fwe_softc *fwe;
112103285Sikob	struct firewire_comm *fc;
113103285Sikob
114150789Sglebius	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
115150789Sglebius		return;
116150789Sglebius
117103285Sikob	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
118103285Sikob	fc = fwe->fd.fc;
119103285Sikob	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
120103285Sikob}
121150789Sglebius#endif /* DEVICE_POLLING */
122150789Sglebius
123103285Sikobstatic void
124103285Sikobfwe_identify(driver_t *driver, device_t parent)
125103285Sikob{
126121953Ssimokawa	BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
127103285Sikob}
128103285Sikob
129103285Sikobstatic int
130103285Sikobfwe_probe(device_t dev)
131103285Sikob{
132103285Sikob	device_t pa;
133103285Sikob
134103285Sikob	pa = device_get_parent(dev);
135103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
136103285Sikob		return(ENXIO);
137103285Sikob	}
138103285Sikob
139108281Ssimokawa	device_set_desc(dev, "Ethernet over FireWire");
140103285Sikob	return (0);
141103285Sikob}
142103285Sikob
143103285Sikobstatic int
144103285Sikobfwe_attach(device_t dev)
145103285Sikob{
146103285Sikob	struct fwe_softc *fwe;
147103285Sikob	struct ifnet *ifp;
148103285Sikob	int unit, s;
149147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
150103285Sikob	u_char *eaddr;
151147256Sbrooks#else
152147256Sbrooks	u_char eaddr[6];
153147256Sbrooks#endif
154109814Ssimokawa	struct fw_eui64 *eui;
155103285Sikob
156103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
157103285Sikob	unit = device_get_unit(dev);
158103285Sikob
159103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
160170374Ssimokawa	mtx_init(&fwe->mtx, "fwe", NULL, MTX_DEF);
161103285Sikob	/* XXX */
162103285Sikob	fwe->stream_ch = stream_ch;
163103285Sikob	fwe->dma_ch = -1;
164103285Sikob
165103285Sikob	fwe->fd.fc = device_get_ivars(dev);
166124251Ssimokawa	if (tx_speed < 0)
167124251Ssimokawa		tx_speed = fwe->fd.fc->speed;
168124251Ssimokawa
169103285Sikob	fwe->fd.dev = dev;
170103285Sikob	fwe->fd.post_explore = NULL;
171103285Sikob	fwe->eth_softc.fwe = fwe;
172103285Sikob
173103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
174103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
175103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
176103285Sikob
177103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
178103285Sikob#define LOCAL (0x02)
179103285Sikob#define GROUP (0x01)
180147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
181147256Sbrooks	eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
182147256Sbrooks#endif
183109814Ssimokawa
184147256Sbrooks
185109814Ssimokawa	eui = &fwe->fd.fc->eui;
186109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
187109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
188109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
189109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
190109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
191109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
192107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
193107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
194103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
195103285Sikob
196103285Sikob	/* fill the rest and attach interface */
197147256Sbrooks	ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
198147256Sbrooks	if (ifp == NULL) {
199147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
200147256Sbrooks		return (ENOSPC);
201147256Sbrooks	}
202103285Sikob	ifp->if_softc = &fwe->eth_softc;
203103285Sikob
204127468Ssimokawa#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
205121953Ssimokawa	if_initname(ifp, device_get_name(dev), unit);
206122212Ssimokawa#else
207122212Ssimokawa	ifp->if_unit = unit;
208122212Ssimokawa	ifp->if_name = "fwe";
209122212Ssimokawa#endif
210103285Sikob	ifp->if_init = fwe_init;
211132430Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
212132430Ssimokawa	ifp->if_output = ether_output;
213132430Ssimokawa#endif
214103285Sikob	ifp->if_start = fwe_start;
215103285Sikob	ifp->if_ioctl = fwe_ioctl;
216103285Sikob	ifp->if_mtu = ETHERMTU;
217170374Ssimokawa	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
218111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
219103285Sikob
220103285Sikob	s = splimp();
221127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
222127468Ssimokawa	ether_ifattach(ifp, 1);
223127468Ssimokawa#else
224106937Ssam	ether_ifattach(ifp, eaddr);
225108712Ssimokawa#endif
226103285Sikob	splx(s);
227103285Sikob
228103285Sikob        /* Tell the upper layer(s) we support long frames. */
229103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
230127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
231151229Sglebius	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_POLLING;
232129552Syar	ifp->if_capenable |= IFCAP_VLAN_MTU;
233108712Ssimokawa#endif
234103285Sikob
235103285Sikob
236122161Ssimokawa	FWEDEBUG(ifp, "interface created\n");
237103285Sikob	return 0;
238103285Sikob}
239103285Sikob
240103285Sikobstatic void
241103285Sikobfwe_stop(struct fwe_softc *fwe)
242103285Sikob{
243103285Sikob	struct firewire_comm *fc;
244103285Sikob	struct fw_xferq *xferq;
245147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
246111942Ssimokawa	struct fw_xfer *xfer, *next;
247111942Ssimokawa	int i;
248103285Sikob
249103285Sikob	fc = fwe->fd.fc;
250103285Sikob
251103285Sikob	if (fwe->dma_ch >= 0) {
252103285Sikob		xferq = fc->ir[fwe->dma_ch];
253103285Sikob
254103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
255103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
256103285Sikob		xferq->flag &=
257113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
258113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
259111942Ssimokawa		xferq->hand =  NULL;
260111942Ssimokawa
261111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
262111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
263111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
264111942Ssimokawa
265111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
266111942Ssimokawa					xfer = next) {
267111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
268111942Ssimokawa			fw_xfer_free(xfer);
269111942Ssimokawa		}
270111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
271111942Ssimokawa
272111942Ssimokawa		xferq->bulkxfer =  NULL;
273103285Sikob		fwe->dma_ch = -1;
274103285Sikob	}
275103285Sikob
276148887Srwatson#if defined(__FreeBSD__)
277148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
278148887Srwatson#else
279103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
280148887Srwatson#endif
281103285Sikob}
282103285Sikob
283103285Sikobstatic int
284103285Sikobfwe_detach(device_t dev)
285103285Sikob{
286103285Sikob	struct fwe_softc *fwe;
287147256Sbrooks	struct ifnet *ifp;
288103285Sikob	int s;
289103285Sikob
290147256Sbrooks	fwe = device_get_softc(dev);
291147256Sbrooks	ifp = fwe->eth_softc.ifp;
292150789Sglebius
293150789Sglebius#ifdef DEVICE_POLLING
294150789Sglebius	if (ifp->if_capenable & IFCAP_POLLING)
295150789Sglebius		ether_poll_deregister(ifp);
296150789Sglebius#endif
297103285Sikob	s = splimp();
298103285Sikob
299103285Sikob	fwe_stop(fwe);
300127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
301147256Sbrooks	ether_ifdetach(ifp, 1);
302127468Ssimokawa#else
303147256Sbrooks	ether_ifdetach(ifp);
304147256Sbrooks	if_free(ifp);
305108712Ssimokawa#endif
306103285Sikob
307103285Sikob	splx(s);
308170374Ssimokawa	mtx_destroy(&fwe->mtx);
309103285Sikob	return 0;
310103285Sikob}
311103285Sikob
312103285Sikobstatic void
313103285Sikobfwe_init(void *arg)
314103285Sikob{
315103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
316103285Sikob	struct firewire_comm *fc;
317147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
318103285Sikob	struct fw_xferq *xferq;
319111942Ssimokawa	struct fw_xfer *xfer;
320113584Ssimokawa	struct mbuf *m;
321103285Sikob	int i;
322103285Sikob
323122161Ssimokawa	FWEDEBUG(ifp, "initializing\n");
324103285Sikob
325103285Sikob	/* XXX keep promiscoud mode */
326103285Sikob	ifp->if_flags |= IFF_PROMISC;
327103285Sikob
328103285Sikob	fc = fwe->fd.fc;
329103285Sikob	if (fwe->dma_ch < 0) {
330170374Ssimokawa		fwe->dma_ch = fw_open_isodma(fc, /* tx */0);
331170374Ssimokawa		if (fwe->dma_ch < 0)
332170374Ssimokawa			return;
333170374Ssimokawa		xferq = fc->ir[fwe->dma_ch];
334170374Ssimokawa		xferq->flag |= FWXFERQ_EXTBUF |
335170374Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
336103285Sikob		fwe->stream_ch = stream_ch;
337103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
338112400Ssimokawa		xferq->flag &= ~0xff;
339103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
340103285Sikob		/* register fwe_input handler */
341103285Sikob		xferq->sc = (caddr_t) fwe;
342103285Sikob		xferq->hand = fwe_as_input;
343122603Ssimokawa		xferq->bnchunk = rx_queue_len;
344111942Ssimokawa		xferq->bnpacket = 1;
345111942Ssimokawa		xferq->psize = MCLBYTES;
346111942Ssimokawa		xferq->queued = 0;
347113584Ssimokawa		xferq->buf = NULL;
348111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
349113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
350113584Ssimokawa							M_FWE, M_WAITOK);
351111942Ssimokawa		if (xferq->bulkxfer == NULL) {
352111942Ssimokawa			printf("if_fwe: malloc failed\n");
353111942Ssimokawa			return;
354111942Ssimokawa		}
355111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
356111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
357111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
358111942Ssimokawa		xferq->stproc = NULL;
359111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
360177599Sru			m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
361113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
362177599Sru			m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
363177599Sru			STAILQ_INSERT_TAIL(&xferq->stfree,
364177599Sru					&xferq->bulkxfer[i], link);
365111942Ssimokawa		}
366111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
367111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
368111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
369111942Ssimokawa			if (xfer == NULL)
370111942Ssimokawa				break;
371120660Ssimokawa			xfer->send.spd = tx_speed;
372111942Ssimokawa			xfer->fc = fwe->fd.fc;
373111942Ssimokawa			xfer->sc = (caddr_t)fwe;
374167632Ssimokawa			xfer->hand = fwe_output_callback;
375111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
376111942Ssimokawa		}
377103285Sikob	} else
378103285Sikob		xferq = fc->ir[fwe->dma_ch];
379103285Sikob
380103285Sikob
381103285Sikob	/* start dma */
382103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
383103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
384103285Sikob
385148887Srwatson#if defined(__FreeBSD__)
386148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
387148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
388148887Srwatson#else
389103285Sikob	ifp->if_flags |= IFF_RUNNING;
390103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
391148887Srwatson#endif
392103285Sikob
393103285Sikob#if 0
394103285Sikob	/* attempt to start output */
395103285Sikob	fwe_start(ifp);
396103285Sikob#endif
397103285Sikob}
398103285Sikob
399103285Sikob
400103285Sikobstatic int
401103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
402103285Sikob{
403103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
404103285Sikob	struct ifstat *ifs = NULL;
405103285Sikob	int s, error, len;
406103285Sikob
407103285Sikob	switch (cmd) {
408103285Sikob		case SIOCSIFFLAGS:
409103285Sikob			s = splimp();
410103285Sikob			if (ifp->if_flags & IFF_UP) {
411148887Srwatson#if defined(__FreeBSD__)
412148887Srwatson				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
413148887Srwatson#else
414103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
415148887Srwatson#endif
416103285Sikob					fwe_init(&fwe->eth_softc);
417103285Sikob			} else {
418148887Srwatson#if defined(__FreeBSD__)
419148887Srwatson				if (ifp->if_drv_flags & IFF_DRV_RUNNING)
420148887Srwatson#else
421103285Sikob				if (ifp->if_flags & IFF_RUNNING)
422148887Srwatson#endif
423103285Sikob					fwe_stop(fwe);
424103285Sikob			}
425103285Sikob			/* XXX keep promiscoud mode */
426103285Sikob			ifp->if_flags |= IFF_PROMISC;
427103285Sikob			splx(s);
428103285Sikob			break;
429103285Sikob		case SIOCADDMULTI:
430103285Sikob		case SIOCDELMULTI:
431108712Ssimokawa			break;
432103285Sikob
433103285Sikob		case SIOCGIFSTATUS:
434103285Sikob			s = splimp();
435103285Sikob			ifs = (struct ifstat *)data;
436103285Sikob			len = strlen(ifs->ascii);
437103285Sikob			if (len < sizeof(ifs->ascii))
438103285Sikob				snprintf(ifs->ascii + len,
439103285Sikob					sizeof(ifs->ascii) - len,
440103285Sikob					"\tch %d dma %d\n",
441103285Sikob						fwe->stream_ch, fwe->dma_ch);
442103285Sikob			splx(s);
443108712Ssimokawa			break;
444150789Sglebius		case SIOCSIFCAP:
445150789Sglebius#ifdef DEVICE_POLLING
446150789Sglebius		    {
447150789Sglebius			struct ifreq *ifr = (struct ifreq *) data;
448150789Sglebius			struct firewire_comm *fc = fc = fwe->fd.fc;
449150789Sglebius
450150789Sglebius			if (ifr->ifr_reqcap & IFCAP_POLLING &&
451150789Sglebius			    !(ifp->if_capenable & IFCAP_POLLING)) {
452150789Sglebius				error = ether_poll_register(fwe_poll, ifp);
453150789Sglebius				if (error)
454150789Sglebius					return(error);
455150789Sglebius				/* Disable interrupts */
456150789Sglebius				fc->set_intr(fc, 0);
457150789Sglebius				ifp->if_capenable |= IFCAP_POLLING;
458150789Sglebius				return (error);
459150789Sglebius			}
460150789Sglebius			if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
461150789Sglebius			    ifp->if_capenable & IFCAP_POLLING) {
462150789Sglebius				error = ether_poll_deregister(ifp);
463150789Sglebius				/* Enable interrupts. */
464150789Sglebius				fc->set_intr(fc, 1);
465150789Sglebius				ifp->if_capenable &= ~IFCAP_POLLING;
466150789Sglebius				return (error);
467150789Sglebius			}
468150789Sglebius		    }
469150789Sglebius#endif /* DEVICE_POLLING */
470150789Sglebius			break;
471127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
472103285Sikob		default:
473108712Ssimokawa#else
474108712Ssimokawa		case SIOCSIFADDR:
475108712Ssimokawa		case SIOCGIFADDR:
476108712Ssimokawa		case SIOCSIFMTU:
477108712Ssimokawa#endif
478106937Ssam			s = splimp();
479106937Ssam			error = ether_ioctl(ifp, cmd, data);
480106937Ssam			splx(s);
481106937Ssam			return (error);
482127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
483108712Ssimokawa		default:
484108712Ssimokawa			return (EINVAL);
485108712Ssimokawa#endif
486103285Sikob	}
487103285Sikob
488103285Sikob	return (0);
489103285Sikob}
490103285Sikob
491103285Sikobstatic void
492111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
493111942Ssimokawa{
494111942Ssimokawa	struct fwe_softc *fwe;
495111942Ssimokawa	struct ifnet *ifp;
496111942Ssimokawa	int s;
497111942Ssimokawa
498111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
499147256Sbrooks	ifp = fwe->eth_softc.ifp;
500111942Ssimokawa	/* XXX error check */
501122161Ssimokawa	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
502111942Ssimokawa	if (xfer->resp != 0)
503111942Ssimokawa		ifp->if_oerrors ++;
504111942Ssimokawa
505111942Ssimokawa	m_freem(xfer->mbuf);
506111942Ssimokawa	fw_xfer_unload(xfer);
507113584Ssimokawa
508111942Ssimokawa	s = splimp();
509170374Ssimokawa	FWE_LOCK(fwe);
510111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
511170374Ssimokawa	FWE_UNLOCK(fwe);
512111942Ssimokawa	splx(s);
513113584Ssimokawa
514113584Ssimokawa	/* for queue full */
515111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
516111942Ssimokawa		fwe_start(ifp);
517111942Ssimokawa}
518111942Ssimokawa
519111942Ssimokawastatic void
520103285Sikobfwe_start(struct ifnet *ifp)
521103285Sikob{
522103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
523103285Sikob	int s;
524103285Sikob
525122161Ssimokawa	FWEDEBUG(ifp, "starting\n");
526103285Sikob
527103285Sikob	if (fwe->dma_ch < 0) {
528103285Sikob		struct mbuf	*m = NULL;
529103285Sikob
530122161Ssimokawa		FWEDEBUG(ifp, "not ready\n");
531103285Sikob
532103285Sikob		s = splimp();
533103285Sikob		do {
534103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
535103285Sikob			if (m != NULL)
536103285Sikob				m_freem(m);
537103285Sikob			ifp->if_oerrors ++;
538103285Sikob		} while (m != NULL);
539103285Sikob		splx(s);
540103285Sikob
541103285Sikob		return;
542103285Sikob	}
543103285Sikob
544103285Sikob	s = splimp();
545148887Srwatson#if defined(__FreeBSD__)
546148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
547148887Srwatson#else
548103285Sikob	ifp->if_flags |= IFF_OACTIVE;
549148887Srwatson#endif
550103285Sikob
551103285Sikob	if (ifp->if_snd.ifq_len != 0)
552103285Sikob		fwe_as_output(fwe, ifp);
553103285Sikob
554148887Srwatson#if defined(__FreeBSD__)
555148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
556148887Srwatson#else
557103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
558148887Srwatson#endif
559103285Sikob	splx(s);
560103285Sikob}
561103285Sikob
562111942Ssimokawa#define HDR_LEN 4
563111942Ssimokawa#ifndef ETHER_ALIGN
564111942Ssimokawa#define ETHER_ALIGN 2
565103285Sikob#endif
566103285Sikob/* Async. stream output */
567103285Sikobstatic void
568103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
569103285Sikob{
570103285Sikob	struct mbuf *m;
571103285Sikob	struct fw_xfer *xfer;
572103285Sikob	struct fw_xferq *xferq;
573103285Sikob	struct fw_pkt *fp;
574103285Sikob	int i = 0;
575103285Sikob
576103285Sikob	xfer = NULL;
577103285Sikob	xferq = fwe->fd.fc->atq;
578170374Ssimokawa	while ((xferq->queued < xferq->maxq - 1) &&
579170374Ssimokawa			(ifp->if_snd.ifq_head != NULL)) {
580170374Ssimokawa		FWE_LOCK(fwe);
581111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
582111942Ssimokawa		if (xfer == NULL) {
583170374Ssimokawa#if 0
584111942Ssimokawa			printf("if_fwe: lack of xfer\n");
585170374Ssimokawa#endif
586170374Ssimokawa			FWE_UNLOCK(fwe);
587170374Ssimokawa			break;
588111942Ssimokawa		}
589170374Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
590170374Ssimokawa		FWE_UNLOCK(fwe);
591170374Ssimokawa
592103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
593170374Ssimokawa		if (m == NULL) {
594170374Ssimokawa			FWE_LOCK(fwe);
595170374Ssimokawa			STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link);
596170374Ssimokawa			FWE_UNLOCK(fwe);
597103285Sikob			break;
598170374Ssimokawa		}
599127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
600108712Ssimokawa		if (ifp->if_bpf != NULL)
601108712Ssimokawa			bpf_mtap(ifp, m);
602127468Ssimokawa#else
603127468Ssimokawa		BPF_MTAP(ifp, m);
604108712Ssimokawa#endif
605103285Sikob
606103285Sikob		/* keep ip packet alignment for alpha */
607111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
608120660Ssimokawa		fp = &xfer->send.hdr;
609129585Sdfr		*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
610113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
611103285Sikob		xfer->mbuf = m;
612120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
613103285Sikob
614111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
615103285Sikob			/* error */
616103285Sikob			ifp->if_oerrors ++;
617103285Sikob			/* XXX set error code */
618103285Sikob			fwe_output_callback(xfer);
619103285Sikob		} else {
620103285Sikob			ifp->if_opackets ++;
621111942Ssimokawa			i++;
622103285Sikob		}
623103285Sikob	}
624103285Sikob#if 0
625103285Sikob	if (i > 1)
626103285Sikob		printf("%d queued\n", i);
627103285Sikob#endif
628111942Ssimokawa	if (i > 0)
629111942Ssimokawa		xferq->start(fwe->fd.fc);
630103285Sikob}
631103285Sikob
632103285Sikob/* Async. stream output */
633103285Sikobstatic void
634103285Sikobfwe_as_input(struct fw_xferq *xferq)
635103285Sikob{
636113584Ssimokawa	struct mbuf *m, *m0;
637103285Sikob	struct ifnet *ifp;
638103285Sikob	struct fwe_softc *fwe;
639111942Ssimokawa	struct fw_bulkxfer *sxfer;
640111942Ssimokawa	struct fw_pkt *fp;
641103285Sikob	u_char *c;
642127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
643111942Ssimokawa	struct ether_header *eh;
644111942Ssimokawa#endif
645103285Sikob
646103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
647147256Sbrooks	ifp = fwe->eth_softc.ifp;
648150789Sglebius
649170374Ssimokawa	/* We do not need a lock here because the bottom half is serialized */
650111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
651111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
652113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
653111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
654111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
655111942Ssimokawa		m = sxfer->mbuf;
656111942Ssimokawa
657119119Ssimokawa		/* insert new rbuf */
658113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
659113584Ssimokawa		if (m0 != NULL) {
660113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
661113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
662113584Ssimokawa		} else
663170374Ssimokawa			printf("%s: m_getcl failed\n", __FUNCTION__);
664111942Ssimokawa
665119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
666119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
667119119Ssimokawa			m_freem(m);
668119119Ssimokawa			ifp->if_ierrors ++;
669119119Ssimokawa			continue;
670119119Ssimokawa		}
671119119Ssimokawa
672111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
673170374Ssimokawa		c = mtod(m, u_char *);
674127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
675111942Ssimokawa		eh = (struct ether_header *)c;
676111942Ssimokawa		m->m_data += sizeof(struct ether_header);
677132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
678132429Ssimokawa		    - sizeof(struct ether_header);
679132429Ssimokawa#else
680132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
681108712Ssimokawa#endif
682103285Sikob		m->m_pkthdr.rcvif = ifp;
683103285Sikob#if 0
684122161Ssimokawa		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
685103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
686103285Sikob			 "%02x %02x %02x %02x\n"
687103285Sikob			 "%02x %02x %02x %02x\n"
688103285Sikob			 "%02x %02x %02x %02x\n"
689103285Sikob			 "%02x %02x %02x %02x\n",
690103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
691103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
692103285Sikob			 c[12], c[13], c[14], c[15],
693103285Sikob			 c[16], c[17], c[18], c[19],
694103285Sikob			 c[20], c[21], c[22], c[23],
695103285Sikob			 c[20], c[21], c[22], c[23]
696103285Sikob		 );
697103285Sikob#endif
698127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
699127468Ssimokawa		ether_input(ifp, eh, m);
700127468Ssimokawa#else
701106937Ssam		(*ifp->if_input)(ifp, m);
702108712Ssimokawa#endif
703103285Sikob		ifp->if_ipackets ++;
704103285Sikob	}
705111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
706111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
707103285Sikob}
708103285Sikob
709103285Sikob
710103285Sikobstatic devclass_t fwe_devclass;
711103285Sikob
712103285Sikobstatic device_method_t fwe_methods[] = {
713103285Sikob	/* device interface */
714103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
715103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
716103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
717103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
718103285Sikob	{ 0, 0 }
719103285Sikob};
720103285Sikob
721103285Sikobstatic driver_t fwe_driver = {
722121953Ssimokawa        "fwe",
723103285Sikob	fwe_methods,
724103285Sikob	sizeof(struct fwe_softc),
725103285Sikob};
726103285Sikob
727103285Sikob
728127468Ssimokawa#ifdef __DragonFly__
729127468SsimokawaDECLARE_DUMMY_MODULE(fwe);
730127468Ssimokawa#endif
731113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
732113506SmdoddMODULE_VERSION(fwe, 1);
733113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
734