ahci.c 63 KB

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