ahci.c 43 KB

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