if_fwe.c revision 193096
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 193096 2009-05-30 15:14:44Z attilio $
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
108193096Sattiliostatic int
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))
115193096Sattilio		return (0);
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);
120193096Sattilio	return (0);
121103285Sikob}
122150789Sglebius#endif /* DEVICE_POLLING */
123150789Sglebius
124103285Sikobstatic void
125103285Sikobfwe_identify(driver_t *driver, device_t parent)
126103285Sikob{
127121953Ssimokawa	BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
128103285Sikob}
129103285Sikob
130103285Sikobstatic int
131103285Sikobfwe_probe(device_t dev)
132103285Sikob{
133103285Sikob	device_t pa;
134103285Sikob
135103285Sikob	pa = device_get_parent(dev);
136103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
137103285Sikob		return(ENXIO);
138103285Sikob	}
139103285Sikob
140108281Ssimokawa	device_set_desc(dev, "Ethernet over FireWire");
141103285Sikob	return (0);
142103285Sikob}
143103285Sikob
144103285Sikobstatic int
145103285Sikobfwe_attach(device_t dev)
146103285Sikob{
147103285Sikob	struct fwe_softc *fwe;
148103285Sikob	struct ifnet *ifp;
149103285Sikob	int unit, s;
150147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
151103285Sikob	u_char *eaddr;
152147256Sbrooks#else
153147256Sbrooks	u_char eaddr[6];
154147256Sbrooks#endif
155109814Ssimokawa	struct fw_eui64 *eui;
156103285Sikob
157103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
158103285Sikob	unit = device_get_unit(dev);
159103285Sikob
160103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
161170374Ssimokawa	mtx_init(&fwe->mtx, "fwe", NULL, MTX_DEF);
162103285Sikob	/* XXX */
163103285Sikob	fwe->stream_ch = stream_ch;
164103285Sikob	fwe->dma_ch = -1;
165103285Sikob
166103285Sikob	fwe->fd.fc = device_get_ivars(dev);
167124251Ssimokawa	if (tx_speed < 0)
168124251Ssimokawa		tx_speed = fwe->fd.fc->speed;
169124251Ssimokawa
170103285Sikob	fwe->fd.dev = dev;
171103285Sikob	fwe->fd.post_explore = NULL;
172103285Sikob	fwe->eth_softc.fwe = fwe;
173103285Sikob
174103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
175103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
176103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
177103285Sikob
178103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
179103285Sikob#define LOCAL (0x02)
180103285Sikob#define GROUP (0x01)
181147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
182147256Sbrooks	eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
183147256Sbrooks#endif
184109814Ssimokawa
185147256Sbrooks
186109814Ssimokawa	eui = &fwe->fd.fc->eui;
187109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
188109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
189109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
190109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
191109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
192109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
193107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
194107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
195103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
196103285Sikob
197103285Sikob	/* fill the rest and attach interface */
198147256Sbrooks	ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
199147256Sbrooks	if (ifp == NULL) {
200147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
201147256Sbrooks		return (ENOSPC);
202147256Sbrooks	}
203103285Sikob	ifp->if_softc = &fwe->eth_softc;
204103285Sikob
205127468Ssimokawa#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
206121953Ssimokawa	if_initname(ifp, device_get_name(dev), unit);
207122212Ssimokawa#else
208122212Ssimokawa	ifp->if_unit = unit;
209122212Ssimokawa	ifp->if_name = "fwe";
210122212Ssimokawa#endif
211103285Sikob	ifp->if_init = fwe_init;
212132430Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
213132430Ssimokawa	ifp->if_output = ether_output;
214132430Ssimokawa#endif
215103285Sikob	ifp->if_start = fwe_start;
216103285Sikob	ifp->if_ioctl = fwe_ioctl;
217103285Sikob	ifp->if_mtu = ETHERMTU;
218170374Ssimokawa	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
219111942Ssimokawa	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
220103285Sikob
221103285Sikob	s = splimp();
222127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
223127468Ssimokawa	ether_ifattach(ifp, 1);
224127468Ssimokawa#else
225106937Ssam	ether_ifattach(ifp, eaddr);
226108712Ssimokawa#endif
227103285Sikob	splx(s);
228103285Sikob
229103285Sikob        /* Tell the upper layer(s) we support long frames. */
230103285Sikob	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
231127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
232151229Sglebius	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_POLLING;
233129552Syar	ifp->if_capenable |= IFCAP_VLAN_MTU;
234108712Ssimokawa#endif
235103285Sikob
236103285Sikob
237122161Ssimokawa	FWEDEBUG(ifp, "interface created\n");
238103285Sikob	return 0;
239103285Sikob}
240103285Sikob
241103285Sikobstatic void
242103285Sikobfwe_stop(struct fwe_softc *fwe)
243103285Sikob{
244103285Sikob	struct firewire_comm *fc;
245103285Sikob	struct fw_xferq *xferq;
246147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
247111942Ssimokawa	struct fw_xfer *xfer, *next;
248111942Ssimokawa	int i;
249103285Sikob
250103285Sikob	fc = fwe->fd.fc;
251103285Sikob
252103285Sikob	if (fwe->dma_ch >= 0) {
253103285Sikob		xferq = fc->ir[fwe->dma_ch];
254103285Sikob
255103285Sikob		if (xferq->flag & FWXFERQ_RUNNING)
256103285Sikob			fc->irx_disable(fc, fwe->dma_ch);
257103285Sikob		xferq->flag &=
258113584Ssimokawa			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
259113584Ssimokawa			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
260111942Ssimokawa		xferq->hand =  NULL;
261111942Ssimokawa
262111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++)
263111942Ssimokawa			m_freem(xferq->bulkxfer[i].mbuf);
264111942Ssimokawa		free(xferq->bulkxfer, M_FWE);
265111942Ssimokawa
266111942Ssimokawa		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
267111942Ssimokawa					xfer = next) {
268111942Ssimokawa			next = STAILQ_NEXT(xfer, link);
269111942Ssimokawa			fw_xfer_free(xfer);
270111942Ssimokawa		}
271111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
272111942Ssimokawa
273111942Ssimokawa		xferq->bulkxfer =  NULL;
274103285Sikob		fwe->dma_ch = -1;
275103285Sikob	}
276103285Sikob
277148887Srwatson#if defined(__FreeBSD__)
278148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
279148887Srwatson#else
280103285Sikob	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
281148887Srwatson#endif
282103285Sikob}
283103285Sikob
284103285Sikobstatic int
285103285Sikobfwe_detach(device_t dev)
286103285Sikob{
287103285Sikob	struct fwe_softc *fwe;
288147256Sbrooks	struct ifnet *ifp;
289103285Sikob	int s;
290103285Sikob
291147256Sbrooks	fwe = device_get_softc(dev);
292147256Sbrooks	ifp = fwe->eth_softc.ifp;
293150789Sglebius
294150789Sglebius#ifdef DEVICE_POLLING
295150789Sglebius	if (ifp->if_capenable & IFCAP_POLLING)
296150789Sglebius		ether_poll_deregister(ifp);
297150789Sglebius#endif
298103285Sikob	s = splimp();
299103285Sikob
300103285Sikob	fwe_stop(fwe);
301127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
302147256Sbrooks	ether_ifdetach(ifp, 1);
303127468Ssimokawa#else
304147256Sbrooks	ether_ifdetach(ifp);
305147256Sbrooks	if_free(ifp);
306108712Ssimokawa#endif
307103285Sikob
308103285Sikob	splx(s);
309170374Ssimokawa	mtx_destroy(&fwe->mtx);
310103285Sikob	return 0;
311103285Sikob}
312103285Sikob
313103285Sikobstatic void
314103285Sikobfwe_init(void *arg)
315103285Sikob{
316103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
317103285Sikob	struct firewire_comm *fc;
318147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
319103285Sikob	struct fw_xferq *xferq;
320111942Ssimokawa	struct fw_xfer *xfer;
321113584Ssimokawa	struct mbuf *m;
322103285Sikob	int i;
323103285Sikob
324122161Ssimokawa	FWEDEBUG(ifp, "initializing\n");
325103285Sikob
326103285Sikob	/* XXX keep promiscoud mode */
327103285Sikob	ifp->if_flags |= IFF_PROMISC;
328103285Sikob
329103285Sikob	fc = fwe->fd.fc;
330103285Sikob	if (fwe->dma_ch < 0) {
331170374Ssimokawa		fwe->dma_ch = fw_open_isodma(fc, /* tx */0);
332170374Ssimokawa		if (fwe->dma_ch < 0)
333170374Ssimokawa			return;
334170374Ssimokawa		xferq = fc->ir[fwe->dma_ch];
335170374Ssimokawa		xferq->flag |= FWXFERQ_EXTBUF |
336170374Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
337103285Sikob		fwe->stream_ch = stream_ch;
338103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
339112400Ssimokawa		xferq->flag &= ~0xff;
340103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
341103285Sikob		/* register fwe_input handler */
342103285Sikob		xferq->sc = (caddr_t) fwe;
343103285Sikob		xferq->hand = fwe_as_input;
344122603Ssimokawa		xferq->bnchunk = rx_queue_len;
345111942Ssimokawa		xferq->bnpacket = 1;
346111942Ssimokawa		xferq->psize = MCLBYTES;
347111942Ssimokawa		xferq->queued = 0;
348113584Ssimokawa		xferq->buf = NULL;
349111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
350113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
351113584Ssimokawa							M_FWE, M_WAITOK);
352111942Ssimokawa		if (xferq->bulkxfer == NULL) {
353111942Ssimokawa			printf("if_fwe: malloc failed\n");
354111942Ssimokawa			return;
355111942Ssimokawa		}
356111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
357111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
358111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
359111942Ssimokawa		xferq->stproc = NULL;
360111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
361177599Sru			m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
362113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
363177599Sru			m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
364177599Sru			STAILQ_INSERT_TAIL(&xferq->stfree,
365177599Sru					&xferq->bulkxfer[i], link);
366111942Ssimokawa		}
367111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
368111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
369111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
370111942Ssimokawa			if (xfer == NULL)
371111942Ssimokawa				break;
372120660Ssimokawa			xfer->send.spd = tx_speed;
373111942Ssimokawa			xfer->fc = fwe->fd.fc;
374111942Ssimokawa			xfer->sc = (caddr_t)fwe;
375167632Ssimokawa			xfer->hand = fwe_output_callback;
376111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
377111942Ssimokawa		}
378103285Sikob	} else
379103285Sikob		xferq = fc->ir[fwe->dma_ch];
380103285Sikob
381103285Sikob
382103285Sikob	/* start dma */
383103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
384103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
385103285Sikob
386148887Srwatson#if defined(__FreeBSD__)
387148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
388148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
389148887Srwatson#else
390103285Sikob	ifp->if_flags |= IFF_RUNNING;
391103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
392148887Srwatson#endif
393103285Sikob
394103285Sikob#if 0
395103285Sikob	/* attempt to start output */
396103285Sikob	fwe_start(ifp);
397103285Sikob#endif
398103285Sikob}
399103285Sikob
400103285Sikob
401103285Sikobstatic int
402103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
403103285Sikob{
404103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
405103285Sikob	struct ifstat *ifs = NULL;
406103285Sikob	int s, error, len;
407103285Sikob
408103285Sikob	switch (cmd) {
409103285Sikob		case SIOCSIFFLAGS:
410103285Sikob			s = splimp();
411103285Sikob			if (ifp->if_flags & IFF_UP) {
412148887Srwatson#if defined(__FreeBSD__)
413148887Srwatson				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
414148887Srwatson#else
415103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
416148887Srwatson#endif
417103285Sikob					fwe_init(&fwe->eth_softc);
418103285Sikob			} else {
419148887Srwatson#if defined(__FreeBSD__)
420148887Srwatson				if (ifp->if_drv_flags & IFF_DRV_RUNNING)
421148887Srwatson#else
422103285Sikob				if (ifp->if_flags & IFF_RUNNING)
423148887Srwatson#endif
424103285Sikob					fwe_stop(fwe);
425103285Sikob			}
426103285Sikob			/* XXX keep promiscoud mode */
427103285Sikob			ifp->if_flags |= IFF_PROMISC;
428103285Sikob			splx(s);
429103285Sikob			break;
430103285Sikob		case SIOCADDMULTI:
431103285Sikob		case SIOCDELMULTI:
432108712Ssimokawa			break;
433103285Sikob
434103285Sikob		case SIOCGIFSTATUS:
435103285Sikob			s = splimp();
436103285Sikob			ifs = (struct ifstat *)data;
437103285Sikob			len = strlen(ifs->ascii);
438103285Sikob			if (len < sizeof(ifs->ascii))
439103285Sikob				snprintf(ifs->ascii + len,
440103285Sikob					sizeof(ifs->ascii) - len,
441103285Sikob					"\tch %d dma %d\n",
442103285Sikob						fwe->stream_ch, fwe->dma_ch);
443103285Sikob			splx(s);
444108712Ssimokawa			break;
445150789Sglebius		case SIOCSIFCAP:
446150789Sglebius#ifdef DEVICE_POLLING
447150789Sglebius		    {
448150789Sglebius			struct ifreq *ifr = (struct ifreq *) data;
449188394Sfjoe			struct firewire_comm *fc = fwe->fd.fc;
450150789Sglebius
451150789Sglebius			if (ifr->ifr_reqcap & IFCAP_POLLING &&
452150789Sglebius			    !(ifp->if_capenable & IFCAP_POLLING)) {
453150789Sglebius				error = ether_poll_register(fwe_poll, ifp);
454150789Sglebius				if (error)
455150789Sglebius					return(error);
456150789Sglebius				/* Disable interrupts */
457150789Sglebius				fc->set_intr(fc, 0);
458150789Sglebius				ifp->if_capenable |= IFCAP_POLLING;
459193096Sattilio				ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
460150789Sglebius				return (error);
461150789Sglebius			}
462150789Sglebius			if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
463150789Sglebius			    ifp->if_capenable & IFCAP_POLLING) {
464150789Sglebius				error = ether_poll_deregister(ifp);
465150789Sglebius				/* Enable interrupts. */
466150789Sglebius				fc->set_intr(fc, 1);
467150789Sglebius				ifp->if_capenable &= ~IFCAP_POLLING;
468193096Sattilio				ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT;
469150789Sglebius				return (error);
470150789Sglebius			}
471150789Sglebius		    }
472150789Sglebius#endif /* DEVICE_POLLING */
473150789Sglebius			break;
474127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
475103285Sikob		default:
476108712Ssimokawa#else
477108712Ssimokawa		case SIOCSIFADDR:
478108712Ssimokawa		case SIOCGIFADDR:
479108712Ssimokawa		case SIOCSIFMTU:
480108712Ssimokawa#endif
481106937Ssam			s = splimp();
482106937Ssam			error = ether_ioctl(ifp, cmd, data);
483106937Ssam			splx(s);
484106937Ssam			return (error);
485127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
486108712Ssimokawa		default:
487108712Ssimokawa			return (EINVAL);
488108712Ssimokawa#endif
489103285Sikob	}
490103285Sikob
491103285Sikob	return (0);
492103285Sikob}
493103285Sikob
494103285Sikobstatic void
495111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
496111942Ssimokawa{
497111942Ssimokawa	struct fwe_softc *fwe;
498111942Ssimokawa	struct ifnet *ifp;
499111942Ssimokawa	int s;
500111942Ssimokawa
501111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
502147256Sbrooks	ifp = fwe->eth_softc.ifp;
503111942Ssimokawa	/* XXX error check */
504122161Ssimokawa	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
505111942Ssimokawa	if (xfer->resp != 0)
506111942Ssimokawa		ifp->if_oerrors ++;
507111942Ssimokawa
508111942Ssimokawa	m_freem(xfer->mbuf);
509111942Ssimokawa	fw_xfer_unload(xfer);
510113584Ssimokawa
511111942Ssimokawa	s = splimp();
512170374Ssimokawa	FWE_LOCK(fwe);
513111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
514170374Ssimokawa	FWE_UNLOCK(fwe);
515111942Ssimokawa	splx(s);
516113584Ssimokawa
517113584Ssimokawa	/* for queue full */
518111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
519111942Ssimokawa		fwe_start(ifp);
520111942Ssimokawa}
521111942Ssimokawa
522111942Ssimokawastatic void
523103285Sikobfwe_start(struct ifnet *ifp)
524103285Sikob{
525103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
526103285Sikob	int s;
527103285Sikob
528122161Ssimokawa	FWEDEBUG(ifp, "starting\n");
529103285Sikob
530103285Sikob	if (fwe->dma_ch < 0) {
531103285Sikob		struct mbuf	*m = NULL;
532103285Sikob
533122161Ssimokawa		FWEDEBUG(ifp, "not ready\n");
534103285Sikob
535103285Sikob		s = splimp();
536103285Sikob		do {
537103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
538103285Sikob			if (m != NULL)
539103285Sikob				m_freem(m);
540103285Sikob			ifp->if_oerrors ++;
541103285Sikob		} while (m != NULL);
542103285Sikob		splx(s);
543103285Sikob
544103285Sikob		return;
545103285Sikob	}
546103285Sikob
547103285Sikob	s = splimp();
548148887Srwatson#if defined(__FreeBSD__)
549148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
550148887Srwatson#else
551103285Sikob	ifp->if_flags |= IFF_OACTIVE;
552148887Srwatson#endif
553103285Sikob
554103285Sikob	if (ifp->if_snd.ifq_len != 0)
555103285Sikob		fwe_as_output(fwe, ifp);
556103285Sikob
557148887Srwatson#if defined(__FreeBSD__)
558148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
559148887Srwatson#else
560103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
561148887Srwatson#endif
562103285Sikob	splx(s);
563103285Sikob}
564103285Sikob
565111942Ssimokawa#define HDR_LEN 4
566111942Ssimokawa#ifndef ETHER_ALIGN
567111942Ssimokawa#define ETHER_ALIGN 2
568103285Sikob#endif
569103285Sikob/* Async. stream output */
570103285Sikobstatic void
571103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
572103285Sikob{
573103285Sikob	struct mbuf *m;
574103285Sikob	struct fw_xfer *xfer;
575103285Sikob	struct fw_xferq *xferq;
576103285Sikob	struct fw_pkt *fp;
577103285Sikob	int i = 0;
578103285Sikob
579103285Sikob	xfer = NULL;
580103285Sikob	xferq = fwe->fd.fc->atq;
581170374Ssimokawa	while ((xferq->queued < xferq->maxq - 1) &&
582170374Ssimokawa			(ifp->if_snd.ifq_head != NULL)) {
583170374Ssimokawa		FWE_LOCK(fwe);
584111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
585111942Ssimokawa		if (xfer == NULL) {
586170374Ssimokawa#if 0
587111942Ssimokawa			printf("if_fwe: lack of xfer\n");
588170374Ssimokawa#endif
589170374Ssimokawa			FWE_UNLOCK(fwe);
590170374Ssimokawa			break;
591111942Ssimokawa		}
592170374Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
593170374Ssimokawa		FWE_UNLOCK(fwe);
594170374Ssimokawa
595103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
596170374Ssimokawa		if (m == NULL) {
597170374Ssimokawa			FWE_LOCK(fwe);
598170374Ssimokawa			STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link);
599170374Ssimokawa			FWE_UNLOCK(fwe);
600103285Sikob			break;
601170374Ssimokawa		}
602127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
603108712Ssimokawa		if (ifp->if_bpf != NULL)
604108712Ssimokawa			bpf_mtap(ifp, m);
605127468Ssimokawa#else
606127468Ssimokawa		BPF_MTAP(ifp, m);
607108712Ssimokawa#endif
608103285Sikob
609103285Sikob		/* keep ip packet alignment for alpha */
610111942Ssimokawa		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
611120660Ssimokawa		fp = &xfer->send.hdr;
612129585Sdfr		*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
613113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
614103285Sikob		xfer->mbuf = m;
615120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
616103285Sikob
617111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
618103285Sikob			/* error */
619103285Sikob			ifp->if_oerrors ++;
620103285Sikob			/* XXX set error code */
621103285Sikob			fwe_output_callback(xfer);
622103285Sikob		} else {
623103285Sikob			ifp->if_opackets ++;
624111942Ssimokawa			i++;
625103285Sikob		}
626103285Sikob	}
627103285Sikob#if 0
628103285Sikob	if (i > 1)
629103285Sikob		printf("%d queued\n", i);
630103285Sikob#endif
631111942Ssimokawa	if (i > 0)
632111942Ssimokawa		xferq->start(fwe->fd.fc);
633103285Sikob}
634103285Sikob
635103285Sikob/* Async. stream output */
636103285Sikobstatic void
637103285Sikobfwe_as_input(struct fw_xferq *xferq)
638103285Sikob{
639113584Ssimokawa	struct mbuf *m, *m0;
640103285Sikob	struct ifnet *ifp;
641103285Sikob	struct fwe_softc *fwe;
642111942Ssimokawa	struct fw_bulkxfer *sxfer;
643111942Ssimokawa	struct fw_pkt *fp;
644103285Sikob	u_char *c;
645127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
646111942Ssimokawa	struct ether_header *eh;
647111942Ssimokawa#endif
648103285Sikob
649103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
650147256Sbrooks	ifp = fwe->eth_softc.ifp;
651150789Sglebius
652170374Ssimokawa	/* We do not need a lock here because the bottom half is serialized */
653111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
654111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
655113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
656111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
657111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
658111942Ssimokawa		m = sxfer->mbuf;
659111942Ssimokawa
660119119Ssimokawa		/* insert new rbuf */
661113584Ssimokawa		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
662113584Ssimokawa		if (m0 != NULL) {
663113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
664113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
665113584Ssimokawa		} else
666170374Ssimokawa			printf("%s: m_getcl failed\n", __FUNCTION__);
667111942Ssimokawa
668119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
669119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
670119119Ssimokawa			m_freem(m);
671119119Ssimokawa			ifp->if_ierrors ++;
672119119Ssimokawa			continue;
673119119Ssimokawa		}
674119119Ssimokawa
675111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
676170374Ssimokawa		c = mtod(m, u_char *);
677127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
678111942Ssimokawa		eh = (struct ether_header *)c;
679111942Ssimokawa		m->m_data += sizeof(struct ether_header);
680132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
681132429Ssimokawa		    - sizeof(struct ether_header);
682132429Ssimokawa#else
683132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
684108712Ssimokawa#endif
685103285Sikob		m->m_pkthdr.rcvif = ifp;
686103285Sikob#if 0
687122161Ssimokawa		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
688103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
689103285Sikob			 "%02x %02x %02x %02x\n"
690103285Sikob			 "%02x %02x %02x %02x\n"
691103285Sikob			 "%02x %02x %02x %02x\n"
692103285Sikob			 "%02x %02x %02x %02x\n",
693103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
694103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
695103285Sikob			 c[12], c[13], c[14], c[15],
696103285Sikob			 c[16], c[17], c[18], c[19],
697103285Sikob			 c[20], c[21], c[22], c[23],
698103285Sikob			 c[20], c[21], c[22], c[23]
699103285Sikob		 );
700103285Sikob#endif
701127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
702127468Ssimokawa		ether_input(ifp, eh, m);
703127468Ssimokawa#else
704106937Ssam		(*ifp->if_input)(ifp, m);
705108712Ssimokawa#endif
706103285Sikob		ifp->if_ipackets ++;
707103285Sikob	}
708111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
709111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
710103285Sikob}
711103285Sikob
712103285Sikob
713103285Sikobstatic devclass_t fwe_devclass;
714103285Sikob
715103285Sikobstatic device_method_t fwe_methods[] = {
716103285Sikob	/* device interface */
717103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
718103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
719103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
720103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
721103285Sikob	{ 0, 0 }
722103285Sikob};
723103285Sikob
724103285Sikobstatic driver_t fwe_driver = {
725121953Ssimokawa        "fwe",
726103285Sikob	fwe_methods,
727103285Sikob	sizeof(struct fwe_softc),
728103285Sikob};
729103285Sikob
730103285Sikob
731127468Ssimokawa#ifdef __DragonFly__
732127468SsimokawaDECLARE_DUMMY_MODULE(fwe);
733127468Ssimokawa#endif
734113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
735113506SmdoddMODULE_VERSION(fwe, 1);
736113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
737