if_fwe.c revision 103285
1/*
2 * Copyright (C) 2002
3 * 	Hidetoshi Shimokawa. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *
16 *	This product includes software developed by Hidetoshi Shimokawa.
17 *
18 * 4. Neither the name of the author nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $FreeBSD: head/sys/dev/firewire/if_fwe.c 103285 2002-09-13 12:31:56Z ikob $
35 */
36
37#include "opt_inet.h"
38
39#include <sys/param.h>
40#include <sys/conf.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/socket.h>
45#include <sys/sockio.h>
46#include <sys/sysctl.h>
47#include <sys/systm.h>
48#include <sys/module.h>
49#include <sys/bus.h>
50
51#include <net/bpf.h>
52#include <net/ethernet.h>
53#include <net/if.h>
54#include <net/if_arp.h>
55#include <net/if_vlan_var.h>
56#include <net/route.h>
57
58#include <netinet/in.h>
59
60#include <dev/firewire/firewire.h>
61#include <dev/firewire/firewirereg.h>
62#include <dev/firewire/if_fwevar.h>
63
64#define FWEDEBUG	if (fwedebug) printf
65#define MAX_QUEUED	IFQ_MAXLEN /* 50 */
66
67/* network interface */
68static void fwe_start __P((struct ifnet *));
69static int fwe_ioctl __P((struct ifnet *, u_long, caddr_t));
70static void fwe_init __P((void *));
71
72static void fwe_as_output __P((struct fwe_softc *, struct ifnet *));
73static void fwe_as_input __P((struct fw_xferq *));
74
75static int fwedebug = 0;
76static int stream_ch = 1;
77
78MALLOC_DECLARE(M_FWE);
79MALLOC_DEFINE(M_FWE, "if_fwe", "Ethernet over Firewire interface");
80SYSCTL_INT(_debug, OID_AUTO, if_fwe_debug, CTLFLAG_RW, &fwedebug, 0, "");
81SYSCTL_DECL(_hw_firewire);
82SYSCTL_NODE(_hw_firewire, OID_AUTO, fwe, CTLFLAG_RD, 0,
83	"Ethernet Emulation Subsystem");
84SYSCTL_INT(_hw_firewire_fwe, OID_AUTO, stream_ch, CTLFLAG_RW, &stream_ch, 0,
85	"Stream channel to use");
86
87#ifdef DEVICE_POLLING
88#define FWE_POLL_REGISTER(func, fwe, ifp)			\
89	if (ether_poll_register(func, ifp)) {			\
90		struct firewire_comm *fc = (fwe)->fd.fc;	\
91		fc->set_intr(fc, 0);				\
92	}
93
94#define FWE_POLL_DEREGISTER(fwe, ifp)				\
95	do {							\
96		struct firewire_comm *fc = (fwe)->fd.fc;	\
97		ether_poll_deregister(ifp);			\
98		fc->set_intr(fc, 1);				\
99	} while(0)						\
100
101static poll_handler_t fwe_poll;
102
103static void
104fwe_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
105{
106	struct fwe_softc *fwe;
107	struct firewire_comm *fc;
108
109	fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
110	fc = fwe->fd.fc;
111	if (cmd == POLL_DEREGISTER) {
112		/* enable interrupts */
113		fc->set_intr(fc, 1);
114		return;
115	}
116	fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
117}
118#else
119#define FWE_POLL_REGISTER(func, fwe, ifp)
120#define FWE_POLL_DEREGISTER(fwe, ifp)
121#endif
122static void
123fwe_identify(driver_t *driver, device_t parent)
124{
125	BUS_ADD_CHILD(parent, 0, "if_fwe", device_get_unit(parent));
126}
127
128static int
129fwe_probe(device_t dev)
130{
131	device_t pa;
132
133	pa = device_get_parent(dev);
134	if(device_get_unit(dev) != device_get_unit(pa)){
135		return(ENXIO);
136	}
137
138	device_set_desc(dev, "Ethernet over Firewire");
139	return (0);
140}
141
142static int
143fwe_attach(device_t dev)
144{
145	struct fwe_softc *fwe;
146	struct ifnet *ifp;
147	int unit, s;
148	u_char *eaddr;
149
150	fwe = ((struct fwe_softc *)device_get_softc(dev));
151	unit = device_get_unit(dev);
152
153	bzero(fwe, sizeof(struct fwe_softc));
154	/* XXX */
155	fwe->stream_ch = stream_ch;
156	fwe->dma_ch = -1;
157
158	fwe->fd.fc = device_get_ivars(dev);
159	fwe->fd.dev = dev;
160	fwe->fd.post_explore = NULL;
161	fwe->eth_softc.fwe = fwe;
162
163	fwe->pkt_hdr.mode.stream.tcode = FWTCODE_STREAM;
164	fwe->pkt_hdr.mode.stream.sy = 0;
165	fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
166
167	/* generate fake MAC address: first and last 3bytes from eui64 */
168#define LOCAL (0x02)
169#define GROUP (0x01)
170	eaddr = &fwe->eth_softc.arpcom.ac_enaddr[0];
171	eaddr[0] = (fwe->fd.fc->eui[0] | LOCAL) & ~GROUP;
172	eaddr[1] = fwe->fd.fc->eui[1];
173	eaddr[2] = fwe->fd.fc->eui[2];
174	eaddr[3] = fwe->fd.fc->eui[5];
175	eaddr[4] = fwe->fd.fc->eui[6];
176	eaddr[5] = fwe->fd.fc->eui[7];
177	printf("if_fwe%d: %02x:%02x:%02x:%02x:%02x:%02x\n", unit,
178		eaddr[0], eaddr[1], eaddr[2], eaddr[3], eaddr[4], eaddr[5]);
179
180	/* fill the rest and attach interface */
181	ifp = &fwe->fwe_if;
182	ifp->if_softc = &fwe->eth_softc;
183
184	ifp->if_unit = unit;
185	ifp->if_name = "fwe";
186	ifp->if_init = fwe_init;
187	ifp->if_output = ether_output;
188	ifp->if_start = fwe_start;
189	ifp->if_ioctl = fwe_ioctl;
190	ifp->if_mtu = ETHERMTU;
191	ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
192	ifp->if_snd.ifq_maxlen = FWMAXQUEUE - 1;
193
194	s = splimp();
195	ether_ifattach(ifp, 1);
196	splx(s);
197
198        /* Tell the upper layer(s) we support long frames. */
199	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
200
201	ifp->if_snd.ifq_maxlen = MAX_QUEUED - 1;
202
203	FWEDEBUG("interface %s%d created.\n", ifp->if_name, ifp->if_unit);
204	return 0;
205}
206
207static void
208fwe_stop(struct fwe_softc *fwe)
209{
210	struct firewire_comm *fc;
211	struct fw_xferq *xferq;
212	struct ifnet *ifp = &fwe->fwe_if;
213
214	fc = fwe->fd.fc;
215
216	FWE_POLL_DEREGISTER(fwe, ifp);
217
218	if (fwe->dma_ch >= 0) {
219		xferq = fc->ir[fwe->dma_ch];
220
221		if (xferq->flag & FWXFERQ_RUNNING)
222			fc->irx_disable(fc, fwe->dma_ch);
223		xferq->flag &=
224			~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_HANDLER);
225		/* XXX dequeue xferq->q */
226		fwe->dma_ch = -1;
227	}
228
229	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
230}
231
232static int
233fwe_detach(device_t dev)
234{
235	struct fwe_softc *fwe;
236	int s;
237
238	fwe = (struct fwe_softc *)device_get_softc(dev);
239	s = splimp();
240
241	fwe_stop(fwe);
242	ether_ifdetach(&fwe->fwe_if, 1);
243
244	splx(s);
245	return 0;
246}
247
248
249static void
250fwe_init(void *arg)
251{
252	struct fwe_softc *fwe = ((struct fwe_eth_softc *)arg)->fwe;
253	struct firewire_comm *fc;
254	struct ifnet *ifp = &fwe->fwe_if;
255	struct fw_xferq *xferq;
256	int i;
257
258	FWEDEBUG("initializing %s%d\n", ifp->if_name, ifp->if_unit);
259
260	/* XXX keep promiscoud mode */
261	ifp->if_flags |= IFF_PROMISC;
262
263	fc = fwe->fd.fc;
264#define START 0
265	if (fwe->dma_ch < 0) {
266		xferq = NULL;
267		for (i = START; i < fc->nisodma; i ++) {
268			xferq = fc->ir[i];
269			if ((xferq->flag & FWXFERQ_OPEN) == 0)
270				break;
271		}
272
273		if (xferq == NULL) {
274			printf("no free dma channel\n");
275			return;
276		}
277		fwe->dma_ch = i;
278		fwe->stream_ch = stream_ch;
279		fwe->pkt_hdr.mode.stream.chtag = fwe->stream_ch;
280		/* allocate DMA channel and init packet mode */
281		xferq->flag |= FWXFERQ_OPEN | FWXFERQ_PACKET;
282		xferq->flag |= fwe->stream_ch & 0xff;
283		/* register fwe_input handler */
284		xferq->sc = (caddr_t) fwe;
285		xferq->hand = fwe_as_input;
286		xferq->flag |= FWXFERQ_HANDLER;
287	} else
288		xferq = fc->ir[fwe->dma_ch];
289
290
291	/* start dma */
292	if ((xferq->flag & FWXFERQ_RUNNING) == 0)
293		fc->irx_enable(fc, fwe->dma_ch);
294
295	ifp->if_flags |= IFF_RUNNING;
296	ifp->if_flags &= ~IFF_OACTIVE;
297
298	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
299#if 0
300	/* attempt to start output */
301	fwe_start(ifp);
302#endif
303}
304
305
306static int
307fwe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
308{
309	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
310	struct ifstat *ifs = NULL;
311	int s, error, len;
312
313	switch (cmd) {
314		case SIOCSIFADDR:
315		case SIOCGIFADDR:
316		case SIOCSIFMTU:
317			s = splimp();
318			error = ether_ioctl(ifp, cmd, data);
319			splx(s);
320			return (error);
321		case SIOCSIFFLAGS:
322			s = splimp();
323			if (ifp->if_flags & IFF_UP) {
324				if (!(ifp->if_flags & IFF_RUNNING))
325					fwe_init(&fwe->eth_softc);
326			} else {
327				if (ifp->if_flags & IFF_RUNNING)
328					fwe_stop(fwe);
329			}
330			/* XXX keep promiscoud mode */
331			ifp->if_flags |= IFF_PROMISC;
332			splx(s);
333			break;
334		case SIOCADDMULTI:
335		case SIOCDELMULTI:
336		break;
337
338		case SIOCGIFSTATUS:
339			s = splimp();
340			ifs = (struct ifstat *)data;
341			len = strlen(ifs->ascii);
342			if (len < sizeof(ifs->ascii))
343				snprintf(ifs->ascii + len,
344					sizeof(ifs->ascii) - len,
345					"\tch %d dma %d\n",
346						fwe->stream_ch, fwe->dma_ch);
347			splx(s);
348		break;
349
350		default:
351			return (EINVAL);
352	}
353
354	return (0);
355}
356
357static void
358fwe_start(struct ifnet *ifp)
359{
360	struct fwe_softc *fwe = ((struct fwe_eth_softc *)ifp->if_softc)->fwe;
361	int s;
362
363#if 1
364	FWEDEBUG("%s%d starting\n", ifp->if_name, ifp->if_unit);
365
366	if (fwe->dma_ch < 0) {
367		struct mbuf	*m = NULL;
368
369		FWEDEBUG("%s%d not ready.\n", ifp->if_name, ifp->if_unit);
370
371		s = splimp();
372		do {
373			IF_DEQUEUE(&ifp->if_snd, m);
374			if (m != NULL)
375				m_freem(m);
376			ifp->if_oerrors ++;
377		} while (m != NULL);
378		splx(s);
379
380		return;
381	}
382
383#endif
384	s = splimp();
385	ifp->if_flags |= IFF_OACTIVE;
386
387	if (ifp->if_snd.ifq_len != 0)
388		fwe_as_output(fwe, ifp);
389
390	ifp->if_flags &= ~IFF_OACTIVE;
391	splx(s);
392}
393
394
395static void
396fwe_output_callback(struct fw_xfer *xfer)
397{
398	struct fwe_softc *fwe;
399	struct ifnet *ifp;
400
401	fwe = (struct fwe_softc *)xfer->sc;
402	/* XXX error check */
403	FWEDEBUG("resp = %d\n", xfer->resp);
404	m_freem(xfer->mbuf);
405	xfer->send.buf = NULL;
406	fw_xfer_free(xfer);
407#if 1
408	/* XXX for queue full */
409	ifp = &fwe->fwe_if;
410	if (ifp->if_snd.ifq_head != NULL)
411		fwe_start(ifp);
412#endif
413}
414
415#define HDR_LEN 4
416#define ALIGN_PAD 2
417/* Async. stream output */
418static void
419fwe_as_output(struct fwe_softc *fwe, struct ifnet *ifp)
420{
421	struct mbuf *m;
422	struct fw_xfer *xfer;
423	struct fw_xferq *xferq;
424	struct fw_pkt *fp;
425	int i = 0;
426
427	xfer = NULL;
428	xferq = fwe->fd.fc->atq;
429	while (xferq->queued < xferq->maxq) {
430		IF_DEQUEUE(&ifp->if_snd, m);
431		if (m == NULL)
432			break;
433		xfer = fw_xfer_alloc();
434		if (xfer == NULL) {
435			return;
436		}
437		if (ifp->if_bpf != NULL)
438			bpf_mtap(ifp, m);
439
440		xfer->send.off = 0;
441		xfer->spd = 2;
442		xfer->fc = fwe->fd.fc;
443		xfer->retry_req = fw_asybusy;
444		xfer->sc = (caddr_t)fwe;
445		xfer->act.hand = fwe_output_callback;
446
447		/* keep ip packet alignment for alpha */
448		M_PREPEND(m, ALIGN_PAD, M_DONTWAIT);
449		fp = (struct fw_pkt *)&xfer->dst; /* XXX */
450		xfer->dst = *((int32_t *)&fwe->pkt_hdr);
451		fp->mode.stream.len = htons(m->m_pkthdr.len);
452		xfer->send.buf = (caddr_t) fp;
453		xfer->mbuf = m;
454		xfer->send.len = m->m_pkthdr.len + HDR_LEN;
455
456		i++;
457		if (fw_asyreq(xfer->fc, -1, xfer) != 0) {
458			/* error */
459			ifp->if_oerrors ++;
460			/* XXX set error code */
461			fwe_output_callback(xfer);
462		} else {
463			ifp->if_opackets ++;
464		}
465	}
466#if 0
467	if (i > 1)
468		printf("%d queued\n", i);
469#endif
470	if (xfer != NULL)
471		xferq->start(xfer->fc);
472}
473
474#if __FreeBSD_version >= 500000
475static void
476fwe_free(void *buf, void *args)
477{
478	FWEDEBUG("fwe_free:\n");
479	free(buf, M_DEVBUF);
480}
481
482#else
483static void
484fwe_free(caddr_t buf, u_int size)
485{
486	int *p;
487	FWEDEBUG("fwe_free:\n");
488	p = (int *)buf;
489	(*p) --;
490	if (*p < 1)
491		free(buf, M_DEVBUF);
492}
493
494static void
495fwe_ref(caddr_t buf, u_int size)
496{
497	int *p;
498
499	FWEDEBUG("fwe_ref: called\n");
500	p = (int *)buf;
501	(*p) ++;
502}
503#endif
504
505/* Async. stream output */
506static void
507fwe_as_input(struct fw_xferq *xferq)
508{
509	struct mbuf *m;
510	struct ether_header *eh;
511	struct ifnet *ifp;
512	struct fw_xfer *xfer;
513	struct fwe_softc *fwe;
514	u_char *c;
515	int len;
516	caddr_t p;
517
518	fwe = (struct fwe_softc *)xferq->sc;
519	ifp = &fwe->fwe_if;
520#if 0
521	FWE_POLL_REGISTER(fwe_poll, fwe, ifp);
522#endif
523	while ((xfer = STAILQ_FIRST(&xferq->q)) != NULL) {
524		STAILQ_REMOVE_HEAD(&xferq->q, link);
525		xferq->queued --;
526		MGETHDR(m, M_DONTWAIT, MT_DATA);
527		if (m == NULL) {
528			printf("MGETHDR failed\n");
529			fw_xfer_free(xfer);
530			return;
531		}
532		len = xfer->recv.off + xfer->recv.len;
533		FWEDEBUG("fwe_as_input len=%d\n", len);
534#if __FreeBSD_version >= 500000
535		MEXTADD(m, xfer->recv.buf, len, fwe_free, NULL, 0, EXT_NET_DRV);
536#else
537		m->m_flags |= M_EXT;
538		m->m_ext.ext_buf = xfer->recv.buf;
539		m->m_ext.ext_size = len;
540		m->m_ext.ext_free = fwe_free;
541		m->m_ext.ext_ref = fwe_ref;
542		*((int *)m->m_ext.ext_buf) = 1;  /* XXX refcount */
543#endif
544		p = xfer->recv.buf + xfer->recv.off + HDR_LEN + ALIGN_PAD;
545		eh = (struct ether_header *)p;
546		p += sizeof(struct ether_header);
547		len -= xfer->recv.off + HDR_LEN + ALIGN_PAD
548						+ sizeof(struct ether_header);
549		m->m_data = p;
550		m->m_len = m->m_pkthdr.len = len;
551		m->m_pkthdr.rcvif = ifp;
552		c = (char *)eh;
553#if 0
554		FWEDEBUG("%02x %02x %02x %02x %02x %02x\n"
555			 "%02x %02x %02x %02x %02x %02x\n"
556			 "%02x %02x %02x %02x\n"
557			 "%02x %02x %02x %02x\n"
558			 "%02x %02x %02x %02x\n"
559			 "%02x %02x %02x %02x\n",
560			 c[0], c[1], c[2], c[3], c[4], c[5],
561			 c[6], c[7], c[8], c[9], c[10], c[11],
562			 c[12], c[13], c[14], c[15],
563			 c[16], c[17], c[18], c[19],
564			 c[20], c[21], c[22], c[23],
565			 c[20], c[21], c[22], c[23]
566		 );
567#endif
568		ether_input(ifp, eh, m);
569		ifp->if_ipackets ++;
570
571		xfer->recv.buf = NULL;
572		fw_xfer_free(xfer);
573	}
574}
575
576
577static devclass_t fwe_devclass;
578
579static device_method_t fwe_methods[] = {
580	/* device interface */
581	DEVMETHOD(device_identify,	fwe_identify),
582	DEVMETHOD(device_probe,		fwe_probe),
583	DEVMETHOD(device_attach,	fwe_attach),
584	DEVMETHOD(device_detach,	fwe_detach),
585	{ 0, 0 }
586};
587
588static driver_t fwe_driver = {
589        "if_fwe",
590	fwe_methods,
591	sizeof(struct fwe_softc),
592};
593
594
595DRIVER_MODULE(if_fwe, firewire, fwe_driver, fwe_devclass, 0, 0);
596MODULE_VERSION(if_fwe, 1);
597MODULE_DEPEND(if_fwe, firewire, 1, 1, 1);
598