Skip to contentSkip to conversation

ahci: add support for VIA VT8251

PATCH/RFC In review Bastiaan Jacques 4 participants 15 messages Apr 11, 2006 – Apr 17, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

This patch adds AHCI support for the VIA VT8251 chipset. The patch does so by:
1) Adding the PCI device ID.
2) Adding a workaround in ahci_probe_reset() for a VIA hardware bug.

---

The patch is based upon the patch[1] provided by VIA. With the softreset and probing work that has been merged, this patch has become much simpler.

I am happy to report that without the workaround in ahci_probe_reset(), the chipset works quite well; both my SATA drives are recognised. However, softreset does not work (but hardreset does), and ata_std_probeinit delays, for my primary SATA drive/port, due to the "busy" bug. A Command List Override will unbusy the drive/port.

Since the CLO sequence also appears in ahci_softreset(), I have moved that code into a new ahci_clo() function.

This patch is against 2.6.17-rc1 (and -mm2).

This patch has only been tested by yours truly. Whilst I am waiting for feedback from other people, I am sending this patch to solicit comments.

NOTE: My email client cuts off some lines (of context) in my patch because they are longer than 78 characters. Therefore, I am making the patch available from my web server [2], as well.

Bastiaan

[1] http://lkml.org/lkml/2005/12/11/204
[2] http://mirrors.pagefault.net/linux-via-ahci-latest.patch

scsi/ahci.c renamed from scsi/ahci.c.orig +13 -0
@@ -297,6 +297,8 @@
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,6 +537,17 @@
 	return -1;
 }
 
+static void ahci_clo(struct ata_port *ap)
+{
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
+	u32 tmp;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+	readl(port_mmio + PORT_CMD); /* flush */
+}
+
 static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int 

*class)
{
struct ahci_host_priv *hpriv = ap->host_set->private_data;
@@ -565,7 +578,6 @@
/* check BUSY/DRQ, perform Command List Override if necessary */ ahci_tf_read(ap, &tf);
if (tf.command & (ATA_BUSY | ATA_DRQ)) {
- u32 tmp;

if (!(hpriv->cap & HOST_CAP_CLO)) {
rc = -EIO;
@@ -573,10 +585,7 @@
goto fail_restart;
}

- tmp = readl(port_mmio + PORT_CMD);
- tmp |= PORT_CMD_CLO;
- writel(tmp, port_mmio + PORT_CMD);
- readl(port_mmio + PORT_CMD); /* flush */
+ ahci_clo(ap);

if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
1, 500)) {
@@ -695,6 +704,16 @@

static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes) {
+ struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
+
+ /* Workaround for VIA VT8251 "busy" bug */
+ if (pdev->device == 0x3349) {
+ if (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY) {
+ /* ATA_BUSY hasn't cleared, so send a CLO */
+ ahci_clo(ap);
+ }
+ }
+
return ata_drive_probe_reset(ap, ata_std_probeinit,
ahci_softreset, ahci_hardreset, ahci_postreset, classes);

#1 lore raw
Quoted 36 lines · On Tue, 11 Apr 2006 19:15:45 +0200 Bastiaan Jacques wrote:

This patch adds AHCI support for the VIA VT8251 chipset. The patch does so by:
1) Adding the PCI device ID.
2) Adding a workaround in ahci_probe_reset() for a VIA hardware bug.

---

The patch is based upon the patch[1] provided by VIA. With the softreset and probing work that has been merged, this patch has become much simpler.

I am happy to report that without the workaround in ahci_probe_reset(), the chipset works quite well; both my SATA drives are recognised. However, softreset does not work (but hardreset does), and ata_std_probeinit delays, for my primary SATA drive/port, due to the "busy" bug. A Command List Override will unbusy the drive/port.

Since the CLO sequence also appears in ahci_softreset(), I have moved that code into a new ahci_clo() function.

This patch is against 2.6.17-rc1 (and -mm2).

This patch has only been tested by yours truly. Whilst I am waiting for feedback from other people, I am sending this patch to solicit comments.

NOTE: My email client cuts off some lines (of context) in my patch because they are longer than 78 characters. Therefore, I am making the patch available from my web server [2], as well.

Bastiaan

[1] http://lkml.org/lkml/2005/12/11/204
[2] http://mirrors.pagefault.net/linux-via-ahci-latest.patch

scsi/ahci.c line 301

Quoted 5 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +2 -0
@@ -297,6 +297,8 @@
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci }, /* VT8251 */

You can add a separate host type (e.g., board_via_vt8251_ahci), and then add an entry for this type to ahci_port_info[]. The entry would have a special flag in .host_flags, but the same .port_ops, so there will not be excessive duplication like in the original vendor patch.

Look like other drivers which need workarounds for hardware bugs (e.g., sata_sil) do this.

Quoted 23 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +11 -0
 	{ }	/* terminate list */
 };
 
@@ -535,6 +537,17 @@
 	return -1;
 }
 
