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