xref: /original-bsd/sys/hp300/dev/sd.c (revision 898c7514)
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.8 (Berkeley) 05/10/95
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 <sys/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 	/* XXX reset other info since readdisklabel screws with it */
464 	lp->d_npartitions = 3;
465 	pi[0].p_size = 0;
466 	return(0);
467 }
468 
469 int
470 sdopen(dev, flags, mode, p)
471 	dev_t dev;
472 	int flags, mode;
473 	struct proc *p;
474 {
475 	register int unit = sdunit(dev);
476 	register struct sd_softc *sc = &sd_softc[unit];
477 	int error, mask;
478 
479 	if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
480 		return(ENXIO);
481 
482 	/*
483 	 * Wait for any pending opens/closes to complete
484 	 */
485 	while (sc->sc_flags & (SDF_OPENING|SDF_CLOSING))
486 		sleep((caddr_t)sc, PRIBIO);
487 
488 	/*
489 	 * On first open, get label and partition info.
490 	 * We may block reading the label, so be careful
491 	 * to stop any other opens.
492 	 */
493 	if (sc->sc_info.si_open == 0) {
494 		sc->sc_flags |= SDF_OPENING;
495 		error = sdgetinfo(dev);
496 		sc->sc_flags &= ~SDF_OPENING;
497 		wakeup((caddr_t)sc);
498 		if (error)
499 			return(error);
500 	}
501 	if (sc->sc_hd->hp_dk >= 0)
502 		dk_wpms[sc->sc_hd->hp_dk] = sc->sc_wpms;
503 
504 	mask = 1 << sdpart(dev);
505 	if (mode == S_IFCHR)
506 		sc->sc_info.si_copen |= mask;
507 	else
508 		sc->sc_info.si_bopen |= mask;
509 	sc->sc_info.si_open |= mask;
510 	return(0);
511 }
512 
513 int
514 sdclose(dev, flag, mode, p)
515 	dev_t dev;
516 	int flag, mode;
517 	struct proc *p;
518 {
519 	int unit = sdunit(dev);
520 	register struct sd_softc *sc = &sd_softc[unit];
521 	register struct sdinfo *si = &sc->sc_info;
522 	int mask, s;
523 
524 	mask = 1 << sdpart(dev);
525 	if (mode == S_IFCHR)
526 		si->si_copen &= ~mask;
527 	else
528 		si->si_bopen &= ~mask;
529 	si->si_open = si->si_bopen | si->si_copen;
530 	/*
531 	 * On last close, we wait for all activity to cease since
532 	 * the label/parition info will become invalid.  Since we
533 	 * might sleep, we must block any opens while we are here.
534 	 * Note we don't have to about other closes since we know
535 	 * we are the last one.
536 	 */
537 	if (si->si_open == 0) {
538 		sc->sc_flags |= SDF_CLOSING;
539 		s = splbio();
540 		while (sdtab[unit].b_active) {
541 			sc->sc_flags |= SDF_WANTED;
542 			sleep((caddr_t)&sdtab[unit], PRIBIO);
543 		}
544 		splx(s);
545 		sc->sc_flags &= ~(SDF_CLOSING|SDF_WLABEL|SDF_ERROR);
546 		wakeup((caddr_t)sc);
547 	}
548 	sc->sc_format_pid = -1;
549 	return(0);
550 }
551 
552 /*
553  * This routine is called for partial block transfers and non-aligned
554  * transfers (the latter only being possible on devices with a block size
555  * larger than DEV_BSIZE).  The operation is performed in three steps
556  * using a locally allocated buffer:
557  *	1. transfer any initial partial block
558  *	2. transfer full blocks
559  *	3. transfer any final partial block
560  */
561 static void
562 sdlblkstrat(bp, bsize)
563 	register struct buf *bp;
564 	register int bsize;
565 {
566 	register struct buf *cbp = (struct buf *)malloc(sizeof(struct buf),
567 							M_DEVBUF, M_WAITOK);
568 	caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
569 	register int bn, resid;
570 	register caddr_t addr;
571 
572 	bzero((caddr_t)cbp, sizeof(*cbp));
573 	cbp->b_proc = curproc;		/* XXX */
574 	cbp->b_dev = bp->b_dev;
575 	bn = bp->b_blkno;
576 	resid = bp->b_bcount;
577 	addr = bp->b_un.b_addr;
578 #ifdef DEBUG
579 	if (sddebug & SDB_PARTIAL)
580 		printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
581 		       bp, bp->b_flags, bn, resid, addr);
582 #endif
583 
584 	while (resid > 0) {
585 		register int boff = dbtob(bn) & (bsize - 1);
586 		register int count;
587 
588 		if (boff || resid < bsize) {
589 			sdstats[sdunit(bp->b_dev)].sdpartials++;
590 			count = min(resid, bsize - boff);
591 			cbp->b_flags = B_BUSY | B_PHYS | B_READ;
592 			cbp->b_blkno = bn - btodb(boff);
593 			cbp->b_un.b_addr = cbuf;
594 			cbp->b_bcount = bsize;
595 #ifdef DEBUG
596 			if (sddebug & SDB_PARTIAL)
597 				printf(" readahead: bn %x cnt %x off %x addr %x\n",
598 				       cbp->b_blkno, count, boff, addr);
599 #endif
600 			sdstrategy(cbp);
601 			biowait(cbp);
602 			if (cbp->b_flags & B_ERROR) {
603 				bp->b_flags |= B_ERROR;
604 				bp->b_error = cbp->b_error;
605 				break;
606 			}
607 			if (bp->b_flags & B_READ) {
608 				bcopy(&cbuf[boff], addr, count);
609 				goto done;
610 			}
611 			bcopy(addr, &cbuf[boff], count);
612 #ifdef DEBUG
613 			if (sddebug & SDB_PARTIAL)
614 				printf(" writeback: bn %x cnt %x off %x addr %x\n",
615 				       cbp->b_blkno, count, boff, addr);
616 #endif
617 		} else {
618 			count = resid & ~(bsize - 1);
619 			cbp->b_blkno = bn;
620 			cbp->b_un.b_addr = addr;
621 			cbp->b_bcount = count;
622 #ifdef DEBUG
623 			if (sddebug & SDB_PARTIAL)
624 				printf(" fulltrans: bn %x cnt %x addr %x\n",
625 				       cbp->b_blkno, count, addr);
626 #endif
627 		}
628 		cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
629 		sdstrategy(cbp);
630 		biowait(cbp);
631 		if (cbp->b_flags & B_ERROR) {
632 			bp->b_flags |= B_ERROR;
633 			bp->b_error = cbp->b_error;
634 			break;
635 		}
636 done:
637 		bn += btodb(count);
638 		resid -= count;
639 		addr += count;
640 #ifdef DEBUG
641 		if (sddebug & SDB_PARTIAL)
642 			printf(" done: bn %x resid %x addr %x\n",
643 			       bn, resid, addr);
644 #endif
645 	}
646 	free(cbuf, M_DEVBUF);
647 	free(cbp, M_DEVBUF);
648 }
649 
650 void
651 sdstrategy(bp)
652 	register struct buf *bp;
653 {
654 	int unit = sdunit(bp->b_dev);
655 	register struct sd_softc *sc = &sd_softc[unit];
656 	register struct buf *dp = &sdtab[unit];
657 	register struct partition *pinfo;
658 	register daddr_t bn;
659 	register int sz, s;
660 
661 	if (sc->sc_format_pid >= 0) {
662 		if (sc->sc_format_pid != curproc->p_pid) {	/* XXX */
663 			bp->b_error = EPERM;
664 			goto bad;
665 		}
666 		bp->b_cylin = 0;
667 	} else {
668 		if (sc->sc_flags & SDF_ERROR) {
669 			bp->b_error = EIO;
670 			goto bad;
671 		}
672 		bn = bp->b_blkno;
673 		sz = howmany(bp->b_bcount, DEV_BSIZE);
674 		pinfo = &sc->sc_info.si_label.d_partitions[sdpart(bp->b_dev)];
675 		if (bn < 0 || bn + sz > pinfo->p_size) {
676 			sz = pinfo->p_size - bn;
677 			if (sz == 0) {
678 				bp->b_resid = bp->b_bcount;
679 				goto done;
680 			}
681 			if (sz < 0) {
682 				bp->b_error = EINVAL;
683 				goto bad;
684 			}
685 			bp->b_bcount = dbtob(sz);
686 		}
687 		/*
688 		 * Check for write to write protected label
689 		 */
690 		if (bn + pinfo->p_offset <= LABELSECTOR &&
691 #if LABELSECTOR != 0
692 		    bn + pinfo->p_offset + sz > LABELSECTOR &&
693 #endif
694 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & SDF_WLABEL)) {
695 			bp->b_error = EROFS;
696 			goto bad;
697 		}
698 		/*
699 		 * Non-aligned or partial-block transfers handled specially.
700 		 */
701 		s = sc->sc_blksize - 1;
702 		if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
703 			sdlblkstrat(bp, sc->sc_blksize);
704 			goto done;
705 		}
706 		bp->b_cylin = (bn + pinfo->p_offset) >> sc->sc_bshift;
707 	}
708 	s = splbio();
709 	disksort(dp, bp);
710 	if (dp->b_active == 0) {
711 		dp->b_active = 1;
712 		sdustart(unit);
713 	}
714 	splx(s);
715 	return;
716 bad:
717 	bp->b_flags |= B_ERROR;
718 done:
719 	biodone(bp);
720 }
721 
722 void
723 sdustart(unit)
724 	register int unit;
725 {
726 	if (scsireq(&sd_softc[unit].sc_dq))
727 		sdstart(unit);
728 }
729 
730 /*
731  * Return:
732  *	0	if not really an error
733  *	<0	if we should do a retry
734  *	>0	if a fatal error
735  */
736 static int
737 sderror(unit, sc, hp, stat)
738 	int unit, stat;
739 	register struct sd_softc *sc;
740 	register struct hp_device *hp;
741 {
742 	int cond = 1;
743 
744 	sdsense[unit].status = stat;
745 	if (stat & STS_CHECKCOND) {
746 		struct scsi_xsense *sp;
747 
748 		scsi_request_sense(hp->hp_ctlr, hp->hp_slave,
749 				   sc->sc_punit, sdsense[unit].sense,
750 				   sizeof(sdsense[unit].sense));
751 		sp = (struct scsi_xsense *)sdsense[unit].sense;
752 		printf("sd%d: scsi sense class %d, code %d", unit,
753 			sp->class, sp->code);
754 		if (sp->class == 7) {
755 			printf(", key %d", sp->key);
756 			if (sp->valid)
757 				printf(", blk %d", *(int *)&sp->info1);
758 			switch (sp->key) {
759 			/* no sense, try again */
760 			case 0:
761 				cond = -1;
762 				break;
763 			/* recovered error, not a problem */
764 			case 1:
765 				cond = 0;
766 				break;
767 			/* possible media change */
768 			case 6:
769 				/*
770 				 * For removable media, if we are doing the
771 				 * first open (i.e. reading the label) go
772 				 * ahead and retry, otherwise someone has
773 				 * changed the media out from under us and
774 				 * we should abort any further operations
775 				 * until a close is done.
776 				 */
777 				if (sc->sc_flags & SDF_RMEDIA) {
778 					if (sc->sc_flags & SDF_OPENING)
779 						cond = -1;
780 					else
781 						sc->sc_flags |= SDF_ERROR;
782 				}
783 				break;
784 			}
785 		}
786 		printf("\n");
787 	}
788 	return(cond);
789 }
790 
791 static void
792 sdfinish(unit, sc, bp)
793 	int unit;
794 	register struct sd_softc *sc;
795 	register struct buf *bp;
796 {
797 	register struct buf *dp = &sdtab[unit];
798 
799 	dp->b_errcnt = 0;
800 	dp->b_actf = bp->b_actf;
801 	bp->b_resid = 0;
802 	biodone(bp);
803 	scsifree(&sc->sc_dq);
804 	if (dp->b_actf)
805 		sdustart(unit);
806 	else {
807 		dp->b_active = 0;
808 		if (sc->sc_flags & SDF_WANTED) {
809 			sc->sc_flags &= ~SDF_WANTED;
810 			wakeup((caddr_t)dp);
811 		}
812 	}
813 }
814 
815 void
816 sdstart(unit)
817 	register int unit;
818 {
819 	register struct sd_softc *sc = &sd_softc[unit];
820 	register struct hp_device *hp = sc->sc_hd;
821 
822 	/*
823 	 * we have the SCSI bus -- in format mode, we may or may not need dma
824 	 * so check now.
825 	 */
826 	if (sc->sc_format_pid >= 0 && legal_cmds[sdcmd[unit].cdb[0]] > 0) {
827 		register struct buf *bp = sdtab[unit].b_actf;
828 		register int sts;
829 
830 		sdtab[unit].b_errcnt = 0;
831 		while (1) {
832 			sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave,
833 						 sc->sc_punit, &sdcmd[unit],
834 						 bp->b_un.b_addr, bp->b_bcount,
835 						 bp->b_flags & B_READ);
836 			sdsense[unit].status = sts;
837 			if ((sts & 0xfe) == 0 ||
838 			    (sts = sderror(unit, sc, hp, sts)) == 0)
839 				break;
840 			if (sts > 0 || sdtab[unit].b_errcnt++ >= SDRETRY) {
841 				bp->b_flags |= B_ERROR;
842 				bp->b_error = EIO;
843 				break;
844 			}
845 		}
846 		sdfinish(unit, sc, bp);
847 
848 	} else if (scsiustart(hp->hp_ctlr))
849 		sdgo(unit);
850 }
851 
852 void
853 sdgo(unit)
854 	register int unit;
855 {
856 	register struct sd_softc *sc = &sd_softc[unit];
857 	register struct hp_device *hp = sc->sc_hd;
858 	register struct buf *bp = sdtab[unit].b_actf;
859 	register int pad;
860 	register struct scsi_fmt_cdb *cmd;
861 
862 	if (sc->sc_format_pid >= 0) {
863 		cmd = &sdcmd[unit];
864 		pad = 0;
865 	} else {
866 		/*
867 		 * Drive is in an error state, abort all operations
868 		 */
869 		if (sc->sc_flags & SDF_ERROR) {
870 			bp->b_flags |= B_ERROR;
871 			bp->b_error = EIO;
872 			sdfinish(unit, sc, bp);
873 			return;
874 		}
875 		cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd;
876 		*(int *)(&cmd->cdb[2]) = bp->b_cylin;
877 		pad = howmany(bp->b_bcount, sc->sc_blksize);
878 		*(u_short *)(&cmd->cdb[7]) = pad;
879 		pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0;
880 #ifdef DEBUG
881 		if (pad)
882 			printf("sd%d: partial block xfer -- %x bytes\n",
883 			       unit, bp->b_bcount);
884 #endif
885 		sdstats[unit].sdtransfers++;
886 	}
887 #ifdef USELEDS
888 	if (inledcontrol == 0)
889 		ledcontrol(0, 0, LED_DISK);
890 #endif
891 	if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) {
892 		if (hp->hp_dk >= 0) {
893 			dk_busy |= 1 << hp->hp_dk;
894 			++dk_seek[hp->hp_dk];
895 			++dk_xfer[hp->hp_dk];
896 			dk_wds[hp->hp_dk] += bp->b_bcount >> 6;
897 		}
898 		return;
899 	}
900 #ifdef DEBUG
901 	if (sddebug & SDB_ERROR)
902 		printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n",
903 		       unit, bp->b_flags & B_READ? "read" : "write",
904 		       bp->b_un.b_addr, bp->b_cylin, bp->b_bcount,
905 		       sdtab[unit].b_errcnt);
906 #endif
907 	bp->b_flags |= B_ERROR;
908 	bp->b_error = EIO;
909 	sdfinish(unit, sc, bp);
910 }
911 
912 void
913 sdintr(unit, stat)
914 	register int unit;
915 	int stat;
916 {
917 	register struct sd_softc *sc = &sd_softc[unit];
918 	register struct buf *bp = sdtab[unit].b_actf;
919 	register struct hp_device *hp = sc->sc_hd;
920 	int cond;
921 
922 	if (bp == NULL) {
923 		printf("sd%d: bp == NULL\n", unit);
924 		return;
925 	}
926 	if (hp->hp_dk >= 0)
927 		dk_busy &=~ (1 << hp->hp_dk);
928 	if (stat) {
929 #ifdef DEBUG
930 		if (sddebug & SDB_ERROR)
931 			printf("sd%d: sdintr: bad scsi status 0x%x\n",
932 				unit, stat);
933 #endif
934 		cond = sderror(unit, sc, hp, stat);
935 		if (cond) {
936 			if (cond < 0 && sdtab[unit].b_errcnt++ < SDRETRY) {
937 #ifdef DEBUG
938 				if (sddebug & SDB_ERROR)
939 					printf("sd%d: retry #%d\n",
940 					       unit, sdtab[unit].b_errcnt);
941 #endif
942 				sdstart(unit);
943 				return;
944 			}
945 			bp->b_flags |= B_ERROR;
946 			bp->b_error = EIO;
947 		}
948 	}
949 	sdfinish(unit, sc, bp);
950 }
951 
952 int
953 sdread(dev, uio, flags)
954 	dev_t dev;
955 	struct uio *uio;
956 	int flags;
957 {
958 	register int unit = sdunit(dev);
959 	register int pid;
960 
961 	if ((pid = sd_softc[unit].sc_format_pid) >= 0 &&
962 	    pid != uio->uio_procp->p_pid)
963 		return (EPERM);
964 
965 	return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio));
966 }
967 
968 int
969 sdwrite(dev, uio, flags)
970 	dev_t dev;
971 	struct uio *uio;
972 	int flags;
973 {
974 	register int unit = sdunit(dev);
975 	register int pid;
976 
977 	if ((pid = sd_softc[unit].sc_format_pid) >= 0 &&
978 	    pid != uio->uio_procp->p_pid)
979 		return (EPERM);
980 
981 	return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio));
982 }
983 
984 int
985 sdioctl(dev, cmd, data, flag, p)
986 	dev_t dev;
987 	u_long cmd;
988 	caddr_t data;
989 	int flag;
990 	struct proc *p;
991 {
992 	int unit = sdunit(dev);
993 	register struct sd_softc *sc = &sd_softc[unit];
994 	register struct disklabel *lp = &sc->sc_info.si_label;
995 	int error, flags;
996 
997 	switch (cmd) {
998 	default:
999 		return (EINVAL);
1000 
1001 	case DIOCGDINFO:
1002 		*(struct disklabel *)data = *lp;
1003 		return (0);
1004 
1005 	case DIOCGPART:
1006 		((struct partinfo *)data)->disklab = lp;
1007 		((struct partinfo *)data)->part =
1008 			&lp->d_partitions[sdpart(dev)];
1009 		return (0);
1010 
1011         case DIOCWLABEL:
1012                 if ((flag & FWRITE) == 0)
1013                         return (EBADF);
1014 		if (*(int *)data)
1015 			sc->sc_flags |= SDF_WLABEL;
1016 		else
1017 			sc->sc_flags &= ~SDF_WLABEL;
1018 		return (0);
1019 
1020         case DIOCSDINFO:
1021                 if ((flag & FWRITE) == 0)
1022                         return (EBADF);
1023 		error = setdisklabel(lp, (struct disklabel *)data,
1024 				     (sc->sc_flags & SDF_WLABEL) ? 0
1025 				     : sc->sc_info.si_open);
1026 		return (error);
1027 
1028         case DIOCWDINFO:
1029 		if ((flag & FWRITE) == 0)
1030 			return (EBADF);
1031 		error = setdisklabel(lp, (struct disklabel *)data,
1032 				     (sc->sc_flags & SDF_WLABEL) ? 0
1033 				     : sc->sc_info.si_open);
1034 		if (error)
1035 			return (error);
1036 		flags = sc->sc_flags;
1037 		sc->sc_flags = SDF_ALIVE | SDF_WLABEL;
1038 		error = writedisklabel(sdlabdev(dev), sdstrategy, lp);
1039 		sc->sc_flags = flags;
1040 		return (error);
1041 
1042 	case SDIOCSFORMAT:
1043 		/* take this device into or out of "format" mode */
1044 		if (suser(p->p_ucred, &p->p_acflag))
1045 			return(EPERM);
1046 
1047 		if (*(int *)data) {
1048 			if (sc->sc_format_pid >= 0)
1049 				return (EPERM);
1050 			sc->sc_format_pid = p->p_pid;
1051 		} else
1052 			sc->sc_format_pid = -1;
1053 		return (0);
1054 
1055 	case SDIOCGFORMAT:
1056 		/* find out who has the device in format mode */
1057 		*(int *)data = sc->sc_format_pid;
1058 		return (0);
1059 
1060 	case SDIOCSCSICOMMAND:
1061 		/*
1062 		 * Save what user gave us as SCSI cdb to use with next
1063 		 * read or write to the char device.
1064 		 */
1065 		if (sc->sc_format_pid != p->p_pid)
1066 			return (EPERM);
1067 		if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
1068 			return (EINVAL);
1069 		bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0]));
1070 		return (0);
1071 
1072 	case SDIOCSENSE:
1073 		/*
1074 		 * return the SCSI sense data saved after the last
1075 		 * operation that completed with "check condition" status.
1076 		 */
1077 		bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0]));
1078 		return (0);
1079 
1080 	}
1081 	/*NOTREACHED*/
1082 }
1083 
1084 int
1085 sdsize(dev)
1086 	dev_t dev;
1087 {
1088 	register int unit = sdunit(dev);
1089 	register struct sd_softc *sc = &sd_softc[unit];
1090 	int psize, didopen = 0;
1091 
1092 	if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
1093 		return(-1);
1094 
1095 	/*
1096 	 * We get called very early on (via swapconf)
1097 	 * without the device being open so we may need
1098 	 * to handle it here.
1099 	 */
1100 	if (sc->sc_info.si_open == 0) {
1101 		if (sdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1102 			return(-1);
1103 		didopen = 1;
1104 	}
1105 	psize = sc->sc_info.si_label.d_partitions[sdpart(dev)].p_size;
1106 	if (didopen)
1107 		(void) sdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1108 	return (psize);
1109 }
1110 
1111 /*
1112  * Non-interrupt driven, non-dma dump routine.
1113  */
1114 int
1115 sddump(dev)
1116 	dev_t dev;
1117 {
1118 	int part = sdpart(dev);
1119 	int unit = sdunit(dev);
1120 	register struct sd_softc *sc = &sd_softc[unit];
1121 	register struct hp_device *hp = sc->sc_hd;
1122 	register struct partition *pinfo;
1123 	register daddr_t baddr;
1124 	register int maddr;
1125 	register int pages, i;
1126 	int stat;
1127 	extern int lowram, dumpsize;
1128 
1129 	/* is drive ok? */
1130 	if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
1131 		return (ENXIO);
1132 	pinfo = &sc->sc_info.si_label.d_partitions[part];
1133 	/* dump parameters in range? */
1134 	if (dumplo < 0 || dumplo >= pinfo->p_size ||
1135 	    pinfo->p_fstype != FS_SWAP)
1136 		return (EINVAL);
1137 	pages = dumpsize;
1138 	if (dumplo + ctod(pages) > pinfo->p_size)
1139 		pages = dtoc(pinfo->p_size - dumplo);
1140 	maddr = lowram;
1141 	baddr = dumplo + pinfo->p_offset;
1142 	/* scsi bus idle? */
1143 	if (!scsireq(&sc->sc_dq)) {
1144 		scsireset(hp->hp_ctlr);
1145 		sdreset(sc, sc->sc_hd);
1146 		printf("[ drive %d reset ] ", unit);
1147 	}
1148 	for (i = 0; i < pages; i++) {
1149 #define NPGMB	(1024*1024/NBPG)
1150 		/* print out how many Mbs we have dumped */
1151 		if (i && (i % NPGMB) == 0)
1152 			printf("%d ", i / NPGMB);
1153 #undef NPBMG
1154 		pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr,
1155 		    VM_PROT_READ, TRUE);
1156 		stat = scsi_tt_write(hp->hp_ctlr, hp->hp_slave, sc->sc_punit,
1157 				     vmmap, NBPG, baddr, sc->sc_bshift);
1158 		if (stat) {
1159 			printf("sddump: scsi write error 0x%x\n", stat);
1160 			return (EIO);
1161 		}
1162 		maddr += NBPG;
1163 		baddr += ctod(1);
1164 	}
1165 	return (0);
1166 }
1167 #endif
1168