xref: /netbsd-src/sys/arch/sun68k/stand/libsa/dev_disk.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: dev_disk.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_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(struct open_file *f)
104 {
105 	struct saioreq *si;
106 
107 #ifdef DEBUG_PROM
108 	if (debug)
109 		printf("disk_close: ocnt=%d\n", disk_opencount);
110 #endif
111 
112 	si = f->f_devdata;
113 	f->f_devdata = NULL;
114 	if (disk_opencount <= 0)
115 		return 0;
116 	if (--disk_opencount == 0)
117 		prom_iclose(si);
118 	return 0;
119 }
120 
121 int
122 disk_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf,
123     size_t *rsize)
124 {
125 	struct saioreq *si;
126 	struct boottab *ops;
127 	char *dmabuf;
128 	int retry, si_flag, xcnt;
129 
130 	si = devdata;
131 	ops = si->si_boottab;
132 
133 #ifdef DEBUG_PROM
134 	if (debug > 1)
135 		printf("disk_strategy: size=%d dblk=%d\n", size, dblk);
136 #endif
137 
138 	dmabuf = dvma_mapin(buf, size);
139 	si_flag = (flag == F_READ) ? SAIO_F_READ : SAIO_F_WRITE;
140 
141 	/*
142 	 * The PROM strategy will occasionally return -1 and expect
143 	 * us to try again.  From mouse@Collatz.McRCIM.McGill.EDU
144 	 */
145 	retry = RETRY_COUNT;
146 	do {
147 		si->si_bn = dblk;
148 		si->si_ma = dmabuf;
149 		si->si_cc = size;
150 		xcnt = (*ops->b_strategy)(si, si_flag);
151 	} while ((xcnt <= 0) && (--retry > 0));
152 
153 	dvma_mapout(dmabuf, size);
154 
155 #ifdef DEBUG_PROM
156 	if (debug > 1)
157 		printf("disk_strategy: xcnt = %x retries=%d\n",
158 		   xcnt, RETRY_COUNT - retry);
159 #endif
160 
161 	if (xcnt <= 0)
162 		return (EIO);
163 
164 	*rsize = xcnt;
165 	return (0);
166 }
167 
168 int
169 disk_ioctl(struct open_file *f, u_long cmd, void *data)
170 {
171 	return EIO;
172 }
173 
174