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