ahci.c 31 KB

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