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.c754
1 files changed, 529 insertions, 225 deletions
diff --git a/drivers/scsi/libata-scsi.c b/drivers/scsi/libata-scsi.c
index f1e129b7c972..93d18a74c401 100644
--- a/drivers/scsi/libata-scsi.c
+++ b/drivers/scsi/libata-scsi.c
@@ -41,6 +41,7 @@
41#include <scsi/scsi_cmnd.h> 41#include <scsi/scsi_cmnd.h>
42#include <scsi/scsi_eh.h> 42#include <scsi/scsi_eh.h>
43#include <scsi/scsi_device.h> 43#include <scsi/scsi_device.h>
44#include <scsi/scsi_tcq.h>
44#include <scsi/scsi_transport.h> 45#include <scsi/scsi_transport.h>
45#include <linux/libata.h> 46#include <linux/libata.h>
46#include <linux/hdreg.h> 47#include <linux/hdreg.h>
@@ -51,10 +52,14 @@
51#define SECTOR_SIZE 512 52#define SECTOR_SIZE 512
52 53
53typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd); 54typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
54static struct ata_device * 55
55ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev); 56static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
56static void ata_scsi_error(struct Scsi_Host *host); 57 const struct scsi_device *scsidev);
57enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd); 58static struct ata_device * ata_scsi_find_dev(struct ata_port *ap,
59 const struct scsi_device *scsidev);
60static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
61 unsigned int id, unsigned int lun);
62
58 63
59#define RW_RECOVERY_MPAGE 0x1 64#define RW_RECOVERY_MPAGE 0x1
60#define RW_RECOVERY_MPAGE_LEN 12 65#define RW_RECOVERY_MPAGE_LEN 12
@@ -102,6 +107,7 @@ static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
102struct scsi_transport_template ata_scsi_transport_template = { 107struct scsi_transport_template ata_scsi_transport_template = {
103 .eh_strategy_handler = ata_scsi_error, 108 .eh_strategy_handler = ata_scsi_error,
104 .eh_timed_out = ata_scsi_timed_out, 109 .eh_timed_out = ata_scsi_timed_out,
110 .user_scan = ata_scsi_user_scan,
105}; 111};
106 112
107 113
@@ -304,7 +310,6 @@ int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
304 310
305/** 311/**
306 * ata_scsi_qc_new - acquire new ata_queued_cmd reference 312 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
307 * @ap: ATA port to which the new command is attached
308 * @dev: ATA device to which the new command is attached 313 * @dev: ATA device to which the new command is attached
309 * @cmd: SCSI command that originated this ATA command 314 * @cmd: SCSI command that originated this ATA command
310 * @done: SCSI command completion function 315 * @done: SCSI command completion function
@@ -323,14 +328,13 @@ int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
323 * RETURNS: 328 * RETURNS:
324 * Command allocated, or %NULL if none available. 329 * Command allocated, or %NULL if none available.
325 */ 330 */
326struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap, 331struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
327 struct ata_device *dev,
328 struct scsi_cmnd *cmd, 332 struct scsi_cmnd *cmd,
329 void (*done)(struct scsi_cmnd *)) 333 void (*done)(struct scsi_cmnd *))
330{ 334{
331 struct ata_queued_cmd *qc; 335 struct ata_queued_cmd *qc;
332 336
333 qc = ata_qc_new_init(ap, dev); 337 qc = ata_qc_new_init(dev);
334 if (qc) { 338 if (qc) {
335 qc->scsicmd = cmd; 339 qc->scsicmd = cmd;
336 qc->scsidone = done; 340 qc->scsidone = done;
@@ -397,18 +401,18 @@ void ata_dump_status(unsigned id, struct ata_taskfile *tf)
397 401
398int ata_scsi_device_resume(struct scsi_device *sdev) 402int ata_scsi_device_resume(struct scsi_device *sdev)
399{ 403{
400 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0]; 404 struct ata_port *ap = ata_shost_to_port(sdev->host);
401 struct ata_device *dev = &ap->device[sdev->id]; 405 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
402 406
403 return ata_device_resume(ap, dev); 407 return ata_device_resume(dev);
404} 408}
405 409
406int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state) 410int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
407{ 411{
408 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0]; 412 struct ata_port *ap = ata_shost_to_port(sdev->host);
409 struct ata_device *dev = &ap->device[sdev->id]; 413 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
410 414
411 return ata_device_suspend(ap, dev, state); 415 return ata_device_suspend(dev, state);
412} 416}
413 417
414/** 418/**
@@ -419,6 +423,7 @@ int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
419 * @sk: the sense key we'll fill out 423 * @sk: the sense key we'll fill out
420 * @asc: the additional sense code we'll fill out 424 * @asc: the additional sense code we'll fill out
421 * @ascq: the additional sense code qualifier we'll fill out 425 * @ascq: the additional sense code qualifier we'll fill out
426 * @verbose: be verbose
422 * 427 *
423 * Converts an ATA error into a SCSI error. Fill out pointers to 428 * Converts an ATA error into a SCSI error. Fill out pointers to
424 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor 429 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
@@ -428,7 +433,7 @@ int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
428 * spin_lock_irqsave(host_set lock) 433 * spin_lock_irqsave(host_set lock)
429 */ 434 */
430void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc, 435void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
431 u8 *ascq) 436 u8 *ascq, int verbose)
432{ 437{
433 int i; 438 int i;
434 439
@@ -493,8 +498,9 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
493 } 498 }
494 } 499 }
495 /* No immediate match */ 500 /* No immediate match */
496 printk(KERN_WARNING "ata%u: no sense translation for " 501 if (verbose)
497 "error 0x%02x\n", id, drv_err); 502 printk(KERN_WARNING "ata%u: no sense translation for "
503 "error 0x%02x\n", id, drv_err);
498 } 504 }
499 505
500 /* Fall back to interpreting status bits */ 506 /* Fall back to interpreting status bits */
@@ -507,8 +513,9 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
507 } 513 }
508 } 514 }
509 /* No error? Undecoded? */ 515 /* No error? Undecoded? */
510 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n", 516 if (verbose)
511 id, drv_stat); 517 printk(KERN_WARNING "ata%u: no sense translation for "
518 "status: 0x%02x\n", id, drv_stat);
512 519
513 /* We need a sensible error return here, which is tricky, and one 520 /* We need a sensible error return here, which is tricky, and one
514 that won't cause people to do things like return a disk wrongly */ 521 that won't cause people to do things like return a disk wrongly */
@@ -517,9 +524,10 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
517 *ascq = 0x00; 524 *ascq = 0x00;
518 525
519 translate_done: 526 translate_done:
520 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to " 527 if (verbose)
521 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err, 528 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
522 *sk, *asc, *ascq); 529 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
530 id, drv_stat, drv_err, *sk, *asc, *ascq);
523 return; 531 return;
524} 532}
525 533
@@ -539,27 +547,23 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
539void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc) 547void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
540{ 548{
541 struct scsi_cmnd *cmd = qc->scsicmd; 549 struct scsi_cmnd *cmd = qc->scsicmd;
542 struct ata_taskfile *tf = &qc->tf; 550 struct ata_taskfile *tf = &qc->result_tf;
543 unsigned char *sb = cmd->sense_buffer; 551 unsigned char *sb = cmd->sense_buffer;
544 unsigned char *desc = sb + 8; 552 unsigned char *desc = sb + 8;
553 int verbose = qc->ap->ops->error_handler == NULL;
545 554
546 memset(sb, 0, SCSI_SENSE_BUFFERSIZE); 555 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
547 556
548 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; 557 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
549 558
550 /* 559 /*
551 * Read the controller registers.
552 */
553 WARN_ON(qc->ap->ops->tf_read == NULL);
554 qc->ap->ops->tf_read(qc->ap, tf);
555
556 /*
557 * Use ata_to_sense_error() to map status register bits 560 * Use ata_to_sense_error() to map status register bits
558 * onto sense key, asc & ascq. 561 * onto sense key, asc & ascq.
559 */ 562 */
560 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { 563 if (qc->err_mask ||
564 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
561 ata_to_sense_error(qc->ap->id, tf->command, tf->feature, 565 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
562 &sb[1], &sb[2], &sb[3]); 566 &sb[1], &sb[2], &sb[3], verbose);
563 sb[1] &= 0x0f; 567 sb[1] &= 0x0f;
564 } 568 }
565 569
@@ -615,26 +619,22 @@ void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
615void ata_gen_fixed_sense(struct ata_queued_cmd *qc) 619void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
616{ 620{
617 struct scsi_cmnd *cmd = qc->scsicmd; 621 struct scsi_cmnd *cmd = qc->scsicmd;
618 struct ata_taskfile *tf = &qc->tf; 622 struct ata_taskfile *tf = &qc->result_tf;
619 unsigned char *sb = cmd->sense_buffer; 623 unsigned char *sb = cmd->sense_buffer;
624 int verbose = qc->ap->ops->error_handler == NULL;
620 625
621 memset(sb, 0, SCSI_SENSE_BUFFERSIZE); 626 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
622 627
623 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; 628 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
624 629
625 /* 630 /*
626 * Read the controller registers.
627 */
628 WARN_ON(qc->ap->ops->tf_read == NULL);
629 qc->ap->ops->tf_read(qc->ap, tf);
630
631 /*
632 * Use ata_to_sense_error() to map status register bits 631 * Use ata_to_sense_error() to map status register bits
633 * onto sense key, asc & ascq. 632 * onto sense key, asc & ascq.
634 */ 633 */
635 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { 634 if (qc->err_mask ||
635 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
636 ata_to_sense_error(qc->ap->id, tf->command, tf->feature, 636 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
637 &sb[2], &sb[12], &sb[13]); 637 &sb[2], &sb[12], &sb[13], verbose);
638 sb[2] &= 0x0f; 638 sb[2] &= 0x0f;
639 } 639 }
640 640
@@ -677,7 +677,7 @@ static void ata_scsi_dev_config(struct scsi_device *sdev,
677 */ 677 */
678 max_sectors = ATA_MAX_SECTORS; 678 max_sectors = ATA_MAX_SECTORS;
679 if (dev->flags & ATA_DFLAG_LBA48) 679 if (dev->flags & ATA_DFLAG_LBA48)
680 max_sectors = 2048; 680 max_sectors = ATA_MAX_SECTORS_LBA48;
681 if (dev->max_sectors) 681 if (dev->max_sectors)
682 max_sectors = dev->max_sectors; 682 max_sectors = dev->max_sectors;
683 683
@@ -692,6 +692,14 @@ static void ata_scsi_dev_config(struct scsi_device *sdev,
692 request_queue_t *q = sdev->request_queue; 692 request_queue_t *q = sdev->request_queue;
693 blk_queue_max_hw_segments(q, q->max_hw_segments - 1); 693 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
694 } 694 }
695
696 if (dev->flags & ATA_DFLAG_NCQ) {
697 int depth;
698
699 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
700 depth = min(ATA_MAX_QUEUE - 1, depth);
701 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
702 }
695} 703}
696 704
697/** 705/**
@@ -708,152 +716,88 @@ static void ata_scsi_dev_config(struct scsi_device *sdev,
708 716
709int ata_scsi_slave_config(struct scsi_device *sdev) 717int ata_scsi_slave_config(struct scsi_device *sdev)
710{ 718{
719 struct ata_port *ap = ata_shost_to_port(sdev->host);
720 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
721
711 ata_scsi_sdev_config(sdev); 722 ata_scsi_sdev_config(sdev);
712 723
713 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD); 724 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
714 725
715 if (sdev->id < ATA_MAX_DEVICES) { 726 if (dev)
716 struct ata_port *ap;
717 struct ata_device *dev;
718
719 ap = (struct ata_port *) &sdev->host->hostdata[0];
720 dev = &ap->device[sdev->id];
721
722 ata_scsi_dev_config(sdev, dev); 727 ata_scsi_dev_config(sdev, dev);
723 }
724 728
725 return 0; /* scsi layer doesn't check return value, sigh */ 729 return 0; /* scsi layer doesn't check return value, sigh */
726} 730}
727 731
728/** 732/**
729 * ata_scsi_timed_out - SCSI layer time out callback 733 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
730 * @cmd: timed out SCSI command 734 * @sdev: SCSI device to be destroyed
731 * 735 *
732 * Handles SCSI layer timeout. We race with normal completion of 736 * @sdev is about to be destroyed for hot/warm unplugging. If
733 * the qc for @cmd. If the qc is already gone, we lose and let 737 * this unplugging was initiated by libata as indicated by NULL
734 * the scsi command finish (EH_HANDLED). Otherwise, the qc has 738 * dev->sdev, this function doesn't have to do anything.
735 * timed out and EH should be invoked. Prevent ata_qc_complete() 739 * Otherwise, SCSI layer initiated warm-unplug is in progress.
736 * from finishing it by setting EH_SCHEDULED and return 740 * Clear dev->sdev, schedule the device for ATA detach and invoke
737 * EH_NOT_HANDLED. 741 * EH.
738 * 742 *
739 * LOCKING: 743 * LOCKING:
740 * Called from timer context 744 * Defined by SCSI layer. We don't really care.
741 *
742 * RETURNS:
743 * EH_HANDLED or EH_NOT_HANDLED
744 */ 745 */
745enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd) 746void ata_scsi_slave_destroy(struct scsi_device *sdev)
746{ 747{
747 struct Scsi_Host *host = cmd->device->host; 748 struct ata_port *ap = ata_shost_to_port(sdev->host);
748 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
749 unsigned long flags; 749 unsigned long flags;
750 struct ata_queued_cmd *qc; 750 struct ata_device *dev;
751 enum scsi_eh_timer_return ret = EH_HANDLED;
752 751
753 DPRINTK("ENTER\n"); 752 if (!ap->ops->error_handler)
753 return;
754 754
755 spin_lock_irqsave(&ap->host_set->lock, flags); 755 spin_lock_irqsave(ap->lock, flags);
756 qc = ata_qc_from_tag(ap, ap->active_tag); 756 dev = __ata_scsi_find_dev(ap, sdev);
757 if (qc) { 757 if (dev && dev->sdev) {
758 WARN_ON(qc->scsicmd != cmd); 758 /* SCSI device already in CANCEL state, no need to offline it */
759 qc->flags |= ATA_QCFLAG_EH_SCHEDULED; 759 dev->sdev = NULL;
760 qc->err_mask |= AC_ERR_TIMEOUT; 760 dev->flags |= ATA_DFLAG_DETACH;
761 ret = EH_NOT_HANDLED; 761 ata_port_schedule_eh(ap);
762 } 762 }
763 spin_unlock_irqrestore(&ap->host_set->lock, flags); 763 spin_unlock_irqrestore(ap->lock, flags);
764
765 DPRINTK("EXIT, ret=%d\n", ret);
766 return ret;
767} 764}
768 765
769/** 766/**
770 * ata_scsi_error - SCSI layer error handler callback 767 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
771 * @host: SCSI host on which error occurred 768 * @sdev: SCSI device to configure queue depth for
769 * @queue_depth: new queue depth
772 * 770 *
773 * Handles SCSI-layer-thrown error events. 771 * This is libata standard hostt->change_queue_depth callback.
772 * SCSI will call into this callback when user tries to set queue
773 * depth via sysfs.
774 * 774 *
775 * LOCKING: 775 * LOCKING:
776 * Inherited from SCSI layer (none, can sleep) 776 * SCSI layer (we don't care)
777 *
778 * RETURNS:
779 * Newly configured queue depth.
777 */ 780 */
778 781int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
779static void ata_scsi_error(struct Scsi_Host *host)
780{ 782{
781 struct ata_port *ap; 783 struct ata_port *ap = ata_shost_to_port(sdev->host);
782 unsigned long flags; 784 struct ata_device *dev;
783 785 int max_depth;
784 DPRINTK("ENTER\n");
785
786 ap = (struct ata_port *) &host->hostdata[0];
787
788 spin_lock_irqsave(&ap->host_set->lock, flags);
789 WARN_ON(ap->flags & ATA_FLAG_IN_EH);
790 ap->flags |= ATA_FLAG_IN_EH;
791 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
792 spin_unlock_irqrestore(&ap->host_set->lock, flags);
793
794 ata_port_flush_task(ap);
795
796 ap->ops->eng_timeout(ap);
797
798 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
799
800 scsi_eh_flush_done_q(&ap->eh_done_q);
801
802 spin_lock_irqsave(&ap->host_set->lock, flags);
803 ap->flags &= ~ATA_FLAG_IN_EH;
804 spin_unlock_irqrestore(&ap->host_set->lock, flags);
805
806 DPRINTK("EXIT\n");
807}
808
809static void ata_eh_scsidone(struct scsi_cmnd *scmd)
810{
811 /* nada */
812}
813
814static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
815{
816 struct ata_port *ap = qc->ap;
817 struct scsi_cmnd *scmd = qc->scsicmd;
818 unsigned long flags;
819 786
820 spin_lock_irqsave(&ap->host_set->lock, flags); 787 if (queue_depth < 1)
821 qc->scsidone = ata_eh_scsidone; 788 return sdev->queue_depth;
822 __ata_qc_complete(qc);
823 WARN_ON(ata_tag_valid(qc->tag));
824 spin_unlock_irqrestore(&ap->host_set->lock, flags);
825 789
826 scsi_eh_finish_cmd(scmd, &ap->eh_done_q); 790 dev = ata_scsi_find_dev(ap, sdev);
827} 791 if (!dev || !ata_dev_enabled(dev))
792 return sdev->queue_depth;
828 793
829/** 794 max_depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
830 * ata_eh_qc_complete - Complete an active ATA command from EH 795 max_depth = min(ATA_MAX_QUEUE - 1, max_depth);
831 * @qc: Command to complete 796 if (queue_depth > max_depth)
832 * 797 queue_depth = max_depth;
833 * Indicate to the mid and upper layers that an ATA command has
834 * completed. To be used from EH.
835 */
836void ata_eh_qc_complete(struct ata_queued_cmd *qc)
837{
838 struct scsi_cmnd *scmd = qc->scsicmd;
839 scmd->retries = scmd->allowed;
840 __ata_eh_qc_complete(qc);
841}
842 798
843/** 799 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
844 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH 800 return queue_depth;
845 * @qc: Command to retry
846 *
847 * Indicate to the mid and upper layers that an ATA command
848 * should be retried. To be used from EH.
849 *
850 * SCSI midlayer limits the number of retries to scmd->allowed.
851 * This function might need to adjust scmd->retries for commands
852 * which get retried due to unrelated NCQ failures.
853 */
854void ata_eh_qc_retry(struct ata_queued_cmd *qc)
855{
856 __ata_eh_qc_complete(qc);
857} 801}
858 802
859/** 803/**
@@ -891,7 +835,7 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
891 tf->nsect = 1; /* 1 sector, lba=0 */ 835 tf->nsect = 1; /* 1 sector, lba=0 */
892 836
893 if (qc->dev->flags & ATA_DFLAG_LBA) { 837 if (qc->dev->flags & ATA_DFLAG_LBA) {
894 qc->tf.flags |= ATA_TFLAG_LBA; 838 tf->flags |= ATA_TFLAG_LBA;
895 839
896 tf->lbah = 0x0; 840 tf->lbah = 0x0;
897 tf->lbam = 0x0; 841 tf->lbam = 0x0;
@@ -1195,6 +1139,7 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
1195 u64 block; 1139 u64 block;
1196 u32 n_block; 1140 u32 n_block;
1197 1141
1142 qc->flags |= ATA_QCFLAG_IO;
1198 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1143 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1199 1144
1200 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 || 1145 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
@@ -1241,7 +1186,36 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicm
1241 */ 1186 */
1242 goto nothing_to_do; 1187 goto nothing_to_do;
1243 1188
1244 if (dev->flags & ATA_DFLAG_LBA) { 1189 if ((dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ)) == ATA_DFLAG_NCQ) {
1190 /* yay, NCQ */
1191 if (!lba_48_ok(block, n_block))
1192 goto out_of_range;
1193
1194 tf->protocol = ATA_PROT_NCQ;
1195 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1196
1197 if (tf->flags & ATA_TFLAG_WRITE)
1198 tf->command = ATA_CMD_FPDMA_WRITE;
1199 else
1200 tf->command = ATA_CMD_FPDMA_READ;
1201
1202 qc->nsect = n_block;
1203
1204 tf->nsect = qc->tag << 3;
1205 tf->hob_feature = (n_block >> 8) & 0xff;
1206 tf->feature = n_block & 0xff;
1207
1208 tf->hob_lbah = (block >> 40) & 0xff;
1209 tf->hob_lbam = (block >> 32) & 0xff;
1210 tf->hob_lbal = (block >> 24) & 0xff;
1211 tf->lbah = (block >> 16) & 0xff;
1212 tf->lbam = (block >> 8) & 0xff;
1213 tf->lbal = block & 0xff;
1214
1215 tf->device = 1 << 6;
1216 if (tf->flags & ATA_TFLAG_FUA)
1217 tf->device |= 1 << 7;
1218 } else if (dev->flags & ATA_DFLAG_LBA) {
1245 tf->flags |= ATA_TFLAG_LBA; 1219 tf->flags |= ATA_TFLAG_LBA;
1246 1220
1247 if (lba_28_ok(block, n_block)) { 1221 if (lba_28_ok(block, n_block)) {
@@ -1332,6 +1306,17 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1332 u8 *cdb = cmd->cmnd; 1306 u8 *cdb = cmd->cmnd;
1333 int need_sense = (qc->err_mask != 0); 1307 int need_sense = (qc->err_mask != 0);
1334 1308
1309 /* We snoop the SET_FEATURES - Write Cache ON/OFF command, and
1310 * schedule EH_REVALIDATE operation to update the IDENTIFY DEVICE
1311 * cache
1312 */
1313 if (!need_sense && (qc->tf.command == ATA_CMD_SET_FEATURES) &&
1314 ((qc->tf.feature == SETFEATURES_WC_ON) ||
1315 (qc->tf.feature == SETFEATURES_WC_OFF))) {
1316 qc->ap->eh_info.action |= ATA_EH_REVALIDATE;
1317 ata_port_schedule_eh(qc->ap);
1318 }
1319
1335 /* For ATA pass thru (SAT) commands, generate a sense block if 1320 /* For ATA pass thru (SAT) commands, generate a sense block if
1336 * user mandated it or if there's an error. Note that if we 1321 * user mandated it or if there's an error. Note that if we
1337 * generate because the user forced us to, a check condition 1322 * generate because the user forced us to, a check condition
@@ -1356,10 +1341,8 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1356 } 1341 }
1357 } 1342 }
1358 1343
1359 if (need_sense) { 1344 if (need_sense && !qc->ap->ops->error_handler)
1360 /* The ata_gen_..._sense routines fill in tf */ 1345 ata_dump_status(qc->ap->id, &qc->result_tf);
1361 ata_dump_status(qc->ap->id, &qc->tf);
1362 }
1363 1346
1364 qc->scsidone(cmd); 1347 qc->scsidone(cmd);
1365 1348
@@ -1367,8 +1350,40 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1367} 1350}
1368 1351
1369/** 1352/**
1353 * ata_scmd_need_defer - Check whether we need to defer scmd
1354 * @dev: ATA device to which the command is addressed
1355 * @is_io: Is the command IO (and thus possibly NCQ)?
1356 *
1357 * NCQ and non-NCQ commands cannot run together. As upper layer
1358 * only knows the queue depth, we are responsible for maintaining
1359 * exclusion. This function checks whether a new command can be
1360 * issued to @dev.
1361 *
1362 * LOCKING:
1363 * spin_lock_irqsave(host_set lock)
1364 *
1365 * RETURNS:
1366 * 1 if deferring is needed, 0 otherwise.
1367 */
1368static int ata_scmd_need_defer(struct ata_device *dev, int is_io)
1369{
1370 struct ata_port *ap = dev->ap;
1371
1372 if (!(dev->flags & ATA_DFLAG_NCQ))
1373 return 0;
1374
1375 if (is_io) {
1376 if (!ata_tag_valid(ap->active_tag))
1377 return 0;
1378 } else {
1379 if (!ata_tag_valid(ap->active_tag) && !ap->sactive)
1380 return 0;
1381 }
1382 return 1;
1383}
1384
1385/**
1370 * ata_scsi_translate - Translate then issue SCSI command to ATA device 1386 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1371 * @ap: ATA port to which the command is addressed
1372 * @dev: ATA device to which the command is addressed 1387 * @dev: ATA device to which the command is addressed
1373 * @cmd: SCSI command to execute 1388 * @cmd: SCSI command to execute
1374 * @done: SCSI command completion function 1389 * @done: SCSI command completion function
@@ -1389,19 +1404,25 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1389 * 1404 *
1390 * LOCKING: 1405 * LOCKING:
1391 * spin_lock_irqsave(host_set lock) 1406 * spin_lock_irqsave(host_set lock)
1407 *
1408 * RETURNS:
1409 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1410 * needs to be deferred.
1392 */ 1411 */
1393 1412static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1394static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1395 struct scsi_cmnd *cmd,
1396 void (*done)(struct scsi_cmnd *), 1413 void (*done)(struct scsi_cmnd *),
1397 ata_xlat_func_t xlat_func) 1414 ata_xlat_func_t xlat_func)
1398{ 1415{
1399 struct ata_queued_cmd *qc; 1416 struct ata_queued_cmd *qc;
1400 u8 *scsicmd = cmd->cmnd; 1417 u8 *scsicmd = cmd->cmnd;
1418 int is_io = xlat_func == ata_scsi_rw_xlat;
1401 1419
1402 VPRINTK("ENTER\n"); 1420 VPRINTK("ENTER\n");
1403 1421
1404 qc = ata_scsi_qc_new(ap, dev, cmd, done); 1422 if (unlikely(ata_scmd_need_defer(dev, is_io)))
1423 goto defer;
1424
1425 qc = ata_scsi_qc_new(dev, cmd, done);
1405 if (!qc) 1426 if (!qc)
1406 goto err_mem; 1427 goto err_mem;
1407 1428
@@ -1409,8 +1430,8 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1409 if (cmd->sc_data_direction == DMA_FROM_DEVICE || 1430 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1410 cmd->sc_data_direction == DMA_TO_DEVICE) { 1431 cmd->sc_data_direction == DMA_TO_DEVICE) {
1411 if (unlikely(cmd->request_bufflen < 1)) { 1432 if (unlikely(cmd->request_bufflen < 1)) {
1412 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", 1433 ata_dev_printk(dev, KERN_WARNING,
1413 ap->id, dev->devno); 1434 "WARNING: zero len r/w req\n");
1414 goto err_did; 1435 goto err_did;
1415 } 1436 }
1416 1437
@@ -1432,13 +1453,13 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1432 ata_qc_issue(qc); 1453 ata_qc_issue(qc);
1433 1454
1434 VPRINTK("EXIT\n"); 1455 VPRINTK("EXIT\n");
1435 return; 1456 return 0;
1436 1457
1437early_finish: 1458early_finish:
1438 ata_qc_free(qc); 1459 ata_qc_free(qc);
1439 done(cmd); 1460 done(cmd);
1440 DPRINTK("EXIT - early finish (good or error)\n"); 1461 DPRINTK("EXIT - early finish (good or error)\n");
1441 return; 1462 return 0;
1442 1463
1443err_did: 1464err_did:
1444 ata_qc_free(qc); 1465 ata_qc_free(qc);
@@ -1446,7 +1467,11 @@ err_mem:
1446 cmd->result = (DID_ERROR << 16); 1467 cmd->result = (DID_ERROR << 16);
1447 done(cmd); 1468 done(cmd);
1448 DPRINTK("EXIT - internal\n"); 1469 DPRINTK("EXIT - internal\n");
1449 return; 1470 return 0;
1471
1472defer:
1473 DPRINTK("EXIT - defer\n");
1474 return SCSI_MLQUEUE_DEVICE_BUSY;
1450} 1475}
1451 1476
1452/** 1477/**
@@ -1944,7 +1969,7 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1944 return 0; 1969 return 0;
1945 1970
1946 dpofua = 0; 1971 dpofua = 0;
1947 if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 && 1972 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
1948 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count)) 1973 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1949 dpofua = 1 << 4; 1974 dpofua = 1 << 4;
1950 1975
@@ -2137,13 +2162,14 @@ void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8
2137 2162
2138static void atapi_sense_complete(struct ata_queued_cmd *qc) 2163static void atapi_sense_complete(struct ata_queued_cmd *qc)
2139{ 2164{
2140 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) 2165 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
2141 /* FIXME: not quite right; we don't want the 2166 /* FIXME: not quite right; we don't want the
2142 * translation of taskfile registers into 2167 * translation of taskfile registers into
2143 * a sense descriptors, since that's only 2168 * a sense descriptors, since that's only
2144 * correct for ATA, not ATAPI 2169 * correct for ATA, not ATAPI
2145 */ 2170 */
2146 ata_gen_ata_desc_sense(qc); 2171 ata_gen_ata_desc_sense(qc);
2172 }
2147 2173
2148 qc->scsidone(qc->scsicmd); 2174 qc->scsidone(qc->scsicmd);
2149 ata_qc_free(qc); 2175 ata_qc_free(qc);
@@ -2207,21 +2233,38 @@ static void atapi_qc_complete(struct ata_queued_cmd *qc)
2207 2233
2208 VPRINTK("ENTER, err_mask 0x%X\n", err_mask); 2234 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2209 2235
2236 /* handle completion from new EH */
2237 if (unlikely(qc->ap->ops->error_handler &&
2238 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2239
2240 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2241 /* FIXME: not quite right; we don't want the
2242 * translation of taskfile registers into a
2243 * sense descriptors, since that's only
2244 * correct for ATA, not ATAPI
2245 */
2246 ata_gen_ata_desc_sense(qc);
2247 }
2248
2249 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2250 qc->scsidone(cmd);
2251 ata_qc_free(qc);
2252 return;
2253 }
2254
2255 /* successful completion or old EH failure path */
2210 if (unlikely(err_mask & AC_ERR_DEV)) { 2256 if (unlikely(err_mask & AC_ERR_DEV)) {
2211 cmd->result = SAM_STAT_CHECK_CONDITION; 2257 cmd->result = SAM_STAT_CHECK_CONDITION;
2212 atapi_request_sense(qc); 2258 atapi_request_sense(qc);
2213 return; 2259 return;
2214 } 2260 } else if (unlikely(err_mask)) {
2215
2216 else if (unlikely(err_mask))
2217 /* FIXME: not quite right; we don't want the 2261 /* FIXME: not quite right; we don't want the
2218 * translation of taskfile registers into 2262 * translation of taskfile registers into
2219 * a sense descriptors, since that's only 2263 * a sense descriptors, since that's only
2220 * correct for ATA, not ATAPI 2264 * correct for ATA, not ATAPI
2221 */ 2265 */
2222 ata_gen_ata_desc_sense(qc); 2266 ata_gen_ata_desc_sense(qc);
2223 2267 } else {
2224 else {
2225 u8 *scsicmd = cmd->cmnd; 2268 u8 *scsicmd = cmd->cmnd;
2226 2269
2227 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) { 2270 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
@@ -2303,11 +2346,9 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
2303 qc->tf.protocol = ATA_PROT_ATAPI_DMA; 2346 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2304 qc->tf.feature |= ATAPI_PKT_DMA; 2347 qc->tf.feature |= ATAPI_PKT_DMA;
2305 2348
2306#ifdef ATAPI_ENABLE_DMADIR 2349 if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE))
2307 /* some SATA bridges need us to indicate data xfer direction */ 2350 /* some SATA bridges need us to indicate data xfer direction */
2308 if (cmd->sc_data_direction != DMA_TO_DEVICE)
2309 qc->tf.feature |= ATAPI_DMADIR; 2351 qc->tf.feature |= ATAPI_DMADIR;
2310#endif
2311 } 2352 }
2312 2353
2313 qc->nbytes = cmd->request_bufflen; 2354 qc->nbytes = cmd->request_bufflen;
@@ -2315,6 +2356,53 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
2315 return 0; 2356 return 0;
2316} 2357}
2317 2358
2359static struct ata_device * ata_find_dev(struct ata_port *ap, int id)
2360{
2361 if (likely(id < ATA_MAX_DEVICES))
2362 return &ap->device[id];
2363 return NULL;
2364}
2365
2366static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
2367 const struct scsi_device *scsidev)
2368{
2369 /* skip commands not addressed to targets we simulate */
2370 if (unlikely(scsidev->channel || scsidev->lun))
2371 return NULL;
2372
2373 return ata_find_dev(ap, scsidev->id);
2374}
2375
2376/**
2377 * ata_scsi_dev_enabled - determine if device is enabled
2378 * @dev: ATA device
2379 *
2380 * Determine if commands should be sent to the specified device.
2381 *
2382 * LOCKING:
2383 * spin_lock_irqsave(host_set lock)
2384 *
2385 * RETURNS:
2386 * 0 if commands are not allowed / 1 if commands are allowed
2387 */
2388
2389static int ata_scsi_dev_enabled(struct ata_device *dev)
2390{
2391 if (unlikely(!ata_dev_enabled(dev)))
2392 return 0;
2393
2394 if (!atapi_enabled || (dev->ap->flags & ATA_FLAG_NO_ATAPI)) {
2395 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2396 ata_dev_printk(dev, KERN_WARNING,
2397 "WARNING: ATAPI is %s, device ignored.\n",
2398 atapi_enabled ? "not supported with this driver" : "disabled");
2399 return 0;
2400 }
2401 }
2402
2403 return 1;
2404}
2405
2318/** 2406/**
2319 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd 2407 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2320 * @ap: ATA port to which the device is attached 2408 * @ap: ATA port to which the device is attached
@@ -2331,33 +2419,14 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
2331 * RETURNS: 2419 * RETURNS:
2332 * Associated ATA device, or %NULL if not found. 2420 * Associated ATA device, or %NULL if not found.
2333 */ 2421 */
2334
2335static struct ata_device * 2422static struct ata_device *
2336ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev) 2423ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2337{ 2424{
2338 struct ata_device *dev; 2425 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
2339 2426
2340 /* skip commands not addressed to targets we simulate */ 2427 if (unlikely(!dev || !ata_scsi_dev_enabled(dev)))
2341 if (likely(scsidev->id < ATA_MAX_DEVICES))
2342 dev = &ap->device[scsidev->id];
2343 else
2344 return NULL; 2428 return NULL;
2345 2429
2346 if (unlikely((scsidev->channel != 0) ||
2347 (scsidev->lun != 0)))
2348 return NULL;
2349
2350 if (unlikely(!ata_dev_present(dev)))
2351 return NULL;
2352
2353 if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2354 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2355 printk(KERN_WARNING "ata%u(%u): WARNING: ATAPI is %s, device ignored.\n",
2356 ap->id, dev->devno, atapi_enabled ? "not supported with this driver" : "disabled");
2357 return NULL;
2358 }
2359 }
2360
2361 return dev; 2430 return dev;
2362} 2431}
2363 2432
@@ -2414,10 +2483,15 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
2414{ 2483{
2415 struct ata_taskfile *tf = &(qc->tf); 2484 struct ata_taskfile *tf = &(qc->tf);
2416 struct scsi_cmnd *cmd = qc->scsicmd; 2485 struct scsi_cmnd *cmd = qc->scsicmd;
2486 struct ata_device *dev = qc->dev;
2417 2487
2418 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN) 2488 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
2419 goto invalid_fld; 2489 goto invalid_fld;
2420 2490
2491 /* We may not issue DMA commands if no DMA mode is set */
2492 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2493 goto invalid_fld;
2494
2421 if (scsicmd[1] & 0xe0) 2495 if (scsicmd[1] & 0xe0)
2422 /* PIO multi not supported yet */ 2496 /* PIO multi not supported yet */
2423 goto invalid_fld; 2497 goto invalid_fld;
@@ -2502,6 +2576,9 @@ ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
2502 */ 2576 */
2503 qc->nsect = cmd->request_bufflen / ATA_SECT_SIZE; 2577 qc->nsect = cmd->request_bufflen / ATA_SECT_SIZE;
2504 2578
2579 /* request result TF */
2580 qc->flags |= ATA_QCFLAG_RESULT_TF;
2581
2505 return 0; 2582 return 0;
2506 2583
2507 invalid_fld: 2584 invalid_fld:
@@ -2578,19 +2655,24 @@ static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2578#endif 2655#endif
2579} 2656}
2580 2657
2581static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), 2658static inline int __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
2582 struct ata_port *ap, struct ata_device *dev) 2659 void (*done)(struct scsi_cmnd *),
2660 struct ata_device *dev)
2583{ 2661{
2662 int rc = 0;
2663
2584 if (dev->class == ATA_DEV_ATA) { 2664 if (dev->class == ATA_DEV_ATA) {
2585 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev, 2665 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2586 cmd->cmnd[0]); 2666 cmd->cmnd[0]);
2587 2667
2588 if (xlat_func) 2668 if (xlat_func)
2589 ata_scsi_translate(ap, dev, cmd, done, xlat_func); 2669 rc = ata_scsi_translate(dev, cmd, done, xlat_func);
2590 else 2670 else
2591 ata_scsi_simulate(ap, dev, cmd, done); 2671 ata_scsi_simulate(dev, cmd, done);
2592 } else 2672 } else
2593 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat); 2673 rc = ata_scsi_translate(dev, cmd, done, atapi_xlat);
2674
2675 return rc;
2594} 2676}
2595 2677
2596/** 2678/**
@@ -2609,39 +2691,39 @@ static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struc
2609 * Releases scsi-layer-held lock, and obtains host_set lock. 2691 * Releases scsi-layer-held lock, and obtains host_set lock.
2610 * 2692 *
2611 * RETURNS: 2693 * RETURNS:
2612 * Zero. 2694 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
2695 * 0 otherwise.
2613 */ 2696 */
2614
2615int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) 2697int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2616{ 2698{
2617 struct ata_port *ap; 2699 struct ata_port *ap;
2618 struct ata_device *dev; 2700 struct ata_device *dev;
2619 struct scsi_device *scsidev = cmd->device; 2701 struct scsi_device *scsidev = cmd->device;
2620 struct Scsi_Host *shost = scsidev->host; 2702 struct Scsi_Host *shost = scsidev->host;
2703 int rc = 0;
2621 2704
2622 ap = (struct ata_port *) &shost->hostdata[0]; 2705 ap = ata_shost_to_port(shost);
2623 2706
2624 spin_unlock(shost->host_lock); 2707 spin_unlock(shost->host_lock);
2625 spin_lock(&ap->host_set->lock); 2708 spin_lock(ap->lock);
2626 2709
2627 ata_scsi_dump_cdb(ap, cmd); 2710 ata_scsi_dump_cdb(ap, cmd);
2628 2711
2629 dev = ata_scsi_find_dev(ap, scsidev); 2712 dev = ata_scsi_find_dev(ap, scsidev);
2630 if (likely(dev)) 2713 if (likely(dev))
2631 __ata_scsi_queuecmd(cmd, done, ap, dev); 2714 rc = __ata_scsi_queuecmd(cmd, done, dev);
2632 else { 2715 else {
2633 cmd->result = (DID_BAD_TARGET << 16); 2716 cmd->result = (DID_BAD_TARGET << 16);
2634 done(cmd); 2717 done(cmd);
2635 } 2718 }
2636 2719
2637 spin_unlock(&ap->host_set->lock); 2720 spin_unlock(ap->lock);
2638 spin_lock(shost->host_lock); 2721 spin_lock(shost->host_lock);
2639 return 0; 2722 return rc;
2640} 2723}
2641 2724
2642/** 2725/**
2643 * ata_scsi_simulate - simulate SCSI command on ATA device 2726 * ata_scsi_simulate - simulate SCSI command on ATA device
2644 * @ap: port the device is connected to
2645 * @dev: the target device 2727 * @dev: the target device
2646 * @cmd: SCSI command being sent to device. 2728 * @cmd: SCSI command being sent to device.
2647 * @done: SCSI command completion function. 2729 * @done: SCSI command completion function.
@@ -2653,14 +2735,12 @@ int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2653 * spin_lock_irqsave(host_set lock) 2735 * spin_lock_irqsave(host_set lock)
2654 */ 2736 */
2655 2737
2656void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev, 2738void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
2657 struct scsi_cmnd *cmd,
2658 void (*done)(struct scsi_cmnd *)) 2739 void (*done)(struct scsi_cmnd *))
2659{ 2740{
2660 struct ata_scsi_args args; 2741 struct ata_scsi_args args;
2661 const u8 *scsicmd = cmd->cmnd; 2742 const u8 *scsicmd = cmd->cmnd;
2662 2743
2663 args.ap = ap;
2664 args.dev = dev; 2744 args.dev = dev;
2665 args.id = dev->id; 2745 args.id = dev->id;
2666 args.cmd = cmd; 2746 args.cmd = cmd;
@@ -2732,17 +2812,241 @@ void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
2732 2812
2733void ata_scsi_scan_host(struct ata_port *ap) 2813void ata_scsi_scan_host(struct ata_port *ap)
2734{ 2814{
2735 struct ata_device *dev;
2736 unsigned int i; 2815 unsigned int i;
2737 2816
2738 if (ap->flags & ATA_FLAG_PORT_DISABLED) 2817 if (ap->flags & ATA_FLAG_DISABLED)
2739 return; 2818 return;
2740 2819
2741 for (i = 0; i < ATA_MAX_DEVICES; i++) { 2820 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2742 dev = &ap->device[i]; 2821 struct ata_device *dev = &ap->device[i];
2822 struct scsi_device *sdev;
2743 2823
2744 if (ata_dev_present(dev)) 2824 if (!ata_dev_enabled(dev) || dev->sdev)
2745 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0); 2825 continue;
2826
2827 sdev = __scsi_add_device(ap->host, 0, i, 0, NULL);
2828 if (!IS_ERR(sdev)) {
2829 dev->sdev = sdev;
2830 scsi_device_put(sdev);
2831 }
2832 }
2833}
2834
2835/**
2836 * ata_scsi_offline_dev - offline attached SCSI device
2837 * @dev: ATA device to offline attached SCSI device for
2838 *
2839 * This function is called from ata_eh_hotplug() and responsible
2840 * for taking the SCSI device attached to @dev offline. This
2841 * function is called with host_set lock which protects dev->sdev
2842 * against clearing.
2843 *
2844 * LOCKING:
2845 * spin_lock_irqsave(host_set lock)
2846 *
2847 * RETURNS:
2848 * 1 if attached SCSI device exists, 0 otherwise.
2849 */
2850int ata_scsi_offline_dev(struct ata_device *dev)
2851{
2852 if (dev->sdev) {
2853 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
2854 return 1;
2746 } 2855 }
2856 return 0;
2747} 2857}
2748 2858
2859/**
2860 * ata_scsi_remove_dev - remove attached SCSI device
2861 * @dev: ATA device to remove attached SCSI device for
2862 *
2863 * This function is called from ata_eh_scsi_hotplug() and
2864 * responsible for removing the SCSI device attached to @dev.
2865 *
2866 * LOCKING:
2867 * Kernel thread context (may sleep).
2868 */
2869static void ata_scsi_remove_dev(struct ata_device *dev)
2870{
2871 struct ata_port *ap = dev->ap;
2872 struct scsi_device *sdev;
2873 unsigned long flags;
2874
2875 /* Alas, we need to grab scan_mutex to ensure SCSI device
2876 * state doesn't change underneath us and thus
2877 * scsi_device_get() always succeeds. The mutex locking can
2878 * be removed if there is __scsi_device_get() interface which
2879 * increments reference counts regardless of device state.
2880 */
2881 mutex_lock(&ap->host->scan_mutex);
2882 spin_lock_irqsave(ap->lock, flags);
2883
2884 /* clearing dev->sdev is protected by host_set lock */
2885 sdev = dev->sdev;
2886 dev->sdev = NULL;
2887
2888 if (sdev) {
2889 /* If user initiated unplug races with us, sdev can go
2890 * away underneath us after the host_set lock and
2891 * scan_mutex are released. Hold onto it.
2892 */
2893 if (scsi_device_get(sdev) == 0) {
2894 /* The following ensures the attached sdev is
2895 * offline on return from ata_scsi_offline_dev()
2896 * regardless it wins or loses the race
2897 * against this function.
2898 */
2899 scsi_device_set_state(sdev, SDEV_OFFLINE);
2900 } else {
2901 WARN_ON(1);
2902 sdev = NULL;
2903 }
2904 }
2905
2906 spin_unlock_irqrestore(ap->lock, flags);
2907 mutex_unlock(&ap->host->scan_mutex);
2908
2909 if (sdev) {
2910 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
2911 sdev->sdev_gendev.bus_id);
2912
2913 scsi_remove_device(sdev);
2914 scsi_device_put(sdev);
2915 }
2916}
2917
2918/**
2919 * ata_scsi_hotplug - SCSI part of hotplug
2920 * @data: Pointer to ATA port to perform SCSI hotplug on
2921 *
2922 * Perform SCSI part of hotplug. It's executed from a separate
2923 * workqueue after EH completes. This is necessary because SCSI
2924 * hot plugging requires working EH and hot unplugging is
2925 * synchronized with hot plugging with a mutex.
2926 *
2927 * LOCKING:
2928 * Kernel thread context (may sleep).
2929 */
2930void ata_scsi_hotplug(void *data)
2931{
2932 struct ata_port *ap = data;
2933 int i;
2934
2935 if (ap->flags & ATA_FLAG_UNLOADING) {
2936 DPRINTK("ENTER/EXIT - unloading\n");
2937 return;
2938 }
2939
2940 DPRINTK("ENTER\n");
2941
2942 /* unplug detached devices */
2943 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2944 struct ata_device *dev = &ap->device[i];
2945 unsigned long flags;
2946
2947 if (!(dev->flags & ATA_DFLAG_DETACHED))
2948 continue;
2949
2950 spin_lock_irqsave(ap->lock, flags);
2951 dev->flags &= ~ATA_DFLAG_DETACHED;
2952 spin_unlock_irqrestore(ap->lock, flags);
2953
2954 ata_scsi_remove_dev(dev);
2955 }
2956
2957 /* scan for new ones */
2958 ata_scsi_scan_host(ap);
2959
2960 /* If we scanned while EH was in progress, scan would have
2961 * failed silently. Requeue if there are enabled but
2962 * unattached devices.
2963 */
2964 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2965 struct ata_device *dev = &ap->device[i];
2966 if (ata_dev_enabled(dev) && !dev->sdev) {
2967 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, HZ);
2968 break;
2969 }
2970 }
2971
2972 DPRINTK("EXIT\n");
2973}
2974
2975/**
2976 * ata_scsi_user_scan - indication for user-initiated bus scan
2977 * @shost: SCSI host to scan
2978 * @channel: Channel to scan
2979 * @id: ID to scan
2980 * @lun: LUN to scan
2981 *
2982 * This function is called when user explicitly requests bus
2983 * scan. Set probe pending flag and invoke EH.
2984 *
2985 * LOCKING:
2986 * SCSI layer (we don't care)
2987 *
2988 * RETURNS:
2989 * Zero.
2990 */
2991static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
2992 unsigned int id, unsigned int lun)
2993{
2994 struct ata_port *ap = ata_shost_to_port(shost);
2995 unsigned long flags;
2996 int rc = 0;
2997
2998 if (!ap->ops->error_handler)
2999 return -EOPNOTSUPP;
3000
3001 if ((channel != SCAN_WILD_CARD && channel != 0) ||
3002 (lun != SCAN_WILD_CARD && lun != 0))
3003 return -EINVAL;
3004
3005 spin_lock_irqsave(ap->lock, flags);
3006
3007 if (id == SCAN_WILD_CARD) {
3008 ap->eh_info.probe_mask |= (1 << ATA_MAX_DEVICES) - 1;
3009 ap->eh_info.action |= ATA_EH_SOFTRESET;
3010 } else {
3011 struct ata_device *dev = ata_find_dev(ap, id);
3012
3013 if (dev) {
3014 ap->eh_info.probe_mask |= 1 << dev->devno;
3015 ap->eh_info.action |= ATA_EH_SOFTRESET;
3016 } else
3017 rc = -EINVAL;
3018 }
3019
3020 if (rc == 0)
3021 ata_port_schedule_eh(ap);
3022
3023 spin_unlock_irqrestore(ap->lock, flags);
3024
3025 return rc;
3026}
3027
3028/**
3029 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
3030 * @data: Pointer to ATA port to perform scsi_rescan_device()
3031 *
3032 * After ATA pass thru (SAT) commands are executed successfully,
3033 * libata need to propagate the changes to SCSI layer. This
3034 * function must be executed from ata_aux_wq such that sdev
3035 * attach/detach don't race with rescan.
3036 *
3037 * LOCKING:
3038 * Kernel thread context (may sleep).
3039 */
3040void ata_scsi_dev_rescan(void *data)
3041{
3042 struct ata_port *ap = data;
3043 struct ata_device *dev;
3044 unsigned int i;
3045
3046 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3047 dev = &ap->device[i];
3048
3049 if (ata_dev_enabled(dev) && dev->sdev)
3050 scsi_rescan_device(&(dev->sdev->sdev_gendev));
3051 }
3052}