Home | History | Annotate | Line # | Download | only in usb
usb_mem.c revision 1.72
      1  1.72       mrg /*	$NetBSD: usb_mem.c,v 1.72 2019/08/28 07:11:19 mrg Exp $	*/
      2   1.1  augustss 
      3   1.1  augustss /*
      4   1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1  augustss  * All rights reserved.
      6   1.1  augustss  *
      7   1.2  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8  1.20  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9   1.2  augustss  * Carlstedt Research & Technology.
     10   1.1  augustss  *
     11   1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12   1.1  augustss  * modification, are permitted provided that the following conditions
     13   1.1  augustss  * are met:
     14   1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15   1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16   1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18   1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19   1.1  augustss  *
     20   1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1  augustss  */
     32   1.1  augustss 
     33   1.1  augustss /*
     34   1.1  augustss  * USB DMA memory allocation.
     35   1.1  augustss  * We need to allocate a lot of small (many 8 byte, some larger)
     36   1.1  augustss  * memory blocks that can be used for DMA.  Using the bus_dma
     37   1.3  augustss  * routines directly would incur large overheads in space and time.
     38   1.1  augustss  */
     39  1.22     lukem 
     40  1.22     lukem #include <sys/cdefs.h>
     41  1.72       mrg __KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.72 2019/08/28 07:11:19 mrg Exp $");
     42  1.65     skrll 
     43  1.65     skrll #ifdef _KERNEL_OPT
     44  1.65     skrll #include "opt_usb.h"
     45  1.65     skrll #endif
     46   1.1  augustss 
     47   1.1  augustss #include <sys/param.h>
     48   1.1  augustss #include <sys/systm.h>
     49   1.1  augustss #include <sys/kernel.h>
     50  1.55     prlw1 #include <sys/kmem.h>
     51   1.1  augustss #include <sys/queue.h>
     52  1.12  augustss #include <sys/device.h>		/* for usbdivar.h */
     53  1.33        ad #include <sys/bus.h>
     54  1.41      matt #include <sys/cpu.h>
     55  1.53       mrg #include <sys/once.h>
     56  1.53       mrg 
     57   1.3  augustss #ifdef DIAGNOSTIC
     58   1.3  augustss #include <sys/proc.h>
     59   1.3  augustss #endif
     60   1.3  augustss 
     61   1.1  augustss #include <dev/usb/usb.h>
     62   1.1  augustss #include <dev/usb/usbdi.h>
     63  1.12  augustss #include <dev/usb/usbdivar.h>	/* just for usb_dma_t */
     64   1.1  augustss #include <dev/usb/usb_mem.h>
     65  1.67     skrll #include <dev/usb/usbhist.h>
     66   1.1  augustss 
     67  1.67     skrll #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
     68  1.67     skrll #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
     69  1.26   thorpej 
     70  1.56      matt #define USB_MEM_SMALL roundup(64, CACHE_LINE_SIZE)
     71   1.1  augustss #define USB_MEM_CHUNKS 64
     72   1.1  augustss #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
     73   1.1  augustss 
     74   1.1  augustss /* This struct is overlayed on free fragments. */
     75   1.1  augustss struct usb_frag_dma {
     76  1.67     skrll 	usb_dma_block_t		*ufd_block;
     77  1.67     skrll 	u_int			ufd_offs;
     78  1.67     skrll 	LIST_ENTRY(usb_frag_dma) ufd_next;
     79   1.1  augustss };
     80   1.1  augustss 
     81  1.21  augustss Static usbd_status	usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
     82  1.55     prlw1 					   usb_dma_block_t **, bool);
     83  1.21  augustss Static void		usb_block_freemem(usb_dma_block_t *);
     84   1.1  augustss 
     85  1.48      matt LIST_HEAD(usb_dma_block_qh, usb_dma_block);
     86  1.48      matt Static struct usb_dma_block_qh usb_blk_freelist =
     87   1.1  augustss 	LIST_HEAD_INITIALIZER(usb_blk_freelist);
     88  1.53       mrg kmutex_t usb_blk_lock;
     89  1.53       mrg 
     90  1.48      matt #ifdef DEBUG
     91  1.48      matt Static struct usb_dma_block_qh usb_blk_fraglist =
     92  1.48      matt 	LIST_HEAD_INITIALIZER(usb_blk_fraglist);
     93  1.48      matt Static struct usb_dma_block_qh usb_blk_fulllist =
     94  1.48      matt 	LIST_HEAD_INITIALIZER(usb_blk_fulllist);
     95  1.48      matt #endif
     96  1.48      matt Static u_int usb_blk_nfree = 0;
     97  1.11  augustss /* XXX should have different free list for different tags (for speed) */
     98  1.19  augustss Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
     99   1.1  augustss 	LIST_HEAD_INITIALIZER(usb_frag_freelist);
    100   1.1  augustss 
    101  1.53       mrg Static int usb_mem_init(void);
    102  1.53       mrg 
    103  1.53       mrg Static int
    104  1.53       mrg usb_mem_init(void)
    105  1.53       mrg {
    106  1.53       mrg 
    107  1.53       mrg 	mutex_init(&usb_blk_lock, MUTEX_DEFAULT, IPL_NONE);
    108  1.53       mrg 	return 0;
    109  1.53       mrg }
    110  1.53       mrg 
    111  1.19  augustss Static usbd_status
    112  1.21  augustss usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
    113  1.55     prlw1 		   usb_dma_block_t **dmap, bool multiseg)
    114   1.1  augustss {
    115  1.48      matt 	usb_dma_block_t *b;
    116   1.1  augustss 	int error;
    117   1.1  augustss 
    118  1.71       mrg 	USBHIST_FUNC();
    119  1.71       mrg 	USBHIST_CALLARGS(usbdebug, "size=%ju align=%ju", size, align, 0, 0);
    120   1.3  augustss 
    121  1.67     skrll 	ASSERT_SLEEPABLE();
    122  1.67     skrll 	KASSERT(size != 0);
    123  1.53       mrg 	KASSERT(mutex_owned(&usb_blk_lock));
    124  1.53       mrg 
    125   1.1  augustss 	/* First check the free list. */
    126  1.48      matt 	LIST_FOREACH(b, &usb_blk_freelist, next) {
    127  1.55     prlw1 		/* Don't allocate multiple segments to unwilling callers */
    128  1.55     prlw1 		if (b->nsegs != 1 && !multiseg)
    129  1.55     prlw1 			continue;
    130  1.48      matt 		if (b->tag == tag && b->size >= size && b->align >= align) {
    131  1.48      matt 			LIST_REMOVE(b, next);
    132  1.18  augustss 			usb_blk_nfree--;
    133  1.48      matt 			*dmap = b;
    134  1.70  pgoyette 			DPRINTFN(6, "free list size=%ju", b->size, 0, 0, 0);
    135  1.67     skrll 			return USBD_NORMAL_COMPLETION;
    136   1.1  augustss 		}
    137   1.1  augustss 	}
    138   1.9  augustss 
    139  1.72       mrg 	DPRINTFN(6, "no freelist entry", 0, 0, 0, 0);
    140  1.67     skrll 	mutex_exit(&usb_blk_lock);
    141  1.67     skrll 
    142  1.67     skrll 	b = kmem_zalloc(sizeof(*b), KM_SLEEP);
    143  1.48      matt 	b->tag = tag;
    144  1.48      matt 	b->size = size;
    145  1.48      matt 	b->align = align;
    146  1.55     prlw1 
    147  1.55     prlw1 	if (!multiseg)
    148  1.55     prlw1 		/* Caller wants one segment */
    149  1.55     prlw1 		b->nsegs = 1;
    150  1.58  christos 	else
    151  1.58  christos 		b->nsegs = (size + (PAGE_SIZE-1)) / PAGE_SIZE;
    152  1.55     prlw1 
    153  1.55     prlw1 	b->segs = kmem_alloc(b->nsegs * sizeof(*b->segs), KM_SLEEP);
    154  1.59  jmcneill 	b->nsegs_alloc = b->nsegs;
    155  1.55     prlw1 
    156  1.48      matt 	error = bus_dmamem_alloc(tag, b->size, align, 0,
    157  1.55     prlw1 				 b->segs, b->nsegs,
    158  1.67     skrll 				 &b->nsegs, BUS_DMA_WAITOK);
    159   1.1  augustss 	if (error)
    160  1.27  augustss 		goto free0;
    161   1.1  augustss 
    162  1.48      matt 	error = bus_dmamem_map(tag, b->segs, b->nsegs, b->size,
    163  1.67     skrll 			       &b->kaddr, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    164   1.1  augustss 	if (error)
    165  1.27  augustss 		goto free1;
    166   1.1  augustss 
    167  1.55     prlw1 	error = bus_dmamap_create(tag, b->size, b->nsegs, b->size,
    168  1.67     skrll 				  0, BUS_DMA_WAITOK, &b->map);
    169   1.1  augustss 	if (error)
    170   1.1  augustss 		goto unmap;
    171   1.1  augustss 
    172  1.48      matt 	error = bus_dmamap_load(tag, b->map, b->kaddr, b->size, NULL,
    173  1.67     skrll 				BUS_DMA_WAITOK);
    174   1.1  augustss 	if (error)
    175   1.1  augustss 		goto destroy;
    176  1.27  augustss 
    177  1.48      matt 	*dmap = b;
    178  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    179  1.48      matt 	memset(b->kaddr, 0, b->size);
    180  1.41      matt #endif
    181  1.67     skrll 	mutex_enter(&usb_blk_lock);
    182  1.55     prlw1 
    183  1.67     skrll 	return USBD_NORMAL_COMPLETION;
    184   1.1  augustss 
    185  1.27  augustss  destroy:
    186  1.48      matt 	bus_dmamap_destroy(tag, b->map);
    187  1.27  augustss  unmap:
    188  1.48      matt 	bus_dmamem_unmap(tag, b->kaddr, b->size);
    189  1.27  augustss  free1:
    190  1.48      matt 	bus_dmamem_free(tag, b->segs, b->nsegs);
    191  1.27  augustss  free0:
    192  1.59  jmcneill 	kmem_free(b->segs, b->nsegs_alloc * sizeof(*b->segs));
    193  1.67     skrll 	kmem_free(b, sizeof(*b));
    194  1.67     skrll 	mutex_enter(&usb_blk_lock);
    195  1.67     skrll 
    196  1.67     skrll 	return USBD_NOMEM;
    197   1.1  augustss }
    198   1.1  augustss 
    199  1.11  augustss #if 0
    200   1.1  augustss void
    201  1.48      matt usb_block_real_freemem(usb_dma_block_t *b)
    202   1.1  augustss {
    203   1.3  augustss #ifdef DIAGNOSTIC
    204  1.64   mlelstv 	if (cpu_softintr_p() || cpu_intr_p()) {
    205   1.3  augustss 		printf("usb_block_real_freemem: in interrupt context\n");
    206   1.3  augustss 		return;
    207   1.3  augustss 	}
    208   1.3  augustss #endif
    209  1.48      matt 	bus_dmamap_unload(b->tag, b->map);
    210  1.48      matt 	bus_dmamap_destroy(b->tag, b->map);
    211  1.48      matt 	bus_dmamem_unmap(b->tag, b->kaddr, b->size);
    212  1.48      matt 	bus_dmamem_free(b->tag, b->segs, b->nsegs);
    213  1.59  jmcneill 	kmem_free(b->segs, b->nsegs_alloc * sizeof(*b->segs));
    214  1.67     skrll 	kmem_free(b, sizeof(*b));
    215   1.1  augustss }
    216  1.11  augustss #endif
    217   1.1  augustss 
    218  1.48      matt #ifdef DEBUG
    219  1.49     rmind static bool
    220  1.48      matt usb_valid_block_p(usb_dma_block_t *b, struct usb_dma_block_qh *qh)
    221  1.48      matt {
    222  1.48      matt 	usb_dma_block_t *xb;
    223  1.48      matt 	LIST_FOREACH(xb, qh, next) {
    224  1.48      matt 		if (xb == b)
    225  1.48      matt 			return true;
    226  1.48      matt 	}
    227  1.48      matt 	return false;
    228  1.48      matt }
    229  1.48      matt #endif
    230  1.48      matt 
    231   1.1  augustss /*
    232   1.1  augustss  * Do not free the memory unconditionally since we might be called
    233   1.1  augustss  * from an interrupt context and that is BAD.
    234   1.4  augustss  * XXX when should we really free?
    235   1.1  augustss  */
    236  1.19  augustss Static void
    237  1.48      matt usb_block_freemem(usb_dma_block_t *b)
    238   1.1  augustss {
    239  1.71       mrg 	USBHIST_FUNC();
    240  1.71       mrg 	USBHIST_CALLARGS(usbdebug, "size=%ju", b->size, 0, 0, 0);
    241  1.53       mrg 
    242  1.53       mrg 	KASSERT(mutex_owned(&usb_blk_lock));
    243   1.4  augustss 
    244  1.48      matt #ifdef DEBUG
    245  1.48      matt 	LIST_REMOVE(b, next);
    246  1.48      matt #endif
    247  1.48      matt 	LIST_INSERT_HEAD(&usb_blk_freelist, b, next);
    248  1.18  augustss 	usb_blk_nfree++;
    249   1.1  augustss }
    250   1.1  augustss 
    251   1.1  augustss usbd_status
    252  1.67     skrll usb_allocmem(struct usbd_bus *bus, size_t size, size_t align, usb_dma_t *p)
    253   1.1  augustss {
    254  1.67     skrll 
    255  1.55     prlw1 	return usb_allocmem_flags(bus, size, align, p, 0);
    256  1.55     prlw1 }
    257  1.55     prlw1 
    258  1.55     prlw1 usbd_status
    259  1.67     skrll usb_allocmem_flags(struct usbd_bus *bus, size_t size, size_t align, usb_dma_t *p,
    260  1.55     prlw1 		   int flags)
    261  1.55     prlw1 {
    262  1.67     skrll 	bus_dma_tag_t tag = bus->ub_dmatag;
    263  1.16  augustss 	usbd_status err;
    264   1.1  augustss 	struct usb_frag_dma *f;
    265   1.1  augustss 	usb_dma_block_t *b;
    266   1.1  augustss 	int i;
    267  1.53       mrg 	static ONCE_DECL(init_control);
    268  1.55     prlw1 	bool frag;
    269  1.53       mrg 
    270  1.67     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    271  1.67     skrll 
    272  1.67     skrll 	ASSERT_SLEEPABLE();
    273  1.67     skrll 
    274  1.53       mrg 	RUN_ONCE(&init_control, usb_mem_init);
    275   1.1  augustss 
    276  1.55     prlw1 	frag = (flags & USBMALLOC_MULTISEG);
    277  1.55     prlw1 
    278   1.1  augustss 	/* If the request is large then just use a full block. */
    279   1.1  augustss 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
    280  1.70  pgoyette 		DPRINTFN(1, "large alloc %jd", size, 0, 0, 0);
    281   1.1  augustss 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
    282  1.53       mrg 		mutex_enter(&usb_blk_lock);
    283  1.67     skrll 		err = usb_block_allocmem(tag, size, align, &p->udma_block, frag);
    284  1.16  augustss 		if (!err) {
    285  1.48      matt #ifdef DEBUG
    286  1.67     skrll 			LIST_INSERT_HEAD(&usb_blk_fulllist, p->udma_block, next);
    287  1.48      matt #endif
    288  1.67     skrll 			p->udma_block->flags = USB_DMA_FULLBLOCK;
    289  1.67     skrll 			p->udma_offs = 0;
    290   1.1  augustss 		}
    291  1.53       mrg 		mutex_exit(&usb_blk_lock);
    292  1.67     skrll 		return err;
    293   1.1  augustss 	}
    294  1.24  augustss 
    295  1.53       mrg 	mutex_enter(&usb_blk_lock);
    296   1.1  augustss 	/* Check for free fragments. */
    297  1.67     skrll 	LIST_FOREACH(f, &usb_frag_freelist, ufd_next) {
    298  1.67     skrll 		KDASSERTMSG(usb_valid_block_p(f->ufd_block, &usb_blk_fraglist),
    299  1.50       jym 		    "%s: usb frag %p: unknown block pointer %p",
    300  1.67     skrll 		    __func__, f, f->ufd_block);
    301  1.67     skrll 		if (f->ufd_block->tag == tag)
    302   1.1  augustss 			break;
    303  1.41      matt 	}
    304  1.16  augustss 	if (f == NULL) {
    305  1.67     skrll 		DPRINTFN(1, "adding fragments", 0, 0, 0, 0);
    306  1.55     prlw1 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL, &b,
    307  1.55     prlw1 					 false);
    308  1.16  augustss 		if (err) {
    309  1.53       mrg 			mutex_exit(&usb_blk_lock);
    310  1.67     skrll 			return err;
    311   1.5  augustss 		}
    312  1.48      matt #ifdef DEBUG
    313  1.48      matt 		LIST_INSERT_HEAD(&usb_blk_fraglist, b, next);
    314  1.48      matt #endif
    315  1.28      fvdl 		b->flags = 0;
    316   1.1  augustss 		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
    317  1.32  christos 			f = (struct usb_frag_dma *)((char *)b->kaddr + i);
    318  1.67     skrll 			f->ufd_block = b;
    319  1.67     skrll 			f->ufd_offs = i;
    320  1.67     skrll 			LIST_INSERT_HEAD(&usb_frag_freelist, f, ufd_next);
    321  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    322  1.41      matt 			i += 1 * USB_MEM_SMALL;
    323  1.41      matt #endif
    324   1.1  augustss 		}
    325   1.1  augustss 		f = LIST_FIRST(&usb_frag_freelist);
    326   1.1  augustss 	}
    327  1.67     skrll 	p->udma_block = f->ufd_block;
    328  1.67     skrll 	p->udma_offs = f->ufd_offs;
    329  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    330  1.68     skrll 	p->udma_offs += USB_MEM_SMALL;
    331  1.41      matt #endif
    332  1.67     skrll 	LIST_REMOVE(f, ufd_next);
    333  1.53       mrg 	mutex_exit(&usb_blk_lock);
    334  1.70  pgoyette 	DPRINTFN(5, "use frag=%#jx size=%jd", (uintptr_t)f, size, 0, 0);
    335  1.55     prlw1 
    336  1.67     skrll 	return USBD_NORMAL_COMPLETION;
    337   1.1  augustss }
    338   1.1  augustss 
    339   1.1  augustss void
    340  1.67     skrll usb_freemem(struct usbd_bus *bus, usb_dma_t *p)
    341   1.1  augustss {
    342   1.1  augustss 	struct usb_frag_dma *f;
    343   1.1  augustss 
    344  1.67     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    345  1.67     skrll 
    346  1.53       mrg 	mutex_enter(&usb_blk_lock);
    347  1.67     skrll 	if (p->udma_block->flags & USB_DMA_FULLBLOCK) {
    348  1.67     skrll 		KDASSERTMSG(usb_valid_block_p(p->udma_block, &usb_blk_fulllist),
    349  1.50       jym 		    "%s: dma %p: invalid block pointer %p",
    350  1.67     skrll 		    __func__, p, p->udma_block);
    351  1.67     skrll 		DPRINTFN(1, "large free", 0, 0, 0, 0);
    352  1.67     skrll 		usb_block_freemem(p->udma_block);
    353  1.53       mrg 		mutex_exit(&usb_blk_lock);
    354   1.1  augustss 		return;
    355   1.1  augustss 	}
    356  1.67     skrll 	KDASSERTMSG(usb_valid_block_p(p->udma_block, &usb_blk_fraglist),
    357  1.50       jym 	    "%s: dma %p: invalid block pointer %p",
    358  1.67     skrll 	    __func__, p, p->udma_block);
    359  1.41      matt 	//usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD);
    360  1.23  augustss 	f = KERNADDR(p, 0);
    361  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    362  1.41      matt 	f = (void *)((uintptr_t)f - USB_MEM_SMALL);
    363  1.41      matt #endif
    364  1.67     skrll 	f->ufd_block = p->udma_block;
    365  1.67     skrll 	f->ufd_offs = p->udma_offs;
    366  1.42      matt #ifdef USB_FRAG_DMA_WORKAROUND
    367  1.68     skrll 	f->ufd_offs -= USB_MEM_SMALL;
    368  1.41      matt #endif
    369  1.67     skrll 	LIST_INSERT_HEAD(&usb_frag_freelist, f, ufd_next);
    370  1.53       mrg 	mutex_exit(&usb_blk_lock);
    371  1.70  pgoyette 	DPRINTFN(5, "frag=%#jx", (uintptr_t)f, 0, 0, 0);
    372   1.1  augustss }
    373  1.28      fvdl 
    374  1.55     prlw1 bus_addr_t
    375  1.55     prlw1 usb_dmaaddr(usb_dma_t *dma, unsigned int offset)
    376  1.55     prlw1 {
    377  1.55     prlw1 	unsigned int i;
    378  1.55     prlw1 	bus_size_t seg_offs;
    379  1.55     prlw1 
    380  1.67     skrll 	offset += dma->udma_offs;
    381  1.55     prlw1 
    382  1.67     skrll 	KASSERTMSG(offset < dma->udma_block->size, "offset %d vs %zu", offset,
    383  1.67     skrll 	    dma->udma_block->size);
    384  1.55     prlw1 
    385  1.67     skrll 	if (dma->udma_block->nsegs == 1) {
    386  1.67     skrll 		KASSERT(dma->udma_block->map->dm_segs[0].ds_len > offset);
    387  1.67     skrll 		return dma->udma_block->map->dm_segs[0].ds_addr + offset;
    388  1.55     prlw1 	}
    389  1.55     prlw1 
    390  1.67     skrll 	/*
    391  1.67     skrll 	 * Search for a bus_segment_t corresponding to this offset. With no
    392  1.55     prlw1 	 * record of the offset in the map to a particular dma_segment_t, we
    393  1.55     prlw1 	 * have to iterate from the start of the list each time. Could be
    394  1.67     skrll 	 * improved
    395  1.67     skrll 	 */
    396  1.55     prlw1 	seg_offs = 0;
    397  1.67     skrll 	for (i = 0; i < dma->udma_block->nsegs; i++) {
    398  1.67     skrll 		if (seg_offs + dma->udma_block->map->dm_segs[i].ds_len > offset)
    399  1.55     prlw1 			break;
    400  1.55     prlw1 
    401  1.67     skrll 		seg_offs += dma->udma_block->map->dm_segs[i].ds_len;
    402  1.55     prlw1 	}
    403  1.55     prlw1 
    404  1.67     skrll 	KASSERT(i != dma->udma_block->nsegs);
    405  1.55     prlw1 	offset -= seg_offs;
    406  1.67     skrll 	return dma->udma_block->map->dm_segs[i].ds_addr + offset;
    407  1.55     prlw1 }
    408  1.55     prlw1 
    409  1.37    bouyer void
    410  1.37    bouyer usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
    411  1.37    bouyer {
    412  1.67     skrll 
    413  1.67     skrll 	bus_dmamap_sync(p->udma_block->tag, p->udma_block->map, p->udma_offs + offset,
    414  1.37    bouyer 	    len, ops);
    415  1.37    bouyer }
    416