sd.c revision 1.116.2.3 1 /* $NetBSD: sd.c,v 1.116.2.3 1997/09/22 06:33:42 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995, 1997 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/file.h>
54 #include <sys/stat.h>
55 #include <sys/ioctl.h>
56 #include <sys/buf.h>
57 #include <sys/uio.h>
58 #include <sys/malloc.h>
59 #include <sys/errno.h>
60 #include <sys/device.h>
61 #include <sys/disklabel.h>
62 #include <sys/disk.h>
63 #include <sys/proc.h>
64 #include <sys/conf.h>
65
66 #include <dev/scsipi/scsi_all.h>
67 #include <dev/scsipi/scsipi_all.h>
68 #include <dev/scsipi/scsi_all.h>
69 #include <dev/scsipi/scsi_disk.h>
70 #include <dev/scsipi/scsipi_disk.h>
71 #include <dev/scsipi/scsiconf.h>
72
73 #ifndef SDOUTSTANDING
74 #define SDOUTSTANDING 4
75 #endif
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 disk 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 #define SDF_ANCIENT 0x10 /* disk is ancient; for minphys */
94 struct scsipi_link *sc_link; /* contains our targ, lun, etc. */
95 struct disk_parms {
96 u_char heads; /* number of heads */
97 u_short cyls; /* number of cylinders */
98 u_char sectors; /* number of sectors/track */
99 int blksize; /* number of bytes/sector */
100 u_long disksize; /* total number sectors */
101 } params;
102 struct buf buf_queue;
103 u_int8_t type;
104 };
105
106 struct scsi_mode_sense_data {
107 struct scsi_mode_header header;
108 struct scsi_blk_desc blk_desc;
109 union scsi_disk_pages pages;
110 };
111
112 #ifdef __BROKEN_INDIRECT_CONFIG
113 int sdmatch __P((struct device *, void *, void *));
114 #else
115 int sdmatch __P((struct device *, struct cfdata *, void *));
116 #endif
117 void sdattach __P((struct device *, struct device *, void *));
118 int sdlock __P((struct sd_softc *));
119 void sdunlock __P((struct sd_softc *));
120 void sdminphys __P((struct buf *));
121 void sdgetdisklabel __P((struct sd_softc *));
122 void sdstart __P((void *));
123 void sddone __P((struct scsipi_xfer *));
124 int sd_reassign_blocks __P((struct sd_softc *, u_long));
125 int sd_get_optparms __P((struct sd_softc *, int, struct disk_parms *));
126 int sd_get_parms __P((struct sd_softc *, int));
127 static int sd_mode_sense __P((struct sd_softc *, struct scsi_mode_sense_data *,
128 int, int));
129
130 struct cfattach sd_ca = {
131 sizeof(struct sd_softc), sdmatch, sdattach
132 };
133
134 struct cfdriver sd_cd = {
135 NULL, "sd", DV_DISK
136 };
137
138 struct dkdriver sddkdriver = { sdstrategy };
139
140 struct scsipi_device sd_switch = {
141 NULL, /* Use default error handler */
142 sdstart, /* have a queue, served by this */
143 NULL, /* have no async handler */
144 sddone, /* deal with stats at interrupt time */
145 };
146
147 struct scsipi_inquiry_pattern sd_patterns[] = {
148 {T_DIRECT, T_FIXED,
149 "", "", ""},
150 {T_DIRECT, T_REMOV,
151 "", "", ""},
152 {T_OPTICAL, T_FIXED,
153 "", "", ""},
154 {T_OPTICAL, T_REMOV,
155 "", "", ""},
156 };
157
158 int
159 sdmatch(parent, match, aux)
160 struct device *parent;
161 #ifdef __BROKEN_INDIRECT_CONFIG
162 void *match;
163 #else
164 struct cfdata *match;
165 #endif
166 void *aux;
167 {
168 struct scsipibus_attach_args *sa = aux;
169 int priority;
170
171 (void)scsipi_inqmatch(&sa->sa_inqbuf,
172 (caddr_t)sd_patterns, sizeof(sd_patterns)/sizeof(sd_patterns[0]),
173 sizeof(sd_patterns[0]), &priority);
174 return (priority);
175 }
176
177 /*
178 * The routine called by the low level scsi routine when it discovers
179 * a device suitable for this driver.
180 */
181 void
182 sdattach(parent, self, aux)
183 struct device *parent, *self;
184 void *aux;
185 {
186 int error;
187 struct sd_softc *sd = (void *)self;
188 struct disk_parms *dp = &sd->params;
189 struct scsipibus_attach_args *sa = aux;
190 struct scsipi_link *sc_link = sa->sa_sc_link;
191
192 SC_DEBUG(sc_link, SDEV_DB2, ("sdattach: "));
193
194 /*
195 * Store information needed to contact our base driver
196 */
197 sd->sc_link = sc_link;
198 sd->type = (sa->sa_inqbuf.type & SID_TYPE);
199 sc_link->device = &sd_switch;
200 sc_link->device_softc = sd;
201 if (sc_link->openings > SDOUTSTANDING)
202 sc_link->openings = SDOUTSTANDING;
203
204 /*
205 * Initialize and attach the disk structure.
206 */
207 sd->sc_dk.dk_driver = &sddkdriver;
208 sd->sc_dk.dk_name = sd->sc_dev.dv_xname;
209 disk_attach(&sd->sc_dk);
210
211 #if !defined(i386)
212 dk_establish(&sd->sc_dk, &sd->sc_dev); /* XXX */
213 #endif
214
215 /*
216 * Note if this device is ancient. This is used in sdminphys().
217 */
218 if ((sa->scsipi_info.scsi_version & SID_ANSII) == 0)
219 sd->flags |= SDF_ANCIENT;
220
221 /*
222 * Use the subdriver to request information regarding
223 * the drive. We cannot use interrupts yet, so the
224 * request must specify this.
225 */
226 printf("\n");
227 printf("%s: ", sd->sc_dev.dv_xname);
228
229 if ((sd->sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
230 error = scsipi_start(sd->sc_link, SSS_START,
231 SCSI_AUTOCONF | SCSI_IGNORE_ILLEGAL_REQUEST |
232 SCSI_IGNORE_MEDIA_CHANGE | SCSI_SILENT);
233 } else
234 error = 0;
235
236 if (error || sd_get_parms(sd, SCSI_AUTOCONF) != 0)
237 printf("drive offline\n");
238 else
239 printf("%ldMB, %d cyl, %d head, %d sec, %d bytes/sect x %ld sectors\n",
240 dp->disksize / (1048576 / dp->blksize), dp->cyls,
241 dp->heads, dp->sectors, dp->blksize, dp->disksize);
242 }
243
244 /*
245 * Wait interruptibly for an exclusive lock.
246 *
247 * XXX
248 * Several drivers do this; it should be abstracted and made MP-safe.
249 */
250 int
251 sdlock(sd)
252 struct sd_softc *sd;
253 {
254 int error;
255
256 while ((sd->flags & SDF_LOCKED) != 0) {
257 sd->flags |= SDF_WANTED;
258 if ((error = tsleep(sd, PRIBIO | PCATCH, "sdlck", 0)) != 0)
259 return error;
260 }
261 sd->flags |= SDF_LOCKED;
262 return 0;
263 }
264
265 /*
266 * Unlock and wake up any waiters.
267 */
268 void
269 sdunlock(sd)
270 struct sd_softc *sd;
271 {
272
273 sd->flags &= ~SDF_LOCKED;
274 if ((sd->flags & SDF_WANTED) != 0) {
275 sd->flags &= ~SDF_WANTED;
276 wakeup(sd);
277 }
278 }
279
280 /*
281 * open the device. Make sure the partition info is a up-to-date as can be.
282 */
283 int
284 sdopen(dev, flag, fmt, p)
285 dev_t dev;
286 int flag, fmt;
287 struct proc *p;
288 {
289 struct sd_softc *sd;
290 struct scsipi_link *sc_link;
291 int unit, part;
292 int error;
293
294 unit = SDUNIT(dev);
295 if (unit >= sd_cd.cd_ndevs)
296 return ENXIO;
297 sd = sd_cd.cd_devs[unit];
298 if (sd == NULL)
299 return ENXIO;
300
301 sc_link = sd->sc_link;
302
303 SC_DEBUG(sc_link, SDEV_DB1,
304 ("sdopen: dev=0x%x (unit %d (of %d), partition %d)\n", dev, unit,
305 sd_cd.cd_ndevs, SDPART(dev)));
306
307 if ((error = sdlock(sd)) != 0)
308 return error;
309
310 if (sd->sc_dk.dk_openmask != 0) {
311 /*
312 * If any partition is open, but the disk has been invalidated,
313 * disallow further opens.
314 */
315 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
316 error = EIO;
317 goto bad3;
318 }
319 } else {
320 /* Check that it is still responding and ok. */
321 error = scsipi_test_unit_ready(sc_link,
322 SCSI_IGNORE_ILLEGAL_REQUEST |
323 SCSI_IGNORE_MEDIA_CHANGE |
324 SCSI_IGNORE_NOT_READY);
325 if (error)
326 goto bad3;
327
328 /* Start the pack spinning if necessary. */
329 if ((sc_link->quirks & SDEV_NOSTARTUNIT) == 0) {
330 error = scsipi_start(sc_link, SSS_START,
331 SCSI_IGNORE_ILLEGAL_REQUEST |
332 SCSI_IGNORE_MEDIA_CHANGE |
333 SCSI_SILENT);
334 if (error)
335 goto bad3;
336 }
337
338 sc_link->flags |= SDEV_OPEN;
339
340 /* Lock the pack in. */
341 error = scsipi_prevent(sc_link, PR_PREVENT,
342 SCSI_IGNORE_ILLEGAL_REQUEST |
343 SCSI_IGNORE_MEDIA_CHANGE);
344 if (error)
345 goto bad;
346
347 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
348 sc_link->flags |= SDEV_MEDIA_LOADED;
349
350 /* Load the physical device parameters. */
351 if (sd_get_parms(sd, 0) != 0) {
352 error = ENXIO;
353 goto bad2;
354 }
355 SC_DEBUG(sc_link, SDEV_DB3, ("Params loaded "));
356
357 /* Load the partition info if not already loaded. */
358 sdgetdisklabel(sd);
359 SC_DEBUG(sc_link, SDEV_DB3, ("Disklabel loaded "));
360 }
361 }
362
363 part = SDPART(dev);
364
365 /* Check that the partition exists. */
366 if (part != RAW_PART &&
367 (part >= sd->sc_dk.dk_label->d_npartitions ||
368 sd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
369 error = ENXIO;
370 goto bad;
371 }
372
373 /* Insure only one open at a time. */
374 switch (fmt) {
375 case S_IFCHR:
376 sd->sc_dk.dk_copenmask |= (1 << part);
377 break;
378 case S_IFBLK:
379 sd->sc_dk.dk_bopenmask |= (1 << part);
380 break;
381 }
382 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
383
384 SC_DEBUG(sc_link, SDEV_DB3, ("open complete\n"));
385 sdunlock(sd);
386 return 0;
387
388 bad2:
389 sc_link->flags &= ~SDEV_MEDIA_LOADED;
390
391 bad:
392 if (sd->sc_dk.dk_openmask == 0) {
393 scsipi_prevent(sc_link, PR_ALLOW,
394 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_MEDIA_CHANGE);
395 sc_link->flags &= ~SDEV_OPEN;
396 }
397
398 bad3:
399 sdunlock(sd);
400 return error;
401 }
402
403 /*
404 * close the device.. only called if we are the LAST occurence of an open
405 * device. Convenient now but usually a pain.
406 */
407 int
408 sdclose(dev, flag, fmt, p)
409 dev_t dev;
410 int flag, fmt;
411 struct proc *p;
412 {
413 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
414 int part = SDPART(dev);
415 int error;
416
417 if ((error = sdlock(sd)) != 0)
418 return error;
419
420 switch (fmt) {
421 case S_IFCHR:
422 sd->sc_dk.dk_copenmask &= ~(1 << part);
423 break;
424 case S_IFBLK:
425 sd->sc_dk.dk_bopenmask &= ~(1 << part);
426 break;
427 }
428 sd->sc_dk.dk_openmask = sd->sc_dk.dk_copenmask | sd->sc_dk.dk_bopenmask;
429
430 if (sd->sc_dk.dk_openmask == 0) {
431 /* XXXX Must wait for I/O to complete! */
432
433 scsipi_prevent(sd->sc_link, PR_ALLOW,
434 SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
435 sd->sc_link->flags &= ~(SDEV_OPEN|SDEV_MEDIA_LOADED);
436 }
437
438 sdunlock(sd);
439 return 0;
440 }
441
442 /*
443 * Actually translate the requested transfer into one the physical driver
444 * can understand. The transfer is described by a buf and will include
445 * only one physical transfer.
446 */
447 void
448 sdstrategy(bp)
449 struct buf *bp;
450 {
451 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
452 int s;
453
454 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdstrategy "));
455 SC_DEBUG(sd->sc_link, SDEV_DB1,
456 ("%ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
457 /*
458 * The transfer must be a whole number of blocks.
459 */
460 if ((bp->b_bcount % sd->sc_dk.dk_label->d_secsize) != 0) {
461 bp->b_error = EINVAL;
462 goto bad;
463 }
464 /*
465 * If the device has been made invalid, error out
466 */
467 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
468 bp->b_error = EIO;
469 goto bad;
470 }
471 /*
472 * If it's a null transfer, return immediatly
473 */
474 if (bp->b_bcount == 0)
475 goto done;
476
477 /*
478 * Do bounds checking, adjust transfer. if error, process.
479 * If end of partition, just return.
480 */
481 if (SDPART(bp->b_dev) != RAW_PART &&
482 bounds_check_with_label(bp, sd->sc_dk.dk_label,
483 (sd->flags & (SDF_WLABEL|SDF_LABELLING)) != 0) <= 0)
484 goto done;
485
486 s = splbio();
487
488 /*
489 * Place it in the queue of disk activities for this disk
490 */
491 disksort(&sd->buf_queue, bp);
492
493 /*
494 * Tell the device to get going on the transfer if it's
495 * not doing anything, otherwise just wait for completion
496 */
497 sdstart(sd);
498
499 splx(s);
500 return;
501
502 bad:
503 bp->b_flags |= B_ERROR;
504 done:
505 /*
506 * Correctly set the buf to indicate a completed xfer
507 */
508 bp->b_resid = bp->b_bcount;
509 biodone(bp);
510 }
511
512 /*
513 * sdstart looks to see if there is a buf waiting for the device
514 * and that the device is not already busy. If both are true,
515 * It dequeues the buf and creates a scsi command to perform the
516 * transfer in the buf. The transfer request will call scsipi_done
517 * on completion, which will in turn call this routine again
518 * so that the next queued transfer is performed.
519 * The bufs are queued by the strategy routine (sdstrategy)
520 *
521 * This routine is also called after other non-queued requests
522 * have been made of the scsi driver, to ensure that the queue
523 * continues to be drained.
524 *
525 * must be called at the correct (highish) spl level
526 * sdstart() is called at splbio from sdstrategy and scsipi_done
527 */
528 void
529 sdstart(v)
530 register void *v;
531 {
532 register struct sd_softc *sd = v;
533 register struct scsipi_link *sc_link = sd->sc_link;
534 struct buf *bp = 0;
535 struct buf *dp;
536 struct scsipi_rw_big cmd_big;
537 struct scsi_rw cmd_small;
538 struct scsipi_generic *cmdp;
539 int blkno, nblks, cmdlen, error;
540 struct partition *p;
541
542 SC_DEBUG(sc_link, SDEV_DB2, ("sdstart "));
543 /*
544 * Check if the device has room for another command
545 */
546 while (sc_link->openings > 0) {
547 /*
548 * there is excess capacity, but a special waits
549 * It'll need the adapter as soon as we clear out of the
550 * way and let it run (user level wait).
551 */
552 if (sc_link->flags & SDEV_WAITING) {
553 sc_link->flags &= ~SDEV_WAITING;
554 wakeup((caddr_t)sc_link);
555 return;
556 }
557
558 /*
559 * See if there is a buf with work for us to do..
560 */
561 dp = &sd->buf_queue;
562 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
563 return;
564 dp->b_actf = bp->b_actf;
565
566 /*
567 * If the device has become invalid, abort all the
568 * reads and writes until all files have been closed and
569 * re-opened
570 */
571 if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0) {
572 bp->b_error = EIO;
573 bp->b_flags |= B_ERROR;
574 bp->b_resid = bp->b_bcount;
575 biodone(bp);
576 continue;
577 }
578
579 /*
580 * We have a buf, now we should make a command
581 *
582 * First, translate the block to absolute and put it in terms
583 * of the logical blocksize of the device.
584 */
585 blkno =
586 bp->b_blkno / (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
587 if (SDPART(bp->b_dev) != RAW_PART) {
588 p = &sd->sc_dk.dk_label->d_partitions[SDPART(bp->b_dev)];
589 blkno += p->p_offset;
590 }
591 nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize);
592
593 /*
594 * Fill out the scsi command. If the transfer will
595 * fit in a "small" cdb, use it.
596 */
597 if (((blkno & 0x1fffff) == blkno) &&
598 ((nblks & 0xff) == nblks)) {
599 /*
600 * We can fit in a small cdb.
601 */
602 bzero(&cmd_small, sizeof(cmd_small));
603 cmd_small.opcode = (bp->b_flags & B_READ) ?
604 SCSI_READ_COMMAND : SCSI_WRITE_COMMAND;
605 _lto3b(blkno, cmd_small.addr);
606 cmd_small.length = nblks & 0xff;
607 cmdlen = sizeof(cmd_small);
608 cmdp = (struct scsipi_generic *)&cmd_small;
609 } else {
610 /*
611 * Need a large cdb.
612 */
613 bzero(&cmd_big, sizeof(cmd_big));
614 cmd_big.opcode = (bp->b_flags & B_READ) ?
615 READ_BIG : WRITE_BIG;
616 _lto4b(blkno, cmd_big.addr);
617 _lto2b(nblks, cmd_big.length);
618 cmdlen = sizeof(cmd_big);
619 cmdp = (struct scsipi_generic *)&cmd_big;
620 }
621
622 /* Instrumentation. */
623 disk_busy(&sd->sc_dk);
624
625 /*
626 * Call the routine that chats with the adapter.
627 * Note: we cannot sleep as we may be an interrupt
628 */
629 error = sc_link->scsipi_cmd(sc_link, cmdp, cmdlen,
630 (u_char *)bp->b_data, bp->b_bcount,
631 SDRETRIES, 60000, bp, SCSI_NOSLEEP |
632 ((bp->b_flags & B_READ) ? SCSI_DATA_IN : SCSI_DATA_OUT));
633 if (error)
634 printf("%s: not queued, error %d\n",
635 sd->sc_dev.dv_xname, error);
636 }
637 }
638
639 void
640 sddone(xs)
641 struct scsipi_xfer *xs;
642 {
643 struct sd_softc *sd = xs->sc_link->device_softc;
644
645 if (xs->bp != NULL)
646 disk_unbusy(&sd->sc_dk, xs->bp->b_bcount - xs->bp->b_resid);
647 }
648
649 void
650 sdminphys(bp)
651 struct buf *bp;
652 {
653 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(bp->b_dev)];
654 long max;
655
656 /*
657 * If the device is ancient, we want to make sure that
658 * the transfer fits into a 6-byte cdb.
659 *
660 * XXX Note that the SCSI-I spec says that 256-block transfers
661 * are allowed in a 6-byte read/write, and are specified
662 * by settng the "length" to 0. However, we're conservative
663 * here, allowing only 255-block transfers in case an
664 * ancient device gets confused by length == 0. A length of 0
665 * in a 10-byte read/write actually means 0 blocks.
666 */
667 if (sd->flags & SDF_ANCIENT) {
668 max = sd->sc_dk.dk_label->d_secsize * 0xff;
669
670 if (bp->b_bcount > max)
671 bp->b_bcount = max;
672 }
673
674 (*sd->sc_link->adapter->scsipi_minphys)(bp);
675 }
676
677 int
678 sdread(dev, uio, ioflag)
679 dev_t dev;
680 struct uio *uio;
681 int ioflag;
682 {
683
684 return (physio(sdstrategy, NULL, dev, B_READ, sdminphys, uio));
685 }
686
687 int
688 sdwrite(dev, uio, ioflag)
689 dev_t dev;
690 struct uio *uio;
691 int ioflag;
692 {
693
694 return (physio(sdstrategy, NULL, dev, B_WRITE, sdminphys, uio));
695 }
696
697 /*
698 * Perform special action on behalf of the user
699 * Knows about the internals of this device
700 */
701 int
702 sdioctl(dev, cmd, addr, flag, p)
703 dev_t dev;
704 u_long cmd;
705 caddr_t addr;
706 int flag;
707 struct proc *p;
708 {
709 struct sd_softc *sd = sd_cd.cd_devs[SDUNIT(dev)];
710 int error;
711
712 SC_DEBUG(sd->sc_link, SDEV_DB2, ("sdioctl 0x%lx ", cmd));
713
714 /*
715 * If the device is not valid.. abandon ship
716 */
717 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) == 0)
718 return EIO;
719
720 switch (cmd) {
721 case DIOCGDINFO:
722 *(struct disklabel *)addr = *(sd->sc_dk.dk_label);
723 return 0;
724
725 case DIOCGPART:
726 ((struct partinfo *)addr)->disklab = sd->sc_dk.dk_label;
727 ((struct partinfo *)addr)->part =
728 &sd->sc_dk.dk_label->d_partitions[SDPART(dev)];
729 return 0;
730
731 case DIOCWDINFO:
732 case DIOCSDINFO:
733 if ((flag & FWRITE) == 0)
734 return EBADF;
735
736 if ((error = sdlock(sd)) != 0)
737 return error;
738 sd->flags |= SDF_LABELLING;
739
740 error = setdisklabel(sd->sc_dk.dk_label,
741 (struct disklabel *)addr, /*sd->sc_dk.dk_openmask : */0,
742 sd->sc_dk.dk_cpulabel);
743 if (error == 0) {
744 if (cmd == DIOCWDINFO)
745 error = writedisklabel(SDLABELDEV(dev),
746 sdstrategy, sd->sc_dk.dk_label,
747 sd->sc_dk.dk_cpulabel);
748 }
749
750 sd->flags &= ~SDF_LABELLING;
751 sdunlock(sd);
752 return error;
753
754 case DIOCWLABEL:
755 if ((flag & FWRITE) == 0)
756 return EBADF;
757 if (*(int *)addr)
758 sd->flags |= SDF_WLABEL;
759 else
760 sd->flags &= ~SDF_WLABEL;
761 return 0;
762
763 case DIOCLOCK:
764 return scsipi_prevent(sd->sc_link,
765 (*(int *)addr) ? PR_PREVENT : PR_ALLOW, 0);
766
767 case DIOCEJECT:
768 return ((sd->sc_link->flags & SDEV_REMOVABLE) == 0 ? ENOTTY :
769 scsipi_start(sd->sc_link, SSS_STOP|SSS_LOEJ, 0));
770
771 default:
772 if (SDPART(dev) != RAW_PART)
773 return ENOTTY;
774 return scsipi_do_ioctl(sd->sc_link, dev, cmd, addr, flag, p);
775 }
776
777 #ifdef DIAGNOSTIC
778 panic("sdioctl: impossible");
779 #endif
780 }
781
782 /*
783 * Load the label information on the named device
784 */
785 void
786 sdgetdisklabel(sd)
787 struct sd_softc *sd;
788 {
789 struct disklabel *lp = sd->sc_dk.dk_label;
790 char *errstring;
791
792 bzero(lp, sizeof(struct disklabel));
793 bzero(sd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
794
795 lp->d_secsize = sd->params.blksize;
796 lp->d_ntracks = sd->params.heads;
797 lp->d_nsectors = sd->params.sectors;
798 lp->d_ncylinders = sd->params.cyls;
799 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
800 if (lp->d_secpercyl == 0) {
801 lp->d_secpercyl = 100;
802 /* as long as it's not 0 - readdisklabel divides by it (?) */
803 }
804
805 if (sd->type == T_OPTICAL)
806 strncpy(lp->d_typename, "SCSI optical", 16);
807 else
808 strncpy(lp->d_typename, "SCSI disk", 16);
809 lp->d_type = DTYPE_SCSI;
810 strncpy(lp->d_packname, "fictitious", 16);
811 lp->d_secperunit = sd->params.disksize;
812 lp->d_rpm = 3600;
813 lp->d_interleave = 1;
814 lp->d_flags = 0;
815
816 lp->d_partitions[RAW_PART].p_offset = 0;
817 lp->d_partitions[RAW_PART].p_size =
818 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
819 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
820 lp->d_npartitions = RAW_PART + 1;
821
822 lp->d_magic = DISKMAGIC;
823 lp->d_magic2 = DISKMAGIC;
824 lp->d_checksum = dkcksum(lp);
825
826 /*
827 * Call the generic disklabel extraction routine
828 */
829 errstring = readdisklabel(MAKESDDEV(0, sd->sc_dev.dv_unit, RAW_PART),
830 sdstrategy, lp, sd->sc_dk.dk_cpulabel);
831 if (errstring) {
832 printf("%s: %s\n", sd->sc_dev.dv_xname, errstring);
833 return;
834 }
835 }
836
837 /*
838 * Tell the device to map out a defective block
839 */
840 int
841 sd_reassign_blocks(sd, blkno)
842 struct sd_softc *sd;
843 u_long blkno;
844 {
845 struct scsi_reassign_blocks scsipi_cmd;
846 struct scsi_reassign_blocks_data rbdata;
847
848 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
849 bzero(&rbdata, sizeof(rbdata));
850 scsipi_cmd.opcode = SCSI_REASSIGN_BLOCKS;
851
852 _lto2b(sizeof(rbdata.defect_descriptor[0]), rbdata.length);
853 _lto4b(blkno, rbdata.defect_descriptor[0].dlbaddr);
854
855 return sd->sc_link->scsipi_cmd(sd->sc_link,
856 (struct scsipi_generic *)&scsipi_cmd,
857 sizeof(scsipi_cmd), (u_char *)&rbdata, sizeof(rbdata), SDRETRIES,
858 5000, NULL, SCSI_DATA_OUT);
859 }
860
861
862
863 static int
864 sd_mode_sense(sd, scsipi_sense, page, flags)
865 struct sd_softc *sd;
866 struct scsi_mode_sense_data *scsipi_sense;
867 int page, flags;
868 {
869 struct scsi_mode_sense scsipi_cmd;
870
871 /*
872 * Make sure the sense buffer is clean before we do
873 * the mode sense, so that checks for bogus values of
874 * 0 will work in case the mode sense fails.
875 */
876 bzero(scsipi_sense, sizeof(*scsipi_sense));
877
878 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
879 scsipi_cmd.opcode = SCSI_MODE_SENSE;
880 scsipi_cmd.page = page;
881 scsipi_cmd.length = 0x20;
882 /*
883 * If the command worked, use the results to fill out
884 * the parameter structure
885 */
886 return sd->sc_link->scsipi_cmd(sd->sc_link,
887 (struct scsipi_generic *)&scsipi_cmd,
888 sizeof(scsipi_cmd), (u_char *)scsipi_sense, sizeof(*scsipi_sense),
889 SDRETRIES, 6000, NULL, flags | SCSI_DATA_IN | SCSI_SILENT);
890 }
891
892 int
893 sd_get_optparms(sd, flags, dp)
894 struct sd_softc *sd;
895 int flags;
896 struct disk_parms *dp;
897 {
898 struct scsi_mode_sense scsipi_cmd;
899 struct scsi_mode_sense_data {
900 struct scsi_mode_header header;
901 struct scsi_blk_desc blk_desc;
902 union scsi_disk_pages pages;
903 } scsipi_sense;
904 u_long sectors;
905 int error;
906
907 dp->blksize = 512;
908 if ((sectors = scsipi_size(sd->sc_link, flags)) == 0)
909 return 1;
910
911 /* XXX
912 * It is better to get the following params from the
913 * mode sense page 6 only (optical device parameter page).
914 * However, there are stupid optical devices which does NOT
915 * support the page 6. Ghaa....
916 */
917 bzero(&scsipi_cmd, sizeof(scsipi_cmd));
918 scsipi_cmd.opcode = SCSI_MODE_SENSE;
919 scsipi_cmd.page = 0x3f; /* all pages */
920 scsipi_cmd.length = sizeof(struct scsi_mode_header) +
921 sizeof(struct scsi_blk_desc);
922
923 if ((error = sd->sc_link->scsipi_cmd(sd->sc_link,
924 (struct scsipi_generic *)&scsipi_cmd, sizeof(scsipi_cmd),
925 (u_char *)&scsipi_sense, sizeof(scsipi_sense), SDRETRIES,
926 6000, NULL, flags | SCSI_DATA_IN)) != 0)
927 return error;
928
929 dp->blksize = _3btol(scsipi_sense.blk_desc.blklen);
930 if (dp->blksize == 0)
931 dp->blksize = 512;
932
933 /*
934 * Create a pseudo-geometry.
935 */
936 dp->heads = 64;
937 dp->sectors = 32;
938 dp->cyls = sectors / (dp->heads * dp->sectors);
939 dp->disksize = sectors;
940
941 return 0;
942 }
943
944 /*
945 * Get the scsi driver to send a full inquiry to the * device and use the
946 * results to fill out the disk parameter structure.
947 */
948 int
949 sd_get_parms(sd, flags)
950 struct sd_softc *sd;
951 int flags;
952 {
953 struct disk_parms *dp = &sd->params;
954 struct scsi_mode_sense_data scsipi_sense;
955 u_long sectors;
956 int page;
957 int error;
958
959 if (sd->type == T_OPTICAL) {
960 if ((error = sd_get_optparms(sd, flags, dp)) != 0)
961 sd->sc_link->flags &= ~SDEV_MEDIA_LOADED;
962 return error;
963 }
964
965 if ((error = sd_mode_sense(sd, &scsipi_sense, page = 4, flags)) == 0) {
966 SC_DEBUG(sd->sc_link, SDEV_DB3,
967 ("%d cyls, %d heads, %d precomp, %d red_write, %d land_zone\n",
968 _3btol(scsipi_sense.pages.rigid_geometry.ncyl),
969 scsipi_sense.pages.rigid_geometry.nheads,
970 _2btol(scsipi_sense.pages.rigid_geometry.st_cyl_wp),
971 _2btol(scsipi_sense.pages.rigid_geometry.st_cyl_rwc),
972 _2btol(scsipi_sense.pages.rigid_geometry.land_zone)));
973
974 /*
975 * KLUDGE!! (for zone recorded disks)
976 * give a number of sectors so that sec * trks * cyls
977 * is <= disk_size
978 * can lead to wasted space! THINK ABOUT THIS !
979 */
980 dp->heads = scsipi_sense.pages.rigid_geometry.nheads;
981 dp->cyls = _3btol(scsipi_sense.pages.rigid_geometry.ncyl);
982 dp->blksize = _3btol(scsipi_sense.blk_desc.blklen);
983
984 if (dp->heads == 0 || dp->cyls == 0)
985 goto fake_it;
986
987 if (dp->blksize == 0)
988 dp->blksize = 512;
989
990 sectors = scsipi_size(sd->sc_link, flags);
991 dp->disksize = sectors;
992 sectors /= (dp->heads * dp->cyls);
993 dp->sectors = sectors; /* XXX dubious on SCSI */
994
995 return 0;
996 }
997
998 if ((error = sd_mode_sense(sd, &scsipi_sense, page = 5, flags)) == 0) {
999 dp->heads = scsipi_sense.pages.flex_geometry.nheads;
1000 dp->cyls = _2btol(scsipi_sense.pages.flex_geometry.ncyl);
1001 dp->blksize = _3btol(scsipi_sense.blk_desc.blklen);
1002 dp->sectors = scsipi_sense.pages.flex_geometry.ph_sec_tr;
1003 dp->disksize = dp->heads * dp->cyls * dp->sectors;
1004 if (dp->disksize == 0)
1005 goto fake_it;
1006
1007 if (dp->blksize == 0)
1008 dp->blksize = 512;
1009
1010 return 0;
1011 }
1012
1013 fake_it:
1014 if ((sd->sc_link->quirks & SDEV_NOMODESENSE) == 0) {
1015 if (error == 0)
1016 printf("%s: mode sense (%d) returned nonsense",
1017 sd->sc_dev.dv_xname, page);
1018 else
1019 printf("%s: could not mode sense (4/5)",
1020 sd->sc_dev.dv_xname);
1021 printf("; using fictitious geometry\n");
1022 }
1023 /*
1024 * use adaptec standard fictitious geometry
1025 * this depends on which controller (e.g. 1542C is
1026 * different. but we have to put SOMETHING here..)
1027 */
1028 sectors = scsipi_size(sd->sc_link, flags);
1029 dp->heads = 64;
1030 dp->sectors = 32;
1031 dp->cyls = sectors / (64 * 32);
1032 dp->blksize = 512;
1033 dp->disksize = sectors;
1034 return 0;
1035 }
1036
1037 int
1038 sdsize(dev)
1039 dev_t dev;
1040 {
1041 struct sd_softc *sd;
1042 int part, unit, omask;
1043 int size;
1044
1045 unit = SDUNIT(dev);
1046 if (unit >= sd_cd.cd_ndevs)
1047 return (-1);
1048 sd = sd_cd.cd_devs[unit];
1049 if (sd == NULL)
1050 return (-1);
1051
1052 part = SDPART(dev);
1053 omask = sd->sc_dk.dk_openmask & (1 << part);
1054
1055 if (omask == 0 && sdopen(dev, 0, S_IFBLK, NULL) != 0)
1056 return (-1);
1057 if (sd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1058 size = -1;
1059 else
1060 size = sd->sc_dk.dk_label->d_partitions[part].p_size *
1061 (sd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1062 if (omask == 0 && sdclose(dev, 0, S_IFBLK, NULL) != 0)
1063 return (-1);
1064 return (size);
1065 }
1066
1067 #ifndef __BDEVSW_DUMP_OLD_TYPE
1068 /* #define SD_DUMP_NOT_TRUSTED if you just want to watch */
1069 static struct scsipi_xfer sx;
1070 static int sddoingadump;
1071
1072 /*
1073 * dump all of physical memory into the partition specified, starting
1074 * at offset 'dumplo' into the partition.
1075 */
1076 int
1077 sddump(dev, blkno, va, size)
1078 dev_t dev;
1079 daddr_t blkno;
1080 caddr_t va;
1081 size_t size;
1082 {
1083 struct sd_softc *sd; /* disk unit to do the I/O */
1084 struct disklabel *lp; /* disk's disklabel */
1085 int unit, part;
1086 int sectorsize; /* size of a disk sector */
1087 int nsects; /* number of sectors in partition */
1088 int sectoff; /* sector offset of partition */
1089 int totwrt; /* total number of sectors left to write */
1090 int nwrt; /* current number of sectors to write */
1091 struct scsipi_rw_big cmd; /* write command */
1092 struct scsipi_xfer *xs; /* ... convenience */
1093 int retval;
1094
1095 /* Check if recursive dump; if so, punt. */
1096 if (sddoingadump)
1097 return EFAULT;
1098
1099 /* Mark as active early. */
1100 sddoingadump = 1;
1101
1102 unit = SDUNIT(dev); /* Decompose unit & partition. */
1103 part = SDPART(dev);
1104
1105 /* Check for acceptable drive number. */
1106 if (unit >= sd_cd.cd_ndevs || (sd = sd_cd.cd_devs[unit]) == NULL)
1107 return ENXIO;
1108
1109 /*
1110 * XXX Can't do this check, since the media might have been
1111 * XXX marked `invalid' by successful unmounting of all
1112 * XXX filesystems.
1113 */
1114 #if 0
1115 /* Make sure it was initialized. */
1116 if ((sd->sc_link->flags & SDEV_MEDIA_LOADED) != SDEV_MEDIA_LOADED)
1117 return ENXIO;
1118 #endif
1119
1120 /* Convert to disk sectors. Request must be a multiple of size. */
1121 lp = sd->sc_dk.dk_label;
1122 sectorsize = lp->d_secsize;
1123 if ((size % sectorsize) != 0)
1124 return EFAULT;
1125 totwrt = size / sectorsize;
1126 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1127
1128 nsects = lp->d_partitions[part].p_size;
1129 sectoff = lp->d_partitions[part].p_offset;
1130
1131 /* Check transfer bounds against partition size. */
1132 if ((blkno < 0) || ((blkno + totwrt) > nsects))
1133 return EINVAL;
1134
1135 /* Offset block number to start of partition. */
1136 blkno += sectoff;
1137
1138 xs = &sx;
1139
1140 while (totwrt > 0) {
1141 nwrt = totwrt; /* XXX */
1142 #ifndef SD_DUMP_NOT_TRUSTED
1143 /*
1144 * Fill out the scsi command
1145 */
1146 bzero(&cmd, sizeof(cmd));
1147 cmd.opcode = WRITE_BIG;
1148 _lto4b(blkno, cmd.addr);
1149 _lto2b(nwrt, cmd.length);
1150 /*
1151 * Fill out the scsipi_xfer structure
1152 * Note: we cannot sleep as we may be an interrupt
1153 * don't use sc_link->scsipi_cmd() as it may want
1154 * to wait for an xs.
1155 */
1156 bzero(xs, sizeof(sx));
1157 xs->flags |= SCSI_AUTOCONF | INUSE | SCSI_DATA_OUT;
1158 xs->sc_link = sd->sc_link;
1159 xs->retries = SDRETRIES;
1160 xs->timeout = 10000; /* 10000 millisecs for a disk ! */
1161 xs->cmd = (struct scsipi_generic *)&cmd;
1162 xs->cmdlen = sizeof(cmd);
1163 xs->resid = nwrt * sectorsize;
1164 xs->error = XS_NOERROR;
1165 xs->bp = 0;
1166 xs->data = va;
1167 xs->datalen = nwrt * sectorsize;
1168
1169 /*
1170 * Pass all this info to the scsi driver.
1171 */
1172 retval = (*(sd->sc_link->adapter->scsipi_cmd)) (xs);
1173 if (retval != COMPLETE)
1174 return ENXIO;
1175 #else /* SD_DUMP_NOT_TRUSTED */
1176 /* Let's just talk about this first... */
1177 printf("sd%d: dump addr 0x%x, blk %d\n", unit, va, blkno);
1178 delay(500 * 1000); /* half a second */
1179 #endif /* SD_DUMP_NOT_TRUSTED */
1180
1181 /* update block count */
1182 totwrt -= nwrt;
1183 blkno += nwrt;
1184 va += sectorsize * nwrt;
1185 }
1186 sddoingadump = 0;
1187 return 0;
1188 }
1189 #else /* __BDEVSW_DUMP_NEW_TYPE */
1190 int
1191 sddump(dev, blkno, va, size)
1192 dev_t dev;
1193 daddr_t blkno;
1194 caddr_t va;
1195 size_t size;
1196 {
1197
1198 /* Not implemented. */
1199 return ENXIO;
1200 }
1201 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1202