aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/hatm/if_hatm_tx.c
blob: c868951d0878102de97805c103268cf56a54e87d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
 * Copyright (c) 2001-2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 * 	All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Author: Hartmut Brandt <harti@freebsd.org>
 *
 * ForeHE driver.
 *
 * Transmission.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_inet.h"
#include "opt_natm.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/bus.h>
#include <sys/errno.h>
#include <sys/conf.h>
#include <sys/module.h>
#include <sys/queue.h>
#include <sys/syslog.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <vm/uma.h>

#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/socket.h>

#include <net/if.h>
#include <net/if_media.h>
#include <net/if_atm.h>
#include <net/route.h>
#ifdef ENABLE_BPF
#include <net/bpf.h>
#endif
#include <netinet/in.h>
#include <netinet/if_atm.h>

#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>

#include <dev/utopia/utopia.h>
#include <dev/hatm/if_hatmconf.h>
#include <dev/hatm/if_hatmreg.h>
#include <dev/hatm/if_hatmvar.h>

/*
 * Allocate a new TPD, zero the TPD part. Cannot return NULL if
 * flag is 0. The TPD is removed from the free list and its used
 * bit is set.
 */
static struct tpd *
hatm_alloc_tpd(struct hatm_softc *sc, u_int flags)
{
	struct tpd *t;

	/* if we allocate a transmit TPD check for the reserve */
	if (flags & M_NOWAIT) {
		if (sc->tpd_nfree <= HE_CONFIG_TPD_RESERVE)
			return (NULL);
	} else {
		if (sc->tpd_nfree == 0)
			return (NULL);
	}

	/* make it beeing used */
	t = SLIST_FIRST(&sc->tpd_free);
	KASSERT(t != NULL, ("tpd botch"));
	SLIST_REMOVE_HEAD(&sc->tpd_free, link);
	TPD_SET_USED(sc, t->no);
	sc->tpd_nfree--;

	/* initialize */
	t->mbuf = NULL;
	t->cid = 0;
	bzero(&t->tpd, sizeof(t->tpd));
	t->tpd.addr = t->no << HE_REGS_TPD_ADDR;

	return (t);
}

/*
 * Free a TPD. If the mbuf pointer in that TPD is not zero, it is assumed, that
 * the DMA map of this TPD was used to load this mbuf. The map is unloaded
 * and the mbuf is freed. The TPD is put back onto the free list and
 * its used bit is cleared.
 */
static void
hatm_free_tpd(struct hatm_softc *sc, struct tpd *tpd)
{
	if (tpd->mbuf != NULL) {
		bus_dmamap_unload(sc->tx_tag, tpd->map);
		m_freem(tpd->mbuf);
		tpd->mbuf = NULL;
	}

	/* insert TPD into free list */
	SLIST_INSERT_HEAD(&sc->tpd_free, tpd, link);
	TPD_CLR_USED(sc, tpd->no);
	sc->tpd_nfree++;
}

/*
 * Queue a number of TPD. If there is not enough space none of the TPDs
 * is queued and an error code is returned.
 */
