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