ahci.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. /*
  2. * ahci.c - 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 2004-2005 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/sched.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/device.h>
  44. #include <scsi/scsi_host.h>
  45. #include <scsi/scsi_cmnd.h>
  46. #include <linux/libata.h>
  47. #include <asm/io.h>
  48. #define DRV_NAME "ahci"
  49. #define DRV_VERSION "1.3"
  50. enum {
  51. AHCI_PCI_BAR = 5,
  52. AHCI_MAX_SG = 168, /* hardware max is 64K */
  53. AHCI_DMA_BOUNDARY = 0xffffffff,
  54. AHCI_USE_CLUSTERING = 0,
  55. AHCI_MAX_CMDS = 32,
  56. AHCI_CMD_SZ = 32,
  57. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  58. AHCI_RX_FIS_SZ = 256,
  59. AHCI_CMD_TBL_CDB = 0x40,
  60. AHCI_CMD_TBL_HDR_SZ = 0x80,
  61. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  62. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  63. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  64. AHCI_RX_FIS_SZ,
  65. AHCI_IRQ_ON_SG = (1 << 31),
  66. AHCI_CMD_ATAPI = (1 << 5),
  67. AHCI_CMD_WRITE = (1 << 6),
  68. AHCI_CMD_PREFETCH = (1 << 7),
  69. AHCI_CMD_RESET = (1 << 8),
  70. AHCI_CMD_CLR_BUSY = (1 << 10),
  71. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  72. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  73. board_ahci = 0,
  74. board_ahci_vt8251 = 1,
  75. /* global controller registers */
  76. HOST_CAP = 0x00, /* host capabilities */
  77. HOST_CTL = 0x04, /* global host control */
  78. HOST_IRQ_STAT = 0x08, /* interrupt status */
  79. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  80. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  81. /* HOST_CTL bits */
  82. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  83. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  84. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  85. /* HOST_CAP bits */
  86. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  87. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  88. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  89. /* registers for each SATA port */
  90. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  91. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  92. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  93. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  94. PORT_IRQ_STAT = 0x10, /* interrupt status */
  95. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  96. PORT_CMD = 0x18, /* port command */
  97. PORT_TFDATA = 0x20, /* taskfile data */
  98. PORT_SIG = 0x24, /* device TF signature */
  99. PORT_CMD_ISSUE = 0x38, /* command issue */
  100. PORT_SCR = 0x28, /* SATA phy register block */
  101. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  102. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  103. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  104. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  105. /* PORT_IRQ_{STAT,MASK} bits */
  106. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  107. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  108. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  109. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  110. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  111. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  112. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  113. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  114. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  115. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  116. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  117. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  118. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  119. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  120. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  121. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  122. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  123. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  124. PORT_IRQ_IF_ERR |
  125. PORT_IRQ_CONNECT |
  126. PORT_IRQ_UNK_FIS,
  127. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  128. PORT_IRQ_TF_ERR |
  129. PORT_IRQ_HBUS_DATA_ERR,
  130. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  131. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  132. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  133. /* PORT_CMD bits */
  134. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  135. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  136. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  137. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  138. PORT_CMD_CLO = (1 << 3), /* Command list override */
  139. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  140. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  141. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  142. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  143. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  144. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  145. /* hpriv->flags bits */
  146. AHCI_FLAG_MSI = (1 << 0),
  147. /* ap->flags bits */
  148. AHCI_FLAG_RESET_NEEDS_CLO = (1 << 24),
  149. };
  150. struct ahci_cmd_hdr {
  151. u32 opts;
  152. u32 status;
  153. u32 tbl_addr;
  154. u32 tbl_addr_hi;
  155. u32 reserved[4];
  156. };
  157. struct ahci_sg {
  158. u32 addr;
  159. u32 addr_hi;
  160. u32 reserved;
  161. u32 flags_size;
  162. };
  163. struct ahci_host_priv {
  164. unsigned long flags;
  165. u32 cap; /* cache of HOST_CAP register */
  166. u32 port_map; /* cache of HOST_PORTS_IMPL reg */
  167. };
  168. struct ahci_port_priv {
  169. struct ahci_cmd_hdr *cmd_slot;
  170. dma_addr_t cmd_slot_dma;
  171. void *cmd_tbl;
  172. dma_addr_t cmd_tbl_dma;
  173. void *rx_fis;
  174. dma_addr_t rx_fis_dma;
  175. };
  176. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
  177. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  178. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  179. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  180. static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
  181. static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes);
  182. static void ahci_irq_clear(struct ata_port *ap);
  183. static int ahci_port_start(struct ata_port *ap);
  184. static void ahci_port_stop(struct ata_port *ap);
  185. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  186. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  187. static u8 ahci_check_status(struct ata_port *ap);
  188. static void ahci_freeze(struct ata_port *ap);
  189. static void ahci_thaw(struct ata_port *ap);
  190. static void ahci_error_handler(struct ata_port *ap);
  191. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  192. static void ahci_remove_one (struct pci_dev *pdev);
  193. static struct scsi_host_template ahci_sht = {
  194. .module = THIS_MODULE,
  195. .name = DRV_NAME,
  196. .ioctl = ata_scsi_ioctl,
  197. .queuecommand = ata_scsi_queuecmd,
  198. .change_queue_depth = ata_scsi_change_queue_depth,
  199. .can_queue = AHCI_MAX_CMDS - 1,
  200. .this_id = ATA_SHT_THIS_ID,
  201. .sg_tablesize = AHCI_MAX_SG,
  202. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  203. .emulated = ATA_SHT_EMULATED,
  204. .use_clustering = AHCI_USE_CLUSTERING,
  205. .proc_name = DRV_NAME,
  206. .dma_boundary = AHCI_DMA_BOUNDARY,
  207. .slave_configure = ata_scsi_slave_config,
  208. .bios_param = ata_std_bios_param,
  209. };
  210. static const struct ata_port_operations ahci_ops = {
  211. .port_disable = ata_port_disable,
  212. .check_status = ahci_check_status,
  213. .check_altstatus = ahci_check_status,
  214. .dev_select = ata_noop_dev_select,
  215. .tf_read = ahci_tf_read,
  216. .probe_reset = ahci_probe_reset,
  217. .qc_prep = ahci_qc_prep,
  218. .qc_issue = ahci_qc_issue,
  219. .irq_handler = ahci_interrupt,
  220. .irq_clear = ahci_irq_clear,
  221. .scr_read = ahci_scr_read,
  222. .scr_write = ahci_scr_write,
  223. .freeze = ahci_freeze,
  224. .thaw = ahci_thaw,
  225. .error_handler = ahci_error_handler,
  226. .post_internal_cmd = ahci_post_internal_cmd,
  227. .port_start = ahci_port_start,
  228. .port_stop = ahci_port_stop,
  229. };
  230. static const struct ata_port_info ahci_port_info[] = {
  231. /* board_ahci */
  232. {
  233. .sht = &ahci_sht,
  234. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  235. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
  236. .pio_mask = 0x1f, /* pio0-4 */
  237. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  238. .port_ops = &ahci_ops,
  239. },
  240. /* board_ahci_vt8251 */
  241. {
  242. .sht = &ahci_sht,
  243. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  244. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  245. AHCI_FLAG_RESET_NEEDS_CLO,
  246. .pio_mask = 0x1f, /* pio0-4 */
  247. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  248. .port_ops = &ahci_ops,
  249. },
  250. };
  251. static const struct pci_device_id ahci_pci_tbl[] = {
  252. { PCI_VENDOR_ID_INTEL, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  253. board_ahci }, /* ICH6 */
  254. { PCI_VENDOR_ID_INTEL, 0x2653, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  255. board_ahci }, /* ICH6M */
  256. { PCI_VENDOR_ID_INTEL, 0x27c1, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  257. board_ahci }, /* ICH7 */
  258. { PCI_VENDOR_ID_INTEL, 0x27c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  259. board_ahci }, /* ICH7M */
  260. { PCI_VENDOR_ID_INTEL, 0x27c3, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  261. board_ahci }, /* ICH7R */
  262. { PCI_VENDOR_ID_AL, 0x5288, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  263. board_ahci }, /* ULi M5288 */
  264. { PCI_VENDOR_ID_INTEL, 0x2681, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  265. board_ahci }, /* ESB2 */
  266. { PCI_VENDOR_ID_INTEL, 0x2682, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  267. board_ahci }, /* ESB2 */
  268. { PCI_VENDOR_ID_INTEL, 0x2683, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  269. board_ahci }, /* ESB2 */
  270. { PCI_VENDOR_ID_INTEL, 0x27c6, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  271. board_ahci }, /* ICH7-M DH */
  272. { PCI_VENDOR_ID_INTEL, 0x2821, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  273. board_ahci }, /* ICH8 */
  274. { PCI_VENDOR_ID_INTEL, 0x2822, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  275. board_ahci }, /* ICH8 */
  276. { PCI_VENDOR_ID_INTEL, 0x2824, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  277. board_ahci }, /* ICH8 */
  278. { PCI_VENDOR_ID_INTEL, 0x2829, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  279. board_ahci }, /* ICH8M */
  280. { PCI_VENDOR_ID_INTEL, 0x282a, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  281. board_ahci }, /* ICH8M */
  282. { 0x197b, 0x2360, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  283. board_ahci }, /* JMicron JMB360 */
  284. { 0x197b, 0x2363, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  285. board_ahci }, /* JMicron JMB363 */
  286. { PCI_VENDOR_ID_ATI, 0x4380, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  287. board_ahci }, /* ATI SB600 non-raid */
  288. { PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  289. board_ahci }, /* ATI SB600 raid */
  290. { PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  291. board_ahci_vt8251 }, /* VIA VT8251 */
  292. { } /* terminate list */
  293. };
  294. static struct pci_driver ahci_pci_driver = {
  295. .name = DRV_NAME,
  296. .id_table = ahci_pci_tbl,
  297. .probe = ahci_init_one,
  298. .remove = ahci_remove_one,
  299. };
  300. static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
  301. {
  302. return base + 0x100 + (port * 0x80);
  303. }
  304. static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int port)
  305. {
  306. return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
  307. }
  308. static int ahci_port_start(struct ata_port *ap)
  309. {
  310. struct device *dev = ap->host_set->dev;
  311. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  312. struct ahci_port_priv *pp;
  313. void __iomem *mmio = ap->host_set->mmio_base;
  314. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  315. void *mem;
  316. dma_addr_t mem_dma;
  317. int rc;
  318. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  319. if (!pp)
  320. return -ENOMEM;
  321. memset(pp, 0, sizeof(*pp));
  322. rc = ata_pad_alloc(ap, dev);
  323. if (rc) {
  324. kfree(pp);
  325. return rc;
  326. }
  327. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  328. if (!mem) {
  329. ata_pad_free(ap, dev);
  330. kfree(pp);
  331. return -ENOMEM;
  332. }
  333. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  334. /*
  335. * First item in chunk of DMA memory: 32-slot command table,
  336. * 32 bytes each in size
  337. */
  338. pp->cmd_slot = mem;
  339. pp->cmd_slot_dma = mem_dma;
  340. mem += AHCI_CMD_SLOT_SZ;
  341. mem_dma += AHCI_CMD_SLOT_SZ;
  342. /*
  343. * Second item: Received-FIS area
  344. */
  345. pp->rx_fis = mem;
  346. pp->rx_fis_dma = mem_dma;
  347. mem += AHCI_RX_FIS_SZ;
  348. mem_dma += AHCI_RX_FIS_SZ;
  349. /*
  350. * Third item: data area for storing a single command
  351. * and its scatter-gather table
  352. */
  353. pp->cmd_tbl = mem;
  354. pp->cmd_tbl_dma = mem_dma;
  355. ap->private_data = pp;
  356. if (hpriv->cap & HOST_CAP_64)
  357. writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  358. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  359. readl(port_mmio + PORT_LST_ADDR); /* flush */
  360. if (hpriv->cap & HOST_CAP_64)
  361. writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  362. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  363. readl(port_mmio + PORT_FIS_ADDR); /* flush */
  364. writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  365. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  366. PORT_CMD_START, port_mmio + PORT_CMD);
  367. readl(port_mmio + PORT_CMD); /* flush */
  368. return 0;
  369. }
  370. static 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 __iomem *mmio = ap->host_set->mmio_base;
  375. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  376. u32 tmp;
  377. tmp = readl(port_mmio + PORT_CMD);
  378. tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX);
  379. writel(tmp, port_mmio + PORT_CMD);
  380. readl(port_mmio + PORT_CMD); /* flush */
  381. /* spec says 500 msecs for each PORT_CMD_{START,FIS_RX} bit, so
  382. * this is slightly incorrect.
  383. */
  384. msleep(500);
  385. ap->private_data = NULL;
  386. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  387. pp->cmd_slot, pp->cmd_slot_dma);
  388. ata_pad_free(ap, dev);
  389. kfree(pp);
  390. }
  391. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  392. {
  393. unsigned int sc_reg;
  394. switch (sc_reg_in) {
  395. case SCR_STATUS: sc_reg = 0; break;
  396. case SCR_CONTROL: sc_reg = 1; break;
  397. case SCR_ERROR: sc_reg = 2; break;
  398. case SCR_ACTIVE: sc_reg = 3; break;
  399. default:
  400. return 0xffffffffU;
  401. }
  402. return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  403. }
  404. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  405. u32 val)
  406. {
  407. unsigned int sc_reg;
  408. switch (sc_reg_in) {
  409. case SCR_STATUS: sc_reg = 0; break;
  410. case SCR_CONTROL: sc_reg = 1; break;
  411. case SCR_ERROR: sc_reg = 2; break;
  412. case SCR_ACTIVE: sc_reg = 3; break;
  413. default:
  414. return;
  415. }
  416. writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  417. }
  418. static int ahci_stop_engine(struct ata_port *ap)
  419. {
  420. void __iomem *mmio = ap->host_set->mmio_base;
  421. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  422. int work;
  423. u32 tmp;
  424. tmp = readl(port_mmio + PORT_CMD);
  425. tmp &= ~PORT_CMD_START;
  426. writel(tmp, port_mmio + PORT_CMD);
  427. /* wait for engine to stop. TODO: this could be
  428. * as long as 500 msec
  429. */
  430. work = 1000;
  431. while (work-- > 0) {
  432. tmp = readl(port_mmio + PORT_CMD);
  433. if ((tmp & PORT_CMD_LIST_ON) == 0)
  434. return 0;
  435. udelay(10);
  436. }
  437. return -EIO;
  438. }
  439. static void ahci_start_engine(struct ata_port *ap)
  440. {
  441. void __iomem *mmio = ap->host_set->mmio_base;
  442. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  443. u32 tmp;
  444. tmp = readl(port_mmio + PORT_CMD);
  445. tmp |= PORT_CMD_START;
  446. writel(tmp, port_mmio + PORT_CMD);
  447. readl(port_mmio + PORT_CMD); /* flush */
  448. }
  449. static unsigned int ahci_dev_classify(struct ata_port *ap)
  450. {
  451. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  452. struct ata_taskfile tf;
  453. u32 tmp;
  454. tmp = readl(port_mmio + PORT_SIG);
  455. tf.lbah = (tmp >> 24) & 0xff;
  456. tf.lbam = (tmp >> 16) & 0xff;
  457. tf.lbal = (tmp >> 8) & 0xff;
  458. tf.nsect = (tmp) & 0xff;
  459. return ata_dev_classify(&tf);
  460. }
  461. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  462. u32 opts)
  463. {
  464. dma_addr_t cmd_tbl_dma;
  465. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  466. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  467. pp->cmd_slot[tag].status = 0;
  468. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  469. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  470. }
  471. static int ahci_clo(struct ata_port *ap)
  472. {
  473. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  474. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  475. u32 tmp;
  476. if (!(hpriv->cap & HOST_CAP_CLO))
  477. return -EOPNOTSUPP;
  478. tmp = readl(port_mmio + PORT_CMD);
  479. tmp |= PORT_CMD_CLO;
  480. writel(tmp, port_mmio + PORT_CMD);
  481. tmp = ata_wait_register(port_mmio + PORT_CMD,
  482. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  483. if (tmp & PORT_CMD_CLO)
  484. return -EIO;
  485. return 0;
  486. }
  487. static int ahci_softreset(struct ata_port *ap, unsigned int *class)
  488. {
  489. struct ahci_port_priv *pp = ap->private_data;
  490. void __iomem *mmio = ap->host_set->mmio_base;
  491. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  492. const u32 cmd_fis_len = 5; /* five dwords */
  493. const char *reason = NULL;
  494. struct ata_taskfile tf;
  495. u32 tmp;
  496. u8 *fis;
  497. int rc;
  498. DPRINTK("ENTER\n");
  499. if (ata_port_offline(ap)) {
  500. DPRINTK("PHY reports no device\n");
  501. *class = ATA_DEV_NONE;
  502. return 0;
  503. }
  504. /* prepare for SRST (AHCI-1.1 10.4.1) */
  505. rc = ahci_stop_engine(ap);
  506. if (rc) {
  507. reason = "failed to stop engine";
  508. goto fail_restart;
  509. }
  510. /* check BUSY/DRQ, perform Command List Override if necessary */
  511. ahci_tf_read(ap, &tf);
  512. if (tf.command & (ATA_BUSY | ATA_DRQ)) {
  513. rc = ahci_clo(ap);
  514. if (rc == -EOPNOTSUPP) {
  515. reason = "port busy but CLO unavailable";
  516. goto fail_restart;
  517. } else if (rc) {
  518. reason = "port busy but CLO failed";
  519. goto fail_restart;
  520. }
  521. }
  522. /* restart engine */
  523. ahci_start_engine(ap);
  524. ata_tf_init(ap->device, &tf);
  525. fis = pp->cmd_tbl;
  526. /* issue the first D2H Register FIS */
  527. ahci_fill_cmd_slot(pp, 0,
  528. cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
  529. tf.ctl |= ATA_SRST;
  530. ata_tf_to_fis(&tf, fis, 0);
  531. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  532. writel(1, port_mmio + PORT_CMD_ISSUE);
  533. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1, 1, 500);
  534. if (tmp & 0x1) {
  535. rc = -EIO;
  536. reason = "1st FIS failed";
  537. goto fail;
  538. }
  539. /* spec says at least 5us, but be generous and sleep for 1ms */
  540. msleep(1);
  541. /* issue the second D2H Register FIS */
  542. ahci_fill_cmd_slot(pp, 0, cmd_fis_len);
  543. tf.ctl &= ~ATA_SRST;
  544. ata_tf_to_fis(&tf, fis, 0);
  545. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  546. writel(1, port_mmio + PORT_CMD_ISSUE);
  547. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  548. /* spec mandates ">= 2ms" before checking status.
  549. * We wait 150ms, because that was the magic delay used for
  550. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  551. * between when the ATA command register is written, and then
  552. * status is checked. Because waiting for "a while" before
  553. * checking status is fine, post SRST, we perform this magic
  554. * delay here as well.
  555. */
  556. msleep(150);
  557. *class = ATA_DEV_NONE;
  558. if (ata_port_online(ap)) {
  559. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  560. rc = -EIO;
  561. reason = "device not ready";
  562. goto fail;
  563. }
  564. *class = ahci_dev_classify(ap);
  565. }
  566. DPRINTK("EXIT, class=%u\n", *class);
  567. return 0;
  568. fail_restart:
  569. ahci_start_engine(ap);
  570. fail:
  571. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  572. return rc;
  573. }
  574. static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
  575. {
  576. int rc;
  577. DPRINTK("ENTER\n");
  578. ahci_stop_engine(ap);
  579. rc = sata_std_hardreset(ap, class);
  580. ahci_start_engine(ap);
  581. if (rc == 0 && ata_port_online(ap))
  582. *class = ahci_dev_classify(ap);
  583. if (*class == ATA_DEV_UNKNOWN)
  584. *class = ATA_DEV_NONE;
  585. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  586. return rc;
  587. }
  588. static void ahci_postreset(struct ata_port *ap, unsigned int *class)
  589. {
  590. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  591. u32 new_tmp, tmp;
  592. ata_std_postreset(ap, class);
  593. /* Make sure port's ATAPI bit is set appropriately */
  594. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  595. if (*class == ATA_DEV_ATAPI)
  596. new_tmp |= PORT_CMD_ATAPI;
  597. else
  598. new_tmp &= ~PORT_CMD_ATAPI;
  599. if (new_tmp != tmp) {
  600. writel(new_tmp, port_mmio + PORT_CMD);
  601. readl(port_mmio + PORT_CMD); /* flush */
  602. }
  603. }
  604. static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
  605. {
  606. if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
  607. (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
  608. /* ATA_BUSY hasn't cleared, so send a CLO */
  609. ahci_clo(ap);
  610. }
  611. return ata_drive_probe_reset(ap, ata_std_probeinit,
  612. ahci_softreset, ahci_hardreset,
  613. ahci_postreset, classes);
  614. }
  615. static u8 ahci_check_status(struct ata_port *ap)
  616. {
  617. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  618. return readl(mmio + PORT_TFDATA) & 0xFF;
  619. }
  620. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  621. {
  622. struct ahci_port_priv *pp = ap->private_data;
  623. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  624. ata_tf_from_fis(d2h_fis, tf);
  625. }
  626. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  627. {
  628. struct scatterlist *sg;
  629. struct ahci_sg *ahci_sg;
  630. unsigned int n_sg = 0;
  631. VPRINTK("ENTER\n");
  632. /*
  633. * Next, the S/G list.
  634. */
  635. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  636. ata_for_each_sg(sg, qc) {
  637. dma_addr_t addr = sg_dma_address(sg);
  638. u32 sg_len = sg_dma_len(sg);
  639. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  640. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  641. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  642. ahci_sg++;
  643. n_sg++;
  644. }
  645. return n_sg;
  646. }
  647. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  648. {
  649. struct ata_port *ap = qc->ap;
  650. struct ahci_port_priv *pp = ap->private_data;
  651. int is_atapi = is_atapi_taskfile(&qc->tf);
  652. void *cmd_tbl;
  653. u32 opts;
  654. const u32 cmd_fis_len = 5; /* five dwords */
  655. unsigned int n_elem;
  656. /*
  657. * Fill in command table information. First, the header,
  658. * a SATA Register - Host to Device command FIS.
  659. */
  660. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  661. ata_tf_to_fis(&qc->tf, cmd_tbl, 0);
  662. if (is_atapi) {
  663. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  664. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  665. }
  666. n_elem = 0;
  667. if (qc->flags & ATA_QCFLAG_DMAMAP)
  668. n_elem = ahci_fill_sg(qc, cmd_tbl);
  669. /*
  670. * Fill in command slot information.
  671. */
  672. opts = cmd_fis_len | n_elem << 16;
  673. if (qc->tf.flags & ATA_TFLAG_WRITE)
  674. opts |= AHCI_CMD_WRITE;
  675. if (is_atapi)
  676. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  677. ahci_fill_cmd_slot(pp, qc->tag, opts);
  678. }
  679. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  680. {
  681. struct ahci_port_priv *pp = ap->private_data;
  682. struct ata_eh_info *ehi = &ap->eh_info;
  683. unsigned int err_mask = 0, action = 0;
  684. struct ata_queued_cmd *qc;
  685. u32 serror;
  686. ata_ehi_clear_desc(ehi);
  687. /* AHCI needs SError cleared; otherwise, it might lock up */
  688. serror = ahci_scr_read(ap, SCR_ERROR);
  689. ahci_scr_write(ap, SCR_ERROR, serror);
  690. /* analyze @irq_stat */
  691. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  692. if (irq_stat & PORT_IRQ_TF_ERR)
  693. err_mask |= AC_ERR_DEV;
  694. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  695. err_mask |= AC_ERR_HOST_BUS;
  696. action |= ATA_EH_SOFTRESET;
  697. }
  698. if (irq_stat & PORT_IRQ_IF_ERR) {
  699. err_mask |= AC_ERR_ATA_BUS;
  700. action |= ATA_EH_SOFTRESET;
  701. ata_ehi_push_desc(ehi, ", interface fatal error");
  702. }
  703. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  704. err_mask |= AC_ERR_ATA_BUS;
  705. action |= ATA_EH_SOFTRESET;
  706. ata_ehi_push_desc(ehi, ", %s", irq_stat & PORT_IRQ_CONNECT ?
  707. "connection status changed" : "PHY RDY changed");
  708. }
  709. if (irq_stat & PORT_IRQ_UNK_FIS) {
  710. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  711. err_mask |= AC_ERR_HSM;
  712. action |= ATA_EH_SOFTRESET;
  713. ata_ehi_push_desc(ehi, ", unknown FIS %08x %08x %08x %08x",
  714. unk[0], unk[1], unk[2], unk[3]);
  715. }
  716. /* okay, let's hand over to EH */
  717. ehi->serror |= serror;
  718. ehi->action |= action;
  719. qc = ata_qc_from_tag(ap, ap->active_tag);
  720. if (qc)
  721. qc->err_mask |= err_mask;
  722. else
  723. ehi->err_mask |= err_mask;
  724. if (irq_stat & PORT_IRQ_FREEZE)
  725. ata_port_freeze(ap);
  726. else
  727. ata_port_abort(ap);
  728. }
  729. static void ahci_host_intr(struct ata_port *ap)
  730. {
  731. void __iomem *mmio = ap->host_set->mmio_base;
  732. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  733. struct ata_eh_info *ehi = &ap->eh_info;
  734. u32 status, qc_active;
  735. int rc;
  736. status = readl(port_mmio + PORT_IRQ_STAT);
  737. writel(status, port_mmio + PORT_IRQ_STAT);
  738. if (unlikely(status & PORT_IRQ_ERROR)) {
  739. ahci_error_intr(ap, status);
  740. return;
  741. }
  742. if (ap->sactive)
  743. qc_active = readl(port_mmio + PORT_SCR_ACT);
  744. else
  745. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  746. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  747. if (rc > 0)
  748. return;
  749. if (rc < 0) {
  750. ehi->err_mask |= AC_ERR_HSM;
  751. ehi->action |= ATA_EH_SOFTRESET;
  752. ata_port_freeze(ap);
  753. return;
  754. }
  755. /* hmmm... a spurious interupt */
  756. /* some devices send D2H reg with I bit set during NCQ command phase */
  757. if (ap->sactive && status & PORT_IRQ_D2H_REG_FIS)
  758. return;
  759. /* ignore interim PIO setup fis interrupts */
  760. if (ata_tag_valid(ap->active_tag)) {
  761. struct ata_queued_cmd *qc =
  762. ata_qc_from_tag(ap, ap->active_tag);
  763. if (qc && qc->tf.protocol == ATA_PROT_PIO &&
  764. (status & PORT_IRQ_PIOS_FIS))
  765. return;
  766. }
  767. if (ata_ratelimit())
  768. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  769. "(irq_stat 0x%x active_tag %d sactive 0x%x)\n",
  770. status, ap->active_tag, ap->sactive);
  771. }
  772. static void ahci_irq_clear(struct ata_port *ap)
  773. {
  774. /* TODO */
  775. }
  776. static irqreturn_t ahci_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  777. {
  778. struct ata_host_set *host_set = dev_instance;
  779. struct ahci_host_priv *hpriv;
  780. unsigned int i, handled = 0;
  781. void __iomem *mmio;
  782. u32 irq_stat, irq_ack = 0;
  783. VPRINTK("ENTER\n");
  784. hpriv = host_set->private_data;
  785. mmio = host_set->mmio_base;
  786. /* sigh. 0xffffffff is a valid return from h/w */
  787. irq_stat = readl(mmio + HOST_IRQ_STAT);
  788. irq_stat &= hpriv->port_map;
  789. if (!irq_stat)
  790. return IRQ_NONE;
  791. spin_lock(&host_set->lock);
  792. for (i = 0; i < host_set->n_ports; i++) {
  793. struct ata_port *ap;
  794. if (!(irq_stat & (1 << i)))
  795. continue;
  796. ap = host_set->ports[i];
  797. if (ap) {
  798. ahci_host_intr(ap);
  799. VPRINTK("port %u\n", i);
  800. } else {
  801. VPRINTK("port %u (no irq)\n", i);
  802. if (ata_ratelimit())
  803. dev_printk(KERN_WARNING, host_set->dev,
  804. "interrupt on disabled port %u\n", i);
  805. }
  806. irq_ack |= (1 << i);
  807. }
  808. if (irq_ack) {
  809. writel(irq_ack, mmio + HOST_IRQ_STAT);
  810. handled = 1;
  811. }
  812. spin_unlock(&host_set->lock);
  813. VPRINTK("EXIT\n");
  814. return IRQ_RETVAL(handled);
  815. }
  816. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  817. {
  818. struct ata_port *ap = qc->ap;
  819. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  820. if (qc->tf.protocol == ATA_PROT_NCQ)
  821. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  822. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  823. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  824. return 0;
  825. }
  826. static void ahci_freeze(struct ata_port *ap)
  827. {
  828. void __iomem *mmio = ap->host_set->mmio_base;
  829. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  830. /* turn IRQ off */
  831. writel(0, port_mmio + PORT_IRQ_MASK);
  832. }
  833. static void ahci_thaw(struct ata_port *ap)
  834. {
  835. void __iomem *mmio = ap->host_set->mmio_base;
  836. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  837. u32 tmp;
  838. /* clear IRQ */
  839. tmp = readl(port_mmio + PORT_IRQ_STAT);
  840. writel(tmp, port_mmio + PORT_IRQ_STAT);
  841. writel(1 << ap->id, mmio + HOST_IRQ_STAT);
  842. /* turn IRQ back on */
  843. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  844. }
  845. static void ahci_error_handler(struct ata_port *ap)
  846. {
  847. if (!(ap->flags & ATA_FLAG_FROZEN)) {
  848. /* restart engine */
  849. ahci_stop_engine(ap);
  850. ahci_start_engine(ap);
  851. }
  852. /* perform recovery */
  853. ata_do_eh(ap, ahci_softreset, ahci_hardreset, ahci_postreset);
  854. }
  855. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  856. {
  857. struct ata_port *ap = qc->ap;
  858. if (qc->flags & ATA_QCFLAG_FAILED)
  859. qc->err_mask |= AC_ERR_OTHER;
  860. if (qc->err_mask) {
  861. /* make DMA engine forget about the failed command */
  862. ahci_stop_engine(ap);
  863. ahci_start_engine(ap);
  864. }
  865. }
  866. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  867. unsigned int port_idx)
  868. {
  869. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  870. base = ahci_port_base_ul(base, port_idx);
  871. VPRINTK("base now==0x%lx\n", base);
  872. port->cmd_addr = base;
  873. port->scr_addr = base + PORT_SCR;
  874. VPRINTK("EXIT\n");
  875. }
  876. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  877. {
  878. struct ahci_host_priv *hpriv = probe_ent->private_data;
  879. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  880. void __iomem *mmio = probe_ent->mmio_base;
  881. u32 tmp, cap_save;
  882. unsigned int i, j, using_dac;
  883. int rc;
  884. void __iomem *port_mmio;
  885. cap_save = readl(mmio + HOST_CAP);
  886. cap_save &= ( (1<<28) | (1<<17) );
  887. cap_save |= (1 << 27);
  888. /* global controller reset */
  889. tmp = readl(mmio + HOST_CTL);
  890. if ((tmp & HOST_RESET) == 0) {
  891. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  892. readl(mmio + HOST_CTL); /* flush */
  893. }
  894. /* reset must complete within 1 second, or
  895. * the hardware should be considered fried.
  896. */
  897. ssleep(1);
  898. tmp = readl(mmio + HOST_CTL);
  899. if (tmp & HOST_RESET) {
  900. dev_printk(KERN_ERR, &pdev->dev,
  901. "controller reset failed (0x%x)\n", tmp);
  902. return -EIO;
  903. }
  904. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  905. (void) readl(mmio + HOST_CTL); /* flush */
  906. writel(cap_save, mmio + HOST_CAP);
  907. writel(0xf, mmio + HOST_PORTS_IMPL);
  908. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  909. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  910. u16 tmp16;
  911. pci_read_config_word(pdev, 0x92, &tmp16);
  912. tmp16 |= 0xf;
  913. pci_write_config_word(pdev, 0x92, tmp16);
  914. }
  915. hpriv->cap = readl(mmio + HOST_CAP);
  916. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  917. probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
  918. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  919. hpriv->cap, hpriv->port_map, probe_ent->n_ports);
  920. using_dac = hpriv->cap & HOST_CAP_64;
  921. if (using_dac &&
  922. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  923. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  924. if (rc) {
  925. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  926. if (rc) {
  927. dev_printk(KERN_ERR, &pdev->dev,
  928. "64-bit DMA enable failed\n");
  929. return rc;
  930. }
  931. }
  932. } else {
  933. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  934. if (rc) {
  935. dev_printk(KERN_ERR, &pdev->dev,
  936. "32-bit DMA enable failed\n");
  937. return rc;
  938. }
  939. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  940. if (rc) {
  941. dev_printk(KERN_ERR, &pdev->dev,
  942. "32-bit consistent DMA enable failed\n");
  943. return rc;
  944. }
  945. }
  946. for (i = 0; i < probe_ent->n_ports; i++) {
  947. #if 0 /* BIOSen initialize this incorrectly */
  948. if (!(hpriv->port_map & (1 << i)))
  949. continue;
  950. #endif
  951. port_mmio = ahci_port_base(mmio, i);
  952. VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
  953. ahci_setup_port(&probe_ent->port[i],
  954. (unsigned long) mmio, i);
  955. /* make sure port is not active */
  956. tmp = readl(port_mmio + PORT_CMD);
  957. VPRINTK("PORT_CMD 0x%x\n", tmp);
  958. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  959. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  960. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  961. PORT_CMD_FIS_RX | PORT_CMD_START);
  962. writel(tmp, port_mmio + PORT_CMD);
  963. readl(port_mmio + PORT_CMD); /* flush */
  964. /* spec says 500 msecs for each bit, so
  965. * this is slightly incorrect.
  966. */
  967. msleep(500);
  968. }
  969. writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
  970. j = 0;
  971. while (j < 100) {
  972. msleep(10);
  973. tmp = readl(port_mmio + PORT_SCR_STAT);
  974. if ((tmp & 0xf) == 0x3)
  975. break;
  976. j++;
  977. }
  978. tmp = readl(port_mmio + PORT_SCR_ERR);
  979. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  980. writel(tmp, port_mmio + PORT_SCR_ERR);
  981. /* ack any pending irq events for this port */
  982. tmp = readl(port_mmio + PORT_IRQ_STAT);
  983. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  984. if (tmp)
  985. writel(tmp, port_mmio + PORT_IRQ_STAT);
  986. writel(1 << i, mmio + HOST_IRQ_STAT);
  987. }
  988. tmp = readl(mmio + HOST_CTL);
  989. VPRINTK("HOST_CTL 0x%x\n", tmp);
  990. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  991. tmp = readl(mmio + HOST_CTL);
  992. VPRINTK("HOST_CTL 0x%x\n", tmp);
  993. pci_set_master(pdev);
  994. return 0;
  995. }
  996. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  997. {
  998. struct ahci_host_priv *hpriv = probe_ent->private_data;
  999. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1000. void __iomem *mmio = probe_ent->mmio_base;
  1001. u32 vers, cap, impl, speed;
  1002. const char *speed_s;
  1003. u16 cc;
  1004. const char *scc_s;
  1005. vers = readl(mmio + HOST_VERSION);
  1006. cap = hpriv->cap;
  1007. impl = hpriv->port_map;
  1008. speed = (cap >> 20) & 0xf;
  1009. if (speed == 1)
  1010. speed_s = "1.5";
  1011. else if (speed == 2)
  1012. speed_s = "3";
  1013. else
  1014. speed_s = "?";
  1015. pci_read_config_word(pdev, 0x0a, &cc);
  1016. if (cc == 0x0101)
  1017. scc_s = "IDE";
  1018. else if (cc == 0x0106)
  1019. scc_s = "SATA";
  1020. else if (cc == 0x0104)
  1021. scc_s = "RAID";
  1022. else
  1023. scc_s = "unknown";
  1024. dev_printk(KERN_INFO, &pdev->dev,
  1025. "AHCI %02x%02x.%02x%02x "
  1026. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1027. ,
  1028. (vers >> 24) & 0xff,
  1029. (vers >> 16) & 0xff,
  1030. (vers >> 8) & 0xff,
  1031. vers & 0xff,
  1032. ((cap >> 8) & 0x1f) + 1,
  1033. (cap & 0x1f) + 1,
  1034. speed_s,
  1035. impl,
  1036. scc_s);
  1037. dev_printk(KERN_INFO, &pdev->dev,
  1038. "flags: "
  1039. "%s%s%s%s%s%s"
  1040. "%s%s%s%s%s%s%s\n"
  1041. ,
  1042. cap & (1 << 31) ? "64bit " : "",
  1043. cap & (1 << 30) ? "ncq " : "",
  1044. cap & (1 << 28) ? "ilck " : "",
  1045. cap & (1 << 27) ? "stag " : "",
  1046. cap & (1 << 26) ? "pm " : "",
  1047. cap & (1 << 25) ? "led " : "",
  1048. cap & (1 << 24) ? "clo " : "",
  1049. cap & (1 << 19) ? "nz " : "",
  1050. cap & (1 << 18) ? "only " : "",
  1051. cap & (1 << 17) ? "pmp " : "",
  1052. cap & (1 << 15) ? "pio " : "",
  1053. cap & (1 << 14) ? "slum " : "",
  1054. cap & (1 << 13) ? "part " : ""
  1055. );
  1056. }
  1057. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1058. {
  1059. static int printed_version;
  1060. struct ata_probe_ent *probe_ent = NULL;
  1061. struct ahci_host_priv *hpriv;
  1062. unsigned long base;
  1063. void __iomem *mmio_base;
  1064. unsigned int board_idx = (unsigned int) ent->driver_data;
  1065. int have_msi, pci_dev_busy = 0;
  1066. int rc;
  1067. VPRINTK("ENTER\n");
  1068. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1069. if (!printed_version++)
  1070. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1071. rc = pci_enable_device(pdev);
  1072. if (rc)
  1073. return rc;
  1074. rc = pci_request_regions(pdev, DRV_NAME);
  1075. if (rc) {
  1076. pci_dev_busy = 1;
  1077. goto err_out;
  1078. }
  1079. if (pci_enable_msi(pdev) == 0)
  1080. have_msi = 1;
  1081. else {
  1082. pci_intx(pdev, 1);
  1083. have_msi = 0;
  1084. }
  1085. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1086. if (probe_ent == NULL) {
  1087. rc = -ENOMEM;
  1088. goto err_out_msi;
  1089. }
  1090. memset(probe_ent, 0, sizeof(*probe_ent));
  1091. probe_ent->dev = pci_dev_to_dev(pdev);
  1092. INIT_LIST_HEAD(&probe_ent->node);
  1093. mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
  1094. if (mmio_base == NULL) {
  1095. rc = -ENOMEM;
  1096. goto err_out_free_ent;
  1097. }
  1098. base = (unsigned long) mmio_base;
  1099. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1100. if (!hpriv) {
  1101. rc = -ENOMEM;
  1102. goto err_out_iounmap;
  1103. }
  1104. memset(hpriv, 0, sizeof(*hpriv));
  1105. probe_ent->sht = ahci_port_info[board_idx].sht;
  1106. probe_ent->host_flags = ahci_port_info[board_idx].host_flags;
  1107. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  1108. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  1109. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  1110. probe_ent->irq = pdev->irq;
  1111. probe_ent->irq_flags = SA_SHIRQ;
  1112. probe_ent->mmio_base = mmio_base;
  1113. probe_ent->private_data = hpriv;
  1114. if (have_msi)
  1115. hpriv->flags |= AHCI_FLAG_MSI;
  1116. /* JMicron-specific fixup: make sure we're in AHCI mode */
  1117. if (pdev->vendor == 0x197b)
  1118. pci_write_config_byte(pdev, 0x41, 0xa1);
  1119. /* initialize adapter */
  1120. rc = ahci_host_init(probe_ent);
  1121. if (rc)
  1122. goto err_out_hpriv;
  1123. if (hpriv->cap & HOST_CAP_NCQ)
  1124. probe_ent->host_flags |= ATA_FLAG_NCQ;
  1125. ahci_print_info(probe_ent);
  1126. /* FIXME: check ata_device_add return value */
  1127. ata_device_add(probe_ent);
  1128. kfree(probe_ent);
  1129. return 0;
  1130. err_out_hpriv:
  1131. kfree(hpriv);
  1132. err_out_iounmap:
  1133. pci_iounmap(pdev, mmio_base);
  1134. err_out_free_ent:
  1135. kfree(probe_ent);
  1136. err_out_msi:
  1137. if (have_msi)
  1138. pci_disable_msi(pdev);
  1139. else
  1140. pci_intx(pdev, 0);
  1141. pci_release_regions(pdev);
  1142. err_out:
  1143. if (!pci_dev_busy)
  1144. pci_disable_device(pdev);
  1145. return rc;
  1146. }
  1147. static void ahci_remove_one (struct pci_dev *pdev)
  1148. {
  1149. struct device *dev = pci_dev_to_dev(pdev);
  1150. struct ata_host_set *host_set = dev_get_drvdata(dev);
  1151. struct ahci_host_priv *hpriv = host_set->private_data;
  1152. struct ata_port *ap;
  1153. unsigned int i;
  1154. int have_msi;
  1155. for (i = 0; i < host_set->n_ports; i++) {
  1156. ap = host_set->ports[i];
  1157. scsi_remove_host(ap->host);
  1158. }
  1159. have_msi = hpriv->flags & AHCI_FLAG_MSI;
  1160. free_irq(host_set->irq, host_set);
  1161. for (i = 0; i < host_set->n_ports; i++) {
  1162. ap = host_set->ports[i];
  1163. ata_scsi_release(ap->host);
  1164. scsi_host_put(ap->host);
  1165. }
  1166. kfree(hpriv);
  1167. pci_iounmap(pdev, host_set->mmio_base);
  1168. kfree(host_set);
  1169. if (have_msi)
  1170. pci_disable_msi(pdev);
  1171. else
  1172. pci_intx(pdev, 0);
  1173. pci_release_regions(pdev);
  1174. pci_disable_device(pdev);
  1175. dev_set_drvdata(dev, NULL);
  1176. }
  1177. static int __init ahci_init(void)
  1178. {
  1179. return pci_module_init(&ahci_pci_driver);
  1180. }
  1181. static void __exit ahci_exit(void)
  1182. {
  1183. pci_unregister_driver(&ahci_pci_driver);
  1184. }
  1185. MODULE_AUTHOR("Jeff Garzik");
  1186. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1187. MODULE_LICENSE("GPL");
  1188. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1189. MODULE_VERSION(DRV_VERSION);
  1190. module_init(ahci_init);
  1191. module_exit(ahci_exit);