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