dtv_scatter.c revision 1.6 1 1.6 msaitoh /* $NetBSD: dtv_scatter.c,v 1.6 2019/12/27 09:41:50 msaitoh 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.6 msaitoh __KERNEL_RCSID(0, "$NetBSD: dtv_scatter.c,v 1.6 2019/12/27 09:41:50 msaitoh 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 } else {
95 1.1 jmcneill sb->sb_page_ary = NULL;
96 1.1 jmcneill }
97 1.1 jmcneill
98 1.5 riastrad minpages = uimin(npages, oldnpages);
99 1.1 jmcneill /* copy any pages that will be reused */
100 1.1 jmcneill for (i = 0; i < minpages; ++i)
101 1.1 jmcneill sb->sb_page_ary[i] = old_ary[i];
102 1.1 jmcneill /* allocate any new pages */
103 1.1 jmcneill for (; i < npages; ++i) {
104 1.4 riastrad sb->sb_page_ary[i] = pool_cache_get(sb->sb_pool, PR_WAITOK);
105 1.1 jmcneill /* TODO: does pool_cache_get return NULL on
106 1.1 jmcneill * ENOMEM? If so, we need to release or note
107 1.1 jmcneill * the pages with did allocate
108 1.1 jmcneill * successfully. */
109 1.1 jmcneill if (sb->sb_page_ary[i] == NULL) {
110 1.1 jmcneill return ENOMEM;
111 1.1 jmcneill }
112 1.1 jmcneill }
113 1.1 jmcneill /* return any pages no longer needed */
114 1.1 jmcneill for (; i < oldnpages; ++i)
115 1.1 jmcneill pool_cache_put(sb->sb_pool, old_ary[i]);
116 1.1 jmcneill
117 1.1 jmcneill if (old_ary != NULL)
118 1.1 jmcneill kmem_free(old_ary, sizeof(uint8_t *) * oldnpages);
119 1.1 jmcneill
120 1.1 jmcneill sb->sb_size = sb->sb_npages << PAGE_SHIFT;
121 1.1 jmcneill
122 1.1 jmcneill return 0;
123 1.1 jmcneill }
124 1.1 jmcneill
125 1.1 jmcneill
126 1.1 jmcneill paddr_t
127 1.1 jmcneill dtv_scatter_buf_map(struct dtv_scatter_buf *sb, off_t off)
128 1.1 jmcneill {
129 1.1 jmcneill size_t pg;
130 1.1 jmcneill paddr_t pa;
131 1.1 jmcneill
132 1.1 jmcneill pg = off >> PAGE_SHIFT;
133 1.1 jmcneill
134 1.1 jmcneill if (pg >= sb->sb_npages)
135 1.1 jmcneill return -1;
136 1.1 jmcneill else if (!pmap_extract(pmap_kernel(), (vaddr_t)sb->sb_page_ary[pg], &pa))
137 1.1 jmcneill return -1;
138 1.1 jmcneill
139 1.1 jmcneill return atop(pa);
140 1.1 jmcneill }
141 1.1 jmcneill
142 1.1 jmcneill /* Initialize data for an io operation on a scatter buffer. Returns
143 1.1 jmcneill * true if the transfer is valid, or false if out of range. */
144 1.1 jmcneill bool
145 1.1 jmcneill dtv_scatter_io_init(struct dtv_scatter_buf *sb,
146 1.1 jmcneill off_t off, size_t len,
147 1.1 jmcneill struct dtv_scatter_io *sio)
148 1.1 jmcneill {
149 1.1 jmcneill if ((off + len) > sb->sb_size) {
150 1.1 jmcneill printf("dtv: %s failed: off=%" PRId64
151 1.1 jmcneill " len=%zu sb->sb_size=%zu\n",
152 1.1 jmcneill __func__, off, len, sb->sb_size);
153 1.1 jmcneill return false;
154 1.1 jmcneill }
155 1.1 jmcneill
156 1.1 jmcneill sio->sio_buf = sb;
157 1.1 jmcneill sio->sio_offset = off;
158 1.1 jmcneill sio->sio_resid = len;
159 1.1 jmcneill
160 1.1 jmcneill return true;
161 1.1 jmcneill }
162 1.1 jmcneill
163 1.1 jmcneill /* Store the pointer and size of the next contiguous segment. Returns
164 1.6 msaitoh * true if the segment is valid, or false if all has been transferred.
165 1.1 jmcneill * Does not check for overflow. */
166 1.1 jmcneill bool
167 1.1 jmcneill dtv_scatter_io_next(struct dtv_scatter_io *sio, void **p, size_t *sz)
168 1.1 jmcneill {
169 1.1 jmcneill size_t pg, pgo;
170 1.1 jmcneill
171 1.1 jmcneill if (sio->sio_resid == 0)
172 1.1 jmcneill return false;
173 1.1 jmcneill
174 1.1 jmcneill pg = sio->sio_offset >> PAGE_SHIFT;
175 1.1 jmcneill pgo = sio->sio_offset & PAGE_MASK;
176 1.1 jmcneill
177 1.5 riastrad *sz = uimin(PAGE_SIZE - pgo, sio->sio_resid);
178 1.1 jmcneill *p = sio->sio_buf->sb_page_ary[pg] + pgo;
179 1.1 jmcneill
180 1.1 jmcneill sio->sio_offset += *sz;
181 1.1 jmcneill sio->sio_resid -= *sz;
182 1.1 jmcneill
183 1.1 jmcneill return true;
184 1.1 jmcneill }
185 1.1 jmcneill
186 1.1 jmcneill /* Semi-undo of a failed segment copy. Updates the scatter_io
187 1.1 jmcneill * struct to the previous values prior to a failed segment copy. */
188 1.1 jmcneill void
189 1.1 jmcneill dtv_scatter_io_undo(struct dtv_scatter_io *sio, size_t sz)
190 1.1 jmcneill {
191 1.1 jmcneill sio->sio_offset -= sz;
192 1.1 jmcneill sio->sio_resid += sz;
193 1.1 jmcneill }
194 1.1 jmcneill
195 1.1 jmcneill /* Copy data from src into the scatter_buf as described by io. */
196 1.1 jmcneill void
197 1.1 jmcneill dtv_scatter_io_copyin(struct dtv_scatter_io *sio, const void *p)
198 1.1 jmcneill {
199 1.1 jmcneill void *dst;
200 1.1 jmcneill const uint8_t *src = p;
201 1.1 jmcneill size_t sz;
202 1.1 jmcneill
203 1.1 jmcneill while (dtv_scatter_io_next(sio, &dst, &sz)) {
204 1.1 jmcneill memcpy(dst, src, sz);
205 1.1 jmcneill src += sz;
206 1.1 jmcneill }
207 1.1 jmcneill }
208 1.1 jmcneill
209 1.1 jmcneill /* --not used; commented to avoid compiler warnings--
210 1.1 jmcneill void
211 1.1 jmcneill dtv_scatter_io_copyout(struct dtv_scatter_io *sio, void *p)
212 1.1 jmcneill {
213 1.1 jmcneill void *src;
214 1.1 jmcneill uint8_t *dst = p;
215 1.1 jmcneill size_t sz;
216 1.1 jmcneill
217 1.1 jmcneill while (dtv_scatter_io_next(sio, &src, &sz)) {
218 1.1 jmcneill memcpy(dst, src, sz);
219 1.1 jmcneill dst += sz;
220 1.1 jmcneill }
221 1.1 jmcneill }
222 1.1 jmcneill */
223 1.1 jmcneill
224 1.1 jmcneill /* Performat a series of uiomove calls on a scatter buf. Returns
225 1.1 jmcneill * EFAULT if uiomove EFAULTs on the first segment. Otherwise, returns
226 1.1 jmcneill * an incomplete transfer but with no error. */
227 1.1 jmcneill int
228 1.1 jmcneill dtv_scatter_io_uiomove(struct dtv_scatter_io *sio, struct uio *uio)
229 1.1 jmcneill {
230 1.1 jmcneill void *p;
231 1.1 jmcneill size_t sz;
232 1.1 jmcneill bool first = true;
233 1.1 jmcneill int err;
234 1.1 jmcneill
235 1.1 jmcneill while (dtv_scatter_io_next(sio, &p, &sz)) {
236 1.1 jmcneill err = uiomove(p, sz, uio);
237 1.1 jmcneill if (err == EFAULT) {
238 1.1 jmcneill dtv_scatter_io_undo(sio, sz);
239 1.1 jmcneill if (first)
240 1.1 jmcneill return EFAULT;
241 1.1 jmcneill else
242 1.1 jmcneill return 0;
243 1.1 jmcneill }
244 1.1 jmcneill first = false;
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill return 0;
248 1.1 jmcneill }
249