ahci.c 62 KB

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