ld.c revision 1.94.2.7 1 1.94.2.7 pgoyette /* $NetBSD: ld.c,v 1.94.2.7 2017/01/07 08:56:31 pgoyette Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran and Charles M. Hannum.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.1 ad /*
33 1.1 ad * Disk driver for use by RAID controllers.
34 1.1 ad */
35 1.12 lukem
36 1.12 lukem #include <sys/cdefs.h>
37 1.94.2.7 pgoyette __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.94.2.7 2017/01/07 08:56:31 pgoyette Exp $");
38 1.1 ad
39 1.1 ad #include <sys/param.h>
40 1.1 ad #include <sys/systm.h>
41 1.1 ad #include <sys/kernel.h>
42 1.1 ad #include <sys/device.h>
43 1.1 ad #include <sys/queue.h>
44 1.1 ad #include <sys/proc.h>
45 1.1 ad #include <sys/buf.h>
46 1.33 yamt #include <sys/bufq.h>
47 1.1 ad #include <sys/endian.h>
48 1.1 ad #include <sys/disklabel.h>
49 1.1 ad #include <sys/disk.h>
50 1.1 ad #include <sys/dkio.h>
51 1.1 ad #include <sys/stat.h>
52 1.1 ad #include <sys/conf.h>
53 1.1 ad #include <sys/fcntl.h>
54 1.2 ad #include <sys/vnode.h>
55 1.1 ad #include <sys/syslog.h>
56 1.44 ad #include <sys/mutex.h>
57 1.94.2.1 pgoyette #include <sys/localcount.h>
58 1.94.2.6 pgoyette #include <sys/module.h>
59 1.94.2.6 pgoyette #include <sys/reboot.h>
60 1.1 ad
61 1.1 ad #include <dev/ldvar.h>
62 1.1 ad
63 1.1 ad static void ldminphys(struct buf *bp);
64 1.67 jmcneill static bool ld_suspend(device_t, const pmf_qual_t *);
65 1.55 jmcneill static bool ld_shutdown(device_t, int);
66 1.89 mlelstv static int ld_diskstart(device_t, struct buf *bp);
67 1.83 mlelstv static void ld_iosize(device_t, int *);
68 1.83 mlelstv static int ld_dumpblocks(device_t, void *, daddr_t, int);
69 1.83 mlelstv static void ld_fake_geometry(struct ld_softc *);
70 1.71 christos static void ld_set_geometry(struct ld_softc *);
71 1.65 cegger static void ld_config_interrupts (device_t);
72 1.83 mlelstv static int ld_lastclose(device_t);
73 1.90 jakllsch static int ld_discard(device_t, off_t, off_t);
74 1.1 ad
75 1.1 ad extern struct cfdriver ld_cd;
76 1.1 ad
77 1.29 thorpej static dev_type_open(ldopen);
78 1.29 thorpej static dev_type_close(ldclose);
79 1.29 thorpej static dev_type_read(ldread);
80 1.29 thorpej static dev_type_write(ldwrite);
81 1.29 thorpej static dev_type_ioctl(ldioctl);
82 1.29 thorpej static dev_type_strategy(ldstrategy);
83 1.29 thorpej static dev_type_dump(lddump);
84 1.29 thorpej static dev_type_size(ldsize);
85 1.90 jakllsch static dev_type_discard(lddiscard);
86 1.16 gehenna
87 1.16 gehenna const struct bdevsw ld_bdevsw = {
88 1.94.2.3 pgoyette DEVSW_MODULE_INIT
89 1.72 dholland .d_open = ldopen,
90 1.72 dholland .d_close = ldclose,
91 1.72 dholland .d_strategy = ldstrategy,
92 1.72 dholland .d_ioctl = ldioctl,
93 1.72 dholland .d_dump = lddump,
94 1.72 dholland .d_psize = ldsize,
95 1.90 jakllsch .d_discard = lddiscard,
96 1.89 mlelstv .d_flag = D_DISK | D_MPSAFE
97 1.16 gehenna };
98 1.16 gehenna
99 1.16 gehenna const struct cdevsw ld_cdevsw = {
100 1.94.2.3 pgoyette DEVSW_MODULE_INIT
101 1.72 dholland .d_open = ldopen,
102 1.72 dholland .d_close = ldclose,
103 1.72 dholland .d_read = ldread,
104 1.72 dholland .d_write = ldwrite,
105 1.72 dholland .d_ioctl = ldioctl,
106 1.72 dholland .d_stop = nostop,
107 1.72 dholland .d_tty = notty,
108 1.72 dholland .d_poll = nopoll,
109 1.72 dholland .d_mmap = nommap,
110 1.72 dholland .d_kqfilter = nokqfilter,
111 1.90 jakllsch .d_discard = lddiscard,
112 1.89 mlelstv .d_flag = D_DISK | D_MPSAFE
113 1.16 gehenna };
114 1.16 gehenna
115 1.83 mlelstv static struct dkdriver lddkdriver = {
116 1.83 mlelstv .d_open = ldopen,
117 1.83 mlelstv .d_close = ldclose,
118 1.83 mlelstv .d_strategy = ldstrategy,
119 1.83 mlelstv .d_iosize = ld_iosize,
120 1.83 mlelstv .d_minphys = ldminphys,
121 1.89 mlelstv .d_diskstart = ld_diskstart,
122 1.83 mlelstv .d_dumpblocks = ld_dumpblocks,
123 1.90 jakllsch .d_lastclose = ld_lastclose,
124 1.90 jakllsch .d_discard = ld_discard
125 1.83 mlelstv };
126 1.1 ad
127 1.1 ad void
128 1.94.2.6 pgoyette ldattach(struct ld_softc *sc, const char *default_strategy)
129 1.1 ad {
130 1.83 mlelstv device_t self = sc->sc_dv;
131 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
132 1.1 ad
133 1.53 ad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
134 1.85 mlelstv cv_init(&sc->sc_drain, "lddrain");
135 1.44 ad
136 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0) {
137 1.7 ad return;
138 1.7 ad }
139 1.7 ad
140 1.83 mlelstv /* Initialise dk and disk structure. */
141 1.83 mlelstv dk_init(dksc, self, DKTYPE_LD);
142 1.83 mlelstv disk_init(&dksc->sc_dkdev, dksc->sc_xname, &lddkdriver);
143 1.83 mlelstv
144 1.1 ad if (sc->sc_maxxfer > MAXPHYS)
145 1.1 ad sc->sc_maxxfer = MAXPHYS;
146 1.9 ad
147 1.19 thorpej /* Build synthetic geometry if necessary. */
148 1.19 thorpej if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
149 1.83 mlelstv sc->sc_ncylinders == 0)
150 1.83 mlelstv ld_fake_geometry(sc);
151 1.19 thorpej
152 1.68 kiyohara sc->sc_disksize512 = sc->sc_secperunit * sc->sc_secsize / DEV_BSIZE;
153 1.1 ad
154 1.83 mlelstv /* Attach dk and disk subsystems */
155 1.83 mlelstv dk_attach(dksc);
156 1.83 mlelstv disk_attach(&dksc->sc_dkdev);
157 1.71 christos ld_set_geometry(sc);
158 1.43 riz
159 1.94.2.6 pgoyette bufq_alloc(&dksc->sc_bufq, default_strategy, BUFQ_SORT_RAWBLOCK);
160 1.1 ad
161 1.55 jmcneill /* Register with PMF */
162 1.83 mlelstv if (!pmf_device_register1(dksc->sc_dev, ld_suspend, NULL, ld_shutdown))
163 1.83 mlelstv aprint_error_dev(dksc->sc_dev,
164 1.55 jmcneill "couldn't establish power handler\n");
165 1.55 jmcneill
166 1.30 thorpej /* Discover wedges on this disk. */
167 1.63 tron config_interrupts(sc->sc_dv, ld_config_interrupts);
168 1.1 ad }
169 1.1 ad
170 1.3 ad int
171 1.37 christos ldadjqparam(struct ld_softc *sc, int xmax)
172 1.3 ad {
173 1.7 ad
174 1.85 mlelstv mutex_enter(&sc->sc_mutex);
175 1.37 christos sc->sc_maxqueuecnt = xmax;
176 1.85 mlelstv mutex_exit(&sc->sc_mutex);
177 1.7 ad
178 1.24 thorpej return (0);
179 1.7 ad }
180 1.7 ad
181 1.7 ad int
182 1.7 ad ldbegindetach(struct ld_softc *sc, int flags)
183 1.7 ad {
184 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
185 1.85 mlelstv int rv = 0;
186 1.7 ad
187 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
188 1.7 ad return (0);
189 1.3 ad
190 1.83 mlelstv rv = disk_begindetach(&dksc->sc_dkdev, ld_lastclose, dksc->sc_dev, flags);
191 1.66 dyoung
192 1.66 dyoung if (rv != 0)
193 1.66 dyoung return rv;
194 1.3 ad
195 1.85 mlelstv mutex_enter(&sc->sc_mutex);
196 1.24 thorpej sc->sc_maxqueuecnt = 0;
197 1.83 mlelstv
198 1.24 thorpej while (sc->sc_queuecnt > 0) {
199 1.24 thorpej sc->sc_flags |= LDF_DRAIN;
200 1.85 mlelstv cv_wait(&sc->sc_drain, &sc->sc_mutex);
201 1.24 thorpej }
202 1.85 mlelstv mutex_exit(&sc->sc_mutex);
203 1.7 ad
204 1.7 ad return (rv);
205 1.3 ad }
206 1.3 ad
207 1.2 ad void
208 1.7 ad ldenddetach(struct ld_softc *sc)
209 1.2 ad {
210 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
211 1.85 mlelstv int bmaj, cmaj, i, mn;
212 1.2 ad
213 1.7 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
214 1.7 ad return;
215 1.7 ad
216 1.85 mlelstv mutex_enter(&sc->sc_mutex);
217 1.85 mlelstv
218 1.2 ad /* Wait for commands queued with the hardware to complete. */
219 1.86 mlelstv if (sc->sc_queuecnt != 0) {
220 1.86 mlelstv if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz))
221 1.83 mlelstv printf("%s: not drained\n", dksc->sc_xname);
222 1.86 mlelstv }
223 1.92 mlelstv mutex_exit(&sc->sc_mutex);
224 1.2 ad
225 1.2 ad /* Kill off any queued buffers. */
226 1.92 mlelstv dk_drain(dksc);
227 1.83 mlelstv bufq_free(dksc->sc_bufq);
228 1.2 ad
229 1.85 mlelstv /* Locate the major numbers. */
230 1.85 mlelstv bmaj = bdevsw_lookup_major(&ld_bdevsw);
231 1.85 mlelstv cmaj = cdevsw_lookup_major(&ld_cdevsw);
232 1.85 mlelstv
233 1.2 ad /* Nuke the vnodes for any open instances. */
234 1.13 drochner for (i = 0; i < MAXPARTITIONS; i++) {
235 1.83 mlelstv mn = DISKMINOR(device_unit(dksc->sc_dev), i);
236 1.13 drochner vdevgone(bmaj, mn, mn, VBLK);
237 1.13 drochner vdevgone(cmaj, mn, mn, VCHR);
238 1.13 drochner }
239 1.13 drochner
240 1.30 thorpej /* Delete all of our wedges. */
241 1.83 mlelstv dkwedge_delall(&dksc->sc_dkdev);
242 1.30 thorpej
243 1.2 ad /* Detach from the disk list. */
244 1.83 mlelstv disk_detach(&dksc->sc_dkdev);
245 1.83 mlelstv disk_destroy(&dksc->sc_dkdev);
246 1.2 ad
247 1.92 mlelstv dk_detach(dksc);
248 1.92 mlelstv
249 1.56 jmcneill /* Deregister with PMF */
250 1.83 mlelstv pmf_device_deregister(dksc->sc_dev);
251 1.56 jmcneill
252 1.24 thorpej /*
253 1.24 thorpej * XXX We can't really flush the cache here, beceause the
254 1.24 thorpej * XXX device may already be non-existent from the controller's
255 1.24 thorpej * XXX perspective.
256 1.24 thorpej */
257 1.24 thorpej #if 0
258 1.2 ad /* Flush the device's cache. */
259 1.2 ad if (sc->sc_flush != NULL)
260 1.62 simonb if ((*sc->sc_flush)(sc, 0) != 0)
261 1.87 mlelstv device_printf(dksc->sc_dev, "unable to flush cache\n");
262 1.24 thorpej #endif
263 1.85 mlelstv cv_destroy(&sc->sc_drain);
264 1.61 ws mutex_destroy(&sc->sc_mutex);
265 1.2 ad }
266 1.2 ad
267 1.8 lukem /* ARGSUSED */
268 1.55 jmcneill static bool
269 1.67 jmcneill ld_suspend(device_t dev, const pmf_qual_t *qual)
270 1.67 jmcneill {
271 1.67 jmcneill return ld_shutdown(dev, 0);
272 1.67 jmcneill }
273 1.67 jmcneill
274 1.67 jmcneill /* ARGSUSED */
275 1.67 jmcneill static bool
276 1.55 jmcneill ld_shutdown(device_t dev, int flags)
277 1.1 ad {
278 1.55 jmcneill struct ld_softc *sc = device_private(dev);
279 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
280 1.1 ad
281 1.94.2.6 pgoyette if ((flags & RB_NOSYNC) == 0 && sc->sc_flush != NULL
282 1.94.2.6 pgoyette && (*sc->sc_flush)(sc, LDFL_POLL) != 0) {
283 1.87 mlelstv device_printf(dksc->sc_dev, "unable to flush cache\n");
284 1.55 jmcneill return false;
285 1.1 ad }
286 1.55 jmcneill
287 1.55 jmcneill return true;
288 1.1 ad }
289 1.1 ad
290 1.8 lukem /* ARGSUSED */
291 1.29 thorpej static int
292 1.42 christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
293 1.1 ad {
294 1.94.2.4 pgoyette device_t self;
295 1.1 ad struct ld_softc *sc;
296 1.83 mlelstv struct dk_softc *dksc;
297 1.83 mlelstv int unit;
298 1.94.2.4 pgoyette int error;
299 1.1 ad
300 1.1 ad unit = DISKUNIT(dev);
301 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
302 1.94.2.4 pgoyette if (self == NULL)
303 1.94.2.4 pgoyette return ENXIO;
304 1.94.2.4 pgoyette sc = device_private(self);
305 1.83 mlelstv dksc = &sc->sc_dksc;
306 1.1 ad
307 1.94.2.4 pgoyette error = dk_open(dksc, dev, flags, fmt, l);
308 1.94.2.4 pgoyette device_release(self);
309 1.94.2.4 pgoyette return error;
310 1.1 ad }
311 1.1 ad
312 1.66 dyoung static int
313 1.83 mlelstv ld_lastclose(device_t self)
314 1.66 dyoung {
315 1.66 dyoung struct ld_softc *sc = device_private(self);
316 1.84 skrll
317 1.66 dyoung if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0)
318 1.87 mlelstv device_printf(self, "unable to flush cache\n");
319 1.84 skrll
320 1.66 dyoung return 0;
321 1.84 skrll }
322 1.66 dyoung
323 1.8 lukem /* ARGSUSED */
324 1.29 thorpej static int
325 1.42 christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
326 1.1 ad {
327 1.94.2.4 pgoyette device_t self;
328 1.1 ad struct ld_softc *sc;
329 1.83 mlelstv struct dk_softc *dksc;
330 1.83 mlelstv int unit;
331 1.94.2.4 pgoyette int error;
332 1.1 ad
333 1.1 ad unit = DISKUNIT(dev);
334 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
335 1.94.2.4 pgoyette if (self == NULL)
336 1.94.2.4 pgoyette return ENXIO;
337 1.94.2.4 pgoyette sc = device_private(self);
338 1.83 mlelstv dksc = &sc->sc_dksc;
339 1.30 thorpej
340 1.94.2.4 pgoyette error = dk_close(dksc, dev, flags, fmt, l);
341 1.94.2.4 pgoyette device_release(self);
342 1.94.2.4 pgoyette return error;
343 1.1 ad }
344 1.1 ad
345 1.8 lukem /* ARGSUSED */
346 1.29 thorpej static int
347 1.42 christos ldread(dev_t dev, struct uio *uio, int ioflag)
348 1.1 ad {
349 1.1 ad
350 1.1 ad return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
351 1.1 ad }
352 1.1 ad
353 1.8 lukem /* ARGSUSED */
354 1.29 thorpej static int
355 1.42 christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
356 1.1 ad {
357 1.1 ad
358 1.1 ad return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
359 1.1 ad }
360 1.1 ad
361 1.8 lukem /* ARGSUSED */
362 1.29 thorpej static int
363 1.46 christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
364 1.1 ad {
365 1.94.2.4 pgoyette device_t self;
366 1.1 ad struct ld_softc *sc;
367 1.83 mlelstv struct dk_softc *dksc;
368 1.80 christos int unit, error;
369 1.1 ad
370 1.1 ad unit = DISKUNIT(dev);
371 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
372 1.94.2.4 pgoyette if (self == NULL)
373 1.94.2.4 pgoyette return ENXIO;
374 1.94.2.4 pgoyette sc = device_private(self);
375 1.83 mlelstv dksc = &sc->sc_dksc;
376 1.1 ad
377 1.83 mlelstv error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
378 1.94.2.4 pgoyette if (error != EPASSTHROUGH) {
379 1.94.2.4 pgoyette device_release(self);
380 1.43 riz return (error);
381 1.94.2.4 pgoyette }
382 1.43 riz
383 1.47 tron error = 0;
384 1.83 mlelstv
385 1.1 ad switch (cmd) {
386 1.32 thorpej case DIOCCACHESYNC:
387 1.32 thorpej /*
388 1.32 thorpej * XXX Do we really need to care about having a writable
389 1.32 thorpej * file descriptor here?
390 1.32 thorpej */
391 1.32 thorpej if ((flag & FWRITE) == 0)
392 1.32 thorpej error = EBADF;
393 1.32 thorpej else if (sc->sc_flush)
394 1.62 simonb error = (*sc->sc_flush)(sc, 0);
395 1.32 thorpej else
396 1.32 thorpej error = 0; /* XXX Error out instead? */
397 1.32 thorpej break;
398 1.94.2.6 pgoyette
399 1.1 ad default:
400 1.94.2.6 pgoyette error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
401 1.1 ad break;
402 1.1 ad }
403 1.1 ad
404 1.94.2.4 pgoyette device_release(self);
405 1.1 ad return (error);
406 1.1 ad }
407 1.1 ad
408 1.29 thorpej static void
409 1.1 ad ldstrategy(struct buf *bp)
410 1.1 ad {
411 1.94.2.4 pgoyette device_t self;
412 1.1 ad struct ld_softc *sc;
413 1.83 mlelstv struct dk_softc *dksc;
414 1.83 mlelstv int unit;
415 1.2 ad
416 1.83 mlelstv unit = DISKUNIT(bp->b_dev);
417 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
418 1.94.2.4 pgoyette if (self == NULL)
419 1.94.2.4 pgoyette return;
420 1.94.2.4 pgoyette sc = device_private(self);
421 1.83 mlelstv dksc = &sc->sc_dksc;
422 1.23 thorpej
423 1.94 mlelstv dk_strategy(dksc, bp);
424 1.94.2.4 pgoyette device_release(self);
425 1.23 thorpej }
426 1.23 thorpej
427 1.89 mlelstv static int
428 1.89 mlelstv ld_diskstart(device_t dev, struct buf *bp)
429 1.23 thorpej {
430 1.94.2.4 pgoyette struct ld_softc *sc;
431 1.23 thorpej int error;
432 1.1 ad
433 1.94.2.4 pgoyette device_acquire(dev);
434 1.94.2.4 pgoyette sc = device_private(dev);
435 1.94.2.4 pgoyette if (sc->sc_queuecnt >= sc->sc_maxqueuecnt) {
436 1.94.2.4 pgoyette device_release(dev);
437 1.89 mlelstv return EAGAIN;
438 1.94.2.4 pgoyette }
439 1.89 mlelstv
440 1.44 ad mutex_enter(&sc->sc_mutex);
441 1.44 ad
442 1.89 mlelstv if (sc->sc_queuecnt >= sc->sc_maxqueuecnt)
443 1.89 mlelstv error = EAGAIN;
444 1.89 mlelstv else {
445 1.89 mlelstv error = (*sc->sc_start)(sc, bp);
446 1.89 mlelstv if (error == 0)
447 1.89 mlelstv sc->sc_queuecnt++;
448 1.1 ad }
449 1.44 ad
450 1.44 ad mutex_exit(&sc->sc_mutex);
451 1.89 mlelstv
452 1.94.2.4 pgoyette device_release(dev);
453 1.89 mlelstv return error;
454 1.1 ad }
455 1.1 ad
456 1.1 ad void
457 1.1 ad lddone(struct ld_softc *sc, struct buf *bp)
458 1.1 ad {
459 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
460 1.1 ad
461 1.83 mlelstv dk_done(dksc, bp);
462 1.1 ad
463 1.44 ad mutex_enter(&sc->sc_mutex);
464 1.7 ad if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
465 1.24 thorpej if ((sc->sc_flags & LDF_DRAIN) != 0) {
466 1.24 thorpej sc->sc_flags &= ~LDF_DRAIN;
467 1.88 mlelstv cv_broadcast(&sc->sc_drain);
468 1.24 thorpej }
469 1.44 ad mutex_exit(&sc->sc_mutex);
470 1.89 mlelstv dk_start(dksc, NULL);
471 1.44 ad } else
472 1.44 ad mutex_exit(&sc->sc_mutex);
473 1.1 ad }
474 1.1 ad
475 1.29 thorpej static int
476 1.1 ad ldsize(dev_t dev)
477 1.1 ad {
478 1.94.2.4 pgoyette device_t self;
479 1.1 ad struct ld_softc *sc;
480 1.83 mlelstv struct dk_softc *dksc;
481 1.83 mlelstv int unit;
482 1.94.2.4 pgoyette int error;
483 1.1 ad
484 1.1 ad unit = DISKUNIT(dev);
485 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
486 1.94.2.4 pgoyette if (self == NULL)
487 1.94.2.7 pgoyette return (-1);
488 1.94.2.4 pgoyette sc = device_private(self);
489 1.83 mlelstv dksc = &sc->sc_dksc;
490 1.83 mlelstv
491 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
492 1.94.2.7 pgoyette return (-1);
493 1.94.2.7 pgoyette
494 1.94.2.7 pgoyette return dk_size(dksc, dev);
495 1.1 ad
496 1.94.2.4 pgoyette device_release(self);
497 1.94.2.4 pgoyette return error;
498 1.1 ad }
499 1.1 ad
500 1.1 ad /*
501 1.1 ad * Take a dump.
502 1.1 ad */
503 1.29 thorpej static int
504 1.83 mlelstv lddump(dev_t dev, daddr_t blkno, void *va, size_t size)
505 1.1 ad {
506 1.94.2.4 pgoyette device_t self;
507 1.1 ad struct ld_softc *sc;
508 1.83 mlelstv struct dk_softc *dksc;
509 1.83 mlelstv int unit;
510 1.94.2.4 pgoyette int error;
511 1.1 ad
512 1.1 ad unit = DISKUNIT(dev);
513 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
514 1.94.2.4 pgoyette if (self == NULL)
515 1.94.2.4 pgoyette return ENXIO;
516 1.94.2.4 pgoyette sc = device_private(self);
517 1.83 mlelstv dksc = &sc->sc_dksc;
518 1.83 mlelstv
519 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
520 1.94.2.4 pgoyette error = (ENODEV);
521 1.94.2.4 pgoyette else
522 1.94.2.4 pgoyette error = dk_dump(dksc, dev, blkno, va, size);
523 1.83 mlelstv
524 1.94.2.4 pgoyette device_release(self);
525 1.94.2.4 pgoyette return error;
526 1.83 mlelstv }
527 1.83 mlelstv
528 1.83 mlelstv static int
529 1.83 mlelstv ld_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
530 1.83 mlelstv {
531 1.94.2.4 pgoyette struct ld_softc *sc;
532 1.94.2.4 pgoyette int error;
533 1.84 skrll
534 1.94.2.4 pgoyette device_acquire(dev);
535 1.94.2.4 pgoyette sc = device_private(dev);
536 1.3 ad if (sc->sc_dump == NULL)
537 1.94.2.4 pgoyette error = ENODEV;
538 1.94.2.4 pgoyette else
539 1.94.2.4 pgoyette error = (*sc->sc_dump)(sc, va, blkno, nblk);
540 1.3 ad
541 1.94.2.4 pgoyette device_release(dev);
542 1.94.2.4 pgoyette return error;
543 1.1 ad }
544 1.1 ad
545 1.1 ad /*
546 1.1 ad * Adjust the size of a transfer.
547 1.1 ad */
548 1.1 ad static void
549 1.1 ad ldminphys(struct buf *bp)
550 1.1 ad {
551 1.94.2.4 pgoyette device_t self;
552 1.83 mlelstv int unit;
553 1.1 ad struct ld_softc *sc;
554 1.1 ad
555 1.83 mlelstv unit = DISKUNIT(bp->b_dev);
556 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
557 1.94.2.4 pgoyette if (self == NULL)
558 1.94.2.4 pgoyette return;
559 1.94.2.4 pgoyette sc = device_private(self);
560 1.1 ad
561 1.83 mlelstv ld_iosize(sc->sc_dv, &bp->b_bcount);
562 1.1 ad minphys(bp);
563 1.94.2.4 pgoyette device_release(self);
564 1.1 ad }
565 1.43 riz
566 1.43 riz static void
567 1.83 mlelstv ld_iosize(device_t d, int *countp)
568 1.83 mlelstv {
569 1.94.2.4 pgoyette struct ld_softc *sc;
570 1.94.2.4 pgoyette
571 1.94.2.4 pgoyette device_acquire(d);
572 1.94.2.4 pgoyette sc = device_private(d);
573 1.83 mlelstv
574 1.83 mlelstv if (*countp > sc->sc_maxxfer)
575 1.83 mlelstv *countp = sc->sc_maxxfer;
576 1.94.2.4 pgoyette
577 1.94.2.4 pgoyette device_release(d);
578 1.83 mlelstv }
579 1.83 mlelstv
580 1.83 mlelstv static void
581 1.83 mlelstv ld_fake_geometry(struct ld_softc *sc)
582 1.83 mlelstv {
583 1.83 mlelstv uint64_t ncyl;
584 1.83 mlelstv
585 1.83 mlelstv if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
586 1.83 mlelstv sc->sc_nheads = 16;
587 1.83 mlelstv else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
588 1.83 mlelstv sc->sc_nheads = 32;
589 1.83 mlelstv else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
590 1.83 mlelstv sc->sc_nheads = 64;
591 1.83 mlelstv else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
592 1.83 mlelstv sc->sc_nheads = 128;
593 1.83 mlelstv else
594 1.83 mlelstv sc->sc_nheads = 255;
595 1.83 mlelstv
596 1.83 mlelstv sc->sc_nsectors = 63;
597 1.83 mlelstv sc->sc_ncylinders = INT_MAX;
598 1.83 mlelstv ncyl = sc->sc_secperunit /
599 1.83 mlelstv (sc->sc_nheads * sc->sc_nsectors);
600 1.83 mlelstv if (ncyl < INT_MAX)
601 1.83 mlelstv sc->sc_ncylinders = (int)ncyl;
602 1.83 mlelstv }
603 1.83 mlelstv
604 1.83 mlelstv static void
605 1.83 mlelstv ld_set_geometry(struct ld_softc *sc)
606 1.43 riz {
607 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
608 1.83 mlelstv struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
609 1.83 mlelstv char tbuf[9];
610 1.83 mlelstv
611 1.83 mlelstv format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
612 1.83 mlelstv sc->sc_secsize);
613 1.83 mlelstv aprint_normal_dev(dksc->sc_dev, "%s, %d cyl, %d head, %d sec, "
614 1.83 mlelstv "%d bytes/sect x %"PRIu64" sectors\n",
615 1.83 mlelstv tbuf, sc->sc_ncylinders, sc->sc_nheads,
616 1.83 mlelstv sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
617 1.43 riz
618 1.71 christos memset(dg, 0, sizeof(*dg));
619 1.83 mlelstv dg->dg_secperunit = sc->sc_secperunit;
620 1.83 mlelstv dg->dg_secsize = sc->sc_secsize;
621 1.83 mlelstv dg->dg_nsectors = sc->sc_nsectors;
622 1.83 mlelstv dg->dg_ntracks = sc->sc_nheads;
623 1.83 mlelstv dg->dg_ncylinders = sc->sc_ncylinders;
624 1.43 riz
625 1.83 mlelstv disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
626 1.43 riz }
627 1.45 riz
628 1.45 riz static void
629 1.65 cegger ld_config_interrupts(device_t d)
630 1.45 riz {
631 1.94.2.4 pgoyette struct ld_softc *sc;
632 1.94.2.5 pgoyette struct dk_softc *dksc;
633 1.83 mlelstv
634 1.94.2.4 pgoyette device_acquire(d);
635 1.94.2.4 pgoyette sc = device_private(d);
636 1.94.2.5 pgoyette dksc = &sc->sc_dksc;
637 1.83 mlelstv dkwedge_discover(&dksc->sc_dkdev);
638 1.94.2.4 pgoyette device_release(d);
639 1.45 riz }
640 1.90 jakllsch
641 1.90 jakllsch static int
642 1.90 jakllsch ld_discard(device_t dev, off_t pos, off_t len)
643 1.90 jakllsch {
644 1.94.2.4 pgoyette struct ld_softc *sc;
645 1.94.2.4 pgoyette int error;
646 1.90 jakllsch
647 1.94.2.4 pgoyette device_acquire(dev);
648 1.94.2.4 pgoyette sc = device_private(dev);
649 1.90 jakllsch if (sc->sc_discard == NULL)
650 1.94.2.4 pgoyette error = (ENODEV);
651 1.94.2.4 pgoyette else
652 1.94.2.4 pgoyette error = (*sc->sc_discard)(sc, pos, len);
653 1.94.2.4 pgoyette device_release(dev);
654 1.94.2.4 pgoyette return error;
655 1.90 jakllsch }
656 1.90 jakllsch
657 1.90 jakllsch static int
658 1.90 jakllsch lddiscard(dev_t dev, off_t pos, off_t len)
659 1.90 jakllsch {
660 1.94.2.5 pgoyette device_t self;
661 1.90 jakllsch struct ld_softc *sc;
662 1.90 jakllsch struct dk_softc *dksc;
663 1.90 jakllsch int unit;
664 1.94.2.4 pgoyette int error;
665 1.90 jakllsch
666 1.90 jakllsch unit = DISKUNIT(dev);
667 1.94.2.4 pgoyette self = device_lookup_acquire(&ld_cd, unit);
668 1.94.2.4 pgoyette if (self == NULL)
669 1.94.2.4 pgoyette return ENXIO;
670 1.94.2.4 pgoyette sc = device_private(self);
671 1.90 jakllsch dksc = &sc->sc_dksc;
672 1.90 jakllsch
673 1.94.2.4 pgoyette error = dk_discard(dksc, dev, pos, len);
674 1.94.2.4 pgoyette device_release(self);
675 1.94.2.4 pgoyette return error;
676 1.90 jakllsch }
677 1.94.2.6 pgoyette
678 1.94.2.6 pgoyette MODULE(MODULE_CLASS_DRIVER, ld, "dk_subr");
679 1.94.2.6 pgoyette
680 1.94.2.6 pgoyette #ifdef _MODULE
681 1.94.2.6 pgoyette CFDRIVER_DECL(ld, DV_DISK, NULL);
682 1.94.2.6 pgoyette #endif
683 1.94.2.6 pgoyette
684 1.94.2.6 pgoyette static int
685 1.94.2.6 pgoyette ld_modcmd(modcmd_t cmd, void *opaque)
686 1.94.2.6 pgoyette {
687 1.94.2.6 pgoyette #ifdef _MODULE
688 1.94.2.6 pgoyette devmajor_t bmajor, cmajor;
689 1.94.2.6 pgoyette #endif
690 1.94.2.6 pgoyette int error = 0;
691 1.94.2.6 pgoyette
692 1.94.2.6 pgoyette #ifdef _MODULE
693 1.94.2.6 pgoyette switch (cmd) {
694 1.94.2.6 pgoyette case MODULE_CMD_INIT:
695 1.94.2.6 pgoyette bmajor = cmajor = -1;
696 1.94.2.6 pgoyette error = devsw_attach(ld_cd.cd_name, &ld_bdevsw, &bmajor,
697 1.94.2.6 pgoyette &ld_cdevsw, &cmajor);
698 1.94.2.6 pgoyette if (error)
699 1.94.2.6 pgoyette break;
700 1.94.2.6 pgoyette error = config_cfdriver_attach(&ld_cd);
701 1.94.2.6 pgoyette break;
702 1.94.2.6 pgoyette case MODULE_CMD_FINI:
703 1.94.2.6 pgoyette error = config_cfdriver_detach(&ld_cd);
704 1.94.2.6 pgoyette if (error)
705 1.94.2.6 pgoyette break;
706 1.94.2.6 pgoyette devsw_detach(&ld_bdevsw, &ld_cdevsw);
707 1.94.2.6 pgoyette break;
708 1.94.2.6 pgoyette default:
709 1.94.2.6 pgoyette error = ENOTTY;
710 1.94.2.6 pgoyette break;
711 1.94.2.6 pgoyette }
712 1.94.2.6 pgoyette #endif
713 1.94.2.6 pgoyette
714 1.94.2.6 pgoyette return error;
715 1.94.2.6 pgoyette }
716