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