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