1 /* $NetBSD: sd.c,v 1.8 2006/06/25 17:40:14 tsutsui Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Van Jacobson of Lawrence Berkeley Laboratory and the Systems
9 * Programming Group of the University of Utah Computer Science Department.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: Utah $Hdr: sd.c 1.9 92/12/21$
37 * @(#)sd.c 8.1 (Berkeley) 6/10/93
40 * Copyright (c) 1988 University of Utah.
42 * This code is derived from software contributed to Berkeley by
43 * Van Jacobson of Lawrence Berkeley Laboratory and the Systems
44 * Programming Group of the University of Utah Computer Science Department.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * from: Utah $Hdr: sd.c 1.9 92/12/21$
76 * @(#)sd.c 8.1 (Berkeley) 6/10/93
80 * SCSI CCS disk driver
83 #include <sys/param.h>
84 #include <sys/disklabel.h>
86 #include <machine/stdarg.h>
88 #include <lib/libsa/stand.h>
90 #include <hp300/stand/common/samachdep.h>
91 #include <hp300/stand/common/conf.h>
94 #include <hp300/stand/common/scsireg.h>
95 #include <hp300/stand/common/scsivar.h>
99 u_long offset
[MAXPARTITIONS
];
109 struct sdminilabel sc_pinfo
;
114 static int sdinit(int ,int);
115 static int sdgetinfo(struct sd_softc
*);
117 struct disklabel sdlabel
;
118 struct sd_softc sd_softc
[NSCSI
][NSD
];
121 sdinit(int ctlr
, int unit
)
123 struct sd_softc
*ss
= &sd_softc
[ctlr
][unit
];
127 stat
= scsi_test_unit_rdy(ctlr
, unit
);
129 /* drive may be doing RTZ - wait a bit */
130 if (stat
== STS_CHECKCOND
) {
132 stat
= scsi_test_unit_rdy(ctlr
, unit
);
135 printf("sd(%d,%d,0,0): init failed (stat=%x)\n",
141 * try to get the drive block size.
145 stat
= scsi_read_capacity(ctlr
, unit
,
146 (u_char
*)capbuf
, sizeof(capbuf
));
148 if (capbuf
[1] > DEV_BSIZE
)
149 for (; capbuf
[1] > DEV_BSIZE
; capbuf
[1] >>= 1)
156 char io_buf
[MAXBSIZE
];
159 sdgetinfo(struct sd_softc
*ss
)
161 struct sdminilabel
*pi
= &ss
->sc_pinfo
;
162 struct disklabel
*lp
= &sdlabel
;
167 memset((void *)lp
, 0, sizeof *lp
);
168 lp
->d_secsize
= (DEV_BSIZE
<< ss
->sc_blkshift
);
170 /* Disklabel is always from RAW_PART. */
171 savepart
= ss
->sc_part
;
172 ss
->sc_part
= RAW_PART
;
173 err
= sdstrategy(ss
, F_READ
, LABELSECTOR
,
174 lp
->d_secsize
? lp
->d_secsize
: DEV_BSIZE
, io_buf
, &i
);
175 ss
->sc_part
= savepart
;
178 printf("sdgetinfo: sdstrategy error %d\n", err
);
182 msg
= getdisklabel(io_buf
, lp
);
184 printf("sd(%d,%d,%d): WARNING: %s\n",
185 ss
->sc_ctlr
, ss
->sc_unit
, ss
->sc_part
, msg
);
187 pi
->offset
[0] = pi
->offset
[1] = -1;
190 pi
->npart
= lp
->d_npartitions
;
191 for (i
= 0; i
< pi
->npart
; i
++)
192 pi
->offset
[i
] = lp
->d_partitions
[i
].p_size
== 0 ?
193 -1 : lp
->d_partitions
[i
].p_offset
;
199 sdopen(struct open_file
*f
, ...)
202 int ctlr
, unit
, part
;
206 ctlr
= va_arg(ap
, int);
207 unit
= va_arg(ap
, int);
208 part
= va_arg(ap
, int);
213 printf("sdopen: ctlr=%d unit=%d part=%d\n",
217 if (ctlr
>= NSCSI
|| scsialive(ctlr
) == 0)
221 ss
= &sd_softc
[ctlr
][unit
];
225 if (ss
->sc_alive
== 0) {
226 if (sdinit(ctlr
, unit
) == 0)
228 if (sdgetinfo(ss
) == 0)
231 if (part
!= RAW_PART
&& /* always allow RAW_PART to be opened */
232 (part
>= ss
->sc_pinfo
.npart
|| ss
->sc_pinfo
.offset
[part
] == -1))
234 f
->f_devdata
= (void *)ss
;
239 sdclose(struct open_file
*f
)
241 struct sd_softc
*ss
= f
->f_devdata
;
244 * Mark the disk `not alive' so that the disklabel
245 * will be re-loaded at next open.
247 memset(ss
, 0, sizeof(sd_softc
));
254 sdstrategy(void *devdata
, int func
, daddr_t dblk
, size_t size
, void *v_buf
,
257 struct sd_softc
*ss
= devdata
;
258 uint8_t *buf
= v_buf
;
259 int ctlr
= ss
->sc_ctlr
;
260 int unit
= ss
->sc_unit
;
261 u_int nblk
= size
>> ss
->sc_blkshift
;
269 * Don't do partition translation on the `raw partition'.
271 blk
= (dblk
+ ((ss
->sc_part
== RAW_PART
) ? 0 :
272 ss
->sc_pinfo
.offset
[ss
->sc_part
])) >> ss
->sc_blkshift
;
278 printf("sdstrategy(%d,%d): size=%d blk=%d nblk=%d\n",
279 ctlr
, unit
, size
, blk
, nblk
);
284 stat
= scsi_tt_read(ctlr
, unit
, buf
, size
, blk
, nblk
);
286 stat
= scsi_tt_write(ctlr
, unit
, buf
, size
, blk
, nblk
);
288 printf("sd(%d,%d,%d): block=%x, error=0x%x\n",
289 ctlr
, unit
, ss
->sc_part
, blk
, stat
);
290 if (++ss
->sc_retry
> SDRETRY
)