ld.c revision 1.100.4.2 1 1.100.4.2 pgoyette /* $NetBSD: ld.c,v 1.100.4.2 2017/04/29 11:12:14 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.100.4.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.100.4.2 2017/04/29 11:12:14 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.97 pgoyette #include <sys/module.h>
58 1.98 jdolecek #include <sys/reboot.h>
59 1.1 ad
60 1.1 ad #include <dev/ldvar.h>
61 1.1 ad
62 1.1 ad static void ldminphys(struct buf *bp);
63 1.67 jmcneill static bool ld_suspend(device_t, const pmf_qual_t *);
64 1.55 jmcneill static bool ld_shutdown(device_t, int);
65 1.89 mlelstv static int ld_diskstart(device_t, struct buf *bp);
66 1.83 mlelstv static void ld_iosize(device_t, int *);
67 1.83 mlelstv static int ld_dumpblocks(device_t, void *, daddr_t, int);
68 1.83 mlelstv static void ld_fake_geometry(struct ld_softc *);
69 1.71 christos static void ld_set_geometry(struct ld_softc *);
70 1.65 cegger static void ld_config_interrupts (device_t);
71 1.83 mlelstv static int ld_lastclose(device_t);
72 1.90 jakllsch static int ld_discard(device_t, off_t, off_t);
73 1.100 jdolecek static int ld_flush(device_t, bool);
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.100.4.1 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.100.4.1 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.95 jdolecek 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.95 jdolecek 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.100 jdolecek * XXX We can't really flush the cache here, because 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.100 jdolecek ld_flush(dksc->sc_dev, false);
259 1.24 thorpej #endif
260 1.85 mlelstv cv_destroy(&sc->sc_drain);
261 1.61 ws mutex_destroy(&sc->sc_mutex);
262 1.2 ad }
263 1.2 ad
264 1.8 lukem /* ARGSUSED */
265 1.55 jmcneill static bool
266 1.67 jmcneill ld_suspend(device_t dev, const pmf_qual_t *qual)
267 1.67 jmcneill {
268 1.67 jmcneill return ld_shutdown(dev, 0);
269 1.67 jmcneill }
270 1.67 jmcneill
271 1.67 jmcneill /* ARGSUSED */
272 1.67 jmcneill static bool
273 1.55 jmcneill ld_shutdown(device_t dev, int flags)
274 1.1 ad {
275 1.100 jdolecek if ((flags & RB_NOSYNC) == 0 && ld_flush(dev, true) != 0)
276 1.55 jmcneill return false;
277 1.55 jmcneill
278 1.55 jmcneill return true;
279 1.1 ad }
280 1.1 ad
281 1.8 lukem /* ARGSUSED */
282 1.29 thorpej static int
283 1.42 christos ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
284 1.1 ad {
285 1.100.4.1 pgoyette device_t self;
286 1.1 ad struct ld_softc *sc;
287 1.83 mlelstv struct dk_softc *dksc;
288 1.83 mlelstv int unit;
289 1.100.4.1 pgoyette int error;
290 1.1 ad
291 1.1 ad unit = DISKUNIT(dev);
292 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
293 1.100.4.1 pgoyette if (self == NULL)
294 1.100.4.1 pgoyette return ENXIO;
295 1.100.4.1 pgoyette sc = device_private(self);
296 1.83 mlelstv dksc = &sc->sc_dksc;
297 1.1 ad
298 1.100.4.1 pgoyette error = dk_open(dksc, dev, flags, fmt, l);
299 1.100.4.1 pgoyette device_release(self);
300 1.100.4.1 pgoyette return error;
301 1.1 ad }
302 1.1 ad
303 1.66 dyoung static int
304 1.83 mlelstv ld_lastclose(device_t self)
305 1.66 dyoung {
306 1.100 jdolecek ld_flush(self, false);
307 1.84 skrll
308 1.66 dyoung return 0;
309 1.84 skrll }
310 1.66 dyoung
311 1.8 lukem /* ARGSUSED */
312 1.29 thorpej static int
313 1.42 christos ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
314 1.1 ad {
315 1.100.4.1 pgoyette device_t self;
316 1.1 ad struct ld_softc *sc;
317 1.83 mlelstv struct dk_softc *dksc;
318 1.83 mlelstv int unit;
319 1.100.4.1 pgoyette int error;
320 1.1 ad
321 1.1 ad unit = DISKUNIT(dev);
322 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
323 1.100.4.1 pgoyette if (self == NULL)
324 1.100.4.1 pgoyette return ENXIO;
325 1.100.4.1 pgoyette sc = device_private(self);
326 1.83 mlelstv dksc = &sc->sc_dksc;
327 1.30 thorpej
328 1.100.4.1 pgoyette error = dk_close(dksc, dev, flags, fmt, l);
329 1.100.4.1 pgoyette device_release(self);
330 1.100.4.1 pgoyette return error;
331 1.1 ad }
332 1.1 ad
333 1.8 lukem /* ARGSUSED */
334 1.29 thorpej static int
335 1.42 christos ldread(dev_t dev, struct uio *uio, int ioflag)
336 1.1 ad {
337 1.1 ad
338 1.1 ad return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
339 1.1 ad }
340 1.1 ad
341 1.8 lukem /* ARGSUSED */
342 1.29 thorpej static int
343 1.42 christos ldwrite(dev_t dev, struct uio *uio, int ioflag)
344 1.1 ad {
345 1.1 ad
346 1.1 ad return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
347 1.1 ad }
348 1.1 ad
349 1.8 lukem /* ARGSUSED */
350 1.29 thorpej static int
351 1.46 christos ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
352 1.1 ad {
353 1.100.4.1 pgoyette device_t self;
354 1.1 ad struct ld_softc *sc;
355 1.83 mlelstv struct dk_softc *dksc;
356 1.80 christos int unit, error;
357 1.1 ad
358 1.1 ad unit = DISKUNIT(dev);
359 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
360 1.100.4.1 pgoyette if (self == NULL)
361 1.100.4.1 pgoyette return ENXIO;
362 1.100.4.1 pgoyette sc = device_private(self);
363 1.83 mlelstv dksc = &sc->sc_dksc;
364 1.1 ad
365 1.100.4.1 pgoyette error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
366 1.100.4.1 pgoyette if (error != EPASSTHROUGH) {
367 1.100.4.1 pgoyette device_release(self);
368 1.100.4.1 pgoyette return (error);
369 1.100.4.1 pgoyette }
370 1.100.4.1 pgoyette
371 1.47 tron error = 0;
372 1.83 mlelstv
373 1.100 jdolecek /*
374 1.100 jdolecek * Some common checks so that individual attachments wouldn't need
375 1.100 jdolecek * to duplicate them.
376 1.100 jdolecek */
377 1.1 ad switch (cmd) {
378 1.32 thorpej case DIOCCACHESYNC:
379 1.32 thorpej /*
380 1.32 thorpej * XXX Do we really need to care about having a writable
381 1.32 thorpej * file descriptor here?
382 1.32 thorpej */
383 1.32 thorpej if ((flag & FWRITE) == 0)
384 1.32 thorpej error = EBADF;
385 1.32 thorpej else
386 1.100 jdolecek error = 0;
387 1.32 thorpej break;
388 1.100 jdolecek }
389 1.100 jdolecek
390 1.100 jdolecek if (error != 0)
391 1.100 jdolecek return (error);
392 1.100 jdolecek
393 1.100 jdolecek if (sc->sc_ioctl) {
394 1.100 jdolecek error = (*sc->sc_ioctl)(sc, cmd, addr, flag, 0);
395 1.100 jdolecek if (error != EPASSTHROUGH)
396 1.100 jdolecek return (error);
397 1.100 jdolecek }
398 1.96 jdolecek
399 1.100 jdolecek /* something not handled by the attachment */
400 1.100 jdolecek return dk_ioctl(dksc, dev, cmd, addr, flag, l);
401 1.100 jdolecek }
402 1.100 jdolecek
403 1.100 jdolecek /*
404 1.100 jdolecek * Flush the device's cache.
405 1.100 jdolecek */
406 1.100 jdolecek static int
407 1.100 jdolecek ld_flush(device_t self, bool poll)
408 1.100 jdolecek {
409 1.100 jdolecek int error = 0;
410 1.100 jdolecek struct ld_softc *sc = device_private(self);
411 1.100 jdolecek
412 1.100 jdolecek if (sc->sc_ioctl) {
413 1.100 jdolecek error = (*sc->sc_ioctl)(sc, DIOCCACHESYNC, NULL, 0, poll);
414 1.100 jdolecek if (error != 0)
415 1.100 jdolecek device_printf(self, "unable to flush cache\n");
416 1.1 ad }
417 1.1 ad
418 1.100 jdolecek return error;
419 1.1 ad }
420 1.1 ad
421 1.29 thorpej static void
422 1.1 ad ldstrategy(struct buf *bp)
423 1.1 ad {
424 1.100.4.1 pgoyette device_t self;
425 1.1 ad struct ld_softc *sc;
426 1.83 mlelstv struct dk_softc *dksc;
427 1.83 mlelstv int unit;
428 1.2 ad
429 1.83 mlelstv unit = DISKUNIT(bp->b_dev);
430 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
431 1.100.4.1 pgoyette if (self == NULL)
432 1.100.4.1 pgoyette return;
433 1.100.4.1 pgoyette sc = device_private(self);
434 1.83 mlelstv dksc = &sc->sc_dksc;
435 1.23 thorpej
436 1.94 mlelstv dk_strategy(dksc, bp);
437 1.100.4.1 pgoyette device_release(self);
438 1.23 thorpej }
439 1.23 thorpej
440 1.89 mlelstv static int
441 1.89 mlelstv ld_diskstart(device_t dev, struct buf *bp)
442 1.23 thorpej {
443 1.100.4.1 pgoyette struct ld_softc *sc;
444 1.23 thorpej int error;
445 1.1 ad
446 1.100.4.1 pgoyette device_acquire(dev);
447 1.100.4.1 pgoyette sc = device_private(dev);
448 1.100.4.1 pgoyette if (sc->sc_queuecnt >= sc->sc_maxqueuecnt) {
449 1.100.4.1 pgoyette device_release(dev);
450 1.89 mlelstv return EAGAIN;
451 1.100.4.1 pgoyette }
452 1.89 mlelstv
453 1.44 ad mutex_enter(&sc->sc_mutex);
454 1.44 ad
455 1.89 mlelstv if (sc->sc_queuecnt >= sc->sc_maxqueuecnt)
456 1.89 mlelstv error = EAGAIN;
457 1.89 mlelstv else {
458 1.89 mlelstv error = (*sc->sc_start)(sc, bp);
459 1.89 mlelstv if (error == 0)
460 1.89 mlelstv sc->sc_queuecnt++;
461 1.1 ad }
462 1.44 ad
463 1.44 ad mutex_exit(&sc->sc_mutex);
464 1.89 mlelstv
465 1.100.4.1 pgoyette device_release(dev);
466 1.89 mlelstv return error;
467 1.1 ad }
468 1.1 ad
469 1.1 ad void
470 1.1 ad lddone(struct ld_softc *sc, struct buf *bp)
471 1.1 ad {
472 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
473 1.1 ad
474 1.83 mlelstv dk_done(dksc, bp);
475 1.1 ad
476 1.44 ad mutex_enter(&sc->sc_mutex);
477 1.7 ad if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
478 1.24 thorpej if ((sc->sc_flags & LDF_DRAIN) != 0) {
479 1.24 thorpej sc->sc_flags &= ~LDF_DRAIN;
480 1.88 mlelstv cv_broadcast(&sc->sc_drain);
481 1.24 thorpej }
482 1.44 ad mutex_exit(&sc->sc_mutex);
483 1.89 mlelstv dk_start(dksc, NULL);
484 1.44 ad } else
485 1.44 ad mutex_exit(&sc->sc_mutex);
486 1.1 ad }
487 1.1 ad
488 1.29 thorpej static int
489 1.1 ad ldsize(dev_t dev)
490 1.1 ad {
491 1.100.4.1 pgoyette device_t self;
492 1.1 ad struct ld_softc *sc;
493 1.83 mlelstv struct dk_softc *dksc;
494 1.83 mlelstv int unit;
495 1.100.4.1 pgoyette int error;
496 1.1 ad
497 1.1 ad unit = DISKUNIT(dev);
498 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
499 1.100.4.1 pgoyette if (self == NULL)
500 1.99 mlelstv return (-1);
501 1.100.4.1 pgoyette sc = device_private(self);
502 1.83 mlelstv dksc = &sc->sc_dksc;
503 1.83 mlelstv
504 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
505 1.99 mlelstv return (-1);
506 1.1 ad
507 1.83 mlelstv return dk_size(dksc, dev);
508 1.100.4.1 pgoyette
509 1.100.4.1 pgoyette device_release(self);
510 1.100.4.1 pgoyette return error;
511 1.1 ad }
512 1.1 ad
513 1.1 ad /*
514 1.1 ad * Take a dump.
515 1.1 ad */
516 1.29 thorpej static int
517 1.83 mlelstv lddump(dev_t dev, daddr_t blkno, void *va, size_t size)
518 1.1 ad {
519 1.100.4.1 pgoyette device_t self;
520 1.1 ad struct ld_softc *sc;
521 1.83 mlelstv struct dk_softc *dksc;
522 1.83 mlelstv int unit;
523 1.100.4.1 pgoyette int error;
524 1.1 ad
525 1.1 ad unit = DISKUNIT(dev);
526 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
527 1.100.4.1 pgoyette if (self == NULL)
528 1.100.4.1 pgoyette return ENXIO;
529 1.100.4.1 pgoyette sc = device_private(self);
530 1.83 mlelstv dksc = &sc->sc_dksc;
531 1.83 mlelstv
532 1.1 ad if ((sc->sc_flags & LDF_ENABLED) == 0)
533 1.100.4.1 pgoyette error = (ENODEV);
534 1.100.4.1 pgoyette else
535 1.100.4.1 pgoyette error = dk_dump(dksc, dev, blkno, va, size);
536 1.83 mlelstv
537 1.100.4.1 pgoyette device_release(self);
538 1.100.4.1 pgoyette return error;
539 1.83 mlelstv }
540 1.83 mlelstv
541 1.83 mlelstv static int
542 1.83 mlelstv ld_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
543 1.83 mlelstv {
544 1.100.4.1 pgoyette struct ld_softc *sc;
545 1.100.4.1 pgoyette int error;
546 1.84 skrll
547 1.100.4.1 pgoyette device_acquire(dev);
548 1.100.4.1 pgoyette sc = device_private(dev);
549 1.3 ad if (sc->sc_dump == NULL)
550 1.100.4.1 pgoyette error = ENODEV;
551 1.100.4.1 pgoyette else
552 1.100.4.1 pgoyette error = (*sc->sc_dump)(sc, va, blkno, nblk);
553 1.3 ad
554 1.100.4.1 pgoyette device_release(dev);
555 1.100.4.1 pgoyette return error;
556 1.1 ad }
557 1.1 ad
558 1.1 ad /*
559 1.1 ad * Adjust the size of a transfer.
560 1.1 ad */
561 1.1 ad static void
562 1.1 ad ldminphys(struct buf *bp)
563 1.1 ad {
564 1.100.4.1 pgoyette device_t self;
565 1.83 mlelstv int unit;
566 1.1 ad struct ld_softc *sc;
567 1.1 ad
568 1.83 mlelstv unit = DISKUNIT(bp->b_dev);
569 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
570 1.100.4.1 pgoyette if (self == NULL)
571 1.100.4.1 pgoyette return;
572 1.100.4.1 pgoyette sc = device_private(self);
573 1.1 ad
574 1.83 mlelstv ld_iosize(sc->sc_dv, &bp->b_bcount);
575 1.1 ad minphys(bp);
576 1.100.4.1 pgoyette device_release(self);
577 1.1 ad }
578 1.43 riz
579 1.43 riz static void
580 1.83 mlelstv ld_iosize(device_t d, int *countp)
581 1.83 mlelstv {
582 1.100.4.1 pgoyette struct ld_softc *sc;
583 1.100.4.1 pgoyette
584 1.100.4.1 pgoyette device_acquire(d);
585 1.100.4.1 pgoyette sc = device_private(d);
586 1.83 mlelstv
587 1.83 mlelstv if (*countp > sc->sc_maxxfer)
588 1.83 mlelstv *countp = sc->sc_maxxfer;
589 1.100.4.1 pgoyette
590 1.100.4.1 pgoyette device_release(d);
591 1.83 mlelstv }
592 1.83 mlelstv
593 1.83 mlelstv static void
594 1.83 mlelstv ld_fake_geometry(struct ld_softc *sc)
595 1.83 mlelstv {
596 1.83 mlelstv uint64_t ncyl;
597 1.83 mlelstv
598 1.83 mlelstv if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
599 1.83 mlelstv sc->sc_nheads = 16;
600 1.83 mlelstv else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
601 1.83 mlelstv sc->sc_nheads = 32;
602 1.83 mlelstv else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
603 1.83 mlelstv sc->sc_nheads = 64;
604 1.83 mlelstv else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
605 1.83 mlelstv sc->sc_nheads = 128;
606 1.83 mlelstv else
607 1.83 mlelstv sc->sc_nheads = 255;
608 1.83 mlelstv
609 1.83 mlelstv sc->sc_nsectors = 63;
610 1.83 mlelstv sc->sc_ncylinders = INT_MAX;
611 1.83 mlelstv ncyl = sc->sc_secperunit /
612 1.83 mlelstv (sc->sc_nheads * sc->sc_nsectors);
613 1.83 mlelstv if (ncyl < INT_MAX)
614 1.83 mlelstv sc->sc_ncylinders = (int)ncyl;
615 1.83 mlelstv }
616 1.83 mlelstv
617 1.83 mlelstv static void
618 1.83 mlelstv ld_set_geometry(struct ld_softc *sc)
619 1.43 riz {
620 1.83 mlelstv struct dk_softc *dksc = &sc->sc_dksc;
621 1.83 mlelstv struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
622 1.83 mlelstv char tbuf[9];
623 1.83 mlelstv
624 1.83 mlelstv format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
625 1.83 mlelstv sc->sc_secsize);
626 1.83 mlelstv aprint_normal_dev(dksc->sc_dev, "%s, %d cyl, %d head, %d sec, "
627 1.83 mlelstv "%d bytes/sect x %"PRIu64" sectors\n",
628 1.83 mlelstv tbuf, sc->sc_ncylinders, sc->sc_nheads,
629 1.83 mlelstv sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
630 1.43 riz
631 1.71 christos memset(dg, 0, sizeof(*dg));
632 1.83 mlelstv dg->dg_secperunit = sc->sc_secperunit;
633 1.83 mlelstv dg->dg_secsize = sc->sc_secsize;
634 1.83 mlelstv dg->dg_nsectors = sc->sc_nsectors;
635 1.83 mlelstv dg->dg_ntracks = sc->sc_nheads;
636 1.83 mlelstv dg->dg_ncylinders = sc->sc_ncylinders;
637 1.43 riz
638 1.83 mlelstv disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
639 1.43 riz }
640 1.45 riz
641 1.45 riz static void
642 1.65 cegger ld_config_interrupts(device_t d)
643 1.45 riz {
644 1.100.4.1 pgoyette struct ld_softc *sc;
645 1.100.4.1 pgoyette struct dk_softc *dksc;
646 1.83 mlelstv
647 1.100.4.1 pgoyette device_acquire(d);
648 1.100.4.1 pgoyette sc = device_private(d);
649 1.100.4.1 pgoyette dksc = &sc->sc_dksc;
650 1.83 mlelstv dkwedge_discover(&dksc->sc_dkdev);
651 1.100.4.1 pgoyette device_release(d);
652 1.45 riz }
653 1.90 jakllsch
654 1.90 jakllsch static int
655 1.90 jakllsch ld_discard(device_t dev, off_t pos, off_t len)
656 1.90 jakllsch {
657 1.100.4.1 pgoyette struct ld_softc *sc;
658 1.100.4.1 pgoyette int error;
659 1.90 jakllsch
660 1.100.4.1 pgoyette device_acquire(dev);
661 1.100.4.1 pgoyette sc = device_private(dev);
662 1.90 jakllsch if (sc->sc_discard == NULL)
663 1.100.4.1 pgoyette error = (ENODEV);
664 1.100.4.1 pgoyette else
665 1.100.4.1 pgoyette error = (*sc->sc_discard)(sc, pos, len);
666 1.100.4.1 pgoyette device_release(dev);
667 1.100.4.1 pgoyette return error;
668 1.90 jakllsch }
669 1.90 jakllsch
670 1.90 jakllsch static int
671 1.90 jakllsch lddiscard(dev_t dev, off_t pos, off_t len)
672 1.90 jakllsch {
673 1.100.4.1 pgoyette device_t self;
674 1.90 jakllsch struct ld_softc *sc;
675 1.90 jakllsch struct dk_softc *dksc;
676 1.90 jakllsch int unit;
677 1.100.4.1 pgoyette int error;
678 1.90 jakllsch
679 1.90 jakllsch unit = DISKUNIT(dev);
680 1.100.4.1 pgoyette self = device_lookup_acquire(&ld_cd, unit);
681 1.100.4.1 pgoyette if (self == NULL)
682 1.100.4.1 pgoyette return ENXIO;
683 1.100.4.1 pgoyette sc = device_private(self);
684 1.90 jakllsch dksc = &sc->sc_dksc;
685 1.90 jakllsch
686 1.100.4.1 pgoyette error = dk_discard(dksc, dev, pos, len);
687 1.100.4.1 pgoyette device_release(self);
688 1.100.4.1 pgoyette return error;
689 1.90 jakllsch }
690 1.97 pgoyette
691 1.97 pgoyette MODULE(MODULE_CLASS_DRIVER, ld, "dk_subr");
692 1.97 pgoyette
693 1.97 pgoyette #ifdef _MODULE
694 1.97 pgoyette CFDRIVER_DECL(ld, DV_DISK, NULL);
695 1.97 pgoyette #endif
696 1.97 pgoyette
697 1.97 pgoyette static int
698 1.97 pgoyette ld_modcmd(modcmd_t cmd, void *opaque)
699 1.97 pgoyette {
700 1.97 pgoyette #ifdef _MODULE
701 1.97 pgoyette devmajor_t bmajor, cmajor;
702 1.97 pgoyette #endif
703 1.97 pgoyette int error = 0;
704 1.97 pgoyette
705 1.97 pgoyette #ifdef _MODULE
706 1.97 pgoyette switch (cmd) {
707 1.97 pgoyette case MODULE_CMD_INIT:
708 1.97 pgoyette bmajor = cmajor = -1;
709 1.97 pgoyette error = devsw_attach(ld_cd.cd_name, &ld_bdevsw, &bmajor,
710 1.97 pgoyette &ld_cdevsw, &cmajor);
711 1.97 pgoyette if (error)
712 1.97 pgoyette break;
713 1.97 pgoyette error = config_cfdriver_attach(&ld_cd);
714 1.97 pgoyette break;
715 1.97 pgoyette case MODULE_CMD_FINI:
716 1.97 pgoyette error = config_cfdriver_detach(&ld_cd);
717 1.97 pgoyette if (error)
718 1.97 pgoyette break;
719 1.97 pgoyette devsw_detach(&ld_bdevsw, &ld_cdevsw);
720 1.97 pgoyette break;
721 1.97 pgoyette default:
722 1.97 pgoyette error = ENOTTY;
723 1.97 pgoyette break;
724 1.97 pgoyette }
725 1.97 pgoyette #endif
726 1.97 pgoyette
727 1.97 pgoyette return error;
728 1.97 pgoyette }
729