sd.c revision 1.3 1 /*
2 * Written by Julian Elischer (julian (at) tfs.com)
3 * Hacked by Theo de Raadt <deraadt (at) fsa.ca>
4 * for TRW Financial Systems for use under the MACH(2.5) operating system.
5 *
6 * TRW Financial Systems, in accordance with their agreement with Carnegie
7 * Mellon University, makes this software available to CMU to distribute
8 * or use in any manner that they see fit as long as this message is kept with
9 * the software. For this reason TFS also grants any other persons or
10 * organisations permission to use or modify this software.
11 *
12 * TFS supplies this software to be publicly redistributed
13 * on the understanding that TFS is not responsible for the correct
14 * functioning of this software in any circumstances.
15 */
16
17 #include "sd.h"
18
19 #include "sys/types.h"
20 #include "sys/param.h"
21 #include "sys/dkbad.h"
22 #include "sys/systm.h"
23 #include "sys/conf.h"
24 #include "sys/proc.h"
25 #include "sys/file.h"
26 #include "sys/stat.h"
27 #include "sys/ioctl.h"
28 #include "sys/buf.h"
29 #include "sys/uio.h"
30 #include "sys/malloc.h"
31 #include "sys/errno.h"
32 #include "sys/disklabel.h"
33 #include "scsi/scsi_all.h"
34 #include "scsi/scsi_disk.h"
35 #include "scsi/scsiconf.h"
36 #include "scsi/sddefs.h"
37
38 long int sdstrats, sdqueues;
39
40 #define SPLSD splbio
41 #define ESUCCESS 0
42
43 #define SECSIZE 512
44 #define PDLOCATION 29
45 #define BOOTRECORDSIGNATURE (0x55aa & 0x00ff)
46 #define SDOUTSTANDING 2
47 #define SDQSIZE 4
48 #define SD_RETRIES 4
49
50 #define MAKESDDEV(maj, unit, part) (makedev(maj, ((unit<<3)+part)))
51 #define UNITSHIFT 3
52 #define PARTITION(z) (minor(z) & 0x07)
53 #define RAW_PART 3
54 #define UNIT(z) ( (minor(z) >> UNITSHIFT) )
55
56 #define WHOLE_DISK(unit) ( (unit << UNITSHIFT) + RAW_PART )
57
58 struct sd_data *sd_data[NSD];
59 int sd_debug = 0;
60
61 /*
62 * The routine called by the low level scsi routine when it discovers
63 * A device suitable for this driver
64 */
65 int
66 sdattach(int masunit, struct scsi_switch *sw, int physid, int unit)
67 {
68 struct scsi_xfer *sd_scsi_xfer;
69 struct disk_parms *dp;
70 struct sd_data *sd;
71 unsigned char *tbl;
72 long int ad_info;
73 int targ, lun, i;
74
75 targ = physid >> 3;
76 lun = physid & 7;
77
78 /*printf("sdattach: sd%d at %s%d target %d lun %d\n",
79 unit, sw->name, masunit, targ, lun);*/
80
81 if(unit > NSD)
82 return -1;
83 if(sd_data[unit])
84 return -1;
85
86 sd = sd_data[unit] = (struct sd_data *)malloc(sizeof *sd,
87 M_TEMP, M_NOWAIT);
88 if(!sd)
89 return -1;
90
91 bzero(sd, sizeof *sd);
92
93 /* store information needed to contact our base driver */
94 sd->sc_sw = sw;
95 sd->ctlr = masunit;
96 sd->targ = targ;
97 sd->lu = lun;
98
99 dp = &(sd->params);
100 if(scsi_debug & PRINTROUTINES)
101 printf("sdattach: ");
102
103 if(sd->sc_sw->adapter_info) {
104 sd->ad_info = ( (*(sd->sc_sw->adapter_info))(masunit));
105 sd->cmdscount = sd->ad_info & AD_INF_MAX_CMDS;
106 if(sd->cmdscount > SDOUTSTANDING)
107 sd->cmdscount = SDOUTSTANDING;
108 } else {
109 sd->ad_info = 1;
110 sd->cmdscount = 1;
111 }
112
113 i = sd->cmdscount;
114 sd_scsi_xfer = (struct scsi_xfer *)malloc(sizeof(struct scsi_xfer) * i,
115 M_TEMP, M_NOWAIT);
116 while(i--) {
117 sd_scsi_xfer->next = sd->freexfer;
118 sd->freexfer = sd_scsi_xfer;
119 sd_scsi_xfer++;
120 }
121
122 /*
123 * Use the subdriver to request information regarding
124 * the drive. We cannot use interrupts yet, so the
125 * request must specify this.
126 */
127 sd_get_parms(unit, SCSI_NOSLEEP | SCSI_NOMASK);
128 printf("sd%d at %s%d targ %d lun %d: %dMB cyl %d head %d sec %d byte/sec %d\n",
129 unit, sw->name, masunit, targ, lun,
130 (dp->cyls*dp->heads*dp->sectors*dp->secsiz)/ (1024*1024),
131 dp->cyls, dp->heads, dp->sectors, dp->secsiz);
132
133 sd->flags |= SDINIT;
134 return 0;
135 }
136
137
138 /*
139 * open the device. Make sure the partition info
140 * is a up-to-date as can be.
141 */
142 int
143 sdopen(int dev)
144 {
145 struct disk_parms disk_parms;
146 struct sd_data *sd;
147 int errcode = 0;
148 int unit, part;
149
150 unit = UNIT(dev);
151 part = PARTITION(dev);
152 if(scsi_debug & (PRINTROUTINES | TRACEOPENS))
153 printf("sdopen: dev=0x%x (unit %d (of %d),partition %d)\n",
154 dev, unit, NSD, part);
155
156 if(unit > NSD)
157 return ENXIO;
158 if( !sd_data[unit]) {
159 if(scsi_debug & PRINTROUTINES)
160 printf("nonexistant!\n");
161 return ENXIO;
162 }
163
164 sd = sd_data[unit];
165 if( !(sd->flags & SDVALID) )
166 return ENXIO;
167
168 /*
169 * Make sure the disk has been initialised.
170 * XXX get the scsi driver to look for a new device if
171 * we are not initted, like SunOS
172 */
173 if( !(sd->flags & SDINIT))
174 return ENXIO;
175
176 /*
177 * If it's been invalidated, and not everybody has
178 * closed it then forbid re-entry.
179 */
180 if( !(sd->flags & SDVALID) && sd->openparts)
181 return ENXIO;
182
183 /*
184 * Check that it is still responding and ok.
185 * "unit attention errors should occur here if the drive
186 * has been restarted or the pack changed
187 */
188 if(scsi_debug & TRACEOPENS)
189 printf("device is ");
190
191 if (sd_test_unit_ready(unit, 0)) {
192 if(scsi_debug & TRACEOPENS)
193 printf("not reponding\n");
194 return ENXIO;
195 }
196 if(scsi_debug & TRACEOPENS)
197 printf("ok\n");
198
199 /*
200 * In case it is a funny one, tell it to start
201 * not needed for most hard drives (ignore failure)
202 */
203 sd_start_unit(unit, SCSI_ERR_OK|SCSI_SILENT);
204 if(scsi_debug & TRACEOPENS)
205 printf("started ");
206
207 /*
208 * Load the physical device parameters
209 */
210 sd_get_parms(unit, 0); /* sets SDVALID */
211 if( sd->params.secsiz != SECSIZE) {
212 printf("sd%d: Can't deal with %d bytes logical blocks\n",
213 unit, sd->params.secsiz);
214 return ENXIO;
215 }
216 if(scsi_debug & TRACEOPENS)
217 printf("Params loaded ");
218
219 /*
220 * Load the partition info if not already loaded
221 */
222 sd_prevent(unit, PR_PREVENT, SCSI_ERR_OK|SCSI_SILENT);
223 if( (errcode=sdgetdisklabel(unit)) && (part != RAW_PART)) {
224 sd_prevent(unit, PR_ALLOW, SCSI_ERR_OK|SCSI_SILENT);
225 return errcode;
226 }
227 if(scsi_debug & TRACEOPENS)
228 printf("Disklabel loaded ");
229
230 /*
231 * Check the partition is legal
232 */
233 if ( part >= MAXPARTITIONS ) {
234 sd_prevent(unit, PR_ALLOW, SCSI_ERR_OK|SCSI_SILENT);
235 return ENXIO;
236 }
237 if(scsi_debug & TRACEOPENS)
238 printf("ok");
239
240 /*
241 * Check that the partition exists
242 */
243 if( sd->disklabel.d_partitions[part].p_size==0 && part!=RAW_PART) {
244 sd_prevent(unit, PR_ALLOW, SCSI_ERR_OK|SCSI_SILENT);
245 return ENXIO;
246 }
247
248 sd->partflags[part] |= SDOPEN;
249 sd->openparts |= (1 << part);
250 if(scsi_debug & TRACEOPENS)
251 printf("open %d %d\n", sdstrats, sdqueues);
252 return 0;
253 }
254
255 /*
256 * Get ownership of a scsi_xfer
257 * If need be, sleep on it, until it comes free
258 */
259 struct scsi_xfer *
260 sd_get_xs(int unit, int flags)
261 {
262 struct sd_data *sd = sd_data[unit];
263 struct scsi_xfer *xs;
264 int s;
265
266 if(flags & (SCSI_NOSLEEP | SCSI_NOMASK)) {
267 if (xs = sd->freexfer) {
268 sd->freexfer = xs->next;
269 xs->flags = 0;
270 }
271 } else {
272 s = SPLSD();
273 while (!(xs = sd->freexfer)) {
274 sd->blockwait++; /* someone waiting! */
275 sleep((caddr_t)&sd->freexfer, PRIBIO+1);
276 sd->blockwait--;
277 }
278 sd->freexfer = xs->next;
279 splx(s);
280 xs->flags = 0;
281 }
282 return xs;
283 }
284
285 /*
286 * Free a scsi_xfer, wake processes waiting for it
287 */
288 void
289 sd_free_xs(int unit, struct scsi_xfer *xs, int flags)
290 {
291 struct sd_data *sd = sd_data[unit];
292 int s;
293
294 if(flags & SCSI_NOMASK) {
295 if (sd->blockwait) {
296 printf("doing a wakeup from NOMASK mode\n");
297 wakeup((caddr_t)&sd->freexfer);
298 }
299 xs->next = sd->freexfer;
300 sd->freexfer = xs;
301 } else {
302 s = SPLSD();
303 if (sd->blockwait)
304 wakeup((caddr_t)&sd->freexfer);
305 xs->next = sd->freexfer;
306 sd->freexfer = xs;
307 splx(s);
308 }
309 }
310
311 /*
312 * trim the size of the transfer if needed, called by physio
313 * basically the smaller of our max and the scsi driver's
314 * minphys (note we have no max)
315 */
316 void
317 sdminphys(struct buf *bp)
318 {
319 (*(sd_data[UNIT(bp->b_dev)]->sc_sw->scsi_minphys))(bp);
320 }
321
322 /*
323 * Actually translate the requested transfer into
324 * one the physical driver can understand
325 * The transfer is described by a buf and will include
326 * only one physical transfer.
327 */
328 int
329 sdstrategy(struct buf *bp)
330 {
331 struct sd_data *sd;
332 unsigned int opri;
333 struct buf *dp;
334 int unit;
335
336 sdstrats++;
337 unit = UNIT((bp->b_dev));
338
339 if(unit > NSD) {
340 printf("sdstrategy bailout: %d %d\n", unit, NSD);
341 bp->b_error = EIO;
342 goto bad;
343 }
344 if( !sd_data[unit]) {
345 printf("sdstrategy bailout\n");
346 bp->b_error = EIO;
347 goto bad;
348 }
349
350 sd = sd_data[unit];
351 if(scsi_debug & PRINTROUTINES)
352 printf("\nsdstrategy ");
353 if(scsi_debug & SHOWREQUESTS)
354 printf("sd%d: %d bytes @ blk%d\n",
355 unit, bp->b_bcount, bp->b_blkno);
356
357 sdminphys(bp);
358
359 /* If the device has been made invalid, error out */
360 if(!(sd->flags & SDVALID)) {
361 bp->b_error = EIO;
362 goto bad;
363 }
364
365 /* "soft" write protect check */
366 if ((sd->flags & SDWRITEPROT) && (bp->b_flags & B_READ) == 0) {
367 bp->b_error = EROFS;
368 goto bad;
369 }
370
371 /* If it's a null transfer, return immediately */
372 if (bp->b_bcount == 0)
373 goto done;
374
375 /*
376 * Decide which unit and partition we are talking about
377 * only raw is ok if no label
378 */
379 if(PARTITION(bp->b_dev) != RAW_PART) {
380 if (!(sd->flags & SDHAVELABEL)) {
381 bp->b_error = EIO;
382 goto bad;
383 }
384
385 /*
386 * do bounds checking, adjust transfer. if error, process.
387 * if end of partition, just return
388 */
389 if (bounds_check_with_label(bp, &sd->disklabel, sd->wlabel) <= 0)
390 goto done;
391 /* otherwise, process transfer request */
392 }
393
394 opri = SPLSD();
395 dp = &(sd_data[unit]->sdbuf);
396
397 /* Place it in the queue of disk activities for this disk */
398 disksort(dp, bp);
399
400 /*
401 * Tell the device to get going on the transfer if it's
402 * not doing anything, otherwise just wait for completion
403 */
404 sdstart(unit);
405
406 splx(opri);
407 return;
408 bad:
409 bp->b_flags |= B_ERROR;
410 done:
411 /* Correctly set the buf to indicate a completed xfer */
412 bp->b_resid = bp->b_bcount;
413 biodone(bp);
414 return;
415 }
416
417 /*
418 * sdstart looks to see if there is a buf waiting for the device
419 * and that the device is not already busy. If both are true,
420 * It deques the buf and creates a scsi command to perform the
421 * transfer in the buf. The transfer request will call sd_done
422 * on completion, which will in turn call this routine again
423 * so that the next queued transfer is performed.
424 * The bufs are queued by the strategy routine (sdstrategy)
425 * This routine is also called after other non-queued requests
426 * have been made of the scsi driver, to ensure that the queue
427 * continues to be drained.
428 * must be called at the correct (highish) spl level
429 * sdstart() is called at SPLSD from sdstrategy and sd_done
430 */
431 void
432 sdstart(int unit)
433 {
434 register struct buf *bp = 0, *dp;
435 struct sd_data *sd = sd_data[unit];
436 struct scsi_rw_big cmd;
437 struct scsi_xfer *xs;
438 struct partition *p;
439 int drivecount, blkno, nblk;
440
441 if(scsi_debug & PRINTROUTINES)
442 printf("sdstart%d ", unit);
443
444 sd = sd_data[unit];
445
446 /*
447 * See if there is a buf to do and we are not already
448 * doing one
449 */
450 if(!sd->freexfer)
451 return; /* none for us, unit already underway */
452
453 if(sd->blockwait) /* there is one, but a special waits */
454 return; /* give the special that's waiting a chance to run */
455
456
457 dp = &(sd_data[unit]->sdbuf);
458 if ((bp = dp->b_actf) != NULL) /* yes, an assign */
459 dp->b_actf = bp->av_forw;
460 else
461 return;
462
463 xs=sd_get_xs(unit, 0); /* ok we can grab it */
464 xs->flags = INUSE; /* Now ours */
465
466 /*
467 * If the device has become invalid, abort all the reads
468 * and writes until all files have been closed and re-openned
469 */
470 if( !(sd->flags & SDVALID) ) {
471 xs->error = XS_DRIVER_STUFFUP;
472 sd_done(unit,xs); /* clean up (calls sdstart) */
473 return ;
474 }
475
476 /*
477 * We have a buf, now we should move the data into
478 * a scsi_xfer definition and try start it
479 * First, translate the block to absolute
480 */
481 p = sd->disklabel.d_partitions + PARTITION(bp->b_dev);
482 blkno = bp->b_blkno + p->p_offset;
483 nblk = (bp->b_bcount + 511) >> 9;
484
485 /* Fill out the scsi command */
486 bzero(&cmd, sizeof(cmd));
487 cmd.op_code = (bp->b_flags & B_READ) ? READ_BIG : WRITE_BIG;
488 cmd.addr_3 = (blkno & 0xff000000) >> 24;
489 cmd.addr_2 = (blkno & 0xff0000) >> 16;
490 cmd.addr_1 = (blkno & 0xff00) >> 8;
491 cmd.addr_0 = blkno & 0xff;
492 cmd.length2 = (nblk & 0xff00) >> 8;
493 cmd.length1 = (nblk & 0xff);
494
495 /*
496 * Fill out the scsi_xfer structure
497 * Note: we cannot sleep as we may be an interrupt
498 */
499 xs->flags |= SCSI_NOSLEEP;
500 xs->adapter = sd->ctlr;
501 xs->targ = sd->targ;
502 xs->lu = sd->lu;
503 xs->retries = SD_RETRIES;
504 xs->timeout = 10000; /* 10000 millisecs for a disk !*/
505 xs->cmd = (struct scsi_generic *)&cmd;
506 xs->cmdlen = sizeof(cmd);
507 xs->resid = bp->b_bcount;
508 xs->when_done = sd_done;
509 xs->done_arg = unit;
510 xs->done_arg2 = (int)xs;
511 xs->error = XS_NOERROR;
512 xs->bp = bp;
513 xs->data = (u_char *)bp->b_un.b_addr;
514 xs->datalen = bp->b_bcount;
515
516 /* Pass all this info to the scsi driver */
517 if ( (*(sd->sc_sw->scsi_cmd))(xs) != SUCCESSFULLY_QUEUED) {
518 printf("sd%d: oops not queued",unit);
519 xs->error = XS_DRIVER_STUFFUP;
520 sd_done(unit, xs); /* clean up (calls sdstart) */
521 }
522 sdqueues++;
523 }
524
525 /*
526 * This routine is called by the scsi interrupt when
527 * the transfer is complete.
528 */
529 int
530 sd_done(int unit, struct scsi_xfer *xs)
531 {
532 struct buf *bp;
533 int retval, retries = 0;
534
535 if(scsi_debug & PRINTROUTINES)
536 printf("sd_done%d ",unit);
537 if( !(xs->flags & INUSE))
538 panic("scsi_xfer not in use!");
539 if(bp = xs->bp) {
540 switch(xs->error) {
541 case XS_NOERROR:
542 bp->b_error = 0;
543 bp->b_resid = 0;
544 break;
545 case XS_SENSE:
546 retval = (sd_interpret_sense(unit,xs));
547 if(retval) {
548 bp->b_flags |= B_ERROR;
549 bp->b_error = retval;
550 }
551 break;
552 case XS_TIMEOUT:
553 printf("sd%d timeout\n",unit);
554 case XS_BUSY: /* should retry -- how? */
555 /*
556 * SHOULD put buf back at head of queue
557 * and decrement retry count in (*xs)
558 * HOWEVER, this should work as a kludge
559 */
560 if(xs->retries--) {
561 xs->error = XS_NOERROR;
562 xs->flags &= ~ITSDONE;
563 if( (*(sd_data[unit]->sc_sw->scsi_cmd))(xs)
564 == SUCCESSFULLY_QUEUED) {
565 /* don't wake the job, ok? */
566 return;
567 }
568 xs->flags |= ITSDONE;
569 } /* fall through */
570
571 case XS_DRIVER_STUFFUP:
572 bp->b_flags |= B_ERROR;
573 bp->b_error = EIO;
574 break;
575 default:
576 printf("sd%d: unknown error category from scsi driver\n", unit);
577 }
578 biodone(bp);
579 sd_free_xs(unit, xs, 0);
580 sdstart(unit); /* If there's anything waiting.. do it */
581 } else
582 wakeup(xs);
583 }
584
585 /*
586 * Perform special action on behalf of the user
587 * Knows about the internals of this device
588 */
589 int
590 sdioctl(dev_t dev, int cmd, caddr_t addr, int flag)
591 {
592 /* struct sd_cmd_buf *args;*/
593 struct scsi_format_parms *fparms;
594 extern struct proc *curproc;
595 register struct sd_data *sd;
596 unsigned char unit, part;
597 unsigned int opri;
598 int error = 0, x;
599
600 /* Find the device that the user is talking about */
601 unit = UNIT(dev);
602 part = PARTITION(dev);
603 if(scsi_debug & PRINTROUTINES)
604 printf("sdioctl%d ",unit);
605
606 /* If the device is not valid.. abandon ship */
607 if(unit > NSD)
608 return EIO;
609 if(sd==NULL)
610 return EIO;
611
612 sd = sd_data[unit];
613 if(!(sd->flags & SDVALID))
614 return EIO;
615
616 switch(cmd) {
617 case DIOCWFORMAT:
618 if( suser(curproc->p_ucred, &curproc->p_acflag))
619 return EPERM;
620
621 x = splbio();
622 if(sd->formatting)
623 return EBUSY;
624 sd->formatting = 1;
625 (void)splx(x);
626
627 fparms = (struct scsi_format_parms *)malloc(sizeof *fparms,
628 M_TEMP, M_NOWAIT);
629 if(!fparms) {
630 error = EAGAIN;
631 goto unlock;
632 }
633
634 if(copyin(&addr, fparms, sizeof fparms)!=0) {
635 free(fparms, M_TEMP);
636 error = EFAULT;
637 goto unlock;
638 }
639 error = sd_format(unit, fparms, 0, 0);
640 if(!error && copyout(&addr, fparms, sizeof fparms) )
641 error = EFAULT;
642 free(fparms, M_TEMP);
643 unlock:
644 x = splbio();
645 sd->formatting = 0;
646 (void)splx(x);
647
648 break;
649 case DIOCRFORMAT:
650 error = EINVAL;
651 break;
652 case DIOCSBAD:
653 error = EINVAL;
654 break;
655 case DIOCGDINFO:
656 *(struct disklabel *)addr = sd->disklabel;
657 break;
658 case DIOCGPART:
659 ((struct partinfo *)addr)->disklab = &sd->disklabel;
660 ((struct partinfo *)addr)->part =
661 &sd->disklabel.d_partitions[PARTITION(dev)];
662 break;
663 case DIOCSDINFO:
664 if ((flag & FWRITE) == 0)
665 error = EBADF;
666 else {
667 error = setdisklabel(&sd->disklabel, (struct disklabel *)addr,
668 /*(sd->flags & DKFL_BSDLABEL) ? sd->openparts : */0,
669 sd->dosparts);
670 }
671 if (error == 0)
672 sd->flags |= SDHAVELABEL;
673 break;
674 case DIOCWLABEL:
675 sd->flags &= ~SDWRITEPROT;
676 if ((flag & FWRITE) == 0)
677 error = EBADF;
678 else
679 sd->wlabel = *(int *)addr;
680 break;
681 case DIOCWDINFO:
682 sd->flags &= ~SDWRITEPROT;
683 if ((flag & FWRITE) == 0)
684 error = EBADF;
685 else {
686 if ((error = setdisklabel(&sd->disklabel,
687 (struct disklabel *)addr,
688 /*(sd->flags & SDHAVELABEL) ? sd->openparts :*/0,
689 sd->dosparts)) == 0) {
690 int wlab;
691
692 sd->flags |= SDHAVELABEL; /* ok write will succeed */
693
694 /* simulate opening partition 0 so write succeeds */
695 sd->openparts |= (1 << 0); /* XXX */
696 wlab = sd->wlabel;
697 sd->wlabel = 1;
698 error = writedisklabel(dev, sdstrategy,
699 &sd->disklabel, sd->dosparts);
700 sd->wlabel = wlab;
701 }
702 }
703 break;
704 default:
705 error = ENOTTY;
706 break;
707 }
708 return error;
709 }
710
711
712 /*
713 * Load the label information on the named device
714 */
715 int
716 sdgetdisklabel(u_char unit)
717 {
718 struct dos_partition *dos_partition_p;
719 struct sd_data *sd = sd_data[unit];
720 /*unsigned int n, m;*/
721 char *errstring;
722
723 /* If the inflo is already loaded, use it */
724 if(sd->flags & SDHAVELABEL)
725 return ESUCCESS;
726
727 bzero(&sd->disklabel, sizeof(struct disklabel));
728 /*
729 * make partition 3 the whole disk in case of failure
730 * then get pdinfo
731 */
732 sd->disklabel.d_partitions[0].p_offset = 0;
733 sd->disklabel.d_partitions[0].p_size = sd->params.disksize;
734 sd->disklabel.d_partitions[RAW_PART].p_offset = 0;
735 sd->disklabel.d_partitions[RAW_PART].p_size = sd->params.disksize;
736 sd->disklabel.d_npartitions = MAXPARTITIONS;
737 sd->disklabel.d_secsize = 512; /* as long as it's not 0 */
738 sd->disklabel.d_ntracks = sd->params.heads;
739 sd->disklabel.d_nsectors = sd->params.sectors;
740 sd->disklabel.d_ncylinders = sd->params.cyls;
741 sd->disklabel.d_secpercyl = sd->params.heads * sd->params.sectors;
742 if (sd->disklabel.d_secpercyl == 0) {
743 /* as long as it's not 0 because readdisklabel() divides by it */
744 sd->disklabel.d_secpercyl = 100;
745 }
746
747 /* all the generic disklabel extraction routine */
748 if(errstring = readdisklabel(makedev(0 ,(unit<<UNITSHIFT )+3),
749 sdstrategy, &sd->disklabel, sd->dosparts, 0, 0)) {
750 printf("sd%d: %s\n",unit, errstring);
751 return ENXIO;
752 }
753
754 /* leave partition 2 "open" for raw I/O */
755
756 sd->flags |= SDHAVELABEL; /* WE HAVE IT ALL NOW */
757 return ESUCCESS;
758 }
759
760 /*
761 * Find out from the device what it's capacity is
762 */
763 int
764 sd_size(int unit, int flags)
765 {
766 struct scsi_read_cap_data rdcap;
767 struct scsi_read_capacity scsi_cmd;
768 int size;
769
770 /*
771 * make up a scsi command and ask the scsi driver to do
772 * it for you.
773 */
774 bzero(&scsi_cmd, sizeof(scsi_cmd));
775 scsi_cmd.op_code = READ_CAPACITY;
776
777 /*
778 * If the command works, interpret the result as a 4 byte
779 * number of blocks
780 */
781 if (sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
782 sizeof(scsi_cmd), (u_char *)&rdcap, sizeof(rdcap), 2000, flags) != 0) {
783 printf("could not get size of unit %d\n", unit);
784 return 0;
785 } else {
786 size = rdcap.addr_0 + 1 ;
787 size += rdcap.addr_1 << 8;
788 size += rdcap.addr_2 << 16;
789 size += rdcap.addr_3 << 24;
790 }
791 return size;
792 }
793
794 /*
795 * Get scsi driver to send a "are you ready?" command
796 */
797 int
798 sd_test_unit_ready(int unit, int flags)
799 {
800 struct scsi_test_unit_ready scsi_cmd;
801
802 bzero(&scsi_cmd, sizeof(scsi_cmd));
803 scsi_cmd.op_code = TEST_UNIT_READY;
804
805 return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
806 sizeof(scsi_cmd), 0, 0, 100000, flags);
807 }
808
809 /*
810 * format disk
811 */
812 int
813 sd_format(int unit, struct scsi_format_parms *f, int flags, int type)
814 {
815 struct scsi_prevent scsi_cmd;
816
817 bzero(&scsi_cmd, sizeof(scsi_cmd));
818 scsi_cmd.op_code = FORMAT_DISK;
819 scsi_cmd.prevent= type;
820 return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
821 sizeof(scsi_cmd), (u_char *)f, sizeof *f, 500000000, flags);
822 }
823
824 /*
825 * Prevent or allow the user to remove the tape
826 */
827 int
828 sd_prevent(int unit, int type, int flags)
829 {
830 struct scsi_prevent scsi_cmd;
831
832 bzero(&scsi_cmd, sizeof(scsi_cmd));
833 scsi_cmd.op_code = PREVENT_ALLOW;
834 scsi_cmd.prevent=type;
835 return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
836 sizeof(scsi_cmd), 0, 0, 5000, flags);
837 }
838
839 /*
840 * Get scsi driver to send a "start up" command
841 */
842 int
843 sd_start_unit(int unit, int flags)
844 {
845 struct scsi_start_stop scsi_cmd;
846
847 bzero(&scsi_cmd, sizeof(scsi_cmd));
848 scsi_cmd.op_code = START_STOP;
849 scsi_cmd.start = 1;
850
851 return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
852 sizeof(scsi_cmd), 0, 0, 2000, flags);
853 }
854
855 /*
856 * Tell the device to map out a defective block
857 */
858 int
859 sd_reassign_blocks(int unit, int block)
860 {
861 struct scsi_reassign_blocks_data rbdata;
862 struct scsi_reassign_blocks scsi_cmd;
863
864 bzero(&scsi_cmd, sizeof(scsi_cmd));
865 bzero(&rbdata, sizeof(rbdata));
866 scsi_cmd.op_code = REASSIGN_BLOCKS;
867
868 rbdata.length_msb = 0;
869 rbdata.length_lsb = sizeof(rbdata.defect_descriptor[0]);
870 rbdata.defect_descriptor[0].dlbaddr_3 = ((block >> 24) & 0xff);
871 rbdata.defect_descriptor[0].dlbaddr_2 = ((block >> 16) & 0xff);
872 rbdata.defect_descriptor[0].dlbaddr_1 = ((block >> 8) & 0xff);
873 rbdata.defect_descriptor[0].dlbaddr_0 = ((block ) & 0xff);
874
875 return sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
876 sizeof(scsi_cmd), (u_char *)&rbdata, sizeof(rbdata), 5000, 0);
877 }
878
879 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
880
881 /*
882 * Get the scsi driver to send a full inquiry to the
883 * device and use the results to fill out the disk
884 * parameter structure.
885 */
886 int
887 sd_get_parms(int unit, int flags)
888 {
889 struct sd_data *sd = sd_data[unit];
890 struct disk_parms *disk_parms = &sd->params;
891 struct scsi_mode_sense scsi_cmd;
892 struct scsi_mode_sense_data {
893 struct scsi_mode_header header;
894 struct blk_desc blk_desc;
895 union disk_pages pages;
896 } scsi_sense;
897 int sectors;
898
899 /* First check if we have it all loaded */
900 if(sd->flags & SDVALID)
901 return 0;
902
903 /* First do a mode sense page 3 */
904 if (sd_debug) {
905 bzero(&scsi_cmd, sizeof(scsi_cmd));
906 scsi_cmd.op_code = MODE_SENSE;
907 scsi_cmd.page_code = 3;
908 scsi_cmd.length = 0x24;
909
910 /*
911 * do the command, but we don't need the results
912 * just print them for our interest's sake
913 */
914 if (sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
915 sizeof(scsi_cmd), (u_char *)&scsi_sense, sizeof(scsi_sense),
916 2000, flags) != 0) {
917 printf("could not mode sense (3) for unit %d\n", unit);
918 return ENXIO;
919 }
920 printf("unit %d: %d trk/zn, %d altsec/zn, %d alttrk/zn, %d alttrk/lun\n",
921 unit, b2tol(scsi_sense.pages.disk_format.trk_z),
922 b2tol(scsi_sense.pages.disk_format.alt_sec),
923 b2tol(scsi_sense.pages.disk_format.alt_trk_z),
924 b2tol(scsi_sense.pages.disk_format.alt_trk_v));
925 printf(" %d sec/trk, %d byte/sec, %d interleave, %d %d bytes/log_blk\n",
926 b2tol(scsi_sense.pages.disk_format.ph_sec_t),
927 b2tol(scsi_sense.pages.disk_format.bytes_s),
928 b2tol(scsi_sense.pages.disk_format.interleave),
929 sd_size(unit, flags),
930 _3btol((u_char *)scsi_sense.blk_desc.blklen));
931 }
932
933
934 /* do a "mode sense page 4" */
935 bzero(&scsi_cmd, sizeof(scsi_cmd));
936 scsi_cmd.op_code = MODE_SENSE;
937 scsi_cmd.page_code = 4;
938 scsi_cmd.length = 0x20;
939
940 /*
941 * If the command worked, use the results to fill out
942 * the parameter structure
943 */
944 if (sd_scsi_cmd(unit, (struct scsi_generic *)&scsi_cmd,
945 sizeof(scsi_cmd), (u_char *)&scsi_sense, sizeof(scsi_sense),
946 2000, flags) != 0) {
947 printf("could not mode sense (4) for unit %d\n", unit);
948 printf(" using ficticious geometry\n");
949 sectors = sd_size(unit, flags);
950 disk_parms->heads = 64;
951 disk_parms->sectors = 32;
952 disk_parms->cyls = sectors/(64 * 32);
953 disk_parms->secsiz = SECSIZE;
954 } else {
955 if (sd_debug) {
956 printf(" %d cyl, %d head, %d precomp, %d redwrite, %d land\n",
957 _3btol((u_char *)&scsi_sense.pages.rigid_geometry.ncyl_2),
958 scsi_sense.pages.rigid_geometry.nheads,
959 b2tol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
960 b2tol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
961 b2tol(scsi_sense.pages.rigid_geometry.land_zone));
962 }
963
964 /*
965 * KLUDGE!!(for zone recorded disks)
966 * give a number of sectors so that sec * trks * cyls
967 * is <= disk_size
968 */
969 disk_parms->heads = scsi_sense.pages.rigid_geometry.nheads;
970 disk_parms->cyls =
971 _3btol((u_char *)&scsi_sense.pages.rigid_geometry.ncyl_2);
972 disk_parms->secsiz = _3btol((u_char *)&scsi_sense.blk_desc.blklen);
973
974 sectors = sd_size(unit, flags);
975 sectors /= disk_parms->cyls;
976 sectors /= disk_parms->heads;
977 disk_parms->sectors = sectors; /* dubious on SCSI*/
978 }
979
980 disk_parms->disksize = disk_parms->sectors * disk_parms->heads *
981 disk_parms->cyls;
982 sd->flags |= SDVALID;
983 return 0;
984 }
985
986 /*
987 * close the device.. only called if we are the LAST
988 * occurence of an open device
989 */
990 int
991 sdclose(dev_t dev)
992 {
993 struct sd_data *sd;
994 unsigned char unit, part;
995 unsigned int old_priority;
996
997 unit = UNIT(dev);
998 part = PARTITION(dev);
999 sd = sd_data[unit];
1000 sd->partflags[part] &= ~SDOPEN;
1001 sd->openparts &= ~(1 << part);
1002 if(sd->openparts == 0)
1003 sd_prevent(unit, PR_ALLOW, SCSI_SILENT|SCSI_ERR_OK);
1004 return 0;
1005 }
1006
1007 /*
1008 * ask the scsi driver to perform a command for us.
1009 * Call it through the switch table, and tell it which
1010 * sub-unit we want, and what target and lu we wish to
1011 * talk to. Also tell it where to find the command
1012 * how long int is.
1013 * Also tell it where to read/write the data, and how
1014 * long the data is supposed to be
1015 */
1016 int
1017 sd_scsi_cmd(int unit, struct scsi_generic *scsi_cmd, int cmdlen,
1018 u_char *data_addr, int datalen, int timeout, int flags)
1019 {
1020 struct sd_data *sd = sd_data[unit];
1021 struct scsi_xfer *xs;
1022 int retval, s;
1023
1024 if(scsi_debug & PRINTROUTINES)
1025 printf("\nsd_scsi_cmd%d ",unit);
1026 if(!sd->sc_sw) {
1027 printf("sd%d: not set up\n",unit);
1028 return EINVAL;
1029 }
1030
1031 xs = sd_get_xs(unit,flags); /* should wait unless booting */
1032 if(!xs) {
1033 printf("sd_scsi_cmd%d: controller busy"
1034 " (this should never happen)\n",unit);
1035 return EBUSY;
1036 }
1037
1038 xs->flags |= INUSE;
1039 xs->flags |= flags;
1040 xs->adapter = sd->ctlr;
1041 xs->targ = sd->targ;
1042 xs->lu = sd->lu;
1043 xs->retries = SD_RETRIES;
1044 xs->timeout = timeout;
1045 xs->cmd = scsi_cmd;
1046 xs->cmdlen = cmdlen;
1047 xs->data = data_addr;
1048 xs->datalen = datalen;
1049 xs->resid = datalen;
1050 xs->when_done = (flags & SCSI_NOMASK) ?(int (*)())0 : sd_done;
1051 xs->done_arg = unit;
1052 xs->done_arg2 = (int)xs;
1053
1054 retry:
1055 xs->error = XS_NOERROR;
1056 xs->bp = 0;
1057 retval = (*(sd->sc_sw->scsi_cmd))(xs);
1058 switch(retval) {
1059 case SUCCESSFULLY_QUEUED:
1060 s = splbio();
1061 while(!(xs->flags & ITSDONE))
1062 sleep(xs,PRIBIO+1);
1063 splx(s);
1064 case HAD_ERROR:
1065 /*printf("err = %d ", xs->error);*/
1066 switch(xs->error) {
1067 case XS_NOERROR:
1068 retval = ESUCCESS;
1069 break;
1070 case XS_SENSE:
1071 retval = sd_interpret_sense(unit, xs);
1072 break;
1073 case XS_DRIVER_STUFFUP:
1074 retval = EIO;
1075 break;
1076 case XS_TIMEOUT:
1077 case XS_BUSY:
1078 if(xs->retries-- ) {
1079 xs->flags &= ~ITSDONE;
1080 goto retry;
1081 }
1082 retval = EIO;
1083 break;
1084 default:
1085 retval = EIO;
1086 printf("sd%d: unknown error category from scsi driver\n", unit);
1087 }
1088 break;
1089 case COMPLETE:
1090 retval = ESUCCESS;
1091 break;
1092 case TRY_AGAIN_LATER:
1093 if(xs->retries-- ) {
1094 xs->flags &= ~ITSDONE;
1095 goto retry;
1096 }
1097 retval = EIO;
1098 break;
1099 default:
1100 retval = EIO;
1101 }
1102
1103 sd_free_xs(unit, xs, flags);
1104 sdstart(unit); /* check if anything is waiting fr the xs */
1105 return retval;
1106 }
1107
1108 /*
1109 * Look at the returned sense and act on the error and detirmine
1110 * The unix error number to pass back... (0 = report no error)
1111 */
1112 int
1113 sd_interpret_sense(int unit, struct scsi_xfer *xs)
1114 {
1115 struct sd_data *sd = sd_data[unit];
1116 struct scsi_sense_data *sense;
1117 int key, silent;
1118
1119 /* If the flags say errs are ok, then always return ok. */
1120 if (xs->flags & SCSI_ERR_OK)
1121 return ESUCCESS;
1122 silent = (xs->flags & SCSI_SILENT);
1123
1124 sense = &(xs->sense);
1125 switch(sense->error_class) {
1126 case 7:
1127 key = sense->ext.extended.sense_key;
1128 switch(key) {
1129 case 0x0:
1130 return ESUCCESS;
1131 case 0x1:
1132 if(!silent) {
1133 printf("sd%d: soft error(corrected) ", unit);
1134 if(sense->valid) {
1135 printf("block no. %d (decimal)",
1136 (sense->ext.extended.info[0] <<24),
1137 (sense->ext.extended.info[1] <<16),
1138 (sense->ext.extended.info[2] <<8),
1139 (sense->ext.extended.info[3] ));
1140 }
1141 printf("\n");
1142 }
1143 return ESUCCESS;
1144 case 0x2:
1145 if(!silent)
1146 printf("sd%d: not ready\n ", unit);
1147 return ENODEV;
1148 case 0x3:
1149 if(!silent) {
1150 printf("sd%d: medium error ", unit);
1151 if(sense->valid) {
1152 printf("block no. %d (decimal)",
1153 (sense->ext.extended.info[0] <<24),
1154 (sense->ext.extended.info[1] <<16),
1155 (sense->ext.extended.info[2] <<8),
1156 (sense->ext.extended.info[3] ));
1157 }
1158 printf("\n");
1159 }
1160 return EIO;
1161 case 0x4:
1162 if(!silent)
1163 printf("sd%d: non-media hardware failure\n ", unit);
1164 return EIO;
1165 case 0x5:
1166 if(!silent)
1167 printf("sd%d: illegal request\n ", unit);
1168 return EINVAL;
1169 case 0x6:
1170 /*
1171 * If we are not open, then this is not an error
1172 * as we don't have state yet. Either way, make
1173 * sure that we don't have any residual state
1174 */
1175 if(!silent)
1176 printf("sd%d: Unit attention.\n ", unit);
1177 sd->flags &= ~(SDVALID | SDHAVELABEL);
1178 if (sd->openparts)
1179 return EIO;
1180 return ESUCCESS; /* not an error if nothing's open */
1181 case 0x7:
1182 if(!silent) {
1183 printf("sd%d: attempted protection violation ", unit);
1184 if(sense->valid) {
1185 printf("block no. %d (decimal)\n",
1186 (sense->ext.extended.info[0] <<24),
1187 (sense->ext.extended.info[1] <<16),
1188 (sense->ext.extended.info[2] <<8),
1189 (sense->ext.extended.info[3] ));
1190 }
1191 printf("\n");
1192 }
1193 return EACCES;
1194 case 0x8:
1195 if(!silent) {
1196 printf("sd%d: block wrong state (worm)\n ", unit);
1197 if(sense->valid) {
1198 printf("block no. %d (decimal)\n",
1199 (sense->ext.extended.info[0] <<24),
1200 (sense->ext.extended.info[1] <<16),
1201 (sense->ext.extended.info[2] <<8),
1202 (sense->ext.extended.info[3] ));
1203 }
1204 printf("\n");
1205 }
1206 return EIO;
1207 case 0x9:
1208 if(!silent)
1209 printf("sd%d: vendor unique\n", unit);
1210 return EIO;
1211 case 0xa:
1212 if(!silent)
1213 printf("sd%d: copy aborted\n ", unit);
1214 return EIO;
1215 case 0xb:
1216 if(!silent)
1217 printf("sd%d: command aborted\n ", unit);
1218 return EIO;
1219 case 0xc:
1220 if(!silent) {
1221 printf("sd%d: search returned\n ", unit);
1222 if(sense->valid) {
1223 printf("block no. %d (decimal)\n",
1224 (sense->ext.extended.info[0] <<24),
1225 (sense->ext.extended.info[1] <<16),
1226 (sense->ext.extended.info[2] <<8),
1227 (sense->ext.extended.info[3] ));
1228 }
1229 printf("\n");
1230 }
1231 return ESUCCESS;
1232 case 0xd:
1233 if(!silent)
1234 printf("sd%d: volume overflow\n ", unit);
1235 return ENOSPC;
1236 case 0xe:
1237 if(!silent) {
1238 printf("sd%d: verify miscompare\n ", unit);
1239 if(sense->valid) {
1240 printf("block no. %d (decimal)\n",
1241 (sense->ext.extended.info[0] <<24),
1242 (sense->ext.extended.info[1] <<16),
1243 (sense->ext.extended.info[2] <<8),
1244 (sense->ext.extended.info[3] ));
1245 }
1246 printf("\n");
1247 }
1248 return EIO;
1249 case 0xf:
1250 if(!silent)
1251 printf("sd%d: unknown error key\n ", unit);
1252 return EIO;
1253 }
1254 break;
1255 case 0:
1256 case 1:
1257 case 2:
1258 case 3:
1259 case 4:
1260 case 5:
1261 case 6:
1262 if(!silent)printf("sd%d: error class %d code %d\n", unit,
1263 sense->error_class, sense->error_code);
1264 if(sense->valid)
1265 if(!silent)
1266 printf("block no. %d (decimal)\n",
1267 (sense->ext.unextended.blockhi <<16),
1268 + (sense->ext.unextended.blockmed <<8),
1269 + (sense->ext.unextended.blocklow ));
1270 return EIO;
1271 }
1272 return 0; /* XXX? */
1273 }
1274
1275 int
1276 sdsize(dev_t dev)
1277 {
1278 int unit = UNIT(dev), part = PARTITION(dev), val;
1279 struct sd_data *sd;
1280
1281 if (unit >= NSD)
1282 return -1;
1283 if(!sd_data[unit])
1284 return -1;
1285
1286 sd = sd_data[unit];
1287 if((sd->flags & SDINIT) == 0)
1288 return -1;
1289
1290 if( sd==0 || (sd->flags & SDHAVELABEL)==0 )
1291 val = sdopen(MAKESDDEV(major(dev), unit, RAW_PART));
1292 if ( val!=0 || sd->flags & SDWRITEPROT)
1293 return -1;
1294
1295 return (int)sd->disklabel.d_partitions[part].p_size;
1296 }
1297
1298 sddump()
1299 {
1300 printf("sddump() -- not implemented\n");
1301 return -1;
1302 }
1303