1 /* $NetBSD: dtv_scatter.c,v 1.1 2011/07/09 14:46:56 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Patrick Mahoney <pat@polycrystal.org> 5 * All rights reserved. 6 * 7 * This code was written by Patrick Mahoney (pat@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.1 2011/07/09 14:46:56 jmcneill 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 #define IPL_DTV IPL_VM 52 #define spldtv() splvm() 53 54 void 55 dtv_scatter_buf_init(struct dtv_scatter_buf *sb) 56 { 57 sb->sb_pool = pool_cache_init(PAGE_SIZE, 0, 0, 0, 58 "dtvscatter", NULL, IPL_DTV, 59 NULL, NULL, NULL); 60 sb->sb_size = 0; 61 sb->sb_npages = 0; 62 sb->sb_page_ary = NULL; 63 } 64 65 void 66 dtv_scatter_buf_destroy(struct dtv_scatter_buf *sb) 67 { 68 /* Do we need to return everything to the pool first? */ 69 dtv_scatter_buf_set_size(sb, 0); 70 pool_cache_destroy(sb->sb_pool); 71 sb->sb_pool = 0; 72 sb->sb_npages = 0; 73 sb->sb_page_ary = NULL; 74 } 75 76 /* Increase or decrease the size of the buffer */ 77 int 78 dtv_scatter_buf_set_size(struct dtv_scatter_buf *sb, size_t sz) 79 { 80 unsigned int i; 81 size_t npages, minpages, oldnpages; 82 uint8_t **old_ary; 83 84 npages = (sz >> PAGE_SHIFT) + ((sz & PAGE_MASK) > 0); 85 86 if (sb->sb_npages == npages) { 87 return 0; 88 } 89 90 oldnpages = sb->sb_npages; 91 old_ary = sb->sb_page_ary; 92 93 sb->sb_npages = npages; 94 if (npages > 0) { 95 sb->sb_page_ary = 96 kmem_alloc(sizeof(uint8_t *) * npages, KM_SLEEP); 97 if (sb->sb_page_ary == NULL) { 98 sb->sb_npages = oldnpages; 99 sb->sb_page_ary = old_ary; 100 return ENOMEM; 101 } 102 } else { 103 sb->sb_page_ary = NULL; 104 } 105 106 minpages = min(npages, oldnpages); 107 /* copy any pages that will be reused */ 108 for (i = 0; i < minpages; ++i) 109 sb->sb_page_ary[i] = old_ary[i]; 110 /* allocate any new pages */ 111 for (; i < npages; ++i) { 112 sb->sb_page_ary[i] = pool_cache_get(sb->sb_pool, 0); 113 /* TODO: does pool_cache_get return NULL on 114 * ENOMEM? If so, we need to release or note 115 * the pages with did allocate 116 * successfully. */ 117 if (sb->sb_page_ary[i] == NULL) { 118 return ENOMEM; 119 } 120 } 121 /* return any pages no longer needed */ 122 for (; i < oldnpages; ++i) 123 pool_cache_put(sb->sb_pool, old_ary[i]); 124 125 if (old_ary != NULL) 126 kmem_free(old_ary, sizeof(uint8_t *) * oldnpages); 127 128 sb->sb_size = sb->sb_npages << PAGE_SHIFT; 129 130 return 0; 131 } 132 133 134 paddr_t 135 dtv_scatter_buf_map(struct dtv_scatter_buf *sb, off_t off) 136 { 137 size_t pg; 138 paddr_t pa; 139 140 pg = off >> PAGE_SHIFT; 141 142 if (pg >= sb->sb_npages) 143 return -1; 144 else if (!pmap_extract(pmap_kernel(), (vaddr_t)sb->sb_page_ary[pg], &pa)) 145 return -1; 146 147 return atop(pa); 148 } 149 150 /* Initialize data for an io operation on a scatter buffer. Returns 151 * true if the transfer is valid, or false if out of range. */ 152 bool 153 dtv_scatter_io_init(struct dtv_scatter_buf *sb, 154 off_t off, size_t len, 155 struct dtv_scatter_io *sio) 156 { 157 if ((off + len) > sb->sb_size) { 158 printf("dtv: %s failed: off=%" PRId64 159 " len=%zu sb->sb_size=%zu\n", 160 __func__, off, len, sb->sb_size); 161 return false; 162 } 163 164 sio->sio_buf = sb; 165 sio->sio_offset = off; 166 sio->sio_resid = len; 167 168 return true; 169 } 170 171 /* Store the pointer and size of the next contiguous segment. Returns 172 * true if the segment is valid, or false if all has been transfered. 173 * Does not check for overflow. */ 174 bool 175 dtv_scatter_io_next(struct dtv_scatter_io *sio, void **p, size_t *sz) 176 { 177 size_t pg, pgo; 178 179 if (sio->sio_resid == 0) 180 return false; 181 182 pg = sio->sio_offset >> PAGE_SHIFT; 183 pgo = sio->sio_offset & PAGE_MASK; 184 185 *sz = min(PAGE_SIZE - pgo, sio->sio_resid); 186 *p = sio->sio_buf->sb_page_ary[pg] + pgo; 187 188 sio->sio_offset += *sz; 189 sio->sio_resid -= *sz; 190 191 return true; 192 } 193 194 /* Semi-undo of a failed segment copy. Updates the scatter_io 195 * struct to the previous values prior to a failed segment copy. */ 196 void 197 dtv_scatter_io_undo(struct dtv_scatter_io *sio, size_t sz) 198 { 199 sio->sio_offset -= sz; 200 sio->sio_resid += sz; 201 } 202 203 /* Copy data from src into the scatter_buf as described by io. */ 204 void 205 dtv_scatter_io_copyin(struct dtv_scatter_io *sio, const void *p) 206 { 207 void *dst; 208 const uint8_t *src = p; 209 size_t sz; 210 211 while (dtv_scatter_io_next(sio, &dst, &sz)) { 212 memcpy(dst, src, sz); 213 src += sz; 214 } 215 } 216 217 /* --not used; commented to avoid compiler warnings-- 218 void 219 dtv_scatter_io_copyout(struct dtv_scatter_io *sio, void *p) 220 { 221 void *src; 222 uint8_t *dst = p; 223 size_t sz; 224 225 while (dtv_scatter_io_next(sio, &src, &sz)) { 226 memcpy(dst, src, sz); 227 dst += sz; 228 } 229 } 230 */ 231 232 /* Performat a series of uiomove calls on a scatter buf. Returns 233 * EFAULT if uiomove EFAULTs on the first segment. Otherwise, returns 234 * an incomplete transfer but with no error. */ 235 int 236 dtv_scatter_io_uiomove(struct dtv_scatter_io *sio, struct uio *uio) 237 { 238 void *p; 239 size_t sz; 240 bool first = true; 241 int err; 242 243 while (dtv_scatter_io_next(sio, &p, &sz)) { 244 err = uiomove(p, sz, uio); 245 if (err == EFAULT) { 246 dtv_scatter_io_undo(sio, sz); 247 if (first) 248 return EFAULT; 249 else 250 return 0; 251 } 252 first = false; 253 } 254 255 return 0; 256 } 257