Use M_INTWAIT and M_WAITOK instead of M_NOWAIT within the USB bus
[dragonfly.git] / sys / bus / usb / usb_mem.c
1 /*
2  * $NetBSD: usb_mem.c,v 1.26 2003/02/01 06:23:40 thorpej Exp $
3  * $FreeBSD: src/sys/dev/usb/usb_mem.c,v 1.5 2003/10/04 22:13:21 joe Exp $
4  * $DragonFly: src/sys/bus/usb/usb_mem.c,v 1.3 2004/03/12 03:43:06 dillon Exp $
5  */
6 /*
7  * Copyright (c) 1998 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Lennart Augustsson (lennart@augustsson.net) at
12  * Carlstedt Research & Technology.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *        This product includes software developed by the NetBSD
25  *        Foundation, Inc. and its contributors.
26  * 4. Neither the name of The NetBSD Foundation nor the names of its
27  *    contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42
43 /*
44  * USB DMA memory allocation.
45  * We need to allocate a lot of small (many 8 byte, some larger)
46  * memory blocks that can be used for DMA.  Using the bus_dma
47  * routines directly would incur large overheads in space and time.
48  */
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #if defined(__NetBSD__) || defined(__OpenBSD__)
54 #include <sys/device.h>         /* for usbdivar.h */
55 #include <machine/bus.h>
56 #elif defined(__FreeBSD__) || defined(__DragonFly__)
57 #include <sys/endian.h>
58 #include <sys/module.h>
59 #include <sys/bus.h>
60 #endif
61 #include <sys/queue.h>
62
63 #include <machine/bus.h>
64 #include <machine/endian.h>
65
66 #ifdef DIAGNOSTIC
67 #include <sys/proc.h>
68 #endif
69
70 #include <bus/usb/usb.h>
71 #include <bus/usb/usbdi.h>
72 #include <bus/usb/usbdivar.h>   /* just for usb_dma_t */
73 #include <bus/usb/usb_mem.h>
74
75 #ifdef USB_DEBUG
76 #define DPRINTF(x)      if (usbdebug) logprintf x
77 #define DPRINTFN(n,x)   if (usbdebug>(n)) logprintf x
78 extern int usbdebug;
79 #else
80 #define DPRINTF(x)
81 #define DPRINTFN(n,x)
82 #endif
83
84 #define USB_MEM_SMALL 64
85 #define USB_MEM_CHUNKS (PAGE_SIZE / USB_MEM_SMALL)
86 #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
87
88 /* This struct is overlayed on free fragments. */
89 struct usb_frag_dma {
90         usb_dma_block_t *block;
91         u_int offs;
92         LIST_ENTRY(usb_frag_dma) next;
93 };
94
95 Static bus_dmamap_callback_t usbmem_callback;
96 Static usbd_status      usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
97                                            usb_dma_block_t **);
98 Static void             usb_block_freemem(usb_dma_block_t *);
99
100 Static LIST_HEAD(, usb_dma_block) usb_blk_freelist =
101         LIST_HEAD_INITIALIZER(usb_blk_freelist);
102 Static int usb_blk_nfree = 0;
103 /* XXX should have different free list for different tags (for speed) */
104 Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
105         LIST_HEAD_INITIALIZER(usb_frag_freelist);
106
107 Static void
108 usbmem_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error)
109 {
110         int i;
111         usb_dma_block_t *p = arg;
112
113         if (error == EFBIG) {
114                 printf("usb: mapping to large\n");
115                 return;
116         }
117
118         p->nsegs = nseg;
119         for (i = 0; i < nseg && i < sizeof p->segs / sizeof *p->segs; i++)
120                 p->segs[i] = segs[i];
121 }
122
123 Static usbd_status
124 usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
125                    usb_dma_block_t **dmap)
126 {
127         usb_dma_block_t *p;
128         int s;
129
130         DPRINTFN(5, ("usb_block_allocmem: size=%lu align=%lu\n",
131                      (u_long)size, (u_long)align));
132
133 #ifdef DIAGNOSTIC
134         if (!curproc) {
135                 printf("usb_block_allocmem: in interrupt context, size=%lu\n",
136                     (unsigned long) size);
137         }
138 #endif
139
140         s = splusb();
141         /* First check the free list. */
142         for (p = LIST_FIRST(&usb_blk_freelist); p; p = LIST_NEXT(p, next)) {
143                 if (p->tag == tag && p->size >= size && p->align >= align) {
144                         LIST_REMOVE(p, next);
145                         usb_blk_nfree--;
146                         splx(s);
147                         *dmap = p;
148                         DPRINTFN(6,("usb_block_allocmem: free list size=%lu\n",
149                                     (u_long)p->size));
150                         return (USBD_NORMAL_COMPLETION);
151                 }
152         }
153         splx(s);
154
155 #ifdef DIAGNOSTIC
156         if (!curproc) {
157                 printf("usb_block_allocmem: in interrupt context, failed\n");
158                 return (USBD_NOMEM);
159         }
160 #endif
161
162         DPRINTFN(6, ("usb_block_allocmem: no free\n"));
163         p = malloc(sizeof *p, M_USB, M_INTWAIT);
164
165 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
166         if (bus_dma_tag_create(tag, align, 0,
167             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
168             size, sizeof(p->segs) / sizeof(p->segs[0]), size,
169             BUS_DMA_ALLOCNOW, NULL, NULL, &p->tag) == ENOMEM)
170 #else
171         if (bus_dma_tag_create(tag, align, 0,
172             BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
173             size, sizeof(p->segs) / sizeof(p->segs[0]), size,
174             BUS_DMA_ALLOCNOW, &p->tag) == ENOMEM)
175 #endif
176         {
177                 goto free;
178         }
179
180         p->size = size;
181         p->align = align;
182         if (bus_dmamem_alloc(p->tag, &p->kaddr,
183             BUS_DMA_NOWAIT|BUS_DMA_COHERENT, &p->map))
184                 goto tagfree;
185
186         if (bus_dmamap_load(p->tag, p->map, p->kaddr, p->size,
187             usbmem_callback, p, 0))
188                 goto memfree;
189
190         /* XXX - override the tag, ok since we never free it */
191         p->tag = tag;
192         *dmap = p;
193         return (USBD_NORMAL_COMPLETION);
194
195         /*
196          * XXX - do we need to _unload? is the order of _free and _destroy
197          * correct?
198          */
199 memfree:
200         bus_dmamem_free(p->tag, p->kaddr, p->map);
201 tagfree:
202         bus_dma_tag_destroy(p->tag);
203 free:
204         free(p, M_USB);
205         return (USBD_NOMEM);
206 }
207
208 /*
209  * Do not free the memory unconditionally since we might be called
210  * from an interrupt context and that is BAD.
211  * XXX when should we really free?
212  */
213 Static void
214 usb_block_freemem(usb_dma_block_t *p)
215 {
216         int s;
217
218         DPRINTFN(6, ("usb_block_freemem: size=%lu\n", (u_long)p->size));
219         s = splusb();
220         LIST_INSERT_HEAD(&usb_blk_freelist, p, next);
221         usb_blk_nfree++;
222         splx(s);
223 }
224
225 usbd_status
226 usb_allocmem(usbd_bus_handle bus, size_t size, size_t align, usb_dma_t *p)
227 {
228         bus_dma_tag_t tag = bus->dmatag;
229         usbd_status err;
230         struct usb_frag_dma *f;
231         usb_dma_block_t *b;
232         int i;
233         int s;
234
235         /* compat w/ Net/OpenBSD */
236         if (align == 0)
237                 align = 1;
238
239         /* If the request is large then just use a full block. */
240         if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
241                 DPRINTFN(1, ("usb_allocmem: large alloc %d\n", (int)size));
242                 size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
243                 err = usb_block_allocmem(tag, size, align, &p->block);
244                 if (!err) {
245                         p->block->fullblock = 1;
246                         p->offs = 0;
247                         p->len = size;
248                 }
249                 return (err);
250         }
251
252         s = splusb();
253         /* Check for free fragments. */
254         for (f = LIST_FIRST(&usb_frag_freelist); f; f = LIST_NEXT(f, next))
255                 if (f->block->tag == tag)
256                         break;
257         if (f == NULL) {
258                 DPRINTFN(1, ("usb_allocmem: adding fragments\n"));
259                 err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,&b);
260                 if (err) {
261                         splx(s);
262                         return (err);
263                 }
264                 b->fullblock = 0;
265                 /* XXX - override the tag, ok since we never free it */
266                 b->tag = tag;
267                 KASSERT(sizeof *f <= USB_MEM_SMALL, ("USB_MEM_SMALL(%d) is too small for struct usb_frag_dma(%zd)\n",
268                     USB_MEM_SMALL, sizeof *f));
269                 for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
270                         f = (struct usb_frag_dma *)((char *)b->kaddr + i);
271                         f->block = b;
272                         f->offs = i;
273                         LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
274                 }
275                 f = LIST_FIRST(&usb_frag_freelist);
276         }
277         p->block = f->block;
278         p->offs = f->offs;
279         p->len = USB_MEM_SMALL;
280         LIST_REMOVE(f, next);
281         splx(s);
282         DPRINTFN(5, ("usb_allocmem: use frag=%p size=%d\n", f, (int)size));
283         return (USBD_NORMAL_COMPLETION);
284 }
285
286 void
287 usb_freemem(usbd_bus_handle bus, usb_dma_t *p)
288 {
289         struct usb_frag_dma *f;
290         int s;
291
292         if (p->block->fullblock) {
293                 DPRINTFN(1, ("usb_freemem: large free\n"));
294                 usb_block_freemem(p->block);
295                 return;
296         }
297         f = KERNADDR(p, 0);
298         f->block = p->block;
299         f->offs = p->offs;
300         s = splusb();
301         LIST_INSERT_HEAD(&usb_frag_freelist, f, next);
302         splx(s);
303         DPRINTFN(5, ("usb_freemem: frag=%p\n", f));
304 }