ahci.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  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 <linux/dmi.h>
  44. #include <scsi/scsi_host.h>
  45. #include <scsi/scsi_cmnd.h>
  46. #include <linux/libata.h>
  47. #define DRV_NAME "ahci"
  48. #define DRV_VERSION "3.0"
  49. static int ahci_skip_host_reset;
  50. module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
  51. MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
  52. static int ahci_enable_alpm(struct ata_port *ap,
  53. enum link_pm policy);
  54. static void ahci_disable_alpm(struct ata_port *ap);
  55. enum {
  56. AHCI_PCI_BAR = 5,
  57. AHCI_MAX_PORTS = 32,
  58. AHCI_MAX_SG = 168, /* hardware max is 64K */
  59. AHCI_DMA_BOUNDARY = 0xffffffff,
  60. AHCI_MAX_CMDS = 32,
  61. AHCI_CMD_SZ = 32,
  62. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  63. AHCI_RX_FIS_SZ = 256,
  64. AHCI_CMD_TBL_CDB = 0x40,
  65. AHCI_CMD_TBL_HDR_SZ = 0x80,
  66. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  67. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  68. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  69. AHCI_RX_FIS_SZ,
  70. AHCI_IRQ_ON_SG = (1 << 31),
  71. AHCI_CMD_ATAPI = (1 << 5),
  72. AHCI_CMD_WRITE = (1 << 6),
  73. AHCI_CMD_PREFETCH = (1 << 7),
  74. AHCI_CMD_RESET = (1 << 8),
  75. AHCI_CMD_CLR_BUSY = (1 << 10),
  76. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  77. RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
  78. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  79. board_ahci = 0,
  80. board_ahci_vt8251 = 1,
  81. board_ahci_ign_iferr = 2,
  82. board_ahci_sb600 = 3,
  83. board_ahci_mv = 4,
  84. board_ahci_sb700 = 5,
  85. board_ahci_mcp65 = 6,
  86. /* global controller registers */
  87. HOST_CAP = 0x00, /* host capabilities */
  88. HOST_CTL = 0x04, /* global host control */
  89. HOST_IRQ_STAT = 0x08, /* interrupt status */
  90. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  91. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  92. /* HOST_CTL bits */
  93. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  94. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  95. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  96. /* HOST_CAP bits */
  97. HOST_CAP_SSC = (1 << 14), /* Slumber capable */
  98. HOST_CAP_PMP = (1 << 17), /* Port Multiplier support */
  99. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  100. HOST_CAP_ALPM = (1 << 26), /* Aggressive Link PM support */
  101. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  102. HOST_CAP_SNTF = (1 << 29), /* SNotification register */
  103. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  104. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  105. /* registers for each SATA port */
  106. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  107. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  108. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  109. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  110. PORT_IRQ_STAT = 0x10, /* interrupt status */
  111. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  112. PORT_CMD = 0x18, /* port command */
  113. PORT_TFDATA = 0x20, /* taskfile data */
  114. PORT_SIG = 0x24, /* device TF signature */
  115. PORT_CMD_ISSUE = 0x38, /* command issue */
  116. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  117. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  118. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  119. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  120. PORT_SCR_NTF = 0x3c, /* SATA phy register: SNotification */
  121. /* PORT_IRQ_{STAT,MASK} bits */
  122. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  123. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  124. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  125. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  126. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  127. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  128. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  129. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  130. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  131. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  132. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  133. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  134. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  135. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  136. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  137. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  138. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  139. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  140. PORT_IRQ_IF_ERR |
  141. PORT_IRQ_CONNECT |
  142. PORT_IRQ_PHYRDY |
  143. PORT_IRQ_UNK_FIS |
  144. PORT_IRQ_BAD_PMP,
  145. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  146. PORT_IRQ_TF_ERR |
  147. PORT_IRQ_HBUS_DATA_ERR,
  148. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  149. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  150. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  151. /* PORT_CMD bits */
  152. PORT_CMD_ASP = (1 << 27), /* Aggressive Slumber/Partial */
  153. PORT_CMD_ALPE = (1 << 26), /* Aggressive Link PM enable */
  154. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  155. PORT_CMD_PMP = (1 << 17), /* PMP attached */
  156. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  157. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  158. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  159. PORT_CMD_CLO = (1 << 3), /* Command list override */
  160. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  161. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  162. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  163. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  164. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  165. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  166. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  167. /* hpriv->flags bits */
  168. AHCI_HFLAG_NO_NCQ = (1 << 0),
  169. AHCI_HFLAG_IGN_IRQ_IF_ERR = (1 << 1), /* ignore IRQ_IF_ERR */
  170. AHCI_HFLAG_IGN_SERR_INTERNAL = (1 << 2), /* ignore SERR_INTERNAL */
  171. AHCI_HFLAG_32BIT_ONLY = (1 << 3), /* force 32bit */
  172. AHCI_HFLAG_MV_PATA = (1 << 4), /* PATA port */
  173. AHCI_HFLAG_NO_MSI = (1 << 5), /* no PCI MSI */
  174. AHCI_HFLAG_NO_PMP = (1 << 6), /* no PMP */
  175. AHCI_HFLAG_NO_HOTPLUG = (1 << 7), /* ignore PxSERR.DIAG.N */
  176. AHCI_HFLAG_SECT255 = (1 << 8), /* max 255 sectors */
  177. AHCI_HFLAG_YES_NCQ = (1 << 9), /* force NCQ cap on */
  178. /* ap->flags bits */
  179. AHCI_FLAG_COMMON = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  180. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  181. ATA_FLAG_ACPI_SATA | ATA_FLAG_AN |
  182. ATA_FLAG_IPM,
  183. ICH_MAP = 0x90, /* ICH MAP register */
  184. };
  185. struct ahci_cmd_hdr {
  186. __le32 opts;
  187. __le32 status;
  188. __le32 tbl_addr;
  189. __le32 tbl_addr_hi;
  190. __le32 reserved[4];
  191. };
  192. struct ahci_sg {
  193. __le32 addr;
  194. __le32 addr_hi;
  195. __le32 reserved;
  196. __le32 flags_size;
  197. };
  198. struct ahci_host_priv {
  199. unsigned int flags; /* AHCI_HFLAG_* */
  200. u32 cap; /* cap to use */
  201. u32 port_map; /* port map to use */
  202. u32 saved_cap; /* saved initial cap */
  203. u32 saved_port_map; /* saved initial port_map */
  204. };
  205. struct ahci_port_priv {
  206. struct ata_link *active_link;
  207. struct ahci_cmd_hdr *cmd_slot;
  208. dma_addr_t cmd_slot_dma;
  209. void *cmd_tbl;
  210. dma_addr_t cmd_tbl_dma;
  211. void *rx_fis;
  212. dma_addr_t rx_fis_dma;
  213. /* for NCQ spurious interrupt analysis */
  214. unsigned int ncq_saw_d2h:1;
  215. unsigned int ncq_saw_dmas:1;
  216. unsigned int ncq_saw_sdb:1;
  217. u32 intr_mask; /* interrupts to enable */
  218. };
  219. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
  220. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
  221. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  222. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  223. static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
  224. static int ahci_port_start(struct ata_port *ap);
  225. static void ahci_port_stop(struct ata_port *ap);
  226. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  227. static void ahci_freeze(struct ata_port *ap);
  228. static void ahci_thaw(struct ata_port *ap);
  229. static void ahci_pmp_attach(struct ata_port *ap);
  230. static void ahci_pmp_detach(struct ata_port *ap);
  231. static int ahci_softreset(struct ata_link *link, unsigned int *class,
  232. unsigned long deadline);
  233. static int ahci_sb600_softreset(struct ata_link *link, unsigned int *class,
  234. unsigned long deadline);
  235. static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  236. unsigned long deadline);
  237. static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
  238. unsigned long deadline);
  239. static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
  240. unsigned long deadline);
  241. static void ahci_postreset(struct ata_link *link, unsigned int *class);
  242. static void ahci_error_handler(struct ata_port *ap);
  243. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  244. static int ahci_port_resume(struct ata_port *ap);
  245. static void ahci_dev_config(struct ata_device *dev);
  246. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl);
  247. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  248. u32 opts);
  249. #ifdef CONFIG_PM
  250. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  251. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  252. static int ahci_pci_device_resume(struct pci_dev *pdev);
  253. #endif
  254. static struct device_attribute *ahci_shost_attrs[] = {
  255. &dev_attr_link_power_management_policy,
  256. NULL
  257. };
  258. static struct scsi_host_template ahci_sht = {
  259. ATA_NCQ_SHT(DRV_NAME),
  260. .can_queue = AHCI_MAX_CMDS - 1,
  261. .sg_tablesize = AHCI_MAX_SG,
  262. .dma_boundary = AHCI_DMA_BOUNDARY,
  263. .shost_attrs = ahci_shost_attrs,
  264. };
  265. static struct ata_port_operations ahci_ops = {
  266. .inherits = &sata_pmp_port_ops,
  267. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  268. .qc_prep = ahci_qc_prep,
  269. .qc_issue = ahci_qc_issue,
  270. .qc_fill_rtf = ahci_qc_fill_rtf,
  271. .freeze = ahci_freeze,
  272. .thaw = ahci_thaw,
  273. .softreset = ahci_softreset,
  274. .hardreset = ahci_hardreset,
  275. .postreset = ahci_postreset,
  276. .pmp_softreset = ahci_softreset,
  277. .error_handler = ahci_error_handler,
  278. .post_internal_cmd = ahci_post_internal_cmd,
  279. .dev_config = ahci_dev_config,
  280. .scr_read = ahci_scr_read,
  281. .scr_write = ahci_scr_write,
  282. .pmp_attach = ahci_pmp_attach,
  283. .pmp_detach = ahci_pmp_detach,
  284. .enable_pm = ahci_enable_alpm,
  285. .disable_pm = ahci_disable_alpm,
  286. #ifdef CONFIG_PM
  287. .port_suspend = ahci_port_suspend,
  288. .port_resume = ahci_port_resume,
  289. #endif
  290. .port_start = ahci_port_start,
  291. .port_stop = ahci_port_stop,
  292. };
  293. static struct ata_port_operations ahci_vt8251_ops = {
  294. .inherits = &ahci_ops,
  295. .hardreset = ahci_vt8251_hardreset,
  296. };
  297. static struct ata_port_operations ahci_p5wdh_ops = {
  298. .inherits = &ahci_ops,
  299. .hardreset = ahci_p5wdh_hardreset,
  300. };
  301. static struct ata_port_operations ahci_sb600_ops = {
  302. .inherits = &ahci_ops,
  303. .softreset = ahci_sb600_softreset,
  304. .pmp_softreset = ahci_sb600_softreset,
  305. };
  306. #define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
  307. static const struct ata_port_info ahci_port_info[] = {
  308. /* board_ahci */
  309. {
  310. .flags = AHCI_FLAG_COMMON,
  311. .pio_mask = 0x1f, /* pio0-4 */
  312. .udma_mask = ATA_UDMA6,
  313. .port_ops = &ahci_ops,
  314. },
  315. /* board_ahci_vt8251 */
  316. {
  317. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_PMP),
  318. .flags = AHCI_FLAG_COMMON,
  319. .pio_mask = 0x1f, /* pio0-4 */
  320. .udma_mask = ATA_UDMA6,
  321. .port_ops = &ahci_vt8251_ops,
  322. },
  323. /* board_ahci_ign_iferr */
  324. {
  325. AHCI_HFLAGS (AHCI_HFLAG_IGN_IRQ_IF_ERR),
  326. .flags = AHCI_FLAG_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_MSI |
  335. AHCI_HFLAG_SECT255),
  336. .flags = AHCI_FLAG_COMMON,
  337. .pio_mask = 0x1f, /* pio0-4 */
  338. .udma_mask = ATA_UDMA6,
  339. .port_ops = &ahci_sb600_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. .pio_mask = 0x1f, /* pio0-4 */
  348. .udma_mask = ATA_UDMA6,
  349. .port_ops = &ahci_ops,
  350. },
  351. /* board_ahci_sb700 */
  352. {
  353. AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL),
  354. .flags = AHCI_FLAG_COMMON,
  355. .pio_mask = 0x1f, /* pio0-4 */
  356. .udma_mask = ATA_UDMA6,
  357. .port_ops = &ahci_sb600_ops,
  358. },
  359. /* board_ahci_mcp65 */
  360. {
  361. AHCI_HFLAGS (AHCI_HFLAG_YES_NCQ),
  362. .flags = AHCI_FLAG_COMMON,
  363. .pio_mask = 0x1f, /* pio0-4 */
  364. .udma_mask = ATA_UDMA6,
  365. .port_ops = &ahci_ops,
  366. },
  367. };
  368. static const struct pci_device_id ahci_pci_tbl[] = {
  369. /* Intel */
  370. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  371. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  372. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  373. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  374. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  375. { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
  376. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  377. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  378. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  379. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  380. { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
  381. { PCI_VDEVICE(INTEL, 0x2822), board_ahci }, /* ICH8 */
  382. { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
  383. { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
  384. { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
  385. { PCI_VDEVICE(INTEL, 0x2922), board_ahci }, /* ICH9 */
  386. { PCI_VDEVICE(INTEL, 0x2923), board_ahci }, /* ICH9 */
  387. { PCI_VDEVICE(INTEL, 0x2924), board_ahci }, /* ICH9 */
  388. { PCI_VDEVICE(INTEL, 0x2925), board_ahci }, /* ICH9 */
  389. { PCI_VDEVICE(INTEL, 0x2927), board_ahci }, /* ICH9 */
  390. { PCI_VDEVICE(INTEL, 0x2929), board_ahci }, /* ICH9M */
  391. { PCI_VDEVICE(INTEL, 0x292a), board_ahci }, /* ICH9M */
  392. { PCI_VDEVICE(INTEL, 0x292b), board_ahci }, /* ICH9M */
  393. { PCI_VDEVICE(INTEL, 0x292c), board_ahci }, /* ICH9M */
  394. { PCI_VDEVICE(INTEL, 0x292f), board_ahci }, /* ICH9M */
  395. { PCI_VDEVICE(INTEL, 0x294d), board_ahci }, /* ICH9 */
  396. { PCI_VDEVICE(INTEL, 0x294e), board_ahci }, /* ICH9M */
  397. { PCI_VDEVICE(INTEL, 0x502a), board_ahci }, /* Tolapai */
  398. { PCI_VDEVICE(INTEL, 0x502b), board_ahci }, /* Tolapai */
  399. { PCI_VDEVICE(INTEL, 0x3a05), board_ahci }, /* ICH10 */
  400. { PCI_VDEVICE(INTEL, 0x3a25), board_ahci }, /* ICH10 */
  401. /* JMicron 360/1/3/5/6, match class to avoid IDE function */
  402. { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  403. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci_ign_iferr },
  404. /* ATI */
  405. { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600 }, /* ATI SB600 */
  406. { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb700 }, /* ATI SB700/800 */
  407. { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb700 }, /* ATI SB700/800 */
  408. { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb700 }, /* ATI SB700/800 */
  409. { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb700 }, /* ATI SB700/800 */
  410. { PCI_VDEVICE(ATI, 0x4394), board_ahci_sb700 }, /* ATI SB700/800 */
  411. { PCI_VDEVICE(ATI, 0x4395), board_ahci_sb700 }, /* ATI SB700/800 */
  412. /* VIA */
  413. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  414. { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251 }, /* VIA VT8251 */
  415. /* NVIDIA */
  416. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci_mcp65 }, /* MCP65 */
  417. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci_mcp65 }, /* MCP65 */
  418. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci_mcp65 }, /* MCP65 */
  419. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci_mcp65 }, /* MCP65 */
  420. { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci_mcp65 }, /* MCP65 */
  421. { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci_mcp65 }, /* MCP65 */
  422. { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci_mcp65 }, /* MCP65 */
  423. { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci_mcp65 }, /* MCP65 */
  424. { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci }, /* MCP67 */
  425. { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci }, /* MCP67 */
  426. { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci }, /* MCP67 */
  427. { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci }, /* MCP67 */
  428. { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
  429. { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
  430. { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
  431. { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
  432. { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
  433. { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
  434. { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
  435. { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
  436. { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci }, /* MCP73 */
  437. { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci }, /* MCP73 */
  438. { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci }, /* MCP73 */
  439. { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci }, /* MCP73 */
  440. { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci }, /* MCP73 */
  441. { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci }, /* MCP73 */
  442. { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci }, /* MCP73 */
  443. { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci }, /* MCP73 */
  444. { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci }, /* MCP73 */
  445. { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci }, /* MCP73 */
  446. { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci }, /* MCP73 */
  447. { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci }, /* MCP73 */
  448. { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci }, /* MCP77 */
  449. { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci }, /* MCP77 */
  450. { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci }, /* MCP77 */
  451. { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci }, /* MCP77 */
  452. { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci }, /* MCP77 */
  453. { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci }, /* MCP77 */
  454. { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci }, /* MCP77 */
  455. { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci }, /* MCP77 */
  456. { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci }, /* MCP77 */
  457. { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci }, /* MCP77 */
  458. { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci }, /* MCP77 */
  459. { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci }, /* MCP77 */
  460. { PCI_VDEVICE(NVIDIA, 0x0ab4), board_ahci }, /* MCP79 */
  461. { PCI_VDEVICE(NVIDIA, 0x0ab5), board_ahci }, /* MCP79 */
  462. { PCI_VDEVICE(NVIDIA, 0x0ab6), board_ahci }, /* MCP79 */
  463. { PCI_VDEVICE(NVIDIA, 0x0ab7), board_ahci }, /* MCP79 */
  464. { PCI_VDEVICE(NVIDIA, 0x0ab8), board_ahci }, /* MCP79 */
  465. { PCI_VDEVICE(NVIDIA, 0x0ab9), board_ahci }, /* MCP79 */
  466. { PCI_VDEVICE(NVIDIA, 0x0aba), board_ahci }, /* MCP79 */
  467. { PCI_VDEVICE(NVIDIA, 0x0abb), board_ahci }, /* MCP79 */
  468. { PCI_VDEVICE(NVIDIA, 0x0abc), board_ahci }, /* MCP79 */
  469. { PCI_VDEVICE(NVIDIA, 0x0abd), board_ahci }, /* MCP79 */
  470. { PCI_VDEVICE(NVIDIA, 0x0abe), board_ahci }, /* MCP79 */
  471. { PCI_VDEVICE(NVIDIA, 0x0abf), board_ahci }, /* MCP79 */
  472. { PCI_VDEVICE(NVIDIA, 0x0bc8), board_ahci }, /* MCP7B */
  473. { PCI_VDEVICE(NVIDIA, 0x0bc9), board_ahci }, /* MCP7B */
  474. { PCI_VDEVICE(NVIDIA, 0x0bca), board_ahci }, /* MCP7B */
  475. { PCI_VDEVICE(NVIDIA, 0x0bcb), board_ahci }, /* MCP7B */
  476. { PCI_VDEVICE(NVIDIA, 0x0bcc), board_ahci }, /* MCP7B */
  477. { PCI_VDEVICE(NVIDIA, 0x0bcd), board_ahci }, /* MCP7B */
  478. { PCI_VDEVICE(NVIDIA, 0x0bce), board_ahci }, /* MCP7B */
  479. { PCI_VDEVICE(NVIDIA, 0x0bcf), board_ahci }, /* MCP7B */
  480. { PCI_VDEVICE(NVIDIA, 0x0bc4), board_ahci }, /* MCP7B */
  481. { PCI_VDEVICE(NVIDIA, 0x0bc5), board_ahci }, /* MCP7B */
  482. { PCI_VDEVICE(NVIDIA, 0x0bc6), board_ahci }, /* MCP7B */
  483. { PCI_VDEVICE(NVIDIA, 0x0bc7), board_ahci }, /* MCP7B */
  484. /* SiS */
  485. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  486. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  487. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  488. /* Marvell */
  489. { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv }, /* 6145 */
  490. { PCI_VDEVICE(MARVELL, 0x6121), board_ahci_mv }, /* 6121 */
  491. /* Generic, PCI class code for AHCI */
  492. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  493. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
  494. { } /* terminate list */
  495. };
  496. static struct pci_driver ahci_pci_driver = {
  497. .name = DRV_NAME,
  498. .id_table = ahci_pci_tbl,
  499. .probe = ahci_init_one,
  500. .remove = ata_pci_remove_one,
  501. #ifdef CONFIG_PM
  502. .suspend = ahci_pci_device_suspend,
  503. .resume = ahci_pci_device_resume,
  504. #endif
  505. };
  506. static inline int ahci_nr_ports(u32 cap)
  507. {
  508. return (cap & 0x1f) + 1;
  509. }
  510. static inline void __iomem *__ahci_port_base(struct ata_host *host,
  511. unsigned int port_no)
  512. {
  513. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  514. return mmio + 0x100 + (port_no * 0x80);
  515. }
  516. static inline void __iomem *ahci_port_base(struct ata_port *ap)
  517. {
  518. return __ahci_port_base(ap->host, ap->port_no);
  519. }
  520. static void ahci_enable_ahci(void __iomem *mmio)
  521. {
  522. int i;
  523. u32 tmp;
  524. /* turn on AHCI_EN */
  525. tmp = readl(mmio + HOST_CTL);
  526. if (tmp & HOST_AHCI_EN)
  527. return;
  528. /* Some controllers need AHCI_EN to be written multiple times.
  529. * Try a few times before giving up.
  530. */
  531. for (i = 0; i < 5; i++) {
  532. tmp |= HOST_AHCI_EN;
  533. writel(tmp, mmio + HOST_CTL);
  534. tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
  535. if (tmp & HOST_AHCI_EN)
  536. return;
  537. msleep(10);
  538. }
  539. WARN_ON(1);
  540. }
  541. /**
  542. * ahci_save_initial_config - Save and fixup initial config values
  543. * @pdev: target PCI device
  544. * @hpriv: host private area to store config values
  545. *
  546. * Some registers containing configuration info might be setup by
  547. * BIOS and might be cleared on reset. This function saves the
  548. * initial values of those registers into @hpriv such that they
  549. * can be restored after controller reset.
  550. *
  551. * If inconsistent, config values are fixed up by this function.
  552. *
  553. * LOCKING:
  554. * None.
  555. */
  556. static void ahci_save_initial_config(struct pci_dev *pdev,
  557. struct ahci_host_priv *hpriv)
  558. {
  559. void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
  560. u32 cap, port_map;
  561. int i;
  562. int mv;
  563. /* make sure AHCI mode is enabled before accessing CAP */
  564. ahci_enable_ahci(mmio);
  565. /* Values prefixed with saved_ are written back to host after
  566. * reset. Values without are used for driver operation.
  567. */
  568. hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
  569. hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
  570. /* some chips have errata preventing 64bit use */
  571. if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
  572. dev_printk(KERN_INFO, &pdev->dev,
  573. "controller can't do 64bit DMA, forcing 32bit\n");
  574. cap &= ~HOST_CAP_64;
  575. }
  576. if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
  577. dev_printk(KERN_INFO, &pdev->dev,
  578. "controller can't do NCQ, turning off CAP_NCQ\n");
  579. cap &= ~HOST_CAP_NCQ;
  580. }
  581. if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
  582. dev_printk(KERN_INFO, &pdev->dev,
  583. "controller can do NCQ, turning on CAP_NCQ\n");
  584. cap |= HOST_CAP_NCQ;
  585. }
  586. if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
  587. dev_printk(KERN_INFO, &pdev->dev,
  588. "controller can't do PMP, turning off CAP_PMP\n");
  589. cap &= ~HOST_CAP_PMP;
  590. }
  591. /*
  592. * Temporary Marvell 6145 hack: PATA port presence
  593. * is asserted through the standard AHCI port
  594. * presence register, as bit 4 (counting from 0)
  595. */
  596. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  597. if (pdev->device == 0x6121)
  598. mv = 0x3;
  599. else
  600. mv = 0xf;
  601. dev_printk(KERN_ERR, &pdev->dev,
  602. "MV_AHCI HACK: port_map %x -> %x\n",
  603. port_map,
  604. port_map & mv);
  605. port_map &= mv;
  606. }
  607. /* cross check port_map and cap.n_ports */
  608. if (port_map) {
  609. int map_ports = 0;
  610. for (i = 0; i < AHCI_MAX_PORTS; i++)
  611. if (port_map & (1 << i))
  612. map_ports++;
  613. /* If PI has more ports than n_ports, whine, clear
  614. * port_map and let it be generated from n_ports.
  615. */
  616. if (map_ports > ahci_nr_ports(cap)) {
  617. dev_printk(KERN_WARNING, &pdev->dev,
  618. "implemented port map (0x%x) contains more "
  619. "ports than nr_ports (%u), using nr_ports\n",
  620. port_map, ahci_nr_ports(cap));
  621. port_map = 0;
  622. }
  623. }
  624. /* fabricate port_map from cap.nr_ports */
  625. if (!port_map) {
  626. port_map = (1 << ahci_nr_ports(cap)) - 1;
  627. dev_printk(KERN_WARNING, &pdev->dev,
  628. "forcing PORTS_IMPL to 0x%x\n", port_map);
  629. /* write the fixed up value to the PI register */
  630. hpriv->saved_port_map = port_map;
  631. }
  632. /* record values to use during operation */
  633. hpriv->cap = cap;
  634. hpriv->port_map = port_map;
  635. }
  636. /**
  637. * ahci_restore_initial_config - Restore initial config
  638. * @host: target ATA host
  639. *
  640. * Restore initial config stored by ahci_save_initial_config().
  641. *
  642. * LOCKING:
  643. * None.
  644. */
  645. static void ahci_restore_initial_config(struct ata_host *host)
  646. {
  647. struct ahci_host_priv *hpriv = host->private_data;
  648. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  649. writel(hpriv->saved_cap, mmio + HOST_CAP);
  650. writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
  651. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  652. }
  653. static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
  654. {
  655. static const int offset[] = {
  656. [SCR_STATUS] = PORT_SCR_STAT,
  657. [SCR_CONTROL] = PORT_SCR_CTL,
  658. [SCR_ERROR] = PORT_SCR_ERR,
  659. [SCR_ACTIVE] = PORT_SCR_ACT,
  660. [SCR_NOTIFICATION] = PORT_SCR_NTF,
  661. };
  662. struct ahci_host_priv *hpriv = ap->host->private_data;
  663. if (sc_reg < ARRAY_SIZE(offset) &&
  664. (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
  665. return offset[sc_reg];
  666. return 0;
  667. }
  668. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  669. {
  670. void __iomem *port_mmio = ahci_port_base(ap);
  671. int offset = ahci_scr_offset(ap, sc_reg);
  672. if (offset) {
  673. *val = readl(port_mmio + offset);
  674. return 0;
  675. }
  676. return -EINVAL;
  677. }
  678. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  679. {
  680. void __iomem *port_mmio = ahci_port_base(ap);
  681. int offset = ahci_scr_offset(ap, sc_reg);
  682. if (offset) {
  683. writel(val, port_mmio + offset);
  684. return 0;
  685. }
  686. return -EINVAL;
  687. }
  688. static void ahci_start_engine(struct ata_port *ap)
  689. {
  690. void __iomem *port_mmio = ahci_port_base(ap);
  691. u32 tmp;
  692. /* start DMA */
  693. tmp = readl(port_mmio + PORT_CMD);
  694. tmp |= PORT_CMD_START;
  695. writel(tmp, port_mmio + PORT_CMD);
  696. readl(port_mmio + PORT_CMD); /* flush */
  697. }
  698. static int ahci_stop_engine(struct ata_port *ap)
  699. {
  700. void __iomem *port_mmio = ahci_port_base(ap);
  701. u32 tmp;
  702. tmp = readl(port_mmio + PORT_CMD);
  703. /* check if the HBA is idle */
  704. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  705. return 0;
  706. /* setting HBA to idle */
  707. tmp &= ~PORT_CMD_START;
  708. writel(tmp, port_mmio + PORT_CMD);
  709. /* wait for engine to stop. This could be as long as 500 msec */
  710. tmp = ata_wait_register(port_mmio + PORT_CMD,
  711. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  712. if (tmp & PORT_CMD_LIST_ON)
  713. return -EIO;
  714. return 0;
  715. }
  716. static void ahci_start_fis_rx(struct ata_port *ap)
  717. {
  718. void __iomem *port_mmio = ahci_port_base(ap);
  719. struct ahci_host_priv *hpriv = ap->host->private_data;
  720. struct ahci_port_priv *pp = ap->private_data;
  721. u32 tmp;
  722. /* set FIS registers */
  723. if (hpriv->cap & HOST_CAP_64)
  724. writel((pp->cmd_slot_dma >> 16) >> 16,
  725. port_mmio + PORT_LST_ADDR_HI);
  726. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  727. if (hpriv->cap & HOST_CAP_64)
  728. writel((pp->rx_fis_dma >> 16) >> 16,
  729. port_mmio + PORT_FIS_ADDR_HI);
  730. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  731. /* enable FIS reception */
  732. tmp = readl(port_mmio + PORT_CMD);
  733. tmp |= PORT_CMD_FIS_RX;
  734. writel(tmp, port_mmio + PORT_CMD);
  735. /* flush */
  736. readl(port_mmio + PORT_CMD);
  737. }
  738. static int ahci_stop_fis_rx(struct ata_port *ap)
  739. {
  740. void __iomem *port_mmio = ahci_port_base(ap);
  741. u32 tmp;
  742. /* disable FIS reception */
  743. tmp = readl(port_mmio + PORT_CMD);
  744. tmp &= ~PORT_CMD_FIS_RX;
  745. writel(tmp, port_mmio + PORT_CMD);
  746. /* wait for completion, spec says 500ms, give it 1000 */
  747. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  748. PORT_CMD_FIS_ON, 10, 1000);
  749. if (tmp & PORT_CMD_FIS_ON)
  750. return -EBUSY;
  751. return 0;
  752. }
  753. static void ahci_power_up(struct ata_port *ap)
  754. {
  755. struct ahci_host_priv *hpriv = ap->host->private_data;
  756. void __iomem *port_mmio = ahci_port_base(ap);
  757. u32 cmd;
  758. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  759. /* spin up device */
  760. if (hpriv->cap & HOST_CAP_SSS) {
  761. cmd |= PORT_CMD_SPIN_UP;
  762. writel(cmd, port_mmio + PORT_CMD);
  763. }
  764. /* wake up link */
  765. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  766. }
  767. static void ahci_disable_alpm(struct ata_port *ap)
  768. {
  769. struct ahci_host_priv *hpriv = ap->host->private_data;
  770. void __iomem *port_mmio = ahci_port_base(ap);
  771. u32 cmd;
  772. struct ahci_port_priv *pp = ap->private_data;
  773. /* IPM bits should be disabled by libata-core */
  774. /* get the existing command bits */
  775. cmd = readl(port_mmio + PORT_CMD);
  776. /* disable ALPM and ASP */
  777. cmd &= ~PORT_CMD_ASP;
  778. cmd &= ~PORT_CMD_ALPE;
  779. /* force the interface back to active */
  780. cmd |= PORT_CMD_ICC_ACTIVE;
  781. /* write out new cmd value */
  782. writel(cmd, port_mmio + PORT_CMD);
  783. cmd = readl(port_mmio + PORT_CMD);
  784. /* wait 10ms to be sure we've come out of any low power state */
  785. msleep(10);
  786. /* clear out any PhyRdy stuff from interrupt status */
  787. writel(PORT_IRQ_PHYRDY, port_mmio + PORT_IRQ_STAT);
  788. /* go ahead and clean out PhyRdy Change from Serror too */
  789. ahci_scr_write(ap, SCR_ERROR, ((1 << 16) | (1 << 18)));
  790. /*
  791. * Clear flag to indicate that we should ignore all PhyRdy
  792. * state changes
  793. */
  794. hpriv->flags &= ~AHCI_HFLAG_NO_HOTPLUG;
  795. /*
  796. * Enable interrupts on Phy Ready.
  797. */
  798. pp->intr_mask |= PORT_IRQ_PHYRDY;
  799. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  800. /*
  801. * don't change the link pm policy - we can be called
  802. * just to turn of link pm temporarily
  803. */
  804. }
  805. static int ahci_enable_alpm(struct ata_port *ap,
  806. enum link_pm policy)
  807. {
  808. struct ahci_host_priv *hpriv = ap->host->private_data;
  809. void __iomem *port_mmio = ahci_port_base(ap);
  810. u32 cmd;
  811. struct ahci_port_priv *pp = ap->private_data;
  812. u32 asp;
  813. /* Make sure the host is capable of link power management */
  814. if (!(hpriv->cap & HOST_CAP_ALPM))
  815. return -EINVAL;
  816. switch (policy) {
  817. case MAX_PERFORMANCE:
  818. case NOT_AVAILABLE:
  819. /*
  820. * if we came here with NOT_AVAILABLE,
  821. * it just means this is the first time we
  822. * have tried to enable - default to max performance,
  823. * and let the user go to lower power modes on request.
  824. */
  825. ahci_disable_alpm(ap);
  826. return 0;
  827. case MIN_POWER:
  828. /* configure HBA to enter SLUMBER */
  829. asp = PORT_CMD_ASP;
  830. break;
  831. case MEDIUM_POWER:
  832. /* configure HBA to enter PARTIAL */
  833. asp = 0;
  834. break;
  835. default:
  836. return -EINVAL;
  837. }
  838. /*
  839. * Disable interrupts on Phy Ready. This keeps us from
  840. * getting woken up due to spurious phy ready interrupts
  841. * TBD - Hot plug should be done via polling now, is
  842. * that even supported?
  843. */
  844. pp->intr_mask &= ~PORT_IRQ_PHYRDY;
  845. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  846. /*
  847. * Set a flag to indicate that we should ignore all PhyRdy
  848. * state changes since these can happen now whenever we
  849. * change link state
  850. */
  851. hpriv->flags |= AHCI_HFLAG_NO_HOTPLUG;
  852. /* get the existing command bits */
  853. cmd = readl(port_mmio + PORT_CMD);
  854. /*
  855. * Set ASP based on Policy
  856. */
  857. cmd |= asp;
  858. /*
  859. * Setting this bit will instruct the HBA to aggressively
  860. * enter a lower power link state when it's appropriate and
  861. * based on the value set above for ASP
  862. */
  863. cmd |= PORT_CMD_ALPE;
  864. /* write out new cmd value */
  865. writel(cmd, port_mmio + PORT_CMD);
  866. cmd = readl(port_mmio + PORT_CMD);
  867. /* IPM bits should be set by libata-core */
  868. return 0;
  869. }
  870. #ifdef CONFIG_PM
  871. static void ahci_power_down(struct ata_port *ap)
  872. {
  873. struct ahci_host_priv *hpriv = ap->host->private_data;
  874. void __iomem *port_mmio = ahci_port_base(ap);
  875. u32 cmd, scontrol;
  876. if (!(hpriv->cap & HOST_CAP_SSS))
  877. return;
  878. /* put device into listen mode, first set PxSCTL.DET to 0 */
  879. scontrol = readl(port_mmio + PORT_SCR_CTL);
  880. scontrol &= ~0xf;
  881. writel(scontrol, port_mmio + PORT_SCR_CTL);
  882. /* then set PxCMD.SUD to 0 */
  883. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  884. cmd &= ~PORT_CMD_SPIN_UP;
  885. writel(cmd, port_mmio + PORT_CMD);
  886. }
  887. #endif
  888. static void ahci_start_port(struct ata_port *ap)
  889. {
  890. /* enable FIS reception */
  891. ahci_start_fis_rx(ap);
  892. /* enable DMA */
  893. ahci_start_engine(ap);
  894. }
  895. static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
  896. {
  897. int rc;
  898. /* disable DMA */
  899. rc = ahci_stop_engine(ap);
  900. if (rc) {
  901. *emsg = "failed to stop engine";
  902. return rc;
  903. }
  904. /* disable FIS reception */
  905. rc = ahci_stop_fis_rx(ap);
  906. if (rc) {
  907. *emsg = "failed stop FIS RX";
  908. return rc;
  909. }
  910. return 0;
  911. }
  912. static int ahci_reset_controller(struct ata_host *host)
  913. {
  914. struct pci_dev *pdev = to_pci_dev(host->dev);
  915. struct ahci_host_priv *hpriv = host->private_data;
  916. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  917. u32 tmp;
  918. /* we must be in AHCI mode, before using anything
  919. * AHCI-specific, such as HOST_RESET.
  920. */
  921. ahci_enable_ahci(mmio);
  922. /* global controller reset */
  923. if (!ahci_skip_host_reset) {
  924. tmp = readl(mmio + HOST_CTL);
  925. if ((tmp & HOST_RESET) == 0) {
  926. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  927. readl(mmio + HOST_CTL); /* flush */
  928. }
  929. /* reset must complete within 1 second, or
  930. * the hardware should be considered fried.
  931. */
  932. ssleep(1);
  933. tmp = readl(mmio + HOST_CTL);
  934. if (tmp & HOST_RESET) {
  935. dev_printk(KERN_ERR, host->dev,
  936. "controller reset failed (0x%x)\n", tmp);
  937. return -EIO;
  938. }
  939. /* turn on AHCI mode */
  940. ahci_enable_ahci(mmio);
  941. /* Some registers might be cleared on reset. Restore
  942. * initial values.
  943. */
  944. ahci_restore_initial_config(host);
  945. } else
  946. dev_printk(KERN_INFO, host->dev,
  947. "skipping global host reset\n");
  948. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  949. u16 tmp16;
  950. /* configure PCS */
  951. pci_read_config_word(pdev, 0x92, &tmp16);
  952. if ((tmp16 & hpriv->port_map) != hpriv->port_map) {
  953. tmp16 |= hpriv->port_map;
  954. pci_write_config_word(pdev, 0x92, tmp16);
  955. }
  956. }
  957. return 0;
  958. }
  959. static void ahci_port_init(struct pci_dev *pdev, struct ata_port *ap,
  960. int port_no, void __iomem *mmio,
  961. void __iomem *port_mmio)
  962. {
  963. const char *emsg = NULL;
  964. int rc;
  965. u32 tmp;
  966. /* make sure port is not active */
  967. rc = ahci_deinit_port(ap, &emsg);
  968. if (rc)
  969. dev_printk(KERN_WARNING, &pdev->dev,
  970. "%s (%d)\n", emsg, rc);
  971. /* clear SError */
  972. tmp = readl(port_mmio + PORT_SCR_ERR);
  973. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  974. writel(tmp, port_mmio + PORT_SCR_ERR);
  975. /* clear port IRQ */
  976. tmp = readl(port_mmio + PORT_IRQ_STAT);
  977. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  978. if (tmp)
  979. writel(tmp, port_mmio + PORT_IRQ_STAT);
  980. writel(1 << port_no, mmio + HOST_IRQ_STAT);
  981. }
  982. static void ahci_init_controller(struct ata_host *host)
  983. {
  984. struct ahci_host_priv *hpriv = host->private_data;
  985. struct pci_dev *pdev = to_pci_dev(host->dev);
  986. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  987. int i;
  988. void __iomem *port_mmio;
  989. u32 tmp;
  990. int mv;
  991. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  992. if (pdev->device == 0x6121)
  993. mv = 2;
  994. else
  995. mv = 4;
  996. port_mmio = __ahci_port_base(host, mv);
  997. writel(0, port_mmio + PORT_IRQ_MASK);
  998. /* clear port IRQ */
  999. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1000. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  1001. if (tmp)
  1002. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1003. }
  1004. for (i = 0; i < host->n_ports; i++) {
  1005. struct ata_port *ap = host->ports[i];
  1006. port_mmio = ahci_port_base(ap);
  1007. if (ata_port_is_dummy(ap))
  1008. continue;
  1009. ahci_port_init(pdev, ap, i, mmio, port_mmio);
  1010. }
  1011. tmp = readl(mmio + HOST_CTL);
  1012. VPRINTK("HOST_CTL 0x%x\n", tmp);
  1013. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  1014. tmp = readl(mmio + HOST_CTL);
  1015. VPRINTK("HOST_CTL 0x%x\n", tmp);
  1016. }
  1017. static void ahci_dev_config(struct ata_device *dev)
  1018. {
  1019. struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
  1020. if (hpriv->flags & AHCI_HFLAG_SECT255) {
  1021. dev->max_sectors = 255;
  1022. ata_dev_printk(dev, KERN_INFO,
  1023. "SB600 AHCI: limiting to 255 sectors per cmd\n");
  1024. }
  1025. }
  1026. static unsigned int ahci_dev_classify(struct ata_port *ap)
  1027. {
  1028. void __iomem *port_mmio = ahci_port_base(ap);
  1029. struct ata_taskfile tf;
  1030. u32 tmp;
  1031. tmp = readl(port_mmio + PORT_SIG);
  1032. tf.lbah = (tmp >> 24) & 0xff;
  1033. tf.lbam = (tmp >> 16) & 0xff;
  1034. tf.lbal = (tmp >> 8) & 0xff;
  1035. tf.nsect = (tmp) & 0xff;
  1036. return ata_dev_classify(&tf);
  1037. }
  1038. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  1039. u32 opts)
  1040. {
  1041. dma_addr_t cmd_tbl_dma;
  1042. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  1043. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  1044. pp->cmd_slot[tag].status = 0;
  1045. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  1046. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  1047. }
  1048. static int ahci_kick_engine(struct ata_port *ap, int force_restart)
  1049. {
  1050. void __iomem *port_mmio = ahci_port_base(ap);
  1051. struct ahci_host_priv *hpriv = ap->host->private_data;
  1052. u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
  1053. u32 tmp;
  1054. int busy, rc;
  1055. /* do we need to kick the port? */
  1056. busy = status & (ATA_BUSY | ATA_DRQ);
  1057. if (!busy && !force_restart)
  1058. return 0;
  1059. /* stop engine */
  1060. rc = ahci_stop_engine(ap);
  1061. if (rc)
  1062. goto out_restart;
  1063. /* need to do CLO? */
  1064. if (!busy) {
  1065. rc = 0;
  1066. goto out_restart;
  1067. }
  1068. if (!(hpriv->cap & HOST_CAP_CLO)) {
  1069. rc = -EOPNOTSUPP;
  1070. goto out_restart;
  1071. }
  1072. /* perform CLO */
  1073. tmp = readl(port_mmio + PORT_CMD);
  1074. tmp |= PORT_CMD_CLO;
  1075. writel(tmp, port_mmio + PORT_CMD);
  1076. rc = 0;
  1077. tmp = ata_wait_register(port_mmio + PORT_CMD,
  1078. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  1079. if (tmp & PORT_CMD_CLO)
  1080. rc = -EIO;
  1081. /* restart engine */
  1082. out_restart:
  1083. ahci_start_engine(ap);
  1084. return rc;
  1085. }
  1086. static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
  1087. struct ata_taskfile *tf, int is_cmd, u16 flags,
  1088. unsigned long timeout_msec)
  1089. {
  1090. const u32 cmd_fis_len = 5; /* five dwords */
  1091. struct ahci_port_priv *pp = ap->private_data;
  1092. void __iomem *port_mmio = ahci_port_base(ap);
  1093. u8 *fis = pp->cmd_tbl;
  1094. u32 tmp;
  1095. /* prep the command */
  1096. ata_tf_to_fis(tf, pmp, is_cmd, fis);
  1097. ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
  1098. /* issue & wait */
  1099. writel(1, port_mmio + PORT_CMD_ISSUE);
  1100. if (timeout_msec) {
  1101. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1,
  1102. 1, timeout_msec);
  1103. if (tmp & 0x1) {
  1104. ahci_kick_engine(ap, 1);
  1105. return -EBUSY;
  1106. }
  1107. } else
  1108. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1109. return 0;
  1110. }
  1111. static int ahci_do_softreset(struct ata_link *link, unsigned int *class,
  1112. int pmp, unsigned long deadline,
  1113. int (*check_ready)(struct ata_link *link))
  1114. {
  1115. struct ata_port *ap = link->ap;
  1116. const char *reason = NULL;
  1117. unsigned long now, msecs;
  1118. struct ata_taskfile tf;
  1119. int rc;
  1120. DPRINTK("ENTER\n");
  1121. /* prepare for SRST (AHCI-1.1 10.4.1) */
  1122. rc = ahci_kick_engine(ap, 1);
  1123. if (rc && rc != -EOPNOTSUPP)
  1124. ata_link_printk(link, KERN_WARNING,
  1125. "failed to reset engine (errno=%d)\n", rc);
  1126. ata_tf_init(link->device, &tf);
  1127. /* issue the first D2H Register FIS */
  1128. msecs = 0;
  1129. now = jiffies;
  1130. if (time_after(now, deadline))
  1131. msecs = jiffies_to_msecs(deadline - now);
  1132. tf.ctl |= ATA_SRST;
  1133. if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
  1134. AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
  1135. rc = -EIO;
  1136. reason = "1st FIS failed";
  1137. goto fail;
  1138. }
  1139. /* spec says at least 5us, but be generous and sleep for 1ms */
  1140. msleep(1);
  1141. /* issue the second D2H Register FIS */
  1142. tf.ctl &= ~ATA_SRST;
  1143. ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
  1144. /* wait for link to become ready */
  1145. rc = ata_wait_after_reset(link, deadline, check_ready);
  1146. /* link occupied, -ENODEV too is an error */
  1147. if (rc) {
  1148. reason = "device not ready";
  1149. goto fail;
  1150. }
  1151. *class = ahci_dev_classify(ap);
  1152. DPRINTK("EXIT, class=%u\n", *class);
  1153. return 0;
  1154. fail:
  1155. ata_link_printk(link, KERN_ERR, "softreset failed (%s)\n", reason);
  1156. return rc;
  1157. }
  1158. static int ahci_check_ready(struct ata_link *link)
  1159. {
  1160. void __iomem *port_mmio = ahci_port_base(link->ap);
  1161. u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
  1162. return ata_check_ready(status);
  1163. }
  1164. static int ahci_softreset(struct ata_link *link, unsigned int *class,
  1165. unsigned long deadline)
  1166. {
  1167. int pmp = sata_srst_pmp(link);
  1168. DPRINTK("ENTER\n");
  1169. return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
  1170. }
  1171. static int ahci_sb600_check_ready(struct ata_link *link)
  1172. {
  1173. void __iomem *port_mmio = ahci_port_base(link->ap);
  1174. u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
  1175. u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
  1176. /*
  1177. * There is no need to check TFDATA if BAD PMP is found due to HW bug,
  1178. * which can save timeout delay.
  1179. */
  1180. if (irq_status & PORT_IRQ_BAD_PMP)
  1181. return -EIO;
  1182. return ata_check_ready(status);
  1183. }
  1184. static int ahci_sb600_softreset(struct ata_link *link, unsigned int *class,
  1185. unsigned long deadline)
  1186. {
  1187. struct ata_port *ap = link->ap;
  1188. void __iomem *port_mmio = ahci_port_base(ap);
  1189. int pmp = sata_srst_pmp(link);
  1190. int rc;
  1191. u32 irq_sts;
  1192. DPRINTK("ENTER\n");
  1193. rc = ahci_do_softreset(link, class, pmp, deadline,
  1194. ahci_sb600_check_ready);
  1195. /*
  1196. * Soft reset fails on some ATI chips with IPMS set when PMP
  1197. * is enabled but SATA HDD/ODD is connected to SATA port,
  1198. * do soft reset again to port 0.
  1199. */
  1200. if (rc == -EIO) {
  1201. irq_sts = readl(port_mmio + PORT_IRQ_STAT);
  1202. if (irq_sts & PORT_IRQ_BAD_PMP) {
  1203. ata_link_printk(link, KERN_WARNING,
  1204. "failed due to HW bug, retry pmp=0\n");
  1205. rc = ahci_do_softreset(link, class, 0, deadline,
  1206. ahci_check_ready);
  1207. }
  1208. }
  1209. return rc;
  1210. }
  1211. static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  1212. unsigned long deadline)
  1213. {
  1214. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  1215. struct ata_port *ap = link->ap;
  1216. struct ahci_port_priv *pp = ap->private_data;
  1217. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1218. struct ata_taskfile tf;
  1219. bool online;
  1220. int rc;
  1221. DPRINTK("ENTER\n");
  1222. ahci_stop_engine(ap);
  1223. /* clear D2H reception area to properly wait for D2H FIS */
  1224. ata_tf_init(link->device, &tf);
  1225. tf.command = 0x80;
  1226. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  1227. rc = sata_link_hardreset(link, timing, deadline, &online,
  1228. ahci_check_ready);
  1229. ahci_start_engine(ap);
  1230. if (online)
  1231. *class = ahci_dev_classify(ap);
  1232. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  1233. return rc;
  1234. }
  1235. static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
  1236. unsigned long deadline)
  1237. {
  1238. struct ata_port *ap = link->ap;
  1239. bool online;
  1240. int rc;
  1241. DPRINTK("ENTER\n");
  1242. ahci_stop_engine(ap);
  1243. rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
  1244. deadline, &online, NULL);
  1245. ahci_start_engine(ap);
  1246. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  1247. /* vt8251 doesn't clear BSY on signature FIS reception,
  1248. * request follow-up softreset.
  1249. */
  1250. return online ? -EAGAIN : rc;
  1251. }
  1252. static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
  1253. unsigned long deadline)
  1254. {
  1255. struct ata_port *ap = link->ap;
  1256. struct ahci_port_priv *pp = ap->private_data;
  1257. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1258. struct ata_taskfile tf;
  1259. bool online;
  1260. int rc;
  1261. ahci_stop_engine(ap);
  1262. /* clear D2H reception area to properly wait for D2H FIS */
  1263. ata_tf_init(link->device, &tf);
  1264. tf.command = 0x80;
  1265. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  1266. rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
  1267. deadline, &online, NULL);
  1268. ahci_start_engine(ap);
  1269. /* The pseudo configuration device on SIMG4726 attached to
  1270. * ASUS P5W-DH Deluxe doesn't send signature FIS after
  1271. * hardreset if no device is attached to the first downstream
  1272. * port && the pseudo device locks up on SRST w/ PMP==0. To
  1273. * work around this, wait for !BSY only briefly. If BSY isn't
  1274. * cleared, perform CLO and proceed to IDENTIFY (achieved by
  1275. * ATA_LFLAG_NO_SRST and ATA_LFLAG_ASSUME_ATA).
  1276. *
  1277. * Wait for two seconds. Devices attached to downstream port
  1278. * which can't process the following IDENTIFY after this will
  1279. * have to be reset again. For most cases, this should
  1280. * suffice while making probing snappish enough.
  1281. */
  1282. if (online) {
  1283. rc = ata_wait_after_reset(link, jiffies + 2 * HZ,
  1284. ahci_check_ready);
  1285. if (rc)
  1286. ahci_kick_engine(ap, 0);
  1287. }
  1288. return rc;
  1289. }
  1290. static void ahci_postreset(struct ata_link *link, unsigned int *class)
  1291. {
  1292. struct ata_port *ap = link->ap;
  1293. void __iomem *port_mmio = ahci_port_base(ap);
  1294. u32 new_tmp, tmp;
  1295. ata_std_postreset(link, class);
  1296. /* Make sure port's ATAPI bit is set appropriately */
  1297. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  1298. if (*class == ATA_DEV_ATAPI)
  1299. new_tmp |= PORT_CMD_ATAPI;
  1300. else
  1301. new_tmp &= ~PORT_CMD_ATAPI;
  1302. if (new_tmp != tmp) {
  1303. writel(new_tmp, port_mmio + PORT_CMD);
  1304. readl(port_mmio + PORT_CMD); /* flush */
  1305. }
  1306. }
  1307. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  1308. {
  1309. struct scatterlist *sg;
  1310. struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  1311. unsigned int si;
  1312. VPRINTK("ENTER\n");
  1313. /*
  1314. * Next, the S/G list.
  1315. */
  1316. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1317. dma_addr_t addr = sg_dma_address(sg);
  1318. u32 sg_len = sg_dma_len(sg);
  1319. ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
  1320. ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1321. ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
  1322. }
  1323. return si;
  1324. }
  1325. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  1326. {
  1327. struct ata_port *ap = qc->ap;
  1328. struct ahci_port_priv *pp = ap->private_data;
  1329. int is_atapi = ata_is_atapi(qc->tf.protocol);
  1330. void *cmd_tbl;
  1331. u32 opts;
  1332. const u32 cmd_fis_len = 5; /* five dwords */
  1333. unsigned int n_elem;
  1334. /*
  1335. * Fill in command table information. First, the header,
  1336. * a SATA Register - Host to Device command FIS.
  1337. */
  1338. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  1339. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
  1340. if (is_atapi) {
  1341. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  1342. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  1343. }
  1344. n_elem = 0;
  1345. if (qc->flags & ATA_QCFLAG_DMAMAP)
  1346. n_elem = ahci_fill_sg(qc, cmd_tbl);
  1347. /*
  1348. * Fill in command slot information.
  1349. */
  1350. opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
  1351. if (qc->tf.flags & ATA_TFLAG_WRITE)
  1352. opts |= AHCI_CMD_WRITE;
  1353. if (is_atapi)
  1354. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  1355. ahci_fill_cmd_slot(pp, qc->tag, opts);
  1356. }
  1357. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  1358. {
  1359. struct ahci_host_priv *hpriv = ap->host->private_data;
  1360. struct ahci_port_priv *pp = ap->private_data;
  1361. struct ata_eh_info *host_ehi = &ap->link.eh_info;
  1362. struct ata_link *link = NULL;
  1363. struct ata_queued_cmd *active_qc;
  1364. struct ata_eh_info *active_ehi;
  1365. u32 serror;
  1366. /* determine active link */
  1367. ata_port_for_each_link(link, ap)
  1368. if (ata_link_active(link))
  1369. break;
  1370. if (!link)
  1371. link = &ap->link;
  1372. active_qc = ata_qc_from_tag(ap, link->active_tag);
  1373. active_ehi = &link->eh_info;
  1374. /* record irq stat */
  1375. ata_ehi_clear_desc(host_ehi);
  1376. ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
  1377. /* AHCI needs SError cleared; otherwise, it might lock up */
  1378. ahci_scr_read(ap, SCR_ERROR, &serror);
  1379. ahci_scr_write(ap, SCR_ERROR, serror);
  1380. host_ehi->serror |= serror;
  1381. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  1382. if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
  1383. irq_stat &= ~PORT_IRQ_IF_ERR;
  1384. if (irq_stat & PORT_IRQ_TF_ERR) {
  1385. /* If qc is active, charge it; otherwise, the active
  1386. * link. There's no active qc on NCQ errors. It will
  1387. * be determined by EH by reading log page 10h.
  1388. */
  1389. if (active_qc)
  1390. active_qc->err_mask |= AC_ERR_DEV;
  1391. else
  1392. active_ehi->err_mask |= AC_ERR_DEV;
  1393. if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
  1394. host_ehi->serror &= ~SERR_INTERNAL;
  1395. }
  1396. if (irq_stat & PORT_IRQ_UNK_FIS) {
  1397. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  1398. active_ehi->err_mask |= AC_ERR_HSM;
  1399. active_ehi->action |= ATA_EH_RESET;
  1400. ata_ehi_push_desc(active_ehi,
  1401. "unknown FIS %08x %08x %08x %08x" ,
  1402. unk[0], unk[1], unk[2], unk[3]);
  1403. }
  1404. if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
  1405. active_ehi->err_mask |= AC_ERR_HSM;
  1406. active_ehi->action |= ATA_EH_RESET;
  1407. ata_ehi_push_desc(active_ehi, "incorrect PMP");
  1408. }
  1409. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  1410. host_ehi->err_mask |= AC_ERR_HOST_BUS;
  1411. host_ehi->action |= ATA_EH_RESET;
  1412. ata_ehi_push_desc(host_ehi, "host bus error");
  1413. }
  1414. if (irq_stat & PORT_IRQ_IF_ERR) {
  1415. host_ehi->err_mask |= AC_ERR_ATA_BUS;
  1416. host_ehi->action |= ATA_EH_RESET;
  1417. ata_ehi_push_desc(host_ehi, "interface fatal error");
  1418. }
  1419. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  1420. ata_ehi_hotplugged(host_ehi);
  1421. ata_ehi_push_desc(host_ehi, "%s",
  1422. irq_stat & PORT_IRQ_CONNECT ?
  1423. "connection status changed" : "PHY RDY changed");
  1424. }
  1425. /* okay, let's hand over to EH */
  1426. if (irq_stat & PORT_IRQ_FREEZE)
  1427. ata_port_freeze(ap);
  1428. else
  1429. ata_port_abort(ap);
  1430. }
  1431. static void ahci_port_intr(struct ata_port *ap)
  1432. {
  1433. void __iomem *port_mmio = ahci_port_base(ap);
  1434. struct ata_eh_info *ehi = &ap->link.eh_info;
  1435. struct ahci_port_priv *pp = ap->private_data;
  1436. struct ahci_host_priv *hpriv = ap->host->private_data;
  1437. int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
  1438. u32 status, qc_active;
  1439. int rc;
  1440. status = readl(port_mmio + PORT_IRQ_STAT);
  1441. writel(status, port_mmio + PORT_IRQ_STAT);
  1442. /* ignore BAD_PMP while resetting */
  1443. if (unlikely(resetting))
  1444. status &= ~PORT_IRQ_BAD_PMP;
  1445. /* If we are getting PhyRdy, this is
  1446. * just a power state change, we should
  1447. * clear out this, plus the PhyRdy/Comm
  1448. * Wake bits from Serror
  1449. */
  1450. if ((hpriv->flags & AHCI_HFLAG_NO_HOTPLUG) &&
  1451. (status & PORT_IRQ_PHYRDY)) {
  1452. status &= ~PORT_IRQ_PHYRDY;
  1453. ahci_scr_write(ap, SCR_ERROR, ((1 << 16) | (1 << 18)));
  1454. }
  1455. if (unlikely(status & PORT_IRQ_ERROR)) {
  1456. ahci_error_intr(ap, status);
  1457. return;
  1458. }
  1459. if (status & PORT_IRQ_SDB_FIS) {
  1460. /* If SNotification is available, leave notification
  1461. * handling to sata_async_notification(). If not,
  1462. * emulate it by snooping SDB FIS RX area.
  1463. *
  1464. * Snooping FIS RX area is probably cheaper than
  1465. * poking SNotification but some constrollers which
  1466. * implement SNotification, ICH9 for example, don't
  1467. * store AN SDB FIS into receive area.
  1468. */
  1469. if (hpriv->cap & HOST_CAP_SNTF)
  1470. sata_async_notification(ap);
  1471. else {
  1472. /* If the 'N' bit in word 0 of the FIS is set,
  1473. * we just received asynchronous notification.
  1474. * Tell libata about it.
  1475. */
  1476. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1477. u32 f0 = le32_to_cpu(f[0]);
  1478. if (f0 & (1 << 15))
  1479. sata_async_notification(ap);
  1480. }
  1481. }
  1482. /* pp->active_link is valid iff any command is in flight */
  1483. if (ap->qc_active && pp->active_link->sactive)
  1484. qc_active = readl(port_mmio + PORT_SCR_ACT);
  1485. else
  1486. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  1487. rc = ata_qc_complete_multiple(ap, qc_active);
  1488. /* while resetting, invalid completions are expected */
  1489. if (unlikely(rc < 0 && !resetting)) {
  1490. ehi->err_mask |= AC_ERR_HSM;
  1491. ehi->action |= ATA_EH_RESET;
  1492. ata_port_freeze(ap);
  1493. }
  1494. }
  1495. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  1496. {
  1497. struct ata_host *host = dev_instance;
  1498. struct ahci_host_priv *hpriv;
  1499. unsigned int i, handled = 0;
  1500. void __iomem *mmio;
  1501. u32 irq_stat, irq_ack = 0;
  1502. VPRINTK("ENTER\n");
  1503. hpriv = host->private_data;
  1504. mmio = host->iomap[AHCI_PCI_BAR];
  1505. /* sigh. 0xffffffff is a valid return from h/w */
  1506. irq_stat = readl(mmio + HOST_IRQ_STAT);
  1507. irq_stat &= hpriv->port_map;
  1508. if (!irq_stat)
  1509. return IRQ_NONE;
  1510. spin_lock(&host->lock);
  1511. for (i = 0; i < host->n_ports; i++) {
  1512. struct ata_port *ap;
  1513. if (!(irq_stat & (1 << i)))
  1514. continue;
  1515. ap = host->ports[i];
  1516. if (ap) {
  1517. ahci_port_intr(ap);
  1518. VPRINTK("port %u\n", i);
  1519. } else {
  1520. VPRINTK("port %u (no irq)\n", i);
  1521. if (ata_ratelimit())
  1522. dev_printk(KERN_WARNING, host->dev,
  1523. "interrupt on disabled port %u\n", i);
  1524. }
  1525. irq_ack |= (1 << i);
  1526. }
  1527. if (irq_ack) {
  1528. writel(irq_ack, mmio + HOST_IRQ_STAT);
  1529. handled = 1;
  1530. }
  1531. spin_unlock(&host->lock);
  1532. VPRINTK("EXIT\n");
  1533. return IRQ_RETVAL(handled);
  1534. }
  1535. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1536. {
  1537. struct ata_port *ap = qc->ap;
  1538. void __iomem *port_mmio = ahci_port_base(ap);
  1539. struct ahci_port_priv *pp = ap->private_data;
  1540. /* Keep track of the currently active link. It will be used
  1541. * in completion path to determine whether NCQ phase is in
  1542. * progress.
  1543. */
  1544. pp->active_link = qc->dev->link;
  1545. if (qc->tf.protocol == ATA_PROT_NCQ)
  1546. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  1547. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  1548. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1549. return 0;
  1550. }
  1551. static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
  1552. {
  1553. struct ahci_port_priv *pp = qc->ap->private_data;
  1554. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1555. ata_tf_from_fis(d2h_fis, &qc->result_tf);
  1556. return true;
  1557. }
  1558. static void ahci_freeze(struct ata_port *ap)
  1559. {
  1560. void __iomem *port_mmio = ahci_port_base(ap);
  1561. /* turn IRQ off */
  1562. writel(0, port_mmio + PORT_IRQ_MASK);
  1563. }
  1564. static void ahci_thaw(struct ata_port *ap)
  1565. {
  1566. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1567. void __iomem *port_mmio = ahci_port_base(ap);
  1568. u32 tmp;
  1569. struct ahci_port_priv *pp = ap->private_data;
  1570. /* clear IRQ */
  1571. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1572. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1573. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1574. /* turn IRQ back on */
  1575. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1576. }
  1577. static void ahci_error_handler(struct ata_port *ap)
  1578. {
  1579. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1580. /* restart engine */
  1581. ahci_stop_engine(ap);
  1582. ahci_start_engine(ap);
  1583. }
  1584. sata_pmp_error_handler(ap);
  1585. }
  1586. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1587. {
  1588. struct ata_port *ap = qc->ap;
  1589. /* make DMA engine forget about the failed command */
  1590. if (qc->flags & ATA_QCFLAG_FAILED)
  1591. ahci_kick_engine(ap, 1);
  1592. }
  1593. static void ahci_pmp_attach(struct ata_port *ap)
  1594. {
  1595. void __iomem *port_mmio = ahci_port_base(ap);
  1596. struct ahci_port_priv *pp = ap->private_data;
  1597. u32 cmd;
  1598. cmd = readl(port_mmio + PORT_CMD);
  1599. cmd |= PORT_CMD_PMP;
  1600. writel(cmd, port_mmio + PORT_CMD);
  1601. pp->intr_mask |= PORT_IRQ_BAD_PMP;
  1602. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1603. }
  1604. static void ahci_pmp_detach(struct ata_port *ap)
  1605. {
  1606. void __iomem *port_mmio = ahci_port_base(ap);
  1607. struct ahci_port_priv *pp = ap->private_data;
  1608. u32 cmd;
  1609. cmd = readl(port_mmio + PORT_CMD);
  1610. cmd &= ~PORT_CMD_PMP;
  1611. writel(cmd, port_mmio + PORT_CMD);
  1612. pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
  1613. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1614. }
  1615. static int ahci_port_resume(struct ata_port *ap)
  1616. {
  1617. ahci_power_up(ap);
  1618. ahci_start_port(ap);
  1619. if (sata_pmp_attached(ap))
  1620. ahci_pmp_attach(ap);
  1621. else
  1622. ahci_pmp_detach(ap);
  1623. return 0;
  1624. }
  1625. #ifdef CONFIG_PM
  1626. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1627. {
  1628. const char *emsg = NULL;
  1629. int rc;
  1630. rc = ahci_deinit_port(ap, &emsg);
  1631. if (rc == 0)
  1632. ahci_power_down(ap);
  1633. else {
  1634. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  1635. ahci_start_port(ap);
  1636. }
  1637. return rc;
  1638. }
  1639. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1640. {
  1641. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1642. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1643. u32 ctl;
  1644. if (mesg.event & PM_EVENT_SLEEP) {
  1645. /* AHCI spec rev1.1 section 8.3.3:
  1646. * Software must disable interrupts prior to requesting a
  1647. * transition of the HBA to D3 state.
  1648. */
  1649. ctl = readl(mmio + HOST_CTL);
  1650. ctl &= ~HOST_IRQ_EN;
  1651. writel(ctl, mmio + HOST_CTL);
  1652. readl(mmio + HOST_CTL); /* flush */
  1653. }
  1654. return ata_pci_device_suspend(pdev, mesg);
  1655. }
  1656. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1657. {
  1658. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1659. int rc;
  1660. rc = ata_pci_device_do_resume(pdev);
  1661. if (rc)
  1662. return rc;
  1663. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1664. rc = ahci_reset_controller(host);
  1665. if (rc)
  1666. return rc;
  1667. ahci_init_controller(host);
  1668. }
  1669. ata_host_resume(host);
  1670. return 0;
  1671. }
  1672. #endif
  1673. static int ahci_port_start(struct ata_port *ap)
  1674. {
  1675. struct device *dev = ap->host->dev;
  1676. struct ahci_port_priv *pp;
  1677. void *mem;
  1678. dma_addr_t mem_dma;
  1679. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1680. if (!pp)
  1681. return -ENOMEM;
  1682. mem = dmam_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma,
  1683. GFP_KERNEL);
  1684. if (!mem)
  1685. return -ENOMEM;
  1686. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1687. /*
  1688. * First item in chunk of DMA memory: 32-slot command table,
  1689. * 32 bytes each in size
  1690. */
  1691. pp->cmd_slot = mem;
  1692. pp->cmd_slot_dma = mem_dma;
  1693. mem += AHCI_CMD_SLOT_SZ;
  1694. mem_dma += AHCI_CMD_SLOT_SZ;
  1695. /*
  1696. * Second item: Received-FIS area
  1697. */
  1698. pp->rx_fis = mem;
  1699. pp->rx_fis_dma = mem_dma;
  1700. mem += AHCI_RX_FIS_SZ;
  1701. mem_dma += AHCI_RX_FIS_SZ;
  1702. /*
  1703. * Third item: data area for storing a single command
  1704. * and its scatter-gather table
  1705. */
  1706. pp->cmd_tbl = mem;
  1707. pp->cmd_tbl_dma = mem_dma;
  1708. /*
  1709. * Save off initial list of interrupts to be enabled.
  1710. * This could be changed later
  1711. */
  1712. pp->intr_mask = DEF_PORT_IRQ;
  1713. ap->private_data = pp;
  1714. /* engage engines, captain */
  1715. return ahci_port_resume(ap);
  1716. }
  1717. static void ahci_port_stop(struct ata_port *ap)
  1718. {
  1719. const char *emsg = NULL;
  1720. int rc;
  1721. /* de-initialize port */
  1722. rc = ahci_deinit_port(ap, &emsg);
  1723. if (rc)
  1724. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1725. }
  1726. static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
  1727. {
  1728. int rc;
  1729. if (using_dac &&
  1730. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1731. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1732. if (rc) {
  1733. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1734. if (rc) {
  1735. dev_printk(KERN_ERR, &pdev->dev,
  1736. "64-bit DMA enable failed\n");
  1737. return rc;
  1738. }
  1739. }
  1740. } else {
  1741. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1742. if (rc) {
  1743. dev_printk(KERN_ERR, &pdev->dev,
  1744. "32-bit DMA enable failed\n");
  1745. return rc;
  1746. }
  1747. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1748. if (rc) {
  1749. dev_printk(KERN_ERR, &pdev->dev,
  1750. "32-bit consistent DMA enable failed\n");
  1751. return rc;
  1752. }
  1753. }
  1754. return 0;
  1755. }
  1756. static void ahci_print_info(struct ata_host *host)
  1757. {
  1758. struct ahci_host_priv *hpriv = host->private_data;
  1759. struct pci_dev *pdev = to_pci_dev(host->dev);
  1760. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1761. u32 vers, cap, impl, speed;
  1762. const char *speed_s;
  1763. u16 cc;
  1764. const char *scc_s;
  1765. vers = readl(mmio + HOST_VERSION);
  1766. cap = hpriv->cap;
  1767. impl = hpriv->port_map;
  1768. speed = (cap >> 20) & 0xf;
  1769. if (speed == 1)
  1770. speed_s = "1.5";
  1771. else if (speed == 2)
  1772. speed_s = "3";
  1773. else
  1774. speed_s = "?";
  1775. pci_read_config_word(pdev, 0x0a, &cc);
  1776. if (cc == PCI_CLASS_STORAGE_IDE)
  1777. scc_s = "IDE";
  1778. else if (cc == PCI_CLASS_STORAGE_SATA)
  1779. scc_s = "SATA";
  1780. else if (cc == PCI_CLASS_STORAGE_RAID)
  1781. scc_s = "RAID";
  1782. else
  1783. scc_s = "unknown";
  1784. dev_printk(KERN_INFO, &pdev->dev,
  1785. "AHCI %02x%02x.%02x%02x "
  1786. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1787. ,
  1788. (vers >> 24) & 0xff,
  1789. (vers >> 16) & 0xff,
  1790. (vers >> 8) & 0xff,
  1791. vers & 0xff,
  1792. ((cap >> 8) & 0x1f) + 1,
  1793. (cap & 0x1f) + 1,
  1794. speed_s,
  1795. impl,
  1796. scc_s);
  1797. dev_printk(KERN_INFO, &pdev->dev,
  1798. "flags: "
  1799. "%s%s%s%s%s%s%s"
  1800. "%s%s%s%s%s%s%s\n"
  1801. ,
  1802. cap & (1 << 31) ? "64bit " : "",
  1803. cap & (1 << 30) ? "ncq " : "",
  1804. cap & (1 << 29) ? "sntf " : "",
  1805. cap & (1 << 28) ? "ilck " : "",
  1806. cap & (1 << 27) ? "stag " : "",
  1807. cap & (1 << 26) ? "pm " : "",
  1808. cap & (1 << 25) ? "led " : "",
  1809. cap & (1 << 24) ? "clo " : "",
  1810. cap & (1 << 19) ? "nz " : "",
  1811. cap & (1 << 18) ? "only " : "",
  1812. cap & (1 << 17) ? "pmp " : "",
  1813. cap & (1 << 15) ? "pio " : "",
  1814. cap & (1 << 14) ? "slum " : "",
  1815. cap & (1 << 13) ? "part " : ""
  1816. );
  1817. }
  1818. /* On ASUS P5W DH Deluxe, the second port of PCI device 00:1f.2 is
  1819. * hardwired to on-board SIMG 4726. The chipset is ICH8 and doesn't
  1820. * support PMP and the 4726 either directly exports the device
  1821. * attached to the first downstream port or acts as a hardware storage
  1822. * controller and emulate a single ATA device (can be RAID 0/1 or some
  1823. * other configuration).
  1824. *
  1825. * When there's no device attached to the first downstream port of the
  1826. * 4726, "Config Disk" appears, which is a pseudo ATA device to
  1827. * configure the 4726. However, ATA emulation of the device is very
  1828. * lame. It doesn't send signature D2H Reg FIS after the initial
  1829. * hardreset, pukes on SRST w/ PMP==0 and has bunch of other issues.
  1830. *
  1831. * The following function works around the problem by always using
  1832. * hardreset on the port and not depending on receiving signature FIS
  1833. * afterward. If signature FIS isn't received soon, ATA class is
  1834. * assumed without follow-up softreset.
  1835. */
  1836. static void ahci_p5wdh_workaround(struct ata_host *host)
  1837. {
  1838. static struct dmi_system_id sysids[] = {
  1839. {
  1840. .ident = "P5W DH Deluxe",
  1841. .matches = {
  1842. DMI_MATCH(DMI_SYS_VENDOR,
  1843. "ASUSTEK COMPUTER INC"),
  1844. DMI_MATCH(DMI_PRODUCT_NAME, "P5W DH Deluxe"),
  1845. },
  1846. },
  1847. { }
  1848. };
  1849. struct pci_dev *pdev = to_pci_dev(host->dev);
  1850. if (pdev->bus->number == 0 && pdev->devfn == PCI_DEVFN(0x1f, 2) &&
  1851. dmi_check_system(sysids)) {
  1852. struct ata_port *ap = host->ports[1];
  1853. dev_printk(KERN_INFO, &pdev->dev, "enabling ASUS P5W DH "
  1854. "Deluxe on-board SIMG4726 workaround\n");
  1855. ap->ops = &ahci_p5wdh_ops;
  1856. ap->link.flags |= ATA_LFLAG_NO_SRST | ATA_LFLAG_ASSUME_ATA;
  1857. }
  1858. }
  1859. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1860. {
  1861. static int printed_version;
  1862. unsigned int board_id = ent->driver_data;
  1863. struct ata_port_info pi = ahci_port_info[board_id];
  1864. const struct ata_port_info *ppi[] = { &pi, NULL };
  1865. struct device *dev = &pdev->dev;
  1866. struct ahci_host_priv *hpriv;
  1867. struct ata_host *host;
  1868. int n_ports, i, rc;
  1869. VPRINTK("ENTER\n");
  1870. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1871. if (!printed_version++)
  1872. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1873. /* acquire resources */
  1874. rc = pcim_enable_device(pdev);
  1875. if (rc)
  1876. return rc;
  1877. /* AHCI controllers often implement SFF compatible interface.
  1878. * Grab all PCI BARs just in case.
  1879. */
  1880. rc = pcim_iomap_regions_request_all(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
  1881. if (rc == -EBUSY)
  1882. pcim_pin_device(pdev);
  1883. if (rc)
  1884. return rc;
  1885. if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
  1886. (pdev->device == 0x2652 || pdev->device == 0x2653)) {
  1887. u8 map;
  1888. /* ICH6s share the same PCI ID for both piix and ahci
  1889. * modes. Enabling ahci mode while MAP indicates
  1890. * combined mode is a bad idea. Yield to ata_piix.
  1891. */
  1892. pci_read_config_byte(pdev, ICH_MAP, &map);
  1893. if (map & 0x3) {
  1894. dev_printk(KERN_INFO, &pdev->dev, "controller is in "
  1895. "combined mode, can't enable AHCI mode\n");
  1896. return -ENODEV;
  1897. }
  1898. }
  1899. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  1900. if (!hpriv)
  1901. return -ENOMEM;
  1902. hpriv->flags |= (unsigned long)pi.private_data;
  1903. /* MCP65 revision A1 and A2 can't do MSI */
  1904. if (board_id == board_ahci_mcp65 &&
  1905. (pdev->revision == 0xa1 || pdev->revision == 0xa2))
  1906. hpriv->flags |= AHCI_HFLAG_NO_MSI;
  1907. if ((hpriv->flags & AHCI_HFLAG_NO_MSI) || pci_enable_msi(pdev))
  1908. pci_intx(pdev, 1);
  1909. /* save initial config */
  1910. ahci_save_initial_config(pdev, hpriv);
  1911. /* prepare host */
  1912. if (hpriv->cap & HOST_CAP_NCQ)
  1913. pi.flags |= ATA_FLAG_NCQ;
  1914. if (hpriv->cap & HOST_CAP_PMP)
  1915. pi.flags |= ATA_FLAG_PMP;
  1916. /* CAP.NP sometimes indicate the index of the last enabled
  1917. * port, at other times, that of the last possible port, so
  1918. * determining the maximum port number requires looking at
  1919. * both CAP.NP and port_map.
  1920. */
  1921. n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
  1922. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  1923. if (!host)
  1924. return -ENOMEM;
  1925. host->iomap = pcim_iomap_table(pdev);
  1926. host->private_data = hpriv;
  1927. for (i = 0; i < host->n_ports; i++) {
  1928. struct ata_port *ap = host->ports[i];
  1929. ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
  1930. ata_port_pbar_desc(ap, AHCI_PCI_BAR,
  1931. 0x100 + ap->port_no * 0x80, "port");
  1932. /* set initial link pm policy */
  1933. ap->pm_policy = NOT_AVAILABLE;
  1934. /* disabled/not-implemented port */
  1935. if (!(hpriv->port_map & (1 << i)))
  1936. ap->ops = &ata_dummy_port_ops;
  1937. }
  1938. /* apply workaround for ASUS P5W DH Deluxe mainboard */
  1939. ahci_p5wdh_workaround(host);
  1940. /* initialize adapter */
  1941. rc = ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
  1942. if (rc)
  1943. return rc;
  1944. rc = ahci_reset_controller(host);
  1945. if (rc)
  1946. return rc;
  1947. ahci_init_controller(host);
  1948. ahci_print_info(host);
  1949. pci_set_master(pdev);
  1950. return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
  1951. &ahci_sht);
  1952. }
  1953. static int __init ahci_init(void)
  1954. {
  1955. return pci_register_driver(&ahci_pci_driver);
  1956. }
  1957. static void __exit ahci_exit(void)
  1958. {
  1959. pci_unregister_driver(&ahci_pci_driver);
  1960. }
  1961. MODULE_AUTHOR("Jeff Garzik");
  1962. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1963. MODULE_LICENSE("GPL");
  1964. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1965. MODULE_VERSION(DRV_VERSION);
  1966. module_init(ahci_init);
  1967. module_exit(ahci_exit);