+static void ahci_clo(struct ata_port *ap)
+{
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
+	u32 tmp;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+	readl(port_mmio + PORT_CMD); /* flush */
+}
+
 static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int 
 *class)
 {
scsi/ahci.c renamed from scsi/ahci.c.orig +0 -0
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;

scsi/ahci.c line 581

Quoted 5 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +0 -1
@@ -565,7 +578,6 @@
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;
 

The following empty line should probably be deleted too.

Quoted 15 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +1 -4
 		if (!(hpriv->cap & HOST_CAP_CLO)) {
 			rc = -EIO;
@@ -573,10 +585,7 @@
 			goto fail_restart;
 		}
 
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
-		readl(port_mmio + PORT_CMD); /* flush */
+		ahci_clo(ap);
 
 		if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
 				       1, 500)) {

scsi/ahci.c line 710

Quoted 7 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +4 -0
@@ -695,6 +704,16 @@
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
+
+	/* Workaround for VIA VT8251 "busy" bug */
+	if (pdev->device == 0x3349) {

After adding a separate host type and a host flag this will be:

	if (ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) {
Quoted 9 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +6 -0
+		if (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY) {
+			/* ATA_BUSY hasn't cleared, so send a CLO */
+			ahci_clo(ap);
+		}
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);
#2 lore raw
Quoted 7 lines · Sergey Vlasov

Review line 568

Quoted 5 lines
(quoted diff) +0 -1
@@ -565,7 +578,6 @@
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;

The following empty line should probably be deleted too.

Done.

Quoted 10 lines

Review line 582

Quoted 2 lines
(quoted diff) +2 -0
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci }, /* VT8251 */

You can add a separate host type (e.g., board_via_vt8251_ahci), and then add an entry for this type to ahci_port_info[]. The entry would have a special flag in .host_flags, but the same .port_ops, so there will not be excessive duplication like in the original vendor patch.

Look like other drivers which need workarounds for hardware bugs (e.g., sata_sil) do this.

I noticed there already is a similar mechanism in ahci.c (see AHCI_FLAG_MSI). I decided to use that instead. Will that be acceptable?

scsi/ahci.c renamed from scsi/ahci.c.orig +15 -1
@@ -152,7 +152,8 @@ enum {
 	PORT_CMD_ICC_SLUMBER	= (0x6 << 28), /* Put i/f in slumber state */
 
 	/* hpriv->flags bits */
-	AHCI_FLAG_MSI		= (1 << 0),
+	AHCI_FLAG_MSI			= (1 << 0),
+	AHCI_FLAG_RESET_NEEDS_CLO 	= (1 << 1),
 };
 
 struct ahci_cmd_hdr {
@@ -297,6 +298,8 @@ static const struct pci_device_id ahci_p
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,6 +538,17 @@ static int ahci_poll_register(void __iom
 	return -1;
 }
 
+static void ahci_clo(struct ata_port *ap)
+{
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
+	u32 tmp;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+	readl(port_mmio + PORT_CMD); /* flush */
+}
+
 static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int 

*class)
{
struct ahci_host_priv *hpriv = ap->host_set->private_data;
@@ -565,18 +579,13 @@ static int ahci_softreset(struct ata_por
/* check BUSY/DRQ, perform Command List Override if necessary */ ahci_tf_read(ap, &tf);
if (tf.command & (ATA_BUSY | ATA_DRQ)) {
- u32 tmp;
-
if (!(hpriv->cap & HOST_CAP_CLO)) {
rc = -EIO;
reason = "port busy but no CLO"; goto fail_restart;
}

- tmp = readl(port_mmio + PORT_CMD);
- tmp |= PORT_CMD_CLO;
- writel(tmp, port_mmio + PORT_CMD);
- readl(port_mmio + PORT_CMD); /* flush */
+ ahci_clo(ap);

if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
1, 500)) {
@@ -695,6 +704,15 @@ static void ahci_postreset(struct ata_po

static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes) {
+ struct ahci_host_priv *hpriv = ap->host_set->private_data;
+
+ if (hpriv->flags & AHCI_FLAG_RESET_NEEDS_CLO) {
+ if (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY) {
+ /* ATA_BUSY hasn't cleared, so send a CLO */
+ ahci_clo(ap);
+ }
+ }
+
return ata_drive_probe_reset(ap, ata_std_probeinit,
ahci_softreset, ahci_hardreset, ahci_postreset, classes);
@@ -1266,6 +1284,10 @@ static int ahci_init_one (struct pci_dev
if (pdev->vendor == 0x197b)
pci_write_config_byte(pdev, 0x41, 0xa1);

+ /* VIA VT8251-specific fixup: CLO before reset */
+ if (pdev->device == 0x3349)
+ hpriv->flags |= AHCI_FLAG_RESET_NEEDS_CLO;
+
/* initialize adapter */
rc = ahci_host_init(probe_ent);
if (rc)

#3 lore raw
In reply to Bastiaan Jacques
Quoted 9 lines · Bastiaan Jacques

This patch adds AHCI support for the VIA VT8251 chipset. The patch does so by:
1) Adding the PCI device ID.
2) Adding a workaround in ahci_probe_reset() for a VIA hardware bug.

---

The patch is based upon the patch[1] provided by VIA. With the softreset and probing work that has been merged, this patch has become much simpler.

Quoted 7 lines

I am happy to report that without the workaround in ahci_probe_reset(), the chipset works quite well; both my SATA drives are recognised. However, softreset does not work (but hardreset does), and ata_std_probeinit delays, for my primary SATA drive/port, due to the "busy" bug. A Command List Override will unbusy the drive/port.

Since the CLO sequence also appears in ahci_softreset(), I have moved that code into a new ahci_clo() function.

This patch is against 2.6.17-rc1 (and -mm2).

This patch has only been tested by yours truly. Whilst I am waiting for feedback from other people, I am sending this patch to solicit comments.

Thanks for doing this. VIA AHCI support was indeed waiting on Tejun's EH work.

Would you please resubmit the latest version of your patch including a Signed-off-by line, per guidelines at http://linux.yyz.us/patch-format.html ?

Jeff

#4 lore raw
In reply to Bastiaan Jacques

Hello, Bastiaan.

scsi/ahci.c line 549

Quoted 10 lines · Bastiaan Jacques
scsi/ahci.c renamed from scsi/ahci.c.orig +10 -0
+static void ahci_clo(struct ata_port *ap)
+{
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
+	u32 tmp;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+	readl(port_mmio + PORT_CMD); /* flush */
+}

Can you please make ahci_clo() check for HOST_CAP_CLO and return -EOPNOTSUPP (or whatever) if it's not supported?

Thanks.

Signature · 1 line
tejun
#5 lore raw
Quoted 2 lines · Tejun Heo

Can you please make ahci_clo() check for HOST_CAP_CLO and return -EOPNOTSUPP (or whatever) if it's not supported?

Sure. I've also moved the ahci_poll_register() call into ahci_clo().

Jeff: when everybody is happy with my patch, I'll resubmit it to this list in accordance with the guidelines you linked to (if that's okay with you).

scsi/ahci.c renamed from scsi/ahci.c.orig +42 -18
@@ -152,7 +152,8 @@
 	PORT_CMD_ICC_SLUMBER	= (0x6 << 28), /* Put i/f in slumber state */
 
 	/* hpriv->flags bits */
-	AHCI_FLAG_MSI		= (1 << 0),
+	AHCI_FLAG_MSI			= (1 << 0),
+	AHCI_FLAG_RESET_NEEDS_CLO 	= (1 << 1),
 };
 
 struct ahci_cmd_hdr {
@@ -297,6 +298,8 @@
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,9 +538,28 @@
 	return -1;
 }
 
-static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+	readl(port_mmio + PORT_CMD); /* flush */
+
+	if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0, 1, 500))
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
@@ -565,23 +587,12 @@
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;
+		if ((rc = ahci_clo(ap))) {
+			if (rc & -EOPNOTSUPP)
+				reason = "port busy but CLO unavailable";
+			else
+				reason = "port busy but CLO failed";
 
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
-			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
-		readl(port_mmio + PORT_CMD); /* flush */
-
-		if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
-				       1, 500)) {
-			rc = -EIO;
-			reason = "CLO failed";
 			goto fail_restart;
 		}
 	}
