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