ahci.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /*
  2. * ahci.c - AHCI SATA support
  3. *
  4. * Copyright 2004 Red Hat, Inc.
  5. *
  6. * The contents of this file are subject to the Open
  7. * Software License version 1.1 that can be found at
  8. * http://www.opensource.org/licenses/osl-1.1.txt and is included herein
  9. * by reference.
  10. *
  11. * Alternatively, the contents of this file may be used under the terms
  12. * of the GNU General Public License version 2 (the "GPL") as distributed
  13. * in the kernel source COPYING file, in which case the provisions of
  14. * the GPL are applicable instead of the above. If you wish to allow
  15. * the use of your version of this file only under the terms of the
  16. * GPL and not to allow others to use your version of this file under
  17. * the OSL, indicate your decision by deleting the provisions above and
  18. * replace them with the notice and other provisions required by the GPL.
  19. * If you do not delete the provisions above, a recipient may use your
  20. * version of this file under either the OSL or the GPL.
  21. *
  22. * Version 1.0 of the AHCI specification:
  23. * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/init.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/delay.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/sched.h>
  34. #include <linux/dma-mapping.h>
  35. #include "scsi.h"
  36. #include <scsi/scsi_host.h>
  37. #include <linux/libata.h>
  38. #include <asm/io.h>
  39. #define DRV_NAME "ahci"
  40. #define DRV_VERSION "1.01"
  41. enum {
  42. AHCI_PCI_BAR = 5,
  43. AHCI_MAX_SG = 168, /* hardware max is 64K */
  44. AHCI_DMA_BOUNDARY = 0xffffffff,
  45. AHCI_USE_CLUSTERING = 0,
  46. AHCI_CMD_SLOT_SZ = 32 * 32,
  47. AHCI_RX_FIS_SZ = 256,
  48. AHCI_CMD_TBL_HDR = 0x80,
  49. AHCI_CMD_TBL_CDB = 0x40,
  50. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR + (AHCI_MAX_SG * 16),
  51. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_SZ +
  52. AHCI_RX_FIS_SZ,
  53. AHCI_IRQ_ON_SG = (1 << 31),
  54. AHCI_CMD_ATAPI = (1 << 5),
  55. AHCI_CMD_WRITE = (1 << 6),
  56. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  57. board_ahci = 0,
  58. /* global controller registers */
  59. HOST_CAP = 0x00, /* host capabilities */
  60. HOST_CTL = 0x04, /* global host control */
  61. HOST_IRQ_STAT = 0x08, /* interrupt status */
  62. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  63. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  64. /* HOST_CTL bits */
  65. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  66. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  67. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  68. /* HOST_CAP bits */
  69. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  70. /* registers for each SATA port */
  71. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  72. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  73. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  74. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  75. PORT_IRQ_STAT = 0x10, /* interrupt status */
  76. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  77. PORT_CMD = 0x18, /* port command */
  78. PORT_TFDATA = 0x20, /* taskfile data */
  79. PORT_SIG = 0x24, /* device TF signature */
  80. PORT_CMD_ISSUE = 0x38, /* command issue */
  81. PORT_SCR = 0x28, /* SATA phy register block */
  82. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  83. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  84. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  85. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  86. /* PORT_IRQ_{STAT,MASK} bits */
  87. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  88. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  89. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  90. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  91. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  92. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  93. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  94. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  95. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  96. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  97. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  98. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  99. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  100. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  101. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  102. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  103. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  104. PORT_IRQ_FATAL = PORT_IRQ_TF_ERR |
  105. PORT_IRQ_HBUS_ERR |
  106. PORT_IRQ_HBUS_DATA_ERR |
  107. PORT_IRQ_IF_ERR,
  108. DEF_PORT_IRQ = PORT_IRQ_FATAL | PORT_IRQ_PHYRDY |
  109. PORT_IRQ_CONNECT | PORT_IRQ_SG_DONE |
  110. PORT_IRQ_UNK_FIS | PORT_IRQ_SDB_FIS |
  111. PORT_IRQ_DMAS_FIS | PORT_IRQ_PIOS_FIS |
  112. PORT_IRQ_D2H_REG_FIS,
  113. /* PORT_CMD bits */
  114. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  115. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  116. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  117. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  118. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  119. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  120. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  121. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  122. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  123. /* hpriv->flags bits */
  124. AHCI_FLAG_MSI = (1 << 0),
  125. };
  126. struct ahci_cmd_hdr {
  127. u32 opts;
  128. u32 status;
  129. u32 tbl_addr;
  130. u32 tbl_addr_hi;
  131. u32 reserved[4];
  132. };
  133. struct ahci_sg {
  134. u32 addr;
  135. u32 addr_hi;
  136. u32 reserved;
  137. u32 flags_size;
  138. };
  139. struct ahci_host_priv {
  140. unsigned long flags;
  141. u32 cap; /* cache of HOST_CAP register */
  142. u32 port_map; /* cache of HOST_PORTS_IMPL reg */
  143. };
  144. struct ahci_port_priv {
  145. struct ahci_cmd_hdr *cmd_slot;
  146. dma_addr_t cmd_slot_dma;
  147. void *cmd_tbl;
  148. dma_addr_t cmd_tbl_dma;
  149. struct ahci_sg *cmd_tbl_sg;
  150. void *rx_fis;
  151. dma_addr_t rx_fis_dma;
  152. };
  153. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
  154. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  155. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  156. static int ahci_qc_issue(struct ata_queued_cmd *qc);
  157. static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs);
  158. static void ahci_phy_reset(struct ata_port *ap);
  159. static void ahci_irq_clear(struct ata_port *ap);
  160. static void ahci_eng_timeout(struct ata_port *ap);
  161. static int ahci_port_start(struct ata_port *ap);
  162. static void ahci_port_stop(struct ata_port *ap);
  163. static void ahci_host_stop(struct ata_host_set *host_set);
  164. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  165. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  166. static u8 ahci_check_status(struct ata_port *ap);
  167. static u8 ahci_check_err(struct ata_port *ap);
  168. static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc);
  169. static void ahci_remove_one (struct pci_dev *pdev);
  170. static Scsi_Host_Template ahci_sht = {
  171. .module = THIS_MODULE,
  172. .name = DRV_NAME,
  173. .ioctl = ata_scsi_ioctl,
  174. .queuecommand = ata_scsi_queuecmd,
  175. .eh_strategy_handler = ata_scsi_error,
  176. .can_queue = ATA_DEF_QUEUE,
  177. .this_id = ATA_SHT_THIS_ID,
  178. .sg_tablesize = AHCI_MAX_SG,
  179. .max_sectors = ATA_MAX_SECTORS,
  180. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  181. .emulated = ATA_SHT_EMULATED,
  182. .use_clustering = AHCI_USE_CLUSTERING,
  183. .proc_name = DRV_NAME,
  184. .dma_boundary = AHCI_DMA_BOUNDARY,
  185. .slave_configure = ata_scsi_slave_config,
  186. .bios_param = ata_std_bios_param,
  187. .ordered_flush = 1,
  188. };
  189. static struct ata_port_operations ahci_ops = {
  190. .port_disable = ata_port_disable,
  191. .check_status = ahci_check_status,
  192. .check_altstatus = ahci_check_status,
  193. .check_err = ahci_check_err,
  194. .dev_select = ata_noop_dev_select,
  195. .tf_read = ahci_tf_read,
  196. .phy_reset = ahci_phy_reset,
  197. .qc_prep = ahci_qc_prep,
  198. .qc_issue = ahci_qc_issue,
  199. .eng_timeout = ahci_eng_timeout,
  200. .irq_handler = ahci_interrupt,
  201. .irq_clear = ahci_irq_clear,
  202. .scr_read = ahci_scr_read,
  203. .scr_write = ahci_scr_write,
  204. .port_start = ahci_port_start,
  205. .port_stop = ahci_port_stop,
  206. .host_stop = ahci_host_stop,
  207. };
  208. static struct ata_port_info ahci_port_info[] = {
  209. /* board_ahci */
  210. {
  211. .sht = &ahci_sht,
  212. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  213. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  214. ATA_FLAG_PIO_DMA,
  215. .pio_mask = 0x03, /* pio3-4 */
  216. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  217. .port_ops = &ahci_ops,
  218. },
  219. };
  220. static struct pci_device_id ahci_pci_tbl[] = {
  221. { PCI_VENDOR_ID_INTEL, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  222. board_ahci }, /* ICH6 */
  223. { PCI_VENDOR_ID_INTEL, 0x2653, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  224. board_ahci }, /* ICH6M */
  225. { PCI_VENDOR_ID_INTEL, 0x27c1, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  226. board_ahci }, /* ICH7 */
  227. { PCI_VENDOR_ID_INTEL, 0x27c5, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  228. board_ahci }, /* ICH7M */
  229. { PCI_VENDOR_ID_INTEL, 0x27c3, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  230. board_ahci }, /* ICH7R */
  231. { PCI_VENDOR_ID_AL, 0x5288, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  232. board_ahci }, /* ULi M5288 */
  233. { PCI_VENDOR_ID_INTEL, 0x2681, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  234. board_ahci }, /* ESB2 */
  235. { PCI_VENDOR_ID_INTEL, 0x2682, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  236. board_ahci }, /* ESB2 */
  237. { PCI_VENDOR_ID_INTEL, 0x2683, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  238. board_ahci }, /* ESB2 */
  239. { } /* terminate list */
  240. };
  241. static struct pci_driver ahci_pci_driver = {
  242. .name = DRV_NAME,
  243. .id_table = ahci_pci_tbl,
  244. .probe = ahci_init_one,
  245. .remove = ahci_remove_one,
  246. };
  247. static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
  248. {
  249. return base + 0x100 + (port * 0x80);
  250. }
  251. static inline void *ahci_port_base (void *base, unsigned int port)
  252. {
  253. return (void *) ahci_port_base_ul((unsigned long)base, port);
  254. }
  255. static void ahci_host_stop(struct ata_host_set *host_set)
  256. {
  257. struct ahci_host_priv *hpriv = host_set->private_data;
  258. kfree(hpriv);
  259. ata_host_stop(host_set);
  260. }
  261. static int ahci_port_start(struct ata_port *ap)
  262. {
  263. struct device *dev = ap->host_set->dev;
  264. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  265. struct ahci_port_priv *pp;
  266. int rc;
  267. void *mem, *mmio = ap->host_set->mmio_base;
  268. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  269. dma_addr_t mem_dma;
  270. rc = ata_port_start(ap);
  271. if (rc)
  272. return rc;
  273. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  274. if (!pp) {
  275. rc = -ENOMEM;
  276. goto err_out;
  277. }
  278. memset(pp, 0, sizeof(*pp));
  279. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  280. if (!mem) {
  281. rc = -ENOMEM;
  282. goto err_out_kfree;
  283. }
  284. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  285. /*
  286. * First item in chunk of DMA memory: 32-slot command table,
  287. * 32 bytes each in size
  288. */
  289. pp->cmd_slot = mem;
  290. pp->cmd_slot_dma = mem_dma;
  291. mem += AHCI_CMD_SLOT_SZ;
  292. mem_dma += AHCI_CMD_SLOT_SZ;
  293. /*
  294. * Second item: Received-FIS area
  295. */
  296. pp->rx_fis = mem;
  297. pp->rx_fis_dma = mem_dma;
  298. mem += AHCI_RX_FIS_SZ;
  299. mem_dma += AHCI_RX_FIS_SZ;
  300. /*
  301. * Third item: data area for storing a single command
  302. * and its scatter-gather table
  303. */
  304. pp->cmd_tbl = mem;
  305. pp->cmd_tbl_dma = mem_dma;
  306. pp->cmd_tbl_sg = mem + AHCI_CMD_TBL_HDR;
  307. ap->private_data = pp;
  308. if (hpriv->cap & HOST_CAP_64)
  309. writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  310. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  311. readl(port_mmio + PORT_LST_ADDR); /* flush */
  312. if (hpriv->cap & HOST_CAP_64)
  313. writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  314. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  315. readl(port_mmio + PORT_FIS_ADDR); /* flush */
  316. writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  317. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  318. PORT_CMD_START, port_mmio + PORT_CMD);
  319. readl(port_mmio + PORT_CMD); /* flush */
  320. return 0;
  321. err_out_kfree:
  322. kfree(pp);
  323. err_out:
  324. ata_port_stop(ap);
  325. return rc;
  326. }
  327. static void ahci_port_stop(struct ata_port *ap)
  328. {
  329. struct device *dev = ap->host_set->dev;
  330. struct ahci_port_priv *pp = ap->private_data;
  331. void *mmio = ap->host_set->mmio_base;
  332. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  333. u32 tmp;
  334. tmp = readl(port_mmio + PORT_CMD);
  335. tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX);
  336. writel(tmp, port_mmio + PORT_CMD);
  337. readl(port_mmio + PORT_CMD); /* flush */
  338. /* spec says 500 msecs for each PORT_CMD_{START,FIS_RX} bit, so
  339. * this is slightly incorrect.
  340. */
  341. msleep(500);
  342. ap->private_data = NULL;
  343. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  344. pp->cmd_slot, pp->cmd_slot_dma);
  345. kfree(pp);
  346. ata_port_stop(ap);
  347. }
  348. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  349. {
  350. unsigned int sc_reg;
  351. switch (sc_reg_in) {
  352. case SCR_STATUS: sc_reg = 0; break;
  353. case SCR_CONTROL: sc_reg = 1; break;
  354. case SCR_ERROR: sc_reg = 2; break;
  355. case SCR_ACTIVE: sc_reg = 3; break;
  356. default:
  357. return 0xffffffffU;
  358. }
  359. return readl((void *) ap->ioaddr.scr_addr + (sc_reg * 4));
  360. }
  361. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  362. u32 val)
  363. {
  364. unsigned int sc_reg;
  365. switch (sc_reg_in) {
  366. case SCR_STATUS: sc_reg = 0; break;
  367. case SCR_CONTROL: sc_reg = 1; break;
  368. case SCR_ERROR: sc_reg = 2; break;
  369. case SCR_ACTIVE: sc_reg = 3; break;
  370. default:
  371. return;
  372. }
  373. writel(val, (void *) ap->ioaddr.scr_addr + (sc_reg * 4));
  374. }
  375. static void ahci_phy_reset(struct ata_port *ap)
  376. {
  377. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  378. struct ata_taskfile tf;
  379. struct ata_device *dev = &ap->device[0];
  380. u32 tmp;
  381. __sata_phy_reset(ap);
  382. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  383. return;
  384. tmp = readl(port_mmio + PORT_SIG);
  385. tf.lbah = (tmp >> 24) & 0xff;
  386. tf.lbam = (tmp >> 16) & 0xff;
  387. tf.lbal = (tmp >> 8) & 0xff;
  388. tf.nsect = (tmp) & 0xff;
  389. dev->class = ata_dev_classify(&tf);
  390. if (!ata_dev_present(dev))
  391. ata_port_disable(ap);
  392. }
  393. static u8 ahci_check_status(struct ata_port *ap)
  394. {
  395. void *mmio = (void *) ap->ioaddr.cmd_addr;
  396. return readl(mmio + PORT_TFDATA) & 0xFF;
  397. }
  398. static u8 ahci_check_err(struct ata_port *ap)
  399. {
  400. void *mmio = (void *) ap->ioaddr.cmd_addr;
  401. return (readl(mmio + PORT_TFDATA) >> 8) & 0xFF;
  402. }
  403. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  404. {
  405. struct ahci_port_priv *pp = ap->private_data;
  406. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  407. ata_tf_from_fis(d2h_fis, tf);
  408. }
  409. static void ahci_fill_sg(struct ata_queued_cmd *qc)
  410. {
  411. struct ahci_port_priv *pp = qc->ap->private_data;
  412. unsigned int i;
  413. VPRINTK("ENTER\n");
  414. /*
  415. * Next, the S/G list.
  416. */
  417. for (i = 0; i < qc->n_elem; i++) {
  418. u32 sg_len;
  419. dma_addr_t addr;
  420. addr = sg_dma_address(&qc->sg[i]);
  421. sg_len = sg_dma_len(&qc->sg[i]);
  422. pp->cmd_tbl_sg[i].addr = cpu_to_le32(addr & 0xffffffff);
  423. pp->cmd_tbl_sg[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  424. pp->cmd_tbl_sg[i].flags_size = cpu_to_le32(sg_len - 1);
  425. }
  426. }
  427. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  428. {
  429. struct ata_port *ap = qc->ap;
  430. struct ahci_port_priv *pp = ap->private_data;
  431. u32 opts;
  432. const u32 cmd_fis_len = 5; /* five dwords */
  433. /*
  434. * Fill in command slot information (currently only one slot,
  435. * slot 0, is currently since we don't do queueing)
  436. */
  437. opts = (qc->n_elem << 16) | cmd_fis_len;
  438. if (qc->tf.flags & ATA_TFLAG_WRITE)
  439. opts |= AHCI_CMD_WRITE;
  440. if (is_atapi_taskfile(&qc->tf))
  441. opts |= AHCI_CMD_ATAPI;
  442. pp->cmd_slot[0].opts = cpu_to_le32(opts);
  443. pp->cmd_slot[0].status = 0;
  444. pp->cmd_slot[0].tbl_addr = cpu_to_le32(pp->cmd_tbl_dma & 0xffffffff);
  445. pp->cmd_slot[0].tbl_addr_hi = cpu_to_le32((pp->cmd_tbl_dma >> 16) >> 16);
  446. /*
  447. * Fill in command table information. First, the header,
  448. * a SATA Register - Host to Device command FIS.
  449. */
  450. ata_tf_to_fis(&qc->tf, pp->cmd_tbl, 0);
  451. if (opts & AHCI_CMD_ATAPI) {
  452. memset(pp->cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  453. memcpy(pp->cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, ap->cdb_len);
  454. }
  455. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  456. return;
  457. ahci_fill_sg(qc);
  458. }
  459. static void ahci_intr_error(struct ata_port *ap, u32 irq_stat)
  460. {
  461. void *mmio = ap->host_set->mmio_base;
  462. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  463. u32 tmp;
  464. int work;
  465. /* stop DMA */
  466. tmp = readl(port_mmio + PORT_CMD);
  467. tmp &= ~PORT_CMD_START;
  468. writel(tmp, port_mmio + PORT_CMD);
  469. /* wait for engine to stop. TODO: this could be
  470. * as long as 500 msec
  471. */
  472. work = 1000;
  473. while (work-- > 0) {
  474. tmp = readl(port_mmio + PORT_CMD);
  475. if ((tmp & PORT_CMD_LIST_ON) == 0)
  476. break;
  477. udelay(10);
  478. }
  479. /* clear SATA phy error, if any */
  480. tmp = readl(port_mmio + PORT_SCR_ERR);
  481. writel(tmp, port_mmio + PORT_SCR_ERR);
  482. /* if DRQ/BSY is set, device needs to be reset.
  483. * if so, issue COMRESET
  484. */
  485. tmp = readl(port_mmio + PORT_TFDATA);
  486. if (tmp & (ATA_BUSY | ATA_DRQ)) {
  487. writel(0x301, port_mmio + PORT_SCR_CTL);
  488. readl(port_mmio + PORT_SCR_CTL); /* flush */
  489. udelay(10);
  490. writel(0x300, port_mmio + PORT_SCR_CTL);
  491. readl(port_mmio + PORT_SCR_CTL); /* flush */
  492. }
  493. /* re-start DMA */
  494. tmp = readl(port_mmio + PORT_CMD);
  495. tmp |= PORT_CMD_START;
  496. writel(tmp, port_mmio + PORT_CMD);
  497. readl(port_mmio + PORT_CMD); /* flush */
  498. printk(KERN_WARNING "ata%u: error occurred, port reset\n", ap->id);
  499. }
  500. static void ahci_eng_timeout(struct ata_port *ap)
  501. {
  502. void *mmio = ap->host_set->mmio_base;
  503. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  504. struct ata_queued_cmd *qc;
  505. DPRINTK("ENTER\n");
  506. ahci_intr_error(ap, readl(port_mmio + PORT_IRQ_STAT));
  507. qc = ata_qc_from_tag(ap, ap->active_tag);
  508. if (!qc) {
  509. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  510. ap->id);
  511. } else {
  512. /* hack alert! We cannot use the supplied completion
  513. * function from inside the ->eh_strategy_handler() thread.
  514. * libata is the only user of ->eh_strategy_handler() in
  515. * any kernel, so the default scsi_done() assumes it is
  516. * not being called from the SCSI EH.
  517. */
  518. qc->scsidone = scsi_finish_command;
  519. ata_qc_complete(qc, ATA_ERR);
  520. }
  521. }
  522. static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
  523. {
  524. void *mmio = ap->host_set->mmio_base;
  525. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  526. u32 status, serr, ci;
  527. serr = readl(port_mmio + PORT_SCR_ERR);
  528. writel(serr, port_mmio + PORT_SCR_ERR);
  529. status = readl(port_mmio + PORT_IRQ_STAT);
  530. writel(status, port_mmio + PORT_IRQ_STAT);
  531. ci = readl(port_mmio + PORT_CMD_ISSUE);
  532. if (likely((ci & 0x1) == 0)) {
  533. if (qc) {
  534. ata_qc_complete(qc, 0);
  535. qc = NULL;
  536. }
  537. }
  538. if (status & PORT_IRQ_FATAL) {
  539. ahci_intr_error(ap, status);
  540. if (qc)
  541. ata_qc_complete(qc, ATA_ERR);
  542. }
  543. return 1;
  544. }
  545. static void ahci_irq_clear(struct ata_port *ap)
  546. {
  547. /* TODO */
  548. }
  549. static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  550. {
  551. struct ata_host_set *host_set = dev_instance;
  552. struct ahci_host_priv *hpriv;
  553. unsigned int i, handled = 0;
  554. void *mmio;
  555. u32 irq_stat, irq_ack = 0;
  556. VPRINTK("ENTER\n");
  557. hpriv = host_set->private_data;
  558. mmio = host_set->mmio_base;
  559. /* sigh. 0xffffffff is a valid return from h/w */
  560. irq_stat = readl(mmio + HOST_IRQ_STAT);
  561. irq_stat &= hpriv->port_map;
  562. if (!irq_stat)
  563. return IRQ_NONE;
  564. spin_lock(&host_set->lock);
  565. for (i = 0; i < host_set->n_ports; i++) {
  566. struct ata_port *ap;
  567. u32 tmp;
  568. VPRINTK("port %u\n", i);
  569. ap = host_set->ports[i];
  570. tmp = irq_stat & (1 << i);
  571. if (tmp && ap) {
  572. struct ata_queued_cmd *qc;
  573. qc = ata_qc_from_tag(ap, ap->active_tag);
  574. if (ahci_host_intr(ap, qc))
  575. irq_ack |= (1 << i);
  576. }
  577. }
  578. if (irq_ack) {
  579. writel(irq_ack, mmio + HOST_IRQ_STAT);
  580. handled = 1;
  581. }
  582. spin_unlock(&host_set->lock);
  583. VPRINTK("EXIT\n");
  584. return IRQ_RETVAL(handled);
  585. }
  586. static int ahci_qc_issue(struct ata_queued_cmd *qc)
  587. {
  588. struct ata_port *ap = qc->ap;
  589. void *port_mmio = (void *) ap->ioaddr.cmd_addr;
  590. writel(1, port_mmio + PORT_SCR_ACT);
  591. readl(port_mmio + PORT_SCR_ACT); /* flush */
  592. writel(1, port_mmio + PORT_CMD_ISSUE);
  593. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  594. return 0;
  595. }
  596. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  597. unsigned int port_idx)
  598. {
  599. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  600. base = ahci_port_base_ul(base, port_idx);
  601. VPRINTK("base now==0x%lx\n", base);
  602. port->cmd_addr = base;
  603. port->scr_addr = base + PORT_SCR;
  604. VPRINTK("EXIT\n");
  605. }
  606. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  607. {
  608. struct ahci_host_priv *hpriv = probe_ent->private_data;
  609. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  610. void __iomem *mmio = probe_ent->mmio_base;
  611. u32 tmp, cap_save;
  612. u16 tmp16;
  613. unsigned int i, j, using_dac;
  614. int rc;
  615. void __iomem *port_mmio;
  616. cap_save = readl(mmio + HOST_CAP);
  617. cap_save &= ( (1<<28) | (1<<17) );
  618. cap_save |= (1 << 27);
  619. /* global controller reset */
  620. tmp = readl(mmio + HOST_CTL);
  621. if ((tmp & HOST_RESET) == 0) {
  622. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  623. readl(mmio + HOST_CTL); /* flush */
  624. }
  625. /* reset must complete within 1 second, or
  626. * the hardware should be considered fried.
  627. */
  628. ssleep(1);
  629. tmp = readl(mmio + HOST_CTL);
  630. if (tmp & HOST_RESET) {
  631. printk(KERN_ERR DRV_NAME "(%s): controller reset failed (0x%x)\n",
  632. pci_name(pdev), tmp);
  633. return -EIO;
  634. }
  635. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  636. (void) readl(mmio + HOST_CTL); /* flush */
  637. writel(cap_save, mmio + HOST_CAP);
  638. writel(0xf, mmio + HOST_PORTS_IMPL);
  639. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  640. pci_read_config_word(pdev, 0x92, &tmp16);
  641. tmp16 |= 0xf;
  642. pci_write_config_word(pdev, 0x92, tmp16);
  643. hpriv->cap = readl(mmio + HOST_CAP);
  644. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  645. probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
  646. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  647. hpriv->cap, hpriv->port_map, probe_ent->n_ports);
  648. using_dac = hpriv->cap & HOST_CAP_64;
  649. if (using_dac &&
  650. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  651. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  652. if (rc) {
  653. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  654. if (rc) {
  655. printk(KERN_ERR DRV_NAME "(%s): 64-bit DMA enable failed\n",
  656. pci_name(pdev));
  657. return rc;
  658. }
  659. }
  660. } else {
  661. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  662. if (rc) {
  663. printk(KERN_ERR DRV_NAME "(%s): 32-bit DMA enable failed\n",
  664. pci_name(pdev));
  665. return rc;
  666. }
  667. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  668. if (rc) {
  669. printk(KERN_ERR DRV_NAME "(%s): 32-bit consistent DMA enable failed\n",
  670. pci_name(pdev));
  671. return rc;
  672. }
  673. }
  674. for (i = 0; i < probe_ent->n_ports; i++) {
  675. #if 0 /* BIOSen initialize this incorrectly */
  676. if (!(hpriv->port_map & (1 << i)))
  677. continue;
  678. #endif
  679. port_mmio = ahci_port_base(mmio, i);
  680. VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
  681. ahci_setup_port(&probe_ent->port[i],
  682. (unsigned long) mmio, i);
  683. /* make sure port is not active */
  684. tmp = readl(port_mmio + PORT_CMD);
  685. VPRINTK("PORT_CMD 0x%x\n", tmp);
  686. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  687. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  688. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  689. PORT_CMD_FIS_RX | PORT_CMD_START);
  690. writel(tmp, port_mmio + PORT_CMD);
  691. readl(port_mmio + PORT_CMD); /* flush */
  692. /* spec says 500 msecs for each bit, so
  693. * this is slightly incorrect.
  694. */
  695. msleep(500);
  696. }
  697. writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
  698. j = 0;
  699. while (j < 100) {
  700. msleep(10);
  701. tmp = readl(port_mmio + PORT_SCR_STAT);
  702. if ((tmp & 0xf) == 0x3)
  703. break;
  704. j++;
  705. }
  706. tmp = readl(port_mmio + PORT_SCR_ERR);
  707. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  708. writel(tmp, port_mmio + PORT_SCR_ERR);
  709. /* ack any pending irq events for this port */
  710. tmp = readl(port_mmio + PORT_IRQ_STAT);
  711. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  712. if (tmp)
  713. writel(tmp, port_mmio + PORT_IRQ_STAT);
  714. writel(1 << i, mmio + HOST_IRQ_STAT);
  715. /* set irq mask (enables interrupts) */
  716. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  717. }
  718. tmp = readl(mmio + HOST_CTL);
  719. VPRINTK("HOST_CTL 0x%x\n", tmp);
  720. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  721. tmp = readl(mmio + HOST_CTL);
  722. VPRINTK("HOST_CTL 0x%x\n", tmp);
  723. pci_set_master(pdev);
  724. return 0;
  725. }
  726. /* move to PCI layer, integrate w/ MSI stuff */
  727. static void pci_intx(struct pci_dev *pdev, int enable)
  728. {
  729. u16 pci_command, new;
  730. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  731. if (enable)
  732. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  733. else
  734. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  735. if (new != pci_command)
  736. pci_write_config_word(pdev, PCI_COMMAND, pci_command);
  737. }
  738. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  739. {
  740. struct ahci_host_priv *hpriv = probe_ent->private_data;
  741. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  742. void *mmio = probe_ent->mmio_base;
  743. u32 vers, cap, impl, speed;
  744. const char *speed_s;
  745. u16 cc;
  746. const char *scc_s;
  747. vers = readl(mmio + HOST_VERSION);
  748. cap = hpriv->cap;
  749. impl = hpriv->port_map;
  750. speed = (cap >> 20) & 0xf;
  751. if (speed == 1)
  752. speed_s = "1.5";
  753. else if (speed == 2)
  754. speed_s = "3";
  755. else
  756. speed_s = "?";
  757. pci_read_config_word(pdev, 0x0a, &cc);
  758. if (cc == 0x0101)
  759. scc_s = "IDE";
  760. else if (cc == 0x0106)
  761. scc_s = "SATA";
  762. else if (cc == 0x0104)
  763. scc_s = "RAID";
  764. else
  765. scc_s = "unknown";
  766. printk(KERN_INFO DRV_NAME "(%s) AHCI %02x%02x.%02x%02x "
  767. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  768. ,
  769. pci_name(pdev),
  770. (vers >> 24) & 0xff,
  771. (vers >> 16) & 0xff,
  772. (vers >> 8) & 0xff,
  773. vers & 0xff,
  774. ((cap >> 8) & 0x1f) + 1,
  775. (cap & 0x1f) + 1,
  776. speed_s,
  777. impl,
  778. scc_s);
  779. printk(KERN_INFO DRV_NAME "(%s) flags: "
  780. "%s%s%s%s%s%s"
  781. "%s%s%s%s%s%s%s\n"
  782. ,
  783. pci_name(pdev),
  784. cap & (1 << 31) ? "64bit " : "",
  785. cap & (1 << 30) ? "ncq " : "",
  786. cap & (1 << 28) ? "ilck " : "",
  787. cap & (1 << 27) ? "stag " : "",
  788. cap & (1 << 26) ? "pm " : "",
  789. cap & (1 << 25) ? "led " : "",
  790. cap & (1 << 24) ? "clo " : "",
  791. cap & (1 << 19) ? "nz " : "",
  792. cap & (1 << 18) ? "only " : "",
  793. cap & (1 << 17) ? "pmp " : "",
  794. cap & (1 << 15) ? "pio " : "",
  795. cap & (1 << 14) ? "slum " : "",
  796. cap & (1 << 13) ? "part " : ""
  797. );
  798. }
  799. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  800. {
  801. static int printed_version;
  802. struct ata_probe_ent *probe_ent = NULL;
  803. struct ahci_host_priv *hpriv;
  804. unsigned long base;
  805. void *mmio_base;
  806. unsigned int board_idx = (unsigned int) ent->driver_data;
  807. int have_msi, pci_dev_busy = 0;
  808. int rc;
  809. VPRINTK("ENTER\n");
  810. if (!printed_version++)
  811. printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
  812. rc = pci_enable_device(pdev);
  813. if (rc)
  814. return rc;
  815. rc = pci_request_regions(pdev, DRV_NAME);
  816. if (rc) {
  817. pci_dev_busy = 1;
  818. goto err_out;
  819. }
  820. if (pci_enable_msi(pdev) == 0)
  821. have_msi = 1;
  822. else {
  823. pci_intx(pdev, 1);
  824. have_msi = 0;
  825. }
  826. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  827. if (probe_ent == NULL) {
  828. rc = -ENOMEM;
  829. goto err_out_msi;
  830. }
  831. memset(probe_ent, 0, sizeof(*probe_ent));
  832. probe_ent->dev = pci_dev_to_dev(pdev);
  833. INIT_LIST_HEAD(&probe_ent->node);
  834. mmio_base = ioremap(pci_resource_start(pdev, AHCI_PCI_BAR),
  835. pci_resource_len(pdev, AHCI_PCI_BAR));
  836. if (mmio_base == NULL) {
  837. rc = -ENOMEM;
  838. goto err_out_free_ent;
  839. }
  840. base = (unsigned long) mmio_base;
  841. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  842. if (!hpriv) {
  843. rc = -ENOMEM;
  844. goto err_out_iounmap;
  845. }
  846. memset(hpriv, 0, sizeof(*hpriv));
  847. probe_ent->sht = ahci_port_info[board_idx].sht;
  848. probe_ent->host_flags = ahci_port_info[board_idx].host_flags;
  849. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  850. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  851. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  852. probe_ent->irq = pdev->irq;
  853. probe_ent->irq_flags = SA_SHIRQ;
  854. probe_ent->mmio_base = mmio_base;
  855. probe_ent->private_data = hpriv;
  856. if (have_msi)
  857. hpriv->flags |= AHCI_FLAG_MSI;
  858. /* initialize adapter */
  859. rc = ahci_host_init(probe_ent);
  860. if (rc)
  861. goto err_out_hpriv;
  862. ahci_print_info(probe_ent);
  863. /* FIXME: check ata_device_add return value */
  864. ata_device_add(probe_ent);
  865. kfree(probe_ent);
  866. return 0;
  867. err_out_hpriv:
  868. kfree(hpriv);
  869. err_out_iounmap:
  870. iounmap(mmio_base);
  871. err_out_free_ent:
  872. kfree(probe_ent);
  873. err_out_msi:
  874. if (have_msi)
  875. pci_disable_msi(pdev);
  876. else
  877. pci_intx(pdev, 0);
  878. pci_release_regions(pdev);
  879. err_out:
  880. if (!pci_dev_busy)
  881. pci_disable_device(pdev);
  882. return rc;
  883. }
  884. static void ahci_remove_one (struct pci_dev *pdev)
  885. {
  886. struct device *dev = pci_dev_to_dev(pdev);
  887. struct ata_host_set *host_set = dev_get_drvdata(dev);
  888. struct ahci_host_priv *hpriv = host_set->private_data;
  889. struct ata_port *ap;
  890. unsigned int i;
  891. int have_msi;
  892. for (i = 0; i < host_set->n_ports; i++) {
  893. ap = host_set->ports[i];
  894. scsi_remove_host(ap->host);
  895. }
  896. have_msi = hpriv->flags & AHCI_FLAG_MSI;
  897. free_irq(host_set->irq, host_set);
  898. for (i = 0; i < host_set->n_ports; i++) {
  899. ap = host_set->ports[i];
  900. ata_scsi_release(ap->host);
  901. scsi_host_put(ap->host);
  902. }
  903. host_set->ops->host_stop(host_set);
  904. kfree(host_set);
  905. if (have_msi)
  906. pci_disable_msi(pdev);
  907. else
  908. pci_intx(pdev, 0);
  909. pci_release_regions(pdev);
  910. pci_disable_device(pdev);
  911. dev_set_drvdata(dev, NULL);
  912. }
  913. static int __init ahci_init(void)
  914. {
  915. return pci_module_init(&ahci_pci_driver);
  916. }
  917. static void __exit ahci_exit(void)
  918. {
  919. pci_unregister_driver(&ahci_pci_driver);
  920. }
  921. MODULE_AUTHOR("Jeff Garzik");
  922. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  923. MODULE_LICENSE("GPL");
  924. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  925. module_init(ahci_init);
  926. module_exit(ahci_exit);