ahci.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899
  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/dma-mapping.h>
  42. #include <linux/device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <linux/libata.h>
  46. #define DRV_NAME "ahci"
  47. #define DRV_VERSION "2.3"
  48. enum {
  49. AHCI_PCI_BAR = 5,
  50. AHCI_MAX_PORTS = 32,
  51. AHCI_MAX_SG = 168, /* hardware max is 64K */
  52. AHCI_DMA_BOUNDARY = 0xffffffff,
  53. AHCI_USE_CLUSTERING = 1,
  54. AHCI_MAX_CMDS = 32,
  55. AHCI_CMD_SZ = 32,
  56. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  57. AHCI_RX_FIS_SZ = 256,
  58. AHCI_CMD_TBL_CDB = 0x40,
  59. AHCI_CMD_TBL_HDR_SZ = 0x80,
  60. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  61. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  62. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  63. AHCI_RX_FIS_SZ,
  64. AHCI_IRQ_ON_SG = (1 << 31),
  65. AHCI_CMD_ATAPI = (1 << 5),
  66. AHCI_CMD_WRITE = (1 << 6),
  67. AHCI_CMD_PREFETCH = (1 << 7),
  68. AHCI_CMD_RESET = (1 << 8),
  69. AHCI_CMD_CLR_BUSY = (1 << 10),
  70. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  71. RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
  72. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  73. board_ahci = 0,
  74. board_ahci_pi = 1,
  75. board_ahci_vt8251 = 2,
  76. board_ahci_ign_iferr = 3,
  77. board_ahci_sb600 = 4,
  78. board_ahci_mv = 5,
  79. /* global controller registers */
  80. HOST_CAP = 0x00, /* host capabilities */
  81. HOST_CTL = 0x04, /* global host control */
  82. HOST_IRQ_STAT = 0x08, /* interrupt status */
  83. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  84. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  85. /* HOST_CTL bits */
  86. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  87. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  88. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  89. /* HOST_CAP bits */
  90. HOST_CAP_SSC = (1 << 14), /* Slumber capable */
  91. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  92. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  93. HOST_CAP_SNTF = (1 << 29), /* SNotification register */
  94. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  95. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  96. /* registers for each SATA port */
  97. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  98. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  99. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  100. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  101. PORT_IRQ_STAT = 0x10, /* interrupt status */
  102. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  103. PORT_CMD = 0x18, /* port command */
  104. PORT_TFDATA = 0x20, /* taskfile data */
  105. PORT_SIG = 0x24, /* device TF signature */
  106. PORT_CMD_ISSUE = 0x38, /* command issue */
  107. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  108. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  109. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  110. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  111. PORT_SCR_NTF = 0x3c, /* SATA phy register: SNotification */
  112. /* PORT_IRQ_{STAT,MASK} bits */
  113. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  114. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  115. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  116. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  117. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  118. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  119. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  120. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  121. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  122. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  123. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  124. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  125. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  126. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  127. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  128. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  129. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  130. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  131. PORT_IRQ_IF_ERR |
  132. PORT_IRQ_CONNECT |
  133. PORT_IRQ_PHYRDY |
  134. PORT_IRQ_UNK_FIS,
  135. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  136. PORT_IRQ_TF_ERR |
  137. PORT_IRQ_HBUS_DATA_ERR,
  138. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  139. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  140. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  141. /* PORT_CMD bits */
  142. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  143. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  144. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  145. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  146. PORT_CMD_CLO = (1 << 3), /* Command list override */
  147. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  148. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  149. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  150. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  151. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  152. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  153. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  154. /* ap->flags bits */
  155. AHCI_FLAG_NO_NCQ = (1 << 24),
  156. AHCI_FLAG_IGN_IRQ_IF_ERR = (1 << 25), /* ignore IRQ_IF_ERR */
  157. AHCI_FLAG_HONOR_PI = (1 << 26), /* honor PORTS_IMPL */
  158. AHCI_FLAG_IGN_SERR_INTERNAL = (1 << 27), /* ignore SERR_INTERNAL */
  159. AHCI_FLAG_32BIT_ONLY = (1 << 28), /* force 32bit */
  160. AHCI_FLAG_MV_PATA = (1 << 29), /* PATA port */
  161. AHCI_FLAG_NO_MSI = (1 << 30), /* no PCI MSI */
  162. AHCI_FLAG_COMMON = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  163. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  164. ATA_FLAG_SKIP_D2H_BSY |
  165. ATA_FLAG_ACPI_SATA,
  166. };
  167. struct ahci_cmd_hdr {
  168. u32 opts;
  169. u32 status;
  170. u32 tbl_addr;
  171. u32 tbl_addr_hi;
  172. u32 reserved[4];
  173. };
  174. struct ahci_sg {
  175. u32 addr;
  176. u32 addr_hi;
  177. u32 reserved;
  178. u32 flags_size;
  179. };
  180. struct ahci_host_priv {
  181. u32 cap; /* cap to use */
  182. u32 port_map; /* port map to use */
  183. u32 saved_cap; /* saved initial cap */
  184. u32 saved_port_map; /* saved initial port_map */
  185. };
  186. struct ahci_port_priv {
  187. struct ahci_cmd_hdr *cmd_slot;
  188. dma_addr_t cmd_slot_dma;
  189. void *cmd_tbl;
  190. dma_addr_t cmd_tbl_dma;
  191. void *rx_fis;
  192. dma_addr_t rx_fis_dma;
  193. /* for NCQ spurious interrupt analysis */
  194. unsigned int ncq_saw_d2h:1;
  195. unsigned int ncq_saw_dmas:1;
  196. unsigned int ncq_saw_sdb:1;
  197. };
  198. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
  199. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
  200. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  201. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  202. static void ahci_irq_clear(struct ata_port *ap);
  203. static int ahci_port_start(struct ata_port *ap);
  204. static void ahci_port_stop(struct ata_port *ap);
  205. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  206. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  207. static u8 ahci_check_status(struct ata_port *ap);
  208. static void ahci_freeze(struct ata_port *ap);
  209. static void ahci_thaw(struct ata_port *ap);
  210. static void ahci_error_handler(struct ata_port *ap);
  211. static void ahci_vt8251_error_handler(struct ata_port *ap);
  212. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  213. static int ahci_port_resume(struct ata_port *ap);
  214. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl);
  215. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  216. u32 opts);
  217. #ifdef CONFIG_PM
  218. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  219. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  220. static int ahci_pci_device_resume(struct pci_dev *pdev);
  221. #endif
  222. static struct scsi_host_template ahci_sht = {
  223. .module = THIS_MODULE,
  224. .name = DRV_NAME,
  225. .ioctl = ata_scsi_ioctl,
  226. .queuecommand = ata_scsi_queuecmd,
  227. .change_queue_depth = ata_scsi_change_queue_depth,
  228. .can_queue = AHCI_MAX_CMDS - 1,
  229. .this_id = ATA_SHT_THIS_ID,
  230. .sg_tablesize = AHCI_MAX_SG,
  231. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  232. .emulated = ATA_SHT_EMULATED,
  233. .use_clustering = AHCI_USE_CLUSTERING,
  234. .proc_name = DRV_NAME,
  235. .dma_boundary = AHCI_DMA_BOUNDARY,
  236. .slave_configure = ata_scsi_slave_config,
  237. .slave_destroy = ata_scsi_slave_destroy,
  238. .bios_param = ata_std_bios_param,
  239. };
  240. static const struct ata_port_operations ahci_ops = {
  241. .port_disable = ata_port_disable,
  242. .check_status = ahci_check_status,
  243. .check_altstatus = ahci_check_status,
  244. .dev_select = ata_noop_dev_select,
  245. .tf_read = ahci_tf_read,
  246. .qc_prep = ahci_qc_prep,
  247. .qc_issue = ahci_qc_issue,
  248. .irq_clear = ahci_irq_clear,
  249. .irq_on = ata_dummy_irq_on,
  250. .irq_ack = ata_dummy_irq_ack,
  251. .scr_read = ahci_scr_read,
  252. .scr_write = ahci_scr_write,
  253. .freeze = ahci_freeze,
  254. .thaw = ahci_thaw,
  255. .error_handler = ahci_error_handler,
  256. .post_internal_cmd = ahci_post_internal_cmd,
  257. #ifdef CONFIG_PM
  258. .port_suspend = ahci_port_suspend,
  259. .port_resume = ahci_port_resume,
  260. #endif
  261. .port_start = ahci_port_start,
  262. .port_stop = ahci_port_stop,
  263. };
  264. static const struct ata_port_operations ahci_vt8251_ops = {
  265. .port_disable = ata_port_disable,
  266. .check_status = ahci_check_status,
  267. .check_altstatus = ahci_check_status,
  268. .dev_select = ata_noop_dev_select,
  269. .tf_read = ahci_tf_read,
  270. .qc_prep = ahci_qc_prep,
  271. .qc_issue = ahci_qc_issue,
  272. .irq_clear = ahci_irq_clear,
  273. .irq_on = ata_dummy_irq_on,
  274. .irq_ack = ata_dummy_irq_ack,
  275. .scr_read = ahci_scr_read,
  276. .scr_write = ahci_scr_write,
  277. .freeze = ahci_freeze,
  278. .thaw = ahci_thaw,
  279. .error_handler = ahci_vt8251_error_handler,
  280. .post_internal_cmd = ahci_post_internal_cmd,
  281. #ifdef CONFIG_PM
  282. .port_suspend = ahci_port_suspend,
  283. .port_resume = ahci_port_resume,
  284. #endif
  285. .port_start = ahci_port_start,
  286. .port_stop = ahci_port_stop,
  287. };
  288. static const struct ata_port_info ahci_port_info[] = {
  289. /* board_ahci */
  290. {
  291. .flags = AHCI_FLAG_COMMON,
  292. .pio_mask = 0x1f, /* pio0-4 */
  293. .udma_mask = ATA_UDMA6,
  294. .port_ops = &ahci_ops,
  295. },
  296. /* board_ahci_pi */
  297. {
  298. .flags = AHCI_FLAG_COMMON | AHCI_FLAG_HONOR_PI,
  299. .pio_mask = 0x1f, /* pio0-4 */
  300. .udma_mask = ATA_UDMA6,
  301. .port_ops = &ahci_ops,
  302. },
  303. /* board_ahci_vt8251 */
  304. {
  305. .flags = AHCI_FLAG_COMMON | ATA_FLAG_HRST_TO_RESUME |
  306. AHCI_FLAG_NO_NCQ,
  307. .pio_mask = 0x1f, /* pio0-4 */
  308. .udma_mask = ATA_UDMA6,
  309. .port_ops = &ahci_vt8251_ops,
  310. },
  311. /* board_ahci_ign_iferr */
  312. {
  313. .flags = AHCI_FLAG_COMMON | AHCI_FLAG_IGN_IRQ_IF_ERR,
  314. .pio_mask = 0x1f, /* pio0-4 */
  315. .udma_mask = ATA_UDMA6,
  316. .port_ops = &ahci_ops,
  317. },
  318. /* board_ahci_sb600 */
  319. {
  320. .flags = AHCI_FLAG_COMMON |
  321. AHCI_FLAG_IGN_SERR_INTERNAL |
  322. AHCI_FLAG_32BIT_ONLY,
  323. .pio_mask = 0x1f, /* pio0-4 */
  324. .udma_mask = ATA_UDMA6,
  325. .port_ops = &ahci_ops,
  326. },
  327. /* board_ahci_mv */
  328. {
  329. .sht = &ahci_sht,
  330. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  331. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  332. ATA_FLAG_SKIP_D2H_BSY | AHCI_FLAG_HONOR_PI |
  333. AHCI_FLAG_NO_NCQ | AHCI_FLAG_NO_MSI |
  334. AHCI_FLAG_MV_PATA,
  335. .pio_mask = 0x1f, /* pio0-4 */
  336. .udma_mask = ATA_UDMA6,
  337. .port_ops = &ahci_ops,
  338. },
  339. };
  340. static const struct pci_device_id ahci_pci_tbl[] = {
  341. /* Intel */
  342. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  343. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  344. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  345. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  346. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  347. { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
  348. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  349. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  350. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  351. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  352. { PCI_VDEVICE(INTEL, 0x2821), board_ahci_pi }, /* ICH8 */
  353. { PCI_VDEVICE(INTEL, 0x2822), board_ahci_pi }, /* ICH8 */
  354. { PCI_VDEVICE(INTEL, 0x2824), board_ahci_pi }, /* ICH8 */
  355. { PCI_VDEVICE(INTEL, 0x2829), board_ahci_pi }, /* ICH8M */
  356. { PCI_VDEVICE(INTEL, 0x282a), board_ahci_pi }, /* ICH8M */
  357. { PCI_VDEVICE(INTEL, 0x2922), board_ahci_pi }, /* ICH9 */
  358. { PCI_VDEVICE(INTEL, 0x2923), board_ahci_pi }, /* ICH9 */
  359. { PCI_VDEVICE(INTEL, 0x2924), board_ahci_pi }, /* ICH9 */
  360. { PCI_VDEVICE(INTEL, 0x2925), board_ahci_pi }, /* ICH9 */
  361. { PCI_VDEVICE(INTEL, 0x2927), board_ahci_pi }, /* ICH9 */
  362. { PCI_VDEVICE(INTEL, 0x2929), board_ahci_pi }, /* ICH9M */
  363. { PCI_VDEVICE(INTEL, 0x292a), board_ahci_pi }, /* ICH9M */
  364. { PCI_VDEVICE(INTEL, 0x292b), board_ahci_pi }, /* ICH9M */
  365. { PCI_VDEVICE(INTEL, 0x292c), board_ahci_pi }, /* ICH9M */
  366. { PCI_VDEVICE(INTEL, 0x292f), board_ahci_pi }, /* ICH9M */
  367. { PCI_VDEVICE(INTEL, 0x294d), board_ahci_pi }, /* ICH9 */
  368. { PCI_VDEVICE(INTEL, 0x294e), board_ahci_pi }, /* ICH9M */
  369. /* JMicron 360/1/3/5/6, match class to avoid IDE function */
  370. { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  371. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci_ign_iferr },
  372. /* ATI */
  373. { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600 }, /* ATI SB600 */
  374. { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb600 }, /* ATI SB700 IDE */
  375. { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb600 }, /* ATI SB700 AHCI */
  376. { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb600 }, /* ATI SB700 nraid5 */
  377. { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb600 }, /* ATI SB700 raid5 */
  378. /* VIA */
  379. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  380. { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251 }, /* VIA VT8251 */
  381. /* NVIDIA */
  382. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
  383. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
  384. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
  385. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
  386. { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci }, /* MCP65 */
  387. { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci }, /* MCP65 */
  388. { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci }, /* MCP65 */
  389. { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci }, /* MCP65 */
  390. { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci }, /* MCP67 */
  391. { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci }, /* MCP67 */
  392. { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci }, /* MCP67 */
  393. { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci }, /* MCP67 */
  394. { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
  395. { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
  396. { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
  397. { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
  398. { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
  399. { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
  400. { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
  401. { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
  402. { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci }, /* MCP73 */
  403. { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci }, /* MCP73 */
  404. { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci }, /* MCP73 */
  405. { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci }, /* MCP73 */
  406. { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci }, /* MCP73 */
  407. { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci }, /* MCP73 */
  408. { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci }, /* MCP73 */
  409. { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci }, /* MCP73 */
  410. { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci }, /* MCP73 */
  411. { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci }, /* MCP73 */
  412. { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci }, /* MCP73 */
  413. { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci }, /* MCP73 */
  414. { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci }, /* MCP77 */
  415. { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci }, /* MCP77 */
  416. { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci }, /* MCP77 */
  417. { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci }, /* MCP77 */
  418. { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci }, /* MCP77 */
  419. { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci }, /* MCP77 */
  420. { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci }, /* MCP77 */
  421. { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci }, /* MCP77 */
  422. { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci }, /* MCP77 */
  423. { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci }, /* MCP77 */
  424. { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci }, /* MCP77 */
  425. { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci }, /* MCP77 */
  426. /* SiS */
  427. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  428. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  429. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  430. /* Marvell */
  431. { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv }, /* 6145 */
  432. /* Generic, PCI class code for AHCI */
  433. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  434. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
  435. { } /* terminate list */
  436. };
  437. static struct pci_driver ahci_pci_driver = {
  438. .name = DRV_NAME,
  439. .id_table = ahci_pci_tbl,
  440. .probe = ahci_init_one,
  441. .remove = ata_pci_remove_one,
  442. #ifdef CONFIG_PM
  443. .suspend = ahci_pci_device_suspend,
  444. .resume = ahci_pci_device_resume,
  445. #endif
  446. };
  447. static inline int ahci_nr_ports(u32 cap)
  448. {
  449. return (cap & 0x1f) + 1;
  450. }
  451. static inline void __iomem *__ahci_port_base(struct ata_host *host,
  452. unsigned int port_no)
  453. {
  454. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  455. return mmio + 0x100 + (port_no * 0x80);
  456. }
  457. static inline void __iomem *ahci_port_base(struct ata_port *ap)
  458. {
  459. return __ahci_port_base(ap->host, ap->port_no);
  460. }
  461. /**
  462. * ahci_save_initial_config - Save and fixup initial config values
  463. * @pdev: target PCI device
  464. * @pi: associated ATA port info
  465. * @hpriv: host private area to store config values
  466. *
  467. * Some registers containing configuration info might be setup by
  468. * BIOS and might be cleared on reset. This function saves the
  469. * initial values of those registers into @hpriv such that they
  470. * can be restored after controller reset.
  471. *
  472. * If inconsistent, config values are fixed up by this function.
  473. *
  474. * LOCKING:
  475. * None.
  476. */
  477. static void ahci_save_initial_config(struct pci_dev *pdev,
  478. const struct ata_port_info *pi,
  479. struct ahci_host_priv *hpriv)
  480. {
  481. void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
  482. u32 cap, port_map;
  483. int i;
  484. /* Values prefixed with saved_ are written back to host after
  485. * reset. Values without are used for driver operation.
  486. */
  487. hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
  488. hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
  489. /* some chips have errata preventing 64bit use */
  490. if ((cap & HOST_CAP_64) && (pi->flags & AHCI_FLAG_32BIT_ONLY)) {
  491. dev_printk(KERN_INFO, &pdev->dev,
  492. "controller can't do 64bit DMA, forcing 32bit\n");
  493. cap &= ~HOST_CAP_64;
  494. }
  495. if ((cap & HOST_CAP_NCQ) && (pi->flags & AHCI_FLAG_NO_NCQ)) {
  496. dev_printk(KERN_INFO, &pdev->dev,
  497. "controller can't do NCQ, turning off CAP_NCQ\n");
  498. cap &= ~HOST_CAP_NCQ;
  499. }
  500. /* fixup zero port_map */
  501. if (!port_map) {
  502. port_map = (1 << ahci_nr_ports(cap)) - 1;
  503. dev_printk(KERN_WARNING, &pdev->dev,
  504. "PORTS_IMPL is zero, forcing 0x%x\n", port_map);
  505. /* write the fixed up value to the PI register */
  506. hpriv->saved_port_map = port_map;
  507. }
  508. /*
  509. * Temporary Marvell 6145 hack: PATA port presence
  510. * is asserted through the standard AHCI port
  511. * presence register, as bit 4 (counting from 0)
  512. */
  513. if (pi->flags & AHCI_FLAG_MV_PATA) {
  514. dev_printk(KERN_ERR, &pdev->dev,
  515. "MV_AHCI HACK: port_map %x -> %x\n",
  516. hpriv->port_map,
  517. hpriv->port_map & 0xf);
  518. port_map &= 0xf;
  519. }
  520. /* cross check port_map and cap.n_ports */
  521. if (pi->flags & AHCI_FLAG_HONOR_PI) {
  522. u32 tmp_port_map = port_map;
  523. int n_ports = ahci_nr_ports(cap);
  524. for (i = 0; i < AHCI_MAX_PORTS && n_ports; i++) {
  525. if (tmp_port_map & (1 << i)) {
  526. n_ports--;
  527. tmp_port_map &= ~(1 << i);
  528. }
  529. }
  530. /* Whine if inconsistent. No need to update cap.
  531. * port_map is used to determine number of ports.
  532. */
  533. if (n_ports || tmp_port_map)
  534. dev_printk(KERN_WARNING, &pdev->dev,
  535. "nr_ports (%u) and implemented port map "
  536. "(0x%x) don't match\n",
  537. ahci_nr_ports(cap), port_map);
  538. } else {
  539. /* fabricate port_map from cap.nr_ports */
  540. port_map = (1 << ahci_nr_ports(cap)) - 1;
  541. }
  542. /* record values to use during operation */
  543. hpriv->cap = cap;
  544. hpriv->port_map = port_map;
  545. }
  546. /**
  547. * ahci_restore_initial_config - Restore initial config
  548. * @host: target ATA host
  549. *
  550. * Restore initial config stored by ahci_save_initial_config().
  551. *
  552. * LOCKING:
  553. * None.
  554. */
  555. static void ahci_restore_initial_config(struct ata_host *host)
  556. {
  557. struct ahci_host_priv *hpriv = host->private_data;
  558. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  559. writel(hpriv->saved_cap, mmio + HOST_CAP);
  560. writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
  561. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  562. }
  563. static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
  564. {
  565. static const int offset[] = {
  566. [SCR_STATUS] = PORT_SCR_STAT,
  567. [SCR_CONTROL] = PORT_SCR_CTL,
  568. [SCR_ERROR] = PORT_SCR_ERR,
  569. [SCR_ACTIVE] = PORT_SCR_ACT,
  570. [SCR_NOTIFICATION] = PORT_SCR_NTF,
  571. };
  572. struct ahci_host_priv *hpriv = ap->host->private_data;
  573. if (sc_reg < ARRAY_SIZE(offset) &&
  574. (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
  575. return offset[sc_reg];
  576. return 0;
  577. }
  578. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  579. {
  580. void __iomem *port_mmio = ahci_port_base(ap);
  581. int offset = ahci_scr_offset(ap, sc_reg);
  582. if (offset) {
  583. *val = readl(port_mmio + offset);
  584. return 0;
  585. }
  586. return -EINVAL;
  587. }
  588. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  589. {
  590. void __iomem *port_mmio = ahci_port_base(ap);
  591. int offset = ahci_scr_offset(ap, sc_reg);
  592. if (offset) {
  593. writel(val, port_mmio + offset);
  594. return 0;
  595. }
  596. return -EINVAL;
  597. }
  598. static void ahci_start_engine(struct ata_port *ap)
  599. {
  600. void __iomem *port_mmio = ahci_port_base(ap);
  601. u32 tmp;
  602. /* start DMA */
  603. tmp = readl(port_mmio + PORT_CMD);
  604. tmp |= PORT_CMD_START;
  605. writel(tmp, port_mmio + PORT_CMD);
  606. readl(port_mmio + PORT_CMD); /* flush */
  607. }
  608. static int ahci_stop_engine(struct ata_port *ap)
  609. {
  610. void __iomem *port_mmio = ahci_port_base(ap);
  611. u32 tmp;
  612. tmp = readl(port_mmio + PORT_CMD);
  613. /* check if the HBA is idle */
  614. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  615. return 0;
  616. /* setting HBA to idle */
  617. tmp &= ~PORT_CMD_START;
  618. writel(tmp, port_mmio + PORT_CMD);
  619. /* wait for engine to stop. This could be as long as 500 msec */
  620. tmp = ata_wait_register(port_mmio + PORT_CMD,
  621. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  622. if (tmp & PORT_CMD_LIST_ON)
  623. return -EIO;
  624. return 0;
  625. }
  626. static void ahci_start_fis_rx(struct ata_port *ap)
  627. {
  628. void __iomem *port_mmio = ahci_port_base(ap);
  629. struct ahci_host_priv *hpriv = ap->host->private_data;
  630. struct ahci_port_priv *pp = ap->private_data;
  631. u32 tmp;
  632. /* set FIS registers */
  633. if (hpriv->cap & HOST_CAP_64)
  634. writel((pp->cmd_slot_dma >> 16) >> 16,
  635. port_mmio + PORT_LST_ADDR_HI);
  636. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  637. if (hpriv->cap & HOST_CAP_64)
  638. writel((pp->rx_fis_dma >> 16) >> 16,
  639. port_mmio + PORT_FIS_ADDR_HI);
  640. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  641. /* enable FIS reception */
  642. tmp = readl(port_mmio + PORT_CMD);
  643. tmp |= PORT_CMD_FIS_RX;
  644. writel(tmp, port_mmio + PORT_CMD);
  645. /* flush */
  646. readl(port_mmio + PORT_CMD);
  647. }
  648. static int ahci_stop_fis_rx(struct ata_port *ap)
  649. {
  650. void __iomem *port_mmio = ahci_port_base(ap);
  651. u32 tmp;
  652. /* disable FIS reception */
  653. tmp = readl(port_mmio + PORT_CMD);
  654. tmp &= ~PORT_CMD_FIS_RX;
  655. writel(tmp, port_mmio + PORT_CMD);
  656. /* wait for completion, spec says 500ms, give it 1000 */
  657. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  658. PORT_CMD_FIS_ON, 10, 1000);
  659. if (tmp & PORT_CMD_FIS_ON)
  660. return -EBUSY;
  661. return 0;
  662. }
  663. static void ahci_power_up(struct ata_port *ap)
  664. {
  665. struct ahci_host_priv *hpriv = ap->host->private_data;
  666. void __iomem *port_mmio = ahci_port_base(ap);
  667. u32 cmd;
  668. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  669. /* spin up device */
  670. if (hpriv->cap & HOST_CAP_SSS) {
  671. cmd |= PORT_CMD_SPIN_UP;
  672. writel(cmd, port_mmio + PORT_CMD);
  673. }
  674. /* wake up link */
  675. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  676. }
  677. #ifdef CONFIG_PM
  678. static void ahci_power_down(struct ata_port *ap)
  679. {
  680. struct ahci_host_priv *hpriv = ap->host->private_data;
  681. void __iomem *port_mmio = ahci_port_base(ap);
  682. u32 cmd, scontrol;
  683. if (!(hpriv->cap & HOST_CAP_SSS))
  684. return;
  685. /* put device into listen mode, first set PxSCTL.DET to 0 */
  686. scontrol = readl(port_mmio + PORT_SCR_CTL);
  687. scontrol &= ~0xf;
  688. writel(scontrol, port_mmio + PORT_SCR_CTL);
  689. /* then set PxCMD.SUD to 0 */
  690. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  691. cmd &= ~PORT_CMD_SPIN_UP;
  692. writel(cmd, port_mmio + PORT_CMD);
  693. }
  694. #endif
  695. static void ahci_start_port(struct ata_port *ap)
  696. {
  697. /* enable FIS reception */
  698. ahci_start_fis_rx(ap);
  699. /* enable DMA */
  700. ahci_start_engine(ap);
  701. }
  702. static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
  703. {
  704. int rc;
  705. /* disable DMA */
  706. rc = ahci_stop_engine(ap);
  707. if (rc) {
  708. *emsg = "failed to stop engine";
  709. return rc;
  710. }
  711. /* disable FIS reception */
  712. rc = ahci_stop_fis_rx(ap);
  713. if (rc) {
  714. *emsg = "failed stop FIS RX";
  715. return rc;
  716. }
  717. return 0;
  718. }
  719. static int ahci_reset_controller(struct ata_host *host)
  720. {
  721. struct pci_dev *pdev = to_pci_dev(host->dev);
  722. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  723. u32 tmp;
  724. /* global controller reset */
  725. tmp = readl(mmio + HOST_CTL);
  726. if ((tmp & HOST_RESET) == 0) {
  727. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  728. readl(mmio + HOST_CTL); /* flush */
  729. }
  730. /* reset must complete within 1 second, or
  731. * the hardware should be considered fried.
  732. */
  733. ssleep(1);
  734. tmp = readl(mmio + HOST_CTL);
  735. if (tmp & HOST_RESET) {
  736. dev_printk(KERN_ERR, host->dev,
  737. "controller reset failed (0x%x)\n", tmp);
  738. return -EIO;
  739. }
  740. /* turn on AHCI mode */
  741. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  742. (void) readl(mmio + HOST_CTL); /* flush */
  743. /* some registers might be cleared on reset. restore initial values */
  744. ahci_restore_initial_config(host);
  745. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  746. u16 tmp16;
  747. /* configure PCS */
  748. pci_read_config_word(pdev, 0x92, &tmp16);
  749. tmp16 |= 0xf;
  750. pci_write_config_word(pdev, 0x92, tmp16);
  751. }
  752. return 0;
  753. }
  754. static void ahci_port_init(struct pci_dev *pdev, struct ata_port *ap,
  755. int port_no, void __iomem *mmio,
  756. void __iomem *port_mmio)
  757. {
  758. const char *emsg = NULL;
  759. int rc;
  760. u32 tmp;
  761. /* make sure port is not active */
  762. rc = ahci_deinit_port(ap, &emsg);
  763. if (rc)
  764. dev_printk(KERN_WARNING, &pdev->dev,
  765. "%s (%d)\n", emsg, rc);
  766. /* clear SError */
  767. tmp = readl(port_mmio + PORT_SCR_ERR);
  768. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  769. writel(tmp, port_mmio + PORT_SCR_ERR);
  770. /* clear port IRQ */
  771. tmp = readl(port_mmio + PORT_IRQ_STAT);
  772. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  773. if (tmp)
  774. writel(tmp, port_mmio + PORT_IRQ_STAT);
  775. writel(1 << port_no, mmio + HOST_IRQ_STAT);
  776. }
  777. static void ahci_init_controller(struct ata_host *host)
  778. {
  779. struct pci_dev *pdev = to_pci_dev(host->dev);
  780. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  781. int i;
  782. void __iomem *port_mmio;
  783. u32 tmp;
  784. if (host->ports[0]->flags & AHCI_FLAG_MV_PATA) {
  785. port_mmio = __ahci_port_base(host, 4);
  786. writel(0, port_mmio + PORT_IRQ_MASK);
  787. /* clear port IRQ */
  788. tmp = readl(port_mmio + PORT_IRQ_STAT);
  789. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  790. if (tmp)
  791. writel(tmp, port_mmio + PORT_IRQ_STAT);
  792. }
  793. for (i = 0; i < host->n_ports; i++) {
  794. struct ata_port *ap = host->ports[i];
  795. port_mmio = ahci_port_base(ap);
  796. if (ata_port_is_dummy(ap))
  797. continue;
  798. ahci_port_init(pdev, ap, i, mmio, port_mmio);
  799. }
  800. tmp = readl(mmio + HOST_CTL);
  801. VPRINTK("HOST_CTL 0x%x\n", tmp);
  802. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  803. tmp = readl(mmio + HOST_CTL);
  804. VPRINTK("HOST_CTL 0x%x\n", tmp);
  805. }
  806. static unsigned int ahci_dev_classify(struct ata_port *ap)
  807. {
  808. void __iomem *port_mmio = ahci_port_base(ap);
  809. struct ata_taskfile tf;
  810. u32 tmp;
  811. tmp = readl(port_mmio + PORT_SIG);
  812. tf.lbah = (tmp >> 24) & 0xff;
  813. tf.lbam = (tmp >> 16) & 0xff;
  814. tf.lbal = (tmp >> 8) & 0xff;
  815. tf.nsect = (tmp) & 0xff;
  816. return ata_dev_classify(&tf);
  817. }
  818. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  819. u32 opts)
  820. {
  821. dma_addr_t cmd_tbl_dma;
  822. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  823. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  824. pp->cmd_slot[tag].status = 0;
  825. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  826. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  827. }
  828. static int ahci_kick_engine(struct ata_port *ap, int force_restart)
  829. {
  830. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  831. struct ahci_host_priv *hpriv = ap->host->private_data;
  832. u32 tmp;
  833. int busy, rc;
  834. /* do we need to kick the port? */
  835. busy = ahci_check_status(ap) & (ATA_BUSY | ATA_DRQ);
  836. if (!busy && !force_restart)
  837. return 0;
  838. /* stop engine */
  839. rc = ahci_stop_engine(ap);
  840. if (rc)
  841. goto out_restart;
  842. /* need to do CLO? */
  843. if (!busy) {
  844. rc = 0;
  845. goto out_restart;
  846. }
  847. if (!(hpriv->cap & HOST_CAP_CLO)) {
  848. rc = -EOPNOTSUPP;
  849. goto out_restart;
  850. }
  851. /* perform CLO */
  852. tmp = readl(port_mmio + PORT_CMD);
  853. tmp |= PORT_CMD_CLO;
  854. writel(tmp, port_mmio + PORT_CMD);
  855. rc = 0;
  856. tmp = ata_wait_register(port_mmio + PORT_CMD,
  857. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  858. if (tmp & PORT_CMD_CLO)
  859. rc = -EIO;
  860. /* restart engine */
  861. out_restart:
  862. ahci_start_engine(ap);
  863. return rc;
  864. }
  865. static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
  866. struct ata_taskfile *tf, int is_cmd, u16 flags,
  867. unsigned long timeout_msec)
  868. {
  869. const u32 cmd_fis_len = 5; /* five dwords */
  870. struct ahci_port_priv *pp = ap->private_data;
  871. void __iomem *port_mmio = ahci_port_base(ap);
  872. u8 *fis = pp->cmd_tbl;
  873. u32 tmp;
  874. /* prep the command */
  875. ata_tf_to_fis(tf, pmp, is_cmd, fis);
  876. ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
  877. /* issue & wait */
  878. writel(1, port_mmio + PORT_CMD_ISSUE);
  879. if (timeout_msec) {
  880. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1,
  881. 1, timeout_msec);
  882. if (tmp & 0x1) {
  883. ahci_kick_engine(ap, 1);
  884. return -EBUSY;
  885. }
  886. } else
  887. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  888. return 0;
  889. }
  890. static int ahci_do_softreset(struct ata_port *ap, unsigned int *class,
  891. int pmp, unsigned long deadline)
  892. {
  893. const char *reason = NULL;
  894. unsigned long now, msecs;
  895. struct ata_taskfile tf;
  896. int rc;
  897. DPRINTK("ENTER\n");
  898. if (ata_port_offline(ap)) {
  899. DPRINTK("PHY reports no device\n");
  900. *class = ATA_DEV_NONE;
  901. return 0;
  902. }
  903. /* prepare for SRST (AHCI-1.1 10.4.1) */
  904. rc = ahci_kick_engine(ap, 1);
  905. if (rc)
  906. ata_port_printk(ap, KERN_WARNING,
  907. "failed to reset engine (errno=%d)", rc);
  908. ata_tf_init(ap->device, &tf);
  909. /* issue the first D2H Register FIS */
  910. msecs = 0;
  911. now = jiffies;
  912. if (time_after(now, deadline))
  913. msecs = jiffies_to_msecs(deadline - now);
  914. tf.ctl |= ATA_SRST;
  915. if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
  916. AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
  917. rc = -EIO;
  918. reason = "1st FIS failed";
  919. goto fail;
  920. }
  921. /* spec says at least 5us, but be generous and sleep for 1ms */
  922. msleep(1);
  923. /* issue the second D2H Register FIS */
  924. tf.ctl &= ~ATA_SRST;
  925. ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
  926. /* spec mandates ">= 2ms" before checking status.
  927. * We wait 150ms, because that was the magic delay used for
  928. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  929. * between when the ATA command register is written, and then
  930. * status is checked. Because waiting for "a while" before
  931. * checking status is fine, post SRST, we perform this magic
  932. * delay here as well.
  933. */
  934. msleep(150);
  935. rc = ata_wait_ready(ap, deadline);
  936. /* link occupied, -ENODEV too is an error */
  937. if (rc) {
  938. reason = "device not ready";
  939. goto fail;
  940. }
  941. *class = ahci_dev_classify(ap);
  942. DPRINTK("EXIT, class=%u\n", *class);
  943. return 0;
  944. fail:
  945. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  946. return rc;
  947. }
  948. static int ahci_softreset(struct ata_port *ap, unsigned int *class,
  949. unsigned long deadline)
  950. {
  951. return ahci_do_softreset(ap, class, 0, deadline);
  952. }
  953. static int ahci_hardreset(struct ata_port *ap, unsigned int *class,
  954. unsigned long deadline)
  955. {
  956. struct ahci_port_priv *pp = ap->private_data;
  957. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  958. struct ata_taskfile tf;
  959. int rc;
  960. DPRINTK("ENTER\n");
  961. ahci_stop_engine(ap);
  962. /* clear D2H reception area to properly wait for D2H FIS */
  963. ata_tf_init(ap->device, &tf);
  964. tf.command = 0x80;
  965. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  966. rc = sata_std_hardreset(ap, class, deadline);
  967. ahci_start_engine(ap);
  968. if (rc == 0 && ata_port_online(ap))
  969. *class = ahci_dev_classify(ap);
  970. if (*class == ATA_DEV_UNKNOWN)
  971. *class = ATA_DEV_NONE;
  972. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  973. return rc;
  974. }
  975. static int ahci_vt8251_hardreset(struct ata_port *ap, unsigned int *class,
  976. unsigned long deadline)
  977. {
  978. u32 serror;
  979. int rc;
  980. DPRINTK("ENTER\n");
  981. ahci_stop_engine(ap);
  982. rc = sata_port_hardreset(ap, sata_ehc_deb_timing(&ap->eh_context),
  983. deadline);
  984. /* vt8251 needs SError cleared for the port to operate */
  985. ahci_scr_read(ap, SCR_ERROR, &serror);
  986. ahci_scr_write(ap, SCR_ERROR, serror);
  987. ahci_start_engine(ap);
  988. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  989. /* vt8251 doesn't clear BSY on signature FIS reception,
  990. * request follow-up softreset.
  991. */
  992. return rc ?: -EAGAIN;
  993. }
  994. static void ahci_postreset(struct ata_port *ap, unsigned int *class)
  995. {
  996. void __iomem *port_mmio = ahci_port_base(ap);
  997. u32 new_tmp, tmp;
  998. ata_std_postreset(ap, class);
  999. /* Make sure port's ATAPI bit is set appropriately */
  1000. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  1001. if (*class == ATA_DEV_ATAPI)
  1002. new_tmp |= PORT_CMD_ATAPI;
  1003. else
  1004. new_tmp &= ~PORT_CMD_ATAPI;
  1005. if (new_tmp != tmp) {
  1006. writel(new_tmp, port_mmio + PORT_CMD);
  1007. readl(port_mmio + PORT_CMD); /* flush */
  1008. }
  1009. }
  1010. static u8 ahci_check_status(struct ata_port *ap)
  1011. {
  1012. void __iomem *mmio = ap->ioaddr.cmd_addr;
  1013. return readl(mmio + PORT_TFDATA) & 0xFF;
  1014. }
  1015. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  1016. {
  1017. struct ahci_port_priv *pp = ap->private_data;
  1018. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1019. ata_tf_from_fis(d2h_fis, tf);
  1020. }
  1021. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  1022. {
  1023. struct scatterlist *sg;
  1024. struct ahci_sg *ahci_sg;
  1025. unsigned int n_sg = 0;
  1026. VPRINTK("ENTER\n");
  1027. /*
  1028. * Next, the S/G list.
  1029. */
  1030. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  1031. ata_for_each_sg(sg, qc) {
  1032. dma_addr_t addr = sg_dma_address(sg);
  1033. u32 sg_len = sg_dma_len(sg);
  1034. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1035. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1036. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  1037. ahci_sg++;
  1038. n_sg++;
  1039. }
  1040. return n_sg;
  1041. }
  1042. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  1043. {
  1044. struct ata_port *ap = qc->ap;
  1045. struct ahci_port_priv *pp = ap->private_data;
  1046. int is_atapi = is_atapi_taskfile(&qc->tf);
  1047. void *cmd_tbl;
  1048. u32 opts;
  1049. const u32 cmd_fis_len = 5; /* five dwords */
  1050. unsigned int n_elem;
  1051. /*
  1052. * Fill in command table information. First, the header,
  1053. * a SATA Register - Host to Device command FIS.
  1054. */
  1055. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  1056. ata_tf_to_fis(&qc->tf, 0, 1, cmd_tbl);
  1057. if (is_atapi) {
  1058. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  1059. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  1060. }
  1061. n_elem = 0;
  1062. if (qc->flags & ATA_QCFLAG_DMAMAP)
  1063. n_elem = ahci_fill_sg(qc, cmd_tbl);
  1064. /*
  1065. * Fill in command slot information.
  1066. */
  1067. opts = cmd_fis_len | n_elem << 16;
  1068. if (qc->tf.flags & ATA_TFLAG_WRITE)
  1069. opts |= AHCI_CMD_WRITE;
  1070. if (is_atapi)
  1071. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  1072. ahci_fill_cmd_slot(pp, qc->tag, opts);
  1073. }
  1074. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  1075. {
  1076. struct ahci_port_priv *pp = ap->private_data;
  1077. struct ata_eh_info *ehi = &ap->eh_info;
  1078. unsigned int err_mask = 0, action = 0;
  1079. struct ata_queued_cmd *qc;
  1080. u32 serror;
  1081. ata_ehi_clear_desc(ehi);
  1082. /* AHCI needs SError cleared; otherwise, it might lock up */
  1083. ahci_scr_read(ap, SCR_ERROR, &serror);
  1084. ahci_scr_write(ap, SCR_ERROR, serror);
  1085. /* analyze @irq_stat */
  1086. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  1087. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  1088. if (ap->flags & AHCI_FLAG_IGN_IRQ_IF_ERR)
  1089. irq_stat &= ~PORT_IRQ_IF_ERR;
  1090. if (irq_stat & PORT_IRQ_TF_ERR) {
  1091. err_mask |= AC_ERR_DEV;
  1092. if (ap->flags & AHCI_FLAG_IGN_SERR_INTERNAL)
  1093. serror &= ~SERR_INTERNAL;
  1094. }
  1095. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  1096. err_mask |= AC_ERR_HOST_BUS;
  1097. action |= ATA_EH_SOFTRESET;
  1098. }
  1099. if (irq_stat & PORT_IRQ_IF_ERR) {
  1100. err_mask |= AC_ERR_ATA_BUS;
  1101. action |= ATA_EH_SOFTRESET;
  1102. ata_ehi_push_desc(ehi, "interface fatal error");
  1103. }
  1104. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  1105. ata_ehi_hotplugged(ehi);
  1106. ata_ehi_push_desc(ehi, "%s", irq_stat & PORT_IRQ_CONNECT ?
  1107. "connection status changed" : "PHY RDY changed");
  1108. }
  1109. if (irq_stat & PORT_IRQ_UNK_FIS) {
  1110. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  1111. err_mask |= AC_ERR_HSM;
  1112. action |= ATA_EH_SOFTRESET;
  1113. ata_ehi_push_desc(ehi, "unknown FIS %08x %08x %08x %08x",
  1114. unk[0], unk[1], unk[2], unk[3]);
  1115. }
  1116. /* okay, let's hand over to EH */
  1117. ehi->serror |= serror;
  1118. ehi->action |= action;
  1119. qc = ata_qc_from_tag(ap, ap->active_tag);
  1120. if (qc)
  1121. qc->err_mask |= err_mask;
  1122. else
  1123. ehi->err_mask |= err_mask;
  1124. if (irq_stat & PORT_IRQ_FREEZE)
  1125. ata_port_freeze(ap);
  1126. else
  1127. ata_port_abort(ap);
  1128. }
  1129. static void ahci_port_intr(struct ata_port *ap)
  1130. {
  1131. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  1132. struct ata_eh_info *ehi = &ap->eh_info;
  1133. struct ahci_port_priv *pp = ap->private_data;
  1134. u32 status, qc_active;
  1135. int rc, known_irq = 0;
  1136. status = readl(port_mmio + PORT_IRQ_STAT);
  1137. writel(status, port_mmio + PORT_IRQ_STAT);
  1138. if (unlikely(status & PORT_IRQ_ERROR)) {
  1139. ahci_error_intr(ap, status);
  1140. return;
  1141. }
  1142. if (ap->sactive)
  1143. qc_active = readl(port_mmio + PORT_SCR_ACT);
  1144. else
  1145. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  1146. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  1147. if (rc > 0)
  1148. return;
  1149. if (rc < 0) {
  1150. ehi->err_mask |= AC_ERR_HSM;
  1151. ehi->action |= ATA_EH_SOFTRESET;
  1152. ata_port_freeze(ap);
  1153. return;
  1154. }
  1155. /* hmmm... a spurious interupt */
  1156. /* if !NCQ, ignore. No modern ATA device has broken HSM
  1157. * implementation for non-NCQ commands.
  1158. */
  1159. if (!ap->sactive)
  1160. return;
  1161. if (status & PORT_IRQ_D2H_REG_FIS) {
  1162. if (!pp->ncq_saw_d2h)
  1163. ata_port_printk(ap, KERN_INFO,
  1164. "D2H reg with I during NCQ, "
  1165. "this message won't be printed again\n");
  1166. pp->ncq_saw_d2h = 1;
  1167. known_irq = 1;
  1168. }
  1169. if (status & PORT_IRQ_DMAS_FIS) {
  1170. if (!pp->ncq_saw_dmas)
  1171. ata_port_printk(ap, KERN_INFO,
  1172. "DMAS FIS during NCQ, "
  1173. "this message won't be printed again\n");
  1174. pp->ncq_saw_dmas = 1;
  1175. known_irq = 1;
  1176. }
  1177. if (status & PORT_IRQ_SDB_FIS) {
  1178. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1179. if (le32_to_cpu(f[1])) {
  1180. /* SDB FIS containing spurious completions
  1181. * might be dangerous, whine and fail commands
  1182. * with HSM violation. EH will turn off NCQ
  1183. * after several such failures.
  1184. */
  1185. ata_ehi_push_desc(ehi,
  1186. "spurious completions during NCQ "
  1187. "issue=0x%x SAct=0x%x FIS=%08x:%08x",
  1188. readl(port_mmio + PORT_CMD_ISSUE),
  1189. readl(port_mmio + PORT_SCR_ACT),
  1190. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1191. ehi->err_mask |= AC_ERR_HSM;
  1192. ehi->action |= ATA_EH_SOFTRESET;
  1193. ata_port_freeze(ap);
  1194. } else {
  1195. if (!pp->ncq_saw_sdb)
  1196. ata_port_printk(ap, KERN_INFO,
  1197. "spurious SDB FIS %08x:%08x during NCQ, "
  1198. "this message won't be printed again\n",
  1199. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1200. pp->ncq_saw_sdb = 1;
  1201. }
  1202. known_irq = 1;
  1203. }
  1204. if (!known_irq)
  1205. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  1206. "(irq_stat 0x%x active_tag 0x%x sactive 0x%x)\n",
  1207. status, ap->active_tag, ap->sactive);
  1208. }
  1209. static void ahci_irq_clear(struct ata_port *ap)
  1210. {
  1211. /* TODO */
  1212. }
  1213. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  1214. {
  1215. struct ata_host *host = dev_instance;
  1216. struct ahci_host_priv *hpriv;
  1217. unsigned int i, handled = 0;
  1218. void __iomem *mmio;
  1219. u32 irq_stat, irq_ack = 0;
  1220. VPRINTK("ENTER\n");
  1221. hpriv = host->private_data;
  1222. mmio = host->iomap[AHCI_PCI_BAR];
  1223. /* sigh. 0xffffffff is a valid return from h/w */
  1224. irq_stat = readl(mmio + HOST_IRQ_STAT);
  1225. irq_stat &= hpriv->port_map;
  1226. if (!irq_stat)
  1227. return IRQ_NONE;
  1228. spin_lock(&host->lock);
  1229. for (i = 0; i < host->n_ports; i++) {
  1230. struct ata_port *ap;
  1231. if (!(irq_stat & (1 << i)))
  1232. continue;
  1233. ap = host->ports[i];
  1234. if (ap) {
  1235. ahci_port_intr(ap);
  1236. VPRINTK("port %u\n", i);
  1237. } else {
  1238. VPRINTK("port %u (no irq)\n", i);
  1239. if (ata_ratelimit())
  1240. dev_printk(KERN_WARNING, host->dev,
  1241. "interrupt on disabled port %u\n", i);
  1242. }
  1243. irq_ack |= (1 << i);
  1244. }
  1245. if (irq_ack) {
  1246. writel(irq_ack, mmio + HOST_IRQ_STAT);
  1247. handled = 1;
  1248. }
  1249. spin_unlock(&host->lock);
  1250. VPRINTK("EXIT\n");
  1251. return IRQ_RETVAL(handled);
  1252. }
  1253. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1254. {
  1255. struct ata_port *ap = qc->ap;
  1256. void __iomem *port_mmio = ahci_port_base(ap);
  1257. if (qc->tf.protocol == ATA_PROT_NCQ)
  1258. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  1259. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  1260. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1261. return 0;
  1262. }
  1263. static void ahci_freeze(struct ata_port *ap)
  1264. {
  1265. void __iomem *port_mmio = ahci_port_base(ap);
  1266. /* turn IRQ off */
  1267. writel(0, port_mmio + PORT_IRQ_MASK);
  1268. }
  1269. static void ahci_thaw(struct ata_port *ap)
  1270. {
  1271. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1272. void __iomem *port_mmio = ahci_port_base(ap);
  1273. u32 tmp;
  1274. /* clear IRQ */
  1275. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1276. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1277. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1278. /* turn IRQ back on */
  1279. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  1280. }
  1281. static void ahci_error_handler(struct ata_port *ap)
  1282. {
  1283. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1284. /* restart engine */
  1285. ahci_stop_engine(ap);
  1286. ahci_start_engine(ap);
  1287. }
  1288. /* perform recovery */
  1289. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_hardreset,
  1290. ahci_postreset);
  1291. }
  1292. static void ahci_vt8251_error_handler(struct ata_port *ap)
  1293. {
  1294. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1295. /* restart engine */
  1296. ahci_stop_engine(ap);
  1297. ahci_start_engine(ap);
  1298. }
  1299. /* perform recovery */
  1300. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_vt8251_hardreset,
  1301. ahci_postreset);
  1302. }
  1303. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1304. {
  1305. struct ata_port *ap = qc->ap;
  1306. /* make DMA engine forget about the failed command */
  1307. if (qc->flags & ATA_QCFLAG_FAILED)
  1308. ahci_kick_engine(ap, 1);
  1309. }
  1310. static int ahci_port_resume(struct ata_port *ap)
  1311. {
  1312. ahci_power_up(ap);
  1313. ahci_start_port(ap);
  1314. return 0;
  1315. }
  1316. #ifdef CONFIG_PM
  1317. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1318. {
  1319. const char *emsg = NULL;
  1320. int rc;
  1321. rc = ahci_deinit_port(ap, &emsg);
  1322. if (rc == 0)
  1323. ahci_power_down(ap);
  1324. else {
  1325. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  1326. ahci_start_port(ap);
  1327. }
  1328. return rc;
  1329. }
  1330. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1331. {
  1332. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1333. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1334. u32 ctl;
  1335. if (mesg.event == PM_EVENT_SUSPEND) {
  1336. /* AHCI spec rev1.1 section 8.3.3:
  1337. * Software must disable interrupts prior to requesting a
  1338. * transition of the HBA to D3 state.
  1339. */
  1340. ctl = readl(mmio + HOST_CTL);
  1341. ctl &= ~HOST_IRQ_EN;
  1342. writel(ctl, mmio + HOST_CTL);
  1343. readl(mmio + HOST_CTL); /* flush */
  1344. }
  1345. return ata_pci_device_suspend(pdev, mesg);
  1346. }
  1347. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1348. {
  1349. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1350. int rc;
  1351. rc = ata_pci_device_do_resume(pdev);
  1352. if (rc)
  1353. return rc;
  1354. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1355. rc = ahci_reset_controller(host);
  1356. if (rc)
  1357. return rc;
  1358. ahci_init_controller(host);
  1359. }
  1360. ata_host_resume(host);
  1361. return 0;
  1362. }
  1363. #endif
  1364. static int ahci_port_start(struct ata_port *ap)
  1365. {
  1366. struct device *dev = ap->host->dev;
  1367. struct ahci_port_priv *pp;
  1368. void *mem;
  1369. dma_addr_t mem_dma;
  1370. int rc;
  1371. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1372. if (!pp)
  1373. return -ENOMEM;
  1374. rc = ata_pad_alloc(ap, dev);
  1375. if (rc)
  1376. return rc;
  1377. mem = dmam_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma,
  1378. GFP_KERNEL);
  1379. if (!mem)
  1380. return -ENOMEM;
  1381. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1382. /*
  1383. * First item in chunk of DMA memory: 32-slot command table,
  1384. * 32 bytes each in size
  1385. */
  1386. pp->cmd_slot = mem;
  1387. pp->cmd_slot_dma = mem_dma;
  1388. mem += AHCI_CMD_SLOT_SZ;
  1389. mem_dma += AHCI_CMD_SLOT_SZ;
  1390. /*
  1391. * Second item: Received-FIS area
  1392. */
  1393. pp->rx_fis = mem;
  1394. pp->rx_fis_dma = mem_dma;
  1395. mem += AHCI_RX_FIS_SZ;
  1396. mem_dma += AHCI_RX_FIS_SZ;
  1397. /*
  1398. * Third item: data area for storing a single command
  1399. * and its scatter-gather table
  1400. */
  1401. pp->cmd_tbl = mem;
  1402. pp->cmd_tbl_dma = mem_dma;
  1403. ap->private_data = pp;
  1404. /* engage engines, captain */
  1405. return ahci_port_resume(ap);
  1406. }
  1407. static void ahci_port_stop(struct ata_port *ap)
  1408. {
  1409. const char *emsg = NULL;
  1410. int rc;
  1411. /* de-initialize port */
  1412. rc = ahci_deinit_port(ap, &emsg);
  1413. if (rc)
  1414. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1415. }
  1416. static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
  1417. {
  1418. int rc;
  1419. if (using_dac &&
  1420. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1421. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1422. if (rc) {
  1423. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1424. if (rc) {
  1425. dev_printk(KERN_ERR, &pdev->dev,
  1426. "64-bit DMA enable failed\n");
  1427. return rc;
  1428. }
  1429. }
  1430. } else {
  1431. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1432. if (rc) {
  1433. dev_printk(KERN_ERR, &pdev->dev,
  1434. "32-bit DMA enable failed\n");
  1435. return rc;
  1436. }
  1437. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1438. if (rc) {
  1439. dev_printk(KERN_ERR, &pdev->dev,
  1440. "32-bit consistent DMA enable failed\n");
  1441. return rc;
  1442. }
  1443. }
  1444. return 0;
  1445. }
  1446. static void ahci_print_info(struct ata_host *host)
  1447. {
  1448. struct ahci_host_priv *hpriv = host->private_data;
  1449. struct pci_dev *pdev = to_pci_dev(host->dev);
  1450. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1451. u32 vers, cap, impl, speed;
  1452. const char *speed_s;
  1453. u16 cc;
  1454. const char *scc_s;
  1455. vers = readl(mmio + HOST_VERSION);
  1456. cap = hpriv->cap;
  1457. impl = hpriv->port_map;
  1458. speed = (cap >> 20) & 0xf;
  1459. if (speed == 1)
  1460. speed_s = "1.5";
  1461. else if (speed == 2)
  1462. speed_s = "3";
  1463. else
  1464. speed_s = "?";
  1465. pci_read_config_word(pdev, 0x0a, &cc);
  1466. if (cc == PCI_CLASS_STORAGE_IDE)
  1467. scc_s = "IDE";
  1468. else if (cc == PCI_CLASS_STORAGE_SATA)
  1469. scc_s = "SATA";
  1470. else if (cc == PCI_CLASS_STORAGE_RAID)
  1471. scc_s = "RAID";
  1472. else
  1473. scc_s = "unknown";
  1474. dev_printk(KERN_INFO, &pdev->dev,
  1475. "AHCI %02x%02x.%02x%02x "
  1476. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1477. ,
  1478. (vers >> 24) & 0xff,
  1479. (vers >> 16) & 0xff,
  1480. (vers >> 8) & 0xff,
  1481. vers & 0xff,
  1482. ((cap >> 8) & 0x1f) + 1,
  1483. (cap & 0x1f) + 1,
  1484. speed_s,
  1485. impl,
  1486. scc_s);
  1487. dev_printk(KERN_INFO, &pdev->dev,
  1488. "flags: "
  1489. "%s%s%s%s%s%s%s"
  1490. "%s%s%s%s%s%s%s\n"
  1491. ,
  1492. cap & (1 << 31) ? "64bit " : "",
  1493. cap & (1 << 30) ? "ncq " : "",
  1494. cap & (1 << 29) ? "sntf " : "",
  1495. cap & (1 << 28) ? "ilck " : "",
  1496. cap & (1 << 27) ? "stag " : "",
  1497. cap & (1 << 26) ? "pm " : "",
  1498. cap & (1 << 25) ? "led " : "",
  1499. cap & (1 << 24) ? "clo " : "",
  1500. cap & (1 << 19) ? "nz " : "",
  1501. cap & (1 << 18) ? "only " : "",
  1502. cap & (1 << 17) ? "pmp " : "",
  1503. cap & (1 << 15) ? "pio " : "",
  1504. cap & (1 << 14) ? "slum " : "",
  1505. cap & (1 << 13) ? "part " : ""
  1506. );
  1507. }
  1508. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1509. {
  1510. static int printed_version;
  1511. struct ata_port_info pi = ahci_port_info[ent->driver_data];
  1512. const struct ata_port_info *ppi[] = { &pi, NULL };
  1513. struct device *dev = &pdev->dev;
  1514. struct ahci_host_priv *hpriv;
  1515. struct ata_host *host;
  1516. int i, rc;
  1517. VPRINTK("ENTER\n");
  1518. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1519. if (!printed_version++)
  1520. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1521. /* acquire resources */
  1522. rc = pcim_enable_device(pdev);
  1523. if (rc)
  1524. return rc;
  1525. rc = pcim_iomap_regions(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
  1526. if (rc == -EBUSY)
  1527. pcim_pin_device(pdev);
  1528. if (rc)
  1529. return rc;
  1530. if ((pi.flags & AHCI_FLAG_NO_MSI) || pci_enable_msi(pdev))
  1531. pci_intx(pdev, 1);
  1532. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  1533. if (!hpriv)
  1534. return -ENOMEM;
  1535. /* save initial config */
  1536. ahci_save_initial_config(pdev, &pi, hpriv);
  1537. /* prepare host */
  1538. if (hpriv->cap & HOST_CAP_NCQ)
  1539. pi.flags |= ATA_FLAG_NCQ;
  1540. host = ata_host_alloc_pinfo(&pdev->dev, ppi, fls(hpriv->port_map));
  1541. if (!host)
  1542. return -ENOMEM;
  1543. host->iomap = pcim_iomap_table(pdev);
  1544. host->private_data = hpriv;
  1545. for (i = 0; i < host->n_ports; i++) {
  1546. struct ata_port *ap = host->ports[i];
  1547. void __iomem *port_mmio = ahci_port_base(ap);
  1548. /* standard SATA port setup */
  1549. if (hpriv->port_map & (1 << i))
  1550. ap->ioaddr.cmd_addr = port_mmio;
  1551. /* disabled/not-implemented port */
  1552. else
  1553. ap->ops = &ata_dummy_port_ops;
  1554. }
  1555. /* initialize adapter */
  1556. rc = ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
  1557. if (rc)
  1558. return rc;
  1559. rc = ahci_reset_controller(host);
  1560. if (rc)
  1561. return rc;
  1562. ahci_init_controller(host);
  1563. ahci_print_info(host);
  1564. pci_set_master(pdev);
  1565. return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
  1566. &ahci_sht);
  1567. }
  1568. static int __init ahci_init(void)
  1569. {
  1570. return pci_register_driver(&ahci_pci_driver);
  1571. }
  1572. static void __exit ahci_exit(void)
  1573. {
  1574. pci_unregister_driver(&ahci_pci_driver);
  1575. }
  1576. MODULE_AUTHOR("Jeff Garzik");
  1577. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1578. MODULE_LICENSE("GPL");
  1579. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1580. MODULE_VERSION(DRV_VERSION);
  1581. module_init(ahci_init);
  1582. module_exit(ahci_exit);