2 * Copyright (C) 2013 Emulex
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * 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 * 3. Neither the name of the Emulex Corporation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * Contact Information:
32 * freebsd-drivers@emulex.com
36 * Costa Mesa, CA 92626
40 /* $FreeBSD: src/sys/dev/oce/oce_util.c,v 1.5 2013/07/07 00:30:13 svnexp Exp $ */
45 static void oce_dma_map_ring(void *arg,
46 bus_dma_segment_t *segs,
51 * @brief Allocate DMA memory
52 * @param sc software handle to the device
53 * @param size bus size
54 * @param dma dma memory area
55 * @param flags creation flags
56 * @returns 0 on success, error otherwize
59 oce_dma_alloc(POCE_SOFTC sc, bus_size_t size, POCE_DMA_MEM dma, int flags)
64 memset(dma, 0, sizeof(OCE_DMA_MEM));
66 rc = bus_dma_tag_create(NULL,
71 size, 1, size, 0, &dma->tag);
74 rc = bus_dmamem_alloc(dma->tag,
76 BUS_DMA_NOWAIT | BUS_DMA_COHERENT |
83 rc = bus_dmamap_load(dma->tag,
88 &dma->paddr, flags | BUS_DMA_NOWAIT);
94 oce_dma_free(sc, dma);
100 * @brief Free DMA memory
101 * @param sc software handle to the device
102 * @param dma dma area to free
105 oce_dma_free(POCE_SOFTC sc, POCE_DMA_MEM dma)
107 if (dma->tag == NULL)
110 if (dma->map != NULL) {
111 bus_dmamap_sync(dma->tag, dma->map,
112 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
113 bus_dmamap_unload(dma->tag, dma->map);
116 if (dma->ptr != NULL) {
117 bus_dmamem_free(dma->tag, dma->ptr, dma->map);
122 bus_dma_tag_destroy(dma->tag);
131 * @brief Map DMA memory segment addresses
132 * @param arg physical address pointer
133 * @param segs dma memory segments
134 * @param nseg number of dma memory segments
135 * @param error if error, zeroes the physical address
138 oce_dma_map_addr(void *arg, bus_dma_segment_t * segs, int nseg, int error)
140 bus_addr_t *paddr = arg;
145 *paddr = segs->ds_addr;
151 * @brief Destroy a ring buffer
152 * @param sc software handle to the device
153 * @param ring ring buffer
157 oce_destroy_ring_buffer(POCE_SOFTC sc, oce_ring_buffer_t *ring)
159 oce_dma_free(sc, &ring->dma);
160 kfree(ring, M_DEVBUF);
166 oce_create_ring_buffer(POCE_SOFTC sc,
167 uint32_t q_len, uint32_t item_size)
169 uint32_t size = q_len * item_size;
171 oce_ring_buffer_t *ring;
174 ring = kmalloc(sizeof(oce_ring_buffer_t), M_DEVBUF, M_NOWAIT | M_ZERO);
178 ring->item_size = item_size;
179 ring->num_items = q_len;
181 rc = bus_dma_tag_create(NULL,
186 size, 8, 4096, 0, &ring->dma.tag);
191 rc = bus_dmamem_alloc(ring->dma.tag,
193 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
198 bzero(ring->dma.ptr, size);
199 bus_dmamap_sync(ring->dma.tag, ring->dma.map,
200 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
206 oce_dma_free(sc, &ring->dma);
207 kfree(ring, M_DEVBUF);
214 struct _oce_dmamap_paddr_table {
215 uint32_t max_entries;
216 uint32_t num_entries;
217 struct phys_addr *paddrs;
223 * @brief Map ring buffer
224 * @param arg dma map phyical address table pointer
225 * @param segs dma memory segments
226 * @param nseg number of dma memory segments
227 * @param error maps only if error is 0
230 oce_dma_map_ring(void *arg, bus_dma_segment_t * segs, int nseg, int error)
233 struct _oce_dmamap_paddr_table *dpt =
234 (struct _oce_dmamap_paddr_table *)arg;
237 if (nseg <= dpt->max_entries) {
238 for (i = 0; i < nseg; i++) {
239 dpt->paddrs[i].lo = ADDR_LO(segs[i].ds_addr);
240 dpt->paddrs[i].hi = ADDR_HI(segs[i].ds_addr);
242 dpt->num_entries = nseg;
250 * @brief Load bus dma map for a ring buffer
251 * @param ring ring buffer pointer
252 * @param pa_list physical address list
253 * @returns number entries
256 oce_page_list(oce_ring_buffer_t *ring, struct phys_addr *pa_list)
258 struct _oce_dmamap_paddr_table dpt;
262 dpt.paddrs = pa_list;
264 bus_dmamap_load(ring->dma.tag,
267 ring->item_size * ring->num_items,
268 oce_dma_map_ring, &dpt, BUS_DMA_NOWAIT);
270 return dpt.num_entries;