Skip to content

Commit 12cb5ce

Browse files
shligitaxboe
authored andcommitted
libata: use blk taging
libata uses its own tag management which is duplication and the implementation is poor. And if we switch to blk-mq, tag is build-in. It's time to switch to generic taging. The SAS driver has its own tag management, and looks we can't directly map the host controler tag to SATA tag. So I just bypassed the SAS case. I changed the code/variable name for the tag management of libata to make it self contained. Only sas will use it. Later if libsas implements its tag management, the tag management code in libata can be deleted easily. Cc: Jens Axboe <axboe@fb.com> Cc: Christoph Hellwig <hch@infradead.org> Signed-off-by: Shaohua Li <shli@fb.com> Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Jens Axboe <axboe@fb.com>
1 parent a4a1cc1 commit 12cb5ce

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

drivers/ata/libata-core.c

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,15 @@ static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
15251525
complete(waiting);
15261526
}
15271527

1528+
static bool ata_valid_internal_tag(struct ata_port *ap, struct ata_device *dev,
1529+
unsigned int tag)
1530+
{
1531+
if (!ap->scsi_host)
1532+
return !test_and_set_bit(tag, &ap->sas_tag_allocated);
1533+
return !dev->sdev ||
1534+
!blk_queue_find_tag(dev->sdev->request_queue, tag);
1535+
}
1536+
15281537
/**
15291538
* ata_exec_internal_sg - execute libata internal command
15301539
* @dev: Device to which the command is sent
@@ -1585,8 +1594,7 @@ unsigned ata_exec_internal_sg(struct ata_device *dev,
15851594
else
15861595
tag = 0;
15871596

1588-
if (test_and_set_bit(tag, &ap->qc_allocated))
1589-
BUG();
1597+
BUG_ON(!ata_valid_internal_tag(ap, dev, tag));
15901598
qc = __ata_qc_from_tag(ap, tag);
15911599

15921600
qc->tag = tag;
@@ -4737,34 +4745,48 @@ void swap_buf_le16(u16 *buf, unsigned int buf_words)
47374745
* None.
47384746
*/
47394747

4740-
static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4748+
static struct ata_queued_cmd *sas_ata_qc_new(struct ata_port *ap)
47414749
{
47424750
struct ata_queued_cmd *qc = NULL;
47434751
unsigned int max_queue = ap->host->n_tags;
47444752
unsigned int i, tag;
47454753

4746-
/* no command while frozen */
4747-
if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4748-
return NULL;
4749-
4750-
for (i = 0, tag = ap->last_tag + 1; i < max_queue; i++, tag++) {
4754+
for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
47514755
tag = tag < max_queue ? tag : 0;
47524756

47534757
/* the last tag is reserved for internal command. */
47544758
if (tag == ATA_TAG_INTERNAL)
47554759
continue;
47564760

4757-
if (!test_and_set_bit(tag, &ap->qc_allocated)) {
4761+
if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
47584762
qc = __ata_qc_from_tag(ap, tag);
47594763
qc->tag = tag;
4760-
ap->last_tag = tag;
4764+
ap->sas_last_tag = tag;
47614765
break;
47624766
}
47634767
}
47644768

47654769
return qc;
47664770
}
47674771

4772+
static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap, int blktag)
4773+
{
4774+
struct ata_queued_cmd *qc;
4775+
4776+
/* no command while frozen */
4777+
if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4778+
return NULL;
4779+
4780+
/* SATA will directly use block tag. libsas need its own tag management */
4781+
if (ap->scsi_host) {
4782+
qc = __ata_qc_from_tag(ap, blktag);
4783+
qc->tag = blktag;
4784+
return qc;
4785+
}
4786+
4787+
return sas_ata_qc_new(ap);
4788+
}
4789+
47684790
/**
47694791
* ata_qc_new_init - Request an available ATA command, and initialize it
47704792
* @dev: Device from whom we request an available command structure
@@ -4773,12 +4795,12 @@ static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
47734795
* None.
47744796
*/
47754797

