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>
54 #include <sys/sockio.h>
56 #include <sys/socket.h>
59 #include <net/if_var.h>
60 #include <net/if_media.h>
61 #include <net/if_types.h>
62 #include <net/if_atm.h>
63 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/if_atm.h>
70 #include <machine/bus.h>
71 #include <machine/resource.h>
74 #include <sys/mbpool.h>
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcivar.h>
78 #include <dev/utopia/utopia.h>
79 #include <dev/patm/idt77252reg.h>
80 #include <dev/patm/if_patmvar.h>
82 MODULE_DEPEND(patm, utopia, 1, 1, 1);
83 MODULE_DEPEND(patm, pci, 1, 1, 1);
84 MODULE_DEPEND(patm, atm, 1, 1, 1);
85 MODULE_DEPEND(patm, libmbpool, 1, 1, 1);
87 devclass_t patm_devclass;
89 static int patm_probe(device_t dev);
90 static int patm_attach(device_t dev);
91 static int patm_detach(device_t dev);
92 static device_method_t patm_methods[] = {
93 DEVMETHOD(device_probe, patm_probe),
94 DEVMETHOD(device_attach, patm_attach),
95 DEVMETHOD(device_detach, patm_detach),
98 static driver_t patm_driver = {
101 sizeof(struct patm_softc),
103 DRIVER_MODULE(patm, pci, patm_driver, patm_devclass, NULL, 0);
105 static const struct {
109 { PCI_DEVICE_IDT77252, "NICStAR (77222/77252) ATM adapter" },
110 { PCI_DEVICE_IDT77v252, "NICStAR (77v252) ATM adapter" },
111 { PCI_DEVICE_IDT77v222, "NICStAR (77v222) ATM adapter" },
115 SYSCTL_DECL(_hw_atm);
117 static int patm_phy_readregs(struct ifatm *, u_int, uint8_t *, u_int *);
118 static int patm_phy_writereg(struct ifatm *, u_int, u_int, u_int);
119 static const struct utopia_methods patm_utopia_methods = {
124 static void patm_destroy(struct patm_softc *sc);
126 static int patm_sysctl_istats(SYSCTL_HANDLER_ARGS);
127 static int patm_sysctl_eeprom(SYSCTL_HANDLER_ARGS);
129 static void patm_read_eeprom(struct patm_softc *sc);
130 static int patm_sq_init(struct patm_softc *sc);
131 static int patm_rbuf_init(struct patm_softc *sc);
132 static int patm_txmap_init(struct patm_softc *sc);
134 static void patm_env_getuint(struct patm_softc *, u_int *, const char *);
137 static int patm_sysctl_regs(SYSCTL_HANDLER_ARGS);
138 static int patm_sysctl_tsq(SYSCTL_HANDLER_ARGS);
139 int patm_dump_vc(u_int unit, u_int vc) __unused;
140 int patm_dump_regs(u_int unit) __unused;
141 int patm_dump_sram(u_int unit, u_int from, u_int words) __unused;
145 * Probe for a IDT77252 controller
148 patm_probe(device_t dev)
152 if (pci_get_vendor(dev) == PCI_VENDOR_IDT) {
153 for (i = 0; devs[i].desc != NULL; i++)
154 if (pci_get_device(dev) == devs[i].devid) {
155 device_set_desc(dev, devs[i].desc);
156 return (BUS_PROBE_DEFAULT);
166 patm_attach(device_t dev)
168 struct patm_softc *sc;
174 static const struct idt_mmap idt_mmap[4] = IDT_MMAP;
176 sc = device_get_softc(dev);
180 sc->debug = IATM_DEBUG;
182 ifp = sc->ifp = if_alloc(IFT_ATM);
187 IFP2IFATM(sc->ifp)->mib.device = ATM_DEVICE_IDTABR25;
188 IFP2IFATM(sc->ifp)->mib.serial = 0;
189 IFP2IFATM(sc->ifp)->mib.hw_version = 0;
190 IFP2IFATM(sc->ifp)->mib.sw_version = 0;
191 IFP2IFATM(sc->ifp)->mib.vpi_bits = PATM_VPI_BITS;
192 IFP2IFATM(sc->ifp)->mib.vci_bits = 0; /* set below */;
193 IFP2IFATM(sc->ifp)->mib.max_vpcs = 0;
194 IFP2IFATM(sc->ifp)->mib.max_vccs = 0; /* set below */
195 IFP2IFATM(sc->ifp)->mib.media = IFM_ATM_UNKNOWN;
196 IFP2IFATM(sc->ifp)->phy = &sc->utopia;
199 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
200 ifp->if_flags = IFF_SIMPLEX;
201 ifp->if_init = patm_init;
202 ifp->if_ioctl = patm_ioctl;
203 ifp->if_start = patm_start;
205 /* do this early so we can destroy unconditionally */
206 mtx_init(&sc->mtx, device_get_nameunit(dev),
207 MTX_NETWORK_LOCK, MTX_DEF);
208 mtx_init(&sc->tst_lock, "tst lock", NULL, MTX_DEF);
209 cv_init(&sc->vcc_cv, "vcc_close");
211 callout_init(&sc->tst_callout, 1);
213 sysctl_ctx_init(&sc->sysctl_ctx);
218 sc->revision = pci_read_config(dev, PCIR_REVID, 4) & 0xf;
221 * Enable PCI bus master and memory
223 pci_enable_busmaster(dev);
225 rid = IDT_PCI_REG_MEMBASE;
226 sc->memres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
228 if (sc->memres == NULL) {
229 patm_printf(sc, "could not map memory\n");
233 sc->memh = rman_get_bushandle(sc->memres);
234 sc->memt = rman_get_bustag(sc->memres);
237 * Allocate the interrupt (enable it later)
240 sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqid,
241 RF_SHAREABLE | RF_ACTIVE);
242 if (sc->irqres == 0) {
243 patm_printf(sc, "could not allocate irq\n");
249 * Construct the sysctl tree
252 if ((sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
253 SYSCTL_STATIC_CHILDREN(_hw_atm), OID_AUTO,
254 device_get_nameunit(dev), CTLFLAG_RD, 0, "")) == NULL)
257 if (SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
258 OID_AUTO, "istats", CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0,
259 patm_sysctl_istats, "S", "internal statistics") == NULL)
262 if (SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
263 OID_AUTO, "eeprom", CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0,
264 patm_sysctl_eeprom, "S", "EEPROM contents") == NULL)
267 if (SYSCTL_ADD_UINT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
268 OID_AUTO, "lbuf_max", CTLFLAG_RD, &sc->lbuf_max,
269 0, "maximum number of large receive buffers") == NULL)
271 patm_env_getuint(sc, &sc->lbuf_max, "lbuf_max");
273 if (SYSCTL_ADD_UINT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
274 OID_AUTO, "max_txmaps", CTLFLAG_RW, &sc->tx_maxmaps,
275 0, "maximum number of TX DMA maps") == NULL)
277 patm_env_getuint(sc, &sc->tx_maxmaps, "tx_maxmaps");
280 if (SYSCTL_ADD_UINT(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
281 OID_AUTO, "debug", CTLFLAG_RW, &sc->debug,
282 0, "debug flags") == NULL)
284 sc->debug = PATM_DEBUG;
285 patm_env_getuint(sc, &sc->debug, "debug");
287 if (SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
288 OID_AUTO, "regs", CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0,
289 patm_sysctl_regs, "S", "registers") == NULL)
292 if (SYSCTL_ADD_PROC(&sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
293 OID_AUTO, "tsq", CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0,
294 patm_sysctl_tsq, "S", "TSQ") == NULL)
301 * Detect and attach the phy.
303 patm_debug(sc, ATTACH, "attaching utopia");
304 IFP2IFATM(sc->ifp)->phy = &sc->utopia;
305 utopia_attach(&sc->utopia, IFP2IFATM(sc->ifp), &sc->media, &sc->mtx,
306 &sc->sysctl_ctx, SYSCTL_CHILDREN(sc->sysctl_tree),
307 &patm_utopia_methods);
310 * Start the PHY because we need the autodetection
312 patm_debug(sc, ATTACH, "starting utopia");
314 utopia_start(&sc->utopia);
315 utopia_reset(&sc->utopia);
316 mtx_unlock(&sc->mtx);
319 patm_read_eeprom(sc);
322 if (strncmp(sc->eeprom + PATM_PROATM_NAME_OFFSET, PATM_PROATM_NAME,
323 strlen(PATM_PROATM_NAME)) == 0) {
324 if (sc->utopia.chip->type == UTP_TYPE_IDT77105) {
325 IFP2IFATM(sc->ifp)->mib.device = ATM_DEVICE_PROATM25;
326 IFP2IFATM(sc->ifp)->mib.pcr = ATM_RATE_25_6M;
327 IFP2IFATM(sc->ifp)->mib.media = IFM_ATM_UTP_25;
328 sc->flags |= PATM_25M;
329 patm_printf(sc, "ProATM 25 interface; ");
332 /* cannot really know which media */
333 IFP2IFATM(sc->ifp)->mib.device = ATM_DEVICE_PROATM155;
334 IFP2IFATM(sc->ifp)->mib.pcr = ATM_RATE_155M;
335 IFP2IFATM(sc->ifp)->mib.media = IFM_ATM_MM_155;
336 patm_printf(sc, "ProATM 155 interface; ");
339 bcopy(sc->eeprom + PATM_PROATM_MAC_OFFSET, IFP2IFATM(sc->ifp)->mib.esi,
340 sizeof(IFP2IFATM(sc->ifp)->mib.esi));
343 if (sc->utopia.chip->type == UTP_TYPE_IDT77105) {
344 IFP2IFATM(sc->ifp)->mib.device = ATM_DEVICE_IDTABR25;
345 IFP2IFATM(sc->ifp)->mib.pcr = ATM_RATE_25_6M;
346 IFP2IFATM(sc->ifp)->mib.media = IFM_ATM_UTP_25;
347 sc->flags |= PATM_25M;
348 patm_printf(sc, "IDT77252 25MBit interface; ");
351 /* cannot really know which media */
352 IFP2IFATM(sc->ifp)->mib.device = ATM_DEVICE_IDTABR155;
353 IFP2IFATM(sc->ifp)->mib.pcr = ATM_RATE_155M;
354 IFP2IFATM(sc->ifp)->mib.media = IFM_ATM_MM_155;
355 patm_printf(sc, "IDT77252 155MBit interface; ");
358 bcopy(sc->eeprom + PATM_IDT_MAC_OFFSET, IFP2IFATM(sc->ifp)->mib.esi,
359 sizeof(IFP2IFATM(sc->ifp)->mib.esi));
361 printf("idt77252 Rev. %c; %s PHY\n", 'A' + sc->revision,
362 sc->utopia.chip->name);
364 utopia_reset_media(&sc->utopia);
365 utopia_init_media(&sc->utopia);
370 for (a = 0; a < 0x20000; a++)
371 patm_sram_write(sc, a, 0);
372 patm_sram_write(sc, 0, 0xdeadbeef);
373 if (patm_sram_read(sc, 0x4004) == 0xdeadbeef)
374 sc->mmap = &idt_mmap[0];
375 else if (patm_sram_read(sc, 0x8000) == 0xdeadbeef)
376 sc->mmap = &idt_mmap[1];
377 else if (patm_sram_read(sc, 0x20000) == 0xdeadbeef)
378 sc->mmap = &idt_mmap[2];
380 sc->mmap = &idt_mmap[3];
382 IFP2IFATM(sc->ifp)->mib.vci_bits = sc->mmap->vcbits - IFP2IFATM(sc->ifp)->mib.vpi_bits;
383 IFP2IFATM(sc->ifp)->mib.max_vccs = sc->mmap->max_conn;
384 patm_sram_write(sc, 0, 0);
385 patm_printf(sc, "%uK x 32 SRAM; %u connections\n", sc->mmap->sram,
388 /* initialize status queues */
389 error = patm_sq_init(sc);
394 sc->tst_soft = malloc(sizeof(uint32_t) * sc->mmap->tst_size,
397 /* allocate all the receive buffer stuff */
398 error = patm_rbuf_init(sc);
405 * Don't use BUS_DMA_ALLOCNOW, because we never need bouncing with
408 error = bus_dma_tag_create(bus_get_dma_tag(dev), PAGE_SIZE, 0,
409 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
410 NULL, NULL, sizeof(struct patm_scd), 1,
411 sizeof(struct patm_scd), 0, NULL, NULL, &sc->scd_tag);
413 patm_printf(sc, "SCD DMA tag create %d\n", error);
416 LIST_INIT(&sc->scd_list);
418 /* allocate VCC zone and pointers */
419 if ((sc->vcc_zone = uma_zcreate("PATM vccs", sizeof(struct patm_vcc),
420 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0)) == NULL) {
421 patm_printf(sc, "cannot allocate zone for vccs\n");
424 sc->vccs = malloc(sizeof(sc->vccs[0]) * sc->mmap->max_conn,
425 M_DEVBUF, M_WAITOK | M_ZERO);
427 /* allocate transmission resources */
428 error = patm_txmap_init(sc);
432 /* poll while we are not running */
433 sc->utopia.flags |= UTP_FL_POLL_CARRIER;
435 patm_debug(sc, ATTACH, "attaching interface");
439 bpfattach(ifp, DLT_ATM_RFC1483, sizeof(struct atmllc));
442 patm_debug(sc, ATTACH, "attaching interrupt handler");
443 error = bus_setup_intr(dev, sc->irqres, INTR_TYPE_NET | INTR_MPSAFE,
444 NULL, patm_intr, sc, &sc->ih);
446 patm_printf(sc, "could not setup interrupt\n");
447 atm_ifdetach(sc->ifp);
463 patm_detach(device_t dev)
465 struct patm_softc *sc;
467 sc = device_get_softc(dev);
471 if (sc->utopia.state & UTP_ST_ATTACHED) {
472 patm_debug(sc, ATTACH, "detaching utopia");
473 utopia_stop(&sc->utopia);
474 utopia_detach(&sc->utopia);
476 mtx_unlock(&sc->mtx);
478 atm_ifdetach(sc->ifp);
486 * Destroy everything. Assume we are stopped.
489 patm_destroy(struct patm_softc *sc)
492 struct patm_txmap *map;
495 bus_teardown_intr(sc->dev, sc->irqres, sc->ih);
497 if (sc->tx_mapzone != NULL) {
498 /* all maps must be free */
499 while ((map = SLIST_FIRST(&sc->tx_maps_free)) != NULL) {
500 bus_dmamap_destroy(sc->tx_tag, map->map);
501 SLIST_REMOVE_HEAD(&sc->tx_maps_free, link);
502 uma_zfree(sc->tx_mapzone, map);
504 uma_zdestroy(sc->tx_mapzone);
507 if (sc->scd_tag != NULL)
508 bus_dma_tag_destroy(sc->scd_tag);
510 if (sc->tx_tag != NULL)
511 bus_dma_tag_destroy(sc->scd_tag);
513 if (sc->vccs != NULL) {
514 for (i = 0; i < sc->mmap->max_conn; i++)
515 if (sc->vccs[i] != NULL)
516 uma_zfree(sc->vcc_zone, sc->vccs[i]);
517 free(sc->vccs, M_DEVBUF);
519 if (sc->vcc_zone != NULL)
520 uma_zdestroy(sc->vcc_zone);
522 if (sc->lbufs != NULL) {
523 for (i = 0; i < sc->lbuf_max; i++)
524 bus_dmamap_destroy(sc->lbuf_tag, sc->lbufs[i].map);
525 free(sc->lbufs, M_DEVBUF);
528 if (sc->lbuf_tag != NULL)
529 bus_dma_tag_destroy(sc->lbuf_tag);
531 if (sc->sbuf_pool != NULL)
532 mbp_destroy(sc->sbuf_pool);
533 if (sc->vbuf_pool != NULL)
534 mbp_destroy(sc->vbuf_pool);
536 if (sc->sbuf_tag != NULL)
537 bus_dma_tag_destroy(sc->sbuf_tag);
539 if (sc->tst_soft != NULL)
540 free(sc->tst_soft, M_DEVBUF);
543 * Free all status queue memory resources
545 if (sc->tsq != NULL) {
546 bus_dmamap_unload(sc->sq_tag, sc->sq_map);
547 bus_dmamem_free(sc->sq_tag, sc->tsq, sc->sq_map);
548 bus_dma_tag_destroy(sc->sq_tag);
551 if (sc->irqres != NULL)
552 bus_release_resource(sc->dev, SYS_RES_IRQ,
553 sc->irqid, sc->irqres);
554 if (sc->memres != NULL)
555 bus_release_resource(sc->dev, SYS_RES_MEMORY,
556 IDT_PCI_REG_MEMBASE, sc->memres);
558 /* this was initialize unconditionally */
559 sysctl_ctx_free(&sc->sysctl_ctx);
560 cv_destroy(&sc->vcc_cv);
561 mtx_destroy(&sc->tst_lock);
562 mtx_destroy(&sc->mtx);
569 * Try to find a variable in the environment and parse it as an unsigned
573 patm_env_getuint(struct patm_softc *sc, u_int *var, const char *name)
575 char full[IFNAMSIZ + 3 + 20];
579 snprintf(full, sizeof(full), "hw.%s.%s",
580 device_get_nameunit(sc->dev), name);
582 if ((val = kern_getenv(full)) != NULL) {
583 u = strtoul(val, &end, 0);
584 if (end > val && *end == '\0') {
586 patm_printf(sc, "%s=%lu\n", full, u);
594 * Sysctl handler for internal statistics
596 * LOCK: unlocked, needed
599 patm_sysctl_istats(SYSCTL_HANDLER_ARGS)
601 struct patm_softc *sc = arg1;
605 ret = malloc(sizeof(sc->stats), M_TEMP, M_WAITOK);
608 bcopy(&sc->stats, ret, sizeof(sc->stats));
609 mtx_unlock(&sc->mtx);
611 error = SYSCTL_OUT(req, ret, sizeof(sc->stats));
618 * Sysctl handler for EEPROM
620 * LOCK: unlocked, needed
623 patm_sysctl_eeprom(SYSCTL_HANDLER_ARGS)
625 struct patm_softc *sc = arg1;
629 ret = malloc(sizeof(sc->eeprom), M_TEMP, M_WAITOK);
632 bcopy(sc->eeprom, ret, sizeof(sc->eeprom));
633 mtx_unlock(&sc->mtx);
635 error = SYSCTL_OUT(req, ret, sizeof(sc->eeprom));
642 * Read the EEPROM. We assume that this is a XIRCOM 25020
645 patm_read_eeprom(struct patm_softc *sc)
651 static const uint32_t tab[] = {
652 /* CS transition to reset the chip */
653 IDT_GP_EECS | IDT_GP_EESCLK, 0,
654 /* read command 0x03 */
660 IDT_GP_EESCLK, IDT_GP_EEDO,
661 IDT_GP_EESCLK | IDT_GP_EEDO, IDT_GP_EEDO,
662 IDT_GP_EESCLK | IDT_GP_EEDO, 0,
674 /* go to a known state (chip enabled) */
675 gp = patm_nor_read(sc, IDT_NOR_GP);
676 gp &= ~(IDT_GP_EESCLK | IDT_GP_EECS | IDT_GP_EEDO);
678 for (i = 0; i < sizeof(tab) / sizeof(tab[0]); i++) {
679 patm_nor_write(sc, IDT_NOR_GP, gp | tab[i]);
683 /* read out the prom */
684 for (addr = 0; addr < 256; addr++) {
686 for (i = 0; i < 8; i++) {
688 if (patm_nor_read(sc, IDT_NOR_GP) & IDT_GP_EEDI)
691 patm_nor_write(sc, IDT_NOR_GP, gp | IDT_GP_EESCLK);
694 patm_nor_write(sc, IDT_NOR_GP, gp);
697 sc->eeprom[addr] = byte;
705 patm_phy_readregs(struct ifatm *ifatm, u_int reg, uint8_t *val, u_int *n)
707 struct patm_softc *sc = ifatm->ifp->if_softc;
714 while (reg < 0x100 && cnt > 0) {
715 patm_nor_write(sc, IDT_NOR_CMD, IDT_MKCMD_RUTIL(1, 0, reg));
717 *val = patm_nor_read(sc, IDT_NOR_D0);
718 patm_debug(sc, PHY, "phy(%02x)=%02x", reg, *val);
731 patm_phy_writereg(struct ifatm *ifatm, u_int reg, u_int mask, u_int val)
733 struct patm_softc *sc = ifatm->ifp->if_softc;
740 patm_nor_write(sc, IDT_NOR_CMD, IDT_MKCMD_RUTIL(1, 0, reg));
743 old = patm_nor_read(sc, IDT_NOR_D0);
744 new = (old & ~mask) | (val & mask);
745 patm_debug(sc, PHY, "phy(%02x) %02x -> %02x", reg, old, new);
747 patm_nor_write(sc, IDT_NOR_D0, new);
748 patm_nor_write(sc, IDT_NOR_CMD, IDT_MKCMD_WUTIL(1, 0, reg));
755 * Allocate a large chunk of DMA able memory for the transmit
756 * and receive status queues. We align this to a page boundary
757 * to ensure the alignment.
760 patm_sq_init(struct patm_softc *sc)
765 /* compute size of the two queues */
766 sc->sq_size = IDT_TSQ_SIZE * IDT_TSQE_SIZE +
767 PATM_RSQ_SIZE * IDT_RSQE_SIZE +
770 patm_debug(sc, ATTACH,
771 "allocating status queues (%zu) ...", sc->sq_size);
775 * Don't use BUS_DMA_ALLOCNOW, because we never need bouncing with
778 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev),
779 PATM_SQ_ALIGNMENT, 0,
780 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
781 NULL, NULL, sc->sq_size, 1, sc->sq_size,
782 0, NULL, NULL, &sc->sq_tag);
784 patm_printf(sc, "memory DMA tag create %d\n", error);
788 /* allocate memory */
789 error = bus_dmamem_alloc(sc->sq_tag, &p, 0, &sc->sq_map);
791 patm_printf(sc, "memory DMA alloc %d\n", error);
792 bus_dma_tag_destroy(sc->sq_tag);
797 sc->tsq_phy = 0x1fff;
798 error = bus_dmamap_load(sc->sq_tag, sc->sq_map, p,
799 sc->sq_size, patm_load_callback, &sc->tsq_phy, BUS_DMA_NOWAIT);
801 patm_printf(sc, "memory DMA map load %d\n", error);
802 bus_dmamem_free(sc->sq_tag, p, sc->sq_map);
803 bus_dma_tag_destroy(sc->sq_tag);
807 /* set queue start */
809 sc->rsq = (void *)((char *)p + IDT_TSQ_SIZE * IDT_TSQE_SIZE);
810 sc->rsq_phy = sc->tsq_phy + IDT_TSQ_SIZE * IDT_TSQE_SIZE;
811 sc->rawhnd = (void *)((char *)sc->rsq + PATM_RSQ_SIZE * IDT_RSQE_SIZE);
812 sc->rawhnd_phy = sc->rsq_phy + PATM_RSQ_SIZE * IDT_RSQE_SIZE;
818 * Initialize all receive buffer stuff
821 patm_rbuf_init(struct patm_softc *sc)
826 patm_debug(sc, ATTACH, "allocating Rx buffer resources ...");
828 * Create a tag for small buffers. We allocate these page wise.
829 * Don't use BUS_DMA_ALLOCNOW, because we never need bouncing with
832 if ((error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), PAGE_SIZE, 0,
833 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
834 SMBUF_PAGE_SIZE, 1, SMBUF_PAGE_SIZE, 0,
835 NULL, NULL, &sc->sbuf_tag)) != 0) {
836 patm_printf(sc, "sbuf DMA tag create %d\n", error);
840 error = mbp_create(&sc->sbuf_pool, "patm sbufs", sc->sbuf_tag,
841 SMBUF_MAX_PAGES, SMBUF_PAGE_SIZE, SMBUF_CHUNK_SIZE);
843 patm_printf(sc, "smbuf pool create %d\n", error);
847 error = mbp_create(&sc->vbuf_pool, "patm vbufs", sc->sbuf_tag,
848 VMBUF_MAX_PAGES, SMBUF_PAGE_SIZE, VMBUF_CHUNK_SIZE);
850 patm_printf(sc, "vmbuf pool create %d\n", error);
855 * Create a tag for large buffers.
856 * Don't use BUS_DMA_ALLOCNOW, because it makes no sense with multiple
857 * maps using one tag. Rather use BUS_DMA_NOWAIT when loading the map
858 * to prevent EINPROGRESS.
860 if ((error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 4, 0,
861 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
862 MCLBYTES, 1, MCLBYTES, 0,
863 NULL, NULL, &sc->lbuf_tag)) != 0) {
864 patm_printf(sc, "lbuf DMA tag create %d\n", error);
868 if (sc->lbuf_max < IDT_FBQ_SIZE)
869 sc->lbuf_max = LMBUF_MAX;
870 sc->lbufs = malloc(sizeof(sc->lbufs[0]) * sc->lbuf_max,
871 M_DEVBUF, M_ZERO | M_WAITOK);
873 SLIST_INIT(&sc->lbuf_free_list);
874 for (i = 0; i < sc->lbuf_max; i++) {
875 struct lmbuf *b = &sc->lbufs[i];
877 error = bus_dmamap_create(sc->lbuf_tag, 0, &b->map);
879 /* must deallocate here, because a test for NULL
880 * does not work on most archs */
882 bus_dmamap_destroy(sc->lbuf_tag,
884 free(sc->lbufs, M_DEVBUF);
889 SLIST_INSERT_HEAD(&sc->lbuf_free_list, b, link);
896 * Allocate everything needed for the transmission maps.
899 patm_txmap_init(struct patm_softc *sc)
902 struct patm_txmap *map;
904 /* get transmission tag */
905 error = bus_dma_tag_create(bus_get_dma_tag(sc->dev), 1, 0,
906 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
907 NULL, NULL, 65536, IDT_SCQ_SIZE - 1, 65536,
908 0, NULL, NULL, &sc->tx_tag);
910 patm_printf(sc, "cannot allocate TX tag %d\n", error);
914 if ((sc->tx_mapzone = uma_zcreate("PATM tx maps",
915 sizeof(struct patm_txmap), NULL, NULL, NULL, NULL,
916 UMA_ALIGN_PTR, 0)) == NULL)
919 if (sc->tx_maxmaps < PATM_CFG_TXMAPS_MAX)
920 sc->tx_maxmaps = PATM_CFG_TXMAPS_MAX;
921 sc->tx_nmaps = PATM_CFG_TXMAPS_INIT;
923 for (sc->tx_nmaps = 0; sc->tx_nmaps < PATM_CFG_TXMAPS_INIT;
925 map = uma_zalloc(sc->tx_mapzone, M_WAITOK);
926 error = bus_dmamap_create(sc->tx_tag, 0, &map->map);
928 uma_zfree(sc->tx_mapzone, map);
931 SLIST_INSERT_HEAD(&sc->tx_maps_free, map, link);
940 * Sysctl handler for REGS
942 * LOCK: unlocked, needed
945 patm_sysctl_regs(SYSCTL_HANDLER_ARGS)
947 struct patm_softc *sc = arg1;
951 ret = malloc(IDT_NOR_END, M_TEMP, M_WAITOK);
954 for (i = 0; i < IDT_NOR_END; i += 4)
955 ret[i / 4] = patm_nor_read(sc, i);
956 mtx_unlock(&sc->mtx);
958 error = SYSCTL_OUT(req, ret, IDT_NOR_END);
965 * Sysctl handler for TSQ
967 * LOCK: unlocked, needed
970 patm_sysctl_tsq(SYSCTL_HANDLER_ARGS)
972 struct patm_softc *sc = arg1;
976 ret = malloc(IDT_TSQ_SIZE * IDT_TSQE_SIZE, M_TEMP, M_WAITOK);
979 memcpy(ret, sc->tsq, IDT_TSQ_SIZE * IDT_TSQE_SIZE);
980 mtx_unlock(&sc->mtx);
982 error = SYSCTL_OUT(req, ret, IDT_TSQ_SIZE * IDT_TSQE_SIZE);
991 static struct patm_softc *
992 patm_dump_unit(u_int unit)
995 struct patm_softc *sc;
997 dc = devclass_find("patm");
999 printf("%s: can't find devclass\n", __func__);
1002 sc = devclass_get_softc(dc, unit);
1004 printf("%s: invalid unit number: %d\n", __func__, unit);
1011 patm_dump_vc(u_int unit, u_int vc)
1013 struct patm_softc *sc;
1019 if ((sc = patm_dump_unit(unit)) == NULL)
1022 for (i = 0; i < 8; i++)
1023 tct[i] = patm_sram_read(sc, vc * 8 + i);
1024 for (i = 0; i < 4; i++)
1025 rct[i] = patm_sram_read(sc, sc->mmap->rct + vc * 4 + i);
1026 for (i = 0; i < 12; i++)
1027 scd[i] = patm_sram_read(sc, (tct[0] & 0x7ffff) + i);
1029 printf("TCT%3u: %08x %08x %08x %08x %08x %08x %08x %08x\n", vc,
1030 tct[0], tct[1], tct[2], tct[3], tct[4], tct[5], tct[6], tct[7]);
1031 printf("RCT%3u: %08x %08x %08x %08x\n", vc,
1032 rct[0], rct[1], rct[2], rct[3]);
1033 printf("SCD%3u: %08x %08x %08x %08x %08x %08x %08x %08x\n", vc,
1034 scd[0], scd[1], scd[2], scd[3], scd[4], scd[5], scd[6], scd[7]);
1035 printf(" %08x %08x %08x %08x\n",
1036 scd[8], scd[9], scd[10], scd[11]);
1042 patm_dump_regs(u_int unit)
1044 struct patm_softc *sc;
1047 if ((sc = patm_dump_unit(unit)) == NULL)
1050 for (i = 0; i <= IDT_NOR_DNOW; i += 4)
1051 printf("%x: %08x\n", i, patm_nor_read(sc, i));
1057 patm_dump_sram(u_int unit, u_int from, u_int words)
1059 struct patm_softc *sc;
1062 if ((sc = patm_dump_unit(unit)) == NULL)
1065 for (i = 0; i < words; i++) {
1067 printf("%05x:", from + i);
1068 printf(" %08x", patm_sram_read(sc, from + i));