2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Author: Hartmut Brandt <harti@freebsd.org>
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
46 #include <sys/errno.h>
48 #include <sys/module.h>
49 #include <sys/queue.h>
50 #include <sys/syslog.h>
51 #include <sys/condvar.h>
52 #include <sys/sysctl.h>
55 #include <sys/sockio.h>
57 #include <sys/socket.h>
60 #include <net/if_media.h>
61 #include <net/if_atm.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/if_atm.h>
66 #include <machine/bus.h>
67 #include <machine/resource.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
73 #include <dev/utopia/utopia.h>
74 #include <dev/hatm/if_hatmconf.h>
75 #include <dev/hatm/if_hatmreg.h>
76 #include <dev/hatm/if_hatmvar.h>
78 static u_int hatm_natm_traffic = ATMIO_TRAFFIC_UBR;
79 static u_int hatm_natm_pcr = 0;
81 static int hatm_sysctl_natm_traffic(SYSCTL_HANDLER_ARGS);
85 SYSCTL_PROC(_hw_atm, OID_AUTO, natm_traffic, CTLTYPE_UINT | CTLFLAG_RW,
86 &hatm_natm_traffic, sizeof(hatm_natm_traffic), hatm_sysctl_natm_traffic,
87 "IU", "traffic type for NATM connections");
88 SYSCTL_UINT(_hw_atm, OID_AUTO, natm_pcr, CTLFLAG_RW,
89 &hatm_natm_pcr, 0, "PCR for NATM connections");
92 * Try to open the given VCC.
95 hatm_open_vcc(struct hatm_softc *sc, struct atmio_openvcc *arg)
101 DBG(sc, VCC, ("Open VCC: %u.%u flags=%#x", arg->param.vpi,
102 arg->param.vci, arg->param.flags));
104 if ((arg->param.vpi & ~HE_VPI_MASK) ||
105 (arg->param.vci & ~HE_VCI_MASK) ||
106 (arg->param.vci == 0))
108 cid = HE_CID(arg->param.vpi, arg->param.vci);
110 if ((arg->param.flags & ATMIO_FLAG_NOTX) &&
111 (arg->param.flags & ATMIO_FLAG_NORX))
114 vcc = uma_zalloc(sc->vcc_zone, M_NOWAIT | M_ZERO);
119 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
123 if (sc->vccs[cid] != NULL) {
127 vcc->param = arg->param;
128 vcc->rxhand = arg->rxhand;
129 switch (vcc->param.aal) {
140 switch (vcc->param.traffic) {
142 case ATMIO_TRAFFIC_UBR:
143 case ATMIO_TRAFFIC_CBR:
144 case ATMIO_TRAFFIC_ABR:
152 vcc->chain = vcc->last = NULL;
153 vcc->ibytes = vcc->ipackets = 0;
154 vcc->obytes = vcc->opackets = 0;
156 if (!(vcc->param.flags & ATMIO_FLAG_NOTX) &&
157 (error = hatm_tx_vcc_can_open(sc, cid, vcc)) != 0)
162 hatm_load_vc(sc, cid, 0);
164 /* don't free below */
169 mtx_unlock(&sc->mtx);
171 uma_zfree(sc->vcc_zone, vcc);
176 hatm_load_vc(struct hatm_softc *sc, u_int cid, int reopen)
178 struct hevcc *vcc = sc->vccs[cid];
180 if (!(vcc->param.flags & ATMIO_FLAG_NOTX))
181 hatm_tx_vcc_open(sc, cid);
182 if (!(vcc->param.flags & ATMIO_FLAG_NORX))
183 hatm_rx_vcc_open(sc, cid);
188 /* inform management about non-NG and NG-PVCs */
189 if (!(vcc->param.flags & ATMIO_FLAG_NG) ||
190 (vcc->param.flags & ATMIO_FLAG_PVC))
191 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), vcc->param.vpi,
196 * VCC has been finally closed.
199 hatm_vcc_closed(struct hatm_softc *sc, u_int cid)
201 struct hevcc *vcc = sc->vccs[cid];
203 /* inform management about non-NG and NG-PVCs */
204 if (!(vcc->param.flags & ATMIO_FLAG_NG) ||
205 (vcc->param.flags & ATMIO_FLAG_PVC))
206 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), HE_VPI(cid), HE_VCI(cid), 0);
209 uma_zfree(sc->vcc_zone, vcc);
210 sc->vccs[cid] = NULL;
214 * Try to close the given VCC
217 hatm_close_vcc(struct hatm_softc *sc, struct atmio_closevcc *arg)
223 DBG(sc, VCC, ("Close VCC: %u.%u", arg->vpi, arg->vci));
225 if((arg->vpi & ~HE_VPI_MASK) ||
226 (arg->vci & ~HE_VCI_MASK) ||
229 cid = HE_CID(arg->vpi, arg->vci);
233 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
238 if (vcc == NULL || !(vcc->vflags & HE_VCC_OPEN)) {
243 if (vcc->vflags & HE_VCC_TX_OPEN)
244 hatm_tx_vcc_close(sc, cid);
245 if (vcc->vflags & HE_VCC_RX_OPEN)
246 hatm_rx_vcc_close(sc, cid);
248 if (vcc->param.flags & ATMIO_FLAG_ASYNC)
251 while ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING) &&
252 (vcc->vflags & (HE_VCC_TX_CLOSING | HE_VCC_RX_CLOSING)))
253 cv_wait(&sc->vcc_cv, &sc->mtx);
255 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
260 if (!(vcc->vflags & ATMIO_FLAG_NOTX))
261 hatm_tx_vcc_closed(sc, cid);
263 hatm_vcc_closed(sc, cid);
266 mtx_unlock(&sc->mtx);
274 hatm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
276 struct ifreq *ifr = (struct ifreq *)data;
277 struct ifaddr *ifa = (struct ifaddr *)data;
278 struct hatm_softc *sc = ifp->if_softc;
279 struct atmio_vcctable *vtab;
286 ifp->if_flags |= IFF_UP;
287 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
289 switch (ifa->ifa_addr->sa_family) {
294 ifa->ifa_rtrequest = atm_rtrequest;
300 mtx_unlock(&sc->mtx);
305 if (ifp->if_flags & IFF_UP) {
306 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
310 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
314 mtx_unlock(&sc->mtx);
319 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
324 * Set the interface MTU.
326 if (ifr->ifr_mtu > ATMMTU)
329 ifp->if_mtu = ifr->ifr_mtu;
333 /* return vcc table */
334 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
335 HE_MAX_VCCS, sc->open_vccs, &sc->mtx, 1);
336 error = copyout(vtab, ifr->ifr_data, sizeof(*vtab) +
337 vtab->count * sizeof(vtab->vccs[0]));
338 free(vtab, M_DEVBUF);
341 case SIOCATMGETVCCS: /* netgraph internal use */
342 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
343 HE_MAX_VCCS, sc->open_vccs, &sc->mtx, 0);
348 *(void **)data = vtab;
351 case SIOCATMOPENVCC: /* kernel internal use */
352 error = hatm_open_vcc(sc, (struct atmio_openvcc *)data);
355 case SIOCATMCLOSEVCC: /* kernel internal use */
356 error = hatm_close_vcc(sc, (struct atmio_closevcc *)data);
360 DBG(sc, IOCTL, ("cmd=%08lx arg=%p", cmd, data));
369 hatm_sysctl_natm_traffic(SYSCTL_HANDLER_ARGS)
374 tmp = hatm_natm_traffic;
375 error = sysctl_handle_int(oidp, &tmp, 0, req);
376 if (error != 0 || req->newptr == NULL)
379 if (tmp != ATMIO_TRAFFIC_UBR && tmp != ATMIO_TRAFFIC_CBR)
382 hatm_natm_traffic = tmp;