ahci.c 38 KB

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