ahci.c 43 KB

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