Merge from vendor branch CVS:
[dragonfly.git] / sys / dev / netif / pdq_layer / pdq_ifsubr.c
1 /*-
2  * Copyright (c) 1995, 1996 Matt Thomas <matt@3am-software.com>
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. The name of the author may not be used to endorse or promote products
11  *    derived from this software withough specific prior written permission
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $FreeBSD: src/sys/dev/pdq/pdq_ifsubr.c,v 1.11.2.1 2000/08/02 22:39:30 peter Exp $
25  * $DragonFly: src/sys/dev/netif/pdq_layer/Attic/pdq_ifsubr.c,v 1.11 2005/02/20 04:41:46 joerg Exp $
26  *
27  */
28
29 /*
30  * DEC PDQ FDDI Controller; code for BSD derived operating systems
31  *
32  *      This module provide bus independent BSD specific O/S functions.
33  *      (ie. it provides an ifnet interface to the rest of the system)
34  */
35
36
37 #include "opt_inet.h"
38
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #if defined(__bsdi__) || defined(__NetBSD__)
43 #include <sys/device.h>
44 #endif
45
46 #include <net/if.h>
47 #include <net/ifq_var.h>
48 #include <net/if_dl.h>
49
50 #include <net/bpf.h>
51
52 #if defined(__DragonFly__) || defined(__FreeBSD__)
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/if_ether.h>
56 #endif
57 #include <netinet/if_fddi.h>
58 #else
59 #include <net/if_fddi.h>
60 #endif
61
62 #if defined(__bsdi__)
63 #include <i386/isa/isavar.h>
64 #endif
65
66 #ifdef NS
67 #include <netns/ns.h>
68 #include <netns/ns_if.h>
69 #endif
70
71 #if defined(__DragonFly__) || defined(__FreeBSD__)
72 #include "pdqvar.h"
73 #include "pdqreg.h"
74 #else
75 #include "pdqvar.h"
76 #include "pdqreg.h"
77 #endif
78
79 #if defined(__bsdi__) && _BSDI_VERSION < 199506 /* XXX */
80 static void
81 arp_ifinit(
82     struct arpcom *ac,
83     struct ifaddr *ifa)
84 {
85     sc->sc_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
86     arpwhohas(&sc->sc_ac, &IA_SIN(ifa)->sin_addr);
87 #if _BSDI_VERSION >= 199401
88     ifa->ifa_rtrequest = arp_rtrequest;
89     ifa->ifa_flags |= RTF_CLONING;
90 #endif
91 }
92 #endif
93
94
95 void
96 pdq_ifinit(
97     pdq_softc_t *sc)
98 {
99     if (sc->sc_if.if_flags & IFF_UP) {
100         sc->sc_if.if_flags |= IFF_RUNNING;
101         if (sc->sc_if.if_flags & IFF_PROMISC) {
102             sc->sc_pdq->pdq_flags |= PDQ_PROMISC;
103         } else {
104             sc->sc_pdq->pdq_flags &= ~PDQ_PROMISC;
105         }
106         if (sc->sc_if.if_flags & IFF_ALLMULTI) {
107             sc->sc_pdq->pdq_flags |= PDQ_ALLMULTI;
108         } else {
109             sc->sc_pdq->pdq_flags &= ~PDQ_ALLMULTI;
110         }
111         if (sc->sc_if.if_flags & IFF_LINK1) {
112             sc->sc_pdq->pdq_flags |= PDQ_PASS_SMT;
113         } else {
114             sc->sc_pdq->pdq_flags &= ~PDQ_PASS_SMT;
115         }
116         sc->sc_pdq->pdq_flags |= PDQ_RUNNING;
117         pdq_run(sc->sc_pdq);
118     } else {
119         sc->sc_if.if_flags &= ~IFF_RUNNING;
120         sc->sc_pdq->pdq_flags &= ~PDQ_RUNNING;
121         pdq_stop(sc->sc_pdq);
122     }
123 }
124 \f
125 void
126 pdq_ifwatchdog(
127     struct ifnet *ifp)
128 {
129     /*
130      * No progress was made on the transmit queue for PDQ_OS_TX_TRANSMIT
131      * seconds.  Remove all queued packets.
132      */
133
134     ifp->if_flags &= ~IFF_OACTIVE;
135     ifp->if_timer = 0;
136     ifq_purge(&ifp->if_snd);
137 }
138
139 ifnet_ret_t
140 pdq_ifstart(
141     struct ifnet *ifp)
142 {
143     pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
144     struct mbuf *m;
145     int tx = 0;
146
147     if ((ifp->if_flags & IFF_RUNNING) == 0)
148         return;
149
150     if (sc->sc_if.if_timer == 0)
151         sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
152
153     if ((sc->sc_pdq->pdq_flags & PDQ_TXOK) == 0) {
154         sc->sc_if.if_flags |= IFF_OACTIVE;
155         return;
156     }
157     for (;; tx = 1) {
158         m = ifq_poll(&ifp->if_snd);
159         if (m == NULL)
160             break;
161
162         if (pdq_queue_transmit_data(ifp, sc->sc_pdq, m) == PDQ_FALSE) {
163             break;
164         }
165     }
166     if (tx)
167         PDQ_DO_TYPE2_PRODUCER(sc->sc_pdq);
168 }
169 \f
170 void
171 pdq_os_receive_pdu(
172     pdq_t *pdq,
173     struct mbuf *m,
174     size_t pktlen)
175 {
176     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
177     struct fddi_header *fh = mtod(m, struct fddi_header *);
178
179     sc->sc_if.if_ipackets++;
180     BPF_MTAP(&sc->sc_if, m);
181     if ((fh->fddi_fc & (FDDIFC_L|FDDIFC_F)) != FDDIFC_LLC_ASYNC) {
182         m_freem(m);
183         return;
184     }
185
186     m->m_pkthdr.len = pktlen;
187     (*sc->sc_if.if_input)(&sc->sc_if, m);
188 }
189
190 void
191 pdq_os_restart_transmitter(
192     pdq_t *pdq)
193 {
194     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
195     sc->sc_if.if_flags &= ~IFF_OACTIVE;
196     if (ifq_is_empty(&sc->sc_if.if_snd)) {
197         pdq_ifstart(&sc->sc_if);
198         sc->sc_if.if_timer = PDQ_OS_TX_TIMEOUT;
199     } else {
200         sc->sc_if.if_timer = 0;
201     }
202 }
203
204 void
205 pdq_os_transmit_done(
206     pdq_t *pdq,
207     struct mbuf *m)
208 {
209     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
210     BPF_MTAP(&sc->sc_if, m);
211     m_freem(m);
212     sc->sc_if.if_opackets++;
213 }
214 \f
215 void
216 pdq_os_addr_fill(
217     pdq_t *pdq,
218     pdq_lanaddr_t *addr,
219     size_t num_addrs)
220 {
221     pdq_softc_t *sc = (pdq_softc_t *) pdq->pdq_os_ctx;
222     struct ifmultiaddr *ifma;
223
224     for (ifma = sc->sc_if.if_multiaddrs.lh_first; ifma && num_addrs > 0;
225          ifma = ifma->ifma_link.le_next) {
226             char *mcaddr;
227             if (ifma->ifma_addr->sa_family != AF_LINK)
228                     continue;
229             mcaddr = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
230             ((u_short *) addr->lanaddr_bytes)[0] = ((u_short *) mcaddr)[0];
231             ((u_short *) addr->lanaddr_bytes)[1] = ((u_short *) mcaddr)[1];
232             ((u_short *) addr->lanaddr_bytes)[2] = ((u_short *) mcaddr)[2];
233             addr++;
234             num_addrs--;
235     }
236 }
237 \f
238 int
239 pdq_ifioctl(struct ifnet *ifp, ioctl_cmd_t cmd, caddr_t data, struct ucred *cr)
240 {
241     pdq_softc_t *sc = (pdq_softc_t *) ((caddr_t) ifp - offsetof(pdq_softc_t, sc_ac.ac_if));
242     int s, error = 0;
243
244     s = splimp();
245
246     switch (cmd) {
247         case SIOCSIFADDR: {
248             struct ifaddr *ifa = (struct ifaddr *)data;
249
250             ifp->if_flags |= IFF_UP;
251             switch(ifa->ifa_addr->sa_family) {
252 #if defined(INET)
253                 case AF_INET: {
254                     pdq_ifinit(sc);
255                     arp_ifinit(&sc->sc_ac.ac_if, ifa);
256                     break;
257                 }
258 #endif /* INET */
259
260 #if defined(NS)
261                 /* This magic copied from if_is.c; I don't use XNS,
262                  * so I have no way of telling if this actually
263                  * works or not.
264                  */
265                 case AF_NS: {
266                     struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
267                     if (ns_nullhost(*ina)) {
268                         ina->x_host = *(union ns_host *)(sc->sc_ac.ac_enaddr);
269                     } else {
270                         ifp->if_flags &= ~IFF_RUNNING;
271                         bcopy((caddr_t)ina->x_host.c_host,
272                               (caddr_t)sc->sc_ac.ac_enaddr,
273                               sizeof sc->sc_ac.ac_enaddr);
274                     }
275
276                     pdq_ifinit(sc);
277                     break;
278                 }
279 #endif /* NS */
280
281                 default: {
282                     pdq_ifinit(sc);
283                     break;
284                 }
285             }
286             break;
287         }
288         case SIOCGIFADDR: {
289             struct ifreq *ifr = (struct ifreq *)data;
290             bcopy((caddr_t) sc->sc_ac.ac_enaddr,
291                   (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
292                   6);
293             break;
294         }
295
296         case SIOCSIFFLAGS: {
297             pdq_ifinit(sc);
298             break;
299         }
300
301         case SIOCADDMULTI:
302         case SIOCDELMULTI:
303                 /*
304                  * Update multicast listeners
305                  */
306                 if (sc->sc_if.if_flags & IFF_RUNNING)
307                         pdq_run(sc->sc_pdq);
308                 error = 0;
309                 break;
310
311 #if defined(SIOCSIFMTU)
312 #if !defined(ifr_mtu)
313 #define ifr_mtu ifr_metric
314 #endif
315         case SIOCSIFMTU: {
316             struct ifreq *ifr = (struct ifreq *)data;
317             /*
318              * Set the interface MTU.
319              */
320             if (ifr->ifr_mtu > FDDIMTU) {
321                 error = EINVAL;
322                 break;
323             }
324             ifp->if_mtu = ifr->ifr_mtu;
325             break;
326         }
327 #endif /* SIOCSIFMTU */
328
329         default: {
330             error = EINVAL;
331             break;
332         }
333     }
334
335     splx(s);
336     return error;
337 }
338 \f
339 #ifndef IFF_NOTRAILERS
340 #define IFF_NOTRAILERS  0
341 #endif
342
343 void
344 pdq_ifattach(
345     pdq_softc_t *sc,
346     ifnet_ret_t (*ifwatchdog)(int unit))
347 {
348     struct ifnet *ifp = &sc->sc_if;
349
350     ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
351
352 #if defined(__DragonFly__) || (defined(__FreeBSD__) && BSD >= 199506) || defined(__NetBSD__)
353     ifp->if_watchdog = pdq_ifwatchdog;
354 #else
355     ifp->if_watchdog = ifwatchdog;
356 #endif
357
358     ifp->if_ioctl = pdq_ifioctl;
359     ifp->if_start = pdq_ifstart;
360     ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
361     ifq_set_ready(&ifp->if_snd);
362   
363     fddi_ifattach(ifp);
364 }