ahci.c 63 KB

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