ahci.c 38 KB

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