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