ahci.c 65 KB

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