xref: /freebsd/sys/dev/usb/usb_busdma.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
502ac6454SAndrew Thompson  *
602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
802ac6454SAndrew Thompson  * are met:
902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1402ac6454SAndrew Thompson  *
1502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1602ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1702ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1802ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1902ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2002ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2102ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2202ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2302ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2402ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2502ac6454SAndrew Thompson  * SUCH DAMAGE.
2602ac6454SAndrew Thompson  */
2702ac6454SAndrew Thompson 
28d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE
29d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE
30d2b99310SHans Petter Selasky #else
31ed6d949aSAndrew Thompson #include <sys/stdint.h>
32ed6d949aSAndrew Thompson #include <sys/stddef.h>
33ed6d949aSAndrew Thompson #include <sys/param.h>
34ed6d949aSAndrew Thompson #include <sys/queue.h>
35ed6d949aSAndrew Thompson #include <sys/types.h>
36ed6d949aSAndrew Thompson #include <sys/systm.h>
37ed6d949aSAndrew Thompson #include <sys/kernel.h>
38ed6d949aSAndrew Thompson #include <sys/bus.h>
39ed6d949aSAndrew Thompson #include <sys/module.h>
40ed6d949aSAndrew Thompson #include <sys/lock.h>
41ed6d949aSAndrew Thompson #include <sys/mutex.h>
42ed6d949aSAndrew Thompson #include <sys/condvar.h>
43ed6d949aSAndrew Thompson #include <sys/sysctl.h>
44ed6d949aSAndrew Thompson #include <sys/sx.h>
45ed6d949aSAndrew Thompson #include <sys/unistd.h>
46ed6d949aSAndrew Thompson #include <sys/callout.h>
47ed6d949aSAndrew Thompson #include <sys/malloc.h>
48ed6d949aSAndrew Thompson #include <sys/priv.h>
49ed6d949aSAndrew Thompson 
5002ac6454SAndrew Thompson #include <dev/usb/usb.h>
51ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
52ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
5302ac6454SAndrew Thompson 
54a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
5502ac6454SAndrew Thompson 
5602ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6302ac6454SAndrew Thompson 
6402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h>
6502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h>
66d2b99310SHans Petter Selasky #endif			/* USB_GLOBAL_INCLUDE_FILE */
6702ac6454SAndrew Thompson 
68bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
69a593f6b8SAndrew Thompson static void	usb_dma_tag_create(struct usb_dma_tag *, usb_size_t, usb_size_t);
70a593f6b8SAndrew Thompson static void	usb_dma_tag_destroy(struct usb_dma_tag *);
71a593f6b8SAndrew Thompson static void	usb_dma_lock_cb(void *, bus_dma_lock_op_t);
72a593f6b8SAndrew Thompson static void	usb_pc_alloc_mem_cb(void *, bus_dma_segment_t *, int, int);
73a593f6b8SAndrew Thompson static void	usb_pc_load_mem_cb(void *, bus_dma_segment_t *, int, int);
74a593f6b8SAndrew Thompson static void	usb_pc_common_mem_cb(void *, bus_dma_segment_t *, int, int,
7502ac6454SAndrew Thompson 		    uint8_t);
7602ac6454SAndrew Thompson #endif
7702ac6454SAndrew Thompson 
7802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
79a593f6b8SAndrew Thompson  *  usbd_get_page - lookup DMA-able memory for the given offset
8002ac6454SAndrew Thompson  *
8102ac6454SAndrew Thompson  * NOTE: Only call this function when the "page_cache" structure has
8202ac6454SAndrew Thompson  * been properly initialized !
8302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
8402ac6454SAndrew Thompson void
usbd_get_page(struct usb_page_cache * pc,usb_frlength_t offset,struct usb_page_search * res)85a593f6b8SAndrew Thompson usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset,
86760bc48eSAndrew Thompson     struct usb_page_search *res)
8702ac6454SAndrew Thompson {
88271ae033SHans Petter Selasky #if USB_HAVE_BUSDMA
89760bc48eSAndrew Thompson 	struct usb_page *page;
9002ac6454SAndrew Thompson 
9102ac6454SAndrew Thompson 	if (pc->page_start) {
9202ac6454SAndrew Thompson 		/* Case 1 - something has been loaded into DMA */
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson 		if (pc->buffer) {
9502ac6454SAndrew Thompson 			/* Case 1a - Kernel Virtual Address */
9602ac6454SAndrew Thompson 
9702ac6454SAndrew Thompson 			res->buffer = USB_ADD_BYTES(pc->buffer, offset);
9802ac6454SAndrew Thompson 		}
9902ac6454SAndrew Thompson 		offset += pc->page_offset_buf;
10002ac6454SAndrew Thompson 
10102ac6454SAndrew Thompson 		/* compute destination page */
10202ac6454SAndrew Thompson 
10302ac6454SAndrew Thompson 		page = pc->page_start;
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson 		if (pc->ismultiseg) {
10602ac6454SAndrew Thompson 			page += (offset / USB_PAGE_SIZE);
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson 			offset %= USB_PAGE_SIZE;
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson 			res->length = USB_PAGE_SIZE - offset;
11102ac6454SAndrew Thompson 			res->physaddr = page->physaddr + offset;
11202ac6454SAndrew Thompson 		} else {
1136d917491SHans Petter Selasky 			res->length = (usb_size_t)-1;
11402ac6454SAndrew Thompson 			res->physaddr = page->physaddr + offset;
11502ac6454SAndrew Thompson 		}
11602ac6454SAndrew Thompson 		if (!pc->buffer) {
11702ac6454SAndrew Thompson 			/* Case 1b - Non Kernel Virtual Address */
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson 			res->buffer = USB_ADD_BYTES(page->buffer, offset);
12002ac6454SAndrew Thompson 		}
121bdc081c6SAndrew Thompson 		return;
122bdc081c6SAndrew Thompson 	}
123bdc081c6SAndrew Thompson #endif
12402ac6454SAndrew Thompson 	/* Case 2 - Plain PIO */
12502ac6454SAndrew Thompson 
12602ac6454SAndrew Thompson 	res->buffer = USB_ADD_BYTES(pc->buffer, offset);
1276d917491SHans Petter Selasky 	res->length = (usb_size_t)-1;
128bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA
12902ac6454SAndrew Thompson 	res->physaddr = 0;
130bdc081c6SAndrew Thompson #endif
13102ac6454SAndrew Thompson }
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
134c6b6f546SHans Petter Selasky  *  usb_pc_buffer_is_aligned - verify alignment
135c6b6f546SHans Petter Selasky  *
136c6b6f546SHans Petter Selasky  * This function is used to check if a page cache buffer is properly
137c6b6f546SHans Petter Selasky  * aligned to reduce the use of bounce buffers in PIO mode.
138c6b6f546SHans Petter Selasky  *------------------------------------------------------------------------*/
139c6b6f546SHans Petter Selasky uint8_t
usb_pc_buffer_is_aligned(struct usb_page_cache * pc,usb_frlength_t offset,usb_frlength_t len,usb_frlength_t mask)140c6b6f546SHans Petter Selasky usb_pc_buffer_is_aligned(struct usb_page_cache *pc, usb_frlength_t offset,
141c6b6f546SHans Petter Selasky     usb_frlength_t len, usb_frlength_t mask)
142c6b6f546SHans Petter Selasky {
143c6b6f546SHans Petter Selasky 	struct usb_page_search buf_res;
144c6b6f546SHans Petter Selasky 
145c6b6f546SHans Petter Selasky 	while (len != 0) {
146c6b6f546SHans Petter Selasky 		usbd_get_page(pc, offset, &buf_res);
147c6b6f546SHans Petter Selasky 
148c6b6f546SHans Petter Selasky 		if (buf_res.length > len)
149c6b6f546SHans Petter Selasky 			buf_res.length = len;
150c6b6f546SHans Petter Selasky 		if (USB_P2U(buf_res.buffer) & mask)
151c6b6f546SHans Petter Selasky 			return (0);
152c6b6f546SHans Petter Selasky 		if (buf_res.length & mask)
153c6b6f546SHans Petter Selasky 			return (0);
154c6b6f546SHans Petter Selasky 
155c6b6f546SHans Petter Selasky 		offset += buf_res.length;
156c6b6f546SHans Petter Selasky 		len -= buf_res.length;
157c6b6f546SHans Petter Selasky 	}
158c6b6f546SHans Petter Selasky 	return (1);
159c6b6f546SHans Petter Selasky }
160c6b6f546SHans Petter Selasky 
161c6b6f546SHans Petter Selasky /*------------------------------------------------------------------------*
162a593f6b8SAndrew Thompson  *  usbd_copy_in - copy directly to DMA-able memory
16302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
16402ac6454SAndrew Thompson void
usbd_copy_in(struct usb_page_cache * cache,usb_frlength_t offset,const void * ptr,usb_frlength_t len)165a593f6b8SAndrew Thompson usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset,
166e0a69b51SAndrew Thompson     const void *ptr, usb_frlength_t len)
16702ac6454SAndrew Thompson {
168760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson 	while (len != 0) {
171a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &buf_res);
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 		if (buf_res.length > len) {
17402ac6454SAndrew Thompson 			buf_res.length = len;
17502ac6454SAndrew Thompson 		}
176271ae033SHans Petter Selasky 		memcpy(buf_res.buffer, ptr, buf_res.length);
17702ac6454SAndrew Thompson 
17802ac6454SAndrew Thompson 		offset += buf_res.length;
17902ac6454SAndrew Thompson 		len -= buf_res.length;
18002ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, buf_res.length);
18102ac6454SAndrew Thompson 	}
18202ac6454SAndrew Thompson }
18302ac6454SAndrew Thompson 
18402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
185a593f6b8SAndrew Thompson  *  usbd_copy_in_user - copy directly to DMA-able memory from userland
18602ac6454SAndrew Thompson  *
18702ac6454SAndrew Thompson  * Return values:
18802ac6454SAndrew Thompson  *    0: Success
18902ac6454SAndrew Thompson  * Else: Failure
19002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
191bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
19202ac6454SAndrew Thompson int
usbd_copy_in_user(struct usb_page_cache * cache,usb_frlength_t offset,const void * ptr,usb_frlength_t len)193a593f6b8SAndrew Thompson usbd_copy_in_user(struct usb_page_cache *cache, usb_frlength_t offset,
194e0a69b51SAndrew Thompson     const void *ptr, usb_frlength_t len)
19502ac6454SAndrew Thompson {
196760bc48eSAndrew Thompson 	struct usb_page_search buf_res;
19702ac6454SAndrew Thompson 	int error;
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson 	while (len != 0) {
200a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &buf_res);
20102ac6454SAndrew Thompson 
20202ac6454SAndrew Thompson 		if (buf_res.length > len) {
20302ac6454SAndrew Thompson 			buf_res.length = len;
20402ac6454SAndrew Thompson 		}
20502ac6454SAndrew Thompson 		error = copyin(ptr, buf_res.buffer, buf_res.length);
20602ac6454SAndrew Thompson 		if (error)
20702ac6454SAndrew Thompson 			return (error);
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 		offset += buf_res.length;
21002ac6454SAndrew Thompson 		len -= buf_res.length;
21102ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, buf_res.length);
21202ac6454SAndrew Thompson 	}
21302ac6454SAndrew Thompson 	return (0);			/* success */
21402ac6454SAndrew Thompson }
215bdc081c6SAndrew Thompson #endif
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
218a593f6b8SAndrew Thompson  *  usbd_m_copy_in - copy a mbuf chain directly into DMA-able memory
21902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
220bdc081c6SAndrew Thompson #if USB_HAVE_MBUF
221a593f6b8SAndrew Thompson struct usb_m_copy_in_arg {
222760bc48eSAndrew Thompson 	struct usb_page_cache *cache;
223e0a69b51SAndrew Thompson 	usb_frlength_t dst_offset;
22402ac6454SAndrew Thompson };
22502ac6454SAndrew Thompson 
226578d0effSAndrew Thompson static int
usbd_m_copy_in_cb(void * arg,void * src,uint32_t count)227a593f6b8SAndrew Thompson usbd_m_copy_in_cb(void *arg, void *src, uint32_t count)
22802ac6454SAndrew Thompson {
2293e85b721SEd Maste 	struct usb_m_copy_in_arg *ua = arg;
23002ac6454SAndrew Thompson 
231a593f6b8SAndrew Thompson 	usbd_copy_in(ua->cache, ua->dst_offset, src, count);
23202ac6454SAndrew Thompson 	ua->dst_offset += count;
23302ac6454SAndrew Thompson 	return (0);
23402ac6454SAndrew Thompson }
23502ac6454SAndrew Thompson 
23602ac6454SAndrew Thompson void
usbd_m_copy_in(struct usb_page_cache * cache,usb_frlength_t dst_offset,struct mbuf * m,usb_size_t src_offset,usb_frlength_t src_len)237a593f6b8SAndrew Thompson usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset,
238f9cb546cSAndrew Thompson     struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len)
23902ac6454SAndrew Thompson {
240a593f6b8SAndrew Thompson 	struct usb_m_copy_in_arg arg = {cache, dst_offset};
241bce421e9SHans Petter Selasky 	(void) m_apply(m, src_offset, src_len, &usbd_m_copy_in_cb, &arg);
24202ac6454SAndrew Thompson }
243bdc081c6SAndrew Thompson #endif
24402ac6454SAndrew Thompson 
24502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
246a593f6b8SAndrew Thompson  *  usb_uiomove - factored out code
24702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
248bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
24902ac6454SAndrew Thompson int
usb_uiomove(struct usb_page_cache * pc,struct uio * uio,usb_frlength_t pc_offset,usb_frlength_t len)250a593f6b8SAndrew Thompson usb_uiomove(struct usb_page_cache *pc, struct uio *uio,
251e0a69b51SAndrew Thompson     usb_frlength_t pc_offset, usb_frlength_t len)
25202ac6454SAndrew Thompson {
253760bc48eSAndrew Thompson 	struct usb_page_search res;
25402ac6454SAndrew Thompson 	int error = 0;
25502ac6454SAndrew Thompson 
25602ac6454SAndrew Thompson 	while (len != 0) {
257a593f6b8SAndrew Thompson 		usbd_get_page(pc, pc_offset, &res);
25802ac6454SAndrew Thompson 
25902ac6454SAndrew Thompson 		if (res.length > len) {
26002ac6454SAndrew Thompson 			res.length = len;
26102ac6454SAndrew Thompson 		}
26202ac6454SAndrew Thompson 		/*
26302ac6454SAndrew Thompson 		 * "uiomove()" can sleep so one needs to make a wrapper,
26402ac6454SAndrew Thompson 		 * exiting the mutex and checking things
26502ac6454SAndrew Thompson 		 */
26602ac6454SAndrew Thompson 		error = uiomove(res.buffer, res.length, uio);
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson 		if (error) {
26902ac6454SAndrew Thompson 			break;
27002ac6454SAndrew Thompson 		}
27102ac6454SAndrew Thompson 		pc_offset += res.length;
27202ac6454SAndrew Thompson 		len -= res.length;
27302ac6454SAndrew Thompson 	}
27402ac6454SAndrew Thompson 	return (error);
27502ac6454SAndrew Thompson }
276bdc081c6SAndrew Thompson #endif
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
279a593f6b8SAndrew Thompson  *  usbd_copy_out - copy directly from DMA-able memory
28002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
28102ac6454SAndrew Thompson void
usbd_copy_out(struct usb_page_cache * cache,usb_frlength_t offset,void * ptr,usb_frlength_t len)282a593f6b8SAndrew Thompson usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset,
283e0a69b51SAndrew Thompson     void *ptr, usb_frlength_t len)
28402ac6454SAndrew Thompson {
285760bc48eSAndrew Thompson 	struct usb_page_search res;
28602ac6454SAndrew Thompson 
28702ac6454SAndrew Thompson 	while (len != 0) {
288a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &res);
28902ac6454SAndrew Thompson 
29002ac6454SAndrew Thompson 		if (res.length > len) {
29102ac6454SAndrew Thompson 			res.length = len;
29202ac6454SAndrew Thompson 		}
293271ae033SHans Petter Selasky 		memcpy(ptr, res.buffer, res.length);
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson 		offset += res.length;
29602ac6454SAndrew Thompson 		len -= res.length;
29702ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, res.length);
29802ac6454SAndrew Thompson 	}
29902ac6454SAndrew Thompson }
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
302a593f6b8SAndrew Thompson  *  usbd_copy_out_user - copy directly from DMA-able memory to userland
30302ac6454SAndrew Thompson  *
30402ac6454SAndrew Thompson  * Return values:
30502ac6454SAndrew Thompson  *    0: Success
30602ac6454SAndrew Thompson  * Else: Failure
30702ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
308bdc081c6SAndrew Thompson #if USB_HAVE_USER_IO
30902ac6454SAndrew Thompson int
usbd_copy_out_user(struct usb_page_cache * cache,usb_frlength_t offset,void * ptr,usb_frlength_t len)310a593f6b8SAndrew Thompson usbd_copy_out_user(struct usb_page_cache *cache, usb_frlength_t offset,
311e0a69b51SAndrew Thompson     void *ptr, usb_frlength_t len)
31202ac6454SAndrew Thompson {
313760bc48eSAndrew Thompson 	struct usb_page_search res;
31402ac6454SAndrew Thompson 	int error;
31502ac6454SAndrew Thompson 
31602ac6454SAndrew Thompson 	while (len != 0) {
317a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &res);
31802ac6454SAndrew Thompson 
31902ac6454SAndrew Thompson 		if (res.length > len) {
32002ac6454SAndrew Thompson 			res.length = len;
32102ac6454SAndrew Thompson 		}
32202ac6454SAndrew Thompson 		error = copyout(res.buffer, ptr, res.length);
32302ac6454SAndrew Thompson 		if (error)
32402ac6454SAndrew Thompson 			return (error);
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 		offset += res.length;
32702ac6454SAndrew Thompson 		len -= res.length;
32802ac6454SAndrew Thompson 		ptr = USB_ADD_BYTES(ptr, res.length);
32902ac6454SAndrew Thompson 	}
33002ac6454SAndrew Thompson 	return (0);			/* success */
33102ac6454SAndrew Thompson }
332bdc081c6SAndrew Thompson #endif
33302ac6454SAndrew Thompson 
33402ac6454SAndrew Thompson /*------------------------------------------------------------------------*
335a593f6b8SAndrew Thompson  *  usbd_frame_zero - zero DMA-able memory
33602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
33702ac6454SAndrew Thompson void
usbd_frame_zero(struct usb_page_cache * cache,usb_frlength_t offset,usb_frlength_t len)338a593f6b8SAndrew Thompson usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset,
339e0a69b51SAndrew Thompson     usb_frlength_t len)
34002ac6454SAndrew Thompson {
341760bc48eSAndrew Thompson 	struct usb_page_search res;
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	while (len != 0) {
344a593f6b8SAndrew Thompson 		usbd_get_page(cache, offset, &res);
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 		if (res.length > len) {
34702ac6454SAndrew Thompson 			res.length = len;
34802ac6454SAndrew Thompson 		}
349271ae033SHans Petter Selasky 		memset(res.buffer, 0, res.length);
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson 		offset += res.length;
35202ac6454SAndrew Thompson 		len -= res.length;
35302ac6454SAndrew Thompson 	}
35402ac6454SAndrew Thompson }
35502ac6454SAndrew Thompson 
3563e873976SAndrew Thompson #if USB_HAVE_BUSDMA
35702ac6454SAndrew Thompson 
35802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
359a593f6b8SAndrew Thompson  *	usb_dma_lock_cb - dummy callback
36002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
36102ac6454SAndrew Thompson static void
usb_dma_lock_cb(void * arg,bus_dma_lock_op_t op)362a593f6b8SAndrew Thompson usb_dma_lock_cb(void *arg, bus_dma_lock_op_t op)
36302ac6454SAndrew Thompson {
36402ac6454SAndrew Thompson 	/* we use "mtx_owned()" instead of this function */
36502ac6454SAndrew Thompson }
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
368a593f6b8SAndrew Thompson  *	usb_dma_tag_create - allocate a DMA tag
36902ac6454SAndrew Thompson  *
37002ac6454SAndrew Thompson  * NOTE: If the "align" parameter has a value of 1 the DMA-tag will
37102ac6454SAndrew Thompson  * allow multi-segment mappings. Else all mappings are single-segment.
37202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
37302ac6454SAndrew Thompson static void
usb_dma_tag_create(struct usb_dma_tag * udt,usb_size_t size,usb_size_t align)374a593f6b8SAndrew Thompson usb_dma_tag_create(struct usb_dma_tag *udt,
375f9cb546cSAndrew Thompson     usb_size_t size, usb_size_t align)
37602ac6454SAndrew Thompson {
37702ac6454SAndrew Thompson 	bus_dma_tag_t tag;
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson 	if (bus_dma_tag_create
38002ac6454SAndrew Thompson 	    ( /* parent    */ udt->tag_parent->tag,
38102ac6454SAndrew Thompson 	     /* alignment */ align,
3821a7c3e90SHans Petter Selasky 	     /* boundary  */ 0,
38302ac6454SAndrew Thompson 	     /* lowaddr   */ (2ULL << (udt->tag_parent->dma_bits - 1)) - 1,
38402ac6454SAndrew Thompson 	     /* highaddr  */ BUS_SPACE_MAXADDR,
38502ac6454SAndrew Thompson 	     /* filter    */ NULL,
38602ac6454SAndrew Thompson 	     /* filterarg */ NULL,
38702ac6454SAndrew Thompson 	     /* maxsize   */ size,
3886c3c4d71SMarius Strobl 	     /* nsegments */ (align == 1 && size > 1) ?
38902ac6454SAndrew Thompson 	    (2 + (size / USB_PAGE_SIZE)) : 1,
3906c3c4d71SMarius Strobl 	     /* maxsegsz  */ (align == 1 && size > USB_PAGE_SIZE) ?
39102ac6454SAndrew Thompson 	    USB_PAGE_SIZE : size,
39202ac6454SAndrew Thompson 	     /* flags     */ BUS_DMA_KEEP_PG_OFFSET,
393a593f6b8SAndrew Thompson 	     /* lockfn    */ &usb_dma_lock_cb,
39402ac6454SAndrew Thompson 	     /* lockarg   */ NULL,
39502ac6454SAndrew Thompson 	    &tag)) {
39602ac6454SAndrew Thompson 		tag = NULL;
39702ac6454SAndrew Thompson 	}
39802ac6454SAndrew Thompson 	udt->tag = tag;
39902ac6454SAndrew Thompson }
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
402a593f6b8SAndrew Thompson  *	usb_dma_tag_free - free a DMA tag
40302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
40402ac6454SAndrew Thompson static void
usb_dma_tag_destroy(struct usb_dma_tag * udt)405a593f6b8SAndrew Thompson usb_dma_tag_destroy(struct usb_dma_tag *udt)
40602ac6454SAndrew Thompson {
40702ac6454SAndrew Thompson 	bus_dma_tag_destroy(udt->tag);
40802ac6454SAndrew Thompson }
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
411a593f6b8SAndrew Thompson  *	usb_pc_alloc_mem_cb - BUS-DMA callback function
41202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
41302ac6454SAndrew Thompson static void
usb_pc_alloc_mem_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)414a593f6b8SAndrew Thompson usb_pc_alloc_mem_cb(void *arg, bus_dma_segment_t *segs,
41502ac6454SAndrew Thompson     int nseg, int error)
41602ac6454SAndrew Thompson {
417a593f6b8SAndrew Thompson 	usb_pc_common_mem_cb(arg, segs, nseg, error, 0);
41802ac6454SAndrew Thompson }
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
421a593f6b8SAndrew Thompson  *	usb_pc_load_mem_cb - BUS-DMA callback function
42202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
42302ac6454SAndrew Thompson static void
usb_pc_load_mem_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error)424a593f6b8SAndrew Thompson usb_pc_load_mem_cb(void *arg, bus_dma_segment_t *segs,
42502ac6454SAndrew Thompson     int nseg, int error)
42602ac6454SAndrew Thompson {
427a593f6b8SAndrew Thompson 	usb_pc_common_mem_cb(arg, segs, nseg, error, 1);
42802ac6454SAndrew Thompson }
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
431a593f6b8SAndrew Thompson  *	usb_pc_common_mem_cb - BUS-DMA callback function
43202ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
43302ac6454SAndrew Thompson static void
usb_pc_common_mem_cb(void * arg,bus_dma_segment_t * segs,int nseg,int error,uint8_t isload)434a593f6b8SAndrew Thompson usb_pc_common_mem_cb(void *arg, bus_dma_segment_t *segs,
43502ac6454SAndrew Thompson     int nseg, int error, uint8_t isload)
43602ac6454SAndrew Thompson {
437760bc48eSAndrew Thompson 	struct usb_dma_parent_tag *uptag;
438760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
439760bc48eSAndrew Thompson 	struct usb_page *pg;
440f9cb546cSAndrew Thompson 	usb_size_t rem;
4411a7c3e90SHans Petter Selasky 	bus_size_t off;
44202ac6454SAndrew Thompson 	uint8_t owned;
44302ac6454SAndrew Thompson 
44402ac6454SAndrew Thompson 	pc = arg;
44502ac6454SAndrew Thompson 	uptag = pc->tag_parent;
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 	/*
44802ac6454SAndrew Thompson 	 * XXX There is sometimes recursive locking here.
44902ac6454SAndrew Thompson 	 * XXX We should try to find a better solution.
45002ac6454SAndrew Thompson 	 * XXX Until further the "owned" variable does
45102ac6454SAndrew Thompson 	 * XXX the trick.
45202ac6454SAndrew Thompson 	 */
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	if (error) {
45502ac6454SAndrew Thompson 		goto done;
45602ac6454SAndrew Thompson 	}
4571a7c3e90SHans Petter Selasky 
4581a7c3e90SHans Petter Selasky 	off = 0;
45902ac6454SAndrew Thompson 	pg = pc->page_start;
460d9c9c81cSPedro F. Giffuni 	pg->physaddr = rounddown2(segs->ds_addr, USB_PAGE_SIZE);
46102ac6454SAndrew Thompson 	rem = segs->ds_addr & (USB_PAGE_SIZE - 1);
46202ac6454SAndrew Thompson 	pc->page_offset_buf = rem;
46302ac6454SAndrew Thompson 	pc->page_offset_end += rem;
464ed6d949aSAndrew Thompson #ifdef USB_DEBUG
465288edd19SHans Petter Selasky 	if (nseg > 1) {
466288edd19SHans Petter Selasky 		int x;
467288edd19SHans Petter Selasky 
468288edd19SHans Petter Selasky 		for (x = 0; x != nseg - 1; x++) {
469288edd19SHans Petter Selasky 			if (((segs[x].ds_addr + segs[x].ds_len) & (USB_PAGE_SIZE - 1)) ==
470288edd19SHans Petter Selasky 			    ((segs[x + 1].ds_addr & (USB_PAGE_SIZE - 1))))
471288edd19SHans Petter Selasky 				continue;
47202ac6454SAndrew Thompson 			/*
473288edd19SHans Petter Selasky 			 * This check verifies there is no page offset
474288edd19SHans Petter Selasky 			 * hole between any of the segments. See the
475a2aa9bebSHans Petter Selasky 			 * BUS_DMA_KEEP_PG_OFFSET flag.
47602ac6454SAndrew Thompson 			 */
477767cb2e2SAndrew Thompson 			DPRINTFN(0, "Page offset was not preserved\n");
47802ac6454SAndrew Thompson 			error = 1;
47902ac6454SAndrew Thompson 			goto done;
48002ac6454SAndrew Thompson 		}
481288edd19SHans Petter Selasky 	}
48202ac6454SAndrew Thompson #endif
4835fb878a9SHans Petter Selasky 	while (pc->ismultiseg) {
4841a7c3e90SHans Petter Selasky 		off += USB_PAGE_SIZE;
4851a7c3e90SHans Petter Selasky 		if (off >= (segs->ds_len + rem)) {
4861a7c3e90SHans Petter Selasky 			/* page crossing */
48702ac6454SAndrew Thompson 			nseg--;
48802ac6454SAndrew Thompson 			segs++;
4891a7c3e90SHans Petter Selasky 			off = 0;
4901a7c3e90SHans Petter Selasky 			rem = 0;
491b7b11d00SHans Petter Selasky 			if (nseg == 0)
492b7b11d00SHans Petter Selasky 				break;
4931a7c3e90SHans Petter Selasky 		}
49402ac6454SAndrew Thompson 		pg++;
495d9c9c81cSPedro F. Giffuni 		pg->physaddr = rounddown2(segs->ds_addr + off, USB_PAGE_SIZE);
49602ac6454SAndrew Thompson 	}
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson done:
49902ac6454SAndrew Thompson 	owned = mtx_owned(uptag->mtx);
50002ac6454SAndrew Thompson 	if (!owned)
5010eb8d462SHans Petter Selasky 		USB_MTX_LOCK(uptag->mtx);
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	uptag->dma_error = (error ? 1 : 0);
50402ac6454SAndrew Thompson 	if (isload) {
50502ac6454SAndrew Thompson 		(uptag->func) (uptag);
50602ac6454SAndrew Thompson 	} else {
5078437751dSAndrew Thompson 		cv_broadcast(uptag->cv);
50802ac6454SAndrew Thompson 	}
50902ac6454SAndrew Thompson 	if (!owned)
5100eb8d462SHans Petter Selasky 		USB_MTX_UNLOCK(uptag->mtx);
51102ac6454SAndrew Thompson }
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
514a593f6b8SAndrew Thompson  *	usb_pc_alloc_mem - allocate DMA'able memory
51502ac6454SAndrew Thompson  *
51602ac6454SAndrew Thompson  * Returns:
51702ac6454SAndrew Thompson  *    0: Success
51802ac6454SAndrew Thompson  * Else: Failure
51902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
52002ac6454SAndrew Thompson uint8_t
usb_pc_alloc_mem(struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)521a593f6b8SAndrew Thompson usb_pc_alloc_mem(struct usb_page_cache *pc, struct usb_page *pg,
522f9cb546cSAndrew Thompson     usb_size_t size, usb_size_t align)
52302ac6454SAndrew Thompson {
524760bc48eSAndrew Thompson 	struct usb_dma_parent_tag *uptag;
525760bc48eSAndrew Thompson 	struct usb_dma_tag *utag;
52602ac6454SAndrew Thompson 	bus_dmamap_t map;
52702ac6454SAndrew Thompson 	void *ptr;
52802ac6454SAndrew Thompson 	int err;
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 	uptag = pc->tag_parent;
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	if (align != 1) {
53302ac6454SAndrew Thompson 		/*
53402ac6454SAndrew Thompson 	         * The alignment must be greater or equal to the
53502ac6454SAndrew Thompson 	         * "size" else the object can be split between two
53602ac6454SAndrew Thompson 	         * memory pages and we get a problem!
53702ac6454SAndrew Thompson 	         */
53802ac6454SAndrew Thompson 		while (align < size) {
53902ac6454SAndrew Thompson 			align *= 2;
54002ac6454SAndrew Thompson 			if (align == 0) {
54102ac6454SAndrew Thompson 				goto error;
54202ac6454SAndrew Thompson 			}
54302ac6454SAndrew Thompson 		}
54402ac6454SAndrew Thompson #if 1
54502ac6454SAndrew Thompson 		/*
54602ac6454SAndrew Thompson 		 * XXX BUS-DMA workaround - FIXME later:
54702ac6454SAndrew Thompson 		 *
548dfd37770SGordon Bergling 		 * We assume that the alignment at this point of
54902ac6454SAndrew Thompson 		 * the code is greater than or equal to the size and
55002ac6454SAndrew Thompson 		 * less than two times the size, so that if we double
55102ac6454SAndrew Thompson 		 * the size, the size will be greater than the
55202ac6454SAndrew Thompson 		 * alignment.
55302ac6454SAndrew Thompson 		 *
55402ac6454SAndrew Thompson 		 * The bus-dma system has a check for "alignment"
55502ac6454SAndrew Thompson 		 * being less than "size". If that check fails we end
55602ac6454SAndrew Thompson 		 * up using contigmalloc which is page based even for
55702ac6454SAndrew Thompson 		 * small allocations. Try to avoid that to save
55802ac6454SAndrew Thompson 		 * memory, hence we sometimes to a large number of
55902ac6454SAndrew Thompson 		 * small allocations!
56002ac6454SAndrew Thompson 		 */
56102ac6454SAndrew Thompson 		if (size <= (USB_PAGE_SIZE / 2)) {
56202ac6454SAndrew Thompson 			size *= 2;
56302ac6454SAndrew Thompson 		}
56402ac6454SAndrew Thompson #endif
56502ac6454SAndrew Thompson 	}
56602ac6454SAndrew Thompson 	/* get the correct DMA tag */
567a593f6b8SAndrew Thompson 	utag = usb_dma_tag_find(uptag, size, align);
56802ac6454SAndrew Thompson 	if (utag == NULL) {
56902ac6454SAndrew Thompson 		goto error;
57002ac6454SAndrew Thompson 	}
57102ac6454SAndrew Thompson 	/* allocate memory */
57202ac6454SAndrew Thompson 	if (bus_dmamem_alloc(
57302ac6454SAndrew Thompson 	    utag->tag, &ptr, (BUS_DMA_WAITOK | BUS_DMA_COHERENT), &map)) {
57402ac6454SAndrew Thompson 		goto error;
57502ac6454SAndrew Thompson 	}
57602ac6454SAndrew Thompson 	/* setup page cache */
57702ac6454SAndrew Thompson 	pc->buffer = ptr;
57802ac6454SAndrew Thompson 	pc->page_start = pg;
57902ac6454SAndrew Thompson 	pc->page_offset_buf = 0;
58002ac6454SAndrew Thompson 	pc->page_offset_end = size;
58102ac6454SAndrew Thompson 	pc->map = map;
58202ac6454SAndrew Thompson 	pc->tag = utag->tag;
58302ac6454SAndrew Thompson 	pc->ismultiseg = (align == 1);
58402ac6454SAndrew Thompson 
5850eb8d462SHans Petter Selasky 	USB_MTX_LOCK(uptag->mtx);
58602ac6454SAndrew Thompson 
58702ac6454SAndrew Thompson 	/* load memory into DMA */
58802ac6454SAndrew Thompson 	err = bus_dmamap_load(
589a593f6b8SAndrew Thompson 	    utag->tag, map, ptr, size, &usb_pc_alloc_mem_cb,
59002ac6454SAndrew Thompson 	    pc, (BUS_DMA_WAITOK | BUS_DMA_COHERENT));
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 	if (err == EINPROGRESS) {
5938437751dSAndrew Thompson 		cv_wait(uptag->cv, uptag->mtx);
59402ac6454SAndrew Thompson 		err = 0;
59502ac6454SAndrew Thompson 	}
5960eb8d462SHans Petter Selasky 	USB_MTX_UNLOCK(uptag->mtx);
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 	if (err || uptag->dma_error) {
59902ac6454SAndrew Thompson 		bus_dmamem_free(utag->tag, ptr, map);
60002ac6454SAndrew Thompson 		goto error;
60102ac6454SAndrew Thompson 	}
602dc91a971SIan Lepore 	pc->isloaded = 1;
603271ae033SHans Petter Selasky 	memset(ptr, 0, size);
60402ac6454SAndrew Thompson 
605a593f6b8SAndrew Thompson 	usb_pc_cpu_flush(pc);
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson 	return (0);
60802ac6454SAndrew Thompson 
60902ac6454SAndrew Thompson error:
61002ac6454SAndrew Thompson 	/* reset most of the page cache */
61102ac6454SAndrew Thompson 	pc->buffer = NULL;
61202ac6454SAndrew Thompson 	pc->page_start = NULL;
61302ac6454SAndrew Thompson 	pc->page_offset_buf = 0;
61402ac6454SAndrew Thompson 	pc->page_offset_end = 0;
615dc91a971SIan Lepore 	pc->isloaded = 0;
61602ac6454SAndrew Thompson 	pc->map = NULL;
61702ac6454SAndrew Thompson 	pc->tag = NULL;
61802ac6454SAndrew Thompson 	return (1);
61902ac6454SAndrew Thompson }
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson /*------------------------------------------------------------------------*
622a593f6b8SAndrew Thompson  *	usb_pc_free_mem - free DMA memory
62302ac6454SAndrew Thompson  *
62402ac6454SAndrew Thompson  * This function is NULL safe.
62502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
62602ac6454SAndrew Thompson void
usb_pc_free_mem(struct usb_page_cache * pc)627a593f6b8SAndrew Thompson usb_pc_free_mem(struct usb_page_cache *pc)
62802ac6454SAndrew Thompson {
62902ac6454SAndrew Thompson 	if (pc && pc->buffer) {
630dc91a971SIan Lepore 		if (pc->isloaded)
63102ac6454SAndrew Thompson 			bus_dmamap_unload(pc->tag, pc->map);
63202ac6454SAndrew Thompson 
63302ac6454SAndrew Thompson 		bus_dmamem_free(pc->tag, pc->buffer, pc->map);
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson 		pc->buffer = NULL;
636dc91a971SIan Lepore 		pc->isloaded = 0;
63702ac6454SAndrew Thompson 	}
63802ac6454SAndrew Thompson }
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
641a593f6b8SAndrew Thompson  *	usb_pc_load_mem - load virtual memory into DMA
64202ac6454SAndrew Thompson  *
64302ac6454SAndrew Thompson  * Return values:
64402ac6454SAndrew Thompson  * 0: Success
64502ac6454SAndrew Thompson  * Else: Error
64602ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
64702ac6454SAndrew Thompson uint8_t
usb_pc_load_mem(struct usb_page_cache * pc,usb_size_t size,uint8_t sync)648a593f6b8SAndrew Thompson usb_pc_load_mem(struct usb_page_cache *pc, usb_size_t size, uint8_t sync)
64902ac6454SAndrew Thompson {
65002ac6454SAndrew Thompson 	/* setup page cache */
65102ac6454SAndrew Thompson 	pc->page_offset_buf = 0;
65202ac6454SAndrew Thompson 	pc->page_offset_end = size;
65302ac6454SAndrew Thompson 	pc->ismultiseg = 1;
65402ac6454SAndrew Thompson 
6550eb8d462SHans Petter Selasky 	USB_MTX_ASSERT(pc->tag_parent->mtx, MA_OWNED);
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson 	if (size > 0) {
65802ac6454SAndrew Thompson 		if (sync) {
659760bc48eSAndrew Thompson 			struct usb_dma_parent_tag *uptag;
66002ac6454SAndrew Thompson 			int err;
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson 			uptag = pc->tag_parent;
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 			/*
66502ac6454SAndrew Thompson 			 * We have to unload the previous loaded DMA
66602ac6454SAndrew Thompson 			 * pages before trying to load a new one!
66702ac6454SAndrew Thompson 			 */
668dc91a971SIan Lepore 			if (pc->isloaded)
66902ac6454SAndrew Thompson 				bus_dmamap_unload(pc->tag, pc->map);
67002ac6454SAndrew Thompson 
67102ac6454SAndrew Thompson 			/*
67202ac6454SAndrew Thompson 			 * Try to load memory into DMA.
67302ac6454SAndrew Thompson 			 */
67402ac6454SAndrew Thompson 			err = bus_dmamap_load(
67502ac6454SAndrew Thompson 			    pc->tag, pc->map, pc->buffer, size,
676a593f6b8SAndrew Thompson 			    &usb_pc_alloc_mem_cb, pc, BUS_DMA_WAITOK);
67702ac6454SAndrew Thompson 			if (err == EINPROGRESS) {
6788437751dSAndrew Thompson 				cv_wait(uptag->cv, uptag->mtx);
67902ac6454SAndrew Thompson 				err = 0;
68002ac6454SAndrew Thompson 			}
68102ac6454SAndrew Thompson 			if (err || uptag->dma_error) {
682dc91a971SIan Lepore 				pc->isloaded = 0;
68302ac6454SAndrew Thompson 				return (1);
68402ac6454SAndrew Thompson 			}
68502ac6454SAndrew Thompson 		} else {
68602ac6454SAndrew Thompson 			/*
68702ac6454SAndrew Thompson 			 * We have to unload the previous loaded DMA
68802ac6454SAndrew Thompson 			 * pages before trying to load a new one!
68902ac6454SAndrew Thompson 			 */
690dc91a971SIan Lepore 			if (pc->isloaded)
69102ac6454SAndrew Thompson 				bus_dmamap_unload(pc->tag, pc->map);
69202ac6454SAndrew Thompson 
69302ac6454SAndrew Thompson 			/*
69402ac6454SAndrew Thompson 			 * Try to load memory into DMA. The callback
69502ac6454SAndrew Thompson 			 * will be called in all cases:
69602ac6454SAndrew Thompson 			 */
69702ac6454SAndrew Thompson 			if (bus_dmamap_load(
69802ac6454SAndrew Thompson 			    pc->tag, pc->map, pc->buffer, size,
699a593f6b8SAndrew Thompson 			    &usb_pc_load_mem_cb, pc, BUS_DMA_WAITOK)) {
70002ac6454SAndrew Thompson 			}
70102ac6454SAndrew Thompson 		}
702dc91a971SIan Lepore 		pc->isloaded = 1;
70302ac6454SAndrew Thompson 	} else {
70402ac6454SAndrew Thompson 		if (!sync) {
70502ac6454SAndrew Thompson 			/*
70602ac6454SAndrew Thompson 			 * Call callback so that refcount is decremented
70702ac6454SAndrew Thompson 			 * properly:
70802ac6454SAndrew Thompson 			 */
70902ac6454SAndrew Thompson 			pc->tag_parent->dma_error = 0;
71002ac6454SAndrew Thompson 			(pc->tag_parent->func) (pc->tag_parent);
71102ac6454SAndrew Thompson 		}
71202ac6454SAndrew Thompson 	}
71302ac6454SAndrew Thompson 	return (0);
71402ac6454SAndrew Thompson }
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
717a593f6b8SAndrew Thompson  *	usb_pc_cpu_invalidate - invalidate CPU cache
71802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
71902ac6454SAndrew Thompson void
usb_pc_cpu_invalidate(struct usb_page_cache * pc)720a593f6b8SAndrew Thompson usb_pc_cpu_invalidate(struct usb_page_cache *pc)
72102ac6454SAndrew Thompson {
72202ac6454SAndrew Thompson 	if (pc->page_offset_end == pc->page_offset_buf) {
72302ac6454SAndrew Thompson 		/* nothing has been loaded into this page cache! */
72402ac6454SAndrew Thompson 		return;
72502ac6454SAndrew Thompson 	}
726f2db024fSAlfred Perlstein 
727f2db024fSAlfred Perlstein 	/*
728f2db024fSAlfred Perlstein 	 * TODO: We currently do XXX_POSTREAD and XXX_PREREAD at the
729f2db024fSAlfred Perlstein 	 * same time, but in the future we should try to isolate the
730f2db024fSAlfred Perlstein 	 * different cases to optimise the code. --HPS
731f2db024fSAlfred Perlstein 	 */
73235d01728SRafal Jaworowski 	bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_POSTREAD);
73335d01728SRafal Jaworowski 	bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREREAD);
73402ac6454SAndrew Thompson }
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
737a593f6b8SAndrew Thompson  *	usb_pc_cpu_flush - flush CPU cache
73802ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
73902ac6454SAndrew Thompson void
usb_pc_cpu_flush(struct usb_page_cache * pc)740a593f6b8SAndrew Thompson usb_pc_cpu_flush(struct usb_page_cache *pc)
74102ac6454SAndrew Thompson {
74202ac6454SAndrew Thompson 	if (pc->page_offset_end == pc->page_offset_buf) {
74302ac6454SAndrew Thompson 		/* nothing has been loaded into this page cache! */
74402ac6454SAndrew Thompson 		return;
74502ac6454SAndrew Thompson 	}
74635d01728SRafal Jaworowski 	bus_dmamap_sync(pc->tag, pc->map, BUS_DMASYNC_PREWRITE);
74702ac6454SAndrew Thompson }
74802ac6454SAndrew Thompson 
74902ac6454SAndrew Thompson /*------------------------------------------------------------------------*
750a593f6b8SAndrew Thompson  *	usb_pc_dmamap_create - create a DMA map
75102ac6454SAndrew Thompson  *
75202ac6454SAndrew Thompson  * Returns:
75302ac6454SAndrew Thompson  *    0: Success
75402ac6454SAndrew Thompson  * Else: Failure
75502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
75602ac6454SAndrew Thompson uint8_t
usb_pc_dmamap_create(struct usb_page_cache * pc,usb_size_t size)757a593f6b8SAndrew Thompson usb_pc_dmamap_create(struct usb_page_cache *pc, usb_size_t size)
75802ac6454SAndrew Thompson {
759760bc48eSAndrew Thompson 	struct usb_xfer_root *info;
760760bc48eSAndrew Thompson 	struct usb_dma_tag *utag;
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson 	/* get info */
763bdc081c6SAndrew Thompson 	info = USB_DMATAG_TO_XROOT(pc->tag_parent);
76402ac6454SAndrew Thompson 
76502ac6454SAndrew Thompson 	/* sanity check */
76602ac6454SAndrew Thompson 	if (info == NULL) {
76702ac6454SAndrew Thompson 		goto error;
76802ac6454SAndrew Thompson 	}
769a593f6b8SAndrew Thompson 	utag = usb_dma_tag_find(pc->tag_parent, size, 1);
77002ac6454SAndrew Thompson 	if (utag == NULL) {
77102ac6454SAndrew Thompson 		goto error;
77202ac6454SAndrew Thompson 	}
77302ac6454SAndrew Thompson 	/* create DMA map */
77402ac6454SAndrew Thompson 	if (bus_dmamap_create(utag->tag, 0, &pc->map)) {
77502ac6454SAndrew Thompson 		goto error;
77602ac6454SAndrew Thompson 	}
77702ac6454SAndrew Thompson 	pc->tag = utag->tag;
77802ac6454SAndrew Thompson 	return 0;			/* success */
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson error:
78102ac6454SAndrew Thompson 	pc->map = NULL;
78202ac6454SAndrew Thompson 	pc->tag = NULL;
78302ac6454SAndrew Thompson 	return 1;			/* failure */
78402ac6454SAndrew Thompson }
78502ac6454SAndrew Thompson 
78602ac6454SAndrew Thompson /*------------------------------------------------------------------------*
787a593f6b8SAndrew Thompson  *	usb_pc_dmamap_destroy
78802ac6454SAndrew Thompson  *
78902ac6454SAndrew Thompson  * This function is NULL safe.
79002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
79102ac6454SAndrew Thompson void
usb_pc_dmamap_destroy(struct usb_page_cache * pc)792a593f6b8SAndrew Thompson usb_pc_dmamap_destroy(struct usb_page_cache *pc)
79302ac6454SAndrew Thompson {
79402ac6454SAndrew Thompson 	if (pc && pc->tag) {
795dc91a971SIan Lepore 		if (pc->isloaded)
796dc91a971SIan Lepore 			bus_dmamap_unload(pc->tag, pc->map);
79702ac6454SAndrew Thompson 		bus_dmamap_destroy(pc->tag, pc->map);
79802ac6454SAndrew Thompson 		pc->tag = NULL;
79902ac6454SAndrew Thompson 		pc->map = NULL;
80002ac6454SAndrew Thompson 	}
80102ac6454SAndrew Thompson }
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
804a593f6b8SAndrew Thompson  *	usb_dma_tag_find - factored out code
80502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
806760bc48eSAndrew Thompson struct usb_dma_tag *
usb_dma_tag_find(struct usb_dma_parent_tag * udpt,usb_size_t size,usb_size_t align)807a593f6b8SAndrew Thompson usb_dma_tag_find(struct usb_dma_parent_tag *udpt,
808f9cb546cSAndrew Thompson     usb_size_t size, usb_size_t align)
80902ac6454SAndrew Thompson {
810760bc48eSAndrew Thompson 	struct usb_dma_tag *udt;
81102ac6454SAndrew Thompson 	uint8_t nudt;
81202ac6454SAndrew Thompson 
813767cb2e2SAndrew Thompson 	USB_ASSERT(align > 0, ("Invalid parameter align = 0\n"));
814767cb2e2SAndrew Thompson 	USB_ASSERT(size > 0, ("Invalid parameter size = 0\n"));
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson 	udt = udpt->utag_first;
81702ac6454SAndrew Thompson 	nudt = udpt->utag_max;
81802ac6454SAndrew Thompson 
81902ac6454SAndrew Thompson 	while (nudt--) {
82002ac6454SAndrew Thompson 		if (udt->align == 0) {
821a593f6b8SAndrew Thompson 			usb_dma_tag_create(udt, size, align);
82202ac6454SAndrew Thompson 			if (udt->tag == NULL) {
82302ac6454SAndrew Thompson 				return (NULL);
82402ac6454SAndrew Thompson 			}
82502ac6454SAndrew Thompson 			udt->align = align;
82602ac6454SAndrew Thompson 			udt->size = size;
82702ac6454SAndrew Thompson 			return (udt);
82802ac6454SAndrew Thompson 		}
82902ac6454SAndrew Thompson 		if ((udt->align == align) && (udt->size == size)) {
83002ac6454SAndrew Thompson 			return (udt);
83102ac6454SAndrew Thompson 		}
83202ac6454SAndrew Thompson 		udt++;
83302ac6454SAndrew Thompson 	}
83402ac6454SAndrew Thompson 	return (NULL);
83502ac6454SAndrew Thompson }
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson /*------------------------------------------------------------------------*
838a593f6b8SAndrew Thompson  *	usb_dma_tag_setup - initialise USB DMA tags
83902ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
84002ac6454SAndrew Thompson void
usb_dma_tag_setup(struct usb_dma_parent_tag * udpt,struct usb_dma_tag * udt,bus_dma_tag_t dmat,struct mtx * mtx,usb_dma_callback_t * func,uint8_t ndmabits,uint8_t nudt)841a593f6b8SAndrew Thompson usb_dma_tag_setup(struct usb_dma_parent_tag *udpt,
842760bc48eSAndrew Thompson     struct usb_dma_tag *udt, bus_dma_tag_t dmat,
843e0a69b51SAndrew Thompson     struct mtx *mtx, usb_dma_callback_t *func,
844bdc081c6SAndrew Thompson     uint8_t ndmabits, uint8_t nudt)
84502ac6454SAndrew Thompson {
846271ae033SHans Petter Selasky 	memset(udpt, 0, sizeof(*udpt));
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 	/* sanity checking */
84902ac6454SAndrew Thompson 	if ((nudt == 0) ||
85002ac6454SAndrew Thompson 	    (ndmabits == 0) ||
85102ac6454SAndrew Thompson 	    (mtx == NULL)) {
85202ac6454SAndrew Thompson 		/* something is corrupt */
85302ac6454SAndrew Thompson 		return;
85402ac6454SAndrew Thompson 	}
85502ac6454SAndrew Thompson 	/* initialise condition variable */
8568437751dSAndrew Thompson 	cv_init(udpt->cv, "USB DMA CV");
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 	/* store some information */
85902ac6454SAndrew Thompson 	udpt->mtx = mtx;
86002ac6454SAndrew Thompson 	udpt->func = func;
86102ac6454SAndrew Thompson 	udpt->tag = dmat;
86202ac6454SAndrew Thompson 	udpt->utag_first = udt;
86302ac6454SAndrew Thompson 	udpt->utag_max = nudt;
86402ac6454SAndrew Thompson 	udpt->dma_bits = ndmabits;
86502ac6454SAndrew Thompson 
86602ac6454SAndrew Thompson 	while (nudt--) {
867271ae033SHans Petter Selasky 		memset(udt, 0, sizeof(*udt));
86802ac6454SAndrew Thompson 		udt->tag_parent = udpt;
86902ac6454SAndrew Thompson 		udt++;
87002ac6454SAndrew Thompson 	}
87102ac6454SAndrew Thompson }
87202ac6454SAndrew Thompson 
87302ac6454SAndrew Thompson /*------------------------------------------------------------------------*
874a593f6b8SAndrew Thompson  *	usb_bus_tag_unsetup - factored out code
87502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
87602ac6454SAndrew Thompson void
usb_dma_tag_unsetup(struct usb_dma_parent_tag * udpt)877a593f6b8SAndrew Thompson usb_dma_tag_unsetup(struct usb_dma_parent_tag *udpt)
87802ac6454SAndrew Thompson {
879760bc48eSAndrew Thompson 	struct usb_dma_tag *udt;
88002ac6454SAndrew Thompson 	uint8_t nudt;
88102ac6454SAndrew Thompson 
88202ac6454SAndrew Thompson 	udt = udpt->utag_first;
88302ac6454SAndrew Thompson 	nudt = udpt->utag_max;
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson 	while (nudt--) {
88602ac6454SAndrew Thompson 		if (udt->align) {
88702ac6454SAndrew Thompson 			/* destroy the USB DMA tag */
888a593f6b8SAndrew Thompson 			usb_dma_tag_destroy(udt);
88902ac6454SAndrew Thompson 			udt->align = 0;
89002ac6454SAndrew Thompson 		}
89102ac6454SAndrew Thompson 		udt++;
89202ac6454SAndrew Thompson 	}
89302ac6454SAndrew Thompson 
89402ac6454SAndrew Thompson 	if (udpt->utag_max) {
89502ac6454SAndrew Thompson 		/* destroy the condition variable */
8968437751dSAndrew Thompson 		cv_destroy(udpt->cv);
89702ac6454SAndrew Thompson 	}
89802ac6454SAndrew Thompson }
89902ac6454SAndrew Thompson 
90002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
901a593f6b8SAndrew Thompson  *	usb_bdma_work_loop
90202ac6454SAndrew Thompson  *
90302ac6454SAndrew Thompson  * This function handles loading of virtual buffers into DMA and is
90402ac6454SAndrew Thompson  * only called when "dma_refcount" is zero.
90502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
90602ac6454SAndrew Thompson void
usb_bdma_work_loop(struct usb_xfer_queue * pq)907a593f6b8SAndrew Thompson usb_bdma_work_loop(struct usb_xfer_queue *pq)
90802ac6454SAndrew Thompson {
909760bc48eSAndrew Thompson 	struct usb_xfer_root *info;
910760bc48eSAndrew Thompson 	struct usb_xfer *xfer;
911e0a69b51SAndrew Thompson 	usb_frcount_t nframes;
91202ac6454SAndrew Thompson 
91302ac6454SAndrew Thompson 	xfer = pq->curr;
91402ac6454SAndrew Thompson 	info = xfer->xroot;
91502ac6454SAndrew Thompson 
9160eb8d462SHans Petter Selasky 	USB_MTX_ASSERT(info->xfer_mtx, MA_OWNED);
91702ac6454SAndrew Thompson 
91802ac6454SAndrew Thompson 	if (xfer->error) {
91902ac6454SAndrew Thompson 		/* some error happened */
92002ac6454SAndrew Thompson 		USB_BUS_LOCK(info->bus);
921a593f6b8SAndrew Thompson 		usbd_transfer_done(xfer, 0);
92202ac6454SAndrew Thompson 		USB_BUS_UNLOCK(info->bus);
92302ac6454SAndrew Thompson 		return;
92402ac6454SAndrew Thompson 	}
92502ac6454SAndrew Thompson 	if (!xfer->flags_int.bdma_setup) {
926760bc48eSAndrew Thompson 		struct usb_page *pg;
927e0a69b51SAndrew Thompson 		usb_frlength_t frlength_0;
92802ac6454SAndrew Thompson 		uint8_t isread;
92902ac6454SAndrew Thompson 
93002ac6454SAndrew Thompson 		xfer->flags_int.bdma_setup = 1;
93102ac6454SAndrew Thompson 
93202ac6454SAndrew Thompson 		/* reset BUS-DMA load state */
93302ac6454SAndrew Thompson 
93402ac6454SAndrew Thompson 		info->dma_error = 0;
93502ac6454SAndrew Thompson 
93602ac6454SAndrew Thompson 		if (xfer->flags_int.isochronous_xfr) {
93702ac6454SAndrew Thompson 			/* only one frame buffer */
93802ac6454SAndrew Thompson 			nframes = 1;
93902ac6454SAndrew Thompson 			frlength_0 = xfer->sumlen;
94002ac6454SAndrew Thompson 		} else {
94102ac6454SAndrew Thompson 			/* can be multiple frame buffers */
94202ac6454SAndrew Thompson 			nframes = xfer->nframes;
94302ac6454SAndrew Thompson 			frlength_0 = xfer->frlengths[0];
94402ac6454SAndrew Thompson 		}
94502ac6454SAndrew Thompson 
94602ac6454SAndrew Thompson 		/*
94702ac6454SAndrew Thompson 		 * Set DMA direction first. This is needed to
94802ac6454SAndrew Thompson 		 * select the correct cache invalidate and cache
94902ac6454SAndrew Thompson 		 * flush operations.
95002ac6454SAndrew Thompson 		 */
95102ac6454SAndrew Thompson 		isread = USB_GET_DATA_ISREAD(xfer);
95202ac6454SAndrew Thompson 		pg = xfer->dma_page_ptr;
95302ac6454SAndrew Thompson 
95402ac6454SAndrew Thompson 		if (xfer->flags_int.control_xfr &&
95502ac6454SAndrew Thompson 		    xfer->flags_int.control_hdr) {
95602ac6454SAndrew Thompson 			/* special case */
957f29a0724SAndrew Thompson 			if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
95802ac6454SAndrew Thompson 				/* The device controller writes to memory */
95902ac6454SAndrew Thompson 				xfer->frbuffers[0].isread = 1;
96002ac6454SAndrew Thompson 			} else {
96102ac6454SAndrew Thompson 				/* The host controller reads from memory */
96202ac6454SAndrew Thompson 				xfer->frbuffers[0].isread = 0;
96302ac6454SAndrew Thompson 			}
96402ac6454SAndrew Thompson 		} else {
96502ac6454SAndrew Thompson 			/* default case */
96602ac6454SAndrew Thompson 			xfer->frbuffers[0].isread = isread;
96702ac6454SAndrew Thompson 		}
96802ac6454SAndrew Thompson 
96902ac6454SAndrew Thompson 		/*
97002ac6454SAndrew Thompson 		 * Setup the "page_start" pointer which points to an array of
97102ac6454SAndrew Thompson 		 * USB pages where information about the physical address of a
97202ac6454SAndrew Thompson 		 * page will be stored. Also initialise the "isread" field of
97302ac6454SAndrew Thompson 		 * the USB page caches.
97402ac6454SAndrew Thompson 		 */
97502ac6454SAndrew Thompson 		xfer->frbuffers[0].page_start = pg;
97602ac6454SAndrew Thompson 
97702ac6454SAndrew Thompson 		info->dma_nframes = nframes;
97802ac6454SAndrew Thompson 		info->dma_currframe = 0;
97902ac6454SAndrew Thompson 		info->dma_frlength_0 = frlength_0;
98002ac6454SAndrew Thompson 
98102ac6454SAndrew Thompson 		pg += (frlength_0 / USB_PAGE_SIZE);
98202ac6454SAndrew Thompson 		pg += 2;
98302ac6454SAndrew Thompson 
98402ac6454SAndrew Thompson 		while (--nframes > 0) {
98502ac6454SAndrew Thompson 			xfer->frbuffers[nframes].isread = isread;
98602ac6454SAndrew Thompson 			xfer->frbuffers[nframes].page_start = pg;
98702ac6454SAndrew Thompson 
98802ac6454SAndrew Thompson 			pg += (xfer->frlengths[nframes] / USB_PAGE_SIZE);
98902ac6454SAndrew Thompson 			pg += 2;
99002ac6454SAndrew Thompson 		}
99102ac6454SAndrew Thompson 	}
99202ac6454SAndrew Thompson 	if (info->dma_error) {
99302ac6454SAndrew Thompson 		USB_BUS_LOCK(info->bus);
994a593f6b8SAndrew Thompson 		usbd_transfer_done(xfer, USB_ERR_DMA_LOAD_FAILED);
99502ac6454SAndrew Thompson 		USB_BUS_UNLOCK(info->bus);
99602ac6454SAndrew Thompson 		return;
99702ac6454SAndrew Thompson 	}
99802ac6454SAndrew Thompson 	if (info->dma_currframe != info->dma_nframes) {
99902ac6454SAndrew Thompson 		if (info->dma_currframe == 0) {
100002ac6454SAndrew Thompson 			/* special case */
1001a593f6b8SAndrew Thompson 			usb_pc_load_mem(xfer->frbuffers,
100202ac6454SAndrew Thompson 			    info->dma_frlength_0, 0);
100302ac6454SAndrew Thompson 		} else {
100402ac6454SAndrew Thompson 			/* default case */
100502ac6454SAndrew Thompson 			nframes = info->dma_currframe;
1006a593f6b8SAndrew Thompson 			usb_pc_load_mem(xfer->frbuffers + nframes,
100702ac6454SAndrew Thompson 			    xfer->frlengths[nframes], 0);
100802ac6454SAndrew Thompson 		}
100902ac6454SAndrew Thompson 
101002ac6454SAndrew Thompson 		/* advance frame index */
101102ac6454SAndrew Thompson 		info->dma_currframe++;
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson 		return;
101402ac6454SAndrew Thompson 	}
101502ac6454SAndrew Thompson 	/* go ahead */
1016a593f6b8SAndrew Thompson 	usb_bdma_pre_sync(xfer);
101702ac6454SAndrew Thompson 
101802ac6454SAndrew Thompson 	/* start loading next USB transfer, if any */
1019a593f6b8SAndrew Thompson 	usb_command_wrapper(pq, NULL);
102002ac6454SAndrew Thompson 
102102ac6454SAndrew Thompson 	/* finally start the hardware */
1022a593f6b8SAndrew Thompson 	usbd_pipe_enter(xfer);
102302ac6454SAndrew Thompson }
102402ac6454SAndrew Thompson 
102502ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1026a593f6b8SAndrew Thompson  *	usb_bdma_done_event
102702ac6454SAndrew Thompson  *
102802ac6454SAndrew Thompson  * This function is called when the BUS-DMA has loaded virtual memory
102902ac6454SAndrew Thompson  * into DMA, if any.
103002ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
103102ac6454SAndrew Thompson void
usb_bdma_done_event(struct usb_dma_parent_tag * udpt)1032a593f6b8SAndrew Thompson usb_bdma_done_event(struct usb_dma_parent_tag *udpt)
103302ac6454SAndrew Thompson {
1034760bc48eSAndrew Thompson 	struct usb_xfer_root *info;
103502ac6454SAndrew Thompson 
1036bdc081c6SAndrew Thompson 	info = USB_DMATAG_TO_XROOT(udpt);
103702ac6454SAndrew Thompson 
10380eb8d462SHans Petter Selasky 	USB_MTX_ASSERT(info->xfer_mtx, MA_OWNED);
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	/* copy error */
104102ac6454SAndrew Thompson 	info->dma_error = udpt->dma_error;
104202ac6454SAndrew Thompson 
104302ac6454SAndrew Thompson 	/* enter workloop again */
1044a593f6b8SAndrew Thompson 	usb_command_wrapper(&info->dma_q,
104502ac6454SAndrew Thompson 	    info->dma_q.curr);
104602ac6454SAndrew Thompson }
104702ac6454SAndrew Thompson 
104802ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1049a593f6b8SAndrew Thompson  *	usb_bdma_pre_sync
105002ac6454SAndrew Thompson  *
105102ac6454SAndrew Thompson  * This function handles DMA synchronisation that must be done before
105202ac6454SAndrew Thompson  * an USB transfer is started.
105302ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
105402ac6454SAndrew Thompson void
usb_bdma_pre_sync(struct usb_xfer * xfer)1055a593f6b8SAndrew Thompson usb_bdma_pre_sync(struct usb_xfer *xfer)
105602ac6454SAndrew Thompson {
1057760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
1058e0a69b51SAndrew Thompson 	usb_frcount_t nframes;
105902ac6454SAndrew Thompson 
106002ac6454SAndrew Thompson 	if (xfer->flags_int.isochronous_xfr) {
106102ac6454SAndrew Thompson 		/* only one frame buffer */
106202ac6454SAndrew Thompson 		nframes = 1;
106302ac6454SAndrew Thompson 	} else {
106402ac6454SAndrew Thompson 		/* can be multiple frame buffers */
106502ac6454SAndrew Thompson 		nframes = xfer->nframes;
106602ac6454SAndrew Thompson 	}
106702ac6454SAndrew Thompson 
106802ac6454SAndrew Thompson 	pc = xfer->frbuffers;
106902ac6454SAndrew Thompson 
107002ac6454SAndrew Thompson 	while (nframes--) {
107102ac6454SAndrew Thompson 		if (pc->isread) {
1072a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(pc);
107302ac6454SAndrew Thompson 		} else {
1074a593f6b8SAndrew Thompson 			usb_pc_cpu_flush(pc);
107502ac6454SAndrew Thompson 		}
107602ac6454SAndrew Thompson 		pc++;
107702ac6454SAndrew Thompson 	}
107802ac6454SAndrew Thompson }
107902ac6454SAndrew Thompson 
108002ac6454SAndrew Thompson /*------------------------------------------------------------------------*
1081a593f6b8SAndrew Thompson  *	usb_bdma_post_sync
108202ac6454SAndrew Thompson  *
108302ac6454SAndrew Thompson  * This function handles DMA synchronisation that must be done after
108402ac6454SAndrew Thompson  * an USB transfer is complete.
108502ac6454SAndrew Thompson  *------------------------------------------------------------------------*/
108602ac6454SAndrew Thompson void
usb_bdma_post_sync(struct usb_xfer * xfer)1087a593f6b8SAndrew Thompson usb_bdma_post_sync(struct usb_xfer *xfer)
108802ac6454SAndrew Thompson {
1089760bc48eSAndrew Thompson 	struct usb_page_cache *pc;
1090e0a69b51SAndrew Thompson 	usb_frcount_t nframes;
109102ac6454SAndrew Thompson 
109202ac6454SAndrew Thompson 	if (xfer->flags_int.isochronous_xfr) {
109302ac6454SAndrew Thompson 		/* only one frame buffer */
109402ac6454SAndrew Thompson 		nframes = 1;
109502ac6454SAndrew Thompson 	} else {
109602ac6454SAndrew Thompson 		/* can be multiple frame buffers */
109702ac6454SAndrew Thompson 		nframes = xfer->nframes;
109802ac6454SAndrew Thompson 	}
109902ac6454SAndrew Thompson 
110002ac6454SAndrew Thompson 	pc = xfer->frbuffers;
110102ac6454SAndrew Thompson 
110202ac6454SAndrew Thompson 	while (nframes--) {
110302ac6454SAndrew Thompson 		if (pc->isread) {
1104a593f6b8SAndrew Thompson 			usb_pc_cpu_invalidate(pc);
110502ac6454SAndrew Thompson 		}
110602ac6454SAndrew Thompson 		pc++;
110702ac6454SAndrew Thompson 	}
110802ac6454SAndrew Thompson }
1109bdc081c6SAndrew Thompson 
1110bdc081c6SAndrew Thompson #endif
1111