2 * Copyright (c) 2010-2011 Solarflare Communications, Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
36 #include <machine/bus.h>
38 #include "common/efx.h"
43 sfxge_dma_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
54 *addr = segs[0].ds_addr;
58 sfxge_dma_map_sg_collapse(bus_dma_tag_t tag, bus_dmamap_t map,
59 struct mbuf **mp, bus_dma_segment_t *segs, int *nsegs, int maxsegs)
61 bus_dma_segment_t *psegs;
68 defragged = err = seg_count = 0;
70 KASSERT(m->m_pkthdr.len, ("packet has zero header length"));
75 if (m->m_next == NULL) {
76 sfxge_map_mbuf_fast(tag, map, m, segs);
80 #if defined(__i386__) || defined(__amd64__)
81 while (m && seg_count < maxsegs) {
83 * firmware doesn't like empty segments
87 sfxge_map_mbuf_fast(tag, map, m, psegs);
93 err = bus_dmamap_load_mbuf_sg(tag, map, *mp, segs, &seg_count, 0);
98 } else if (err == EFBIG || seg_count >= maxsegs) {
100 m = m_defrag(*mp, M_NOWAIT);
119 sfxge_dma_free(efsys_mem_t *esmp)
122 bus_dmamap_unload(esmp->esm_tag, esmp->esm_map);
123 bus_dmamem_free(esmp->esm_tag, esmp->esm_base, esmp->esm_map);
124 bus_dma_tag_destroy(esmp->esm_tag);
127 esmp->esm_base = NULL;
131 sfxge_dma_alloc(struct sfxge_softc *sc, bus_size_t len, efsys_mem_t *esmp)
135 /* Create the child DMA tag. */
136 if (bus_dma_tag_create(sc->parent_dma_tag, PAGE_SIZE, 0,
137 MIN(0x3FFFFFFFFFFFUL, BUS_SPACE_MAXADDR), BUS_SPACE_MAXADDR, NULL,
138 NULL, len, 1, len, 0, NULL, NULL, &esmp->esm_tag) != 0) {
139 device_printf(sc->dev, "Couldn't allocate txq DMA tag\n");
143 /* Allocate kernel memory. */
144 if (bus_dmamem_alloc(esmp->esm_tag, (void **)&vaddr,
145 BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
146 &esmp->esm_map) != 0) {
147 device_printf(sc->dev, "Couldn't allocate DMA memory\n");
148 bus_dma_tag_destroy(esmp->esm_tag);
152 /* Load map into device memory. */
153 if (bus_dmamap_load(esmp->esm_tag, esmp->esm_map, vaddr, len,
154 sfxge_dma_cb, &esmp->esm_addr, 0) != 0) {
155 device_printf(sc->dev, "Couldn't load DMA mapping\n");
156 bus_dmamem_free(esmp->esm_tag, esmp->esm_base, esmp->esm_map);
157 bus_dma_tag_destroy(esmp->esm_tag);
162 * The callback gets error information about the mapping
163 * and will have set our vaddr to NULL if something went
169 esmp->esm_base = vaddr;
175 sfxge_dma_fini(struct sfxge_softc *sc)
178 bus_dma_tag_destroy(sc->parent_dma_tag);
182 sfxge_dma_init(struct sfxge_softc *sc)
185 /* Create the parent dma tag. */
186 if (bus_dma_tag_create(bus_get_dma_tag(sc->dev), /* parent */
187 1, 0, /* algnmnt, boundary */
188 BUS_SPACE_MAXADDR, /* lowaddr */
189 BUS_SPACE_MAXADDR, /* highaddr */
190 NULL, NULL, /* filter, filterarg */
191 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
192 BUS_SPACE_UNRESTRICTED, /* nsegments */
193 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
195 NULL, NULL, /* lock, lockarg */
196 &sc->parent_dma_tag)) {
197 device_printf(sc->dev, "Cannot allocate parent DMA tag\n");