sata_mv.c 43 KB

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