ofw_disk.c revision 236579
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: head/sys/boot/ofw/libofw/ofw_disk.c 236579 2012-06-04 20:45:33Z marius $"); 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 41#include "bootstrap.h" 42#include "libofw.h" 43 44static int ofwd_init(void); 45static int ofwd_strategy(void *devdata, int flag, daddr_t dblk, 46 size_t size, char *buf, size_t *rsize); 47static int ofwd_open(struct open_file *f, ...); 48static int ofwd_close(struct open_file *f); 49static int ofwd_ioctl(struct open_file *f, u_long cmd, void *data); 50static void ofwd_print(int verbose); 51 52struct devsw ofwdisk = { 53 "block", 54 DEVT_DISK, 55 ofwd_init, 56 ofwd_strategy, 57 ofwd_open, 58 ofwd_close, 59 ofwd_ioctl, 60 ofwd_print 61}; 62 63/* 64 * We're not guaranteed to be able to open a device more than once and there 65 * is no OFW standard method to determine whether a device is already opened. 66 * Opening a device multiple times simultaneously happens to work with most 67 * OFW block device drivers but triggers a trap with at least the driver for 68 * the on-board controllers of Sun Fire V100 and Ultra 1. Upper layers and MI 69 * code expect to be able to open a device more than once however. Given that 70 * different partitions of the same device might be opened at the same time as 71 * done by ZFS, we can't generally just keep track of the opened devices and 72 * reuse the instance handle when asked to open an already opened device. So 73 * the best we can do is to cache the lastly used device path and close and 74 * open devices in ofwd_strategy() as needed. 75 */ 76static struct ofw_devdesc *kdp; 77 78static int 79ofwd_init(void) 80{ 81 82 return (0); 83} 84 85static int 86ofwd_strategy(void *devdata, int flag __unused, daddr_t dblk, size_t size, 87 char *buf, size_t *rsize) 88{ 89 struct ofw_devdesc *dp = (struct ofw_devdesc *)devdata; 90 daddr_t pos; 91 int n; 92 93 if (dp != kdp) { 94 if (kdp != NULL) { 95#if !defined(__powerpc__) 96 OF_close(kdp->d_handle); 97#endif 98 kdp = NULL; 99 } 100 if ((dp->d_handle = OF_open(dp->d_path)) == -1) 101 return (ENOENT); 102 kdp = dp; 103 } 104 105 pos = dblk * 512; 106 do { 107 if (OF_seek(dp->d_handle, pos) < 0) 108 return (EIO); 109 n = OF_read(dp->d_handle, buf, size); 110 if (n < 0 && n != -2) 111 return (EIO); 112 } while (n == -2); 113 *rsize = size; 114 return (0); 115} 116 117static int 118ofwd_open(struct open_file *f, ...) 119{ 120 struct ofw_devdesc *dp; 121 va_list vl; 122 123 va_start(vl, f); 124 dp = va_arg(vl, struct ofw_devdesc *); 125 va_end(vl); 126 127 if (dp != kdp) { 128 if (kdp != NULL) { 129 OF_close(kdp->d_handle); 130 kdp = NULL; 131 } 132 if ((dp->d_handle = OF_open(dp->d_path)) == -1) { 133 printf("%s: Could not open %s\n", __func__, 134 dp->d_path); 135 return (ENOENT); 136 } 137 kdp = dp; 138 } 139 return (0); 140} 141 142static int 143ofwd_close(struct open_file *f) 144{ 145 struct ofw_devdesc *dev = f->f_devdata; 146 147 if (dev == kdp) { 148#if !defined(__powerpc__) 149 OF_close(dev->d_handle); 150#endif 151 kdp = NULL; 152 } 153 return (0); 154} 155 156static int 157ofwd_ioctl(struct open_file *f __unused, u_long cmd __unused, 158 void *data __unused) 159{ 160 161 return (EINVAL); 162} 163 164static void 165ofwd_print(int verbose __unused) 166{ 167 168} 169