sd.c revision 1.208 1 /* $NetBSD: sd.c,v 1.208 2003/09/13 15:49:05 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.208 2003/09/13 15:49:05 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 int silent;
415
416 if (part == RAW_PART && fmt == S_IFCHR)
417 silent = XS_CTL_SILENT;
418 else
419 silent = 0;
420
421 /* Check that it is still responding and ok. */
422 error = scsipi_test_unit_ready(periph,
423 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_MEDIA_CHANGE |
424 silent);
425
426 /*
427 * Start the pack spinning if necessary. Always allow the
428 * raw parition to be opened, for raw IOCTLs. Data transfers
429 * will check for SDEV_MEDIA_LOADED.
430 */
431 if (error == EIO) {
432 int error2;
433
434 error2 = scsipi_start(periph, SSS_START, silent);
435 switch (error2) {
436 case 0:
437 error = 0;
438 break;
439 case EIO:
440 case EINVAL:
441 break;
442 default:
443 error = error2;
444 break;
445 }
446 }
447 if (error) {
448 if (silent)
449 goto out;
450 goto bad3;
451 }
452
453 periph->periph_flags |= PERIPH_OPEN;
454
455 if (periph->periph_flags & PERIPH_REMOVABLE) {
456 /* Lock the pack in. */
457 error = scsipi_prevent(periph, PR_PREVENT,
458 XS_CTL_IGNORE_ILLEGAL_REQUEST |
459 XS_CTL_IGNORE_MEDIA_CHANGE);
460 if (error)
461 goto bad;
462 }
463
464 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
465 periph->periph_flags |= PERIPH_MEDIA_LOADED;
466
467 /*
468 * Load the physical device parameters.
469 *
470 * Note that if media is present but unformatted,
471 * we allow the open (so that it can be formatted!).
472 * The drive should refuse real I/O, if the media is
473 * unformatted.
474 */
475 if ((*sd->sc_ops->sdo_get_parms)(sd, &sd->params,
476 0) == SDGP_RESULT_OFFLINE) {
477 error = ENXIO;
478 goto bad2;
479 }
480 SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
481
482 /* Load the partition info if not already loaded. */
483 sdgetdisklabel(sd);
484 SC_DEBUG(periph, SCSIPI_DB3, ("Disklabel loaded "));
485 }
486 }
487
488 /* Check that the partition exists. */
489 if (part != RAW_PART &&
490 (part >= sd->sc_dk.dk_label->d_npartitions ||
491 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
492 error = ENXIO;
493 goto bad;
494 }
495
496 out: /* Insure only one open at a time. */
497 switch (fmt) {
498 case S_IFCHR:
499 sd->sc_dk.dk_copenmask |= (1 << part);
500 break;
501 case S_IFBLK:
502 sd->sc_dk.dk_bopenmask |= (1 << part);
503 break;
504 }
505 sd->sc_dk.dk_openmask =
506 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
507
508 SC_DEBUG(periph, SCSIPI_DB3, ("open complete\n"));
509 sdunlock(sd);
510 return (0);
511
512 bad2:
513 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
514
515 bad:
516 if (sd->sc_dk.dk_openmask == 0) {
517 if (periph->periph_flags & PERIPH_REMOVABLE)
518 scsipi_prevent(periph, PR_ALLOW,
519 XS_CTL_IGNORE_ILLEGAL_REQUEST |
520 XS_CTL_IGNORE_MEDIA_CHANGE);
521 periph->periph_flags &= ~PERIPH_OPEN;
522 }
523
524 bad3:
525 sdunlock(sd);
526 bad4:
527 if (sd->sc_dk.dk_openmask == 0)
528 scsipi_adapter_delref(adapt);
529 return (error);
530 }
531
532 /*
533 * close the device.. only called if we are the LAST occurence of an open
534 * device. Convenient now but usually a pain.
535 */
536 int
537 sdclose(dev, flag, fmt, p)
538 dev_t dev;
539 int flag, fmt;
540 struct proc *p;
541 {
542 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
543 struct scsipi_periph *periph = sd->sc_periph;
544 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
545 int part = SDPART(dev);
546 int error;
547
548 if ((error = sdlock(sd)) != 0)
549 return (error);
550
551 switch (fmt) {
552 case S_IFCHR:
553 sd->sc_dk.dk_copenmask &= ~(1 << part);
554 break;
555 case S_IFBLK:
556 sd->sc_dk.dk_bopenmask &= ~(1 << part);
557 break;
558 }
559 sd->sc_dk.dk_openmask =
560 sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
561
562 if (sd->sc_dk.dk_openmask == 0) {
563 /*
564 * If the disk cache needs flushing, and the disk supports
565 * it, do it now.
566 */
567 if ((sd->flags & SDF_DIRTY) != 0 &&
568 sd->sc_ops->sdo_flush != NULL) {
569 if ((*sd->sc_ops->sdo_flush)(sd, 0)) {
570 printf("%s: cache synchronization failed\n",
571 sd->sc_dev.dv_xname);
572 sd->flags &= ~SDF_FLUSHING;
573 } else
574 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
575 }
576
577 if (! (periph->periph_flags & PERIPH_KEEP_LABEL))
578 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
579
580 scsipi_wait_drain(periph);
581
582 if (periph->periph_flags & PERIPH_REMOVABLE)
583 scsipi_prevent(periph, PR_ALLOW,
584 XS_CTL_IGNORE_ILLEGAL_REQUEST |
585 XS_CTL_IGNORE_NOT_READY);
586 periph->periph_flags &= ~PERIPH_OPEN;
587
588 scsipi_wait_drain(periph);
589
590 scsipi_adapter_delref(adapt);
591 }
592
593 sdunlock(sd);
594 return (0);
595 }
596
597 /*
598 * Actually translate the requested transfer into one the physical driver
599 * can understand. The transfer is described by a buf and will include
600 * only one physical transfer.
601 */
602 void
603 sdstrategy(bp)
604 struct buf *bp;
605 {
606 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
607 struct scsipi_periph *periph = sd->sc_periph;
608 struct disklabel *lp;
609 daddr_t blkno;
610 int s;
611 boolean_t sector_aligned;
612
613 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdstrategy "));
614 SC_DEBUG(sd->sc_periph, SCSIPI_DB1,
615 ("%ld bytes @ blk %" PRId64 "\n", bp->b_bcount, bp->b_blkno));
616 /*
617 * If the device has been made invalid, error out
618 */
619 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0 ||
620 (sd->sc_dev.dv_flags & DVF_ACTIVE) == 0) {
621 if (periph->periph_flags & PERIPH_OPEN)
622 bp->b_error = EIO;
623 else
624 bp->b_error = ENODEV;
625 goto bad;
626 }
627
628 lp = sd->sc_dk.dk_label;
629
630 /*
631 * The transfer must be a whole number of blocks, offset must not be
632 * negative.
633 */
634 if (lp->d_secsize == DEV_BSIZE) {
635 sector_aligned = (bp->b_bcount & (DEV_BSIZE - 1)) == 0;
636 } else {
637 sector_aligned = (bp->b_bcount % lp->d_secsize) == 0;
638 }
639 if (!sector_aligned || bp->b_blkno < 0) {
640 bp->b_error = EINVAL;
641 goto bad;
642 }
643 /*
644 * If it's a null transfer, return immediatly
645 */
646 if (bp->b_bcount == 0)
647 goto done;
648
649 /*
650 * Do bounds checking, adjust transfer. if error, process.
651 * If end of partition, just return.
652 */
653 if (SDPART(bp->b_dev) == RAW_PART) {
654 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
655 sd->params.disksize512) <= 0)
656 goto done;
657 } else {
658 if (bounds_check_with_label(&sd->sc_dk, bp,
659 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
660 goto done;
661 }
662
663 /*
664 * Now convert the block number to absolute and put it in
665 * terms of the device's logical block size.
666 */
667 if (lp->d_secsize == DEV_BSIZE)
668 blkno = bp->b_blkno;
669 else if (lp->d_secsize > DEV_BSIZE)
670 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
671 else
672 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
673
674 if (SDPART(bp->b_dev) != RAW_PART)
675 blkno += lp->d_partitions[SDPART(bp->b_dev)].p_offset;
676
677 bp->b_rawblkno = blkno;
678
679 s = splbio();
680
681 /*
682 * Place it in the queue of disk activities for this disk.
683 *
684 * XXX Only do disksort() if the current operating mode does not
685 * XXX include tagged queueing.
686 */
687 BUFQ_PUT(&sd->buf_queue, bp);
688
689 /*
690 * Tell the device to get going on the transfer if it's
691 * not doing anything, otherwise just wait for completion
692 */
693 sdstart(sd->sc_periph);
694
695 splx(s);
696 return;
697
698 bad:
699 bp->b_flags |= B_ERROR;
700 done:
701 /*
702 * Correctly set the buf to indicate a completed xfer
703 */
704 bp->b_resid = bp->b_bcount;
705 biodone(bp);
706 }
707
708 /*
709 * sdstart looks to see if there is a buf waiting for the device
710 * and that the device is not already busy. If both are true,
711 * It dequeues the buf and creates a scsi command to perform the
712 * transfer in the buf. The transfer request will call scsipi_done
713 * on completion, which will in turn call this routine again
714 * so that the next queued transfer is performed.
715 * The bufs are queued by the strategy routine (sdstrategy)
716 *
717 * This routine is also called after other non-queued requests
718 * have been made of the scsi driver, to ensure that the queue
719 * continues to be drained.
720 *
721 * must be called at the correct (highish) spl level
722 * sdstart() is called at splbio from sdstrategy and scsipi_done
723 */
724 void
725 sdstart(periph)
726 struct scsipi_periph *periph;
727 {
728 struct sd_softc *sd = (void *)periph->periph_dev;
729 struct disklabel *lp = sd->sc_dk.dk_label;
730 struct buf *bp = 0;
731 struct scsipi_rw_big cmd_big;
732 #if NSD_SCSIBUS > 0
733 struct scsi_rw cmd_small;
734 #endif
735 struct scsipi_generic *cmdp;
736 int nblks, cmdlen, error, flags;
737
738 SC_DEBUG(periph, SCSIPI_DB2, ("sdstart "));
739 /*
740 * Check if the device has room for another command
741 */
742 while (periph->periph_active < periph->periph_openings) {
743 /*
744 * there is excess capacity, but a special waits
745 * It'll need the adapter as soon as we clear out of the
746 * way and let it run (user level wait).
747 */
748 if (periph->periph_flags & PERIPH_WAITING) {
749 periph->periph_flags &= ~PERIPH_WAITING;
750 wakeup((caddr_t)periph);
751 return;
752 }
753
754 /*
755 * See if there is a buf with work for us to do..
756 */
757 if ((bp = BUFQ_GET(&sd->buf_queue)) == NULL)
758 return;
759
760 /*
761 * If the device has become invalid, abort all the
762 * reads and writes until all files have been closed and
763 * re-opened
764 */
765 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
766 bp->b_error = EIO;
767 bp->b_flags |= B_ERROR;
768 bp->b_resid = bp->b_bcount;
769 biodone(bp);
770 continue;
771 }
772
773 /*
774 * We have a buf, now we should make a command.
775 */
776
777 if (lp->d_secsize == DEV_BSIZE)
778 nblks = bp->b_bcount >> DEV_BSHIFT;
779 else
780 nblks = howmany(bp->b_bcount, lp->d_secsize);
781
782 #if NSD_SCSIBUS > 0
783 /*
784 * Fill out the scsi command. If the transfer will
785 * fit in a "small" cdb, use it.
786 */
787 if (((bp->b_rawblkno & 0x1fffff) == bp->b_rawblkno) &&
788 ((nblks & 0xff) == nblks) &&
789 !(periph->periph_quirks & PQUIRK_ONLYBIG)) {
790 /*
791 * We can fit in a small cdb.
792 */
793 memset(&cmd_small, 0, sizeof(cmd_small));
794 cmd_small.opcode = (bp->b_flags & B_READ) ?
795 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
796 _lto3b(bp->b_rawblkno, cmd_small.addr);
797 cmd_small.length = nblks & 0xff;
798 cmdlen = sizeof(cmd_small);
799 cmdp = (struct scsipi_generic *)&cmd_small;
800 } else
801 #endif /* NSD_SCSIBUS > 0 */
802 {
803 /*
804 * Need a large cdb.
805 */
806 memset(&cmd_big, 0, sizeof(cmd_big));
807 cmd_big.opcode = (bp->b_flags & B_READ) ?
808 READ_BIG : WRITE_BIG;
809 _lto4b(bp->b_rawblkno, cmd_big.addr);
810 _lto2b(nblks, cmd_big.length);
811 cmdlen = sizeof(cmd_big);
812 cmdp = (struct scsipi_generic *)&cmd_big;
813 }
814
815 /* Instrumentation. */
816 disk_busy(&sd->sc_dk);
817
818 /*
819 * Mark the disk dirty so that the cache will be
820 * flushed on close.
821 */
822 if ((bp->b_flags & B_READ) == 0)
823 sd->flags |= SDF_DIRTY;
824
825 /*
826 * Figure out what flags to use.
827 */
828 flags = XS_CTL_NOSLEEP|XS_CTL_ASYNC|XS_CTL_SIMPLE_TAG;
829 if (bp->b_flags & B_READ)
830 flags |= XS_CTL_DATA_IN;
831 else
832 flags |= XS_CTL_DATA_OUT;
833
834 /*
835 * Call the routine that chats with the adapter.
836 * Note: we cannot sleep as we may be an interrupt
837 */
838 error = scsipi_command(periph, cmdp, cmdlen,
839 (u_char *)bp->b_data, bp->b_bcount,
840 SDRETRIES, SD_IO_TIMEOUT, bp, flags);
841 if (error) {
842 disk_unbusy(&sd->sc_dk, 0, 0);
843 printf("%s: not queued, error %d\n",
844 sd->sc_dev.dv_xname, error);
845 }
846 }
847 }
848
849 void
850 sddone(xs)
851 struct scsipi_xfer *xs;
852 {
853 struct sd_softc *sd = (void *)xs->xs_periph->periph_dev;
854
855 if (sd->flags & SDF_FLUSHING) {
856 /* Flush completed, no longer dirty. */
857 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
858 }
859
860 if (xs->bp != NULL) {
861 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid,
862 (xs->bp->b_flags & B_READ));
863 #if NRND > 0
864 rnd_add_uint32(&sd->rnd_source, xs->bp->b_rawblkno);
865 #endif
866 }
867 }
868
869 void
870 sdminphys(bp)
871 struct buf *bp;
872 {
873 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
874 long max;
875
876 /*
877 * If the device is ancient, we want to make sure that
878 * the transfer fits into a 6-byte cdb.
879 *
880 * XXX Note that the SCSI-I spec says that 256-block transfers
881 * are allowed in a 6-byte read/write, and are specified
882 * by settng the "length" to 0. However, we're conservative
883 * here, allowing only 255-block transfers in case an
884 * ancient device gets confused by length == 0. A length of 0
885 * in a 10-byte read/write actually means 0 blocks.
886 */
887 if ((sd->flags & SDF_ANCIENT) &&
888 ((sd->sc_periph->periph_flags &
889 (PERIPH_REMOVABLE | PERIPH_MEDIA_LOADED)) != PERIPH_REMOVABLE)) {
890 max = sd->sc_dk.dk_label->d_secsize * 0xff;
891
892 if (bp->b_bcount > max)
893 bp->b_bcount = max;
894 }
895
896 (*sd->sc_periph->periph_channel->chan_adapter->adapt_minphys)(bp);
897 }
898
899 int
900 sdread(dev, uio, ioflag)
901 dev_t dev;
902 struct uio *uio;
903 int ioflag;
904 {
905
906 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
907 }
908
909 int
910 sdwrite(dev, uio, ioflag)
911 dev_t dev;
912 struct uio *uio;
913 int ioflag;
914 {
915
916 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
917 }
918
919 /*
920 * Perform special action on behalf of the user
921 * Knows about the internals of this device
922 */
923 int
924 sdioctl(dev, cmd, addr, flag, p)
925 dev_t dev;
926 u_long cmd;
927 caddr_t addr;
928 int flag;
929 struct proc *p;
930 {
931 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
932 struct scsipi_periph *periph = sd->sc_periph;
933 int part = SDPART(dev);
934 int error = 0;
935 #ifdef __HAVE_OLD_DISKLABEL
936 struct disklabel *newlabel = NULL;
937 #endif
938
939 SC_DEBUG(sd->sc_periph, SCSIPI_DB2, ("sdioctl 0x%lx ", cmd));
940
941 /*
942 * If the device is not valid, some IOCTLs can still be
943 * handled on the raw partition. Check this here.
944 */
945 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
946 switch (cmd) {
947 case DIOCKLABEL:
948 case DIOCWLABEL:
949 case DIOCLOCK:
950 case DIOCEJECT:
951 case ODIOCEJECT:
952 case DIOCGCACHE:
953 case DIOCSCACHE:
954 case SCIOCIDENTIFY:
955 case OSCIOCIDENTIFY:
956 case SCIOCCOMMAND:
957 case SCIOCDEBUG:
958 if (part == RAW_PART)
959 break;
960 /* FALLTHROUGH */
961 default:
962 if ((periph->periph_flags & PERIPH_OPEN) == 0)
963 return (ENODEV);
964 else
965 return (EIO);
966 }
967 }
968
969 switch (cmd) {
970 case DIOCGDINFO:
971 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
972 return (0);
973
974 #ifdef __HAVE_OLD_DISKLABEL
975 case ODIOCGDINFO:
976 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
977 if (newlabel == NULL)
978 return EIO;
979 memcpy(newlabel, sd->sc_dk.dk_label, sizeof (*newlabel));
980 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
981 memcpy(addr, newlabel, sizeof (struct olddisklabel));
982 else
983 error = ENOTTY;
984 free(newlabel, M_TEMP);
985 return error;
986 #endif
987
988 case DIOCGPART:
989 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
990 ((struct partinfo *)addr)->part =
991 &sd->sc_dk.dk_label->d_partitions[part];
992 return (0);
993
994 case DIOCWDINFO:
995 case DIOCSDINFO:
996 #ifdef __HAVE_OLD_DISKLABEL
997 case ODIOCWDINFO:
998 case ODIOCSDINFO:
999 #endif
1000 {
1001 struct disklabel *lp;
1002
1003 if ((flag & FWRITE) == 0)
1004 return (EBADF);
1005
1006 #ifdef __HAVE_OLD_DISKLABEL
1007 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
1008 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1009 if (newlabel == NULL)
1010 return EIO;
1011 memset(newlabel, 0, sizeof newlabel);
1012 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1013 lp = newlabel;
1014 } else
1015 #endif
1016 lp = (struct disklabel *)addr;
1017
1018 if ((error = sdlock(sd)) != 0)
1019 goto bad;
1020 sd->flags |= SDF_LABELLING;
1021
1022 error = setdisklabel(sd->sc_dk.dk_label,
1023 lp, /*sd->sc_dk.dk_openmask : */0,
1024 sd->sc_dk.dk_cpulabel);
1025 if (error == 0) {
1026 if (cmd == DIOCWDINFO
1027 #ifdef __HAVE_OLD_DISKLABEL
1028 || cmd == ODIOCWDINFO
1029 #endif
1030 )
1031 error = writedisklabel(SDLABELDEV(dev),
1032 sdstrategy, sd->sc_dk.dk_label,
1033 sd->sc_dk.dk_cpulabel);
1034 }
1035
1036 sd->flags &= ~SDF_LABELLING;
1037 sdunlock(sd);
1038 bad:
1039 #ifdef __HAVE_OLD_DISKLABEL
1040 if (newlabel != NULL)
1041 free(newlabel, M_TEMP);
1042 #endif
1043 return (error);
1044 }
1045
1046 case DIOCKLABEL:
1047 if (*(int *)addr)
1048 periph->periph_flags |= PERIPH_KEEP_LABEL;
1049 else
1050 periph->periph_flags &= ~PERIPH_KEEP_LABEL;
1051 return (0);
1052
1053 case DIOCWLABEL:
1054 if ((flag & FWRITE) == 0)
1055 return (EBADF);
1056 if (*(int *)addr)
1057 sd->flags |= SDF_WLABEL;
1058 else
1059 sd->flags &= ~SDF_WLABEL;
1060 return (0);
1061
1062 case DIOCLOCK:
1063 return (scsipi_prevent(periph,
1064 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0));
1065
1066 case DIOCEJECT:
1067 if ((periph->periph_flags & PERIPH_REMOVABLE) == 0)
1068 return (ENOTTY);
1069 if (*(int *)addr == 0) {
1070 /*
1071 * Don't force eject: check that we are the only
1072 * partition open. If so, unlock it.
1073 */
1074 if ((sd->sc_dk.dk_openmask & ~(1 << part)) == 0 &&
1075 sd->sc_dk.dk_bopenmask + sd->sc_dk.dk_copenmask ==
1076 sd->sc_dk.dk_openmask) {
1077 error = scsipi_prevent(periph, PR_ALLOW,
1078 XS_CTL_IGNORE_NOT_READY);
1079 if (error)
1080 return (error);
1081 } else {
1082 return (EBUSY);
1083 }
1084 }
1085 /* FALLTHROUGH */
1086 case ODIOCEJECT:
1087 return ((periph->periph_flags & PERIPH_REMOVABLE) == 0 ?
1088 ENOTTY : scsipi_start(periph, SSS_STOP|SSS_LOEJ, 0));
1089
1090 case DIOCGDEFLABEL:
1091 sdgetdefaultlabel(sd, (struct disklabel *)addr);
1092 return (0);
1093
1094 #ifdef __HAVE_OLD_DISKLABEL
1095 case ODIOCGDEFLABEL:
1096 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1097 if (newlabel == NULL)
1098 return EIO;
1099 sdgetdefaultlabel(sd, newlabel);
1100 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1101 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1102 else
1103 error = ENOTTY;
1104 free(newlabel, M_TEMP);
1105 return error;
1106 #endif
1107
1108 case DIOCGCACHE:
1109 if (sd->sc_ops->sdo_getcache != NULL)
1110 return ((*sd->sc_ops->sdo_getcache)(sd, (int *) addr));
1111
1112 /* Not supported on this device. */
1113 *(int *) addr = 0;
1114 return (0);
1115
1116 case DIOCSCACHE:
1117 if ((flag & FWRITE) == 0)
1118 return (EBADF);
1119 if (sd->sc_ops->sdo_setcache != NULL)
1120 return ((*sd->sc_ops->sdo_setcache)(sd, *(int *) addr));
1121
1122 /* Not supported on this device. */
1123 return (EOPNOTSUPP);
1124
1125 case DIOCCACHESYNC:
1126 /*
1127 * XXX Do we really need to care about having a writable
1128 * file descriptor here?
1129 */
1130 if ((flag & FWRITE) == 0)
1131 return (EBADF);
1132 if (((sd->flags & SDF_DIRTY) != 0 || *(int *)addr != 0) &&
1133 sd->sc_ops->sdo_flush != NULL) {
1134 error = (*sd->sc_ops->sdo_flush)(sd, 0);
1135 if (error)
1136 sd->flags &= ~SDF_FLUSHING;
1137 else
1138 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1139 } else
1140 error = 0;
1141 return (error);
1142
1143 default:
1144 if (part != RAW_PART)
1145 return (ENOTTY);
1146 return (scsipi_do_ioctl(periph, dev, cmd, addr, flag, p));
1147 }
1148
1149 #ifdef DIAGNOSTIC
1150 panic("sdioctl: impossible");
1151 #endif
1152 }
1153
1154 void
1155 sdgetdefaultlabel(sd, lp)
1156 struct sd_softc *sd;
1157 struct disklabel *lp;
1158 {
1159
1160 memset(lp, 0, sizeof(struct disklabel));
1161
1162 lp->d_secsize = sd->params.blksize;
1163 lp->d_ntracks = sd->params.heads;
1164 lp->d_nsectors = sd->params.sectors;
1165 lp->d_ncylinders = sd->params.cyls;
1166 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1167
1168 switch (scsipi_periph_bustype(sd->sc_periph)) {
1169 #if NSD_SCSIBUS > 0
1170 case SCSIPI_BUSTYPE_SCSI:
1171 lp->d_type = DTYPE_SCSI;
1172 break;
1173 #endif
1174 #if NSD_ATAPIBUS > 0
1175 case SCSIPI_BUSTYPE_ATAPI:
1176 lp->d_type = DTYPE_ATAPI;
1177 break;
1178 #endif
1179 }
1180 strncpy(lp->d_typename, sd->name, 16);
1181 strncpy(lp->d_packname, "fictitious", 16);
1182 lp->d_secperunit = sd->params.disksize;
1183 lp->d_rpm = sd->params.rot_rate;
1184 lp->d_interleave = 1;
1185 lp->d_flags = 0;
1186
1187 lp->d_partitions[RAW_PART].p_offset = 0;
1188 lp->d_partitions[RAW_PART].p_size =
1189 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1190 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1191 lp->d_npartitions = RAW_PART + 1;
1192
1193 lp->d_magic = DISKMAGIC;
1194 lp->d_magic2 = DISKMAGIC;
1195 lp->d_checksum = dkcksum(lp);
1196 }
1197
1198
1199 /*
1200 * Load the label information on the named device
1201 */
1202 void
1203 sdgetdisklabel(sd)
1204 struct sd_softc *sd;
1205 {
1206 struct disklabel *lp = sd->sc_dk.dk_label;
1207 const char *errstring;
1208
1209 memset(sd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1210
1211 sdgetdefaultlabel(sd, lp);
1212
1213 if (lp->d_secpercyl == 0) {
1214 lp->d_secpercyl = 100;
1215 /* as long as it's not 0 - readdisklabel divides by it (?) */
1216 }
1217
1218 /*
1219 * Call the generic disklabel extraction routine
1220 */
1221 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
1222 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
1223 if (errstring) {
1224 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
1225 return;
1226 }
1227 }
1228
1229 void
1230 sd_shutdown(arg)
1231 void *arg;
1232 {
1233 struct sd_softc *sd = arg;
1234
1235 /*
1236 * If the disk cache needs to be flushed, and the disk supports
1237 * it, flush it. We're cold at this point, so we poll for
1238 * completion.
1239 */
1240 if ((sd->flags & SDF_DIRTY) != 0 && sd->sc_ops->sdo_flush != NULL) {
1241 if ((*sd->sc_ops->sdo_flush)(sd, XS_CTL_NOSLEEP|XS_CTL_POLL)) {
1242 printf("%s: cache synchronization failed\n",
1243 sd->sc_dev.dv_xname);
1244 sd->flags &= ~SDF_FLUSHING;
1245 } else
1246 sd->flags &= ~(SDF_FLUSHING|SDF_DIRTY);
1247 }
1248 }
1249
1250 /*
1251 * Tell the device to map out a defective block
1252 */
1253 int
1254 sd_reassign_blocks(sd, blkno)
1255 struct sd_softc *sd;
1256 u_long blkno;
1257 {
1258 struct scsi_reassign_blocks scsipi_cmd;
1259 struct scsi_reassign_blocks_data rbdata;
1260
1261 memset(&scsipi_cmd, 0, sizeof(scsipi_cmd));
1262 memset(&rbdata, 0, sizeof(rbdata));
1263 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
1264
1265 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
1266 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
1267
1268 return (scsipi_command(sd->sc_periph,
1269 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
1270 (u_char *)&rbdata, sizeof(rbdata), SDRETRIES, 5000, NULL,
1271 XS_CTL_DATA_OUT | XS_CTL_DATA_ONSTACK));
1272 }
1273
1274 /*
1275 * Check Errors
1276 */
1277 int
1278 sd_interpret_sense(xs)
1279 struct scsipi_xfer *xs;
1280 {
1281 struct scsipi_periph *periph = xs->xs_periph;
1282 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
1283 struct sd_softc *sd = (void *)periph->periph_dev;
1284 int s, error, retval = EJUSTRETURN;
1285
1286 /*
1287 * If the periph is already recovering, just do the normal
1288 * error processing.
1289 */
1290 if (periph->periph_flags & PERIPH_RECOVERING)
1291 return (retval);
1292
1293 /*
1294 * If the device is not open yet, let the generic code handle it.
1295 */
1296 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1297 return (retval);
1298
1299 /*
1300 * If it isn't a extended or extended/deferred error, let
1301 * the generic code handle it.
1302 */
1303 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
1304 (sense->error_code & SSD_ERRCODE) != 0x71)
1305 return (retval);
1306
1307 if ((sense->flags & SSD_KEY) == SKEY_NOT_READY &&
1308 sense->add_sense_code == 0x4) {
1309 if (sense->add_sense_code_qual == 0x01) {
1310 /*
1311 * Unit In The Process Of Becoming Ready.
1312 */
1313 printf("%s: waiting for pack to spin up...\n",
1314 sd->sc_dev.dv_xname);
1315 if (!callout_pending(&periph->periph_callout))
1316 scsipi_periph_freeze(periph, 1);
1317 callout_reset(&periph->periph_callout,
1318 5 * hz, scsipi_periph_timed_thaw, periph);
1319 retval = ERESTART;
1320 } else if (sense->add_sense_code_qual == 0x02) {
1321 printf("%s: pack is stopped, restarting...\n",
1322 sd->sc_dev.dv_xname);
1323 s = splbio();
1324 periph->periph_flags |= PERIPH_RECOVERING;
1325 splx(s);
1326 error = scsipi_start(periph, SSS_START,
1327 XS_CTL_URGENT|XS_CTL_HEAD_TAG|
1328 XS_CTL_THAW_PERIPH|XS_CTL_FREEZE_PERIPH);
1329 if (error) {
1330 printf("%s: unable to restart pack\n",
1331 sd->sc_dev.dv_xname);
1332 retval = error;
1333 } else
1334 retval = ERESTART;
1335 s = splbio();
1336 periph->periph_flags &= ~PERIPH_RECOVERING;
1337 splx(s);
1338 }
1339 }
1340 return (retval);
1341 }
1342
1343
1344 int
1345 sdsize(dev)
1346 dev_t dev;
1347 {
1348 struct sd_softc *sd;
1349 int part, unit, omask;
1350 int size;
1351
1352 unit = SDUNIT(dev);
1353 if (unit >= sd_cd.cd_ndevs)
1354 return (-1);
1355 sd = sd_cd.cd_devs[unit];
1356 if (sd == NULL)
1357 return (-1);
1358
1359 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1360 return (-1);
1361
1362 part = SDPART(dev);
1363 omask = sd->sc_dk.dk_openmask & (1 << part);
1364
1365 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1366 return (-1);
1367 if ((sd->sc_periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1368 size = -1;
1369 else if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1370 size = -1;
1371 else
1372 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1373 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1374 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1375 return (-1);
1376 return (size);
1377 }
1378
1379 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1380 static struct scsipi_xfer sx;
1381 static int sddoingadump;
1382
1383 /*
1384 * dump all of physical memory into the partition specified, starting
1385 * at offset 'dumplo' into the partition.
1386 */
1387 int
1388 sddump(dev, blkno, va, size)
1389 dev_t dev;
1390 daddr_t blkno;
1391 caddr_t va;
1392 size_t size;
1393 {
1394 struct sd_softc *sd; /* disk unit to do the I/O */
1395 struct disklabel *lp; /* disk's disklabel */
1396 int unit, part;
1397 int sectorsize; /* size of a disk sector */
1398 int nsects; /* number of sectors in partition */
1399 int sectoff; /* sector offset of partition */
1400 int totwrt; /* total number of sectors left to write */
1401 int nwrt; /* current number of sectors to write */
1402 struct scsipi_rw_big cmd; /* write command */
1403 struct scsipi_xfer *xs; /* ... convenience */
1404 struct scsipi_periph *periph;
1405 struct scsipi_channel *chan;
1406
1407 /* Check if recursive dump; if so, punt. */
1408 if (sddoingadump)
1409 return (EFAULT);
1410
1411 /* Mark as active early. */
1412 sddoingadump = 1;
1413
1414 unit = SDUNIT(dev); /* Decompose unit & partition. */
1415 part = SDPART(dev);
1416
1417 /* Check for acceptable drive number. */
1418 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1419 return (ENXIO);
1420
1421 if ((sd->sc_dev.dv_flags & DVF_ACTIVE) == 0)
1422 return (ENODEV);
1423
1424 periph = sd->sc_periph;
1425 chan = periph->periph_channel;
1426
1427 /* Make sure it was initialized. */
1428 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
1429 return (ENXIO);
1430
1431 /* Convert to disk sectors. Request must be a multiple of size. */
1432 lp = sd->sc_dk.dk_label;
1433 sectorsize = lp->d_secsize;
1434 if ((size % sectorsize) != 0)
1435 return (EFAULT);
1436 totwrt = size / sectorsize;
1437 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1438
1439 nsects = lp->d_partitions[part].p_size;
1440 sectoff = lp->d_partitions[part].p_offset;
1441
1442 /* Check transfer bounds against partition size. */
1443 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1444 return (EINVAL);
1445
1446 /* Offset block number to start of partition. */
1447 blkno += sectoff;
1448
1449 xs = &sx;
1450
1451 while (totwrt > 0) {
1452 nwrt = totwrt; /* XXX */
1453 #ifndef SD_DUMP_NOT_TRUSTED
1454 /*
1455 * Fill out the scsi command
1456 */
1457 memset(&cmd, 0, sizeof(cmd));
1458 cmd.opcode = WRITE_BIG;
1459 _lto4b(blkno, cmd.addr);
1460 _lto2b(nwrt, cmd.length);
1461 /*
1462 * Fill out the scsipi_xfer structure
1463 * Note: we cannot sleep as we may be an interrupt
1464 * don't use scsipi_command() as it may want to wait
1465 * for an xs.
1466 */
1467 memset(xs, 0, sizeof(sx));
1468 xs->xs_control |= XS_CTL_NOSLEEP | XS_CTL_POLL |
1469 XS_CTL_DATA_OUT;
1470 xs->xs_status = 0;
1471 xs->xs_periph = periph;
1472 xs->xs_retries = SDRETRIES;
1473 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1474 xs->cmd = (struct scsipi_generic *)&cmd;
1475 xs->cmdlen = sizeof(cmd);
1476 xs->resid = nwrt * sectorsize;
1477 xs->error = XS_NOERROR;
1478 xs->bp = 0;
1479 xs->data = va;
1480 xs->datalen = nwrt * sectorsize;
1481
1482 /*
1483 * Pass all this info to the scsi driver.
1484 */
1485 scsipi_adapter_request(chan, ADAPTER_REQ_RUN_XFER, xs);
1486 if ((xs->xs_status & XS_STS_DONE) == 0 ||
1487 xs->error != XS_NOERROR)
1488 return (EIO);
1489 #else /* SD_DUMP_NOT_TRUSTED */
1490 /* Let's just talk about this first... */
1491 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1492 delay(500 * 1000); /* half a second */
1493 #endif /* SD_DUMP_NOT_TRUSTED */
1494
1495 /* update block count */
1496 totwrt -= nwrt;
1497 blkno += nwrt;
1498 va += sectorsize * nwrt;
1499 }
1500 sddoingadump = 0;
1501 return (0);
1502 }
1503