if_patm.c revision 119418
1/*
2 * Copyright (c) 2003
3 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4 * 	All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 * Author: Hartmut Brandt <harti@freebsd.org>
28 *
29 * Driver for IDT77252 based cards like ProSum's.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/patm/if_patm.c 119418 2003-08-24 17:55:58Z obrien $");
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/dev/patm/if_patm.c 119418 2003-08-24 17:55:58Z obrien $");
36
37#include "opt_inet.h"
38#include "opt_natm.h"
39
40#include <sys/types.h>
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/malloc.h>
44#include <sys/kernel.h>
45#include <sys/bus.h>
46#include <sys/errno.h>
47#include <sys/conf.h>
48#include <sys/module.h>
49#include <sys/lock.h>
50#include <sys/mutex.h>
51#include <sys/sysctl.h>
52#include <sys/queue.h>
53#include <sys/condvar.h>
54#include <sys/endian.h>
55#include <vm/uma.h>
56
57#include <sys/sockio.h>
58#include <sys/mbuf.h>
59#include <sys/socket.h>
60
61#include <net/if.h>
62#include <net/if_media.h>
63#include <net/if_atm.h>
64#include <net/route.h>
65#include <netinet/in.h>
66#include <netinet/if_atm.h>
67
68#include <machine/bus.h>
69#include <machine/resource.h>
70#include <sys/bus.h>
71#include <sys/rman.h>
72#include <sys/mbpool.h>
73
74#include <dev/utopia/utopia.h>
75#include <dev/patm/idt77252reg.h>
76#include <dev/patm/if_patmvar.h>
77
78static void patm_tst_init(struct patm_softc *sc);
79static void patm_scd_init(struct patm_softc *sc);
80
81/*
82 * Start the card. This assumes the mutex to be held
83 */
84void
85patm_initialize(struct patm_softc *sc)
86{
87	uint32_t cfg;
88	u_int i;
89
90	patm_debug(sc, ATTACH, "configuring...");
91
92	/* clear SRAM */
93	for (i = 0; i < sc->mmap->sram * 1024; i += 4)
94		patm_sram_write4(sc, i, 0, 0, 0, 0);
95	patm_scd_init(sc);
96
97	/* configuration register. Setting NOIDLE makes the timing wrong! */
98	cfg = IDT_CFG_TXFIFO9 | IDT_CFG_RXQ512 | PATM_CFG_VPI |
99	    /* IDT_CFG_NOIDLE | */ sc->mmap->rxtab;
100	if (!(sc->flags & PATM_UNASS))
101		cfg |= IDT_CFG_IDLECLP;
102	patm_nor_write(sc, IDT_NOR_CFG, cfg);
103
104	/* clean all the status queues and the Raw handle */
105	memset(sc->tsq, 0, sc->sq_size);
106
107	/* initialize RSQ */
108	patm_debug(sc, ATTACH, "RSQ %llx", (unsigned long long)sc->rsq_phy);
109	patm_nor_write(sc, IDT_NOR_RSQB, sc->rsq_phy);
110	patm_nor_write(sc, IDT_NOR_RSQT, sc->rsq_phy);
111	patm_nor_write(sc, IDT_NOR_RSQH, 0);
112	sc->rsq_last = PATM_RSQ_SIZE - 1;
113
114	/* initialize TSTB */
115	patm_nor_write(sc, IDT_NOR_TSTB, sc->mmap->tst1base << 2);
116	patm_tst_init(sc);
117
118	/* initialize TSQ */
119	for (i = 0; i < IDT_TSQ_SIZE; i++)
120		sc->tsq[i].stamp = htole32(IDT_TSQE_EMPTY);
121	patm_nor_write(sc, IDT_NOR_TSQB, sc->tsq_phy);
122	patm_nor_write(sc, IDT_NOR_TSQH, 0);
123	patm_nor_write(sc, IDT_NOR_TSQT, 0);
124	sc->tsq_next = sc->tsq;
125
126	/* GP */
127#if BYTE_ORDER == BIG_ENDIAN && 0
128	patm_nor_write(sc, IDT_NOR_GP, IDT_GP_BIGE);
129#else
130	patm_nor_write(sc, IDT_NOR_GP, 0);
131#endif
132
133	/* VPM */
134	patm_nor_write(sc, IDT_NOR_VPM, 0);
135
136	/* RxFIFO */
137	patm_nor_write(sc, IDT_NOR_RXFD,
138	    IDT_RXFD(sc->mmap->rxfifo_addr, sc->mmap->rxfifo_code));
139	patm_nor_write(sc, IDT_NOR_RXFT, 0);
140	patm_nor_write(sc, IDT_NOR_RXFH, 0);
141
142	/* RAWHND */
143	patm_debug(sc, ATTACH, "RWH %llx",
144	    (unsigned long long)sc->rawhnd_phy);
145	patm_nor_write(sc, IDT_NOR_RAWHND, sc->rawhnd_phy);
146
147	/* ABRSTD */
148	patm_nor_write(sc, IDT_NOR_ABRSTD,
149	    IDT_ABRSTD(sc->mmap->abrstd_addr, sc->mmap->abrstd_code));
150	for (i = 0; i < sc->mmap->abrstd_size; i++)
151		patm_sram_write(sc, sc->mmap->abrstd_addr + i, 0);
152	patm_nor_write(sc, IDT_NOR_ABRRQ, 0);
153	patm_nor_write(sc, IDT_NOR_VBRRQ, 0);
154
155	/* rate tables */
156	if (sc->flags & PATM_25M) {
157		for (i = 0; i < patm_rtables_size; i++)
158			patm_sram_write(sc, sc->mmap->rtables + i,
159			    patm_rtables25[i]);
160	} else {
161		for (i = 0; i < patm_rtables_size; i++)
162			patm_sram_write(sc, sc->mmap->rtables + i,
163			    patm_rtables155[i]);
164	}
165	patm_nor_write(sc, IDT_NOR_RTBL, sc->mmap->rtables << 2);
166
167	/* Maximum deficit */
168	patm_nor_write(sc, IDT_NOR_MXDFCT, 32 | IDT_MDFCT_LCI | IDT_MDFCT_LNI);
169
170	/* Free buffer queues */
171	patm_nor_write(sc, IDT_NOR_FBQP0, 0);
172	patm_nor_write(sc, IDT_NOR_FBQP1, 0);
173	patm_nor_write(sc, IDT_NOR_FBQP2, 0);
174	patm_nor_write(sc, IDT_NOR_FBQP3, 0);
175
176	patm_nor_write(sc, IDT_NOR_FBQWP0, 0);
177	patm_nor_write(sc, IDT_NOR_FBQWP1, 0);
178	patm_nor_write(sc, IDT_NOR_FBQWP2, 0);
179	patm_nor_write(sc, IDT_NOR_FBQWP3, 0);
180
181	patm_nor_write(sc, IDT_NOR_FBQS0,
182	    (SMBUF_THRESHOLD << 28) |
183	    (SMBUF_NI_THRESH << 24) |
184	    (SMBUF_CI_THRESH << 20) |
185	    SMBUF_CELLS);
186	patm_nor_write(sc, IDT_NOR_FBQS1,
187	    (LMBUF_THRESHOLD << 28) |
188	    (LMBUF_NI_THRESH << 24) |
189	    (LMBUF_CI_THRESH << 20) |
190	    LMBUF_CELLS);
191	patm_nor_write(sc, IDT_NOR_FBQS2,
192	    (VMBUF_THRESHOLD << 28) | VMBUF_CELLS);
193	patm_nor_write(sc, IDT_NOR_FBQS3, 0);
194
195	/* make SCD0 for UBR0 */
196	if ((sc->scd0 = patm_scd_alloc(sc)) == NULL) {
197		patm_printf(sc, "cannot create UBR0 SCD\n");
198		patm_reset(sc);
199		return;
200	}
201	sc->scd0->q.ifq_maxlen = PATM_DLFT_MAXQ;
202
203	patm_scd_setup(sc, sc->scd0);
204	patm_tct_setup(sc, sc->scd0, NULL);
205
206	patm_debug(sc, ATTACH, "go...");
207
208	sc->utopia.flags &= ~UTP_FL_POLL_CARRIER;
209	sc->ifatm.ifnet.if_flags |= IFF_RUNNING;
210
211	/* enable interrupts, Tx and Rx paths */
212	cfg |= IDT_CFG_RXPTH | IDT_CFG_RXIIMM | IDT_CFG_RAWIE | IDT_CFG_RQFIE |
213	    IDT_CFG_TIMOIE | IDT_CFG_FBIE | IDT_CFG_TXENB | IDT_CFG_TXINT |
214	    IDT_CFG_TXUIE | IDT_CFG_TXSFI | IDT_CFG_PHYIE;
215	patm_nor_write(sc, IDT_NOR_CFG, cfg);
216
217	for (i = 0; i < sc->mmap->max_conn; i++)
218		if (sc->vccs[i] != NULL)
219			patm_load_vc(sc, sc->vccs[i], 1);
220
221	ATMEV_SEND_IFSTATE_CHANGED(&sc->ifatm,
222	    sc->utopia.carrier == UTP_CARR_OK);
223}
224
225/*
226 * External callable start function
227 */
228void
229patm_init(void *p)
230{
231	struct patm_softc *sc = p;
232
233	mtx_lock(&sc->mtx);
234	patm_stop(sc);
235	patm_initialize(sc);
236	mtx_unlock(&sc->mtx);
237}
238
239/*
240 * Stop the interface
241 */
242void
243patm_stop(struct patm_softc *sc)
244{
245	u_int i;
246	struct mbuf *m;
247	struct patm_txmap *map;
248	struct patm_scd *scd;
249
250	sc->ifatm.ifnet.if_flags &= ~IFF_RUNNING;
251	sc->utopia.flags |= UTP_FL_POLL_CARRIER;
252
253	patm_reset(sc);
254
255	mtx_lock(&sc->tst_lock);
256	i = sc->tst_state;
257	sc->tst_state = 0;
258	callout_stop(&sc->tst_callout);
259	mtx_unlock(&sc->tst_lock);
260
261	if (i != 0) {
262		/* this means we are just entering or leaving the timeout.
263		 * wait a little bit. Doing this correctly would be more
264		 * involved */
265		DELAY(1000);
266	}
267
268	/*
269	 * Give any waiters on closing a VCC a chance. They will stop
270	 * to wait if they see that IFF_RUNNING disappeared.
271	 */
272	while (!(cv_waitq_empty(&sc->vcc_cv))) {
273		cv_broadcast(&sc->vcc_cv);
274		DELAY(100);
275	}
276
277	/* free large buffers */
278	patm_debug(sc, ATTACH, "freeing large buffers...");
279	for (i = 0; i < sc->lbuf_max; i++)
280		if (sc->lbufs[i].m != NULL)
281			patm_lbuf_free(sc, &sc->lbufs[i]);
282
283	/* free small buffers that are on the card */
284	patm_debug(sc, ATTACH, "freeing small buffers...");
285	mbp_card_free(sc->sbuf_pool);
286
287	/* free aal0 buffers that are on the card */
288	patm_debug(sc, ATTACH, "freeing aal0 buffers...");
289	mbp_card_free(sc->vbuf_pool);
290
291	/* freeing partial receive chains and reset vcc state */
292	for (i = 0; i < sc->mmap->max_conn; i++) {
293		if (sc->vccs[i] != NULL) {
294			if (sc->vccs[i]->chain != NULL) {
295				m_freem(sc->vccs[i]->chain);
296				sc->vccs[i]->chain = NULL;
297				sc->vccs[i]->last = NULL;
298			}
299
300			if (sc->vccs[i]->vflags & (PATM_VCC_RX_CLOSING |
301			    PATM_VCC_TX_CLOSING)) {
302				uma_zfree(sc->vcc_zone, sc->vccs[i]);
303				sc->vccs[i] = NULL;
304			} else {
305				/* keep */
306				sc->vccs[i]->vflags &= ~PATM_VCC_OPEN;
307				sc->vccs[i]->cps = 0;
308				sc->vccs[i]->scd = NULL;
309			}
310		}
311	}
312
313	/* stop all active SCDs */
314	while ((scd = LIST_FIRST(&sc->scd_list)) != NULL) {
315		/* free queue packets */
316		for (;;) {
317			_IF_DEQUEUE(&scd->q, m);
318			if (m == NULL)
319				break;
320			m_freem(m);
321		}
322
323		/* free transmitting packets */
324		for (i = 0; i < IDT_TSQE_TAG_SPACE; i++) {
325			if ((m = scd->on_card[i]) != NULL) {
326				scd->on_card[i] = 0;
327				map = m->m_pkthdr.header;
328
329				bus_dmamap_unload(sc->tx_tag, map->map);
330				SLIST_INSERT_HEAD(&sc->tx_maps_free, map, link);
331				m_freem(m);
332			}
333		}
334		patm_scd_free(sc, scd);
335	}
336	sc->scd0 = NULL;
337
338	sc->flags &= ~PATM_CLR;
339
340	/* reset raw cell queue */
341	sc->rawh = NULL;
342
343	ATMEV_SEND_IFSTATE_CHANGED(&sc->ifatm,
344	    sc->utopia.carrier == UTP_CARR_OK);
345}
346
347/*
348 * Stop the card and reset it
349 */
350void
351patm_reset(struct patm_softc *sc)
352{
353
354	patm_debug(sc, ATTACH, "resetting...");
355
356	patm_nor_write(sc, IDT_NOR_CFG, IDT_CFG_SWRST);
357	DELAY(200);
358	patm_nor_write(sc, IDT_NOR_CFG, 0);
359	DELAY(200);
360
361	patm_nor_write(sc, IDT_NOR_RSQH, 0);
362	patm_nor_write(sc, IDT_NOR_TSQH, 0);
363
364	patm_nor_write(sc, IDT_NOR_GP, IDT_GP_PHY_RST);
365	DELAY(50);
366	patm_nor_write(sc, IDT_NOR_GP, IDT_GP_EEDO | IDT_GP_EECS);
367	DELAY(50);
368}
369
370/*
371 * Initialize the soft TST to contain only ABR scheduling and
372 * write it to SRAM
373 */
374static void
375patm_tst_init(struct patm_softc *sc)
376{
377	u_int i;
378	u_int base, idle;
379
380	base = sc->mmap->tst1base;
381	idle = sc->mmap->tst1base + sc->mmap->tst_size;
382
383	/* soft */
384	for (i = 0; i < sc->mmap->tst_size - 1; i++)
385		sc->tst_soft[i] = IDT_TST_VBR;
386
387	sc->tst_state = 0;
388	sc->tst_jump[0] = base + sc->mmap->tst_size - 1;
389	sc->tst_jump[1] = idle + sc->mmap->tst_size - 1;
390	sc->tst_base[0] = base;
391	sc->tst_base[1] = idle;
392
393	/* TST1 */
394	for (i = 0; i < sc->mmap->tst_size - 1; i++)
395		patm_sram_write(sc, base + i, IDT_TST_VBR);
396	patm_sram_write(sc, sc->tst_jump[0], IDT_TST_BR | (base << 2));
397
398	/* TST2 */
399	for (i = 0; i < sc->mmap->tst_size - 1; i++)
400		patm_sram_write(sc, idle + i, IDT_TST_VBR);
401	patm_sram_write(sc, sc->tst_jump[1], IDT_TST_BR | (idle << 2));
402
403	sc->tst_free = sc->mmap->tst_size - 1;
404	sc->tst_reserve = sc->tst_free * PATM_TST_RESERVE / 100;
405	sc->bwrem = sc->ifatm.mib.pcr;
406}
407
408/*
409 * Initialize the SCDs. This is done by building a list of all free
410 * SCDs in SRAM. The first word of each potential SCD is used as a
411 * link to the next free SCD. The list is rooted in softc.
412 */
413static void
414patm_scd_init(struct patm_softc *sc)
415{
416	u_int s;	/* SRAM address of current SCD */
417
418	sc->scd_free = 0;
419	for (s = sc->mmap->scd_base; s + 12 <= sc->mmap->tst1base; s += 12) {
420		patm_sram_write(sc, s, sc->scd_free);
421		sc->scd_free = s;
422	}
423}
424
425/*
426 * allocate an SCQ
427 */
428struct patm_scd *
429patm_scd_alloc(struct patm_softc *sc)
430{
431	u_int sram, next;	/* SRAM address of this and next SCD */
432	int error;
433	void *p;
434	struct patm_scd *scd;
435	bus_dmamap_t map;
436	bus_addr_t phy;
437
438	/* get an SCD from the free list */
439	if ((sram = sc->scd_free) == 0)
440		return (NULL);
441	next = patm_sram_read(sc, sram);
442
443	/* allocate memory for the queue and our host stuff */
444	error = bus_dmamem_alloc(sc->scd_tag, &p, BUS_DMA_NOWAIT, &map);
445	if (error != 0)
446		return (NULL);
447	phy = 0x3ff;
448	error = bus_dmamap_load(sc->scd_tag, map, p, sizeof(scd->scq),
449	    patm_load_callback, &phy, BUS_DMA_NOWAIT);
450	if (error != 0) {
451		bus_dmamem_free(sc->scd_tag, p, map);
452		return (NULL);
453	}
454	KASSERT((phy & 0x1ff) == 0, ("SCD not aligned %lx", (u_long)phy));
455
456	scd = p;
457	bzero(scd, sizeof(*scd));
458
459	scd->sram = sram;
460	scd->phy = phy;
461	scd->map = map;
462	scd->space = IDT_SCQ_SIZE;
463	scd->last_tag = IDT_TSQE_TAG_SPACE - 1;
464	scd->q.ifq_maxlen = PATM_TX_IFQLEN;
465
466	/* remove the scd from the free list */
467	sc->scd_free = next;
468	LIST_INSERT_HEAD(&sc->scd_list, scd, link);
469
470	return (scd);
471}
472
473/*
474 * Free an SCD
475 */
476void
477patm_scd_free(struct patm_softc *sc, struct patm_scd *scd)
478{
479
480	LIST_REMOVE(scd, link);
481
482	/* clear SCD and insert link word */
483	patm_sram_write4(sc, scd->sram, sc->scd_free, 0, 0, 0);
484	patm_sram_write4(sc, scd->sram, 0, 0, 0, 0);
485	patm_sram_write4(sc, scd->sram, 0, 0, 0, 0);
486
487	/* put on free list */
488	sc->scd_free = scd->sram;
489
490	/* free memory */
491	bus_dmamap_unload(sc->scd_tag, scd->map);
492	bus_dmamem_free(sc->scd_tag, scd, scd->map);
493}
494
495/*
496 * DMA loading helper function. This function handles the loading of
497 * all one segment DMA maps. The argument is a pointer to a bus_addr_t
498 * which must contain the desired alignment of the address as a bitmap.
499 */
500void
501patm_load_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
502{
503	bus_addr_t *phy = arg;
504
505	if (error)
506		return;
507
508	KASSERT(nsegs == 1,
509	    ("too many segments for DMA: %d", nsegs));
510	KASSERT(segs[0].ds_addr <= 0xffffffffUL,
511	    ("phys addr too large %lx", (u_long)segs[0].ds_addr));
512	KASSERT((segs[0].ds_addr & *phy) == 0,
513	    ("bad alignment %lx:%lx",  (u_long)segs[0].ds_addr, (u_long)*phy));
514
515	*phy = segs[0].ds_addr;
516}
517