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