ahci.c 51 KB

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