ahci.c 37 KB

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