@@ -695,6 +706,15 @@
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+
+	if (hpriv->flags & AHCI_FLAG_RESET_NEEDS_CLO) {
+		if (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY) {
+			/* ATA_BUSY hasn't cleared, so send a CLO */
+			ahci_clo(ap);
+		}
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);
@@ -1266,6 +1286,10 @@
 	if (pdev->vendor == 0x197b)
 		pci_write_config_byte(pdev, 0x41, 0xa1);
 
+	/* VIA VT8251-specific fixup: CLO before reset */
+	if (pdev->device == 0x3349)
+		hpriv->flags |= AHCI_FLAG_RESET_NEEDS_CLO;
+
 	/* initialize adapter */
 	rc = ahci_host_init(probe_ent);
 	if (rc)
#6 lore raw
Quoted 2 lines · Bastiaan Jacques

Jeff: when everybody is happy with my patch, I'll resubmit it to this list in accordance with the guidelines you linked to (if that's okay with you).

Looking pretty good so far, minor comments inline...

Quoted 29 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +6 -2
@@ -152,7 +152,8 @@
 	PORT_CMD_ICC_SLUMBER	= (0x6 << 28), /* Put i/f in slumber state */
 
 	/* hpriv->flags bits */
-	AHCI_FLAG_MSI		= (1 << 0),
+	AHCI_FLAG_MSI			= (1 << 0),
+	AHCI_FLAG_RESET_NEEDS_CLO 	= (1 << 1),
 };
 
 struct ahci_cmd_hdr {
@@ -297,6 +298,8 @@
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,9 +538,28 @@
 	return -1;
 }
 
-static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;

scsi/ahci.c line 553

Quoted 10 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +9 -0
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+	readl(port_mmio + PORT_CMD); /* flush */

