ahci.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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 <scsi/scsi_host.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <linux/libata.h>
  46. #define DRV_NAME "ahci"
  47. #define DRV_VERSION "2.1"
  48. enum {
  49. AHCI_PCI_BAR = 5,
  50. AHCI_MAX_PORTS = 32,
  51. AHCI_MAX_SG = 168, /* hardware max is 64K */
  52. AHCI_DMA_BOUNDARY = 0xffffffff,
  53. AHCI_USE_CLUSTERING = 0,
  54. AHCI_MAX_CMDS = 32,
  55. AHCI_CMD_SZ = 32,
  56. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  57. AHCI_RX_FIS_SZ = 256,
  58. AHCI_CMD_TBL_CDB = 0x40,
  59. AHCI_CMD_TBL_HDR_SZ = 0x80,
  60. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  61. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  62. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  63. AHCI_RX_FIS_SZ,
  64. AHCI_IRQ_ON_SG = (1 << 31),
  65. AHCI_CMD_ATAPI = (1 << 5),
  66. AHCI_CMD_WRITE = (1 << 6),
  67. AHCI_CMD_PREFETCH = (1 << 7),
  68. AHCI_CMD_RESET = (1 << 8),
  69. AHCI_CMD_CLR_BUSY = (1 << 10),
  70. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  71. RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
  72. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  73. board_ahci = 0,
  74. board_ahci_pi = 1,
  75. board_ahci_vt8251 = 2,
  76. board_ahci_ign_iferr = 3,
  77. /* global controller registers */
  78. HOST_CAP = 0x00, /* host capabilities */
  79. HOST_CTL = 0x04, /* global host control */
  80. HOST_IRQ_STAT = 0x08, /* interrupt status */
  81. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  82. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  83. /* HOST_CTL bits */
  84. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  85. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  86. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  87. /* HOST_CAP bits */
  88. HOST_CAP_SSC = (1 << 14), /* Slumber capable */
  89. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  90. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  91. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  92. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  93. /* registers for each SATA port */
  94. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  95. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  96. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  97. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  98. PORT_IRQ_STAT = 0x10, /* interrupt status */
  99. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  100. PORT_CMD = 0x18, /* port command */
  101. PORT_TFDATA = 0x20, /* taskfile data */
  102. PORT_SIG = 0x24, /* device TF signature */
  103. PORT_CMD_ISSUE = 0x38, /* command issue */
  104. PORT_SCR = 0x28, /* SATA phy register block */
  105. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  106. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  107. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  108. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  109. /* PORT_IRQ_{STAT,MASK} bits */
  110. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  111. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  112. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  113. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  114. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  115. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  116. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  117. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  118. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  119. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  120. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  121. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  122. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  123. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  124. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  125. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  126. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  127. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  128. PORT_IRQ_IF_ERR |
  129. PORT_IRQ_CONNECT |
  130. PORT_IRQ_PHYRDY |
  131. PORT_IRQ_UNK_FIS,
  132. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  133. PORT_IRQ_TF_ERR |
  134. PORT_IRQ_HBUS_DATA_ERR,
  135. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  136. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  137. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  138. /* PORT_CMD bits */
  139. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  140. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  141. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  142. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  143. PORT_CMD_CLO = (1 << 3), /* Command list override */
  144. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  145. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  146. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  147. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  148. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  149. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  150. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  151. /* ap->flags bits */
  152. AHCI_FLAG_NO_NCQ = (1 << 24),
  153. AHCI_FLAG_IGN_IRQ_IF_ERR = (1 << 25), /* ignore IRQ_IF_ERR */
  154. AHCI_FLAG_HONOR_PI = (1 << 26), /* honor PORTS_IMPL */
  155. };
  156. struct ahci_cmd_hdr {
  157. u32 opts;
  158. u32 status;
  159. u32 tbl_addr;
  160. u32 tbl_addr_hi;
  161. u32 reserved[4];
  162. };
  163. struct ahci_sg {
  164. u32 addr;
  165. u32 addr_hi;
  166. u32 reserved;
  167. u32 flags_size;
  168. };
  169. struct ahci_host_priv {
  170. u32 cap; /* cache of HOST_CAP register */
  171. u32 port_map; /* cache of HOST_PORTS_IMPL reg */
  172. };
  173. struct ahci_port_priv {
  174. struct ahci_cmd_hdr *cmd_slot;
  175. dma_addr_t cmd_slot_dma;
  176. void *cmd_tbl;
  177. dma_addr_t cmd_tbl_dma;
  178. void *rx_fis;
  179. dma_addr_t rx_fis_dma;
  180. /* for NCQ spurious interrupt analysis */
  181. unsigned int ncq_saw_d2h:1;
  182. unsigned int ncq_saw_dmas:1;
  183. };
  184. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
  185. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  186. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  187. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  188. static irqreturn_t ahci_interrupt (int irq, void *dev_instance);
  189. static void ahci_irq_clear(struct ata_port *ap);
  190. static int ahci_port_start(struct ata_port *ap);
  191. static void ahci_port_stop(struct ata_port *ap);
  192. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  193. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  194. static u8 ahci_check_status(struct ata_port *ap);
  195. static void ahci_freeze(struct ata_port *ap);
  196. static void ahci_thaw(struct ata_port *ap);
  197. static void ahci_error_handler(struct ata_port *ap);
  198. static void ahci_vt8251_error_handler(struct ata_port *ap);
  199. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  200. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  201. static int ahci_port_resume(struct ata_port *ap);
  202. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  203. static int ahci_pci_device_resume(struct pci_dev *pdev);
  204. static struct scsi_host_template ahci_sht = {
  205. .module = THIS_MODULE,
  206. .name = DRV_NAME,
  207. .ioctl = ata_scsi_ioctl,
  208. .queuecommand = ata_scsi_queuecmd,
  209. .change_queue_depth = ata_scsi_change_queue_depth,
  210. .can_queue = AHCI_MAX_CMDS - 1,
  211. .this_id = ATA_SHT_THIS_ID,
  212. .sg_tablesize = AHCI_MAX_SG,
  213. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  214. .emulated = ATA_SHT_EMULATED,
  215. .use_clustering = AHCI_USE_CLUSTERING,
  216. .proc_name = DRV_NAME,
  217. .dma_boundary = AHCI_DMA_BOUNDARY,
  218. .slave_configure = ata_scsi_slave_config,
  219. .slave_destroy = ata_scsi_slave_destroy,
  220. .bios_param = ata_std_bios_param,
  221. .suspend = ata_scsi_device_suspend,
  222. .resume = ata_scsi_device_resume,
  223. };
  224. static const struct ata_port_operations ahci_ops = {
  225. .port_disable = ata_port_disable,
  226. .check_status = ahci_check_status,
  227. .check_altstatus = ahci_check_status,
  228. .dev_select = ata_noop_dev_select,
  229. .tf_read = ahci_tf_read,
  230. .qc_prep = ahci_qc_prep,
  231. .qc_issue = ahci_qc_issue,
  232. .irq_handler = ahci_interrupt,
  233. .irq_clear = ahci_irq_clear,
  234. .irq_on = ata_dummy_irq_on,
  235. .irq_ack = ata_dummy_irq_ack,
  236. .scr_read = ahci_scr_read,
  237. .scr_write = ahci_scr_write,
  238. .freeze = ahci_freeze,
  239. .thaw = ahci_thaw,
  240. .error_handler = ahci_error_handler,
  241. .post_internal_cmd = ahci_post_internal_cmd,
  242. .port_suspend = ahci_port_suspend,
  243. .port_resume = ahci_port_resume,
  244. .port_start = ahci_port_start,
  245. .port_stop = ahci_port_stop,
  246. };
  247. static const struct ata_port_operations ahci_vt8251_ops = {
  248. .port_disable = ata_port_disable,
  249. .check_status = ahci_check_status,
  250. .check_altstatus = ahci_check_status,
  251. .dev_select = ata_noop_dev_select,
  252. .tf_read = ahci_tf_read,
  253. .qc_prep = ahci_qc_prep,
  254. .qc_issue = ahci_qc_issue,
  255. .irq_handler = ahci_interrupt,
  256. .irq_clear = ahci_irq_clear,
  257. .irq_on = ata_dummy_irq_on,
  258. .irq_ack = ata_dummy_irq_ack,
  259. .scr_read = ahci_scr_read,
  260. .scr_write = ahci_scr_write,
  261. .freeze = ahci_freeze,
  262. .thaw = ahci_thaw,
  263. .error_handler = ahci_vt8251_error_handler,
  264. .post_internal_cmd = ahci_post_internal_cmd,
  265. .port_suspend = ahci_port_suspend,
  266. .port_resume = ahci_port_resume,
  267. .port_start = ahci_port_start,
  268. .port_stop = ahci_port_stop,
  269. };
  270. static const struct ata_port_info ahci_port_info[] = {
  271. /* board_ahci */
  272. {
  273. .sht = &ahci_sht,
  274. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  275. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  276. ATA_FLAG_SKIP_D2H_BSY,
  277. .pio_mask = 0x1f, /* pio0-4 */
  278. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  279. .port_ops = &ahci_ops,
  280. },
  281. /* board_ahci_pi */
  282. {
  283. .sht = &ahci_sht,
  284. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  285. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  286. ATA_FLAG_SKIP_D2H_BSY | AHCI_FLAG_HONOR_PI,
  287. .pio_mask = 0x1f, /* pio0-4 */
  288. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  289. .port_ops = &ahci_ops,
  290. },
  291. /* board_ahci_vt8251 */
  292. {
  293. .sht = &ahci_sht,
  294. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  295. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  296. ATA_FLAG_SKIP_D2H_BSY |
  297. ATA_FLAG_HRST_TO_RESUME | AHCI_FLAG_NO_NCQ,
  298. .pio_mask = 0x1f, /* pio0-4 */
  299. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  300. .port_ops = &ahci_vt8251_ops,
  301. },
  302. /* board_ahci_ign_iferr */
  303. {
  304. .sht = &ahci_sht,
  305. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  306. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  307. ATA_FLAG_SKIP_D2H_BSY |
  308. AHCI_FLAG_IGN_IRQ_IF_ERR,
  309. .pio_mask = 0x1f, /* pio0-4 */
  310. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  311. .port_ops = &ahci_ops,
  312. },
  313. };
  314. static const struct pci_device_id ahci_pci_tbl[] = {
  315. /* Intel */
  316. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  317. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  318. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  319. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  320. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  321. { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
  322. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  323. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  324. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  325. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  326. { PCI_VDEVICE(INTEL, 0x2821), board_ahci_pi }, /* ICH8 */
  327. { PCI_VDEVICE(INTEL, 0x2822), board_ahci_pi }, /* ICH8 */
  328. { PCI_VDEVICE(INTEL, 0x2824), board_ahci_pi }, /* ICH8 */
  329. { PCI_VDEVICE(INTEL, 0x2829), board_ahci_pi }, /* ICH8M */
  330. { PCI_VDEVICE(INTEL, 0x282a), board_ahci_pi }, /* ICH8M */
  331. { PCI_VDEVICE(INTEL, 0x2922), board_ahci_pi }, /* ICH9 */
  332. { PCI_VDEVICE(INTEL, 0x2923), board_ahci_pi }, /* ICH9 */
  333. { PCI_VDEVICE(INTEL, 0x2924), board_ahci_pi }, /* ICH9 */
  334. { PCI_VDEVICE(INTEL, 0x2925), board_ahci_pi }, /* ICH9 */
  335. { PCI_VDEVICE(INTEL, 0x2927), board_ahci_pi }, /* ICH9 */
  336. { PCI_VDEVICE(INTEL, 0x2929), board_ahci_pi }, /* ICH9M */
  337. { PCI_VDEVICE(INTEL, 0x292a), board_ahci_pi }, /* ICH9M */
  338. { PCI_VDEVICE(INTEL, 0x292b), board_ahci_pi }, /* ICH9M */
  339. { PCI_VDEVICE(INTEL, 0x292f), board_ahci_pi }, /* ICH9M */
  340. { PCI_VDEVICE(INTEL, 0x294d), board_ahci_pi }, /* ICH9 */
  341. { PCI_VDEVICE(INTEL, 0x294e), board_ahci_pi }, /* ICH9M */
  342. /* JMicron */
  343. { PCI_VDEVICE(JMICRON, 0x2360), board_ahci_ign_iferr }, /* JMB360 */
  344. { PCI_VDEVICE(JMICRON, 0x2361), board_ahci_ign_iferr }, /* JMB361 */
  345. { PCI_VDEVICE(JMICRON, 0x2363), board_ahci_ign_iferr }, /* JMB363 */
  346. { PCI_VDEVICE(JMICRON, 0x2365), board_ahci_ign_iferr }, /* JMB365 */
  347. { PCI_VDEVICE(JMICRON, 0x2366), board_ahci_ign_iferr }, /* JMB366 */
  348. /* ATI */
  349. { PCI_VDEVICE(ATI, 0x4380), board_ahci }, /* ATI SB600 non-raid */
  350. { PCI_VDEVICE(ATI, 0x4381), board_ahci }, /* ATI SB600 raid */
  351. /* VIA */
  352. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  353. /* NVIDIA */
  354. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
  355. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
  356. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
  357. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
  358. { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci }, /* MCP65 */
  359. { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci }, /* MCP65 */
  360. { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci }, /* MCP65 */
  361. { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci }, /* MCP65 */
  362. { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci }, /* MCP67 */
  363. { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci }, /* MCP67 */
  364. { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci }, /* MCP67 */
  365. { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci }, /* MCP67 */
  366. { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
  367. { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
  368. { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
  369. { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
  370. { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
  371. { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
  372. { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
  373. { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
  374. /* SiS */
  375. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  376. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  377. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  378. /* Generic, PCI class code for AHCI */
  379. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  380. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
  381. { } /* terminate list */
  382. };
  383. static struct pci_driver ahci_pci_driver = {
  384. .name = DRV_NAME,
  385. .id_table = ahci_pci_tbl,
  386. .probe = ahci_init_one,
  387. .remove = ata_pci_remove_one,
  388. .suspend = ahci_pci_device_suspend,
  389. .resume = ahci_pci_device_resume,
  390. };
  391. static inline int ahci_nr_ports(u32 cap)
  392. {
  393. return (cap & 0x1f) + 1;
  394. }
  395. static inline void __iomem *ahci_port_base(void __iomem *base,
  396. unsigned int port)
  397. {
  398. return base + 0x100 + (port * 0x80);
  399. }
  400. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  401. {
  402. unsigned int sc_reg;
  403. switch (sc_reg_in) {
  404. case SCR_STATUS: sc_reg = 0; break;
  405. case SCR_CONTROL: sc_reg = 1; break;
  406. case SCR_ERROR: sc_reg = 2; break;
  407. case SCR_ACTIVE: sc_reg = 3; break;
  408. default:
  409. return 0xffffffffU;
  410. }
  411. return readl(ap->ioaddr.scr_addr + (sc_reg * 4));
  412. }
  413. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  414. u32 val)
  415. {
  416. unsigned int sc_reg;
  417. switch (sc_reg_in) {
  418. case SCR_STATUS: sc_reg = 0; break;
  419. case SCR_CONTROL: sc_reg = 1; break;
  420. case SCR_ERROR: sc_reg = 2; break;
  421. case SCR_ACTIVE: sc_reg = 3; break;
  422. default:
  423. return;
  424. }
  425. writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
  426. }
  427. static void ahci_start_engine(void __iomem *port_mmio)
  428. {
  429. u32 tmp;
  430. /* start DMA */
  431. tmp = readl(port_mmio + PORT_CMD);
  432. tmp |= PORT_CMD_START;
  433. writel(tmp, port_mmio + PORT_CMD);
  434. readl(port_mmio + PORT_CMD); /* flush */
  435. }
  436. static int ahci_stop_engine(void __iomem *port_mmio)
  437. {
  438. u32 tmp;
  439. tmp = readl(port_mmio + PORT_CMD);
  440. /* check if the HBA is idle */
  441. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  442. return 0;
  443. /* setting HBA to idle */
  444. tmp &= ~PORT_CMD_START;
  445. writel(tmp, port_mmio + PORT_CMD);
  446. /* wait for engine to stop. This could be as long as 500 msec */
  447. tmp = ata_wait_register(port_mmio + PORT_CMD,
  448. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  449. if (tmp & PORT_CMD_LIST_ON)
  450. return -EIO;
  451. return 0;
  452. }
  453. static void ahci_start_fis_rx(void __iomem *port_mmio, u32 cap,
  454. dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
  455. {
  456. u32 tmp;
  457. /* set FIS registers */
  458. if (cap & HOST_CAP_64)
  459. writel((cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  460. writel(cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  461. if (cap & HOST_CAP_64)
  462. writel((rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  463. writel(rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  464. /* enable FIS reception */
  465. tmp = readl(port_mmio + PORT_CMD);
  466. tmp |= PORT_CMD_FIS_RX;
  467. writel(tmp, port_mmio + PORT_CMD);
  468. /* flush */
  469. readl(port_mmio + PORT_CMD);
  470. }
  471. static int ahci_stop_fis_rx(void __iomem *port_mmio)
  472. {
  473. u32 tmp;
  474. /* disable FIS reception */
  475. tmp = readl(port_mmio + PORT_CMD);
  476. tmp &= ~PORT_CMD_FIS_RX;
  477. writel(tmp, port_mmio + PORT_CMD);
  478. /* wait for completion, spec says 500ms, give it 1000 */
  479. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  480. PORT_CMD_FIS_ON, 10, 1000);
  481. if (tmp & PORT_CMD_FIS_ON)
  482. return -EBUSY;
  483. return 0;
  484. }
  485. static void ahci_power_up(void __iomem *port_mmio, u32 cap)
  486. {
  487. u32 cmd;
  488. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  489. /* spin up device */
  490. if (cap & HOST_CAP_SSS) {
  491. cmd |= PORT_CMD_SPIN_UP;
  492. writel(cmd, port_mmio + PORT_CMD);
  493. }
  494. /* wake up link */
  495. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  496. }
  497. static void ahci_power_down(void __iomem *port_mmio, u32 cap)
  498. {
  499. u32 cmd, scontrol;
  500. if (!(cap & HOST_CAP_SSS))
  501. return;
  502. /* put device into listen mode, first set PxSCTL.DET to 0 */
  503. scontrol = readl(port_mmio + PORT_SCR_CTL);
  504. scontrol &= ~0xf;
  505. writel(scontrol, port_mmio + PORT_SCR_CTL);
  506. /* then set PxCMD.SUD to 0 */
  507. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  508. cmd &= ~PORT_CMD_SPIN_UP;
  509. writel(cmd, port_mmio + PORT_CMD);
  510. }
  511. static void ahci_init_port(void __iomem *port_mmio, u32 cap,
  512. dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
  513. {
  514. /* enable FIS reception */
  515. ahci_start_fis_rx(port_mmio, cap, cmd_slot_dma, rx_fis_dma);
  516. /* enable DMA */
  517. ahci_start_engine(port_mmio);
  518. }
  519. static int ahci_deinit_port(void __iomem *port_mmio, u32 cap, const char **emsg)
  520. {
  521. int rc;
  522. /* disable DMA */
  523. rc = ahci_stop_engine(port_mmio);
  524. if (rc) {
  525. *emsg = "failed to stop engine";
  526. return rc;
  527. }
  528. /* disable FIS reception */
  529. rc = ahci_stop_fis_rx(port_mmio);
  530. if (rc) {
  531. *emsg = "failed stop FIS RX";
  532. return rc;
  533. }
  534. return 0;
  535. }
  536. static int ahci_reset_controller(void __iomem *mmio, struct pci_dev *pdev)
  537. {
  538. u32 cap_save, impl_save, tmp;
  539. cap_save = readl(mmio + HOST_CAP);
  540. impl_save = readl(mmio + HOST_PORTS_IMPL);
  541. /* global controller reset */
  542. tmp = readl(mmio + HOST_CTL);
  543. if ((tmp & HOST_RESET) == 0) {
  544. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  545. readl(mmio + HOST_CTL); /* flush */
  546. }
  547. /* reset must complete within 1 second, or
  548. * the hardware should be considered fried.
  549. */
  550. ssleep(1);
  551. tmp = readl(mmio + HOST_CTL);
  552. if (tmp & HOST_RESET) {
  553. dev_printk(KERN_ERR, &pdev->dev,
  554. "controller reset failed (0x%x)\n", tmp);
  555. return -EIO;
  556. }
  557. /* turn on AHCI mode */
  558. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  559. (void) readl(mmio + HOST_CTL); /* flush */
  560. /* These write-once registers are normally cleared on reset.
  561. * Restore BIOS values... which we HOPE were present before
  562. * reset.
  563. */
  564. if (!impl_save) {
  565. impl_save = (1 << ahci_nr_ports(cap_save)) - 1;
  566. dev_printk(KERN_WARNING, &pdev->dev,
  567. "PORTS_IMPL is zero, forcing 0x%x\n", impl_save);
  568. }
  569. writel(cap_save, mmio + HOST_CAP);
  570. writel(impl_save, mmio + HOST_PORTS_IMPL);
  571. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  572. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  573. u16 tmp16;
  574. /* configure PCS */
  575. pci_read_config_word(pdev, 0x92, &tmp16);
  576. tmp16 |= 0xf;
  577. pci_write_config_word(pdev, 0x92, tmp16);
  578. }
  579. return 0;
  580. }
  581. static void ahci_init_controller(void __iomem *mmio, struct pci_dev *pdev,
  582. int n_ports, unsigned int port_flags,
  583. struct ahci_host_priv *hpriv)
  584. {
  585. int i, rc;
  586. u32 tmp;
  587. for (i = 0; i < n_ports; i++) {
  588. void __iomem *port_mmio = ahci_port_base(mmio, i);
  589. const char *emsg = NULL;
  590. if ((port_flags & AHCI_FLAG_HONOR_PI) &&
  591. !(hpriv->port_map & (1 << i)))
  592. continue;
  593. /* make sure port is not active */
  594. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  595. if (rc)
  596. dev_printk(KERN_WARNING, &pdev->dev,
  597. "%s (%d)\n", emsg, rc);
  598. /* clear SError */
  599. tmp = readl(port_mmio + PORT_SCR_ERR);
  600. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  601. writel(tmp, port_mmio + PORT_SCR_ERR);
  602. /* clear port IRQ */
  603. tmp = readl(port_mmio + PORT_IRQ_STAT);
  604. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  605. if (tmp)
  606. writel(tmp, port_mmio + PORT_IRQ_STAT);
  607. writel(1 << i, mmio + HOST_IRQ_STAT);
  608. }
  609. tmp = readl(mmio + HOST_CTL);
  610. VPRINTK("HOST_CTL 0x%x\n", tmp);
  611. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  612. tmp = readl(mmio + HOST_CTL);
  613. VPRINTK("HOST_CTL 0x%x\n", tmp);
  614. }
  615. static unsigned int ahci_dev_classify(struct ata_port *ap)
  616. {
  617. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  618. struct ata_taskfile tf;
  619. u32 tmp;
  620. tmp = readl(port_mmio + PORT_SIG);
  621. tf.lbah = (tmp >> 24) & 0xff;
  622. tf.lbam = (tmp >> 16) & 0xff;
  623. tf.lbal = (tmp >> 8) & 0xff;
  624. tf.nsect = (tmp) & 0xff;
  625. return ata_dev_classify(&tf);
  626. }
  627. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  628. u32 opts)
  629. {
  630. dma_addr_t cmd_tbl_dma;
  631. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  632. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  633. pp->cmd_slot[tag].status = 0;
  634. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  635. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  636. }
  637. static int ahci_clo(struct ata_port *ap)
  638. {
  639. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  640. struct ahci_host_priv *hpriv = ap->host->private_data;
  641. u32 tmp;
  642. if (!(hpriv->cap & HOST_CAP_CLO))
  643. return -EOPNOTSUPP;
  644. tmp = readl(port_mmio + PORT_CMD);
  645. tmp |= PORT_CMD_CLO;
  646. writel(tmp, port_mmio + PORT_CMD);
  647. tmp = ata_wait_register(port_mmio + PORT_CMD,
  648. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  649. if (tmp & PORT_CMD_CLO)
  650. return -EIO;
  651. return 0;
  652. }
  653. static int ahci_softreset(struct ata_port *ap, unsigned int *class)
  654. {
  655. struct ahci_port_priv *pp = ap->private_data;
  656. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  657. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  658. const u32 cmd_fis_len = 5; /* five dwords */
  659. const char *reason = NULL;
  660. struct ata_taskfile tf;
  661. u32 tmp;
  662. u8 *fis;
  663. int rc;
  664. DPRINTK("ENTER\n");
  665. if (ata_port_offline(ap)) {
  666. DPRINTK("PHY reports no device\n");
  667. *class = ATA_DEV_NONE;
  668. return 0;
  669. }
  670. /* prepare for SRST (AHCI-1.1 10.4.1) */
  671. rc = ahci_stop_engine(port_mmio);
  672. if (rc) {
  673. reason = "failed to stop engine";
  674. goto fail_restart;
  675. }
  676. /* check BUSY/DRQ, perform Command List Override if necessary */
  677. if (ahci_check_status(ap) & (ATA_BUSY | ATA_DRQ)) {
  678. rc = ahci_clo(ap);
  679. if (rc == -EOPNOTSUPP) {
  680. reason = "port busy but CLO unavailable";
  681. goto fail_restart;
  682. } else if (rc) {
  683. reason = "port busy but CLO failed";
  684. goto fail_restart;
  685. }
  686. }
  687. /* restart engine */
  688. ahci_start_engine(port_mmio);
  689. ata_tf_init(ap->device, &tf);
  690. fis = pp->cmd_tbl;
  691. /* issue the first D2H Register FIS */
  692. ahci_fill_cmd_slot(pp, 0,
  693. cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
  694. tf.ctl |= ATA_SRST;
  695. ata_tf_to_fis(&tf, fis, 0);
  696. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  697. writel(1, port_mmio + PORT_CMD_ISSUE);
  698. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1, 1, 500);
  699. if (tmp & 0x1) {
  700. rc = -EIO;
  701. reason = "1st FIS failed";
  702. goto fail;
  703. }
  704. /* spec says at least 5us, but be generous and sleep for 1ms */
  705. msleep(1);
  706. /* issue the second D2H Register FIS */
  707. ahci_fill_cmd_slot(pp, 0, cmd_fis_len);
  708. tf.ctl &= ~ATA_SRST;
  709. ata_tf_to_fis(&tf, fis, 0);
  710. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  711. writel(1, port_mmio + PORT_CMD_ISSUE);
  712. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  713. /* spec mandates ">= 2ms" before checking status.
  714. * We wait 150ms, because that was the magic delay used for
  715. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  716. * between when the ATA command register is written, and then
  717. * status is checked. Because waiting for "a while" before
  718. * checking status is fine, post SRST, we perform this magic
  719. * delay here as well.
  720. */
  721. msleep(150);
  722. *class = ATA_DEV_NONE;
  723. if (ata_port_online(ap)) {
  724. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  725. rc = -EIO;
  726. reason = "device not ready";
  727. goto fail;
  728. }
  729. *class = ahci_dev_classify(ap);
  730. }
  731. DPRINTK("EXIT, class=%u\n", *class);
  732. return 0;
  733. fail_restart:
  734. ahci_start_engine(port_mmio);
  735. fail:
  736. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  737. return rc;
  738. }
  739. static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
  740. {
  741. struct ahci_port_priv *pp = ap->private_data;
  742. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  743. struct ata_taskfile tf;
  744. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  745. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  746. int rc;
  747. DPRINTK("ENTER\n");
  748. ahci_stop_engine(port_mmio);
  749. /* clear D2H reception area to properly wait for D2H FIS */
  750. ata_tf_init(ap->device, &tf);
  751. tf.command = 0x80;
  752. ata_tf_to_fis(&tf, d2h_fis, 0);
  753. rc = sata_std_hardreset(ap, class);
  754. ahci_start_engine(port_mmio);
  755. if (rc == 0 && ata_port_online(ap))
  756. *class = ahci_dev_classify(ap);
  757. if (*class == ATA_DEV_UNKNOWN)
  758. *class = ATA_DEV_NONE;
  759. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  760. return rc;
  761. }
  762. static int ahci_vt8251_hardreset(struct ata_port *ap, unsigned int *class)
  763. {
  764. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  765. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  766. int rc;
  767. DPRINTK("ENTER\n");
  768. ahci_stop_engine(port_mmio);
  769. rc = sata_port_hardreset(ap, sata_ehc_deb_timing(&ap->eh_context));
  770. /* vt8251 needs SError cleared for the port to operate */
  771. ahci_scr_write(ap, SCR_ERROR, ahci_scr_read(ap, SCR_ERROR));
  772. ahci_start_engine(port_mmio);
  773. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  774. /* vt8251 doesn't clear BSY on signature FIS reception,
  775. * request follow-up softreset.
  776. */
  777. return rc ?: -EAGAIN;
  778. }
  779. static void ahci_postreset(struct ata_port *ap, unsigned int *class)
  780. {
  781. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  782. u32 new_tmp, tmp;
  783. ata_std_postreset(ap, class);
  784. /* Make sure port's ATAPI bit is set appropriately */
  785. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  786. if (*class == ATA_DEV_ATAPI)
  787. new_tmp |= PORT_CMD_ATAPI;
  788. else
  789. new_tmp &= ~PORT_CMD_ATAPI;
  790. if (new_tmp != tmp) {
  791. writel(new_tmp, port_mmio + PORT_CMD);
  792. readl(port_mmio + PORT_CMD); /* flush */
  793. }
  794. }
  795. static u8 ahci_check_status(struct ata_port *ap)
  796. {
  797. void __iomem *mmio = ap->ioaddr.cmd_addr;
  798. return readl(mmio + PORT_TFDATA) & 0xFF;
  799. }
  800. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  801. {
  802. struct ahci_port_priv *pp = ap->private_data;
  803. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  804. ata_tf_from_fis(d2h_fis, tf);
  805. }
  806. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  807. {
  808. struct scatterlist *sg;
  809. struct ahci_sg *ahci_sg;
  810. unsigned int n_sg = 0;
  811. VPRINTK("ENTER\n");
  812. /*
  813. * Next, the S/G list.
  814. */
  815. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  816. ata_for_each_sg(sg, qc) {
  817. dma_addr_t addr = sg_dma_address(sg);
  818. u32 sg_len = sg_dma_len(sg);
  819. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  820. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  821. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  822. ahci_sg++;
  823. n_sg++;
  824. }
  825. return n_sg;
  826. }
  827. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  828. {
  829. struct ata_port *ap = qc->ap;
  830. struct ahci_port_priv *pp = ap->private_data;
  831. int is_atapi = is_atapi_taskfile(&qc->tf);
  832. void *cmd_tbl;
  833. u32 opts;
  834. const u32 cmd_fis_len = 5; /* five dwords */
  835. unsigned int n_elem;
  836. /*
  837. * Fill in command table information. First, the header,
  838. * a SATA Register - Host to Device command FIS.
  839. */
  840. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  841. ata_tf_to_fis(&qc->tf, cmd_tbl, 0);
  842. if (is_atapi) {
  843. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  844. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  845. }
  846. n_elem = 0;
  847. if (qc->flags & ATA_QCFLAG_DMAMAP)
  848. n_elem = ahci_fill_sg(qc, cmd_tbl);
  849. /*
  850. * Fill in command slot information.
  851. */
  852. opts = cmd_fis_len | n_elem << 16;
  853. if (qc->tf.flags & ATA_TFLAG_WRITE)
  854. opts |= AHCI_CMD_WRITE;
  855. if (is_atapi)
  856. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  857. ahci_fill_cmd_slot(pp, qc->tag, opts);
  858. }
  859. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  860. {
  861. struct ahci_port_priv *pp = ap->private_data;
  862. struct ata_eh_info *ehi = &ap->eh_info;
  863. unsigned int err_mask = 0, action = 0;
  864. struct ata_queued_cmd *qc;
  865. u32 serror;
  866. ata_ehi_clear_desc(ehi);
  867. /* AHCI needs SError cleared; otherwise, it might lock up */
  868. serror = ahci_scr_read(ap, SCR_ERROR);
  869. ahci_scr_write(ap, SCR_ERROR, serror);
  870. /* analyze @irq_stat */
  871. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  872. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  873. if (ap->flags & AHCI_FLAG_IGN_IRQ_IF_ERR)
  874. irq_stat &= ~PORT_IRQ_IF_ERR;
  875. if (irq_stat & PORT_IRQ_TF_ERR)
  876. err_mask |= AC_ERR_DEV;
  877. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  878. err_mask |= AC_ERR_HOST_BUS;
  879. action |= ATA_EH_SOFTRESET;
  880. }
  881. if (irq_stat & PORT_IRQ_IF_ERR) {
  882. err_mask |= AC_ERR_ATA_BUS;
  883. action |= ATA_EH_SOFTRESET;
  884. ata_ehi_push_desc(ehi, ", interface fatal error");
  885. }
  886. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  887. ata_ehi_hotplugged(ehi);
  888. ata_ehi_push_desc(ehi, ", %s", irq_stat & PORT_IRQ_CONNECT ?
  889. "connection status changed" : "PHY RDY changed");
  890. }
  891. if (irq_stat & PORT_IRQ_UNK_FIS) {
  892. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  893. err_mask |= AC_ERR_HSM;
  894. action |= ATA_EH_SOFTRESET;
  895. ata_ehi_push_desc(ehi, ", unknown FIS %08x %08x %08x %08x",
  896. unk[0], unk[1], unk[2], unk[3]);
  897. }
  898. /* okay, let's hand over to EH */
  899. ehi->serror |= serror;
  900. ehi->action |= action;
  901. qc = ata_qc_from_tag(ap, ap->active_tag);
  902. if (qc)
  903. qc->err_mask |= err_mask;
  904. else
  905. ehi->err_mask |= err_mask;
  906. if (irq_stat & PORT_IRQ_FREEZE)
  907. ata_port_freeze(ap);
  908. else
  909. ata_port_abort(ap);
  910. }
  911. static void ahci_host_intr(struct ata_port *ap)
  912. {
  913. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  914. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  915. struct ata_eh_info *ehi = &ap->eh_info;
  916. struct ahci_port_priv *pp = ap->private_data;
  917. u32 status, qc_active;
  918. int rc, known_irq = 0;
  919. status = readl(port_mmio + PORT_IRQ_STAT);
  920. writel(status, port_mmio + PORT_IRQ_STAT);
  921. if (unlikely(status & PORT_IRQ_ERROR)) {
  922. ahci_error_intr(ap, status);
  923. return;
  924. }
  925. if (ap->sactive)
  926. qc_active = readl(port_mmio + PORT_SCR_ACT);
  927. else
  928. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  929. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  930. if (rc > 0)
  931. return;
  932. if (rc < 0) {
  933. ehi->err_mask |= AC_ERR_HSM;
  934. ehi->action |= ATA_EH_SOFTRESET;
  935. ata_port_freeze(ap);
  936. return;
  937. }
  938. /* hmmm... a spurious interupt */
  939. /* if !NCQ, ignore. No modern ATA device has broken HSM
  940. * implementation for non-NCQ commands.
  941. */
  942. if (!ap->sactive)
  943. return;
  944. if (status & PORT_IRQ_D2H_REG_FIS) {
  945. if (!pp->ncq_saw_d2h)
  946. ata_port_printk(ap, KERN_INFO,
  947. "D2H reg with I during NCQ, "
  948. "this message won't be printed again\n");
  949. pp->ncq_saw_d2h = 1;
  950. known_irq = 1;
  951. }
  952. if (status & PORT_IRQ_DMAS_FIS) {
  953. if (!pp->ncq_saw_dmas)
  954. ata_port_printk(ap, KERN_INFO,
  955. "DMAS FIS during NCQ, "
  956. "this message won't be printed again\n");
  957. pp->ncq_saw_dmas = 1;
  958. known_irq = 1;
  959. }
  960. if (status & PORT_IRQ_SDB_FIS) {
  961. /* SDB FIS containing spurious completions might be
  962. * dangerous, whine and fail commands with HSM
  963. * violation. EH will turn off NCQ after several such
  964. * failures.
  965. */
  966. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  967. ata_ehi_push_desc(ehi, "spurious completion during NCQ "
  968. "issue=0x%x SAct=0x%x FIS=%08x:%08x",
  969. readl(port_mmio + PORT_CMD_ISSUE),
  970. readl(port_mmio + PORT_SCR_ACT),
  971. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  972. ehi->err_mask |= AC_ERR_HSM;
  973. ehi->action |= ATA_EH_SOFTRESET;
  974. ata_port_freeze(ap);
  975. known_irq = 1;
  976. }
  977. if (!known_irq)
  978. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  979. "(irq_stat 0x%x active_tag 0x%x sactive 0x%x)\n",
  980. status, ap->active_tag, ap->sactive);
  981. }
  982. static void ahci_irq_clear(struct ata_port *ap)
  983. {
  984. /* TODO */
  985. }
  986. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  987. {
  988. struct ata_host *host = dev_instance;
  989. struct ahci_host_priv *hpriv;
  990. unsigned int i, handled = 0;
  991. void __iomem *mmio;
  992. u32 irq_stat, irq_ack = 0;
  993. VPRINTK("ENTER\n");
  994. hpriv = host->private_data;
  995. mmio = host->iomap[AHCI_PCI_BAR];
  996. /* sigh. 0xffffffff is a valid return from h/w */
  997. irq_stat = readl(mmio + HOST_IRQ_STAT);
  998. irq_stat &= hpriv->port_map;
  999. if (!irq_stat)
  1000. return IRQ_NONE;
  1001. spin_lock(&host->lock);
  1002. for (i = 0; i < host->n_ports; i++) {
  1003. struct ata_port *ap;
  1004. if (!(irq_stat & (1 << i)))
  1005. continue;
  1006. ap = host->ports[i];
  1007. if (ap) {
  1008. ahci_host_intr(ap);
  1009. VPRINTK("port %u\n", i);
  1010. } else {
  1011. VPRINTK("port %u (no irq)\n", i);
  1012. if (ata_ratelimit())
  1013. dev_printk(KERN_WARNING, host->dev,
  1014. "interrupt on disabled port %u\n", i);
  1015. }
  1016. irq_ack |= (1 << i);
  1017. }
  1018. if (irq_ack) {
  1019. writel(irq_ack, mmio + HOST_IRQ_STAT);
  1020. handled = 1;
  1021. }
  1022. spin_unlock(&host->lock);
  1023. VPRINTK("EXIT\n");
  1024. return IRQ_RETVAL(handled);
  1025. }
  1026. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1027. {
  1028. struct ata_port *ap = qc->ap;
  1029. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  1030. if (qc->tf.protocol == ATA_PROT_NCQ)
  1031. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  1032. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  1033. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1034. return 0;
  1035. }
  1036. static void ahci_freeze(struct ata_port *ap)
  1037. {
  1038. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1039. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1040. /* turn IRQ off */
  1041. writel(0, port_mmio + PORT_IRQ_MASK);
  1042. }
  1043. static void ahci_thaw(struct ata_port *ap)
  1044. {
  1045. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1046. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1047. u32 tmp;
  1048. /* clear IRQ */
  1049. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1050. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1051. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1052. /* turn IRQ back on */
  1053. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  1054. }
  1055. static void ahci_error_handler(struct ata_port *ap)
  1056. {
  1057. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1058. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1059. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1060. /* restart engine */
  1061. ahci_stop_engine(port_mmio);
  1062. ahci_start_engine(port_mmio);
  1063. }
  1064. /* perform recovery */
  1065. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_hardreset,
  1066. ahci_postreset);
  1067. }
  1068. static void ahci_vt8251_error_handler(struct ata_port *ap)
  1069. {
  1070. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1071. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1072. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1073. /* restart engine */
  1074. ahci_stop_engine(port_mmio);
  1075. ahci_start_engine(port_mmio);
  1076. }
  1077. /* perform recovery */
  1078. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_vt8251_hardreset,
  1079. ahci_postreset);
  1080. }
  1081. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1082. {
  1083. struct ata_port *ap = qc->ap;
  1084. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1085. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1086. if (qc->flags & ATA_QCFLAG_FAILED)
  1087. qc->err_mask |= AC_ERR_OTHER;
  1088. if (qc->err_mask) {
  1089. /* make DMA engine forget about the failed command */
  1090. ahci_stop_engine(port_mmio);
  1091. ahci_start_engine(port_mmio);
  1092. }
  1093. }
  1094. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1095. {
  1096. struct ahci_host_priv *hpriv = ap->host->private_data;
  1097. struct ahci_port_priv *pp = ap->private_data;
  1098. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1099. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1100. const char *emsg = NULL;
  1101. int rc;
  1102. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  1103. if (rc == 0)
  1104. ahci_power_down(port_mmio, hpriv->cap);
  1105. else {
  1106. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  1107. ahci_init_port(port_mmio, hpriv->cap,
  1108. pp->cmd_slot_dma, pp->rx_fis_dma);
  1109. }
  1110. return rc;
  1111. }
  1112. static int ahci_port_resume(struct ata_port *ap)
  1113. {
  1114. struct ahci_port_priv *pp = ap->private_data;
  1115. struct ahci_host_priv *hpriv = ap->host->private_data;
  1116. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1117. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1118. ahci_power_up(port_mmio, hpriv->cap);
  1119. ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
  1120. return 0;
  1121. }
  1122. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1123. {
  1124. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1125. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1126. u32 ctl;
  1127. if (mesg.event == PM_EVENT_SUSPEND) {
  1128. /* AHCI spec rev1.1 section 8.3.3:
  1129. * Software must disable interrupts prior to requesting a
  1130. * transition of the HBA to D3 state.
  1131. */
  1132. ctl = readl(mmio + HOST_CTL);
  1133. ctl &= ~HOST_IRQ_EN;
  1134. writel(ctl, mmio + HOST_CTL);
  1135. readl(mmio + HOST_CTL); /* flush */
  1136. }
  1137. return ata_pci_device_suspend(pdev, mesg);
  1138. }
  1139. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1140. {
  1141. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1142. struct ahci_host_priv *hpriv = host->private_data;
  1143. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1144. int rc;
  1145. rc = ata_pci_device_do_resume(pdev);
  1146. if (rc)
  1147. return rc;
  1148. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1149. rc = ahci_reset_controller(mmio, pdev);
  1150. if (rc)
  1151. return rc;
  1152. ahci_init_controller(mmio, pdev, host->n_ports,
  1153. host->ports[0]->flags, hpriv);
  1154. }
  1155. ata_host_resume(host);
  1156. return 0;
  1157. }
  1158. static int ahci_port_start(struct ata_port *ap)
  1159. {
  1160. struct device *dev = ap->host->dev;
  1161. struct ahci_host_priv *hpriv = ap->host->private_data;
  1162. struct ahci_port_priv *pp;
  1163. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1164. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1165. void *mem;
  1166. dma_addr_t mem_dma;
  1167. int rc;
  1168. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1169. if (!pp)
  1170. return -ENOMEM;
  1171. rc = ata_pad_alloc(ap, dev);
  1172. if (rc)
  1173. return rc;
  1174. mem = dmam_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma,
  1175. GFP_KERNEL);
  1176. if (!mem)
  1177. return -ENOMEM;
  1178. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1179. /*
  1180. * First item in chunk of DMA memory: 32-slot command table,
  1181. * 32 bytes each in size
  1182. */
  1183. pp->cmd_slot = mem;
  1184. pp->cmd_slot_dma = mem_dma;
  1185. mem += AHCI_CMD_SLOT_SZ;
  1186. mem_dma += AHCI_CMD_SLOT_SZ;
  1187. /*
  1188. * Second item: Received-FIS area
  1189. */
  1190. pp->rx_fis = mem;
  1191. pp->rx_fis_dma = mem_dma;
  1192. mem += AHCI_RX_FIS_SZ;
  1193. mem_dma += AHCI_RX_FIS_SZ;
  1194. /*
  1195. * Third item: data area for storing a single command
  1196. * and its scatter-gather table
  1197. */
  1198. pp->cmd_tbl = mem;
  1199. pp->cmd_tbl_dma = mem_dma;
  1200. ap->private_data = pp;
  1201. /* power up port */
  1202. ahci_power_up(port_mmio, hpriv->cap);
  1203. /* initialize port */
  1204. ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
  1205. return 0;
  1206. }
  1207. static void ahci_port_stop(struct ata_port *ap)
  1208. {
  1209. struct ahci_host_priv *hpriv = ap->host->private_data;
  1210. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1211. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1212. const char *emsg = NULL;
  1213. int rc;
  1214. /* de-initialize port */
  1215. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  1216. if (rc)
  1217. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1218. }
  1219. static void ahci_setup_port(struct ata_ioports *port, void __iomem *base,
  1220. unsigned int port_idx)
  1221. {
  1222. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  1223. base = ahci_port_base(base, port_idx);
  1224. VPRINTK("base now==0x%lx\n", base);
  1225. port->cmd_addr = base;
  1226. port->scr_addr = base + PORT_SCR;
  1227. VPRINTK("EXIT\n");
  1228. }
  1229. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  1230. {
  1231. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1232. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1233. void __iomem *mmio = probe_ent->iomap[AHCI_PCI_BAR];
  1234. unsigned int i, cap_n_ports, using_dac;
  1235. int rc;
  1236. rc = ahci_reset_controller(mmio, pdev);
  1237. if (rc)
  1238. return rc;
  1239. hpriv->cap = readl(mmio + HOST_CAP);
  1240. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  1241. cap_n_ports = ahci_nr_ports(hpriv->cap);
  1242. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  1243. hpriv->cap, hpriv->port_map, cap_n_ports);
  1244. if (probe_ent->port_flags & AHCI_FLAG_HONOR_PI) {
  1245. unsigned int n_ports = cap_n_ports;
  1246. u32 port_map = hpriv->port_map;
  1247. int max_port = 0;
  1248. for (i = 0; i < AHCI_MAX_PORTS && n_ports; i++) {
  1249. if (port_map & (1 << i)) {
  1250. n_ports--;
  1251. port_map &= ~(1 << i);
  1252. max_port = i;
  1253. } else
  1254. probe_ent->dummy_port_mask |= 1 << i;
  1255. }
  1256. if (n_ports || port_map)
  1257. dev_printk(KERN_WARNING, &pdev->dev,
  1258. "nr_ports (%u) and implemented port map "
  1259. "(0x%x) don't match\n",
  1260. cap_n_ports, hpriv->port_map);
  1261. probe_ent->n_ports = max_port + 1;
  1262. } else
  1263. probe_ent->n_ports = cap_n_ports;
  1264. using_dac = hpriv->cap & HOST_CAP_64;
  1265. if (using_dac &&
  1266. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1267. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1268. if (rc) {
  1269. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1270. if (rc) {
  1271. dev_printk(KERN_ERR, &pdev->dev,
  1272. "64-bit DMA enable failed\n");
  1273. return rc;
  1274. }
  1275. }
  1276. } else {
  1277. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1278. if (rc) {
  1279. dev_printk(KERN_ERR, &pdev->dev,
  1280. "32-bit DMA enable failed\n");
  1281. return rc;
  1282. }
  1283. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1284. if (rc) {
  1285. dev_printk(KERN_ERR, &pdev->dev,
  1286. "32-bit consistent DMA enable failed\n");
  1287. return rc;
  1288. }
  1289. }
  1290. for (i = 0; i < probe_ent->n_ports; i++)
  1291. ahci_setup_port(&probe_ent->port[i], mmio, i);
  1292. ahci_init_controller(mmio, pdev, probe_ent->n_ports,
  1293. probe_ent->port_flags, hpriv);
  1294. pci_set_master(pdev);
  1295. return 0;
  1296. }
  1297. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  1298. {
  1299. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1300. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1301. void __iomem *mmio = probe_ent->iomap[AHCI_PCI_BAR];
  1302. u32 vers, cap, impl, speed;
  1303. const char *speed_s;
  1304. u16 cc;
  1305. const char *scc_s;
  1306. vers = readl(mmio + HOST_VERSION);
  1307. cap = hpriv->cap;
  1308. impl = hpriv->port_map;
  1309. speed = (cap >> 20) & 0xf;
  1310. if (speed == 1)
  1311. speed_s = "1.5";
  1312. else if (speed == 2)
  1313. speed_s = "3";
  1314. else
  1315. speed_s = "?";
  1316. pci_read_config_word(pdev, 0x0a, &cc);
  1317. if (cc == PCI_CLASS_STORAGE_IDE)
  1318. scc_s = "IDE";
  1319. else if (cc == PCI_CLASS_STORAGE_SATA)
  1320. scc_s = "SATA";
  1321. else if (cc == PCI_CLASS_STORAGE_RAID)
  1322. scc_s = "RAID";
  1323. else
  1324. scc_s = "unknown";
  1325. dev_printk(KERN_INFO, &pdev->dev,
  1326. "AHCI %02x%02x.%02x%02x "
  1327. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1328. ,
  1329. (vers >> 24) & 0xff,
  1330. (vers >> 16) & 0xff,
  1331. (vers >> 8) & 0xff,
  1332. vers & 0xff,
  1333. ((cap >> 8) & 0x1f) + 1,
  1334. (cap & 0x1f) + 1,
  1335. speed_s,
  1336. impl,
  1337. scc_s);
  1338. dev_printk(KERN_INFO, &pdev->dev,
  1339. "flags: "
  1340. "%s%s%s%s%s%s"
  1341. "%s%s%s%s%s%s%s\n"
  1342. ,
  1343. cap & (1 << 31) ? "64bit " : "",
  1344. cap & (1 << 30) ? "ncq " : "",
  1345. cap & (1 << 28) ? "ilck " : "",
  1346. cap & (1 << 27) ? "stag " : "",
  1347. cap & (1 << 26) ? "pm " : "",
  1348. cap & (1 << 25) ? "led " : "",
  1349. cap & (1 << 24) ? "clo " : "",
  1350. cap & (1 << 19) ? "nz " : "",
  1351. cap & (1 << 18) ? "only " : "",
  1352. cap & (1 << 17) ? "pmp " : "",
  1353. cap & (1 << 15) ? "pio " : "",
  1354. cap & (1 << 14) ? "slum " : "",
  1355. cap & (1 << 13) ? "part " : ""
  1356. );
  1357. }
  1358. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1359. {
  1360. static int printed_version;
  1361. unsigned int board_idx = (unsigned int) ent->driver_data;
  1362. struct device *dev = &pdev->dev;
  1363. struct ata_probe_ent *probe_ent;
  1364. struct ahci_host_priv *hpriv;
  1365. int rc;
  1366. VPRINTK("ENTER\n");
  1367. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1368. if (!printed_version++)
  1369. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1370. if (pdev->vendor == PCI_VENDOR_ID_JMICRON) {
  1371. /* Function 1 is the PATA controller except on the 368, where
  1372. we are not AHCI anyway */
  1373. if (PCI_FUNC(pdev->devfn))
  1374. return -ENODEV;
  1375. }
  1376. rc = pcim_enable_device(pdev);
  1377. if (rc)
  1378. return rc;
  1379. rc = pcim_iomap_regions(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
  1380. if (rc == -EBUSY)
  1381. pcim_pin_device(pdev);
  1382. if (rc)
  1383. return rc;
  1384. if (pci_enable_msi(pdev))
  1385. pci_intx(pdev, 1);
  1386. probe_ent = devm_kzalloc(dev, sizeof(*probe_ent), GFP_KERNEL);
  1387. if (probe_ent == NULL)
  1388. return -ENOMEM;
  1389. probe_ent->dev = pci_dev_to_dev(pdev);
  1390. INIT_LIST_HEAD(&probe_ent->node);
  1391. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  1392. if (!hpriv)
  1393. return -ENOMEM;
  1394. probe_ent->sht = ahci_port_info[board_idx].sht;
  1395. probe_ent->port_flags = ahci_port_info[board_idx].flags;
  1396. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  1397. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  1398. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  1399. probe_ent->irq = pdev->irq;
  1400. probe_ent->irq_flags = IRQF_SHARED;
  1401. probe_ent->iomap = pcim_iomap_table(pdev);
  1402. probe_ent->private_data = hpriv;
  1403. /* initialize adapter */
  1404. rc = ahci_host_init(probe_ent);
  1405. if (rc)
  1406. return rc;
  1407. if (!(probe_ent->port_flags & AHCI_FLAG_NO_NCQ) &&
  1408. (hpriv->cap & HOST_CAP_NCQ))
  1409. probe_ent->port_flags |= ATA_FLAG_NCQ;
  1410. ahci_print_info(probe_ent);
  1411. if (!ata_device_add(probe_ent))
  1412. return -ENODEV;
  1413. devm_kfree(dev, probe_ent);
  1414. return 0;
  1415. }
  1416. static int __init ahci_init(void)
  1417. {
  1418. return pci_register_driver(&ahci_pci_driver);
  1419. }
  1420. static void __exit ahci_exit(void)
  1421. {
  1422. pci_unregister_driver(&ahci_pci_driver);
  1423. }
  1424. MODULE_AUTHOR("Jeff Garzik");
  1425. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1426. MODULE_LICENSE("GPL");
  1427. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1428. MODULE_VERSION(DRV_VERSION);
  1429. module_init(ahci_init);
  1430. module_exit(ahci_exit);