ahci.c 47 KB

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