Add missing "return(error)".
[dragonfly.git] / sys / bus / firewire / fwdma.c
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  * 
34  * $DragonFly: src/sys/bus/firewire/fwdma.c,v 1.5 2004/02/05 17:51:43 joerg Exp $
35  */
36
37 #ifndef __DragonFly__
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/sys/dev/firewire/fwdma.c,v 1.5 2003/08/24 17:46:07 obrien Exp $");
40 #endif
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102 
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #endif
52
53 #include <sys/bus.h>
54 #include <machine/bus.h>
55
56 #ifdef __DragonFly__
57 #include <bus/firewire/firewire.h>
58 #include <bus/firewire/firewirereg.h>
59 #include <bus/firewire/fwdma.h>
60 #else
61 #include <dev/firewire/firewire.h>
62 #include <dev/firewire/firewirereg.h>
63 #include <dev/firewire/fwdma.h>
64 #endif
65
66 static void
67 fwdma_map_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
68 {
69         bus_addr_t *baddr;
70
71         if (error)
72                 printf("fwdma_map_cb: error=%d\n", error);
73         baddr = (bus_addr_t *)arg;
74         *baddr = segs->ds_addr;
75 }
76
77 void *
78 fwdma_malloc(struct firewire_comm *fc, int alignment, bus_size_t size,
79         struct fwdma_alloc *dma, int flag)
80 {
81         int err;
82
83         dma->v_addr = NULL;
84         err = bus_dma_tag_create(
85                 /*parent*/ fc->dmat,
86                 /*alignment*/ alignment,
87                 /*boundary*/ 0,
88                 /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
89                 /*highaddr*/ BUS_SPACE_MAXADDR,
90                 /*filter*/NULL, /*filterarg*/NULL,
91                 /*maxsize*/ size,
92                 /*nsegments*/ 1,
93                 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
94                 /*flags*/ BUS_DMA_ALLOCNOW,
95 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102 
96                 /*lockfunc*/busdma_lock_mutex,
97                 /*lockarg*/&Giant,
98 #endif
99                 &dma->dma_tag);
100         if (err) {
101                 printf("fwdma_malloc: failed(1)\n");
102                 return(NULL);
103         }
104
105         err = bus_dmamem_alloc(dma->dma_tag, &dma->v_addr,
106                 flag, &dma->dma_map);
107         if (err) {
108                 printf("fwdma_malloc: failed(2)\n");
109                 /* XXX destory tag */
110                 return(NULL);
111         }
112
113         bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->v_addr,
114                 size, fwdma_map_cb, &dma->bus_addr, /*flags*/0);
115
116         return(dma->v_addr);
117 }
118
119 void
120 fwdma_free(struct firewire_comm *fc, struct fwdma_alloc *dma)
121 {
122         bus_dmamap_unload(dma->dma_tag, dma->dma_map);
123         bus_dmamem_free(dma->dma_tag, dma->v_addr, dma->dma_map);
124         bus_dma_tag_destroy(dma->dma_tag);
125 }
126
127
128 void *
129 fwdma_malloc_size(bus_dma_tag_t dmat, bus_dmamap_t *dmamap,
130         bus_size_t size, bus_addr_t *bus_addr, int flag)
131 {
132         void *v_addr;
133
134         if (bus_dmamem_alloc(dmat, &v_addr, flag, dmamap)) {
135                 printf("fwdma_malloc_size: failed(1)\n");
136                 return(NULL);
137         }
138         bus_dmamap_load(dmat, *dmamap, v_addr, size,
139                         fwdma_map_cb, bus_addr, /*flags*/0);
140         return(v_addr);
141 }
142
143 void
144 fwdma_free_size(bus_dma_tag_t dmat, bus_dmamap_t dmamap,
145                 void *vaddr, bus_size_t size)
146 {
147         bus_dmamap_unload(dmat, dmamap);
148         bus_dmamem_free(dmat, vaddr, dmamap);
149
150
151 /*
152  * Allocate multisegment dma buffers
153  * each segment size is eqaul to ssize except last segment.
154  */
155 struct fwdma_alloc_multi *
156 fwdma_malloc_multiseg(struct firewire_comm *fc, int alignment,
157                 int esize, int n, int flag)
158 {
159         struct fwdma_alloc_multi *am;
160         struct fwdma_seg *seg;
161         bus_size_t ssize;
162         int nseg;
163
164         if (esize > PAGE_SIZE) {
165                 /* round up to PAGE_SIZE */
166                 esize = ssize = roundup2(esize, PAGE_SIZE);
167                 nseg = n;
168         } else {
169                 /* allocate PAGE_SIZE segment for small elements */
170                 ssize = rounddown(PAGE_SIZE, esize);
171                 nseg = howmany(n, ssize / esize);
172         }
173         am = (struct fwdma_alloc_multi *)malloc(sizeof(struct fwdma_alloc_multi)
174                         + sizeof(struct fwdma_seg)*nseg, M_FW, M_WAITOK);
175         if (am == NULL) {
176                 printf("fwdma_malloc_multiseg: malloc failed\n");
177                 return(NULL);
178         }
179         am->ssize = ssize;
180         am->esize = esize;
181         am->nseg = 0;
182         if (bus_dma_tag_create(
183                         /*parent*/ fc->dmat,
184                         /*alignment*/ alignment,
185                         /*boundary*/ 0,
186                         /*lowaddr*/ BUS_SPACE_MAXADDR_32BIT,
187                         /*highaddr*/ BUS_SPACE_MAXADDR,
188                         /*filter*/NULL, /*filterarg*/NULL,
189                         /*maxsize*/ ssize,
190                         /*nsegments*/ 1,
191                         /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
192                         /*flags*/ BUS_DMA_ALLOCNOW,
193 #if defined(__FreeBSD__) && __FreeBSD_version >= 501102
194                         /*lockfunc*/busdma_lock_mutex,
195                         /*lockarg*/&Giant,
196 #endif
197                         &am->dma_tag)) {
198                 printf("fwdma_malloc_multiseg: tag_create failed\n");
199                 free(am, M_FW);
200                 return(NULL);
201         }
202
203 #if 0
204 #if defined(__DragonFly__) || __FreeBSD_version < 500000
205         printf("malloc_multi: ssize=%d nseg=%d\n", ssize, nseg);
206 #else
207         printf("malloc_multi: ssize=%td nseg=%d\n", ssize, nseg);
208 #endif
209 #endif
210         for (seg = &am->seg[0]; nseg --; seg ++) {
211                 seg->v_addr = fwdma_malloc_size(am->dma_tag, &seg->dma_map,
212                         ssize, &seg->bus_addr, flag);
213                 if (seg->v_addr == NULL) {
214                         printf("fwdma_malloc_multi: malloc_size failed %d\n",
215                                 am->nseg);
216                         fwdma_free_multiseg(am);
217                         return(NULL);
218                 }
219                 am->nseg++;
220         }
221         return(am);
222 }
223
224 void
225 fwdma_free_multiseg(struct fwdma_alloc_multi *am)
226 {
227         struct fwdma_seg *seg;
228
229         for (seg = &am->seg[0]; am->nseg --; seg ++) {
230                 fwdma_free_size(am->dma_tag, seg->dma_map,
231                         seg->v_addr, am->ssize);
232         }
233         bus_dma_tag_destroy(am->dma_tag);
234         free(am, M_FW);
235 }