ahci_poll_register() should take care of flush for you.

Quoted 12 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +8 -0
+	if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0, 1, 500))
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);

scsi/ahci.c line 590

Quoted 5 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +1 -1
@@ -565,23 +587,12 @@
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;
+		if ((rc = ahci_clo(ap))) {

Don't combine an assignment and an 'if' test in the same line.

This style of statement+if is a constant source of bugs, because it is much more difficult for the human eye to notice that two statements are present rather than one, and it takes longer for the human brain to figure out whether the original author intended to use '==' rather than '='.

scsi/ahci.c line 591

Quoted 1 line
scsi/ahci.c renamed from scsi/ahci.c.orig +1 -0
+			if (rc & -EOPNOTSUPP)

rc isn't a bitmask, the test should be "rc == -EOPNOTSUPP"

Quoted 23 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +3 -15
+				reason = "port busy but CLO unavailable";
+			else
+				reason = "port busy but CLO failed";
 
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
-			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
-		readl(port_mmio + PORT_CMD); /* flush */
-
-		if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
-				       1, 500)) {
-			rc = -EIO;
-			reason = "CLO failed";
 			goto fail_restart;
 		}
 	}

scsi/ahci.c line 712

Quoted 7 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +4 -0
@@ -695,6 +706,15 @@
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+
+	if (hpriv->flags & AHCI_FLAG_RESET_NEEDS_CLO) {
+		if (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY) {

combine these two 'if' statements into a single 'if' statement with '&&'

Quoted 9 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +5 -0
+			/* ATA_BUSY hasn't cleared, so send a CLO */
+			ahci_clo(ap);
+		}
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);

scsi/ahci.c line 1291

Quoted 6 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +3 -0
@@ -1266,6 +1286,10 @@
 	if (pdev->vendor == 0x197b)
 		pci_write_config_byte(pdev, 0x41, 0xa1);
 
+	/* VIA VT8251-specific fixup: CLO before reset */
+	if (pdev->device == 0x3349)
+		hpriv->flags |= AHCI_FLAG_RESET_NEEDS_CLO;

This is flawed because it doesn't check the PCI vendor ID.

In any case, the proper way to do this is:

1) create board_ahci_via next to board_ahci

2) duplicate entry #0 (board_ahci) of ahci_port_info[] as entry #1 (board_ahci_via).

3) Add AHCI_FLAG_RESET_NEEDS_CLO to entry #1.

4) test ap->flags & AHCI_FLAG_RESET_NEEDS_CLO

5) when adding VIA PCI device to ahci_pci_tbl[], the final entry should be 'board_ahci_via'

You could do it has an hpriv->flag, but its easier and more standard to pass chip-specific flags in the manner I have described.

Jeff

#7 lore raw
Quoted 3 lines · Jeff Garzik

Review

Quoted 1 line
(quoted diff) +1 -0
+	readl(port_mmio + PORT_CMD); /* flush */

ahci_poll_register() should take care of flush for you.

Done.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		if ((rc = ahci_clo(ap))) {

Don't combine an assignment and an 'if' test in the same line.

Done.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+	/* VIA VT8251-specific fixup: CLO before reset */
+	if (pdev->device == 0x3349)
+		hpriv->flags |= AHCI_FLAG_RESET_NEEDS_CLO;

This is flawed because it doesn't check the PCI vendor ID.

Quoted 6 lines

In any case, the proper way to do this is:

1) create board_ahci_via next to board_ahci

2) duplicate entry #0 (board_ahci) of ahci_port_info[] as entry #1 (board_ahci_via).

The vendor patch already contained a similar entry which I used in this revision. However, I modified it as follows:

a) Named it board_via_vt8251_ahci, as per Sergey Vlasov's suggestion. Future VIA chipsets won't necessarily require this workaround.
b) Removed ATA_FLAG_SRST from .host_flags as it appears to be deprecated (and soft/hardreset works anyway).
c) Left .pio_mask = 0x03, /* pio3-4 */ from vendor patch intact, as I didn't find anything in the AHCI specification that would require otherwise (and I assume VIA did this for a reason).

Quoted 6 lines

3) Add AHCI_FLAG_RESET_NEEDS_CLO to entry #1.

4) test ap->flags & AHCI_FLAG_RESET_NEEDS_CLO

5) when adding VIA PCI device to ahci_pci_tbl[], the final entry should be 'board_ahci_via'

Done 1-5.

