Home | History | Annotate | Line # | Download | only in dtv
      1 /* $NetBSD: dtv_scatter.c,v 1.6 2019/12/27 09:41:50 msaitoh Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2008 Patrick Mahoney <pat (at) polycrystal.org>
      5  * All rights reserved.
      6  *
      7  * This code was written by Patrick Mahoney (pat (at) polycrystal.org) as
      8  * part of Google Summer of Code 2008.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: dtv_scatter.c,v 1.6 2019/12/27 09:41:50 msaitoh Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/ioctl.h>
     37 #include <sys/fcntl.h>
     38 #include <sys/vnode.h>
     39 #include <sys/poll.h>
     40 #include <sys/select.h>
     41 #include <sys/kmem.h>
     42 #include <sys/pool.h>
     43 #include <sys/conf.h>
     44 #include <sys/types.h>
     45 #include <sys/device.h>
     46 #include <sys/condvar.h>
     47 #include <sys/queue.h>
     48 
     49 #include <dev/dtv/dtvvar.h>
     50 
     51 void
     52 dtv_scatter_buf_init(struct dtv_scatter_buf *sb)
     53 {
     54 	sb->sb_pool = pool_cache_init(PAGE_SIZE, 0, 0, 0,
     55 				      "dtvscatter", NULL, IPL_SCHED,
     56 				      NULL, NULL, NULL);
     57 	sb->sb_size = 0;
     58 	sb->sb_npages = 0;
     59 	sb->sb_page_ary = NULL;
     60 }
     61 
     62 void
     63 dtv_scatter_buf_destroy(struct dtv_scatter_buf *sb)
     64 {
     65 	/* Do we need to return everything to the pool first? */
     66 	dtv_scatter_buf_set_size(sb, 0);
     67 	pool_cache_destroy(sb->sb_pool);
     68 	sb->sb_pool = 0;
     69 	sb->sb_npages = 0;
     70 	sb->sb_page_ary = NULL;
     71 }
     72 
     73 /* Increase or decrease the size of the buffer */
     74 int
     75 dtv_scatter_buf_set_size(struct dtv_scatter_buf *sb, size_t sz)
     76 {
     77 	unsigned int i;
     78 	size_t npages, minpages, oldnpages;
     79 	uint8_t **old_ary;
     80 
     81 	npages = (sz >> PAGE_SHIFT) + ((sz & PAGE_MASK) > 0);
     82 
     83 	if (sb->sb_npages == npages) {
     84 		return 0;
     85 	}
     86 
     87 	oldnpages = sb->sb_npages;
     88 	old_ary = sb->sb_page_ary;
     89 
     90 	sb->sb_npages = npages;
     91 	if (npages > 0) {
     92 		sb->sb_page_ary =
     93 		    kmem_alloc(sizeof(uint8_t *) * npages, KM_SLEEP);
     94 	} else {
     95 		sb->sb_page_ary = NULL;
     96 	}
     97 
     98 	minpages = uimin(npages, oldnpages);
     99 	/* copy any pages that will be reused */
    100 	for (i = 0; i < minpages; ++i)
    101 		sb->sb_page_ary[i] = old_ary[i];
    102 	/* allocate any new pages */
    103 	for (; i < npages; ++i) {
    104 		sb->sb_page_ary[i] = pool_cache_get(sb->sb_pool, PR_WAITOK);
    105 		/* TODO: does pool_cache_get return NULL on
    106 		 * ENOMEM?  If so, we need to release or note
    107 		 * the pages with did allocate
    108 		 * successfully. */
    109 		if (sb->sb_page_ary[i] == NULL) {
    110 			return ENOMEM;
    111 		}
    112 	}
    113 	/* return any pages no longer needed */
    114 	for (; i < oldnpages; ++i)
    115 		pool_cache_put(sb->sb_pool, old_ary[i]);
    116 
    117 	if (old_ary != NULL)
    118 		kmem_free(old_ary, sizeof(uint8_t *) * oldnpages);
    119 
    120 	sb->sb_size = sb->sb_npages << PAGE_SHIFT;
    121 
    122 	return 0;
    123 }
    124 
    125 
    126 paddr_t
    127 dtv_scatter_buf_map(struct dtv_scatter_buf *sb, off_t off)
    128 {
    129 	size_t pg;
    130 	paddr_t pa;
    131 
    132 	pg = off >> PAGE_SHIFT;
    133 
    134 	if (pg >= sb->sb_npages)
    135 		return -1;
    136 	else if (!pmap_extract(pmap_kernel(), (vaddr_t)sb->sb_page_ary[pg], &pa))
    137 		return -1;
    138 
    139 	return atop(pa);
    140 }
    141 
    142 /* Initialize data for an io operation on a scatter buffer. Returns
    143  * true if the transfer is valid, or false if out of range. */
    144 bool
    145 dtv_scatter_io_init(struct dtv_scatter_buf *sb,
    146 		    off_t off, size_t len,
    147 		    struct dtv_scatter_io *sio)
    148 {
    149 	if ((off + len) > sb->sb_size) {
    150 		printf("dtv: %s failed: off=%" PRId64
    151 			 " len=%zu sb->sb_size=%zu\n",
    152 			 __func__, off, len, sb->sb_size);
    153 		return false;
    154 	}
    155 
    156 	sio->sio_buf = sb;
    157 	sio->sio_offset = off;
    158 	sio->sio_resid = len;
    159 
    160 	return true;
    161 }
    162 
    163 /* Store the pointer and size of the next contiguous segment.  Returns
    164  * true if the segment is valid, or false if all has been transferred.
    165  * Does not check for overflow. */
    166 bool
    167 dtv_scatter_io_next(struct dtv_scatter_io *sio, void **p, size_t *sz)
    168 {
    169 	size_t pg, pgo;
    170 
    171 	if (sio->sio_resid == 0)
    172 		return false;
    173 
    174 	pg = sio->sio_offset >> PAGE_SHIFT;
    175 	pgo = sio->sio_offset & PAGE_MASK;
    176 
    177 	*sz = uimin(PAGE_SIZE - pgo, sio->sio_resid);
    178 	*p = sio->sio_buf->sb_page_ary[pg] + pgo;
    179 
    180 	sio->sio_offset += *sz;
    181 	sio->sio_resid -= *sz;
    182 
    183 	return true;
    184 }
    185 
    186 /* Semi-undo of a failed segment copy.  Updates the scatter_io
    187  * struct to the previous values prior to a failed segment copy. */
    188 void
    189 dtv_scatter_io_undo(struct dtv_scatter_io *sio, size_t sz)
    190 {
    191 	sio->sio_offset -= sz;
    192 	sio->sio_resid += sz;
    193 }
    194 
    195 /* Copy data from src into the scatter_buf as described by io. */
    196 void
    197 dtv_scatter_io_copyin(struct dtv_scatter_io *sio, const void *p)
    198 {
    199 	void *dst;
    200 	const uint8_t *src = p;
    201 	size_t sz;
    202 
    203 	while (dtv_scatter_io_next(sio, &dst, &sz)) {
    204 		memcpy(dst, src, sz);
    205 		src += sz;
    206 	}
    207 }
    208 
    209 /* --not used; commented to avoid compiler warnings--
    210 void
    211 dtv_scatter_io_copyout(struct dtv_scatter_io *sio, void *p)
    212 {
    213 	void *src;
    214 	uint8_t *dst = p;
    215 	size_t sz;
    216 
    217 	while (dtv_scatter_io_next(sio, &src, &sz)) {
    218 		memcpy(dst, src, sz);
    219 		dst += sz;
    220 	}
    221 }
    222 */
    223 
    224 /* Performat a series of uiomove calls on a scatter buf.  Returns
    225  * EFAULT if uiomove EFAULTs on the first segment.  Otherwise, returns
    226  * an incomplete transfer but with no error. */
    227 int
    228 dtv_scatter_io_uiomove(struct dtv_scatter_io *sio, struct uio *uio)
    229 {
    230 	void *p;
    231 	size_t sz;
    232 	bool first = true;
    233 	int err;
    234 
    235 	while (dtv_scatter_io_next(sio, &p, &sz)) {
    236 		err = uiomove(p, sz, uio);
    237 		if (err == EFAULT) {
    238 			dtv_scatter_io_undo(sio, sz);
    239 			if (first)
    240 				return EFAULT;
    241 			else
    242 				return 0;
    243 		}
    244 		first = false;
    245 	}
    246 
    247 	return 0;
    248 }
    249