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