ahci.c 35 KB

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