ahci.c 43 KB

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