sd.c revision 1.5 1 1.5 thorpej /* $NetBSD: sd.c,v 1.5 2004/08/28 17:45:24 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1990, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.3 agc *
7 1.3 agc * This code is derived from software contributed to Berkeley by
8 1.3 agc * Van Jacobson of Lawrence Berkeley Laboratory and the Systems
9 1.3 agc * Programming Group of the University of Utah Computer Science Department.
10 1.3 agc *
11 1.3 agc * Redistribution and use in source and binary forms, with or without
12 1.3 agc * modification, are permitted provided that the following conditions
13 1.3 agc * are met:
14 1.3 agc * 1. Redistributions of source code must retain the above copyright
15 1.3 agc * notice, this list of conditions and the following disclaimer.
16 1.3 agc * 2. Redistributions in binary form must reproduce the above copyright
17 1.3 agc * notice, this list of conditions and the following disclaimer in the
18 1.3 agc * documentation and/or other materials provided with the distribution.
19 1.3 agc * 3. Neither the name of the University nor the names of its contributors
20 1.3 agc * may be used to endorse or promote products derived from this software
21 1.3 agc * without specific prior written permission.
22 1.3 agc *
23 1.3 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.3 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.3 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.3 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.3 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.3 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.3 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.3 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.3 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.3 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.3 agc * SUCH DAMAGE.
34 1.3 agc *
35 1.3 agc * from: Utah $Hdr: sd.c 1.9 92/12/21$
36 1.3 agc *
37 1.3 agc * @(#)sd.c 8.1 (Berkeley) 6/10/93
38 1.3 agc */
39 1.3 agc /*
40 1.3 agc * Copyright (c) 1988 University of Utah.
41 1.1 thorpej *
42 1.1 thorpej * This code is derived from software contributed to Berkeley by
43 1.1 thorpej * Van Jacobson of Lawrence Berkeley Laboratory and the Systems
44 1.1 thorpej * Programming Group of the University of Utah Computer Science Department.
45 1.1 thorpej *
46 1.1 thorpej * Redistribution and use in source and binary forms, with or without
47 1.1 thorpej * modification, are permitted provided that the following conditions
48 1.1 thorpej * are met:
49 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
50 1.1 thorpej * notice, this list of conditions and the following disclaimer.
51 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
53 1.1 thorpej * documentation and/or other materials provided with the distribution.
54 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
55 1.1 thorpej * must display the following acknowledgement:
56 1.1 thorpej * This product includes software developed by the University of
57 1.1 thorpej * California, Berkeley and its contributors.
58 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
59 1.1 thorpej * may be used to endorse or promote products derived from this software
60 1.1 thorpej * without specific prior written permission.
61 1.1 thorpej *
62 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.1 thorpej * SUCH DAMAGE.
73 1.1 thorpej *
74 1.1 thorpej * from: Utah $Hdr: sd.c 1.9 92/12/21$
75 1.1 thorpej *
76 1.1 thorpej * @(#)sd.c 8.1 (Berkeley) 6/10/93
77 1.1 thorpej */
78 1.1 thorpej
79 1.1 thorpej /*
80 1.1 thorpej * SCSI CCS disk driver
81 1.1 thorpej */
82 1.1 thorpej
83 1.1 thorpej #include <sys/param.h>
84 1.1 thorpej #include <sys/disklabel.h>
85 1.1 thorpej
86 1.4 tsutsui #include <machine/stdarg.h>
87 1.4 tsutsui
88 1.1 thorpej #include <lib/libsa/stand.h>
89 1.1 thorpej
90 1.1 thorpej #include <hp300/stand/common/samachdep.h>
91 1.4 tsutsui #include <hp300/stand/common/conf.h>
92 1.1 thorpej
93 1.1 thorpej #define _IOCTL_
94 1.2 tsutsui #include <hp300/stand/common/scsireg.h>
95 1.4 tsutsui #include <hp300/stand/common/scsivar.h>
96 1.1 thorpej
97 1.1 thorpej struct sdminilabel {
98 1.1 thorpej u_short npart;
99 1.1 thorpej u_long offset[MAXPARTITIONS];
100 1.1 thorpej };
101 1.1 thorpej
102 1.1 thorpej struct sd_softc {
103 1.1 thorpej int sc_ctlr;
104 1.1 thorpej int sc_unit;
105 1.1 thorpej int sc_part;
106 1.1 thorpej char sc_retry;
107 1.1 thorpej char sc_alive;
108 1.1 thorpej short sc_blkshift;
109 1.1 thorpej struct sdminilabel sc_pinfo;
110 1.4 tsutsui };
111 1.1 thorpej
112 1.1 thorpej #define SDRETRY 2
113 1.1 thorpej
114 1.4 tsutsui static int sdinit(int ,int);
115 1.4 tsutsui static int sdgetinfo(struct sd_softc *);
116 1.4 tsutsui
117 1.4 tsutsui struct disklabel sdlabel;
118 1.4 tsutsui struct sd_softc sd_softc[NSCSI][NSD];
119 1.4 tsutsui
120 1.4 tsutsui static int
121 1.1 thorpej sdinit(ctlr, unit)
122 1.1 thorpej int ctlr, unit;
123 1.1 thorpej {
124 1.4 tsutsui struct sd_softc *ss = &sd_softc[ctlr][unit];
125 1.1 thorpej u_char stat;
126 1.1 thorpej int capbuf[2];
127 1.1 thorpej
128 1.1 thorpej stat = scsi_test_unit_rdy(ctlr, unit);
129 1.1 thorpej if (stat) {
130 1.1 thorpej /* drive may be doing RTZ - wait a bit */
131 1.1 thorpej if (stat == STS_CHECKCOND) {
132 1.1 thorpej DELAY(1000000);
133 1.1 thorpej stat = scsi_test_unit_rdy(ctlr, unit);
134 1.1 thorpej }
135 1.1 thorpej if (stat) {
136 1.1 thorpej printf("sd(%d,%d,0,0): init failed (stat=%x)\n",
137 1.1 thorpej ctlr, unit, stat);
138 1.4 tsutsui return 0;
139 1.1 thorpej }
140 1.1 thorpej }
141 1.1 thorpej /*
142 1.1 thorpej * try to get the drive block size.
143 1.1 thorpej */
144 1.1 thorpej capbuf[0] = 0;
145 1.1 thorpej capbuf[1] = 0;
146 1.1 thorpej stat = scsi_read_capacity(ctlr, unit,
147 1.1 thorpej (u_char *)capbuf, sizeof(capbuf));
148 1.1 thorpej if (stat == 0) {
149 1.1 thorpej if (capbuf[1] > DEV_BSIZE)
150 1.1 thorpej for (; capbuf[1] > DEV_BSIZE; capbuf[1] >>= 1)
151 1.1 thorpej ++ss->sc_blkshift;
152 1.1 thorpej }
153 1.1 thorpej ss->sc_alive = 1;
154 1.4 tsutsui return 1;
155 1.1 thorpej }
156 1.1 thorpej
157 1.1 thorpej char io_buf[MAXBSIZE];
158 1.1 thorpej
159 1.4 tsutsui static int
160 1.1 thorpej sdgetinfo(ss)
161 1.4 tsutsui struct sd_softc *ss;
162 1.1 thorpej {
163 1.4 tsutsui struct sdminilabel *pi = &ss->sc_pinfo;
164 1.4 tsutsui struct disklabel *lp = &sdlabel;
165 1.4 tsutsui char *msg;
166 1.4 tsutsui int err, savepart;
167 1.1 thorpej size_t i;
168 1.1 thorpej
169 1.4 tsutsui memset((caddr_t)lp, 0, sizeof *lp);
170 1.1 thorpej lp->d_secsize = (DEV_BSIZE << ss->sc_blkshift);
171 1.1 thorpej
172 1.1 thorpej /* Disklabel is always from RAW_PART. */
173 1.1 thorpej savepart = ss->sc_part;
174 1.1 thorpej ss->sc_part = RAW_PART;
175 1.1 thorpej err = sdstrategy(ss, F_READ, LABELSECTOR,
176 1.1 thorpej lp->d_secsize ? lp->d_secsize : DEV_BSIZE, io_buf, &i);
177 1.1 thorpej ss->sc_part = savepart;
178 1.1 thorpej
179 1.1 thorpej if (err) {
180 1.1 thorpej printf("sdgetinfo: sdstrategy error %d\n", err);
181 1.4 tsutsui return 0;
182 1.1 thorpej }
183 1.1 thorpej
184 1.1 thorpej msg = getdisklabel(io_buf, lp);
185 1.1 thorpej if (msg) {
186 1.5 thorpej printf("sd(%d,%d,%d): WARNING: %s\n",
187 1.1 thorpej ss->sc_ctlr, ss->sc_unit, ss->sc_part, msg);
188 1.1 thorpej pi->npart = 3;
189 1.1 thorpej pi->offset[0] = pi->offset[1] = -1;
190 1.1 thorpej pi->offset[2] = 0;
191 1.1 thorpej } else {
192 1.1 thorpej pi->npart = lp->d_npartitions;
193 1.1 thorpej for (i = 0; i < pi->npart; i++)
194 1.1 thorpej pi->offset[i] = lp->d_partitions[i].p_size == 0 ?
195 1.1 thorpej -1 : lp->d_partitions[i].p_offset;
196 1.1 thorpej }
197 1.4 tsutsui return 1;
198 1.1 thorpej }
199 1.1 thorpej
200 1.4 tsutsui int
201 1.4 tsutsui sdopen(struct open_file *f, ...)
202 1.4 tsutsui {
203 1.4 tsutsui va_list ap;
204 1.1 thorpej int ctlr, unit, part;
205 1.4 tsutsui struct sd_softc *ss;
206 1.4 tsutsui
207 1.4 tsutsui va_start(ap, f);
208 1.4 tsutsui ctlr = va_arg(ap, int);
209 1.4 tsutsui unit = va_arg(ap, int);
210 1.4 tsutsui part = va_arg(ap, int);
211 1.4 tsutsui va_end(ap);
212 1.1 thorpej
213 1.1 thorpej #ifdef SD_DEBUG
214 1.1 thorpej if (debug)
215 1.1 thorpej printf("sdopen: ctlr=%d unit=%d part=%d\n",
216 1.1 thorpej ctlr, unit, part);
217 1.1 thorpej #endif
218 1.1 thorpej
219 1.1 thorpej if (ctlr >= NSCSI || scsialive(ctlr) == 0)
220 1.4 tsutsui return EADAPT;
221 1.1 thorpej if (unit >= NSD)
222 1.4 tsutsui return ECTLR;
223 1.1 thorpej ss = &sd_softc[ctlr][unit];
224 1.1 thorpej ss->sc_part = part;
225 1.1 thorpej ss->sc_unit = unit;
226 1.1 thorpej ss->sc_ctlr = ctlr;
227 1.1 thorpej if (ss->sc_alive == 0) {
228 1.1 thorpej if (sdinit(ctlr, unit) == 0)
229 1.4 tsutsui return ENXIO;
230 1.1 thorpej if (sdgetinfo(ss) == 0)
231 1.4 tsutsui return ERDLAB;
232 1.1 thorpej }
233 1.1 thorpej if (part != RAW_PART && /* always allow RAW_PART to be opened */
234 1.1 thorpej (part >= ss->sc_pinfo.npart || ss->sc_pinfo.offset[part] == -1))
235 1.4 tsutsui return EPART;
236 1.1 thorpej f->f_devdata = (void *)ss;
237 1.4 tsutsui return 0;
238 1.1 thorpej }
239 1.1 thorpej
240 1.4 tsutsui int
241 1.1 thorpej sdclose(f)
242 1.1 thorpej struct open_file *f;
243 1.1 thorpej {
244 1.1 thorpej struct sd_softc *ss = f->f_devdata;
245 1.1 thorpej
246 1.1 thorpej /*
247 1.1 thorpej * Mark the disk `not alive' so that the disklabel
248 1.1 thorpej * will be re-loaded at next open.
249 1.1 thorpej */
250 1.4 tsutsui memset(ss, 0, sizeof(sd_softc));
251 1.1 thorpej f->f_devdata = NULL;
252 1.1 thorpej
253 1.4 tsutsui return 0;
254 1.1 thorpej }
255 1.1 thorpej
256 1.4 tsutsui int
257 1.4 tsutsui sdstrategy(devdata, func, dblk, size, v_buf, rsize)
258 1.4 tsutsui void *devdata;
259 1.1 thorpej int func;
260 1.1 thorpej daddr_t dblk;
261 1.1 thorpej size_t size;
262 1.1 thorpej void *v_buf;
263 1.1 thorpej size_t *rsize;
264 1.1 thorpej {
265 1.4 tsutsui struct sd_softc *ss = devdata;
266 1.1 thorpej char *buf = v_buf;
267 1.4 tsutsui int ctlr = ss->sc_ctlr;
268 1.4 tsutsui int unit = ss->sc_unit;
269 1.1 thorpej u_int nblk = size >> ss->sc_blkshift;
270 1.1 thorpej daddr_t blk;
271 1.1 thorpej char stat;
272 1.1 thorpej
273 1.1 thorpej if (size == 0)
274 1.4 tsutsui return 0;
275 1.1 thorpej
276 1.1 thorpej /*
277 1.1 thorpej * Don't do partition translation on the `raw partition'.
278 1.1 thorpej */
279 1.1 thorpej blk = (dblk + ((ss->sc_part == RAW_PART) ? 0 :
280 1.1 thorpej ss->sc_pinfo.offset[ss->sc_part])) >> ss->sc_blkshift;
281 1.1 thorpej
282 1.1 thorpej ss->sc_retry = 0;
283 1.1 thorpej
284 1.1 thorpej #ifdef SD_DEBUG
285 1.1 thorpej if (debug)
286 1.1 thorpej printf("sdstrategy(%d,%d): size=%d blk=%d nblk=%d\n",
287 1.1 thorpej ctlr, unit, size, blk, nblk);
288 1.1 thorpej #endif
289 1.1 thorpej
290 1.1 thorpej retry:
291 1.1 thorpej if (func == F_READ)
292 1.1 thorpej stat = scsi_tt_read(ctlr, unit, buf, size, blk, nblk);
293 1.1 thorpej else
294 1.1 thorpej stat = scsi_tt_write(ctlr, unit, buf, size, blk, nblk);
295 1.1 thorpej if (stat) {
296 1.1 thorpej printf("sd(%d,%d,%d): block=%x, error=0x%x\n",
297 1.1 thorpej ctlr, unit, ss->sc_part, blk, stat);
298 1.1 thorpej if (++ss->sc_retry > SDRETRY)
299 1.4 tsutsui return EIO;
300 1.1 thorpej goto retry;
301 1.1 thorpej }
302 1.1 thorpej *rsize = size;
303 1.1 thorpej
304 1.4 tsutsui return 0;
305 1.1 thorpej }
306