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