ed_mca.c revision 1.66.16.1 1 1.66.16.1 pgoyette /* $NetBSD: ed_mca.c,v 1.66.16.1 2018/09/06 06:55:50 pgoyette Exp $ */
2 1.1 jdolecek
3 1.1 jdolecek /*
4 1.1 jdolecek * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.41 martin * All rights reserved.
6 1.1 jdolecek *
7 1.1 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jdolecek * by Jaromir Dolecek.
9 1.1 jdolecek *
10 1.1 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.1 jdolecek * modification, are permitted provided that the following conditions
12 1.1 jdolecek * are met:
13 1.1 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.1 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.1 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.1 jdolecek * documentation and/or other materials provided with the distribution.
18 1.1 jdolecek *
19 1.41 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.41 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.41 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.41 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.41 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.41 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.41 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.41 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.41 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.41 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.41 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jdolecek */
31 1.1 jdolecek
32 1.1 jdolecek /*
33 1.10 jdolecek * Disk drive goo for MCA ESDI controller driver.
34 1.1 jdolecek */
35 1.9 lukem
36 1.9 lukem #include <sys/cdefs.h>
37 1.66.16.1 pgoyette __KERNEL_RCSID(0, "$NetBSD: ed_mca.c,v 1.66.16.1 2018/09/06 06:55:50 pgoyette Exp $");
38 1.1 jdolecek
39 1.1 jdolecek #include <sys/param.h>
40 1.1 jdolecek #include <sys/systm.h>
41 1.1 jdolecek #include <sys/kernel.h>
42 1.1 jdolecek #include <sys/conf.h>
43 1.1 jdolecek #include <sys/file.h>
44 1.1 jdolecek #include <sys/stat.h>
45 1.1 jdolecek #include <sys/ioctl.h>
46 1.1 jdolecek #include <sys/buf.h>
47 1.27 yamt #include <sys/bufq.h>
48 1.1 jdolecek #include <sys/uio.h>
49 1.1 jdolecek #include <sys/malloc.h>
50 1.1 jdolecek #include <sys/device.h>
51 1.1 jdolecek #include <sys/disklabel.h>
52 1.1 jdolecek #include <sys/disk.h>
53 1.1 jdolecek #include <sys/syslog.h>
54 1.1 jdolecek #include <sys/proc.h>
55 1.1 jdolecek #include <sys/vnode.h>
56 1.63 riastrad #include <sys/rndsource.h>
57 1.1 jdolecek
58 1.39 ad #include <sys/intr.h>
59 1.39 ad #include <sys/bus.h>
60 1.1 jdolecek
61 1.4 jdolecek #include <dev/mca/mcavar.h>
62 1.4 jdolecek
63 1.2 jdolecek #include <dev/mca/edcreg.h>
64 1.1 jdolecek #include <dev/mca/edvar.h>
65 1.2 jdolecek #include <dev/mca/edcvar.h>
66 1.1 jdolecek
67 1.23 thorpej /* #define ATADEBUG */
68 1.1 jdolecek
69 1.23 thorpej #ifdef ATADEBUG
70 1.23 thorpej #define ATADEBUG_PRINT(args, level) printf args
71 1.1 jdolecek #else
72 1.23 thorpej #define ATADEBUG_PRINT(args, level)
73 1.1 jdolecek #endif
74 1.1 jdolecek
75 1.1 jdolecek #define EDLABELDEV(dev) (MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART))
76 1.1 jdolecek
77 1.65 msaitoh static int ed_mca_probe (device_t, cfdata_t, void *);
78 1.65 msaitoh static void ed_mca_attach (device_t, device_t, void *);
79 1.1 jdolecek
80 1.53 chs CFATTACH_DECL_NEW(ed_mca, sizeof(struct ed_softc),
81 1.17 thorpej ed_mca_probe, ed_mca_attach, NULL, NULL);
82 1.1 jdolecek
83 1.1 jdolecek extern struct cfdriver ed_cd;
84 1.1 jdolecek
85 1.28 perry static int ed_get_params(struct ed_softc *, int *);
86 1.28 perry static void edgetdisklabel(dev_t, struct ed_softc *);
87 1.28 perry static void edgetdefaultlabel(struct ed_softc *, struct disklabel *);
88 1.14 gehenna
89 1.14 gehenna dev_type_open(edmcaopen);
90 1.14 gehenna dev_type_close(edmcaclose);
91 1.14 gehenna dev_type_read(edmcaread);
92 1.14 gehenna dev_type_write(edmcawrite);
93 1.14 gehenna dev_type_ioctl(edmcaioctl);
94 1.14 gehenna dev_type_strategy(edmcastrategy);
95 1.14 gehenna dev_type_dump(edmcadump);
96 1.14 gehenna dev_type_size(edmcasize);
97 1.14 gehenna
98 1.14 gehenna const struct bdevsw ed_bdevsw = {
99 1.54 dholland .d_open = edmcaopen,
100 1.54 dholland .d_close = edmcaclose,
101 1.54 dholland .d_strategy = edmcastrategy,
102 1.54 dholland .d_ioctl = edmcaioctl,
103 1.54 dholland .d_dump = edmcadump,
104 1.54 dholland .d_psize = edmcasize,
105 1.56 dholland .d_discard = nodiscard,
106 1.54 dholland .d_flag = D_DISK
107 1.14 gehenna };
108 1.14 gehenna
109 1.14 gehenna const struct cdevsw ed_cdevsw = {
110 1.54 dholland .d_open = edmcaopen,
111 1.54 dholland .d_close = edmcaclose,
112 1.54 dholland .d_read = edmcaread,
113 1.54 dholland .d_write = edmcawrite,
114 1.54 dholland .d_ioctl = edmcaioctl,
115 1.54 dholland .d_stop = nostop,
116 1.54 dholland .d_tty = notty,
117 1.54 dholland .d_poll = nopoll,
118 1.54 dholland .d_mmap = nommap,
119 1.54 dholland .d_kqfilter = nokqfilter,
120 1.57 dholland .d_discard = nodiscard,
121 1.54 dholland .d_flag = D_DISK
122 1.14 gehenna };
123 1.1 jdolecek
124 1.64 mlelstv static struct dkdriver eddkdriver = {
125 1.64 mlelstv .d_strategy = edmcastrategy,
126 1.64 mlelstv .d_minphys = minphys
127 1.64 mlelstv };
128 1.1 jdolecek
129 1.1 jdolecek /*
130 1.1 jdolecek * Just check if it's possible to identify the disk.
131 1.1 jdolecek */
132 1.1 jdolecek static int
133 1.53 chs ed_mca_probe(device_t parent, cfdata_t cf, void *aux)
134 1.1 jdolecek {
135 1.53 chs struct edc_mca_softc *sc = device_private(parent);
136 1.53 chs struct ed_attach_args *eda = aux;
137 1.1 jdolecek u_int16_t cmd_args[2];
138 1.6 jdolecek int found = 1;
139 1.1 jdolecek
140 1.1 jdolecek /*
141 1.1 jdolecek * Get Device Configuration (09).
142 1.1 jdolecek */
143 1.6 jdolecek cmd_args[0] = 14; /* Options: 00s110, s: 0=Physical 1=Pseudo */
144 1.1 jdolecek cmd_args[1] = 0;
145 1.10 jdolecek if (edc_run_cmd(sc, CMD_GET_DEV_CONF, eda->edc_drive, cmd_args, 2, 1))
146 1.6 jdolecek found = 0;
147 1.1 jdolecek
148 1.6 jdolecek return (found);
149 1.1 jdolecek }
150 1.1 jdolecek
151 1.1 jdolecek static void
152 1.47 cegger ed_mca_attach(device_t parent, device_t self, void *aux)
153 1.1 jdolecek {
154 1.32 thorpej struct ed_softc *ed = device_private(self);
155 1.32 thorpej struct edc_mca_softc *sc = device_private(parent);
156 1.53 chs struct ed_attach_args *eda = aux;
157 1.25 thorpej char pbuf[8];
158 1.10 jdolecek int drv_flags;
159 1.1 jdolecek
160 1.53 chs ed->sc_dev = self;
161 1.2 jdolecek ed->edc_softc = sc;
162 1.10 jdolecek ed->sc_devno = eda->edc_drive;
163 1.10 jdolecek edc_add_disk(sc, ed);
164 1.1 jdolecek
165 1.30 yamt bufq_alloc(&ed->sc_q, "disksort", BUFQ_SORT_RAWBLOCK);
166 1.55 skrll mutex_init(&ed->sc_q_lock, MUTEX_DEFAULT, IPL_VM);
167 1.1 jdolecek
168 1.10 jdolecek if (ed_get_params(ed, &drv_flags)) {
169 1.66 msaitoh aprint_error(": IDENTIFY failed, no disk found\n");
170 1.1 jdolecek return;
171 1.1 jdolecek }
172 1.1 jdolecek
173 1.1 jdolecek format_bytes(pbuf, sizeof(pbuf),
174 1.1 jdolecek (u_int64_t) ed->sc_capacity * DEV_BSIZE);
175 1.66 msaitoh aprint_normal(": %s, %u cyl, %u head, %u sec, 512 bytes/sect x "
176 1.66 msaitoh "%u sectors\n", pbuf,
177 1.66 msaitoh ed->cyl, ed->heads, ed->sectors,
178 1.66 msaitoh ed->sc_capacity);
179 1.66 msaitoh
180 1.66 msaitoh aprint_normal("%s: %u spares/cyl, %s, %s, %s, %s, %s\n",
181 1.66 msaitoh device_xname(ed->sc_dev), ed->spares,
182 1.66 msaitoh (drv_flags & (1 << 0)) ? "NoRetries" : "Retries",
183 1.66 msaitoh (drv_flags & (1 << 1)) ? "Removable" : "Fixed",
184 1.66 msaitoh (drv_flags & (1 << 2)) ? "SkewedFormat" : "NoSkew",
185 1.66 msaitoh (drv_flags & (1 << 3)) ? "ZeroDefect" : "Defects",
186 1.66 msaitoh (drv_flags & (1 << 4)) ? "InvalidSecondary" : "SecondaryOK");
187 1.8 sommerfe
188 1.1 jdolecek /*
189 1.1 jdolecek * Initialize and attach the disk structure.
190 1.1 jdolecek */
191 1.53 chs disk_init(&ed->sc_dk, device_xname(ed->sc_dev), &eddkdriver);
192 1.1 jdolecek disk_attach(&ed->sc_dk);
193 1.53 chs rnd_attach_source(&ed->rnd_source, device_xname(ed->sc_dev),
194 1.58 tls RND_TYPE_DISK, RND_FLAG_DEFAULT);
195 1.1 jdolecek
196 1.6 jdolecek ed->sc_flags |= EDF_INIT;
197 1.25 thorpej
198 1.26 thorpej /*
199 1.26 thorpej * XXX We should try to discovery wedges here, but
200 1.26 thorpej * XXX that would mean being able to do I/O. Should
201 1.26 thorpej * XXX use config_defer() here.
202 1.26 thorpej */
203 1.1 jdolecek }
204 1.1 jdolecek
205 1.1 jdolecek /*
206 1.1 jdolecek * Read/write routine for a buffer. Validates the arguments and schedules the
207 1.1 jdolecek * transfer. Does not wait for the transfer to complete.
208 1.1 jdolecek */
209 1.1 jdolecek void
210 1.44 dsl edmcastrategy(struct buf *bp)
211 1.1 jdolecek {
212 1.42 tsutsui struct ed_softc *ed;
213 1.42 tsutsui struct disklabel *lp;
214 1.1 jdolecek daddr_t blkno;
215 1.1 jdolecek
216 1.42 tsutsui ed = device_lookup_private(&ed_cd, DISKUNIT(bp->b_dev));
217 1.42 tsutsui lp = ed->sc_dk.dk_label;
218 1.42 tsutsui
219 1.53 chs ATADEBUG_PRINT(("edmcastrategy (%s)\n", device_xname(ed->sc_dev)),
220 1.1 jdolecek DEBUG_XFERS);
221 1.8 sommerfe
222 1.1 jdolecek /* Valid request? */
223 1.1 jdolecek if (bp->b_blkno < 0 ||
224 1.1 jdolecek (bp->b_bcount % lp->d_secsize) != 0 ||
225 1.1 jdolecek (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
226 1.1 jdolecek bp->b_error = EINVAL;
227 1.37 ad goto done;
228 1.1 jdolecek }
229 1.8 sommerfe
230 1.1 jdolecek /* If device invalidated (e.g. media change, door open), error. */
231 1.10 jdolecek if ((ed->sc_flags & WDF_LOADED) == 0) {
232 1.1 jdolecek bp->b_error = EIO;
233 1.37 ad goto done;
234 1.1 jdolecek }
235 1.1 jdolecek
236 1.1 jdolecek /* If it's a null transfer, return immediately. */
237 1.1 jdolecek if (bp->b_bcount == 0)
238 1.1 jdolecek goto done;
239 1.1 jdolecek
240 1.1 jdolecek /*
241 1.1 jdolecek * Do bounds checking, adjust transfer. if error, process.
242 1.1 jdolecek * If end of partition, just return.
243 1.1 jdolecek */
244 1.1 jdolecek if (DISKPART(bp->b_dev) != RAW_PART &&
245 1.20 thorpej bounds_check_with_label(&ed->sc_dk, bp,
246 1.10 jdolecek (ed->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
247 1.1 jdolecek goto done;
248 1.1 jdolecek
249 1.1 jdolecek /*
250 1.1 jdolecek * Now convert the block number to absolute and put it in
251 1.1 jdolecek * terms of the device's logical block size.
252 1.1 jdolecek */
253 1.1 jdolecek if (lp->d_secsize >= DEV_BSIZE)
254 1.1 jdolecek blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
255 1.1 jdolecek else
256 1.1 jdolecek blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
257 1.1 jdolecek
258 1.1 jdolecek if (DISKPART(bp->b_dev) != RAW_PART)
259 1.1 jdolecek blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
260 1.1 jdolecek
261 1.1 jdolecek bp->b_rawblkno = blkno;
262 1.1 jdolecek
263 1.1 jdolecek /* Queue transfer on drive, activate drive and controller if idle. */
264 1.55 skrll mutex_enter(&ed->sc_q_lock);
265 1.43 yamt bufq_put(ed->sc_q, bp);
266 1.55 skrll mutex_exit(&ed->sc_q_lock);
267 1.1 jdolecek
268 1.1 jdolecek /* Ring the worker thread */
269 1.48 rmind wakeup(ed->edc_softc);
270 1.1 jdolecek
271 1.1 jdolecek return;
272 1.1 jdolecek done:
273 1.1 jdolecek /* Toss transfer; we're done early. */
274 1.1 jdolecek bp->b_resid = bp->b_bcount;
275 1.1 jdolecek biodone(bp);
276 1.1 jdolecek }
277 1.1 jdolecek
278 1.1 jdolecek int
279 1.34 christos edmcaread(dev_t dev, struct uio *uio, int flags)
280 1.1 jdolecek {
281 1.23 thorpej ATADEBUG_PRINT(("edread\n"), DEBUG_XFERS);
282 1.1 jdolecek return (physio(edmcastrategy, NULL, dev, B_READ, minphys, uio));
283 1.1 jdolecek }
284 1.1 jdolecek
285 1.1 jdolecek int
286 1.34 christos edmcawrite(dev_t dev, struct uio *uio, int flags)
287 1.1 jdolecek {
288 1.23 thorpej ATADEBUG_PRINT(("edwrite\n"), DEBUG_XFERS);
289 1.1 jdolecek return (physio(edmcastrategy, NULL, dev, B_WRITE, minphys, uio));
290 1.1 jdolecek }
291 1.1 jdolecek
292 1.1 jdolecek int
293 1.34 christos edmcaopen(dev_t dev, int flag, int fmt, struct lwp *l)
294 1.1 jdolecek {
295 1.1 jdolecek struct ed_softc *wd;
296 1.1 jdolecek int part, error;
297 1.1 jdolecek
298 1.23 thorpej ATADEBUG_PRINT(("edopen\n"), DEBUG_FUNCS);
299 1.42 tsutsui wd = device_lookup_private(&ed_cd, DISKUNIT(dev));
300 1.6 jdolecek if (wd == NULL || (wd->sc_flags & EDF_INIT) == 0)
301 1.1 jdolecek return (ENXIO);
302 1.1 jdolecek
303 1.25 thorpej part = DISKPART(dev);
304 1.25 thorpej
305 1.36 ad mutex_enter(&wd->sc_dk.dk_openlock);
306 1.25 thorpej
307 1.25 thorpej /*
308 1.25 thorpej * If there are wedges, and this is not RAW_PART, then we
309 1.25 thorpej * need to fail.
310 1.25 thorpej */
311 1.25 thorpej if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
312 1.25 thorpej error = EBUSY;
313 1.25 thorpej goto bad1;
314 1.25 thorpej }
315 1.1 jdolecek
316 1.1 jdolecek if (wd->sc_dk.dk_openmask != 0) {
317 1.1 jdolecek /*
318 1.1 jdolecek * If any partition is open, but the disk has been invalidated,
319 1.1 jdolecek * disallow further opens.
320 1.1 jdolecek */
321 1.1 jdolecek if ((wd->sc_flags & WDF_LOADED) == 0) {
322 1.1 jdolecek error = EIO;
323 1.25 thorpej goto bad1;
324 1.1 jdolecek }
325 1.1 jdolecek } else {
326 1.1 jdolecek if ((wd->sc_flags & WDF_LOADED) == 0) {
327 1.10 jdolecek int s;
328 1.10 jdolecek
329 1.1 jdolecek wd->sc_flags |= WDF_LOADED;
330 1.1 jdolecek
331 1.1 jdolecek /* Load the physical device parameters. */
332 1.10 jdolecek s = splbio();
333 1.10 jdolecek ed_get_params(wd, NULL);
334 1.10 jdolecek splx(s);
335 1.1 jdolecek
336 1.1 jdolecek /* Load the partition info if not already loaded. */
337 1.10 jdolecek edgetdisklabel(dev, wd);
338 1.1 jdolecek }
339 1.1 jdolecek }
340 1.1 jdolecek
341 1.1 jdolecek /* Check that the partition exists. */
342 1.1 jdolecek if (part != RAW_PART &&
343 1.1 jdolecek (part >= wd->sc_dk.dk_label->d_npartitions ||
344 1.1 jdolecek wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
345 1.1 jdolecek error = ENXIO;
346 1.25 thorpej goto bad1;
347 1.1 jdolecek }
348 1.8 sommerfe
349 1.1 jdolecek /* Insure only one open at a time. */
350 1.1 jdolecek switch (fmt) {
351 1.1 jdolecek case S_IFCHR:
352 1.1 jdolecek wd->sc_dk.dk_copenmask |= (1 << part);
353 1.1 jdolecek break;
354 1.1 jdolecek case S_IFBLK:
355 1.1 jdolecek wd->sc_dk.dk_bopenmask |= (1 << part);
356 1.1 jdolecek break;
357 1.1 jdolecek }
358 1.1 jdolecek wd->sc_dk.dk_openmask =
359 1.1 jdolecek wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
360 1.1 jdolecek
361 1.36 ad error = 0;
362 1.25 thorpej bad1:
363 1.36 ad mutex_exit(&wd->sc_dk.dk_openlock);
364 1.1 jdolecek return (error);
365 1.1 jdolecek }
366 1.1 jdolecek
367 1.1 jdolecek int
368 1.34 christos edmcaclose(dev_t dev, int flag, int fmt, struct lwp *l)
369 1.1 jdolecek {
370 1.42 tsutsui struct ed_softc *wd = device_lookup_private(&ed_cd, DISKUNIT(dev));
371 1.1 jdolecek int part = DISKPART(dev);
372 1.8 sommerfe
373 1.23 thorpej ATADEBUG_PRINT(("edmcaclose\n"), DEBUG_FUNCS);
374 1.25 thorpej
375 1.36 ad mutex_enter(&wd->sc_dk.dk_openlock);
376 1.1 jdolecek
377 1.1 jdolecek switch (fmt) {
378 1.1 jdolecek case S_IFCHR:
379 1.1 jdolecek wd->sc_dk.dk_copenmask &= ~(1 << part);
380 1.1 jdolecek break;
381 1.1 jdolecek case S_IFBLK:
382 1.1 jdolecek wd->sc_dk.dk_bopenmask &= ~(1 << part);
383 1.1 jdolecek break;
384 1.1 jdolecek }
385 1.1 jdolecek wd->sc_dk.dk_openmask =
386 1.1 jdolecek wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
387 1.1 jdolecek
388 1.1 jdolecek if (wd->sc_dk.dk_openmask == 0) {
389 1.1 jdolecek #if 0
390 1.1 jdolecek wd_flushcache(wd, AT_WAIT);
391 1.1 jdolecek #endif
392 1.1 jdolecek /* XXXX Must wait for I/O to complete! */
393 1.1 jdolecek
394 1.1 jdolecek if (! (wd->sc_flags & WDF_KLABEL))
395 1.1 jdolecek wd->sc_flags &= ~WDF_LOADED;
396 1.1 jdolecek }
397 1.1 jdolecek
398 1.36 ad mutex_exit(&wd->sc_dk.dk_openlock);
399 1.1 jdolecek
400 1.1 jdolecek return 0;
401 1.1 jdolecek }
402 1.1 jdolecek
403 1.1 jdolecek static void
404 1.44 dsl edgetdefaultlabel(struct ed_softc *ed, struct disklabel *lp)
405 1.1 jdolecek {
406 1.23 thorpej ATADEBUG_PRINT(("edgetdefaultlabel\n"), DEBUG_FUNCS);
407 1.1 jdolecek memset(lp, 0, sizeof(struct disklabel));
408 1.1 jdolecek
409 1.1 jdolecek lp->d_secsize = DEV_BSIZE;
410 1.10 jdolecek lp->d_ntracks = ed->heads;
411 1.10 jdolecek lp->d_nsectors = ed->sectors;
412 1.10 jdolecek lp->d_ncylinders = ed->cyl;
413 1.1 jdolecek lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
414 1.1 jdolecek
415 1.62 christos lp->d_type = DKTYPE_ESDI;
416 1.1 jdolecek
417 1.1 jdolecek strncpy(lp->d_typename, "ESDI", 16);
418 1.1 jdolecek strncpy(lp->d_packname, "fictitious", 16);
419 1.10 jdolecek lp->d_secperunit = ed->sc_capacity;
420 1.1 jdolecek lp->d_rpm = 3600;
421 1.1 jdolecek lp->d_interleave = 1;
422 1.1 jdolecek lp->d_flags = 0;
423 1.1 jdolecek
424 1.1 jdolecek lp->d_partitions[RAW_PART].p_offset = 0;
425 1.1 jdolecek lp->d_partitions[RAW_PART].p_size =
426 1.1 jdolecek lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
427 1.1 jdolecek lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
428 1.1 jdolecek lp->d_npartitions = RAW_PART + 1;
429 1.1 jdolecek
430 1.1 jdolecek lp->d_magic = DISKMAGIC;
431 1.1 jdolecek lp->d_magic2 = DISKMAGIC;
432 1.1 jdolecek lp->d_checksum = dkcksum(lp);
433 1.1 jdolecek }
434 1.1 jdolecek
435 1.1 jdolecek /*
436 1.1 jdolecek * Fabricate a default disk label, and try to read the correct one.
437 1.1 jdolecek */
438 1.1 jdolecek static void
439 1.44 dsl edgetdisklabel(dev_t dev, struct ed_softc *ed)
440 1.1 jdolecek {
441 1.10 jdolecek struct disklabel *lp = ed->sc_dk.dk_label;
442 1.19 dsl const char *errstring;
443 1.1 jdolecek
444 1.23 thorpej ATADEBUG_PRINT(("edgetdisklabel\n"), DEBUG_FUNCS);
445 1.1 jdolecek
446 1.10 jdolecek memset(ed->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
447 1.1 jdolecek
448 1.10 jdolecek edgetdefaultlabel(ed, lp);
449 1.1 jdolecek
450 1.10 jdolecek errstring = readdisklabel(
451 1.10 jdolecek EDLABELDEV(dev), edmcastrategy, lp, ed->sc_dk.dk_cpulabel);
452 1.1 jdolecek if (errstring) {
453 1.1 jdolecek /*
454 1.1 jdolecek * This probably happened because the drive's default
455 1.1 jdolecek * geometry doesn't match the DOS geometry. We
456 1.1 jdolecek * assume the DOS geometry is now in the label and try
457 1.1 jdolecek * again. XXX This is a kluge.
458 1.1 jdolecek */
459 1.1 jdolecek #if 0
460 1.1 jdolecek if (wd->drvp->state > RECAL)
461 1.52 bouyer wd->drvp->drive_flags |= ATA_DRIVE_RESET;
462 1.1 jdolecek #endif
463 1.10 jdolecek errstring = readdisklabel(EDLABELDEV(dev),
464 1.10 jdolecek edmcastrategy, lp, ed->sc_dk.dk_cpulabel);
465 1.1 jdolecek }
466 1.1 jdolecek if (errstring) {
467 1.53 chs printf("%s: %s\n", device_xname(ed->sc_dev), errstring);
468 1.1 jdolecek return;
469 1.1 jdolecek }
470 1.1 jdolecek }
471 1.1 jdolecek
472 1.1 jdolecek int
473 1.44 dsl edmcaioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
474 1.1 jdolecek {
475 1.42 tsutsui struct ed_softc *ed = device_lookup_private(&ed_cd, DISKUNIT(dev));
476 1.1 jdolecek int error;
477 1.1 jdolecek
478 1.23 thorpej ATADEBUG_PRINT(("edioctl\n"), DEBUG_FUNCS);
479 1.1 jdolecek
480 1.10 jdolecek if ((ed->sc_flags & WDF_LOADED) == 0)
481 1.1 jdolecek return EIO;
482 1.1 jdolecek
483 1.61 christos error = disk_ioctl(&ed->sc_dk, dev, xfer, addr, flag, l);
484 1.60 christos if (error != EPASSTHROUGH)
485 1.60 christos return error;
486 1.60 christos
487 1.1 jdolecek switch (xfer) {
488 1.1 jdolecek case DIOCWDINFO:
489 1.1 jdolecek case DIOCSDINFO:
490 1.1 jdolecek {
491 1.1 jdolecek struct disklabel *lp;
492 1.1 jdolecek
493 1.1 jdolecek lp = (struct disklabel *)addr;
494 1.1 jdolecek
495 1.1 jdolecek if ((flag & FWRITE) == 0)
496 1.1 jdolecek return EBADF;
497 1.1 jdolecek
498 1.36 ad mutex_enter(&ed->sc_dk.dk_openlock);
499 1.10 jdolecek ed->sc_flags |= WDF_LABELLING;
500 1.1 jdolecek
501 1.10 jdolecek error = setdisklabel(ed->sc_dk.dk_label,
502 1.1 jdolecek lp, /*wd->sc_dk.dk_openmask : */0,
503 1.10 jdolecek ed->sc_dk.dk_cpulabel);
504 1.1 jdolecek if (error == 0) {
505 1.1 jdolecek #if 0
506 1.1 jdolecek if (wd->drvp->state > RECAL)
507 1.52 bouyer wd->drvp->drive_flags |= ATA_DRIVE_RESET;
508 1.1 jdolecek #endif
509 1.10 jdolecek if (xfer == DIOCWDINFO)
510 1.1 jdolecek error = writedisklabel(EDLABELDEV(dev),
511 1.10 jdolecek edmcastrategy, ed->sc_dk.dk_label,
512 1.10 jdolecek ed->sc_dk.dk_cpulabel);
513 1.1 jdolecek }
514 1.1 jdolecek
515 1.10 jdolecek ed->sc_flags &= ~WDF_LABELLING;
516 1.36 ad mutex_exit(&ed->sc_dk.dk_openlock);
517 1.10 jdolecek return (error);
518 1.1 jdolecek }
519 1.1 jdolecek
520 1.1 jdolecek case DIOCKLABEL:
521 1.1 jdolecek if (*(int *)addr)
522 1.10 jdolecek ed->sc_flags |= WDF_KLABEL;
523 1.1 jdolecek else
524 1.10 jdolecek ed->sc_flags &= ~WDF_KLABEL;
525 1.1 jdolecek return 0;
526 1.8 sommerfe
527 1.1 jdolecek case DIOCWLABEL:
528 1.1 jdolecek if ((flag & FWRITE) == 0)
529 1.1 jdolecek return EBADF;
530 1.1 jdolecek if (*(int *)addr)
531 1.10 jdolecek ed->sc_flags |= WDF_WLABEL;
532 1.1 jdolecek else
533 1.10 jdolecek ed->sc_flags &= ~WDF_WLABEL;
534 1.1 jdolecek return 0;
535 1.1 jdolecek
536 1.1 jdolecek case DIOCGDEFLABEL:
537 1.10 jdolecek edgetdefaultlabel(ed, (struct disklabel *)addr);
538 1.1 jdolecek return 0;
539 1.1 jdolecek
540 1.10 jdolecek #if 0
541 1.1 jdolecek case DIOCWFORMAT:
542 1.1 jdolecek if ((flag & FWRITE) == 0)
543 1.1 jdolecek return EBADF;
544 1.1 jdolecek {
545 1.1 jdolecek register struct format_op *fop;
546 1.1 jdolecek struct iovec aiov;
547 1.1 jdolecek struct uio auio;
548 1.8 sommerfe
549 1.1 jdolecek fop = (struct format_op *)addr;
550 1.1 jdolecek aiov.iov_base = fop->df_buf;
551 1.1 jdolecek aiov.iov_len = fop->df_count;
552 1.1 jdolecek auio.uio_iov = &aiov;
553 1.1 jdolecek auio.uio_iovcnt = 1;
554 1.1 jdolecek auio.uio_resid = fop->df_count;
555 1.1 jdolecek auio.uio_segflg = 0;
556 1.1 jdolecek auio.uio_offset =
557 1.1 jdolecek fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
558 1.31 christos auio.uio_lwp = l;
559 1.1 jdolecek error = physio(wdformat, NULL, dev, B_WRITE, minphys,
560 1.1 jdolecek &auio);
561 1.1 jdolecek fop->df_count -= auio.uio_resid;
562 1.1 jdolecek fop->df_reg[0] = wdc->sc_status;
563 1.1 jdolecek fop->df_reg[1] = wdc->sc_error;
564 1.1 jdolecek return error;
565 1.1 jdolecek }
566 1.1 jdolecek #endif
567 1.1 jdolecek
568 1.1 jdolecek default:
569 1.1 jdolecek return ENOTTY;
570 1.1 jdolecek }
571 1.1 jdolecek
572 1.1 jdolecek #ifdef DIAGNOSTIC
573 1.3 jdolecek panic("edioctl: impossible");
574 1.1 jdolecek #endif
575 1.1 jdolecek }
576 1.1 jdolecek
577 1.1 jdolecek int
578 1.44 dsl edmcasize(dev_t dev)
579 1.1 jdolecek {
580 1.1 jdolecek struct ed_softc *wd;
581 1.1 jdolecek int part, omask;
582 1.1 jdolecek int size;
583 1.1 jdolecek
584 1.23 thorpej ATADEBUG_PRINT(("edsize\n"), DEBUG_FUNCS);
585 1.1 jdolecek
586 1.42 tsutsui wd = device_lookup_private(&ed_cd, DISKUNIT(dev));
587 1.1 jdolecek if (wd == NULL)
588 1.1 jdolecek return (-1);
589 1.1 jdolecek
590 1.1 jdolecek part = DISKPART(dev);
591 1.1 jdolecek omask = wd->sc_dk.dk_openmask & (1 << part);
592 1.1 jdolecek
593 1.1 jdolecek if (omask == 0 && edmcaopen(dev, 0, S_IFBLK, NULL) != 0)
594 1.1 jdolecek return (-1);
595 1.1 jdolecek if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
596 1.1 jdolecek size = -1;
597 1.1 jdolecek else
598 1.1 jdolecek size = wd->sc_dk.dk_label->d_partitions[part].p_size *
599 1.1 jdolecek (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
600 1.1 jdolecek if (omask == 0 && edmcaclose(dev, 0, S_IFBLK, NULL) != 0)
601 1.1 jdolecek return (-1);
602 1.1 jdolecek return (size);
603 1.1 jdolecek }
604 1.1 jdolecek
605 1.1 jdolecek /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
606 1.6 jdolecek static int eddoingadump = 0;
607 1.6 jdolecek static int eddumprecalibrated = 0;
608 1.6 jdolecek static int eddumpmulti = 1;
609 1.1 jdolecek
610 1.1 jdolecek /*
611 1.1 jdolecek * Dump core after a system crash.
612 1.1 jdolecek */
613 1.1 jdolecek int
614 1.44 dsl edmcadump(dev_t dev, daddr_t blkno, void *va, size_t size)
615 1.1 jdolecek {
616 1.6 jdolecek struct ed_softc *ed; /* disk unit to do the I/O */
617 1.1 jdolecek struct disklabel *lp; /* disk's disklabel */
618 1.7 jdolecek int part;
619 1.1 jdolecek int nblks; /* total number of sectors left to write */
620 1.10 jdolecek int error;
621 1.1 jdolecek
622 1.1 jdolecek /* Check if recursive dump; if so, punt. */
623 1.6 jdolecek if (eddoingadump)
624 1.1 jdolecek return EFAULT;
625 1.6 jdolecek eddoingadump = 1;
626 1.1 jdolecek
627 1.42 tsutsui ed = device_lookup_private(&ed_cd, DISKUNIT(dev));
628 1.6 jdolecek if (ed == NULL)
629 1.1 jdolecek return (ENXIO);
630 1.1 jdolecek
631 1.1 jdolecek part = DISKPART(dev);
632 1.1 jdolecek
633 1.1 jdolecek /* Make sure it was initialized. */
634 1.6 jdolecek if ((ed->sc_flags & EDF_INIT) == 0)
635 1.1 jdolecek return ENXIO;
636 1.1 jdolecek
637 1.1 jdolecek /* Convert to disk sectors. Request must be a multiple of size. */
638 1.6 jdolecek lp = ed->sc_dk.dk_label;
639 1.1 jdolecek if ((size % lp->d_secsize) != 0)
640 1.1 jdolecek return EFAULT;
641 1.1 jdolecek nblks = size / lp->d_secsize;
642 1.1 jdolecek blkno = blkno / (lp->d_secsize / DEV_BSIZE);
643 1.1 jdolecek
644 1.1 jdolecek /* Check transfer bounds against partition size. */
645 1.1 jdolecek if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
646 1.8 sommerfe return EINVAL;
647 1.1 jdolecek
648 1.1 jdolecek /* Offset block number to start of partition. */
649 1.1 jdolecek blkno += lp->d_partitions[part].p_offset;
650 1.1 jdolecek
651 1.1 jdolecek /* Recalibrate, if first dump transfer. */
652 1.6 jdolecek if (eddumprecalibrated == 0) {
653 1.6 jdolecek eddumprecalibrated = 1;
654 1.6 jdolecek eddumpmulti = 8;
655 1.1 jdolecek #if 0
656 1.1 jdolecek wd->drvp->state = RESET;
657 1.1 jdolecek #endif
658 1.1 jdolecek }
659 1.8 sommerfe
660 1.1 jdolecek while (nblks > 0) {
661 1.10 jdolecek error = edc_bio(ed->edc_softc, ed, va, blkno,
662 1.66.16.1 pgoyette uimin(nblks, eddumpmulti) * lp->d_secsize, 0, 1);
663 1.10 jdolecek if (error)
664 1.10 jdolecek return (error);
665 1.1 jdolecek
666 1.1 jdolecek /* update block count */
667 1.66.16.1 pgoyette nblks -= uimin(nblks, eddumpmulti);
668 1.66.16.1 pgoyette blkno += uimin(nblks, eddumpmulti);
669 1.66.16.1 pgoyette va = (char *)va + uimin(nblks, eddumpmulti) * lp->d_secsize;
670 1.1 jdolecek }
671 1.1 jdolecek
672 1.6 jdolecek eddoingadump = 0;
673 1.6 jdolecek return (0);
674 1.1 jdolecek }
675 1.1 jdolecek
676 1.1 jdolecek static int
677 1.44 dsl ed_get_params(struct ed_softc *ed, int *drv_flags)
678 1.1 jdolecek {
679 1.1 jdolecek u_int16_t cmd_args[2];
680 1.1 jdolecek
681 1.1 jdolecek /*
682 1.1 jdolecek * Get Device Configuration (09).
683 1.1 jdolecek */
684 1.5 jdolecek cmd_args[0] = 14; /* Options: 00s110, s: 0=Physical 1=Pseudo */
685 1.1 jdolecek cmd_args[1] = 0;
686 1.6 jdolecek if (edc_run_cmd(ed->edc_softc, CMD_GET_DEV_CONF, ed->sc_devno,
687 1.10 jdolecek cmd_args, 2, 1))
688 1.1 jdolecek return (1);
689 1.1 jdolecek
690 1.10 jdolecek ed->spares = ed->sense_data[1] >> 8;
691 1.10 jdolecek if (drv_flags)
692 1.10 jdolecek *drv_flags = ed->sense_data[1] & 0x1f;
693 1.10 jdolecek ed->rba = ed->sense_data[2] | (ed->sense_data[3] << 16);
694 1.1 jdolecek /* Instead of using:
695 1.10 jdolecek ed->cyl = ed->sense_data[4];
696 1.10 jdolecek ed->heads = ed->sense_data[5] & 0xff;
697 1.10 jdolecek ed->sectors = ed->sense_data[5] >> 8;
698 1.1 jdolecek * we fabricate the numbers from RBA count, so that
699 1.1 jdolecek * number of sectors is 32 and heads 64. This seems
700 1.1 jdolecek * to be necessary for integrated ESDI controller.
701 1.1 jdolecek */
702 1.1 jdolecek ed->sectors = 32;
703 1.1 jdolecek ed->heads = 64;
704 1.1 jdolecek ed->cyl = ed->rba / (ed->heads * ed->sectors);
705 1.1 jdolecek ed->sc_capacity = ed->rba;
706 1.1 jdolecek
707 1.1 jdolecek return (0);
708 1.1 jdolecek }
709