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_media.h>
60 #include <net/if_atm.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/if_atm.h>
65 #include <machine/bus.h>
66 #include <machine/resource.h>
69 #include <sys/mbpool.h>
71 #include <dev/utopia/utopia.h>
72 #include <dev/patm/idt77252reg.h>
73 #include <dev/patm/if_patmvar.h>
76 * Open the VCC with the given parameters
79 patm_open_vcc(struct patm_softc *sc, struct atmio_openvcc *arg)
85 patm_debug(sc, VCC, "Open VCC: %u.%u flags=%#x", arg->param.vpi,
86 arg->param.vci, arg->param.flags);
88 if (!LEGAL_VPI(sc, arg->param.vpi) || !LEGAL_VCI(sc, arg->param.vci))
90 if (arg->param.vci == 0 && (arg->param.vpi != 0 ||
91 !(arg->param.flags & ATMIO_FLAG_NOTX) ||
92 arg->param.aal != ATMIO_AAL_RAW))
94 cid = PATM_CID(sc, arg->param.vpi, arg->param.vci);
96 if ((arg->param.flags & ATMIO_FLAG_NOTX) &&
97 (arg->param.flags & ATMIO_FLAG_NORX))
100 if ((arg->param.traffic == ATMIO_TRAFFIC_ABR) &&
101 (arg->param.flags & (ATMIO_FLAG_NOTX | ATMIO_FLAG_NORX)))
105 vcc = uma_zalloc(sc->vcc_zone, M_NOWAIT | M_ZERO);
110 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
111 /* stopped while we have analyzed the arguments */
115 if (sc->vccs[cid] != NULL) {
116 /* ups, already open */
121 /* check some parameters */
123 vcc->vcc = arg->param;
125 vcc->rxhand = arg->rxhand;
126 switch (vcc->vcc.aal) {
134 if (arg->param.vci == 0 &&
135 !(arg->param.flags & ATMIO_FLAG_NOTX)) {
145 switch (vcc->vcc.traffic) {
147 case ATMIO_TRAFFIC_VBR:
148 case ATMIO_TRAFFIC_UBR:
149 case ATMIO_TRAFFIC_CBR:
150 case ATMIO_TRAFFIC_ABR:
161 vcc->ibytes = vcc->ipackets = 0;
162 vcc->obytes = vcc->opackets = 0;
164 /* ask the TX and RX sides */
165 patm_debug(sc, VCC, "Open VCC: asking Rx/Tx");
166 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX) &&
167 (error = patm_tx_vcc_can_open(sc, vcc)) != 0)
169 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX) &&
170 (error = patm_rx_vcc_can_open(sc, vcc)) != 0)
175 patm_load_vc(sc, vcc, 0);
177 /* don't free below */
183 mtx_unlock(&sc->mtx);
185 uma_zfree(sc->vcc_zone, vcc);
190 patm_load_vc(struct patm_softc *sc, struct patm_vcc *vcc, int reload)
193 patm_debug(sc, VCC, "Open VCC: opening");
194 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX))
195 patm_tx_vcc_open(sc, vcc);
196 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX))
197 patm_rx_vcc_open(sc, vcc);
200 /* inform management about non-NG and NG-PVCs */
201 if (!(vcc->vcc.flags & ATMIO_FLAG_NG) ||
202 (vcc->vcc.flags & ATMIO_FLAG_PVC))
203 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), vcc->vcc.vpi,
207 patm_debug(sc, VCC, "Open VCC: now open");
211 * Try to close the given VCC
214 patm_close_vcc(struct patm_softc *sc, struct atmio_closevcc *arg)
217 struct patm_vcc *vcc;
220 patm_debug(sc, VCC, "Close VCC: %u.%u", arg->vpi, arg->vci);
222 if (!LEGAL_VPI(sc, arg->vpi) || !LEGAL_VCI(sc, arg->vci))
224 cid = PATM_CID(sc, arg->vpi, arg->vci);
227 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
228 /* stopped while we have analyzed the arguments */
234 if (vcc == NULL || !(vcc->vflags & PATM_VCC_OPEN)) {
239 if (vcc->vflags & PATM_VCC_TX_OPEN)
240 patm_tx_vcc_close(sc, vcc);
241 if (vcc->vflags & PATM_VCC_RX_OPEN)
242 patm_rx_vcc_close(sc, vcc);
244 if (vcc->vcc.flags & ATMIO_FLAG_ASYNC)
247 while (vcc->vflags & (PATM_VCC_TX_CLOSING | PATM_VCC_RX_CLOSING)) {
248 cv_wait(&sc->vcc_cv, &sc->mtx);
249 if (!(sc->ifp->if_drv_flags & IFF_DRV_RUNNING)) {
250 /* ups, has been stopped */
256 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX))
257 patm_tx_vcc_closed(sc, vcc);
258 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX))
259 patm_rx_vcc_closed(sc, vcc);
261 patm_vcc_closed(sc, vcc);
264 mtx_unlock(&sc->mtx);
270 * VCC has been finally closed.
273 patm_vcc_closed(struct patm_softc *sc, struct patm_vcc *vcc)
276 /* inform management about non-NG and NG-PVCs */
277 if (!(vcc->vcc.flags & ATMIO_FLAG_NG) ||
278 (vcc->vcc.flags & ATMIO_FLAG_PVC))
279 ATMEV_SEND_VCC_CHANGED(IFP2IFATM(sc->ifp), vcc->vcc.vpi,
283 sc->vccs[vcc->cid] = NULL;
284 uma_zfree(sc->vcc_zone, vcc);
288 patm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
290 struct ifreq *ifr = (struct ifreq *)data;
291 struct ifaddr *ifa = (struct ifaddr *)data;
292 struct patm_softc *sc = ifp->if_softc;
295 struct atmio_vcctable *vtab;
301 ifp->if_flags |= IFF_UP;
302 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
304 switch (ifa->ifa_addr->sa_family) {
309 ifa->ifa_rtrequest = atm_rtrequest;
315 mtx_unlock(&sc->mtx);
320 if (ifp->if_flags & IFF_UP) {
321 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
325 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
329 mtx_unlock(&sc->mtx);
334 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
337 * We need to toggle unassigned/idle cells ourself because
338 * the 77252 generates null cells for spacing. When switching
339 * null cells of it gets the timing wrong.
342 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
343 if (sc->utopia.state & UTP_ST_UNASS) {
344 if (!(sc->flags & PATM_UNASS)) {
345 cfg = patm_nor_read(sc, IDT_NOR_CFG);
346 cfg &= ~IDT_CFG_IDLECLP;
347 patm_nor_write(sc, IDT_NOR_CFG, cfg);
348 sc->flags |= PATM_UNASS;
351 if (sc->flags & PATM_UNASS) {
352 cfg = patm_nor_read(sc, IDT_NOR_CFG);
353 cfg |= IDT_CFG_IDLECLP;
354 patm_nor_write(sc, IDT_NOR_CFG, cfg);
355 sc->flags &= ~PATM_UNASS;
359 if (sc->utopia.state & UTP_ST_UNASS)
360 sc->flags |= PATM_UNASS;
362 sc->flags &= ~PATM_UNASS;
364 mtx_unlock(&sc->mtx);
369 * Set the interface MTU.
371 if (ifr->ifr_mtu > ATMMTU)
374 ifp->if_mtu = ifr->ifr_mtu;
377 case SIOCATMOPENVCC: /* kernel internal use */
378 error = patm_open_vcc(sc, (struct atmio_openvcc *)data);
381 case SIOCATMCLOSEVCC: /* kernel internal use */
382 error = patm_close_vcc(sc, (struct atmio_closevcc *)data);
385 case SIOCATMGVCCS: /* external use */
386 /* return vcc table */
387 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
388 sc->mmap->max_conn, sc->vccs_open, &sc->mtx, 1);
389 error = copyout(vtab, ifr->ifr_data, sizeof(*vtab) +
390 vtab->count * sizeof(vtab->vccs[0]));
391 free(vtab, M_DEVBUF);
394 case SIOCATMGETVCCS: /* netgraph internal use */
395 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
396 sc->mmap->max_conn, sc->vccs_open, &sc->mtx, 0);
401 *(void **)data = vtab;
405 patm_debug(sc, IOCTL, "unknown cmd=%08lx arg=%p", cmd, data);