ld.c revision 1.94.2.1 1 /* $NetBSD: ld.c,v 1.94.2.1 2016/07/18 03:49:59 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran and Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Disk driver for use by RAID controllers.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ld.c,v 1.94.2.1 2016/07/18 03:49:59 pgoyette Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/proc.h>
45 #include <sys/buf.h>
46 #include <sys/bufq.h>
47 #include <sys/endian.h>
48 #include <sys/disklabel.h>
49 #include <sys/disk.h>
50 #include <sys/dkio.h>
51 #include <sys/stat.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/vnode.h>
55 #include <sys/syslog.h>
56 #include <sys/mutex.h>
57 #include <sys/localcount.h>
58
59 #include <dev/ldvar.h>
60
61 #include <prop/proplib.h>
62
63 static void ldminphys(struct buf *bp);
64 static bool ld_suspend(device_t, const pmf_qual_t *);
65 static bool ld_shutdown(device_t, int);
66 static int ld_diskstart(device_t, struct buf *bp);
67 static void ld_iosize(device_t, int *);
68 static int ld_dumpblocks(device_t, void *, daddr_t, int);
69 static void ld_fake_geometry(struct ld_softc *);
70 static void ld_set_geometry(struct ld_softc *);
71 static void ld_config_interrupts (device_t);
72 static int ld_lastclose(device_t);
73 static int ld_discard(device_t, off_t, off_t);
74
75 extern struct cfdriver ld_cd;
76
77 static dev_type_open(ldopen);
78 static dev_type_close(ldclose);
79 static dev_type_read(ldread);
80 static dev_type_write(ldwrite);
81 static dev_type_ioctl(ldioctl);
82 static dev_type_strategy(ldstrategy);
83 static dev_type_dump(lddump);
84 static dev_type_size(ldsize);
85 static dev_type_discard(lddiscard);
86
87 #ifdef _MODULE
88 struct localcount ld_b_localcount, ld_c_localcount;
89 #endif
90
91 const struct bdevsw ld_bdevsw = {
92 .d_open = ldopen,
93 .d_close = ldclose,
94 .d_strategy = ldstrategy,
95 .d_ioctl = ldioctl,
96 .d_dump = lddump,
97 .d_psize = ldsize,
98 .d_discard = lddiscard,
99 #ifdef _MODULE
100 .d_localcount = &ld_b_localcount;
101 #endif
102 .d_flag = D_DISK | D_MPSAFE
103 };
104
105 const struct cdevsw ld_cdevsw = {
106 .d_open = ldopen,
107 .d_close = ldclose,
108 .d_read = ldread,
109 .d_write = ldwrite,
110 .d_ioctl = ldioctl,
111 .d_stop = nostop,
112 .d_tty = notty,
113 .d_poll = nopoll,
114 .d_mmap = nommap,
115 .d_kqfilter = nokqfilter,
116 .d_discard = lddiscard,
117 #ifdef _MODULE
118 .d_localcount = &ld_c_localcount;
119 #endif
120 .d_flag = D_DISK | D_MPSAFE
121 };
122
123 static struct dkdriver lddkdriver = {
124 .d_open = ldopen,
125 .d_close = ldclose,
126 .d_strategy = ldstrategy,
127 .d_iosize = ld_iosize,
128 .d_minphys = ldminphys,
129 .d_diskstart = ld_diskstart,
130 .d_dumpblocks = ld_dumpblocks,
131 .d_lastclose = ld_lastclose,
132 .d_discard = ld_discard
133 };
134
135 void
136 ldattach(struct ld_softc *sc)
137 {
138 device_t self = sc->sc_dv;
139 struct dk_softc *dksc = &sc->sc_dksc;
140
141 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
142 cv_init(&sc->sc_drain, "lddrain");
143
144 if ((sc->sc_flags & LDF_ENABLED) == 0) {
145 return;
146 }
147
148 /* Initialise dk and disk structure. */
149 dk_init(dksc, self, DKTYPE_LD);
150 disk_init(&dksc->sc_dkdev, dksc->sc_xname, &lddkdriver);
151
152 if (sc->sc_maxxfer > MAXPHYS)
153 sc->sc_maxxfer = MAXPHYS;
154
155 /* Build synthetic geometry if necessary. */
156 if (sc->sc_nheads == 0 || sc->sc_nsectors == 0 ||
157 sc->sc_ncylinders == 0)
158 ld_fake_geometry(sc);
159
160 sc->sc_disksize512 = sc->sc_secperunit * sc->sc_secsize / DEV_BSIZE;
161
162 /* Attach dk and disk subsystems */
163 dk_attach(dksc);
164 disk_attach(&dksc->sc_dkdev);
165 ld_set_geometry(sc);
166
167 bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
168
169 /* Register with PMF */
170 if (!pmf_device_register1(dksc->sc_dev, ld_suspend, NULL, ld_shutdown))
171 aprint_error_dev(dksc->sc_dev,
172 "couldn't establish power handler\n");
173
174 /* Discover wedges on this disk. */
175 config_interrupts(sc->sc_dv, ld_config_interrupts);
176 }
177
178 int
179 ldadjqparam(struct ld_softc *sc, int xmax)
180 {
181
182 mutex_enter(&sc->sc_mutex);
183 sc->sc_maxqueuecnt = xmax;
184 mutex_exit(&sc->sc_mutex);
185
186 return (0);
187 }
188
189 int
190 ldbegindetach(struct ld_softc *sc, int flags)
191 {
192 struct dk_softc *dksc = &sc->sc_dksc;
193 int rv = 0;
194
195 if ((sc->sc_flags & LDF_ENABLED) == 0)
196 return (0);
197
198 rv = disk_begindetach(&dksc->sc_dkdev, ld_lastclose, dksc->sc_dev, flags);
199
200 if (rv != 0)
201 return rv;
202
203 mutex_enter(&sc->sc_mutex);
204 sc->sc_maxqueuecnt = 0;
205
206 while (sc->sc_queuecnt > 0) {
207 sc->sc_flags |= LDF_DRAIN;
208 cv_wait(&sc->sc_drain, &sc->sc_mutex);
209 }
210 mutex_exit(&sc->sc_mutex);
211
212 return (rv);
213 }
214
215 void
216 ldenddetach(struct ld_softc *sc)
217 {
218 struct dk_softc *dksc = &sc->sc_dksc;
219 int bmaj, cmaj, i, mn;
220
221 if ((sc->sc_flags & LDF_ENABLED) == 0)
222 return;
223
224 mutex_enter(&sc->sc_mutex);
225
226 /* Wait for commands queued with the hardware to complete. */
227 if (sc->sc_queuecnt != 0) {
228 if (cv_timedwait(&sc->sc_drain, &sc->sc_mutex, 30 * hz))
229 printf("%s: not drained\n", dksc->sc_xname);
230 }
231 mutex_exit(&sc->sc_mutex);
232
233 /* Kill off any queued buffers. */
234 dk_drain(dksc);
235 bufq_free(dksc->sc_bufq);
236
237 /* Locate the major numbers. */
238 bmaj = bdevsw_lookup_major(&ld_bdevsw);
239 cmaj = cdevsw_lookup_major(&ld_cdevsw);
240
241 /* Nuke the vnodes for any open instances. */
242 for (i = 0; i < MAXPARTITIONS; i++) {
243 mn = DISKMINOR(device_unit(dksc->sc_dev), i);
244 vdevgone(bmaj, mn, mn, VBLK);
245 vdevgone(cmaj, mn, mn, VCHR);
246 }
247
248 /* Delete all of our wedges. */
249 dkwedge_delall(&dksc->sc_dkdev);
250
251 /* Detach from the disk list. */
252 disk_detach(&dksc->sc_dkdev);
253 disk_destroy(&dksc->sc_dkdev);
254
255 dk_detach(dksc);
256
257 /* Deregister with PMF */
258 pmf_device_deregister(dksc->sc_dev);
259
260 /*
261 * XXX We can't really flush the cache here, beceause the
262 * XXX device may already be non-existent from the controller's
263 * XXX perspective.
264 */
265 #if 0
266 /* Flush the device's cache. */
267 if (sc->sc_flush != NULL)
268 if ((*sc->sc_flush)(sc, 0) != 0)
269 device_printf(dksc->sc_dev, "unable to flush cache\n");
270 #endif
271 cv_destroy(&sc->sc_drain);
272 mutex_destroy(&sc->sc_mutex);
273 }
274
275 /* ARGSUSED */
276 static bool
277 ld_suspend(device_t dev, const pmf_qual_t *qual)
278 {
279 return ld_shutdown(dev, 0);
280 }
281
282 /* ARGSUSED */
283 static bool
284 ld_shutdown(device_t dev, int flags)
285 {
286 struct ld_softc *sc = device_private(dev);
287 struct dk_softc *dksc = &sc->sc_dksc;
288
289 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, LDFL_POLL) != 0) {
290 device_printf(dksc->sc_dev, "unable to flush cache\n");
291 return false;
292 }
293
294 return true;
295 }
296
297 /* ARGSUSED */
298 static int
299 ldopen(dev_t dev, int flags, int fmt, struct lwp *l)
300 {
301 struct ld_softc *sc;
302 struct dk_softc *dksc;
303 int unit;
304
305 unit = DISKUNIT(dev);
306 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
307 return (ENXIO);
308 dksc = &sc->sc_dksc;
309
310 return dk_open(dksc, dev, flags, fmt, l);
311 }
312
313 static int
314 ld_lastclose(device_t self)
315 {
316 struct ld_softc *sc = device_private(self);
317
318 if (sc->sc_flush != NULL && (*sc->sc_flush)(sc, 0) != 0)
319 device_printf(self, "unable to flush cache\n");
320
321 return 0;
322 }
323
324 /* ARGSUSED */
325 static int
326 ldclose(dev_t dev, int flags, int fmt, struct lwp *l)
327 {
328 struct ld_softc *sc;
329 struct dk_softc *dksc;
330 int unit;
331
332 unit = DISKUNIT(dev);
333 sc = device_lookup_private(&ld_cd, unit);
334 dksc = &sc->sc_dksc;
335
336 return dk_close(dksc, dev, flags, fmt, l);
337 }
338
339 /* ARGSUSED */
340 static int
341 ldread(dev_t dev, struct uio *uio, int ioflag)
342 {
343
344 return (physio(ldstrategy, NULL, dev, B_READ, ldminphys, uio));
345 }
346
347 /* ARGSUSED */
348 static int
349 ldwrite(dev_t dev, struct uio *uio, int ioflag)
350 {
351
352 return (physio(ldstrategy, NULL, dev, B_WRITE, ldminphys, uio));
353 }
354
355 /* ARGSUSED */
356 static int
357 ldioctl(dev_t dev, u_long cmd, void *addr, int32_t flag, struct lwp *l)
358 {
359 struct ld_softc *sc;
360 struct dk_softc *dksc;
361 int unit, error;
362
363 unit = DISKUNIT(dev);
364 sc = device_lookup_private(&ld_cd, unit);
365 dksc = &sc->sc_dksc;
366
367 error = dk_ioctl(dksc, dev, cmd, addr, flag, l);
368 if (error != EPASSTHROUGH)
369 return (error);
370
371 error = 0;
372
373 switch (cmd) {
374 case DIOCCACHESYNC:
375 /*
376 * XXX Do we really need to care about having a writable
377 * file descriptor here?
378 */
379 if ((flag & FWRITE) == 0)
380 error = EBADF;
381 else if (sc->sc_flush)
382 error = (*sc->sc_flush)(sc, 0);
383 else
384 error = 0; /* XXX Error out instead? */
385 break;
386 default:
387 error = ENOTTY;
388 break;
389 }
390
391 return (error);
392 }
393
394 static void
395 ldstrategy(struct buf *bp)
396 {
397 struct ld_softc *sc;
398 struct dk_softc *dksc;
399 int unit;
400
401 unit = DISKUNIT(bp->b_dev);
402 sc = device_lookup_private(&ld_cd, unit);
403 dksc = &sc->sc_dksc;
404
405 dk_strategy(dksc, bp);
406 }
407
408 static int
409 ld_diskstart(device_t dev, struct buf *bp)
410 {
411 struct ld_softc *sc = device_private(dev);
412 int error;
413
414 if (sc->sc_queuecnt >= sc->sc_maxqueuecnt)
415 return EAGAIN;
416
417 mutex_enter(&sc->sc_mutex);
418
419 if (sc->sc_queuecnt >= sc->sc_maxqueuecnt)
420 error = EAGAIN;
421 else {
422 error = (*sc->sc_start)(sc, bp);
423 if (error == 0)
424 sc->sc_queuecnt++;
425 }
426
427 mutex_exit(&sc->sc_mutex);
428
429 return error;
430 }
431
432 void
433 lddone(struct ld_softc *sc, struct buf *bp)
434 {
435 struct dk_softc *dksc = &sc->sc_dksc;
436
437 dk_done(dksc, bp);
438
439 mutex_enter(&sc->sc_mutex);
440 if (--sc->sc_queuecnt <= sc->sc_maxqueuecnt) {
441 if ((sc->sc_flags & LDF_DRAIN) != 0) {
442 sc->sc_flags &= ~LDF_DRAIN;
443 cv_broadcast(&sc->sc_drain);
444 }
445 mutex_exit(&sc->sc_mutex);
446 dk_start(dksc, NULL);
447 } else
448 mutex_exit(&sc->sc_mutex);
449 }
450
451 static int
452 ldsize(dev_t dev)
453 {
454 struct ld_softc *sc;
455 struct dk_softc *dksc;
456 int unit;
457
458 unit = DISKUNIT(dev);
459 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
460 return (ENODEV);
461 dksc = &sc->sc_dksc;
462
463 if ((sc->sc_flags & LDF_ENABLED) == 0)
464 return (ENODEV);
465
466 return dk_size(dksc, dev);
467 }
468
469 /*
470 * Take a dump.
471 */
472 static int
473 lddump(dev_t dev, daddr_t blkno, void *va, size_t size)
474 {
475 struct ld_softc *sc;
476 struct dk_softc *dksc;
477 int unit;
478
479 unit = DISKUNIT(dev);
480 if ((sc = device_lookup_private(&ld_cd, unit)) == NULL)
481 return (ENXIO);
482 dksc = &sc->sc_dksc;
483
484 if ((sc->sc_flags & LDF_ENABLED) == 0)
485 return (ENODEV);
486
487 return dk_dump(dksc, dev, blkno, va, size);
488 }
489
490 static int
491 ld_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
492 {
493 struct ld_softc *sc = device_private(dev);
494
495 if (sc->sc_dump == NULL)
496 return (ENODEV);
497
498 return (*sc->sc_dump)(sc, va, blkno, nblk);
499 }
500
501 /*
502 * Adjust the size of a transfer.
503 */
504 static void
505 ldminphys(struct buf *bp)
506 {
507 int unit;
508 struct ld_softc *sc;
509
510 unit = DISKUNIT(bp->b_dev);
511 sc = device_lookup_private(&ld_cd, unit);
512
513 ld_iosize(sc->sc_dv, &bp->b_bcount);
514 minphys(bp);
515 }
516
517 static void
518 ld_iosize(device_t d, int *countp)
519 {
520 struct ld_softc *sc = device_private(d);
521
522 if (*countp > sc->sc_maxxfer)
523 *countp = sc->sc_maxxfer;
524 }
525
526 static void
527 ld_fake_geometry(struct ld_softc *sc)
528 {
529 uint64_t ncyl;
530
531 if (sc->sc_secperunit <= 528 * 2048) /* 528MB */
532 sc->sc_nheads = 16;
533 else if (sc->sc_secperunit <= 1024 * 2048) /* 1GB */
534 sc->sc_nheads = 32;
535 else if (sc->sc_secperunit <= 21504 * 2048) /* 21GB */
536 sc->sc_nheads = 64;
537 else if (sc->sc_secperunit <= 43008 * 2048) /* 42GB */
538 sc->sc_nheads = 128;
539 else
540 sc->sc_nheads = 255;
541
542 sc->sc_nsectors = 63;
543 sc->sc_ncylinders = INT_MAX;
544 ncyl = sc->sc_secperunit /
545 (sc->sc_nheads * sc->sc_nsectors);
546 if (ncyl < INT_MAX)
547 sc->sc_ncylinders = (int)ncyl;
548 }
549
550 static void
551 ld_set_geometry(struct ld_softc *sc)
552 {
553 struct dk_softc *dksc = &sc->sc_dksc;
554 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
555 char tbuf[9];
556
557 format_bytes(tbuf, sizeof(tbuf), sc->sc_secperunit *
558 sc->sc_secsize);
559 aprint_normal_dev(dksc->sc_dev, "%s, %d cyl, %d head, %d sec, "
560 "%d bytes/sect x %"PRIu64" sectors\n",
561 tbuf, sc->sc_ncylinders, sc->sc_nheads,
562 sc->sc_nsectors, sc->sc_secsize, sc->sc_secperunit);
563
564 memset(dg, 0, sizeof(*dg));
565 dg->dg_secperunit = sc->sc_secperunit;
566 dg->dg_secsize = sc->sc_secsize;
567 dg->dg_nsectors = sc->sc_nsectors;
568 dg->dg_ntracks = sc->sc_nheads;
569 dg->dg_ncylinders = sc->sc_ncylinders;
570
571 disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, NULL);
572 }
573
574 static void
575 ld_config_interrupts(device_t d)
576 {
577 struct ld_softc *sc = device_private(d);
578 struct dk_softc *dksc = &sc->sc_dksc;
579
580 dkwedge_discover(&dksc->sc_dkdev);
581 }
582
583 static int
584 ld_discard(device_t dev, off_t pos, off_t len)
585 {
586 struct ld_softc *sc = device_private(dev);
587
588 if (sc->sc_discard == NULL)
589 return (ENODEV);
590
591 return (*sc->sc_discard)(sc, pos, len);
592 }
593
594 static int
595 lddiscard(dev_t dev, off_t pos, off_t len)
596 {
597 struct ld_softc *sc;
598 struct dk_softc *dksc;
599 int unit;
600
601 unit = DISKUNIT(dev);
602 sc = device_lookup_private(&ld_cd, unit);
603 dksc = &sc->sc_dksc;
604
605 return dk_discard(dksc, dev, pos, len);
606 }
607