Skip to content

Commit 759e875

Browse files
committed
ata: libata-scsi: do not needlessly defer commands when using PMP with FBS
The ACS specification does not allow a non-NCQ command to be issued while an NCQ command is outstanding. Commit 0ea8408 ("ata: libata-scsi: avoid Non-NCQ command starvation") introduced a feature where a deferred non-NCQ command gets issued from a workqueue. The design stores a single non-NCQ command per port. However, when using Port Multipliers (PMPs), specifically PMPs that support FIS-Based Switching (FBS), non-NCQ and NCQ commands can be mixed on the same port, just not for the same link, see e.g. ata_std_qc_defer() which is, and always has operated on a per-link basis. Therefore, move the deferred_qc from struct ata_port to struct ata_link. This way, when using a PMP with FBS, we will not needlessly defer commands to all other links, just because one link issued a non-NCQ command while having an NCQ command outstanding. Only commands for that specific link will be deferred. This is in line with how PMPs with FBS worked before commit 0ea8408 ("ata: libata-scsi: avoid Non-NCQ command starvation"). Fixes: 0ea8408 ("ata: libata-scsi: avoid Non-NCQ command starvation") Tested-by: Tommy Kelly <linux@tkel.ly> Reviewed-by: Damien Le Moal <dlemoal@kernel.org> Signed-off-by: Niklas Cassel <cassel@kernel.org>
1 parent f233124 commit 759e875

5 files changed

Lines changed: 42 additions & 29 deletions

File tree

drivers/ata/libata-core.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5584,6 +5584,7 @@ void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
55845584
link->pmp = pmp;
55855585
link->active_tag = ATA_TAG_POISON;
55865586
link->hw_sata_spd_limit = UINT_MAX;
5587+
INIT_WORK(&link->deferred_qc_work, ata_scsi_deferred_qc_work);
55875588

55885589
/* can't use iterator, ap isn't initialized yet */
55895590
for (i = 0; i < ATA_MAX_DEVICES; i++) {
@@ -5666,7 +5667,6 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
56665667
mutex_init(&ap->scsi_scan_mutex);
56675668
INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
56685669
INIT_DELAYED_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5669-
INIT_WORK(&ap->deferred_qc_work, ata_scsi_deferred_qc_work);
56705670
INIT_LIST_HEAD(&ap->eh_done_q);
56715671
init_waitqueue_head(&ap->eh_wait_q);
56725672
init_completion(&ap->park_req_pending);
@@ -6291,12 +6291,15 @@ static void ata_port_detach(struct ata_port *ap)
62916291

62926292
/* It better be dead now and not have any remaining deferred qc. */
62936293
WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6294-
WARN_ON(ap->deferred_qc);
62956294

6296-
cancel_work_sync(&ap->deferred_qc_work);
62976295
cancel_delayed_work_sync(&ap->hotplug_task);
62986296
cancel_delayed_work_sync(&ap->scsi_rescan_task);
62996297

6298+
ata_for_each_link(link, ap, PMP_FIRST) {
6299+
WARN_ON(link->deferred_qc);
6300+
cancel_work_sync(&link->deferred_qc_work);
6301+
}
6302+
63006303
/* Delete port multiplier link transport devices */
63016304
if (ap->pmp_link) {
63026305
int i;

drivers/ata/libata-eh.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,11 @@ int ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
651651
if (qc->scsicmd != scmd)
652652
continue;
653653
if ((qc->flags & ATA_QCFLAG_ACTIVE) ||
654-
qc == ap->deferred_qc)
654+
qc == qc->dev->link->deferred_qc)
655655
break;
656656
}
657657

