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) {
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));
191
"64-bit DMA enable failed\n");
196
rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
198
dev_err(&pdev->dev, "32-bit DMA enable failed\n");
201
rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
204
"32-bit consistent DMA enable failed\n");
211
static void acard_ahci_pci_print_info(struct ata_host *host)
213
struct pci_dev *pdev = to_pci_dev(host->dev);
217
pci_read_config_word(pdev, 0x0a, &cc);
218
if (cc == PCI_CLASS_STORAGE_IDE)
220
else if (cc == PCI_CLASS_STORAGE_SATA)
222
else if (cc == PCI_CLASS_STORAGE_RAID)
227
ahci_print_info(host, scc_s);
230
static unsigned int acard_ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
232
struct scatterlist *sg;
233
struct acard_sg *acard_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
234
unsigned int si, last_si = 0;
239
* Next, the S/G list.
241
for_each_sg(qc->sg, sg, qc->n_elem, si) {
242
dma_addr_t addr = sg_dma_address(sg);
243
u32 sg_len = sg_dma_len(sg);
247
* We must set an end-of-table (EOT) bit,
248
* and the segment cannot exceed 64k (0x10000)
250
acard_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
251
acard_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
252
acard_sg[si].size = cpu_to_le32(sg_len);
256
acard_sg[last_si].size |= cpu_to_le32(1 << 31); /* set EOT */
261
static void acard_ahci_qc_prep(struct ata_queued_cmd *qc)
263
struct ata_port *ap = qc->ap;
264
struct ahci_port_priv *pp = ap->private_data;
265
int is_atapi = ata_is_atapi(qc->tf.protocol);
268
const u32 cmd_fis_len = 5; /* five dwords */
272
* Fill in command table information. First, the header,
273
* a SATA Register - Host to Device command FIS.
275
cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
277
ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
279
memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
280
memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
284
if (qc->flags & ATA_QCFLAG_DMAMAP)
285
n_elem = acard_ahci_fill_sg(qc, cmd_tbl);
288
* Fill in command slot information.
290
* ACard note: prd table length not filled in
292
opts = cmd_fis_len | (qc->dev->link->pmp << 12);
293
if (qc->tf.flags & ATA_TFLAG_WRITE)
294
opts |= AHCI_CMD_WRITE;
296
opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
298
ahci_fill_cmd_slot(pp, qc->tag, opts);
301
static bool acard_ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
303
struct ahci_port_priv *pp = qc->ap->private_data;
304
u8 *rx_fis = pp->rx_fis;
307
rx_fis += qc->dev->link->pmp * ACARD_AHCI_RX_FIS_SZ;
310
* After a successful execution of an ATA PIO data-in command,
311
* the device doesn't send D2H Reg FIS to update the TF and
312
* the host should take TF and E_Status from the preceding PIO
315
if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
316
!(qc->flags & ATA_QCFLAG_FAILED)) {
317
ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
318
qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
320
ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
325
static int acard_ahci_port_start(struct ata_port *ap)
327
struct ahci_host_priv *hpriv = ap->host->private_data;
328
struct device *dev = ap->host->dev;
329
struct ahci_port_priv *pp;
332
size_t dma_sz, rx_fis_sz;
334
pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
338
/* check FBS capability */
339
if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
340
void __iomem *port_mmio = ahci_port_base(ap);
341
u32 cmd = readl(port_mmio + PORT_CMD);
342
if (cmd & PORT_CMD_FBSCP)
343
pp->fbs_supported = true;
344
else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
345
dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
347
pp->fbs_supported = true;
349
dev_warn(dev, "port %d is not capable of FBS\n",
353
if (pp->fbs_supported) {
354
dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
355
rx_fis_sz = ACARD_AHCI_RX_FIS_SZ * 16;
357
dma_sz = AHCI_PORT_PRIV_DMA_SZ;
358
rx_fis_sz = ACARD_AHCI_RX_FIS_SZ;
361
mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
364
memset(mem, 0, dma_sz);
367
* First item in chunk of DMA memory: 32-slot command table,
368
* 32 bytes each in size
371
pp->cmd_slot_dma = mem_dma;
373
mem += AHCI_CMD_SLOT_SZ;
374
mem_dma += AHCI_CMD_SLOT_SZ;
377
* Second item: Received-FIS area
380
pp->rx_fis_dma = mem_dma;
383
mem_dma += rx_fis_sz;
386
* Third item: data area for storing a single command
387
* and its scatter-gather table
390
pp->cmd_tbl_dma = mem_dma;
393
* Save off initial list of interrupts to be enabled.
394
* This could be changed later
396
pp->intr_mask = DEF_PORT_IRQ;
398
ap->private_data = pp;
400
/* engage engines, captain */
401
return ahci_port_resume(ap);
404
static int acard_ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
406
unsigned int board_id = ent->driver_data;
407
struct ata_port_info pi = acard_ahci_port_info[board_id];
408
const struct ata_port_info *ppi[] = { &pi, NULL };
409
struct device *dev = &pdev->dev;
410
struct ahci_host_priv *hpriv;
411
struct ata_host *host;
416
WARN_ON((int)ATA_MAX_QUEUE > AHCI_MAX_CMDS);
418
ata_print_version_once(&pdev->dev, DRV_VERSION);
420
/* acquire resources */
421
rc = pcim_enable_device(pdev);
425
/* AHCI controllers often implement SFF compatible interface.
426
* Grab all PCI BARs just in case.
428
rc = pcim_iomap_regions_request_all(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
430
pcim_pin_device(pdev);
434
hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
437
hpriv->flags |= (unsigned long)pi.private_data;
439
if (!(hpriv->flags & AHCI_HFLAG_NO_MSI))
440
pci_enable_msi(pdev);
442
hpriv->mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
444
/* save initial config */
445
ahci_save_initial_config(&pdev->dev, hpriv, 0, 0);
448
if (hpriv->cap & HOST_CAP_NCQ)
449
pi.flags |= ATA_FLAG_NCQ;
451
if (hpriv->cap & HOST_CAP_PMP)
452
pi.flags |= ATA_FLAG_PMP;
454
ahci_set_em_messages(hpriv, &pi);
456
/* CAP.NP sometimes indicate the index of the last enabled
457
* port, at other times, that of the last possible port, so
458
* determining the maximum port number requires looking at
459
* both CAP.NP and port_map.
461
n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
463
host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
466
host->private_data = hpriv;
468
if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
469
host->flags |= ATA_HOST_PARALLEL_SCAN;
471
printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
473
for (i = 0; i < host->n_ports; i++) {
474
struct ata_port *ap = host->ports[i];
476
ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
477
ata_port_pbar_desc(ap, AHCI_PCI_BAR,
478
0x100 + ap->port_no * 0x80, "port");
480
/* set initial link pm policy */
482
ap->pm_policy = NOT_AVAILABLE;
484
/* disabled/not-implemented port */
485
if (!(hpriv->port_map & (1 << i)))
486
ap->ops = &ata_dummy_port_ops;
489
/* initialize adapter */
490
rc = acard_ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
494
rc = ahci_reset_controller(host);
498
ahci_init_controller(host);
499
acard_ahci_pci_print_info(host);
501
pci_set_master(pdev);
502
return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
506
static int __init acard_ahci_init(void)
508
return pci_register_driver(&acard_ahci_pci_driver);
511
static void __exit acard_ahci_exit(void)
513
pci_unregister_driver(&acard_ahci_pci_driver);
516
MODULE_AUTHOR("Jeff Garzik");
517
MODULE_DESCRIPTION("ACard AHCI SATA low-level driver");
518
MODULE_LICENSE("GPL");
519
MODULE_DEVICE_TABLE(pci, acard_ahci_pci_tbl);
520
MODULE_VERSION(DRV_VERSION);
522
module_init(acard_ahci_init);
523
module_exit(acard_ahci_exit);