1*7f27d7e1Stsutsui /* $NetBSD: sd.c,v 1.13 2023/06/24 07:02:11 tsutsui Exp $ */
2df6945b8Stsutsui
3df6945b8Stsutsui /*
4df6945b8Stsutsui * Copyright (c) 1992 OMRON Corporation.
5df6945b8Stsutsui *
6df6945b8Stsutsui * This code is derived from software contributed to Berkeley by
7df6945b8Stsutsui * OMRON Corporation.
8df6945b8Stsutsui *
9df6945b8Stsutsui * Redistribution and use in source and binary forms, with or without
10df6945b8Stsutsui * modification, are permitted provided that the following conditions
11df6945b8Stsutsui * are met:
12df6945b8Stsutsui * 1. Redistributions of source code must retain the above copyright
13df6945b8Stsutsui * notice, this list of conditions and the following disclaimer.
14df6945b8Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
15df6945b8Stsutsui * notice, this list of conditions and the following disclaimer in the
16df6945b8Stsutsui * documentation and/or other materials provided with the distribution.
17df6945b8Stsutsui * 3. All advertising materials mentioning features or use of this software
18df6945b8Stsutsui * must display the following acknowledgement:
19df6945b8Stsutsui * This product includes software developed by the University of
20df6945b8Stsutsui * California, Berkeley and its contributors.
21df6945b8Stsutsui * 4. Neither the name of the University nor the names of its contributors
22df6945b8Stsutsui * may be used to endorse or promote products derived from this software
23df6945b8Stsutsui * without specific prior written permission.
24df6945b8Stsutsui *
25df6945b8Stsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26df6945b8Stsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27df6945b8Stsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28df6945b8Stsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29df6945b8Stsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30df6945b8Stsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31df6945b8Stsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32df6945b8Stsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33df6945b8Stsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34df6945b8Stsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35df6945b8Stsutsui * SUCH DAMAGE.
36df6945b8Stsutsui *
37df6945b8Stsutsui * @(#)sd.c 8.1 (Berkeley) 6/10/93
38df6945b8Stsutsui */
39df6945b8Stsutsui /*
40df6945b8Stsutsui * Copyright (c) 1992, 1993
41df6945b8Stsutsui * The Regents of the University of California. All rights reserved.
42df6945b8Stsutsui *
43df6945b8Stsutsui * This code is derived from software contributed to Berkeley by
44df6945b8Stsutsui * OMRON Corporation.
45df6945b8Stsutsui *
46df6945b8Stsutsui * Redistribution and use in source and binary forms, with or without
47df6945b8Stsutsui * modification, are permitted provided that the following conditions
48df6945b8Stsutsui * are met:
49df6945b8Stsutsui * 1. Redistributions of source code must retain the above copyright
50df6945b8Stsutsui * notice, this list of conditions and the following disclaimer.
51df6945b8Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
52df6945b8Stsutsui * notice, this list of conditions and the following disclaimer in the
53df6945b8Stsutsui * documentation and/or other materials provided with the distribution.
54df6945b8Stsutsui * 3. Neither the name of the University nor the names of its contributors
55df6945b8Stsutsui * may be used to endorse or promote products derived from this software
56df6945b8Stsutsui * without specific prior written permission.
57df6945b8Stsutsui *
58df6945b8Stsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59df6945b8Stsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60df6945b8Stsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61df6945b8Stsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62df6945b8Stsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63df6945b8Stsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64df6945b8Stsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65df6945b8Stsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66df6945b8Stsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67df6945b8Stsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68df6945b8Stsutsui * SUCH DAMAGE.
69df6945b8Stsutsui *
70df6945b8Stsutsui * @(#)sd.c 8.1 (Berkeley) 6/10/93
71df6945b8Stsutsui */
72df6945b8Stsutsui
73df6945b8Stsutsui /*
74df6945b8Stsutsui * sd.c -- SCSI DISK device driver
75df6945b8Stsutsui * by A.Fujita, FEB-26-1992
76df6945b8Stsutsui */
77df6945b8Stsutsui
78df6945b8Stsutsui
79df6945b8Stsutsui /*
80df6945b8Stsutsui * SCSI CCS (Command Command Set) disk driver.
81df6945b8Stsutsui */
82df6945b8Stsutsui #include <sys/param.h>
83df6945b8Stsutsui #include <sys/disklabel.h>
84df6945b8Stsutsui #include <luna68k/stand/boot/samachdep.h>
85df6945b8Stsutsui #include <luna68k/stand/boot/scsireg.h>
86df6945b8Stsutsui
87df6945b8Stsutsui struct sd_softc {
8825e766daStsutsui int sc_unit;
8925e766daStsutsui uint sc_ctlr;
9025e766daStsutsui uint sc_tgt;
9125e766daStsutsui uint sc_lun;
9225e766daStsutsui int sc_part;
9325e766daStsutsui uint sc_bshift; /* convert device blocks to DEV_BSIZE blks */
9425e766daStsutsui uint sc_blks; /* number of blocks on device */
95df6945b8Stsutsui int sc_blksize; /* device block size in bytes */
9625e766daStsutsui struct disklabel sc_label;
97df6945b8Stsutsui };
98df6945b8Stsutsui
9925e766daStsutsui static int sdident(struct sd_softc *);
100a628ff77Stsutsui static struct sd_softc *sdinit(uint);
101df6945b8Stsutsui
102a628ff77Stsutsui static int
sdident(struct sd_softc * sc)10325e766daStsutsui sdident(struct sd_softc *sc)
104df6945b8Stsutsui {
10525e766daStsutsui struct scsi_inquiry inqbuf;
10625e766daStsutsui uint32_t capbuf[2];
107df6945b8Stsutsui int i;
108df6945b8Stsutsui
10925e766daStsutsui if (!scident(sc->sc_ctlr, sc->sc_tgt, sc->sc_lun, &inqbuf, capbuf))
11025e766daStsutsui return -1;
111df6945b8Stsutsui
112df6945b8Stsutsui sc->sc_blks = capbuf[0];
113df6945b8Stsutsui sc->sc_blksize = capbuf[1];
114df6945b8Stsutsui
115df6945b8Stsutsui if (sc->sc_blksize != DEV_BSIZE) {
116df6945b8Stsutsui if (sc->sc_blksize < DEV_BSIZE) {
1170dfe4134Stsutsui return -1;
118df6945b8Stsutsui }
119df6945b8Stsutsui for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
120df6945b8Stsutsui ++sc->sc_bshift;
121df6945b8Stsutsui sc->sc_blks <<= sc->sc_bshift;
122df6945b8Stsutsui }
1230dfe4134Stsutsui return inqbuf.type;
124df6945b8Stsutsui }
125df6945b8Stsutsui
126a628ff77Stsutsui static struct sd_softc *
sdinit(uint unit)12725e766daStsutsui sdinit(uint unit)
128df6945b8Stsutsui {
12925e766daStsutsui struct sd_softc *sc;
130df6945b8Stsutsui struct disklabel *lp;
131df6945b8Stsutsui char *msg;
13225e766daStsutsui int type;
133df6945b8Stsutsui
134df6945b8Stsutsui #ifdef DEBUG
13525e766daStsutsui printf("sdinit: unit = %d\n", unit);
136df6945b8Stsutsui #endif
13725e766daStsutsui sc = alloc(sizeof *sc);
13825e766daStsutsui memset(sc, 0, sizeof *sc);
13925e766daStsutsui
14025e766daStsutsui sc->sc_unit = unit;
14125e766daStsutsui sc->sc_ctlr = CTLR(unit);
14225e766daStsutsui sc->sc_tgt = TARGET(unit);
14325e766daStsutsui sc->sc_lun = 0; /* XXX no LUN support yet */
14425e766daStsutsui type = sdident(sc);
14525e766daStsutsui if (type < 0)
14625e766daStsutsui return NULL;
147df6945b8Stsutsui
148df6945b8Stsutsui /*
149df6945b8Stsutsui * Use the default sizes until we've read the label,
150df6945b8Stsutsui * or longer if there isn't one there.
151df6945b8Stsutsui */
15225e766daStsutsui lp = &sc->sc_label;
153df6945b8Stsutsui
154df6945b8Stsutsui if (lp->d_secpercyl == 0) {
155df6945b8Stsutsui lp->d_secsize = DEV_BSIZE;
156df6945b8Stsutsui lp->d_nsectors = 32;
157df6945b8Stsutsui lp->d_ntracks = 20;
158df6945b8Stsutsui lp->d_secpercyl = 32*20;
159df6945b8Stsutsui lp->d_npartitions = 1;
160df6945b8Stsutsui lp->d_partitions[0].p_offset = 0;
161df6945b8Stsutsui lp->d_partitions[0].p_size = LABELSECTOR + 1;
162df6945b8Stsutsui }
163df6945b8Stsutsui
164df6945b8Stsutsui /*
165df6945b8Stsutsui * read disklabel
166df6945b8Stsutsui */
16725e766daStsutsui msg = readdisklabel(sc->sc_ctlr, sc->sc_tgt, lp);
168df6945b8Stsutsui if (msg != NULL)
16925e766daStsutsui printf("sd(%d): %s\n", unit, msg);
170df6945b8Stsutsui
17125e766daStsutsui return sc;
172df6945b8Stsutsui }
173df6945b8Stsutsui
174df6945b8Stsutsui int
sdopen(struct open_file * f,...)175df6945b8Stsutsui sdopen(struct open_file *f, ...)
176df6945b8Stsutsui {
177df6945b8Stsutsui va_list ap;
17825e766daStsutsui struct sd_softc *sc;
179df6945b8Stsutsui int unit, part;
180df6945b8Stsutsui
181df6945b8Stsutsui va_start(ap, f);
182df6945b8Stsutsui unit = va_arg(ap, int);
183df6945b8Stsutsui part = va_arg(ap, int);
184df6945b8Stsutsui va_end(ap);
185df6945b8Stsutsui
18625e766daStsutsui if (unit < 0 || CTLR(unit) >= 2 || TARGET(unit) >= 7)
1870dfe4134Stsutsui return -1;
18825e766daStsutsui if (part < 0 || part >= MAXPARTITIONS)
1890dfe4134Stsutsui return -1;
190df6945b8Stsutsui
19125e766daStsutsui sc = sdinit(unit);
19225e766daStsutsui if (sc == NULL)
19325e766daStsutsui return -1;
19425e766daStsutsui
19525e766daStsutsui sc->sc_part = part;
19625e766daStsutsui f->f_devdata = (void *)sc;
197df6945b8Stsutsui
198df6945b8Stsutsui return 0;
199df6945b8Stsutsui }
200df6945b8Stsutsui
201df6945b8Stsutsui int
sdclose(struct open_file * f)202df6945b8Stsutsui sdclose(struct open_file *f)
203df6945b8Stsutsui {
20425e766daStsutsui struct sd_softc *sc = f->f_devdata;
205df6945b8Stsutsui
20625e766daStsutsui dealloc(sc, sizeof *sc);
207df6945b8Stsutsui f->f_devdata = NULL;
208df6945b8Stsutsui
209df6945b8Stsutsui return 0;
210df6945b8Stsutsui }
211df6945b8Stsutsui
212d28b1498Stsutsui static struct scsi_generic_cdb cdb_read = {
213df6945b8Stsutsui 10,
214df6945b8Stsutsui { CMD_READ_EXT, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
215df6945b8Stsutsui };
216df6945b8Stsutsui
217d28b1498Stsutsui static struct scsi_generic_cdb cdb_write = {
218*7f27d7e1Stsutsui 10,
219df6945b8Stsutsui { CMD_WRITE_EXT, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
220df6945b8Stsutsui };
221df6945b8Stsutsui
222df6945b8Stsutsui int
sdstrategy(void * devdata,int func,daddr_t dblk,size_t size,void * v_buf,size_t * rsize)223df6945b8Stsutsui sdstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf,
224df6945b8Stsutsui size_t *rsize)
225df6945b8Stsutsui {
22625e766daStsutsui struct sd_softc *sc;
227df6945b8Stsutsui struct disklabel *lp;
22825e766daStsutsui uint8_t *buf;
229d28b1498Stsutsui struct scsi_generic_cdb *cdb;
230df6945b8Stsutsui daddr_t blk;
23125e766daStsutsui u_int nblk;
23225e766daStsutsui int stat;
233df6945b8Stsutsui #ifdef DEBUG
234df6945b8Stsutsui int i;
235df6945b8Stsutsui #endif
236df6945b8Stsutsui
23725e766daStsutsui sc = devdata;
23825e766daStsutsui if (sc == NULL)
23925e766daStsutsui return -1;
240df6945b8Stsutsui
24125e766daStsutsui buf = v_buf;
24225e766daStsutsui lp = &sc->sc_label;
24325e766daStsutsui blk = dblk + (lp->d_partitions[sc->sc_part].p_offset >> sc->sc_bshift);
24425e766daStsutsui nblk = size >> sc->sc_bshift;
245df6945b8Stsutsui
246df6945b8Stsutsui if (func == F_READ)
247df6945b8Stsutsui cdb = &cdb_read;
248df6945b8Stsutsui else
249df6945b8Stsutsui cdb = &cdb_write;
250df6945b8Stsutsui
251df6945b8Stsutsui cdb->cdb[2] = (blk & 0xff000000) >> 24;
252df6945b8Stsutsui cdb->cdb[3] = (blk & 0x00ff0000) >> 16;
253df6945b8Stsutsui cdb->cdb[4] = (blk & 0x0000ff00) >> 8;
254df6945b8Stsutsui cdb->cdb[5] = (blk & 0x000000ff);
255df6945b8Stsutsui
256df6945b8Stsutsui cdb->cdb[7] = ((nblk >> DEV_BSHIFT) & 0xff00) >> 8;
257df6945b8Stsutsui cdb->cdb[8] = ((nblk >> DEV_BSHIFT) & 0x00ff);
258df6945b8Stsutsui
259df6945b8Stsutsui #ifdef DEBUG
26025e766daStsutsui printf("sdstrategy: unit = %d\n", sc->sc_unit);
2610dfe4134Stsutsui printf("sdstrategy: blk = %lu (0x%lx), nblk = %u (0x%x)\n",
2620dfe4134Stsutsui (u_long)blk, (long)blk, nblk, nblk);
263df6945b8Stsutsui for (i = 0; i < 10; i++)
264df6945b8Stsutsui printf("sdstrategy: cdb[%d] = 0x%x\n", i, cdb->cdb[i]);
26525e766daStsutsui printf("sdstrategy: ctlr = %d, target = %d\n", sc->sc_ctlr, sc->sc_tgt);
266df6945b8Stsutsui #endif
26725e766daStsutsui stat = scsi_immed_command(sc->sc_ctlr, sc->sc_tgt, sc->sc_lun,
26825e766daStsutsui cdb, buf, size);
269d0f9358cStsutsui if (stat != 0)
270d0f9358cStsutsui return EIO;
271d0f9358cStsutsui
272df6945b8Stsutsui if (rsize)
273df6945b8Stsutsui *rsize = size;
274df6945b8Stsutsui
275df6945b8Stsutsui return 0;
276df6945b8Stsutsui }
277