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