ahci.c 63 KB

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