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