if_fwe.c revision 121816
1254721Semaste/*
2254721Semaste * Copyright (c) 2002-2003
3353358Sdim * 	Hidetoshi Shimokawa. All rights reserved.
4353358Sdim *
5353358Sdim * Redistribution and use in source and binary forms, with or without
6254721Semaste * modification, are permitted provided that the following conditions
7254721Semaste * are met:
8254721Semaste * 1. Redistributions of source code must retain the above copyright
9254721Semaste *    notice, this list of conditions and the following disclaimer.
10254721Semaste * 2. Redistributions in binary form must reproduce the above copyright
11254721Semaste *    notice, this list of conditions and the following disclaimer in the
12254721Semaste *    documentation and/or other materials provided with the distribution.
13254721Semaste * 3. All advertising materials mentioning features or use of this software
14341825Sdim *    must display the following acknowledgement:
15341825Sdim *
16341825Sdim *	This product includes software developed by Hidetoshi Shimokawa.
17258054Semaste *
18258054Semaste * 4. Neither the name of the author nor the names of its contributors
19258054Semaste *    may be used to endorse or promote products derived from this software
20258054Semaste *    without specific prior written permission.
21258054Semaste *
22258054Semaste * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23321369Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24321369Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25296417Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26254721Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27314564Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28254721Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29314564Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30254721Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32254721Semaste * SUCH DAMAGE.
33254721Semaste *
34254721Semaste * $FreeBSD: head/sys/dev/firewire/if_fwe.c 121816 2003-10-31 18:32:15Z brooks $
35254721Semaste */
36254721Semaste
37314564Sdim#include "opt_inet.h"
38353358Sdim
39353358Sdim#include <sys/param.h>
40314564Sdim#include <sys/kernel.h>
41296417Sdim#include <sys/malloc.h>
42314564Sdim#include <sys/mbuf.h>
43296417Sdim#include <sys/socket.h>
44314564Sdim#include <sys/sockio.h>
45314564Sdim#include <sys/sysctl.h>
46314564Sdim#include <sys/systm.h>
47314564Sdim#include <sys/module.h>
48254721Semaste#include <sys/bus.h>
49258054Semaste#include <machine/bus.h>
50314564Sdim
51258054Semaste#include <net/bpf.h>
52314564Sdim#include <net/ethernet.h>
53258054Semaste#include <net/if.h>
54314564Sdim#include <net/if_arp.h>
55296417Sdim#include <net/if_vlan_var.h>
56254721Semaste
57314564Sdim#include <dev/firewire/firewire.h>
58254721Semaste#include <dev/firewire/firewirereg.h>
59254721Semaste#include <dev/firewire/if_fwevar.h>
60314564Sdim
61314564Sdim#define FWEDEBUG	if (fwedebug) printf
62314564Sdim#define TX_MAX_QUEUE	(FWMAXQUEUE - 1)
63254721Semaste#define RX_MAX_QUEUE	FWMAXQUEUE
64314564Sdim
65254721Semaste/* network interface */
66254721Semastestatic void fwe_start __P((struct ifnet *));
67314564Sdimstatic int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
68254721Semastestatic void fwe_init __P((void *));
69353358Sdim
70353358Sdimstatic void fwe_output_callback __P((struct fw_xfer *));
71254721Semastestatic void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
72254721Semastestatic void fwe_as_input __P((struct fw_xferq *));
73314564Sdim
74314564Sdimstatic int fwedebug = 0;
75254721Semastestatic int stream_ch = 1;
76254721Semastestatic int tx_speed = 2;
77314564Sdim
78314564SdimMALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over FireWire interface");
79314564SdimSYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
80254721SemasteSYSCTL_DECL(_hw_firewire);
81314564SdimSYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
82254721Semaste	"Ethernet Emulation Subsystem");
83314564SdimSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
84254721Semaste	"Stream channel to use");
85314564SdimSYSCTL_INT(_hw_firewire_fwe, OID_AUTO, tx_speed, CTLFLAG_RW, &tx_speed, 0,
86314564Sdim	"Transmission Speed");
87254721Semaste
88314564Sdim#ifdef DEVICE_POLLING
89314564Sdim#define FWE_POLL_REGISTER(func, fwe, ifp)			\
90254721Semaste	if (ether_poll_register(func, ifp)) {			\
91314564Sdim		struct firewire_comm *fc = (fwe)->fd.fc;	\
92314564Sdim		fc->set_intr(fc, 0);				\
93254721Semaste	}
94314564Sdim
95314564Sdim#define FWE_POLL_DEREGISTER(fwe, ifp)				\
96314564Sdim	do {							\
97254721Semaste		struct firewire_comm *fc = (fwe)->fd.fc;	\
98254721Semaste		ether_poll_deregister(ifp);			\
99314564Sdim		fc->set_intr(fc, 1);				\
100296417Sdim	} while(0)						\
101254721Semaste
102314564Sdimstatic poll_handler_t fwe_poll;
103254721Semaste
104254721Semastestatic void
105314564Sdimfwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
106254721Semaste{
107296417Sdim	struct fwe_softc *fwe;
108254721Semaste	struct firewire_comm *fc;
109314564Sdim
110314564Sdim	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
111314564Sdim	fc = fwe->fd.fc;
112296417Sdim	if (cmd == POLL_DEREGISTER) {
113254721Semaste		/* enable interrupts */
114314564Sdim		fc->set_intr(fc, 1);
115314564Sdim		return;
116254721Semaste	}
117314564Sdim	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
118314564Sdim}
119314564Sdim#else
120296417Sdim#define FWE_POLL_REGISTER(func, fwe, ifp)
121254721Semaste#define FWE_POLL_DEREGISTER(fwe, ifp)
122314564Sdim#endif
123314564Sdimstatic void
124314564Sdimfwe_identify(driver_t *driver, device_t parent)
125314564Sdim{
126314564Sdim	BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent));
127314564Sdim}
128314564Sdim
129254721Semastestatic int
130314564Sdimfwe_probe(device_t dev)
131254721Semaste{
132314564Sdim	device_t pa;
133314564Sdim
134254721Semaste	pa = device_get_parent(dev);
135314564Sdim	if(device_get_unit(dev) != device_get_unit(pa)){
136314564Sdim		return(ENXIO);
137314564Sdim	}
138314564Sdim
139254721Semaste	device_set_desc(dev, "Ethernet over FireWire");
140314564Sdim	return (0);
141314564Sdim}
142314564Sdim
143314564Sdimstatic int
144314564Sdimfwe_attach(device_t dev)
145314564Sdim{
146314564Sdim	struct fwe_softc *fwe;
147254721Semaste	struct ifnet *ifp;
148314564Sdim	int unit, s;
149254721Semaste	u_char *eaddr;
150314564Sdim	struct fw_eui64 *eui;
151254721Semaste
152314564Sdim	fwe = ((struct fwe_softc *)device_get_softc(dev));
153254721Semaste	unit = device_get_unit(dev);
154314564Sdim
155314564Sdim	bzero(fwe, sizeof(struct fwe_softc));
156254721Semaste	/* XXX */
157314564Sdim	fwe->stream_ch = stream_ch;
158314564Sdim	fwe->dma_ch = -1;
159254721Semaste
160314564Sdim	fwe->fd.fc = device_get_ivars(dev);
161314564Sdim	fwe->fd.dev = dev;
162254721Semaste	fwe->fd.post_explore = NULL;
163254721Semaste	fwe->eth_softc.fwe = fwe;
164314564Sdim
165254721Semaste	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
166254721Semaste	fwe->pkt_hdr.mode.stream.sy = 0;
167314564Sdim	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
168314564Sdim
169254721Semaste	/* generate fake MAC address: first and last 3bytes from eui64 */
170314564Sdim#define LOCAL (0x02)
171314564Sdim#define GROUP (0x01)
172314564Sdim	eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
173314564Sdim
174314564Sdim	eui = &fwe->fd.fc->eui;
175314564Sdim	eaddr[0] = (FW_EUI64_BYTE(eui, 0) | LOCAL) & ~GROUP;
176314564Sdim	eaddr[1] = FW_EUI64_BYTE(eui, 1);
177314564Sdim	eaddr[2] = FW_EUI64_BYTE(eui, 2);
178314564Sdim	eaddr[3] = FW_EUI64_BYTE(eui, 5);
179314564Sdim	eaddr[4] = FW_EUI64_BYTE(eui, 6);
180314564Sdim	eaddr[5] = FW_EUI64_BYTE(eui, 7);
181254721Semaste	printf("if_fwe%d: Fake Ethernet address: "
182254721Semaste		"%02x:%02x:%02x:%02x:%02x:%02x\n", unit,
183314564Sdim		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
184314564Sdim
185314564Sdim	/* fill the rest and attach interface */
186314564Sdim	ifp = &fwe->fwe_if;
187314564Sdim	ifp->if_softc = &fwe->eth_softc;
188314564Sdim
189314564Sdim	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
190314564Sdim	ifp->if_init = fwe_init;
191314564Sdim	ifp->if_output = ether_output;
192314564Sdim	ifp->if_start = fwe_start;
193314564Sdim	ifp->if_ioctl = fwe_ioctl;
194254721Semaste	ifp->if_mtu = ETHERMTU;
195254721Semaste	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
196314564Sdim	ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
197314564Sdim
198314564Sdim	s = splimp();
199314564Sdim#if __FreeBSD_version >= 500000
200314564Sdim	ether_ifattach(ifp, eaddr);
201254721Semaste#else
202254721Semaste	ether_ifattach(ifp, 1);
203314564Sdim#endif
204314564Sdim	splx(s);
205314564Sdim
206314564Sdim        /* Tell the upper layer(s) we support long frames. */
207314564Sdim	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
208314564Sdim#if __FreeBSD_version >= 500000
209254721Semaste	ifp->if_capabilities |= IFCAP_VLAN_MTU;
210254721Semaste#endif
211314564Sdim
212314564Sdim
213314564Sdim	FWEDEBUG("interface %s created.\n", ifp->if_xname);
214254721Semaste	return 0;
215254721Semaste}
216314564Sdim
217314564Sdimstatic void
218314564Sdimfwe_stop(struct fwe_softc *fwe)
219314564Sdim{
220254721Semaste	struct firewire_comm *fc;
221254721Semaste	struct fw_xferq *xferq;
222314564Sdim	struct ifnet *ifp = &fwe->fwe_if;
223314564Sdim	struct fw_xfer *xfer, *next;
224314564Sdim	int i;
225314564Sdim
226314564Sdim	fc = fwe->fd.fc;
227254721Semaste
228254721Semaste	FWE_POLL_DEREGISTER(fwe, ifp);
229314564Sdim
230314564Sdim	if (fwe->dma_ch >= 0) {
231314564Sdim		xferq = fc->ir[fwe->dma_ch];
232254721Semaste
233254721Semaste		if (xferq->flag & FWXFERQ_RUNNING)
234314564Sdim			fc->irx_disable(fc, fwe->dma_ch);
235314564Sdim		xferq->flag &=
236254721Semaste			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
237254721Semaste			FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
238314564Sdim		xferq->hand =  NULL;
239314564Sdim
240254721Semaste		for (i = 0; i < xferq->bnchunk; i ++)
241254721Semaste			m_freem(xferq->bulkxfer[i].mbuf);
242314564Sdim		free(xferq->bulkxfer, M_FWE);
243314564Sdim
244288943Sdim		for (xfer = STAILQ_FIRST(&fwe->xferlist); xfer != NULL;
245254721Semaste					xfer = next) {
246314564Sdim			next = STAILQ_NEXT(xfer, link);
247314564Sdim			fw_xfer_free(xfer);
248314564Sdim		}
249314564Sdim		STAILQ_INIT(&fwe->xferlist);
250314564Sdim
251314564Sdim		xferq->bulkxfer =  NULL;
252254721Semaste		fwe->dma_ch = -1;
253254721Semaste	}
254314564Sdim
255314564Sdim	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
256314564Sdim}
257314564Sdim
258314564Sdimstatic int
259314564Sdimfwe_detach(device_t dev)
260314564Sdim{
261314564Sdim	struct fwe_softc *fwe;
262254721Semaste	int s;
263254721Semaste
264314564Sdim	fwe = (struct fwe_softc *)device_get_softc(dev);
265314564Sdim	s = splimp();
266314564Sdim
267314564Sdim	fwe_stop(fwe);
268314564Sdim#if __FreeBSD_version >= 500000
269314564Sdim	ether_ifdetach(&fwe->fwe_if);
270314564Sdim#else
271314564Sdim	ether_ifdetach(&fwe->fwe_if, 1);
272254721Semaste#endif
273254721Semaste
274314564Sdim	splx(s);
275314564Sdim	return 0;
276314564Sdim}
277314564Sdim
278314564Sdimstatic void
279314564Sdimfwe_init(void *arg)
280314564Sdim{
281314564Sdim	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
282254721Semaste	struct firewire_comm *fc;
283254721Semaste	struct ifnet *ifp = &fwe->fwe_if;
284314564Sdim	struct fw_xferq *xferq;
285314564Sdim	struct fw_xfer *xfer;
286314564Sdim	struct mbuf *m;
287314564Sdim	int i;
288314564Sdim
289314564Sdim	FWEDEBUG("initializing %s\n", ifp->if_xname);
290314564Sdim
291314564Sdim	/* XXX keep promiscoud mode */
292254721Semaste	ifp->if_flags |= IFF_PROMISC;
293254721Semaste
294314564Sdim	fc = fwe->fd.fc;
295314564Sdim#define START 0
296314564Sdim	if (fwe->dma_ch < 0) {
297314564Sdim		for (i = START; i < fc->nisodma; i ++) {
298314564Sdim			xferq = fc->ir[i];
299314564Sdim			if ((xferq->flag & FWXFERQ_OPEN) == 0)
300314564Sdim				goto found;
301314564Sdim		}
302314564Sdim		printf("no free dma channel\n");
303254721Semaste		return;
304254721Semastefound:
305314564Sdim		fwe->dma_ch = i;
306314564Sdim		fwe->stream_ch = stream_ch;
307254721Semaste		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
308254721Semaste		/* allocate DMA channel and init packet mode */
309314564Sdim		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_EXTBUF |
310314564Sdim				FWXFERQ_HANDLER | FWXFERQ_STREAM;
311254721Semaste		xferq->flag &= ~0xff;
312254721Semaste		xferq->flag |= fwe->stream_ch & 0xff;
313314564Sdim		/* register fwe_input handler */
314314564Sdim		xferq->sc = (caddr_t) fwe;
315314564Sdim		xferq->hand = fwe_as_input;
316254721Semaste		xferq->bnchunk = RX_MAX_QUEUE;
317254721Semaste		xferq->bnpacket = 1;
318314564Sdim		xferq->psize = MCLBYTES;
319314564Sdim		xferq->queued = 0;
320314564Sdim		xferq->buf = NULL;
321254721Semaste		xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
322254721Semaste			sizeof(struct fw_bulkxfer) * xferq->bnchunk,
323314564Sdim							M_FWE, M_WAITOK);
324314564Sdim		if (xferq->bulkxfer == NULL) {
325314564Sdim			printf("if_fwe: malloc failed\n");
326254721Semaste			return;
327254721Semaste		}
328314564Sdim		STAILQ_INIT(&xferq->stvalid);
329314564Sdim		STAILQ_INIT(&xferq->stfree);
330314564Sdim		STAILQ_INIT(&xferq->stdma);
331254721Semaste		xferq->stproc = NULL;
332254721Semaste		for (i = 0; i < xferq->bnchunk; i ++) {
333314564Sdim			m =
334314564Sdim#if __FreeBSD_version >= 500000
335314564Sdim				m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
336254721Semaste#else
337254721Semaste				m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
338314564Sdim#endif
339314564Sdim			xferq->bulkxfer[i].mbuf = m;
340314564Sdim			if (m != NULL) {
341254721Semaste				m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
342254721Semaste				STAILQ_INSERT_TAIL(&xferq->stfree,
343314564Sdim						&xferq->bulkxfer[i], link);
344314564Sdim			} else
345314564Sdim				printf("fwe_as_input: m_getcl failed\n");
346254721Semaste		}
347254721Semaste		STAILQ_INIT(&fwe->xferlist);
348314564Sdim		for (i = 0; i < TX_MAX_QUEUE; i++) {
349314564Sdim			xfer = fw_xfer_alloc(M_FWE);
350254721Semaste			if (xfer == NULL)
351254721Semaste				break;
352314564Sdim			xfer->send.spd = tx_speed;
353314564Sdim			xfer->fc = fwe->fd.fc;
354314564Sdim			xfer->retry_req = fw_asybusy;
355254721Semaste			xfer->sc = (caddr_t)fwe;
356254721Semaste			xfer->act.hand = fwe_output_callback;
357314564Sdim			STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
358314564Sdim		}
359314564Sdim	} else
360254721Semaste		xferq = fc->ir[fwe->dma_ch];
361254721Semaste
362314564Sdim
363314564Sdim	/* start dma */
364254721Semaste	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
365254721Semaste		fc->irx_enable(fc, fwe->dma_ch);
366314564Sdim
367314564Sdim	ifp->if_flags |= IFF_RUNNING;
368314564Sdim	ifp->if_flags &= ~IFF_OACTIVE;
369254721Semaste
370314564Sdim	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
371254721Semaste#if 0
372314564Sdim	/* attempt to start output */
373314564Sdim	fwe_start(ifp);
374314564Sdim#endif
375314564Sdim}
376254721Semaste
377314564Sdim
378314564Sdimstatic int
379254721Semastefwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
380314564Sdim{
381314564Sdim	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
382314564Sdim	struct ifstat *ifs = NULL;
383314564Sdim	int s, error, len;
384314564Sdim
385314564Sdim	switch (cmd) {
386254721Semaste		case SIOCSIFFLAGS:
387314564Sdim			s = splimp();
388314564Sdim			if (ifp->if_flags & IFF_UP) {
389314564Sdim				if (!(ifp->if_flags & IFF_RUNNING))
390314564Sdim					fwe_init(&fwe->eth_softc);
391254721Semaste			} else {
392314564Sdim				if (ifp->if_flags & IFF_RUNNING)
393314564Sdim					fwe_stop(fwe);
394314564Sdim			}
395314564Sdim			/* XXX keep promiscoud mode */
396314564Sdim			ifp->if_flags |= IFF_PROMISC;
397254721Semaste			splx(s);
398314564Sdim			break;
399314564Sdim		case SIOCADDMULTI:
400314564Sdim		case SIOCDELMULTI:
401314564Sdim			break;
402314564Sdim
403314564Sdim		case SIOCGIFSTATUS:
404314564Sdim			s = splimp();
405314564Sdim			ifs = (struct ifstat *)data;
406314564Sdim			len = strlen(ifs->ascii);
407314564Sdim			if (len < sizeof(ifs->ascii))
408254721Semaste				snprintf(ifs->ascii + len,
409314564Sdim					sizeof(ifs->ascii) - len,
410314564Sdim					"\tch %d dma %d\n",
411314564Sdim						fwe->stream_ch, fwe->dma_ch);
412314564Sdim			splx(s);
413314564Sdim			break;
414254721Semaste#if __FreeBSD_version >= 500000
415314564Sdim		default:
416314564Sdim#else
417314564Sdim		case SIOCSIFADDR:
418314564Sdim		case SIOCGIFADDR:
419314564Sdim		case SIOCSIFMTU:
420314564Sdim#endif
421314564Sdim			s = splimp();
422254721Semaste			error = ether_ioctl(ifp, cmd, data);
423314564Sdim			splx(s);
424314564Sdim			return (error);
425314564Sdim#if __FreeBSD_version < 500000
426314564Sdim		default:
427296417Sdim			return (EINVAL);
428314564Sdim#endif
429314564Sdim	}
430314564Sdim
431314564Sdim	return (0);
432314564Sdim}
433296417Sdim
434314564Sdimstatic void
435314564Sdimfwe_output_callback(struct fw_xfer *xfer)
436254721Semaste{
437296417Sdim	struct fwe_softc *fwe;
438314564Sdim	struct ifnet *ifp;
439254721Semaste	int s;
440314564Sdim
441254721Semaste	fwe = (struct fwe_softc *)xfer->sc;
442314564Sdim	ifp = &fwe->fwe_if;
443254721Semaste	/* XXX error check */
444314564Sdim	FWEDEBUG("resp = %d\n", xfer->resp);
445254721Semaste	if (xfer->resp != 0)
446314564Sdim		ifp->if_oerrors ++;
447314564Sdim
448314564Sdim	m_freem(xfer->mbuf);
449314564Sdim	fw_xfer_unload(xfer);
450254721Semaste
451314564Sdim	s = splimp();
452314564Sdim	STAILQ_INSERT_TAIL(&fwe->xferlist, xfer, link);
453314564Sdim	splx(s);
454314564Sdim
455254721Semaste	/* for queue full */
456353358Sdim	if (ifp->if_snd.ifq_head != NULL)
457353358Sdim		fwe_start(ifp);
458254721Semaste}
459254721Semaste
460314564Sdimstatic void
461258054Semastefwe_start(struct ifnet *ifp)
462314564Sdim{
463258054Semaste	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
464314564Sdim	int s;
465258054Semaste
466314564Sdim	FWEDEBUG("%s starting\n", ifp->if_xname);
467314564Sdim
468314564Sdim	if (fwe->dma_ch < 0) {
469314564Sdim		struct mbuf	*m = NULL;
470258054Semaste
471314564Sdim		FWEDEBUG("%s not ready.\n", ifp->if_xname);
472258054Semaste
473314564Sdim		s = splimp();
474258054Semaste		do {
475314564Sdim			IF_DEQUEUE(&ifp->if_snd, m);
476314564Sdim			if (m != NULL)
477254721Semaste				m_freem(m);
478254721Semaste			ifp->if_oerrors ++;
479314564Sdim		} while (m != NULL);
480254721Semaste		splx(s);
481314564Sdim
482296417Sdim		return;
483314564Sdim	}
484296417Sdim
485314564Sdim	s = splimp();
486254721Semaste	ifp->if_flags |= IFF_OACTIVE;
487254721Semaste
488314564Sdim	if (ifp->if_snd.ifq_len != 0)
489254721Semaste		fwe_as_output(fwe, ifp);
490314564Sdim
491254721Semaste	ifp->if_flags &= ~IFF_OACTIVE;
492314564Sdim	splx(s);
493314564Sdim}
494314564Sdim
495314564Sdim#define HDR_LEN 4
496314564Sdim#ifndef ETHER_ALIGN
497314564Sdim#define ETHER_ALIGN 2
498314564Sdim#endif
499314564Sdim/* Async. stream output */
500314564Sdimstatic void
501314564Sdimfwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
502314564Sdim{
503314564Sdim	struct mbuf *m;
504314564Sdim	struct fw_xfer *xfer;
505314564Sdim	struct fw_xferq *xferq;
506314564Sdim	struct fw_pkt *fp;
507314564Sdim	int i = 0;
508314564Sdim
509314564Sdim	xfer = NULL;
510314564Sdim	xferq = fwe->fd.fc->atq;
511314564Sdim	while (xferq->queued < xferq->maxq - 1) {
512314564Sdim		xfer = STAILQ_FIRST(&fwe->xferlist);
513314564Sdim		if (xfer == NULL) {
514314564Sdim			printf("if_fwe: lack of xfer\n");
515314564Sdim			return;
516314564Sdim		}
517314564Sdim		IF_DEQUEUE(&ifp->if_snd, m);
518314564Sdim		if (m == NULL)
519314564Sdim			break;
520314564Sdim		STAILQ_REMOVE_HEAD(&fwe->xferlist, link);
521314564Sdim#if __FreeBSD_version >= 500000
522314564Sdim		BPF_MTAP(ifp, m);
523314564Sdim#else
524314564Sdim		if (ifp->if_bpf != NULL)
525314564Sdim			bpf_mtap(ifp, m);
526314564Sdim#endif
527314564Sdim
528314564Sdim		/* keep ip packet alignment for alpha */
529314564Sdim		M_PREPEND(m, ETHER_ALIGN, M_DONTWAIT);
530314564Sdim		fp = &xfer->send.hdr;
531314564Sdim		*(u_int32_t *)&xfer->send.hdr = *(int32_t *)&fwe->pkt_hdr;
532314564Sdim		fp->mode.stream.len = m->m_pkthdr.len;
533314564Sdim		xfer->mbuf = m;
534314564Sdim		xfer->send.pay_len = m->m_pkthdr.len;
535314564Sdim
536314564Sdim		if (fw_asyreq(fwe->fd.fc, -1, xfer) != 0) {
537314564Sdim			/* error */
538314564Sdim			ifp->if_oerrors ++;
539314564Sdim			/* XXX set error code */
540314564Sdim			fwe_output_callback(xfer);
541254721Semaste		} else {
542314564Sdim			ifp->if_opackets ++;
543254721Semaste			i++;
544314564Sdim		}
545254721Semaste	}
546314564Sdim#if 0
547314564Sdim	if (i > 1)
548314564Sdim		printf("%d queued\n", i);
549314564Sdim#endif
550314564Sdim	if (i > 0)
551254721Semaste		xferq->start(fwe->fd.fc);
552314564Sdim}
553314564Sdim
554254721Semaste/* Async. stream output */
555314564Sdimstatic void
556314564Sdimfwe_as_input(struct fw_xferq *xferq)
557314564Sdim{
558314564Sdim	struct mbuf *m, *m0;
559314564Sdim	struct ifnet *ifp;
560314564Sdim	struct fwe_softc *fwe;
561314564Sdim	struct fw_bulkxfer *sxfer;
562254721Semaste	struct fw_pkt *fp;
563254721Semaste	u_char *c;
564314564Sdim#if __FreeBSD_version < 500000
565314564Sdim	struct ether_header *eh;
566314564Sdim#endif
567314564Sdim
568314564Sdim	fwe = (struct fwe_softc *)xferq->sc;
569254721Semaste	ifp = &fwe->fwe_if;
570314564Sdim#if 0
571254721Semaste	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
572314564Sdim#endif
573254721Semaste	while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
574254721Semaste		STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
575314564Sdim		fp = mtod(sxfer->mbuf, struct fw_pkt *);
576314564Sdim		if (fwe->fd.fc->irx_post != NULL)
577314564Sdim			fwe->fd.fc->irx_post(fwe->fd.fc, fp->mode.ld);
578314564Sdim		m = sxfer->mbuf;
579254721Semaste
580254721Semaste		/* insert new rbuf */
581314564Sdim		sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
582314564Sdim		if (m0 != NULL) {
583314564Sdim			m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
584314564Sdim			STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
585254721Semaste		} else
586254721Semaste			printf("fwe_as_input: m_getcl failed\n");
587314564Sdim
588314564Sdim		if (sxfer->resp != 0 || fp->mode.stream.len <
589314564Sdim		    ETHER_ALIGN + sizeof(struct ether_header)) {
590254721Semaste			m_freem(m);
591254721Semaste			ifp->if_ierrors ++;
592314564Sdim			continue;
593314564Sdim		}
594314564Sdim
595254721Semaste		m->m_data += HDR_LEN + ETHER_ALIGN;
596254721Semaste		c = mtod(m, char *);
597314564Sdim#if __FreeBSD_version < 500000
598314564Sdim		eh = (struct ether_header *)c;
599314564Sdim		m->m_data += sizeof(struct ether_header);
600254721Semaste#endif
601254721Semaste		m->m_len = m->m_pkthdr.len =
602314564Sdim				fp->mode.stream.len - ETHER_ALIGN;
603314564Sdim		m->m_pkthdr.rcvif = ifp;
604314564Sdim#if 0
605254721Semaste		FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
606254721Semaste			 "%02x %02x %02x %02x %02x %02x\n"
607254721Semaste			 "%02x %02x %02x %02x\n"
608254721Semaste			 "%02x %02x %02x %02x\n"
609296417Sdim			 "%02x %02x %02x %02x\n"
610			 "%02x %02x %02x %02x\n",
611			 c[0], c[1], c[2], c[3], c[4], c[5],
612			 c[6], c[7], c[8], c[9], c[10], c[11],
613			 c[12], c[13], c[14], c[15],
614			 c[16], c[17], c[18], c[19],
615			 c[20], c[21], c[22], c[23],
616			 c[20], c[21], c[22], c[23]
617		 );
618#endif
619#if __FreeBSD_version >= 500000
620		(*ifp->if_input)(ifp, m);
621#else
622		ether_input(ifp, eh, m);
623#endif
624		ifp->if_ipackets ++;
625	}
626	if (STAILQ_FIRST(&xferq->stfree) != NULL)
627		fwe->fd.fc->irx_enable(fwe->fd.fc, fwe->dma_ch);
628}
629
630
631static devclass_t fwe_devclass;
632
633static device_method_t fwe_methods[] = {
634	/* device interface */
635	DEVMETHOD(device_identify,	fwe_identify),
636	DEVMETHOD(device_probe,		fwe_probe),
637	DEVMETHOD(device_attach,	fwe_attach),
638	DEVMETHOD(device_detach,	fwe_detach),
639	{ 0, 0 }
640};
641
642static driver_t fwe_driver = {
643        "if_fwe",
644	fwe_methods,
645	sizeof(struct fwe_softc),
646};
647
648
649DRIVER_MODULE(fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
650MODULE_VERSION(fwe, 1);
651MODULE_DEPEND(fwe, firewire, 1, 1, 1);
652