ahci.c 46 KB

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