aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKuan Luo <kluo@nvidia.com>2007-10-15 15:16:53 -0400
committerJeff Garzik <jeff@garzik.org>2007-10-15 15:16:53 -0400
commitf140f0f12fc8dc7264d2f97cbe663564e7d24f6d (patch)
treefe59760afaa523b4918901c13d241fd1d339f388
parent2b9e68f728d6b2cf38b252650f017576e8dae2ad (diff)
[libata] sata_nv: add SW NCQ support for MCP51/MCP55/MCP61
Add the Software NCQ support to sata_nv.c for MCP51/MCP55/MCP61 SATA controller. NCQ function is disable by default, you can enable it with 'swncq=1'. NCQ will be turned off if the drive is Maxtor on MCP51 or MCP55 rev 0xa2 platform. [akpm@linux-foundation.org: build fix] Signed-off-by: Kuan Luo <kluo@nvidia.com> Signed-off-by: Peer Chen <pchen@nvidia.com> Cc: Zoltan Boszormenyi <zboszor@dunaweb.hu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Jeff Garzik <jgarzik@redhat.com>
-rw-r--r--drivers/ata/sata_nv.c851
1 files changed, 842 insertions, 9 deletions
diff --git a/drivers/ata/sata_nv.c b/drivers/ata/sata_nv.c
index 40557fe2ffdf..240a8920d0bd 100644
--- a/drivers/ata/sata_nv.c
+++ b/drivers/ata/sata_nv.c
@@ -169,6 +169,35 @@ enum {
169 NV_ADMA_PORT_REGISTER_MODE = (1 << 0), 169 NV_ADMA_PORT_REGISTER_MODE = (1 << 0),
170 NV_ADMA_ATAPI_SETUP_COMPLETE = (1 << 1), 170 NV_ADMA_ATAPI_SETUP_COMPLETE = (1 << 1),
171 171
172 /* MCP55 reg offset */
173 NV_CTL_MCP55 = 0x400,
174 NV_INT_STATUS_MCP55 = 0x440,
175 NV_INT_ENABLE_MCP55 = 0x444,
176 NV_NCQ_REG_MCP55 = 0x448,
177
178 /* MCP55 */
179 NV_INT_ALL_MCP55 = 0xffff,
180 NV_INT_PORT_SHIFT_MCP55 = 16, /* each port occupies 16 bits */
181 NV_INT_MASK_MCP55 = NV_INT_ALL_MCP55 & 0xfffd,
182
183 /* SWNCQ ENABLE BITS*/
184 NV_CTL_PRI_SWNCQ = 0x02,
185 NV_CTL_SEC_SWNCQ = 0x04,
186
187 /* SW NCQ status bits*/
188 NV_SWNCQ_IRQ_DEV = (1 << 0),
189 NV_SWNCQ_IRQ_PM = (1 << 1),
190 NV_SWNCQ_IRQ_ADDED = (1 << 2),
191 NV_SWNCQ_IRQ_REMOVED = (1 << 3),
192
193 NV_SWNCQ_IRQ_BACKOUT = (1 << 4),
194 NV_SWNCQ_IRQ_SDBFIS = (1 << 5),
195 NV_SWNCQ_IRQ_DHREGFIS = (1 << 6),
196 NV_SWNCQ_IRQ_DMASETUP = (1 << 7),
197
198 NV_SWNCQ_IRQ_HOTPLUG = NV_SWNCQ_IRQ_ADDED |
199 NV_SWNCQ_IRQ_REMOVED,
200
172}; 201};
173 202
174/* ADMA Physical Region Descriptor - one SG segment */ 203/* ADMA Physical Region Descriptor - one SG segment */
@@ -226,6 +255,42 @@ struct nv_host_priv {
226 unsigned long type; 255 unsigned long type;
227}; 256};
228 257
258struct defer_queue {
259 u32 defer_bits;
260 unsigned int head;
261 unsigned int tail;
262 unsigned int tag[ATA_MAX_QUEUE];
263};
264
265enum ncq_saw_flag_list {
266 ncq_saw_d2h = (1U << 0),
267 ncq_saw_dmas = (1U << 1),
268 ncq_saw_sdb = (1U << 2),
269 ncq_saw_backout = (1U << 3),
270};
271
272struct nv_swncq_port_priv {
273 struct ata_prd *prd; /* our SG list */
274 dma_addr_t prd_dma; /* and its DMA mapping */
275 void __iomem *sactive_block;
276 void __iomem *irq_block;
277 void __iomem *tag_block;
278 u32 qc_active;
279
280 unsigned int last_issue_tag;
281
282 /* fifo circular queue to store deferral command */
283 struct defer_queue defer_queue;
284
285 /* for NCQ interrupt analysis */
286 u32 dhfis_bits;
287 u32 dmafis_bits;
288 u32 sdbfis_bits;
289
290 unsigned int ncq_flags;
291};
292
293
229#define NV_ADMA_CHECK_INTR(GCTL, PORT) ((GCTL) & ( 1 << (19 + (12 * (PORT))))) 294#define NV_ADMA_CHECK_INTR(GCTL, PORT) ((GCTL) & ( 1 << (19 + (12 * (PORT)))))
230 295
231static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent); 296static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
@@ -263,13 +328,29 @@ static void nv_adma_host_stop(struct ata_host *host);
263static void nv_adma_post_internal_cmd(struct ata_queued_cmd *qc); 328static void nv_adma_post_internal_cmd(struct ata_queued_cmd *qc);
264static void nv_adma_tf_read(struct ata_port *ap, struct ata_taskfile *tf); 329static void nv_adma_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
265 330
331static void nv_mcp55_thaw(struct ata_port *ap);
332static void nv_mcp55_freeze(struct ata_port *ap);
333static void nv_swncq_error_handler(struct ata_port *ap);
334static int nv_swncq_slave_config(struct scsi_device *sdev);
335static int nv_swncq_port_start(struct ata_port *ap);
336static void nv_swncq_qc_prep(struct ata_queued_cmd *qc);
337static void nv_swncq_fill_sg(struct ata_queued_cmd *qc);
338static unsigned int nv_swncq_qc_issue(struct ata_queued_cmd *qc);
339static void nv_swncq_irq_clear(struct ata_port *ap, u16 fis);
340static irqreturn_t nv_swncq_interrupt(int irq, void *dev_instance);
341#ifdef CONFIG_PM
342static int nv_swncq_port_suspend(struct ata_port *ap, pm_message_t mesg);
343static int nv_swncq_port_resume(struct ata_port *ap);
344#endif
345
266enum nv_host_type 346enum nv_host_type
267{ 347{
268 GENERIC, 348 GENERIC,
269 NFORCE2, 349 NFORCE2,
270 NFORCE3 = NFORCE2, /* NF2 == NF3 as far as sata_nv is concerned */ 350 NFORCE3 = NFORCE2, /* NF2 == NF3 as far as sata_nv is concerned */
271 CK804, 351 CK804,
272 ADMA 352 ADMA,
353 SWNCQ,
273}; 354};
274 355
275static const struct pci_device_id nv_pci_tbl[] = { 356static const struct pci_device_id nv_pci_tbl[] = {
@@ -280,13 +361,13 @@ static const struct pci_device_id nv_pci_tbl[] = {
280 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2), CK804 }, 361 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_SATA2), CK804 },
281 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA), CK804 }, 362 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA), CK804 },
282 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2), CK804 }, 363 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_SATA2), CK804 },
283 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA), GENERIC }, 364 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA), SWNCQ },
284 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2), GENERIC }, 365 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2), SWNCQ },
285 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA), GENERIC }, 366 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA), SWNCQ },
286 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2), GENERIC }, 367 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2), SWNCQ },
287 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA), GENERIC }, 368 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA), SWNCQ },
288 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2), GENERIC }, 369 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA2), SWNCQ },
289 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3), GENERIC }, 370 { PCI_VDEVICE(NVIDIA, PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_SATA3), SWNCQ },
290 371
291 { } /* terminate list */ 372 { } /* terminate list */
292}; 373};
@@ -339,6 +420,25 @@ static struct scsi_host_template nv_adma_sht = {
339 .bios_param = ata_std_bios_param, 420 .bios_param = ata_std_bios_param,
340}; 421};
341 422
423static struct scsi_host_template nv_swncq_sht = {
424 .module = THIS_MODULE,
425 .name = DRV_NAME,
426 .ioctl = ata_scsi_ioctl,
427 .queuecommand = ata_scsi_queuecmd,
428 .change_queue_depth = ata_scsi_change_queue_depth,
429 .can_queue = ATA_MAX_QUEUE,
430 .this_id = ATA_SHT_THIS_ID,
431 .sg_tablesize = LIBATA_MAX_PRD,
432 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
433 .emulated = ATA_SHT_EMULATED,
434 .use_clustering = ATA_SHT_USE_CLUSTERING,
435 .proc_name = DRV_NAME,
436 .dma_boundary = ATA_DMA_BOUNDARY,
437 .slave_configure = nv_swncq_slave_config,
438 .slave_destroy = ata_scsi_slave_destroy,
439 .bios_param = ata_std_bios_param,
440};
441
342static const struct ata_port_operations nv_generic_ops = { 442static const struct ata_port_operations nv_generic_ops = {
343 .tf_load = ata_tf_load, 443 .tf_load = ata_tf_load,
344 .tf_read = ata_tf_read, 444 .tf_read = ata_tf_read,
@@ -444,6 +544,35 @@ static const struct ata_port_operations nv_adma_ops = {
444 .host_stop = nv_adma_host_stop, 544 .host_stop = nv_adma_host_stop,
445}; 545};
446 546
547static const struct ata_port_operations nv_swncq_ops = {
548 .tf_load = ata_tf_load,
549 .tf_read = ata_tf_read,
550 .exec_command = ata_exec_command,
551 .check_status = ata_check_status,
552 .dev_select = ata_std_dev_select,
553 .bmdma_setup = ata_bmdma_setup,
554 .bmdma_start = ata_bmdma_start,
555 .bmdma_stop = ata_bmdma_stop,
556 .bmdma_status = ata_bmdma_status,
557 .qc_defer = ata_std_qc_defer,
558 .qc_prep = nv_swncq_qc_prep,
559 .qc_issue = nv_swncq_qc_issue,
560 .freeze = nv_mcp55_freeze,
561 .thaw = nv_mcp55_thaw,
562 .error_handler = nv_swncq_error_handler,
563 .post_internal_cmd = ata_bmdma_post_internal_cmd,
564 .data_xfer = ata_data_xfer,
565 .irq_clear = ata_bmdma_irq_clear,
566 .irq_on = ata_irq_on,
567 .scr_read = nv_scr_read,
568 .scr_write = nv_scr_write,
569#ifdef CONFIG_PM
570 .port_suspend = nv_swncq_port_suspend,
571 .port_resume = nv_swncq_port_resume,
572#endif
573 .port_start = nv_swncq_port_start,
574};
575
447static const struct ata_port_info nv_port_info[] = { 576static const struct ata_port_info nv_port_info[] = {
448 /* generic */ 577 /* generic */
449 { 578 {
@@ -490,6 +619,18 @@ static const struct ata_port_info nv_port_info[] = {
490 .port_ops = &nv_adma_ops, 619 .port_ops = &nv_adma_ops,
491 .irq_handler = nv_adma_interrupt, 620 .irq_handler = nv_adma_interrupt,
492 }, 621 },
622 /* SWNCQ */
623 {
624 .sht = &nv_swncq_sht,
625 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
626 ATA_FLAG_NCQ,
627 .link_flags = ATA_LFLAG_HRST_TO_RESUME,
628 .pio_mask = NV_PIO_MASK,
629 .mwdma_mask = NV_MWDMA_MASK,
630 .udma_mask = NV_UDMA_MASK,
631 .port_ops = &nv_swncq_ops,
632 .irq_handler = nv_swncq_interrupt,
633 },
493}; 634};
494 635
495MODULE_AUTHOR("NVIDIA"); 636MODULE_AUTHOR("NVIDIA");
@@ -499,6 +640,7 @@ MODULE_DEVICE_TABLE(pci, nv_pci_tbl);
499MODULE_VERSION(DRV_VERSION); 640MODULE_VERSION(DRV_VERSION);
500 641
501static int adma_enabled = 1; 642static int adma_enabled = 1;
643static int swncq_enabled;
502 644
503static void nv_adma_register_mode(struct ata_port *ap) 645static void nv_adma_register_mode(struct ata_port *ap)
504{ 646{
@@ -1452,6 +1594,34 @@ static void nv_ck804_thaw(struct ata_port *ap)
1452 writeb(mask, mmio_base + NV_INT_ENABLE_CK804); 1594 writeb(mask, mmio_base + NV_INT_ENABLE_CK804);
1453} 1595}
1454 1596
1597static void nv_mcp55_freeze(struct ata_port *ap)
1598{
1599 void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
1600 int shift = ap->port_no * NV_INT_PORT_SHIFT_MCP55;
1601 u32 mask;
1602
1603 writel(NV_INT_ALL_MCP55 << shift, mmio_base + NV_INT_STATUS_MCP55);
1604
1605 mask = readl(mmio_base + NV_INT_ENABLE_MCP55);
1606 mask &= ~(NV_INT_ALL_MCP55 << shift);
1607 writel(mask, mmio_base + NV_INT_ENABLE_MCP55);
1608 ata_bmdma_freeze(ap);
1609}
1610
1611static void nv_mcp55_thaw(struct ata_port *ap)
1612{
1613 void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
1614 int shift = ap->port_no * NV_INT_PORT_SHIFT_MCP55;
1615 u32 mask;
1616
1617 writel(NV_INT_ALL_MCP55 << shift, mmio_base + NV_INT_STATUS_MCP55);
1618
1619 mask = readl(mmio_base + NV_INT_ENABLE_MCP55);
1620 mask |= (NV_INT_MASK_MCP55 << shift);
1621 writel(mask, mmio_base + NV_INT_ENABLE_MCP55);
1622 ata_bmdma_thaw(ap);
1623}
1624
1455static int nv_hardreset(struct ata_link *link, unsigned int *class, 1625static int nv_hardreset(struct ata_link *link, unsigned int *class,
1456 unsigned long deadline) 1626 unsigned long deadline)
1457{ 1627{
@@ -1525,6 +1695,663 @@ static void nv_adma_error_handler(struct ata_port *ap)
1525 nv_hardreset, ata_std_postreset); 1695 nv_hardreset, ata_std_postreset);
1526} 1696}
1527 1697
1698static void nv_swncq_qc_to_dq(struct ata_port *ap, struct ata_queued_cmd *qc)
1699{
1700 struct nv_swncq_port_priv *pp = ap->private_data;
1701 struct defer_queue *dq = &pp->defer_queue;
1702
1703 /* queue is full */
1704 WARN_ON(dq->tail - dq->head == ATA_MAX_QUEUE);
1705 dq->defer_bits |= (1 << qc->tag);
1706 dq->tag[dq->tail++ & (ATA_MAX_QUEUE - 1)] = qc->tag;
1707}
1708
1709static struct ata_queued_cmd *nv_swncq_qc_from_dq(struct ata_port *ap)
1710{
1711 struct nv_swncq_port_priv *pp = ap->private_data;
1712 struct defer_queue *dq = &pp->defer_queue;
1713 unsigned int tag;
1714
1715 if (dq->head == dq->tail) /* null queue */
1716 return NULL;
1717
1718 tag = dq->tag[dq->head & (ATA_MAX_QUEUE - 1)];
1719 dq->tag[dq->head++ & (ATA_MAX_QUEUE - 1)] = ATA_TAG_POISON;
1720 WARN_ON(!(dq->defer_bits & (1 << tag)));
1721 dq->defer_bits &= ~(1 << tag);
1722
1723 return ata_qc_from_tag(ap, tag);
1724}
1725
1726static void nv_swncq_fis_reinit(struct ata_port *ap)
1727{
1728 struct nv_swncq_port_priv *pp = ap->private_data;
1729
1730 pp->dhfis_bits = 0;
1731 pp->dmafis_bits = 0;
1732 pp->sdbfis_bits = 0;
1733 pp->ncq_flags = 0;
1734}
1735
1736static void nv_swncq_pp_reinit(struct ata_port *ap)
1737{
1738 struct nv_swncq_port_priv *pp = ap->private_data;
1739 struct defer_queue *dq = &pp->defer_queue;
1740
1741 dq->head = 0;
1742 dq->tail = 0;
1743 dq->defer_bits = 0;
1744 pp->qc_active = 0;
1745 pp->last_issue_tag = ATA_TAG_POISON;
1746 nv_swncq_fis_reinit(ap);
1747}
1748
1749static void nv_swncq_irq_clear(struct ata_port *ap, u16 fis)
1750{
1751 struct nv_swncq_port_priv *pp = ap->private_data;
1752
1753 writew(fis, pp->irq_block);
1754}
1755
1756static void __ata_bmdma_stop(struct ata_port *ap)
1757{
1758 struct ata_queued_cmd qc;
1759
1760 qc.ap = ap;
1761 ata_bmdma_stop(&qc);
1762}
1763
1764static void nv_swncq_ncq_stop(struct ata_port *ap)
1765{
1766 struct nv_swncq_port_priv *pp = ap->private_data;
1767 unsigned int i;
1768 u32 sactive;
1769 u32 done_mask;
1770
1771 ata_port_printk(ap, KERN_ERR,
1772 "EH in SWNCQ mode,QC:qc_active 0x%X sactive 0x%X\n",
1773 ap->qc_active, ap->link.sactive);
1774 ata_port_printk(ap, KERN_ERR,
1775 "SWNCQ:qc_active 0x%X defer_bits 0x%X last_issue_tag 0x%x\n "
1776 "dhfis 0x%X dmafis 0x%X sdbfis 0x%X\n",
1777 pp->qc_active, pp->defer_queue.defer_bits, pp->last_issue_tag,
1778 pp->dhfis_bits, pp->dmafis_bits, pp->sdbfis_bits);
1779
1780 ata_port_printk(ap, KERN_ERR, "ATA_REG 0x%X ERR_REG 0x%X\n",
1781 ap->ops->check_status(ap),
1782 ioread8(ap->ioaddr.error_addr));
1783
1784 sactive = readl(pp->sactive_block);
1785 done_mask = pp->qc_active ^ sactive;
1786
1787 ata_port_printk(ap, KERN_ERR, "tag : dhfis dmafis sdbfis sacitve\n");
1788 for (i = 0; i < ATA_MAX_QUEUE; i++) {
1789 u8 err = 0;
1790 if (pp->qc_active & (1 << i))
1791 err = 0;
1792 else if (done_mask & (1 << i))
1793 err = 1;
1794 else
1795 continue;
1796
1797 ata_port_printk(ap, KERN_ERR,
1798 "tag 0x%x: %01x %01x %01x %01x %s\n", i,
1799 (pp->dhfis_bits >> i) & 0x1,
1800 (pp->dmafis_bits >> i) & 0x1,
1801 (pp->sdbfis_bits >> i) & 0x1,
1802 (sactive >> i) & 0x1,
1803 (err ? "error! tag doesn't exit" : " "));
1804 }
1805
1806 nv_swncq_pp_reinit(ap);
1807 ap->ops->irq_clear(ap);
1808 __ata_bmdma_stop(ap);
1809 nv_swncq_irq_clear(ap, 0xffff);
1810}
1811
1812static void nv_swncq_error_handler(struct ata_port *ap)
1813{
1814 struct ata_eh_context *ehc = &ap->link.eh_context;
1815
1816 if (ap->link.sactive) {
1817 nv_swncq_ncq_stop(ap);
1818 ehc->i.action |= ATA_EH_HARDRESET;
1819 }
1820
1821 ata_bmdma_drive_eh(ap, ata_std_prereset, ata_std_softreset,
1822 nv_hardreset, ata_std_postreset);
1823}
1824
1825#ifdef CONFIG_PM
1826static int nv_swncq_port_suspend(struct ata_port *ap, pm_message_t mesg)
1827{
1828 void __iomem *mmio = ap->host->iomap[NV_MMIO_BAR];
1829 u32 tmp;
1830
1831 /* clear irq */
1832 writel(~0, mmio + NV_INT_STATUS_MCP55);
1833
1834 /* disable irq */
1835 writel(0, mmio + NV_INT_ENABLE_MCP55);
1836
1837 /* disable swncq */
1838 tmp = readl(mmio + NV_CTL_MCP55);
1839 tmp &= ~(NV_CTL_PRI_SWNCQ | NV_CTL_SEC_SWNCQ);
1840 writel(tmp, mmio + NV_CTL_MCP55);
1841
1842 return 0;
1843}
1844
1845static int nv_swncq_port_resume(struct ata_port *ap)
1846{
1847 void __iomem *mmio = ap->host->iomap[NV_MMIO_BAR];
1848 u32 tmp;
1849
1850 /* clear irq */
1851 writel(~0, mmio + NV_INT_STATUS_MCP55);
1852
1853 /* enable irq */
1854 writel(0x00fd00fd, mmio + NV_INT_ENABLE_MCP55);
1855
1856 /* enable swncq */
1857 tmp = readl(mmio + NV_CTL_MCP55);
1858 writel(tmp | NV_CTL_PRI_SWNCQ | NV_CTL_SEC_SWNCQ, mmio + NV_CTL_MCP55);
1859
1860 return 0;
1861}
1862#endif
1863
1864static void nv_swncq_host_init(struct ata_host *host)
1865{
1866 u32 tmp;
1867 void __iomem *mmio = host->iomap[NV_MMIO_BAR];
1868 struct pci_dev *pdev = to_pci_dev(host->dev);
1869 u8 regval;
1870
1871 /* disable ECO 398 */
1872 pci_read_config_byte(pdev, 0x7f, &regval);
1873 regval &= ~(1 << 7);
1874 pci_write_config_byte(pdev, 0x7f, regval);
1875
1876 /* enable swncq */
1877 tmp = readl(mmio + NV_CTL_MCP55);
1878 VPRINTK("HOST_CTL:0x%X\n", tmp);
1879 writel(tmp | NV_CTL_PRI_SWNCQ | NV_CTL_SEC_SWNCQ, mmio + NV_CTL_MCP55);
1880
1881 /* enable irq intr */
1882 tmp = readl(mmio + NV_INT_ENABLE_MCP55);
1883 VPRINTK("HOST_ENABLE:0x%X\n", tmp);
1884 writel(tmp | 0x00fd00fd, mmio + NV_INT_ENABLE_MCP55);
1885
1886 /* clear port irq */
1887 writel(~0x0, mmio + NV_INT_STATUS_MCP55);
1888}
1889
1890static int nv_swncq_slave_config(struct scsi_device *sdev)
1891{
1892 struct ata_port *ap = ata_shost_to_port(sdev->host);
1893 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
1894 struct ata_device *dev;
1895 int rc;
1896 u8 rev;
1897 u8 check_maxtor = 0;
1898 unsigned char model_num[ATA_ID_PROD_LEN + 1];
1899
1900 rc = ata_scsi_slave_config(sdev);
1901 if (sdev->id >= ATA_MAX_DEVICES || sdev->channel || sdev->lun)
1902 /* Not a proper libata device, ignore */
1903 return rc;
1904
1905 dev = &ap->link.device[sdev->id];
1906 if (!(ap->flags & ATA_FLAG_NCQ) || dev->class == ATA_DEV_ATAPI)
1907 return rc;
1908
1909 /* if MCP51 and Maxtor, then disable ncq */
1910 if (pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA ||
1911 pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_SATA2)
1912 check_maxtor = 1;
1913
1914 /* if MCP55 and rev <= a2 and Maxtor, then disable ncq */
1915 if (pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA ||
1916 pdev->device == PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_SATA2) {
1917 pci_read_config_byte(pdev, 0x8, &rev);
1918 if (rev <= 0xa2)
1919 check_maxtor = 1;
1920 }
1921
1922 if (!check_maxtor)
1923 return rc;
1924
1925 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
1926
1927 if (strncmp(model_num, "Maxtor", 6) == 0) {
1928 ata_scsi_change_queue_depth(sdev, 1);
1929 ata_dev_printk(dev, KERN_NOTICE,
1930 "Disabling SWNCQ mode (depth %x)\n", sdev->queue_depth);
1931 }
1932
1933 return rc;
1934}
1935
1936static int nv_swncq_port_start(struct ata_port *ap)
1937{
1938 struct device *dev = ap->host->dev;
1939 void __iomem *mmio = ap->host->iomap[NV_MMIO_BAR];
1940 struct nv_swncq_port_priv *pp;
1941 int rc;
1942
1943 rc = ata_port_start(ap);
1944 if (rc)
1945 return rc;
1946
1947 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1948 if (!pp)
1949 return -ENOMEM;
1950
1951 pp->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ * ATA_MAX_QUEUE,
1952 &pp->prd_dma, GFP_KERNEL);
1953 if (!pp->prd)
1954 return -ENOMEM;
1955 memset(pp->prd, 0, ATA_PRD_TBL_SZ * ATA_MAX_QUEUE);
1956
1957 ap->private_data = pp;
1958 pp->sactive_block = ap->ioaddr.scr_addr + 4 * SCR_ACTIVE;
1959 pp->irq_block = mmio + NV_INT_STATUS_MCP55 + ap->port_no * 2;
1960 pp->tag_block = mmio + NV_NCQ_REG_MCP55 + ap->port_no * 2;
1961
1962 return 0;
1963}
1964
1965static void nv_swncq_qc_prep(struct ata_queued_cmd *qc)
1966{
1967 if (qc->tf.protocol != ATA_PROT_NCQ) {
1968 ata_qc_prep(qc);
1969 return;
1970 }
1971
1972 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1973 return;
1974
1975 nv_swncq_fill_sg(qc);
1976}
1977
1978static void nv_swncq_fill_sg(struct ata_queued_cmd *qc)
1979{
1980 struct ata_port *ap = qc->ap;
1981 struct scatterlist *sg;
1982 unsigned int idx;
1983 struct nv_swncq_port_priv *pp = ap->private_data;
1984 struct ata_prd *prd;
1985
1986 WARN_ON(qc->__sg == NULL);
1987 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
1988
1989 prd = pp->prd + ATA_MAX_PRD * qc->tag;
1990
1991 idx = 0;
1992 ata_for_each_sg(sg, qc) {
1993 u32 addr, offset;
1994 u32 sg_len, len;
1995
1996 addr = (u32)sg_dma_address(sg);
1997 sg_len = sg_dma_len(sg);
1998
1999 while (sg_len) {
2000 offset = addr & 0xffff;
2001 len = sg_len;
2002 if ((offset + sg_len) > 0x10000)
2003 len = 0x10000 - offset;
2004
2005 prd[idx].addr = cpu_to_le32(addr);
2006 prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2007
2008 idx++;
2009 sg_len -= len;
2010 addr += len;
2011 }
2012 }
2013
2014 if (idx)
2015 prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2016}
2017
2018static unsigned int nv_swncq_issue_atacmd(struct ata_port *ap,
2019 struct ata_queued_cmd *qc)
2020{
2021 struct nv_swncq_port_priv *pp = ap->private_data;
2022
2023 if (qc == NULL)
2024 return 0;
2025
2026 DPRINTK("Enter\n");
2027
2028 writel((1 << qc->tag), pp->sactive_block);
2029 pp->last_issue_tag = qc->tag;
2030 pp->dhfis_bits &= ~(1 << qc->tag);
2031 pp->dmafis_bits &= ~(1 << qc->tag);
2032 pp->qc_active |= (0x1 << qc->tag);
2033
2034 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
2035 ap->ops->exec_command(ap, &qc->tf);
2036
2037 DPRINTK("Issued tag %u\n", qc->tag);
2038
2039 return 0;
2040}
2041
2042static unsigned int nv_swncq_qc_issue(struct ata_queued_cmd *qc)
2043{
2044 struct ata_port *ap = qc->ap;
2045 struct nv_swncq_port_priv *pp = ap->private_data;
2046
2047 if (qc->tf.protocol != ATA_PROT_NCQ)
2048 return ata_qc_issue_prot(qc);
2049
2050 DPRINTK("Enter\n");
2051
2052 if (!pp->qc_active)
2053 nv_swncq_issue_atacmd(ap, qc);
2054 else
2055 nv_swncq_qc_to_dq(ap, qc); /* add qc to defer queue */
2056
2057 return 0;
2058}
2059
2060static void nv_swncq_hotplug(struct ata_port *ap, u32 fis)
2061{
2062 u32 serror;
2063 struct ata_eh_info *ehi = &ap->link.eh_info;
2064
2065 ata_ehi_clear_desc(ehi);
2066
2067 /* AHCI needs SError cleared; otherwise, it might lock up */
2068 sata_scr_read(&ap->link, SCR_ERROR, &serror);
2069 sata_scr_write(&ap->link, SCR_ERROR, serror);
2070
2071 /* analyze @irq_stat */
2072 if (fis & NV_SWNCQ_IRQ_ADDED)
2073 ata_ehi_push_desc(ehi, "hot plug");
2074 else if (fis & NV_SWNCQ_IRQ_REMOVED)
2075 ata_ehi_push_desc(ehi, "hot unplug");
2076
2077 ata_ehi_hotplugged(ehi);
2078
2079 /* okay, let's hand over to EH */
2080 ehi->serror |= serror;
2081
2082 ata_port_freeze(ap);
2083}
2084
2085static int nv_swncq_sdbfis(struct ata_port *ap)
2086{
2087 struct ata_queued_cmd *qc;
2088 struct nv_swncq_port_priv *pp = ap->private_data;
2089 struct ata_eh_info *ehi = &ap->link.eh_info;
2090 u32 sactive;
2091 int nr_done = 0;
2092 u32 done_mask;
2093 int i;
2094 u8 host_stat;
2095 u8 lack_dhfis = 0;
2096
2097 host_stat = ap->ops->bmdma_status(ap);
2098 if (unlikely(host_stat & ATA_DMA_ERR)) {
2099 /* error when transfering data to/from memory */
2100 ata_ehi_clear_desc(ehi);
2101 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
2102 ehi->err_mask |= AC_ERR_HOST_BUS;
2103 ehi->action |= ATA_EH_SOFTRESET;
2104 return -EINVAL;
2105 }
2106
2107 ap->ops->irq_clear(ap);
2108 __ata_bmdma_stop(ap);
2109
2110 sactive = readl(pp->sactive_block);
2111 done_mask = pp->qc_active ^ sactive;
2112
2113 if (unlikely(done_mask & sactive)) {
2114 ata_ehi_clear_desc(ehi);
2115 ata_ehi_push_desc(ehi, "illegal SWNCQ:qc_active transition"
2116 "(%08x->%08x)", pp->qc_active, sactive);
2117 ehi->err_mask |= AC_ERR_HSM;
2118 ehi->action |= ATA_EH_HARDRESET;
2119 return -EINVAL;
2120 }
2121 for (i = 0; i < ATA_MAX_QUEUE; i++) {
2122 if (!(done_mask & (1 << i)))
2123 continue;
2124
2125 qc = ata_qc_from_tag(ap, i);
2126 if (qc) {
2127 ata_qc_complete(qc);
2128 pp->qc_active &= ~(1 << i);
2129 pp->dhfis_bits &= ~(1 << i);
2130 pp->dmafis_bits &= ~(1 << i);
2131 pp->sdbfis_bits |= (1 << i);
2132 nr_done++;
2133 }
2134 }
2135
2136 if (!ap->qc_active) {
2137 DPRINTK("over\n");
2138 nv_swncq_pp_reinit(ap);
2139 return nr_done;
2140 }
2141
2142 if (pp->qc_active & pp->dhfis_bits)
2143 return nr_done;
2144
2145 if ((pp->ncq_flags & ncq_saw_backout) ||
2146 (pp->qc_active ^ pp->dhfis_bits))
2147 /* if the controller cann't get a device to host register FIS,
2148 * The driver needs to reissue the new command.
2149 */
2150 lack_dhfis = 1;
2151
2152 DPRINTK("id 0x%x QC: qc_active 0x%x,"
2153 "SWNCQ:qc_active 0x%X defer_bits %X "
2154 "dhfis 0x%X dmafis 0x%X last_issue_tag %x\n",
2155 ap->print_id, ap->qc_active, pp->qc_active,
2156 pp->defer_queue.defer_bits, pp->dhfis_bits,
2157 pp->dmafis_bits, pp->last_issue_tag);
2158
2159 nv_swncq_fis_reinit(ap);
2160
2161 if (lack_dhfis) {
2162 qc = ata_qc_from_tag(ap, pp->last_issue_tag);
2163 nv_swncq_issue_atacmd(ap, qc);
2164 return nr_done;
2165 }
2166
2167 if (pp->defer_queue.defer_bits) {
2168 /* send deferral queue command */
2169 qc = nv_swncq_qc_from_dq(ap);
2170 WARN_ON(qc == NULL);
2171 nv_swncq_issue_atacmd(ap, qc);
2172 }
2173
2174 return nr_done;
2175}
2176
2177static inline u32 nv_swncq_tag(struct ata_port *ap)
2178{
2179 struct nv_swncq_port_priv *pp = ap->private_data;
2180 u32 tag;
2181
2182 tag = readb(pp->tag_block) >> 2;
2183 return (tag & 0x1f);
2184}
2185
2186static int nv_swncq_dmafis(struct ata_port *ap)
2187{
2188 struct ata_queued_cmd *qc;
2189 unsigned int rw;
2190 u8 dmactl;
2191 u32 tag;
2192 struct nv_swncq_port_priv *pp = ap->private_data;
2193
2194 __ata_bmdma_stop(ap);
2195 tag = nv_swncq_tag(ap);
2196
2197 DPRINTK("dma setup tag 0x%x\n", tag);
2198 qc = ata_qc_from_tag(ap, tag);
2199
2200 if (unlikely(!qc))
2201 return 0;
2202
2203 rw = qc->tf.flags & ATA_TFLAG_WRITE;
2204
2205 /* load PRD table addr. */
2206 iowrite32(pp->prd_dma + ATA_PRD_TBL_SZ * qc->tag,
2207 ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2208
2209 /* specify data direction, triple-check start bit is clear */
2210 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2211 dmactl &= ~ATA_DMA_WR;
2212 if (!rw)
2213 dmactl |= ATA_DMA_WR;
2214
2215 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2216
2217 return 1;
2218}
2219
2220static void nv_swncq_host_interrupt(struct ata_port *ap, u16 fis)
2221{
2222 struct nv_swncq_port_priv *pp = ap->private_data;
2223 struct ata_queued_cmd *qc;
2224 struct ata_eh_info *ehi = &ap->link.eh_info;
2225 u32 serror;
2226 u8 ata_stat;
2227 int rc = 0;
2228
2229 ata_stat = ap->ops->check_status(ap);
2230 nv_swncq_irq_clear(ap, fis);
2231 if (!fis)
2232 return;
2233
2234 if (ap->pflags & ATA_PFLAG_FROZEN)
2235 return;
2236
2237 if (fis & NV_SWNCQ_IRQ_HOTPLUG) {
2238 nv_swncq_hotplug(ap, fis);
2239 return;
2240 }
2241
2242 if (!pp->qc_active)
2243 return;
2244
2245 if (ap->ops->scr_read(ap, SCR_ERROR, &serror))
2246 return;
2247 ap->ops->scr_write(ap, SCR_ERROR, serror);
2248
2249 if (ata_stat & ATA_ERR) {
2250 ata_ehi_clear_desc(ehi);
2251 ata_ehi_push_desc(ehi, "Ata error. fis:0x%X", fis);
2252 ehi->err_mask |= AC_ERR_DEV;
2253 ehi->serror |= serror;
2254 ehi->action |= ATA_EH_SOFTRESET;
2255 ata_port_freeze(ap);
2256 return;
2257 }
2258
2259 if (fis & NV_SWNCQ_IRQ_BACKOUT) {
2260 /* If the IRQ is backout, driver must issue
2261 * the new command again some time later.
2262 */
2263 pp->ncq_flags |= ncq_saw_backout;
2264 }
2265
2266 if (fis & NV_SWNCQ_IRQ_SDBFIS) {
2267 pp->ncq_flags |= ncq_saw_sdb;
2268 DPRINTK("id 0x%x SWNCQ: qc_active 0x%X "
2269 "dhfis 0x%X dmafis 0x%X sactive 0x%X\n",
2270 ap->print_id, pp->qc_active, pp->dhfis_bits,
2271 pp->dmafis_bits, readl(pp->sactive_block));
2272 rc = nv_swncq_sdbfis(ap);
2273 if (rc < 0)
2274 goto irq_error;
2275 }
2276
2277 if (fis & NV_SWNCQ_IRQ_DHREGFIS) {
2278 /* The interrupt indicates the new command
2279 * was transmitted correctly to the drive.
2280 */
2281 pp->dhfis_bits |= (0x1 << pp->last_issue_tag);
2282 pp->ncq_flags |= ncq_saw_d2h;
2283 if (pp->ncq_flags & (ncq_saw_sdb | ncq_saw_backout)) {
2284 ata_ehi_push_desc(ehi, "illegal fis transaction");
2285 ehi->err_mask |= AC_ERR_HSM;
2286 ehi->action |= ATA_EH_HARDRESET;
2287 goto irq_error;
2288 }
2289
2290 if (!(fis & NV_SWNCQ_IRQ_DMASETUP) &&
2291 !(pp->ncq_flags & ncq_saw_dmas)) {
2292 ata_stat = ap->ops->check_status(ap);
2293 if (ata_stat & ATA_BUSY)
2294 goto irq_exit;
2295
2296 if (pp->defer_queue.defer_bits) {
2297 DPRINTK("send next command\n");
2298 qc = nv_swncq_qc_from_dq(ap);
2299 nv_swncq_issue_atacmd(ap, qc);
2300 }
2301 }
2302 }
2303
2304 if (fis & NV_SWNCQ_IRQ_DMASETUP) {
2305 /* program the dma controller with appropriate PRD buffers
2306 * and start the DMA transfer for requested command.
2307 */
2308 pp->dmafis_bits |= (0x1 << nv_swncq_tag(ap));
2309 pp->ncq_flags |= ncq_saw_dmas;
2310 rc = nv_swncq_dmafis(ap);
2311 }
2312
2313irq_exit:
2314 return;
2315irq_error:
2316 ata_ehi_push_desc(ehi, "fis:0x%x", fis);
2317 ata_port_freeze(ap);
2318 return;
2319}
2320
2321static irqreturn_t nv_swncq_interrupt(int irq, void *dev_instance)
2322{
2323 struct ata_host *host = dev_instance;
2324 unsigned int i;
2325 unsigned int handled = 0;
2326 unsigned long flags;
2327 u32 irq_stat;
2328
2329 spin_lock_irqsave(&host->lock, flags);
2330
2331 irq_stat = readl(host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_MCP55);
2332
2333 for (i = 0; i < host->n_ports; i++) {
2334 struct ata_port *ap = host->ports[i];
2335
2336 if (ap && !(ap->flags & ATA_FLAG_DISABLED)) {
2337 if (ap->link.sactive) {
2338 nv_swncq_host_interrupt(ap, (u16)irq_stat);
2339 handled = 1;
2340 } else {
2341 if (irq_stat) /* reserve Hotplug */
2342 nv_swncq_irq_clear(ap, 0xfff0);
2343
2344 handled += nv_host_intr(ap, (u8)irq_stat);
2345 }
2346 }
2347 irq_stat >>= NV_INT_PORT_SHIFT_MCP55;
2348 }
2349
2350 spin_unlock_irqrestore(&host->lock, flags);
2351
2352 return IRQ_RETVAL(handled);
2353}
2354
1528static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent) 2355static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1529{ 2356{
1530 static int printed_version = 0; 2357 static int printed_version = 0;
@@ -1551,7 +2378,7 @@ static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1551 return rc; 2378 return rc;
1552 2379
1553 /* determine type and allocate host */ 2380 /* determine type and allocate host */
1554 if (type >= CK804 && adma_enabled) { 2381 if (type == CK804 && adma_enabled) {
1555 dev_printk(KERN_NOTICE, &pdev->dev, "Using ADMA mode\n"); 2382 dev_printk(KERN_NOTICE, &pdev->dev, "Using ADMA mode\n");
1556 type = ADMA; 2383 type = ADMA;
1557 } 2384 }
@@ -1597,6 +2424,9 @@ static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1597 rc = nv_adma_host_init(host); 2424 rc = nv_adma_host_init(host);
1598 if (rc) 2425 if (rc)
1599 return rc; 2426 return rc;
2427 } else if (type == SWNCQ && swncq_enabled) {
2428 dev_printk(KERN_NOTICE, &pdev->dev, "Using SWNCQ mode\n");
2429 nv_swncq_host_init(host);
1600 } 2430 }
1601 2431
1602 pci_set_master(pdev); 2432 pci_set_master(pdev);
@@ -1696,3 +2526,6 @@ module_init(nv_init);
1696module_exit(nv_exit); 2526module_exit(nv_exit);
1697module_param_named(adma, adma_enabled, bool, 0444); 2527module_param_named(adma, adma_enabled, bool, 0444);
1698MODULE_PARM_DESC(adma, "Enable use of ADMA (Default: true)"); 2528MODULE_PARM_DESC(adma, "Enable use of ADMA (Default: true)");
2529module_param_named(swncq, swncq_enabled, bool, 0444);
2530MODULE_PARM_DESC(swncq, "Enable use of SWNCQ (Default: false)");
2531