658-
if (i < ATA_MAX_QUEUE && qc == ap->deferred_qc) {
658+
if (i < ATA_MAX_QUEUE && qc == qc->dev->link->deferred_qc) {
659659
/*
660660
* This is a deferred command that timed out while
661661
* waiting for the command queue to drain. Since the qc
@@ -666,8 +666,8 @@ int ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
666666
* deferred qc work from issuing this qc.
667667
*/
668668
WARN_ON_ONCE(qc->flags & ATA_QCFLAG_ACTIVE);
669-
ap->deferred_qc = NULL;
670-
cancel_work(&ap->deferred_qc_work);
669+
qc->dev->link->deferred_qc = NULL;
670+
cancel_work(&qc->dev->link->deferred_qc_work);
671671
set_host_byte(scmd, DID_TIME_OUT);
672672
scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
673673
} else if (i < ATA_MAX_QUEUE) {

drivers/ata/libata-pmp.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,11 @@ static void sata_pmp_detach(struct ata_device *dev)
582582
if (ap->ops->pmp_detach)
583583
ap->ops->pmp_detach(ap);
584584

585-
ata_for_each_link(tlink, ap, EDGE)
585+
ata_for_each_link(tlink, ap, EDGE) {
586+
WARN_ON(tlink->deferred_qc);
587+
cancel_work_sync(&tlink->deferred_qc_work);
586588
ata_eh_detach_dev(tlink->device);
589+
}
587590

588591
spin_lock_irqsave(ap->lock, flags);
589592
ap->nr_pmp_links = 0;

drivers/ata/libata-scsi.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,8 +1664,9 @@ static void ata_scsi_qc_done(struct ata_queued_cmd *qc, bool set_result,
16641664

16651665
void ata_scsi_deferred_qc_work(struct work_struct *work)
16661666
{
1667-
struct ata_port *ap =
1668-
container_of(work, struct ata_port, deferred_qc_work);
1667+
struct ata_link *link =
1668+
container_of(work, struct ata_link, deferred_qc_work);
1669+
struct ata_port *ap = link->ap;
16691670
struct ata_queued_cmd *qc;
16701671
unsigned long flags;
16711672

@@ -1676,10 +1677,10 @@ void ata_scsi_deferred_qc_work(struct work_struct *work)
16761677
* such case, we should not need any more deferring the qc, so warn if
16771678
* qc_defer() says otherwise.
16781679
*/
1679-
qc = ap->deferred_qc;
1680+
qc = link->deferred_qc;
16801681
if (qc && !ata_port_eh_scheduled(ap)) {
16811682
WARN_ON_ONCE(ap->ops->qc_defer(qc));
1682-
ap->deferred_qc = NULL;
1683+
link->deferred_qc = NULL;
16831684
ata_qc_issue(qc);
16841685
}
16851686

@@ -1688,7 +1689,7 @@ void ata_scsi_deferred_qc_work(struct work_struct *work)
16881689

16891690
void ata_scsi_requeue_deferred_qc(struct ata_port *ap)
16901691
{
1691-
struct ata_queued_cmd *qc = ap->deferred_qc;
1692+
struct ata_link *link;
16921693

16931694
lockdep_assert_held(ap->lock);
16941695

@@ -1697,16 +1698,21 @@ void ata_scsi_requeue_deferred_qc(struct ata_port *ap)
16971698
* do not try to be smart about what to do with this deferred command
16981699
* and simply requeue it by completing it with DID_REQUEUE.
16991700
*/
1700-
if (qc) {
1701-
ap->deferred_qc = NULL;
1702-
cancel_work(&ap->deferred_qc_work);
1703-
ata_scsi_qc_done(qc, true, DID_REQUEUE << 16);
1701+
ata_for_each_link(link, ap, PMP_FIRST) {
1702+
struct ata_queued_cmd *qc = link->deferred_qc;
1703+
1704+
if (qc) {
1705+
link->deferred_qc = NULL;
1706+
cancel_work(&link->deferred_qc_work);
1707+
ata_scsi_qc_done(qc, true, DID_REQUEUE << 16);
1708+
}
17041709
}
17051710
}
17061711

1707-
static void ata_scsi_schedule_deferred_qc(struct ata_port *ap)
1712+
static void ata_scsi_schedule_deferred_qc(struct ata_link *link)
17081713
{
1709-
struct ata_queued_cmd *qc = ap->deferred_qc;
1714+
struct ata_queued_cmd *qc = link->deferred_qc;
1715+
struct ata_port *ap = link->ap;
17101716

17111717
lockdep_assert_held(ap->lock);
17121718

@@ -1723,12 +1729,12 @@ static void ata_scsi_schedule_deferred_qc(struct ata_port *ap)
17231729
return;
17241730
}
17251731
if (!ap->ops->qc_defer(qc))
1726-
queue_work(system_highpri_wq, &ap->deferred_qc_work);
1732+
queue_work(system_highpri_wq, &link->deferred_qc_work);
17271733
}
17281734

17291735
static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
17301736
{
1731-
struct ata_port *ap = qc->ap;
1737+
struct ata_link *link = qc->dev->link;
17321738
struct scsi_cmnd *cmd = qc->scsicmd;
17331739
u8 *cdb = cmd->cmnd;
17341740
bool have_sense = qc->flags & ATA_QCFLAG_SENSE_VALID;
@@ -1759,11 +1765,12 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
17591765

17601766
ata_scsi_qc_done(qc, false, 0);
17611767

1762-
ata_scsi_schedule_deferred_qc(ap);
1768+
ata_scsi_schedule_deferred_qc(link);
17631769
}
17641770

17651771
static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
17661772
{
1773+
struct ata_link *link = qc->dev->link;
17671774
int ret;
17681775

17691776
if (!ap->ops->qc_defer)
@@ -1774,7 +1781,7 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
17741781
* requeue and defer all incoming commands until the deferred qc is
17751782
* processed, once all on-going commands complete.
17761783
*/
1777-
if (ap->deferred_qc) {
1784+
if (link->deferred_qc) {
17781785
ata_qc_free(qc);
17791786
return SCSI_MLQUEUE_DEVICE_BUSY;
17801787
}
@@ -1790,8 +1797,8 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
17901797
case ATA_DEFER_LINK_EXCL:
17911798
/*
17921799
* Drivers making use of ap->excl_link cannot store the QC in
1793-
* ap->deferred_qc, because the ap->excl_link handling is
1794-
* incompatible with the ap->deferred_qc workqueue handling.
1800+
* link->deferred_qc, because the ap->excl_link handling is
1801+
* incompatible with the link->deferred_qc workqueue handling.
17951802
*/
17961803
ret = SCSI_MLQUEUE_DEVICE_BUSY;
17971804
goto free_qc;
@@ -1817,7 +1824,7 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
18171824
* commands complete.
18181825
*/
18191826
if (!ata_is_ncq(qc->tf.protocol)) {
1820-
ap->deferred_qc = qc;
1827+
link->deferred_qc = qc;
18211828
return 0;
18221829
}
18231830

include/linux/libata.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,9 @@ struct ata_link {
855855
unsigned int sata_spd; /* current SATA PHY speed */
856856
enum ata_lpm_policy lpm_policy;
857857

858+
struct work_struct deferred_qc_work;
859+
struct ata_queued_cmd *deferred_qc;
860+
858861
/* record runtime error info, protected by host_set lock */
859862
struct ata_eh_info eh_info;
860863
/* EH context */
@@ -900,9 +903,6 @@ struct ata_port {
900903
u64 qc_active;
901904
int nr_active_links; /* #links with active qcs */
902905

903-
struct work_struct deferred_qc_work;
904-
struct ata_queued_cmd *deferred_qc;
905-
906906
struct ata_link link; /* host default link */
907907
struct ata_link *slave_link; /* see ata_slave_link_init() */
908908

0 commit comments

Comments
 (0)