ahci.c 61 KB

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