1 /*
2 * Copyright (c) 1992, 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 and Ralph Campbell.
7 *
8 * %sccs.include.redist.c%
9 *
10 * @(#)rz.c 8.4 (Berkeley) 06/02/95
11 */
12
13 /*
14 * SCSI CCS (Command Command Set) disk driver.
15 * NOTE: The name was changed from "sd" to "rz" for DEC naming compatibility.
16 * I guess I can't avoid confusion someplace.
17 */
18 #include "rz.h"
19 #if NRZ > 0
20
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/buf.h>
24 #include <sys/errno.h>
25 #include <sys/fcntl.h>
26 #include <sys/ioctl.h>
27 #include <sys/dkstat.h>
28 #include <sys/disklabel.h>
29 #include <sys/malloc.h>
30 #include <sys/proc.h>
31 #include <sys/uio.h>
32 #include <sys/stat.h>
33 #include <sys/syslog.h>
34
35 #include <pmax/dev/device.h>
36 #include <pmax/dev/scsi.h>
37
38 extern int splbio();
39 extern void splx();
40 extern int physio();
41
42 int rzprobe();
43 void rzstrategy(), rzstart(), rzdone();
44
45 struct driver rzdriver = {
46 "rz", rzprobe, rzstart, rzdone,
47 };
48
49 struct size {
50 u_long strtblk;
51 u_long nblocks;
52 };
53
54 /*
55 * Since the SCSI standard tends to hide the disk structure, we define
56 * partitions in terms of DEV_BSIZE blocks. The default partition table
57 * (for an unlabeled disk) reserves 8K for a boot area, has an 8 meg
58 * root and 32 meg of swap. The rest of the space on the drive goes in
59 * the G partition. As usual, the C partition covers the entire disk
60 * (including the boot area).
61 */
62 static struct size rzdefaultpart[MAXPARTITIONS] = {
63 0, 16384, /* A */
64 16384, 65536, /* B */
65 0, 0, /* C */
66 17408, 0, /* D */
67 115712, 0, /* E */
68 218112, 0, /* F */
69 81920, 0, /* G */
70 115712, 0, /* H */
71 };
72
73 #define RAWPART 2 /* 'c' partition */ /* XXX */
74
75 struct rzstats {
76 long rzresets;
77 long rztransfers;
78 long rzpartials;
79 };
80
81 struct rz_softc {
82 struct scsi_device *sc_sd; /* physical unit info */
83 pid_t sc_format_pid; /* process using "format" mode */
84 u_long sc_openpart; /* partitions open */
85 u_long sc_bopenpart; /* block partitions open */
86 u_long sc_copenpart; /* character partitions open */
87 short sc_flags; /* see below */
88 short sc_type; /* drive type from INQUIRY cmd */
89 u_int sc_blks; /* number of blocks on device */
90 int sc_blksize; /* device block size in bytes */
91 int sc_bshift; /* convert device blocks to DEV_BSIZE */
92 u_int sc_wpms; /* average xfer rate in 16bit wds/sec */
93 struct disklabel sc_label; /* disk label for this disk */
94 struct rzstats sc_stats; /* statisic counts */
95 struct buf sc_tab; /* queue of pending operations */
96 struct buf sc_buf; /* buf for doing I/O */
97 struct buf sc_errbuf; /* buf for doing REQUEST_SENSE */
98 struct ScsiCmd sc_cmd; /* command for controller */
99 ScsiGroup1Cmd sc_rwcmd; /* SCSI cmd if not in "format" mode */
100 struct scsi_fmt_cdb sc_cdb; /* SCSI cmd if in "format" mode */
101 struct scsi_fmt_sense sc_sense; /* sense data from last cmd */
102 u_char sc_capbuf[8]; /* buffer for SCSI_READ_CAPACITY */
103 } rz_softc[NRZ];
104
105 /* sc_flags values */
106 #define RZF_ALIVE 0x0001 /* drive found and ready */
107 #define RZF_SENSEINPROGRESS 0x0002 /* REQUEST_SENSE command in progress */
108 #define RZF_ALTCMD 0x0004 /* alternate command in progress */
109 #define RZF_HAVELABEL 0x0008 /* valid label found on disk */
110 #define RZF_WLABEL 0x0010 /* label is writeable */
111 #define RZF_WAIT 0x0020 /* waiting for sc_tab to drain */
112 #define RZF_REMOVEABLE 0x0040 /* disk is removable */
113 #define RZF_TRYSYNC 0x0080 /* try synchronous operation */
114 #define RZF_NOERR 0x0100 /* don't print error messages */
115
116 #ifdef DEBUG
117 int rzdebug = 3;
118 #define RZB_ERROR 0x01
119 #define RZB_PARTIAL 0x02
120 #define RZB_PRLABEL 0x04
121 #endif
122
123 #define rzunit(x) (minor(x) >> 3)
124 #define rzpart(x) (minor(x) & 0x7)
125 #define b_cylin b_resid
126
127 /*
128 * Table of scsi commands users are allowed to access via "format" mode.
129 * 0 means not legal.
130 * 1 means legal.
131 */
132 static char legal_cmds[256] = {
133 /***** 0 1 2 3 4 5 6 7 8 9 A B C D E F */
134 /*00*/ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
135 /*10*/ 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
136 /*20*/ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137 /*30*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 /*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
139 /*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
140 /*60*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
141 /*70*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
142 /*80*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
143 /*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
144 /*a0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
145 /*b0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 /*c0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147 /*d0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
148 /*e0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
149 /*f0*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
150 };
151
152 /*
153 * Test to see if the unit is ready and if not, try to make it ready.
154 * Also, find the drive capacity.
155 */
156 static int
rzready(sc)157 rzready(sc)
158 register struct rz_softc *sc;
159 {
160 register int tries, i;
161 ScsiClass7Sense *sp;
162
163 /* don't print SCSI errors */
164 sc->sc_flags |= RZF_NOERR | RZF_ALTCMD;
165
166 /* see if the device is ready */
167 for (tries = 10; ; ) {
168 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
169 scsiGroup0Cmd(SCSI_TEST_UNIT_READY, sc->sc_rwcmd.unitNumber,
170 0, 0, (ScsiGroup0Cmd *)sc->sc_cdb.cdb);
171 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
172 sc->sc_buf.b_bcount = 0;
173 sc->sc_buf.b_un.b_addr = (caddr_t)0;
174 sc->sc_buf.b_actf = (struct buf *)0;
175 sc->sc_tab.b_actf = &sc->sc_buf;
176
177 sc->sc_cmd.cmd = sc->sc_cdb.cdb;
178 sc->sc_cmd.cmdlen = sc->sc_cdb.len;
179 sc->sc_cmd.buf = (caddr_t)0;
180 sc->sc_cmd.buflen = 0;
181 /* setup synchronous data transfers if the device supports it */
182 if (tries == 10 && (sc->sc_flags & RZF_TRYSYNC))
183 sc->sc_cmd.flags = SCSICMD_USE_SYNC;
184 else
185 sc->sc_cmd.flags = 0;
186
187 (*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd);
188 if (!biowait(&sc->sc_buf))
189 break;
190 if (--tries < 0)
191 return (0);
192 if (!(sc->sc_sense.status & SCSI_STATUS_CHECKCOND))
193 goto again;
194 sp = (ScsiClass7Sense *)sc->sc_sense.sense;
195 if (sp->error7 != 0x70)
196 goto again;
197 if (sp->key == SCSI_CLASS7_UNIT_ATTN && tries != 9) {
198 /* drive recalibrating, give it a while */
199 DELAY(1000000);
200 continue;
201 }
202 if (sp->key == SCSI_CLASS7_NOT_READY) {
203 ScsiStartStopCmd *cp;
204
205 /* try to spin-up disk with start/stop command */
206 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
207 cp = (ScsiStartStopCmd *)sc->sc_cdb.cdb;
208 cp->command = SCSI_START_STOP;
209 cp->unitNumber = sc->sc_rwcmd.unitNumber;
210 cp->immed = 0;
211 cp->loadEject = 0;
212 cp->start = 1;
213 cp->pad1 = 0;
214 cp->pad2 = 0;
215 cp->pad3 = 0;
216 cp->pad4 = 0;
217 cp->control = 0;
218 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
219 sc->sc_buf.b_bcount = 0;
220 sc->sc_buf.b_un.b_addr = (caddr_t)0;
221 sc->sc_buf.b_actf = (struct buf *)0;
222 sc->sc_tab.b_actf = &sc->sc_buf;
223 rzstart(sc->sc_cmd.unit);
224 if (biowait(&sc->sc_buf))
225 return (0);
226 continue;
227 }
228 again:
229 DELAY(1000);
230 }
231
232 /* print SCSI errors */
233 sc->sc_flags &= ~(RZF_NOERR | RZF_ALTCMD);
234
235 /* find out how big a disk this is */
236 sc->sc_cdb.len = sizeof(ScsiGroup1Cmd);
237 scsiGroup1Cmd(SCSI_READ_CAPACITY, sc->sc_rwcmd.unitNumber, 0, 0,
238 (ScsiGroup1Cmd *)sc->sc_cdb.cdb);
239 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
240 sc->sc_buf.b_bcount = sizeof(sc->sc_capbuf);
241 sc->sc_buf.b_un.b_addr = (caddr_t)sc->sc_capbuf;
242 sc->sc_buf.b_actf = (struct buf *)0;
243 sc->sc_tab.b_actf = &sc->sc_buf;
244 sc->sc_flags |= RZF_ALTCMD;
245 rzstart(sc->sc_cmd.unit);
246 sc->sc_flags &= ~RZF_ALTCMD;
247 if (biowait(&sc->sc_buf) || sc->sc_buf.b_resid != 0)
248 return (0);
249 sc->sc_blks = ((sc->sc_capbuf[0] << 24) | (sc->sc_capbuf[1] << 16) |
250 (sc->sc_capbuf[2] << 8) | sc->sc_capbuf[3]) + 1;
251 sc->sc_blksize = (sc->sc_capbuf[4] << 24) | (sc->sc_capbuf[5] << 16) |
252 (sc->sc_capbuf[6] << 8) | sc->sc_capbuf[7];
253
254 sc->sc_bshift = 0;
255 for (i = sc->sc_blksize; i > DEV_BSIZE; i >>= 1)
256 ++sc->sc_bshift;
257 sc->sc_blks <<= sc->sc_bshift;
258
259 return (1);
260 }
261
262 /*
263 * Test to see if device is present.
264 * Return true if found and initialized ok.
265 */
rzprobe(sd)266 rzprobe(sd)
267 register struct scsi_device *sd;
268 {
269 register struct rz_softc *sc = &rz_softc[sd->sd_unit];
270 register int i;
271 ScsiInquiryData inqbuf;
272 ScsiClass7Sense *sp;
273
274 /* init some parameters that don't change */
275 sc->sc_sd = sd;
276 sc->sc_cmd.sd = sd;
277 sc->sc_cmd.unit = sd->sd_unit;
278 sc->sc_rwcmd.unitNumber = sd->sd_slave;
279
280 /* try to find out what type of device this is */
281 sc->sc_format_pid = 1; /* force use of sc_cdb */
282 sc->sc_flags = RZF_NOERR; /* don't print SCSI errors */
283 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
284 scsiGroup0Cmd(SCSI_INQUIRY, sd->sd_slave, 0, sizeof(inqbuf),
285 (ScsiGroup0Cmd *)sc->sc_cdb.cdb);
286 sc->sc_buf.b_flags = B_BUSY | B_PHYS | B_READ;
287 sc->sc_buf.b_bcount = sizeof(inqbuf);
288 sc->sc_buf.b_un.b_addr = (caddr_t)&inqbuf;
289 sc->sc_buf.b_actf = (struct buf *)0;
290 sc->sc_tab.b_actf = &sc->sc_buf;
291 rzstart(sd->sd_unit);
292 if (biowait(&sc->sc_buf) ||
293 (i = sizeof(inqbuf) - sc->sc_buf.b_resid) < 5)
294 goto bad;
295 switch (inqbuf.type) {
296 case SCSI_DISK_TYPE: /* disk */
297 case SCSI_WORM_TYPE: /* WORM */
298 case SCSI_ROM_TYPE: /* CD-ROM */
299 case SCSI_OPTICAL_MEM_TYPE: /* Magneto-optical */
300 break;
301
302 default: /* not a disk */
303 goto bad;
304 }
305 sc->sc_type = inqbuf.type;
306 if (inqbuf.flags & SCSI_SYNC)
307 sc->sc_flags |= RZF_TRYSYNC;
308
309 if (!inqbuf.rmb) {
310 if (!rzready(sc))
311 goto bad;
312 }
313
314 printf("rz%d at %s%d drive %d slave %d", sd->sd_unit,
315 sd->sd_cdriver->d_name, sd->sd_ctlr, sd->sd_drive,
316 sd->sd_slave);
317 if (inqbuf.version > 2 || i < 36)
318 printf(" type 0x%x, qual 0x%x, ver %d",
319 inqbuf.type, inqbuf.qualifier, inqbuf.version);
320 else {
321 char vid[9], pid[17], revl[5];
322
323 bcopy((caddr_t)inqbuf.vendorID, (caddr_t)vid, 8);
324 bcopy((caddr_t)inqbuf.productID, (caddr_t)pid, 16);
325 bcopy((caddr_t)inqbuf.revLevel, (caddr_t)revl, 4);
326 for (i = 8; --i > 0; )
327 if (vid[i] != ' ')
328 break;
329 vid[i+1] = 0;
330 for (i = 16; --i > 0; )
331 if (pid[i] != ' ')
332 break;
333 pid[i+1] = 0;
334 for (i = 4; --i > 0; )
335 if (revl[i] != ' ')
336 break;
337 revl[i+1] = 0;
338 printf(" %s %s rev %s", vid, pid, revl);
339 }
340 printf(", %d %d byte blocks\n", sc->sc_blks, sc->sc_blksize);
341 if (!inqbuf.rmb && sc->sc_blksize != DEV_BSIZE) {
342 if (sc->sc_blksize < DEV_BSIZE) {
343 printf("rz%d: need %d byte blocks - drive ignored\n",
344 sd->sd_unit, DEV_BSIZE);
345 goto bad;
346 }
347 }
348 sc->sc_wpms = 32 * (60 * DEV_BSIZE / 2); /* XXX */
349 sc->sc_format_pid = 0;
350 sc->sc_flags |= RZF_ALIVE;
351 if (inqbuf.rmb)
352 sc->sc_flags |= RZF_REMOVEABLE;
353 sc->sc_buf.b_flags = 0;
354 return (1);
355
356 bad:
357 /* doesn't exist or not a CCS device */
358 sc->sc_format_pid = 0;
359 sc->sc_buf.b_flags = 0;
360 return (0);
361 }
362
363 /*
364 * This routine is called for partial block transfers and non-aligned
365 * transfers (the latter only being possible on devices with a block size
366 * larger than DEV_BSIZE). The operation is performed in three steps
367 * using a locally allocated buffer:
368 * 1. transfer any initial partial block
369 * 2. transfer full blocks
370 * 3. transfer any final partial block
371 */
372 static void
rzlblkstrat(bp,bsize)373 rzlblkstrat(bp, bsize)
374 register struct buf *bp;
375 register int bsize;
376 {
377 register struct buf *cbp;
378 caddr_t cbuf;
379 register int bn, resid;
380 register caddr_t addr;
381
382 cbp = (struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK);
383 cbuf = (caddr_t)malloc(bsize, M_DEVBUF, M_WAITOK);
384 bzero((caddr_t)cbp, sizeof(*cbp));
385 cbp->b_proc = curproc;
386 cbp->b_dev = bp->b_dev;
387 bn = bp->b_blkno;
388 resid = bp->b_bcount;
389 addr = bp->b_un.b_addr;
390 #ifdef DEBUG
391 if (rzdebug & RZB_PARTIAL)
392 printf("rzlblkstrat: bp %x flags %x bn %x resid %x addr %x\n",
393 bp, bp->b_flags, bn, resid, addr);
394 #endif
395
396 while (resid > 0) {
397 register int boff = dbtob(bn) & (bsize - 1);
398 register int count;
399
400 if (boff || resid < bsize) {
401 rz_softc[rzunit(bp->b_dev)].sc_stats.rzpartials++;
402 count = min(resid, bsize - boff);
403 cbp->b_flags = B_BUSY | B_PHYS | B_READ;
404 cbp->b_blkno = bn - btodb(boff);
405 cbp->b_un.b_addr = cbuf;
406 cbp->b_bcount = bsize;
407 #ifdef DEBUG
408 if (rzdebug & RZB_PARTIAL)
409 printf(" readahead: bn %x cnt %x off %x addr %x\n",
410 cbp->b_blkno, count, boff, addr);
411 #endif
412 rzstrategy(cbp);
413 biowait(cbp);
414 if (cbp->b_flags & B_ERROR) {
415 bp->b_flags |= B_ERROR;
416 bp->b_error = cbp->b_error;
417 break;
418 }
419 if (bp->b_flags & B_READ) {
420 bcopy(&cbuf[boff], addr, count);
421 goto done;
422 }
423 bcopy(addr, &cbuf[boff], count);
424 #ifdef DEBUG
425 if (rzdebug & RZB_PARTIAL)
426 printf(" writeback: bn %x cnt %x off %x addr %x\n",
427 cbp->b_blkno, count, boff, addr);
428 #endif
429 } else {
430 count = resid & ~(bsize - 1);
431 cbp->b_blkno = bn;
432 cbp->b_un.b_addr = addr;
433 cbp->b_bcount = count;
434 #ifdef DEBUG
435 if (rzdebug & RZB_PARTIAL)
436 printf(" fulltrans: bn %x cnt %x addr %x\n",
437 cbp->b_blkno, count, addr);
438 #endif
439 }
440 cbp->b_flags = B_BUSY | B_PHYS | (bp->b_flags & B_READ);
441 rzstrategy(cbp);
442 biowait(cbp);
443 if (cbp->b_flags & B_ERROR) {
444 bp->b_flags |= B_ERROR;
445 bp->b_error = cbp->b_error;
446 break;
447 }
448 done:
449 bn += btodb(count);
450 resid -= count;
451 addr += count;
452 #ifdef DEBUG
453 if (rzdebug & RZB_PARTIAL)
454 printf(" done: bn %x resid %x addr %x\n",
455 bn, resid, addr);
456 #endif
457 }
458 free(cbuf, M_DEVBUF);
459 free(cbp, M_DEVBUF);
460 }
461
462 void
rzstrategy(bp)463 rzstrategy(bp)
464 register struct buf *bp;
465 {
466 register int unit = rzunit(bp->b_dev);
467 register int part = rzpart(bp->b_dev);
468 register struct rz_softc *sc = &rz_softc[unit];
469 register struct partition *pp = &sc->sc_label.d_partitions[part];
470 register daddr_t bn;
471 register long sz, s;
472
473 if (sc->sc_format_pid) {
474 if (sc->sc_format_pid != curproc->p_pid) {
475 bp->b_error = EPERM;
476 goto bad;
477 }
478 bp->b_cylin = 0;
479 } else {
480 bn = bp->b_blkno;
481 sz = howmany(bp->b_bcount, DEV_BSIZE);
482 if ((unsigned)bn + sz > pp->p_size) {
483 sz = pp->p_size - bn;
484 /* if exactly at end of disk, return an EOF */
485 if (sz == 0) {
486 bp->b_resid = bp->b_bcount;
487 goto done;
488 }
489 /* if none of it fits, error */
490 if (sz < 0) {
491 bp->b_error = EINVAL;
492 goto bad;
493 }
494 /* otherwise, truncate */
495 bp->b_bcount = dbtob(sz);
496 }
497 /* check for write to write protected label */
498 if (bn + pp->p_offset <= LABELSECTOR &&
499 #if LABELSECTOR != 0
500 bn + pp->p_offset + sz > LABELSECTOR &&
501 #endif
502 !(bp->b_flags & B_READ) && !(sc->sc_flags & RZF_WLABEL)) {
503 bp->b_error = EROFS;
504 goto bad;
505 }
506 /*
507 * Non-aligned or partial-block transfers handled specially.
508 */
509 s = sc->sc_blksize - 1;
510 if ((dbtob(bn) & s) || (bp->b_bcount & s)) {
511 rzlblkstrat(bp, sc->sc_blksize);
512 goto done;
513 }
514 bp->b_cylin = (bn + pp->p_offset) >> sc->sc_bshift;
515 }
516 /* don't let disksort() see sc_errbuf */
517 while (sc->sc_flags & RZF_SENSEINPROGRESS)
518 printf("SENSE\n"); /* XXX */
519 s = splbio();
520 disksort(&sc->sc_tab, bp);
521 if (sc->sc_tab.b_active == 0) {
522 sc->sc_tab.b_active = 1;
523 rzstart(unit);
524 }
525 splx(s);
526 return;
527 bad:
528 bp->b_flags |= B_ERROR;
529 done:
530 biodone(bp);
531 }
532
533 void
rzstart(unit)534 rzstart(unit)
535 int unit;
536 {
537 register struct rz_softc *sc = &rz_softc[unit];
538 register struct buf *bp = sc->sc_tab.b_actf;
539 register int n;
540
541 sc->sc_cmd.buf = bp->b_un.b_addr;
542 sc->sc_cmd.buflen = bp->b_bcount;
543
544 if (sc->sc_format_pid ||
545 (sc->sc_flags & (RZF_SENSEINPROGRESS | RZF_ALTCMD))) {
546 sc->sc_cmd.flags = !(bp->b_flags & B_READ) ?
547 SCSICMD_DATA_TO_DEVICE : 0;
548 sc->sc_cmd.cmd = sc->sc_cdb.cdb;
549 sc->sc_cmd.cmdlen = sc->sc_cdb.len;
550 } else {
551 if (bp->b_flags & B_READ) {
552 sc->sc_cmd.flags = 0;
553 sc->sc_rwcmd.command = SCSI_READ_EXT;
554 } else {
555 sc->sc_cmd.flags = SCSICMD_DATA_TO_DEVICE;
556 sc->sc_rwcmd.command = SCSI_WRITE_EXT;
557 }
558 sc->sc_cmd.cmd = (u_char *)&sc->sc_rwcmd;
559 sc->sc_cmd.cmdlen = sizeof(sc->sc_rwcmd);
560 n = bp->b_cylin;
561 sc->sc_rwcmd.highAddr = n >> 24;
562 sc->sc_rwcmd.midHighAddr = n >> 16;
563 sc->sc_rwcmd.midLowAddr = n >> 8;
564 sc->sc_rwcmd.lowAddr = n;
565 n = howmany(bp->b_bcount, sc->sc_blksize);
566 sc->sc_rwcmd.highBlockCount = n >> 8;
567 sc->sc_rwcmd.lowBlockCount = n;
568 #ifdef DEBUG
569 if ((bp->b_bcount & (sc->sc_blksize - 1)) != 0)
570 printf("rz%d: partial block xfer -- %x bytes\n",
571 unit, bp->b_bcount);
572 #endif
573 sc->sc_stats.rztransfers++;
574 if ((n = sc->sc_sd->sd_dk) >= 0) {
575 dk_busy |= 1 << n;
576 ++dk_seek[n];
577 ++dk_xfer[n];
578 dk_wds[n] += bp->b_bcount >> 6;
579 }
580 }
581
582 /* tell controller to start this command */
583 (*sc->sc_sd->sd_cdriver->d_start)(&sc->sc_cmd);
584 }
585
586 /*
587 * This is called by the controller driver when the command is done.
588 */
589 void
rzdone(unit,error,resid,status)590 rzdone(unit, error, resid, status)
591 register int unit;
592 int error; /* error number from errno.h */
593 int resid; /* amount not transfered */
594 int status; /* SCSI status byte */
595 {
596 register struct rz_softc *sc = &rz_softc[unit];
597 register struct buf *bp = sc->sc_tab.b_actf;
598 register struct scsi_device *sd = sc->sc_sd;
599
600 if (bp == NULL) {
601 printf("rz%d: bp == NULL\n", unit);
602 return;
603 }
604 if (sd->sd_dk >= 0)
605 dk_busy &= ~(1 << sd->sd_dk);
606 if (sc->sc_flags & RZF_SENSEINPROGRESS) {
607 sc->sc_flags &= ~RZF_SENSEINPROGRESS;
608 sc->sc_tab.b_actf = bp = bp->b_actf; /* remove sc_errbuf */
609
610 if (error || (status & SCSI_STATUS_CHECKCOND)) {
611 #ifdef DEBUG
612 if (rzdebug & RZB_ERROR)
613 printf("rz%d: error reading sense data: error %d scsi status 0x%x\n",
614 unit, error, status);
615 #endif
616 /*
617 * We got an error during the REQUEST_SENSE,
618 * fill in no sense for data.
619 */
620 sc->sc_sense.sense[0] = 0x70;
621 sc->sc_sense.sense[2] = SCSI_CLASS7_NO_SENSE;
622 } else if (!(sc->sc_flags & RZF_NOERR)) {
623 printf("rz%d: ", unit);
624 scsiPrintSense((ScsiClass7Sense *)sc->sc_sense.sense,
625 sizeof(sc->sc_sense.sense) - resid);
626 }
627 } else if (error || (status & SCSI_STATUS_CHECKCOND)) {
628 #ifdef DEBUG
629 if (!(sc->sc_flags & RZF_NOERR) && (rzdebug & RZB_ERROR))
630 printf("rz%d: error %d scsi status 0x%x\n",
631 unit, error, status);
632 #endif
633 /* save error info */
634 sc->sc_sense.status = status;
635 bp->b_flags |= B_ERROR;
636 bp->b_error = error;
637 bp->b_resid = resid;
638
639 if (status & SCSI_STATUS_CHECKCOND) {
640 /*
641 * Start a REQUEST_SENSE command.
642 * Since we are called at interrupt time, we can't
643 * wait for the command to finish; that's why we use
644 * the sc_flags field.
645 */
646 sc->sc_flags |= RZF_SENSEINPROGRESS;
647 sc->sc_cdb.len = sizeof(ScsiGroup0Cmd);
648 scsiGroup0Cmd(SCSI_REQUEST_SENSE, sd->sd_slave, 0,
649 sizeof(sc->sc_sense.sense),
650 (ScsiGroup0Cmd *)sc->sc_cdb.cdb);
651 sc->sc_errbuf.b_flags = B_BUSY | B_PHYS | B_READ;
652 sc->sc_errbuf.b_bcount = sizeof(sc->sc_sense.sense);
653 sc->sc_errbuf.b_un.b_addr = (caddr_t)sc->sc_sense.sense;
654 sc->sc_errbuf.b_actf = bp;
655 sc->sc_tab.b_actf = &sc->sc_errbuf;
656 rzstart(unit);
657 return;
658 }
659 } else {
660 sc->sc_sense.status = status;
661 bp->b_resid = resid;
662 }
663
664 sc->sc_tab.b_actf = bp->b_actf;
665 biodone(bp);
666 if (sc->sc_tab.b_actf)
667 rzstart(unit);
668 else {
669 sc->sc_tab.b_active = 0;
670 /* finish close protocol */
671 if (sc->sc_openpart == 0)
672 wakeup((caddr_t)&sc->sc_tab);
673 }
674 }
675
676 /*
677 * Read or constuct a disklabel
678 */
679 void
rzgetinfo(dev)680 rzgetinfo(dev)
681 dev_t dev;
682 {
683 register int unit = rzunit(dev);
684 register struct rz_softc *sc = &rz_softc[unit];
685 register struct disklabel *lp = &sc->sc_label;
686 register int i;
687 char *msg;
688 int part;
689 extern char *readdisklabel();
690
691 part = rzpart(dev);
692 sc->sc_flags |= RZF_HAVELABEL;
693
694 if (sc->sc_type == SCSI_ROM_TYPE) {
695 lp->d_type = DTYPE_SCSI;
696 lp->d_secsize = sc->sc_blksize;
697 lp->d_nsectors = 100;
698 lp->d_ntracks = 1;
699 lp->d_ncylinders = (sc->sc_blks / 100) + 1;
700 lp->d_secpercyl = 100;
701 lp->d_secperunit = sc->sc_blks;
702 lp->d_rpm = 300;
703 lp->d_interleave = 1;
704 lp->d_flags = D_REMOVABLE;
705 lp->d_npartitions = 1;
706 lp->d_partitions[0].p_offset = 0;
707 lp->d_partitions[0].p_size = sc->sc_blks;
708 lp->d_partitions[0].p_fstype = FS_ISO9660;
709 lp->d_magic = DISKMAGIC;
710 lp->d_magic2 = DISKMAGIC;
711 lp->d_checksum = dkcksum(lp);
712 return;
713 }
714
715 lp->d_type = DTYPE_SCSI;
716 lp->d_secsize = DEV_BSIZE;
717 lp->d_secpercyl = 1 << sc->sc_bshift;
718 lp->d_npartitions = MAXPARTITIONS;
719 lp->d_partitions[part].p_offset = 0;
720 lp->d_partitions[part].p_size = sc->sc_blks;
721
722 /*
723 * Now try to read the disklabel
724 */
725 msg = readdisklabel(dev, rzstrategy, lp);
726 if (msg == NULL)
727 return;
728
729 printf("rz%d: WARNING: %s\n", unit, msg);
730 lp->d_magic = DISKMAGIC;
731 lp->d_magic2 = DISKMAGIC;
732 lp->d_type = DTYPE_SCSI;
733 lp->d_subtype = 0;
734 lp->d_typename[0] = '\0';
735 lp->d_secsize = DEV_BSIZE;
736 lp->d_secperunit = sc->sc_blks;
737 lp->d_npartitions = MAXPARTITIONS;
738 for (i = 0; i < MAXPARTITIONS; i++) {
739 lp->d_partitions[i].p_size = rzdefaultpart[i].nblocks;
740 lp->d_partitions[i].p_offset = rzdefaultpart[i].strtblk;
741 }
742 lp->d_partitions[RAWPART].p_size = sc->sc_blks;
743 }
744
745 int
rzopen(dev,flags,mode,p)746 rzopen(dev, flags, mode, p)
747 dev_t dev;
748 int flags, mode;
749 struct proc *p;
750 {
751 register int unit = rzunit(dev);
752 register struct rz_softc *sc = &rz_softc[unit];
753 register struct disklabel *lp;
754 register int i;
755 int part;
756 u_long mask;
757
758 if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE))
759 return (ENXIO);
760
761 /* make sure disk is ready */
762 if (sc->sc_flags & RZF_REMOVEABLE) {
763 if (!rzready(sc))
764 return (ENXIO);
765 }
766
767 /* try to read disk label and partition table information */
768 part = rzpart(dev);
769 if (!(sc->sc_flags & RZF_HAVELABEL))
770 rzgetinfo(dev);
771
772 lp = &sc->sc_label;
773 if (part >= lp->d_npartitions || lp->d_partitions[part].p_size == 0)
774 return (ENXIO);
775 /*
776 * Warn if a partition is opened that overlaps another
777 * already open, unless either is the `raw' partition
778 * (whole disk).
779 */
780 mask = 1 << part;
781 if ((sc->sc_openpart & mask) == 0 && part != RAWPART) {
782 register struct partition *pp;
783 u_long start, end;
784
785 pp = &lp->d_partitions[part];
786 start = pp->p_offset;
787 end = pp->p_offset + pp->p_size;
788 for (pp = lp->d_partitions, i = 0;
789 i < lp->d_npartitions; pp++, i++) {
790 if (pp->p_offset + pp->p_size <= start ||
791 pp->p_offset >= end || i == RAWPART)
792 continue;
793 if (sc->sc_openpart & (1 << i))
794 log(LOG_WARNING,
795 "rz%d%c: overlaps open partition (%c)\n",
796 unit, part + 'a', i + 'a');
797 }
798 }
799 switch (mode) {
800 case S_IFCHR:
801 sc->sc_copenpart |= mask;
802 break;
803 case S_IFBLK:
804 sc->sc_bopenpart |= mask;
805 break;
806 }
807 sc->sc_openpart |= mask;
808 if (sc->sc_sd->sd_dk >= 0)
809 dk_wpms[sc->sc_sd->sd_dk] = sc->sc_wpms;
810 return (0);
811 }
812
rzclose(dev,flags,mode)813 rzclose(dev, flags, mode)
814 dev_t dev;
815 int flags, mode;
816 {
817 register struct rz_softc *sc = &rz_softc[rzunit(dev)];
818 u_long mask = (1 << rzpart(dev));
819 int s;
820
821 switch (mode) {
822 case S_IFCHR:
823 sc->sc_copenpart &= ~mask;
824 break;
825 case S_IFBLK:
826 sc->sc_bopenpart &= ~mask;
827 break;
828 }
829 sc->sc_openpart = sc->sc_copenpart | sc->sc_bopenpart;
830
831 /*
832 * Should wait for I/O to complete on this partition even if
833 * others are open, but wait for work on blkflush().
834 */
835 if (sc->sc_openpart == 0) {
836 s = splbio();
837 while (sc->sc_tab.b_actf)
838 sleep((caddr_t)&sc->sc_tab, PZERO - 1);
839 splx(s);
840 sc->sc_flags &= ~RZF_WLABEL;
841 }
842 return (0);
843 }
844
845 int
rzread(dev,uio)846 rzread(dev, uio)
847 dev_t dev;
848 struct uio *uio;
849 {
850 register struct rz_softc *sc = &rz_softc[rzunit(dev)];
851
852 if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid)
853 return (EPERM);
854
855 return (physio(rzstrategy, (struct buf *)0, dev,
856 B_READ, minphys, uio));
857 }
858
859 int
rzwrite(dev,uio)860 rzwrite(dev, uio)
861 dev_t dev;
862 struct uio *uio;
863 {
864 register struct rz_softc *sc = &rz_softc[rzunit(dev)];
865
866 if (sc->sc_type == SCSI_ROM_TYPE)
867 return (EROFS);
868
869 if (sc->sc_format_pid && sc->sc_format_pid != curproc->p_pid)
870 return (EPERM);
871
872 return (physio(rzstrategy, (struct buf *)0, dev,
873 B_WRITE, minphys, uio));
874 }
875
876 int
rzioctl(dev,cmd,data,flag,p)877 rzioctl(dev, cmd, data, flag, p)
878 dev_t dev;
879 u_long cmd;
880 caddr_t data;
881 int flag;
882 struct proc *p;
883 {
884 register struct rz_softc *sc = &rz_softc[rzunit(dev)];
885 int error;
886 int flags;
887
888 switch (cmd) {
889 default:
890 return (EINVAL);
891
892 case SDIOCSFORMAT:
893 /* take this device into or out of "format" mode */
894 if (suser(p->p_ucred, &p->p_acflag))
895 return (EPERM);
896
897 if (*(int *)data) {
898 if (sc->sc_format_pid)
899 return (EPERM);
900 sc->sc_format_pid = p->p_pid;
901 } else
902 sc->sc_format_pid = 0;
903 return (0);
904
905 case SDIOCGFORMAT:
906 /* find out who has the device in format mode */
907 *(int *)data = sc->sc_format_pid;
908 return (0);
909
910 case SDIOCSCSICOMMAND:
911 /*
912 * Save what user gave us as SCSI cdb to use with next
913 * read or write to the char device.
914 */
915 if (sc->sc_format_pid != p->p_pid)
916 return (EPERM);
917 if (legal_cmds[((struct scsi_fmt_cdb *)data)->cdb[0]] == 0)
918 return (EINVAL);
919 bcopy(data, (caddr_t)&sc->sc_cdb, sizeof(sc->sc_cdb));
920 return (0);
921
922 case SDIOCSENSE:
923 /*
924 * return the SCSI sense data saved after the last
925 * operation that completed with "check condition" status.
926 */
927 bcopy((caddr_t)&sc->sc_sense, data, sizeof(sc->sc_sense));
928 return (0);
929
930 case DIOCGDINFO:
931 /* get the current disk label */
932 *(struct disklabel *)data = sc->sc_label;
933 return (0);
934
935 case DIOCSDINFO:
936 /* set the current disk label */
937 if (!(flag & FWRITE))
938 return (EBADF);
939 error = setdisklabel(&sc->sc_label,
940 (struct disklabel *)data,
941 (sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart);
942 return (error);
943
944 case DIOCGPART:
945 /* return the disk partition data */
946 ((struct partinfo *)data)->disklab = &sc->sc_label;
947 ((struct partinfo *)data)->part =
948 &sc->sc_label.d_partitions[rzpart(dev)];
949 return (0);
950
951 case DIOCWLABEL:
952 if (!(flag & FWRITE))
953 return (EBADF);
954 if (*(int *)data)
955 sc->sc_flags |= RZF_WLABEL;
956 else
957 sc->sc_flags &= ~RZF_WLABEL;
958 return (0);
959
960 case DIOCWDINFO:
961 /* write the disk label to disk */
962 if (!(flag & FWRITE))
963 return (EBADF);
964 error = setdisklabel(&sc->sc_label,
965 (struct disklabel *)data,
966 (sc->sc_flags & RZF_WLABEL) ? 0 : sc->sc_openpart);
967 if (error)
968 return (error);
969
970 /* simulate opening partition 0 so write succeeds */
971 flags = sc->sc_flags;
972 sc->sc_flags = RZF_ALIVE | RZF_WLABEL;
973 error = writedisklabel(dev, rzstrategy, &sc->sc_label);
974 sc->sc_flags = flags;
975 return (error);
976 }
977 /*NOTREACHED*/
978 }
979
980 int
rzsize(dev)981 rzsize(dev)
982 dev_t dev;
983 {
984 register int unit = rzunit(dev);
985 register int part = rzpart(dev);
986 register struct rz_softc *sc = &rz_softc[unit];
987
988 if (unit >= NRZ || !(sc->sc_flags & RZF_ALIVE))
989 return (-1);
990
991 /*
992 * We get called very early on (via swapconf)
993 * without the device being open so we need to
994 * read the disklabel here.
995 */
996 if (!(sc->sc_flags & RZF_HAVELABEL))
997 rzgetinfo(dev);
998
999 if (part >= sc->sc_label.d_npartitions)
1000 return (-1);
1001 return (sc->sc_label.d_partitions[part].p_size);
1002 }
1003
1004 /*
1005 * Non-interrupt driven, non-dma dump routine.
1006 */
1007 int
rzdump(dev)1008 rzdump(dev)
1009 dev_t dev;
1010 {
1011 #ifdef notdef
1012 int part = rzpart(dev);
1013 int unit = rzunit(dev);
1014 register struct rz_softc *sc = &rz_softc[unit];
1015 register struct scsi_device *sd = sc->sc_hd;
1016 register daddr_t baddr;
1017 register int maddr;
1018 register int pages, i;
1019 int stat;
1020 extern int lowram;
1021
1022 /*
1023 * Hmm... all vax drivers dump maxfree pages which is physmem minus
1024 * the message buffer. Is there a reason for not dumping the
1025 * message buffer? Savecore expects to read 'dumpsize' pages of
1026 * dump, where dumpsys() sets dumpsize to physmem!
1027 */
1028 pages = physmem;
1029
1030 /* is drive ok? */
1031 if (unit >= NRZ || (sc->sc_flags & RZF_ALIVE) == 0)
1032 return (ENXIO);
1033 /* dump parameters in range? */
1034 if (dumplo < 0 || dumplo >= sc->sc_info.part[part].nblocks)
1035 return (EINVAL);
1036 if (dumplo + ctod(pages) > sc->sc_info.part[part].nblocks)
1037 pages = dtoc(sc->sc_info.part[part].nblocks - dumplo);
1038 maddr = lowram;
1039 baddr = dumplo + sc->sc_info.part[part].strtblk;
1040 /* scsi bus idle? */
1041 if (!scsireq(&sc->sc_dq)) {
1042 scsireset(sd->sd_ctlr);
1043 sc->sc_stats.rzresets++;
1044 printf("[ drive %d reset ] ", unit);
1045 }
1046 for (i = 0; i < pages; i++) {
1047 #define NPGMB (1024*1024/NBPG)
1048 /* print out how many Mbs we have dumped */
1049 if (i && (i % NPGMB) == 0)
1050 printf("%d ", i / NPGMB);
1051 #undef NPBMG
1052 mapin(mmap, (u_int)vmmap, btop(maddr), PG_URKR|PG_CI|PG_V);
1053 stat = scsi_tt_write(sd->sd_ctlr, sd->sd_drive, sd->sd_slave,
1054 vmmap, NBPG, baddr, sc->sc_bshift);
1055 if (stat) {
1056 printf("rzdump: scsi write error 0x%x\n", stat);
1057 return (EIO);
1058 }
1059 maddr += NBPG;
1060 baddr += ctod(1);
1061 }
1062 return (0);
1063 #else /* notdef */
1064 return (ENXIO);
1065 #endif /* notdef */
1066 }
1067 #endif
1068