ahci.c 41 KB

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