ahci.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  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 <linux/dmi.h>
  44. #include <scsi/scsi_host.h>
  45. #include <scsi/scsi_cmnd.h>
  46. #include <linux/libata.h>
  47. #define DRV_NAME "ahci"
  48. #define DRV_VERSION "3.0"
  49. enum {
  50. AHCI_PCI_BAR = 5,
  51. AHCI_MAX_PORTS = 32,
  52. AHCI_MAX_SG = 168, /* hardware max is 64K */
  53. AHCI_DMA_BOUNDARY = 0xffffffff,
  54. AHCI_USE_CLUSTERING = 1,
  55. AHCI_MAX_CMDS = 32,
  56. AHCI_CMD_SZ = 32,
  57. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  58. AHCI_RX_FIS_SZ = 256,
  59. AHCI_CMD_TBL_CDB = 0x40,
  60. AHCI_CMD_TBL_HDR_SZ = 0x80,
  61. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  62. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  63. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  64. AHCI_RX_FIS_SZ,
  65. AHCI_IRQ_ON_SG = (1 << 31),
  66. AHCI_CMD_ATAPI = (1 << 5),
  67. AHCI_CMD_WRITE = (1 << 6),
  68. AHCI_CMD_PREFETCH = (1 << 7),
  69. AHCI_CMD_RESET = (1 << 8),
  70. AHCI_CMD_CLR_BUSY = (1 << 10),
  71. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  72. RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
  73. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  74. board_ahci = 0,
  75. board_ahci_vt8251 = 1,
  76. board_ahci_ign_iferr = 2,
  77. board_ahci_sb600 = 3,
  78. board_ahci_mv = 4,
  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_PMP = (1 << 17), /* Port Multiplier support */
  92. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  93. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  94. HOST_CAP_SNTF = (1 << 29), /* SNotification register */
  95. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  96. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  97. /* registers for each SATA port */
  98. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  99. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  100. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  101. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  102. PORT_IRQ_STAT = 0x10, /* interrupt status */
  103. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  104. PORT_CMD = 0x18, /* port command */
  105. PORT_TFDATA = 0x20, /* taskfile data */
  106. PORT_SIG = 0x24, /* device TF signature */
  107. PORT_CMD_ISSUE = 0x38, /* command issue */
  108. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  109. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  110. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  111. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  112. PORT_SCR_NTF = 0x3c, /* SATA phy register: SNotification */
  113. /* PORT_IRQ_{STAT,MASK} bits */
  114. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  115. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  116. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  117. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  118. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  119. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  120. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  121. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  122. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  123. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  124. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  125. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  126. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  127. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  128. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  129. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  130. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  131. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  132. PORT_IRQ_IF_ERR |
  133. PORT_IRQ_CONNECT |
  134. PORT_IRQ_PHYRDY |
  135. PORT_IRQ_UNK_FIS |
  136. PORT_IRQ_BAD_PMP,
  137. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  138. PORT_IRQ_TF_ERR |
  139. PORT_IRQ_HBUS_DATA_ERR,
  140. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  141. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  142. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  143. /* PORT_CMD bits */
  144. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  145. PORT_CMD_PMP = (1 << 17), /* PMP attached */
  146. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  147. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  148. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  149. PORT_CMD_CLO = (1 << 3), /* Command list override */
  150. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  151. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  152. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  153. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  154. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  155. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  156. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  157. /* hpriv->flags bits */
  158. AHCI_HFLAG_NO_NCQ = (1 << 0),
  159. AHCI_HFLAG_IGN_IRQ_IF_ERR = (1 << 1), /* ignore IRQ_IF_ERR */
  160. AHCI_HFLAG_IGN_SERR_INTERNAL = (1 << 2), /* ignore SERR_INTERNAL */
  161. AHCI_HFLAG_32BIT_ONLY = (1 << 3), /* force 32bit */
  162. AHCI_HFLAG_MV_PATA = (1 << 4), /* PATA port */
  163. AHCI_HFLAG_NO_MSI = (1 << 5), /* no PCI MSI */
  164. AHCI_HFLAG_NO_PMP = (1 << 6), /* no PMP */
  165. /* ap->flags bits */
  166. AHCI_FLAG_NO_HOTPLUG = (1 << 24), /* ignore PxSERR.DIAG.N */
  167. AHCI_FLAG_COMMON = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  168. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  169. ATA_FLAG_ACPI_SATA | ATA_FLAG_AN,
  170. AHCI_LFLAG_COMMON = ATA_LFLAG_SKIP_D2H_BSY,
  171. };
  172. struct ahci_cmd_hdr {
  173. u32 opts;
  174. u32 status;
  175. u32 tbl_addr;
  176. u32 tbl_addr_hi;
  177. u32 reserved[4];
  178. };
  179. struct ahci_sg {
  180. u32 addr;
  181. u32 addr_hi;
  182. u32 reserved;
  183. u32 flags_size;
  184. };
  185. struct ahci_host_priv {
  186. unsigned int flags; /* AHCI_HFLAG_* */
  187. u32 cap; /* cap to use */
  188. u32 port_map; /* port map to use */
  189. u32 saved_cap; /* saved initial cap */
  190. u32 saved_port_map; /* saved initial port_map */
  191. };
  192. struct ahci_port_priv {
  193. struct ata_link *active_link;
  194. struct ahci_cmd_hdr *cmd_slot;
  195. dma_addr_t cmd_slot_dma;
  196. void *cmd_tbl;
  197. dma_addr_t cmd_tbl_dma;
  198. void *rx_fis;
  199. dma_addr_t rx_fis_dma;
  200. /* for NCQ spurious interrupt analysis */
  201. unsigned int ncq_saw_d2h:1;
  202. unsigned int ncq_saw_dmas:1;
  203. unsigned int ncq_saw_sdb:1;
  204. u32 intr_mask; /* interrupts to enable */
  205. };
  206. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
  207. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
  208. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  209. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  210. static void ahci_irq_clear(struct ata_port *ap);
  211. static int ahci_port_start(struct ata_port *ap);
  212. static void ahci_port_stop(struct ata_port *ap);
  213. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  214. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  215. static u8 ahci_check_status(struct ata_port *ap);
  216. static void ahci_freeze(struct ata_port *ap);
  217. static void ahci_thaw(struct ata_port *ap);
  218. static void ahci_pmp_attach(struct ata_port *ap);
  219. static void ahci_pmp_detach(struct ata_port *ap);
  220. static void ahci_error_handler(struct ata_port *ap);
  221. static void ahci_vt8251_error_handler(struct ata_port *ap);
  222. static void ahci_p5wdh_error_handler(struct ata_port *ap);
  223. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  224. static int ahci_port_resume(struct ata_port *ap);
  225. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl);
  226. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  227. u32 opts);
  228. #ifdef CONFIG_PM
  229. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  230. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  231. static int ahci_pci_device_resume(struct pci_dev *pdev);
  232. #endif
  233. static struct scsi_host_template ahci_sht = {
  234. .module = THIS_MODULE,
  235. .name = DRV_NAME,
  236. .ioctl = ata_scsi_ioctl,
  237. .queuecommand = ata_scsi_queuecmd,
  238. .change_queue_depth = ata_scsi_change_queue_depth,
  239. .can_queue = AHCI_MAX_CMDS - 1,
  240. .this_id = ATA_SHT_THIS_ID,
  241. .sg_tablesize = AHCI_MAX_SG,
  242. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  243. .emulated = ATA_SHT_EMULATED,
  244. .use_clustering = AHCI_USE_CLUSTERING,
  245. .proc_name = DRV_NAME,
  246. .dma_boundary = AHCI_DMA_BOUNDARY,
  247. .slave_configure = ata_scsi_slave_config,
  248. .slave_destroy = ata_scsi_slave_destroy,
  249. .bios_param = ata_std_bios_param,
  250. };
  251. static const struct ata_port_operations ahci_ops = {
  252. .check_status = ahci_check_status,
  253. .check_altstatus = ahci_check_status,
  254. .dev_select = ata_noop_dev_select,
  255. .tf_read = ahci_tf_read,
  256. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  257. .qc_prep = ahci_qc_prep,
  258. .qc_issue = ahci_qc_issue,
  259. .irq_clear = ahci_irq_clear,
  260. .scr_read = ahci_scr_read,
  261. .scr_write = ahci_scr_write,
  262. .freeze = ahci_freeze,
  263. .thaw = ahci_thaw,
  264. .error_handler = ahci_error_handler,
  265. .post_internal_cmd = ahci_post_internal_cmd,
  266. .pmp_attach = ahci_pmp_attach,
  267. .pmp_detach = ahci_pmp_detach,
  268. #ifdef CONFIG_PM
  269. .port_suspend = ahci_port_suspend,
  270. .port_resume = ahci_port_resume,
  271. #endif
  272. .port_start = ahci_port_start,
  273. .port_stop = ahci_port_stop,
  274. };
  275. static const struct ata_port_operations ahci_vt8251_ops = {
  276. .check_status = ahci_check_status,
  277. .check_altstatus = ahci_check_status,
  278. .dev_select = ata_noop_dev_select,
  279. .tf_read = ahci_tf_read,
  280. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  281. .qc_prep = ahci_qc_prep,
  282. .qc_issue = ahci_qc_issue,
  283. .irq_clear = ahci_irq_clear,
  284. .scr_read = ahci_scr_read,
  285. .scr_write = ahci_scr_write,
  286. .freeze = ahci_freeze,
  287. .thaw = ahci_thaw,
  288. .error_handler = ahci_vt8251_error_handler,
  289. .post_internal_cmd = ahci_post_internal_cmd,
  290. .pmp_attach = ahci_pmp_attach,
  291. .pmp_detach = ahci_pmp_detach,
  292. #ifdef CONFIG_PM
  293. .port_suspend = ahci_port_suspend,
  294. .port_resume = ahci_port_resume,
  295. #endif
  296. .port_start = ahci_port_start,
  297. .port_stop = ahci_port_stop,
  298. };
  299. static const struct ata_port_operations ahci_p5wdh_ops = {
  300. .check_status = ahci_check_status,
  301. .check_altstatus = ahci_check_status,
  302. .dev_select = ata_noop_dev_select,
  303. .tf_read = ahci_tf_read,
  304. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  305. .qc_prep = ahci_qc_prep,
  306. .qc_issue = ahci_qc_issue,
  307. .irq_clear = ahci_irq_clear,
  308. .scr_read = ahci_scr_read,
  309. .scr_write = ahci_scr_write,
  310. .freeze = ahci_freeze,
  311. .thaw = ahci_thaw,
  312. .error_handler = ahci_p5wdh_error_handler,
  313. .post_internal_cmd = ahci_post_internal_cmd,
  314. .pmp_attach = ahci_pmp_attach,
  315. .pmp_detach = ahci_pmp_detach,
  316. #ifdef CONFIG_PM
  317. .port_suspend = ahci_port_suspend,
  318. .port_resume = ahci_port_resume,
  319. #endif
  320. .port_start = ahci_port_start,
  321. .port_stop = ahci_port_stop,
  322. };
  323. #define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
  324. static const struct ata_port_info ahci_port_info[] = {
  325. /* board_ahci */
  326. {
  327. .flags = AHCI_FLAG_COMMON,
  328. .link_flags = AHCI_LFLAG_COMMON,
  329. .pio_mask = 0x1f, /* pio0-4 */
  330. .udma_mask = ATA_UDMA6,
  331. .port_ops = &ahci_ops,
  332. },
  333. /* board_ahci_vt8251 */
  334. {
  335. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_PMP),
  336. .flags = AHCI_FLAG_COMMON,
  337. .link_flags = AHCI_LFLAG_COMMON | ATA_LFLAG_HRST_TO_RESUME,
  338. .pio_mask = 0x1f, /* pio0-4 */
  339. .udma_mask = ATA_UDMA6,
  340. .port_ops = &ahci_vt8251_ops,
  341. },
  342. /* board_ahci_ign_iferr */
  343. {
  344. AHCI_HFLAGS (AHCI_HFLAG_IGN_IRQ_IF_ERR),
  345. .flags = AHCI_FLAG_COMMON,
  346. .link_flags = AHCI_LFLAG_COMMON,
  347. .pio_mask = 0x1f, /* pio0-4 */
  348. .udma_mask = ATA_UDMA6,
  349. .port_ops = &ahci_ops,
  350. },
  351. /* board_ahci_sb600 */
  352. {
  353. AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL |
  354. AHCI_HFLAG_32BIT_ONLY | AHCI_HFLAG_NO_PMP),
  355. .flags = AHCI_FLAG_COMMON,
  356. .link_flags = AHCI_LFLAG_COMMON,
  357. .pio_mask = 0x1f, /* pio0-4 */
  358. .udma_mask = ATA_UDMA6,
  359. .port_ops = &ahci_ops,
  360. },
  361. /* board_ahci_mv */
  362. {
  363. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_MSI |
  364. AHCI_HFLAG_MV_PATA),
  365. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  366. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
  367. .link_flags = AHCI_LFLAG_COMMON,
  368. .pio_mask = 0x1f, /* pio0-4 */
  369. .udma_mask = ATA_UDMA6,
  370. .port_ops = &ahci_ops,
  371. },
  372. };
  373. static const struct pci_device_id ahci_pci_tbl[] = {
  374. /* Intel */
  375. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  376. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  377. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  378. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  379. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  380. { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
  381. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  382. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  383. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  384. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  385. { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
  386. { PCI_VDEVICE(INTEL, 0x2822), board_ahci }, /* ICH8 */
  387. { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
  388. { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
  389. { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
  390. { PCI_VDEVICE(INTEL, 0x2922), board_ahci }, /* ICH9 */
  391. { PCI_VDEVICE(INTEL, 0x2923), board_ahci }, /* ICH9 */
  392. { PCI_VDEVICE(INTEL, 0x2924), board_ahci }, /* ICH9 */
  393. { PCI_VDEVICE(INTEL, 0x2925), board_ahci }, /* ICH9 */
  394. { PCI_VDEVICE(INTEL, 0x2927), board_ahci }, /* ICH9 */
  395. { PCI_VDEVICE(INTEL, 0x2929), board_ahci }, /* ICH9M */
  396. { PCI_VDEVICE(INTEL, 0x292a), board_ahci }, /* ICH9M */
  397. { PCI_VDEVICE(INTEL, 0x292b), board_ahci }, /* ICH9M */
  398. { PCI_VDEVICE(INTEL, 0x292c), board_ahci }, /* ICH9M */
  399. { PCI_VDEVICE(INTEL, 0x292f), board_ahci }, /* ICH9M */
  400. { PCI_VDEVICE(INTEL, 0x294d), board_ahci }, /* ICH9 */
  401. { PCI_VDEVICE(INTEL, 0x294e), board_ahci }, /* ICH9M */
  402. { PCI_VDEVICE(INTEL, 0x502a), board_ahci }, /* Tolapai */
  403. { PCI_VDEVICE(INTEL, 0x502b), board_ahci }, /* Tolapai */
  404. /* JMicron 360/1/3/5/6, match class to avoid IDE function */
  405. { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  406. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci_ign_iferr },
  407. /* ATI */
  408. { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600 }, /* ATI SB600 */
  409. { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb600 }, /* ATI SB700/800 */
  410. { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb600 }, /* ATI SB700/800 */
  411. { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb600 }, /* ATI SB700/800 */
  412. { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb600 }, /* ATI SB700/800 */
  413. { PCI_VDEVICE(ATI, 0x4394), board_ahci_sb600 }, /* ATI SB700/800 */
  414. { PCI_VDEVICE(ATI, 0x4395), board_ahci_sb600 }, /* ATI SB700/800 */
  415. /* VIA */
  416. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  417. { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251 }, /* VIA VT8251 */
  418. /* NVIDIA */
  419. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
  420. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
  421. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
  422. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
  423. { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci }, /* MCP65 */
  424. { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci }, /* MCP65 */
  425. { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci }, /* MCP65 */
  426. { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci }, /* MCP65 */
  427. { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci }, /* MCP67 */
  428. { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci }, /* MCP67 */
  429. { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci }, /* MCP67 */
  430. { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci }, /* MCP67 */
  431. { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
  432. { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
  433. { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
  434. { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
  435. { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
  436. { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
  437. { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
  438. { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
  439. { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci }, /* MCP73 */
  440. { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci }, /* MCP73 */
  441. { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci }, /* MCP73 */
  442. { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci }, /* MCP73 */
  443. { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci }, /* MCP73 */
  444. { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci }, /* MCP73 */
  445. { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci }, /* MCP73 */
  446. { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci }, /* MCP73 */
  447. { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci }, /* MCP73 */
  448. { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci }, /* MCP73 */
  449. { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci }, /* MCP73 */
  450. { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci }, /* MCP73 */
  451. { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci }, /* MCP77 */
  452. { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci }, /* MCP77 */
  453. { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci }, /* MCP77 */
  454. { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci }, /* MCP77 */
  455. { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci }, /* MCP77 */
  456. { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci }, /* MCP77 */
  457. { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci }, /* MCP77 */
  458. { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci }, /* MCP77 */
  459. { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci }, /* MCP77 */
  460. { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci }, /* MCP77 */
  461. { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci }, /* MCP77 */
  462. { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci }, /* MCP77 */
  463. { PCI_VDEVICE(NVIDIA, 0x0ab8), board_ahci }, /* MCP79 */
  464. { PCI_VDEVICE(NVIDIA, 0x0ab9), board_ahci }, /* MCP79 */
  465. { PCI_VDEVICE(NVIDIA, 0x0aba), board_ahci }, /* MCP79 */
  466. { PCI_VDEVICE(NVIDIA, 0x0abb), board_ahci }, /* MCP79 */
  467. { PCI_VDEVICE(NVIDIA, 0x0abc), board_ahci }, /* MCP79 */
  468. { PCI_VDEVICE(NVIDIA, 0x0abd), board_ahci }, /* MCP79 */
  469. { PCI_VDEVICE(NVIDIA, 0x0abe), board_ahci }, /* MCP79 */
  470. { PCI_VDEVICE(NVIDIA, 0x0abf), board_ahci }, /* MCP79 */
  471. /* SiS */
  472. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  473. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  474. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  475. /* Marvell */
  476. { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv }, /* 6145 */
  477. /* Generic, PCI class code for AHCI */
  478. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  479. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
  480. { } /* terminate list */
  481. };
  482. static struct pci_driver ahci_pci_driver = {
  483. .name = DRV_NAME,
  484. .id_table = ahci_pci_tbl,
  485. .probe = ahci_init_one,
  486. .remove = ata_pci_remove_one,
  487. #ifdef CONFIG_PM
  488. .suspend = ahci_pci_device_suspend,
  489. .resume = ahci_pci_device_resume,
  490. #endif
  491. };
  492. static inline int ahci_nr_ports(u32 cap)
  493. {
  494. return (cap & 0x1f) + 1;
  495. }
  496. static inline void __iomem *__ahci_port_base(struct ata_host *host,
  497. unsigned int port_no)
  498. {
  499. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  500. return mmio + 0x100 + (port_no * 0x80);
  501. }
  502. static inline void __iomem *ahci_port_base(struct ata_port *ap)
  503. {
  504. return __ahci_port_base(ap->host, ap->port_no);
  505. }
  506. /**
  507. * ahci_save_initial_config - Save and fixup initial config values
  508. * @pdev: target PCI device
  509. * @hpriv: host private area to store config values
  510. *
  511. * Some registers containing configuration info might be setup by
  512. * BIOS and might be cleared on reset. This function saves the
  513. * initial values of those registers into @hpriv such that they
  514. * can be restored after controller reset.
  515. *
  516. * If inconsistent, config values are fixed up by this function.
  517. *
  518. * LOCKING:
  519. * None.
  520. */
  521. static void ahci_save_initial_config(struct pci_dev *pdev,
  522. struct ahci_host_priv *hpriv)
  523. {
  524. void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
  525. u32 cap, port_map;
  526. int i;
  527. /* Values prefixed with saved_ are written back to host after
  528. * reset. Values without are used for driver operation.
  529. */
  530. hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
  531. hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
  532. /* some chips have errata preventing 64bit use */
  533. if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
  534. dev_printk(KERN_INFO, &pdev->dev,
  535. "controller can't do 64bit DMA, forcing 32bit\n");
  536. cap &= ~HOST_CAP_64;
  537. }
  538. if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
  539. dev_printk(KERN_INFO, &pdev->dev,
  540. "controller can't do NCQ, turning off CAP_NCQ\n");
  541. cap &= ~HOST_CAP_NCQ;
  542. }
  543. if ((cap && HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
  544. dev_printk(KERN_INFO, &pdev->dev,
  545. "controller can't do PMP, turning off CAP_PMP\n");
  546. cap &= ~HOST_CAP_PMP;
  547. }
  548. /*
  549. * Temporary Marvell 6145 hack: PATA port presence
  550. * is asserted through the standard AHCI port
  551. * presence register, as bit 4 (counting from 0)
  552. */
  553. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  554. dev_printk(KERN_ERR, &pdev->dev,
  555. "MV_AHCI HACK: port_map %x -> %x\n",
  556. hpriv->port_map,
  557. hpriv->port_map & 0xf);
  558. port_map &= 0xf;
  559. }
  560. /* cross check port_map and cap.n_ports */
  561. if (port_map) {
  562. u32 tmp_port_map = port_map;
  563. int n_ports = ahci_nr_ports(cap);
  564. for (i = 0; i < AHCI_MAX_PORTS && n_ports; i++) {
  565. if (tmp_port_map & (1 << i)) {
  566. n_ports--;
  567. tmp_port_map &= ~(1 << i);
  568. }
  569. }
  570. /* If n_ports and port_map are inconsistent, whine and
  571. * clear port_map and let it be generated from n_ports.
  572. */
  573. if (n_ports || tmp_port_map) {
  574. dev_printk(KERN_WARNING, &pdev->dev,
  575. "nr_ports (%u) and implemented port map "
  576. "(0x%x) don't match, using nr_ports\n",
  577. ahci_nr_ports(cap), port_map);
  578. port_map = 0;
  579. }
  580. }
  581. /* fabricate port_map from cap.nr_ports */
  582. if (!port_map) {
  583. port_map = (1 << ahci_nr_ports(cap)) - 1;
  584. dev_printk(KERN_WARNING, &pdev->dev,
  585. "forcing PORTS_IMPL to 0x%x\n", port_map);
  586. /* write the fixed up value to the PI register */
  587. hpriv->saved_port_map = port_map;
  588. }
  589. /* record values to use during operation */
  590. hpriv->cap = cap;
  591. hpriv->port_map = port_map;
  592. }
  593. /**
  594. * ahci_restore_initial_config - Restore initial config
  595. * @host: target ATA host
  596. *
  597. * Restore initial config stored by ahci_save_initial_config().
  598. *
  599. * LOCKING:
  600. * None.
  601. */
  602. static void ahci_restore_initial_config(struct ata_host *host)
  603. {
  604. struct ahci_host_priv *hpriv = host->private_data;
  605. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  606. writel(hpriv->saved_cap, mmio + HOST_CAP);
  607. writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
  608. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  609. }
  610. static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
  611. {
  612. static const int offset[] = {
  613. [SCR_STATUS] = PORT_SCR_STAT,
  614. [SCR_CONTROL] = PORT_SCR_CTL,
  615. [SCR_ERROR] = PORT_SCR_ERR,
  616. [SCR_ACTIVE] = PORT_SCR_ACT,
  617. [SCR_NOTIFICATION] = PORT_SCR_NTF,
  618. };
  619. struct ahci_host_priv *hpriv = ap->host->private_data;
  620. if (sc_reg < ARRAY_SIZE(offset) &&
  621. (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
  622. return offset[sc_reg];
  623. return 0;
  624. }
  625. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  626. {
  627. void __iomem *port_mmio = ahci_port_base(ap);
  628. int offset = ahci_scr_offset(ap, sc_reg);
  629. if (offset) {
  630. *val = readl(port_mmio + offset);
  631. return 0;
  632. }
  633. return -EINVAL;
  634. }
  635. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  636. {
  637. void __iomem *port_mmio = ahci_port_base(ap);
  638. int offset = ahci_scr_offset(ap, sc_reg);
  639. if (offset) {
  640. writel(val, port_mmio + offset);
  641. return 0;
  642. }
  643. return -EINVAL;
  644. }
  645. static void ahci_start_engine(struct ata_port *ap)
  646. {
  647. void __iomem *port_mmio = ahci_port_base(ap);
  648. u32 tmp;
  649. /* start DMA */
  650. tmp = readl(port_mmio + PORT_CMD);
  651. tmp |= PORT_CMD_START;
  652. writel(tmp, port_mmio + PORT_CMD);
  653. readl(port_mmio + PORT_CMD); /* flush */
  654. }
  655. static int ahci_stop_engine(struct ata_port *ap)
  656. {
  657. void __iomem *port_mmio = ahci_port_base(ap);
  658. u32 tmp;
  659. tmp = readl(port_mmio + PORT_CMD);
  660. /* check if the HBA is idle */
  661. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  662. return 0;
  663. /* setting HBA to idle */
  664. tmp &= ~PORT_CMD_START;
  665. writel(tmp, port_mmio + PORT_CMD);
  666. /* wait for engine to stop. This could be as long as 500 msec */
  667. tmp = ata_wait_register(port_mmio + PORT_CMD,
  668. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  669. if (tmp & PORT_CMD_LIST_ON)
  670. return -EIO;
  671. return 0;
  672. }
  673. static void ahci_start_fis_rx(struct ata_port *ap)
  674. {
  675. void __iomem *port_mmio = ahci_port_base(ap);
  676. struct ahci_host_priv *hpriv = ap->host->private_data;
  677. struct ahci_port_priv *pp = ap->private_data;
  678. u32 tmp;
  679. /* set FIS registers */
  680. if (hpriv->cap & HOST_CAP_64)
  681. writel((pp->cmd_slot_dma >> 16) >> 16,
  682. port_mmio + PORT_LST_ADDR_HI);
  683. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  684. if (hpriv->cap & HOST_CAP_64)
  685. writel((pp->rx_fis_dma >> 16) >> 16,
  686. port_mmio + PORT_FIS_ADDR_HI);
  687. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  688. /* enable FIS reception */
  689. tmp = readl(port_mmio + PORT_CMD);
  690. tmp |= PORT_CMD_FIS_RX;
  691. writel(tmp, port_mmio + PORT_CMD);
  692. /* flush */
  693. readl(port_mmio + PORT_CMD);
  694. }
  695. static int ahci_stop_fis_rx(struct ata_port *ap)
  696. {
  697. void __iomem *port_mmio = ahci_port_base(ap);
  698. u32 tmp;
  699. /* disable FIS reception */
  700. tmp = readl(port_mmio + PORT_CMD);
  701. tmp &= ~PORT_CMD_FIS_RX;
  702. writel(tmp, port_mmio + PORT_CMD);
  703. /* wait for completion, spec says 500ms, give it 1000 */
  704. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  705. PORT_CMD_FIS_ON, 10, 1000);
  706. if (tmp & PORT_CMD_FIS_ON)
  707. return -EBUSY;
  708. return 0;
  709. }
  710. static void ahci_power_up(struct ata_port *ap)
  711. {
  712. struct ahci_host_priv *hpriv = ap->host->private_data;
  713. void __iomem *port_mmio = ahci_port_base(ap);
  714. u32 cmd;
  715. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  716. /* spin up device */
  717. if (hpriv->cap & HOST_CAP_SSS) {
  718. cmd |= PORT_CMD_SPIN_UP;
  719. writel(cmd, port_mmio + PORT_CMD);
  720. }
  721. /* wake up link */
  722. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  723. }
  724. #ifdef CONFIG_PM
  725. static void ahci_power_down(struct ata_port *ap)
  726. {
  727. struct ahci_host_priv *hpriv = ap->host->private_data;
  728. void __iomem *port_mmio = ahci_port_base(ap);
  729. u32 cmd, scontrol;
  730. if (!(hpriv->cap & HOST_CAP_SSS))
  731. return;
  732. /* put device into listen mode, first set PxSCTL.DET to 0 */
  733. scontrol = readl(port_mmio + PORT_SCR_CTL);
  734. scontrol &= ~0xf;
  735. writel(scontrol, port_mmio + PORT_SCR_CTL);
  736. /* then set PxCMD.SUD to 0 */
  737. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  738. cmd &= ~PORT_CMD_SPIN_UP;
  739. writel(cmd, port_mmio + PORT_CMD);
  740. }
  741. #endif
  742. static void ahci_start_port(struct ata_port *ap)
  743. {
  744. /* enable FIS reception */
  745. ahci_start_fis_rx(ap);
  746. /* enable DMA */
  747. ahci_start_engine(ap);
  748. }
  749. static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
  750. {
  751. int rc;
  752. /* disable DMA */
  753. rc = ahci_stop_engine(ap);
  754. if (rc) {
  755. *emsg = "failed to stop engine";
  756. return rc;
  757. }
  758. /* disable FIS reception */
  759. rc = ahci_stop_fis_rx(ap);
  760. if (rc) {
  761. *emsg = "failed stop FIS RX";
  762. return rc;
  763. }
  764. return 0;
  765. }
  766. static int ahci_reset_controller(struct ata_host *host)
  767. {
  768. struct pci_dev *pdev = to_pci_dev(host->dev);
  769. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  770. u32 tmp;
  771. /* we must be in AHCI mode, before using anything
  772. * AHCI-specific, such as HOST_RESET.
  773. */
  774. tmp = readl(mmio + HOST_CTL);
  775. if (!(tmp & HOST_AHCI_EN))
  776. writel(tmp | HOST_AHCI_EN, mmio + HOST_CTL);
  777. /* global controller reset */
  778. if ((tmp & HOST_RESET) == 0) {
  779. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  780. readl(mmio + HOST_CTL); /* flush */
  781. }
  782. /* reset must complete within 1 second, or
  783. * the hardware should be considered fried.
  784. */
  785. ssleep(1);
  786. tmp = readl(mmio + HOST_CTL);
  787. if (tmp & HOST_RESET) {
  788. dev_printk(KERN_ERR, host->dev,
  789. "controller reset failed (0x%x)\n", tmp);
  790. return -EIO;
  791. }
  792. /* turn on AHCI mode */
  793. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  794. (void) readl(mmio + HOST_CTL); /* flush */
  795. /* some registers might be cleared on reset. restore initial values */
  796. ahci_restore_initial_config(host);
  797. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  798. u16 tmp16;
  799. /* configure PCS */
  800. pci_read_config_word(pdev, 0x92, &tmp16);
  801. tmp16 |= 0xf;
  802. pci_write_config_word(pdev, 0x92, tmp16);
  803. }
  804. return 0;
  805. }
  806. static void ahci_port_init(struct pci_dev *pdev, struct ata_port *ap,
  807. int port_no, void __iomem *mmio,
  808. void __iomem *port_mmio)
  809. {
  810. const char *emsg = NULL;
  811. int rc;
  812. u32 tmp;
  813. /* make sure port is not active */
  814. rc = ahci_deinit_port(ap, &emsg);
  815. if (rc)
  816. dev_printk(KERN_WARNING, &pdev->dev,
  817. "%s (%d)\n", emsg, rc);
  818. /* clear SError */
  819. tmp = readl(port_mmio + PORT_SCR_ERR);
  820. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  821. writel(tmp, port_mmio + PORT_SCR_ERR);
  822. /* clear port IRQ */
  823. tmp = readl(port_mmio + PORT_IRQ_STAT);
  824. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  825. if (tmp)
  826. writel(tmp, port_mmio + PORT_IRQ_STAT);
  827. writel(1 << port_no, mmio + HOST_IRQ_STAT);
  828. }
  829. static void ahci_init_controller(struct ata_host *host)
  830. {
  831. struct ahci_host_priv *hpriv = host->private_data;
  832. struct pci_dev *pdev = to_pci_dev(host->dev);
  833. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  834. int i;
  835. void __iomem *port_mmio;
  836. u32 tmp;
  837. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  838. port_mmio = __ahci_port_base(host, 4);
  839. writel(0, port_mmio + PORT_IRQ_MASK);
  840. /* clear port IRQ */
  841. tmp = readl(port_mmio + PORT_IRQ_STAT);
  842. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  843. if (tmp)
  844. writel(tmp, port_mmio + PORT_IRQ_STAT);
  845. }
  846. for (i = 0; i < host->n_ports; i++) {
  847. struct ata_port *ap = host->ports[i];
  848. port_mmio = ahci_port_base(ap);
  849. if (ata_port_is_dummy(ap))
  850. continue;
  851. ahci_port_init(pdev, ap, i, mmio, port_mmio);
  852. }
  853. tmp = readl(mmio + HOST_CTL);
  854. VPRINTK("HOST_CTL 0x%x\n", tmp);
  855. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  856. tmp = readl(mmio + HOST_CTL);
  857. VPRINTK("HOST_CTL 0x%x\n", tmp);
  858. }
  859. static unsigned int ahci_dev_classify(struct ata_port *ap)
  860. {
  861. void __iomem *port_mmio = ahci_port_base(ap);
  862. struct ata_taskfile tf;
  863. u32 tmp;
  864. tmp = readl(port_mmio + PORT_SIG);
  865. tf.lbah = (tmp >> 24) & 0xff;
  866. tf.lbam = (tmp >> 16) & 0xff;
  867. tf.lbal = (tmp >> 8) & 0xff;
  868. tf.nsect = (tmp) & 0xff;
  869. return ata_dev_classify(&tf);
  870. }
  871. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  872. u32 opts)
  873. {
  874. dma_addr_t cmd_tbl_dma;
  875. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  876. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  877. pp->cmd_slot[tag].status = 0;
  878. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  879. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  880. }
  881. static int ahci_kick_engine(struct ata_port *ap, int force_restart)
  882. {
  883. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  884. struct ahci_host_priv *hpriv = ap->host->private_data;
  885. u32 tmp;
  886. int busy, rc;
  887. /* do we need to kick the port? */
  888. busy = ahci_check_status(ap) & (ATA_BUSY | ATA_DRQ);
  889. if (!busy && !force_restart)
  890. return 0;
  891. /* stop engine */
  892. rc = ahci_stop_engine(ap);
  893. if (rc)
  894. goto out_restart;
  895. /* need to do CLO? */
  896. if (!busy) {
  897. rc = 0;
  898. goto out_restart;
  899. }
  900. if (!(hpriv->cap & HOST_CAP_CLO)) {
  901. rc = -EOPNOTSUPP;
  902. goto out_restart;
  903. }
  904. /* perform CLO */
  905. tmp = readl(port_mmio + PORT_CMD);
  906. tmp |= PORT_CMD_CLO;
  907. writel(tmp, port_mmio + PORT_CMD);
  908. rc = 0;
  909. tmp = ata_wait_register(port_mmio + PORT_CMD,
  910. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  911. if (tmp & PORT_CMD_CLO)
  912. rc = -EIO;
  913. /* restart engine */
  914. out_restart:
  915. ahci_start_engine(ap);
  916. return rc;
  917. }
  918. static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
  919. struct ata_taskfile *tf, int is_cmd, u16 flags,
  920. unsigned long timeout_msec)
  921. {
  922. const u32 cmd_fis_len = 5; /* five dwords */
  923. struct ahci_port_priv *pp = ap->private_data;
  924. void __iomem *port_mmio = ahci_port_base(ap);
  925. u8 *fis = pp->cmd_tbl;
  926. u32 tmp;
  927. /* prep the command */
  928. ata_tf_to_fis(tf, pmp, is_cmd, fis);
  929. ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
  930. /* issue & wait */
  931. writel(1, port_mmio + PORT_CMD_ISSUE);
  932. if (timeout_msec) {
  933. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1,
  934. 1, timeout_msec);
  935. if (tmp & 0x1) {
  936. ahci_kick_engine(ap, 1);
  937. return -EBUSY;
  938. }
  939. } else
  940. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  941. return 0;
  942. }
  943. static int ahci_do_softreset(struct ata_link *link, unsigned int *class,
  944. int pmp, unsigned long deadline)
  945. {
  946. struct ata_port *ap = link->ap;
  947. const char *reason = NULL;
  948. unsigned long now, msecs;
  949. struct ata_taskfile tf;
  950. int rc;
  951. DPRINTK("ENTER\n");
  952. if (ata_link_offline(link)) {
  953. DPRINTK("PHY reports no device\n");
  954. *class = ATA_DEV_NONE;
  955. return 0;
  956. }
  957. /* prepare for SRST (AHCI-1.1 10.4.1) */
  958. rc = ahci_kick_engine(ap, 1);
  959. if (rc)
  960. ata_link_printk(link, KERN_WARNING,
  961. "failed to reset engine (errno=%d)", rc);
  962. ata_tf_init(link->device, &tf);
  963. /* issue the first D2H Register FIS */
  964. msecs = 0;
  965. now = jiffies;
  966. if (time_after(now, deadline))
  967. msecs = jiffies_to_msecs(deadline - now);
  968. tf.ctl |= ATA_SRST;
  969. if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
  970. AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
  971. rc = -EIO;
  972. reason = "1st FIS failed";
  973. goto fail;
  974. }
  975. /* spec says at least 5us, but be generous and sleep for 1ms */
  976. msleep(1);
  977. /* issue the second D2H Register FIS */
  978. tf.ctl &= ~ATA_SRST;
  979. ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
  980. /* spec mandates ">= 2ms" before checking status.
  981. * We wait 150ms, because that was the magic delay used for
  982. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  983. * between when the ATA command register is written, and then
  984. * status is checked. Because waiting for "a while" before
  985. * checking status is fine, post SRST, we perform this magic
  986. * delay here as well.
  987. */
  988. msleep(150);
  989. rc = ata_wait_ready(ap, deadline);
  990. /* link occupied, -ENODEV too is an error */
  991. if (rc) {
  992. reason = "device not ready";
  993. goto fail;
  994. }
  995. *class = ahci_dev_classify(ap);
  996. DPRINTK("EXIT, class=%u\n", *class);
  997. return 0;
  998. fail:
  999. ata_link_printk(link, KERN_ERR, "softreset failed (%s)\n", reason);
  1000. return rc;
  1001. }
  1002. static int ahci_softreset(struct ata_link *link, unsigned int *class,
  1003. unsigned long deadline)
  1004. {
  1005. int pmp = 0;
  1006. if (link->ap->flags & ATA_FLAG_PMP)
  1007. pmp = SATA_PMP_CTRL_PORT;
  1008. return ahci_do_softreset(link, class, pmp, deadline);
  1009. }
  1010. static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  1011. unsigned long deadline)
  1012. {
  1013. struct ata_port *ap = link->ap;
  1014. struct ahci_port_priv *pp = ap->private_data;
  1015. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1016. struct ata_taskfile tf;
  1017. int rc;
  1018. DPRINTK("ENTER\n");
  1019. ahci_stop_engine(ap);
  1020. /* clear D2H reception area to properly wait for D2H FIS */
  1021. ata_tf_init(link->device, &tf);
  1022. tf.command = 0x80;
  1023. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  1024. rc = sata_std_hardreset(link, class, deadline);
  1025. ahci_start_engine(ap);
  1026. if (rc == 0 && ata_link_online(link))
  1027. *class = ahci_dev_classify(ap);
  1028. if (rc != -EAGAIN && *class == ATA_DEV_UNKNOWN)
  1029. *class = ATA_DEV_NONE;
  1030. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  1031. return rc;
  1032. }
  1033. static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
  1034. unsigned long deadline)
  1035. {
  1036. struct ata_port *ap = link->ap;
  1037. u32 serror;
  1038. int rc;
  1039. DPRINTK("ENTER\n");
  1040. ahci_stop_engine(ap);
  1041. rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
  1042. deadline);
  1043. /* vt8251 needs SError cleared for the port to operate */
  1044. ahci_scr_read(ap, SCR_ERROR, &serror);
  1045. ahci_scr_write(ap, SCR_ERROR, serror);
  1046. ahci_start_engine(ap);
  1047. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  1048. /* vt8251 doesn't clear BSY on signature FIS reception,
  1049. * request follow-up softreset.
  1050. */
  1051. return rc ?: -EAGAIN;
  1052. }
  1053. static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
  1054. unsigned long deadline)
  1055. {
  1056. struct ata_port *ap = link->ap;
  1057. struct ahci_port_priv *pp = ap->private_data;
  1058. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1059. struct ata_taskfile tf;
  1060. int rc;
  1061. ahci_stop_engine(ap);
  1062. /* clear D2H reception area to properly wait for D2H FIS */
  1063. ata_tf_init(link->device, &tf);
  1064. tf.command = 0x80;
  1065. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  1066. rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
  1067. deadline);
  1068. ahci_start_engine(ap);
  1069. if (rc || ata_link_offline(link))
  1070. return rc;
  1071. /* spec mandates ">= 2ms" before checking status */
  1072. msleep(150);
  1073. /* The pseudo configuration device on SIMG4726 attached to
  1074. * ASUS P5W-DH Deluxe doesn't send signature FIS after
  1075. * hardreset if no device is attached to the first downstream
  1076. * port && the pseudo device locks up on SRST w/ PMP==0. To
  1077. * work around this, wait for !BSY only briefly. If BSY isn't
  1078. * cleared, perform CLO and proceed to IDENTIFY (achieved by
  1079. * ATA_LFLAG_NO_SRST and ATA_LFLAG_ASSUME_ATA).
  1080. *
  1081. * Wait for two seconds. Devices attached to downstream port
  1082. * which can't process the following IDENTIFY after this will
  1083. * have to be reset again. For most cases, this should
  1084. * suffice while making probing snappish enough.
  1085. */
  1086. rc = ata_wait_ready(ap, jiffies + 2 * HZ);
  1087. if (rc)
  1088. ahci_kick_engine(ap, 0);
  1089. return 0;
  1090. }
  1091. static void ahci_postreset(struct ata_link *link, unsigned int *class)
  1092. {
  1093. struct ata_port *ap = link->ap;
  1094. void __iomem *port_mmio = ahci_port_base(ap);
  1095. u32 new_tmp, tmp;
  1096. ata_std_postreset(link, class);
  1097. /* Make sure port's ATAPI bit is set appropriately */
  1098. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  1099. if (*class == ATA_DEV_ATAPI)
  1100. new_tmp |= PORT_CMD_ATAPI;
  1101. else
  1102. new_tmp &= ~PORT_CMD_ATAPI;
  1103. if (new_tmp != tmp) {
  1104. writel(new_tmp, port_mmio + PORT_CMD);
  1105. readl(port_mmio + PORT_CMD); /* flush */
  1106. }
  1107. }
  1108. static int ahci_pmp_softreset(struct ata_link *link, unsigned int *class,
  1109. unsigned long deadline)
  1110. {
  1111. return ahci_do_softreset(link, class, link->pmp, deadline);
  1112. }
  1113. static u8 ahci_check_status(struct ata_port *ap)
  1114. {
  1115. void __iomem *mmio = ap->ioaddr.cmd_addr;
  1116. return readl(mmio + PORT_TFDATA) & 0xFF;
  1117. }
  1118. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  1119. {
  1120. struct ahci_port_priv *pp = ap->private_data;
  1121. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1122. ata_tf_from_fis(d2h_fis, tf);
  1123. }
  1124. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  1125. {
  1126. struct scatterlist *sg;
  1127. struct ahci_sg *ahci_sg;
  1128. unsigned int n_sg = 0;
  1129. VPRINTK("ENTER\n");
  1130. /*
  1131. * Next, the S/G list.
  1132. */
  1133. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  1134. ata_for_each_sg(sg, qc) {
  1135. dma_addr_t addr = sg_dma_address(sg);
  1136. u32 sg_len = sg_dma_len(sg);
  1137. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1138. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1139. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  1140. ahci_sg++;
  1141. n_sg++;
  1142. }
  1143. return n_sg;
  1144. }
  1145. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  1146. {
  1147. struct ata_port *ap = qc->ap;
  1148. struct ahci_port_priv *pp = ap->private_data;
  1149. int is_atapi = is_atapi_taskfile(&qc->tf);
  1150. void *cmd_tbl;
  1151. u32 opts;
  1152. const u32 cmd_fis_len = 5; /* five dwords */
  1153. unsigned int n_elem;
  1154. /*
  1155. * Fill in command table information. First, the header,
  1156. * a SATA Register - Host to Device command FIS.
  1157. */
  1158. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  1159. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
  1160. if (is_atapi) {
  1161. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  1162. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  1163. }
  1164. n_elem = 0;
  1165. if (qc->flags & ATA_QCFLAG_DMAMAP)
  1166. n_elem = ahci_fill_sg(qc, cmd_tbl);
  1167. /*
  1168. * Fill in command slot information.
  1169. */
  1170. opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
  1171. if (qc->tf.flags & ATA_TFLAG_WRITE)
  1172. opts |= AHCI_CMD_WRITE;
  1173. if (is_atapi)
  1174. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  1175. ahci_fill_cmd_slot(pp, qc->tag, opts);
  1176. }
  1177. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  1178. {
  1179. struct ahci_host_priv *hpriv = ap->host->private_data;
  1180. struct ahci_port_priv *pp = ap->private_data;
  1181. struct ata_eh_info *host_ehi = &ap->link.eh_info;
  1182. struct ata_link *link = NULL;
  1183. struct ata_queued_cmd *active_qc;
  1184. struct ata_eh_info *active_ehi;
  1185. u32 serror;
  1186. /* determine active link */
  1187. ata_port_for_each_link(link, ap)
  1188. if (ata_link_active(link))
  1189. break;
  1190. if (!link)
  1191. link = &ap->link;
  1192. active_qc = ata_qc_from_tag(ap, link->active_tag);
  1193. active_ehi = &link->eh_info;
  1194. /* record irq stat */
  1195. ata_ehi_clear_desc(host_ehi);
  1196. ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
  1197. /* AHCI needs SError cleared; otherwise, it might lock up */
  1198. ahci_scr_read(ap, SCR_ERROR, &serror);
  1199. ahci_scr_write(ap, SCR_ERROR, serror);
  1200. host_ehi->serror |= serror;
  1201. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  1202. if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
  1203. irq_stat &= ~PORT_IRQ_IF_ERR;
  1204. if (irq_stat & PORT_IRQ_TF_ERR) {
  1205. /* If qc is active, charge it; otherwise, the active
  1206. * link. There's no active qc on NCQ errors. It will
  1207. * be determined by EH by reading log page 10h.
  1208. */
  1209. if (active_qc)
  1210. active_qc->err_mask |= AC_ERR_DEV;
  1211. else
  1212. active_ehi->err_mask |= AC_ERR_DEV;
  1213. if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
  1214. host_ehi->serror &= ~SERR_INTERNAL;
  1215. }
  1216. if (irq_stat & PORT_IRQ_UNK_FIS) {
  1217. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  1218. active_ehi->err_mask |= AC_ERR_HSM;
  1219. active_ehi->action |= ATA_EH_SOFTRESET;
  1220. ata_ehi_push_desc(active_ehi,
  1221. "unknown FIS %08x %08x %08x %08x" ,
  1222. unk[0], unk[1], unk[2], unk[3]);
  1223. }
  1224. if (ap->nr_pmp_links && (irq_stat & PORT_IRQ_BAD_PMP)) {
  1225. active_ehi->err_mask |= AC_ERR_HSM;
  1226. active_ehi->action |= ATA_EH_SOFTRESET;
  1227. ata_ehi_push_desc(active_ehi, "incorrect PMP");
  1228. }
  1229. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  1230. host_ehi->err_mask |= AC_ERR_HOST_BUS;
  1231. host_ehi->action |= ATA_EH_SOFTRESET;
  1232. ata_ehi_push_desc(host_ehi, "host bus error");
  1233. }
  1234. if (irq_stat & PORT_IRQ_IF_ERR) {
  1235. host_ehi->err_mask |= AC_ERR_ATA_BUS;
  1236. host_ehi->action |= ATA_EH_SOFTRESET;
  1237. ata_ehi_push_desc(host_ehi, "interface fatal error");
  1238. }
  1239. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  1240. ata_ehi_hotplugged(host_ehi);
  1241. ata_ehi_push_desc(host_ehi, "%s",
  1242. irq_stat & PORT_IRQ_CONNECT ?
  1243. "connection status changed" : "PHY RDY changed");
  1244. }
  1245. /* okay, let's hand over to EH */
  1246. if (irq_stat & PORT_IRQ_FREEZE)
  1247. ata_port_freeze(ap);
  1248. else
  1249. ata_port_abort(ap);
  1250. }
  1251. static void ahci_port_intr(struct ata_port *ap)
  1252. {
  1253. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  1254. struct ata_eh_info *ehi = &ap->link.eh_info;
  1255. struct ahci_port_priv *pp = ap->private_data;
  1256. struct ahci_host_priv *hpriv = ap->host->private_data;
  1257. int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
  1258. u32 status, qc_active;
  1259. int rc, known_irq = 0;
  1260. status = readl(port_mmio + PORT_IRQ_STAT);
  1261. writel(status, port_mmio + PORT_IRQ_STAT);
  1262. /* ignore BAD_PMP while resetting */
  1263. if (unlikely(resetting))
  1264. status &= ~PORT_IRQ_BAD_PMP;
  1265. if (unlikely(status & PORT_IRQ_ERROR)) {
  1266. ahci_error_intr(ap, status);
  1267. return;
  1268. }
  1269. if (status & PORT_IRQ_SDB_FIS) {
  1270. /* If SNotification is available, leave notification
  1271. * handling to sata_async_notification(). If not,
  1272. * emulate it by snooping SDB FIS RX area.
  1273. *
  1274. * Snooping FIS RX area is probably cheaper than
  1275. * poking SNotification but some constrollers which
  1276. * implement SNotification, ICH9 for example, don't
  1277. * store AN SDB FIS into receive area.
  1278. */
  1279. if (hpriv->cap & HOST_CAP_SNTF)
  1280. sata_async_notification(ap);
  1281. else {
  1282. /* If the 'N' bit in word 0 of the FIS is set,
  1283. * we just received asynchronous notification.
  1284. * Tell libata about it.
  1285. */
  1286. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1287. u32 f0 = le32_to_cpu(f[0]);
  1288. if (f0 & (1 << 15))
  1289. sata_async_notification(ap);
  1290. }
  1291. }
  1292. /* pp->active_link is valid iff any command is in flight */
  1293. if (ap->qc_active && pp->active_link->sactive)
  1294. qc_active = readl(port_mmio + PORT_SCR_ACT);
  1295. else
  1296. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  1297. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  1298. /* If resetting, spurious or invalid completions are expected,
  1299. * return unconditionally.
  1300. */
  1301. if (resetting)
  1302. return;
  1303. if (rc > 0)
  1304. return;
  1305. if (rc < 0) {
  1306. ehi->err_mask |= AC_ERR_HSM;
  1307. ehi->action |= ATA_EH_SOFTRESET;
  1308. ata_port_freeze(ap);
  1309. return;
  1310. }
  1311. /* hmmm... a spurious interrupt */
  1312. /* if !NCQ, ignore. No modern ATA device has broken HSM
  1313. * implementation for non-NCQ commands.
  1314. */
  1315. if (!ap->link.sactive)
  1316. return;
  1317. if (status & PORT_IRQ_D2H_REG_FIS) {
  1318. if (!pp->ncq_saw_d2h)
  1319. ata_port_printk(ap, KERN_INFO,
  1320. "D2H reg with I during NCQ, "
  1321. "this message won't be printed again\n");
  1322. pp->ncq_saw_d2h = 1;
  1323. known_irq = 1;
  1324. }
  1325. if (status & PORT_IRQ_DMAS_FIS) {
  1326. if (!pp->ncq_saw_dmas)
  1327. ata_port_printk(ap, KERN_INFO,
  1328. "DMAS FIS during NCQ, "
  1329. "this message won't be printed again\n");
  1330. pp->ncq_saw_dmas = 1;
  1331. known_irq = 1;
  1332. }
  1333. if (status & PORT_IRQ_SDB_FIS) {
  1334. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1335. if (le32_to_cpu(f[1])) {
  1336. /* SDB FIS containing spurious completions
  1337. * might be dangerous, whine and fail commands
  1338. * with HSM violation. EH will turn off NCQ
  1339. * after several such failures.
  1340. */
  1341. ata_ehi_push_desc(ehi,
  1342. "spurious completions during NCQ "
  1343. "issue=0x%x SAct=0x%x FIS=%08x:%08x",
  1344. readl(port_mmio + PORT_CMD_ISSUE),
  1345. readl(port_mmio + PORT_SCR_ACT),
  1346. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1347. ehi->err_mask |= AC_ERR_HSM;
  1348. ehi->action |= ATA_EH_SOFTRESET;
  1349. ata_port_freeze(ap);
  1350. } else {
  1351. if (!pp->ncq_saw_sdb)
  1352. ata_port_printk(ap, KERN_INFO,
  1353. "spurious SDB FIS %08x:%08x during NCQ, "
  1354. "this message won't be printed again\n",
  1355. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1356. pp->ncq_saw_sdb = 1;
  1357. }
  1358. known_irq = 1;
  1359. }
  1360. if (!known_irq)
  1361. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  1362. "(irq_stat 0x%x active_tag 0x%x sactive 0x%x)\n",
  1363. status, ap->link.active_tag, ap->link.sactive);
  1364. }
  1365. static void ahci_irq_clear(struct ata_port *ap)
  1366. {
  1367. /* TODO */
  1368. }
  1369. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  1370. {
  1371. struct ata_host *host = dev_instance;
  1372. struct ahci_host_priv *hpriv;
  1373. unsigned int i, handled = 0;
  1374. void __iomem *mmio;
  1375. u32 irq_stat, irq_ack = 0;
  1376. VPRINTK("ENTER\n");
  1377. hpriv = host->private_data;
  1378. mmio = host->iomap[AHCI_PCI_BAR];
  1379. /* sigh. 0xffffffff is a valid return from h/w */
  1380. irq_stat = readl(mmio + HOST_IRQ_STAT);
  1381. irq_stat &= hpriv->port_map;
  1382. if (!irq_stat)
  1383. return IRQ_NONE;
  1384. spin_lock(&host->lock);
  1385. for (i = 0; i < host->n_ports; i++) {
  1386. struct ata_port *ap;
  1387. if (!(irq_stat & (1 << i)))
  1388. continue;
  1389. ap = host->ports[i];
  1390. if (ap) {
  1391. ahci_port_intr(ap);
  1392. VPRINTK("port %u\n", i);
  1393. } else {
  1394. VPRINTK("port %u (no irq)\n", i);
  1395. if (ata_ratelimit())
  1396. dev_printk(KERN_WARNING, host->dev,
  1397. "interrupt on disabled port %u\n", i);
  1398. }
  1399. irq_ack |= (1 << i);
  1400. }
  1401. if (irq_ack) {
  1402. writel(irq_ack, mmio + HOST_IRQ_STAT);
  1403. handled = 1;
  1404. }
  1405. spin_unlock(&host->lock);
  1406. VPRINTK("EXIT\n");
  1407. return IRQ_RETVAL(handled);
  1408. }
  1409. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1410. {
  1411. struct ata_port *ap = qc->ap;
  1412. void __iomem *port_mmio = ahci_port_base(ap);
  1413. struct ahci_port_priv *pp = ap->private_data;
  1414. /* Keep track of the currently active link. It will be used
  1415. * in completion path to determine whether NCQ phase is in
  1416. * progress.
  1417. */
  1418. pp->active_link = qc->dev->link;
  1419. if (qc->tf.protocol == ATA_PROT_NCQ)
  1420. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  1421. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  1422. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1423. return 0;
  1424. }
  1425. static void ahci_freeze(struct ata_port *ap)
  1426. {
  1427. void __iomem *port_mmio = ahci_port_base(ap);
  1428. /* turn IRQ off */
  1429. writel(0, port_mmio + PORT_IRQ_MASK);
  1430. }
  1431. static void ahci_thaw(struct ata_port *ap)
  1432. {
  1433. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1434. void __iomem *port_mmio = ahci_port_base(ap);
  1435. u32 tmp;
  1436. struct ahci_port_priv *pp = ap->private_data;
  1437. /* clear IRQ */
  1438. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1439. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1440. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1441. /* turn IRQ back on */
  1442. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1443. }
  1444. static void ahci_error_handler(struct ata_port *ap)
  1445. {
  1446. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1447. /* restart engine */
  1448. ahci_stop_engine(ap);
  1449. ahci_start_engine(ap);
  1450. }
  1451. /* perform recovery */
  1452. sata_pmp_do_eh(ap, ata_std_prereset, ahci_softreset,
  1453. ahci_hardreset, ahci_postreset,
  1454. sata_pmp_std_prereset, ahci_pmp_softreset,
  1455. sata_pmp_std_hardreset, sata_pmp_std_postreset);
  1456. }
  1457. static void ahci_vt8251_error_handler(struct ata_port *ap)
  1458. {
  1459. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1460. /* restart engine */
  1461. ahci_stop_engine(ap);
  1462. ahci_start_engine(ap);
  1463. }
  1464. /* perform recovery */
  1465. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_vt8251_hardreset,
  1466. ahci_postreset);
  1467. }
  1468. static void ahci_p5wdh_error_handler(struct ata_port *ap)
  1469. {
  1470. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1471. /* restart engine */
  1472. ahci_stop_engine(ap);
  1473. ahci_start_engine(ap);
  1474. }
  1475. /* perform recovery */
  1476. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_p5wdh_hardreset,
  1477. ahci_postreset);
  1478. }
  1479. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1480. {
  1481. struct ata_port *ap = qc->ap;
  1482. /* make DMA engine forget about the failed command */
  1483. if (qc->flags & ATA_QCFLAG_FAILED)
  1484. ahci_kick_engine(ap, 1);
  1485. }
  1486. static void ahci_pmp_attach(struct ata_port *ap)
  1487. {
  1488. void __iomem *port_mmio = ahci_port_base(ap);
  1489. struct ahci_port_priv *pp = ap->private_data;
  1490. u32 cmd;
  1491. cmd = readl(port_mmio + PORT_CMD);
  1492. cmd |= PORT_CMD_PMP;
  1493. writel(cmd, port_mmio + PORT_CMD);
  1494. pp->intr_mask |= PORT_IRQ_BAD_PMP;
  1495. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1496. }
  1497. static void ahci_pmp_detach(struct ata_port *ap)
  1498. {
  1499. void __iomem *port_mmio = ahci_port_base(ap);
  1500. struct ahci_port_priv *pp = ap->private_data;
  1501. u32 cmd;
  1502. cmd = readl(port_mmio + PORT_CMD);
  1503. cmd &= ~PORT_CMD_PMP;
  1504. writel(cmd, port_mmio + PORT_CMD);
  1505. pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
  1506. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1507. }
  1508. static int ahci_port_resume(struct ata_port *ap)
  1509. {
  1510. ahci_power_up(ap);
  1511. ahci_start_port(ap);
  1512. if (ap->nr_pmp_links)
  1513. ahci_pmp_attach(ap);
  1514. else
  1515. ahci_pmp_detach(ap);
  1516. return 0;
  1517. }
  1518. #ifdef CONFIG_PM
  1519. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1520. {
  1521. const char *emsg = NULL;
  1522. int rc;
  1523. rc = ahci_deinit_port(ap, &emsg);
  1524. if (rc == 0)
  1525. ahci_power_down(ap);
  1526. else {
  1527. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  1528. ahci_start_port(ap);
  1529. }
  1530. return rc;
  1531. }
  1532. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1533. {
  1534. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1535. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1536. u32 ctl;
  1537. if (mesg.event == PM_EVENT_SUSPEND) {
  1538. /* AHCI spec rev1.1 section 8.3.3:
  1539. * Software must disable interrupts prior to requesting a
  1540. * transition of the HBA to D3 state.
  1541. */
  1542. ctl = readl(mmio + HOST_CTL);
  1543. ctl &= ~HOST_IRQ_EN;
  1544. writel(ctl, mmio + HOST_CTL);
  1545. readl(mmio + HOST_CTL); /* flush */
  1546. }
  1547. return ata_pci_device_suspend(pdev, mesg);
  1548. }
  1549. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1550. {
  1551. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1552. int rc;
  1553. rc = ata_pci_device_do_resume(pdev);
  1554. if (rc)
  1555. return rc;
  1556. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1557. rc = ahci_reset_controller(host);
  1558. if (rc)
  1559. return rc;
  1560. ahci_init_controller(host);
  1561. }
  1562. ata_host_resume(host);
  1563. return 0;
  1564. }
  1565. #endif
  1566. static int ahci_port_start(struct ata_port *ap)
  1567. {
  1568. struct device *dev = ap->host->dev;
  1569. struct ahci_port_priv *pp;
  1570. void *mem;
  1571. dma_addr_t mem_dma;
  1572. int rc;
  1573. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1574. if (!pp)
  1575. return -ENOMEM;
  1576. rc = ata_pad_alloc(ap, dev);
  1577. if (rc)
  1578. return rc;
  1579. mem = dmam_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma,
  1580. GFP_KERNEL);
  1581. if (!mem)
  1582. return -ENOMEM;
  1583. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1584. /*
  1585. * First item in chunk of DMA memory: 32-slot command table,
  1586. * 32 bytes each in size
  1587. */
  1588. pp->cmd_slot = mem;
  1589. pp->cmd_slot_dma = mem_dma;
  1590. mem += AHCI_CMD_SLOT_SZ;
  1591. mem_dma += AHCI_CMD_SLOT_SZ;
  1592. /*
  1593. * Second item: Received-FIS area
  1594. */
  1595. pp->rx_fis = mem;
  1596. pp->rx_fis_dma = mem_dma;
  1597. mem += AHCI_RX_FIS_SZ;
  1598. mem_dma += AHCI_RX_FIS_SZ;
  1599. /*
  1600. * Third item: data area for storing a single command
  1601. * and its scatter-gather table
  1602. */
  1603. pp->cmd_tbl = mem;
  1604. pp->cmd_tbl_dma = mem_dma;
  1605. /*
  1606. * Save off initial list of interrupts to be enabled.
  1607. * This could be changed later
  1608. */
  1609. pp->intr_mask = DEF_PORT_IRQ;
  1610. ap->private_data = pp;
  1611. /* engage engines, captain */
  1612. return ahci_port_resume(ap);
  1613. }
  1614. static void ahci_port_stop(struct ata_port *ap)
  1615. {
  1616. const char *emsg = NULL;
  1617. int rc;
  1618. /* de-initialize port */
  1619. rc = ahci_deinit_port(ap, &emsg);
  1620. if (rc)
  1621. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1622. }
  1623. static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
  1624. {
  1625. int rc;
  1626. if (using_dac &&
  1627. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1628. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1629. if (rc) {
  1630. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1631. if (rc) {
  1632. dev_printk(KERN_ERR, &pdev->dev,
  1633. "64-bit DMA enable failed\n");
  1634. return rc;
  1635. }
  1636. }
  1637. } else {
  1638. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1639. if (rc) {
  1640. dev_printk(KERN_ERR, &pdev->dev,
  1641. "32-bit DMA enable failed\n");
  1642. return rc;
  1643. }
  1644. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1645. if (rc) {
  1646. dev_printk(KERN_ERR, &pdev->dev,
  1647. "32-bit consistent DMA enable failed\n");
  1648. return rc;
  1649. }
  1650. }
  1651. return 0;
  1652. }
  1653. static void ahci_print_info(struct ata_host *host)
  1654. {
  1655. struct ahci_host_priv *hpriv = host->private_data;
  1656. struct pci_dev *pdev = to_pci_dev(host->dev);
  1657. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1658. u32 vers, cap, impl, speed;
  1659. const char *speed_s;
  1660. u16 cc;
  1661. const char *scc_s;
  1662. vers = readl(mmio + HOST_VERSION);
  1663. cap = hpriv->cap;
  1664. impl = hpriv->port_map;
  1665. speed = (cap >> 20) & 0xf;
  1666. if (speed == 1)
  1667. speed_s = "1.5";
  1668. else if (speed == 2)
  1669. speed_s = "3";
  1670. else
  1671. speed_s = "?";
  1672. pci_read_config_word(pdev, 0x0a, &cc);
  1673. if (cc == PCI_CLASS_STORAGE_IDE)
  1674. scc_s = "IDE";
  1675. else if (cc == PCI_CLASS_STORAGE_SATA)
  1676. scc_s = "SATA";
  1677. else if (cc == PCI_CLASS_STORAGE_RAID)
  1678. scc_s = "RAID";
  1679. else
  1680. scc_s = "unknown";
  1681. dev_printk(KERN_INFO, &pdev->dev,
  1682. "AHCI %02x%02x.%02x%02x "
  1683. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1684. ,
  1685. (vers >> 24) & 0xff,
  1686. (vers >> 16) & 0xff,
  1687. (vers >> 8) & 0xff,
  1688. vers & 0xff,
  1689. ((cap >> 8) & 0x1f) + 1,
  1690. (cap & 0x1f) + 1,
  1691. speed_s,
  1692. impl,
  1693. scc_s);
  1694. dev_printk(KERN_INFO, &pdev->dev,
  1695. "flags: "
  1696. "%s%s%s%s%s%s%s"
  1697. "%s%s%s%s%s%s%s\n"
  1698. ,
  1699. cap & (1 << 31) ? "64bit " : "",
  1700. cap & (1 << 30) ? "ncq " : "",
  1701. cap & (1 << 29) ? "sntf " : "",
  1702. cap & (1 << 28) ? "ilck " : "",
  1703. cap & (1 << 27) ? "stag " : "",
  1704. cap & (1 << 26) ? "pm " : "",
  1705. cap & (1 << 25) ? "led " : "",
  1706. cap & (1 << 24) ? "clo " : "",
  1707. cap & (1 << 19) ? "nz " : "",
  1708. cap & (1 << 18) ? "only " : "",
  1709. cap & (1 << 17) ? "pmp " : "",
  1710. cap & (1 << 15) ? "pio " : "",
  1711. cap & (1 << 14) ? "slum " : "",
  1712. cap & (1 << 13) ? "part " : ""
  1713. );
  1714. }
  1715. /* On ASUS P5W DH Deluxe, the second port of PCI device 00:1f.2 is
  1716. * hardwired to on-board SIMG 4726. The chipset is ICH8 and doesn't
  1717. * support PMP and the 4726 either directly exports the device
  1718. * attached to the first downstream port or acts as a hardware storage
  1719. * controller and emulate a single ATA device (can be RAID 0/1 or some
  1720. * other configuration).
  1721. *
  1722. * When there's no device attached to the first downstream port of the
  1723. * 4726, "Config Disk" appears, which is a pseudo ATA device to
  1724. * configure the 4726. However, ATA emulation of the device is very
  1725. * lame. It doesn't send signature D2H Reg FIS after the initial
  1726. * hardreset, pukes on SRST w/ PMP==0 and has bunch of other issues.
  1727. *
  1728. * The following function works around the problem by always using
  1729. * hardreset on the port and not depending on receiving signature FIS
  1730. * afterward. If signature FIS isn't received soon, ATA class is
  1731. * assumed without follow-up softreset.
  1732. */
  1733. static void ahci_p5wdh_workaround(struct ata_host *host)
  1734. {
  1735. static struct dmi_system_id sysids[] = {
  1736. {
  1737. .ident = "P5W DH Deluxe",
  1738. .matches = {
  1739. DMI_MATCH(DMI_SYS_VENDOR,
  1740. "ASUSTEK COMPUTER INC"),
  1741. DMI_MATCH(DMI_PRODUCT_NAME, "P5W DH Deluxe"),
  1742. },
  1743. },
  1744. { }
  1745. };
  1746. struct pci_dev *pdev = to_pci_dev(host->dev);
  1747. if (pdev->bus->number == 0 && pdev->devfn == PCI_DEVFN(0x1f, 2) &&
  1748. dmi_check_system(sysids)) {
  1749. struct ata_port *ap = host->ports[1];
  1750. dev_printk(KERN_INFO, &pdev->dev, "enabling ASUS P5W DH "
  1751. "Deluxe on-board SIMG4726 workaround\n");
  1752. ap->ops = &ahci_p5wdh_ops;
  1753. ap->link.flags |= ATA_LFLAG_NO_SRST | ATA_LFLAG_ASSUME_ATA;
  1754. }
  1755. }
  1756. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1757. {
  1758. static int printed_version;
  1759. struct ata_port_info pi = ahci_port_info[ent->driver_data];
  1760. const struct ata_port_info *ppi[] = { &pi, NULL };
  1761. struct device *dev = &pdev->dev;
  1762. struct ahci_host_priv *hpriv;
  1763. struct ata_host *host;
  1764. int i, rc;
  1765. VPRINTK("ENTER\n");
  1766. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1767. if (!printed_version++)
  1768. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1769. /* acquire resources */
  1770. rc = pcim_enable_device(pdev);
  1771. if (rc)
  1772. return rc;
  1773. rc = pcim_iomap_regions(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
  1774. if (rc == -EBUSY)
  1775. pcim_pin_device(pdev);
  1776. if (rc)
  1777. return rc;
  1778. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  1779. if (!hpriv)
  1780. return -ENOMEM;
  1781. hpriv->flags |= (unsigned long)pi.private_data;
  1782. if ((hpriv->flags & AHCI_HFLAG_NO_MSI) || pci_enable_msi(pdev))
  1783. pci_intx(pdev, 1);
  1784. /* save initial config */
  1785. ahci_save_initial_config(pdev, hpriv);
  1786. /* prepare host */
  1787. if (hpriv->cap & HOST_CAP_NCQ)
  1788. pi.flags |= ATA_FLAG_NCQ;
  1789. if (hpriv->cap & HOST_CAP_PMP)
  1790. pi.flags |= ATA_FLAG_PMP;
  1791. host = ata_host_alloc_pinfo(&pdev->dev, ppi, fls(hpriv->port_map));
  1792. if (!host)
  1793. return -ENOMEM;
  1794. host->iomap = pcim_iomap_table(pdev);
  1795. host->private_data = hpriv;
  1796. for (i = 0; i < host->n_ports; i++) {
  1797. struct ata_port *ap = host->ports[i];
  1798. void __iomem *port_mmio = ahci_port_base(ap);
  1799. ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
  1800. ata_port_pbar_desc(ap, AHCI_PCI_BAR,
  1801. 0x100 + ap->port_no * 0x80, "port");
  1802. /* standard SATA port setup */
  1803. if (hpriv->port_map & (1 << i))
  1804. ap->ioaddr.cmd_addr = port_mmio;
  1805. /* disabled/not-implemented port */
  1806. else
  1807. ap->ops = &ata_dummy_port_ops;
  1808. }
  1809. /* apply workaround for ASUS P5W DH Deluxe mainboard */
  1810. ahci_p5wdh_workaround(host);
  1811. /* initialize adapter */
  1812. rc = ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
  1813. if (rc)
  1814. return rc;
  1815. rc = ahci_reset_controller(host);
  1816. if (rc)
  1817. return rc;
  1818. ahci_init_controller(host);
  1819. ahci_print_info(host);
  1820. pci_set_master(pdev);
  1821. return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
  1822. &ahci_sht);
  1823. }
  1824. static int __init ahci_init(void)
  1825. {
  1826. return pci_register_driver(&ahci_pci_driver);
  1827. }
  1828. static void __exit ahci_exit(void)
  1829. {
  1830. pci_unregister_driver(&ahci_pci_driver);
  1831. }
  1832. MODULE_AUTHOR("Jeff Garzik");
  1833. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1834. MODULE_LICENSE("GPL");
  1835. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1836. MODULE_VERSION(DRV_VERSION);
  1837. module_init(ahci_init);
  1838. module_exit(ahci_exit);