1 /* $NetBSD: sector.c,v 1.7 2008/04/28 20:23:18 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 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: sector.c,v 1.7 2008/04/28 20:23:18 martin Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/buf.h> 38 #include <machine/sector.h> 39 40 struct sector_rw { 41 struct buf *buf; 42 void (*strategy)(struct buf *); 43 bool busy; 44 } __context; 45 46 void * 47 sector_init(dev_t dev, void (*strat)(struct buf *)) 48 { 49 struct sector_rw *rw =& __context; 50 51 if (rw->busy) 52 return 0; 53 rw->busy = true; 54 rw->strategy = strat; 55 rw->buf = geteblk(DEV_BSIZE); 56 rw->buf->b_dev = dev; 57 58 return rw; 59 } 60 61 void 62 sector_fini(void *self) 63 { 64 struct sector_rw *rw = self; 65 66 brelse(rw->buf, 0); 67 rw->busy = false; 68 } 69 70 bool 71 sector_read_n(void *self, uint8_t *buf, daddr_t sector, int count) 72 { 73 int i; 74 75 for (i = 0; i < count; i++) { 76 if (!sector_read(self, buf, sector)) 77 return false; 78 buf += DEV_BSIZE; 79 sector++; 80 } 81 82 return true; 83 } 84 85 bool 86 sector_read(void *self, uint8_t *buf, daddr_t sector) 87 { 88 struct sector_rw *rw = self; 89 struct buf *b = rw->buf; 90 91 b->b_blkno = sector; 92 b->b_cylinder = sector / 100; 93 b->b_bcount = DEV_BSIZE; 94 b->b_oflags &= ~(BO_DONE); 95 b->b_flags |= B_READ; 96 rw->strategy(b); 97 98 if (biowait(b) != 0) 99 return false; 100 101 memcpy(buf, b->b_data, DEV_BSIZE); 102 103 return true; 104 } 105 106 bool 107 sector_write_n(void *self, uint8_t *buf, daddr_t sector, int count) 108 { 109 int i; 110 111 for (i = 0; i < count; i++) { 112 if (!sector_write(self, buf, sector)) 113 return false; 114 buf += DEV_BSIZE; 115 sector++; 116 } 117 118 return true; 119 } 120 121 bool 122 sector_write(void *self, uint8_t *buf, daddr_t sector) 123 { 124 struct sector_rw *rw = self; 125 struct buf *b = rw->buf; 126 127 b->b_blkno = sector; 128 b->b_cylinder = sector / 100; 129 b->b_bcount = DEV_BSIZE; 130 b->b_flags &= ~(B_READ); 131 b->b_oflags &= ~(BO_DONE); 132 b->b_flags |= B_WRITE; 133 memcpy(b->b_data, buf, DEV_BSIZE); 134 rw->strategy(b); 135 136 if (biowait(b) != 0) 137 return false; 138 139 return true; 140 } 141