39 #include <linux/kernel.h>
40 #include <linux/module.h>
42 #include <linux/pci.h>
47 #include <linux/device.h>
52 #define DRV_NAME "sata_nv"
53 #define DRV_VERSION "3.5"
55 #define NV_ADMA_DMA_BOUNDARY 0xffffffffUL
296 #define NV_ADMA_CHECK_INTR(GCTL, PORT) ((GCTL) & (1 << (19 + (12 * (PORT)))))
300 static int nv_pci_device_resume(
struct pci_dev *pdev);
303 static irqreturn_t nv_generic_interrupt(
int irq,
void *dev_instance);
304 static irqreturn_t nv_nf2_interrupt(
int irq,
void *dev_instance);
305 static irqreturn_t nv_ck804_interrupt(
int irq,
void *dev_instance);
309 static int nv_hardreset(
struct ata_link *
link,
unsigned int *
class,
310 unsigned long deadline);
311 static void nv_nf2_freeze(
struct ata_port *ap);
312 static void nv_nf2_thaw(
struct ata_port *ap);
313 static void nv_ck804_freeze(
struct ata_port *ap);
314 static void nv_ck804_thaw(
struct ata_port *ap);
315 static int nv_adma_slave_config(
struct scsi_device *sdev);
319 static irqreturn_t nv_adma_interrupt(
int irq,
void *dev_instance);
320 static void nv_adma_irq_clear(
struct ata_port *ap);
321 static int nv_adma_port_start(
struct ata_port *ap);
322 static void nv_adma_port_stop(
struct ata_port *ap);
325 static int nv_adma_port_resume(
struct ata_port *ap);
327 static void nv_adma_freeze(
struct ata_port *ap);
328 static void nv_adma_thaw(
struct ata_port *ap);
329 static void nv_adma_error_handler(
struct ata_port *ap);
334 static void nv_mcp55_thaw(
struct ata_port *ap);
335 static void nv_mcp55_freeze(
struct ata_port *ap);
336 static void nv_swncq_error_handler(
struct ata_port *ap);
337 static int nv_swncq_slave_config(
struct scsi_device *sdev);
338 static int nv_swncq_port_start(
struct ata_port *ap);
343 static irqreturn_t nv_swncq_interrupt(
int irq,
void *dev_instance);
346 static int nv_swncq_port_resume(
struct ata_port *ap);
381 .id_table = nv_pci_tbl,
382 .probe = nv_init_one,
384 .suspend = ata_pci_device_suspend,
385 .resume = nv_pci_device_resume,
387 .remove = ata_pci_remove_one,
399 .slave_configure = nv_adma_slave_config,
407 .slave_configure = nv_swncq_slave_config,
469 .inherits = &ata_bmdma_port_ops,
471 .scr_read = nv_scr_read,
472 .scr_write = nv_scr_write,
473 .hardreset = nv_hardreset,
477 .inherits = &nv_generic_ops,
483 .inherits = &nv_generic_ops,
484 .
freeze = nv_ck804_freeze,
485 .thaw = nv_ck804_thaw,
486 .host_stop = nv_ck804_host_stop,
490 .inherits = &nv_ck804_ops,
493 .sff_tf_read = nv_adma_tf_read,
495 .qc_prep = nv_adma_qc_prep,
496 .qc_issue = nv_adma_qc_issue,
497 .sff_irq_clear = nv_adma_irq_clear,
499 .freeze = nv_adma_freeze,
500 .thaw = nv_adma_thaw,
501 .error_handler = nv_adma_error_handler,
502 .post_internal_cmd = nv_adma_post_internal_cmd,
504 .port_start = nv_adma_port_start,
505 .port_stop = nv_adma_port_stop,
507 .port_suspend = nv_adma_port_suspend,
508 .port_resume = nv_adma_port_resume,
510 .host_stop = nv_adma_host_stop,
514 .inherits = &nv_generic_ops,
517 .qc_prep = nv_swncq_qc_prep,
518 .qc_issue = nv_swncq_qc_issue,
520 .freeze = nv_mcp55_freeze,
521 .thaw = nv_mcp55_thaw,
522 .error_handler = nv_swncq_error_handler,
525 .port_suspend = nv_swncq_port_suspend,
526 .port_resume = nv_swncq_port_resume,
528 .port_start = nv_swncq_port_start,
536 #define NV_PI_PRIV(_irq_handler, _sht) \
537 &(struct nv_pi_priv){ .irq_handler = _irq_handler, .sht = _sht }
546 .port_ops = &nv_generic_ops,
547 .private_data =
NV_PI_PRIV(nv_generic_interrupt, &nv_sht),
555 .port_ops = &nv_nf2_ops,
556 .private_data =
NV_PI_PRIV(nv_nf2_interrupt, &nv_sht),
564 .port_ops = &nv_ck804_ops,
565 .private_data =
NV_PI_PRIV(nv_ck804_interrupt, &nv_sht),
573 .port_ops = &nv_adma_ops,
574 .private_data =
NV_PI_PRIV(nv_adma_interrupt, &nv_adma_sht),
582 .port_ops = &nv_generic_ops,
583 .private_data =
NV_PI_PRIV(nv_generic_interrupt, &nv_sht),
591 .port_ops = &nv_swncq_ops,
592 .private_data =
NV_PI_PRIV(nv_swncq_interrupt, &nv_swncq_sht),
602 static bool adma_enabled;
603 static bool swncq_enabled = 1;
604 static bool msi_enabled;
606 static void nv_adma_register_mode(
struct ata_port *ap)
623 ata_port_warn(ap,
"timeout waiting for ADMA IDLE, stat=0x%hx\n",
638 "timeout waiting for ADMA LEGACY, stat=0x%hx\n",
644 static void nv_adma_mode(
struct ata_port *ap)
660 while (((status & NV_ADMA_STAT_LEGACY) ||
661 !(status & NV_ADMA_STAT_IDLE)) && count < 20) {
668 "timeout waiting for ADMA LEGACY clear and IDLE, stat=0x%hx\n",
674 static int nv_adma_slave_config(
struct scsi_device *sdev)
681 unsigned long segment_boundary,
flags;
682 unsigned short sg_tablesize;
685 u32 current_reg, new_reg, config_mask;
711 nv_adma_register_mode(ap);
728 new_reg = current_reg | config_mask;
731 new_reg = current_reg & ~config_mask;
735 if (current_reg != new_reg)
738 port0 = ap->
host->ports[0]->private_data;
739 port1 = ap->
host->ports[1]->private_data;
740 sdev0 = ap->
host->ports[0]->link.device[0].sdev;
741 sdev1 = ap->
host->ports[1]->link.device[0].sdev;
775 "DMA mask 0x%llX, segment boundary 0x%lX, hw segs %hu\n",
776 (
unsigned long long)*ap->
host->dev->dma_mask,
777 segment_boundary, sg_tablesize);
779 spin_unlock_irqrestore(ap->
lock, flags);
799 nv_adma_register_mode(ap);
806 unsigned int idx = 0;
836 static int nv_adma_check_cpb(
struct ata_port *ap,
int cpb_num,
int force_err)
839 u8 flags = pp->
cpb[cpb_num].resp_flags;
841 VPRINTK(
"CPB %d, flags=0x%x\n", cpb_num, flags);
881 static int nv_host_intr(
struct ata_port *ap,
u8 irq_stat)
902 return ata_bmdma_port_intr(ap, qc);
905 static irqreturn_t nv_adma_interrupt(
int irq,
void *dev_instance)
909 u32 notifier_clears[2];
911 spin_lock(&host->
lock);
913 for (i = 0; i < host->
n_ports; i++) {
919 u32 notifier, notifier_error;
921 notifier_clears[
i] = 0;
927 handled += nv_host_intr(ap, irq_stat);
935 if (ata_tag_valid(ap->
link.active_tag))
940 irq_stat |= NV_INT_DEV;
941 handled += nv_host_intr(ap, irq_stat);
946 notifier_clears[
i] = notifier | notifier_error;
982 ata_ehi_hotplugged(ehi);
985 ata_ehi_hotplugged(ehi);
999 u32 check_commands = notifier_clears[
i];
1005 if (ata_tag_valid(ap->
link.active_tag))
1006 check_commands = 1 <<
1007 ap->
link.active_tag;
1009 check_commands = ap->
link.sactive;
1013 while ((pos =
ffs(check_commands))) {
1015 rc = nv_adma_check_cpb(ap, pos,
1016 notifier_error & (1 << pos));
1018 done_mask |= 1 <<
pos;
1021 check_commands &= ~(1 <<
pos);
1027 if (notifier_clears[0] || notifier_clears[1]) {
1032 pp = host->
ports[1]->private_data;
1036 spin_unlock(&host->
lock);
1041 static void nv_adma_freeze(
struct ata_port *ap)
1047 nv_ck804_freeze(ap);
1063 static void nv_adma_thaw(
struct ata_port *ap)
1081 static void nv_adma_irq_clear(
struct ata_port *ap)
1085 u32 notifier_clears[2];
1088 ata_bmdma_irq_clear(ap);
1102 notifier_clears[0] = 0xFFFFFFFF;
1103 notifier_clears[1] = 0;
1105 notifier_clears[0] = 0;
1106 notifier_clears[1] = 0xFFFFFFFF;
1108 pp = ap->
host->ports[0]->private_data;
1110 pp = ap->
host->ports[1]->private_data;
1119 ata_bmdma_post_internal_cmd(qc);
1122 static int nv_adma_port_start(
struct ata_port *ap)
1140 rc = pci_set_consistent_dma_mask(pdev,
DMA_BIT_MASK(32));
1145 rc = ata_bmdma_port_start(ap);
1221 static void nv_adma_port_stop(
struct ata_port *ap)
1237 nv_adma_register_mode(ap);
1248 static int nv_adma_port_resume(
struct ata_port *ap)
1283 static void nv_adma_setup_port(
struct ata_port *ap)
1286 struct ata_ioports *
ioport = &ap->ioaddr;
1292 ioport->cmd_addr = mmio;
1294 ioport->error_addr =
1301 ioport->status_addr =
1303 ioport->altstatus_addr =
1304 ioport->ctl_addr = mmio + 0x20;
1307 static int nv_adma_host_init(
struct ata_host *host)
1324 for (i = 0; i < host->
n_ports; i++)
1325 nv_adma_setup_port(host->
ports[i]);
1338 if (idx == qc->
n_elem - 1)
1359 aprd = (si < 5) ? &cpb->
aprd[si] :
1361 nv_adma_fill_aprd(qc, sg, si, aprd);
1393 if (nv_adma_use_reg_mode(qc)) {
1396 nv_adma_register_mode(qc->
ap);
1397 ata_bmdma_qc_prep(qc);
1416 nv_adma_tf_to_cpb(&qc->
tf, cpb->
tf);
1419 nv_adma_fill_sg(qc, cpb);
1449 if (nv_adma_use_reg_mode(qc)) {
1451 VPRINTK(
"using ATA register mode: 0x%lx\n", qc->
flags);
1454 nv_adma_register_mode(qc->
ap);
1455 return ata_bmdma_qc_issue(qc);
1457 nv_adma_mode(qc->
ap);
1477 static irqreturn_t nv_generic_interrupt(
int irq,
void *dev_instance)
1479 struct ata_host *host = dev_instance;
1481 unsigned int handled = 0;
1482 unsigned long flags;
1486 for (i = 0; i < host->
n_ports; i++) {
1490 qc = ata_qc_from_tag(ap, ap->
link.active_tag);
1492 handled += ata_bmdma_port_intr(ap, qc);
1498 ap->
ops->sff_check_status(ap);
1502 spin_unlock_irqrestore(&host->
lock, flags);
1511 for (i = 0; i < host->
n_ports; i++) {
1512 handled += nv_host_intr(host->
ports[i], irq_stat);
1519 static irqreturn_t nv_nf2_interrupt(
int irq,
void *dev_instance)
1521 struct ata_host *host = dev_instance;
1525 spin_lock(&host->
lock);
1527 ret = nv_do_interrupt(host, irq_stat);
1528 spin_unlock(&host->
lock);
1533 static irqreturn_t nv_ck804_interrupt(
int irq,
void *dev_instance)
1535 struct ata_host *host = dev_instance;
1539 spin_lock(&host->
lock);
1541 ret = nv_do_interrupt(host, irq_stat);
1542 spin_unlock(&host->
lock);
1552 *val =
ioread32(link->
ap->ioaddr.scr_addr + (sc_reg * 4));
1556 static int nv_scr_write(
struct ata_link *link,
unsigned int sc_reg,
u32 val)
1561 iowrite32(val, link->
ap->ioaddr.scr_addr + (sc_reg * 4));
1565 static int nv_hardreset(
struct ata_link *link,
unsigned int *
class,
1566 unsigned long deadline)
1574 !ata_dev_enabled(link->
device))
1578 const unsigned long *
timing = sata_ehc_deb_timing(ehc);
1583 "nv: skipping hardreset on occupied port\n");
1597 static void nv_nf2_freeze(
struct ata_port *ap)
1599 void __iomem *scr_addr = ap->
host->ports[0]->ioaddr.scr_addr;
1608 static void nv_nf2_thaw(
struct ata_port *ap)
1610 void __iomem *scr_addr = ap->
host->ports[0]->ioaddr.scr_addr;
1621 static void nv_ck804_freeze(
struct ata_port *ap)
1632 static void nv_ck804_thaw(
struct ata_port *ap)
1645 static void nv_mcp55_freeze(
struct ata_port *ap)
1658 static void nv_mcp55_thaw(
struct ata_port *ap)
1671 static void nv_adma_error_handler(
struct ata_port *ap)
1679 if (ata_tag_valid(ap->
link.active_tag) || ap->
link.sactive) {
1688 "EH in ADMA mode, notifier 0x%X "
1689 "notifier_error 0x%X gen_ctl 0x%X status 0x%X "
1690 "next cpb count 0x%X next cpb idx 0x%x\n",
1691 notifier, notifier_error, gen_ctl, status,
1692 cpb_count, next_cpb_idx);
1696 if ((ata_tag_valid(ap->
link.active_tag) && i == ap->
link.active_tag) ||
1697 ap->
link.sactive & (1 << i))
1699 "CPB %d: ctl_flags 0x%x, resp_flags 0x%x\n",
1705 nv_adma_register_mode(ap);
1724 ata_bmdma_error_handler(ap);
1752 return ata_qc_from_tag(ap, tag);
1755 static void nv_swncq_fis_reinit(
struct ata_port *ap)
1765 static void nv_swncq_pp_reinit(
struct ata_port *ap)
1775 nv_swncq_fis_reinit(ap);
1785 static void __ata_bmdma_stop(
struct ata_port *ap)
1790 ata_bmdma_stop(&qc);
1793 static void nv_swncq_ncq_stop(
struct ata_port *ap)
1800 ata_port_err(ap,
"EH in SWNCQ mode,QC:qc_active 0x%X sactive 0x%X\n",
1803 "SWNCQ:qc_active 0x%X defer_bits 0x%X last_issue_tag 0x%x\n "
1804 "dhfis 0x%X dmafis 0x%X sdbfis 0x%X\n",
1809 ap->
ops->sff_check_status(ap),
1810 ioread8(ap->ioaddr.error_addr));
1815 ata_port_err(ap,
"tag : dhfis dmafis sdbfis sactive\n");
1820 else if (done_mask & (1 << i))
1826 "tag 0x%x: %01x %01x %01x %01x %s\n", i,
1830 (sactive >> i) & 0x1,
1831 (err ?
"error! tag doesn't exit" :
" "));
1834 nv_swncq_pp_reinit(ap);
1835 ap->
ops->sff_irq_clear(ap);
1836 __ata_bmdma_stop(ap);
1837 nv_swncq_irq_clear(ap, 0xffff);
1840 static void nv_swncq_error_handler(
struct ata_port *ap)
1844 if (ap->
link.sactive) {
1845 nv_swncq_ncq_stop(ap);
1849 ata_bmdma_error_handler(ap);
1872 static int nv_swncq_port_resume(
struct ata_port *ap)
1891 static void nv_swncq_host_init(
struct ata_host *host)
1899 pci_read_config_byte(pdev, 0x7f, ®val);
1900 regval &= ~(1 << 7);
1901 pci_write_config_byte(pdev, 0x7f, regval);
1905 VPRINTK(
"HOST_CTL:0x%X\n", tmp);
1910 VPRINTK(
"HOST_ENABLE:0x%X\n", tmp);
1917 static int nv_swncq_slave_config(
struct scsi_device *sdev)
1924 u8 check_maxtor = 0;
1932 dev = &ap->
link.device[sdev->
id];
1944 pci_read_config_byte(pdev, 0x8, &rev);
1954 if (
strncmp(model_num,
"Maxtor", 6) == 0) {
1963 static int nv_swncq_port_start(
struct ata_port *ap)
1971 rc = ata_bmdma_port_start(ap);
1996 ata_bmdma_qc_prep(qc);
2003 nv_swncq_fill_sg(qc);
2012 unsigned int si,
idx;
2025 offset = addr & 0xffff;
2027 if ((offset + sg_len) > 0x10000)
2042 static unsigned int nv_swncq_issue_atacmd(
struct ata_port *ap,
2058 ap->
ops->sff_tf_load(ap, &qc->
tf);
2059 ap->
ops->sff_exec_command(ap, &qc->
tf);
2072 return ata_bmdma_qc_issue(qc);
2077 nv_swncq_issue_atacmd(ap, qc);
2079 nv_swncq_qc_to_dq(ap, qc);
2101 ata_ehi_hotplugged(ehi);
2109 static int nv_swncq_sdbfis(
struct ata_port *ap)
2119 host_stat = ap->
ops->bmdma_status(ap);
2129 ap->
ops->sff_irq_clear(ap);
2130 __ata_bmdma_stop(ap);
2143 nv_swncq_pp_reinit(ap);
2157 DPRINTK(
"id 0x%x QC: qc_active 0x%x,"
2158 "SWNCQ:qc_active 0x%X defer_bits %X "
2159 "dhfis 0x%X dmafis 0x%X last_issue_tag %x\n",
2164 nv_swncq_fis_reinit(ap);
2168 nv_swncq_issue_atacmd(ap, qc);
2174 qc = nv_swncq_qc_from_dq(ap);
2176 nv_swncq_issue_atacmd(ap, qc);
2182 static inline u32 nv_swncq_tag(
struct ata_port *ap)
2188 return (tag & 0x1f);
2191 static void nv_swncq_dmafis(
struct ata_port *ap)
2199 __ata_bmdma_stop(ap);
2200 tag = nv_swncq_tag(ap);
2202 DPRINTK(
"dma setup tag 0x%x\n", tag);
2203 qc = ata_qc_from_tag(ap, tag);
2223 static void nv_swncq_host_interrupt(
struct ata_port *ap,
u16 fis)
2231 ata_stat = ap->
ops->sff_check_status(ap);
2232 nv_swncq_irq_clear(ap, fis);
2240 nv_swncq_hotplug(ap, fis);
2270 DPRINTK(
"id 0x%x SWNCQ: qc_active 0x%X "
2271 "dhfis 0x%X dmafis 0x%X sactive 0x%X\n",
2274 if (nv_swncq_sdbfis(ap) < 0)
2293 ata_stat = ap->
ops->sff_check_status(ap);
2298 DPRINTK(
"send next command\n");
2299 qc = nv_swncq_qc_from_dq(ap);
2300 nv_swncq_issue_atacmd(ap, qc);
2305 if (fis & NV_SWNCQ_IRQ_DMASETUP) {
2311 nv_swncq_dmafis(ap);
2322 static irqreturn_t nv_swncq_interrupt(
int irq,
void *dev_instance)
2324 struct ata_host *host = dev_instance;
2326 unsigned int handled = 0;
2327 unsigned long flags;
2334 for (i = 0; i < host->
n_ports; i++) {
2337 if (ap->
link.sactive) {
2338 nv_swncq_host_interrupt(ap, (
u16)irq_stat);
2342 nv_swncq_irq_clear(ap, 0xfff0);
2344 handled += nv_host_intr(ap, (
u8)irq_stat);
2349 spin_unlock_irqrestore(&host->
lock, flags);
2368 for (bar = 0; bar < 6; bar++)
2379 if (type ==
CK804 && adma_enabled) {
2380 dev_notice(&pdev->
dev,
"Using ADMA mode\n");
2382 }
else if (type ==
MCP5x && swncq_enabled) {
2383 dev_notice(&pdev->
dev,
"Using SWNCQ mode\n");
2387 ppi[0] = &nv_port_info[
type];
2389 rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
2410 if (type >=
CK804) {
2420 rc = nv_adma_host_init(host);
2423 }
else if (type ==
SWNCQ)
2424 nv_swncq_host_init(host);
2427 dev_notice(&pdev->
dev,
"Using MSI\n");
2428 pci_enable_msi(pdev);
2432 return ata_pci_sff_activate_host(host, ipriv->
irq_handler, ipriv->
sht);
2436 static int nv_pci_device_resume(
struct pci_dev *pdev)
2442 rc = ata_pci_device_do_resume(pdev);
2460 pp = host->
ports[0]->private_data;
2467 pp = host->
ports[1]->private_data;
2479 ata_host_resume(host);
2485 static void nv_ck804_host_stop(
struct ata_host *host)
2496 static void nv_adma_host_stop(
struct ata_host *host)
2510 nv_ck804_host_stop(host);