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