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 "disk.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 .dv_fmtdev = disk_fmtdev, 64 }; 65 66 /* 67 * We're not guaranteed to be able to open a device more than once and there 68 * is no OFW standard method to determine whether a device is already opened. 69 * Opening a device multiple times simultaneously happens to work with most 70 * OFW block device drivers but triggers a trap with at least the driver for 71 * the on-board controllers of Sun Fire V100 and Ultra 1. Upper layers and MI 72 * code expect to be able to open a device more than once however. Given that 73 * different partitions of the same device might be opened at the same time as 74 * done by ZFS, we can't generally just keep track of the opened devices and 75 * reuse the instance handle when asked to open an already opened device. So 76 * the best we can do is to cache the lastly used device path and close and 77 * open devices in ofwd_strategy() as needed. 78 */ 79 static struct ofw_devdesc *kdp; 80 81 static int 82 ofwd_init(void) 83 { 84 85 return (0); 86 } 87 88 static int 89 ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size, 90 char *buf, size_t *rsize) 91 { 92 struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata; 93 daddr_t pos; 94 int n; 95 96 if (dp != kdp) { 97 if (kdp != NULL) { 98 #if !defined(__powerpc__) 99 OF_close(kdp->d_handle); 100 #endif 101 kdp = NULL; 102 } 103 if ((dp->d_handle = OF_open(dp->d_path)) == -1) 104 return (ENOENT); 105 kdp = dp; 106 } 107 108 pos = dblk * 512; 109 do { 110 if (OF_seek(dp->d_handle, pos) < 0) 111 return (EIO); 112 n = OF_read(dp->d_handle, buf, size); 113 if (n < 0 && n != -2) 114 return (EIO); 115 } while (n == -2); 116 *rsize = size; 117 return (0); 118 } 119 120 static int 121 ofwd_open(struct open_file *f, ...) 122 { 123 struct ofw_devdesc *dp; 124 va_list vl; 125 126 va_start(vl, f); 127 dp = va_arg(vl, struct ofw_devdesc *); 128 va_end(vl); 129 130 if (dp != kdp) { 131 if (kdp != NULL) { 132 OF_close(kdp->d_handle); 133 kdp = NULL; 134 } 135 if ((dp->d_handle = OF_open(dp->d_path)) == -1) { 136 printf("%s: Could not open %s\n", __func__, 137 dp->d_path); 138 return (ENOENT); 139 } 140 kdp = dp; 141 } 142 return (0); 143 } 144 145 static int 146 ofwd_close(struct open_file *f) 147 { 148 struct ofw_devdesc *dev = f->f_devdata; 149 150 if (dev == kdp) { 151 #if !defined(__powerpc__) 152 OF_close(dev->d_handle); 153 #endif 154 kdp = NULL; 155 } 156 return (0); 157 } 158 159 static int 160 ofwd_ioctl(struct open_file *f, u_long cmd, void *data) 161 { 162 struct ofw_devdesc *dev = f->f_devdata; 163 int block_size; 164 unsigned int n; 165 166 switch (cmd) { 167 case DIOCGSECTORSIZE: 168 block_size = OF_block_size(dev->d_handle); 169 *(u_int *)data = block_size; 170 break; 171 case DIOCGMEDIASIZE: 172 block_size = OF_block_size(dev->d_handle); 173 n = OF_blocks(dev->d_handle); 174 *(uint64_t *)data = (uint64_t)(n * block_size); 175 break; 176 default: 177 return (ENOTTY); 178 } 179 return (0); 180 } 181 182 static int 183 ofwd_print(int verbose __unused) 184 { 185 return (0); 186 } 187