ahci.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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/dma-mapping.h>
  42. #include <linux/device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <linux/libata.h>
  46. #define DRV_NAME "ahci"
  47. #define DRV_VERSION "3.0"
  48. enum {
  49. AHCI_PCI_BAR = 5,
  50. AHCI_MAX_PORTS = 32,
  51. AHCI_MAX_SG = 168, /* hardware max is 64K */
  52. AHCI_DMA_BOUNDARY = 0xffffffff,
  53. AHCI_USE_CLUSTERING = 1,
  54. AHCI_MAX_CMDS = 32,
  55. AHCI_CMD_SZ = 32,
  56. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  57. AHCI_RX_FIS_SZ = 256,
  58. AHCI_CMD_TBL_CDB = 0x40,
  59. AHCI_CMD_TBL_HDR_SZ = 0x80,
  60. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  61. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  62. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  63. AHCI_RX_FIS_SZ,
  64. AHCI_IRQ_ON_SG = (1 << 31),
  65. AHCI_CMD_ATAPI = (1 << 5),
  66. AHCI_CMD_WRITE = (1 << 6),
  67. AHCI_CMD_PREFETCH = (1 << 7),
  68. AHCI_CMD_RESET = (1 << 8),
  69. AHCI_CMD_CLR_BUSY = (1 << 10),
  70. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  71. RX_FIS_SDB = 0x58, /* offset of SDB 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. board_ahci_sb600 = 3,
  77. board_ahci_mv = 4,
  78. /* global controller registers */
  79. HOST_CAP = 0x00, /* host capabilities */
  80. HOST_CTL = 0x04, /* global host control */
  81. HOST_IRQ_STAT = 0x08, /* interrupt status */
  82. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  83. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  84. /* HOST_CTL bits */
  85. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  86. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  87. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  88. /* HOST_CAP bits */
  89. HOST_CAP_SSC = (1 << 14), /* Slumber capable */
  90. HOST_CAP_PMP = (1 << 17), /* Port Multiplier support */
  91. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  92. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  93. HOST_CAP_SNTF = (1 << 29), /* SNotification register */
  94. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  95. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  96. /* registers for each SATA port */
  97. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  98. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  99. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  100. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  101. PORT_IRQ_STAT = 0x10, /* interrupt status */
  102. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  103. PORT_CMD = 0x18, /* port command */
  104. PORT_TFDATA = 0x20, /* taskfile data */
  105. PORT_SIG = 0x24, /* device TF signature */
  106. PORT_CMD_ISSUE = 0x38, /* command issue */
  107. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  108. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  109. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  110. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  111. PORT_SCR_NTF = 0x3c, /* SATA phy register: SNotification */
  112. /* PORT_IRQ_{STAT,MASK} bits */
  113. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  114. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  115. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  116. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  117. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  118. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  119. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  120. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  121. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  122. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  123. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  124. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  125. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  126. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  127. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  128. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  129. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  130. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  131. PORT_IRQ_IF_ERR |
  132. PORT_IRQ_CONNECT |
  133. PORT_IRQ_PHYRDY |
  134. PORT_IRQ_UNK_FIS |
  135. PORT_IRQ_BAD_PMP,
  136. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  137. PORT_IRQ_TF_ERR |
  138. PORT_IRQ_HBUS_DATA_ERR,
  139. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  140. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  141. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  142. /* PORT_CMD bits */
  143. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  144. PORT_CMD_PMP = (1 << 17), /* PMP attached */
  145. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  146. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  147. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  148. PORT_CMD_CLO = (1 << 3), /* Command list override */
  149. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  150. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  151. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  152. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  153. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  154. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  155. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  156. /* hpriv->flags bits */
  157. AHCI_HFLAG_NO_NCQ = (1 << 0),
  158. AHCI_HFLAG_IGN_IRQ_IF_ERR = (1 << 1), /* ignore IRQ_IF_ERR */
  159. AHCI_HFLAG_IGN_SERR_INTERNAL = (1 << 2), /* ignore SERR_INTERNAL */
  160. AHCI_HFLAG_32BIT_ONLY = (1 << 3), /* force 32bit */
  161. AHCI_HFLAG_MV_PATA = (1 << 4), /* PATA port */
  162. AHCI_HFLAG_NO_MSI = (1 << 5), /* no PCI MSI */
  163. AHCI_HFLAG_NO_PMP = (1 << 6), /* no PMP */
  164. /* ap->flags bits */
  165. AHCI_FLAG_NO_HOTPLUG = (1 << 24), /* ignore PxSERR.DIAG.N */
  166. AHCI_FLAG_COMMON = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  167. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  168. ATA_FLAG_ACPI_SATA | ATA_FLAG_AN,
  169. AHCI_LFLAG_COMMON = ATA_LFLAG_SKIP_D2H_BSY,
  170. };
  171. struct ahci_cmd_hdr {
  172. u32 opts;
  173. u32 status;
  174. u32 tbl_addr;
  175. u32 tbl_addr_hi;
  176. u32 reserved[4];
  177. };
  178. struct ahci_sg {
  179. u32 addr;
  180. u32 addr_hi;
  181. u32 reserved;
  182. u32 flags_size;
  183. };
  184. struct ahci_host_priv {
  185. unsigned int flags; /* AHCI_HFLAG_* */
  186. u32 cap; /* cap to use */
  187. u32 port_map; /* port map to use */
  188. u32 saved_cap; /* saved initial cap */
  189. u32 saved_port_map; /* saved initial port_map */
  190. };
  191. struct ahci_port_priv {
  192. struct ata_link *active_link;
  193. struct ahci_cmd_hdr *cmd_slot;
  194. dma_addr_t cmd_slot_dma;
  195. void *cmd_tbl;
  196. dma_addr_t cmd_tbl_dma;
  197. void *rx_fis;
  198. dma_addr_t rx_fis_dma;
  199. /* for NCQ spurious interrupt analysis */
  200. unsigned int ncq_saw_d2h:1;
  201. unsigned int ncq_saw_dmas:1;
  202. unsigned int ncq_saw_sdb:1;
  203. u32 intr_mask; /* interrupts to enable */
  204. };
  205. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
  206. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
  207. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  208. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  209. static void ahci_irq_clear(struct ata_port *ap);
  210. static int ahci_port_start(struct ata_port *ap);
  211. static void ahci_port_stop(struct ata_port *ap);
  212. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  213. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  214. static u8 ahci_check_status(struct ata_port *ap);
  215. static void ahci_freeze(struct ata_port *ap);
  216. static void ahci_thaw(struct ata_port *ap);
  217. static void ahci_pmp_attach(struct ata_port *ap);
  218. static void ahci_pmp_detach(struct ata_port *ap);
  219. static int ahci_pmp_read(struct ata_device *dev, int pmp, int reg, u32 *r_val);
  220. static int ahci_pmp_write(struct ata_device *dev, int pmp, int reg, u32 val);
  221. static void ahci_error_handler(struct ata_port *ap);
  222. static void ahci_vt8251_error_handler(struct ata_port *ap);
  223. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  224. static int ahci_port_resume(struct ata_port *ap);
  225. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl);
  226. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  227. u32 opts);
  228. #ifdef CONFIG_PM
  229. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  230. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  231. static int ahci_pci_device_resume(struct pci_dev *pdev);
  232. #endif
  233. static struct scsi_host_template ahci_sht = {
  234. .module = THIS_MODULE,
  235. .name = DRV_NAME,
  236. .ioctl = ata_scsi_ioctl,
  237. .queuecommand = ata_scsi_queuecmd,
  238. .change_queue_depth = ata_scsi_change_queue_depth,
  239. .can_queue = AHCI_MAX_CMDS - 1,
  240. .this_id = ATA_SHT_THIS_ID,
  241. .sg_tablesize = AHCI_MAX_SG,
  242. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  243. .emulated = ATA_SHT_EMULATED,
  244. .use_clustering = AHCI_USE_CLUSTERING,
  245. .proc_name = DRV_NAME,
  246. .dma_boundary = AHCI_DMA_BOUNDARY,
  247. .slave_configure = ata_scsi_slave_config,
  248. .slave_destroy = ata_scsi_slave_destroy,
  249. .bios_param = ata_std_bios_param,
  250. };
  251. static const struct ata_port_operations ahci_ops = {
  252. .check_status = ahci_check_status,
  253. .check_altstatus = ahci_check_status,
  254. .dev_select = ata_noop_dev_select,
  255. .tf_read = ahci_tf_read,
  256. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  257. .qc_prep = ahci_qc_prep,
  258. .qc_issue = ahci_qc_issue,
  259. .irq_clear = ahci_irq_clear,
  260. .scr_read = ahci_scr_read,
  261. .scr_write = ahci_scr_write,
  262. .freeze = ahci_freeze,
  263. .thaw = ahci_thaw,
  264. .error_handler = ahci_error_handler,
  265. .post_internal_cmd = ahci_post_internal_cmd,
  266. .pmp_attach = ahci_pmp_attach,
  267. .pmp_detach = ahci_pmp_detach,
  268. .pmp_read = ahci_pmp_read,
  269. .pmp_write = ahci_pmp_write,
  270. #ifdef CONFIG_PM
  271. .port_suspend = ahci_port_suspend,
  272. .port_resume = ahci_port_resume,
  273. #endif
  274. .port_start = ahci_port_start,
  275. .port_stop = ahci_port_stop,
  276. };
  277. static const struct ata_port_operations ahci_vt8251_ops = {
  278. .check_status = ahci_check_status,
  279. .check_altstatus = ahci_check_status,
  280. .dev_select = ata_noop_dev_select,
  281. .tf_read = ahci_tf_read,
  282. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  283. .qc_prep = ahci_qc_prep,
  284. .qc_issue = ahci_qc_issue,
  285. .irq_clear = ahci_irq_clear,
  286. .scr_read = ahci_scr_read,
  287. .scr_write = ahci_scr_write,
  288. .freeze = ahci_freeze,
  289. .thaw = ahci_thaw,
  290. .error_handler = ahci_vt8251_error_handler,
  291. .post_internal_cmd = ahci_post_internal_cmd,
  292. .pmp_attach = ahci_pmp_attach,
  293. .pmp_detach = ahci_pmp_detach,
  294. .pmp_read = ahci_pmp_read,
  295. .pmp_write = ahci_pmp_write,
  296. #ifdef CONFIG_PM
  297. .port_suspend = ahci_port_suspend,
  298. .port_resume = ahci_port_resume,
  299. #endif
  300. .port_start = ahci_port_start,
  301. .port_stop = ahci_port_stop,
  302. };
  303. #define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
  304. static const struct ata_port_info ahci_port_info[] = {
  305. /* board_ahci */
  306. {
  307. .flags = AHCI_FLAG_COMMON,
  308. .link_flags = AHCI_LFLAG_COMMON,
  309. .pio_mask = 0x1f, /* pio0-4 */
  310. .udma_mask = ATA_UDMA6,
  311. .port_ops = &ahci_ops,
  312. },
  313. /* board_ahci_vt8251 */
  314. {
  315. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_PMP),
  316. .flags = AHCI_FLAG_COMMON,
  317. .link_flags = AHCI_LFLAG_COMMON | ATA_LFLAG_HRST_TO_RESUME,
  318. .pio_mask = 0x1f, /* pio0-4 */
  319. .udma_mask = ATA_UDMA6,
  320. .port_ops = &ahci_vt8251_ops,
  321. },
  322. /* board_ahci_ign_iferr */
  323. {
  324. AHCI_HFLAGS (AHCI_HFLAG_IGN_IRQ_IF_ERR),
  325. .flags = AHCI_FLAG_COMMON,
  326. .link_flags = AHCI_LFLAG_COMMON,
  327. .pio_mask = 0x1f, /* pio0-4 */
  328. .udma_mask = ATA_UDMA6,
  329. .port_ops = &ahci_ops,
  330. },
  331. /* board_ahci_sb600 */
  332. {
  333. AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL |
  334. AHCI_HFLAG_32BIT_ONLY | AHCI_HFLAG_NO_PMP),
  335. .flags = AHCI_FLAG_COMMON,
  336. .link_flags = AHCI_LFLAG_COMMON,
  337. .pio_mask = 0x1f, /* pio0-4 */
  338. .udma_mask = ATA_UDMA6,
  339. .port_ops = &ahci_ops,
  340. },
  341. /* board_ahci_mv */
  342. {
  343. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_MSI |
  344. AHCI_HFLAG_MV_PATA),
  345. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  346. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
  347. .link_flags = AHCI_LFLAG_COMMON,
  348. .pio_mask = 0x1f, /* pio0-4 */
  349. .udma_mask = ATA_UDMA6,
  350. .port_ops = &ahci_ops,
  351. },
  352. };
  353. static const struct pci_device_id ahci_pci_tbl[] = {
  354. /* Intel */
  355. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  356. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  357. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  358. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  359. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  360. { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
  361. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  362. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  363. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  364. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  365. { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
  366. { PCI_VDEVICE(INTEL, 0x2822), board_ahci }, /* ICH8 */
  367. { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
  368. { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
  369. { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
  370. { PCI_VDEVICE(INTEL, 0x2922), board_ahci }, /* ICH9 */
  371. { PCI_VDEVICE(INTEL, 0x2923), board_ahci }, /* ICH9 */
  372. { PCI_VDEVICE(INTEL, 0x2924), board_ahci }, /* ICH9 */
  373. { PCI_VDEVICE(INTEL, 0x2925), board_ahci }, /* ICH9 */
  374. { PCI_VDEVICE(INTEL, 0x2927), board_ahci }, /* ICH9 */
  375. { PCI_VDEVICE(INTEL, 0x2929), board_ahci }, /* ICH9M */
  376. { PCI_VDEVICE(INTEL, 0x292a), board_ahci }, /* ICH9M */
  377. { PCI_VDEVICE(INTEL, 0x292b), board_ahci }, /* ICH9M */
  378. { PCI_VDEVICE(INTEL, 0x292c), board_ahci }, /* ICH9M */
  379. { PCI_VDEVICE(INTEL, 0x292f), board_ahci }, /* ICH9M */
  380. { PCI_VDEVICE(INTEL, 0x294d), board_ahci }, /* ICH9 */
  381. { PCI_VDEVICE(INTEL, 0x294e), board_ahci }, /* ICH9M */
  382. { PCI_VDEVICE(INTEL, 0x502a), board_ahci }, /* Tolapai */
  383. { PCI_VDEVICE(INTEL, 0x502b), board_ahci }, /* Tolapai */
  384. /* JMicron 360/1/3/5/6, match class to avoid IDE function */
  385. { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  386. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci_ign_iferr },
  387. /* ATI */
  388. { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600 }, /* ATI SB600 */
  389. { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb600 }, /* ATI SB700/800 */
  390. { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb600 }, /* ATI SB700/800 */
  391. { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb600 }, /* ATI SB700/800 */
  392. { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb600 }, /* ATI SB700/800 */
  393. { PCI_VDEVICE(ATI, 0x4394), board_ahci_sb600 }, /* ATI SB700/800 */
  394. { PCI_VDEVICE(ATI, 0x4395), board_ahci_sb600 }, /* ATI SB700/800 */
  395. /* VIA */
  396. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  397. { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251 }, /* VIA VT8251 */
  398. /* NVIDIA */
  399. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
  400. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
  401. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
  402. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
  403. { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci }, /* MCP65 */
  404. { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci }, /* MCP65 */
  405. { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci }, /* MCP65 */
  406. { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci }, /* MCP65 */
  407. { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci }, /* MCP67 */
  408. { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci }, /* MCP67 */
  409. { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci }, /* MCP67 */
  410. { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci }, /* MCP67 */
  411. { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
  412. { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
  413. { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
  414. { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
  415. { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
  416. { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
  417. { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
  418. { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
  419. { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci }, /* MCP73 */
  420. { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci }, /* MCP73 */
  421. { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci }, /* MCP73 */
  422. { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci }, /* MCP73 */
  423. { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci }, /* MCP73 */
  424. { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci }, /* MCP73 */
  425. { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci }, /* MCP73 */
  426. { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci }, /* MCP73 */
  427. { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci }, /* MCP73 */
  428. { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci }, /* MCP73 */
  429. { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci }, /* MCP73 */
  430. { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci }, /* MCP73 */
  431. { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci }, /* MCP77 */
  432. { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci }, /* MCP77 */
  433. { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci }, /* MCP77 */
  434. { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci }, /* MCP77 */
  435. { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci }, /* MCP77 */
  436. { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci }, /* MCP77 */
  437. { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci }, /* MCP77 */
  438. { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci }, /* MCP77 */
  439. { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci }, /* MCP77 */
  440. { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci }, /* MCP77 */
  441. { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci }, /* MCP77 */
  442. { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci }, /* MCP77 */
  443. { PCI_VDEVICE(NVIDIA, 0x0ab8), board_ahci }, /* MCP79 */
  444. { PCI_VDEVICE(NVIDIA, 0x0ab9), board_ahci }, /* MCP79 */
  445. { PCI_VDEVICE(NVIDIA, 0x0aba), board_ahci }, /* MCP79 */
  446. { PCI_VDEVICE(NVIDIA, 0x0abb), board_ahci }, /* MCP79 */
  447. { PCI_VDEVICE(NVIDIA, 0x0abc), board_ahci }, /* MCP79 */
  448. { PCI_VDEVICE(NVIDIA, 0x0abd), board_ahci }, /* MCP79 */
  449. { PCI_VDEVICE(NVIDIA, 0x0abe), board_ahci }, /* MCP79 */
  450. { PCI_VDEVICE(NVIDIA, 0x0abf), board_ahci }, /* MCP79 */
  451. /* SiS */
  452. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  453. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  454. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  455. /* Marvell */
  456. { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv }, /* 6145 */
  457. /* Generic, PCI class code for AHCI */
  458. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  459. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
  460. { } /* terminate list */
  461. };
  462. static struct pci_driver ahci_pci_driver = {
  463. .name = DRV_NAME,
  464. .id_table = ahci_pci_tbl,
  465. .probe = ahci_init_one,
  466. .remove = ata_pci_remove_one,
  467. #ifdef CONFIG_PM
  468. .suspend = ahci_pci_device_suspend,
  469. .resume = ahci_pci_device_resume,
  470. #endif
  471. };
  472. static inline int ahci_nr_ports(u32 cap)
  473. {
  474. return (cap & 0x1f) + 1;
  475. }
  476. static inline void __iomem *__ahci_port_base(struct ata_host *host,
  477. unsigned int port_no)
  478. {
  479. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  480. return mmio + 0x100 + (port_no * 0x80);
  481. }
  482. static inline void __iomem *ahci_port_base(struct ata_port *ap)
  483. {
  484. return __ahci_port_base(ap->host, ap->port_no);
  485. }
  486. /**
  487. * ahci_save_initial_config - Save and fixup initial config values
  488. * @pdev: target PCI device
  489. * @hpriv: host private area to store config values
  490. *
  491. * Some registers containing configuration info might be setup by
  492. * BIOS and might be cleared on reset. This function saves the
  493. * initial values of those registers into @hpriv such that they
  494. * can be restored after controller reset.
  495. *
  496. * If inconsistent, config values are fixed up by this function.
  497. *
  498. * LOCKING:
  499. * None.
  500. */
  501. static void ahci_save_initial_config(struct pci_dev *pdev,
  502. struct ahci_host_priv *hpriv)
  503. {
  504. void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
  505. u32 cap, port_map;
  506. int i;
  507. /* Values prefixed with saved_ are written back to host after
  508. * reset. Values without are used for driver operation.
  509. */
  510. hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
  511. hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
  512. /* some chips have errata preventing 64bit use */
  513. if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
  514. dev_printk(KERN_INFO, &pdev->dev,
  515. "controller can't do 64bit DMA, forcing 32bit\n");
  516. cap &= ~HOST_CAP_64;
  517. }
  518. if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
  519. dev_printk(KERN_INFO, &pdev->dev,
  520. "controller can't do NCQ, turning off CAP_NCQ\n");
  521. cap &= ~HOST_CAP_NCQ;
  522. }
  523. if ((cap && HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
  524. dev_printk(KERN_INFO, &pdev->dev,
  525. "controller can't do PMP, turning off CAP_PMP\n");
  526. cap &= ~HOST_CAP_PMP;
  527. }
  528. /*
  529. * Temporary Marvell 6145 hack: PATA port presence
  530. * is asserted through the standard AHCI port
  531. * presence register, as bit 4 (counting from 0)
  532. */
  533. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  534. dev_printk(KERN_ERR, &pdev->dev,
  535. "MV_AHCI HACK: port_map %x -> %x\n",
  536. hpriv->port_map,
  537. hpriv->port_map & 0xf);
  538. port_map &= 0xf;
  539. }
  540. /* cross check port_map and cap.n_ports */
  541. if (port_map) {
  542. u32 tmp_port_map = port_map;
  543. int n_ports = ahci_nr_ports(cap);
  544. for (i = 0; i < AHCI_MAX_PORTS && n_ports; i++) {
  545. if (tmp_port_map & (1 << i)) {
  546. n_ports--;
  547. tmp_port_map &= ~(1 << i);
  548. }
  549. }
  550. /* If n_ports and port_map are inconsistent, whine and
  551. * clear port_map and let it be generated from n_ports.
  552. */
  553. if (n_ports || tmp_port_map) {
  554. dev_printk(KERN_WARNING, &pdev->dev,
  555. "nr_ports (%u) and implemented port map "
  556. "(0x%x) don't match, using nr_ports\n",
  557. ahci_nr_ports(cap), port_map);
  558. port_map = 0;
  559. }
  560. }
  561. /* fabricate port_map from cap.nr_ports */
  562. if (!port_map) {
  563. port_map = (1 << ahci_nr_ports(cap)) - 1;
  564. dev_printk(KERN_WARNING, &pdev->dev,
  565. "forcing PORTS_IMPL to 0x%x\n", port_map);
  566. /* write the fixed up value to the PI register */
  567. hpriv->saved_port_map = port_map;
  568. }
  569. /* record values to use during operation */
  570. hpriv->cap = cap;
  571. hpriv->port_map = port_map;
  572. }
  573. /**
  574. * ahci_restore_initial_config - Restore initial config
  575. * @host: target ATA host
  576. *
  577. * Restore initial config stored by ahci_save_initial_config().
  578. *
  579. * LOCKING:
  580. * None.
  581. */
  582. static void ahci_restore_initial_config(struct ata_host *host)
  583. {
  584. struct ahci_host_priv *hpriv = host->private_data;
  585. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  586. writel(hpriv->saved_cap, mmio + HOST_CAP);
  587. writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
  588. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  589. }
  590. static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
  591. {
  592. static const int offset[] = {
  593. [SCR_STATUS] = PORT_SCR_STAT,
  594. [SCR_CONTROL] = PORT_SCR_CTL,
  595. [SCR_ERROR] = PORT_SCR_ERR,
  596. [SCR_ACTIVE] = PORT_SCR_ACT,
  597. [SCR_NOTIFICATION] = PORT_SCR_NTF,
  598. };
  599. struct ahci_host_priv *hpriv = ap->host->private_data;
  600. if (sc_reg < ARRAY_SIZE(offset) &&
  601. (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
  602. return offset[sc_reg];
  603. return 0;
  604. }
  605. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  606. {
  607. void __iomem *port_mmio = ahci_port_base(ap);
  608. int offset = ahci_scr_offset(ap, sc_reg);
  609. if (offset) {
  610. *val = readl(port_mmio + offset);
  611. return 0;
  612. }
  613. return -EINVAL;
  614. }
  615. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  616. {
  617. void __iomem *port_mmio = ahci_port_base(ap);
  618. int offset = ahci_scr_offset(ap, sc_reg);
  619. if (offset) {
  620. writel(val, port_mmio + offset);
  621. return 0;
  622. }
  623. return -EINVAL;
  624. }
  625. static void ahci_start_engine(struct ata_port *ap)
  626. {
  627. void __iomem *port_mmio = ahci_port_base(ap);
  628. u32 tmp;
  629. /* start DMA */
  630. tmp = readl(port_mmio + PORT_CMD);
  631. tmp |= PORT_CMD_START;
  632. writel(tmp, port_mmio + PORT_CMD);
  633. readl(port_mmio + PORT_CMD); /* flush */
  634. }
  635. static int ahci_stop_engine(struct ata_port *ap)
  636. {
  637. void __iomem *port_mmio = ahci_port_base(ap);
  638. u32 tmp;
  639. tmp = readl(port_mmio + PORT_CMD);
  640. /* check if the HBA is idle */
  641. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  642. return 0;
  643. /* setting HBA to idle */
  644. tmp &= ~PORT_CMD_START;
  645. writel(tmp, port_mmio + PORT_CMD);
  646. /* wait for engine to stop. This could be as long as 500 msec */
  647. tmp = ata_wait_register(port_mmio + PORT_CMD,
  648. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  649. if (tmp & PORT_CMD_LIST_ON)
  650. return -EIO;
  651. return 0;
  652. }
  653. static void ahci_start_fis_rx(struct ata_port *ap)
  654. {
  655. void __iomem *port_mmio = ahci_port_base(ap);
  656. struct ahci_host_priv *hpriv = ap->host->private_data;
  657. struct ahci_port_priv *pp = ap->private_data;
  658. u32 tmp;
  659. /* set FIS registers */
  660. if (hpriv->cap & HOST_CAP_64)
  661. writel((pp->cmd_slot_dma >> 16) >> 16,
  662. port_mmio + PORT_LST_ADDR_HI);
  663. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  664. if (hpriv->cap & HOST_CAP_64)
  665. writel((pp->rx_fis_dma >> 16) >> 16,
  666. port_mmio + PORT_FIS_ADDR_HI);
  667. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  668. /* enable FIS reception */
  669. tmp = readl(port_mmio + PORT_CMD);
  670. tmp |= PORT_CMD_FIS_RX;
  671. writel(tmp, port_mmio + PORT_CMD);
  672. /* flush */
  673. readl(port_mmio + PORT_CMD);
  674. }
  675. static int ahci_stop_fis_rx(struct ata_port *ap)
  676. {
  677. void __iomem *port_mmio = ahci_port_base(ap);
  678. u32 tmp;
  679. /* disable FIS reception */
  680. tmp = readl(port_mmio + PORT_CMD);
  681. tmp &= ~PORT_CMD_FIS_RX;
  682. writel(tmp, port_mmio + PORT_CMD);
  683. /* wait for completion, spec says 500ms, give it 1000 */
  684. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  685. PORT_CMD_FIS_ON, 10, 1000);
  686. if (tmp & PORT_CMD_FIS_ON)
  687. return -EBUSY;
  688. return 0;
  689. }
  690. static void ahci_power_up(struct ata_port *ap)
  691. {
  692. struct ahci_host_priv *hpriv = ap->host->private_data;
  693. void __iomem *port_mmio = ahci_port_base(ap);
  694. u32 cmd;
  695. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  696. /* spin up device */
  697. if (hpriv->cap & HOST_CAP_SSS) {
  698. cmd |= PORT_CMD_SPIN_UP;
  699. writel(cmd, port_mmio + PORT_CMD);
  700. }
  701. /* wake up link */
  702. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  703. }
  704. #ifdef CONFIG_PM
  705. static void ahci_power_down(struct ata_port *ap)
  706. {
  707. struct ahci_host_priv *hpriv = ap->host->private_data;
  708. void __iomem *port_mmio = ahci_port_base(ap);
  709. u32 cmd, scontrol;
  710. if (!(hpriv->cap & HOST_CAP_SSS))
  711. return;
  712. /* put device into listen mode, first set PxSCTL.DET to 0 */
  713. scontrol = readl(port_mmio + PORT_SCR_CTL);
  714. scontrol &= ~0xf;
  715. writel(scontrol, port_mmio + PORT_SCR_CTL);
  716. /* then set PxCMD.SUD to 0 */
  717. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  718. cmd &= ~PORT_CMD_SPIN_UP;
  719. writel(cmd, port_mmio + PORT_CMD);
  720. }
  721. #endif
  722. static void ahci_start_port(struct ata_port *ap)
  723. {
  724. /* enable FIS reception */
  725. ahci_start_fis_rx(ap);
  726. /* enable DMA */
  727. ahci_start_engine(ap);
  728. }
  729. static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
  730. {
  731. int rc;
  732. /* disable DMA */
  733. rc = ahci_stop_engine(ap);
  734. if (rc) {
  735. *emsg = "failed to stop engine";
  736. return rc;
  737. }
  738. /* disable FIS reception */
  739. rc = ahci_stop_fis_rx(ap);
  740. if (rc) {
  741. *emsg = "failed stop FIS RX";
  742. return rc;
  743. }
  744. return 0;
  745. }
  746. static int ahci_reset_controller(struct ata_host *host)
  747. {
  748. struct pci_dev *pdev = to_pci_dev(host->dev);
  749. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  750. u32 tmp;
  751. /* we must be in AHCI mode, before using anything
  752. * AHCI-specific, such as HOST_RESET.
  753. */
  754. tmp = readl(mmio + HOST_CTL);
  755. if (!(tmp & HOST_AHCI_EN))
  756. writel(tmp | HOST_AHCI_EN, mmio + HOST_CTL);
  757. /* global controller reset */
  758. if ((tmp & HOST_RESET) == 0) {
  759. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  760. readl(mmio + HOST_CTL); /* flush */
  761. }
  762. /* reset must complete within 1 second, or
  763. * the hardware should be considered fried.
  764. */
  765. ssleep(1);
  766. tmp = readl(mmio + HOST_CTL);
  767. if (tmp & HOST_RESET) {
  768. dev_printk(KERN_ERR, host->dev,
  769. "controller reset failed (0x%x)\n", tmp);
  770. return -EIO;
  771. }
  772. /* turn on AHCI mode */
  773. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  774. (void) readl(mmio + HOST_CTL); /* flush */
  775. /* some registers might be cleared on reset. restore initial values */
  776. ahci_restore_initial_config(host);
  777. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  778. u16 tmp16;
  779. /* configure PCS */
  780. pci_read_config_word(pdev, 0x92, &tmp16);
  781. tmp16 |= 0xf;
  782. pci_write_config_word(pdev, 0x92, tmp16);
  783. }
  784. return 0;
  785. }
  786. static void ahci_port_init(struct pci_dev *pdev, struct ata_port *ap,
  787. int port_no, void __iomem *mmio,
  788. void __iomem *port_mmio)
  789. {
  790. const char *emsg = NULL;
  791. int rc;
  792. u32 tmp;
  793. /* make sure port is not active */
  794. rc = ahci_deinit_port(ap, &emsg);
  795. if (rc)
  796. dev_printk(KERN_WARNING, &pdev->dev,
  797. "%s (%d)\n", emsg, rc);
  798. /* clear SError */
  799. tmp = readl(port_mmio + PORT_SCR_ERR);
  800. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  801. writel(tmp, port_mmio + PORT_SCR_ERR);
  802. /* clear port IRQ */
  803. tmp = readl(port_mmio + PORT_IRQ_STAT);
  804. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  805. if (tmp)
  806. writel(tmp, port_mmio + PORT_IRQ_STAT);
  807. writel(1 << port_no, mmio + HOST_IRQ_STAT);
  808. }
  809. static void ahci_init_controller(struct ata_host *host)
  810. {
  811. struct ahci_host_priv *hpriv = host->private_data;
  812. struct pci_dev *pdev = to_pci_dev(host->dev);
  813. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  814. int i;
  815. void __iomem *port_mmio;
  816. u32 tmp;
  817. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  818. port_mmio = __ahci_port_base(host, 4);
  819. writel(0, port_mmio + PORT_IRQ_MASK);
  820. /* clear port IRQ */
  821. tmp = readl(port_mmio + PORT_IRQ_STAT);
  822. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  823. if (tmp)
  824. writel(tmp, port_mmio + PORT_IRQ_STAT);
  825. }
  826. for (i = 0; i < host->n_ports; i++) {
  827. struct ata_port *ap = host->ports[i];
  828. port_mmio = ahci_port_base(ap);
  829. if (ata_port_is_dummy(ap))
  830. continue;
  831. ahci_port_init(pdev, ap, i, mmio, port_mmio);
  832. }
  833. tmp = readl(mmio + HOST_CTL);
  834. VPRINTK("HOST_CTL 0x%x\n", tmp);
  835. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  836. tmp = readl(mmio + HOST_CTL);
  837. VPRINTK("HOST_CTL 0x%x\n", tmp);
  838. }
  839. static unsigned int ahci_dev_classify(struct ata_port *ap)
  840. {
  841. void __iomem *port_mmio = ahci_port_base(ap);
  842. struct ata_taskfile tf;
  843. u32 tmp;
  844. tmp = readl(port_mmio + PORT_SIG);
  845. tf.lbah = (tmp >> 24) & 0xff;
  846. tf.lbam = (tmp >> 16) & 0xff;
  847. tf.lbal = (tmp >> 8) & 0xff;
  848. tf.nsect = (tmp) & 0xff;
  849. return ata_dev_classify(&tf);
  850. }
  851. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  852. u32 opts)
  853. {
  854. dma_addr_t cmd_tbl_dma;
  855. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  856. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  857. pp->cmd_slot[tag].status = 0;
  858. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  859. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  860. }
  861. static int ahci_kick_engine(struct ata_port *ap, int force_restart)
  862. {
  863. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  864. struct ahci_host_priv *hpriv = ap->host->private_data;
  865. u32 tmp;
  866. int busy, rc;
  867. /* do we need to kick the port? */
  868. busy = ahci_check_status(ap) & (ATA_BUSY | ATA_DRQ);
  869. if (!busy && !force_restart)
  870. return 0;
  871. /* stop engine */
  872. rc = ahci_stop_engine(ap);
  873. if (rc)
  874. goto out_restart;
  875. /* need to do CLO? */
  876. if (!busy) {
  877. rc = 0;
  878. goto out_restart;
  879. }
  880. if (!(hpriv->cap & HOST_CAP_CLO)) {
  881. rc = -EOPNOTSUPP;
  882. goto out_restart;
  883. }
  884. /* perform CLO */
  885. tmp = readl(port_mmio + PORT_CMD);
  886. tmp |= PORT_CMD_CLO;
  887. writel(tmp, port_mmio + PORT_CMD);
  888. rc = 0;
  889. tmp = ata_wait_register(port_mmio + PORT_CMD,
  890. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  891. if (tmp & PORT_CMD_CLO)
  892. rc = -EIO;
  893. /* restart engine */
  894. out_restart:
  895. ahci_start_engine(ap);
  896. return rc;
  897. }
  898. static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
  899. struct ata_taskfile *tf, int is_cmd, u16 flags,
  900. unsigned long timeout_msec)
  901. {
  902. const u32 cmd_fis_len = 5; /* five dwords */
  903. struct ahci_port_priv *pp = ap->private_data;
  904. void __iomem *port_mmio = ahci_port_base(ap);
  905. u8 *fis = pp->cmd_tbl;
  906. u32 tmp;
  907. /* prep the command */
  908. ata_tf_to_fis(tf, pmp, is_cmd, fis);
  909. ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
  910. /* issue & wait */
  911. writel(1, port_mmio + PORT_CMD_ISSUE);
  912. if (timeout_msec) {
  913. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1,
  914. 1, timeout_msec);
  915. if (tmp & 0x1) {
  916. ahci_kick_engine(ap, 1);
  917. return -EBUSY;
  918. }
  919. } else
  920. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  921. return 0;
  922. }
  923. static int ahci_do_softreset(struct ata_link *link, unsigned int *class,
  924. int pmp, unsigned long deadline)
  925. {
  926. struct ata_port *ap = link->ap;
  927. const char *reason = NULL;
  928. unsigned long now, msecs;
  929. struct ata_taskfile tf;
  930. int rc;
  931. DPRINTK("ENTER\n");
  932. if (ata_link_offline(link)) {
  933. DPRINTK("PHY reports no device\n");
  934. *class = ATA_DEV_NONE;
  935. return 0;
  936. }
  937. /* prepare for SRST (AHCI-1.1 10.4.1) */
  938. rc = ahci_kick_engine(ap, 1);
  939. if (rc)
  940. ata_link_printk(link, KERN_WARNING,
  941. "failed to reset engine (errno=%d)", rc);
  942. ata_tf_init(link->device, &tf);
  943. /* issue the first D2H Register FIS */
  944. msecs = 0;
  945. now = jiffies;
  946. if (time_after(now, deadline))
  947. msecs = jiffies_to_msecs(deadline - now);
  948. tf.ctl |= ATA_SRST;
  949. if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
  950. AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
  951. rc = -EIO;
  952. reason = "1st FIS failed";
  953. goto fail;
  954. }
  955. /* spec says at least 5us, but be generous and sleep for 1ms */
  956. msleep(1);
  957. /* issue the second D2H Register FIS */
  958. tf.ctl &= ~ATA_SRST;
  959. ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
  960. /* spec mandates ">= 2ms" before checking status.
  961. * We wait 150ms, because that was the magic delay used for
  962. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  963. * between when the ATA command register is written, and then
  964. * status is checked. Because waiting for "a while" before
  965. * checking status is fine, post SRST, we perform this magic
  966. * delay here as well.
  967. */
  968. msleep(150);
  969. rc = ata_wait_ready(ap, deadline);
  970. /* link occupied, -ENODEV too is an error */
  971. if (rc) {
  972. reason = "device not ready";
  973. goto fail;
  974. }
  975. *class = ahci_dev_classify(ap);
  976. DPRINTK("EXIT, class=%u\n", *class);
  977. return 0;
  978. fail:
  979. ata_link_printk(link, KERN_ERR, "softreset failed (%s)\n", reason);
  980. return rc;
  981. }
  982. static int ahci_softreset(struct ata_link *link, unsigned int *class,
  983. unsigned long deadline)
  984. {
  985. int pmp = 0;
  986. if (link->ap->flags & ATA_FLAG_PMP)
  987. pmp = SATA_PMP_CTRL_PORT;
  988. return ahci_do_softreset(link, class, pmp, deadline);
  989. }
  990. static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  991. unsigned long deadline)
  992. {
  993. struct ata_port *ap = link->ap;
  994. struct ahci_port_priv *pp = ap->private_data;
  995. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  996. struct ata_taskfile tf;
  997. int rc;
  998. DPRINTK("ENTER\n");
  999. ahci_stop_engine(ap);
  1000. /* clear D2H reception area to properly wait for D2H FIS */
  1001. ata_tf_init(link->device, &tf);
  1002. tf.command = 0x80;
  1003. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  1004. rc = sata_std_hardreset(link, class, deadline);
  1005. ahci_start_engine(ap);
  1006. if (rc == 0 && ata_link_online(link))
  1007. *class = ahci_dev_classify(ap);
  1008. if (rc != -EAGAIN && *class == ATA_DEV_UNKNOWN)
  1009. *class = ATA_DEV_NONE;
  1010. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  1011. return rc;
  1012. }
  1013. static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
  1014. unsigned long deadline)
  1015. {
  1016. struct ata_port *ap = link->ap;
  1017. u32 serror;
  1018. int rc;
  1019. DPRINTK("ENTER\n");
  1020. ahci_stop_engine(ap);
  1021. rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
  1022. deadline);
  1023. /* vt8251 needs SError cleared for the port to operate */
  1024. ahci_scr_read(ap, SCR_ERROR, &serror);
  1025. ahci_scr_write(ap, SCR_ERROR, serror);
  1026. ahci_start_engine(ap);
  1027. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  1028. /* vt8251 doesn't clear BSY on signature FIS reception,
  1029. * request follow-up softreset.
  1030. */
  1031. return rc ?: -EAGAIN;
  1032. }
  1033. static void ahci_postreset(struct ata_link *link, unsigned int *class)
  1034. {
  1035. struct ata_port *ap = link->ap;
  1036. void __iomem *port_mmio = ahci_port_base(ap);
  1037. u32 new_tmp, tmp;
  1038. ata_std_postreset(link, class);
  1039. /* Make sure port's ATAPI bit is set appropriately */
  1040. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  1041. if (*class == ATA_DEV_ATAPI)
  1042. new_tmp |= PORT_CMD_ATAPI;
  1043. else
  1044. new_tmp &= ~PORT_CMD_ATAPI;
  1045. if (new_tmp != tmp) {
  1046. writel(new_tmp, port_mmio + PORT_CMD);
  1047. readl(port_mmio + PORT_CMD); /* flush */
  1048. }
  1049. }
  1050. static int ahci_pmp_softreset(struct ata_link *link, unsigned int *class,
  1051. unsigned long deadline)
  1052. {
  1053. return ahci_do_softreset(link, class, link->pmp, deadline);
  1054. }
  1055. static u8 ahci_check_status(struct ata_port *ap)
  1056. {
  1057. void __iomem *mmio = ap->ioaddr.cmd_addr;
  1058. return readl(mmio + PORT_TFDATA) & 0xFF;
  1059. }
  1060. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  1061. {
  1062. struct ahci_port_priv *pp = ap->private_data;
  1063. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1064. ata_tf_from_fis(d2h_fis, tf);
  1065. }
  1066. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  1067. {
  1068. struct scatterlist *sg;
  1069. struct ahci_sg *ahci_sg;
  1070. unsigned int n_sg = 0;
  1071. VPRINTK("ENTER\n");
  1072. /*
  1073. * Next, the S/G list.
  1074. */
  1075. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  1076. ata_for_each_sg(sg, qc) {
  1077. dma_addr_t addr = sg_dma_address(sg);
  1078. u32 sg_len = sg_dma_len(sg);
  1079. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1080. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1081. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  1082. ahci_sg++;
  1083. n_sg++;
  1084. }
  1085. return n_sg;
  1086. }
  1087. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  1088. {
  1089. struct ata_port *ap = qc->ap;
  1090. struct ahci_port_priv *pp = ap->private_data;
  1091. int is_atapi = is_atapi_taskfile(&qc->tf);
  1092. void *cmd_tbl;
  1093. u32 opts;
  1094. const u32 cmd_fis_len = 5; /* five dwords */
  1095. unsigned int n_elem;
  1096. /*
  1097. * Fill in command table information. First, the header,
  1098. * a SATA Register - Host to Device command FIS.
  1099. */
  1100. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  1101. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
  1102. if (is_atapi) {
  1103. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  1104. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  1105. }
  1106. n_elem = 0;
  1107. if (qc->flags & ATA_QCFLAG_DMAMAP)
  1108. n_elem = ahci_fill_sg(qc, cmd_tbl);
  1109. /*
  1110. * Fill in command slot information.
  1111. */
  1112. opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
  1113. if (qc->tf.flags & ATA_TFLAG_WRITE)
  1114. opts |= AHCI_CMD_WRITE;
  1115. if (is_atapi)
  1116. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  1117. ahci_fill_cmd_slot(pp, qc->tag, opts);
  1118. }
  1119. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  1120. {
  1121. struct ahci_host_priv *hpriv = ap->host->private_data;
  1122. struct ahci_port_priv *pp = ap->private_data;
  1123. struct ata_eh_info *host_ehi = &ap->link.eh_info;
  1124. struct ata_link *link = NULL;
  1125. struct ata_queued_cmd *active_qc;
  1126. struct ata_eh_info *active_ehi;
  1127. u32 serror;
  1128. /* determine active link */
  1129. ata_port_for_each_link(link, ap)
  1130. if (ata_link_active(link))
  1131. break;
  1132. if (!link)
  1133. link = &ap->link;
  1134. active_qc = ata_qc_from_tag(ap, link->active_tag);
  1135. active_ehi = &link->eh_info;
  1136. /* record irq stat */
  1137. ata_ehi_clear_desc(host_ehi);
  1138. ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
  1139. /* AHCI needs SError cleared; otherwise, it might lock up */
  1140. ahci_scr_read(ap, SCR_ERROR, &serror);
  1141. ahci_scr_write(ap, SCR_ERROR, serror);
  1142. host_ehi->serror |= serror;
  1143. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  1144. if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
  1145. irq_stat &= ~PORT_IRQ_IF_ERR;
  1146. if (irq_stat & PORT_IRQ_TF_ERR) {
  1147. /* If qc is active, charge it; otherwise, the active
  1148. * link. There's no active qc on NCQ errors. It will
  1149. * be determined by EH by reading log page 10h.
  1150. */
  1151. if (active_qc)
  1152. active_qc->err_mask |= AC_ERR_DEV;
  1153. else
  1154. active_ehi->err_mask |= AC_ERR_DEV;
  1155. if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
  1156. host_ehi->serror &= ~SERR_INTERNAL;
  1157. }
  1158. if (irq_stat & PORT_IRQ_UNK_FIS) {
  1159. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  1160. active_ehi->err_mask |= AC_ERR_HSM;
  1161. active_ehi->action |= ATA_EH_SOFTRESET;
  1162. ata_ehi_push_desc(active_ehi,
  1163. "unknown FIS %08x %08x %08x %08x" ,
  1164. unk[0], unk[1], unk[2], unk[3]);
  1165. }
  1166. if (ap->nr_pmp_links && (irq_stat & PORT_IRQ_BAD_PMP)) {
  1167. active_ehi->err_mask |= AC_ERR_HSM;
  1168. active_ehi->action |= ATA_EH_SOFTRESET;
  1169. ata_ehi_push_desc(active_ehi, "incorrect PMP");
  1170. }
  1171. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  1172. host_ehi->err_mask |= AC_ERR_HOST_BUS;
  1173. host_ehi->action |= ATA_EH_SOFTRESET;
  1174. ata_ehi_push_desc(host_ehi, "host bus error");
  1175. }
  1176. if (irq_stat & PORT_IRQ_IF_ERR) {
  1177. host_ehi->err_mask |= AC_ERR_ATA_BUS;
  1178. host_ehi->action |= ATA_EH_SOFTRESET;
  1179. ata_ehi_push_desc(host_ehi, "interface fatal error");
  1180. }
  1181. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  1182. ata_ehi_hotplugged(host_ehi);
  1183. ata_ehi_push_desc(host_ehi, "%s",
  1184. irq_stat & PORT_IRQ_CONNECT ?
  1185. "connection status changed" : "PHY RDY changed");
  1186. }
  1187. /* okay, let's hand over to EH */
  1188. if (irq_stat & PORT_IRQ_FREEZE)
  1189. ata_port_freeze(ap);
  1190. else
  1191. ata_port_abort(ap);
  1192. }
  1193. static void ahci_port_intr(struct ata_port *ap)
  1194. {
  1195. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  1196. struct ata_eh_info *ehi = &ap->link.eh_info;
  1197. struct ahci_port_priv *pp = ap->private_data;
  1198. u32 status, qc_active;
  1199. int rc, known_irq = 0;
  1200. status = readl(port_mmio + PORT_IRQ_STAT);
  1201. writel(status, port_mmio + PORT_IRQ_STAT);
  1202. if (unlikely(status & PORT_IRQ_ERROR)) {
  1203. ahci_error_intr(ap, status);
  1204. return;
  1205. }
  1206. if (status & PORT_IRQ_SDB_FIS) {
  1207. /* If the 'N' bit in word 0 of the FIS is set, we just
  1208. * received asynchronous notification. Tell libata
  1209. * about it. Note that as the SDB FIS itself is
  1210. * accessible, SNotification can be emulated by the
  1211. * driver but don't bother for the time being.
  1212. */
  1213. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1214. u32 f0 = le32_to_cpu(f[0]);
  1215. if (f0 & (1 << 15))
  1216. sata_async_notification(ap);
  1217. }
  1218. /* pp->active_link is valid iff any command is in flight */
  1219. if (ap->qc_active && pp->active_link->sactive)
  1220. qc_active = readl(port_mmio + PORT_SCR_ACT);
  1221. else
  1222. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  1223. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  1224. if (rc > 0)
  1225. return;
  1226. if (rc < 0) {
  1227. ehi->err_mask |= AC_ERR_HSM;
  1228. ehi->action |= ATA_EH_SOFTRESET;
  1229. ata_port_freeze(ap);
  1230. return;
  1231. }
  1232. /* hmmm... a spurious interupt */
  1233. /* if !NCQ, ignore. No modern ATA device has broken HSM
  1234. * implementation for non-NCQ commands.
  1235. */
  1236. if (!ap->link.sactive)
  1237. return;
  1238. if (status & PORT_IRQ_D2H_REG_FIS) {
  1239. if (!pp->ncq_saw_d2h)
  1240. ata_port_printk(ap, KERN_INFO,
  1241. "D2H reg with I during NCQ, "
  1242. "this message won't be printed again\n");
  1243. pp->ncq_saw_d2h = 1;
  1244. known_irq = 1;
  1245. }
  1246. if (status & PORT_IRQ_DMAS_FIS) {
  1247. if (!pp->ncq_saw_dmas)
  1248. ata_port_printk(ap, KERN_INFO,
  1249. "DMAS FIS during NCQ, "
  1250. "this message won't be printed again\n");
  1251. pp->ncq_saw_dmas = 1;
  1252. known_irq = 1;
  1253. }
  1254. if (status & PORT_IRQ_SDB_FIS) {
  1255. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1256. if (le32_to_cpu(f[1])) {
  1257. /* SDB FIS containing spurious completions
  1258. * might be dangerous, whine and fail commands
  1259. * with HSM violation. EH will turn off NCQ
  1260. * after several such failures.
  1261. */
  1262. ata_ehi_push_desc(ehi,
  1263. "spurious completions during NCQ "
  1264. "issue=0x%x SAct=0x%x FIS=%08x:%08x",
  1265. readl(port_mmio + PORT_CMD_ISSUE),
  1266. readl(port_mmio + PORT_SCR_ACT),
  1267. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1268. ehi->err_mask |= AC_ERR_HSM;
  1269. ehi->action |= ATA_EH_SOFTRESET;
  1270. ata_port_freeze(ap);
  1271. } else {
  1272. if (!pp->ncq_saw_sdb)
  1273. ata_port_printk(ap, KERN_INFO,
  1274. "spurious SDB FIS %08x:%08x during NCQ, "
  1275. "this message won't be printed again\n",
  1276. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1277. pp->ncq_saw_sdb = 1;
  1278. }
  1279. known_irq = 1;
  1280. }
  1281. if (!known_irq)
  1282. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  1283. "(irq_stat 0x%x active_tag 0x%x sactive 0x%x)\n",
  1284. status, ap->link.active_tag, ap->link.sactive);
  1285. }
  1286. static void ahci_irq_clear(struct ata_port *ap)
  1287. {
  1288. /* TODO */
  1289. }
  1290. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  1291. {
  1292. struct ata_host *host = dev_instance;
  1293. struct ahci_host_priv *hpriv;
  1294. unsigned int i, handled = 0;
  1295. void __iomem *mmio;
  1296. u32 irq_stat, irq_ack = 0;
  1297. VPRINTK("ENTER\n");
  1298. hpriv = host->private_data;
  1299. mmio = host->iomap[AHCI_PCI_BAR];
  1300. /* sigh. 0xffffffff is a valid return from h/w */
  1301. irq_stat = readl(mmio + HOST_IRQ_STAT);
  1302. irq_stat &= hpriv->port_map;
  1303. if (!irq_stat)
  1304. return IRQ_NONE;
  1305. spin_lock(&host->lock);
  1306. for (i = 0; i < host->n_ports; i++) {
  1307. struct ata_port *ap;
  1308. if (!(irq_stat & (1 << i)))
  1309. continue;
  1310. ap = host->ports[i];
  1311. if (ap) {
  1312. ahci_port_intr(ap);
  1313. VPRINTK("port %u\n", i);
  1314. } else {
  1315. VPRINTK("port %u (no irq)\n", i);
  1316. if (ata_ratelimit())
  1317. dev_printk(KERN_WARNING, host->dev,
  1318. "interrupt on disabled port %u\n", i);
  1319. }
  1320. irq_ack |= (1 << i);
  1321. }
  1322. if (irq_ack) {
  1323. writel(irq_ack, mmio + HOST_IRQ_STAT);
  1324. handled = 1;
  1325. }
  1326. spin_unlock(&host->lock);
  1327. VPRINTK("EXIT\n");
  1328. return IRQ_RETVAL(handled);
  1329. }
  1330. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1331. {
  1332. struct ata_port *ap = qc->ap;
  1333. void __iomem *port_mmio = ahci_port_base(ap);
  1334. struct ahci_port_priv *pp = ap->private_data;
  1335. /* Keep track of the currently active link. It will be used
  1336. * in completion path to determine whether NCQ phase is in
  1337. * progress.
  1338. */
  1339. pp->active_link = qc->dev->link;
  1340. if (qc->tf.protocol == ATA_PROT_NCQ)
  1341. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  1342. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  1343. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1344. return 0;
  1345. }
  1346. static void ahci_freeze(struct ata_port *ap)
  1347. {
  1348. void __iomem *port_mmio = ahci_port_base(ap);
  1349. /* turn IRQ off */
  1350. writel(0, port_mmio + PORT_IRQ_MASK);
  1351. }
  1352. static void ahci_thaw(struct ata_port *ap)
  1353. {
  1354. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1355. void __iomem *port_mmio = ahci_port_base(ap);
  1356. u32 tmp;
  1357. struct ahci_port_priv *pp = ap->private_data;
  1358. /* clear IRQ */
  1359. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1360. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1361. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1362. /* turn IRQ back on, ignore BAD_PMP if PMP isn't attached */
  1363. tmp = pp->intr_mask;
  1364. if (!ap->nr_pmp_links)
  1365. tmp &= ~PORT_IRQ_BAD_PMP;
  1366. writel(tmp, port_mmio + PORT_IRQ_MASK);
  1367. }
  1368. static void ahci_error_handler(struct ata_port *ap)
  1369. {
  1370. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1371. /* restart engine */
  1372. ahci_stop_engine(ap);
  1373. ahci_start_engine(ap);
  1374. }
  1375. /* perform recovery */
  1376. sata_pmp_do_eh(ap, ata_std_prereset, ahci_softreset,
  1377. ahci_hardreset, ahci_postreset,
  1378. sata_pmp_std_prereset, ahci_pmp_softreset,
  1379. sata_pmp_std_hardreset, sata_pmp_std_postreset);
  1380. }
  1381. static void ahci_vt8251_error_handler(struct ata_port *ap)
  1382. {
  1383. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1384. /* restart engine */
  1385. ahci_stop_engine(ap);
  1386. ahci_start_engine(ap);
  1387. }
  1388. /* perform recovery */
  1389. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_vt8251_hardreset,
  1390. ahci_postreset);
  1391. }
  1392. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1393. {
  1394. struct ata_port *ap = qc->ap;
  1395. /* make DMA engine forget about the failed command */
  1396. if (qc->flags & ATA_QCFLAG_FAILED)
  1397. ahci_kick_engine(ap, 1);
  1398. }
  1399. static void ahci_pmp_attach(struct ata_port *ap)
  1400. {
  1401. void __iomem *port_mmio = ahci_port_base(ap);
  1402. u32 cmd;
  1403. cmd = readl(port_mmio + PORT_CMD);
  1404. cmd |= PORT_CMD_PMP;
  1405. writel(cmd, port_mmio + PORT_CMD);
  1406. }
  1407. static void ahci_pmp_detach(struct ata_port *ap)
  1408. {
  1409. void __iomem *port_mmio = ahci_port_base(ap);
  1410. u32 cmd;
  1411. cmd = readl(port_mmio + PORT_CMD);
  1412. cmd &= ~PORT_CMD_PMP;
  1413. writel(cmd, port_mmio + PORT_CMD);
  1414. }
  1415. static int ahci_pmp_read(struct ata_device *dev, int pmp, int reg, u32 *r_val)
  1416. {
  1417. struct ata_port *ap = dev->link->ap;
  1418. struct ata_taskfile tf;
  1419. int rc;
  1420. ahci_kick_engine(ap, 0);
  1421. sata_pmp_read_init_tf(&tf, dev, pmp, reg);
  1422. rc = ahci_exec_polled_cmd(ap, SATA_PMP_CTRL_PORT, &tf, 1, 0,
  1423. SATA_PMP_SCR_TIMEOUT);
  1424. if (rc == 0) {
  1425. ahci_tf_read(ap, &tf);
  1426. *r_val = sata_pmp_read_val(&tf);
  1427. }
  1428. return rc;
  1429. }
  1430. static int ahci_pmp_write(struct ata_device *dev, int pmp, int reg, u32 val)
  1431. {
  1432. struct ata_port *ap = dev->link->ap;
  1433. struct ata_taskfile tf;
  1434. ahci_kick_engine(ap, 0);
  1435. sata_pmp_write_init_tf(&tf, dev, pmp, reg, val);
  1436. return ahci_exec_polled_cmd(ap, SATA_PMP_CTRL_PORT, &tf, 1, 0,
  1437. SATA_PMP_SCR_TIMEOUT);
  1438. }
  1439. static int ahci_port_resume(struct ata_port *ap)
  1440. {
  1441. ahci_power_up(ap);
  1442. ahci_start_port(ap);
  1443. if (ap->nr_pmp_links)
  1444. ahci_pmp_attach(ap);
  1445. else
  1446. ahci_pmp_detach(ap);
  1447. return 0;
  1448. }
  1449. #ifdef CONFIG_PM
  1450. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1451. {
  1452. const char *emsg = NULL;
  1453. int rc;
  1454. rc = ahci_deinit_port(ap, &emsg);
  1455. if (rc == 0)
  1456. ahci_power_down(ap);
  1457. else {
  1458. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  1459. ahci_start_port(ap);
  1460. }
  1461. return rc;
  1462. }
  1463. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1464. {
  1465. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1466. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1467. u32 ctl;
  1468. if (mesg.event == PM_EVENT_SUSPEND) {
  1469. /* AHCI spec rev1.1 section 8.3.3:
  1470. * Software must disable interrupts prior to requesting a
  1471. * transition of the HBA to D3 state.
  1472. */
  1473. ctl = readl(mmio + HOST_CTL);
  1474. ctl &= ~HOST_IRQ_EN;
  1475. writel(ctl, mmio + HOST_CTL);
  1476. readl(mmio + HOST_CTL); /* flush */
  1477. }
  1478. return ata_pci_device_suspend(pdev, mesg);
  1479. }
  1480. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1481. {
  1482. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1483. int rc;
  1484. rc = ata_pci_device_do_resume(pdev);
  1485. if (rc)
  1486. return rc;
  1487. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1488. rc = ahci_reset_controller(host);
  1489. if (rc)
  1490. return rc;
  1491. ahci_init_controller(host);
  1492. }
  1493. ata_host_resume(host);
  1494. return 0;
  1495. }
  1496. #endif
  1497. static int ahci_port_start(struct ata_port *ap)
  1498. {
  1499. struct device *dev = ap->host->dev;
  1500. struct ahci_port_priv *pp;
  1501. void *mem;
  1502. dma_addr_t mem_dma;
  1503. int rc;
  1504. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1505. if (!pp)
  1506. return -ENOMEM;
  1507. rc = ata_pad_alloc(ap, dev);
  1508. if (rc)
  1509. return rc;
  1510. mem = dmam_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma,
  1511. GFP_KERNEL);
  1512. if (!mem)
  1513. return -ENOMEM;
  1514. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1515. /*
  1516. * First item in chunk of DMA memory: 32-slot command table,
  1517. * 32 bytes each in size
  1518. */
  1519. pp->cmd_slot = mem;
  1520. pp->cmd_slot_dma = mem_dma;
  1521. mem += AHCI_CMD_SLOT_SZ;
  1522. mem_dma += AHCI_CMD_SLOT_SZ;
  1523. /*
  1524. * Second item: Received-FIS area
  1525. */
  1526. pp->rx_fis = mem;
  1527. pp->rx_fis_dma = mem_dma;
  1528. mem += AHCI_RX_FIS_SZ;
  1529. mem_dma += AHCI_RX_FIS_SZ;
  1530. /*
  1531. * Third item: data area for storing a single command
  1532. * and its scatter-gather table
  1533. */
  1534. pp->cmd_tbl = mem;
  1535. pp->cmd_tbl_dma = mem_dma;
  1536. /*
  1537. * Save off initial list of interrupts to be enabled.
  1538. * This could be changed later
  1539. */
  1540. pp->intr_mask = DEF_PORT_IRQ;
  1541. ap->private_data = pp;
  1542. /* engage engines, captain */
  1543. return ahci_port_resume(ap);
  1544. }
  1545. static void ahci_port_stop(struct ata_port *ap)
  1546. {
  1547. const char *emsg = NULL;
  1548. int rc;
  1549. /* de-initialize port */
  1550. rc = ahci_deinit_port(ap, &emsg);
  1551. if (rc)
  1552. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1553. }
  1554. static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
  1555. {
  1556. int rc;
  1557. if (using_dac &&
  1558. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1559. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1560. if (rc) {
  1561. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1562. if (rc) {
  1563. dev_printk(KERN_ERR, &pdev->dev,
  1564. "64-bit DMA enable failed\n");
  1565. return rc;
  1566. }
  1567. }
  1568. } else {
  1569. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1570. if (rc) {
  1571. dev_printk(KERN_ERR, &pdev->dev,
  1572. "32-bit DMA enable failed\n");
  1573. return rc;
  1574. }
  1575. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1576. if (rc) {
  1577. dev_printk(KERN_ERR, &pdev->dev,
  1578. "32-bit consistent DMA enable failed\n");
  1579. return rc;
  1580. }
  1581. }
  1582. return 0;
  1583. }
  1584. static void ahci_print_info(struct ata_host *host)
  1585. {
  1586. struct ahci_host_priv *hpriv = host->private_data;
  1587. struct pci_dev *pdev = to_pci_dev(host->dev);
  1588. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1589. u32 vers, cap, impl, speed;
  1590. const char *speed_s;
  1591. u16 cc;
  1592. const char *scc_s;
  1593. vers = readl(mmio + HOST_VERSION);
  1594. cap = hpriv->cap;
  1595. impl = hpriv->port_map;
  1596. speed = (cap >> 20) & 0xf;
  1597. if (speed == 1)
  1598. speed_s = "1.5";
  1599. else if (speed == 2)
  1600. speed_s = "3";
  1601. else
  1602. speed_s = "?";
  1603. pci_read_config_word(pdev, 0x0a, &cc);
  1604. if (cc == PCI_CLASS_STORAGE_IDE)
  1605. scc_s = "IDE";
  1606. else if (cc == PCI_CLASS_STORAGE_SATA)
  1607. scc_s = "SATA";
  1608. else if (cc == PCI_CLASS_STORAGE_RAID)
  1609. scc_s = "RAID";
  1610. else
  1611. scc_s = "unknown";
  1612. dev_printk(KERN_INFO, &pdev->dev,
  1613. "AHCI %02x%02x.%02x%02x "
  1614. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1615. ,
  1616. (vers >> 24) & 0xff,
  1617. (vers >> 16) & 0xff,
  1618. (vers >> 8) & 0xff,
  1619. vers & 0xff,
  1620. ((cap >> 8) & 0x1f) + 1,
  1621. (cap & 0x1f) + 1,
  1622. speed_s,
  1623. impl,
  1624. scc_s);
  1625. dev_printk(KERN_INFO, &pdev->dev,
  1626. "flags: "
  1627. "%s%s%s%s%s%s%s"
  1628. "%s%s%s%s%s%s%s\n"
  1629. ,
  1630. cap & (1 << 31) ? "64bit " : "",
  1631. cap & (1 << 30) ? "ncq " : "",
  1632. cap & (1 << 29) ? "sntf " : "",
  1633. cap & (1 << 28) ? "ilck " : "",
  1634. cap & (1 << 27) ? "stag " : "",
  1635. cap & (1 << 26) ? "pm " : "",
  1636. cap & (1 << 25) ? "led " : "",
  1637. cap & (1 << 24) ? "clo " : "",
  1638. cap & (1 << 19) ? "nz " : "",
  1639. cap & (1 << 18) ? "only " : "",
  1640. cap & (1 << 17) ? "pmp " : "",
  1641. cap & (1 << 15) ? "pio " : "",
  1642. cap & (1 << 14) ? "slum " : "",
  1643. cap & (1 << 13) ? "part " : ""
  1644. );
  1645. }
  1646. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1647. {
  1648. static int printed_version;
  1649. struct ata_port_info pi = ahci_port_info[ent->driver_data];
  1650. const struct ata_port_info *ppi[] = { &pi, NULL };
  1651. struct device *dev = &pdev->dev;
  1652. struct ahci_host_priv *hpriv;
  1653. struct ata_host *host;
  1654. int i, rc;
  1655. VPRINTK("ENTER\n");
  1656. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1657. if (!printed_version++)
  1658. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1659. /* acquire resources */
  1660. rc = pcim_enable_device(pdev);
  1661. if (rc)
  1662. return rc;
  1663. rc = pcim_iomap_regions(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
  1664. if (rc == -EBUSY)
  1665. pcim_pin_device(pdev);
  1666. if (rc)
  1667. return rc;
  1668. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  1669. if (!hpriv)
  1670. return -ENOMEM;
  1671. hpriv->flags |= (unsigned long)pi.private_data;
  1672. if ((hpriv->flags & AHCI_HFLAG_NO_MSI) || pci_enable_msi(pdev))
  1673. pci_intx(pdev, 1);
  1674. /* save initial config */
  1675. ahci_save_initial_config(pdev, hpriv);
  1676. /* prepare host */
  1677. if (hpriv->cap & HOST_CAP_NCQ)
  1678. pi.flags |= ATA_FLAG_NCQ;
  1679. if (hpriv->cap & HOST_CAP_PMP)
  1680. pi.flags |= ATA_FLAG_PMP;
  1681. host = ata_host_alloc_pinfo(&pdev->dev, ppi, fls(hpriv->port_map));
  1682. if (!host)
  1683. return -ENOMEM;
  1684. host->iomap = pcim_iomap_table(pdev);
  1685. host->private_data = hpriv;
  1686. for (i = 0; i < host->n_ports; i++) {
  1687. struct ata_port *ap = host->ports[i];
  1688. void __iomem *port_mmio = ahci_port_base(ap);
  1689. ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
  1690. ata_port_pbar_desc(ap, AHCI_PCI_BAR,
  1691. 0x100 + ap->port_no * 0x80, "port");
  1692. /* standard SATA port setup */
  1693. if (hpriv->port_map & (1 << i))
  1694. ap->ioaddr.cmd_addr = port_mmio;
  1695. /* disabled/not-implemented port */
  1696. else
  1697. ap->ops = &ata_dummy_port_ops;
  1698. }
  1699. /* initialize adapter */
  1700. rc = ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
  1701. if (rc)
  1702. return rc;
  1703. rc = ahci_reset_controller(host);
  1704. if (rc)
  1705. return rc;
  1706. ahci_init_controller(host);
  1707. ahci_print_info(host);
  1708. pci_set_master(pdev);
  1709. return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
  1710. &ahci_sht);
  1711. }
  1712. static int __init ahci_init(void)
  1713. {
  1714. return pci_register_driver(&ahci_pci_driver);
  1715. }
  1716. static void __exit ahci_exit(void)
  1717. {
  1718. pci_unregister_driver(&ahci_pci_driver);
  1719. }
  1720. MODULE_AUTHOR("Jeff Garzik");
  1721. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1722. MODULE_LICENSE("GPL");
  1723. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1724. MODULE_VERSION(DRV_VERSION);
  1725. module_init(ahci_init);
  1726. module_exit(ahci_exit);