if_fwip.c revision 1.4.18.5 1 1.4.18.5 yamt /* $NetBSD: if_fwip.c,v 1.4.18.5 2007/11/15 11:44:13 yamt Exp $ */
2 1.4.18.2 yamt /*-
3 1.4.18.2 yamt * Copyright (c) 2004
4 1.4.18.2 yamt * Doug Rabson
5 1.4.18.2 yamt * Copyright (c) 2002-2003
6 1.4.18.2 yamt * Hidetoshi Shimokawa. All rights reserved.
7 1.4.18.2 yamt *
8 1.4.18.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.4.18.2 yamt * modification, are permitted provided that the following conditions
10 1.4.18.2 yamt * are met:
11 1.4.18.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.4.18.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.4.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.4.18.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.4.18.2 yamt * documentation and/or other materials provided with the distribution.
16 1.4.18.2 yamt * 3. All advertising materials mentioning features or use of this software
17 1.4.18.2 yamt * must display the following acknowledgement:
18 1.4.18.2 yamt *
19 1.4.18.2 yamt * This product includes software developed by Hidetoshi Shimokawa.
20 1.4.18.2 yamt *
21 1.4.18.2 yamt * 4. Neither the name of the author nor the names of its contributors
22 1.4.18.2 yamt * may be used to endorse or promote products derived from this software
23 1.4.18.2 yamt * without specific prior written permission.
24 1.4.18.2 yamt *
25 1.4.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.4.18.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.4.18.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.4.18.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.4.18.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.4.18.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.4.18.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.4.18.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.4.18.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.4.18.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.4.18.2 yamt * SUCH DAMAGE.
36 1.4.18.2 yamt *
37 1.4.18.5 yamt * $FreeBSD: src/sys/dev/firewire/if_fwip.c,v 1.16 2007/06/06 14:31:36 simokawa Exp $
38 1.4.18.2 yamt */
39 1.4.18.2 yamt
40 1.4.18.3 yamt #ifdef HAVE_KERNEL_OPTION_HEADERS
41 1.4.18.3 yamt #include "opt_device_polling.h"
42 1.4.18.2 yamt #include "opt_inet.h"
43 1.4.18.3 yamt #endif
44 1.4.18.2 yamt
45 1.4.18.2 yamt #if defined(__FreeBSD__)
46 1.4.18.2 yamt #include <sys/param.h>
47 1.4.18.2 yamt #include <sys/kernel.h>
48 1.4.18.2 yamt #include <sys/malloc.h>
49 1.4.18.2 yamt #include <sys/mbuf.h>
50 1.4.18.2 yamt #include <sys/socket.h>
51 1.4.18.2 yamt #include <sys/sockio.h>
52 1.4.18.2 yamt #include <sys/sysctl.h>
53 1.4.18.2 yamt #include <sys/systm.h>
54 1.4.18.2 yamt #include <sys/taskqueue.h>
55 1.4.18.2 yamt #include <sys/module.h>
56 1.4.18.2 yamt #include <sys/bus.h>
57 1.4.18.4 yamt #include <sys/bus.h>
58 1.4.18.2 yamt
59 1.4.18.2 yamt #include <net/bpf.h>
60 1.4.18.2 yamt #include <net/if.h>
61 1.4.18.2 yamt #include <net/firewire.h>
62 1.4.18.2 yamt #include <net/if_arp.h>
63 1.4.18.3 yamt #include <net/if_types.h>
64 1.4.18.2 yamt #ifdef __DragonFly__
65 1.4.18.2 yamt #include <bus/firewire/fw_port.h>
66 1.4.18.2 yamt #include <bus/firewire/firewire.h>
67 1.4.18.2 yamt #include <bus/firewire/firewirereg.h>
68 1.4.18.2 yamt #include "if_fwipvar.h"
69 1.4.18.2 yamt #else
70 1.4.18.2 yamt #include <dev/firewire/fw_port.h>
71 1.4.18.2 yamt #include <dev/firewire/firewire.h>
72 1.4.18.2 yamt #include <dev/firewire/firewirereg.h>
73 1.4.18.2 yamt #include <dev/firewire/iec13213.h>
74 1.4.18.2 yamt #include <dev/firewire/if_fwipvar.h>
75 1.4.18.2 yamt #endif
76 1.4.18.2 yamt #elif defined(__NetBSD__)
77 1.4.18.2 yamt #include <sys/param.h>
78 1.4.18.2 yamt #include <sys/device.h>
79 1.4.18.2 yamt #include <sys/errno.h>
80 1.4.18.2 yamt #include <sys/malloc.h>
81 1.4.18.2 yamt #include <sys/mbuf.h>
82 1.4.18.2 yamt #include <sys/sysctl.h>
83 1.4.18.2 yamt
84 1.4.18.4 yamt #include <sys/bus.h>
85 1.4.18.2 yamt
86 1.4.18.2 yamt #include <net/if.h>
87 1.4.18.2 yamt #include <net/if_ieee1394.h>
88 1.4.18.3 yamt #include <net/if_types.h>
89 1.4.18.2 yamt
90 1.4.18.2 yamt #include <dev/ieee1394/fw_port.h>
91 1.4.18.2 yamt #include <dev/ieee1394/firewire.h>
92 1.4.18.2 yamt #include <dev/ieee1394/firewirereg.h>
93 1.4.18.2 yamt #include <dev/ieee1394/iec13213.h>
94 1.4.18.2 yamt #include <dev/ieee1394/if_fwipvar.h>
95 1.4.18.2 yamt #endif
96 1.4.18.2 yamt
97 1.4.18.2 yamt /*
98 1.4.18.2 yamt * We really need a mechanism for allocating regions in the FIFO
99 1.4.18.2 yamt * address space. We pick a address in the OHCI controller's 'middle'
100 1.4.18.2 yamt * address space. This means that the controller will automatically
101 1.4.18.2 yamt * send responses for us, which is fine since we don't have any
102 1.4.18.2 yamt * important information to put in the response anyway.
103 1.4.18.2 yamt */
104 1.4.18.2 yamt #define INET_FIFO 0xfffe00000000LL
105 1.4.18.2 yamt
106 1.4.18.2 yamt #if defined(__FreeBSD__)
107 1.4.18.2 yamt #define FWIPDEBUG if (fwipdebug) if_printf
108 1.4.18.2 yamt #elif defined(__NetBSD__)
109 1.4.18.5 yamt #define FWIPDEBUG(ifp, fmt, ...) \
110 1.4.18.5 yamt if (fwipdebug) { \
111 1.4.18.5 yamt printf("%s: ", (ifp)->if_xname);\
112 1.4.18.5 yamt printf((fmt) , ##__VA_ARGS__); \
113 1.4.18.2 yamt }
114 1.4.18.2 yamt #endif
115 1.4.18.2 yamt #define TX_MAX_QUEUE (FWMAXQUEUE - 1)
116 1.4.18.2 yamt
117 1.4.18.2 yamt #if defined(__NetBSD__)
118 1.4.18.2 yamt int fwipmatch (struct device *, struct cfdata *, void *);
119 1.4.18.2 yamt void fwipattach (struct device *, struct device *, void *);
120 1.4.18.2 yamt int fwipdetach (struct device *, int);
121 1.4.18.2 yamt int fwipactivate (struct device *, enum devact);
122 1.4.18.2 yamt
123 1.4.18.2 yamt #endif
124 1.4.18.2 yamt /* network interface */
125 1.4.18.2 yamt static void fwip_start (struct ifnet *);
126 1.4.18.3 yamt static int fwip_ioctl (struct ifnet *, u_long, void *);
127 1.4.18.5 yamt #if defined(__FreeBSD__)
128 1.4.18.5 yamt static void fwip_init(void *);
129 1.4.18.5 yamt static void fwip_stop(struct fwip_softc *);
130 1.4.18.5 yamt #elif defined(__NetBSD__)
131 1.4.18.5 yamt static int fwip_init(struct ifnet *);
132 1.4.18.5 yamt static void fwip_stop(struct ifnet *, int);
133 1.4.18.5 yamt #endif
134 1.4.18.2 yamt
135 1.4.18.2 yamt static void fwip_post_busreset (void *);
136 1.4.18.2 yamt static void fwip_output_callback (struct fw_xfer *);
137 1.4.18.2 yamt static void fwip_async_output (struct fwip_softc *, struct ifnet *);
138 1.4.18.2 yamt static void fwip_start_send (void *, int);
139 1.4.18.2 yamt static void fwip_stream_input (struct fw_xferq *);
140 1.4.18.2 yamt static void fwip_unicast_input(struct fw_xfer *);
141 1.4.18.2 yamt
142 1.4.18.2 yamt static int fwipdebug = 0;
143 1.4.18.2 yamt static int broadcast_channel = 0xc0 | 0x1f; /* tag | channel(XXX) */
144 1.4.18.2 yamt static int tx_speed = 2;
145 1.4.18.2 yamt static int rx_queue_len = FWMAXQUEUE;
146 1.4.18.2 yamt
147 1.4.18.2 yamt #if defined(__FreeBSD__)
148 1.4.18.2 yamt MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over FireWire interface");
149 1.4.18.2 yamt SYSCTL_INT(_debug, OID_AUTO, if_fwip_debug, CTLFLAG_RW, &fwipdebug, 0, "");
150 1.4.18.2 yamt SYSCTL_DECL(_hw_firewire);
151 1.4.18.2 yamt SYSCTL_NODE(_hw_firewire, OID_AUTO, fwip, CTLFLAG_RD, 0,
152 1.4.18.2 yamt "Firewire ip subsystem");
153 1.4.18.2 yamt SYSCTL_INT(_hw_firewire_fwip, OID_AUTO, rx_queue_len, CTLFLAG_RW, &rx_queue_len,
154 1.4.18.2 yamt 0, "Length of the receive queue");
155 1.4.18.2 yamt
156 1.4.18.2 yamt TUNABLE_INT("hw.firewire.fwip.rx_queue_len", &rx_queue_len);
157 1.4.18.2 yamt #elif defined(__NetBSD__)
158 1.4.18.2 yamt MALLOC_DEFINE(M_FWIP, "if_fwip", "IP over IEEE1394 interface");
159 1.4.18.2 yamt /*
160 1.4.18.2 yamt * Setup sysctl(3) MIB, hw.fwip.*
161 1.4.18.2 yamt *
162 1.4.18.2 yamt * TBD condition CTLFLAG_PERMANENT on being an LKM or not
163 1.4.18.2 yamt */
164 1.4.18.2 yamt SYSCTL_SETUP(sysctl_fwip, "sysctl fwip(4) subtree setup")
165 1.4.18.2 yamt {
166 1.4.18.2 yamt int rc, fwip_node_num;
167 1.4.18.2 yamt const struct sysctlnode *node;
168 1.4.18.2 yamt
169 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, NULL,
170 1.4.18.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
171 1.4.18.2 yamt NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
172 1.4.18.2 yamt goto err;
173 1.4.18.2 yamt }
174 1.4.18.2 yamt
175 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &node,
176 1.4.18.2 yamt CTLFLAG_PERMANENT, CTLTYPE_NODE, "fwip",
177 1.4.18.2 yamt SYSCTL_DESCR("fwip controls"),
178 1.4.18.2 yamt NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
179 1.4.18.2 yamt goto err;
180 1.4.18.2 yamt }
181 1.4.18.2 yamt fwip_node_num = node->sysctl_num;
182 1.4.18.2 yamt
183 1.4.18.2 yamt /* fwip RX queue length */
184 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &node,
185 1.4.18.2 yamt CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
186 1.4.18.2 yamt "rx_queue_len", SYSCTL_DESCR("Length of the receive queue"),
187 1.4.18.2 yamt NULL, 0, &rx_queue_len,
188 1.4.18.2 yamt 0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
189 1.4.18.2 yamt goto err;
190 1.4.18.2 yamt }
191 1.4.18.2 yamt
192 1.4.18.2 yamt /* fwip RX queue length */
193 1.4.18.2 yamt if ((rc = sysctl_createv(clog, 0, NULL, &node,
194 1.4.18.2 yamt CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
195 1.4.18.2 yamt "if_fwip_debug", SYSCTL_DESCR("fwip driver debug flag"),
196 1.4.18.2 yamt NULL, 0, &fwipdebug,
197 1.4.18.2 yamt 0, CTL_HW, fwip_node_num, CTL_CREATE, CTL_EOL)) != 0) {
198 1.4.18.2 yamt goto err;
199 1.4.18.2 yamt }
200 1.4.18.2 yamt
201 1.4.18.2 yamt return;
202 1.4.18.2 yamt
203 1.4.18.2 yamt err:
204 1.4.18.2 yamt printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
205 1.4.18.2 yamt }
206 1.4.18.2 yamt #endif
207 1.4.18.2 yamt
208 1.4.18.2 yamt #ifdef DEVICE_POLLING
209 1.4.18.2 yamt static poll_handler_t fwip_poll;
210 1.4.18.2 yamt
211 1.4.18.2 yamt static void
212 1.4.18.2 yamt fwip_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
213 1.4.18.2 yamt {
214 1.4.18.2 yamt struct fwip_softc *fwip;
215 1.4.18.2 yamt struct firewire_comm *fc;
216 1.4.18.2 yamt
217 1.4.18.3 yamt if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
218 1.4.18.3 yamt return;
219 1.4.18.3 yamt
220 1.4.18.2 yamt fwip = ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
221 1.4.18.2 yamt fc = fwip->fd.fc;
222 1.4.18.2 yamt fc->poll(fc, (cmd == POLL_AND_CHECK_STATUS)?0:1, count);
223 1.4.18.2 yamt }
224 1.4.18.3 yamt #endif /* DEVICE_POLLING */
225 1.4.18.2 yamt #if defined(__FreeBSD__)
226 1.4.18.2 yamt static void
227 1.4.18.2 yamt fwip_identify(driver_t *driver, device_t parent)
228 1.4.18.2 yamt {
229 1.4.18.5 yamt BUS_ADD_CHILD(parent, 0, "fwip", fw_get_unit(parent));
230 1.4.18.2 yamt }
231 1.4.18.2 yamt
232 1.4.18.2 yamt static int
233 1.4.18.2 yamt fwip_probe(device_t dev)
234 1.4.18.2 yamt {
235 1.4.18.2 yamt device_t pa;
236 1.4.18.2 yamt
237 1.4.18.2 yamt pa = device_get_parent(dev);
238 1.4.18.5 yamt if(fw_get_unit(dev) != fw_get_unit(pa)){
239 1.4.18.2 yamt return(ENXIO);
240 1.4.18.2 yamt }
241 1.4.18.2 yamt
242 1.4.18.2 yamt device_set_desc(dev, "IP over FireWire");
243 1.4.18.2 yamt return (0);
244 1.4.18.2 yamt }
245 1.4.18.2 yamt #elif defined(__NetBSD__)
246 1.4.18.2 yamt int
247 1.4.18.2 yamt fwipmatch(struct device *parent, struct cfdata *cf, void *aux)
248 1.4.18.2 yamt {
249 1.4.18.2 yamt struct fw_attach_args *fwa = aux;
250 1.4.18.2 yamt
251 1.4.18.2 yamt if (strcmp(fwa->name, "fwip") == 0)
252 1.4.18.2 yamt return (1);
253 1.4.18.2 yamt return (0);
254 1.4.18.2 yamt }
255 1.4.18.2 yamt #endif
256 1.4.18.2 yamt
257 1.4.18.2 yamt FW_ATTACH(fwip)
258 1.4.18.2 yamt {
259 1.4.18.2 yamt FW_ATTACH_START(fwip, fwip, fwa);
260 1.4.18.2 yamt FWIP_ATTACH_START;
261 1.4.18.2 yamt struct ifnet *ifp;
262 1.4.18.2 yamt int s;
263 1.4.18.2 yamt
264 1.4.18.2 yamt FWIP_ATTACH_SETUP;
265 1.4.18.2 yamt
266 1.4.18.3 yamt ifp = fwip->fw_softc.fwip_ifp;
267 1.4.18.3 yamt if (ifp == NULL)
268 1.4.18.3 yamt FW_ATTACH_RETURN(ENOSPC);
269 1.4.18.3 yamt
270 1.4.18.5 yamt fw_mtx_init(&fwip->mtx, "fwip", NULL, MTX_DEF);
271 1.4.18.2 yamt /* XXX */
272 1.4.18.2 yamt fwip->dma_ch = -1;
273 1.4.18.2 yamt
274 1.4.18.2 yamt fwip->fd.fc = fwa->fc;
275 1.4.18.2 yamt if (tx_speed < 0)
276 1.4.18.2 yamt tx_speed = fwip->fd.fc->speed;
277 1.4.18.2 yamt
278 1.4.18.2 yamt fwip->fd.post_explore = NULL;
279 1.4.18.2 yamt fwip->fd.post_busreset = fwip_post_busreset;
280 1.4.18.2 yamt fwip->fw_softc.fwip = fwip;
281 1.4.18.5 yamt FW_TASK_INIT(&fwip->start_send, 0, fwip_start_send, fwip);
282 1.4.18.2 yamt
283 1.4.18.2 yamt /*
284 1.4.18.2 yamt * Encode our hardware the way that arp likes it.
285 1.4.18.2 yamt */
286 1.4.18.2 yamt hwaddr->sender_unique_ID_hi = htonl(fwip->fd.fc->eui.hi);
287 1.4.18.2 yamt hwaddr->sender_unique_ID_lo = htonl(fwip->fd.fc->eui.lo);
288 1.4.18.2 yamt hwaddr->sender_max_rec = fwip->fd.fc->maxrec;
289 1.4.18.2 yamt hwaddr->sspd = fwip->fd.fc->speed;
290 1.4.18.2 yamt hwaddr->sender_unicast_FIFO_hi = htons((uint16_t)(INET_FIFO >> 32));
291 1.4.18.2 yamt hwaddr->sender_unicast_FIFO_lo = htonl((uint32_t)INET_FIFO);
292 1.4.18.2 yamt
293 1.4.18.2 yamt /* fill the rest and attach interface */
294 1.4.18.2 yamt ifp->if_softc = &fwip->fw_softc;
295 1.4.18.2 yamt
296 1.4.18.2 yamt #if __FreeBSD_version >= 501113 || defined(__DragonFly__) || defined(__NetBSD__)
297 1.4.18.2 yamt IF_INITNAME(ifp, dev, unit);
298 1.4.18.2 yamt #else
299 1.4.18.2 yamt ifp->if_unit = unit;
300 1.4.18.2 yamt ifp->if_name = "fwip";
301 1.4.18.2 yamt #endif
302 1.4.18.2 yamt #if defined(__NetBSD__)
303 1.4.18.2 yamt IFQ_SET_READY(&ifp->if_snd);
304 1.4.18.2 yamt #endif
305 1.4.18.2 yamt SET_IFFUNC(ifp, fwip_start, fwip_ioctl, fwip_init, fwip_stop);
306 1.4.18.5 yamt ifp->if_flags = (IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST);
307 1.4.18.2 yamt ifp->if_snd.ifq_maxlen = TX_MAX_QUEUE;
308 1.4.18.3 yamt #ifdef DEVICE_POLLING
309 1.4.18.3 yamt ifp->if_capabilities |= IFCAP_POLLING;
310 1.4.18.3 yamt #endif
311 1.4.18.2 yamt
312 1.4.18.2 yamt s = splfwnet();
313 1.4.18.2 yamt FIREWIRE_IFATTACH(ifp, hwaddr);
314 1.4.18.2 yamt splx(s);
315 1.4.18.2 yamt
316 1.4.18.2 yamt FWIPDEBUG(ifp, "interface created\n");
317 1.4.18.2 yamt FW_ATTACH_RETURN(0);
318 1.4.18.2 yamt }
319 1.4.18.2 yamt
320 1.4.18.2 yamt IF_STOP(fwip)
321 1.4.18.2 yamt {
322 1.4.18.2 yamt IF_STOP_START(fwip, ifp, fwip);
323 1.4.18.2 yamt struct firewire_comm *fc;
324 1.4.18.2 yamt struct fw_xferq *xferq;
325 1.4.18.2 yamt struct fw_xfer *xfer, *next;
326 1.4.18.2 yamt int i;
327 1.4.18.2 yamt
328 1.4.18.2 yamt fc = fwip->fd.fc;
329 1.4.18.2 yamt
330 1.4.18.2 yamt if (fwip->dma_ch >= 0) {
331 1.4.18.2 yamt xferq = fc->ir[fwip->dma_ch];
332 1.4.18.2 yamt
333 1.4.18.2 yamt if (xferq->flag & FWXFERQ_RUNNING)
334 1.4.18.2 yamt fc->irx_disable(fc, fwip->dma_ch);
335 1.4.18.2 yamt xferq->flag &=
336 1.4.18.2 yamt ~(FWXFERQ_MODEMASK | FWXFERQ_OPEN | FWXFERQ_STREAM |
337 1.4.18.2 yamt FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_CHTAGMASK);
338 1.4.18.2 yamt xferq->hand = NULL;
339 1.4.18.2 yamt
340 1.4.18.2 yamt for (i = 0; i < xferq->bnchunk; i ++)
341 1.4.18.2 yamt m_freem(xferq->bulkxfer[i].mbuf);
342 1.4.18.2 yamt free(xferq->bulkxfer, M_FWIP);
343 1.4.18.2 yamt
344 1.4.18.2 yamt fw_bindremove(fc, &fwip->fwb);
345 1.4.18.2 yamt for (xfer = STAILQ_FIRST(&fwip->fwb.xferlist); xfer != NULL;
346 1.4.18.2 yamt xfer = next) {
347 1.4.18.2 yamt next = STAILQ_NEXT(xfer, link);
348 1.4.18.2 yamt fw_xfer_free(xfer);
349 1.4.18.2 yamt }
350 1.4.18.2 yamt
351 1.4.18.2 yamt for (xfer = STAILQ_FIRST(&fwip->xferlist); xfer != NULL;
352 1.4.18.2 yamt xfer = next) {
353 1.4.18.2 yamt next = STAILQ_NEXT(xfer, link);
354 1.4.18.2 yamt fw_xfer_free(xfer);
355 1.4.18.2 yamt }
356 1.4.18.2 yamt STAILQ_INIT(&fwip->xferlist);
357 1.4.18.2 yamt
358 1.4.18.2 yamt xferq->bulkxfer = NULL;
359 1.4.18.2 yamt fwip->dma_ch = -1;
360 1.4.18.2 yamt }
361 1.4.18.2 yamt
362 1.4.18.3 yamt #if defined(__FreeBSD__)
363 1.4.18.3 yamt ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
364 1.4.18.3 yamt #elif defined(__NetBSD__)
365 1.4.18.2 yamt ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
366 1.4.18.3 yamt #endif
367 1.4.18.2 yamt }
368 1.4.18.2 yamt
369 1.4.18.2 yamt FW_DETACH(fwip)
370 1.4.18.2 yamt {
371 1.4.18.2 yamt IF_DETACH_START(fwip, fwip);
372 1.4.18.3 yamt struct ifnet *ifp;
373 1.4.18.2 yamt int s;
374 1.4.18.2 yamt
375 1.4.18.3 yamt ifp = fwip->fw_softc.fwip_ifp;
376 1.4.18.3 yamt
377 1.4.18.3 yamt #ifdef DEVICE_POLLING
378 1.4.18.3 yamt if (ifp->if_capenable & IFCAP_POLLING)
379 1.4.18.3 yamt ether_poll_deregister(ifp);
380 1.4.18.3 yamt #endif
381 1.4.18.3 yamt
382 1.4.18.2 yamt s = splfwnet();
383 1.4.18.2 yamt
384 1.4.18.2 yamt FWIP_STOP(fwip);
385 1.4.18.3 yamt FIREWIRE_IFDETACH(ifp);
386 1.4.18.5 yamt fw_mtx_destroy(&fwip->mtx);
387 1.4.18.2 yamt
388 1.4.18.2 yamt splx(s);
389 1.4.18.2 yamt return 0;
390 1.4.18.2 yamt }
391 1.4.18.2 yamt
392 1.4.18.2 yamt #if defined(__NetBSD__)
393 1.4.18.2 yamt int
394 1.4.18.2 yamt fwipactivate(struct device *self, enum devact act)
395 1.4.18.2 yamt {
396 1.4.18.2 yamt struct fwip_softc *fwip = (struct fwip_softc *)self;
397 1.4.18.2 yamt int s, error = 0;
398 1.4.18.2 yamt
399 1.4.18.2 yamt s = splfwnet();
400 1.4.18.2 yamt switch (act) {
401 1.4.18.2 yamt case DVACT_ACTIVATE:
402 1.4.18.2 yamt error = EOPNOTSUPP;
403 1.4.18.2 yamt break;
404 1.4.18.2 yamt
405 1.4.18.2 yamt case DVACT_DEACTIVATE:
406 1.4.18.3 yamt if_deactivate(fwip->fw_softc.fwip_ifp);
407 1.4.18.2 yamt break;
408 1.4.18.2 yamt }
409 1.4.18.2 yamt splx(s);
410 1.4.18.2 yamt
411 1.4.18.2 yamt return (error);
412 1.4.18.2 yamt }
413 1.4.18.2 yamt
414 1.4.18.2 yamt #endif
415 1.4.18.2 yamt IF_INIT(fwip)
416 1.4.18.2 yamt {
417 1.4.18.2 yamt IF_INIT_START(fwip, fwip, ifp);
418 1.4.18.2 yamt struct firewire_comm *fc;
419 1.4.18.2 yamt struct fw_xferq *xferq;
420 1.4.18.2 yamt struct fw_xfer *xfer;
421 1.4.18.2 yamt struct mbuf *m;
422 1.4.18.2 yamt int i;
423 1.4.18.2 yamt
424 1.4.18.2 yamt FWIPDEBUG(ifp, "initializing\n");
425 1.4.18.2 yamt
426 1.4.18.2 yamt fc = fwip->fd.fc;
427 1.4.18.2 yamt #define START 0
428 1.4.18.2 yamt if (fwip->dma_ch < 0) {
429 1.4.18.5 yamt fwip->dma_ch = fw_open_isodma(fc, /* tx */0);
430 1.4.18.5 yamt if (fwip->dma_ch < 0)
431 1.4.18.5 yamt IF_INIT_RETURN(ENXIO);
432 1.4.18.5 yamt xferq = fc->ir[fwip->dma_ch];
433 1.4.18.5 yamt xferq->flag |=
434 1.4.18.5 yamt FWXFERQ_EXTBUF | FWXFERQ_HANDLER | FWXFERQ_STREAM;
435 1.4.18.2 yamt xferq->flag &= ~0xff;
436 1.4.18.2 yamt xferq->flag |= broadcast_channel & 0xff;
437 1.4.18.2 yamt /* register fwip_input handler */
438 1.4.18.3 yamt xferq->sc = (void *) fwip;
439 1.4.18.2 yamt xferq->hand = fwip_stream_input;
440 1.4.18.2 yamt xferq->bnchunk = rx_queue_len;
441 1.4.18.2 yamt xferq->bnpacket = 1;
442 1.4.18.2 yamt xferq->psize = MCLBYTES;
443 1.4.18.2 yamt xferq->queued = 0;
444 1.4.18.2 yamt xferq->buf = NULL;
445 1.4.18.2 yamt xferq->bulkxfer = (struct fw_bulkxfer *) malloc(
446 1.4.18.2 yamt sizeof(struct fw_bulkxfer) * xferq->bnchunk,
447 1.4.18.2 yamt M_FWIP, M_WAITOK);
448 1.4.18.2 yamt if (xferq->bulkxfer == NULL) {
449 1.4.18.2 yamt printf("if_fwip: malloc failed\n");
450 1.4.18.2 yamt IF_INIT_RETURN(ENOMEM);
451 1.4.18.2 yamt }
452 1.4.18.2 yamt STAILQ_INIT(&xferq->stvalid);
453 1.4.18.2 yamt STAILQ_INIT(&xferq->stfree);
454 1.4.18.2 yamt STAILQ_INIT(&xferq->stdma);
455 1.4.18.2 yamt xferq->stproc = NULL;
456 1.4.18.2 yamt for (i = 0; i < xferq->bnchunk; i ++) {
457 1.4.18.2 yamt m =
458 1.4.18.2 yamt #if defined(__DragonFly__) || __FreeBSD_version < 500000
459 1.4.18.2 yamt m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
460 1.4.18.2 yamt #else
461 1.4.18.2 yamt m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
462 1.4.18.2 yamt #endif
463 1.4.18.2 yamt xferq->bulkxfer[i].mbuf = m;
464 1.4.18.2 yamt if (m != NULL) {
465 1.4.18.2 yamt m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
466 1.4.18.2 yamt STAILQ_INSERT_TAIL(&xferq->stfree,
467 1.4.18.2 yamt &xferq->bulkxfer[i], link);
468 1.4.18.2 yamt } else
469 1.4.18.2 yamt printf("fwip_as_input: m_getcl failed\n");
470 1.4.18.2 yamt }
471 1.4.18.2 yamt
472 1.4.18.2 yamt fwip->fwb.start = INET_FIFO;
473 1.4.18.2 yamt fwip->fwb.end = INET_FIFO + 16384; /* S3200 packet size */
474 1.4.18.2 yamt
475 1.4.18.2 yamt /* pre-allocate xfer */
476 1.4.18.2 yamt STAILQ_INIT(&fwip->fwb.xferlist);
477 1.4.18.2 yamt for (i = 0; i < rx_queue_len; i ++) {
478 1.4.18.2 yamt xfer = fw_xfer_alloc(M_FWIP);
479 1.4.18.2 yamt if (xfer == NULL)
480 1.4.18.2 yamt break;
481 1.4.18.2 yamt m = m_getcl(M_TRYWAIT, MT_DATA, M_PKTHDR);
482 1.4.18.2 yamt xfer->recv.payload = mtod(m, uint32_t *);
483 1.4.18.2 yamt xfer->recv.pay_len = MCLBYTES;
484 1.4.18.2 yamt xfer->hand = fwip_unicast_input;
485 1.4.18.2 yamt xfer->fc = fc;
486 1.4.18.3 yamt xfer->sc = (void *)fwip;
487 1.4.18.2 yamt xfer->mbuf = m;
488 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
489 1.4.18.2 yamt }
490 1.4.18.2 yamt fw_bindadd(fc, &fwip->fwb);
491 1.4.18.2 yamt
492 1.4.18.2 yamt STAILQ_INIT(&fwip->xferlist);
493 1.4.18.2 yamt for (i = 0; i < TX_MAX_QUEUE; i++) {
494 1.4.18.2 yamt xfer = fw_xfer_alloc(M_FWIP);
495 1.4.18.2 yamt if (xfer == NULL)
496 1.4.18.2 yamt break;
497 1.4.18.2 yamt xfer->send.spd = tx_speed;
498 1.4.18.2 yamt xfer->fc = fwip->fd.fc;
499 1.4.18.3 yamt xfer->sc = (void *)fwip;
500 1.4.18.2 yamt xfer->hand = fwip_output_callback;
501 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
502 1.4.18.2 yamt }
503 1.4.18.2 yamt } else
504 1.4.18.2 yamt xferq = fc->ir[fwip->dma_ch];
505 1.4.18.2 yamt
506 1.4.18.2 yamt fwip->last_dest.hi = 0;
507 1.4.18.2 yamt fwip->last_dest.lo = 0;
508 1.4.18.2 yamt
509 1.4.18.2 yamt /* start dma */
510 1.4.18.2 yamt if ((xferq->flag & FWXFERQ_RUNNING) == 0)
511 1.4.18.2 yamt fc->irx_enable(fc, fwip->dma_ch);
512 1.4.18.2 yamt
513 1.4.18.3 yamt #if defined(__FreeBSD__)
514 1.4.18.3 yamt ifp->if_drv_flags |= IFF_DRV_RUNNING;
515 1.4.18.3 yamt ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
516 1.4.18.3 yamt #elif defined(__NetBSD__)
517 1.4.18.2 yamt ifp->if_flags |= IFF_RUNNING;
518 1.4.18.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
519 1.4.18.3 yamt #endif
520 1.4.18.2 yamt
521 1.4.18.2 yamt #if 0
522 1.4.18.2 yamt /* attempt to start output */
523 1.4.18.2 yamt fwip_start(ifp);
524 1.4.18.2 yamt #endif
525 1.4.18.2 yamt IF_INIT_RETURN(0);
526 1.4.18.2 yamt }
527 1.4.18.2 yamt
528 1.4.18.2 yamt static int
529 1.4.18.3 yamt fwip_ioctl(struct ifnet *ifp, u_long cmd, void *data)
530 1.4.18.2 yamt {
531 1.4.18.2 yamt IF_IOCTL_START(fwip, fwip);
532 1.4.18.2 yamt int s, error;
533 1.4.18.2 yamt
534 1.4.18.2 yamt switch (cmd) {
535 1.4.18.2 yamt case SIOCSIFFLAGS:
536 1.4.18.2 yamt s = splfwnet();
537 1.4.18.2 yamt if (ifp->if_flags & IFF_UP) {
538 1.4.18.3 yamt #if defined(__FreeBSD__)
539 1.4.18.3 yamt if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
540 1.4.18.3 yamt #elif defined(__NetBSD__)
541 1.4.18.2 yamt if (!(ifp->if_flags & IFF_RUNNING))
542 1.4.18.3 yamt #endif
543 1.4.18.2 yamt FWIP_INIT(fwip);
544 1.4.18.2 yamt } else {
545 1.4.18.3 yamt #if defined(__FreeBSD__)
546 1.4.18.3 yamt if (ifp->if_drv_flags & IFF_DRV_RUNNING)
547 1.4.18.3 yamt #elif defined(__NetBSD__)
548 1.4.18.2 yamt if (ifp->if_flags & IFF_RUNNING)
549 1.4.18.3 yamt #endif
550 1.4.18.2 yamt FWIP_STOP(fwip);
551 1.4.18.2 yamt }
552 1.4.18.2 yamt splx(s);
553 1.4.18.2 yamt break;
554 1.4.18.2 yamt case SIOCADDMULTI:
555 1.4.18.2 yamt case SIOCDELMULTI:
556 1.4.18.2 yamt break;
557 1.4.18.3 yamt case SIOCSIFCAP:
558 1.4.18.3 yamt #ifdef DEVICE_POLLING
559 1.4.18.3 yamt {
560 1.4.18.3 yamt struct ifreq *ifr = (struct ifreq *) data;
561 1.4.18.3 yamt struct firewire_comm *fc = fc = fwip->fd.fc;
562 1.4.18.3 yamt
563 1.4.18.3 yamt if (ifr->ifr_reqcap & IFCAP_POLLING &&
564 1.4.18.3 yamt !(ifp->if_capenable & IFCAP_POLLING)) {
565 1.4.18.3 yamt error = ether_poll_register(fwip_poll, ifp);
566 1.4.18.3 yamt if (error)
567 1.4.18.3 yamt return(error);
568 1.4.18.3 yamt /* Disable interrupts */
569 1.4.18.3 yamt fc->set_intr(fc, 0);
570 1.4.18.3 yamt ifp->if_capenable |= IFCAP_POLLING;
571 1.4.18.3 yamt return (error);
572 1.4.18.3 yamt
573 1.4.18.3 yamt }
574 1.4.18.3 yamt if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
575 1.4.18.3 yamt ifp->if_capenable & IFCAP_POLLING) {
576 1.4.18.3 yamt error = ether_poll_deregister(ifp);
577 1.4.18.3 yamt /* Enable interrupts. */
578 1.4.18.3 yamt fc->set_intr(fc, 1);
579 1.4.18.3 yamt ifp->if_capenable &= ~IFCAP_POLLING;
580 1.4.18.3 yamt return (error);
581 1.4.18.3 yamt }
582 1.4.18.3 yamt }
583 1.4.18.3 yamt #endif /* DEVICE_POLLING */
584 1.4.18.3 yamt break;
585 1.4.18.2 yamt
586 1.4.18.2 yamt #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__NetBSD__)
587 1.4.18.2 yamt default:
588 1.4.18.2 yamt #else
589 1.4.18.2 yamt case SIOCSIFADDR:
590 1.4.18.2 yamt case SIOCGIFADDR:
591 1.4.18.2 yamt case SIOCSIFMTU:
592 1.4.18.2 yamt #endif
593 1.4.18.2 yamt s = splfwnet();
594 1.4.18.2 yamt error = FIREWIRE_IOCTL(ifp, cmd, data);
595 1.4.18.2 yamt splx(s);
596 1.4.18.2 yamt return (error);
597 1.4.18.2 yamt #if defined(__DragonFly__) || \
598 1.4.18.2 yamt (defined(__FreeBSD__) && __FreeBSD_version < 500000)
599 1.4.18.2 yamt default:
600 1.4.18.2 yamt return (EINVAL);
601 1.4.18.2 yamt #endif
602 1.4.18.2 yamt }
603 1.4.18.2 yamt
604 1.4.18.2 yamt return (0);
605 1.4.18.2 yamt }
606 1.4.18.2 yamt
607 1.4.18.2 yamt static void
608 1.4.18.2 yamt fwip_post_busreset(void *arg)
609 1.4.18.2 yamt {
610 1.4.18.2 yamt struct fwip_softc *fwip = arg;
611 1.4.18.2 yamt struct crom_src *src;
612 1.4.18.2 yamt struct crom_chunk *root;
613 1.4.18.2 yamt
614 1.4.18.2 yamt src = fwip->fd.fc->crom_src;
615 1.4.18.2 yamt root = fwip->fd.fc->crom_root;
616 1.4.18.2 yamt
617 1.4.18.2 yamt /* RFC2734 IPv4 over IEEE1394 */
618 1.4.18.2 yamt bzero(&fwip->unit4, sizeof(struct crom_chunk));
619 1.4.18.2 yamt crom_add_chunk(src, root, &fwip->unit4, CROM_UDIR);
620 1.4.18.2 yamt crom_add_entry(&fwip->unit4, CSRKEY_SPEC, CSRVAL_IETF);
621 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit4, &fwip->spec4, "IANA");
622 1.4.18.2 yamt crom_add_entry(&fwip->unit4, CSRKEY_VER, 1);
623 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit4, &fwip->ver4, "IPv4");
624 1.4.18.2 yamt
625 1.4.18.2 yamt /* RFC3146 IPv6 over IEEE1394 */
626 1.4.18.2 yamt bzero(&fwip->unit6, sizeof(struct crom_chunk));
627 1.4.18.2 yamt crom_add_chunk(src, root, &fwip->unit6, CROM_UDIR);
628 1.4.18.2 yamt crom_add_entry(&fwip->unit6, CSRKEY_SPEC, CSRVAL_IETF);
629 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit6, &fwip->spec6, "IANA");
630 1.4.18.2 yamt crom_add_entry(&fwip->unit6, CSRKEY_VER, 2);
631 1.4.18.2 yamt crom_add_simple_text(src, &fwip->unit6, &fwip->ver6, "IPv6");
632 1.4.18.2 yamt
633 1.4.18.2 yamt fwip->last_dest.hi = 0;
634 1.4.18.2 yamt fwip->last_dest.lo = 0;
635 1.4.18.3 yamt FIREWIRE_BUSRESET(fwip->fw_softc.fwip_ifp);
636 1.4.18.2 yamt }
637 1.4.18.2 yamt
638 1.4.18.2 yamt static void
639 1.4.18.2 yamt fwip_output_callback(struct fw_xfer *xfer)
640 1.4.18.2 yamt {
641 1.4.18.2 yamt struct fwip_softc *fwip;
642 1.4.18.2 yamt struct ifnet *ifp;
643 1.4.18.2 yamt int s;
644 1.4.18.2 yamt
645 1.4.18.2 yamt fwip = (struct fwip_softc *)xfer->sc;
646 1.4.18.3 yamt ifp = fwip->fw_softc.fwip_ifp;
647 1.4.18.2 yamt /* XXX error check */
648 1.4.18.2 yamt FWIPDEBUG(ifp, "resp = %d\n", xfer->resp);
649 1.4.18.2 yamt if (xfer->resp != 0)
650 1.4.18.2 yamt ifp->if_oerrors ++;
651 1.4.18.2 yamt
652 1.4.18.2 yamt m_freem(xfer->mbuf);
653 1.4.18.2 yamt fw_xfer_unload(xfer);
654 1.4.18.2 yamt
655 1.4.18.2 yamt s = splfwnet();
656 1.4.18.5 yamt FWIP_LOCK(fwip);
657 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
658 1.4.18.5 yamt FWIP_UNLOCK(fwip);
659 1.4.18.2 yamt splx(s);
660 1.4.18.2 yamt
661 1.4.18.2 yamt /* for queue full */
662 1.4.18.5 yamt if (ifp->if_snd.ifq_head != NULL) {
663 1.4.18.2 yamt fwip_start(ifp);
664 1.4.18.5 yamt }
665 1.4.18.2 yamt }
666 1.4.18.2 yamt
667 1.4.18.2 yamt static void
668 1.4.18.2 yamt fwip_start(struct ifnet *ifp)
669 1.4.18.2 yamt {
670 1.4.18.3 yamt struct fwip_softc *fwip =
671 1.4.18.3 yamt ((struct fwip_eth_softc *)ifp->if_softc)->fwip;
672 1.4.18.2 yamt int s;
673 1.4.18.2 yamt
674 1.4.18.2 yamt FWIPDEBUG(ifp, "starting\n");
675 1.4.18.2 yamt
676 1.4.18.2 yamt if (fwip->dma_ch < 0) {
677 1.4.18.2 yamt struct mbuf *m = NULL;
678 1.4.18.2 yamt
679 1.4.18.2 yamt FWIPDEBUG(ifp, "not ready\n");
680 1.4.18.2 yamt
681 1.4.18.2 yamt s = splfwnet();
682 1.4.18.2 yamt do {
683 1.4.18.2 yamt IF_DEQUEUE(&ifp->if_snd, m);
684 1.4.18.2 yamt if (m != NULL)
685 1.4.18.2 yamt m_freem(m);
686 1.4.18.2 yamt ifp->if_oerrors ++;
687 1.4.18.2 yamt } while (m != NULL);
688 1.4.18.2 yamt splx(s);
689 1.4.18.2 yamt
690 1.4.18.2 yamt return;
691 1.4.18.2 yamt }
692 1.4.18.2 yamt
693 1.4.18.2 yamt s = splfwnet();
694 1.4.18.3 yamt #if defined(__FreeBSD__)
695 1.4.18.3 yamt ifp->if_drv_flags |= IFF_DRV_OACTIVE;
696 1.4.18.3 yamt #elif defined(__NetBSD__)
697 1.4.18.2 yamt ifp->if_flags |= IFF_OACTIVE;
698 1.4.18.3 yamt #endif
699 1.4.18.2 yamt
700 1.4.18.2 yamt if (ifp->if_snd.ifq_len != 0)
701 1.4.18.2 yamt fwip_async_output(fwip, ifp);
702 1.4.18.2 yamt
703 1.4.18.3 yamt #if defined(__FreeBSD__)
704 1.4.18.3 yamt ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
705 1.4.18.3 yamt #elif defined(__NetBSD__)
706 1.4.18.2 yamt ifp->if_flags &= ~IFF_OACTIVE;
707 1.4.18.3 yamt #endif
708 1.4.18.2 yamt splx(s);
709 1.4.18.2 yamt }
710 1.4.18.2 yamt
711 1.4.18.2 yamt /* Async. stream output */
712 1.4.18.2 yamt static void
713 1.4.18.2 yamt fwip_async_output(struct fwip_softc *fwip, struct ifnet *ifp)
714 1.4.18.2 yamt {
715 1.4.18.2 yamt struct firewire_comm *fc = fwip->fd.fc;
716 1.4.18.2 yamt struct mbuf *m;
717 1.4.18.2 yamt struct m_tag *mtag;
718 1.4.18.2 yamt struct fw_hwaddr *destfw;
719 1.4.18.2 yamt struct fw_xfer *xfer;
720 1.4.18.2 yamt struct fw_xferq *xferq;
721 1.4.18.2 yamt struct fw_pkt *fp;
722 1.4.18.2 yamt uint16_t nodeid;
723 1.4.18.2 yamt int error;
724 1.4.18.2 yamt int i = 0;
725 1.4.18.2 yamt
726 1.4.18.2 yamt xfer = NULL;
727 1.4.18.5 yamt xferq = fc->atq;
728 1.4.18.5 yamt while ((xferq->queued < xferq->maxq - 1) &&
729 1.4.18.5 yamt (ifp->if_snd.ifq_head != NULL)) {
730 1.4.18.5 yamt FWIP_LOCK(fwip);
731 1.4.18.2 yamt xfer = STAILQ_FIRST(&fwip->xferlist);
732 1.4.18.2 yamt if (xfer == NULL) {
733 1.4.18.5 yamt FWIP_UNLOCK(fwip);
734 1.4.18.5 yamt #if 0
735 1.4.18.2 yamt printf("if_fwip: lack of xfer\n");
736 1.4.18.5 yamt #endif
737 1.4.18.5 yamt break;
738 1.4.18.2 yamt }
739 1.4.18.5 yamt STAILQ_REMOVE_HEAD(&fwip->xferlist, link);
740 1.4.18.5 yamt FWIP_UNLOCK(fwip);
741 1.4.18.5 yamt
742 1.4.18.2 yamt IF_DEQUEUE(&ifp->if_snd, m);
743 1.4.18.5 yamt if (m == NULL) {
744 1.4.18.5 yamt FWIP_LOCK(fwip);
745 1.4.18.5 yamt STAILQ_INSERT_HEAD(&fwip->xferlist, xfer, link);
746 1.4.18.5 yamt FWIP_UNLOCK(fwip);
747 1.4.18.2 yamt break;
748 1.4.18.5 yamt }
749 1.4.18.2 yamt
750 1.4.18.2 yamt /*
751 1.4.18.2 yamt * Dig out the link-level address which
752 1.4.18.2 yamt * firewire_output got via arp or neighbour
753 1.4.18.2 yamt * discovery. If we don't have a link-level address,
754 1.4.18.2 yamt * just stick the thing on the broadcast channel.
755 1.4.18.2 yamt */
756 1.4.18.2 yamt mtag = m_tag_locate(m, MTAG_FIREWIRE, MTAG_FIREWIRE_HWADDR, 0);
757 1.4.18.2 yamt if (mtag == NULL)
758 1.4.18.2 yamt destfw = 0;
759 1.4.18.2 yamt else
760 1.4.18.2 yamt destfw = (struct fw_hwaddr *) (mtag + 1);
761 1.4.18.2 yamt
762 1.4.18.2 yamt /*
763 1.4.18.2 yamt * We don't do any bpf stuff here - the generic code
764 1.4.18.2 yamt * in firewire_output gives the packet to bpf before
765 1.4.18.2 yamt * it adds the link-level encapsulation.
766 1.4.18.2 yamt */
767 1.4.18.2 yamt
768 1.4.18.2 yamt /*
769 1.4.18.2 yamt * Put the mbuf in the xfer early in case we hit an
770 1.4.18.2 yamt * error case below - fwip_output_callback will free
771 1.4.18.2 yamt * the mbuf.
772 1.4.18.2 yamt */
773 1.4.18.2 yamt xfer->mbuf = m;
774 1.4.18.2 yamt
775 1.4.18.2 yamt /*
776 1.4.18.2 yamt * We use the arp result (if any) to add a suitable firewire
777 1.4.18.2 yamt * packet header before handing off to the bus.
778 1.4.18.2 yamt */
779 1.4.18.2 yamt fp = &xfer->send.hdr;
780 1.4.18.2 yamt nodeid = FWLOCALBUS | fc->nodeid;
781 1.4.18.2 yamt if ((m->m_flags & M_BCAST) || !destfw) {
782 1.4.18.2 yamt /*
783 1.4.18.2 yamt * Broadcast packets are sent as GASP packets with
784 1.4.18.2 yamt * specifier ID 0x00005e, version 1 on the broadcast
785 1.4.18.2 yamt * channel. To be conservative, we send at the
786 1.4.18.2 yamt * slowest possible speed.
787 1.4.18.2 yamt */
788 1.4.18.2 yamt uint32_t *p;
789 1.4.18.2 yamt
790 1.4.18.2 yamt M_PREPEND(m, 2*sizeof(uint32_t), M_DONTWAIT);
791 1.4.18.2 yamt p = mtod(m, uint32_t *);
792 1.4.18.2 yamt fp->mode.stream.len = m->m_pkthdr.len;
793 1.4.18.2 yamt fp->mode.stream.chtag = broadcast_channel;
794 1.4.18.2 yamt fp->mode.stream.tcode = FWTCODE_STREAM;
795 1.4.18.2 yamt fp->mode.stream.sy = 0;
796 1.4.18.2 yamt xfer->send.spd = 0;
797 1.4.18.2 yamt p[0] = htonl(nodeid << 16);
798 1.4.18.2 yamt p[1] = htonl((0x5e << 24) | 1);
799 1.4.18.2 yamt } else {
800 1.4.18.2 yamt /*
801 1.4.18.2 yamt * Unicast packets are sent as block writes to the
802 1.4.18.2 yamt * target's unicast fifo address. If we can't
803 1.4.18.2 yamt * find the node address, we just give up. We
804 1.4.18.2 yamt * could broadcast it but that might overflow
805 1.4.18.2 yamt * the packet size limitations due to the
806 1.4.18.2 yamt * extra GASP header. Note: the hardware
807 1.4.18.2 yamt * address is stored in network byte order to
808 1.4.18.2 yamt * make life easier for ARP.
809 1.4.18.2 yamt */
810 1.4.18.2 yamt struct fw_device *fd;
811 1.4.18.2 yamt struct fw_eui64 eui;
812 1.4.18.2 yamt
813 1.4.18.2 yamt eui.hi = ntohl(destfw->sender_unique_ID_hi);
814 1.4.18.2 yamt eui.lo = ntohl(destfw->sender_unique_ID_lo);
815 1.4.18.2 yamt if (fwip->last_dest.hi != eui.hi ||
816 1.4.18.2 yamt fwip->last_dest.lo != eui.lo) {
817 1.4.18.2 yamt fd = fw_noderesolve_eui64(fc, &eui);
818 1.4.18.2 yamt if (!fd) {
819 1.4.18.2 yamt /* error */
820 1.4.18.2 yamt ifp->if_oerrors ++;
821 1.4.18.2 yamt /* XXX set error code */
822 1.4.18.2 yamt fwip_output_callback(xfer);
823 1.4.18.2 yamt continue;
824 1.4.18.2 yamt
825 1.4.18.2 yamt }
826 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.dst = FWLOCALBUS | fd->dst;
827 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.tlrt = 0;
828 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.tcode = FWTCODE_WREQB;
829 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.pri = 0;
830 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.src = nodeid;
831 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.dest_hi =
832 1.4.18.2 yamt ntohs(destfw->sender_unicast_FIFO_hi);
833 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.dest_lo =
834 1.4.18.2 yamt ntohl(destfw->sender_unicast_FIFO_lo);
835 1.4.18.2 yamt fwip->last_hdr.mode.wreqb.extcode = 0;
836 1.4.18.2 yamt fwip->last_dest = eui;
837 1.4.18.2 yamt }
838 1.4.18.2 yamt
839 1.4.18.2 yamt fp->mode.wreqb = fwip->last_hdr.mode.wreqb;
840 1.4.18.2 yamt fp->mode.wreqb.len = m->m_pkthdr.len;
841 1.4.18.2 yamt xfer->send.spd = min(destfw->sspd, fc->speed);
842 1.4.18.2 yamt }
843 1.4.18.2 yamt
844 1.4.18.2 yamt xfer->send.pay_len = m->m_pkthdr.len;
845 1.4.18.2 yamt
846 1.4.18.2 yamt error = fw_asyreq(fc, -1, xfer);
847 1.4.18.2 yamt if (error == EAGAIN) {
848 1.4.18.2 yamt /*
849 1.4.18.2 yamt * We ran out of tlabels - requeue the packet
850 1.4.18.2 yamt * for later transmission.
851 1.4.18.2 yamt */
852 1.4.18.2 yamt xfer->mbuf = 0;
853 1.4.18.5 yamt FWIP_LOCK(fwip);
854 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->xferlist, xfer, link);
855 1.4.18.5 yamt FWIP_UNLOCK(fwip);
856 1.4.18.2 yamt IF_PREPEND(&ifp->if_snd, m);
857 1.4.18.2 yamt break;
858 1.4.18.2 yamt }
859 1.4.18.2 yamt if (error) {
860 1.4.18.2 yamt /* error */
861 1.4.18.2 yamt ifp->if_oerrors ++;
862 1.4.18.2 yamt /* XXX set error code */
863 1.4.18.2 yamt fwip_output_callback(xfer);
864 1.4.18.2 yamt continue;
865 1.4.18.2 yamt } else {
866 1.4.18.2 yamt ifp->if_opackets ++;
867 1.4.18.2 yamt i++;
868 1.4.18.2 yamt }
869 1.4.18.2 yamt }
870 1.4.18.2 yamt #if 0
871 1.4.18.2 yamt if (i > 1)
872 1.4.18.2 yamt printf("%d queued\n", i);
873 1.4.18.2 yamt #endif
874 1.4.18.5 yamt if (i > 0)
875 1.4.18.2 yamt xferq->start(fc);
876 1.4.18.2 yamt }
877 1.4.18.2 yamt
878 1.4.18.2 yamt static void
879 1.4.18.2 yamt fwip_start_send (void *arg, int count)
880 1.4.18.2 yamt {
881 1.4.18.2 yamt struct fwip_softc *fwip = arg;
882 1.4.18.2 yamt
883 1.4.18.2 yamt fwip->fd.fc->atq->start(fwip->fd.fc);
884 1.4.18.2 yamt }
885 1.4.18.2 yamt
886 1.4.18.2 yamt /* Async. stream output */
887 1.4.18.2 yamt static void
888 1.4.18.2 yamt fwip_stream_input(struct fw_xferq *xferq)
889 1.4.18.2 yamt {
890 1.4.18.2 yamt struct mbuf *m, *m0;
891 1.4.18.2 yamt struct m_tag *mtag;
892 1.4.18.2 yamt struct ifnet *ifp;
893 1.4.18.2 yamt struct fwip_softc *fwip;
894 1.4.18.2 yamt struct fw_bulkxfer *sxfer;
895 1.4.18.2 yamt struct fw_pkt *fp;
896 1.4.18.2 yamt uint16_t src;
897 1.4.18.2 yamt uint32_t *p;
898 1.4.18.2 yamt
899 1.4.18.2 yamt fwip = (struct fwip_softc *)xferq->sc;
900 1.4.18.3 yamt ifp = fwip->fw_softc.fwip_ifp;
901 1.4.18.2 yamt while ((sxfer = STAILQ_FIRST(&xferq->stvalid)) != NULL) {
902 1.4.18.2 yamt STAILQ_REMOVE_HEAD(&xferq->stvalid, link);
903 1.4.18.2 yamt fp = mtod(sxfer->mbuf, struct fw_pkt *);
904 1.4.18.2 yamt if (fwip->fd.fc->irx_post != NULL)
905 1.4.18.2 yamt fwip->fd.fc->irx_post(fwip->fd.fc, fp->mode.ld);
906 1.4.18.2 yamt m = sxfer->mbuf;
907 1.4.18.2 yamt
908 1.4.18.2 yamt /* insert new rbuf */
909 1.4.18.2 yamt sxfer->mbuf = m0 = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
910 1.4.18.2 yamt if (m0 != NULL) {
911 1.4.18.2 yamt m0->m_len = m0->m_pkthdr.len = m0->m_ext.ext_size;
912 1.4.18.2 yamt STAILQ_INSERT_TAIL(&xferq->stfree, sxfer, link);
913 1.4.18.2 yamt } else
914 1.4.18.2 yamt printf("fwip_as_input: m_getcl failed\n");
915 1.4.18.2 yamt
916 1.4.18.2 yamt /*
917 1.4.18.2 yamt * We must have a GASP header - leave the
918 1.4.18.2 yamt * encapsulation sanity checks to the generic
919 1.4.18.2 yamt * code. Remeber that we also have the firewire async
920 1.4.18.2 yamt * stream header even though that isn't accounted for
921 1.4.18.2 yamt * in mode.stream.len.
922 1.4.18.2 yamt */
923 1.4.18.2 yamt if (sxfer->resp != 0 || fp->mode.stream.len <
924 1.4.18.2 yamt 2*sizeof(uint32_t)) {
925 1.4.18.2 yamt m_freem(m);
926 1.4.18.2 yamt ifp->if_ierrors ++;
927 1.4.18.2 yamt continue;
928 1.4.18.2 yamt }
929 1.4.18.2 yamt m->m_len = m->m_pkthdr.len = fp->mode.stream.len
930 1.4.18.2 yamt + sizeof(fp->mode.stream);
931 1.4.18.2 yamt
932 1.4.18.2 yamt /*
933 1.4.18.2 yamt * If we received the packet on the broadcast channel,
934 1.4.18.2 yamt * mark it as broadcast, otherwise we assume it must
935 1.4.18.2 yamt * be multicast.
936 1.4.18.2 yamt */
937 1.4.18.2 yamt if (fp->mode.stream.chtag == broadcast_channel)
938 1.4.18.2 yamt m->m_flags |= M_BCAST;
939 1.4.18.2 yamt else
940 1.4.18.2 yamt m->m_flags |= M_MCAST;
941 1.4.18.2 yamt
942 1.4.18.2 yamt /*
943 1.4.18.2 yamt * Make sure we recognise the GASP specifier and
944 1.4.18.2 yamt * version.
945 1.4.18.2 yamt */
946 1.4.18.2 yamt p = mtod(m, uint32_t *);
947 1.4.18.2 yamt if ((((ntohl(p[1]) & 0xffff) << 8) | ntohl(p[2]) >> 24) != 0x00005e
948 1.4.18.2 yamt || (ntohl(p[2]) & 0xffffff) != 1) {
949 1.4.18.2 yamt FWIPDEBUG(ifp, "Unrecognised GASP header %#08x %#08x\n",
950 1.4.18.2 yamt ntohl(p[1]), ntohl(p[2]));
951 1.4.18.2 yamt m_freem(m);
952 1.4.18.2 yamt ifp->if_ierrors ++;
953 1.4.18.2 yamt continue;
954 1.4.18.2 yamt }
955 1.4.18.2 yamt
956 1.4.18.2 yamt /*
957 1.4.18.2 yamt * Record the sender ID for possible BPF usage.
958 1.4.18.2 yamt */
959 1.4.18.2 yamt src = ntohl(p[1]) >> 16;
960 1.4.18.3 yamt if (bpf_peers_present(ifp->if_bpf)) {
961 1.4.18.2 yamt mtag = m_tag_alloc(MTAG_FIREWIRE,
962 1.4.18.2 yamt MTAG_FIREWIRE_SENDER_EUID,
963 1.4.18.2 yamt 2*sizeof(uint32_t), M_NOWAIT);
964 1.4.18.2 yamt if (mtag) {
965 1.4.18.2 yamt /* bpf wants it in network byte order */
966 1.4.18.2 yamt struct fw_device *fd;
967 1.4.18.2 yamt uint32_t *p2 = (uint32_t *) (mtag + 1);
968 1.4.18.2 yamt fd = fw_noderesolve_nodeid(fwip->fd.fc,
969 1.4.18.2 yamt src & 0x3f);
970 1.4.18.2 yamt if (fd) {
971 1.4.18.2 yamt p2[0] = htonl(fd->eui.hi);
972 1.4.18.2 yamt p2[1] = htonl(fd->eui.lo);
973 1.4.18.2 yamt } else {
974 1.4.18.2 yamt p2[0] = 0;
975 1.4.18.2 yamt p2[1] = 0;
976 1.4.18.2 yamt }
977 1.4.18.2 yamt m_tag_prepend(m, mtag);
978 1.4.18.2 yamt }
979 1.4.18.2 yamt }
980 1.4.18.2 yamt
981 1.4.18.2 yamt /*
982 1.4.18.2 yamt * Trim off the GASP header
983 1.4.18.2 yamt */
984 1.4.18.2 yamt m_adj(m, 3*sizeof(uint32_t));
985 1.4.18.2 yamt m->m_pkthdr.rcvif = ifp;
986 1.4.18.2 yamt FIREWIRE_INPUT(ifp, m, src);
987 1.4.18.2 yamt ifp->if_ipackets ++;
988 1.4.18.2 yamt }
989 1.4.18.2 yamt if (STAILQ_FIRST(&xferq->stfree) != NULL)
990 1.4.18.2 yamt fwip->fd.fc->irx_enable(fwip->fd.fc, fwip->dma_ch);
991 1.4.18.2 yamt }
992 1.4.18.2 yamt
993 1.4.18.2 yamt static inline void
994 1.4.18.2 yamt fwip_unicast_input_recycle(struct fwip_softc *fwip, struct fw_xfer *xfer)
995 1.4.18.2 yamt {
996 1.4.18.2 yamt struct mbuf *m;
997 1.4.18.2 yamt
998 1.4.18.2 yamt /*
999 1.4.18.2 yamt * We have finished with a unicast xfer. Allocate a new
1000 1.4.18.2 yamt * cluster and stick it on the back of the input queue.
1001 1.4.18.2 yamt */
1002 1.4.18.2 yamt m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1003 1.4.18.2 yamt if (m == NULL)
1004 1.4.18.2 yamt printf("fwip_unicast_input_recycle: m_getcl failed\n");
1005 1.4.18.2 yamt xfer->mbuf = m;
1006 1.4.18.2 yamt xfer->recv.payload = mtod(m, uint32_t *);
1007 1.4.18.2 yamt xfer->recv.pay_len = MCLBYTES;
1008 1.4.18.2 yamt xfer->mbuf = m;
1009 1.4.18.2 yamt STAILQ_INSERT_TAIL(&fwip->fwb.xferlist, xfer, link);
1010 1.4.18.2 yamt }
1011 1.4.18.2 yamt
1012 1.4.18.2 yamt static void
1013 1.4.18.2 yamt fwip_unicast_input(struct fw_xfer *xfer)
1014 1.4.18.2 yamt {
1015 1.4.18.2 yamt uint64_t address;
1016 1.4.18.2 yamt struct mbuf *m;
1017 1.4.18.2 yamt struct m_tag *mtag;
1018 1.4.18.2 yamt struct ifnet *ifp;
1019 1.4.18.2 yamt struct fwip_softc *fwip;
1020 1.4.18.2 yamt struct fw_pkt *fp;
1021 1.4.18.2 yamt //struct fw_pkt *sfp;
1022 1.4.18.2 yamt int rtcode;
1023 1.4.18.2 yamt
1024 1.4.18.2 yamt fwip = (struct fwip_softc *)xfer->sc;
1025 1.4.18.3 yamt ifp = fwip->fw_softc.fwip_ifp;
1026 1.4.18.2 yamt m = xfer->mbuf;
1027 1.4.18.2 yamt xfer->mbuf = 0;
1028 1.4.18.2 yamt fp = &xfer->recv.hdr;
1029 1.4.18.2 yamt
1030 1.4.18.2 yamt /*
1031 1.4.18.2 yamt * Check the fifo address - we only accept addresses of
1032 1.4.18.2 yamt * exactly INET_FIFO.
1033 1.4.18.2 yamt */
1034 1.4.18.2 yamt address = ((uint64_t)fp->mode.wreqb.dest_hi << 32)
1035 1.4.18.2 yamt | fp->mode.wreqb.dest_lo;
1036 1.4.18.2 yamt if (fp->mode.wreqb.tcode != FWTCODE_WREQB) {
1037 1.4.18.2 yamt rtcode = FWRCODE_ER_TYPE;
1038 1.4.18.2 yamt } else if (address != INET_FIFO) {
1039 1.4.18.2 yamt rtcode = FWRCODE_ER_ADDR;
1040 1.4.18.2 yamt } else {
1041 1.4.18.2 yamt rtcode = FWRCODE_COMPLETE;
1042 1.4.18.2 yamt }
1043 1.4.18.2 yamt
1044 1.4.18.2 yamt /*
1045 1.4.18.2 yamt * Pick up a new mbuf and stick it on the back of the receive
1046 1.4.18.2 yamt * queue.
1047 1.4.18.2 yamt */
1048 1.4.18.2 yamt fwip_unicast_input_recycle(fwip, xfer);
1049 1.4.18.2 yamt
1050 1.4.18.2 yamt /*
1051 1.4.18.2 yamt * If we've already rejected the packet, give up now.
1052 1.4.18.2 yamt */
1053 1.4.18.2 yamt if (rtcode != FWRCODE_COMPLETE) {
1054 1.4.18.2 yamt m_freem(m);
1055 1.4.18.2 yamt ifp->if_ierrors ++;
1056 1.4.18.2 yamt return;
1057 1.4.18.2 yamt }
1058 1.4.18.2 yamt
1059 1.4.18.3 yamt if (bpf_peers_present(ifp->if_bpf)) {
1060 1.4.18.2 yamt /*
1061 1.4.18.2 yamt * Record the sender ID for possible BPF usage.
1062 1.4.18.2 yamt */
1063 1.4.18.2 yamt mtag = m_tag_alloc(MTAG_FIREWIRE, MTAG_FIREWIRE_SENDER_EUID,
1064 1.4.18.2 yamt 2*sizeof(uint32_t), M_NOWAIT);
1065 1.4.18.2 yamt if (mtag) {
1066 1.4.18.2 yamt /* bpf wants it in network byte order */
1067 1.4.18.2 yamt struct fw_device *fd;
1068 1.4.18.2 yamt uint32_t *p = (uint32_t *) (mtag + 1);
1069 1.4.18.2 yamt fd = fw_noderesolve_nodeid(fwip->fd.fc,
1070 1.4.18.2 yamt fp->mode.wreqb.src & 0x3f);
1071 1.4.18.2 yamt if (fd) {
1072 1.4.18.2 yamt p[0] = htonl(fd->eui.hi);
1073 1.4.18.2 yamt p[1] = htonl(fd->eui.lo);
1074 1.4.18.2 yamt } else {
1075 1.4.18.2 yamt p[0] = 0;
1076 1.4.18.2 yamt p[1] = 0;
1077 1.4.18.2 yamt }
1078 1.4.18.2 yamt m_tag_prepend(m, mtag);
1079 1.4.18.2 yamt }
1080 1.4.18.2 yamt }
1081 1.4.18.2 yamt
1082 1.4.18.2 yamt /*
1083 1.4.18.2 yamt * Hand off to the generic encapsulation code. We don't use
1084 1.4.18.2 yamt * ifp->if_input so that we can pass the source nodeid as an
1085 1.4.18.2 yamt * argument to facilitate link-level fragment reassembly.
1086 1.4.18.2 yamt */
1087 1.4.18.2 yamt m->m_len = m->m_pkthdr.len = fp->mode.wreqb.len;
1088 1.4.18.2 yamt m->m_pkthdr.rcvif = ifp;
1089 1.4.18.2 yamt FIREWIRE_INPUT(ifp, m, fp->mode.wreqb.src);
1090 1.4.18.2 yamt ifp->if_ipackets ++;
1091 1.4.18.2 yamt }
1092 1.4.18.2 yamt
1093 1.4.18.2 yamt #if defined(__FreeBSD__)
1094 1.4.18.2 yamt static devclass_t fwip_devclass;
1095 1.4.18.2 yamt
1096 1.4.18.2 yamt static device_method_t fwip_methods[] = {
1097 1.4.18.2 yamt /* device interface */
1098 1.4.18.2 yamt DEVMETHOD(device_identify, fwip_identify),
1099 1.4.18.2 yamt DEVMETHOD(device_probe, fwip_probe),
1100 1.4.18.2 yamt DEVMETHOD(device_attach, fwip_attach),
1101 1.4.18.2 yamt DEVMETHOD(device_detach, fwip_detach),
1102 1.4.18.2 yamt { 0, 0 }
1103 1.4.18.2 yamt };
1104 1.4.18.2 yamt
1105 1.4.18.2 yamt static driver_t fwip_driver = {
1106 1.4.18.2 yamt "fwip",
1107 1.4.18.2 yamt fwip_methods,
1108 1.4.18.2 yamt sizeof(struct fwip_softc),
1109 1.4.18.2 yamt };
1110 1.4.18.2 yamt
1111 1.4.18.2 yamt
1112 1.4.18.2 yamt #ifdef __DragonFly__
1113 1.4.18.2 yamt DECLARE_DUMMY_MODULE(fwip);
1114 1.4.18.2 yamt #endif
1115 1.4.18.2 yamt DRIVER_MODULE(fwip, firewire, fwip_driver, fwip_devclass, 0, 0);
1116 1.4.18.2 yamt MODULE_VERSION(fwip, 1);
1117 1.4.18.2 yamt MODULE_DEPEND(fwip, firewire, 1, 1, 1);
1118 1.4.18.2 yamt #elif defined(__NetBSD__)
1119 1.4.18.2 yamt CFATTACH_DECL(fwip, sizeof (struct fwip_softc),
1120 1.4.18.2 yamt fwipmatch, fwipattach, fwipdetach, NULL);
1121 1.4.18.2 yamt #endif
1122