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