ahci.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. void *mem, *mmio = ap->host_set->mmio_base;
  267. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  268. dma_addr_t mem_dma;
  269. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  270. if (!pp)
  271. return -ENOMEM;
  272. memset(pp, 0, sizeof(*pp));
  273. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  274. if (!mem) {
  275. kfree(pp);
  276. return -ENOMEM;
  277. }
  278. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  279. /*
  280. * First item in chunk of DMA memory: 32-slot command table,
  281. * 32 bytes each in size
  282. */
  283. pp->cmd_slot = mem;
  284. pp->cmd_slot_dma = mem_dma;
  285. mem += AHCI_CMD_SLOT_SZ;
  286. mem_dma += AHCI_CMD_SLOT_SZ;
  287. /*
  288. * Second item: Received-FIS area
  289. */
  290. pp->rx_fis = mem;
  291. pp->rx_fis_dma = mem_dma;
  292. mem += AHCI_RX_FIS_SZ;
  293. mem_dma += AHCI_RX_FIS_SZ;
  294. /*
  295. * Third item: data area for storing a single command
  296. * and its scatter-gather table
  297. */
  298. pp->cmd_tbl = mem;
  299. pp->cmd_tbl_dma = mem_dma;
  300. pp->cmd_tbl_sg = mem + AHCI_CMD_TBL_HDR;
  301. ap->private_data = pp;
  302. if (hpriv->cap & HOST_CAP_64)
  303. writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  304. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  305. readl(port_mmio + PORT_LST_ADDR); /* flush */
  306. if (hpriv->cap & HOST_CAP_64)
  307. writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  308. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  309. readl(port_mmio + PORT_FIS_ADDR); /* flush */
  310. writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  311. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  312. PORT_CMD_START, port_mmio + PORT_CMD);
  313. readl(port_mmio + PORT_CMD); /* flush */
  314. return 0;
  315. }
  316. static void ahci_port_stop(struct ata_port *ap)
  317. {
  318. struct device *dev = ap->host_set->dev;
  319. struct ahci_port_priv *pp = ap->private_data;
  320. void *mmio = ap->host_set->mmio_base;
  321. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  322. u32 tmp;
  323. tmp = readl(port_mmio + PORT_CMD);
  324. tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX);
  325. writel(tmp, port_mmio + PORT_CMD);
  326. readl(port_mmio + PORT_CMD); /* flush */
  327. /* spec says 500 msecs for each PORT_CMD_{START,FIS_RX} bit, so
  328. * this is slightly incorrect.
  329. */
  330. msleep(500);
  331. ap->private_data = NULL;
  332. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  333. pp->cmd_slot, pp->cmd_slot_dma);
  334. kfree(pp);
  335. }
  336. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  337. {
  338. unsigned int sc_reg;
  339. switch (sc_reg_in) {
  340. case SCR_STATUS: sc_reg = 0; break;
  341. case SCR_CONTROL: sc_reg = 1; break;
  342. case SCR_ERROR: sc_reg = 2; break;
  343. case SCR_ACTIVE: sc_reg = 3; break;
  344. default:
  345. return 0xffffffffU;
  346. }
  347. return readl((void *) ap->ioaddr.scr_addr + (sc_reg * 4));
  348. }
  349. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  350. u32 val)
  351. {
  352. unsigned int sc_reg;
  353. switch (sc_reg_in) {
  354. case SCR_STATUS: sc_reg = 0; break;
  355. case SCR_CONTROL: sc_reg = 1; break;
  356. case SCR_ERROR: sc_reg = 2; break;
  357. case SCR_ACTIVE: sc_reg = 3; break;
  358. default:
  359. return;
  360. }
  361. writel(val, (void *) ap->ioaddr.scr_addr + (sc_reg * 4));
  362. }
  363. static void ahci_phy_reset(struct ata_port *ap)
  364. {
  365. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  366. struct ata_taskfile tf;
  367. struct ata_device *dev = &ap->device[0];
  368. u32 tmp;
  369. __sata_phy_reset(ap);
  370. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  371. return;
  372. tmp = readl(port_mmio + PORT_SIG);
  373. tf.lbah = (tmp >> 24) & 0xff;
  374. tf.lbam = (tmp >> 16) & 0xff;
  375. tf.lbal = (tmp >> 8) & 0xff;
  376. tf.nsect = (tmp) & 0xff;
  377. dev->class = ata_dev_classify(&tf);
  378. if (!ata_dev_present(dev))
  379. ata_port_disable(ap);
  380. }
  381. static u8 ahci_check_status(struct ata_port *ap)
  382. {
  383. void *mmio = (void *) ap->ioaddr.cmd_addr;
  384. return readl(mmio + PORT_TFDATA) & 0xFF;
  385. }
  386. static u8 ahci_check_err(struct ata_port *ap)
  387. {
  388. void *mmio = (void *) ap->ioaddr.cmd_addr;
  389. return (readl(mmio + PORT_TFDATA) >> 8) & 0xFF;
  390. }
  391. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  392. {
  393. struct ahci_port_priv *pp = ap->private_data;
  394. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  395. ata_tf_from_fis(d2h_fis, tf);
  396. }
  397. static void ahci_fill_sg(struct ata_queued_cmd *qc)
  398. {
  399. struct ahci_port_priv *pp = qc->ap->private_data;
  400. unsigned int i;
  401. VPRINTK("ENTER\n");
  402. /*
  403. * Next, the S/G list.
  404. */
  405. for (i = 0; i < qc->n_elem; i++) {
  406. u32 sg_len;
  407. dma_addr_t addr;
  408. addr = sg_dma_address(&qc->sg[i]);
  409. sg_len = sg_dma_len(&qc->sg[i]);
  410. pp->cmd_tbl_sg[i].addr = cpu_to_le32(addr & 0xffffffff);
  411. pp->cmd_tbl_sg[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  412. pp->cmd_tbl_sg[i].flags_size = cpu_to_le32(sg_len - 1);
  413. }
  414. }
  415. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  416. {
  417. struct ata_port *ap = qc->ap;
  418. struct ahci_port_priv *pp = ap->private_data;
  419. u32 opts;
  420. const u32 cmd_fis_len = 5; /* five dwords */
  421. /*
  422. * Fill in command slot information (currently only one slot,
  423. * slot 0, is currently since we don't do queueing)
  424. */
  425. opts = (qc->n_elem << 16) | cmd_fis_len;
  426. if (qc->tf.flags & ATA_TFLAG_WRITE)
  427. opts |= AHCI_CMD_WRITE;
  428. if (is_atapi_taskfile(&qc->tf))
  429. opts |= AHCI_CMD_ATAPI;
  430. pp->cmd_slot[0].opts = cpu_to_le32(opts);
  431. pp->cmd_slot[0].status = 0;
  432. pp->cmd_slot[0].tbl_addr = cpu_to_le32(pp->cmd_tbl_dma & 0xffffffff);
  433. pp->cmd_slot[0].tbl_addr_hi = cpu_to_le32((pp->cmd_tbl_dma >> 16) >> 16);
  434. /*
  435. * Fill in command table information. First, the header,
  436. * a SATA Register - Host to Device command FIS.
  437. */
  438. ata_tf_to_fis(&qc->tf, pp->cmd_tbl, 0);
  439. if (opts & AHCI_CMD_ATAPI) {
  440. memset(pp->cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  441. memcpy(pp->cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, ap->cdb_len);
  442. }
  443. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  444. return;
  445. ahci_fill_sg(qc);
  446. }
  447. static void ahci_intr_error(struct ata_port *ap, u32 irq_stat)
  448. {
  449. void *mmio = ap->host_set->mmio_base;
  450. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  451. u32 tmp;
  452. int work;
  453. /* stop DMA */
  454. tmp = readl(port_mmio + PORT_CMD);
  455. tmp &= ~PORT_CMD_START;
  456. writel(tmp, port_mmio + PORT_CMD);
  457. /* wait for engine to stop. TODO: this could be
  458. * as long as 500 msec
  459. */
  460. work = 1000;
  461. while (work-- > 0) {
  462. tmp = readl(port_mmio + PORT_CMD);
  463. if ((tmp & PORT_CMD_LIST_ON) == 0)
  464. break;
  465. udelay(10);
  466. }
  467. /* clear SATA phy error, if any */
  468. tmp = readl(port_mmio + PORT_SCR_ERR);
  469. writel(tmp, port_mmio + PORT_SCR_ERR);
  470. /* if DRQ/BSY is set, device needs to be reset.
  471. * if so, issue COMRESET
  472. */
  473. tmp = readl(port_mmio + PORT_TFDATA);
  474. if (tmp & (ATA_BUSY | ATA_DRQ)) {
  475. writel(0x301, port_mmio + PORT_SCR_CTL);
  476. readl(port_mmio + PORT_SCR_CTL); /* flush */
  477. udelay(10);
  478. writel(0x300, port_mmio + PORT_SCR_CTL);
  479. readl(port_mmio + PORT_SCR_CTL); /* flush */
  480. }
  481. /* re-start DMA */
  482. tmp = readl(port_mmio + PORT_CMD);
  483. tmp |= PORT_CMD_START;
  484. writel(tmp, port_mmio + PORT_CMD);
  485. readl(port_mmio + PORT_CMD); /* flush */
  486. printk(KERN_WARNING "ata%u: error occurred, port reset\n", ap->id);
  487. }
  488. static void ahci_eng_timeout(struct ata_port *ap)
  489. {
  490. void *mmio = ap->host_set->mmio_base;
  491. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  492. struct ata_queued_cmd *qc;
  493. DPRINTK("ENTER\n");
  494. ahci_intr_error(ap, readl(port_mmio + PORT_IRQ_STAT));
  495. qc = ata_qc_from_tag(ap, ap->active_tag);
  496. if (!qc) {
  497. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  498. ap->id);
  499. } else {
  500. /* hack alert! We cannot use the supplied completion
  501. * function from inside the ->eh_strategy_handler() thread.
  502. * libata is the only user of ->eh_strategy_handler() in
  503. * any kernel, so the default scsi_done() assumes it is
  504. * not being called from the SCSI EH.
  505. */
  506. qc->scsidone = scsi_finish_command;
  507. ata_qc_complete(qc, ATA_ERR);
  508. }
  509. }
  510. static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
  511. {
  512. void *mmio = ap->host_set->mmio_base;
  513. void *port_mmio = ahci_port_base(mmio, ap->port_no);
  514. u32 status, serr, ci;
  515. serr = readl(port_mmio + PORT_SCR_ERR);
  516. writel(serr, port_mmio + PORT_SCR_ERR);
  517. status = readl(port_mmio + PORT_IRQ_STAT);
  518. writel(status, port_mmio + PORT_IRQ_STAT);
  519. ci = readl(port_mmio + PORT_CMD_ISSUE);
  520. if (likely((ci & 0x1) == 0)) {
  521. if (qc) {
  522. ata_qc_complete(qc, 0);
  523. qc = NULL;
  524. }
  525. }
  526. if (status & PORT_IRQ_FATAL) {
  527. ahci_intr_error(ap, status);
  528. if (qc)
  529. ata_qc_complete(qc, ATA_ERR);
  530. }
  531. return 1;
  532. }
  533. static void ahci_irq_clear(struct ata_port *ap)
  534. {
  535. /* TODO */
  536. }
  537. static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  538. {
  539. struct ata_host_set *host_set = dev_instance;
  540. struct ahci_host_priv *hpriv;
  541. unsigned int i, handled = 0;
  542. void *mmio;
  543. u32 irq_stat, irq_ack = 0;
  544. VPRINTK("ENTER\n");
  545. hpriv = host_set->private_data;
  546. mmio = host_set->mmio_base;
  547. /* sigh. 0xffffffff is a valid return from h/w */
  548. irq_stat = readl(mmio + HOST_IRQ_STAT);
  549. irq_stat &= hpriv->port_map;
  550. if (!irq_stat)
  551. return IRQ_NONE;
  552. spin_lock(&host_set->lock);
  553. for (i = 0; i < host_set->n_ports; i++) {
  554. struct ata_port *ap;
  555. u32 tmp;
  556. VPRINTK("port %u\n", i);
  557. ap = host_set->ports[i];
  558. tmp = irq_stat & (1 << i);
  559. if (tmp && ap) {
  560. struct ata_queued_cmd *qc;
  561. qc = ata_qc_from_tag(ap, ap->active_tag);
  562. if (ahci_host_intr(ap, qc))
  563. irq_ack |= (1 << i);
  564. }
  565. }
  566. if (irq_ack) {
  567. writel(irq_ack, mmio + HOST_IRQ_STAT);
  568. handled = 1;
  569. }
  570. spin_unlock(&host_set->lock);
  571. VPRINTK("EXIT\n");
  572. return IRQ_RETVAL(handled);
  573. }
  574. static int ahci_qc_issue(struct ata_queued_cmd *qc)
  575. {
  576. struct ata_port *ap = qc->ap;
  577. void *port_mmio = (void *) ap->ioaddr.cmd_addr;
  578. writel(1, port_mmio + PORT_SCR_ACT);
  579. readl(port_mmio + PORT_SCR_ACT); /* flush */
  580. writel(1, port_mmio + PORT_CMD_ISSUE);
  581. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  582. return 0;
  583. }
  584. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  585. unsigned int port_idx)
  586. {
  587. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  588. base = ahci_port_base_ul(base, port_idx);
  589. VPRINTK("base now==0x%lx\n", base);
  590. port->cmd_addr = base;
  591. port->scr_addr = base + PORT_SCR;
  592. VPRINTK("EXIT\n");
  593. }
  594. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  595. {
  596. struct ahci_host_priv *hpriv = probe_ent->private_data;
  597. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  598. void __iomem *mmio = probe_ent->mmio_base;
  599. u32 tmp, cap_save;
  600. u16 tmp16;
  601. unsigned int i, j, using_dac;
  602. int rc;
  603. void __iomem *port_mmio;
  604. cap_save = readl(mmio + HOST_CAP);
  605. cap_save &= ( (1<<28) | (1<<17) );
  606. cap_save |= (1 << 27);
  607. /* global controller reset */
  608. tmp = readl(mmio + HOST_CTL);
  609. if ((tmp & HOST_RESET) == 0) {
  610. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  611. readl(mmio + HOST_CTL); /* flush */
  612. }
  613. /* reset must complete within 1 second, or
  614. * the hardware should be considered fried.
  615. */
  616. ssleep(1);
  617. tmp = readl(mmio + HOST_CTL);
  618. if (tmp & HOST_RESET) {
  619. printk(KERN_ERR DRV_NAME "(%s): controller reset failed (0x%x)\n",
  620. pci_name(pdev), tmp);
  621. return -EIO;
  622. }
  623. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  624. (void) readl(mmio + HOST_CTL); /* flush */
  625. writel(cap_save, mmio + HOST_CAP);
  626. writel(0xf, mmio + HOST_PORTS_IMPL);
  627. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  628. pci_read_config_word(pdev, 0x92, &tmp16);
  629. tmp16 |= 0xf;
  630. pci_write_config_word(pdev, 0x92, tmp16);
  631. hpriv->cap = readl(mmio + HOST_CAP);
  632. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  633. probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
  634. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  635. hpriv->cap, hpriv->port_map, probe_ent->n_ports);
  636. using_dac = hpriv->cap & HOST_CAP_64;
  637. if (using_dac &&
  638. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  639. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  640. if (rc) {
  641. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  642. if (rc) {
  643. printk(KERN_ERR DRV_NAME "(%s): 64-bit DMA enable failed\n",
  644. pci_name(pdev));
  645. return rc;
  646. }
  647. }
  648. } else {
  649. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  650. if (rc) {
  651. printk(KERN_ERR DRV_NAME "(%s): 32-bit DMA enable failed\n",
  652. pci_name(pdev));
  653. return rc;
  654. }
  655. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  656. if (rc) {
  657. printk(KERN_ERR DRV_NAME "(%s): 32-bit consistent DMA enable failed\n",
  658. pci_name(pdev));
  659. return rc;
  660. }
  661. }
  662. for (i = 0; i < probe_ent->n_ports; i++) {
  663. #if 0 /* BIOSen initialize this incorrectly */
  664. if (!(hpriv->port_map & (1 << i)))
  665. continue;
  666. #endif
  667. port_mmio = ahci_port_base(mmio, i);
  668. VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
  669. ahci_setup_port(&probe_ent->port[i],
  670. (unsigned long) mmio, i);
  671. /* make sure port is not active */
  672. tmp = readl(port_mmio + PORT_CMD);
  673. VPRINTK("PORT_CMD 0x%x\n", tmp);
  674. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  675. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  676. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  677. PORT_CMD_FIS_RX | PORT_CMD_START);
  678. writel(tmp, port_mmio + PORT_CMD);
  679. readl(port_mmio + PORT_CMD); /* flush */
  680. /* spec says 500 msecs for each bit, so
  681. * this is slightly incorrect.
  682. */
  683. msleep(500);
  684. }
  685. writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
  686. j = 0;
  687. while (j < 100) {
  688. msleep(10);
  689. tmp = readl(port_mmio + PORT_SCR_STAT);
  690. if ((tmp & 0xf) == 0x3)
  691. break;
  692. j++;
  693. }
  694. tmp = readl(port_mmio + PORT_SCR_ERR);
  695. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  696. writel(tmp, port_mmio + PORT_SCR_ERR);
  697. /* ack any pending irq events for this port */
  698. tmp = readl(port_mmio + PORT_IRQ_STAT);
  699. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  700. if (tmp)
  701. writel(tmp, port_mmio + PORT_IRQ_STAT);
  702. writel(1 << i, mmio + HOST_IRQ_STAT);
  703. /* set irq mask (enables interrupts) */
  704. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  705. }
  706. tmp = readl(mmio + HOST_CTL);
  707. VPRINTK("HOST_CTL 0x%x\n", tmp);
  708. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  709. tmp = readl(mmio + HOST_CTL);
  710. VPRINTK("HOST_CTL 0x%x\n", tmp);
  711. pci_set_master(pdev);
  712. return 0;
  713. }
  714. /* move to PCI layer, integrate w/ MSI stuff */
  715. static void pci_intx(struct pci_dev *pdev, int enable)
  716. {
  717. u16 pci_command, new;
  718. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  719. if (enable)
  720. new = pci_command & ~PCI_COMMAND_INTX_DISABLE;
  721. else
  722. new = pci_command | PCI_COMMAND_INTX_DISABLE;
  723. if (new != pci_command)
  724. pci_write_config_word(pdev, PCI_COMMAND, pci_command);
  725. }
  726. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  727. {
  728. struct ahci_host_priv *hpriv = probe_ent->private_data;
  729. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  730. void *mmio = probe_ent->mmio_base;
  731. u32 vers, cap, impl, speed;
  732. const char *speed_s;
  733. u16 cc;
  734. const char *scc_s;
  735. vers = readl(mmio + HOST_VERSION);
  736. cap = hpriv->cap;
  737. impl = hpriv->port_map;
  738. speed = (cap >> 20) & 0xf;
  739. if (speed == 1)
  740. speed_s = "1.5";
  741. else if (speed == 2)
  742. speed_s = "3";
  743. else
  744. speed_s = "?";
  745. pci_read_config_word(pdev, 0x0a, &cc);
  746. if (cc == 0x0101)
  747. scc_s = "IDE";
  748. else if (cc == 0x0106)
  749. scc_s = "SATA";
  750. else if (cc == 0x0104)
  751. scc_s = "RAID";
  752. else
  753. scc_s = "unknown";
  754. printk(KERN_INFO DRV_NAME "(%s) AHCI %02x%02x.%02x%02x "
  755. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  756. ,
  757. pci_name(pdev),
  758. (vers >> 24) & 0xff,
  759. (vers >> 16) & 0xff,
  760. (vers >> 8) & 0xff,
  761. vers & 0xff,
  762. ((cap >> 8) & 0x1f) + 1,
  763. (cap & 0x1f) + 1,
  764. speed_s,
  765. impl,
  766. scc_s);
  767. printk(KERN_INFO DRV_NAME "(%s) flags: "
  768. "%s%s%s%s%s%s"
  769. "%s%s%s%s%s%s%s\n"
  770. ,
  771. pci_name(pdev),
  772. cap & (1 << 31) ? "64bit " : "",
  773. cap & (1 << 30) ? "ncq " : "",
  774. cap & (1 << 28) ? "ilck " : "",
  775. cap & (1 << 27) ? "stag " : "",
  776. cap & (1 << 26) ? "pm " : "",
  777. cap & (1 << 25) ? "led " : "",
  778. cap & (1 << 24) ? "clo " : "",
  779. cap & (1 << 19) ? "nz " : "",
  780. cap & (1 << 18) ? "only " : "",
  781. cap & (1 << 17) ? "pmp " : "",
  782. cap & (1 << 15) ? "pio " : "",
  783. cap & (1 << 14) ? "slum " : "",
  784. cap & (1 << 13) ? "part " : ""
  785. );
  786. }
  787. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  788. {
  789. static int printed_version;
  790. struct ata_probe_ent *probe_ent = NULL;
  791. struct ahci_host_priv *hpriv;
  792. unsigned long base;
  793. void *mmio_base;
  794. unsigned int board_idx = (unsigned int) ent->driver_data;
  795. int have_msi, pci_dev_busy = 0;
  796. int rc;
  797. VPRINTK("ENTER\n");
  798. if (!printed_version++)
  799. printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
  800. rc = pci_enable_device(pdev);
  801. if (rc)
  802. return rc;
  803. rc = pci_request_regions(pdev, DRV_NAME);
  804. if (rc) {
  805. pci_dev_busy = 1;
  806. goto err_out;
  807. }
  808. if (pci_enable_msi(pdev) == 0)
  809. have_msi = 1;
  810. else {
  811. pci_intx(pdev, 1);
  812. have_msi = 0;
  813. }
  814. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  815. if (probe_ent == NULL) {
  816. rc = -ENOMEM;
  817. goto err_out_msi;
  818. }
  819. memset(probe_ent, 0, sizeof(*probe_ent));
  820. probe_ent->dev = pci_dev_to_dev(pdev);
  821. INIT_LIST_HEAD(&probe_ent->node);
  822. mmio_base = ioremap(pci_resource_start(pdev, AHCI_PCI_BAR),
  823. pci_resource_len(pdev, AHCI_PCI_BAR));
  824. if (mmio_base == NULL) {
  825. rc = -ENOMEM;
  826. goto err_out_free_ent;
  827. }
  828. base = (unsigned long) mmio_base;
  829. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  830. if (!hpriv) {
  831. rc = -ENOMEM;
  832. goto err_out_iounmap;
  833. }
  834. memset(hpriv, 0, sizeof(*hpriv));
  835. probe_ent->sht = ahci_port_info[board_idx].sht;
  836. probe_ent->host_flags = ahci_port_info[board_idx].host_flags;
  837. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  838. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  839. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  840. probe_ent->irq = pdev->irq;
  841. probe_ent->irq_flags = SA_SHIRQ;
  842. probe_ent->mmio_base = mmio_base;
  843. probe_ent->private_data = hpriv;
  844. if (have_msi)
  845. hpriv->flags |= AHCI_FLAG_MSI;
  846. /* initialize adapter */
  847. rc = ahci_host_init(probe_ent);
  848. if (rc)
  849. goto err_out_hpriv;
  850. ahci_print_info(probe_ent);
  851. /* FIXME: check ata_device_add return value */
  852. ata_device_add(probe_ent);
  853. kfree(probe_ent);
  854. return 0;
  855. err_out_hpriv:
  856. kfree(hpriv);
  857. err_out_iounmap:
  858. iounmap(mmio_base);
  859. err_out_free_ent:
  860. kfree(probe_ent);
  861. err_out_msi:
  862. if (have_msi)
  863. pci_disable_msi(pdev);
  864. else
  865. pci_intx(pdev, 0);
  866. pci_release_regions(pdev);
  867. err_out:
  868. if (!pci_dev_busy)
  869. pci_disable_device(pdev);
  870. return rc;
  871. }
  872. static void ahci_remove_one (struct pci_dev *pdev)
  873. {
  874. struct device *dev = pci_dev_to_dev(pdev);
  875. struct ata_host_set *host_set = dev_get_drvdata(dev);
  876. struct ahci_host_priv *hpriv = host_set->private_data;
  877. struct ata_port *ap;
  878. unsigned int i;
  879. int have_msi;
  880. for (i = 0; i < host_set->n_ports; i++) {
  881. ap = host_set->ports[i];
  882. scsi_remove_host(ap->host);
  883. }
  884. have_msi = hpriv->flags & AHCI_FLAG_MSI;
  885. free_irq(host_set->irq, host_set);
  886. for (i = 0; i < host_set->n_ports; i++) {
  887. ap = host_set->ports[i];
  888. ata_scsi_release(ap->host);
  889. scsi_host_put(ap->host);
  890. }
  891. host_set->ops->host_stop(host_set);
  892. kfree(host_set);
  893. if (have_msi)
  894. pci_disable_msi(pdev);
  895. else
  896. pci_intx(pdev, 0);
  897. pci_release_regions(pdev);
  898. pci_disable_device(pdev);
  899. dev_set_drvdata(dev, NULL);
  900. }
  901. static int __init ahci_init(void)
  902. {
  903. return pci_module_init(&ahci_pci_driver);
  904. }
  905. static void __exit ahci_exit(void)
  906. {
  907. pci_unregister_driver(&ahci_pci_driver);
  908. }
  909. MODULE_AUTHOR("Jeff Garzik");
  910. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  911. MODULE_LICENSE("GPL");
  912. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  913. module_init(ahci_init);
  914. module_exit(ahci_exit);