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