date and time created 92/07/20 12:09:06 by bostic
[unix-history] / usr / src / sys / hp300 / dev / sd.c
CommitLineData
60f56dfc
KM
1/*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * 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 *
479c0df7 10 * @(#)sd.c 7.14 (Berkeley) %G%
60f56dfc
KM
11 */
12
13/*
14 * SCSI CCS (Command Command Set) disk driver.
15 */
16#include "sd.h"
17#if NSD > 0
18
19#ifndef lint
9acfa6cd 20static char rcsid[] = "$Header: /usr/src/sys/hp300/dev/RCS/sd.c,v 1.2 92/04/10 20:48:35 mike Exp $";
60f56dfc
KM
21#endif
22
b28b3a13
KB
23#include "sys/param.h"
24#include "sys/systm.h"
25#include "sys/buf.h"
b28b3a13
KB
26#include "sys/dkstat.h"
27#include "sys/disklabel.h"
b28b3a13 28#include "sys/malloc.h"
b28b3a13 29#include "sys/proc.h"
60f56dfc 30
9acfa6cd 31#include "hp/dev/device.h"
88f29710 32#include "scsireg.h"
b28b3a13 33#include "vm/vm_param.h"
88f29710 34#include "vm/lock.h"
b28b3a13 35#include "vm/vm_prot.h"
e4fc9b79 36#include "vm/pmap.h"
22d09b27 37
60f56dfc
KM
38extern int scsi_test_unit_rdy();
39extern int scsi_request_sense();
40extern int scsi_inquiry();
41extern int scsi_read_capacity();
42extern int scsi_tt_write();
43extern int scsireq();
44extern int scsiustart();
45extern int scsigo();
46extern void scsifree();
47extern void scsireset();
98d7aa4d 48extern void scsi_delay();
60f56dfc 49
60f56dfc 50extern void disksort();
60f56dfc
KM
51extern void biodone();
52extern int physio();
53extern void TBIS();
54
55int sdinit();
56void sdstrategy(), sdstart(), sdustart(), sdgo(), sdintr();
57
58struct driver sddriver = {
59 sdinit, "sd", (int (*)())sdstart, (int (*)())sdgo, (int (*)())sdintr,
60};
61
62struct size {
63 u_long strtblk;
64 u_long endblk;
65 int nblocks;
66};
67
68struct sdinfo {
69 struct size part[8];
70};
71
72/*
73 * since the SCSI standard tends to hide the disk structure, we define
74 * partitions in terms of DEV_BSIZE blocks. The default partition table
75 * (for an unlabeled disk) reserves 512K for a boot area, has an 8 meg
76 * root and 32 meg of swap. The rest of the space on the drive goes in
77 * the G partition. As usual, the C partition covers the entire disk
78 * (including the boot area).
79 */
80struct sdinfo sddefaultpart = {
81 1024, 17408, 16384 , /* A */
82 17408, 82944, 65536 , /* B */
83 0, 0, 0 , /* C */
84 17408, 115712, 98304 , /* D */
85 115712, 218112, 102400 , /* E */
86 218112, 0, 0 , /* F */
87 82944, 0, 0 , /* G */
88 115712, 0, 0 , /* H */
89};
90
91struct sd_softc {
92 struct hp_device *sc_hd;
93 struct devqueue sc_dq;
94 int sc_format_pid; /* process using "format" mode */
95 short sc_flags;
96 short sc_type; /* drive type */
97 short sc_punit; /* physical unit (scsi lun) */
98 u_short sc_bshift; /* convert device blocks to DEV_BSIZE blks */
99 u_int sc_blks; /* number of blocks on device */
100 int sc_blksize; /* device block size in bytes */
101 u_int sc_wpms; /* average xfer rate in 16 bit wds/sec. */
102 struct sdinfo sc_info; /* drive partition table & label info */
103} sd_softc[NSD];
104
105/* sc_flags values */
106#define SDF_ALIVE 0x1
9acfa6cd
MH
107#define SDF_WANTED 0x2
108#define SDF_RMEDIA 0x4
60f56dfc
KM
109
110#ifdef DEBUG
111int sddebug = 1;
112#define SDB_ERROR 0x01
113#define SDB_PARTIAL 0x02
114#endif
115
116struct sdstats {
117 long sdresets;
118 long sdtransfers;
119 long sdpartials;
120} sdstats[NSD];
121
122struct buf sdtab[NSD];
60f56dfc
KM
123struct scsi_fmt_cdb sdcmd[NSD];
124struct scsi_fmt_sense sdsense[NSD];
125
126static struct scsi_fmt_cdb sd_read_cmd = { 10, CMD_READ_EXT };
127static struct scsi_fmt_cdb sd_write_cmd = { 10, CMD_WRITE_EXT };
128
88f29710 129#define sdunit(x) (minor(x) >> 3)
60f56dfc
KM
130#define sdpart(x) (minor(x) & 0x7)
131#define sdpunit(x) ((x) & 7)
132#define b_cylin b_resid
22d09b27 133
60f56dfc
KM
134#define SDRETRY 2
135
136/*
137 * Table of scsi commands users are allowed to access via "format"
138 * mode. 0 means not legal. 1 means "immediate" (doesn't need dma).
139 * -1 means needs dma and/or wait for intr.
140 */
141static char legal_cmds[256] = {
142/***** 0 1 2 3 4 5 6 7 8 9 A B C D E F */
143/*00*/ 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
144/*10*/ 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
145/*20*/ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146/*30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147/*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
148/*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149/*60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
150/*70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151/*80*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
152/*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
153/*a0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
154/*b0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
155/*c0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
156/*d0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157/*e0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158/*f0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159};
160
161static struct scsi_inquiry inqbuf;
162static struct scsi_fmt_cdb inq = {
163 6,
164 CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0
165};
166
167static u_char capbuf[8];
168struct scsi_fmt_cdb cap = {
169 10,
170 CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0
171};
172
173static int
174sdident(sc, hd)
175 struct sd_softc *sc;
176 struct hp_device *hd;
177{
178 int unit;
179 register int ctlr, slave;
180 register int i;
181 register int tries = 10;
98d7aa4d 182 char idstr[32];
22d09b27 183 int ismo = 0;
60f56dfc
KM
184
185 ctlr = hd->hp_ctlr;
186 slave = hd->hp_slave;
187 unit = sc->sc_punit;
98d7aa4d 188 scsi_delay(-1);
60f56dfc
KM
189
190 /*
191 * See if unit exists and is a disk then read block size & nblocks.
192 */
193 while ((i = scsi_test_unit_rdy(ctlr, slave, unit)) != 0) {
22d09b27
KM
194 if (i == -1 || --tries < 0) {
195 if (ismo)
196 break;
60f56dfc 197 /* doesn't exist or not a CCS device */
98d7aa4d 198 goto failed;
22d09b27 199 }
60f56dfc
KM
200 if (i == STS_CHECKCOND) {
201 u_char sensebuf[128];
202 struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf;
203
204 scsi_request_sense(ctlr, slave, unit, sensebuf,
205 sizeof(sensebuf));
22d09b27
KM
206 if (sp->class == 7)
207 switch (sp->key) {
208 /* not ready -- might be MO with no media */
209 case 2:
210 if (sp->len == 12 &&
211 sensebuf[12] == 10) /* XXX */
212 ismo = 1;
213 break;
60f56dfc 214 /* drive doing an RTZ -- give it a while */
22d09b27
KM
215 case 6:
216 DELAY(1000000);
217 break;
218 default:
219 break;
220 }
60f56dfc
KM
221 }
222 DELAY(1000);
223 }
22d09b27
KM
224 /*
225 * Find out about device
226 */
227 if (scsi_immed_command(ctlr, slave, unit, &inq,
228 (u_char *)&inqbuf, sizeof(inqbuf), B_READ))
98d7aa4d 229 goto failed;
60f56dfc
KM
230 switch (inqbuf.type) {
231 case 0: /* disk */
232 case 4: /* WORM */
233 case 5: /* CD-ROM */
234 case 7: /* Magneto-optical */
235 break;
236 default: /* not a disk */
98d7aa4d 237 goto failed;
60f56dfc 238 }
22d09b27 239 /*
98d7aa4d 240 * Get a usable id string
22d09b27 241 */
98d7aa4d
MH
242 if (inqbuf.version != 1) {
243 bcopy("UNKNOWN", &idstr[0], 8);
244 bcopy("DRIVE TYPE", &idstr[8], 11);
245 } else {
246 bcopy((caddr_t)&inqbuf.vendor_id, (caddr_t)idstr, 28);
247 for (i = 27; i > 23; --i)
248 if (idstr[i] != ' ')
249 break;
250 idstr[i+1] = 0;
251 for (i = 23; i > 7; --i)
252 if (idstr[i] != ' ')
253 break;
254 idstr[i+1] = 0;
255 for (i = 7; i >= 0; --i)
256 if (idstr[i] != ' ')
257 break;
258 idstr[i+1] = 0;
22d09b27
KM
259 }
260 i = scsi_immed_command(ctlr, slave, unit, &cap,
261 (u_char *)&capbuf, sizeof(capbuf), B_READ);
262 if (i) {
98d7aa4d
MH
263 if (i != STS_CHECKCOND ||
264 bcmp(&idstr[0], "HP", 3) ||
265 bcmp(&idstr[8], "S6300.650A", 11))
266 goto failed;
22d09b27 267 /* XXX unformatted or non-existant MO media; fake it */
98d7aa4d
MH
268 sc->sc_blks = 318664;
269 sc->sc_blksize = 1024;
22d09b27
KM
270 } else {
271 sc->sc_blks = *(u_int *)&capbuf[0];
272 sc->sc_blksize = *(int *)&capbuf[4];
273 }
274 /* return value of read capacity is last valid block number */
275 sc->sc_blks++;
60f56dfc
KM
276
277 if (inqbuf.version != 1)
278 printf("sd%d: type 0x%x, qual 0x%x, ver %d", hd->hp_unit,
279 inqbuf.type, inqbuf.qual, inqbuf.version);
98d7aa4d 280 else
60f56dfc
KM
281 printf("sd%d: %s %s rev %s", hd->hp_unit, idstr, &idstr[8],
282 &idstr[24]);
60f56dfc 283 printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize);
9acfa6cd
MH
284 if (inqbuf.qual & 0x80)
285 sc->sc_flags |= SDF_RMEDIA;
60f56dfc
KM
286 if (sc->sc_blksize != DEV_BSIZE) {
287 if (sc->sc_blksize < DEV_BSIZE) {
288 printf("sd%d: need %d byte blocks - drive ignored\n",
289 unit, DEV_BSIZE);
98d7aa4d 290 goto failed;
60f56dfc
KM
291 }
292 for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
293 ++sc->sc_bshift;
294 sc->sc_blks <<= sc->sc_bshift;
295 }
296 sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2); /* XXX */
98d7aa4d 297 scsi_delay(0);
60f56dfc 298 return(inqbuf.type);
98d7aa4d
MH
299failed:
300 scsi_delay(0);
301 return(-1);
60f56dfc
KM
302}
303
304int
305sdinit(hd)
306 register struct hp_device *hd;
307{
308 register struct sd_softc *sc = &sd_softc[hd->hp_unit];
309
310 sc->sc_hd = hd;
9acfa6cd 311 sc->sc_flags = 0;
60f56dfc
KM
312 sc->sc_punit = sdpunit(hd->hp_flags);
313 sc->sc_type = sdident(sc, hd);
314 if (sc->sc_type < 0)
315 return(0);
316 sc->sc_dq.dq_ctlr = hd->hp_ctlr;
317 sc->sc_dq.dq_unit = hd->hp_unit;
318 sc->sc_dq.dq_slave = hd->hp_slave;
319 sc->sc_dq.dq_driver = &sddriver;
320
321 /*
322 * If we don't have a disk label, build a default partition
323 * table with 'standard' size root & swap and everything else
324 * in the G partition.
325 */
326 sc->sc_info = sddefaultpart;
327 /* C gets everything */
328 sc->sc_info.part[2].nblocks = sc->sc_blks;
329 sc->sc_info.part[2].endblk = sc->sc_blks;
330 /* G gets from end of B to end of disk */
331 sc->sc_info.part[6].nblocks = sc->sc_blks - sc->sc_info.part[1].endblk;
332 sc->sc_info.part[6].endblk = sc->sc_blks;
333 /*
334 * We also define the D, E and F paritions as an alternative to
335 * B and G. D is 48Mb, starts after A and is intended for swapping.
336 * E is 50Mb, starts after D and is intended for /usr. F starts
337 * after E and is what ever is left.
338 */
339 if (sc->sc_blks >= sc->sc_info.part[4].endblk) {
340 sc->sc_info.part[5].nblocks =
341 sc->sc_blks - sc->sc_info.part[4].endblk;
342 sc->sc_info.part[5].endblk = sc->sc_blks;
343 } else {
344 sc->sc_info.part[5].strtblk = 0;
345 sc->sc_info.part[3] = sc->sc_info.part[5];
346 sc->sc_info.part[4] = sc->sc_info.part[5];
347 }
348 /*
349 * H is a single partition alternative to E and F.
350 */
351 if (sc->sc_blks >= sc->sc_info.part[3].endblk) {
352 sc->sc_info.part[7].nblocks =
353 sc->sc_blks - sc->sc_info.part[3].endblk;
354 sc->sc_info.part[7].endblk = sc->sc_blks;
355 } else {
356 sc->sc_info.part[7].strtblk = 0;
357 }
358
9acfa6cd 359 sc->sc_flags |= SDF_ALIVE;
60f56dfc
KM
360 return(1);
361}
362
363void
364sdreset(sc, hd)
365 register struct sd_softc *sc;
366 register struct hp_device *hd;
367{
368 sdstats[hd->hp_unit].sdresets++;
369}
370
371int
88f29710 372sdopen(dev, flags, mode, p)
60f56dfc 373 dev_t dev;
88f29710
MK
374 int flags, mode;
375 struct proc *p;
60f56dfc
KM
376{
377 register int unit = sdunit(dev);
378 register struct sd_softc *sc = &sd_softc[unit];
379
380 if (unit >= NSD)
381 return(ENXIO);
88f29710 382 if ((sc->sc_flags & SDF_ALIVE) == 0 && suser(p->p_ucred, &p->p_acflag))
60f56dfc
KM
383 return(ENXIO);
384
385 if (sc->sc_hd->hp_dk >= 0)
386 dk_wpms[sc->sc_hd->hp_dk] = sc->sc_wpms;
387 return(0);
388}
389
9acfa6cd
MH
390int
391sdclose(dev, flag, mode, p)
392 dev_t dev;
393 int flag, mode;
394 struct proc *p;
395{
396 int unit = sdunit(dev);
397 register struct sd_softc *sc = &sd_softc[unit];
398 int s;
399
400 /*
401 * XXX we should really do this for all drives.
402 */
403 if (sc->sc_flags & SDF_RMEDIA) {
404 s = splbio();
405 while (sdtab[unit].b_active) {
406 sc->sc_flags |= SDF_WANTED;
407 sleep((caddr_t)&sdtab[unit], PRIBIO);
408 }
409 splx(s);
410 }
411 sc->sc_format_pid = 0;
412}
413
60f56dfc
KM
414/*
415 * This routine is called for partial block transfers and non-aligned
416 * transfers (the latter only being possible on devices with a block size
417 * larger than DEV_BSIZE). The operation is performed in three steps
418 * using a locally allocated buffer:
419 * 1. transfer any initial partial block
420 * 2. transfer full blocks
421 * 3. transfer any final partial block
422 */
423static void
424sdlblkstrat(bp, bsize)
425 register struct buf *bp;
426 register int bsize;
427{
428 register struct buf *cbp = (struct buf *)malloc(sizeof(struct buf),
429 M_DEVBUF, M_WAITOK);
430 caddr_t cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
431 register int bn, resid;
432 register caddr_t addr;
433
434 bzero((caddr_t)cbp, sizeof(*cbp));
88f29710 435 cbp->b_proc = curproc; /* XXX */
60f56dfc
KM
436 cbp->b_dev = bp->b_dev;
437 bn = bp->b_blkno;
438 resid = bp->b_bcount;
439 addr = bp->b_un.b_addr;
440#ifdef DEBUG
441 if (sddebug & SDB_PARTIAL)
442 printf("sdlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
443 bp, bp->b_flags, bn, resid, addr);
444#endif
445
446 while (resid > 0) {
447 register int boff = dbtob(bn) & (bsize - 1);
448 register int count;
449
450 if (boff || resid < bsize) {
451 sdstats[sdunit(bp->b_dev)].sdpartials++;
479c0df7 452 count = min(resid, bsize - boff);
60f56dfc
KM
453 cbp->b_flags = B_BUSY | B_PHYS | B_READ;
454 cbp->b_blkno = bn - btodb(boff);
455 cbp->b_un.b_addr = cbuf;
456 cbp->b_bcount = bsize;
457#ifdef DEBUG
458 if (sddebug & SDB_PARTIAL)
459 printf(" readahead: bn %x cnt %x off %x addr %x\n",
460 cbp->b_blkno, count, boff, addr);
461#endif
462 sdstrategy(cbp);
463 biowait(cbp);
464 if (cbp->b_flags & B_ERROR) {
465 bp->b_flags |= B_ERROR;
466 bp->b_error = cbp->b_error;
467 break;
468 }
469 if (bp->b_flags & B_READ) {
470 bcopy(&cbuf[boff], addr, count);
471 goto done;
472 }
473 bcopy(addr, &cbuf[boff], count);
474#ifdef DEBUG
475 if (sddebug & SDB_PARTIAL)
476 printf(" writeback: bn %x cnt %x off %x addr %x\n",
477 cbp->b_blkno, count, boff, addr);
478#endif
479 } else {
480 count = resid & ~(bsize - 1);
481 cbp->b_blkno = bn;
482 cbp->b_un.b_addr = addr;
483 cbp->b_bcount = count;
484#ifdef DEBUG
485 if (sddebug & SDB_PARTIAL)
486 printf(" fulltrans: bn %x cnt %x addr %x\n",
487 cbp->b_blkno, count, addr);
488#endif
489 }
490 cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
491 sdstrategy(cbp);
492 biowait(cbp);
493 if (cbp->b_flags & B_ERROR) {
494 bp->b_flags |= B_ERROR;
495 bp->b_error = cbp->b_error;
496 break;
497 }
498done:
499 bn += btodb(count);
500 resid -= count;
501 addr += count;
502#ifdef DEBUG
503 if (sddebug & SDB_PARTIAL)
504 printf(" done: bn %x resid %x addr %x\n",
505 bn, resid, addr);
506#endif
507 }
508 free(cbuf, M_DEVBUF);
509 free(cbp, M_DEVBUF);
510}
511
512void
513sdstrategy(bp)
514 register struct buf *bp;
515{
60f56dfc 516 register int unit = sdunit(bp->b_dev);
60f56dfc 517 register struct sd_softc *sc = &sd_softc[unit];
22d09b27 518 register struct size *pinfo = &sc->sc_info.part[sdpart(bp->b_dev)];
60f56dfc 519 register struct buf *dp = &sdtab[unit];
22d09b27
KM
520 register daddr_t bn;
521 register int sz, s;
60f56dfc
KM
522
523 if (sc->sc_format_pid) {
88f29710 524 if (sc->sc_format_pid != curproc->p_pid) { /* XXX */
60f56dfc 525 bp->b_error = EPERM;
22d09b27
KM
526 bp->b_flags |= B_ERROR;
527 goto done;
60f56dfc
KM
528 }
529 bp->b_cylin = 0;
530 } else {
531 bn = bp->b_blkno;
22d09b27
KM
532 sz = howmany(bp->b_bcount, DEV_BSIZE);
533 if (bn < 0 || bn + sz > pinfo->nblocks) {
534 sz = pinfo->nblocks - bn;
535 if (sz == 0) {
60f56dfc
KM
536 bp->b_resid = bp->b_bcount;
537 goto done;
538 }
22d09b27
KM
539 if (sz < 0) {
540 bp->b_error = EINVAL;
541 bp->b_flags |= B_ERROR;
542 goto done;
543 }
544 bp->b_bcount = dbtob(sz);
60f56dfc
KM
545 }
546 /*
547 * Non-aligned or partial-block transfers handled specially.
548 */
549 s = sc->sc_blksize - 1;
550 if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
551 sdlblkstrat(bp, sc->sc_blksize);
552 goto done;
553 }
22d09b27 554 bp->b_cylin = (bn + pinfo->strtblk) >> sc->sc_bshift;
60f56dfc
KM
555 }
556 s = splbio();
557 disksort(dp, bp);
558 if (dp->b_active == 0) {
559 dp->b_active = 1;
560 sdustart(unit);
561 }
562 splx(s);
563 return;
60f56dfc 564done:
22d09b27 565 biodone(bp);
60f56dfc
KM
566}
567
568void
569sdustart(unit)
570 register int unit;
571{
572 if (scsireq(&sd_softc[unit].sc_dq))
573 sdstart(unit);
574}
575
761052c0
MK
576/*
577 * Return:
578 * 0 if not really an error
579 * <0 if we should do a retry
580 * >0 if a fatal error
581 */
22d09b27 582static int
60f56dfc
KM
583sderror(unit, sc, hp, stat)
584 int unit, stat;
585 register struct sd_softc *sc;
586 register struct hp_device *hp;
587{
761052c0 588 int cond = 1;
22d09b27 589
60f56dfc
KM
590 sdsense[unit].status = stat;
591 if (stat & STS_CHECKCOND) {
592 struct scsi_xsense *sp;
593
594 scsi_request_sense(hp->hp_ctlr, hp->hp_slave,
595 sc->sc_punit, sdsense[unit].sense,
596 sizeof(sdsense[unit].sense));
597 sp = (struct scsi_xsense *)sdsense[unit].sense;
598 printf("sd%d: scsi sense class %d, code %d", unit,
599 sp->class, sp->code);
600 if (sp->class == 7) {
601 printf(", key %d", sp->key);
602 if (sp->valid)
603 printf(", blk %d", *(int *)&sp->info1);
761052c0
MK
604 switch (sp->key) {
605 /* no sense, try again */
606 case 0:
607 cond = -1;
608 break;
609 /* recovered error, not a problem */
610 case 1:
611 cond = 0;
612 break;
613 }
60f56dfc
KM
614 }
615 printf("\n");
616 }
761052c0 617 return(cond);
60f56dfc
KM
618}
619
620static void
621sdfinish(unit, sc, bp)
622 int unit;
623 register struct sd_softc *sc;
624 register struct buf *bp;
625{
9acfa6cd
MH
626 register struct buf *dp = &sdtab[unit];
627
628 dp->b_errcnt = 0;
629 dp->b_actf = bp->b_actf;
60f56dfc 630 bp->b_resid = 0;
22d09b27 631 biodone(bp);
60f56dfc 632 scsifree(&sc->sc_dq);
9acfa6cd 633 if (dp->b_actf)
60f56dfc 634 sdustart(unit);
9acfa6cd
MH
635 else {
636 dp->b_active = 0;
637 if (sc->sc_flags & SDF_WANTED) {
638 sc->sc_flags &= ~SDF_WANTED;
639 wakeup((caddr_t)dp);
640 }
641 }
60f56dfc
KM
642}
643
644void
645sdstart(unit)
646 register int unit;
647{
648 register struct sd_softc *sc = &sd_softc[unit];
649 register struct hp_device *hp = sc->sc_hd;
650
651 /*
652 * we have the SCSI bus -- in format mode, we may or may not need dma
653 * so check now.
654 */
655 if (sc->sc_format_pid && legal_cmds[sdcmd[unit].cdb[0]] > 0) {
656 register struct buf *bp = sdtab[unit].b_actf;
657 register int sts;
658
659 sts = scsi_immed_command(hp->hp_ctlr, hp->hp_slave,
660 sc->sc_punit, &sdcmd[unit],
661 bp->b_un.b_addr, bp->b_bcount,
662 bp->b_flags & B_READ);
663 sdsense[unit].status = sts;
664 if (sts & 0xfe) {
22d09b27 665 (void) sderror(unit, sc, hp, sts);
60f56dfc
KM
666 bp->b_flags |= B_ERROR;
667 bp->b_error = EIO;
668 }
669 sdfinish(unit, sc, bp);
670
671 } else if (scsiustart(hp->hp_ctlr))
672 sdgo(unit);
673}
674
675void
676sdgo(unit)
677 register int unit;
678{
679 register struct sd_softc *sc = &sd_softc[unit];
680 register struct hp_device *hp = sc->sc_hd;
681 register struct buf *bp = sdtab[unit].b_actf;
682 register int pad;
683 register struct scsi_fmt_cdb *cmd;
684
685 if (sc->sc_format_pid) {
686 cmd = &sdcmd[unit];
687 pad = 0;
688 } else {
689 cmd = bp->b_flags & B_READ? &sd_read_cmd : &sd_write_cmd;
690 *(int *)(&cmd->cdb[2]) = bp->b_cylin;
691 pad = howmany(bp->b_bcount, sc->sc_blksize);
692 *(u_short *)(&cmd->cdb[7]) = pad;
693 pad = (bp->b_bcount & (sc->sc_blksize - 1)) != 0;
694#ifdef DEBUG
695 if (pad)
696 printf("sd%d: partial block xfer -- %x bytes\n",
697 unit, bp->b_bcount);
698#endif
699 sdstats[unit].sdtransfers++;
700 }
701 if (scsigo(hp->hp_ctlr, hp->hp_slave, sc->sc_punit, bp, cmd, pad) == 0) {
702 if (hp->hp_dk >= 0) {
703 dk_busy |= 1 << hp->hp_dk;
704 ++dk_seek[hp->hp_dk];
705 ++dk_xfer[hp->hp_dk];
706 dk_wds[hp->hp_dk] += bp->b_bcount >> 6;
707 }
708 return;
709 }
710#ifdef DEBUG
711 if (sddebug & SDB_ERROR)
712 printf("sd%d: sdstart: %s adr %d blk %d len %d ecnt %d\n",
713 unit, bp->b_flags & B_READ? "read" : "write",
714 bp->b_un.b_addr, bp->b_cylin, bp->b_bcount,
715 sdtab[unit].b_errcnt);
716#endif
717 bp->b_flags |= B_ERROR;
718 bp->b_error = EIO;
719 sdfinish(unit, sc, bp);
720}
721
722void
723sdintr(unit, stat)
724 register int unit;
725 int stat;
726{
727 register struct sd_softc *sc = &sd_softc[unit];
728 register struct buf *bp = sdtab[unit].b_actf;
729 register struct hp_device *hp = sc->sc_hd;
761052c0 730 int cond;
60f56dfc
KM
731
732 if (bp == NULL) {
733 printf("sd%d: bp == NULL\n", unit);
734 return;
735 }
736 if (hp->hp_dk >= 0)
737 dk_busy &=~ (1 << hp->hp_dk);
738 if (stat) {
739#ifdef DEBUG
740 if (sddebug & SDB_ERROR)
741 printf("sd%d: sdintr: bad scsi status 0x%x\n",
742 unit, stat);
743#endif
761052c0
MK
744 cond = sderror(unit, sc, hp, stat);
745 if (cond) {
746 if (cond < 0 && sdtab[unit].b_errcnt++ < SDRETRY) {
ebe91f3c 747#ifdef DEBUG
761052c0
MK
748 if (sddebug & SDB_ERROR)
749 printf("sd%d: retry #%d\n",
750 unit, sdtab[unit].b_errcnt);
ebe91f3c 751#endif
761052c0
MK
752 sdstart(unit);
753 return;
754 }
755 bp->b_flags |= B_ERROR;
756 bp->b_error = EIO;
22d09b27 757 }
60f56dfc
KM
758 }
759 sdfinish(unit, sc, bp);
760}
761
762int
88f29710 763sdread(dev, uio, flags)
60f56dfc
KM
764 dev_t dev;
765 struct uio *uio;
88f29710 766 int flags;
60f56dfc
KM
767{
768 register int unit = sdunit(dev);
769 register int pid;
770
88f29710
MK
771 if ((pid = sd_softc[unit].sc_format_pid) &&
772 pid != uio->uio_procp->p_pid)
60f56dfc
KM
773 return (EPERM);
774
88f29710 775 return (physio(sdstrategy, NULL, dev, B_READ, minphys, uio));
60f56dfc
KM
776}
777
778int
88f29710 779sdwrite(dev, uio, flags)
60f56dfc
KM
780 dev_t dev;
781 struct uio *uio;
88f29710 782 int flags;
60f56dfc
KM
783{
784 register int unit = sdunit(dev);
785 register int pid;
786
88f29710
MK
787 if ((pid = sd_softc[unit].sc_format_pid) &&
788 pid != uio->uio_procp->p_pid)
60f56dfc
KM
789 return (EPERM);
790
88f29710 791 return (physio(sdstrategy, NULL, dev, B_WRITE, minphys, uio));
60f56dfc
KM
792}
793
794int
88f29710 795sdioctl(dev, cmd, data, flag, p)
60f56dfc
KM
796 dev_t dev;
797 int cmd;
798 caddr_t data;
799 int flag;
88f29710 800 struct proc *p;
60f56dfc
KM
801{
802 register int unit = sdunit(dev);
803 register struct sd_softc *sc = &sd_softc[unit];
804
805 switch (cmd) {
806 default:
807 return (EINVAL);
808
809 case SDIOCSFORMAT:
810 /* take this device into or out of "format" mode */
88f29710 811 if (suser(p->p_ucred, &p->p_acflag))
60f56dfc
KM
812 return(EPERM);
813
814 if (*(int *)data) {
815 if (sc->sc_format_pid)
816 return (EPERM);
88f29710 817 sc->sc_format_pid = p->p_pid;
60f56dfc
KM
818 } else
819 sc->sc_format_pid = 0;
820 return (0);
821
822 case SDIOCGFORMAT:
823 /* find out who has the device in format mode */
824 *(int *)data = sc->sc_format_pid;
825 return (0);
826
827 case SDIOCSCSICOMMAND:
828 /*
829 * Save what user gave us as SCSI cdb to use with next
830 * read or write to the char device.
831 */
88f29710 832 if (sc->sc_format_pid != p->p_pid)
60f56dfc
KM
833 return (EPERM);
834 if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
835 return (EINVAL);
836 bcopy(data, (caddr_t)&sdcmd[unit], sizeof(sdcmd[0]));
837 return (0);
838
839 case SDIOCSENSE:
840 /*
841 * return the SCSI sense data saved after the last
842 * operation that completed with "check condition" status.
843 */
844 bcopy((caddr_t)&sdsense[unit], data, sizeof(sdsense[0]));
845 return (0);
846
847 }
848 /*NOTREACHED*/
849}
850
851int
852sdsize(dev)
853 dev_t dev;
854{
855 register int unit = sdunit(dev);
856 register struct sd_softc *sc = &sd_softc[unit];
857
858 if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
859 return(-1);
860
861 return(sc->sc_info.part[sdpart(dev)].nblocks);
862}
863
60f56dfc
KM
864/*
865 * Non-interrupt driven, non-dma dump routine.
866 */
867int
868sddump(dev)
869 dev_t dev;
870{
871 int part = sdpart(dev);
872 int unit = sdunit(dev);
873 register struct sd_softc *sc = &sd_softc[unit];
874 register struct hp_device *hp = sc->sc_hd;
875 register daddr_t baddr;
876 register int maddr;
877 register int pages, i;
878 int stat;
879 extern int lowram;
880
881 /*
882 * Hmm... all vax drivers dump maxfree pages which is physmem minus
883 * the message buffer. Is there a reason for not dumping the
884 * message buffer? Savecore expects to read 'dumpsize' pages of
885 * dump, where dumpsys() sets dumpsize to physmem!
886 */
887 pages = physmem;
888
889 /* is drive ok? */
890 if (unit >= NSD || (sc->sc_flags & SDF_ALIVE) == 0)
891 return (ENXIO);
892 /* dump parameters in range? */
893 if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks)
894 return (EINVAL);
895 if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks)
896 pages = dtoc(sc->sc_info.part[part].nblocks - dumplo);
897 maddr = lowram;
898 baddr = dumplo + sc->sc_info.part[part].strtblk;
899 /* scsi bus idle? */
900 if (!scsireq(&sc->sc_dq)) {
901 scsireset(hp->hp_ctlr);
902 sdreset(sc, sc->sc_hd);
903 printf("[ drive %d reset ] ", unit);
904 }
905 for (i = 0; i < pages; i++) {
906#define NPGMB (1024*1024/NBPG)
907 /* print out how many Mbs we have dumped */
908 if (i && (i % NPGMB) == 0)
909 printf("%d ", i / NPGMB);
910#undef NPBMG
7cf88bec 911 pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr,
e0f6df7b 912 VM_PROT_READ, TRUE);
60f56dfc
KM
913 stat = scsi_tt_write(hp->hp_ctlr, hp->hp_slave, sc->sc_punit,
914 vmmap, NBPG, baddr, sc->sc_bshift);
915 if (stat) {
916 printf("sddump: scsi write error 0x%x\n", stat);
917 return (EIO);
918 }
919 maddr += NBPG;
920 baddr += ctod(1);
921 }
922 return (0);
923}
924#endif