static int
hatm_queue_tpds(struct hatm_softc *sc, u_int count, struct tpd **list,
    u_int cid)
{
	u_int space;
	u_int i;

	if (count >= sc->tpdrq.size) {
		sc->istats.tdprq_full++;
		return (EBUSY);
	}

	if (sc->tpdrq.tail < sc->tpdrq.head)
		space = sc->tpdrq.head - sc->tpdrq.tail;
	else
		space = sc->tpdrq.head - sc->tpdrq.tail +  sc->tpdrq.size;

	if (space <= count) {
		sc->tpdrq.head =
		    (READ4(sc, HE_REGO_TPDRQ_H) >> HE_REGS_TPDRQ_H_H) &
		    (sc->tpdrq.size - 1);

		if (sc->tpdrq.tail < sc->tpdrq.head)
			space = sc->tpdrq.head - sc->tpdrq.tail;
		else
			space = sc->tpdrq.head - sc->tpdrq.tail +
			    sc->tpdrq.size;

		if (space <= count) {
			if_printf(&sc->ifatm.ifnet, "TPDRQ full\n");
			sc->istats.tdprq_full++;
			return (EBUSY);
		}
	}

	/* we are going to write to the TPD queue space */
	bus_dmamap_sync(sc->tpdrq.mem.tag, sc->tpdrq.mem.map,
	    BUS_DMASYNC_PREWRITE);

	/* put the entries into the TPD space */
	for (i = 0; i < count; i++) {
		/* we are going to 'write' the TPD to the device */
		bus_dmamap_sync(sc->tpds.tag, sc->tpds.map,
		    BUS_DMASYNC_PREWRITE);

		sc->tpdrq.tpdrq[sc->tpdrq.tail].tpd =
		    sc->tpds.paddr + HE_TPD_SIZE * list[i]->no;
		sc->tpdrq.tpdrq[sc->tpdrq.tail].cid = cid;

		if (++sc->tpdrq.tail == sc->tpdrq.size)
			sc->tpdrq.tail = 0;
	}

	/* update tail pointer */
	WRITE4(sc, HE_REGO_TPDRQ_T, (sc->tpdrq.tail << HE_REGS_TPDRQ_T_T));

	return (0);
}

/*
 * Helper struct for communication with the DMA load helper.
 */
struct load_txbuf_arg {
	struct hatm_softc *sc;
	struct tpd *first;
	struct mbuf *mbuf;
	struct hevcc *vcc;
	int error;
	u_int pti;
	u_int vpi, vci;
};

/*
 * Loader callback for the mbuf. This function allocates the TPDs and
 * fills them. It puts the dmamap and and the mbuf pointer into the last
 * TPD and then tries to queue all the TPDs. If anything fails, all TPDs
 * allocated by this function are freed and the error flag is set in the
 * argument structure. The first TPD must then be freed by the caller.
 */
static void
hatm_load_txbuf(void *uarg, bus_dma_segment_t *segs, int nseg,
    bus_size_t mapsize, int error)
{
	struct load_txbuf_arg *arg = uarg;
	u_int tpds_needed, i, n, tpd_cnt;
	int need_intr;
	struct tpd *tpd;
	struct tpd *tpd_list[HE_CONFIG_MAX_TPD_PER_PACKET];

	if (error != 0) {
		DBG(arg->sc, DMA, ("%s -- error=%d plen=%d\n",
		    __func__, error, arg->mbuf->m_pkthdr.len));
		return;
	}

	/* ensure, we have enough TPDs (remember, we already have one) */
	tpds_needed = (nseg + 2) / 3;
	if (HE_CONFIG_TPD_RESERVE + tpds_needed - 1 > arg->sc->tpd_nfree) {
		if_printf(&arg->sc->ifatm.ifnet, "%s -- out of TPDs (need %d, "
		    "have %u)\n", __func__, tpds_needed - 1,
		    arg->sc->tpd_nfree + 1);
		arg->error = 1;
		return;
	}

	/*
	 * Check for the maximum number of TPDs on the connection.
	 */
	need_intr = 0;
	if (arg->sc->max_tpd > 0) {
		if (arg->vcc->ntpds + tpds_needed > arg->sc->max_tpd) {
			arg->sc->istats.flow_closed++;
			arg->vcc->vflags |= HE_VCC_FLOW_CTRL;
			ATMEV_SEND_FLOW_CONTROL(&arg->sc->ifatm,
			    arg->vpi, arg->vci, 1);
			arg->error = 1;
			return;
		}
		if (arg->vcc->ntpds + tpds_needed >
		    (9 * arg->sc->max_tpd) / 10)
			need_intr = 1;
	}

	tpd = arg->first;
	tpd_cnt = 0;
	tpd_list[tpd_cnt++] = tpd;
	for (i = n = 0; i < nseg; i++, n++) {
		if (n == 3) {
			if ((tpd = hatm_alloc_tpd(arg->sc, M_NOWAIT)) == NULL)
				/* may not fail (see check above) */
				panic("%s: out of TPDs", __func__);
			tpd->cid = arg->first->cid;
			tpd->tpd.addr |= arg->pti;
			tpd_list[tpd_cnt++] = tpd;
			n = 0;
		}
		KASSERT(segs[i].ds_addr <= 0xffffffffLU,
		    ("phys addr too large %lx", (u_long)segs[i].ds_addr));

		DBG(arg->sc, DMA, ("DMA loaded: %lx/%lu",
		    (u_long)segs[i].ds_addr, (u_long)segs[i].ds_len));

		tpd->tpd.bufs[n].addr = segs[i].ds_addr;
		tpd->tpd.bufs[n].len = segs[i].ds_len;

		DBG(arg->sc, TX, ("seg[%u]=tpd[%u,%u]=%x/%u", i,
		    tpd_cnt, n, tpd->tpd.bufs[n].addr, tpd->tpd.bufs[n].len));

		if (i == nseg - 1)
			tpd->tpd.bufs[n].len |= HE_REGM_TPD_LST;
	}

	/*
	 * Swap the MAP in the first and the last TPD and set the mbuf
	 * pointer into the last TPD. We use the map in the last TPD, because
	 * the map must stay valid until the last TPD is processed by the card.
	 */
	if (tpd_cnt > 1) {
		bus_dmamap_t tmp;

		tmp = arg->first->map;
		arg->first->map = tpd_list[tpd_cnt - 1]->map;
		tpd_list[tpd_cnt - 1]->map = tmp;
	}
	tpd_list[tpd_cnt - 1]->mbuf = arg->mbuf;

	if (need_intr)
		tpd_list[tpd_cnt - 1]->tpd.addr |= HE_REGM_TPD_INTR;

	/* queue the TPDs */
	if (hatm_queue_tpds(arg->sc, tpd_cnt, tpd_list, arg->first->cid)) {
		/* free all, except the first TPD */
		for (i = 1; i < tpd_cnt; i++)
			hatm_free_tpd(arg->sc, tpd_list[i]);
		arg->error = 1;
		return;
	}
	arg->vcc->ntpds += tpd_cnt;
}


