rd.c revision 1.64 1 1.64 thorpej /* $NetBSD: rd.c,v 1.64 2004/08/28 17:45:24 thorpej Exp $ */
2 1.35 thorpej
3 1.35 thorpej /*-
4 1.35 thorpej * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 1.35 thorpej * All rights reserved.
6 1.35 thorpej *
7 1.35 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.35 thorpej * by Jason R. Thorpe.
9 1.35 thorpej *
10 1.35 thorpej * Redistribution and use in source and binary forms, with or without
11 1.35 thorpej * modification, are permitted provided that the following conditions
12 1.35 thorpej * are met:
13 1.35 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.35 thorpej * notice, this list of conditions and the following disclaimer.
15 1.35 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.35 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.35 thorpej * documentation and/or other materials provided with the distribution.
18 1.35 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.35 thorpej * must display the following acknowledgement:
20 1.35 thorpej * This product includes software developed by the NetBSD
21 1.35 thorpej * Foundation, Inc. and its contributors.
22 1.35 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.35 thorpej * contributors may be used to endorse or promote products derived
24 1.35 thorpej * from this software without specific prior written permission.
25 1.35 thorpej *
26 1.35 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.35 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.35 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.35 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.35 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.35 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.35 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.35 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.35 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.35 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.35 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.35 thorpej */
38 1.10 cgd
39 1.1 cgd /*
40 1.8 mycroft * Copyright (c) 1982, 1990, 1993
41 1.8 mycroft * The Regents of the University of California. All rights reserved.
42 1.1 cgd *
43 1.1 cgd * This code is derived from software contributed to Berkeley by
44 1.1 cgd * the Systems Programming Group of the University of Utah Computer
45 1.1 cgd * Science Department.
46 1.1 cgd *
47 1.1 cgd * Redistribution and use in source and binary forms, with or without
48 1.1 cgd * modification, are permitted provided that the following conditions
49 1.1 cgd * are met:
50 1.1 cgd * 1. Redistributions of source code must retain the above copyright
51 1.1 cgd * notice, this list of conditions and the following disclaimer.
52 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 cgd * notice, this list of conditions and the following disclaimer in the
54 1.1 cgd * documentation and/or other materials provided with the distribution.
55 1.61 agc * 3. Neither the name of the University nor the names of its contributors
56 1.61 agc * may be used to endorse or promote products derived from this software
57 1.61 agc * without specific prior written permission.
58 1.61 agc *
59 1.61 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.61 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.61 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.61 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.61 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.61 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.61 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.61 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.61 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.61 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.61 agc * SUCH DAMAGE.
70 1.61 agc *
71 1.61 agc * from: Utah $Hdr: rd.c 1.44 92/12/26$
72 1.61 agc *
73 1.61 agc * @(#)rd.c 8.2 (Berkeley) 5/19/94
74 1.61 agc */
75 1.61 agc /*
76 1.61 agc * Copyright (c) 1988 University of Utah.
77 1.61 agc *
78 1.61 agc * This code is derived from software contributed to Berkeley by
79 1.61 agc * the Systems Programming Group of the University of Utah Computer
80 1.61 agc * Science Department.
81 1.61 agc *
82 1.61 agc * Redistribution and use in source and binary forms, with or without
83 1.61 agc * modification, are permitted provided that the following conditions
84 1.61 agc * are met:
85 1.61 agc * 1. Redistributions of source code must retain the above copyright
86 1.61 agc * notice, this list of conditions and the following disclaimer.
87 1.61 agc * 2. Redistributions in binary form must reproduce the above copyright
88 1.61 agc * notice, this list of conditions and the following disclaimer in the
89 1.61 agc * documentation and/or other materials provided with the distribution.
90 1.1 cgd * 3. All advertising materials mentioning features or use of this software
91 1.1 cgd * must display the following acknowledgement:
92 1.1 cgd * This product includes software developed by the University of
93 1.1 cgd * California, Berkeley and its contributors.
94 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
95 1.1 cgd * may be used to endorse or promote products derived from this software
96 1.1 cgd * without specific prior written permission.
97 1.1 cgd *
98 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 1.1 cgd * SUCH DAMAGE.
109 1.1 cgd *
110 1.8 mycroft * from: Utah $Hdr: rd.c 1.44 92/12/26$
111 1.8 mycroft *
112 1.10 cgd * @(#)rd.c 8.2 (Berkeley) 5/19/94
113 1.1 cgd */
114 1.1 cgd
115 1.1 cgd /*
116 1.1 cgd * CS80/SS80 disk driver
117 1.1 cgd */
118 1.48 gmcgarry
119 1.48 gmcgarry #include <sys/cdefs.h>
120 1.64 thorpej __KERNEL_RCSID(0, "$NetBSD: rd.c,v 1.64 2004/08/28 17:45:24 thorpej Exp $");
121 1.1 cgd
122 1.44 he #include "opt_useleds.h"
123 1.44 he #include "rnd.h"
124 1.44 he
125 1.5 mycroft #include <sys/param.h>
126 1.5 mycroft #include <sys/systm.h>
127 1.5 mycroft #include <sys/buf.h>
128 1.28 scottr #include <sys/conf.h>
129 1.28 scottr #include <sys/device.h>
130 1.28 scottr #include <sys/disk.h>
131 1.5 mycroft #include <sys/disklabel.h>
132 1.28 scottr #include <sys/fcntl.h>
133 1.8 mycroft #include <sys/ioctl.h>
134 1.28 scottr #include <sys/proc.h>
135 1.28 scottr #include <sys/stat.h>
136 1.27 thorpej
137 1.44 he #if NRND > 0
138 1.44 he #include <sys/rnd.h>
139 1.44 he #endif
140 1.44 he
141 1.27 thorpej #include <hp300/dev/hpibvar.h>
142 1.1 cgd
143 1.5 mycroft #include <hp300/dev/rdreg.h>
144 1.8 mycroft #include <hp300/dev/rdvar.h>
145 1.1 cgd
146 1.31 thorpej #ifdef USELEDS
147 1.31 thorpej #include <hp300/hp300/leds.h>
148 1.31 thorpej #endif
149 1.31 thorpej
150 1.8 mycroft int rderrthresh = RDRETRY-1; /* when to start reporting errors */
151 1.1 cgd
152 1.1 cgd #ifdef DEBUG
153 1.1 cgd /* error message tables */
154 1.63 thorpej static const char *err_reject[] = {
155 1.1 cgd 0, 0,
156 1.1 cgd "channel parity error", /* 0x2000 */
157 1.1 cgd 0, 0,
158 1.1 cgd "illegal opcode", /* 0x0400 */
159 1.1 cgd "module addressing", /* 0x0200 */
160 1.1 cgd "address bounds", /* 0x0100 */
161 1.1 cgd "parameter bounds", /* 0x0080 */
162 1.1 cgd "illegal parameter", /* 0x0040 */
163 1.1 cgd "message sequence", /* 0x0020 */
164 1.1 cgd 0,
165 1.1 cgd "message length", /* 0x0008 */
166 1.1 cgd 0, 0, 0
167 1.1 cgd };
168 1.1 cgd
169 1.63 thorpej static const char *err_fault[] = {
170 1.1 cgd 0,
171 1.1 cgd "cross unit", /* 0x4000 */
172 1.1 cgd 0,
173 1.1 cgd "controller fault", /* 0x1000 */
174 1.1 cgd 0, 0,
175 1.1 cgd "unit fault", /* 0x0200 */
176 1.1 cgd 0,
177 1.1 cgd "diagnostic result", /* 0x0080 */
178 1.1 cgd 0,
179 1.1 cgd "operator release request", /* 0x0020 */
180 1.1 cgd "diagnostic release request", /* 0x0010 */
181 1.1 cgd "internal maintenance release request", /* 0x0008 */
182 1.1 cgd 0,
183 1.1 cgd "power fail", /* 0x0002 */
184 1.1 cgd "retransmit" /* 0x0001 */
185 1.1 cgd };
186 1.1 cgd
187 1.63 thorpej static const char *err_access[] = {
188 1.1 cgd "illegal parallel operation", /* 0x8000 */
189 1.1 cgd "uninitialized media", /* 0x4000 */
190 1.1 cgd "no spares available", /* 0x2000 */
191 1.1 cgd "not ready", /* 0x1000 */
192 1.1 cgd "write protect", /* 0x0800 */
193 1.1 cgd "no data found", /* 0x0400 */
194 1.1 cgd 0, 0,
195 1.1 cgd "unrecoverable data overflow", /* 0x0080 */
196 1.1 cgd "unrecoverable data", /* 0x0040 */
197 1.1 cgd 0,
198 1.1 cgd "end of file", /* 0x0010 */
199 1.1 cgd "end of volume", /* 0x0008 */
200 1.1 cgd 0, 0, 0
201 1.1 cgd };
202 1.1 cgd
203 1.63 thorpej static const char *err_info[] = {
204 1.1 cgd "operator release request", /* 0x8000 */
205 1.1 cgd "diagnostic release request", /* 0x4000 */
206 1.1 cgd "internal maintenance release request", /* 0x2000 */
207 1.1 cgd "media wear", /* 0x1000 */
208 1.1 cgd "latency induced", /* 0x0800 */
209 1.1 cgd 0, 0,
210 1.1 cgd "auto sparing invoked", /* 0x0100 */
211 1.1 cgd 0,
212 1.1 cgd "recoverable data overflow", /* 0x0040 */
213 1.1 cgd "marginal data", /* 0x0020 */
214 1.1 cgd "recoverable data", /* 0x0010 */
215 1.1 cgd 0,
216 1.1 cgd "maintenance track overflow", /* 0x0004 */
217 1.1 cgd 0, 0
218 1.1 cgd };
219 1.8 mycroft
220 1.8 mycroft int rddebug = 0x80;
221 1.8 mycroft #define RDB_FOLLOW 0x01
222 1.8 mycroft #define RDB_STATUS 0x02
223 1.8 mycroft #define RDB_IDENT 0x04
224 1.8 mycroft #define RDB_IO 0x08
225 1.8 mycroft #define RDB_ASYNC 0x10
226 1.8 mycroft #define RDB_ERROR 0x80
227 1.1 cgd #endif
228 1.1 cgd
229 1.1 cgd /*
230 1.8 mycroft * Misc. HW description, indexed by sc_type.
231 1.8 mycroft * Nothing really critical here, could do without it.
232 1.1 cgd */
233 1.64 thorpej static const struct rdidentinfo rdidentinfo[] = {
234 1.14 thorpej { RD7946AID, 0, "7945A", NRD7945ABPT,
235 1.14 thorpej NRD7945ATRK, 968, 108416 },
236 1.14 thorpej
237 1.14 thorpej { RD9134DID, 1, "9134D", NRD9134DBPT,
238 1.14 thorpej NRD9134DTRK, 303, 29088 },
239 1.14 thorpej
240 1.14 thorpej { RD9134LID, 1, "9122S", NRD9122SBPT,
241 1.14 thorpej NRD9122STRK, 77, 1232 },
242 1.14 thorpej
243 1.14 thorpej { RD7912PID, 0, "7912P", NRD7912PBPT,
244 1.14 thorpej NRD7912PTRK, 572, 128128 },
245 1.14 thorpej
246 1.14 thorpej { RD7914PID, 0, "7914P", NRD7914PBPT,
247 1.14 thorpej NRD7914PTRK, 1152, 258048 },
248 1.14 thorpej
249 1.14 thorpej { RD7958AID, 0, "7958A", NRD7958ABPT,
250 1.14 thorpej NRD7958ATRK, 1013, 255276 },
251 1.14 thorpej
252 1.14 thorpej { RD7957AID, 0, "7957A", NRD7957ABPT,
253 1.14 thorpej NRD7957ATRK, 1036, 159544 },
254 1.14 thorpej
255 1.14 thorpej { RD7933HID, 0, "7933H", NRD7933HBPT,
256 1.14 thorpej NRD7933HTRK, 1321, 789958 },
257 1.14 thorpej
258 1.14 thorpej { RD9134LID, 1, "9134L", NRD9134LBPT,
259 1.14 thorpej NRD9134LTRK, 973, 77840 },
260 1.14 thorpej
261 1.14 thorpej { RD7936HID, 0, "7936H", NRD7936HBPT,
262 1.14 thorpej NRD7936HTRK, 698, 600978 },
263 1.14 thorpej
264 1.14 thorpej { RD7937HID, 0, "7937H", NRD7937HBPT,
265 1.14 thorpej NRD7937HTRK, 698, 1116102 },
266 1.14 thorpej
267 1.14 thorpej { RD7914CTID, 0, "7914CT", NRD7914PBPT,
268 1.14 thorpej NRD7914PTRK, 1152, 258048 },
269 1.14 thorpej
270 1.14 thorpej { RD7946AID, 0, "7946A", NRD7945ABPT,
271 1.14 thorpej NRD7945ATRK, 968, 108416 },
272 1.14 thorpej
273 1.14 thorpej { RD9134LID, 1, "9122D", NRD9122SBPT,
274 1.14 thorpej NRD9122STRK, 77, 1232 },
275 1.14 thorpej
276 1.14 thorpej { RD7957BID, 0, "7957B", NRD7957BBPT,
277 1.14 thorpej NRD7957BTRK, 1269, 159894 },
278 1.14 thorpej
279 1.14 thorpej { RD7958BID, 0, "7958B", NRD7958BBPT,
280 1.14 thorpej NRD7958BTRK, 786, 297108 },
281 1.14 thorpej
282 1.14 thorpej { RD7959BID, 0, "7959B", NRD7959BBPT,
283 1.14 thorpej NRD7959BTRK, 1572, 594216 },
284 1.14 thorpej
285 1.14 thorpej { RD2200AID, 0, "2200A", NRD2200ABPT,
286 1.14 thorpej NRD2200ATRK, 1449, 654948 },
287 1.14 thorpej
288 1.14 thorpej { RD2203AID, 0, "2203A", NRD2203ABPT,
289 1.14 thorpej NRD2203ATRK, 1449, 1309896 }
290 1.1 cgd };
291 1.63 thorpej static const int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
292 1.1 cgd
293 1.63 thorpej static int rdident(struct device *, struct rd_softc *,
294 1.63 thorpej struct hpibbus_attach_args *);
295 1.63 thorpej static void rdreset(struct rd_softc *);
296 1.63 thorpej static void rdustart(struct rd_softc *);
297 1.63 thorpej static int rdgetinfo(dev_t);
298 1.63 thorpej static void rdrestart(void *);
299 1.63 thorpej static struct buf *rdfinish(struct rd_softc *, struct buf *);
300 1.63 thorpej
301 1.63 thorpej static void rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
302 1.63 thorpej static void rdrestart(void *);
303 1.63 thorpej static void rdustart(struct rd_softc *);
304 1.63 thorpej static struct buf *rdfinish(struct rd_softc *, struct buf *);
305 1.63 thorpej static void rdstart(void *);
306 1.63 thorpej static void rdgo(void *);
307 1.63 thorpej static void rdintr(void *);
308 1.63 thorpej static int rdstatus(struct rd_softc *);
309 1.63 thorpej static int rderror(int);
310 1.29 scottr #ifdef DEBUG
311 1.63 thorpej static void rdprinterr(char *, short, char **);
312 1.29 scottr #endif
313 1.29 scottr
314 1.63 thorpej static int rdmatch(struct device *, struct cfdata *, void *);
315 1.63 thorpej static void rdattach(struct device *, struct device *, void *);
316 1.29 scottr
317 1.53 thorpej CFATTACH_DECL(rd, sizeof(struct rd_softc),
318 1.53 thorpej rdmatch, rdattach, NULL, NULL);
319 1.29 scottr
320 1.36 thorpej extern struct cfdriver rd_cd;
321 1.51 gehenna
322 1.63 thorpej static dev_type_open(rdopen);
323 1.63 thorpej static dev_type_close(rdclose);
324 1.63 thorpej static dev_type_read(rdread);
325 1.63 thorpej static dev_type_write(rdwrite);
326 1.63 thorpej static dev_type_ioctl(rdioctl);
327 1.63 thorpej static dev_type_strategy(rdstrategy);
328 1.63 thorpej static dev_type_dump(rddump);
329 1.63 thorpej static dev_type_size(rdsize);
330 1.51 gehenna
331 1.51 gehenna const struct bdevsw rd_bdevsw = {
332 1.51 gehenna rdopen, rdclose, rdstrategy, rdioctl, rddump, rdsize, D_DISK
333 1.51 gehenna };
334 1.51 gehenna
335 1.51 gehenna const struct cdevsw rd_cdevsw = {
336 1.51 gehenna rdopen, rdclose, rdread, rdwrite, rdioctl,
337 1.54 jdolecek nostop, notty, nopoll, nommap, nokqfilter, D_DISK
338 1.51 gehenna };
339 1.29 scottr
340 1.63 thorpej static int
341 1.63 thorpej rdmatch(struct device *parent, struct cfdata *match, void *aux)
342 1.1 cgd {
343 1.27 thorpej struct hpibbus_attach_args *ha = aux;
344 1.1 cgd
345 1.27 thorpej /*
346 1.27 thorpej * Set punit if operator specified one in the kernel
347 1.27 thorpej * configuration file.
348 1.27 thorpej */
349 1.34 kleink if (match->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
350 1.27 thorpej match->hpibbuscf_punit < HPIB_NPUNITS)
351 1.27 thorpej ha->ha_punit = match->hpibbuscf_punit;
352 1.27 thorpej
353 1.27 thorpej if (rdident(parent, NULL, ha) == 0) {
354 1.18 thorpej /*
355 1.27 thorpej * XXX Some aging HP-IB drives are slow to
356 1.27 thorpej * XXX respond; give them a chance to catch
357 1.27 thorpej * XXX up and probe them again.
358 1.18 thorpej */
359 1.27 thorpej delay(10000);
360 1.27 thorpej ha->ha_id = hpibid(parent->dv_unit, ha->ha_slave);
361 1.27 thorpej return (rdident(parent, NULL, ha));
362 1.18 thorpej }
363 1.27 thorpej return (1);
364 1.27 thorpej }
365 1.17 thorpej
366 1.63 thorpej static void
367 1.63 thorpej rdattach(struct device *parent, struct device *self, void *aux)
368 1.27 thorpej {
369 1.27 thorpej struct rd_softc *sc = (struct rd_softc *)self;
370 1.27 thorpej struct hpibbus_attach_args *ha = aux;
371 1.27 thorpej
372 1.50 hannken bufq_alloc(&sc->sc_tab, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
373 1.37 thorpej
374 1.27 thorpej if (rdident(parent, sc, ha) == 0) {
375 1.27 thorpej printf("\n%s: didn't respond to describe command!\n",
376 1.27 thorpej sc->sc_dev.dv_xname);
377 1.27 thorpej return;
378 1.27 thorpej }
379 1.17 thorpej
380 1.17 thorpej /*
381 1.17 thorpej * Initialize and attach the disk structure.
382 1.17 thorpej */
383 1.46 gmcgarry memset(&sc->sc_dkdev, 0, sizeof(sc->sc_dkdev));
384 1.27 thorpej sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
385 1.27 thorpej disk_attach(&sc->sc_dkdev);
386 1.27 thorpej
387 1.27 thorpej sc->sc_slave = ha->ha_slave;
388 1.27 thorpej sc->sc_punit = ha->ha_punit;
389 1.27 thorpej
390 1.41 thorpej callout_init(&sc->sc_restart_ch);
391 1.41 thorpej
392 1.27 thorpej /* Initialize the hpib job queue entry */
393 1.27 thorpej sc->sc_hq.hq_softc = sc;
394 1.27 thorpej sc->sc_hq.hq_slave = sc->sc_slave;
395 1.27 thorpej sc->sc_hq.hq_start = rdstart;
396 1.27 thorpej sc->sc_hq.hq_go = rdgo;
397 1.27 thorpej sc->sc_hq.hq_intr = rdintr;
398 1.15 thorpej
399 1.27 thorpej sc->sc_flags = RDF_ALIVE;
400 1.1 cgd #ifdef DEBUG
401 1.1 cgd /* always report errors */
402 1.1 cgd if (rddebug & RDB_ERROR)
403 1.1 cgd rderrthresh = 0;
404 1.1 cgd #endif
405 1.44 he #if NRND > 0
406 1.44 he /*
407 1.44 he * attach the device into the random source list
408 1.44 he */
409 1.44 he rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
410 1.44 he RND_TYPE_DISK, 0);
411 1.44 he #endif
412 1.1 cgd }
413 1.1 cgd
414 1.63 thorpej static int
415 1.63 thorpej rdident(struct device *parent, struct rd_softc *sc,
416 1.63 thorpej struct hpibbus_attach_args *ha)
417 1.1 cgd {
418 1.27 thorpej struct rd_describe *desc = sc != NULL ? &sc->sc_rddesc : NULL;
419 1.1 cgd u_char stat, cmd[3];
420 1.1 cgd char name[7];
421 1.27 thorpej int i, id, n, ctlr, slave;
422 1.27 thorpej
423 1.27 thorpej ctlr = parent->dv_unit;
424 1.27 thorpej slave = ha->ha_slave;
425 1.1 cgd
426 1.27 thorpej /* Verify that we have a CS80 device. */
427 1.27 thorpej if ((ha->ha_id & 0x200) == 0)
428 1.27 thorpej return (0);
429 1.27 thorpej
430 1.27 thorpej /* Is it one of the disks we support? */
431 1.27 thorpej for (id = 0; id < numrdidentinfo; id++)
432 1.27 thorpej if (ha->ha_id == rdidentinfo[id].ri_hwid)
433 1.27 thorpej break;
434 1.27 thorpej if (id == numrdidentinfo || ha->ha_punit > rdidentinfo[id].ri_maxunum)
435 1.27 thorpej return (0);
436 1.1 cgd
437 1.1 cgd /*
438 1.27 thorpej * If we're just probing for the device, that's all the
439 1.27 thorpej * work we need to do.
440 1.1 cgd */
441 1.27 thorpej if (sc == NULL)
442 1.27 thorpej return (1);
443 1.1 cgd
444 1.1 cgd /*
445 1.27 thorpej * Reset device and collect description
446 1.1 cgd */
447 1.27 thorpej rdreset(sc);
448 1.27 thorpej cmd[0] = C_SUNIT(ha->ha_punit);
449 1.1 cgd cmd[1] = C_SVOL(0);
450 1.1 cgd cmd[2] = C_DESC;
451 1.1 cgd hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
452 1.14 thorpej hpibrecv(ctlr, slave, C_EXEC, desc, 37);
453 1.1 cgd hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
454 1.62 tsutsui memset(name, 0, sizeof(name));
455 1.27 thorpej if (stat == 0) {
456 1.27 thorpej n = desc->d_name;
457 1.1 cgd for (i = 5; i >= 0; i--) {
458 1.1 cgd name[i] = (n & 0xf) + '0';
459 1.1 cgd n >>= 4;
460 1.1 cgd }
461 1.1 cgd }
462 1.27 thorpej
463 1.1 cgd #ifdef DEBUG
464 1.1 cgd if (rddebug & RDB_IDENT) {
465 1.27 thorpej printf("\n%s: name: %x ('%s')\n",
466 1.27 thorpej sc->sc_dev.dv_xname, desc->d_name, name);
467 1.24 christos printf(" iuw %x, maxxfr %d, ctype %d\n",
468 1.27 thorpej desc->d_iuw, desc->d_cmaxxfr, desc->d_ctype);
469 1.24 christos printf(" utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
470 1.27 thorpej desc->d_utype, desc->d_sectsize,
471 1.27 thorpej desc->d_blkbuf, desc->d_burstsize, desc->d_blocktime);
472 1.24 christos printf(" avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
473 1.27 thorpej desc->d_uavexfr, desc->d_retry, desc->d_access,
474 1.27 thorpej desc->d_maxint, desc->d_fvbyte, desc->d_rvbyte);
475 1.24 christos printf(" maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
476 1.27 thorpej desc->d_maxcyl, desc->d_maxhead, desc->d_maxsect,
477 1.27 thorpej desc->d_maxvsectl, desc->d_interleave);
478 1.27 thorpej printf("%s", sc->sc_dev.dv_xname);
479 1.1 cgd }
480 1.1 cgd #endif
481 1.27 thorpej
482 1.1 cgd /*
483 1.1 cgd * Take care of a couple of anomolies:
484 1.1 cgd * 1. 7945A and 7946A both return same HW id
485 1.1 cgd * 2. 9122S and 9134D both return same HW id
486 1.1 cgd * 3. 9122D and 9134L both return same HW id
487 1.1 cgd */
488 1.27 thorpej switch (ha->ha_id) {
489 1.1 cgd case RD7946AID:
490 1.46 gmcgarry if (memcmp(name, "079450", 6) == 0)
491 1.1 cgd id = RD7945A;
492 1.1 cgd else
493 1.1 cgd id = RD7946A;
494 1.1 cgd break;
495 1.1 cgd
496 1.1 cgd case RD9134LID:
497 1.46 gmcgarry if (memcmp(name, "091340", 6) == 0)
498 1.1 cgd id = RD9134L;
499 1.1 cgd else
500 1.1 cgd id = RD9122D;
501 1.1 cgd break;
502 1.1 cgd
503 1.1 cgd case RD9134DID:
504 1.46 gmcgarry if (memcmp(name, "091220", 6) == 0)
505 1.1 cgd id = RD9122S;
506 1.1 cgd else
507 1.1 cgd id = RD9134D;
508 1.1 cgd break;
509 1.1 cgd }
510 1.27 thorpej
511 1.27 thorpej sc->sc_type = id;
512 1.27 thorpej
513 1.14 thorpej /*
514 1.14 thorpej * XXX We use DEV_BSIZE instead of the sector size value pulled
515 1.27 thorpej * XXX off the driver because all of this code assumes 512 byte
516 1.27 thorpej * XXX blocks. ICK!
517 1.14 thorpej */
518 1.27 thorpej printf(": %s\n", rdidentinfo[id].ri_desc);
519 1.27 thorpej printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
520 1.27 thorpej sc->sc_dev.dv_xname, rdidentinfo[id].ri_ncyl,
521 1.27 thorpej rdidentinfo[id].ri_ntpc, rdidentinfo[id].ri_nblocks,
522 1.27 thorpej DEV_BSIZE);
523 1.27 thorpej
524 1.27 thorpej return (1);
525 1.1 cgd }
526 1.1 cgd
527 1.63 thorpej static void
528 1.63 thorpej rdreset(struct rd_softc *rs)
529 1.1 cgd {
530 1.27 thorpej int ctlr = rs->sc_dev.dv_parent->dv_unit;
531 1.27 thorpej int slave = rs->sc_slave;
532 1.1 cgd u_char stat;
533 1.1 cgd
534 1.1 cgd rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
535 1.1 cgd rs->sc_clear.c_cmd = C_CLEAR;
536 1.27 thorpej hpibsend(ctlr, slave, C_TCMD, &rs->sc_clear, sizeof(rs->sc_clear));
537 1.27 thorpej hpibswait(ctlr, slave);
538 1.27 thorpej hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
539 1.27 thorpej
540 1.1 cgd rs->sc_src.c_unit = C_SUNIT(RDCTLR);
541 1.1 cgd rs->sc_src.c_nop = C_NOP;
542 1.1 cgd rs->sc_src.c_cmd = C_SREL;
543 1.1 cgd rs->sc_src.c_param = C_REL;
544 1.27 thorpej hpibsend(ctlr, slave, C_CMD, &rs->sc_src, sizeof(rs->sc_src));
545 1.27 thorpej hpibswait(ctlr, slave);
546 1.27 thorpej hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
547 1.27 thorpej
548 1.1 cgd rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
549 1.1 cgd rs->sc_ssmc.c_cmd = C_SSM;
550 1.1 cgd rs->sc_ssmc.c_refm = REF_MASK;
551 1.1 cgd rs->sc_ssmc.c_fefm = FEF_MASK;
552 1.1 cgd rs->sc_ssmc.c_aefm = AEF_MASK;
553 1.1 cgd rs->sc_ssmc.c_iefm = IEF_MASK;
554 1.27 thorpej hpibsend(ctlr, slave, C_CMD, &rs->sc_ssmc, sizeof(rs->sc_ssmc));
555 1.27 thorpej hpibswait(ctlr, slave);
556 1.27 thorpej hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
557 1.1 cgd #ifdef DEBUG
558 1.27 thorpej rs->sc_stats.rdresets++;
559 1.1 cgd #endif
560 1.1 cgd }
561 1.1 cgd
562 1.8 mycroft /*
563 1.8 mycroft * Read or constuct a disklabel
564 1.8 mycroft */
565 1.63 thorpej static int
566 1.63 thorpej rdgetinfo(dev_t dev)
567 1.8 mycroft {
568 1.8 mycroft int unit = rdunit(dev);
569 1.27 thorpej struct rd_softc *rs = rd_cd.cd_devs[unit];
570 1.28 scottr struct disklabel *lp = rs->sc_dkdev.dk_label;
571 1.28 scottr struct partition *pi;
572 1.57 dsl const char *msg;
573 1.8 mycroft
574 1.8 mycroft /*
575 1.8 mycroft * Set some default values to use while reading the label
576 1.8 mycroft * or to use if there isn't a label.
577 1.8 mycroft */
578 1.46 gmcgarry memset((caddr_t)lp, 0, sizeof *lp);
579 1.49 gmcgarry rdgetdefaultlabel(rs, lp);
580 1.8 mycroft
581 1.8 mycroft /*
582 1.8 mycroft * Now try to read the disklabel
583 1.8 mycroft */
584 1.16 thorpej msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
585 1.8 mycroft if (msg == NULL)
586 1.28 scottr return (0);
587 1.8 mycroft
588 1.8 mycroft pi = lp->d_partitions;
589 1.64 thorpej printf("%s: WARNING: %s\n", rs->sc_dev.dv_xname, msg);
590 1.64 thorpej
591 1.8 mycroft pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
592 1.9 mycroft /* XXX reset other info since readdisklabel screws with it */
593 1.9 mycroft lp->d_npartitions = 3;
594 1.9 mycroft pi[0].p_size = 0;
595 1.64 thorpej
596 1.8 mycroft return(0);
597 1.8 mycroft }
598 1.8 mycroft
599 1.63 thorpej static int
600 1.63 thorpej rdopen(dev_t dev, int flags, int mode, struct proc *p)
601 1.1 cgd {
602 1.28 scottr int unit = rdunit(dev);
603 1.28 scottr struct rd_softc *rs;
604 1.26 thorpej int error, mask, part;
605 1.1 cgd
606 1.27 thorpej if (unit >= rd_cd.cd_ndevs ||
607 1.27 thorpej (rs = rd_cd.cd_devs[unit]) == NULL ||
608 1.27 thorpej (rs->sc_flags & RDF_ALIVE) == 0)
609 1.27 thorpej return (ENXIO);
610 1.8 mycroft
611 1.8 mycroft /*
612 1.8 mycroft * Wait for any pending opens/closes to complete
613 1.8 mycroft */
614 1.8 mycroft while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
615 1.43 thorpej (void) tsleep(rs, PRIBIO, "rdopen", 0);
616 1.8 mycroft
617 1.8 mycroft /*
618 1.8 mycroft * On first open, get label and partition info.
619 1.8 mycroft * We may block reading the label, so be careful
620 1.8 mycroft * to stop any other opens.
621 1.8 mycroft */
622 1.17 thorpej if (rs->sc_dkdev.dk_openmask == 0) {
623 1.8 mycroft rs->sc_flags |= RDF_OPENING;
624 1.8 mycroft error = rdgetinfo(dev);
625 1.8 mycroft rs->sc_flags &= ~RDF_OPENING;
626 1.8 mycroft wakeup((caddr_t)rs);
627 1.8 mycroft if (error)
628 1.8 mycroft return(error);
629 1.8 mycroft }
630 1.8 mycroft
631 1.26 thorpej part = rdpart(dev);
632 1.26 thorpej mask = 1 << part;
633 1.26 thorpej
634 1.26 thorpej /* Check that the partition exists. */
635 1.26 thorpej if (part != RAW_PART &&
636 1.26 thorpej (part > rs->sc_dkdev.dk_label->d_npartitions ||
637 1.26 thorpej rs->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
638 1.26 thorpej return (ENXIO);
639 1.26 thorpej
640 1.26 thorpej /* Ensure only one open at a time. */
641 1.26 thorpej switch (mode) {
642 1.26 thorpej case S_IFCHR:
643 1.17 thorpej rs->sc_dkdev.dk_copenmask |= mask;
644 1.26 thorpej break;
645 1.26 thorpej case S_IFBLK:
646 1.17 thorpej rs->sc_dkdev.dk_bopenmask |= mask;
647 1.26 thorpej break;
648 1.26 thorpej }
649 1.26 thorpej rs->sc_dkdev.dk_openmask =
650 1.26 thorpej rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
651 1.26 thorpej
652 1.8 mycroft return(0);
653 1.8 mycroft }
654 1.8 mycroft
655 1.63 thorpej static int
656 1.63 thorpej rdclose(dev_t dev, int flag, int mode, struct proc *p)
657 1.8 mycroft {
658 1.8 mycroft int unit = rdunit(dev);
659 1.27 thorpej struct rd_softc *rs = rd_cd.cd_devs[unit];
660 1.28 scottr struct disk *dk = &rs->sc_dkdev;
661 1.8 mycroft int mask, s;
662 1.8 mycroft
663 1.8 mycroft mask = 1 << rdpart(dev);
664 1.8 mycroft if (mode == S_IFCHR)
665 1.17 thorpej dk->dk_copenmask &= ~mask;
666 1.8 mycroft else
667 1.17 thorpej dk->dk_bopenmask &= ~mask;
668 1.17 thorpej dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
669 1.8 mycroft /*
670 1.8 mycroft * On last close, we wait for all activity to cease since
671 1.8 mycroft * the label/parition info will become invalid. Since we
672 1.8 mycroft * might sleep, we must block any opens while we are here.
673 1.8 mycroft * Note we don't have to about other closes since we know
674 1.8 mycroft * we are the last one.
675 1.8 mycroft */
676 1.17 thorpej if (dk->dk_openmask == 0) {
677 1.8 mycroft rs->sc_flags |= RDF_CLOSING;
678 1.8 mycroft s = splbio();
679 1.37 thorpej while (rs->sc_active) {
680 1.8 mycroft rs->sc_flags |= RDF_WANTED;
681 1.43 thorpej (void) tsleep(&rs->sc_tab, PRIBIO, "rdclose", 0);
682 1.8 mycroft }
683 1.8 mycroft splx(s);
684 1.8 mycroft rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
685 1.8 mycroft wakeup((caddr_t)rs);
686 1.8 mycroft }
687 1.1 cgd return(0);
688 1.1 cgd }
689 1.1 cgd
690 1.63 thorpej static void
691 1.63 thorpej rdstrategy(struct buf *bp)
692 1.1 cgd {
693 1.8 mycroft int unit = rdunit(bp->b_dev);
694 1.27 thorpej struct rd_softc *rs = rd_cd.cd_devs[unit];
695 1.28 scottr struct partition *pinfo;
696 1.28 scottr daddr_t bn;
697 1.28 scottr int sz, s;
698 1.26 thorpej int offset;
699 1.1 cgd
700 1.1 cgd #ifdef DEBUG
701 1.1 cgd if (rddebug & RDB_FOLLOW)
702 1.29 scottr printf("rdstrategy(%p): dev %x, bn %x, bcount %lx, %c\n",
703 1.1 cgd bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
704 1.1 cgd (bp->b_flags & B_READ) ? 'R' : 'W');
705 1.1 cgd #endif
706 1.1 cgd bn = bp->b_blkno;
707 1.1 cgd sz = howmany(bp->b_bcount, DEV_BSIZE);
708 1.17 thorpej pinfo = &rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
709 1.26 thorpej
710 1.26 thorpej /* Don't perform partition translation on RAW_PART. */
711 1.26 thorpej offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
712 1.26 thorpej
713 1.26 thorpej if (rdpart(bp->b_dev) != RAW_PART) {
714 1.26 thorpej /*
715 1.26 thorpej * XXX This block of code belongs in
716 1.26 thorpej * XXX bounds_check_with_label()
717 1.26 thorpej */
718 1.26 thorpej
719 1.26 thorpej if (bn < 0 || bn + sz > pinfo->p_size) {
720 1.26 thorpej sz = pinfo->p_size - bn;
721 1.26 thorpej if (sz == 0) {
722 1.26 thorpej bp->b_resid = bp->b_bcount;
723 1.26 thorpej goto done;
724 1.26 thorpej }
725 1.26 thorpej if (sz < 0) {
726 1.26 thorpej bp->b_error = EINVAL;
727 1.26 thorpej goto bad;
728 1.26 thorpej }
729 1.26 thorpej bp->b_bcount = dbtob(sz);
730 1.1 cgd }
731 1.26 thorpej /*
732 1.26 thorpej * Check for write to write protected label
733 1.26 thorpej */
734 1.26 thorpej if (bn + offset <= LABELSECTOR &&
735 1.26 thorpej #if LABELSECTOR != 0
736 1.26 thorpej bn + offset + sz > LABELSECTOR &&
737 1.26 thorpej #endif
738 1.26 thorpej !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
739 1.26 thorpej bp->b_error = EROFS;
740 1.8 mycroft goto bad;
741 1.1 cgd }
742 1.8 mycroft }
743 1.39 thorpej bp->b_rawblkno = bn + offset;
744 1.1 cgd s = splbio();
745 1.50 hannken BUFQ_PUT(&rs->sc_tab, bp);
746 1.37 thorpej if (rs->sc_active == 0) {
747 1.37 thorpej rs->sc_active = 1;
748 1.27 thorpej rdustart(rs);
749 1.1 cgd }
750 1.1 cgd splx(s);
751 1.1 cgd return;
752 1.8 mycroft bad:
753 1.8 mycroft bp->b_flags |= B_ERROR;
754 1.1 cgd done:
755 1.1 cgd biodone(bp);
756 1.1 cgd }
757 1.1 cgd
758 1.1 cgd /*
759 1.1 cgd * Called from timeout() when handling maintenance releases
760 1.1 cgd */
761 1.63 thorpej static void
762 1.63 thorpej rdrestart(void *arg)
763 1.1 cgd {
764 1.1 cgd int s = splbio();
765 1.27 thorpej rdustart((struct rd_softc *)arg);
766 1.1 cgd splx(s);
767 1.1 cgd }
768 1.1 cgd
769 1.63 thorpej static void
770 1.63 thorpej rdustart(struct rd_softc *rs)
771 1.1 cgd {
772 1.28 scottr struct buf *bp;
773 1.1 cgd
774 1.50 hannken bp = BUFQ_PEEK(&rs->sc_tab);
775 1.42 thorpej rs->sc_addr = bp->b_data;
776 1.1 cgd rs->sc_resid = bp->b_bcount;
777 1.27 thorpej if (hpibreq(rs->sc_dev.dv_parent, &rs->sc_hq))
778 1.27 thorpej rdstart(rs);
779 1.1 cgd }
780 1.1 cgd
781 1.63 thorpej static struct buf *
782 1.63 thorpej rdfinish(struct rd_softc *rs, struct buf *bp)
783 1.8 mycroft {
784 1.8 mycroft
785 1.37 thorpej rs->sc_errcnt = 0;
786 1.50 hannken (void)BUFQ_GET(&rs->sc_tab);
787 1.8 mycroft bp->b_resid = 0;
788 1.8 mycroft biodone(bp);
789 1.27 thorpej hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
790 1.50 hannken if ((bp = BUFQ_PEEK(&rs->sc_tab)) != NULL)
791 1.37 thorpej return (bp);
792 1.37 thorpej rs->sc_active = 0;
793 1.8 mycroft if (rs->sc_flags & RDF_WANTED) {
794 1.8 mycroft rs->sc_flags &= ~RDF_WANTED;
795 1.38 kleink wakeup((caddr_t)&rs->sc_tab);
796 1.8 mycroft }
797 1.27 thorpej return (NULL);
798 1.8 mycroft }
799 1.8 mycroft
800 1.63 thorpej static void
801 1.63 thorpej rdstart(void *arg)
802 1.1 cgd {
803 1.27 thorpej struct rd_softc *rs = arg;
804 1.50 hannken struct buf *bp = BUFQ_PEEK(&rs->sc_tab);
805 1.28 scottr int part, ctlr, slave;
806 1.27 thorpej
807 1.27 thorpej ctlr = rs->sc_dev.dv_parent->dv_unit;
808 1.27 thorpej slave = rs->sc_slave;
809 1.1 cgd
810 1.1 cgd again:
811 1.1 cgd #ifdef DEBUG
812 1.1 cgd if (rddebug & RDB_FOLLOW)
813 1.29 scottr printf("rdstart(%s): bp %p, %c\n", rs->sc_dev.dv_xname, bp,
814 1.1 cgd (bp->b_flags & B_READ) ? 'R' : 'W');
815 1.1 cgd #endif
816 1.1 cgd part = rdpart(bp->b_dev);
817 1.1 cgd rs->sc_flags |= RDF_SEEK;
818 1.1 cgd rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
819 1.1 cgd rs->sc_ioc.c_volume = C_SVOL(0);
820 1.1 cgd rs->sc_ioc.c_saddr = C_SADDR;
821 1.1 cgd rs->sc_ioc.c_hiaddr = 0;
822 1.39 thorpej rs->sc_ioc.c_addr = RDBTOS(bp->b_rawblkno);
823 1.1 cgd rs->sc_ioc.c_nop2 = C_NOP;
824 1.1 cgd rs->sc_ioc.c_slen = C_SLEN;
825 1.1 cgd rs->sc_ioc.c_len = rs->sc_resid;
826 1.1 cgd rs->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
827 1.1 cgd #ifdef DEBUG
828 1.1 cgd if (rddebug & RDB_IO)
829 1.29 scottr printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
830 1.27 thorpej ctlr, slave, C_CMD,
831 1.1 cgd &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
832 1.1 cgd #endif
833 1.27 thorpej if (hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
834 1.1 cgd sizeof(rs->sc_ioc)-2) == sizeof(rs->sc_ioc)-2) {
835 1.17 thorpej
836 1.17 thorpej /* Instrumentation. */
837 1.17 thorpej disk_busy(&rs->sc_dkdev);
838 1.17 thorpej rs->sc_dkdev.dk_seek++;
839 1.17 thorpej
840 1.1 cgd #ifdef DEBUG
841 1.1 cgd if (rddebug & RDB_IO)
842 1.27 thorpej printf("rdstart: hpibawait(%x)\n", ctlr);
843 1.1 cgd #endif
844 1.27 thorpej hpibawait(ctlr);
845 1.1 cgd return;
846 1.1 cgd }
847 1.1 cgd /*
848 1.1 cgd * Experience has shown that the hpibwait in this hpibsend will
849 1.1 cgd * occasionally timeout. It appears to occur mostly on old 7914
850 1.1 cgd * drives with full maintenance tracks. We should probably
851 1.1 cgd * integrate this with the backoff code in rderror.
852 1.1 cgd */
853 1.1 cgd #ifdef DEBUG
854 1.1 cgd if (rddebug & RDB_ERROR)
855 1.40 kleink printf("%s: rdstart: cmd %x adr %lx blk %d len %d ecnt %d\n",
856 1.27 thorpej rs->sc_dev.dv_xname, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
857 1.37 thorpej bp->b_blkno, rs->sc_resid, rs->sc_errcnt);
858 1.27 thorpej rs->sc_stats.rdretries++;
859 1.1 cgd #endif
860 1.1 cgd rs->sc_flags &= ~RDF_SEEK;
861 1.27 thorpej rdreset(rs);
862 1.37 thorpej if (rs->sc_errcnt++ < RDRETRY)
863 1.1 cgd goto again;
864 1.56 gmcgarry printf("%s: rdstart err: cmd 0x%x sect %ld blk %" PRId64 " len %d\n",
865 1.27 thorpej rs->sc_dev.dv_xname, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
866 1.1 cgd bp->b_blkno, rs->sc_resid);
867 1.1 cgd bp->b_flags |= B_ERROR;
868 1.1 cgd bp->b_error = EIO;
869 1.27 thorpej bp = rdfinish(rs, bp);
870 1.8 mycroft if (bp) {
871 1.42 thorpej rs->sc_addr = bp->b_data;
872 1.8 mycroft rs->sc_resid = bp->b_bcount;
873 1.27 thorpej if (hpibreq(rs->sc_dev.dv_parent, &rs->sc_hq))
874 1.8 mycroft goto again;
875 1.1 cgd }
876 1.1 cgd }
877 1.1 cgd
878 1.63 thorpej static void
879 1.63 thorpej rdgo(void *arg)
880 1.1 cgd {
881 1.27 thorpej struct rd_softc *rs = arg;
882 1.50 hannken struct buf *bp = BUFQ_PEEK(&rs->sc_tab);
883 1.27 thorpej int rw, ctlr, slave;
884 1.27 thorpej
885 1.27 thorpej ctlr = rs->sc_dev.dv_parent->dv_unit;
886 1.27 thorpej slave = rs->sc_slave;
887 1.13 thorpej
888 1.13 thorpej rw = bp->b_flags & B_READ;
889 1.1 cgd
890 1.17 thorpej /* Instrumentation. */
891 1.17 thorpej disk_busy(&rs->sc_dkdev);
892 1.17 thorpej
893 1.8 mycroft #ifdef USELEDS
894 1.31 thorpej ledcontrol(0, 0, LED_DISK);
895 1.8 mycroft #endif
896 1.27 thorpej hpibgo(ctlr, slave, C_EXEC, rs->sc_addr, rs->sc_resid, rw, rw != 0);
897 1.1 cgd }
898 1.1 cgd
899 1.27 thorpej /* ARGSUSED */
900 1.63 thorpej static void
901 1.63 thorpej rdintr(void *arg)
902 1.1 cgd {
903 1.28 scottr struct rd_softc *rs = arg;
904 1.27 thorpej int unit = rs->sc_dev.dv_unit;
905 1.50 hannken struct buf *bp = BUFQ_PEEK(&rs->sc_tab);
906 1.1 cgd u_char stat = 13; /* in case hpibrecv fails */
907 1.27 thorpej int rv, restart, ctlr, slave;
908 1.27 thorpej
909 1.27 thorpej ctlr = rs->sc_dev.dv_parent->dv_unit;
910 1.27 thorpej slave = rs->sc_slave;
911 1.27 thorpej
912 1.1 cgd #ifdef DEBUG
913 1.1 cgd if (rddebug & RDB_FOLLOW)
914 1.29 scottr printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
915 1.1 cgd (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
916 1.1 cgd if (bp == NULL) {
917 1.27 thorpej printf("%s: bp == NULL\n", rs->sc_dev.dv_xname);
918 1.1 cgd return;
919 1.1 cgd }
920 1.1 cgd #endif
921 1.55 mrg disk_unbusy(&rs->sc_dkdev, (bp->b_bcount - bp->b_resid),
922 1.55 mrg (bp->b_flags & B_READ));
923 1.17 thorpej
924 1.1 cgd if (rs->sc_flags & RDF_SEEK) {
925 1.1 cgd rs->sc_flags &= ~RDF_SEEK;
926 1.27 thorpej if (hpibustart(ctlr))
927 1.27 thorpej rdgo(rs);
928 1.1 cgd return;
929 1.1 cgd }
930 1.1 cgd if ((rs->sc_flags & RDF_SWAIT) == 0) {
931 1.1 cgd #ifdef DEBUG
932 1.27 thorpej rs->sc_stats.rdpolltries++;
933 1.1 cgd #endif
934 1.27 thorpej if (hpibpptest(ctlr, slave) == 0) {
935 1.1 cgd #ifdef DEBUG
936 1.27 thorpej rs->sc_stats.rdpollwaits++;
937 1.1 cgd #endif
938 1.17 thorpej
939 1.17 thorpej /* Instrumentation. */
940 1.17 thorpej disk_busy(&rs->sc_dkdev);
941 1.1 cgd rs->sc_flags |= RDF_SWAIT;
942 1.27 thorpej hpibawait(ctlr);
943 1.1 cgd return;
944 1.1 cgd }
945 1.1 cgd } else
946 1.1 cgd rs->sc_flags &= ~RDF_SWAIT;
947 1.27 thorpej rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
948 1.1 cgd if (rv != 1 || stat) {
949 1.1 cgd #ifdef DEBUG
950 1.1 cgd if (rddebug & RDB_ERROR)
951 1.24 christos printf("rdintr: recv failed or bad stat %d\n", stat);
952 1.1 cgd #endif
953 1.1 cgd restart = rderror(unit);
954 1.1 cgd #ifdef DEBUG
955 1.27 thorpej rs->sc_stats.rdretries++;
956 1.1 cgd #endif
957 1.37 thorpej if (rs->sc_errcnt++ < RDRETRY) {
958 1.1 cgd if (restart)
959 1.27 thorpej rdstart(rs);
960 1.1 cgd return;
961 1.1 cgd }
962 1.1 cgd bp->b_flags |= B_ERROR;
963 1.1 cgd bp->b_error = EIO;
964 1.1 cgd }
965 1.27 thorpej if (rdfinish(rs, bp))
966 1.27 thorpej rdustart(rs);
967 1.44 he #if NRND > 0
968 1.44 he rnd_add_uint32(&rs->rnd_source, bp->b_blkno);
969 1.44 he #endif
970 1.1 cgd }
971 1.1 cgd
972 1.63 thorpej static int
973 1.63 thorpej rdstatus(struct rd_softc *rs)
974 1.1 cgd {
975 1.28 scottr int c, s;
976 1.1 cgd u_char stat;
977 1.1 cgd int rv;
978 1.1 cgd
979 1.27 thorpej c = rs->sc_dev.dv_parent->dv_unit;
980 1.27 thorpej s = rs->sc_slave;
981 1.1 cgd rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
982 1.1 cgd rs->sc_rsc.c_sram = C_SRAM;
983 1.1 cgd rs->sc_rsc.c_ram = C_RAM;
984 1.1 cgd rs->sc_rsc.c_cmd = C_STATUS;
985 1.46 gmcgarry memset((caddr_t)&rs->sc_stat, 0, sizeof(rs->sc_stat));
986 1.1 cgd rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
987 1.1 cgd if (rv != sizeof(rs->sc_rsc)) {
988 1.1 cgd #ifdef DEBUG
989 1.1 cgd if (rddebug & RDB_STATUS)
990 1.24 christos printf("rdstatus: send C_CMD failed %d != %d\n",
991 1.1 cgd rv, sizeof(rs->sc_rsc));
992 1.1 cgd #endif
993 1.1 cgd return(1);
994 1.1 cgd }
995 1.1 cgd rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
996 1.1 cgd if (rv != sizeof(rs->sc_stat)) {
997 1.1 cgd #ifdef DEBUG
998 1.1 cgd if (rddebug & RDB_STATUS)
999 1.24 christos printf("rdstatus: send C_EXEC failed %d != %d\n",
1000 1.1 cgd rv, sizeof(rs->sc_stat));
1001 1.1 cgd #endif
1002 1.1 cgd return(1);
1003 1.1 cgd }
1004 1.1 cgd rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
1005 1.1 cgd if (rv != 1 || stat) {
1006 1.1 cgd #ifdef DEBUG
1007 1.1 cgd if (rddebug & RDB_STATUS)
1008 1.24 christos printf("rdstatus: recv failed %d or bad stat %d\n",
1009 1.1 cgd rv, stat);
1010 1.1 cgd #endif
1011 1.1 cgd return(1);
1012 1.1 cgd }
1013 1.1 cgd return(0);
1014 1.1 cgd }
1015 1.1 cgd
1016 1.1 cgd /*
1017 1.1 cgd * Deal with errors.
1018 1.1 cgd * Returns 1 if request should be restarted,
1019 1.1 cgd * 0 if we should just quietly give up.
1020 1.1 cgd */
1021 1.63 thorpej static int
1022 1.63 thorpej rderror(int unit)
1023 1.1 cgd {
1024 1.27 thorpej struct rd_softc *rs = rd_cd.cd_devs[unit];
1025 1.28 scottr struct rd_stat *sp;
1026 1.1 cgd struct buf *bp;
1027 1.1 cgd daddr_t hwbn, pbn;
1028 1.63 thorpej char *hexstr(int, int); /* XXX */
1029 1.1 cgd
1030 1.1 cgd if (rdstatus(rs)) {
1031 1.1 cgd #ifdef DEBUG
1032 1.27 thorpej printf("%s: couldn't get status\n", rs->sc_dev.dv_xname);
1033 1.1 cgd #endif
1034 1.27 thorpej rdreset(rs);
1035 1.1 cgd return(1);
1036 1.1 cgd }
1037 1.1 cgd sp = &rs->sc_stat;
1038 1.1 cgd if (sp->c_fef & FEF_REXMT)
1039 1.1 cgd return(1);
1040 1.1 cgd if (sp->c_fef & FEF_PF) {
1041 1.27 thorpej rdreset(rs);
1042 1.1 cgd return(1);
1043 1.1 cgd }
1044 1.1 cgd /*
1045 1.1 cgd * Unit requests release for internal maintenance.
1046 1.1 cgd * We just delay awhile and try again later. Use expontially
1047 1.1 cgd * increasing backoff ala ethernet drivers since we don't really
1048 1.1 cgd * know how long the maintenance will take. With RDWAITC and
1049 1.1 cgd * RDRETRY as defined, the range is 1 to 32 seconds.
1050 1.1 cgd */
1051 1.1 cgd if (sp->c_fef & FEF_IMR) {
1052 1.1 cgd extern int hz;
1053 1.37 thorpej int rdtimo = RDWAITC << rs->sc_errcnt;
1054 1.1 cgd #ifdef DEBUG
1055 1.24 christos printf("%s: internal maintenance, %d second timeout\n",
1056 1.27 thorpej rs->sc_dev.dv_xname, rdtimo);
1057 1.27 thorpej rs->sc_stats.rdtimeouts++;
1058 1.1 cgd #endif
1059 1.27 thorpej hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
1060 1.41 thorpej callout_reset(&rs->sc_restart_ch, rdtimo * hz, rdrestart, rs);
1061 1.1 cgd return(0);
1062 1.1 cgd }
1063 1.1 cgd /*
1064 1.1 cgd * Only report error if we have reached the error reporting
1065 1.1 cgd * threshhold. By default, this will only report after the
1066 1.1 cgd * retry limit has been exceeded.
1067 1.1 cgd */
1068 1.37 thorpej if (rs->sc_errcnt < rderrthresh)
1069 1.1 cgd return(1);
1070 1.1 cgd
1071 1.1 cgd /*
1072 1.45 wiz * First conjure up the block number at which the error occurred.
1073 1.1 cgd * Note that not all errors report a block number, in that case
1074 1.1 cgd * we just use b_blkno.
1075 1.1 cgd */
1076 1.50 hannken bp = BUFQ_PEEK(&rs->sc_tab);
1077 1.17 thorpej pbn = rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
1078 1.1 cgd if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
1079 1.1 cgd (sp->c_ief & IEF_RRMASK)) {
1080 1.1 cgd hwbn = RDBTOS(pbn + bp->b_blkno);
1081 1.1 cgd pbn = bp->b_blkno;
1082 1.1 cgd } else {
1083 1.1 cgd hwbn = sp->c_blk;
1084 1.1 cgd pbn = RDSTOB(hwbn) - pbn;
1085 1.1 cgd }
1086 1.1 cgd /*
1087 1.1 cgd * Now output a generic message suitable for badsect.
1088 1.1 cgd * Note that we don't use harderr cuz it just prints
1089 1.1 cgd * out b_blkno which is just the beginning block number
1090 1.45 wiz * of the transfer, not necessary where the error occurred.
1091 1.1 cgd */
1092 1.56 gmcgarry printf("%s%c: hard error sn%" PRId64 "\n", rs->sc_dev.dv_xname,
1093 1.27 thorpej 'a'+rdpart(bp->b_dev), pbn);
1094 1.1 cgd /*
1095 1.1 cgd * Now report the status as returned by the hardware with
1096 1.1 cgd * attempt at interpretation (unless debugging).
1097 1.1 cgd */
1098 1.27 thorpej printf("%s %s error:", rs->sc_dev.dv_xname,
1099 1.27 thorpej (bp->b_flags & B_READ) ? "read" : "write");
1100 1.1 cgd #ifdef DEBUG
1101 1.1 cgd if (rddebug & RDB_ERROR) {
1102 1.1 cgd /* status info */
1103 1.24 christos printf("\n volume: %d, unit: %d\n",
1104 1.1 cgd (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
1105 1.1 cgd rdprinterr("reject", sp->c_ref, err_reject);
1106 1.1 cgd rdprinterr("fault", sp->c_fef, err_fault);
1107 1.1 cgd rdprinterr("access", sp->c_aef, err_access);
1108 1.1 cgd rdprinterr("info", sp->c_ief, err_info);
1109 1.24 christos printf(" block: %d, P1-P10: ", hwbn);
1110 1.30 thorpej printf("0x%x", *(u_int *)&sp->c_raw[0]);
1111 1.30 thorpej printf("0x%x", *(u_int *)&sp->c_raw[4]);
1112 1.30 thorpej printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1113 1.1 cgd /* command */
1114 1.24 christos printf(" ioc: ");
1115 1.30 thorpej printf("0x%x", *(u_int *)&rs->sc_ioc.c_pad);
1116 1.30 thorpej printf("0x%x", *(u_short *)&rs->sc_ioc.c_hiaddr);
1117 1.30 thorpej printf("0x%x", *(u_int *)&rs->sc_ioc.c_addr);
1118 1.30 thorpej printf("0x%x", *(u_short *)&rs->sc_ioc.c_nop2);
1119 1.30 thorpej printf("0x%x", *(u_int *)&rs->sc_ioc.c_len);
1120 1.30 thorpej printf("0x%x\n", *(u_short *)&rs->sc_ioc.c_cmd);
1121 1.1 cgd return(1);
1122 1.1 cgd }
1123 1.1 cgd #endif
1124 1.24 christos printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
1125 1.1 cgd (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
1126 1.1 cgd sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
1127 1.24 christos printf("P1-P10: ");
1128 1.30 thorpej printf("0x%x", *(u_int *)&sp->c_raw[0]);
1129 1.30 thorpej printf("0x%x", *(u_int *)&sp->c_raw[4]);
1130 1.30 thorpej printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1131 1.1 cgd return(1);
1132 1.12 thorpej }
1133 1.12 thorpej
1134 1.63 thorpej static int
1135 1.63 thorpej rdread(dev_t dev, struct uio *uio, int flags)
1136 1.12 thorpej {
1137 1.12 thorpej
1138 1.12 thorpej return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
1139 1.12 thorpej }
1140 1.12 thorpej
1141 1.63 thorpej static int
1142 1.63 thorpej rdwrite(dev_t dev, struct uio *uio, int flags)
1143 1.12 thorpej {
1144 1.12 thorpej
1145 1.12 thorpej return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
1146 1.1 cgd }
1147 1.1 cgd
1148 1.63 thorpej static int
1149 1.63 thorpej rdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1150 1.1 cgd {
1151 1.8 mycroft int unit = rdunit(dev);
1152 1.27 thorpej struct rd_softc *sc = rd_cd.cd_devs[unit];
1153 1.28 scottr struct disklabel *lp = sc->sc_dkdev.dk_label;
1154 1.8 mycroft int error, flags;
1155 1.8 mycroft
1156 1.8 mycroft switch (cmd) {
1157 1.8 mycroft case DIOCGDINFO:
1158 1.8 mycroft *(struct disklabel *)data = *lp;
1159 1.8 mycroft return (0);
1160 1.8 mycroft
1161 1.8 mycroft case DIOCGPART:
1162 1.8 mycroft ((struct partinfo *)data)->disklab = lp;
1163 1.8 mycroft ((struct partinfo *)data)->part =
1164 1.8 mycroft &lp->d_partitions[rdpart(dev)];
1165 1.8 mycroft return (0);
1166 1.8 mycroft
1167 1.8 mycroft case DIOCWLABEL:
1168 1.8 mycroft if ((flag & FWRITE) == 0)
1169 1.8 mycroft return (EBADF);
1170 1.8 mycroft if (*(int *)data)
1171 1.8 mycroft sc->sc_flags |= RDF_WLABEL;
1172 1.8 mycroft else
1173 1.8 mycroft sc->sc_flags &= ~RDF_WLABEL;
1174 1.8 mycroft return (0);
1175 1.8 mycroft
1176 1.8 mycroft case DIOCSDINFO:
1177 1.8 mycroft if ((flag & FWRITE) == 0)
1178 1.8 mycroft return (EBADF);
1179 1.8 mycroft return (setdisklabel(lp, (struct disklabel *)data,
1180 1.8 mycroft (sc->sc_flags & RDF_WLABEL) ? 0
1181 1.17 thorpej : sc->sc_dkdev.dk_openmask,
1182 1.8 mycroft (struct cpu_disklabel *)0));
1183 1.8 mycroft
1184 1.8 mycroft case DIOCWDINFO:
1185 1.8 mycroft if ((flag & FWRITE) == 0)
1186 1.8 mycroft return (EBADF);
1187 1.8 mycroft error = setdisklabel(lp, (struct disklabel *)data,
1188 1.8 mycroft (sc->sc_flags & RDF_WLABEL) ? 0
1189 1.17 thorpej : sc->sc_dkdev.dk_openmask,
1190 1.8 mycroft (struct cpu_disklabel *)0);
1191 1.8 mycroft if (error)
1192 1.8 mycroft return (error);
1193 1.8 mycroft flags = sc->sc_flags;
1194 1.8 mycroft sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1195 1.8 mycroft error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
1196 1.8 mycroft (struct cpu_disklabel *)0);
1197 1.8 mycroft sc->sc_flags = flags;
1198 1.8 mycroft return (error);
1199 1.47 gmcgarry
1200 1.47 gmcgarry case DIOCGDEFLABEL:
1201 1.47 gmcgarry rdgetdefaultlabel(sc, (struct disklabel *)data);
1202 1.47 gmcgarry return (0);
1203 1.8 mycroft }
1204 1.1 cgd return(EINVAL);
1205 1.47 gmcgarry }
1206 1.47 gmcgarry
1207 1.63 thorpej static void
1208 1.63 thorpej rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
1209 1.47 gmcgarry {
1210 1.47 gmcgarry int type = sc->sc_type;
1211 1.47 gmcgarry
1212 1.47 gmcgarry memset((caddr_t)lp, 0, sizeof(struct disklabel));
1213 1.47 gmcgarry
1214 1.47 gmcgarry lp->d_type = DTYPE_HPIB;
1215 1.49 gmcgarry lp->d_secsize = DEV_BSIZE;
1216 1.49 gmcgarry lp->d_nsectors = rdidentinfo[type].ri_nbpt;
1217 1.47 gmcgarry lp->d_ntracks = rdidentinfo[type].ri_ntpc;
1218 1.47 gmcgarry lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
1219 1.49 gmcgarry lp->d_secperunit = rdidentinfo[type].ri_nblocks;
1220 1.47 gmcgarry lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1221 1.62 tsutsui
1222 1.47 gmcgarry strncpy(lp->d_typename, rdidentinfo[type].ri_desc, 16);
1223 1.47 gmcgarry strncpy(lp->d_packname, "fictitious", 16);
1224 1.47 gmcgarry lp->d_rpm = 3000;
1225 1.47 gmcgarry lp->d_interleave = 1;
1226 1.47 gmcgarry lp->d_flags = 0;
1227 1.47 gmcgarry
1228 1.47 gmcgarry lp->d_partitions[RAW_PART].p_offset = 0;
1229 1.47 gmcgarry lp->d_partitions[RAW_PART].p_size =
1230 1.47 gmcgarry lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1231 1.47 gmcgarry lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1232 1.47 gmcgarry lp->d_npartitions = RAW_PART + 1;
1233 1.47 gmcgarry
1234 1.47 gmcgarry lp->d_magic = DISKMAGIC;
1235 1.47 gmcgarry lp->d_magic2 = DISKMAGIC;
1236 1.47 gmcgarry lp->d_checksum = dkcksum(lp);
1237 1.1 cgd }
1238 1.1 cgd
1239 1.1 cgd int
1240 1.63 thorpej rdsize(dev_t dev)
1241 1.1 cgd {
1242 1.28 scottr int unit = rdunit(dev);
1243 1.27 thorpej struct rd_softc *rs;
1244 1.8 mycroft int psize, didopen = 0;
1245 1.1 cgd
1246 1.27 thorpej if (unit >= rd_cd.cd_ndevs ||
1247 1.27 thorpej (rs = rd_cd.cd_devs[unit]) == NULL ||
1248 1.27 thorpej (rs->sc_flags & RDF_ALIVE) == 0)
1249 1.27 thorpej return (-1);
1250 1.8 mycroft
1251 1.8 mycroft /*
1252 1.8 mycroft * We get called very early on (via swapconf)
1253 1.8 mycroft * without the device being open so we may need
1254 1.8 mycroft * to handle it here.
1255 1.8 mycroft */
1256 1.17 thorpej if (rs->sc_dkdev.dk_openmask == 0) {
1257 1.8 mycroft if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1258 1.8 mycroft return(-1);
1259 1.8 mycroft didopen = 1;
1260 1.8 mycroft }
1261 1.32 thorpej psize = rs->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
1262 1.32 thorpej (rs->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
1263 1.8 mycroft if (didopen)
1264 1.8 mycroft (void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1265 1.8 mycroft return (psize);
1266 1.1 cgd }
1267 1.1 cgd
1268 1.1 cgd #ifdef DEBUG
1269 1.63 thorpej static void
1270 1.63 thorpej rdprinterr(const char *str, short err, const char **tab)
1271 1.1 cgd {
1272 1.28 scottr int i;
1273 1.1 cgd int printed;
1274 1.1 cgd
1275 1.1 cgd if (err == 0)
1276 1.1 cgd return;
1277 1.29 scottr printf(" %s error %d field:", str, err);
1278 1.1 cgd printed = 0;
1279 1.1 cgd for (i = 0; i < 16; i++)
1280 1.1 cgd if (err & (0x8000 >> i))
1281 1.24 christos printf("%s%s", printed++ ? " + " : " ", tab[i]);
1282 1.24 christos printf("\n");
1283 1.1 cgd }
1284 1.1 cgd #endif
1285 1.1 cgd
1286 1.22 thorpej static int rddoingadump; /* simple mutex */
1287 1.22 thorpej
1288 1.1 cgd /*
1289 1.58 wiz * Non-interrupt driven, non-DMA dump routine.
1290 1.1 cgd */
1291 1.63 thorpej static int
1292 1.63 thorpej rddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1293 1.22 thorpej {
1294 1.22 thorpej int sectorsize; /* size of a disk sector */
1295 1.22 thorpej int nsects; /* number of sectors in partition */
1296 1.22 thorpej int sectoff; /* sector offset of partition */
1297 1.22 thorpej int totwrt; /* total number of sectors left to write */
1298 1.22 thorpej int nwrt; /* current number of sectors to write */
1299 1.22 thorpej int unit, part;
1300 1.27 thorpej int ctlr, slave;
1301 1.22 thorpej struct rd_softc *rs;
1302 1.22 thorpej struct disklabel *lp;
1303 1.1 cgd char stat;
1304 1.1 cgd
1305 1.22 thorpej /* Check for recursive dump; if so, punt. */
1306 1.22 thorpej if (rddoingadump)
1307 1.22 thorpej return (EFAULT);
1308 1.22 thorpej rddoingadump = 1;
1309 1.22 thorpej
1310 1.22 thorpej /* Decompose unit and partition. */
1311 1.22 thorpej unit = rdunit(dev);
1312 1.22 thorpej part = rdpart(dev);
1313 1.22 thorpej
1314 1.22 thorpej /* Make sure dump device is ok. */
1315 1.27 thorpej if (unit >= rd_cd.cd_ndevs ||
1316 1.27 thorpej (rs = rd_cd.cd_devs[unit]) == NULL ||
1317 1.27 thorpej (rs->sc_flags & RDF_ALIVE) == 0)
1318 1.22 thorpej return (ENXIO);
1319 1.27 thorpej
1320 1.27 thorpej ctlr = rs->sc_dev.dv_parent->dv_unit;
1321 1.27 thorpej slave = rs->sc_slave;
1322 1.22 thorpej
1323 1.22 thorpej /*
1324 1.22 thorpej * Convert to disk sectors. Request must be a multiple of size.
1325 1.22 thorpej */
1326 1.22 thorpej lp = rs->sc_dkdev.dk_label;
1327 1.22 thorpej sectorsize = lp->d_secsize;
1328 1.22 thorpej if ((size % sectorsize) != 0)
1329 1.22 thorpej return (EFAULT);
1330 1.22 thorpej totwrt = size / sectorsize;
1331 1.22 thorpej blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1332 1.22 thorpej
1333 1.22 thorpej nsects = lp->d_partitions[part].p_size;
1334 1.22 thorpej sectoff = lp->d_partitions[part].p_offset;
1335 1.22 thorpej
1336 1.22 thorpej /* Check transfer bounds against partition size. */
1337 1.22 thorpej if ((blkno < 0) || (blkno + totwrt) > nsects)
1338 1.8 mycroft return (EINVAL);
1339 1.22 thorpej
1340 1.22 thorpej /* Offset block number to start of partition. */
1341 1.22 thorpej blkno += sectoff;
1342 1.22 thorpej
1343 1.22 thorpej while (totwrt > 0) {
1344 1.22 thorpej nwrt = totwrt; /* XXX */
1345 1.22 thorpej #ifndef RD_DUMP_NOT_TRUSTED
1346 1.22 thorpej /*
1347 1.22 thorpej * Fill out and send HPIB command.
1348 1.22 thorpej */
1349 1.1 cgd rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
1350 1.1 cgd rs->sc_ioc.c_volume = C_SVOL(0);
1351 1.1 cgd rs->sc_ioc.c_saddr = C_SADDR;
1352 1.1 cgd rs->sc_ioc.c_hiaddr = 0;
1353 1.22 thorpej rs->sc_ioc.c_addr = RDBTOS(blkno);
1354 1.1 cgd rs->sc_ioc.c_nop2 = C_NOP;
1355 1.1 cgd rs->sc_ioc.c_slen = C_SLEN;
1356 1.22 thorpej rs->sc_ioc.c_len = nwrt * sectorsize;
1357 1.1 cgd rs->sc_ioc.c_cmd = C_WRITE;
1358 1.27 thorpej hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
1359 1.27 thorpej sizeof(rs->sc_ioc)-2);
1360 1.27 thorpej if (hpibswait(ctlr, slave))
1361 1.1 cgd return (EIO);
1362 1.22 thorpej
1363 1.22 thorpej /*
1364 1.22 thorpej * Send the data.
1365 1.22 thorpej */
1366 1.27 thorpej hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
1367 1.27 thorpej (void) hpibswait(ctlr, slave);
1368 1.27 thorpej hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
1369 1.8 mycroft if (stat)
1370 1.1 cgd return (EIO);
1371 1.22 thorpej #else /* RD_DUMP_NOT_TRUSTED */
1372 1.22 thorpej /* Let's just talk about this first... */
1373 1.27 thorpej printf("%s: dump addr %p, blk %d\n", sc->sc_dev.dv_xname,
1374 1.22 thorpej va, blkno);
1375 1.22 thorpej delay(500 * 1000); /* half a second */
1376 1.22 thorpej #endif /* RD_DUMP_NOT_TRUSTED */
1377 1.22 thorpej
1378 1.22 thorpej /* update block count */
1379 1.22 thorpej totwrt -= nwrt;
1380 1.22 thorpej blkno += nwrt;
1381 1.22 thorpej va += sectorsize * nwrt;
1382 1.1 cgd }
1383 1.22 thorpej rddoingadump = 0;
1384 1.1 cgd return (0);
1385 1.1 cgd }
1386