ahci.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  331. {
  332. unsigned int sc_reg;
  333. switch (sc_reg_in) {
  334. case SCR_STATUS: sc_reg = 0; break;
  335. case SCR_CONTROL: sc_reg = 1; break;
  336. case SCR_ERROR: sc_reg = 2; break;
  337. case SCR_ACTIVE: sc_reg = 3; break;
  338. default:
  339. return 0xffffffffU;
  340. }
  341. return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  342. }
  343. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  344. u32 val)
  345. {
  346. unsigned int sc_reg;
  347. switch (sc_reg_in) {
  348. case SCR_STATUS: sc_reg = 0; break;
  349. case SCR_CONTROL: sc_reg = 1; break;
  350. case SCR_ERROR: sc_reg = 2; break;
  351. case SCR_ACTIVE: sc_reg = 3; break;
  352. default:
  353. return;
  354. }
  355. writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  356. }
  357. static int ahci_start_engine(void __iomem *port_mmio)
  358. {
  359. u32 tmp;
  360. /* get current status */
  361. tmp = readl(port_mmio + PORT_CMD);
  362. /* AHCI rev 1.1 section 10.3.1:
  363. * Software shall not set PxCMD.ST to '1' until it verifies
  364. * that PxCMD.CR is '0' and has set PxCMD.FRE to '1'
  365. */
  366. if ((tmp & PORT_CMD_FIS_RX) == 0)
  367. return -EPERM;
  368. /* wait for engine to become idle */
  369. tmp = ata_wait_register(port_mmio + PORT_CMD,
  370. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1,500);
  371. if (tmp & PORT_CMD_LIST_ON)
  372. return -EBUSY;
  373. /* start DMA */
  374. tmp |= PORT_CMD_START;
  375. writel(tmp, port_mmio + PORT_CMD);
  376. readl(port_mmio + PORT_CMD); /* flush */
  377. return 0;
  378. }
  379. static int ahci_stop_engine(void __iomem *port_mmio)
  380. {
  381. u32 tmp;
  382. tmp = readl(port_mmio + PORT_CMD);
  383. /* check if the HBA is idle */
  384. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  385. return 0;
  386. /* setting HBA to idle */
  387. tmp &= ~PORT_CMD_START;
  388. writel(tmp, port_mmio + PORT_CMD);
  389. /* wait for engine to stop. This could be as long as 500 msec */
  390. tmp = ata_wait_register(port_mmio + PORT_CMD,
  391. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  392. if (tmp & PORT_CMD_LIST_ON)
  393. return -EIO;
  394. return 0;
  395. }
  396. static unsigned int ahci_dev_classify(struct ata_port *ap)
  397. {
  398. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  399. struct ata_taskfile tf;
  400. u32 tmp;
  401. tmp = readl(port_mmio + PORT_SIG);
  402. tf.lbah = (tmp >> 24) & 0xff;
  403. tf.lbam = (tmp >> 16) & 0xff;
  404. tf.lbal = (tmp >> 8) & 0xff;
  405. tf.nsect = (tmp) & 0xff;
  406. return ata_dev_classify(&tf);
  407. }
  408. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  409. u32 opts)
  410. {
  411. dma_addr_t cmd_tbl_dma;
  412. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  413. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  414. pp->cmd_slot[tag].status = 0;
  415. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  416. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  417. }
  418. static int ahci_clo(struct ata_port *ap)
  419. {
  420. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  421. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  422. u32 tmp;
  423. if (!(hpriv->cap & HOST_CAP_CLO))
  424. return -EOPNOTSUPP;
  425. tmp = readl(port_mmio + PORT_CMD);
  426. tmp |= PORT_CMD_CLO;
  427. writel(tmp, port_mmio + PORT_CMD);
  428. tmp = ata_wait_register(port_mmio + PORT_CMD,
  429. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  430. if (tmp & PORT_CMD_CLO)
  431. return -EIO;
  432. return 0;
  433. }
  434. static int ahci_prereset(struct ata_port *ap)
  435. {
  436. if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
  437. (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
  438. /* ATA_BUSY hasn't cleared, so send a CLO */
  439. ahci_clo(ap);
  440. }
  441. return ata_std_prereset(ap);
  442. }
  443. static int ahci_softreset(struct ata_port *ap, unsigned int *class)
  444. {
  445. struct ahci_port_priv *pp = ap->private_data;
  446. void __iomem *mmio = ap->host_set->mmio_base;
  447. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  448. const u32 cmd_fis_len = 5; /* five dwords */
  449. const char *reason = NULL;
  450. struct ata_taskfile tf;
  451. u32 tmp;
  452. u8 *fis;
  453. int rc;
  454. DPRINTK("ENTER\n");
  455. if (ata_port_offline(ap)) {
  456. DPRINTK("PHY reports no device\n");
  457. *class = ATA_DEV_NONE;
  458. return 0;
  459. }
  460. /* prepare for SRST (AHCI-1.1 10.4.1) */
  461. rc = ahci_stop_engine(port_mmio);
  462. if (rc) {
  463. reason = "failed to stop engine";
  464. goto fail_restart;
  465. }
  466. /* check BUSY/DRQ, perform Command List Override if necessary */
  467. ahci_tf_read(ap, &tf);
  468. if (tf.command & (ATA_BUSY | ATA_DRQ)) {
  469. rc = ahci_clo(ap);
  470. if (rc == -EOPNOTSUPP) {
  471. reason = "port busy but CLO unavailable";
  472. goto fail_restart;
  473. } else if (rc) {
  474. reason = "port busy but CLO failed";
  475. goto fail_restart;
  476. }
  477. }
  478. /* restart engine */
  479. ahci_start_engine(port_mmio);
  480. ata_tf_init(ap->device, &tf);
  481. fis = pp->cmd_tbl;
  482. /* issue the first D2H Register FIS */
  483. ahci_fill_cmd_slot(pp, 0,
  484. cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
  485. tf.ctl |= ATA_SRST;
  486. ata_tf_to_fis(&tf, fis, 0);
  487. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  488. writel(1, port_mmio + PORT_CMD_ISSUE);
  489. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1, 1, 500);
  490. if (tmp & 0x1) {
  491. rc = -EIO;
  492. reason = "1st FIS failed";
  493. goto fail;
  494. }
  495. /* spec says at least 5us, but be generous and sleep for 1ms */
  496. msleep(1);
  497. /* issue the second D2H Register FIS */
  498. ahci_fill_cmd_slot(pp, 0, cmd_fis_len);
  499. tf.ctl &= ~ATA_SRST;
  500. ata_tf_to_fis(&tf, fis, 0);
  501. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  502. writel(1, port_mmio + PORT_CMD_ISSUE);
  503. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  504. /* spec mandates ">= 2ms" before checking status.
  505. * We wait 150ms, because that was the magic delay used for
  506. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  507. * between when the ATA command register is written, and then
  508. * status is checked. Because waiting for "a while" before
  509. * checking status is fine, post SRST, we perform this magic
  510. * delay here as well.
  511. */
  512. msleep(150);
  513. *class = ATA_DEV_NONE;
  514. if (ata_port_online(ap)) {
  515. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  516. rc = -EIO;
  517. reason = "device not ready";
  518. goto fail;
  519. }
  520. *class = ahci_dev_classify(ap);
  521. }
  522. DPRINTK("EXIT, class=%u\n", *class);
  523. return 0;
  524. fail_restart:
  525. ahci_start_engine(port_mmio);
  526. fail:
  527. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  528. return rc;
  529. }
  530. static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
  531. {
  532. struct ahci_port_priv *pp = ap->private_data;
  533. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  534. struct ata_taskfile tf;
  535. void __iomem *mmio = ap->host_set->mmio_base;
  536. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  537. int rc;
  538. DPRINTK("ENTER\n");
  539. ahci_stop_engine(port_mmio);
  540. /* clear D2H reception area to properly wait for D2H FIS */
  541. ata_tf_init(ap->device, &tf);
  542. tf.command = 0xff;
  543. ata_tf_to_fis(&tf, d2h_fis, 0);
  544. rc = sata_std_hardreset(ap, class);
  545. ahci_start_engine(port_mmio);
  546. if (rc == 0 && ata_port_online(ap))
  547. *class = ahci_dev_classify(ap);
  548. if (*class == ATA_DEV_UNKNOWN)
  549. *class = ATA_DEV_NONE;
  550. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  551. return rc;
  552. }
  553. static void ahci_postreset(struct ata_port *ap, unsigned int *class)
  554. {
  555. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  556. u32 new_tmp, tmp;
  557. ata_std_postreset(ap, class);
  558. /* Make sure port's ATAPI bit is set appropriately */
  559. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  560. if (*class == ATA_DEV_ATAPI)
  561. new_tmp |= PORT_CMD_ATAPI;
  562. else
  563. new_tmp &= ~PORT_CMD_ATAPI;
  564. if (new_tmp != tmp) {
  565. writel(new_tmp, port_mmio + PORT_CMD);
  566. readl(port_mmio + PORT_CMD); /* flush */
  567. }
  568. }
  569. static u8 ahci_check_status(struct ata_port *ap)
  570. {
  571. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  572. return readl(mmio + PORT_TFDATA) & 0xFF;
  573. }
  574. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  575. {
  576. struct ahci_port_priv *pp = ap->private_data;
  577. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  578. ata_tf_from_fis(d2h_fis, tf);
  579. }
  580. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  581. {
  582. struct scatterlist *sg;
  583. struct ahci_sg *ahci_sg;
  584. unsigned int n_sg = 0;
  585. VPRINTK("ENTER\n");
  586. /*
  587. * Next, the S/G list.
  588. */
  589. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  590. ata_for_each_sg(sg, qc) {
  591. dma_addr_t addr = sg_dma_address(sg);
  592. u32 sg_len = sg_dma_len(sg);
  593. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  594. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  595. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  596. ahci_sg++;
  597. n_sg++;
  598. }
  599. return n_sg;
  600. }
  601. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  602. {
  603. struct ata_port *ap = qc->ap;
  604. struct ahci_port_priv *pp = ap->private_data;
  605. int is_atapi = is_atapi_taskfile(&qc->tf);
  606. void *cmd_tbl;
  607. u32 opts;
  608. const u32 cmd_fis_len = 5; /* five dwords */
  609. unsigned int n_elem;
  610. /*
  611. * Fill in command table information. First, the header,
  612. * a SATA Register - Host to Device command FIS.
  613. */
  614. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  615. ata_tf_to_fis(&qc->tf, cmd_tbl, 0);
  616. if (is_atapi) {
  617. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  618. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  619. }
  620. n_elem = 0;
  621. if (qc->flags & ATA_QCFLAG_DMAMAP)
  622. n_elem = ahci_fill_sg(qc, cmd_tbl);
  623. /*
  624. * Fill in command slot information.
  625. */
  626. opts = cmd_fis_len | n_elem << 16;
  627. if (qc->tf.flags & ATA_TFLAG_WRITE)
  628. opts |= AHCI_CMD_WRITE;
  629. if (is_atapi)
  630. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  631. ahci_fill_cmd_slot(pp, qc->tag, opts);
  632. }
  633. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  634. {
  635. struct ahci_port_priv *pp = ap->private_data;
  636. struct ata_eh_info *ehi = &ap->eh_info;
  637. unsigned int err_mask = 0, action = 0;
  638. struct ata_queued_cmd *qc;
  639. u32 serror;
  640. ata_ehi_clear_desc(ehi);
  641. /* AHCI needs SError cleared; otherwise, it might lock up */
  642. serror = ahci_scr_read(ap, SCR_ERROR);
  643. ahci_scr_write(ap, SCR_ERROR, serror);
  644. /* analyze @irq_stat */
  645. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  646. if (irq_stat & PORT_IRQ_TF_ERR)
  647. err_mask |= AC_ERR_DEV;
  648. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  649. err_mask |= AC_ERR_HOST_BUS;
  650. action |= ATA_EH_SOFTRESET;
  651. }
  652. if (irq_stat & PORT_IRQ_IF_ERR) {
  653. err_mask |= AC_ERR_ATA_BUS;
  654. action |= ATA_EH_SOFTRESET;
  655. ata_ehi_push_desc(ehi, ", interface fatal error");
  656. }
  657. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  658. ata_ehi_hotplugged(ehi);
  659. ata_ehi_push_desc(ehi, ", %s", irq_stat & PORT_IRQ_CONNECT ?
  660. "connection status changed" : "PHY RDY changed");
  661. }
  662. if (irq_stat & PORT_IRQ_UNK_FIS) {
  663. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  664. err_mask |= AC_ERR_HSM;
  665. action |= ATA_EH_SOFTRESET;
  666. ata_ehi_push_desc(ehi, ", unknown FIS %08x %08x %08x %08x",
  667. unk[0], unk[1], unk[2], unk[3]);
  668. }
  669. /* okay, let's hand over to EH */
  670. ehi->serror |= serror;
  671. ehi->action |= action;
  672. qc = ata_qc_from_tag(ap, ap->active_tag);
  673. if (qc)
  674. qc->err_mask |= err_mask;
  675. else
  676. ehi->err_mask |= err_mask;
  677. if (irq_stat & PORT_IRQ_FREEZE)
  678. ata_port_freeze(ap);
  679. else
  680. ata_port_abort(ap);
  681. }
  682. static void ahci_host_intr(struct ata_port *ap)
  683. {
  684. void __iomem *mmio = ap->host_set->mmio_base;
  685. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  686. struct ata_eh_info *ehi = &ap->eh_info;
  687. u32 status, qc_active;
  688. int rc;
  689. status = readl(port_mmio + PORT_IRQ_STAT);
  690. writel(status, port_mmio + PORT_IRQ_STAT);
  691. if (unlikely(status & PORT_IRQ_ERROR)) {
  692. ahci_error_intr(ap, status);
  693. return;
  694. }
  695. if (ap->sactive)
  696. qc_active = readl(port_mmio + PORT_SCR_ACT);
  697. else
  698. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  699. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  700. if (rc > 0)
  701. return;
  702. if (rc < 0) {
  703. ehi->err_mask |= AC_ERR_HSM;
  704. ehi->action |= ATA_EH_SOFTRESET;
  705. ata_port_freeze(ap);
  706. return;
  707. }
  708. /* hmmm... a spurious interupt */
  709. /* some devices send D2H reg with I bit set during NCQ command phase */
  710. if (ap->sactive && status & PORT_IRQ_D2H_REG_FIS)
  711. return;
  712. /* ignore interim PIO setup fis interrupts */
  713. if (ata_tag_valid(ap->active_tag)) {
  714. struct ata_queued_cmd *qc =
  715. ata_qc_from_tag(ap, ap->active_tag);
  716. if (qc && qc->tf.protocol == ATA_PROT_PIO &&
  717. (status & PORT_IRQ_PIOS_FIS))
  718. return;
  719. }
  720. if (ata_ratelimit())
  721. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  722. "(irq_stat 0x%x active_tag %d sactive 0x%x)\n",
  723. status, ap->active_tag, ap->sactive);
  724. }
  725. static void ahci_irq_clear(struct ata_port *ap)
  726. {
  727. /* TODO */
  728. }
  729. static irqreturn_t ahci_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
  730. {
  731. struct ata_host_set *host_set = dev_instance;
  732. struct ahci_host_priv *hpriv;
  733. unsigned int i, handled = 0;
  734. void __iomem *mmio;
  735. u32 irq_stat, irq_ack = 0;
  736. VPRINTK("ENTER\n");
  737. hpriv = host_set->private_data;
  738. mmio = host_set->mmio_base;
  739. /* sigh. 0xffffffff is a valid return from h/w */
  740. irq_stat = readl(mmio + HOST_IRQ_STAT);
  741. irq_stat &= hpriv->port_map;
  742. if (!irq_stat)
  743. return IRQ_NONE;
  744. spin_lock(&host_set->lock);
  745. for (i = 0; i < host_set->n_ports; i++) {
  746. struct ata_port *ap;
  747. if (!(irq_stat & (1 << i)))
  748. continue;
  749. ap = host_set->ports[i];
  750. if (ap) {
  751. ahci_host_intr(ap);
  752. VPRINTK("port %u\n", i);
  753. } else {
  754. VPRINTK("port %u (no irq)\n", i);
  755. if (ata_ratelimit())
  756. dev_printk(KERN_WARNING, host_set->dev,
  757. "interrupt on disabled port %u\n", i);
  758. }
  759. irq_ack |= (1 << i);
  760. }
  761. if (irq_ack) {
  762. writel(irq_ack, mmio + HOST_IRQ_STAT);
  763. handled = 1;
  764. }
  765. spin_unlock(&host_set->lock);
  766. VPRINTK("EXIT\n");
  767. return IRQ_RETVAL(handled);
  768. }
  769. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  770. {
  771. struct ata_port *ap = qc->ap;
  772. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  773. if (qc->tf.protocol == ATA_PROT_NCQ)
  774. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  775. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  776. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  777. return 0;
  778. }
  779. static void ahci_freeze(struct ata_port *ap)
  780. {
  781. void __iomem *mmio = ap->host_set->mmio_base;
  782. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  783. /* turn IRQ off */
  784. writel(0, port_mmio + PORT_IRQ_MASK);
  785. }
  786. static void ahci_thaw(struct ata_port *ap)
  787. {
  788. void __iomem *mmio = ap->host_set->mmio_base;
  789. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  790. u32 tmp;
  791. /* clear IRQ */
  792. tmp = readl(port_mmio + PORT_IRQ_STAT);
  793. writel(tmp, port_mmio + PORT_IRQ_STAT);
  794. writel(1 << ap->id, mmio + HOST_IRQ_STAT);
  795. /* turn IRQ back on */
  796. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  797. }
  798. static void ahci_error_handler(struct ata_port *ap)
  799. {
  800. void __iomem *mmio = ap->host_set->mmio_base;
  801. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  802. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  803. /* restart engine */
  804. ahci_stop_engine(port_mmio);
  805. ahci_start_engine(port_mmio);
  806. }
  807. /* perform recovery */
  808. ata_do_eh(ap, ahci_prereset, ahci_softreset, ahci_hardreset,
  809. ahci_postreset);
  810. }
  811. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  812. {
  813. struct ata_port *ap = qc->ap;
  814. void __iomem *mmio = ap->host_set->mmio_base;
  815. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  816. if (qc->flags & ATA_QCFLAG_FAILED)
  817. qc->err_mask |= AC_ERR_OTHER;
  818. if (qc->err_mask) {
  819. /* make DMA engine forget about the failed command */
  820. ahci_stop_engine(port_mmio);
  821. ahci_start_engine(port_mmio);
  822. }
  823. }
  824. static int ahci_port_start(struct ata_port *ap)
  825. {
  826. struct device *dev = ap->host_set->dev;
  827. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  828. struct ahci_port_priv *pp;
  829. void __iomem *mmio = ap->host_set->mmio_base;
  830. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  831. void *mem;
  832. dma_addr_t mem_dma;
  833. int rc;
  834. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  835. if (!pp)
  836. return -ENOMEM;
  837. memset(pp, 0, sizeof(*pp));
  838. rc = ata_pad_alloc(ap, dev);
  839. if (rc) {
  840. kfree(pp);
  841. return rc;
  842. }
  843. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  844. if (!mem) {
  845. ata_pad_free(ap, dev);
  846. kfree(pp);
  847. return -ENOMEM;
  848. }
  849. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  850. /*
  851. * First item in chunk of DMA memory: 32-slot command table,
  852. * 32 bytes each in size
  853. */
  854. pp->cmd_slot = mem;
  855. pp->cmd_slot_dma = mem_dma;
  856. mem += AHCI_CMD_SLOT_SZ;
  857. mem_dma += AHCI_CMD_SLOT_SZ;
  858. /*
  859. * Second item: Received-FIS area
  860. */
  861. pp->rx_fis = mem;
  862. pp->rx_fis_dma = mem_dma;
  863. mem += AHCI_RX_FIS_SZ;
  864. mem_dma += AHCI_RX_FIS_SZ;
  865. /*
  866. * Third item: data area for storing a single command
  867. * and its scatter-gather table
  868. */
  869. pp->cmd_tbl = mem;
  870. pp->cmd_tbl_dma = mem_dma;
  871. ap->private_data = pp;
  872. if (hpriv->cap & HOST_CAP_64)
  873. writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  874. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  875. readl(port_mmio + PORT_LST_ADDR); /* flush */
  876. if (hpriv->cap & HOST_CAP_64)
  877. writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  878. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  879. readl(port_mmio + PORT_FIS_ADDR); /* flush */
  880. writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  881. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  882. PORT_CMD_START, port_mmio + PORT_CMD);
  883. readl(port_mmio + PORT_CMD); /* flush */
  884. return 0;
  885. }
  886. static void ahci_port_stop(struct ata_port *ap)
  887. {
  888. struct device *dev = ap->host_set->dev;
  889. struct ahci_port_priv *pp = ap->private_data;
  890. void __iomem *mmio = ap->host_set->mmio_base;
  891. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  892. u32 tmp;
  893. tmp = readl(port_mmio + PORT_CMD);
  894. tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX);
  895. writel(tmp, port_mmio + PORT_CMD);
  896. readl(port_mmio + PORT_CMD); /* flush */
  897. /* spec says 500 msecs for each PORT_CMD_{START,FIS_RX} bit, so
  898. * this is slightly incorrect.
  899. */
  900. msleep(500);
  901. ap->private_data = NULL;
  902. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  903. pp->cmd_slot, pp->cmd_slot_dma);
  904. ata_pad_free(ap, dev);
  905. kfree(pp);
  906. }
  907. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  908. unsigned int port_idx)
  909. {
  910. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  911. base = ahci_port_base_ul(base, port_idx);
  912. VPRINTK("base now==0x%lx\n", base);
  913. port->cmd_addr = base;
  914. port->scr_addr = base + PORT_SCR;
  915. VPRINTK("EXIT\n");
  916. }
  917. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  918. {
  919. struct ahci_host_priv *hpriv = probe_ent->private_data;
  920. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  921. void __iomem *mmio = probe_ent->mmio_base;
  922. u32 tmp, cap_save;
  923. unsigned int i, j, using_dac;
  924. int rc;
  925. void __iomem *port_mmio;
  926. cap_save = readl(mmio + HOST_CAP);
  927. cap_save &= ( (1<<28) | (1<<17) );
  928. cap_save |= (1 << 27);
  929. /* global controller reset */
  930. tmp = readl(mmio + HOST_CTL);
  931. if ((tmp & HOST_RESET) == 0) {
  932. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  933. readl(mmio + HOST_CTL); /* flush */
  934. }
  935. /* reset must complete within 1 second, or
  936. * the hardware should be considered fried.
  937. */
  938. ssleep(1);
  939. tmp = readl(mmio + HOST_CTL);
  940. if (tmp & HOST_RESET) {
  941. dev_printk(KERN_ERR, &pdev->dev,
  942. "controller reset failed (0x%x)\n", tmp);
  943. return -EIO;
  944. }
  945. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  946. (void) readl(mmio + HOST_CTL); /* flush */
  947. writel(cap_save, mmio + HOST_CAP);
  948. writel(0xf, mmio + HOST_PORTS_IMPL);
  949. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  950. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  951. u16 tmp16;
  952. pci_read_config_word(pdev, 0x92, &tmp16);
  953. tmp16 |= 0xf;
  954. pci_write_config_word(pdev, 0x92, tmp16);
  955. }
  956. hpriv->cap = readl(mmio + HOST_CAP);
  957. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  958. probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
  959. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  960. hpriv->cap, hpriv->port_map, probe_ent->n_ports);
  961. using_dac = hpriv->cap & HOST_CAP_64;
  962. if (using_dac &&
  963. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  964. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  965. if (rc) {
  966. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  967. if (rc) {
  968. dev_printk(KERN_ERR, &pdev->dev,
  969. "64-bit DMA enable failed\n");
  970. return rc;
  971. }
  972. }
  973. } else {
  974. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  975. if (rc) {
  976. dev_printk(KERN_ERR, &pdev->dev,
  977. "32-bit DMA enable failed\n");
  978. return rc;
  979. }
  980. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  981. if (rc) {
  982. dev_printk(KERN_ERR, &pdev->dev,
  983. "32-bit consistent DMA enable failed\n");
  984. return rc;
  985. }
  986. }
  987. for (i = 0; i < probe_ent->n_ports; i++) {
  988. #if 0 /* BIOSen initialize this incorrectly */
  989. if (!(hpriv->port_map & (1 << i)))
  990. continue;
  991. #endif
  992. port_mmio = ahci_port_base(mmio, i);
  993. VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
  994. ahci_setup_port(&probe_ent->port[i],
  995. (unsigned long) mmio, i);
  996. /* make sure port is not active */
  997. tmp = readl(port_mmio + PORT_CMD);
  998. VPRINTK("PORT_CMD 0x%x\n", tmp);
  999. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  1000. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  1001. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  1002. PORT_CMD_FIS_RX | PORT_CMD_START);
  1003. writel(tmp, port_mmio + PORT_CMD);
  1004. readl(port_mmio + PORT_CMD); /* flush */
  1005. /* spec says 500 msecs for each bit, so
  1006. * this is slightly incorrect.
  1007. */
  1008. msleep(500);
  1009. }
  1010. writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
  1011. j = 0;
  1012. while (j < 100) {
  1013. msleep(10);
  1014. tmp = readl(port_mmio + PORT_SCR_STAT);
  1015. if ((tmp & 0xf) == 0x3)
  1016. break;
  1017. j++;
  1018. }
  1019. tmp = readl(port_mmio + PORT_SCR_ERR);
  1020. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  1021. writel(tmp, port_mmio + PORT_SCR_ERR);
  1022. /* ack any pending irq events for this port */
  1023. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1024. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  1025. if (tmp)
  1026. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1027. writel(1 << i, mmio + HOST_IRQ_STAT);
  1028. }
  1029. tmp = readl(mmio + HOST_CTL);
  1030. VPRINTK("HOST_CTL 0x%x\n", tmp);
  1031. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  1032. tmp = readl(mmio + HOST_CTL);
  1033. VPRINTK("HOST_CTL 0x%x\n", tmp);
  1034. pci_set_master(pdev);
  1035. return 0;
  1036. }
  1037. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  1038. {
  1039. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1040. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1041. void __iomem *mmio = probe_ent->mmio_base;
  1042. u32 vers, cap, impl, speed;
  1043. const char *speed_s;
  1044. u16 cc;
  1045. const char *scc_s;
  1046. vers = readl(mmio + HOST_VERSION);
  1047. cap = hpriv->cap;
  1048. impl = hpriv->port_map;
  1049. speed = (cap >> 20) & 0xf;
  1050. if (speed == 1)
  1051. speed_s = "1.5";
  1052. else if (speed == 2)
  1053. speed_s = "3";
  1054. else
  1055. speed_s = "?";
  1056. pci_read_config_word(pdev, 0x0a, &cc);
  1057. if (cc == 0x0101)
  1058. scc_s = "IDE";
  1059. else if (cc == 0x0106)
  1060. scc_s = "SATA";
  1061. else if (cc == 0x0104)
  1062. scc_s = "RAID";
  1063. else
  1064. scc_s = "unknown";
  1065. dev_printk(KERN_INFO, &pdev->dev,
  1066. "AHCI %02x%02x.%02x%02x "
  1067. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1068. ,
  1069. (vers >> 24) & 0xff,
  1070. (vers >> 16) & 0xff,
  1071. (vers >> 8) & 0xff,
  1072. vers & 0xff,
  1073. ((cap >> 8) & 0x1f) + 1,
  1074. (cap & 0x1f) + 1,
  1075. speed_s,
  1076. impl,
  1077. scc_s);
  1078. dev_printk(KERN_INFO, &pdev->dev,
  1079. "flags: "
  1080. "%s%s%s%s%s%s"
  1081. "%s%s%s%s%s%s%s\n"
  1082. ,
  1083. cap & (1 << 31) ? "64bit " : "",
  1084. cap & (1 << 30) ? "ncq " : "",
  1085. cap & (1 << 28) ? "ilck " : "",
  1086. cap & (1 << 27) ? "stag " : "",
  1087. cap & (1 << 26) ? "pm " : "",
  1088. cap & (1 << 25) ? "led " : "",
  1089. cap & (1 << 24) ? "clo " : "",
  1090. cap & (1 << 19) ? "nz " : "",
  1091. cap & (1 << 18) ? "only " : "",
  1092. cap & (1 << 17) ? "pmp " : "",
  1093. cap & (1 << 15) ? "pio " : "",
  1094. cap & (1 << 14) ? "slum " : "",
  1095. cap & (1 << 13) ? "part " : ""
  1096. );
  1097. }
  1098. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1099. {
  1100. static int printed_version;
  1101. struct ata_probe_ent *probe_ent = NULL;
  1102. struct ahci_host_priv *hpriv;
  1103. unsigned long base;
  1104. void __iomem *mmio_base;
  1105. unsigned int board_idx = (unsigned int) ent->driver_data;
  1106. int have_msi, pci_dev_busy = 0;
  1107. int rc;
  1108. VPRINTK("ENTER\n");
  1109. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1110. if (!printed_version++)
  1111. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1112. /* JMicron-specific fixup: make sure we're in AHCI mode */
  1113. /* This is protected from races with ata_jmicron by the pci probe
  1114. locking */
  1115. if (pdev->vendor == PCI_VENDOR_ID_JMICRON) {
  1116. /* AHCI enable, AHCI on function 0 */
  1117. pci_write_config_byte(pdev, 0x41, 0xa1);
  1118. /* Function 1 is the PATA controller */
  1119. if (PCI_FUNC(pdev->devfn))
  1120. return -ENODEV;
  1121. }
  1122. rc = pci_enable_device(pdev);
  1123. if (rc)
  1124. return rc;
  1125. rc = pci_request_regions(pdev, DRV_NAME);
  1126. if (rc) {
  1127. pci_dev_busy = 1;
  1128. goto err_out;
  1129. }
  1130. if (pci_enable_msi(pdev) == 0)
  1131. have_msi = 1;
  1132. else {
  1133. pci_intx(pdev, 1);
  1134. have_msi = 0;
  1135. }
  1136. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1137. if (probe_ent == NULL) {
  1138. rc = -ENOMEM;
  1139. goto err_out_msi;
  1140. }
  1141. memset(probe_ent, 0, sizeof(*probe_ent));
  1142. probe_ent->dev = pci_dev_to_dev(pdev);
  1143. INIT_LIST_HEAD(&probe_ent->node);
  1144. mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
  1145. if (mmio_base == NULL) {
  1146. rc = -ENOMEM;
  1147. goto err_out_free_ent;
  1148. }
  1149. base = (unsigned long) mmio_base;
  1150. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1151. if (!hpriv) {
  1152. rc = -ENOMEM;
  1153. goto err_out_iounmap;
  1154. }
  1155. memset(hpriv, 0, sizeof(*hpriv));
  1156. probe_ent->sht = ahci_port_info[board_idx].sht;
  1157. probe_ent->host_flags = ahci_port_info[board_idx].host_flags;
  1158. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  1159. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  1160. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  1161. probe_ent->irq = pdev->irq;
  1162. probe_ent->irq_flags = IRQF_SHARED;
  1163. probe_ent->mmio_base = mmio_base;
  1164. probe_ent->private_data = hpriv;
  1165. if (have_msi)
  1166. hpriv->flags |= AHCI_FLAG_MSI;
  1167. /* initialize adapter */
  1168. rc = ahci_host_init(probe_ent);
  1169. if (rc)
  1170. goto err_out_hpriv;
  1171. if (!(probe_ent->host_flags & AHCI_FLAG_NO_NCQ) &&
  1172. (hpriv->cap & HOST_CAP_NCQ))
  1173. probe_ent->host_flags |= ATA_FLAG_NCQ;
  1174. ahci_print_info(probe_ent);
  1175. /* FIXME: check ata_device_add return value */
  1176. ata_device_add(probe_ent);
  1177. kfree(probe_ent);
  1178. return 0;
  1179. err_out_hpriv:
  1180. kfree(hpriv);
  1181. err_out_iounmap:
  1182. pci_iounmap(pdev, mmio_base);
  1183. err_out_free_ent:
  1184. kfree(probe_ent);
  1185. err_out_msi:
  1186. if (have_msi)
  1187. pci_disable_msi(pdev);
  1188. else
  1189. pci_intx(pdev, 0);
  1190. pci_release_regions(pdev);
  1191. err_out:
  1192. if (!pci_dev_busy)
  1193. pci_disable_device(pdev);
  1194. return rc;
  1195. }
  1196. static void ahci_remove_one (struct pci_dev *pdev)
  1197. {
  1198. struct device *dev = pci_dev_to_dev(pdev);
  1199. struct ata_host_set *host_set = dev_get_drvdata(dev);
  1200. struct ahci_host_priv *hpriv = host_set->private_data;
  1201. unsigned int i;
  1202. int have_msi;
  1203. for (i = 0; i < host_set->n_ports; i++)
  1204. ata_port_detach(host_set->ports[i]);
  1205. have_msi = hpriv->flags & AHCI_FLAG_MSI;
  1206. free_irq(host_set->irq, host_set);
  1207. for (i = 0; i < host_set->n_ports; i++) {
  1208. struct ata_port *ap = host_set->ports[i];
  1209. ata_scsi_release(ap->host);
  1210. scsi_host_put(ap->host);
  1211. }
  1212. kfree(hpriv);
  1213. pci_iounmap(pdev, host_set->mmio_base);
  1214. kfree(host_set);
  1215. if (have_msi)
  1216. pci_disable_msi(pdev);
  1217. else
  1218. pci_intx(pdev, 0);
  1219. pci_release_regions(pdev);
  1220. pci_disable_device(pdev);
  1221. dev_set_drvdata(dev, NULL);
  1222. }
  1223. static int __init ahci_init(void)
  1224. {
  1225. return pci_module_init(&ahci_pci_driver);
  1226. }
  1227. static void __exit ahci_exit(void)
  1228. {
  1229. pci_unregister_driver(&ahci_pci_driver);
  1230. }
  1231. MODULE_AUTHOR("Jeff Garzik");
  1232. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1233. MODULE_LICENSE("GPL");
  1234. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1235. MODULE_VERSION(DRV_VERSION);
  1236. module_init(ahci_init);
  1237. module_exit(ahci_exit);