sd.c revision 1.16 1 /* $NetBSD: sd.c,v 1.16 2023/02/09 15:20:40 tsutsui Exp $ */
2 /*
3 * Copyright (c) 1994 Rolf Grossmann
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Rolf Grossmann.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/disklabel.h>
34 #include <sys/bootblock.h>
35 #include <dev/scsipi/scsi_spc.h>
36 #include <dev/scsipi/scsipi_all.h>
37 #include <dev/scsipi/scsi_all.h>
38 #include <dev/scsipi/scsipi_disk.h>
39 #include <lib/libsa/stand.h>
40 #include <lib/libkern/libkern.h> /* for bzero() */
41 #include "dmareg.h"
42
43 #ifdef SD_DEBUG
44 #define DPRINTF(x) printf x;
45 #else
46 #define DPRINTF(x)
47 #endif
48
49 struct sdminilabel {
50 u_short npart;
51 long offset[MAXPARTITIONS+1]; /* offset from absolute block 0! */
52 };
53
54 struct sd_softc {
55 int sc_unit;
56 int sc_lun;
57 int sc_part;
58 int sc_dev_bsize;
59 struct sdminilabel sc_pinfo;
60 };
61
62 #define NSD 7
63 #define MAXRETRIES 5 /* must be at least one */
64
65 void scsi_init(void);
66 int scsiicmd(char, char, u_char *, int, char *, int *);
67 int sdstrategy(struct sd_softc *ss, int rw, daddr_t dblk, size_t size,
68 void *buf, size_t *rsize);
69 int sdprobe(char target, char lun);
70 int sdgetinfo(struct sd_softc *ss);
71 int sdopen(struct open_file *f, char count, char lun, char part);
72 int sdclose(struct open_file *f);
73
74 int
75 sdprobe(char target, char lun)
76 {
77 struct scsi_test_unit_ready cdb1;
78 struct scsipi_inquiry cdb2;
79 struct scsipi_inquiry_data inq;
80 int error, retries;
81 int count;
82
83 memset(&cdb1, 0, sizeof(cdb1));
84 cdb1.opcode = SCSI_TEST_UNIT_READY;
85
86 retries = 0;
87 do {
88 count = 0;
89 error = scsiicmd(target, lun, (u_char *)&cdb1, sizeof(cdb1), NULL, &count);
90 if (error == -SCSI_BUSY) {
91 register int N = 10000000; while (--N > 0);
92 }
93 } while ((error == -SCSI_CHECK || error == -SCSI_BUSY)
94 && retries++ < MAXRETRIES);
95
96 if (error)
97 return error<0 ? ENODEV : error;
98
99 memset(&cdb2, 0, sizeof(cdb2));
100 cdb2.opcode = INQUIRY;
101 cdb2.length = SCSIPI_INQUIRY_LENGTH_SCSI2;
102 count = SCSIPI_INQUIRY_LENGTH_SCSI2;
103 error = scsiicmd(target, lun, (u_char *)&cdb2, sizeof(cdb2),
104 (char *)&inq, &count);
105 if (error != 0)
106 return error<0 ? EHER : error;
107
108 if ((inq.device & SID_TYPE) != T_DIRECT
109 && (inq.device & SID_TYPE) != T_CDROM)
110 return EUNIT; /* not a disk */
111
112 DPRINTF(("booting disk %s.\n", inq.vendor));
113
114 return 0;
115 }
116
117 int
118 sdgetinfo(struct sd_softc *ss)
119 {
120 struct scsipi_read_capacity_10 cdb;
121 struct scsipi_read_capacity_10_data cap;
122 struct sdminilabel *pi = &ss->sc_pinfo;
123 struct next68k_disklabel *label;
124 int error, i, blklen;
125 char io_buf[NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET];
126 int count;
127 int sc_blkshift = 0;
128
129 memset(&cdb, 0, sizeof(cdb));
130 cdb.opcode = READ_CAPACITY_10;
131 count = sizeof(cap);
132 error = scsiicmd(ss->sc_unit, ss->sc_lun, (u_char *)&cdb, sizeof(cdb),
133 (char *)&cap, &count);
134 if (error != 0)
135 return error<0 ? EHER : error;
136 blklen = (cap.length[0]<<24) + (cap.length[1]<<16)
137 + (cap.length[2]<<8) + cap.length[3];
138
139 /* avoid division by zero trap even on possible xfer errors */
140 if (blklen == 0)
141 blklen = DEV_BSIZE;
142 ss->sc_dev_bsize = blklen;
143
144 ss->sc_pinfo.offset[ss->sc_part] = 0; /* read absolute sector */
145 error = sdstrategy(ss, F_READ, NEXT68K_LABEL_SECTOR,
146 NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET, io_buf, (unsigned int *)&i);
147 if (error != 0) {
148 DPRINTF(("sdgetinfo: sdstrategy error %d\n", error));
149 return(ERDLAB);
150 }
151 label = (struct next68k_disklabel *)(io_buf+NEXT68K_LABEL_OFFSET);
152
153 if (!IS_DISKLABEL(label)) /* || (label->cd_flags & CD_UNINIT)!=0) */
154 return EUNLAB; /* bad magic */
155
156 /* XXX calculate checksum ... for now we rely on the magic number */
157 DPRINTF(("Disk is %s (%s, %s).\n",
158 label->cd_label,label->cd_name, label->cd_type));
159
160 while (label->cd_secsize > blklen)
161 {
162 blklen <<= 1;
163 ++sc_blkshift;
164 }
165 if (label->cd_secsize < blklen)
166 {
167 printf("bad label sectorsize (%d) or device blocksize (%d).\n",
168 label->cd_secsize, blklen>>sc_blkshift);
169 return ENXIO;
170 }
171 pi->npart = 0;
172 for(i=0; i<MAXPARTITIONS; i++) {
173 if (label->cd_partitions[i].cp_size > 0) {
174 pi->offset[pi->npart] = (label->cd_partitions[i].cp_offset
175 + label->cd_front) << sc_blkshift;
176 }
177 else
178 pi->offset[pi->npart] = -1;
179 DPRINTF (("%d: [%d]=%ld\n", i, pi->npart, pi->offset[pi->npart]));
180 pi->npart++;
181 if (pi->npart == RAW_PART)
182 pi->npart++;
183 }
184 pi->offset[RAW_PART] = -1;
185
186 return 0;
187 }
188
189 int
190 sdopen(struct open_file *f, char count, char lun, char part)
191 {
192 register struct sd_softc *ss;
193 char unit, cnt;
194 int error;
195
196 DPRINTF(("open: sd(%d,%d,%d)\n", count, lun, part));
197
198 if (lun >= NSD)
199 return EUNIT;
200
201 scsi_init();
202
203 for(cnt=0, unit=0; unit < NSD; unit++)
204 {
205 DPRINTF(("trying target %d lun %d.\n", unit, lun));
206 error = sdprobe(unit, lun);
207 if (error == 0)
208 {
209 if (cnt++ == count)
210 break;
211 }
212 else if (error != EUNIT)
213 return error;
214 }
215
216 if (unit >= NSD)
217 return EUNIT;
218
219 ss = alloc(sizeof(struct sd_softc));
220 ss->sc_unit = unit;
221 ss->sc_lun = lun;
222 ss->sc_part = part;
223
224 if ((error = sdgetinfo(ss)) != 0)
225 return error;
226
227 if ((unsigned char)part >= ss->sc_pinfo.npart
228 || ss->sc_pinfo.offset[(int)part] == -1)
229 return EPART;
230
231 f->f_devdata = ss;
232 return 0;
233 }
234
235 int
236 sdclose(struct open_file *f)
237 {
238 register struct sd_softc *ss = f->f_devdata;
239
240 dealloc(ss, sizeof(struct sd_softc));
241 return 0;
242 }
243
244 int
245 sdstrategy(struct sd_softc *ss, int rw, daddr_t dblk, size_t size,
246 void *buf, size_t *rsize)
247 {
248 u_long blk = dblk + ss->sc_pinfo.offset[ss->sc_part];
249 struct scsipi_rw_10 cdb;
250 int error;
251
252 if (size == 0)
253 return 0;
254
255 if (rw != F_READ)
256 {
257 printf("sdstrategy: write not implemented.\n");
258 return EOPNOTSUPP;
259 }
260
261 *rsize = 0;
262 while (size > 0) {
263 u_long nblks;
264 int tsize;
265 if (size > MAX_DMASIZE)
266 tsize = MAX_DMASIZE;
267 else
268 tsize = size;
269
270 nblks = howmany(tsize, ss->sc_dev_bsize);
271
272 DPRINTF(("sdstrategy: read block %ld, %d bytes (%ld blks a %d bytes).\n",
273 blk, tsize, nblks, ss->sc_dev_bsize));
274
275 memset(&cdb, 0, sizeof(cdb));
276 cdb.opcode = READ_10;
277 cdb.addr[0] = (blk & 0xff000000) >> 24;
278 cdb.addr[1] = (blk & 0xff0000) >> 16;
279 cdb.addr[2] = (blk & 0xff00) >> 8;
280 cdb.addr[3] = blk & 0xff;
281 cdb.length[0] = (nblks & 0xff00) >> 8;
282 cdb.length[1] = nblks & 0xff;
283
284 error = scsiicmd(ss->sc_unit, ss->sc_lun,
285 (u_char *)&cdb, sizeof(cdb), (char *)buf + *rsize, &tsize);
286 if (error != 0)
287 {
288 DPRINTF(("sdstrategy: scsiicmd failed: %d = %s.\n", error, strerror(error)));
289 return error<0 ? EIO : error;
290 }
291 *rsize += tsize;
292 size -= tsize;
293 blk += nblks;
294 }
295 DPRINTF(("sdstrategy: read %d bytes\n", *rsize));
296 return 0;
297 }
298