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>
29 * Driver for IDT77252 based cards like ProSum's.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
44 #include <sys/errno.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/queue.h>
51 #include <sys/condvar.h>
54 #include <sys/sockio.h>
56 #include <sys/socket.h>
59 #include <net/if_var.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 <sys/mbpool.h>
72 #include <dev/utopia/utopia.h>
73 #include <dev/patm/idt77252reg.h>
74 #include <dev/patm/if_patmvar.h>
77 * Open the VCC with the given parameters
80 patm_open_vcc(struct patm_softc *sc, struct atmio_openvcc *arg)
86 patm_debug(sc, VCC, "Open VCC: %u.%u flags=%#x", arg->param.vpi,
87 arg->param.vci, arg->param.flags);
89 if (!LEGAL_VPI(sc, arg->param.vpi) || !LEGAL_VCI(sc, arg->param.vci))
91 if (arg->param.vci == 0 && (arg->param.vpi != 0 ||
92 !(arg->param.flags & ATMIO_FLAG_NOTX) ||
93 arg->param.aal != ATMIO_AAL_RAW))
95 cid = PATM_CID(sc, arg->param.vpi, arg->param.vci);
97 if ((arg->param.flags & ATMIO_FLAG_NOTX) &&
98 (arg->param.flags & ATMIO_FLAG_NORX))
101 if ((arg->param.traffic == ATMIO_TRAFFIC_ABR) &&
102 (arg->param.flags & (ATMIO_FLAG_NOTX | ATMIO_FLAG_NORX)))
106 vcc = uma_zalloc(sc->vcc_zone, M_NOWAIT | M_ZERO);
111 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
112 /* stopped while we have analyzed the arguments */
116 if (sc->vccs[cid] != NULL) {
117 /* ups, already open */
122 /* check some parameters */
124 vcc->vcc = arg->param;
126 vcc->rxhand = arg->rxhand;
127 switch (vcc->vcc.aal) {
135 if (arg->param.vci == 0 &&
136 !(arg->param.flags & ATMIO_FLAG_NOTX)) {
146 switch (vcc->vcc.traffic) {
148 case ATMIO_TRAFFIC_VBR:
149 case ATMIO_TRAFFIC_UBR:
150 case ATMIO_TRAFFIC_CBR:
151 case ATMIO_TRAFFIC_ABR:
162 vcc->ibytes = vcc->ipackets = 0;
163 vcc->obytes = vcc->opackets = 0;
165 /* ask the TX and RX sides */
166 patm_debug(sc, VCC, "Open VCC: asking Rx/Tx");
167 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX) &&
168 (error = patm_tx_vcc_can_open(sc, vcc)) != 0)
170 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX) &&
171 (error = patm_rx_vcc_can_open(sc, vcc)) != 0)
176 patm_load_vc(sc, vcc, 0);
178 /* don't free below */
184 mtx_unlock(&sc->mtx);
186 uma_zfree(sc->vcc_zone, vcc);
191 patm_load_vc(struct patm_softc *sc, struct patm_vcc *vcc, int reload)
194 patm_debug(sc, VCC, "Open VCC: opening");
195 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX))
196 patm_tx_vcc_open(sc, vcc);
197 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX))
198 patm_rx_vcc_open(sc, vcc);
201 /* inform management about non-NG and NG-PVCs */
202 if (!(vcc->vcc.flags & ATMIO_FLAG_NG) ||
203 (vcc->vcc.flags & ATMIO_FLAG_PVC))
204 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), vcc->vcc.vpi,
208 patm_debug(sc, VCC, "Open VCC: now open");
212 * Try to close the given VCC
215 patm_close_vcc(struct patm_softc *sc, struct atmio_closevcc *arg)
218 struct patm_vcc *vcc;
221 patm_debug(sc, VCC, "Close VCC: %u.%u", arg->vpi, arg->vci);
223 if (!LEGAL_VPI(sc, arg->vpi) || !LEGAL_VCI(sc, arg->vci))
225 cid = PATM_CID(sc, arg->vpi, arg->vci);
228 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
229 /* stopped while we have analyzed the arguments */
235 if (vcc == NULL || !(vcc->vflags & PATM_VCC_OPEN)) {
240 if (vcc->vflags & PATM_VCC_TX_OPEN)
241 patm_tx_vcc_close(sc, vcc);
242 if (vcc->vflags & PATM_VCC_RX_OPEN)
243 patm_rx_vcc_close(sc, vcc);
245 if (vcc->vcc.flags & ATMIO_FLAG_ASYNC)
248 while (vcc->vflags & (PATM_VCC_TX_CLOSING | PATM_VCC_RX_CLOSING)) {
249 cv_wait(&sc->vcc_cv, &sc->mtx);
250 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
251 /* ups, has been stopped */
257 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX))
258 patm_tx_vcc_closed(sc, vcc);
259 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX))
260 patm_rx_vcc_closed(sc, vcc);
262 patm_vcc_closed(sc, vcc);
265 mtx_unlock(&sc->mtx);
271 * VCC has been finally closed.
274 patm_vcc_closed(struct patm_softc *sc, struct patm_vcc *vcc)
277 /* inform management about non-NG and NG-PVCs */
278 if (!(vcc->vcc.flags & ATMIO_FLAG_NG) ||
279 (vcc->vcc.flags & ATMIO_FLAG_PVC))
280 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), vcc->vcc.vpi,
284 sc->vccs[vcc->cid] = NULL;
285 uma_zfree(sc->vcc_zone, vcc);
289 patm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
291 struct ifreq *ifr = (struct ifreq *)data;
292 struct ifaddr *ifa = (struct ifaddr *)data;
293 struct patm_softc *sc = ifp->if_softc;
296 struct atmio_vcctable *vtab;
302 ifp->if_flags |= IFF_UP;
303 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
305 switch (ifa->ifa_addr->sa_family) {
310 ifa->ifa_rtrequest = atm_rtrequest;
316 mtx_unlock(&sc->mtx);
321 if (ifp->if_flags & IFF_UP) {
322 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
326 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
330 mtx_unlock(&sc->mtx);
335 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
338 * We need to toggle unassigned/idle cells ourself because
339 * the 77252 generates null cells for spacing. When switching
340 * null cells of it gets the timing wrong.
343 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
344 if (sc->utopia.state & UTP_ST_UNASS) {
345 if (!(sc->flags & PATM_UNASS)) {
346 cfg = patm_nor_read(sc, IDT_NOR_CFG);
347 cfg &= ~IDT_CFG_IDLECLP;
348 patm_nor_write(sc, IDT_NOR_CFG, cfg);
349 sc->flags |= PATM_UNASS;
352 if (sc->flags & PATM_UNASS) {
353 cfg = patm_nor_read(sc, IDT_NOR_CFG);
354 cfg |= IDT_CFG_IDLECLP;
355 patm_nor_write(sc, IDT_NOR_CFG, cfg);
356 sc->flags &= ~PATM_UNASS;
360 if (sc->utopia.state & UTP_ST_UNASS)
361 sc->flags |= PATM_UNASS;
363 sc->flags &= ~PATM_UNASS;
365 mtx_unlock(&sc->mtx);
370 * Set the interface MTU.
372 if (ifr->ifr_mtu > ATMMTU)
375 ifp->if_mtu = ifr->ifr_mtu;
378 case SIOCATMOPENVCC: /* kernel internal use */
379 error = patm_open_vcc(sc, (struct atmio_openvcc *)data);
382 case SIOCATMCLOSEVCC: /* kernel internal use */
383 error = patm_close_vcc(sc, (struct atmio_closevcc *)data);
386 case SIOCATMGVCCS: /* external use */
387 /* return vcc table */
388 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
389 sc->mmap->max_conn, sc->vccs_open, &sc->mtx, 1);
390 error = copyout(vtab, ifr->ifr_data, sizeof(*vtab) +
391 vtab->count * sizeof(vtab->vccs[0]));
392 free(vtab, M_DEVBUF);
395 case SIOCATMGETVCCS: /* netgraph internal use */
396 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
397 sc->mmap->max_conn, sc->vccs_open, &sc->mtx, 0);
402 *(void **)data = vtab;
406 patm_debug(sc, IOCTL, "unknown cmd=%08lx arg=%p", cmd, data);