ahci.c 64 KB

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