scsi/ahci.c renamed from scsi/ahci.c.orig +46 -16
@@ -73,6 +73,7 @@ enum {
 	RX_FIS_D2H_REG		= 0x40,	/* offset of D2H Register FIS data */
 
 	board_ahci		= 0,
+	board_via_vt8251_ahci   = 1,
 
 	/* global controller registers */
 	HOST_CAP		= 0x00, /* host capabilities */
@@ -153,6 +154,9 @@ enum {
 
 	/* hpriv->flags bits */
 	AHCI_FLAG_MSI		= (1 << 0),
+
+	/* ap->flags bits */
+	AHCI_FLAG_RESET_NEEDS_CLO	= (1 << 24),
 };
 
 struct ahci_cmd_hdr {
@@ -256,6 +260,16 @@ static const struct ata_port_info ahci_p
 		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
 		.port_ops	= &ahci_ops,
 	},
+	/* board_via_vt8251_ahci */
+	{
+		.sht		= &ahci_sht,
+		.host_flags	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
+				  ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
+				  AHCI_FLAG_RESET_NEEDS_CLO,
+		.pio_mask	= 0x03, /* pio3-4 */
+		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
+		.port_ops	= &ahci_ops,
+	},
 };
 
 static const struct pci_device_id ahci_pci_tbl[] = {
@@ -297,6 +311,8 @@ static const struct pci_device_id ahci_p
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_via_vt8251_ahci }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,9 +551,27 @@ static int ahci_poll_register(void __iom
 	return -1;
 }
 
-static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+
+	if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0, 1, 500))
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
@@ -565,23 +599,13 @@ static int ahci_softreset(struct ata_por
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;
+		rc = ahci_clo(ap);
 
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
+		if (rc == -EOPNOTSUPP) {
+			reason = "port busy but CLO unavailable";
 			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
-		readl(port_mmio + PORT_CMD); /* flush */
-
-		if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
-				       1, 500)) {
-			rc = -EIO;
-			reason = "CLO failed";
+		} else if (rc == -EIO) {
+			reason = "port busy but CLO failed";
 			goto fail_restart;
 		}
 	}
@@ -695,6 +719,12 @@ static void ahci_postreset(struct ata_po
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	if (ap->flags & AHCI_FLAG_RESET_NEEDS_CLO &&
+	    ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY) {
+		/* ATA_BUSY hasn't cleared, so send a CLO */
+		ahci_clo(ap);
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);
#8 lore raw

Bastiaan Jacques wrote:

Thanks, a couple remaining minor nits:

scsi/ahci.c enum { · line 76

Quoted 7 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +1 -0
@@ -73,6 +73,7 @@ enum {
 	RX_FIS_D2H_REG		= 0x40,	/* offset of D2H Register FIS data */
 
 	board_ahci		= 0,
+	board_via_vt8251_ahci   = 1,

too long, and puts ahci at the end.

suggest board_ahci_vt8251.

Quoted 13 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +3 -0
 	/* global controller registers */
 	HOST_CAP		= 0x00, /* host capabilities */
@@ -153,6 +154,9 @@ enum {
 
 	/* hpriv->flags bits */
 	AHCI_FLAG_MSI		= (1 << 0),
+
+	/* ap->flags bits */
+	AHCI_FLAG_RESET_NEEDS_CLO	= (1 << 24),
 };
 
 struct ahci_cmd_hdr {

scsi/ahci.c static const struct ata_port_info ahci_p · line 269

Quoted 10 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +7 -0
@@ -256,6 +260,16 @@ static const struct ata_port_info ahci_p
 		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
 		.port_ops	= &ahci_ops,
 	},
+	/* board_via_vt8251_ahci */
+	{
+		.sht		= &ahci_sht,
+		.host_flags	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
+				  ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
+				  AHCI_FLAG_RESET_NEEDS_CLO,
+		.pio_mask	= 0x03, /* pio3-4 */

pio mask should be the same as the previous entry (0x1f).

Never trust vendor patches, they are often patched against ancient libata versions.

Quoted 74 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +29 -16
+		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
+		.port_ops	= &ahci_ops,
+	},
 };
 
 static const struct pci_device_id ahci_pci_tbl[] = {
@@ -297,6 +311,8 @@ static const struct pci_device_id ahci_p
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_via_vt8251_ahci }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,9 +551,27 @@ static int ahci_poll_register(void __iom
 	return -1;
 }
 
-static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+
+	if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0, 1, 500))
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
@@ -565,23 +599,13 @@ static int ahci_softreset(struct ata_por
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;
+		rc = ahci_clo(ap);
 
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
+		if (rc == -EOPNOTSUPP) {
+			reason = "port busy but CLO unavailable";
 			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
-		readl(port_mmio + PORT_CMD); /* flush */
-
-		if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
-				       1, 500)) {
-			rc = -EIO;
-			reason = "CLO failed";
+		} else if (rc == -EIO) {
+			reason = "port busy but CLO failed";
 			goto fail_restart;
 		}
 	}

