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