xref: /netbsd/sys/arch/sun68k/stand/libsa/dev_disk.c (revision bf9ec67e)
1 /*	$NetBSD: dev_disk.c,v 1.1 2001/06/14 12:57:14 fredette 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_disk.h"
58 
59 #define RETRY_COUNT 5
60 
61 int disk_opencount;
62 struct saioreq disk_ioreq;
63 
64 int
65 disk_open(struct open_file *f, ...)
66 {
67 	struct bootparam *bp;
68 	struct saioreq *si;
69 	int error;
70 
71 #ifdef DEBUG_PROM
72 	char *devname;		/* Device part of file name (or NULL). */
73 	va_list ap;
74 
75 	va_start(ap, f);
76 	devname = va_arg(ap, char *);
77 	if (debug)
78 		printf("disk_open: %s\n", devname);
79 	va_end(ap);
80 #endif
81 
82 	si = &disk_ioreq;
83 	if (disk_opencount == 0) {
84 		/*
85 		 * Setup our part of the saioreq.
86 		 * (determines what gets opened)
87 		 */
88 		bp = *romVectorPtr->bootParam;
89 		si->si_boottab = bp->bootDevice;
90 		si->si_ctlr = bp->ctlrNum;
91 		si->si_unit = bp->unitNum;
92 		si->si_boff = bp->partNum;
93 		if ((error = prom_iopen(si)) != 0)
94 			return (error);
95 	}
96 	disk_opencount++;
97 
98 	f->f_devdata = si;
99 	return 0;
100 }
101 
102 int
103 disk_close(f)
104 	struct open_file *f;
105 {
106 	struct saioreq *si;
107 
108 #ifdef DEBUG_PROM
109 	if (debug)
110 		printf("disk_close: ocnt=%d\n", disk_opencount);
111 #endif
112 
113 	si = f->f_devdata;
114 	f->f_devdata = NULL;
115 	if (disk_opencount <= 0)
116 		return 0;
117 	if (--disk_opencount == 0)
118 		prom_iclose(si);
119 	return 0;
120 }
121 
122 int
123 disk_strategy(devdata, flag, dblk, size, buf, rsize)
124 	void	*devdata;
125 	int	flag;
126 	daddr_t	dblk;
127 	size_t	size;
128 	void	*buf;
129 	size_t	*rsize;
130 {
131 	struct saioreq *si;
132 	struct boottab *ops;
133 	char *dmabuf;
134 	int retry, si_flag, xcnt;
135 
136 	si = devdata;
137 	ops = si->si_boottab;
138 
139 #ifdef DEBUG_PROM
140 	if (debug > 1)
141 		printf("disk_strategy: size=%d dblk=%d\n", size, dblk);
142 #endif
143 
144 	dmabuf = dvma_mapin(buf, size);
145 	si_flag = (flag == F_READ) ? SAIO_F_READ : SAIO_F_WRITE;
146 
147 	/*
148 	 * The PROM strategy will occasionally return -1 and expect
149 	 * us to try again.  From mouse@Collatz.McRCIM.McGill.EDU
150 	 */
151 	retry = RETRY_COUNT;
152 	do {
153 		si->si_bn = dblk;
154 		si->si_ma = dmabuf;
155 		si->si_cc = size;
156 		xcnt = (*ops->b_strategy)(si, si_flag);
157 	} while ((xcnt <= 0) && (--retry > 0));
158 
159 	dvma_mapout(dmabuf, size);
160 
161 #ifdef DEBUG_PROM
162 	if (debug > 1)
163 		printf("disk_strategy: xcnt = %x retries=%d\n",
164 		   xcnt, RETRY_COUNT - retry);
165 #endif
166 
167 	if (xcnt <= 0)
168 		return (EIO);
169 
170 	*rsize = xcnt;
171 	return (0);
172 }
173 
174 int
175 disk_ioctl(f, cmd, data)
176 	struct open_file *f;
177 	u_long cmd;
178 	void *data;
179 {
180 	return EIO;
181 }
182 
183