Thu Jun 9 07:17:02 2011 UTC ()
Use __arraycount


(matt)
diff -r1.46 -r1.47 src/sys/dev/usb/usb_mem.c

cvs diff -r1.46 -r1.47 src/sys/dev/usb/usb_mem.c (switch to unified diff)

--- src/sys/dev/usb/usb_mem.c 2011/03/20 17:38:11 1.46
+++ src/sys/dev/usb/usb_mem.c 2011/06/09 07:17:02 1.47
@@ -1,418 +1,418 @@ @@ -1,418 +1,418 @@
1/* $NetBSD: usb_mem.c,v 1.46 2011/03/20 17:38:11 tsutsui Exp $ */ 1/* $NetBSD: usb_mem.c,v 1.47 2011/06/09 07:17:02 matt Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at 8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology. 9 * Carlstedt Research & Technology.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright 16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the 17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution. 18 * documentation and/or other materials provided with the distribution.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE. 30 * POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33/* 33/*
34 * USB DMA memory allocation. 34 * USB DMA memory allocation.
35 * We need to allocate a lot of small (many 8 byte, some larger) 35 * We need to allocate a lot of small (many 8 byte, some larger)
36 * memory blocks that can be used for DMA. Using the bus_dma 36 * memory blocks that can be used for DMA. Using the bus_dma
37 * routines directly would incur large overheads in space and time. 37 * routines directly would incur large overheads in space and time.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.46 2011/03/20 17:38:11 tsutsui Exp $"); 41__KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.47 2011/06/09 07:17:02 matt Exp $");
42 42
43#ifdef _KERNEL_OPT 43#ifdef _KERNEL_OPT
44#include "opt_usb.h" 44#include "opt_usb.h"
45#endif 45#endif
46 46
47#include <sys/param.h> 47#include <sys/param.h>
48#include <sys/systm.h> 48#include <sys/systm.h>
49#include <sys/kernel.h> 49#include <sys/kernel.h>
50#include <sys/malloc.h> 50#include <sys/malloc.h>
51#include <sys/queue.h> 51#include <sys/queue.h>
52#include <sys/device.h> /* for usbdivar.h */ 52#include <sys/device.h> /* for usbdivar.h */
53#include <sys/bus.h> 53#include <sys/bus.h>
54#include <sys/cpu.h> 54#include <sys/cpu.h>
55 55
56#ifdef __NetBSD__ 56#ifdef __NetBSD__
57#include <sys/extent.h> 57#include <sys/extent.h>
58#endif 58#endif
59 59
60#ifdef DIAGNOSTIC 60#ifdef DIAGNOSTIC
61#include <sys/proc.h> 61#include <sys/proc.h>
62#endif 62#endif
63 63
64#include <dev/usb/usb.h> 64#include <dev/usb/usb.h>
65#include <dev/usb/usbdi.h> 65#include <dev/usb/usbdi.h>
66#include <dev/usb/usbdivar.h> /* just for usb_dma_t */ 66#include <dev/usb/usbdivar.h> /* just for usb_dma_t */
67#include <dev/usb/usb_mem.h> 67#include <dev/usb/usb_mem.h>
68 68
69#ifdef USB_DEBUG 69#ifdef USB_DEBUG
70#define DPRINTF(x) if (usbdebug) printf x 70#define DPRINTF(x) if (usbdebug) printf x
71#define DPRINTFN(n,x) if (usbdebug>(n)) printf x 71#define DPRINTFN(n,x) if (usbdebug>(n)) printf x
72extern int usbdebug; 72extern int usbdebug;
73#else 73#else
74#define DPRINTF(x) 74#define DPRINTF(x)
75#define DPRINTFN(n,x) 75#define DPRINTFN(n,x)
76#endif 76#endif
77 77
78#define USB_MEM_SMALL 64 78#define USB_MEM_SMALL 64
79#define USB_MEM_CHUNKS 64 79#define USB_MEM_CHUNKS 64
80#define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS) 80#define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
81 81
82/* This struct is overlayed on free fragments. */ 82/* This struct is overlayed on free fragments. */
83struct usb_frag_dma { 83struct usb_frag_dma {
84 usb_dma_block_t *block; 84 usb_dma_block_t *block;
85 u_int offs; 85 u_int offs;
86 LIST_ENTRY(usb_frag_dma) next; 86 LIST_ENTRY(usb_frag_dma) next;
87}; 87};
88 88
89Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t, 89Static usbd_status usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
90 usb_dma_block_t **); 90 usb_dma_block_t **);
91Static void usb_block_freemem(usb_dma_block_t *); 91Static void usb_block_freemem(usb_dma_block_t *);
92 92
93Static LIST_HEAD(, usb_dma_block) usb_blk_freelist = 93Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
94 LIST_HEAD_INITIALIZER(usb_blk_freelist); 94 LIST_HEAD_INITIALIZER(usb_blk_freelist);
95Static int usb_blk_nfree = 0; 95Static int usb_blk_nfree = 0;
96/* XXX should have different free list for different tags (for speed) */ 96/* XXX should have different free list for different tags (for speed) */
97Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist = 97Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
98 LIST_HEAD_INITIALIZER(usb_frag_freelist); 98 LIST_HEAD_INITIALIZER(usb_frag_freelist);
99 99
100Static usbd_status 100Static usbd_status
101usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align, 101usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
102 usb_dma_block_t **dmap) 102 usb_dma_block_t **dmap)
103{ 103{
104 int error; 104 int error;
105 usb_dma_block_t *p; 105 usb_dma_block_t *p;
106 int s; 106 int s;
107 107
108 DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n", 108 DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
109 (u_long)size, (u_long)align)); 109 (u_long)size, (u_long)align));
110 110
111#ifdef DIAGNOSTIC 111#ifdef DIAGNOSTIC
112 if (cpu_intr_p()) { 112 if (cpu_intr_p()) {
113 printf("usb_block_allocmem: in interrupt context, size=%lu\n", 113 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
114 (unsigned long) size); 114 (unsigned long) size);
115 } 115 }
116#endif 116#endif
117 117
118 s = splusb(); 118 s = splusb();
119 /* First check the free list. */ 119 /* First check the free list. */
120 LIST_FOREACH(p, &usb_blk_freelist, next) { 120 LIST_FOREACH(p, &usb_blk_freelist, next) {
121 if (p->tag == tag && p->size >= size && p->align >= align) { 121 if (p->tag == tag && p->size >= size && p->align >= align) {
122 LIST_REMOVE(p, next); 122 LIST_REMOVE(p, next);
123 usb_blk_nfree--; 123 usb_blk_nfree--;
124 splx(s); 124 splx(s);
125 *dmap = p; 125 *dmap = p;
126 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n", 126 DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
127 (u_long)p->size)); 127 (u_long)p->size));
128 return (USBD_NORMAL_COMPLETION); 128 return (USBD_NORMAL_COMPLETION);
129 } 129 }
130 } 130 }
131 splx(s); 131 splx(s);
132 132
133#ifdef DIAGNOSTIC 133#ifdef DIAGNOSTIC
134 if (cpu_intr_p()) { 134 if (cpu_intr_p()) {
135 printf("usb_block_allocmem: in interrupt context, failed\n"); 135 printf("usb_block_allocmem: in interrupt context, failed\n");
136 return (USBD_NOMEM); 136 return (USBD_NOMEM);
137 } 137 }
138#endif 138#endif
139 139
140 DPRINTFN(6, ("usb_block_allocmem: no free\n")); 140 DPRINTFN(6, ("usb_block_allocmem: no free\n"));
141 p = malloc(sizeof *p, M_USB, M_NOWAIT); 141 p = malloc(sizeof *p, M_USB, M_NOWAIT);
142 if (p == NULL) 142 if (p == NULL)
143 return (USBD_NOMEM); 143 return (USBD_NOMEM);
144 144
145 p->tag = tag; 145 p->tag = tag;
146 p->size = size; 146 p->size = size;
147 p->align = align; 147 p->align = align;
148 error = bus_dmamem_alloc(tag, p->size, align, 0, 148 error = bus_dmamem_alloc(tag, p->size, align, 0,
149 p->segs, sizeof(p->segs)/sizeof(p->segs[0]), 149 p->segs, __arraycount(p->segs),
150 &p->nsegs, BUS_DMA_NOWAIT); 150 &p->nsegs, BUS_DMA_NOWAIT);
151 if (error) 151 if (error)
152 goto free0; 152 goto free0;
153 153
154 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size, 154 error = bus_dmamem_map(tag, p->segs, p->nsegs, p->size,
155 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 155 &p->kaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
156 if (error) 156 if (error)
157 goto free1; 157 goto free1;
158 158
159 error = bus_dmamap_create(tag, p->size, 1, p->size, 159 error = bus_dmamap_create(tag, p->size, 1, p->size,
160 0, BUS_DMA_NOWAIT, &p->map); 160 0, BUS_DMA_NOWAIT, &p->map);
161 if (error) 161 if (error)
162 goto unmap; 162 goto unmap;
163 163
164 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL, 164 error = bus_dmamap_load(tag, p->map, p->kaddr, p->size, NULL,
165 BUS_DMA_NOWAIT); 165 BUS_DMA_NOWAIT);
166 if (error) 166 if (error)
167 goto destroy; 167 goto destroy;
168 168
169 *dmap = p; 169 *dmap = p;
170#ifdef USB_FRAG_DMA_WORKAROUND 170#ifdef USB_FRAG_DMA_WORKAROUND
171 memset(p->kaddr, 0, p->size); 171 memset(p->kaddr, 0, p->size);
172#endif 172#endif
173 return (USBD_NORMAL_COMPLETION); 173 return (USBD_NORMAL_COMPLETION);
174 174
175 destroy: 175 destroy:
176 bus_dmamap_destroy(tag, p->map); 176 bus_dmamap_destroy(tag, p->map);
177 unmap: 177 unmap:
178 bus_dmamem_unmap(tag, p->kaddr, p->size); 178 bus_dmamem_unmap(tag, p->kaddr, p->size);
179 free1: 179 free1:
180 bus_dmamem_free(tag, p->segs, p->nsegs); 180 bus_dmamem_free(tag, p->segs, p->nsegs);
181 free0: 181 free0:
182 free(p, M_USB); 182 free(p, M_USB);
183 return (USBD_NOMEM); 183 return (USBD_NOMEM);
184} 184}
185 185
186#if 0 186#if 0
187void 187void
188usb_block_real_freemem(usb_dma_block_t *p) 188usb_block_real_freemem(usb_dma_block_t *p)
189{ 189{
190#ifdef DIAGNOSTIC 190#ifdef DIAGNOSTIC
191 if (cpu_intr_p()) { 191 if (cpu_intr_p()) {
192 printf("usb_block_real_freemem: in interrupt context\n"); 192 printf("usb_block_real_freemem: in interrupt context\n");
193 return; 193 return;
194 } 194 }
195#endif 195#endif
196 bus_dmamap_unload(p->tag, p->map); 196 bus_dmamap_unload(p->tag, p->map);
197 bus_dmamap_destroy(p->tag, p->map); 197 bus_dmamap_destroy(p->tag, p->map);
198 bus_dmamem_unmap(p->tag, p->kaddr, p->size); 198 bus_dmamem_unmap(p->tag, p->kaddr, p->size);
199 bus_dmamem_free(p->tag, p->segs, p->nsegs); 199 bus_dmamem_free(p->tag, p->segs, p->nsegs);
200 free(p, M_USB); 200 free(p, M_USB);
201} 201}
202#endif 202#endif
203 203
204/* 204/*
205 * Do not free the memory unconditionally since we might be called 205 * Do not free the memory unconditionally since we might be called
206 * from an interrupt context and that is BAD. 206 * from an interrupt context and that is BAD.
207 * XXX when should we really free? 207 * XXX when should we really free?
208 */ 208 */
209Static void 209Static void
210usb_block_freemem(usb_dma_block_t *p) 210usb_block_freemem(usb_dma_block_t *p)
211{ 211{
212 int s; 212 int s;
213 213
214 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size)); 214 DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
215 s = splusb(); 215 s = splusb();
216 LIST_INSERT_HEAD(&usb_blk_freelist, p, next); 216 LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
217 usb_blk_nfree++; 217 usb_blk_nfree++;
218 splx(s); 218 splx(s);
219} 219}
220 220
221usbd_status 221usbd_status
222usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p) 222usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
223{ 223{
224 bus_dma_tag_t tag = bus->dmatag; 224 bus_dma_tag_t tag = bus->dmatag;
225 usbd_status err; 225 usbd_status err;
226 struct usb_frag_dma *f; 226 struct usb_frag_dma *f;
227 usb_dma_block_t *b; 227 usb_dma_block_t *b;
228 int i; 228 int i;
229 int s; 229 int s;
230 230
231 /* If the request is large then just use a full block. */ 231 /* If the request is large then just use a full block. */
232 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) { 232 if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
233 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size)); 233 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
234 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1); 234 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
235 err = usb_block_allocmem(tag, size, align, &p->block); 235 err = usb_block_allocmem(tag, size, align, &p->block);
236 if (!err) { 236 if (!err) {
237 p->block->flags = USB_DMA_FULLBLOCK; 237 p->block->flags = USB_DMA_FULLBLOCK;
238 p->offs = 0; 238 p->offs = 0;
239 } 239 }
240 return (err); 240 return (err);
241 } 241 }
242 242
243 s = splusb(); 243 s = splusb();
244 /* Check for free fragments. */ 244 /* Check for free fragments. */
245 LIST_FOREACH(f, &usb_frag_freelist, next) { 245 LIST_FOREACH(f, &usb_frag_freelist, next) {
246 if (f->block->tag == tag) 246 if (f->block->tag == tag)
247 break; 247 break;
248 } 248 }
249 if (f == NULL) { 249 if (f == NULL) {
250 DPRINTFN(1, ("usb_allocmem: adding fragments\n")); 250 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
251 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b); 251 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
252 if (err) { 252 if (err) {
253 splx(s); 253 splx(s);
254 return (err); 254 return (err);
255 } 255 }
256 b->flags = 0; 256 b->flags = 0;
257 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) { 257 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
258 f = (struct usb_frag_dma *)((char *)b->kaddr + i); 258 f = (struct usb_frag_dma *)((char *)b->kaddr + i);
259 f->block = b; 259 f->block = b;
260 f->offs = i; 260 f->offs = i;
261 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 261 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
262#ifdef USB_FRAG_DMA_WORKAROUND 262#ifdef USB_FRAG_DMA_WORKAROUND
263 i += 1 * USB_MEM_SMALL; 263 i += 1 * USB_MEM_SMALL;
264#endif 264#endif
265 } 265 }
266 f = LIST_FIRST(&usb_frag_freelist); 266 f = LIST_FIRST(&usb_frag_freelist);
267 } 267 }
268 p->block = f->block; 268 p->block = f->block;
269 p->offs = f->offs; 269 p->offs = f->offs;
270#ifdef USB_FRAG_DMA_WORKAROUND 270#ifdef USB_FRAG_DMA_WORKAROUND
271 p->offs += USB_MEM_SMALL; 271 p->offs += USB_MEM_SMALL;
272#endif 272#endif
273 p->block->flags &= ~USB_DMA_RESERVE; 273 p->block->flags &= ~USB_DMA_RESERVE;
274 LIST_REMOVE(f, next); 274 LIST_REMOVE(f, next);
275 splx(s); 275 splx(s);
276 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size)); 276 DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
277 return (USBD_NORMAL_COMPLETION); 277 return (USBD_NORMAL_COMPLETION);
278} 278}
279 279
280void 280void
281usb_freemem(usbd_bus_handle bus, usb_dma_t *p) 281usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
282{ 282{
283 struct usb_frag_dma *f; 283 struct usb_frag_dma *f;
284 int s; 284 int s;
285 285
286 if (p->block->flags & USB_DMA_FULLBLOCK) { 286 if (p->block->flags & USB_DMA_FULLBLOCK) {
287 DPRINTFN(1, ("usb_freemem: large free\n")); 287 DPRINTFN(1, ("usb_freemem: large free\n"));
288 usb_block_freemem(p->block); 288 usb_block_freemem(p->block);
289 return; 289 return;
290 } 290 }
291 //usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD); 291 //usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD);
292 f = KERNADDR(p, 0); 292 f = KERNADDR(p, 0);
293#ifdef USB_FRAG_DMA_WORKAROUND 293#ifdef USB_FRAG_DMA_WORKAROUND
294 f = (void *)((uintptr_t)f - USB_MEM_SMALL); 294 f = (void *)((uintptr_t)f - USB_MEM_SMALL);
295#endif 295#endif
296 f->block = p->block; 296 f->block = p->block;
297 f->offs = p->offs; 297 f->offs = p->offs;
298#ifdef USB_FRAG_DMA_WORKAROUND 298#ifdef USB_FRAG_DMA_WORKAROUND
299 f->offs -= USB_MEM_SMALL; 299 f->offs -= USB_MEM_SMALL;
300#endif 300#endif
301 s = splusb(); 301 s = splusb();
302 LIST_INSERT_HEAD(&usb_frag_freelist, f, next); 302 LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
303 splx(s); 303 splx(s);
304 DPRINTFN(5, ("usb_freemem: frag=%p\n", f)); 304 DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
305} 305}
306 306
307void 307void
308usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops) 308usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
309{ 309{
310 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset, 310 bus_dmamap_sync(p->block->tag, p->block->map, p->offs + offset,
311 len, ops); 311 len, ops);
312} 312}
313 313
314 314
315#ifdef __NetBSD__ 315#ifdef __NetBSD__
316usbd_status 316usbd_status
317usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size) 317usb_reserve_allocm(struct usb_dma_reserve *rs, usb_dma_t *dma, u_int32_t size)
318{ 318{
319 int error; 319 int error;
320 u_long start; 320 u_long start;
321 bus_addr_t baddr; 321 bus_addr_t baddr;
322 322
323 if (rs->vaddr == 0 || size > USB_MEM_RESERVE) 323 if (rs->vaddr == 0 || size > USB_MEM_RESERVE)
324 return USBD_NOMEM; 324 return USBD_NOMEM;
325 325
326 dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT); 326 dma->block = malloc(sizeof *dma->block, M_USB, M_ZERO | M_NOWAIT);
327 if (dma->block == NULL) 327 if (dma->block == NULL)
328 return USBD_NOMEM; 328 return USBD_NOMEM;
329 329
330 error = extent_alloc(rs->extent, size, PAGE_SIZE, 0, 330 error = extent_alloc(rs->extent, size, PAGE_SIZE, 0,
331 EX_NOWAIT, &start); 331 EX_NOWAIT, &start);
332 332
333 if (error != 0) { 333 if (error != 0) {
334 aprint_error_dev(rs->dv, 334 aprint_error_dev(rs->dv,
335 "usb_reserve_allocm of size %u failed (error %d)\n", 335 "usb_reserve_allocm of size %u failed (error %d)\n",
336 size, error); 336 size, error);
337 return USBD_NOMEM; 337 return USBD_NOMEM;
338 } 338 }
339 339
340 baddr = start; 340 baddr = start;
341 dma->offs = baddr - rs->paddr; 341 dma->offs = baddr - rs->paddr;
342 dma->block->flags = USB_DMA_RESERVE; 342 dma->block->flags = USB_DMA_RESERVE;
343 dma->block->align = PAGE_SIZE; 343 dma->block->align = PAGE_SIZE;
344 dma->block->size = size; 344 dma->block->size = size;
345 dma->block->nsegs = 1; 345 dma->block->nsegs = 1;
346 /* XXX segs appears to be unused */ 346 /* XXX segs appears to be unused */
347 dma->block->segs[0] = rs->map->dm_segs[0]; 347 dma->block->segs[0] = rs->map->dm_segs[0];
348 dma->block->map = rs->map; 348 dma->block->map = rs->map;
349 dma->block->kaddr = rs->vaddr; 349 dma->block->kaddr = rs->vaddr;
350 dma->block->tag = rs->dtag; 350 dma->block->tag = rs->dtag;
351 351
352 return USBD_NORMAL_COMPLETION; 352 return USBD_NORMAL_COMPLETION;
353} 353}
354 354
355void 355void
356usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma) 356usb_reserve_freem(struct usb_dma_reserve *rs, usb_dma_t *dma)
357{ 357{
358 int error; 358 int error;
359 359
360 error = extent_free(rs->extent, 360 error = extent_free(rs->extent,
361 (u_long)(rs->paddr + dma->offs), dma->block->size, 0); 361 (u_long)(rs->paddr + dma->offs), dma->block->size, 0);
362 free(dma->block, M_USB); 362 free(dma->block, M_USB);
363} 363}
364 364
365int 365int
366usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag, 366usb_setup_reserve(device_t dv, struct usb_dma_reserve *rs, bus_dma_tag_t dtag,
367 size_t size) 367 size_t size)
368{ 368{
369 int error, nseg; 369 int error, nseg;
370 bus_dma_segment_t seg; 370 bus_dma_segment_t seg;
371 371
372 rs->dtag = dtag; 372 rs->dtag = dtag;
373 rs->size = size; 373 rs->size = size;
374 rs->dv = dv; 374 rs->dv = dv;
375 375
376 error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0, 376 error = bus_dmamem_alloc(dtag, USB_MEM_RESERVE, PAGE_SIZE, 0,
377 &seg, 1, &nseg, BUS_DMA_NOWAIT); 377 &seg, 1, &nseg, BUS_DMA_NOWAIT);
378 if (error != 0) 378 if (error != 0)
379 return error; 379 return error;
380 380
381 error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE, 381 error = bus_dmamem_map(dtag, &seg, nseg, USB_MEM_RESERVE,
382 &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT); 382 &rs->vaddr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
383 if (error != 0) 383 if (error != 0)
384 goto freeit; 384 goto freeit;
385 385
386 error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1, 386 error = bus_dmamap_create(dtag, USB_MEM_RESERVE, 1,
387 USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map); 387 USB_MEM_RESERVE, 0, BUS_DMA_NOWAIT, &rs->map);
388 if (error != 0) 388 if (error != 0)
389 goto unmap; 389 goto unmap;
390 390
391 error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE, 391 error = bus_dmamap_load(dtag, rs->map, rs->vaddr, USB_MEM_RESERVE,
392 NULL, BUS_DMA_NOWAIT); 392 NULL, BUS_DMA_NOWAIT);
393 if (error != 0) 393 if (error != 0)
394 goto destroy; 394 goto destroy;
395 395
396 rs->paddr = rs->map->dm_segs[0].ds_addr; 396 rs->paddr = rs->map->dm_segs[0].ds_addr;
397 rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr, 397 rs->extent = extent_create(device_xname(dv), (u_long)rs->paddr,
398 (u_long)(rs->paddr + USB_MEM_RESERVE - 1), 398 (u_long)(rs->paddr + USB_MEM_RESERVE - 1),
399 M_USB, 0, 0, 0); 399 M_USB, 0, 0, 0);
400 if (rs->extent == NULL) { 400 if (rs->extent == NULL) {
401 rs->vaddr = 0; 401 rs->vaddr = 0;
402 return ENOMEM; 402 return ENOMEM;
403 } 403 }
404 404
405 return 0; 405 return 0;
406 406
407 destroy: 407 destroy:
408 bus_dmamap_destroy(dtag, rs->map); 408 bus_dmamap_destroy(dtag, rs->map);
409 unmap: 409 unmap:
410 bus_dmamem_unmap(dtag, rs->vaddr, size); 410 bus_dmamem_unmap(dtag, rs->vaddr, size);
411 freeit: 411 freeit:
412 bus_dmamem_free(dtag, &seg, nseg); 412 bus_dmamem_free(dtag, &seg, nseg);
413 413
414 rs->vaddr = 0; 414 rs->vaddr = 0;
415 415
416 return error; 416 return error;
417} 417}
418#endif 418#endif