ahci.c 42 KB

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