ahci.c 44 KB

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