Deleted Added
sdiff udiff text old ( 118601 ) new ( 119418 )
full compact
1/*
2 * Copyright (c) 2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 * Author: Hartmut Brandt <harti@freebsd.org>
28 *
29 * Driver for IDT77252 based cards like ProSum's.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/patm/if_patm_ioctl.c 119418 2003-08-24 17:55:58Z obrien $");
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/dev/patm/if_patm_ioctl.c 119418 2003-08-24 17:55:58Z obrien $");
36
37#include "opt_inet.h"
38#include "opt_natm.h"
39
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>
45#include <sys/bus.h>
46#include <sys/errno.h>
47#include <sys/conf.h>
48#include <sys/module.h>
49#include <sys/lock.h>
50#include <sys/mutex.h>
51#include <sys/sysctl.h>
52#include <sys/queue.h>
53#include <sys/condvar.h>
54#include <vm/uma.h>
55
56#include <sys/sockio.h>
57#include <sys/mbuf.h>
58#include <sys/socket.h>
59
60#include <net/if.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>
66
67#include <machine/bus.h>
68#include <machine/resource.h>
69#include <sys/bus.h>
70#include <sys/rman.h>
71#include <sys/mbpool.h>
72
73#include <dev/utopia/utopia.h>
74#include <dev/patm/idt77252reg.h>
75#include <dev/patm/if_patmvar.h>
76
77/*
78 * Open the VCC with the given parameters
79 */
80static int
81patm_open_vcc(struct patm_softc *sc, struct atmio_openvcc *arg)
82{
83 u_int cid;
84 struct patm_vcc *vcc;
85 int error = 0;
86
87 patm_debug(sc, VCC, "Open VCC: %u.%u flags=%#x", arg->param.vpi,
88 arg->param.vci, arg->param.flags);
89
90 if (!LEGAL_VPI(sc, arg->param.vpi) || !LEGAL_VCI(sc, arg->param.vci))
91 return (EINVAL);
92 if (arg->param.vci == 0 && (arg->param.vpi != 0 ||
93 !(arg->param.flags & ATMIO_FLAG_NOTX) ||
94 arg->param.aal != ATMIO_AAL_RAW))
95 return (EINVAL);
96 cid = PATM_CID(sc, arg->param.vpi, arg->param.vci);
97
98 if ((arg->param.flags & ATMIO_FLAG_NOTX) &&
99 (arg->param.flags & ATMIO_FLAG_NORX))
100 return (EINVAL);
101
102 if ((arg->param.traffic == ATMIO_TRAFFIC_ABR) &&
103 (arg->param.flags & (ATMIO_FLAG_NOTX | ATMIO_FLAG_NORX)))
104 return (EINVAL);
105
106 /* allocate vcc */
107 vcc = uma_zalloc(sc->vcc_zone, M_NOWAIT | M_ZERO);
108 if (vcc == NULL)
109 return (ENOMEM);
110
111 mtx_lock(&sc->mtx);
112 if (!(sc->ifatm.ifnet.if_flags & IFF_RUNNING)) {
113 /* stopped while we have analyzed the arguments */
114 error = EIO;
115 goto done;
116 }
117 if (sc->vccs[cid] != NULL) {
118 /* ups, already open */
119 error = EBUSY;
120 goto done;
121 }
122
123 /* check some parameters */
124 vcc->cid = cid;
125 vcc->vcc = arg->param;
126 vcc->vflags = 0;
127 vcc->rxhand = arg->rxhand;
128 switch (vcc->vcc.aal) {
129
130 case ATMIO_AAL_0:
131 case ATMIO_AAL_34:
132 case ATMIO_AAL_5:
133 break;
134
135 case ATMIO_AAL_RAW:
136 if (arg->param.vci == 0 &&
137 !(arg->param.flags & ATMIO_FLAG_NOTX)) {
138 error = EINVAL;
139 goto done;
140 }
141 break;
142
143 default:
144 error = EINVAL;
145 goto done;
146 }
147 switch (vcc->vcc.traffic) {
148
149 case ATMIO_TRAFFIC_VBR:
150 case ATMIO_TRAFFIC_UBR:
151 case ATMIO_TRAFFIC_CBR:
152 case ATMIO_TRAFFIC_ABR:
153 break;
154
155 default:
156 error = EINVAL;
157 goto done;
158 }
159
160 /* initialize */
161 vcc->chain = NULL;
162 vcc->last = NULL;
163 vcc->ibytes = vcc->ipackets = 0;
164 vcc->obytes = vcc->opackets = 0;
165
166 /* ask the TX and RX sides */
167 patm_debug(sc, VCC, "Open VCC: asking Rx/Tx");
168 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX) &&
169 (error = patm_tx_vcc_can_open(sc, vcc)) != 0)
170 goto done;
171 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX) &&
172 (error = patm_rx_vcc_can_open(sc, vcc)) != 0)
173 goto done;
174
175 /* ok - go ahead */
176 sc->vccs[cid] = vcc;
177 patm_load_vc(sc, vcc, 0);
178
179 /* don't free below */
180 vcc = NULL;
181 sc->vccs_open++;
182
183 /* done */
184 done:
185 mtx_unlock(&sc->mtx);
186 if (vcc != NULL)
187 uma_zfree(sc->vcc_zone, vcc);
188 return (error);
189}
190
191void
192patm_load_vc(struct patm_softc *sc, struct patm_vcc *vcc, int reload)
193{
194
195 patm_debug(sc, VCC, "Open VCC: opening");
196 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX))
197 patm_tx_vcc_open(sc, vcc);
198 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX))
199 patm_rx_vcc_open(sc, vcc);
200
201 if (!reload) {
202 /* inform management about non-NG and NG-PVCs */
203 if (!(vcc->vcc.flags & ATMIO_FLAG_NG) ||
204 (vcc->vcc.flags & ATMIO_FLAG_PVC))
205 ATMEV_SEND_VCC_CHANGED(&sc->ifatm, vcc->vcc.vpi,
206 vcc->vcc.vci, 1);
207 }
208
209 patm_debug(sc, VCC, "Open VCC: now open");
210}
211
212/*
213 * Try to close the given VCC
214 */
215static int
216patm_close_vcc(struct patm_softc *sc, struct atmio_closevcc *arg)
217{
218 u_int cid;
219 struct patm_vcc *vcc;
220 int error = 0;
221
222 patm_debug(sc, VCC, "Close VCC: %u.%u", arg->vpi, arg->vci);
223
224 if (!LEGAL_VPI(sc, arg->vpi) || !LEGAL_VCI(sc, arg->vci))
225 return (EINVAL);
226 cid = PATM_CID(sc, arg->vpi, arg->vci);
227
228 mtx_lock(&sc->mtx);
229 if (!(sc->ifatm.ifnet.if_flags & IFF_RUNNING)) {
230 /* stopped while we have analyzed the arguments */
231 error = EIO;
232 goto done;
233 }
234
235 vcc = sc->vccs[cid];
236 if (vcc == NULL || !(vcc->vflags & PATM_VCC_OPEN)) {
237 error = ENOENT;
238 goto done;
239 }
240
241 if (vcc->vflags & PATM_VCC_TX_OPEN)
242 patm_tx_vcc_close(sc, vcc);
243 if (vcc->vflags & PATM_VCC_RX_OPEN)
244 patm_rx_vcc_close(sc, vcc);
245
246 if (vcc->vcc.flags & ATMIO_FLAG_ASYNC)
247 goto done;
248
249 while (vcc->vflags & (PATM_VCC_TX_CLOSING | PATM_VCC_RX_CLOSING)) {
250 cv_wait(&sc->vcc_cv, &sc->mtx);
251 if (!(sc->ifatm.ifnet.if_flags & IFF_RUNNING)) {
252 /* ups, has been stopped */
253 error = EIO;
254 goto done;
255 }
256 }
257
258 if (!(vcc->vcc.flags & ATMIO_FLAG_NOTX))
259 patm_tx_vcc_closed(sc, vcc);
260 if (!(vcc->vcc.flags & ATMIO_FLAG_NORX))
261 patm_rx_vcc_closed(sc, vcc);
262
263 patm_vcc_closed(sc, vcc);
264
265 done:
266 mtx_unlock(&sc->mtx);
267
268 return (error);
269}
270
271/*
272 * VCC has been finally closed.
273 */
274void
275patm_vcc_closed(struct patm_softc *sc, struct patm_vcc *vcc)
276{
277
278 /* inform management about non-NG and NG-PVCs */
279 if (!(vcc->vcc.flags & ATMIO_FLAG_NG) ||
280 (vcc->vcc.flags & ATMIO_FLAG_PVC))
281 ATMEV_SEND_VCC_CHANGED(&sc->ifatm, vcc->vcc.vpi,
282 vcc->vcc.vci, 0);
283
284 sc->vccs_open--;
285 sc->vccs[vcc->cid] = NULL;
286 uma_zfree(sc->vcc_zone, vcc);
287}
288
289int
290patm_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
291{
292 struct ifreq *ifr = (struct ifreq *)data;
293 struct ifaddr *ifa = (struct ifaddr *)data;
294 struct patm_softc *sc = ifp->if_softc;
295 int error = 0;
296 uint32_t cfg;
297 struct atmio_vcctable *vtab;
298
299 switch (cmd) {
300
301 case SIOCSIFADDR:
302 mtx_lock(&sc->mtx);
303 ifp->if_flags |= IFF_UP;
304 if (!(ifp->if_flags & IFF_RUNNING))
305 patm_initialize(sc);
306 switch (ifa->ifa_addr->sa_family) {
307
308#ifdef INET
309 case AF_INET:
310 case AF_INET6:
311 ifa->ifa_rtrequest = atm_rtrequest;
312 break;
313#endif
314 default:
315 break;
316 }
317 mtx_unlock(&sc->mtx);
318 break;
319
320 case SIOCSIFFLAGS:
321 mtx_lock(&sc->mtx);
322 if (ifp->if_flags & IFF_UP) {
323 if (!(ifp->if_flags & IFF_RUNNING)) {
324 patm_initialize(sc);
325 }
326 } else {
327 if (ifp->if_flags & IFF_RUNNING) {
328 patm_stop(sc);
329 }
330 }
331 mtx_unlock(&sc->mtx);
332 break;
333
334 case SIOCGIFMEDIA:
335 case SIOCSIFMEDIA:
336 error = ifmedia_ioctl(ifp, ifr, &sc->media, cmd);
337
338 /*
339 * We need to toggle unassigned/idle cells ourself because
340 * the 77252 generates null cells for spacing. When switching
341 * null cells of it gets the timing wrong.
342 */
343 mtx_lock(&sc->mtx);
344 if (ifp->if_flags & IFF_RUNNING) {
345 if (sc->utopia.state & UTP_ST_UNASS) {
346 if (!(sc->flags & PATM_UNASS)) {
347 cfg = patm_nor_read(sc, IDT_NOR_CFG);
348 cfg &= ~IDT_CFG_IDLECLP;
349 patm_nor_write(sc, IDT_NOR_CFG, cfg);
350 sc->flags |= PATM_UNASS;
351 }
352 } else {
353 if (sc->flags & PATM_UNASS) {
354 cfg = patm_nor_read(sc, IDT_NOR_CFG);
355 cfg |= IDT_CFG_IDLECLP;
356 patm_nor_write(sc, IDT_NOR_CFG, cfg);
357 sc->flags &= ~PATM_UNASS;
358 }
359 }
360 } else {
361 if (sc->utopia.state & UTP_ST_UNASS)
362 sc->flags |= PATM_UNASS;
363 else
364 sc->flags &= ~PATM_UNASS;
365 }
366 mtx_unlock(&sc->mtx);
367 break;
368
369 case SIOCSIFMTU:
370 /*
371 * Set the interface MTU.
372 */
373 if (ifr->ifr_mtu > ATMMTU)
374 error = EINVAL;
375 else
376 ifp->if_mtu = ifr->ifr_mtu;
377 break;
378
379 case SIOCATMOPENVCC: /* kernel internal use */
380 error = patm_open_vcc(sc, (struct atmio_openvcc *)data);
381 break;
382
383 case SIOCATMCLOSEVCC: /* kernel internal use */
384 error = patm_close_vcc(sc, (struct atmio_closevcc *)data);
385 break;
386
387 case SIOCATMGVCCS: /* external use */
388 /* return vcc table */
389 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
390 sc->mmap->max_conn, sc->vccs_open, &sc->mtx, 1);
391 error = copyout(vtab, ifr->ifr_data, sizeof(*vtab) +
392 vtab->count * sizeof(vtab->vccs[0]));
393 free(vtab, M_DEVBUF);
394 break;
395
396 case SIOCATMGETVCCS: /* netgraph internal use */
397 vtab = atm_getvccs((struct atmio_vcc **)sc->vccs,
398 sc->mmap->max_conn, sc->vccs_open, &sc->mtx, 0);
399 if (vtab == NULL) {
400 error = ENOMEM;
401 break;
402 }
403 *(void **)data = vtab;
404 break;
405
406 default:
407 patm_debug(sc, IOCTL, "unknown cmd=%08lx arg=%p", cmd, data);
408 error = EINVAL;
409 break;
410 }
411
412 return (error);
413}