Home | History | Annotate | Line # | Download | only in usb
usb_mem.c revision 1.78
      1  1.78  jmcneill /*	$NetBSD: usb_mem.c,v 1.78 2021/01/02 12:39:03 jmcneill 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.78  jmcneill __KERNEL_RCSID(0, "$NetBSD: usb_mem.c,v 1.78 2021/01/02 12:39:03 jmcneill 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.74     skrll #include <sys/bus.h>
     49  1.75     skrll #include <sys/cpu.h>
     50  1.74     skrll #include <sys/device.h>		/* for usbdivar.h */
     51   1.1  augustss #include <sys/kernel.h>
     52  1.55     prlw1 #include <sys/kmem.h>
     53  1.74     skrll #include <sys/once.h>
     54   1.1  augustss #include <sys/queue.h>
     55  1.74     skrll #include <sys/systm.h>
     56  1.53       mrg 
     57   1.1  augustss #include <dev/usb/usb.h>
     58   1.1  augustss #include <dev/usb/usbdi.h>
     59  1.12  augustss #include <dev/usb/usbdivar.h>	/* just for usb_dma_t */
     60  1.74     skrll #include <dev/usb/usbhist.h>
     61   1.1  augustss #include <dev/usb/usb_mem.h>
     62   1.1  augustss 
     63  1.67     skrll #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
     64  1.67     skrll #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
     65  1.26   thorpej 
     66  1.56      matt #define USB_MEM_SMALL roundup(64, CACHE_LINE_SIZE)
     67   1.1  augustss #define USB_MEM_CHUNKS 64
     68   1.1  augustss #define USB_MEM_BLOCK (USB_MEM_SMALL * USB_MEM_CHUNKS)
     69   1.1  augustss 
     70   1.1  augustss /* This struct is overlayed on free fragments. */
     71   1.1  augustss struct usb_frag_dma {
     72  1.67     skrll 	usb_dma_block_t		*ufd_block;
     73  1.67     skrll 	u_int			ufd_offs;
     74  1.67     skrll 	LIST_ENTRY(usb_frag_dma) ufd_next;
     75   1.1  augustss };
     76   1.1  augustss 
     77  1.21  augustss Static usbd_status	usb_block_allocmem(bus_dma_tag_t, size_t, size_t,
     78  1.76     skrll 			    u_int, usb_dma_block_t **);
     79  1.21  augustss Static void		usb_block_freemem(usb_dma_block_t *);
     80   1.1  augustss 
     81  1.48      matt LIST_HEAD(usb_dma_block_qh, usb_dma_block);
     82  1.48      matt Static struct usb_dma_block_qh usb_blk_freelist =
     83   1.1  augustss 	LIST_HEAD_INITIALIZER(usb_blk_freelist);
     84  1.53       mrg kmutex_t usb_blk_lock;
     85  1.53       mrg 
     86  1.48      matt #ifdef DEBUG
     87  1.48      matt Static struct usb_dma_block_qh usb_blk_fraglist =
     88  1.48      matt 	LIST_HEAD_INITIALIZER(usb_blk_fraglist);
     89  1.48      matt Static struct usb_dma_block_qh usb_blk_fulllist =
     90  1.48      matt 	LIST_HEAD_INITIALIZER(usb_blk_fulllist);
     91  1.48      matt #endif
     92  1.48      matt Static u_int usb_blk_nfree = 0;
     93  1.11  augustss /* XXX should have different free list for different tags (for speed) */
     94  1.19  augustss Static LIST_HEAD(, usb_frag_dma) usb_frag_freelist =
     95   1.1  augustss 	LIST_HEAD_INITIALIZER(usb_frag_freelist);
     96   1.1  augustss 
     97  1.53       mrg Static int usb_mem_init(void);
     98  1.53       mrg 
     99  1.53       mrg Static int
    100  1.53       mrg usb_mem_init(void)
    101  1.53       mrg {
    102  1.53       mrg 
    103  1.53       mrg 	mutex_init(&usb_blk_lock, MUTEX_DEFAULT, IPL_NONE);
    104  1.53       mrg 	return 0;
    105  1.53       mrg }
    106  1.53       mrg 
    107  1.19  augustss Static usbd_status
    108  1.21  augustss usb_block_allocmem(bus_dma_tag_t tag, size_t size, size_t align,
    109  1.76     skrll     u_int flags, usb_dma_block_t **dmap)
    110   1.1  augustss {
    111  1.48      matt 	usb_dma_block_t *b;
    112   1.1  augustss 	int error;
    113   1.1  augustss 
    114  1.71       mrg 	USBHIST_FUNC();
    115  1.76     skrll 	USBHIST_CALLARGS(usbdebug, "size=%ju align=%ju flags=%#jx", size, align, flags, 0);
    116   1.3  augustss 
    117  1.67     skrll 	ASSERT_SLEEPABLE();
    118  1.67     skrll 	KASSERT(size != 0);
    119  1.53       mrg 	KASSERT(mutex_owned(&usb_blk_lock));
    120  1.53       mrg 
    121  1.76     skrll 	bool multiseg = (flags & USBMALLOC_MULTISEG) != 0;
    122  1.76     skrll 	bool coherent = (flags & USBMALLOC_COHERENT) != 0;
    123  1.76     skrll 	u_int dmaflags = coherent ? USB_DMA_COHERENT : 0;
    124  1.76     skrll 
    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.76     skrll 		if (b->tag == tag &&
    131  1.76     skrll 		    b->size >= size &&
    132  1.76     skrll 		    b->align >= align &&
    133  1.76     skrll 		    (b->flags & USB_DMA_COHERENT) == dmaflags) {
    134  1.48      matt 			LIST_REMOVE(b, next);
    135  1.18  augustss 			usb_blk_nfree--;
    136  1.48      matt 			*dmap = b;
    137  1.70  pgoyette 			DPRINTFN(6, "free list size=%ju", b->size, 0, 0, 0);
    138  1.67     skrll 			return USBD_NORMAL_COMPLETION;
    139   1.1  augustss 		}
    140   1.1  augustss 	}
    141   1.9  augustss 
    142  1.72       mrg 	DPRINTFN(6, "no freelist entry", 0, 0, 0, 0);
    143  1.67     skrll 	mutex_exit(&usb_blk_lock);
    144  1.67     skrll 
    145  1.67     skrll 	b = kmem_zalloc(sizeof(*b), KM_SLEEP);
    146  1.48      matt 	b->tag = tag;
    147  1.48      matt 	b->size = size;
    148  1.48      matt 	b->align = align;
    149  1.76     skrll 	b->flags = dmaflags;
    150  1.55     prlw1 
    151  1.55     prlw1 	if (!multiseg)
    152  1.55     prlw1 		/* Caller wants one segment */
    153  1.55     prlw1 		b->nsegs = 1;
    154  1.58  christos 	else
    155  1.77     skrll 		b->nsegs = howmany(size, PAGE_SIZE);
    156  1.55     prlw1 
    157  1.55     prlw1 	b->segs = kmem_alloc(b->nsegs * sizeof(*b->segs), KM_SLEEP);
    158  1.59  jmcneill 	b->nsegs_alloc = b->nsegs;
    159  1.55     prlw1 
    160  1.48      matt 	error = bus_dmamem_alloc(tag, b->size, align, 0,
    161  1.55     prlw1 				 b->segs, b->nsegs,
    162  1.67     skrll 				 &b->nsegs, BUS_DMA_WAITOK);
    163   1.1  augustss 	if (error)
    164  1.27  augustss 		goto free0;
    165   1.1  augustss 
    166  1.76     skrll 	error = bus_dmamem_map(tag, b->segs, b->nsegs, b->size, &b->kaddr,
    167  1.76     skrll 	    BUS_DMA_WAITOK | (coherent ? BUS_DMA_COHERENT : 0));
    168   1.1  augustss 	if (error)
    169  1.27  augustss 		goto free1;
    170   1.1  augustss 
    171  1.55     prlw1 	error = bus_dmamap_create(tag, b->size, b->nsegs, b->size,
    172  1.67     skrll 				  0, BUS_DMA_WAITOK, &b->map);
    173   1.1  augustss 	if (error)
    174   1.1  augustss 		goto unmap;
    175   1.1  augustss 
    176  1.48      matt 	error = bus_dmamap_load(tag, b->map, b->kaddr, b->size, NULL,
    177  1.67     skrll 				BUS_DMA_WAITOK);
    178   1.1  augustss 	if (error)
    179   1.1  augustss 		goto destroy;
    180  1.27  augustss 
    181  1.48      matt 	*dmap = b;
    182  1.78  jmcneill 
    183  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    184  1.78  jmcneill 	flags |= USBMALLOC_ZERO;
    185  1.41      matt #endif
    186  1.78  jmcneill 	if ((flags & USBMALLOC_ZERO) != 0) {
    187  1.78  jmcneill 		memset(b->kaddr, 0, b->size);
    188  1.78  jmcneill 		bus_dmamap_sync(b->tag, b->map, 0, b->size,
    189  1.78  jmcneill 		    BUS_DMASYNC_PREWRITE);
    190  1.78  jmcneill 	}
    191  1.78  jmcneill 
    192  1.67     skrll 	mutex_enter(&usb_blk_lock);
    193  1.55     prlw1 
    194  1.67     skrll 	return USBD_NORMAL_COMPLETION;
    195   1.1  augustss 
    196  1.27  augustss  destroy:
    197  1.48      matt 	bus_dmamap_destroy(tag, b->map);
    198  1.27  augustss  unmap:
    199  1.48      matt 	bus_dmamem_unmap(tag, b->kaddr, b->size);
    200  1.27  augustss  free1:
    201  1.48      matt 	bus_dmamem_free(tag, b->segs, b->nsegs);
    202  1.27  augustss  free0:
    203  1.59  jmcneill 	kmem_free(b->segs, b->nsegs_alloc * sizeof(*b->segs));
    204  1.67     skrll 	kmem_free(b, sizeof(*b));
    205  1.67     skrll 	mutex_enter(&usb_blk_lock);
    206  1.67     skrll 
    207  1.67     skrll 	return USBD_NOMEM;
    208   1.1  augustss }
    209   1.1  augustss 
    210  1.11  augustss #if 0
    211   1.1  augustss void
    212  1.48      matt usb_block_real_freemem(usb_dma_block_t *b)
    213   1.1  augustss {
    214   1.3  augustss #ifdef DIAGNOSTIC
    215  1.64   mlelstv 	if (cpu_softintr_p() || cpu_intr_p()) {
    216   1.3  augustss 		printf("usb_block_real_freemem: in interrupt context\n");
    217   1.3  augustss 		return;
    218   1.3  augustss 	}
    219   1.3  augustss #endif
    220  1.48      matt 	bus_dmamap_unload(b->tag, b->map);
    221  1.48      matt 	bus_dmamap_destroy(b->tag, b->map);
    222  1.48      matt 	bus_dmamem_unmap(b->tag, b->kaddr, b->size);
    223  1.48      matt 	bus_dmamem_free(b->tag, b->segs, b->nsegs);
    224  1.59  jmcneill 	kmem_free(b->segs, b->nsegs_alloc * sizeof(*b->segs));
    225  1.67     skrll 	kmem_free(b, sizeof(*b));
    226   1.1  augustss }
    227  1.11  augustss #endif
    228   1.1  augustss 
    229  1.48      matt #ifdef DEBUG
    230  1.49     rmind static bool
    231  1.48      matt usb_valid_block_p(usb_dma_block_t *b, struct usb_dma_block_qh *qh)
    232  1.48      matt {
    233  1.48      matt 	usb_dma_block_t *xb;
    234  1.48      matt 	LIST_FOREACH(xb, qh, next) {
    235  1.48      matt 		if (xb == b)
    236  1.48      matt 			return true;
    237  1.48      matt 	}
    238  1.48      matt 	return false;
    239  1.48      matt }
    240  1.48      matt #endif
    241  1.48      matt 
    242   1.1  augustss /*
    243   1.1  augustss  * Do not free the memory unconditionally since we might be called
    244   1.1  augustss  * from an interrupt context and that is BAD.
    245   1.4  augustss  * XXX when should we really free?
    246   1.1  augustss  */
    247  1.19  augustss Static void
    248  1.48      matt usb_block_freemem(usb_dma_block_t *b)
    249   1.1  augustss {
    250  1.71       mrg 	USBHIST_FUNC();
    251  1.71       mrg 	USBHIST_CALLARGS(usbdebug, "size=%ju", b->size, 0, 0, 0);
    252  1.53       mrg 
    253  1.53       mrg 	KASSERT(mutex_owned(&usb_blk_lock));
    254   1.4  augustss 
    255  1.48      matt #ifdef DEBUG
    256  1.48      matt 	LIST_REMOVE(b, next);
    257  1.48      matt #endif
    258  1.48      matt 	LIST_INSERT_HEAD(&usb_blk_freelist, b, next);
    259  1.18  augustss 	usb_blk_nfree++;
    260   1.1  augustss }
    261   1.1  augustss 
    262   1.1  augustss usbd_status
    263  1.76     skrll usb_allocmem(struct usbd_bus *bus, size_t size, size_t align, u_int flags,
    264  1.76     skrll     usb_dma_t *p)
    265  1.55     prlw1 {
    266  1.67     skrll 	bus_dma_tag_t tag = bus->ub_dmatag;
    267  1.16  augustss 	usbd_status err;
    268   1.1  augustss 	struct usb_frag_dma *f;
    269   1.1  augustss 	usb_dma_block_t *b;
    270   1.1  augustss 	int i;
    271  1.53       mrg 	static ONCE_DECL(init_control);
    272  1.53       mrg 
    273  1.67     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    274  1.67     skrll 
    275  1.67     skrll 	ASSERT_SLEEPABLE();
    276  1.67     skrll 
    277  1.53       mrg 	RUN_ONCE(&init_control, usb_mem_init);
    278   1.1  augustss 
    279  1.76     skrll 	u_int dmaflags = (flags & USBMALLOC_COHERENT) ? USB_DMA_COHERENT : 0;
    280  1.55     prlw1 
    281   1.1  augustss 	/* If the request is large then just use a full block. */
    282   1.1  augustss 	if (size > USB_MEM_SMALL || align > USB_MEM_SMALL) {
    283  1.70  pgoyette 		DPRINTFN(1, "large alloc %jd", size, 0, 0, 0);
    284   1.1  augustss 		size = (size + USB_MEM_BLOCK - 1) & ~(USB_MEM_BLOCK - 1);
    285  1.53       mrg 		mutex_enter(&usb_blk_lock);
    286  1.76     skrll 		err = usb_block_allocmem(tag, size, align, flags,
    287  1.76     skrll 		    &p->udma_block);
    288  1.16  augustss 		if (!err) {
    289  1.48      matt #ifdef DEBUG
    290  1.67     skrll 			LIST_INSERT_HEAD(&usb_blk_fulllist, p->udma_block, next);
    291  1.48      matt #endif
    292  1.76     skrll 			p->udma_block->flags = USB_DMA_FULLBLOCK | dmaflags;
    293  1.67     skrll 			p->udma_offs = 0;
    294   1.1  augustss 		}
    295  1.53       mrg 		mutex_exit(&usb_blk_lock);
    296  1.67     skrll 		return err;
    297   1.1  augustss 	}
    298  1.24  augustss 
    299  1.53       mrg 	mutex_enter(&usb_blk_lock);
    300   1.1  augustss 	/* Check for free fragments. */
    301  1.67     skrll 	LIST_FOREACH(f, &usb_frag_freelist, ufd_next) {
    302  1.67     skrll 		KDASSERTMSG(usb_valid_block_p(f->ufd_block, &usb_blk_fraglist),
    303  1.50       jym 		    "%s: usb frag %p: unknown block pointer %p",
    304  1.67     skrll 		    __func__, f, f->ufd_block);
    305  1.76     skrll 		if (f->ufd_block->tag == tag &&
    306  1.76     skrll 		    (f->ufd_block->flags & USB_DMA_COHERENT) == dmaflags)
    307   1.1  augustss 			break;
    308  1.41      matt 	}
    309  1.16  augustss 	if (f == NULL) {
    310  1.67     skrll 		DPRINTFN(1, "adding fragments", 0, 0, 0, 0);
    311  1.76     skrll 
    312  1.76     skrll 		err = usb_block_allocmem(tag, USB_MEM_BLOCK, USB_MEM_SMALL,
    313  1.76     skrll 		    flags, &b);
    314  1.16  augustss 		if (err) {
    315  1.53       mrg 			mutex_exit(&usb_blk_lock);
    316  1.67     skrll 			return err;
    317   1.5  augustss 		}
    318  1.48      matt #ifdef DEBUG
    319  1.48      matt 		LIST_INSERT_HEAD(&usb_blk_fraglist, b, next);
    320  1.48      matt #endif
    321  1.28      fvdl 		b->flags = 0;
    322   1.1  augustss 		for (i = 0; i < USB_MEM_BLOCK; i += USB_MEM_SMALL) {
    323  1.32  christos 			f = (struct usb_frag_dma *)((char *)b->kaddr + i);
    324  1.67     skrll 			f->ufd_block = b;
    325  1.67     skrll 			f->ufd_offs = i;
    326  1.67     skrll 			LIST_INSERT_HEAD(&usb_frag_freelist, f, ufd_next);
    327  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    328  1.41      matt 			i += 1 * USB_MEM_SMALL;
    329  1.41      matt #endif
    330   1.1  augustss 		}
    331   1.1  augustss 		f = LIST_FIRST(&usb_frag_freelist);
    332   1.1  augustss 	}
    333  1.67     skrll 	p->udma_block = f->ufd_block;
    334  1.67     skrll 	p->udma_offs = f->ufd_offs;
    335  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    336  1.68     skrll 	p->udma_offs += USB_MEM_SMALL;
    337  1.41      matt #endif
    338  1.67     skrll 	LIST_REMOVE(f, ufd_next);
    339  1.53       mrg 	mutex_exit(&usb_blk_lock);
    340  1.70  pgoyette 	DPRINTFN(5, "use frag=%#jx size=%jd", (uintptr_t)f, size, 0, 0);
    341  1.55     prlw1 
    342  1.67     skrll 	return USBD_NORMAL_COMPLETION;
    343   1.1  augustss }
    344   1.1  augustss 
    345   1.1  augustss void
    346  1.67     skrll usb_freemem(struct usbd_bus *bus, usb_dma_t *p)
    347   1.1  augustss {
    348   1.1  augustss 	struct usb_frag_dma *f;
    349   1.1  augustss 
    350  1.67     skrll 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    351  1.67     skrll 
    352  1.53       mrg 	mutex_enter(&usb_blk_lock);
    353  1.67     skrll 	if (p->udma_block->flags & USB_DMA_FULLBLOCK) {
    354  1.67     skrll 		KDASSERTMSG(usb_valid_block_p(p->udma_block, &usb_blk_fulllist),
    355  1.50       jym 		    "%s: dma %p: invalid block pointer %p",
    356  1.67     skrll 		    __func__, p, p->udma_block);
    357  1.67     skrll 		DPRINTFN(1, "large free", 0, 0, 0, 0);
    358  1.67     skrll 		usb_block_freemem(p->udma_block);
    359  1.53       mrg 		mutex_exit(&usb_blk_lock);
    360   1.1  augustss 		return;
    361   1.1  augustss 	}
    362  1.67     skrll 	KDASSERTMSG(usb_valid_block_p(p->udma_block, &usb_blk_fraglist),
    363  1.50       jym 	    "%s: dma %p: invalid block pointer %p",
    364  1.67     skrll 	    __func__, p, p->udma_block);
    365  1.41      matt 	//usb_syncmem(p, 0, USB_MEM_SMALL, BUS_DMASYNC_POSTREAD);
    366  1.23  augustss 	f = KERNADDR(p, 0);
    367  1.41      matt #ifdef USB_FRAG_DMA_WORKAROUND
    368  1.41      matt 	f = (void *)((uintptr_t)f - USB_MEM_SMALL);
    369  1.41      matt #endif
    370  1.67     skrll 	f->ufd_block = p->udma_block;
    371  1.67     skrll 	f->ufd_offs = p->udma_offs;
    372  1.42      matt #ifdef USB_FRAG_DMA_WORKAROUND
    373  1.68     skrll 	f->ufd_offs -= USB_MEM_SMALL;
    374  1.41      matt #endif
    375  1.67     skrll 	LIST_INSERT_HEAD(&usb_frag_freelist, f, ufd_next);
    376  1.53       mrg 	mutex_exit(&usb_blk_lock);
    377  1.70  pgoyette 	DPRINTFN(5, "frag=%#jx", (uintptr_t)f, 0, 0, 0);
    378   1.1  augustss }
    379  1.28      fvdl 
    380  1.55     prlw1 bus_addr_t
    381  1.55     prlw1 usb_dmaaddr(usb_dma_t *dma, unsigned int offset)
    382  1.55     prlw1 {
    383  1.55     prlw1 	unsigned int i;
    384  1.55     prlw1 	bus_size_t seg_offs;
    385  1.55     prlw1 
    386  1.67     skrll 	offset += dma->udma_offs;
    387  1.55     prlw1 
    388  1.67     skrll 	KASSERTMSG(offset < dma->udma_block->size, "offset %d vs %zu", offset,
    389  1.67     skrll 	    dma->udma_block->size);
    390  1.55     prlw1 
    391  1.67     skrll 	if (dma->udma_block->nsegs == 1) {
    392  1.67     skrll 		KASSERT(dma->udma_block->map->dm_segs[0].ds_len > offset);
    393  1.67     skrll 		return dma->udma_block->map->dm_segs[0].ds_addr + offset;
    394  1.55     prlw1 	}
    395  1.55     prlw1 
    396  1.67     skrll 	/*
    397  1.67     skrll 	 * Search for a bus_segment_t corresponding to this offset. With no
    398  1.55     prlw1 	 * record of the offset in the map to a particular dma_segment_t, we
    399  1.55     prlw1 	 * have to iterate from the start of the list each time. Could be
    400  1.67     skrll 	 * improved
    401  1.67     skrll 	 */
    402  1.55     prlw1 	seg_offs = 0;
    403  1.67     skrll 	for (i = 0; i < dma->udma_block->nsegs; i++) {
    404  1.67     skrll 		if (seg_offs + dma->udma_block->map->dm_segs[i].ds_len > offset)
    405  1.55     prlw1 			break;
    406  1.55     prlw1 
    407  1.67     skrll 		seg_offs += dma->udma_block->map->dm_segs[i].ds_len;
    408  1.55     prlw1 	}
    409  1.55     prlw1 
    410  1.67     skrll 	KASSERT(i != dma->udma_block->nsegs);
    411  1.55     prlw1 	offset -= seg_offs;
    412  1.67     skrll 	return dma->udma_block->map->dm_segs[i].ds_addr + offset;
    413  1.55     prlw1 }
    414  1.55     prlw1 
    415  1.37    bouyer void
    416  1.37    bouyer usb_syncmem(usb_dma_t *p, bus_addr_t offset, bus_size_t len, int ops)
    417  1.37    bouyer {
    418  1.67     skrll 
    419  1.76     skrll 	bus_dmamap_sync(p->udma_block->tag, p->udma_block->map,
    420  1.76     skrll 	    p->udma_offs + offset, len, ops);
    421  1.37    bouyer }
    422