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
47extern int debug;
48
49struct mvmeprom_dskio tape_ioreq;
50
51static 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 */
59int
60devopen(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
72int
73tape_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
114int
115tape_close(struct open_file *f)
116{
117	f->f_devdata = NULL;
118	return 0;
119}
120
121#define MVMEPROM_SCALE (512/MVMEPROM_BLOCK_SIZE)
122
123int
124tape_strategy(void *devdata, int flag, daddr_t dblk, u_int size, void *buf,
125    u_int *rsize)
126{
127	struct mvmeprom_dskio *ti;
128	int ret;
129
130	ti = devdata;
131
132	if (flag != F_READ)
133		return EROFS;
134
135	ti->status = 0;
136	ti->pbuffer = buf;
137	/* don't change block #, set in open */
138	ti->blk_cnt = size / (512 / MVMEPROM_SCALE);
139
140	/* work around for stupid '147 prom bug */
141	if (bugargs.cputyp == 0x147)
142		ret = hackprom_diskrd(ti);
143	else
144		ret = mvmeprom_diskrd(ti);
145
146	if (ret != 0)
147		return EIO;
148
149	*rsize = (ti->blk_cnt / MVMEPROM_SCALE) * 512;
150	ti->flag |= IGNORE_FILENUM; /* ignore next time */
151
152	return 0;
153}
154
155int
156tape_ioctl(struct open_file *f, u_long cmd, void *data)
157{
158
159	return EIO;
160}
161
162static int
163hackprom_diskrd(struct mvmeprom_dskio *ti)
164{
165	static int blkoffset = 0;
166
167#define	hackload_addr	((char *)0x080000)	/* Load tape segment here */
168#define hackload_blocks 0x3000			/* 3Mb worth */
169
170	if ((ti->flag & IGNORE_FILENUM) == 0) {
171		/*
172		 * First time through. Load the whole tape segment...
173		 */
174		struct mvmeprom_dskio nti;
175		int ret;
176
177		nti = *ti;
178
179		nti.pbuffer = hackload_addr;
180		nti.blk_cnt = hackload_blocks;
181		nti.flag |= END_OF_FILE;
182
183		ret = mvmeprom_diskrd(&nti);
184
185		/*
186		 * PROM returns 1 on end-of-file. This isn't an
187		 * error in this instance, just in case you're wondering! ;-)
188		 */
189		if (ret < 0 || ret > 1)
190			return ret;
191
192		blkoffset = 0;
193	}
194
195	/*
196	 * Grab the required number of block(s)
197	 */
198	memcpy(ti->pbuffer, &(hackload_addr[blkoffset]),
199	    ti->blk_cnt * MVMEPROM_BLOCK_SIZE);
200
201	blkoffset += (ti->blk_cnt * MVMEPROM_BLOCK_SIZE);
202
203	return 0;
204}
205