ahci.c 63 KB

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