sata_mv.c 44 KB

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