ahci.c 30 KB

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