xref: /original-bsd/sys/dev/scsi/sd.c (revision 8bb6539f)
1 /*
2  * Copyright (c) 1990, 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)sd.c	5.2 (Berkeley) 07/13/92
12  *
13  * from: $Header: sd.c,v 1.18 92/06/11 17:55:56 torek Exp $
14  */
15 
16 /*
17  * SCSI CCS (Command Command Set) disk driver.
18  *
19  * MACHINE INDEPENDENT (do not put machine dependent goo in here!)
20  *
21  * (from sd.c,v 1.7 90/12/15 14:11:26 van Exp)
22  */
23 
24 #include "sys/param.h"
25 #include "sys/systm.h"
26 #include "sys/proc.h"
27 #include "sys/buf.h"
28 #include "sys/errno.h"
29 #include "sys/device.h"
30 #include "sys/disklabel.h"
31 #include "sys/dkstat.h"
32 #include "sys/disk.h"
33 #include "sys/ioctl.h"
34 #include "sys/malloc.h"
35 
36 #include "scsi/scsi.h"
37 #include "scsi/disk.h"
38 #include "scsi/scsivar.h"
39 #include "scsi/scsi_ioctl.h"
40 
41 #include "machine/cpu.h"
42 
43 #include "sdtrace.h"
44 
45 #define SUN_LABEL_HACK
46 
47 #ifdef SUN_LABEL_HACK
48 #include "sparc/sunos/sun_disklabel.h"
49 #endif
50 
51 /*
52  * Per-disk variables.
53  *
54  * sd_dk contains all the `disk' specific stuff (label/partitions,
55  * transfer rate, etc).  We need only things that are special to
56  * scsi disks.  Note that our blocks are in terms of DEV_BSIZE blocks.
57  */
58 struct sd_softc {
59 	struct	dkdevice sc_dk;	/* base disk device, must be first */
60 	struct	unit sc_unit;	/* scsi unit */
61 	pid_t	sc_format_pid;	/* process using "format" mode */
62 	u_char	sc_type;	/* drive type */
63 	u_char	sc_bshift;	/* convert device blocks to DEV_BSIZE blks */
64 	short	sc_flags;	/* see below */
65 	u_int	sc_blks;	/* number of blocks on device */
66 	int	sc_blksize;	/* device block size in bytes */
67 
68 	/* should be in dkdevice?? */
69 	struct	buf sc_tab;	/* transfer queue */
70 
71 	/* statistics */
72 	long	sc_resets;	/* number of times reset */
73 	long	sc_transfers;	/* count of total transfers */
74 	long	sc_partials;	/* count of `partial' transfers */
75 
76 	/* for user formatting */
77 	struct	scsi_cdb sc_cmd;
78 	struct	scsi_fmt_sense sc_sense;
79 };
80 
81 #define	SDF_ALIVE	1	/* drive OK for regular kernel use */
82 
83 /* definition of the autoconfig driver */
84 int	sdmatch __P((struct device *, struct cfdata *, void *));
85 void	sdattach __P((struct device *, struct device *, void *));
86 
87 struct cfdriver sdcd =
88     { NULL, "sd", sdmatch, sdattach, DV_DISK, sizeof(struct sd_softc) };
89 
90 /* definition of the unit driver, for hba */
91 void	sdigo __P((struct device *, struct scsi_cdb *));
92 void	sdgo __P((struct device *, struct scsi_cdb *));
93 void	sdintr __P((struct device *, int, int));
94 void	sdreset __P((struct unit *));
95 
96 static struct unitdriver sdunitdriver = { /*sdgo, sdintr*/ sdreset };
97 
98 /* definition of the disk driver, for kernel */
99 void	sdstrategy __P((struct buf *));
100 
101 #ifdef notyet
102 static struct sddkdriver = { sdstrategy };
103 #endif
104 
105 #ifdef DEBUG
106 int sddebug = 1;
107 #define SDB_ERROR	0x01
108 #define SDB_PARTIAL	0x02
109 #endif
110 
111 #define	sdunit(x)	(minor(x) >> 3)
112 #define sdpart(x)	(minor(x) & 0x7)
113 
114 #define	b_cylin		b_resid
115 
116 #define	SDRETRY		2
117 
118 /*
119  * Table of scsi commands users are allowed to access via `format'
120  * mode.  0 means not legal.  1 means `immediate' (doesn't need dma).
121  * -1 means needs dma and/or wait for intr (i.e., `slow').
122  */
123 static char legal_cmds[256] = {
124 /*****  0   1   2   3   4   5   6   7     8   9   A   B   C   D   E   F */
125 /*00*/	0,  0,  0,  0, -1,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
126 /*10*/	0,  0,  1,  0,  0,  1,  0,  0,    0,  0,  1,  0,  0,  0,  0,  0,
127 /*20*/	0,  0,  0,  0,  0,  1,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
128 /*30*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
129 /*40*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
130 /*50*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
131 /*60*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
132 /*70*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
133 /*80*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
134 /*90*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
135 /*a0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
136 /*b0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
137 /*c0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
138 /*d0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
139 /*e0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
140 /*f0*/	0,  0,  0,  0,  0,  0,  0,  0,    0,  0,  0,  0,  0,  0,  0,  0,
141 };
142 
143 int
144 sdmatch(parent, cf, aux)
145 	struct device *parent;
146 	register struct cfdata *cf;
147 	void *aux;
148 {
149 	register struct scsi_attach_args *sa = aux;
150 #ifdef DEBUG
151 	char *whynot;
152 #endif
153 
154 	/*
155 	 * unit number must match, or be given as `any'
156 	 */
157 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != sa->sa_unit)
158 		return (0);
159 	/*
160 	 * drive must be a disk, and of a kind we recognize
161 	 */
162 	if ((sa->sa_inq_status & STS_MASK) != STS_GOOD) {
163 #ifdef DEBUG
164 		whynot = "INQUIRY failed";
165 #endif
166 		goto notdisk;
167 	}
168 
169 	switch (sa->sa_si.si_type & TYPE_TYPE_MASK) {
170 
171 	case TYPE_DAD:		/* disk */
172 	case TYPE_WORM:		/* WORM */
173 	case TYPE_ROM:		/* CD-ROM */
174 	case TYPE_MO:		/* Magneto-optical */
175 	case TYPE_JUKEBOX:	/* medium changer */
176 		break;
177 
178 	default:
179 	notdisk:
180 #ifdef DEBUG
181 		whynot = "not a disk";
182 		printf("[not matching `sd' at unit %d: %s]\n",
183 		    sa->sa_unit, whynot);
184 #endif
185 		return (0);
186 	}
187 
188 	/*
189 	 * It is a disk of some kind; take it.  We will figure out
190 	 * the rest in the attach routine.
191 	 */
192 	return (1);
193 }
194 
195 /*
196  * Attach a disk (called after sdmatch returns true).
197  * Note that this routine is never reentered (so we can use statics).
198  */
199 void
200 sdattach(parent, self, aux)
201 	struct device *parent, *self;
202 	void *aux;
203 {
204 	register struct sd_softc *sc = (struct sd_softc *)self;
205 	register struct scsi_attach_args *sa = aux;
206 	register int i;
207 	char vendor[10], drive[17], rev[5];
208 	static u_char capbuf[8];
209 	static struct scsi_cdb cap = { CMD_READ_CAPACITY };
210 #ifdef SUN_LABEL_HACK
211 	static struct scsi_cdb rd0 = { CMD_READ10, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
212 	caddr_t sector;
213 #endif
214 
215 	/*
216 	 * Declare our existence.
217 	 */
218 	sc->sc_unit.u_driver = &sdunitdriver;
219 	scsi_establish(&sc->sc_unit, &sc->sc_dk.dk_dev, sa->sa_unit);
220 
221 	/*
222 	 * Figure out what kind of disk this is.
223 	 * We only accepted it if the inquiry succeeded, so
224 	 * we can inspect those fields.
225 	 */
226 	i = (sa->sa_si.si_version >> VER_ANSI_SHIFT) & VER_ANSI_MASK;
227 	if (i == 1 || i == 2) {
228 		scsi_inq_ansi((struct scsi_inq_ansi *)&sa->sa_si,
229 		    vendor, drive, rev);
230 		printf(": %s %s", vendor, drive);
231 		/* XXX should we even ever bother printing this? */
232 		if (rev[0])
233 			printf(" %s", rev);
234 	} else {
235 		/* bleah */
236 		bcopy("<unknown>", vendor, 10);
237 		bcopy("<unknown>", drive, 10);
238 		printf(": type 0x%x, qual 0x%x, ver %d",
239 		    sa->sa_si.si_type, sa->sa_si.si_qual,
240 		    sa->sa_si.si_version);
241 	}
242 
243 #ifdef notyet
244 	sc->sc_dk.dk_driver = &sddkdriver;
245 	dk_establish(&sc->sc_dk);
246 	/* READ DISK LABEL HERE, UNLESS REMOVABLE MEDIUM... NEEDS THOUGHT */
247 #endif
248 
249 	CDB10(&cap)->cdb_lun_rel = sc->sc_unit.u_unit << 5;
250 	i = (*sc->sc_unit.u_hbd->hd_icmd)(sc->sc_unit.u_hba,
251 	    sc->sc_unit.u_targ, &cap, (char *)capbuf, sizeof capbuf, B_READ);
252 	i &= STS_MASK;
253 	if (i == STS_GOOD) {
254 #define	NUMBER(p) (((p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3])
255 		sc->sc_blks = NUMBER(&capbuf[0]);
256 		sc->sc_blksize = NUMBER(&capbuf[4]);
257 	} else if (i == STS_CHECKCOND &&
258 	    (strcmp(vendor, "HP") == 0 && strcmp(drive, "S6300.650A") == 0)) {
259 		/* XXX unformatted or nonexistent MO medium: fake it */
260 		sc->sc_blks = 318664;
261 		sc->sc_blksize = 1024;
262 	} else {
263 		printf(": unable to determine drive capacity [sts=%x]\n", i);
264 		return;
265 	}
266 	/* return value from read capacity is last valid block, not nblocks */
267 	sc->sc_blks++;
268 	printf(", %u %d byte blocks\n", sc->sc_blks, sc->sc_blksize);
269 	if (sc->sc_blksize != DEV_BSIZE) {
270 		for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
271 			++sc->sc_bshift;
272 		if (i != DEV_BSIZE) {
273 			printf("%s: blksize not multiple of %d: cannot use\n",
274 			    sc->sc_dk.dk_dev.dv_xname, DEV_BSIZE);
275 			return;
276 		}
277 		sc->sc_blks <<= sc->sc_bshift;
278 	}
279 	sc->sc_type = sa->sa_si.si_type;	/* sufficient? */
280 	sc->sc_dk.dk_wpms = 32 * (60 * DEV_BSIZE / 2);	/* XXX */
281 
282 	sc->sc_dk.dk_label.d_secsize = 512;	/* XXX */
283 
284 #ifdef SUN_LABEL_HACK
285 	sector = (caddr_t)malloc(sc->sc_blksize, M_DEVBUF, M_NOWAIT);
286 	CDB10(&rd0)->cdb_lun_rel = sc->sc_unit.u_unit << 5;
287 	i = (*sc->sc_unit.u_hbd->hd_icmd)(sc->sc_unit.u_hba,
288 	    sc->sc_unit.u_targ, &rd0, sector, sc->sc_blksize, B_READ);
289 	if (i == STS_GOOD) {
290 		printf("%s: <%s>\n", sc->sc_dk.dk_dev.dv_xname,
291 		    ((struct sun_disklabel *)sector)->sl_text);
292 		if (sun_disklabel(sector, &sc->sc_dk.dk_label))
293 			sc->sc_flags |= SDF_ALIVE;
294 		else
295 			printf("%s: sun_disklabel fails\n",
296 			    sc->sc_dk.dk_dev.dv_xname);
297 	} else
298 		printf("%s: could not read sector 0 for disk label\n",
299 		    sc->sc_dk.dk_dev.dv_xname);
300 	free(sector, M_DEVBUF);
301 #endif
302 }
303 
304 /*
305  * Reset a disk, after a SCSI bus reset.
306  *
307  * XXX untested and probably incomplete/incorrect
308  */
309 void
310 sdreset(u)
311 	register struct unit *u;
312 {
313 	register struct sd_softc *sc = (struct sd_softc *)u->u_dev;
314 
315 	printf(" %s", sc->sc_dk.dk_dev.dv_xname);
316 	sc->sc_resets++;
317 }
318 
319 /* dev_t is short, must use prototype syntax */
320 int
321 sdopen(dev_t dev, int flags, int ifmt, struct proc *p)
322 {
323 	register int unit = sdunit(dev);
324 	register struct sd_softc *sc;
325 
326 	if (unit >= sdcd.cd_ndevs || (sc = sdcd.cd_devs[unit]) == NULL)
327 		return (ENXIO);
328 	if ((sc->sc_flags & SDF_ALIVE) == 0 &&
329 	    !suser(p->p_ucred, &p->p_acflag))
330 		return (ENXIO);
331 	return (0);
332 }
333 
334 int
335 sdclose(dev_t dev, int flags, int ifmt, struct proc *p)
336 {
337 	register struct sd_softc *sc = sdcd.cd_devs[sdunit(dev)];
338 
339 	sc->sc_format_pid = 0;
340 	return (0);
341 }
342 
343 /*
344  * This routine is called for partial block transfers and non-aligned
345  * transfers (the latter only being possible on devices with a block size
346  * larger than DEV_BSIZE).  The operation is performed in three steps
347  * using a locally allocated buffer:
348  *	1. transfer any initial partial block
349  *	2. transfer full blocks
350  *	3. transfer any final partial block
351  */
352 static void
353 sdlblkstrat(bp, bsize)
354 	register struct buf *bp;
355 	register int bsize;
356 {
357 	register int bn, resid, boff, count;
358 	register caddr_t addr, cbuf;
359 	struct buf tbp;
360 
361 	cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
362 	bzero((caddr_t)&tbp, sizeof tbp);
363 	tbp.b_proc = curproc;
364 	tbp.b_dev = bp->b_dev;
365 	bn = bp->b_blkno;
366 	resid = bp->b_bcount;
367 	addr = bp->b_un.b_addr;
368 #ifdef DEBUG
369 	if (sddebug & SDB_PARTIAL)
370 		printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
371 		       bp, bp->b_flags, bn, resid, addr);
372 #endif
373 
374 	while (resid > 0) {
375 		boff = dbtob(bn) & (bsize - 1);
376 		if (boff || resid < bsize) {
377 			struct sd_softc *sc = sdcd.cd_devs[sdunit(bp->b_dev)];
378 			sc->sc_partials++;
379 			count = min(resid, bsize - boff);
380 			tbp.b_flags = B_BUSY | B_READ;
381 			tbp.b_blkno = bn - btodb(boff);
382 			tbp.b_un.b_addr = cbuf;
383 			tbp.b_bcount = bsize;
384 #ifdef DEBUG
385 			if (sddebug & SDB_PARTIAL)
386 				printf(" readahead: bn %x cnt %x off %x addr %x\n",
387 				       tbp.b_blkno, count, boff, addr);
388 #endif
389 			sdstrategy(&tbp);
390 			biowait(&tbp);
391 			if (tbp.b_flags & B_ERROR) {
392 				bp->b_flags |= B_ERROR;
393 				bp->b_error = tbp.b_error;
394 				break;
395 			}
396 			if (bp->b_flags & B_READ) {
397 				bcopy(&cbuf[boff], addr, count);
398 				goto done;
399 			}
400 			bcopy(addr, &cbuf[boff], count);
401 #ifdef DEBUG
402 			if (sddebug & SDB_PARTIAL)
403 				printf(" writeback: bn %x cnt %x off %x addr %x\n",
404 				       tbp.b_blkno, count, boff, addr);
405 #endif
406 		} else {
407 			count = resid & ~(bsize - 1);
408 			tbp.b_blkno = bn;
409 			tbp.b_un.b_addr = addr;
410 			tbp.b_bcount = count;
411 #ifdef DEBUG
412 			if (sddebug & SDB_PARTIAL)
413 				printf(" fulltrans: bn %x cnt %x addr %x\n",
414 				       tbp.b_blkno, count, addr);
415 #endif
416 		}
417 		tbp.b_flags = B_BUSY | (bp->b_flags & B_READ);
418 		sdstrategy(&tbp);
419 		biowait(&tbp);
420 		if (tbp.b_flags & B_ERROR) {
421 			bp->b_flags |= B_ERROR;
422 			bp->b_error = tbp.b_error;
423 			break;
424 		}
425 done:
426 		bn += btodb(count);
427 		resid -= count;
428 		addr += count;
429 #ifdef DEBUG
430 		if (sddebug & SDB_PARTIAL)
431 			printf(" done: bn %x resid %x addr %x\n",
432 			       bn, resid, addr);
433 #endif
434 	}
435 	free(cbuf, M_DEVBUF);
436 	biodone(bp);
437 }
438 
439 /*
440  * Start a transfer on sc as described by bp
441  * (i.e., call hba or target start).
442  * If in format mode, we may not need dma.
443  */
444 #define sdstart(sc, bp) { \
445 	SD_TRACE(T_START, sc, bp); \
446 	if ((sc)->sc_format_pid && legal_cmds[(sc)->sc_cmd.cdb_bytes[0]] > 0) \
447 		(*(sc)->sc_unit.u_start)((sc)->sc_unit.u_updev, \
448 		    &(sc)->sc_unit.u_forw, (struct buf *)NULL, \
449 		    sdigo, &(sc)->sc_dk.dk_dev); \
450 	else \
451 		(*(sc)->sc_unit.u_start)((sc)->sc_unit.u_updev, \
452 		    &(sc)->sc_unit.u_forw, bp, sdgo, &(sc)->sc_dk.dk_dev); \
453 }
454 
455 void
456 sdstrategy(bp)
457 	register struct buf *bp;
458 {
459 	register struct sd_softc *sc = sdcd.cd_devs[sdunit(bp->b_dev)];
460 	register int s;
461 
462 	if (sc->sc_format_pid) {
463 		/* XXXXXXXXX SHOULD NOT COMPARE curproc IN HERE!?! */
464 		/*
465 		 * In format mode, only allow the owner to mess
466 		 * with the drive.  Skip all the partition checks.
467 		 */
468 		if (sc->sc_format_pid != curproc->p_pid) {
469 			bp->b_error = EPERM;
470 			bp->b_flags |= B_ERROR;
471 			biodone(bp);
472 			return;
473 		}
474 		bp->b_cylin = 0;
475 	} else {
476 		register daddr_t bn = bp->b_blkno;
477 		register int sz = howmany(bp->b_bcount, DEV_BSIZE);
478 		register struct partition *p;
479 
480 		/*
481 		 * Make sure transfer is within partition.
482 		 * If it starts at the end, return EOF; if
483 		 * it extends past the end, truncate it.
484 		 */
485 		p = &sc->sc_dk.dk_label.d_partitions[sdpart(bp->b_dev)];
486 		if ((unsigned)bn >= p->p_size) {
487 			if ((unsigned)bn > p->p_size) {
488 				bp->b_error = EINVAL;
489 				bp->b_flags |= B_ERROR;
490 			} else
491 				bp->b_resid = bp->b_bcount;
492 			biodone(bp);
493 			return;
494 		}
495 		if (bn + sz > p->p_size) {
496 			sz = p->p_size - bn;
497 			bp->b_bcount = dbtob(sz);
498 		}
499 		/*
500 		 * Non-aligned or partial-block transfers handled specially.
501 		 * SHOULD THIS BE AT A HIGHER LEVEL?
502 		 */
503 		s = sc->sc_blksize - 1;
504 		if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
505 			sdlblkstrat(bp, sc->sc_blksize);
506 			return;
507 		}
508 		bp->b_cylin = (bn + p->p_offset) >> sc->sc_bshift;
509 	}
510 
511 	/*
512 	 * Transfer valid, or format mode.  Queue the request
513 	 * on the drive, and maybe try to start it.
514 	 */
515 	s = splbio();
516 	disksort(&sc->sc_tab, bp);
517 	if (sc->sc_tab.b_active == 0) {
518 		sc->sc_tab.b_active = 1;
519 		sdstart(sc, bp);
520 	}
521 	splx(s);
522 }
523 
524 int
525 sderror(sc, stat)
526 	register struct sd_softc *sc;
527 	register int stat;
528 {
529 	register struct scsi_sense *sn;
530 	int retry = 0;
531 
532 	sc->sc_sense.status = stat;
533 	if ((stat & STS_MASK) == STS_CHECKCOND) {
534 		sn = (struct scsi_sense *)sc->sc_sense.sense;
535 		stat = scsi_request_sense(sc->sc_unit.u_hba,
536 		    sc->sc_unit.u_targ, sc->sc_unit.u_unit,
537 		    (caddr_t)sn, sizeof sc->sc_sense.sense);
538 		sc->sc_sense.status = stat;	/* ??? */
539 		if ((stat & STS_MASK) != STS_GOOD) {
540 			printf("%s: sense failed, status %x\n",
541 			    sc->sc_dk.dk_dev.dv_xname, stat);
542 			return (0);
543 		}
544 		printf("%s: scsi sense class %d, code %d",
545 		    sc->sc_dk.dk_dev.dv_xname,
546 		    SENSE_ECLASS(sn), SENSE_ECODE(sn));
547 		if (SENSE_ISXSENSE(sn) && XSENSE_ISSTD(sn)) {
548 			int key;
549 
550 			/*
551 			 * Standard extended sense: can examine sense key
552 			 * and (if valid) info.
553 			 */
554 			key = XSENSE_KEY(sn);
555 			printf(", key %d", key);
556 			if (XSENSE_IVALID(sn))
557 				printf(", blk %d", XSENSE_INFO(sn));
558 			/* no sense or recovered error, try again */
559 			if (key == 0 || key == 1)
560 				retry = 1;
561 		}
562 		printf("\n");
563 	}
564 	return (retry);
565 }
566 
567 /*
568  * sdigo is called from the hba driver when it has got the scsi bus
569  * for us, and we were doing a format op that did not need dma.
570  */
571 void
572 sdigo(sc0, cdb)
573 	struct device *sc0;
574 	struct scsi_cdb *cdb;
575 {
576 	register struct sd_softc *sc = (struct sd_softc *)sc0;
577 	register struct buf *bp = sc->sc_tab.b_actf;
578 	register int stat;
579 
580 	stat = (*sc->sc_unit.u_hbd->hd_icmd)(sc->sc_unit.u_hba,
581 	    sc->sc_unit.u_targ, &sc->sc_cmd, bp->b_un.b_addr, bp->b_bcount,
582 	    bp->b_flags & B_READ);
583 	sc->sc_sense.status = stat;
584 	if (stat & 0xfe) {		/* XXX */
585 		(void) sderror(sc, stat);
586 		bp->b_flags |= B_ERROR;
587 		bp->b_error = EIO;
588 	}
589 	/*
590 	 * Done with SCSI bus, before we `ought' to be.  Release it.
591 	 */
592 	(*sc->sc_unit.u_rel)(sc->sc_unit.u_updev);
593 	bp->b_resid = 0;
594 	sc->sc_tab.b_errcnt = 0;
595 	sc->sc_tab.b_actf = bp->b_actf;
596 	biodone(bp);
597 	if ((bp = sc->sc_tab.b_actf) == NULL)
598 		sc->sc_tab.b_active = 0;
599 	else
600 		sdstart(sc, bp);
601 }
602 
603 /*
604  * sdgo is called from the hba driver or target code when it has
605  * allocated the scsi bus and DMA resources and target datapath for us.
606  */
607 void
608 sdgo(sc0, cdb)
609 	struct device *sc0;
610 	register struct scsi_cdb *cdb;
611 {
612 	register struct sd_softc *sc = (struct sd_softc *)sc0;
613 	register struct buf *bp = sc->sc_tab.b_actf;
614 	register int n;
615 	register unsigned int u;
616 
617 	SD_TRACE(T_MKCDB, sc, bp);
618 	if (sc->sc_format_pid) {
619 		*cdb = sc->sc_cmd;
620 		n = 0;
621 	} else {
622 		CDB10(cdb)->cdb_cmd = bp->b_flags & B_READ ? CMD_READ10 :
623 		    CMD_WRITE10;
624 		CDB10(cdb)->cdb_lun_rel = sc->sc_unit.u_unit << 5;
625 		u = bp->b_cylin;
626 		CDB10(cdb)->cdb_lbah = u >> 24;
627 		CDB10(cdb)->cdb_lbahm = u >> 16;
628 		CDB10(cdb)->cdb_lbalm = u >> 8;
629 		CDB10(cdb)->cdb_lbal = u;
630 		CDB10(cdb)->cdb_xxx = 0;
631 		n = sc->sc_blksize - 1;
632 		u = (bp->b_bcount + n) >> (DEV_BSHIFT + sc->sc_bshift);
633 		CDB10(cdb)->cdb_lenh = u >> 8;
634 		CDB10(cdb)->cdb_lenl = u;
635 		CDB10(cdb)->cdb_ctrl = 0;
636 		n = (bp->b_bcount & n) != 0;
637 #ifdef DEBUG
638 		if (n)
639 			printf("%s: partial block xfer -- %x bytes\n",
640 			    sc->sc_dk.dk_dev.dv_xname, bp->b_bcount);
641 #endif
642 		sc->sc_transfers++;
643 	}
644 	if ((*sc->sc_unit.u_go)(sc->sc_unit.u_updev, sc->sc_unit.u_targ,
645 	    sdintr, (void *)sc, bp, n) == 0) {
646 #ifdef notyet
647 		sc->sc_dk.dk_busy = 1;
648 		sc->sc_dk.dk_seek++;	/* XXX */
649 		sc->sc_dk.dk_xfer++;
650 		sc->sc_dk.dk_wds += bp->b_bcount >> 6;
651 #endif
652 		return;
653 	}
654 	/*
655 	 * Some sort of nasty unrecoverable error: clobber the
656 	 * transfer.  Call the bus release function first, though.
657 	 */
658 	(*sc->sc_unit.u_rel)(sc->sc_unit.u_updev);
659 #ifdef DEBUG
660 	if (sddebug & SDB_ERROR)
661 		printf("%s: sdgo: %s adr %d blk %d len %d ecnt %d\n",
662 		    sc->sc_dk.dk_dev.dv_xname,
663 		    bp->b_flags & B_READ? "read" : "write",
664 		    bp->b_un.b_addr, bp->b_cylin, bp->b_bcount,
665 		    sc->sc_tab.b_errcnt);
666 #endif
667 	bp->b_flags |= B_ERROR;
668 	bp->b_error = EIO;
669 	bp->b_resid = 0;
670 	sc->sc_tab.b_errcnt = 0;
671 	sc->sc_tab.b_actf = bp->b_actf;
672 	biodone(bp);
673 	if ((bp = sc->sc_tab.b_actf) == NULL)
674 		sc->sc_tab.b_active = 0;
675 	else
676 		sdstart(sc, bp);
677 }
678 
679 /*
680  * A transfer finished (or, someday, disconnected).
681  * We are already off the target/hba queues.
682  * Restart this one for error recovery, or start the next, as appropriate.
683  */
684 void
685 sdintr(sc0, stat, resid)
686 	struct device *sc0;
687 	int stat, resid;
688 {
689 	register struct sd_softc *sc = (struct sd_softc *)sc0;
690 	register struct buf *bp = sc->sc_tab.b_actf;
691 	int retry;
692 
693 	if (bp == NULL)
694 		panic("sdintr");
695 	SD_TRACE(T_INTR, sc, bp);
696 #ifdef notyet
697 	sc->sc_dk.dk_busy = 0;
698 #endif
699 	if ((stat & STS_MASK) != STS_GOOD) {
700 #ifdef DEBUG
701 		if (sddebug & SDB_ERROR)
702 			printf("%s: sdintr scsi status 0x%x resid %d\n",
703 			    sc->sc_dk.dk_dev.dv_xname, stat, resid);
704 #endif
705 		retry = sderror(sc, stat);
706 		if (retry && ++sc->sc_tab.b_errcnt <= SDRETRY) {
707 			printf("%s: retry %d\n",
708 			    sc->sc_dk.dk_dev.dv_xname, sc->sc_tab.b_errcnt);
709 			goto restart;
710 		}
711 		bp->b_flags |= B_ERROR;
712 		bp->b_error = EIO;
713 	}
714 	bp->b_resid = resid;
715 	sc->sc_tab.b_errcnt = 0;
716 	sc->sc_tab.b_actf = bp->b_actf;
717 	biodone(bp);
718 	if ((bp = sc->sc_tab.b_actf) == NULL)
719 		sc->sc_tab.b_active = 0;
720 	else {
721 restart:
722 		sdstart(sc, bp);
723 	}
724 }
725 
726 int
727 sdioctl(dev_t dev, int cmd, register caddr_t data, int flag, struct proc *p)
728 {
729 	register struct sd_softc *sc = sdcd.cd_devs[sdunit(dev)];
730 #ifdef COMPAT_SUNOS
731 	int error;
732 
733 	error = sun_dkioctl(&sc->sc_dk, cmd, data, sdpart(dev));
734 	if (error >= 0)
735 		return (error);
736 #endif
737 	switch (cmd) {
738 
739 	case SDIOCSFORMAT:
740 		/* take this device into or out of "format" mode */
741 		if (!suser(p->p_ucred, &p->p_acflag))
742 			return (EPERM);
743 		if (*(int *)data) {
744 			if (sc->sc_format_pid)
745 				return (EPERM);
746 			sc->sc_format_pid = p->p_pid;
747 		} else
748 			sc->sc_format_pid = 0;
749 		break;
750 
751 	case SDIOCGFORMAT:
752 		/* find out who has the device in format mode */
753 		*(int *)data = sc->sc_format_pid;
754 		break;
755 
756 	case SDIOCSCSICOMMAND:
757 #define cdb ((struct scsi_cdb *)data)
758 		/*
759 		 * Save what user gave us as SCSI cdb to use with next
760 		 * read or write to the char device.
761 		 */
762 		if (sc->sc_format_pid != p->p_pid)
763 			return (EPERM);
764 		if (legal_cmds[cdb->cdb_bytes[0]] == 0)
765 			return (EINVAL);
766 		sc->sc_cmd = *cdb;
767 #undef	cdb
768 		break;
769 
770 	case SDIOCSENSE:
771 		/*
772 		 * return the SCSI sense data saved after the last
773 		 * operation that completed with "check condition" status.
774 		 */
775 		sc->sc_sense = *(struct scsi_fmt_sense *)data;
776 		break;
777 
778 	default:
779 		return (ENOTTY);
780 	}
781 	return (0);
782 }
783 
784 int
785 sdsize(dev_t dev)
786 {
787 	register int unit = sdunit(dev);
788 	register struct sd_softc *sc;
789 
790 	if (unit >= sdcd.cd_ndevs || (sc = sdcd.cd_devs[unit]) == NULL ||
791 	    (sc->sc_flags & SDF_ALIVE) == 0)
792 		return (-1);
793 	return (sc->sc_dk.dk_label.d_partitions[sdpart(dev)].p_size);
794 }
795 
796 /*
797  * Write `len' bytes from address `addr' to drive and partition in `dev',
798  * at block blkoff from the beginning of the partition.  The address is
799  * either kernel virtual or physical (some machines may never use one or
800  * the other, but we need it in the protocol to stay machine-independent).
801  */
802 int
803 sddump(dev_t dev, daddr_t blkoff, caddr_t addr, int len)
804 {
805 	register struct sd_softc *sc;
806 	register struct partition *p;
807 	register daddr_t bn, n, nblks;
808 	register struct hba_softc *hba;
809 	register int stat, unit;
810 	struct scsi_cdb cdb;
811 
812 	/* drive ok? */
813 	unit = sdunit(dev);
814 	if (unit >= sdcd.cd_ndevs || (sc = sdcd.cd_devs[unit]) == NULL ||
815 	    (sc->sc_flags & SDF_ALIVE) == 0)
816 		return (ENXIO);
817 
818 	/* blocks in range? */
819 	p = &sc->sc_dk.dk_label.d_partitions[sdpart(dev)];
820 	n = (len + sc->sc_blksize - 1) >> DEV_BSHIFT;
821 	if (blkoff < 0 || blkoff >= p->p_size || blkoff + n > p->p_size)
822 		return (EINVAL);
823 	bn = blkoff + p->p_offset;
824 	bn >>= sc->sc_bshift;
825 
826 	/* scsi bus idle? */
827 	hba = sc->sc_unit.u_hba;
828 	if (hba->hba_head) {
829 		(*hba->hba_driver->hd_reset)(hba, 0);
830 		printf("[reset %s] ", sc->sc_dk.dk_dev.dv_xname);
831 	}
832 
833 	CDB10(&cdb)->cdb_cmd = CMD_WRITE10;
834 	CDB10(&cdb)->cdb_lun_rel = sc->sc_unit.u_unit << 5;
835 	CDB10(&cdb)->cdb_xxx = 0;
836 	CDB10(&cdb)->cdb_ctrl = 0;
837 
838 #define	DUMP_MAX	(32 * 1024)	/* no more than 32k per write */
839 	for (;;) {
840 		if ((n = len) > DUMP_MAX)
841 			n = DUMP_MAX;
842 		CDB10(&cdb)->cdb_lbah = bn >> 24;
843 		CDB10(&cdb)->cdb_lbahm = bn >> 16;
844 		CDB10(&cdb)->cdb_lbalm = bn >> 8;
845 		CDB10(&cdb)->cdb_lbal = bn;
846 		nblks = n >> (DEV_BSHIFT + sc->sc_bshift);
847 		CDB10(&cdb)->cdb_lenh = nblks >> 8;
848 		CDB10(&cdb)->cdb_lenl = nblks;
849 		stat = hba->hba_driver->hd_dump(hba, sc->sc_unit.u_targ,
850 		    &cdb, addr, n);
851 		if ((stat & STS_MASK) != STS_GOOD) {
852 			printf("%s: scsi write error 0x%x\ndump ",
853 			    sc->sc_dk.dk_dev.dv_xname, stat);
854 			return (EIO);
855 		}
856 		if ((len -= n) == 0)
857 			return (0);
858 		addr += n;
859 		bn += nblks;
860 	}
861 }
862