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