1139749Simp/*-
2116491Sharti * Copyright (c) 2001-2003
3116491Sharti *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4116491Sharti * 	All rights reserved.
5116491Sharti *
6116491Sharti * Redistribution and use in source and binary forms, with or without
7116491Sharti * modification, are permitted provided that the following conditions
8116491Sharti * are met:
9116491Sharti * 1. Redistributions of source code must retain the above copyright
10116491Sharti *    notice, this list of conditions and the following disclaimer.
11116491Sharti * 2. Redistributions in binary form must reproduce the above copyright
12116491Sharti *    notice, this list of conditions and the following disclaimer in the
13116491Sharti *    documentation and/or other materials provided with the distribution.
14116491Sharti *
15116491Sharti * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16116491Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17116491Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18116491Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19116491Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20116491Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21116491Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22116491Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23116491Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24116491Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25116491Sharti * SUCH DAMAGE.
26116491Sharti *
27116491Sharti * Author: Hartmut Brandt <harti@freebsd.org>
28116491Sharti *
29116491Sharti * ForeHE driver.
30116491Sharti *
31116491Sharti * Transmission.
32116491Sharti */
33116491Sharti
34116519Sharti#include <sys/cdefs.h>
35116519Sharti__FBSDID("$FreeBSD$");
36116519Sharti
37116491Sharti#include "opt_inet.h"
38116491Sharti#include "opt_natm.h"
39116491Sharti
40116491Sharti#include <sys/types.h>
41116491Sharti#include <sys/param.h>
42116491Sharti#include <sys/systm.h>
43116491Sharti#include <sys/kernel.h>
44116491Sharti#include <sys/malloc.h>
45116491Sharti#include <sys/bus.h>
46116491Sharti#include <sys/errno.h>
47116491Sharti#include <sys/conf.h>
48116491Sharti#include <sys/module.h>
49116491Sharti#include <sys/queue.h>
50116491Sharti#include <sys/syslog.h>
51116491Sharti#include <sys/condvar.h>
52116491Sharti#include <sys/sysctl.h>
53116491Sharti#include <vm/uma.h>
54116491Sharti
55116491Sharti#include <sys/sockio.h>
56116491Sharti#include <sys/mbuf.h>
57116491Sharti#include <sys/socket.h>
58116491Sharti
59116491Sharti#include <net/if.h>
60116491Sharti#include <net/if_media.h>
61116491Sharti#include <net/if_atm.h>
62116491Sharti#include <net/route.h>
63116491Sharti#ifdef ENABLE_BPF
64116491Sharti#include <net/bpf.h>
65116491Sharti#endif
66116491Sharti#include <netinet/in.h>
67116491Sharti#include <netinet/if_atm.h>
68116491Sharti
69116491Sharti#include <machine/bus.h>
70116491Sharti#include <machine/resource.h>
71116491Sharti#include <sys/bus.h>
72116491Sharti#include <sys/rman.h>
73119280Simp#include <dev/pci/pcireg.h>
74119280Simp#include <dev/pci/pcivar.h>
75116491Sharti
76116491Sharti#include <dev/utopia/utopia.h>
77116491Sharti#include <dev/hatm/if_hatmconf.h>
78116491Sharti#include <dev/hatm/if_hatmreg.h>
79116491Sharti#include <dev/hatm/if_hatmvar.h>
80116491Sharti
81122112Sharti
82116491Sharti/*
83122112Sharti * These macros are used to trace the flow of transmit mbufs and to
84122112Sharti * detect transmit mbuf leaks in the driver.
85122112Sharti */
86122112Sharti#ifdef HATM_DEBUG
87122112Sharti#define	hatm_free_txmbuf(SC)						\
88122112Sharti	do {								\
89122112Sharti		if (--sc->txmbuf < 0)					\
90122112Sharti			DBG(sc, TX, ("txmbuf below 0!"));		\
91122112Sharti		else if (sc->txmbuf == 0)				\
92122112Sharti			DBG(sc, TX, ("txmbuf now 0"));			\
93122112Sharti	} while (0)
94122112Sharti#define	hatm_get_txmbuf(SC)						\
95122112Sharti	do {								\
96122112Sharti		if (++sc->txmbuf > 20000)				\
97146617Sharti			DBG(sc,	TX, ("txmbuf %u", sc->txmbuf));		\
98122112Sharti		else if (sc->txmbuf == 1)				\
99122112Sharti			DBG(sc, TX, ("txmbuf leaves 0"));		\
100122112Sharti	} while (0)
101122112Sharti#else
102122112Sharti#define	hatm_free_txmbuf(SC)	do { } while (0)
103122112Sharti#define	hatm_get_txmbuf(SC)	do { } while (0)
104122112Sharti#endif
105122112Sharti
106122112Sharti/*
107116491Sharti * Allocate a new TPD, zero the TPD part. Cannot return NULL if
108116491Sharti * flag is 0. The TPD is removed from the free list and its used
109116491Sharti * bit is set.
110116491Sharti */
111116491Shartistatic struct tpd *
112116491Shartihatm_alloc_tpd(struct hatm_softc *sc, u_int flags)
113116491Sharti{
114116491Sharti	struct tpd *t;
115116491Sharti
116116491Sharti	/* if we allocate a transmit TPD check for the reserve */
117116491Sharti	if (flags & M_NOWAIT) {
118116491Sharti		if (sc->tpd_nfree <= HE_CONFIG_TPD_RESERVE)
119116491Sharti			return (NULL);
120116491Sharti	} else {
121116491Sharti		if (sc->tpd_nfree == 0)
122116491Sharti			return (NULL);
123116491Sharti	}
124116491Sharti
125116491Sharti	/* make it beeing used */
126116491Sharti	t = SLIST_FIRST(&sc->tpd_free);
127116491Sharti	KASSERT(t != NULL, ("tpd botch"));
128116491Sharti	SLIST_REMOVE_HEAD(&sc->tpd_free, link);
129116491Sharti	TPD_SET_USED(sc, t->no);
130116491Sharti	sc->tpd_nfree--;
131116491Sharti
132116491Sharti	/* initialize */
133116491Sharti	t->mbuf = NULL;
134116491Sharti	t->cid = 0;
135116491Sharti	bzero(&t->tpd, sizeof(t->tpd));
136116491Sharti	t->tpd.addr = t->no << HE_REGS_TPD_ADDR;
137116491Sharti
138116491Sharti	return (t);
139116491Sharti}
140116491Sharti
141116491Sharti/*
142116491Sharti * Free a TPD. If the mbuf pointer in that TPD is not zero, it is assumed, that
143116491Sharti * the DMA map of this TPD was used to load this mbuf. The map is unloaded
144116491Sharti * and the mbuf is freed. The TPD is put back onto the free list and
145116491Sharti * its used bit is cleared.
146116491Sharti */
147116491Shartistatic void
148116491Shartihatm_free_tpd(struct hatm_softc *sc, struct tpd *tpd)
149116491Sharti{
150116491Sharti	if (tpd->mbuf != NULL) {
151116491Sharti		bus_dmamap_unload(sc->tx_tag, tpd->map);
152122112Sharti		hatm_free_txmbuf(sc);
153116491Sharti		m_freem(tpd->mbuf);
154116491Sharti		tpd->mbuf = NULL;
155116491Sharti	}
156116491Sharti
157116491Sharti	/* insert TPD into free list */
158116491Sharti	SLIST_INSERT_HEAD(&sc->tpd_free, tpd, link);
159116491Sharti	TPD_CLR_USED(sc, tpd->no);
160116491Sharti	sc->tpd_nfree++;
161116491Sharti}
162116491Sharti
163116491Sharti/*
164116491Sharti * Queue a number of TPD. If there is not enough space none of the TPDs
165116491Sharti * is queued and an error code is returned.
166116491Sharti */
167116491Shartistatic int
168116491Shartihatm_queue_tpds(struct hatm_softc *sc, u_int count, struct tpd **list,
169116491Sharti    u_int cid)
170116491Sharti{
171116491Sharti	u_int space;
172116491Sharti	u_int i;
173116491Sharti
174116491Sharti	if (count >= sc->tpdrq.size) {
175116491Sharti		sc->istats.tdprq_full++;
176116491Sharti		return (EBUSY);
177116491Sharti	}
178116491Sharti
179116491Sharti	if (sc->tpdrq.tail < sc->tpdrq.head)
180116491Sharti		space = sc->tpdrq.head - sc->tpdrq.tail;
181116491Sharti	else
182116491Sharti		space = sc->tpdrq.head - sc->tpdrq.tail +  sc->tpdrq.size;
183116491Sharti
184116491Sharti	if (space <= count) {
185116491Sharti		sc->tpdrq.head =
186116491Sharti		    (READ4(sc, HE_REGO_TPDRQ_H) >> HE_REGS_TPDRQ_H_H) &
187116491Sharti		    (sc->tpdrq.size - 1);
188116491Sharti
189116491Sharti		if (sc->tpdrq.tail < sc->tpdrq.head)
190116491Sharti			space = sc->tpdrq.head - sc->tpdrq.tail;
191116491Sharti		else
192116491Sharti			space = sc->tpdrq.head - sc->tpdrq.tail +
193116491Sharti			    sc->tpdrq.size;
194116491Sharti
195116491Sharti		if (space <= count) {
196147256Sbrooks			if_printf(sc->ifp, "TPDRQ full\n");
197116491Sharti			sc->istats.tdprq_full++;
198116491Sharti			return (EBUSY);
199116491Sharti		}
200116491Sharti	}
201116491Sharti
202116491Sharti	/* we are going to write to the TPD queue space */
203116491Sharti	bus_dmamap_sync(sc->tpdrq.mem.tag, sc->tpdrq.mem.map,
204116491Sharti	    BUS_DMASYNC_PREWRITE);
205116491Sharti
206116491Sharti	/* put the entries into the TPD space */
207116491Sharti	for (i = 0; i < count; i++) {
208116491Sharti		/* we are going to 'write' the TPD to the device */
209116491Sharti		bus_dmamap_sync(sc->tpds.tag, sc->tpds.map,
210116491Sharti		    BUS_DMASYNC_PREWRITE);
211116491Sharti
212116491Sharti		sc->tpdrq.tpdrq[sc->tpdrq.tail].tpd =
213116491Sharti		    sc->tpds.paddr + HE_TPD_SIZE * list[i]->no;
214116491Sharti		sc->tpdrq.tpdrq[sc->tpdrq.tail].cid = cid;
215116491Sharti
216116491Sharti		if (++sc->tpdrq.tail == sc->tpdrq.size)
217116491Sharti			sc->tpdrq.tail = 0;
218116491Sharti	}
219116491Sharti
220116491Sharti	/* update tail pointer */
221116491Sharti	WRITE4(sc, HE_REGO_TPDRQ_T, (sc->tpdrq.tail << HE_REGS_TPDRQ_T_T));
222116491Sharti
223116491Sharti	return (0);
224116491Sharti}
225116491Sharti
226116491Sharti/*
227116491Sharti * Helper struct for communication with the DMA load helper.
228116491Sharti */
229116491Shartistruct load_txbuf_arg {
230116491Sharti	struct hatm_softc *sc;
231116491Sharti	struct tpd *first;
232116491Sharti	struct mbuf *mbuf;
233116491Sharti	struct hevcc *vcc;
234116491Sharti	int error;
235116491Sharti	u_int pti;
236116491Sharti	u_int vpi, vci;
237116491Sharti};
238116491Sharti
239116491Sharti/*
240116491Sharti * Loader callback for the mbuf. This function allocates the TPDs and
241116491Sharti * fills them. It puts the dmamap and and the mbuf pointer into the last
242116491Sharti * TPD and then tries to queue all the TPDs. If anything fails, all TPDs
243116491Sharti * allocated by this function are freed and the error flag is set in the
244116491Sharti * argument structure. The first TPD must then be freed by the caller.
245116491Sharti */
246116491Shartistatic void
247116491Shartihatm_load_txbuf(void *uarg, bus_dma_segment_t *segs, int nseg,
248116491Sharti    bus_size_t mapsize, int error)
249116491Sharti{
250116491Sharti	struct load_txbuf_arg *arg = uarg;
251116491Sharti	u_int tpds_needed, i, n, tpd_cnt;
252116491Sharti	int need_intr;
253116491Sharti	struct tpd *tpd;
254116491Sharti	struct tpd *tpd_list[HE_CONFIG_MAX_TPD_PER_PACKET];
255116491Sharti
256116491Sharti	if (error != 0) {
257116491Sharti		DBG(arg->sc, DMA, ("%s -- error=%d plen=%d\n",
258116491Sharti		    __func__, error, arg->mbuf->m_pkthdr.len));
259116491Sharti		return;
260116491Sharti	}
261116491Sharti
262116491Sharti	/* ensure, we have enough TPDs (remember, we already have one) */
263116491Sharti	tpds_needed = (nseg + 2) / 3;
264116491Sharti	if (HE_CONFIG_TPD_RESERVE + tpds_needed - 1 > arg->sc->tpd_nfree) {
265147256Sbrooks		if_printf(arg->sc->ifp, "%s -- out of TPDs (need %d, "
266116491Sharti		    "have %u)\n", __func__, tpds_needed - 1,
267116491Sharti		    arg->sc->tpd_nfree + 1);
268116491Sharti		arg->error = 1;
269116491Sharti		return;
270116491Sharti	}
271116491Sharti
272116491Sharti	/*
273116491Sharti	 * Check for the maximum number of TPDs on the connection.
274116491Sharti	 */
275116491Sharti	need_intr = 0;
276116491Sharti	if (arg->sc->max_tpd > 0) {
277116491Sharti		if (arg->vcc->ntpds + tpds_needed > arg->sc->max_tpd) {
278116491Sharti			arg->sc->istats.flow_closed++;
279116491Sharti			arg->vcc->vflags |= HE_VCC_FLOW_CTRL;
280147256Sbrooks			ATMEV_SEND_FLOW_CONTROL(IFP2IFATM(arg->sc->ifp),
281118170Sharti			    arg->vpi, arg->vci, 1);
282116491Sharti			arg->error = 1;
283116491Sharti			return;
284116491Sharti		}
285116491Sharti		if (arg->vcc->ntpds + tpds_needed >
286116491Sharti		    (9 * arg->sc->max_tpd) / 10)
287116491Sharti			need_intr = 1;
288116491Sharti	}
289116491Sharti
290116491Sharti	tpd = arg->first;
291116491Sharti	tpd_cnt = 0;
292116491Sharti	tpd_list[tpd_cnt++] = tpd;
293116491Sharti	for (i = n = 0; i < nseg; i++, n++) {
294116491Sharti		if (n == 3) {
295116491Sharti			if ((tpd = hatm_alloc_tpd(arg->sc, M_NOWAIT)) == NULL)
296116491Sharti				/* may not fail (see check above) */
297116491Sharti				panic("%s: out of TPDs", __func__);
298116491Sharti			tpd->cid = arg->first->cid;
299116491Sharti			tpd->tpd.addr |= arg->pti;
300116491Sharti			tpd_list[tpd_cnt++] = tpd;
301116491Sharti			n = 0;
302116491Sharti		}
303116491Sharti		KASSERT(segs[i].ds_addr <= 0xffffffffLU,
304116491Sharti		    ("phys addr too large %lx", (u_long)segs[i].ds_addr));
305116491Sharti
306116491Sharti		DBG(arg->sc, DMA, ("DMA loaded: %lx/%lu",
307116491Sharti		    (u_long)segs[i].ds_addr, (u_long)segs[i].ds_len));
308116491Sharti
309116491Sharti		tpd->tpd.bufs[n].addr = segs[i].ds_addr;
310116491Sharti		tpd->tpd.bufs[n].len = segs[i].ds_len;
311116491Sharti
312116491Sharti		DBG(arg->sc, TX, ("seg[%u]=tpd[%u,%u]=%x/%u", i,
313116491Sharti		    tpd_cnt, n, tpd->tpd.bufs[n].addr, tpd->tpd.bufs[n].len));
314116491Sharti
315116491Sharti		if (i == nseg - 1)
316116491Sharti			tpd->tpd.bufs[n].len |= HE_REGM_TPD_LST;
317116491Sharti	}
318116491Sharti
319116491Sharti	/*
320116491Sharti	 * Swap the MAP in the first and the last TPD and set the mbuf
321116491Sharti	 * pointer into the last TPD. We use the map in the last TPD, because
322116491Sharti	 * the map must stay valid until the last TPD is processed by the card.
323116491Sharti	 */
324116491Sharti	if (tpd_cnt > 1) {
325116491Sharti		bus_dmamap_t tmp;
326116491Sharti
327116491Sharti		tmp = arg->first->map;
328116491Sharti		arg->first->map = tpd_list[tpd_cnt - 1]->map;
329116491Sharti		tpd_list[tpd_cnt - 1]->map = tmp;
330116491Sharti	}
331116491Sharti	tpd_list[tpd_cnt - 1]->mbuf = arg->mbuf;
332116491Sharti
333116491Sharti	if (need_intr)
334116491Sharti		tpd_list[tpd_cnt - 1]->tpd.addr |= HE_REGM_TPD_INTR;
335116491Sharti
336116491Sharti	/* queue the TPDs */
337116491Sharti	if (hatm_queue_tpds(arg->sc, tpd_cnt, tpd_list, arg->first->cid)) {
338116491Sharti		/* free all, except the first TPD */
339116491Sharti		for (i = 1; i < tpd_cnt; i++)
340116491Sharti			hatm_free_tpd(arg->sc, tpd_list[i]);
341116491Sharti		arg->error = 1;
342116491Sharti		return;
343116491Sharti	}
344116491Sharti	arg->vcc->ntpds += tpd_cnt;
345116491Sharti}
346116491Sharti
347116491Sharti
348116491Sharti/*
349116491Sharti * Start output on the interface
350116491Sharti */
351116491Shartivoid
352116491Shartihatm_start(struct ifnet *ifp)
353116491Sharti{
354147721Sharti	struct hatm_softc *sc = ifp->if_softc;
355116491Sharti	struct mbuf *m;
356116491Sharti	struct atm_pseudohdr *aph;
357116491Sharti	u_int cid;
358116491Sharti	struct tpd *tpd;
359116491Sharti	struct load_txbuf_arg arg;
360116491Sharti	u_int len;
361116491Sharti	int error;
362116491Sharti
363148887Srwatson	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
364116491Sharti		return;
365116491Sharti	mtx_lock(&sc->mtx);
366116491Sharti	arg.sc = sc;
367116491Sharti
368116491Sharti	while (1) {
369116491Sharti		IF_DEQUEUE(&ifp->if_snd, m);
370116491Sharti		if (m == NULL)
371116491Sharti			break;
372116491Sharti
373122112Sharti		hatm_get_txmbuf(sc);
374122112Sharti
375116491Sharti		if (m->m_len < sizeof(*aph))
376122112Sharti			if ((m = m_pullup(m, sizeof(*aph))) == NULL) {
377122112Sharti				hatm_free_txmbuf(sc);
378116491Sharti				continue;
379122112Sharti			}
380116491Sharti
381116491Sharti		aph = mtod(m, struct atm_pseudohdr *);
382116491Sharti		arg.vci = ATM_PH_VCI(aph);
383116491Sharti		arg.vpi = ATM_PH_VPI(aph);
384116491Sharti		m_adj(m, sizeof(*aph));
385116491Sharti
386116491Sharti		if ((len = m->m_pkthdr.len) == 0) {
387122112Sharti			hatm_free_txmbuf(sc);
388116491Sharti			m_freem(m);
389116491Sharti			continue;
390116491Sharti		}
391116491Sharti
392116491Sharti		if ((arg.vpi & ~HE_VPI_MASK) || (arg.vci & ~HE_VCI_MASK) ||
393116491Sharti		    (arg.vci == 0)) {
394122112Sharti			hatm_free_txmbuf(sc);
395116491Sharti			m_freem(m);
396116491Sharti			continue;
397116491Sharti		}
398116491Sharti		cid = HE_CID(arg.vpi, arg.vci);
399116491Sharti		arg.vcc = sc->vccs[cid];
400116491Sharti
401116491Sharti		if (arg.vcc == NULL || !(arg.vcc->vflags & HE_VCC_OPEN)) {
402122112Sharti			hatm_free_txmbuf(sc);
403116491Sharti			m_freem(m);
404116491Sharti			continue;
405116491Sharti		}
406116491Sharti		if (arg.vcc->vflags & HE_VCC_FLOW_CTRL) {
407122112Sharti			hatm_free_txmbuf(sc);
408116491Sharti			m_freem(m);
409116491Sharti			sc->istats.flow_drop++;
410116491Sharti			continue;
411116491Sharti		}
412116491Sharti
413116491Sharti		arg.pti = 0;
414116491Sharti		if (arg.vcc->param.aal == ATMIO_AAL_RAW) {
415116491Sharti			if (len < 52) {
416121687Sharti				/* too short */
417122112Sharti				hatm_free_txmbuf(sc);
418116491Sharti				m_freem(m);
419116491Sharti				continue;
420116491Sharti			}
421121687Sharti
422121687Sharti			/*
423121687Sharti			 * Get the header and ignore except
424121687Sharti			 * payload type and CLP.
425121687Sharti			 */
426122112Sharti			if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
427122112Sharti				hatm_free_txmbuf(sc);
428116491Sharti				continue;
429122112Sharti			}
430116491Sharti			arg.pti = mtod(m, u_char *)[3] & 0xf;
431116491Sharti			arg.pti = ((arg.pti & 0xe) << 2) | ((arg.pti & 1) << 1);
432116491Sharti			m_adj(m, 4);
433116491Sharti			len -= 4;
434121687Sharti
435121687Sharti			if (len % 48 != 0) {
436121687Sharti				m_adj(m, -((int)(len % 48)));
437121687Sharti				len -= len % 48;
438121687Sharti			}
439116491Sharti		}
440116491Sharti
441116491Sharti#ifdef ENABLE_BPF
442116491Sharti		if (!(arg.vcc->param.flags & ATMIO_FLAG_NG) &&
443118540Sharti		    (arg.vcc->param.aal == ATMIO_AAL_5) &&
444116491Sharti		    (arg.vcc->param.flags & ATM_PH_LLCSNAP))
445116491Sharti		 	BPF_MTAP(ifp, m);
446116491Sharti#endif
447116491Sharti
448116491Sharti		/* Now load a DMA map with the packet. Allocate the first
449116491Sharti		 * TPD to get a map. Additional TPDs may be allocated by the
450116491Sharti		 * callback. */
451116491Sharti		if ((tpd = hatm_alloc_tpd(sc, M_NOWAIT)) == NULL) {
452122112Sharti			hatm_free_txmbuf(sc);
453116491Sharti			m_freem(m);
454147256Sbrooks			sc->ifp->if_oerrors++;
455116491Sharti			continue;
456116491Sharti		}
457116491Sharti		tpd->cid = cid;
458116491Sharti		tpd->tpd.addr |= arg.pti;
459116491Sharti		arg.first = tpd;
460116491Sharti		arg.error = 0;
461116491Sharti		arg.mbuf = m;
462116491Sharti
463116491Sharti		error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m,
464117382Sharti		    hatm_load_txbuf, &arg, BUS_DMA_NOWAIT);
465116491Sharti
466116491Sharti		if (error == EFBIG) {
467116491Sharti			/* try to defragment the packet */
468116491Sharti			sc->istats.defrag++;
469243857Sglebius			m = m_defrag(m, M_NOWAIT);
470116491Sharti			if (m == NULL) {
471122112Sharti				tpd->mbuf = NULL;
472122112Sharti				hatm_free_txmbuf(sc);
473122112Sharti				hatm_free_tpd(sc, tpd);
474147256Sbrooks				sc->ifp->if_oerrors++;
475116491Sharti				continue;
476116491Sharti			}
477116491Sharti			arg.mbuf = m;
478116491Sharti			error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m,
479117382Sharti			    hatm_load_txbuf, &arg, BUS_DMA_NOWAIT);
480116491Sharti		}
481116491Sharti
482116491Sharti		if (error != 0) {
483147256Sbrooks			if_printf(sc->ifp, "mbuf loaded error=%d\n",
484116491Sharti			    error);
485116491Sharti			hatm_free_tpd(sc, tpd);
486147256Sbrooks			sc->ifp->if_oerrors++;
487116491Sharti			continue;
488116491Sharti		}
489116491Sharti		if (arg.error) {
490116491Sharti			hatm_free_tpd(sc, tpd);
491147256Sbrooks			sc->ifp->if_oerrors++;
492116491Sharti			continue;
493116491Sharti		}
494116491Sharti		arg.vcc->opackets++;
495116491Sharti		arg.vcc->obytes += len;
496147256Sbrooks		sc->ifp->if_opackets++;
497116491Sharti	}
498116491Sharti	mtx_unlock(&sc->mtx);
499116491Sharti}
500116491Sharti
501116491Shartivoid
502116491Shartihatm_tx_complete(struct hatm_softc *sc, struct tpd *tpd, uint32_t flags)
503116491Sharti{
504116491Sharti	struct hevcc *vcc = sc->vccs[tpd->cid];
505116491Sharti
506116491Sharti	DBG(sc, TX, ("tx_complete cid=%#x flags=%#x", tpd->cid, flags));
507116491Sharti
508116491Sharti	if (vcc == NULL)
509116491Sharti		return;
510116491Sharti	if ((flags & HE_REGM_TBRQ_EOS) && (vcc->vflags & HE_VCC_TX_CLOSING)) {
511116491Sharti		vcc->vflags &= ~HE_VCC_TX_CLOSING;
512118540Sharti		if (vcc->param.flags & ATMIO_FLAG_ASYNC) {
513116491Sharti			hatm_tx_vcc_closed(sc, tpd->cid);
514116491Sharti			if (!(vcc->vflags & HE_VCC_OPEN)) {
515116491Sharti				hatm_vcc_closed(sc, tpd->cid);
516116491Sharti				vcc = NULL;
517116491Sharti			}
518116491Sharti		} else
519116491Sharti			cv_signal(&sc->vcc_cv);
520116491Sharti	}
521116491Sharti	hatm_free_tpd(sc, tpd);
522116491Sharti
523116491Sharti	if (vcc == NULL)
524116491Sharti		return;
525116491Sharti
526116491Sharti	vcc->ntpds--;
527116491Sharti
528116491Sharti	if ((vcc->vflags & HE_VCC_FLOW_CTRL) &&
529116491Sharti	    vcc->ntpds <= HE_CONFIG_TPD_FLOW_ENB) {
530116491Sharti		vcc->vflags &= ~HE_VCC_FLOW_CTRL;
531147256Sbrooks		ATMEV_SEND_FLOW_CONTROL(IFP2IFATM(sc->ifp),
532118170Sharti		    HE_VPI(tpd->cid), HE_VCI(tpd->cid), 0);
533116491Sharti	}
534116491Sharti}
535116491Sharti
536116491Sharti/*
537116491Sharti * Convert CPS to Rate for a rate group
538116491Sharti */
539116491Shartistatic u_int
540116491Sharticps_to_rate(struct hatm_softc *sc, uint32_t cps)
541116491Sharti{
542116491Sharti	u_int clk = sc->he622 ? HE_622_CLOCK : HE_155_CLOCK;
543116491Sharti	u_int period, rate;
544116491Sharti
545116491Sharti	/* how many double ticks between two cells */
546116491Sharti	period = (clk + 2 * cps - 1) / (2 * cps);
547116491Sharti	rate = hatm_cps2atmf(period);
548116491Sharti	if (hatm_atmf2cps(rate) < period)
549116491Sharti		rate++;
550116491Sharti
551116491Sharti	return (rate);
552116491Sharti}
553116491Sharti
554116491Sharti/*
555116491Sharti * Check whether the VCC is really closed on the hardware and available for
556116491Sharti * open. Check that we have enough resources. If this function returns ok,
557116491Sharti * a later actual open must succeed. Assume, that we are locked between this
558116491Sharti * function and the next one, so that nothing does change. For CBR this
559116491Sharti * assigns the rate group and set the rate group's parameter.
560116491Sharti */
561116491Shartiint
562116491Shartihatm_tx_vcc_can_open(struct hatm_softc *sc, u_int cid, struct hevcc *vcc)
563116491Sharti{
564116491Sharti	uint32_t v, line_rate;
565116491Sharti	u_int rc, idx, free_idx;
566116491Sharti	struct atmio_tparam *t = &vcc->param.tparam;
567116491Sharti
568116491Sharti	/* verify that connection is closed */
569116491Sharti#if 0
570116491Sharti	v = READ_TSR(sc, cid, 4);
571116491Sharti	if(!(v & HE_REGM_TSR4_SESS_END)) {
572147256Sbrooks		if_printf(sc->ifp, "cid=%#x not closed (TSR4)\n", cid);
573116491Sharti		return (EBUSY);
574116491Sharti	}
575116491Sharti#endif
576116491Sharti	v = READ_TSR(sc, cid, 0);
577116491Sharti	if((v & HE_REGM_TSR0_CONN_STATE) != 0) {
578147256Sbrooks		if_printf(sc->ifp, "cid=%#x not closed (TSR0=%#x)\n",
579116491Sharti		    cid, v);
580116491Sharti		return (EBUSY);
581116491Sharti	}
582116491Sharti
583116491Sharti	/* check traffic parameters */
584116491Sharti	line_rate = sc->he622 ? ATM_RATE_622M : ATM_RATE_155M;
585116491Sharti	switch (vcc->param.traffic) {
586116491Sharti
587116491Sharti	  case ATMIO_TRAFFIC_UBR:
588116491Sharti		if (t->pcr == 0 || t->pcr > line_rate)
589116491Sharti			t->pcr = line_rate;
590116491Sharti		if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 ||
591116491Sharti		    t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 ||
592116491Sharti		    t->cdf != 0)
593116491Sharti			return (EINVAL);
594116491Sharti		break;
595116491Sharti
596116491Sharti	  case ATMIO_TRAFFIC_CBR:
597116491Sharti		/*
598116491Sharti		 * Compute rate group index
599116491Sharti		 */
600116491Sharti		if (t->pcr < 10)
601116491Sharti			t->pcr = 10;
602116491Sharti		if (sc->cbr_bw + t->pcr > line_rate)
603116491Sharti			return (EINVAL);
604116491Sharti		if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 ||
605116491Sharti		    t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 ||
606116491Sharti		    t->cdf != 0)
607116491Sharti			return (EINVAL);
608116491Sharti
609116491Sharti		rc = cps_to_rate(sc, t->pcr);
610116491Sharti		free_idx = HE_REGN_CS_STPER;
611116491Sharti		for (idx = 0; idx < HE_REGN_CS_STPER; idx++) {
612116491Sharti			if (sc->rate_ctrl[idx].refcnt == 0) {
613116491Sharti				if (free_idx == HE_REGN_CS_STPER)
614116491Sharti					free_idx = idx;
615116491Sharti			} else {
616116491Sharti				if (sc->rate_ctrl[idx].rate == rc)
617116491Sharti					break;
618116491Sharti			}
619116491Sharti		}
620116491Sharti		if (idx == HE_REGN_CS_STPER) {
621116491Sharti			if ((idx = free_idx) == HE_REGN_CS_STPER)
622116491Sharti				return (EBUSY);
623116491Sharti			sc->rate_ctrl[idx].rate = rc;
624116491Sharti		}
625116491Sharti		vcc->rc = idx;
626118598Sharti
627118598Sharti		/* commit */
628118598Sharti		sc->rate_ctrl[idx].refcnt++;
629118598Sharti		sc->cbr_bw += t->pcr;
630116491Sharti		break;
631116491Sharti
632116491Sharti	  case ATMIO_TRAFFIC_ABR:
633116491Sharti		if (t->pcr > line_rate)
634116491Sharti			t->pcr = line_rate;
635116491Sharti		if (t->mcr > line_rate)
636116491Sharti			t->mcr = line_rate;
637116491Sharti		if (t->icr > line_rate)
638116491Sharti			t->icr = line_rate;
639116491Sharti		if (t->tbe == 0 || t->tbe >= 1 << 24 || t->nrm > 7 ||
640116491Sharti		    t->trm > 7 || t->adtf >= 1 << 10 || t->rif > 15 ||
641116491Sharti		    t->rdf > 15 || t->cdf > 7)
642116491Sharti			return (EINVAL);
643116491Sharti		break;
644116491Sharti
645116491Sharti	  default:
646116491Sharti		return (EINVAL);
647116491Sharti	}
648116491Sharti	return (0);
649116491Sharti}
650116491Sharti
651116491Sharti#define NRM_CODE2VAL(CODE) (2 * (1 << (CODE)))
652116491Sharti
653116491Sharti/*
654116491Sharti * Actually open the transmit VCC
655116491Sharti */
656116491Shartivoid
657116491Shartihatm_tx_vcc_open(struct hatm_softc *sc, u_int cid)
658116491Sharti{
659116491Sharti	struct hevcc *vcc = sc->vccs[cid];
660116491Sharti	uint32_t tsr0, tsr4, atmf, crm;
661116491Sharti	const struct atmio_tparam *t = &vcc->param.tparam;
662116491Sharti
663116491Sharti	if (vcc->param.aal == ATMIO_AAL_5) {
664116491Sharti		tsr0 = HE_REGM_TSR0_AAL_5 << HE_REGS_TSR0_AAL;
665116491Sharti		tsr4 = HE_REGM_TSR4_AAL_5 << HE_REGS_TSR4_AAL;
666116491Sharti	} else {
667116491Sharti		tsr0 = HE_REGM_TSR0_AAL_0 << HE_REGS_TSR0_AAL;
668116491Sharti		tsr4 = HE_REGM_TSR4_AAL_0 << HE_REGS_TSR4_AAL;
669116491Sharti	}
670116491Sharti	tsr4 |= 1;
671116491Sharti
672116491Sharti	switch (vcc->param.traffic) {
673116491Sharti
674116491Sharti	  case ATMIO_TRAFFIC_UBR:
675116491Sharti		atmf = hatm_cps2atmf(t->pcr);
676116491Sharti
677116491Sharti		tsr0 |= HE_REGM_TSR0_TRAFFIC_UBR << HE_REGS_TSR0_TRAFFIC;
678116491Sharti		tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER;
679116491Sharti
680116491Sharti		WRITE_TSR(sc, cid, 0, 0xf, tsr0);
681116491Sharti		WRITE_TSR(sc, cid, 4, 0xf, tsr4);
682116491Sharti		WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR));
683116491Sharti		WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR));
684116491Sharti		WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT);
685116491Sharti		WRITE_TSR(sc, cid, 3, 0xf, 0);
686116491Sharti		WRITE_TSR(sc, cid, 5, 0xf, 0);
687116491Sharti		WRITE_TSR(sc, cid, 6, 0xf, 0);
688116491Sharti		WRITE_TSR(sc, cid, 7, 0xf, 0);
689116491Sharti		WRITE_TSR(sc, cid, 8, 0xf, 0);
690116491Sharti		WRITE_TSR(sc, cid, 10, 0xf, 0);
691116491Sharti		WRITE_TSR(sc, cid, 11, 0xf, 0);
692116491Sharti		WRITE_TSR(sc, cid, 12, 0xf, 0);
693116491Sharti		WRITE_TSR(sc, cid, 13, 0xf, 0);
694116491Sharti		WRITE_TSR(sc, cid, 14, 0xf, 0);
695116491Sharti		break;
696116491Sharti
697116491Sharti	  case ATMIO_TRAFFIC_CBR:
698116491Sharti		atmf = hatm_cps2atmf(t->pcr);
699116491Sharti
700118598Sharti		if (sc->rate_ctrl[vcc->rc].refcnt == 1)
701118598Sharti			WRITE_MBOX4(sc, HE_REGO_CS_STPER(vcc->rc),
702118598Sharti			    sc->rate_ctrl[vcc->rc].rate);
703118598Sharti
704116491Sharti		tsr0 |= HE_REGM_TSR0_TRAFFIC_CBR << HE_REGS_TSR0_TRAFFIC;
705116491Sharti		tsr0 |= vcc->rc;
706116491Sharti
707116491Sharti		WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR));
708116491Sharti		WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR));
709116491Sharti		WRITE_TSR(sc, cid, 3, 0xf, 0);
710116491Sharti		WRITE_TSR(sc, cid, 5, 0xf, 0);
711116491Sharti		WRITE_TSR(sc, cid, 6, 0xf, 0);
712116491Sharti		WRITE_TSR(sc, cid, 7, 0xf, 0);
713116491Sharti		WRITE_TSR(sc, cid, 8, 0xf, 0);
714116491Sharti		WRITE_TSR(sc, cid, 10, 0xf, 0);
715116491Sharti		WRITE_TSR(sc, cid, 11, 0xf, 0);
716116491Sharti		WRITE_TSR(sc, cid, 12, 0xf, 0);
717116491Sharti		WRITE_TSR(sc, cid, 13, 0xf, 0);
718116491Sharti		WRITE_TSR(sc, cid, 14, 0xf, 0);
719116491Sharti		WRITE_TSR(sc, cid, 4, 0xf, tsr4);
720116491Sharti		WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT);
721116491Sharti		WRITE_TSR(sc, cid, 0, 0xf, tsr0);
722116491Sharti
723116491Sharti		break;
724116491Sharti
725116491Sharti	  case ATMIO_TRAFFIC_ABR:
726116491Sharti		if ((crm = t->tbe / NRM_CODE2VAL(t->nrm)) > 0xffff)
727116491Sharti			crm = 0xffff;
728116491Sharti
729116491Sharti		tsr0 |= HE_REGM_TSR0_TRAFFIC_ABR << HE_REGS_TSR0_TRAFFIC;
730116491Sharti		tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER;
731116491Sharti
732116491Sharti		WRITE_TSR(sc, cid, 0, 0xf, tsr0);
733116491Sharti		WRITE_TSR(sc, cid, 4, 0xf, tsr4);
734116491Sharti
735116491Sharti		WRITE_TSR(sc, cid, 1, 0xf,
736116491Sharti		    ((hatm_cps2atmf(t->pcr) << HE_REGS_TSR1_PCR) |
737116491Sharti		     (hatm_cps2atmf(t->mcr) << HE_REGS_TSR1_MCR)));
738116491Sharti		WRITE_TSR(sc, cid, 2, 0xf,
739116491Sharti		    (hatm_cps2atmf(t->icr) << HE_REGS_TSR2_ACR));
740116491Sharti		WRITE_TSR(sc, cid, 3, 0xf,
741116491Sharti		    ((NRM_CODE2VAL(t->nrm) - 1) << HE_REGS_TSR3_NRM) |
742116491Sharti		    (crm << HE_REGS_TSR3_CRM));
743116491Sharti
744116491Sharti		WRITE_TSR(sc, cid, 5, 0xf, 0);
745116491Sharti		WRITE_TSR(sc, cid, 6, 0xf, 0);
746116491Sharti		WRITE_TSR(sc, cid, 7, 0xf, 0);
747116491Sharti		WRITE_TSR(sc, cid, 8, 0xf, 0);
748116491Sharti		WRITE_TSR(sc, cid, 10, 0xf, 0);
749116491Sharti		WRITE_TSR(sc, cid, 12, 0xf, 0);
750116491Sharti		WRITE_TSR(sc, cid, 14, 0xf, 0);
751116491Sharti		WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT);
752116491Sharti
753116491Sharti		WRITE_TSR(sc, cid, 11, 0xf,
754116491Sharti		    (hatm_cps2atmf(t->icr) << HE_REGS_TSR11_ICR) |
755116491Sharti		    (t->trm << HE_REGS_TSR11_TRM) |
756116491Sharti		    (t->nrm << HE_REGS_TSR11_NRM) |
757116491Sharti		    (t->adtf << HE_REGS_TSR11_ADTF));
758116491Sharti
759116491Sharti		WRITE_TSR(sc, cid, 13, 0xf,
760116491Sharti		    (t->rdf << HE_REGS_TSR13_RDF) |
761116491Sharti		    (t->rif << HE_REGS_TSR13_RIF) |
762116491Sharti		    (t->cdf << HE_REGS_TSR13_CDF) |
763116491Sharti		    (crm << HE_REGS_TSR13_CRM));
764116491Sharti
765116491Sharti		break;
766116491Sharti
767116491Sharti	  default:
768116491Sharti		return;
769116491Sharti	}
770116491Sharti
771116491Sharti	vcc->vflags |= HE_VCC_TX_OPEN;
772116491Sharti}
773116491Sharti
774116491Sharti/*
775116491Sharti * Close the TX side of a VCC. Set the CLOSING flag.
776116491Sharti */
777116491Shartivoid
778116491Shartihatm_tx_vcc_close(struct hatm_softc *sc, u_int cid)
779116491Sharti{
780116491Sharti	struct hevcc *vcc = sc->vccs[cid];
781116491Sharti	struct tpd *tpd_list[1];
782116491Sharti	u_int i, pcr = 0;
783116491Sharti
784116491Sharti	WRITE_TSR(sc, cid, 4, 0x8, HE_REGM_TSR4_FLUSH);
785116491Sharti
786116491Sharti	switch (vcc->param.traffic) {
787116491Sharti
788116491Sharti	  case ATMIO_TRAFFIC_CBR:
789116491Sharti		WRITE_TSR(sc, cid, 14, 0x8, HE_REGM_TSR14_CBR_DELETE);
790116491Sharti		break;
791116491Sharti
792116491Sharti	  case ATMIO_TRAFFIC_ABR:
793116491Sharti		WRITE_TSR(sc, cid, 14, 0x4, HE_REGM_TSR14_ABR_CLOSE);
794116491Sharti		pcr = vcc->param.tparam.pcr;
795116491Sharti		/* FALL THROUGH */
796116491Sharti
797116491Sharti	  case ATMIO_TRAFFIC_UBR:
798116491Sharti		WRITE_TSR(sc, cid, 1, 0xf,
799116491Sharti		    hatm_cps2atmf(HE_CONFIG_FLUSH_RATE) << HE_REGS_TSR1_MCR |
800116491Sharti		    hatm_cps2atmf(pcr) << HE_REGS_TSR1_PCR);
801116491Sharti		break;
802116491Sharti	}
803116491Sharti
804116491Sharti	tpd_list[0] = hatm_alloc_tpd(sc, 0);
805116491Sharti	tpd_list[0]->tpd.addr |= HE_REGM_TPD_EOS | HE_REGM_TPD_INTR;
806116491Sharti	tpd_list[0]->cid = cid;
807116491Sharti
808116491Sharti	vcc->vflags |= HE_VCC_TX_CLOSING;
809116491Sharti	vcc->vflags &= ~HE_VCC_TX_OPEN;
810116491Sharti
811116491Sharti	i = 0;
812116491Sharti	while (hatm_queue_tpds(sc, 1, tpd_list, cid) != 0) {
813116491Sharti		if (++i == 1000)
814116491Sharti			panic("TPDRQ permanently full");
815116491Sharti		DELAY(1000);
816116491Sharti	}
817116491Sharti}
818116491Sharti
819116491Shartivoid
820116491Shartihatm_tx_vcc_closed(struct hatm_softc *sc, u_int cid)
821116491Sharti{
822116491Sharti	if (sc->vccs[cid]->param.traffic == ATMIO_TRAFFIC_CBR) {
823116491Sharti		sc->cbr_bw -= sc->vccs[cid]->param.tparam.pcr;
824116491Sharti		sc->rate_ctrl[sc->vccs[cid]->rc].refcnt--;
825116491Sharti	}
826116491Sharti}
827