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