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