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