/*
 * Start output on the interface
 *
 * For raw aal we process only the first cell in the mbuf chain! XXX
 */
void
hatm_start(struct ifnet *ifp)
{
	struct hatm_softc *sc = (struct hatm_softc *)ifp->if_softc;
	struct mbuf *m;
	struct atm_pseudohdr *aph;
	u_int cid;
	struct tpd *tpd;
	struct load_txbuf_arg arg;
	u_int len;
	int error;

	if (!(ifp->if_flags & IFF_RUNNING))
		return;
	mtx_lock(&sc->mtx);
	arg.sc = sc;

	while (1) {
		IF_DEQUEUE(&ifp->if_snd, m);
		if (m == NULL)
			break;

		if (m->m_len < sizeof(*aph))
			if ((m = m_pullup(m, sizeof(*aph))) == NULL)
				continue;

		aph = mtod(m, struct atm_pseudohdr *);
		arg.vci = ATM_PH_VCI(aph);
		arg.vpi = ATM_PH_VPI(aph);
		m_adj(m, sizeof(*aph));

		if ((len = m->m_pkthdr.len) == 0) {
			m_freem(m);
			continue;
		}

		if ((arg.vpi & ~HE_VPI_MASK) || (arg.vci & ~HE_VCI_MASK) ||
		    (arg.vci == 0)) {
			m_freem(m);
			continue;
		}
		cid = HE_CID(arg.vpi, arg.vci);
		arg.vcc = sc->vccs[cid];

		if (arg.vcc == NULL || !(arg.vcc->vflags & HE_VCC_OPEN)) {
			m_freem(m);
			continue;
		}
		if (arg.vcc->vflags & HE_VCC_FLOW_CTRL) {
			m_freem(m);
			sc->istats.flow_drop++;
			continue;
		}

		arg.pti = 0;
		if (arg.vcc->param.aal == ATMIO_AAL_RAW) {
			if (len < 52) {
				m_freem(m);
				continue;
			}
			if (len > 52) {
				m_adj(m, -((int)(len - 52)));
				len = 52;
			}
			if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
				continue;

			/* ignore header except payload type and CLP */
			arg.pti = mtod(m, u_char *)[3] & 0xf;
			arg.pti = ((arg.pti & 0xe) << 2) | ((arg.pti & 1) << 1);
			m_adj(m, 4);
			len -= 4;
		}

#ifdef ENABLE_BPF
		if (!(arg.vcc->param.flags & ATMIO_FLAG_NG) &&
		    (arg.vcc->param.aal == ATMIO_AAL_5) &&
		    (arg.vcc->param.flags & ATM_PH_LLCSNAP))
		 	BPF_MTAP(ifp, m);
#endif

		/* Now load a DMA map with the packet. Allocate the first
		 * TPD to get a map. Additional TPDs may be allocated by the
		 * callback. */
		if ((tpd = hatm_alloc_tpd(sc, M_NOWAIT)) == NULL) {
			m_freem(m);
			sc->ifatm.ifnet.if_oerrors++;
			continue;
		}
		tpd->cid = cid;
		tpd->tpd.addr |= arg.pti;
		arg.first = tpd;
		arg.error = 0;
		arg.mbuf = m;

		error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m,
		    hatm_load_txbuf, &arg, BUS_DMA_NOWAIT);

		if (error == EFBIG) {
			/* try to defragment the packet */
			sc->istats.defrag++;
			m = m_defrag(m, M_DONTWAIT);
			if (m == NULL) {
				sc->ifatm.ifnet.if_oerrors++;
				continue;
			}
			arg.mbuf = m;
			error = bus_dmamap_load_mbuf(sc->tx_tag, tpd->map, m,
			    hatm_load_txbuf, &arg, BUS_DMA_NOWAIT);
		}

		if (error != 0) {
			if_printf(&sc->ifatm.ifnet, "mbuf loaded error=%d\n",
			    error);
			hatm_free_tpd(sc, tpd);
			sc->ifatm.ifnet.if_oerrors++;
			continue;
		}
		if (arg.error) {
			hatm_free_tpd(sc, tpd);
			sc->ifatm.ifnet.if_oerrors++;
			continue;
		}
		arg.vcc->opackets++;
		arg.vcc->obytes += len;
		sc->ifatm.ifnet.if_opackets++;
	}
	mtx_unlock(&sc->mtx);
}

