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