summaryrefslogtreecommitdiff
path: root/sys/arch/next68k/stand/boot/sd.c
blob: c8a3fbd4e7551377cf935848d73c0de6ac30302a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*      $NetBSD: sd.c,v 1.17 2023/02/12 08:25:09 tsutsui Exp $        */
/*
 * Copyright (c) 1994 Rolf Grossmann
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Rolf Grossmann.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/bootblock.h>
#include <dev/scsipi/scsi_spc.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_disk.h>
#include <lib/libsa/stand.h>
#include <lib/libkern/libkern.h> /* for bzero() */

#include "dmareg.h"
#include "scsivar.h"
#include "samachdep.h"

#ifdef SD_DEBUG
#define DPRINTF(x) printf x;
#else
#define DPRINTF(x)
#endif

struct sdminilabel {
    u_short npart;
    long offset[MAXPARTITIONS+1]; /* offset from absolute block 0! */
};

struct  sd_softc {
    int sc_unit;
    int sc_lun;
    int sc_part;
    int sc_dev_bsize;
    struct sdminilabel sc_pinfo;
};

#define NSD 7
#define MAXRETRIES 5	/* must be at least one */

int sdprobe(char target, char lun);
int sdgetinfo(struct sd_softc *ss);

int
sdprobe(char target, char lun)
{
    struct scsi_test_unit_ready cdb1;
    struct scsipi_inquiry cdb2;
    struct scsipi_inquiry_data inq;
    int error, retries;
    int count;

    memset(&cdb1, 0, sizeof(cdb1));
    cdb1.opcode =  SCSI_TEST_UNIT_READY;

    retries = 0;
    do {
	    count = 0;
	error = scsiicmd(target, lun, (u_char *)&cdb1, sizeof(cdb1), NULL, &count);
	if (error == -SCSI_BUSY) {
		register int N = 10000000; while (--N > 0);
	}
    } while ((error == -SCSI_CHECK || error == -SCSI_BUSY)
	     && retries++ < MAXRETRIES);

    if (error)
	return error<0 ? ENODEV : error;

    memset(&cdb2, 0, sizeof(cdb2));
    cdb2.opcode = INQUIRY;
    cdb2.length = SCSIPI_INQUIRY_LENGTH_SCSI2;
    count = SCSIPI_INQUIRY_LENGTH_SCSI2;
    error = scsiicmd(target, lun, (u_char *)&cdb2, sizeof(cdb2),
		     (char *)&inq, &count);
    if (error != 0)
      return error<0 ? EHER : error;

    if ((inq.device & SID_TYPE) != T_DIRECT
	&& (inq.device & SID_TYPE) != T_CDROM)
      return EUNIT;	/* not a disk */

    DPRINTF(("booting disk %s.\n", inq.vendor));

    return 0;
}

int
sdgetinfo(struct sd_softc *ss)
{
    struct scsipi_read_capacity_10 cdb;
    struct scsipi_read_capacity_10_data cap;
    struct sdminilabel *pi = &ss->sc_pinfo;
    struct next68k_disklabel *label;
    int error, i, blklen;
    char io_buf[NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET];
    int count;
    int sc_blkshift = 0;

    memset(&cdb, 0, sizeof(cdb));
    cdb.opcode = READ_CAPACITY_10;
    count = sizeof(cap);
    error = scsiicmd(ss->sc_unit, ss->sc_lun, (u_char *)&cdb, sizeof(cdb),
		     (char *)&cap, &count);
    if (error != 0)
	return error<0 ? EHER : error;
    blklen = (cap.length[0]<<24) + (cap.length[1]<<16)
	     + (cap.length[2]<<8) + cap.length[3];

    /* avoid division by zero trap even on possible xfer errors */
    if (blklen == 0)
	blklen = DEV_BSIZE;
    ss->sc_dev_bsize = blklen;

    ss->sc_pinfo.offset[ss->sc_part] = 0; /* read absolute sector */
    error = sdstrategy(ss, F_READ, NEXT68K_LABEL_SECTOR,
		       NEXT68K_LABEL_SIZE+NEXT68K_LABEL_OFFSET, io_buf, (unsigned int *)&i);
    if (error != 0) {
	DPRINTF(("sdgetinfo: sdstrategy error %d\n", error));
        return(ERDLAB);
    }
    label = (struct next68k_disklabel *)(io_buf+NEXT68K_LABEL_OFFSET);

    if (!IS_DISKLABEL(label)) /*  || (label->cd_flags & CD_UNINIT)!=0) */
	return EUNLAB;		/* bad magic */

    /* XXX calculate checksum ... for now we rely on the magic number */
    DPRINTF(("Disk is %s (%s, %s).\n",
	     label->cd_label,label->cd_name, label->cd_type));

    while (label->cd_secsize > blklen)
    {
	blklen <<= 1;
	++sc_blkshift;
    }
    if (label->cd_secsize < blklen)
    {
	printf("bad label sectorsize (%d) or device blocksize (%d).\n",
	       label->cd_secsize, blklen>>sc_blkshift);
	return ENXIO;
    }
    pi->npart = 0;
    for(i=0; i<MAXPARTITIONS; i++) {
	if (label->cd_partitions[i].cp_size > 0) {
	    pi->offset[pi->npart] = (label->cd_partitions[i].cp_offset
				     + label->cd_front) << sc_blkshift;
	}
	else
	    pi->offset[pi->npart] = -1;
	DPRINTF (("%d: [%d]=%ld\n", i, pi->npart, pi->offset[pi->npart]));
	pi->npart++;
	if (pi->npart == RAW_PART)
	    pi->npart++;
    }
    pi->offset[RAW_PART] = -1;

    return 0;
}

