sata_mv.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2005: EMC Corporation, all rights reserved.
  5. *
  6. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/init.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/sched.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <linux/libata.h>
  35. #include <asm/io.h>
  36. #define DRV_NAME "sata_mv"
  37. #define DRV_VERSION "0.25"
  38. enum {
  39. /* BAR's are enumerated in terms of pci_resource_start() terms */
  40. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  41. MV_IO_BAR = 2, /* offset 0x18: IO space */
  42. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  43. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  44. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  45. MV_PCI_REG_BASE = 0,
  46. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  47. MV_SATAHC0_REG_BASE = 0x20000,
  48. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  49. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  50. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  51. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  52. MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
  53. MV_MAX_Q_DEPTH = 32,
  54. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  55. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  56. * CRPB needs alignment on a 256B boundary. Size == 256B
  57. * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
  58. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  59. */
  60. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  61. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  62. MV_MAX_SG_CT = 176,
  63. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  64. MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
  65. /* Our DMA boundary is determined by an ePRD being unable to handle
  66. * anything larger than 64KB
  67. */
  68. MV_DMA_BOUNDARY = 0xffffU,
  69. MV_PORTS_PER_HC = 4,
  70. /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
  71. MV_PORT_HC_SHIFT = 2,
  72. /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
  73. MV_PORT_MASK = 3,
  74. /* Host Flags */
  75. MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
  76. MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
  77. MV_FLAG_GLBL_SFT_RST = (1 << 28), /* Global Soft Reset support */
  78. MV_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  79. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO),
  80. MV_6XXX_FLAGS = (MV_FLAG_IRQ_COALESCE |
  81. MV_FLAG_GLBL_SFT_RST),
  82. chip_504x = 0,
  83. chip_508x = 1,
  84. chip_604x = 2,
  85. chip_608x = 3,
  86. CRQB_FLAG_READ = (1 << 0),
  87. CRQB_TAG_SHIFT = 1,
  88. CRQB_CMD_ADDR_SHIFT = 8,
  89. CRQB_CMD_CS = (0x2 << 11),
  90. CRQB_CMD_LAST = (1 << 15),
  91. CRPB_FLAG_STATUS_SHIFT = 8,
  92. EPRD_FLAG_END_OF_TBL = (1 << 31),
  93. /* PCI interface registers */
  94. PCI_COMMAND_OFS = 0xc00,
  95. PCI_MAIN_CMD_STS_OFS = 0xd30,
  96. STOP_PCI_MASTER = (1 << 2),
  97. PCI_MASTER_EMPTY = (1 << 3),
  98. GLOB_SFT_RST = (1 << 4),
  99. PCI_IRQ_CAUSE_OFS = 0x1d58,
  100. PCI_IRQ_MASK_OFS = 0x1d5c,
  101. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  102. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  103. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  104. PORT0_ERR = (1 << 0), /* shift by port # */
  105. PORT0_DONE = (1 << 1), /* shift by port # */
  106. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  107. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  108. PCI_ERR = (1 << 18),
  109. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  110. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  111. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  112. GPIO_INT = (1 << 22),
  113. SELF_INT = (1 << 23),
  114. TWSI_INT = (1 << 24),
  115. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  116. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  117. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  118. HC_MAIN_RSVD),
  119. /* SATAHC registers */
  120. HC_CFG_OFS = 0,
  121. HC_IRQ_CAUSE_OFS = 0x14,
  122. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  123. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  124. DEV_IRQ = (1 << 8), /* shift by port # */
  125. /* Shadow block registers */
  126. SHD_BLK_OFS = 0x100,
  127. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  128. /* SATA registers */
  129. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  130. SATA_ACTIVE_OFS = 0x350,
  131. /* Port registers */
  132. EDMA_CFG_OFS = 0,
  133. EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
  134. EDMA_CFG_NCQ = (1 << 5),
  135. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  136. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  137. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  138. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  139. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  140. EDMA_ERR_D_PAR = (1 << 0),
  141. EDMA_ERR_PRD_PAR = (1 << 1),
  142. EDMA_ERR_DEV = (1 << 2),
  143. EDMA_ERR_DEV_DCON = (1 << 3),
  144. EDMA_ERR_DEV_CON = (1 << 4),
  145. EDMA_ERR_SERR = (1 << 5),
  146. EDMA_ERR_SELF_DIS = (1 << 7),
  147. EDMA_ERR_BIST_ASYNC = (1 << 8),
  148. EDMA_ERR_CRBQ_PAR = (1 << 9),
  149. EDMA_ERR_CRPB_PAR = (1 << 10),
  150. EDMA_ERR_INTRL_PAR = (1 << 11),
  151. EDMA_ERR_IORDY = (1 << 12),
  152. EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
  153. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
  154. EDMA_ERR_LNK_DATA_RX = (0xf << 17),
  155. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
  156. EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
  157. EDMA_ERR_TRANS_PROTO = (1 << 31),
  158. EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  159. EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
  160. EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
  161. EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
  162. EDMA_ERR_LNK_DATA_RX |
  163. EDMA_ERR_LNK_DATA_TX |
  164. EDMA_ERR_TRANS_PROTO),
  165. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  166. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  167. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  168. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  169. EDMA_REQ_Q_PTR_SHIFT = 5,
  170. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  171. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  172. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  173. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  174. EDMA_RSP_Q_PTR_SHIFT = 3,
  175. EDMA_CMD_OFS = 0x28,
  176. EDMA_EN = (1 << 0),
  177. EDMA_DS = (1 << 1),
  178. ATA_RST = (1 << 2),
  179. /* Host private flags (hp_flags) */
  180. MV_HP_FLAG_MSI = (1 << 0),
  181. /* Port private flags (pp_flags) */
  182. MV_PP_FLAG_EDMA_EN = (1 << 0),
  183. MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
  184. };
  185. /* Command ReQuest Block: 32B */
  186. struct mv_crqb {
  187. u32 sg_addr;
  188. u32 sg_addr_hi;
  189. u16 ctrl_flags;
  190. u16 ata_cmd[11];
  191. };
  192. /* Command ResPonse Block: 8B */
  193. struct mv_crpb {
  194. u16 id;
  195. u16 flags;
  196. u32 tmstmp;
  197. };
  198. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  199. struct mv_sg {
  200. u32 addr;
  201. u32 flags_size;
  202. u32 addr_hi;
  203. u32 reserved;
  204. };
  205. struct mv_port_priv {
  206. struct mv_crqb *crqb;
  207. dma_addr_t crqb_dma;
  208. struct mv_crpb *crpb;
  209. dma_addr_t crpb_dma;
  210. struct mv_sg *sg_tbl;
  211. dma_addr_t sg_tbl_dma;
  212. unsigned req_producer; /* cp of req_in_ptr */
  213. unsigned rsp_consumer; /* cp of rsp_out_ptr */
  214. u32 pp_flags;
  215. };
  216. struct mv_host_priv {
  217. u32 hp_flags;
  218. };
  219. static void mv_irq_clear(struct ata_port *ap);
  220. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  221. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  222. static void mv_phy_reset(struct ata_port *ap);
  223. static void mv_host_stop(struct ata_host_set *host_set);
  224. static int mv_port_start(struct ata_port *ap);
  225. static void mv_port_stop(struct ata_port *ap);
  226. static void mv_qc_prep(struct ata_queued_cmd *qc);
  227. static int mv_qc_issue(struct ata_queued_cmd *qc);
  228. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  229. struct pt_regs *regs);
  230. static void mv_eng_timeout(struct ata_port *ap);
  231. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  232. static struct scsi_host_template mv_sht = {
  233. .module = THIS_MODULE,
  234. .name = DRV_NAME,
  235. .ioctl = ata_scsi_ioctl,
  236. .queuecommand = ata_scsi_queuecmd,
  237. .eh_strategy_handler = ata_scsi_error,
  238. .can_queue = MV_USE_Q_DEPTH,
  239. .this_id = ATA_SHT_THIS_ID,
  240. .sg_tablesize = MV_MAX_SG_CT,
  241. .max_sectors = ATA_MAX_SECTORS,
  242. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  243. .emulated = ATA_SHT_EMULATED,
  244. .use_clustering = ATA_SHT_USE_CLUSTERING,
  245. .proc_name = DRV_NAME,
  246. .dma_boundary = MV_DMA_BOUNDARY,
  247. .slave_configure = ata_scsi_slave_config,
  248. .bios_param = ata_std_bios_param,
  249. .ordered_flush = 1,
  250. };
  251. static const struct ata_port_operations mv_ops = {
  252. .port_disable = ata_port_disable,
  253. .tf_load = ata_tf_load,
  254. .tf_read = ata_tf_read,
  255. .check_status = ata_check_status,
  256. .exec_command = ata_exec_command,
  257. .dev_select = ata_std_dev_select,
  258. .phy_reset = mv_phy_reset,
  259. .qc_prep = mv_qc_prep,
  260. .qc_issue = mv_qc_issue,
  261. .eng_timeout = mv_eng_timeout,
  262. .irq_handler = mv_interrupt,
  263. .irq_clear = mv_irq_clear,
  264. .scr_read = mv_scr_read,
  265. .scr_write = mv_scr_write,
  266. .port_start = mv_port_start,
  267. .port_stop = mv_port_stop,
  268. .host_stop = mv_host_stop,
  269. };
  270. static struct ata_port_info mv_port_info[] = {
  271. { /* chip_504x */
  272. .sht = &mv_sht,
  273. .host_flags = MV_COMMON_FLAGS,
  274. .pio_mask = 0x1f, /* pio0-4 */
  275. .udma_mask = 0, /* 0x7f (udma0-6 disabled for now) */
  276. .port_ops = &mv_ops,
  277. },
  278. { /* chip_508x */
  279. .sht = &mv_sht,
  280. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  281. .pio_mask = 0x1f, /* pio0-4 */
  282. .udma_mask = 0, /* 0x7f (udma0-6 disabled for now) */
  283. .port_ops = &mv_ops,
  284. },
  285. { /* chip_604x */
  286. .sht = &mv_sht,
  287. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  288. .pio_mask = 0x1f, /* pio0-4 */
  289. .udma_mask = 0x7f, /* udma0-6 */
  290. .port_ops = &mv_ops,
  291. },
  292. { /* chip_608x */
  293. .sht = &mv_sht,
  294. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  295. MV_FLAG_DUAL_HC),
  296. .pio_mask = 0x1f, /* pio0-4 */
  297. .udma_mask = 0x7f, /* udma0-6 */
  298. .port_ops = &mv_ops,
  299. },
  300. };
  301. static struct pci_device_id mv_pci_tbl[] = {
  302. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
  303. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
  304. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_508x},
  305. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
  306. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
  307. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
  308. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
  309. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
  310. {} /* terminate list */
  311. };
  312. static struct pci_driver mv_pci_driver = {
  313. .name = DRV_NAME,
  314. .id_table = mv_pci_tbl,
  315. .probe = mv_init_one,
  316. .remove = ata_pci_remove_one,
  317. };
  318. /*
  319. * Functions
  320. */
  321. static inline void writelfl(unsigned long data, void __iomem *addr)
  322. {
  323. writel(data, addr);
  324. (void) readl(addr); /* flush to avoid PCI posted write */
  325. }
  326. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  327. {
  328. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  329. }
  330. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  331. {
  332. return (mv_hc_base(base, port >> MV_PORT_HC_SHIFT) +
  333. MV_SATAHC_ARBTR_REG_SZ +
  334. ((port & MV_PORT_MASK) * MV_PORT_REG_SZ));
  335. }
  336. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  337. {
  338. return mv_port_base(ap->host_set->mmio_base, ap->port_no);
  339. }
  340. static inline int mv_get_hc_count(unsigned long hp_flags)
  341. {
  342. return ((hp_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  343. }
  344. static void mv_irq_clear(struct ata_port *ap)
  345. {
  346. }
  347. /**
  348. * mv_start_dma - Enable eDMA engine
  349. * @base: port base address
  350. * @pp: port private data
  351. *
  352. * Verify the local cache of the eDMA state is accurate with an
  353. * assert.
  354. *
  355. * LOCKING:
  356. * Inherited from caller.
  357. */
  358. static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
  359. {
  360. if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
  361. writelfl(EDMA_EN, base + EDMA_CMD_OFS);
  362. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  363. }
  364. assert(EDMA_EN & readl(base + EDMA_CMD_OFS));
  365. }
  366. /**
  367. * mv_stop_dma - Disable eDMA engine
  368. * @ap: ATA channel to manipulate
  369. *
  370. * Verify the local cache of the eDMA state is accurate with an
  371. * assert.
  372. *
  373. * LOCKING:
  374. * Inherited from caller.
  375. */
  376. static void mv_stop_dma(struct ata_port *ap)
  377. {
  378. void __iomem *port_mmio = mv_ap_base(ap);
  379. struct mv_port_priv *pp = ap->private_data;
  380. u32 reg;
  381. int i;
  382. if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
  383. /* Disable EDMA if active. The disable bit auto clears.
  384. */
  385. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  386. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  387. } else {
  388. assert(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
  389. }
  390. /* now properly wait for the eDMA to stop */
  391. for (i = 1000; i > 0; i--) {
  392. reg = readl(port_mmio + EDMA_CMD_OFS);
  393. if (!(EDMA_EN & reg)) {
  394. break;
  395. }
  396. udelay(100);
  397. }
  398. if (EDMA_EN & reg) {
  399. printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
  400. /* FIXME: Consider doing a reset here to recover */
  401. }
  402. }
  403. #ifdef ATA_DEBUG
  404. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  405. {
  406. int b, w;
  407. for (b = 0; b < bytes; ) {
  408. DPRINTK("%p: ", start + b);
  409. for (w = 0; b < bytes && w < 4; w++) {
  410. printk("%08x ",readl(start + b));
  411. b += sizeof(u32);
  412. }
  413. printk("\n");
  414. }
  415. }
  416. #endif
  417. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  418. {
  419. #ifdef ATA_DEBUG
  420. int b, w;
  421. u32 dw;
  422. for (b = 0; b < bytes; ) {
  423. DPRINTK("%02x: ", b);
  424. for (w = 0; b < bytes && w < 4; w++) {
  425. (void) pci_read_config_dword(pdev,b,&dw);
  426. printk("%08x ",dw);
  427. b += sizeof(u32);
  428. }
  429. printk("\n");
  430. }
  431. #endif
  432. }
  433. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  434. struct pci_dev *pdev)
  435. {
  436. #ifdef ATA_DEBUG
  437. void __iomem *hc_base = mv_hc_base(mmio_base,
  438. port >> MV_PORT_HC_SHIFT);
  439. void __iomem *port_base;
  440. int start_port, num_ports, p, start_hc, num_hcs, hc;
  441. if (0 > port) {
  442. start_hc = start_port = 0;
  443. num_ports = 8; /* shld be benign for 4 port devs */
  444. num_hcs = 2;
  445. } else {
  446. start_hc = port >> MV_PORT_HC_SHIFT;
  447. start_port = port;
  448. num_ports = num_hcs = 1;
  449. }
  450. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  451. num_ports > 1 ? num_ports - 1 : start_port);
  452. if (NULL != pdev) {
  453. DPRINTK("PCI config space regs:\n");
  454. mv_dump_pci_cfg(pdev, 0x68);
  455. }
  456. DPRINTK("PCI regs:\n");
  457. mv_dump_mem(mmio_base+0xc00, 0x3c);
  458. mv_dump_mem(mmio_base+0xd00, 0x34);
  459. mv_dump_mem(mmio_base+0xf00, 0x4);
  460. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  461. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  462. hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
  463. DPRINTK("HC regs (HC %i):\n", hc);
  464. mv_dump_mem(hc_base, 0x1c);
  465. }
  466. for (p = start_port; p < start_port + num_ports; p++) {
  467. port_base = mv_port_base(mmio_base, p);
  468. DPRINTK("EDMA regs (port %i):\n",p);
  469. mv_dump_mem(port_base, 0x54);
  470. DPRINTK("SATA regs (port %i):\n",p);
  471. mv_dump_mem(port_base+0x300, 0x60);
  472. }
  473. #endif
  474. }
  475. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  476. {
  477. unsigned int ofs;
  478. switch (sc_reg_in) {
  479. case SCR_STATUS:
  480. case SCR_CONTROL:
  481. case SCR_ERROR:
  482. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  483. break;
  484. case SCR_ACTIVE:
  485. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  486. break;
  487. default:
  488. ofs = 0xffffffffU;
  489. break;
  490. }
  491. return ofs;
  492. }
  493. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  494. {
  495. unsigned int ofs = mv_scr_offset(sc_reg_in);
  496. if (0xffffffffU != ofs) {
  497. return readl(mv_ap_base(ap) + ofs);
  498. } else {
  499. return (u32) ofs;
  500. }
  501. }
  502. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  503. {
  504. unsigned int ofs = mv_scr_offset(sc_reg_in);
  505. if (0xffffffffU != ofs) {
  506. writelfl(val, mv_ap_base(ap) + ofs);
  507. }
  508. }
  509. /**
  510. * mv_global_soft_reset - Perform the 6xxx global soft reset
  511. * @mmio_base: base address of the HBA
  512. *
  513. * This routine only applies to 6xxx parts.
  514. *
  515. * LOCKING:
  516. * Inherited from caller.
  517. */
  518. static int mv_global_soft_reset(void __iomem *mmio_base)
  519. {
  520. void __iomem *reg = mmio_base + PCI_MAIN_CMD_STS_OFS;
  521. int i, rc = 0;
  522. u32 t;
  523. /* Following procedure defined in PCI "main command and status
  524. * register" table.
  525. */
  526. t = readl(reg);
  527. writel(t | STOP_PCI_MASTER, reg);
  528. for (i = 0; i < 1000; i++) {
  529. udelay(1);
  530. t = readl(reg);
  531. if (PCI_MASTER_EMPTY & t) {
  532. break;
  533. }
  534. }
  535. if (!(PCI_MASTER_EMPTY & t)) {
  536. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  537. rc = 1;
  538. goto done;
  539. }
  540. /* set reset */
  541. i = 5;
  542. do {
  543. writel(t | GLOB_SFT_RST, reg);
  544. t = readl(reg);
  545. udelay(1);
  546. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  547. if (!(GLOB_SFT_RST & t)) {
  548. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  549. rc = 1;
  550. goto done;
  551. }
  552. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  553. i = 5;
  554. do {
  555. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  556. t = readl(reg);
  557. udelay(1);
  558. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  559. if (GLOB_SFT_RST & t) {
  560. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  561. rc = 1;
  562. }
  563. done:
  564. return rc;
  565. }
  566. /**
  567. * mv_host_stop - Host specific cleanup/stop routine.
  568. * @host_set: host data structure
  569. *
  570. * Disable ints, cleanup host memory, call general purpose
  571. * host_stop.
  572. *
  573. * LOCKING:
  574. * Inherited from caller.
  575. */
  576. static void mv_host_stop(struct ata_host_set *host_set)
  577. {
  578. struct mv_host_priv *hpriv = host_set->private_data;
  579. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  580. if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
  581. pci_disable_msi(pdev);
  582. } else {
  583. pci_intx(pdev, 0);
  584. }
  585. kfree(hpriv);
  586. ata_host_stop(host_set);
  587. }
  588. static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
  589. {
  590. dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
  591. }
  592. /**
  593. * mv_port_start - Port specific init/start routine.
  594. * @ap: ATA channel to manipulate
  595. *
  596. * Allocate and point to DMA memory, init port private memory,
  597. * zero indices.
  598. *
  599. * LOCKING:
  600. * Inherited from caller.
  601. */
  602. static int mv_port_start(struct ata_port *ap)
  603. {
  604. struct device *dev = ap->host_set->dev;
  605. struct mv_port_priv *pp;
  606. void __iomem *port_mmio = mv_ap_base(ap);
  607. void *mem;
  608. dma_addr_t mem_dma;
  609. int rc = -ENOMEM;
  610. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  611. if (!pp)
  612. goto err_out;
  613. memset(pp, 0, sizeof(*pp));
  614. mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma,
  615. GFP_KERNEL);
  616. if (!mem)
  617. goto err_out_pp;
  618. memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
  619. rc = ata_pad_alloc(ap, dev);
  620. if (rc)
  621. goto err_out_priv;
  622. /* First item in chunk of DMA memory:
  623. * 32-slot command request table (CRQB), 32 bytes each in size
  624. */
  625. pp->crqb = mem;
  626. pp->crqb_dma = mem_dma;
  627. mem += MV_CRQB_Q_SZ;
  628. mem_dma += MV_CRQB_Q_SZ;
  629. /* Second item:
  630. * 32-slot command response table (CRPB), 8 bytes each in size
  631. */
  632. pp->crpb = mem;
  633. pp->crpb_dma = mem_dma;
  634. mem += MV_CRPB_Q_SZ;
  635. mem_dma += MV_CRPB_Q_SZ;
  636. /* Third item:
  637. * Table of scatter-gather descriptors (ePRD), 16 bytes each
  638. */
  639. pp->sg_tbl = mem;
  640. pp->sg_tbl_dma = mem_dma;
  641. writelfl(EDMA_CFG_Q_DEPTH | EDMA_CFG_RD_BRST_EXT |
  642. EDMA_CFG_WR_BUFF_LEN, port_mmio + EDMA_CFG_OFS);
  643. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  644. writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
  645. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  646. writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  647. writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  648. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  649. writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
  650. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  651. pp->req_producer = pp->rsp_consumer = 0;
  652. /* Don't turn on EDMA here...do it before DMA commands only. Else
  653. * we'll be unable to send non-data, PIO, etc due to restricted access
  654. * to shadow regs.
  655. */
  656. ap->private_data = pp;
  657. return 0;
  658. err_out_priv:
  659. mv_priv_free(pp, dev);
  660. err_out_pp:
  661. kfree(pp);
  662. err_out:
  663. return rc;
  664. }
  665. /**
  666. * mv_port_stop - Port specific cleanup/stop routine.
  667. * @ap: ATA channel to manipulate
  668. *
  669. * Stop DMA, cleanup port memory.
  670. *
  671. * LOCKING:
  672. * This routine uses the host_set lock to protect the DMA stop.
  673. */
  674. static void mv_port_stop(struct ata_port *ap)
  675. {
  676. struct device *dev = ap->host_set->dev;
  677. struct mv_port_priv *pp = ap->private_data;
  678. unsigned long flags;
  679. spin_lock_irqsave(&ap->host_set->lock, flags);
  680. mv_stop_dma(ap);
  681. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  682. ap->private_data = NULL;
  683. ata_pad_free(ap, dev);
  684. mv_priv_free(pp, dev);
  685. kfree(pp);
  686. }
  687. /**
  688. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  689. * @qc: queued command whose SG list to source from
  690. *
  691. * Populate the SG list and mark the last entry.
  692. *
  693. * LOCKING:
  694. * Inherited from caller.
  695. */
  696. static void mv_fill_sg(struct ata_queued_cmd *qc)
  697. {
  698. struct mv_port_priv *pp = qc->ap->private_data;
  699. unsigned int i = 0;
  700. struct scatterlist *sg;
  701. ata_for_each_sg(sg, qc) {
  702. u32 sg_len;
  703. dma_addr_t addr;
  704. addr = sg_dma_address(sg);
  705. sg_len = sg_dma_len(sg);
  706. pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
  707. pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  708. assert(0 == (sg_len & ~MV_DMA_BOUNDARY));
  709. pp->sg_tbl[i].flags_size = cpu_to_le32(sg_len);
  710. if (ata_sg_is_last(sg, qc))
  711. pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  712. i++;
  713. }
  714. }
  715. static inline unsigned mv_inc_q_index(unsigned *index)
  716. {
  717. *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
  718. return *index;
  719. }
  720. static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
  721. {
  722. *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  723. (last ? CRQB_CMD_LAST : 0);
  724. }
  725. /**
  726. * mv_qc_prep - Host specific command preparation.
  727. * @qc: queued command to prepare
  728. *
  729. * This routine simply redirects to the general purpose routine
  730. * if command is not DMA. Else, it handles prep of the CRQB
  731. * (command request block), does some sanity checking, and calls
  732. * the SG load routine.
  733. *
  734. * LOCKING:
  735. * Inherited from caller.
  736. */
  737. static void mv_qc_prep(struct ata_queued_cmd *qc)
  738. {
  739. struct ata_port *ap = qc->ap;
  740. struct mv_port_priv *pp = ap->private_data;
  741. u16 *cw;
  742. struct ata_taskfile *tf;
  743. u16 flags = 0;
  744. if (ATA_PROT_DMA != qc->tf.protocol) {
  745. return;
  746. }
  747. /* the req producer index should be the same as we remember it */
  748. assert(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
  749. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  750. pp->req_producer);
  751. /* Fill in command request block
  752. */
  753. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  754. flags |= CRQB_FLAG_READ;
  755. }
  756. assert(MV_MAX_Q_DEPTH > qc->tag);
  757. flags |= qc->tag << CRQB_TAG_SHIFT;
  758. pp->crqb[pp->req_producer].sg_addr =
  759. cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  760. pp->crqb[pp->req_producer].sg_addr_hi =
  761. cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  762. pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
  763. cw = &pp->crqb[pp->req_producer].ata_cmd[0];
  764. tf = &qc->tf;
  765. /* Sadly, the CRQB cannot accomodate all registers--there are
  766. * only 11 bytes...so we must pick and choose required
  767. * registers based on the command. So, we drop feature and
  768. * hob_feature for [RW] DMA commands, but they are needed for
  769. * NCQ. NCQ will drop hob_nsect.
  770. */
  771. switch (tf->command) {
  772. case ATA_CMD_READ:
  773. case ATA_CMD_READ_EXT:
  774. case ATA_CMD_WRITE:
  775. case ATA_CMD_WRITE_EXT:
  776. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  777. break;
  778. #ifdef LIBATA_NCQ /* FIXME: remove this line when NCQ added */
  779. case ATA_CMD_FPDMA_READ:
  780. case ATA_CMD_FPDMA_WRITE:
  781. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  782. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  783. break;
  784. #endif /* FIXME: remove this line when NCQ added */
  785. default:
  786. /* The only other commands EDMA supports in non-queued and
  787. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  788. * of which are defined/used by Linux. If we get here, this
  789. * driver needs work.
  790. *
  791. * FIXME: modify libata to give qc_prep a return value and
  792. * return error here.
  793. */
  794. BUG_ON(tf->command);
  795. break;
  796. }
  797. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  798. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  799. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  800. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  801. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  802. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  803. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  804. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  805. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  806. if (!(qc->flags & ATA_QCFLAG_DMAMAP)) {
  807. return;
  808. }
  809. mv_fill_sg(qc);
  810. }
  811. /**
  812. * mv_qc_issue - Initiate a command to the host
  813. * @qc: queued command to start
  814. *
  815. * This routine simply redirects to the general purpose routine
  816. * if command is not DMA. Else, it sanity checks our local
  817. * caches of the request producer/consumer indices then enables
  818. * DMA and bumps the request producer index.
  819. *
  820. * LOCKING:
  821. * Inherited from caller.
  822. */
  823. static int mv_qc_issue(struct ata_queued_cmd *qc)
  824. {
  825. void __iomem *port_mmio = mv_ap_base(qc->ap);
  826. struct mv_port_priv *pp = qc->ap->private_data;
  827. u32 in_ptr;
  828. if (ATA_PROT_DMA != qc->tf.protocol) {
  829. /* We're about to send a non-EDMA capable command to the
  830. * port. Turn off EDMA so there won't be problems accessing
  831. * shadow block, etc registers.
  832. */
  833. mv_stop_dma(qc->ap);
  834. return ata_qc_issue_prot(qc);
  835. }
  836. in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  837. /* the req producer index should be the same as we remember it */
  838. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  839. pp->req_producer);
  840. /* until we do queuing, the queue should be empty at this point */
  841. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  842. ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >>
  843. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  844. mv_inc_q_index(&pp->req_producer); /* now incr producer index */
  845. mv_start_dma(port_mmio, pp);
  846. /* and write the request in pointer to kick the EDMA to life */
  847. in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
  848. in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
  849. writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  850. return 0;
  851. }
  852. /**
  853. * mv_get_crpb_status - get status from most recently completed cmd
  854. * @ap: ATA channel to manipulate
  855. *
  856. * This routine is for use when the port is in DMA mode, when it
  857. * will be using the CRPB (command response block) method of
  858. * returning command completion information. We assert indices
  859. * are good, grab status, and bump the response consumer index to
  860. * prove that we're up to date.
  861. *
  862. * LOCKING:
  863. * Inherited from caller.
  864. */
  865. static u8 mv_get_crpb_status(struct ata_port *ap)
  866. {
  867. void __iomem *port_mmio = mv_ap_base(ap);
  868. struct mv_port_priv *pp = ap->private_data;
  869. u32 out_ptr;
  870. out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  871. /* the response consumer index should be the same as we remember it */
  872. assert(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  873. pp->rsp_consumer);
  874. /* increment our consumer index... */
  875. pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
  876. /* and, until we do NCQ, there should only be 1 CRPB waiting */
  877. assert(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >>
  878. EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  879. pp->rsp_consumer);
  880. /* write out our inc'd consumer index so EDMA knows we're caught up */
  881. out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
  882. out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
  883. writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  884. /* Return ATA status register for completed CRPB */
  885. return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
  886. }
  887. /**
  888. * mv_err_intr - Handle error interrupts on the port
  889. * @ap: ATA channel to manipulate
  890. *
  891. * In most cases, just clear the interrupt and move on. However,
  892. * some cases require an eDMA reset, which is done right before
  893. * the COMRESET in mv_phy_reset(). The SERR case requires a
  894. * clear of pending errors in the SATA SERROR register. Finally,
  895. * if the port disabled DMA, update our cached copy to match.
  896. *
  897. * LOCKING:
  898. * Inherited from caller.
  899. */
  900. static void mv_err_intr(struct ata_port *ap)
  901. {
  902. void __iomem *port_mmio = mv_ap_base(ap);
  903. u32 edma_err_cause, serr = 0;
  904. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  905. if (EDMA_ERR_SERR & edma_err_cause) {
  906. serr = scr_read(ap, SCR_ERROR);
  907. scr_write_flush(ap, SCR_ERROR, serr);
  908. }
  909. if (EDMA_ERR_SELF_DIS & edma_err_cause) {
  910. struct mv_port_priv *pp = ap->private_data;
  911. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  912. }
  913. DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
  914. "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
  915. /* Clear EDMA now that SERR cleanup done */
  916. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  917. /* check for fatal here and recover if needed */
  918. if (EDMA_ERR_FATAL & edma_err_cause) {
  919. mv_phy_reset(ap);
  920. }
  921. }
  922. /**
  923. * mv_host_intr - Handle all interrupts on the given host controller
  924. * @host_set: host specific structure
  925. * @relevant: port error bits relevant to this host controller
  926. * @hc: which host controller we're to look at
  927. *
  928. * Read then write clear the HC interrupt status then walk each
  929. * port connected to the HC and see if it needs servicing. Port
  930. * success ints are reported in the HC interrupt status reg, the
  931. * port error ints are reported in the higher level main
  932. * interrupt status register and thus are passed in via the
  933. * 'relevant' argument.
  934. *
  935. * LOCKING:
  936. * Inherited from caller.
  937. */
  938. static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
  939. unsigned int hc)
  940. {
  941. void __iomem *mmio = host_set->mmio_base;
  942. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  943. struct ata_port *ap;
  944. struct ata_queued_cmd *qc;
  945. u32 hc_irq_cause;
  946. int shift, port, port0, hard_port, handled;
  947. unsigned int err_mask;
  948. u8 ata_status = 0;
  949. if (hc == 0) {
  950. port0 = 0;
  951. } else {
  952. port0 = MV_PORTS_PER_HC;
  953. }
  954. /* we'll need the HC success int register in most cases */
  955. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  956. if (hc_irq_cause) {
  957. writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  958. }
  959. VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
  960. hc,relevant,hc_irq_cause);
  961. for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
  962. ap = host_set->ports[port];
  963. hard_port = port & MV_PORT_MASK; /* range 0-3 */
  964. handled = 0; /* ensure ata_status is set if handled++ */
  965. if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
  966. /* new CRPB on the queue; just one at a time until NCQ
  967. */
  968. ata_status = mv_get_crpb_status(ap);
  969. handled++;
  970. } else if ((DEV_IRQ << hard_port) & hc_irq_cause) {
  971. /* received ATA IRQ; read the status reg to clear INTRQ
  972. */
  973. ata_status = readb((void __iomem *)
  974. ap->ioaddr.status_addr);
  975. handled++;
  976. }
  977. err_mask = ac_err_mask(ata_status);
  978. shift = port << 1; /* (port * 2) */
  979. if (port >= MV_PORTS_PER_HC) {
  980. shift++; /* skip bit 8 in the HC Main IRQ reg */
  981. }
  982. if ((PORT0_ERR << shift) & relevant) {
  983. mv_err_intr(ap);
  984. err_mask |= AC_ERR_OTHER;
  985. handled++;
  986. }
  987. if (handled && ap) {
  988. qc = ata_qc_from_tag(ap, ap->active_tag);
  989. if (NULL != qc) {
  990. VPRINTK("port %u IRQ found for qc, "
  991. "ata_status 0x%x\n", port,ata_status);
  992. /* mark qc status appropriately */
  993. ata_qc_complete(qc, err_mask);
  994. }
  995. }
  996. }
  997. VPRINTK("EXIT\n");
  998. }
  999. /**
  1000. * mv_interrupt -
  1001. * @irq: unused
  1002. * @dev_instance: private data; in this case the host structure
  1003. * @regs: unused
  1004. *
  1005. * Read the read only register to determine if any host
  1006. * controllers have pending interrupts. If so, call lower level
  1007. * routine to handle. Also check for PCI errors which are only
  1008. * reported here.
  1009. *
  1010. * LOCKING:
  1011. * This routine holds the host_set lock while processing pending
  1012. * interrupts.
  1013. */
  1014. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  1015. struct pt_regs *regs)
  1016. {
  1017. struct ata_host_set *host_set = dev_instance;
  1018. unsigned int hc, handled = 0, n_hcs;
  1019. void __iomem *mmio = host_set->mmio_base;
  1020. u32 irq_stat;
  1021. irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
  1022. /* check the cases where we either have nothing pending or have read
  1023. * a bogus register value which can indicate HW removal or PCI fault
  1024. */
  1025. if (!irq_stat || (0xffffffffU == irq_stat)) {
  1026. return IRQ_NONE;
  1027. }
  1028. n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
  1029. spin_lock(&host_set->lock);
  1030. for (hc = 0; hc < n_hcs; hc++) {
  1031. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  1032. if (relevant) {
  1033. mv_host_intr(host_set, relevant, hc);
  1034. handled++;
  1035. }
  1036. }
  1037. if (PCI_ERR & irq_stat) {
  1038. printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
  1039. readl(mmio + PCI_IRQ_CAUSE_OFS));
  1040. DPRINTK("All regs @ PCI error\n");
  1041. mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
  1042. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1043. handled++;
  1044. }
  1045. spin_unlock(&host_set->lock);
  1046. return IRQ_RETVAL(handled);
  1047. }
  1048. /**
  1049. * mv_phy_reset - Perform eDMA reset followed by COMRESET
  1050. * @ap: ATA channel to manipulate
  1051. *
  1052. * Part of this is taken from __sata_phy_reset and modified to
  1053. * not sleep since this routine gets called from interrupt level.
  1054. *
  1055. * LOCKING:
  1056. * Inherited from caller. This is coded to safe to call at
  1057. * interrupt level, i.e. it does not sleep.
  1058. */
  1059. static void mv_phy_reset(struct ata_port *ap)
  1060. {
  1061. void __iomem *port_mmio = mv_ap_base(ap);
  1062. struct ata_taskfile tf;
  1063. struct ata_device *dev = &ap->device[0];
  1064. unsigned long timeout;
  1065. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  1066. mv_stop_dma(ap);
  1067. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  1068. udelay(25); /* allow reset propagation */
  1069. /* Spec never mentions clearing the bit. Marvell's driver does
  1070. * clear the bit, however.
  1071. */
  1072. writelfl(0, port_mmio + EDMA_CMD_OFS);
  1073. VPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  1074. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1075. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1076. /* proceed to init communications via the scr_control reg */
  1077. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1078. mdelay(1);
  1079. scr_write_flush(ap, SCR_CONTROL, 0x300);
  1080. timeout = jiffies + (HZ * 1);
  1081. do {
  1082. mdelay(10);
  1083. if ((scr_read(ap, SCR_STATUS) & 0xf) != 1)
  1084. break;
  1085. } while (time_before(jiffies, timeout));
  1086. VPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  1087. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1088. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1089. if (sata_dev_present(ap)) {
  1090. ata_port_probe(ap);
  1091. } else {
  1092. printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
  1093. ap->id, scr_read(ap, SCR_STATUS));
  1094. ata_port_disable(ap);
  1095. return;
  1096. }
  1097. ap->cbl = ATA_CBL_SATA;
  1098. tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
  1099. tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
  1100. tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
  1101. tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
  1102. dev->class = ata_dev_classify(&tf);
  1103. if (!ata_dev_present(dev)) {
  1104. VPRINTK("Port disabled post-sig: No device present.\n");
  1105. ata_port_disable(ap);
  1106. }
  1107. VPRINTK("EXIT\n");
  1108. }
  1109. /**
  1110. * mv_eng_timeout - Routine called by libata when SCSI times out I/O
  1111. * @ap: ATA channel to manipulate
  1112. *
  1113. * Intent is to clear all pending error conditions, reset the
  1114. * chip/bus, fail the command, and move on.
  1115. *
  1116. * LOCKING:
  1117. * This routine holds the host_set lock while failing the command.
  1118. */
  1119. static void mv_eng_timeout(struct ata_port *ap)
  1120. {
  1121. struct ata_queued_cmd *qc;
  1122. unsigned long flags;
  1123. printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
  1124. DPRINTK("All regs @ start of eng_timeout\n");
  1125. mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
  1126. to_pci_dev(ap->host_set->dev));
  1127. qc = ata_qc_from_tag(ap, ap->active_tag);
  1128. printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
  1129. ap->host_set->mmio_base, ap, qc, qc->scsicmd,
  1130. &qc->scsicmd->cmnd);
  1131. mv_err_intr(ap);
  1132. mv_phy_reset(ap);
  1133. if (!qc) {
  1134. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  1135. ap->id);
  1136. } else {
  1137. /* hack alert! We cannot use the supplied completion
  1138. * function from inside the ->eh_strategy_handler() thread.
  1139. * libata is the only user of ->eh_strategy_handler() in
  1140. * any kernel, so the default scsi_done() assumes it is
  1141. * not being called from the SCSI EH.
  1142. */
  1143. spin_lock_irqsave(&ap->host_set->lock, flags);
  1144. qc->scsidone = scsi_finish_command;
  1145. ata_qc_complete(qc, AC_ERR_OTHER);
  1146. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1147. }
  1148. }
  1149. /**
  1150. * mv_port_init - Perform some early initialization on a single port.
  1151. * @port: libata data structure storing shadow register addresses
  1152. * @port_mmio: base address of the port
  1153. *
  1154. * Initialize shadow register mmio addresses, clear outstanding
  1155. * interrupts on the port, and unmask interrupts for the future
  1156. * start of the port.
  1157. *
  1158. * LOCKING:
  1159. * Inherited from caller.
  1160. */
  1161. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  1162. {
  1163. unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
  1164. unsigned serr_ofs;
  1165. /* PIO related setup
  1166. */
  1167. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  1168. port->error_addr =
  1169. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  1170. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  1171. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  1172. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  1173. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  1174. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  1175. port->status_addr =
  1176. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  1177. /* special case: control/altstatus doesn't have ATA_REG_ address */
  1178. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  1179. /* unused: */
  1180. port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
  1181. /* Clear any currently outstanding port interrupt conditions */
  1182. serr_ofs = mv_scr_offset(SCR_ERROR);
  1183. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  1184. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1185. /* unmask all EDMA error interrupts */
  1186. writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  1187. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  1188. readl(port_mmio + EDMA_CFG_OFS),
  1189. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  1190. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  1191. }
  1192. /**
  1193. * mv_host_init - Perform some early initialization of the host.
  1194. * @probe_ent: early data struct representing the host
  1195. *
  1196. * If possible, do an early global reset of the host. Then do
  1197. * our port init and clear/unmask all/relevant host interrupts.
  1198. *
  1199. * LOCKING:
  1200. * Inherited from caller.
  1201. */
  1202. static int mv_host_init(struct ata_probe_ent *probe_ent)
  1203. {
  1204. int rc = 0, n_hc, port, hc;
  1205. void __iomem *mmio = probe_ent->mmio_base;
  1206. void __iomem *port_mmio;
  1207. if ((MV_FLAG_GLBL_SFT_RST & probe_ent->host_flags) &&
  1208. mv_global_soft_reset(probe_ent->mmio_base)) {
  1209. rc = 1;
  1210. goto done;
  1211. }
  1212. n_hc = mv_get_hc_count(probe_ent->host_flags);
  1213. probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
  1214. for (port = 0; port < probe_ent->n_ports; port++) {
  1215. port_mmio = mv_port_base(mmio, port);
  1216. mv_port_init(&probe_ent->port[port], port_mmio);
  1217. }
  1218. for (hc = 0; hc < n_hc; hc++) {
  1219. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1220. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  1221. "(before clear)=0x%08x\n", hc,
  1222. readl(hc_mmio + HC_CFG_OFS),
  1223. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  1224. /* Clear any currently outstanding hc interrupt conditions */
  1225. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  1226. }
  1227. /* Clear any currently outstanding host interrupt conditions */
  1228. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1229. /* and unmask interrupt generation for host regs */
  1230. writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
  1231. writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
  1232. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  1233. "PCI int cause/mask=0x%08x/0x%08x\n",
  1234. readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
  1235. readl(mmio + HC_MAIN_IRQ_MASK_OFS),
  1236. readl(mmio + PCI_IRQ_CAUSE_OFS),
  1237. readl(mmio + PCI_IRQ_MASK_OFS));
  1238. done:
  1239. return rc;
  1240. }
  1241. /**
  1242. * mv_print_info - Dump key info to kernel log for perusal.
  1243. * @probe_ent: early data struct representing the host
  1244. *
  1245. * FIXME: complete this.
  1246. *
  1247. * LOCKING:
  1248. * Inherited from caller.
  1249. */
  1250. static void mv_print_info(struct ata_probe_ent *probe_ent)
  1251. {
  1252. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1253. struct mv_host_priv *hpriv = probe_ent->private_data;
  1254. u8 rev_id, scc;
  1255. const char *scc_s;
  1256. /* Use this to determine the HW stepping of the chip so we know
  1257. * what errata to workaround
  1258. */
  1259. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1260. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  1261. if (scc == 0)
  1262. scc_s = "SCSI";
  1263. else if (scc == 0x01)
  1264. scc_s = "RAID";
  1265. else
  1266. scc_s = "unknown";
  1267. dev_printk(KERN_INFO, &pdev->dev,
  1268. "%u slots %u ports %s mode IRQ via %s\n",
  1269. (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
  1270. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  1271. }
  1272. /**
  1273. * mv_init_one - handle a positive probe of a Marvell host
  1274. * @pdev: PCI device found
  1275. * @ent: PCI device ID entry for the matched host
  1276. *
  1277. * LOCKING:
  1278. * Inherited from caller.
  1279. */
  1280. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1281. {
  1282. static int printed_version = 0;
  1283. struct ata_probe_ent *probe_ent = NULL;
  1284. struct mv_host_priv *hpriv;
  1285. unsigned int board_idx = (unsigned int)ent->driver_data;
  1286. void __iomem *mmio_base;
  1287. int pci_dev_busy = 0, rc;
  1288. if (!printed_version++)
  1289. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  1290. rc = pci_enable_device(pdev);
  1291. if (rc) {
  1292. return rc;
  1293. }
  1294. rc = pci_request_regions(pdev, DRV_NAME);
  1295. if (rc) {
  1296. pci_dev_busy = 1;
  1297. goto err_out;
  1298. }
  1299. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1300. if (probe_ent == NULL) {
  1301. rc = -ENOMEM;
  1302. goto err_out_regions;
  1303. }
  1304. memset(probe_ent, 0, sizeof(*probe_ent));
  1305. probe_ent->dev = pci_dev_to_dev(pdev);
  1306. INIT_LIST_HEAD(&probe_ent->node);
  1307. mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
  1308. if (mmio_base == NULL) {
  1309. rc = -ENOMEM;
  1310. goto err_out_free_ent;
  1311. }
  1312. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1313. if (!hpriv) {
  1314. rc = -ENOMEM;
  1315. goto err_out_iounmap;
  1316. }
  1317. memset(hpriv, 0, sizeof(*hpriv));
  1318. probe_ent->sht = mv_port_info[board_idx].sht;
  1319. probe_ent->host_flags = mv_port_info[board_idx].host_flags;
  1320. probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
  1321. probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
  1322. probe_ent->port_ops = mv_port_info[board_idx].port_ops;
  1323. probe_ent->irq = pdev->irq;
  1324. probe_ent->irq_flags = SA_SHIRQ;
  1325. probe_ent->mmio_base = mmio_base;
  1326. probe_ent->private_data = hpriv;
  1327. /* initialize adapter */
  1328. rc = mv_host_init(probe_ent);
  1329. if (rc) {
  1330. goto err_out_hpriv;
  1331. }
  1332. /* Enable interrupts */
  1333. if (pci_enable_msi(pdev) == 0) {
  1334. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  1335. } else {
  1336. pci_intx(pdev, 1);
  1337. }
  1338. mv_dump_pci_cfg(pdev, 0x68);
  1339. mv_print_info(probe_ent);
  1340. if (ata_device_add(probe_ent) == 0) {
  1341. rc = -ENODEV; /* No devices discovered */
  1342. goto err_out_dev_add;
  1343. }
  1344. kfree(probe_ent);
  1345. return 0;
  1346. err_out_dev_add:
  1347. if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
  1348. pci_disable_msi(pdev);
  1349. } else {
  1350. pci_intx(pdev, 0);
  1351. }
  1352. err_out_hpriv:
  1353. kfree(hpriv);
  1354. err_out_iounmap:
  1355. pci_iounmap(pdev, mmio_base);
  1356. err_out_free_ent:
  1357. kfree(probe_ent);
  1358. err_out_regions:
  1359. pci_release_regions(pdev);
  1360. err_out:
  1361. if (!pci_dev_busy) {
  1362. pci_disable_device(pdev);
  1363. }
  1364. return rc;
  1365. }
  1366. static int __init mv_init(void)
  1367. {
  1368. return pci_module_init(&mv_pci_driver);
  1369. }
  1370. static void __exit mv_exit(void)
  1371. {
  1372. pci_unregister_driver(&mv_pci_driver);
  1373. }
  1374. MODULE_AUTHOR("Brett Russ");
  1375. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  1376. MODULE_LICENSE("GPL");
  1377. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  1378. MODULE_VERSION(DRV_VERSION);
  1379. module_init(mv_init);
  1380. module_exit(mv_exit);