ahci.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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 void ahci_phy_reset(struct ata_port *ap);
  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_strategy_handler = ata_scsi_error,
  188. .can_queue = ATA_DEF_QUEUE,
  189. .this_id = ATA_SHT_THIS_ID,
  190. .sg_tablesize = AHCI_MAX_SG,
  191. .max_sectors = ATA_MAX_SECTORS,
  192. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  193. .emulated = ATA_SHT_EMULATED,
  194. .use_clustering = AHCI_USE_CLUSTERING,
  195. .proc_name = DRV_NAME,
  196. .dma_boundary = AHCI_DMA_BOUNDARY,
  197. .slave_configure = ata_scsi_slave_config,
  198. .bios_param = ata_std_bios_param,
  199. };
  200. static const struct ata_port_operations ahci_ops = {
  201. .port_disable = ata_port_disable,
  202. .check_status = ahci_check_status,
  203. .check_altstatus = ahci_check_status,
  204. .dev_select = ata_noop_dev_select,
  205. .tf_read = ahci_tf_read,
  206. .phy_reset = ahci_phy_reset,
  207. .qc_prep = ahci_qc_prep,
  208. .qc_issue = ahci_qc_issue,
  209. .eng_timeout = ahci_eng_timeout,
  210. .irq_handler = ahci_interrupt,
  211. .irq_clear = ahci_irq_clear,
  212. .scr_read = ahci_scr_read,
  213. .scr_write = ahci_scr_write,
  214. .port_start = ahci_port_start,
  215. .port_stop = ahci_port_stop,
  216. };
  217. static const struct ata_port_info ahci_port_info[] = {
  218. /* board_ahci */
  219. {
  220. .sht = &ahci_sht,
  221. .host_flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  222. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
  223. 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. { } /* terminate list */
  263. };
  264. static struct pci_driver ahci_pci_driver = {
  265. .name = DRV_NAME,
  266. .id_table = ahci_pci_tbl,
  267. .probe = ahci_init_one,
  268. .remove = ahci_remove_one,
  269. };
  270. static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
  271. {
  272. return base + 0x100 + (port * 0x80);
  273. }
  274. static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int port)
  275. {
  276. return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
  277. }
  278. static int ahci_port_start(struct ata_port *ap)
  279. {
  280. struct device *dev = ap->host_set->dev;
  281. struct ahci_host_priv *hpriv = ap->host_set->private_data;
  282. struct ahci_port_priv *pp;
  283. void __iomem *mmio = ap->host_set->mmio_base;
  284. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  285. void *mem;
  286. dma_addr_t mem_dma;
  287. int rc;
  288. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  289. if (!pp)
  290. return -ENOMEM;
  291. memset(pp, 0, sizeof(*pp));
  292. rc = ata_pad_alloc(ap, dev);
  293. if (rc) {
  294. kfree(pp);
  295. return rc;
  296. }
  297. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  298. if (!mem) {
  299. ata_pad_free(ap, dev);
  300. kfree(pp);
  301. return -ENOMEM;
  302. }
  303. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  304. /*
  305. * First item in chunk of DMA memory: 32-slot command table,
  306. * 32 bytes each in size
  307. */
  308. pp->cmd_slot = mem;
  309. pp->cmd_slot_dma = mem_dma;
  310. mem += AHCI_CMD_SLOT_SZ;
  311. mem_dma += AHCI_CMD_SLOT_SZ;
  312. /*
  313. * Second item: Received-FIS area
  314. */
  315. pp->rx_fis = mem;
  316. pp->rx_fis_dma = mem_dma;
  317. mem += AHCI_RX_FIS_SZ;
  318. mem_dma += AHCI_RX_FIS_SZ;
  319. /*
  320. * Third item: data area for storing a single command
  321. * and its scatter-gather table
  322. */
  323. pp->cmd_tbl = mem;
  324. pp->cmd_tbl_dma = mem_dma;
  325. pp->cmd_tbl_sg = mem + AHCI_CMD_TBL_HDR;
  326. ap->private_data = pp;
  327. if (hpriv->cap & HOST_CAP_64)
  328. writel((pp->cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  329. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  330. readl(port_mmio + PORT_LST_ADDR); /* flush */
  331. if (hpriv->cap & HOST_CAP_64)
  332. writel((pp->rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  333. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  334. readl(port_mmio + PORT_FIS_ADDR); /* flush */
  335. writel(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  336. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  337. PORT_CMD_START, port_mmio + PORT_CMD);
  338. readl(port_mmio + PORT_CMD); /* flush */
  339. return 0;
  340. }
  341. static void ahci_port_stop(struct ata_port *ap)
  342. {
  343. struct device *dev = ap->host_set->dev;
  344. struct ahci_port_priv *pp = ap->private_data;
  345. void __iomem *mmio = ap->host_set->mmio_base;
  346. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  347. u32 tmp;
  348. tmp = readl(port_mmio + PORT_CMD);
  349. tmp &= ~(PORT_CMD_START | PORT_CMD_FIS_RX);
  350. writel(tmp, port_mmio + PORT_CMD);
  351. readl(port_mmio + PORT_CMD); /* flush */
  352. /* spec says 500 msecs for each PORT_CMD_{START,FIS_RX} bit, so
  353. * this is slightly incorrect.
  354. */
  355. msleep(500);
  356. ap->private_data = NULL;
  357. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  358. pp->cmd_slot, pp->cmd_slot_dma);
  359. ata_pad_free(ap, dev);
  360. kfree(pp);
  361. }
  362. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  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 0xffffffffU;
  372. }
  373. return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  374. }
  375. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  376. u32 val)
  377. {
  378. unsigned int sc_reg;
  379. switch (sc_reg_in) {
  380. case SCR_STATUS: sc_reg = 0; break;
  381. case SCR_CONTROL: sc_reg = 1; break;
  382. case SCR_ERROR: sc_reg = 2; break;
  383. case SCR_ACTIVE: sc_reg = 3; break;
  384. default:
  385. return;
  386. }
  387. writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  388. }
  389. static int ahci_stop_engine(struct ata_port *ap)
  390. {
  391. void __iomem *mmio = ap->host_set->mmio_base;
  392. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  393. int work;
  394. u32 tmp;
  395. tmp = readl(port_mmio + PORT_CMD);
  396. tmp &= ~PORT_CMD_START;
  397. writel(tmp, port_mmio + PORT_CMD);
  398. /* wait for engine to stop. TODO: this could be
  399. * as long as 500 msec
  400. */
  401. work = 1000;
  402. while (work-- > 0) {
  403. tmp = readl(port_mmio + PORT_CMD);
  404. if ((tmp & PORT_CMD_LIST_ON) == 0)
  405. return 0;
  406. udelay(10);
  407. }
  408. return -EIO;
  409. }
  410. static void ahci_start_engine(struct ata_port *ap)
  411. {
  412. void __iomem *mmio = ap->host_set->mmio_base;
  413. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  414. u32 tmp;
  415. tmp = readl(port_mmio + PORT_CMD);
  416. tmp |= PORT_CMD_START;
  417. writel(tmp, port_mmio + PORT_CMD);
  418. readl(port_mmio + PORT_CMD); /* flush */
  419. }
  420. static unsigned int ahci_dev_classify(struct ata_port *ap)
  421. {
  422. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  423. struct ata_taskfile tf;
  424. u32 tmp;
  425. tmp = readl(port_mmio + PORT_SIG);
  426. tf.lbah = (tmp >> 24) & 0xff;
  427. tf.lbam = (tmp >> 16) & 0xff;
  428. tf.lbal = (tmp >> 8) & 0xff;
  429. tf.nsect = (tmp) & 0xff;
  430. return ata_dev_classify(&tf);
  431. }
  432. static void ahci_phy_reset(struct ata_port *ap)
  433. {
  434. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  435. struct ata_device *dev = &ap->device[0];
  436. u32 new_tmp, tmp;
  437. ahci_stop_engine(ap);
  438. __sata_phy_reset(ap);
  439. ahci_start_engine(ap);
  440. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  441. return;
  442. dev->class = ahci_dev_classify(ap);
  443. if (!ata_dev_present(dev)) {
  444. ata_port_disable(ap);
  445. return;
  446. }
  447. /* Make sure port's ATAPI bit is set appropriately */
  448. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  449. if (dev->class == ATA_DEV_ATAPI)
  450. new_tmp |= PORT_CMD_ATAPI;
  451. else
  452. new_tmp &= ~PORT_CMD_ATAPI;
  453. if (new_tmp != tmp) {
  454. writel(new_tmp, port_mmio + PORT_CMD);
  455. readl(port_mmio + PORT_CMD); /* flush */
  456. }
  457. }
  458. static u8 ahci_check_status(struct ata_port *ap)
  459. {
  460. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  461. return readl(mmio + PORT_TFDATA) & 0xFF;
  462. }
  463. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  464. {
  465. struct ahci_port_priv *pp = ap->private_data;
  466. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  467. ata_tf_from_fis(d2h_fis, tf);
  468. }
  469. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc)
  470. {
  471. struct ahci_port_priv *pp = qc->ap->private_data;
  472. struct scatterlist *sg;
  473. struct ahci_sg *ahci_sg;
  474. unsigned int n_sg = 0;
  475. VPRINTK("ENTER\n");
  476. /*
  477. * Next, the S/G list.
  478. */
  479. ahci_sg = pp->cmd_tbl_sg;
  480. ata_for_each_sg(sg, qc) {
  481. dma_addr_t addr = sg_dma_address(sg);
  482. u32 sg_len = sg_dma_len(sg);
  483. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  484. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  485. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  486. ahci_sg++;
  487. n_sg++;
  488. }
  489. return n_sg;
  490. }
  491. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  492. {
  493. struct ata_port *ap = qc->ap;
  494. struct ahci_port_priv *pp = ap->private_data;
  495. u32 opts;
  496. const u32 cmd_fis_len = 5; /* five dwords */
  497. unsigned int n_elem;
  498. /*
  499. * Fill in command slot information (currently only one slot,
  500. * slot 0, is currently since we don't do queueing)
  501. */
  502. opts = cmd_fis_len;
  503. if (qc->tf.flags & ATA_TFLAG_WRITE)
  504. opts |= AHCI_CMD_WRITE;
  505. if (is_atapi_taskfile(&qc->tf))
  506. opts |= AHCI_CMD_ATAPI;
  507. pp->cmd_slot[0].opts = cpu_to_le32(opts);
  508. pp->cmd_slot[0].status = 0;
  509. pp->cmd_slot[0].tbl_addr = cpu_to_le32(pp->cmd_tbl_dma & 0xffffffff);
  510. pp->cmd_slot[0].tbl_addr_hi = cpu_to_le32((pp->cmd_tbl_dma >> 16) >> 16);
  511. /*
  512. * Fill in command table information. First, the header,
  513. * a SATA Register - Host to Device command FIS.
  514. */
  515. ata_tf_to_fis(&qc->tf, pp->cmd_tbl, 0);
  516. if (opts & AHCI_CMD_ATAPI) {
  517. memset(pp->cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  518. memcpy(pp->cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, ap->cdb_len);
  519. }
  520. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  521. return;
  522. n_elem = ahci_fill_sg(qc);
  523. pp->cmd_slot[0].opts |= cpu_to_le32(n_elem << 16);
  524. }
  525. static void ahci_restart_port(struct ata_port *ap, u32 irq_stat)
  526. {
  527. void __iomem *mmio = ap->host_set->mmio_base;
  528. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  529. u32 tmp;
  530. if ((ap->device[0].class != ATA_DEV_ATAPI) ||
  531. ((irq_stat & PORT_IRQ_TF_ERR) == 0))
  532. printk(KERN_WARNING "ata%u: port reset, "
  533. "p_is %x is %x pis %x cmd %x tf %x ss %x se %x\n",
  534. ap->id,
  535. irq_stat,
  536. readl(mmio + HOST_IRQ_STAT),
  537. readl(port_mmio + PORT_IRQ_STAT),
  538. readl(port_mmio + PORT_CMD),
  539. readl(port_mmio + PORT_TFDATA),
  540. readl(port_mmio + PORT_SCR_STAT),
  541. readl(port_mmio + PORT_SCR_ERR));
  542. /* stop DMA */
  543. ahci_stop_engine(ap);
  544. /* clear SATA phy error, if any */
  545. tmp = readl(port_mmio + PORT_SCR_ERR);
  546. writel(tmp, port_mmio + PORT_SCR_ERR);
  547. /* if DRQ/BSY is set, device needs to be reset.
  548. * if so, issue COMRESET
  549. */
  550. tmp = readl(port_mmio + PORT_TFDATA);
  551. if (tmp & (ATA_BUSY | ATA_DRQ)) {
  552. writel(0x301, port_mmio + PORT_SCR_CTL);
  553. readl(port_mmio + PORT_SCR_CTL); /* flush */
  554. udelay(10);
  555. writel(0x300, port_mmio + PORT_SCR_CTL);
  556. readl(port_mmio + PORT_SCR_CTL); /* flush */
  557. }
  558. /* re-start DMA */
  559. ahci_start_engine(ap);
  560. }
  561. static void ahci_eng_timeout(struct ata_port *ap)
  562. {
  563. struct ata_host_set *host_set = ap->host_set;
  564. void __iomem *mmio = host_set->mmio_base;
  565. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  566. struct ata_queued_cmd *qc;
  567. unsigned long flags;
  568. printk(KERN_WARNING "ata%u: handling error/timeout\n", ap->id);
  569. spin_lock_irqsave(&host_set->lock, flags);
  570. qc = ata_qc_from_tag(ap, ap->active_tag);
  571. if (!qc) {
  572. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  573. ap->id);
  574. } else {
  575. ahci_restart_port(ap, readl(port_mmio + PORT_IRQ_STAT));
  576. qc->err_mask |= AC_ERR_TIMEOUT;
  577. }
  578. spin_unlock_irqrestore(&host_set->lock, flags);
  579. if (qc)
  580. ata_eh_qc_complete(qc);
  581. }
  582. static inline int ahci_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
  583. {
  584. void __iomem *mmio = ap->host_set->mmio_base;
  585. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  586. u32 status, serr, ci;
  587. serr = readl(port_mmio + PORT_SCR_ERR);
  588. writel(serr, port_mmio + PORT_SCR_ERR);
  589. status = readl(port_mmio + PORT_IRQ_STAT);
  590. writel(status, port_mmio + PORT_IRQ_STAT);
  591. ci = readl(port_mmio + PORT_CMD_ISSUE);
  592. if (likely((ci & 0x1) == 0)) {
  593. if (qc) {
  594. assert(qc->err_mask == 0);
  595. ata_qc_complete(qc);
  596. qc = NULL;
  597. }
  598. }
  599. if (status & PORT_IRQ_FATAL) {
  600. unsigned int err_mask;
  601. if (status & PORT_IRQ_TF_ERR)
  602. err_mask = AC_ERR_DEV;
  603. else if (status & PORT_IRQ_IF_ERR)
  604. err_mask = AC_ERR_ATA_BUS;
  605. else
  606. err_mask = AC_ERR_HOST_BUS;
  607. /* command processing has stopped due to error; restart */
  608. ahci_restart_port(ap, status);
  609. if (qc) {
  610. qc->err_mask |= err_mask;
  611. ata_qc_complete(qc);
  612. }
  613. }
  614. return 1;
  615. }
  616. static void ahci_irq_clear(struct ata_port *ap)
  617. {
  618. /* TODO */
  619. }
  620. static irqreturn_t ahci_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
  621. {
  622. struct ata_host_set *host_set = dev_instance;
  623. struct ahci_host_priv *hpriv;
  624. unsigned int i, handled = 0;
  625. void __iomem *mmio;
  626. u32 irq_stat, irq_ack = 0;
  627. VPRINTK("ENTER\n");
  628. hpriv = host_set->private_data;
  629. mmio = host_set->mmio_base;
  630. /* sigh. 0xffffffff is a valid return from h/w */
  631. irq_stat = readl(mmio + HOST_IRQ_STAT);
  632. irq_stat &= hpriv->port_map;
  633. if (!irq_stat)
  634. return IRQ_NONE;
  635. spin_lock(&host_set->lock);
  636. for (i = 0; i < host_set->n_ports; i++) {
  637. struct ata_port *ap;
  638. if (!(irq_stat & (1 << i)))
  639. continue;
  640. ap = host_set->ports[i];
  641. if (ap) {
  642. struct ata_queued_cmd *qc;
  643. qc = ata_qc_from_tag(ap, ap->active_tag);
  644. if (!ahci_host_intr(ap, qc))
  645. if (ata_ratelimit()) {
  646. struct pci_dev *pdev =
  647. to_pci_dev(ap->host_set->dev);
  648. dev_printk(KERN_WARNING, &pdev->dev,
  649. "unhandled interrupt on port %u\n",
  650. i);
  651. }
  652. VPRINTK("port %u\n", i);
  653. } else {
  654. VPRINTK("port %u (no irq)\n", i);
  655. if (ata_ratelimit()) {
  656. struct pci_dev *pdev =
  657. to_pci_dev(ap->host_set->dev);
  658. dev_printk(KERN_WARNING, &pdev->dev,
  659. "interrupt on disabled port %u\n", i);
  660. }
  661. }
  662. irq_ack |= (1 << i);
  663. }
  664. if (irq_ack) {
  665. writel(irq_ack, mmio + HOST_IRQ_STAT);
  666. handled = 1;
  667. }
  668. spin_unlock(&host_set->lock);
  669. VPRINTK("EXIT\n");
  670. return IRQ_RETVAL(handled);
  671. }
  672. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  673. {
  674. struct ata_port *ap = qc->ap;
  675. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  676. writel(1, port_mmio + PORT_CMD_ISSUE);
  677. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  678. return 0;
  679. }
  680. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  681. unsigned int port_idx)
  682. {
  683. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  684. base = ahci_port_base_ul(base, port_idx);
  685. VPRINTK("base now==0x%lx\n", base);
  686. port->cmd_addr = base;
  687. port->scr_addr = base + PORT_SCR;
  688. VPRINTK("EXIT\n");
  689. }
  690. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  691. {
  692. struct ahci_host_priv *hpriv = probe_ent->private_data;
  693. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  694. void __iomem *mmio = probe_ent->mmio_base;
  695. u32 tmp, cap_save;
  696. unsigned int i, j, using_dac;
  697. int rc;
  698. void __iomem *port_mmio;
  699. cap_save = readl(mmio + HOST_CAP);
  700. cap_save &= ( (1<<28) | (1<<17) );
  701. cap_save |= (1 << 27);
  702. /* global controller reset */
  703. tmp = readl(mmio + HOST_CTL);
  704. if ((tmp & HOST_RESET) == 0) {
  705. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  706. readl(mmio + HOST_CTL); /* flush */
  707. }
  708. /* reset must complete within 1 second, or
  709. * the hardware should be considered fried.
  710. */
  711. ssleep(1);
  712. tmp = readl(mmio + HOST_CTL);
  713. if (tmp & HOST_RESET) {
  714. dev_printk(KERN_ERR, &pdev->dev,
  715. "controller reset failed (0x%x)\n", tmp);
  716. return -EIO;
  717. }
  718. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  719. (void) readl(mmio + HOST_CTL); /* flush */
  720. writel(cap_save, mmio + HOST_CAP);
  721. writel(0xf, mmio + HOST_PORTS_IMPL);
  722. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  723. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  724. u16 tmp16;
  725. pci_read_config_word(pdev, 0x92, &tmp16);
  726. tmp16 |= 0xf;
  727. pci_write_config_word(pdev, 0x92, tmp16);
  728. }
  729. hpriv->cap = readl(mmio + HOST_CAP);
  730. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  731. probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
  732. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  733. hpriv->cap, hpriv->port_map, probe_ent->n_ports);
  734. using_dac = hpriv->cap & HOST_CAP_64;
  735. if (using_dac &&
  736. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  737. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  738. if (rc) {
  739. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  740. if (rc) {
  741. dev_printk(KERN_ERR, &pdev->dev,
  742. "64-bit DMA enable failed\n");
  743. return rc;
  744. }
  745. }
  746. } else {
  747. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  748. if (rc) {
  749. dev_printk(KERN_ERR, &pdev->dev,
  750. "32-bit DMA enable failed\n");
  751. return rc;
  752. }
  753. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  754. if (rc) {
  755. dev_printk(KERN_ERR, &pdev->dev,
  756. "32-bit consistent DMA enable failed\n");
  757. return rc;
  758. }
  759. }
  760. for (i = 0; i < probe_ent->n_ports; i++) {
  761. #if 0 /* BIOSen initialize this incorrectly */
  762. if (!(hpriv->port_map & (1 << i)))
  763. continue;
  764. #endif
  765. port_mmio = ahci_port_base(mmio, i);
  766. VPRINTK("mmio %p port_mmio %p\n", mmio, port_mmio);
  767. ahci_setup_port(&probe_ent->port[i],
  768. (unsigned long) mmio, i);
  769. /* make sure port is not active */
  770. tmp = readl(port_mmio + PORT_CMD);
  771. VPRINTK("PORT_CMD 0x%x\n", tmp);
  772. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  773. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  774. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  775. PORT_CMD_FIS_RX | PORT_CMD_START);
  776. writel(tmp, port_mmio + PORT_CMD);
  777. readl(port_mmio + PORT_CMD); /* flush */
  778. /* spec says 500 msecs for each bit, so
  779. * this is slightly incorrect.
  780. */
  781. msleep(500);
  782. }
  783. writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
  784. j = 0;
  785. while (j < 100) {
  786. msleep(10);
  787. tmp = readl(port_mmio + PORT_SCR_STAT);
  788. if ((tmp & 0xf) == 0x3)
  789. break;
  790. j++;
  791. }
  792. tmp = readl(port_mmio + PORT_SCR_ERR);
  793. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  794. writel(tmp, port_mmio + PORT_SCR_ERR);
  795. /* ack any pending irq events for this port */
  796. tmp = readl(port_mmio + PORT_IRQ_STAT);
  797. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  798. if (tmp)
  799. writel(tmp, port_mmio + PORT_IRQ_STAT);
  800. writel(1 << i, mmio + HOST_IRQ_STAT);
  801. /* set irq mask (enables interrupts) */
  802. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  803. }
  804. tmp = readl(mmio + HOST_CTL);
  805. VPRINTK("HOST_CTL 0x%x\n", tmp);
  806. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  807. tmp = readl(mmio + HOST_CTL);
  808. VPRINTK("HOST_CTL 0x%x\n", tmp);
  809. pci_set_master(pdev);
  810. return 0;
  811. }
  812. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  813. {
  814. struct ahci_host_priv *hpriv = probe_ent->private_data;
  815. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  816. void __iomem *mmio = probe_ent->mmio_base;
  817. u32 vers, cap, impl, speed;
  818. const char *speed_s;
  819. u16 cc;
  820. const char *scc_s;
  821. vers = readl(mmio + HOST_VERSION);
  822. cap = hpriv->cap;
  823. impl = hpriv->port_map;
  824. speed = (cap >> 20) & 0xf;
  825. if (speed == 1)
  826. speed_s = "1.5";
  827. else if (speed == 2)
  828. speed_s = "3";
  829. else
  830. speed_s = "?";
  831. pci_read_config_word(pdev, 0x0a, &cc);
  832. if (cc == 0x0101)
  833. scc_s = "IDE";
  834. else if (cc == 0x0106)
  835. scc_s = "SATA";
  836. else if (cc == 0x0104)
  837. scc_s = "RAID";
  838. else
  839. scc_s = "unknown";
  840. dev_printk(KERN_INFO, &pdev->dev,
  841. "AHCI %02x%02x.%02x%02x "
  842. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  843. ,
  844. (vers >> 24) & 0xff,
  845. (vers >> 16) & 0xff,
  846. (vers >> 8) & 0xff,
  847. vers & 0xff,
  848. ((cap >> 8) & 0x1f) + 1,
  849. (cap & 0x1f) + 1,
  850. speed_s,
  851. impl,
  852. scc_s);
  853. dev_printk(KERN_INFO, &pdev->dev,
  854. "flags: "
  855. "%s%s%s%s%s%s"
  856. "%s%s%s%s%s%s%s\n"
  857. ,
  858. cap & (1 << 31) ? "64bit " : "",
  859. cap & (1 << 30) ? "ncq " : "",
  860. cap & (1 << 28) ? "ilck " : "",
  861. cap & (1 << 27) ? "stag " : "",
  862. cap & (1 << 26) ? "pm " : "",
  863. cap & (1 << 25) ? "led " : "",
  864. cap & (1 << 24) ? "clo " : "",
  865. cap & (1 << 19) ? "nz " : "",
  866. cap & (1 << 18) ? "only " : "",
  867. cap & (1 << 17) ? "pmp " : "",
  868. cap & (1 << 15) ? "pio " : "",
  869. cap & (1 << 14) ? "slum " : "",
  870. cap & (1 << 13) ? "part " : ""
  871. );
  872. }
  873. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  874. {
  875. static int printed_version;
  876. struct ata_probe_ent *probe_ent = NULL;
  877. struct ahci_host_priv *hpriv;
  878. unsigned long base;
  879. void __iomem *mmio_base;
  880. unsigned int board_idx = (unsigned int) ent->driver_data;
  881. int have_msi, pci_dev_busy = 0;
  882. int rc;
  883. VPRINTK("ENTER\n");
  884. if (!printed_version++)
  885. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  886. rc = pci_enable_device(pdev);
  887. if (rc)
  888. return rc;
  889. rc = pci_request_regions(pdev, DRV_NAME);
  890. if (rc) {
  891. pci_dev_busy = 1;
  892. goto err_out;
  893. }
  894. if (pci_enable_msi(pdev) == 0)
  895. have_msi = 1;
  896. else {
  897. pci_intx(pdev, 1);
  898. have_msi = 0;
  899. }
  900. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  901. if (probe_ent == NULL) {
  902. rc = -ENOMEM;
  903. goto err_out_msi;
  904. }
  905. memset(probe_ent, 0, sizeof(*probe_ent));
  906. probe_ent->dev = pci_dev_to_dev(pdev);
  907. INIT_LIST_HEAD(&probe_ent->node);
  908. mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
  909. if (mmio_base == NULL) {
  910. rc = -ENOMEM;
  911. goto err_out_free_ent;
  912. }
  913. base = (unsigned long) mmio_base;
  914. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  915. if (!hpriv) {
  916. rc = -ENOMEM;
  917. goto err_out_iounmap;
  918. }
  919. memset(hpriv, 0, sizeof(*hpriv));
  920. probe_ent->sht = ahci_port_info[board_idx].sht;
  921. probe_ent->host_flags = ahci_port_info[board_idx].host_flags;
  922. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  923. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  924. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  925. probe_ent->irq = pdev->irq;
  926. probe_ent->irq_flags = SA_SHIRQ;
  927. probe_ent->mmio_base = mmio_base;
  928. probe_ent->private_data = hpriv;
  929. if (have_msi)
  930. hpriv->flags |= AHCI_FLAG_MSI;
  931. /* JMicron-specific fixup: make sure we're in AHCI mode */
  932. if (pdev->vendor == 0x197b)
  933. pci_write_config_byte(pdev, 0x41, 0xa1);
  934. /* initialize adapter */
  935. rc = ahci_host_init(probe_ent);
  936. if (rc)
  937. goto err_out_hpriv;
  938. ahci_print_info(probe_ent);
  939. /* FIXME: check ata_device_add return value */
  940. ata_device_add(probe_ent);
  941. kfree(probe_ent);
  942. return 0;
  943. err_out_hpriv:
  944. kfree(hpriv);
  945. err_out_iounmap:
  946. pci_iounmap(pdev, mmio_base);
  947. err_out_free_ent:
  948. kfree(probe_ent);
  949. err_out_msi:
  950. if (have_msi)
  951. pci_disable_msi(pdev);
  952. else
  953. pci_intx(pdev, 0);
  954. pci_release_regions(pdev);
  955. err_out:
  956. if (!pci_dev_busy)
  957. pci_disable_device(pdev);
  958. return rc;
  959. }
  960. static void ahci_remove_one (struct pci_dev *pdev)
  961. {
  962. struct device *dev = pci_dev_to_dev(pdev);
  963. struct ata_host_set *host_set = dev_get_drvdata(dev);
  964. struct ahci_host_priv *hpriv = host_set->private_data;
  965. struct ata_port *ap;
  966. unsigned int i;
  967. int have_msi;
  968. for (i = 0; i < host_set->n_ports; i++) {
  969. ap = host_set->ports[i];
  970. scsi_remove_host(ap->host);
  971. }
  972. have_msi = hpriv->flags & AHCI_FLAG_MSI;
  973. free_irq(host_set->irq, host_set);
  974. for (i = 0; i < host_set->n_ports; i++) {
  975. ap = host_set->ports[i];
  976. ata_scsi_release(ap->host);
  977. scsi_host_put(ap->host);
  978. }
  979. kfree(hpriv);
  980. pci_iounmap(pdev, host_set->mmio_base);
  981. kfree(host_set);
  982. if (have_msi)
  983. pci_disable_msi(pdev);
  984. else
  985. pci_intx(pdev, 0);
  986. pci_release_regions(pdev);
  987. pci_disable_device(pdev);
  988. dev_set_drvdata(dev, NULL);
  989. }
  990. static int __init ahci_init(void)
  991. {
  992. return pci_module_init(&ahci_pci_driver);
  993. }
  994. static void __exit ahci_exit(void)
  995. {
  996. pci_unregister_driver(&ahci_pci_driver);
  997. }
  998. MODULE_AUTHOR("Jeff Garzik");
  999. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1000. MODULE_LICENSE("GPL");
  1001. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1002. MODULE_VERSION(DRV_VERSION);
  1003. module_init(ahci_init);
  1004. module_exit(ahci_exit);