int
sdopen(struct open_file *f, ...)
{
    va_list ap;
    int count, lun, part;
    register struct sd_softc *ss;
    char unit, cnt;
    int error;

    va_start(ap, f);
    count = va_arg(ap, int);
    lun = va_arg(ap, int);
    part = va_arg(ap, int);
    va_end(ap);

    DPRINTF(("open: sd(%d,%d,%d)\n", count, lun, part));

    if (lun >= NSD)
	return EUNIT;

    scsi_init();

    for(cnt=0, unit=0; unit < NSD; unit++)
    {
	DPRINTF(("trying target %d lun %d.\n", unit, lun));
	error = sdprobe(unit, lun);
	if (error == 0)
	{
	    if (cnt++ == count)
		break;
	}
	else if (error != EUNIT)
	    return error;
    }

    if (unit >= NSD)
	return EUNIT;

    ss = alloc(sizeof(struct sd_softc));
    ss->sc_unit = unit;
    ss->sc_lun = lun;
    ss->sc_part = part;

    if ((error = sdgetinfo(ss)) != 0)
	return error;

    if ((unsigned char)part >= ss->sc_pinfo.npart
	|| ss->sc_pinfo.offset[(int)part] == -1)
	return EPART;

    f->f_devdata = ss;
    return 0;
}

int
sdclose(struct open_file *f)
{
    register struct sd_softc *ss = f->f_devdata;

    dealloc(ss, sizeof(struct sd_softc));
    return 0;
}

int
sdstrategy(void *devdata, int rw, daddr_t dblk, size_t size,
	   void *buf, size_t *rsize)
{
    struct sd_softc *ss = devdata;
    u_long blk = dblk + ss->sc_pinfo.offset[ss->sc_part];
    struct scsipi_rw_10 cdb;
    int error;

    if (size == 0)
	return 0;

    if (rw != F_READ)
    {
	printf("sdstrategy: write not implemented.\n");
	return EOPNOTSUPP;
    }

    *rsize = 0;
    while (size > 0) {
	    u_long nblks;
	    int tsize;
	    if (size > MAX_DMASIZE)
		    tsize = MAX_DMASIZE;
	    else
		    tsize = size;

	    nblks = howmany(tsize, ss->sc_dev_bsize);

	    DPRINTF(("sdstrategy: read block %ld, %d bytes (%ld blks a %d bytes).\n",
		     blk, tsize, nblks, ss->sc_dev_bsize));

	    memset(&cdb, 0, sizeof(cdb));
	    cdb.opcode = READ_10;
	    cdb.addr[0] = (blk & 0xff000000) >> 24;
	    cdb.addr[1] = (blk & 0xff0000) >> 16;
	    cdb.addr[2] = (blk & 0xff00) >> 8;
	    cdb.addr[3] = blk & 0xff;
	    cdb.length[0] = (nblks & 0xff00) >> 8;
	    cdb.length[1] = nblks & 0xff;

	    error = scsiicmd(ss->sc_unit, ss->sc_lun,
			     (u_char *)&cdb, sizeof(cdb), (char *)buf + *rsize, &tsize);
	    if (error != 0)
	    {
		    DPRINTF(("sdstrategy: scsiicmd failed: %d = %s.\n", error, strerror(error)));
		    return error<0 ? EIO : error;
	    }
	    *rsize += tsize;
	    size -= tsize;
	    blk += nblks;
    }
    DPRINTF(("sdstrategy: read %d bytes\n", *rsize));
    return 0;
}