void
hatm_tx_complete(struct hatm_softc *sc, struct tpd *tpd, uint32_t flags)
{
	struct hevcc *vcc = sc->vccs[tpd->cid];

	DBG(sc, TX, ("tx_complete cid=%#x flags=%#x", tpd->cid, flags));

	if (vcc == NULL)
		return;
	if ((flags & HE_REGM_TBRQ_EOS) && (vcc->vflags & HE_VCC_TX_CLOSING)) {
		vcc->vflags &= ~HE_VCC_TX_CLOSING;
		if (vcc->param.flags & ATMIO_FLAG_ASYNC) {
			hatm_tx_vcc_closed(sc, tpd->cid);
			if (!(vcc->vflags & HE_VCC_OPEN)) {
				hatm_vcc_closed(sc, tpd->cid);
				vcc = NULL;
			}
		} else
			cv_signal(&sc->vcc_cv);
	}
	hatm_free_tpd(sc, tpd);

	if (vcc == NULL)
		return;

	vcc->ntpds--;

	if ((vcc->vflags & HE_VCC_FLOW_CTRL) &&
	    vcc->ntpds <= HE_CONFIG_TPD_FLOW_ENB) {
		vcc->vflags &= ~HE_VCC_FLOW_CTRL;
		ATMEV_SEND_FLOW_CONTROL(&sc->ifatm,
		    HE_VPI(tpd->cid), HE_VCI(tpd->cid), 0);
	}
}

/*
 * Convert CPS to Rate for a rate group
 */
static u_int
cps_to_rate(struct hatm_softc *sc, uint32_t cps)
{
	u_int clk = sc->he622 ? HE_622_CLOCK : HE_155_CLOCK;
	u_int period, rate;

	/* how many double ticks between two cells */
	period = (clk + 2 * cps - 1) / (2 * cps);
	rate = hatm_cps2atmf(period);
	if (hatm_atmf2cps(rate) < period)
		rate++;

	return (rate);
}

