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