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