/*
 * Check whether the VCC is really closed on the hardware and available for
 * open. Check that we have enough resources. If this function returns ok,
 * a later actual open must succeed. Assume, that we are locked between this
 * function and the next one, so that nothing does change. For CBR this
 * assigns the rate group and set the rate group's parameter.
 */
int
hatm_tx_vcc_can_open(struct hatm_softc *sc, u_int cid, struct hevcc *vcc)
{
	uint32_t v, line_rate;
	u_int rc, idx, free_idx;
	struct atmio_tparam *t = &vcc->param.tparam;

	/* verify that connection is closed */
#if 0
	v = READ_TSR(sc, cid, 4);
	if(!(v & HE_REGM_TSR4_SESS_END)) {
		if_printf(&sc->ifatm.ifnet, "cid=%#x not closed (TSR4)\n", cid);
		return (EBUSY);
	}
#endif
	v = READ_TSR(sc, cid, 0);
	if((v & HE_REGM_TSR0_CONN_STATE) != 0) {
		if_printf(&sc->ifatm.ifnet, "cid=%#x not closed (TSR0=%#x)\n",
		    cid, v);
		return (EBUSY);
	}

	/* check traffic parameters */
	line_rate = sc->he622 ? ATM_RATE_622M : ATM_RATE_155M;
	switch (vcc->param.traffic) {

	  case ATMIO_TRAFFIC_UBR:
		if (t->pcr == 0 || t->pcr > line_rate)
			t->pcr = line_rate;
		if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 ||
		    t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 ||
		    t->cdf != 0)
			return (EINVAL);
		break;

	  case ATMIO_TRAFFIC_CBR:
		/*
		 * Compute rate group index
		 */
		if (t->pcr < 10)
			t->pcr = 10;
		if (sc->cbr_bw + t->pcr > line_rate)
			return (EINVAL);
		if (t->mcr != 0 || t->icr != 0 || t->tbe != 0 || t->nrm != 0 ||
		    t->trm != 0 || t->adtf != 0 || t->rif != 0 || t->rdf != 0 ||
		    t->cdf != 0)
			return (EINVAL);

		rc = cps_to_rate(sc, t->pcr);
		free_idx = HE_REGN_CS_STPER;
		for (idx = 0; idx < HE_REGN_CS_STPER; idx++) {
			if (sc->rate_ctrl[idx].refcnt == 0) {
				if (free_idx == HE_REGN_CS_STPER)
					free_idx = idx;
			} else {
				if (sc->rate_ctrl[idx].rate == rc)
					break;
			}
		}
		if (idx == HE_REGN_CS_STPER) {
			if ((idx = free_idx) == HE_REGN_CS_STPER)
				return (EBUSY);
			sc->rate_ctrl[idx].rate = rc;
		}
		vcc->rc = idx;

		/* commit */
		sc->rate_ctrl[idx].refcnt++;
		sc->cbr_bw += t->pcr;
		break;

	  case ATMIO_TRAFFIC_ABR:
		if (t->pcr > line_rate)
			t->pcr = line_rate;
		if (t->mcr > line_rate)
			t->mcr = line_rate;
		if (t->icr > line_rate)
			t->icr = line_rate;
		if (t->tbe == 0 || t->tbe >= 1 << 24 || t->nrm > 7 ||
		    t->trm > 7 || t->adtf >= 1 << 10 || t->rif > 15 ||
		    t->rdf > 15 || t->cdf > 7)
			return (EINVAL);
		break;

	  default:
		return (EINVAL);
	}
	return (0);
}

#define NRM_CODE2VAL(CODE) (2 * (1 << (CODE)))

/*
 * Actually open the transmit VCC
 */
