1 /* $NetBSD: dev_tape.c,v 1.11 2009/04/11 10:56:13 scw Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg. 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 /* 33 * This module implements a "raw device" interface suitable for 34 * use by the stand-alone I/O library UFS file-system code, and 35 * possibly for direct access (i.e. boot from tape). 36 */ 37 38 #include <sys/types.h> 39 #include <machine/prom.h> 40 #include <machine/stdarg.h> 41 42 #include <lib/libkern/libkern.h> 43 44 #include <lib/libsa/stand.h> 45 #include "libsa.h" 46 #include "dev_tape.h" 47 48 extern int debug; 49 50 struct mvmeprom_dskio tape_ioreq; 51 52 static int hackprom_diskrd(struct mvmeprom_dskio *); 53 54 /* 55 * This is a special version of devopen() for tape boot. 56 * In this version, the file name is a numeric string of 57 * one digit, which is passed to the device open so it 58 * can open the appropriate tape segment. 59 */ 60 int 61 devopen(struct open_file *f, const char *fname, char **file) 62 { 63 struct devsw *dp; 64 65 *file = (char *)fname; 66 dp = &devsw[0]; 67 f->f_dev = dp; 68 69 /* The following will call tape_open() */ 70 return dp->dv_open(f, fname); 71 } 72 73 int 74 tape_open(struct open_file *f, ...) 75 { 76 char *fname; /* partition number, i.e. "1" */ 77 int part; 78 struct mvmeprom_dskio *ti; 79 va_list ap; 80 81 va_start(ap, f); 82 fname = va_arg(ap, char *); 83 va_end(ap); 84 85 /* 86 * Set the tape segment number to the one indicated 87 * by the single digit fname passed in above. 88 */ 89 if ((fname[0] < '0') && (fname[0] > '9')) { 90 return ENOENT; 91 } 92 part = fname[0] - '0'; 93 94 /* 95 * Setup our part of the saioreq. 96 * (determines what gets opened) 97 */ 98 ti = &tape_ioreq; 99 memset((void *)ti, 0, sizeof(*ti)); 100 101 ti->ctrl_lun = bugargs.ctrl_lun; 102 ti->dev_lun = bugargs.dev_lun; 103 ti->status = 0; 104 ti->pbuffer = NULL; 105 ti->blk_num = part; 106 ti->blk_cnt = 0; 107 ti->flag = 0; 108 ti->addr_mod = 0; 109 110 f->f_devdata = ti; 111 112 return 0; 113 } 114 115 int 116 tape_close(struct open_file *f) 117 { 118 struct mvmeprom_dskio *ti; 119 120 ti = f->f_devdata; 121 f->f_devdata = NULL; 122 return 0; 123 } 124 125 #define MVMEPROM_SCALE (512/MVMEPROM_BLOCK_SIZE) 126 127 int 128 tape_strategy(void *devdata, int flag, daddr_t dblk, u_int size, void *buf, 129 u_int *rsize) 130 { 131 struct mvmeprom_dskio *ti; 132 int ret; 133 134 ti = devdata; 135 136 if (flag != F_READ) 137 return EROFS; 138 139 ti->status = 0; 140 ti->pbuffer = buf; 141 /* don't change block #, set in open */ 142 ti->blk_cnt = size / (512 / MVMEPROM_SCALE); 143 144 /* work around for stupid '147 prom bug */ 145 if (bugargs.cputyp == 0x147) 146 ret = hackprom_diskrd(ti); 147 else 148 ret = mvmeprom_diskrd(ti); 149 150 if (ret != 0) 151 return EIO; 152 153 *rsize = (ti->blk_cnt / MVMEPROM_SCALE) * 512; 154 ti->flag |= IGNORE_FILENUM; /* ignore next time */ 155 156 return 0; 157 } 158 159 int 160 tape_ioctl(struct open_file *f, u_long cmd, void *data) 161 { 162 163 return EIO; 164 } 165 166 static int 167 hackprom_diskrd(struct mvmeprom_dskio *ti) 168 { 169 static int blkoffset = 0; 170 171 #define hackload_addr ((char *)0x080000) /* Load tape segment here */ 172 #define hackload_blocks 0x3000 /* 3Mb worth */ 173 174 if ((ti->flag & IGNORE_FILENUM) == 0) { 175 /* 176 * First time through. Load the whole tape segment... 177 */ 178 struct mvmeprom_dskio nti; 179 int ret; 180 181 nti = *ti; 182 183 nti.pbuffer = hackload_addr; 184 nti.blk_cnt = hackload_blocks; 185 nti.flag |= END_OF_FILE; 186 187 ret = mvmeprom_diskrd(&nti); 188 189 /* 190 * PROM returns 1 on end-of-file. This isn't an 191 * error in this instance, just in case you're wondering! ;-) 192 */ 193 if (ret < 0 || ret > 1) 194 return ret; 195 196 blkoffset = 0; 197 } 198 199 /* 200 * Grab the required number of block(s) 201 */ 202 memcpy(ti->pbuffer, &(hackload_addr[blkoffset]), 203 ti->blk_cnt * MVMEPROM_BLOCK_SIZE); 204 205 blkoffset += (ti->blk_cnt * MVMEPROM_BLOCK_SIZE); 206 207 return 0; 208 } 209