ahci.c 45 KB

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