void
hatm_tx_vcc_open(struct hatm_softc *sc, u_int cid)
{
	struct hevcc *vcc = sc->vccs[cid];
	uint32_t tsr0, tsr4, atmf, crm;
	const struct atmio_tparam *t = &vcc->param.tparam;

	if (vcc->param.aal == ATMIO_AAL_5) {
		tsr0 = HE_REGM_TSR0_AAL_5 << HE_REGS_TSR0_AAL;
		tsr4 = HE_REGM_TSR4_AAL_5 << HE_REGS_TSR4_AAL;
	} else {
		tsr0 = HE_REGM_TSR0_AAL_0 << HE_REGS_TSR0_AAL;
		tsr4 = HE_REGM_TSR4_AAL_0 << HE_REGS_TSR4_AAL;
	}
	tsr4 |= 1;

	switch (vcc->param.traffic) {

	  case ATMIO_TRAFFIC_UBR:
		atmf = hatm_cps2atmf(t->pcr);

		tsr0 |= HE_REGM_TSR0_TRAFFIC_UBR << HE_REGS_TSR0_TRAFFIC;
		tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER;

		WRITE_TSR(sc, cid, 0, 0xf, tsr0);
		WRITE_TSR(sc, cid, 4, 0xf, tsr4);
		WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR));
		WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR));
		WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT);
		WRITE_TSR(sc, cid, 3, 0xf, 0);
		WRITE_TSR(sc, cid, 5, 0xf, 0);
		WRITE_TSR(sc, cid, 6, 0xf, 0);
		WRITE_TSR(sc, cid, 7, 0xf, 0);
		WRITE_TSR(sc, cid, 8, 0xf, 0);
		WRITE_TSR(sc, cid, 10, 0xf, 0);
		WRITE_TSR(sc, cid, 11, 0xf, 0);
		WRITE_TSR(sc, cid, 12, 0xf, 0);
		WRITE_TSR(sc, cid, 13, 0xf, 0);
		WRITE_TSR(sc, cid, 14, 0xf, 0);
		break;

	  case ATMIO_TRAFFIC_CBR:
		atmf = hatm_cps2atmf(t->pcr);

		if (sc->rate_ctrl[vcc->rc].refcnt == 1)
			WRITE_MBOX4(sc, HE_REGO_CS_STPER(vcc->rc),
			    sc->rate_ctrl[vcc->rc].rate);

		tsr0 |= HE_REGM_TSR0_TRAFFIC_CBR << HE_REGS_TSR0_TRAFFIC;
		tsr0 |= vcc->rc;

		WRITE_TSR(sc, cid, 1, 0xf, (atmf << HE_REGS_TSR1_PCR));
		WRITE_TSR(sc, cid, 2, 0xf, (atmf << HE_REGS_TSR2_ACR));
		WRITE_TSR(sc, cid, 3, 0xf, 0);
		WRITE_TSR(sc, cid, 5, 0xf, 0);
		WRITE_TSR(sc, cid, 6, 0xf, 0);
		WRITE_TSR(sc, cid, 7, 0xf, 0);
		WRITE_TSR(sc, cid, 8, 0xf, 0);
		WRITE_TSR(sc, cid, 10, 0xf, 0);
		WRITE_TSR(sc, cid, 11, 0xf, 0);
		WRITE_TSR(sc, cid, 12, 0xf, 0);
		WRITE_TSR(sc, cid, 13, 0xf, 0);
		WRITE_TSR(sc, cid, 14, 0xf, 0);
		WRITE_TSR(sc, cid, 4, 0xf, tsr4);
		WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT);
		WRITE_TSR(sc, cid, 0, 0xf, tsr0);

		break;

	  case ATMIO_TRAFFIC_ABR:
		if ((crm = t->tbe / NRM_CODE2VAL(t->nrm)) > 0xffff)
			crm = 0xffff;

		tsr0 |= HE_REGM_TSR0_TRAFFIC_ABR << HE_REGS_TSR0_TRAFFIC;
		tsr0 |= HE_REGM_TSR0_USE_WMIN | HE_REGM_TSR0_UPDATE_GER;

		WRITE_TSR(sc, cid, 0, 0xf, tsr0);
		WRITE_TSR(sc, cid, 4, 0xf, tsr4);

		WRITE_TSR(sc, cid, 1, 0xf,
		    ((hatm_cps2atmf(t->pcr) << HE_REGS_TSR1_PCR) |
		     (hatm_cps2atmf(t->mcr) << HE_REGS_TSR1_MCR)));
		WRITE_TSR(sc, cid, 2, 0xf,
		    (hatm_cps2atmf(t->icr) << HE_REGS_TSR2_ACR));
		WRITE_TSR(sc, cid, 3, 0xf,
		    ((NRM_CODE2VAL(t->nrm) - 1) << HE_REGS_TSR3_NRM) |
		    (crm << HE_REGS_TSR3_CRM));

		WRITE_TSR(sc, cid, 5, 0xf, 0);
		WRITE_TSR(sc, cid, 6, 0xf, 0);
		WRITE_TSR(sc, cid, 7, 0xf, 0);
		WRITE_TSR(sc, cid, 8, 0xf, 0);
		WRITE_TSR(sc, cid, 10, 0xf, 0);
		WRITE_TSR(sc, cid, 12, 0xf, 0);
		WRITE_TSR(sc, cid, 14, 0xf, 0);
		WRITE_TSR(sc, cid, 9, 0xf, HE_REGM_TSR9_INIT);

		WRITE_TSR(sc, cid, 11, 0xf,
		    (hatm_cps2atmf(t->icr) << HE_REGS_TSR11_ICR) |
		    (t->trm << HE_REGS_TSR11_TRM) |
		    (t->nrm << HE_REGS_TSR11_NRM) |
		    (t->adtf << HE_REGS_TSR11_ADTF));

		WRITE_TSR(sc, cid, 13, 0xf,
		    (t->rdf << HE_REGS_TSR13_RDF) |
		    (t->rif << HE_REGS_TSR13_RIF) |
		    (t->cdf << HE_REGS_TSR13_CDF) |
		    (crm << HE_REGS_TSR13_CRM));

		break;

	  default:
		return;
	}

	vcc->vflags |= HE_VCC_TX_OPEN;
}

