flash_io.c revision 1.2 1 1.2 ahoka /* $NetBSD: flash_io.c,v 1.2 2011/06/28 20:58:00 ahoka Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*-
4 1.1 ahoka * Copyright (c) 2011 Department of Software Engineering,
5 1.1 ahoka * University of Szeged, Hungary
6 1.1 ahoka * Copyright (c) 2011 Adam Hoka <ahoka (at) NetBSD.org>
7 1.1 ahoka * All rights reserved.
8 1.1 ahoka *
9 1.1 ahoka * This code is derived from software contributed to The NetBSD Foundation
10 1.1 ahoka * by the Department of Software Engineering, University of Szeged, Hungary
11 1.1 ahoka *
12 1.1 ahoka * Redistribution and use in source and binary forms, with or without
13 1.1 ahoka * modification, are permitted provided that the following conditions
14 1.1 ahoka * are met:
15 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
16 1.1 ahoka * notice, this list of conditions and the following disclaimer.
17 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
19 1.1 ahoka * documentation and/or other materials provided with the distribution.
20 1.1 ahoka *
21 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 ahoka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 ahoka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 ahoka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 ahoka * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 ahoka * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 ahoka * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 ahoka * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 ahoka * SUCH DAMAGE.
32 1.1 ahoka */
33 1.1 ahoka
34 1.1 ahoka #include <sys/cdefs.h>
35 1.2 ahoka __KERNEL_RCSID(0, "$NetBSD: flash_io.c,v 1.2 2011/06/28 20:58:00 ahoka Exp $");
36 1.1 ahoka
37 1.1 ahoka #include <sys/param.h>
38 1.1 ahoka #include <sys/buf.h>
39 1.1 ahoka #include <sys/bufq.h>
40 1.1 ahoka #include <sys/kernel.h>
41 1.1 ahoka #include <sys/kmem.h>
42 1.1 ahoka #include <sys/kthread.h>
43 1.1 ahoka #include <sys/mutex.h>
44 1.1 ahoka #include <sys/sysctl.h>
45 1.1 ahoka
46 1.1 ahoka #include <dev/flash/flash.h>
47 1.1 ahoka #include <dev/flash/flash_io.h>
48 1.1 ahoka
49 1.1 ahoka #ifdef FLASH_DEBUG
50 1.1 ahoka extern int flashdebug;
51 1.1 ahoka #endif
52 1.1 ahoka
53 1.1 ahoka int flash_cachesync_timeout = 1;
54 1.1 ahoka int flash_cachesync_nodenum;
55 1.1 ahoka
56 1.1 ahoka void flash_io_read(struct flash_io *, struct buf *);
57 1.1 ahoka void flash_io_write(struct flash_io *, struct buf *);
58 1.1 ahoka void flash_io_done(struct flash_io *, struct buf *, int);
59 1.1 ahoka int flash_io_cache_write(struct flash_io *, flash_addr_t, struct buf *);
60 1.1 ahoka void flash_io_cache_sync(struct flash_io *);
61 1.1 ahoka
62 1.1 ahoka static int
63 1.1 ahoka flash_timestamp_diff(struct bintime *bt, struct bintime *b2)
64 1.1 ahoka {
65 1.1 ahoka struct bintime b1 = *bt;
66 1.1 ahoka struct timeval tv;
67 1.1 ahoka
68 1.1 ahoka bintime_sub(&b1, b2);
69 1.1 ahoka bintime2timeval(&b1, &tv);
70 1.1 ahoka
71 1.1 ahoka return tvtohz(&tv);
72 1.1 ahoka }
73 1.1 ahoka
74 1.1 ahoka static flash_addr_t
75 1.1 ahoka flash_io_getblock(struct flash_io *fio, struct buf *bp)
76 1.1 ahoka {
77 1.1 ahoka flash_off_t block, last;
78 1.1 ahoka
79 1.1 ahoka /* get block number of first byte */
80 1.1 ahoka block = bp->b_rawblkno * DEV_BSIZE / fio->fio_if->erasesize;
81 1.1 ahoka
82 1.1 ahoka /* block of the last bite */
83 1.1 ahoka last = (bp->b_rawblkno * DEV_BSIZE + bp->b_resid - 1)
84 1.1 ahoka / fio->fio_if->erasesize;
85 1.1 ahoka
86 1.1 ahoka /* spans trough multiple blocks, needs special handling */
87 1.1 ahoka if (last != block) {
88 1.1 ahoka printf("0x%jx -> 0x%jx\n",
89 1.1 ahoka bp->b_rawblkno * DEV_BSIZE,
90 1.1 ahoka bp->b_rawblkno * DEV_BSIZE + bp->b_resid - 1);
91 1.1 ahoka panic("TODO: multiple block write. last: %jd, current: %jd",
92 1.1 ahoka (intmax_t )last, (intmax_t )block);
93 1.1 ahoka }
94 1.1 ahoka
95 1.1 ahoka return block;
96 1.1 ahoka }
97 1.1 ahoka
98 1.1 ahoka int
99 1.1 ahoka flash_sync_thread_init(struct flash_io *fio, struct flash_interface *flash_if)
100 1.1 ahoka {
101 1.1 ahoka int error;
102 1.1 ahoka
103 1.1 ahoka FLDPRINTF(("starting flash io thread\n"));
104 1.1 ahoka
105 1.1 ahoka fio->fio_if = flash_if;
106 1.1 ahoka
107 1.1 ahoka fio->fio_data = kmem_alloc(fio->fio_if->erasesize, KM_SLEEP);
108 1.1 ahoka
109 1.1 ahoka mutex_init(&fio->fio_lock, MUTEX_DEFAULT, IPL_NONE);
110 1.1 ahoka cv_init(&fio->fio_cv, "flashcv");
111 1.1 ahoka
112 1.1 ahoka error = bufq_alloc(&fio->fio_bufq, "fcfs", BUFQ_SORT_RAWBLOCK);
113 1.1 ahoka if (error)
114 1.1 ahoka goto err_bufq;
115 1.1 ahoka
116 1.1 ahoka fio->fio_exiting = false;
117 1.1 ahoka fio->fio_write_pending = false;
118 1.1 ahoka
119 1.1 ahoka /* arrange to allocate the kthread */
120 1.1 ahoka error = kthread_create(PRI_NONE, KTHREAD_JOINABLE | KTHREAD_MPSAFE,
121 1.1 ahoka NULL, flash_sync_thread, fio, &fio->fio_thread, "flashio");
122 1.1 ahoka
123 1.1 ahoka if (!error)
124 1.1 ahoka return 0;
125 1.1 ahoka
126 1.1 ahoka bufq_free(fio->fio_bufq);
127 1.1 ahoka err_bufq:
128 1.1 ahoka cv_destroy(&fio->fio_cv);
129 1.1 ahoka mutex_destroy(&fio->fio_lock);
130 1.1 ahoka kmem_free(fio->fio_data, fio->fio_if->erasesize);
131 1.1 ahoka
132 1.1 ahoka return error;
133 1.1 ahoka }
134 1.1 ahoka
135 1.1 ahoka void
136 1.1 ahoka flash_sync_thread_destroy(struct flash_io *fio)
137 1.1 ahoka {
138 1.1 ahoka FLDPRINTF(("stopping flash io thread\n"));
139 1.1 ahoka
140 1.1 ahoka mutex_enter(&fio->fio_lock);
141 1.1 ahoka
142 1.1 ahoka fio->fio_exiting = true;
143 1.1 ahoka cv_broadcast(&fio->fio_cv);
144 1.1 ahoka
145 1.1 ahoka mutex_exit(&fio->fio_lock);
146 1.1 ahoka
147 1.1 ahoka kthread_join(fio->fio_thread);
148 1.1 ahoka
149 1.1 ahoka kmem_free(fio->fio_data, fio->fio_if->erasesize);
150 1.1 ahoka bufq_free(fio->fio_bufq);
151 1.1 ahoka mutex_destroy(&fio->fio_lock);
152 1.1 ahoka cv_destroy(&fio->fio_cv);
153 1.1 ahoka }
154 1.1 ahoka
155 1.1 ahoka int
156 1.1 ahoka flash_io_submit(struct flash_io *fio, struct buf *bp)
157 1.1 ahoka {
158 1.1 ahoka FLDPRINTF(("submitting job to flash io thread: %p\n", bp));
159 1.1 ahoka
160 1.1 ahoka if (__predict_false(fio->fio_exiting)) {
161 1.1 ahoka flash_io_done(fio, bp, ENODEV);
162 1.1 ahoka return ENODEV;
163 1.1 ahoka }
164 1.1 ahoka
165 1.1 ahoka if (BUF_ISREAD(bp)) {
166 1.1 ahoka FLDPRINTF(("we have a read job\n"));
167 1.1 ahoka
168 1.1 ahoka mutex_enter(&fio->fio_lock);
169 1.1 ahoka if (fio->fio_write_pending)
170 1.1 ahoka flash_io_cache_sync(fio);
171 1.1 ahoka mutex_exit(&fio->fio_lock);
172 1.1 ahoka
173 1.1 ahoka flash_io_read(fio, bp);
174 1.1 ahoka } else {
175 1.1 ahoka FLDPRINTF(("we have a write job\n"));
176 1.1 ahoka
177 1.1 ahoka flash_io_write(fio, bp);
178 1.1 ahoka }
179 1.1 ahoka return 0;
180 1.1 ahoka }
181 1.1 ahoka
182 1.1 ahoka int
183 1.1 ahoka flash_io_cache_write(struct flash_io *fio, flash_addr_t block, struct buf *bp)
184 1.1 ahoka {
185 1.1 ahoka size_t retlen;
186 1.1 ahoka flash_addr_t base, offset;
187 1.1 ahoka int error;
188 1.1 ahoka
189 1.1 ahoka KASSERT(mutex_owned(&fio->fio_lock));
190 1.1 ahoka KASSERT(fio->fio_if->erasesize != 0);
191 1.1 ahoka
192 1.1 ahoka base = block * fio->fio_if->erasesize;
193 1.1 ahoka offset = bp->b_rawblkno * DEV_BSIZE - base;
194 1.1 ahoka
195 1.1 ahoka FLDPRINTF(("io cache write, offset: %jd\n", (intmax_t )offset));
196 1.1 ahoka
197 1.1 ahoka if (!fio->fio_write_pending) {
198 1.1 ahoka fio->fio_block = block;
199 1.1 ahoka /*
200 1.1 ahoka * fill the cache with data from flash,
201 1.1 ahoka * so we dont have to bother with gaps later
202 1.1 ahoka */
203 1.1 ahoka FLDPRINTF(("filling buffer from offset %ju\n", (uintmax_t)base));
204 1.1 ahoka error = fio->fio_if->read(fio->fio_dev,
205 1.1 ahoka base, fio->fio_if->erasesize,
206 1.1 ahoka &retlen, fio->fio_data);
207 1.1 ahoka FLDPRINTF(("cache filled\n"));
208 1.1 ahoka
209 1.1 ahoka if (error)
210 1.1 ahoka return error;
211 1.1 ahoka
212 1.1 ahoka fio->fio_write_pending = true;
213 1.1 ahoka /* save creation time for aging */
214 1.1 ahoka binuptime(&fio->fio_creation);
215 1.1 ahoka }
216 1.1 ahoka /* copy data to cache */
217 1.1 ahoka memcpy(fio->fio_data + offset, bp->b_data, bp->b_resid);
218 1.1 ahoka bufq_put(fio->fio_bufq, bp);
219 1.1 ahoka
220 1.1 ahoka /* update timestamp */
221 1.1 ahoka binuptime(&fio->fio_last_write);
222 1.1 ahoka
223 1.1 ahoka return 0;
224 1.1 ahoka }
225 1.1 ahoka
226 1.1 ahoka void
227 1.1 ahoka flash_io_cache_sync(struct flash_io *fio)
228 1.1 ahoka {
229 1.1 ahoka struct flash_erase_instruction ei;
230 1.1 ahoka struct buf *bp;
231 1.1 ahoka size_t retlen;
232 1.1 ahoka flash_addr_t base;
233 1.1 ahoka int error;
234 1.1 ahoka
235 1.1 ahoka KASSERT(mutex_owned(&fio->fio_lock));
236 1.1 ahoka
237 1.1 ahoka if (!fio->fio_write_pending) {
238 1.1 ahoka FLDPRINTF(("trying to sync with an invalid buffer\n"));
239 1.1 ahoka return;
240 1.1 ahoka }
241 1.1 ahoka
242 1.1 ahoka base = fio->fio_block * fio->fio_if->erasesize;
243 1.1 ahoka
244 1.1 ahoka FLDPRINTF(("eraseing block at 0x%jx\n", (uintmax_t )base));
245 1.1 ahoka ei.ei_addr = base;
246 1.1 ahoka ei.ei_len = fio->fio_if->erasesize;
247 1.1 ahoka ei.ei_callback = NULL;
248 1.1 ahoka error = fio->fio_if->erase(fio->fio_dev, &ei);
249 1.1 ahoka
250 1.1 ahoka if (error) {
251 1.1 ahoka aprint_error_dev(fio->fio_dev, "cannot erase flash flash!\n");
252 1.1 ahoka goto out;
253 1.1 ahoka }
254 1.1 ahoka
255 1.2 ahoka FLDPRINTF(("writing %" PRIu32 " bytes to 0x%jx\n",
256 1.1 ahoka fio->fio_if->erasesize, (uintmax_t )base));
257 1.1 ahoka
258 1.1 ahoka error = fio->fio_if->write(fio->fio_dev,
259 1.1 ahoka base, fio->fio_if->erasesize, &retlen, fio->fio_data);
260 1.1 ahoka
261 1.1 ahoka if (error || retlen != fio->fio_if->erasesize) {
262 1.1 ahoka aprint_error_dev(fio->fio_dev, "can't sync write cache: %d\n", error);
263 1.1 ahoka goto out;
264 1.1 ahoka }
265 1.1 ahoka
266 1.1 ahoka out:
267 1.1 ahoka while ((bp = bufq_get(fio->fio_bufq)) != NULL)
268 1.1 ahoka flash_io_done(fio, bp, error);
269 1.1 ahoka
270 1.1 ahoka fio->fio_block = -1;
271 1.1 ahoka fio->fio_write_pending = false;
272 1.1 ahoka }
273 1.1 ahoka
274 1.1 ahoka void
275 1.1 ahoka flash_sync_thread(void * arg)
276 1.1 ahoka {
277 1.1 ahoka struct flash_io *fio = arg;
278 1.1 ahoka struct bintime now;
279 1.1 ahoka
280 1.1 ahoka mutex_enter(&fio->fio_lock);
281 1.1 ahoka
282 1.1 ahoka while (!fio->fio_exiting) {
283 1.1 ahoka cv_timedwait_sig(&fio->fio_cv, &fio->fio_lock, hz / 4);
284 1.1 ahoka if (!fio->fio_write_pending) {
285 1.1 ahoka continue;
286 1.1 ahoka }
287 1.1 ahoka /* see if the cache is older than 3 seconds (safety limit),
288 1.1 ahoka * or if we havent touched the cache since more than 1 ms
289 1.1 ahoka */
290 1.1 ahoka binuptime(&now);
291 1.1 ahoka if (flash_timestamp_diff(&now, &fio->fio_last_write) > hz / 5) {
292 1.1 ahoka FLDPRINTF(("syncing write cache after timeout\n"));
293 1.1 ahoka flash_io_cache_sync(fio);
294 1.1 ahoka } else if (flash_timestamp_diff(&now, &fio->fio_creation)
295 1.1 ahoka > 3 * hz) {
296 1.1 ahoka aprint_error_dev(fio->fio_dev,
297 1.1 ahoka "syncing write cache after 3 sec timeout!\n");
298 1.1 ahoka flash_io_cache_sync(fio);
299 1.1 ahoka }
300 1.1 ahoka }
301 1.1 ahoka
302 1.1 ahoka mutex_exit(&fio->fio_lock);
303 1.1 ahoka
304 1.1 ahoka kthread_exit(0);
305 1.1 ahoka }
306 1.1 ahoka
307 1.1 ahoka void
308 1.1 ahoka flash_io_read(struct flash_io *fio, struct buf *bp)
309 1.1 ahoka {
310 1.1 ahoka size_t retlen;
311 1.1 ahoka flash_addr_t offset;
312 1.1 ahoka int error;
313 1.1 ahoka
314 1.1 ahoka FLDPRINTF(("flash io read\n"));
315 1.1 ahoka
316 1.1 ahoka offset = bp->b_rawblkno * DEV_BSIZE;
317 1.1 ahoka
318 1.1 ahoka error = fio->fio_if->read(fio->fio_dev, offset, bp->b_resid,
319 1.1 ahoka &retlen, bp->b_data);
320 1.1 ahoka
321 1.1 ahoka flash_io_done(fio, bp, error);
322 1.1 ahoka }
323 1.1 ahoka
324 1.1 ahoka void
325 1.1 ahoka flash_io_write(struct flash_io *fio, struct buf *bp)
326 1.1 ahoka {
327 1.1 ahoka flash_addr_t block;
328 1.1 ahoka
329 1.1 ahoka FLDPRINTF(("flash io write\n"));
330 1.1 ahoka
331 1.1 ahoka block = flash_io_getblock(fio, bp);
332 1.1 ahoka FLDPRINTF(("write to block %jd\n", (intmax_t )block));
333 1.1 ahoka
334 1.1 ahoka mutex_enter(&fio->fio_lock);
335 1.1 ahoka
336 1.1 ahoka if (fio->fio_write_pending && fio->fio_block != block) {
337 1.1 ahoka FLDPRINTF(("writing to new block, syncing caches\n"));
338 1.1 ahoka flash_io_cache_sync(fio);
339 1.1 ahoka }
340 1.1 ahoka
341 1.1 ahoka flash_io_cache_write(fio, block, bp);
342 1.1 ahoka
343 1.1 ahoka mutex_exit(&fio->fio_lock);
344 1.1 ahoka }
345 1.1 ahoka
346 1.1 ahoka void
347 1.1 ahoka flash_io_done(struct flash_io *fio, struct buf *bp, int error)
348 1.1 ahoka {
349 1.1 ahoka FLDPRINTF(("io done: %p\n", bp));
350 1.1 ahoka
351 1.1 ahoka if (error == 0)
352 1.1 ahoka bp->b_resid = 0;
353 1.1 ahoka
354 1.1 ahoka bp->b_error = error;
355 1.1 ahoka biodone(bp);
356 1.1 ahoka }
357 1.1 ahoka
358 1.1 ahoka static int
359 1.1 ahoka sysctl_flash_verify(SYSCTLFN_ARGS)
360 1.1 ahoka {
361 1.1 ahoka int error, t;
362 1.1 ahoka struct sysctlnode node;
363 1.1 ahoka
364 1.1 ahoka node = *rnode;
365 1.1 ahoka t = *(int *)rnode->sysctl_data;
366 1.1 ahoka node.sysctl_data = &t;
367 1.1 ahoka error = sysctl_lookup(SYSCTLFN_CALL(&node));
368 1.1 ahoka if (error || newp == NULL)
369 1.1 ahoka return error;
370 1.1 ahoka
371 1.1 ahoka if (node.sysctl_num == flash_cachesync_nodenum) {
372 1.1 ahoka if (t <= 0 || t > 60)
373 1.1 ahoka return EINVAL;
374 1.1 ahoka } else {
375 1.1 ahoka return EINVAL;
376 1.1 ahoka }
377 1.1 ahoka
378 1.1 ahoka *(int *)rnode->sysctl_data = t;
379 1.1 ahoka
380 1.1 ahoka return 0;
381 1.1 ahoka }
382 1.1 ahoka
383 1.1 ahoka SYSCTL_SETUP(sysctl_flash, "sysctl flash subtree setup")
384 1.1 ahoka {
385 1.1 ahoka int rc, flash_root_num;
386 1.1 ahoka const struct sysctlnode *node;
387 1.1 ahoka
388 1.1 ahoka if ((rc = sysctl_createv(clog, 0, NULL, NULL,
389 1.1 ahoka CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
390 1.1 ahoka NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
391 1.1 ahoka goto error;
392 1.1 ahoka }
393 1.1 ahoka
394 1.1 ahoka if ((rc = sysctl_createv(clog, 0, NULL, &node,
395 1.1 ahoka CTLFLAG_PERMANENT, CTLTYPE_NODE, "flash",
396 1.1 ahoka SYSCTL_DESCR("FLASH driver controls"),
397 1.1 ahoka NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
398 1.1 ahoka goto error;
399 1.1 ahoka }
400 1.1 ahoka
401 1.1 ahoka flash_root_num = node->sysctl_num;
402 1.1 ahoka
403 1.1 ahoka if ((rc = sysctl_createv(clog, 0, NULL, &node,
404 1.1 ahoka CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
405 1.1 ahoka CTLTYPE_INT, "cache_sync_timeout",
406 1.1 ahoka SYSCTL_DESCR("FLASH write cache sync timeout in seconds"),
407 1.1 ahoka sysctl_flash_verify, 0, &flash_cachesync_timeout,
408 1.1 ahoka 0, CTL_HW, flash_root_num, CTL_CREATE,
409 1.1 ahoka CTL_EOL)) != 0) {
410 1.1 ahoka goto error;
411 1.1 ahoka }
412 1.1 ahoka
413 1.1 ahoka flash_cachesync_nodenum = node->sysctl_num;
414 1.1 ahoka
415 1.1 ahoka return;
416 1.1 ahoka
417 1.1 ahoka error:
418 1.1 ahoka aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
419 1.1 ahoka }
420