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