ahci.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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 "1.3"
  50. enum {
  51. AHCI_PCI_BAR = 5,
  52. AHCI_MAX_SG = 168, /* hardware max is 64K */
  53. AHCI_DMA_BOUNDARY = 0xffffffff,
  54. AHCI_USE_CLUSTERING = 0,
  55. AHCI_MAX_CMDS = 32,
  56. AHCI_CMD_SZ = 32,
  57. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  58. AHCI_RX_FIS_SZ = 256,
  59. AHCI_CMD_TBL_CDB = 0x40,
  60. AHCI_CMD_TBL_HDR_SZ = 0x80,
  61. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  62. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  63. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  64. AHCI_RX_FIS_SZ,
  65. AHCI_IRQ_ON_SG = (1 << 31),
  66. AHCI_CMD_ATAPI = (1 << 5),
  67. AHCI_CMD_WRITE = (1 << 6),
  68. AHCI_CMD_PREFETCH = (1 << 7),
  69. AHCI_CMD_RESET = (1 << 8),
  70. AHCI_CMD_CLR_BUSY = (1 << 10),
  71. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  72. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  73. board_ahci = 0,
  74. board_ahci_vt8251 = 1,
  75. /* global controller registers */
  76. HOST_CAP = 0x00, /* host capabilities */
  77. HOST_CTL = 0x04, /* global host control */
  78. HOST_IRQ_STAT = 0x08, /* interrupt status */
  79. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  80. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  81. /* HOST_CTL bits */
  82. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  83. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  84. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  85. /* HOST_CAP bits */
  86. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  87. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  88. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  89. /* registers for each SATA port */
  90. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  91. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  92. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  93. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  94. PORT_IRQ_STAT = 0x10, /* interrupt status */
  95. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  96. PORT_CMD = 0x18, /* port command */
  97. PORT_TFDATA = 0x20, /* taskfile data */
  98. PORT_SIG = 0x24, /* device TF signature */
  99. PORT_CMD_ISSUE = 0x38, /* command issue */
  100. PORT_SCR = 0x28, /* SATA phy register block */
  101. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  102. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  103. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  104. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  105. /* PORT_IRQ_{STAT,MASK} bits */
  106. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  107. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  108. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  109. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  110. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  111. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  112. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  113. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  114. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  115. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  116. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  117. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  118. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  119. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  120. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  121. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  122. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  123. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  124. PORT_IRQ_IF_ERR |
  125. PORT_IRQ_CONNECT |
  126. PORT_IRQ_UNK_FIS,
  127. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  128. PORT_IRQ_TF_ERR |
  129. PORT_IRQ_HBUS_DATA_ERR,
  130. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  131. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  132. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  133. /* PORT_CMD bits */
  134. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  135. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  136. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  137. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  138. PORT_CMD_CLO = (1 << 3), /* Command list override */
  139. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  140. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  141. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  142. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  143. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  144. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  145. /* hpriv->flags bits */
  146. AHCI_FLAG_MSI = (1 << 0),
  147. /* ap->flags bits */
  148. AHCI_FLAG_RESET_NEEDS_CLO = (1 << 24),
  149. };
  150. struct ahci_cmd_hdr {
  151. u32 opts;
  152. u32 status;
  153. u32 tbl_addr;
  154. u32 tbl_addr_hi;
  155. u32 reserved[4];
  156. };
  157. struct ahci_sg {
  158. u32 addr;
  159. u32 addr_hi;
  160. u32 reserved;
  161. u32 flags_size;
  162. };
  163. struct ahci_host_priv {
  164. unsigned long flags;
  165. u32 cap; /* cache of HOST_CAP register */
  166. u32 port_map; /* cache of HOST_PORTS_IMPL reg */
  167. };
  168. struct ahci_port_priv {
  169. struct ahci_cmd_hdr *cmd_slot;
  170. dma_addr_t cmd_slot_dma;
  171. void *cmd_tbl;
  172. dma_addr_t cmd_tbl_dma;
  173. void *rx_fis;
  174. dma_addr_t rx_fis_dma;
  175. };
  176. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
  177. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  178. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  179. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  180. static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
  181. static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes);
  182. static void ahci_irq_clear(struct ata_port *ap);
  183. static int ahci_port_start(struct ata_port *ap);
  184. static void ahci_port_stop(struct ata_port *ap);
  185. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  186. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  187. static u8 ahci_check_status(struct ata_port *ap);
  188. static void ahci_freeze(struct ata_port *ap);
  189. static void ahci_thaw(struct ata_port *ap);
  190. static void ahci_error_handler(struct ata_port *ap);
  191. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  192. static void ahci_remove_one (struct pci_dev *pdev);
  193. static struct scsi_host_template ahci_sht = {
  194. .module = THIS_MODULE,
  195. .name = DRV_NAME,
  196. .ioctl = ata_scsi_ioctl,
  197. .queuecommand = ata_scsi_queuecmd,
  198. .change_queue_depth = ata_scsi_change_queue_depth,
  199. .can_queue = AHCI_MAX_CMDS - 1,
  200. .this_id = ATA_SHT_THIS_ID,
  201. .sg_tablesize = AHCI_MAX_SG,
  202. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  203. .emulated = ATA_SHT_EMULATED,
  204. .use_clustering = AHCI_USE_CLUSTERING,
  205. .proc_name = DRV_NAME,
  206. .dma_boundary = AHCI_DMA_BOUNDARY,
  207. .slave_configure = ata_scsi_slave_config,
  208. .slave_destroy = ata_scsi_slave_destroy,
  209. .bios_param = ata_std_bios_param,
  210. };
  211. static const struct ata_port_operations ahci_ops = {
  212. .port_disable = ata_port_disable,
  213. .check_status = ahci_check_status,
  214. .check_altstatus = ahci_check_status,
  215. .dev_select = ata_noop_dev_select,
  216. .tf_read = ahci_tf_read,
  217. .probe_reset = ahci_probe_reset,
  218. .qc_prep = ahci_qc_prep,
  219. .qc_issue = ahci_qc_issue,
  220. .irq_handler = ahci_interrupt,
  221. .irq_clear = ahci_irq_clear,
  222. .scr_read = ahci_scr_read,
  223. .scr_write = ahci_scr_write,
  224. .freeze = ahci_freeze,
  225. .thaw = ahci_thaw,
  226. .error_handler = ahci_error_handler,
  227. .post_internal_cmd = ahci_post_internal_cmd,
  228. .port_start = ahci_port_start,
  229. .port_stop = ahci_port_stop,
  230. };
  231. static const struct ata_port_info ahci_port_info[] = {
  232. /* board_ahci */
  233. {
  234. .sht = &ahci_sht,
  235. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  236. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
  237. .pio_mask = 0x1f, /* pio0-4 */
  238. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  239. .port_ops = &ahci_ops,
  240. },
  241. /* board_ahci_vt8251 */
  242. {
  243. .sht = &ahci_sht,
  244. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  245. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  246. AHCI_FLAG_RESET_NEEDS_CLO,
  247. .pio_mask = 0x1f, /* pio0-4 */
  248. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  249. .port_ops = &ahci_ops,
  250. },
  251. };
  252. static const struct pci_device_id ahci_pci_tbl[] = {
  253. { PCI_VENDOR_ID_INTEL, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  254. board_ahci }, /* ICH6 */
  255. { PCI_VENDOR_ID_INTEL, 0x2653, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  256. board_ahci }, /* ICH6M */
  257. { PCI_VENDOR_ID_INTEL, 0x27c1, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  258. board_ahci }, /* ICH7 */
  259. { PCI_VENDOR_ID_INTEL, 0x27c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  260. board_ahci }, /* ICH7M */
  261. { PCI_VENDOR_ID_INTEL, 0x27c3, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  262. board_ahci }, /* ICH7R */
  263. { PCI_VENDOR_ID_AL, 0x5288, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  264. board_ahci }, /* ULi M5288 */
  265. { PCI_VENDOR_ID_INTEL, 0x2681, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  266. board_ahci }, /* ESB2 */
  267. { PCI_VENDOR_ID_INTEL, 0x2682, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  268. board_ahci }, /* ESB2 */
  269. { PCI_VENDOR_ID_INTEL, 0x2683, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  270. board_ahci }, /* ESB2 */
  271. { PCI_VENDOR_ID_INTEL, 0x27c6, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  272. board_ahci }, /* ICH7-M DH */
  273. { PCI_VENDOR_ID_INTEL, 0x2821, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  274. board_ahci }, /* ICH8 */
  275. { PCI_VENDOR_ID_INTEL, 0x2822, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  276. board_ahci }, /* ICH8 */
  277. { PCI_VENDOR_ID_INTEL, 0x2824, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  278. board_ahci }, /* ICH8 */
  279. { PCI_VENDOR_ID_INTEL, 0x2829, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  280. board_ahci }, /* ICH8M */
  281. { PCI_VENDOR_ID_INTEL, 0x282a, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  282. board_ahci }, /* ICH8M */
  283. { 0x197b, 0x2360, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  284. board_ahci }, /* JMicron JMB360 */
  285. { 0x197b, 0x2363, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  286. board_ahci }, /* JMicron JMB363 */
  287. { PCI_VENDOR_ID_ATI, 0x4380, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  288. board_ahci }, /* ATI SB600 non-raid */
  289. { PCI_VENDOR_ID_ATI, 0x4381, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  290. board_ahci }, /* ATI SB600 raid */
  291. { PCI_VENDOR_ID_VIA, 0x3349, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  292. board_ahci_vt8251 }, /* VIA VT8251 */
  293. { } /* terminate list */
  294. };
  295. static struct pci_driver ahci_pci_driver = {
  296. .name = DRV_NAME,
  297. .id_table = ahci_pci_tbl,
  298. .probe = ahci_init_one,
  299. .remove = ahci_remove_one,
  300. };
  301. static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
  302. {
  303. return base + 0x100 + (port * 0x80);
  304. }
  305. static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int port)
  306. {
  307. return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
  308. }
  309. static int ahci_port_start(struct ata_port *ap)
  310. {
  311. struct device *dev = ap->host_set->dev;
  312. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  313. struct ahci_port_priv *pp;
  314. void __iomem *mmio = ap->host_set->mmio_base;
  315. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  316. void *mem;
  317. dma_addr_t mem_dma;
  318. int rc;
  319. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  320. if (!pp)
  321. return -ENOMEM;
  322. memset(pp, 0, sizeof(*pp));
  323. rc = ata_pad_alloc(ap, dev);
  324. if (rc) {
  325. kfree(pp);
  326. return rc;
  327. }
  328. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  329. if (!mem) {
  330. ata_pad_free(ap, dev);
  331. kfree(pp);
  332. return -ENOMEM;
  333. }
  334. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  335. /*
  336. * First item in chunk of DMA memory: 32-slot command table,
  337. * 32 bytes each in size
  338. */
  339. pp->cmd_slot = mem;
  340. pp->cmd_slot_dma = mem_dma;
  341. mem += AHCI_CMD_SLOT_SZ;
  342. mem_dma += AHCI_CMD_SLOT_SZ;
  343. /*
  344. * Second item: Received-FIS area
  345. */
  346. pp->rx_fis = mem;
  347. pp->rx_fis_dma = mem_dma;
  348. mem += AHCI_RX_FIS_SZ;
  349. mem_dma += AHCI_RX_FIS_SZ;
  350. /*
  351. * Third item: data area for storing a single command
  352. * and its scatter-gather table
  353. */
  354. pp->cmd_tbl = mem;
  355. pp->cmd_tbl_dma = mem_dma;
  356. ap->private_data = pp;
  357. if (hpriv->cap & HOST_CAP_64)
  358. writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  359. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  360. readl(port_mmio + PORT_LST_ADDR); /* flush */
  361. if (hpriv->cap & HOST_CAP_64)
  362. writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  363. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  364. readl(port_mmio + PORT_FIS_ADDR); /* flush */
  365. writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  366. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  367. PORT_CMD_START, port_mmio + PORT_CMD);
  368. readl(port_mmio + PORT_CMD); /* flush */
  369. return 0;
  370. }
  371. static void ahci_port_stop(struct ata_port *ap)
  372. {
  373. struct device *dev = ap->host_set->dev;
  374. struct ahci_port_priv *pp = ap->private_data;
  375. void __iomem *mmio = ap->host_set->mmio_base;
  376. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  377. u32 tmp;
  378. tmp = readl(port_mmio + PORT_CMD);
  379. tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX);
  380. writel(tmp, port_mmio + PORT_CMD);
  381. readl(port_mmio + PORT_CMD); /* flush */
  382. /* spec says 500 msecs for each PORT_CMD_{START,FIS_RX} bit, so
  383. * this is slightly incorrect.
  384. */
  385. msleep(500);
  386. ap->private_data = NULL;
  387. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  388. pp->cmd_slot, pp->cmd_slot_dma);
  389. ata_pad_free(ap, dev);
  390. kfree(pp);
  391. }
  392. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  393. {
  394. unsigned int sc_reg;
  395. switch (sc_reg_in) {
  396. case SCR_STATUS: sc_reg = 0; break;
  397. case SCR_CONTROL: sc_reg = 1; break;
  398. case SCR_ERROR: sc_reg = 2; break;
  399. case SCR_ACTIVE: sc_reg = 3; break;
  400. default:
  401. return 0xffffffffU;
  402. }
  403. return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  404. }
  405. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  406. u32 val)
  407. {
  408. unsigned int sc_reg;
  409. switch (sc_reg_in) {
  410. case SCR_STATUS: sc_reg = 0; break;
  411. case SCR_CONTROL: sc_reg = 1; break;
  412. case SCR_ERROR: sc_reg = 2; break;
  413. case SCR_ACTIVE: sc_reg = 3; break;
  414. default:
  415. return;
  416. }
  417. writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  418. }
  419. static int ahci_stop_engine(struct ata_port *ap)
  420. {
  421. void __iomem *mmio = ap->host_set->mmio_base;
  422. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  423. int work;
  424. u32 tmp;
  425. tmp = readl(port_mmio + PORT_CMD);
  426. tmp &= ~PORT_CMD_START;
  427. writel(tmp, port_mmio + PORT_CMD);
  428. /* wait for engine to stop. TODO: this could be
  429. * as long as 500 msec
  430. */
  431. work = 1000;
  432. while (work-- > 0) {
  433. tmp = readl(port_mmio + PORT_CMD);
  434. if ((tmp & PORT_CMD_LIST_ON) == 0)
  435. return 0;
  436. udelay(10);
  437. }
  438. return -EIO;
  439. }
  440. static void ahci_start_engine(struct ata_port *ap)
  441. {
  442. void __iomem *mmio = ap->host_set->mmio_base;
  443. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  444. u32 tmp;
  445. tmp = readl(port_mmio + PORT_CMD);
  446. tmp |= PORT_CMD_START;
  447. writel(tmp, port_mmio + PORT_CMD);
  448. readl(port_mmio + PORT_CMD); /* flush */
  449. }
  450. static unsigned int ahci_dev_classify(struct ata_port *ap)
  451. {
  452. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  453. struct ata_taskfile tf;
  454. u32 tmp;
  455. tmp = readl(port_mmio + PORT_SIG);
  456. tf.lbah = (tmp >> 24) & 0xff;
  457. tf.lbam = (tmp >> 16) & 0xff;
  458. tf.lbal = (tmp >> 8) & 0xff;
  459. tf.nsect = (tmp) & 0xff;
  460. return ata_dev_classify(&tf);
  461. }
  462. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  463. u32 opts)
  464. {
  465. dma_addr_t cmd_tbl_dma;
  466. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  467. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  468. pp->cmd_slot[tag].status = 0;
  469. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  470. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  471. }
  472. static int ahci_clo(struct ata_port *ap)
  473. {
  474. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  475. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  476. u32 tmp;
  477. if (!(hpriv->cap & HOST_CAP_CLO))
  478. return -EOPNOTSUPP;
  479. tmp = readl(port_mmio + PORT_CMD);
  480. tmp |= PORT_CMD_CLO;
  481. writel(tmp, port_mmio + PORT_CMD);
  482. tmp = ata_wait_register(port_mmio + PORT_CMD,
  483. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  484. if (tmp & PORT_CMD_CLO)
  485. return -EIO;
  486. return 0;
  487. }
  488. static int ahci_softreset(struct ata_port *ap, unsigned int *class)
  489. {
  490. struct ahci_port_priv *pp = ap->private_data;
  491. void __iomem *mmio = ap->host_set->mmio_base;
  492. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  493. const u32 cmd_fis_len = 5; /* five dwords */
  494. const char *reason = NULL;
  495. struct ata_taskfile tf;
  496. u32 tmp;
  497. u8 *fis;
  498. int rc;
  499. DPRINTK("ENTER\n");
  500. if (ata_port_offline(ap)) {
  501. DPRINTK("PHY reports no device\n");
  502. *class = ATA_DEV_NONE;
  503. return 0;
  504. }
  505. /* prepare for SRST (AHCI-1.1 10.4.1) */
  506. rc = ahci_stop_engine(ap);
  507. if (rc) {
  508. reason = "failed to stop engine";
  509. goto fail_restart;
  510. }
  511. /* check BUSY/DRQ, perform Command List Override if necessary */
  512. ahci_tf_read(ap, &tf);
  513. if (tf.command & (ATA_BUSY | ATA_DRQ)) {
  514. rc = ahci_clo(ap);
  515. if (rc == -EOPNOTSUPP) {
  516. reason = "port busy but CLO unavailable";
  517. goto fail_restart;
  518. } else if (rc) {
  519. reason = "port busy but CLO failed";
  520. goto fail_restart;
  521. }
  522. }
  523. /* restart engine */
  524. ahci_start_engine(ap);
  525. ata_tf_init(ap->device, &tf);
  526. fis = pp->cmd_tbl;
  527. /* issue the first D2H Register FIS */
  528. ahci_fill_cmd_slot(pp, 0,
  529. cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
  530. tf.ctl |= ATA_SRST;
  531. ata_tf_to_fis(&tf, fis, 0);
  532. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  533. writel(1, port_mmio + PORT_CMD_ISSUE);
  534. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1, 1, 500);
  535. if (tmp & 0x1) {
  536. rc = -EIO;
  537. reason = "1st FIS failed";
  538. goto fail;
  539. }
  540. /* spec says at least 5us, but be generous and sleep for 1ms */
  541. msleep(1);
  542. /* issue the second D2H Register FIS */
  543. ahci_fill_cmd_slot(pp, 0, cmd_fis_len);
  544. tf.ctl &= ~ATA_SRST;
  545. ata_tf_to_fis(&tf, fis, 0);
  546. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  547. writel(1, port_mmio + PORT_CMD_ISSUE);
  548. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  549. /* spec mandates ">= 2ms" before checking status.
  550. * We wait 150ms, because that was the magic delay used for
  551. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  552. * between when the ATA command register is written, and then
  553. * status is checked. Because waiting for "a while" before
  554. * checking status is fine, post SRST, we perform this magic
  555. * delay here as well.
  556. */
  557. msleep(150);
  558. *class = ATA_DEV_NONE;
  559. if (ata_port_online(ap)) {
  560. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  561. rc = -EIO;
  562. reason = "device not ready";
  563. goto fail;
  564. }
  565. *class = ahci_dev_classify(ap);
  566. }
  567. DPRINTK("EXIT, class=%u\n", *class);
  568. return 0;
  569. fail_restart:
  570. ahci_start_engine(ap);
  571. fail:
  572. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  573. return rc;
  574. }
  575. static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
  576. {
  577. int rc;
  578. DPRINTK("ENTER\n");
  579. ahci_stop_engine(ap);
  580. rc = sata_std_hardreset(ap, class);
  581. ahci_start_engine(ap);
  582. if (rc == 0 && ata_port_online(ap))
  583. *class = ahci_dev_classify(ap);
  584. if (*class == ATA_DEV_UNKNOWN)
  585. *class = ATA_DEV_NONE;
  586. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  587. return rc;
  588. }
  589. static void ahci_postreset(struct ata_port *ap, unsigned int *class)
  590. {
  591. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  592. u32 new_tmp, tmp;
  593. ata_std_postreset(ap, class);
  594. /* Make sure port's ATAPI bit is set appropriately */
  595. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  596. if (*class == ATA_DEV_ATAPI)
  597. new_tmp |= PORT_CMD_ATAPI;
  598. else
  599. new_tmp &= ~PORT_CMD_ATAPI;
  600. if (new_tmp != tmp) {
  601. writel(new_tmp, port_mmio + PORT_CMD);
  602. readl(port_mmio + PORT_CMD); /* flush */
  603. }
  604. }
  605. static int ahci_probe_reset(struct ata_port *ap, unsigned int *classes)
  606. {
  607. if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
  608. (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
  609. /* ATA_BUSY hasn't cleared, so send a CLO */
  610. ahci_clo(ap);
  611. }
  612. return ata_drive_probe_reset(ap, ata_std_probeinit,
  613. ahci_softreset, ahci_hardreset,
  614. ahci_postreset, classes);
  615. }
  616. static u8 ahci_check_status(struct ata_port *ap)
  617. {
  618. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  619. return readl(mmio + PORT_TFDATA) & 0xFF;
  620. }
  621. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  622. {
  623. struct ahci_port_priv *pp = ap->private_data;
  624. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  625. ata_tf_from_fis(d2h_fis, tf);
  626. }
  627. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  628. {
  629. struct scatterlist *sg;
  630. struct ahci_sg *ahci_sg;
  631. unsigned int n_sg = 0;
  632. VPRINTK("ENTER\n");
  633. /*
  634. * Next, the S/G list.
  635. */
  636. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  637. ata_for_each_sg(sg, qc) {
  638. dma_addr_t addr = sg_dma_address(sg);
  639. u32 sg_len = sg_dma_len(sg);
  640. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  641. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  642. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  643. ahci_sg++;
  644. n_sg++;
  645. }
  646. return n_sg;
  647. }
  648. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  649. {
  650. struct ata_port *ap = qc->ap;
  651. struct ahci_port_priv *pp = ap->private_data;
  652. int is_atapi = is_atapi_taskfile(&qc->tf);
  653. void *cmd_tbl;
  654. u32 opts;
  655. const u32 cmd_fis_len = 5; /* five dwords */
  656. unsigned int n_elem;
  657. /*
  658. * Fill in command table information. First, the header,
  659. * a SATA Register - Host to Device command FIS.
  660. */
  661. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  662. ata_tf_to_fis(&qc->tf, cmd_tbl, 0);
  663. if (is_atapi) {
  664. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  665. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  666. }
  667. n_elem = 0;
  668. if (qc->flags & ATA_QCFLAG_DMAMAP)
  669. n_elem = ahci_fill_sg(qc, cmd_tbl);
  670. /*
  671. * Fill in command slot information.
  672. */
  673. opts = cmd_fis_len | n_elem << 16;
  674. if (qc->tf.flags & ATA_TFLAG_WRITE)
  675. opts |= AHCI_CMD_WRITE;
  676. if (is_atapi)
  677. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  678. ahci_fill_cmd_slot(pp, qc->tag, opts);
  679. }
  680. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  681. {
  682. struct ahci_port_priv *pp = ap->private_data;
  683. struct ata_eh_info *ehi = &ap->eh_info;
  684. unsigned int err_mask = 0, action = 0;
  685. struct ata_queued_cmd *qc;
  686. u32 serror;
  687. ata_ehi_clear_desc(ehi);
  688. /* AHCI needs SError cleared; otherwise, it might lock up */
  689. serror = ahci_scr_read(ap, SCR_ERROR);
  690. ahci_scr_write(ap, SCR_ERROR, serror);
  691. /* analyze @irq_stat */
  692. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  693. if (irq_stat & PORT_IRQ_TF_ERR)
  694. err_mask |= AC_ERR_DEV;
  695. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  696. err_mask |= AC_ERR_HOST_BUS;
  697. action |= ATA_EH_SOFTRESET;
  698. }
  699. if (irq_stat & PORT_IRQ_IF_ERR) {
  700. err_mask |= AC_ERR_ATA_BUS;
  701. action |= ATA_EH_SOFTRESET;
  702. ata_ehi_push_desc(ehi, ", interface fatal error");
  703. }
  704. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  705. err_mask |= AC_ERR_ATA_BUS;
  706. action |= ATA_EH_SOFTRESET;
  707. ata_ehi_push_desc(ehi, ", %s", irq_stat & PORT_IRQ_CONNECT ?
  708. "connection status changed" : "PHY RDY changed");
  709. }
  710. if (irq_stat & PORT_IRQ_UNK_FIS) {
  711. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  712. err_mask |= AC_ERR_HSM;
  713. action |= ATA_EH_SOFTRESET;
  714. ata_ehi_push_desc(ehi, ", unknown FIS %08x %08x %08x %08x",
  715. unk[0], unk[1], unk[2], unk[3]);
  716. }
  717. /* okay, let's hand over to EH */
  718. ehi->serror |= serror;
  719. ehi->action |= action;
  720. qc = ata_qc_from_tag(ap, ap->active_tag);
  721. if (qc)
  722. qc->err_mask |= err_mask;
  723. else
  724. ehi->err_mask |= err_mask;
  725. if (irq_stat & PORT_IRQ_FREEZE)
  726. ata_port_freeze(ap);
  727. else
  728. ata_port_abort(ap);
  729. }
  730. static void ahci_host_intr(struct ata_port *ap)
  731. {
  732. void __iomem *mmio = ap->host_set->mmio_base;
  733. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  734. struct ata_eh_info *ehi = &ap->eh_info;
  735. u32 status, qc_active;
  736. int rc;
  737. status = readl(port_mmio + PORT_IRQ_STAT);
  738. writel(status, port_mmio + PORT_IRQ_STAT);
  739. if (unlikely(status & PORT_IRQ_ERROR)) {
  740. ahci_error_intr(ap, status);
  741. return;
  742. }
  743. if (ap->sactive)
  744. qc_active = readl(port_mmio + PORT_SCR_ACT);
  745. else
  746. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  747. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  748. if (rc > 0)
  749. return;
  750. if (rc < 0) {
  751. ehi->err_mask |= AC_ERR_HSM;
  752. ehi->action |= ATA_EH_SOFTRESET;
  753. ata_port_freeze(ap);
  754. return;
  755. }
  756. /* hmmm... a spurious interupt */
  757. /* some devices send D2H reg with I bit set during NCQ command phase */
  758. if (ap->sactive && status & PORT_IRQ_D2H_REG_FIS)
  759. return;
  760. /* ignore interim PIO setup fis interrupts */
  761. if (ata_tag_valid(ap->active_tag)) {
  762. struct ata_queued_cmd *qc =
  763. ata_qc_from_tag(ap, ap->active_tag);
  764. if (qc && qc->tf.protocol == ATA_PROT_PIO &&
  765. (status & PORT_IRQ_PIOS_FIS))
  766. return;
  767. }
  768. if (ata_ratelimit())
  769. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  770. "(irq_stat 0x%x active_tag %d sactive 0x%x)\n",
  771. status, ap->active_tag, ap->sactive);
  772. }
  773. static void ahci_irq_clear(struct ata_port *ap)
  774. {
  775. /* TODO */
  776. }
  777. static irqreturn_t ahci_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  778. {
  779. struct ata_host_set *host_set = dev_instance;
  780. struct ahci_host_priv *hpriv;
  781. unsigned int i, handled = 0;
  782. void __iomem *mmio;
  783. u32 irq_stat, irq_ack = 0;
  784. VPRINTK("ENTER\n");
  785. hpriv = host_set->private_data;
  786. mmio = host_set->mmio_base;
  787. /* sigh. 0xffffffff is a valid return from h/w */
  788. irq_stat = readl(mmio + HOST_IRQ_STAT);
  789. irq_stat &= hpriv->port_map;
  790. if (!irq_stat)
  791. return IRQ_NONE;
  792. spin_lock(&host_set->lock);
  793. for (i = 0; i < host_set->n_ports; i++) {
  794. struct ata_port *ap;
  795. if (!(irq_stat & (1 << i)))
  796. continue;
  797. ap = host_set->ports[i];
  798. if (ap) {
  799. ahci_host_intr(ap);
  800. VPRINTK("port %u\n", i);
  801. } else {
  802. VPRINTK("port %u (no irq)\n", i);
  803. if (ata_ratelimit())
  804. dev_printk(KERN_WARNING, host_set->dev,
  805. "interrupt on disabled port %u\n", i);
  806. }
  807. irq_ack |= (1 << i);
  808. }
  809. if (irq_ack) {
  810. writel(irq_ack, mmio + HOST_IRQ_STAT);
  811. handled = 1;
  812. }
  813. spin_unlock(&host_set->lock);
  814. VPRINTK("EXIT\n");
  815. return IRQ_RETVAL(handled);
  816. }
  817. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  818. {
  819. struct ata_port *ap = qc->ap;
  820. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  821. if (qc->tf.protocol == ATA_PROT_NCQ)
  822. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  823. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  824. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  825. return 0;
  826. }
  827. static void ahci_freeze(struct ata_port *ap)
  828. {
  829. void __iomem *mmio = ap->host_set->mmio_base;
  830. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  831. /* turn IRQ off */
  832. writel(0, port_mmio + PORT_IRQ_MASK);
  833. }
  834. static void ahci_thaw(struct ata_port *ap)
  835. {
  836. void __iomem *mmio = ap->host_set->mmio_base;
  837. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  838. u32 tmp;
  839. /* clear IRQ */
  840. tmp = readl(port_mmio + PORT_IRQ_STAT);
  841. writel(tmp, port_mmio + PORT_IRQ_STAT);
  842. writel(1 << ap->id, mmio + HOST_IRQ_STAT);
  843. /* turn IRQ back on */
  844. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  845. }
  846. static void ahci_error_handler(struct ata_port *ap)
  847. {
  848. if (!(ap->flags & ATA_FLAG_FROZEN)) {
  849. /* restart engine */
  850. ahci_stop_engine(ap);
  851. ahci_start_engine(ap);
  852. }
  853. /* perform recovery */
  854. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_hardreset,
  855. ahci_postreset);
  856. }
  857. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  858. {
  859. struct ata_port *ap = qc->ap;
  860. if (qc->flags & ATA_QCFLAG_FAILED)
  861. qc->err_mask |= AC_ERR_OTHER;
  862. if (qc->err_mask) {
  863. /* make DMA engine forget about the failed command */
  864. ahci_stop_engine(ap);
  865. ahci_start_engine(ap);
  866. }
  867. }
  868. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  869. unsigned int port_idx)
  870. {
  871. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  872. base = ahci_port_base_ul(base, port_idx);
  873. VPRINTK("base now==0x%lx\n", base);
  874. port->cmd_addr = base;
  875. port->scr_addr = base + PORT_SCR;
  876. VPRINTK("EXIT\n");
  877. }
  878. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  879. {
  880. struct ahci_host_priv *hpriv = probe_ent->private_data;
  881. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  882. void __iomem *mmio = probe_ent->mmio_base;
  883. u32 tmp, cap_save;
  884. unsigned int i, j, using_dac;
  885. int rc;
  886. void __iomem *port_mmio;
  887. cap_save = readl(mmio + HOST_CAP);
  888. cap_save &= ( (1<<28) | (1<<17) );
  889. cap_save |= (1 << 27);
  890. /* global controller reset */
  891. tmp = readl(mmio + HOST_CTL);
  892. if ((tmp & HOST_RESET) == 0) {
  893. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  894. readl(mmio + HOST_CTL); /* flush */
  895. }
  896. /* reset must complete within 1 second, or
  897. * the hardware should be considered fried.
  898. */
  899. ssleep(1);
  900. tmp = readl(mmio + HOST_CTL);
  901. if (tmp & HOST_RESET) {
  902. dev_printk(KERN_ERR, &pdev->dev,
  903. "controller reset failed (0x%x)\n", tmp);
  904. return -EIO;
  905. }
  906. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  907. (void) readl(mmio + HOST_CTL); /* flush */
  908. writel(cap_save, mmio + HOST_CAP);
  909. writel(0xf, mmio + HOST_PORTS_IMPL);
  910. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  911. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  912. u16 tmp16;
  913. pci_read_config_word(pdev, 0x92, &tmp16);
  914. tmp16 |= 0xf;
  915. pci_write_config_word(pdev, 0x92, tmp16);
  916. }
  917. hpriv->cap = readl(mmio + HOST_CAP);
  918. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  919. probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
  920. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  921. hpriv->cap, hpriv->port_map, probe_ent->n_ports);
  922. using_dac = hpriv->cap & HOST_CAP_64;
  923. if (using_dac &&
  924. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  925. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  926. if (rc) {
  927. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  928. if (rc) {
  929. dev_printk(KERN_ERR, &pdev->dev,
  930. "64-bit DMA enable failed\n");
  931. return rc;
  932. }
  933. }
  934. } else {
  935. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  936. if (rc) {
  937. dev_printk(KERN_ERR, &pdev->dev,
  938. "32-bit DMA enable failed\n");
  939. return rc;
  940. }
  941. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  942. if (rc) {
  943. dev_printk(KERN_ERR, &pdev->dev,
  944. "32-bit consistent DMA enable failed\n");
  945. return rc;
  946. }
  947. }
  948. for (i = 0; i < probe_ent->n_ports; i++) {
  949. #if 0 /* BIOSen initialize this incorrectly */
  950. if (!(hpriv->port_map & (1 << i)))
  951. continue;
  952. #endif
  953. port_mmio = ahci_port_base(mmio, i);
  954. VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
  955. ahci_setup_port(&probe_ent->port[i],
  956. (unsigned long) mmio, i);
  957. /* make sure port is not active */
  958. tmp = readl(port_mmio + PORT_CMD);
  959. VPRINTK("PORT_CMD 0x%x\n", tmp);
  960. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  961. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  962. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  963. PORT_CMD_FIS_RX | PORT_CMD_START);
  964. writel(tmp, port_mmio + PORT_CMD);
  965. readl(port_mmio + PORT_CMD); /* flush */
  966. /* spec says 500 msecs for each bit, so
  967. * this is slightly incorrect.
  968. */
  969. msleep(500);
  970. }
  971. writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
  972. j = 0;
  973. while (j < 100) {
  974. msleep(10);
  975. tmp = readl(port_mmio + PORT_SCR_STAT);
  976. if ((tmp & 0xf) == 0x3)
  977. break;
  978. j++;
  979. }
  980. tmp = readl(port_mmio + PORT_SCR_ERR);
  981. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  982. writel(tmp, port_mmio + PORT_SCR_ERR);
  983. /* ack any pending irq events for this port */
  984. tmp = readl(port_mmio + PORT_IRQ_STAT);
  985. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  986. if (tmp)
  987. writel(tmp, port_mmio + PORT_IRQ_STAT);
  988. writel(1 << i, mmio + HOST_IRQ_STAT);
  989. }
  990. tmp = readl(mmio + HOST_CTL);
  991. VPRINTK("HOST_CTL 0x%x\n", tmp);
  992. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  993. tmp = readl(mmio + HOST_CTL);
  994. VPRINTK("HOST_CTL 0x%x\n", tmp);
  995. pci_set_master(pdev);
  996. return 0;
  997. }
  998. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  999. {
  1000. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1001. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1002. void __iomem *mmio = probe_ent->mmio_base;
  1003. u32 vers, cap, impl, speed;
  1004. const char *speed_s;
  1005. u16 cc;
  1006. const char *scc_s;
  1007. vers = readl(mmio + HOST_VERSION);
  1008. cap = hpriv->cap;
  1009. impl = hpriv->port_map;
  1010. speed = (cap >> 20) & 0xf;
  1011. if (speed == 1)
  1012. speed_s = "1.5";
  1013. else if (speed == 2)
  1014. speed_s = "3";
  1015. else
  1016. speed_s = "?";
  1017. pci_read_config_word(pdev, 0x0a, &cc);
  1018. if (cc == 0x0101)
  1019. scc_s = "IDE";
  1020. else if (cc == 0x0106)
  1021. scc_s = "SATA";
  1022. else if (cc == 0x0104)
  1023. scc_s = "RAID";
  1024. else
  1025. scc_s = "unknown";
  1026. dev_printk(KERN_INFO, &pdev->dev,
  1027. "AHCI %02x%02x.%02x%02x "
  1028. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1029. ,
  1030. (vers >> 24) & 0xff,
  1031. (vers >> 16) & 0xff,
  1032. (vers >> 8) & 0xff,
  1033. vers & 0xff,
  1034. ((cap >> 8) & 0x1f) + 1,
  1035. (cap & 0x1f) + 1,
  1036. speed_s,
  1037. impl,
  1038. scc_s);
  1039. dev_printk(KERN_INFO, &pdev->dev,
  1040. "flags: "
  1041. "%s%s%s%s%s%s"
  1042. "%s%s%s%s%s%s%s\n"
  1043. ,
  1044. cap & (1 << 31) ? "64bit " : "",
  1045. cap & (1 << 30) ? "ncq " : "",
  1046. cap & (1 << 28) ? "ilck " : "",
  1047. cap & (1 << 27) ? "stag " : "",
  1048. cap & (1 << 26) ? "pm " : "",
  1049. cap & (1 << 25) ? "led " : "",
  1050. cap & (1 << 24) ? "clo " : "",
  1051. cap & (1 << 19) ? "nz " : "",
  1052. cap & (1 << 18) ? "only " : "",
  1053. cap & (1 << 17) ? "pmp " : "",
  1054. cap & (1 << 15) ? "pio " : "",
  1055. cap & (1 << 14) ? "slum " : "",
  1056. cap & (1 << 13) ? "part " : ""
  1057. );
  1058. }
  1059. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1060. {
  1061. static int printed_version;
  1062. struct ata_probe_ent *probe_ent = NULL;
  1063. struct ahci_host_priv *hpriv;
  1064. unsigned long base;
  1065. void __iomem *mmio_base;
  1066. unsigned int board_idx = (unsigned int) ent->driver_data;
  1067. int have_msi, pci_dev_busy = 0;
  1068. int rc;
  1069. VPRINTK("ENTER\n");
  1070. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1071. if (!printed_version++)
  1072. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1073. rc = pci_enable_device(pdev);
  1074. if (rc)
  1075. return rc;
  1076. rc = pci_request_regions(pdev, DRV_NAME);
  1077. if (rc) {
  1078. pci_dev_busy = 1;
  1079. goto err_out;
  1080. }
  1081. if (pci_enable_msi(pdev) == 0)
  1082. have_msi = 1;
  1083. else {
  1084. pci_intx(pdev, 1);
  1085. have_msi = 0;
  1086. }
  1087. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1088. if (probe_ent == NULL) {
  1089. rc = -ENOMEM;
  1090. goto err_out_msi;
  1091. }
  1092. memset(probe_ent, 0, sizeof(*probe_ent));
  1093. probe_ent->dev = pci_dev_to_dev(pdev);
  1094. INIT_LIST_HEAD(&probe_ent->node);
  1095. mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
  1096. if (mmio_base == NULL) {
  1097. rc = -ENOMEM;
  1098. goto err_out_free_ent;
  1099. }
  1100. base = (unsigned long) mmio_base;
  1101. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1102. if (!hpriv) {
  1103. rc = -ENOMEM;
  1104. goto err_out_iounmap;
  1105. }
  1106. memset(hpriv, 0, sizeof(*hpriv));
  1107. probe_ent->sht = ahci_port_info[board_idx].sht;
  1108. probe_ent->host_flags = ahci_port_info[board_idx].host_flags;
  1109. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  1110. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  1111. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  1112. probe_ent->irq = pdev->irq;
  1113. probe_ent->irq_flags = SA_SHIRQ;
  1114. probe_ent->mmio_base = mmio_base;
  1115. probe_ent->private_data = hpriv;
  1116. if (have_msi)
  1117. hpriv->flags |= AHCI_FLAG_MSI;
  1118. /* JMicron-specific fixup: make sure we're in AHCI mode */
  1119. if (pdev->vendor == 0x197b)
  1120. pci_write_config_byte(pdev, 0x41, 0xa1);
  1121. /* initialize adapter */
  1122. rc = ahci_host_init(probe_ent);
  1123. if (rc)
  1124. goto err_out_hpriv;
  1125. if (hpriv->cap & HOST_CAP_NCQ)
  1126. probe_ent->host_flags |= ATA_FLAG_NCQ;
  1127. ahci_print_info(probe_ent);
  1128. /* FIXME: check ata_device_add return value */
  1129. ata_device_add(probe_ent);
  1130. kfree(probe_ent);
  1131. return 0;
  1132. err_out_hpriv:
  1133. kfree(hpriv);
  1134. err_out_iounmap:
  1135. pci_iounmap(pdev, mmio_base);
  1136. err_out_free_ent:
  1137. kfree(probe_ent);
  1138. err_out_msi:
  1139. if (have_msi)
  1140. pci_disable_msi(pdev);
  1141. else
  1142. pci_intx(pdev, 0);
  1143. pci_release_regions(pdev);
  1144. err_out:
  1145. if (!pci_dev_busy)
  1146. pci_disable_device(pdev);
  1147. return rc;
  1148. }
  1149. static void ahci_remove_one (struct pci_dev *pdev)
  1150. {
  1151. struct device *dev = pci_dev_to_dev(pdev);
  1152. struct ata_host_set *host_set = dev_get_drvdata(dev);
  1153. struct ahci_host_priv *hpriv = host_set->private_data;
  1154. unsigned int i;
  1155. int have_msi;
  1156. for (i = 0; i < host_set->n_ports; i++)
  1157. ata_port_detach(host_set->ports[i]);
  1158. have_msi = hpriv->flags & AHCI_FLAG_MSI;
  1159. free_irq(host_set->irq, host_set);
  1160. for (i = 0; i < host_set->n_ports; i++) {
  1161. struct ata_port *ap = host_set->ports[i];
  1162. ata_scsi_release(ap->host);
  1163. scsi_host_put(ap->host);
  1164. }
  1165. kfree(hpriv);
  1166. pci_iounmap(pdev, host_set->mmio_base);
  1167. kfree(host_set);
  1168. if (have_msi)
  1169. pci_disable_msi(pdev);
  1170. else
  1171. pci_intx(pdev, 0);
  1172. pci_release_regions(pdev);
  1173. pci_disable_device(pdev);
  1174. dev_set_drvdata(dev, NULL);
  1175. }
  1176. static int __init ahci_init(void)
  1177. {
  1178. return pci_module_init(&ahci_pci_driver);
  1179. }
  1180. static void __exit ahci_exit(void)
  1181. {
  1182. pci_unregister_driver(&ahci_pci_driver);
  1183. }
  1184. MODULE_AUTHOR("Jeff Garzik");
  1185. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1186. MODULE_LICENSE("GPL");
  1187. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1188. MODULE_VERSION(DRV_VERSION);
  1189. module_init(ahci_init);
  1190. module_exit(ahci_exit);