Remove inclusion of <sys/cdefs.h> from kernel .c files.
[dragonfly.git] / sys / bus / firewire / fwdma.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 2003
3 * Hidetoshi Shimokawa. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 *
16 * This product includes software developed by Hidetoshi Shimokawa.
17 *
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
32af04f7 34 * $FreeBSD: src/sys/dev/firewire/fwdma.c,v 1.5 2003/08/24 17:46:07 obrien Exp $
984263bc 35 */
78748771 36
984263bc
MD
37#include <sys/param.h>
38#include <sys/systm.h>
78748771 39#include <sys/conf.h>
1f7ab7c9 40#include <sys/bus.h>
984263bc
MD
41#include <sys/types.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
90dec0df 44#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
78748771
JS
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#endif
984263bc 48
78748771
JS
49#ifdef __DragonFly__
50#include <bus/firewire/firewire.h>
51#include <bus/firewire/firewirereg.h>
52#include <bus/firewire/fwdma.h>
53#else
54#include <dev/firewire/firewire.h>
55#include <dev/firewire/firewirereg.h>
56#include <dev/firewire/fwdma.h>
57#endif
984263bc
MD
58
59static void
60fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
61{
62 bus_addr_t *baddr;
63
64 if (error)
85f8e2ea 65 kprintf("fwdma_map_cb: error=%d\n", error);
984263bc
MD
66 baddr = (bus_addr_t *)arg;
67 *baddr = segs->ds_addr;
68}
69
70void *
71fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
72 struct fwdma_alloc *dma, int flag)
73{
74 int err;
75
76 dma->v_addr = NULL;
77 err = bus_dma_tag_create(
78 /*parent*/ fc->dmat,
79 /*alignment*/ alignment,
80 /*boundary*/ 0,
81 /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
82 /*highaddr*/ BUS_SPACE_MAXADDR,
83 /*filter*/NULL, /*filterarg*/NULL,
84 /*maxsize*/ size,
85 /*nsegments*/ 1,
86 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
78748771 87 /*flags*/ BUS_DMA_ALLOCNOW,
90dec0df 88#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
78748771
JS
89 /*lockfunc*/busdma_lock_mutex,
90 /*lockarg*/&Giant,
91#endif
92 &dma->dma_tag);
984263bc 93 if (err) {
85f8e2ea 94 kprintf("fwdma_malloc: failed(1)\n");
984263bc
MD
95 return(NULL);
96 }
97
98 err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr,
99 flag, &dma->dma_map);
100 if (err) {
85f8e2ea 101 kprintf("fwdma_malloc: failed(2)\n");
984263bc
MD
102 /* XXX destory tag */
103 return(NULL);
104 }
105
106 bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr,
107 size, fwdma_map_cb, &dma->bus_addr, /*flags*/0);
108
109 return(dma->v_addr);
110}
111
112void
113fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
114{
115 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
116 bus_dmamem_free(dma->dma_tag, dma->v_addr, dma->dma_map);
117 bus_dma_tag_destroy(dma->dma_tag);
118}
119
120
121void *
122fwdma_malloc_size(bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
123 bus_size_t size, bus_addr_t *bus_addr, int flag)
124{
125 void *v_addr;
126
127 if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
85f8e2ea 128 kprintf("fwdma_malloc_size: failed(1)\n");
984263bc
MD
129 return(NULL);
130 }
131 bus_dmamap_load(dmat, *dmamap, v_addr, size,
132 fwdma_map_cb, bus_addr, /*flags*/0);
133 return(v_addr);
134}
135
136void
137fwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap,
138 void *vaddr, bus_size_t size)
139{
140 bus_dmamap_unload(dmat, dmamap);
141 bus_dmamem_free(dmat, vaddr, dmamap);
142}
143
144/*
145 * Allocate multisegment dma buffers
146 * each segment size is eqaul to ssize except last segment.
147 */
148struct fwdma_alloc_multi *
149fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment,
150 int esize, int n, int flag)
151{
152 struct fwdma_alloc_multi *am;
153 struct fwdma_seg *seg;
154 bus_size_t ssize;
155 int nseg;
156
157 if (esize > PAGE_SIZE) {
158 /* round up to PAGE_SIZE */
159 esize = ssize = roundup2(esize, PAGE_SIZE);
160 nseg = n;
161 } else {
162 /* allocate PAGE_SIZE segment for small elements */
163 ssize = rounddown(PAGE_SIZE, esize);
164 nseg = howmany(n, ssize / esize);
165 }
77652cad 166 am = (struct fwdma_alloc_multi *)kmalloc(sizeof(struct fwdma_alloc_multi)
984263bc 167 + sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK);
984263bc
MD
168 am->ssize = ssize;
169 am->esize = esize;
170 am->nseg = 0;
171 if (bus_dma_tag_create(
172 /*parent*/ fc->dmat,
173 /*alignment*/ alignment,
174 /*boundary*/ 0,
175 /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
176 /*highaddr*/ BUS_SPACE_MAXADDR,
177 /*filter*/NULL, /*filterarg*/NULL,
178 /*maxsize*/ ssize,
179 /*nsegments*/ 1,
180 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
78748771 181 /*flags*/ BUS_DMA_ALLOCNOW,
90dec0df 182#if defined(__FreeBSD__) && __FreeBSD_version >= 501102
78748771
JS
183 /*lockfunc*/busdma_lock_mutex,
184 /*lockarg*/&Giant,
185#endif
186 &am->dma_tag)) {
85f8e2ea 187 kprintf("fwdma_malloc_multiseg: tag_create failed\n");
efda3bd0 188 kfree(am, M_FW);
984263bc
MD
189 return(NULL);
190 }
191
192#if 0
90dec0df 193#if defined(__DragonFly__) || __FreeBSD_version < 500000
85f8e2ea 194 kprintf("malloc_multi: ssize=%d nseg=%d\n", ssize, nseg);
984263bc 195#else
85f8e2ea 196 kprintf("malloc_multi: ssize=%td nseg=%d\n", ssize, nseg);
984263bc
MD
197#endif
198#endif
199 for (seg = &am->seg[0]; nseg --; seg ++) {
200 seg->v_addr = fwdma_malloc_size(am->dma_tag, &seg->dma_map,
201 ssize, &seg->bus_addr, flag);
202 if (seg->v_addr == NULL) {
85f8e2ea 203 kprintf("fwdma_malloc_multi: malloc_size failed %d\n",
984263bc
MD
204 am->nseg);
205 fwdma_free_multiseg(am);
206 return(NULL);
207 }
208 am->nseg++;
209 }
210 return(am);
211}
212
213void
214fwdma_free_multiseg(struct fwdma_alloc_multi *am)
215{
216 struct fwdma_seg *seg;
217
218 for (seg = &am->seg[0]; am->nseg --; seg ++) {
219 fwdma_free_size(am->dma_tag, seg->dma_map,
220 seg->v_addr, am->ssize);
221 }
222 bus_dma_tag_destroy(am->dma_tag);
efda3bd0 223 kfree(am, M_FW);
984263bc 224}