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.c534
1 files changed, 391 insertions, 143 deletions
diff --git a/drivers/scsi/libata-scsi.c b/drivers/scsi/libata-scsi.c
index 03b7a6dd95fe..c64169ca7ff0 100644
--- a/drivers/scsi/libata-scsi.c
+++ b/drivers/scsi/libata-scsi.c
@@ -49,6 +49,14 @@ static struct ata_device *
49ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev); 49ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
50 50
51 51
52static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
53 void (*done)(struct scsi_cmnd *))
54{
55 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
56 /* "Invalid field in cbd" */
57 done(cmd);
58}
59
52/** 60/**
53 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. 61 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
54 * @sdev: SCSI device for which BIOS geometry is to be determined 62 * @sdev: SCSI device for which BIOS geometry is to be determined
@@ -182,7 +190,6 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
182{ 190{
183 struct scsi_cmnd *cmd = qc->scsicmd; 191 struct scsi_cmnd *cmd = qc->scsicmd;
184 u8 err = 0; 192 u8 err = 0;
185 unsigned char *sb = cmd->sense_buffer;
186 /* Based on the 3ware driver translation table */ 193 /* Based on the 3ware driver translation table */
187 static unsigned char sense_table[][4] = { 194 static unsigned char sense_table[][4] = {
188 /* BBD|ECC|ID|MAR */ 195 /* BBD|ECC|ID|MAR */
@@ -225,8 +232,6 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
225 }; 232 };
226 int i = 0; 233 int i = 0;
227 234
228 cmd->result = SAM_STAT_CHECK_CONDITION;
229
230 /* 235 /*
231 * Is this an error we can process/parse 236 * Is this an error we can process/parse
232 */ 237 */
@@ -281,11 +286,9 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
281 /* Look for best matches first */ 286 /* Look for best matches first */
282 if((sense_table[i][0] & err) == sense_table[i][0]) 287 if((sense_table[i][0] & err) == sense_table[i][0])
283 { 288 {
284 sb[0] = 0x70; 289 ata_scsi_set_sense(cmd, sense_table[i][1] /* sk */,
285 sb[2] = sense_table[i][1]; 290 sense_table[i][2] /* asc */,
286 sb[7] = 0x0a; 291 sense_table[i][3] /* ascq */ );
287 sb[12] = sense_table[i][2];
288 sb[13] = sense_table[i][3];
289 return; 292 return;
290 } 293 }
291 i++; 294 i++;
@@ -300,11 +303,9 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
300 { 303 {
301 if(stat_table[i][0] & drv_stat) 304 if(stat_table[i][0] & drv_stat)
302 { 305 {
303 sb[0] = 0x70; 306 ata_scsi_set_sense(cmd, sense_table[i][1] /* sk */,
304 sb[2] = stat_table[i][1]; 307 sense_table[i][2] /* asc */,
305 sb[7] = 0x0a; 308 sense_table[i][3] /* ascq */ );
306 sb[12] = stat_table[i][2];
307 sb[13] = stat_table[i][3];
308 return; 309 return;
309 } 310 }
310 i++; 311 i++;
@@ -313,15 +314,12 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
313 printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat); 314 printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
314 /* additional-sense-code[-qualifier] */ 315 /* additional-sense-code[-qualifier] */
315 316
316 sb[0] = 0x70;
317 sb[2] = MEDIUM_ERROR;
318 sb[7] = 0x0A;
319 if (cmd->sc_data_direction == DMA_FROM_DEVICE) { 317 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
320 sb[12] = 0x11; /* "unrecovered read error" */ 318 ata_scsi_set_sense(cmd, MEDIUM_ERROR, 0x11, 0x4);
321 sb[13] = 0x04; 319 /* "unrecovered read error" */
322 } else { 320 } else {
323 sb[12] = 0x0C; /* "write error - */ 321 ata_scsi_set_sense(cmd, MEDIUM_ERROR, 0xc, 0x2);
324 sb[13] = 0x02; /* auto-reallocation failed" */ 322 /* "write error - auto-reallocation failed" */
325 } 323 }
326} 324}
327 325
@@ -430,15 +428,26 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
430 ; /* ignore IMMED bit, violates sat-r05 */ 428 ; /* ignore IMMED bit, violates sat-r05 */
431 } 429 }
432 if (scsicmd[4] & 0x2) 430 if (scsicmd[4] & 0x2)
433 return 1; /* LOEJ bit set not supported */ 431 goto invalid_fld; /* LOEJ bit set not supported */
434 if (((scsicmd[4] >> 4) & 0xf) != 0) 432 if (((scsicmd[4] >> 4) & 0xf) != 0)
435 return 1; /* power conditions not supported */ 433 goto invalid_fld; /* power conditions not supported */
436 if (scsicmd[4] & 0x1) { 434 if (scsicmd[4] & 0x1) {
437 tf->nsect = 1; /* 1 sector, lba=0 */ 435 tf->nsect = 1; /* 1 sector, lba=0 */
438 tf->lbah = 0x0; 436
439 tf->lbam = 0x0; 437 if (qc->dev->flags & ATA_DFLAG_LBA) {
440 tf->lbal = 0x0; 438 qc->tf.flags |= ATA_TFLAG_LBA;
441 tf->device |= ATA_LBA; 439
440 tf->lbah = 0x0;
441 tf->lbam = 0x0;
442 tf->lbal = 0x0;
443 tf->device |= ATA_LBA;
444 } else {
445 /* CHS */
446 tf->lbal = 0x1; /* sect */
447 tf->lbam = 0x0; /* cyl low */
448 tf->lbah = 0x0; /* cyl high */
449 }
450
442 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ 451 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
443 } else { 452 } else {
444 tf->nsect = 0; /* time period value (0 implies now) */ 453 tf->nsect = 0; /* time period value (0 implies now) */
@@ -453,6 +462,11 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
453 */ 462 */
454 463
455 return 0; 464 return 0;
465
466invalid_fld:
467 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
468 /* "Invalid field in cbd" */
469 return 1;
456} 470}
457 471
458 472
@@ -488,6 +502,99 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
488} 502}
489 503
490/** 504/**
505 * scsi_6_lba_len - Get LBA and transfer length
506 * @scsicmd: SCSI command to translate
507 *
508 * Calculate LBA and transfer length for 6-byte commands.
509 *
510 * RETURNS:
511 * @plba: the LBA
512 * @plen: the transfer length
513 */
514
515static void scsi_6_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
516{
517 u64 lba = 0;
518 u32 len = 0;
519
520 VPRINTK("six-byte command\n");
521
522 lba |= ((u64)scsicmd[2]) << 8;
523 lba |= ((u64)scsicmd[3]);
524
525 len |= ((u32)scsicmd[4]);
526
527 *plba = lba;
528 *plen = len;
529}
530
531/**
532 * scsi_10_lba_len - Get LBA and transfer length
533 * @scsicmd: SCSI command to translate
534 *
535 * Calculate LBA and transfer length for 10-byte commands.
536 *
537 * RETURNS:
538 * @plba: the LBA
539 * @plen: the transfer length
540 */
541
542static void scsi_10_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
543{
544 u64 lba = 0;
545 u32 len = 0;
546
547 VPRINTK("ten-byte command\n");
548
549 lba |= ((u64)scsicmd[2]) << 24;
550 lba |= ((u64)scsicmd[3]) << 16;
551 lba |= ((u64)scsicmd[4]) << 8;
552 lba |= ((u64)scsicmd[5]);
553
554 len |= ((u32)scsicmd[7]) << 8;
555 len |= ((u32)scsicmd[8]);
556
557 *plba = lba;
558 *plen = len;
559}
560
561/**
562 * scsi_16_lba_len - Get LBA and transfer length
563 * @scsicmd: SCSI command to translate
564 *
565 * Calculate LBA and transfer length for 16-byte commands.
566 *
567 * RETURNS:
568 * @plba: the LBA
569 * @plen: the transfer length
570 */
571
572static void scsi_16_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
573{
574 u64 lba = 0;
575 u32 len = 0;
576
577 VPRINTK("sixteen-byte command\n");
578
579 lba |= ((u64)scsicmd[2]) << 56;
580 lba |= ((u64)scsicmd[3]) << 48;
581 lba |= ((u64)scsicmd[4]) << 40;
582 lba |= ((u64)scsicmd[5]) << 32;
583 lba |= ((u64)scsicmd[6]) << 24;
584 lba |= ((u64)scsicmd[7]) << 16;
585 lba |= ((u64)scsicmd[8]) << 8;
586 lba |= ((u64)scsicmd[9]);
587
588 len |= ((u32)scsicmd[10]) << 24;
589 len |= ((u32)scsicmd[11]) << 16;
590 len |= ((u32)scsicmd[12]) << 8;
591 len |= ((u32)scsicmd[13]);
592
593 *plba = lba;
594 *plen = len;
595}
596
597/**
491 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one 598 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
492 * @qc: Storage for translated ATA taskfile 599 * @qc: Storage for translated ATA taskfile
493 * @scsicmd: SCSI command to translate 600 * @scsicmd: SCSI command to translate
@@ -508,53 +615,31 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
508 unsigned int lba = tf->flags & ATA_TFLAG_LBA; 615 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
509 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 616 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
510 u64 dev_sectors = qc->dev->n_sectors; 617 u64 dev_sectors = qc->dev->n_sectors;
511 u64 block = 0; 618 u64 block;
512 u32 n_block = 0; 619 u32 n_block;
513 620
514 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 621 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
515 tf->protocol = ATA_PROT_NODATA; 622 tf->protocol = ATA_PROT_NODATA;
516 623
517 if (scsicmd[0] == VERIFY) { 624 if (scsicmd[0] == VERIFY)
518 block |= ((u64)scsicmd[2]) << 24; 625 scsi_10_lba_len(scsicmd, &block, &n_block);
519 block |= ((u64)scsicmd[3]) << 16; 626 else if (scsicmd[0] == VERIFY_16)
520 block |= ((u64)scsicmd[4]) << 8; 627 scsi_16_lba_len(scsicmd, &block, &n_block);
521 block |= ((u64)scsicmd[5]);
522
523 n_block |= ((u32)scsicmd[7]) << 8;
524 n_block |= ((u32)scsicmd[8]);
525 }
526
527 else if (scsicmd[0] == VERIFY_16) {
528 block |= ((u64)scsicmd[2]) << 56;
529 block |= ((u64)scsicmd[3]) << 48;
530 block |= ((u64)scsicmd[4]) << 40;
531 block |= ((u64)scsicmd[5]) << 32;
532 block |= ((u64)scsicmd[6]) << 24;
533 block |= ((u64)scsicmd[7]) << 16;
534 block |= ((u64)scsicmd[8]) << 8;
535 block |= ((u64)scsicmd[9]);
536
537 n_block |= ((u32)scsicmd[10]) << 24;
538 n_block |= ((u32)scsicmd[11]) << 16;
539 n_block |= ((u32)scsicmd[12]) << 8;
540 n_block |= ((u32)scsicmd[13]);
541 }
542
543 else 628 else
544 return 1; 629 goto invalid_fld;
545 630
546 if (!n_block) 631 if (!n_block)
547 return 1; 632 goto nothing_to_do;
548 if (block >= dev_sectors) 633 if (block >= dev_sectors)
549 return 1; 634 goto out_of_range;
550 if ((block + n_block) > dev_sectors) 635 if ((block + n_block) > dev_sectors)
551 return 1; 636 goto out_of_range;
552 if (lba48) { 637 if (lba48) {
553 if (n_block > (64 * 1024)) 638 if (n_block > (64 * 1024))
554 return 1; 639 goto invalid_fld;
555 } else { 640 } else {
556 if (n_block > 256) 641 if (n_block > 256)
557 return 1; 642 goto invalid_fld;
558 } 643 }
559 644
560 if (lba) { 645 if (lba) {
@@ -589,14 +674,15 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
589 head = track % dev->heads; 674 head = track % dev->heads;
590 sect = (u32)block % dev->sectors + 1; 675 sect = (u32)block % dev->sectors + 1;
591 676
592 DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n", (u32)block, track, cyl, head, sect); 677 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
678 (u32)block, track, cyl, head, sect);
593 679
594 /* Check whether the converted CHS can fit. 680 /* Check whether the converted CHS can fit.
595 Cylinder: 0-65535 681 Cylinder: 0-65535
596 Head: 0-15 682 Head: 0-15
597 Sector: 1-255*/ 683 Sector: 1-255*/
598 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) 684 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
599 return 1; 685 goto out_of_range;
600 686
601 tf->command = ATA_CMD_VERIFY; 687 tf->command = ATA_CMD_VERIFY;
602 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ 688 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
@@ -607,6 +693,20 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
607 } 693 }
608 694
609 return 0; 695 return 0;
696
697invalid_fld:
698 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
699 /* "Invalid field in cbd" */
700 return 1;
701
702out_of_range:
703 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
704 /* "Logical Block Address out of range" */
705 return 1;
706
707nothing_to_do:
708 qc->scsicmd->result = SAM_STAT_GOOD;
709 return 1;
610} 710}
611 711
612/** 712/**
@@ -635,8 +735,8 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
635 struct ata_device *dev = qc->dev; 735 struct ata_device *dev = qc->dev;
636 unsigned int lba = tf->flags & ATA_TFLAG_LBA; 736 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
637 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 737 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
638 u64 block = 0; 738 u64 block;
639 u32 n_block = 0; 739 u32 n_block;
640 740
641 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 741 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
642 tf->protocol = qc->dev->xfer_protocol; 742 tf->protocol = qc->dev->xfer_protocol;
@@ -650,56 +750,44 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
650 } 750 }
651 751
652 /* Calculate the SCSI LBA and transfer length. */ 752 /* Calculate the SCSI LBA and transfer length. */
653 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) { 753 switch (scsicmd[0]) {
654 block |= ((u64)scsicmd[2]) << 24; 754 case READ_10:
655 block |= ((u64)scsicmd[3]) << 16; 755 case WRITE_10:
656 block |= ((u64)scsicmd[4]) << 8; 756 scsi_10_lba_len(scsicmd, &block, &n_block);
657 block |= ((u64)scsicmd[5]); 757 break;
658 758 case READ_6:
659 n_block |= ((u32)scsicmd[7]) << 8; 759 case WRITE_6:
660 n_block |= ((u32)scsicmd[8]); 760 scsi_6_lba_len(scsicmd, &block, &n_block);
661
662 VPRINTK("ten-byte command\n");
663 } else if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
664 block |= ((u64)scsicmd[2]) << 8;
665 block |= ((u64)scsicmd[3]);
666 761
667 n_block |= ((u32)scsicmd[4]); 762 /* for 6-byte r/w commands, transfer length 0
763 * means 256 blocks of data, not 0 block.
764 */
668 if (!n_block) 765 if (!n_block)
669 n_block = 256; 766 n_block = 256;
670 767 break;
671 VPRINTK("six-byte command\n"); 768 case READ_16:
672 } else if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) { 769 case WRITE_16:
673 block |= ((u64)scsicmd[2]) << 56; 770 scsi_16_lba_len(scsicmd, &block, &n_block);
674 block |= ((u64)scsicmd[3]) << 48; 771 break;
675 block |= ((u64)scsicmd[4]) << 40; 772 default:
676 block |= ((u64)scsicmd[5]) << 32;
677 block |= ((u64)scsicmd[6]) << 24;
678 block |= ((u64)scsicmd[7]) << 16;
679 block |= ((u64)scsicmd[8]) << 8;
680 block |= ((u64)scsicmd[9]);
681
682 n_block |= ((u32)scsicmd[10]) << 24;
683 n_block |= ((u32)scsicmd[11]) << 16;
684 n_block |= ((u32)scsicmd[12]) << 8;
685 n_block |= ((u32)scsicmd[13]);
686
687 VPRINTK("sixteen-byte command\n");
688 } else {
689 DPRINTK("no-byte command\n"); 773 DPRINTK("no-byte command\n");
690 return 1; 774 goto invalid_fld;
691 } 775 }
692 776
693 /* Check and compose ATA command */ 777 /* Check and compose ATA command */
694 if (!n_block) 778 if (!n_block)
695 /* In ATA, sector count 0 means 256 or 65536 sectors, not 0 sectors. */ 779 /* For 10-byte and 16-byte SCSI R/W commands, transfer
696 return 1; 780 * length 0 means transfer 0 block of data.
781 * However, for ATA R/W commands, sector count 0 means
782 * 256 or 65536 sectors, not 0 sectors as in SCSI.
783 */
784 goto nothing_to_do;
697 785
698 if (lba) { 786 if (lba) {
699 if (lba48) { 787 if (lba48) {
700 /* The request -may- be too large for LBA48. */ 788 /* The request -may- be too large for LBA48. */
701 if ((block >> 48) || (n_block > 65536)) 789 if ((block >> 48) || (n_block > 65536))
702 return 1; 790 goto out_of_range;
703 791
704 tf->hob_nsect = (n_block >> 8) & 0xff; 792 tf->hob_nsect = (n_block >> 8) & 0xff;
705 793
@@ -711,11 +799,11 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
711 799
712 /* The request -may- be too large for LBA28. */ 800 /* The request -may- be too large for LBA28. */
713 if ((block >> 28) || (n_block > 256)) 801 if ((block >> 28) || (n_block > 256))
714 return 1; 802 goto out_of_range;
715 803
716 tf->device |= (block >> 24) & 0xf; 804 tf->device |= (block >> 24) & 0xf;
717 } 805 }
718 806
719 qc->nsect = n_block; 807 qc->nsect = n_block;
720 tf->nsect = n_block & 0xff; 808 tf->nsect = n_block & 0xff;
721 809
@@ -730,24 +818,24 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
730 818
731 /* The request -may- be too large for CHS addressing. */ 819 /* The request -may- be too large for CHS addressing. */
732 if ((block >> 28) || (n_block > 256)) 820 if ((block >> 28) || (n_block > 256))
733 return 1; 821 goto out_of_range;
734 822
735 /* Convert LBA to CHS */ 823 /* Convert LBA to CHS */
736 track = (u32)block / dev->sectors; 824 track = (u32)block / dev->sectors;
737 cyl = track / dev->heads; 825 cyl = track / dev->heads;
738 head = track % dev->heads; 826 head = track % dev->heads;
739 sect = (u32)block % dev->sectors + 1; 827 sect = (u32)block % dev->sectors + 1;
740 828
741 DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n", 829 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
742 (u32)block, track, cyl, head, sect); 830 (u32)block, track, cyl, head, sect);
743 831
744 /* Check whether the converted CHS can fit. 832 /* Check whether the converted CHS can fit.
745 Cylinder: 0-65535 833 Cylinder: 0-65535
746 Head: 0-15 834 Head: 0-15
747 Sector: 1-255*/ 835 Sector: 1-255*/
748 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) 836 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
749 return 1; 837 goto out_of_range;
750 838
751 qc->nsect = n_block; 839 qc->nsect = n_block;
752 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ 840 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
753 tf->lbal = sect; 841 tf->lbal = sect;
@@ -757,6 +845,20 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
757 } 845 }
758 846
759 return 0; 847 return 0;
848
849invalid_fld:
850 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
851 /* "Invalid field in cbd" */
852 return 1;
853
854out_of_range:
855 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
856 /* "Logical Block Address out of range" */
857 return 1;
858
859nothing_to_do:
860 qc->scsicmd->result = SAM_STAT_GOOD;
861 return 1;
760} 862}
761 863
762static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) 864static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
@@ -788,6 +890,12 @@ static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
788 * This function sets up an ata_queued_cmd structure for the 890 * This function sets up an ata_queued_cmd structure for the
789 * SCSI command, and sends that ata_queued_cmd to the hardware. 891 * SCSI command, and sends that ata_queued_cmd to the hardware.
790 * 892 *
893 * The xlat_func argument (actor) returns 0 if ready to execute
894 * ATA command, else 1 to finish translation. If 1 is returned
895 * then cmd->result (and possibly cmd->sense_buffer) are assumed
896 * to be set reflecting an error condition or clean (early)
897 * termination.
898 *
791 * LOCKING: 899 * LOCKING:
792 * spin_lock_irqsave(host_set lock) 900 * spin_lock_irqsave(host_set lock)
793 */ 901 */
@@ -804,7 +912,7 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
804 912
805 qc = ata_scsi_qc_new(ap, dev, cmd, done); 913 qc = ata_scsi_qc_new(ap, dev, cmd, done);
806 if (!qc) 914 if (!qc)
807 return; 915 goto err_mem;
808 916
809 /* data is present; dma-map it */ 917 /* data is present; dma-map it */
810 if (cmd->sc_data_direction == DMA_FROM_DEVICE || 918 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
@@ -812,7 +920,7 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
812 if (unlikely(cmd->request_bufflen < 1)) { 920 if (unlikely(cmd->request_bufflen < 1)) {
813 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", 921 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
814 ap->id, dev->devno); 922 ap->id, dev->devno);
815 goto err_out; 923 goto err_did;
816 } 924 }
817 925
818 if (cmd->use_sg) 926 if (cmd->use_sg)
@@ -827,19 +935,28 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
827 qc->complete_fn = ata_scsi_qc_complete; 935 qc->complete_fn = ata_scsi_qc_complete;
828 936
829 if (xlat_func(qc, scsicmd)) 937 if (xlat_func(qc, scsicmd))
830 goto err_out; 938 goto early_finish;
831 939
832 /* select device, send command to hardware */ 940 /* select device, send command to hardware */
833 if (ata_qc_issue(qc)) 941 if (ata_qc_issue(qc))
834 goto err_out; 942 goto err_did;
835 943
836 VPRINTK("EXIT\n"); 944 VPRINTK("EXIT\n");
837 return; 945 return;
838 946
839err_out: 947early_finish:
948 ata_qc_free(qc);
949 done(cmd);
950 DPRINTK("EXIT - early finish (good or error)\n");
951 return;
952
953err_did:
840 ata_qc_free(qc); 954 ata_qc_free(qc);
841 ata_bad_cdb(cmd, done); 955err_mem:
842 DPRINTK("EXIT - badcmd\n"); 956 cmd->result = (DID_ERROR << 16);
957 done(cmd);
958 DPRINTK("EXIT - internal\n");
959 return;
843} 960}
844 961
845/** 962/**
@@ -906,7 +1023,8 @@ static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
906 * Mapping the response buffer, calling the command's handler, 1023 * Mapping the response buffer, calling the command's handler,
907 * and handling the handler's return value. This return value 1024 * and handling the handler's return value. This return value
908 * indicates whether the handler wishes the SCSI command to be 1025 * indicates whether the handler wishes the SCSI command to be
909 * completed successfully, or not. 1026 * completed successfully (0), or not (in which case cmd->result
1027 * and sense buffer are assumed to be set).
910 * 1028 *
911 * LOCKING: 1029 * LOCKING:
912 * spin_lock_irqsave(host_set lock) 1030 * spin_lock_irqsave(host_set lock)
@@ -925,12 +1043,9 @@ void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
925 rc = actor(args, rbuf, buflen); 1043 rc = actor(args, rbuf, buflen);
926 ata_scsi_rbuf_put(cmd, rbuf); 1044 ata_scsi_rbuf_put(cmd, rbuf);
927 1045
928 if (rc) 1046 if (rc == 0)
929 ata_bad_cdb(cmd, args->done);
930 else {
931 cmd->result = SAM_STAT_GOOD; 1047 cmd->result = SAM_STAT_GOOD;
932 args->done(cmd); 1048 args->done(cmd);
933 }
934} 1049}
935 1050
936/** 1051/**
@@ -1236,8 +1351,16 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1236 * in the same manner) 1351 * in the same manner)
1237 */ 1352 */
1238 page_control = scsicmd[2] >> 6; 1353 page_control = scsicmd[2] >> 6;
1239 if ((page_control != 0) && (page_control != 3)) 1354 switch (page_control) {
1240 return 1; 1355 case 0: /* current */
1356 break; /* supported */
1357 case 3: /* saved */
1358 goto saving_not_supp;
1359 case 1: /* changeable */
1360 case 2: /* defaults */
1361 default:
1362 goto invalid_fld;
1363 }
1241 1364
1242 if (six_byte) 1365 if (six_byte)
1243 output_len = 4; 1366 output_len = 4;
@@ -1268,7 +1391,7 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1268 break; 1391 break;
1269 1392
1270 default: /* invalid page code */ 1393 default: /* invalid page code */
1271 return 1; 1394 goto invalid_fld;
1272 } 1395 }
1273 1396
1274 if (six_byte) { 1397 if (six_byte) {
@@ -1281,6 +1404,16 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1281 } 1404 }
1282 1405
1283 return 0; 1406 return 0;
1407
1408invalid_fld:
1409 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1410 /* "Invalid field in cbd" */
1411 return 1;
1412
1413saving_not_supp:
1414 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1415 /* "Saving parameters not supported" */
1416 return 1;
1284} 1417}
1285 1418
1286/** 1419/**
@@ -1380,6 +1513,34 @@ unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1380} 1513}
1381 1514
1382/** 1515/**
1516 * ata_scsi_set_sense - Set SCSI sense data and status
1517 * @cmd: SCSI request to be handled
1518 * @sk: SCSI-defined sense key
1519 * @asc: SCSI-defined additional sense code
1520 * @ascq: SCSI-defined additional sense code qualifier
1521 *
1522 * Helper function that builds a valid fixed format, current
1523 * response code and the given sense key (sk), additional sense
1524 * code (asc) and additional sense code qualifier (ascq) with
1525 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1526 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1527 *
1528 * LOCKING:
1529 * Not required
1530 */
1531
1532void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1533{
1534 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1535
1536 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
1537 cmd->sense_buffer[2] = sk;
1538 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
1539 cmd->sense_buffer[12] = asc;
1540 cmd->sense_buffer[13] = ascq;
1541}
1542
1543/**
1383 * ata_scsi_badcmd - End a SCSI request with an error 1544 * ata_scsi_badcmd - End a SCSI request with an error
1384 * @cmd: SCSI request to be handled 1545 * @cmd: SCSI request to be handled
1385 * @done: SCSI command completion function 1546 * @done: SCSI command completion function
@@ -1397,30 +1558,84 @@ unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1397void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq) 1558void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1398{ 1559{
1399 DPRINTK("ENTER\n"); 1560 DPRINTK("ENTER\n");
1400 cmd->result = SAM_STAT_CHECK_CONDITION; 1561 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1401
1402 cmd->sense_buffer[0] = 0x70;
1403 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1404 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1405 cmd->sense_buffer[12] = asc;
1406 cmd->sense_buffer[13] = ascq;
1407 1562
1408 done(cmd); 1563 done(cmd);
1409} 1564}
1410 1565
1566void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
1567 struct scsi_cmnd *cmd)
1568{
1569 DECLARE_COMPLETION(wait);
1570 struct ata_queued_cmd *qc;
1571 unsigned long flags;
1572 int rc;
1573
1574 DPRINTK("ATAPI request sense\n");
1575
1576 qc = ata_qc_new_init(ap, dev);
1577 BUG_ON(qc == NULL);
1578
1579 /* FIXME: is this needed? */
1580 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
1581
1582 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
1583 qc->dma_dir = DMA_FROM_DEVICE;
1584
1585 memset(&qc->cdb, 0, ap->cdb_len);
1586 qc->cdb[0] = REQUEST_SENSE;
1587 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
1588
1589 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1590 qc->tf.command = ATA_CMD_PACKET;
1591
1592 qc->tf.protocol = ATA_PROT_ATAPI;
1593 qc->tf.lbam = (8 * 1024) & 0xff;
1594 qc->tf.lbah = (8 * 1024) >> 8;
1595 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
1596
1597 qc->waiting = &wait;
1598 qc->complete_fn = ata_qc_complete_noop;
1599
1600 spin_lock_irqsave(&ap->host_set->lock, flags);
1601 rc = ata_qc_issue(qc);
1602 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1603
1604 if (rc)
1605 ata_port_disable(ap);
1606 else
1607 wait_for_completion(&wait);
1608
1609 DPRINTK("EXIT\n");
1610}
1611
1411static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) 1612static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1412{ 1613{
1413 struct scsi_cmnd *cmd = qc->scsicmd; 1614 struct scsi_cmnd *cmd = qc->scsicmd;
1414 1615
1415 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) { 1616 VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat);
1617
1618 if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ)))
1619 ata_to_sense_error(qc, drv_stat);
1620
1621 else if (unlikely(drv_stat & ATA_ERR)) {
1416 DPRINTK("request check condition\n"); 1622 DPRINTK("request check condition\n");
1417 1623
1624 /* FIXME: command completion with check condition
1625 * but no sense causes the error handler to run,
1626 * which then issues REQUEST SENSE, fills in the sense
1627 * buffer, and completes the command (for the second
1628 * time). We need to issue REQUEST SENSE some other
1629 * way, to avoid completing the command twice.
1630 */
1418 cmd->result = SAM_STAT_CHECK_CONDITION; 1631 cmd->result = SAM_STAT_CHECK_CONDITION;
1419 1632
1420 qc->scsidone(cmd); 1633 qc->scsidone(cmd);
1421 1634
1422 return 1; 1635 return 1;
1423 } else { 1636 }
1637
1638 else {
1424 u8 *scsicmd = cmd->cmnd; 1639 u8 *scsicmd = cmd->cmnd;
1425 1640
1426 if (scsicmd[0] == INQUIRY) { 1641 if (scsicmd[0] == INQUIRY) {
@@ -1428,15 +1643,30 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1428 unsigned int buflen; 1643 unsigned int buflen;
1429 1644
1430 buflen = ata_scsi_rbuf_get(cmd, &buf); 1645 buflen = ata_scsi_rbuf_get(cmd, &buf);
1431 buf[2] = 0x5; 1646
1432 buf[3] = (buf[3] & 0xf0) | 2; 1647 /* ATAPI devices typically report zero for their SCSI version,
1648 * and sometimes deviate from the spec WRT response data
1649 * format. If SCSI version is reported as zero like normal,
1650 * then we make the following fixups: 1) Fake MMC-5 version,
1651 * to indicate to the Linux scsi midlayer this is a modern
1652 * device. 2) Ensure response data format / ATAPI information
1653 * are always correct.
1654 */
1655 /* FIXME: do we ever override EVPD pages and the like, with
1656 * this code?
1657 */
1658 if (buf[2] == 0) {
1659 buf[2] = 0x5;
1660 buf[3] = 0x32;
1661 }
1662
1433 ata_scsi_rbuf_put(cmd, buf); 1663 ata_scsi_rbuf_put(cmd, buf);
1434 } 1664 }
1665
1435 cmd->result = SAM_STAT_GOOD; 1666 cmd->result = SAM_STAT_GOOD;
1436 } 1667 }
1437 1668
1438 qc->scsidone(cmd); 1669 qc->scsidone(cmd);
1439
1440 return 0; 1670 return 0;
1441} 1671}
1442/** 1672/**
@@ -1697,7 +1927,7 @@ void ata_scsi_simulate(u16 *id,
1697 1927
1698 case INQUIRY: 1928 case INQUIRY:
1699 if (scsicmd[1] & 2) /* is CmdDt set? */ 1929 if (scsicmd[1] & 2) /* is CmdDt set? */
1700 ata_bad_cdb(cmd, done); 1930 ata_scsi_invalid_field(cmd, done);
1701 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ 1931 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1702 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); 1932 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1703 else if (scsicmd[2] == 0x00) 1933 else if (scsicmd[2] == 0x00)
@@ -1707,7 +1937,7 @@ void ata_scsi_simulate(u16 *id,
1707 else if (scsicmd[2] == 0x83) 1937 else if (scsicmd[2] == 0x83)
1708 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); 1938 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1709 else 1939 else
1710 ata_bad_cdb(cmd, done); 1940 ata_scsi_invalid_field(cmd, done);
1711 break; 1941 break;
1712 1942
1713 case MODE_SENSE: 1943 case MODE_SENSE:
@@ -1717,7 +1947,7 @@ void ata_scsi_simulate(u16 *id,
1717 1947
1718 case MODE_SELECT: /* unconditionally return */ 1948 case MODE_SELECT: /* unconditionally return */
1719 case MODE_SELECT_10: /* bad-field-in-cdb */ 1949 case MODE_SELECT_10: /* bad-field-in-cdb */
1720 ata_bad_cdb(cmd, done); 1950 ata_scsi_invalid_field(cmd, done);
1721 break; 1951 break;
1722 1952
1723 case READ_CAPACITY: 1953 case READ_CAPACITY:
@@ -1728,7 +1958,7 @@ void ata_scsi_simulate(u16 *id,
1728 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) 1958 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1729 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); 1959 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1730 else 1960 else
1731 ata_bad_cdb(cmd, done); 1961 ata_scsi_invalid_field(cmd, done);
1732 break; 1962 break;
1733 1963
1734 case REPORT_LUNS: 1964 case REPORT_LUNS:
@@ -1740,8 +1970,26 @@ void ata_scsi_simulate(u16 *id,
1740 1970
1741 /* all other commands */ 1971 /* all other commands */
1742 default: 1972 default:
1743 ata_bad_scsiop(cmd, done); 1973 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
1974 /* "Invalid command operation code" */
1975 done(cmd);
1744 break; 1976 break;
1745 } 1977 }
1746} 1978}
1747 1979
1980void ata_scsi_scan_host(struct ata_port *ap)
1981{
1982 struct ata_device *dev;
1983 unsigned int i;
1984
1985 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1986 return;
1987
1988 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1989 dev = &ap->device[i];
1990
1991 if (ata_dev_present(dev))
1992 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
1993 }
1994}
1995