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