acard-ahci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * acard-ahci.c - ACard AHCI SATA support
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2010 Red Hat, Inc.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. *
  26. * libata documentation is available via 'make {ps|pdf}docs',
  27. * as Documentation/DocBook/libata.*
  28. *
  29. * AHCI hardware documentation:
  30. * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
  31. * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/delay.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/device.h>
  43. #include <linux/dmi.h>
  44. #include <linux/gfp.h>
  45. #include <scsi/scsi_host.h>
  46. #include <scsi/scsi_cmnd.h>
  47. #include <linux/libata.h>
  48. #include "ahci.h"
  49. #define DRV_NAME "acard-ahci"
  50. #define DRV_VERSION "1.0"
  51. /*
  52. Received FIS structure limited to 80h.
  53. */
  54. #define ACARD_AHCI_RX_FIS_SZ 128
  55. enum {
  56. AHCI_PCI_BAR = 5,
  57. };
  58. enum board_ids {
  59. board_acard_ahci,
  60. };
  61. struct acard_sg {
  62. __le32 addr;
  63. __le32 addr_hi;
  64. __le32 reserved;
  65. __le32 size; /* bit 31 (EOT) max==0x10000 (64k) */
  66. };
  67. static void acard_ahci_qc_prep(struct ata_queued_cmd *qc);
  68. static bool acard_ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
  69. static int acard_ahci_port_start(struct ata_port *ap);
  70. static int acard_ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  71. #ifdef CONFIG_PM
  72. static int acard_ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  73. static int acard_ahci_pci_device_resume(struct pci_dev *pdev);
  74. #endif
  75. static struct scsi_host_template acard_ahci_sht = {
  76. AHCI_SHT("acard-ahci"),
  77. };
  78. static struct ata_port_operations acard_ops = {
  79. .inherits = &ahci_ops,
  80. .qc_prep = acard_ahci_qc_prep,
  81. .qc_fill_rtf = acard_ahci_qc_fill_rtf,
  82. .port_start = acard_ahci_port_start,
  83. };
  84. #define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
  85. static const struct ata_port_info acard_ahci_port_info[] = {
  86. [board_acard_ahci] =
  87. {
  88. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ),
  89. .flags = AHCI_FLAG_COMMON,
  90. .pio_mask = ATA_PIO4,
  91. .udma_mask = ATA_UDMA6,
  92. .port_ops = &acard_ops,
  93. },
  94. };
  95. static const struct pci_device_id acard_ahci_pci_tbl[] = {
  96. /* ACard */
  97. { PCI_VDEVICE(ARTOP, 0x000d), board_acard_ahci }, /* ATP8620 */
  98. { } /* terminate list */
  99. };
  100. static struct pci_driver acard_ahci_pci_driver = {
  101. .name = DRV_NAME,
  102. .id_table = acard_ahci_pci_tbl,
  103. .probe = acard_ahci_init_one,
  104. .remove = ata_pci_remove_one,
  105. #ifdef CONFIG_PM
  106. .suspend = acard_ahci_pci_device_suspend,
  107. .resume = acard_ahci_pci_device_resume,
  108. #endif
  109. };
  110. #ifdef CONFIG_PM
  111. static int acard_ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  112. {
  113. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  114. struct ahci_host_priv *hpriv = host->private_data;
  115. void __iomem *mmio = hpriv->mmio;
  116. u32 ctl;
  117. if (mesg.event & PM_EVENT_SUSPEND &&
  118. hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
  119. dev_printk(KERN_ERR, &pdev->dev,
  120. "BIOS update required for suspend/resume\n");
  121. return -EIO;
  122. }
  123. if (mesg.event & PM_EVENT_SLEEP) {
  124. /* AHCI spec rev1.1 section 8.3.3:
  125. * Software must disable interrupts prior to requesting a
  126. * transition of the HBA to D3 state.
  127. */
  128. ctl = readl(mmio + HOST_CTL);
  129. ctl &= ~HOST_IRQ_EN;
  130. writel(ctl, mmio + HOST_CTL);
  131. readl(mmio + HOST_CTL); /* flush */
  132. }
  133. return ata_pci_device_suspend(pdev, mesg);
  134. }
  135. static int acard_ahci_pci_device_resume(struct pci_dev *pdev)
  136. {
  137. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  138. int rc;
  139. rc = ata_pci_device_do_resume(pdev);
  140. if (rc)
  141. return rc;
  142. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  143. rc = ahci_reset_controller(host);
  144. if (rc)
  145. return rc;
  146. ahci_init_controller(host);
  147. }
  148. ata_host_resume(host);
  149. return 0;
  150. }
  151. #endif
  152. static int acard_ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
  153. {
  154. int rc;
  155. if (using_dac &&
  156. !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  157. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  158. if (rc) {
  159. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  160. if (rc) {
  161. dev_printk(KERN_ERR, &pdev->dev,
  162. "64-bit DMA enable failed\n");
  163. return rc;
  164. }
  165. }
  166. } else {
  167. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  168. if (rc) {
  169. dev_printk(KERN_ERR, &pdev->dev,
  170. "32-bit DMA enable failed\n");
  171. return rc;
  172. }
  173. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  174. if (rc) {
  175. dev_printk(KERN_ERR, &pdev->dev,
  176. "32-bit consistent DMA enable failed\n");
  177. return rc;
  178. }
  179. }
  180. return 0;
  181. }
  182. static void acard_ahci_pci_print_info(struct ata_host *host)
  183. {
  184. struct pci_dev *pdev = to_pci_dev(host->dev);
  185. u16 cc;
  186. const char *scc_s;
  187. pci_read_config_word(pdev, 0x0a, &cc);
  188. if (cc == PCI_CLASS_STORAGE_IDE)
  189. scc_s = "IDE";
  190. else if (cc == PCI_CLASS_STORAGE_SATA)
  191. scc_s = "SATA";
  192. else if (cc == PCI_CLASS_STORAGE_RAID)
  193. scc_s = "RAID";
  194. else
  195. scc_s = "unknown";
  196. ahci_print_info(host, scc_s);
  197. }
  198. static unsigned int acard_ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  199. {
  200. struct scatterlist *sg;
  201. struct acard_sg *acard_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  202. unsigned int si, last_si = 0;
  203. VPRINTK("ENTER\n");
  204. /*
  205. * Next, the S/G list.
  206. */
  207. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  208. dma_addr_t addr = sg_dma_address(sg);
  209. u32 sg_len = sg_dma_len(sg);
  210. /*
  211. * ACard note:
  212. * We must set an end-of-table (EOT) bit,
  213. * and the segment cannot exceed 64k (0x10000)
  214. */
  215. acard_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
  216. acard_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  217. acard_sg[si].size = cpu_to_le32(sg_len);
  218. last_si = si;
  219. }
  220. acard_sg[last_si].size |= cpu_to_le32(1 << 31); /* set EOT */
  221. return si;
  222. }
  223. static void acard_ahci_qc_prep(struct ata_queued_cmd *qc)
  224. {
  225. struct ata_port *ap = qc->ap;
  226. struct ahci_port_priv *pp = ap->private_data;
  227. int is_atapi = ata_is_atapi(qc->tf.protocol);
  228. void *cmd_tbl;
  229. u32 opts;
  230. const u32 cmd_fis_len = 5; /* five dwords */
  231. unsigned int n_elem;
  232. /*
  233. * Fill in command table information. First, the header,
  234. * a SATA Register - Host to Device command FIS.
  235. */
  236. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  237. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
  238. if (is_atapi) {
  239. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  240. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  241. }
  242. n_elem = 0;
  243. if (qc->flags & ATA_QCFLAG_DMAMAP)
  244. n_elem = acard_ahci_fill_sg(qc, cmd_tbl);
  245. /*
  246. * Fill in command slot information.
  247. *
  248. * ACard note: prd table length not filled in
  249. */
  250. opts = cmd_fis_len | (qc->dev->link->pmp << 12);
  251. if (qc->tf.flags & ATA_TFLAG_WRITE)
  252. opts |= AHCI_CMD_WRITE;
  253. if (is_atapi)
  254. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  255. ahci_fill_cmd_slot(pp, qc->tag, opts);
  256. }
  257. static bool acard_ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
  258. {
  259. struct ahci_port_priv *pp = qc->ap->private_data;
  260. u8 *rx_fis = pp->rx_fis;
  261. if (pp->fbs_enabled)
  262. rx_fis += qc->dev->link->pmp * ACARD_AHCI_RX_FIS_SZ;
  263. /*
  264. * After a successful execution of an ATA PIO data-in command,
  265. * the device doesn't send D2H Reg FIS to update the TF and
  266. * the host should take TF and E_Status from the preceding PIO
  267. * Setup FIS.
  268. */
  269. if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
  270. !(qc->flags & ATA_QCFLAG_FAILED)) {
  271. ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
  272. qc->result_tf.command = (rx_fis + RX_FIS_PIO_SETUP)[15];
  273. } else
  274. ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
  275. return true;
  276. }
  277. static int acard_ahci_port_start(struct ata_port *ap)
  278. {
  279. struct ahci_host_priv *hpriv = ap->host->private_data;
  280. struct device *dev = ap->host->dev;
  281. struct ahci_port_priv *pp;
  282. void *mem;
  283. dma_addr_t mem_dma;
  284. size_t dma_sz, rx_fis_sz;
  285. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  286. if (!pp)
  287. return -ENOMEM;
  288. /* check FBS capability */
  289. if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
  290. void __iomem *port_mmio = ahci_port_base(ap);
  291. u32 cmd = readl(port_mmio + PORT_CMD);
  292. if (cmd & PORT_CMD_FBSCP)
  293. pp->fbs_supported = true;
  294. else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
  295. dev_printk(KERN_INFO, dev,
  296. "port %d can do FBS, forcing FBSCP\n",
  297. ap->port_no);
  298. pp->fbs_supported = true;
  299. } else
  300. dev_printk(KERN_WARNING, dev,
  301. "port %d is not capable of FBS\n",
  302. ap->port_no);
  303. }
  304. if (pp->fbs_supported) {
  305. dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
  306. rx_fis_sz = ACARD_AHCI_RX_FIS_SZ * 16;
  307. } else {
  308. dma_sz = AHCI_PORT_PRIV_DMA_SZ;
  309. rx_fis_sz = ACARD_AHCI_RX_FIS_SZ;
  310. }
  311. mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
  312. if (!mem)
  313. return -ENOMEM;
  314. memset(mem, 0, dma_sz);
  315. /*
  316. * First item in chunk of DMA memory: 32-slot command table,
  317. * 32 bytes each in size
  318. */
  319. pp->cmd_slot = mem;
  320. pp->cmd_slot_dma = mem_dma;
  321. mem += AHCI_CMD_SLOT_SZ;
  322. mem_dma += AHCI_CMD_SLOT_SZ;
  323. /*
  324. * Second item: Received-FIS area
  325. */
  326. pp->rx_fis = mem;
  327. pp->rx_fis_dma = mem_dma;
  328. mem += rx_fis_sz;
  329. mem_dma += rx_fis_sz;
  330. /*
  331. * Third item: data area for storing a single command
  332. * and its scatter-gather table
  333. */
  334. pp->cmd_tbl = mem;
  335. pp->cmd_tbl_dma = mem_dma;
  336. /*
  337. * Save off initial list of interrupts to be enabled.
  338. * This could be changed later
  339. */
  340. pp->intr_mask = DEF_PORT_IRQ;
  341. ap->private_data = pp;
  342. /* engage engines, captain */
  343. return ahci_port_resume(ap);
  344. }
  345. static int acard_ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  346. {
  347. static int printed_version;
  348. unsigned int board_id = ent->driver_data;
  349. struct ata_port_info pi = acard_ahci_port_info[board_id];
  350. const struct ata_port_info *ppi[] = { &pi, NULL };
  351. struct device *dev = &pdev->dev;
  352. struct ahci_host_priv *hpriv;
  353. struct ata_host *host;
  354. int n_ports, i, rc;
  355. VPRINTK("ENTER\n");
  356. WARN_ON((int)ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  357. if (!printed_version++)
  358. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  359. /* acquire resources */
  360. rc = pcim_enable_device(pdev);
  361. if (rc)
  362. return rc;
  363. /* AHCI controllers often implement SFF compatible interface.
  364. * Grab all PCI BARs just in case.
  365. */
  366. rc = pcim_iomap_regions_request_all(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
  367. if (rc == -EBUSY)
  368. pcim_pin_device(pdev);
  369. if (rc)
  370. return rc;
  371. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  372. if (!hpriv)
  373. return -ENOMEM;
  374. hpriv->flags |= (unsigned long)pi.private_data;
  375. if (!(hpriv->flags & AHCI_HFLAG_NO_MSI))
  376. pci_enable_msi(pdev);
  377. hpriv->mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
  378. /* save initial config */
  379. ahci_save_initial_config(&pdev->dev, hpriv, 0, 0);
  380. /* prepare host */
  381. if (hpriv->cap & HOST_CAP_NCQ)
  382. pi.flags |= ATA_FLAG_NCQ;
  383. if (hpriv->cap & HOST_CAP_PMP)
  384. pi.flags |= ATA_FLAG_PMP;
  385. ahci_set_em_messages(hpriv, &pi);
  386. /* CAP.NP sometimes indicate the index of the last enabled
  387. * port, at other times, that of the last possible port, so
  388. * determining the maximum port number requires looking at
  389. * both CAP.NP and port_map.
  390. */
  391. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  392. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  393. if (!host)
  394. return -ENOMEM;
  395. host->private_data = hpriv;
  396. if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
  397. host->flags |= ATA_HOST_PARALLEL_SCAN;
  398. else
  399. printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
  400. for (i = 0; i < host->n_ports; i++) {
  401. struct ata_port *ap = host->ports[i];
  402. ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
  403. ata_port_pbar_desc(ap, AHCI_PCI_BAR,
  404. 0x100 + ap->port_no * 0x80, "port");
  405. /* set initial link pm policy */
  406. /*
  407. ap->pm_policy = NOT_AVAILABLE;
  408. */
  409. /* disabled/not-implemented port */
  410. if (!(hpriv->port_map & (1 << i)))
  411. ap->ops = &ata_dummy_port_ops;
  412. }
  413. /* initialize adapter */
  414. rc = acard_ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
  415. if (rc)
  416. return rc;
  417. rc = ahci_reset_controller(host);
  418. if (rc)
  419. return rc;
  420. ahci_init_controller(host);
  421. acard_ahci_pci_print_info(host);
  422. pci_set_master(pdev);
  423. return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
  424. &acard_ahci_sht);
  425. }
  426. static int __init acard_ahci_init(void)
  427. {
  428. return pci_register_driver(&acard_ahci_pci_driver);
  429. }
  430. static void __exit acard_ahci_exit(void)
  431. {
  432. pci_unregister_driver(&acard_ahci_pci_driver);
  433. }
  434. MODULE_AUTHOR("Jeff Garzik");
  435. MODULE_DESCRIPTION("ACard AHCI SATA low-level driver");
  436. MODULE_LICENSE("GPL");
  437. MODULE_DEVICE_TABLE(pci, acard_ahci_pci_tbl);
  438. MODULE_VERSION(DRV_VERSION);
  439. module_init(acard_ahci_init);
  440. module_exit(acard_ahci_exit);