if_hatm_tx.c revision 116519
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 116519 2003-06-18 09:31:37Z 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; 254116491Sharti#ifdef notyet 255116491Sharti atm_message(&arg->sc->ifatm.ifnet, ATM_MSG_FLOW_CONTROL, 256116491Sharti (1 << 24) | (arg->vpi << 16) | arg->vci); 257116491Sharti#endif 258116491Sharti arg->error = 1; 259116491Sharti return; 260116491Sharti } 261116491Sharti if (arg->vcc->ntpds + tpds_needed > 262116491Sharti (9 * arg->sc->max_tpd) / 10) 263116491Sharti need_intr = 1; 264116491Sharti } 265116491Sharti 266116491Sharti tpd = arg->first; 267116491Sharti tpd_cnt = 0; 268116491Sharti tpd_list[tpd_cnt++] = tpd; 269116491Sharti for (i = n = 0; i < nseg; i++, n++) { 270116491Sharti if (n == 3) { 271116491Sharti if ((tpd = hatm_alloc_tpd(arg->sc, M_NOWAIT)) == NULL) 272116491Sharti /* may not fail (see check above) */ 273116491Sharti panic("%s: out of TPDs", __func__); 274116491Sharti tpd->cid = arg->first->cid; 275116491Sharti tpd->tpd.addr |= arg->pti; 276116491Sharti tpd_list[tpd_cnt++] = tpd; 277116491Sharti n = 0; 278116491Sharti } 279116491Sharti KASSERT(segs[i].ds_addr <= 0xffffffffLU, 280116491Sharti ("phys addr too large %lx", (u_long)segs[i].ds_addr)); 281116491Sharti 282116491Sharti DBG(arg->sc, DMA, ("DMA loaded: %lx/%lu", 283116491Sharti (u_long)segs[i].ds_addr, (u_long)segs[i].ds_len)); 284116491Sharti 285116491Sharti tpd->tpd.bufs[n].addr = segs[i].ds_addr; 286116491Sharti tpd->tpd.bufs[n].len = segs[i].ds_len; 287116491Sharti 288116491Sharti DBG(arg->sc, TX, ("seg[%u]=tpd[%u,%u]=%x/%u", i, 289116491Sharti tpd_cnt, n, tpd->tpd.bufs[n].addr, tpd->tpd.bufs[n].len)); 290116491Sharti 291116491Sharti if (i == nseg - 1) 292116491Sharti tpd->tpd.bufs[n].len |= HE_REGM_TPD_LST; 293116491Sharti } 294116491Sharti 295116491Sharti /* 296116491Sharti * Swap the MAP in the first and the last TPD and set the mbuf 297116491Sharti * pointer into the last TPD. We use the map in the last TPD, because 298116491Sharti * the map must stay valid until the last TPD is processed by the card. 299116491Sharti */ 300116491Sharti if (tpd_cnt > 1) { 301116491Sharti bus_dmamap_t tmp; 302116491Sharti 303116491Sharti tmp = arg->first->map; 304116491Sharti arg->first->map = tpd_list[tpd_cnt - 1]->map; 305116491Sharti tpd_list[tpd_cnt - 1]->map = tmp; 306116491Sharti } 307116491Sharti tpd_list[tpd_cnt - 1]->mbuf = arg->mbuf; 308116491Sharti 309116491Sharti if (need_intr) 310116491Sharti tpd_list[tpd_cnt - 1]->tpd.addr |= HE_REGM_TPD_INTR; 311116491Sharti 312116491Sharti /* queue the TPDs */ 313116491Sharti if (hatm_queue_tpds(arg->sc, tpd_cnt, tpd_list, arg->first->cid)) { 314116491Sharti /* free all, except the first TPD */ 315116491Sharti for (i = 1; i < tpd_cnt; i++) 316116491Sharti hatm_free_tpd(arg->sc, tpd_list[i]); 317116491Sharti arg->error = 1; 318116491Sharti return; 319116491Sharti } 320116491Sharti arg->vcc->ntpds += tpd_cnt; 321116491Sharti} 322116491Sharti 323116491Sharti 324116491Sharti/* 325116491Sharti * Start output on the interface 326116491Sharti * 327116491Sharti * For raw aal we process only the first cell in the mbuf chain! XXX 328116491Sharti */ 329116491Shartivoid 330116491Shartihatm_start(struct ifnet *ifp) 331116491Sharti{ 332116491Sharti struct hatm_softc *sc = (struct hatm_softc *)ifp->if_softc; 333116491Sharti struct mbuf *m; 334116491Sharti struct atm_pseudohdr *aph; 335116491Sharti u_int cid; 336116491Sharti struct tpd *tpd; 337116491Sharti struct load_txbuf_arg arg; 338116491Sharti u_int len; 339116491Sharti int error; 340116491Sharti 341116491Sharti if (!(ifp->if_flags & IFF_RUNNING)) 342116491Sharti return; 343116491Sharti mtx_lock(&sc->mtx); 344116491Sharti arg.sc = sc; 345116491Sharti 346116491Sharti while (1) { 347116491Sharti IF_DEQUEUE(&ifp->if_snd, m); 348116491Sharti if (m == NULL) 349116491Sharti break; 350116491Sharti 351116491Sharti if (m->m_len < sizeof(*aph)) 352116491Sharti if ((m = m_pullup(m, sizeof(*aph))) == NULL) 353116491Sharti continue; 354116491Sharti 355116491Sharti aph = mtod(m, struct atm_pseudohdr *); 356116491Sharti arg.vci = ATM_PH_VCI(aph); 357116491Sharti arg.vpi = ATM_PH_VPI(aph); 358116491Sharti m_adj(m, sizeof(*aph)); 359116491Sharti 360116491Sharti if ((len = m->m_pkthdr.len) == 0) { 361116491Sharti m_freem(m); 362116491Sharti continue; 363116491Sharti } 364116491Sharti 365116491Sharti if ((arg.vpi & ~HE_VPI_MASK) || (arg.vci & ~HE_VCI_MASK) || 366116491Sharti (arg.vci == 0)) { 367116491Sharti m_freem(m); 368116491Sharti continue; 369116491Sharti } 370116491Sharti cid = HE_CID(arg.vpi, arg.vci); 371116491Sharti arg.vcc = sc->vccs[cid]; 372116491Sharti 373116491Sharti if (arg.vcc == NULL || !(arg.vcc->vflags & HE_VCC_OPEN)) { 374116491Sharti m_freem(m); 375116491Sharti continue; 376116491Sharti } 377116491Sharti if (arg.vcc->vflags & HE_VCC_FLOW_CTRL) { 378116491Sharti m_freem(m); 379116491Sharti sc->istats.flow_drop++; 380116491Sharti continue; 381116491Sharti } 382116491Sharti 383116491Sharti arg.pti = 0; 384116491Sharti if (arg.vcc->param.aal == ATMIO_AAL_RAW) { 385116491Sharti if (len < 52) { 386116491Sharti m_freem(m); 387116491Sharti continue; 388116491Sharti } 389116491Sharti if (len > 52) { 390116491Sharti m_adj(m, -((int)(len - 52))); 391116491Sharti len = 52; 392116491Sharti } 393116491Sharti if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) 394116491Sharti continue; 395116491Sharti 396116491Sharti /* ignore header except payload type and CLP */ 397116491Sharti arg.pti = mtod(m, u_char *)[3] & 0xf; 398116491Sharti arg.pti = ((arg.pti & 0xe) << 2) | ((arg.pti & 1) << 1); 399116491Sharti m_adj(m, 4); 400116491Sharti len -= 4; 401116491Sharti } 402116491Sharti 403116491Sharti#ifdef ENABLE_BPF 404116491Sharti if (!(arg.vcc->param.flags & ATMIO_FLAG_NG) && 405116491Sharti (arg.vcc->param.flags & ATM_PH_AAL5) && 406116491Sharti (arg.vcc->param.flags & ATM_PH_LLCSNAP)) 407116491Sharti BPF_MTAP(ifp, m); 408116491Sharti#endif 409116491Sharti 410116491Sharti /* Now load a DMA map with the packet. Allocate the first 411116491Sharti * TPD to get a map. Additional TPDs may be allocated by the 412116491Sharti * callback. */ 413116491Sharti if ((tpd = hatm_alloc_tpd(sc, M_NOWAIT)) == NULL) { 414116491Sharti m_freem(m); 415116491Sharti sc->ifatm.ifnet.if_oerrors++; 416116491Sharti continue; 417116491Sharti } 418116491Sharti tpd->cid = cid; 419116491Sharti tpd->tpd.addr |= arg.pti; 420116491Sharti arg.first = tpd; 421116491Sharti arg.error = 0; 422116491Sharti arg.mbuf = m; 423116491Sharti 424116491Sharti error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m, 425116491Sharti hatm_load_txbuf, &arg, 0); 426116491Sharti 427116491Sharti if (error == EFBIG) { 428116491Sharti /* try to defragment the packet */ 429116491Sharti sc->istats.defrag++; 430116491Sharti m = m_defrag(m, M_DONTWAIT); 431116491Sharti if (m == NULL) { 432116491Sharti sc->ifatm.ifnet.if_oerrors++; 433116491Sharti continue; 434116491Sharti } 435116491Sharti arg.mbuf = m; 436116491Sharti error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m, 437116491Sharti hatm_load_txbuf, &arg, 0); 438116491Sharti } 439116491Sharti 440116491Sharti if (error != 0) { 441116491Sharti if_printf(&sc->ifatm.ifnet, "mbuf loaded error=%d\n", 442116491Sharti error); 443116491Sharti hatm_free_tpd(sc, tpd); 444116491Sharti sc->ifatm.ifnet.if_oerrors++; 445116491Sharti continue; 446116491Sharti } 447116491Sharti if (arg.error) { 448116491Sharti hatm_free_tpd(sc, tpd); 449116491Sharti sc->ifatm.ifnet.if_oerrors++; 450116491Sharti continue; 451116491Sharti } 452116491Sharti arg.vcc->opackets++; 453116491Sharti arg.vcc->obytes += len; 454116491Sharti sc->ifatm.ifnet.if_opackets++; 455116491Sharti } 456116491Sharti mtx_unlock(&sc->mtx); 457116491Sharti} 458116491Sharti 459116491Shartivoid 460116491Shartihatm_tx_complete(struct hatm_softc *sc, struct tpd *tpd, uint32_t flags) 461116491Sharti{ 462116491Sharti struct hevcc *vcc = sc->vccs[tpd->cid]; 463116491Sharti 464116491Sharti DBG(sc, TX, ("tx_complete cid=%#x flags=%#x", tpd->cid, flags)); 465116491Sharti 466116491Sharti if (vcc == NULL) 467116491Sharti return; 468116491Sharti if ((flags & HE_REGM_TBRQ_EOS) && (vcc->vflags & HE_VCC_TX_CLOSING)) { 469116491Sharti vcc->vflags &= ~HE_VCC_TX_CLOSING; 470116491Sharti if (vcc->vflags & HE_VCC_ASYNC) { 471116491Sharti hatm_tx_vcc_closed(sc, tpd->cid); 472116491Sharti if (!(vcc->vflags & HE_VCC_OPEN)) { 473116491Sharti hatm_vcc_closed(sc, tpd->cid); 474116491Sharti vcc = NULL; 475116491Sharti } 476116491Sharti } else 477116491Sharti cv_signal(&sc->vcc_cv); 478116491Sharti } 479116491Sharti hatm_free_tpd(sc, tpd); 480116491Sharti 481116491Sharti if (vcc == NULL) 482116491Sharti return; 483116491Sharti 484116491Sharti vcc->ntpds--; 485116491Sharti 486116491Sharti if ((vcc->vflags & HE_VCC_FLOW_CTRL) && 487116491Sharti vcc->ntpds <= HE_CONFIG_TPD_FLOW_ENB) { 488116491Sharti vcc->vflags &= ~HE_VCC_FLOW_CTRL; 489116491Sharti#ifdef notyet 490116491Sharti atm_message(&sc->ifatm.ifnet, ATM_MSG_FLOW_CONTROL, 491116491Sharti (0 << 24) | (HE_VPI(tpd->cid) << 16) | HE_VCI(tpd->cid)); 492116491Sharti#endif 493116491Sharti } 494116491Sharti} 495116491Sharti 496116491Sharti/* 497116491Sharti * Convert CPS to Rate for a rate group 498116491Sharti */ 499116491Shartistatic u_int 500116491Sharticps_to_rate(struct hatm_softc *sc, uint32_t cps) 501116491Sharti{ 502116491Sharti u_int clk = sc->he622 ? HE_622_CLOCK : HE_155_CLOCK; 503116491Sharti u_int period, rate; 504116491Sharti 505116491Sharti /* how many double ticks between two cells */ 506116491Sharti period = (clk + 2 * cps - 1) / (2 * cps); 507116491Sharti rate = hatm_cps2atmf(period); 508116491Sharti if (hatm_atmf2cps(rate) < period) 509116491Sharti rate++; 510116491Sharti 511116491Sharti return (rate); 512116491Sharti} 513116491Sharti 514116491Sharti/* 515116491Sharti * Check whether the VCC is really closed on the hardware and available for 516116491Sharti * open. Check that we have enough resources. If this function returns ok, 517116491Sharti * a later actual open must succeed. Assume, that we are locked between this 518116491Sharti * function and the next one, so that nothing does change. For CBR this 519116491Sharti * assigns the rate group and set the rate group's parameter. 520116491Sharti */ 521116491Shartiint 522116491Shartihatm_tx_vcc_can_open(struct hatm_softc *sc, u_int cid, struct hevcc *vcc) 523116491Sharti{ 524116491Sharti uint32_t v, line_rate; 525116491Sharti u_int rc, idx, free_idx; 526116491Sharti struct atmio_tparam *t = &vcc->param.tparam; 527116491Sharti 528116491Sharti /* verify that connection is closed */ 529116491Sharti#if 0 530116491Sharti v = READ_TSR(sc, cid, 4); 531116491Sharti if(!(v & HE_REGM_TSR4_SESS_END)) { 532116491Sharti if_printf(&sc->ifatm.ifnet, "cid=%#x not closed (TSR4)\n", cid); 533116491Sharti return (EBUSY); 534116491Sharti } 535116491Sharti#endif 536116491Sharti v = READ_TSR(sc, cid, 0); 537116491Sharti if((v & HE_REGM_TSR0_CONN_STATE) != 0) { 538116491Sharti if_printf(&sc->ifatm.ifnet, "cid=%#x not closed (TSR0=%#x)\n", 539116491Sharti cid, v); 540116491Sharti return (EBUSY); 541116491Sharti } 542116491Sharti 543116491Sharti /* check traffic parameters */ 544116491Sharti line_rate = sc->he622 ? ATM_RATE_622M : ATM_RATE_155M; 545116491Sharti switch (vcc->param.traffic) { 546116491Sharti 547116491Sharti case ATMIO_TRAFFIC_UBR: 548116491Sharti if (t->pcr == 0 || t->pcr > line_rate) 549116491Sharti t->pcr = line_rate; 550116491Sharti if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 || 551116491Sharti t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 || 552116491Sharti t->cdf != 0) 553116491Sharti return (EINVAL); 554116491Sharti break; 555116491Sharti 556116491Sharti case ATMIO_TRAFFIC_CBR: 557116491Sharti /* 558116491Sharti * Compute rate group index 559116491Sharti */ 560116491Sharti if (t->pcr < 10) 561116491Sharti t->pcr = 10; 562116491Sharti if (sc->cbr_bw + t->pcr > line_rate) 563116491Sharti return (EINVAL); 564116491Sharti if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 || 565116491Sharti t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 || 566116491Sharti t->cdf != 0) 567116491Sharti return (EINVAL); 568116491Sharti 569116491Sharti rc = cps_to_rate(sc, t->pcr); 570116491Sharti free_idx = HE_REGN_CS_STPER; 571116491Sharti for (idx = 0; idx < HE_REGN_CS_STPER; idx++) { 572116491Sharti if (sc->rate_ctrl[idx].refcnt == 0) { 573116491Sharti if (free_idx == HE_REGN_CS_STPER) 574116491Sharti free_idx = idx; 575116491Sharti } else { 576116491Sharti if (sc->rate_ctrl[idx].rate == rc) 577116491Sharti break; 578116491Sharti } 579116491Sharti } 580116491Sharti if (idx == HE_REGN_CS_STPER) { 581116491Sharti if ((idx = free_idx) == HE_REGN_CS_STPER) 582116491Sharti return (EBUSY); 583116491Sharti sc->rate_ctrl[idx].rate = rc; 584116491Sharti WRITE_MBOX4(sc, HE_REGO_CS_STPER(idx), rc); 585116491Sharti } 586116491Sharti vcc->rc = idx; 587116491Sharti break; 588116491Sharti 589116491Sharti case ATMIO_TRAFFIC_ABR: 590116491Sharti if (t->pcr > line_rate) 591116491Sharti t->pcr = line_rate; 592116491Sharti if (t->mcr > line_rate) 593116491Sharti t->mcr = line_rate; 594116491Sharti if (t->icr > line_rate) 595116491Sharti t->icr = line_rate; 596116491Sharti if (t->tbe == 0 || t->tbe >= 1 << 24 || t->nrm > 7 || 597116491Sharti t->trm > 7 || t->adtf >= 1 << 10 || t->rif > 15 || 598116491Sharti t->rdf > 15 || t->cdf > 7) 599116491Sharti return (EINVAL); 600116491Sharti break; 601116491Sharti 602116491Sharti default: 603116491Sharti return (EINVAL); 604116491Sharti } 605116491Sharti return (0); 606116491Sharti} 607116491Sharti 608116491Sharti#define NRM_CODE2VAL(CODE) (2 * (1 << (CODE))) 609116491Sharti 610116491Sharti/* 611116491Sharti * Actually open the transmit VCC 612116491Sharti */ 613116491Shartivoid 614116491Shartihatm_tx_vcc_open(struct hatm_softc *sc, u_int cid) 615116491Sharti{ 616116491Sharti struct hevcc *vcc = sc->vccs[cid]; 617116491Sharti uint32_t tsr0, tsr4, atmf, crm; 618116491Sharti const struct atmio_tparam *t = &vcc->param.tparam; 619116491Sharti 620116491Sharti if (vcc->param.aal == ATMIO_AAL_5) { 621116491Sharti tsr0 = HE_REGM_TSR0_AAL_5 << HE_REGS_TSR0_AAL; 622116491Sharti tsr4 = HE_REGM_TSR4_AAL_5 << HE_REGS_TSR4_AAL; 623116491Sharti } else { 624116491Sharti tsr0 = HE_REGM_TSR0_AAL_0 << HE_REGS_TSR0_AAL; 625116491Sharti tsr4 = HE_REGM_TSR4_AAL_0 << HE_REGS_TSR4_AAL; 626116491Sharti } 627116491Sharti tsr4 |= 1; 628116491Sharti 629116491Sharti switch (vcc->param.traffic) { 630116491Sharti 631116491Sharti case ATMIO_TRAFFIC_UBR: 632116491Sharti atmf = hatm_cps2atmf(t->pcr); 633116491Sharti 634116491Sharti tsr0 |= HE_REGM_TSR0_TRAFFIC_UBR << HE_REGS_TSR0_TRAFFIC; 635116491Sharti tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER; 636116491Sharti 637116491Sharti WRITE_TSR(sc, cid, 0, 0xf, tsr0); 638116491Sharti WRITE_TSR(sc, cid, 4, 0xf, tsr4); 639116491Sharti WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR)); 640116491Sharti WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR)); 641116491Sharti WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT); 642116491Sharti WRITE_TSR(sc, cid, 3, 0xf, 0); 643116491Sharti WRITE_TSR(sc, cid, 5, 0xf, 0); 644116491Sharti WRITE_TSR(sc, cid, 6, 0xf, 0); 645116491Sharti WRITE_TSR(sc, cid, 7, 0xf, 0); 646116491Sharti WRITE_TSR(sc, cid, 8, 0xf, 0); 647116491Sharti WRITE_TSR(sc, cid, 10, 0xf, 0); 648116491Sharti WRITE_TSR(sc, cid, 11, 0xf, 0); 649116491Sharti WRITE_TSR(sc, cid, 12, 0xf, 0); 650116491Sharti WRITE_TSR(sc, cid, 13, 0xf, 0); 651116491Sharti WRITE_TSR(sc, cid, 14, 0xf, 0); 652116491Sharti break; 653116491Sharti 654116491Sharti case ATMIO_TRAFFIC_CBR: 655116491Sharti atmf = hatm_cps2atmf(t->pcr); 656116491Sharti sc->rate_ctrl[vcc->rc].refcnt++; 657116491Sharti 658116491Sharti tsr0 |= HE_REGM_TSR0_TRAFFIC_CBR << HE_REGS_TSR0_TRAFFIC; 659116491Sharti tsr0 |= vcc->rc; 660116491Sharti 661116491Sharti WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR)); 662116491Sharti WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR)); 663116491Sharti WRITE_TSR(sc, cid, 3, 0xf, 0); 664116491Sharti WRITE_TSR(sc, cid, 5, 0xf, 0); 665116491Sharti WRITE_TSR(sc, cid, 6, 0xf, 0); 666116491Sharti WRITE_TSR(sc, cid, 7, 0xf, 0); 667116491Sharti WRITE_TSR(sc, cid, 8, 0xf, 0); 668116491Sharti WRITE_TSR(sc, cid, 10, 0xf, 0); 669116491Sharti WRITE_TSR(sc, cid, 11, 0xf, 0); 670116491Sharti WRITE_TSR(sc, cid, 12, 0xf, 0); 671116491Sharti WRITE_TSR(sc, cid, 13, 0xf, 0); 672116491Sharti WRITE_TSR(sc, cid, 14, 0xf, 0); 673116491Sharti WRITE_TSR(sc, cid, 4, 0xf, tsr4); 674116491Sharti WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT); 675116491Sharti WRITE_TSR(sc, cid, 0, 0xf, tsr0); 676116491Sharti 677116491Sharti sc->cbr_bw += t->pcr; 678116491Sharti break; 679116491Sharti 680116491Sharti case ATMIO_TRAFFIC_ABR: 681116491Sharti if ((crm = t->tbe / NRM_CODE2VAL(t->nrm)) > 0xffff) 682116491Sharti crm = 0xffff; 683116491Sharti 684116491Sharti tsr0 |= HE_REGM_TSR0_TRAFFIC_ABR << HE_REGS_TSR0_TRAFFIC; 685116491Sharti tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER; 686116491Sharti 687116491Sharti WRITE_TSR(sc, cid, 0, 0xf, tsr0); 688116491Sharti WRITE_TSR(sc, cid, 4, 0xf, tsr4); 689116491Sharti 690116491Sharti WRITE_TSR(sc, cid, 1, 0xf, 691116491Sharti ((hatm_cps2atmf(t->pcr) << HE_REGS_TSR1_PCR) | 692116491Sharti (hatm_cps2atmf(t->mcr) << HE_REGS_TSR1_MCR))); 693116491Sharti WRITE_TSR(sc, cid, 2, 0xf, 694116491Sharti (hatm_cps2atmf(t->icr) << HE_REGS_TSR2_ACR)); 695116491Sharti WRITE_TSR(sc, cid, 3, 0xf, 696116491Sharti ((NRM_CODE2VAL(t->nrm) - 1) << HE_REGS_TSR3_NRM) | 697116491Sharti (crm << HE_REGS_TSR3_CRM)); 698116491Sharti 699116491Sharti WRITE_TSR(sc, cid, 5, 0xf, 0); 700116491Sharti WRITE_TSR(sc, cid, 6, 0xf, 0); 701116491Sharti WRITE_TSR(sc, cid, 7, 0xf, 0); 702116491Sharti WRITE_TSR(sc, cid, 8, 0xf, 0); 703116491Sharti WRITE_TSR(sc, cid, 10, 0xf, 0); 704116491Sharti WRITE_TSR(sc, cid, 12, 0xf, 0); 705116491Sharti WRITE_TSR(sc, cid, 14, 0xf, 0); 706116491Sharti WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT); 707116491Sharti 708116491Sharti WRITE_TSR(sc, cid, 11, 0xf, 709116491Sharti (hatm_cps2atmf(t->icr) << HE_REGS_TSR11_ICR) | 710116491Sharti (t->trm << HE_REGS_TSR11_TRM) | 711116491Sharti (t->nrm << HE_REGS_TSR11_NRM) | 712116491Sharti (t->adtf << HE_REGS_TSR11_ADTF)); 713116491Sharti 714116491Sharti WRITE_TSR(sc, cid, 13, 0xf, 715116491Sharti (t->rdf << HE_REGS_TSR13_RDF) | 716116491Sharti (t->rif << HE_REGS_TSR13_RIF) | 717116491Sharti (t->cdf << HE_REGS_TSR13_CDF) | 718116491Sharti (crm << HE_REGS_TSR13_CRM)); 719116491Sharti 720116491Sharti break; 721116491Sharti 722116491Sharti default: 723116491Sharti return; 724116491Sharti } 725116491Sharti 726116491Sharti vcc->vflags |= HE_VCC_TX_OPEN; 727116491Sharti} 728116491Sharti 729116491Sharti/* 730116491Sharti * Close the TX side of a VCC. Set the CLOSING flag. 731116491Sharti */ 732116491Shartivoid 733116491Shartihatm_tx_vcc_close(struct hatm_softc *sc, u_int cid) 734116491Sharti{ 735116491Sharti struct hevcc *vcc = sc->vccs[cid]; 736116491Sharti struct tpd *tpd_list[1]; 737116491Sharti u_int i, pcr = 0; 738116491Sharti 739116491Sharti WRITE_TSR(sc, cid, 4, 0x8, HE_REGM_TSR4_FLUSH); 740116491Sharti 741116491Sharti switch (vcc->param.traffic) { 742116491Sharti 743116491Sharti case ATMIO_TRAFFIC_CBR: 744116491Sharti WRITE_TSR(sc, cid, 14, 0x8, HE_REGM_TSR14_CBR_DELETE); 745116491Sharti break; 746116491Sharti 747116491Sharti case ATMIO_TRAFFIC_ABR: 748116491Sharti WRITE_TSR(sc, cid, 14, 0x4, HE_REGM_TSR14_ABR_CLOSE); 749116491Sharti pcr = vcc->param.tparam.pcr; 750116491Sharti /* FALL THROUGH */ 751116491Sharti 752116491Sharti case ATMIO_TRAFFIC_UBR: 753116491Sharti WRITE_TSR(sc, cid, 1, 0xf, 754116491Sharti hatm_cps2atmf(HE_CONFIG_FLUSH_RATE) << HE_REGS_TSR1_MCR | 755116491Sharti hatm_cps2atmf(pcr) << HE_REGS_TSR1_PCR); 756116491Sharti break; 757116491Sharti } 758116491Sharti 759116491Sharti tpd_list[0] = hatm_alloc_tpd(sc, 0); 760116491Sharti tpd_list[0]->tpd.addr |= HE_REGM_TPD_EOS | HE_REGM_TPD_INTR; 761116491Sharti tpd_list[0]->cid = cid; 762116491Sharti 763116491Sharti vcc->vflags |= HE_VCC_TX_CLOSING; 764116491Sharti vcc->vflags &= ~HE_VCC_TX_OPEN; 765116491Sharti 766116491Sharti i = 0; 767116491Sharti while (hatm_queue_tpds(sc, 1, tpd_list, cid) != 0) { 768116491Sharti if (++i == 1000) 769116491Sharti panic("TPDRQ permanently full"); 770116491Sharti DELAY(1000); 771116491Sharti } 772116491Sharti} 773116491Sharti 774116491Shartivoid 775116491Shartihatm_tx_vcc_closed(struct hatm_softc *sc, u_int cid) 776116491Sharti{ 777116491Sharti if (sc->vccs[cid]->param.traffic == ATMIO_TRAFFIC_CBR) { 778116491Sharti sc->cbr_bw -= sc->vccs[cid]->param.tparam.pcr; 779116491Sharti sc->rate_ctrl[sc->vccs[cid]->rc].refcnt--; 780116491Sharti } 781116491Sharti} 782