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