ahci.c 34 KB

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