Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / dev / netif / ic / if_ic.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1998 Nicolas Souchu
3 * 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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: src/sys/dev/iicbus/if_ic.c,v 1.8 1999/12/29 04:35:39 peter Exp $
78195a76 27 * $DragonFly: src/sys/dev/netif/ic/if_ic.c,v 1.14 2005/11/28 17:13:42 dillon Exp $
984263bc
MD
28 */
29
30/*
31 * I2C bus IP driver
32 */
33
34#ifdef _KERNEL
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/proc.h>
38#include <sys/mbuf.h>
39#include <sys/socket.h>
40#include <sys/filio.h>
41#include <sys/sockio.h>
42#include <sys/kernel.h>
43#include <sys/module.h>
44#include <sys/bus.h>
45#include <sys/time.h>
46#include <sys/malloc.h>
9c2a771a 47#include <sys/thread2.h>
984263bc
MD
48
49#include <net/if.h>
0e23cec8 50#include <net/ifq_var.h>
984263bc
MD
51#include <net/if_types.h>
52#include <net/netisr.h>
53
54#endif
55#include <sys/mbuf.h>
56#include <sys/socket.h>
57#include <net/netisr.h>
58#include <net/route.h>
59#include <netinet/in.h>
60#include <netinet/in_systm.h>
61#include <netinet/in_var.h>
62#include <netinet/ip.h>
63#include <netinet/if_ether.h>
64
65#include <net/bpf.h>
66
1f2de5d4
MD
67#include <bus/iicbus/iiconf.h>
68#include <bus/iicbus/iicbus.h>
984263bc
MD
69
70#include "iicbus_if.h"
71
1f8e62c9 72#define ICHDRLEN sizeof(uint32_t)
984263bc
MD
73#define ICMTU 1500 /* default mtu */
74
75struct ic_softc {
76 struct ifnet ic_if;
77
78 u_char ic_addr; /* peer I2C address */
79
80 int ic_sending;
81
82 char *ic_obuf;
83 char *ic_ifbuf;
84 char *ic_cp;
85
86 int ic_xfercnt;
87
88 int ic_iferrs;
89};
90
91static devclass_t ic_devclass;
92
93static int icprobe(device_t);
94static int icattach(device_t);
95
9974b71d 96static int icioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
984263bc
MD
97static int icoutput(struct ifnet *, struct mbuf *, struct sockaddr *,
98 struct rtentry *);
99
100static void icintr(device_t, int, char *);
101
102static device_method_t ic_methods[] = {
103 /* device interface */
104 DEVMETHOD(device_probe, icprobe),
105 DEVMETHOD(device_attach, icattach),
106
107 /* iicbus interface */
108 DEVMETHOD(iicbus_intr, icintr),
109
110 { 0, 0 }
111};
112
113static driver_t ic_driver = {
114 "ic",
115 ic_methods,
116 sizeof(struct ic_softc),
117};
118
119/*
120 * icprobe()
121 */
122static int
123icprobe(device_t dev)
124{
125 return (0);
126}
127
128/*
129 * icattach()
130 */
131static int
132icattach(device_t dev)
133{
134 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
135 struct ifnet *ifp = &sc->ic_if;
136
137 sc->ic_addr = iicbus_get_addr(dev);
138
139 ifp->if_softc = sc;
3e4a09e7 140 if_initname(ifp, "ic", device_get_unit(dev));
984263bc
MD
141 ifp->if_mtu = ICMTU;
142 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
143 ifp->if_ioctl = icioctl;
144 ifp->if_output = icoutput;
145 ifp->if_type = IFT_PARA;
146 ifp->if_hdrlen = 0;
147 ifp->if_addrlen = 0;
0e23cec8 148 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
984263bc 149
78195a76 150 if_attach(ifp, NULL);
984263bc
MD
151
152 bpfattach(ifp, DLT_NULL, ICHDRLEN);
153
154 return (0);
155}
156
157/*
158 * iciotcl()
159 */
160static int
9974b71d 161icioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
984263bc 162{
3e4a09e7 163 device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
984263bc
MD
164 device_t parent = device_get_parent(icdev);
165 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
166
167 struct ifaddr *ifa = (struct ifaddr *)data;
168 struct ifreq *ifr = (struct ifreq *)data;
169
170 u_char *iptr, *optr;
171 int error;
172
173 switch (cmd) {
174
175 case SIOCSIFDSTADDR:
176 case SIOCAIFADDR:
177 case SIOCSIFADDR:
178 if (ifa->ifa_addr->sa_family != AF_INET)
179 return EAFNOSUPPORT;
180 ifp->if_flags |= IFF_UP;
181 /* FALLTHROUGH */
182 case SIOCSIFFLAGS:
183 if ((!(ifp->if_flags & IFF_UP)) && (ifp->if_flags & IFF_RUNNING)) {
184
185 /* XXX disable PCF */
186 ifp->if_flags &= ~IFF_RUNNING;
187
188 /* IFF_UP is not set, try to release the bus anyway */
189 iicbus_release_bus(parent, icdev);
190 break;
191 }
192 if (((ifp->if_flags & IFF_UP)) && (!(ifp->if_flags & IFF_RUNNING))) {
193
194 if ((error = iicbus_request_bus(parent, icdev, IIC_WAIT|IIC_INTR)))
195 return (error);
196
197 sc->ic_obuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
198 M_DEVBUF, M_WAITOK);
199 if (!sc->ic_obuf) {
200 iicbus_release_bus(parent, icdev);
201 return ENOBUFS;
202 }
203
204 sc->ic_ifbuf = malloc(sc->ic_if.if_mtu + ICHDRLEN,
205 M_DEVBUF, M_WAITOK);
206 if (!sc->ic_ifbuf) {
207 iicbus_release_bus(parent, icdev);
208 return ENOBUFS;
209 }
210
211 iicbus_reset(parent, IIC_FASTEST, 0, NULL);
212
213 ifp->if_flags |= IFF_RUNNING;
214 }
215 break;
216
217 case SIOCSIFMTU:
218 /* save previous buffers */
219 iptr = sc->ic_ifbuf;
220 optr = sc->ic_obuf;
221
222 /* allocate input buffer */
c5541aee 223 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_WAITOK);
984263bc
MD
224 if (!sc->ic_ifbuf) {
225
226 sc->ic_ifbuf = iptr;
227 sc->ic_obuf = optr;
228
229 return ENOBUFS;
230 }
231
232 /* allocate output buffer */
c5541aee 233 sc->ic_ifbuf = malloc(ifr->ifr_mtu+ICHDRLEN, M_DEVBUF, M_WAITOK);
984263bc
MD
234 if (!sc->ic_obuf) {
235
236 free(sc->ic_ifbuf,M_DEVBUF);
237
238 sc->ic_ifbuf = iptr;
239 sc->ic_obuf = optr;
240
241 return ENOBUFS;
242 }
243
244 if (iptr)
245 free(iptr,M_DEVBUF);
246
247 if (optr)
248 free(optr,M_DEVBUF);
249
250 sc->ic_if.if_mtu = ifr->ifr_mtu;
251 break;
252
253 case SIOCGIFMTU:
254 ifr->ifr_mtu = sc->ic_if.if_mtu;
255 break;
256
257 case SIOCADDMULTI:
258 case SIOCDELMULTI:
259 if (ifr == 0) {
260 return EAFNOSUPPORT; /* XXX */
261 }
262 switch (ifr->ifr_addr.sa_family) {
263
264 case AF_INET:
265 break;
266
267 default:
268 return EAFNOSUPPORT;
269 }
270 break;
271
272 default:
273 return EINVAL;
274 }
275 return 0;
276}
277
278/*
279 * icintr()
280 */
281static void
282icintr (device_t dev, int event, char *ptr)
283{
284 struct ic_softc *sc = (struct ic_softc *)device_get_softc(dev);
285 int unit = device_get_unit(dev);
9c2a771a 286 int len;
984263bc
MD
287 struct mbuf *top;
288
9c2a771a 289 crit_enter();
984263bc
MD
290
291 switch (event) {
292
293 case INTR_GENERAL:
294 case INTR_START:
295 sc->ic_cp = sc->ic_ifbuf;
296 sc->ic_xfercnt = 0;
297 break;
298
299 case INTR_STOP:
300
301 /* if any error occured during transfert,
302 * drop the packet */
303 if (sc->ic_iferrs)
304 goto err;
305
306 if ((len = sc->ic_xfercnt) == 0)
307 break; /* ignore */
308
309 if (len <= ICHDRLEN)
310 goto err;
311
984263bc
MD
312 len -= ICHDRLEN;
313 sc->ic_if.if_ipackets ++;
314 sc->ic_if.if_ibytes += len;
315
7600679e 316 BPF_TAP(&sc->ic_if, sc->ic_ifbuf, len + ICHDRLEN);
984263bc
MD
317
318 top = m_devget(sc->ic_ifbuf + ICHDRLEN, len, 0, &sc->ic_if, 0);
319
8bde602d
JH
320 if (top)
321 netisr_dispatch(NETISR_IP, top);
984263bc
MD
322 break;
323
324 err:
325 printf("ic%d: errors (%d)!\n", unit, sc->ic_iferrs);
326
327 sc->ic_iferrs = 0; /* reset error count */
328 sc->ic_if.if_ierrors ++;
329
330 break;
331
332 case INTR_RECEIVE:
333 if (sc->ic_xfercnt >= sc->ic_if.if_mtu+ICHDRLEN) {
334 sc->ic_iferrs ++;
335
336 } else {
337 *sc->ic_cp++ = *ptr;
338 sc->ic_xfercnt ++;
339 }
340 break;
341
342 case INTR_NOACK: /* xfer terminated by master */
343 break;
344
345 case INTR_TRANSMIT:
346 *ptr = 0xff; /* XXX */
347 break;
348
349 case INTR_ERROR:
350 sc->ic_iferrs ++;
351 break;
352
353 default:
5e2195bf 354 panic("%s: unknown event (%d)!", __func__, event);
984263bc
MD
355 }
356
9c2a771a 357 crit_exit();
984263bc
MD
358}
359
360/*
361 * icoutput()
362 */
363static int
364icoutput(struct ifnet *ifp, struct mbuf *m,
365 struct sockaddr *dst, struct rtentry *rt)
366{
3e4a09e7 367 device_t icdev = devclass_get_device(ic_devclass, ifp->if_dunit);
984263bc
MD
368 device_t parent = device_get_parent(icdev);
369 struct ic_softc *sc = (struct ic_softc *)device_get_softc(icdev);
9c2a771a 370 int len, sent;
984263bc
MD
371 struct mbuf *mm;
372 u_char *cp;
1f8e62c9 373 uint32_t hdr = dst->sa_family;
984263bc
MD
374
375 ifp->if_flags |= IFF_RUNNING;
376
9c2a771a 377 crit_enter();
984263bc
MD
378
379 /* already sending? */
380 if (sc->ic_sending) {
381 ifp->if_oerrors ++;
382 goto error;
383 }
384
385 /* insert header */
386 bcopy ((char *)&hdr, sc->ic_obuf, ICHDRLEN);
387
388 cp = sc->ic_obuf + ICHDRLEN;
389 len = 0;
390 mm = m;
391 do {
392 if (len + mm->m_len > sc->ic_if.if_mtu) {
393 /* packet to large */
394 ifp->if_oerrors ++;
395 goto error;
396 }
397
398 bcopy(mtod(mm,char *), cp, mm->m_len);
399 cp += mm->m_len;
400 len += mm->m_len;
401
402 } while ((mm = mm->m_next));
403
1f8e62c9
JS
404 if (ifp->if_bpf)
405 bpf_ptap(ifp->if_bpf, m, &hdr, ICHDRLEN);
984263bc
MD
406
407 sc->ic_sending = 1;
408
409 m_freem(m);
9c2a771a
JS
410
411 crit_exit();
984263bc
MD
412
413 /* send the packet */
414 if (iicbus_block_write(parent, sc->ic_addr, sc->ic_obuf,
415 len + ICHDRLEN, &sent))
416
417 ifp->if_oerrors ++;
418 else {
419 ifp->if_opackets ++;
420 ifp->if_obytes += len;
421 }
422
423 sc->ic_sending = 0;
424
425 return (0);
426
427error:
428 m_freem(m);
9c2a771a 429 crit_exit();
984263bc
MD
430
431 return(0);
432}
433
32832096
MD
434DECLARE_DUMMY_MODULE(if_ic);
435DRIVER_MODULE(if_ic, iicbus, ic_driver, ic_devclass, 0, 0);
436