ahci.c 64 KB

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