3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Author: Hartmut Brandt <harti@freebsd.org>
29 * Driver for IDT77252 based cards like ProSum's.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
44 #include <sys/errno.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/queue.h>
51 #include <sys/condvar.h>
52 #include <sys/endian.h>
55 #include <sys/sockio.h>
57 #include <sys/socket.h>
60 #include <net/if_media.h>
61 #include <net/if_atm.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/if_atm.h>
66 #include <machine/bus.h>
67 #include <machine/resource.h>
70 #include <sys/mbpool.h>
72 #include <dev/utopia/utopia.h>
73 #include <dev/patm/idt77252reg.h>
74 #include <dev/patm/if_patmvar.h>
76 static void patm_tst_init(struct patm_softc *sc);
77 static void patm_scd_init(struct patm_softc *sc);
80 * Start the card. This assumes the mutex to be held
83 patm_initialize(struct patm_softc *sc)
88 patm_debug(sc, ATTACH, "configuring...");
91 for (i = 0; i < sc->mmap->sram * 1024; i += 4)
92 patm_sram_write4(sc, i, 0, 0, 0, 0);
95 /* configuration register. Setting NOIDLE makes the timing wrong! */
96 cfg = IDT_CFG_TXFIFO9 | IDT_CFG_RXQ512 | PATM_CFG_VPI |
97 /* IDT_CFG_NOIDLE | */ sc->mmap->rxtab;
98 if (!(sc->flags & PATM_UNASS))
99 cfg |= IDT_CFG_IDLECLP;
100 patm_nor_write(sc, IDT_NOR_CFG, cfg);
102 /* clean all the status queues and the Raw handle */
103 memset(sc->tsq, 0, sc->sq_size);
106 patm_debug(sc, ATTACH, "RSQ %llx", (unsigned long long)sc->rsq_phy);
107 patm_nor_write(sc, IDT_NOR_RSQB, sc->rsq_phy);
108 patm_nor_write(sc, IDT_NOR_RSQT, sc->rsq_phy);
109 patm_nor_write(sc, IDT_NOR_RSQH, 0);
110 sc->rsq_last = PATM_RSQ_SIZE - 1;
112 /* initialize TSTB */
113 patm_nor_write(sc, IDT_NOR_TSTB, sc->mmap->tst1base << 2);
117 for (i = 0; i < IDT_TSQ_SIZE; i++)
118 sc->tsq[i].stamp = htole32(IDT_TSQE_EMPTY);
119 patm_nor_write(sc, IDT_NOR_TSQB, sc->tsq_phy);
120 patm_nor_write(sc, IDT_NOR_TSQH, 0);
121 patm_nor_write(sc, IDT_NOR_TSQT, 0);
122 sc->tsq_next = sc->tsq;
125 #if BYTE_ORDER == BIG_ENDIAN && 0
126 patm_nor_write(sc, IDT_NOR_GP, IDT_GP_BIGE);
128 patm_nor_write(sc, IDT_NOR_GP, 0);
132 patm_nor_write(sc, IDT_NOR_VPM, 0);
135 patm_nor_write(sc, IDT_NOR_RXFD,
136 IDT_RXFD(sc->mmap->rxfifo_addr, sc->mmap->rxfifo_code));
137 patm_nor_write(sc, IDT_NOR_RXFT, 0);
138 patm_nor_write(sc, IDT_NOR_RXFH, 0);
141 patm_debug(sc, ATTACH, "RWH %llx",
142 (unsigned long long)sc->rawhnd_phy);
143 patm_nor_write(sc, IDT_NOR_RAWHND, sc->rawhnd_phy);
146 patm_nor_write(sc, IDT_NOR_ABRSTD,
147 IDT_ABRSTD(sc->mmap->abrstd_addr, sc->mmap->abrstd_code));
148 for (i = 0; i < sc->mmap->abrstd_size; i++)
149 patm_sram_write(sc, sc->mmap->abrstd_addr + i, 0);
150 patm_nor_write(sc, IDT_NOR_ABRRQ, 0);
151 patm_nor_write(sc, IDT_NOR_VBRRQ, 0);
154 if (sc->flags & PATM_25M) {
155 for (i = 0; i < patm_rtables_size; i++)
156 patm_sram_write(sc, sc->mmap->rtables + i,
159 for (i = 0; i < patm_rtables_size; i++)
160 patm_sram_write(sc, sc->mmap->rtables + i,
163 patm_nor_write(sc, IDT_NOR_RTBL, sc->mmap->rtables << 2);
165 /* Maximum deficit */
166 patm_nor_write(sc, IDT_NOR_MXDFCT, 32 | IDT_MDFCT_LCI | IDT_MDFCT_LNI);
168 /* Free buffer queues */
169 patm_nor_write(sc, IDT_NOR_FBQP0, 0);
170 patm_nor_write(sc, IDT_NOR_FBQP1, 0);
171 patm_nor_write(sc, IDT_NOR_FBQP2, 0);
172 patm_nor_write(sc, IDT_NOR_FBQP3, 0);
174 patm_nor_write(sc, IDT_NOR_FBQWP0, 0);
175 patm_nor_write(sc, IDT_NOR_FBQWP1, 0);
176 patm_nor_write(sc, IDT_NOR_FBQWP2, 0);
177 patm_nor_write(sc, IDT_NOR_FBQWP3, 0);
179 patm_nor_write(sc, IDT_NOR_FBQS0,
180 (SMBUF_THRESHOLD << 28) |
181 (SMBUF_NI_THRESH << 24) |
182 (SMBUF_CI_THRESH << 20) |
184 patm_nor_write(sc, IDT_NOR_FBQS1,
185 (LMBUF_THRESHOLD << 28) |
186 (LMBUF_NI_THRESH << 24) |
187 (LMBUF_CI_THRESH << 20) |
189 patm_nor_write(sc, IDT_NOR_FBQS2,
190 (VMBUF_THRESHOLD << 28) | VMBUF_CELLS);
191 patm_nor_write(sc, IDT_NOR_FBQS3, 0);
193 /* make SCD0 for UBR0 */
194 if ((sc->scd0 = patm_scd_alloc(sc)) == NULL) {
195 patm_printf(sc, "cannot create UBR0 SCD\n");
199 sc->scd0->q.ifq_maxlen = PATM_DLFT_MAXQ;
201 patm_scd_setup(sc, sc->scd0);
202 patm_tct_setup(sc, sc->scd0, NULL);
204 patm_debug(sc, ATTACH, "go...");
206 sc->utopia.flags &= ~UTP_FL_POLL_CARRIER;
207 sc->ifp->if_drv_flags |= IFF_DRV_RUNNING;
209 /* enable interrupts, Tx and Rx paths */
210 cfg |= IDT_CFG_RXPTH | IDT_CFG_RXIIMM | IDT_CFG_RAWIE | IDT_CFG_RQFIE |
211 IDT_CFG_TIMOIE | IDT_CFG_FBIE | IDT_CFG_TXENB | IDT_CFG_TXINT |
212 IDT_CFG_TXUIE | IDT_CFG_TXSFI | IDT_CFG_PHYIE;
213 patm_nor_write(sc, IDT_NOR_CFG, cfg);
215 for (i = 0; i < sc->mmap->max_conn; i++)
216 if (sc->vccs[i] != NULL)
217 patm_load_vc(sc, sc->vccs[i], 1);
219 ATMEV_SEND_IFSTATE_CHANGED(IFP2IFATM(sc->ifp),
220 sc->utopia.carrier == UTP_CARR_OK);
224 * External callable start function
229 struct patm_softc *sc = p;
234 mtx_unlock(&sc->mtx);
241 patm_stop(struct patm_softc *sc)
245 struct patm_txmap *map;
246 struct patm_scd *scd;
248 sc->ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
249 sc->utopia.flags |= UTP_FL_POLL_CARRIER;
253 mtx_lock(&sc->tst_lock);
256 callout_stop(&sc->tst_callout);
257 mtx_unlock(&sc->tst_lock);
260 /* this means we are just entering or leaving the timeout.
261 * wait a little bit. Doing this correctly would be more
267 * Give any waiters on closing a VCC a chance. They will stop
268 * to wait if they see that IFF_DRV_RUNNING disappeared.
270 cv_broadcast(&sc->vcc_cv);
272 /* free large buffers */
273 patm_debug(sc, ATTACH, "freeing large buffers...");
274 for (i = 0; i < sc->lbuf_max; i++)
275 if (sc->lbufs[i].m != NULL)
276 patm_lbuf_free(sc, &sc->lbufs[i]);
278 /* free small buffers that are on the card */
279 patm_debug(sc, ATTACH, "freeing small buffers...");
280 mbp_card_free(sc->sbuf_pool);
282 /* free aal0 buffers that are on the card */
283 patm_debug(sc, ATTACH, "freeing aal0 buffers...");
284 mbp_card_free(sc->vbuf_pool);
286 /* freeing partial receive chains and reset vcc state */
287 for (i = 0; i < sc->mmap->max_conn; i++) {
288 if (sc->vccs[i] != NULL) {
289 if (sc->vccs[i]->chain != NULL) {
290 m_freem(sc->vccs[i]->chain);
291 sc->vccs[i]->chain = NULL;
292 sc->vccs[i]->last = NULL;
295 if (sc->vccs[i]->vflags & (PATM_VCC_RX_CLOSING |
296 PATM_VCC_TX_CLOSING)) {
297 uma_zfree(sc->vcc_zone, sc->vccs[i]);
301 sc->vccs[i]->vflags &= ~PATM_VCC_OPEN;
302 sc->vccs[i]->cps = 0;
303 sc->vccs[i]->scd = NULL;
308 /* stop all active SCDs */
309 while ((scd = LIST_FIRST(&sc->scd_list)) != NULL) {
310 /* free queue packets */
312 _IF_DEQUEUE(&scd->q, m);
318 /* free transmitting packets */
319 for (i = 0; i < IDT_TSQE_TAG_SPACE; i++) {
320 if ((m = scd->on_card[i]) != NULL) {
322 map = m->m_pkthdr.PH_loc.ptr;
324 bus_dmamap_unload(sc->tx_tag, map->map);
325 SLIST_INSERT_HEAD(&sc->tx_maps_free, map, link);
329 patm_scd_free(sc, scd);
333 sc->flags &= ~PATM_CLR;
335 /* reset raw cell queue */
338 ATMEV_SEND_IFSTATE_CHANGED(IFP2IFATM(sc->ifp),
339 sc->utopia.carrier == UTP_CARR_OK);
343 * Stop the card and reset it
346 patm_reset(struct patm_softc *sc)
349 patm_debug(sc, ATTACH, "resetting...");
351 patm_nor_write(sc, IDT_NOR_CFG, IDT_CFG_SWRST);
353 patm_nor_write(sc, IDT_NOR_CFG, 0);
356 patm_nor_write(sc, IDT_NOR_RSQH, 0);
357 patm_nor_write(sc, IDT_NOR_TSQH, 0);
359 patm_nor_write(sc, IDT_NOR_GP, IDT_GP_PHY_RST);
361 patm_nor_write(sc, IDT_NOR_GP, IDT_GP_EEDO | IDT_GP_EECS);
366 * Initialize the soft TST to contain only ABR scheduling and
370 patm_tst_init(struct patm_softc *sc)
375 base = sc->mmap->tst1base;
376 idle = sc->mmap->tst1base + sc->mmap->tst_size;
379 for (i = 0; i < sc->mmap->tst_size - 1; i++)
380 sc->tst_soft[i] = IDT_TST_VBR;
383 sc->tst_jump[0] = base + sc->mmap->tst_size - 1;
384 sc->tst_jump[1] = idle + sc->mmap->tst_size - 1;
385 sc->tst_base[0] = base;
386 sc->tst_base[1] = idle;
389 for (i = 0; i < sc->mmap->tst_size - 1; i++)
390 patm_sram_write(sc, base + i, IDT_TST_VBR);
391 patm_sram_write(sc, sc->tst_jump[0], IDT_TST_BR | (base << 2));
394 for (i = 0; i < sc->mmap->tst_size - 1; i++)
395 patm_sram_write(sc, idle + i, IDT_TST_VBR);
396 patm_sram_write(sc, sc->tst_jump[1], IDT_TST_BR | (idle << 2));
398 sc->tst_free = sc->mmap->tst_size - 1;
399 sc->tst_reserve = sc->tst_free * PATM_TST_RESERVE / 100;
400 sc->bwrem = IFP2IFATM(sc->ifp)->mib.pcr;
404 * Initialize the SCDs. This is done by building a list of all free
405 * SCDs in SRAM. The first word of each potential SCD is used as a
406 * link to the next free SCD. The list is rooted in softc.
409 patm_scd_init(struct patm_softc *sc)
411 u_int s; /* SRAM address of current SCD */
414 for (s = sc->mmap->scd_base; s + 12 <= sc->mmap->tst1base; s += 12) {
415 patm_sram_write(sc, s, sc->scd_free);
424 patm_scd_alloc(struct patm_softc *sc)
426 u_int sram, next; /* SRAM address of this and next SCD */
429 struct patm_scd *scd;
433 /* get an SCD from the free list */
434 if ((sram = sc->scd_free) == 0)
436 next = patm_sram_read(sc, sram);
438 /* allocate memory for the queue and our host stuff */
439 error = bus_dmamem_alloc(sc->scd_tag, &p, BUS_DMA_NOWAIT, &map);
443 error = bus_dmamap_load(sc->scd_tag, map, p, sizeof(scd->scq),
444 patm_load_callback, &phy, BUS_DMA_NOWAIT);
446 bus_dmamem_free(sc->scd_tag, p, map);
449 KASSERT((phy & 0x1ff) == 0, ("SCD not aligned %lx", (u_long)phy));
452 bzero(scd, sizeof(*scd));
457 scd->space = IDT_SCQ_SIZE;
458 scd->last_tag = IDT_TSQE_TAG_SPACE - 1;
459 scd->q.ifq_maxlen = PATM_TX_IFQLEN;
461 /* remove the scd from the free list */
463 LIST_INSERT_HEAD(&sc->scd_list, scd, link);
472 patm_scd_free(struct patm_softc *sc, struct patm_scd *scd)
475 LIST_REMOVE(scd, link);
477 /* clear SCD and insert link word */
478 patm_sram_write4(sc, scd->sram, sc->scd_free, 0, 0, 0);
479 patm_sram_write4(sc, scd->sram, 0, 0, 0, 0);
480 patm_sram_write4(sc, scd->sram, 0, 0, 0, 0);
482 /* put on free list */
483 sc->scd_free = scd->sram;
486 bus_dmamap_unload(sc->scd_tag, scd->map);
487 bus_dmamem_free(sc->scd_tag, scd, scd->map);
491 * DMA loading helper function. This function handles the loading of
492 * all one segment DMA maps. The argument is a pointer to a bus_addr_t
493 * which must contain the desired alignment of the address as a bitmap.
496 patm_load_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
498 bus_addr_t *phy = arg;
504 ("too many segments for DMA: %d", nsegs));
505 KASSERT(segs[0].ds_addr <= 0xffffffffUL,
506 ("phys addr too large %lx", (u_long)segs[0].ds_addr));
507 KASSERT((segs[0].ds_addr & *phy) == 0,
508 ("bad alignment %lx:%lx", (u_long)segs[0].ds_addr, (u_long)*phy));
510 *phy = segs[0].ds_addr;