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