aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/ahci.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/ahci.c')
-rw-r--r--drivers/scsi/ahci.c669
1 files changed, 437 insertions, 232 deletions
diff --git a/drivers/scsi/ahci.c b/drivers/scsi/ahci.c
index 77e7202a0eba..68fd7667a082 100644
--- a/drivers/scsi/ahci.c
+++ b/drivers/scsi/ahci.c
@@ -92,7 +92,9 @@ enum {
92 HOST_AHCI_EN = (1 << 31), /* AHCI enabled */ 92 HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
93 93
94 /* HOST_CAP bits */ 94 /* HOST_CAP bits */
95 HOST_CAP_SSC = (1 << 14), /* Slumber capable */
95 HOST_CAP_CLO = (1 << 24), /* Command List Override support */ 96 HOST_CAP_CLO = (1 << 24), /* Command List Override support */
97 HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
96 HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */ 98 HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
97 HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */ 99 HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
98 100
@@ -155,6 +157,7 @@ enum {
155 PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */ 157 PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
156 PORT_CMD_START = (1 << 0), /* Enable port DMA engine */ 158 PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
157 159
160 PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
158 PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */ 161 PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
159 PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */ 162 PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
160 PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */ 163 PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
@@ -212,6 +215,10 @@ static void ahci_freeze(struct ata_port *ap);
212static void ahci_thaw(struct ata_port *ap); 215static void ahci_thaw(struct ata_port *ap);
213static void ahci_error_handler(struct ata_port *ap); 216static void ahci_error_handler(struct ata_port *ap);
214static void ahci_post_internal_cmd(struct ata_queued_cmd *qc); 217static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
218static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
219static int ahci_port_resume(struct ata_port *ap);
220static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
221static int ahci_pci_device_resume(struct pci_dev *pdev);
215static void ahci_remove_one (struct pci_dev *pdev); 222static void ahci_remove_one (struct pci_dev *pdev);
216 223
217static struct scsi_host_template ahci_sht = { 224static struct scsi_host_template ahci_sht = {
@@ -231,6 +238,8 @@ static struct scsi_host_template ahci_sht = {
231 .slave_configure = ata_scsi_slave_config, 238 .slave_configure = ata_scsi_slave_config,
232 .slave_destroy = ata_scsi_slave_destroy, 239 .slave_destroy = ata_scsi_slave_destroy,
233 .bios_param = ata_std_bios_param, 240 .bios_param = ata_std_bios_param,
241 .suspend = ata_scsi_device_suspend,
242 .resume = ata_scsi_device_resume,
234}; 243};
235 244
236static const struct ata_port_operations ahci_ops = { 245static const struct ata_port_operations ahci_ops = {
@@ -257,6 +266,9 @@ static const struct ata_port_operations ahci_ops = {
257 .error_handler = ahci_error_handler, 266 .error_handler = ahci_error_handler,
258 .post_internal_cmd = ahci_post_internal_cmd, 267 .post_internal_cmd = ahci_post_internal_cmd,
259 268
269 .port_suspend = ahci_port_suspend,
270 .port_resume = ahci_port_resume,
271
260 .port_start = ahci_port_start, 272 .port_start = ahci_port_start,
261 .port_stop = ahci_port_stop, 273 .port_stop = ahci_port_stop,
262}; 274};
@@ -350,6 +362,14 @@ static const struct pci_device_id ahci_pci_tbl[] = {
350 { PCI_VENDOR_ID_NVIDIA, 0x044f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 362 { PCI_VENDOR_ID_NVIDIA, 0x044f, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
351 board_ahci }, /* MCP65 */ 363 board_ahci }, /* MCP65 */
352 364
365 /* SiS */
366 { PCI_VENDOR_ID_SI, 0x1184, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
367 board_ahci }, /* SiS 966 */
368 { PCI_VENDOR_ID_SI, 0x1185, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
369 board_ahci }, /* SiS 966 */
370 { PCI_VENDOR_ID_SI, 0x0186, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
371 board_ahci }, /* SiS 968 */
372
353 { } /* terminate list */ 373 { } /* terminate list */
354}; 374};
355 375
@@ -358,6 +378,8 @@ static struct pci_driver ahci_pci_driver = {
358 .name = DRV_NAME, 378 .name = DRV_NAME,
359 .id_table = ahci_pci_tbl, 379 .id_table = ahci_pci_tbl,
360 .probe = ahci_init_one, 380 .probe = ahci_init_one,
381 .suspend = ahci_pci_device_suspend,
382 .resume = ahci_pci_device_resume,
361 .remove = ahci_remove_one, 383 .remove = ahci_remove_one,
362}; 384};
363 385
@@ -372,177 +394,288 @@ static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int por
372 return (void __iomem *) ahci_port_base_ul((unsigned long)base, port); 394 return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
373} 395}
374 396
375static int ahci_port_start(struct ata_port *ap) 397static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
376{ 398{
377 struct device *dev = ap->host_set->dev; 399 unsigned int sc_reg;
378 struct ahci_host_priv *hpriv = ap->host_set->private_data;
379 struct ahci_port_priv *pp;
380 void __iomem *mmio = ap->host_set->mmio_base;
381 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
382 void *mem;
383 dma_addr_t mem_dma;
384 int rc;
385
386 pp = kmalloc(sizeof(*pp), GFP_KERNEL);
387 if (!pp)
388 return -ENOMEM;
389 memset(pp, 0, sizeof(*pp));
390 400
391 rc = ata_pad_alloc(ap, dev); 401 switch (sc_reg_in) {
392 if (rc) { 402 case SCR_STATUS: sc_reg = 0; break;
393 kfree(pp); 403 case SCR_CONTROL: sc_reg = 1; break;
394 return rc; 404 case SCR_ERROR: sc_reg = 2; break;
405 case SCR_ACTIVE: sc_reg = 3; break;
406 default:
407 return 0xffffffffU;
395 } 408 }
396 409
397 mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL); 410 return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
398 if (!mem) { 411}
399 ata_pad_free(ap, dev);
400 kfree(pp);
401 return -ENOMEM;
402 }
403 memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
404 412
405 /*
406 * First item in chunk of DMA memory: 32-slot command table,
407 * 32 bytes each in size
408 */
409 pp->cmd_slot = mem;
410 pp->cmd_slot_dma = mem_dma;
411 413
412 mem += AHCI_CMD_SLOT_SZ; 414static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
413 mem_dma += AHCI_CMD_SLOT_SZ; 415 u32 val)
416{
417 unsigned int sc_reg;
414 418
415 /* 419 switch (sc_reg_in) {
416 * Second item: Received-FIS area 420 case SCR_STATUS: sc_reg = 0; break;
417 */ 421 case SCR_CONTROL: sc_reg = 1; break;
418 pp->rx_fis = mem; 422 case SCR_ERROR: sc_reg = 2; break;
419 pp->rx_fis_dma = mem_dma; 423 case SCR_ACTIVE: sc_reg = 3; break;
424 default:
425 return;
426 }
420 427
421 mem += AHCI_RX_FIS_SZ; 428 writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
422 mem_dma += AHCI_RX_FIS_SZ; 429}
423 430
424 /* 431static void ahci_start_engine(void __iomem *port_mmio)
425 * Third item: data area for storing a single command 432{
426 * and its scatter-gather table 433 u32 tmp;
427 */
428 pp->cmd_tbl = mem;
429 pp->cmd_tbl_dma = mem_dma;
430 434
431 ap->private_data = pp; 435 /* start DMA */
436 tmp = readl(port_mmio + PORT_CMD);
437 tmp |= PORT_CMD_START;
438 writel(tmp, port_mmio + PORT_CMD);
439 readl(port_mmio + PORT_CMD); /* flush */
440}
432 441
433 if (hpriv->cap & HOST_CAP_64) 442static int ahci_stop_engine(void __iomem *port_mmio)
434 writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI); 443{
435 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR); 444 u32 tmp;
436 readl(port_mmio + PORT_LST_ADDR); /* flush */
437 445
438 if (hpriv->cap & HOST_CAP_64) 446 tmp = readl(port_mmio + PORT_CMD);
439 writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
440 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
441 readl(port_mmio + PORT_FIS_ADDR); /* flush */
442 447
443 writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX | 448 /* check if the HBA is idle */
444 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP | 449 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
445 PORT_CMD_START, port_mmio + PORT_CMD); 450 return 0;
446 readl(port_mmio + PORT_CMD); /* flush */ 451
452 /* setting HBA to idle */
453 tmp &= ~PORT_CMD_START;
454 writel(tmp, port_mmio + PORT_CMD);
455
456 /* wait for engine to stop. This could be as long as 500 msec */
457 tmp = ata_wait_register(port_mmio + PORT_CMD,
458 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
459 if (tmp & PORT_CMD_LIST_ON)
460 return -EIO;
447 461
448 return 0; 462 return 0;
449} 463}
450 464
465static void ahci_start_fis_rx(void __iomem *port_mmio, u32 cap,
466 dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
467{
468 u32 tmp;
451 469
452static void ahci_port_stop(struct ata_port *ap) 470 /* set FIS registers */
471 if (cap & HOST_CAP_64)
472 writel((cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
473 writel(cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
474
475 if (cap & HOST_CAP_64)
476 writel((rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
477 writel(rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
478
479 /* enable FIS reception */
480 tmp = readl(port_mmio + PORT_CMD);
481 tmp |= PORT_CMD_FIS_RX;
482 writel(tmp, port_mmio + PORT_CMD);
483
484 /* flush */
485 readl(port_mmio + PORT_CMD);
486}
487
488static int ahci_stop_fis_rx(void __iomem *port_mmio)
453{ 489{
454 struct device *dev = ap->host_set->dev;
455 struct ahci_port_priv *pp = ap->private_data;
456 void __iomem *mmio = ap->host_set->mmio_base;
457 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
458 u32 tmp; 490 u32 tmp;
459 491
492 /* disable FIS reception */
460 tmp = readl(port_mmio + PORT_CMD); 493 tmp = readl(port_mmio + PORT_CMD);
461 tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX); 494 tmp &= ~PORT_CMD_FIS_RX;
462 writel(tmp, port_mmio + PORT_CMD); 495 writel(tmp, port_mmio + PORT_CMD);
463 readl(port_mmio + PORT_CMD); /* flush */
464 496
465 /* spec says 500 msecs for each PORT_CMD_{START,FIS_RX} bit, so 497 /* wait for completion, spec says 500ms, give it 1000 */
466 * this is slightly incorrect. 498 tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
467 */ 499 PORT_CMD_FIS_ON, 10, 1000);
468 msleep(500); 500 if (tmp & PORT_CMD_FIS_ON)
501 return -EBUSY;
469 502
470 ap->private_data = NULL; 503 return 0;
471 dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
472 pp->cmd_slot, pp->cmd_slot_dma);
473 ata_pad_free(ap, dev);
474 kfree(pp);
475} 504}
476 505
477static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in) 506static void ahci_power_up(void __iomem *port_mmio, u32 cap)
478{ 507{
479 unsigned int sc_reg; 508 u32 cmd;
480 509
481 switch (sc_reg_in) { 510 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
482 case SCR_STATUS: sc_reg = 0; break; 511
483 case SCR_CONTROL: sc_reg = 1; break; 512 /* spin up device */
484 case SCR_ERROR: sc_reg = 2; break; 513 if (cap & HOST_CAP_SSS) {
485 case SCR_ACTIVE: sc_reg = 3; break; 514 cmd |= PORT_CMD_SPIN_UP;
486 default: 515 writel(cmd, port_mmio + PORT_CMD);
487 return 0xffffffffU;
488 } 516 }
489 517
490 return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4)); 518 /* wake up link */
519 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
491} 520}
492 521
522static void ahci_power_down(void __iomem *port_mmio, u32 cap)
523{
524 u32 cmd, scontrol;
493 525
494static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in, 526 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
495 u32 val) 527
528 if (cap & HOST_CAP_SSC) {
529 /* enable transitions to slumber mode */
530 scontrol = readl(port_mmio + PORT_SCR_CTL);
531 if ((scontrol & 0x0f00) > 0x100) {
532 scontrol &= ~0xf00;
533 writel(scontrol, port_mmio + PORT_SCR_CTL);
534 }
535
536 /* put device into slumber mode */
537 writel(cmd | PORT_CMD_ICC_SLUMBER, port_mmio + PORT_CMD);
538
539 /* wait for the transition to complete */
540 ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_ICC_SLUMBER,
541 PORT_CMD_ICC_SLUMBER, 1, 50);
542 }
543
544 /* put device into listen mode */
545 if (cap & HOST_CAP_SSS) {
546 /* first set PxSCTL.DET to 0 */
547 scontrol = readl(port_mmio + PORT_SCR_CTL);
548 scontrol &= ~0xf;
549 writel(scontrol, port_mmio + PORT_SCR_CTL);
550
551 /* then set PxCMD.SUD to 0 */
552 cmd &= ~PORT_CMD_SPIN_UP;
553 writel(cmd, port_mmio + PORT_CMD);
554 }
555}
556
557static void ahci_init_port(void __iomem *port_mmio, u32 cap,
558 dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
496{ 559{
497 unsigned int sc_reg; 560 /* power up */
561 ahci_power_up(port_mmio, cap);
498 562
499 switch (sc_reg_in) { 563 /* enable FIS reception */
500 case SCR_STATUS: sc_reg = 0; break; 564 ahci_start_fis_rx(port_mmio, cap, cmd_slot_dma, rx_fis_dma);
501 case SCR_CONTROL: sc_reg = 1; break; 565
502 case SCR_ERROR: sc_reg = 2; break; 566 /* enable DMA */
503 case SCR_ACTIVE: sc_reg = 3; break; 567 ahci_start_engine(port_mmio);
504 default: 568}
505 return; 569
570static int ahci_deinit_port(void __iomem *port_mmio, u32 cap, const char **emsg)
571{
572 int rc;
573
574 /* disable DMA */
575 rc = ahci_stop_engine(port_mmio);
576 if (rc) {
577 *emsg = "failed to stop engine";
578 return rc;
506 } 579 }
507 580
508 writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4)); 581 /* disable FIS reception */
582 rc = ahci_stop_fis_rx(port_mmio);
583 if (rc) {
584 *emsg = "failed stop FIS RX";
585 return rc;
586 }
587
588 /* put device into slumber mode */
589 ahci_power_down(port_mmio, cap);
590
591 return 0;
509} 592}
510 593
511static int ahci_stop_engine(struct ata_port *ap) 594static int ahci_reset_controller(void __iomem *mmio, struct pci_dev *pdev)
512{ 595{
513 void __iomem *mmio = ap->host_set->mmio_base; 596 u32 cap_save, tmp;
514 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
515 int work;
516 u32 tmp;
517 597
518 tmp = readl(port_mmio + PORT_CMD); 598 cap_save = readl(mmio + HOST_CAP);
519 tmp &= ~PORT_CMD_START; 599 cap_save &= ( (1<<28) | (1<<17) );
520 writel(tmp, port_mmio + PORT_CMD); 600 cap_save |= (1 << 27);
521 601
522 /* wait for engine to stop. TODO: this could be 602 /* global controller reset */
523 * as long as 500 msec 603 tmp = readl(mmio + HOST_CTL);
604 if ((tmp & HOST_RESET) == 0) {
605 writel(tmp | HOST_RESET, mmio + HOST_CTL);
606 readl(mmio + HOST_CTL); /* flush */
607 }
608
609 /* reset must complete within 1 second, or
610 * the hardware should be considered fried.
524 */ 611 */
525 work = 1000; 612 ssleep(1);
526 while (work-- > 0) { 613
527 tmp = readl(port_mmio + PORT_CMD); 614 tmp = readl(mmio + HOST_CTL);
528 if ((tmp & PORT_CMD_LIST_ON) == 0) 615 if (tmp & HOST_RESET) {
529 return 0; 616 dev_printk(KERN_ERR, &pdev->dev,
530 udelay(10); 617 "controller reset failed (0x%x)\n", tmp);
618 return -EIO;
619 }
620
621 writel(HOST_AHCI_EN, mmio + HOST_CTL);
622 (void) readl(mmio + HOST_CTL); /* flush */
623 writel(cap_save, mmio + HOST_CAP);
624 writel(0xf, mmio + HOST_PORTS_IMPL);
625 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
626
627 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
628 u16 tmp16;
629
630 /* configure PCS */
631 pci_read_config_word(pdev, 0x92, &tmp16);
632 tmp16 |= 0xf;
633 pci_write_config_word(pdev, 0x92, tmp16);
531 } 634 }
532 635
533 return -EIO; 636 return 0;
534} 637}
535 638
536static void ahci_start_engine(struct ata_port *ap) 639static void ahci_init_controller(void __iomem *mmio, struct pci_dev *pdev,
640 int n_ports, u32 cap)
537{ 641{
538 void __iomem *mmio = ap->host_set->mmio_base; 642 int i, rc;
539 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
540 u32 tmp; 643 u32 tmp;
541 644
542 tmp = readl(port_mmio + PORT_CMD); 645 for (i = 0; i < n_ports; i++) {
543 tmp |= PORT_CMD_START; 646 void __iomem *port_mmio = ahci_port_base(mmio, i);
544 writel(tmp, port_mmio + PORT_CMD); 647 const char *emsg = NULL;
545 readl(port_mmio + PORT_CMD); /* flush */ 648
649#if 0 /* BIOSen initialize this incorrectly */
650 if (!(hpriv->port_map & (1 << i)))
651 continue;
652#endif
653
654 /* make sure port is not active */
655 rc = ahci_deinit_port(port_mmio, cap, &emsg);
656 if (rc)
657 dev_printk(KERN_WARNING, &pdev->dev,
658 "%s (%d)\n", emsg, rc);
659
660 /* clear SError */
661 tmp = readl(port_mmio + PORT_SCR_ERR);
662 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
663 writel(tmp, port_mmio + PORT_SCR_ERR);
664
665 /* clear port IRQ */
666 tmp = readl(port_mmio + PORT_IRQ_STAT);
667 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
668 if (tmp)
669 writel(tmp, port_mmio + PORT_IRQ_STAT);
670
671 writel(1 << i, mmio + HOST_IRQ_STAT);
672 }
673
674 tmp = readl(mmio + HOST_CTL);
675 VPRINTK("HOST_CTL 0x%x\n", tmp);
676 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
677 tmp = readl(mmio + HOST_CTL);
678 VPRINTK("HOST_CTL 0x%x\n", tmp);
546} 679}
547 680
548static unsigned int ahci_dev_classify(struct ata_port *ap) 681static unsigned int ahci_dev_classify(struct ata_port *ap)
@@ -626,7 +759,7 @@ static int ahci_softreset(struct ata_port *ap, unsigned int *class)
626 } 759 }
627 760
628 /* prepare for SRST (AHCI-1.1 10.4.1) */ 761 /* prepare for SRST (AHCI-1.1 10.4.1) */
629 rc = ahci_stop_engine(ap); 762 rc = ahci_stop_engine(port_mmio);
630 if (rc) { 763 if (rc) {
631 reason = "failed to stop engine"; 764 reason = "failed to stop engine";
632 goto fail_restart; 765 goto fail_restart;
@@ -647,7 +780,7 @@ static int ahci_softreset(struct ata_port *ap, unsigned int *class)
647 } 780 }
648 781
649 /* restart engine */ 782 /* restart engine */
650 ahci_start_engine(ap); 783 ahci_start_engine(port_mmio);
651 784
652 ata_tf_init(ap->device, &tf); 785 ata_tf_init(ap->device, &tf);
653 fis = pp->cmd_tbl; 786 fis = pp->cmd_tbl;
@@ -706,7 +839,7 @@ static int ahci_softreset(struct ata_port *ap, unsigned int *class)
706 return 0; 839 return 0;
707 840
708 fail_restart: 841 fail_restart:
709 ahci_start_engine(ap); 842 ahci_start_engine(port_mmio);
710 fail: 843 fail:
711 ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason); 844 ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
712 return rc; 845 return rc;
@@ -717,11 +850,13 @@ static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
717 struct ahci_port_priv *pp = ap->private_data; 850 struct ahci_port_priv *pp = ap->private_data;
718 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG; 851 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
719 struct ata_taskfile tf; 852 struct ata_taskfile tf;
853 void __iomem *mmio = ap->host_set->mmio_base;
854 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
720 int rc; 855 int rc;
721 856
722 DPRINTK("ENTER\n"); 857 DPRINTK("ENTER\n");
723 858
724 ahci_stop_engine(ap); 859 ahci_stop_engine(port_mmio);
725 860
726 /* clear D2H reception area to properly wait for D2H FIS */ 861 /* clear D2H reception area to properly wait for D2H FIS */
727 ata_tf_init(ap->device, &tf); 862 ata_tf_init(ap->device, &tf);
@@ -730,7 +865,7 @@ static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
730 865
731 rc = sata_std_hardreset(ap, class); 866 rc = sata_std_hardreset(ap, class);
732 867
733 ahci_start_engine(ap); 868 ahci_start_engine(port_mmio);
734 869
735 if (rc == 0 && ata_port_online(ap)) 870 if (rc == 0 && ata_port_online(ap))
736 *class = ahci_dev_classify(ap); 871 *class = ahci_dev_classify(ap);
@@ -940,14 +1075,8 @@ static void ahci_host_intr(struct ata_port *ap)
940 return; 1075 return;
941 1076
942 /* ignore interim PIO setup fis interrupts */ 1077 /* ignore interim PIO setup fis interrupts */
943 if (ata_tag_valid(ap->active_tag)) { 1078 if (ata_tag_valid(ap->active_tag) && (status & PORT_IRQ_PIOS_FIS))
944 struct ata_queued_cmd *qc = 1079 return;
945 ata_qc_from_tag(ap, ap->active_tag);
946
947 if (qc && qc->tf.protocol == ATA_PROT_PIO &&
948 (status & PORT_IRQ_PIOS_FIS))
949 return;
950 }
951 1080
952 if (ata_ratelimit()) 1081 if (ata_ratelimit())
953 ata_port_printk(ap, KERN_INFO, "spurious interrupt " 1082 ata_port_printk(ap, KERN_INFO, "spurious interrupt "
@@ -1052,10 +1181,13 @@ static void ahci_thaw(struct ata_port *ap)
1052 1181
1053static void ahci_error_handler(struct ata_port *ap) 1182static void ahci_error_handler(struct ata_port *ap)
1054{ 1183{
1184 void __iomem *mmio = ap->host_set->mmio_base;
1185 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1186
1055 if (!(ap->pflags & ATA_PFLAG_FROZEN)) { 1187 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
1056 /* restart engine */ 1188 /* restart engine */
1057 ahci_stop_engine(ap); 1189 ahci_stop_engine(port_mmio);
1058 ahci_start_engine(ap); 1190 ahci_start_engine(port_mmio);
1059 } 1191 }
1060 1192
1061 /* perform recovery */ 1193 /* perform recovery */
@@ -1066,15 +1198,176 @@ static void ahci_error_handler(struct ata_port *ap)
1066static void ahci_post_internal_cmd(struct ata_queued_cmd *qc) 1198static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
1067{ 1199{
1068 struct ata_port *ap = qc->ap; 1200 struct ata_port *ap = qc->ap;
1201 void __iomem *mmio = ap->host_set->mmio_base;
1202 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1069 1203
1070 if (qc->flags & ATA_QCFLAG_FAILED) 1204 if (qc->flags & ATA_QCFLAG_FAILED)
1071 qc->err_mask |= AC_ERR_OTHER; 1205 qc->err_mask |= AC_ERR_OTHER;
1072 1206
1073 if (qc->err_mask) { 1207 if (qc->err_mask) {
1074 /* make DMA engine forget about the failed command */ 1208 /* make DMA engine forget about the failed command */
1075 ahci_stop_engine(ap); 1209 ahci_stop_engine(port_mmio);
1076 ahci_start_engine(ap); 1210 ahci_start_engine(port_mmio);
1211 }
1212}
1213
1214static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
1215{
1216 struct ahci_host_priv *hpriv = ap->host_set->private_data;
1217 struct ahci_port_priv *pp = ap->private_data;
1218 void __iomem *mmio = ap->host_set->mmio_base;
1219 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1220 const char *emsg = NULL;
1221 int rc;
1222
1223 rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
1224 if (rc) {
1225 ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
1226 ahci_init_port(port_mmio, hpriv->cap,
1227 pp->cmd_slot_dma, pp->rx_fis_dma);
1228 }
1229
1230 return rc;
1231}
1232
1233static int ahci_port_resume(struct ata_port *ap)
1234{
1235 struct ahci_port_priv *pp = ap->private_data;
1236 struct ahci_host_priv *hpriv = ap->host_set->private_data;
1237 void __iomem *mmio = ap->host_set->mmio_base;
1238 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1239
1240 ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
1241
1242 return 0;
1243}
1244
1245static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
1246{
1247 struct ata_host_set *host_set = dev_get_drvdata(&pdev->dev);
1248 void __iomem *mmio = host_set->mmio_base;
1249 u32 ctl;
1250
1251 if (mesg.event == PM_EVENT_SUSPEND) {
1252 /* AHCI spec rev1.1 section 8.3.3:
1253 * Software must disable interrupts prior to requesting a
1254 * transition of the HBA to D3 state.
1255 */
1256 ctl = readl(mmio + HOST_CTL);
1257 ctl &= ~HOST_IRQ_EN;
1258 writel(ctl, mmio + HOST_CTL);
1259 readl(mmio + HOST_CTL); /* flush */
1077 } 1260 }
1261
1262 return ata_pci_device_suspend(pdev, mesg);
1263}
1264
1265static int ahci_pci_device_resume(struct pci_dev *pdev)
1266{
1267 struct ata_host_set *host_set = dev_get_drvdata(&pdev->dev);
1268 struct ahci_host_priv *hpriv = host_set->private_data;
1269 void __iomem *mmio = host_set->mmio_base;
1270 int rc;
1271
1272 ata_pci_device_do_resume(pdev);
1273
1274 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
1275 rc = ahci_reset_controller(mmio, pdev);
1276 if (rc)
1277 return rc;
1278
1279 ahci_init_controller(mmio, pdev, host_set->n_ports, hpriv->cap);
1280 }
1281
1282 ata_host_set_resume(host_set);
1283
1284 return 0;
1285}
1286
1287static int ahci_port_start(struct ata_port *ap)
1288{
1289 struct device *dev = ap->host_set->dev;
1290 struct ahci_host_priv *hpriv = ap->host_set->private_data;
1291 struct ahci_port_priv *pp;
1292 void __iomem *mmio = ap->host_set->mmio_base;
1293 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1294 void *mem;
1295 dma_addr_t mem_dma;
1296 int rc;
1297
1298 pp = kmalloc(sizeof(*pp), GFP_KERNEL);
1299 if (!pp)
1300 return -ENOMEM;
1301 memset(pp, 0, sizeof(*pp));
1302
1303 rc = ata_pad_alloc(ap, dev);
1304 if (rc) {
1305 kfree(pp);
1306 return rc;
1307 }
1308
1309 mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
1310 if (!mem) {
1311 ata_pad_free(ap, dev);
1312 kfree(pp);
1313 return -ENOMEM;
1314 }
1315 memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
1316
1317 /*
1318 * First item in chunk of DMA memory: 32-slot command table,
1319 * 32 bytes each in size
1320 */
1321 pp->cmd_slot = mem;
1322 pp->cmd_slot_dma = mem_dma;
1323
1324 mem += AHCI_CMD_SLOT_SZ;
1325 mem_dma += AHCI_CMD_SLOT_SZ;
1326
1327 /*
1328 * Second item: Received-FIS area
1329 */
1330 pp->rx_fis = mem;
1331 pp->rx_fis_dma = mem_dma;
1332
1333 mem += AHCI_RX_FIS_SZ;
1334 mem_dma += AHCI_RX_FIS_SZ;
1335
1336 /*
1337 * Third item: data area for storing a single command
1338 * and its scatter-gather table
1339 */
1340 pp->cmd_tbl = mem;
1341 pp->cmd_tbl_dma = mem_dma;
1342
1343 ap->private_data = pp;
1344
1345 /* initialize port */
1346 ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
1347
1348 return 0;
1349}
1350
1351static void ahci_port_stop(struct ata_port *ap)
1352{
1353 struct device *dev = ap->host_set->dev;
1354 struct ahci_host_priv *hpriv = ap->host_set->private_data;
1355 struct ahci_port_priv *pp = ap->private_data;
1356 void __iomem *mmio = ap->host_set->mmio_base;
1357 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1358 const char *emsg = NULL;
1359 int rc;
1360
1361 /* de-initialize port */
1362 rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
1363 if (rc)
1364 ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
1365
1366 ap->private_data = NULL;
1367 dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
1368 pp->cmd_slot, pp->cmd_slot_dma);
1369 ata_pad_free(ap, dev);
1370 kfree(pp);
1078} 1371}
1079 1372
1080static void ahci_setup_port(struct ata_ioports *port, unsigned long base, 1373static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
@@ -1095,47 +1388,12 @@ static int ahci_host_init(struct ata_probe_ent *probe_ent)
1095 struct ahci_host_priv *hpriv = probe_ent->private_data; 1388 struct ahci_host_priv *hpriv = probe_ent->private_data;
1096 struct pci_dev *pdev = to_pci_dev(probe_ent->dev); 1389 struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
1097 void __iomem *mmio = probe_ent->mmio_base; 1390 void __iomem *mmio = probe_ent->mmio_base;
1098 u32 tmp, cap_save; 1391 unsigned int i, using_dac;
1099 unsigned int i, j, using_dac;
1100 int rc; 1392 int rc;
1101 void __iomem *port_mmio;
1102 1393
1103 cap_save = readl(mmio + HOST_CAP); 1394 rc = ahci_reset_controller(mmio, pdev);
1104 cap_save &= ( (1<<28) | (1<<17) ); 1395 if (rc)
1105 cap_save |= (1 << 27); 1396 return rc;
1106
1107 /* global controller reset */
1108 tmp = readl(mmio + HOST_CTL);
1109 if ((tmp & HOST_RESET) == 0) {
1110 writel(tmp | HOST_RESET, mmio + HOST_CTL);
1111 readl(mmio + HOST_CTL); /* flush */
1112 }
1113
1114 /* reset must complete within 1 second, or
1115 * the hardware should be considered fried.
1116 */
1117 ssleep(1);
1118
1119 tmp = readl(mmio + HOST_CTL);
1120 if (tmp & HOST_RESET) {
1121 dev_printk(KERN_ERR, &pdev->dev,
1122 "controller reset failed (0x%x)\n", tmp);
1123 return -EIO;
1124 }
1125
1126 writel(HOST_AHCI_EN, mmio + HOST_CTL);
1127 (void) readl(mmio + HOST_CTL); /* flush */
1128 writel(cap_save, mmio + HOST_CAP);
1129 writel(0xf, mmio + HOST_PORTS_IMPL);
1130 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
1131
1132 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
1133 u16 tmp16;
1134
1135 pci_read_config_word(pdev, 0x92, &tmp16);
1136 tmp16 |= 0xf;
1137 pci_write_config_word(pdev, 0x92, tmp16);
1138 }
1139 1397
1140 hpriv->cap = readl(mmio + HOST_CAP); 1398 hpriv->cap = readl(mmio + HOST_CAP);
1141 hpriv->port_map = readl(mmio + HOST_PORTS_IMPL); 1399 hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
@@ -1171,63 +1429,10 @@ static int ahci_host_init(struct ata_probe_ent *probe_ent)
1171 } 1429 }
1172 } 1430 }
1173 1431
1174 for (i = 0; i < probe_ent->n_ports; i++) { 1432 for (i = 0; i < probe_ent->n_ports; i++)
1175#if 0 /* BIOSen initialize this incorrectly */ 1433 ahci_setup_port(&probe_ent->port[i], (unsigned long) mmio, i);
1176 if (!(hpriv->port_map & (1 << i)))
1177 continue;
1178#endif
1179
1180 port_mmio = ahci_port_base(mmio, i);
1181 VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
1182
1183 ahci_setup_port(&probe_ent->port[i],
1184 (unsigned long) mmio, i);
1185
1186 /* make sure port is not active */
1187 tmp = readl(port_mmio + PORT_CMD);
1188 VPRINTK("PORT_CMD 0x%x\n", tmp);
1189 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
1190 PORT_CMD_FIS_RX | PORT_CMD_START)) {
1191 tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
1192 PORT_CMD_FIS_RX | PORT_CMD_START);
1193 writel(tmp, port_mmio + PORT_CMD);
1194 readl(port_mmio + PORT_CMD); /* flush */
1195
1196 /* spec says 500 msecs for each bit, so
1197 * this is slightly incorrect.
1198 */
1199 msleep(500);
1200 }
1201
1202 writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
1203
1204 j = 0;
1205 while (j < 100) {
1206 msleep(10);
1207 tmp = readl(port_mmio + PORT_SCR_STAT);
1208 if ((tmp & 0xf) == 0x3)
1209 break;
1210 j++;
1211 }
1212
1213 tmp = readl(port_mmio + PORT_SCR_ERR);
1214 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1215 writel(tmp, port_mmio + PORT_SCR_ERR);
1216 1434
1217 /* ack any pending irq events for this port */ 1435 ahci_init_controller(mmio, pdev, probe_ent->n_ports, hpriv->cap);
1218 tmp = readl(port_mmio + PORT_IRQ_STAT);
1219 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1220 if (tmp)
1221 writel(tmp, port_mmio + PORT_IRQ_STAT);
1222
1223 writel(1 << i, mmio + HOST_IRQ_STAT);
1224 }
1225
1226 tmp = readl(mmio + HOST_CTL);
1227 VPRINTK("HOST_CTL 0x%x\n", tmp);
1228 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1229 tmp = readl(mmio + HOST_CTL);
1230 VPRINTK("HOST_CTL 0x%x\n", tmp);
1231 1436
1232 pci_set_master(pdev); 1437 pci_set_master(pdev);
1233 1438