4776-
struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4798+
struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int blktag)
47774799
{
47784800
struct ata_port *ap = dev->link->ap;
47794801
struct ata_queued_cmd *qc;
47804802

4781-
qc = ata_qc_new(ap);
4803+
qc = ata_qc_new(ap, blktag);
47824804
if (qc) {
47834805
qc->scsicmd = NULL;
47844806
qc->ap = ap;
@@ -4800,6 +4822,12 @@ struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
48004822
* LOCKING:
48014823
* spin_lock_irqsave(host lock)
48024824
*/
4825+
static void sas_ata_qc_free(unsigned int tag, struct ata_port *ap)
4826+
{
4827+
if (!ap->scsi_host)
4828+
clear_bit(tag, &ap->sas_tag_allocated);
4829+
}
4830+
48034831
void ata_qc_free(struct ata_queued_cmd *qc)
48044832
{
48054833
struct ata_port *ap;
@@ -4812,7 +4840,7 @@ void ata_qc_free(struct ata_queued_cmd *qc)
48124840
tag = qc->tag;
48134841
if (likely(ata_tag_valid(tag))) {
48144842
qc->tag = ATA_TAG_POISON;
4815-
clear_bit(tag, &ap->qc_allocated);
4843+
sas_ata_qc_free(tag, ap);
48164844
}
48174845
}
48184846

drivers/ata/libata-scsi.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
756756
{
757757
struct ata_queued_cmd *qc;
758758

759-
qc = ata_qc_new_init(dev);
759+
qc = ata_qc_new_init(dev, cmd->request->tag);
760760
if (qc) {
761761
qc->scsicmd = cmd;
762762
qc->scsidone = cmd->scsi_done;
@@ -3666,6 +3666,8 @@ int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
36663666
*/
36673667
shost->max_host_blocked = 1;
36683668

3669+
scsi_init_shared_tag_map(shost, host->n_tags);
3670+
36693671
rc = scsi_add_host_with_dma(ap->scsi_host,
36703672
&ap->tdev, ap->host->dev);
36713673
if (rc)

drivers/ata/libata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extern struct ata_link *ata_dev_phys_link(struct ata_device *dev);
6363
extern void ata_force_cbl(struct ata_port *ap);
6464
extern u64 ata_tf_to_lba(const struct ata_taskfile *tf);
6565
extern u64 ata_tf_to_lba48(const struct ata_taskfile *tf);
66-
extern struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev);
66+
extern struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag);
6767
extern int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
6868
u64 block, u32 n_block, unsigned int tf_flags,
6969
unsigned int tag);

include/linux/libata.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,10 @@ struct ata_port {
821821
unsigned int cbl; /* cable type; ATA_CBL_xxx */
822822

823823
struct ata_queued_cmd qcmd[ATA_MAX_QUEUE];
824-
unsigned long qc_allocated;
824+
unsigned long sas_tag_allocated; /* for sas tag allocation only */
825825
unsigned int qc_active;
826826
int nr_active_links; /* #links with active qcs */
827-
unsigned int last_tag; /* track next tag hw expects */
827+
unsigned int sas_last_tag; /* track next tag hw expects */
828828

829829
struct ata_link link; /* host default link */
830830
struct ata_link *slave_link; /* see ata_slave_link_init() */
@@ -1344,6 +1344,7 @@ extern struct device_attribute *ata_common_sdev_attrs[];
13441344
.ioctl = ata_scsi_ioctl, \
13451345
.queuecommand = ata_scsi_queuecmd, \
13461346
.can_queue = ATA_DEF_QUEUE, \
1347+
.tag_alloc_policy = BLK_TAG_ALLOC_RR, \
13471348
.this_id = ATA_SHT_THIS_ID, \
13481349
.cmd_per_lun = ATA_SHT_CMD_PER_LUN, \
13491350
.emulated = ATA_SHT_EMULATED, \

0 commit comments

Comments
 (0)