sd.c revision 1.18.2.5 1 /*
2 * Written by Julian Elischer (julian (at) dialix.oz.au)
3 * for TRW Financial Systems for use under the MACH(2.5) operating system.
4 *
5 * TRW Financial Systems, in accordance with their agreement with Carnegie
6 * Mellon University, makes this software available to CMU to distribute
7 * or use in any manner that they see fit as long as this message is kept with
8 * the software. For this reason TFS also grants any other persons or
9 * organisations permission to use or modify this software.
10 *
11 * TFS supplies this software to be publicly redistributed
12 * on the understanding that TFS is not responsible for the correct
13 * functioning of this software in any circumstances.
14 *
15 * Ported to run under 386BSD by Julian Elischer (julian (at) dialix.oz.au) Sept 1992
16 *
17 * $Id: sd.c,v 1.18.2.5 1993/11/24 20:24:26 mycroft Exp $
18 */
19
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/dkbad.h>
23 #include <sys/systm.h>
24 #include <sys/conf.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/device.h>
33 #include <sys/disklabel.h>
34 #include <sys/disk.h>
35
36 #include <scsi/scsi_all.h>
37 #include <scsi/scsi_disk.h>
38 #include <scsi/scsiconf.h>
39
40 #ifdef DDB
41 int Debugger();
42 #else /* DDB */
43 #define Debugger()
44 #endif /* DDB */
45
46 #define SECSIZE 512
47 #define SDOUTSTANDING 2
48 #define SDQSIZE 4
49 #define SD_RETRIES 4
50
51 #define MAKESDDEV(maj, unit, part) (makedev(maj,(unit<<3)|part))
52 #define SDPART(z) (minor(z) & 7)
53 #define SDUNIT(z) (minor(z) >> 3)
54 #define RAW_PART 3
55
56 void sdstrategy();
57 void sdstart();
58
59 struct scsi_device sd_switch =
60 {
61 NULL, /* Use default error handler */
62 sdstart, /* have a queue, served by this */
63 NULL, /* have no async handler */
64 NULL, /* Use default 'done' routine */
65 "sd",
66 0
67 };
68
69 struct sd_data {
70 struct dkdevice sc_dk;
71
72 u_int32 flags;
73 #define SDINIT 0x04 /* device has been init'd */
74 #define SDHAVELABEL 0x10 /* have read the label */
75 #define SDDOSPART 0x20 /* Have read the DOS partition table */
76 #define SDWRITEPROT 0x40 /* Device in readonly mode (S/W) */
77 struct scsi_link *sc_link; /* contains our targ, lun etc. */
78 u_int32 ad_info; /* info about the adapter */
79 u_int32 cmdscount; /* cmds allowed outstanding by board */
80 boolean wlabel; /* label is writable */
81 struct disk_parms {
82 u_char heads; /* Number of heads */
83 u_int16 cyls; /* Number of cylinders */
84 u_char sectors; /*dubious *//* Number of sectors/track */
85 u_int16 secsiz; /* Number of bytes/sector */
86 u_int32 disksize; /* total number sectors */
87 } params;
88 u_int32 partflags[MAXPARTITIONS]; /* per partition flags */
89 #define SDOPEN 0x01
90 u_int32 openparts; /* one bit for each open partition */
91 u_int32 sd_start_of_unix; /* unix vs dos partitions */
92 struct buf buf_queue;
93 u_int32 xfer_block_wait;
94 };
95
96 void sdattach __P((struct device *, struct device *, void *));
97
98 struct cfdriver sdcd =
99 { NULL, "sd", scsi_targmatch, sdattach, DV_DISK, sizeof(struct sd_data) };
100
101 int sdgetdisklabel __P((struct sd_data *));
102 int sd_get_parms __P((struct sd_data *, int));
103
104 /*
105 * The routine called by the low level scsi routine when it discovers
106 * a device suitable for this driver.
107 */
108 void
109 sdattach(parent, self, aux)
110 struct device *parent, *self;
111 void *aux;
112 {
113 struct sd_data *sd = (struct sd_data *)self;
114 struct disk_parms *dp = &sd->params;
115 struct scsi_link *sc_link = aux;
116
117 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
118
119 /*
120 * Store information needed to contact our base driver
121 */
122 sd->sc_link = sc_link;
123 sc_link->device = &sd_switch;
124 sc_link->dev_unit = self->dv_unit;
125
126 if (sd->sc_link->adapter->adapter_info) {
127 sd->ad_info = ((*(sd->sc_link->adapter->adapter_info)) (sc_link->adapter_softc));
128 sd->cmdscount = sd->ad_info & AD_INF_MAX_CMDS;
129 if (sd->cmdscount > SDOUTSTANDING) {
130 sd->cmdscount = SDOUTSTANDING;
131 }
132 } else {
133 sd->ad_info = 1;
134 sd->cmdscount = 1;
135 }
136 sc_link->opennings = sd->cmdscount;
137 /*
138 * Use the subdriver to request information regarding
139 * the drive. We cannot use interrupts yet, so the
140 * request must specify this.
141 */
142 sd_get_parms(sd, SCSI_NOSLEEP | SCSI_NOMASK);
143 printf(": %dMB (%d total sec), %d cyl, %d head, %d sec, %d bytes/sec\n",
144 self->dv_xname, dp->disksize / ((1024L * 1024L) / dp->secsiz),
145 dp->disksize, dp->cyls, dp->heads, dp->sectors, dp->secsiz);
146 sd->flags |= SDINIT;
147 }
148
149 /*
150 * open the device. Make sure the partition info is a up-to-date as can be.
151 */
152 int
153 sdopen(dev)
154 dev_t dev;
155 {
156 int errcode = 0;
157 int unit, part;
158 struct sd_data *sd;
159 struct scsi_link *sc_link;
160
161 unit = SDUNIT(dev);
162 part = SDPART(dev);
163
164 if (unit >= sdcd.cd_ndevs)
165 return ENXIO;
166 sd = sdcd.cd_devs[unit];
167 /*
168 * Make sure the disk has been initialised
169 * At some point in the future, get the scsi driver
170 * to look for a new device if we are not initted
171 */
172 if (!sd || !(sd->flags & SDINIT))
173 return ENXIO;
174
175 sc_link = sd->sc_link;
176
177 SC_DEBUG(sc_link, SDEV_DB1,
178 ("sdopen: dev=0x%x (unit %d (of %d),partition %d)\n"
179 ,dev, unit, sdcd.cd_ndevs, part));
180
181 /*
182 * "unit attention" errors should occur here if the
183 * drive has been restarted or the pack changed.
184 * just ingnore the result, it's a decoy instruction
185 * The error code will act on the error though
186 * and invalidate any media information we had.
187 */
188 scsi_test_unit_ready(sc_link, 0);
189
190 /*
191 * If it's been invalidated, then forget the label
192 */
193 sc_link->flags |= SDEV_OPEN; /* unit attn becomes an err now */
194 if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
195 sd->flags &= ~SDHAVELABEL;
196
197 /*
198 * If somebody still has it open, then forbid re-entry.
199 */
200 if (sd->openparts) {
201 errcode = ENXIO;
202 goto bad;
203 }
204 }
205
206 /*
207 * In case it is a funny one, tell it to start
208 * not needed for most hard drives (ignore failure)
209 */
210 scsi_start_unit(sc_link, SCSI_ERR_OK | SCSI_SILENT);
211
212 /*
213 * Check that it is still responding and ok.
214 */
215 if (scsi_test_unit_ready(sc_link, 0)) {
216 SC_DEBUG(sc_link, SDEV_DB3, ("device not reponding\n"));
217 errcode = ENXIO;
218 goto bad;
219 }
220 SC_DEBUG(sc_link, SDEV_DB3, ("device ok\n"));
221
222 /*
223 * Load the physical device parameters
224 */
225 sd_get_parms(sd, 0); /* sets SDEV_MEDIA_LOADED */
226 if (sd->params.secsiz != SECSIZE) { /* XXX One day... */
227 printf("sd%d: Can't deal with %d bytes logical blocks\n",
228 unit, sd->params.secsiz);
229 Debugger();
230 errcode = ENXIO;
231 goto bad;
232 }
233 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
234
235 /* Lock the pack in. */
236 scsi_prevent(sc_link, PR_PREVENT, SCSI_ERR_OK | SCSI_SILENT);
237
238 /*
239 * Load the partition info if not already loaded.
240 */
241 if ((errcode = sdgetdisklabel(sd)) && (part != RAW_PART))
242 goto bad;
243 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
244
245 /*
246 * Check the partition is legal
247 */
248 if (part >= MAXPARTITIONS) {
249 errcode = ENXIO;
250 goto bad;
251 }
252 SC_DEBUG(sc_link, SDEV_DB3, ("partition ok"));
253
254 /*
255 * Check that the partition exists
256 */
257 if ((sd->sc_dk.dk_label.d_partitions[part].p_size == 0)
258 && (part != RAW_PART)) {
259 errcode = ENXIO;
260 goto bad;
261 }
262 sd->partflags[part] |= SDOPEN;
263 sd->openparts |= (1 << part);
264 SC_DEBUG(sc_link, SDEV_DB3, ("open\n"));
265 return 0;
266
267 bad:
268 if (!(sd->openparts)) {
269 scsi_prevent(sc_link, PR_ALLOW, SCSI_ERR_OK | SCSI_SILENT);
270 sc_link->flags &= ~SDEV_OPEN;
271 }
272 return errcode;
273 }
274
275 /*
276 * close the device.. only called if we are the LAST occurence of an open
277 * device. Convenient now but usually a pain.
278 */
279 int
280 sdclose(dev)
281 dev_t dev;
282 {
283 int unit, part;
284 struct sd_data *sd;
285
286 unit = SDUNIT(dev);
287 part = SDPART(dev);
288 sd = sdcd.cd_devs[unit];
289 sd->partflags[part] &= ~SDOPEN;
290 sd->openparts &= ~(1 << part);
291 if (!(sd->openparts)) {
292 scsi_prevent(sd->sc_link, PR_ALLOW, SCSI_SILENT | SCSI_ERR_OK);
293 sd->sc_link->flags &= ~SDEV_OPEN;
294 }
295 return 0;
296 }
297
298 /*
299 * trim the size of the transfer if needed, called by physio
300 * basically the smaller of our max and the scsi driver's
301 * minphys (note we have no max)
302 *
303 * Trim buffer length if buffer-size is bigger than page size
304 */
305 void
306 sdminphys(bp)
307 struct buf *bp;
308 {
309 register struct sd_data *sd = sdcd.cd_devs[SDUNIT(bp->b_dev)];
310
311 (sd->sc_link->adapter->scsi_minphys) (bp);
312 }
313
314 /*
315 * Actually translate the requested transfer into one the physical driver
316 * can understand. The transfer is described by a buf and will include
317 * only one physical transfer.
318 */
319 void
320 sdstrategy(bp)
321 struct buf *bp;
322 {
323 struct buf *dp;
324 int opri;
325 struct sd_data *sd;
326 int unit;
327
328 unit = SDUNIT(bp->b_dev);
329 sd = sdcd.cd_devs[unit];
330 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
331 SC_DEBUG(sd->sc_link, SDEV_DB1,
332 (" %d bytes @ blk%d\n", bp->b_bcount, bp->b_blkno));
333 sdminphys(bp);
334 /*
335 * If the device has been made invalid, error out
336 */
337 if (!(sd->sc_link->flags & SDEV_MEDIA_LOADED)) {
338 sd->flags &= ~SDHAVELABEL;
339 bp->b_error = EIO;
340 goto bad;
341 }
342 /*
343 * "soft" write protect check
344 */
345 if ((sd->flags & SDWRITEPROT) && (bp->b_flags & B_READ) == 0) {
346 bp->b_error = EROFS;
347 goto bad;
348 }
349 /*
350 * If it's a null transfer, return immediatly
351 */
352 if (bp->b_bcount == 0) {
353 goto done;
354 }
355 /*
356 * Decide which unit and partition we are talking about
357 * only raw is ok if no label
358 */
359 if (SDPART(bp->b_dev) != RAW_PART) {
360 if (!(sd->flags & SDHAVELABEL)) {
361 bp->b_error = EIO;
362 goto bad;
363 }
364 /*
365 * do bounds checking, adjust transfer. if error, process.
366 * if end of partition, just return
367 */
368 if (bounds_check_with_label(bp, &sd->sc_dk.dk_label, sd->wlabel) <= 0)
369 goto done;
370 /* otherwise, process transfer request */
371 }
372 opri = splbio();
373 dp = &sd->buf_queue;
374
375 /*
376 * Place it in the queue of disk activities for this disk
377 */
378 disksort(dp, bp);
379
380 /*
381 * Tell the device to get going on the transfer if it's
382 * not doing anything, otherwise just wait for completion
383 */
384 sdstart(unit);
385
386 splx(opri);
387 bad:
388 bp->b_flags |= B_ERROR;
389 done:
390
391 /*
392 * Correctly set the buf to indicate a completed xfer
393 */
394 bp->b_resid = bp->b_bcount;
395 biodone(bp);
396 }
397
398 /*
399 * sdstart looks to see if there is a buf waiting for the device
400 * and that the device is not already busy. If both are true,
401 * It dequeues the buf and creates a scsi command to perform the
402 * transfer in the buf. The transfer request will call scsi_done
403 * on completion, which will in turn call this routine again
404 * so that the next queued transfer is performed.
405 * The bufs are queued by the strategy routine (sdstrategy)
406 *
407 * This routine is also called after other non-queued requests
408 * have been made of the scsi driver, to ensure that the queue
409 * continues to be drained.
410 *
411 * must be called at the correct (highish) spl level
412 * sdstart() is called at splbio from sdstrategy and scsi_done
413 */
414 void
415 sdstart(unit)
416 int unit;
417 {
418 register struct sd_data *sd = sdcd.cd_devs[unit];
419 register struct scsi_link *sc_link = sd->sc_link;
420 struct buf *bp = 0;
421 struct buf *dp;
422 struct scsi_rw_big cmd;
423 int blkno, nblk;
424 struct partition *p;
425
426 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
427 /*
428 * Check if the device has room for another command
429 */
430 while (sc_link->opennings) {
431
432 /*
433 * there is excess capacity, but a special waits
434 * It'll need the adapter as soon as we clear out of the
435 * way and let it run (user level wait).
436 */
437 if (sc_link->flags & SDEV_WAITING) {
438 return;
439 }
440 /*
441 * See if there is a buf with work for us to do..
442 */
443 dp = &sd->buf_queue;
444 if ((bp = dp->b_actf) == NULL) { /* yes, an assign */
445 return;
446 }
447 dp->b_actf = bp->av_forw;
448
449 /*
450 * If the device has become invalid, abort all the
451 * reads and writes until all files have been closed and
452 * re-openned
453 */
454 if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
455 sd->flags &= ~SDHAVELABEL;
456 goto bad;
457 }
458 /*
459 * We have a buf, now we know we are going to go through
460 * With this thing..
461 *
462 * First, translate the block to absolute
463 */
464 p = sd->sc_dk.dk_label.d_partitions + SDPART(bp->b_dev);
465 blkno = bp->b_blkno + p->p_offset;
466 nblk = (bp->b_bcount + 511) >> 9;
467
468 /*
469 * Fill out the scsi command
470 */
471 bzero(&cmd, sizeof(cmd));
472 cmd.op_code = (bp->b_flags & B_READ)
473 ? READ_BIG : WRITE_BIG;
474 cmd.addr_3 = (blkno & 0xff000000) >> 24;
475 cmd.addr_2 = (blkno & 0xff0000) >> 16;
476 cmd.addr_1 = (blkno & 0xff00) >> 8;
477 cmd.addr_0 = blkno & 0xff;
478 cmd.length2 = (nblk & 0xff00) >> 8;
479 cmd.length1 = (nblk & 0xff);
480 /*
481 * Call the routine that chats with the adapter.
482 * Note: we cannot sleep as we may be an interrupt
483 */
484 if (scsi_scsi_cmd(sc_link,
485 (struct scsi_generic *) &cmd,
486 sizeof(cmd),
487 (u_char *) bp->b_un.b_addr,
488 bp->b_bcount,
489 SD_RETRIES,
490 10000,
491 bp,
492 SCSI_NOSLEEP | ((bp->b_flags & B_READ) ?
493 SCSI_DATA_IN : SCSI_DATA_OUT))
494 == SUCCESSFULLY_QUEUED) {
495 } else {
496 bad:
497 printf("sd%d: oops not queued", unit);
498 bp->b_error = EIO;
499 bp->b_flags |= B_ERROR;
500 biodone(bp);
501 }
502 }
503 }
504
505 /*
506 * Perform special action on behalf of the user
507 * Knows about the internals of this device
508 */
509 int
510 sdioctl(dev_t dev, int cmd, caddr_t addr, int flag)
511 {
512 /* struct sd_cmd_buf *args; */
513 int error = 0;
514 unsigned char unit, part;
515 register struct sd_data *sd;
516
517 /*
518 * Find the device that the user is talking about
519 */
520 unit = SDUNIT(dev);
521 part = SDPART(dev);
522 sd = sdcd.cd_devs[unit];
523 SC_DEBUG(sd->sc_link, SDEV_DB1, ("sdioctl (0x%x)", cmd));
524
525 /*
526 * If the device is not valid.. abandon ship
527 */
528 if (!(sd->sc_link->flags & SDEV_MEDIA_LOADED))
529 return EIO;
530 switch (cmd) {
531
532 case DIOCSBAD:
533 error = EINVAL;
534 break;
535
536 case DIOCGDINFO:
537 *(struct disklabel *) addr = sd->sc_dk.dk_label;
538 break;
539
540 case DIOCGPART:
541 ((struct partinfo *) addr)->disklab = &sd->sc_dk.dk_label;
542 ((struct partinfo *) addr)->part =
543 &sd->sc_dk.dk_label.d_partitions[SDPART(dev)];
544 break;
545
546 case DIOCSDINFO:
547 if ((flag & FWRITE) == 0)
548 error = EBADF;
549 else
550 error = setdisklabel(&sd->sc_dk.dk_label,
551 (struct disklabel *)addr,
552 /*(sd->flags & DKFL_BSDLABEL) ? sd->openparts : */ 0,
553 &sd->sc_dk.dk_cpulabel);
554 if (error == 0)
555 sd->flags |= SDHAVELABEL;
556 break;
557
558 case DIOCWLABEL:
559 sd->flags &= ~SDWRITEPROT;
560 if ((flag & FWRITE) == 0)
561 error = EBADF;
562 else
563 sd->wlabel = *(boolean *) addr;
564 break;
565
566 case DIOCWDINFO:
567 sd->flags &= ~SDWRITEPROT;
568 if ((flag & FWRITE) == 0)
569 error = EBADF;
570 else {
571 error = setdisklabel(&sd->sc_dk.dk_label,
572 (struct disklabel *)addr,
573 /*(sd->flags & SDHAVELABEL) ? sd->openparts : */ 0,
574 &sd->sc_dk.dk_cpulabel);
575 if (!error) {
576 boolean wlab;
577
578 /* ok - write will succeed */
579 sd->flags |= SDHAVELABEL;
580
581 /* simulate opening partition 0 so write succeeds */
582 sd->openparts |= (1 << 0); /* XXX */
583 wlab = sd->wlabel;
584 sd->wlabel = 1;
585 error = writedisklabel(dev, sdstrategy,
586 &sd->sc_dk.dk_label,
587 &sd->sc_dk.dk_cpulabel);
588 sd->wlabel = wlab;
589 }
590 }
591 break;
592
593 default:
594 if (part == RAW_PART)
595 error = scsi_do_ioctl(sd->sc_link, cmd, addr, flag);
596 else
597 error = ENOTTY;
598 break;
599 }
600 return error;
601 }
602
603 /*
604 * Load the label information on the named device
605 */
606 int
607 sdgetdisklabel(sd)
608 struct sd_data *sd;
609 {
610 char *errstring;
611
612 /*
613 * If the inflo is already loaded, use it
614 */
615 if (sd->flags & SDHAVELABEL)
616 return 0;
617
618 bzero(&sd->sc_dk.dk_label, sizeof(struct disklabel));
619 /*
620 * make partition 3 the whole disk in case of failure then get pdinfo
621 * for historical reasons, make part a same as raw part
622 */
623 sd->sc_dk.dk_label.d_partitions[0].p_offset = 0;
624 sd->sc_dk.dk_label.d_partitions[0].p_size = sd->params.disksize;
625 sd->sc_dk.dk_label.d_partitions[RAW_PART].p_offset = 0;
626 sd->sc_dk.dk_label.d_partitions[RAW_PART].p_size = sd->params.disksize;
627 sd->sc_dk.dk_label.d_npartitions = MAXPARTITIONS;
628 sd->sc_dk.dk_label.d_secsize = SECSIZE; /* as long as it's not 0 */
629 sd->sc_dk.dk_label.d_ntracks = sd->params.heads;
630 sd->sc_dk.dk_label.d_nsectors = sd->params.sectors;
631 sd->sc_dk.dk_label.d_ncylinders = sd->params.cyls;
632 sd->sc_dk.dk_label.d_secpercyl = sd->params.heads * sd->params.sectors;
633 if (sd->sc_dk.dk_label.d_secpercyl == 0) {
634 sd->sc_dk.dk_label.d_secpercyl = 100;
635 /* as long as it's not 0 - readdisklabel divides by it (?) */
636 }
637 /*
638 * Call the generic disklabel extraction routine
639 */
640 if (errstring = readdisklabel(MAKESDDEV(0, sd->sc_dk.dk_dev.dv_unit,
641 RAW_PART), sdstrategy,
642 &sd->sc_dk.dk_label,
643 &sd->sc_dk.dk_cpulabel)) {
644 printf("%s: %s\n", sd->sc_dk.dk_dev.dv_xname, errstring);
645 return ENXIO;
646 }
647 sd->flags |= SDHAVELABEL; /* WE HAVE IT ALL NOW */
648 return 0;
649 }
650
651 /*
652 * Find out from the device what it's capacity is
653 */
654 u_int32
655 sd_size(sd, flags)
656 struct sd_data *sd;
657 int flags;
658 {
659 struct scsi_read_cap_data rdcap;
660 struct scsi_read_capacity scsi_cmd;
661 u_int32 size;
662
663 /*
664 * make up a scsi command and ask the scsi driver to do
665 * it for you.
666 */
667 bzero(&scsi_cmd, sizeof(scsi_cmd));
668 scsi_cmd.op_code = READ_CAPACITY;
669
670 /*
671 * If the command works, interpret the result as a 4 byte
672 * number of blocks
673 */
674 if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *) &scsi_cmd,
675 sizeof(scsi_cmd), (u_char *) &rdcap, sizeof(rdcap),
676 SD_RETRIES, 2000, NULL, flags | SCSI_DATA_IN) != 0) {
677 printf("%s: could not get size\n", sd->sc_dk.dk_dev.dv_xname);
678 return 0;
679 } else {
680 size = rdcap.addr_0 + 1;
681 size += rdcap.addr_1 << 8;
682 size += rdcap.addr_2 << 16;
683 size += rdcap.addr_3 << 24;
684 }
685 return size;
686 }
687
688 /*
689 * Tell the device to map out a defective block
690 */
691 int
692 sd_reassign_blocks(sd, block)
693 struct sd_data *sd;
694 int block;
695 {
696 struct scsi_reassign_blocks scsi_cmd;
697 struct scsi_reassign_blocks_data rbdata;
698
699 bzero(&scsi_cmd, sizeof(scsi_cmd));
700 bzero(&rbdata, sizeof(rbdata));
701 scsi_cmd.op_code = REASSIGN_BLOCKS;
702
703 rbdata.length_msb = 0;
704 rbdata.length_lsb = sizeof(rbdata.defect_descriptor[0]);
705 rbdata.defect_descriptor[0].dlbaddr_3 = ((block >> 24) & 0xff);
706 rbdata.defect_descriptor[0].dlbaddr_2 = ((block >> 16) & 0xff);
707 rbdata.defect_descriptor[0].dlbaddr_1 = ((block >> 8) & 0xff);
708 rbdata.defect_descriptor[0].dlbaddr_0 = ((block) & 0xff);
709
710 return scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *) &scsi_cmd,
711 sizeof(scsi_cmd), (u_char *) &rbdata,
712 sizeof(rbdata), SD_RETRIES, 5000, NULL,
713 SCSI_DATA_OUT);
714 }
715
716 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
717
718 /*
719 * Get the scsi driver to send a full inquiry to the
720 * device and use the results to fill out the disk
721 * parameter structure.
722 */
723 int
724 sd_get_parms(sd, flags)
725 struct sd_data *sd;
726 int flags;
727 {
728 struct disk_parms *disk_parms = &sd->params;
729 struct scsi_mode_sense scsi_cmd;
730 struct scsi_mode_sense_data {
731 struct scsi_mode_header header;
732 struct blk_desc blk_desc;
733 union disk_pages pages;
734 } scsi_sense;
735 u_int32 sectors;
736
737 /*
738 * First check if we have it all loaded
739 */
740 if (sd->flags & SDEV_MEDIA_LOADED)
741 return 0;
742
743 /*
744 * do a "mode sense page 4"
745 */
746 bzero(&scsi_cmd, sizeof(scsi_cmd));
747 scsi_cmd.op_code = MODE_SENSE;
748 scsi_cmd.page = 4;
749 scsi_cmd.length = 0x20;
750 /*
751 * If the command worked, use the results to fill out
752 * the parameter structure
753 */
754 if (scsi_scsi_cmd(sd->sc_link, (struct scsi_generic *) &scsi_cmd,
755 sizeof(scsi_cmd), (u_char *) &scsi_sense,
756 sizeof(scsi_sense), SD_RETRIES, 2000, NULL,
757 flags | SCSI_DATA_IN) != 0) {
758
759 printf("%s: could not mode sense", sd->sc_dk.dk_dev.dv_xname);
760 printf(" (4); using ficticious geometry\n");
761 /*
762 * use adaptec standard ficticious geometry
763 * this depends on which controller (e.g. 1542C is
764 * different. but we have to put SOMETHING here..)
765 */
766 sectors = sd_size(sd, flags);
767 disk_parms->heads = 64;
768 disk_parms->sectors = 32;
769 disk_parms->cyls = sectors / (64 * 32);
770 disk_parms->secsiz = SECSIZE;
771 disk_parms->disksize = sectors;
772 } else {
773
774 SC_DEBUG(sd->sc_link, SDEV_DB3,
775 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
776 _3btol(&scsi_sense.pages.rigid_geometry.ncyl_2),
777 scsi_sense.pages.rigid_geometry.nheads,
778 b2tol(scsi_sense.pages.rigid_geometry.st_cyl_wp),
779 b2tol(scsi_sense.pages.rigid_geometry.st_cyl_rwc),
780 b2tol(scsi_sense.pages.rigid_geometry.land_zone)));
781
782 /*
783 * KLUDGE!!(for zone recorded disks)
784 * give a number of sectors so that sec * trks * cyls
785 * is <= disk_size
786 * can lead to wasted space! THINK ABOUT THIS !
787 */
788 disk_parms->heads = scsi_sense.pages.rigid_geometry.nheads;
789 disk_parms->cyls = _3btol(&scsi_sense.pages.rigid_geometry.ncyl_2);
790 disk_parms->secsiz = _3btol(scsi_sense.blk_desc.blklen);
791
792 sectors = sd_size(sd, flags);
793 disk_parms->disksize = sectors;
794 sectors /= (disk_parms->heads * disk_parms->cyls);
795 disk_parms->sectors = sectors; /* dubious on SCSI *//*XXX */
796 }
797 sd->sc_link->flags |= SDEV_MEDIA_LOADED;
798 return 0;
799 }
800
801 int
802 sdsize(dev_t dev)
803 {
804 int unit = SDUNIT(dev), part = SDPART(dev), val;
805 struct sd_data *sd;
806
807 if (unit >= sdcd.cd_ndevs)
808 return -1;
809 sd = sdcd.cd_devs[unit];
810 if (!sd || !(sd->flags & SDINIT))
811 return -1;
812
813 if ((sd->flags & SDHAVELABEL) == 0) {
814 val = sdopen(MAKESDDEV(major(dev), unit, RAW_PART), FREAD, S_IFBLK, 0);
815 if (val != 0)
816 return -1;
817 }
818 if (sd->flags & SDWRITEPROT)
819 return -1;
820
821 return (int)sd->sc_dk.dk_label.d_partitions[part].p_size;
822 }
823
824
825 #define SCSIDUMP 1
826 #undef SCSIDUMP
827 #define NOT_TRUSTED 1
828
829 #ifdef SCSIDUMP
830 #include <vm/vm.h>
831
832 static struct scsi_xfer sx;
833 #define MAXTRANSFER 8 /* 1 page at a time */
834
835 /*
836 * dump all of physical memory into the partition specified, starting
837 * at offset 'dumplo' into the partition.
838 */
839 int
840 sddump(dev_t dev)
841 { /* dump core after a system crash */
842 register struct sd_data *sd; /* disk unit to do the IO */
843 int32 num; /* number of sectors to write */
844 u_int32 unit, part;
845 int32 blkoff, blknum, blkcnt = MAXTRANSFER;
846 int32 nblocks;
847 char *addr;
848 struct scsi_rw_big cmd;
849 extern int Maxmem;
850 static int sddoingadump = 0;
851 #define MAPTO CADDR1
852 extern caddr_t MAPTO; /* map the page we are about to write, here */
853 struct scsi_xfer *xs = &sx;
854 int retval;
855 int c;
856
857 addr = (char *) 0; /* starting address */
858
859 /* toss any characters present prior to dump */
860 while ((c = sgetc(1)) && (c != 0x100)); /*syscons and pccons differ */
861
862 /* size of memory to dump */
863 num = Maxmem;
864 unit = SDUNIT(dev); /* eventually support floppies? */
865 part = SDPART(dev); /* file system */
866 /* check for acceptable drive number */
867 if (unit >= sdcd.cd_ndevs)
868 return ENXIO;
869
870 sd = sd_data[unit];
871 if (!sd)
872 return ENXIO;
873 /* was it ever initialized etc. ? */
874 if (!(sd->flags & SDINIT))
875 return ENXIO;
876 if (sd->sc_link->flags & SDEV_MEDIA_LOADED != SDEV_MEDIA_LOADED)
877 return ENXIO;
878 if (sd->flags & SDWRITEPROT)
879 return ENXIO;
880
881 /* Convert to disk sectors */
882 num = (u_int32) num * NBPG / sd->sc_dk.dk_label.d_secsize;
883
884 /* check if controller active */
885 if (sddoingadump)
886 return EFAULT;
887
888 nblocks = sd->sc_dk.dk_label.d_partitions[part].p_size;
889 blkoff = sd->sc_dk.dk_label.d_partitions[part].p_offset;
890
891 /* check transfer bounds against partition size */
892 if ((dumplo < 0) || ((dumplo + num) > nblocks))
893 return EINVAL;
894
895 sddoingadump = 1;
896
897 blknum = dumplo + blkoff;
898 /* blkcnt = initialise_me; */
899 while (num > 0) {
900 pmap_enter(kernel_pmap,
901 MAPTO,
902 trunc_page(addr),
903 VM_PROT_READ,
904 TRUE);
905 #ifndef NOT_TRUSTED
906 /*
907 * Fill out the scsi command
908 */
909 bzero(&cmd, sizeof(cmd));
910 cmd.op_code = WRITE_BIG;
911 cmd.addr_3 = (blknum & 0xff000000) >> 24;
912 cmd.addr_2 = (blknum & 0xff0000) >> 16;
913 cmd.addr_1 = (blknum & 0xff00) >> 8;
914 cmd.addr_0 = blknum & 0xff;
915 cmd.length2 = (blkcnt & 0xff00) >> 8;
916 cmd.length1 = (blkcnt & 0xff);
917 /*
918 * Fill out the scsi_xfer structure
919 * Note: we cannot sleep as we may be an interrupt
920 * don't use scsi_scsi_cmd() as it may want
921 * to wait for an xs.
922 */
923 bzero(xs, sizeof(sx));
924 xs->flags |= SCSI_NOMASK | SCSI_NOSLEEP | INUSE;
925 xs->sc_link = sd->sc_link;
926 xs->retries = SD_RETRIES;
927 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
928 xs->cmd = (struct scsi_generic *) &cmd;
929 xs->cmdlen = sizeof(cmd);
930 xs->resid = blkcnt * 512;
931 xs->error = XS_NOERROR;
932 xs->bp = 0;
933 xs->data = (u_char *) MAPTO;
934 xs->datalen = blkcnt * 512;
935
936 /*
937 * Pass all this info to the scsi driver.
938 */
939 retval = (*(sd->sc_link->adapter->scsi_cmd)) (xs);
940 switch (retval) {
941 case SUCCESSFULLY_QUEUED:
942 case HAD_ERROR:
943 return ENXIO; /* we said not to sleep! */
944 case COMPLETE:
945 break;
946 default:
947 return ENXIO; /* we said not to sleep! */
948 }
949 #else /* NOT_TRUSTED */
950 /* lets just talk about this first... */
951 printf("sd%d: dump addr 0x%x, blk %d\n", unit, addr, blknum);
952 #endif /* NOT_TRUSTED */
953
954 if ((unsigned) addr % (1024 * 1024) == 0)
955 printf("%d ", num / 2048);
956 /* update block count */
957 num -= blkcnt;
958 blknum += blkcnt;
959 (int) addr += 512 * blkcnt;
960
961 /* operator aborting dump? */
962 if ((c = sgetc(1)) && (c != 0x100))
963 return EINTR;
964 }
965 return 0;
966 }
967 #else /* SCSIDUMP */
968 int
969 sddump()
970 {
971 printf("\nsddump() -- not implemented\n");
972 DELAY(60000000); /* 60 seconds */
973 return -1;
974 }
975 #endif /* SCSIDUMP */
976