aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ata/sata_nv.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ata/sata_nv.c')
-rw-r--r--drivers/ata/sata_nv.c629
1 files changed, 375 insertions, 254 deletions
diff --git a/drivers/ata/sata_nv.c b/drivers/ata/sata_nv.c
index f7a963eb1f02..095ef1b2cd0e 100644
--- a/drivers/ata/sata_nv.c
+++ b/drivers/ata/sata_nv.c
@@ -49,11 +49,13 @@
49#include <linux/libata.h> 49#include <linux/libata.h>
50 50
51#define DRV_NAME "sata_nv" 51#define DRV_NAME "sata_nv"
52#define DRV_VERSION "3.2" 52#define DRV_VERSION "3.3"
53 53
54#define NV_ADMA_DMA_BOUNDARY 0xffffffffUL 54#define NV_ADMA_DMA_BOUNDARY 0xffffffffUL
55 55
56enum { 56enum {
57 NV_MMIO_BAR = 5,
58
57 NV_PORTS = 2, 59 NV_PORTS = 2,
58 NV_PIO_MASK = 0x1f, 60 NV_PIO_MASK = 0x1f,
59 NV_MWDMA_MASK = 0x07, 61 NV_MWDMA_MASK = 0x07,
@@ -213,12 +215,21 @@ struct nv_adma_port_priv {
213 dma_addr_t cpb_dma; 215 dma_addr_t cpb_dma;
214 struct nv_adma_prd *aprd; 216 struct nv_adma_prd *aprd;
215 dma_addr_t aprd_dma; 217 dma_addr_t aprd_dma;
218 void __iomem * ctl_block;
219 void __iomem * gen_block;
220 void __iomem * notifier_clear_block;
216 u8 flags; 221 u8 flags;
217}; 222};
218 223
224struct nv_host_priv {
225 unsigned long type;
226};
227
219#define NV_ADMA_CHECK_INTR(GCTL, PORT) ((GCTL) & ( 1 << (19 + (12 * (PORT))))) 228#define NV_ADMA_CHECK_INTR(GCTL, PORT) ((GCTL) & ( 1 << (19 + (12 * (PORT)))))
220 229
221static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent); 230static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
231static void nv_remove_one (struct pci_dev *pdev);
232static int nv_pci_device_resume(struct pci_dev *pdev);
222static void nv_ck804_host_stop(struct ata_host *host); 233static void nv_ck804_host_stop(struct ata_host *host);
223static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance); 234static irqreturn_t nv_generic_interrupt(int irq, void *dev_instance);
224static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance); 235static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance);
@@ -239,6 +250,8 @@ static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance);
239static void nv_adma_irq_clear(struct ata_port *ap); 250static void nv_adma_irq_clear(struct ata_port *ap);
240static int nv_adma_port_start(struct ata_port *ap); 251static int nv_adma_port_start(struct ata_port *ap);
241static void nv_adma_port_stop(struct ata_port *ap); 252static void nv_adma_port_stop(struct ata_port *ap);
253static int nv_adma_port_suspend(struct ata_port *ap, pm_message_t mesg);
254static int nv_adma_port_resume(struct ata_port *ap);
242static void nv_adma_error_handler(struct ata_port *ap); 255static void nv_adma_error_handler(struct ata_port *ap);
243static void nv_adma_host_stop(struct ata_host *host); 256static void nv_adma_host_stop(struct ata_host *host);
244static void nv_adma_bmdma_setup(struct ata_queued_cmd *qc); 257static void nv_adma_bmdma_setup(struct ata_queued_cmd *qc);
@@ -284,7 +297,9 @@ static struct pci_driver nv_pci_driver = {
284 .name = DRV_NAME, 297 .name = DRV_NAME,
285 .id_table = nv_pci_tbl, 298 .id_table = nv_pci_tbl,
286 .probe = nv_init_one, 299 .probe = nv_init_one,
287 .remove = ata_pci_remove_one, 300 .suspend = ata_pci_device_suspend,
301 .resume = nv_pci_device_resume,
302 .remove = nv_remove_one,
288}; 303};
289 304
290static struct scsi_host_template nv_sht = { 305static struct scsi_host_template nv_sht = {
@@ -303,6 +318,8 @@ static struct scsi_host_template nv_sht = {
303 .slave_configure = ata_scsi_slave_config, 318 .slave_configure = ata_scsi_slave_config,
304 .slave_destroy = ata_scsi_slave_destroy, 319 .slave_destroy = ata_scsi_slave_destroy,
305 .bios_param = ata_std_bios_param, 320 .bios_param = ata_std_bios_param,
321 .suspend = ata_scsi_device_suspend,
322 .resume = ata_scsi_device_resume,
306}; 323};
307 324
308static struct scsi_host_template nv_adma_sht = { 325static struct scsi_host_template nv_adma_sht = {
@@ -321,6 +338,8 @@ static struct scsi_host_template nv_adma_sht = {
321 .slave_configure = nv_adma_slave_config, 338 .slave_configure = nv_adma_slave_config,
322 .slave_destroy = ata_scsi_slave_destroy, 339 .slave_destroy = ata_scsi_slave_destroy,
323 .bios_param = ata_std_bios_param, 340 .bios_param = ata_std_bios_param,
341 .suspend = ata_scsi_device_suspend,
342 .resume = ata_scsi_device_resume,
324}; 343};
325 344
326static const struct ata_port_operations nv_generic_ops = { 345static const struct ata_port_operations nv_generic_ops = {
@@ -340,14 +359,14 @@ static const struct ata_port_operations nv_generic_ops = {
340 .thaw = ata_bmdma_thaw, 359 .thaw = ata_bmdma_thaw,
341 .error_handler = nv_error_handler, 360 .error_handler = nv_error_handler,
342 .post_internal_cmd = ata_bmdma_post_internal_cmd, 361 .post_internal_cmd = ata_bmdma_post_internal_cmd,
343 .data_xfer = ata_pio_data_xfer, 362 .data_xfer = ata_data_xfer,
344 .irq_handler = nv_generic_interrupt, 363 .irq_handler = nv_generic_interrupt,
345 .irq_clear = ata_bmdma_irq_clear, 364 .irq_clear = ata_bmdma_irq_clear,
365 .irq_on = ata_irq_on,
366 .irq_ack = ata_irq_ack,
346 .scr_read = nv_scr_read, 367 .scr_read = nv_scr_read,
347 .scr_write = nv_scr_write, 368 .scr_write = nv_scr_write,
348 .port_start = ata_port_start, 369 .port_start = ata_port_start,
349 .port_stop = ata_port_stop,
350 .host_stop = ata_pci_host_stop,
351}; 370};
352 371
353static const struct ata_port_operations nv_nf2_ops = { 372static const struct ata_port_operations nv_nf2_ops = {
@@ -367,14 +386,14 @@ static const struct ata_port_operations nv_nf2_ops = {
367 .thaw = nv_nf2_thaw, 386 .thaw = nv_nf2_thaw,
368 .error_handler = nv_error_handler, 387 .error_handler = nv_error_handler,
369 .post_internal_cmd = ata_bmdma_post_internal_cmd, 388 .post_internal_cmd = ata_bmdma_post_internal_cmd,
370 .data_xfer = ata_pio_data_xfer, 389 .data_xfer = ata_data_xfer,
371 .irq_handler = nv_nf2_interrupt, 390 .irq_handler = nv_nf2_interrupt,
372 .irq_clear = ata_bmdma_irq_clear, 391 .irq_clear = ata_bmdma_irq_clear,
392 .irq_on = ata_irq_on,
393 .irq_ack = ata_irq_ack,
373 .scr_read = nv_scr_read, 394 .scr_read = nv_scr_read,
374 .scr_write = nv_scr_write, 395 .scr_write = nv_scr_write,
375 .port_start = ata_port_start, 396 .port_start = ata_port_start,
376 .port_stop = ata_port_stop,
377 .host_stop = ata_pci_host_stop,
378}; 397};
379 398
380static const struct ata_port_operations nv_ck804_ops = { 399static const struct ata_port_operations nv_ck804_ops = {
@@ -394,13 +413,14 @@ static const struct ata_port_operations nv_ck804_ops = {
394 .thaw = nv_ck804_thaw, 413 .thaw = nv_ck804_thaw,
395 .error_handler = nv_error_handler, 414 .error_handler = nv_error_handler,
396 .post_internal_cmd = ata_bmdma_post_internal_cmd, 415 .post_internal_cmd = ata_bmdma_post_internal_cmd,
397 .data_xfer = ata_pio_data_xfer, 416 .data_xfer = ata_data_xfer,
398 .irq_handler = nv_ck804_interrupt, 417 .irq_handler = nv_ck804_interrupt,
399 .irq_clear = ata_bmdma_irq_clear, 418 .irq_clear = ata_bmdma_irq_clear,
419 .irq_on = ata_irq_on,
420 .irq_ack = ata_irq_ack,
400 .scr_read = nv_scr_read, 421 .scr_read = nv_scr_read,
401 .scr_write = nv_scr_write, 422 .scr_write = nv_scr_write,
402 .port_start = ata_port_start, 423 .port_start = ata_port_start,
403 .port_stop = ata_port_stop,
404 .host_stop = nv_ck804_host_stop, 424 .host_stop = nv_ck804_host_stop,
405}; 425};
406 426
@@ -422,13 +442,17 @@ static const struct ata_port_operations nv_adma_ops = {
422 .thaw = nv_ck804_thaw, 442 .thaw = nv_ck804_thaw,
423 .error_handler = nv_adma_error_handler, 443 .error_handler = nv_adma_error_handler,
424 .post_internal_cmd = nv_adma_bmdma_stop, 444 .post_internal_cmd = nv_adma_bmdma_stop,
425 .data_xfer = ata_mmio_data_xfer, 445 .data_xfer = ata_data_xfer,
426 .irq_handler = nv_adma_interrupt, 446 .irq_handler = nv_adma_interrupt,
427 .irq_clear = nv_adma_irq_clear, 447 .irq_clear = nv_adma_irq_clear,
448 .irq_on = ata_irq_on,
449 .irq_ack = ata_irq_ack,
428 .scr_read = nv_scr_read, 450 .scr_read = nv_scr_read,
429 .scr_write = nv_scr_write, 451 .scr_write = nv_scr_write,
430 .port_start = nv_adma_port_start, 452 .port_start = nv_adma_port_start,
431 .port_stop = nv_adma_port_stop, 453 .port_stop = nv_adma_port_stop,
454 .port_suspend = nv_adma_port_suspend,
455 .port_resume = nv_adma_port_resume,
432 .host_stop = nv_adma_host_stop, 456 .host_stop = nv_adma_host_stop,
433}; 457};
434 458
@@ -467,6 +491,7 @@ static struct ata_port_info nv_port_info[] = {
467 { 491 {
468 .sht = &nv_adma_sht, 492 .sht = &nv_adma_sht,
469 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY | 493 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
494 ATA_FLAG_HRST_TO_RESUME |
470 ATA_FLAG_MMIO | ATA_FLAG_NCQ, 495 ATA_FLAG_MMIO | ATA_FLAG_NCQ,
471 .pio_mask = NV_PIO_MASK, 496 .pio_mask = NV_PIO_MASK,
472 .mwdma_mask = NV_MWDMA_MASK, 497 .mwdma_mask = NV_MWDMA_MASK,
@@ -483,57 +508,72 @@ MODULE_VERSION(DRV_VERSION);
483 508
484static int adma_enabled = 1; 509static int adma_enabled = 1;
485 510
486static inline void __iomem *__nv_adma_ctl_block(void __iomem *mmio,
487 unsigned int port_no)
488{
489 mmio += NV_ADMA_PORT + port_no * NV_ADMA_PORT_SIZE;
490 return mmio;
491}
492
493static inline void __iomem *nv_adma_ctl_block(struct ata_port *ap)
494{
495 return __nv_adma_ctl_block(ap->host->mmio_base, ap->port_no);
496}
497
498static inline void __iomem *nv_adma_gen_block(struct ata_port *ap)
499{
500 return (ap->host->mmio_base + NV_ADMA_GEN);
501}
502
503static inline void __iomem *nv_adma_notifier_clear_block(struct ata_port *ap)
504{
505 return (nv_adma_gen_block(ap) + NV_ADMA_NOTIFIER_CLEAR + (4 * ap->port_no));
506}
507
508static void nv_adma_register_mode(struct ata_port *ap) 511static void nv_adma_register_mode(struct ata_port *ap)
509{ 512{
510 void __iomem *mmio = nv_adma_ctl_block(ap);
511 struct nv_adma_port_priv *pp = ap->private_data; 513 struct nv_adma_port_priv *pp = ap->private_data;
512 u16 tmp; 514 void __iomem *mmio = pp->ctl_block;
515 u16 tmp, status;
516 int count = 0;
513 517
514 if (pp->flags & NV_ADMA_PORT_REGISTER_MODE) 518 if (pp->flags & NV_ADMA_PORT_REGISTER_MODE)
515 return; 519 return;
516 520
521 status = readw(mmio + NV_ADMA_STAT);
522 while(!(status & NV_ADMA_STAT_IDLE) && count < 20) {
523 ndelay(50);
524 status = readw(mmio + NV_ADMA_STAT);
525 count++;
526 }
527 if(count == 20)
528 ata_port_printk(ap, KERN_WARNING,
529 "timeout waiting for ADMA IDLE, stat=0x%hx\n",
530 status);
531
517 tmp = readw(mmio + NV_ADMA_CTL); 532 tmp = readw(mmio + NV_ADMA_CTL);
518 writew(tmp & ~NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL); 533 writew(tmp & ~NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL);
519 534
535 count = 0;
536 status = readw(mmio + NV_ADMA_STAT);
537 while(!(status & NV_ADMA_STAT_LEGACY) && count < 20) {
538 ndelay(50);
539 status = readw(mmio + NV_ADMA_STAT);
540 count++;
541 }
542 if(count == 20)
543 ata_port_printk(ap, KERN_WARNING,
544 "timeout waiting for ADMA LEGACY, stat=0x%hx\n",
545 status);
546
520 pp->flags |= NV_ADMA_PORT_REGISTER_MODE; 547 pp->flags |= NV_ADMA_PORT_REGISTER_MODE;
521} 548}
522 549
523static void nv_adma_mode(struct ata_port *ap) 550static void nv_adma_mode(struct ata_port *ap)
524{ 551{
525 void __iomem *mmio = nv_adma_ctl_block(ap);
526 struct nv_adma_port_priv *pp = ap->private_data; 552 struct nv_adma_port_priv *pp = ap->private_data;
527 u16 tmp; 553 void __iomem *mmio = pp->ctl_block;
554 u16 tmp, status;
555 int count = 0;
528 556
529 if (!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)) 557 if (!(pp->flags & NV_ADMA_PORT_REGISTER_MODE))
530 return; 558 return;
531 559
532 WARN_ON(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE); 560 WARN_ON(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE);
533 561
534 tmp = readw(mmio + NV_ADMA_CTL); 562 tmp = readw(mmio + NV_ADMA_CTL);
535 writew(tmp | NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL); 563 writew(tmp | NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL);
536 564
565 status = readw(mmio + NV_ADMA_STAT);
566 while(((status & NV_ADMA_STAT_LEGACY) ||
567 !(status & NV_ADMA_STAT_IDLE)) && count < 20) {
568 ndelay(50);
569 status = readw(mmio + NV_ADMA_STAT);
570 count++;
571 }
572 if(count == 20)
573 ata_port_printk(ap, KERN_WARNING,
574 "timeout waiting for ADMA LEGACY clear and IDLE, stat=0x%hx\n",
575 status);
576
537 pp->flags &= ~NV_ADMA_PORT_REGISTER_MODE; 577 pp->flags &= ~NV_ADMA_PORT_REGISTER_MODE;
538} 578}
539 579
@@ -568,7 +608,7 @@ static int nv_adma_slave_config(struct scsi_device *sdev)
568 /* Subtract 1 since an extra entry may be needed for padding, see 608 /* Subtract 1 since an extra entry may be needed for padding, see
569 libata-scsi.c */ 609 libata-scsi.c */
570 sg_tablesize = LIBATA_MAX_PRD - 1; 610 sg_tablesize = LIBATA_MAX_PRD - 1;
571 611
572 /* Since the legacy DMA engine is in use, we need to disable ADMA 612 /* Since the legacy DMA engine is in use, we need to disable ADMA
573 on the port. */ 613 on the port. */
574 adma_enable = 0; 614 adma_enable = 0;
@@ -580,7 +620,7 @@ static int nv_adma_slave_config(struct scsi_device *sdev)
580 sg_tablesize = NV_ADMA_SGTBL_TOTAL_LEN; 620 sg_tablesize = NV_ADMA_SGTBL_TOTAL_LEN;
581 adma_enable = 1; 621 adma_enable = 1;
582 } 622 }
583 623
584 pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &current_reg); 624 pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &current_reg);
585 625
586 if(ap->port_no == 1) 626 if(ap->port_no == 1)
@@ -589,7 +629,7 @@ static int nv_adma_slave_config(struct scsi_device *sdev)
589 else 629 else
590 config_mask = NV_MCP_SATA_CFG_20_PORT0_EN | 630 config_mask = NV_MCP_SATA_CFG_20_PORT0_EN |
591 NV_MCP_SATA_CFG_20_PORT0_PWB_EN; 631 NV_MCP_SATA_CFG_20_PORT0_PWB_EN;
592 632
593 if(adma_enable) { 633 if(adma_enable) {
594 new_reg = current_reg | config_mask; 634 new_reg = current_reg | config_mask;
595 pp->flags &= ~NV_ADMA_ATAPI_SETUP_COMPLETE; 635 pp->flags &= ~NV_ADMA_ATAPI_SETUP_COMPLETE;
@@ -598,10 +638,10 @@ static int nv_adma_slave_config(struct scsi_device *sdev)
598 new_reg = current_reg & ~config_mask; 638 new_reg = current_reg & ~config_mask;
599 pp->flags |= NV_ADMA_ATAPI_SETUP_COMPLETE; 639 pp->flags |= NV_ADMA_ATAPI_SETUP_COMPLETE;
600 } 640 }
601 641
602 if(current_reg != new_reg) 642 if(current_reg != new_reg)
603 pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, new_reg); 643 pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, new_reg);
604 644
605 blk_queue_bounce_limit(sdev->request_queue, bounce_limit); 645 blk_queue_bounce_limit(sdev->request_queue, bounce_limit);
606 blk_queue_segment_boundary(sdev->request_queue, segment_boundary); 646 blk_queue_segment_boundary(sdev->request_queue, segment_boundary);
607 blk_queue_max_hw_segments(sdev->request_queue, sg_tablesize); 647 blk_queue_max_hw_segments(sdev->request_queue, sg_tablesize);
@@ -648,53 +688,62 @@ static unsigned int nv_adma_tf_to_cpb(struct ata_taskfile *tf, __le16 *cpb)
648 return idx; 688 return idx;
649} 689}
650 690
651static void nv_adma_check_cpb(struct ata_port *ap, int cpb_num, int force_err) 691static int nv_adma_check_cpb(struct ata_port *ap, int cpb_num, int force_err)
652{ 692{
653 struct nv_adma_port_priv *pp = ap->private_data; 693 struct nv_adma_port_priv *pp = ap->private_data;
654 int complete = 0, have_err = 0;
655 u8 flags = pp->cpb[cpb_num].resp_flags; 694 u8 flags = pp->cpb[cpb_num].resp_flags;
656 695
657 VPRINTK("CPB %d, flags=0x%x\n", cpb_num, flags); 696 VPRINTK("CPB %d, flags=0x%x\n", cpb_num, flags);
658 697
659 if (flags & NV_CPB_RESP_DONE) { 698 if (unlikely((force_err ||
660 VPRINTK("CPB flags done, flags=0x%x\n", flags); 699 flags & (NV_CPB_RESP_ATA_ERR |
661 complete = 1; 700 NV_CPB_RESP_CMD_ERR |
662 } 701 NV_CPB_RESP_CPB_ERR)))) {
663 if (flags & NV_CPB_RESP_ATA_ERR) { 702 struct ata_eh_info *ehi = &ap->eh_info;
664 ata_port_printk(ap, KERN_ERR, "CPB flags ATA err, flags=0x%x\n", flags); 703 int freeze = 0;
665 have_err = 1; 704
666 complete = 1; 705 ata_ehi_clear_desc(ehi);
667 } 706 ata_ehi_push_desc(ehi, "CPB resp_flags 0x%x", flags );
668 if (flags & NV_CPB_RESP_CMD_ERR) { 707 if (flags & NV_CPB_RESP_ATA_ERR) {
669 ata_port_printk(ap, KERN_ERR, "CPB flags CMD err, flags=0x%x\n", flags); 708 ata_ehi_push_desc(ehi, ": ATA error");
670 have_err = 1; 709 ehi->err_mask |= AC_ERR_DEV;
671 complete = 1; 710 } else if (flags & NV_CPB_RESP_CMD_ERR) {
672 } 711 ata_ehi_push_desc(ehi, ": CMD error");
673 if (flags & NV_CPB_RESP_CPB_ERR) { 712 ehi->err_mask |= AC_ERR_DEV;
674 ata_port_printk(ap, KERN_ERR, "CPB flags CPB err, flags=0x%x\n", flags); 713 } else if (flags & NV_CPB_RESP_CPB_ERR) {
675 have_err = 1; 714 ata_ehi_push_desc(ehi, ": CPB error");
676 complete = 1; 715 ehi->err_mask |= AC_ERR_SYSTEM;
716 freeze = 1;
717 } else {
718 /* notifier error, but no error in CPB flags? */
719 ehi->err_mask |= AC_ERR_OTHER;
720 freeze = 1;
721 }
722 /* Kill all commands. EH will determine what actually failed. */
723 if (freeze)
724 ata_port_freeze(ap);
725 else
726 ata_port_abort(ap);
727 return 1;
677 } 728 }
678 if(complete || force_err) 729
679 { 730 if (flags & NV_CPB_RESP_DONE) {
680 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, cpb_num); 731 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, cpb_num);
681 if(likely(qc)) { 732 VPRINTK("CPB flags done, flags=0x%x\n", flags);
682 u8 ata_status = 0; 733 if (likely(qc)) {
683 /* Only use the ATA port status for non-NCQ commands. 734 /* Grab the ATA port status for non-NCQ commands.
684 For NCQ commands the current status may have nothing to do with 735 For NCQ commands the current status may have nothing to do with
685 the command just completed. */ 736 the command just completed. */
686 if(qc->tf.protocol != ATA_PROT_NCQ) 737 if (qc->tf.protocol != ATA_PROT_NCQ) {
687 ata_status = readb(nv_adma_ctl_block(ap) + (ATA_REG_STATUS * 4)); 738 u8 ata_status = readb(pp->ctl_block + (ATA_REG_STATUS * 4));
688 739 qc->err_mask |= ac_err_mask(ata_status);
689 if(have_err || force_err) 740 }
690 ata_status |= ATA_ERR;
691
692 qc->err_mask |= ac_err_mask(ata_status);
693 DPRINTK("Completing qc from tag %d with err_mask %u\n",cpb_num, 741 DPRINTK("Completing qc from tag %d with err_mask %u\n",cpb_num,
694 qc->err_mask); 742 qc->err_mask);
695 ata_qc_complete(qc); 743 ata_qc_complete(qc);
696 } 744 }
697 } 745 }
746 return 0;
698} 747}
699 748
700static int nv_host_intr(struct ata_port *ap, u8 irq_stat) 749static int nv_host_intr(struct ata_port *ap, u8 irq_stat)
@@ -735,15 +784,14 @@ static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance)
735 784
736 if (ap && !(ap->flags & ATA_FLAG_DISABLED)) { 785 if (ap && !(ap->flags & ATA_FLAG_DISABLED)) {
737 struct nv_adma_port_priv *pp = ap->private_data; 786 struct nv_adma_port_priv *pp = ap->private_data;
738 void __iomem *mmio = nv_adma_ctl_block(ap); 787 void __iomem *mmio = pp->ctl_block;
739 u16 status; 788 u16 status;
740 u32 gen_ctl; 789 u32 gen_ctl;
741 int have_global_err = 0;
742 u32 notifier, notifier_error; 790 u32 notifier, notifier_error;
743 791
744 /* if in ATA register mode, use standard ata interrupt handler */ 792 /* if in ATA register mode, use standard ata interrupt handler */
745 if (pp->flags & NV_ADMA_PORT_REGISTER_MODE) { 793 if (pp->flags & NV_ADMA_PORT_REGISTER_MODE) {
746 u8 irq_stat = readb(host->mmio_base + NV_INT_STATUS_CK804) 794 u8 irq_stat = readb(host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_CK804)
747 >> (NV_INT_PORT_SHIFT * i); 795 >> (NV_INT_PORT_SHIFT * i);
748 if(ata_tag_valid(ap->active_tag)) 796 if(ata_tag_valid(ap->active_tag))
749 /** NV_INT_DEV indication seems unreliable at times 797 /** NV_INT_DEV indication seems unreliable at times
@@ -758,7 +806,7 @@ static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance)
758 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR); 806 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
759 notifier_clears[i] = notifier | notifier_error; 807 notifier_clears[i] = notifier | notifier_error;
760 808
761 gen_ctl = readl(nv_adma_gen_block(ap) + NV_ADMA_GEN_CTL); 809 gen_ctl = readl(pp->gen_block + NV_ADMA_GEN_CTL);
762 810
763 if( !NV_ADMA_CHECK_INTR(gen_ctl, ap->port_no) && !notifier && 811 if( !NV_ADMA_CHECK_INTR(gen_ctl, ap->port_no) && !notifier &&
764 !notifier_error) 812 !notifier_error)
@@ -774,52 +822,60 @@ static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance)
774 readw(mmio + NV_ADMA_STAT); /* flush posted write */ 822 readw(mmio + NV_ADMA_STAT); /* flush posted write */
775 rmb(); 823 rmb();
776 824
777 /* freeze if hotplugged */ 825 handled++; /* irq handled if we got here */
778 if (unlikely(status & (NV_ADMA_STAT_HOTPLUG | NV_ADMA_STAT_HOTUNPLUG))) { 826
779 ata_port_printk(ap, KERN_NOTICE, "Hotplug event, freezing\n"); 827 /* freeze if hotplugged or controller error */
828 if (unlikely(status & (NV_ADMA_STAT_HOTPLUG |
829 NV_ADMA_STAT_HOTUNPLUG |
830 NV_ADMA_STAT_TIMEOUT))) {
831 struct ata_eh_info *ehi = &ap->eh_info;
832
833 ata_ehi_clear_desc(ehi);
834 ata_ehi_push_desc(ehi, "ADMA status 0x%08x", status );
835 if (status & NV_ADMA_STAT_TIMEOUT) {
836 ehi->err_mask |= AC_ERR_SYSTEM;
837 ata_ehi_push_desc(ehi, ": timeout");
838 } else if (status & NV_ADMA_STAT_HOTPLUG) {
839 ata_ehi_hotplugged(ehi);
840 ata_ehi_push_desc(ehi, ": hotplug");
841 } else if (status & NV_ADMA_STAT_HOTUNPLUG) {
842 ata_ehi_hotplugged(ehi);
843 ata_ehi_push_desc(ehi, ": hot unplug");
844 }
780 ata_port_freeze(ap); 845 ata_port_freeze(ap);
781 handled++;
782 continue; 846 continue;
783 } 847 }
784 848
785 if (status & NV_ADMA_STAT_TIMEOUT) { 849 if (status & (NV_ADMA_STAT_DONE |
786 ata_port_printk(ap, KERN_ERR, "timeout, stat=0x%x\n", status); 850 NV_ADMA_STAT_CPBERR)) {
787 have_global_err = 1;
788 }
789 if (status & NV_ADMA_STAT_CPBERR) {
790 ata_port_printk(ap, KERN_ERR, "CPB error, stat=0x%x\n", status);
791 have_global_err = 1;
792 }
793 if ((status & NV_ADMA_STAT_DONE) || have_global_err) {
794 /** Check CPBs for completed commands */ 851 /** Check CPBs for completed commands */
795 852
796 if(ata_tag_valid(ap->active_tag)) 853 if (ata_tag_valid(ap->active_tag)) {
797 /* Non-NCQ command */ 854 /* Non-NCQ command */
798 nv_adma_check_cpb(ap, ap->active_tag, have_global_err || 855 nv_adma_check_cpb(ap, ap->active_tag,
799 (notifier_error & (1 << ap->active_tag))); 856 notifier_error & (1 << ap->active_tag));
800 else { 857 } else {
801 int pos; 858 int pos, error = 0;
802 u32 active = ap->sactive; 859 u32 active = ap->sactive;
803 while( (pos = ffs(active)) ) { 860
861 while ((pos = ffs(active)) && !error) {
804 pos--; 862 pos--;
805 nv_adma_check_cpb(ap, pos, have_global_err || 863 error = nv_adma_check_cpb(ap, pos,
806 (notifier_error & (1 << pos)) ); 864 notifier_error & (1 << pos) );
807 active &= ~(1 << pos ); 865 active &= ~(1 << pos );
808 } 866 }
809 } 867 }
810 } 868 }
811
812 handled++; /* irq handled if we got here */
813 } 869 }
814 } 870 }
815 871
816 if(notifier_clears[0] || notifier_clears[1]) { 872 if(notifier_clears[0] || notifier_clears[1]) {
817 /* Note: Both notifier clear registers must be written 873 /* Note: Both notifier clear registers must be written
818 if either is set, even if one is zero, according to NVIDIA. */ 874 if either is set, even if one is zero, according to NVIDIA. */
819 writel(notifier_clears[0], 875 struct nv_adma_port_priv *pp = host->ports[0]->private_data;
820 nv_adma_notifier_clear_block(host->ports[0])); 876 writel(notifier_clears[0], pp->notifier_clear_block);
821 writel(notifier_clears[1], 877 pp = host->ports[1]->private_data;
822 nv_adma_notifier_clear_block(host->ports[1])); 878 writel(notifier_clears[1], pp->notifier_clear_block);
823 } 879 }
824 880
825 spin_unlock(&host->lock); 881 spin_unlock(&host->lock);
@@ -829,19 +885,20 @@ static irqreturn_t nv_adma_interrupt(int irq, void *dev_instance)
829 885
830static void nv_adma_irq_clear(struct ata_port *ap) 886static void nv_adma_irq_clear(struct ata_port *ap)
831{ 887{
832 void __iomem *mmio = nv_adma_ctl_block(ap); 888 struct nv_adma_port_priv *pp = ap->private_data;
889 void __iomem *mmio = pp->ctl_block;
833 u16 status = readw(mmio + NV_ADMA_STAT); 890 u16 status = readw(mmio + NV_ADMA_STAT);
834 u32 notifier = readl(mmio + NV_ADMA_NOTIFIER); 891 u32 notifier = readl(mmio + NV_ADMA_NOTIFIER);
835 u32 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR); 892 u32 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
836 unsigned long dma_stat_addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS; 893 void __iomem *dma_stat_addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
837 894
838 /* clear ADMA status */ 895 /* clear ADMA status */
839 writew(status, mmio + NV_ADMA_STAT); 896 writew(status, mmio + NV_ADMA_STAT);
840 writel(notifier | notifier_error, 897 writel(notifier | notifier_error,
841 nv_adma_notifier_clear_block(ap)); 898 pp->notifier_clear_block);
842 899
843 /** clear legacy status */ 900 /** clear legacy status */
844 outb(inb(dma_stat_addr), dma_stat_addr); 901 iowrite8(ioread8(dma_stat_addr), dma_stat_addr);
845} 902}
846 903
847static void nv_adma_bmdma_setup(struct ata_queued_cmd *qc) 904static void nv_adma_bmdma_setup(struct ata_queued_cmd *qc)
@@ -857,15 +914,15 @@ static void nv_adma_bmdma_setup(struct ata_queued_cmd *qc)
857 } 914 }
858 915
859 /* load PRD table addr. */ 916 /* load PRD table addr. */
860 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS); 917 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
861 918
862 /* specify data direction, triple-check start bit is clear */ 919 /* specify data direction, triple-check start bit is clear */
863 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 920 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
864 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START); 921 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
865 if (!rw) 922 if (!rw)
866 dmactl |= ATA_DMA_WR; 923 dmactl |= ATA_DMA_WR;
867 924
868 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 925 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
869 926
870 /* issue r/w command */ 927 /* issue r/w command */
871 ata_exec_command(ap, &qc->tf); 928 ata_exec_command(ap, &qc->tf);
@@ -883,9 +940,9 @@ static void nv_adma_bmdma_start(struct ata_queued_cmd *qc)
883 } 940 }
884 941
885 /* start host DMA transaction */ 942 /* start host DMA transaction */
886 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 943 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
887 outb(dmactl | ATA_DMA_START, 944 iowrite8(dmactl | ATA_DMA_START,
888 ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 945 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
889} 946}
890 947
891static void nv_adma_bmdma_stop(struct ata_queued_cmd *qc) 948static void nv_adma_bmdma_stop(struct ata_queued_cmd *qc)
@@ -897,8 +954,8 @@ static void nv_adma_bmdma_stop(struct ata_queued_cmd *qc)
897 return; 954 return;
898 955
899 /* clear start/stop bit */ 956 /* clear start/stop bit */
900 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START, 957 iowrite8(ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
901 ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 958 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
902 959
903 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */ 960 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
904 ata_altstatus(ap); /* dummy read */ 961 ata_altstatus(ap); /* dummy read */
@@ -910,7 +967,7 @@ static u8 nv_adma_bmdma_status(struct ata_port *ap)
910 967
911 WARN_ON(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)); 968 WARN_ON(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE));
912 969
913 return inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); 970 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
914} 971}
915 972
916static int nv_adma_port_start(struct ata_port *ap) 973static int nv_adma_port_start(struct ata_port *ap)
@@ -920,7 +977,7 @@ static int nv_adma_port_start(struct ata_port *ap)
920 int rc; 977 int rc;
921 void *mem; 978 void *mem;
922 dma_addr_t mem_dma; 979 dma_addr_t mem_dma;
923 void __iomem *mmio = nv_adma_ctl_block(ap); 980 void __iomem *mmio;
924 u16 tmp; 981 u16 tmp;
925 982
926 VPRINTK("ENTER\n"); 983 VPRINTK("ENTER\n");
@@ -929,19 +986,21 @@ static int nv_adma_port_start(struct ata_port *ap)
929 if (rc) 986 if (rc)
930 return rc; 987 return rc;
931 988
932 pp = kzalloc(sizeof(*pp), GFP_KERNEL); 989 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
933 if (!pp) { 990 if (!pp)
934 rc = -ENOMEM; 991 return -ENOMEM;
935 goto err_out; 992
936 } 993 mmio = ap->host->iomap[NV_MMIO_BAR] + NV_ADMA_PORT +
937 994 ap->port_no * NV_ADMA_PORT_SIZE;
938 mem = dma_alloc_coherent(dev, NV_ADMA_PORT_PRIV_DMA_SZ, 995 pp->ctl_block = mmio;
939 &mem_dma, GFP_KERNEL); 996 pp->gen_block = ap->host->iomap[NV_MMIO_BAR] + NV_ADMA_GEN;
940 997 pp->notifier_clear_block = pp->gen_block +
941 if (!mem) { 998 NV_ADMA_NOTIFIER_CLEAR + (4 * ap->port_no);
942 rc = -ENOMEM; 999
943 goto err_out_kfree; 1000 mem = dmam_alloc_coherent(dev, NV_ADMA_PORT_PRIV_DMA_SZ,
944 } 1001 &mem_dma, GFP_KERNEL);
1002 if (!mem)
1003 return -ENOMEM;
945 memset(mem, 0, NV_ADMA_PORT_PRIV_DMA_SZ); 1004 memset(mem, 0, NV_ADMA_PORT_PRIV_DMA_SZ);
946 1005
947 /* 1006 /*
@@ -975,9 +1034,9 @@ static int nv_adma_port_start(struct ata_port *ap)
975 /* clear CPB fetch count */ 1034 /* clear CPB fetch count */
976 writew(0, mmio + NV_ADMA_CPB_COUNT); 1035 writew(0, mmio + NV_ADMA_CPB_COUNT);
977 1036
978 /* clear GO for register mode */ 1037 /* clear GO for register mode, enable interrupt */
979 tmp = readw(mmio + NV_ADMA_CTL); 1038 tmp = readw(mmio + NV_ADMA_CTL);
980 writew(tmp & ~NV_ADMA_CTL_GO, mmio + NV_ADMA_CTL); 1039 writew( (tmp & ~NV_ADMA_CTL_GO) | NV_ADMA_CTL_AIEN, mmio + NV_ADMA_CTL);
981 1040
982 tmp = readw(mmio + NV_ADMA_CTL); 1041 tmp = readw(mmio + NV_ADMA_CTL);
983 writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL); 1042 writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
@@ -987,53 +1046,89 @@ static int nv_adma_port_start(struct ata_port *ap)
987 readl( mmio + NV_ADMA_CTL ); /* flush posted write */ 1046 readl( mmio + NV_ADMA_CTL ); /* flush posted write */
988 1047
989 return 0; 1048 return 0;
990
991err_out_kfree:
992 kfree(pp);
993err_out:
994 ata_port_stop(ap);
995 return rc;
996} 1049}
997 1050
998static void nv_adma_port_stop(struct ata_port *ap) 1051static void nv_adma_port_stop(struct ata_port *ap)
999{ 1052{
1000 struct device *dev = ap->host->dev;
1001 struct nv_adma_port_priv *pp = ap->private_data; 1053 struct nv_adma_port_priv *pp = ap->private_data;
1002 void __iomem *mmio = nv_adma_ctl_block(ap); 1054 void __iomem *mmio = pp->ctl_block;
1003 1055
1004 VPRINTK("ENTER\n"); 1056 VPRINTK("ENTER\n");
1057 writew(0, mmio + NV_ADMA_CTL);
1058}
1059
1060static int nv_adma_port_suspend(struct ata_port *ap, pm_message_t mesg)
1061{
1062 struct nv_adma_port_priv *pp = ap->private_data;
1063 void __iomem *mmio = pp->ctl_block;
1005 1064
1065 /* Go to register mode - clears GO */
1066 nv_adma_register_mode(ap);
1067
1068 /* clear CPB fetch count */
1069 writew(0, mmio + NV_ADMA_CPB_COUNT);
1070
1071 /* disable interrupt, shut down port */
1006 writew(0, mmio + NV_ADMA_CTL); 1072 writew(0, mmio + NV_ADMA_CTL);
1007 1073
1008 ap->private_data = NULL; 1074 return 0;
1009 dma_free_coherent(dev, NV_ADMA_PORT_PRIV_DMA_SZ, pp->cpb, pp->cpb_dma);
1010 kfree(pp);
1011 ata_port_stop(ap);
1012} 1075}
1013 1076
1077static int nv_adma_port_resume(struct ata_port *ap)
1078{
1079 struct nv_adma_port_priv *pp = ap->private_data;
1080 void __iomem *mmio = pp->ctl_block;
1081 u16 tmp;
1082
1083 /* set CPB block location */
1084 writel(pp->cpb_dma & 0xFFFFFFFF, mmio + NV_ADMA_CPB_BASE_LOW);
1085 writel((pp->cpb_dma >> 16 ) >> 16, mmio + NV_ADMA_CPB_BASE_HIGH);
1086
1087 /* clear any outstanding interrupt conditions */
1088 writew(0xffff, mmio + NV_ADMA_STAT);
1089
1090 /* initialize port variables */
1091 pp->flags |= NV_ADMA_PORT_REGISTER_MODE;
1092
1093 /* clear CPB fetch count */
1094 writew(0, mmio + NV_ADMA_CPB_COUNT);
1095
1096 /* clear GO for register mode, enable interrupt */
1097 tmp = readw(mmio + NV_ADMA_CTL);
1098 writew((tmp & ~NV_ADMA_CTL_GO) | NV_ADMA_CTL_AIEN, mmio + NV_ADMA_CTL);
1099
1100 tmp = readw(mmio + NV_ADMA_CTL);
1101 writew(tmp | NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
1102 readl( mmio + NV_ADMA_CTL ); /* flush posted write */
1103 udelay(1);
1104 writew(tmp & ~NV_ADMA_CTL_CHANNEL_RESET, mmio + NV_ADMA_CTL);
1105 readl( mmio + NV_ADMA_CTL ); /* flush posted write */
1106
1107 return 0;
1108}
1014 1109
1015static void nv_adma_setup_port(struct ata_probe_ent *probe_ent, unsigned int port) 1110static void nv_adma_setup_port(struct ata_probe_ent *probe_ent, unsigned int port)
1016{ 1111{
1017 void __iomem *mmio = probe_ent->mmio_base; 1112 void __iomem *mmio = probe_ent->iomap[NV_MMIO_BAR];
1018 struct ata_ioports *ioport = &probe_ent->port[port]; 1113 struct ata_ioports *ioport = &probe_ent->port[port];
1019 1114
1020 VPRINTK("ENTER\n"); 1115 VPRINTK("ENTER\n");
1021 1116
1022 mmio += NV_ADMA_PORT + port * NV_ADMA_PORT_SIZE; 1117 mmio += NV_ADMA_PORT + port * NV_ADMA_PORT_SIZE;
1023 1118
1024 ioport->cmd_addr = (unsigned long) mmio; 1119 ioport->cmd_addr = mmio;
1025 ioport->data_addr = (unsigned long) mmio + (ATA_REG_DATA * 4); 1120 ioport->data_addr = mmio + (ATA_REG_DATA * 4);
1026 ioport->error_addr = 1121 ioport->error_addr =
1027 ioport->feature_addr = (unsigned long) mmio + (ATA_REG_ERR * 4); 1122 ioport->feature_addr = mmio + (ATA_REG_ERR * 4);
1028 ioport->nsect_addr = (unsigned long) mmio + (ATA_REG_NSECT * 4); 1123 ioport->nsect_addr = mmio + (ATA_REG_NSECT * 4);
1029 ioport->lbal_addr = (unsigned long) mmio + (ATA_REG_LBAL * 4); 1124 ioport->lbal_addr = mmio + (ATA_REG_LBAL * 4);
1030 ioport->lbam_addr = (unsigned long) mmio + (ATA_REG_LBAM * 4); 1125 ioport->lbam_addr = mmio + (ATA_REG_LBAM * 4);
1031 ioport->lbah_addr = (unsigned long) mmio + (ATA_REG_LBAH * 4); 1126 ioport->lbah_addr = mmio + (ATA_REG_LBAH * 4);
1032 ioport->device_addr = (unsigned long) mmio + (ATA_REG_DEVICE * 4); 1127 ioport->device_addr = mmio + (ATA_REG_DEVICE * 4);
1033 ioport->status_addr = 1128 ioport->status_addr =
1034 ioport->command_addr = (unsigned long) mmio + (ATA_REG_STATUS * 4); 1129 ioport->command_addr = mmio + (ATA_REG_STATUS * 4);
1035 ioport->altstatus_addr = 1130 ioport->altstatus_addr =
1036 ioport->ctl_addr = (unsigned long) mmio + 0x20; 1131 ioport->ctl_addr = mmio + 0x20;
1037} 1132}
1038 1133
1039static int nv_adma_host_init(struct ata_probe_ent *probe_ent) 1134static int nv_adma_host_init(struct ata_probe_ent *probe_ent)
@@ -1056,15 +1151,6 @@ static int nv_adma_host_init(struct ata_probe_ent *probe_ent)
1056 for (i = 0; i < probe_ent->n_ports; i++) 1151 for (i = 0; i < probe_ent->n_ports; i++)
1057 nv_adma_setup_port(probe_ent, i); 1152 nv_adma_setup_port(probe_ent, i);
1058 1153
1059 for (i = 0; i < probe_ent->n_ports; i++) {
1060 void __iomem *mmio = __nv_adma_ctl_block(probe_ent->mmio_base, i);
1061 u16 tmp;
1062
1063 /* enable interrupt, clear reset if not already clear */
1064 tmp = readw(mmio + NV_ADMA_CTL);
1065 writew(tmp | NV_ADMA_CTL_AIEN, mmio + NV_ADMA_CTL);
1066 }
1067
1068 return 0; 1154 return 0;
1069} 1155}
1070 1156
@@ -1110,18 +1196,31 @@ static void nv_adma_fill_sg(struct ata_queued_cmd *qc, struct nv_adma_cpb *cpb)
1110 cpb->next_aprd = cpu_to_le64(((u64)(pp->aprd_dma + NV_ADMA_SGTBL_SZ * qc->tag))); 1196 cpb->next_aprd = cpu_to_le64(((u64)(pp->aprd_dma + NV_ADMA_SGTBL_SZ * qc->tag)));
1111} 1197}
1112 1198
1199static int nv_adma_use_reg_mode(struct ata_queued_cmd *qc)
1200{
1201 struct nv_adma_port_priv *pp = qc->ap->private_data;
1202
1203 /* ADMA engine can only be used for non-ATAPI DMA commands,
1204 or interrupt-driven no-data commands. */
1205 if((pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE) ||
1206 (qc->tf.flags & ATA_TFLAG_POLLING))
1207 return 1;
1208
1209 if((qc->flags & ATA_QCFLAG_DMAMAP) ||
1210 (qc->tf.protocol == ATA_PROT_NODATA))
1211 return 0;
1212
1213 return 1;
1214}
1215
1113static void nv_adma_qc_prep(struct ata_queued_cmd *qc) 1216static void nv_adma_qc_prep(struct ata_queued_cmd *qc)
1114{ 1217{
1115 struct nv_adma_port_priv *pp = qc->ap->private_data; 1218 struct nv_adma_port_priv *pp = qc->ap->private_data;
1116 struct nv_adma_cpb *cpb = &pp->cpb[qc->tag]; 1219 struct nv_adma_cpb *cpb = &pp->cpb[qc->tag];
1117 u8 ctl_flags = NV_CPB_CTL_CPB_VALID | 1220 u8 ctl_flags = NV_CPB_CTL_CPB_VALID |
1118 NV_CPB_CTL_APRD_VALID |
1119 NV_CPB_CTL_IEN; 1221 NV_CPB_CTL_IEN;
1120 1222
1121 VPRINTK("qc->flags = 0x%lx\n", qc->flags); 1223 if (nv_adma_use_reg_mode(qc)) {
1122
1123 if (!(qc->flags & ATA_QCFLAG_DMAMAP) ||
1124 (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)) {
1125 nv_adma_register_mode(qc->ap); 1224 nv_adma_register_mode(qc->ap);
1126 ata_qc_prep(qc); 1225 ata_qc_prep(qc);
1127 return; 1226 return;
@@ -1137,9 +1236,15 @@ static void nv_adma_qc_prep(struct ata_queued_cmd *qc)
1137 if (qc->tf.protocol == ATA_PROT_NCQ) 1236 if (qc->tf.protocol == ATA_PROT_NCQ)
1138 ctl_flags |= NV_CPB_CTL_QUEUE | NV_CPB_CTL_FPDMA; 1237 ctl_flags |= NV_CPB_CTL_QUEUE | NV_CPB_CTL_FPDMA;
1139 1238
1239 VPRINTK("qc->flags = 0x%lx\n", qc->flags);
1240
1140 nv_adma_tf_to_cpb(&qc->tf, cpb->tf); 1241 nv_adma_tf_to_cpb(&qc->tf, cpb->tf);
1141 1242
1142 nv_adma_fill_sg(qc, cpb); 1243 if(qc->flags & ATA_QCFLAG_DMAMAP) {
1244 nv_adma_fill_sg(qc, cpb);
1245 ctl_flags |= NV_CPB_CTL_APRD_VALID;
1246 } else
1247 memset(&cpb->aprd[0], 0, sizeof(struct nv_adma_prd) * 5);
1143 1248
1144 /* Be paranoid and don't let the device see NV_CPB_CTL_CPB_VALID until we are 1249 /* Be paranoid and don't let the device see NV_CPB_CTL_CPB_VALID until we are
1145 finished filling in all of the contents */ 1250 finished filling in all of the contents */
@@ -1150,14 +1255,13 @@ static void nv_adma_qc_prep(struct ata_queued_cmd *qc)
1150static unsigned int nv_adma_qc_issue(struct ata_queued_cmd *qc) 1255static unsigned int nv_adma_qc_issue(struct ata_queued_cmd *qc)
1151{ 1256{
1152 struct nv_adma_port_priv *pp = qc->ap->private_data; 1257 struct nv_adma_port_priv *pp = qc->ap->private_data;
1153 void __iomem *mmio = nv_adma_ctl_block(qc->ap); 1258 void __iomem *mmio = pp->ctl_block;
1154 1259
1155 VPRINTK("ENTER\n"); 1260 VPRINTK("ENTER\n");
1156 1261
1157 if (!(qc->flags & ATA_QCFLAG_DMAMAP) || 1262 if (nv_adma_use_reg_mode(qc)) {
1158 (pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)) {
1159 /* use ATA register mode */ 1263 /* use ATA register mode */
1160 VPRINTK("no dmamap or ATAPI, using ATA register mode: 0x%lx\n", qc->flags); 1264 VPRINTK("using ATA register mode: 0x%lx\n", qc->flags);
1161 nv_adma_register_mode(qc->ap); 1265 nv_adma_register_mode(qc->ap);
1162 return ata_qc_issue_prot(qc); 1266 return ata_qc_issue_prot(qc);
1163 } else 1267 } else
@@ -1229,7 +1333,7 @@ static irqreturn_t nv_nf2_interrupt(int irq, void *dev_instance)
1229 irqreturn_t ret; 1333 irqreturn_t ret;
1230 1334
1231 spin_lock(&host->lock); 1335 spin_lock(&host->lock);
1232 irq_stat = inb(host->ports[0]->ioaddr.scr_addr + NV_INT_STATUS); 1336 irq_stat = ioread8(host->ports[0]->ioaddr.scr_addr + NV_INT_STATUS);
1233 ret = nv_do_interrupt(host, irq_stat); 1337 ret = nv_do_interrupt(host, irq_stat);
1234 spin_unlock(&host->lock); 1338 spin_unlock(&host->lock);
1235 1339
@@ -1243,7 +1347,7 @@ static irqreturn_t nv_ck804_interrupt(int irq, void *dev_instance)
1243 irqreturn_t ret; 1347 irqreturn_t ret;
1244 1348
1245 spin_lock(&host->lock); 1349 spin_lock(&host->lock);
1246 irq_stat = readb(host->mmio_base + NV_INT_STATUS_CK804); 1350 irq_stat = readb(host->iomap[NV_MMIO_BAR] + NV_INT_STATUS_CK804);
1247 ret = nv_do_interrupt(host, irq_stat); 1351 ret = nv_do_interrupt(host, irq_stat);
1248 spin_unlock(&host->lock); 1352 spin_unlock(&host->lock);
1249 1353
@@ -1255,7 +1359,7 @@ static u32 nv_scr_read (struct ata_port *ap, unsigned int sc_reg)
1255 if (sc_reg > SCR_CONTROL) 1359 if (sc_reg > SCR_CONTROL)
1256 return 0xffffffffU; 1360 return 0xffffffffU;
1257 1361
1258 return ioread32((void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4)); 1362 return ioread32(ap->ioaddr.scr_addr + (sc_reg * 4));
1259} 1363}
1260 1364
1261static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val) 1365static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
@@ -1263,36 +1367,36 @@ static void nv_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val)
1263 if (sc_reg > SCR_CONTROL) 1367 if (sc_reg > SCR_CONTROL)
1264 return; 1368 return;
1265 1369
1266 iowrite32(val, (void __iomem *)ap->ioaddr.scr_addr + (sc_reg * 4)); 1370 iowrite32(val, ap->ioaddr.scr_addr + (sc_reg * 4));
1267} 1371}
1268 1372
1269static void nv_nf2_freeze(struct ata_port *ap) 1373static void nv_nf2_freeze(struct ata_port *ap)
1270{ 1374{
1271 unsigned long scr_addr = ap->host->ports[0]->ioaddr.scr_addr; 1375 void __iomem *scr_addr = ap->host->ports[0]->ioaddr.scr_addr;
1272 int shift = ap->port_no * NV_INT_PORT_SHIFT; 1376 int shift = ap->port_no * NV_INT_PORT_SHIFT;
1273 u8 mask; 1377 u8 mask;
1274 1378
1275 mask = inb(scr_addr + NV_INT_ENABLE); 1379 mask = ioread8(scr_addr + NV_INT_ENABLE);
1276 mask &= ~(NV_INT_ALL << shift); 1380 mask &= ~(NV_INT_ALL << shift);
1277 outb(mask, scr_addr + NV_INT_ENABLE); 1381 iowrite8(mask, scr_addr + NV_INT_ENABLE);
1278} 1382}
1279 1383
1280static void nv_nf2_thaw(struct ata_port *ap) 1384static void nv_nf2_thaw(struct ata_port *ap)
1281{ 1385{
1282 unsigned long scr_addr = ap->host->ports[0]->ioaddr.scr_addr; 1386 void __iomem *scr_addr = ap->host->ports[0]->ioaddr.scr_addr;
1283 int shift = ap->port_no * NV_INT_PORT_SHIFT; 1387 int shift = ap->port_no * NV_INT_PORT_SHIFT;
1284 u8 mask; 1388 u8 mask;
1285 1389
1286 outb(NV_INT_ALL << shift, scr_addr + NV_INT_STATUS); 1390 iowrite8(NV_INT_ALL << shift, scr_addr + NV_INT_STATUS);
1287 1391
1288 mask = inb(scr_addr + NV_INT_ENABLE); 1392 mask = ioread8(scr_addr + NV_INT_ENABLE);
1289 mask |= (NV_INT_MASK << shift); 1393 mask |= (NV_INT_MASK << shift);
1290 outb(mask, scr_addr + NV_INT_ENABLE); 1394 iowrite8(mask, scr_addr + NV_INT_ENABLE);
1291} 1395}
1292 1396
1293static void nv_ck804_freeze(struct ata_port *ap) 1397static void nv_ck804_freeze(struct ata_port *ap)
1294{ 1398{
1295 void __iomem *mmio_base = ap->host->mmio_base; 1399 void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
1296 int shift = ap->port_no * NV_INT_PORT_SHIFT; 1400 int shift = ap->port_no * NV_INT_PORT_SHIFT;
1297 u8 mask; 1401 u8 mask;
1298 1402
@@ -1303,7 +1407,7 @@ static void nv_ck804_freeze(struct ata_port *ap)
1303 1407
1304static void nv_ck804_thaw(struct ata_port *ap) 1408static void nv_ck804_thaw(struct ata_port *ap)
1305{ 1409{
1306 void __iomem *mmio_base = ap->host->mmio_base; 1410 void __iomem *mmio_base = ap->host->iomap[NV_MMIO_BAR];
1307 int shift = ap->port_no * NV_INT_PORT_SHIFT; 1411 int shift = ap->port_no * NV_INT_PORT_SHIFT;
1308 u8 mask; 1412 u8 mask;
1309 1413
@@ -1335,32 +1439,13 @@ static void nv_adma_error_handler(struct ata_port *ap)
1335{ 1439{
1336 struct nv_adma_port_priv *pp = ap->private_data; 1440 struct nv_adma_port_priv *pp = ap->private_data;
1337 if(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)) { 1441 if(!(pp->flags & NV_ADMA_PORT_REGISTER_MODE)) {
1338 void __iomem *mmio = nv_adma_ctl_block(ap); 1442 void __iomem *mmio = pp->ctl_block;
1339 int i; 1443 int i;
1340 u16 tmp; 1444 u16 tmp;
1341 1445
1342 u32 notifier = readl(mmio + NV_ADMA_NOTIFIER);
1343 u32 notifier_error = readl(mmio + NV_ADMA_NOTIFIER_ERROR);
1344 u32 gen_ctl = readl(nv_adma_gen_block(ap) + NV_ADMA_GEN_CTL);
1345 u32 status = readw(mmio + NV_ADMA_STAT);
1346
1347 ata_port_printk(ap, KERN_ERR, "EH in ADMA mode, notifier 0x%X "
1348 "notifier_error 0x%X gen_ctl 0x%X status 0x%X\n",
1349 notifier, notifier_error, gen_ctl, status);
1350
1351 for( i=0;i<NV_ADMA_MAX_CPBS;i++) {
1352 struct nv_adma_cpb *cpb = &pp->cpb[i];
1353 if( cpb->ctl_flags || cpb->resp_flags )
1354 ata_port_printk(ap, KERN_ERR,
1355 "CPB %d: ctl_flags 0x%x, resp_flags 0x%x\n",
1356 i, cpb->ctl_flags, cpb->resp_flags);
1357 }
1358
1359 /* Push us back into port register mode for error handling. */ 1446 /* Push us back into port register mode for error handling. */
1360 nv_adma_register_mode(ap); 1447 nv_adma_register_mode(ap);
1361 1448
1362 ata_port_printk(ap, KERN_ERR, "Resetting port\n");
1363
1364 /* Mark all of the CPBs as invalid to prevent them from being executed */ 1449 /* Mark all of the CPBs as invalid to prevent them from being executed */
1365 for( i=0;i<NV_ADMA_MAX_CPBS;i++) 1450 for( i=0;i<NV_ADMA_MAX_CPBS;i++)
1366 pp->cpb[i].ctl_flags &= ~NV_CPB_CTL_CPB_VALID; 1451 pp->cpb[i].ctl_flags &= ~NV_CPB_CTL_CPB_VALID;
@@ -1386,10 +1471,10 @@ static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1386 static int printed_version = 0; 1471 static int printed_version = 0;
1387 struct ata_port_info *ppi[2]; 1472 struct ata_port_info *ppi[2];
1388 struct ata_probe_ent *probe_ent; 1473 struct ata_probe_ent *probe_ent;
1389 int pci_dev_busy = 0; 1474 struct nv_host_priv *hpriv;
1390 int rc; 1475 int rc;
1391 u32 bar; 1476 u32 bar;
1392 unsigned long base; 1477 void __iomem *base;
1393 unsigned long type = ent->driver_data; 1478 unsigned long type = ent->driver_data;
1394 int mask_set = 0; 1479 int mask_set = 0;
1395 1480
@@ -1400,17 +1485,17 @@ static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1400 if (pci_resource_start(pdev, bar) == 0) 1485 if (pci_resource_start(pdev, bar) == 0)
1401 return -ENODEV; 1486 return -ENODEV;
1402 1487
1403 if ( !printed_version++) 1488 if (!printed_version++)
1404 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n"); 1489 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
1405 1490
1406 rc = pci_enable_device(pdev); 1491 rc = pcim_enable_device(pdev);
1407 if (rc) 1492 if (rc)
1408 goto err_out; 1493 return rc;
1409 1494
1410 rc = pci_request_regions(pdev, DRV_NAME); 1495 rc = pci_request_regions(pdev, DRV_NAME);
1411 if (rc) { 1496 if (rc) {
1412 pci_dev_busy = 1; 1497 pcim_pin_device(pdev);
1413 goto err_out_disable; 1498 return rc;
1414 } 1499 }
1415 1500
1416 if(type >= CK804 && adma_enabled) { 1501 if(type >= CK804 && adma_enabled) {
@@ -1424,27 +1509,31 @@ static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1424 if(!mask_set) { 1509 if(!mask_set) {
1425 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK); 1510 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
1426 if (rc) 1511 if (rc)
1427 goto err_out_regions; 1512 return rc;
1428 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK); 1513 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
1429 if (rc) 1514 if (rc)
1430 goto err_out_regions; 1515 return rc;
1431 } 1516 }
1432 1517
1433 rc = -ENOMEM; 1518 rc = -ENOMEM;
1434 1519
1520 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
1521 if (!hpriv)
1522 return -ENOMEM;
1523
1435 ppi[0] = ppi[1] = &nv_port_info[type]; 1524 ppi[0] = ppi[1] = &nv_port_info[type];
1436 probe_ent = ata_pci_init_native_mode(pdev, ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY); 1525 probe_ent = ata_pci_init_native_mode(pdev, ppi, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
1437 if (!probe_ent) 1526 if (!probe_ent)
1438 goto err_out_regions; 1527 return -ENOMEM;
1439 1528
1440 probe_ent->mmio_base = pci_iomap(pdev, 5, 0); 1529 if (!pcim_iomap(pdev, NV_MMIO_BAR, 0))
1441 if (!probe_ent->mmio_base) { 1530 return -EIO;
1442 rc = -EIO; 1531 probe_ent->iomap = pcim_iomap_table(pdev);
1443 goto err_out_free_ent;
1444 }
1445 1532
1446 base = (unsigned long)probe_ent->mmio_base; 1533 probe_ent->private_data = hpriv;
1534 hpriv->type = type;
1447 1535
1536 base = probe_ent->iomap[NV_MMIO_BAR];
1448 probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET; 1537 probe_ent->port[0].scr_addr = base + NV_PORT0_SCR_REG_OFFSET;
1449 probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET; 1538 probe_ent->port[1].scr_addr = base + NV_PORT1_SCR_REG_OFFSET;
1450 1539
@@ -1462,28 +1551,72 @@ static int nv_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1462 if (type == ADMA) { 1551 if (type == ADMA) {
1463 rc = nv_adma_host_init(probe_ent); 1552 rc = nv_adma_host_init(probe_ent);
1464 if (rc) 1553 if (rc)
1465 goto err_out_iounmap; 1554 return rc;
1466 } 1555 }
1467 1556
1468 rc = ata_device_add(probe_ent); 1557 rc = ata_device_add(probe_ent);
1469 if (rc != NV_PORTS) 1558 if (rc != NV_PORTS)
1470 goto err_out_iounmap; 1559 return -ENODEV;
1471
1472 kfree(probe_ent);
1473 1560
1561 devm_kfree(&pdev->dev, probe_ent);
1474 return 0; 1562 return 0;
1563}
1475 1564
1476err_out_iounmap: 1565static void nv_remove_one (struct pci_dev *pdev)
1477 pci_iounmap(pdev, probe_ent->mmio_base); 1566{
1478err_out_free_ent: 1567 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1479 kfree(probe_ent); 1568 struct nv_host_priv *hpriv = host->private_data;
1480err_out_regions: 1569
1481 pci_release_regions(pdev); 1570 ata_pci_remove_one(pdev);
1482err_out_disable: 1571 kfree(hpriv);
1483 if (!pci_dev_busy) 1572}
1484 pci_disable_device(pdev); 1573
1485err_out: 1574static int nv_pci_device_resume(struct pci_dev *pdev)
1486 return rc; 1575{
1576 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1577 struct nv_host_priv *hpriv = host->private_data;
1578 int rc;
1579
1580 rc = ata_pci_device_do_resume(pdev);
1581 if(rc)
1582 return rc;
1583
1584 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
1585 if(hpriv->type >= CK804) {
1586 u8 regval;
1587
1588 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
1589 regval |= NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
1590 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
1591 }
1592 if(hpriv->type == ADMA) {
1593 u32 tmp32;
1594 struct nv_adma_port_priv *pp;
1595 /* enable/disable ADMA on the ports appropriately */
1596 pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32);
1597
1598 pp = host->ports[0]->private_data;
1599 if(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)
1600 tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT0_EN |
1601 NV_MCP_SATA_CFG_20_PORT0_PWB_EN);
1602 else
1603 tmp32 |= (NV_MCP_SATA_CFG_20_PORT0_EN |
1604 NV_MCP_SATA_CFG_20_PORT0_PWB_EN);
1605 pp = host->ports[1]->private_data;
1606 if(pp->flags & NV_ADMA_ATAPI_SETUP_COMPLETE)
1607 tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT1_EN |
1608 NV_MCP_SATA_CFG_20_PORT1_PWB_EN);
1609 else
1610 tmp32 |= (NV_MCP_SATA_CFG_20_PORT1_EN |
1611 NV_MCP_SATA_CFG_20_PORT1_PWB_EN);
1612
1613 pci_write_config_dword(pdev, NV_MCP_SATA_CFG_20, tmp32);
1614 }
1615 }
1616
1617 ata_host_resume(host);
1618
1619 return 0;
1487} 1620}
1488 1621
1489static void nv_ck804_host_stop(struct ata_host *host) 1622static void nv_ck804_host_stop(struct ata_host *host)
@@ -1495,25 +1628,13 @@ static void nv_ck804_host_stop(struct ata_host *host)
1495 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval); 1628 pci_read_config_byte(pdev, NV_MCP_SATA_CFG_20, &regval);
1496 regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN; 1629 regval &= ~NV_MCP_SATA_CFG_20_SATA_SPACE_EN;
1497 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval); 1630 pci_write_config_byte(pdev, NV_MCP_SATA_CFG_20, regval);
1498
1499 ata_pci_host_stop(host);
1500} 1631}
1501 1632
1502static void nv_adma_host_stop(struct ata_host *host) 1633static void nv_adma_host_stop(struct ata_host *host)
1503{ 1634{
1504 struct pci_dev *pdev = to_pci_dev(host->dev); 1635 struct pci_dev *pdev = to_pci_dev(host->dev);
1505 int i;
1506 u32 tmp32; 1636 u32 tmp32;
1507 1637
1508 for (i = 0; i < host->n_ports; i++) {
1509 void __iomem *mmio = __nv_adma_ctl_block(host->mmio_base, i);
1510 u16 tmp;
1511
1512 /* disable interrupt */
1513 tmp = readw(mmio + NV_ADMA_CTL);
1514 writew(tmp & ~NV_ADMA_CTL_AIEN, mmio + NV_ADMA_CTL);
1515 }
1516
1517 /* disable ADMA on the ports */ 1638 /* disable ADMA on the ports */
1518 pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32); 1639 pci_read_config_dword(pdev, NV_MCP_SATA_CFG_20, &tmp32);
1519 tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT0_EN | 1640 tmp32 &= ~(NV_MCP_SATA_CFG_20_PORT0_EN |