/*
 * Close the TX side of a VCC. Set the CLOSING flag.
 */
void
hatm_tx_vcc_close(struct hatm_softc *sc, u_int cid)
{
	struct hevcc *vcc = sc->vccs[cid];
	struct tpd *tpd_list[1];
	u_int i, pcr = 0;

	WRITE_TSR(sc, cid, 4, 0x8, HE_REGM_TSR4_FLUSH);

	switch (vcc->param.traffic) {

	  case ATMIO_TRAFFIC_CBR:
		WRITE_TSR(sc, cid, 14, 0x8, HE_REGM_TSR14_CBR_DELETE);
		break;

	  case ATMIO_TRAFFIC_ABR:
		WRITE_TSR(sc, cid, 14, 0x4, HE_REGM_TSR14_ABR_CLOSE);
		pcr = vcc->param.tparam.pcr;
		/* FALL THROUGH */

	  case ATMIO_TRAFFIC_UBR:
		WRITE_TSR(sc, cid, 1, 0xf,
		    hatm_cps2atmf(HE_CONFIG_FLUSH_RATE) << HE_REGS_TSR1_MCR |
		    hatm_cps2atmf(pcr) << HE_REGS_TSR1_PCR);
		break;
	}

	tpd_list[0] = hatm_alloc_tpd(sc, 0);
	tpd_list[0]->tpd.addr |= HE_REGM_TPD_EOS | HE_REGM_TPD_INTR;
	tpd_list[0]->cid = cid;

	vcc->vflags |= HE_VCC_TX_CLOSING;
	vcc->vflags &= ~HE_VCC_TX_OPEN;

	i = 0;
	while (hatm_queue_tpds(sc, 1, tpd_list, cid) != 0) {
		if (++i == 1000)
			panic("TPDRQ permanently full");
		DELAY(1000);
	}
}

void
hatm_tx_vcc_closed(struct hatm_softc *sc, u_int cid)
{
	if (sc->vccs[cid]->param.traffic == ATMIO_TRAFFIC_CBR) {
		sc->cbr_bw -= sc->vccs[cid]->param.tparam.pcr;
		sc->rate_ctrl[sc->vccs[cid]->rc].refcnt--;
	}
}