sd.c revision 1.204 1 /* $NetBSD: sd.c,v 1.204 2003/09/08 18:51:38 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Originally written by Julian Elischer (julian (at) dialix.oz.au)
41 * for TRW Financial Systems for use under the MACH(2.5) operating system.
42 *
43 * TRW Financial Systems, in accordance with their agreement with Carnegie
44 * Mellon University, makes this software available to CMU to distribute
45 * or use in any manner that they see fit as long as this message is kept with
46 * the software. For this reason TFS also grants any other persons or
47 * organisations permission to use or modify this software.
48 *
49 * TFS supplies this software to be publicly redistributed
50 * on the understanding that TFS is not responsible for the correct
51 * functioning of this software in any circumstances.
52 *
53 * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
54 */
55
56 #include <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: sd.c,v 1.204 2003/09/08 18:51:38 mycroft Exp $");
58
59 #include "opt_scsi.h"
60 #include "opt_bufq.h"
61 #include "rnd.h"
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/file.h>
67 #include <sys/stat.h>
68 #include <sys/ioctl.h>
69 #include <sys/scsiio.h>
70 #include <sys/buf.h>
71 #include <sys/uio.h>
72 #include <sys/malloc.h>
73 #include <sys/errno.h>
74 #include <sys/device.h>
75 #include <sys/disklabel.h>
76 #include <sys/disk.h>
77 #include <sys/proc.h>
78 #include <sys/conf.h>
79 #include <sys/vnode.h>
80 #if NRND > 0
81 #include <sys/rnd.h>
82 #endif
83
84 #include <dev/scsipi/scsipi_all.h>
85 #include <dev/scsipi/scsi_all.h>
86 #include <dev/scsipi/scsipi_disk.h>
87 #include <dev/scsipi/scsi_disk.h>
88 #include <dev/scsipi/scsiconf.h>
89 #include <dev/scsipi/sdvar.h>
90
91 #include "sd.h" /* NSD_SCSIBUS and NSD_ATAPIBUS come from here */
92
93 #define SDUNIT(dev) DISKUNIT(dev)
94 #define SDPART(dev) DISKPART(dev)
95 #define SDMINOR(unit, part) DISKMINOR(unit, part)
96 #define MAKESDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
97
98 #define SDLABELDEV(dev) (MAKESDDEV(major(dev), SDUNIT(dev), RAW_PART))
99
100 int sdlock __P((struct sd_softc *));
101 void sdunlock __P((struct sd_softc *));
102 void sdminphys __P((struct buf *));
103 void sdgetdefaultlabel __P((struct sd_softc *, struct disklabel *));
104 void sdgetdisklabel __P((struct sd_softc *));
105 void sdstart __P((struct scsipi_periph *));
106 void sddone __P((struct scsipi_xfer *));
107 void sd_shutdown __P((void *));
108 int sd_reassign_blocks __P((struct sd_softc *, u_long));
109 int sd_interpret_sense __P((struct scsipi_xfer *));
110
111 extern struct cfdriver sd_cd;
112
113 dev_type_open(sdopen);
114 dev_type_close(sdclose);
115 dev_type_read(sdread);
116 dev_type_write(sdwrite);
117 dev_type_ioctl(sdioctl);
118 dev_type_strategy(sdstrategy);
119 dev_type_dump(sddump);
120 dev_type_size(sdsize);
121
122 const struct bdevsw sd_bdevsw = {
123 sdopen, sdclose, sdstrategy, sdioctl, sddump, sdsize, D_DISK
124 };
125
126 const struct cdevsw sd_cdevsw = {
127 sdopen, sdclose, sdread, sdwrite, sdioctl,
128 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
129 };
130
131 struct dkdriver sddkdriver = { sdstrategy };
132
133 const struct scsipi_periphsw sd_switch = {
134 sd_interpret_sense, /* check our error handler first */
135 sdstart, /* have a queue, served by this */
136 NULL, /* have no async handler */
137 sddone, /* deal with stats at interrupt time */
138 };
139
140 /*
141 * Attach routine common to atapi & scsi.
142 */
143 void
144 sdattach(parent, sd, periph, ops)
145 struct device *parent;
146 struct sd_softc *sd;
147 struct scsipi_periph *periph;
148 const struct sd_ops *ops;
149 {
150 int error, result;
151 struct disk_parms *dp = &sd->params;
152 char pbuf[9];
153
154 SC_DEBUG(periph, SCSIPI_DB2, ("sdattach: "));
155
156 #ifdef NEW_BUFQ_STRATEGY
157 bufq_alloc(&sd->buf_queue, BUFQ_READ_PRIO|BUFQ_SORT_RAWBLOCK);
158 #else
159 bufq_alloc(&sd->buf_queue, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
160 #endif
161
162 /*
163 * Store information needed to contact our base driver
164 */
165 sd->sc_periph = periph;
166 sd->sc_ops = ops;
167
168 periph->periph_dev = &sd->sc_dev;
169 periph->periph_switch = &sd_switch;
170
171 /*
172 * Increase our openings to the maximum-per-periph
173 * supported by the adapter. This will either be
174 * clamped down or grown by the adapter if necessary.
175 */
176 periph->periph_openings =
177 SCSIPI_CHAN_MAX_PERIPH(periph->periph_channel);
178 periph->periph_flags |= PERIPH_GROW_OPENINGS;
179
180 /*
181 * Initialize and attach the disk structure.
182 */
183 sd->sc_dk.dk_driver = &sddkdriver;
184 sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
185 disk_attach(&sd->sc_dk);
186
187 /*
188 * Use the subdriver to request information regarding the drive.
189 */
190 aprint_naive("\n");
191 aprint_normal("\n");
192
193 error = scsipi_test_unit_ready(periph,
194 XS_CTL_DISCOVERY | XS_CTL_IGNORE_ILLEGAL_REQUEST |
195 XS_CTL_IGNORE_MEDIA_CHANGE | XS_CTL_SILENT_NODEV);
196
197 if (error)
198 result = SDGP_RESULT_OFFLINE;
199 else
200 result = (*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
201 XS_CTL_DISCOVERY);
202 aprint_normal("%s: ", sd->sc_dev.dv_xname);
203 switch (result) {
204 case SDGP_RESULT_OK:
205 format_bytes(pbuf, sizeof(pbuf),
206 (u_int64_t)dp->disksize * dp->blksize);
207 aprint_normal(
208 "%s, %ld cyl, %ld head, %ld sec, %ld bytes/sect x %llu sectors",
209 pbuf, dp->cyls, dp->heads, dp->sectors, dp->blksize,
210 (unsigned long long)dp->disksize);
211 break;
212
213 case SDGP_RESULT_OFFLINE:
214 aprint_normal("drive offline");
215 break;
216
217 case SDGP_RESULT_UNFORMATTED:
218 aprint_normal("unformatted media");
219 break;
220
221 #ifdef DIAGNOSTIC
222 default:
223 panic("sdattach: unknown result from get_parms");
224 break;
225 #endif
226 }
227 aprint_normal("\n");
228
229 /*
230 * Establish a shutdown hook so that we can ensure that
231 * our data has actually made it onto the platter at
232 * shutdown time. Note that this relies on the fact
233 * that the shutdown hook code puts us at the head of
234 * the list (thus guaranteeing that our hook runs before
235 * our ancestors').
236 */
237 if ((sd->sc_sdhook =
238 shutdownhook_establish(sd_shutdown, sd)) == NULL)
239 aprint_error("%s: WARNING: unable to establish shutdown hook\n",
240 sd->sc_dev.dv_xname);
241
242 #if NRND > 0
243 /*
244 * attach the device into the random source list
245 */
246 rnd_attach_source(&sd->rnd_source, sd->sc_dev.dv_xname,
247 RND_TYPE_DISK, 0);
248 #endif
249 }
250
251 int
252 sdactivate(self, act)
253 struct device *self;
254 enum devact act;
255 {
256 int rv = 0;
257
258 switch (act) {
259 case DVACT_ACTIVATE:
260 rv = EOPNOTSUPP;
261 break;
262
263 case DVACT_DEACTIVATE:
264 /*
265 * Nothing to do; we key off the device's DVF_ACTIVE.
266 */
267 break;
268 }
269 return (rv);
270 }
271
272 int
273 sddetach(self, flags)
274 struct device *self;
275 int flags;
276 {
277 struct sd_softc *sd = (struct sd_softc *) self;
278 struct buf *bp;
279 int s, bmaj, cmaj, i, mn;
280
281 /* locate the major number */
282 bmaj = bdevsw_lookup_major(&sd_bdevsw);
283 cmaj = cdevsw_lookup_major(&sd_cdevsw);
284
285 s = splbio();
286
287 /* Kill off any queued buffers. */
288 while ((bp = BUFQ_GET(&sd->buf_queue)) != NULL) {
289 bp->b_error = EIO;
290 bp->b_flags |= B_ERROR;
291 bp->b_resid = bp->b_bcount;
292 biodone(bp);
293 }
294
295 bufq_free(&sd->buf_queue);
296
297 /* Kill off any pending commands. */
298 scsipi_kill_pending(sd->sc_periph);
299
300 splx(s);
301
302 /* Nuke the vnodes for any open instances */
303 for (i = 0; i < MAXPARTITIONS; i++) {
304 mn = SDMINOR(self->dv_unit, i);
305 vdevgone(bmaj, mn, mn, VBLK);
306 vdevgone(cmaj, mn, mn, VCHR);
307 }
308
309 /* Detach from the disk list. */
310 disk_detach(&sd->sc_dk);
311
312 /* Get rid of the shutdown hook. */
313 shutdownhook_disestablish(sd->sc_sdhook);
314
315 #if NRND > 0
316 /* Unhook the entropy source. */
317 rnd_detach_source(&sd->rnd_source);
318 #endif
319
320 return (0);
321 }
322
323 /*
324 * Wait interruptibly for an exclusive lock.
325 *
326 * XXX
327 * Several drivers do this; it should be abstracted and made MP-safe.
328 */
329 int
330 sdlock(sd)
331 struct sd_softc *sd;
332 {
333 int error;
334
335 while ((sd->flags & SDF_LOCKED) != 0) {
336 sd->flags |= SDF_WANTED;
337 if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0)
338 return (error);
339 }
340 sd->flags |= SDF_LOCKED;
341 return (0);
342 }
343
344 /*
345 * Unlock and wake up any waiters.
346 */
347 void
348 sdunlock(sd)
349 struct sd_softc *sd;
350 {
351
352 sd->flags &= ~SDF_LOCKED;
353 if ((sd->flags & SDF_WANTED) != 0) {
354 sd->flags &= ~SDF_WANTED;
355 wakeup(sd);
356 }
357 }
358
359 /*
360 * open the device. Make sure the partition info is a up-to-date as can be.
361 */
362 int
363 sdopen(dev, flag, fmt, p)
364 dev_t dev;
365 int flag, fmt;
366 struct proc *p;
367 {
368 struct sd_softc *sd;
369 struct scsipi_periph *periph;
370 struct scsipi_adapter *adapt;
371 int unit, part;
372 int error;
373
374 unit = SDUNIT(dev);
375 if (unit >= sd_cd.cd_ndevs)
376 return (ENXIO);
377 sd = sd_cd.cd_devs[unit];
378 if (sd == NULL)
379 return (ENXIO);
380
381 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
382 return (ENODEV);
383
384 periph = sd->sc_periph;
385 adapt = periph->periph_channel->chan_adapter;
386 part = SDPART(dev);
387
388 SC_DEBUG(periph, SCSIPI_DB1,
389 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
390 sd_cd.cd_ndevs, part));
391
392 /*
393 * If this is the first open of this device, add a reference
394 * to the adapter.
395 */
396 if (sd->sc_dk.dk_openmask == 0 &&
397 (error = scsipi_adapter_addref(adapt)) != 0)
398 return (error);
399
400 if ((error = sdlock(sd)) != 0)
401 goto bad4;
402
403 if ((periph->periph_flags & PERIPH_OPEN) != 0) {
404 /*
405 * If any partition is open, but the disk has been invalidated,
406 * disallow further opens of non-raw partition
407 */
408 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 &&
409 (part != RAW_PART || fmt != S_IFCHR)) {
410 error = EIO;
411 goto bad3;
412 }
413 } else {
414 /* Check that it is still responding and ok. */
415 error = scsipi_test_unit_ready(periph,
416 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
417 XS_CTL_SILENT_NODEV);
418
419 /*
420 * Start the pack spinning if necessary. Always allow the
421 * raw parition to be opened, for raw IOCTLs. Data transfers
422 * will check for SDEV_MEDIA_LOADED.
423 */
424 if (error == ENODEV) {
425 int silent, error2;
426
427 if (part == RAW_PART && fmt == S_IFCHR)
428 silent = XS_CTL_SILENT;
429 else
430 silent = 0;
431 error2 = scsipi_start(periph, SSS_START, silent);
432 switch (error2) {
433 case 0:
434 error = 0;
435 break;
436 case ENODEV:
437 case EINVAL:
438 if (silent)
439 goto out;
440 break;
441 default:
442 error = error2;
443 break;
444 }
445 }
446 if (error)
447 goto bad3;
448
449 periph->periph_flags |= PERIPH_OPEN;
450
451 if (periph->periph_flags & PERIPH_REMOVABLE) {
452 /* Lock the pack in. */
453 error = scsipi_prevent(periph, PR_PREVENT,
454 XS_CTL_IGNORE_ILLEGAL_REQUEST |
455 XS_CTL_IGNORE_MEDIA_CHANGE);
456 if (error)
457 goto bad;
458 }
459
460 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
461 periph->periph_flags |= PERIPH_MEDIA_LOADED;
462
463 /*
464 * Load the physical device parameters.
465 *
466 * Note that if media is present but unformatted,
467 * we allow the open (so that it can be formatted!).
468 * The drive should refuse real I/O, if the media is
469 * unformatted.
470 */
471 if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
472 0) == SDGP_RESULT_OFFLINE) {
473 error = ENXIO;
474 goto bad2;
475 }
476 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
477
478 /* Load the partition info if not already loaded. */
479 sdgetdisklabel(sd);
480 SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel loaded "));
481 }
482 }
483
484 /* Check that the partition exists. */
485 if (part != RAW_PART &&
486 (part >= sd->sc_dk.dk_label->d_npartitions ||
487 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
488 error = ENXIO;
489 goto bad;
490 }
491
492 out: /* Insure only one open at a time. */
493 switch (fmt) {
494 case S_IFCHR:
495 sd->sc_dk.dk_copenmask |= (1 << part);
496 break;
497 case S_IFBLK:
498 sd->sc_dk.dk_bopenmask |= (1 << part);
499 break;
500 }
501 sd->sc_dk.dk_openmask =
502 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
503
504 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
505 sdunlock(sd);
506 return (0);
507
508 bad2:
509 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
510
511 bad:
512 if (sd->sc_dk.dk_openmask == 0) {
513 scsipi_prevent(periph, PR_ALLOW,
514 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE);
515 periph->periph_flags &= ~PERIPH_OPEN;
516 }
517
518 bad3:
519 sdunlock(sd);
520 bad4:
521 if (sd->sc_dk.dk_openmask == 0)
522 scsipi_adapter_delref(adapt);
523 return (error);
524 }
525
526 /*
527 * close the device.. only called if we are the LAST occurence of an open
528 * device. Convenient now but usually a pain.
529 */
530 int
531 sdclose(dev, flag, fmt, p)
532 dev_t dev;
533 int flag, fmt;
534 struct proc *p;
535 {
536 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
537 struct scsipi_periph *periph = sd->sc_periph;
538 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
539 int part = SDPART(dev);
540 int error;
541
542 if ((error = sdlock(sd)) != 0)
543 return (error);
544
545 switch (fmt) {
546 case S_IFCHR:
547 sd->sc_dk.dk_copenmask &= ~(1 << part);
548 break;
549 case S_IFBLK:
550 sd->sc_dk.dk_bopenmask &= ~(1 << part);
551 break;
552 }
553 sd->sc_dk.dk_openmask =
554 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
555
556 if (sd->sc_dk.dk_openmask == 0) {
557 /*
558 * If the disk cache needs flushing, and the disk supports
559 * it, do it now.
560 */
561 if ((sd->flags & SDF_DIRTY) != 0 &&
562 sd->sc_ops->sdo_flush != NULL) {
563 if ((*sd->sc_ops->sdo_flush)(sd, 0)) {
564 printf("%s: cache synchronization failed\n",
565 sd->sc_dev.dv_xname);
566 sd->flags &= ~SDF_FLUSHING;
567 } else
568 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
569 }
570
571 if (! (periph->periph_flags & PERIPH_KEEP_LABEL))
572 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
573
574 scsipi_wait_drain(periph);
575
576 if (periph->periph_flags & PERIPH_REMOVABLE) {
577 scsipi_prevent(periph, PR_ALLOW,
578 XS_CTL_IGNORE_ILLEGAL_REQUEST |
579 XS_CTL_IGNORE_NOT_READY);
580 }
581 periph->periph_flags &= ~PERIPH_OPEN;
582
583 scsipi_wait_drain(periph);
584
585 scsipi_adapter_delref(adapt);
586 }
587
588 sdunlock(sd);
589 return (0);
590 }
591
592 /*
593 * Actually translate the requested transfer into one the physical driver
594 * can understand. The transfer is described by a buf and will include
595 * only one physical transfer.
596 */
597 void
598 sdstrategy(bp)
599 struct buf *bp;
600 {
601 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
602 struct scsipi_periph *periph = sd->sc_periph;
603 struct disklabel *lp;
604 daddr_t blkno;
605 int s;
606 boolean_t sector_aligned;
607
608 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
609 SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
610 ("%ld bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
611 /*
612 * If the device has been made invalid, error out
613 */
614 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
615 (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
616 if (periph->periph_flags & PERIPH_OPEN)
617 bp->b_error = EIO;
618 else
619 bp->b_error = ENODEV;
620 goto bad;
621 }
622
623 lp = sd->sc_dk.dk_label;
624
625 /*
626 * The transfer must be a whole number of blocks, offset must not be
627 * negative.
628 */
629 if (lp->d_secsize == DEV_BSIZE) {
630 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
631 } else {
632 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
633 }
634 if (!sector_aligned || bp->b_blkno < 0) {
635 bp->b_error = EINVAL;
636 goto bad;
637 }
638 /*
639 * If it's a null transfer, return immediatly
640 */
641 if (bp->b_bcount == 0)
642 goto done;
643
644 /*
645 * Do bounds checking, adjust transfer. if error, process.
646 * If end of partition, just return.
647 */
648 if (SDPART(bp->b_dev) == RAW_PART) {
649 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
650 sd->params.disksize512) <= 0)
651 goto done;
652 } else {
653 if (bounds_check_with_label(&sd->sc_dk, bp,
654 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
655 goto done;
656 }
657
658 /*
659 * Now convert the block number to absolute and put it in
660 * terms of the device's logical block size.
661 */
662 if (lp->d_secsize == DEV_BSIZE)
663 blkno = bp->b_blkno;
664 else if (lp->d_secsize > DEV_BSIZE)
665 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
666 else
667 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
668
669 if (SDPART(bp->b_dev) != RAW_PART)
670 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
671
672 bp->b_rawblkno = blkno;
673
674 s = splbio();
675
676 /*
677 * Place it in the queue of disk activities for this disk.
678 *
679 * XXX Only do disksort() if the current operating mode does not
680 * XXX include tagged queueing.
681 */
682 BUFQ_PUT(&sd->buf_queue, bp);
683
684 /*
685 * Tell the device to get going on the transfer if it's
686 * not doing anything, otherwise just wait for completion
687 */
688 sdstart(sd->sc_periph);
689
690 splx(s);
691 return;
692
693 bad:
694 bp->b_flags |= B_ERROR;
695 done:
696 /*
697 * Correctly set the buf to indicate a completed xfer
698 */
699 bp->b_resid = bp->b_bcount;
700 biodone(bp);
701 }
702
703 /*
704 * sdstart looks to see if there is a buf waiting for the device
705 * and that the device is not already busy. If both are true,
706 * It dequeues the buf and creates a scsi command to perform the
707 * transfer in the buf. The transfer request will call scsipi_done
708 * on completion, which will in turn call this routine again
709 * so that the next queued transfer is performed.
710 * The bufs are queued by the strategy routine (sdstrategy)
711 *
712 * This routine is also called after other non-queued requests
713 * have been made of the scsi driver, to ensure that the queue
714 * continues to be drained.
715 *
716 * must be called at the correct (highish) spl level
717 * sdstart() is called at splbio from sdstrategy and scsipi_done
718 */
719 void
720 sdstart(periph)
721 struct scsipi_periph *periph;
722 {
723 struct sd_softc *sd = (void *)periph->periph_dev;
724 struct disklabel *lp = sd->sc_dk.dk_label;
725 struct buf *bp = 0;
726 struct scsipi_rw_big cmd_big;
727 #if NSD_SCSIBUS > 0
728 struct scsi_rw cmd_small;
729 #endif
730 struct scsipi_generic *cmdp;
731 int nblks, cmdlen, error, flags;
732
733 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
734 /*
735 * Check if the device has room for another command
736 */
737 while (periph->periph_active < periph->periph_openings) {
738 /*
739 * there is excess capacity, but a special waits
740 * It'll need the adapter as soon as we clear out of the
741 * way and let it run (user level wait).
742 */
743 if (periph->periph_flags & PERIPH_WAITING) {
744 periph->periph_flags &= ~PERIPH_WAITING;
745 wakeup((caddr_t)periph);
746 return;
747 }
748
749 /*
750 * See if there is a buf with work for us to do..
751 */
752 if ((bp = BUFQ_GET(&sd->buf_queue)) == NULL)
753 return;
754
755 /*
756 * If the device has become invalid, abort all the
757 * reads and writes until all files have been closed and
758 * re-opened
759 */
760 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
761 bp->b_error = EIO;
762 bp->b_flags |= B_ERROR;
763 bp->b_resid = bp->b_bcount;
764 biodone(bp);
765 continue;
766 }
767
768 /*
769 * We have a buf, now we should make a command.
770 */
771
772 if (lp->d_secsize == DEV_BSIZE)
773 nblks = bp->b_bcount >> DEV_BSHIFT;
774 else
775 nblks = howmany(bp->b_bcount, lp->d_secsize);
776
777 #if NSD_SCSIBUS > 0
778 /*
779 * Fill out the scsi command. If the transfer will
780 * fit in a "small" cdb, use it.
781 */
782 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
783 ((nblks & 0xff) == nblks) &&
784 !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
785 /*
786 * We can fit in a small cdb.
787 */
788 memset(&cmd_small, 0, sizeof(cmd_small));
789 cmd_small.opcode = (bp->b_flags & B_READ) ?
790 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
791 _lto3b(bp->b_rawblkno, cmd_small.addr);
792 cmd_small.length = nblks & 0xff;
793 cmdlen = sizeof(cmd_small);
794 cmdp = (struct scsipi_generic *)&cmd_small;
795 } else
796 #endif /* NSD_SCSIBUS > 0 */
797 {
798 /*
799 * Need a large cdb.
800 */
801 memset(&cmd_big, 0, sizeof(cmd_big));
802 cmd_big.opcode = (bp->b_flags & B_READ) ?
803 READ_BIG : WRITE_BIG;
804 _lto4b(bp->b_rawblkno, cmd_big.addr);
805 _lto2b(nblks, cmd_big.length);
806 cmdlen = sizeof(cmd_big);
807 cmdp = (struct scsipi_generic *)&cmd_big;
808 }
809
810 /* Instrumentation. */
811 disk_busy(&sd->sc_dk);
812
813 /*
814 * Mark the disk dirty so that the cache will be
815 * flushed on close.
816 */
817 if ((bp->b_flags & B_READ) == 0)
818 sd->flags |= SDF_DIRTY;
819
820 /*
821 * Figure out what flags to use.
822 */
823 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
824 if (bp->b_flags & B_READ)
825 flags |= XS_CTL_DATA_IN;
826 else
827 flags |= XS_CTL_DATA_OUT;
828
829 /*
830 * Call the routine that chats with the adapter.
831 * Note: we cannot sleep as we may be an interrupt
832 */
833 error = scsipi_command(periph, cmdp, cmdlen,
834 (u_char *)bp->b_data, bp->b_bcount,
835 SDRETRIES, SD_IO_TIMEOUT, bp, flags);
836 if (error) {
837 disk_unbusy(&sd->sc_dk, 0, 0);
838 printf("%s: not queued, error %d\n",
839 sd->sc_dev.dv_xname, error);
840 }
841 }
842 }
843
844 void
845 sddone(xs)
846 struct scsipi_xfer *xs;
847 {
848 struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
849
850 if (sd->flags & SDF_FLUSHING) {
851 /* Flush completed, no longer dirty. */
852 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
853 }
854
855 if (xs->bp != NULL) {
856 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid,
857 (xs->bp->b_flags & B_READ));
858 #if NRND > 0
859 rnd_add_uint32(&sd->rnd_source, xs->bp->b_rawblkno);
860 #endif
861 }
862 }
863
864 void
865 sdminphys(bp)
866 struct buf *bp;
867 {
868 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
869 long max;
870
871 /*
872 * If the device is ancient, we want to make sure that
873 * the transfer fits into a 6-byte cdb.
874 *
875 * XXX Note that the SCSI-I spec says that 256-block transfers
876 * are allowed in a 6-byte read/write, and are specified
877 * by settng the "length" to 0. However, we're conservative
878 * here, allowing only 255-block transfers in case an
879 * ancient device gets confused by length == 0. A length of 0
880 * in a 10-byte read/write actually means 0 blocks.
881 */
882 if ((sd->flags & SDF_ANCIENT) &&
883 ((sd->sc_periph->periph_flags &
884 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
885 max = sd->sc_dk.dk_label->d_secsize * 0xff;
886
887 if (bp->b_bcount > max)
888 bp->b_bcount = max;
889 }
890
891 (*sd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp);
892 }
893
894 int
895 sdread(dev, uio, ioflag)
896 dev_t dev;
897 struct uio *uio;
898 int ioflag;
899 {
900
901 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
902 }
903
904 int
905 sdwrite(dev, uio, ioflag)
906 dev_t dev;
907 struct uio *uio;
908 int ioflag;
909 {
910
911 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
912 }
913
914 /*
915 * Perform special action on behalf of the user
916 * Knows about the internals of this device
917 */
918 int
919 sdioctl(dev, cmd, addr, flag, p)
920 dev_t dev;
921 u_long cmd;
922 caddr_t addr;
923 int flag;
924 struct proc *p;
925 {
926 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
927 struct scsipi_periph *periph = sd->sc_periph;
928 int part = SDPART(dev);
929 int error = 0;
930 #ifdef __HAVE_OLD_DISKLABEL
931 struct disklabel *newlabel = NULL;
932 #endif
933
934 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
935
936 /*
937 * If the device is not valid, some IOCTLs can still be
938 * handled on the raw partition. Check this here.
939 */
940 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
941 switch (cmd) {
942 case DIOCKLABEL:
943 case DIOCWLABEL:
944 case DIOCLOCK:
945 case DIOCEJECT:
946 case ODIOCEJECT:
947 case DIOCGCACHE:
948 case DIOCSCACHE:
949 case SCIOCIDENTIFY:
950 case OSCIOCIDENTIFY:
951 case SCIOCCOMMAND:
952 case SCIOCDEBUG:
953 if (part == RAW_PART)
954 break;
955 /* FALLTHROUGH */
956 default:
957 if ((periph->periph_flags & PERIPH_OPEN) == 0)
958 return (ENODEV);
959 else
960 return (EIO);
961 }
962 }
963
964 switch (cmd) {
965 case DIOCGDINFO:
966 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
967 return (0);
968
969 #ifdef __HAVE_OLD_DISKLABEL
970 case ODIOCGDINFO:
971 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
972 if (newlabel == NULL)
973 return EIO;
974 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
975 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
976 memcpy(addr, newlabel, sizeof (struct olddisklabel));
977 else
978 error = ENOTTY;
979 free(newlabel, M_TEMP);
980 return error;
981 #endif
982
983 case DIOCGPART:
984 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
985 ((struct partinfo *)addr)->part =
986 &sd->sc_dk.dk_label->d_partitions[part];
987 return (0);
988
989 case DIOCWDINFO:
990 case DIOCSDINFO:
991 #ifdef __HAVE_OLD_DISKLABEL
992 case ODIOCWDINFO:
993 case ODIOCSDINFO:
994 #endif
995 {
996 struct disklabel *lp;
997
998 if ((flag & FWRITE) == 0)
999 return (EBADF);
1000
1001 #ifdef __HAVE_OLD_DISKLABEL
1002 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1003 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1004 if (newlabel == NULL)
1005 return EIO;
1006 memset(newlabel, 0, sizeof newlabel);
1007 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1008 lp = newlabel;
1009 } else
1010 #endif
1011 lp = (struct disklabel *)addr;
1012
1013 if ((error = sdlock(sd)) != 0)
1014 goto bad;
1015 sd->flags |= SDF_LABELLING;
1016
1017 error = setdisklabel(sd->sc_dk.dk_label,
1018 lp, /*sd->sc_dk.dk_openmask : */0,
1019 sd->sc_dk.dk_cpulabel);
1020 if (error == 0) {
1021 if (cmd == DIOCWDINFO
1022 #ifdef __HAVE_OLD_DISKLABEL
1023 || cmd == ODIOCWDINFO
1024 #endif
1025 )
1026 error = writedisklabel(SDLABELDEV(dev),
1027 sdstrategy, sd->sc_dk.dk_label,
1028 sd->sc_dk.dk_cpulabel);
1029 }
1030
1031 sd->flags &= ~SDF_LABELLING;
1032 sdunlock(sd);
1033 bad:
1034 #ifdef __HAVE_OLD_DISKLABEL
1035 if (newlabel != NULL)
1036 free(newlabel, M_TEMP);
1037 #endif
1038 return (error);
1039 }
1040
1041 case DIOCKLABEL:
1042 if (*(int *)addr)
1043 periph->periph_flags |= PERIPH_KEEP_LABEL;
1044 else
1045 periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1046 return (0);
1047
1048 case DIOCWLABEL:
1049 if ((flag & FWRITE) == 0)
1050 return (EBADF);
1051 if (*(int *)addr)
1052 sd->flags |= SDF_WLABEL;
1053 else
1054 sd->flags &= ~SDF_WLABEL;
1055 return (0);
1056
1057 case DIOCLOCK:
1058 return (scsipi_prevent(periph,
1059 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1060
1061 case DIOCEJECT:
1062 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1063 return (ENOTTY);
1064 if (*(int *)addr == 0) {
1065 /*
1066 * Don't force eject: check that we are the only
1067 * partition open. If so, unlock it.
1068 */
1069 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1070 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1071 sd->sc_dk.dk_openmask) {
1072 error = scsipi_prevent(periph, PR_ALLOW,
1073 XS_CTL_IGNORE_NOT_READY);
1074 if (error)
1075 return (error);
1076 } else {
1077 return (EBUSY);
1078 }
1079 }
1080 /* FALLTHROUGH */
1081 case ODIOCEJECT:
1082 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1083 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1084
1085 case DIOCGDEFLABEL:
1086 sdgetdefaultlabel(sd, (struct disklabel *)addr);
1087 return (0);
1088
1089 #ifdef __HAVE_OLD_DISKLABEL
1090 case ODIOCGDEFLABEL:
1091 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1092 if (newlabel == NULL)
1093 return EIO;
1094 sdgetdefaultlabel(sd, newlabel);
1095 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1096 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1097 else
1098 error = ENOTTY;
1099 free(newlabel, M_TEMP);
1100 return error;
1101 #endif
1102
1103 case DIOCGCACHE:
1104 if (sd->sc_ops->sdo_getcache != NULL)
1105 return ((*sd->sc_ops->sdo_getcache)(sd, (int *) addr));
1106
1107 /* Not supported on this device. */
1108 *(int *) addr = 0;
1109 return (0);
1110
1111 case DIOCSCACHE:
1112 if ((flag & FWRITE) == 0)
1113 return (EBADF);
1114 if (sd->sc_ops->sdo_setcache != NULL)
1115 return ((*sd->sc_ops->sdo_setcache)(sd, *(int *) addr));
1116
1117 /* Not supported on this device. */
1118 return (EOPNOTSUPP);
1119
1120 case DIOCCACHESYNC:
1121 /*
1122 * XXX Do we really need to care about having a writable
1123 * file descriptor here?
1124 */
1125 if ((flag & FWRITE) == 0)
1126 return (EBADF);
1127 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0) &&
1128 sd->sc_ops->sdo_flush != NULL) {
1129 error = (*sd->sc_ops->sdo_flush)(sd, 0);
1130 if (error)
1131 sd->flags &= ~SDF_FLUSHING;
1132 else
1133 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1134 } else
1135 error = 0;
1136 return (error);
1137
1138 default:
1139 if (part != RAW_PART)
1140 return (ENOTTY);
1141 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
1142 }
1143
1144 #ifdef DIAGNOSTIC
1145 panic("sdioctl: impossible");
1146 #endif
1147 }
1148
1149 void
1150 sdgetdefaultlabel(sd, lp)
1151 struct sd_softc *sd;
1152 struct disklabel *lp;
1153 {
1154
1155 memset(lp, 0, sizeof(struct disklabel));
1156
1157 lp->d_secsize = sd->params.blksize;
1158 lp->d_ntracks = sd->params.heads;
1159 lp->d_nsectors = sd->params.sectors;
1160 lp->d_ncylinders = sd->params.cyls;
1161 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1162
1163 switch (scsipi_periph_bustype(sd->sc_periph)) {
1164 #if NSD_SCSIBUS > 0
1165 case SCSIPI_BUSTYPE_SCSI:
1166 lp->d_type = DTYPE_SCSI;
1167 break;
1168 #endif
1169 #if NSD_ATAPIBUS > 0
1170 case SCSIPI_BUSTYPE_ATAPI:
1171 lp->d_type = DTYPE_ATAPI;
1172 break;
1173 #endif
1174 }
1175 strncpy(lp->d_typename, sd->name, 16);
1176 strncpy(lp->d_packname, "fictitious", 16);
1177 lp->d_secperunit = sd->params.disksize;
1178 lp->d_rpm = sd->params.rot_rate;
1179 lp->d_interleave = 1;
1180 lp->d_flags = 0;
1181
1182 lp->d_partitions[RAW_PART].p_offset = 0;
1183 lp->d_partitions[RAW_PART].p_size =
1184 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1185 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1186 lp->d_npartitions = RAW_PART + 1;
1187
1188 lp->d_magic = DISKMAGIC;
1189 lp->d_magic2 = DISKMAGIC;
1190 lp->d_checksum = dkcksum(lp);
1191 }
1192
1193
1194 /*
1195 * Load the label information on the named device
1196 */
1197 void
1198 sdgetdisklabel(sd)
1199 struct sd_softc *sd;
1200 {
1201 struct disklabel *lp = sd->sc_dk.dk_label;
1202 const char *errstring;
1203
1204 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1205
1206 sdgetdefaultlabel(sd, lp);
1207
1208 if (lp->d_secpercyl == 0) {
1209 lp->d_secpercyl = 100;
1210 /* as long as it's not 0 - readdisklabel divides by it (?) */
1211 }
1212
1213 /*
1214 * Call the generic disklabel extraction routine
1215 */
1216 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
1217 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1218 if (errstring) {
1219 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
1220 return;
1221 }
1222 }
1223
1224 void
1225 sd_shutdown(arg)
1226 void *arg;
1227 {
1228 struct sd_softc *sd = arg;
1229
1230 /*
1231 * If the disk cache needs to be flushed, and the disk supports
1232 * it, flush it. We're cold at this point, so we poll for
1233 * completion.
1234 */
1235 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) {
1236 if ((*sd->sc_ops->sdo_flush)(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1237 printf("%s: cache synchronization failed\n",
1238 sd->sc_dev.dv_xname);
1239 sd->flags &= ~SDF_FLUSHING;
1240 } else
1241 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1242 }
1243 }
1244
1245 /*
1246 * Tell the device to map out a defective block
1247 */
1248 int
1249 sd_reassign_blocks(sd, blkno)
1250 struct sd_softc *sd;
1251 u_long blkno;
1252 {
1253 struct scsi_reassign_blocks scsipi_cmd;
1254 struct scsi_reassign_blocks_data rbdata;
1255
1256 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1257 memset(&rbdata, 0, sizeof(rbdata));
1258 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
1259
1260 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
1261 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
1262
1263 return (scsipi_command(sd->sc_periph,
1264 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1265 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
1266 XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK));
1267 }
1268
1269 /*
1270 * Check Errors
1271 */
1272 int
1273 sd_interpret_sense(xs)
1274 struct scsipi_xfer *xs;
1275 {
1276 struct scsipi_periph *periph = xs->xs_periph;
1277 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
1278 struct sd_softc *sd = (void *)periph->periph_dev;
1279 int s, error, retval = EJUSTRETURN;
1280
1281 /*
1282 * If the periph is already recovering, just do the normal
1283 * error processing.
1284 */
1285 if (periph->periph_flags & PERIPH_RECOVERING)
1286 return (retval);
1287
1288 /*
1289 * If the device is not open yet, let the generic code handle it.
1290 */
1291 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1292 return (retval);
1293
1294 /*
1295 * If it isn't a extended or extended/deferred error, let
1296 * the generic code handle it.
1297 */
1298 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
1299 (sense->error_code & SSD_ERRCODE) != 0x71)
1300 return (retval);
1301
1302 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
1303 sense->add_sense_code == 0x4) {
1304 if (sense->add_sense_code_qual == 0x01) {
1305 /*
1306 * Unit In The Process Of Becoming Ready.
1307 */
1308 printf("%s: waiting for pack to spin up...\n",
1309 sd->sc_dev.dv_xname);
1310 if (!callout_pending(&periph->periph_callout))
1311 scsipi_periph_freeze(periph, 1);
1312 callout_reset(&periph->periph_callout,
1313 5 * hz, scsipi_periph_timed_thaw, periph);
1314 retval = ERESTART;
1315 } else if (sense->add_sense_code_qual == 0x02) {
1316 printf("%s: pack is stopped, restarting...\n",
1317 sd->sc_dev.dv_xname);
1318 s = splbio();
1319 periph->periph_flags |= PERIPH_RECOVERING;
1320 splx(s);
1321 error = scsipi_start(periph, SSS_START,
1322 XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1323 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1324 if (error) {
1325 printf("%s: unable to restart pack\n",
1326 sd->sc_dev.dv_xname);
1327 retval = error;
1328 } else
1329 retval = ERESTART;
1330 s = splbio();
1331 periph->periph_flags &= ~PERIPH_RECOVERING;
1332 splx(s);
1333 }
1334 }
1335 return (retval);
1336 }
1337
1338
1339 int
1340 sdsize(dev)
1341 dev_t dev;
1342 {
1343 struct sd_softc *sd;
1344 int part, unit, omask;
1345 int size;
1346
1347 unit = SDUNIT(dev);
1348 if (unit >= sd_cd.cd_ndevs)
1349 return (-1);
1350 sd = sd_cd.cd_devs[unit];
1351 if (sd == NULL)
1352 return (-1);
1353
1354 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1355 return (-1);
1356
1357 part = SDPART(dev);
1358 omask = sd->sc_dk.dk_openmask & (1 << part);
1359
1360 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1361 return (-1);
1362 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1363 size = -1;
1364 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1365 size = -1;
1366 else
1367 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1368 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1369 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1370 return (-1);
1371 return (size);
1372 }
1373
1374 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1375 static struct scsipi_xfer sx;
1376 static int sddoingadump;
1377
1378 /*
1379 * dump all of physical memory into the partition specified, starting
1380 * at offset 'dumplo' into the partition.
1381 */
1382 int
1383 sddump(dev, blkno, va, size)
1384 dev_t dev;
1385 daddr_t blkno;
1386 caddr_t va;
1387 size_t size;
1388 {
1389 struct sd_softc *sd; /* disk unit to do the I/O */
1390 struct disklabel *lp; /* disk's disklabel */
1391 int unit, part;
1392 int sectorsize; /* size of a disk sector */
1393 int nsects; /* number of sectors in partition */
1394 int sectoff; /* sector offset of partition */
1395 int totwrt; /* total number of sectors left to write */
1396 int nwrt; /* current number of sectors to write */
1397 struct scsipi_rw_big cmd; /* write command */
1398 struct scsipi_xfer *xs; /* ... convenience */
1399 struct scsipi_periph *periph;
1400 struct scsipi_channel *chan;
1401
1402 /* Check if recursive dump; if so, punt. */
1403 if (sddoingadump)
1404 return (EFAULT);
1405
1406 /* Mark as active early. */
1407 sddoingadump = 1;
1408
1409 unit = SDUNIT(dev); /* Decompose unit & partition. */
1410 part = SDPART(dev);
1411
1412 /* Check for acceptable drive number. */
1413 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1414 return (ENXIO);
1415
1416 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1417 return (ENODEV);
1418
1419 periph = sd->sc_periph;
1420 chan = periph->periph_channel;
1421
1422 /* Make sure it was initialized. */
1423 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1424 return (ENXIO);
1425
1426 /* Convert to disk sectors. Request must be a multiple of size. */
1427 lp = sd->sc_dk.dk_label;
1428 sectorsize = lp->d_secsize;
1429 if ((size % sectorsize) != 0)
1430 return (EFAULT);
1431 totwrt = size / sectorsize;
1432 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1433
1434 nsects = lp->d_partitions[part].p_size;
1435 sectoff = lp->d_partitions[part].p_offset;
1436
1437 /* Check transfer bounds against partition size. */
1438 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1439 return (EINVAL);
1440
1441 /* Offset block number to start of partition. */
1442 blkno += sectoff;
1443
1444 xs = &sx;
1445
1446 while (totwrt > 0) {
1447 nwrt = totwrt; /* XXX */
1448 #ifndef SD_DUMP_NOT_TRUSTED
1449 /*
1450 * Fill out the scsi command
1451 */
1452 memset(&cmd, 0, sizeof(cmd));
1453 cmd.opcode = WRITE_BIG;
1454 _lto4b(blkno, cmd.addr);
1455 _lto2b(nwrt, cmd.length);
1456 /*
1457 * Fill out the scsipi_xfer structure
1458 * Note: we cannot sleep as we may be an interrupt
1459 * don't use scsipi_command() as it may want to wait
1460 * for an xs.
1461 */
1462 memset(xs, 0, sizeof(sx));
1463 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1464 XS_CTL_DATA_OUT;
1465 xs->xs_status = 0;
1466 xs->xs_periph = periph;
1467 xs->xs_retries = SDRETRIES;
1468 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1469 xs->cmd = (struct scsipi_generic *)&cmd;
1470 xs->cmdlen = sizeof(cmd);
1471 xs->resid = nwrt * sectorsize;
1472 xs->error = XS_NOERROR;
1473 xs->bp = 0;
1474 xs->data = va;
1475 xs->datalen = nwrt * sectorsize;
1476
1477 /*
1478 * Pass all this info to the scsi driver.
1479 */
1480 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1481 if ((xs->xs_status & XS_STS_DONE) == 0 ||
1482 xs->error != XS_NOERROR)
1483 return (EIO);
1484 #else /* SD_DUMP_NOT_TRUSTED */
1485 /* Let's just talk about this first... */
1486 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1487 delay(500 * 1000); /* half a second */
1488 #endif /* SD_DUMP_NOT_TRUSTED */
1489
1490 /* update block count */
1491 totwrt -= nwrt;
1492 blkno += nwrt;
1493 va += sectorsize * nwrt;
1494 }
1495 sddoingadump = 0;
1496 return (0);
1497 }
1498