Home | History | Annotate | Line # | Download | only in bootst
dev_tape.c revision 1.8.26.1
      1 /*	$NetBSD: dev_tape.c,v 1.8.26.1 2008/02/18 21:04:49 mjf 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * This module implements a "raw device" interface suitable for
     41  * use by the stand-alone I/O library UFS file-system code, and
     42  * possibly for direct access (i.e. boot from tape).
     43  */
     44 
     45 #include <sys/types.h>
     46 #include <machine/prom.h>
     47 #include <machine/stdarg.h>
     48 
     49 #include <lib/libkern/libkern.h>
     50 
     51 #include <lib/libsa/stand.h>
     52 #include "libsa.h"
     53 #include "dev_tape.h"
     54 
     55 extern int debug;
     56 
     57 struct mvmeprom_dskio tape_ioreq;
     58 
     59 static int hackprom_diskrd(struct mvmeprom_dskio *);
     60 
     61 /*
     62  * This is a special version of devopen() for tape boot.
     63  * In this version, the file name is a numeric string of
     64  * one digit, which is passed to the device open so it
     65  * can open the appropriate tape segment.
     66  */
     67 int
     68 devopen(struct open_file *f, const char *fname, char **file)
     69 {
     70 	struct devsw *dp;
     71 
     72 	*file = (char *)fname;
     73 	dp = &devsw[0];
     74 	f->f_dev = dp;
     75 
     76 	/* The following will call tape_open() */
     77 	return dp->dv_open(f, fname);
     78 }
     79 
     80 int
     81 tape_open(struct open_file *f, ...)
     82 {
     83 	char *fname;		/* partition number, i.e. "1" */
     84 	int part;
     85 	struct mvmeprom_dskio *ti;
     86 	va_list ap;
     87 
     88 	va_start(ap, f);
     89 	fname = va_arg(ap, char *);
     90 	va_end(ap);
     91 
     92 	/*
     93 	 * Set the tape segment number to the one indicated
     94 	 * by the single digit fname passed in above.
     95 	 */
     96 	if ((fname[0] < '0') && (fname[0] > '9')) {
     97 		return ENOENT;
     98 	}
     99 	part = fname[0] - '0';
    100 
    101 	/*
    102 	 * Setup our part of the saioreq.
    103 	 * (determines what gets opened)
    104 	 */
    105 	ti = &tape_ioreq;
    106 	memset((void *)ti, 0, sizeof(*ti));
    107 
    108 	ti->ctrl_lun = bugargs.ctrl_lun;
    109 	ti->dev_lun = bugargs.dev_lun;
    110 	ti->status = 0;
    111 	ti->pbuffer = NULL;
    112 	ti->blk_num = part;
    113 	ti->blk_cnt = 0;
    114 	ti->flag = 0;
    115 	ti->addr_mod = 0;
    116 
    117 	f->f_devdata = ti;
    118 
    119 	return 0;
    120 }
    121 
    122 int
    123 tape_close(struct open_file *f)
    124 {
    125 	struct mvmeprom_dskio *ti;
    126 
    127 	ti = f->f_devdata;
    128 	f->f_devdata = NULL;
    129 	return 0;
    130 }
    131 
    132 #define MVMEPROM_SCALE (512/MVMEPROM_BLOCK_SIZE)
    133 
    134 int
    135 tape_strategy(void *devdata, int flag, daddr_t dblk, u_int size, void *buf,
    136     u_int *rsize)
    137 {
    138 	struct mvmeprom_dskio *ti;
    139 	int ret;
    140 
    141 	ti = devdata;
    142 
    143 	if (flag != F_READ)
    144 		return EROFS;
    145 
    146 	ti->status = 0;
    147 	ti->pbuffer = buf;
    148 	/* don't change block #, set in open */
    149 	ti->blk_cnt = size / (512 / MVMEPROM_SCALE);
    150 
    151 	/* work around for stupid '147 prom bug */
    152 	if (bugargs.cputyp == 0x147)
    153 		ret = hackprom_diskrd(ti);
    154 	else
    155 		ret = mvmeprom_diskrd(ti);
    156 
    157 	if (ret != 0)
    158 		return EIO;
    159 
    160 	*rsize = (ti->blk_cnt / MVMEPROM_SCALE) * 512;
    161 	ti->flag |= IGNORE_FILENUM; /* ignore next time */
    162 
    163 	return 0;
    164 }
    165 
    166 int
    167 tape_ioctl(struct open_file *f, u_long cmd, void *data)
    168 {
    169 
    170 	return EIO;
    171 }
    172 
    173 static int
    174 hackprom_diskrd(struct mvmeprom_dskio *ti)
    175 {
    176 	static int blkoffset = 0;
    177 
    178 #define	hackload_addr	((char *)0x080000)	/* Load tape segment here */
    179 #define hackload_blocks 0x2000			/* 2Mb worth */
    180 
    181 	if ((ti->flag & IGNORE_FILENUM) == 0) {
    182 		/*
    183 		 * First time through. Load the whole tape segment...
    184 		 */
    185 		struct mvmeprom_dskio nti;
    186 		int ret;
    187 
    188 		nti = *ti;
    189 
    190 		nti.pbuffer = hackload_addr;
    191 		nti.blk_cnt = hackload_blocks;
    192 		nti.flag |= END_OF_FILE;
    193 
    194 		ret = mvmeprom_diskrd(&nti);
    195 
    196 		/*
    197 		 * PROM returns 1 on end-of-file. This isn't an
    198 		 * error in this instance, just in case you're wondering! ;-)
    199 		 */
    200 		if (ret < 0 || ret > 1)
    201 			return ret;
    202 
    203 		blkoffset = 0;
    204 	}
    205 
    206 	/*
    207 	 * Grab the required number of block(s)
    208 	 */
    209 	memcpy(ti->pbuffer, &(hackload_addr[blkoffset]),
    210 	    ti->blk_cnt * MVMEPROM_BLOCK_SIZE);
    211 
    212 	blkoffset += (ti->blk_cnt * MVMEPROM_BLOCK_SIZE);
    213 
    214 	return 0;
    215 }
    216