aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libata-scsi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libata-scsi.c')
-rw-r--r--drivers/scsi/libata-scsi.c386
1 files changed, 262 insertions, 124 deletions
diff --git a/drivers/scsi/libata-scsi.c b/drivers/scsi/libata-scsi.c
index 8295a656e521..45ebe9fd52ea 100644
--- a/drivers/scsi/libata-scsi.c
+++ b/drivers/scsi/libata-scsi.c
@@ -435,10 +435,21 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
435 return 1; /* power conditions not supported */ 435 return 1; /* power conditions not supported */
436 if (scsicmd[4] & 0x1) { 436 if (scsicmd[4] & 0x1) {
437 tf->nsect = 1; /* 1 sector, lba=0 */ 437 tf->nsect = 1; /* 1 sector, lba=0 */
438 tf->lbah = 0x0; 438
439 tf->lbam = 0x0; 439 if (qc->dev->flags & ATA_DFLAG_LBA) {
440 tf->lbal = 0x0; 440 qc->tf.flags |= ATA_TFLAG_LBA;
441 tf->device |= ATA_LBA; 441
442 tf->lbah = 0x0;
443 tf->lbam = 0x0;
444 tf->lbal = 0x0;
445 tf->device |= ATA_LBA;
446 } else {
447 /* CHS */
448 tf->lbal = 0x1; /* sect */
449 tf->lbam = 0x0; /* cyl low */
450 tf->lbah = 0x0; /* cyl high */
451 }
452
442 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ 453 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
443 } else { 454 } else {
444 tf->nsect = 0; /* time period value (0 implies now) */ 455 tf->nsect = 0; /* time period value (0 implies now) */
@@ -488,6 +499,99 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
488} 499}
489 500
490/** 501/**
502 * scsi_6_lba_len - Get LBA and transfer length
503 * @scsicmd: SCSI command to translate
504 *
505 * Calculate LBA and transfer length for 6-byte commands.
506 *
507 * RETURNS:
508 * @plba: the LBA
509 * @plen: the transfer length
510 */
511
512static void scsi_6_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
513{
514 u64 lba = 0;
515 u32 len = 0;
516
517 VPRINTK("six-byte command\n");
518
519 lba |= ((u64)scsicmd[2]) << 8;
520 lba |= ((u64)scsicmd[3]);
521
522 len |= ((u32)scsicmd[4]);
523
524 *plba = lba;
525 *plen = len;
526}
527
528/**
529 * scsi_10_lba_len - Get LBA and transfer length
530 * @scsicmd: SCSI command to translate
531 *
532 * Calculate LBA and transfer length for 10-byte commands.
533 *
534 * RETURNS:
535 * @plba: the LBA
536 * @plen: the transfer length
537 */
538
539static void scsi_10_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
540{
541 u64 lba = 0;
542 u32 len = 0;
543
544 VPRINTK("ten-byte command\n");
545
546 lba |= ((u64)scsicmd[2]) << 24;
547 lba |= ((u64)scsicmd[3]) << 16;
548 lba |= ((u64)scsicmd[4]) << 8;
549 lba |= ((u64)scsicmd[5]);
550
551 len |= ((u32)scsicmd[7]) << 8;
552 len |= ((u32)scsicmd[8]);
553
554 *plba = lba;
555 *plen = len;
556}
557
558/**
559 * scsi_16_lba_len - Get LBA and transfer length
560 * @scsicmd: SCSI command to translate
561 *
562 * Calculate LBA and transfer length for 16-byte commands.
563 *
564 * RETURNS:
565 * @plba: the LBA
566 * @plen: the transfer length
567 */
568
569static void scsi_16_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
570{
571 u64 lba = 0;
572 u32 len = 0;
573
574 VPRINTK("sixteen-byte command\n");
575
576 lba |= ((u64)scsicmd[2]) << 56;
577 lba |= ((u64)scsicmd[3]) << 48;
578 lba |= ((u64)scsicmd[4]) << 40;
579 lba |= ((u64)scsicmd[5]) << 32;
580 lba |= ((u64)scsicmd[6]) << 24;
581 lba |= ((u64)scsicmd[7]) << 16;
582 lba |= ((u64)scsicmd[8]) << 8;
583 lba |= ((u64)scsicmd[9]);
584
585 len |= ((u32)scsicmd[10]) << 24;
586 len |= ((u32)scsicmd[11]) << 16;
587 len |= ((u32)scsicmd[12]) << 8;
588 len |= ((u32)scsicmd[13]);
589
590 *plba = lba;
591 *plen = len;
592}
593
594/**
491 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one 595 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
492 * @qc: Storage for translated ATA taskfile 596 * @qc: Storage for translated ATA taskfile
493 * @scsicmd: SCSI command to translate 597 * @scsicmd: SCSI command to translate
@@ -504,77 +608,86 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
504static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 608static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
505{ 609{
506 struct ata_taskfile *tf = &qc->tf; 610 struct ata_taskfile *tf = &qc->tf;
611 struct ata_device *dev = qc->dev;
612 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
507 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 613 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
508 u64 dev_sectors = qc->dev->n_sectors; 614 u64 dev_sectors = qc->dev->n_sectors;
509 u64 sect = 0; 615 u64 block;
510 u32 n_sect = 0; 616 u32 n_block;
511 617
512 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 618 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
513 tf->protocol = ATA_PROT_NODATA; 619 tf->protocol = ATA_PROT_NODATA;
514 tf->device |= ATA_LBA;
515
516 if (scsicmd[0] == VERIFY) {
517 sect |= ((u64)scsicmd[2]) << 24;
518 sect |= ((u64)scsicmd[3]) << 16;
519 sect |= ((u64)scsicmd[4]) << 8;
520 sect |= ((u64)scsicmd[5]);
521
522 n_sect |= ((u32)scsicmd[7]) << 8;
523 n_sect |= ((u32)scsicmd[8]);
524 }
525
526 else if (scsicmd[0] == VERIFY_16) {
527 sect |= ((u64)scsicmd[2]) << 56;
528 sect |= ((u64)scsicmd[3]) << 48;
529 sect |= ((u64)scsicmd[4]) << 40;
530 sect |= ((u64)scsicmd[5]) << 32;
531 sect |= ((u64)scsicmd[6]) << 24;
532 sect |= ((u64)scsicmd[7]) << 16;
533 sect |= ((u64)scsicmd[8]) << 8;
534 sect |= ((u64)scsicmd[9]);
535
536 n_sect |= ((u32)scsicmd[10]) << 24;
537 n_sect |= ((u32)scsicmd[11]) << 16;
538 n_sect |= ((u32)scsicmd[12]) << 8;
539 n_sect |= ((u32)scsicmd[13]);
540 }
541 620
621 if (scsicmd[0] == VERIFY)
622 scsi_10_lba_len(scsicmd, &block, &n_block);
623 else if (scsicmd[0] == VERIFY_16)
624 scsi_16_lba_len(scsicmd, &block, &n_block);
542 else 625 else
543 return 1; 626 return 1;
544 627
545 if (!n_sect) 628 if (!n_block)
546 return 1; 629 return 1;
547 if (sect >= dev_sectors) 630 if (block >= dev_sectors)
548 return 1; 631 return 1;
549 if ((sect + n_sect) > dev_sectors) 632 if ((block + n_block) > dev_sectors)
550 return 1; 633 return 1;
551 if (lba48) { 634 if (lba48) {
552 if (n_sect > (64 * 1024)) 635 if (n_block > (64 * 1024))
553 return 1; 636 return 1;
554 } else { 637 } else {
555 if (n_sect > 256) 638 if (n_block > 256)
556 return 1; 639 return 1;
557 } 640 }
558 641
559 if (lba48) { 642 if (lba) {
560 tf->command = ATA_CMD_VERIFY_EXT; 643 if (lba48) {
644 tf->command = ATA_CMD_VERIFY_EXT;
561 645
562 tf->hob_nsect = (n_sect >> 8) & 0xff; 646 tf->hob_nsect = (n_block >> 8) & 0xff;
563 647
564 tf->hob_lbah = (sect >> 40) & 0xff; 648 tf->hob_lbah = (block >> 40) & 0xff;
565 tf->hob_lbam = (sect >> 32) & 0xff; 649 tf->hob_lbam = (block >> 32) & 0xff;
566 tf->hob_lbal = (sect >> 24) & 0xff; 650 tf->hob_lbal = (block >> 24) & 0xff;
567 } else { 651 } else {
568 tf->command = ATA_CMD_VERIFY; 652 tf->command = ATA_CMD_VERIFY;
569 653
570 tf->device |= (sect >> 24) & 0xf; 654 tf->device |= (block >> 24) & 0xf;
571 } 655 }
572 656
573 tf->nsect = n_sect & 0xff; 657 tf->nsect = n_block & 0xff;
574 658
575 tf->lbah = (sect >> 16) & 0xff; 659 tf->lbah = (block >> 16) & 0xff;
576 tf->lbam = (sect >> 8) & 0xff; 660 tf->lbam = (block >> 8) & 0xff;
577 tf->lbal = sect & 0xff; 661 tf->lbal = block & 0xff;
662
663 tf->device |= ATA_LBA;
664 } else {
665 /* CHS */
666 u32 sect, head, cyl, track;
667
668 /* Convert LBA to CHS */
669 track = (u32)block / dev->sectors;
670 cyl = track / dev->heads;
671 head = track % dev->heads;
672 sect = (u32)block % dev->sectors + 1;
673
674 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
675 (u32)block, track, cyl, head, sect);
676
677 /* Check whether the converted CHS can fit.
678 Cylinder: 0-65535
679 Head: 0-15
680 Sector: 1-255*/
681 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
682 return 1;
683
684 tf->command = ATA_CMD_VERIFY;
685 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
686 tf->lbal = sect;
687 tf->lbam = cyl;
688 tf->lbah = cyl >> 8;
689 tf->device |= head;
690 }
578 691
579 return 0; 692 return 0;
580} 693}
@@ -602,11 +715,14 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
602static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 715static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
603{ 716{
604 struct ata_taskfile *tf = &qc->tf; 717 struct ata_taskfile *tf = &qc->tf;
718 struct ata_device *dev = qc->dev;
719 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
605 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 720 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
721 u64 block;
722 u32 n_block;
606 723
607 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 724 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
608 tf->protocol = qc->dev->xfer_protocol; 725 tf->protocol = qc->dev->xfer_protocol;
609 tf->device |= ATA_LBA;
610 726
611 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 || 727 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
612 scsicmd[0] == READ_16) { 728 scsicmd[0] == READ_16) {
@@ -616,90 +732,102 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
616 tf->flags |= ATA_TFLAG_WRITE; 732 tf->flags |= ATA_TFLAG_WRITE;
617 } 733 }
618 734
619 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) { 735 /* Calculate the SCSI LBA and transfer length. */
620 if (lba48) { 736 switch (scsicmd[0]) {
621 tf->hob_nsect = scsicmd[7]; 737 case READ_10:
622 tf->hob_lbal = scsicmd[2]; 738 case WRITE_10:
623 739 scsi_10_lba_len(scsicmd, &block, &n_block);
624 qc->nsect = ((unsigned int)scsicmd[7] << 8) | 740 break;
625 scsicmd[8]; 741 case READ_6:
626 } else { 742 case WRITE_6:
627 /* if we don't support LBA48 addressing, the request 743 scsi_6_lba_len(scsicmd, &block, &n_block);
628 * -may- be too large. */
629 if ((scsicmd[2] & 0xf0) || scsicmd[7])
630 return 1;
631 744
632 /* stores LBA27:24 in lower 4 bits of device reg */ 745 /* for 6-byte r/w commands, transfer length 0
633 tf->device |= scsicmd[2]; 746 * means 256 blocks of data, not 0 block.
747 */
748 if (!n_block)
749 n_block = 256;
750 break;
751 case READ_16:
752 case WRITE_16:
753 scsi_16_lba_len(scsicmd, &block, &n_block);
754 break;
755 default:
756 DPRINTK("no-byte command\n");
757 return 1;
758 }
634 759
635 qc->nsect = scsicmd[8]; 760 /* Check and compose ATA command */
636 } 761 if (!n_block)
762 /* For 10-byte and 16-byte SCSI R/W commands, transfer
763 * length 0 means transfer 0 block of data.
764 * However, for ATA R/W commands, sector count 0 means
765 * 256 or 65536 sectors, not 0 sectors as in SCSI.
766 */
767 return 1;
637 768
638 tf->nsect = scsicmd[8]; 769 if (lba) {
639 tf->lbal = scsicmd[5]; 770 if (lba48) {
640 tf->lbam = scsicmd[4]; 771 /* The request -may- be too large for LBA48. */
641 tf->lbah = scsicmd[3]; 772 if ((block >> 48) || (n_block > 65536))
773 return 1;
642 774
643 VPRINTK("ten-byte command\n"); 775 tf->hob_nsect = (n_block >> 8) & 0xff;
644 if (qc->nsect == 0) /* we don't support length==0 cmds */
645 return 1;
646 return 0;
647 }
648 776
649 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) { 777 tf->hob_lbah = (block >> 40) & 0xff;
650 qc->nsect = tf->nsect = scsicmd[4]; 778 tf->hob_lbam = (block >> 32) & 0xff;
651 if (!qc->nsect) { 779 tf->hob_lbal = (block >> 24) & 0xff;
652 qc->nsect = 256; 780 } else {
653 if (lba48) 781 /* LBA28 */
654 tf->hob_nsect = 1;
655 }
656 782
657 tf->lbal = scsicmd[3]; 783 /* The request -may- be too large for LBA28. */
658 tf->lbam = scsicmd[2]; 784 if ((block >> 28) || (n_block > 256))
659 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */ 785 return 1;
660 786
661 VPRINTK("six-byte command\n"); 787 tf->device |= (block >> 24) & 0xf;
662 return 0; 788 }
663 }
664 789
665 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) { 790 qc->nsect = n_block;
666 /* rule out impossible LBAs and sector counts */ 791 tf->nsect = n_block & 0xff;
667 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
668 return 1;
669 792
670 if (lba48) { 793 tf->lbah = (block >> 16) & 0xff;
671 tf->hob_nsect = scsicmd[12]; 794 tf->lbam = (block >> 8) & 0xff;
672 tf->hob_lbal = scsicmd[6]; 795 tf->lbal = block & 0xff;
673 tf->hob_lbam = scsicmd[5];
674 tf->hob_lbah = scsicmd[4];
675 796
676 qc->nsect = ((unsigned int)scsicmd[12] << 8) | 797 tf->device |= ATA_LBA;
677 scsicmd[13]; 798 } else {
678 } else { 799 /* CHS */
679 /* once again, filter out impossible non-zero values */ 800 u32 sect, head, cyl, track;
680 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
681 (scsicmd[6] & 0xf0))
682 return 1;
683 801
684 /* stores LBA27:24 in lower 4 bits of device reg */ 802 /* The request -may- be too large for CHS addressing. */
685 tf->device |= scsicmd[6]; 803 if ((block >> 28) || (n_block > 256))
804 return 1;
686 805
687 qc->nsect = scsicmd[13]; 806 /* Convert LBA to CHS */
688 } 807 track = (u32)block / dev->sectors;
808 cyl = track / dev->heads;
809 head = track % dev->heads;
810 sect = (u32)block % dev->sectors + 1;
689 811
690 tf->nsect = scsicmd[13]; 812 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
691 tf->lbal = scsicmd[9]; 813 (u32)block, track, cyl, head, sect);
692 tf->lbam = scsicmd[8];
693 tf->lbah = scsicmd[7];
694 814
695 VPRINTK("sixteen-byte command\n"); 815 /* Check whether the converted CHS can fit.
696 if (qc->nsect == 0) /* we don't support length==0 cmds */ 816 Cylinder: 0-65535
817 Head: 0-15
818 Sector: 1-255*/
819 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
697 return 1; 820 return 1;
698 return 0; 821
822 qc->nsect = n_block;
823 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
824 tf->lbal = sect;
825 tf->lbam = cyl;
826 tf->lbah = cyl >> 8;
827 tf->device |= head;
699 } 828 }
700 829
701 DPRINTK("no-byte command\n"); 830 return 0;
702 return 1;
703} 831}
704 832
705static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) 833static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
@@ -1246,10 +1374,20 @@ unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1246 1374
1247 VPRINTK("ENTER\n"); 1375 VPRINTK("ENTER\n");
1248 1376
1249 if (ata_id_has_lba48(args->id)) 1377 if (ata_id_has_lba(args->id)) {
1250 n_sectors = ata_id_u64(args->id, 100); 1378 if (ata_id_has_lba48(args->id))
1251 else 1379 n_sectors = ata_id_u64(args->id, 100);
1252 n_sectors = ata_id_u32(args->id, 60); 1380 else
1381 n_sectors = ata_id_u32(args->id, 60);
1382 } else {
1383 /* CHS default translation */
1384 n_sectors = args->id[1] * args->id[3] * args->id[6];
1385
1386 if (ata_id_current_chs_valid(args->id))
1387 /* CHS current translation */
1388 n_sectors = ata_id_u32(args->id, 57);
1389 }
1390
1253 n_sectors--; /* ATA TotalUserSectors - 1 */ 1391 n_sectors--; /* ATA TotalUserSectors - 1 */
1254 1392
1255 if (args->cmd->cmnd[0] == READ_CAPACITY) { 1393 if (args->cmd->cmnd[0] == READ_CAPACITY) {