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