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_var.h>
61 #include <net/if_media.h>
62 #include <net/if_atm.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/if_atm.h>
67 #include <machine/bus.h>
68 #include <machine/resource.h>
71 #include <dev/pci/pcireg.h>
72 #include <dev/pci/pcivar.h>
74 #include <dev/utopia/utopia.h>
75 #include <dev/hatm/if_hatmconf.h>
76 #include <dev/hatm/if_hatmreg.h>
77 #include <dev/hatm/if_hatmvar.h>
79 static u_int hatm_natm_traffic = ATMIO_TRAFFIC_UBR;
80 static u_int hatm_natm_pcr = 0;
82 static int hatm_sysctl_natm_traffic(SYSCTL_HANDLER_ARGS);
86 SYSCTL_PROC(_hw_atm, OID_AUTO, natm_traffic, CTLTYPE_UINT | CTLFLAG_RW,
87 &hatm_natm_traffic, sizeof(hatm_natm_traffic), hatm_sysctl_natm_traffic,
88 "IU", "traffic type for NATM connections");
89 SYSCTL_UINT(_hw_atm, OID_AUTO, natm_pcr, CTLFLAG_RW,
90 &hatm_natm_pcr, 0, "PCR for NATM connections");
93 * Try to open the given VCC.
96 hatm_open_vcc(struct hatm_softc *sc, struct atmio_openvcc *arg)
102 DBG(sc, VCC, ("Open VCC: %u.%u flags=%#x", arg->param.vpi,
103 arg->param.vci, arg->param.flags));
105 if ((arg->param.vpi & ~HE_VPI_MASK) ||
106 (arg->param.vci & ~HE_VCI_MASK) ||
107 (arg->param.vci == 0))
109 cid = HE_CID(arg->param.vpi, arg->param.vci);
111 if ((arg->param.flags & ATMIO_FLAG_NOTX) &&
112 (arg->param.flags & ATMIO_FLAG_NORX))
115 vcc = uma_zalloc(sc->vcc_zone, M_NOWAIT | M_ZERO);
120 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
124 if (sc->vccs[cid] != NULL) {
128 vcc->param = arg->param;
129 vcc->rxhand = arg->rxhand;
130 switch (vcc->param.aal) {
141 switch (vcc->param.traffic) {
143 case ATMIO_TRAFFIC_UBR:
144 case ATMIO_TRAFFIC_CBR:
145 case ATMIO_TRAFFIC_ABR:
153 vcc->chain = vcc->last = NULL;
154 vcc->ibytes = vcc->ipackets = 0;
155 vcc->obytes = vcc->opackets = 0;
157 if (!(vcc->param.flags & ATMIO_FLAG_NOTX) &&
158 (error = hatm_tx_vcc_can_open(sc, cid, vcc)) != 0)
163 hatm_load_vc(sc, cid, 0);
165 /* don't free below */
170 mtx_unlock(&sc->mtx);
172 uma_zfree(sc->vcc_zone, vcc);
177 hatm_load_vc(struct hatm_softc *sc, u_int cid, int reopen)
179 struct hevcc *vcc = sc->vccs[cid];
181 if (!(vcc->param.flags & ATMIO_FLAG_NOTX))
182 hatm_tx_vcc_open(sc, cid);
183 if (!(vcc->param.flags & ATMIO_FLAG_NORX))
184 hatm_rx_vcc_open(sc, cid);
189 /* inform management about non-NG and NG-PVCs */
190 if (!(vcc->param.flags & ATMIO_FLAG_NG) ||
191 (vcc->param.flags & ATMIO_FLAG_PVC))
192 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), vcc->param.vpi,
197 * VCC has been finally closed.
200 hatm_vcc_closed(struct hatm_softc *sc, u_int cid)
202 struct hevcc *vcc = sc->vccs[cid];
204 /* inform management about non-NG and NG-PVCs */
205 if (!(vcc->param.flags & ATMIO_FLAG_NG) ||
206 (vcc->param.flags & ATMIO_FLAG_PVC))
207 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), HE_VPI(cid), HE_VCI(cid), 0);
210 uma_zfree(sc->vcc_zone, vcc);
211 sc->vccs[cid] = NULL;
215 * Try to close the given VCC
218 hatm_close_vcc(struct hatm_softc *sc, struct atmio_closevcc *arg)
224 DBG(sc, VCC, ("Close VCC: %u.%u", arg->vpi, arg->vci));
226 if((arg->vpi & ~HE_VPI_MASK) ||
227 (arg->vci & ~HE_VCI_MASK) ||
230 cid = HE_CID(arg->vpi, arg->vci);
234 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
239 if (vcc == NULL || !(vcc->vflags & HE_VCC_OPEN)) {
244 if (vcc->vflags & HE_VCC_TX_OPEN)
245 hatm_tx_vcc_close(sc, cid);
246 if (vcc->vflags & HE_VCC_RX_OPEN)
247 hatm_rx_vcc_close(sc, cid);
249 if (vcc->param.flags & ATMIO_FLAG_ASYNC)
252 while ((sc->ifp->if_drv_flags & IFF_DRV_RUNNING) &&
253 (vcc->vflags & (HE_VCC_TX_CLOSING | HE_VCC_RX_CLOSING)))
254 cv_wait(&sc->vcc_cv, &sc->mtx);
256 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
261 if (!(vcc->vflags & ATMIO_FLAG_NOTX))
262 hatm_tx_vcc_closed(sc, cid);
264 hatm_vcc_closed(sc, cid);
267 mtx_unlock(&sc->mtx);
275 hatm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
277 struct ifreq *ifr = (struct ifreq *)data;
278 struct ifaddr *ifa = (struct ifaddr *)data;
279 struct hatm_softc *sc = ifp->if_softc;
280 struct atmio_vcctable *vtab;
287 ifp->if_flags |= IFF_UP;
288 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
290 switch (ifa->ifa_addr->sa_family) {
295 ifa->ifa_rtrequest = atm_rtrequest;
301 mtx_unlock(&sc->mtx);
306 if (ifp->if_flags & IFF_UP) {
307 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
311 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
315 mtx_unlock(&sc->mtx);
320 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
325 * Set the interface MTU.
327 if (ifr->ifr_mtu > ATMMTU)
330 ifp->if_mtu = ifr->ifr_mtu;
334 /* return vcc table */
335 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
336 HE_MAX_VCCS, sc->open_vccs, &sc->mtx, 1);
337 error = copyout(vtab, ifr->ifr_data, sizeof(*vtab) +
338 vtab->count * sizeof(vtab->vccs[0]));
339 free(vtab, M_DEVBUF);
342 case SIOCATMGETVCCS: /* netgraph internal use */
343 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
344 HE_MAX_VCCS, sc->open_vccs, &sc->mtx, 0);
349 *(void **)data = vtab;
352 case SIOCATMOPENVCC: /* kernel internal use */
353 error = hatm_open_vcc(sc, (struct atmio_openvcc *)data);
356 case SIOCATMCLOSEVCC: /* kernel internal use */
357 error = hatm_close_vcc(sc, (struct atmio_closevcc *)data);
361 DBG(sc, IOCTL, ("cmd=%08lx arg=%p", cmd, data));
370 hatm_sysctl_natm_traffic(SYSCTL_HANDLER_ARGS)
375 tmp = hatm_natm_traffic;
376 error = sysctl_handle_int(oidp, &tmp, 0, req);
377 if (error != 0 || req->newptr == NULL)
380 if (tmp != ATMIO_TRAFFIC_UBR && tmp != ATMIO_TRAFFIC_CBR)
383 hatm_natm_traffic = tmp;