1 /*- 2 * Copyright (C) 2000 Benno Rice. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 /* 30 * Disk I/O routines using Open Firmware 31 */ 32 33 #include <sys/param.h> 34 35 #include <netinet/in.h> 36 37 #include <machine/stdarg.h> 38 39 #include <stand.h> 40 #include <sys/disk.h> 41 42 #include "bootstrap.h" 43 #include "libofw.h" 44 45 static int ofwd_init(void); 46 static int ofwd_strategy(void *devdata, int flag, daddr_t dblk, 47 size_t size, char *buf, size_t *rsize); 48 static int ofwd_open(struct open_file *f, ...); 49 static int ofwd_close(struct open_file *f); 50 static int ofwd_ioctl(struct open_file *f, u_long cmd, void *data); 51 static int ofwd_print(int verbose); 52 53 struct devsw ofwdisk = { 54 .dv_name = "block", 55 .dv_type = DEVT_DISK, 56 .dv_init = ofwd_init, 57 .dv_strategy = ofwd_strategy, 58 .dv_open = ofwd_open, 59 .dv_close = ofwd_close, 60 .dv_ioctl = ofwd_ioctl, 61 .dv_print = ofwd_print, 62 .dv_cleanup = nullsys, 63 }; 64 65 /* 66 * We're not guaranteed to be able to open a device more than once and there 67 * is no OFW standard method to determine whether a device is already opened. 68 * Opening a device multiple times simultaneously happens to work with most 69 * OFW block device drivers but triggers a trap with at least the driver for 70 * the on-board controllers of Sun Fire V100 and Ultra 1. Upper layers and MI 71 * code expect to be able to open a device more than once however. Given that 72 * different partitions of the same device might be opened at the same time as 73 * done by ZFS, we can't generally just keep track of the opened devices and 74 * reuse the instance handle when asked to open an already opened device. So 75 * the best we can do is to cache the lastly used device path and close and 76 * open devices in ofwd_strategy() as needed. 77 */ 78 static struct ofw_devdesc *kdp; 79 80 static int 81 ofwd_init(void) 82 { 83 84 return (0); 85 } 86 87 static int 88 ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size, 89 char *buf, size_t *rsize) 90 { 91 struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata; 92 daddr_t pos; 93 int n; 94 95 if (dp != kdp) { 96 if (kdp != NULL) { 97 #if !defined(__powerpc__) 98 OF_close(kdp->d_handle); 99 #endif 100 kdp = NULL; 101 } 102 if ((dp->d_handle = OF_open(dp->d_path)) == -1) 103 return (ENOENT); 104 kdp = dp; 105 } 106 107 pos = dblk * 512; 108 do { 109 if (OF_seek(dp->d_handle, pos) < 0) 110 return (EIO); 111 n = OF_read(dp->d_handle, buf, size); 112 if (n < 0 && n != -2) 113 return (EIO); 114 } while (n == -2); 115 *rsize = size; 116 return (0); 117 } 118 119 static int 120 ofwd_open(struct open_file *f, ...) 121 { 122 struct ofw_devdesc *dp; 123 va_list vl; 124 125 va_start(vl, f); 126 dp = va_arg(vl, struct ofw_devdesc *); 127 va_end(vl); 128 129 if (dp != kdp) { 130 if (kdp != NULL) { 131 OF_close(kdp->d_handle); 132 kdp = NULL; 133 } 134 if ((dp->d_handle = OF_open(dp->d_path)) == -1) { 135 printf("%s: Could not open %s\n", __func__, 136 dp->d_path); 137 return (ENOENT); 138 } 139 kdp = dp; 140 } 141 return (0); 142 } 143 144 static int 145 ofwd_close(struct open_file *f) 146 { 147 struct ofw_devdesc *dev = f->f_devdata; 148 149 if (dev == kdp) { 150 #if !defined(__powerpc__) 151 OF_close(dev->d_handle); 152 #endif 153 kdp = NULL; 154 } 155 return (0); 156 } 157 158 static int 159 ofwd_ioctl(struct open_file *f, u_long cmd, void *data) 160 { 161 struct ofw_devdesc *dev = f->f_devdata; 162 int block_size; 163 unsigned int n; 164 165 switch (cmd) { 166 case DIOCGSECTORSIZE: 167 block_size = OF_block_size(dev->d_handle); 168 *(u_int *)data = block_size; 169 break; 170 case DIOCGMEDIASIZE: 171 block_size = OF_block_size(dev->d_handle); 172 n = OF_blocks(dev->d_handle); 173 *(uint64_t *)data = (uint64_t)(n * block_size); 174 break; 175 default: 176 return (ENOTTY); 177 } 178 return (0); 179 } 180 181 static int 182 ofwd_print(int verbose __unused) 183 { 184 return (0); 185 } 186