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