ahci.c 46 KB

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