ahci.c 47 KB

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