sata_mv.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241
  1. /*
  2. * sata_mv.c - Marvell SATA support
  3. *
  4. * Copyright 2005: EMC Corporation, all rights reserved.
  5. *
  6. * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/init.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/sched.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <linux/libata.h>
  35. #include <asm/io.h>
  36. #define DRV_NAME "sata_mv"
  37. #define DRV_VERSION "0.25"
  38. enum {
  39. /* BAR's are enumerated in terms of pci_resource_start() terms */
  40. MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
  41. MV_IO_BAR = 2, /* offset 0x18: IO space */
  42. MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
  43. MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
  44. MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
  45. MV_PCI_REG_BASE = 0,
  46. MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
  47. MV_SATAHC0_REG_BASE = 0x20000,
  48. MV_FLASH_CTL = 0x1046c,
  49. MV_GPIO_PORT_CTL = 0x104f0,
  50. MV_RESET_CFG = 0x180d8,
  51. MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  52. MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
  53. MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
  54. MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
  55. MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
  56. MV_MAX_Q_DEPTH = 32,
  57. MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
  58. /* CRQB needs alignment on a 1KB boundary. Size == 1KB
  59. * CRPB needs alignment on a 256B boundary. Size == 256B
  60. * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
  61. * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
  62. */
  63. MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
  64. MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
  65. MV_MAX_SG_CT = 176,
  66. MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
  67. MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
  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_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  77. ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO),
  78. MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
  79. CRQB_FLAG_READ = (1 << 0),
  80. CRQB_TAG_SHIFT = 1,
  81. CRQB_CMD_ADDR_SHIFT = 8,
  82. CRQB_CMD_CS = (0x2 << 11),
  83. CRQB_CMD_LAST = (1 << 15),
  84. CRPB_FLAG_STATUS_SHIFT = 8,
  85. EPRD_FLAG_END_OF_TBL = (1 << 31),
  86. /* PCI interface registers */
  87. PCI_COMMAND_OFS = 0xc00,
  88. PCI_MAIN_CMD_STS_OFS = 0xd30,
  89. STOP_PCI_MASTER = (1 << 2),
  90. PCI_MASTER_EMPTY = (1 << 3),
  91. GLOB_SFT_RST = (1 << 4),
  92. MV_PCI_MODE = 0xd00,
  93. MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
  94. MV_PCI_DISC_TIMER = 0xd04,
  95. MV_PCI_MSI_TRIGGER = 0xc38,
  96. MV_PCI_SERR_MASK = 0xc28,
  97. MV_PCI_XBAR_TMOUT = 0x1d04,
  98. MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
  99. MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
  100. MV_PCI_ERR_ATTRIBUTE = 0x1d48,
  101. MV_PCI_ERR_COMMAND = 0x1d50,
  102. PCI_IRQ_CAUSE_OFS = 0x1d58,
  103. PCI_IRQ_MASK_OFS = 0x1d5c,
  104. PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
  105. HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
  106. HC_MAIN_IRQ_MASK_OFS = 0x1d64,
  107. PORT0_ERR = (1 << 0), /* shift by port # */
  108. PORT0_DONE = (1 << 1), /* shift by port # */
  109. HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
  110. HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
  111. PCI_ERR = (1 << 18),
  112. TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
  113. TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
  114. PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
  115. GPIO_INT = (1 << 22),
  116. SELF_INT = (1 << 23),
  117. TWSI_INT = (1 << 24),
  118. HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
  119. HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
  120. PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
  121. HC_MAIN_RSVD),
  122. /* SATAHC registers */
  123. HC_CFG_OFS = 0,
  124. HC_IRQ_CAUSE_OFS = 0x14,
  125. CRPB_DMA_DONE = (1 << 0), /* shift by port # */
  126. HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
  127. DEV_IRQ = (1 << 8), /* shift by port # */
  128. /* Shadow block registers */
  129. SHD_BLK_OFS = 0x100,
  130. SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
  131. /* SATA registers */
  132. SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
  133. SATA_ACTIVE_OFS = 0x350,
  134. PHY_MODE3 = 0x310,
  135. PHY_MODE4 = 0x314,
  136. PHY_MODE2 = 0x330,
  137. MV5_PHY_MODE = 0x74,
  138. MV5_LT_MODE = 0x30,
  139. MV5_PHY_CTL = 0x0C,
  140. SATA_INTERFACE_CTL = 0x050,
  141. MV_M2_PREAMP_MASK = 0x7e0,
  142. /* Port registers */
  143. EDMA_CFG_OFS = 0,
  144. EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
  145. EDMA_CFG_NCQ = (1 << 5),
  146. EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
  147. EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
  148. EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
  149. EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
  150. EDMA_ERR_IRQ_MASK_OFS = 0xc,
  151. EDMA_ERR_D_PAR = (1 << 0),
  152. EDMA_ERR_PRD_PAR = (1 << 1),
  153. EDMA_ERR_DEV = (1 << 2),
  154. EDMA_ERR_DEV_DCON = (1 << 3),
  155. EDMA_ERR_DEV_CON = (1 << 4),
  156. EDMA_ERR_SERR = (1 << 5),
  157. EDMA_ERR_SELF_DIS = (1 << 7),
  158. EDMA_ERR_BIST_ASYNC = (1 << 8),
  159. EDMA_ERR_CRBQ_PAR = (1 << 9),
  160. EDMA_ERR_CRPB_PAR = (1 << 10),
  161. EDMA_ERR_INTRL_PAR = (1 << 11),
  162. EDMA_ERR_IORDY = (1 << 12),
  163. EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
  164. EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
  165. EDMA_ERR_LNK_DATA_RX = (0xf << 17),
  166. EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
  167. EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
  168. EDMA_ERR_TRANS_PROTO = (1 << 31),
  169. EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
  170. EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
  171. EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
  172. EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
  173. EDMA_ERR_LNK_DATA_RX |
  174. EDMA_ERR_LNK_DATA_TX |
  175. EDMA_ERR_TRANS_PROTO),
  176. EDMA_REQ_Q_BASE_HI_OFS = 0x10,
  177. EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
  178. EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
  179. EDMA_REQ_Q_PTR_SHIFT = 5,
  180. EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
  181. EDMA_RSP_Q_IN_PTR_OFS = 0x20,
  182. EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
  183. EDMA_RSP_Q_PTR_SHIFT = 3,
  184. EDMA_CMD_OFS = 0x28,
  185. EDMA_EN = (1 << 0),
  186. EDMA_DS = (1 << 1),
  187. ATA_RST = (1 << 2),
  188. EDMA_IORDY_TMOUT = 0x34,
  189. EDMA_ARB_CFG = 0x38,
  190. /* Host private flags (hp_flags) */
  191. MV_HP_FLAG_MSI = (1 << 0),
  192. MV_HP_ERRATA_50XXB0 = (1 << 1),
  193. MV_HP_ERRATA_50XXB2 = (1 << 2),
  194. MV_HP_ERRATA_60X1B2 = (1 << 3),
  195. MV_HP_ERRATA_60X1C0 = (1 << 4),
  196. MV_HP_50XX = (1 << 5),
  197. /* Port private flags (pp_flags) */
  198. MV_PP_FLAG_EDMA_EN = (1 << 0),
  199. MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
  200. };
  201. #define IS_50XX(hpriv) ((hpriv)->hp_flags & MV_HP_50XX)
  202. #define IS_60XX(hpriv) (((hpriv)->hp_flags & MV_HP_50XX) == 0)
  203. enum {
  204. /* Our DMA boundary is determined by an ePRD being unable to handle
  205. * anything larger than 64KB
  206. */
  207. MV_DMA_BOUNDARY = 0xffffU,
  208. EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
  209. EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
  210. };
  211. enum chip_type {
  212. chip_504x,
  213. chip_508x,
  214. chip_5080,
  215. chip_604x,
  216. chip_608x,
  217. };
  218. /* Command ReQuest Block: 32B */
  219. struct mv_crqb {
  220. u32 sg_addr;
  221. u32 sg_addr_hi;
  222. u16 ctrl_flags;
  223. u16 ata_cmd[11];
  224. };
  225. /* Command ResPonse Block: 8B */
  226. struct mv_crpb {
  227. u16 id;
  228. u16 flags;
  229. u32 tmstmp;
  230. };
  231. /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
  232. struct mv_sg {
  233. u32 addr;
  234. u32 flags_size;
  235. u32 addr_hi;
  236. u32 reserved;
  237. };
  238. struct mv_port_priv {
  239. struct mv_crqb *crqb;
  240. dma_addr_t crqb_dma;
  241. struct mv_crpb *crpb;
  242. dma_addr_t crpb_dma;
  243. struct mv_sg *sg_tbl;
  244. dma_addr_t sg_tbl_dma;
  245. unsigned req_producer; /* cp of req_in_ptr */
  246. unsigned rsp_consumer; /* cp of rsp_out_ptr */
  247. u32 pp_flags;
  248. };
  249. struct mv_port_signal {
  250. u32 amps;
  251. u32 pre;
  252. };
  253. struct mv_host_priv;
  254. struct mv_hw_ops {
  255. void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
  256. unsigned int port);
  257. void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
  258. void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
  259. void __iomem *mmio);
  260. int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
  261. unsigned int n_hc);
  262. void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
  263. void (*reset_bus)(struct pci_dev *pdev, void __iomem *mmio);
  264. };
  265. struct mv_host_priv {
  266. u32 hp_flags;
  267. struct mv_port_signal signal[8];
  268. const struct mv_hw_ops *ops;
  269. };
  270. static void mv_irq_clear(struct ata_port *ap);
  271. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  272. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  273. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
  274. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
  275. static void mv_phy_reset(struct ata_port *ap);
  276. static void __mv_phy_reset(struct ata_port *ap, int can_sleep);
  277. static void mv_host_stop(struct ata_host_set *host_set);
  278. static int mv_port_start(struct ata_port *ap);
  279. static void mv_port_stop(struct ata_port *ap);
  280. static void mv_qc_prep(struct ata_queued_cmd *qc);
  281. static int mv_qc_issue(struct ata_queued_cmd *qc);
  282. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  283. struct pt_regs *regs);
  284. static void mv_eng_timeout(struct ata_port *ap);
  285. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  286. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  287. unsigned int port);
  288. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  289. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  290. void __iomem *mmio);
  291. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  292. unsigned int n_hc);
  293. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  294. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio);
  295. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  296. unsigned int port);
  297. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
  298. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  299. void __iomem *mmio);
  300. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  301. unsigned int n_hc);
  302. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
  303. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio);
  304. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  305. unsigned int port_no);
  306. static void mv_stop_and_reset(struct ata_port *ap);
  307. static struct scsi_host_template mv_sht = {
  308. .module = THIS_MODULE,
  309. .name = DRV_NAME,
  310. .ioctl = ata_scsi_ioctl,
  311. .queuecommand = ata_scsi_queuecmd,
  312. .eh_strategy_handler = ata_scsi_error,
  313. .can_queue = MV_USE_Q_DEPTH,
  314. .this_id = ATA_SHT_THIS_ID,
  315. .sg_tablesize = MV_MAX_SG_CT / 2,
  316. .max_sectors = ATA_MAX_SECTORS,
  317. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  318. .emulated = ATA_SHT_EMULATED,
  319. .use_clustering = ATA_SHT_USE_CLUSTERING,
  320. .proc_name = DRV_NAME,
  321. .dma_boundary = MV_DMA_BOUNDARY,
  322. .slave_configure = ata_scsi_slave_config,
  323. .bios_param = ata_std_bios_param,
  324. .ordered_flush = 1,
  325. };
  326. static const struct ata_port_operations mv5_ops = {
  327. .port_disable = ata_port_disable,
  328. .tf_load = ata_tf_load,
  329. .tf_read = ata_tf_read,
  330. .check_status = ata_check_status,
  331. .exec_command = ata_exec_command,
  332. .dev_select = ata_std_dev_select,
  333. .phy_reset = mv_phy_reset,
  334. .qc_prep = mv_qc_prep,
  335. .qc_issue = mv_qc_issue,
  336. .eng_timeout = mv_eng_timeout,
  337. .irq_handler = mv_interrupt,
  338. .irq_clear = mv_irq_clear,
  339. .scr_read = mv5_scr_read,
  340. .scr_write = mv5_scr_write,
  341. .port_start = mv_port_start,
  342. .port_stop = mv_port_stop,
  343. .host_stop = mv_host_stop,
  344. };
  345. static const struct ata_port_operations mv6_ops = {
  346. .port_disable = ata_port_disable,
  347. .tf_load = ata_tf_load,
  348. .tf_read = ata_tf_read,
  349. .check_status = ata_check_status,
  350. .exec_command = ata_exec_command,
  351. .dev_select = ata_std_dev_select,
  352. .phy_reset = mv_phy_reset,
  353. .qc_prep = mv_qc_prep,
  354. .qc_issue = mv_qc_issue,
  355. .eng_timeout = mv_eng_timeout,
  356. .irq_handler = mv_interrupt,
  357. .irq_clear = mv_irq_clear,
  358. .scr_read = mv_scr_read,
  359. .scr_write = mv_scr_write,
  360. .port_start = mv_port_start,
  361. .port_stop = mv_port_stop,
  362. .host_stop = mv_host_stop,
  363. };
  364. static struct ata_port_info mv_port_info[] = {
  365. { /* chip_504x */
  366. .sht = &mv_sht,
  367. .host_flags = MV_COMMON_FLAGS,
  368. .pio_mask = 0x1f, /* pio0-4 */
  369. .udma_mask = 0x7f, /* udma0-6 */
  370. .port_ops = &mv5_ops,
  371. },
  372. { /* chip_508x */
  373. .sht = &mv_sht,
  374. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  375. .pio_mask = 0x1f, /* pio0-4 */
  376. .udma_mask = 0x7f, /* udma0-6 */
  377. .port_ops = &mv5_ops,
  378. },
  379. { /* chip_5080 */
  380. .sht = &mv_sht,
  381. .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
  382. .pio_mask = 0x1f, /* pio0-4 */
  383. .udma_mask = 0x7f, /* udma0-6 */
  384. .port_ops = &mv5_ops,
  385. },
  386. { /* chip_604x */
  387. .sht = &mv_sht,
  388. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
  389. .pio_mask = 0x1f, /* pio0-4 */
  390. .udma_mask = 0x7f, /* udma0-6 */
  391. .port_ops = &mv6_ops,
  392. },
  393. { /* chip_608x */
  394. .sht = &mv_sht,
  395. .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
  396. MV_FLAG_DUAL_HC),
  397. .pio_mask = 0x1f, /* pio0-4 */
  398. .udma_mask = 0x7f, /* udma0-6 */
  399. .port_ops = &mv6_ops,
  400. },
  401. };
  402. static const struct pci_device_id mv_pci_tbl[] = {
  403. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
  404. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
  405. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_5080},
  406. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
  407. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
  408. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
  409. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
  410. {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
  411. {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x0241), 0, 0, chip_604x},
  412. {} /* terminate list */
  413. };
  414. static struct pci_driver mv_pci_driver = {
  415. .name = DRV_NAME,
  416. .id_table = mv_pci_tbl,
  417. .probe = mv_init_one,
  418. .remove = ata_pci_remove_one,
  419. };
  420. static const struct mv_hw_ops mv5xxx_ops = {
  421. .phy_errata = mv5_phy_errata,
  422. .enable_leds = mv5_enable_leds,
  423. .read_preamp = mv5_read_preamp,
  424. .reset_hc = mv5_reset_hc,
  425. .reset_flash = mv5_reset_flash,
  426. .reset_bus = mv5_reset_bus,
  427. };
  428. static const struct mv_hw_ops mv6xxx_ops = {
  429. .phy_errata = mv6_phy_errata,
  430. .enable_leds = mv6_enable_leds,
  431. .read_preamp = mv6_read_preamp,
  432. .reset_hc = mv6_reset_hc,
  433. .reset_flash = mv6_reset_flash,
  434. .reset_bus = mv_reset_pci_bus,
  435. };
  436. /*
  437. * Functions
  438. */
  439. static inline void writelfl(unsigned long data, void __iomem *addr)
  440. {
  441. writel(data, addr);
  442. (void) readl(addr); /* flush to avoid PCI posted write */
  443. }
  444. static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
  445. {
  446. return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
  447. }
  448. static inline unsigned int mv_hc_from_port(unsigned int port)
  449. {
  450. return port >> MV_PORT_HC_SHIFT;
  451. }
  452. static inline unsigned int mv_hardport_from_port(unsigned int port)
  453. {
  454. return port & MV_PORT_MASK;
  455. }
  456. static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
  457. unsigned int port)
  458. {
  459. return mv_hc_base(base, mv_hc_from_port(port));
  460. }
  461. static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
  462. {
  463. return mv_hc_base_from_port(base, port) +
  464. MV_SATAHC_ARBTR_REG_SZ +
  465. (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
  466. }
  467. static inline void __iomem *mv_ap_base(struct ata_port *ap)
  468. {
  469. return mv_port_base(ap->host_set->mmio_base, ap->port_no);
  470. }
  471. static inline int mv_get_hc_count(unsigned long host_flags)
  472. {
  473. return ((host_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
  474. }
  475. static void mv_irq_clear(struct ata_port *ap)
  476. {
  477. }
  478. /**
  479. * mv_start_dma - Enable eDMA engine
  480. * @base: port base address
  481. * @pp: port private data
  482. *
  483. * Verify the local cache of the eDMA state is accurate with an
  484. * assert.
  485. *
  486. * LOCKING:
  487. * Inherited from caller.
  488. */
  489. static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
  490. {
  491. if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
  492. writelfl(EDMA_EN, base + EDMA_CMD_OFS);
  493. pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
  494. }
  495. assert(EDMA_EN & readl(base + EDMA_CMD_OFS));
  496. }
  497. /**
  498. * mv_stop_dma - Disable eDMA engine
  499. * @ap: ATA channel to manipulate
  500. *
  501. * Verify the local cache of the eDMA state is accurate with an
  502. * assert.
  503. *
  504. * LOCKING:
  505. * Inherited from caller.
  506. */
  507. static void mv_stop_dma(struct ata_port *ap)
  508. {
  509. void __iomem *port_mmio = mv_ap_base(ap);
  510. struct mv_port_priv *pp = ap->private_data;
  511. u32 reg;
  512. int i;
  513. if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
  514. /* Disable EDMA if active. The disable bit auto clears.
  515. */
  516. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  517. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  518. } else {
  519. assert(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
  520. }
  521. /* now properly wait for the eDMA to stop */
  522. for (i = 1000; i > 0; i--) {
  523. reg = readl(port_mmio + EDMA_CMD_OFS);
  524. if (!(EDMA_EN & reg)) {
  525. break;
  526. }
  527. udelay(100);
  528. }
  529. if (EDMA_EN & reg) {
  530. printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
  531. /* FIXME: Consider doing a reset here to recover */
  532. }
  533. }
  534. #ifdef ATA_DEBUG
  535. static void mv_dump_mem(void __iomem *start, unsigned bytes)
  536. {
  537. int b, w;
  538. for (b = 0; b < bytes; ) {
  539. DPRINTK("%p: ", start + b);
  540. for (w = 0; b < bytes && w < 4; w++) {
  541. printk("%08x ",readl(start + b));
  542. b += sizeof(u32);
  543. }
  544. printk("\n");
  545. }
  546. }
  547. #endif
  548. static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
  549. {
  550. #ifdef ATA_DEBUG
  551. int b, w;
  552. u32 dw;
  553. for (b = 0; b < bytes; ) {
  554. DPRINTK("%02x: ", b);
  555. for (w = 0; b < bytes && w < 4; w++) {
  556. (void) pci_read_config_dword(pdev,b,&dw);
  557. printk("%08x ",dw);
  558. b += sizeof(u32);
  559. }
  560. printk("\n");
  561. }
  562. #endif
  563. }
  564. static void mv_dump_all_regs(void __iomem *mmio_base, int port,
  565. struct pci_dev *pdev)
  566. {
  567. #ifdef ATA_DEBUG
  568. void __iomem *hc_base = mv_hc_base(mmio_base,
  569. port >> MV_PORT_HC_SHIFT);
  570. void __iomem *port_base;
  571. int start_port, num_ports, p, start_hc, num_hcs, hc;
  572. if (0 > port) {
  573. start_hc = start_port = 0;
  574. num_ports = 8; /* shld be benign for 4 port devs */
  575. num_hcs = 2;
  576. } else {
  577. start_hc = port >> MV_PORT_HC_SHIFT;
  578. start_port = port;
  579. num_ports = num_hcs = 1;
  580. }
  581. DPRINTK("All registers for port(s) %u-%u:\n", start_port,
  582. num_ports > 1 ? num_ports - 1 : start_port);
  583. if (NULL != pdev) {
  584. DPRINTK("PCI config space regs:\n");
  585. mv_dump_pci_cfg(pdev, 0x68);
  586. }
  587. DPRINTK("PCI regs:\n");
  588. mv_dump_mem(mmio_base+0xc00, 0x3c);
  589. mv_dump_mem(mmio_base+0xd00, 0x34);
  590. mv_dump_mem(mmio_base+0xf00, 0x4);
  591. mv_dump_mem(mmio_base+0x1d00, 0x6c);
  592. for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
  593. hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
  594. DPRINTK("HC regs (HC %i):\n", hc);
  595. mv_dump_mem(hc_base, 0x1c);
  596. }
  597. for (p = start_port; p < start_port + num_ports; p++) {
  598. port_base = mv_port_base(mmio_base, p);
  599. DPRINTK("EDMA regs (port %i):\n",p);
  600. mv_dump_mem(port_base, 0x54);
  601. DPRINTK("SATA regs (port %i):\n",p);
  602. mv_dump_mem(port_base+0x300, 0x60);
  603. }
  604. #endif
  605. }
  606. static unsigned int mv_scr_offset(unsigned int sc_reg_in)
  607. {
  608. unsigned int ofs;
  609. switch (sc_reg_in) {
  610. case SCR_STATUS:
  611. case SCR_CONTROL:
  612. case SCR_ERROR:
  613. ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
  614. break;
  615. case SCR_ACTIVE:
  616. ofs = SATA_ACTIVE_OFS; /* active is not with the others */
  617. break;
  618. default:
  619. ofs = 0xffffffffU;
  620. break;
  621. }
  622. return ofs;
  623. }
  624. static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  625. {
  626. unsigned int ofs = mv_scr_offset(sc_reg_in);
  627. if (0xffffffffU != ofs) {
  628. return readl(mv_ap_base(ap) + ofs);
  629. } else {
  630. return (u32) ofs;
  631. }
  632. }
  633. static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  634. {
  635. unsigned int ofs = mv_scr_offset(sc_reg_in);
  636. if (0xffffffffU != ofs) {
  637. writelfl(val, mv_ap_base(ap) + ofs);
  638. }
  639. }
  640. /**
  641. * mv_host_stop - Host specific cleanup/stop routine.
  642. * @host_set: host data structure
  643. *
  644. * Disable ints, cleanup host memory, call general purpose
  645. * host_stop.
  646. *
  647. * LOCKING:
  648. * Inherited from caller.
  649. */
  650. static void mv_host_stop(struct ata_host_set *host_set)
  651. {
  652. struct mv_host_priv *hpriv = host_set->private_data;
  653. struct pci_dev *pdev = to_pci_dev(host_set->dev);
  654. if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
  655. pci_disable_msi(pdev);
  656. } else {
  657. pci_intx(pdev, 0);
  658. }
  659. kfree(hpriv);
  660. ata_host_stop(host_set);
  661. }
  662. static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
  663. {
  664. dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
  665. }
  666. /**
  667. * mv_port_start - Port specific init/start routine.
  668. * @ap: ATA channel to manipulate
  669. *
  670. * Allocate and point to DMA memory, init port private memory,
  671. * zero indices.
  672. *
  673. * LOCKING:
  674. * Inherited from caller.
  675. */
  676. static int mv_port_start(struct ata_port *ap)
  677. {
  678. struct device *dev = ap->host_set->dev;
  679. struct mv_port_priv *pp;
  680. void __iomem *port_mmio = mv_ap_base(ap);
  681. void *mem;
  682. dma_addr_t mem_dma;
  683. int rc = -ENOMEM;
  684. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  685. if (!pp)
  686. goto err_out;
  687. memset(pp, 0, sizeof(*pp));
  688. mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma,
  689. GFP_KERNEL);
  690. if (!mem)
  691. goto err_out_pp;
  692. memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
  693. rc = ata_pad_alloc(ap, dev);
  694. if (rc)
  695. goto err_out_priv;
  696. /* First item in chunk of DMA memory:
  697. * 32-slot command request table (CRQB), 32 bytes each in size
  698. */
  699. pp->crqb = mem;
  700. pp->crqb_dma = mem_dma;
  701. mem += MV_CRQB_Q_SZ;
  702. mem_dma += MV_CRQB_Q_SZ;
  703. /* Second item:
  704. * 32-slot command response table (CRPB), 8 bytes each in size
  705. */
  706. pp->crpb = mem;
  707. pp->crpb_dma = mem_dma;
  708. mem += MV_CRPB_Q_SZ;
  709. mem_dma += MV_CRPB_Q_SZ;
  710. /* Third item:
  711. * Table of scatter-gather descriptors (ePRD), 16 bytes each
  712. */
  713. pp->sg_tbl = mem;
  714. pp->sg_tbl_dma = mem_dma;
  715. writelfl(EDMA_CFG_Q_DEPTH | EDMA_CFG_RD_BRST_EXT |
  716. EDMA_CFG_WR_BUFF_LEN, port_mmio + EDMA_CFG_OFS);
  717. writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
  718. writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
  719. port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  720. writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
  721. writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
  722. writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
  723. writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
  724. port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  725. pp->req_producer = pp->rsp_consumer = 0;
  726. /* Don't turn on EDMA here...do it before DMA commands only. Else
  727. * we'll be unable to send non-data, PIO, etc due to restricted access
  728. * to shadow regs.
  729. */
  730. ap->private_data = pp;
  731. return 0;
  732. err_out_priv:
  733. mv_priv_free(pp, dev);
  734. err_out_pp:
  735. kfree(pp);
  736. err_out:
  737. return rc;
  738. }
  739. /**
  740. * mv_port_stop - Port specific cleanup/stop routine.
  741. * @ap: ATA channel to manipulate
  742. *
  743. * Stop DMA, cleanup port memory.
  744. *
  745. * LOCKING:
  746. * This routine uses the host_set lock to protect the DMA stop.
  747. */
  748. static void mv_port_stop(struct ata_port *ap)
  749. {
  750. struct device *dev = ap->host_set->dev;
  751. struct mv_port_priv *pp = ap->private_data;
  752. unsigned long flags;
  753. spin_lock_irqsave(&ap->host_set->lock, flags);
  754. mv_stop_dma(ap);
  755. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  756. ap->private_data = NULL;
  757. ata_pad_free(ap, dev);
  758. mv_priv_free(pp, dev);
  759. kfree(pp);
  760. }
  761. /**
  762. * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
  763. * @qc: queued command whose SG list to source from
  764. *
  765. * Populate the SG list and mark the last entry.
  766. *
  767. * LOCKING:
  768. * Inherited from caller.
  769. */
  770. static void mv_fill_sg(struct ata_queued_cmd *qc)
  771. {
  772. struct mv_port_priv *pp = qc->ap->private_data;
  773. unsigned int i = 0;
  774. struct scatterlist *sg;
  775. ata_for_each_sg(sg, qc) {
  776. dma_addr_t addr;
  777. u32 sg_len, len, offset;
  778. addr = sg_dma_address(sg);
  779. sg_len = sg_dma_len(sg);
  780. while (sg_len) {
  781. offset = addr & MV_DMA_BOUNDARY;
  782. len = sg_len;
  783. if ((offset + sg_len) > 0x10000)
  784. len = 0x10000 - offset;
  785. pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
  786. pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  787. pp->sg_tbl[i].flags_size = cpu_to_le32(len);
  788. sg_len -= len;
  789. addr += len;
  790. if (!sg_len && ata_sg_is_last(sg, qc))
  791. pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
  792. i++;
  793. }
  794. }
  795. }
  796. static inline unsigned mv_inc_q_index(unsigned *index)
  797. {
  798. *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
  799. return *index;
  800. }
  801. static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
  802. {
  803. *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
  804. (last ? CRQB_CMD_LAST : 0);
  805. }
  806. /**
  807. * mv_qc_prep - Host specific command preparation.
  808. * @qc: queued command to prepare
  809. *
  810. * This routine simply redirects to the general purpose routine
  811. * if command is not DMA. Else, it handles prep of the CRQB
  812. * (command request block), does some sanity checking, and calls
  813. * the SG load routine.
  814. *
  815. * LOCKING:
  816. * Inherited from caller.
  817. */
  818. static void mv_qc_prep(struct ata_queued_cmd *qc)
  819. {
  820. struct ata_port *ap = qc->ap;
  821. struct mv_port_priv *pp = ap->private_data;
  822. u16 *cw;
  823. struct ata_taskfile *tf;
  824. u16 flags = 0;
  825. if (ATA_PROT_DMA != qc->tf.protocol) {
  826. return;
  827. }
  828. /* the req producer index should be the same as we remember it */
  829. assert(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
  830. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  831. pp->req_producer);
  832. /* Fill in command request block
  833. */
  834. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  835. flags |= CRQB_FLAG_READ;
  836. }
  837. assert(MV_MAX_Q_DEPTH > qc->tag);
  838. flags |= qc->tag << CRQB_TAG_SHIFT;
  839. pp->crqb[pp->req_producer].sg_addr =
  840. cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
  841. pp->crqb[pp->req_producer].sg_addr_hi =
  842. cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
  843. pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
  844. cw = &pp->crqb[pp->req_producer].ata_cmd[0];
  845. tf = &qc->tf;
  846. /* Sadly, the CRQB cannot accomodate all registers--there are
  847. * only 11 bytes...so we must pick and choose required
  848. * registers based on the command. So, we drop feature and
  849. * hob_feature for [RW] DMA commands, but they are needed for
  850. * NCQ. NCQ will drop hob_nsect.
  851. */
  852. switch (tf->command) {
  853. case ATA_CMD_READ:
  854. case ATA_CMD_READ_EXT:
  855. case ATA_CMD_WRITE:
  856. case ATA_CMD_WRITE_EXT:
  857. mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
  858. break;
  859. #ifdef LIBATA_NCQ /* FIXME: remove this line when NCQ added */
  860. case ATA_CMD_FPDMA_READ:
  861. case ATA_CMD_FPDMA_WRITE:
  862. mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
  863. mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
  864. break;
  865. #endif /* FIXME: remove this line when NCQ added */
  866. default:
  867. /* The only other commands EDMA supports in non-queued and
  868. * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
  869. * of which are defined/used by Linux. If we get here, this
  870. * driver needs work.
  871. *
  872. * FIXME: modify libata to give qc_prep a return value and
  873. * return error here.
  874. */
  875. BUG_ON(tf->command);
  876. break;
  877. }
  878. mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
  879. mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
  880. mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
  881. mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
  882. mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
  883. mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
  884. mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
  885. mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
  886. mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
  887. if (!(qc->flags & ATA_QCFLAG_DMAMAP)) {
  888. return;
  889. }
  890. mv_fill_sg(qc);
  891. }
  892. /**
  893. * mv_qc_issue - Initiate a command to the host
  894. * @qc: queued command to start
  895. *
  896. * This routine simply redirects to the general purpose routine
  897. * if command is not DMA. Else, it sanity checks our local
  898. * caches of the request producer/consumer indices then enables
  899. * DMA and bumps the request producer index.
  900. *
  901. * LOCKING:
  902. * Inherited from caller.
  903. */
  904. static int mv_qc_issue(struct ata_queued_cmd *qc)
  905. {
  906. void __iomem *port_mmio = mv_ap_base(qc->ap);
  907. struct mv_port_priv *pp = qc->ap->private_data;
  908. u32 in_ptr;
  909. if (ATA_PROT_DMA != qc->tf.protocol) {
  910. /* We're about to send a non-EDMA capable command to the
  911. * port. Turn off EDMA so there won't be problems accessing
  912. * shadow block, etc registers.
  913. */
  914. mv_stop_dma(qc->ap);
  915. return ata_qc_issue_prot(qc);
  916. }
  917. in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  918. /* the req producer index should be the same as we remember it */
  919. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  920. pp->req_producer);
  921. /* until we do queuing, the queue should be empty at this point */
  922. assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  923. ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >>
  924. EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
  925. mv_inc_q_index(&pp->req_producer); /* now incr producer index */
  926. mv_start_dma(port_mmio, pp);
  927. /* and write the request in pointer to kick the EDMA to life */
  928. in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
  929. in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
  930. writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
  931. return 0;
  932. }
  933. /**
  934. * mv_get_crpb_status - get status from most recently completed cmd
  935. * @ap: ATA channel to manipulate
  936. *
  937. * This routine is for use when the port is in DMA mode, when it
  938. * will be using the CRPB (command response block) method of
  939. * returning command completion information. We assert indices
  940. * are good, grab status, and bump the response consumer index to
  941. * prove that we're up to date.
  942. *
  943. * LOCKING:
  944. * Inherited from caller.
  945. */
  946. static u8 mv_get_crpb_status(struct ata_port *ap)
  947. {
  948. void __iomem *port_mmio = mv_ap_base(ap);
  949. struct mv_port_priv *pp = ap->private_data;
  950. u32 out_ptr;
  951. out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  952. /* the response consumer index should be the same as we remember it */
  953. assert(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  954. pp->rsp_consumer);
  955. /* increment our consumer index... */
  956. pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
  957. /* and, until we do NCQ, there should only be 1 CRPB waiting */
  958. assert(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >>
  959. EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
  960. pp->rsp_consumer);
  961. /* write out our inc'd consumer index so EDMA knows we're caught up */
  962. out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
  963. out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
  964. writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
  965. /* Return ATA status register for completed CRPB */
  966. return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
  967. }
  968. /**
  969. * mv_err_intr - Handle error interrupts on the port
  970. * @ap: ATA channel to manipulate
  971. *
  972. * In most cases, just clear the interrupt and move on. However,
  973. * some cases require an eDMA reset, which is done right before
  974. * the COMRESET in mv_phy_reset(). The SERR case requires a
  975. * clear of pending errors in the SATA SERROR register. Finally,
  976. * if the port disabled DMA, update our cached copy to match.
  977. *
  978. * LOCKING:
  979. * Inherited from caller.
  980. */
  981. static void mv_err_intr(struct ata_port *ap)
  982. {
  983. void __iomem *port_mmio = mv_ap_base(ap);
  984. u32 edma_err_cause, serr = 0;
  985. edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  986. if (EDMA_ERR_SERR & edma_err_cause) {
  987. serr = scr_read(ap, SCR_ERROR);
  988. scr_write_flush(ap, SCR_ERROR, serr);
  989. }
  990. if (EDMA_ERR_SELF_DIS & edma_err_cause) {
  991. struct mv_port_priv *pp = ap->private_data;
  992. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  993. }
  994. DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
  995. "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
  996. /* Clear EDMA now that SERR cleanup done */
  997. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  998. /* check for fatal here and recover if needed */
  999. if (EDMA_ERR_FATAL & edma_err_cause) {
  1000. mv_stop_and_reset(ap);
  1001. }
  1002. }
  1003. /**
  1004. * mv_host_intr - Handle all interrupts on the given host controller
  1005. * @host_set: host specific structure
  1006. * @relevant: port error bits relevant to this host controller
  1007. * @hc: which host controller we're to look at
  1008. *
  1009. * Read then write clear the HC interrupt status then walk each
  1010. * port connected to the HC and see if it needs servicing. Port
  1011. * success ints are reported in the HC interrupt status reg, the
  1012. * port error ints are reported in the higher level main
  1013. * interrupt status register and thus are passed in via the
  1014. * 'relevant' argument.
  1015. *
  1016. * LOCKING:
  1017. * Inherited from caller.
  1018. */
  1019. static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
  1020. unsigned int hc)
  1021. {
  1022. void __iomem *mmio = host_set->mmio_base;
  1023. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1024. struct ata_port *ap;
  1025. struct ata_queued_cmd *qc;
  1026. u32 hc_irq_cause;
  1027. int shift, port, port0, hard_port, handled;
  1028. unsigned int err_mask;
  1029. u8 ata_status = 0;
  1030. if (hc == 0) {
  1031. port0 = 0;
  1032. } else {
  1033. port0 = MV_PORTS_PER_HC;
  1034. }
  1035. /* we'll need the HC success int register in most cases */
  1036. hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
  1037. if (hc_irq_cause) {
  1038. writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
  1039. }
  1040. VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
  1041. hc,relevant,hc_irq_cause);
  1042. for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
  1043. ap = host_set->ports[port];
  1044. hard_port = port & MV_PORT_MASK; /* range 0-3 */
  1045. handled = 0; /* ensure ata_status is set if handled++ */
  1046. if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
  1047. /* new CRPB on the queue; just one at a time until NCQ
  1048. */
  1049. ata_status = mv_get_crpb_status(ap);
  1050. handled++;
  1051. } else if ((DEV_IRQ << hard_port) & hc_irq_cause) {
  1052. /* received ATA IRQ; read the status reg to clear INTRQ
  1053. */
  1054. ata_status = readb((void __iomem *)
  1055. ap->ioaddr.status_addr);
  1056. handled++;
  1057. }
  1058. err_mask = ac_err_mask(ata_status);
  1059. shift = port << 1; /* (port * 2) */
  1060. if (port >= MV_PORTS_PER_HC) {
  1061. shift++; /* skip bit 8 in the HC Main IRQ reg */
  1062. }
  1063. if ((PORT0_ERR << shift) & relevant) {
  1064. mv_err_intr(ap);
  1065. err_mask |= AC_ERR_OTHER;
  1066. handled++;
  1067. }
  1068. if (handled && ap) {
  1069. qc = ata_qc_from_tag(ap, ap->active_tag);
  1070. if (NULL != qc) {
  1071. VPRINTK("port %u IRQ found for qc, "
  1072. "ata_status 0x%x\n", port,ata_status);
  1073. /* mark qc status appropriately */
  1074. ata_qc_complete(qc, err_mask);
  1075. }
  1076. }
  1077. }
  1078. VPRINTK("EXIT\n");
  1079. }
  1080. /**
  1081. * mv_interrupt -
  1082. * @irq: unused
  1083. * @dev_instance: private data; in this case the host structure
  1084. * @regs: unused
  1085. *
  1086. * Read the read only register to determine if any host
  1087. * controllers have pending interrupts. If so, call lower level
  1088. * routine to handle. Also check for PCI errors which are only
  1089. * reported here.
  1090. *
  1091. * LOCKING:
  1092. * This routine holds the host_set lock while processing pending
  1093. * interrupts.
  1094. */
  1095. static irqreturn_t mv_interrupt(int irq, void *dev_instance,
  1096. struct pt_regs *regs)
  1097. {
  1098. struct ata_host_set *host_set = dev_instance;
  1099. unsigned int hc, handled = 0, n_hcs;
  1100. void __iomem *mmio = host_set->mmio_base;
  1101. u32 irq_stat;
  1102. irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
  1103. /* check the cases where we either have nothing pending or have read
  1104. * a bogus register value which can indicate HW removal or PCI fault
  1105. */
  1106. if (!irq_stat || (0xffffffffU == irq_stat)) {
  1107. return IRQ_NONE;
  1108. }
  1109. n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
  1110. spin_lock(&host_set->lock);
  1111. for (hc = 0; hc < n_hcs; hc++) {
  1112. u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
  1113. if (relevant) {
  1114. mv_host_intr(host_set, relevant, hc);
  1115. handled++;
  1116. }
  1117. }
  1118. if (PCI_ERR & irq_stat) {
  1119. printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
  1120. readl(mmio + PCI_IRQ_CAUSE_OFS));
  1121. DPRINTK("All regs @ PCI error\n");
  1122. mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
  1123. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1124. handled++;
  1125. }
  1126. spin_unlock(&host_set->lock);
  1127. return IRQ_RETVAL(handled);
  1128. }
  1129. static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
  1130. {
  1131. void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
  1132. unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
  1133. return hc_mmio + ofs;
  1134. }
  1135. static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
  1136. {
  1137. unsigned int ofs;
  1138. switch (sc_reg_in) {
  1139. case SCR_STATUS:
  1140. case SCR_ERROR:
  1141. case SCR_CONTROL:
  1142. ofs = sc_reg_in * sizeof(u32);
  1143. break;
  1144. default:
  1145. ofs = 0xffffffffU;
  1146. break;
  1147. }
  1148. return ofs;
  1149. }
  1150. static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
  1151. {
  1152. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1153. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1154. if (ofs != 0xffffffffU)
  1155. return readl(mmio + ofs);
  1156. else
  1157. return (u32) ofs;
  1158. }
  1159. static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
  1160. {
  1161. void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
  1162. unsigned int ofs = mv5_scr_offset(sc_reg_in);
  1163. if (ofs != 0xffffffffU)
  1164. writelfl(val, mmio + ofs);
  1165. }
  1166. static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio)
  1167. {
  1168. u8 rev_id;
  1169. int early_5080;
  1170. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1171. early_5080 = (pdev->device == 0x5080) && (rev_id == 0);
  1172. if (!early_5080) {
  1173. u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1174. tmp |= (1 << 0);
  1175. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1176. }
  1177. mv_reset_pci_bus(pdev, mmio);
  1178. }
  1179. static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1180. {
  1181. writel(0x0fcfffff, mmio + MV_FLASH_CTL);
  1182. }
  1183. static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
  1184. void __iomem *mmio)
  1185. {
  1186. void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
  1187. u32 tmp;
  1188. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1189. hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
  1190. hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
  1191. }
  1192. static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1193. {
  1194. u32 tmp;
  1195. writel(0, mmio + MV_GPIO_PORT_CTL);
  1196. /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
  1197. tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1198. tmp |= ~(1 << 0);
  1199. writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
  1200. }
  1201. static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1202. unsigned int port)
  1203. {
  1204. void __iomem *phy_mmio = mv5_phy_base(mmio, port);
  1205. const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
  1206. u32 tmp;
  1207. int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
  1208. if (fix_apm_sq) {
  1209. tmp = readl(phy_mmio + MV5_LT_MODE);
  1210. tmp |= (1 << 19);
  1211. writel(tmp, phy_mmio + MV5_LT_MODE);
  1212. tmp = readl(phy_mmio + MV5_PHY_CTL);
  1213. tmp &= ~0x3;
  1214. tmp |= 0x1;
  1215. writel(tmp, phy_mmio + MV5_PHY_CTL);
  1216. }
  1217. tmp = readl(phy_mmio + MV5_PHY_MODE);
  1218. tmp &= ~mask;
  1219. tmp |= hpriv->signal[port].pre;
  1220. tmp |= hpriv->signal[port].amps;
  1221. writel(tmp, phy_mmio + MV5_PHY_MODE);
  1222. }
  1223. #undef ZERO
  1224. #define ZERO(reg) writel(0, port_mmio + (reg))
  1225. static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
  1226. unsigned int port)
  1227. {
  1228. void __iomem *port_mmio = mv_port_base(mmio, port);
  1229. writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
  1230. mv_channel_reset(hpriv, mmio, port);
  1231. ZERO(0x028); /* command */
  1232. writel(0x11f, port_mmio + EDMA_CFG_OFS);
  1233. ZERO(0x004); /* timer */
  1234. ZERO(0x008); /* irq err cause */
  1235. ZERO(0x00c); /* irq err mask */
  1236. ZERO(0x010); /* rq bah */
  1237. ZERO(0x014); /* rq inp */
  1238. ZERO(0x018); /* rq outp */
  1239. ZERO(0x01c); /* respq bah */
  1240. ZERO(0x024); /* respq outp */
  1241. ZERO(0x020); /* respq inp */
  1242. ZERO(0x02c); /* test control */
  1243. writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
  1244. }
  1245. #undef ZERO
  1246. #define ZERO(reg) writel(0, hc_mmio + (reg))
  1247. static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1248. unsigned int hc)
  1249. {
  1250. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1251. u32 tmp;
  1252. ZERO(0x00c);
  1253. ZERO(0x010);
  1254. ZERO(0x014);
  1255. ZERO(0x018);
  1256. tmp = readl(hc_mmio + 0x20);
  1257. tmp &= 0x1c1c1c1c;
  1258. tmp |= 0x03030303;
  1259. writel(tmp, hc_mmio + 0x20);
  1260. }
  1261. #undef ZERO
  1262. static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1263. unsigned int n_hc)
  1264. {
  1265. unsigned int hc, port;
  1266. for (hc = 0; hc < n_hc; hc++) {
  1267. for (port = 0; port < MV_PORTS_PER_HC; port++)
  1268. mv5_reset_hc_port(hpriv, mmio,
  1269. (hc * MV_PORTS_PER_HC) + port);
  1270. mv5_reset_one_hc(hpriv, mmio, hc);
  1271. }
  1272. return 0;
  1273. }
  1274. #undef ZERO
  1275. #define ZERO(reg) writel(0, mmio + (reg))
  1276. static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio)
  1277. {
  1278. u32 tmp;
  1279. tmp = readl(mmio + MV_PCI_MODE);
  1280. tmp &= 0xff00ffff;
  1281. writel(tmp, mmio + MV_PCI_MODE);
  1282. ZERO(MV_PCI_DISC_TIMER);
  1283. ZERO(MV_PCI_MSI_TRIGGER);
  1284. writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
  1285. ZERO(HC_MAIN_IRQ_MASK_OFS);
  1286. ZERO(MV_PCI_SERR_MASK);
  1287. ZERO(PCI_IRQ_CAUSE_OFS);
  1288. ZERO(PCI_IRQ_MASK_OFS);
  1289. ZERO(MV_PCI_ERR_LOW_ADDRESS);
  1290. ZERO(MV_PCI_ERR_HIGH_ADDRESS);
  1291. ZERO(MV_PCI_ERR_ATTRIBUTE);
  1292. ZERO(MV_PCI_ERR_COMMAND);
  1293. }
  1294. #undef ZERO
  1295. static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
  1296. {
  1297. u32 tmp;
  1298. mv5_reset_flash(hpriv, mmio);
  1299. tmp = readl(mmio + MV_GPIO_PORT_CTL);
  1300. tmp &= 0x3;
  1301. tmp |= (1 << 5) | (1 << 6);
  1302. writel(tmp, mmio + MV_GPIO_PORT_CTL);
  1303. }
  1304. /**
  1305. * mv6_reset_hc - Perform the 6xxx global soft reset
  1306. * @mmio: base address of the HBA
  1307. *
  1308. * This routine only applies to 6xxx parts.
  1309. *
  1310. * LOCKING:
  1311. * Inherited from caller.
  1312. */
  1313. static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
  1314. unsigned int n_hc)
  1315. {
  1316. void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
  1317. int i, rc = 0;
  1318. u32 t;
  1319. /* Following procedure defined in PCI "main command and status
  1320. * register" table.
  1321. */
  1322. t = readl(reg);
  1323. writel(t | STOP_PCI_MASTER, reg);
  1324. for (i = 0; i < 1000; i++) {
  1325. udelay(1);
  1326. t = readl(reg);
  1327. if (PCI_MASTER_EMPTY & t) {
  1328. break;
  1329. }
  1330. }
  1331. if (!(PCI_MASTER_EMPTY & t)) {
  1332. printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
  1333. rc = 1;
  1334. goto done;
  1335. }
  1336. /* set reset */
  1337. i = 5;
  1338. do {
  1339. writel(t | GLOB_SFT_RST, reg);
  1340. t = readl(reg);
  1341. udelay(1);
  1342. } while (!(GLOB_SFT_RST & t) && (i-- > 0));
  1343. if (!(GLOB_SFT_RST & t)) {
  1344. printk(KERN_ERR DRV_NAME ": can't set global reset\n");
  1345. rc = 1;
  1346. goto done;
  1347. }
  1348. /* clear reset and *reenable the PCI master* (not mentioned in spec) */
  1349. i = 5;
  1350. do {
  1351. writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
  1352. t = readl(reg);
  1353. udelay(1);
  1354. } while ((GLOB_SFT_RST & t) && (i-- > 0));
  1355. if (GLOB_SFT_RST & t) {
  1356. printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
  1357. rc = 1;
  1358. }
  1359. done:
  1360. return rc;
  1361. }
  1362. static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
  1363. void __iomem *mmio)
  1364. {
  1365. void __iomem *port_mmio;
  1366. u32 tmp;
  1367. tmp = readl(mmio + MV_RESET_CFG);
  1368. if ((tmp & (1 << 0)) == 0) {
  1369. hpriv->signal[idx].amps = 0x7 << 8;
  1370. hpriv->signal[idx].pre = 0x1 << 5;
  1371. return;
  1372. }
  1373. port_mmio = mv_port_base(mmio, idx);
  1374. tmp = readl(port_mmio + PHY_MODE2);
  1375. hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
  1376. hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
  1377. }
  1378. static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
  1379. {
  1380. writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
  1381. }
  1382. static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
  1383. unsigned int port)
  1384. {
  1385. void __iomem *port_mmio = mv_port_base(mmio, port);
  1386. u32 hp_flags = hpriv->hp_flags;
  1387. int fix_phy_mode2 =
  1388. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1389. int fix_phy_mode4 =
  1390. hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
  1391. u32 m2, tmp;
  1392. if (fix_phy_mode2) {
  1393. m2 = readl(port_mmio + PHY_MODE2);
  1394. m2 &= ~(1 << 16);
  1395. m2 |= (1 << 31);
  1396. writel(m2, port_mmio + PHY_MODE2);
  1397. udelay(200);
  1398. m2 = readl(port_mmio + PHY_MODE2);
  1399. m2 &= ~((1 << 16) | (1 << 31));
  1400. writel(m2, port_mmio + PHY_MODE2);
  1401. udelay(200);
  1402. }
  1403. /* who knows what this magic does */
  1404. tmp = readl(port_mmio + PHY_MODE3);
  1405. tmp &= ~0x7F800000;
  1406. tmp |= 0x2A800000;
  1407. writel(tmp, port_mmio + PHY_MODE3);
  1408. if (fix_phy_mode4) {
  1409. u32 m4;
  1410. m4 = readl(port_mmio + PHY_MODE4);
  1411. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1412. tmp = readl(port_mmio + 0x310);
  1413. m4 = (m4 & ~(1 << 1)) | (1 << 0);
  1414. writel(m4, port_mmio + PHY_MODE4);
  1415. if (hp_flags & MV_HP_ERRATA_60X1B2)
  1416. writel(tmp, port_mmio + 0x310);
  1417. }
  1418. /* Revert values of pre-emphasis and signal amps to the saved ones */
  1419. m2 = readl(port_mmio + PHY_MODE2);
  1420. m2 &= ~MV_M2_PREAMP_MASK;
  1421. m2 |= hpriv->signal[port].amps;
  1422. m2 |= hpriv->signal[port].pre;
  1423. m2 &= ~(1 << 16);
  1424. writel(m2, port_mmio + PHY_MODE2);
  1425. }
  1426. static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
  1427. unsigned int port_no)
  1428. {
  1429. void __iomem *port_mmio = mv_port_base(mmio, port_no);
  1430. writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
  1431. if (IS_60XX(hpriv)) {
  1432. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1433. ifctl |= (1 << 12) | (1 << 7);
  1434. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1435. }
  1436. udelay(25); /* allow reset propagation */
  1437. /* Spec never mentions clearing the bit. Marvell's driver does
  1438. * clear the bit, however.
  1439. */
  1440. writelfl(0, port_mmio + EDMA_CMD_OFS);
  1441. hpriv->ops->phy_errata(hpriv, mmio, port_no);
  1442. if (IS_50XX(hpriv))
  1443. mdelay(1);
  1444. }
  1445. static void mv_stop_and_reset(struct ata_port *ap)
  1446. {
  1447. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1448. void __iomem *mmio = ap->host_set->mmio_base;
  1449. mv_stop_dma(ap);
  1450. mv_channel_reset(hpriv, mmio, ap->port_no);
  1451. __mv_phy_reset(ap, 0);
  1452. }
  1453. static inline void __msleep(unsigned int msec, int can_sleep)
  1454. {
  1455. if (can_sleep)
  1456. msleep(msec);
  1457. else
  1458. mdelay(msec);
  1459. }
  1460. /**
  1461. * __mv_phy_reset - Perform eDMA reset followed by COMRESET
  1462. * @ap: ATA channel to manipulate
  1463. *
  1464. * Part of this is taken from __sata_phy_reset and modified to
  1465. * not sleep since this routine gets called from interrupt level.
  1466. *
  1467. * LOCKING:
  1468. * Inherited from caller. This is coded to safe to call at
  1469. * interrupt level, i.e. it does not sleep.
  1470. */
  1471. static void __mv_phy_reset(struct ata_port *ap, int can_sleep)
  1472. {
  1473. struct mv_port_priv *pp = ap->private_data;
  1474. struct mv_host_priv *hpriv = ap->host_set->private_data;
  1475. void __iomem *port_mmio = mv_ap_base(ap);
  1476. struct ata_taskfile tf;
  1477. struct ata_device *dev = &ap->device[0];
  1478. unsigned long timeout;
  1479. int retry = 5;
  1480. u32 sstatus;
  1481. VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
  1482. DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
  1483. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1484. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1485. /* Issue COMRESET via SControl */
  1486. comreset_retry:
  1487. scr_write_flush(ap, SCR_CONTROL, 0x301);
  1488. __msleep(1, can_sleep);
  1489. scr_write_flush(ap, SCR_CONTROL, 0x300);
  1490. __msleep(20, can_sleep);
  1491. timeout = jiffies + msecs_to_jiffies(200);
  1492. do {
  1493. sstatus = scr_read(ap, SCR_STATUS) & 0x3;
  1494. if ((sstatus == 3) || (sstatus == 0))
  1495. break;
  1496. __msleep(1, can_sleep);
  1497. } while (time_before(jiffies, timeout));
  1498. /* work around errata */
  1499. if (IS_60XX(hpriv) &&
  1500. (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
  1501. (retry-- > 0))
  1502. goto comreset_retry;
  1503. DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
  1504. "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
  1505. mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
  1506. if (sata_dev_present(ap)) {
  1507. ata_port_probe(ap);
  1508. } else {
  1509. printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
  1510. ap->id, scr_read(ap, SCR_STATUS));
  1511. ata_port_disable(ap);
  1512. return;
  1513. }
  1514. ap->cbl = ATA_CBL_SATA;
  1515. /* even after SStatus reflects that device is ready,
  1516. * it seems to take a while for link to be fully
  1517. * established (and thus Status no longer 0x80/0x7F),
  1518. * so we poll a bit for that, here.
  1519. */
  1520. retry = 20;
  1521. while (1) {
  1522. u8 drv_stat = ata_check_status(ap);
  1523. if ((drv_stat != 0x80) && (drv_stat != 0x7f))
  1524. break;
  1525. __msleep(500, can_sleep);
  1526. if (retry-- <= 0)
  1527. break;
  1528. }
  1529. tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
  1530. tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
  1531. tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
  1532. tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
  1533. dev->class = ata_dev_classify(&tf);
  1534. if (!ata_dev_present(dev)) {
  1535. VPRINTK("Port disabled post-sig: No device present.\n");
  1536. ata_port_disable(ap);
  1537. }
  1538. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1539. pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
  1540. VPRINTK("EXIT\n");
  1541. }
  1542. static void mv_phy_reset(struct ata_port *ap)
  1543. {
  1544. __mv_phy_reset(ap, 1);
  1545. }
  1546. /**
  1547. * mv_eng_timeout - Routine called by libata when SCSI times out I/O
  1548. * @ap: ATA channel to manipulate
  1549. *
  1550. * Intent is to clear all pending error conditions, reset the
  1551. * chip/bus, fail the command, and move on.
  1552. *
  1553. * LOCKING:
  1554. * This routine holds the host_set lock while failing the command.
  1555. */
  1556. static void mv_eng_timeout(struct ata_port *ap)
  1557. {
  1558. struct ata_queued_cmd *qc;
  1559. unsigned long flags;
  1560. printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
  1561. DPRINTK("All regs @ start of eng_timeout\n");
  1562. mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
  1563. to_pci_dev(ap->host_set->dev));
  1564. qc = ata_qc_from_tag(ap, ap->active_tag);
  1565. printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
  1566. ap->host_set->mmio_base, ap, qc, qc->scsicmd,
  1567. &qc->scsicmd->cmnd);
  1568. mv_err_intr(ap);
  1569. mv_stop_and_reset(ap);
  1570. if (!qc) {
  1571. printk(KERN_ERR "ata%u: BUG: timeout without command\n",
  1572. ap->id);
  1573. } else {
  1574. /* hack alert! We cannot use the supplied completion
  1575. * function from inside the ->eh_strategy_handler() thread.
  1576. * libata is the only user of ->eh_strategy_handler() in
  1577. * any kernel, so the default scsi_done() assumes it is
  1578. * not being called from the SCSI EH.
  1579. */
  1580. spin_lock_irqsave(&ap->host_set->lock, flags);
  1581. qc->scsidone = scsi_finish_command;
  1582. ata_qc_complete(qc, AC_ERR_OTHER);
  1583. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1584. }
  1585. }
  1586. /**
  1587. * mv_port_init - Perform some early initialization on a single port.
  1588. * @port: libata data structure storing shadow register addresses
  1589. * @port_mmio: base address of the port
  1590. *
  1591. * Initialize shadow register mmio addresses, clear outstanding
  1592. * interrupts on the port, and unmask interrupts for the future
  1593. * start of the port.
  1594. *
  1595. * LOCKING:
  1596. * Inherited from caller.
  1597. */
  1598. static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
  1599. {
  1600. unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
  1601. unsigned serr_ofs;
  1602. /* PIO related setup
  1603. */
  1604. port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
  1605. port->error_addr =
  1606. port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
  1607. port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
  1608. port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
  1609. port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
  1610. port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
  1611. port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
  1612. port->status_addr =
  1613. port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
  1614. /* special case: control/altstatus doesn't have ATA_REG_ address */
  1615. port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
  1616. /* unused: */
  1617. port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
  1618. /* Clear any currently outstanding port interrupt conditions */
  1619. serr_ofs = mv_scr_offset(SCR_ERROR);
  1620. writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
  1621. writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
  1622. /* unmask all EDMA error interrupts */
  1623. writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
  1624. VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
  1625. readl(port_mmio + EDMA_CFG_OFS),
  1626. readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
  1627. readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
  1628. }
  1629. static int mv_chip_id(struct pci_dev *pdev, struct mv_host_priv *hpriv,
  1630. unsigned int board_idx)
  1631. {
  1632. u8 rev_id;
  1633. u32 hp_flags = hpriv->hp_flags;
  1634. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1635. switch(board_idx) {
  1636. case chip_5080:
  1637. hpriv->ops = &mv5xxx_ops;
  1638. hp_flags |= MV_HP_50XX;
  1639. switch (rev_id) {
  1640. case 0x1:
  1641. hp_flags |= MV_HP_ERRATA_50XXB0;
  1642. break;
  1643. case 0x3:
  1644. hp_flags |= MV_HP_ERRATA_50XXB2;
  1645. break;
  1646. default:
  1647. dev_printk(KERN_WARNING, &pdev->dev,
  1648. "Applying 50XXB2 workarounds to unknown rev\n");
  1649. hp_flags |= MV_HP_ERRATA_50XXB2;
  1650. break;
  1651. }
  1652. break;
  1653. case chip_504x:
  1654. case chip_508x:
  1655. hpriv->ops = &mv5xxx_ops;
  1656. hp_flags |= MV_HP_50XX;
  1657. switch (rev_id) {
  1658. case 0x0:
  1659. hp_flags |= MV_HP_ERRATA_50XXB0;
  1660. break;
  1661. case 0x3:
  1662. hp_flags |= MV_HP_ERRATA_50XXB2;
  1663. break;
  1664. default:
  1665. dev_printk(KERN_WARNING, &pdev->dev,
  1666. "Applying B2 workarounds to unknown rev\n");
  1667. hp_flags |= MV_HP_ERRATA_50XXB2;
  1668. break;
  1669. }
  1670. break;
  1671. case chip_604x:
  1672. case chip_608x:
  1673. hpriv->ops = &mv6xxx_ops;
  1674. switch (rev_id) {
  1675. case 0x7:
  1676. hp_flags |= MV_HP_ERRATA_60X1B2;
  1677. break;
  1678. case 0x9:
  1679. hp_flags |= MV_HP_ERRATA_60X1C0;
  1680. break;
  1681. default:
  1682. dev_printk(KERN_WARNING, &pdev->dev,
  1683. "Applying B2 workarounds to unknown rev\n");
  1684. hp_flags |= MV_HP_ERRATA_60X1B2;
  1685. break;
  1686. }
  1687. break;
  1688. default:
  1689. printk(KERN_ERR DRV_NAME ": BUG: invalid board index %u\n", board_idx);
  1690. return 1;
  1691. }
  1692. hpriv->hp_flags = hp_flags;
  1693. return 0;
  1694. }
  1695. /**
  1696. * mv_init_host - Perform some early initialization of the host.
  1697. * @pdev: host PCI device
  1698. * @probe_ent: early data struct representing the host
  1699. *
  1700. * If possible, do an early global reset of the host. Then do
  1701. * our port init and clear/unmask all/relevant host interrupts.
  1702. *
  1703. * LOCKING:
  1704. * Inherited from caller.
  1705. */
  1706. static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
  1707. unsigned int board_idx)
  1708. {
  1709. int rc = 0, n_hc, port, hc;
  1710. void __iomem *mmio = probe_ent->mmio_base;
  1711. struct mv_host_priv *hpriv = probe_ent->private_data;
  1712. /* global interrupt mask */
  1713. writel(0, mmio + HC_MAIN_IRQ_MASK_OFS);
  1714. rc = mv_chip_id(pdev, hpriv, board_idx);
  1715. if (rc)
  1716. goto done;
  1717. n_hc = mv_get_hc_count(probe_ent->host_flags);
  1718. probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
  1719. for (port = 0; port < probe_ent->n_ports; port++)
  1720. hpriv->ops->read_preamp(hpriv, port, mmio);
  1721. rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
  1722. if (rc)
  1723. goto done;
  1724. hpriv->ops->reset_flash(hpriv, mmio);
  1725. hpriv->ops->reset_bus(pdev, mmio);
  1726. hpriv->ops->enable_leds(hpriv, mmio);
  1727. for (port = 0; port < probe_ent->n_ports; port++) {
  1728. if (IS_60XX(hpriv)) {
  1729. void __iomem *port_mmio = mv_port_base(mmio, port);
  1730. u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
  1731. ifctl |= (1 << 12);
  1732. writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
  1733. }
  1734. hpriv->ops->phy_errata(hpriv, mmio, port);
  1735. }
  1736. for (port = 0; port < probe_ent->n_ports; port++) {
  1737. void __iomem *port_mmio = mv_port_base(mmio, port);
  1738. mv_port_init(&probe_ent->port[port], port_mmio);
  1739. }
  1740. for (hc = 0; hc < n_hc; hc++) {
  1741. void __iomem *hc_mmio = mv_hc_base(mmio, hc);
  1742. VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
  1743. "(before clear)=0x%08x\n", hc,
  1744. readl(hc_mmio + HC_CFG_OFS),
  1745. readl(hc_mmio + HC_IRQ_CAUSE_OFS));
  1746. /* Clear any currently outstanding hc interrupt conditions */
  1747. writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
  1748. }
  1749. /* Clear any currently outstanding host interrupt conditions */
  1750. writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
  1751. /* and unmask interrupt generation for host regs */
  1752. writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
  1753. writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
  1754. VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
  1755. "PCI int cause/mask=0x%08x/0x%08x\n",
  1756. readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
  1757. readl(mmio + HC_MAIN_IRQ_MASK_OFS),
  1758. readl(mmio + PCI_IRQ_CAUSE_OFS),
  1759. readl(mmio + PCI_IRQ_MASK_OFS));
  1760. done:
  1761. return rc;
  1762. }
  1763. /**
  1764. * mv_print_info - Dump key info to kernel log for perusal.
  1765. * @probe_ent: early data struct representing the host
  1766. *
  1767. * FIXME: complete this.
  1768. *
  1769. * LOCKING:
  1770. * Inherited from caller.
  1771. */
  1772. static void mv_print_info(struct ata_probe_ent *probe_ent)
  1773. {
  1774. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1775. struct mv_host_priv *hpriv = probe_ent->private_data;
  1776. u8 rev_id, scc;
  1777. const char *scc_s;
  1778. /* Use this to determine the HW stepping of the chip so we know
  1779. * what errata to workaround
  1780. */
  1781. pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
  1782. pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
  1783. if (scc == 0)
  1784. scc_s = "SCSI";
  1785. else if (scc == 0x01)
  1786. scc_s = "RAID";
  1787. else
  1788. scc_s = "unknown";
  1789. dev_printk(KERN_INFO, &pdev->dev,
  1790. "%u slots %u ports %s mode IRQ via %s\n",
  1791. (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
  1792. scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
  1793. }
  1794. /**
  1795. * mv_init_one - handle a positive probe of a Marvell host
  1796. * @pdev: PCI device found
  1797. * @ent: PCI device ID entry for the matched host
  1798. *
  1799. * LOCKING:
  1800. * Inherited from caller.
  1801. */
  1802. static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1803. {
  1804. static int printed_version = 0;
  1805. struct ata_probe_ent *probe_ent = NULL;
  1806. struct mv_host_priv *hpriv;
  1807. unsigned int board_idx = (unsigned int)ent->driver_data;
  1808. void __iomem *mmio_base;
  1809. int pci_dev_busy = 0, rc;
  1810. if (!printed_version++)
  1811. dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
  1812. rc = pci_enable_device(pdev);
  1813. if (rc) {
  1814. return rc;
  1815. }
  1816. rc = pci_request_regions(pdev, DRV_NAME);
  1817. if (rc) {
  1818. pci_dev_busy = 1;
  1819. goto err_out;
  1820. }
  1821. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1822. if (probe_ent == NULL) {
  1823. rc = -ENOMEM;
  1824. goto err_out_regions;
  1825. }
  1826. memset(probe_ent, 0, sizeof(*probe_ent));
  1827. probe_ent->dev = pci_dev_to_dev(pdev);
  1828. INIT_LIST_HEAD(&probe_ent->node);
  1829. mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
  1830. if (mmio_base == NULL) {
  1831. rc = -ENOMEM;
  1832. goto err_out_free_ent;
  1833. }
  1834. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1835. if (!hpriv) {
  1836. rc = -ENOMEM;
  1837. goto err_out_iounmap;
  1838. }
  1839. memset(hpriv, 0, sizeof(*hpriv));
  1840. probe_ent->sht = mv_port_info[board_idx].sht;
  1841. probe_ent->host_flags = mv_port_info[board_idx].host_flags;
  1842. probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
  1843. probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
  1844. probe_ent->port_ops = mv_port_info[board_idx].port_ops;
  1845. probe_ent->irq = pdev->irq;
  1846. probe_ent->irq_flags = SA_SHIRQ;
  1847. probe_ent->mmio_base = mmio_base;
  1848. probe_ent->private_data = hpriv;
  1849. /* initialize adapter */
  1850. rc = mv_init_host(pdev, probe_ent, board_idx);
  1851. if (rc) {
  1852. goto err_out_hpriv;
  1853. }
  1854. /* Enable interrupts */
  1855. if (pci_enable_msi(pdev) == 0) {
  1856. hpriv->hp_flags |= MV_HP_FLAG_MSI;
  1857. } else {
  1858. pci_intx(pdev, 1);
  1859. }
  1860. mv_dump_pci_cfg(pdev, 0x68);
  1861. mv_print_info(probe_ent);
  1862. if (ata_device_add(probe_ent) == 0) {
  1863. rc = -ENODEV; /* No devices discovered */
  1864. goto err_out_dev_add;
  1865. }
  1866. kfree(probe_ent);
  1867. return 0;
  1868. err_out_dev_add:
  1869. if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
  1870. pci_disable_msi(pdev);
  1871. } else {
  1872. pci_intx(pdev, 0);
  1873. }
  1874. err_out_hpriv:
  1875. kfree(hpriv);
  1876. err_out_iounmap:
  1877. pci_iounmap(pdev, mmio_base);
  1878. err_out_free_ent:
  1879. kfree(probe_ent);
  1880. err_out_regions:
  1881. pci_release_regions(pdev);
  1882. err_out:
  1883. if (!pci_dev_busy) {
  1884. pci_disable_device(pdev);
  1885. }
  1886. return rc;
  1887. }
  1888. static int __init mv_init(void)
  1889. {
  1890. return pci_module_init(&mv_pci_driver);
  1891. }
  1892. static void __exit mv_exit(void)
  1893. {
  1894. pci_unregister_driver(&mv_pci_driver);
  1895. }
  1896. MODULE_AUTHOR("Brett Russ");
  1897. MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
  1898. MODULE_LICENSE("GPL");
  1899. MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
  1900. MODULE_VERSION(DRV_VERSION);
  1901. module_init(mv_init);
  1902. module_exit(mv_exit);