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