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