if_fwe.c revision 267992
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 267992 2014-06-28 03:56:17Z hselasky $
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>
57257176Sglebius#include <net/if_var.h>
58103285Sikob#include <net/if_arp.h>
59147256Sbrooks#include <net/if_types.h>
60127468Ssimokawa#ifdef __DragonFly__
61127468Ssimokawa#include <net/vlan/if_vlan_var.h>
62127468Ssimokawa#include <bus/firewire/firewire.h>
63127468Ssimokawa#include <bus/firewire/firewirereg.h>
64127468Ssimokawa#include "if_fwevar.h"
65127468Ssimokawa#else
66103285Sikob#include <net/if_vlan_var.h>
67103285Sikob
68103285Sikob#include <dev/firewire/firewire.h>
69103285Sikob#include <dev/firewire/firewirereg.h>
70103285Sikob#include <dev/firewire/if_fwevar.h>
71127468Ssimokawa#endif
72103285Sikob
73122161Ssimokawa#define FWEDEBUG	if (fwedebug) if_printf
74111942Ssimokawa#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
75103285Sikob
76103285Sikob/* network interface */
77124169Ssimokawastatic void fwe_start (struct ifnet *);
78124169Ssimokawastatic int fwe_ioctl (struct ifnet *, u_long, caddr_t);
79124169Ssimokawastatic void fwe_init (void *);
80103285Sikob
81124169Ssimokawastatic void fwe_output_callback (struct fw_xfer *);
82124169Ssimokawastatic void fwe_as_output (struct fwe_softc *, struct ifnet *);
83124169Ssimokawastatic void fwe_as_input (struct fw_xferq *);
84103285Sikob
85103285Sikobstatic int fwedebug = 0;
86103285Sikobstatic int stream_ch = 1;
87116139Ssimokawastatic int tx_speed = 2;
88122603Ssimokawastatic int rx_queue_len = FWMAXQUEUE;
89103285Sikob
90227293Sedstatic MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
91267992ShselaskySYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RWTUN, &fwedebug, 0, "");
92103285SikobSYSCTL_DECL(_hw_firewire);
93227309Sedstatic SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
94122603Ssimokawa	"Ethernet emulation subsystem");
95267992ShselaskySYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RWTUN, &stream_ch, 0,
96103285Sikob	"Stream channel to use");
97267992ShselaskySYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RWTUN, &tx_speed, 0,
98122603Ssimokawa	"Transmission speed");
99267992ShselaskySYSCTL_INT(_hw_firewire_fwe, OID_AUTO, rx_queue_len, CTLFLAG_RWTUN, &rx_queue_len,
100122603Ssimokawa	0, "Length of the receive queue");
101103285Sikob
102103285Sikob#ifdef DEVICE_POLLING
103103285Sikobstatic poll_handler_t fwe_poll;
104103285Sikob
105193096Sattiliostatic int
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))
112193096Sattilio		return (0);
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);
117193096Sattilio	return (0);
118103285Sikob}
119150789Sglebius#endif /* DEVICE_POLLING */
120150789Sglebius
121103285Sikobstatic void
122103285Sikobfwe_identify(driver_t *driver, device_t parent)
123103285Sikob{
124121953Ssimokawa	BUS_ADD_CHILD(parent, 0, "fwe", device_get_unit(parent));
125103285Sikob}
126103285Sikob
127103285Sikobstatic int
128103285Sikobfwe_probe(device_t dev)
129103285Sikob{
130103285Sikob	device_t pa;
131103285Sikob
132103285Sikob	pa = device_get_parent(dev);
133103285Sikob	if(device_get_unit(dev) != device_get_unit(pa)){
134103285Sikob		return(ENXIO);
135103285Sikob	}
136103285Sikob
137108281Ssimokawa	device_set_desc(dev, "Ethernet over FireWire");
138103285Sikob	return (0);
139103285Sikob}
140103285Sikob
141103285Sikobstatic int
142103285Sikobfwe_attach(device_t dev)
143103285Sikob{
144103285Sikob	struct fwe_softc *fwe;
145103285Sikob	struct ifnet *ifp;
146103285Sikob	int unit, s;
147147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
148103285Sikob	u_char *eaddr;
149147256Sbrooks#else
150147256Sbrooks	u_char eaddr[6];
151147256Sbrooks#endif
152109814Ssimokawa	struct fw_eui64 *eui;
153103285Sikob
154103285Sikob	fwe = ((struct fwe_softc *)device_get_softc(dev));
155103285Sikob	unit = device_get_unit(dev);
156103285Sikob
157103285Sikob	bzero(fwe, sizeof(struct fwe_softc));
158170374Ssimokawa	mtx_init(&fwe->mtx, "fwe", NULL, MTX_DEF);
159103285Sikob	/* XXX */
160103285Sikob	fwe->stream_ch = stream_ch;
161103285Sikob	fwe->dma_ch = -1;
162103285Sikob
163103285Sikob	fwe->fd.fc = device_get_ivars(dev);
164124251Ssimokawa	if (tx_speed < 0)
165124251Ssimokawa		tx_speed = fwe->fd.fc->speed;
166124251Ssimokawa
167103285Sikob	fwe->fd.dev = dev;
168103285Sikob	fwe->fd.post_explore = NULL;
169103285Sikob	fwe->eth_softc.fwe = fwe;
170103285Sikob
171103285Sikob	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
172103285Sikob	fwe->pkt_hdr.mode.stream.sy = 0;
173103285Sikob	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
174103285Sikob
175103285Sikob	/* generate fake MAC address: first and last 3bytes from eui64 */
176103285Sikob#define LOCAL (0x02)
177103285Sikob#define GROUP (0x01)
178147256Sbrooks#if defined(__DragonFly__) || __FreeBSD_version < 500000
179147256Sbrooks	eaddr = &IFP2ENADDR(fwe->eth_softc.ifp)[0];
180147256Sbrooks#endif
181109814Ssimokawa
182147256Sbrooks
183109814Ssimokawa	eui = &fwe->fd.fc->eui;
184109814Ssimokawa	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
185109814Ssimokawa	eaddr[1] = FW_EUI64_BYTE(eui, 1);
186109814Ssimokawa	eaddr[2] = FW_EUI64_BYTE(eui, 2);
187109814Ssimokawa	eaddr[3] = FW_EUI64_BYTE(eui, 5);
188109814Ssimokawa	eaddr[4] = FW_EUI64_BYTE(eui, 6);
189109814Ssimokawa	eaddr[5] = FW_EUI64_BYTE(eui, 7);
190107653Ssimokawa	printf("if_fwe%d: Fake Ethernet address: "
191107653Ssimokawa		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
192103285Sikob		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
193103285Sikob
194103285Sikob	/* fill the rest and attach interface */
195147256Sbrooks	ifp = fwe->eth_softc.ifp = if_alloc(IFT_ETHER);
196147256Sbrooks	if (ifp == NULL) {
197147256Sbrooks		device_printf(dev, "can not if_alloc()\n");
198147256Sbrooks		return (ENOSPC);
199147256Sbrooks	}
200103285Sikob	ifp->if_softc = &fwe->eth_softc;
201103285Sikob
202127468Ssimokawa#if __FreeBSD_version >= 501113 || defined(__DragonFly__)
203121953Ssimokawa	if_initname(ifp, device_get_name(dev), unit);
204122212Ssimokawa#else
205122212Ssimokawa	ifp->if_unit = unit;
206122212Ssimokawa	ifp->if_name = "fwe";
207122212Ssimokawa#endif
208103285Sikob	ifp->if_init = fwe_init;
209132430Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
210132430Ssimokawa	ifp->if_output = ether_output;
211132430Ssimokawa#endif
212103285Sikob	ifp->if_start = fwe_start;
213103285Sikob	ifp->if_ioctl = fwe_ioctl;
214170374Ssimokawa	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
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
228151229Sglebius	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);
305170374Ssimokawa	mtx_destroy(&fwe->mtx);
306103285Sikob	return 0;
307103285Sikob}
308103285Sikob
309103285Sikobstatic void
310103285Sikobfwe_init(void *arg)
311103285Sikob{
312103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
313103285Sikob	struct firewire_comm *fc;
314147256Sbrooks	struct ifnet *ifp = fwe->eth_softc.ifp;
315103285Sikob	struct fw_xferq *xferq;
316111942Ssimokawa	struct fw_xfer *xfer;
317113584Ssimokawa	struct mbuf *m;
318103285Sikob	int i;
319103285Sikob
320122161Ssimokawa	FWEDEBUG(ifp, "initializing\n");
321103285Sikob
322103285Sikob	/* XXX keep promiscoud mode */
323103285Sikob	ifp->if_flags |= IFF_PROMISC;
324103285Sikob
325103285Sikob	fc = fwe->fd.fc;
326103285Sikob	if (fwe->dma_ch < 0) {
327170374Ssimokawa		fwe->dma_ch = fw_open_isodma(fc, /* tx */0);
328170374Ssimokawa		if (fwe->dma_ch < 0)
329170374Ssimokawa			return;
330170374Ssimokawa		xferq = fc->ir[fwe->dma_ch];
331170374Ssimokawa		xferq->flag |= FWXFERQ_EXTBUF |
332170374Ssimokawa				FWXFERQ_HANDLER | FWXFERQ_STREAM;
333103285Sikob		fwe->stream_ch = stream_ch;
334103285Sikob		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
335112400Ssimokawa		xferq->flag &= ~0xff;
336103285Sikob		xferq->flag |= fwe->stream_ch & 0xff;
337103285Sikob		/* register fwe_input handler */
338103285Sikob		xferq->sc = (caddr_t) fwe;
339103285Sikob		xferq->hand = fwe_as_input;
340122603Ssimokawa		xferq->bnchunk = rx_queue_len;
341111942Ssimokawa		xferq->bnpacket = 1;
342111942Ssimokawa		xferq->psize = MCLBYTES;
343111942Ssimokawa		xferq->queued = 0;
344113584Ssimokawa		xferq->buf = NULL;
345111942Ssimokawa		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
346113584Ssimokawa			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
347113584Ssimokawa							M_FWE, M_WAITOK);
348111942Ssimokawa		if (xferq->bulkxfer == NULL) {
349111942Ssimokawa			printf("if_fwe: malloc failed\n");
350111942Ssimokawa			return;
351111942Ssimokawa		}
352111942Ssimokawa		STAILQ_INIT(&xferq->stvalid);
353111942Ssimokawa		STAILQ_INIT(&xferq->stfree);
354111942Ssimokawa		STAILQ_INIT(&xferq->stdma);
355111942Ssimokawa		xferq->stproc = NULL;
356111942Ssimokawa		for (i = 0; i < xferq->bnchunk; i ++) {
357243857Sglebius			m = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR);
358113584Ssimokawa			xferq->bulkxfer[i].mbuf = m;
359177599Sru			m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
360177599Sru			STAILQ_INSERT_TAIL(&xferq->stfree,
361177599Sru					&xferq->bulkxfer[i], link);
362111942Ssimokawa		}
363111942Ssimokawa		STAILQ_INIT(&fwe->xferlist);
364111942Ssimokawa		for (i = 0; i < TX_MAX_QUEUE; i++) {
365111942Ssimokawa			xfer = fw_xfer_alloc(M_FWE);
366111942Ssimokawa			if (xfer == NULL)
367111942Ssimokawa				break;
368120660Ssimokawa			xfer->send.spd = tx_speed;
369111942Ssimokawa			xfer->fc = fwe->fd.fc;
370111942Ssimokawa			xfer->sc = (caddr_t)fwe;
371167632Ssimokawa			xfer->hand = fwe_output_callback;
372111942Ssimokawa			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
373111942Ssimokawa		}
374103285Sikob	} else
375103285Sikob		xferq = fc->ir[fwe->dma_ch];
376103285Sikob
377103285Sikob
378103285Sikob	/* start dma */
379103285Sikob	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
380103285Sikob		fc->irx_enable(fc, fwe->dma_ch);
381103285Sikob
382148887Srwatson#if defined(__FreeBSD__)
383148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
384148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
385148887Srwatson#else
386103285Sikob	ifp->if_flags |= IFF_RUNNING;
387103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
388148887Srwatson#endif
389103285Sikob
390103285Sikob#if 0
391103285Sikob	/* attempt to start output */
392103285Sikob	fwe_start(ifp);
393103285Sikob#endif
394103285Sikob}
395103285Sikob
396103285Sikob
397103285Sikobstatic int
398103285Sikobfwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
399103285Sikob{
400103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
401103285Sikob	struct ifstat *ifs = NULL;
402260394Smelifaro	int s, error;
403103285Sikob
404103285Sikob	switch (cmd) {
405103285Sikob		case SIOCSIFFLAGS:
406103285Sikob			s = splimp();
407103285Sikob			if (ifp->if_flags & IFF_UP) {
408148887Srwatson#if defined(__FreeBSD__)
409148887Srwatson				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
410148887Srwatson#else
411103285Sikob				if (!(ifp->if_flags & IFF_RUNNING))
412148887Srwatson#endif
413103285Sikob					fwe_init(&fwe->eth_softc);
414103285Sikob			} else {
415148887Srwatson#if defined(__FreeBSD__)
416148887Srwatson				if (ifp->if_drv_flags & IFF_DRV_RUNNING)
417148887Srwatson#else
418103285Sikob				if (ifp->if_flags & IFF_RUNNING)
419148887Srwatson#endif
420103285Sikob					fwe_stop(fwe);
421103285Sikob			}
422103285Sikob			/* XXX keep promiscoud mode */
423103285Sikob			ifp->if_flags |= IFF_PROMISC;
424103285Sikob			splx(s);
425103285Sikob			break;
426103285Sikob		case SIOCADDMULTI:
427103285Sikob		case SIOCDELMULTI:
428108712Ssimokawa			break;
429103285Sikob
430103285Sikob		case SIOCGIFSTATUS:
431103285Sikob			s = splimp();
432103285Sikob			ifs = (struct ifstat *)data;
433260394Smelifaro			snprintf(ifs->ascii, sizeof(ifs->ascii),
434260394Smelifaro			    "\tch %d dma %d\n",	fwe->stream_ch, fwe->dma_ch);
435103285Sikob			splx(s);
436108712Ssimokawa			break;
437150789Sglebius		case SIOCSIFCAP:
438150789Sglebius#ifdef DEVICE_POLLING
439150789Sglebius		    {
440150789Sglebius			struct ifreq *ifr = (struct ifreq *) data;
441188394Sfjoe			struct firewire_comm *fc = fwe->fd.fc;
442150789Sglebius
443150789Sglebius			if (ifr->ifr_reqcap & IFCAP_POLLING &&
444150789Sglebius			    !(ifp->if_capenable & IFCAP_POLLING)) {
445150789Sglebius				error = ether_poll_register(fwe_poll, ifp);
446150789Sglebius				if (error)
447150789Sglebius					return(error);
448150789Sglebius				/* Disable interrupts */
449150789Sglebius				fc->set_intr(fc, 0);
450150789Sglebius				ifp->if_capenable |= IFCAP_POLLING;
451193096Sattilio				ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
452150789Sglebius				return (error);
453150789Sglebius			}
454150789Sglebius			if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
455150789Sglebius			    ifp->if_capenable & IFCAP_POLLING) {
456150789Sglebius				error = ether_poll_deregister(ifp);
457150789Sglebius				/* Enable interrupts. */
458150789Sglebius				fc->set_intr(fc, 1);
459150789Sglebius				ifp->if_capenable &= ~IFCAP_POLLING;
460193096Sattilio				ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT;
461150789Sglebius				return (error);
462150789Sglebius			}
463150789Sglebius		    }
464150789Sglebius#endif /* DEVICE_POLLING */
465150789Sglebius			break;
466127468Ssimokawa#if defined(__FreeBSD__) && __FreeBSD_version >= 500000
467103285Sikob		default:
468108712Ssimokawa#else
469108712Ssimokawa		case SIOCSIFADDR:
470108712Ssimokawa		case SIOCGIFADDR:
471108712Ssimokawa		case SIOCSIFMTU:
472108712Ssimokawa#endif
473106937Ssam			s = splimp();
474106937Ssam			error = ether_ioctl(ifp, cmd, data);
475106937Ssam			splx(s);
476106937Ssam			return (error);
477127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
478108712Ssimokawa		default:
479108712Ssimokawa			return (EINVAL);
480108712Ssimokawa#endif
481103285Sikob	}
482103285Sikob
483103285Sikob	return (0);
484103285Sikob}
485103285Sikob
486103285Sikobstatic void
487111942Ssimokawafwe_output_callback(struct fw_xfer *xfer)
488111942Ssimokawa{
489111942Ssimokawa	struct fwe_softc *fwe;
490111942Ssimokawa	struct ifnet *ifp;
491111942Ssimokawa	int s;
492111942Ssimokawa
493111942Ssimokawa	fwe = (struct fwe_softc *)xfer->sc;
494147256Sbrooks	ifp = fwe->eth_softc.ifp;
495111942Ssimokawa	/* XXX error check */
496122161Ssimokawa	FWEDEBUG(ifp, "resp = %d\n", xfer->resp);
497111942Ssimokawa	if (xfer->resp != 0)
498111942Ssimokawa		ifp->if_oerrors ++;
499111942Ssimokawa
500111942Ssimokawa	m_freem(xfer->mbuf);
501111942Ssimokawa	fw_xfer_unload(xfer);
502113584Ssimokawa
503111942Ssimokawa	s = splimp();
504170374Ssimokawa	FWE_LOCK(fwe);
505111942Ssimokawa	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
506170374Ssimokawa	FWE_UNLOCK(fwe);
507111942Ssimokawa	splx(s);
508113584Ssimokawa
509113584Ssimokawa	/* for queue full */
510111942Ssimokawa	if (ifp->if_snd.ifq_head != NULL)
511111942Ssimokawa		fwe_start(ifp);
512111942Ssimokawa}
513111942Ssimokawa
514111942Ssimokawastatic void
515103285Sikobfwe_start(struct ifnet *ifp)
516103285Sikob{
517103285Sikob	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
518103285Sikob	int s;
519103285Sikob
520122161Ssimokawa	FWEDEBUG(ifp, "starting\n");
521103285Sikob
522103285Sikob	if (fwe->dma_ch < 0) {
523103285Sikob		struct mbuf	*m = NULL;
524103285Sikob
525122161Ssimokawa		FWEDEBUG(ifp, "not ready\n");
526103285Sikob
527103285Sikob		s = splimp();
528103285Sikob		do {
529103285Sikob			IF_DEQUEUE(&ifp->if_snd, m);
530103285Sikob			if (m != NULL)
531103285Sikob				m_freem(m);
532103285Sikob			ifp->if_oerrors ++;
533103285Sikob		} while (m != NULL);
534103285Sikob		splx(s);
535103285Sikob
536103285Sikob		return;
537103285Sikob	}
538103285Sikob
539103285Sikob	s = splimp();
540148887Srwatson#if defined(__FreeBSD__)
541148887Srwatson	ifp->if_drv_flags |= IFF_DRV_OACTIVE;
542148887Srwatson#else
543103285Sikob	ifp->if_flags |= IFF_OACTIVE;
544148887Srwatson#endif
545103285Sikob
546103285Sikob	if (ifp->if_snd.ifq_len != 0)
547103285Sikob		fwe_as_output(fwe, ifp);
548103285Sikob
549148887Srwatson#if defined(__FreeBSD__)
550148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
551148887Srwatson#else
552103285Sikob	ifp->if_flags &= ~IFF_OACTIVE;
553148887Srwatson#endif
554103285Sikob	splx(s);
555103285Sikob}
556103285Sikob
557111942Ssimokawa#define HDR_LEN 4
558111942Ssimokawa#ifndef ETHER_ALIGN
559111942Ssimokawa#define ETHER_ALIGN 2
560103285Sikob#endif
561103285Sikob/* Async. stream output */
562103285Sikobstatic void
563103285Sikobfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
564103285Sikob{
565103285Sikob	struct mbuf *m;
566103285Sikob	struct fw_xfer *xfer;
567103285Sikob	struct fw_xferq *xferq;
568103285Sikob	struct fw_pkt *fp;
569103285Sikob	int i = 0;
570103285Sikob
571103285Sikob	xfer = NULL;
572103285Sikob	xferq = fwe->fd.fc->atq;
573170374Ssimokawa	while ((xferq->queued < xferq->maxq - 1) &&
574170374Ssimokawa			(ifp->if_snd.ifq_head != NULL)) {
575170374Ssimokawa		FWE_LOCK(fwe);
576111942Ssimokawa		xfer = STAILQ_FIRST(&fwe->xferlist);
577111942Ssimokawa		if (xfer == NULL) {
578170374Ssimokawa#if 0
579111942Ssimokawa			printf("if_fwe: lack of xfer\n");
580170374Ssimokawa#endif
581170374Ssimokawa			FWE_UNLOCK(fwe);
582170374Ssimokawa			break;
583111942Ssimokawa		}
584170374Ssimokawa		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
585170374Ssimokawa		FWE_UNLOCK(fwe);
586170374Ssimokawa
587103285Sikob		IF_DEQUEUE(&ifp->if_snd, m);
588170374Ssimokawa		if (m == NULL) {
589170374Ssimokawa			FWE_LOCK(fwe);
590170374Ssimokawa			STAILQ_INSERT_HEAD(&fwe->xferlist, xfer, link);
591170374Ssimokawa			FWE_UNLOCK(fwe);
592103285Sikob			break;
593170374Ssimokawa		}
594127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
595108712Ssimokawa		if (ifp->if_bpf != NULL)
596108712Ssimokawa			bpf_mtap(ifp, m);
597127468Ssimokawa#else
598127468Ssimokawa		BPF_MTAP(ifp, m);
599108712Ssimokawa#endif
600103285Sikob
601103285Sikob		/* keep ip packet alignment for alpha */
602243857Sglebius		M_PREPEND(m, ETHER_ALIGN, M_NOWAIT);
603120660Ssimokawa		fp = &xfer->send.hdr;
604129585Sdfr		*(uint32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
605113584Ssimokawa		fp->mode.stream.len = m->m_pkthdr.len;
606103285Sikob		xfer->mbuf = m;
607120660Ssimokawa		xfer->send.pay_len = m->m_pkthdr.len;
608103285Sikob
609111942Ssimokawa		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
610103285Sikob			/* error */
611103285Sikob			ifp->if_oerrors ++;
612103285Sikob			/* XXX set error code */
613103285Sikob			fwe_output_callback(xfer);
614103285Sikob		} else {
615103285Sikob			ifp->if_opackets ++;
616111942Ssimokawa			i++;
617103285Sikob		}
618103285Sikob	}
619103285Sikob#if 0
620103285Sikob	if (i > 1)
621103285Sikob		printf("%d queued\n", i);
622103285Sikob#endif
623111942Ssimokawa	if (i > 0)
624111942Ssimokawa		xferq->start(fwe->fd.fc);
625103285Sikob}
626103285Sikob
627103285Sikob/* Async. stream output */
628103285Sikobstatic void
629103285Sikobfwe_as_input(struct fw_xferq *xferq)
630103285Sikob{
631113584Ssimokawa	struct mbuf *m, *m0;
632103285Sikob	struct ifnet *ifp;
633103285Sikob	struct fwe_softc *fwe;
634111942Ssimokawa	struct fw_bulkxfer *sxfer;
635111942Ssimokawa	struct fw_pkt *fp;
636103285Sikob	u_char *c;
637127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
638111942Ssimokawa	struct ether_header *eh;
639111942Ssimokawa#endif
640103285Sikob
641103285Sikob	fwe = (struct fwe_softc *)xferq->sc;
642147256Sbrooks	ifp = fwe->eth_softc.ifp;
643150789Sglebius
644170374Ssimokawa	/* We do not need a lock here because the bottom half is serialized */
645111942Ssimokawa	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
646111942Ssimokawa		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
647113584Ssimokawa		fp = mtod(sxfer->mbuf, struct fw_pkt *);
648111942Ssimokawa		if (fwe->fd.fc->irx_post != NULL)
649111942Ssimokawa			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
650111942Ssimokawa		m = sxfer->mbuf;
651111942Ssimokawa
652119119Ssimokawa		/* insert new rbuf */
653243857Sglebius		sxfer->mbuf = m0 = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
654113584Ssimokawa		if (m0 != NULL) {
655113584Ssimokawa			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
656113584Ssimokawa			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
657113584Ssimokawa		} else
658170374Ssimokawa			printf("%s: m_getcl failed\n", __FUNCTION__);
659111942Ssimokawa
660119119Ssimokawa		if (sxfer->resp != 0 || fp->mode.stream.len <
661119119Ssimokawa		    ETHER_ALIGN + sizeof(struct ether_header)) {
662119119Ssimokawa			m_freem(m);
663119119Ssimokawa			ifp->if_ierrors ++;
664119119Ssimokawa			continue;
665119119Ssimokawa		}
666119119Ssimokawa
667111942Ssimokawa		m->m_data += HDR_LEN + ETHER_ALIGN;
668170374Ssimokawa		c = mtod(m, u_char *);
669127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
670111942Ssimokawa		eh = (struct ether_header *)c;
671111942Ssimokawa		m->m_data += sizeof(struct ether_header);
672132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN
673132429Ssimokawa		    - sizeof(struct ether_header);
674132429Ssimokawa#else
675132429Ssimokawa		m->m_len = m->m_pkthdr.len = fp->mode.stream.len - ETHER_ALIGN;
676108712Ssimokawa#endif
677103285Sikob		m->m_pkthdr.rcvif = ifp;
678103285Sikob#if 0
679122161Ssimokawa		FWEDEBUG(ifp, "%02x %02x %02x %02x %02x %02x\n"
680103285Sikob			 "%02x %02x %02x %02x %02x %02x\n"
681103285Sikob			 "%02x %02x %02x %02x\n"
682103285Sikob			 "%02x %02x %02x %02x\n"
683103285Sikob			 "%02x %02x %02x %02x\n"
684103285Sikob			 "%02x %02x %02x %02x\n",
685103285Sikob			 c[0], c[1], c[2], c[3], c[4], c[5],
686103285Sikob			 c[6], c[7], c[8], c[9], c[10], c[11],
687103285Sikob			 c[12], c[13], c[14], c[15],
688103285Sikob			 c[16], c[17], c[18], c[19],
689103285Sikob			 c[20], c[21], c[22], c[23],
690103285Sikob			 c[20], c[21], c[22], c[23]
691103285Sikob		 );
692103285Sikob#endif
693127468Ssimokawa#if defined(__DragonFly__) || __FreeBSD_version < 500000
694127468Ssimokawa		ether_input(ifp, eh, m);
695127468Ssimokawa#else
696106937Ssam		(*ifp->if_input)(ifp, m);
697108712Ssimokawa#endif
698103285Sikob		ifp->if_ipackets ++;
699103285Sikob	}
700111942Ssimokawa	if (STAILQ_FIRST(&xferq->stfree) != NULL)
701111942Ssimokawa		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
702103285Sikob}
703103285Sikob
704103285Sikob
705103285Sikobstatic devclass_t fwe_devclass;
706103285Sikob
707103285Sikobstatic device_method_t fwe_methods[] = {
708103285Sikob	/* device interface */
709103285Sikob	DEVMETHOD(device_identify,	fwe_identify),
710103285Sikob	DEVMETHOD(device_probe,		fwe_probe),
711103285Sikob	DEVMETHOD(device_attach,	fwe_attach),
712103285Sikob	DEVMETHOD(device_detach,	fwe_detach),
713103285Sikob	{ 0, 0 }
714103285Sikob};
715103285Sikob
716103285Sikobstatic driver_t fwe_driver = {
717121953Ssimokawa        "fwe",
718103285Sikob	fwe_methods,
719103285Sikob	sizeof(struct fwe_softc),
720103285Sikob};
721103285Sikob
722103285Sikob
723127468Ssimokawa#ifdef __DragonFly__
724127468SsimokawaDECLARE_DUMMY_MODULE(fwe);
725127468Ssimokawa#endif
726113506SmdoddDRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
727113506SmdoddMODULE_VERSION(fwe, 1);
728113506SmdoddMODULE_DEPEND(fwe, firewire, 1, 1, 1);
729