ahci.c 45 KB

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