if_hatm_tx.c revision 118598
1116491Sharti/* 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: head/sys/dev/hatm/if_hatm_tx.c 118598 2003-08-07 13:42:31Z harti $"); 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> 73116491Sharti#include <pci/pcireg.h> 74116491Sharti#include <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 81116491Sharti/* 82116491Sharti * Allocate a new TPD, zero the TPD part. Cannot return NULL if 83116491Sharti * flag is 0. The TPD is removed from the free list and its used 84116491Sharti * bit is set. 85116491Sharti */ 86116491Shartistatic struct tpd * 87116491Shartihatm_alloc_tpd(struct hatm_softc *sc, u_int flags) 88116491Sharti{ 89116491Sharti struct tpd *t; 90116491Sharti 91116491Sharti /* if we allocate a transmit TPD check for the reserve */ 92116491Sharti if (flags & M_NOWAIT) { 93116491Sharti if (sc->tpd_nfree <= HE_CONFIG_TPD_RESERVE) 94116491Sharti return (NULL); 95116491Sharti } else { 96116491Sharti if (sc->tpd_nfree == 0) 97116491Sharti return (NULL); 98116491Sharti } 99116491Sharti 100116491Sharti /* make it beeing used */ 101116491Sharti t = SLIST_FIRST(&sc->tpd_free); 102116491Sharti KASSERT(t != NULL, ("tpd botch")); 103116491Sharti SLIST_REMOVE_HEAD(&sc->tpd_free, link); 104116491Sharti TPD_SET_USED(sc, t->no); 105116491Sharti sc->tpd_nfree--; 106116491Sharti 107116491Sharti /* initialize */ 108116491Sharti t->mbuf = NULL; 109116491Sharti t->cid = 0; 110116491Sharti bzero(&t->tpd, sizeof(t->tpd)); 111116491Sharti t->tpd.addr = t->no << HE_REGS_TPD_ADDR; 112116491Sharti 113116491Sharti return (t); 114116491Sharti} 115116491Sharti 116116491Sharti/* 117116491Sharti * Free a TPD. If the mbuf pointer in that TPD is not zero, it is assumed, that 118116491Sharti * the DMA map of this TPD was used to load this mbuf. The map is unloaded 119116491Sharti * and the mbuf is freed. The TPD is put back onto the free list and 120116491Sharti * its used bit is cleared. 121116491Sharti */ 122116491Shartistatic void 123116491Shartihatm_free_tpd(struct hatm_softc *sc, struct tpd *tpd) 124116491Sharti{ 125116491Sharti if (tpd->mbuf != NULL) { 126116491Sharti bus_dmamap_unload(sc->tx_tag, tpd->map); 127116491Sharti m_freem(tpd->mbuf); 128116491Sharti tpd->mbuf = NULL; 129116491Sharti } 130116491Sharti 131116491Sharti /* insert TPD into free list */ 132116491Sharti SLIST_INSERT_HEAD(&sc->tpd_free, tpd, link); 133116491Sharti TPD_CLR_USED(sc, tpd->no); 134116491Sharti sc->tpd_nfree++; 135116491Sharti} 136116491Sharti 137116491Sharti/* 138116491Sharti * Queue a number of TPD. If there is not enough space none of the TPDs 139116491Sharti * is queued and an error code is returned. 140116491Sharti */ 141116491Shartistatic int 142116491Shartihatm_queue_tpds(struct hatm_softc *sc, u_int count, struct tpd **list, 143116491Sharti u_int cid) 144116491Sharti{ 145116491Sharti u_int space; 146116491Sharti u_int i; 147116491Sharti 148116491Sharti if (count >= sc->tpdrq.size) { 149116491Sharti sc->istats.tdprq_full++; 150116491Sharti return (EBUSY); 151116491Sharti } 152116491Sharti 153116491Sharti if (sc->tpdrq.tail < sc->tpdrq.head) 154116491Sharti space = sc->tpdrq.head - sc->tpdrq.tail; 155116491Sharti else 156116491Sharti space = sc->tpdrq.head - sc->tpdrq.tail + sc->tpdrq.size; 157116491Sharti 158116491Sharti if (space <= count) { 159116491Sharti sc->tpdrq.head = 160116491Sharti (READ4(sc, HE_REGO_TPDRQ_H) >> HE_REGS_TPDRQ_H_H) & 161116491Sharti (sc->tpdrq.size - 1); 162116491Sharti 163116491Sharti if (sc->tpdrq.tail < sc->tpdrq.head) 164116491Sharti space = sc->tpdrq.head - sc->tpdrq.tail; 165116491Sharti else 166116491Sharti space = sc->tpdrq.head - sc->tpdrq.tail + 167116491Sharti sc->tpdrq.size; 168116491Sharti 169116491Sharti if (space <= count) { 170116491Sharti if_printf(&sc->ifatm.ifnet, "TPDRQ full\n"); 171116491Sharti sc->istats.tdprq_full++; 172116491Sharti return (EBUSY); 173116491Sharti } 174116491Sharti } 175116491Sharti 176116491Sharti /* we are going to write to the TPD queue space */ 177116491Sharti bus_dmamap_sync(sc->tpdrq.mem.tag, sc->tpdrq.mem.map, 178116491Sharti BUS_DMASYNC_PREWRITE); 179116491Sharti 180116491Sharti /* put the entries into the TPD space */ 181116491Sharti for (i = 0; i < count; i++) { 182116491Sharti /* we are going to 'write' the TPD to the device */ 183116491Sharti bus_dmamap_sync(sc->tpds.tag, sc->tpds.map, 184116491Sharti BUS_DMASYNC_PREWRITE); 185116491Sharti 186116491Sharti sc->tpdrq.tpdrq[sc->tpdrq.tail].tpd = 187116491Sharti sc->tpds.paddr + HE_TPD_SIZE * list[i]->no; 188116491Sharti sc->tpdrq.tpdrq[sc->tpdrq.tail].cid = cid; 189116491Sharti 190116491Sharti if (++sc->tpdrq.tail == sc->tpdrq.size) 191116491Sharti sc->tpdrq.tail = 0; 192116491Sharti } 193116491Sharti 194116491Sharti /* update tail pointer */ 195116491Sharti WRITE4(sc, HE_REGO_TPDRQ_T, (sc->tpdrq.tail << HE_REGS_TPDRQ_T_T)); 196116491Sharti 197116491Sharti return (0); 198116491Sharti} 199116491Sharti 200116491Sharti/* 201116491Sharti * Helper struct for communication with the DMA load helper. 202116491Sharti */ 203116491Shartistruct load_txbuf_arg { 204116491Sharti struct hatm_softc *sc; 205116491Sharti struct tpd *first; 206116491Sharti struct mbuf *mbuf; 207116491Sharti struct hevcc *vcc; 208116491Sharti int error; 209116491Sharti u_int pti; 210116491Sharti u_int vpi, vci; 211116491Sharti}; 212116491Sharti 213116491Sharti/* 214116491Sharti * Loader callback for the mbuf. This function allocates the TPDs and 215116491Sharti * fills them. It puts the dmamap and and the mbuf pointer into the last 216116491Sharti * TPD and then tries to queue all the TPDs. If anything fails, all TPDs 217116491Sharti * allocated by this function are freed and the error flag is set in the 218116491Sharti * argument structure. The first TPD must then be freed by the caller. 219116491Sharti */ 220116491Shartistatic void 221116491Shartihatm_load_txbuf(void *uarg, bus_dma_segment_t *segs, int nseg, 222116491Sharti bus_size_t mapsize, int error) 223116491Sharti{ 224116491Sharti struct load_txbuf_arg *arg = uarg; 225116491Sharti u_int tpds_needed, i, n, tpd_cnt; 226116491Sharti int need_intr; 227116491Sharti struct tpd *tpd; 228116491Sharti struct tpd *tpd_list[HE_CONFIG_MAX_TPD_PER_PACKET]; 229116491Sharti 230116491Sharti if (error != 0) { 231116491Sharti DBG(arg->sc, DMA, ("%s -- error=%d plen=%d\n", 232116491Sharti __func__, error, arg->mbuf->m_pkthdr.len)); 233116491Sharti return; 234116491Sharti } 235116491Sharti 236116491Sharti /* ensure, we have enough TPDs (remember, we already have one) */ 237116491Sharti tpds_needed = (nseg + 2) / 3; 238116491Sharti if (HE_CONFIG_TPD_RESERVE + tpds_needed - 1 > arg->sc->tpd_nfree) { 239116491Sharti if_printf(&arg->sc->ifatm.ifnet, "%s -- out of TPDs (need %d, " 240116491Sharti "have %u)\n", __func__, tpds_needed - 1, 241116491Sharti arg->sc->tpd_nfree + 1); 242116491Sharti arg->error = 1; 243116491Sharti return; 244116491Sharti } 245116491Sharti 246116491Sharti /* 247116491Sharti * Check for the maximum number of TPDs on the connection. 248116491Sharti */ 249116491Sharti need_intr = 0; 250116491Sharti if (arg->sc->max_tpd > 0) { 251116491Sharti if (arg->vcc->ntpds + tpds_needed > arg->sc->max_tpd) { 252116491Sharti arg->sc->istats.flow_closed++; 253116491Sharti arg->vcc->vflags |= HE_VCC_FLOW_CTRL; 254118170Sharti ATMEV_SEND_FLOW_CONTROL(&arg->sc->ifatm, 255118170Sharti arg->vpi, arg->vci, 1); 256116491Sharti arg->error = 1; 257116491Sharti return; 258116491Sharti } 259116491Sharti if (arg->vcc->ntpds + tpds_needed > 260116491Sharti (9 * arg->sc->max_tpd) / 10) 261116491Sharti need_intr = 1; 262116491Sharti } 263116491Sharti 264116491Sharti tpd = arg->first; 265116491Sharti tpd_cnt = 0; 266116491Sharti tpd_list[tpd_cnt++] = tpd; 267116491Sharti for (i = n = 0; i < nseg; i++, n++) { 268116491Sharti if (n == 3) { 269116491Sharti if ((tpd = hatm_alloc_tpd(arg->sc, M_NOWAIT)) == NULL) 270116491Sharti /* may not fail (see check above) */ 271116491Sharti panic("%s: out of TPDs", __func__); 272116491Sharti tpd->cid = arg->first->cid; 273116491Sharti tpd->tpd.addr |= arg->pti; 274116491Sharti tpd_list[tpd_cnt++] = tpd; 275116491Sharti n = 0; 276116491Sharti } 277116491Sharti KASSERT(segs[i].ds_addr <= 0xffffffffLU, 278116491Sharti ("phys addr too large %lx", (u_long)segs[i].ds_addr)); 279116491Sharti 280116491Sharti DBG(arg->sc, DMA, ("DMA loaded: %lx/%lu", 281116491Sharti (u_long)segs[i].ds_addr, (u_long)segs[i].ds_len)); 282116491Sharti 283116491Sharti tpd->tpd.bufs[n].addr = segs[i].ds_addr; 284116491Sharti tpd->tpd.bufs[n].len = segs[i].ds_len; 285116491Sharti 286116491Sharti DBG(arg->sc, TX, ("seg[%u]=tpd[%u,%u]=%x/%u", i, 287116491Sharti tpd_cnt, n, tpd->tpd.bufs[n].addr, tpd->tpd.bufs[n].len)); 288116491Sharti 289116491Sharti if (i == nseg - 1) 290116491Sharti tpd->tpd.bufs[n].len |= HE_REGM_TPD_LST; 291116491Sharti } 292116491Sharti 293116491Sharti /* 294116491Sharti * Swap the MAP in the first and the last TPD and set the mbuf 295116491Sharti * pointer into the last TPD. We use the map in the last TPD, because 296116491Sharti * the map must stay valid until the last TPD is processed by the card. 297116491Sharti */ 298116491Sharti if (tpd_cnt > 1) { 299116491Sharti bus_dmamap_t tmp; 300116491Sharti 301116491Sharti tmp = arg->first->map; 302116491Sharti arg->first->map = tpd_list[tpd_cnt - 1]->map; 303116491Sharti tpd_list[tpd_cnt - 1]->map = tmp; 304116491Sharti } 305116491Sharti tpd_list[tpd_cnt - 1]->mbuf = arg->mbuf; 306116491Sharti 307116491Sharti if (need_intr) 308116491Sharti tpd_list[tpd_cnt - 1]->tpd.addr |= HE_REGM_TPD_INTR; 309116491Sharti 310116491Sharti /* queue the TPDs */ 311116491Sharti if (hatm_queue_tpds(arg->sc, tpd_cnt, tpd_list, arg->first->cid)) { 312116491Sharti /* free all, except the first TPD */ 313116491Sharti for (i = 1; i < tpd_cnt; i++) 314116491Sharti hatm_free_tpd(arg->sc, tpd_list[i]); 315116491Sharti arg->error = 1; 316116491Sharti return; 317116491Sharti } 318116491Sharti arg->vcc->ntpds += tpd_cnt; 319116491Sharti} 320116491Sharti 321116491Sharti 322116491Sharti/* 323116491Sharti * Start output on the interface 324116491Sharti * 325116491Sharti * For raw aal we process only the first cell in the mbuf chain! XXX 326116491Sharti */ 327116491Shartivoid 328116491Shartihatm_start(struct ifnet *ifp) 329116491Sharti{ 330116491Sharti struct hatm_softc *sc = (struct hatm_softc *)ifp->if_softc; 331116491Sharti struct mbuf *m; 332116491Sharti struct atm_pseudohdr *aph; 333116491Sharti u_int cid; 334116491Sharti struct tpd *tpd; 335116491Sharti struct load_txbuf_arg arg; 336116491Sharti u_int len; 337116491Sharti int error; 338116491Sharti 339116491Sharti if (!(ifp->if_flags & IFF_RUNNING)) 340116491Sharti return; 341116491Sharti mtx_lock(&sc->mtx); 342116491Sharti arg.sc = sc; 343116491Sharti 344116491Sharti while (1) { 345116491Sharti IF_DEQUEUE(&ifp->if_snd, m); 346116491Sharti if (m == NULL) 347116491Sharti break; 348116491Sharti 349116491Sharti if (m->m_len < sizeof(*aph)) 350116491Sharti if ((m = m_pullup(m, sizeof(*aph))) == NULL) 351116491Sharti continue; 352116491Sharti 353116491Sharti aph = mtod(m, struct atm_pseudohdr *); 354116491Sharti arg.vci = ATM_PH_VCI(aph); 355116491Sharti arg.vpi = ATM_PH_VPI(aph); 356116491Sharti m_adj(m, sizeof(*aph)); 357116491Sharti 358116491Sharti if ((len = m->m_pkthdr.len) == 0) { 359116491Sharti m_freem(m); 360116491Sharti continue; 361116491Sharti } 362116491Sharti 363116491Sharti if ((arg.vpi & ~HE_VPI_MASK) || (arg.vci & ~HE_VCI_MASK) || 364116491Sharti (arg.vci == 0)) { 365116491Sharti m_freem(m); 366116491Sharti continue; 367116491Sharti } 368116491Sharti cid = HE_CID(arg.vpi, arg.vci); 369116491Sharti arg.vcc = sc->vccs[cid]; 370116491Sharti 371116491Sharti if (arg.vcc == NULL || !(arg.vcc->vflags & HE_VCC_OPEN)) { 372116491Sharti m_freem(m); 373116491Sharti continue; 374116491Sharti } 375116491Sharti if (arg.vcc->vflags & HE_VCC_FLOW_CTRL) { 376116491Sharti m_freem(m); 377116491Sharti sc->istats.flow_drop++; 378116491Sharti continue; 379116491Sharti } 380116491Sharti 381116491Sharti arg.pti = 0; 382116491Sharti if (arg.vcc->param.aal == ATMIO_AAL_RAW) { 383116491Sharti if (len < 52) { 384116491Sharti m_freem(m); 385116491Sharti continue; 386116491Sharti } 387116491Sharti if (len > 52) { 388116491Sharti m_adj(m, -((int)(len - 52))); 389116491Sharti len = 52; 390116491Sharti } 391116491Sharti if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) 392116491Sharti continue; 393116491Sharti 394116491Sharti /* ignore header except payload type and CLP */ 395116491Sharti arg.pti = mtod(m, u_char *)[3] & 0xf; 396116491Sharti arg.pti = ((arg.pti & 0xe) << 2) | ((arg.pti & 1) << 1); 397116491Sharti m_adj(m, 4); 398116491Sharti len -= 4; 399116491Sharti } 400116491Sharti 401116491Sharti#ifdef ENABLE_BPF 402116491Sharti if (!(arg.vcc->param.flags & ATMIO_FLAG_NG) && 403118540Sharti (arg.vcc->param.aal == ATMIO_AAL_5) && 404116491Sharti (arg.vcc->param.flags & ATM_PH_LLCSNAP)) 405116491Sharti BPF_MTAP(ifp, m); 406116491Sharti#endif 407116491Sharti 408116491Sharti /* Now load a DMA map with the packet. Allocate the first 409116491Sharti * TPD to get a map. Additional TPDs may be allocated by the 410116491Sharti * callback. */ 411116491Sharti if ((tpd = hatm_alloc_tpd(sc, M_NOWAIT)) == NULL) { 412116491Sharti m_freem(m); 413116491Sharti sc->ifatm.ifnet.if_oerrors++; 414116491Sharti continue; 415116491Sharti } 416116491Sharti tpd->cid = cid; 417116491Sharti tpd->tpd.addr |= arg.pti; 418116491Sharti arg.first = tpd; 419116491Sharti arg.error = 0; 420116491Sharti arg.mbuf = m; 421116491Sharti 422116491Sharti error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m, 423117382Sharti hatm_load_txbuf, &arg, BUS_DMA_NOWAIT); 424116491Sharti 425116491Sharti if (error == EFBIG) { 426116491Sharti /* try to defragment the packet */ 427116491Sharti sc->istats.defrag++; 428116491Sharti m = m_defrag(m, M_DONTWAIT); 429116491Sharti if (m == NULL) { 430116491Sharti sc->ifatm.ifnet.if_oerrors++; 431116491Sharti continue; 432116491Sharti } 433116491Sharti arg.mbuf = m; 434116491Sharti error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m, 435117382Sharti hatm_load_txbuf, &arg, BUS_DMA_NOWAIT); 436116491Sharti } 437116491Sharti 438116491Sharti if (error != 0) { 439116491Sharti if_printf(&sc->ifatm.ifnet, "mbuf loaded error=%d\n", 440116491Sharti error); 441116491Sharti hatm_free_tpd(sc, tpd); 442116491Sharti sc->ifatm.ifnet.if_oerrors++; 443116491Sharti continue; 444116491Sharti } 445116491Sharti if (arg.error) { 446116491Sharti hatm_free_tpd(sc, tpd); 447116491Sharti sc->ifatm.ifnet.if_oerrors++; 448116491Sharti continue; 449116491Sharti } 450116491Sharti arg.vcc->opackets++; 451116491Sharti arg.vcc->obytes += len; 452116491Sharti sc->ifatm.ifnet.if_opackets++; 453116491Sharti } 454116491Sharti mtx_unlock(&sc->mtx); 455116491Sharti} 456116491Sharti 457116491Shartivoid 458116491Shartihatm_tx_complete(struct hatm_softc *sc, struct tpd *tpd, uint32_t flags) 459116491Sharti{ 460116491Sharti struct hevcc *vcc = sc->vccs[tpd->cid]; 461116491Sharti 462116491Sharti DBG(sc, TX, ("tx_complete cid=%#x flags=%#x", tpd->cid, flags)); 463116491Sharti 464116491Sharti if (vcc == NULL) 465116491Sharti return; 466116491Sharti if ((flags & HE_REGM_TBRQ_EOS) && (vcc->vflags & HE_VCC_TX_CLOSING)) { 467116491Sharti vcc->vflags &= ~HE_VCC_TX_CLOSING; 468118540Sharti if (vcc->param.flags & ATMIO_FLAG_ASYNC) { 469116491Sharti hatm_tx_vcc_closed(sc, tpd->cid); 470116491Sharti if (!(vcc->vflags & HE_VCC_OPEN)) { 471116491Sharti hatm_vcc_closed(sc, tpd->cid); 472116491Sharti vcc = NULL; 473116491Sharti } 474116491Sharti } else 475116491Sharti cv_signal(&sc->vcc_cv); 476116491Sharti } 477116491Sharti hatm_free_tpd(sc, tpd); 478116491Sharti 479116491Sharti if (vcc == NULL) 480116491Sharti return; 481116491Sharti 482116491Sharti vcc->ntpds--; 483116491Sharti 484116491Sharti if ((vcc->vflags & HE_VCC_FLOW_CTRL) && 485116491Sharti vcc->ntpds <= HE_CONFIG_TPD_FLOW_ENB) { 486116491Sharti vcc->vflags &= ~HE_VCC_FLOW_CTRL; 487118170Sharti ATMEV_SEND_FLOW_CONTROL(&sc->ifatm, 488118170Sharti HE_VPI(tpd->cid), HE_VCI(tpd->cid), 0); 489116491Sharti } 490116491Sharti} 491116491Sharti 492116491Sharti/* 493116491Sharti * Convert CPS to Rate for a rate group 494116491Sharti */ 495116491Shartistatic u_int 496116491Sharticps_to_rate(struct hatm_softc *sc, uint32_t cps) 497116491Sharti{ 498116491Sharti u_int clk = sc->he622 ? HE_622_CLOCK : HE_155_CLOCK; 499116491Sharti u_int period, rate; 500116491Sharti 501116491Sharti /* how many double ticks between two cells */ 502116491Sharti period = (clk + 2 * cps - 1) / (2 * cps); 503116491Sharti rate = hatm_cps2atmf(period); 504116491Sharti if (hatm_atmf2cps(rate) < period) 505116491Sharti rate++; 506116491Sharti 507116491Sharti return (rate); 508116491Sharti} 509116491Sharti 510116491Sharti/* 511116491Sharti * Check whether the VCC is really closed on the hardware and available for 512116491Sharti * open. Check that we have enough resources. If this function returns ok, 513116491Sharti * a later actual open must succeed. Assume, that we are locked between this 514116491Sharti * function and the next one, so that nothing does change. For CBR this 515116491Sharti * assigns the rate group and set the rate group's parameter. 516116491Sharti */ 517116491Shartiint 518116491Shartihatm_tx_vcc_can_open(struct hatm_softc *sc, u_int cid, struct hevcc *vcc) 519116491Sharti{ 520116491Sharti uint32_t v, line_rate; 521116491Sharti u_int rc, idx, free_idx; 522116491Sharti struct atmio_tparam *t = &vcc->param.tparam; 523116491Sharti 524116491Sharti /* verify that connection is closed */ 525116491Sharti#if 0 526116491Sharti v = READ_TSR(sc, cid, 4); 527116491Sharti if(!(v & HE_REGM_TSR4_SESS_END)) { 528116491Sharti if_printf(&sc->ifatm.ifnet, "cid=%#x not closed (TSR4)\n", cid); 529116491Sharti return (EBUSY); 530116491Sharti } 531116491Sharti#endif 532116491Sharti v = READ_TSR(sc, cid, 0); 533116491Sharti if((v & HE_REGM_TSR0_CONN_STATE) != 0) { 534116491Sharti if_printf(&sc->ifatm.ifnet, "cid=%#x not closed (TSR0=%#x)\n", 535116491Sharti cid, v); 536116491Sharti return (EBUSY); 537116491Sharti } 538116491Sharti 539116491Sharti /* check traffic parameters */ 540116491Sharti line_rate = sc->he622 ? ATM_RATE_622M : ATM_RATE_155M; 541116491Sharti switch (vcc->param.traffic) { 542116491Sharti 543116491Sharti case ATMIO_TRAFFIC_UBR: 544116491Sharti if (t->pcr == 0 || t->pcr > line_rate) 545116491Sharti t->pcr = line_rate; 546116491Sharti if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 || 547116491Sharti t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 || 548116491Sharti t->cdf != 0) 549116491Sharti return (EINVAL); 550116491Sharti break; 551116491Sharti 552116491Sharti case ATMIO_TRAFFIC_CBR: 553116491Sharti /* 554116491Sharti * Compute rate group index 555116491Sharti */ 556116491Sharti if (t->pcr < 10) 557116491Sharti t->pcr = 10; 558116491Sharti if (sc->cbr_bw + t->pcr > line_rate) 559116491Sharti return (EINVAL); 560116491Sharti if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 || 561116491Sharti t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 || 562116491Sharti t->cdf != 0) 563116491Sharti return (EINVAL); 564116491Sharti 565116491Sharti rc = cps_to_rate(sc, t->pcr); 566116491Sharti free_idx = HE_REGN_CS_STPER; 567116491Sharti for (idx = 0; idx < HE_REGN_CS_STPER; idx++) { 568116491Sharti if (sc->rate_ctrl[idx].refcnt == 0) { 569116491Sharti if (free_idx == HE_REGN_CS_STPER) 570116491Sharti free_idx = idx; 571116491Sharti } else { 572116491Sharti if (sc->rate_ctrl[idx].rate == rc) 573116491Sharti break; 574116491Sharti } 575116491Sharti } 576116491Sharti if (idx == HE_REGN_CS_STPER) { 577116491Sharti if ((idx = free_idx) == HE_REGN_CS_STPER) 578116491Sharti return (EBUSY); 579116491Sharti sc->rate_ctrl[idx].rate = rc; 580116491Sharti } 581116491Sharti vcc->rc = idx; 582118598Sharti 583118598Sharti /* commit */ 584118598Sharti sc->rate_ctrl[idx].refcnt++; 585118598Sharti sc->cbr_bw += t->pcr; 586116491Sharti break; 587116491Sharti 588116491Sharti case ATMIO_TRAFFIC_ABR: 589116491Sharti if (t->pcr > line_rate) 590116491Sharti t->pcr = line_rate; 591116491Sharti if (t->mcr > line_rate) 592116491Sharti t->mcr = line_rate; 593116491Sharti if (t->icr > line_rate) 594116491Sharti t->icr = line_rate; 595116491Sharti if (t->tbe == 0 || t->tbe >= 1 << 24 || t->nrm > 7 || 596116491Sharti t->trm > 7 || t->adtf >= 1 << 10 || t->rif > 15 || 597116491Sharti t->rdf > 15 || t->cdf > 7) 598116491Sharti return (EINVAL); 599116491Sharti break; 600116491Sharti 601116491Sharti default: 602116491Sharti return (EINVAL); 603116491Sharti } 604116491Sharti return (0); 605116491Sharti} 606116491Sharti 607116491Sharti#define NRM_CODE2VAL(CODE) (2 * (1 << (CODE))) 608116491Sharti 609116491Sharti/* 610116491Sharti * Actually open the transmit VCC 611116491Sharti */ 612116491Shartivoid 613116491Shartihatm_tx_vcc_open(struct hatm_softc *sc, u_int cid) 614116491Sharti{ 615116491Sharti struct hevcc *vcc = sc->vccs[cid]; 616116491Sharti uint32_t tsr0, tsr4, atmf, crm; 617116491Sharti const struct atmio_tparam *t = &vcc->param.tparam; 618116491Sharti 619116491Sharti if (vcc->param.aal == ATMIO_AAL_5) { 620116491Sharti tsr0 = HE_REGM_TSR0_AAL_5 << HE_REGS_TSR0_AAL; 621116491Sharti tsr4 = HE_REGM_TSR4_AAL_5 << HE_REGS_TSR4_AAL; 622116491Sharti } else { 623116491Sharti tsr0 = HE_REGM_TSR0_AAL_0 << HE_REGS_TSR0_AAL; 624116491Sharti tsr4 = HE_REGM_TSR4_AAL_0 << HE_REGS_TSR4_AAL; 625116491Sharti } 626116491Sharti tsr4 |= 1; 627116491Sharti 628116491Sharti switch (vcc->param.traffic) { 629116491Sharti 630116491Sharti case ATMIO_TRAFFIC_UBR: 631116491Sharti atmf = hatm_cps2atmf(t->pcr); 632116491Sharti 633116491Sharti tsr0 |= HE_REGM_TSR0_TRAFFIC_UBR << HE_REGS_TSR0_TRAFFIC; 634116491Sharti tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER; 635116491Sharti 636116491Sharti WRITE_TSR(sc, cid, 0, 0xf, tsr0); 637116491Sharti WRITE_TSR(sc, cid, 4, 0xf, tsr4); 638116491Sharti WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR)); 639116491Sharti WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR)); 640116491Sharti WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT); 641116491Sharti WRITE_TSR(sc, cid, 3, 0xf, 0); 642116491Sharti WRITE_TSR(sc, cid, 5, 0xf, 0); 643116491Sharti WRITE_TSR(sc, cid, 6, 0xf, 0); 644116491Sharti WRITE_TSR(sc, cid, 7, 0xf, 0); 645116491Sharti WRITE_TSR(sc, cid, 8, 0xf, 0); 646116491Sharti WRITE_TSR(sc, cid, 10, 0xf, 0); 647116491Sharti WRITE_TSR(sc, cid, 11, 0xf, 0); 648116491Sharti WRITE_TSR(sc, cid, 12, 0xf, 0); 649116491Sharti WRITE_TSR(sc, cid, 13, 0xf, 0); 650116491Sharti WRITE_TSR(sc, cid, 14, 0xf, 0); 651116491Sharti break; 652116491Sharti 653116491Sharti case ATMIO_TRAFFIC_CBR: 654116491Sharti atmf = hatm_cps2atmf(t->pcr); 655116491Sharti 656118598Sharti if (sc->rate_ctrl[vcc->rc].refcnt == 1) 657118598Sharti WRITE_MBOX4(sc, HE_REGO_CS_STPER(vcc->rc), 658118598Sharti sc->rate_ctrl[vcc->rc].rate); 659118598Sharti 660116491Sharti tsr0 |= HE_REGM_TSR0_TRAFFIC_CBR << HE_REGS_TSR0_TRAFFIC; 661116491Sharti tsr0 |= vcc->rc; 662116491Sharti 663116491Sharti WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR)); 664116491Sharti WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR)); 665116491Sharti WRITE_TSR(sc, cid, 3, 0xf, 0); 666116491Sharti WRITE_TSR(sc, cid, 5, 0xf, 0); 667116491Sharti WRITE_TSR(sc, cid, 6, 0xf, 0); 668116491Sharti WRITE_TSR(sc, cid, 7, 0xf, 0); 669116491Sharti WRITE_TSR(sc, cid, 8, 0xf, 0); 670116491Sharti WRITE_TSR(sc, cid, 10, 0xf, 0); 671116491Sharti WRITE_TSR(sc, cid, 11, 0xf, 0); 672116491Sharti WRITE_TSR(sc, cid, 12, 0xf, 0); 673116491Sharti WRITE_TSR(sc, cid, 13, 0xf, 0); 674116491Sharti WRITE_TSR(sc, cid, 14, 0xf, 0); 675116491Sharti WRITE_TSR(sc, cid, 4, 0xf, tsr4); 676116491Sharti WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT); 677116491Sharti WRITE_TSR(sc, cid, 0, 0xf, tsr0); 678116491Sharti 679116491Sharti break; 680116491Sharti 681116491Sharti case ATMIO_TRAFFIC_ABR: 682116491Sharti if ((crm = t->tbe / NRM_CODE2VAL(t->nrm)) > 0xffff) 683116491Sharti crm = 0xffff; 684116491Sharti 685116491Sharti tsr0 |= HE_REGM_TSR0_TRAFFIC_ABR << HE_REGS_TSR0_TRAFFIC; 686116491Sharti tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER; 687116491Sharti 688116491Sharti WRITE_TSR(sc, cid, 0, 0xf, tsr0); 689116491Sharti WRITE_TSR(sc, cid, 4, 0xf, tsr4); 690116491Sharti 691116491Sharti WRITE_TSR(sc, cid, 1, 0xf, 692116491Sharti ((hatm_cps2atmf(t->pcr) << HE_REGS_TSR1_PCR) | 693116491Sharti (hatm_cps2atmf(t->mcr) << HE_REGS_TSR1_MCR))); 694116491Sharti WRITE_TSR(sc, cid, 2, 0xf, 695116491Sharti (hatm_cps2atmf(t->icr) << HE_REGS_TSR2_ACR)); 696116491Sharti WRITE_TSR(sc, cid, 3, 0xf, 697116491Sharti ((NRM_CODE2VAL(t->nrm) - 1) << HE_REGS_TSR3_NRM) | 698116491Sharti (crm << HE_REGS_TSR3_CRM)); 699116491Sharti 700116491Sharti WRITE_TSR(sc, cid, 5, 0xf, 0); 701116491Sharti WRITE_TSR(sc, cid, 6, 0xf, 0); 702116491Sharti WRITE_TSR(sc, cid, 7, 0xf, 0); 703116491Sharti WRITE_TSR(sc, cid, 8, 0xf, 0); 704116491Sharti WRITE_TSR(sc, cid, 10, 0xf, 0); 705116491Sharti WRITE_TSR(sc, cid, 12, 0xf, 0); 706116491Sharti WRITE_TSR(sc, cid, 14, 0xf, 0); 707116491Sharti WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT); 708116491Sharti 709116491Sharti WRITE_TSR(sc, cid, 11, 0xf, 710116491Sharti (hatm_cps2atmf(t->icr) << HE_REGS_TSR11_ICR) | 711116491Sharti (t->trm << HE_REGS_TSR11_TRM) | 712116491Sharti (t->nrm << HE_REGS_TSR11_NRM) | 713116491Sharti (t->adtf << HE_REGS_TSR11_ADTF)); 714116491Sharti 715116491Sharti WRITE_TSR(sc, cid, 13, 0xf, 716116491Sharti (t->rdf << HE_REGS_TSR13_RDF) | 717116491Sharti (t->rif << HE_REGS_TSR13_RIF) | 718116491Sharti (t->cdf << HE_REGS_TSR13_CDF) | 719116491Sharti (crm << HE_REGS_TSR13_CRM)); 720116491Sharti 721116491Sharti break; 722116491Sharti 723116491Sharti default: 724116491Sharti return; 725116491Sharti } 726116491Sharti 727116491Sharti vcc->vflags |= HE_VCC_TX_OPEN; 728116491Sharti} 729116491Sharti 730116491Sharti/* 731116491Sharti * Close the TX side of a VCC. Set the CLOSING flag. 732116491Sharti */ 733116491Shartivoid 734116491Shartihatm_tx_vcc_close(struct hatm_softc *sc, u_int cid) 735116491Sharti{ 736116491Sharti struct hevcc *vcc = sc->vccs[cid]; 737116491Sharti struct tpd *tpd_list[1]; 738116491Sharti u_int i, pcr = 0; 739116491Sharti 740116491Sharti WRITE_TSR(sc, cid, 4, 0x8, HE_REGM_TSR4_FLUSH); 741116491Sharti 742116491Sharti switch (vcc->param.traffic) { 743116491Sharti 744116491Sharti case ATMIO_TRAFFIC_CBR: 745116491Sharti WRITE_TSR(sc, cid, 14, 0x8, HE_REGM_TSR14_CBR_DELETE); 746116491Sharti break; 747116491Sharti 748116491Sharti case ATMIO_TRAFFIC_ABR: 749116491Sharti WRITE_TSR(sc, cid, 14, 0x4, HE_REGM_TSR14_ABR_CLOSE); 750116491Sharti pcr = vcc->param.tparam.pcr; 751116491Sharti /* FALL THROUGH */ 752116491Sharti 753116491Sharti case ATMIO_TRAFFIC_UBR: 754116491Sharti WRITE_TSR(sc, cid, 1, 0xf, 755116491Sharti hatm_cps2atmf(HE_CONFIG_FLUSH_RATE) << HE_REGS_TSR1_MCR | 756116491Sharti hatm_cps2atmf(pcr) << HE_REGS_TSR1_PCR); 757116491Sharti break; 758116491Sharti } 759116491Sharti 760116491Sharti tpd_list[0] = hatm_alloc_tpd(sc, 0); 761116491Sharti tpd_list[0]->tpd.addr |= HE_REGM_TPD_EOS | HE_REGM_TPD_INTR; 762116491Sharti tpd_list[0]->cid = cid; 763116491Sharti 764116491Sharti vcc->vflags |= HE_VCC_TX_CLOSING; 765116491Sharti vcc->vflags &= ~HE_VCC_TX_OPEN; 766116491Sharti 767116491Sharti i = 0; 768116491Sharti while (hatm_queue_tpds(sc, 1, tpd_list, cid) != 0) { 769116491Sharti if (++i == 1000) 770116491Sharti panic("TPDRQ permanently full"); 771116491Sharti DELAY(1000); 772116491Sharti } 773116491Sharti} 774116491Sharti 775116491Shartivoid 776116491Shartihatm_tx_vcc_closed(struct hatm_softc *sc, u_int cid) 777116491Sharti{ 778116491Sharti if (sc->vccs[cid]->param.traffic == ATMIO_TRAFFIC_CBR) { 779116491Sharti sc->cbr_bw -= sc->vccs[cid]->param.tparam.pcr; 780116491Sharti sc->rate_ctrl[sc->vccs[cid]->rc].refcnt--; 781116491Sharti } 782116491Sharti} 783