ahci.c 43 KB

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