1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/pci.h>
30#include <linux/init.h>
31#include <linux/blkdev.h>
32#include <linux/delay.h>
33#include <linux/interrupt.h>
34#include <linux/sched.h>
35#include "scsi.h"
36#include <scsi/scsi_host.h>
37#include <linux/libata.h>
38#include <asm/io.h>
39
40#define DRV_NAME "ahci"
41#define DRV_VERSION "1.00"
42
43enum {
44 AHCI_PCI_BAR = 5,
45 AHCI_MAX_SG = 168,
46 AHCI_DMA_BOUNDARY = 0xffffffff,
47 AHCI_USE_CLUSTERING = 0,
48 AHCI_CMD_SLOT_SZ = 32 * 32,
49 AHCI_RX_FIS_SZ = 256,
50 AHCI_CMD_TBL_HDR = 0x80,
51 AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR + (AHCI_MAX_SG * 16),
52 AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_SZ +
53 AHCI_RX_FIS_SZ,
54 AHCI_IRQ_ON_SG = (1 << 31),
55 AHCI_CMD_ATAPI = (1 << 5),
56 AHCI_CMD_WRITE = (1 << 6),
57
58 RX_FIS_D2H_REG = 0x40,
59
60 board_ahci = 0,
61
62
63 HOST_CAP = 0x00,
64 HOST_CTL = 0x04,
65 HOST_IRQ_STAT = 0x08,
66 HOST_PORTS_IMPL = 0x0c,
67 HOST_VERSION = 0x10,
68
69
70 HOST_RESET = (1 << 0),
71 HOST_IRQ_EN = (1 << 1),
72 HOST_AHCI_EN = (1 << 31),
73
74
75 HOST_CAP_64 = (1 << 31),
76
77
78 PORT_LST_ADDR = 0x00,
79 PORT_LST_ADDR_HI = 0x04,
80 PORT_FIS_ADDR = 0x08,
81 PORT_FIS_ADDR_HI = 0x0c,
82 PORT_IRQ_STAT = 0x10,
83 PORT_IRQ_MASK = 0x14,
84 PORT_CMD = 0x18,
85 PORT_TFDATA = 0x20,
86 PORT_SIG = 0x24,
87 PORT_CMD_ISSUE = 0x38,
88 PORT_SCR = 0x28,
89 PORT_SCR_STAT = 0x28,
90 PORT_SCR_CTL = 0x2c,
91 PORT_SCR_ERR = 0x30,
92 PORT_SCR_ACT = 0x34,
93
94
95 PORT_IRQ_COLD_PRES = (1 << 31),
96 PORT_IRQ_TF_ERR = (1 << 30),
97 PORT_IRQ_HBUS_ERR = (1 << 29),
98 PORT_IRQ_HBUS_DATA_ERR = (1 << 28),
99 PORT_IRQ_IF_ERR = (1 << 27),
100 PORT_IRQ_IF_NONFATAL = (1 << 26),
101 PORT_IRQ_OVERFLOW = (1 << 24),
102 PORT_IRQ_BAD_PMP = (1 << 23),
103
104 PORT_IRQ_PHYRDY = (1 << 22),
105 PORT_IRQ_DEV_ILCK = (1 << 7),
106 PORT_IRQ_CONNECT = (1 << 6),
107 PORT_IRQ_SG_DONE = (1 << 5),
108 PORT_IRQ_UNK_FIS = (1 << 4),
109 PORT_IRQ_SDB_FIS = (1 << 3),
110 PORT_IRQ_DMAS_FIS = (1 << 2),
111 PORT_IRQ_PIOS_FIS = (1 << 1),
112 PORT_IRQ_D2H_REG_FIS = (1 << 0),
113
114 PORT_IRQ_FATAL = PORT_IRQ_TF_ERR |
115 PORT_IRQ_HBUS_ERR |
116 PORT_IRQ_HBUS_DATA_ERR |
117 PORT_IRQ_IF_ERR,
118 DEF_PORT_IRQ = PORT_IRQ_FATAL | PORT_IRQ_PHYRDY |
119 PORT_IRQ_CONNECT | PORT_IRQ_SG_DONE |
120 PORT_IRQ_UNK_FIS | PORT_IRQ_SDB_FIS |
121 PORT_IRQ_DMAS_FIS | PORT_IRQ_PIOS_FIS |
122 PORT_IRQ_D2H_REG_FIS,
123
124
125 PORT_CMD_LIST_ON = (1 << 15),
126 PORT_CMD_FIS_ON = (1 << 14),
127 PORT_CMD_FIS_RX = (1 << 4),
128 PORT_CMD_POWER_ON = (1 << 2),
129 PORT_CMD_SPIN_UP = (1 << 1),
130 PORT_CMD_START = (1 << 0),
131
132 PORT_CMD_ICC_ACTIVE = (0x1 << 28),
133 PORT_CMD_ICC_PARTIAL = (0x2 << 28),
134 PORT_CMD_ICC_SLUMBER = (0x6 << 28),
135};
136
137struct ahci_cmd_hdr {
138 u32 opts;
139 u32 status;
140 u32 tbl_addr;
141 u32 tbl_addr_hi;
142 u32 reserved[4];
143};
144
145struct ahci_sg {
146 u32 addr;
147 u32 addr_hi;
148 u32 reserved;
149 u32 flags_size;
150};
151
152struct ahci_host_priv {
153 unsigned long flags;
154 u32 cap;
155 u32 port_map;
156};
157
158struct ahci_port_priv {
159 struct ahci_cmd_hdr *cmd_slot;
160 dma_addr_t cmd_slot_dma;
161 void *cmd_tbl;
162 dma_addr_t cmd_tbl_dma;
163 struct ahci_sg *cmd_tbl_sg;
164 void *rx_fis;
165 dma_addr_t rx_fis_dma;
166};
167
168static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
169static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
170static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
171static int ahci_qc_issue(struct ata_queued_cmd *qc);
172static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
173static void ahci_phy_reset(struct ata_port *ap);
174static void ahci_irq_clear(struct ata_port *ap);
175static void ahci_eng_timeout(struct ata_port *ap);
176static int ahci_port_start(struct ata_port *ap);
177static void ahci_port_stop(struct ata_port *ap);
178static void ahci_host_stop(struct ata_host_set *host_set);
179static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
180static void ahci_qc_prep(struct ata_queued_cmd *qc);
181static u8 ahci_check_status(struct ata_port *ap);
182static u8 ahci_check_err(struct ata_port *ap);
183static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc);
184
185static Scsi_Host_Template ahci_sht = {
186 .module = THIS_MODULE,
187 .name = DRV_NAME,
188 .ioctl = ata_scsi_ioctl,
189 .detect = ata_scsi_detect,
190 .release = ata_scsi_release,
191 .queuecommand = ata_scsi_queuecmd,
192 .eh_strategy_handler = ata_scsi_error,
193 .can_queue = ATA_DEF_QUEUE,
194 .this_id = ATA_SHT_THIS_ID,
195 .sg_tablesize = AHCI_MAX_SG,
196 .max_sectors = ATA_MAX_SECTORS,
197 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
198 .use_new_eh_code = ATA_SHT_NEW_EH_CODE,
199 .emulated = ATA_SHT_EMULATED,
200 .use_clustering = AHCI_USE_CLUSTERING,
201 .proc_name = DRV_NAME,
202 .bios_param = ata_std_bios_param,
203};
204
205static struct ata_port_operations ahci_ops = {
206 .port_disable = ata_port_disable,
207
208 .check_status = ahci_check_status,
209 .check_altstatus = ahci_check_status,
210 .check_err = ahci_check_err,
211 .dev_select = ata_noop_dev_select,
212
213 .tf_read = ahci_tf_read,
214
215 .phy_reset = ahci_phy_reset,
216
217 .qc_prep = ahci_qc_prep,
218 .qc_issue = ahci_qc_issue,
219
220 .eng_timeout = ahci_eng_timeout,
221
222 .irq_handler = ahci_interrupt,
223 .irq_clear = ahci_irq_clear,
224
225 .scr_read = ahci_scr_read,
226 .scr_write = ahci_scr_write,
227
228 .port_start = ahci_port_start,
229 .port_stop = ahci_port_stop,
230 .host_stop = ahci_host_stop,
231};
232
233static struct ata_port_info ahci_port_info[] = {
234
235 {
236 .sht = &ahci_sht,
237 .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
238 ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
239 ATA_FLAG_PIO_DMA,
240 .pio_mask = 0x03,
241 .udma_mask = 0x7f,
242 .port_ops = &ahci_ops,
243 },
244};
245
246static struct pci_device_id ahci_pci_tbl[] = {
247 { PCI_VENDOR_ID_INTEL, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
248 board_ahci },
249 { PCI_VENDOR_ID_INTEL, 0x2653, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
250 board_ahci },
251 { PCI_VENDOR_ID_INTEL, 0x27c1, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
252 board_ahci },
253 { PCI_VENDOR_ID_INTEL, 0x27c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
254 board_ahci },
255 { PCI_VENDOR_ID_INTEL, 0x27c3, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
256 board_ahci },
257 { PCI_VENDOR_ID_AL, 0x5288, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
258 board_ahci },
259 { }
260};
261
262
263static struct pci_driver ahci_pci_driver = {
264 .name = DRV_NAME,
265 .id_table = ahci_pci_tbl,
266 .probe = ahci_init_one,
267 .remove = ata_pci_remove_one,
268};
269
270
271static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
272{
273 return base + 0x100 + (port * 0x80);
274}
275
276static inline void *ahci_port_base (void *base, unsigned int port)
277{
278 return (void *) ahci_port_base_ul((unsigned long)base, port);
279}
280
281static void ahci_host_stop(struct ata_host_set *host_set)
282{
283 struct ahci_host_priv *hpriv = host_set->private_data;
284 kfree(hpriv);
285}
286
287static int ahci_port_start(struct ata_port *ap)
288{
289 struct device *dev = ap->host_set->dev;
290 struct ahci_host_priv *hpriv = ap->host_set->private_data;
291 struct ahci_port_priv *pp;
292 int rc;
293 void *mem, *mmio = ap->host_set->mmio_base;
294 void *port_mmio = ahci_port_base(mmio, ap->port_no);
295 dma_addr_t mem_dma;
296
297 rc = ata_port_start(ap);
298 if (rc)
299 return rc;
300
301 pp = kmalloc(sizeof(*pp), GFP_KERNEL);
302 if (!pp) {
303 rc = -ENOMEM;
304 goto err_out;
305 }
306 memset(pp, 0, sizeof(*pp));
307
308 mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
309 if (!mem) {
310 rc = -ENOMEM;
311 goto err_out_kfree;
312 }
313 memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
314
315
316
317
318
319 pp->cmd_slot = mem;
320 pp->cmd_slot_dma = mem_dma;
321
322 mem += AHCI_CMD_SLOT_SZ;
323 mem_dma += AHCI_CMD_SLOT_SZ;
324
325
326
327
328 pp->rx_fis = mem;
329 pp->rx_fis_dma = mem_dma;
330
331 mem += AHCI_RX_FIS_SZ;
332 mem_dma += AHCI_RX_FIS_SZ;
333
334
335
336
337
338 pp->cmd_tbl = mem;
339 pp->cmd_tbl_dma = mem_dma;
340
341 pp->cmd_tbl_sg = mem + AHCI_CMD_TBL_HDR;
342
343 ap->private_data = pp;
344
345 if (hpriv->cap & HOST_CAP_64)
346 writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
347 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
348 readl(port_mmio + PORT_LST_ADDR);
349
350 if (hpriv->cap & HOST_CAP_64)
351 writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
352 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
353 readl(port_mmio + PORT_FIS_ADDR);
354
355 writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
356 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
357 PORT_CMD_START, port_mmio + PORT_CMD);
358 readl(port_mmio + PORT_CMD);
359
360 return 0;
361
362err_out_kfree:
363 kfree(pp);
364err_out:
365 ata_port_stop(ap);
366 return rc;
367}
368
369
370static void ahci_port_stop(struct ata_port *ap)
371{
372 struct device *dev = ap->host_set->dev;
373 struct ahci_port_priv *pp = ap->private_data;
374 void *mmio = ap->host_set->mmio_base;
375 void *port_mmio = ahci_port_base(mmio, ap->port_no);
376 u32 tmp;
377
378 tmp = readl(port_mmio + PORT_CMD);
379 tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX);
380 writel(tmp, port_mmio + PORT_CMD);
381 readl(port_mmio + PORT_CMD);
382
383
384
385
386 msleep(500);
387
388 ap->private_data = NULL;
389 dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
390 pp->cmd_slot, pp->cmd_slot_dma);
391 kfree(pp);
392 ata_port_stop(ap);
393}
394
395static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
396{
397 unsigned int sc_reg;
398
399 switch (sc_reg_in) {
400 case SCR_STATUS: sc_reg = 0; break;
401 case SCR_CONTROL: sc_reg = 1; break;
402 case SCR_ERROR: sc_reg = 2; break;
403 case SCR_ACTIVE: sc_reg = 3; break;
404 default:
405 return 0xffffffffU;
406 }
407
408 return readl((void *) ap->ioaddr.scr_addr + (sc_reg * 4));
409}
410
411
412static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
413 u32 val)
414{
415 unsigned int sc_reg;
416
417 switch (sc_reg_in) {
418 case SCR_STATUS: sc_reg = 0; break;
419 case SCR_CONTROL: sc_reg = 1; break;
420 case SCR_ERROR: sc_reg = 2; break;
421 case SCR_ACTIVE: sc_reg = 3; break;
422 default:
423 return;
424 }
425
426 writel(val, (void *) ap->ioaddr.scr_addr + (sc_reg * 4));
427}
428
429static void ahci_phy_reset(struct ata_port *ap)
430{
431 void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
432 struct ata_taskfile tf;
433 struct ata_device *dev = &ap->device[0];
434 u32 tmp;
435
436 __sata_phy_reset(ap);
437
438 if (ap->flags & ATA_FLAG_PORT_DISABLED)
439 return;
440
441 tmp = readl(port_mmio + PORT_SIG);
442 tf.lbah = (tmp >> 24) & 0xff;
443 tf.lbam = (tmp >> 16) & 0xff;
444 tf.lbal = (tmp >> 8) & 0xff;
445 tf.nsect = (tmp) & 0xff;
446
447 dev->class = ata_dev_classify(&tf);
448 if (!ata_dev_present(dev))
449 ata_port_disable(ap);
450}
451
452static u8 ahci_check_status(struct ata_port *ap)
453{
454 void *mmio = (void *) ap->ioaddr.cmd_addr;
455
456 return readl(mmio + PORT_TFDATA) & 0xFF;
457}
458
459static u8 ahci_check_err(struct ata_port *ap)
460{
461 void *mmio = (void *) ap->ioaddr.cmd_addr;
462
463 return (readl(mmio + PORT_TFDATA) >> 8) & 0xFF;
464}
465
466static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
467{
468 struct ahci_port_priv *pp = ap->private_data;
469 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
470
471 ata_tf_from_fis(d2h_fis, tf);
472}
473
474static void ahci_fill_sg(struct ata_queued_cmd *qc)
475{
476 struct ahci_port_priv *pp = qc->ap->private_data;
477 unsigned int i;
478
479 VPRINTK("ENTER\n");
480
481
482
483
484 for (i = 0; i < qc->n_elem; i++) {
485 u32 sg_len;
486 dma_addr_t addr;
487
488 addr = sg_dma_address(&qc->sg[i]);
489 sg_len = sg_dma_len(&qc->sg[i]);
490
491 pp->cmd_tbl_sg[i].addr = cpu_to_le32(addr & 0xffffffff);
492 pp->cmd_tbl_sg[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
493 pp->cmd_tbl_sg[i].flags_size = cpu_to_le32(sg_len - 1);
494 }
495}
496
497static void ahci_qc_prep(struct ata_queued_cmd *qc)
498{
499 struct ahci_port_priv *pp = qc->ap->private_data;
500 u32 opts;
501 const u32 cmd_fis_len = 5;
502
503
504
505
506
507
508 opts = (qc->n_elem << 16) | cmd_fis_len;
509 if (qc->tf.flags & ATA_TFLAG_WRITE)
510 opts |= AHCI_CMD_WRITE;
511
512 switch (qc->tf.protocol) {
513 case ATA_PROT_ATAPI:
514 case ATA_PROT_ATAPI_NODATA:
515 case ATA_PROT_ATAPI_DMA:
516 opts |= AHCI_CMD_ATAPI;
517 break;
518
519 default:
520
521 break;
522 }
523
524 pp->cmd_slot[0].opts = cpu_to_le32(opts);
525 pp->cmd_slot[0].status = 0;
526 pp->cmd_slot[0].tbl_addr = cpu_to_le32(pp->cmd_tbl_dma & 0xffffffff);
527 pp->cmd_slot[0].tbl_addr_hi = cpu_to_le32((pp->cmd_tbl_dma >> 16) >> 16);
528
529
530
531
532
533 ata_tf_to_fis(&qc->tf, pp->cmd_tbl, 0);
534
535 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
536 return;
537
538 ahci_fill_sg(qc);
539}
540
541static void ahci_intr_error(struct ata_port *ap, u32 irq_stat)
542{
543 void *mmio = ap->host_set->mmio_base;
544 void *port_mmio = ahci_port_base(mmio, ap->port_no);
545 u32 tmp;
546 int work;
547
548
549 tmp = readl(port_mmio + PORT_CMD);
550 tmp &= ~PORT_CMD_START;
551 writel(tmp, port_mmio + PORT_CMD);
552
553
554
555
556 work = 1000;
557 while (work-- > 0) {
558 tmp = readl(port_mmio + PORT_CMD);
559 if ((tmp & PORT_CMD_LIST_ON) == 0)
560 break;
561 udelay(10);
562 }
563
564
565 tmp = readl(port_mmio + PORT_SCR_ERR);
566 writel(tmp, port_mmio + PORT_SCR_ERR);
567
568
569
570
571 tmp = readl(port_mmio + PORT_TFDATA);
572 if (tmp & (ATA_BUSY | ATA_DRQ)) {
573 writel(0x301, port_mmio + PORT_SCR_CTL);
574 readl(port_mmio + PORT_SCR_CTL);
575 udelay(10);
576 writel(0x300, port_mmio + PORT_SCR_CTL);
577 readl(port_mmio + PORT_SCR_CTL);
578 }
579
580
581 tmp = readl(port_mmio + PORT_CMD);
582 tmp |= PORT_CMD_START;
583 writel(tmp, port_mmio + PORT_CMD);
584 readl(port_mmio + PORT_CMD);
585
586 printk(KERN_WARNING "ata%u: error occurred, port reset\n", ap->id);
587}
588
589static void ahci_eng_timeout(struct ata_port *ap)
590{
591 void *mmio = ap->host_set->mmio_base;
592 void *port_mmio = ahci_port_base(mmio, ap->port_no);
593 struct ata_queued_cmd *qc;
594
595 DPRINTK("ENTER\n");
596
597 ahci_intr_error(ap, readl(port_mmio + PORT_IRQ_STAT));
598
599 qc = ata_qc_from_tag(ap, ap->active_tag);
600 if (!qc) {
601 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
602 ap->id);
603 } else {
604
605
606
607
608
609
610 qc->scsidone = scsi_finish_command;
611 ata_qc_complete(qc, ATA_ERR);
612 }
613
614}
615
616static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
617{
618 void *mmio = ap->host_set->mmio_base;
619 void *port_mmio = ahci_port_base(mmio, ap->port_no);
620 u32 status, serr, ci;
621
622 serr = readl(port_mmio + PORT_SCR_ERR);
623 writel(serr, port_mmio + PORT_SCR_ERR);
624
625 status = readl(port_mmio + PORT_IRQ_STAT);
626 writel(status, port_mmio + PORT_IRQ_STAT);
627
628 ci = readl(port_mmio + PORT_CMD_ISSUE);
629 if (likely((ci & 0x1) == 0)) {
630 if (qc) {
631 ata_qc_complete(qc, 0);
632 qc = NULL;
633 }
634 }
635
636 if (status & PORT_IRQ_FATAL) {
637 ahci_intr_error(ap, status);
638 if (qc)
639 ata_qc_complete(qc, ATA_ERR);
640 }
641
642 return 1;
643}
644
645static void ahci_irq_clear(struct ata_port *ap)
646{
647
648}
649
650static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
651{
652 struct ata_host_set *host_set = dev_instance;
653 struct ahci_host_priv *hpriv;
654 unsigned int i, handled = 0;
655 void *mmio;
656 u32 irq_stat, irq_ack = 0;
657
658 VPRINTK("ENTER\n");
659
660 hpriv = host_set->private_data;
661 mmio = host_set->mmio_base;
662
663
664 irq_stat = readl(mmio + HOST_IRQ_STAT);
665 irq_stat &= hpriv->port_map;
666 if (!irq_stat)
667 return IRQ_NONE;
668
669 spin_lock(&host_set->lock);
670
671 for (i = 0; i < host_set->n_ports; i++) {
672 struct ata_port *ap;
673 u32 tmp;
674
675 VPRINTK("port %u\n", i);
676 ap = host_set->ports[i];
677 tmp = irq_stat & (1 << i);
678 if (tmp && ap) {
679 struct ata_queued_cmd *qc;
680 qc = ata_qc_from_tag(ap, ap->active_tag);
681 if (ahci_host_intr(ap, qc))
682 irq_ack |= (1 << i);
683 }
684 }
685
686 if (irq_ack) {
687 writel(irq_ack, mmio + HOST_IRQ_STAT);
688 handled = 1;
689 }
690
691 spin_unlock(&host_set->lock);
692
693 VPRINTK("EXIT\n");
694
695 return IRQ_RETVAL(handled);
696}
697
698static int ahci_qc_issue(struct ata_queued_cmd *qc)
699{
700 struct ata_port *ap = qc->ap;
701 void *port_mmio = (void *) ap->ioaddr.cmd_addr;
702
703 writel(1, port_mmio + PORT_SCR_ACT);
704 readl(port_mmio + PORT_SCR_ACT);
705
706 writel(1, port_mmio + PORT_CMD_ISSUE);
707 readl(port_mmio + PORT_CMD_ISSUE);
708
709 return 0;
710}
711
712static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
713 unsigned int port_idx)
714{
715 VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
716 base = ahci_port_base_ul(base, port_idx);
717 VPRINTK("base now==0x%lx\n", base);
718
719 port->cmd_addr = base;
720 port->scr_addr = base + PORT_SCR;
721
722 VPRINTK("EXIT\n");
723}
724
725static int ahci_host_init(struct ata_probe_ent *probe_ent)
726{
727 struct ahci_host_priv *hpriv = probe_ent->private_data;
728 struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
729 void __iomem *mmio = probe_ent->mmio_base;
730 u32 tmp, cap_save;
731 u16 tmp16;
732 unsigned int i, j, using_dac;
733 int rc;
734 void __iomem *port_mmio;
735
736 cap_save = readl(mmio + HOST_CAP);
737 cap_save &= ( (1<<28) | (1<<17) );
738 cap_save |= (1 << 27);
739
740
741 tmp = readl(mmio + HOST_CTL);
742 if ((tmp & HOST_RESET) == 0) {
743 writel(tmp | HOST_RESET, mmio + HOST_CTL);
744 readl(mmio + HOST_CTL);
745 }
746
747
748
749
750 ssleep(1);
751
752 tmp = readl(mmio + HOST_CTL);
753 if (tmp & HOST_RESET) {
754 printk(KERN_ERR DRV_NAME "(%s): controller reset failed (0x%x)\n",
755 pci_name(pdev), tmp);
756 return -EIO;
757 }
758
759 writel(HOST_AHCI_EN, mmio + HOST_CTL);
760 (void) readl(mmio + HOST_CTL);
761 writel(cap_save, mmio + HOST_CAP);
762 writel(0xf, mmio + HOST_PORTS_IMPL);
763 (void) readl(mmio + HOST_PORTS_IMPL);
764
765 pci_read_config_word(pdev, 0x92, &tmp16);
766 tmp16 |= 0xf;
767 pci_write_config_word(pdev, 0x92, tmp16);
768
769 hpriv->cap = readl(mmio + HOST_CAP);
770 hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
771 probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
772
773 VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
774 hpriv->cap, hpriv->port_map, probe_ent->n_ports);
775
776 using_dac = hpriv->cap & HOST_CAP_64;
777 if (using_dac &&
778 !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
779 hpriv->flags |= HOST_CAP_64;
780 } else {
781 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
782 if (rc) {
783 printk(KERN_ERR DRV_NAME "(%s): 32-bit DMA enable failed\n",
784 pci_name(pdev));
785 return rc;
786 }
787 }
788
789 for (i = 0; i < probe_ent->n_ports; i++) {
790#if 0
791 if (!(hpriv->port_map & (1 << i)))
792 continue;
793#endif
794
795 port_mmio = ahci_port_base(mmio, i);
796 VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
797
798 ahci_setup_port(&probe_ent->port[i],
799 (unsigned long) mmio, i);
800
801
802 tmp = readl(port_mmio + PORT_CMD);
803 VPRINTK("PORT_CMD 0x%x\n", tmp);
804 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
805 PORT_CMD_FIS_RX | PORT_CMD_START)) {
806 tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
807 PORT_CMD_FIS_RX | PORT_CMD_START);
808 writel(tmp, port_mmio + PORT_CMD);
809 readl(port_mmio + PORT_CMD);
810
811
812
813
814 msleep(500);
815 }
816
817 writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
818
819 j = 0;
820 while (j < 100) {
821 msleep(10);
822 tmp = readl(port_mmio + PORT_SCR_STAT);
823 if ((tmp & 0xf) == 0x3)
824 break;
825 j++;
826 }
827
828 tmp = readl(port_mmio + PORT_SCR_ERR);
829 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
830 writel(tmp, port_mmio + PORT_SCR_ERR);
831
832
833 tmp = readl(port_mmio + PORT_IRQ_STAT);
834 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
835 if (tmp)
836 writel(tmp, port_mmio + PORT_IRQ_STAT);
837
838 writel(1 << i, mmio + HOST_IRQ_STAT);
839
840
841 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
842 }
843
844 tmp = readl(mmio + HOST_CTL);
845 VPRINTK("HOST_CTL 0x%x\n", tmp);
846 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
847 tmp = readl(mmio + HOST_CTL);
848 VPRINTK("HOST_CTL 0x%x\n", tmp);
849
850 pci_set_master(pdev);
851
852 return 0;
853}
854
855
856static void pci_enable_intx(struct pci_dev *pdev)
857{
858 u16 pci_command;
859
860 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
861 if (pci_command & PCI_COMMAND_INTX_DISABLE) {
862 pci_command &= ~PCI_COMMAND_INTX_DISABLE;
863 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
864 }
865}
866
867static void ahci_print_info(struct ata_probe_ent *probe_ent)
868{
869 struct ahci_host_priv *hpriv = probe_ent->private_data;
870 struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
871 void *mmio = probe_ent->mmio_base;
872 u32 vers, cap, impl, speed;
873 const char *speed_s;
874 u16 cc;
875 const char *scc_s;
876
877 vers = readl(mmio + HOST_VERSION);
878 cap = hpriv->cap;
879 impl = hpriv->port_map;
880
881 speed = (cap >> 20) & 0xf;
882 if (speed == 1)
883 speed_s = "1.5";
884 else if (speed == 2)
885 speed_s = "3";
886 else
887 speed_s = "?";
888
889 pci_read_config_word(pdev, 0x0a, &cc);
890 if (cc == 0x0101)
891 scc_s = "IDE";
892 else if (cc == 0x0106)
893 scc_s = "SATA";
894 else if (cc == 0x0104)
895 scc_s = "RAID";
896 else
897 scc_s = "unknown";
898
899 printk(KERN_INFO DRV_NAME "(%s) AHCI %02x%02x.%02x%02x "
900 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
901 ,
902 pci_name(pdev),
903
904 (vers >> 24) & 0xff,
905 (vers >> 16) & 0xff,
906 (vers >> 8) & 0xff,
907 vers & 0xff,
908
909 ((cap >> 8) & 0x1f) + 1,
910 (cap & 0x1f) + 1,
911 speed_s,
912 impl,
913 scc_s);
914
915 printk(KERN_INFO DRV_NAME "(%s) flags: "
916 "%s%s%s%s%s%s"
917 "%s%s%s%s%s%s%s\n"
918 ,
919 pci_name(pdev),
920
921 cap & (1 << 31) ? "64bit " : "",
922 cap & (1 << 30) ? "ncq " : "",
923 cap & (1 << 28) ? "ilck " : "",
924 cap & (1 << 27) ? "stag " : "",
925 cap & (1 << 26) ? "pm " : "",
926 cap & (1 << 25) ? "led " : "",
927
928 cap & (1 << 24) ? "clo " : "",
929 cap & (1 << 19) ? "nz " : "",
930 cap & (1 << 18) ? "only " : "",
931 cap & (1 << 17) ? "pmp " : "",
932 cap & (1 << 15) ? "pio " : "",
933 cap & (1 << 14) ? "slum " : "",
934 cap & (1 << 13) ? "part " : ""
935 );
936}
937
938static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
939{
940 static int printed_version;
941 struct ata_probe_ent *probe_ent = NULL;
942 struct ahci_host_priv *hpriv;
943 unsigned long base;
944 void *mmio_base;
945 unsigned int board_idx = (unsigned int) ent->driver_data;
946 int pci_dev_busy = 0;
947 int rc;
948
949 VPRINTK("ENTER\n");
950
951 if (!printed_version++)
952 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
953
954 rc = pci_enable_device(pdev);
955 if (rc)
956 return rc;
957
958 rc = pci_request_regions(pdev, DRV_NAME);
959 if (rc) {
960 pci_dev_busy = 1;
961 goto err_out;
962 }
963
964 pci_enable_intx(pdev);
965
966 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
967 if (probe_ent == NULL) {
968 rc = -ENOMEM;
969 goto err_out_regions;
970 }
971
972 memset(probe_ent, 0, sizeof(*probe_ent));
973 probe_ent->dev = pci_dev_to_dev(pdev);
974 INIT_LIST_HEAD(&probe_ent->node);
975
976 mmio_base = ioremap(pci_resource_start(pdev, AHCI_PCI_BAR),
977 pci_resource_len(pdev, AHCI_PCI_BAR));
978 if (mmio_base == NULL) {
979 rc = -ENOMEM;
980 goto err_out_free_ent;
981 }
982 base = (unsigned long) mmio_base;
983
984 hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
985 if (!hpriv) {
986 rc = -ENOMEM;
987 goto err_out_iounmap;
988 }
989 memset(hpriv, 0, sizeof(*hpriv));
990
991 probe_ent->sht = ahci_port_info[board_idx].sht;
992 probe_ent->host_flags = ahci_port_info[board_idx].host_flags;
993 probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
994 probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
995 probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
996
997 probe_ent->irq = pdev->irq;
998 probe_ent->irq_flags = SA_SHIRQ;
999 probe_ent->mmio_base = mmio_base;
1000 probe_ent->private_data = hpriv;
1001
1002
1003 rc = ahci_host_init(probe_ent);
1004 if (rc)
1005 goto err_out_hpriv;
1006
1007 ahci_print_info(probe_ent);
1008
1009 ata_add_to_probe_list(probe_ent);
1010
1011 return 0;
1012
1013err_out_hpriv:
1014 kfree(hpriv);
1015err_out_iounmap:
1016 iounmap(mmio_base);
1017err_out_free_ent:
1018 kfree(probe_ent);
1019err_out_regions:
1020 pci_release_regions(pdev);
1021err_out:
1022 if (!pci_dev_busy)
1023 pci_disable_device(pdev);
1024 return rc;
1025}
1026
1027
1028static int __init ahci_init(void)
1029{
1030 int rc;
1031
1032 rc = pci_module_init(&ahci_pci_driver);
1033 if (rc)
1034 return rc;
1035
1036 rc = scsi_register_module(MODULE_SCSI_HA, &ahci_sht);
1037 if (rc) {
1038 rc = -ENODEV;
1039 goto err_out;
1040 }
1041
1042 return 0;
1043
1044err_out:
1045 pci_unregister_driver(&ahci_pci_driver);
1046 return rc;
1047}
1048
1049
1050static void __exit ahci_exit(void)
1051{
1052 scsi_unregister_module(MODULE_SCSI_HA, &ahci_sht);
1053 pci_unregister_driver(&ahci_pci_driver);
1054}
1055
1056
1057MODULE_AUTHOR("Jeff Garzik");
1058MODULE_DESCRIPTION("AHCI SATA low-level driver");
1059MODULE_LICENSE("GPL");
1060MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
1061
1062module_init(ahci_init);
1063module_exit(ahci_exit);
1064