scsi/ahci.c static void ahci_postreset(struct ata_po · line 722

Quoted 4 lines
scsi/ahci.c renamed from scsi/ahci.c.orig +1 -0
@@ -695,6 +719,12 @@ static void ahci_postreset(struct ata_po
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	if (ap->flags & AHCI_FLAG_RESET_NEEDS_CLO &&

Please add parens for easy reading

scsi/ahci.c static void ahci_postreset(struct ata_po · line 723

Quoted 1 line
scsi/ahci.c renamed from scsi/ahci.c.orig +1 -0
+	    ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY) {

likewise

Jeff

#9 lore raw

PATCH 1/1 ahci: add support for VIA VT8251

Adds AHCI support for the VIA VT8251.

Includes a workaround for a hardware bug which requires a Command List Override before reset.

---

drivers/scsi/ahci.c +46 -16
@@ -73,6 +73,7 @@ enum {
 	RX_FIS_D2H_REG		= 0x40,	/* offset of D2H Register FIS data */
 
 	board_ahci		= 0,
+	board_ahci_vt8251   = 1,
 
 	/* global controller registers */
 	HOST_CAP		= 0x00, /* host capabilities */
@@ -153,6 +154,9 @@ enum {
 
 	/* hpriv->flags bits */
 	AHCI_FLAG_MSI		= (1 << 0),
+
+	/* ap->flags bits */
+	AHCI_FLAG_RESET_NEEDS_CLO	= (1 << 24),
 };
 
 struct ahci_cmd_hdr {
@@ -256,6 +260,16 @@ static const struct ata_port_info ahci_p
 		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
 		.port_ops	= &ahci_ops,
 	},
+	/* board_ahci_vt8251 */
+	{
+		.sht		= &ahci_sht,
+		.host_flags	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
+				  ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
+				  AHCI_FLAG_RESET_NEEDS_CLO,
+		.pio_mask	= 0x1f, /* pio0-4 */
+		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
+		.port_ops	= &ahci_ops,
+	},
 };
 
 static const struct pci_device_id ahci_pci_tbl[] = {
@@ -297,6 +311,8 @@ static const struct pci_device_id ahci_p
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci_vt8251 }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,9 +551,27 @@ static int ahci_poll_register(void __iom
 	return -1;
 }
 
-static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+
+	if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0, 1, 500))
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
@@ -565,23 +599,13 @@ static int ahci_softreset(struct ata_por
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;
+		rc = ahci_clo(ap);
 
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
+		if (rc == -EOPNOTSUPP) {
+			reason = "port busy but CLO unavailable";
 			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
-		readl(port_mmio + PORT_CMD); /* flush */
-
-		if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
-				       1, 500)) {
-			rc = -EIO;
-			reason = "CLO failed";
+		} else if (rc == -EIO) {
+			reason = "port busy but CLO failed";
 			goto fail_restart;
 		}
 	}
@@ -695,6 +719,12 @@ static void ahci_postreset(struct ata_po
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
+	    (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
+		/* ATA_BUSY hasn't cleared, so send a CLO */
+		ahci_clo(ap);
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);
#10 lore raw

PATCH 1/1 ahci: add support for VIA VT8251

On Thursday 13 April 2006 00:19, Bastiaan Jacques wrote:

Adds AHCI support for the VIA VT8251.

Includes a workaround for a hardware bug which requires a Command List Override before reset.

---

And let's not forget indentation..

drivers/scsi/ahci.c +46 -16
@@ -73,6 +73,7 @@ enum {
 	RX_FIS_D2H_REG		= 0x40,	/* offset of D2H Register FIS data */
 
 	board_ahci		= 0,
+	board_ahci_vt8251	= 1,
 
 	/* global controller registers */
 	HOST_CAP		= 0x00, /* host capabilities */
@@ -153,6 +154,9 @@ enum {
 
 	/* hpriv->flags bits */
 	AHCI_FLAG_MSI		= (1 << 0),
+
+	/* ap->flags bits */
+	AHCI_FLAG_RESET_NEEDS_CLO	= (1 << 24),
 };
 
 struct ahci_cmd_hdr {
@@ -256,6 +260,16 @@ static const struct ata_port_info ahci_p
 		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
 		.port_ops	= &ahci_ops,
 	},
+	/* board_ahci_vt8251 */
+	{
+		.sht		= &ahci_sht,
+		.host_flags	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
+				  ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
+				  AHCI_FLAG_RESET_NEEDS_CLO,
+		.pio_mask	= 0x1f, /* pio0-4 */
+		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
+		.port_ops	= &ahci_ops,
+	},
 };
 
 static const struct pci_device_id ahci_pci_tbl[] = {
@@ -297,6 +311,8 @@ static const struct pci_device_id ahci_p
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci_vt8251 }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -535,9 +551,27 @@ static int ahci_poll_register(void __iom
 	return -1;
 }
 
-static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+
+	if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0, 1, 500))
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, int verbose, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
@@ -565,23 +599,13 @@ static int ahci_softreset(struct ata_por
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		u32 tmp;
+		rc = ahci_clo(ap);
 
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
+		if (rc == -EOPNOTSUPP) {
+			reason = "port busy but CLO unavailable";
 			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
-		readl(port_mmio + PORT_CMD); /* flush */
-
-		if (ahci_poll_register(port_mmio + PORT_CMD, PORT_CMD_CLO, 0x0,
-				       1, 500)) {
-			rc = -EIO;
-			reason = "CLO failed";
+		} else if (rc == -EIO) {
+			reason = "port busy but CLO failed";
 			goto fail_restart;
 		}
 	}
@@ -695,6 +719,12 @@ static void ahci_postreset(struct ata_po
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
+	    (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
+		/* ATA_BUSY hasn't cleared, so send a CLO */
+		ahci_clo(ap);
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);
#11 lore raw
In reply to Bastiaan Jacques
Quoted 6 lines · Bastiaan Jacques

Adds AHCI support for the VIA VT8251.

Includes a workaround for a hardware bug which requires a Command List Override before reset.

ACK.

It doesn't currently apply to branch 'upstream'[1] of libata-dev.git. Can you send a patch against that?

Jeff

[1] 26ec634c31a11a003040e10b4d650495158632fd

#12 lore raw

PATCH 1/1 ahci: add support for VIA VT8251

Adds AHCI support for the VIA VT8251.

Includes a workaround for a hardware bug which requires a Command List Override before reset.

---

This patch is against libata-dev.git upstream[1].

[1] 26ec634c31a11a003040e10b4d650495158632fd

drivers/scsi/ahci.c +49 -15
index 1b8429c..3e3b519 100644
@@ -73,6 +73,7 @@ enum {
 	RX_FIS_D2H_REG		= 0x40,	/* offset of D2H Register FIS data */
 
 	board_ahci		= 0,
+	board_ahci_vt8251	= 1,
 
 	/* global controller registers */
 	HOST_CAP		= 0x00, /* host capabilities */
@@ -153,6 +154,9 @@ enum {
 
 	/* hpriv->flags bits */
 	AHCI_FLAG_MSI		= (1 << 0),
+
+	/* ap->flags bits */
+	AHCI_FLAG_RESET_NEEDS_CLO	= (1 << 24),
 };
 
 struct ahci_cmd_hdr {
@@ -255,6 +259,16 @@ static const struct ata_port_info ahci_p
 		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
 		.port_ops	= &ahci_ops,
 	},
+	/* board_ahci_vt8251 */
+	{
+		.sht		= &ahci_sht,
+		.host_flags	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
+				  ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
+				  AHCI_FLAG_RESET_NEEDS_CLO,
+		.pio_mask	= 0x1f, /* pio0-4 */
+		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
+		.port_ops	= &ahci_ops,
+	},
 };
 
 static const struct pci_device_id ahci_pci_tbl[] = {
@@ -296,6 +310,8 @@ static const struct pci_device_id ahci_p
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci_vt8251 }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -516,9 +532,29 @@ static void ahci_fill_cmd_slot(struct ah
 	pp->cmd_slot[0].tbl_addr_hi = cpu_to_le32((pp->cmd_tbl_dma >> 16) >> 16);
 }
 
-static int ahci_softreset(struct ata_port *ap, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+
+	tmp = ata_wait_register(port_mmio + PORT_CMD,
+				PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
+	if (tmp & PORT_CMD_CLO)
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
@@ -547,21 +583,13 @@ static int ahci_softreset(struct ata_por
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
-			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
+		rc = ahci_clo(ap);
 
-		tmp = ata_wait_register(port_mmio + PORT_CMD,
-					PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
-		if (tmp & PORT_CMD_CLO) {
-			rc = -EIO;
-			reason = "CLO failed";
+		if (rc == -EOPNOTSUPP) {
+			reason = "port busy but CLO unavailable";
+			goto fail_restart;
+		} else if (rc == -EIO) {
+			reason = "port busy but CLO failed";
 			goto fail_restart;
 		}
 	}
@@ -672,6 +700,12 @@ static void ahci_postreset(struct ata_po
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
+	    (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
+		/* ATA_BUSY hasn't cleared, so send a CLO */
+		ahci_clo(ap);
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);
#13 lore raw

Hello, Bastiaan.

Looks good to me. Please see below.

Quoted 8 lines · Bastiaan Jacques

Adds AHCI support for the VIA VT8251.

Includes a workaround for a hardware bug which requires a Command List Override before reset.

---

[--snip--]

drivers/scsi/ahci.c static void ahci_postreset(struct ata_po · line 704

Quoted 4 lines

static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes) {

drivers/scsi/ahci.c +2 -0
+	if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
+	    (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {

I'm kind of uncomfortable with busy sleeping with @cnt at 1000. That's >> 10ms of busy waiting. However, this is a minor issue and even if it gets changed it probably belongs to another patch.

Hmmm, ata_busy_sleep() would be a bit weird with the 'be patient' message and I always thought the function contained way too much busy waiting. Processors are blazing fast and context switch is cheap these days.

Maybe we need to update ata_busy_sleep() so that it ignores tmout_pat if it's zero. What do you think, Jeff?

Quoted 4 lines
drivers/scsi/ahci.c +4 -0
+		/* ATA_BUSY hasn't cleared, so send a CLO */
+		ahci_clo(ap);
+	}
+
Signature · 1 line
tejun
#14 lore raw

PATCH 1/1 ahci: add support for VIA VT8251

In reply to Jeff Garzik

Adds AHCI support for the VIA VT8251.

Includes a workaround for a hardware bug which requires a Command List Override before softreset.

---

I have been informed that VIA have also made a patch available for the sata_via driver for this chip. Do you think it would be useful to merge that patch in addition to the AHCI one, or does AHCI support make the "native" sata_via driver obsolete?

Below is the latest version of the (AHCI) patch, with a small change to the error handling in ahci_softreset(): test for "if (rc)" just in case someone decides to change the error return values of ahci_clo().

This patch is against libata-dev.git upstream[1].

[1] 26ec634c31a11a003040e10b4d650495158632fd

drivers/scsi/ahci.c +49 -15
index 1b8429c..2fe4850 100644
@@ -73,6 +73,7 @@ enum {
 	RX_FIS_D2H_REG		= 0x40,	/* offset of D2H Register FIS data */
 
 	board_ahci		= 0,
+	board_ahci_vt8251	= 1,
 
 	/* global controller registers */
 	HOST_CAP		= 0x00, /* host capabilities */
@@ -153,6 +154,9 @@ enum {
 
 	/* hpriv->flags bits */
 	AHCI_FLAG_MSI		= (1 << 0),
+
+	/* ap->flags bits */
+	AHCI_FLAG_RESET_NEEDS_CLO	= (1 << 24),
 };
 
 struct ahci_cmd_hdr {
@@ -255,6 +259,16 @@ static const struct ata_port_info ahci_p
 		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
 		.port_ops	= &ahci_ops,
 	},
+	/* board_ahci_vt8251 */
+	{
+		.sht		= &ahci_sht,
+		.host_flags	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
+				  ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
+				  AHCI_FLAG_RESET_NEEDS_CLO,
+		.pio_mask	= 0x1f, /* pio0-4 */
+		.udma_mask	= 0x7f, /* udma0-6 ; FIXME */
+		.port_ops	= &ahci_ops,
+	},
 };
 
 static const struct pci_device_id ahci_pci_tbl[] = {
@@ -296,6 +310,8 @@ static const struct pci_device_id ahci_p
 	  board_ahci }, /* ATI SB600 non-raid */
 	{ PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
 	  board_ahci }, /* ATI SB600 raid */
+	{ PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
+	  board_ahci_vt8251 }, /* VT8251 */
 	{ }	/* terminate list */
 };
 
@@ -516,9 +532,29 @@ static void ahci_fill_cmd_slot(struct ah
 	pp->cmd_slot[0].tbl_addr_hi = cpu_to_le32((pp->cmd_tbl_dma >> 16) >> 16);
 }
 
-static int ahci_softreset(struct ata_port *ap, unsigned int *class)
+static int ahci_clo(struct ata_port *ap)
 {
+	void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
 	struct ahci_host_priv *hpriv = ap->host_set->private_data;
+	u32 tmp;
+
+	if (!(hpriv->cap & HOST_CAP_CLO))
+		return -EOPNOTSUPP;
+
+	tmp = readl(port_mmio + PORT_CMD);
+	tmp |= PORT_CMD_CLO;
+	writel(tmp, port_mmio + PORT_CMD);
+
+	tmp = ata_wait_register(port_mmio + PORT_CMD,
+				PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
+	if (tmp & PORT_CMD_CLO)
+		return -EIO;
+
+	return 0;
+}
+
+static int ahci_softreset(struct ata_port *ap, unsigned int *class)
+{
 	struct ahci_port_priv *pp = ap->private_data;
 	void __iomem *mmio = ap->host_set->mmio_base;
 	void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
@@ -547,21 +583,13 @@ static int ahci_softreset(struct ata_por
 	/* check BUSY/DRQ, perform Command List Override if necessary */
 	ahci_tf_read(ap, &tf);
 	if (tf.command & (ATA_BUSY | ATA_DRQ)) {
-		if (!(hpriv->cap & HOST_CAP_CLO)) {
-			rc = -EIO;
-			reason = "port busy but no CLO";
-			goto fail_restart;
-		}
-
-		tmp = readl(port_mmio + PORT_CMD);
-		tmp |= PORT_CMD_CLO;
-		writel(tmp, port_mmio + PORT_CMD);
+		rc = ahci_clo(ap);
 
-		tmp = ata_wait_register(port_mmio + PORT_CMD,
-					PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
-		if (tmp & PORT_CMD_CLO) {
-			rc = -EIO;
-			reason = "CLO failed";
+		if (rc == -EOPNOTSUPP) {
+			reason = "port busy but CLO unavailable";
+			goto fail_restart;
+		} else if (rc) {
+			reason = "port busy but CLO failed";
 			goto fail_restart;
 		}
 	}
@@ -672,6 +700,12 @@ static void ahci_postreset(struct ata_po
 
 static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
 {
+	if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
+	    (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
+		/* ATA_BUSY hasn't cleared, so send a CLO */
+		ahci_clo(ap);
+	}
+
 	return ata_drive_probe_reset(ap, ata_std_probeinit,
 				     ahci_softreset, ahci_hardreset,
 				     ahci_postreset, classes);