3 * acard-ahci.c - ACard AHCI SATA support
5 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
6 * Please ALWAYS copy linux-ide@vger.kernel.org
9 * Copyright 2010 Red Hat, Inc.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * AHCI hardware documentation:
31 * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
32 * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
36 #include <linux/kernel.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/init.h>
40 #include <linux/blkdev.h>
41 #include <linux/delay.h>
42 #include <linux/interrupt.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/device.h>
45 #include <linux/dmi.h>
46 #include <linux/gfp.h>
47 #include <scsi/scsi_host.h>
48 #include <scsi/scsi_cmnd.h>
49 #include <linux/libata.h>
52 #define DRV_NAME "acard-ahci"
53 #define DRV_VERSION "1.0"
56 Received FIS structure limited to 80h.
59 #define ACARD_AHCI_RX_FIS_SZ 128
73 __le32 size
; /* bit 31 (EOT) max==0x10000 (64k) */
76 static void acard_ahci_qc_prep(struct ata_queued_cmd
*qc
);
77 static bool acard_ahci_qc_fill_rtf(struct ata_queued_cmd
*qc
);
78 static int acard_ahci_port_start(struct ata_port
*ap
);
79 static int acard_ahci_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
);
82 static int acard_ahci_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t mesg
);
83 static int acard_ahci_pci_device_resume(struct pci_dev
*pdev
);
86 static struct scsi_host_template acard_ahci_sht
= {
87 AHCI_SHT("acard-ahci"),
90 static struct ata_port_operations acard_ops
= {
91 .inherits
= &ahci_ops
,
92 .qc_prep
= acard_ahci_qc_prep
,
93 .qc_fill_rtf
= acard_ahci_qc_fill_rtf
,
94 .port_start
= acard_ahci_port_start
,
97 #define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
99 static const struct ata_port_info acard_ahci_port_info
[] = {
102 AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ
),
103 .flags
= AHCI_FLAG_COMMON
,
104 .pio_mask
= ATA_PIO4
,
105 .udma_mask
= ATA_UDMA6
,
106 .port_ops
= &acard_ops
,
110 static const struct pci_device_id acard_ahci_pci_tbl
[] = {
112 { PCI_VDEVICE(ARTOP
, 0x000d), board_acard_ahci
}, /* ATP8620 */
114 { } /* terminate list */
117 static struct pci_driver acard_ahci_pci_driver
= {
119 .id_table
= acard_ahci_pci_tbl
,
120 .probe
= acard_ahci_init_one
,
121 .remove
= ata_pci_remove_one
,
123 .suspend
= acard_ahci_pci_device_suspend
,
124 .resume
= acard_ahci_pci_device_resume
,
129 static int acard_ahci_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
131 struct ata_host
*host
= dev_get_drvdata(&pdev
->dev
);
132 struct ahci_host_priv
*hpriv
= host
->private_data
;
133 void __iomem
*mmio
= hpriv
->mmio
;
136 if (mesg
.event
& PM_EVENT_SUSPEND
&&
137 hpriv
->flags
& AHCI_HFLAG_NO_SUSPEND
) {
138 dev_printk(KERN_ERR
, &pdev
->dev
,
139 "BIOS update required for suspend/resume\n");
143 if (mesg
.event
& PM_EVENT_SLEEP
) {
144 /* AHCI spec rev1.1 section 8.3.3:
145 * Software must disable interrupts prior to requesting a
146 * transition of the HBA to D3 state.
148 ctl
= readl(mmio
+ HOST_CTL
);
150 writel(ctl
, mmio
+ HOST_CTL
);
151 readl(mmio
+ HOST_CTL
); /* flush */
154 return ata_pci_device_suspend(pdev
, mesg
);
157 static int acard_ahci_pci_device_resume(struct pci_dev
*pdev
)
159 struct ata_host
*host
= dev_get_drvdata(&pdev
->dev
);
162 rc
= ata_pci_device_do_resume(pdev
);
166 if (pdev
->dev
.power
.power_state
.event
== PM_EVENT_SUSPEND
) {
167 rc
= ahci_reset_controller(host
);
171 ahci_init_controller(host
);
174 ata_host_resume(host
);
180 static int acard_ahci_configure_dma_masks(struct pci_dev
*pdev
, int using_dac
)
185 !pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
186 rc
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
188 rc
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
190 dev_printk(KERN_ERR
, &pdev
->dev
,
191 "64-bit DMA enable failed\n");
196 rc
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
198 dev_printk(KERN_ERR
, &pdev
->dev
,
199 "32-bit DMA enable failed\n");
202 rc
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
204 dev_printk(KERN_ERR
, &pdev
->dev
,
205 "32-bit consistent DMA enable failed\n");
212 static void acard_ahci_pci_print_info(struct ata_host
*host
)
214 struct pci_dev
*pdev
= to_pci_dev(host
->dev
);
218 pci_read_config_word(pdev
, 0x0a, &cc
);
219 if (cc
== PCI_CLASS_STORAGE_IDE
)
221 else if (cc
== PCI_CLASS_STORAGE_SATA
)
223 else if (cc
== PCI_CLASS_STORAGE_RAID
)
228 ahci_print_info(host
, scc_s
);
231 static unsigned int acard_ahci_fill_sg(struct ata_queued_cmd
*qc
, void *cmd_tbl
)
233 struct scatterlist
*sg
;
234 struct acard_sg
*acard_sg
= cmd_tbl
+ AHCI_CMD_TBL_HDR_SZ
;
235 unsigned int si
, last_si
= 0;
240 * Next, the S/G list.
242 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
) {
243 dma_addr_t addr
= sg_dma_address(sg
);
244 u32 sg_len
= sg_dma_len(sg
);
248 * We must set an end-of-table (EOT) bit,
249 * and the segment cannot exceed 64k (0x10000)
251 acard_sg
[si
].addr
= cpu_to_le32(addr
& 0xffffffff);
252 acard_sg
[si
].addr_hi
= cpu_to_le32((addr
>> 16) >> 16);
253 acard_sg
[si
].size
= cpu_to_le32(sg_len
);
257 acard_sg
[last_si
].size
|= cpu_to_le32(1 << 31); /* set EOT */
262 static void acard_ahci_qc_prep(struct ata_queued_cmd
*qc
)
264 struct ata_port
*ap
= qc
->ap
;
265 struct ahci_port_priv
*pp
= ap
->private_data
;
266 int is_atapi
= ata_is_atapi(qc
->tf
.protocol
);
269 const u32 cmd_fis_len
= 5; /* five dwords */
273 * Fill in command table information. First, the header,
274 * a SATA Register - Host to Device command FIS.
276 cmd_tbl
= pp
->cmd_tbl
+ qc
->tag
* AHCI_CMD_TBL_SZ
;
278 ata_tf_to_fis(&qc
->tf
, qc
->dev
->link
->pmp
, 1, cmd_tbl
);
280 memset(cmd_tbl
+ AHCI_CMD_TBL_CDB
, 0, 32);
281 memcpy(cmd_tbl
+ AHCI_CMD_TBL_CDB
, qc
->cdb
, qc
->dev
->cdb_len
);
285 if (qc
->flags
& ATA_QCFLAG_DMAMAP
)
286 n_elem
= acard_ahci_fill_sg(qc
, cmd_tbl
);
289 * Fill in command slot information.
291 * ACard note: prd table length not filled in
293 opts
= cmd_fis_len
| (qc
->dev
->link
->pmp
<< 12);
294 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
)
295 opts
|= AHCI_CMD_WRITE
;
297 opts
|= AHCI_CMD_ATAPI
| AHCI_CMD_PREFETCH
;
299 ahci_fill_cmd_slot(pp
, qc
->tag
, opts
);
302 static bool acard_ahci_qc_fill_rtf(struct ata_queued_cmd
*qc
)
304 struct ahci_port_priv
*pp
= qc
->ap
->private_data
;
305 u8
*rx_fis
= pp
->rx_fis
;
308 rx_fis
+= qc
->dev
->link
->pmp
* ACARD_AHCI_RX_FIS_SZ
;
311 * After a successful execution of an ATA PIO data-in command,
312 * the device doesn't send D2H Reg FIS to update the TF and
313 * the host should take TF and E_Status from the preceding PIO
316 if (qc
->tf
.protocol
== ATA_PROT_PIO
&& qc
->dma_dir
== DMA_FROM_DEVICE
&&
317 !(qc
->flags
& ATA_QCFLAG_FAILED
)) {
318 ata_tf_from_fis(rx_fis
+ RX_FIS_PIO_SETUP
, &qc
->result_tf
);
319 qc
->result_tf
.command
= (rx_fis
+ RX_FIS_PIO_SETUP
)[15];
321 ata_tf_from_fis(rx_fis
+ RX_FIS_D2H_REG
, &qc
->result_tf
);
326 static int acard_ahci_port_start(struct ata_port
*ap
)
328 struct ahci_host_priv
*hpriv
= ap
->host
->private_data
;
329 struct device
*dev
= ap
->host
->dev
;
330 struct ahci_port_priv
*pp
;
333 size_t dma_sz
, rx_fis_sz
;
335 pp
= devm_kzalloc(dev
, sizeof(*pp
), GFP_KERNEL
);
339 /* check FBS capability */
340 if ((hpriv
->cap
& HOST_CAP_FBS
) && sata_pmp_supported(ap
)) {
341 void __iomem
*port_mmio
= ahci_port_base(ap
);
342 u32 cmd
= readl(port_mmio
+ PORT_CMD
);
343 if (cmd
& PORT_CMD_FBSCP
)
344 pp
->fbs_supported
= true;
345 else if (hpriv
->flags
& AHCI_HFLAG_YES_FBS
) {
346 dev_printk(KERN_INFO
, dev
,
347 "port %d can do FBS, forcing FBSCP\n",
349 pp
->fbs_supported
= true;
351 dev_printk(KERN_WARNING
, dev
,
352 "port %d is not capable of FBS\n",
356 if (pp
->fbs_supported
) {
357 dma_sz
= AHCI_PORT_PRIV_FBS_DMA_SZ
;
358 rx_fis_sz
= ACARD_AHCI_RX_FIS_SZ
* 16;
360 dma_sz
= AHCI_PORT_PRIV_DMA_SZ
;
361 rx_fis_sz
= ACARD_AHCI_RX_FIS_SZ
;
364 mem
= dmam_alloc_coherent(dev
, dma_sz
, &mem_dma
, GFP_KERNEL
);
367 memset(mem
, 0, dma_sz
);
370 * First item in chunk of DMA memory: 32-slot command table,
371 * 32 bytes each in size
374 pp
->cmd_slot_dma
= mem_dma
;
376 mem
+= AHCI_CMD_SLOT_SZ
;
377 mem_dma
+= AHCI_CMD_SLOT_SZ
;
380 * Second item: Received-FIS area
383 pp
->rx_fis_dma
= mem_dma
;
386 mem_dma
+= rx_fis_sz
;
389 * Third item: data area for storing a single command
390 * and its scatter-gather table
393 pp
->cmd_tbl_dma
= mem_dma
;
396 * Save off initial list of interrupts to be enabled.
397 * This could be changed later
399 pp
->intr_mask
= DEF_PORT_IRQ
;
401 ap
->private_data
= pp
;
403 /* engage engines, captain */
404 return ahci_port_resume(ap
);
407 static int acard_ahci_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
409 static int printed_version
;
410 unsigned int board_id
= ent
->driver_data
;
411 struct ata_port_info pi
= acard_ahci_port_info
[board_id
];
412 const struct ata_port_info
*ppi
[] = { &pi
, NULL
};
413 struct device
*dev
= &pdev
->dev
;
414 struct ahci_host_priv
*hpriv
;
415 struct ata_host
*host
;
420 WARN_ON((int)ATA_MAX_QUEUE
> AHCI_MAX_CMDS
);
422 if (!printed_version
++)
423 dev_printk(KERN_DEBUG
, &pdev
->dev
, "version " DRV_VERSION
"\n");
425 /* acquire resources */
426 rc
= pcim_enable_device(pdev
);
430 /* AHCI controllers often implement SFF compatible interface.
431 * Grab all PCI BARs just in case.
433 rc
= pcim_iomap_regions_request_all(pdev
, 1 << AHCI_PCI_BAR
, DRV_NAME
);
435 pcim_pin_device(pdev
);
439 hpriv
= devm_kzalloc(dev
, sizeof(*hpriv
), GFP_KERNEL
);
442 hpriv
->flags
|= (unsigned long)pi
.private_data
;
444 if (!(hpriv
->flags
& AHCI_HFLAG_NO_MSI
))
445 pci_enable_msi(pdev
);
447 hpriv
->mmio
= pcim_iomap_table(pdev
)[AHCI_PCI_BAR
];
449 /* save initial config */
450 ahci_save_initial_config(&pdev
->dev
, hpriv
, 0, 0);
453 if (hpriv
->cap
& HOST_CAP_NCQ
)
454 pi
.flags
|= ATA_FLAG_NCQ
;
456 if (hpriv
->cap
& HOST_CAP_PMP
)
457 pi
.flags
|= ATA_FLAG_PMP
;
459 ahci_set_em_messages(hpriv
, &pi
);
461 /* CAP.NP sometimes indicate the index of the last enabled
462 * port, at other times, that of the last possible port, so
463 * determining the maximum port number requires looking at
464 * both CAP.NP and port_map.
466 n_ports
= max(ahci_nr_ports(hpriv
->cap
), fls(hpriv
->port_map
));
468 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, n_ports
);
471 host
->private_data
= hpriv
;
473 if (!(hpriv
->cap
& HOST_CAP_SSS
) || ahci_ignore_sss
)
474 host
->flags
|= ATA_HOST_PARALLEL_SCAN
;
476 printk(KERN_INFO
"ahci: SSS flag set, parallel bus scan disabled\n");
478 for (i
= 0; i
< host
->n_ports
; i
++) {
479 struct ata_port
*ap
= host
->ports
[i
];
481 ata_port_pbar_desc(ap
, AHCI_PCI_BAR
, -1, "abar");
482 ata_port_pbar_desc(ap
, AHCI_PCI_BAR
,
483 0x100 + ap
->port_no
* 0x80, "port");
485 /* set initial link pm policy */
487 ap->pm_policy = NOT_AVAILABLE;
489 /* disabled/not-implemented port */
490 if (!(hpriv
->port_map
& (1 << i
)))
491 ap
->ops
= &ata_dummy_port_ops
;
494 /* initialize adapter */
495 rc
= acard_ahci_configure_dma_masks(pdev
, hpriv
->cap
& HOST_CAP_64
);
499 rc
= ahci_reset_controller(host
);
503 ahci_init_controller(host
);
504 acard_ahci_pci_print_info(host
);
506 pci_set_master(pdev
);
507 return ata_host_activate(host
, pdev
->irq
, ahci_interrupt
, IRQF_SHARED
,
511 static int __init
acard_ahci_init(void)
513 return pci_register_driver(&acard_ahci_pci_driver
);
516 static void __exit
acard_ahci_exit(void)
518 pci_unregister_driver(&acard_ahci_pci_driver
);
521 MODULE_AUTHOR("Jeff Garzik");
522 MODULE_DESCRIPTION("ACard AHCI SATA low-level driver");
523 MODULE_LICENSE("GPL");
524 MODULE_DEVICE_TABLE(pci
, acard_ahci_pci_tbl
);
525 MODULE_VERSION(DRV_VERSION
);
527 module_init(acard_ahci_init
);
528 module_exit(acard_ahci_exit
);