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