hp.c revision 1.54 1 1.54 ragge /* $NetBSD: hp.c,v 1.54 2017/05/22 17:13:09 ragge Exp $ */
2 1.1 ragge /*
3 1.4 ragge * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 1.1 ragge * All rights reserved.
5 1.1 ragge *
6 1.1 ragge * Redistribution and use in source and binary forms, with or without
7 1.1 ragge * modification, are permitted provided that the following conditions
8 1.1 ragge * are met:
9 1.1 ragge * 1. Redistributions of source code must retain the above copyright
10 1.1 ragge * notice, this list of conditions and the following disclaimer.
11 1.1 ragge * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 ragge * notice, this list of conditions and the following disclaimer in the
13 1.1 ragge * documentation and/or other materials provided with the distribution.
14 1.1 ragge *
15 1.1 ragge * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 ragge * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 ragge * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 ragge * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 ragge * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 ragge * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 ragge * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 ragge * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 ragge * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 ragge * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 ragge */
26 1.1 ragge
27 1.4 ragge /*
28 1.4 ragge * Simple device driver routine for massbuss disks.
29 1.4 ragge * TODO:
30 1.4 ragge * Fix support for Standard DEC BAD144 bad block forwarding.
31 1.4 ragge * Be able to to handle soft/hard transfer errors.
32 1.4 ragge * Handle non-data transfer interrupts.
33 1.4 ragge * Autoconfiguration of disk drives 'on the fly'.
34 1.4 ragge * Handle disk media changes.
35 1.4 ragge * Dual-port operations should be supported.
36 1.4 ragge */
37 1.35 lukem
38 1.35 lukem #include <sys/cdefs.h>
39 1.54 ragge __KERNEL_RCSID(0, "$NetBSD: hp.c,v 1.54 2017/05/22 17:13:09 ragge Exp $");
40 1.35 lukem
41 1.2 mycroft #include <sys/param.h>
42 1.8 ragge #include <sys/systm.h>
43 1.48 matt #include <sys/bus.h>
44 1.48 matt #include <sys/cpu.h>
45 1.4 ragge #include <sys/device.h>
46 1.4 ragge #include <sys/disklabel.h>
47 1.4 ragge #include <sys/disk.h>
48 1.4 ragge #include <sys/dkio.h>
49 1.4 ragge #include <sys/buf.h>
50 1.37 yamt #include <sys/bufq.h>
51 1.4 ragge #include <sys/stat.h>
52 1.4 ragge #include <sys/ioccom.h>
53 1.2 mycroft #include <sys/fcntl.h>
54 1.24 ragge #include <sys/conf.h>
55 1.30 jdolecek #include <sys/event.h>
56 1.48 matt #include <sys/syslog.h>
57 1.4 ragge
58 1.4 ragge #include <vax/mba/mbavar.h>
59 1.2 mycroft #include <vax/mba/mbareg.h>
60 1.4 ragge #include <vax/mba/hpreg.h>
61 1.3 mycroft
62 1.24 ragge #include "ioconf.h"
63 1.16 jtk #include "locators.h"
64 1.16 jtk
65 1.46 matt struct hp_softc {
66 1.46 matt device_t sc_dev;
67 1.46 matt struct disk sc_disk;
68 1.24 ragge bus_space_tag_t sc_iot;
69 1.24 ragge bus_space_handle_t sc_ioh;
70 1.46 matt struct mba_device sc_md; /* Common struct used by mbaqueue. */
71 1.46 matt int sc_wlabel; /* Disklabel area is writable */
72 1.4 ragge };
73 1.1 ragge
74 1.46 matt int hpmatch(device_t, cfdata_t, void *);
75 1.46 matt void hpattach(device_t, device_t, void *);
76 1.24 ragge void hpstart(struct mba_device *);
77 1.24 ragge int hpattn(struct mba_device *);
78 1.24 ragge enum xfer_action hpfinish(struct mba_device *, int, int *);
79 1.1 ragge
80 1.46 matt CFATTACH_DECL_NEW(hp, sizeof(struct hp_softc),
81 1.29 thorpej hpmatch, hpattach, NULL, NULL);
82 1.17 thorpej
83 1.46 matt static dev_type_open(hpopen);
84 1.46 matt static dev_type_close(hpclose);
85 1.46 matt static dev_type_read(hpread);
86 1.46 matt static dev_type_write(hpwrite);
87 1.46 matt static dev_type_ioctl(hpioctl);
88 1.46 matt static dev_type_strategy(hpstrategy);
89 1.46 matt static dev_type_size(hppsize);
90 1.26 gehenna
91 1.26 gehenna const struct bdevsw hp_bdevsw = {
92 1.46 matt .d_open = hpopen,
93 1.46 matt .d_close = hpclose,
94 1.46 matt .d_strategy = hpstrategy,
95 1.46 matt .d_ioctl = hpioctl,
96 1.46 matt .d_dump = nulldump,
97 1.46 matt .d_psize = hppsize,
98 1.49 dholland .d_discard = nodiscard,
99 1.46 matt .d_flag = D_DISK
100 1.26 gehenna };
101 1.26 gehenna
102 1.26 gehenna const struct cdevsw hp_cdevsw = {
103 1.46 matt .d_open = hpopen,
104 1.46 matt .d_close = hpclose,
105 1.46 matt .d_read = hpread,
106 1.46 matt .d_write = hpwrite,
107 1.46 matt .d_ioctl = hpioctl,
108 1.46 matt .d_stop = nostop,
109 1.46 matt .d_tty = notty,
110 1.46 matt .d_poll = nopoll,
111 1.46 matt .d_mmap = nommap,
112 1.46 matt .d_kqfilter = nokqfilter,
113 1.50 dholland .d_discard = nodiscard,
114 1.46 matt .d_flag = D_DISK
115 1.26 gehenna };
116 1.26 gehenna
117 1.24 ragge #define HP_WCSR(reg, val) \
118 1.24 ragge bus_space_write_4(sc->sc_iot, sc->sc_ioh, (reg), (val))
119 1.24 ragge #define HP_RCSR(reg) \
120 1.24 ragge bus_space_read_4(sc->sc_iot, sc->sc_ioh, (reg))
121 1.24 ragge
122 1.1 ragge
123 1.4 ragge /*
124 1.4 ragge * Check if this is a disk drive; done by checking type from mbaattach.
125 1.4 ragge */
126 1.4 ragge int
127 1.46 matt hpmatch(device_t parent, cfdata_t cf, void *aux)
128 1.4 ragge {
129 1.46 matt struct mba_attach_args * const ma = aux;
130 1.1 ragge
131 1.16 jtk if (cf->cf_loc[MBACF_DRIVE] != MBACF_DRIVE_DEFAULT &&
132 1.24 ragge cf->cf_loc[MBACF_DRIVE] != ma->ma_unit)
133 1.4 ragge return 0;
134 1.1 ragge
135 1.24 ragge if (ma->ma_devtyp != MB_RP)
136 1.4 ragge return 0;
137 1.1 ragge
138 1.4 ragge return 1;
139 1.1 ragge }
140 1.1 ragge
141 1.4 ragge /*
142 1.4 ragge * Disk drive found; fake a disklabel and try to read the real one.
143 1.4 ragge * If the on-disk label can't be read; we lose.
144 1.4 ragge */
145 1.4 ragge void
146 1.46 matt hpattach(device_t parent, device_t self, void *aux)
147 1.4 ragge {
148 1.46 matt struct hp_softc * const sc = device_private(self);
149 1.46 matt struct mba_softc * const ms = device_private(parent);
150 1.46 matt struct mba_attach_args * const ma = aux;
151 1.46 matt struct disklabel *dl;
152 1.46 matt const char *msg;
153 1.4 ragge
154 1.46 matt sc->sc_dev = self;
155 1.24 ragge sc->sc_iot = ma->ma_iot;
156 1.24 ragge sc->sc_ioh = ma->ma_ioh;
157 1.46 matt
158 1.4 ragge /*
159 1.4 ragge * Init the common struct for both the adapter and its slaves.
160 1.4 ragge */
161 1.38 yamt bufq_alloc(&sc->sc_md.md_q, "disksort", BUFQ_SORT_CYLINDER);
162 1.46 matt sc->sc_md.md_softc = sc; /* Pointer to this softc */
163 1.46 matt sc->sc_md.md_mba = ms; /* Pointer to parent softc */
164 1.4 ragge sc->sc_md.md_start = hpstart; /* Disk start routine */
165 1.4 ragge sc->sc_md.md_attn = hpattn; /* Disk attention routine */
166 1.4 ragge sc->sc_md.md_finish = hpfinish; /* Disk xfer finish routine */
167 1.4 ragge
168 1.24 ragge ms->sc_md[ma->ma_unit] = &sc->sc_md; /* Per-unit backpointer */
169 1.1 ragge
170 1.4 ragge /*
171 1.4 ragge * Init and attach the disk structure.
172 1.4 ragge */
173 1.46 matt disk_init(&sc->sc_disk, device_xname(sc->sc_dev), NULL);
174 1.4 ragge disk_attach(&sc->sc_disk);
175 1.1 ragge
176 1.4 ragge /*
177 1.4 ragge * Fake a disklabel to be able to read in the real label.
178 1.4 ragge */
179 1.4 ragge dl = sc->sc_disk.dk_label;
180 1.1 ragge
181 1.4 ragge dl->d_secsize = DEV_BSIZE;
182 1.4 ragge dl->d_ntracks = 1;
183 1.4 ragge dl->d_nsectors = 32;
184 1.4 ragge dl->d_secpercyl = 32;
185 1.1 ragge
186 1.4 ragge /*
187 1.4 ragge * Read in label.
188 1.4 ragge */
189 1.40 thorpej if ((msg = readdisklabel(makedev(0, device_unit(self) * 8), hpstrategy,
190 1.4 ragge dl, NULL)) != NULL)
191 1.12 christos printf(": %s", msg);
192 1.12 christos printf(": %s, size = %d sectors\n", dl->d_typename, dl->d_secperunit);
193 1.4 ragge }
194 1.4 ragge
195 1.4 ragge
196 1.4 ragge void
197 1.24 ragge hpstrategy(struct buf *bp)
198 1.1 ragge {
199 1.46 matt struct hp_softc *sc;
200 1.46 matt struct buf *gp;
201 1.21 thorpej struct disklabel *lp;
202 1.46 matt int unit, s, err;
203 1.4 ragge
204 1.4 ragge unit = DISKUNIT(bp->b_dev);
205 1.46 matt sc = device_lookup_private(&hp_cd, unit);
206 1.21 thorpej lp = sc->sc_disk.dk_label;
207 1.4 ragge
208 1.34 thorpej err = bounds_check_with_label(&sc->sc_disk, bp, sc->sc_wlabel);
209 1.32 bouyer if (err <= 0)
210 1.4 ragge goto done;
211 1.21 thorpej
212 1.21 thorpej bp->b_rawblkno =
213 1.21 thorpej bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
214 1.21 thorpej bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
215 1.21 thorpej
216 1.4 ragge s = splbio();
217 1.4 ragge
218 1.47 yamt gp = bufq_peek(sc->sc_md.md_q);
219 1.47 yamt bufq_put(sc->sc_md.md_q, bp);
220 1.4 ragge if (gp == 0)
221 1.4 ragge mbaqueue(&sc->sc_md);
222 1.4 ragge
223 1.4 ragge splx(s);
224 1.4 ragge return;
225 1.4 ragge
226 1.4 ragge done:
227 1.4 ragge bp->b_resid = bp->b_bcount;
228 1.4 ragge biodone(bp);
229 1.4 ragge }
230 1.1 ragge
231 1.1 ragge /*
232 1.4 ragge * Start transfer on given disk. Called from mbastart().
233 1.1 ragge */
234 1.4 ragge void
235 1.46 matt hpstart(struct mba_device *md)
236 1.4 ragge {
237 1.46 matt struct hp_softc * const sc = md->md_softc;
238 1.46 matt struct disklabel * const lp = sc->sc_disk.dk_label;
239 1.47 yamt struct buf *bp = bufq_peek(md->md_q);
240 1.4 ragge unsigned bn, cn, sn, tn;
241 1.4 ragge
242 1.4 ragge /*
243 1.4 ragge * Collect statistics.
244 1.4 ragge */
245 1.4 ragge disk_busy(&sc->sc_disk);
246 1.42 he iostat_seek(sc->sc_disk.dk_stats);
247 1.4 ragge
248 1.21 thorpej bn = bp->b_rawblkno;
249 1.4 ragge if (bn) {
250 1.4 ragge cn = bn / lp->d_secpercyl;
251 1.4 ragge sn = bn % lp->d_secpercyl;
252 1.4 ragge tn = sn / lp->d_nsectors;
253 1.4 ragge sn = sn % lp->d_nsectors;
254 1.4 ragge } else
255 1.4 ragge cn = sn = tn = 0;
256 1.4 ragge
257 1.24 ragge HP_WCSR(HP_DC, cn);
258 1.24 ragge HP_WCSR(HP_DA, (tn << 8) | sn);
259 1.4 ragge if (bp->b_flags & B_READ)
260 1.24 ragge HP_WCSR(HP_CS1, HPCS_READ);
261 1.4 ragge else
262 1.24 ragge HP_WCSR(HP_CS1, HPCS_WRITE);
263 1.4 ragge }
264 1.4 ragge
265 1.4 ragge int
266 1.39 christos hpopen(dev_t dev, int flag, int fmt, struct lwp *l)
267 1.4 ragge {
268 1.46 matt struct hp_softc *sc;
269 1.46 matt int part = DISKPART(dev);
270 1.4 ragge
271 1.46 matt sc = device_lookup_private(&hp_cd, DISKUNIT(dev));
272 1.46 matt if (sc == NULL)
273 1.4 ragge return ENXIO;
274 1.4 ragge
275 1.5 ragge if (part >= sc->sc_disk.dk_label->d_npartitions)
276 1.4 ragge return ENXIO;
277 1.4 ragge
278 1.4 ragge switch (fmt) {
279 1.46 matt case S_IFCHR:
280 1.4 ragge sc->sc_disk.dk_copenmask |= (1 << part);
281 1.4 ragge break;
282 1.4 ragge
283 1.46 matt case S_IFBLK:
284 1.4 ragge sc->sc_disk.dk_bopenmask |= (1 << part);
285 1.4 ragge break;
286 1.1 ragge }
287 1.4 ragge sc->sc_disk.dk_openmask =
288 1.4 ragge sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask;
289 1.4 ragge
290 1.4 ragge return 0;
291 1.4 ragge }
292 1.4 ragge
293 1.4 ragge int
294 1.39 christos hpclose(dev_t dev, int flag, int fmt, struct lwp *l)
295 1.4 ragge {
296 1.46 matt struct hp_softc * const sc = device_lookup_private(&hp_cd, DISKUNIT(dev));
297 1.46 matt const int part = DISKPART(dev);
298 1.4 ragge
299 1.4 ragge switch (fmt) {
300 1.46 matt case S_IFCHR:
301 1.4 ragge sc->sc_disk.dk_copenmask &= ~(1 << part);
302 1.4 ragge break;
303 1.4 ragge
304 1.46 matt case S_IFBLK:
305 1.4 ragge sc->sc_disk.dk_bopenmask &= ~(1 << part);
306 1.4 ragge break;
307 1.1 ragge }
308 1.4 ragge sc->sc_disk.dk_openmask =
309 1.4 ragge sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask;
310 1.4 ragge
311 1.4 ragge return 0;
312 1.4 ragge }
313 1.1 ragge
314 1.4 ragge int
315 1.43 christos hpioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
316 1.4 ragge {
317 1.46 matt struct hp_softc * const sc = device_lookup_private(&hp_cd, DISKUNIT(dev));
318 1.46 matt struct disklabel * const lp = sc->sc_disk.dk_label;
319 1.5 ragge int error;
320 1.1 ragge
321 1.53 msaitoh error = disk_ioctl(&sc->sc_disk, dev, cmd, addr, flag, l);
322 1.51 christos if (error != EPASSTHROUGH)
323 1.52 christos return error;
324 1.51 christos
325 1.4 ragge switch (cmd) {
326 1.46 matt case DIOCSDINFO:
327 1.4 ragge if ((flag & FWRITE) == 0)
328 1.4 ragge return EBADF;
329 1.4 ragge
330 1.4 ragge return setdisklabel(lp, (struct disklabel *)addr, 0, 0);
331 1.4 ragge
332 1.46 matt case DIOCWDINFO:
333 1.4 ragge if ((flag & FWRITE) == 0)
334 1.5 ragge error = EBADF;
335 1.5 ragge else {
336 1.5 ragge sc->sc_wlabel = 1;
337 1.5 ragge error = writedisklabel(dev, hpstrategy, lp, 0);
338 1.5 ragge sc->sc_wlabel = 0;
339 1.5 ragge }
340 1.5 ragge return error;
341 1.46 matt case DIOCWLABEL:
342 1.6 ragge if ((flag & FWRITE) == 0)
343 1.6 ragge return EBADF;
344 1.6 ragge sc->sc_wlabel = 1;
345 1.6 ragge break;
346 1.4 ragge
347 1.4 ragge default:
348 1.4 ragge return ENOTTY;
349 1.4 ragge }
350 1.6 ragge return 0;
351 1.1 ragge }
352 1.1 ragge
353 1.1 ragge /*
354 1.4 ragge * Called when a transfer is finished. Check if transfer went OK,
355 1.4 ragge * Return info about what-to-do-now.
356 1.1 ragge */
357 1.4 ragge enum xfer_action
358 1.24 ragge hpfinish(struct mba_device *md, int mbasr, int *attn)
359 1.1 ragge {
360 1.46 matt struct hp_softc * const sc = md->md_softc;
361 1.47 yamt struct buf *bp = bufq_peek(md->md_q);
362 1.24 ragge int er1, er2, bc;
363 1.4 ragge unsigned byte;
364 1.4 ragge
365 1.24 ragge er1 = HP_RCSR(HP_ER1);
366 1.24 ragge er2 = HP_RCSR(HP_ER2);
367 1.24 ragge HP_WCSR(HP_ER1, 0);
368 1.24 ragge HP_WCSR(HP_ER2, 0);
369 1.24 ragge
370 1.4 ragge hper1:
371 1.4 ragge switch (ffs(er1) - 1) {
372 1.4 ragge case -1:
373 1.24 ragge HP_WCSR(HP_ER1, 0);
374 1.4 ragge goto hper2;
375 1.4 ragge
376 1.4 ragge case HPER1_DCK: /* Corrected? data read. Just notice. */
377 1.24 ragge bc = bus_space_read_4(md->md_mba->sc_iot,
378 1.24 ragge md->md_mba->sc_ioh, MBA_BC);
379 1.4 ragge byte = ~(bc >> 16);
380 1.36 pk diskerr(bp, hp_cd.cd_name, "soft ecc", LOG_PRINTF,
381 1.4 ragge btodb(bp->b_bcount - byte), sc->sc_disk.dk_label);
382 1.4 ragge er1 &= ~(1<<HPER1_DCK);
383 1.4 ragge break;
384 1.4 ragge
385 1.4 ragge default:
386 1.46 matt aprint_error_dev(sc->sc_dev, "drive error: er1 %x er2 %x\n",
387 1.46 matt er1, er2);
388 1.24 ragge HP_WCSR(HP_ER1, 0);
389 1.24 ragge HP_WCSR(HP_ER2, 0);
390 1.4 ragge goto hper2;
391 1.4 ragge }
392 1.4 ragge goto hper1;
393 1.1 ragge
394 1.4 ragge hper2:
395 1.4 ragge mbasr &= ~(MBASR_DTBUSY|MBASR_DTCMP|MBASR_ATTN);
396 1.4 ragge if (mbasr)
397 1.46 matt aprint_error_dev(sc->sc_dev, "massbuss error: %x\n", mbasr);
398 1.4 ragge
399 1.47 yamt bufq_peek(md->md_q)->b_resid = 0;
400 1.47 yamt disk_unbusy(&sc->sc_disk, bufq_peek(md->md_q)->b_bcount,
401 1.31 mrg (bp->b_flags & B_READ));
402 1.4 ragge return XFER_FINISH;
403 1.1 ragge }
404 1.1 ragge
405 1.1 ragge /*
406 1.4 ragge * Non-data transfer interrupt; like volume change.
407 1.1 ragge */
408 1.4 ragge int
409 1.24 ragge hpattn(struct mba_device *md)
410 1.4 ragge {
411 1.46 matt struct hp_softc * const sc = md->md_softc;
412 1.4 ragge int er1, er2;
413 1.4 ragge
414 1.24 ragge er1 = HP_RCSR(HP_ER1);
415 1.24 ragge er2 = HP_RCSR(HP_ER2);
416 1.4 ragge
417 1.46 matt aprint_error_dev(sc->sc_dev, "Attention! er1 %x er2 %x\n", er1, er2);
418 1.4 ragge return 0;
419 1.4 ragge }
420 1.4 ragge
421 1.4 ragge
422 1.4 ragge int
423 1.46 matt hppsize(dev_t dev)
424 1.1 ragge {
425 1.46 matt struct hp_softc * const sc = device_lookup_private(&hp_cd, DISKUNIT(dev));
426 1.46 matt const int part = DISKPART(dev);
427 1.4 ragge
428 1.46 matt if (sc == NULL || part >= sc->sc_disk.dk_label->d_npartitions)
429 1.4 ragge return -1;
430 1.4 ragge
431 1.46 matt return sc->sc_disk.dk_label->d_partitions[part].p_size *
432 1.15 thorpej (sc->sc_disk.dk_label->d_secsize / DEV_BSIZE);
433 1.4 ragge }
434 1.1 ragge
435 1.4 ragge int
436 1.24 ragge hpread(dev_t dev, struct uio *uio, int ioflag)
437 1.4 ragge {
438 1.4 ragge return (physio(hpstrategy, NULL, dev, B_READ, minphys, uio));
439 1.1 ragge }
440 1.1 ragge
441 1.4 ragge int
442 1.24 ragge hpwrite(dev_t dev, struct uio *uio, int ioflag)
443 1.1 ragge {
444 1.4 ragge return (physio(hpstrategy, NULL, dev, B_WRITE, minphys, uio));
445 1.1 ragge }
446