ahci.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  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 "2.0"
  50. enum {
  51. AHCI_PCI_BAR = 5,
  52. AHCI_MAX_PORTS = 32,
  53. AHCI_MAX_SG = 168, /* hardware max is 64K */
  54. AHCI_DMA_BOUNDARY = 0xffffffff,
  55. AHCI_USE_CLUSTERING = 0,
  56. AHCI_MAX_CMDS = 32,
  57. AHCI_CMD_SZ = 32,
  58. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  59. AHCI_RX_FIS_SZ = 256,
  60. AHCI_CMD_TBL_CDB = 0x40,
  61. AHCI_CMD_TBL_HDR_SZ = 0x80,
  62. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  63. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  64. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  65. AHCI_RX_FIS_SZ,
  66. AHCI_IRQ_ON_SG = (1 << 31),
  67. AHCI_CMD_ATAPI = (1 << 5),
  68. AHCI_CMD_WRITE = (1 << 6),
  69. AHCI_CMD_PREFETCH = (1 << 7),
  70. AHCI_CMD_RESET = (1 << 8),
  71. AHCI_CMD_CLR_BUSY = (1 << 10),
  72. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  73. RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
  74. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  75. board_ahci = 0,
  76. board_ahci_pi = 1,
  77. board_ahci_vt8251 = 2,
  78. board_ahci_ign_iferr = 3,
  79. /* global controller registers */
  80. HOST_CAP = 0x00, /* host capabilities */
  81. HOST_CTL = 0x04, /* global host control */
  82. HOST_IRQ_STAT = 0x08, /* interrupt status */
  83. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  84. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  85. /* HOST_CTL bits */
  86. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  87. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  88. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  89. /* HOST_CAP bits */
  90. HOST_CAP_SSC = (1 << 14), /* Slumber capable */
  91. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  92. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  93. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  94. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  95. /* registers for each SATA port */
  96. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  97. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  98. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  99. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  100. PORT_IRQ_STAT = 0x10, /* interrupt status */
  101. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  102. PORT_CMD = 0x18, /* port command */
  103. PORT_TFDATA = 0x20, /* taskfile data */
  104. PORT_SIG = 0x24, /* device TF signature */
  105. PORT_CMD_ISSUE = 0x38, /* command issue */
  106. PORT_SCR = 0x28, /* SATA phy register block */
  107. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  108. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  109. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  110. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  111. /* PORT_IRQ_{STAT,MASK} bits */
  112. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  113. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  114. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  115. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  116. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  117. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  118. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  119. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  120. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  121. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  122. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  123. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  124. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  125. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  126. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  127. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  128. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  129. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  130. PORT_IRQ_IF_ERR |
  131. PORT_IRQ_CONNECT |
  132. PORT_IRQ_PHYRDY |
  133. PORT_IRQ_UNK_FIS,
  134. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  135. PORT_IRQ_TF_ERR |
  136. PORT_IRQ_HBUS_DATA_ERR,
  137. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  138. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  139. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  140. /* PORT_CMD bits */
  141. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  142. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  143. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  144. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  145. PORT_CMD_CLO = (1 << 3), /* Command list override */
  146. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  147. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  148. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  149. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  150. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  151. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  152. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  153. /* hpriv->flags bits */
  154. AHCI_FLAG_MSI = (1 << 0),
  155. /* ap->flags bits */
  156. AHCI_FLAG_NO_NCQ = (1 << 24),
  157. AHCI_FLAG_IGN_IRQ_IF_ERR = (1 << 25), /* ignore IRQ_IF_ERR */
  158. AHCI_FLAG_HONOR_PI = (1 << 26), /* honor PORTS_IMPL */
  159. };
  160. struct ahci_cmd_hdr {
  161. u32 opts;
  162. u32 status;
  163. u32 tbl_addr;
  164. u32 tbl_addr_hi;
  165. u32 reserved[4];
  166. };
  167. struct ahci_sg {
  168. u32 addr;
  169. u32 addr_hi;
  170. u32 reserved;
  171. u32 flags_size;
  172. };
  173. struct ahci_host_priv {
  174. unsigned long flags;
  175. u32 cap; /* cache of HOST_CAP register */
  176. u32 port_map; /* cache of HOST_PORTS_IMPL reg */
  177. };
  178. struct ahci_port_priv {
  179. struct ahci_cmd_hdr *cmd_slot;
  180. dma_addr_t cmd_slot_dma;
  181. void *cmd_tbl;
  182. dma_addr_t cmd_tbl_dma;
  183. void *rx_fis;
  184. dma_addr_t rx_fis_dma;
  185. /* for NCQ spurious interrupt analysis */
  186. int ncq_saw_spurious_sdb_cnt;
  187. unsigned int ncq_saw_d2h:1;
  188. unsigned int ncq_saw_dmas:1;
  189. };
  190. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
  191. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  192. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  193. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  194. static irqreturn_t ahci_interrupt (int irq, void *dev_instance);
  195. static void ahci_irq_clear(struct ata_port *ap);
  196. static int ahci_port_start(struct ata_port *ap);
  197. static void ahci_port_stop(struct ata_port *ap);
  198. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  199. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  200. static u8 ahci_check_status(struct ata_port *ap);
  201. static void ahci_freeze(struct ata_port *ap);
  202. static void ahci_thaw(struct ata_port *ap);
  203. static void ahci_error_handler(struct ata_port *ap);
  204. static void ahci_vt8251_error_handler(struct ata_port *ap);
  205. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  206. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  207. static int ahci_port_resume(struct ata_port *ap);
  208. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  209. static int ahci_pci_device_resume(struct pci_dev *pdev);
  210. static void ahci_remove_one (struct pci_dev *pdev);
  211. static struct scsi_host_template ahci_sht = {
  212. .module = THIS_MODULE,
  213. .name = DRV_NAME,
  214. .ioctl = ata_scsi_ioctl,
  215. .queuecommand = ata_scsi_queuecmd,
  216. .change_queue_depth = ata_scsi_change_queue_depth,
  217. .can_queue = AHCI_MAX_CMDS - 1,
  218. .this_id = ATA_SHT_THIS_ID,
  219. .sg_tablesize = AHCI_MAX_SG,
  220. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  221. .emulated = ATA_SHT_EMULATED,
  222. .use_clustering = AHCI_USE_CLUSTERING,
  223. .proc_name = DRV_NAME,
  224. .dma_boundary = AHCI_DMA_BOUNDARY,
  225. .slave_configure = ata_scsi_slave_config,
  226. .slave_destroy = ata_scsi_slave_destroy,
  227. .bios_param = ata_std_bios_param,
  228. .suspend = ata_scsi_device_suspend,
  229. .resume = ata_scsi_device_resume,
  230. };
  231. static const struct ata_port_operations ahci_ops = {
  232. .port_disable = ata_port_disable,
  233. .check_status = ahci_check_status,
  234. .check_altstatus = ahci_check_status,
  235. .dev_select = ata_noop_dev_select,
  236. .tf_read = ahci_tf_read,
  237. .qc_prep = ahci_qc_prep,
  238. .qc_issue = ahci_qc_issue,
  239. .irq_handler = ahci_interrupt,
  240. .irq_clear = ahci_irq_clear,
  241. .scr_read = ahci_scr_read,
  242. .scr_write = ahci_scr_write,
  243. .freeze = ahci_freeze,
  244. .thaw = ahci_thaw,
  245. .error_handler = ahci_error_handler,
  246. .post_internal_cmd = ahci_post_internal_cmd,
  247. .port_suspend = ahci_port_suspend,
  248. .port_resume = ahci_port_resume,
  249. .port_start = ahci_port_start,
  250. .port_stop = ahci_port_stop,
  251. };
  252. static const struct ata_port_operations ahci_vt8251_ops = {
  253. .port_disable = ata_port_disable,
  254. .check_status = ahci_check_status,
  255. .check_altstatus = ahci_check_status,
  256. .dev_select = ata_noop_dev_select,
  257. .tf_read = ahci_tf_read,
  258. .qc_prep = ahci_qc_prep,
  259. .qc_issue = ahci_qc_issue,
  260. .irq_handler = ahci_interrupt,
  261. .irq_clear = ahci_irq_clear,
  262. .scr_read = ahci_scr_read,
  263. .scr_write = ahci_scr_write,
  264. .freeze = ahci_freeze,
  265. .thaw = ahci_thaw,
  266. .error_handler = ahci_vt8251_error_handler,
  267. .post_internal_cmd = ahci_post_internal_cmd,
  268. .port_suspend = ahci_port_suspend,
  269. .port_resume = ahci_port_resume,
  270. .port_start = ahci_port_start,
  271. .port_stop = ahci_port_stop,
  272. };
  273. static const struct ata_port_info ahci_port_info[] = {
  274. /* board_ahci */
  275. {
  276. .sht = &ahci_sht,
  277. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  278. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  279. ATA_FLAG_SKIP_D2H_BSY,
  280. .pio_mask = 0x1f, /* pio0-4 */
  281. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  282. .port_ops = &ahci_ops,
  283. },
  284. /* board_ahci_pi */
  285. {
  286. .sht = &ahci_sht,
  287. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  288. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  289. ATA_FLAG_SKIP_D2H_BSY | AHCI_FLAG_HONOR_PI,
  290. .pio_mask = 0x1f, /* pio0-4 */
  291. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  292. .port_ops = &ahci_ops,
  293. },
  294. /* board_ahci_vt8251 */
  295. {
  296. .sht = &ahci_sht,
  297. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  298. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  299. ATA_FLAG_SKIP_D2H_BSY |
  300. ATA_FLAG_HRST_TO_RESUME | AHCI_FLAG_NO_NCQ,
  301. .pio_mask = 0x1f, /* pio0-4 */
  302. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  303. .port_ops = &ahci_vt8251_ops,
  304. },
  305. /* board_ahci_ign_iferr */
  306. {
  307. .sht = &ahci_sht,
  308. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  309. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  310. ATA_FLAG_SKIP_D2H_BSY |
  311. AHCI_FLAG_IGN_IRQ_IF_ERR,
  312. .pio_mask = 0x1f, /* pio0-4 */
  313. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  314. .port_ops = &ahci_ops,
  315. },
  316. };
  317. static const struct pci_device_id ahci_pci_tbl[] = {
  318. /* Intel */
  319. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  320. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  321. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  322. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  323. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  324. { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
  325. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  326. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  327. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  328. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  329. { PCI_VDEVICE(INTEL, 0x2821), board_ahci_pi }, /* ICH8 */
  330. { PCI_VDEVICE(INTEL, 0x2822), board_ahci_pi }, /* ICH8 */
  331. { PCI_VDEVICE(INTEL, 0x2824), board_ahci_pi }, /* ICH8 */
  332. { PCI_VDEVICE(INTEL, 0x2829), board_ahci_pi }, /* ICH8M */
  333. { PCI_VDEVICE(INTEL, 0x282a), board_ahci_pi }, /* ICH8M */
  334. { PCI_VDEVICE(INTEL, 0x2922), board_ahci_pi }, /* ICH9 */
  335. { PCI_VDEVICE(INTEL, 0x2923), board_ahci_pi }, /* ICH9 */
  336. { PCI_VDEVICE(INTEL, 0x2924), board_ahci_pi }, /* ICH9 */
  337. { PCI_VDEVICE(INTEL, 0x2925), board_ahci_pi }, /* ICH9 */
  338. { PCI_VDEVICE(INTEL, 0x2927), board_ahci_pi }, /* ICH9 */
  339. { PCI_VDEVICE(INTEL, 0x2929), board_ahci_pi }, /* ICH9M */
  340. { PCI_VDEVICE(INTEL, 0x292a), board_ahci_pi }, /* ICH9M */
  341. { PCI_VDEVICE(INTEL, 0x292b), board_ahci_pi }, /* ICH9M */
  342. { PCI_VDEVICE(INTEL, 0x292f), board_ahci_pi }, /* ICH9M */
  343. { PCI_VDEVICE(INTEL, 0x294d), board_ahci_pi }, /* ICH9 */
  344. { PCI_VDEVICE(INTEL, 0x294e), board_ahci_pi }, /* ICH9M */
  345. /* JMicron */
  346. { PCI_VDEVICE(JMICRON, 0x2360), board_ahci_ign_iferr }, /* JMB360 */
  347. { PCI_VDEVICE(JMICRON, 0x2361), board_ahci_ign_iferr }, /* JMB361 */
  348. { PCI_VDEVICE(JMICRON, 0x2363), board_ahci_ign_iferr }, /* JMB363 */
  349. { PCI_VDEVICE(JMICRON, 0x2365), board_ahci_ign_iferr }, /* JMB365 */
  350. { PCI_VDEVICE(JMICRON, 0x2366), board_ahci_ign_iferr }, /* JMB366 */
  351. /* ATI */
  352. { PCI_VDEVICE(ATI, 0x4380), board_ahci }, /* ATI SB600 non-raid */
  353. { PCI_VDEVICE(ATI, 0x4381), board_ahci }, /* ATI SB600 raid */
  354. /* VIA */
  355. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  356. /* NVIDIA */
  357. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
  358. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
  359. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
  360. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
  361. { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci }, /* MCP65 */
  362. { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci }, /* MCP65 */
  363. { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci }, /* MCP65 */
  364. { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci }, /* MCP65 */
  365. { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci }, /* MCP67 */
  366. { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci }, /* MCP67 */
  367. { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci }, /* MCP67 */
  368. { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci }, /* MCP67 */
  369. { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
  370. { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
  371. { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
  372. { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
  373. { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
  374. { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
  375. { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
  376. { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
  377. /* SiS */
  378. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  379. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  380. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  381. /* Generic, PCI class code for AHCI */
  382. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  383. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
  384. { } /* terminate list */
  385. };
  386. static struct pci_driver ahci_pci_driver = {
  387. .name = DRV_NAME,
  388. .id_table = ahci_pci_tbl,
  389. .probe = ahci_init_one,
  390. .suspend = ahci_pci_device_suspend,
  391. .resume = ahci_pci_device_resume,
  392. .remove = ahci_remove_one,
  393. };
  394. static inline int ahci_nr_ports(u32 cap)
  395. {
  396. return (cap & 0x1f) + 1;
  397. }
  398. static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
  399. {
  400. return base + 0x100 + (port * 0x80);
  401. }
  402. static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int port)
  403. {
  404. return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
  405. }
  406. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  407. {
  408. unsigned int sc_reg;
  409. switch (sc_reg_in) {
  410. case SCR_STATUS: sc_reg = 0; break;
  411. case SCR_CONTROL: sc_reg = 1; break;
  412. case SCR_ERROR: sc_reg = 2; break;
  413. case SCR_ACTIVE: sc_reg = 3; break;
  414. default:
  415. return 0xffffffffU;
  416. }
  417. return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  418. }
  419. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  420. u32 val)
  421. {
  422. unsigned int sc_reg;
  423. switch (sc_reg_in) {
  424. case SCR_STATUS: sc_reg = 0; break;
  425. case SCR_CONTROL: sc_reg = 1; break;
  426. case SCR_ERROR: sc_reg = 2; break;
  427. case SCR_ACTIVE: sc_reg = 3; break;
  428. default:
  429. return;
  430. }
  431. writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  432. }
  433. static void ahci_start_engine(void __iomem *port_mmio)
  434. {
  435. u32 tmp;
  436. /* start DMA */
  437. tmp = readl(port_mmio + PORT_CMD);
  438. tmp |= PORT_CMD_START;
  439. writel(tmp, port_mmio + PORT_CMD);
  440. readl(port_mmio + PORT_CMD); /* flush */
  441. }
  442. static int ahci_stop_engine(void __iomem *port_mmio)
  443. {
  444. u32 tmp;
  445. tmp = readl(port_mmio + PORT_CMD);
  446. /* check if the HBA is idle */
  447. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  448. return 0;
  449. /* setting HBA to idle */
  450. tmp &= ~PORT_CMD_START;
  451. writel(tmp, port_mmio + PORT_CMD);
  452. /* wait for engine to stop. This could be as long as 500 msec */
  453. tmp = ata_wait_register(port_mmio + PORT_CMD,
  454. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  455. if (tmp & PORT_CMD_LIST_ON)
  456. return -EIO;
  457. return 0;
  458. }
  459. static void ahci_start_fis_rx(void __iomem *port_mmio, u32 cap,
  460. dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
  461. {
  462. u32 tmp;
  463. /* set FIS registers */
  464. if (cap & HOST_CAP_64)
  465. writel((cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  466. writel(cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  467. if (cap & HOST_CAP_64)
  468. writel((rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  469. writel(rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  470. /* enable FIS reception */
  471. tmp = readl(port_mmio + PORT_CMD);
  472. tmp |= PORT_CMD_FIS_RX;
  473. writel(tmp, port_mmio + PORT_CMD);
  474. /* flush */
  475. readl(port_mmio + PORT_CMD);
  476. }
  477. static int ahci_stop_fis_rx(void __iomem *port_mmio)
  478. {
  479. u32 tmp;
  480. /* disable FIS reception */
  481. tmp = readl(port_mmio + PORT_CMD);
  482. tmp &= ~PORT_CMD_FIS_RX;
  483. writel(tmp, port_mmio + PORT_CMD);
  484. /* wait for completion, spec says 500ms, give it 1000 */
  485. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  486. PORT_CMD_FIS_ON, 10, 1000);
  487. if (tmp & PORT_CMD_FIS_ON)
  488. return -EBUSY;
  489. return 0;
  490. }
  491. static void ahci_power_up(void __iomem *port_mmio, u32 cap)
  492. {
  493. u32 cmd;
  494. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  495. /* spin up device */
  496. if (cap & HOST_CAP_SSS) {
  497. cmd |= PORT_CMD_SPIN_UP;
  498. writel(cmd, port_mmio + PORT_CMD);
  499. }
  500. /* wake up link */
  501. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  502. }
  503. static void ahci_power_down(void __iomem *port_mmio, u32 cap)
  504. {
  505. u32 cmd, scontrol;
  506. if (!(cap & HOST_CAP_SSS))
  507. return;
  508. /* put device into listen mode, first set PxSCTL.DET to 0 */
  509. scontrol = readl(port_mmio + PORT_SCR_CTL);
  510. scontrol &= ~0xf;
  511. writel(scontrol, port_mmio + PORT_SCR_CTL);
  512. /* then set PxCMD.SUD to 0 */
  513. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  514. cmd &= ~PORT_CMD_SPIN_UP;
  515. writel(cmd, port_mmio + PORT_CMD);
  516. }
  517. static void ahci_init_port(void __iomem *port_mmio, u32 cap,
  518. dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
  519. {
  520. /* enable FIS reception */
  521. ahci_start_fis_rx(port_mmio, cap, cmd_slot_dma, rx_fis_dma);
  522. /* enable DMA */
  523. ahci_start_engine(port_mmio);
  524. }
  525. static int ahci_deinit_port(void __iomem *port_mmio, u32 cap, const char **emsg)
  526. {
  527. int rc;
  528. /* disable DMA */
  529. rc = ahci_stop_engine(port_mmio);
  530. if (rc) {
  531. *emsg = "failed to stop engine";
  532. return rc;
  533. }
  534. /* disable FIS reception */
  535. rc = ahci_stop_fis_rx(port_mmio);
  536. if (rc) {
  537. *emsg = "failed stop FIS RX";
  538. return rc;
  539. }
  540. return 0;
  541. }
  542. static int ahci_reset_controller(void __iomem *mmio, struct pci_dev *pdev)
  543. {
  544. u32 cap_save, impl_save, tmp;
  545. cap_save = readl(mmio + HOST_CAP);
  546. impl_save = readl(mmio + HOST_PORTS_IMPL);
  547. /* global controller reset */
  548. tmp = readl(mmio + HOST_CTL);
  549. if ((tmp & HOST_RESET) == 0) {
  550. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  551. readl(mmio + HOST_CTL); /* flush */
  552. }
  553. /* reset must complete within 1 second, or
  554. * the hardware should be considered fried.
  555. */
  556. ssleep(1);
  557. tmp = readl(mmio + HOST_CTL);
  558. if (tmp & HOST_RESET) {
  559. dev_printk(KERN_ERR, &pdev->dev,
  560. "controller reset failed (0x%x)\n", tmp);
  561. return -EIO;
  562. }
  563. /* turn on AHCI mode */
  564. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  565. (void) readl(mmio + HOST_CTL); /* flush */
  566. /* These write-once registers are normally cleared on reset.
  567. * Restore BIOS values... which we HOPE were present before
  568. * reset.
  569. */
  570. if (!impl_save) {
  571. impl_save = (1 << ahci_nr_ports(cap_save)) - 1;
  572. dev_printk(KERN_WARNING, &pdev->dev,
  573. "PORTS_IMPL is zero, forcing 0x%x\n", impl_save);
  574. }
  575. writel(cap_save, mmio + HOST_CAP);
  576. writel(impl_save, mmio + HOST_PORTS_IMPL);
  577. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  578. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  579. u16 tmp16;
  580. /* configure PCS */
  581. pci_read_config_word(pdev, 0x92, &tmp16);
  582. tmp16 |= 0xf;
  583. pci_write_config_word(pdev, 0x92, tmp16);
  584. }
  585. return 0;
  586. }
  587. static void ahci_init_controller(void __iomem *mmio, struct pci_dev *pdev,
  588. int n_ports, unsigned int port_flags,
  589. struct ahci_host_priv *hpriv)
  590. {
  591. int i, rc;
  592. u32 tmp;
  593. for (i = 0; i < n_ports; i++) {
  594. void __iomem *port_mmio = ahci_port_base(mmio, i);
  595. const char *emsg = NULL;
  596. if ((port_flags & AHCI_FLAG_HONOR_PI) &&
  597. !(hpriv->port_map & (1 << i)))
  598. continue;
  599. /* make sure port is not active */
  600. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  601. if (rc)
  602. dev_printk(KERN_WARNING, &pdev->dev,
  603. "%s (%d)\n", emsg, rc);
  604. /* clear SError */
  605. tmp = readl(port_mmio + PORT_SCR_ERR);
  606. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  607. writel(tmp, port_mmio + PORT_SCR_ERR);
  608. /* clear port IRQ */
  609. tmp = readl(port_mmio + PORT_IRQ_STAT);
  610. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  611. if (tmp)
  612. writel(tmp, port_mmio + PORT_IRQ_STAT);
  613. writel(1 << i, mmio + HOST_IRQ_STAT);
  614. }
  615. tmp = readl(mmio + HOST_CTL);
  616. VPRINTK("HOST_CTL 0x%x\n", tmp);
  617. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  618. tmp = readl(mmio + HOST_CTL);
  619. VPRINTK("HOST_CTL 0x%x\n", tmp);
  620. }
  621. static unsigned int ahci_dev_classify(struct ata_port *ap)
  622. {
  623. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  624. struct ata_taskfile tf;
  625. u32 tmp;
  626. tmp = readl(port_mmio + PORT_SIG);
  627. tf.lbah = (tmp >> 24) & 0xff;
  628. tf.lbam = (tmp >> 16) & 0xff;
  629. tf.lbal = (tmp >> 8) & 0xff;
  630. tf.nsect = (tmp) & 0xff;
  631. return ata_dev_classify(&tf);
  632. }
  633. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  634. u32 opts)
  635. {
  636. dma_addr_t cmd_tbl_dma;
  637. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  638. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  639. pp->cmd_slot[tag].status = 0;
  640. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  641. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  642. }
  643. static int ahci_clo(struct ata_port *ap)
  644. {
  645. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  646. struct ahci_host_priv *hpriv = ap->host->private_data;
  647. u32 tmp;
  648. if (!(hpriv->cap & HOST_CAP_CLO))
  649. return -EOPNOTSUPP;
  650. tmp = readl(port_mmio + PORT_CMD);
  651. tmp |= PORT_CMD_CLO;
  652. writel(tmp, port_mmio + PORT_CMD);
  653. tmp = ata_wait_register(port_mmio + PORT_CMD,
  654. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  655. if (tmp & PORT_CMD_CLO)
  656. return -EIO;
  657. return 0;
  658. }
  659. static int ahci_softreset(struct ata_port *ap, unsigned int *class)
  660. {
  661. struct ahci_port_priv *pp = ap->private_data;
  662. void __iomem *mmio = ap->host->mmio_base;
  663. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  664. const u32 cmd_fis_len = 5; /* five dwords */
  665. const char *reason = NULL;
  666. struct ata_taskfile tf;
  667. u32 tmp;
  668. u8 *fis;
  669. int rc;
  670. DPRINTK("ENTER\n");
  671. if (ata_port_offline(ap)) {
  672. DPRINTK("PHY reports no device\n");
  673. *class = ATA_DEV_NONE;
  674. return 0;
  675. }
  676. /* prepare for SRST (AHCI-1.1 10.4.1) */
  677. rc = ahci_stop_engine(port_mmio);
  678. if (rc) {
  679. reason = "failed to stop engine";
  680. goto fail_restart;
  681. }
  682. /* check BUSY/DRQ, perform Command List Override if necessary */
  683. if (ahci_check_status(ap) & (ATA_BUSY | ATA_DRQ)) {
  684. rc = ahci_clo(ap);
  685. if (rc == -EOPNOTSUPP) {
  686. reason = "port busy but CLO unavailable";
  687. goto fail_restart;
  688. } else if (rc) {
  689. reason = "port busy but CLO failed";
  690. goto fail_restart;
  691. }
  692. }
  693. /* restart engine */
  694. ahci_start_engine(port_mmio);
  695. ata_tf_init(ap->device, &tf);
  696. fis = pp->cmd_tbl;
  697. /* issue the first D2H Register FIS */
  698. ahci_fill_cmd_slot(pp, 0,
  699. cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
  700. tf.ctl |= ATA_SRST;
  701. ata_tf_to_fis(&tf, fis, 0);
  702. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  703. writel(1, port_mmio + PORT_CMD_ISSUE);
  704. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1, 1, 500);
  705. if (tmp & 0x1) {
  706. rc = -EIO;
  707. reason = "1st FIS failed";
  708. goto fail;
  709. }
  710. /* spec says at least 5us, but be generous and sleep for 1ms */
  711. msleep(1);
  712. /* issue the second D2H Register FIS */
  713. ahci_fill_cmd_slot(pp, 0, cmd_fis_len);
  714. tf.ctl &= ~ATA_SRST;
  715. ata_tf_to_fis(&tf, fis, 0);
  716. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  717. writel(1, port_mmio + PORT_CMD_ISSUE);
  718. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  719. /* spec mandates ">= 2ms" before checking status.
  720. * We wait 150ms, because that was the magic delay used for
  721. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  722. * between when the ATA command register is written, and then
  723. * status is checked. Because waiting for "a while" before
  724. * checking status is fine, post SRST, we perform this magic
  725. * delay here as well.
  726. */
  727. msleep(150);
  728. *class = ATA_DEV_NONE;
  729. if (ata_port_online(ap)) {
  730. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  731. rc = -EIO;
  732. reason = "device not ready";
  733. goto fail;
  734. }
  735. *class = ahci_dev_classify(ap);
  736. }
  737. DPRINTK("EXIT, class=%u\n", *class);
  738. return 0;
  739. fail_restart:
  740. ahci_start_engine(port_mmio);
  741. fail:
  742. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  743. return rc;
  744. }
  745. static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
  746. {
  747. struct ahci_port_priv *pp = ap->private_data;
  748. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  749. struct ata_taskfile tf;
  750. void __iomem *mmio = ap->host->mmio_base;
  751. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  752. int rc;
  753. DPRINTK("ENTER\n");
  754. ahci_stop_engine(port_mmio);
  755. /* clear D2H reception area to properly wait for D2H FIS */
  756. ata_tf_init(ap->device, &tf);
  757. tf.command = 0x80;
  758. ata_tf_to_fis(&tf, d2h_fis, 0);
  759. rc = sata_std_hardreset(ap, class);
  760. ahci_start_engine(port_mmio);
  761. if (rc == 0 && ata_port_online(ap))
  762. *class = ahci_dev_classify(ap);
  763. if (*class == ATA_DEV_UNKNOWN)
  764. *class = ATA_DEV_NONE;
  765. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  766. return rc;
  767. }
  768. static int ahci_vt8251_hardreset(struct ata_port *ap, unsigned int *class)
  769. {
  770. void __iomem *mmio = ap->host->mmio_base;
  771. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  772. int rc;
  773. DPRINTK("ENTER\n");
  774. ahci_stop_engine(port_mmio);
  775. rc = sata_port_hardreset(ap, sata_ehc_deb_timing(&ap->eh_context));
  776. /* vt8251 needs SError cleared for the port to operate */
  777. ahci_scr_write(ap, SCR_ERROR, ahci_scr_read(ap, SCR_ERROR));
  778. ahci_start_engine(port_mmio);
  779. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  780. /* vt8251 doesn't clear BSY on signature FIS reception,
  781. * request follow-up softreset.
  782. */
  783. return rc ?: -EAGAIN;
  784. }
  785. static void ahci_postreset(struct ata_port *ap, unsigned int *class)
  786. {
  787. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  788. u32 new_tmp, tmp;
  789. ata_std_postreset(ap, class);
  790. /* Make sure port's ATAPI bit is set appropriately */
  791. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  792. if (*class == ATA_DEV_ATAPI)
  793. new_tmp |= PORT_CMD_ATAPI;
  794. else
  795. new_tmp &= ~PORT_CMD_ATAPI;
  796. if (new_tmp != tmp) {
  797. writel(new_tmp, port_mmio + PORT_CMD);
  798. readl(port_mmio + PORT_CMD); /* flush */
  799. }
  800. }
  801. static u8 ahci_check_status(struct ata_port *ap)
  802. {
  803. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  804. return readl(mmio + PORT_TFDATA) & 0xFF;
  805. }
  806. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  807. {
  808. struct ahci_port_priv *pp = ap->private_data;
  809. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  810. ata_tf_from_fis(d2h_fis, tf);
  811. }
  812. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  813. {
  814. struct scatterlist *sg;
  815. struct ahci_sg *ahci_sg;
  816. unsigned int n_sg = 0;
  817. VPRINTK("ENTER\n");
  818. /*
  819. * Next, the S/G list.
  820. */
  821. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  822. ata_for_each_sg(sg, qc) {
  823. dma_addr_t addr = sg_dma_address(sg);
  824. u32 sg_len = sg_dma_len(sg);
  825. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  826. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  827. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  828. ahci_sg++;
  829. n_sg++;
  830. }
  831. return n_sg;
  832. }
  833. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  834. {
  835. struct ata_port *ap = qc->ap;
  836. struct ahci_port_priv *pp = ap->private_data;
  837. int is_atapi = is_atapi_taskfile(&qc->tf);
  838. void *cmd_tbl;
  839. u32 opts;
  840. const u32 cmd_fis_len = 5; /* five dwords */
  841. unsigned int n_elem;
  842. /*
  843. * Fill in command table information. First, the header,
  844. * a SATA Register - Host to Device command FIS.
  845. */
  846. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  847. ata_tf_to_fis(&qc->tf, cmd_tbl, 0);
  848. if (is_atapi) {
  849. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  850. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  851. }
  852. n_elem = 0;
  853. if (qc->flags & ATA_QCFLAG_DMAMAP)
  854. n_elem = ahci_fill_sg(qc, cmd_tbl);
  855. /*
  856. * Fill in command slot information.
  857. */
  858. opts = cmd_fis_len | n_elem << 16;
  859. if (qc->tf.flags & ATA_TFLAG_WRITE)
  860. opts |= AHCI_CMD_WRITE;
  861. if (is_atapi)
  862. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  863. ahci_fill_cmd_slot(pp, qc->tag, opts);
  864. }
  865. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  866. {
  867. struct ahci_port_priv *pp = ap->private_data;
  868. struct ata_eh_info *ehi = &ap->eh_info;
  869. unsigned int err_mask = 0, action = 0;
  870. struct ata_queued_cmd *qc;
  871. u32 serror;
  872. ata_ehi_clear_desc(ehi);
  873. /* AHCI needs SError cleared; otherwise, it might lock up */
  874. serror = ahci_scr_read(ap, SCR_ERROR);
  875. ahci_scr_write(ap, SCR_ERROR, serror);
  876. /* analyze @irq_stat */
  877. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  878. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  879. if (ap->flags & AHCI_FLAG_IGN_IRQ_IF_ERR)
  880. irq_stat &= ~PORT_IRQ_IF_ERR;
  881. if (irq_stat & PORT_IRQ_TF_ERR)
  882. err_mask |= AC_ERR_DEV;
  883. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  884. err_mask |= AC_ERR_HOST_BUS;
  885. action |= ATA_EH_SOFTRESET;
  886. }
  887. if (irq_stat & PORT_IRQ_IF_ERR) {
  888. err_mask |= AC_ERR_ATA_BUS;
  889. action |= ATA_EH_SOFTRESET;
  890. ata_ehi_push_desc(ehi, ", interface fatal error");
  891. }
  892. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  893. ata_ehi_hotplugged(ehi);
  894. ata_ehi_push_desc(ehi, ", %s", irq_stat & PORT_IRQ_CONNECT ?
  895. "connection status changed" : "PHY RDY changed");
  896. }
  897. if (irq_stat & PORT_IRQ_UNK_FIS) {
  898. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  899. err_mask |= AC_ERR_HSM;
  900. action |= ATA_EH_SOFTRESET;
  901. ata_ehi_push_desc(ehi, ", unknown FIS %08x %08x %08x %08x",
  902. unk[0], unk[1], unk[2], unk[3]);
  903. }
  904. /* okay, let's hand over to EH */
  905. ehi->serror |= serror;
  906. ehi->action |= action;
  907. qc = ata_qc_from_tag(ap, ap->active_tag);
  908. if (qc)
  909. qc->err_mask |= err_mask;
  910. else
  911. ehi->err_mask |= err_mask;
  912. if (irq_stat & PORT_IRQ_FREEZE)
  913. ata_port_freeze(ap);
  914. else
  915. ata_port_abort(ap);
  916. }
  917. static void ahci_host_intr(struct ata_port *ap)
  918. {
  919. void __iomem *mmio = ap->host->mmio_base;
  920. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  921. struct ata_eh_info *ehi = &ap->eh_info;
  922. struct ahci_port_priv *pp = ap->private_data;
  923. u32 status, qc_active;
  924. int rc, known_irq = 0;
  925. status = readl(port_mmio + PORT_IRQ_STAT);
  926. writel(status, port_mmio + PORT_IRQ_STAT);
  927. if (unlikely(status & PORT_IRQ_ERROR)) {
  928. ahci_error_intr(ap, status);
  929. return;
  930. }
  931. if (ap->sactive)
  932. qc_active = readl(port_mmio + PORT_SCR_ACT);
  933. else
  934. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  935. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  936. if (rc > 0)
  937. return;
  938. if (rc < 0) {
  939. ehi->err_mask |= AC_ERR_HSM;
  940. ehi->action |= ATA_EH_SOFTRESET;
  941. ata_port_freeze(ap);
  942. return;
  943. }
  944. /* hmmm... a spurious interupt */
  945. /* if !NCQ, ignore. No modern ATA device has broken HSM
  946. * implementation for non-NCQ commands.
  947. */
  948. if (!ap->sactive)
  949. return;
  950. if (status & PORT_IRQ_D2H_REG_FIS) {
  951. if (!pp->ncq_saw_d2h)
  952. ata_port_printk(ap, KERN_INFO,
  953. "D2H reg with I during NCQ, "
  954. "this message won't be printed again\n");
  955. pp->ncq_saw_d2h = 1;
  956. known_irq = 1;
  957. }
  958. if (status & PORT_IRQ_DMAS_FIS) {
  959. if (!pp->ncq_saw_dmas)
  960. ata_port_printk(ap, KERN_INFO,
  961. "DMAS FIS during NCQ, "
  962. "this message won't be printed again\n");
  963. pp->ncq_saw_dmas = 1;
  964. known_irq = 1;
  965. }
  966. if (status & PORT_IRQ_SDB_FIS &&
  967. pp->ncq_saw_spurious_sdb_cnt < 10) {
  968. /* SDB FIS containing spurious completions might be
  969. * dangerous, we need to know more about them. Print
  970. * more of it.
  971. */
  972. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  973. ata_port_printk(ap, KERN_INFO, "Spurious SDB FIS during NCQ "
  974. "issue=0x%x SAct=0x%x FIS=%08x:%08x%s\n",
  975. readl(port_mmio + PORT_CMD_ISSUE),
  976. readl(port_mmio + PORT_SCR_ACT),
  977. le32_to_cpu(f[0]), le32_to_cpu(f[1]),
  978. pp->ncq_saw_spurious_sdb_cnt < 10 ?
  979. "" : ", shutting up");
  980. pp->ncq_saw_spurious_sdb_cnt++;
  981. known_irq = 1;
  982. }
  983. if (!known_irq)
  984. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  985. "(irq_stat 0x%x active_tag 0x%x sactive 0x%x)\n",
  986. status, ap->active_tag, ap->sactive);
  987. }
  988. static void ahci_irq_clear(struct ata_port *ap)
  989. {
  990. /* TODO */
  991. }
  992. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  993. {
  994. struct ata_host *host = dev_instance;
  995. struct ahci_host_priv *hpriv;
  996. unsigned int i, handled = 0;
  997. void __iomem *mmio;
  998. u32 irq_stat, irq_ack = 0;
  999. VPRINTK("ENTER\n");
  1000. hpriv = host->private_data;
  1001. mmio = host->mmio_base;
  1002. /* sigh. 0xffffffff is a valid return from h/w */
  1003. irq_stat = readl(mmio + HOST_IRQ_STAT);
  1004. irq_stat &= hpriv->port_map;
  1005. if (!irq_stat)
  1006. return IRQ_NONE;
  1007. spin_lock(&host->lock);
  1008. for (i = 0; i < host->n_ports; i++) {
  1009. struct ata_port *ap;
  1010. if (!(irq_stat & (1 << i)))
  1011. continue;
  1012. ap = host->ports[i];
  1013. if (ap) {
  1014. ahci_host_intr(ap);
  1015. VPRINTK("port %u\n", i);
  1016. } else {
  1017. VPRINTK("port %u (no irq)\n", i);
  1018. if (ata_ratelimit())
  1019. dev_printk(KERN_WARNING, host->dev,
  1020. "interrupt on disabled port %u\n", i);
  1021. }
  1022. irq_ack |= (1 << i);
  1023. }
  1024. if (irq_ack) {
  1025. writel(irq_ack, mmio + HOST_IRQ_STAT);
  1026. handled = 1;
  1027. }
  1028. spin_unlock(&host->lock);
  1029. VPRINTK("EXIT\n");
  1030. return IRQ_RETVAL(handled);
  1031. }
  1032. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1033. {
  1034. struct ata_port *ap = qc->ap;
  1035. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  1036. if (qc->tf.protocol == ATA_PROT_NCQ)
  1037. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  1038. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  1039. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1040. return 0;
  1041. }
  1042. static void ahci_freeze(struct ata_port *ap)
  1043. {
  1044. void __iomem *mmio = ap->host->mmio_base;
  1045. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1046. /* turn IRQ off */
  1047. writel(0, port_mmio + PORT_IRQ_MASK);
  1048. }
  1049. static void ahci_thaw(struct ata_port *ap)
  1050. {
  1051. void __iomem *mmio = ap->host->mmio_base;
  1052. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1053. u32 tmp;
  1054. /* clear IRQ */
  1055. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1056. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1057. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1058. /* turn IRQ back on */
  1059. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  1060. }
  1061. static void ahci_error_handler(struct ata_port *ap)
  1062. {
  1063. void __iomem *mmio = ap->host->mmio_base;
  1064. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1065. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1066. /* restart engine */
  1067. ahci_stop_engine(port_mmio);
  1068. ahci_start_engine(port_mmio);
  1069. }
  1070. /* perform recovery */
  1071. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_hardreset,
  1072. ahci_postreset);
  1073. }
  1074. static void ahci_vt8251_error_handler(struct ata_port *ap)
  1075. {
  1076. void __iomem *mmio = ap->host->mmio_base;
  1077. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1078. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1079. /* restart engine */
  1080. ahci_stop_engine(port_mmio);
  1081. ahci_start_engine(port_mmio);
  1082. }
  1083. /* perform recovery */
  1084. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_vt8251_hardreset,
  1085. ahci_postreset);
  1086. }
  1087. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1088. {
  1089. struct ata_port *ap = qc->ap;
  1090. void __iomem *mmio = ap->host->mmio_base;
  1091. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1092. if (qc->flags & ATA_QCFLAG_FAILED)
  1093. qc->err_mask |= AC_ERR_OTHER;
  1094. if (qc->err_mask) {
  1095. /* make DMA engine forget about the failed command */
  1096. ahci_stop_engine(port_mmio);
  1097. ahci_start_engine(port_mmio);
  1098. }
  1099. }
  1100. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1101. {
  1102. struct ahci_host_priv *hpriv = ap->host->private_data;
  1103. struct ahci_port_priv *pp = ap->private_data;
  1104. void __iomem *mmio = ap->host->mmio_base;
  1105. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1106. const char *emsg = NULL;
  1107. int rc;
  1108. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  1109. if (rc == 0)
  1110. ahci_power_down(port_mmio, hpriv->cap);
  1111. else {
  1112. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  1113. ahci_init_port(port_mmio, hpriv->cap,
  1114. pp->cmd_slot_dma, pp->rx_fis_dma);
  1115. }
  1116. return rc;
  1117. }
  1118. static int ahci_port_resume(struct ata_port *ap)
  1119. {
  1120. struct ahci_port_priv *pp = ap->private_data;
  1121. struct ahci_host_priv *hpriv = ap->host->private_data;
  1122. void __iomem *mmio = ap->host->mmio_base;
  1123. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1124. ahci_power_up(port_mmio, hpriv->cap);
  1125. ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
  1126. return 0;
  1127. }
  1128. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1129. {
  1130. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1131. void __iomem *mmio = host->mmio_base;
  1132. u32 ctl;
  1133. if (mesg.event == PM_EVENT_SUSPEND) {
  1134. /* AHCI spec rev1.1 section 8.3.3:
  1135. * Software must disable interrupts prior to requesting a
  1136. * transition of the HBA to D3 state.
  1137. */
  1138. ctl = readl(mmio + HOST_CTL);
  1139. ctl &= ~HOST_IRQ_EN;
  1140. writel(ctl, mmio + HOST_CTL);
  1141. readl(mmio + HOST_CTL); /* flush */
  1142. }
  1143. return ata_pci_device_suspend(pdev, mesg);
  1144. }
  1145. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1146. {
  1147. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1148. struct ahci_host_priv *hpriv = host->private_data;
  1149. void __iomem *mmio = host->mmio_base;
  1150. int rc;
  1151. rc = ata_pci_device_do_resume(pdev);
  1152. if (rc)
  1153. return rc;
  1154. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1155. rc = ahci_reset_controller(mmio, pdev);
  1156. if (rc)
  1157. return rc;
  1158. ahci_init_controller(mmio, pdev, host->n_ports,
  1159. host->ports[0]->flags, hpriv);
  1160. }
  1161. ata_host_resume(host);
  1162. return 0;
  1163. }
  1164. static int ahci_port_start(struct ata_port *ap)
  1165. {
  1166. struct device *dev = ap->host->dev;
  1167. struct ahci_host_priv *hpriv = ap->host->private_data;
  1168. struct ahci_port_priv *pp;
  1169. void __iomem *mmio = ap->host->mmio_base;
  1170. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1171. void *mem;
  1172. dma_addr_t mem_dma;
  1173. int rc;
  1174. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  1175. if (!pp)
  1176. return -ENOMEM;
  1177. memset(pp, 0, sizeof(*pp));
  1178. rc = ata_pad_alloc(ap, dev);
  1179. if (rc) {
  1180. kfree(pp);
  1181. return rc;
  1182. }
  1183. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  1184. if (!mem) {
  1185. ata_pad_free(ap, dev);
  1186. kfree(pp);
  1187. return -ENOMEM;
  1188. }
  1189. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1190. /*
  1191. * First item in chunk of DMA memory: 32-slot command table,
  1192. * 32 bytes each in size
  1193. */
  1194. pp->cmd_slot = mem;
  1195. pp->cmd_slot_dma = mem_dma;
  1196. mem += AHCI_CMD_SLOT_SZ;
  1197. mem_dma += AHCI_CMD_SLOT_SZ;
  1198. /*
  1199. * Second item: Received-FIS area
  1200. */
  1201. pp->rx_fis = mem;
  1202. pp->rx_fis_dma = mem_dma;
  1203. mem += AHCI_RX_FIS_SZ;
  1204. mem_dma += AHCI_RX_FIS_SZ;
  1205. /*
  1206. * Third item: data area for storing a single command
  1207. * and its scatter-gather table
  1208. */
  1209. pp->cmd_tbl = mem;
  1210. pp->cmd_tbl_dma = mem_dma;
  1211. ap->private_data = pp;
  1212. /* power up port */
  1213. ahci_power_up(port_mmio, hpriv->cap);
  1214. /* initialize port */
  1215. ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
  1216. return 0;
  1217. }
  1218. static void ahci_port_stop(struct ata_port *ap)
  1219. {
  1220. struct device *dev = ap->host->dev;
  1221. struct ahci_host_priv *hpriv = ap->host->private_data;
  1222. struct ahci_port_priv *pp = ap->private_data;
  1223. void __iomem *mmio = ap->host->mmio_base;
  1224. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1225. const char *emsg = NULL;
  1226. int rc;
  1227. /* de-initialize port */
  1228. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  1229. if (rc)
  1230. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1231. ap->private_data = NULL;
  1232. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  1233. pp->cmd_slot, pp->cmd_slot_dma);
  1234. ata_pad_free(ap, dev);
  1235. kfree(pp);
  1236. }
  1237. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  1238. unsigned int port_idx)
  1239. {
  1240. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  1241. base = ahci_port_base_ul(base, port_idx);
  1242. VPRINTK("base now==0x%lx\n", base);
  1243. port->cmd_addr = base;
  1244. port->scr_addr = base + PORT_SCR;
  1245. VPRINTK("EXIT\n");
  1246. }
  1247. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  1248. {
  1249. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1250. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1251. void __iomem *mmio = probe_ent->mmio_base;
  1252. unsigned int i, cap_n_ports, using_dac;
  1253. int rc;
  1254. rc = ahci_reset_controller(mmio, pdev);
  1255. if (rc)
  1256. return rc;
  1257. hpriv->cap = readl(mmio + HOST_CAP);
  1258. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  1259. cap_n_ports = ahci_nr_ports(hpriv->cap);
  1260. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  1261. hpriv->cap, hpriv->port_map, cap_n_ports);
  1262. if (probe_ent->port_flags & AHCI_FLAG_HONOR_PI) {
  1263. unsigned int n_ports = cap_n_ports;
  1264. u32 port_map = hpriv->port_map;
  1265. int max_port = 0;
  1266. for (i = 0; i < AHCI_MAX_PORTS && n_ports; i++) {
  1267. if (port_map & (1 << i)) {
  1268. n_ports--;
  1269. port_map &= ~(1 << i);
  1270. max_port = i;
  1271. } else
  1272. probe_ent->dummy_port_mask |= 1 << i;
  1273. }
  1274. if (n_ports || port_map)
  1275. dev_printk(KERN_WARNING, &pdev->dev,
  1276. "nr_ports (%u) and implemented port map "
  1277. "(0x%x) don't match\n",
  1278. cap_n_ports, hpriv->port_map);
  1279. probe_ent->n_ports = max_port + 1;
  1280. } else
  1281. probe_ent->n_ports = cap_n_ports;
  1282. using_dac = hpriv->cap & HOST_CAP_64;
  1283. if (using_dac &&
  1284. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1285. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1286. if (rc) {
  1287. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1288. if (rc) {
  1289. dev_printk(KERN_ERR, &pdev->dev,
  1290. "64-bit DMA enable failed\n");
  1291. return rc;
  1292. }
  1293. }
  1294. } else {
  1295. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1296. if (rc) {
  1297. dev_printk(KERN_ERR, &pdev->dev,
  1298. "32-bit DMA enable failed\n");
  1299. return rc;
  1300. }
  1301. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1302. if (rc) {
  1303. dev_printk(KERN_ERR, &pdev->dev,
  1304. "32-bit consistent DMA enable failed\n");
  1305. return rc;
  1306. }
  1307. }
  1308. for (i = 0; i < probe_ent->n_ports; i++)
  1309. ahci_setup_port(&probe_ent->port[i], (unsigned long) mmio, i);
  1310. ahci_init_controller(mmio, pdev, probe_ent->n_ports,
  1311. probe_ent->port_flags, hpriv);
  1312. pci_set_master(pdev);
  1313. return 0;
  1314. }
  1315. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  1316. {
  1317. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1318. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1319. void __iomem *mmio = probe_ent->mmio_base;
  1320. u32 vers, cap, impl, speed;
  1321. const char *speed_s;
  1322. u16 cc;
  1323. const char *scc_s;
  1324. vers = readl(mmio + HOST_VERSION);
  1325. cap = hpriv->cap;
  1326. impl = hpriv->port_map;
  1327. speed = (cap >> 20) & 0xf;
  1328. if (speed == 1)
  1329. speed_s = "1.5";
  1330. else if (speed == 2)
  1331. speed_s = "3";
  1332. else
  1333. speed_s = "?";
  1334. pci_read_config_word(pdev, 0x0a, &cc);
  1335. if (cc == PCI_CLASS_STORAGE_IDE)
  1336. scc_s = "IDE";
  1337. else if (cc == PCI_CLASS_STORAGE_SATA)
  1338. scc_s = "SATA";
  1339. else if (cc == PCI_CLASS_STORAGE_RAID)
  1340. scc_s = "RAID";
  1341. else
  1342. scc_s = "unknown";
  1343. dev_printk(KERN_INFO, &pdev->dev,
  1344. "AHCI %02x%02x.%02x%02x "
  1345. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1346. ,
  1347. (vers >> 24) & 0xff,
  1348. (vers >> 16) & 0xff,
  1349. (vers >> 8) & 0xff,
  1350. vers & 0xff,
  1351. ((cap >> 8) & 0x1f) + 1,
  1352. (cap & 0x1f) + 1,
  1353. speed_s,
  1354. impl,
  1355. scc_s);
  1356. dev_printk(KERN_INFO, &pdev->dev,
  1357. "flags: "
  1358. "%s%s%s%s%s%s"
  1359. "%s%s%s%s%s%s%s\n"
  1360. ,
  1361. cap & (1 << 31) ? "64bit " : "",
  1362. cap & (1 << 30) ? "ncq " : "",
  1363. cap & (1 << 28) ? "ilck " : "",
  1364. cap & (1 << 27) ? "stag " : "",
  1365. cap & (1 << 26) ? "pm " : "",
  1366. cap & (1 << 25) ? "led " : "",
  1367. cap & (1 << 24) ? "clo " : "",
  1368. cap & (1 << 19) ? "nz " : "",
  1369. cap & (1 << 18) ? "only " : "",
  1370. cap & (1 << 17) ? "pmp " : "",
  1371. cap & (1 << 15) ? "pio " : "",
  1372. cap & (1 << 14) ? "slum " : "",
  1373. cap & (1 << 13) ? "part " : ""
  1374. );
  1375. }
  1376. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1377. {
  1378. static int printed_version;
  1379. struct ata_probe_ent *probe_ent = NULL;
  1380. struct ahci_host_priv *hpriv;
  1381. unsigned long base;
  1382. void __iomem *mmio_base;
  1383. unsigned int board_idx = (unsigned int) ent->driver_data;
  1384. int have_msi, pci_dev_busy = 0;
  1385. int rc;
  1386. VPRINTK("ENTER\n");
  1387. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1388. if (!printed_version++)
  1389. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1390. if (pdev->vendor == PCI_VENDOR_ID_JMICRON) {
  1391. /* Function 1 is the PATA controller except on the 368, where
  1392. we are not AHCI anyway */
  1393. if (PCI_FUNC(pdev->devfn))
  1394. return -ENODEV;
  1395. }
  1396. rc = pci_enable_device(pdev);
  1397. if (rc)
  1398. return rc;
  1399. rc = pci_request_regions(pdev, DRV_NAME);
  1400. if (rc) {
  1401. pci_dev_busy = 1;
  1402. goto err_out;
  1403. }
  1404. if (pci_enable_msi(pdev) == 0)
  1405. have_msi = 1;
  1406. else {
  1407. pci_intx(pdev, 1);
  1408. have_msi = 0;
  1409. }
  1410. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1411. if (probe_ent == NULL) {
  1412. rc = -ENOMEM;
  1413. goto err_out_msi;
  1414. }
  1415. memset(probe_ent, 0, sizeof(*probe_ent));
  1416. probe_ent->dev = pci_dev_to_dev(pdev);
  1417. INIT_LIST_HEAD(&probe_ent->node);
  1418. mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
  1419. if (mmio_base == NULL) {
  1420. rc = -ENOMEM;
  1421. goto err_out_free_ent;
  1422. }
  1423. base = (unsigned long) mmio_base;
  1424. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1425. if (!hpriv) {
  1426. rc = -ENOMEM;
  1427. goto err_out_iounmap;
  1428. }
  1429. memset(hpriv, 0, sizeof(*hpriv));
  1430. probe_ent->sht = ahci_port_info[board_idx].sht;
  1431. probe_ent->port_flags = ahci_port_info[board_idx].flags;
  1432. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  1433. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  1434. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  1435. probe_ent->irq = pdev->irq;
  1436. probe_ent->irq_flags = IRQF_SHARED;
  1437. probe_ent->mmio_base = mmio_base;
  1438. probe_ent->private_data = hpriv;
  1439. if (have_msi)
  1440. hpriv->flags |= AHCI_FLAG_MSI;
  1441. /* initialize adapter */
  1442. rc = ahci_host_init(probe_ent);
  1443. if (rc)
  1444. goto err_out_hpriv;
  1445. if (!(probe_ent->port_flags & AHCI_FLAG_NO_NCQ) &&
  1446. (hpriv->cap & HOST_CAP_NCQ))
  1447. probe_ent->port_flags |= ATA_FLAG_NCQ;
  1448. ahci_print_info(probe_ent);
  1449. /* FIXME: check ata_device_add return value */
  1450. ata_device_add(probe_ent);
  1451. kfree(probe_ent);
  1452. return 0;
  1453. err_out_hpriv:
  1454. kfree(hpriv);
  1455. err_out_iounmap:
  1456. pci_iounmap(pdev, mmio_base);
  1457. err_out_free_ent:
  1458. kfree(probe_ent);
  1459. err_out_msi:
  1460. if (have_msi)
  1461. pci_disable_msi(pdev);
  1462. else
  1463. pci_intx(pdev, 0);
  1464. pci_release_regions(pdev);
  1465. err_out:
  1466. if (!pci_dev_busy)
  1467. pci_disable_device(pdev);
  1468. return rc;
  1469. }
  1470. static void ahci_remove_one(struct pci_dev *pdev)
  1471. {
  1472. struct device *dev = pci_dev_to_dev(pdev);
  1473. struct ata_host *host = dev_get_drvdata(dev);
  1474. struct ahci_host_priv *hpriv = host->private_data;
  1475. ata_host_remove(host);
  1476. pci_iounmap(pdev, host->mmio_base);
  1477. if (hpriv->flags & AHCI_FLAG_MSI)
  1478. pci_disable_msi(pdev);
  1479. else
  1480. pci_intx(pdev, 0);
  1481. pci_release_regions(pdev);
  1482. pci_disable_device(pdev);
  1483. dev_set_drvdata(dev, NULL);
  1484. kfree(hpriv);
  1485. }
  1486. static int __init ahci_init(void)
  1487. {
  1488. return pci_register_driver(&ahci_pci_driver);
  1489. }
  1490. static void __exit ahci_exit(void)
  1491. {
  1492. pci_unregister_driver(&ahci_pci_driver);
  1493. }
  1494. MODULE_AUTHOR("Jeff Garzik");
  1495. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1496. MODULE_LICENSE("GPL");
  1497. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1498. MODULE_VERSION(DRV_VERSION);
  1499. module_init(ahci_init);
  1500. module_exit(ahci_exit);