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