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