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