xref: /netbsd-src/sys/arch/sun68k/stand/tapeboot/dev_tape.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: dev_tape.c,v 1.5 2009/01/12 07:01:00 tsutsui 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  * The implementation is deceptively simple because it uses the
38  * drivers provided by the Sun PROM monitor.  Note that only the
39  * PROM driver used to load the boot program is available here.
40  */
41 
42 #include <sys/types.h>
43 #include <machine/mon.h>
44 #include <machine/stdarg.h>
45 #include <stand.h>
46 
47 #include "libsa.h"
48 #include "dvma.h"
49 #include "saio.h"
50 #include "dev_tape.h"
51 
52 extern int debug;
53 
54 struct saioreq tape_ioreq;
55 
56 /*
57  * This is a special version of devopen() for tape boot.
58  * In this version, the file name is a numeric string of
59  * one digit, which is passed to the device open so it
60  * can open the appropriate tape segment.
61  */
62 int
63 devopen(struct open_file *f, const char *fname,	char **file)
64 {
65 	struct devsw *dp;
66 
67 	*file = (char *)fname;
68 	dp = &devsw[0];
69 	f->f_dev = dp;
70 
71 	/* The following will call tape_open() */
72 	return (dp->dv_open(f, fname));
73 }
74 
75 int
76 tape_open(struct open_file *f, ...)
77 {
78 	struct bootparam *bp;
79 	struct saioreq *si;
80 	int error, part;
81 	char *fname;		/* partition number, i.e. "1" */
82 	va_list ap;
83 
84 	va_start(ap, f);
85 	fname = va_arg(ap, char *);
86 #ifdef DEBUG
87 	printf("tape_open: part=%s\n", fname);
88 #endif
89 	va_end(ap);
90 
91 	/*
92 	 * Set the tape segment number to the one indicated
93 	 * by the single digit fname passed in above.
94 	 */
95 	if ((fname[0] < '0') && (fname[0] > '9')) {
96 		return ENOENT;
97 	}
98 	part = fname[0] - '0';
99 
100 	/*
101 	 * Setup our part of the saioreq.
102 	 * (determines what gets opened)
103 	 */
104 	si = &tape_ioreq;
105 	memset(si, 0, sizeof(*si));
106 
107 	bp = *romVectorPtr->bootParam;
108 	si->si_boottab = bp->bootDevice;
109 	si->si_ctlr = bp->ctlrNum;
110 	si->si_unit = bp->unitNum;
111 	si->si_boff = part; 	/* default = bp->partNum + 1; */
112 
113 	error = prom_iopen(si);
114 
115 #ifdef DEBUG
116 	printf("tape_open: prom_iopen returned 0x%x\n", error);
117 #endif
118 
119 	if (!error)
120 		f->f_devdata = si;
121 
122 	return (error);
123 }
124 
125 int
126 tape_close(struct open_file *f)
127 {
128 	struct saioreq *si;
129 
130 #ifdef DEBUG
131 	printf("tape_close: calling prom_iclose\n");
132 #endif
133 
134 	si = f->f_devdata;
135 	prom_iclose(si);
136 	f->f_devdata = NULL;
137 	return 0;
138 }
139 
140 int
141 tape_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf,
142     size_t *rsize)
143 {
144 	struct saioreq *si;
145 	struct boottab *ops;
146 	char *dmabuf;
147 	int	si_flag, xcnt;
148 
149 	si = devdata;
150 	ops = si->si_boottab;
151 
152 #ifdef DEBUG
153 	if (debug > 1)
154 		printf("tape_strategy: size=%d dblk=%d\n", size, dblk);
155 #endif
156 
157 	dmabuf = dvma_mapin(buf, size);
158 
159 	si->si_bn = dblk;
160 	si->si_ma = dmabuf;
161 	si->si_cc = size;
162 
163 	si_flag = (flag == F_READ) ? SAIO_F_READ : SAIO_F_WRITE;
164 	xcnt = (*ops->b_strategy)(si, si_flag);
165 	dvma_mapout(dmabuf, size);
166 
167 #ifdef DEBUG
168 	if (debug > 1)
169 		printf("tape_strategy: xcnt = %x\n", xcnt);
170 #endif
171 
172 	/* At end of tape, xcnt == 0 (not an error) */
173 	if (xcnt < 0)
174 		return (EIO);
175 
176 	*rsize = xcnt;
177 	return (0);
178 }
179 
180 int
181 tape_ioctl(struct open_file *f, u_long cmd, void *data)
182 {
183 	return EIO;
184 }
185 
186