xref: /original-bsd/sys/hp300/dev/sd.c (revision 5d76345d)
1 /*
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Van Jacobson of Lawrence Berkeley Laboratory.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)sd.c	8.4 (Berkeley) 04/22/94
11  */
12 
13 /*
14  * SCSI CCS (Command Command Set) disk driver.
15  */
16 #include "sd.h"
17 #if NSD > 0
18 
19 #ifndef lint
20 static char rcsid[] = "$Header: /sys.lite/hp300/dev/RCS/sd.c,v 1.2 1994/01/10 18:29:19 mike Exp mike $";
21 #endif
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/buf.h>
26 #include <sys/stat.h>
27 #include <sys/dkstat.h>
28 #include <sys/disklabel.h>
29 #include <sys/malloc.h>
30 #include <sys/proc.h>
31 #include <sys/ioctl.h>
32 #include <sys/fcntl.h>
33 
34 #include <hp/dev/device.h>
35 #include <hp300/dev/scsireg.h>
36 #include <hp300/dev/sdvar.h>
37 #ifdef USELEDS
38 #include <hp300/hp300/led.h>
39 #endif
40 
41 #include <vm/vm_param.h>
42 #include <vm/lock.h>
43 #include <vm/vm_prot.h>
44 #include <vm/pmap.h>
45 
46 extern int scsi_test_unit_rdy();
47 extern int scsi_request_sense();
48 extern int scsi_inquiry();
49 extern int scsi_read_capacity();
50 extern int scsi_tt_write();
51 extern int scsireq();
52 extern int scsiustart();
53 extern int scsigo();
54 extern void scsifree();
55 extern void scsireset();
56 extern void scsi_delay();
57 
58 extern void disksort();
59 extern void biodone();
60 extern int physio();
61 extern void TBIS();
62 
63 int	sdinit();
64 void	sdstrategy(), sdstart(), sdustart(), sdgo(), sdintr();
65 
66 struct	driver sddriver = {
67 	sdinit, "sd", (int (*)())sdstart, (int (*)())sdgo, (int (*)())sdintr,
68 };
69 
70 #ifdef DEBUG
71 int sddebug = 1;
72 #define SDB_ERROR	0x01
73 #define SDB_PARTIAL	0x02
74 #define SDB_CAPACITY	0x04
75 #endif
76 
77 struct	sd_softc sd_softc[NSD];
78 struct	sdstats sdstats[NSD];
79 struct	buf sdtab[NSD];
80 struct	scsi_fmt_cdb sdcmd[NSD];
81 struct	scsi_fmt_sense sdsense[NSD];
82 
83 static struct scsi_fmt_cdb sd_read_cmd = { 10, CMD_READ_EXT };
84 static struct scsi_fmt_cdb sd_write_cmd = { 10, CMD_WRITE_EXT };
85 
86 /*
87  * Table of scsi commands users are allowed to access via "format"
88  * mode.  0 means not legal.  1 means "immediate" (doesn't need dma).
89  * -1 means needs dma and/or wait for intr.
90  */
91 static char legal_cmds[256] = {
92 /*****  0   1   2   3   4   5   6   7     8   9   A   B   C   D   E   F */
93 /*00*/	0,  0,  0,  0, -1,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
94 /*10*/	0,  0,  1,  0,  0,  1,  0,  0,    0,  0,  1,  0,  0,  0,  0,  0,
95 /*20*/	0,  0,  0,  0,  0,  1,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
96 /*30*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
97 /*40*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
98 /*50*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
99 /*60*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
100 /*70*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
101 /*80*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
102 /*90*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
103 /*a0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
104 /*b0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
105 /*c0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
106 /*d0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
107 /*e0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
108 /*f0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
109 };
110 
111 static struct scsi_inquiry inqbuf;
112 static struct scsi_fmt_cdb inq = {
113 	6,
114 	CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0
115 };
116 
117 static int
118 sdident(sc, hd)
119 	struct sd_softc *sc;
120 	struct hp_device *hd;
121 {
122 	int unit;
123 	register int ctlr, slave;
124 	register int i;
125 	register int tries = 10;
126 	char idstr[32];
127 	int isrm = 0;
128 
129 	ctlr = hd->hp_ctlr;
130 	slave = hd->hp_slave;
131 	unit = sc->sc_punit;
132 	scsi_delay(-1);
133 
134 	/*
135 	 * See if unit exists and is a disk then read block size & nblocks.
136 	 */
137 	while ((i = scsi_test_unit_rdy(ctlr, slave, unit)) != 0) {
138 		if (i == -1 || --tries < 0) {
139 			if (isrm)
140 				break;
141 			/* doesn't exist or not a CCS device */
142 			goto failed;
143 		}
144 		if (i == STS_CHECKCOND) {
145 			u_char sensebuf[128];
146 			struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf;
147 
148 			scsi_request_sense(ctlr, slave, unit, sensebuf,
149 					   sizeof(sensebuf));
150 			if (sp->class == 7)
151 				switch (sp->key) {
152 				/*
153 				 * Not ready -- might be removable media
154 				 * device with no media.  Assume as much,
155 				 * if it really isn't, the inquiry commmand
156 				 * below will fail.
157 				 */
158 				case 2:
159 					isrm = 1;
160 					break;
161 				/* drive doing an RTZ -- give it a while */
162 				case 6:
163 					DELAY(1000000);
164 					break;
165 				default:
166 					break;
167 				}
168 		}
169 		DELAY(1000);
170 	}
171 	/*
172 	 * Find out about device
173 	 */
174 	if (scsi_immed_command(ctlr, slave, unit, &inq,
175 			       (u_char *)&inqbuf, sizeof(inqbuf), B_READ))
176 		goto failed;
177 	switch (inqbuf.type) {
178 	case 0:		/* disk */
179 	case 4:		/* WORM */
180 	case 5:		/* CD-ROM */
181 	case 7:		/* Magneto-optical */
182 		break;
183 	default:	/* not a disk */
184 		goto failed;
185 	}
186 	/*
187 	 * Get a usable id string
188 	 */
189 	switch (inqbuf.version) {
190 	case 1:
191 	case 2:
192 		bcopy((caddr_t)&inqbuf.vendor_id, (caddr_t)idstr, 28);
193 		for (i = 27; i > 23; --i)
194 			if (idstr[i] != ' ')
195 				break;
196 		idstr[i+1] = 0;
197 		for (i = 23; i > 7; --i)
198 			if (idstr[i] != ' ')
199 				break;
200 		idstr[i+1] = 0;
201 		for (i = 7; i >= 0; --i)
202 			if (idstr[i] != ' ')
203 				break;
204 		idstr[i+1] = 0;
205 		break;
206 	default:
207 		bcopy("UNKNOWN", &idstr[0], 8);
208 		bcopy("DRIVE TYPE", &idstr[8], 11);
209 	}
210 	if (inqbuf.qual & 0x80)
211 		sc->sc_flags |= SDF_RMEDIA;
212 
213 	if (sdgetcapacity(sc, hd, NODEV) < 0)
214 		goto failed;
215 
216 	switch (inqbuf.version) {
217 	case 1:
218 	case 2:
219 		printf("sd%d: %s %s rev %s", hd->hp_unit, idstr, &idstr[8],
220 			&idstr[24]);
221 		if (inqbuf.version == 2)
222 			printf(" (SCSI-2)");
223 		break;
224 	default:
225 		printf("sd%d: type 0x%x, qual 0x%x, ver %d", hd->hp_unit,
226 		       inqbuf.type, inqbuf.qual, inqbuf.version);
227 		break;
228 	}
229 	if (sc->sc_blks)
230 		printf(", %d %d byte blocks",
231 		       sc->sc_blks >> sc->sc_bshift, sc->sc_blksize);
232 	printf("\n");
233 	sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2);	/* XXX */
234 	scsi_delay(0);
235 	return(inqbuf.type);
236 failed:
237 	scsi_delay(0);
238 	return(-1);
239 }
240 
241 int
242 sdinit(hd)
243 	register struct hp_device *hd;
244 {
245 	register struct sd_softc *sc = &sd_softc[hd->hp_unit];
246 
247 	sc->sc_hd = hd;
248 	sc->sc_flags = 0;
249 	/*
250 	 * XXX formerly 0 meant unused but now pid 0 can legitimately
251 	 * use this interface (sdgetcapacity).
252 	 */
253 	sc->sc_format_pid = -1;
254 	sc->sc_punit = sdpunit(hd->hp_flags);
255 	sc->sc_type = sdident(sc, hd);
256 	if (sc->sc_type < 0)
257 		return(0);
258 	sc->sc_dq.dq_ctlr = hd->hp_ctlr;
259 	sc->sc_dq.dq_unit = hd->hp_unit;
260 	sc->sc_dq.dq_slave = hd->hp_slave;
261 	sc->sc_dq.dq_driver = &sddriver;
262 
263 	sc->sc_flags |= SDF_ALIVE;
264 	return(1);
265 }
266 
267 void
268 sdreset(sc, hd)
269 	register struct sd_softc *sc;
270 	register struct hp_device *hd;
271 {
272 	sdstats[hd->hp_unit].sdresets++;
273 }
274 
275 /*
276  * Determine capacity of a drive.
277  * Returns -1 on a failure, 0 on success, 1 on a failure that is probably
278  * due to missing media.
279  */
280 int
281 sdgetcapacity(sc, hd, dev)
282 	struct sd_softc *sc;
283 	struct hp_device *hd;
284 	dev_t dev;
285 {
286 	static struct scsi_fmt_cdb cap = {
287 		10,
288 		CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0
289 	};
290 	u_char *capbuf;
291 	int i, capbufsize;
292 
293 	/*
294 	 * Cannot use stack space for this buffer since stack KVA may not
295 	 * be valid (i.e. in context of this process) when the operation
296 	 * actually starts.
297 	 */
298 	capbufsize = 8;
299 	capbuf = malloc(capbufsize, M_DEVBUF, M_WAITOK);
300 
301 	if (dev == NODEV) {
302 		i = scsi_immed_command(hd->hp_ctlr, hd->hp_slave, sc->sc_punit,
303 				       &cap, capbuf, capbufsize, B_READ);
304 	} else {
305 		struct buf *bp;
306 
307 		/*
308 		 * XXX this is horrible
309 		 */
310 		if (sc->sc_format_pid >= 0)
311 			panic("sdgetcapacity");
312 		bp = malloc(sizeof *bp, M_DEVBUF, M_WAITOK);
313 		sc->sc_format_pid = curproc->p_pid;
314 		bcopy((caddr_t)&cap, (caddr_t)&sdcmd[hd->hp_unit], sizeof cap);
315 		bp->b_dev = dev;
316 		bp->b_flags = B_READ | B_BUSY;
317 		bp->b_un.b_addr = (caddr_t)capbuf;
318 		bp->b_bcount = capbufsize;
319 		sdstrategy(bp);
320 		i = biowait(bp) ? sdsense[hd->hp_unit].status : 0;
321 		free(bp, M_DEVBUF);
322 		sc->sc_format_pid = -1;
323 	}
324 	if (i) {
325 		if (i != STS_CHECKCOND || (sc->sc_flags & SDF_RMEDIA) == 0) {
326 #ifdef DEBUG
327 			if (sddebug & SDB_CAPACITY)
328 				printf("sd%d: read_capacity returns %d\n",
329 				       hd->hp_unit, i);
330 #endif
331 			free(capbuf, M_DEVBUF);
332 			return (-1);
333 		}
334 		/*
335 		 * XXX assume unformatted or non-existant media
336 		 */
337 		sc->sc_blks = 0;
338 		sc->sc_blksize = DEV_BSIZE;
339 		sc->sc_bshift = 0;
340 #ifdef DEBUG
341 		if (sddebug & SDB_CAPACITY)
342 			printf("sd%d: removable media not present\n",
343 			       hd->hp_unit);
344 #endif
345 		free(capbuf, M_DEVBUF);
346 		return (1);
347 	}
348 	sc->sc_blks = *(u_int *)&capbuf[0];
349 	sc->sc_blksize = *(int *)&capbuf[4];
350 	free(capbuf, M_DEVBUF);
351 	sc->sc_bshift = 0;
352 
353 	/* return value of read capacity is last valid block number */
354 	sc->sc_blks++;
355 
356 	if (sc->sc_blksize != DEV_BSIZE) {
357 		if (sc->sc_blksize < DEV_BSIZE) {
358 			printf("sd%d: need at least %d byte blocks - %s\n",
359 				hd->hp_unit, DEV_BSIZE, "drive ignored");
360 			return (-1);
361 		}
362 		for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
363 			++sc->sc_bshift;
364 		sc->sc_blks <<= sc->sc_bshift;
365 	}
366 #ifdef DEBUG
367 	if (sddebug & SDB_CAPACITY)
368 		printf("sd%d: blks=%d, blksize=%d, bshift=%d\n", hd->hp_unit,
369 		       sc->sc_blks, sc->sc_blksize, sc->sc_bshift);
370 #endif
371 	return (0);
372 }
373 
374 /*
375  * Read or constuct a disklabel
376  */
377 int
378 sdgetinfo(dev)
379 	dev_t dev;
380 {
381 	int unit = sdunit(dev);
382 	register struct sd_softc *sc = &sd_softc[unit];
383 	register struct disklabel *lp = &sc->sc_info.si_label;
384 	register struct partition *pi;
385 	char *msg, *readdisklabel();
386 #ifdef COMPAT_NOLABEL
387 	int usedefault = 1;
388 
389 	/*
390 	 * For CD-ROM just define a single partition
391 	 */
392 	if (sc->sc_type == 5)
393 		usedefault = 0;
394 #endif
395 
396 	bzero((caddr_t)lp, sizeof *lp);
397 	msg = NULL;
398 
399 	/*
400 	 * If removable media or the size unavailable at boot time
401 	 * (i.e. unformatted hard disk), attempt to set the capacity
402 	 * now.
403 	 */
404 	if ((sc->sc_flags & SDF_RMEDIA) || sc->sc_blks == 0) {
405 		switch (sdgetcapacity(sc, sc->sc_hd, dev)) {
406 		case 0:
407 			break;
408 		case -1:
409 			/*
410 			 * Hard error, just return (open will fail).
411 			 */
412 			return (EIO);
413 		case 1:
414 			/*
415 			 * XXX return 0 so open can continue just in case
416 			 * the media is unformatted and we want to format it.
417 			 * We set the error flag so they cannot do much else.
418 			 */
419 			sc->sc_flags |= SDF_ERROR;
420 			msg = "unformatted/missing media";
421 #ifdef COMPAT_NOLABEL
422 			usedefault = 0;
423 #endif
424 			break;
425 		}
426 	}
427 
428 	/*
429 	 * Set some default values to use while reading the label
430 	 * (or to use if there isn't a label) and try reading it.
431 	 */
432 	if (msg == NULL) {
433 		lp->d_type = DTYPE_SCSI;
434 		lp->d_secsize = DEV_BSIZE;
435 		lp->d_nsectors = 32;
436 		lp->d_ntracks = 20;
437 		lp->d_ncylinders = 1;
438 		lp->d_secpercyl = 32*20;
439 		lp->d_npartitions = 3;
440 		lp->d_partitions[2].p_offset = 0;
441 		/* XXX we can open a device even without SDF_ALIVE */
442 		if (sc->sc_blksize == 0)
443 			sc->sc_blksize = DEV_BSIZE;
444 		/* XXX ensure size is at least one device block */
445 		lp->d_partitions[2].p_size =
446 			roundup(LABELSECTOR+1, btodb(sc->sc_blksize));
447 		msg = readdisklabel(sdlabdev(dev), sdstrategy, lp);
448 		if (msg == NULL)
449 			return (0);
450 	}
451 
452 	pi = lp->d_partitions;
453 	printf("sd%d: WARNING: %s, ", unit, msg);
454 #ifdef COMPAT_NOLABEL
455 	if (usedefault) {
456 		printf("using old default partitioning\n");
457 		sdmakedisklabel(unit, lp);
458 		return(0);
459 	}
460 #endif
461 	printf("defining `c' partition as entire disk\n");
462 	pi[2].p_size = sc->sc_blks;
463 	return(0);
464 }
465 
466 int
467 sdopen(dev, flags, mode, p)
468 	dev_t dev;
469 	int flags, mode;
470 	struct proc *p;
471 {
472 	register int unit = sdunit(dev);
473 	register struct sd_softc *sc = &sd_softc[unit];
474 	int mask, error;
475 
476 	if (unit >= NSD)
477 		return(ENXIO);
478 	/*
479 	 * If a drive's position was fully qualified (i.e. not wildcarded in
480 	 * any way, we allow root to open the device even though it wasn't
481 	 * found at autoconfig time.  This allows initial formatting of disks.
482 	 * However, if any part of the specification was wildcarded, we won't
483 	 * be able to locate the drive so there is nothing we can do.
484 	 */
485 	if ((sc->sc_flags & SDF_ALIVE) == 0 &&
486 	    (suser(p->p_ucred, &p->p_acflag) ||
487 	     sc->sc_hd->hp_ctlr < 0 || sc->sc_hd->hp_slave < 0))
488 		return(ENXIO);
489 	if (sc->sc_flags & SDF_ERROR)
490 		return(EIO);
491 
492 	/*
493 	 * Wait for any pending opens/closes to complete
494 	 */
495 	while (sc->sc_flags & (SDF_OPENING|SDF_CLOSING))
496 		sleep((caddr_t)sc, PRIBIO);
497 	/*
498 	 * On first open, get label and partition info.
499 	 * We may block reading the label, so be careful
500 	 * to stop any other opens.
501 	 */
502 	if (sc->sc_info.si_open == 0) {
503 		sc->sc_flags |= SDF_OPENING;
504 		error = sdgetinfo(dev);
505 		sc->sc_flags &= ~SDF_OPENING;
506 		wakeup((caddr_t)sc);
507 		if (error)
508 			return(error);
509 	}
510 	if (sc->sc_hd->hp_dk >= 0)
511 		dk_wpms[sc->sc_hd->hp_dk] = sc->sc_wpms;
512 
513 	mask = 1 << sdpart(dev);
514 	if (mode == S_IFCHR)
515 		sc->sc_info.si_copen |= mask;
516 	else
517 		sc->sc_info.si_bopen |= mask;
518 	sc->sc_info.si_open |= mask;
519 	return(0);
520 }
521 
522 int
523 sdclose(dev, flag, mode, p)
524 	dev_t dev;
525 	int flag, mode;
526 	struct proc *p;
527 {
528 	int unit = sdunit(dev);
529 	register struct sd_softc *sc = &sd_softc[unit];
530 	register struct sdinfo *si = &sc->sc_info;
531 	int mask, s;
532 
533 	mask = 1 << sdpart(dev);
534 	if (mode == S_IFCHR)
535 		si->si_copen &= ~mask;
536 	else
537 		si->si_bopen &= ~mask;
538 	si->si_open = si->si_bopen | si->si_copen;
539 	/*
540 	 * On last close, we wait for all activity to cease since
541 	 * the label/parition info will become invalid.  Since we
542 	 * might sleep, we must block any opens while we are here.
543 	 * Note we don't have to about other closes since we know
544 	 * we are the last one.
545 	 */
546 	if (si->si_open == 0) {
547 		sc->sc_flags |= SDF_CLOSING;
548 		s = splbio();
549 		while (sdtab[unit].b_active) {
550 			sc->sc_flags |= SDF_WANTED;
551 			sleep((caddr_t)&sdtab[unit], PRIBIO);
552 		}
553 		splx(s);
554 		sc->sc_flags &= ~(SDF_CLOSING|SDF_WLABEL|SDF_ERROR);
555 		wakeup((caddr_t)sc);
556 	}
557 	sc->sc_format_pid = -1;
558 	return(0);
559 }
560 
561 /*
562  * This routine is called for partial block transfers and non-aligned
563  * transfers (the latter only being possible on devices with a block size
564  * larger than DEV_BSIZE).  The operation is performed in three steps
565  * using a locally allocated buffer:
566  *	1. transfer any initial partial block
567  *	2. transfer full blocks
568  *	3. transfer any final partial block
569  */
570 static void
571 sdlblkstrat(bp, bsize)
572 	register struct buf *bp;
573 	register int bsize;
574 {
575 	register struct buf *cbp = (struct buf *)malloc(sizeof(struct buf),
576 							M_DEVBUF, M_WAITOK);
577 	caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
578 	register int bn, resid;
579 	register caddr_t addr;
580 
581 	bzero((caddr_t)cbp, sizeof(*cbp));
582 	cbp->b_proc = curproc;		/* XXX */
583 	cbp->b_dev = bp->b_dev;
584 	bn = bp->b_blkno;
585 	resid = bp->b_bcount;
586 	addr = bp->b_un.b_addr;
587 #ifdef DEBUG
588 	if (sddebug & SDB_PARTIAL)
589 		printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
590 		       bp, bp->b_flags, bn, resid, addr);
591 #endif
592 
593 	while (resid > 0) {
594 		register int boff = dbtob(bn) & (bsize - 1);
595 		register int count;
596 
597 		if (boff || resid < bsize) {
598 			sdstats[sdunit(bp->b_dev)].sdpartials++;
599 			count = min(resid, bsize - boff);
600 			cbp->b_flags = B_BUSY | B_PHYS | B_READ;
601 			cbp->b_blkno = bn - btodb(boff);
602 			cbp->b_un.b_addr = cbuf;
603 			cbp->b_bcount = bsize;
604 #ifdef DEBUG
605 			if (sddebug & SDB_PARTIAL)
606 				printf(" readahead: bn %x cnt %x off %x addr %x\n",
607 				       cbp->b_blkno, count, boff, addr);
608 #endif
609 			sdstrategy(cbp);
610 			biowait(cbp);
611 			if (cbp->b_flags & B_ERROR) {
612 				bp->b_flags |= B_ERROR;
613 				bp->b_error = cbp->b_error;
614 				break;
615 			}
616 			if (bp->b_flags & B_READ) {
617 				bcopy(&cbuf[boff], addr, count);
618 				goto done;
619 			}
620 			bcopy(addr, &cbuf[boff], count);
621 #ifdef DEBUG
622 			if (sddebug & SDB_PARTIAL)
623 				printf(" writeback: bn %x cnt %x off %x addr %x\n",
624 				       cbp->b_blkno, count, boff, addr);
625 #endif
626 		} else {
627 			count = resid & ~(bsize - 1);
628 			cbp->b_blkno = bn;
629 			cbp->b_un.b_addr = addr;
630 			cbp->b_bcount = count;
631 #ifdef DEBUG
632 			if (sddebug & SDB_PARTIAL)
633 				printf(" fulltrans: bn %x cnt %x addr %x\n",
634 				       cbp->b_blkno, count, addr);
635 #endif
636 		}
637 		cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
638 		sdstrategy(cbp);
639 		biowait(cbp);
640 		if (cbp->b_flags & B_ERROR) {
641 			bp->b_flags |= B_ERROR;
642 			bp->b_error = cbp->b_error;
643 			break;
644 		}
645 done:
646 		bn += btodb(count);
647 		resid -= count;
648 		addr += count;
649 #ifdef DEBUG
650 		if (sddebug & SDB_PARTIAL)
651 			printf(" done: bn %x resid %x addr %x\n",
652 			       bn, resid, addr);
653 #endif
654 	}
655 	free(cbuf, M_DEVBUF);
656 	free(cbp, M_DEVBUF);
657 }
658 
659 void
660 sdstrategy(bp)
661 	register struct buf *bp;
662 {
663 	int unit = sdunit(bp->b_dev);
664 	register struct sd_softc *sc = &sd_softc[unit];
665 	register struct buf *dp = &sdtab[unit];
666 	register struct partition *pinfo;
667 	register daddr_t bn;
668 	register int sz, s;
669 
670 	if (sc->sc_format_pid >= 0) {
671 		if (sc->sc_format_pid != curproc->p_pid) {	/* XXX */
672 			bp->b_error = EPERM;
673 			goto bad;
674 		}
675 		bp->b_cylin = 0;
676 	} else {
677 		if (sc->sc_flags & SDF_ERROR) {
678 			bp->b_error = EIO;
679 			goto bad;
680 		}
681 		bn = bp->b_blkno;
682 		sz = howmany(bp->b_bcount, DEV_BSIZE);
683 		pinfo = &sc->sc_info.si_label.d_partitions[sdpart(bp->b_dev)];
684 		if (bn < 0 || bn + sz > pinfo->p_size) {
685 			sz = pinfo->p_size - bn;
686 			if (sz == 0) {
687 				bp->b_resid = bp->b_bcount;
688 				goto done;
689 			}
690 			if (sz < 0) {
691 				bp->b_error = EINVAL;
692 				goto bad;
693 			}
694 			bp->b_bcount = dbtob(sz);
695 		}
696 		/*
697 		 * Check for write to write protected label
698 		 */
699 		if (bn + pinfo->p_offset <= LABELSECTOR &&
700 #if LABELSECTOR != 0
701 		    bn + pinfo->p_offset + sz > LABELSECTOR &&
702 #endif
703 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & SDF_WLABEL)) {
704 			bp->b_error = EROFS;
705 			goto bad;
706 		}
707 		/*
708 		 * Non-aligned or partial-block transfers handled specially.
709 		 */
710 		s = sc->sc_blksize - 1;
711 		if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
712 			sdlblkstrat(bp, sc->sc_blksize);
713 			goto done;
714 		}
715 		bp->b_cylin = (bn + pinfo->p_offset) >> sc->sc_bshift;
716 	}
717 	s = splbio();
718 	disksort(dp, bp);
719 	if (dp->b_active == 0) {
720 		dp->b_active = 1;
721 		sdustart(unit);
722 	}
723 	splx(s);
724 	return;
725 bad:
726 	bp->b_flags |= B_ERROR;
727 done:
728 	biodone(bp);
729 }
730 
731 void
732 sdustart(unit)
733 	register int unit;
734 {
735 	if (scsireq(&sd_softc[unit].sc_dq))
736 		sdstart(unit);
737 }
738 
739 /*
740  * Return:
741  *	0	if not really an error
742  *	<0	if we should do a retry
743  *	>0	if a fatal error
744  */
745 static int
746 sderror(unit, sc, hp, stat)
747 	int unit, stat;
748 	register struct sd_softc *sc;
749 	register struct hp_device *hp;
750 {
751 	int cond = 1;
752 
753 	sdsense[unit].status = stat;
754 	if (stat & STS_CHECKCOND) {
755 		struct scsi_xsense *sp;
756 
757 		scsi_request_sense(hp->hp_ctlr, hp->hp_slave,
758 				   sc->sc_punit, sdsense[unit].sense,
759 				   sizeof(sdsense[unit].sense));
760 		sp = (struct scsi_xsense *)sdsense[unit].sense;
761 		printf("sd%d: scsi sense class %d, code %d", unit,
762 			sp->class, sp->code);
763 		if (sp->class == 7) {
764 			printf(", key %d", sp->key);
765 			if (sp->valid)
766 				printf(", blk %d", *(int *)&sp->info1);
767 			switch (sp->key) {
768 			/* no sense, try again */
769 			case 0:
770 				cond = -1;
771 				break;
772 			/* recovered error, not a problem */
773 			case 1:
774 				cond = 0;
775 				break;
776 			/* possible media change */
777 			case 6:
778 				/*
779 				 * For removable media, if we are doing the
780 				 * first open (i.e. reading the label) go
781 				 * ahead and retry, otherwise someone has
782 				 * changed the media out from under us and
783 				 * we should abort any further operations
784 				 * until a close is done.
785 				 */
786 				if (sc->sc_flags & SDF_RMEDIA) {
787 					if (sc->sc_flags & SDF_OPENING)
788 						cond = -1;
789 					else
790 						sc->sc_flags |= SDF_ERROR;
791 				}
792 				break;
793 			}
794 		}
795 		printf("\n");
796 	}
797 	return(cond);
798 }
799 
800 static void
801 sdfinish(unit, sc, bp)
802 	int unit;
803 	register struct sd_softc *sc;
804 	register struct buf *bp;
805 {
806 	register struct buf *dp = &sdtab[unit];
807 
808 	dp->b_errcnt = 0;
809 	dp->b_actf = bp->b_actf;
810 	bp->b_resid = 0;
811 	biodone(bp);
812 	scsifree(&sc->sc_dq);
813 	if (dp->b_actf)
814 		sdustart(unit);
815 	else {
816 		dp->b_active = 0;
817 		if (sc->sc_flags & SDF_WANTED) {
818 			sc->sc_flags &= ~SDF_WANTED;
819 			wakeup((caddr_t)dp);
820 		}
821 	}
822 }
823 
824 void
825 sdstart(unit)
826 	register int unit;
827 {
828 	register struct sd_softc *sc = &sd_softc[unit];
829 	register struct hp_device *hp = sc->sc_hd;
830 
831 	/*
832 	 * we have the SCSI bus -- in format mode, we may or may not need dma
833 	 * so check now.
834 	 */
835 	if (sc->sc_format_pid >= 0 && legal_cmds[sdcmd[unit].cdb[0]] > 0) {
836 		register struct buf *bp = sdtab[unit].b_actf;
837 		register int sts;
838 
839 		sdtab[unit].b_errcnt = 0;
840 		while (1) {
841 			sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave,
842 						 sc->sc_punit, &sdcmd[unit],
843 						 bp->b_un.b_addr, bp->b_bcount,
844 						 bp->b_flags & B_READ);
845 			sdsense[unit].status = sts;
846 			if ((sts & 0xfe) == 0 ||
847 			    (sts = sderror(unit, sc, hp, sts)) == 0)
848 				break;
849 			if (sts > 0 || sdtab[unit].b_errcnt++ >= SDRETRY) {
850 				bp->b_flags |= B_ERROR;
851 				bp->b_error = EIO;
852 				break;
853 			}
854 		}
855 		sdfinish(unit, sc, bp);
856 
857 	} else if (scsiustart(hp->hp_ctlr))
858 		sdgo(unit);
859 }
860 
861 void
862 sdgo(unit)
863 	register int unit;
864 {
865 	register struct sd_softc *sc = &sd_softc[unit];
866 	register struct hp_device *hp = sc->sc_hd;
867 	register struct buf *bp = sdtab[unit].b_actf;
868 	register int pad;
869 	register struct scsi_fmt_cdb *cmd;
870 
871 	if (sc->sc_format_pid >= 0) {
872 		cmd = &sdcmd[unit];
873 		pad = 0;
874 	} else {
875 		/*
876 		 * Drive is in an error state, abort all operations
877 		 */
878 		if (sc->sc_flags & SDF_ERROR) {
879 			bp->b_flags |= B_ERROR;
880 			bp->b_error = EIO;
881 			sdfinish(unit, sc, bp);
882 			return;
883 		}
884 		cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd;
885 		*(int *)(&cmd->cdb[2]) = bp->b_cylin;
886 		pad = howmany(bp->b_bcount, sc->sc_blksize);
887 		*(u_short *)(&cmd->cdb[7]) = pad;
888 		pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0;
889 #ifdef DEBUG
890 		if (pad)
891 			printf("sd%d: partial block xfer -- %x bytes\n",
892 			       unit, bp->b_bcount);
893 #endif
894 		sdstats[unit].sdtransfers++;
895 	}
896 #ifdef USELEDS
897 	if (inledcontrol == 0)
898 		ledcontrol(0, 0, LED_DISK);
899 #endif
900 	if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) {
901 		if (hp->hp_dk >= 0) {
902 			dk_busy |= 1 << hp->hp_dk;
903 			++dk_seek[hp->hp_dk];
904 			++dk_xfer[hp->hp_dk];
905 			dk_wds[hp->hp_dk] += bp->b_bcount >> 6;
906 		}
907 		return;
908 	}
909 #ifdef DEBUG
910 	if (sddebug & SDB_ERROR)
911 		printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n",
912 		       unit, bp->b_flags & B_READ? "read" : "write",
913 		       bp->b_un.b_addr, bp->b_cylin, bp->b_bcount,
914 		       sdtab[unit].b_errcnt);
915 #endif
916 	bp->b_flags |= B_ERROR;
917 	bp->b_error = EIO;
918 	sdfinish(unit, sc, bp);
919 }
920 
921 void
922 sdintr(unit, stat)
923 	register int unit;
924 	int stat;
925 {
926 	register struct sd_softc *sc = &sd_softc[unit];
927 	register struct buf *bp = sdtab[unit].b_actf;
928 	register struct hp_device *hp = sc->sc_hd;
929 	int cond;
930 
931 	if (bp == NULL) {
932 		printf("sd%d: bp == NULL\n", unit);
933 		return;
934 	}
935 	if (hp->hp_dk >= 0)
936 		dk_busy &=~ (1 << hp->hp_dk);
937 	if (stat) {
938 #ifdef DEBUG
939 		if (sddebug & SDB_ERROR)
940 			printf("sd%d: sdintr: bad scsi status 0x%x\n",
941 				unit, stat);
942 #endif
943 		cond = sderror(unit, sc, hp, stat);
944 		if (cond) {
945 			if (cond < 0 && sdtab[unit].b_errcnt++ < SDRETRY) {
946 #ifdef DEBUG
947 				if (sddebug & SDB_ERROR)
948 					printf("sd%d: retry #%d\n",
949 					       unit, sdtab[unit].b_errcnt);
950 #endif
951 				sdstart(unit);
952 				return;
953 			}
954 			bp->b_flags |= B_ERROR;
955 			bp->b_error = EIO;
956 		}
957 	}
958 	sdfinish(unit, sc, bp);
959 }
960 
961 int
962 sdread(dev, uio, flags)
963 	dev_t dev;
964 	struct uio *uio;
965 	int flags;
966 {
967 	register int unit = sdunit(dev);
968 	register int pid;
969 
970 	if ((pid = sd_softc[unit].sc_format_pid) >= 0 &&
971 	    pid != uio->uio_procp->p_pid)
972 		return (EPERM);
973 
974 	return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio));
975 }
976 
977 int
978 sdwrite(dev, uio, flags)
979 	dev_t dev;
980 	struct uio *uio;
981 	int flags;
982 {
983 	register int unit = sdunit(dev);
984 	register int pid;
985 
986 	if ((pid = sd_softc[unit].sc_format_pid) >= 0 &&
987 	    pid != uio->uio_procp->p_pid)
988 		return (EPERM);
989 
990 	return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio));
991 }
992 
993 int
994 sdioctl(dev, cmd, data, flag, p)
995 	dev_t dev;
996 	int cmd;
997 	caddr_t data;
998 	int flag;
999 	struct proc *p;
1000 {
1001 	int unit = sdunit(dev);
1002 	register struct sd_softc *sc = &sd_softc[unit];
1003 	register struct disklabel *lp = &sc->sc_info.si_label;
1004 	int error, flags;
1005 
1006 	switch (cmd) {
1007 	default:
1008 		return (EINVAL);
1009 
1010 	case DIOCGDINFO:
1011 		*(struct disklabel *)data = *lp;
1012 		return (0);
1013 
1014 	case DIOCGPART:
1015 		((struct partinfo *)data)->disklab = lp;
1016 		((struct partinfo *)data)->part =
1017 			&lp->d_partitions[sdpart(dev)];
1018 		return (0);
1019 
1020         case DIOCWLABEL:
1021                 if ((flag & FWRITE) == 0)
1022                         return (EBADF);
1023 		if (*(int *)data)
1024 			sc->sc_flags |= SDF_WLABEL;
1025 		else
1026 			sc->sc_flags &= ~SDF_WLABEL;
1027 		return (0);
1028 
1029         case DIOCSDINFO:
1030                 if ((flag & FWRITE) == 0)
1031                         return (EBADF);
1032 		error = setdisklabel(lp, (struct disklabel *)data,
1033 				     (sc->sc_flags & SDF_WLABEL) ? 0
1034 				     : sc->sc_info.si_open);
1035 		return (error);
1036 
1037         case DIOCWDINFO:
1038 		if ((flag & FWRITE) == 0)
1039 			return (EBADF);
1040 		error = setdisklabel(lp, (struct disklabel *)data,
1041 				     (sc->sc_flags & SDF_WLABEL) ? 0
1042 				     : sc->sc_info.si_open);
1043 		if (error)
1044 			return (error);
1045 		flags = sc->sc_flags;
1046 		sc->sc_flags = SDF_ALIVE | SDF_WLABEL;
1047 		error = writedisklabel(sdlabdev(dev), sdstrategy, lp);
1048 		sc->sc_flags = flags;
1049 		return (error);
1050 
1051 	case SDIOCSFORMAT:
1052 		/* take this device into or out of "format" mode */
1053 		if (suser(p->p_ucred, &p->p_acflag))
1054 			return(EPERM);
1055 
1056 		if (*(int *)data) {
1057 			if (sc->sc_format_pid >= 0)
1058 				return (EPERM);
1059 			sc->sc_format_pid = p->p_pid;
1060 		} else
1061 			sc->sc_format_pid = -1;
1062 		return (0);
1063 
1064 	case SDIOCGFORMAT:
1065 		/* find out who has the device in format mode */
1066 		*(int *)data = sc->sc_format_pid;
1067 		return (0);
1068 
1069 	case SDIOCSCSICOMMAND:
1070 		/*
1071 		 * Save what user gave us as SCSI cdb to use with next
1072 		 * read or write to the char device.
1073 		 */
1074 		if (sc->sc_format_pid != p->p_pid)
1075 			return (EPERM);
1076 		if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
1077 			return (EINVAL);
1078 		bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0]));
1079 		return (0);
1080 
1081 	case SDIOCSENSE:
1082 		/*
1083 		 * return the SCSI sense data saved after the last
1084 		 * operation that completed with "check condition" status.
1085 		 */
1086 		bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0]));
1087 		return (0);
1088 
1089 	}
1090 	/*NOTREACHED*/
1091 }
1092 
1093 int
1094 sdsize(dev)
1095 	dev_t dev;
1096 {
1097 	register int unit = sdunit(dev);
1098 	register struct sd_softc *sc = &sd_softc[unit];
1099 	int psize, didopen = 0;
1100 
1101 	if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
1102 		return(-1);
1103 
1104 	/*
1105 	 * We get called very early on (via swapconf)
1106 	 * without the device being open so we may need
1107 	 * to handle it here.
1108 	 */
1109 	if (sc->sc_info.si_open == 0) {
1110 		if (sdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1111 			return(-1);
1112 		didopen = 1;
1113 	}
1114 	psize = sc->sc_info.si_label.d_partitions[sdpart(dev)].p_size;
1115 	if (didopen)
1116 		(void) sdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1117 	return (psize);
1118 }
1119 
1120 /*
1121  * Non-interrupt driven, non-dma dump routine.
1122  */
1123 int
1124 sddump(dev)
1125 	dev_t dev;
1126 {
1127 	int part = sdpart(dev);
1128 	int unit = sdunit(dev);
1129 	register struct sd_softc *sc = &sd_softc[unit];
1130 	register struct hp_device *hp = sc->sc_hd;
1131 	register struct partition *pinfo;
1132 	register daddr_t baddr;
1133 	register int maddr;
1134 	register int pages, i;
1135 	int stat;
1136 	extern int lowram, dumpsize;
1137 
1138 	/* is drive ok? */
1139 	if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
1140 		return (ENXIO);
1141 	pinfo = &sc->sc_info.si_label.d_partitions[part];
1142 	/* dump parameters in range? */
1143 	if (dumplo < 0 || dumplo >= pinfo->p_size ||
1144 	    pinfo->p_fstype != FS_SWAP)
1145 		return (EINVAL);
1146 	pages = dumpsize;
1147 	if (dumplo + ctod(pages) > pinfo->p_size)
1148 		pages = dtoc(pinfo->p_size - dumplo);
1149 	maddr = lowram;
1150 	baddr = dumplo + pinfo->p_offset;
1151 	/* scsi bus idle? */
1152 	if (!scsireq(&sc->sc_dq)) {
1153 		scsireset(hp->hp_ctlr);
1154 		sdreset(sc, sc->sc_hd);
1155 		printf("[ drive %d reset ] ", unit);
1156 	}
1157 	for (i = 0; i < pages; i++) {
1158 #define NPGMB	(1024*1024/NBPG)
1159 		/* print out how many Mbs we have dumped */
1160 		if (i && (i % NPGMB) == 0)
1161 			printf("%d ", i / NPGMB);
1162 #undef NPBMG
1163 		pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr,
1164 		    VM_PROT_READ, TRUE);
1165 		stat = scsi_tt_write(hp->hp_ctlr, hp->hp_slave, sc->sc_punit,
1166 				     vmmap, NBPG, baddr, sc->sc_bshift);
1167 		if (stat) {
1168 			printf("sddump: scsi write error 0x%x\n", stat);
1169 			return (EIO);
1170 		}
1171 		maddr += NBPG;
1172 		baddr += ctod(1);
1173 	}
1174 	return (0);
1175 }
1176 #endif
1177