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