iris_disk.c revision 1.1.2.2 1 1.1.2.2 pgoyette /* $NetBSD: iris_disk.c,v 1.1.2.2 2019/01/18 08:50:23 pgoyette Exp $ */
2 1.1.2.2 pgoyette
3 1.1.2.2 pgoyette /*
4 1.1.2.2 pgoyette * Copyright (c) 2018 Naruaki Etomi
5 1.1.2.2 pgoyette * All rights reserved.
6 1.1.2.2 pgoyette *
7 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
9 1.1.2.2 pgoyette * are met:
10 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1.2.2 pgoyette *
16 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 pgoyette * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 pgoyette * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 pgoyette * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 pgoyette * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1.2.2 pgoyette * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1.2.2 pgoyette * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1.2.2 pgoyette * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1.2.2 pgoyette * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1.2.2 pgoyette * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1.2.2 pgoyette */
27 1.1.2.2 pgoyette
28 1.1.2.2 pgoyette /*
29 1.1.2.2 pgoyette * Copyright (c) 1988 University of Utah.
30 1.1.2.2 pgoyette * Copyright (c) 1990, 1993
31 1.1.2.2 pgoyette * The Regents of the University of California. All rights reserved.
32 1.1.2.2 pgoyette *
33 1.1.2.2 pgoyette * This code is derived from software contributed to Berkeley by
34 1.1.2.2 pgoyette * Van Jacobson of Lawrence Berkeley Laboratory and the Systems
35 1.1.2.2 pgoyette * Programming Group of the University of Utah Computer Science Department.
36 1.1.2.2 pgoyette *
37 1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
38 1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
39 1.1.2.2 pgoyette * are met:
40 1.1.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
41 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
42 1.1.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
43 1.1.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
44 1.1.2.2 pgoyette * documentation and/or other materials provided with the distribution.
45 1.1.2.2 pgoyette * 3. Neither the name of the University nor the names of its contributors
46 1.1.2.2 pgoyette * may be used to endorse or promote products derived from this software
47 1.1.2.2 pgoyette * without specific prior written permission.
48 1.1.2.2 pgoyette *
49 1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1.2.2 pgoyette * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1.2.2 pgoyette * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1.2.2 pgoyette * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1.2.2 pgoyette * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1.2.2 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1.2.2 pgoyette * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1.2.2 pgoyette * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1.2.2 pgoyette * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1.2.2 pgoyette * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1.2.2 pgoyette * SUCH DAMAGE.
60 1.1.2.2 pgoyette *
61 1.1.2.2 pgoyette * from: Utah $Hdr: sd.c 1.9 92/12/21$
62 1.1.2.2 pgoyette *
63 1.1.2.2 pgoyette * @(#)sd.c 8.1 (Berkeley) 6/10/93
64 1.1.2.2 pgoyette */
65 1.1.2.2 pgoyette
66 1.1.2.2 pgoyette /*
67 1.1.2.2 pgoyette * Silicon Graphics "IRIS" series MIPS processors machine bootloader.
68 1.1.2.2 pgoyette * Disk I/O API routine.
69 1.1.2.2 pgoyette * Most of the following was adapted from /sys/arch/hp300/stand/common/sd.c.
70 1.1.2.2 pgoyette * NetBSD: sd.c,v 1.11 2011/07/17 20:54:40 joerg Exp
71 1.1.2.2 pgoyette */
72 1.1.2.2 pgoyette
73 1.1.2.2 pgoyette #include <lib/libsa/stand.h>
74 1.1.2.2 pgoyette
75 1.1.2.2 pgoyette #include <sys/param.h>
76 1.1.2.2 pgoyette #include <sys/disklabel.h>
77 1.1.2.2 pgoyette
78 1.1.2.2 pgoyette #ifndef INDIGO_R3K_MODE
79 1.1.2.2 pgoyette #include <dev/arcbios/arcbios.h>
80 1.1.2.2 pgoyette #endif
81 1.1.2.2 pgoyette
82 1.1.2.2 pgoyette #include "disk.h"
83 1.1.2.2 pgoyette
84 1.1.2.2 pgoyette #include "iris_machdep.h"
85 1.1.2.2 pgoyette #include "iris_scsivar.h"
86 1.1.2.2 pgoyette
87 1.1.2.2 pgoyette struct disk_softc {
88 1.1.2.2 pgoyette int sc_part; /* disk partition number */
89 1.1.2.2 pgoyette char sc_retry;
90 1.1.2.2 pgoyette char sc_alive;
91 1.1.2.2 pgoyette short sc_blkshift;
92 1.1.2.2 pgoyette struct disklabel sc_label; /* disk label for this disk */
93 1.1.2.2 pgoyette };
94 1.1.2.2 pgoyette
95 1.1.2.2 pgoyette static int diskinit(struct disk_softc *);
96 1.1.2.2 pgoyette
97 1.1.2.2 pgoyette #define DISKRETRY 2
98 1.1.2.2 pgoyette
99 1.1.2.2 pgoyette int
100 1.1.2.2 pgoyette diskstrategy(void *devdata, int rw, daddr_t bn, size_t reqcnt, void *addr,
101 1.1.2.2 pgoyette size_t *cnt)
102 1.1.2.2 pgoyette {
103 1.1.2.2 pgoyette struct disk_softc *sc;
104 1.1.2.2 pgoyette struct disklabel *lp;
105 1.1.2.2 pgoyette uint8_t *buf;
106 1.1.2.2 pgoyette daddr_t blk;
107 1.1.2.2 pgoyette u_int nblk;
108 1.1.2.2 pgoyette int stat;
109 1.1.2.2 pgoyette
110 1.1.2.2 pgoyette sc = devdata;
111 1.1.2.2 pgoyette
112 1.1.2.2 pgoyette buf = addr;
113 1.1.2.2 pgoyette lp = &sc->sc_label;
114 1.1.2.2 pgoyette blk = bn + (lp->d_partitions[sc->sc_part].p_offset >> sc->sc_blkshift);
115 1.1.2.2 pgoyette nblk = reqcnt >> sc->sc_blkshift;
116 1.1.2.2 pgoyette
117 1.1.2.2 pgoyette sc->sc_retry = 0;
118 1.1.2.2 pgoyette
119 1.1.2.2 pgoyette retry:
120 1.1.2.2 pgoyette stat = scsi_read(buf, reqcnt, blk, nblk);
121 1.1.2.2 pgoyette
122 1.1.2.2 pgoyette if (stat) {
123 1.1.2.2 pgoyette DELAY(1000000);
124 1.1.2.2 pgoyette if (++sc->sc_retry > DISKRETRY) {
125 1.1.2.2 pgoyette return EIO;
126 1.1.2.2 pgoyette }
127 1.1.2.2 pgoyette printf("diskstrategy retry\n");
128 1.1.2.2 pgoyette goto retry;
129 1.1.2.2 pgoyette }
130 1.1.2.2 pgoyette
131 1.1.2.2 pgoyette *cnt = reqcnt;
132 1.1.2.2 pgoyette
133 1.1.2.2 pgoyette return 0;
134 1.1.2.2 pgoyette }
135 1.1.2.2 pgoyette
136 1.1.2.2 pgoyette static int
137 1.1.2.2 pgoyette diskinit(struct disk_softc *sc)
138 1.1.2.2 pgoyette {
139 1.1.2.2 pgoyette uint8_t capbuf[2];
140 1.1.2.2 pgoyette
141 1.1.2.2 pgoyette uint8_t stat;
142 1.1.2.2 pgoyette
143 1.1.2.2 pgoyette stat = scsi_test_unit_rdy();
144 1.1.2.2 pgoyette
145 1.1.2.2 pgoyette if (stat != 0) {
146 1.1.2.2 pgoyette /* drive may be doing RTZ - wait a bit */
147 1.1.2.2 pgoyette DELAY(1000000);
148 1.1.2.2 pgoyette stat = scsi_test_unit_rdy();
149 1.1.2.2 pgoyette
150 1.1.2.2 pgoyette if (stat != 0) {
151 1.1.2.2 pgoyette printf("diskinit abort!\n");
152 1.1.2.2 pgoyette printf("Boot failed! Halting...\n");
153 1.1.2.2 pgoyette reboot();
154 1.1.2.2 pgoyette }
155 1.1.2.2 pgoyette }
156 1.1.2.2 pgoyette
157 1.1.2.2 pgoyette /*
158 1.1.2.2 pgoyette * try to get the drive block size.
159 1.1.2.2 pgoyette */
160 1.1.2.2 pgoyette capbuf[0] = 0;
161 1.1.2.2 pgoyette capbuf[1] = 0;
162 1.1.2.2 pgoyette
163 1.1.2.2 pgoyette stat = scsi_read_capacity((uint8_t *)capbuf, sizeof(capbuf));
164 1.1.2.2 pgoyette
165 1.1.2.2 pgoyette if (stat == 0) {
166 1.1.2.2 pgoyette if (capbuf[1] > DEV_BSIZE)
167 1.1.2.2 pgoyette for (; capbuf[1] > DEV_BSIZE; capbuf[1] >>= 1)
168 1.1.2.2 pgoyette ++sc->sc_blkshift;
169 1.1.2.2 pgoyette }
170 1.1.2.2 pgoyette
171 1.1.2.2 pgoyette sc->sc_alive = 1;
172 1.1.2.2 pgoyette return 1;
173 1.1.2.2 pgoyette }
174 1.1.2.2 pgoyette
175 1.1.2.2 pgoyette int
176 1.1.2.2 pgoyette diskopen(struct open_file *f, ...)
177 1.1.2.2 pgoyette {
178 1.1.2.2 pgoyette struct disk_softc *sc;
179 1.1.2.2 pgoyette struct disklabel *lp;
180 1.1.2.2 pgoyette size_t cnt;
181 1.1.2.2 pgoyette char *msg, buf[DEV_BSIZE];
182 1.1.2.2 pgoyette int error;
183 1.1.2.2 pgoyette
184 1.1.2.2 pgoyette cnt = 0;
185 1.1.2.2 pgoyette
186 1.1.2.2 pgoyette sc = alloc(sizeof(struct disk_softc));
187 1.1.2.2 pgoyette memset(sc, 0, sizeof(struct disk_softc));
188 1.1.2.2 pgoyette f->f_devdata = (void *)sc;
189 1.1.2.2 pgoyette
190 1.1.2.2 pgoyette sc->sc_part = scsi_part;
191 1.1.2.2 pgoyette
192 1.1.2.2 pgoyette if (sc->sc_alive == 0) {
193 1.1.2.2 pgoyette if (diskinit(sc) == 0)
194 1.1.2.2 pgoyette return ENXIO;
195 1.1.2.2 pgoyette }
196 1.1.2.2 pgoyette
197 1.1.2.2 pgoyette /* try to read disk label and partition table information */
198 1.1.2.2 pgoyette lp = &sc->sc_label;
199 1.1.2.2 pgoyette lp->d_secsize = DEV_BSIZE;
200 1.1.2.2 pgoyette lp->d_secpercyl = 1;
201 1.1.2.2 pgoyette lp->d_npartitions = MAXPARTITIONS;
202 1.1.2.2 pgoyette lp->d_partitions[scsi_part].p_offset = 0;
203 1.1.2.2 pgoyette lp->d_partitions[scsi_part].p_size = 0x7fffffff;
204 1.1.2.2 pgoyette
205 1.1.2.2 pgoyette error = diskstrategy(sc, F_READ, (daddr_t)LABELSECTOR, DEV_BSIZE, buf,
206 1.1.2.2 pgoyette &cnt);
207 1.1.2.2 pgoyette
208 1.1.2.2 pgoyette if (error || cnt != DEV_BSIZE) {
209 1.1.2.2 pgoyette printf("diskstrategy error...\n");
210 1.1.2.2 pgoyette dealloc(sc, sizeof(struct disk_softc));
211 1.1.2.2 pgoyette return ENXIO;
212 1.1.2.2 pgoyette }
213 1.1.2.2 pgoyette
214 1.1.2.2 pgoyette msg = getdisklabel(buf, lp);
215 1.1.2.2 pgoyette
216 1.1.2.2 pgoyette if (msg) {
217 1.1.2.2 pgoyette /* If no label, just assume 0 and return */
218 1.1.2.2 pgoyette printf("No disklabel...\n");
219 1.1.2.2 pgoyette reboot();
220 1.1.2.2 pgoyette }
221 1.1.2.2 pgoyette
222 1.1.2.2 pgoyette return 0;
223 1.1.2.2 pgoyette }
224 1.1.2.2 pgoyette
225 1.1.2.2 pgoyette int
226 1.1.2.2 pgoyette diskclose(struct open_file *f)
227 1.1.2.2 pgoyette {
228 1.1.2.2 pgoyette struct disk_softc *sc = f->f_devdata;
229 1.1.2.2 pgoyette
230 1.1.2.2 pgoyette dealloc(sc, sizeof *sc);
231 1.1.2.2 pgoyette f->f_devdata = NULL;
232 1.1.2.2 pgoyette
233 1.1.2.2 pgoyette return 0;
234 1.1.2.2 pgoyette }
235