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