ahci.c 47 KB

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