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